1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gtk.GLArea;
26 
27 private import gdk.GLContext;
28 private import glib.ConstructionException;
29 private import glib.ErrorG;
30 private import gobject.ObjectG;
31 private import gobject.Signals;
32 private import gtk.Widget;
33 public  import gtkc.gdktypes;
34 private import gtkc.gtk;
35 public  import gtkc.gtktypes;
36 
37 
38 /**
39  * #GtkGLArea is a widget that allows drawing with OpenGL.
40  * 
41  * #GtkGLArea sets up its own #GdkGLContext for the window it creates, and
42  * creates a custom GL framebuffer that the widget will do GL rendering onto.
43  * It also ensures that this framebuffer is the default GL rendering target
44  * when rendering.
45  * 
46  * In order to draw, you have to connect to the #GtkGLArea::render signal,
47  * or subclass #GtkGLArea and override the @GtkGLAreaClass.render() virtual
48  * function.
49  * 
50  * The #GtkGLArea widget ensures that the #GdkGLContext is associated with
51  * the widget's drawing area, and it is kept updated when the size and
52  * position of the drawing area changes.
53  * 
54  * ## Drawing with GtkGLArea ##
55  * 
56  * The simplest way to draw using OpenGL commands in a #GtkGLArea is to
57  * create a widget instance and connect to the #GtkGLArea::render signal:
58  * 
59  * |[<!-- language="C" -->
60  * // create a GtkGLArea instance
61  * GtkWidget *gl_area = gtk_gl_area_new ();
62  * 
63  * // connect to the "render" signal
64  * g_signal_connect (gl_area, "render", G_CALLBACK (render), NULL);
65  * ]|
66  * 
67  * The `render()` function will be called when the #GtkGLArea is ready
68  * for you to draw its content:
69  * 
70  * |[<!-- language="C" -->
71  * static gboolean
72  * render (GtkGLArea *area, GdkGLContext *context)
73  * {
74  * // inside this function it's safe to use GL; the given
75  * // #GdkGLContext has been made current to the drawable
76  * // surface used by the #GtkGLArea and the viewport has
77  * // already been set to be the size of the allocation
78  * 
79  * // we can start by clearing the buffer
80  * glClearColor (0, 0, 0, 0);
81  * glClear (GL_COLOR_BUFFER_BIT);
82  * 
83  * // draw your object
84  * draw_an_object ();
85  * 
86  * // we completed our drawing; the draw commands will be
87  * // flushed at the end of the signal emission chain, and
88  * // the buffers will be drawn on the window
89  * return TRUE;
90  * }
91  * ]|
92  * 
93  * If you need to initialize OpenGL state, e.g. buffer objects or
94  * shaders, you should use the #GtkWidget::realize signal; you
95  * can use the #GtkWidget::unrealize signal to clean up. Since the
96  * #GdkGLContext creation and initialization may fail, you will
97  * need to check for errors, using gtk_gl_area_get_error(). An example
98  * of how to safely initialize the GL state is:
99  * 
100  * |[<!-- language="C" -->
101  * static void
102  * on_realize (GtkGLarea *area)
103  * {
104  * // We need to make the context current if we want to
105  * // call GL API
106  * gtk_gl_area_make_current (area);
107  * 
108  * // If there were errors during the initialization or
109  * // when trying to make the context current, this
110  * // function will return a #GError for you to catch
111  * if (gtk_gl_area_get_error (area) != NULL)
112  * return;
113  * 
114  * // You can also use gtk_gl_area_set_error() in order
115  * // to show eventual initialization errors on the
116  * // GtkGLArea widget itself
117  * GError *internal_error = NULL;
118  * init_buffer_objects (&error);
119  * if (error != NULL)
120  * {
121  * gtk_gl_area_set_error (area, error);
122  * g_error_free (error);
123  * return;
124  * }
125  * 
126  * init_shaders (&error);
127  * if (error != NULL)
128  * {
129  * gtk_gl_area_set_error (area, error);
130  * g_error_free (error);
131  * return;
132  * }
133  * }
134  * ]|
135  * 
136  * If you need to change the options for creating the #GdkGLContext
137  * you should use the #GtkGLArea::create-context signal.
138  *
139  * Since: 3.16
140  */
141 public class GLArea : Widget
142 {
143 	/** the main Gtk struct */
144 	protected GtkGLArea* gtkGLArea;
145 
146 	/** Get the main Gtk struct */
147 	public GtkGLArea* getGLAreaStruct()
148 	{
149 		return gtkGLArea;
150 	}
151 
152 	/** the main Gtk struct as a void* */
153 	protected override void* getStruct()
154 	{
155 		return cast(void*)gtkGLArea;
156 	}
157 
158 	protected override void setStruct(GObject* obj)
159 	{
160 		gtkGLArea = cast(GtkGLArea*)obj;
161 		super.setStruct(obj);
162 	}
163 
164 	/**
165 	 * Sets our main struct and passes it to the parent class.
166 	 */
167 	public this (GtkGLArea* gtkGLArea, bool ownedRef = false)
168 	{
169 		this.gtkGLArea = gtkGLArea;
170 		super(cast(GtkWidget*)gtkGLArea, ownedRef);
171 	}
172 
173 
174 	/** */
175 	public static GType getType()
176 	{
177 		return gtk_gl_area_get_type();
178 	}
179 
180 	/**
181 	 * Creates a new #GtkGLArea widget.
182 	 *
183 	 * Return: the newly created #GtkGLArea
184 	 *
185 	 * Since: 3.16
186 	 *
187 	 * Throws: ConstructionException GTK+ fails to create the object.
188 	 */
189 	public this()
190 	{
191 		auto p = gtk_gl_area_new();
192 		
193 		if(p is null)
194 		{
195 			throw new ConstructionException("null returned by new");
196 		}
197 		
198 		this(cast(GtkGLArea*) p, true);
199 	}
200 
201 	/**
202 	 * Ensures that the @area framebuffer object is made the current draw
203 	 * and read target, and that all the required buffers for the @area
204 	 * are created and bound to the frambuffer.
205 	 *
206 	 * This function is automatically called before emitting the
207 	 * #GtkGLArea::render signal, and doesn't normally need to be called
208 	 * by application code.
209 	 *
210 	 * Since: 3.16
211 	 */
212 	public void attachBuffers()
213 	{
214 		gtk_gl_area_attach_buffers(gtkGLArea);
215 	}
216 
217 	/**
218 	 * Returns whether the area is in auto render mode or not.
219 	 *
220 	 * Return: %TRUE if the @area is auto rendering, %FALSE otherwise
221 	 *
222 	 * Since: 3.16
223 	 */
224 	public bool getAutoRender()
225 	{
226 		return gtk_gl_area_get_auto_render(gtkGLArea) != 0;
227 	}
228 
229 	/**
230 	 * Retrieves the #GdkGLContext used by @area.
231 	 *
232 	 * Return: the #GdkGLContext
233 	 *
234 	 * Since: 3.16
235 	 */
236 	public GLContext getContext()
237 	{
238 		auto p = gtk_gl_area_get_context(gtkGLArea);
239 		
240 		if(p is null)
241 		{
242 			return null;
243 		}
244 		
245 		return ObjectG.getDObject!(GLContext)(cast(GdkGLContext*) p);
246 	}
247 
248 	/**
249 	 * Gets the current error set on the @area.
250 	 *
251 	 * Return: the #GError or %NULL
252 	 *
253 	 * Since: 3.16
254 	 */
255 	public ErrorG getError()
256 	{
257 		auto p = gtk_gl_area_get_error(gtkGLArea);
258 		
259 		if(p is null)
260 		{
261 			return null;
262 		}
263 		
264 		return new ErrorG(cast(GError*) p);
265 	}
266 
267 	/**
268 	 * Returns whether the area has an alpha component.
269 	 *
270 	 * Return: %TRUE if the @area has an alpha component, %FALSE otherwise
271 	 *
272 	 * Since: 3.16
273 	 */
274 	public bool getHasAlpha()
275 	{
276 		return gtk_gl_area_get_has_alpha(gtkGLArea) != 0;
277 	}
278 
279 	/**
280 	 * Returns whether the area has a depth buffer.
281 	 *
282 	 * Return: %TRUE if the @area has a depth buffer, %FALSE otherwise
283 	 *
284 	 * Since: 3.16
285 	 */
286 	public bool getHasDepthBuffer()
287 	{
288 		return gtk_gl_area_get_has_depth_buffer(gtkGLArea) != 0;
289 	}
290 
291 	/**
292 	 * Returns whether the area has a stencil buffer.
293 	 *
294 	 * Return: %TRUE if the @area has a stencil buffer, %FALSE otherwise
295 	 *
296 	 * Since: 3.16
297 	 */
298 	public bool getHasStencilBuffer()
299 	{
300 		return gtk_gl_area_get_has_stencil_buffer(gtkGLArea) != 0;
301 	}
302 
303 	/**
304 	 * Retrieves the required version of OpenGL set
305 	 * using gtk_gl_area_set_required_version().
306 	 *
307 	 * Params:
308 	 *     major = return location for the required major version
309 	 *     minor = return location for the required minor version
310 	 *
311 	 * Since: 3.16
312 	 */
313 	public void getRequiredVersion(out int major, out int minor)
314 	{
315 		gtk_gl_area_get_required_version(gtkGLArea, &major, &minor);
316 	}
317 
318 	/**
319 	 * Ensures that the #GdkGLContext used by @area is associated with
320 	 * the #GtkGLArea.
321 	 *
322 	 * This function is automatically called before emitting the
323 	 * #GtkGLArea::render signal, and doesn't normally need to be called
324 	 * by application code.
325 	 *
326 	 * Since: 3.16
327 	 */
328 	public void makeCurrent()
329 	{
330 		gtk_gl_area_make_current(gtkGLArea);
331 	}
332 
333 	/**
334 	 * Marks the currently rendered data (if any) as invalid, and queues
335 	 * a redraw of the widget, ensuring that the #GtkGLArea::render signal
336 	 * is emitted during the draw.
337 	 *
338 	 * This is only needed when the gtk_gl_area_set_auto_render() has
339 	 * been called with a %FALSE value. The default behaviour is to
340 	 * emit #GtkGLArea::render on each draw.
341 	 *
342 	 * Since: 3.16
343 	 */
344 	public void queueRender()
345 	{
346 		gtk_gl_area_queue_render(gtkGLArea);
347 	}
348 
349 	/**
350 	 * If @auto_render is %TRUE the #GtkGLArea::render signal will be
351 	 * emitted every time the widget draws. This is the default and is
352 	 * useful if drawing the widget is faster.
353 	 *
354 	 * If @auto_render is %FALSE the data from previous rendering is kept
355 	 * around and will be used for drawing the widget the next time,
356 	 * unless the window is resized. In order to force a rendering
357 	 * gtk_gl_area_queue_render() must be called. This mode is useful when
358 	 * the scene changes seldomly, but takes a long time to redraw.
359 	 *
360 	 * Params:
361 	 *     autoRender = a boolean
362 	 *
363 	 * Since: 3.16
364 	 */
365 	public void setAutoRender(bool autoRender)
366 	{
367 		gtk_gl_area_set_auto_render(gtkGLArea, autoRender);
368 	}
369 
370 	/**
371 	 * Sets an error on the area which will be shown instead of the
372 	 * GL rendering. This is useful in the #GtkGLArea::create-context
373 	 * signal if GL context creation fails.
374 	 *
375 	 * Params:
376 	 *     error = a new #GError, or %NULL to unset the error
377 	 *
378 	 * Since: 3.16
379 	 */
380 	public void setError(ErrorG error)
381 	{
382 		gtk_gl_area_set_error(gtkGLArea, (error is null) ? null : error.getErrorGStruct());
383 	}
384 
385 	/**
386 	 * If @has_alpha is %TRUE the buffer allocated by the widget will have
387 	 * an alpha channel component, and when rendering to the window the
388 	 * result will be composited over whatever is below the widget.
389 	 *
390 	 * If @has_alpha is %FALSE there will be no alpha channel, and the
391 	 * buffer will fully replace anything below the widget.
392 	 *
393 	 * Params:
394 	 *     hasAlpha = %TRUE to add an alpha component
395 	 *
396 	 * Since: 3.16
397 	 */
398 	public void setHasAlpha(bool hasAlpha)
399 	{
400 		gtk_gl_area_set_has_alpha(gtkGLArea, hasAlpha);
401 	}
402 
403 	/**
404 	 * If @has_depth_buffer is %TRUE the widget will allocate and
405 	 * enable a depth buffer for the target framebuffer. Otherwise
406 	 * there will be none.
407 	 *
408 	 * Params:
409 	 *     hasDepthBuffer = %TRUE to add a depth buffer
410 	 *
411 	 * Since: 3.16
412 	 */
413 	public void setHasDepthBuffer(bool hasDepthBuffer)
414 	{
415 		gtk_gl_area_set_has_depth_buffer(gtkGLArea, hasDepthBuffer);
416 	}
417 
418 	/**
419 	 * If @has_stencil_buffer is %TRUE the widget will allocate and
420 	 * enable a stencil buffer for the target framebuffer. Otherwise
421 	 * there will be none.
422 	 *
423 	 * Params:
424 	 *     hasStencilBuffer = %TRUE to add a stencil buffer
425 	 *
426 	 * Since: 3.16
427 	 */
428 	public void setHasStencilBuffer(bool hasStencilBuffer)
429 	{
430 		gtk_gl_area_set_has_stencil_buffer(gtkGLArea, hasStencilBuffer);
431 	}
432 
433 	/**
434 	 * Sets the required version of OpenGL to be used when creating the context
435 	 * for the widget.
436 	 *
437 	 * This function must be called before the area has been realized.
438 	 *
439 	 * Params:
440 	 *     major = the major version
441 	 *     minor = the minor version
442 	 *
443 	 * Since: 3.16
444 	 */
445 	public void setRequiredVersion(int major, int minor)
446 	{
447 		gtk_gl_area_set_required_version(gtkGLArea, major, minor);
448 	}
449 
450 	int[string] connectedSignals;
451 
452 	GLContext delegate(GLArea)[] onCreateContextListeners;
453 	/**
454 	 * The ::create-context signal is emitted when the widget is being
455 	 * realized, and allows you to override how the GL context is
456 	 * created. This is useful when you want to reuse an existing GL
457 	 * context, or if you want to try creating different kinds of GL
458 	 * options.
459 	 *
460 	 * If context creation fails then the signal handler can use
461 	 * gtk_gl_area_set_error() to register a more detailed error
462 	 * of how the construction failed.
463 	 *
464 	 * Return: a newly created #GdkGLContext;
465 	 *     the #GtkGLArea widget will take ownership of the returned value.
466 	 *
467 	 * Since: 3.16
468 	 */
469 	void addOnCreateContext(GLContext delegate(GLArea) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
470 	{
471 		if ( "create-context" !in connectedSignals )
472 		{
473 			Signals.connectData(
474 				this,
475 				"create-context",
476 				cast(GCallback)&callBackCreateContext,
477 				cast(void*)this,
478 				null,
479 				connectFlags);
480 			connectedSignals["create-context"] = 1;
481 		}
482 		onCreateContextListeners ~= dlg;
483 	}
484 	extern(C) static GdkGLContext* callBackCreateContext(GtkGLArea* glareaStruct, GLArea _glarea)
485 	{
486 		foreach ( GLContext delegate(GLArea) dlg; _glarea.onCreateContextListeners )
487 		{
488 			if ( auto r = dlg(_glarea) )
489 				return r.getGLContextStruct();
490 		}
491 		
492 		return null;
493 	}
494 
495 	bool delegate(GLContext, GLArea)[] onRenderListeners;
496 	/**
497 	 * The ::render signal is emitted every time the contents
498 	 * of the #GtkGLArea should be redrawn.
499 	 *
500 	 * The @context is bound to the @area prior to emitting this function,
501 	 * and the buffers are painted to the window once the emission terminates.
502 	 *
503 	 * Params:
504 	 *     context = the #GdkGLContext used by @area
505 	 *
506 	 * Return: %TRUE to stop other handlers from being invoked for the event.
507 	 *     %FALSE to propagate the event further.
508 	 *
509 	 * Since: 3.16
510 	 */
511 	void addOnRender(bool delegate(GLContext, GLArea) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
512 	{
513 		if ( "render" !in connectedSignals )
514 		{
515 			Signals.connectData(
516 				this,
517 				"render",
518 				cast(GCallback)&callBackRender,
519 				cast(void*)this,
520 				null,
521 				connectFlags);
522 			connectedSignals["render"] = 1;
523 		}
524 		onRenderListeners ~= dlg;
525 	}
526 	extern(C) static int callBackRender(GtkGLArea* glareaStruct, GdkGLContext* context, GLArea _glarea)
527 	{
528 		foreach ( bool delegate(GLContext, GLArea) dlg; _glarea.onRenderListeners )
529 		{
530 			if ( dlg(ObjectG.getDObject!(GLContext)(context), _glarea) )
531 			{
532 				return 1;
533 			}
534 		}
535 		
536 		return 0;
537 	}
538 
539 	void delegate(int, int, GLArea)[] onResizeListeners;
540 	/**
541 	 * The ::resize signal is emitted once when the widget is realized, and
542 	 * then each time the widget is changed while realized. This is useful
543 	 * in order to keep GL state up to date with the widget size, like for
544 	 * instance camera properties which may depend on the width/height ratio.
545 	 *
546 	 * The GL context for the area is guaranteed to be current when this signal
547 	 * is emitted.
548 	 *
549 	 * The default handler sets up the GL viewport.
550 	 *
551 	 * Params:
552 	 *     width = the width of the viewport
553 	 *     height = the height of the viewport
554 	 *
555 	 * Since: 3.16
556 	 */
557 	void addOnResize(void delegate(int, int, GLArea) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
558 	{
559 		if ( "resize" !in connectedSignals )
560 		{
561 			Signals.connectData(
562 				this,
563 				"resize",
564 				cast(GCallback)&callBackResize,
565 				cast(void*)this,
566 				null,
567 				connectFlags);
568 			connectedSignals["resize"] = 1;
569 		}
570 		onResizeListeners ~= dlg;
571 	}
572 	extern(C) static void callBackResize(GtkGLArea* glareaStruct, int width, int height, GLArea _glarea)
573 	{
574 		foreach ( void delegate(int, int, GLArea) dlg; _glarea.onResizeListeners )
575 		{
576 			dlg(width, height, _glarea);
577 		}
578 	}
579 }