Gtk.DrawingArea

g GObject.GInterface GObject.GInterface Gtk.Accessible Gtk.Accessible GObject.GInterface->Gtk.Accessible Gtk.Buildable Gtk.Buildable GObject.GInterface->Gtk.Buildable Gtk.ConstraintTarget Gtk.ConstraintTarget GObject.GInterface->Gtk.ConstraintTarget GObject.InitiallyUnowned GObject.InitiallyUnowned Gtk.Widget Gtk.Widget GObject.InitiallyUnowned->Gtk.Widget GObject.Object GObject.Object GObject.Object->GObject.InitiallyUnowned Gtk.Accessible->Gtk.Widget Gtk.Buildable->Gtk.Widget Gtk.ConstraintTarget->Gtk.Widget Gtk.DrawingArea Gtk.DrawingArea Gtk.Widget->Gtk.DrawingArea

Example

../_images/DrawingArea.png
Subclasses:

None

Methods

Inherited:

Gtk.Widget (181), GObject.Object (37), Gtk.Accessible (15), Gtk.Buildable (1)

Structs:

Gtk.WidgetClass (18), GObject.ObjectClass (5)

class

new ()

get_content_height ()

get_content_width ()

set_content_height (height)

set_content_width (width)

set_draw_func (draw_func, *user_data)

Virtual Methods

Inherited:

Gtk.Widget (25), GObject.Object (7), Gtk.Accessible (6), Gtk.Buildable (9)

do_resize (width, height)

Properties

Inherited:

Gtk.Widget (34), Gtk.Accessible (1)

Name

Type

Flags

Short Description

content-height

int

r/w/en

content-width

int

r/w/en

Signals

Inherited:

Gtk.Widget (13), GObject.Object (1)

Name

Short Description

resize

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

Fields

Inherited:

Gtk.Widget (13), GObject.Object (1)

Name

Type

Access

Description

widget

Gtk.Widget

r

Class Details

class Gtk.DrawingArea(**kwargs)
Bases:

Gtk.Widget

Abstract:

No

Structure:

Gtk.DrawingAreaClass

GtkDrawingArea is a widget that allows drawing with cairo.

An example Gtk.DrawingArea

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

  • The [signal`Gtk`.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 [signal`Gtk`.DrawingArea::resize] signal to take any necessary actions when the widget changes size.

  • Call [method`Gtk`.DrawingArea.set_draw_func] to handle redrawing the contents of the widget.

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

Simple Gtk.DrawingArea usage

```c static void draw_function (Gtk.DrawingArea *area, cairo.Context *cr, int width, int height, object data) { Gdk.RGBA color;

cairo.Context.arc (cr, width / 2.0, height / 2.0, MIN (width, height) / 2.0, 0, 2 * GLib.PI);

Gtk.Widget.get_color (GTK_WIDGET (area), &color); Gdk.cairo_set_source_rgba (cr, &color);

cairo.Context.fill (cr); }

int main (int argc, str **argv) { Gtk.init ();

Gtk.Widget *area = Gtk.DrawingArea.new (); Gtk.DrawingArea.set_content_width (GTK_DRAWING_AREA (area), 100); Gtk.DrawingArea.set_content_height (GTK_DRAWING_AREA (area), 100); Gtk.DrawingArea.set_draw_func (GTK_DRAWING_AREA (area), draw_function, None, None); 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 [method`Gtk`.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 [func`Gdk`.:obj:cairo.Context.set_source_rgba] or [func`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.

classmethod new()[source]
Returns:

a new GtkDrawingArea

Return type:

Gtk.Widget

Creates a new drawing area.

get_content_height()[source]
Returns:

The height requested for content of the drawing area

Return type:

int

Retrieves the content height of the GtkDrawingArea.

get_content_width()[source]
Returns:

The width requested for content of the drawing area

Return type:

int

Retrieves the content width of the GtkDrawingArea.

set_content_height(height)[source]
Parameters:

height (int) – the height of contents

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 [method`Gtk`.Widget.set_valign] to avoid that.

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

set_content_width(width)[source]
Parameters:

width (int) – the width of contents

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 [method`Gtk`.Widget.set_halign] to avoid that.

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

set_draw_func(draw_func, *user_data)[source]
Parameters:

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 [method`Gtk`.Widget.queue_draw] on the drawing area. This will cause a redraw and will call draw_func again.

do_resize(width, height) virtual
Parameters:
  • width (int) –

  • height (int) –

Signal Details

Gtk.DrawingArea.signals.resize(drawing_area, width, height)
Signal Name:

resize

Flags:

RUN_LAST

Parameters:
  • drawing_area (Gtk.DrawingArea) – The object which received the signal

  • width (int) – the width of the viewport

  • height (int) – the height of the viewport

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.

Property Details

Gtk.DrawingArea.props.content_height
Name:

content-height

Type:

int

Default Value:

0

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

The content height.

Gtk.DrawingArea.props.content_width
Name:

content-width

Type:

int

Default Value:

0

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

The content width.