# $Id: tnotebook.wgt,v 1.1 2012/02/20 03:52:34 rozen Exp rozen $

##############################################################################
#
# Visual TCL - A cross-platform application development environment
#
# Copyright (C) 2001 Damon Courtney
#
# Description file for Bwidgets notebook
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software

# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

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

Class       TNotebook
Lib     ttk

TtkWidget       yes

Icon        icon_BwTabnotebook.gif
TreeLabel   Themed Notebook
#CreateCmd   ttk::notebook
MegaWidget  yes
DefaultOptions  -width 300 -height 200
DefaultValues   -background -foreground -font
ClassOption     -_ttk_notebook_pages
ClassOption     -_ttk_notebook_position            ;# Rozen
#ClassOption     -closetab
CreateCmd       vTcl::widgets::ttk::notebook::createCmd  
InsertCmd       vTcl::widgets::ttk::notebook::insertCmd
DeleteCmd       vTcl::widgets::ttk::notebook::deleteCmd
TreeChildrenCmd {vTcl::widgets::ttk::notebook::treeChildrenCmd
         vTcl::widgets::ttk::notebook::treeChildrenChildsite}
SelectSiteCmd   vTcl::widgets::ttk::notebook::selectSiteCmd
DeleteSiteCmd   vTcl::widgets::ttk::notebook::deleteSiteCmd
DumpCmd         vTcl::widgets::ttk::notebook::dumpCmd
DumpInfoCmd     vTcl::widgets::ttk::notebook::dumpInfoCmd
GetImagesCmd    vTcl::widgets::ttk::notebook::getImagesCmd

DoubleClickCmd  vTcl::widgets::ttk::notebook::dblClkCmd

lappend vTcl(complex_class) TNotebook

VirtualEvents <<NotebookTabChanged>>

## Tabbed notebook support
NewOption -_ttk_notebook_pages  "pages"  combobox
# All of the commands below are to be found in lib/lib_ttk.tcl
OptionConfigCmd -_ttk_notebook_pages     config     vTcl::widgets::ttk::config_pages
OptionConfigCmd -_ttk_notebook_pages     update     vTcl::widgets::ttk::update_pages
OptionConfigCmd -_ttk_notebook_pages     get        vTcl::widgets::ttk::get_pages
OptionConfigCmd -_ttk_notebook_pages     select     vTcl::widgets::ttk::select_page
OptionConfigCmd -_ttk_notebook_pages     editArg    vTcl::widgets::ttk::notebook::edit
# The editor for notebook tabs is located in lib/lib_core.tcl
OptionConfigCmd -_ttk_notebook_pages     edit       vTcl::itemEdit::edit

## Page-specific options
NewOption -createcmd        "create cmd"        command
NewOption -raisecmd     "raise cmd"     command
NewOption -leavecmd     "leave cmd"     command
NewOption -closetab     "close tabs"    boolean "0 1"       ;# Rozen 12/10/17
#SpecialOpt -state "state" choice {normal disabled hidden}

NewOption -_ttk_notebook_position "tab position" choice \
	{"" nw n ne en e es se s sw ws w wn}

# I just copied the stuff from above for -_ttk_notebook_pages. The
# following 3 functions are in lib_ttk.tcl.

OptionConfigCmd -_ttk_notebook_position     config     vTcl::widgets::ttk::config_position
OptionConfigCmd -_ttk_notebook_position     update     vTcl::widgets::ttk::update_position
OptionConfigCmd -_ttk_notebook_position     get        vTcl::widgets::ttk::get_position
#OptionConfigCmd -_ttk_notebook_position     select     vTcl::widgets::ttk::select_position

namespace eval ::vTcl::widgets::ttk::notebook::edit {

    # set close_file [file join $vTcl(VTCL_HOME) images "close_inactive.gif"]
    # variable close_image [image create photo -file $close_file]

    proc getTitle {target} {
        global ::widget
        set alias $::widget(rev,$target)
        return "Edit tabs for $alias"
    }

    proc getLabelOption {} {
        return -text
    }

    proc getItems {target} {
        ## first item in the list is the current index
        # Rozen, I think that this just returns the list of tab names.
        set pages [$target tabs]
        #set current [$target select]
        set current [lsearch $pages [$target select]] ;# 'cause select returns
                                                      # a page name not an index
        set values $current
        set count 0
        foreach page $pages {
            lappend values [$target tab $count -text]
            incr count
        }
        return $values
    }

    proc addItem {target} {
        # Make sure that the new tab widget does not repeat any tabs
        # that may have existed in this notebook in this run of PAGE.
        global vTcl
        global widget
		variable close_image
        namespace eval ::notebook::${target} {variable t_numbers }
        set pages [$target tabs]
        set ll [llength $pages]
        for {set i 0} {$i < $ll} {incr i} {
            set page_i [lrange $pages $i $i]
            regexp {\.t(\d*)$} $page_i trash tab_number
            lappend ::notebook::${target}::t_numbers $tab_number
        }
        set tab_no 0
        set list_t_num ::notebook::${target}::t_numbers
        set list_t_num [set $list_t_num]
        while {$tab_no in $list_t_num} {
            incr tab_no
        }
        set newpage "$target.t$tab_no"
		if {$vTcl(current_theme) eq "page-legacy"} {
			$target add [frame $newpage -background $vTcl(actual_gui_bg)] \
				-text {New Tab} -compound left
		} else {
			$target add [frame $newpage] \
				-text {New Tab} -compound left
		}			
        $target select $newpage
        set was {}
        if {[info exists widget(rev,$target)]} {
            set was $widget(rev,$target)
        }
        vTcl:set_alias $target $was
        vTcl:init_wtree
        return "New Tab"
    }

    proc removeItem {target index} {
        namespace eval ::notebook::${target} {variable t_numbers }
        lappend ::notebook::${target}::t_numbers $index
        $target forget $index
        vTcl:init_wtree
    }

    proc itemConfigure {target index args} {
        global vTcl
		# Think that this is wrong because [target select xxx] does
		# not return a value so page wile be "".
		set page [$target select $index]
		
		if {$args == ""} {
            set conf1 [$target tab $index]
            set conf_len [llength $conf1]
            foreach {c1 c2} $conf1 {
                set p [list $c1 $c2]
                lappend conf_pair $p
            }
            return $conf_pair
        } else {
            eval $target tab $page $index $args
        }
    }

    proc debug_dump_conf {target} {
        # Show configure of all tabs.
        set tabs [$target tabs]
        dpr tabs
        foreach tab $tabs {
            dpr tab
            set conf [$target tab $tab]
            dpr -s conf
        }
    }

    proc moveUpOrDown {target index direction} {
        set offset(up) -1
        set offset(down) 1
        set pages [$target tabs]
        set length [llength $pages]
        $target insert $index [expr ($index + $offset($direction)) % $length]
        vTcl:init_wtree
    }
}

Function "Edit Tabs..."    vTcl::widgets::ttk::notebook::editCmd
Function "Fill Container"  vTcl::widgets::ttk::notebook::fill_container

namespace eval vTcl::widgets::ttk::notebook {

	global vTcl

    variable close_file \
		[file join $vTcl(VTCL_HOME) images "close_inactive.gif"]
    variable close_image [image create photo -file $close_file]

	proc createCmd {args} {
		global vTcl
		# if {$vTcl(current_theme) eq "default"} {
		# 	ttk::style configure TFrame -background $vTcl(actual_gui_bg)
		# }
		ttk::notebook {*}$args
	}		

	
    proc fill_container {} {
         # Rozen function to expand widget to fill container.
         set w $::vTcl(w,widget)
         vTcl:fill_container $w
    }

    proc editCmd {} {
		
        # Called from the context menu for the tnotebook.
        set target $::vTcl(w,widget)
        # ::vTcl::itemEdit::edit located in lib_core.tcl
        ::vTcl::itemEdit::edit $target ::vTcl::widgets::ttk::notebook::edit
    }

    proc deleteCmd {target} {
        bindtags $target "nothing"
        #NoteBook::_destroy $target
        destroy $target
    }

    proc insertCmd {target} {
        # Generates first two tabs. Called from vTcl:create_widget
        global vTcl
		variable close_image
		if {$vTcl(current_theme) eq "page-legacy"} {
			$target add [frame $target.t0 -background $vTcl(actual_gui_bg)] \
				-text {Page 1} -compound left 
			$target add [frame $target.t1 -background $vTcl(actual_gui_bg)] \
				-text {Page 2} -compound left 
		} else {
			$target add [frame $target.t0 ] \
				-text {Page 1} -compound left 
			$target add [frame $target.t1 ] \
				-text {Page 2} -compound left 
		}
        $target select 0
        $target configure -takefocus ""
        set vTcl($target,tabcnt) 2
    }

    # All right, I explain it: 0 is the number of extra levels
    # in the window path between the megawidget and its
    # pages (childsites).

    proc treeChildrenCmd {target {diff \#0}} {
        set children ""
        set wantsdiff [expr {$diff != ""}]

        foreach site [treeChildrenChildsite $target] {
            foreach child [::vTcl:complete_widget_tree $site $wantsdiff] {
                lappend children $child$diff
            }
        }

        return $children
    }

    proc treeChildrenChildsite {target} {
        set pages [$target tabs]
        set sites ""
        foreach page $pages {
            lappend sites $page
        }
        return $sites
    }

    proc selectSiteCmd {target site} {
        set pages [$target tabs]
        foreach page $pages {
            if {$page == $site} {
				$target select $page
				break
            }
        }
    }

    proc deleteSiteCmd {target site} {
        set pages [$target pages]
        foreach page $pages {
           if {$page == $site} {
                $target delete $page
                $target raise [$target pages 0]
                break
            }
        }
    }

    proc dumpCmd {target basename} {
        global vTcl
        set result [vTcl:dump_widget_opt $target $basename]
        set pages [$target tabs]
        set i 0
        foreach page $pages {
            set pairs [$target tab $page]
            set last [lindex [split $page .] end]
            append result [vTcl:dump_widget_opt $page $basename.$last]
            #set page [vTcl:replace_fixed_name page target]
            if {$vTcl(copy)} {
                # Need to change the path of the page to the new value.
                # format: regsub ?switches? exp string subSpec
                # ?varName?  Searches string for exp and substitutes
                # subSpec in variable name.
                regsub $vTcl(copy_target) $page "\$target" page
            } else {
                # In order to implement borrow, need to remove fixed
                # target and use the basename which has $top as
                # containing window.
                regsub $target $page $basename page
            }
            # Put in image stuff before adding the page.
            # See if we have an image on the page.

            append result "$::vTcl(tab)$basename add $page \\\n"
            append result "[vTcl:clean_pairs $pairs]\n"


            append result "$::vTcl(tab)"
            set site [lindex $pages $i]            ;# new
            set sitevariable "site_[llength [split $site .]]_$i"
            append result "set $sitevariable "
             set last [lindex [split $page .] end]   ;# new
            append result " $basename.$last\n"      ;# new
			# vTcl::widgets::ttk::dump_subwidgets located in lib_ttk.tcl
            append result \
                [vTcl::widgets::ttk::dump_subwidgets $site \$$sitevariable]
            incr i
        }
		update
        return $result
    }


    proc dumpInfoCmd {target basename} {
        global classes
        set result [vTcl:dump:widget_info $target $basename]
        set pages [$target tabs]
        set i 0
        foreach page $pages {

            set site [lindex $pages $i]
            append result "$::vTcl(tab)"
            set sitevariable "site_[llength [split $site .]]_$i"
            append result "set $sitevariable $page \n"
            append result [$classes(Frame,dumpInfoCmd) $site \$$sitevariable]
            incr i
        }
#        set size  [llength $pages]
#        for {set i 0} {$i < $size} {incr i} {
#            #set site [$target getframe [lindex $pages $i]]
#            set site [lindex $pages $i]
#            append result "$::vTcl(tab)"
#            set sitevariable "site_[llength [split $site .]]_$i"
#            append result "set $sitevariable "
#            append result "\[lindex $pages $i]\n"
##            append result "\[$basename getframe [lindex $pages $i]\]\n"
#            append result [$classes(Frame,dumpInfoCmd) $site \$$sitevariable]
#        }
        return $result
    }

    proc getImagesCmd {target} {
        set result {}
        #set pages [$target pages]
        set pages [$target tabs]
        foreach page $pages {
            set image [$target tab $page -image]
            if {$image != ""} {
                lappend result $image
            }
        }
        return $result
    }

    proc dblClkCmd {target} {
		# The tab editor is in lib_core.tcl
        ::vTcl::itemEdit::edit $target \
	          ::vTcl::widgets::ttk::notebook::edit
    }

}
