Renamed src/ to hildon/
[hildon] / hildon / hildon-animation-actor.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2008 Nokia Corporation, all rights reserved.
5  *
6  * Contact: Rodrigo Novo <rodrigo.novo@nokia.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24
25 /**
26  * SECTION:hildon-animation-window
27  * @short_description: Widget representing an animation actor for
28  * WM-assisted animation effects in the Hildon framework.
29  *
30  * The #HildonAnimationActor is a GTK+ widget which represents an
31  * animation actor for WM-assisted animation effects in the Hildon
32  * framework. It derives from #GtkWindow and can host any widgets much
33  * like a normal window. The special features available to the
34  * animation actor is the ability to set its position, scale factor
35  * and rotation. These parameters are interpreted by Hildon's
36  * compositing window manager to alter the on-screen representation of
37  * the animation actor window. Bear in mind, however, that by design
38  * decision, animation actors are not reactive -- the widgets placed
39  * in such window will not receive keyboard, motion or button
40  * events. Animation actors are input-transparent -- the input events
41  * will be sent to the underlying real windows and widgets.
42  *
43  * The animation actors may exist in a parented or an unparented
44  * state. To be displayed, animations actors must be parented to
45  * another top-level window widget. Animation actors display on top
46  * (in front) of the standard window contents unless the position
47  * (depth, z-coordinate) is specifically adjusted. Animation actors in
48  * an unparented state do not display at all.
49  *
50  * Parented animation actors are placed in the coordinate space of the
51  * parent window and visually become a part of the parent window
52  * iteslf -- they inherit the transformations and effects enforced by
53  * the window manager on the parent window (task switcher animations,
54  * minimize events, etc.).
55  *
56  * All animation actor settings (position, scale, rotation, opacity,
57  * depth) can be applied to unparented actors, but will only take
58  * effect as the actor is parented to a top-level window and that
59  * window is shown. All settings are preserved during
60  * unparenting/reparenting.
61  *
62  * The #HildonAnimationActor API closely follows the #ClutterActor
63  * API.  Please take a look at the #ClutterActor description for the
64  * modelview transformations order that applies to
65  * HildonAnimationActor and ClutterActor alike.
66  *
67  * Animation actor widget controls the animation actor as it is
68  * transformed by the window manager using ClientMessage X11
69  * events. It tries to minimize the amount of such events and couples
70  * conceptually related parameters (visibility and opacity, position
71  * and depth) to the same message.  The API, however, offers
72  * convenience functions for the programmer to be able to modify every
73  * parameter individually.
74  *
75  * <example>
76  * <title>Basic HildonAnimationActor example</title>
77  * <programlisting>
78  * static void
79  * animation_cb (void *obj)
80  * {
81  *     HildonAnimationActor *actor = HILDON_ANIMATION_ACTOR (obj);
82  * <!-- -->
83  *     static int x_inc = 1;
84  *     static int y_inc = 1;
85  *     static int x = 0;
86  *     static int y = 0;
87  *     static int r = 0;
88  * <!-- -->
89  *     if (((x_inc > 0) && (x > 800)) ||
90  *         ((x_inc < 0) && (x < 1)))
91  *         x_inc = -x_inc;
92  *     if (((y_inc > 0) && (y > 480)) ||
93  *         ((y_inc < 0) && (y < 1)))
94  *         y_inc = -y_inc;
95  * <!-- -->
96  *     x += x_inc;
97  *     y += y_inc;
98  *     r ++;
99  * <!-- -->
100  *     // Set animation actor position and rotation
101  *     hildon_animation_actor_set_position (actor, x, y);
102  *     hildon_animation_actor_set_rotation (actor,
103  *                                          HILDON_AA_Z_AXIS,
104  *                                          r,
105  *                                          0, 0, 0);
106  * }
107  * <!-- -->
108  * int
109  * main (int argc, char **argv)
110  * {
111  *     GtkWidget *win;
112  *     GtkWidget *image;
113  *     GtkWidget *actor;
114  * <!-- -->
115  *     gtk_init (&amp;argc, &amp;argv);
116  * <!-- -->
117  *     // ... set up a normal window
118  *     win = hildon_window_new ();
119  *     g_signal_connect (win, "destroy", G_CALLBACK (gtk_main_quit), NULL);
120  *     gtk_widget_show_all (win);
121  * <!-- -->
122  *     // ... load an image
123  *     image = gtk_image_new_from_file ("image.jpg");
124  * <!-- -->
125  *     actor = hildon_animation_actor_new();
126  *     gtk_container_add (GTK_CONTAINER (actor), image);
127  * <!-- -->
128  *     // Parent the animation actor
129  *     hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (actor), win);
130  * <!-- -->
131  *     // Set anchor point to the actor center
132  *     hildon_animation_actor_set_anchor_from_gravity (HILDON_ANIMATION_ACTOR (actor),
133                                                        HILDON_AA_CENTER_GRAVITY);
134  * <!-- -->
135  *     gtk_widget_show_all (actor);
136  * <!-- -->
137  *     // Set up animation
138  *     g_timeout_add (100, (GSourceFunc)animation_cb, actor);
139  * <!-- -->
140  *     gtk_main ();
141  * <!-- -->
142  *     return 0;
143  * }
144  * </programlisting>
145  * </example>
146  */
147
148 #include                                        <gdk/gdkx.h>
149 #include                                        <X11/Xatom.h>
150
151 #include                                        "hildon-animation-actor.h"
152 #include                                        "hildon-animation-actor-private.h"
153
154 G_DEFINE_TYPE (HildonAnimationActor, hildon_animation_actor, GTK_TYPE_WINDOW);
155
156 static GdkFilterReturn
157 hildon_animation_actor_event_filter (GdkXEvent *xevent,
158                                      GdkEvent *event,
159                                      gpointer data);
160 static void
161 hildon_animation_actor_update_ready (HildonAnimationActor *self);
162 static void
163 hildon_animation_actor_send_pending_messages (HildonAnimationActor *self);
164 static void
165 hildon_animation_actor_send_all_messages (HildonAnimationActor *self);
166 static gboolean
167 hildon_animation_actor_parent_map_event (GtkWidget *parent,
168                                          GdkEvent *event,
169                                          gpointer user_data);
170 static gboolean
171 hildon_animation_actor_map_event (GtkWidget *widget,
172                                   GdkEvent *event,
173                                   gpointer user_data);
174
175 static guint32 show_atom;
176 static guint32 position_atom;
177 static guint32 rotation_atom;
178 static guint32 scale_atom;
179 static guint32 anchor_atom;
180 static guint32 parent_atom;
181 static guint32 ready_atom;
182
183 static gboolean atoms_initialized = FALSE;
184
185 static void
186 hildon_animation_actor_realize                 (GtkWidget *widget)
187 {
188     GdkDisplay *display;
189     Atom wm_type, applet_type;
190
191     GTK_WIDGET_CLASS (hildon_animation_actor_parent_class)->realize (widget);
192
193     /* Set animation actor window type. */
194
195     display = gdk_drawable_get_display (widget->window);
196
197     wm_type = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE");
198     applet_type = gdk_x11_get_xatom_by_name_for_display (display, "_HILDON_WM_WINDOW_TYPE_ANIMATION_ACTOR");
199
200     XChangeProperty (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XID (widget->window), wm_type,
201                      XA_ATOM, 32, PropModeReplace,
202                      (unsigned char *) &applet_type, 1);
203
204     /* This is a bit of a hack, but for the sake of speed (it is assumed that
205      * once HildonAnimationActor is created, a lot of ClientMessages will
206      * follow), we cache all ClientMessages atoms in static variables. */
207
208     if (!atoms_initialized)
209     {
210         show_atom =
211             gdk_x11_get_xatom_by_name_for_display
212             (display, "_HILDON_ANIMATION_CLIENT_MESSAGE_SHOW");
213         position_atom =
214             gdk_x11_get_xatom_by_name_for_display
215             (display, "_HILDON_ANIMATION_CLIENT_MESSAGE_POSITION");
216         rotation_atom =
217             gdk_x11_get_xatom_by_name_for_display
218             (display, "_HILDON_ANIMATION_CLIENT_MESSAGE_ROTATION");
219         scale_atom =
220             gdk_x11_get_xatom_by_name_for_display
221             (display, "_HILDON_ANIMATION_CLIENT_MESSAGE_SCALE");
222         anchor_atom =
223             gdk_x11_get_xatom_by_name_for_display
224             (display, "_HILDON_ANIMATION_CLIENT_MESSAGE_ANCHOR");
225         parent_atom =
226             gdk_x11_get_xatom_by_name_for_display
227             (display, "_HILDON_ANIMATION_CLIENT_MESSAGE_PARENT");
228         ready_atom =
229             gdk_x11_get_xatom_by_name_for_display
230             (display, "_HILDON_ANIMATION_CLIENT_READY");
231 #if 0
232         g_debug ("show atom = %lu\n", show_atom);
233         g_debug ("position atom = %lu\n", position_atom);
234         g_debug ("rotation atom = %lu\n", rotation_atom);
235         g_debug ("scale atom = %lu\n", scale_atom);
236         g_debug ("anchor atom = %lu\n", anchor_atom);
237         g_debug ("parent atom = %lu\n", parent_atom);
238         g_debug ("ready atom = %lu\n", ready_atom);
239 #endif
240
241         atoms_initialized = TRUE;
242     }
243
244     /* Wait for a ready message */
245
246     gdk_window_add_filter (widget->window,
247                            hildon_animation_actor_event_filter,
248                            widget);
249 }
250
251 static void
252 hildon_animation_actor_unrealize               (GtkWidget *widget)
253 {
254     gdk_window_remove_filter (widget->window,
255                               hildon_animation_actor_event_filter,
256                               widget);
257
258     GTK_WIDGET_CLASS (hildon_animation_actor_parent_class)->unrealize (widget);
259 }
260
261 static void
262 hildon_animation_actor_show                    (GtkWidget *widget)
263 {
264     HildonAnimationActor        *self = HILDON_ANIMATION_ACTOR (widget);
265
266     GTK_WIDGET_CLASS (hildon_animation_actor_parent_class)->show (widget);
267     hildon_animation_actor_set_show (self, 1);
268 }
269
270 static void
271 hildon_animation_actor_hide                    (GtkWidget *widget)
272 {
273     HildonAnimationActor        *self = HILDON_ANIMATION_ACTOR (widget);
274
275     hildon_animation_actor_set_show (self, 0);
276     GTK_WIDGET_CLASS (hildon_animation_actor_parent_class)->hide (widget);
277 }
278
279 static void
280 hildon_animation_actor_class_init              (HildonAnimationActorClass *klass)
281 {
282     GtkWidgetClass    *widget_class = GTK_WIDGET_CLASS (klass);
283
284     widget_class->realize           = hildon_animation_actor_realize;
285     widget_class->unrealize         = hildon_animation_actor_unrealize;
286     widget_class->show              = hildon_animation_actor_show;
287     widget_class->hide              = hildon_animation_actor_hide;
288
289     g_type_class_add_private (klass, sizeof (HildonAnimationActorPrivate));
290 }
291
292 static void
293 hildon_animation_actor_init                    (HildonAnimationActor *self)
294 {
295     HildonAnimationActorPrivate
296                        *priv = HILDON_ANIMATION_ACTOR_GET_PRIVATE (self);
297
298     /* Default non-zero values for the private variables */
299
300     priv->scale_x = 1 << 16;
301     priv->scale_y = 1 << 16;
302     priv->opacity = 0xff;
303 }
304
305 /**
306  * hildon_animation_actor_new:
307  *
308  * Creates a new #HildonAnimationActor.
309  *
310  * Return value: A #HildonAnimationActor
311  *
312  * Since: 2.2
313  **/
314 GtkWidget*
315 hildon_animation_actor_new                     (void)
316 {
317     HildonAnimationActor *newwindow = g_object_new (HILDON_TYPE_ANIMATION_ACTOR, NULL);
318
319     gtk_window_set_decorated (GTK_WINDOW (newwindow), FALSE);
320
321     return GTK_WIDGET (newwindow);
322 }
323
324 /*
325  * An filter for GDK X11 events, waiting for PropertyNotify (window property
326  * changes) events, keeping track of animation actor ready atom.
327  * Having the ready atom set on the window by the window manager will trigger
328  * updates of actor parameters (position/rotation/etc...) to be sent off
329  * to the window manager for processing.
330  */
331 static GdkFilterReturn
332 hildon_animation_actor_event_filter             (GdkXEvent *xevent,
333                                                  GdkEvent *event,
334                                                  gpointer data)
335 {
336     HildonAnimationActor *self = HILDON_ANIMATION_ACTOR (data);
337     XAnyEvent *any = xevent;
338
339     if (any->type == PropertyNotify)
340     {
341         XPropertyEvent *property = xevent;
342
343         if (property->atom == ready_atom)
344         {
345             hildon_animation_actor_update_ready (self);
346         }
347     }
348
349     return GDK_FILTER_CONTINUE;
350 }
351
352 /*
353  * Check for the ready atom on the animation actor X11 window.
354  * If present, send all pending animation actor messages to the
355  * window manager.
356  */
357 static void
358 hildon_animation_actor_update_ready (HildonAnimationActor *self)
359 {
360     HildonAnimationActorPrivate
361                        *priv = HILDON_ANIMATION_ACTOR_GET_PRIVATE (self);
362     GtkWidget          *widget = GTK_WIDGET (self);
363     Display            *display = GDK_WINDOW_XDISPLAY (widget->window);
364     Window              window = GDK_WINDOW_XID (widget->window);
365
366     int status;
367     gint xerror;
368
369     Atom actual_type;
370     int  actual_format;
371     unsigned long nitems, bytes_after;
372     unsigned char *prop = NULL;
373
374     /* Check for the "ready" property */
375
376     gdk_error_trap_push ();
377     status = XGetWindowProperty (display, window,
378                                  ready_atom, 0, 32,
379                                  False, XA_ATOM,
380                                  &actual_type, &actual_format,
381                                  &nitems, &bytes_after, &prop);
382     xerror = gdk_error_trap_pop();
383
384     if (prop)
385     {
386         /* We do not actually use the property value for anything,
387          * it is enough that the property is set. */
388
389         XFree (prop);
390     }
391
392     if (xerror ||
393         (status != Success) || (actual_type != XA_ATOM) ||
394         (actual_format != 32) || (nitems != 1))
395     {
396         priv->ready = 0;
397         return;
398     }
399
400     if (priv->ready)
401     {
402         /* The ready flag has been set once already. This means that
403          * the WM has restarted. Trigger re-mapping of the widget to
404          * update the texture actor first. Then push all animation
405          * actor settings anew. */
406
407         priv->map_event_cb_id =
408             g_signal_connect (G_OBJECT (self),
409                               "map-event",
410                               G_CALLBACK(hildon_animation_actor_map_event),
411                               self);
412
413         if (GTK_WIDGET_MAPPED (GTK_WIDGET (self)))
414         {
415             gtk_widget_unmap (GTK_WIDGET (self));
416             gtk_widget_map (GTK_WIDGET (self));
417         }
418
419         return;
420     }
421
422     priv->ready = 1;
423
424     /* Send all pending messages */
425
426     hildon_animation_actor_send_pending_messages (self);
427 }
428
429 static void
430 hildon_animation_actor_send_pending_messages (HildonAnimationActor *self)
431 {
432     HildonAnimationActorPrivate
433                        *priv = HILDON_ANIMATION_ACTOR_GET_PRIVATE (self);
434
435     if (priv->set_anchor)
436     {
437         if (priv->gravity == 0)
438             hildon_animation_actor_set_anchor (self,
439                                                priv->anchor_x,
440                                                priv->anchor_y);
441         else
442             hildon_animation_actor_set_anchor_from_gravity (self,
443                                                             priv->gravity);
444     }
445
446     if (priv->set_position)
447         hildon_animation_actor_set_position_full (self,
448                                                   priv->position_x,
449                                                   priv->position_y,
450                                                   priv->depth);
451
452     if (priv->set_rotation & (1 << HILDON_AA_X_AXIS))
453         hildon_animation_actor_set_rotationx (self,
454                                               HILDON_AA_X_AXIS,
455                                               priv->x_rotation_angle,
456                                               0,
457                                               priv->x_rotation_y,
458                                               priv->x_rotation_z);
459
460     if (priv->set_rotation & (1 << HILDON_AA_Y_AXIS))
461         hildon_animation_actor_set_rotationx (self,
462                                               HILDON_AA_Y_AXIS,
463                                               priv->y_rotation_angle,
464                                               priv->y_rotation_x,
465                                               0,
466                                               priv->y_rotation_z);
467
468     if (priv->set_rotation & (1 << HILDON_AA_Z_AXIS))
469         hildon_animation_actor_set_rotationx (self,
470                                               HILDON_AA_Z_AXIS,
471                                               priv->z_rotation_angle,
472                                               priv->z_rotation_x,
473                                               priv->z_rotation_y,
474                                               0);
475
476     if (priv->set_scale)
477         hildon_animation_actor_set_scalex (self,
478                                            priv->scale_x,
479                                            priv->scale_y);
480
481     if (priv->set_parent)
482         hildon_animation_actor_set_parent (self,
483                                            priv->parent);
484
485     if (priv->set_show)
486         hildon_animation_actor_set_show_full (self,
487                                               priv->show, priv->opacity);
488 }
489
490 static void
491 hildon_animation_actor_send_all_messages (HildonAnimationActor *self)
492 {
493     HildonAnimationActorPrivate
494                        *priv = HILDON_ANIMATION_ACTOR_GET_PRIVATE (self);
495
496     priv->set_anchor = 1;
497     priv->set_position = 1;
498     priv->set_rotation = (1 << HILDON_AA_X_AXIS) |
499                          (1 << HILDON_AA_Y_AXIS) |
500                          (1 << HILDON_AA_Z_AXIS);
501     priv->set_scale = 1;
502     priv->set_parent = 1;
503     priv->set_show = 1;
504
505     hildon_animation_actor_send_pending_messages (self);
506 }
507
508 /* ------------------------------------------------------------- */
509
510 /**
511  * hildon_animation_actor_send_message:
512  * @self: A #HildonAnimationActor
513  * @message_type: Message id for the animation actor message.
514  * @l0: 1st animation actor message parameter.
515  * @l1: 2nd animation actor message parameter.
516  * @l2: 3rd animation actor message parameter.
517  * @l3: 4th animation actor message parameter.
518  * @l4: 5th animation actor message parameter.
519  *
520  * Sends an X11 ClientMessage event to the window manager with
521  * the specified parameters -- id (@message_type) and data (@l0,
522  * @l1, @l2, @l3, @l4).
523  *
524  * This is an internal utility function that application will
525  * not need to call directly.
526  *
527  * Since: 2.2
528  **/
529 void
530 hildon_animation_actor_send_message (HildonAnimationActor *self,
531                                      guint32 message_type,
532                                      guint32 l0,
533                                      guint32 l1,
534                                      guint32 l2,
535                                      guint32 l3,
536                                      guint32 l4)
537 {
538     GtkWidget          *widget = GTK_WIDGET (self);
539     Display            *display = GDK_WINDOW_XDISPLAY (widget->window);
540     Window              window = GDK_WINDOW_XID (widget->window);
541 #if 0
542     XClientMessageEvent event;
543
544     event.type = ClientMessage;
545     event.window = window;
546     event.message_type = (Atom)message_type;
547     event.format = 32;
548     event.data.l[0] = l0;
549     event.data.l[1] = l1;
550     event.data.l[2] = l2;
551     event.data.l[3] = l3;
552     event.data.l[4] = l4;
553 #else
554     XEvent event = { 0 };
555
556     event.xclient.type = ClientMessage;
557     event.xclient.window = window;
558     event.xclient.message_type = (Atom)message_type;
559     event.xclient.format = 32;
560     event.xclient.data.l[0] = l0;
561     event.xclient.data.l[1] = l1;
562     event.xclient.data.l[2] = l2;
563     event.xclient.data.l[3] = l3;
564     event.xclient.data.l[4] = l4;
565 #endif
566
567 #if 0
568     g_debug ("%lu (%lu %lu %lu %lu %lu) -> %lu\n",
569              message_type,
570              l0, l1, l2, l3, l4,
571              window);
572 #endif
573
574     XSendEvent (display, window, True,
575                 StructureNotifyMask,
576                 (XEvent *)&event);
577 }
578
579 /**
580  * hildon_animation_actor_set_show_full:
581  * @self: A #HildonAnimationActor
582  * @show: A boolean flag setting the visibility of the animation actor.
583  * @opacity: Desired opacity setting
584  *
585  * Send a message to the window manager setting the visibility of
586  * the animation actor. This will only affect the visibility of
587  * the animation actor set by the compositing window manager in its own
588  * rendering pipeline, after X has drawn the window to the off-screen
589  * buffer. This setting, naturally, has no effect if the #HildonAnimationActor
590  * widget is not visible in X11 terms (i.e. realized and mapped).
591  *
592  * Furthermore, if a widget is parented, its final visibility will be
593  * affected by that of the parent window.
594  *
595  * The opacity setting ranges from zero (0), being completely transparent
596  * to 255 (0xff) being fully opaque.
597  *
598  * If the animation actor WM-counterpart is not ready, the show message
599  * will be queued until the WM is ready for it.
600  *
601  * Since: 2.2
602  **/
603 void
604 hildon_animation_actor_set_show_full (HildonAnimationActor *self,
605                                       gint show,
606                                       gint opacity)
607 {
608     HildonAnimationActorPrivate
609                        *priv = HILDON_ANIMATION_ACTOR_GET_PRIVATE (self);
610     GtkWidget          *widget = GTK_WIDGET (self);
611
612     if (opacity > 255)
613         opacity = 255;
614
615     if (opacity < 0)
616         opacity = 0;
617
618     priv->show = show;
619     priv->opacity = opacity;
620     priv->set_show = 1;
621
622     if (GTK_WIDGET_MAPPED (widget) && priv->ready)
623     {
624         /* Defer show messages until the animation actor is parented
625          * and the parent window is mapped */
626
627         if (!priv->parent || !GTK_WIDGET_MAPPED (GTK_WIDGET (priv->parent)))
628             return;
629
630         hildon_animation_actor_send_message (self,
631                                              show_atom,
632                                              show, opacity,
633                                              0, 0, 0);
634         priv->set_show = 0;
635     }
636 }
637
638 /**
639  * hildon_animation_actor_set_show:
640  * @self: A #HildonAnimationActor
641  * @show: A boolean flag setting the visibility of the animation actor.
642  *
643  * This function is a shortcut for hildon_animation_actor_set_show_full(),
644  * setting the overall actor visibility without changing it's opacity
645  * setting.
646  *
647  * Since: 2.2
648  **/
649 void
650 hildon_animation_actor_set_show (HildonAnimationActor *self,
651                                  gint show)
652 {
653     HildonAnimationActorPrivate
654                        *priv = HILDON_ANIMATION_ACTOR_GET_PRIVATE (self);
655
656     hildon_animation_actor_set_show_full (self,
657                                           show, priv->opacity);
658 }
659
660 /**
661  * hildon_animation_actor_set_opacity:
662  * @self: A #HildonAnimationActor
663  * @opacity: Desired opacity setting
664  *
665  * This function is a shortcut for hildon_animation_actor_set_show_full(),
666  * setting actor opacity without changing it's overall visibility.
667  *
668  * See hildon_animation_actor_set_show_full() for description of the range
669  * of values @opacity argument takes.
670  *
671  * Since: 2.2
672  **/
673 void
674 hildon_animation_actor_set_opacity (HildonAnimationActor *self,
675                                     gint opacity)
676 {
677     HildonAnimationActorPrivate
678                        *priv = HILDON_ANIMATION_ACTOR_GET_PRIVATE (self);
679
680     hildon_animation_actor_set_show_full (self,
681                                           priv->show, opacity);
682 }
683
684 /**
685  * hildon_animation_actor_set_position_full:
686  * @self: A #HildonAnimationActor
687  * @x: Desired X coordinate
688  * @y: Desired Y coordinate
689  * @depth: Desired window depth (Z coordinate)
690  *
691  * Send a message to the window manager setting the position of the
692  * animation actor. This will set the position of the animation
693  * actor off-screen bitmap as it is rendered to the screen. The
694  * position of the actor is relative to the parent window. The actor
695  * is also subject to the animation effects rendered by the compositing
696  * window manager on that window (like those by task switcher).
697  *
698  * The window depth affects the stacking of animation actors within
699  * a parent window and, more generally, the stacking of clutter actors
700  * within a stage/container. The default depth is 0 and a parent
701  * window's container will have it's window texture stacked at that level.
702  * The stacking at any depth level is sequential -- animation actor B
703  * created/parented after animation actor A will obscure the latter
704  * if they overlap.
705  *
706  * Animation actors with non-zero depth settings are subject to scaling as
707  * per the global scene perspective setup, which limits the depth setting
708  * as the primary parameter to control the stacking order. Since the
709  * stacking order follows the parenting order, it may be better to use
710  * hildon_animation_actor_set_parent() for setting the stacking.
711  *
712  * If the animation actor WM-counterpart is not ready, the show message
713  * will be queued until the WM is ready for it.
714  *
715  * Since: 2.2
716  **/
717 void
718 hildon_animation_actor_set_position_full (HildonAnimationActor *self,
719                                           gint x,
720                                           gint y,
721                                           gint depth)
722 {
723     HildonAnimationActorPrivate
724                        *priv = HILDON_ANIMATION_ACTOR_GET_PRIVATE (self);
725     GtkWidget          *widget = GTK_WIDGET (self);
726
727     priv->position_x = x;
728     priv->position_y = y;
729     priv->depth = depth;
730     priv->set_position = 1;
731
732     if (GTK_WIDGET_MAPPED (widget) && priv->ready)
733     {
734         hildon_animation_actor_send_message (self,
735                                              position_atom,
736                                              x, y, depth,
737                                              0, 0);
738         priv->set_position = 0;
739     }
740 }
741
742 /**
743  * hildon_animation_actor_set_position:
744  * @self: A #HildonAnimationActor
745  * @x: Desired window X coordinate
746  * @y: Desired window Y coordinate
747  *
748  * A shortcut for hildon_animation_actor_set_position_full(),
749  * changing the window position, but preserving it's depth setting.
750  *
751  * Since: 2.2
752  **/
753 void
754 hildon_animation_actor_set_position (HildonAnimationActor *self,
755                                      gint x,
756                                      gint y)
757 {
758     HildonAnimationActorPrivate
759                        *priv = HILDON_ANIMATION_ACTOR_GET_PRIVATE (self);
760
761     hildon_animation_actor_set_position_full (self,
762                                               x, y, priv->depth);
763 }
764
765 /**
766  * hildon_animation_actor_set_depth:
767  * @self: A #HildonAnimationActor
768  * @depth: Desired window depth (Z coordinate)
769  *
770  * A shortcut for hildon_animation_actor_set_position_full(),
771  * changing the window depth, but preserving it's position.
772  *
773  * Since: 2.2
774  **/
775 void
776 hildon_animation_actor_set_depth (HildonAnimationActor *self,
777                                   gint depth)
778 {
779     HildonAnimationActorPrivate
780                        *priv = HILDON_ANIMATION_ACTOR_GET_PRIVATE (self);
781
782     hildon_animation_actor_set_position_full (self,
783                                               priv->position_x,
784                                               priv->position_y,
785                                               depth);
786 }
787
788 /**
789  * hildon_animation_actor_set_scalex:
790  * @self: A #HildonAnimationActor
791  * @x_scale: Window's desired scale factor along the X-axis
792  * @y_scale: Window's desired scale factor along the Y-axis
793  *
794  * This function is just like hildon_animation_actor_set_scale(),
795  * but the scale factors are given as 16-bit fixed-point number.
796  *
797  * Since: 2.2
798  **/
799 void
800 hildon_animation_actor_set_scalex (HildonAnimationActor *self,
801                                    gint32 x_scale,
802                                    gint32 y_scale)
803 {
804     HildonAnimationActorPrivate
805                        *priv = HILDON_ANIMATION_ACTOR_GET_PRIVATE (self);
806     GtkWidget          *widget = GTK_WIDGET (self);
807
808     priv->scale_x = x_scale;
809     priv->scale_y = y_scale;
810     priv->set_scale = 1;
811
812     if (GTK_WIDGET_MAPPED (widget) && priv->ready)
813     {
814         hildon_animation_actor_send_message (self,
815                                              scale_atom,
816                                              x_scale, y_scale,
817                                              0, 0, 0);
818         priv->set_scale = 0;
819     }
820 }
821
822 /**
823  * hildon_animation_actor_set_scale:
824  * @self: A #HildonAnimationActor
825  * @x_scale: Window's desired scale factor along the X-axis
826  * @y_scale: Window's desired scale factor along the Y-axis
827  *
828  * Send a message to the window manager setting the scale factors of the
829  * animation actor. This will set the scale factors on the animation
830  * actor off-screen bitmap as it is rendered to the screen. If the
831  * animation actor is parented to another top-level window, the
832  * animation effects rendered by the compositing window manager
833  * on that top-level window (like those by task switcher) will
834  * also affect the animation actor.
835  *
836  * If the animation actor WM-counterpart is not ready, the show message
837  * will be queued until the WM is ready for it.
838  *
839  * Since: 2.2
840  **/
841 void
842 hildon_animation_actor_set_scale (HildonAnimationActor *self,
843                                   double x_scale,
844                                   double y_scale)
845 {
846     gint32 f_x_scale = x_scale * (1 << 16);
847     gint32 f_y_scale = y_scale * (1 << 16);
848
849     hildon_animation_actor_set_scalex (self, f_x_scale, f_y_scale);
850 }
851
852 /**
853  * hildon_animation_actor_set_rotationx:
854  * @self: A #HildonAnimationActor
855  * @axis: The rotation axis.
856  * @degrees: The rotation angle in degrees.
857  * @x: Center of the rotation, X coordinate.
858  * @y: Center of the rotation, Y coordinate.
859  * @z: Center of the rotation, Z coordinate.
860  *
861  * This function is just like hildon_animation_actor_set_rotation(),
862  * but the rotation angle is given as 16-bit fixed-point number.
863  *
864  * Since: 2.2
865  **/
866 void
867 hildon_animation_actor_set_rotationx (HildonAnimationActor *self,
868                                       gint   axis,
869                                       gint32 degrees,
870                                       gint   x,
871                                       gint   y,
872                                       gint   z)
873 {
874     HildonAnimationActorPrivate
875                        *priv = HILDON_ANIMATION_ACTOR_GET_PRIVATE (self);
876     GtkWidget          *widget = GTK_WIDGET (self);
877
878     guint mask = 0;
879
880     switch (axis)
881     {
882         case HILDON_AA_X_AXIS:
883             priv->x_rotation_angle = degrees;
884             priv->x_rotation_y = y;
885             priv->x_rotation_z = z;
886             mask = (1 << HILDON_AA_X_AXIS);
887             break;
888         case HILDON_AA_Y_AXIS:
889             priv->y_rotation_angle = degrees;
890             priv->y_rotation_x = x;
891             priv->y_rotation_z = z;
892             mask = (1 << HILDON_AA_Y_AXIS);
893             break;
894         case HILDON_AA_Z_AXIS:
895             priv->z_rotation_angle = degrees;
896             priv->z_rotation_x = x;
897             priv->z_rotation_y = y;
898             mask = (1 << HILDON_AA_Z_AXIS);
899             break;
900         default:
901             return;
902     }
903
904     priv->set_rotation |= mask;
905
906     if (GTK_WIDGET_MAPPED (widget) && priv->ready)
907     {
908         hildon_animation_actor_send_message (self,
909                                              rotation_atom,
910                                              axis, degrees, x, y, z);
911         priv->set_rotation &= ~mask;
912     }
913 }
914
915 /**
916  * hildon_animation_actor_set_rotation:
917  * @self: A #HildonAnimationActor
918  * @axis: The rotation axis.
919  * @degrees: The rotation angle in degrees.
920  * @x: Center of the rotation, X coordinate.
921  * @y: Center of the rotation, Y coordinate.
922  * @z: Center of the rotation, Z coordinate.
923  *
924  * Send a message to the window manager setting the animation actor
925  * rotation around one of the three axes. The rotation center coordinates
926  * depend on the axis of rotation:
927  *
928  *   * %HILDON_AA_X_AXIS requires @y and @z coordinates.
929  *   * %HILDON_AA_Y_AXIS requires @x and @z coordinates.
930  *   * %HILDON_AA_Z_AXIS requires @x and @y coordinates.
931  *
932  * If the animation actor WM-counterpart is not ready, the show message
933  * will be queued until the WM is ready for it.
934  *
935  * Since: 2.2
936  **/
937 void
938 hildon_animation_actor_set_rotation (HildonAnimationActor *self,
939                                      gint   axis,
940                                      double degrees,
941                                      gint   x,
942                                      gint   y,
943                                      gint   z)
944 {
945     gint32 f_degrees = degrees * (1 << 16);
946
947     hildon_animation_actor_set_rotationx (self, axis, f_degrees, x, y, z);
948 }
949
950 /**
951  * hildon_animation_actor_set_anchor:
952  * @self: A #HildonAnimationActor
953  * @x: The X coordinate of the anchor point.
954  * @y: The Y coordinate of the anchor point.
955  *
956  * Send a message to the window manager setting the anchor point for
957  * the animation actor. The anchor point is the point to which the
958  * actor position within its parent it is relative.
959  *
960  * If the animation actor WM-counterpart is not ready, the show message
961  * will be queued until the WM is ready for it.
962  *
963  * Since: 2.2
964  **/
965 void
966 hildon_animation_actor_set_anchor (HildonAnimationActor *self,
967                                    gint x,
968                                    gint y)
969 {
970     HildonAnimationActorPrivate
971                        *priv = HILDON_ANIMATION_ACTOR_GET_PRIVATE (self);
972     GtkWidget          *widget = GTK_WIDGET (self);
973
974     priv->gravity = 0;
975     priv->anchor_x = x;
976     priv->anchor_y = y;
977     priv->set_anchor = 1;
978
979     if (GTK_WIDGET_MAPPED (widget) && priv->ready)
980     {
981         hildon_animation_actor_send_message (self,
982                                              anchor_atom,
983                                              0, x, y,
984                                              0, 0);
985         priv->set_anchor = 0;
986     }
987 }
988
989 /**
990  * hildon_animation_actor_set_anchor_from_gravity:
991  * @self: A #HildonAnimationActor
992  * @gravity: The gravity constant.
993  *
994  * Send a message to the window manager setting the anchor point for
995  * the animation actor. The anchor point is the point to which the
996  * actor position within its parent it is relative. Instead of being
997  * defined in (x, y)-coordinates, the anchor point is defined in the
998  * relative "gravity" constant as:
999  *
1000  *   * %HILDON_AA_N_GRAVITY translates to (width / 2, 0) coordinate
1001  *   * %HILDON_AA_NE_GRAVITY translates to (width, 0) coordinate
1002  *   * %HILDON_AA_E_GRAVITY translates to (width, height / 2) coordinate
1003  *   * %HILDON_AA_SE_GRAVITY translates to (width, height) coordinate
1004  *   * %HILDON_AA_S_GRAVITY translates to (width / 2, height) coordinate
1005  *   * %HILDON_AA_SW_GRAVITY translates to (0, height) coordinate
1006  *   * %HILDON_AA_W_GRAVITY translates to (0, height / 2) coordinate
1007  *   * %HILDON_AA_NW_GRAVITY translates to (0, 0) coordinate
1008  *   * %HILDON_AA_CENTER_GRAVITY translates to (width / 2, height / 2) coordinate
1009  *
1010  * If the animation actor WM-counterpart is not ready, the show message
1011  * will be queued until the WM is ready for it.
1012  *
1013  * Since: 2.2
1014  **/
1015 void
1016 hildon_animation_actor_set_anchor_from_gravity (HildonAnimationActor *self,
1017                                                 guint gravity)
1018 {
1019     HildonAnimationActorPrivate
1020                        *priv = HILDON_ANIMATION_ACTOR_GET_PRIVATE (self);
1021     GtkWidget          *widget = GTK_WIDGET (self);
1022
1023     priv->gravity = gravity;
1024     priv->set_anchor = 1;
1025
1026     if (GTK_WIDGET_MAPPED (widget) && priv->ready)
1027     {
1028         hildon_animation_actor_send_message (self,
1029                                              anchor_atom,
1030                                              gravity, 0, 0,
1031                                              0, 0);
1032         priv->set_anchor = 0;
1033     }
1034 }
1035
1036 /*
1037  * This callback will be triggered by the parent widget of
1038  * an animation actor when it is mapped. The compositing
1039  * window manager is now ready to parent the animation actor
1040  * into the target parent window.
1041  */
1042 static gboolean
1043 hildon_animation_actor_parent_map_event (GtkWidget *parent,
1044                                          GdkEvent *event,
1045                                          gpointer user_data)
1046 {
1047     hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (user_data),
1048                                        GTK_WINDOW (parent));
1049     return FALSE;
1050 }
1051
1052 /*
1053  * This callback will be triggered by the widget re-mapping
1054  * itself in case of WM restarting. The point is to push all
1055  * animation actor parameters anew to the WM.
1056  */
1057 static gboolean
1058 hildon_animation_actor_map_event (GtkWidget *widget,
1059                                   GdkEvent *event,
1060                                   gpointer user_data)
1061 {
1062     HildonAnimationActor
1063                        *self = HILDON_ANIMATION_ACTOR (user_data);
1064     HildonAnimationActorPrivate
1065                        *priv = HILDON_ANIMATION_ACTOR_GET_PRIVATE (self);
1066
1067     hildon_animation_actor_send_all_messages (self);
1068
1069     /* Disconnect the "map-event" handler after the "emergency resend all
1070      * actor parameters" drill is over. */
1071
1072     if (priv->map_event_cb_id)
1073     {
1074         g_signal_handler_disconnect (self,
1075                                      priv->map_event_cb_id);
1076         priv->map_event_cb_id = 0;
1077     }
1078
1079     return FALSE;
1080 }
1081
1082 /**
1083  * hildon_animation_actor_set_parent:
1084  * @self: A #HildonAnimationActor
1085  * @parent: A #GtkWindow that the actor will be parented to.
1086  *
1087  * Send a message to the window manager setting the parent window
1088  * for the animation actor. Parenting an actor will not affect the
1089  * X window that the HildonAnimationActor represents, but it's off-screen
1090  * bitmap as it is handled by the compositing window manager.
1091  *
1092  * Parenting an animation actor will affect its visibility as set
1093  * by the gtk_widget_show(), gtk_widget_hide() and
1094  * hildon_animation_actor_set_show(). The animation actor will only be
1095  * visible when the top-level window it is parented is visible.
1096  *
1097  * Passing %NULL as a @parent argument will unparent the animation actor.
1098  * This will restore the actor's visibility if it was suppressed by
1099  * being unparented or parented to an unmapped window.
1100  *
1101  * If the animation actor WM-counterpart is not ready, the show message
1102  * will be queued until the WM is ready for it.
1103  *
1104  * Since: 2.2
1105  **/
1106 void
1107 hildon_animation_actor_set_parent (HildonAnimationActor *self,
1108                                    GtkWindow *parent)
1109 {
1110     HildonAnimationActorPrivate
1111                        *priv = HILDON_ANIMATION_ACTOR_GET_PRIVATE (self);
1112     GtkWidget          *widget = GTK_WIDGET (self);
1113
1114     gtk_window_set_transient_for (GTK_WINDOW (self), parent);
1115
1116     if (priv->parent != parent)
1117     {
1118         /* Setting a new parent */
1119
1120         if (priv->parent)
1121         {
1122             if (priv->parent_map_event_cb_id)
1123                 g_signal_handler_disconnect (priv->parent,
1124                                              priv->parent_map_event_cb_id);
1125
1126             /* Might need a synchronized "parent(0)" or "parent(new parent)"
1127              * message here before we can safely decrease the reference count. */
1128
1129             g_object_unref (priv->parent);
1130         }
1131
1132         priv->parent = parent;
1133         priv->set_parent = 1;
1134
1135         if (parent != 0)
1136         {
1137             /* The widget is being (re)parented, not unparented. */
1138
1139             g_object_ref (parent);
1140
1141             priv->parent_map_event_cb_id =
1142                 g_signal_connect (G_OBJECT (priv->parent),
1143                                   "map-event",
1144                                   G_CALLBACK(hildon_animation_actor_parent_map_event),
1145                                   self);
1146         }
1147         else
1148         {
1149             priv->parent_map_event_cb_id = 0;
1150         }
1151     }
1152
1153     if (GTK_WIDGET_MAPPED (widget) && priv->ready)
1154     {
1155         Window win = 0;
1156
1157         /* If the animation actor is being unparented or parented to an
1158          * unmapped widget, force its visibility to "hidden". */
1159
1160         if (!priv->parent || !GTK_WIDGET_MAPPED (GTK_WIDGET (priv->parent)))
1161         {
1162             hildon_animation_actor_send_message (self,
1163                                                  show_atom,
1164                                                  0, priv->opacity,
1165                                                  0, 0, 0);
1166         }
1167
1168         /* If the widget is being parented (parent != 0), only proceed when
1169          * the parent widget is realized, since we need the X window id of
1170          * the parent. If the widget is being unparented (parent == 0), pass
1171          * the "special" window id of 0 in the message. */
1172
1173         if (priv->parent)
1174         {
1175             if (!GTK_WIDGET_MAPPED (GTK_WIDGET (priv->parent)))
1176                 return;
1177
1178             GdkWindow *gdk = GTK_WIDGET (parent)->window;
1179             win = GDK_WINDOW_XID (gdk);
1180         }
1181
1182         hildon_animation_actor_send_message (self,
1183                                              parent_atom,
1184                                              win,
1185                                              0, 0, 0, 0);
1186         priv->set_parent = 0;
1187
1188         /* Set animation actor visibility to desired value (in case it was
1189          * forced off when the actor was parented into an unmapped widget). */
1190
1191         hildon_animation_actor_send_message (self,
1192                                              show_atom,
1193                                              priv->show, priv->opacity,
1194                                              0, 0, 0);
1195         priv->set_show = 0;
1196     }
1197 }
1198