2009-03-04 Alberto Garcia <agarcia@igalia.com>
[hildon] / src / hildon-window-stack.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2008 Nokia Corporation, all rights reserved.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation; version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19  * 02110-1301 USA
20  *
21  */
22
23 /**
24  * SECTION:hildon-window-stack
25  * @short_description: Object representing a stack of windows in the Hildon framework
26  * @see_also: #HildonStackableWindow
27  *
28  * The #HildonWindowStack is an object used to represent a stack of
29  * windows in the Hildon framework.
30  *
31  * Stacks contain all #HildonStackableWindow<!-- -->s that are being
32  * shown. The user can only interact with the topmost window from each
33  * stack (as it covers all the others), but all of them are mapped and
34  * visible from the Gtk point of view.
35  *
36  * Each window can only be in one stack at a time. All stacked windows
37  * are visible and all visible windows are stacked.
38  *
39  * Each application has a default stack, and windows are automatically
40  * added to it when they are shown with gtk_widget_show().
41  *
42  * Additional stacks can be created at any time using
43  * hildon_window_stack_new(). To add a window to a specific stack, use
44  * hildon_window_stack_push_1() (remember that, for the default stack,
45  * gtk_widget_show() can be used instead).
46  *
47  * To remove a window from a stack use hildon_window_stack_pop_1(), or
48  * simply gtk_widget_hide().
49  *
50  * For more complex layout changes, applications can push and/or pop
51  * several windows at the same time in a single step. See
52  * hildon_window_stack_push(), hildon_window_stack_pop() and
53  * hildon_window_stack_pop_and_push() for more details.
54  */
55
56 #include                                        "hildon-window-stack.h"
57 #include                                        "hildon-window-stack-private.h"
58 #include                                        "hildon-stackable-window-private.h"
59
60 struct                                          _HildonWindowStackPrivate
61 {
62     GList *list;
63     GtkWindowGroup *group;
64     GdkWindow *leader; /* X Window group hint for all windows in a group */
65 };
66
67 #define                                         HILDON_WINDOW_STACK_GET_PRIVATE(obj) \
68                                                 (G_TYPE_INSTANCE_GET_PRIVATE ((obj),\
69                                                 HILDON_TYPE_WINDOW_STACK, HildonWindowStackPrivate))
70
71 G_DEFINE_TYPE (HildonWindowStack, hildon_window_stack, G_TYPE_OBJECT);
72
73 enum {
74     PROP_GROUP = 1,
75 };
76
77 static void
78 hildon_window_stack_set_window_group             (HildonWindowStack *stack,
79                                                   GtkWindowGroup    *group)
80 {
81     g_return_if_fail (HILDON_IS_WINDOW_STACK (stack));
82     g_return_if_fail (!group || GTK_IS_WINDOW_GROUP (group));
83
84     /* The window group is only to be set once during construction */
85     g_return_if_fail (stack->priv->group == NULL);
86
87     if (!group)
88         group = gtk_window_group_new ();
89
90     stack->priv->group = group;
91 }
92
93 static GtkWindowGroup *
94 hildon_window_stack_get_window_group             (HildonWindowStack *stack)
95 {
96     g_return_val_if_fail (HILDON_IS_WINDOW_STACK (stack), NULL);
97
98     return stack->priv->group;
99 }
100
101 /**
102  * hildon_window_stack_get_default:
103  *
104  * Returns the default window stack. This stack always exists and
105  * doesn't need to be created by the application.
106  *
107  * Return value: the default #HildonWindowStack
108  * 
109  * Since: 2.2
110  **/
111 HildonWindowStack *
112 hildon_window_stack_get_default                 (void)
113 {
114     static HildonWindowStack *stack = NULL;
115     if (G_UNLIKELY (stack == NULL)) {
116         stack = g_object_new (HILDON_TYPE_WINDOW_STACK,
117                               "window-group", gtk_window_get_group (NULL),
118                               NULL);
119     }
120     return stack;
121 }
122
123 /**
124  * hildon_window_stack_new:
125  *
126  * Creates a new #HildonWindowStack. The stack is initially empty.
127  *
128  * Return value: a new #HildonWindowStack
129  *
130  * Since: 2.2
131  **/
132 HildonWindowStack *
133 hildon_window_stack_new                         (void)
134 {
135     HildonWindowStack *stack = g_object_new (HILDON_TYPE_WINDOW_STACK, NULL);
136     return stack;
137 }
138
139 /**
140  * hildon_window_stack_size:
141  * @stack: A #HildonWindowStack
142  *
143  * Returns the number of windows in @stack
144  *
145  * Return value: Number of windows in @stack
146  *
147  * Since: 2.2
148  **/
149 gint
150 hildon_window_stack_size                        (HildonWindowStack *stack)
151 {
152     g_return_val_if_fail (HILDON_IS_WINDOW_STACK (stack), 0);
153
154     return g_list_length (stack->priv->list);
155 }
156
157 static GdkWindow *
158 hildon_window_stack_get_leader_window           (HildonWindowStack *stack,
159                                                  GtkWidget         *win)
160 {
161     /* Create the X Window group (leader) if we haven't. */
162     if (!stack->priv->leader) {
163         if (stack == hildon_window_stack_get_default ()) {
164             GdkDisplay *dpy;
165
166             /* We're the default stack, use the default group. */
167             dpy = gtk_widget_get_display (win);
168             stack->priv->leader = gdk_display_get_default_group (dpy);
169         } else {
170             static GdkWindowAttr attr = {
171                 .window_type = GDK_WINDOW_TOPLEVEL,
172                 .x = 10, .y = 10, .width = 10, .height = 10,
173                 .wclass = GDK_INPUT_OUTPUT, .event_mask = 0,
174             };
175             GdkWindow *root;
176
177             /* Create a new X Window group. */
178             root = gtk_widget_get_root_window (win);
179             stack->priv->leader = gdk_window_new (root, &attr, GDK_WA_X | GDK_WA_Y);
180         }
181     }
182
183     return stack->priv->leader;
184 }
185
186 /* Set the X Window group of a window when it is realized. */
187 static void
188 hildon_window_stack_window_realized             (GtkWidget         *win,
189                                                  HildonWindowStack *stack)
190 {
191     GdkWindow *leader = hildon_window_stack_get_leader_window (stack, win);
192     gdk_window_set_group (win->window, leader);
193 }
194
195 /* Remove a window from its stack, no matter its position */
196 void G_GNUC_INTERNAL
197 hildon_window_stack_remove                      (HildonStackableWindow *win)
198 {
199     HildonWindowStack *stack = hildon_stackable_window_get_stack (win);
200
201     /* If the window is stacked */
202     if (stack) {
203         GList *pos;
204
205         hildon_stackable_window_set_stack (win, NULL, -1);
206         gtk_window_set_transient_for (GTK_WINDOW (win), NULL);
207         if (GTK_WIDGET (win)->window) {
208             gdk_window_set_group (GTK_WIDGET (win)->window, NULL);
209         }
210
211         /* If the window removed is in the middle of the stack, update
212          * transiency of other windows */
213         pos = g_list_find (stack->priv->list, win);
214         g_assert (pos != NULL);
215         if (pos->prev) {
216             GtkWindow *upper = GTK_WINDOW (pos->prev->data);
217             GtkWindow *lower = pos->next ? GTK_WINDOW (pos->next->data) : NULL;
218             gtk_window_set_transient_for (upper, lower);
219         }
220
221         stack->priv->list = g_list_remove (stack->priv->list, win);
222
223         g_signal_handlers_disconnect_by_func (win, hildon_window_stack_window_realized, stack);
224     }
225 }
226
227 /**
228  * hildon_window_stack_peek:
229  * @stack: A %HildonWindowStack
230  *
231  * Returns the window on top of @stack. The stack is never modified.
232  *
233  * Return value: the window on top of the stack, or %NULL if the stack
234  * is empty.
235  *
236  * Since: 2.2
237  **/
238 GtkWidget *
239 hildon_window_stack_peek                        (HildonWindowStack *stack)
240 {
241     GtkWidget *win = NULL;
242
243     g_return_val_if_fail (HILDON_IS_WINDOW_STACK (stack), NULL);
244
245     if (stack->priv->list != NULL) {
246         win = GTK_WIDGET (stack->priv->list->data);
247     }
248
249     return win;
250 }
251
252 /* This function does everything to push a window to the stack _but_
253  * actually calling gtk_widget_show().
254  * It's up to each specific push function to decide the order in which
255  * to show windows. */
256 gboolean G_GNUC_INTERNAL
257 _hildon_window_stack_do_push                    (HildonWindowStack     *stack,
258                                                  HildonStackableWindow *win)
259 {
260     HildonWindowStack *current_stack;
261
262     g_return_val_if_fail (HILDON_IS_WINDOW_STACK (stack), FALSE);
263     g_return_val_if_fail (HILDON_IS_STACKABLE_WINDOW (win), FALSE);
264
265     current_stack = hildon_stackable_window_get_stack (win);
266
267     if (current_stack == NULL) {
268         GtkWidget *parent = hildon_window_stack_peek (stack);
269
270         /* Push the window */
271         hildon_stackable_window_set_stack (win, stack, g_list_length (stack->priv->list));
272         stack->priv->list = g_list_prepend (stack->priv->list, win);
273
274         /* Make the window part of the same group as its parent */
275         if (parent) {
276             gtk_window_set_transient_for (GTK_WINDOW (win), GTK_WINDOW (parent));
277         } else {
278             gtk_window_group_add_window (stack->priv->group, GTK_WINDOW (win));
279         }
280
281         /* Set win's group after it's been realized. */
282         g_signal_connect (win, "realize",
283                           G_CALLBACK (hildon_window_stack_window_realized),
284                           stack);
285
286         return TRUE;
287     } else {
288         g_warning ("Trying to push a window that is already on a stack");
289         return FALSE;
290     }
291 }
292
293 static GtkWidget *
294 _hildon_window_stack_do_pop                     (HildonWindowStack *stack)
295 {
296     GtkWidget *win = hildon_window_stack_peek (stack);
297
298     if (win)
299         hildon_window_stack_remove (HILDON_STACKABLE_WINDOW (win));
300
301     return win;
302 }
303
304 /**
305  * hildon_window_stack_push_1:
306  * @stack: A %HildonWindowStack
307  * @win: A %HildonStackableWindow
308  *
309  * Adds @win to the top of @stack, and shows it. The window must not
310  * be already stacked.
311  *
312  * Since: 2.2
313  **/
314 void
315 hildon_window_stack_push_1                      (HildonWindowStack     *stack,
316                                                  HildonStackableWindow *win)
317 {
318     if (_hildon_window_stack_do_push (stack, win))
319         gtk_widget_show (GTK_WIDGET (win));
320 }
321
322 /**
323  * hildon_window_stack_pop_1:
324  * @stack: A %HildonWindowStack
325  *
326  * Removes the window on top of @stack, and hides it. If the stack is
327  * empty nothing happens.
328  *
329  * Return value: the window on top of the stack, or %NULL if the stack
330  * is empty.
331  *
332  * Since: 2.2
333  **/
334 GtkWidget *
335 hildon_window_stack_pop_1                       (HildonWindowStack *stack)
336 {
337     GtkWidget *win = _hildon_window_stack_do_pop (stack);
338     if (win)
339         gtk_widget_hide (win);
340     return win;
341 }
342
343 /**
344  * hildon_window_stack_push_list:
345  * @stack: A %HildonWindowStack
346  * @list: A list of %HildonStackableWindow<!-- -->s to push
347  *
348  * Pushes all windows in @list to the top of @stack, and shows
349  * them. Everything is done in a single transition, so the user will
350  * only see the last window in @list during this operation. None of
351  * the windows must be already stacked.
352  *
353  * Since: 2.2
354  **/
355 void
356 hildon_window_stack_push_list                   (HildonWindowStack *stack,
357                                                  GList             *list)
358 {
359     HildonStackableWindow *win;
360     GList *l;
361     GList *pushed = NULL;
362
363     g_return_if_fail (HILDON_IS_WINDOW_STACK (stack));
364
365     /* Stack all windows */
366     for (l = list; l != NULL; l = g_list_next (l)) {
367         win = HILDON_STACKABLE_WINDOW (l->data);
368         if (win) {
369             _hildon_window_stack_do_push (stack, win);
370             pushed = g_list_prepend (pushed, win);
371         } else {
372             g_warning ("Trying to stack a non-stackable window!");
373         }
374     }
375
376     /* Show windows in reverse order (topmost first) */
377     g_list_foreach (pushed, (GFunc) gtk_widget_show, NULL);
378
379     g_list_free (pushed);
380 }
381
382 /**
383  * hildon_window_stack_push:
384  * @stack: A %HildonWindowStack
385  * @win1: The first window to push
386  * @Varargs: A %NULL-terminated list of additional #HildonStackableWindow<!-- -->s to push.
387  *
388  * Pushes all windows to the top of @stack, and shows them. Everything
389  * is done in a single transition, so the user will only see the last
390  * window. None of the windows must be already stacked.
391  *
392  * Since: 2.2
393  **/
394 void
395 hildon_window_stack_push                        (HildonWindowStack     *stack,
396                                                  HildonStackableWindow *win1,
397                                                  ...)
398 {
399     HildonStackableWindow *win = win1;
400     GList *list = NULL;
401     va_list args;
402
403     va_start (args, win1);
404
405     while (win != NULL) {
406         list = g_list_prepend (list, win);
407         win = va_arg (args, HildonStackableWindow *);
408     }
409
410     va_end (args);
411
412     list = g_list_reverse (list);
413
414     hildon_window_stack_push_list (stack, list);
415     g_list_free (list);
416 }
417
418 /**
419  * hildon_window_stack_pop:
420  * @stack: A %HildonWindowStack
421  * @nwindows: Number of windows to pop
422  * @popped_windows: if non-%NULL, the list of popped windows is stored here
423  *
424  * Pops @nwindows windows from @stack, and hides them. Everything is
425  * done in a single transition, so the user will not see any of the
426  * windows being popped in this operation.
427  *
428  * If @popped_windows is not %NULL, the list of popped windows is
429  * stored there (ordered bottom-up). That list must be freed by the
430  * user.
431  *
432  * Since: 2.2
433  **/
434 void
435 hildon_window_stack_pop                         (HildonWindowStack  *stack,
436                                                  gint                nwindows,
437                                                  GList             **popped_windows)
438 {
439     gint i;
440     GList *popped = NULL;
441
442     g_return_if_fail (HILDON_IS_WINDOW_STACK (stack));
443     g_return_if_fail (nwindows > 0);
444     g_return_if_fail (g_list_length (stack->priv->list) >= nwindows);
445
446     /* Pop windows */
447     for (i = 0; i < nwindows; i++) {
448         GtkWidget *win = _hildon_window_stack_do_pop (stack);
449         popped = g_list_prepend (popped, win);
450     }
451
452     /* Hide windows in reverse order (topmost last) */
453     g_list_foreach (popped, (GFunc) gtk_widget_hide, NULL);
454
455     if (popped_windows) {
456         *popped_windows = popped;
457     } else {
458         g_list_free (popped);
459     }
460 }
461
462 /**
463  * hildon_window_stack_pop_and_push_list:
464  * @stack: A %HildonWindowStack
465  * @nwindows: Number of windows to pop.
466  * @popped_windows: if non-%NULL, the list of popped windows is stored here
467  * @list: A list of %HildonStackableWindow<!-- -->s to push
468  *
469  * Pops @nwindows windows from @stack (and hides them), then pushes
470  * all windows in @list (and shows them). Everything is done in a
471  * single transition, so the user will only see the last window from
472  * @list. None of the pushed windows must be already stacked.
473  *
474  * If @popped_windows is not %NULL, the list of popped windows is
475  * stored there (ordered bottom-up). That list must be freed by the
476  * user.
477  *
478  * Since: 2.2
479  **/
480 void
481 hildon_window_stack_pop_and_push_list           (HildonWindowStack  *stack,
482                                                  gint                nwindows,
483                                                  GList             **popped_windows,
484                                                  GList              *list)
485 {
486     gint i;
487     GList *l;
488     GList *popped = NULL;
489     GList *pushed = NULL;
490
491     g_return_if_fail (HILDON_IS_WINDOW_STACK (stack));
492     g_return_if_fail (nwindows > 0);
493     g_return_if_fail (g_list_length (stack->priv->list) >= nwindows);
494
495     /* Pop windows */
496     for (i = 0; i < nwindows; i++) {
497         GtkWidget *win = _hildon_window_stack_do_pop (stack);
498         popped = g_list_prepend (popped, win);
499     }
500
501     /* Push windows */
502     for (l = list; l != NULL; l = g_list_next (l)) {
503         HildonStackableWindow *win = HILDON_STACKABLE_WINDOW (l->data);
504         if (win) {
505             _hildon_window_stack_do_push (stack, win);
506             pushed = g_list_prepend (pushed, win);
507         } else {
508             g_warning ("Trying to stack a non-stackable window!");
509         }
510     }
511
512     /* Show windows in reverse order (topmost first) */
513     g_list_foreach (pushed, (GFunc) gtk_widget_show, NULL);
514
515     /* Hide windows in reverse order (topmost last) */
516     g_list_foreach (popped, (GFunc) gtk_widget_hide, NULL);
517
518     g_list_free (pushed);
519     if (popped_windows) {
520         *popped_windows = popped;
521     } else {
522         g_list_free (popped);
523     }
524 }
525
526 /**
527  * hildon_window_stack_pop_and_push:
528  * @stack: A %HildonWindowStack
529  * @nwindows: Number of windows to pop.
530  * @popped_windows: if non-%NULL, the list of popped windows is stored here
531  * @win1: The first window to push
532  * @Varargs: A %NULL-terminated list of additional #HildonStackableWindow<!-- -->s to push.
533  *
534  * Pops @nwindows windows from @stack (and hides them), then pushes
535  * all passed windows (and shows them). Everything is done in a single
536  * transition, so the user will only see the last pushed window. None
537  * of the pushed windows must be already stacked.
538  *
539  * If @popped_windows is not %NULL, the list of popped windows is
540  * stored there (ordered bottom-up). That list must be freed by the
541  * user.
542  *
543  * Since: 2.2
544  **/
545 void
546 hildon_window_stack_pop_and_push                (HildonWindowStack      *stack,
547                                                  gint                    nwindows,
548                                                  GList                 **popped_windows,
549                                                  HildonStackableWindow  *win1,
550                                                  ...)
551 {
552     HildonStackableWindow *win = win1;
553     GList *list = NULL;
554     va_list args;
555
556     va_start (args, win1);
557
558     while (win != NULL) {
559         list = g_list_prepend (list, win);
560         win = va_arg (args, HildonStackableWindow *);
561     }
562
563     va_end (args);
564
565     list = g_list_reverse (list);
566
567     hildon_window_stack_pop_and_push_list (stack, nwindows, popped_windows, list);
568     g_list_free (list);
569 }
570
571 static void
572 hildon_window_stack_finalize (GObject *object)
573 {
574     HildonWindowStack *stack = HILDON_WINDOW_STACK (object);
575
576     if (stack->priv->list)
577         hildon_window_stack_pop (stack, hildon_window_stack_size (stack), NULL);
578
579     if (stack->priv->group)
580         g_object_unref (stack->priv->group);
581
582     /* Since the default group stack shouldn't be finalized,
583      * it's safe to destroy the X Window group we created. */
584     if (stack->priv->leader)
585         gdk_window_destroy (stack->priv->leader);
586
587     G_OBJECT_CLASS (hildon_window_stack_parent_class)->finalize (object);
588 }
589
590 static void
591 hildon_window_stack_set_property                (GObject      *object,
592                                                  guint         prop_id,
593                                                  const GValue *value,
594                                                  GParamSpec   *pspec)
595 {
596     HildonWindowStack *stack = HILDON_WINDOW_STACK (object);
597
598     switch (prop_id)
599     {
600     case PROP_GROUP:
601         hildon_window_stack_set_window_group (stack, g_value_get_object (value));
602         break;
603     default:
604         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
605         break;
606     }
607 }
608
609 static void
610 hildon_window_stack_get_property                (GObject    *object,
611                                                  guint       prop_id,
612                                                  GValue     *value,
613                                                  GParamSpec *pspec)
614 {
615     HildonWindowStack *stack = HILDON_WINDOW_STACK (object);
616
617     switch (prop_id)
618     {
619     case PROP_GROUP:
620         g_value_set_object (value, hildon_window_stack_get_window_group (stack));
621         break;
622     default:
623         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
624         break;
625     }
626 }
627
628 static void
629 hildon_window_stack_class_init (HildonWindowStackClass *klass)
630 {
631     GObjectClass *gobject_class = (GObjectClass *)klass;
632
633     gobject_class->set_property = hildon_window_stack_set_property;
634     gobject_class->get_property = hildon_window_stack_get_property;
635     gobject_class->finalize = hildon_window_stack_finalize;
636
637     g_object_class_install_property (
638         gobject_class,
639         PROP_GROUP,
640         g_param_spec_object (
641             "window-group",
642             "GtkWindowGroup for this stack",
643             "GtkWindowGroup that all windows on this stack belong to",
644             GTK_TYPE_WINDOW_GROUP,
645             G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
646
647     g_type_class_add_private (klass, sizeof (HildonWindowStackPrivate));
648 }
649
650 static void
651 hildon_window_stack_init (HildonWindowStack *self)
652 {
653     HildonWindowStackPrivate *priv;
654
655     priv = self->priv = HILDON_WINDOW_STACK_GET_PRIVATE (self);
656
657     priv->list = NULL;
658     priv->group = NULL;
659 }