Updates for 1.0 API changes in Clutter
[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                        gboolean      queue_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
366   clutter_do_event (&cevent);
367
368   return FALSE;
369 }
370
371 static gboolean
372 gtk_clutter_embed_map_event (GtkWidget   *widget,
373                              GdkEventAny *event)
374 {
375   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv;
376   GtkWidgetClass *parent_class;
377   gboolean res = FALSE;
378
379   parent_class = GTK_WIDGET_CLASS (gtk_clutter_embed_parent_class);
380   if (parent_class->map_event)
381     res = parent_class->map_event (widget, event);
382
383   clutter_actor_map (priv->stage);
384
385   return res;
386 }
387
388 static gboolean
389 gtk_clutter_embed_unmap_event (GtkWidget   *widget,
390                                GdkEventAny *event)
391 {
392   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv;
393   GtkWidgetClass *parent_class;
394   gboolean res = FALSE;
395
396   parent_class = GTK_WIDGET_CLASS (gtk_clutter_embed_parent_class);
397   if (parent_class->unmap_event)
398     res = parent_class->unmap_event (widget, event);
399
400   clutter_actor_unmap (priv->stage);
401
402   return res;
403 }
404
405 static void
406 gtk_clutter_embed_unmap (GtkWidget *widget)
407 {
408   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv;
409
410   clutter_actor_unmap (priv->stage);
411
412   GTK_WIDGET_CLASS (gtk_clutter_embed_parent_class)->unmap (widget);
413 }
414
415 static gboolean
416 gtk_clutter_embed_focus_in (GtkWidget     *widget,
417                             GdkEventFocus *event)
418 {
419   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv;
420
421   g_signal_emit_by_name (priv->stage, "activate");
422
423   return FALSE;
424 }
425
426 static gboolean
427 gtk_clutter_embed_focus_out (GtkWidget     *widget,
428                              GdkEventFocus *event)
429 {
430   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv;
431
432   g_signal_emit_by_name (priv->stage, "deactivate");
433
434   /* give back key focus to the stage */
435   clutter_stage_set_key_focus (CLUTTER_STAGE (priv->stage), NULL);
436
437   return FALSE;
438 }
439
440 static gboolean
441 gtk_clutter_embed_scroll_event (GtkWidget      *widget,
442                                 GdkEventScroll *event)
443 {
444   GtkClutterEmbedPrivate *priv = GTK_CLUTTER_EMBED (widget)->priv;
445
446   ClutterEvent cevent = { 0, };
447
448   if (event->type == GDK_SCROLL)
449     cevent.type = cevent.scroll.type = CLUTTER_SCROLL;
450   else
451     return FALSE;
452
453   cevent.any.stage = CLUTTER_STAGE (priv->stage);
454   cevent.scroll.x = (gint) event->x;
455   cevent.scroll.y = (gint) event->y;
456   cevent.scroll.time = event->time;
457   cevent.scroll.direction = event->direction;
458   cevent.scroll.modifier_state = event->state;
459
460   clutter_do_event (&cevent);
461
462   return FALSE;
463 }
464
465 static void
466 gtk_clutter_embed_style_set (GtkWidget *widget,
467                              GtkStyle  *old_style)
468 {
469   GdkScreen *screen;
470   GtkSettings *settings;
471   gdouble dpi;
472   gchar *font_name;
473   const cairo_font_options_t *font_options;
474   gint double_click_time, double_click_distance;
475   ClutterBackend *backend;
476
477   GTK_WIDGET_CLASS (gtk_clutter_embed_parent_class)->style_set (widget,
478                                                                 old_style);
479
480   if (gtk_widget_has_screen (widget))
481     screen = gtk_widget_get_screen (widget);
482   else
483     screen = gdk_screen_get_default ();
484
485   dpi = gdk_screen_get_resolution (screen);
486   if (dpi < 0)
487     dpi = 96.0;
488
489   font_options = gdk_screen_get_font_options (screen);
490
491   settings = gtk_settings_get_for_screen (screen);
492   g_object_get (G_OBJECT (settings),
493                 "gtk-font-name", &font_name,
494                 "gtk-double-click-time", &double_click_time,
495                 "gtk-double-click-distance", &double_click_distance,
496                 NULL);
497
498   /* copy all settings and values coming from GTK+ into
499    * the ClutterBackend; this way, a scene embedded into
500    * a GtkClutterEmbed will not look completely alien
501    */
502   backend = clutter_get_default_backend ();
503   clutter_backend_set_resolution (backend, dpi);
504   clutter_backend_set_font_options (backend, font_options);
505   clutter_backend_set_font_name (backend, font_name);
506   clutter_backend_set_double_click_time (backend, double_click_time);
507   clutter_backend_set_double_click_distance (backend, double_click_distance);
508
509   g_free (font_name);
510 }
511
512 static void
513 gtk_clutter_embed_class_init (GtkClutterEmbedClass *klass)
514 {
515   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
516   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
517
518   g_type_class_add_private (klass, sizeof (GtkClutterEmbedPrivate));
519
520   gobject_class->dispose = gtk_clutter_embed_dispose;
521
522   widget_class->style_set = gtk_clutter_embed_style_set;
523   widget_class->size_allocate = gtk_clutter_embed_size_allocate;
524   widget_class->realize = gtk_clutter_embed_realize;
525   widget_class->unrealize = gtk_clutter_embed_unrealize;
526   widget_class->show = gtk_clutter_embed_show;
527   widget_class->hide = gtk_clutter_embed_hide;
528   widget_class->unmap = gtk_clutter_embed_unmap;
529   widget_class->expose_event = gtk_clutter_embed_expose_event;
530   widget_class->button_press_event = gtk_clutter_embed_button_event;
531   widget_class->button_release_event = gtk_clutter_embed_button_event;
532   widget_class->key_press_event = gtk_clutter_embed_key_event;
533   widget_class->key_release_event = gtk_clutter_embed_key_event;
534   widget_class->motion_notify_event = gtk_clutter_embed_motion_notify_event;
535   widget_class->expose_event = gtk_clutter_embed_expose_event;
536   widget_class->map_event = gtk_clutter_embed_map_event;
537   widget_class->unmap_event = gtk_clutter_embed_unmap_event;
538   widget_class->focus_in_event = gtk_clutter_embed_focus_in;
539   widget_class->focus_out_event = gtk_clutter_embed_focus_out;
540   widget_class->scroll_event = gtk_clutter_embed_scroll_event;
541 }
542
543 static void
544 gtk_clutter_embed_init (GtkClutterEmbed *embed)
545 {
546   GtkClutterEmbedPrivate *priv;
547
548   embed->priv = priv = GTK_CLUTTER_EMBED_GET_PRIVATE (embed);
549
550   GTK_WIDGET_SET_FLAGS (embed, GTK_CAN_FOCUS);
551   GTK_WIDGET_UNSET_FLAGS (embed, GTK_NO_WINDOW);
552
553   /* disable double-buffering: it's automatically provided
554    * by OpenGL
555    */
556   gtk_widget_set_double_buffered (GTK_WIDGET (embed), FALSE);
557
558   /* we always create new stages rather than use the default */
559   priv->stage = clutter_stage_new ();
560
561   /* intercept the queue-redraw signal of the stage to know when
562    * Clutter-side requests a redraw; this way we can also request
563    * a redraw GTK-side
564    */
565   priv->queue_redraw_id =
566     g_signal_connect (priv->stage,
567                       "queue-redraw", G_CALLBACK (on_stage_queue_redraw),
568                       embed);
569 }
570
571 /**
572  * gtk_clutter_embed_new:
573  *
574  * Creates a new #GtkClutterEmbed widget. This widget can be
575  * used to build a scene using Clutter API into a GTK+ application.
576  *
577  * Return value: the newly created #GtkClutterEmbed
578  *
579  * Since: 0.6
580  */
581 GtkWidget *
582 gtk_clutter_embed_new (void)
583 {
584   return g_object_new (GTK_CLUTTER_TYPE_EMBED, NULL);
585 }
586
587 /**
588  * gtk_clutter_embed_get_stage:
589  * @embed: a #GtkClutterEmbed
590  *
591  * Retrieves the #ClutterStage from @embed. The returned stage can be
592  * used to add actors to the Clutter scene.
593  *
594  * Return value: the Clutter stage. You should never destroy or unref
595  *   the returned actor.
596  *
597  * Since: 0.6
598  */
599 ClutterActor *
600 gtk_clutter_embed_get_stage (GtkClutterEmbed *embed)
601 {
602   g_return_val_if_fail (GTK_CLUTTER_IS_EMBED (embed), NULL);
603
604   return embed->priv->stage;
605 }