GLAreaProtocol
public protocol GLAreaProtocol : WidgetProtocol
GtkGLArea is a widget that allows drawing with OpenGL.

GtkGLArea sets up its own [classGdk.GLContext], and creates a custom
GL framebuffer that the widget will do GL rendering onto. It also ensures
that this framebuffer is the default GL rendering target when rendering.
In order to draw, you have to connect to the [signalGtk.GLArea::render]
signal, or subclass GtkGLArea and override the GtkGLAreaClass.render
virtual function.
The GtkGLArea widget ensures that the GdkGLContext is associated with
the widget’s drawing area, and it is kept updated when the size and
position of the drawing area changes.
Drawing with GtkGLArea
The simplest way to draw using OpenGL commands in a GtkGLArea is to
create a widget instance and connect to the [signalGtk.GLArea::render] signal:
The render() function will be called when the GtkGLArea is ready
for you to draw its content:
static gboolean
render (GtkGLArea *area, GdkGLContext *context)
{
// inside this function it's safe to use GL; the given
// GdkGLContext has been made current to the drawable
// surface used by the `GtkGLArea` and the viewport has
// already been set to be the size of the allocation
// we can start by clearing the buffer
glClearColor (0, 0, 0, 0);
glClear (GL_COLOR_BUFFER_BIT);
// draw your object
// draw_an_object ();
// we completed our drawing; the draw commands will be
// flushed at the end of the signal emission chain, and
// the buffers will be drawn on the window
return TRUE;
}
void setup_glarea (void)
{
// create a GtkGLArea instance
GtkWidget *gl_area = gtk_gl_area_new ();
// connect to the "render" signal
g_signal_connect (gl_area, "render", G_CALLBACK (render), NULL);
}
If you need to initialize OpenGL state, e.g. buffer objects or
shaders, you should use the [signalGtk.Widget::realize] signal;
you can use the [signalGtk.Widget::unrealize] signal to clean up.
Since the GdkGLContext creation and initialization may fail, you
will need to check for errors, using [methodGtk.GLArea.get_error].
An example of how to safely initialize the GL state is:
static void
on_realize (GtkGLarea *area)
{
// We need to make the context current if we want to
// call GL API
gtk_gl_area_make_current (area);
// If there were errors during the initialization or
// when trying to make the context current, this
// function will return a GError for you to catch
if (gtk_gl_area_get_error (area) != NULL)
return;
// You can also use `gtk_gl_area_set_error()` in order
// to show eventual initialization errors on the
// GtkGLArea widget itself
GError *internal_error = NULL;
init_buffer_objects (&error);
if (error != NULL)
{
gtk_gl_area_set_error (area, error);
g_error_free (error);
return;
}
init_shaders (&error);
if (error != NULL)
{
gtk_gl_area_set_error (area, error);
g_error_free (error);
return;
}
}
If you need to change the options for creating the GdkGLContext
you should use the [signalGtk.GLArea::create-context] signal.
The GLAreaProtocol protocol exposes the methods and properties of an underlying GtkGLArea instance.
The default implementation of these can be found in the protocol extension below.
For a concrete class that implements these methods and properties, see GLArea.
Alternatively, use GLAreaRef as a lighweight, unowned reference if you already have an instance you just want to use.
-
Untyped pointer to the underlying
GtkGLAreainstance.Declaration
Swift
var ptr: UnsafeMutableRawPointer! { get } -
gl_area_ptrDefault implementationTyped pointer to the underlying
GtkGLAreainstance.Default Implementation
Return the stored, untyped pointer as a typed pointer to the
GtkGLAreainstance.Declaration
Swift
var gl_area_ptr: UnsafeMutablePointer<GtkGLArea>! { get } -
Required Initialiser for types conforming to
GLAreaProtocolDeclaration
Swift
init(raw: UnsafeMutableRawPointer)
-
bind(property:Extension methodto: _: flags: transformFrom: transformTo: ) Bind a
GLAreaPropertyNamesource property to a given target object.Declaration
Swift
@discardableResult @inlinable func bind<Q, T>(property source_property: GLAreaPropertyName, to target: T, _ target_property: Q, flags f: BindingFlags = .default, transformFrom transform_from: @escaping GLibObject.ValueTransformer = { $0.transform(destValue: $1) }, transformTo transform_to: @escaping GLibObject.ValueTransformer = { $0.transform(destValue: $1) }) -> BindingRef! where Q : PropertyNameProtocol, T : ObjectProtocolParameters
source_propertythe source property to bind
targetthe target object to bind to
target_propertythe target property to bind to
flagsthe flags to pass to the
Bindingtransform_fromValueTransformerto use for forward transformationtransform_toValueTransformerto use for backwards transformationReturn Value
binding reference or
nilin case of an error -
get(property:Extension method) Get the value of a GLArea property
Declaration
Swift
@inlinable func get(property: GLAreaPropertyName) -> GLibObject.ValueParameters
propertythe property to get the value for
Return Value
the value of the named property
-
set(property:Extension methodvalue: ) Set the value of a GLArea property. Note that this will only have an effect on properties that are writable and not construct-only!
Declaration
Swift
@inlinable func set(property: GLAreaPropertyName, value v: GLibObject.Value)Parameters
propertythe property to get the value for
Return Value
the value of the named property
-
connect(signal:Extension methodflags: handler: ) Connect a Swift signal handler to the given, typed
GLAreaSignalNamesignalDeclaration
Swift
@discardableResult @inlinable func connect(signal s: GLAreaSignalName, flags f: ConnectFlags = ConnectFlags(0), handler h: @escaping SignalHandler) -> IntParameters
signalThe signal to connect
flagsThe connection flags to use
dataA pointer to user data to provide to the callback
destroyDataA
GClosureNotifyC function to destroy the data pointed to byuserDatahandlerThe Swift signal handler (function or callback) to invoke on the given signal
Return Value
The signal handler ID (always greater than 0 for successful connections)
-
connect(signal:Extension methodflags: data: destroyData: signalHandler: ) Connect a C signal handler to the given, typed
GLAreaSignalNamesignalDeclaration
Swift
@discardableResult @inlinable func connect(signal s: GLAreaSignalName, flags f: ConnectFlags = ConnectFlags(0), data userData: gpointer!, destroyData destructor: GClosureNotify? = nil, signalHandler h: @escaping GCallback) -> IntParameters
signalThe signal to connect
flagsThe connection flags to use
dataA pointer to user data to provide to the callback
destroyDataA
GClosureNotifyC function to destroy the data pointed to byuserDatasignalHandlerThe C function to be called on the given signal
Return Value
The signal handler ID (always greater than 0 for successful connections)
-
createContextSignalExtension methodEmitted when the widget is being realized.
This allows you to override how the GL context is created. This is useful when you want to reuse an existing GL context, or if you want to try creating different kinds of GL options.
If context creation fails then the signal handler can use [method
Gtk.GLArea.set_error] to register a more detailed error of how the construction failed.Note
This represents the underlyingcreate-contextsignalWarning
aonCreateContextwrapper for this signal could not be generated because it contains unimplemented features: { (9) Record return type is not yet supported }Note
Instead, you can connectcreateContextSignalusing theconnect(signal:)methodsDeclaration
Swift
static var createContextSignal: GLAreaSignalName { get }Parameters
flagsFlags
unownedSelfReference to instance of self
handlera newly created
GdkGLContext; theGtkGLAreawidget will take ownership of the returned value. -
onRender(flags:Extension methodhandler: ) Emitted every time the contents of the
GtkGLAreashould be redrawn.The
contextis bound to theareaprior to emitting this function, and the buffers are painted to the window once the emission terminates.Note
This represents the underlyingrendersignalDeclaration
Swift
@discardableResult @inlinable func onRender(flags: ConnectFlags = ConnectFlags(0), handler: @escaping (_ unownedSelf: GLAreaRef, _ context: Gdk.GLContextRef) -> Bool) -> IntParameters
flagsFlags
unownedSelfReference to instance of self
contextthe
GdkGLContextused byareahandlertrueto stop other handlers from being invoked for the event.falseto propagate the event further. Run the given callback whenever therendersignal is emitted -
renderSignalExtension methodTyped
rendersignal for using theconnect(signal:)methodsDeclaration
Swift
static var renderSignal: GLAreaSignalName { get } -
onResize(flags:Extension methodhandler: ) Emitted once when the widget is realized, and then each time the widget is changed while realized.
This is useful in order to keep GL state up to date with the widget size, like for instance camera properties which may depend on the width/height ratio.
The GL context for the area is guaranteed to be current when this signal is emitted.
The default handler sets up the GL viewport.
Note
This represents the underlyingresizesignalDeclaration
Swift
@discardableResult @inlinable func onResize(flags: ConnectFlags = ConnectFlags(0), handler: @escaping (_ unownedSelf: GLAreaRef, _ width: Int, _ height: Int) -> Void) -> IntParameters
flagsFlags
unownedSelfReference to instance of self
widththe width of the viewport
heightthe height of the viewport
handlerThe signal handler to call Run the given callback whenever the
resizesignal is emitted -
resizeSignalExtension methodTyped
resizesignal for using theconnect(signal:)methodsDeclaration
Swift
static var resizeSignal: GLAreaSignalName { get } -
onNotifyAutoRender(flags:Extension methodhandler: ) The notify signal is emitted on an object when one of its properties has its value set through
g_object_set_property(),g_object_set(), et al.Note that getting this signal doesn’t itself guarantee that the value of the property has actually changed. When it is emitted is determined by the derived GObject class. If the implementor did not create the property with
G_PARAM_EXPLICIT_NOTIFY, then any call tog_object_set_property()results innotifybeing emitted, even if the new value is the same as the old. If they did passG_PARAM_EXPLICIT_NOTIFY, then this signal is emitted only when they explicitly callg_object_notify()org_object_notify_by_pspec(), and common practice is to do that only when the value has actually changed.This signal is typically used to obtain change notification for a single property, by specifying the property name as a detail in the
g_signal_connect()call, like this:(C Language Example):
g_signal_connect (text_view->buffer, "notify::paste-target-list", G_CALLBACK (gtk_text_view_target_list_notify), text_view)It is important to note that you must use canonical parameter names as detail strings for the notify signal.
Note
This represents the underlyingnotify::auto-rendersignalDeclaration
Swift
@discardableResult @inlinable func onNotifyAutoRender(flags: ConnectFlags = ConnectFlags(0), handler: @escaping (_ unownedSelf: GLAreaRef, _ pspec: ParamSpecRef) -> Void) -> IntParameters
flagsFlags
unownedSelfReference to instance of self
pspecthe
GParamSpecof the property which changed.handlerThe signal handler to call Run the given callback whenever the
notifyAutoRendersignal is emitted -
notifyAutoRenderSignalExtension methodTyped
notify::auto-rendersignal for using theconnect(signal:)methodsDeclaration
Swift
static var notifyAutoRenderSignal: GLAreaSignalName { get } -
onNotifyContext(flags:Extension methodhandler: ) The notify signal is emitted on an object when one of its properties has its value set through
g_object_set_property(),g_object_set(), et al.Note that getting this signal doesn’t itself guarantee that the value of the property has actually changed. When it is emitted is determined by the derived GObject class. If the implementor did not create the property with
G_PARAM_EXPLICIT_NOTIFY, then any call tog_object_set_property()results innotifybeing emitted, even if the new value is the same as the old. If they did passG_PARAM_EXPLICIT_NOTIFY, then this signal is emitted only when they explicitly callg_object_notify()org_object_notify_by_pspec(), and common practice is to do that only when the value has actually changed.This signal is typically used to obtain change notification for a single property, by specifying the property name as a detail in the
g_signal_connect()call, like this:(C Language Example):
g_signal_connect (text_view->buffer, "notify::paste-target-list", G_CALLBACK (gtk_text_view_target_list_notify), text_view)It is important to note that you must use canonical parameter names as detail strings for the notify signal.
Note
This represents the underlyingnotify::contextsignalDeclaration
Swift
@discardableResult @inlinable func onNotifyContext(flags: ConnectFlags = ConnectFlags(0), handler: @escaping (_ unownedSelf: GLAreaRef, _ pspec: ParamSpecRef) -> Void) -> IntParameters
flagsFlags
unownedSelfReference to instance of self
pspecthe
GParamSpecof the property which changed.handlerThe signal handler to call Run the given callback whenever the
notifyContextsignal is emitted -
notifyContextSignalExtension methodTyped
notify::contextsignal for using theconnect(signal:)methodsDeclaration
Swift
static var notifyContextSignal: GLAreaSignalName { get } -
onNotifyHasDepthBuffer(flags:Extension methodhandler: ) The notify signal is emitted on an object when one of its properties has its value set through
g_object_set_property(),g_object_set(), et al.Note that getting this signal doesn’t itself guarantee that the value of the property has actually changed. When it is emitted is determined by the derived GObject class. If the implementor did not create the property with
G_PARAM_EXPLICIT_NOTIFY, then any call tog_object_set_property()results innotifybeing emitted, even if the new value is the same as the old. If they did passG_PARAM_EXPLICIT_NOTIFY, then this signal is emitted only when they explicitly callg_object_notify()org_object_notify_by_pspec(), and common practice is to do that only when the value has actually changed.This signal is typically used to obtain change notification for a single property, by specifying the property name as a detail in the
g_signal_connect()call, like this:(C Language Example):
g_signal_connect (text_view->buffer, "notify::paste-target-list", G_CALLBACK (gtk_text_view_target_list_notify), text_view)It is important to note that you must use canonical parameter names as detail strings for the notify signal.
Note
This represents the underlyingnotify::has-depth-buffersignalDeclaration
Swift
@discardableResult @inlinable func onNotifyHasDepthBuffer(flags: ConnectFlags = ConnectFlags(0), handler: @escaping (_ unownedSelf: GLAreaRef, _ pspec: ParamSpecRef) -> Void) -> IntParameters
flagsFlags
unownedSelfReference to instance of self
pspecthe
GParamSpecof the property which changed.handlerThe signal handler to call Run the given callback whenever the
notifyHasDepthBuffersignal is emitted -
notifyHasDepthBufferSignalExtension methodTyped
notify::has-depth-buffersignal for using theconnect(signal:)methodsDeclaration
Swift
static var notifyHasDepthBufferSignal: GLAreaSignalName { get } -
onNotifyHasStencilBuffer(flags:Extension methodhandler: ) The notify signal is emitted on an object when one of its properties has its value set through
g_object_set_property(),g_object_set(), et al.Note that getting this signal doesn’t itself guarantee that the value of the property has actually changed. When it is emitted is determined by the derived GObject class. If the implementor did not create the property with
G_PARAM_EXPLICIT_NOTIFY, then any call tog_object_set_property()results innotifybeing emitted, even if the new value is the same as the old. If they did passG_PARAM_EXPLICIT_NOTIFY, then this signal is emitted only when they explicitly callg_object_notify()org_object_notify_by_pspec(), and common practice is to do that only when the value has actually changed.This signal is typically used to obtain change notification for a single property, by specifying the property name as a detail in the
g_signal_connect()call, like this:(C Language Example):
g_signal_connect (text_view->buffer, "notify::paste-target-list", G_CALLBACK (gtk_text_view_target_list_notify), text_view)It is important to note that you must use canonical parameter names as detail strings for the notify signal.
Note
This represents the underlyingnotify::has-stencil-buffersignalDeclaration
Swift
@discardableResult @inlinable func onNotifyHasStencilBuffer(flags: ConnectFlags = ConnectFlags(0), handler: @escaping (_ unownedSelf: GLAreaRef, _ pspec: ParamSpecRef) -> Void) -> IntParameters
flagsFlags
unownedSelfReference to instance of self
pspecthe
GParamSpecof the property which changed.handlerThe signal handler to call Run the given callback whenever the
notifyHasStencilBuffersignal is emitted -
notifyHasStencilBufferSignalExtension methodTyped
notify::has-stencil-buffersignal for using theconnect(signal:)methodsDeclaration
Swift
static var notifyHasStencilBufferSignal: GLAreaSignalName { get } -
onNotifyUseEs(flags:Extension methodhandler: ) The notify signal is emitted on an object when one of its properties has its value set through
g_object_set_property(),g_object_set(), et al.Note that getting this signal doesn’t itself guarantee that the value of the property has actually changed. When it is emitted is determined by the derived GObject class. If the implementor did not create the property with
G_PARAM_EXPLICIT_NOTIFY, then any call tog_object_set_property()results innotifybeing emitted, even if the new value is the same as the old. If they did passG_PARAM_EXPLICIT_NOTIFY, then this signal is emitted only when they explicitly callg_object_notify()org_object_notify_by_pspec(), and common practice is to do that only when the value has actually changed.This signal is typically used to obtain change notification for a single property, by specifying the property name as a detail in the
g_signal_connect()call, like this:(C Language Example):
g_signal_connect (text_view->buffer, "notify::paste-target-list", G_CALLBACK (gtk_text_view_target_list_notify), text_view)It is important to note that you must use canonical parameter names as detail strings for the notify signal.
Note
This represents the underlyingnotify::use-essignalDeclaration
Swift
@discardableResult @inlinable func onNotifyUseEs(flags: ConnectFlags = ConnectFlags(0), handler: @escaping (_ unownedSelf: GLAreaRef, _ pspec: ParamSpecRef) -> Void) -> IntParameters
flagsFlags
unownedSelfReference to instance of self
pspecthe
GParamSpecof the property which changed.handlerThe signal handler to call Run the given callback whenever the
notifyUseEssignal is emitted -
notifyUseEsSignalExtension methodTyped
notify::use-essignal for using theconnect(signal:)methodsDeclaration
Swift
static var notifyUseEsSignal: GLAreaSignalName { get }
-
attachBuffers()Extension methodBinds buffers to the framebuffer.
Ensures that the
areaframebuffer object is made the current draw and read target, and that all the required buffers for theareaare created and bound to the framebuffer.This function is automatically called before emitting the [signal
Gtk.GLArea::render] signal, and doesn’t normally need to be called by application code.Declaration
Swift
@inlinable func attachBuffers() -
getAutoRender()Extension methodReturns whether the area is in auto render mode or not.
Declaration
Swift
@inlinable func getAutoRender() -> Bool -
getContext()Extension methodRetrieves the
GdkGLContextused byarea.Declaration
Swift
@inlinable func getContext() -> Gdk.GLContextRef! -
getError()Extension methodGets the current error set on the
area.Declaration
Swift
@inlinable func getError() -> GLib.ErrorRef! -
getHasDepthBuffer()Extension methodReturns whether the area has a depth buffer.
Declaration
Swift
@inlinable func getHasDepthBuffer() -> Bool -
getHasStencilBuffer()Extension methodReturns whether the area has a stencil buffer.
Declaration
Swift
@inlinable func getHasStencilBuffer() -> Bool -
getRequiredVersion(major:Extension methodminor: ) Retrieves the required version of OpenGL.
See [method
Gtk.GLArea.set_required_version].Declaration
Swift
@inlinable func getRequiredVersion(major: UnsafeMutablePointer<gint>!, minor: UnsafeMutablePointer<gint>!) -
getUseEs()Extension methodReturns whether the
GtkGLAreashould use OpenGL ES.See [method
Gtk.GLArea.set_use_es].Declaration
Swift
@inlinable func getUseEs() -> Bool -
makeCurrent()Extension methodEnsures that the
GdkGLContextused byareais associated with theGtkGLArea.This function is automatically called before emitting the [signal
Gtk.GLArea::render] signal, and doesn’t normally need to be called by application code.Declaration
Swift
@inlinable func makeCurrent() -
queueRender()Extension methodMarks the currently rendered data (if any) as invalid, and queues a redraw of the widget.
This ensures that the [signal
Gtk.GLArea::render] signal is emitted during the draw.This is only needed when [method
Gtk.GLArea.set_auto_render] has been called with afalsevalue. The default behaviour is to emit [signalGtk.GLArea::render] on each draw.Declaration
Swift
@inlinable func queueRender() -
set(autoRender:Extension method) Sets whether the
GtkGLAreais in auto render mode.If
auto_renderistruethe [signalGtk.GLArea::render] signal will be emitted every time the widget draws. This is the default and is useful if drawing the widget is faster.If
auto_renderisfalsethe data from previous rendering is kept around and will be used for drawing the widget the next time, unless the window is resized. In order to force a rendering [methodGtk.GLArea.queue_render] must be called. This mode is useful when the scene changes seldom, but takes a long time to redraw.Declaration
Swift
@inlinable func set(autoRender: Bool) -
set(error:Extension method) Sets an error on the area which will be shown instead of the GL rendering.
This is useful in the [signal
Gtk.GLArea::create-context] signal if GL context creation fails.Declaration
Swift
@inlinable func set(error: ErrorRef? = nil) -
set(error:Extension method) Sets an error on the area which will be shown instead of the GL rendering.
This is useful in the [signal
Gtk.GLArea::create-context] signal if GL context creation fails.Declaration
Swift
@inlinable func set<GLibErrorT>(error: GLibErrorT?) where GLibErrorT : ErrorProtocol -
set(hasDepthBuffer:Extension method) Sets whether the
GtkGLAreashould use a depth buffer.If
has_depth_bufferistruethe widget will allocate and enable a depth buffer for the target framebuffer. Otherwise there will be none.Declaration
Swift
@inlinable func set(hasDepthBuffer: Bool) -
set(hasStencilBuffer:Extension method) Sets whether the
GtkGLAreashould use a stencil buffer.If
has_stencil_bufferistruethe widget will allocate and enable a stencil buffer for the target framebuffer. Otherwise there will be none.Declaration
Swift
@inlinable func set(hasStencilBuffer: Bool) -
setRequiredVersion(major:Extension methodminor: ) Sets the required version of OpenGL to be used when creating the context for the widget.
This function must be called before the area has been realized.
Declaration
Swift
@inlinable func setRequiredVersion(major: Int, minor: Int) -
set(useEs:Extension method) Sets whether the
areashould create an OpenGL or an OpenGL ES context.You should check the capabilities of the
GdkGLContextbefore drawing with either API.Declaration
Swift
@inlinable func set(useEs: Bool) -
autoRenderExtension methodReturns whether the area is in auto render mode or not.
Declaration
Swift
@inlinable var autoRender: Bool { get nonmutating set } -
contextExtension methodThe
GdkGLContextused by theGtkGLAreawidget.The
GtkGLAreawidget is responsible for creating theGdkGLContextinstance. If you need to render with other kinds of buffers (stencil, depth, etc), use render buffers.Declaration
Swift
@inlinable var context: Gdk.GLContextRef! { get } -
errorExtension methodGets the current error set on the
area.Declaration
Swift
@inlinable var error: GLib.ErrorRef! { get nonmutating set } -
hasDepthBufferExtension methodReturns whether the area has a depth buffer.
Declaration
Swift
@inlinable var hasDepthBuffer: Bool { get nonmutating set } -
hasStencilBufferExtension methodReturns whether the area has a stencil buffer.
Declaration
Swift
@inlinable var hasStencilBuffer: Bool { get nonmutating set } -
useEsExtension methodReturns whether the
GtkGLAreashould use OpenGL ES.See [method
Gtk.GLArea.set_use_es].Declaration
Swift
@inlinable var useEs: Bool { get nonmutating set }
View on GitHub
Install in Dash
GLAreaProtocol Protocol Reference