| [ Team LiB ] |
|
Bitmaps and ImagesThe label and all the button widgets have an image attribute that specifies a graphic image to display. Using an image takes two steps. In the first step the image is created via the image create command. This command returns an identifier for the image, and it is this identifier that is passed to widgets as the value of their image attribute. Example 41-3 Specifying an image for a widgetset im [image create bitmap \ -file glyph.bitmap -maskfile glyph.mask \ -background white -foreground blue] button .foo -image $im There are three things that can be displayed by labels and all the buttons: text, bitmaps, and images. If more than one of these attributes are specified, then the image has priority over the bitmap, and the bitmap has priority over the text. You can remove the image or bitmap attribute by specifying a null string for its value:
.foo config -image {}
Tk 8.4 introduced the compound attribute for labels, menu entries, and the various button widgets, which specifies whether the widgets should display both an image (or bitmap) and text, and if so, where the image should be placed relative to the text. For example, the following command would cause a label to display a bitmap on the left, and text to the right: label .warn -text Warning -bitmap warning -compound left The image CommandTable 41-5 summarizes the image command.
The exact set of options for image create depend on the image type. There are two built-in image types: bitmap and photo. Chapter 48 describes the C interface for defining new image types. Bitmap ImagesA bitmap image has a main image and an optional mask image. The main image is drawn in the foreground color. The mask image is drawn in the background color, unless the corresponding bit is set in the main image. The remaining bits are "clear" and the widget's normal background color shows through. Table 41-6 lists the options supported by the bitmap image type:
The bitmap definition files are stylized C structure definitions that the Tk library parses. The files usually have a .xbm file name extension. These are generated by bitmap editors such as bitmap program, which comes with the standard X distribution. The -file and -maskfile options name a file that contains such a definition. The -data and -maskdata options specify a string in the same format as the contents of one of those files. The bitmap AttributeThe label and all the button widgets also support a bitmap attribute, which is a special case of an image. This attribute is a little more convenient than the image attribute because the extra step of creating an image is not required. However, there are some power and flexibility with the image command, such as the ability to reconfigure a named image (e.g., for animation) that is not possible with a bitmap. Example 41-4 Specifying a bitmap for a widgetbutton .foo -bitmap @glyph.xbm -fg blue The @ syntax for the bitmap attribute signals that a file containing the bitmap is being specified. It is also possible to name built-in bitmaps. The predefined bitmaps are shown in the next figure along with their symbolic name. Chapter 48 describes the C interface for defining built in bitmaps. Example 41-5 The built-in bitmaps
frame .f -bd 4; frame .g -bd 4 ; pack .f .g -side left
set parent .f ; set next .g
foreach name {error gray12 gray50 hourglass \
info questhead question warning} {
frame $parent.$name
label $parent.$name.l -text $name -width 9 -anchor w
label $parent.$name.b -bitmap $name
pack $parent.$name.l -side right
pack $parent.$name.b -side top
pack $parent.$name -side top -expand true -fill x
set tmp $parent ; set parent $next ; set next $tmp
}
Photo ImagesThe photo image type was contributed to Tk by Paul Mackerras. It displays full color images and can do dithering and gamma correction. Table 41-7 lists the attributes for photo images. These are specified in the image create photo command.
The format indicates what format the data are in. The photo image supports different image formats. Tk has built-in support for the PPM, PGM, and GIF formats. There is a C interface to define new photo formats. The CD-ROM has a "plus-patch" version of Tk that supports pixmaps and JPEG files. Normally you do not need to specify the format because the photo implementation will try all format handlers until it find one that accepts the data. An explicit format limits what handlers are tried. The format name is treated as a prefix that is compared against the names of handlers. Case is not significant in the format name. The palette setting determines how many colors or graylevels are used when rendering an image. If a single number is specified, the image is rendered in greyscale with that many shades of gray. For full color, three numbers separated by slashes specify the number of shades of red, green, and blue, respectively. The more shades you specify the more room you take up in your colormap. The photo widget will switch to a private colormap if necessary. Multiply the number of red, green, and blue shades to determine how many different colors you use. If you have an 8-bit display, there are only 256 colors available. Reasonable palette settings that do not hog the colormap include 5/5/4 and 6/6/5. You can use fewer shades of blue because the human eye is less sensitive to blue. After you create an image you can operate on it. Table 41-8 lists the image instance operations. In the table, $p is a photo image handle returned by the image create photo command.
Table 41-9 lists the options available when you copy data from one image to another. The regions involved in the copy are specified by the upper-left and lower-right corners. If the lower-right corner of the source is not specified, then it defaults to the lower-right corner of the image. If the lower-right corner of the destination is not specified, then the size is determined by the area of the source. Otherwise, the source image may be cropped or replicated to fill the destination.
Table 41-10 lists the read options. If not specified, the format is determined automatically. If there are multiple image types that can read the same data, you may specify a read format
Table 41-11 lists the options used for write and data. When writing to files, the -format option is important because if you don't specify it, the first format found is used. On the other hand, you shouldn't use the -format option with data operation, as data returns the image date as a list of rows, where each row is a list of colors in #rrggbb format (suitable as input to the put command).
|
| [ Team LiB ] |
|