Merge branch '1.0-integration'
[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_CLUTTER_TYPE_EMBED, GtkClutterEmbedPrivate))
64
65 struct _GtkClutterEmbedPrivate
66 {
67   ClutterActor *stage;
68
69   guint queue_redraw_id;
70 };
71
72 static void
73 gtk_clutter_embed_send_configure (GtkClutterEmbed *embed)
74 {
75   GtkWidget *widget;
76   GdkEvent *event = gdk_event_new (GDK_CONFIGURE);
77
78   widget = GTK_WIDGET (embed);
79
80   event->configure.window = g_object_ref (widget->window);
81   event->configure.send_event = TRUE;
82   event->configure.x = widget->allocation.x;
83   event->configure.y = widget->allocation.y;
84   event->configure.width = widget->allocation.width;
85   event->configure.height = widget->allocation.height;
86   
87   gtk_widget_event (widget, event);
88   gdk_event_free (event);
89 }
90
91 static void
92 on_stage_queue_redraw (ClutterStage *stage,
93                        ClutterActor *origin,
94                        gpointer      user_data)
95 {
96   GtkWidget *embed = user_data;
97
98   /* we stop the emission of the Stage::queue-redraw signal to prevent
99    * the default handler from running; then we queue a redraw on the
100    * GtkClutterEmbed widget which will cause an expose event to be
101    * emitted. the Stage is redrawn in the expose event handler, thus
102    * "slaving" the Clutter redraw cycle to GTK+'s own
103    */
104   g_signal_stop_emission_by_name (stage, "queue-redraw");
105
106   gtk_widget_queue_draw (embed);
107 }
108
109 static void
110 gtk_clutter_embed_dispose (GObject *gobject)
111 {
112   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (gobject)->priv;
113
114   if (priv->queue_redraw_id)
115     {
116       g_signal_handler_disconnect (priv->stage, priv->queue_redraw_id);
117       priv->queue_redraw_id = 0;
118     }
119
120   if (priv->stage)
121     {
122       clutter_actor_destroy (priv->stage);
123       priv->stage = NULL;
124     }
125
126   G_OBJECT_CLASS (gtk_clutter_embed_parent_class)->dispose (gobject);
127 }
128
129 static void
130 gtk_clutter_embed_show (GtkWidget *widget)
131 {
132   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv;
133
134   if (GTK_WIDGET_REALIZED (widget))
135     clutter_actor_show (priv->stage);
136
137   GTK_WIDGET_CLASS (gtk_clutter_embed_parent_class)->show (widget);
138 }
139
140 static void
141 gtk_clutter_embed_hide (GtkWidget *widget)
142 {
143   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv;
144
145   clutter_actor_hide (priv->stage);
146
147   GTK_WIDGET_CLASS (gtk_clutter_embed_parent_class)->hide (widget);
148 }
149
150 static void
151 gtk_clutter_embed_realize (GtkWidget *widget)
152 {
153   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv; 
154   GdkWindowAttr attributes;
155   int attributes_mask;
156
157 #ifdef HAVE_CLUTTER_GTK_X11
158   {
159     const XVisualInfo *xvinfo;
160     GdkVisual *visual;
161     GdkColormap *colormap;
162
163     /* We need to use the colormap from the Clutter visual */
164     xvinfo = clutter_x11_get_stage_visual (CLUTTER_STAGE (priv->stage));
165     if (xvinfo == None)
166       {
167         g_critical ("Unable to retrieve the XVisualInfo from Clutter");
168         return;
169       }
170
171     visual = gdk_x11_screen_lookup_visual (gtk_widget_get_screen (widget),
172                                            xvinfo->visualid);
173     colormap = gdk_colormap_new (visual, FALSE);
174     gtk_widget_set_colormap (widget, colormap);
175   }
176 #endif
177
178   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
179
180   attributes.window_type = GDK_WINDOW_CHILD;
181   attributes.x = widget->allocation.x;
182   attributes.y = widget->allocation.y;
183   attributes.width = widget->allocation.width;
184   attributes.height = widget->allocation.height;
185   attributes.wclass = GDK_INPUT_OUTPUT;
186   attributes.visual = gtk_widget_get_visual (widget);
187   attributes.colormap = gtk_widget_get_colormap (widget);
188
189   /* NOTE: GDK_MOTION_NOTIFY above should be safe as Clutter does its own
190    *       throtling. 
191   */
192   attributes.event_mask = gtk_widget_get_events (widget)
193                         | GDK_EXPOSURE_MASK
194                         | GDK_BUTTON_PRESS_MASK
195                         | GDK_BUTTON_RELEASE_MASK
196                         | GDK_KEY_PRESS_MASK
197                         | GDK_KEY_RELEASE_MASK
198                         | GDK_POINTER_MOTION_MASK;
199
200   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
201
202   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
203                                    &attributes,
204                                    attributes_mask);
205   gdk_window_set_user_data (widget->window, widget);
206
207   widget->style = gtk_style_attach (widget->style, widget->window);
208   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
209   
210   gdk_window_set_back_pixmap (widget->window, NULL, FALSE);
211
212 #if defined(HAVE_CLUTTER_GTK_X11)
213   clutter_x11_set_stage_foreign (CLUTTER_STAGE (priv->stage), 
214                                  GDK_WINDOW_XID (widget->window));
215 #elif defined(HAVE_CLUTTER_GTK_WIN32)
216   clutter_win32_set_stage_foreign (CLUTTER_STAGE (priv->stage), 
217                                    GDK_WINDOW_HWND (widget->window));
218 #endif /* HAVE_CLUTTER_GTK_{X11,WIN32} */
219
220   clutter_actor_realize (priv->stage);
221
222   if (GTK_WIDGET_VISIBLE (widget))
223     clutter_actor_show (priv->stage);
224
225   gtk_clutter_embed_send_configure (GTK_CLUTTER_EMBED (widget));
226 }
227
228 static void
229 gtk_clutter_embed_unrealize (GtkWidget *widget)
230 {
231   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv;
232
233   clutter_actor_hide (priv->stage);
234
235   GTK_WIDGET_CLASS (gtk_clutter_embed_parent_class)->unrealize (widget);
236 }
237
238 static void
239 gtk_clutter_embed_size_allocate (GtkWidget     *widget,
240                                  GtkAllocation *allocation)
241 {
242   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv;
243
244   widget->allocation = *allocation;
245
246   if (GTK_WIDGET_REALIZED (widget))
247     {
248       gdk_window_move_resize (widget->window,
249                               allocation->x, allocation->y,
250                               allocation->width, allocation->height);
251
252       gtk_clutter_embed_send_configure (GTK_CLUTTER_EMBED (widget));
253     }
254
255   /* change the size of the stage and ensure that the viewport
256    * has been updated as well
257    */
258   clutter_actor_set_size (priv->stage,
259                           allocation->width,
260                           allocation->height);
261
262   clutter_stage_ensure_viewport (CLUTTER_STAGE (priv->stage));
263 }
264
265 static gboolean
266 gtk_clutter_embed_expose_event (GtkWidget *widget,
267                                 GdkEventExpose *event)
268 {
269   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv;
270
271   /* force a redraw on expose */
272   clutter_redraw (CLUTTER_STAGE (priv->stage));
273
274   return FALSE;
275 }
276
277 static gboolean
278 gtk_clutter_embed_motion_notify_event (GtkWidget      *widget,
279                                        GdkEventMotion *event)
280 {
281   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv;
282   ClutterEvent cevent = { 0, };
283
284   cevent.type = CLUTTER_MOTION;
285   cevent.any.stage = CLUTTER_STAGE (priv->stage);
286   cevent.motion.x = event->x;
287   cevent.motion.y = event->y;
288   cevent.motion.time = event->time;
289   cevent.motion.modifier_state = event->state;
290
291   clutter_do_event (&cevent);
292
293   /* doh - motion events can push ENTER/LEAVE events onto Clutters
294    * internal event queue which we do really ever touch (essentially
295    * proxying from gtks queue). The below pumps them back out and
296    * processes.
297    * *could* be side effects with below though doubful as no other
298    * events reach the queue (we shut down event collection). Maybe
299    * a peek_mask type call could be even safer. 
300   */
301   while (clutter_events_pending())
302     {
303       ClutterEvent *ev = clutter_event_get ();
304       if (ev)
305         {
306           clutter_do_event (ev);
307           clutter_event_free (ev);
308         }
309     }
310
311   return FALSE;
312 }
313
314 static gboolean
315 gtk_clutter_embed_button_event (GtkWidget      *widget,
316                                 GdkEventButton *event)
317 {
318   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv;
319   ClutterEvent cevent = { 0, };
320
321   if (event->type == GDK_BUTTON_PRESS ||
322       event->type == GDK_2BUTTON_PRESS ||
323       event->type == GDK_3BUTTON_PRESS)
324     cevent.type = cevent.button.type = CLUTTER_BUTTON_PRESS;
325   else if (event->type == GDK_BUTTON_RELEASE)
326     cevent.type = cevent.button.type = CLUTTER_BUTTON_RELEASE;
327   else
328     return FALSE;
329
330   cevent.any.stage = CLUTTER_STAGE (priv->stage);
331   cevent.button.x = event->x;
332   cevent.button.y = event->y;
333   cevent.button.time = event->time;
334   cevent.button.click_count =
335     (event->type == GDK_BUTTON_PRESS ? 1
336                                      : (event->type == GDK_2BUTTON_PRESS ? 2
337                                                                          : 3));
338   cevent.button.modifier_state = event->state;
339   cevent.button.button = event->button;
340
341   clutter_do_event (&cevent);
342
343   return FALSE;
344 }
345
346 static gboolean
347 gtk_clutter_embed_key_event (GtkWidget   *widget,
348                              GdkEventKey *event)
349 {
350   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv;
351   ClutterEvent cevent = { 0, };
352
353   if (event->type == GDK_KEY_PRESS)
354     cevent.type = cevent.key.type = CLUTTER_KEY_PRESS;
355   else if (event->type == GDK_KEY_RELEASE)
356     cevent.type = cevent.key.type = CLUTTER_KEY_RELEASE;
357   else
358     return FALSE;
359
360   cevent.any.stage = CLUTTER_STAGE (priv->stage);
361   cevent.key.time = event->time;
362   cevent.key.modifier_state = event->state;
363   cevent.key.keyval = event->keyval;
364   cevent.key.hardware_keycode = event->hardware_keycode;
365   cevent.key.unicode_value = gdk_keyval_to_unicode (event->keyval);
366
367   clutter_do_event (&cevent);
368
369   return FALSE;
370 }
371
372 static gboolean
373 gtk_clutter_embed_map_event (GtkWidget   *widget,
374                              GdkEventAny *event)
375 {
376   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv;
377   GtkWidgetClass *parent_class;
378   gboolean res = FALSE;
379
380   parent_class = GTK_WIDGET_CLASS (gtk_clutter_embed_parent_class);
381   if (parent_class->map_event)
382     res = parent_class->map_event (widget, event);
383
384   clutter_actor_map (priv->stage);
385
386   return res;
387 }
388
389 static gboolean
390 gtk_clutter_embed_unmap_event (GtkWidget   *widget,
391                                GdkEventAny *event)
392 {
393   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv;
394   GtkWidgetClass *parent_class;
395   gboolean res = FALSE;
396
397   parent_class = GTK_WIDGET_CLASS (gtk_clutter_embed_parent_class);
398   if (parent_class->unmap_event)
399     res = parent_class->unmap_event (widget, event);
400
401   clutter_actor_unmap (priv->stage);
402
403   return res;
404 }
405
406 static void
407 gtk_clutter_embed_unmap (GtkWidget *widget)
408 {
409   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv;
410
411   clutter_actor_unmap (priv->stage);
412
413   GTK_WIDGET_CLASS (gtk_clutter_embed_parent_class)->unmap (widget);
414 }
415
416 static gboolean
417 gtk_clutter_embed_focus_in (GtkWidget     *widget,
418                             GdkEventFocus *event)
419 {
420   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv;
421
422   g_signal_emit_by_name (priv->stage, "activate");
423
424   return FALSE;
425 }
426
427 static gboolean
428 gtk_clutter_embed_focus_out (GtkWidget     *widget,
429                              GdkEventFocus *event)
430 {
431   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv;
432
433   g_signal_emit_by_name (priv->stage, "deactivate");
434
435   /* give back key focus to the stage */
436   clutter_stage_set_key_focus (CLUTTER_STAGE (priv->stage), NULL);
437
438   return FALSE;
439 }
440
441 static gboolean
442 gtk_clutter_embed_scroll_event (GtkWidget      *widget,
443                                 GdkEventScroll *event)
444 {
445   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv;
446
447   ClutterEvent cevent = { 0, };
448
449   if (event->type == GDK_SCROLL)
450     cevent.type = cevent.scroll.type = CLUTTER_SCROLL;
451   else
452     return FALSE;
453
454   cevent.any.stage = CLUTTER_STAGE (priv->stage);
455   cevent.scroll.x = (gint) event->x;
456   cevent.scroll.y = (gint) event->y;
457   cevent.scroll.time = event->time;
458   cevent.scroll.direction = event->direction;
459   cevent.scroll.modifier_state = event->state;
460
461   clutter_do_event (&cevent);
462
463   return FALSE;
464 }
465
466 static void
467 gtk_clutter_embed_style_set (GtkWidget *widget,
468                              GtkStyle  *old_style)
469 {
470   GdkScreen *screen;
471   GtkSettings *settings;
472   gdouble dpi;
473   gchar *font_name;
474   const cairo_font_options_t *font_options;
475   gint double_click_time, double_click_distance;
476   ClutterBackend *backend;
477
478   GTK_WIDGET_CLASS (gtk_clutter_embed_parent_class)->style_set (widget,
479                                                                 old_style);
480
481   if (gtk_widget_has_screen (widget))
482     screen = gtk_widget_get_screen (widget);
483   else
484     screen = gdk_screen_get_default ();
485
486   dpi = gdk_screen_get_resolution (screen);
487   if (dpi < 0)
488     dpi = 96.0;
489
490   font_options = gdk_screen_get_font_options (screen);
491
492   settings = gtk_settings_get_for_screen (screen);
493   g_object_get (G_OBJECT (settings),
494                 "gtk-font-name", &font_name,
495                 "gtk-double-click-time", &double_click_time,
496                 "gtk-double-click-distance", &double_click_distance,
497                 NULL);
498
499   /* copy all settings and values coming from GTK+ into
500    * the ClutterBackend; this way, a scene embedded into
501    * a GtkClutterEmbed will not look completely alien
502    */
503   backend = clutter_get_default_backend ();
504   clutter_backend_set_resolution (backend, dpi);
505   clutter_backend_set_font_options (backend, font_options);
506   clutter_backend_set_font_name (backend, font_name);
507   clutter_backend_set_double_click_time (backend, double_click_time);
508   clutter_backend_set_double_click_distance (backend, double_click_distance);
509
510   g_free (font_name);
511 }
512
513 static void
514 gtk_clutter_embed_class_init (GtkClutterEmbedClass *klass)
515 {
516   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
517   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
518
519   g_type_class_add_private (klass, sizeof (GtkClutterEmbedPrivate));
520
521   gobject_class->dispose = gtk_clutter_embed_dispose;
522
523   widget_class->style_set = gtk_clutter_embed_style_set;
524   widget_class->size_allocate = gtk_clutter_embed_size_allocate;
525   widget_class->realize = gtk_clutter_embed_realize;
526   widget_class->unrealize = gtk_clutter_embed_unrealize;
527   widget_class->show = gtk_clutter_embed_show;
528   widget_class->hide = gtk_clutter_embed_hide;
529   widget_class->unmap = gtk_clutter_embed_unmap;
530   widget_class->expose_event = gtk_clutter_embed_expose_event;
531   widget_class->button_press_event = gtk_clutter_embed_button_event;
532   widget_class->button_release_event = gtk_clutter_embed_button_event;
533   widget_class->key_press_event = gtk_clutter_embed_key_event;
534   widget_class->key_release_event = gtk_clutter_embed_key_event;
535   widget_class->motion_notify_event = gtk_clutter_embed_motion_notify_event;
536   widget_class->expose_event = gtk_clutter_embed_expose_event;
537   widget_class->map_event = gtk_clutter_embed_map_event;
538   widget_class->unmap_event = gtk_clutter_embed_unmap_event;
539   widget_class->focus_in_event = gtk_clutter_embed_focus_in;
540   widget_class->focus_out_event = gtk_clutter_embed_focus_out;
541   widget_class->scroll_event = gtk_clutter_embed_scroll_event;
542 }
543
544 static void
545 gtk_clutter_embed_init (GtkClutterEmbed *embed)
546 {
547   GtkClutterEmbedPrivate *priv;
548
549   embed->priv = priv = GTK_CLUTTER_EMBED_GET_PRIVATE (embed);
550
551   GTK_WIDGET_SET_FLAGS (embed, GTK_CAN_FOCUS);
552   GTK_WIDGET_UNSET_FLAGS (embed, GTK_NO_WINDOW);
553
554   /* disable double-buffering: it's automatically provided
555    * by OpenGL
556    */
557   gtk_widget_set_double_buffered (GTK_WIDGET (embed), FALSE);
558
559   /* we always create new stages rather than use the default */
560   priv->stage = clutter_stage_new ();
561
562   /* intercept the queue-redraw signal of the stage to know when
563    * Clutter-side requests a redraw; this way we can also request
564    * a redraw GTK-side
565    */
566   priv->queue_redraw_id =
567     g_signal_connect (priv->stage,
568                       "queue-redraw", G_CALLBACK (on_stage_queue_redraw),
569                       embed);
570 }
571
572 /**
573  * gtk_clutter_embed_new:
574  *
575  * Creates a new #GtkClutterEmbed widget. This widget can be
576  * used to build a scene using Clutter API into a GTK+ application.
577  *
578  * Return value: the newly created #GtkClutterEmbed
579  *
580  * Since: 0.6
581  */
582 GtkWidget *
583 gtk_clutter_embed_new (void)
584 {
585   return g_object_new (GTK_CLUTTER_TYPE_EMBED, NULL);
586 }
587
588 /**
589  * gtk_clutter_embed_get_stage:
590  * @embed: a #GtkClutterEmbed
591  *
592  * Retrieves the #ClutterStage from @embed. The returned stage can be
593  * used to add actors to the Clutter scene.
594  *
595  * Return value: the Clutter stage. You should never destroy or unref
596  *   the returned actor.
597  *
598  * Since: 0.6
599  */
600 ClutterActor *
601 gtk_clutter_embed_get_stage (GtkClutterEmbed *embed)
602 {
603   g_return_val_if_fail (GTK_CLUTTER_IS_EMBED (embed), NULL);
604
605   return embed->priv->stage;
606 }