
proc PageitemStartDrag {c x y tag} {
    global PageSetup
    set PageSetup(lastX) $x
    set PageSetup(lastY) $y
    #$c dtag $tag
    #$c addtag $tag withtag current
    #$c raise current
}

proc PageitemDrag {c x y} {
    global PageSetup
#     set x [$c canvasx $x]
#     set y [$c canvasy $y]
    set taglist [ $c gettags current]
    set PageSetup(ChangingMarginEntries) 2
    set factor $PageSetup(CurrentFactorCanvas)
    if { $PageSetup(PageUnits) == "In"} {    
	set factor [ expr $PageSetup(CurrentFactorCanvas)*$PageSetup(In2mm)]
    }
    if { [ lsearch $taglist left] != -1} {
	$c move current [expr $x-$PageSetup(lastX)] 0
	set PageSetup(EntryLeft) [ expr $PageSetup(EntryLeft) + \
		( $x - $PageSetup(lastX)) / $factor]
    } elseif { [ lsearch $taglist right] != -1} {
	$c move current [expr $x-$PageSetup(lastX)] 0
	set PageSetup(EntryRight) [ expr $PageSetup(EntryRight) - \
		( $x - $PageSetup(lastX)) / $factor]
    } elseif { [ lsearch $taglist top] != -1} {
	$c move current 0 [expr $y-$PageSetup(lastY)]
	set PageSetup(EntryTop) [ expr $PageSetup(EntryTop) + \
		( $y - $PageSetup(lastY)) / $factor]
    } elseif { [ lsearch $taglist bottom] != -1} {
	$c move current 0 [expr $y-$PageSetup(lastY)]
	set PageSetup(EntryBottom) [ expr $PageSetup(EntryBottom) - \
		( $y - $PageSetup(lastY)) / $factor]
    }
    set PageSetup(ChangingMarginEntries) 0
    set PageSetup(lastX) $x
    set PageSetup(lastY) $y
}

proc PageFillDefaults {} {
    global PageSetup

    set page_dim [.central.s info post get pagedimensions]
    # en pulgadas: PS_PAGE_X, PS_PAGE_Y, PS_TAM_X, PS_TAM_Y, PS_TAM_W, PS_TAM_H, landscape/portrait
    #puts $page_dim
    
    set pagewidth [ lindex $page_dim 0]
    set pageheight [ lindex $page_dim 1]
    set leftmargin [ lindex $page_dim 2]
    set topmargin [ lindex $page_dim 3]
    set imgwidth [ lindex $page_dim 4]
    set imgheight [ lindex $page_dim 5]

    if { [ lindex $page_dim 6] == "landscape"} {
	set PageSetup(PageOrientation) [_ "Landscape"]
	set maxwidth $pageheight
	set maxheight $pagewidth
    } else {
	set PageSetup(PageOrientation) Portrait
	set maxwidth $pagewidth
	set maxheight $pageheight
    }

    if { ( [ expr $leftmargin + $imgwidth] > $maxwidth) || \
	    ( [ expr $topmargin + $imgheight] > $maxheight)} {
	set pp $imgwidth
	set imgwidth $imgheight
	set imgheight $pp
    }

    set PageSetup(DefaultMarginTop) [ expr $topmargin * $PageSetup(In2mm)]
    set PageSetup(DefaultMarginLeft)  [ expr $leftmargin * $PageSetup(In2mm)]
    if { [ lindex $page_dim 6] == "landscape"} {
	set PageSetup(DefaultMarginBottom) [ expr ( $pagewidth - $topmargin - $imgheight) \
		* $PageSetup(In2mm)]
	set PageSetup(DefaultMarginRight) [ expr ( $pageheight - $leftmargin - $imgwidth) \
		* $PageSetup(In2mm)]
    } else {
	set PageSetup(DefaultMarginBottom) [ expr ( $pageheight - $topmargin - $imgheight) \
		* $PageSetup(In2mm)]
	set PageSetup(DefaultMarginRight) [ expr ( $pagewidth - $leftmargin - $imgwidth) \
		* $PageSetup(In2mm)]
    }
    set PageSetup(DefaultPageWidth) [ expr $pagewidth * $PageSetup(In2mm)]
    set PageSetup(DefaultPageHeight) [ expr $pageheight * $PageSetup(In2mm)]
    set PageSetup(DefaultWidth) 20
    set PageSetup(DefaultHeight) 20

    set PageSetup(DefaultMarginTop) [ format %.4g $PageSetup(DefaultMarginTop)]
    set PageSetup(DefaultMarginBottom) [ format %.4g $PageSetup(DefaultMarginBottom)]
    set PageSetup(DefaultMarginLeft) [ format %.4g $PageSetup(DefaultMarginLeft)]
    set PageSetup(DefaultMarginRight) [ format %.4g $PageSetup(DefaultMarginRight)]
    set PageSetup(DefaultPageWidth) [ format %.3g $PageSetup(DefaultPageWidth)]
    set PageSetup(DefaultPageHeight) [ format %.3g $PageSetup(DefaultPageHeight)]

    set HardcopyOptions [ .central.s info post get HardcopyOptions]
    
    set PageSetup(AutoCrop) [ lindex $HardcopyOptions 1]

    #puts "$PageSetup(DefaultMarginTop) $PageSetup(DefaultMarginBottom)"
    #puts "$PageSetup(DefaultMarginLeft) $PageSetup(DefaultMarginRight)"
    #puts "$PageSetup(DefaultPageWidth) $PageSetup(DefaultPageHeight)"

}

proc PageAcceptEntry { cual } {
    global PageSetup

    #set PageSetup(Entry$cual) $PageSetup(Entry$cual)
    PageChangeEntry$cual "" "" ""
}

proc ChangeCenteredPage { cv width height} {
    global PageSetup

    set min $PageSetup(PageCanvasWidth)
    if { $PageSetup(PageCanvasHeight) < $min} {
	set min $PageSetup(PageCanvasHeight)
    }

    set factor [ expr $min * $PageSetup(FactorCanvas) ]

    set PageSetup(CurrentFactorCanvas) $factor

    set w [ expr $width * $factor]
    set h [ expr $height * $factor]
    set x1 [ expr ( $PageSetup(PageCanvasWidth) - $w ) / 2 ]
    set y1 [ expr ( $PageSetup(PageCanvasHeight) - $h ) / 2 ]

    #$cv create rectangle 1 1 $PageSetup(PageCanvasWidth) $PageSetup(PageCanvasHeight) -outline red

    # la sombra
    if { [ info exist PageSetup(PageViewSombra) ] } {
	$cv delete $PageSetup(PageViewSombra) 
    }
    set PageSetup(PageViewSombra) [ $cv create rectangle [ expr $x1 + 6] [ expr $y1 + 4]  \
	    [ expr $x1 + $w + 6] [ expr $y1 + $h + 4] \
	    -fill grey60 -outline grey60]

    if { [ info exist PageSetup(PageView) ] } {
	$cv delete $PageSetup(PageView) 
    }
    set PageSetup(PageView) [ $cv create rectangle $x1 $y1 [ expr $x1 + $w] [ expr $y1 + $h] \
	    -fill white -outline black]

    if { [ info exist PageSetup(ImageView) ] } {
	$cv delete $PageSetup(ImageView) 
    }
    set ml [ expr $PageSetup(MarginLeft) * $factor]
    set mr [ expr $PageSetup(MarginRight) * $factor]
    set mt [ expr $PageSetup(MarginTop) * $factor]
    set mb [ expr $PageSetup(MarginBottom) * $factor]
    set PageSetup(ImageView) [ $cv create rectangle [ expr $x1 + $ml] [ expr $y1 + $mt] \
	    [ expr $x1 + $w - $mr] [ expr $y1 + $h - $mb] \
	    -fill black -outline black]

    if { [ info exist PageSetup(PageViewImagen) ] } {
	$cv delete $PageSetup(PageViewImagen) 
    }
    set midx [ expr ( $x1 + $ml + $x1 + $w - $mr) / 2.0]
    set midy [ expr ( $y1 + $mt + $y1 + $h - $mb) / 2.0]
    set PageSetup(PageViewImagen) [ $cv create image $midx $midy \
	    -image $PageSetup(GiDImage) -anchor center]

    # Margenes
    if { [ info exist PageSetup(PageViewLeftMargin)] } {
	$cv delete $PageSetup(PageViewLeftMargin)
	$cv dtag left
    }
    set PageSetup(PageViewLeftMargin) [ $cv create line [ expr $x1 + $ml] 0 \
	    [ expr $x1 + $ml] $PageSetup(PageCanvasHeight) -fill grey60]
    $cv addtag left withtag $PageSetup(PageViewLeftMargin)
    $cv bind left <1> "PageitemStartDrag $cv %x %y left"
    $cv bind left <Any-Enter> "$cv itemconfig current -fill red"
    $cv bind left <Any-Leave> "$cv itemconfig current -fill grey60"
    $cv bind left <ButtonRelease-1> "PageAcceptEntry Left"

    if { [ info exist PageSetup(PageViewRightMargin)] } {
	$cv delete $PageSetup(PageViewRightMargin)
	$cv dtag right
    }
    set PageSetup(PageViewRightMargin) [ $cv create line [ expr $x1 + $w - $mr] 0 \
	    [ expr $x1 + $w - $mr] $PageSetup(PageCanvasHeight) -fill grey60]
    $cv addtag right withtag $PageSetup(PageViewRightMargin)
    $cv bind right <1> "PageitemStartDrag $cv %x %y right"
    $cv bind right <Any-Enter> "$cv itemconfig current -fill red"
    $cv bind right <Any-Leave> "$cv itemconfig current -fill grey60"
    $cv bind right <ButtonRelease-1> "PageAcceptEntry Right"

    if { [ info exist PageSetup(PageViewTopMargin)] } {
	$cv delete $PageSetup(PageViewTopMargin)
	$cv dtag top
    }
    set PageSetup(PageViewTopMargin) [ $cv create line 0 [ expr $y1 + $mt]\
	    $PageSetup(PageCanvasWidth) [ expr $y1 + $mt] -fill grey60]
    $cv addtag top withtag $PageSetup(PageViewTopMargin)
    $cv bind top <1> "PageitemStartDrag $cv %x %y top"
    $cv bind top <Any-Enter> "$cv itemconfig current -fill red"
    $cv bind top <Any-Leave> "$cv itemconfig current -fill grey60"
    $cv bind top <ButtonRelease-1> "PageAcceptEntry Top"

    if { [ info exist PageSetup(PageViewBottomMargin)] } {
	$cv delete $PageSetup(PageViewBottomMargin)
	$cv dtag bottom
    }
    set PageSetup(PageViewBottomMargin) [ $cv create line 0  [ expr $y1 + $h - $mb] \
	    $PageSetup(PageCanvasWidth) [ expr $y1 + $h - $mb] -fill grey60]
    $cv addtag bottom withtag $PageSetup(PageViewBottomMargin)
    $cv bind bottom <1> "PageitemStartDrag $cv %x %y bottom"
    $cv bind bottom <Any-Enter> "$cv itemconfig current -fill red"
    $cv bind bottom <Any-Leave> "$cv itemconfig current -fill grey60"
    $cv bind bottom <ButtonRelease-1> "PageAcceptEntry Bottom"
}

proc PageDefaultMargins {} {
    global PageSetup

    if { $PageSetup(PageUnits) == "In"} {
	set PageSetup(EntryTop) [ expr $PageSetup(DefaultMarginTop) / $PageSetup(In2mm)]
	set PageSetup(EntryBottom) [ expr $PageSetup(DefaultMarginBottom) / $PageSetup(In2mm)]
	set PageSetup(EntryLeft) [ expr $PageSetup(DefaultMarginLeft) / $PageSetup(In2mm)]
	set PageSetup(EntryRight) [ expr $PageSetup(DefaultMarginRight) / $PageSetup(In2mm)]
    } else {
	set PageSetup(EntryTop) $PageSetup(DefaultMarginTop)
	set PageSetup(EntryBottom) $PageSetup(DefaultMarginBottom)
	set PageSetup(EntryLeft) $PageSetup(DefaultMarginLeft)
	set PageSetup(EntryRight) $PageSetup(DefaultMarginRight)
    }
}

proc PageChangeOrientation { name1 name2 op} {
    global PageSetup

    if { $PageSetup(PageOrientation) == [_ "Landscape"] } {
	# Landscape
	ChangeCenteredPage $PageSetup(PageCanvas) $PageSetup(PageHeightMM) $PageSetup(PageWidthMM)
	if { $PageSetup(PageUnits) == "In"} {
	    set PageSetup(EntryWidth) $PageSetup(PageHeightP)
	    set PageSetup(EntryHeight) $PageSetup(PageWidthP)
	} else {
	    set PageSetup(EntryWidth) $PageSetup(PageHeightMM)
	    set PageSetup(EntryHeight) $PageSetup(PageWidthMM)
	}
    } else {
	# Portrait
	ChangeCenteredPage $PageSetup(PageCanvas) $PageSetup(PageWidthMM) $PageSetup(PageHeightMM)
	if { $PageSetup(PageUnits) == "In"} {
	    set PageSetup(EntryWidth) $PageSetup(PageWidthP)
	    set PageSetup(EntryHeight) $PageSetup(PageHeightP)
	} else {
	    set PageSetup(EntryWidth) $PageSetup(PageWidthMM)
	    set PageSetup(EntryHeight) $PageSetup(PageHeightMM)
	}
    }

    if { $name1 != ""} {
	PageAcceptEntry Top
	PageAcceptEntry Bottom
	PageAcceptEntry Left
	PageAcceptEntry Right
    }

}

proc PageChangePulgadas { name1 name2 op} {
    global PageSetup

    set PageSetup(ChangingMarginEntries) 1
    if { $PageSetup(PageUnits) == "In"} {
	set PageSetup(EntryTop) [ expr $PageSetup(MarginTop) / $PageSetup(In2mm)]
	set PageSetup(EntryBottom) [ expr $PageSetup(MarginBottom) / $PageSetup(In2mm)]
	set PageSetup(EntryLeft) [ expr $PageSetup(MarginLeft) / $PageSetup(In2mm)]
	set PageSetup(EntryRight) [ expr $PageSetup(MarginRight) / $PageSetup(In2mm)]
	if { $PageSetup(PageOrientation) == [_ "Landscape"] } {
	    set PageSetup(EntryWidth) $PageSetup(PageHeightP)
	    set PageSetup(EntryHeight) $PageSetup(PageWidthP)
	} else {
	    set PageSetup(EntryWidth) $PageSetup(PageWidthP)
	    set PageSetup(EntryHeight) $PageSetup(PageHeightP)
	}
    } else {
	if { $PageSetup(PageOrientation) == [_ "Landscape"] } {
	    set PageSetup(EntryWidth) $PageSetup(PageHeightMM)
	    set PageSetup(EntryHeight) $PageSetup(PageWidthMM)
	} else {
	    set PageSetup(EntryWidth) $PageSetup(PageWidthMM)
	    set PageSetup(EntryHeight) $PageSetup(PageHeightMM)
	}
	set PageSetup(EntryTop) $PageSetup(MarginTop)
	set PageSetup(EntryBottom) $PageSetup(MarginBottom)
	set PageSetup(EntryLeft) $PageSetup(MarginLeft)
	set PageSetup(EntryRight) $PageSetup(MarginRight)
    }

    set PageSetup(ChangingMarginEntries) 0
}

proc PageSetupAccept { w} {
    global PageSetup

    set line "hardcopy options orientation"
    if { $PageSetup(PageOrientation) == [_ "Landscape"] } {
	lappend line landscape
    } else {
	lappend line portrait
    }

    lappend line resolution
    if { $PageSetup(PageResolution) != [_ "other"] } {
	lappend line $PageSetup(PageResolution)
    } else {
	if { $PageSetup(PageUserResolution) != "" } {
	    lappend line $PageSetup(PageUserResolution)
	} else {
	    lappend line [_ "Screen"]
	}
    }

    .central.s process $line escape
    set line "hardcopy options"

    set Wpage $PageSetup(PageWidthP)
    set Hpage $PageSetup(PageHeightP)
    if { $PageSetup(PageOrientation) == [_ "Landscape"] } {
	set Wimage [ expr $PageSetup(PageHeightP) - \
		( $PageSetup(MarginLeft) + $PageSetup(MarginRight)) / $PageSetup(In2mm)]
	set Himage [ expr $PageSetup(PageWidthP) - \
		( $PageSetup(MarginTop) + $PageSetup(MarginBottom)) / $PageSetup(In2mm)]
    } else {
	set Wimage [ expr $PageSetup(PageWidthP) - \
		( $PageSetup(MarginLeft) + $PageSetup(MarginRight)) / $PageSetup(In2mm)]
	set Himage [ expr $PageSetup(PageHeightP) - \
		( $PageSetup(MarginTop) + $PageSetup(MarginBottom)) / $PageSetup(In2mm)]
    }
    set Wmargin [ expr $PageSetup(MarginLeft) / $PageSetup(In2mm)]
    set Hmargin [ expr $PageSetup(MarginTop) / $PageSetup(In2mm)]

    lappend line width $Wpage height $Hpage wimage $Wimage himage $Himage \
	    wmargin $Wmargin hmargin $Hmargin escape

    .central.s process $line
    set val No
    if { $PageSetup(AutoCrop)} {
	set val Yes
    }
    .central.s process escape escape escape escape \
	    view hardcopy options autocrop $val \
	    escape

}

proc PageSetupPrint { w} {
    global PageSetup
    global tcl_platform
    
    PageSetupAccept $w

    lower $w

    if { $PageSetup(Destination) == "file" } {
	#WarnWin $PageSetup(FileType)
	if { $PageSetup(FileType) == "PSV" } {
	    .central.s process Hardcopy \
		    Options VectorialPS yes escape Hardcopy PS
	} elseif { $PageSetup(FileType) == "EPSV" } { 
	    .central.s process Hardcopy \
		    Options VectorialPS yes escape Hardcopy EPS
	} else {
	    .central.s process Hardcopy $PageSetup(FileType)
	}
    } 

    if { $PageSetup(Destination) == "printer" } {
	if { $tcl_platform(platform) == "windows" } {
	    .central.s process hardcopy print
	} else {
	    #WarnWin $PageSetup(Printer)
	    if { $PageSetup(Printer) == "User" } {
		#WarnWin "HardcopyToPrint {Hardcopy PS} \"lpr\" $w"
		##WarnWin "HardcopyToPrint {Hardcopy PS} \"lpr\" $w"
		HardcopyToPrint {Hardcopy PS} 
		#"lpr" $w
	    } else {
		#WarnWin "HardcopyToPrint {Hardcopy PS} \"lpr -P$PageSetup(Printer)\" $w"
		##WarnWin "HardcopyToPrint {Hardcopy PS} \"lpr -P$PageSetup(Printer)\" $w"
		HardcopyToPrint {Hardcopy PS} "lpr -P$PageSetup(Printer)" $w
		#HardcopyToPrint { Hardcopy PS } 
	    }
	}
    } 

    raise $w

}

proc PageSetupEnd { w} {
    global PageSetup

    trace vdelete PageSetup(PageOrientation) w PageChangeOrientation
    trace vdelete PageSetup(PageUnits) w PageChangePulgadas
    trace vdelete PageSetup(EntryTop) w PageChangeEntryTop
    trace vdelete PageSetup(EntryBottom) w PageChangeEntryBottom
    trace vdelete PageSetup(EntryLeft) w PageChangeEntryLeft
    trace vdelete PageSetup(EntryRight) w PageChangeEntryRight
    trace vdelete PageSetup(Destination) w PageChangeDestination
    trace vdelete PageSetup(PageResolution) w PageChangeResolution
    trace vdelete PageSetup(PageUserResolution) w PageVerifyInteger

    destroy $w
    #destroy .
}

proc PageResizeCanvas { width height} {
    global PageSetup

    if { $PageSetup(FirstTimeResize) } {
	set PageSetup(FirstTimeResize) 0
	return
    }
    set PageSetup(PageCanvasWidth) [ expr $width - 10]
    set PageSetup(PageCanvasHeight) [ expr $height - 10]

    if { $PageSetup(PageCanvasWidth) < 150} {
	set PageSetup(PageCanvasWidth) 150
    }
    if { $PageSetup(PageCanvasHeight) < 150} {
	set PageSetup(PageCanvasHeight) 150
    }

    $PageSetup(PageCanvas) conf -width $PageSetup(PageCanvasWidth)
    $PageSetup(PageCanvas) conf -height $PageSetup(PageCanvasHeight)

    PageChangeOrientation "" "" ""
}

proc PageChangeSize { i} {
    global PageSetup

    set PageSetup(PageSize) $PageSetup(PageSizeList,$i,Name)
    set PageSetup(PageWidthMM) $PageSetup(PageSizeList,$i,WidthMM)
    set PageSetup(PageHeightMM) $PageSetup(PageSizeList,$i,HeightMM)
    set PageSetup(PageWidthP) $PageSetup(PageSizeList,$i,WidthP)
    set PageSetup(PageHeightP) $PageSetup(PageSizeList,$i,HeightP)

    PageChangeOrientation "" "" ""
}

proc PageReadSizes { menu file} {
    global PageSetup

    set PageSetup(NumPageSizes) 0
    set PageSetup(FactorCanvas) 200
    set res [ catch { set fi [ open $file]} ]
    if { !$res} {
	while { ![ eof $fi] } {
	    gets $fi line
	    if { ( [ string index $line 0] != "#") && ( [ llength $line] >= 3)} {
		if { [ regexp ^(\[0-9\]*.?\[0-9\]+)x(\[0-9\]*.?\[0-9\]+)$ [ lindex $line 1] \
			sizeP wP hP] && \
			[ regexp ^(\[0-9\]*.?\[0-9\]+)x(\[0-9\]*.?\[0-9\]+)$ [ lindex $line 2] \
			sizeMM wMM hMM] } {
		    # primer campo: nombre
		    set PageSetup(PageSizeList,$PageSetup(NumPageSizes),Name) [ lindex $line 0]
		    # segundo campo: tamanyo en pulgadas
		    set PageSetup(PageSizeList,$PageSetup(NumPageSizes),WidthP) $wP
		    set PageSetup(PageSizeList,$PageSetup(NumPageSizes),HeightP) $hP
		    # tercer campo: tamanyo en milimetros
		    set PageSetup(PageSizeList,$PageSetup(NumPageSizes),WidthMM) $wMM
		    set PageSetup(PageSizeList,$PageSetup(NumPageSizes),HeightMM) $hMM
		    incr PageSetup(NumPageSizes)
		    
		    if { $wMM > $PageSetup(FactorCanvas) } {
			set PageSetup(FactorCanvas) $wMM
		    }
		    if { $hMM > $PageSetup(FactorCanvas) } {
			set PageSetup(FactorCanvas) $hMM
		    }
		}
	    }
	}
	close $fi
    } else {
	# Tambien se puede tener esta informacion en el archivo $GIDDEFAULTTCL/PageSizes.txt
	# 1 inch = 25.3955838340800426
	set pagelist { \
		"A4      8.27x11.69  210x297" "A3      11.69x16.54 297x420" \
		"A5      5.83x8.27   148x210" "B4      9.84x13.9   250x353" \
		"B5      6.93x9.84   176x250" "Letter  8.5x11      216x280" \
		"Legal   8.5x14      216x356" "Tabloid 11.01x16.98 280x431"  }
	#B6      4.92x6.93   125x176
	#C4      9.02x12.76  229x324
	#C5      6.38x9.02   162x229
	#C6      4.49x6.38   114x162
	#C65     4.49x8.94   114x227
	#DL      4.33x8.66   110x220
	foreach line $pagelist {
	    if { ( [ string index $line 0] != "#") && ( [ llength $line] >= 3)} {
		if { [ regexp ^(\[0-9\]*.?\[0-9\]+)x(\[0-9\]*.?\[0-9\]+)$ [ lindex $line 1] \
			sizeP wP hP] && \
			[ regexp ^(\[0-9\]*.?\[0-9\]+)x(\[0-9\]*.?\[0-9\]+)$ [ lindex $line 2] \
			sizeMM wMM hMM] } {
		    # primer campo: nombre
		    set PageSetup(PageSizeList,$PageSetup(NumPageSizes),Name) [ lindex $line 0]
		    # segundo campo: tamanyo en pulgadas
		    set PageSetup(PageSizeList,$PageSetup(NumPageSizes),WidthP) $wP
		    set PageSetup(PageSizeList,$PageSetup(NumPageSizes),HeightP) $hP
		    # tercer campo: tamanyo en milimetros
		    set PageSetup(PageSizeList,$PageSetup(NumPageSizes),WidthMM) $wMM
		    set PageSetup(PageSizeList,$PageSetup(NumPageSizes),HeightMM) $hMM
		    incr PageSetup(NumPageSizes)
		    
		    if { $wMM > $PageSetup(FactorCanvas) } {
			set PageSetup(FactorCanvas) $wMM
		    }
		    if { $hMM > $PageSetup(FactorCanvas) } {
			set PageSetup(FactorCanvas) $hMM
		    }
		}
	    }
	}
    }
	
    set PageSetup(FactorCanvas) [ expr 1.0 / ( $PageSetup(FactorCanvas) + 16)]
	
    $menu del 0 end

    set found 0

    for { set i 0} { $i < $PageSetup(NumPageSizes)} { incr i} {
	$menu add command -label $PageSetup(PageSizeList,$i,Name) \
		-command "PageChangeSize $i"
	if { ( ( $PageSetup(PageSizeList,$i,WidthMM) == $PageSetup(DefaultPageWidth)) && \
		( $PageSetup(PageSizeList,$i,HeightMM) == $PageSetup(DefaultPageHeight))) || \
		( ( $PageSetup(PageSizeList,$i,WidthMM) == $PageSetup(DefaultPageHeight)) && \
		( $PageSetup(PageSizeList,$i,HeightMM) == $PageSetup(DefaultPageWidth)))  } {
	    set found $i
	}
    }
    if { $PageSetup(NumPageSizes) > 0} {
	PageChangeSize $found
    }
}


proc PageChangeEntryTop { name1 name2 op} {
    global PageSetup

    if { $PageSetup(ChangingMarginEntries) == 2} { return}

    set cual Top
    
    set nada 0
    if { $PageSetup(Entry$cual) == ""} {
	set nada 1
	set PageSetup(Entry$cual) 0
    }

    if { !$PageSetup(ChangingMarginEntries) } {

	if { $PageSetup(Entry$cual) < 0} {
	    #WarnWinText "PageSetup(Entry$cual) $PageSetup(Entry$cual) < 0"
	    bell
	    set PageSetup(Entry$cual) 0
	}

	if { $PageSetup(PageUnits) == "In"} {
	    set min [ expr $PageSetup(DefaultHeight) / $PageSetup(In2mm)]
	} else {
	    set min $PageSetup(DefaultHeight)
	}
	#WarnWinText "min = $min"
	set queda [ expr $PageSetup(EntryHeight) - $PageSetup(EntryBottom) - $min]
	#WarnWinText " $PageSetup(EntryHeight) - $PageSetup(EntryBottom) - $min"
	#WarnWinText $queda
	if { $queda < 0} {
	    set queda 0
	}

	if { $PageSetup(Entry$cual) > $queda} {
	    #WarnWinText "PageSetup(Entry$cual) $PageSetup(Entry$cual) > $queda"
	    bell
	    set PageSetup(Entry$cual) $queda
	}
    }	

    if { $PageSetup(PageUnits) == "In"} {
	set PageSetup(Margin$cual) [ expr $PageSetup(Entry$cual) * $PageSetup(In2mm)]
    } else {
	set PageSetup(Margin$cual) $PageSetup(Entry$cual)
    }

    if { [ string length $PageSetup(Margin$cual)] >= 6 } {
	set PageSetup(Margin$cual) [ format %.4g $PageSetup(Margin$cual)]
    }
    if { [ string length $PageSetup(Entry$cual)] >= 6 } {
	set PageSetup(Entry$cual) [ format %.4g $PageSetup(Entry$cual)]
    }

    PageChangeOrientation "" "" ""

    if { $nada} {
	set PageSetup(Entry$cual) ""
    }

}

proc PageChangeEntryBottom { name1 name2 op} {
    global PageSetup

    if { $PageSetup(ChangingMarginEntries) == 2} { return}

    set cual Bottom

    set nada 0
    if { $PageSetup(Entry$cual) == ""} {
	set nada 1
	set PageSetup(Entry$cual) 0
    }

    if { !$PageSetup(ChangingMarginEntries) } {

	if { $PageSetup(Entry$cual) < 0} {
	    bell
	    set PageSetup(Entry$cual) 0
	}

	if { $PageSetup(PageUnits) == "In"} {
	    set min [ expr $PageSetup(DefaultHeight) / $PageSetup(In2mm)]
	} else {
	    set min $PageSetup(DefaultHeight)
	}
	set queda [ expr $PageSetup(EntryHeight) - $PageSetup(EntryTop) - $min]
	if { $queda < 0} {
	    set queda 0
	}

	if { $PageSetup(Entry$cual) > $queda} {
	    bell
	    set PageSetup(Entry$cual) $queda
	}
    }

    if { $PageSetup(PageUnits) == "In"} {
	set PageSetup(Margin$cual) [ expr $PageSetup(Entry$cual) * $PageSetup(In2mm)]
    } else {
	set PageSetup(Margin$cual) $PageSetup(Entry$cual)
    }

    if { [ string length $PageSetup(Margin$cual)] >= 6 } {
	set PageSetup(Margin$cual) [ format %.4g $PageSetup(Margin$cual)]
    }
    if { [ string length $PageSetup(Entry$cual)] >= 6 } {
	set PageSetup(Entry$cual) [ format %.4g $PageSetup(Entry$cual)]
    }

    PageChangeOrientation "" "" ""

    if { $nada} {
	set PageSetup(Entry$cual) ""
    }
}

proc PageChangeEntryLeft { name1 name2 op} {
    global PageSetup
    if { $PageSetup(ChangingMarginEntries) == 2} { return}
    set cual Left

    set nada 0
    if { $PageSetup(Entry$cual) == ""} {
	set nada 1
	set PageSetup(Entry$cual) 0
    }

    if { !$PageSetup(ChangingMarginEntries) } {

	if { $PageSetup(Entry$cual) < 0} {
	    bell
	    set PageSetup(Entry$cual) 0
	}

	if { $PageSetup(PageUnits) == "In"} {
	    set min [ expr $PageSetup(DefaultWidth) / $PageSetup(In2mm)]
	} else {
	    set min $PageSetup(DefaultWidth)
	}
	set queda [ expr $PageSetup(EntryWidth) - $PageSetup(EntryRight) - $min]
	if { $queda < 0} {
	    set queda 0
	}
	
	if { $PageSetup(Entry$cual) > $queda} {
	    bell
	    set PageSetup(Entry$cual) $queda
	}
    }

    if { $PageSetup(PageUnits) == "In"} {
	set PageSetup(Margin$cual) [ expr $PageSetup(Entry$cual) * $PageSetup(In2mm)]
    } else {
	set PageSetup(Margin$cual) $PageSetup(Entry$cual)
    }

    if { [ string length $PageSetup(Margin$cual)] >= 6 } {
	set PageSetup(Margin$cual) [ format %.4g $PageSetup(Margin$cual)]
    }
    if { [ string length $PageSetup(Entry$cual)] >= 6 } {
	set PageSetup(Entry$cual) [ format %.4g $PageSetup(Entry$cual)]
    }

    PageChangeOrientation "" "" ""

    if { $nada} {
	set PageSetup(Entry$cual) ""
    }
}

proc PageChangeEntryRight { name1 name2 op} {
    global PageSetup

    if { $PageSetup(ChangingMarginEntries) == 2} { return}

    set cual Right

    set nada 0
    if { $PageSetup(Entry$cual) == ""} {
	set nada 1
	set PageSetup(Entry$cual) 0
    }

    if { !$PageSetup(ChangingMarginEntries) } {

	if { $PageSetup(Entry$cual) < 0} {
	    bell
	    set PageSetup(Entry$cual) 0
	}

	if { $PageSetup(PageUnits) == "In"} {
	    set min [ expr $PageSetup(DefaultWidth) / $PageSetup(In2mm)]
	} else {
	    set min $PageSetup(DefaultWidth)
	}
	set queda [ expr $PageSetup(EntryWidth) - $PageSetup(EntryLeft) - $min]
	if { $queda < 0} {
	    set queda 0
	}

	if { $PageSetup(Entry$cual) > $queda} {
	    bell
	    set PageSetup(Entry$cual) $queda
	}
    }

    if { $PageSetup(PageUnits) == "In"} {
	set PageSetup(Margin$cual) [ expr $PageSetup(Entry$cual) * $PageSetup(In2mm)]
    } else {
	set PageSetup(Margin$cual) $PageSetup(Entry$cual)
    }

    if { [ string length $PageSetup(Margin$cual)] >= 6 } {
	set PageSetup(Margin$cual) [ format %.4g $PageSetup(Margin$cual)]
    }
    if { [ string length $PageSetup(Entry$cual)] >= 6 } {
	set PageSetup(Entry$cual) [ format %.4g $PageSetup(Entry$cual)]
    }

    PageChangeOrientation "" "" ""

    if { $nada} {
	set PageSetup(Entry$cual) ""
    }
}

proc PageChangeDestination { name1 name2 op} {
    global PageSetup

    set w $PageSetup(DestinationFrame)

    if { $PageSetup(Destination) == "file" } {
	$w.cb2_mb conf -state disabled
	$w.cb1_mb conf -state normal
    }
    if { $PageSetup(Destination) == "printer" } {
	$w.cb2_mb conf -state normal
	$w.cb1_mb conf -state disabled
    }
}

proc PageChangeResolution { name1 name2 op} {
    global PageSetup

    set w $PageSetup(DestinationFrame)
    if { $PageSetup(PageResolution) == [_ "other"]} {
	$w.fp.f2.e1 conf -bd 1 -relief sunken -state normal -foreground black
	focus $w.fp.f2.e1
    } else {
	$w.fp.f2.e1 conf -bd 0 -state disabled -foreground grey60
    }
}

proc PageVerifyInteger { name1 name2 op} {
    global PageSetup

    if { ![ regexp {^[0-9]*$} $PageSetup(PageUserResolution)] } {
	regexp {[0-9]*} $PageSetup(PageUserResolution) dummy
	bell
	set w $PageSetup(DestinationFrame)
	$w.fp.f2.e1 del 0 end
	$w.fp.f2.e1 ins end $dummy
	set PageSetup(PageUserResolution) $dummy
    }
}

proc PageLookForPrinters {} {
    global tcl_platform

    if { $tcl_platform(platform) == "windows" } {
	return "default"
    }
    
    set fi [ open /etc/printcap]
    
    set lst ""

    gets $fi line
    while { "[ string index $line [ expr [ string length $line] - 1]]" == "\\"} {
	gets $fi line_cont
	set dum [ string range $line 0 [ expr [ string length $line] - 2]]$line_cont
	set line $dum
    }

    while { ![ eof $fi]} {
	set line [ string trim $line]
	if { ( $line != "") && ( [ string first "#" $line] != 0)} {
	    set pos [ string first "|" $line]
	    if { $pos != -1} {
		set line [ string range $line 0 [ expr $pos - 1]]
	    } else {
		set pos [ string first ":" $line]
		if { $pos != -1} {
		    set line [ string range $line 0 [ expr $pos - 1]]
		}
	    }
	    if { $line != ""} {
		lappend lst $line
	    }
	}
	gets $fi line
	while { "[ string index $line [ expr [ string length $line] - 1]]" == "\\"} {
	    gets $fi line_cont
	    set dum [ string range $line 0 [ expr [ string length $line] - 2]]$line_cont
	    set line $dum
	}
    }

    lappend lst User

    return $lst
}


proc PageSetup { { w .wPageSetup}} {

    global PageSetup GIDDEFAULT
    global GidPriv


    #source stuff/scripts/combo.tcl

    InitWindow $w [_ "Page Setup"] PageSetupWindowGeom 
#PageSetup

    
    # if { [ winfo exist $w]} {
    #    PageSetupEnd $w
    # }

    #frame .f -bd 4

    #toplevel $w
    #wm title $w "Page Setup"

    frame $w.f -bd 1 -relief sunken

    frame $w.f.ft -bd 0
    label $w.f.ft.l -text [_ "Top:"]
    entry $w.f.ft.e -textvariable PageSetup(EntryTop) -relief sunken -width 6 -bd 1
    grid $w.f.ft.l $w.f.ft.e -padx 2 -pady 0 -sticky new

    frame $w.f.fl -bd 0
    label $w.f.fl.l -text [_ "Left:"]
    entry $w.f.fl.e -textvariable PageSetup(EntryLeft) -relief sunken -width 6 -bd 1
    grid $w.f.fl.l -padx 2 -pady 0 -sticky ws
    grid $w.f.fl.e -padx 2 -pady 0 -sticky nw

    frame $w.f.fc -bd 2 -relief groove
    set PageSetup(PageCanvas) $w.f.fc.cv
    set PageSetup(PageCanvasWidth) 300
    set PageSetup(PageCanvasHeight) 300
    canvas $w.f.fc.cv -width $PageSetup(PageCanvasWidth) -height $PageSetup(PageCanvasHeight) -bd 0
    grid $w.f.fc.cv -sticky news

    frame $w.f.fr -bd 0
    label $w.f.fr.l -text [_ "Right:"]
    entry $w.f.fr.e -textvariable PageSetup(EntryRight) -relief sunken -width 6 -bd 1
    grid $w.f.fr.l -padx 2 -pady 0 -sticky ws
    grid $w.f.fr.e -padx 2 -pady 0 -sticky ne

    frame $w.f.fb -bd 0
    label $w.f.fb.l -text [_ "Bottom:"]
    entry $w.f.fb.e -textvariable PageSetup(EntryBottom) -relief sunken -width 6 -bd 1
    grid $w.f.fb.l $w.f.fb.e -padx 2 -pady 0 -sticky ens

    button $w.f.bdef -text [_ "Reset"] -bd 1 -command "PageDefaultMargins" -padx 3

    grid $w.f.ft -row 0 -column 0 -columnspan 3 -sticky wen
    grid $w.f.fl -row 1 -column 0 -sticky nsw
    grid $w.f.fc -row 1 -column 1 -sticky news
    grid $w.f.fr -row 1 -column 2 -sticky nse
    grid $w.f.fb -row 2 -column 0 -columnspan 3 -sticky wes

    grid $w.f.bdef -row 2 -column 2 -sticky es
    #grid columnconfigure $w.f 0 -weight 1
    grid columnconfigure $w.f 1 -weight 1
    #grid columnconfigure $w.f 2 -weight 1
    grid rowconfigure $w.f 0 -weight 0
    grid rowconfigure $w.f 1 -weight 1
    grid rowconfigure $w.f 2 -weight 0

    set PageSetup(FirstTimeResize) 1
    bind $w.f.fc <Configure> "PageResizeCanvas %w %h"

    #bind $w.f.fc <1> "PageitemStartDrag $w.f.fc %x %y"
    bind $w.f.fc.cv <B1-Motion> "PageitemDrag $w.f.fc.cv %x %y"
    set PageSetup(lastX) 0
    set PageSetup(lastY) 0

    frame $w.fsize -bd 1 -relief sunken

    frame $w.fsize.fp -bd 0
    frame $w.fsize.fp.f1 -bd 0
    set menu [ tk_optionMenu $w.fsize.fp.f1.mb PageSetup(PageSize) "" ]
    $w.fsize.fp.f1.mb conf -bd 1 -highlightthickness 1 -width 5
    $menu conf -bd 1 -activeborderwidth 1
   
    set menu [tk_optionMenuRAM $w.fsize.fp.f1.mbo PageSetup(PageOrientation) \
	    [list [_ "Portrait"] [_ "Landscape"]] \
	    {Portrait Landscape}]
    $w.fsize.fp.f1.mbo conf -bd 1 -highlightthickness 1 -width 9
    $menu conf -bd 1 -activeborderwidth 1

    grid $w.fsize.fp.f1.mb $w.fsize.fp.f1.mbo -ipadx 1 -sticky w

    #frame $w.fsize.fres -bd 0
    frame $w.fsize.fp.f2 -bd 0

    set menu [ tk_optionMenuRAM $w.fsize.fp.f2.mres PageSetup(PageResolution) \
	    [list [_ "Screen"] "150" "300" "600" [_ "other"]] \
	    { Screen 150 300 600 other}]
    $w.fsize.fp.f2.mres conf -bd 1 -highlightthickness 1 -width 5
    $menu conf -bd 1 -activeborderwidth 1

    set PageSetup(PageUserResolution) ""
    entry $w.fsize.fp.f2.e1 -bd 0 -width 4 -textvariable PageSetup(PageUserResolution) \
	    -state disabled -highlightthickness 0
    label $w.fsize.fp.f2.l -text [_ "dpi"]
    
    grid $w.fsize.fp.f2.mres $w.fsize.fp.f2.e1 $w.fsize.fp.f2.l -ipadx 1 -sticky e

    grid $w.fsize.fp.f1 $w.fsize.fp.f2 -ipadx 1 -sticky we

    frame $w.fsize.funi -bd 0

    checkbutton $w.fsize.funi.cb -text [_ "Auto crop image   "] -variable PageSetup(AutoCrop)

    entry $w.fsize.funi.e1 -bd 0 -width 6 -textvariable PageSetup(EntryWidth) \
	    -state disabled -highlightthickness 0
    label $w.fsize.funi.l -text x
    entry $w.fsize.funi.e2 -bd 0 -width 6 -textvariable PageSetup(EntryHeight) \
	    -state disabled -highlightthickness 0

    set menu [ tk_optionMenuRAM $w.fsize.funi.mb PageSetup(PageUnits)\
	    [list mm [_ "In"]] \
	    {mm In}]
    $w.fsize.funi.mb conf -bd 1 -highlightthickness 1 -width 3
    $menu conf -bd 1 -activeborderwidth 1

    grid $w.fsize.funi.cb $w.fsize.funi.e1 $w.fsize.funi.l $w.fsize.funi.e2 \
	    $w.fsize.funi.mb -ipadx 1 -sticky e

    #grid $w.fsize.mb -sticky w -row 0 -column 0 -columnspan 1
    #grid $w.fsize.mbo -sticky w -row 0 -column 1 -columnspan 1
    grid $w.fsize.fp -sticky w -row 0 -column 0 -columnspan 4
    #grid $w.fsize.fres -sticky e -row 0 -column 2 -columnspan 2
    grid $w.fsize.funi -sticky e -columnspan 4
    #grid $w.fsize.fres -sticky w -row 1 -column 0 -columnspan 2
    #grid $w.fsize.funi -sticky e -row 1 -column 2 -columnspan 2

    radiobutton $w.fsize.cb1 -text [_ "To File"] -variable PageSetup(Destination) -value file
    set menu [ tk_optionMenu $w.fsize.cb1_mb PageSetup(FileType) PS PSV EPS EPSV BMP GIF PPM JPEG PNG TIFF VRML]
    $w.fsize.cb1_mb conf -bd 1 -highlightthickness 1 -width 4
    $menu conf -bd 1 -activeborderwidth 1

    radiobutton $w.fsize.cb2 -text [_ "To Printer"] -variable PageSetup(Destination) -value printer
    set printers [ PageLookForPrinters]    
    set menu [ eval [ join "tk_optionMenu $w.fsize.cb2_mb PageSetup(Printer) $printers"] ]
    $w.fsize.cb2_mb conf -bd 1 -highlightthickness 1 -width 5
    $menu conf -bd 1 -activeborderwidth 1

    set PageSetup(DestinationFrame) $w.fsize
    set PageSetup(Destination) file
    PageChangeDestination "" "" ""

    grid $w.fsize.cb1 $w.fsize.cb1_mb $w.fsize.cb2 $w.fsize.cb2_mb -sticky ew

    #grid $w.fsize.mbo $w.fsize.mb $w.fsize.funi -sticky we

    frame $w.fb -bd 0
    button $w.fb.ba -text [_ "Set Page"] -command "PageSetupAccept $w" -bd 1
    button $w.fb.bi -text [_ "Print"] -command "PageSetupPrint $w" -bd 1
    button $w.fb.bc -text [_ "Close"] -command "PageSetupEnd $w" -bd 1
    grid $w.fb.ba  -sticky e
    grid $w.fb.bi -sticky e 
    grid $w.fb.bc -sticky e
    
    grid $w.f -pady 2 -ipady 2 -sticky wens -padx 3 -pady 2 -columnspan 2
    grid $w.fsize -sticky ew -padx 3
    grid $w.fb -sticky sew -pady 2 -ipady 2 -sticky s -padx 2 -pady 2 -row 1 -column 1
    grid columnconfigure $w 0 -weight 1
    grid rowconfigure $w 0 -weight 1

    set PageSetup(PageOrientation) [_ "Landscape"]
    set PageSetup(PageUnits) mm

    set PageSetup(In2mm)  25.4
    #25.3955838340800426

    set PageSetup(GiDImage) [ image create bitmap -file $GIDDEFAULT/bitmaps/GiDicono.xbm -foreground black -background yellow]


    PageFillDefaults
    # set PageSetup(DefaultMarginTop) 10
    # set PageSetup(DefaultMarginBottom) 10
    # set PageSetup(DefaultMarginLeft) 10
    # set PageSetup(DefaultMarginRight) 10
    # set PageSetup(DefaultWidth) 20
    # set PageSetup(DefaultHeight) 20
    # set PageSetup(DefaultPageWidth) 20
    # set PageSetup(DefaultPageHeight) 20

    set PageSetup(MarginTop) $PageSetup(DefaultMarginTop)
    set PageSetup(MarginBottom) $PageSetup(DefaultMarginBottom)
    set PageSetup(MarginLeft) $PageSetup(DefaultMarginLeft)
    set PageSetup(MarginRight) $PageSetup(DefaultMarginRight)
    PageReadSizes $w.fsize.fp.f1.mb.menu $GIDDEFAULT/scripts/PageSizes.txt
    PageChangePulgadas "" "" ""

    set PageSetup(ChangingMarginEntries) 0
    trace variable PageSetup(PageOrientation) w PageChangeOrientation
    trace variable PageSetup(PageUnits) w PageChangePulgadas
    trace variable PageSetup(EntryTop) w PageChangeEntryTop
    trace variable PageSetup(EntryBottom) w PageChangeEntryBottom
    trace variable PageSetup(EntryLeft) w PageChangeEntryLeft
    trace variable PageSetup(EntryRight) w PageChangeEntryRight
    trace variable PageSetup(Destination) w PageChangeDestination
    trace variable PageSetup(PageResolution) w PageChangeResolution
    trace variable PageSetup(PageUserResolution) w PageVerifyInteger
}
