Table of Contents The DrawingArea widget wraps a gtk.gdk.Window which is a subclass of gtk.gdk.Drawable (as is a gtk.gdk.Pixmap). In effect the DrawingArea provides a simple 'canvas' area (the wrapped gtk.gdk.Window) that can be drawn on using the methods of the gtk.gdk.Drawable class. A DrawingArea is created using the constructor:
A DrawingArea is initially created with a size of (0, 0) so you should use the following method to make the drawing_area visible by setting its width and height to useful values greater than zero:
To draw on a DrawingArea you must retrieve the wrapped gtk.gdk.Window using the window attribute of the DrawingArea as follows:
Then you can draw on drawable using the gtk.gdk.Drawable methods described in Section 12.2, “Drawing Methods”. NoteThe DrawingArea must be realized (i.e. the Widget methods realize() or show() have been called) to have an associated gtk.gdk.Window that can be used for drawing. A variety of methods are available to draw onto the gtk.gdk.Window of a DrawingArea. All these methods require a graphics context (gtk.gdk.GC) to encapsulate, as attributes, the information required for drawing. The attributes of a gtk.gdk.GC are:
background specifies an allocated gtk.gdk.Color that is used to draw the background color. foreground specifies an allocated gtk.gdk.Color that is used to draw the foreground color. A gtk.gdk.Color represents a color that may be allocated or unallocated. An unallocated color can be created using the constructor:
where red, green and blue are integers in the range of 0 to 65535. pixel is not usually specified because it is overwritten when the color is allocated. Alternatively, an unallocated gtk.gdk.Color can be created using the function:
where spec is a color specification string that can be either:
A gtk.gdk.Color representing an allocated color is created using the gtk.gdk.Colormap alloc_color() method which has three signatures:
color is an unallocated gtk.gdk.Color. spec is a color specification string as described above for the gtk.gdk.color_parse() function. red, green and blue are integer color values as described for the gtk.gdk.Color() constructor. You can optionally specify whether the allocated color should be writeable (i.e. can be changed later but cannot be shared) or whether a best match with existing colors should be made if the exact color is not available. For example:
The colormap associated with a widget can be retrieved using the method:
cap_style specifies the line ending style that is used when drawing the end of a line that is not joined to another line. The available cap styles are:
clip_mask specifies a gtk.gdk.Pixmap that is used to clip the drawing in the drawing_area. clip_x_origin and clip_y_origin specify the origin x and y values relative to the upper left corner of the drawing_area for clipping. fill specifies the fill style to be used when drawing. The available fill styles are:
font is a gtk.gdk.Font that is used as the default font for drawing text. NoteThe use of the font attribute is deprecated. function specifies how the bit values for the source pixels are combined with the bit values for destination pixels to produce the resulting pixels bits. The sixteen values here correspond to the 16 different possible 2x2 truth tables but only a couple of these values are usually useful. For color images, only COPY, XOR and INVERT are generally useful while for bitmaps, AND and OR are also useful. The function values are:
graphics_exposures specifies whether graphics exposures are enabled (TRUE) or disabled (FALSE). When graphics_exposures is TRUE, a failure when copy pixels in a drawing operation will cause an expose event to be issued. If the copy succeeds, a noexpose event is issued. join_style specifies the style of joint to be used when lines meet at an angle. The available styles are:
line_style specifies the style that a line will be drawn with. The available styles are:
line_width specifies the width that lines will be drawn with. stipple specifies the gtk.gdk.Pixmap that will be used for stippled drawing when the fill is set to either STIPPLED or OPAQUE_STIPPLED. sub_window specifies the mode of drawing into a gtk.gdk.Window that has child gtk.gdk.Windows. The possible values of sub_window are:
tile specifies the gtk.gdk.Pixmap to used for tiled drawing when the fill is set to TILED. ts_x_origin and ts_y_origin specify the tiling/stippling origin (the starting position for the stippling bitmap or tiling pixmap). A new Graphics Context is created by a call to the gtk.gdk.Drawable.new_gc() method:
In order for a new Graphics Context to be created with this method, the drawable must be:
The various attributes of the Graphics Context have default values if not set in the new_gc() method. If you want to set the GC attributes using the new_gc() method, it's much easier to use the Python keyword arguments. The individual attributes of a gtk.gdk.GC can also be set by assigning a value to the GC object attribute. For example:
or by using the following methods:
The dash pattern to be used when the line_style is LINE_ON_OFF_DASH or LINE_DOUBLE_DASH can be set using the following method:
where offset is the index of the starting dash value in dash_list and dash_list is a list or tuple containing numbers of pixels to be drawn or skipped to form the dashes. The dashes are drawn starting with the number of pixels at the offset position; then the next number of pixels is skipped; and then the next number is drawn; and so on rotating through all the dash_list numbers and starting over when the end is reached. For example, if the dash_list is (2, 4, 8, 16) and the offset is 1, the dashes will be drawn as: draw 4 pixels, skip 8 pixels, draw 16 pixels, skip 2 pixels, draw 4 pixels and so on. A copy of an existing gtk.gdk.GC can be made using the method:
The attributes of gc will then be the same as src_gc. |