# $Revision$ on $Date$
# The above revision line needs to be the first line for all techtk files
# Copyright (c) 2006 Ansys Inc
namespace eval ::AIEnv::GUIManagement {}
#-------------------------------------------------------------------------------

proc ::AIEnv::GUIManagement::enterMenuZoneBuild {NodeID} {
	# Intentionally empty
}

proc ::AIEnv::GUIManagement::leaveMenuZoneBuild {NodeID} {
	# Intentionally empty
}

proc ::AIEnv::GUIManagement::enterPopupMenuBuild {NodeID} {
	variable GUIDisplayData
	variable GUIZones
	variable PopupCount
	set GUITree [TreeStack peek]
	
	if { ![info exists PopupCount] } {
		set PopupCount 0
	}
	set Path $GUIZones("PopupZone")${PopupCount}
	incr PopupCount
	$GUITree set $NodeID -key "WindowPath" $Path
	menu $Path -type normal -tearoff $GUIDisplayData("MenuTearOffEnable") -font $GUIDisplayData("MenuFont") 
	WindowPathStack push $Path
	
}

proc ::AIEnv::GUIManagement::leavePopupMenuBuild {NodeID} {
	WindowPathStack pop
}

proc ::AIEnv::GUIManagement::enterPopupZoneBuild {NodeID} {
	# Intentionally empty	
}

proc ::AIEnv::GUIManagement::leavePopupZoneBuild {NodeID} {
	# Intentionally empty	
}

proc ::AIEnv::GUIManagement::enterMenuBarBuild {NodeID} {
    variable GUIZones
    WindowPathStack push $GUIZones("MenuZone")
    set GUIZones("TopMenus") ""
}

proc ::AIEnv::GUIManagement::leaveMenuBarBuild {NodeID} {
    WindowPathStack pop

    variable GUIZones
    eval pack $GUIZones("TopMenus") -side left
}

proc ::AIEnv::GUIManagement::enterTopMenuBuild {NodeID} {
    variable GUIDisplayData
 
    set GUITree [TreeStack peek]
    set CurPath [WindowPathStack peek]
    set NewPath "$CurPath.$NodeID"
    menubutton $NewPath -text [$GUITree get $NodeID -key "Name"] -menu $NewPath.menu
    menu $NewPath.menu -type normal \
                   -tearoff $GUIDisplayData("MenuTearOffEnable") \
                   -font $GUIDisplayData("MenuFont")

    set cmd [$GUITree get $NodeID -key "Proc"]
    if {($cmd != "NULL") && ([info command $cmd] != "")} {
       eval $cmd $NewPath.menu
    }

    $GUITree set $NodeID -key "WindowPath" $NewPath
    WindowPathStack push $NewPath.menu

    variable GUIZones
    lappend GUIZones("TopMenus") $NewPath
}

proc ::AIEnv::GUIManagement::leaveTopMenuBuild {NodeID} {
    WindowPathStack pop
}

proc ::AIEnv::GUIManagement::enterMenuBuild {NodeID} {
    variable GUIDisplayData

    set GUITree [TreeStack peek]
    set CurPath [WindowPathStack peek]
    set NewPath "$CurPath.$NodeID"
    $CurPath add cascade -label [$GUITree get $NodeID -key "Name"] -menu $NewPath
    menu $NewPath -type normal -tearoff $GUIDisplayData("MenuTearOffEnable") -font $GUIDisplayData("MenuFont") 
    $GUITree set $NodeID -key "Index" [$CurPath index [$GUITree get $NodeID -key "Name"]]
    $GUITree set $NodeID -key "WindowPath" $CurPath
    WindowPathStack push $NewPath
}

proc ::AIEnv::GUIManagement::leaveMenuBuild {NodeID} {
    WindowPathStack pop
}

proc ::AIEnv::GUIManagement::enterMenuItemBuild {NodeID} {
	set GUITree [TreeStack peek]
    [WindowPathStack peek] add command -label   [$GUITree get $NodeID -key "Name"] \
                                       -command [$GUITree get $NodeID -key "Proc"]
    $GUITree set $NodeID -key "Index" [[WindowPathStack peek] index [$GUITree get $NodeID -key "Name"]]
    $GUITree set $NodeID -key "WindowPath" [WindowPathStack peek]
}

proc ::AIEnv::GUIManagement::Popup {PopupName X Y args} {
	variable ItemNameMap
	set GUITree MainTree
	if { [info exists ItemNameMap($PopupName)] } {
		set NodeID $ItemNameMap($PopupName) 
		set Path [$GUITree get $NodeID -key "WindowPath"]
		tk_popup $Path $X $Y
	} else {
		#mess "No popup menu.\n Name: $PopupName \n X: $X\n Y: $Y \n args : $args"
	}
}

proc ::AIEnv::GUIManagement::leaveMenuItemBuild {NodeID} {
    # Do nothing
}

proc ::AIEnv::GUIManagement::enterMenuCheckBuild {NodeID} {
    set GUITree [TreeStack peek]
    set Name [$GUITree get $NodeID -key "Name"]
    set VariableName [$GUITree get $NodeID -key "CheckFlag"]
    [WindowPathStack peek] add checkbutton -label   $Name \
	-command [$GUITree get $NodeID -key "Proc"] \
	-offvalue 0 -onvalue 1 -variable $VariableName
    global $VariableName
    set $VariableName 0

    $GUITree set $NodeID -key "Index" [[WindowPathStack peek] index $Name]
    $GUITree set $NodeID -key "WindowPath" [WindowPathStack peek]
}

proc ::AIEnv::GUIManagement::leaveMenuCheckBuild {NodeID} {
    # Do nothing
}

proc ::AIEnv::GUIManagement::enterMenuRadioBuild {NodeID} {
    set GUITree [TreeStack peek]
    set Name [$GUITree get $NodeID -key "Name"]
    set VariableName [$GUITree get $NodeID -key "VarName"]
    set Value [$GUITree get $NodeID -key "Value"]
    [WindowPathStack peek] add radiobutton -label $Name \
	-command [$GUITree get $NodeID -key "Proc"] \
	-variable $VariableName -value $Value
    global $VariableName
    set $VariableName 0

    $GUITree set $NodeID -key "Index" [[WindowPathStack peek] index $Name]
    $GUITree set $NodeID -key "WindowPath" [WindowPathStack peek]
}

proc ::AIEnv::GUIManagement::leaveMenuRadioBuild {NodeID} {
    # Do nothing
}

proc ::AIEnv::GUIManagement::enterSeparatorBuild {NodeID} {
    set w [WindowPathStack peek]
    if {[winfo class $w] == "Frame"} {
	# This is a tabbed toolbar (i.e. button box)
	$w add -text "" -relief raised -borderwidth 1
    } else {
	$w add separator
    }
}

proc ::AIEnv::GUIManagement::leaveSeparatorBuild {NodeID} {
    # Do nothing
}
