44c339f2283c2393da6fe9e2c41db269426baa51
[clutter-gtk] / clutter-gtk / gtk-clutter-embed.c
1 /* gtk-clutter-embed.c: Embeddable ClutterStage
2  *
3  * Copyright (C) 2007 OpenedHand
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library. If not see <http://www.fsf.org/licensing>.
17  *
18  * Authors:
19  *   Iain Holmes  <iain@openedhand.com>
20  *   Emmanuele Bassi  <ebassi@openedhand.com>
21  */
22
23 /**
24  * SECTION:gtk-clutter-embed
25  * @short_description: Widget for embedding a Clutter scene
26  *
27  * #GtkClutterEmbed is a GTK+ widget embedding a #ClutterStage. Using
28  * a #GtkClutterEmbed widget is possible to build, show and interact with
29  * a scene built using Clutter inside a GTK+ application.
30  *
31  * <note>To avoid flickering on show, you should call gtk_widget_show()
32  * or gtk_widget_realize() before calling clutter_actor_show() on the
33  * embedded #ClutterStage actor. This is needed for Clutter to be able
34  * to paint on the #GtkClutterEmbed widget.</note>
35  *
36  * Since: 0.6
37  */
38
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42
43 #include "gtk-clutter-embed.h"
44
45 #include <glib-object.h>
46
47 #include <gdk/gdk.h>
48
49 #if defined(HAVE_CLUTTER_GTK_X11)
50
51 #include <clutter/x11/clutter-x11.h>
52 #include <gdk/gdkx.h>
53
54 #elif defined(HAVE_CLUTTER_GTK_WIN32)
55
56 #include <clutter/clutter-win32.h>
57 #include <gdk/gdkwin32.h>
58
59 #endif /* HAVE_CLUTTER_GTK_{X11,WIN32} */
60
61 G_DEFINE_TYPE (GtkClutterEmbed, gtk_clutter_embed, GTK_TYPE_WIDGET);
62
63 #define GTK_CLUTTER_EMBED_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_CLUTTER_EMBED, GtkClutterEmbedPrivate))
64
65 struct _GtkClutterEmbedPrivate
66 {
67   ClutterActor *stage;
68 };
69
70 static void
71 gtk_clutter_embed_send_configure (GtkClutterEmbed *embed)
72 {
73   GtkWidget *widget;
74   GdkEvent *event = gdk_event_new (GDK_CONFIGURE);
75
76   widget = GTK_WIDGET (embed);
77
78   event->configure.window = g_object_ref (widget->window);
79   event->configure.send_event = TRUE;
80   event->configure.x = widget->allocation.x;
81   event->configure.y = widget->allocation.y;
82   event->configure.width = widget->allocation.width;
83   event->configure.height = widget->allocation.height;
84   
85   gtk_widget_event (widget, event);
86   gdk_event_free (event);
87 }
88
89 static void
90 gtk_clutter_embed_dispose (GObject *gobject)
91 {
92   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (gobject)->priv;
93
94   if (priv->stage)
95     {
96       clutter_actor_destroy (priv->stage);
97       priv->stage = NULL;
98     }
99
100   G_OBJECT_CLASS (gtk_clutter_embed_parent_class)->dispose (gobject);
101 }
102
103 static void
104 gtk_clutter_embed_show (GtkWidget *widget)
105 {
106   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv;
107
108   /* Make sure the widget is realised before we show */
109   if (!GTK_WIDGET_REALIZED (widget))
110     gtk_widget_realize (widget);
111
112   clutter_actor_show (priv->stage);
113
114   GTK_WIDGET_CLASS (gtk_clutter_embed_parent_class)->show (widget);
115 }
116
117 static void
118 gtk_clutter_embed_hide (GtkWidget *widget)
119 {
120   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv;
121
122   clutter_actor_hide (priv->stage);
123
124   GTK_WIDGET_CLASS (gtk_clutter_embed_parent_class)->hide (widget);
125 }
126
127 static void
128 gtk_clutter_embed_realize (GtkWidget *widget)
129 {
130   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv; 
131   GdkWindowAttr attributes;
132   int attributes_mask;
133   
134   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
135
136   attributes.window_type = GDK_WINDOW_CHILD;
137   attributes.x = widget->allocation.x;
138   attributes.y = widget->allocation.y;
139   attributes.width = widget->allocation.width;
140   attributes.height = widget->allocation.height;
141   attributes.wclass = GDK_INPUT_OUTPUT;
142   attributes.visual = gtk_widget_get_visual (widget);
143   attributes.colormap = gtk_widget_get_colormap (widget);
144
145   /* NOTE: GDK_MOTION_NOTIFY above should be safe as Clutter does its own
146    *       throtling. 
147   */
148   attributes.event_mask = gtk_widget_get_events (widget)
149                         | GDK_EXPOSURE_MASK
150                         | GDK_BUTTON_PRESS_MASK
151                         | GDK_BUTTON_RELEASE_MASK
152                         | GDK_KEY_PRESS_MASK
153                         | GDK_KEY_RELEASE_MASK
154                         | GDK_POINTER_MOTION_MASK;
155
156   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
157
158   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
159                                    &attributes,
160                                    attributes_mask);
161   gdk_window_set_user_data (widget->window, widget);
162
163   widget->style = gtk_style_attach (widget->style, widget->window);
164   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
165   
166   gdk_window_set_back_pixmap (widget->window, NULL, FALSE);
167
168 #if defined(HAVE_CLUTTER_GTK_X11)
169   clutter_x11_set_stage_foreign (CLUTTER_STAGE (priv->stage), 
170                                  GDK_WINDOW_XID (widget->window));
171 #elif defined(HAVE_CLUTTER_GTK_WIN32)
172   clutter_win32_set_stage_foreign (CLUTTER_STAGE (priv->stage), 
173                                    GDK_WINDOW_HWND (widget->window));
174 #endif /* HAVE_CLUTTER_GTK_{X11,WIN32} */
175
176   clutter_actor_queue_redraw (CLUTTER_ACTOR (priv->stage));
177
178   gtk_clutter_embed_send_configure (GTK_CLUTTER_EMBED (widget));
179 }
180
181 static void
182 gtk_clutter_embed_size_allocate (GtkWidget     *widget,
183                                  GtkAllocation *allocation)
184 {
185   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv;
186
187   widget->allocation = *allocation;
188
189   if (GTK_WIDGET_REALIZED (widget))
190     {
191       gdk_window_move_resize (widget->window,
192                               allocation->x, allocation->y,
193                               allocation->width, allocation->height);
194
195       gtk_clutter_embed_send_configure (GTK_CLUTTER_EMBED (widget));
196     }
197
198   clutter_actor_set_size (priv->stage,
199                           allocation->width,
200                           allocation->height);
201
202   clutter_actor_queue_relayout (priv->stage);
203 }
204
205 static gboolean
206 gtk_clutter_embed_motion_notify_event (GtkWidget      *widget,
207                                        GdkEventMotion *event)
208 {
209   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv;
210   ClutterEvent cevent = { 0, };
211
212   cevent.type = CLUTTER_MOTION;
213   cevent.any.stage = CLUTTER_STAGE (priv->stage);
214   cevent.motion.x = event->x;
215   cevent.motion.y = event->y;
216   cevent.motion.time = event->time;
217   cevent.motion.modifier_state = event->state;
218
219   clutter_do_event (&cevent);
220
221   /* doh - motion events can push ENTER/LEAVE events onto Clutters
222    * internal event queue which we do really ever touch (essentially
223    * proxying from gtks queue). The below pumps them back out and
224    * processes.
225    * *could* be side effects with below though doubful as no other
226    * events reach the queue (we shut down event collection). Maybe
227    * a peek_mask type call could be even safer. 
228   */
229   while (clutter_events_pending())
230     {
231       ClutterEvent *ev = clutter_event_get ();
232       if (ev)
233         {
234           clutter_do_event (ev);
235           clutter_event_free (ev);
236         }
237     }
238
239   return FALSE;
240 }
241
242 static gboolean
243 gtk_clutter_embed_button_event (GtkWidget      *widget,
244                                 GdkEventButton *event)
245 {
246   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv;
247   ClutterEvent cevent = { 0, };
248
249   if (event->type == GDK_BUTTON_PRESS ||
250       event->type == GDK_2BUTTON_PRESS ||
251       event->type == GDK_3BUTTON_PRESS)
252     cevent.type = cevent.button.type = CLUTTER_BUTTON_PRESS;
253   else if (event->type == GDK_BUTTON_RELEASE)
254     cevent.type = cevent.button.type = CLUTTER_BUTTON_RELEASE;
255   else
256     return FALSE;
257
258   cevent.any.stage = CLUTTER_STAGE (priv->stage);
259   cevent.button.x = event->x;
260   cevent.button.y = event->y;
261   cevent.button.time = event->time;
262   cevent.button.click_count =
263     (event->type == GDK_BUTTON_PRESS ? 1
264                                      : (event->type == GDK_2BUTTON_PRESS ? 2
265                                                                          : 3));
266   cevent.button.modifier_state = event->state;
267   cevent.button.button = event->button;
268
269   clutter_do_event (&cevent);
270
271   return FALSE;
272 }
273
274 static gboolean
275 gtk_clutter_embed_key_event (GtkWidget   *widget,
276                              GdkEventKey *event)
277 {
278   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv;
279   ClutterEvent cevent = { 0, };
280
281   if (event->type == GDK_KEY_PRESS)
282     cevent.type = cevent.key.type = CLUTTER_KEY_PRESS;
283   else if (event->type == GDK_KEY_RELEASE)
284     cevent.type = cevent.key.type = CLUTTER_KEY_RELEASE;
285   else
286     return FALSE;
287
288   cevent.any.stage = CLUTTER_STAGE (priv->stage);
289   cevent.key.time = event->time;
290   cevent.key.modifier_state = event->state;
291   cevent.key.keyval = event->keyval;
292   cevent.key.hardware_keycode = event->hardware_keycode;
293
294   clutter_do_event (&cevent);
295
296   return FALSE;
297 }
298
299 static gboolean
300 gtk_clutter_embed_expose_event (GtkWidget      *widget,
301                                 GdkEventExpose *event)
302 {
303   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv;
304
305   if (CLUTTER_ACTOR_IS_VISIBLE (priv->stage))
306     clutter_actor_queue_redraw (priv->stage);
307
308   return FALSE;
309 }
310
311 static gboolean
312 gtk_clutter_embed_map_event (GtkWidget   *widget,
313                              GdkEventAny *event)
314 {
315   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv;
316
317   /* The backend wont get the XEvent as we go strait to do_event().
318    * So we have to make sure we set the event here.
319   */
320   CLUTTER_ACTOR_SET_FLAGS (priv->stage, CLUTTER_ACTOR_MAPPED);
321
322   return FALSE;
323 }
324
325 static gboolean
326 gtk_clutter_embed_focus_in (GtkWidget *widget,
327                             GdkEventFocus *event)
328 {
329   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv;
330
331   g_signal_emit_by_name (priv->stage, "activate");
332
333   return FALSE;
334 }
335
336 static gboolean
337 gtk_clutter_embed_focus_out (GtkWidget     *widget,
338                              GdkEventFocus *event)
339 {
340   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv;
341
342   g_signal_emit_by_name (priv->stage, "deactivate");
343
344   /* give back key focus to the stage */
345   clutter_stage_set_key_focus (CLUTTER_STAGE (priv->stage), NULL);
346
347   return FALSE;
348 }
349
350 static gboolean
351 gtk_clutter_embed_scroll_event (GtkWidget      *widget,
352                                 GdkEventScroll *event)
353 {
354   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv;
355
356   ClutterEvent cevent = { 0, };
357
358   if (event->type == GDK_SCROLL)
359     cevent.type = cevent.scroll.type = CLUTTER_SCROLL;
360   else
361     return FALSE;
362
363   cevent.any.stage = CLUTTER_STAGE (priv->stage);
364   cevent.scroll.x = (gint) event->x;
365   cevent.scroll.y = (gint) event->y;
366   cevent.scroll.time = event->time;
367   cevent.scroll.direction = event->direction;
368   cevent.scroll.modifier_state = event->state;
369
370   clutter_do_event (&cevent);
371
372   return FALSE;
373 }
374
375 static void
376 gtk_clutter_embed_class_init (GtkClutterEmbedClass *klass)
377 {
378   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
379   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
380
381   g_type_class_add_private (klass, sizeof (GtkClutterEmbedPrivate));
382
383   gobject_class->dispose = gtk_clutter_embed_dispose;
384
385   widget_class->size_allocate = gtk_clutter_embed_size_allocate;
386   widget_class->realize = gtk_clutter_embed_realize;
387   widget_class->show = gtk_clutter_embed_show;
388   widget_class->hide = gtk_clutter_embed_hide;
389   widget_class->button_press_event = gtk_clutter_embed_button_event;
390   widget_class->button_release_event = gtk_clutter_embed_button_event;
391   widget_class->key_press_event = gtk_clutter_embed_key_event;
392   widget_class->key_release_event = gtk_clutter_embed_key_event;
393   widget_class->motion_notify_event = gtk_clutter_embed_motion_notify_event;
394   widget_class->expose_event = gtk_clutter_embed_expose_event;
395   widget_class->map_event = gtk_clutter_embed_map_event;
396   widget_class->focus_in_event = gtk_clutter_embed_focus_in;
397   widget_class->focus_out_event = gtk_clutter_embed_focus_out;
398   widget_class->scroll_event = gtk_clutter_embed_scroll_event;
399 }
400
401 static void
402 gtk_clutter_embed_init (GtkClutterEmbed *embed)
403 {
404   GtkClutterEmbedPrivate *priv;
405
406   embed->priv = priv = GTK_CLUTTER_EMBED_GET_PRIVATE (embed);
407
408   GTK_WIDGET_SET_FLAGS (embed, GTK_CAN_FOCUS);
409
410   /* disable double-buffering: it's automatically provided
411    * by OpenGL
412    */
413   gtk_widget_set_double_buffered (GTK_WIDGET (embed), FALSE);
414
415   /* we always create new stages rather than use the default */
416   priv->stage = clutter_stage_new ();
417
418   /* we must realize the stage to get it ready for embedding */
419   clutter_actor_realize (priv->stage);
420
421 #ifdef HAVE_CLUTTER_GTK_X11
422   {
423     const XVisualInfo *xvinfo;
424     GdkVisual *visual;
425     GdkColormap *colormap;
426
427     /* We need to use the colormap from the Clutter visual */
428     xvinfo = clutter_x11_get_stage_visual (CLUTTER_STAGE (priv->stage));
429     visual = gdk_x11_screen_lookup_visual (gdk_screen_get_default (),
430                                            xvinfo->visualid);
431     colormap = gdk_colormap_new (visual, FALSE);
432     gtk_widget_set_colormap (GTK_WIDGET (embed), colormap);
433   }
434 #endif
435 }
436
437 /**
438  * gtk_clutter_init:
439  * @argc: pointer to the arguments count, or %NULL
440  * @argv: pointer to the arguments vector, or %NULL
441  *
442  * This function should be called instead of clutter_init() and
443  * gtk_init().
444  *
445  * Return value: %CLUTTER_INIT_SUCCESS on success, a negative integer
446  *   on failure.
447  *
448  * Since: 0.8
449  */
450 ClutterInitError
451 gtk_clutter_init (int    *argc,
452                   char ***argv)
453 {
454   if (!gtk_init_check (argc, argv))
455     return CLUTTER_INIT_ERROR_GTK;
456
457 #if defined(HAVE_CLUTTER_GTK_X11)
458   clutter_x11_set_display (GDK_DISPLAY());
459   clutter_x11_disable_event_retrieval ();
460 #elif defined(HAVE_CLUTTER_GTK_WIN32)
461   clutter_win32_disable_event_retrieval ();
462 #endif /* HAVE_CLUTTER_GTK_{X11,WIN32} */
463
464   return clutter_init (argc, argv);
465 }
466
467 /**
468  * gtk_clutter_embed_new:
469  *
470  * Creates a new #GtkClutterEmbed widget. This widget can be
471  * used to build a scene using Clutter API into a GTK+ application.
472  *
473  * Return value: the newly created #GtkClutterEmbed
474  *
475  * Since: 0.6
476  */
477 GtkWidget *
478 gtk_clutter_embed_new (void)
479 {
480   return g_object_new (GTK_TYPE_CLUTTER_EMBED, NULL);
481 }
482
483 /**
484  * gtk_clutter_embed_get_stage:
485  * @embed: a #GtkClutterEmbed
486  *
487  * Retrieves the #ClutterStage from @embed. The returned stage can be
488  * used to add actors to the Clutter scene.
489  *
490  * Return value: the Clutter stage. You should never destroy or unref
491  *   the returned actor.
492  *
493  * Since: 0.6
494  */
495 ClutterActor *
496 gtk_clutter_embed_get_stage (GtkClutterEmbed *embed)
497 {
498   g_return_val_if_fail (GTK_IS_CLUTTER_EMBED (embed), NULL);
499
500   return embed->priv->stage;
501 }