Allows drawing with cairo.
<picture> <source srcset="drawingarea-dark.png" media="(prefers-color-scheme: dark)"> <img alt="An example GtkDrawingArea" src="drawingarea.png"> </picture> It's essentially a blank widget; you can draw on it. After creating a drawing area, the application may want to connect to:
The [signalGtk.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 [signalGtk.DrawingArea::resize] signal to take any necessary actions
when the widget changes size.
Call [methodGtk.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 GtkDrawingArea usage
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 [methodGtk.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](https://www.cairographics.org/manual/); GDK offers
additional API to integrate with Cairo, like
[funcGdk.cairo_set_source_rgba] or [funcGdk.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.
<gtkada_demo>create_drawing_area.adb</gtkada_demo>
function "+"
(Widget : access Gtk_Drawing_Area_Record'Class)
return Gtk.Accessible.Gtk_Accessible
function "+"
(Widget : access Gtk_Drawing_Area_Record'Class)
return Gtk.Buildable.Gtk_Buildable
function "+"
(Widget : access Gtk_Drawing_Area_Record'Class)
return Gtk.Constraint_Target.Gtk_Constraint_Target
function "-"
(Interf : Gtk.Accessible.Gtk_Accessible)
return Gtk_Drawing_Area
function "-"
(Interf : Gtk.Buildable.Gtk_Buildable)
return Gtk_Drawing_Area
function "-"
(Interf : Gtk.Constraint_Target.Gtk_Constraint_Target)
return Gtk_Drawing_Area
procedure Announce
(Self : not null access Gtk_Drawing_Area_Record;
Message : UTF8_String;
Priority : Gtk.Accessible.Gtk_Accessible_Announcement_Priority)
type Cb_GObject_Gint_Gint_Void is not null access procedure
(Self : access Glib.Object.GObject_Record'Class;
Width : Glib.Gint;
Height : Glib.Gint);
type Cb_Gtk_Drawing_Area_Gint_Gint_Void is not null access procedure
(Self : access Gtk_Drawing_Area_Record'Class;
Width : Glib.Gint;
Height : Glib.Gint);
Content_Height_Property : constant Glib.Properties.Property_Int;
The content height.
Content_Width_Property : constant Glib.Properties.Property_Int;
The content width.
function Get_Accessible_Id
(Self : not null access Gtk_Drawing_Area_Record) return UTF8_String
function Get_Accessible_Parent
(Self : not null access Gtk_Drawing_Area_Record)
return Gtk.Accessible.Gtk_Accessible
function Get_Accessible_Role
(Self : not null access Gtk_Drawing_Area_Record)
return Gtk.Accessible.Gtk_Accessible_Role
function Get_At_Context
(Self : not null access Gtk_Drawing_Area_Record)
return Gtk.Atcontext.Gtk_Atcontext
function Get_Bounds
(Self : not null access Gtk_Drawing_Area_Record;
X : out Glib.Gint;
Y : out Glib.Gint;
Width : out Glib.Gint;
Height : out Glib.Gint) return Boolean
function Get_Content_Height
(Self : not null access Gtk_Drawing_Area_Record) return Glib.Gint
Retrieves the content height of the GtkDrawingArea.
The height requested for content of the drawing area
function Get_Content_Width
(Self : not null access Gtk_Drawing_Area_Record) return Glib.Gint
Retrieves the content width of the GtkDrawingArea.
The width requested for content of the drawing area
function Get_First_Accessible_Child
(Self : not null access Gtk_Drawing_Area_Record)
return Gtk.Accessible.Gtk_Accessible
function Get_Next_Accessible_Sibling
(Self : not null access Gtk_Drawing_Area_Record)
return Gtk.Accessible.Gtk_Accessible
function Get_Platform_State
(Self : not null access Gtk_Drawing_Area_Record;
State : Gtk.Accessible.Gtk_Accessible_Platform_State) return Boolean
function Get_Type return Glib.GType
type Gtk_Drawing_Area is access all Gtk_Drawing_Area_Record'Class;
type Gtk_Drawing_Area_Draw_Func is access procedure
(Drawing_Area : not null access Gtk_Drawing_Area_Record'Class;
Cr : Cairo.Cairo_Context;
Width : Glib.Gint;
Height : Glib.Gint);
Whenever Drawing_Area needs to redraw, this function will be called. This function should exclusively redraw the contents of the drawing area and must not call any widget functions that cause changes.
the GtkDrawingArea to redraw
the context to draw to
the actual width of the contents. This value will be at least as wide as GtkDrawingArea:width.
the actual height of the contents. This value will be at least as wide as GtkDrawingArea:height.
function Gtk_Drawing_Area_New return Gtk_Drawing_Area
Creates a new drawing area.
type Gtk_Drawing_Area_Record is new Gtk_Widget_Record with null record;
procedure Gtk_New (Self : out Gtk_Drawing_Area)
Creates a new drawing area. Initialize does nothing if the object was already created with another call to Initialize* or G_New.
package Implements_Gtk_Accessible is new Glib.Types.Implements
(Gtk.Accessible.Gtk_Accessible, Gtk_Drawing_Area_Record, Gtk_Drawing_Area);
package Implements_Gtk_Buildable is new Glib.Types.Implements
(Gtk.Buildable.Gtk_Buildable, Gtk_Drawing_Area_Record, Gtk_Drawing_Area);
package Implements_Gtk_Constraint_Target is new Glib.Types.Implements
(Gtk.Constraint_Target.Gtk_Constraint_Target, Gtk_Drawing_Area_Record, Gtk_Drawing_Area);
procedure Initialize
(Self : not null access Gtk_Drawing_Area_Record'Class)
Creates a new drawing area. Initialize does nothing if the object was already created with another call to Initialize* or G_New.
procedure On_Resize
(Self : not null access Gtk_Drawing_Area_Record;
Call : Cb_GObject_Gint_Gint_Void;
Slot : not null access Glib.Object.GObject_Record'Class;
After : Boolean := False)
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.
Callback parameters: -- @param Width the width of the viewport -- @param Height the height of the viewport
procedure On_Resize
(Self : not null access Gtk_Drawing_Area_Record;
Call : Cb_Gtk_Drawing_Area_Gint_Gint_Void;
After : Boolean := False)
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.
Callback parameters: -- @param Width the width of the viewport -- @param Height the height of the viewport
procedure Reset_Property
(Self : not null access Gtk_Drawing_Area_Record;
Property : Gtk.Accessible.Gtk_Accessible_Property)
procedure Reset_Relation
(Self : not null access Gtk_Drawing_Area_Record;
Relation : Gtk.Accessible.Gtk_Accessible_Relation)
procedure Reset_State
(Self : not null access Gtk_Drawing_Area_Record;
State : Gtk.Accessible.Gtk_Accessible_State)
procedure Set_Accessible_Parent
(Self : not null access Gtk_Drawing_Area_Record;
Parent : Gtk.Accessible.Gtk_Accessible;
Next_Sibling : Gtk.Accessible.Gtk_Accessible)
procedure Set_Content_Height
(Self : not null access Gtk_Drawing_Area_Record;
Height : Glib.Gint)
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 [methodGtk.Widget.set_valign] to avoid that. If the height is set to 0 (the default), the drawing area may disappear.
the height of contents
procedure Set_Content_Width
(Self : not null access Gtk_Drawing_Area_Record;
Width : Glib.Gint)
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 [methodGtk.Widget.set_halign] to avoid that. If the width is set to 0 (the default), the drawing area may disappear.
the width of contents
procedure Set_Draw_Func
(Self : not null access Gtk_Drawing_Area_Record;
Draw_Func : Gtk_Drawing_Area_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 [methodGtk.Widget.queue_draw] on the drawing area. This will cause a redraw and will call Draw_Func again.
callback that lets you draw the drawing area's contents
Signal_Resize : constant Glib.Signal_Name := "resize";
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.
Callback parameters: -- @param Width the width of the viewport -- @param Height the height of the viewport
procedure Update_Next_Accessible_Sibling
(Self : not null access Gtk_Drawing_Area_Record;
New_Sibling : Gtk.Accessible.Gtk_Accessible)
procedure Update_Platform_State
(Self : not null access Gtk_Drawing_Area_Record;
State : Gtk.Accessible.Gtk_Accessible_Platform_State)