# $Id: notebook.wgt,v 1.10 2002/10/10 06:36:32 cgavin Exp $

##############################################################################
#
# Visual TCL - A cross-platform application development environment
#
# Copyright (C) 2001 Christian Gavin
#
# Description file for [Incr Widgets]
#
# 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		Notebook
Lib		itcl

CreateCmd	::iwidgets::notebook
Icon		icon_notebook.gif
DumpChildren	no
MegaWidget	yes
ClassOption     -_itcl_notebook_pages
InsertCmd	vTcl::widgets::iwidgets::notebook::insertCmd
SelectSiteCmd   vTcl::widgets::iwidgets::notebook::selectSiteCmd
DeleteSiteCmd   vTcl::widgets::iwidgets::notebook::deleteSiteCmd
TagsCmd         vTcl:lib_itcl:tagscmd
TreeChildrenCmd	{vTcl::widgets::iwidgets::notebook::treeChildrenCmd
		 vTcl::widgets::iwidgets::notebook::treeChildrenChildsite}
DumpCmd		vTcl::widgets::iwidgets::notebook::dumpCmd
DumpInfoCmd	vTcl::widgets::iwidgets::notebook::dumpInfoCmd

#############################
## Right-click menu

Function        "Edit Pages..."         vTcl::widgets::iwidgets::notebook::editCmd

###################################################
# for compatibility with 1.60 and previous versions

proc vTcl:widget:notebook:treeChildrenChildsite {target} {
    return [vTcl::widgets::iwidgets::notebook::treeChildrenChildsite $target]
}

namespace eval vTcl::widgets::iwidgets::notebook {

    proc editCmd {} {
        set target $::vTcl(w,widget)
	::vTcl::itemEdit::edit $target vTcl::widgets::iwidgets::notebooks::edit
    }

    proc insertCmd {target} {
        $target add -label {Page 1}
        $target add -label {Page 2}
        $target add -label {Page 3}
        $target select 0
	foreach site [$target childsite] {
	    vTcl::widgets::iwidgets::notebooks::edit::saveBkgnd $site
	}
    }

    proc deleteSiteCmd {target site} {
        set index [lsearch -exact [$target childsite] $site]

        if {$index != -1} {
            $target delete $index
            $target select 0
        }
    }

    proc selectSiteCmd {target site} {
        set index [lsearch -exact [$target childsite] $site]

        if {$index != -1} {
            $target select $index
        }
    }

    # All right, I explain it: -2 is the number of levels
    # in the window path between the megawidget and its
    # pages (childsites). This is used by the widget tree
    # to skip 2 levels (otherwise it would draw 2 vertical
    # lines)

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

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

        return $children
    }

    proc treeChildrenChildsite {target} {
        return [$target childsite]
    }

    proc dumpCmd {target basename} {
        global vTcl
        set result [vTcl:dump_widget_opt $target $basename]
        set entries [$target childsite]
        set size [llength $entries]
        for {set i 0} {$i < $size} {incr i} {
            set conf [$target pageconfigure $i]
            set pairs [vTcl:get_subopts_special $conf $target]
            append result "$vTcl(tab)$basename add \\\n"
            append result "[vTcl:clean_pairs $pairs]\n"
        }
        for {set i 0} {$i < $size} {incr i} {
            set page [lindex $entries $i]
            append result "$vTcl(tab)"
            set sitevariable "site_[llength [split $page .]]_$i"
            append result "set $sitevariable "
            append result "\[lindex \[$basename childsite\] $i\]\n"
            append result [vTcl:lib_itcl:dump_subwidgets $page \$$sitevariable]
        }
        append result "$vTcl(tab)$basename select 0\n"
        return $result
    }

    proc dumpInfoCmd {target basename} {
        global vTcl classes
        set result [vTcl:dump:widget_info $target $basename]
        set entries [$target childsite]
        set size [llength $entries]
        for {set i 0} {$i < $size} {incr i} {
            set page [lindex $entries $i]
            append result "$vTcl(tab)"
            set sitevariable "site_[llength [split $page .]]_$i"
            append result "set $sitevariable "
            append result "\[lindex \[$basename childsite\] $i\]\n"
            append result [$classes(Frame,dumpInfoCmd) $page \$$sitevariable]
        }
        return $result
    }
}

