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
GtkGLArea
instance.Declaration
Swift
var ptr: UnsafeMutableRawPointer! { get }
-
gl_area_ptr
Default implementationTyped pointer to the underlying
GtkGLArea
instance.Default Implementation
Return the stored, untyped pointer as a typed pointer to the
GtkGLArea
instance.Declaration
Swift
var gl_area_ptr: UnsafeMutablePointer<GtkGLArea>! { get }
-
Required Initialiser for types conforming to
GLAreaProtocol
Declaration
Swift
init(raw: UnsafeMutableRawPointer)
-
bind(property:
Extension methodto: _: flags: transformFrom: transformTo: ) Bind a
GLAreaPropertyName
source 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 : ObjectProtocol
Parameters
source_property
the source property to bind
target
the target object to bind to
target_property
the target property to bind to
flags
the flags to pass to the
Binding
transform_from
ValueTransformer
to use for forward transformationtransform_to
ValueTransformer
to use for backwards transformationReturn Value
binding reference or
nil
in case of an error -
get(property:
Extension method) Get the value of a GLArea property
Declaration
Swift
@inlinable func get(property: GLAreaPropertyName) -> GLibObject.Value
Parameters
property
the 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
property
the 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
GLAreaSignalName
signalDeclaration
Swift
@discardableResult @inlinable func connect(signal s: GLAreaSignalName, flags f: ConnectFlags = ConnectFlags(0), handler h: @escaping SignalHandler) -> Int
Parameters
signal
The signal to connect
flags
The connection flags to use
data
A pointer to user data to provide to the callback
destroyData
A
GClosureNotify
C function to destroy the data pointed to byuserData
handler
The 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
GLAreaSignalName
signalDeclaration
Swift
@discardableResult @inlinable func connect(signal s: GLAreaSignalName, flags f: ConnectFlags = ConnectFlags(0), data userData: gpointer!, destroyData destructor: GClosureNotify? = nil, signalHandler h: @escaping GCallback) -> Int
Parameters
signal
The signal to connect
flags
The connection flags to use
data
A pointer to user data to provide to the callback
destroyData
A
GClosureNotify
C function to destroy the data pointed to byuserData
signalHandler
The C function to be called on the given signal
Return Value
The signal handler ID (always greater than 0 for successful connections)
-
createContextSignal
Extension 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-context
signalWarning
aonCreateContext
wrapper for this signal could not be generated because it contains unimplemented features: { (9) Record return type is not yet supported }Note
Instead, you can connectcreateContextSignal
using theconnect(signal:)
methodsDeclaration
Swift
static var createContextSignal: GLAreaSignalName { get }
Parameters
flags
Flags
unownedSelf
Reference to instance of self
handler
a newly created
GdkGLContext
; theGtkGLArea
widget will take ownership of the returned value. -
onRender(flags:
Extension methodhandler: ) Emitted every time the contents of the
GtkGLArea
should be redrawn.The
context
is bound to thearea
prior to emitting this function, and the buffers are painted to the window once the emission terminates.Note
This represents the underlyingrender
signalDeclaration
Swift
@discardableResult @inlinable func onRender(flags: ConnectFlags = ConnectFlags(0), handler: @escaping (_ unownedSelf: GLAreaRef, _ context: Gdk.GLContextRef) -> Bool) -> Int
Parameters
flags
Flags
unownedSelf
Reference to instance of self
context
the
GdkGLContext
used byarea
handler
true
to stop other handlers from being invoked for the event.false
to propagate the event further. Run the given callback whenever therender
signal is emitted -
renderSignal
Extension methodTyped
render
signal 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 underlyingresize
signalDeclaration
Swift
@discardableResult @inlinable func onResize(flags: ConnectFlags = ConnectFlags(0), handler: @escaping (_ unownedSelf: GLAreaRef, _ width: Int, _ height: Int) -> Void) -> Int
Parameters
flags
Flags
unownedSelf
Reference to instance of self
width
the width of the viewport
height
the height of the viewport
handler
The signal handler to call Run the given callback whenever the
resize
signal is emitted -
resizeSignal
Extension methodTyped
resize
signal 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 innotify
being 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-render
signalDeclaration
Swift
@discardableResult @inlinable func onNotifyAutoRender(flags: ConnectFlags = ConnectFlags(0), handler: @escaping (_ unownedSelf: GLAreaRef, _ pspec: ParamSpecRef) -> Void) -> Int
Parameters
flags
Flags
unownedSelf
Reference to instance of self
pspec
the
GParamSpec
of the property which changed.handler
The signal handler to call Run the given callback whenever the
notifyAutoRender
signal is emitted -
notifyAutoRenderSignal
Extension methodTyped
notify::auto-render
signal 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 innotify
being 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::context
signalDeclaration
Swift
@discardableResult @inlinable func onNotifyContext(flags: ConnectFlags = ConnectFlags(0), handler: @escaping (_ unownedSelf: GLAreaRef, _ pspec: ParamSpecRef) -> Void) -> Int
Parameters
flags
Flags
unownedSelf
Reference to instance of self
pspec
the
GParamSpec
of the property which changed.handler
The signal handler to call Run the given callback whenever the
notifyContext
signal is emitted -
notifyContextSignal
Extension methodTyped
notify::context
signal 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 innotify
being 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-buffer
signalDeclaration
Swift
@discardableResult @inlinable func onNotifyHasDepthBuffer(flags: ConnectFlags = ConnectFlags(0), handler: @escaping (_ unownedSelf: GLAreaRef, _ pspec: ParamSpecRef) -> Void) -> Int
Parameters
flags
Flags
unownedSelf
Reference to instance of self
pspec
the
GParamSpec
of the property which changed.handler
The signal handler to call Run the given callback whenever the
notifyHasDepthBuffer
signal is emitted -
notifyHasDepthBufferSignal
Extension methodTyped
notify::has-depth-buffer
signal 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 innotify
being 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-buffer
signalDeclaration
Swift
@discardableResult @inlinable func onNotifyHasStencilBuffer(flags: ConnectFlags = ConnectFlags(0), handler: @escaping (_ unownedSelf: GLAreaRef, _ pspec: ParamSpecRef) -> Void) -> Int
Parameters
flags
Flags
unownedSelf
Reference to instance of self
pspec
the
GParamSpec
of the property which changed.handler
The signal handler to call Run the given callback whenever the
notifyHasStencilBuffer
signal is emitted -
notifyHasStencilBufferSignal
Extension methodTyped
notify::has-stencil-buffer
signal 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 innotify
being 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-es
signalDeclaration
Swift
@discardableResult @inlinable func onNotifyUseEs(flags: ConnectFlags = ConnectFlags(0), handler: @escaping (_ unownedSelf: GLAreaRef, _ pspec: ParamSpecRef) -> Void) -> Int
Parameters
flags
Flags
unownedSelf
Reference to instance of self
pspec
the
GParamSpec
of the property which changed.handler
The signal handler to call Run the given callback whenever the
notifyUseEs
signal is emitted -
notifyUseEsSignal
Extension methodTyped
notify::use-es
signal for using theconnect(signal:)
methodsDeclaration
Swift
static var notifyUseEsSignal: GLAreaSignalName { get }
-
attachBuffers()
Extension methodBinds buffers to the framebuffer.
Ensures that the
area
framebuffer object is made the current draw and read target, and that all the required buffers for thearea
are 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
GdkGLContext
used 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
GtkGLArea
should use OpenGL ES.See [method
Gtk.GLArea.set_use_es
].Declaration
Swift
@inlinable func getUseEs() -> Bool
-
makeCurrent()
Extension methodEnsures that the
GdkGLContext
used byarea
is 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 afalse
value. The default behaviour is to emit [signalGtk.GLArea::render
] on each draw.Declaration
Swift
@inlinable func queueRender()
-
set(autoRender:
Extension method) Sets whether the
GtkGLArea
is in auto render mode.If
auto_render
istrue
the [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_render
isfalse
the 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
GtkGLArea
should use a depth buffer.If
has_depth_buffer
istrue
the 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
GtkGLArea
should use a stencil buffer.If
has_stencil_buffer
istrue
the 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
area
should create an OpenGL or an OpenGL ES context.You should check the capabilities of the
GdkGLContext
before drawing with either API.Declaration
Swift
@inlinable func set(useEs: Bool)
-
autoRender
Extension methodReturns whether the area is in auto render mode or not.
Declaration
Swift
@inlinable var autoRender: Bool { get nonmutating set }
-
context
Extension methodThe
GdkGLContext
used by theGtkGLArea
widget.The
GtkGLArea
widget is responsible for creating theGdkGLContext
instance. If you need to render with other kinds of buffers (stencil, depth, etc), use render buffers.Declaration
Swift
@inlinable var context: Gdk.GLContextRef! { get }
-
error
Extension methodGets the current error set on the
area
.Declaration
Swift
@inlinable var error: GLib.ErrorRef! { get nonmutating set }
-
hasDepthBuffer
Extension methodReturns whether the area has a depth buffer.
Declaration
Swift
@inlinable var hasDepthBuffer: Bool { get nonmutating set }
-
hasStencilBuffer
Extension methodReturns whether the area has a stencil buffer.
Declaration
Swift
@inlinable var hasStencilBuffer: Bool { get nonmutating set }
-
useEs
Extension methodReturns whether the
GtkGLArea
should use OpenGL ES.See [method
Gtk.GLArea.set_use_es
].Declaration
Swift
@inlinable var useEs: Bool { get nonmutating set }