gi-gtk-3.0.34: 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.GLArea

Description

GLArea is a widget that allows drawing with OpenGL.

GLArea sets up its own GLContext for the window it creates, 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 render signal, or subclass GLArea and override the gtkGLAreaClass.render() virtual function.

The GLArea widget ensures that the GLContext 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 GLArea is to create a widget instance and connect to the render signal:

C code

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

The render() function will be called when the GLArea is ready for you to draw its content:

C code

 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;
 }

If you need to initialize OpenGL state, e.g. buffer objects or shaders, you should use the realize signal; you can use the unrealize signal to clean up. Since the GLContext creation and initialization may fail, you will need to check for errors, using gLAreaGetError. An example of how to safely initialize the GL state is:

C code

 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 GLContext you should use the createContext signal.

Since: 3.16

Synopsis

Exported types

newtype GLArea Source #

Memory-managed wrapper type.

Constructors

GLArea (ManagedPtr GLArea) 

Instances

Instances details
Eq GLArea Source # 
Instance details

Defined in GI.Gtk.Objects.GLArea

Methods

(==) :: GLArea -> GLArea -> Bool

(/=) :: GLArea -> GLArea -> Bool

GObject GLArea Source # 
Instance details

Defined in GI.Gtk.Objects.GLArea

ManagedPtrNewtype GLArea Source # 
Instance details

Defined in GI.Gtk.Objects.GLArea

Methods

toManagedPtr :: GLArea -> ManagedPtr GLArea

TypedObject GLArea Source # 
Instance details

Defined in GI.Gtk.Objects.GLArea

Methods

glibType :: IO GType

IsGValue GLArea Source #

Convert GLArea to and from GValue with toGValue and fromGValue.

Instance details

Defined in GI.Gtk.Objects.GLArea

Methods

toGValue :: GLArea -> IO GValue

fromGValue :: GValue -> IO GLArea

HasParentTypes GLArea Source # 
Instance details

Defined in GI.Gtk.Objects.GLArea

type ParentTypes GLArea Source # 
Instance details

Defined in GI.Gtk.Objects.GLArea

type ParentTypes GLArea = '[Widget, Object, ImplementorIface, Buildable]

class (GObject o, IsDescendantOf GLArea o) => IsGLArea o Source #

Type class for types which can be safely cast to GLArea, for instance with toGLArea.

Instances

Instances details
(GObject o, IsDescendantOf GLArea o) => IsGLArea o Source # 
Instance details

Defined in GI.Gtk.Objects.GLArea

toGLArea :: (MonadIO m, IsGLArea o) => o -> m GLArea Source #

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

Methods

Overloaded methods

attachBuffers

gLAreaAttachBuffers Source #

Arguments

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

area: a GLArea

-> m () 

Ensures that the area framebuffer object is made the current draw and read target, and that all the required buffers for the area are created and bound to the frambuffer.

This function is automatically called before emitting the render signal, and doesn't normally need to be called by application code.

Since: 3.16

getAutoRender

gLAreaGetAutoRender Source #

Arguments

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

area: a GLArea

-> m Bool

Returns: True if the area is auto rendering, False otherwise

Returns whether the area is in auto render mode or not.

Since: 3.16

getContext

gLAreaGetContext Source #

Arguments

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

area: a GLArea

-> m GLContext

Returns: the GLContext

Retrieves the GLContext used by area.

Since: 3.16

getError

gLAreaGetError Source #

Arguments

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

area: a GLArea

-> m (Maybe GError)

Returns: the GError or Nothing

Gets the current error set on the area.

Since: 3.16

getHasAlpha

gLAreaGetHasAlpha Source #

Arguments

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

area: a GLArea

-> m Bool

Returns: True if the area has an alpha component, False otherwise

Returns whether the area has an alpha component.

Since: 3.16

getHasDepthBuffer

gLAreaGetHasDepthBuffer Source #

Arguments

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

area: a GLArea

-> m Bool

Returns: True if the area has a depth buffer, False otherwise

Returns whether the area has a depth buffer.

Since: 3.16

getHasStencilBuffer

gLAreaGetHasStencilBuffer Source #

Arguments

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

area: a GLArea

-> m Bool

Returns: True if the area has a stencil buffer, False otherwise

Returns whether the area has a stencil buffer.

Since: 3.16

getRequiredVersion

gLAreaGetRequiredVersion Source #

Arguments

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

area: a GLArea

-> m (Int32, Int32) 

Retrieves the required version of OpenGL set using gLAreaSetRequiredVersion.

Since: 3.16

getUseEs

gLAreaGetUseEs Source #

Arguments

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

area: a GLArea

-> m Bool

Returns: True if the GLArea should create an OpenGL ES context and False otherwise

Retrieves the value set by gLAreaSetUseEs.

Since: 3.22

makeCurrent

gLAreaMakeCurrent Source #

Arguments

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

area: a GLArea

-> m () 

Ensures that the GLContext used by area is associated with the GLArea.

This function is automatically called before emitting the render signal, and doesn't normally need to be called by application code.

Since: 3.16

new

gLAreaNew Source #

Arguments

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

Returns: a new GLArea

Creates a new GLArea widget.

Since: 3.16

queueRender

gLAreaQueueRender Source #

Arguments

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

area: a GLArea

-> m () 

Marks the currently rendered data (if any) as invalid, and queues a redraw of the widget, ensuring that the render signal is emitted during the draw.

This is only needed when the gLAreaSetAutoRender has been called with a False value. The default behaviour is to emit render on each draw.

Since: 3.16

setAutoRender

gLAreaSetAutoRender Source #

Arguments

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

area: a GLArea

-> Bool

autoRender: a boolean

-> m () 

If autoRender is True the render signal will be emitted every time the widget draws. This is the default and is useful if drawing the widget is faster.

If autoRender is False 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 gLAreaQueueRender must be called. This mode is useful when the scene changes seldomly, but takes a long time to redraw.

Since: 3.16

setError

gLAreaSetError Source #

Arguments

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

area: a GLArea

-> Maybe GError

error: a new GError, or Nothing to unset the error

-> m () 

Sets an error on the area which will be shown instead of the GL rendering. This is useful in the createContext signal if GL context creation fails.

Since: 3.16

setHasAlpha

gLAreaSetHasAlpha Source #

Arguments

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

area: a GLArea

-> Bool

hasAlpha: True to add an alpha component

-> m () 

If hasAlpha is True the buffer allocated by the widget will have an alpha channel component, and when rendering to the window the result will be composited over whatever is below the widget.

If hasAlpha is False there will be no alpha channel, and the buffer will fully replace anything below the widget.

Since: 3.16

setHasDepthBuffer

gLAreaSetHasDepthBuffer Source #

Arguments

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

area: a GLArea

-> Bool

hasDepthBuffer: True to add a depth buffer

-> m () 

If hasDepthBuffer is True the widget will allocate and enable a depth buffer for the target framebuffer. Otherwise there will be none.

Since: 3.16

setHasStencilBuffer

gLAreaSetHasStencilBuffer Source #

Arguments

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

area: a GLArea

-> Bool

hasStencilBuffer: True to add a stencil buffer

-> m () 

If hasStencilBuffer is True the widget will allocate and enable a stencil buffer for the target framebuffer. Otherwise there will be none.

Since: 3.16

setRequiredVersion

gLAreaSetRequiredVersion Source #

Arguments

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

area: a GLArea

-> Int32

major: the major version

-> Int32

minor: the minor version

-> m () 

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.

Since: 3.16

setUseEs

gLAreaSetUseEs Source #

Arguments

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

area: a GLArea

-> Bool

useEs: whether to use OpenGL or OpenGL ES

-> m () 

Sets whether the area should create an OpenGL or an OpenGL ES context.

You should check the capabilities of the GLContext before drawing with either API.

Since: 3.22

Properties

autoRender

If set to True the render signal will be emitted every time the widget draws. This is the default and is useful if drawing the widget is faster.

If set to False 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 gLAreaQueueRender must be called. This mode is useful when the scene changes seldomly, but takes a long time to redraw.

Since: 3.16

constructGLAreaAutoRender :: (IsGLArea o, MonadIO m) => Bool -> m (GValueConstruct o) Source #

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

getGLAreaAutoRender :: (MonadIO m, IsGLArea o) => o -> m Bool Source #

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

get gLArea #autoRender

setGLAreaAutoRender :: (MonadIO m, IsGLArea o) => o -> Bool -> m () Source #

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

set gLArea [ #autoRender := value ]

context

The GLContext used by the GLArea widget.

The GLArea widget is responsible for creating the GLContext instance. If you need to render with other kinds of buffers (stencil, depth, etc), use render buffers.

Since: 3.16

getGLAreaContext :: (MonadIO m, IsGLArea o) => o -> m GLContext Source #

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

get gLArea #context

hasAlpha

If set to True the buffer allocated by the widget will have an alpha channel component, and when rendering to the window the result will be composited over whatever is below the widget.

If set to False there will be no alpha channel, and the buffer will fully replace anything below the widget.

Since: 3.16

constructGLAreaHasAlpha :: (IsGLArea o, MonadIO m) => Bool -> m (GValueConstruct o) Source #

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

getGLAreaHasAlpha :: (MonadIO m, IsGLArea o) => o -> m Bool Source #

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

get gLArea #hasAlpha

setGLAreaHasAlpha :: (MonadIO m, IsGLArea o) => o -> Bool -> m () Source #

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

set gLArea [ #hasAlpha := value ]

hasDepthBuffer

If set to True the widget will allocate and enable a depth buffer for the target framebuffer.

Since: 3.16

constructGLAreaHasDepthBuffer :: (IsGLArea o, MonadIO m) => Bool -> m (GValueConstruct o) Source #

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

getGLAreaHasDepthBuffer :: (MonadIO m, IsGLArea o) => o -> m Bool Source #

Get the value of the “has-depth-buffer” property. When overloading is enabled, this is equivalent to

get gLArea #hasDepthBuffer

setGLAreaHasDepthBuffer :: (MonadIO m, IsGLArea o) => o -> Bool -> m () Source #

Set the value of the “has-depth-buffer” property. When overloading is enabled, this is equivalent to

set gLArea [ #hasDepthBuffer := value ]

hasStencilBuffer

If set to True the widget will allocate and enable a stencil buffer for the target framebuffer.

Since: 3.16

constructGLAreaHasStencilBuffer :: (IsGLArea o, MonadIO m) => Bool -> m (GValueConstruct o) Source #

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

getGLAreaHasStencilBuffer :: (MonadIO m, IsGLArea o) => o -> m Bool Source #

Get the value of the “has-stencil-buffer” property. When overloading is enabled, this is equivalent to

get gLArea #hasStencilBuffer

setGLAreaHasStencilBuffer :: (MonadIO m, IsGLArea o) => o -> Bool -> m () Source #

Set the value of the “has-stencil-buffer” property. When overloading is enabled, this is equivalent to

set gLArea [ #hasStencilBuffer := value ]

useEs

If set to True the widget will try to create a GLContext using OpenGL ES instead of OpenGL.

See also: gLContextSetUseEs

Since: 3.22

constructGLAreaUseEs :: (IsGLArea o, MonadIO m) => Bool -> m (GValueConstruct o) Source #

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

getGLAreaUseEs :: (MonadIO m, IsGLArea o) => o -> m Bool Source #

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

get gLArea #useEs

setGLAreaUseEs :: (MonadIO m, IsGLArea o) => o -> Bool -> m () Source #

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

set gLArea [ #useEs := value ]

Signals

createContext

type C_GLAreaCreateContextCallback = Ptr () -> Ptr () -> IO (Ptr GLContext) Source #

Type for the callback on the (unwrapped) C side.

type GLAreaCreateContextCallback Source #

Arguments

 = IO GLContext

Returns: a newly created GLContext; the GLArea widget will take ownership of the returned value.

The createContext signal is emitted when the widget is being realized, and 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 gLAreaSetError to register a more detailed error of how the construction failed.

Since: 3.16

afterGLAreaCreateContext :: (IsGLArea a, MonadIO m) => a -> GLAreaCreateContextCallback -> m SignalHandlerId Source #

Connect a signal handler for the createContext signal, to be run after the default handler. When overloading is enabled, this is equivalent to

after gLArea #createContext callback

onGLAreaCreateContext :: (IsGLArea a, MonadIO m) => a -> GLAreaCreateContextCallback -> m SignalHandlerId Source #

Connect a signal handler for the createContext signal, to be run before the default handler. When overloading is enabled, this is equivalent to

on gLArea #createContext callback

render

type C_GLAreaRenderCallback = Ptr () -> Ptr GLContext -> Ptr () -> IO CInt Source #

Type for the callback on the (unwrapped) C side.

type GLAreaRenderCallback Source #

Arguments

 = GLContext

context: the GLContext used by area

-> IO Bool

Returns: True to stop other handlers from being invoked for the event. False to propagate the event further.

The render signal is emitted every time the contents of the GLArea should be redrawn.

The context is bound to the area prior to emitting this function, and the buffers are painted to the window once the emission terminates.

Since: 3.16

afterGLAreaRender :: (IsGLArea a, MonadIO m) => a -> GLAreaRenderCallback -> m SignalHandlerId Source #

Connect a signal handler for the render signal, to be run after the default handler. When overloading is enabled, this is equivalent to

after gLArea #render callback

genClosure_GLAreaRender :: MonadIO m => GLAreaRenderCallback -> m (GClosure C_GLAreaRenderCallback) Source #

Wrap the callback into a GClosure.

mk_GLAreaRenderCallback :: C_GLAreaRenderCallback -> IO (FunPtr C_GLAreaRenderCallback) Source #

Generate a function pointer callable from C code, from a C_GLAreaRenderCallback.

noGLAreaRenderCallback :: Maybe GLAreaRenderCallback Source #

A convenience synonym for Nothing :: Maybe GLAreaRenderCallback.

onGLAreaRender :: (IsGLArea a, MonadIO m) => a -> GLAreaRenderCallback -> m SignalHandlerId Source #

Connect a signal handler for the render signal, to be run before the default handler. When overloading is enabled, this is equivalent to

on gLArea #render callback

resize

type C_GLAreaResizeCallback = Ptr () -> Int32 -> Int32 -> Ptr () -> IO () Source #

Type for the callback on the (unwrapped) C side.

type GLAreaResizeCallback Source #

Arguments

 = Int32

width: the width of the viewport

-> Int32

height: the height of the viewport

-> IO () 

The resize signal is 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.

Since: 3.16

afterGLAreaResize :: (IsGLArea a, MonadIO m) => a -> GLAreaResizeCallback -> m SignalHandlerId Source #

Connect a signal handler for the resize signal, to be run after the default handler. When overloading is enabled, this is equivalent to

after gLArea #resize callback

genClosure_GLAreaResize :: MonadIO m => GLAreaResizeCallback -> m (GClosure C_GLAreaResizeCallback) Source #

Wrap the callback into a GClosure.

mk_GLAreaResizeCallback :: C_GLAreaResizeCallback -> IO (FunPtr C_GLAreaResizeCallback) Source #

Generate a function pointer callable from C code, from a C_GLAreaResizeCallback.

noGLAreaResizeCallback :: Maybe GLAreaResizeCallback Source #

A convenience synonym for Nothing :: Maybe GLAreaResizeCallback.

onGLAreaResize :: (IsGLArea a, MonadIO m) => a -> GLAreaResizeCallback -> m SignalHandlerId Source #

Connect a signal handler for the resize signal, to be run before the default handler. When overloading is enabled, this is equivalent to

on gLArea #resize callback