#
# Module fontDialog.tcl
#
# $Log: fontDialog.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
#
# This is simple font selection dialog 
# 
namespace eval selectFont {
variable	ChooseFont_exitFlag	0	\
		family_list		[list]  \
		isbold			false	\
		isitalic		false	\
		isunderline		false	\
		isoverstrike		false	\
		size_list		[list 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 26 28 30 32 34]
}
#
#set ::selectFont::family_list [list Arial "Arial Black" "Andale Mono" "Courier New" "Comic Sans MS" Georgia Impact Verdana "Times New Roman" "Trebuchet MS"]      
#
proc UpdateFontView {} {
    set c .fontSelect.view.cv
    set f .fontSelect.fontFrame
    set fs $f.styleFrame
    set fnt [$f.efnt get]
    set size [$f.esize get]
    set style ""
    if { $::selectFont::isbold } { set style "bold" }
    if { $::selectFont::isitalic } { set style "$style italic" }
    if { $::selectFont::isunderline } { set style "$style underline" }
    if { $::selectFont::isoverstrike } { set style "$style overstrike" }
    set fff "\{$fnt\} $size $style"    
    $c itemconfigure 1 -font fixed
    $c itemconfigure 1 -font $fff
}
#
proc ChooseFont_GUIconstructor { } {
    set w .fontSelect
    set f $w.fontFrame
    set fs $f.styleFrame
    set fv $w.view
    set b $w.buttonFrame
    # 
    # Create dialog
    #
    toplevel $w -class Dialog
    wm transient $w [winfo toplevel [winfo parent $w]]	
    wm title $w "Font selection Dialog"
    wm withdraw $w
    
    frame $f
    pack $f -side top -anchor nw -padx 5 -pady 5 -expand on -fill both
#
    label $f.lfnt -text "Font:"
    grid $f.lfnt -in $f \
	-row 0 -column 0 -columnspan 2 -rowspan 1 -sticky w
    label $f.lsize -text "Size:" 
    grid $f.lsize -in $f \
	-row 0 -column 2 -columnspan 2 -rowspan 1 -sticky w
    label $f.lstyle -text "  Style:"
    grid $f.lstyle -in $f \
	-row 0 -column 4 -columnspan 2 -rowspan 1 -sticky w
    #
    entry $f.efnt -width 32 -state disabled
    grid $f.efnt -in $f \
	-row 1 -column 0 -columnspan 2 -rowspan 1 -sticky news

    entry $f.esize -width 7 -state disabled
    grid $f.esize -in $f \
	-row 1 -column 2 -columnspan 2 -rowspan 1 -sticky news
    #
    listbox $f.sfnt -width 30 -height 8 -yscrollcommand "$f.syfnt set"
    grid $f.sfnt -in $f \
	-row 2 -column 0 -columnspan 1 -rowspan 1 -sticky news
    scrollbar $f.syfnt -command "$f.sfnt yview"
    grid $f.syfnt -in $f \
	-row 2 -column 1 -columnspan 1 -rowspan 1 -sticky news

    bind $f.sfnt <Double-1> {	
	set f .fontSelect.fontFrame
	$f.efnt configure -state normal    
	$f.efnt delete 0 end
	$f.efnt insert 0 [$f.sfnt get [$f.sfnt curselection]]
	$f.efnt configure -state disabled  
	UpdateFontView
    }
	    	    
    listbox $f.ssize -width 5 -height 8 -yscrollcommand "$f.sysize set"
    grid $f.ssize -in $f \
	-row 2 -column 2 -columnspan 1 -rowspan 1 -sticky news
    scrollbar $f.sysize -command "$f.ssize yview"
    grid $f.sysize -in $f \
	-row 2 -column 3 -columnspan 1 -rowspan 1 -sticky news

    bind $f.ssize <Double-1> {	
	set f .fontSelect.fontFrame
	$f.esize configure -state normal    
	$f.esize delete 0 end
	$f.esize insert 0 [$f.ssize get [$f.ssize curselection]]
	$f.esize configure -state disabled  
	UpdateFontView
    }
    #
    frame $fs -relief sunken -borderwidth 2
    grid $fs -in $f \
	-row 2 -column 4 -columnspan 1 -rowspan 1 -sticky news
    checkbutton $fs.chkbold -text "Bold"       -variable selectFont::isbold       -command "UpdateFontView" \
	-onvalue true -offvalue false 
    checkbutton $fs.chkital -text "Italic"     -variable selectFont::isitalic     -command "UpdateFontView" \
	-onvalue true -offvalue false 
    checkbutton $fs.chkundr -text "Underline"  -variable selectFont::isunderline  -command "UpdateFontView" \
    	-onvalue true -offvalue false 
    checkbutton $fs.chkovr  -text "OverStrike" -variable selectFont::isoverstrike -command "UpdateFontView" \
    	-onvalue true -offvalue false 
    
    pack $fs.chkbold $fs.chkital $fs.chkundr $fs.chkovr -side top -padx 5 -pady 5 -anchor w
    #
    frame $fv -relief raised
    pack $fv -side top  -anchor nw -padx 5 -pady 5 -expand on -fill both

    label $fv.lb -text "Example:"
    pack $fv.lb -side top -padx 5 -pady 5 
    
    canvas $fv.cv  -height 100p -bg white -state disabled -relief sunken
    pack $fv.cv -side bottom \
        -padx 5 -pady 5 -expand on -fill both 
    $fv.cv create text 5 3 -anchor nw -justify left -font fixed \
	    -text {
0123456789 !@#$%^&*()_+\?<>,./|[]{}
abcdefjhijklmnopqrstuwxyz
ABCDEFGHIJKLMNOPQRSTUWXYZ
ÁÂ×ÇÄÅ£ÖÚÉÊËÌÍÎÏÐÒÓÔÕÆÈÃÞÛÝßÙØÜÀÑ
áâ÷çäå³öúéêëìíîïðòóôõæèãþûýÿùøüàñ

}		    
    frame $b 
    pack $b -side bottom -padx 5 -pady 5 -expand off -anchor s
    button $b.ok -text "Ok" -command "set selectFont::ChooseFont_exitFlag 1"
    button $b.cancel -text "Cancel" -command "set selectFont::ChooseFont_exitFlag 2"
    pack $b.ok $b.cancel -side left -padx 5 -pady 5

    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
    #
    if { [llength $::selectFont::family_list] == 0 } {
	set ::selectFont::family_list [lsort [font families ]]
    }
    foreach i $::selectFont::family_list { $f.sfnt  insert end $i }
    foreach i $::selectFont::size_list   { $f.ssize insert end $i }
    UpdateFontView    
    
    wm resizable $w 0 1
    bind $w <Destroy> "set selectFont::ChooseFont_exitFlag 2"

}
#
proc ChooseFont { fdescr } {
    #
    set w .fontSelect
    set f $w.fontFrame
    set fv $w.view
    set b $w.buttonFrame
    #
    if { ![winfo exists $w] } {
	ChooseFont_GUIconstructor
    }    
#    
#  Load actual data
#
    $f.efnt configure -state normal
    $f.efnt delete 0 end
    $f.esize configure -state normal
    $f.esize delete 0 end
    if { $fdescr== "" } {
	$f.efnt insert   0 [ $f.sfnt get 0]
	$f.esize insert  0 "8"
	set selectFont::isbold		false
	set selectFont::isitalic	false
	set selectFont::isunderline	false
	set selectFont::isoverstrike	false
    } else {
	$f.efnt insert   0 [ font actual "$fdescr" -family ]
	$f.esize insert  0 [ font actual "$fdescr" -size ]
	if { [string equal  [ font actual "$fdescr" -weight ] bold ] } {
	    set selectFont::isbold		true	
	}
	if { [string equal  [ font actual "$fdescr" -slant ] italic ] } {
	    set selectFont::isitalic		true	
	}
	if  { [ font actual "$fdescr" -underline ] == 1 } {
	    set selectFont::isunderline		true
	}
	if  { [ font actual "$fdescr" -overstrike ] == 1 } {
	    set selectFont::isoverstrike	true
	}	    
    }
    $f.efnt configure -state disabled
    $f.esize configure -state disabled
    set idx [ lsearch $::selectFont::family_list [ $f.efnt get ]]
    if { $idx >0 } { $f.sfnt see $idx }   
    UpdateFontView
#
# 
#
    set selectFont::ChooseFont_exitFlag 0
    if { [string equal [wm state $w] withdrawn ] } {
        wm deiconify $w
    }
    raise $w
    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 selectFont::ChooseFont_exitFlag

    if {[winfo exists $w]} {
	wm withdraw $w
	catch {focus $oldFocus}
	grab release $w 
	if {$selectFont::ChooseFont_exitFlag==1} {
	    # Ok button pressed
	    set fdescr [$fv.cv itemcget 1 -font ]
	}
	# cleanup entries
    }
    #
    if {[string compare $oldGrab ""] && [winfo exists $oldGrab] } {
	if {![string compare $grabStatus "global"]} {
	    grab -global $oldGrab
	} else {
	    grab $oldGrab
	}
    }
    return $fdescr
}
#
#
#  add dialog for pregeneration
#
lappend ::GUI_pregenerate_list [list {ChooseFont_GUIconstructor} {Loading ChooseFont dialog...}]
#