# Copyright (c) 2010 ancestor <xmpp: ancestor@jabber.org>
# Tabbed tkCon plugin
# This plugin contains some graphics of KDE 3.5.8 <http://kde.org>

namespace eval tabbedcon {
    if {[catch {package require tkcon}]} {
        return
    }

    variable tbicon
    variable tabico
    variable contab

    ::msgcat::mcload [file join [file dirname [info script]] msgs]

    set tbicon "[file dirname [info script]]/pixmaps/console22.png"
    set tabico "[file dirname [info script]]/pixmaps/console16.png"

    hook::add finload_hook [namespace current]::replace_tkcon_menu_func 35
}

########################################################################################

proc tabbedcon::replace_tkcon_menu_func {} {
    if {!$::usetabbar} {
        return
    }

    variable tbicon
    variable tabico

    catch {
        package require Img
        
        image create photo plugins/tabbedcon/toolbar -file $tbicon
        
        ::ifacetk::add_toolbar_button plugins/tabbedcon/toolbar \
            [namespace current]::show_tabbed_console \
            [::msgcat::mc "Console"]
        
        image create photo plugins/tabbedcon/tabbar -file $tabico
    }

    rename ::plugins::tkcon::add_tkcon_to_tkabber_menu ::plugins::tkcon::add_tkcon_to_tkabber_menu_old

    proc ::plugins::tkcon::add_tkcon_to_tkabber_menu {args} {
        set menu [.mainframe getmenu debug]
        
        $menu add command -label [::msgcat::mc "Show TkCon console"] \
            -command ::plugins::tabbedcon::show_tabbed_console
    }
}

proc tabbedcon::set_con_focus {} {
    if {[info exists ::tkcon::PRIV(curtab)]} {
        focus $::tkcon::PRIV(curtab)
    }
}

proc tabbedcon::get_tabs_compound {} {
    set compound none

    foreach tab [.nb pages] {
        set compound [lindex [.nb.b:$tab configure -compound] end]
        
        if {$compound != "none"} {
            return $compound
        }
    }

    return $compound
}

proc tabbedcon::reconfig_menu {m} {
    variable contab

    foreach mni {{Main Console} {New Console} \
                {New Tab} {Delete Tab}} {
        
        set midx [$m index $mni]
        
        $m entryconfigure $midx -state disabled
    }

    set midx [$m index {Close Console}]

    $m entryconfigure $midx -command [list ::ifacetk::destroy_tab $contab]
}

proc tabbedcon::restore_menu {m} {
    foreach mni {{New Console} {New Tab}} {
        set midx [$m index $mni]
        
        $m entryconfigure $midx -state normal
    }

    set midx [$m index {Close Console}]

    $m entryconfigure $midx -command [list ::tkcon::Destroy]
}

proc tabbedcon::create_pseudo_menu {frm} {
    foreach mnl [winfo children .tkcon.mbar] {
        switch -glob -- $mnl {
            *.pop -
            *.file -
            *.context   {continue}
        }
        
        set mnn [winfo name $mnl]
        
        set m $frm.$mnn.menu
        
        set mnb [menubutton $frm.$mnn \
            -text [string totitle $mnn] \
            -anchor nw \
            -menu $m]
            
        $mnl clone $m
        
        if {$mnn == "console"} {
            reconfig_menu $m
        }
        
        pack $mnb -side left
    }
}

proc tabbedcon::show_tabbed_console {} {
    variable contab

    set w .tbcon
    
    if {[winfo exists $w]} {
        .nb activate $contab
        return
    }

    tkcon hide

    add_win $w -title [::msgcat::mc "Console"] \
	   -tabtitle [::msgcat::mc "Console"] \
	   -class TabbedCon \
       -raisecmd [list [namespace current]::set_con_focus] \
	   -raise 1

    set contab [lindex [.nb pages] end]

    set tabimg plugins/tabbedcon/tabbar

    set compound [get_tabs_compound]

    if {[lsearch [image names] $tabimg] != -1 && $compound != "none"} {
        .nb.b:$contab configure -image $tabimg \
            -compound $compound
    }

    set mf [frame $w.mf]
    pack $mf -side top -anchor w -fill x -pady 2

    set tools [frame $w.tools]
    pack $tools -side bottom -anchor nw -fill x -pady 5

    button $tools.detach -text [::msgcat::mc "Detach"] \
        -command [namespace current]::detach_console
        
    pack $tools.detach -side left -anchor w

    button $tools.clear -text [::msgcat::mc "Clear"] \
        -command {clear; ::tkcon::Prompt}
        
    pack $tools.clear -side left -anchor w

    if {![info exists ::tkcon::PRIV(root)]} {
        set ::tkcon::PRIV(showOnStartup) 0
        set ::tkcon::PRIV(protocol) {tkcon hide}
        set ::tkcon::PRIV(root) .tkcon
        set ::tkcon::OPT(exec) ""
    }

    if {![winfo exists $::tkcon::PRIV(root)]} {
        ::tkcon::Init
    }

    create_pseudo_menu $mf

    set tbf [frame $w.tbf]
    set nt [::tkcon::InitTab $tbf]

    pack $tbf -fill both -expand yes
    pack $nt -fill both -expand yes

    ::tkcon::GotoTab $nt

    set sy [scrollbar $nt.sy -takefocus 0 -command [list $nt yview]]

    $nt configure -yscrollcommand [list $sy set]
    $nt configure -font Terminus

    pack $sy -side right -fill both

    bind $nt <Destroy> {
        ::tkcon::DeleteTab $::tkcon::PRIV(curtab)
    }
}

proc tabbedcon::detach_console {} {
    set atab [.nb raise]

    ::ifacetk::destroy_tab $atab

    restore_menu .tkcon.mbar.console
    
    tkcon show
}
