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