#
# print options
#
# (c) Q Solutions 1998
#

proc prnOptionInit {} {}

proc prnBut { w text cmd } {

	set bt $w.but[newWin]
	pack [button $bt -text $text -command $cmd] -fill x -anc n -padx 5
}

proc prnCB { prn w text var } {

	set bt $w.but[newWin]
	pack [checkbutton $bt -text $text -variable $prn\($var) ] -anc nw -padx 5
}
proc prnRB { prn w text var val} {

	set bt $w.but[newWin]
	pack [radiobutton $bt -text $text -variable $prn\($var) -value $val] -anc nw -padx 5
}

proc prnE { prn w text len var } {

	set bt $w.en[newWin]
	pack [frame $bt -bd 1 -relief sunken] -anc nw -pady 2 -ipadx 2 -padx 5
	pack [label $bt.l -text $text -width 10 -anc w] -side left -anc w -padx 3
	pack [entry  $bt.e  -width $len -textvariable $prn\($var) ] \
		-side left -anc w -ipadx 5
}

proc prnF { prn w text var } {

	set bt $w.en[newWin]
	pack [frame $bt -bd 1 -relief sunken] -anc nw -pady 2 -ipadx 2 -padx 5
	pack [label $bt.l -text $text -width 6 -anc w] -side left -anc w -padx 3
	pack [entry  $bt.e  -width 15 -textvariable $prn\($var) ] \
		-side left -anc w -ipadx 5
	pack [button $bt.b -text "..." -command "prnFont $bt.e"] \
			-anc w -padx 5 -side left
}

proc prnC { prn w text var } {

	set bt $w.en[newWin]
	pack [frame $bt -bd 1 -relief sunken] -anc nw -pady 0 -ipadx 2 -padx 5
	pack [label $bt.l -text $text -width 4 -anc w] -side left -anc w -padx 3
	pack [entry  $bt.e  -width 8 -textvariable $prn\($var) ] \
		-side left -anc w -ipadx 5
	pack [button $bt.b -text "..." -command "prnColour $bt.e"] \
			-anc w -padx 5 -side left
}

proc prnFont { e } {
	set f [getFont]
	if { $f == "" } { return }
	$e delete 0 end
	$e insert end [join $f]
}

proc prnColour { e } {
	set f [getColour [$e get]]
	if { $f == "" } { return }
	$e delete 0 end
	$e insert end [join $f]
}

proc prnT { w text } {

	set bt $w.en[newWin]
	pack [label $bt -text $text -anc w] -anc nw -padx 3 -pady 3
	set f [$bt cget -font]
	$bt config -font "[lindex $f 0] [lindex $f 1] bold"
}

proc newWin {} {
	global plotWindowID 
	if { ! [info exists plotWindowID] } { set plotWindowID 0 }
	incr plotWindowID
	return $plotWindowID
}


proc prnOptions { name } {

	global GBL

	upvar #0 $name data

	set w .w${name}_options
	catch {destroy $w}
	#Log "$name: Names [lsort [array names data]]"
	set win $data(Win)
	toplevel $w 
	wm withdraw $w
	wm transient $w $win
	set geom [winfo geometry [winfo toplevel $win]]
	regsub {[0-9]+x[0-9]+} $geom "" geom
	wm geometry $w $geom
	#grab $w

	set title "Print Setup" 
	set mode Print
	pack [label $w.title -text $title] -pady 10
	set f [$w.title cget -font]
	set fn [lindex $f 0]
	set fs [lindex $f 1]
	set ft [lindex $f 2]

	pack [frame $w.p4 -bd 1 -relief solid] -side left -anc nw \
			-fill both -padx 2 -pady 5 -padx 5
	frame $w.p1 -bd 1 -relief solid 
	frame $w.p2 -bd 1 -relief solid
	frame $w.p3 -bd 1 -relief solid 


	if { ! ($mode == "Edit") } {
		pack [label $w.p1.h -text "  Page  " -bd 1 -relief sunken \
				-fg Blue -font "$fn [expr int($fs * 1.5)] $ft"] \
				-anc n -pady 2
		prnT $w.p1 "Margins"
		prnE $name $w.p1 Width 3 Page,Width
		prnE $name $w.p1 Height 3 Page,Height
		prnE $name $w.p1 Left 3 Page,LeftMargin
		prnE $name $w.p1 Top 3 Page,TopMargin
		prnE $name $w.p1 Scale 6 Page,Scale
		eval tk_optionMenu $w.p1.slist ${name}\(Page,Scale) $data(Page,SList)
		pack $w.p1.slist -side top -anc n
		prnT $w.p1 "Orientation"
		prnRB $name $w.p1 Landscape Page,Orientation Landscape
		prnRB $name $w.p1 Portrait Page,Orientation Protrait
		prnT $w.p1 "Options"
		prnCB $name $w.p1 Frame Page,PrintFrame
		prnCB $name $w.p1 Black/White Page,Monochrome
	}
	pack [label $w.p2.h -text "  Layers  " -bd 1 -relief sunken \
				-fg Blue -font "$fn [expr int($fs * 1.5)] $ft"] \
				-anc n -pady 2
	pack [frame $w.p2.disp] -anc nw
	pack [frame $w.p2.disp.l] -anc nw -side left
	pack [frame $w.p2.disp.r] -anc nw -side left
	pack [frame $w.p2.disp.rr] -anc nw -side left

	prnT $w.p2.disp.l "Display"
#	prnT $w.p2.disp.r "Display"
	foreach t $GBL(LayerList) {
		prnCB $name $w.p2.disp.l $t prn,Layer,$t
	}
	#prnCB $name $w.p2.disp.l Legend	DrawLegend
	#prnCB $name $w.p2.disp.l Dots	DrawDots
	#prnCB $name $w.p2.disp.l Lines	DrawLines
	#prnCB $name $w.p2.disp.l Smooth	LineSmooth
	#prnCB $name $w.p2.disp.l Events	DrawEvents
	#prnCB $name $w.p2.disp.l Marks	DrawMarks
	#prnCB $name $w.p2.disp.l Grid	DrawGrid
	#prnCB $name $w.p2.disp.r Reference	DrawReference
	#prnCB $name $w.p2.disp.r Text	DrawText
	#prnCB $name $w.p2.disp.r Title	DrawTitle
	#prnCB $name $w.p2.disp.r "Float Axis" AxisFloat

	pack [label $w.p3.h -text "  Types  " -bd 1 -relief sunken \
				-fg Blue -font "$fn [expr int($fs * 1.5)] $ft"] \
				-anc n -pady 2
	pack [frame $w.p3.disp] -anc nw
	pack [frame $w.p3.disp.l] -anc nw -side left
	pack [frame $w.p3.disp.r] -anc nw -side left
	pack [frame $w.p3.disp.rr] -anc nw -side left
	prnT $w.p3.disp.l "Display"
	foreach t $GBL(TypeList) {
		prnCB $name $w.p3.disp.l $t prn,Type,$t
	}
	prnT $w.p4 "Configure"
	if { $mode != "Edit" } {
		prnBut $w.p4 Page..  "pack forget $w.p2 $w.p3 ; \
			pack $w.p1 -anc nw -fill y -pady 5 -side left"
	}
	prnBut $w.p4 Layers..  "pack forget $w.p1 $w.p3 ; \
			pack $w.p2 -anc nw -fill y -pady 5 -side left"
	if { $mode != "Default" } {
		prnBut $w.p4 Types.. "pack forget $w.p1 $w.p2 ; \
			pack $w.p3 -anc nw -fill y -pady 5 -side left"
	}
	prnT $w.p4 "Control"
	if { $mode == "Print" } {
		set data(prn,Cancel) 0
		prnBut $w.p4 Print "destroy $w"
		prnBut $w.p4 Cancel "set $name\(prn,Cancel) 1 ; destroy $w"
	}
	pack $w.p1 -anc nw -fill y -pady 5 -side left
#	update idletasks
	wm deiconify $w
	return $w
}

proc prnOpt { name } {

	upvar #0 $name data
	global ColourMap

	set units {}

	tkwait window [prnOptions $name]
}





