# Copyright (c) 2000, Red Hat, Inc.
# 
# This file is part of Source-Navigator.
# 
# Source-Navigator is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 2, or (at your option)
# any later version.
# 
# Source-Navigator is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along
# with Source-Navigator; see the file COPYING.  If not, write to
# the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
# MA 02111-1307, USA.
# 
#catch {itcl_unload PrintDialog&}

itcl_class PrintDialog& {
	inherit Toplevel&
	
	global sn_options
	
	constructor {config} {
		global sn_options
		global $this-wait
		global tcl_platform

		Toplevel&@@constructor

		foreach attr $config {
			config -$attr [set $attr]
		}

		if {$init_withdraw} {
		    $this withdraw
		}
		set header_font $sn_options(def,layout-font)

		$this title [get_indep String SQLPname]
		$this on_close "$twindow.actionframe.button_1 invoke"

		frame $twindow.pageframe\
			-relief {raised}\
			-border {2}

		label $twindow.pageframe.label\
			-relief {flat}\
			-anchor w\
			-text [get_indep String SQLPform]
	
		radiobutton $twindow.pageframe.landscape\
			-relief {flat}\
			-variable $this-rotate\
			-value 1\
			-anchor w\
			-text [get_indep String SQLPformland]

		radiobutton $twindow.pageframe.normal\
			-relief {flat}\
			-variable $this-rotate\
			-value 0\
			-anchor w\
			-text [get_indep String SQLPformnorm]

		#print command
		frame $twindow.printerframe\
			-relief {raised}\
			-border {2}
	
	    entry $twindow.printerframe.entry\
			-textvariable $this-PrinterCmd\
			-relief {sunken}
		@@bind $twindow.printerframe.entry <1> \
			"$twindow.printerframe.label select"

		@@bind $twindow.printerframe.entry <Return> \
			"$twindow.actionframe.button_0 invoke"

		# Printer
		radiobutton $twindow.printerframe.label\
			-variable $this-PrintTo\
			-value printer\
			-relief {flat}\
			-anchor w\
			-text [get_indep String SQLPprintercmd]\
			-command "$twindow.printerframe.entry config	-state normal"

		frame $twindow.printfile\
			-relief {raised}\
			-border {2}
	
		# Printfile
		radiobutton $twindow.printfile.label\
			-variable $this-PrintTo\
			-value file\
			-relief {flat}\
			-anchor w\
			-text [get_indep String SQLPfile]
	
		#Page format
		frame $twindow.pageformat\
			-relief {raised}\
			-border {2}
		label $twindow.pageformat.label\
			-relief {flat}\
			-anchor w\
			-text [get_indep String PrtPageFormat]
	
		set i 0
		foreach n $sn_options(def,page-formats) {
			radiobutton $twindow.pageformat.frmt$i \
				-relief {flat}\
				-variable sn_options(def,page-format)\
				-value [lindex $n 0]\
				-anchor w\
				-text [lindex $n 0] \
				-command "$this pageformat [lindex $n 0]" 
			incr i
		}
		
		#Print into file
		entry $twindow.printfile.entry\
			-textvariable $this-file\
			-relief {sunken}

		@@bind $twindow.printfile.entry <Return> \
			"$twindow.actionframe.button_0 invoke"
		@@bind $twindow.printfile.entry <1> \
			"$twindow.printfile.label invoke"

		frame $twindow.size\
			-relief {raised}\
			-border {2}

		label $twindow.size.label\
			-relief {flat}\
			-anchor w\
			-text [get_indep String SQLPprintwidth]

		entry $twindow.size.width${entry_pars}\
			-textvariable $this-pagewidth\
			-relief {sunken}\
			-width {5}
	
		@@bind $twindow.size.width${entry_pars} <Return> \
			"$twindow.actionframe.button_0 invoke"

		label $twindow.size.w_label\
			-relief {flat}\
			-anchor w\
			-text [get_indep String SQLPprintheight]
	
		entry $twindow.size.height${entry_pars}\
			-textvariable $this-pageheight\
			-relief {sunken}\
			-width {5}

		@@bind $twindow.size.height${entry_pars} <Return> \
			"$twindow.actionframe.button_0 invoke"

		label $twindow.size.unit\
			-relief {flat}\
			-anchor w\
			-textvariable $this-unit
			
		frame $twindow.coords\
			-relief {raised}\
			-border {2}

		label $twindow.coords.leftm_label\
			-relief {flat}\
			-anchor w\
			-text [get_indep String SQLPprintleftm]
	
		entry $twindow.coords.leftm_entry${entry_pars}\
			-textvariable $this-pagex\
			-relief sunken\
			-width 4

		@@bind $twindow.coords.leftm_entry${entry_pars} <Return> \
			"$twindow.actionframe.button_0 invoke"

	    label $twindow.coords.rightm_label\
		    -relief {flat}\
		    -anchor w\
		    -text [get_indep String ODDReportrightm]

	    @@set $this-pager $pager
	    entry $twindow.coords.rightm_entry${entry_pars}\
		    -textvariable $this-pager\
		    -relief sunken\
		    -width 4

		@@bind $twindow.coords.rightm_entry${entry_pars} <Return> \
			"$twindow.actionframe.button_0 invoke"

		label $twindow.coords.topm_label\
			-relief {flat}\
			-anchor w\
			-text [get_indep String SQLPprinttopm]
	
		entry $twindow.coords.topm_entry${entry_pars}\
			-textvariable $this-pagey\
			-relief {sunken}\
			-width 4

		@@bind $twindow.coords.topm_entry${entry_pars} <Return> \
			"$twindow.actionframe.button_0 invoke"

	    label $twindow.coords.botm_label\
		    -relief {flat}\
		    -anchor w\
		    -text [get_indep String ODDReportbotm]

	    @@set $this-pageb $pageb
	    entry $twindow.coords.botm_entry${entry_pars}\
		    -textvariable $this-pageb\
		    -relief {sunken}\
		    -width 4

		@@bind $twindow.coords.botm_entry${entry_pars} <Return> \
			"$twindow.actionframe.button_0 invoke"

		###########################################################
		#PostScript Factor and PageCount ##########################
		###########################################################
		frame $twindow.ps\
			-relief {raised}\
			-border {2}

		label $twindow.ps.label\
			-relief {flat}\
			-anchor w\
			-text [get_indep String SQLPprintfactor]
		entry $twindow.ps.factor_entry \
			-textvariable $this-factor_entry\
			-relief {sunken}\
			-width 4
		@@bind $twindow.ps.factor_entry <Leave> \
			"$this CalcPostScriptPages \[string trim \[%W get\]\]"
		@@bind $twindow.ps.factor_entry <Return> \
			"$twindow.actionframe.button_0 invoke"
			
		label $twindow.ps.pagev_label\
			-relief {flat}\
			-anchor w\
			-text [get_indep String SQLPprintPageV]
		entry $twindow.ps.pagev_entry \
			-textvariable $this-pagev_entry\
			-relief {sunken}\
			-width 4
			
		label $twindow.ps.pageh_label \
			-relief {flat} \
			-anchor w \
			-text [get_indep String SQLPprintPageH]
		entry $twindow.ps.pageh_entry \
			-textvariable $this-pageh_entry\
			-relief {sunken}\
			-width 4

		global $this-pr_pagenum
		@@set $this-pr_pagenum 1
		checkbutton $twindow.ps.pr_pagenum\
			-relief {flat}\
			-variable $this-pr_pagenum\
			-anchor w\
			-text [get_indep String SQLPPrintPageNum]

		###############################################################
		#END ##########################################################
		###############################################################

		frame $twindow.range_copy\
			-relief {raised}\
			-border {2}
		label $twindow.range_copy.range_label\
			-relief {flat}\
			-anchor w\
			-text [get_indep String SQLPprintrange]

		entry $twindow.range_copy.range_entry\
			-textvariable $this-range\
			-relief sunken\
			-width 4

		@@bind $twindow.range_copy.range_entry <Return> \
			"$twindow.actionframe.button_0 invoke"

		label $twindow.range_copy.copy_label\
			-relief {flat}\
			-anchor w\
			-text [get_indep String SQLPcopies]
	
		entry $twindow.range_copy.copy_entry%6_2_0\
			-textvariable $this-copycount\
			-relief sunken\
			-width 3

		@@bind $twindow.range_copy.copy_entry%6_2_0 <Return> \
			"$twindow.actionframe.button_0 invoke"

		frame $twindow.colorframe\
			-relief {raised}\
			-border {2}
	
		# Farbmodell
		label $twindow.colorframe.label\
			-relief {flat}\
			-anchor w\
			-text [get_indep String SQLPcolormodell]
	
		radiobutton $twindow.colorframe.mono\
			-relief {flat}\
			-variable $this-colormode\
			-value mono\
			-anchor w\
			-text [get_indep String SQLPmono]

		radiobutton $twindow.colorframe.gray\
			-relief {flat}\
			-variable $this-colormode\
			-value gray\
			-anchor w\
			-text [get_indep String SQLPgrey]
	
		radiobutton $twindow.colorframe.color\
			-relief {flat}\
			-variable $this-colormode\
			-value color\
			-anchor w\
			-text [get_indep String SQLPcolor]

		frame $twindow.actionframe\
			-relief {flat}\
			-border {2}

		# Buttons 
		sn_motif_buttons $twindow.actionframe\
			bottom 0\
			[get_indep String SQLPprintit]\
			[get_indep String cancel]

		if {$action == ""} {
			if {$canvas != ""} {
			    $twindow.actionframe.button_0 configure\
					-command "$this printit"
			} else {
			    $twindow.actionframe.button_0 configure\
					-command "set $this-wait 1"
			}
		} else {
			$twindow.actionframe.button_0 configure\
				-command "set $this-wait 1; $action $this"
		}

		$twindow.actionframe.button_1 configure\
			-command "set $this-wait 1;$this delete"

		#no printer command on windows (using standard font dialog)
		if {$tcl_platform(platform) != "windows"} {
			pack append $twindow \
				$twindow.printerframe {top frame center fillx}\
				$twindow.printfile {top frame center fillx}
		}
			
		pack append $twindow \
			$twindow.pageformat {top frame center fillx}\
			$twindow.size {top frame center fillx}\
			$twindow.coords {top frame center fillx}
		if {$canvas != ""} {
			pack append $twindow\
				$twindow.ps {top frame center fillx}
		}
		pack append $twindow\
			$twindow.range_copy {top frame center fillx}\
			$twindow.pageframe {top frame center fillx}
		if {$colormode != "none"} {
		    pack append $twindow\
			$twindow.colorframe {top frame center fillx}
		}
		pack append $twindow\
			$twindow.actionframe {bottom frame center fillx}
	
		#view radiobuttons for variable number of page formats
		pack append $twindow.pageformat\
			$twindow.pageformat.label {left frame w}
		set i 0
		foreach n $sn_options(def,page-formats) {
			pack append $twindow.pageformat \
				$twindow.pageformat.frmt$i {left frame w}
			incr i
		}

		pack append $twindow.pageframe\
			$twindow.pageframe.label {left frame w}\
			$twindow.pageframe.landscape {left frame w}\
			$twindow.pageframe.normal {left frame w}
	
		pack append $twindow.printerframe\
			$twindow.printerframe.label {left frame w}\
			$twindow.printerframe.entry \
			{left frame e fillx expand }
	
		pack append $twindow.printfile\
			$twindow.printfile.label {left frame w}\
			$twindow.printfile.entry {left frame e fillx expand}
	
		pack	$twindow.size.label -side left -anchor w
		pack	$twindow.size.width${entry_pars} -side left -anchor w
		pack	$twindow.size.w_label -side left -anchor w
		pack	$twindow.size.height${entry_pars} \
			    -side left -anchor w
		pack	$twindow.size.unit -side left -anchor w
			    
		pack	$twindow.coords.leftm_label -side left -anchor w -padx 5
		pack	$twindow.coords.leftm_entry${entry_pars} \
			    -side left -anchor w
		pack	$twindow.coords.rightm_label -side left -anchor w -padx 5
		pack	$twindow.coords.rightm_entry${entry_pars} \
			    -side left -anchor w
		pack	$twindow.coords.topm_label -side left -anchor w -padx 5
		pack	$twindow.coords.topm_entry${entry_pars} \
			    -side left -anchor w
		pack	$twindow.coords.botm_label -side left -anchor w -padx 5
		pack	$twindow.coords.botm_entry${entry_pars} \
			    -side left -anchor w

		pack append $twindow.ps\
			$twindow.ps.label {left frame w}\
			$twindow.ps.factor_entry {left frame w}\
			$twindow.ps.pagev_label {left frame w}\
			$twindow.ps.pagev_entry {left frame w}\
			$twindow.ps.pageh_label {left frame w}\
			$twindow.ps.pageh_entry {left frame w} \
			$twindow.ps.pr_pagenum {left frame w}
	
		pack append $twindow.colorframe\
			$twindow.colorframe.label { left frame w }\
			$twindow.colorframe.mono { left frame w }\
			$twindow.colorframe.gray { left frame w }\
			$twindow.colorframe.color { left frame w }
	
		set PrinterCmd $last_print_cmd

# We need the next loop to configure the global variables!
		set class [info class]
		foreach pub [info public] {
			if {[string first "$class@@" $pub] != -1} {
				config -$pub [set $pub]
			}
		}
		if {$canvas != ""} {
			$this CalcPostScriptPages ""
		}

		$this take_focus
		
		if {!$init_withdraw} {
		    $this move_to_mouse

			catch {$this resizable no no}

			update idletasks
			set geom [split [lindex [split [$this geometry] "+"] 0] "x"]
			$this minsize [lindex $geom 0] [lindex $geom 1]
		}
	}

	destructor {
		global $this-PrinterCmd
		set last_print_cmd [set $this-PrinterCmd]

		foreach var [@@info global "$this-*"] {
			catch {uplevel #0 unset $var}
		}
	}

	method config {config} {
	}
	
	#deletes an entry from string array
	proc DelArrEntry {array n} {
		upvar $array arr
		
		if { ! [info exist arr] } {
			set arr ""
		}
		
		set i [lsearch $arr $n]
		if { $i != -1 } {
			set arr [lreplace $arr $i $i]
		}
	}
	
	proc Round {num} {
		set i [string first . $num]
		if { $i != -1} {
			set mnom [string range $num [expr $i + 1] end]
			set num  [string range $num 0 [expr $i - 1]]
			if { $mnom > 0.0} {
				set num [expr $num + 1]
			}
		}
		return $num
	}
	
	method CalcPostScriptPages {factor} {
		global $this-colormode
		global $this-file
		global $this-pagex
		global $this-pagey
		global $this-pager
		global $this-pageb
		global $this-rotate
		global $this-pageheight
		global $this-pagewidth
		global $this-factor_entry
		global $this-pagev_entry
		global $this-pageh_entry		
		upvar #0 $this-pr_pagenum pr_pagenum
		
		set fac $factor
		DelArrEntry fac "%"
		
		if { [catch {set fac [expr int($fac) + 0]}]} {
			set fac 100
		}
		
		if {$fac > 600} {
			set fac 600
		}
		set tfac [expr $fac / 100.0]

		#get geometry to center textes
		if {$pr_pagenum} {
			set id [$canvas create text 0 0 -text "Page H0 V0"\
				-font $header_font]
			set tgeom   [$canvas bbox $id]
			set twidth  [expr [lindex $tgeom 2] - [lindex $tgeom 0]]
			set theight [expr [lindex $tgeom 3] - [lindex $tgeom 1]]
			set step    [expr $theight + 2]
			#add factor to the step
			#set step    [expr $step * $tfac]
			$canvas delete $id
		} else {
			set step 0.0
		}
		
		set scr [$canvas cget -scrollregion]
		set canv_width  [lindex $scr 2]
		set canv_height [lindex $scr 3]
		if { $canv_width == ""} {
			return ""
		}
		if { $rotate == 0 } {
		   set pixels_per_pageh [winfo fpixels $canvas [set $this-pagewidth]$unit]
		   set pixels_per_pagev [winfo fpixels $canvas [set $this-pageheight]$unit]
		} else {
		   set pixels_per_pagev [winfo fpixels $canvas [set $this-pagewidth]$unit]
		   set pixels_per_pageh [winfo fpixels $canvas [set $this-pageheight]$unit]
		}
		set pages_h [expr [expr $canv_width  * $tfac] / $pixels_per_pageh]
		set pages_v [expr [expr $canv_height * $tfac] / $pixels_per_pagev]

#		set pages_v [expr $pages_v * [expr $fac / 100.0]]
#		set pages_h [expr $pages_h * [expr $fac / 100.0]]
		
		if {$pages_v < 0} {
		   set pages_v 1
		}
		if {$pages_h < 0} {
		   set pages_h 1
		}
		
		#round to next (gerade) number
		set pages_h [Round $pages_h]
		set pages_v [Round $pages_v]
		
		$twindow.ps.factor_entry delete 0 end
		$twindow.ps.factor_entry insert 0 "$fac"

#		$twindow.ps.factor_entry insert end "%"

		$twindow.ps.pagev_entry delete 0 end
		$twindow.ps.pagev_entry insert 0 "$pages_v"

		$twindow.ps.pageh_entry delete 0 end
		$twindow.ps.pageh_entry insert 0 "$pages_h"
		
		return [list $pages_v $pages_h]
	}


	method PrintPostScriptPages {} {
		global $this-colormode
		global $this-file
		global $this-pagex
		global $this-pagey
		global $this-pager
		global $this-pageb
		global $this-rotate
		global $this-pageheight
		global $this-pagewidth
		global $this-factor_entry
		global $this-pagev_entry
		global $this-pageh_entry
		global print_fontmap
		global sn_options
		
		#move all objects of the canvas to display a title
		if { ($pages_h > 1 || $pages_v > 1) && $pr_pagenum != 0 } {
#			foreach it [$canvas find withtag all] {
#				$canvas move $it 0 $step
#			}
			$canvas move all 0 $step
		}
		#move all boxes tow pixels to upper, to center text in the boxes on paper
		#that meen, the outfit is different on SCREEN and on PAPER
		#This is a workaround for a Tk bug.
		$canvas move %boxes% 0 -2
		
		set factor [expr [set $this-factor_entry] / 100.0]
		set pixels_hi [expr $pixels_per_pagev / $factor]
		set pixels_wi [expr $pixels_per_pageh / $factor]

		set t [expr [expr $pagew * $pixels_hi] / $pageh]
		if { [expr [expr $pixels_hi + $step] / $pageh] > [expr $pixels_wi / $pagew]} {
			set pixels_wi [expr [expr $pagew * $pixels_hi] / $pageh]
		}

		set pfd [open [set $this-file] "w+"]
		@@fconfigure $pfd -encoding $sn_options(def,system-encoding)
		set t "_"
		set i 0
		while {$i < $pages_h} {
		
			set j 0
			while {$j < $pages_v} {
			
				#horizontal pages
				set xi [expr $pixels_wi * $i]
				#vertical pages
				set yi [expr $pixels_hi * $j]
				#set pageanchor "center"
				
				#print a title if more than one page
				if { ($pages_h > 1 || $pages_v > 1) && $pr_pagenum != 0 } {
					set geom [list $xi $yi [expr $xi + $pixels_wi] [expr $yi + $step - 1]]
					set id2 [eval $canvas create rect $geom \
						-outline red \
						-fill white \
						-outline white \
						-tags "Printer:Dlg"]
					set tx	[expr $xi + [expr [expr $pixels_wi - $twidth] / 2]]
					set ty	[expr $yi + [expr [expr $step - $theight] / 2]]
					set id1 [$canvas create text \
						$tx $ty \
						-font $header_font\
						-text "[get_indep String SQLPPage] [expr $j + 1]-[expr $i + 1]" \
						-anchor nw\
						-tag "Printer:Dlg"]
					$canvas raise $id1 $id2
					
					#wait to draw the new widgets
					update
				}
				
#				eval
				puts $pfd [$canvas postscript \
						-x $xi\
						-y $yi\
						-width $pixels_wi \
						-height [expr $pixels_hi + $step] \
						-pagex $leftm$unit \
						-pagey $topm$unit \
						-rotate [set $this-rotate] \
						-colormode [set $this-colormode] \
						-fontmap print_fontmap \
						-pagewidth  $pagew$unit \
						-pageheight $pageh$unit \
						-pageanchor $pageanchor]
#						-file [set $this-file]$j$t$i.ps
				incr j
			}
			incr i
		}
		close $pfd
		
		#move back objects of the canvas to original coordinates
		set test 0
		if {$test == 0} {
		if { ($pages_h > 1 || $pages_v > 1) && $pr_pagenum != 0 } {
			#remove all created items on the canvas created by printer
			$canvas delete Printer:Dlg
#			foreach it [$canvas find withtag all] {
#				$canvas move $it 0 -${step}
#			}
			$canvas move all 0 -${step}
		}
		}
		$canvas move %boxes% 0 2
	}
	
	method postscript_parameters {} {
		global $this-colormode
		global $this-file
		global $this-pagex
		global $this-pagey
		global $this-pager
		global $this-pageb
		global $this-rotate
		global $this-pageheight
		global $this-pagewidth
		global $this-factor_entry
		global $this-pagev_entry
		global $this-pageh_entry		
		
		if { $rotate == 0 } {
		} else {
		}

		set rotate [set $this-rotate]
		if {$rotate} {
			set topm    [set $this-pagex]
			set leftm   [set $this-pagey]
			set rightm  [set $this-pageb]
			set botm    [set $this-pager]

			set pageh [expr [expr [set $this-pagewidth ] - $topm ] - $botm]
			set pagew [expr [expr [set $this-pageheight] - $leftm] - $rightm]
		} else {
			set topm    [expr [set $this-pageheight] - [set $this-pagey]]
			#set topm    [set $this-pagey]
			set leftm   [set $this-pagex]
			set rightm  [set $this-pager]
			set botm    [set $this-pageb]

			set pagew [expr [expr [set $this-pagewidth] - $leftm] - $rightm]
			set pageh [expr $topm - $botm]
		}
		##scale the returned values to Printer DPI's
		set pagew [expr $pagew * $scr_factor]
		set pageh [expr $pageh * $scr_factor]
		
		set pars "-colormode [set $this-colormode]\
			-file [set $this-file]\
			-pagex $leftm$unit\
			-pagey $topm$unit\
			-rotate $rotate\
			-pageanchor $pageanchor\
			-x $x\
			-y $y "

		set scr [lindex [$canvas config -scrollregion] 4]
		set x_size [lindex $scr 2]
		set y_size [lindex $scr 3]

		if	{[winfo fpixels $canvas [set $this-pageheight]$unit] < $y_size} {
			set pgh [expr [set $this-pageheight] - [set $this-pagey]]
			append pars "-width $x_size -height $y_size -pageheight $pgh$unit"
		} elseif {[winfo fpixels $canvas [set $this-pagewidth]$unit] < $x_size} {
			set pgh [expr [set $this-pagewidth] - [set $this-pagex]]
			append pars "-width $x_size -height $y_size -pagewidth $pgh$unit"
		} else {
			append pars "-width [set $this-pagewidth]$unit\
				-height [set $this-pageheight]$unit"
		}
		
		if {[CalcPostScriptPages [set $this-factor_entry]] == "" } {
			return ""
		}
		
		return $pars
	}

	method printit {} {
		global sn_options
		global $this-PrintTo
		global $this-PrinterCmd
		global $this-file
		global $this-colormode
		global env sn_debug tcl_platform

		set bsy [Busy& #auto]
		$bsy hold;			# hold toplevel twindows

		if {$no_show_lines &&
			[string first "color" [winfo visual .]] != -1 &&
			[set $this-colormode] == "mono"} {

			set map 1
		} else {
			set map 0
		}

		if {$map} {
			set pars ""
			foreach it [$canvas find withtag all] {
				set type [$canvas type $it]
				switch $type {
				"rectangle" {
					lappend pars [list $it -outline \
						[lindex [$canvas \
						itemconfigure $it -outline] 4]]
					$canvas itemconfigure $it\
						-outline $sn_options(def,print-form-fg)
				    }
				"line" {
					lappend pars [list $it -fill \
						[lindex [$canvas \
						itemconfigure $it -fill] 4]]
					$canvas itemconfigure $it -fill $sn_options(def,print-form-bg)
					}
				"text" {
					lappend pars [list $it -fill \
						[lindex [$canvas \
						itemconfigure $it -fill] 4]]
					$canvas itemconfigure $it -fill Black
				    }
				}
			}
		}
		
		set ps_cmd [postscript_parameters]
		sn_log "postscript parameters: <$ps_cmd>"

		if {$tcl_platform(platform) != "windows" && $ps_cmd == "" } {
			bell -displayof $canvas
			$bsy delete
			return
		}
		
		if {$tcl_platform(platform) != "windows"} {
			set print_cmd [set $this-PrinterCmd]
			set file [set $this-file]
			set file [file nativename $file]
		} else {
			set file [sn_tmpFileName]
			set $this-file $file
			#dummy
			set print_cmd ide_winprint
		}

		#print postscript into the specified file
		$this PrintPostScriptPages
		
		if {$map} {
			foreach cmd $pars {
				eval $canvas itemconfigure $cmd
			}
		}

		if {$tcl_platform(platform) == "windows"} {
			sn_print_file $print_cmd $file -postscript
			catch {file delete -force -- $file}
		} elseif {[set $this-PrintTo] == "printer" &&	[string compare $print_cmd ""] != 0} {
			sn_print_file $print_cmd $file
		}
		$bsy delete

		$this delete
	}

	method Take {} {
		#tkwait visibility $twindow
		if {$grab} {
			$this grab set
		}
		tkwait variable $this-wait

		if {$grab} {
			catch {$this grab release}
		}
		set pars [$this postscript_parameters]

		$this delete

		return $pars
	}

	public no_show_lines 0

	public colormap {} {
	}
	public colormode {mono} {
		global $this-colormode
		@@set $this-colormode $colormode
	}
	public file {} {
		global env
		global $this-file
		global sn_options

		if {[string compare $file ""] == 0} {
			set file [file join $sn_options(profile_dir) print.ps]
		}
		@@set $this-file $file
	}
	public fontmap {} {
	}
	public pageanchor {nw} {
	}
	public pageheight {29.7} {
		global $this-pageheight
		@@set $this-pageheight $pageheight
	}
	public pagewidth {21} {
		global $this-pagewidth
		@@set $this-pagewidth $pagewidth
	}
	public pagex {0.5} {
		global $this-pagex
		@@set $this-pagex $pagex
	}
	public pager {0.5} {
		global $this-pager
		@@set $this-pager $pager
	}
	public pagey {1.0} {
		global $this-pagey
		@@set $this-pagey $pagey
	}
	public pageb {1.0} {
		global $this-pageb
		@@set $this-pageb $pageb
	}
	public rotate {0} {
		global $this-rotate
		@@set $this-rotate $rotate
	}
	public x {0}

	public y {0}

	public PrintTo {file} {
		global $this-PrintTo
		@@set $this-PrintTo $PrintTo
	}
	public PrinterCmd {} {
		global sn_options
		global $this-PrinterCmd

		if {[string compare $PrinterCmd ""] == 0} {
			catch {set PrinterCmd $sn_options(def,print-command)}
		}
		if {[string compare $PrinterCmd ""] != 0} {
			set state normal
		} else {
			set state disabled
		}
		if {[winfo exists $twindow]} {
			$twindow.printerframe.entry config -state $state
		}
		@@set $this-PrinterCmd $PrinterCmd
	}
	public range [get_indep String SQLPprintall] {
		global $this-range
		@@set $this-range $range
	}
	public copycount {1} {
		global $this-copycount
		@@set $this-copycount $copycount
	}
	
	public pageformat "" {
		global sn_options
		
		if {$sn_options(def,page-format) == ""} {
			@@set sn_options(def,page-format) [lindex [lindex $sn_options(def,page-formats) 0] 0]
		}
		
		if {$pageformat != ""} {
			@@set sn_options(def,page-format) $pageformat
		} else {
			set pageformat $sn_options(def,page-format)
		}
		
		pageformat $pageformat
	}
	
	method pageformat {fmt} {
		global sn_options
		global $this-pagewidth $this-pageheight $this-unit
		global $this-pagex 0.5 $this-pager $this-pagey $this-pageb

		upvar #0 sn_options(def,page-format) pf
		upvar #0 $this-factor_entry factor
		
		set pageformat $pf
		
		set fnd 0
		foreach n $sn_options(def,page-formats) {
			if {[lindex $n 0] == $fmt} {
				set pagewidth  [lindex $n 1]
				set pageheight [lindex $n 2]
				set unit      [lindex $n 3]
				set fnd 1
			}
		}
		if {$fnd == 0} {
			bell
			return
		}
		if {$unit == "cm"} {
			set unit "c"
		} else {
			set unit "i"
		}
		
		@@set $this-pagewidth $pagewidth
		@@set $this-pageheight $pageheight
		if {$unit == "i"} {
			@@set $this-unit "inch"
			set pagex 0.2
			set pager 0.2
			set pagey 0.4
			set pageb 0.4
		} else {
			@@set $this-unit "cm"
			set pagex 0.5
			set pager 0.5
			set pagey 1.0
			set pageb 1.0
		}
		@@set $this-pagex $pagex
		@@set $this-pager $pager
		@@set $this-pagey $pagey
		@@set $this-pageb $pageb
		CalcPostScriptPages $factor
	}
	
	public canvas {}
	public action {}
	public unit {c}
	protected busy {}
	protected entry_pars "%1_6_2"
	common last_print_cmd {}
	public init_withdraw 0
	public grab 1

	common    scr_factor 1.0
	protected x_size
	protected y_size
	protected pixels_per_pageh
	protected pixels_per_pagev
	protected pages_v
	protected pages_h
	protected topm
	protected leftm
	protected rightm
	protected botm
	protected pageh
	protected pagew
	protected tx
	protected ty
	protected twidth
	protected theight
	protected step
	protected pr_pagenum 1
	common header_font
}

proc custom_pos {} {
    global lay_pos
    
    if {[info exists rep_txt_pos] == 1} {
	return
    }
    set lay_pos [Toplevel& @@ next_window_name]
    Toplevel& $lay_pos
    $lay_pos withdraw
    $lay_pos title [get_indep String ODDReportFieldPosTitle]
    
    set obj $lay_pos.fr_pos
    frame $obj 
    frame $obj.tmp -bd 5
    text $obj.tmp.text_pos -relief raised -bd 2 \
	    -yscrollcommand "$obj.tmp.scroll set" \
	    -width 40 -height 10
    scrollbar $obj.tmp.scroll \
	    -command "$obj.tmp.text_pos yview"
    label $obj.tmp.l -text [get_indep String ODDReportFieldPos] -padx 10
    pack $obj.tmp.l -side left
    pack $obj.tmp.scroll -side right -fill y
    pack $obj.tmp.text_pos -side left -fill both -expand y
    pack $obj.tmp -side top -fill both -expand y
    frame $obj.butt -bd 5
    button $obj.butt.ok -text [get_indep String ODDReportTakeOver] -relief raised\
	    -command {get_pos; unset lay_pos; unset rep_names_pos} -width 10 
    button $obj.butt.his -text [get_indep String ODDSapHistory] -relief raised\
	    -command "SQLHistory" -width 10 
    button $obj.butt.cancel -text [get_indep String cancel] -relief raised\
	    -command {$lay_pos delete; unset lay_pos; unset rep_names_pos} -width 10 
    pack $obj.butt.ok -side left -expand y
    pack $obj.butt.his -side left -expand y
    pack $obj.butt.cancel -side left -expand y
    pack $obj.butt -side top -fill x
    pack $obj -side bottom -fill both -expand y
    $lay_pos move_to_mouse
    $lay_pos on_close "$lay_pos delete; unset lay_pos; unset rep_names_pos"
    put_field_names
}

proc put_field_names {} {
    global lay_pos
    global field_names_pos
    global rep_names_pos
		global rep_txt_pos
    
    set new_field_name ""
    if {$field_names_pos != ""} {
	foreach elem $field_names_pos {
	    set new_field_name [lappend new_field_name $elem\n]
	}
	regsub -all "\{" $new_field_name "" new_field_name
	regsub -all "\} " $new_field_name "" new_field_name
	regsub -all "\}" $new_field_name "" new_field_name
	regsub -all "," $new_field_name "." new_field_name
	set rep_txt_pos $new_field_name
	set rep_names_pos $rep_txt_pos
	$lay_pos.fr_pos.tmp.text_pos insert 0.0 $rep_txt_pos
    }
}

proc get_pos {} {
    global lay_pos
    global rep_txt_pos
    global rep_names_pos
    
    set rep_txt_pos ""
    set tmp_field_name ""
    if {[winfo exists $lay_pos.fr_pos.tmp.text_pos] == 1} {
	set tmp_field_name "[$lay_pos.fr_pos.tmp.text_pos get 0.0 end]"
    }
    if {$tmp_field_name != ""} {
	set rep_txt_pos [split $tmp_field_name "\n"]
	regsub -all "\{\}" $rep_txt_pos "" rep_txt_pos
    }
    regsub -all "," $rep_txt_pos "." rep_txt_pos
    set poslen [llength $rep_txt_pos]
    for {set anz 0} {$anz < $poslen} {incr anz} {
	set elem [lindex $rep_txt_pos $anz]
	if {[llength $elem] != 3} {
	    if {[llength $elem] != 1} {
		sn_error_dialog [get_indep String ReportPosError]
		return
	    }
	}
	if {[llength $elem] == 3} {
	    if {![regexp {^([ 0-9. ]+)$|^[ 0-9 ]+$} [lindex $elem 1]] ||
	    ![regexp {^([ 0-9. ]+)$|^[ 0-9 ]+$} [lindex $elem 2]] ||
	    [lindex $elem 1]	== "." || [lindex $elem 2]	== "."} {
		sn_error_dialog [get_indep String ReportPosError]
		return
	    }
	}
    }
    set rep_names_pos $rep_txt_pos
    set HistString "report $rep_txt_pos"
    SQLHistoryAppendCommand $HistString
    SQLHistoryWrite	
    
    destroy $lay_pos
}

proc REPClear {} {
    global lay_pos
    
    if {[winfo exists $lay_pos.fr_pos.tmp.text_pos] == 1} {
	$lay_pos.fr_pos.tmp.text_pos delete 1.0 end
    }
}

proc put_field_pos {field_names} {
    global lay_pos
    global rep_txt_pos
    global rep_names_pos
    
    set new_field_name ""
    if {$field_names != ""} {
	set new_field_name [lappend new_field_name $field_names\n]
	regsub -all "\{" $new_field_name "" new_field_name
	regsub -all "\} " $new_field_name "" new_field_name
	regsub -all "\}" $new_field_name "" new_field_name
	regsub -all "," $new_field_name "."	new_field_name		
	set rep_txt_pos $new_field_name
	set rep_names_pos $rep_txt_pos
	$lay_pos.fr_pos.tmp.text_pos insert 0.0 $rep_txt_pos
    }	
    set rep_names_pos $rep_txt_pos
    return $rep_txt_pos
}

