#
# Module ifGroups.tcl
#
# This is some match-Groups dialogs.
#
#  $Log: ifGroups.tcl,v $
#  Revision 1.3  2006/10/26 09:39:17  vfom
#  Added code for netstate server query
#
#  Revision 1.2  2006/10/19 08:40:26  vfom
#  .
#
#  Revision 1.1.1.1  2006/09/21 10:57:02  vfom
#  Loading TkNetmon to CVS
#
#
# Global variables initialisation:
# 
# set ifGroupList [list]
# array set ifGroupDefinition [list]
#
#
namespace eval ::ifGrp {
variable	viewifGrp_exitFlag	0 \
		editifGrp_exitFlag	0 \
		err_msg			{}\
		gList		[list]	  
array set	gBodyArray	[list]	  
}
#---------------------------------------------------------------------------
# EditGroup definition 
#  
#
#---------------------------------------------------------------------------
proc EditIfGrp_GUIconstructor { p } {
    set w $p.editIfGrp
    set f $w.nameFrame
    set t $w.textFrame
    set b $w.mFrame
#
    toplevel $w -class Dialog
    wm transient $w [winfo toplevel [winfo parent $w]]	
    wm withdraw $w
#
    frame $f
    pack $f -side top -padx 5 -pady 5 -expand on -fill both
    label $f.lb -text [msgcat::mc "Group name:"]
    entry $f.emname -width 48 \
	-validatecommand { 
	    if { [string equal [%W cget -state] "normal"] && 
		[lsearch $::ifGrp::gList [%W get]]>=0 } { return false }
	    return true
	    } \
	-invalidcommand  {
	    if { [string equal [%W cget -state] "normal"] } {
		set tmp_mname [%W get]
		selection clear
		tk_dialog .groups.editMethod.error [msgcat::mc "Error"] \
		    [msgcat::mc "Such a group exist."]\
		    error 0 [msgcat::mc "Ok"]		
	    }	
	    focus %W	
	}
    pack $f.lb $f.emname -side left -padx 5 -pady 5 
#
    frame $t
    pack $t -side top -padx 5 -pady 5 -expand on -fill both 
    grid rowconfig    $t 0 -weight 1 -minsize 0 
    grid columnconfig $t 0 -weight 1 -minsize 0 
    text $t.tx -width 60 -height 28  \
	-xscrollcommand "$t.sx set" -yscrollcommand "$t.sy set"
    grid $t.tx -in $t \
	-row 0 -column 0 -columnspan 1 -rowspan 1 -sticky news
    scrollbar $t.sy -command "$t.tx yview"
    grid $t.sy -in $t \
	-row 0 -column 1 -columnspan 1 -rowspan 1 -sticky news
    scrollbar $t.sx -command "$t.tx xview" -orient horiz
    grid $t.sx -in $t \
	-row 1 -column 0 -columnspan 1 -rowspan 1 -sticky news	
#    
    frame $b
    pack $b -side bottom -padx 5 -pady 5 -expand off -anchor s
    button $b.ok -text [msgcat::mc "Ok"] -command {
	if { [ .groups.editIfGrp.nameFrame.emname get ] == "" } {
	    tk_dialog .groups.editIfGrp.error [msgcat::mc "Error"] \
		[msgcat::mc "Enter non-empty group name." ]\
		error 0 [msgcat::mc "Ok"]
	    focus .groups.editIfGrp.nameFrame.emname	
    	    return 
	}
	if { ![ .groups.editIfGrp.nameFrame.emname validate] } { 
	    return 
	}
	if { ![ IsBracesBalanced [.groups.editIfGrp.textFrame.tx get 0.0 end ] ]} {
	    tk_dialog .groups.editMethod.error [msgcat::mc "Method Body Syntax Error"] \
		[msgcat::mc "Unbalanced curve braces, OR\nclose brace before open brace." ]\
		error 0 [msgcat::mc "Ok"]
	    return
	}
	set ::ifGrp::editIfGrp_exitFlag 1
    }
    button $b.cancel -text [msgcat::mc "Cancel"] -command "set ::ifGrp::editIfGrp_exitFlag 2"
    pack $b.ok $b.cancel -side left -padx 5 -pady 5

    update idletasks
    set x [expr {[winfo screenwidth $w] - [winfo reqwidth $w] \
    	- [winfo vrootx [winfo parent $w]]}]
    set y [expr {[winfo screenheight $w]/2 - [winfo reqheight $w]/2 \
    	- [winfo vrooty [winfo parent $w]]}]
    wm geom $w +$x+$y
    #	
    bind $w <Destroy> "set ::ifGrp::editIfGrp_exitFlag 2"
    bind $w <Escape>  "set ::ifGrp::editIfGrp_exitFlag 2"
}

proc EditIfGrp { p act el } {
    if { [string equal $act "add"] } {
	set mname ""
    } else {
	set pos  [$el curselection] 
	if { $pos != "" } {
	    set mname [lindex $::ifGrp::gList $pos ]    
	    if { [string equal $act "delete" ] } {
		if {[info exists ::ifGrp::gBodyArray($mname)] } {
		    unset ::ifGrp::gBodyArray($mname)
		}
		$el delete $pos
		return
	    }
	} else { return }
    }
    #    
    set w $p.editIfGrp
    set f $w.nameFrame
    set t $w.textFrame
    set b $w.mFrame
    if { ![winfo exists $w] } {
    	EditIfGrp_GUIconstructor $p
    }
#    
#  Load actual data
#    
    if {[string equal $act "edit"]} {
	wm title $w [msgcat::mc "Edit Group"]
	$f.emname insert 0 $mname
	$f.emname configure -state disabled
	if {[info exists ::ifGrp::gBodyArray($mname)]} {
	    $t.tx insert 0.0 [ join $::ifGrp::gBodyArray($mname) ]
	}	
	$t.tx configure -state normal

    } elseif {[string equal $act "add"]} {
	wm title $w [msgcat::mc "Add New Group"]
	$f.emname configure -state normal
    } else {
	return;
    }
#
# Start entry validation
#
    $f.emname configure -validate focusout
#
    if { [string equal [wm state $w] withdrawn ] } {
        wm deiconify $w
    } 
    raise $w
        
    set ::ifGrp::editIfGrp_exitFlag 0
    update idletasks
    set oldFocus [focus]
    set oldGrab [grab current $w]
    if { [string compare $oldGrab ""] } {
	set grabStatus [grab status $oldGrab]
    }
    grab $w 
    focus $b.ok
    
    tkwait variable ::ifGrp::editIfGrp_exitFlag

    if {[winfo exists $w]} {
	wm withdraw $w
	catch {focus $oldFocus}
	grab release $w
	if {$::ifGrp::editIfGrp_exitFlag==1} {
	    # Ok button pressed
	    if {[string equal $act "edit"]} {
		set ::ifGrp::gBodyArray($mname) [list [ $t.tx get 0.0 "end-1 chars" ] ]		
	    } elseif {[string equal $act "add"]} {
		set mname [ $f.emname get ]	    
		if { $mname != "" } {
		    lappend ::ifGrp::gList $mname
		    set ::ifGrp::gBodyArray($mname)  [list [$t.tx get 0.0 "end-1 chars"]]
		}
	    }	    
	}
    #
    # Stop entry validation
    #
	$f.emname configure -validate none
    # cleanup entries
	$f.emname configure -state normal
	$f.emname delete  0 end
	$t.tx configure -state normal
	$t.tx delete 0.0 end
    }
    if {[string compare $oldGrab ""] && [winfo exists $oldGrab] } {
	if {![string compare $grabStatus "global"]} {
	    grab -global $oldGrab
	} else {
	    grab $oldGrab
	}
    }

    return
}
#---------------------------------------------------------------------------
# ViewIfGrp
#  
#
#  Usage:
#
# ViewIfGrp
#
#
#---------------------------------------------------------------------------
proc ViewIfGrp_GUIconstructor { } {
    set w .groups
    set f $w.mFrame
    set l $f.listFrame	
    set c $f.btFrame	
    set b $w.buttonFrame
    toplevel $w -class Dialog
    wm transient $w [winfo toplevel [winfo parent $w]]	
    wm withdraw $w
#	
    frame $f
    pack $f -side top -padx 5 -pady 5 -expand off -anchor n
#
    frame $l
    pack $l -side top -padx 5 -pady 5 -expand on -fill both
	
    listbox $l.elist -width 48 -height 16 -yscrollcommand "$l.sy set" \
	-selectbackground navy -selectforeground white -listvar ::ifGrp::gList
    grid $l.elist -in $l \
	-row 2 -column 0 -columnspan 1 -rowspan 1 -sticky news
    scrollbar $l.sy -command "$l.elist yview"
    grid $l.sy -in $l \
	-row 2 -column 1 -columnspan 1 -rowspan 1 -sticky news
    bind $l.elist <Double-1> "EditIfGrp $w edit $l.elist"
#
    frame $c
    pack $c -side bottom -padx 5 -pady 5 -expand off -anchor s 
    button $c.new    -text [msgcat::mc "Add"]    -command "EditIfGrp $w add     $l.elist"
    button $c.edit   -text [msgcat::mc "Edit"]   -command "EditIfGrp $w edit    $l.elist"
    button $c.delete -text [msgcat::mc "Delete"] -command "EditIfGrp $w delete  $l.elist" 
    pack $c.new $c.edit $c.delete -side left -padx 5 -pady 5	
#    
    frame $b
    pack $b -side bottom -padx 5 -pady 5 -expand off -anchor s
    button $b.ok -text [msgcat::mc "Ok"] -command "set ::ifGrp::viewIfGrp_exitFlag 1"
    button $b.cancel -text [msgcat::mc "Cancel"] -command "set ::ifGrp::viewIfGrp_exitFlag 2"
    pack $b.ok $b.cancel -side left -padx 5 -pady 5
#
    update idletasks
    set x [expr {[winfo screenwidth $w]/2 - [winfo reqwidth $w]/2 \
	- [winfo vrootx [winfo parent $w]]}]
    set y [expr {[winfo screenheight $w]/2 - [winfo reqheight $w]/2 \
	- [winfo vrooty [winfo parent $w]]}]
    wm geom $w +$x+$y
#
    wm resizable $w 0 0
    bind $w <Destroy> "set ::ifGrp::viewIfGrp_exitFlag 2"
    bind $w <Escape>  "set ::ifGrp::viewIfGrp_exitFlag 2"
}
#
proc ViewIfGrp { } {
    #
    set w .groups
    set f $w.mFrame
    set l $f.listFrame	
    set c $f.btFrame	
    set b $w.buttonFrame
    #
    if { [winfo exists $w] } {
	if { [string equal [wm state $w] withdrawn ] } {
	    wm deiconify $w
	}
	raise $w    
    } else {
	ViewIfGrp_GUIconstructor
    }
#    
#  Load actual data
#
    wm title $w [msgcat::mc "Group list" ]

    set ::ifGrp::gList       $::ifGroupList
    array set ::ifGrp::gBodyArray [ array get ::ifGroupDefinition ]
#
# 
#
    set ::ifGrp::viewIfGrp_exitFlag 0
    update idletasks
    wm deiconify $w
    raise $w
    set oldFocus [focus]
    set oldGrab [grab current $w]
    if { [string compare $oldGrab ""] } {
	set grabStatus [grab status $oldGrab]
    }
    grab $w 
    focus $b.ok
    
    tkwait variable ::ifGrp::viewIfGrp_exitFlag

    if {[winfo exists $w]} {
	wm withdraw $w
	catch {focus $oldFocus}
	grab release $w
	if {$::ifGrp::viewIfGrp_exitFlag==1} {
	    # Ok button pressed
	    set ::ifGroupList  $::ifGrp::gList
	    array set ::ifGroupDefinition [array get ::ifGrp::gBodyArray]
	}
    }
    # cleanup entries
    set ::ifGrp::gList [ list ]
    array unset ::ifGrp::gBodyArray
    if {[string compare $oldGrab ""]} {
	if {![string compare $grabStatus "global"]} {
	    grab -global $oldGrab
	} else {
	    grab $oldGrab
	}
    }
    return
}

#
#  add dialog for pregeneration
#
lappend ::GUI_pregenerate_list [list {ViewIfGrp_GUIconstructor} {Loading Interfaces Groups dialog...}]
#
