#
#  Module Text.tcl
#  
# $Log: Text.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 textDialog {
variable	textProperties_exitFlag	0 \
		ljustify	"center"
}
#
proc NewText {} {
    updateScrollRegion {}
}
#
proc TextProperties_GUIconstructor { } {
    set w .textProperties
    set g $w.grid
    set c $w.canvas
    set f $w.entries
    set fj $f.just
    set fc $f.coords
    set ff $f.font
    set fl $f.color
    set b $w.buttons

    toplevel .textProperties -class Dialog 
    wm transient $w [winfo toplevel [winfo parent $w]]
    wm title $w [msgcat::mc "Text Properties"]
    wm withdraw $w

    frame $g

    canvas $c -width 256 -height 128 -relief sunken  -bg $::globalBG
    $c create text 128 64  -text "XXX" -font fixed -fill $::globalFG -anchor center 
    text $w.etext  -width 32 -height 6 
    pack $g -fill both -expand on -padx 5 -pady 8 -side top

    grid rowconfig $g 0 -weight 1 -minsize 0
    grid rowconfig $g 1 -weight 1 -minsize 0
    grid columnconfig $g 0 -weight 1 -minsize 0
    grid $c -padx 5 -pady 2 -in $g \
	-row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news
    grid $w.etext -padx 5 -pady 2 -in $g \
	-row 1 -column 0 -rowspan 1 -columnspan 1 -sticky news    

    bind $w.etext <FocusOut> "updateTextView"

    bind $c <Configure> {
	set c_list [ grid bbox .textProperties.grid 0 0 0 0 ]
	set new_w [expr [lindex $c_list 2]/2]
	set new_h [expr [lindex $c_list 3]/2]
	.textProperties.canvas coords 1 $new_w $new_h
    }
    bind $c <Double-1> "updateTextView"

    frame $f
    pack $f -side top

    frame $fj
    pack $fj -fill both -side top -expand on 
    label $fj.ltext -text [msgcat::mc "Justify:"] 
    radiobutton $fj.rbleft -text [msgcat::mc "Left"]   \
	-variable ::textDialog::ljustify -value "left"
    radiobutton $fj.rbcenter -text [msgcat::mc "Center"]  \
	-variable ::textDialog::ljustify -value "center"
    radiobutton $fj.rbright -text [msgcat::mc "Right"]   \
	-variable ::textDialog::ljustify -value "right"
    pack $fj.ltext $fj.rbleft $fj.rbcenter $fj.rbright -side left -padx 5 -pady 5 
    bind $fj.rbleft <FocusOut> "updateTextView"
    bind $fj.rbcenter <FocusOut> "updateTextView"
    bind $fj.rbright <FocusOut> "updateTextView"

    frame $fc
    pack $fc -side top 
    label $fc.lx -text "X: " 
    entry $fc.ex -width 8 \
        -validatecommand { IsValidCoordinate [%W get] } \
	-invalidcommand {
	    selection clear
	    tk_dialog .textProperties.dlg [msgcat::mc "Error"]  \
	        [msgcat::mc "Invalid X coordinate."] \
		error 0 [msgcat::mc "Ok" ]
	    focus %W	    
	}
	
    label $fc.ly -text "Y: " 
    entry $fc.ey -width 8  \
        -validatecommand {  IsValidCoordinate [%W get] 	} \
	-invalidcommand {
	    selection clear
	    tk_dialog .textProperties.dlg [msgcat::mc "Error"]  \
	        [msgcat::mc "Invalid Y coordinate."] \
		error 0 [msgcat::mc "Ok" ]
	    focus %W	    
	}
    pack   $fc.lx $fc.ex $fc.ly $fc.ey -side left -padx 5 -pady 5 

    frame $ff
    pack $ff -side top 
    label $ff.lfont -text [msgcat::mc "Text  font:"] 
    entry $ff.efont -width 20 \
	-validatecommand { IsValidFont [%W get ] } \
	-invalidcommand  {
	    selection clear
	    tk_dialog .textProperties.dlg [msgcat::mc "Error" ]  \
	        [ msgcat::mc "Invalid Font." ] \
		error 0 [msgcat::mc "Ok" ]
	    focus %W
	}	
    button $ff.bfont -text "..."  -command "tk_chooseFont $ff.efont"
    pack $ff.lfont $ff.efont $ff.bfont -side left -padx 5 -pady 5 
    bind $ff.efont <FocusOut> "updateTextView"

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

    frame $b
    pack $b -side bottom -padx 10 -pady 10

    button $b.apply -text [msgcat::mc "Apply"]  -command {
	set w .textProperties
	set f $w.entries
	set fc $f.coords
	set ff $f.font
	set fl $f.color
	if { ![$fc.ex validate ] || ![$fc.ey validate] ||
	     ![$ff.efont validate] || ![$fl.ecolor validate] 
	   } {	return  }
	set ::textDialog::textProperties_exitFlag 1
    }
    button $b.cancel  -text [msgcat::mc "Cancel"]  -command "set ::textDialog::textProperties_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 ::textDialog::textProperties_exitFlag 2"
}
#
proc TextProperties { id } {

    set w .textProperties
    set g $w.grid
    set c $w.canvas
    set f $w.entries
    set fj $f.just
    set fc $f.coords
    set ff $f.font
    set fl $f.color
    set b $w.buttons
        
    if { ![winfo exists $w] } {
	TextProperties_GUIconstructor
    }    
#
#   Load actual data
#
    $w.etext insert 0.0 [.canvas itemcget $id -text]
    set need_space [$c bbox 1 ]
    set x1 [lindex $need_space 0]
    set y1 [lindex $need_space 1]
    set x2 [lindex $need_space 2]
    set y2 [lindex $need_space 3]
    if { [expr ($y2-$y1)>128] || [expr ($x2-$x1)>256]} {
        $c configure -height [expr $y2 - $y1 + 16] \
	    -width [expr $x2 - $x1 +16]
    }
    set ::textDialog::ljustify [.canvas itemcget $id -justify ]
    switch $::textDialog::ljustify {
	left   { $fj.rbleft select }
	center { $fj.rbcenter select}
	right  { $fj.rbright select}
    }
    set lXY [.canvas coords $id]
    $fc.ex delete 0 end
    $fc.ex insert 0 [lindex $lXY 0]
    $fc.ey delete 0 end
    $fc.ey insert 0 [lindex $lXY 1]
    $ff.efont insert 0 [.canvas itemcget $id -font]
    $fl.ecolor insert 0 [.canvas itemcget $id -fill]
# start validation
    $fc.ex configure -validate focusout
    $fc.ey configure -validate focusout
#
    updateTextView
    set ::textDialog::textProperties_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 ::textDialog::textProperties_exitFlag
    
    if { [winfo exists $w ] } {
	wm withdraw $w
	catch {focus $oldFocus}
	grab release $w 
	if { $::textDialog::textProperties_exitFlag==1 } {
	    set gc .canvas
	    $gc itemconfigure $id -text [$w.etext get 0.0 "end-1 chars"] 
	    $gc coords $id [$fc.ex get] [$fc.ey get] 
	    $gc itemconfigure $id -justify $::textDialog::ljustify
	    $gc itemconfigure $id -font [$ff.efont get] 
	    $gc itemconfigure $id -fill [$fl.ecolor get] 
	    updateScrollRegion $id
	    set ::activeFileChanged "yes"
	}
	# stop validation
	$fc.ex configure -validate none
	$fc.ey configure -validate none
	# cleanup
	$w.etext delete 0.0 end
	$fc.ex delete 0 end
	$fc.ey delete 0 end
	$ff.efont delete 0 end
	$fl.ecolor delete 0 end
	 
    }
    if {[string compare $oldGrab ""]} {
	if {![string compare $grabStatus "global"]} {
	    grab -global $oldGrab
	} else {
	    grab $oldGrab
	}
    }
    return 
}

proc updateTextView {} {
    set w .textProperties
    set wc $w.canvas
    $wc itemconfigure 1 -text [$w.etext get 0.0 "end-1 chars"]
    $wc itemconfigure 1 -justify $::textDialog::ljustify
    set tmp_fnt [$w.entries.font.efont get]
    if { $tmp_fnt !="" && [IsValidFont $tmp_fnt] } {
	$wc itemconfigure 1 -font $tmp_fnt
    }
    set tmp_clr [$w.entries.color.ecolor get]
    if { $tmp_clr !="" && [IsValidColor $tmp_clr] } {
	$wc itemconfigure 1 -fill $tmp_clr
    }
    set need_space [$wc bbox 1  ]
    set x1 [lindex $need_space 0]
    set y1 [lindex $need_space 1]
    set x2 [lindex $need_space 2]
    set y2 [lindex $need_space 3]
    if { [expr ($y2-$y1)>128] || [expr ($x2-$x1)>256]} {
	$wc configure -height [expr $y2 - $y1 + 16] \
	    -width [expr $x2 - $x1 + 16]
    }
}
#
# Text popup while edit enabled
#
proc mkTextPopupMenu {} {
    uplevel #0 {
  set m .canvas.canvas_text_popup
  menu $m -tearoff 0 
  $m add command -label [msgcat::mc "Properties"] -command { 
    global activeID
    TextProperties $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 {TextProperties_GUIconstructor} {Loading TextProperties dialog...}]
#