#************************************************************
# printdlg.tcl
#
# Print Dialog box
#
# Author : Dan Gunter
# Created: 2/27/98
#************************************************************

#########################
# PrintSelect --
#
#    Widget to select IPs.
#
proc PrintSelect { parent } {
    global param prOpt env widget

    set bwid 10
    set bbg $param(color-buttonbg)
    set bag $param(color-buttonabg)
    set bfg $param(color-buttonfg)
    set cbg $param(color-ctlbg)
    set fbg "#cccccc"
    set lbg #ffffff

    toplevel $parent

    # Main frames

    frame $parent.printers -bg $cbg
    frame $parent.options  -bg $cbg
    frame $parent.cmd      -bg $cbg
    frame $parent.formats  -bg $cbg

    # PRINTERS frame

    set f $parent.printers
    label $f.flabel -text "Printers" -bg $lbg \
	    -highlightbackground $lbg
    pack $f.flabel -side top
    if {[info exists env(PRINTER)]} {
	set defpr 0
    } else {
	set defpr 1
	set env(PRINTER) [lindex $param(printers) 0]
    }
    foreach i $param(printers) {
	radiobutton [string tolower "$f.$i"] -text $i -value $i -variable prOpt(printer) \
		-bg $cbg -highlightbackground $cbg
        if { !$defpr && $i == $env(PRINTER) } {
           set defpr 1
        }
	pack $f.$i -side top -anchor w
    }
    if { $defpr == 0 } {
	set i $env(PRINTER)
	radiobutton $f.$i -text $i -value $i -variable prOpt(printer) \
		-bg $cbg -highlightbackground $cbg
	pack $f.$i -side top -anchor w
    }
    set prOpt(printer) $env(PRINTER)

    # PRINTERS : Print to File

    set file [frame $f.file -bg $cbg]
    set widget(prOpt,file) $file
    checkbutton $file.c1 -text "Print to a file" \
	    -variable prOpt(file) \
	    -command DrawFileWidget \
	    -bg $cbg \
	    -highlightbackground $cbg
    frame $file.fmt -bg $fbg
    label       $file.c2 -text "Filename" \
	    -bg $cbg
    entry       $file.c3 -textvar prOpt(filename) \
	    -relief sunken \
	    -bg "white"
    DrawFileWidget
    pack $file.c1 $file.fmt $file.c2 $file.c3 -side top -anchor w
    pack $file -side bottom

    # FORMAT frame

    set f $parent.formats
    label $f.title \
	    -text "Format" \
	    -bg $lbg \
	    -highlightbackground $lbg
    set f [frame $f.options -bg $cbg]
    radiobutton $f.ps \
	    -bg $cbg \
	    -highlightbackground $cbg \
	    -text "PostScript(tm)" \
	    -variable prOpt(format) \
	    -value "postscript" \
	    -command ToggleGnuplotChoices
    radiobutton $f.gp \
	    -bg $cbg \
	    -highlightbackground $cbg \
	    -text "Gnuplot" \
	    -variable prOpt(format) \
	    -value "gnuplot" \
	    -command ToggleGnuplotChoices
    pack $f.ps $f.gp -side left -anchor w
    set f $parent.formats
    frame $f.gpc \
	    -bg $fbg
    CreateGnuplotChoices $f.gpc
    pack $f.title $f.options -side top -anchor w
    ToggleGnuplotChoices

    # OPTIONS frame

    set f $parent.options
    label $f.flabel -text "Options" -bg $lbg \
	    -highlightbackground $lbg
    frame $f.display -bg $cbg
    set disp $f.display
    pack $f.flabel $disp -side top

    label       $disp.a0 -text "ORIENTATION" -bg $cbg -highlightbackground $cbg
    radiobutton $disp.a1 -text "Landscape" -variable prOpt(orient) \
	    -value 1 -bg $cbg  -highlightbackground $cbg
    radiobutton $disp.a2 -text "Portrait" -variable prOpt(orient) \
	    -value 0 -bg $cbg  -highlightbackground $cbg
    label       $disp.b0 -text "COLOR" -bg $cbg \
	    -highlightbackground $cbg
    radiobutton $disp.b3 -text "Color" -variable prOpt(color) -value 0 -bg $cbg \
	    -highlightbackground $cbg
#    radiobutton $disp.b2 -text "Greyscale" -variable prOpt(color) \
#	    -value 1 -bg $cbg -highlightbackground $cbg
     radiobutton $disp.b1 -text "Black and White" -variable prOpt(color) \
            -value 2 -bg $cbg -highlightbackground $cbg

    pack $disp.a0 $disp.a1 $disp.a2 -side top -anchor w
    pack $disp.b0 $disp.b1 $disp.b3 -side top -anchor w

    # COMMANDS

    button $parent.cmd.ok -text "OK" \
	    -command [list prOKButtonPressed $parent] \
	    -borderwidth 4 -highlightbackground $cbg \
	    -bg $bbg -activebackground $bag -fg $bfg -padx 5 -pady 5
    button $parent.cmd.cancel -text "Cancel" \
	    -command [list prCancelButtonPressed $parent] \
	    -borderwidth 4 -highlightbackground $cbg \
	    -bg $bbg -activebackground $bag -fg $bfg -padx 5 -pady 5
    pack $parent.cmd.ok $parent.cmd.cancel -side left

    pack $parent.cmd \
	    -side bottom \
	    -fill x
    pack $parent.printers $parent.formats $parent.options \
	    -fill both \
	    -side left \
	    -anchor n
    return $parent
}

###################################

proc DrawFileWidget { } {
    global widget param prOpt

    set file $widget(prOpt,file)
    switch -exact $prOpt(file) {
	1 {
	    set newstate normal
	    set newbg "white"
	    set newfg "black"
	}
	0 { 
	    set newstate disabled
	    set newbg "#999999"
	    set newfg $newbg
	}
    }
 
    $file.c2 configure -fg $newfg
    $file.c3 configure -bg $newbg -state $newstate
}

##################################

proc CreateGnuplotChoices { parent } {
    global prOpt param widget

    set choices { \
            gnuplot \
	    aed512         aed767         aifm           bitgraph \
	    cgm            corel          dumb           dxf \
	    eepic          emtex          epson-180dpi   epson-60dpi \
	    epson-lx800    fig            gif            gpic \
	    hp2623a        hp2648         hp500c         hpdj \
	    hpgl           hpljii         hppj           imagen \
	    kc-tek40xx     km-tek40xx     latex          \
	    mf             mif            nec-cp6        okidata \
	    pbm            pcl5           png            postscript \
	    pslatex        pstex          pstricks       qms \
	    regis          selanar        starc          table \
	    tandy-60dpi    tek40xx        tek410x        texdraw \
	    tgif           tkcanvas       tpic           vttek \
	    x11            xlib }
    set cols 4
    set len [llength $choices]
    for { set i 0 } { $i < $cols } { incr i } {
	frame $parent.$i -bg $param(color-ctlbg)
    }
    for { set i 0 ; set j 0 } { $i < $len } { incr i } {
	radiobutton $parent.$j.$i -text [lindex $choices $i] \
		-variable prOpt(gnuplot_format) \
		-value [lindex $choices $i] \
		-bg $param(color-ctlbg) \
		-highlightbackground $param(color-ctlbg)
	pack $parent.$j.$i -side top -anchor w
	incr j
	set j [expr $j % $cols]
    }
    for { set i 0 } { $i < $cols } { incr i } {
	pack $parent.$i -side left -fill y
    }
    set widget(gpc_cols) $cols
    set widget(gpc) $parent
}

###############################

proc ToggleGnuplotChoices { } {
    global prOpt widget param

    if { $prOpt(format) == "gnuplot" } {
	$widget(gpc) configure -bg $param(color-ctlbg)
	pack $widget(gpc) -side top
    } else {
	pack forget $widget(gpc)
    }
}

#########################
# prOKButtonPressed

proc prOKButtonPressed { parent } {
    global param didPause prOpt

    set graphWidget .graph

    if { $prOpt(file) == 1 && $prOpt(filename) == "" } {
       return
    }


    # --- Print depending on format ---

    set landscape $prOpt(orient)
    set colormode [lindex {color gray mono} $prOpt(color)]

    if { $prOpt(format) == "gnuplot" } {
	if { $prOpt(file) } {
	    set dev $prOpt(filename)
	} else {
	    set dev "|$param(lpr) $prOpt(printer)"
	}
	set box [MessageBox "Writing the window to device '$dev' \
		using format '$prOpt(gnuplot_format)'." "Status" nowait]
	PrintGnuplot $prOpt(file) $prOpt(filename) $prOpt(gnuplot_format) \
		$landscape $colormode
	destroy $box
    } else { 
	if { $colormode == "mono" } {
	    Decolor
	}
	
	# Make elements for legend
	set ctr 0
	set f $param(DashFactor)
	foreach c $param(CLIST,name) {
	    if { 0 == [ catch { $graphWidget element configure L$ctr } ] } {
		$graphWidget element delete L$ctr
	    }
	    if { $colormode == "mono" } {
		$graphWidget element create L$ctr -label $c \
			-symbol none -dashes [expr $ctr*$f + 1]
	    } else {
		$graphWidget element create L$ctr -label $c \
			-symbol none -color $param(CLIST,color,$c)	 
	    }
	    incr ctr
	}
	# Map legend
	$graphWidget legend configure -hide no
	update idletasks
	
	if { $prOpt(file) == 1 } {
	    set ofile $prOpt(filename)
	} else {
	    set ofile "killme.tmp.ps"
	}
	
	$graphWidget postscript output $ofile \
		-landscape $landscape \
		-colormode $colormode \
		-paperheight 11i \
		-paperwidth 8.5i \
		-height 7.5i \
		-width 9.5i \
		-padx 0 \
		-pady 0
	
	if { $colormode == "mono" } {
	    Recolor
	}
	
	# Unmap legend
	$graphWidget legend configure -hide yes
	# Delete elements for legend
	set ctr 0
	foreach c $param(CLIST,name) {
	    $graphWidget element delete L$ctr
	    incr ctr
	}
	
	if { $prOpt(file) == 0 } {
	    set printCommand \
		    [exec $param(lpr) -P$prOpt(printer) $param(lpropts) $ofile]
	    exec rm $ofile
	}
    }

    # --- Cleanup ---

    destroy $parent
    update idletasks
    if { $didPause } {
	set param(Paused) 0
	Play
    }
}

#########################
# prCancelButtonPressed

proc prCancelButtonPressed { parent } {
    global didPause
    destroy $parent
    if { $didPause } {
	set param(Paused) 0
	Play
    }
}

#########################
# ShowPrintDlg --
#
#    Initialize and draw the widget
#
proc ShowPrintDlg { } {
    global param didPause

    if { $param(Paused) == 0 } { 
	set param(Paused) 1
	Pause
	set didPause 1 
    } else {
	set didPause 0
    }
    set dlg [PrintSelect .print]
    wm title $dlg "Print"
    wm geometry $dlg +100+200
}

########################

proc standalone { } {
    global param

    set param(Paused) 1
    set param(color-winbg) #b0d9d9
    set param(color-graphbg) #ffffff
    set param(color-menubg) #ffffff
    set param(color-menufg) #000000
    set param(color-menuabg) #00f9f9
    set param(color-ctlbg) #b0d9d9
    set param(color-buttonbg) #c1e2e2
    set param(color-buttonfg) #000000
    set param(color-buttonabg) #00f9f9
    set param(color-rewoff) #eeeeee
    set param(color-rewon) #b0d9d9
    set param(color-annotation) \#000000
    set param(printers) { itg1 itg2 itg3 }
}

global didPause
set prOpt(orient) 1
set prOpt(color) 2
set prOpt(format) postscript
set prOpt(gnuplot_format) gnuplot
