Pad++ Programmer's Guide
-units
configuration option). All coordinates refer to the Pad++ surface and are independent of the current view. Therefore if the view happens to have a magnification of 2.0, and you create an item that is 50 pixels wide, it will appear 100 pixels wide for this specific view. Larger y-coordinates refer to points higher on the screen; larger x-coordinates refer to points farther to the right. Notice that the y coordinate is inverted as compared to the Tk canvas.
The following code draws a simple ruler:
pad .pad -units inches pack .pad .pad create rectangle 0 0 5 1 -fill white -penwidth .05 for {set x 0} {$x <= 5} {set x [expr $x + 0.125]} { if {[expr int($x) == $x]} { # Draw inch markers .pad create line $x .5 $x 1 -penwidth .04 } elseif {[expr int($x / .5) * .5 == $x]} { # Draw half-inch markers .pad create line $x .65 $x 1 -penwidth .04 } elseif {[expr int($x / .25) * .25 == $x]} { # Draw quarter-inch markers .pad create line $x .8 $x 1 -penwidth .04 } else { # Draw eighth-inch markers .pad create line $x .9 $x 1 -penwidth .04 } } .pad config -units pixels ;# Return to default units
Suppose you wanted to create a layout of nested boxes. These coordinates wouldn't be quite as easy to compute. But we can use relative coordinate frames to do this. Pad++ maintains a stack of coordinate frames. Each coordinate frame is a bounding box on the Pad++ surface. All coordinates are specified as a unit square within this coordinate frame, i.e., (0, 0)-(1, 1). That is, when a coordinate frame is specified, coordinates are no longer absolute units, and instead, are relative to the specific frame. Coordinate frames may be specified by an item, or as any bounding box. Note that pen width and minsize and maxsize are also relative to the coordinate frame. In these cases, a value of 1 refers to the average of the width and height of the frame.
This example draws a box with four boxes inside of it, and four boxes inside of each of those, and so on, four levels deep. (Note that from now on, the examples assume a pad widget called .pad has already been created with pixels as the current units.)
proc draw_a_box {x1 y1 x2 y2 level} { set id [.pad create rectangle $x1 $y1 $x2 $y2 -penwidth .01] puts $id .pad pushcoordframe $id draw_nested_boxes [expr $level + 1] .pad popcoordframe } proc draw_nested_boxes {level} { if {$level >= 5} {return} draw_a_box .1 .1 .45 .45 $level ;# Draw lower-left box draw_a_box .55 .1 .9 .45 $level ;# Draw lower-right box draw_a_box .1 .55 .45 .9 $level ;# Draw upper-left box draw_a_box .55 .55 .9 .9 $level ;# Draw upper-right box } draw_a_box 0 0 300 300 1See the
pushcoordframe
, popcoordframe
, and resetcoordframe
commands for more information.
Copyright Computer Science Department, The University of New Mexico