Overview of Item Types

Grid Items

Items of type grid arrange one or more items in rows and columns and treats them as a group. It is based on the Tk grid geometry manager and its behavior and Tcl syntax are very similar to it. In pad, all manipulations of a grid once it is created are affected through the grid sub-command. Note that rows and columns start from the top left corner of the grid (as in the Tk grid). The complete grid sub-command is described in this section.

Grids are created with widget commands of the following form:

pathName create grid [slaves...]

Grid creation is slightly different from creation of other pad objects. Instead of the normal command-line option-value pairs a list of slaves and their grid configuration can be specified (see the section below on sub-commands and slave configuration). Grids are special group objects and inherit much of the group functionality and support the "-divisible" option which can be set (using itemconfigure) once the grid is created:

-divisible [16] True if events go through a group to its members

The syntax of the grid sub-command is:

pathName grid slave [slave...] option value [option value...]
pathName grid command arg [arg...]

If the first argument of the grid command is a slave object then the remainder of the command line is processed in the same way as the grid configure command. The "-in" option can be used to add a slave to a grid. The following grid sub-commands are allowed:

$PAD grid arrange master

Forces arrangement of the given grid. Any pending layout request for the grid is removed. This can be useful when an application has done several grid configuration and wants them to take effect immediately. Normally, grid arrangement is done at "idle" times.


$PAD grid bbox master column row

The bounding box (in pixels) is returned for the space occupied by the grid position indicated by column and row. The return value consists of 4 integers. The first two are the pixel offset from the master window (x then y) of the top-left corner of the grid cell, and the second two are the width and height of the cell.


$PAD grid columnconfigure master index [-option value...]

Query or set the column properties of the index column of the geometry master, master. The valid options are -minsize and -weight. The -minsize option sets the minimum column size, in screen units, and the -weight option (a floating point value) sets the relative weight for apportioning any extra spaces among columns. If no value is specified, the current value is returned.


$PAD grid configure slave [slave ...] [options]

The arguments consist of one or more slaves followed by pairs of arguments that specify how to manage the slaves. The characters -, x and ^, can be specified instead of a window name to alter the default location of a slave, as described in the ``RELATIVE PLACEMENT'' section, below. If any of the slaves are already managed by the grid then any unspecified options for them retain their previous values rather than receiving default values. The following options are supported:

-column n

Insert the slave so that it occupies the nth column in the grid. Column numbers start with 0. If this option is not supplied, then the slave is arranged just to the right of previous slave specified on this call to grid, or column "0" if it is the first slave. For each x that immediately precedes the slave, the column position is incremented by one. Thus the x represents a blank column for this row in the grid.

-columnspan n

Insert the slave so that it occupies n columns in the grid. The default is one column, unless the slave is followed by a -, in which case the columnspan is incremented once for each immediately following -.

-in other

Insert the slave(s) in the grid object given by other (which must be an existing grid).

-padx amount

The amount specifies how much horizontal external padding to leave on each side of the slave(s). The amount defaults to 0.

-pady amount

The amount specifies how much vertical external padding to leave on the top and bottom of the slave(s). The amount defaults to 0.

-row n

Insert the slave so that it occupies the nth row in the grid. Row numbers start with 0. If this option is not supplied, then the slave is arranged on the same row as the previous slave specified on this call to grid, or the first unoccupied row if this is the first slave.

-rowspan n

Insert the slave so that it occupies n rows in the grid. The default is one row. If the next grid command contains ^ characters instead of slaves that line up with the columns of this slave, then the rowspan of this slave is extended by one.

-sticky style

If a slave's parcel is larger than its requested dimensions, this option may be used to position (or stretch) the slave within its cavity. Style is a string that contains zero or more of the characters n, s, e or w. The string can optionally contains spaces or commas, but they are ignored. Each letter refers to a side (north, south, east, or west) that the slave will "stick" to. If both n and s (or e and w) are specified, the slave will be stretched to fill the entire height (or width) of its cavity. The sticky option subsumes the combination of -anchor and -fill that is used by pack. The default is {}, which causes the slave to be centered in its cavity, at its requested size.


$PAD grid forget slave [slave ...]

Removes each of the slaves from their grid.


$PAD grid info slave

Returns a list whose elements are the current configuration state of the slave given by slave in the same option-value form that might be specified to grid configure. The first two elements of the list are ``-in master'' where master is the slave's master.


$PAD grid location master x y

Given x and y values in screen units relative to the master object, the column and row number at that x and y location is returned. For locations that are above or to the left of the grid, -1 is returned.


$PAD grid rowconfigure master index [-option value...]

Query or set the row properties of the index row of the geometry master, master. The valid options are -minsize and -weight. Minsize sets the minimum row size, in screen units, and weight sets the relative weight for apportioning any extra spaces among rows. If no value is specified, the current value is returned.


$PAD grid size master

Returns the size of the grid (in columns then rows) for master. The size is determined either by the slave occupying the largest row or column, or the largest column or row with a minsize or weight.


$PAD grid slaves master [-option value]

If no options are supplied, a list of all of the slaves in master are returned. Option can be either -row or -column which causes only the slaves in the row (or column) specified by value to be returned.

Relative Placement

The grid command contains a limited set of capabilities that permit layouts to be created without specifying the row and column information for each slave. This permits slaves to be rearranged, added, or removed without the need to explicitly specify row and column information.

When no column or row information is specified for a slave, default values are chosen for column, row, columnspan and rowspan at the time the slave is managed. The values are chosen based upon the current layout of the grid, the position of the slave relative to other slaves in the same grid command, and the presence of the characters -, ^, and ^ in grid command where slave names are normally expected.

- This increases the columnspan of the slave to the left. Several -'s in a row will successively increase the columnspan. S - may not follow a ^ or a x.

x This leaves an empty column between the slave on the left and the slave on the right.

^ This extends the rowspan of the slave above the ^'s in the grid. The number of ^'s in a row must match the number of columns spanned by the slave above it.

Restrictions on Master Windows

In pad, the master for each slave is the slave's parent (which is a grid object). This means if an object belongs to an existing group then it cannot be added to a grid.

Differences Between Pad++ and TK Grid Commands

Examples

1) put four objects in a 2x2 grid with 10 pixels horizontal and vertical pading:

set obj1 [.pad create rectangle 0 0 50 50]
set obj2 [.pad create rectangle 50 50 100 100]
	set obj3 [.pad create rectangle 100 100 150 150]
set obj4 [.pad create rectangle 150 150 200 200]
set thegrid [.pad create grid $obj1 $obj2 -padx 10 -pady 10]
.pad grid $obj3 $obj4 -in $thegrid -row 1 -padx 10 -pady 10


2) read objects from pad files in a directory and place them in a Nx2 grid (this can be useful for creating palettes):

proc read_files {PAD dir} {
		set objs ""
					# Go though list of files
		foreach file [glob $dir/*.pad] {
					# Read file and put all its object in a group (Pad_ObjectList will be
					# set to list of objects read from file).
			$PAD read $file
			set group [$PAD create group -members $Pad_ObjectList]
			lappend objs $group
		}
		return $objs
}

proc create_palette {PAD objs} {
					# Create the grid object
		set thegrid [$PAD create grid]
		set row 0
		set col 0

					# Go through objects and place them two per row
		foreach obj $objs {
					# Add obj to the grid
			$PAD grid $obj -in $thegrid -row $row -column $col -padx 10 -pady 5

					# Set row and column position for next object
			if {$col == 0} {
				incr col
			} else {
				set col 0
				incr row
			}
		}

					# Have the grid arrange itself now
		$PAD grid arrange $thegrid

		return $thegrid
}

	create_palette .pad [read_files .pad $env(PADHOME)/draw/scrapbook]

Alternatively,

proc create_palette {PAD objs} {
		# create the grid object
		set thegrid [$PAD create grid]

		# go through list of objects and place them two per row
		set numobjs [llength $objs]
		for {set i 0} {$i < $numobjs} {incr i 2} {
			set obj1 [lindex $objs $i]
			if {$i < [expr $numobjs-1]} {
				set obj2 [lindex $objs [expr $i+1]]
			} else {
				set obj2 ""
			}
			$PAD grid $obj1 $obj2 -in $thegrid -padx 10 -pady 5
		}

		$PAD grid arrange $thegrid
		return $thegrid
}

	create_palette .pad [read_files .pad $env(PADHOME)/draw/scrapbook]

3) Draw horizontal and vertical grid lines and a bounding rectangle for an existing grid. Make a group for the line objects and the existing grid. Assume the grid is a normal MxN table (i.e. all rows have N columns and all columns have M rows).

proc create_gridlines { PAD thegrid } {
						# Get bounding box, width and height and location of the grid
		set gbbox [$PAD bbox $thegrid]
		set gwidth [expr [lindex $gbbox 2] - [lindex $gbbox 0]]
		set gheight [expr [lindex $gbbox 3] - [lindex $gbbox 1]]
		set gx [lindex $gbbox 0]
		set gy [lindex $gbbox 1]

						# Get number of rows and columns
		set numrows [lindex [$PAD grid size $thegrid] 1]
		set numcols [lindex [$PAD grid size $thegrid] 0]

						# Create the bounding rectangle
		set grect [eval $PAD create rectangle $gbbox]

		set items "$grect"
		set scale [$PAD scale $thegrid]

						# Create horizontal lines by looking at the <r, 0> grid elemments.
		for {set r 1} {$r < $numrows} {incr r} {
						# Get location of the <r, 0> element (including padding)
			set rinfo [$PAD grid bbox $thegrid 0 $r]
			set x1 [expr [lindex $rinfo 0]*$scale + $gx]
						# Transform the y coord for pad (grid's is from top left corner)
			set y1 [expr ($gheight - [lindex $rinfo 1]*$scale) + $gy]
			set x2 [expr $x1 + $gwidth]
			set y2 $y1
			lappend items [$PAD create line $x1 $y1 $x2 $y2 -tags gridrowline_$thegrid]
		}

						# Draw vertical lines by looking at the <0, c> elements
		for {set c 1} {$c < $numcols} {incr c} {
			set cinfo [$PAD grid bbox $thegrid $c 0]
			set x1 [expr [lindex $cinfo 0]*$scale + $gx]
			set y1 [expr ($gheight - [lindex $cinfo 1]*$scale) + $gy]
			set x2 $x1
			set y2 [expr $y1 - $gheight]
			lappend items [$PAD create line $x1 $y1 $x2 $y2 -tags gridcolline_$thegrid]
		}

						# Create a group for all the grid lines
		set glines [$PAD create group -members $items -divisible 0 \
						 -tags gridlines_$thegrid]

						# Create a group for the lines and the grid
		set newgrp [$PAD create group -members "$glines $thegrid" -tags grid_$thegrid \
						-divisible 1]

		return $newgrp
}

	set thegrid [create_palette .pad [read_files .pad ./draw/scrapbook]]
create_gridlines .pad $thegrid

Relative Placement
Restrictions on Master Windows
Differences Between Pad++ and TK Grid Commands
Examples

Pad++ Reference Manual - 20 JUN 1997

Copyright Computer Science Department, The University of New Mexico

Web Accessibility