#
#
#  Module Line.tcl
#
#  $Log: Line.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 lineDialog {
variable	lineProperties_exitFlag	0
}
#
proc NewLine {} {
    updateScrollRegion {}
}
#
proc LineProperties_GUIconstructor { } {
    set w .lineProperties
    set v $w.view
    set vc $v.canvas
    set e $w.entries
    set ef $e.first
    set es $e.second
    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 "Line 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 $ef
    pack $ef -side top 

    label $ef.lx1 -text "X1: " 
    entry $ef.ex1 -width 8 \
        -validatecommand { IsValidCoordinate [%W get] } \
	-invalidcommand {
	    selection clear
	    tk_dialog .lineProperties.dlg [msgcat::mc "Error"]  \
	        [msgcat::mc "Invalid X1 coordinate."] \
		error 0 [msgcat::mc "Ok" ]
	    focus %W	    
	}
	
    label $ef.ly1 -text "Y1: " 
    entry $ef.ey1 -width 8  \
        -validatecommand { IsValidCoordinate [%W get] } \
	-invalidcommand {
	    selection clear
	    tk_dialog .lineProperties.dlg [msgcat::mc "Error"]  \
	        [msgcat::mc "Invalid Y1 coordinate."] \
		error 0 [msgcat::mc "Ok" ]
	    focus %W	    
	}
    pack $ef.lx1 $ef.ex1 $ef.ly1 $ef.ey1 -side left -padx 5 -pady 5 

    frame $es
    pack $es -side top  
    label $es.lx2 -text "X2: " 
    entry $es.ex2 -width 8 \
        -validatecommand { IsValidCoordinate [%W get] } \
	-invalidcommand {
	    selection clear
	    tk_dialog .lineProperties.dlg [msgcat::mc "Error"]  \
	        [msgcat::mc "Invalid X2 coordinate."] \
		error 0 [msgcat::mc "Ok" ]
	    focus %W	    
	}
    label $es.ly2 -text "Y2: " 
    entry $es.ey2 -width 8 \
        -validatecommand { IsValidCoordinate [%W get] } \
	-invalidcommand {
	    selection clear
	    tk_dialog .lineProperties.dlg [msgcat::mc "Error"]  \
	        [msgcat::mc "Invalid Y2 coordinate."] \
		error 0 [msgcat::mc "Ok" ]
	    focus %W	    
	}
    pack   $es.lx2 $es.ex2 $es.ly2 $es.ey2 -side left -padx 5 -pady 5 

    frame $ew
    pack $ew -side top 
    label $ew.lwidth -text [msgcat::mc "Line width:"] 
    entry $ew.ewidth -width 4 \
        -validatecommand { IsValidLineWidth [%W get] } \
	-invalidcommand {
	    selection clear
	    tk_dialog .lineProperties.dlg [msgcat::mc "Error"]  \
	        [msgcat::mc "Invalid line width."] \
		error 0 [msgcat::mc "Ok" ]
	    focus %W	    
	}
    pack $ew.lwidth $ew.ewidth -side left -padx 5 -pady 5 
    bind $ew.ewidth <FocusOut> "updateLineView"

    frame $ec
    pack $ec -side top 
    label $ec.lcolor -text [msgcat::mc "Line color:"] 
    entry $ec.ecolor -width 10 \
        -validatecommand { IsValidColor [%W get] } \
	-invalidcommand {
	    selection clear
	    tk_dialog .lineProperties.dlg [msgcat::mc "Error"]  \
	        [msgcat::mc "Invalid line 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> "updateLineView"

    frame $b
    pack $b -side bottom

    button $b.apply -text [msgcat::mc "Apply"]   -command { 
	set w .lineProperties
	set e $w.entries
	set ef $e.first
	set es $e.second
	set ew $e.width
	set ec $e.color

	if { ![$ef.ex1 validate] || ![$ef.ey1 validate] ||
	     ![$es.ex2 validate] || ![$es.ey2 validate] ||
	     ![$ew.ewidth validate ] || ![$ec.ecolor validate] 
	   } {	return  }
	set  ::lineDialog::lineProperties_exitFlag 1
    }
    button $b.cancel  -text [msgcat::mc "Cancel"]  -command "set  ::lineDialog::lineProperties_exitFlag 2"

    pack $b.apply $b.cancel  -side left -padx 5 -pady 5

    wm withdraw $w
    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  ::lineDialog::lineProperties_exitFlag 2"

}

proc LineProperties { id } {
    set w .lineProperties
    set v $w.view
    set vc $v.canvas
    set e $w.entries
    set ef $e.first
    set es $e.second
    set ew $e.width
    set ec $e.color
    set b $w.buttons


    if { ![winfo exists $w] } {
	LineProperties_GUIconstructor
    }
#
#  Load actual data
#
    set lXY [.canvas coords $id]
    $ef.ex1 insert 0 [lindex $lXY 0]
    $ef.ey1 insert 0 [lindex $lXY 1]
    $es.ex2 insert 0 [lindex $lXY 2]
    $es.ey2 insert 0 [lindex $lXY 3]
    $ew.ewidth insert 0 [.canvas itemcget $id -width]
    $ec.ecolor insert 0 [.canvas itemcget $id -fill]
# 
#  start validation
#
    $ef.ex1 configure -validate focusout
    $ef.ey1 configure -validate focusout
    $es.ex2 configure -validate focusout
    $es.ey2 configure -validate focusout
    $ew.ewidth configure -validate focusout
#
    updateLineView
    set  ::lineDialog::lineProperties_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 ::lineDialog::lineProperties_exitFlag

    if { [winfo exists $w] } {
	wm withdraw $w
	catch {focus $oldFocus}
	grab release $w
	if { $::lineDialog::lineProperties_exitFlag==1 } {
    	    .canvas coords $id [$ef.ex1 get] [$ef.ey1 get] [$es.ex2 get] [$es.ey2 get]
	    .canvas itemconfigure $id -width [$ew.ewidth get] -fill  [$ec.ecolor get] 
	    updateScrollRegion $id
	    set ::activeFileChanged "yes"
	} 
    #
	$ef.ex1 configure -validate none
	$ef.ey1 configure -validate none
	$es.ex2 configure -validate none
	$es.ey2 configure -validate none
	$ew.ewidth configure -validate none
    #	
	$ef.ex1 delete 0 end
	$ef.ey1 delete 0 end
	$es.ex2 delete 0 end
	$es.ey2 delete 0 end
	$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 updateLineView {} {
    set w .lineProperties
    set v $w.view
    set vc $v.canvas
    set e $w.entries
    set ew $e.width
    set ec $e.color
    set tmp_wdt [$ew.ewidth get]
    set tmp_clr [$ec.ecolor get]
    
    if { $tmp_wdt!="" && [IsValidLineWidth $tmp_wdt ] } {
	$vc itemconfigure 1 -width $tmp_wdt
    }
    if { $tmp_clr!="" && [IsValidColor $tmp_clr ]  } {
	$vc itemconfigure 1 -fill  $tmp_clr
    }
}
#
# Line popup menu while edit enabled
#
proc mkLinePopupMenu {} {
    uplevel #0 {
  set m .canvas.canvas_line_popup
  menu $m -tearoff 0 
  $m add command -label [msgcat::mc "Properties"] -command { 
    global activeID
    LineProperties $activeID
  }
  $m add command -label [msgcat::mc "Delete"] -command { 
    global activeID
    .canvas delete $activeID
  }
  bind $m <FocusOut> {    .canvas delete [.canvas find withtag tempFrame ] }
#
# While edit disabled - no popup invoked
#
}}
#
#
#  add dialog for pregeneration
#
lappend ::GUI_pregenerate_list [list {LineProperties_GUIconstructor} {Loading LineProperties dialog...}]
#