📄 canvasprintbox.itk
字号:
## CanvasPrintBox v1.5# ----------------------------------------------------------------------# Implements a print box for printing the contents of a canvas widget# to a printer or a file. It is possible to specify page orientation, the# number of pages to print the image on and if the output should be# stretched to fit the page.# # CanvasPrintBox is a "super-widget" that can be used as an# element in ones own GUIs. It is used to print the contents# of a canvas (called the source hereafter) to a printer or a# file. Possible settings include: portrait and landscape orientation# of the output, stretching the output to fit the page while maintaining# a proper aspect-ratio and posterizing to enlarge the output to fit on# multiple pages. A stamp-sized copy of the source will be shown (called# the stamp hereafter) at all times to reflect the effect of changing# the settings will have on the output.## ----------------------------------------------------------------------# AUTHOR: Tako Schotanus EMAIL: Tako.Schotanus@bouw.tno.nl# ----------------------------------------------------------------------# Copyright (c) 1995 Tako Schotanus# ======================================================================# Permission is hereby granted, without written agreement and without# license or royalty fees, to use, copy, modify, and distribute this# software and its documentation for any purpose, provided that the# above copyright notice and the following two paragraphs appear in# all copies of this software.# # IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR# DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES # ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN # IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH # DAMAGE.## THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND # FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS# ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO# PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.# ======================================================================## Default resources.#option add *Canvasprintbox.filename "canvas.ps" widgetDefaultoption add *Canvasprintbox.hPageCnt 1 widgetDefaultoption add *Canvasprintbox.orient landscape widgetDefaultoption add *Canvasprintbox.output printer widgetDefaultoption add *Canvasprintbox.pageSize A4 widgetDefaultoption add *Canvasprintbox.posterize 0 widgetDefaultoption add *Canvasprintbox.printCmd lpr widgetDefaultoption add *Canvasprintbox.printRegion "" widgetDefaultoption add *Canvasprintbox.vPageCnt 1 widgetDefault## Usual options.#itk::usual Canvasprintbox { keep -background -cursor -textbackground -foreground}#<## CanvasPrintBox is a "super-widget" that can be used as an# element in ones own GUIs. It is used to print the contents# of a canvas (called the source hereafter) to a printer or a# file. Possible settings include: portrait and landscape orientation# of the output, stretching the output to fit the page while maintaining# a proper aspect-ratio and posterizing to enlarge the output to fit on# multiple pages. A stamp-sized copy of the source will be shown (called# the stamp hereafter) at all times to reflect the effect of changing# the settings will have on the output.##>itcl::class iwidgets::Canvasprintbox { inherit itk::Widget # # Holds the current state for all check- and radiobuttons. # itk_option define -filename filename FileName "canvas.ps" itk_option define -hpagecnt hPageCnt PageCnt 1 itk_option define -orient orient Orient "landscape" itk_option define -output output Output "printer" itk_option define -pagesize pageSize PageSize "A4" itk_option define -posterize posterize Posterize 0 itk_option define -printcmd printCmd PrintCmd "" itk_option define -printregion printRegion PrintRegion "" itk_option define -stretch stretch Stretch 0 itk_option define -vpagecnt vPageCnt PageCnt 1 constructor {args} {} destructor {} # --------------------------------------------------------------- # PUBLIC #---------------------------------------------------------------- public { method getoutput {} method print {} method refresh {} method setcanvas {canv} method stop {} } # --------------------------------------------------------------- # PROTECTED #---------------------------------------------------------------- protected { # # Just holds the names of some widgets/objects. "win" is used to # determine if the object is fully constructed and initialized. # variable win "" variable canvw "" # # The canvas we want to print. # variable canvas "" # # Boolean indicating if the attribute "orient" is set # to landscape or not. # variable rotate 1 # # Holds the configure options that were used to create this object. # variable init_opts "" # # The following attributes hold a list of lines that are # currently drawn on the "stamp" to show how the page(s) is/are # oriented. The first holds the vertical dividing lines and the # second the horizontal ones. # variable hlines "" variable vlines "" # # Updating is set when the thumbnail is being drawn. Settings # this to 0 while drawing is still busy will terminate the # proces. # Restart_update can be set to 1 when the thumbnail is being # drawn to force a redraw. # variable _reposition "" variable _update_attr_id "" method _calc_poster_size {} method _calc_print_region {} method _calc_print_scale {} method _mapEventHandler {} method _update_attr {{when later}} method _update_canvas {{when later}} common _globVar proc ezPaperInfo {size {attr ""} \ {orient "portrait"} {window ""}} {} }}## Provide a lowercased access method for the Canvasprintbox class.# proc ::iwidgets::canvasprintbox {args} { uplevel ::iwidgets::Canvasprintbox $args}# ------------------------------------------------------------------# OPTIONS# ------------------------------------------------------------------#<# A list of four coordinates specifying which part of the canvas to print.# An empty list means that the canvas' entire scrollregion should be# printed. Any change to this attribute will automatically update the "stamp".# Defaults to an empty list.#>itcl::configbody iwidgets::Canvasprintbox::printregion { if {$itk_option(-printregion) != "" && [llength $itk_option(-printregion)] != 4} { error {bad option "printregion": should contain 4 coordinates} } _update_canvas}#<# Specifies where the postscript output should go: to the printer# or to a file. Can take on the values "printer" or "file".# The corresponding entry-widget will reflect the contents of# either the printcmd attribute or the filename attribute.#>itcl::configbody iwidgets::Canvasprintbox::output { switch $itk_option(-output) { file - printer { set _globVar($this,output) $itk_option(-output) } default { error {bad output option \"$itk_option(-output)\":\ should be file or printer} } } _update_attr}#<# The command to execute when printing the postscript output.# The command will get the postscript directed to its standard# input. (Only when output is set to "printer")#>itcl::configbody iwidgets::Canvasprintbox::printcmd { set _globVar($this,printeref) $itk_option(-printcmd) _update_attr}#<# The file to write the postscript output to (Only when output# is set to "file"). If posterizing is turned on and hpagecnt# and/or vpagecnt is more than 1, x.y is appended to the filename# where x is the horizontal page number and y the vertical page number.#>itcl::configbody iwidgets::Canvasprintbox::filename { set _globVar($this,fileef) $itk_option(-filename) _update_attr}#<# The pagesize the printer supports. Changes to this attribute# will be reflected immediately in the "stamp".#>itcl::configbody iwidgets::Canvasprintbox::pagesize { set opt [string tolower $itk_option(-pagesize)] set lst [string tolower [ezPaperInfo types]] if {[lsearch $lst $opt] == -1} { error "bad option \"pagesize\": should be one of: [ezPaperInfo types]" } $itk_component(paperom) select "*[string range $opt 1 end]" _update_canvas}#<# Determines the orientation of the output to the printer (or file).# It can take the value "portrait" or "landscape" (default). Changes# to this attribute will be reflected immediately in the "stamp".#>itcl::configbody iwidgets::Canvasprintbox::orient { switch $itk_option(-orient) { "portrait" - "landscape" { $itk_component(orientom) select $itk_option(-orient) _update_canvas } default { error "bad orient option \"$itk_option(-orient)\":\ should be portrait or landscape" } }}#<# Determines if the output should be stretched to fill the# page (as defined by the attribute pagesize) as large as# possible. The aspect-ratio of the output will be retained# and the output will never fall outside of the boundaries# of the page.#>itcl::configbody iwidgets::Canvasprintbox::stretch { if {$itk_option(-stretch) != 0 && $itk_option(-stretch) != 1} { error {bad option "stretch": should be a boolean} } set _globVar($this,stretchcb) $itk_option(-stretch) _update_attr}#<# Indicates if posterizing is turned on or not. Posterizing# the output means that it is possible to distribute the# output over more than one page. This way it is possible to# print a canvas/region which is larger than the specified# pagesize without stretching. If used in combination with# stretching it can be used to "blow up" the contents of a# canvas to as large as size as you want (See attributes:# hpagecnt end vpagecnt). Any change to this attribute will# automatically update the "stamp".#>itcl::configbody iwidgets::Canvasprintbox::posterize { if {$itk_option(-posterize) != "0" && $itk_option(-posterize) != "1"} { error "expected boolean but got \"$itk_option(-posterize)\"" } set _globVar($this,postercb) $itk_option(-posterize) _update_canvas}#<# Is used in combination with "posterize" to determine over# how many pages the output should be distributed. This# attribute specifies how many pages should be used horizontaly.# Any change to this attribute will automatically update the "stamp".#>itcl::configbody iwidgets::Canvasprintbox::hpagecnt { set _globVar($this,hpc) $itk_option(-hpagecnt) _update_canvas}#<# Is used in combination with "posterize" to determine over# how many pages the output should be distributed. This# attribute specifies how many pages should be used verticaly.# Any change to this attribute will automatically update the "stamp".#>itcl::configbody iwidgets::Canvasprintbox::vpagecnt { set _globVar($this,vpc) $itk_option(-vpagecnt) _update_canvas}# ------------------------------------------------------------------# CONSTRUCTOR# ------------------------------------------------------------------itcl::body iwidgets::Canvasprintbox::constructor {args} { set _globVar($this,output) printer set _globVar($this,printeref) "" set _globVar($this,fileef) "canvas.ps" set _globVar($this,hpc) 1 set _globVar($this,vpc) 1 set _globVar($this,postercb) 0 set _globVar($this,stretchcb) 0 itk_component add canvasframe { frame $itk_interior.f18 -bd 2 } itk_component add canvas { canvas $itk_component(canvasframe).c1 \ -bd 2 -relief sunken \ -scrollregion {0c 0c 10c 10c} \ -width 250 } pack $itk_component(canvas) -expand 1 -fill both itk_component add outputom { iwidgets::Labeledframe $itk_interior.outputom \ -labelpos nw \ -labeltext "Output to" } set cs [$itk_component(outputom) childsite] itk_component add printerrb { radiobutton $cs.printerrb \ -text Printer \ -variable [itcl::scope _globVar($this,output)] \ -anchor w \ -justify left \ -value printer \ -command [itcl::code $this _update_attr] } { usual rename -font -labelfont labelFont Font } itk_component add printeref { iwidgets::entryfield $cs.printeref \ -labeltext "command:" \ -state normal \ -labelpos w \ -textvariable [itcl::scope _globVar($this,printeref)] }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -