#
# Module Dialogs.tcl
#
#
# $Log: Dialogs.tcl,v $
# Revision 1.2  2006/10/26 09:39:17  vfom
# Added code for netstate server query
#
#
proc Play4test { w } {
    global soundList arSounds
    set lind  [ $w curselection ]
    set i [lindex $soundList $lind ]
    playSound $arSounds($i) 
}
#
proc tk_chooseSound  { parentW entryW  } {
    global soundList nodeUpSound tk_chooseSound_exitFlag arSounds
    set action ""

    set w $parentW.chooseSound
    if [winfo exists $w ] {
	raise $w 
	return
    }
    toplevel $w -class Dialog
    wm transient $w [winfo toplevel [winfo parent $w]]

    frame $w.entry
    pack $w.entry -padx 10 -pady 10

    set l $w.entry.l
    frame $l

    listbox $l.list -width 16 -height 8   -selectmode single\
	-yscrollcommand "$l.scroll set"
    scrollbar $l.scroll -command "$l.list yview"
    eval "$l.list insert 0 $soundList"
    set tmp_sound [ $entryW get ]
    if { [catch { $arSouns($tmp_sound) get -file } ] } {
	set tmp_sound $nodeUpSound
    }

    set lind [lsearch -exact $soundList $tmp_sound ]
    if { $lind >0 } {
	$l.list selection set $lind
	$l.list see $lind
    } else {
	$l.list selection set 0
	$l.list see 0
    }

    pack $l.list $l.scroll  -side left -fill y -expand 1

    set r $w.entry.r
    frame $r

    set s_play [ image create bitmap -data {
#define Play_width 32
#define Play_height 32
static char Play_bits[] = {
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x1f,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00 0x00, 0x00, 0x34, 0x00,
   0x00, 0x00, 0x04, 0x00, 0xf8, 0xff, 0xff, 0x1f, 0x00, 0x00, 0x04, 0x00,
   0x00, 0x00, 0x04, 0x06, 0x00, 0x80, 0x05, 0x1a, 0x00, 0xc0, 0x07, 0x02,
   0xf8, 0xff, 0xff, 0x1f 0x00, 0xc0, 0x03, 0x02, 0x00, 0x8c, 0x01, 0x02,
   0x00, 0x34, 0xc0, 0x02, 0x00, 0x04, 0xe0, 0x03, 0xf8, 0xff, 0xff, 0x1f,
   0x00, 0x04, 0xe0, 0x01, 0x00, 0x04, 0xc0, 0x00, 0x80, 0x05, 0x00, 0x00
   0xc0, 0x07, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x1f, 0xc0, 0x03, 0x00, 0x00,
   0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
}]
    set s_vol [ image create bitmap -data  {
#define Vol_width 32
#define Vol_height 32
static char Vol_bits[] = {
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0xfc, 0x00, 0x20, 0x00, 0xfc, 0x00, 0x30, 0x00, 0xfc, 0x00, 0x38, 0x00,
   0xfc, 0x00, 0x2c, 0x00, 0xfc, 0x00, 0x26, 0x00 0xf8, 0x00, 0x23, 0x08,
   0xf8, 0x80, 0x21, 0x04, 0xf8, 0xfc, 0x20, 0x02, 0xf8, 0x7c, 0x20, 0x01,
   0xf0, 0x7c, 0x20, 0x00, 0xf0, 0x7c, 0x20, 0x00, 0xf0, 0x7c, 0x20, 0x00,
   0xf0, 0x7c, 0x20, 0x0f 0xe0, 0x7c, 0x20, 0x00, 0xe0, 0x7c, 0x20, 0x00,
   0xe0, 0x7c, 0x20, 0x00, 0xe0, 0x7c, 0x20, 0x01, 0xc0, 0xfc, 0x20, 0x02,
   0xc0, 0x80, 0x21, 0x04, 0xc0, 0x00, 0x23, 0x08, 0xc0, 0x00, 0x26, 0x00
   0x80, 0x00, 0x2c, 0x00, 0x80, 0x00, 0x38, 0x00, 0x80, 0x00, 0x30, 0x00,
   0x80, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
}]
    
    button $r.play -image $s_play -command "Play4test $l.list"
    button $r.vol -image $s_vol -command "exec [file join $::app_path libexec SndCtrl.tcl ] &"
    pack $r.play  $r.vol -side top -padx 5 -pady 5  

    pack $l $r -side left -padx 5 -pady 5

    frame $w.buttons
    pack $w.buttons -side bottom
    set b $w.buttons
    button $b.sel -text [msgcat::mc "Select"]  \
	-command  "tk_chooseSoundAction  $entryW $l.list"
 
    button $b.cancel -text [msgcat::mc "Cancel"]  \
	-command "set tk_chooseSound_exitFlag 0 "
    pack $b.sel $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
    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 $w.buttons.sel

    tkwait variable tk_chooseSound_exitFlag
    catch {focus $oldFocus}
    grab release $w

    destroy $w
    if {[string compare $oldGrab ""]} {
	if {![string compare $grabStatus "global"]} {
	    grab -global $oldGrab
	} else {
	    grab $oldGrab
	}
    }
    return
}
proc tk_chooseSoundAction  { entryW llist } {
    global soundList tk_chooseSound_exitFlag
    $entryW delete 0 end 
    $entryW insert 0 [lindex $soundList [$llist curselection]] 
    set tk_chooseSound_exitFlag 0 
}
#
proc tk_chooseIcon  { parentW entryW  } {
    global iconList nodeIcon tk_chooseIcon_exitFlag arIcons
    set action ""

    set w $parentW.chooseIcon
    if [winfo exists $w ] {
	raise $w 
	return
    }
    toplevel $w -class Dialog
    wm transient $w [winfo toplevel [winfo parent $w]]

    frame $w.entry
    pack $w.entry -padx 10 -pady 10

    set l $w.entry.l
    frame $l

    listbox $l.list -width 16 -height 8   -selectmode single\
	-yscrollcommand "$l.scroll set"
    scrollbar $l.scroll -command "$l.list yview"
    eval "$l.list insert 0 $iconList"
    set tmp_icon [ $entryW get ]
    if { [catch { $arIcons($tmp_icon) get -file } ] } {
	set tmp_icon $nodeIcon
    }

    set lind [lsearch -exact $iconList $tmp_icon ]
    if { $lind >0 } {
	$l.list selection set $lind
	$l.list see $lind
    } else {
	$l.list selection set 0
	$l.list see 0
    }
    bind $l.list <ButtonRelease-1> {
	global iconList arIcons
	set lind  [%W curselection]
	set parF [winfo parent [winfo parent %W]]
	set i [lindex $iconList $lind ]
	$parF.r.view configure -image $arIcons($i)
    }

    pack $l.list $l.scroll  -side left -fill y -expand 1

    set r $w.entry.r
    frame $r


    label $r.view -image $arIcons($tmp_icon) -borderwidth 2 -relief raised
    pack $r.view -padx 5 -pady 5  

    pack $l $r -side left -padx 5 -pady 5

    frame $w.buttons
    pack $w.buttons -side bottom
    set b $w.buttons
    button $b.sel -text [msgcat::mc "Select"]  \
	-command  "tk_chooseIconAction  $entryW $l.list"
 
    button $b.cancel -text [msgcat::mc "Cancel"]  \
	-command "set tk_chooseIcon_exitFlag 0 "
    pack $b.sel $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
    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 $w.buttons.sel

    tkwait variable tk_chooseIcon_exitFlag
    catch {focus $oldFocus}
    grab release $w

    destroy $w
    if {[string compare $oldGrab ""]} {
	if {![string compare $grabStatus "global"]} {
	    grab -global $oldGrab
	} else {
	    grab $oldGrab
	}
    }
    return
}
#
proc tk_chooseIconAction  { entryW llist } {
    global iconList tk_chooseIcon_exitFlag
    $entryW delete 0 end 
    $entryW insert 0 [lindex $iconList [$llist curselection]] 
    set tk_chooseIcon_exitFlag 0 
}
#
proc browseDir { edir title} {
    set selected [ ::tk::dialog::file::chooseDir:: \
	-title $title -initialdir [$edir get] -parent $edir ]
    if {$selected !=""} {
	$edir delete 0 end
	$edir insert 0 $selected
    }	
}
#
proc browseFile { efile title} {
    set selected [ tk_getOpenFile -title $title]
    if {$selected !=""} {
	$efile delete 0 end
	$efile insert 0 $selected
    }	
}
#
proc tk_chooseOption  { parentW entryW lst } {

    set w $parentW.chooseOption
    set l $w.entry
    set b $w.buttons

    if { [winfo exists $w] } {
	if { [string equal [wm state $w] withdrawn ] } {
	    wm deiconify $w
	} 
	raise $w    
    } else {
    	toplevel $w -class Dialog
    	wm transient $w [winfo toplevel [winfo parent $w]]

    	frame $l
    	pack $l -side top -padx 10 -pady 10 -fill y -expand on
	
    	listbox $l.list -width 24 -height 12   -selectmode single \
		-yscrollcommand "$l.scroll set"
    	scrollbar $l.scroll -command "$l.list yview"
    	pack $l.list $l.scroll  -side left -fill y -expand 1
    	pack $l -side top -padx 5 -pady 5 -fill y -expand 1

    	frame $b
    	pack $b -side bottom 
    	button $b.sel    -text [msgcat::mc "Select"]  -command "set ::tk_chooseOption_exitFlag 1"
    	button $b.cancel -text [msgcat::mc "Cancel"]  -command "set ::tk_chooseOption_exitFlag 2"
    	pack $b.sel $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
    	# wm deiconify $w
    }	
    #
    # Load data
    #
    upvar $lst xlst
    foreach i $xlst { $l.list insert end $i }		
    
    set ::tk_chooseOption_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 $w.buttons.sel

    tkwait variable ::tk_chooseOption_exitFlag
    
    if {[winfo exists $w] } {
	wm withdraw $w
	catch {focus $oldFocus}
	grab release $w 
	if { $::tk_chooseOption_exitFlag == 1 } {
	    set idx [ $l.list curselection ]
	    if { $idx !="" } {
		set opt [$l.list get $idx ]
		if { [string match [$entryW cget -state ] disabled ]} {
		    $entryW configure -state normal
		    $entryW delete 0 end
		    $entryW insert 0 $opt
		    $entryW configure -state disabled		    
		} else {
		    $entryW delete 0 end
		    $entryW insert 0 $opt
		}
	    }
	}
	$l.list delete 0 end
    }
    if {[string compare $oldGrab ""]} {
	if {![string compare $grabStatus "global"]} {
	    grab -global $oldGrab
	} else {
	    grab $oldGrab
	}
    }
    return
}
#
proc tk_chooseFont  { entryW } {
    set old_fnt [ $entryW get ]
    if { [IsValidFont $old_fnt ] } {
	set fdescr [ ChooseFont $old_fnt ]
    } else {
	set fdescr [ ChooseFont "" ]
    }
    if { [string equal [$entryW cget -state ] disabled ] } {
	$entryW configure -state normal
	$entryW delete 0 end
	$entryW insert end $fdescr	 
	$entryW configure -state disabled
    }  else {
	$entryW delete 0 end
	$entryW insert end $fdescr	 
    }
}
#
proc tk_chooseColor2 { entryW } {
    set old_clr [ $entryW get ]
    if { [IsValidColor $old_clr] } {
	set cdescr [ tk_chooseColor -initialcolor $old_clr]
    } else {
	set cdescr [ tk_chooseColor ]
    }
    if { $cdescr == "" } { return }
    if { [string equal [$entryW cget -state ] disabled ] } {
	$entryW configure -state normal
	$entryW delete 0 end
	$entryW insert end $cdescr	 
	$entryW configure -state disabled
    }  else {
	$entryW delete 0 end
	$entryW insert end $cdescr	 
    }    
}
#
namespace eval showErrorLog {
variable showErrorLog_exitFlag	0 \
	background	black	  \
	foreground	white     \
	width		80 	  \
	height          20 	  \
	lastGeometry	""
}
#
proc showErrorLog_GUIconstructor {  }  {
    set w .showErrorLog
    set fl $w.f
    set fx $w.b

    toplevel $w -class Dialog
    wm title $w [ msgcat::mc "Error Log"]
    wm withdraw $w 
    
    frame $fl	-relief raised
    pack $fl -padx 1 -pady 1 -side top -expand on -fill both
    
    grid rowconfig    $fl 0 -weight 1 -minsize 0
    grid columnconfig $fl 0 -weight 1 -minsize 0
    
    scrollbar $fl.hs -orient horiz -command "$fl.txt xview"	
    scrollbar $fl.vs  -command "$fl.txt yview"	
    
    text $fl.txt -state disabled \
	-width $::showErrorLog::width -height $::showErrorLog::height \
	-background $::showErrorLog::background -foreground $::showErrorLog::foreground \
	-xscrollcommand "$fl.hs set" \
	-yscrollcommand "$fl.vs set" 
    
    grid $fl.txt -in $fl \
	-row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news
    grid $fl.hs -in $fl \
	-row 1 -column 0 -rowspan 1 -columnspan 1 -sticky news
    grid $fl.vs -in $fl \
	-row 0 -column 1 -rowspan 1 -columnspan 1 -sticky news
    
    frame $fx -relief raised
    pack $fx -padx 2 -pady 2  -anchor e
    button $fx.done -text [msgcat::mc "Done"]  -command "wm withdraw $w"
    pack $fx.done -padx 5 -pady 2 -side left
    update idletasks
    if { $::showErrorLog::lastGeometry=="" } {
	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]]}]
	set wd [winfo reqwidth  $w ]
	set h  [winfo reqheight $w ]
	wm geom $w [format "%sx%s+%s+%s" $wd $h $x $y]
    } else {
	wm geom $w $::showErrorLog::lastGeometry
    }
    bind $w <Escape>  "wm withdraw $w"  
    bind $w <Destroy> {
	set ::showErrorLog::lastGeometry [ wm geometry .showErrorLog ]
    }
}
#
proc showErrorLog { start stop }  {
    set w .showErrorLog
    set fl $w.f
    set fx $w.b
    if { ![winfo exists $w] } {
	showErrorLog_GUIconstructor
    }
    $fl.txt configure -state normal
    $fl.txt delete 0.0 end
    
    if { [ catch { open $::myLogFile "r" } fd ] } {
	$fl.txt insert 0.0 $fd 	    
    } else {
        seek $fd $start
	$fl.txt insert 0.0 [ read $fd [expr $stop - $start ] ] 
	close $fd
    }    
    
    $fl.txt configure -state disabled
    if { [string equal [wm state $w] withdrawn ] } {
        wm deiconify $w
    }
    raise $w
    focus $fx.done
    return
}
#
lappend ::GUI_pregenerate_list [list { showErrorLog_GUIconstructor } {Loading ErrorLog dialog...}]
#
