#!/bin/sh
#Toolbars  -*- TCL -*- Created: ramsan Jun-2000, Modified: ramsan Jun-2000


#----------------------Toolbars ALONE---------------------------
# the next line restarts using wish \
	exec wish8.0 "$0" "$@"

set ToolbarsRunningAlone 0
catch {
    if {$argv0 == [info script] } {
	set ToolbarsRunningAlone 1
	set tcl_interactive 0
	set tk_strictMotif 1
	wm withdraw .

	if { [catch { set GIDDEFAULT $env(GIDDEFAULT)}]!=1 } {
	    set GIDDEFAULTTCL $GIDDEFAULT
	    catch { set GIDDEFAULTTCL $env(GIDDEFAULTTCL) }
	    set auto_path "$GIDDEFAULTTCL"
	    source $GIDDEFAULTTCL/init.tcl
	    source $GIDDEFAULTTCL/tk.tcl
	}
    }
}

#  LOOK AT THE END OF THE FILE !!!
#-------------------------------------------------------------------

proc TOExecuteSelection { w } {
    global ToolbarsPriv
    if { $ToolbarsPriv(inout) == "" } { return }

    set aa [$w.bars.t curselection]
    if { $aa == "" } {
	return
    }

    set traslatedname [$w.bars.t get $aa]
    set found 0
    foreach i $ToolbarsPriv(toolbars) {
	if { [llength $i] == "2" } {
	    #old compatibility, previous versions not exists field 3 title, used field 0 name
	    set title [lindex $i 0]
	} else {
	    set title [lindex $i 3]
	}
	if { $traslatedname == $title } {
	    set found 1
	    break
	}
    }
    if { !$found } {
	WarnWinText [_ "Toolbar %s not found" $traslatedname]
	return
    }
    set comm [lindex $i 2]

    switch $ToolbarsPriv(inout) {
	inside {
	    if { $ToolbarsPriv(lrtb) == "" } {
		set type INSIDE
	    } else {
		switch $ToolbarsPriv(lrtb) {
		    l {
		        set type INSIDELEFT
		    }
		    r {
		        set type INSIDERIGHT
		    }
		    t {
		        set type INSIDETOP
		    }
		    b {
		        set type INSIDEBOTTOM
		    }
		}
	    }
	}
	outside { 
	    set type OPEN
	}
	none {
	    set type NONE
	}
    }
    eval $comm $type
}

proc TOConnectPosButtonsCB { w } {
    global ToolbarsPriv GiDPrivToolBar

    trace vdelete ToolbarsPriv(inout) w "TOConnectPosButtonsCB $w ;#"
    trace vdelete ToolbarsPriv(lrtb) w "TOExecuteSelection $w ;#"
    if { $ToolbarsPriv(inout) == "inside" } {
	set aa [$w.bars.t curselection]
	if { $aa == "" } { 
	    trace var ToolbarsPriv(inout) w "TOConnectPosButtonsCB $w ;#"
	    trace var ToolbarsPriv(lrtb) w "TOExecuteSelection $w ;#"
	    return
	}
	set name [$w.bars.t get $aa]
	if { [string match *bar $name] } {
	    TOConnectPosButtons $w normal
	    if { $ToolbarsPriv(lrtb) == "" } {
		set ToolbarsPriv(lrtb) t
	    }
	} else {
	    set ToolbarsPriv(lrtb) ""
	    TOConnectPosButtons $w disabled
	}
    } else {
	set ToolbarsPriv(lrtb) ""
	TOConnectPosButtons $w disabled
    }
    TOExecuteSelection $w
    trace var ToolbarsPriv(inout) w "TOConnectPosButtonsCB $w ;#"
    trace var ToolbarsPriv(lrtb) w "TOExecuteSelection $w ;#"
}

proc TOConnectPosButtons { w state} {
    global ToolbarsPriv

    foreach i [winfo children $w.middle.f2] {
	$i conf -state $state
    }
}

proc TOCheckCurrentBar { w } {
    global ToolbarsPriv GiDPrivToolBar GidPriv
    
    trace vdelete ToolbarsPriv(inout) w "TOConnectPosButtonsCB $w ;#"
    trace vdelete ToolbarsPriv(lrtb) w "TOExecuteSelection $w ;#"

    set aa [$w.bars.t curselection]
    if { $aa == "" } {
	$w.middle.f2.l conf -state disabled
	set ToolbarsPriv(inout) none
	TOConnectPosButtons $w disabled
	trace var ToolbarsPriv(inout) w "TOConnectPosButtonsCB $w ;#"
	trace var ToolbarsPriv(lrtb) w "TOExecuteSelection $w ;#"
	return
    }
    set traslatedname [$w.bars.t get $aa]
    set found 0
    foreach i $ToolbarsPriv(toolbars) {
	if { [llength $i] == "2" } {
	    #old compatibility, previous versions not exists field 3 title, used field 0 name
	    set title [lindex $i 0]
	} else {
	    set title [lindex $i 3]
	}
	if { $traslatedname == $title } {
	    set found 1
	    break
	}
    }
    if { !$found } {
	WarnWinText [_ "Toolbar %s not found" $traslatedname]
	return
    }

    set name [lindex $i 0]
    set geomname [lindex $i 1]
    if { [info exists GidPriv($geomname)] } {
	set type [lindex $GidPriv($geomname) 0]
    } elseif { [string match *bar $name] } {
	set type INSIDELEFT
    } else {
	set type INSIDE
    }

    switch -glob $type {
	INSIDE {
	    set ToolbarsPriv(inout) inside
	    set ToolbarsPriv(lrtb) ""
	    TOConnectPosButtons $w disabled
	}
	NONE {
	    set ToolbarsPriv(inout) none
	    set ToolbarsPriv(lrtb) ""
	    TOConnectPosButtons $w disabled
	}
	OPEN {
	    set ToolbarsPriv(inout) outside
	    set ToolbarsPriv(lrtb) ""
	    TOConnectPosButtons $w disabled
	}
	INSIDETOP* {
	    set ToolbarsPriv(inout) inside
	    set ToolbarsPriv(lrtb) t
	    TOConnectPosButtons $w normal
	}
	INSIDELEFT* {
	    set ToolbarsPriv(inout) inside
	    set ToolbarsPriv(lrtb) l
	    TOConnectPosButtons $w normal
	}
	INSIDEBOTTOM* {
	    set ToolbarsPriv(inout) inside
	    set ToolbarsPriv(lrtb) b
	    TOConnectPosButtons $w normal
	}
	INSIDERIGHT* {
	    set ToolbarsPriv(inout) inside
	    set ToolbarsPriv(lrtb) r
	    TOConnectPosButtons $w normal
	}
	default {
	    WarnWin [_ "Unknown position: --%s--" $type]
	}
    }
    trace var ToolbarsPriv(inout) w "TOConnectPosButtonsCB $w ;#"
    trace var ToolbarsPriv(lrtb) w "TOExecuteSelection $w ;#"
}


proc TOInitializeToolbars {} {
    global ToolbarsPriv

    if { ![info exists ToolbarsPriv(toolbars)] } {
	set ToolbarsPriv(toolbars) [list \
		[list {Right buttons} PrePostRightButWindowGeom RightButtons [_ "Right buttons"]] \
		[list {Command line} PrePostEntryWindowGeom BottomEntryFrame [_ "Command line"]] \
		[list {Up menu} PrePostTopMenuWindowGeom TopMenuFrame [_ "Up menu"]] \
		[list {Geometry & View bar} PrePostBitmapsWindowGeom CreateBitmaps [_ "Geometry & View bar"]] \
		[list {Standard bar} PrePostStdBarWindowGeom StdBitmaps [_ "Standard bar"]] ]
    }
    
}

proc AddNewToolbar { name geomvarname command {title ""} } {
    global ToolbarsPriv
    if { $title == "" } { set title $name }

    if { ![string match *bar $name] } {
	WarnWin [_ "Names of the toolbars must end with 'bar'"]
    }
    TOInitializeToolbars
    
    foreach i $ToolbarsPriv(toolbars) {
	if { $name == [lindex $i 0] } { return }
    }

    lappend ToolbarsPriv(toolbars) [list $name $geomvarname $command $title]
}

proc ReleaseToolbar { name } {
    global ToolbarsPriv

    TOInitializeToolbars
    
    set newlist ""
    foreach i $ToolbarsPriv(toolbars) {
	if { $name != [lindex $i 0] } {
	    lappend newlist $i
	}
    }
    set ToolbarsPriv(toolbars) $newlist
}


proc TOBegin { {w .gid.toolbars } } {
    global ToolbarsPriv GIDDEFAULT

    InitWindow $w [_ "Toolbars position"] ToolbarsWindowGeom TOBegin "" 1

    frame $w.bars

    listbox $w.bars.t -relief sunken \
	    -yscroll "$w.bars.yscroll set" -width 35 -height 5 -exportselection 0
    $w.bars.t delete 0 end
    scrollbar $w.bars.yscroll -relief sunken \
	    -orient vertical -command "$w.bars.t yview"

    GidHelp "$w.bars.t $w.bars.yscroll" [_ "Select here the toolbar that you want \
	    to activate or deactivate"]

    grid $w.bars.t  -row 1 -column 2 -sticky nsew
    grid $w.bars.yscroll  -row 1 -column 3 -sticky ns
    grid rowconf $w.bars 1 -weight 1
    grid columnconf $w.bars 2 -weight 1

    frame $w.middle
    frame $w.middle.f1 -relief groove -bd 2

    radiobutton $w.middle.f1.b1 -text [_ "Inside"] -variable ToolbarsPriv(inout) -value inside
    radiobutton $w.middle.f1.b2 -text [_ "Outside"] -variable ToolbarsPriv(inout) -value outside
    radiobutton $w.middle.f1.b3 -text [_ "None"] -variable ToolbarsPriv(inout) -value none

    GidHelp "$w.middle.f1.b1 $w.middle.f1.b2 $w.middle.f1.b3" \
	    [_ "Selects the status of the toolbar. Options are:\n\
	    inside: Toolbar will be inside the main program window\n\
	    outside: Toolbar will become an independent window\n\
	    none: Toolbar will be deactivated"]
    

    grid $w.middle.f1.b1 -sticky w
    grid $w.middle.f1.b2 -sticky w
    grid $w.middle.f1.b3 -sticky w

    frame $w.middle.f2 -relief groove -bd 2
    set im [imagefrom point.gif]
    radiobutton $w.middle.f2.l -indicatoron 0 -selectcolor "" \
	    -image $im -variable ToolbarsPriv(lrtb) -value l
    radiobutton $w.middle.f2.r -indicatoron 0 -selectcolor "" \
	    -image $im -variable ToolbarsPriv(lrtb) -value r
    radiobutton $w.middle.f2.b -indicatoron 0 -selectcolor "" \
	    -image $im -variable ToolbarsPriv(lrtb) -value b
    radiobutton $w.middle.f2.t -indicatoron 0 -selectcolor "" \
	    -image $im -variable ToolbarsPriv(lrtb) -value t


    GidHelp $w.middle.f2 [_ "If 'inside' is selected and the toolbar can adopt\
	    different positions inside the main window, these buttons choose\
	    if the window is up, left, right or bottom of the main window."]

    grid $w.middle.f2.t -row 1 -column 2
    grid $w.middle.f2.l -row 2 -column 1
    grid $w.middle.f2.r -row 2 -column 3
    grid $w.middle.f2.b -row 3 -column 2

    grid $w.middle.f1 -row 1 -column 1 -sticky wns
    grid $w.middle.f2 -row 1 -column 2 -sticky wns
    grid columnconf $w.middle 1 -weight 2
    grid columnconf $w.middle 2 -weight 1

    #frame $w.but -relief groove -bd 2
    frame $w.but -bg [CCColorActivo [$w cget -background]]
    button $w.but.close -text [_ "Close"] -command "destroy $w" -und 0
    bind $w <Alt-c> "$w.but.close invoke ; break"
    grid $w.but.close -padx 5 -pady 5
    
    grid $w.bars -row 1 -column 1 -sticky nsew
    grid $w.middle -row 2 -column 1 -sticky ew
    grid $w.but -row 3 -column 1 -sticky ew

    grid rowconf $w 1 -weight 1 -pad 3
    grid rowconf $w 2 -weight 0 -pad 3
    grid rowconf $w 3 -weight 0 -pad 3
    grid columnconf $w 1 -weight 1

    TOInitializeToolbars
    
    foreach i $ToolbarsPriv(toolbars) {
	if { [llength $i] == "2" } {
	    #old compatibility, previous versions not exists field 3 title, used field 0 name
	    set title [lindex $i 0]
	} else {
	    set title [lindex $i 3]
	}
	$w.bars.t ins end $title
    }
    $w.bars.t selection set 0

    TOCheckCurrentBar $w
    bind $w.bars.t <ButtonRelease-1> "TOCheckCurrentBar $w"
    bind $w.bars.t <KeyRelease> "TOCheckCurrentBar $w"

    focus $w.bars.t
}


#----------------------Toolbars ALONE---------------------------
if { $ToolbarsRunningAlone == 1 } {
    TOBegin .toolbars
} 
#-----------------------------------------------------------------
