1.48 <GtkDrawingArea>

Class: <GtkDrawingArea>

It derives from <GtkWidget>.

Allows drawing with cairo.

It’s essentially a blank widget; you can draw on it. After creating a drawing area, the application may want to connect to:

  • The widget:realize signal to take any necessary actions when the widget is instantiated on a particular display. (Create GDK resources in response to this signal.)

The following code portion demonstrates using a drawing area to display a circle in the normal widget foreground color.

Simple GtkDrawingArea usage

static void
draw_function (GtkDrawingArea *area,
               cairo_t        *cr,
               int             width,
               int             height,
               gpointer        data)
{
  GdkRGBA color;

  cairo_arc (cr,
             width / 2.0, height / 2.0,
             MIN (width, height) / 2.0,
             0, 2 * G_PI);

  gtk_widget_get_color (GTK_WIDGET (area),
                        &color);
  gdk_cairo_set_source_rgba (cr, &color);

  cairo_fill (cr);
}

int
main (int argc, char **argv)
{
  gtk_init ();

  GtkWidget *area = gtk_drawing_area_new ();
  gtk_drawing_area_set_content_width (GTK_DRAWING_AREA (area), 100);
  gtk_drawing_area_set_content_height (GTK_DRAWING_AREA (area), 100);
  gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (area),
                                  draw_function,
                                  NULL, NULL);
  return 0;
}

The draw function is normally called when a drawing area first comes onscreen, or when it’s covered by another window and then uncovered. You can also force a redraw by adding to the “damage region” of the drawing area’s window using widget:queue-draw. This will cause the drawing area to call the draw function again.

The available routines for drawing are documented in the Cairo documentation; GDK offers additional API to integrate with Cairo, like cairo-set-source-rgba or Gdk.cairo_set_source_pixbuf.

To receive mouse events on a drawing area, you will need to use event controllers. To receive keyboard events, you will need to set the “can-focus” property on the drawing area, and you should probably draw some user-visible indication that the drawing area is focused.

If you need more complex control over your widget, you should consider creating your own <GtkWidget> subclass.

Fields

widget

Properties

Property: drawing-area:content-height self

The content height.

Its value is a 32-bit integer.

Set it with (set! (drawing-area:content-height self) value).

Property: drawing-area:content-width self

The content width.

Its value is a 32-bit integer.

Set it with (set! (drawing-area:content-width self) value).

Procedures

Method: drawing-area:get-content-height self ⇒ %return
Method: get-content-height self ⇒ %return

Retrieves the content height of the <GtkDrawingArea>.

self

Passed: a <GtkDrawingArea>.

a <GtkDrawingArea>

%return

Returned: a 32-bit integer.

The height requested for content of the drawing area

Method: drawing-area:get-content-width self ⇒ %return
Method: get-content-width self ⇒ %return

Retrieves the content width of the <GtkDrawingArea>.

self

Passed: a <GtkDrawingArea>.

a <GtkDrawingArea>

%return

Returned: a 32-bit integer.

The width requested for content of the drawing area

Procedure: drawing-area:new ⇒ %return

Creates a new drawing area.

%return

Returned: a <GtkWidget>.

a new <GtkDrawingArea>

Method: drawing-area:set-content-height self height
Method: set-content-height self height

Sets the desired height of the contents of the drawing area.

Note that because widgets may be allocated larger sizes than they requested, it is possible that the actual height passed to your draw function is larger than the height set here. You can use widget:set-valign to avoid that.

If the height is set to 0 (the default), the drawing area may disappear.

self

Passed: a <GtkDrawingArea>.

a <GtkDrawingArea>

height

Passed: a 32-bit integer.

the height of contents

Method: drawing-area:set-content-width self width
Method: set-content-width self width

Sets the desired width of the contents of the drawing area.

Note that because widgets may be allocated larger sizes than they requested, it is possible that the actual width passed to your draw function is larger than the width set here. You can use widget:set-halign to avoid that.

If the width is set to 0 (the default), the drawing area may disappear.

self

Passed: a <GtkDrawingArea>.

a <GtkDrawingArea>

width

Passed: a 32-bit integer.

the width of contents

Method: drawing-area:set-draw-func self [draw-func]
Method: set-draw-func self [draw-func]

Setting a draw function is the main thing you want to do when using a drawing area.

The draw function is called whenever GTK needs to draw the contents of the drawing area to the screen.

The draw function will be called during the drawing stage of GTK. In the drawing stage it is not allowed to change properties of any GTK widgets or call any functions that would cause any properties to be changed. You should restrict yourself exclusively to drawing your contents in the draw function.

If what you are drawing does change, call widget:queue-draw on the drawing area. This will cause a redraw and will call draw-func again.

self

Passed: a <GtkDrawingArea>.

a <GtkDrawingArea>

draw-func

Passed, or left out: a procedure, or #f.

callback that lets you draw the drawing area’s contents

See GtkDrawingAreaDrawFunc for how it is called.

user-data

Not passed: guile-gi supplies it.

user data passed to draw-func

destroy

Not passed: guile-gi supplies it.

destroy notifier for user-data

See GDestroyNotify for how it is called.

Signals

Signal: drawing-area:resize

Connect a handler with (connect object drawing-area:resize handler). It is called as (handler self width height).

Emitted once when the widget is realized, and then each time the widget is changed while realized.

This is useful in order to keep state up to date with the widget size, like for instance a backing surface.

self

Passed: a <GtkDrawingArea>.

width

Passed: a 32-bit integer.

the width of the viewport

height

Passed: a 32-bit integer.

the height of the viewport

Virtual methods

Virtual method: drawing-area:do-resize implementor self width height

To override it in a new class, give register-type #:vfuncs `((resize . ,procedure)). The procedure is called as (procedure self width height).

implementor

Passed: the class whose implementation to call: <GtkDrawingArea>, or a class that derives from it, or its GType number.

self

Passed: a <GtkDrawingArea>.

width

Passed: a 32-bit integer.

height

Passed: a 32-bit integer.