Package ch.bailu.gtk.gtk
Class DrawingArea
- All Implemented Interfaces:
PointerInterface
`GtkDrawingArea` is a widget that allows drawing with cairo.
![An example GtkDrawingArea](drawingarea.png)
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 GtkDrawingArea usage
```c
static 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);
}
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 [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](https://www.cairographics.org/manual/); GDK
offers additional API to integrate with Cairo, like [func@Gdk.cairo_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.
![An example GtkDrawingArea](drawingarea.png)
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 GtkDrawingArea usage
```c
static 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);
}
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 [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](https://www.cairographics.org/manual/); GDK
offers additional API to integrate with Cairo, like [func@Gdk.cairo_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.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
static interface
static interface
Nested classes/interfaces inherited from class ch.bailu.gtk.gtk.Widget
Widget.OnDestroy, Widget.OnDirectionChanged, Widget.OnHide, Widget.OnKeynavFailed, Widget.OnMap, Widget.OnMnemonicActivate, Widget.OnMoveFocus, Widget.OnQueryTooltip, Widget.OnRealize, Widget.OnShow, Widget.OnStateFlagsChanged, Widget.OnTickCallback, Widget.OnUnmap, Widget.OnUnrealize
Nested classes/interfaces inherited from class ch.bailu.gtk.gobject.Object
Object.OnBindingTransformFunc, Object.OnDuplicateFunc, Object.OnNotify, Object.OnToggleNotify, Object.OnWeakNotify
-
Field Summary
Fields inherited from class ch.bailu.gtk.gtk.Widget
SIGNAL_ON_DESTROY, SIGNAL_ON_DIRECTION_CHANGED, SIGNAL_ON_HIDE, SIGNAL_ON_KEYNAV_FAILED, SIGNAL_ON_MAP, SIGNAL_ON_MNEMONIC_ACTIVATE, SIGNAL_ON_MOVE_FOCUS, SIGNAL_ON_QUERY_TOOLTIP, SIGNAL_ON_REALIZE, SIGNAL_ON_SHOW, SIGNAL_ON_STATE_FLAGS_CHANGED, SIGNAL_ON_UNMAP, SIGNAL_ON_UNREALIZE
Fields inherited from class ch.bailu.gtk.gobject.Object
SIGNAL_ON_NOTIFY
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionImplements interfaceAccessible
.Implements interfaceBuildable
.Implements interfaceConstraintTarget
.static ClassHandler
int
Retrieves the content height of the `GtkDrawingArea`.int
Retrieves the content width of the `GtkDrawingArea`.static int
static long
static TypeSystem.TypeSize
static long
static TypeSystem.TypeSize
onResize
(DrawingArea.OnResize signal) Connect to signal "resize".void
setContentHeight
(int height) Sets the desired height of the contents of the drawing area.void
setContentWidth
(int width) Sets the desired width of the contents of the drawing area.void
setDrawFunc
(DrawingArea.OnDrawingAreaDrawFunc draw_func, Pointer user_data, DrawingArea.OnDestroyNotify destroy) Setting a draw function is the main thing you want to do when using
a drawing area.Methods inherited from class ch.bailu.gtk.gtk.Widget
actionSetEnabled, actionSetEnabled, activate, activateAction, activateAction, activateActionVariant, activateActionVariant, activateDefault, addController, addCssClass, addCssClass, addMnemonicLabel, addTickCallback, allocate, childFocus, computeBounds, computeExpand, computePoint, computeTransform, contains, createPangoContext, createPangoLayout, createPangoLayout, disposeTemplate, dragCheckThreshold, errorBell, getAllocatedBaseline, getAllocatedHeight, getAllocatedWidth, getAllocation, getAncestor, getCanFocus, getCanTarget, getChildVisible, getClipboard, getCssClasses, getCssName, getCursor, getDefaultDirection, getDirection, getDisplay, getFirstChild, getFocusable, getFocusChild, getFocusOnClick, getFontMap, getFontOptions, getFrameClock, getHalign, getHasTooltip, getHeight, getHexpand, getHexpandSet, getLastChild, getLayoutManager, getMapped, getMarginBottom, getMarginEnd, getMarginStart, getMarginTop, getName, getNative, getNextSibling, getOpacity, getOverflow, getPangoContext, getParent, getPreferredSize, getPrevSibling, getPrimaryClipboard, getRealized, getReceivesDefault, getRequestMode, getRoot, getScaleFactor, getSensitive, getSettings, getSize, getSizeRequest, getStateFlags, getStyleContext, getTemplateChild, getTemplateChild, getTooltipMarkup, getTooltipText, getValign, getVexpand, getVexpandSet, getVisible, getWidth, grabFocus, hasCssClass, hasCssClass, hasDefault, hasFocus, hasVisibleFocus, hide, inDestruction, initTemplate, insertActionGroup, insertActionGroup, insertAfter, insertBefore, isAncestor, isDrawable, isFocus, isSensitive, isVisible, keynavFailed, listMnemonicLabels, map, measure, mnemonicActivate, observeChildren, observeControllers, onDestroy, onDirectionChanged, onHide, onKeynavFailed, onMap, onMnemonicActivate, onMoveFocus, onQueryTooltip, onRealize, onShow, onStateFlagsChanged, onUnmap, onUnrealize, pick, queueAllocate, queueDraw, queueResize, realize, removeController, removeCssClass, removeCssClass, removeMnemonicLabel, removeTickCallback, setCanFocus, setCanTarget, setChildVisible, setCssClasses, setCursor, setCursorFromName, setCursorFromName, setDefaultDirection, setDirection, setFocusable, setFocusChild, setFocusOnClick, setFontMap, setFontOptions, setHalign, setHasTooltip, setHexpand, setHexpandSet, setLayoutManager, setMarginBottom, setMarginEnd, setMarginStart, setMarginTop, setName, setName, setOpacity, setOverflow, setParent, setReceivesDefault, setSensitive, setSizeRequest, setStateFlags, setTooltipMarkup, setTooltipMarkup, setTooltipText, setTooltipText, setValign, setVexpand, setVexpandSet, setVisible, shouldLayout, show, sizeAllocate, snapshotChild, triggerTooltipQuery, unmap, unparent, unrealize, unsetStateFlags
Methods inherited from class ch.bailu.gtk.gobject.Object
addToggleRef, bindProperty, bindProperty, bindPropertyFull, bindPropertyFull, bindPropertyWithClosures, bindPropertyWithClosures, compatControl, connect, connect, disconnect, disconnect, dupData, dupData, dupQdata, forceFloating, freezeNotify, get, get, getData, getData, getProperty, getProperty, getQdata, interfaceFindProperty, interfaceInstallProperty, isFloating, notify, notify, notifyByPspec, onNotify, ref, refSink, removeToggleRef, replaceData, replaceData, replaceQdata, runDispose, set, set, setData, setData, setDataFull, setDataFull, setProperty, setProperty, setQdata, setQdataFull, stealData, stealData, stealQdata, takeRef, thawNotify, unref, watchClosure, weakRef, weakUnref
Methods inherited from class ch.bailu.gtk.type.Pointer
asCPointer, cast, connectSignal, disconnectSignals, disconnectSignals, equals, hashCode, throwIfNull, throwNullPointerException, toString, unregisterCallbacks, unregisterCallbacks
Methods inherited from class ch.bailu.gtk.type.Type
asCPointer, asCPointer, asCPointerNotNull, asJnaPointer, asJnaPointer, asPointer, asPointer, cast, cast, throwIfNull
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface ch.bailu.gtk.type.PointerInterface
asCPointerNotNull, asJnaPointer, asPointer, isNotNull, isNull
-
Field Details
-
SIGNAL_ON_RESIZE
- See Also:
-
-
Constructor Details
-
DrawingArea
-
DrawingArea
public DrawingArea()Creates a new drawing area.
-
-
Method Details
-
getClassHandler
-
getContentHeight
public int getContentHeight()Retrieves the content height of the `GtkDrawingArea`.- Returns:
- The height requested for content of the drawing area
-
getContentWidth
public int getContentWidth()Retrieves the content width of the `GtkDrawingArea`.- Returns:
- The width requested for content of the drawing area
-
setContentHeight
public void setContentHeight(int 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
[method@Gtk.Widget.set_valign] to avoid that.
If the height is set to 0 (the default), the drawing area may disappear.- Parameters:
height
- the height of contents
-
setContentWidth
public void setContentWidth(int 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
[method@Gtk.Widget.set_halign] to avoid that.
If the width is set to 0 (the default), the drawing area may disappear.- Parameters:
width
- the width of contents
-
setDrawFunc
public void setDrawFunc(DrawingArea.OnDrawingAreaDrawFunc draw_func, @Nullable Pointer user_data, DrawingArea.OnDestroyNotify destroy) 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.- Parameters:
draw_func
- callback that lets you draw the drawing area's contentsuser_data
- user data passed to @draw_funcdestroy
- destroy notifier for @user_data
-
onResize
Connect to signal "resize".
SeeDrawingArea.OnResize.onResize(int, int)
for signal description.
FieldSIGNAL_ON_RESIZE
contains original signal name and can be used as resource reference.- Parameters:
signal
- callback function (lambda).- Returns:
SignalHandler
. Can be used to disconnect signal and to release callback function.
-
asAccessible
Implements interfaceAccessible
. Call this to get access to interface functions.- Overrides:
asAccessible
in classWidget
- Returns:
Accessible
-
asBuildable
Implements interfaceBuildable
. Call this to get access to interface functions.- Overrides:
asBuildable
in classWidget
- Returns:
Buildable
-
asConstraintTarget
Implements interfaceConstraintTarget
. Call this to get access to interface functions.- Overrides:
asConstraintTarget
in classWidget
- Returns:
ConstraintTarget
-
getTypeID
public static long getTypeID() -
getParentTypeID
public static long getParentTypeID() -
getTypeSize
-
getParentTypeSize
-
getInstanceSize
public static int getInstanceSize()
-