#
# QCad
# (c) Q Solutions 1998
#
#
#
#
# Print Routines
#
#
###
### NEEDS WORK
###

proc canvasPrint { name  } {
	upvar #0 $name data
	global ColourMap
	global GBL 
	set rot 0
	set raw 0
	# FIX ME
	source printopt.tk

	prnOpt $name
	if { $data(prn,Cancel) } { return }

	set sizex $data(Page,Width)
	set sizey $data(Page,Height)
	set lmargin $data(Page,LeftMargin)
	set tmargin $data(Page,TopMargin)
	set data(Print,Canvas) $data(Win).print
	set units $data(Page,Units)
	set scale $data(Page,Scale)
	destroy $data(Print,Canvas)
	canvas $data(Print,Canvas)

	set data(Message) "Copying Canvas items ..."
	prnCopyCanvas $data(Canvas) $data(Print,Canvas)
	set c $data(Print,Canvas)
	set rev [expr 1.0 / $data(RelScale)]
	$c scale Obj 0 0 $rev $rev
	set anc sw
	set rot 0
	if { $data(Page,Orientation) == "Landscape" } {
		set rot 1
	}
	$c create rectangle 0 0 $sizex$units $sizey$units \
		-fill {} -outline Black -width 0.5 -tag PageFrame
	foreach {px1 py1 px2 py2} [$c bbox PageFrame] {}
	$c delete PageFrame
	if { $rot } {
		set anc nw
		prnSwap sizex sizey
		prnSwap lmargin tmargin
	}
	set cbox [$c bbox Obj] 
	#
	# Now remove data not wanted
	# do it here so the picture will print the same size
	# regardless of what layers and types are printed.
	foreach l $GBL(LayerList) {
		if { ! $data(prn,Layer,$l) } {
			$c delete Layer($l)
		}
	}
	foreach l $GBL(TypeList) {
		if { ! $data(prn,Type,$l) } {
			$c delete Type($l)
		}
	}
	if { $scale == "Fit" } {
		prnDoFit $name $c $cbox [list $rot $sizex $sizey $units] [list $tmargin $lmargin]
	} else {
		prnDoAtSize $name $c $cbox [list $rot $sizex $sizey $units] [list $tmargin $lmargin]
	}
	exec gv dump.ps &
	return ""
}

proc prnDoFit { name c csize psize margins} {

	upvar #0 $name data
	global GBL

	foreach {x1 y1 x2 y2} $csize {}

	foreach v {x2 y2} {set $v [expr [set $v] + 5]}
	foreach v {x1 y1} {set $v [expr [set $v] - 5]}

	foreach {rot sizex sizey units} $psize {}
	foreach {tmargin lmargin} $margins {}

	set ts  [winfo fpixels $c $tmargin$units]
	set ls  [winfo fpixels $c $lmargin$units]
	set px  [winfo fpixels $c $sizex$units]
	set py  [winfo fpixels $c $sizey$units]
	set ppx [expr $px - 2 * $ls]
	set ppy [expr $py - 2 * $ts]

	set cx [expr $x2 - $x1]
	set cy [expr $y2 - $y1]

	set rx [expr $ppx / $cx]
	set ry [expr $ppy / $cy]

	set mag [expr $rx > $ry ? $rx : $ry]
	set ls [expr $ls / $mag]
	set ts [expr $ts / $mag]

	set xp1 [expr $x1 - $ls]
	set xp2 [expr $x2 + $ls]
	set yp1 [expr $y1 - $ts]
	set yp2 [expr $y2 + $ts]

	if { $rx < $ry } {
		set pw "-pagewidth $sizex$units"
	} else {
		set pw "-pageheight $sizey$units"
	}

	# I GIVE UP
	# rotating postscript moves center of image
	# Try computing our own page center
	#
	set cx [expr $sizex / 2.0 ]
	set cy [expr $sizey / 2.0 ]
	if { $rot } { prnSwap cx cy}

	if {$data(Page,PrintFrame) } { 
		$c create rectangle [expr $x1 + 1  ] [expr $y1 + 1 ] [expr $x2 -1] [expr $y2 -1 ] \
			-fill {} -outline Black -width 0.5 -tag PageFrame
		#$c create rectangle [expr $xp1 +1] [expr $yp1+1] \
					[expr $xp2  -1] [expr $yp2 -1] \
			-fill {} -outline Blue -width 2 -tag PageFrame
	}
	set ps  "$c postscript -file dump.ps \
		-rotate $rot \
		-colormode $data(Print,Mode) \
		-colormap ::ColourMap \
		-x $xp1 -y $yp1  -width [expr $xp2 - $xp1] -height [expr $yp2 - $yp1] \
		$pw   -pagex $cx$units -pagey $cy$units \
		"
	eval $ps
	set data(Message) "Canvas printed to dump.ps \n $pw / $sizex $sizey"

}

proc prnDoAtSize {name c csize psize margins} {

	upvar #0 $name data
	global GBL 

	foreach {x1 y1 x2 y2} $csize {}
	foreach {rot sizex sizey units px1 py1 px2 py2} $psize {}
	foreach {tmargin lmargin} $margins {}
	set xs [expr $x2  - $x1]
	set ys [expr $y2  - $y1]
	set pxs $px2
	set pys $py2

	set cscale [expr [winfo fpixels $data(Win) 0.1i] / 100.0]
	foreach {a b} [split $data(Page,Scale) :] {}
	set cscale [expr $cscale * $a / $b]
	set cscale [expr $cscale * $a / $b]

	$c scale Obj 0 0 $cscale $cscale
	foreach {x1 y1 x2 y2} [$c bbox Obj] {}

	# FIX ME
	# SCALE Fonts 
	#
	set pw "-pagewidth $sizex$units -pageheight $sizey$units"
	if {$data(Page,PrintFrame) } { 
		$c create rectangle [expr $x1 - 5] [expr $y1 -5] [expr $x2 + 5] [expr $y2 + 5] \
			-fill {} -outline Black -width 0.5 -tag Frame
	}
	#$c create rectangle $px1 $py1 [expr $pxs] [expr $pys] \
			-fill {} -outline Blue -width 1 -tag xPage

	# Do each page
	set pw ""
	set ps  "$c postscript -file dump.ps \
		-rotate $rot \
		-colormode $data(Print,Mode) \
		-colormap ::ColourMap \
		$pw -pageanc nw  -pagex 1c -pagey 28c \
		-x $px1 -y $py1  -width $pxs -height $pys"
	eval $ps
	#puts "[$c bbox xPage] $psize"
}

proc prnCopyCanvas {src dst} {


	$dst delete all
	foreach id [$src find all] {
		set type [$src type $id]
		set coords [$src coords $id]
		set tags {}
		set opts {}
		foreach cfg [$src itemconfigure $id] {
			set opt [lindex $cfg 0]
			set data [lindex $cfg 4]
			if { "$opt" == "-tags" } {
				set tags $data
			} else {
				lappend opts $opt
				lappend opts $data
			}
	}
	set item [eval $dst create $type $coords $opts -tags {$tags}]

	}
	$dst delete System
	$dst delete Cursor
}

proc prnSwap {p1 p2} {upvar 1 $p1 a ; upvar 1 $p2 b; set t $a; set a $b ; set b $t}



