#************************************************************
#
# symdlg.tcl --
#
#    Dialog box to change the symbols for line, point,
#    and load-line types.
#
# Created: 12/15/98
# Author: Dan Gunter
#
#************************************************************

#############################################################
#
# ShowSymDlg --
#
#     Show the dialog box which allows the user to
#     choose the symbol (or "none") which will be used
#     for each point on the graph.
#
#     The display consists of one little graph widget for
#     each element in the list $param(SYM,types), from
#     which the user selects the appropriate symbol.
#
#     The standard OK or Cancel buttons are at the bottom
#     of the dialog box.
#
#
proc ShowSymDlg { } {
    global param widget symtmp

    set types $param(SYM,types)

    if { [info exists param(_symdlg)] } {
	wm iconify $param(_symdlg)
	wm deiconify $param(_symdlg)
	return
    } else {
	set param(_symdlg) [toplevel .symdlg]
	wm title .symdlg "Graph Symbols"
	foreach i $types {
	    set symtmp($i) $param(SYM,$i)
	}
    }
    set bopts [list -background $param(color-buttonbg) \
		   -foreground $param(color-buttonfg) \
		   -activebackground $param(color-buttonabg) \
		   -highlightbackground $param(color-ctlbg)]
    
    set b $param(_symdlg)
    # Some instructions
    set f [frame $b.instructions -background $param(color-winbg)]
    label $f.txt -text "Click with mouse-button 1 on the desired symbol." \
	-bg $param(color-menubg) \
	-fg \#000000
    pack $f.txt $f -side top -fill x -expand 1
    # OK/Cancel buttons
    set f [frame $b.decision -background $param(color-winbg)]
    eval {button $f.ok -text "OK" -command CommitSymDlg} $bopts
    eval {button $f.cancel -text "Cancel" -command CancelSymDlg} $bopts
    pack $f.ok $f.cancel -side left -expand 1
    pack $f -side bottom -fill x
    # Symbol display
    foreach i $types {
	set f [frame $b.$i -background $param(color-winbg)]
	label $f.label -text $i -background $param(color-winbg)
	set g [ blt::graph $f.graph \
		    -plotbackground $param(color-graphbg) \
		    -title "" \
		    -background $param(color-winbg) \
		    -rightmargin 0 \
		    -leftmargin 0 \
		    -bottommargin 0 \
		    -topmargin 0 \
		    -width 2i \
		    -height 2i
	       ]
	$g yaxis configure \
	    -min 0 \
	    -max 2 \
	    -stepsize 1 \
	    -subdivisions 0 \
	    -command SymLabel
	$g xaxis configure -hide yes
	$g grid configure -hide yes
	SymPlace $g [list [SymLabel zz 0] [SymLabel zz 1] [SymLabel zz 2]]
	SymHighlight $g $param(SYM,$i)
	pack $g -side bottom
	pack $f.label -side top -fill x -expand 1
	pack $f -side left
	# Graph bindings
	bind $g <Button-1> {
	    global symtmp
	    
	    set coords [%W invtransform %x %y]
	    set y [expr int(floor([lindex $coords 1] +0.5))]
	    set sym [SymLabel zz $y]
	    regexp symdlg\.(.*)\.graph %W zz ty
	    # Turn off previously highlighted element
	    SymHighlight %W $symtmp($ty) no
	    set symtmp($ty) $sym
	    # Highlight new element
	    SymHighlight %W $sym
	}
    }
}

#############################################################
#
# SymPlace --
#
#     Place the given list of symbols in the given graph,
#     in ascending Y order from 0..(#symbols-1)
#
proc SymPlace {graphname symlist} {
    set ctr 0
    foreach sym $symlist {
	if { $sym == "none" } { set sym "diamond" }
	$graphname element create e$ctr \
	    -data [list 0 $ctr] \
	    -symbol $sym \
	    -linewidth 1 \
	    -label ""
	# Set to default color
	SymHighlight $graphname $sym off
	incr ctr
    }
    return
}
#############################################################
#
# SymHighlight --
#
#      Turn on/off highlighting for a given symbol in a
#      given graph.
#
proc SymHighlight {graphname symname {highlight on}} {
    set num [SymNumber $symname]
    if { $highlight } {
	$graphname element configure e$num \
	    -color \#ff0000
    } else {
	$graphname element configure e$num \
	    -color \#000000
    }
    return
}

#############################################################
#
# SymLabel --
#
#     Return a label for a numbered tick. This is given
#     to the graph as the procedure to use to label the
#     Y-axis.
#
proc SymLabel {pathname tick} {
    switch $tick {
	0 { return none }
	1 { return cross }
	2 { return circle }
    }
    return unknown
}
#############################################################
#
# SymStringFromTypenum --
#
#     Given an integer type number, discover the current
#     symbol which is used for that element type.
#
proc SymStringFromTypenum {typenum} {
    global param
    set typename [lindex $param(SYM,types) $typenum]
    return $param(SYM,$typename)
}

#############################################################
#
# SymNumber --
#
#     Given a symbol, return the Y-axis number for it.
#     This is the inverse of "SymLabel".
#
proc SymNumber {label} {
    switch $label {
	none {return 0}
	diamond {return 0}
	cross {return 1}
	circle {return 2}
    }
    puts "Warning: Unrecognized symbol $label"
    return -1
}
#############################################################
#
# CommitSymDlg --
#
#    When the user clicks on OK, we then copy the values
#    in the array symtmp(..) to param(SYM,..). For each
#    changed value, we call the procedure "ChangeSymbols"
#    to run through and modify all the on-screen elements.
#
proc CommitSymDlg { } {
    global param symtmp

    foreach i $param(SYM,types) {
	if { $param(SYM,$i) != $symtmp($i) } {
	    set param(SYM,$i) $symtmp($i)
	    ChangeSymbols $i $param(SYM,$i)
	}
    }
    destroy $param(_symdlg)
    unset param(_symdlg)
}
#############################################################
#
# CancelSymDlg --
#
#    Do NOT copy the temporary values to the permanent
#    values; just destroy the widget and return.
#
proc CancelSymDlg { } {
    global param

    destroy $param(_symdlg)
    unset param(_symdlg)
}
#############################################################
#
# ChangeSymbols --
#
#    This is a placeholder for a procedure that will
#    be written in C.
#
proc ChangeSymbols { which what } {
    global param

    toplevel .changing
    wm title .changing "Changing Symbols"
    set b .changing
    set f [frame $b.f1 -bg $param(color-winbg)]
    label $f.label -text "I am now changing the symbols for\ngraph element type '$which' to: $what\nPlease wait..." \
	-bg $param(color-winbg) \
	-justify left
    pack $f.label $f -side top -fill both -expand 1
    update idletasks
    set whichnum [lsearch $param(SYM,types) $which]
    SetGraphSymbols $whichnum $what
    destroy .changing
}
