gi-gtk-4.0.1: Gtk bindings
CopyrightWill Thompson Iñaki García Etxebarria and Jonas Platte
LicenseLGPL-2.1
MaintainerIñaki García Etxebarria
Safe HaskellNone
LanguageHaskell2010

GI.Gtk.Objects.DrawingArea

Description

The DrawingArea widget is used for creating custom user interface elements. It’s essentially a blank widget; you can draw on it. After creating a drawing area, the application may want to connect to:

  • The 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 sizeAllocate signal to take any necessary actions when the widget changes size.
  • Call drawingAreaSetDrawFunc 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

C code

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

  context = gtk_widget_get_style_context (GTK_WIDGET (area));

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

  gtk_style_context_get_color (context,
                               &color);
  gdk_cairo_set_source_rgba (cr, &color);

  cairo_fill (cr);
}

void 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);

}

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 gtk_widget_queue_draw_region(), gtk_widget_queue_draw_area() or widgetQueueDraw. This will cause the drawing area to call the draw function again.

The available routines for drawing are documented on the [GDK Drawing Primitives][gdk3-Cairo-Interaction] page and the cairo documentation.

To receive mouse events on a drawing area, you will need to enable them with gtk_widget_add_events(). 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. Use widgetHasFocus in your expose event handler to decide whether to draw the focus indicator. See renderFocus for one way to draw focus.

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

Synopsis

Exported types

newtype DrawingArea Source #

Memory-managed wrapper type.

class (GObject o, IsDescendantOf DrawingArea o) => IsDrawingArea o Source #

Type class for types which can be safely cast to DrawingArea, for instance with toDrawingArea.

Instances

Instances details
(GObject o, IsDescendantOf DrawingArea o) => IsDrawingArea o Source # 
Instance details

Defined in GI.Gtk.Objects.DrawingArea

toDrawingArea :: (MonadIO m, IsDrawingArea o) => o -> m DrawingArea Source #

Cast to DrawingArea, for types for which this is known to be safe. For general casts, use castTo.

Methods

Overloaded methods

getContentHeight

drawingAreaGetContentHeight Source #

Arguments

:: (HasCallStack, MonadIO m, IsDrawingArea a) 
=> a

self: a DrawingArea

-> m Int32

Returns: The height requested for content of the drawing area

Retrieves the value previously set via drawingAreaSetContentHeight.

getContentWidth

drawingAreaGetContentWidth Source #

Arguments

:: (HasCallStack, MonadIO m, IsDrawingArea a) 
=> a

self: a DrawingArea

-> m Int32

Returns: The width requested for content of the drawing area

Retrieves the value previously set via drawingAreaSetContentWidth.

new

drawingAreaNew Source #

Arguments

:: (HasCallStack, MonadIO m) 
=> m DrawingArea

Returns: a new DrawingArea

Creates a new drawing area.

setContentHeight

drawingAreaSetContentHeight Source #

Arguments

:: (HasCallStack, MonadIO m, IsDrawingArea a) 
=> a

self: a DrawingArea

-> Int32

height: the height of contents

-> m () 

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 widgetSetValign to avoid that.

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

setContentWidth

drawingAreaSetContentWidth Source #

Arguments

:: (HasCallStack, MonadIO m, IsDrawingArea a) 
=> a

self: a DrawingArea

-> Int32

width: the width of contents

-> m () 

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 widgetSetHalign to avoid that.

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

setDrawFunc

drawingAreaSetDrawFunc Source #

Arguments

:: (HasCallStack, MonadIO m, IsDrawingArea a) 
=> a

self: a DrawingArea

-> Maybe DrawingAreaDrawFunc

drawFunc: callback that lets you draw the drawing area's contents

-> m () 

Setting a draw function is the main thing you want to do when using a drawing area. It 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 widgetQueueDraw on the drawing area. This will cause a redraw and will call drawFunc again.

Properties

contentHeight

The content height. See drawingAreaSetContentHeight for details.

constructDrawingAreaContentHeight :: IsDrawingArea o => Int32 -> IO (GValueConstruct o) Source #

Construct a GValueConstruct with valid value for the “content-height” property. This is rarely needed directly, but it is used by new.

getDrawingAreaContentHeight :: (MonadIO m, IsDrawingArea o) => o -> m Int32 Source #

Get the value of the “content-height” property. When overloading is enabled, this is equivalent to

get drawingArea #contentHeight

setDrawingAreaContentHeight :: (MonadIO m, IsDrawingArea o) => o -> Int32 -> m () Source #

Set the value of the “content-height” property. When overloading is enabled, this is equivalent to

set drawingArea [ #contentHeight := value ]

contentWidth

The content width. See drawingAreaSetContentWidth for details.

constructDrawingAreaContentWidth :: IsDrawingArea o => Int32 -> IO (GValueConstruct o) Source #

Construct a GValueConstruct with valid value for the “content-width” property. This is rarely needed directly, but it is used by new.

getDrawingAreaContentWidth :: (MonadIO m, IsDrawingArea o) => o -> m Int32 Source #

Get the value of the “content-width” property. When overloading is enabled, this is equivalent to

get drawingArea #contentWidth

setDrawingAreaContentWidth :: (MonadIO m, IsDrawingArea o) => o -> Int32 -> m () Source #

Set the value of the “content-width” property. When overloading is enabled, this is equivalent to

set drawingArea [ #contentWidth := value ]