# Print routines
# (c) Wallis Consulting

# DESCRIPTION:	Display a print dialog
# INPUT:	cmd - print command
proc dm_Print {cmd} {
	global Dm_Pref Dm_Path tcl_platform

	if {$tcl_platform(platform) == "unix"} {
		if ![winfo exists .print] {
			dm_CreatePrint .print
		} else {
			wm deiconify .print
			raise .print
		}
		.print.fb.ok config -command "dm_PrintCB $cmd" -text Print
	} else {
		# Use Windows common print dialog
		if {[info commands print] == ""} {
			load $Dm_Path/bin/comdlg.dll
		}
		if {[set result [print dialog]] != ""} {
			set fName [eval $cmd]
			if [catch {print file [rlindex result 0] $fName} err] {
				dm_Prompt .err $err Continue
			}
		}
	}
}

# DESCRIPTION:	Create Unix print dialog
proc dm_CreatePrint {w} {
	global Dm_Radio Dm_PrintCopies env tcl_platform

	set Dm_PrintCopies 1
	dm_MakeDlg $w "Print" {
		{Radio "Send To" {Printer File}}
		{Combo "Printer Name" {}}
		{Spin "Copies" 1 Dm_PrintCopies 99}
	}

	# File/Printer toggle
	set Dm_Radio($w.f.v0) 0
	$w.f.v0.r0 config -command "dm_PrintSendTo $w 0"
	$w.f.v0.r1 config -command "dm_PrintSendTo $w 1"

	dm_CreateDropList .printers $w.f.v1
	$w.f.mb1 config -command "dm_ShowDropList .printers"
	set i 0
	if {$tcl_platform(os) != "AIX"} {
		catch {
			foreach p [exec lpstat -a | sed "s/ .*//"] {
				.printers.l insert 0 [rlindex p 0]
				incr i
			}
		}
	} else { 
		#AIX
		catch {
			foreach p [exec lpstat -a | sed "s/ .*//"] {
				if {[set j [string last : $p]] != -1} {
					set p [string range $p 0 [incr j -1]]
					.printers.l insert 0 [rlindex p 0]
					incr i
				}
			}
		}
	}
	if {$i == 0} {
		# No printers?  Use environment variables (if any)
		$w.f.mb1 config -state disabled
		if [info exists env(PRINTER)] {
			$w.f.v1 insert 0 $env(PRINTER)
		} elseif [info exists env(LPDEST)] {
			$w.f.v1 insert 0 $env(LPDEST)
		}
	} else {
		# Default to first printer
		$w.f.v1 insert 0 [rlindex p 0]
	}

	pack forget $w.status

	$w.fb.cancel config -command {wm withdraw .print; wm withdraw .printers}
	$w.fb.help config -state normal -command {dm_Help help/dm.html#printSetup}
}

# DESCRIPTION:	Toggle between file and printer
# INPUT:	isFile - to file
proc dm_PrintSendTo {w isFile} {
	if $isFile {
		$w.f.l1 config -text "File Name:"
		pack forget $w.f.mb1
		foreach n {v2 s2.u s2.d} {
			$w.f.$n config -state disabled
		}
	} else {
		$w.f.l1 config -text "Printer Name:"
		pack $w.f.mb1 -side right -in $w.f.f1
		foreach n {v2 s2.u s2.d} {
			$w.f.$n config -state normal
		}
	}
}

# DESCRIPTION:	Generate a graph image and print it
# INPUT:	cmd - print proc
proc dm_PrintCB {args} {
	global Dm_Radio Dm_PrintCopies Dm_Status env tcl_platform

	if $Dm_Radio(.print.f.v0) {
		eval $args [.print.f.v1 get]
	} else {
		set fName [eval $args]
		if {$tcl_platform(os) == "HP-UX"} {
			if [catch {catch {eval exec lp -d[.print.f.v1 get] -n$Dm_PrintCopies $fName &}} err] {
				set Dm_Status $err; bell
				return
			}
		} else {
			if [catch {catch {eval exec lpr -P[.print.f.v1 get] -#$Dm_PrintCopies $fName &}} err] {
				set Dm_Status $err; bell
				return
			}
		}
	}
	wm withdraw .print
}
