# $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 {}

namespace eval ::AIEnv::GUIManagement::ExclusiveGroup {
	variable Commands
	variable Options
	variable ExclusiveGroupData
	variable ExclusiveGroupTypes
	set ExclusiveGroupTypes [list "radio" "dropdown" "icon" "check"]
	
	set Commands {
		set
		get
		buildInit
		buildFinal
		insertItem
		getItemFrame
		widgetgrey
		widgetungrey
	}
	
	set Options {
		frameLabel
		type
		dropDownLabel
		numCols
		variable
		selectProc
		parent
		nodeID
		interiorFrame
	}
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::Clean {Name} {
   variable ExclusiveGroupData

   set pat "${Name}*"
   foreach n [array names ExclusiveGroupData $pat] {
      unset ExclusiveGroupData($n)
   }
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::RouterProc {Name Cmd args} {

	if {[catch {eval [list RouterProcImpl $Name $Cmd] $args} result] != 0} {
		::AIEnv::GUIManagement::RuntimeError $result
	} else {
		return $result
	}
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::RouterProcImpl {Name Cmd args} {
	# Check the command to see if it is valid
	variable Commands
	if { [lsearch $Commands $Cmd] != -1 } {
		return [eval [list ::AIEnv::GUIManagement::ExclusiveGroup::_$Cmd $Name] $args]
	} else {
		set Optlist [join $Commands ", "]
		set Optlist [linsert $Optlist "end-1" "or"]
		error "Unknown command: \"Cmd\" given to exclusiveGroup named: \"$Name\".  Valid commands are: \"$Optlist\"."
	}	
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::_set {Name args} {
	if {[catch {eval [list SetImpl $Name] $args} result] != 0} {
		::AIEnv::GUIManagement::RuntimeError $result		
	} else {
		return $result
	}
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::SetImpl {Name args} {
	variable Options
	variable ExclusiveGroupData
	variable ExclusiveGroupTypes
	if { [llength $args] == 3 } {
		set Flag [lindex $args 0]
		set Key [lindex $args 1]
		set Value [lindex $args 2]
		if { $Flag == "-key" } {
			if { [lsearch $Options $Key] != -1 } {
				if { $Key == "type" } {
					set Value [string tolower $Value]
					if { [lsearch $ExclusiveGroupTypes $Value] == -1 } {
						set Optlist [join $ExclusiveGroupTypes ", "]
						set Optlist [linsert $Optlist "end-1" "or"]
						error "Invalid selection type: \"$Value\" given for multi-selector: \"$Name\"\n. Valid selection types are: $Optlist."
					}
				}
				set ExclusiveGroupData($Name,$Key) $Value
			} else {
				set Optlist [join $Options ", "]
				set Optlist [linsert $Optlist "end-1" "or"]
				error "Invalid option: \"$Key\" for multi-selector: \"$Name\". Valid options are $Optlist."
			}
		} else {
			error "Invalid flag: \"$Flag\" for multi-selector: \"$Name\". Must be -key"
		}
	} else {
		error "Wrong number of arguments: Should be \"$Name set -key key value\""
	}
}


proc ::AIEnv::GUIManagement::ExclusiveGroup::_get {Name args} {
	if {[catch {eval [list GetImpl $Name] $args} result] != 0} {
		::AIEnv::GUIManagement::RuntimeError $result
	} else {
		return $result
	}
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::GetImpl {Name args} {
	variable Options
	variable ExclusiveGroupData
	if { [llength $args] == 2 } {
		set Flag [lindex $args 0]
		set Key [lindex $args 1]
		if { $Flag == "-key" } {
			if { [lsearch $Options $Key] != -1 } {
				return $ExclusiveGroupData($Name,$Key)
			} else {
				set Optlist [join $Options ", "]
				set Optlist [linsert $Optlist "end-1" "or"]
				error "Invalid option: \"$Key\" for multi-selector: \"$Name\". Valid options are $Optlist."
			}
		} else {
			error "Invalid flag: \"$Flag\" for multi-selector: \"$Name\". Must be -key"
		}
	} else {
		error "Wrong number of arguments: Should be \"$Name get -key key\""
	}
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::_insertItem {Name args} {
	if {[catch {eval [list InsertItemImpl $Name] $args} result] != 0} {
		::AIEnv::GUIManagement::RuntimeError $result
	} else {
		return $result
	}	
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::InsertItemImpl {Name args} {
	variable ExclusiveGroupData
	if [info exist ExclusiveGroupData($Name-ChildCount)] {
		incr ExclusiveGroupData($Name-ChildCount)
	} else {
		set ExclusiveGroupData($Name-ChildCount) 0
	}
	if { [llength $args] == 3 } {
		lappend ExclusiveGroupData($Name,Children,Name) [lindex $args 0]
		lappend ExclusiveGroupData($Name,Children,Label) [lindex $args 1]
		lappend ExclusiveGroupData($Name,Children,Icon) [lindex $args 2]
	} else {
		error "Wrong number of arguements for $Name insertItem.\nShould be $Name insertItem ItemName ItemLabel \{ItemIcon\}"
	}
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::_buildInit {Name args} {
	if {[catch {eval [list BuildInitImpl $Name] $args} result] != 0} {
		::AIEnv::GUIManagement::RuntimeError $result
	} else {
		return $result
	}
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::BuildInitImpl {Name args} {
	variable ExclusiveGroupData
	switch $ExclusiveGroupData($Name,type) {
		"radio" {
			BuildRadio $Name
		}		
		"dropdown" {
			BuildDropdown $Name
		}
		"icon" {
			BuildIcon $Name
		}
	        "check" {
		    BuildCheck $Name
		}
		default {
			error "Unknown exclusive group type: $ExclusiveGroupData($Name,type)"
		}
	}
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::BuildRadio {Name} {
	variable ExclusiveGroupData
	variable ::AIEnv::GUIManagement::GUIDisplayData
	set tmp [array get ::AIEnv::GUIManagement::GUIDisplayData]
	array set LocalDisplayData $tmp
	
	set windowName [string tolower $Name]
	set RowParent [::AIEnv::GUIManagement::DEZInsert 1 1]
	set TopRow [lindex $RowParent 0]
	set ParentFrame [lindex $RowParent 1]
	
        set Title $ExclusiveGroupData($Name,frameLabel)
	if {$Title == "NULL"} {
	   set LabelFrame [frame $ParentFrame.$windowName-f -bd 0 -relief flat]
           set InteriorFrame $LabelFrame
	} elseif {$Title == "GROOVERELIEF"} {
	   set LabelFrame [frame $ParentFrame.$windowName-f -bd 2 -relief groove]
	   set InteriorFrame $LabelFrame
	} else {
	   set LabelFrame [TitleFrame $ParentFrame.$windowName-f -text $Title \
			-font $LocalDisplayData("DEZTitleFont")]
           set InteriorFrame [$LabelFrame getframe]
	}

        set RGTitle $ExclusiveGroupData($Name,dropDownLabel)
        if {$RGTitle == "NULL"} {
           set RadioGroupFrame [frame $InteriorFrame.r -bd 0 -relief flat]
           set RadioGroupInteriorFrame $RadioGroupFrame
         # set Sep [Separator $InteriorFrame.sep  -orient horizontal]
        } elseif {$RGTitle == "GROOVERELIEF"} {
           set RadioGroupFrame [frame $InteriorFrame.r -bd 2 -relief groove]
           set RadioGroupInteriorFrame $RadioGroupFrame
        } else {
           set RadioGroupFrame [TitleFrame $InteriorFrame.r -text $RGTitle \
			-font $LocalDisplayData("DEZTitleFont")]
           set RadioGroupInteriorFrame [$RadioGroupFrame getframe]
        }

	set GroupFrame [frame $InteriorFrame.f]
	set NumGroups [llength $ExclusiveGroupData($Name,Children,Name)]
	set Variable $ExclusiveGroupData($Name,variable)
	
	set Value 0
	foreach Child $ExclusiveGroupData($Name,Children,Name) {
		set Row [expr int($Value / $ExclusiveGroupData($Name,numCols))]
    	set Col [expr int($Value % $ExclusiveGroupData($Name,numCols))]	
    	set winName [string tolower $Child]
    	set Radio [radiobutton $RadioGroupInteriorFrame.$winName -value $Value  \
					-text [lindex $ExclusiveGroupData($Name,Children,Label) $Value] \
					-wraplength $LocalDisplayData("RadioWrapLength") \
                                        -justify $LocalDisplayData("RadioJustify") \
					-font $LocalDisplayData("LabelFont") -variable $Variable \
					-command "::AIEnv::GUIManagement::ExclusiveGroup::RadioHandler $Name $Value"]
		
		grid $Radio -row $Row -column $Col -sticky w -padx $LocalDisplayData("DEZPadx")
		lappend ExclusiveGroupData($Name,RadioButtons) $Radio
		incr Value
	}
	incr Row
	grid columnconfigure $RadioGroupInteriorFrame $ExclusiveGroupData($Name,numCols) -weight 1
	grid rowconfigure $RadioGroupInteriorFrame $Row -weight 1
	
	grid $LabelFrame -row $TopRow -column 0 -columnspan 3 -sticky nsew
	
	grid $RadioGroupFrame -row 0 -column 0 -sticky nsew
	grid $GroupFrame -row 1 -column 0 -sticky nsew
	
	grid columnconfigure $InteriorFrame 0 -weight 1
	grid rowconfigure $InteriorFrame 0 -weight 0
	grid rowconfigure $InteriorFrame 1 -weight 1
	
	set ExclusiveGroupData($Name,interiorFrame) $GroupFrame
	set ExclusiveGroupData($Name,OuterFrame) $LabelFrame
	set ExclusiveGroupData($Name,RadioGroupFrame) $RadioGroupFrame
	CreateGroupItemFrames $Name
	
	trace variable $Variable "w" "::AIEnv::GUIManagement::ExclusiveGroup::MonitorGroupChange $Name"
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::BuildDropdown {Name} {
	variable ExclusiveGroupData
	variable ::AIEnv::GUIManagement::GUIDisplayData
    set tmp [array get ::AIEnv::GUIManagement::GUIDisplayData]
	array set LocalDisplayData $tmp
	
	set windowName [string tolower $Name]
	set RowParent [::AIEnv::GUIManagement::DEZInsert 1 1]
	set TopRow [lindex $RowParent 0]
	set ParentFrame [lindex $RowParent 1]
	
    set Title $ExclusiveGroupData($Name,frameLabel)
    if {$Title == "NULL"} {
       set LabelFrame [frame $ParentFrame.$windowName-f -bd 0 -relief flat]
       set InteriorFrame $LabelFrame
	} elseif {$Title == "GROOVERELIEF"} {
	   set LabelFrame [frame $ParentFrame.$windowName-f -bd 2 -relief groove]
	   set InteriorFrame $LabelFrame
    } else {
       set LabelFrame [TitleFrame $ParentFrame.$windowName-f -text $Title \
                        -font $LocalDisplayData("DEZTitleFont")]
       set InteriorFrame [$LabelFrame getframe]
    }

	set Label [label $InteriorFrame.$windowName-tf -text $ExclusiveGroupData($Name,dropDownLabel) \
	                                           -font $LocalDisplayData("EntryFont") \
	                                           -wraplength $LocalDisplayData("LabelWrapLength") \
	                                           -justify $LocalDisplayData("LabelJustify") ]
	# Pick out the group labels
	set NumGroups [llength $ExclusiveGroupData($Name,Children,Name)]
	set Variable $ExclusiveGroupData($Name,variable)
	set num_groups $NumGroups
	if {$NumGroups > 25} {
	    set num_groups [expr $NumGroups / 2]
	}
	set CBox [ComboBox $InteriorFrame.$windowName-cb -height $num_groups \
	        -modifycmd "::AIEnv::GUIManagement::ExclusiveGroup::DropDownHandler $Name $Variable" \
		    -font $LocalDisplayData("EntryFont") -editable false  \
	        -values $ExclusiveGroupData($Name,Children,Label) -entrybg white -exportselection 0]
    $CBox configure -foreground black 
    
	set Sep [Separator $InteriorFrame.$windowName-sep -orient horizontal]
	set GroupFrame [frame $InteriorFrame.$windowName-f]
	
	grid $Label -row 0 -column 0 -sticky w -pady $LocalDisplayData("DEZPady")
	grid $CBox  -row 0 -column 1 -sticky ew -padx $LocalDisplayData("DEZPadx") \
	                                        -pady $LocalDisplayData("DEZPady")
	grid $Sep   -row 1 -column 0 -sticky ew -padx $LocalDisplayData("DEZPadx") \
	            -columnspan 2 -pady $LocalDisplayData("DEZPady")
	grid $GroupFrame -row 2 -column 0 -sticky nsew -padx $LocalDisplayData("DEZPadx") \
	                 -pady $LocalDisplayData("DEZPady") -columnspan 2
	
	grid columnconfigure $InteriorFrame 0 -weight 0
	grid columnconfigure $InteriorFrame 1 -weight 1
	grid rowconfigure $InteriorFrame 3 -weight 1
	
	grid $LabelFrame -row $TopRow -column 0 -columnspan 3 -sticky nsew
	
	set ExclusiveGroupData($Name,CBox) $CBox
	set ExclusiveGroupData($Name,interiorFrame) $GroupFrame
	set ExclusiveGroupData($Name,OuterFrame) $LabelFrame
	set ExclusiveGroupData($Name,CBoxLabel) $Label
	
	CreateGroupItemFrames $Name
	
	trace variable $Variable "w" "::AIEnv::GUIManagement::ExclusiveGroup::MonitorGroupChange $Name"
	
	# Set the auto update for the combo box
	set Parent $ExclusiveGroupData($Name,parent)
	#$Parent addloadproc "::AIEnv::GUIManagement::ExclusiveGroup::DropDownHandler $Name $Variable" AFTER_ON_LOAD
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::BuildIcon {Name} {
	variable ExclusiveGroupData
	variable ::AIEnv::GUIManagement::GUIDisplayData
	variable ::AIEnv::GUIManagement::CATRootDir
        global toolBarInfo

	set RootDir $::AIEnv::GUIManagement::CATRootDir
	set tmp [array get ::AIEnv::GUIManagement::GUIDisplayData]
	array set LocalDisplayData $tmp
	
	set windowName [string tolower $Name]
	set RowParent [::AIEnv::GUIManagement::DEZInsert 1 1]
	set TopRow [lindex $RowParent 0]
	set ParentFrame [lindex $RowParent 1]
	
#	set LabelFrame [TitleFrame $ParentFrame.$windowName-f -text $ExclusiveGroupData($Name,frameLabel) \
#			-font $LocalDisplayData("DEZTitleFont")]
#	set InteriorFrame [$LabelFrame getframe]
	set Title $ExclusiveGroupData($Name,frameLabel)
	if {$Title == "NULL"} {
	   set LabelFrame [frame $ParentFrame.$windowName-f -bd 0 -relief flat]
	   set InteriorFrame $LabelFrame
	} elseif {$Title == "GROOVERELIEF"} {
	   set LabelFrame [frame $ParentFrame.$windowName-f -bd 2 -relief groove]
	   set InteriorFrame $LabelFrame
	} else {
	   set LabelFrame [TitleFrame $ParentFrame.$windowName-f -text $Title \
			-font $LocalDisplayData("DEZTitleFont")]
	   set InteriorFrame [$LabelFrame getframe]
	}
	# Figure out how many icon buttons there are, and then compute the number
	# of button boxes we will need
	set NumGroups [llength $ExclusiveGroupData($Name,Children,Name)]
	set Variable $ExclusiveGroupData($Name,variable)
# Anshul: Below is commented to have uniformity for number of icons in one row
# Ganesan: If numcols < 0, then override    
    if {$ExclusiveGroupData($Name,numCols) < 0} {
	set use_ncols 1
    } else {
	set use_ncols 0
    }

    if $use_ncols {
	set Rows [ceil [expr $NumGroups / $ExclusiveGroupData($Name,numCols)]]
    } else {
        set Rows [ceil [expr $NumGroups / $LocalDisplayData("NumberofEntryinGroupRow")]]
    }

	set Value 0
	set Row 0
        set ToolBar $windowName
        set toolBarInfo($ToolBar-current) ""
	while {$Value < $NumGroups} {
# Anshul: Below is commented to have uniformity for number of icons in one row
#		if { [expr $Value % $ExclusiveGroupData($Name,numCols)] == 0 } {
#		}

	    if $use_ncols {
		set condition [expr $Value % $ExclusiveGroupData($Name,numCols)]
	    } else {
		set condition [expr $Value % $LocalDisplayData("NumberofEntryinGroupRow")] 
	    }

	    if { $condition == 0 } {
		        set BBox [ButtonBox $InteriorFrame.bbox${Row} -spacing $LocalDisplayData("ButtonBoxSpacing") -padx 1 -pady 1]
			lappend ExclusiveGroupData($Name,ButtonBoxes) $BBox
			grid $BBox -row $Row -column 0 -pady 2 -sticky w
			incr Row
		}
		set FileName [::AIEnv::GUIManagement::GetFileName [file join $RootDir "icons" \
											  [lindex $ExclusiveGroupData($Name,Children,Icon) $Value]]]
		set im1 [image create photo -format GIF -file $FileName]
		set tb [ $BBox add -image $im1 -highlightthickness 0 -takefocus 0 -relief link \
			     -borderwidth 1  -padx 1 -pady 1  \
			     -helptext [lindex $ExclusiveGroupData($Name,Children,Label) $Value]]
		$tb configure -command "::AIEnv::GUIManagement::toolBarSelect $ToolBar $tb; ::AIEnv::GUIManagement::ExclusiveGroup::IconHandler $Name $Value $Variable"
		set wname [lindex $ExclusiveGroupData($Name,Children,Name) $Value]
		set toolBarInfo($wname-win) $ToolBar
		set toolBarInfo($wname-tool) $tb
		incr Value
	}
	
	grid columnconfigure $InteriorFrame 0 -weight 1
	set Sep [Separator $InteriorFrame.sep1 -orient horizontal]
	set GroupFrame [frame $InteriorFrame.gf]
	
	incr Row
	grid $Sep -row $Row -column 0 -sticky ew -pady 3 -padx 3
	incr Row
	grid $GroupFrame -row $Row -column 0 -sticky nsew
	
	grid $LabelFrame -row $TopRow -column 0 -columnspan 3 -sticky nsew
	set ExclusiveGroupData($Name,interiorFrame) $GroupFrame
	set ExclusiveGroupData($Name,OuterFrame) $LabelFrame
	CreateGroupItemFrames $Name
	
	trace variable $Variable "w" "::AIEnv::GUIManagement::ExclusiveGroup::MonitorGroupChange $Name"
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::BuildCheck {Name} {
    variable ExclusiveGroupData
    variable ::AIEnv::GUIManagement::GUIDisplayData
    set tmp [array get ::AIEnv::GUIManagement::GUIDisplayData]
    array set LocalDisplayData $tmp
    
    set windowName [string tolower $Name]
    set RowParent [::AIEnv::GUIManagement::DEZInsert 1 1]
    set TopRow [lindex $RowParent 0]
    set ParentFrame [lindex $RowParent 1]
    
    set Title $ExclusiveGroupData($Name,frameLabel)        
    set Value 0
    set Variable $ExclusiveGroupData($Name,variable)
    set CFrame [CheckFrame $ParentFrame.$windowName-f \
		    -text $Title \
		    -font $LocalDisplayData("DEZTitleFont") -variable $Variable \
		    -command "::AIEnv::GUIManagement::ExclusiveGroup::CheckHandler $Name $Value"]

    set InteriorFrame [$CFrame getframe]
   
    # grid in the frame
    grid $CFrame -row $TopRow -column 0 -columnspan 3 -sticky nsew

    grid columnconfigure $InteriorFrame 0 -weight 1
    grid rowconfigure $InteriorFrame 0 -weight 0
    grid rowconfigure $InteriorFrame 1 -weight 1
    set ExclusiveGroupData($Name,interiorFrame) $InteriorFrame
 
    CreateGroupItemFrames $Name	
    trace variable $Variable "w" "::AIEnv::GUIManagement::ExclusiveGroup::MonitorGroupChange $Name"
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::CreateGroupItemFrames {Name} {
    
	variable ExclusiveGroupData
	set Children $ExclusiveGroupData($Name,Children,Name)
	foreach Child $Children {
		set frameName [string tolower $Child]
		set ChildFrame [frame $ExclusiveGroupData($Name,interiorFrame).$frameName]
		grid columnconfigure $ChildFrame 0 -weight 0
		grid columnconfigure $ChildFrame 1 -weight 1
		grid columnconfigure $ChildFrame 2 -weight 0
		lappend ExclusiveGroupData($Name,Children,Frame) $ChildFrame
	}
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::_buildFinal {Name args} {
	if {[catch {eval [list BuildFinalImpl $Name] $args} result] != 0} {
		::AIEnv::GUIManagement::RuntimeError $result
	} else {
		return $result
	}	
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::BuildFinalImpl {Name args} {
	variable ExclusiveGroupData
	
	set Height 0
	set Width 0
	set ChildFrames $ExclusiveGroupData($Name,Children,Frame)
	update idletasks
	
	foreach Frame $ChildFrames {
		set w [winfo reqwidth $Frame]
		set h [winfo reqheight $Frame]
		if { $w > $Width } {
			set Width $w
		} 
		if { $h > $Height } {
			set Height $h
		}
	}
	$ExclusiveGroupData($Name,interiorFrame) configure -width $Width -height $Height
	grid propagate $ExclusiveGroupData($Name,interiorFrame) 0
	grid rowconfigure $ExclusiveGroupData($Name,interiorFrame) 1 -weight 1
	grid columnconfigure $ExclusiveGroupData($Name,interiorFrame) 0 -weight 1
	set ExclusiveGroupData($Name,CurrentGroupName) "NULL"
}


proc ::AIEnv::GUIManagement::ExclusiveGroup::_getItemFrame {Name args} {
	if {[catch {eval [list GetItemFrameImpl $Name] $args} result] != 0} {
		::AIEnv::GUIManagement::RuntimeError $result
	} else {
		return $result
	}		
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::GetItemFrameImpl {Name args} {
    variable ExclusiveGroupData
    set ItemName [lindex $args 0]
    set ItemIndex [lsearch $ExclusiveGroupData($Name,Children,Name) $ItemName]
    if { $ItemIndex != -1 } {
	set fr [lindex $ExclusiveGroupData($Name,Children,Frame) $ItemIndex]
	if {$fr == ""} {
	    error "No frame exists for $ItemName in exclusive group $Name"
	}
	return [lindex $ExclusiveGroupData($Name,Children,Frame) $ItemIndex]
    } else {
	error "Unknown exclusive group item: $ItemName in exclusive group $Name"
    }
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::DropDownHandler {Name Variable} {
	variable ExclusiveGroupData
	
	if { $ExclusiveGroupData($Name,CurrentGroupName) != "NULL" } {
		set OldGroup $ExclusiveGroupData($Name,CurrentGroupName)
		set Index [lsearch $ExclusiveGroupData($Name,Children,Name) $OldGroup]
		if { $Index != -1 } {
			grid forget [lindex $ExclusiveGroupData($Name,Children,Frame) $Index]
			OnUnLoad $Name $OldGroup
		} else {
			error "The internal state of the exclusive widget named: $Name was compromised.\nThe current group: $OldGroup could not be found in the list of child groups."
		}
	}
	
	set SelectionIndex [$ExclusiveGroupData($Name,CBox) getvalue]
	if { $SelectionIndex != -1 } {
		set NewGroup [lindex $ExclusiveGroupData($Name,Children,Frame) $SelectionIndex]
		set NewGroupName [lindex $ExclusiveGroupData($Name,Children,Name) $SelectionIndex]
		grid $NewGroup -row 0 -column 0 -sticky nsew
		set ExclusiveGroupData($Name,CurrentGroupName) $NewGroupName
	        if {$Variable != ""} { set $Variable $SelectionIndex }
		OnLoad $Name $NewGroupName
	} else {
		set ExclusiveGroupData($Name,CurrentGroupName) "NULL"
	}
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::GetCurrentGroupName {Name} {
   variable ExclusiveGroupData
   if {[info exist ExclusiveGroupData($Name,CurrentGroupName)]} {   
        return $ExclusiveGroupData($Name,CurrentGroupName)
   } else {
        return ""
   }
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::GetGroupItemWidget {ParentName ThisName} {
    variable ExclusiveGroupData

    set k [lsearch $ExclusiveGroupData($ParentName,Children,Name) $ThisName]
    if {$k < 0} {return ""}

    switch -- $ExclusiveGroupData($ParentName,type) {
       "radio" {return [lindex $ExclusiveGroupData($ParentName,RadioButtons) $k]}
       "icon"  {
           global toolBarInfo
           if [info exist toolBarInfo($ThisName-tool)] {
              return $toolBarInfo($ThisName-tool)
           } else {
              foreach r $ExclusiveGroupData($ParentName,ButtonBoxes) {
                 set n [llength $r]
                 if {$n > $k} {
                    return [lindex $r $k]
                 } else {
                    set k [expr $k - $n]
                 } 
              }
              return ""
           }
       }
       "dropdown" {return ""}
       "check" {return ""}
       default {
          mess "$ParentName type $ExclusiveGroupData($ParentName,type)\n"
          return ""
       }
    }
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::OnLoad {WidgetName GroupItemName} {
	variable ExclusiveGroupData

    global select_action 

    # unselect hexa entities if any
    select_hexa_reset

    # Unselect geom entities if any
    global aie_guiOpt
    if {$aie_guiOpt(withMedGWin)} {
	aie_reset_geo_selected
    }

    set ProcName "on${GroupItemName}Load"
    set Parent $ExclusiveGroupData($WidgetName,parent)

    # wait for reset selction to finish
    after 100 "$Parent proc $ProcName"
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::OnUnLoad {WidgetName GroupItemName} {
	variable ExclusiveGroupData
	set ProcName "on${GroupItemName}UnLoad"
	set Parent $ExclusiveGroupData($WidgetName,parent)
	$Parent proc $ProcName	
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::RadioHandler {Name Value} {
	variable ExclusiveGroupData
	
	if { $ExclusiveGroupData($Name,CurrentGroupName) != "NULL" } {
		set OldGroup $ExclusiveGroupData($Name,CurrentGroupName)
		set Index [lsearch $ExclusiveGroupData($Name,Children,Name) $OldGroup]
		if { $Index != -1 } {
			grid forget [lindex $ExclusiveGroupData($Name,Children,Frame) $Index]
			OnUnLoad $Name $OldGroup
		} else {
			error "The internal state of the exclusive widget named: $Name was compromised.\nThe current group: $OldGroup could not be found in the list of child groups."
		}
	}
	if { [expr $Value >= 0] && [expr $Value < [llength $ExclusiveGroupData($Name,Children,Name)]] } {
		set NewGroup [lindex $ExclusiveGroupData($Name,Children,Frame) $Value]
		set NewGroupName [lindex $ExclusiveGroupData($Name,Children,Name) $Value]
		grid $NewGroup -row 0 -column 0 -sticky nsew
		set ExclusiveGroupData($Name,CurrentGroupName) $NewGroupName
		OnLoad $Name $NewGroupName
	} else {
		set ExclusiveGroupData($Name,CurrentGroupName) "NULL"
	}
	
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::CheckHandler {Name Value} {
    variable ExclusiveGroupData
	
    if {[set $ExclusiveGroupData($Name,variable)] == 0} {
	set Value -1
    }
    if { $ExclusiveGroupData($Name,CurrentGroupName) != "NULL" } {
	set OldGroup $ExclusiveGroupData($Name,CurrentGroupName)
	set Index [lsearch $ExclusiveGroupData($Name,Children,Name) $OldGroup]
	if { $Index != -1 } {
	    grid forget [lindex $ExclusiveGroupData($Name,Children,Frame) $Index]
	    OnUnLoad $Name $OldGroup
	} else {
	    error "The internal state of the exclusive widget named: $Name was compromised.\nThe current group: $OldGroup could not be found in the list of child groups."
	}
    }
    if { [expr $Value >= 0] && [expr $Value < [llength $ExclusiveGroupData($Name,Children,Name)]] } {
	set NewGroup [lindex $ExclusiveGroupData($Name,Children,Frame) $Value]
	set NewGroupName [lindex $ExclusiveGroupData($Name,Children,Name) $Value]
	grid $NewGroup -row 0 -column 0 -sticky nsew
	set ExclusiveGroupData($Name,CurrentGroupName) $NewGroupName
	OnLoad $Name $NewGroupName
    } else {
	set ExclusiveGroupData($Name,CurrentGroupName) "NULL"
    }
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::MonitorGroupChange {Name VarName Entry op} {
    variable ExclusiveGroupData
    global toolBarInfo
    upvar $VarName NewGroupIndex
    set NewGroupName [lindex $ExclusiveGroupData($Name,Children,Name) $NewGroupIndex]
    set CurrentGroup $ExclusiveGroupData($Name,CurrentGroupName)
    set CurrentGroupIndex [lsearch $ExclusiveGroupData($Name,Children,Name) $CurrentGroup]
    
    # --- This is weird. CurrentGroupIndex  and NewGroupIndex
    #     are the same because those Handler set CurrentGroupName
    #     The following is added to do something really necessary.
    
    if {![info exist ExclusiveGroupData($Name,LastGroupName)] || \
	    ($ExclusiveGroupData($Name,LastGroupName) != $NewGroupName)} {
	if {[info command [lindex $ExclusiveGroupData($Name,selectProc) 0]] != ""} {
	    eval $ExclusiveGroupData($Name,selectProc) $NewGroupName
	}
	set ExclusiveGroupData($Name,LastGroupName) $NewGroupName
    }    
    
    if { $CurrentGroupIndex != $NewGroupIndex } {
	set CurrentGroupName $ExclusiveGroupData($Name,CurrentGroupName)
	if { $CurrentGroupName != "NULL" } {
	    grid forget [lindex $ExclusiveGroupData($Name,Children,Frame) $CurrentGroupIndex]
	    OnUnLoad $Name $ExclusiveGroupData($Name,CurrentGroupName)
	    
	    if {$NewGroupIndex == -1} {
		# Just clear DEZ
		if [info exists toolBarInfo($CurrentGroupName-win)] {
		    set win $toolBarInfo($CurrentGroupName-win)
		    ::AIEnv::GUIManagement::toolBarSelect  $win "" 1
		} 
		return
	    }
	}

	set NewGroup [lindex $ExclusiveGroupData($Name,Children,Frame) $NewGroupIndex]
	if {$NewGroup == ""} {
	    return
	}
	grid $NewGroup -row 0 -column 0 -sticky nsew
	set ExclusiveGroupData($Name,CurrentGroupName) $NewGroupName
#       OnLoad $Name $NewGroupName	        
	if [info exists toolBarInfo($NewGroupName-win)] {
	    set win $toolBarInfo($NewGroupName-win)
	    set tool  $toolBarInfo($NewGroupName-tool)
	    set toolBarInfo(external_select) 1
	    ::AIEnv::GUIManagement::toolBarSelect $win $tool
	    set toolBarInfo(external_select) 1
	}
    }
    
    # Check to see what kind of exclusive group this is
    switch $ExclusiveGroupData($Name,type) {
	"radio" {
	    
	}		
	"dropdown" {
	    $ExclusiveGroupData($Name,CBox) setvalue @$NewGroupIndex
	}
	"icon" {
	    
	}
	"check" {
	}
	default {
	    error "Unknown exclusive group type: $ExclusiveGroupData($Name,type)"
	}
    }


   
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::IconHandler {Name Value Variable} {
	variable ExclusiveGroupData
	
	if { $ExclusiveGroupData($Name,CurrentGroupName) != "NULL" } {
		set OldGroup $ExclusiveGroupData($Name,CurrentGroupName)
		set Index [lsearch $ExclusiveGroupData($Name,Children,Name) $OldGroup]
		if { $Index != -1 } {
			grid forget [lindex $ExclusiveGroupData($Name,Children,Frame) $Index]
			OnUnLoad $Name $OldGroup
		} else {
			error "The internal state of the exclusive widget named: $Name was compromised.\nThe current group: $OldGroup could not be found in the list of child groups."
		}
	}
	if { [expr $Value >= 0] && [expr $Value < [llength $ExclusiveGroupData($Name,Children,Name)]] } {
		set NewGroup [lindex $ExclusiveGroupData($Name,Children,Frame) $Value]
		set NewGroupName [lindex $ExclusiveGroupData($Name,Children,Name) $Value]
		grid $NewGroup -row 0 -column 0 -sticky nsew
		set ExclusiveGroupData($Name,CurrentGroupName) $NewGroupName
		set $Variable $Value
		OnLoad $Name $NewGroupName
	} else {
		set ExclusiveGroupData($Name,CurrentGroupName) "NULL"
	}	
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::_widgetgrey {Name args} {
	if {[catch {eval [list WidgetGreyImpl $Name] $args} result] != 0} {
		::AIEnv::GUIManagement::RuntimeError $result
	} else {
		return $result
	}	
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::WidgetGreyImpl {Name args} {
	variable ExclusiveGroupData
	switch $ExclusiveGroupData($Name,type) {
		"radio" {
			GreyRadio $Name disabled
		}		
		"dropdown" {
			GreyDropDown $Name disabled
		}
		"icon" {
			GreyIcon $Name disabled
		}
	        "check" {
			GreyRadio $Name disabled
		}
		default {
			error "Unknown exclusive group type: $ExclusiveGroupData($Name,type)"
		}
	}
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::_widgetungrey {Name args} {
	if {[catch {eval [list WidgetUnGreyImpl $Name] $args} result] != 0} {
		::AIEnv::GUIManagement::RuntimeError $result
	} else {
		return $result
	}	
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::WidgetUnGreyImpl {Name args} {
	variable ExclusiveGroupData
	switch $ExclusiveGroupData($Name,type) {
		"radio" {
			GreyRadio $Name normal
		}		
		"dropdown" {
			GreyDropDown $Name normal
		}
		"icon" {
			GreyIcon $Name normal
		}
	        "check" {
			GreyRadio $Name normal
		}	
		default {
			error "Unknown exclusive group type: $ExclusiveGroupData($Name,type)"
		}
	}	
}


proc ::AIEnv::GUIManagement::ExclusiveGroup::GreyIcon {Name State} {
	variable ExclusiveGroupData
	# Loop through the buttons changing their state
	foreach BBox $ExclusiveGroupData($Name,ButtonBoxes) {
		set Buttons [winfo children $BBox]
		foreach Button $Buttons {
			$Button configure -state $State
		}
	}
	# Change the state of the main frame
#	$ExclusiveGroupData($Name,OuterFrame) configure -state $State
	::AIEnv::GUIManagement::SetState \
		$ExclusiveGroupData($Name,OuterFrame) $State
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::GreyRadio {Name State} {
	variable ExclusiveGroupData
	
	foreach RadioButton $ExclusiveGroupData($Name,RadioButtons) {
		$RadioButton configure -state $State
	}
	# Change the state of the main frame
#	$ExclusiveGroupData($Name,OuterFrame) configure -state $State
	::AIEnv::GUIManagement::SetState \
		$ExclusiveGroupData($Name,OuterFrame) $State
	# Change the state of the radio frame
	$ExclusiveGroupData($Name,RadioGroupFrame) configure -state $State
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::GreyCheck {Name State} {
	variable ExclusiveGroupData
	
	foreach CheckButton $ExclusiveGroupData($Name,CheckButtons) {
		$CheckButton configure -state $State
	}
	# Change the state of the main frame
#	$ExclusiveGroupData($Name,OuterFrame) configure -state $State
	::AIEnv::GUIManagement::SetState \
		$ExclusiveGroupData($Name,OuterFrame) $State
	# Change the state of the check frame
	$ExclusiveGroupData($Name,CheckGroupFrame) configure -state $State
}

proc ::AIEnv::GUIManagement::ExclusiveGroup::GreyDropDown {Name State} {
	variable ExclusiveGroupData
	variable ::AIEnv::GUIManagement::GUIDisplayData
	set tmp [array get ::AIEnv::GUIManagement::GUIDisplayData]
	array set LocalDisplayData $tmp
	
	$ExclusiveGroupData($Name,CBox) configure -state $State
	if { $State == "disabled" } {
		$ExclusiveGroupData($Name,CBox) configure -entrybg $LocalDisplayData("DisabledBackground")
	} else {
		$ExclusiveGroupData($Name,CBox) configure -entrybg $LocalDisplayData("NormalBackground")
	}
#	$ExclusiveGroupData($Name,OuterFrame) configure -state $State
	::AIEnv::GUIManagement::SetState \
		$ExclusiveGroupData($Name,OuterFrame) $State

	$ExclusiveGroupData($Name,CBoxLabel) configure -state $State
}




