#
# Module Link.tcl
#
# $Log: Link.tcl,v $
# Revision 1.2  2006/10/26 09:39:17  vfom
# Added code for netstate server query
#
# Revision 1.1.1.1  2006/09/21 10:57:02  vfom
# Loading TkNetmon to CVS
#
#
namespace eval linkDialog {
variable	linkProperties_exitFlag	0
}
#
proc NewLink {} {
}
#
proc LinkProperties_GUIconstructor { } {
    set w .linkProperties
    set v $w.view
    set vc $v.canvas
    set e $w.entries
    set ew $e.width
    set ec $e.color    
    set b $w.buttons
        
    toplevel $w -class Dialog
    wm transient $w [winfo toplevel [winfo parent $w]]
    wm title $w [msgcat::mc "Link Properties"]
    wm withdraw $w

    frame $v
    pack $v -side top -padx 15 -pady 15
    canvas $vc -width 256 -height 128 -relief sunken  -bg $::globalBG
    $vc create line 32 32 224 96  -width 1 -fill $::globalFG
    pack $vc

    frame $e
    pack $e -side top

    frame $ew
    pack $ew -side top 
    label $ew.lwidth -text [msgcat::mc "Link width:"] 
    entry $ew.ewidth -width 4 \
	-validatecommand { IsValidLineWidth [%W get ] } \
	-invalidcommand  {
	    selection clear
	    tk_dialog .linkProperties.dlg [msgcat::mc "Error" ]  \
	        [ msgcat::mc "Invalid link width." ] \
		error 0 [msgcat::mc "Ok" ]
	    focus %W
	}	

    pack $ew.lwidth $ew.ewidth -side left -padx 5 -pady 5 
    bind $ew.ewidth <FocusOut> "updateLinkView"

    frame $ec
    pack $ec -side top 
    label $ec.lcolor -text [msgcat::mc "Link color:"]
    entry $ec.ecolor -width 10 \
	-validatecommand { IsValidColor [%W get ] } \
	-invalidcommand  {
	    selection clear
	    tk_dialog .linkProperties.dlg [msgcat::mc "Error" ]  \
	        [ msgcat::mc "Invalid Color." ] \
		error 0 [msgcat::mc "Ok" ]
	    focus %W
	}	
     
    button $ec.bcolor -text "..."  -command "tk_chooseColor2 $ec.ecolor"
    pack $ec.lcolor $ec.ecolor $ec.bcolor -side left -padx 5 -pady 5 
    bind $ec.ecolor <FocusOut> "updateLinkView"

    frame $b
    pack $b -side bottom

    button $b.apply -text [msgcat::mc "Apply"]   -command { 
	set w .linkProperties
	set e $w.entries
	set ew $e.width
	set ec $e.color    
	if  { ![$ew.ewidth validate] || ![$ec.ecolor validate ] } {
	    return
	}
	set ::linkDialog::linkProperties_exitFlag 1
    }
    button $b.cancel  -text [msgcat::mc "Cancel"]  -command "set ::linkDialog::linkProperties_exitFlag 2"
    pack  $b.apply $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
    bind $w <Destroy> "set ::linkDialog::linkProperties_exitFlag 2"
}

proc LinkProperties { id } {
    set w .linkProperties
    set v $w.view
    set vc $v.canvas
    set e $w.entries
    set ew $e.width
    set ec $e.color    
    set b $w.buttons
    
    if { ![winfo exists $w] } {
	LinkProperties_GUIconstructor    
    }
#    
# Load actual data
#
    $ew.ewidth insert 0 [.canvas itemcget $id -width]
    $ec.ecolor insert 0 [.canvas itemcget $id -fill]
# start validation
    $ew.ewidth configure -validate focusout
#
    updateLinkView
    set ::linkDialog::linkProperties_exitFlag 0
    update idletasks    
    if { [string equal [wm state $w] withdrawn ] } {
        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.apply

    tkwait variable ::linkDialog::linkProperties_exitFlag

    if { [winfo exists $w ] } { 
	wm withdraw $w
	catch {focus $oldFocus}
	grab release $w
	if { $::linkDialog::linkProperties_exitFlag==1 } {	
	    .canvas itemconfigure $id -width [$ew.ewidth get] -fill [$ec.ecolor get] 
	    set ::activeFileChanged "yes"
	}
	# stop validate
	$ew.ewidth configure -validate none
	# cleanup
	$ew.ewidth delete 0 end
	$ec.ecolor delete 0 end	
    }
    if {[string compare $oldGrab ""]} {
	if {![string compare $grabStatus "global"]} {
	    grab -global $oldGrab
	} else {
	    grab $oldGrab
	}
    }
    return 
}

proc updateLinkView {} {
    set w .linkProperties
    set v $w.view
    set vc $v.canvas
    set e $w.entries
    set ew $e.width
    set ec $e.color    
    if { [IsValidLineWidth [$ew.ewidth get]] } {
	$vc itemconfigure 1 -width [$ew.ewidth get] 
    }
    if { [IsValidColor [$ec.ecolor get]] } {
	$vc itemconfigure 1 -fill  [$ec.ecolor get] 
    }
}
#
# Link popup menu while  edit enabled
#
#
proc mkLinkPopupMenu {} {
    uplevel #0  {
  set m .canvas.canvas_link_popup
  menu $m -tearoff 0 
  $m add command -label [msgcat::mc "Properties"] -command { 
    global activeID
    LinkProperties $activeID
  }
  $m add command -label [msgcat::mc "Delete"] -command { 
    global activeID 
    .canvas delete $activeID
  }
  bind $m <FocusOut> {    .canvas delete [.canvas find withtag tempFrame ] }
#
# If edit disabled - no popup invoked
#
}}
#
#
#  add dialog for pregeneration
#
lappend ::GUI_pregenerate_list [list {LinkProperties_GUIconstructor} {Loading LinkProperties dialog...}]
#