#!/usr/local/bin/wish -f

# set system dependent variables
set local_path "/home/neural01/gradst/claypool/proj/tkgnuplot"
set bitdir "/home/neural01/gradst/claypool/proj/tkgnuplot/bitmaps"
set auto_path "$local_path $auto_path"

# initialize all variables
Init
wm title . "tkgnuplot $version"

# start TIX stuff
set TIX_LIBRARY "/project/nero/lib/TixLib"
lappend auto_path $TIX_LIBRARY
tixInit -libdir $TIX_LIBRARY -scheme $color_scheme -fontset 12Point -binding Motif

# open gnuplot process
set gnuplot [open {|gnuplot } w ]

# title frame on top of main window, pack words at end to center
#frame .title -bg Grey
#pack .title -side top 
#label .title.lbl -text "TkGnuplot" -font "12x24" -relief ridge -bd 5
#pack .title.lbl -side left

####################################################################
## top level system frame and buttons

frame .system -relief raised -borderwidth 4
pack .system -side top -fill x

# file
menubutton .system.file -text File -underline 0 -menu .system.file.menu
pack .system.file -side left
menu .system.file.menu
.system.file.menu add command -label "Load TkGnuplot Configuration" \
	-command LoadTkGp
.system.file.menu add command -label "Save TkGnuplot Configuration" \
	-command SaveTkGp
.system.file.menu add command -label "Reset TkGnuplot Configuration" \
	-command reset
.system.file.menu add separator
.system.file.menu add command -label "Save Gnuplot Configuration" \
	-command SaveGp
.system.file.menu add separator
.system.file.menu add command -label "Quit TkGnuplot" -command exit \
	-accelerator "Ctrl-Q"

# help
menubutton .system.help -text Help -underline 0 -menu .system.help.menu
pack .system.help -side right
menu .system.help.menu
.system.help.menu add command -label "About TkGnuplot" \
    -command "help .helpwin0 about_text"
.system.help.menu add command -label "Getting Started" \
    -command "help .helpwin1 start_text"
.system.help.menu add command -label "Add or Remove Plots" \
    -command "help .helpwin2 add_or_remove"
.system.help.menu add command -label "Change Settings" \
    -command "help .helpwin3 change_settings"
.system.help.menu add command -label "Output Types" \
    -command "help .helpwin4 output_type"
.system.help.menu add command -label "Save and Load Settings" \
    -command "help .helpwin5 save_and_load"
.system.help.menu add command -label "Version Changes" \
    -command "help .helpwin6 changes"

# preferences
menubutton .system.preferences -text Preferences -underline 0 \
	-menu .system.preferences.menu
pack .system.preferences -side right
menu .system.preferences.menu
.system.preferences.menu add checkbutton -label "Balloon Help" \
	-variable balloon_help  -command {
    if {$balloon_help == 0} {
	tixDisableBalloon
	set message "Balloon Help Off"
    } else {
	tixEnableBalloon
	set message "Balloon Help On"
    }
}
.system.preferences.menu add separator
.system.preferences.menu add radiobutton -label "Gray Color Scheme" \
	-variable color_scheme -value Gray -command {
    set message "Color scheme set to Gray. Save preferences to enable change for next session."
}
.system.preferences.menu add radiobutton -label "Blue Color Scheme" \
	-variable color_scheme -value Blue -command {
    set message "Color scheme set to Blue. Save preferences to enable change for next session."
}
.system.preferences.menu add separator
.system.preferences.menu add command -label "Save Preferences" -command {
   set fp [open  "~/.tkgnuplotrc" w]

   # balloon help
   if {$balloon_help == 1} {
      puts $fp "BalloonHelp yes"
   } else {
      puts $fp "BalloonHelp no"
   }

   # color
   puts $fp "ColorScheme $color_scheme"

   close $fp

   set message "Preferences saved in \"~/.tkgnuplotrc\""
}


####################################################################
# message frame
set message_frame [tixLabelFrame .message -label "Messages"]
pack .message -side bottom -fill x
set message_box $message_frame.box
tixCombobox $message_box -type static \
	-height 5 -fancy no -scrollbar both -histlimit 30 -foreground Blue
pack $message_box -fill both -expand yes -ipady 1m -ipadx 2m

####################################################################
# plot button frame and button
frame .p -relief flat -borderwidth 4
pack .p -fill both -side bottom
button .p.doit -text "Plot" -font -Adobe-Times-Medium-R-Normal-*-180-* \
   -command {
   CallGnuplot plot_list $plot_count
}
pack .p.doit -ipadx 1c -ipady 1m -side bottom -fill none

####################################################################
## settings frame
set settings [tixLabelFrame .settings -label "Plot Settings"]
pack .settings -side bottom

# key
frame $settings.key
label $settings.key.lbl -text "\  key:\ "
button $settings.key.scale -textvariable keysty -command {\
	if {$keysty == "auto place"} {
	   set keysty "manual place"
           set message "Manual place key. Enter X and Y location."
        } else {
	   set keysty "auto place"
           set message "Auto place key. Press PLOT to change plot."
        }
    }
label $settings.key.lbl2 -text "\ key x:"
entry $settings.key.x -font 8x13 -bg white -width 10 -relief sunken\
      -textvariable keyx
label $settings.key.lbl3 -text "\ key y:"
entry $settings.key.y -font 8x13 -bg white -width 10 -relief sunken \
      -textvariable keyy
pack $settings.key -side bottom -fill x -pady 1m
pack $settings.key.lbl -side left
pack $settings.key.scale -side left
pack $settings.key.y -side right
pack $settings.key.lbl3 -side right
pack $settings.key.x -side right
pack $settings.key.lbl2 -side right

frame $settings.left  
frame $settings.right -bd 1 -width 1i

frame $settings.titleframe 
label $settings.title -text " title:"
entry $settings.titleentry -font 8x13 -bg white -relief sunken \
      -textvariable title

frame $settings.xlabelframe
label $settings.xlabel -text "xlabel:"
entry $settings.xlabelentry -font 8x13 -bg white -relief sunken \
      -textvariable xlabel

frame $settings.ylabelframe
label $settings.ylabel -text "ylabel:"
entry $settings.ylabelentry -font 8x13 -bg white -relief sunken \
      -textvariable ylabel

frame $settings.xrangeframe
label $settings.xrange1 -text "xrange:\ \ \ from"
entry $settings.xrangefrom -font 8x13 -bg white -width 10 -relief sunken\
      -textvariable xrangefrom
label $settings.xrange2 -text "to"
entry $settings.xrangeto -font 8x13 -bg white -width 10 -relief sunken \
      -textvariable xrangeto

frame $settings.yrangeframe
label $settings.yrange1 -text "yrange:\ \ \ from"
entry $settings.yrangefrom -font 8x13 -bg white -width 10 -relief sunken\
      -textvariable yrangefrom
label $settings.yrange2 -text "to"
entry $settings.yrangeto -font 8x13 -bg white -width 10 -relief sunken \
      -textvariable yrangeto

frame $settings.right.gridframe
button $settings.grid -textvariable gridsty -command {\
	if {$gridsty == "grid on"} {
	   set gridsty "grid off"
           set message "Grid off. Press PLOT to change plot."
        } else {
	   set gridsty "grid on"
           set message "Grid on. Press PLOT to change plot."
        }
    }

frame $settings.right.scaleframe
button $settings.scale -textvariable scalesty -command {\
	if {$scalesty == "auto scale"} {
	   set scalesty "manual scale"
           set message "Manual scale. Enter X and Y range."
        } else {
	   set scalesty "auto scale"
           set message "Auto scale. Press PLOT to change plot."
        }
    }


frame $settings.action -bg linen -relief ridge -bd 1

pack $settings.action  -side bottom -fill x

pack $settings.left $settings.right -side left -fill both

pack $settings.titleframe  -in $settings.left -side top -fill x -pady 1m
pack $settings.title  -in $settings.titleframe -side left -padx 2.5m 
pack $settings.titleentry  -in $settings.titleframe -side left -fill x \
     -expand 1 -padx 1m

pack $settings.xlabelframe  -in $settings.left -side top -fill x  -pady 1m
pack $settings.xlabel -in $settings.xlabelframe -side left -padx 1m 
pack $settings.xlabelentry -in $settings.xlabelframe -side left -fill x \
      -expand 1 -padx 1m 

pack $settings.ylabelframe  -in $settings.left -side top -fill x -pady 1m 
pack $settings.ylabel  -in $settings.ylabelframe -side left -padx 1m 
pack $settings.ylabelentry -in $settings.ylabelframe -side left -fill x \
      -expand 1 -padx 1m

pack $settings.xrangeframe  -in $settings.left -side top -fill x  -pady 1m
pack $settings.xrange1 $settings.xrangefrom $settings.xrange2 $settings.xrangeto -side left\
     -in $settings.xrangeframe -padx 1m  

pack $settings.yrangeframe  -in $settings.left -side top -fill x -pady 1m
pack $settings.yrange1 $settings.yrangefrom $settings.yrange2 $settings.yrangeto -side left\
     -in $settings.yrangeframe -padx 1m 

pack $settings.grid -in $settings.right -side top -fill x -expand yes

pack $settings.scale -in $settings.right -side top -fill x 

# logscale xy buttons
button $settings.right.logy -textvariable logscale_y -command {\
	if {$logscale_y == "logy on"} {
	   set logscale_y "logy off"
           set message "Logscale Y off. Press PLOT to change plot scale."
        } else {
           set logscale_y "logy on"
           set message "Logscale Y on. Press PLOT to change plot scale."
        }
}
pack $settings.right.logy -side top -fill x -expand yes
button $settings.right.logx -textvariable logscale_x -command {\
	if {$logscale_x == "logx on"} {
	   set logscale_x "logx off"
           set message "Logscale X off. Press PLOT to change plot scale."
        } else {
           set logscale_x "logx on"
           set message "Logscale X on. Press PLOT to change plot scale."
        }
}
pack $settings.right.logx -side top -fill x -expand yes

# output type
menubutton $settings.right.output -textvariable output_type \
   -relief raised  -menu $settings.right.output.menu 
menu $settings.right.output.menu
$settings.right.output.menu add radiobutton -label "xwindow" \
	-variable output_type -value "xwindow output" -command {
   puts $gnuplot "set terminal x11"
   puts $gnuplot "set output"
}
$settings.right.output.menu add radiobutton -label "postscript" \
	-variable output_type -value "postscript output" -command {
   psout
}
$settings.right.output.menu add radiobutton -label "latex" \
	-variable output_type -value "latex output" -command {
   latex
}
pack $settings.right.output -side top

####################################################################
## plot frame 
frame .plot -width $MainWidth -height 1i -relief flat -borderwidth 4
pack .plot -side top -fill both

# frame for modifying plot list
set mod_list [tixLabelFrame .plot.mod_list -label "Plot List"]
button $mod_list.file     -text "   add file   " -command FileAdd 
button $mod_list.function -text " add function " -command {
   FunctionAdd plot_list plot_count
}
button $mod_list.delete   -text " delete " -command {
   Delete plot_list plot_count
}

# TixCombobox for plot list
tixCombobox $mod_list.list -type static -command Choose \
	-height 5 -fancy no -scrollbar both
pack $mod_list.list -side top -expand yes
pack $mod_list.list -side top -fill x
pack $mod_list.file -side top -fill x
pack $mod_list.function -side top -fill x
pack $mod_list.delete -side top -fill x
pack .plot.mod_list -side left -fill x

# frame for current selected
set current [tixLabelFrame .plot.current -label "Plot Current"]

# key
frame $current.key
pack $current.key -side top -fill x
label $current.key.lbl -text "key name: "
pack $current.key.lbl -side left -fill y
entry $current.key.keyentry -width 10 -relief sunken \
   -textvariable current_selection_key
pack $current.key.keyentry -padx 1m -pady 1m
pack .plot.current -side bottom

# linestyle
frame $current.linestyle
pack  $current.linestyle -side top -fill x
menubutton $current.linestyle.btn -textvariable \
	current_selection_linestyle \
	-relief raised  -menu $current.linestyle.btn.menu 
menu $current.linestyle.btn.menu 
$current.linestyle.btn.menu add radiobutton -bitmap "@$bitdir/lines.xbm" \
   -variable current_selection_linestyle -value lines
$current.linestyle.btn.menu add radiobutton -bitmap \
	"@$bitdir/points.xbm" \
	-variable current_selection_linestyle -value points
$current.linestyle.btn.menu add radiobutton -bitmap \
	"@$bitdir/linespoints.xbm" -variable current_selection_linestyle \
	-value linespoints
$current.linestyle.btn.menu add radiobutton -bitmap \
	"@$bitdir/errorbars.xbm" -variable current_selection_linestyle \
	-value errorbars
$current.linestyle.btn.menu add radiobutton -bitmap \
	"@$bitdir/dots.xbm" -variable current_selection_linestyle \
	-value dots
$current.linestyle.btn.menu add radiobutton -bitmap \
	"@$bitdir/impulses.xbm" \
	-variable current_selection_linestyle -value impulses
$current.linestyle.btn.menu add radiobutton -bitmap "@$bitdir/boxes.xbm" \
	-variable current_selection_linestyle -value boxes
$current.linestyle.btn.menu add radiobutton -bitmap "@$bitdir/steps.xbm" \
	-variable current_selection_linestyle -value steps
label $current.linestyle.ls -text "  linestyle: "
pack $current.linestyle.ls -side left -fill y
pack $current.linestyle.btn -side left -fill x -expand yes

# xy columns (and top and bottom for errorbars)
frame $current.columns
frame $current.columns.xcol
frame $current.columns.ycol
frame $current.columns.top
frame $current.columns.bot
pack $current.columns -side top -fill x
pack $current.columns.xcol -side top -fill x
pack $current.columns.ycol -side top -fill x
pack $current.columns.top -side top -fill x
pack $current.columns.bot -side top -fill x
label $current.columns.xcol.lbl -text " x column: "
label $current.columns.ycol.lbl -text " y column: "
label $current.columns.top.lbl -text " top column: "
label $current.columns.bot.lbl -text " bot column: "
pack $current.columns.xcol.lbl -side left
pack $current.columns.ycol.lbl -side left
pack $current.columns.top.lbl -side left
pack $current.columns.bot.lbl -side left

menubutton $current.columns.xcol.button -textvariable \
   current_selection_xcol -relief raised -menu $current.columns.xcol.button.menu 
menu $current.columns.xcol.button.menu 
$current.columns.xcol.button.menu add radiobutton -label "(none)" \
   -variable current_selection_xcol -value "(none)"
$current.columns.xcol.button.menu add radiobutton -label "1" \
   -variable current_selection_xcol -value 1
$current.columns.xcol.button.menu add radiobutton -label "2" \
   -variable current_selection_xcol -value 2
$current.columns.xcol.button.menu add radiobutton -label "3" \
   -variable current_selection_xcol -value 3
$current.columns.xcol.button.menu add radiobutton -label "4" \
   -variable current_selection_xcol -value 4
$current.columns.xcol.button.menu add radiobutton -label "5" \
   -variable current_selection_xcol -value 5
$current.columns.xcol.button.menu add radiobutton -label "6" \
   -variable current_selection_xcol -value 6
$current.columns.xcol.button.menu add radiobutton -label "7" \
   -variable current_selection_xcol -value 7
$current.columns.xcol.button.menu add radiobutton -label "8" \
   -variable current_selection_xcol -value 8
$current.columns.xcol.button.menu add radiobutton -label "9" \
   -variable current_selection_xcol -value 9
pack $current.columns.xcol.button -side left -fill x -expand yes

menubutton $current.columns.ycol.button -textvariable \
   current_selection_ycol -relief raised -menu $current.columns.ycol.button.menu 
menu $current.columns.ycol.button.menu 
$current.columns.ycol.button.menu add radiobutton -label "1" \
   -variable current_selection_ycol -value 1
$current.columns.ycol.button.menu add radiobutton -label "2" \
   -variable current_selection_ycol -value 2
$current.columns.ycol.button.menu add radiobutton -label "3" \
   -variable current_selection_ycol -value 3
$current.columns.ycol.button.menu add radiobutton -label "4" \
   -variable current_selection_ycol -value 4
$current.columns.ycol.button.menu add radiobutton -label "5" \
   -variable current_selection_ycol -value 5
$current.columns.ycol.button.menu add radiobutton -label "6" \
   -variable current_selection_ycol -value 6
$current.columns.ycol.button.menu add radiobutton -label "7" \
   -variable current_selection_ycol -value 7
$current.columns.ycol.button.menu add radiobutton -label "8" \
   -variable current_selection_ycol -value 8
$current.columns.ycol.button.menu add radiobutton -label "9" \
   -variable current_selection_ycol -value 9
pack $current.columns.ycol.button -side left -fill x -expand yes

menubutton $current.columns.top.button -textvariable \
   current_selection_topcol -relief raised -menu $current.columns.top.button.menu 
menu $current.columns.top.button.menu 
$current.columns.top.button.menu add radiobutton -label "1" \
   -variable current_selection_topcol -value 1
$current.columns.top.button.menu add radiobutton -label "2" \
   -variable current_selection_topcol -value 2
$current.columns.top.button.menu add radiobutton -label "3" \
   -variable current_selection_topcol -value 3
$current.columns.top.button.menu add radiobutton -label "4" \
   -variable current_selection_topcol -value 4
$current.columns.top.button.menu add radiobutton -label "5" \
   -variable current_selection_topcol -value 5
$current.columns.top.button.menu add radiobutton -label "6" \
   -variable current_selection_topcol -value 6
$current.columns.top.button.menu add radiobutton -label "7" \
   -variable current_selection_topcol -value 7
$current.columns.top.button.menu add radiobutton -label "8" \
   -variable current_selection_topcol -value 8
$current.columns.top.button.menu add radiobutton -label "9" \
   -variable current_selection_topcol -value 9
pack $current.columns.top.button -side left -fill x -expand yes

menubutton $current.columns.bot.button -textvariable \
   current_selection_botcol -relief raised -menu $current.columns.bot.button.menu 
menu $current.columns.bot.button.menu 
$current.columns.bot.button.menu add radiobutton -label "1" \
   -variable current_selection_botcol -value 1
$current.columns.bot.button.menu add radiobutton -label "2" \
   -variable current_selection_botcol -value 2
$current.columns.bot.button.menu add radiobutton -label "3" \
   -variable current_selection_botcol -value 3
$current.columns.bot.button.menu add radiobutton -label "4" \
   -variable current_selection_botcol -value 4
$current.columns.bot.button.menu add radiobutton -label "5" \
   -variable current_selection_botcol -value 5
$current.columns.bot.button.menu add radiobutton -label "6" \
   -variable current_selection_botcol -value 6
$current.columns.bot.button.menu add radiobutton -label "7" \
   -variable current_selection_botcol -value 7
$current.columns.bot.button.menu add radiobutton -label "8" \
   -variable current_selection_botcol -value 8
$current.columns.bot.button.menu add radiobutton -label "9" \
   -variable current_selection_botcol -value 9
pack $current.columns.bot.button -side left -fill x -expand yes

####################################################################
# set up the trace to modify the current seletions when needed
####################################################################
trace variable current_selection w ModifiedCurrentSelection
trace variable current_selection_linestyle w ModifiedCurrentSelectionLinestyle
trace variable current_selection_key w ModifiedCurrentSelectionKey
trace variable current_selection_xcol w ModifiedCurrentSelectionXcol
trace variable current_selection_ycol w ModifiedCurrentSelectionYcol
trace variable current_selection_topcol w ModifiedCurrentSelectionTopcol
trace variable current_selection_botcol w ModifiedCurrentSelectionBotcol
trace variable message w ModifiedMessage
trace variable scalesty w DisableRange
trace variable keysty w KeyButtons

####################################################################
## Key Bindings
####################################################################
# Set up bindings for the tkgnuplot
bind . <Control-c> {destroy .}

# settings
bind $settings.key.x <Return> "focus $settings.key.y"
bind $settings.key.y <Return> "focus $settings.titleentry"
bind $settings.titleentry <Return> "focus $settings.xlabelentry"
bind $settings.xlabelentry <Return> "focus $settings.ylabelentry"
bind $settings.ylabelentry <Return> "focus $settings.xrangefrom"
bind $settings.xrangefrom <Return> "focus $settings.xrangeto"
bind $settings.xrangeto <Return> "focus $settings.yrangefrom"
bind $settings.yrangefrom <Return> "focus $settings.yrangeto"
bind $settings.yrangeto <Return> "focus $settings.key.x"

# bind changing key to hitting enter in key name window to avoid errors
bind $current.key.keyentry <Return> {
   set plot_list($current_selection.key) $current_selection_key 
   set message "Changed key name to $current_selection_key"
   focus none
}

# Menu hot-keys
bind . <Control-l> {
   load
}

# Menu hot-keys
bind . <Control-q> {
   exit
}

####################################################################
## Procedure definitions
####################################################################
####################################################################
##  ModifiedMessage
proc ModifiedMessage {name element op} {
   global message
   global message_box

   $message_box appendhistory $message
   $message_box pick $message
}

####################################################################
##  ModifiedCurrentSelectionXcol
proc ModifiedCurrentSelectionXcol {name element op} {
   global plot_list
   global current_selection
   global current_selection_xcol

   #puts stdout "ModifiedCurrentSelectionXcol():"
   if {$current_selection != -1} {
      if {$plot_list($current_selection.xcol) != $current_selection_xcol} {
         set plot_list($current_selection.xcol) $current_selection_xcol
      }
   }
}

####################################################################
##  ModifiedCurrentSelectionTopcol
proc ModifiedCurrentSelectionTopcol {name element op} {
   global plot_list
   global current_selection
   global current_selection_topcol

   #puts stdout "ModifiedCurrentSelectionTopcol():"
   if {$current_selection != -1} {
      if {$plot_list($current_selection.topcol) != $current_selection_topcol} {
         set plot_list($current_selection.topcol) $current_selection_topcol
      }
   }
}

####################################################################
##  ModifiedCurrentSelectionBotcol
proc ModifiedCurrentSelectionBotcol {name element op} {
   global plot_list
   global current_selection
   global current_selection_botcol

   #puts stdout "ModifiedCurrentSelectionBotcol():"
   if {$current_selection != -1} {
      if {$plot_list($current_selection.botcol) != $current_selection_botcol} {
         set plot_list($current_selection.botcol) $current_selection_botcol
      }
   }
}

####################################################################
##  ModifiedCurrentSelectionYcol
proc ModifiedCurrentSelectionYcol {name element op} {
   global plot_list
   global current_selection
   global current_selection_ycol

   #puts stdout "ModifiedCurrentSelectionYcol():"
   if {$current_selection != -1} {
      if {$plot_list($current_selection.ycol) != $current_selection_ycol} {
         set plot_list($current_selection.ycol) $current_selection_ycol
      }
   }
}

####################################################################
##  ModifiedCurrentSelectionKey
proc ModifiedCurrentSelectionKey {name element op} {
   global plot_list
   global current_selection
   global current_selection_key

   #puts stdout "ModifiedCurrentSelectionKey():"
   if {$current_selection != -1} {
      if {$plot_list($current_selection.key) != $current_selection_key} {
         set plot_list($current_selection.key) $current_selection_key
      }
   }
}

####################################################################
##  ModifiedCurrentSelectionLinestyle
proc ModifiedCurrentSelectionLinestyle {name element op} {
   global plot_list
   global current_selection
   global current_selection_linestyle
   global current_selection_topcol
   global current_selection_botcol

   #puts stdout "ModifiedCurrentSelectionLinestyle():"
   if {$current_selection != -1} {
      if {$plot_list($current_selection.linestyle) !=\
          $current_selection_linestyle} {
         set plot_list($current_selection.linestyle) \
            $current_selection_linestyle
         set message "Changed linestyle to $current_selection_linestyle"
      }
      if {$current_selection_linestyle == "errorbars"} {
         if {$plot_list($current_selection.topcol) == ""} {
            set current_selection_topcol 3
            set current_selection_botcol 4
         }
      }
   }
   DisableButtons
}

####################################################################
##  ModifiedCurrentSelection
# if the current_selection variable is modified, change the buttons
proc ModifiedCurrentSelection {name element op} {
   global plot_list
   global current_selection
   global current_selection_name
   global current_selection_key
   global current_selection_linestyle
   global current_selection_xcol
   global current_selection_ycol
   global current_selection_topcol
   global current_selection_botcol

   #puts stdout "ModifiedCurrentSelection():"
   if {$current_selection == -1} {
      set current_selection_name ""
      set current_selection_key ""
      set current_selection_linestyle ""
      set current_selection_xcol ""
      set current_selection_ycol ""
      set current_selection_topcol ""
      set current_selection_botcol ""
   } else { 
      set current_selection_name $plot_list($current_selection)
      set current_selection_key $plot_list($current_selection.key)
      set current_selection_linestyle $plot_list($current_selection.linestyle)
      set current_selection_xcol $plot_list($current_selection.xcol)
      set current_selection_ycol $plot_list($current_selection.ycol)
      set current_selection_topcol $plot_list($current_selection.topcol)
      set current_selection_botcol $plot_list($current_selection.botcol)
   }

   # enable/disable the columns as needed
   DisableButtons
}

####################################################################
## Choose
## Choose the new item from the combobox
proc Choose {string} \
{
   global plot_list
   global plot_count
   global current_selection

   set current_selection [Locate plot_list $plot_count $string]
}


####################################################################
## Delete
## remove the selected file/function
proc Delete {sys_plot_list sys_count} {
   upvar $sys_plot_list plot_list
   upvar $sys_count plot_count
   global current_selection
   global message
   global mod_list

   if {$current_selection == -1} {
      dialog .d {No Plot List} {The Plot List is Empty.} error 0 {Ok} 
      return
   }

   #puts stdout "Delete():"
   #puts stdout "Before"
   #PrintList plot_list $plot_count

   set item $plot_list($current_selection)
   set spot [Locate plot_list $plot_count $item]
   #puts stdout "located at $spot"
   for {set j $spot} {$j < [expr $plot_count - 1]} {incr j} {
      set succ [expr $j + 1]
      set plot_list($j) $plot_list($succ)
      set plot_list($j.type) $plot_list($succ.type)
      set plot_list($j.key) $plot_list($succ.key) 
      set plot_list($j.path) $plot_list($succ.path)
      set plot_list($j.linestyle) $plot_list($succ.linestyle)
      set plot_list($j.xcol) $plot_list($succ.xcol)
      set plot_list($j.ycol) $plot_list($succ.ycol)
   } 

   unset plot_list($j)
   unset plot_list($j.type)
   unset plot_list($j.key)
   unset plot_list($j.linestyle)
   unset plot_list($j.path)
   unset plot_list($j.xcol)
   unset plot_list($j.ycol)
   incr plot_count -1

   # puts stdout "After"
   # PrintList plot_list $plot_count
   $mod_list.list entry delete 0 10
   $mod_list.list listbox delete $spot
   set current_selection [expr $spot - 1]
   #puts stdout "plot_count is $plot_count"
   #puts stdout "current_seleciton is $current_selection"
   if {$current_selection == -1} {
      if {$plot_count > 0} {
         set current_selection 0
         $mod_list.list pick $plot_list($current_selection)
      } else {
         $mod_list.list pick ""
      }
   } else {
      $mod_list.list pick $plot_list($current_selection)
   }       

   set message "Deleted \"$item\" from plot."
}

####################################################################
# check for spaces
proc IsSpaces {item} {
   # if it has spaces in it, flag it as an error and return 1
   set i 0
   while {$i < [string length $item]} {
      if {[string index $item $i] == " "} {
         return 1
      }
      incr i
   }
   return 0
}

####################################################################
proc Locate {sys_plot_list plot_count item} {
   set i 0
   upvar $sys_plot_list plot_list

   #puts stdout "Locate(): item is eval $item"

   while {$plot_list($i) != $item} {
      incr i
   }
   return $i  
}

####################################################################
proc PrintList {sys_plot_list plot_count} {
   set i 0
   upvar $sys_plot_list plot_list

   #puts stdout "PrintList(): plot_count is  $plot_count"
   while {$i < $plot_count} {
      #puts stdout "$plot_list($i), $plot_list($i.type), $plot_list($i.key) $plot_list($i.linestyle)"
      set i [expr $i + 1]
   }
}

####################################################################
# Make a dialog box with the indicated buttons
proc dialog {w title text bitmap default args} {
   global button
   
   # 1. create the top-level window and divide it into top
   # and bottom parts.

   toplevel $w -class Dialog
   wm title $w $title
   wm iconname $w Dialog
   frame $w.top -relief raised -bd 1
   pack $w.top -side top -fill both
   frame $w.bot -relief raised -bd 1
   pack $w.bot -side bottom -fill both
 

   # 2. Fill the top part with the bitmap and message.
   message $w.top.msg -width 3i -text $text \
      -font -Adobe-Times-Medium-R-Normal-*-180-*
   pack $w.top.msg -side right -expand 1 -fill both \
      -padx 5m -pady 5m
   if {$bitmap != ""} {
      label $w.top.bitmap -bitmap $bitmap
      pack $w.top.bitmap -side left -padx 5m -pady 5m
   }

   # 3. Create a row of buttons at the bottom of the dialog.
   set i 0
   foreach but $args {
      button $w.bot.button$i -text $but -command \
         "set button $i"
      if {$i == $default} {
         frame $w.bot.default -relief sunken -bd 1
         raise $w.bot.button$i
         pack $w.bot.default -side left -expand 1\
            -padx 5m -pady 2m
         pack $w.bot.button$i -in $w.bot.default \
            -side left -padx 3m -pady 3m\
            -padx 5m -ipady 1m -ipadx 5m
      } else {
         pack $w.bot.button$i -side left -expand 1\
            -padx 5m -pady 5m -ipadx 2m -ipady 5m
      }
      incr i
   }

   # 4. Set up a binding for <return>, if ther is a default,
   # set a grab, and claim the focus too.

   if {$default >= 0} {
      bind $w <Return> "$w.bot.button$default flash; \
         set button $default"
   }
   set oldFocus [focus]
   grab set $w
   focus $w

   # 5. wait for the user to respond, then restore the focus
   # and return the index of the selected butoon.

   tkwait variable button
   destroy $w
   focus $oldFocus
   return $button
}
  

###################################################################
# disable range for autoscale
proc DisableRange {name element op} { 
   global scalesty
   global settings

   if {$scalesty == "auto scale"} then { 
      $settings.xrangeto configure -state disabled -relief flat -bg Gray
      $settings.xrangefrom configure -state disabled -relief flat -bg Gray
      $settings.yrangefrom configure -state disabled -relief flat -bg Gray
      $settings.yrangeto configure -state disabled -relief flat -bg Gray
   } else {
      $settings.xrangeto configure -state normal -relief sunken -bg white
      $settings.xrangefrom configure -state normal -relief sunken -bg white
      $settings.yrangefrom configure -state normal -relief sunken -bg white
      $settings.yrangeto configure -state normal -relief sunken -bg white
   }
}

###################################################################
# disable range for auto place of key, enable for manual place
proc KeyButtons {name element op} { 
    global keysty
    global settings

    if {$keysty == "auto place"} {
      $settings.key.x configure -state disabled -relief flat -bg Gray
      $settings.key.y configure -state disabled -relief flat -bg Gray
    } else {
      $settings.key.x configure -state normal -relief sunken -bg white
      $settings.key.y configure -state normal -relief sunken -bg white
    }
}

###################################################################
# disable range for autoscale
proc DisableButtons {} { 
   global current_selection
   global plot_list
   global current

   if {$current_selection == -1} {
      # disable all 
      $current.columns.top.button configure -state disabled -relief flat
      $current.columns.bot.button configure -state disabled -relief flat
      $current.columns.xcol.button configure -state disabled -relief flat
      $current.columns.ycol.button configure -state disabled -relief flat
      $current.key.keyentry configure -state disabled -relief flat
      $current.linestyle.btn configure -state disabled -relief flat
      return
   }

   if {$plot_list($current_selection.type) == "function"} {
      $current.columns.top.button configure -state disabled -relief flat
      $current.columns.bot.button configure -state disabled -relief flat
      $current.columns.xcol.button configure -state disabled -relief flat
      $current.columns.ycol.button configure -state disabled -relief flat
      $current.key.keyentry configure -state normal -relief sunken
      $current.linestyle.btn configure -state normal -relief raised
      return
   }

   if {$plot_list($current_selection.linestyle) == "errorbars"} {
      # enable all
      $current.columns.top.button configure -state normal -relief raised
      $current.columns.bot.button configure -state normal -relief raised
      $current.columns.xcol.button configure -state normal -relief raised
      $current.columns.ycol.button configure -state normal -relief raised
      $current.key.keyentry configure -state normal -relief sunken
      $current.linestyle.btn configure -state normal -relief raised
      return
   }

   # enable x and y
   $current.columns.xcol.button configure -state normal -relief raised
   $current.columns.ycol.button configure -state normal -relief raised
   $current.columns.top.button configure -state disabled -relief flat
   $current.columns.bot.button configure -state disabled -relief flat
   $current.key.keyentry configure -state normal -relief sunken
   $current.linestyle.btn configure -state normal -relief raised

}

###################################################################
# Balloons
set BalloonWait 600
option add *tixBalloonFg Black
option add *tixBalloonBg Grey
tixAddBalloon .p.doit \
	{Send "plot" command to gnuplot with current settings.} \
	$BalloonWait
tixAddBalloon $mod_list.delete \
	{Delete item in "Plot Current" from the plot list.} \
	$BalloonWait
tixAddBalloon $mod_list.function \
	{Enter a function to be added to the "Plot List".} \
	$BalloonWait
tixAddBalloon $mod_list.file \
	{Select a file to be added to the "Plot List".} \
	$BalloonWait
tixAddBalloon $mod_list.list \
	{This Button pulls down a list of all items in the "Plot List". The "Plot List" contains all the items to be sent to gnuplot when the "Plot" button is pressed. You can select the "Plot Current" item from the "Plot List".} \
	$BalloonWait
tixAddBalloon .message \
	{Informative messages from TkGnuplot are displayed here. Click on the button on the right to see previous messages.} \
	$BalloonWait
tixAddBalloon .plot.current \
	{This frame contains the current plot selection.  All enabled attributes in the box can be modified. The choose a different plot item, select from the "Plot List" to the right.}
###################################################################
# Execute these scripts at the beginning executed at beginning
DisableButtons
set scalesty $scalesty   
set keysty $keysty   
if {$balloon_help == 0} {
   tixDisableBalloon
} else {
   tixEnableBalloon
}
set message  "Welcome to TkGnuplot!"

