GTK+ 2.0 Tutorial
<<< Previous | Scribble, A Simple Example Drawing Program | Next >>>

# The DrawingArea Widget, And Drawing

We now turn to the process of drawing on the screen. The widget we use for this is the DrawingArea widget. A drawing area widget is essentially an X window and nothing more. It is a blank canvas in which we can draw whatever we like. A drawing area is created using the call:

    GtkWidget* gtk_drawing_area_new        (void);

A default size for the widget can be specified by calling:

    void       gtk_drawing_area_size       (GtkDrawingArea      *darea,
    					gint                 width,
    					gint                 height);

This default size can be overridden, as is true for all widgets, by calling `gtk_widget_set_size_request()`, and that, in turn, can be overridden if the user manually resizes the the window containing the drawing area.

It should be noted that when we create a DrawingArea widget, we are completely responsible for drawing the contents. If our window is obscured then uncovered, we get an exposure event and must redraw what was previously hidden.

Having to remember everything that was drawn on the screen so we can properly redraw it can, to say the least, be a nuisance. In addition, it can be visually distracting if portions of the window are cleared, then redrawn step by step. The solution to this problem is to use an offscreen backing pixmap. Instead of drawing directly to the screen, we draw to an image stored in server memory but not displayed, then when the image changes or new portions of the image are displayed, we copy the relevant portions onto the screen.

To create an offscreen pixmap, we call the function:

    GdkPixmap* gdk_pixmap_new               (GdkWindow  *window,
    					 gint        width,
    					 gint        height,
    					 gint        depth);

The `window` parameter specifies a GDK window that this pixmap takes some of its properties from. `width` and `height` specify the size of the pixmap. `depth` specifies the color depth, that is the number of bits per pixel, for the new window. If the depth is specified as `-1`, it will match the depth of `window`.

We create the pixmap in our "configure_event" handler. This event is generated whenever the window changes size, including when it is originally created.

    /* Backing pixmap for drawing area */
    static GdkPixmap *pixmap = NULL;

    /* Create a new backing pixmap of the appropriate size */
    static gint
    configure_event (GtkWidget *widget, GdkEventConfigure *event)
    {
      if (pixmap)
        gdk_pixmap_unref(pixmap);

      pixmap = gdk_pixmap_new(widget->window,
    			  widget->allocation.width,
    			  widget->allocation.height,
    			  -1);
      gdk_draw_rectangle (pixmap,
    		      widget->style->white_gc,
    		      TRUE,
    		      0, 0,
    		      widget->allocation.width,
    		      widget->allocation.height);

      return TRUE;
    }

The call to `gdk_draw_rectangle()` clears the pixmap initially to white. We'll say more about that in a moment.

Our exposure event handler then simply copies the relevant portion of the pixmap onto the screen (we determine the area we need to redraw by using the event->area field of the exposure event):

    /* Redraw the screen from the backing pixmap */
    static gint
    expose_event (GtkWidget *widget, GdkEventExpose *event)
    {
      gdk_draw_pixmap(widget->window,
    		  widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
    		  pixmap,
    		  event->area.x, event->area.y,
    		  event->area.x, event->area.y,
    		  event->area.width, event->area.height);

      return FALSE;
    }

We've now seen how to keep the screen up to date with our pixmap, but how do we actually draw interesting stuff on our pixmap? There are a large number of calls in GTK's GDK library for drawing on drawables. A drawable is simply something that can be drawn upon. It can be a window, a pixmap, or a bitmap (a black and white image). We've already seen two such calls above, `gdk_draw_rectangle()` and `gdk_draw_pixmap()`. The complete list is:

    gdk_draw_line ()
    gdk_draw_rectangle ()
    gdk_draw_arc ()
    gdk_draw_polygon ()
    gdk_draw_string ()
    gdk_draw_text ()
    gdk_draw_pixmap ()
    gdk_draw_bitmap ()
    gdk_draw_image ()
    gdk_draw_points ()
    gdk_draw_segments ()

See the reference documentation or the header file `<gdk/gdk.h>` for further details on these functions. These functions all share the same first two arguments. The first argument is the drawable to draw upon, the second argument is a graphics context (GC).

A graphics context encapsulates information about things such as foreground and background color and line width. GDK has a full set of functions for creating and modifying graphics contexts, but to keep things simple we'll just use predefined graphics contexts. Each widget has an associated style. (Which can be modified in a gtkrc file, see the section GTK's rc file.) This, among other things, stores a number of graphics contexts. Some examples of accessing these graphics contexts are:

    widget->style->white_gc
    widget->style->black_gc
    widget->style->fg_gc[GTK_STATE_NORMAL]
    widget->style->bg_gc[GTK_WIDGET_STATE(widget)]

The fields `fg_gc`, `bg_gc`, `dark_gc`, and `light_gc` are indexed by a parameter of type `GtkStateType` which can take on the values:

    GTK_STATE_NORMAL,
    GTK_STATE_ACTIVE,
    GTK_STATE_PRELIGHT,
    GTK_STATE_SELECTED,
    GTK_STATE_INSENSITIVE

For instance, for `GTK_STATE_SELECTED` the default foreground color is white and the default background color, dark blue.

Our function `draw_brush()`, which does the actual drawing on the screen, is then:

    /* Draw a rectangle on the screen */
    static void
    draw_brush (GtkWidget *widget, gdouble x, gdouble y)
    {
      GdkRectangle update_rect;

      update_rect.x = x - 5;
      update_rect.y = y - 5;
      update_rect.width = 10;
      update_rect.height = 10;
      gdk_draw_rectangle (pixmap,
    		      widget->style->black_gc,
    		      TRUE,
    		      update_rect.x, update_rect.y,
    		      update_rect.width, update_rect.height);
      gtk_widget_draw (widget, &update_rect);
    }

After we draw the rectangle representing the brush onto the pixmap, we call the function:

    void       gtk_widget_draw                (GtkWidget           *widget,
    					   GdkRectangle        *area);

which notifies X that the area given by the `area` parameter needs to be updated. X will eventually generate an expose event (possibly combining the areas passed in several calls to `gtk_widget_draw()`) which will cause our expose event handler to copy the relevant portions to the screen.

We have now covered the entire drawing program except for a few mundane details like creating the main window.

<<< Previous | Home | Next >>>
Event Handling | Up | Adding XInput support