Renamed src/ to hildon/
[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: 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_get_windows:
229  * @stack: a #HildonWindowStack
230  *
231  * Returns the list of windows on this stack (topmost first). The
232  * widgets in the list are not individually referenced. Once you are
233  * done with the list you must call g_list_free().
234  *
235  * Returns: a newly-allocated list of #HildonStackableWindow<!-- -->s
236  *
237  * Since: 2.2
238  **/
239 GList *
240 hildon_window_stack_get_windows                 (HildonWindowStack *stack)
241 {
242     g_return_val_if_fail (HILDON_IS_WINDOW_STACK (stack), NULL);
243
244     return g_list_copy (stack->priv->list);
245 }
246
247 /**
248  * hildon_window_stack_peek:
249  * @stack: A %HildonWindowStack
250  *
251  * Returns the window on top of @stack. The stack is never modified.
252  *
253  * Return value: the window on top of the stack, or %NULL if the stack
254  * is empty.
255  *
256  * Since: 2.2
257  **/
258 GtkWidget *
259 hildon_window_stack_peek                        (HildonWindowStack *stack)
260 {
261     GtkWidget *win = NULL;
262
263     g_return_val_if_fail (HILDON_IS_WINDOW_STACK (stack), NULL);
264
265     if (stack->priv->list != NULL) {
266         win = GTK_WIDGET (stack->priv->list->data);
267     }
268
269     return win;
270 }
271
272 /* This function does everything to push a window to the stack _but_
273  * actually calling gtk_widget_show().
274  * It's up to each specific push function to decide the order in which
275  * to show windows. */
276 gboolean G_GNUC_INTERNAL
277 _hildon_window_stack_do_push                    (HildonWindowStack     *stack,
278                                                  HildonStackableWindow *win)
279 {
280     HildonWindowStack *current_stack;
281
282     g_return_val_if_fail (HILDON_IS_WINDOW_STACK (stack), FALSE);
283     g_return_val_if_fail (HILDON_IS_STACKABLE_WINDOW (win), FALSE);
284
285     current_stack = hildon_stackable_window_get_stack (win);
286
287     if (current_stack == NULL) {
288         GtkWidget *parent = hildon_window_stack_peek (stack);
289
290         /* Push the window */
291         hildon_stackable_window_set_stack (win, stack, g_list_length (stack->priv->list));
292         stack->priv->list = g_list_prepend (stack->priv->list, win);
293
294         /* Make the window part of the same group as its parent */
295         if (parent) {
296             gtk_window_set_transient_for (GTK_WINDOW (win), GTK_WINDOW (parent));
297         } else {
298             gtk_window_group_add_window (stack->priv->group, GTK_WINDOW (win));
299         }
300
301         /* Set win's group after it's been realized. */
302         g_signal_connect (win, "realize",
303                           G_CALLBACK (hildon_window_stack_window_realized),
304                           stack);
305
306         return TRUE;
307     } else {
308         g_warning ("Trying to push a window that is already on a stack");
309         return FALSE;
310     }
311 }
312
313 static GtkWidget *
314 _hildon_window_stack_do_pop                     (HildonWindowStack *stack)
315 {
316     GtkWidget *win = hildon_window_stack_peek (stack);
317
318     if (win)
319         hildon_window_stack_remove (HILDON_STACKABLE_WINDOW (win));
320
321     return win;
322 }
323
324 /**
325  * hildon_window_stack_push_1:
326  * @stack: A %HildonWindowStack
327  * @win: A %HildonStackableWindow
328  *
329  * Adds @win to the top of @stack, and shows it. The window must not
330  * be already stacked.
331  *
332  * Since: 2.2
333  **/
334 void
335 hildon_window_stack_push_1                      (HildonWindowStack     *stack,
336                                                  HildonStackableWindow *win)
337 {
338     if (_hildon_window_stack_do_push (stack, win))
339         gtk_widget_show (GTK_WIDGET (win));
340 }
341
342 /**
343  * hildon_window_stack_pop_1:
344  * @stack: A %HildonWindowStack
345  *
346  * Removes the window on top of @stack, and hides it. If the stack is
347  * empty nothing happens.
348  *
349  * Return value: the window on top of the stack, or %NULL if the stack
350  * is empty.
351  *
352  * Since: 2.2
353  **/
354 GtkWidget *
355 hildon_window_stack_pop_1                       (HildonWindowStack *stack)
356 {
357     GtkWidget *win = _hildon_window_stack_do_pop (stack);
358     if (win)
359         gtk_widget_hide (win);
360     return win;
361 }
362
363 /**
364  * hildon_window_stack_push_list:
365  * @stack: A %HildonWindowStack
366  * @list: A list of %HildonStackableWindow<!-- -->s to push
367  *
368  * Pushes all windows in @list to the top of @stack, and shows
369  * them. Everything is done in a single transition, so the user will
370  * only see the last window in @list during this operation. None of
371  * the windows must be already stacked.
372  *
373  * Since: 2.2
374  **/
375 void
376 hildon_window_stack_push_list                   (HildonWindowStack *stack,
377                                                  GList             *list)
378 {
379     HildonStackableWindow *win;
380     GList *l;
381     GList *pushed = NULL;
382
383     g_return_if_fail (HILDON_IS_WINDOW_STACK (stack));
384
385     /* Stack all windows */
386     for (l = list; l != NULL; l = g_list_next (l)) {
387         win = HILDON_STACKABLE_WINDOW (l->data);
388         if (win) {
389             _hildon_window_stack_do_push (stack, win);
390             pushed = g_list_prepend (pushed, win);
391         } else {
392             g_warning ("Trying to stack a non-stackable window!");
393         }
394     }
395
396     /* Show windows in reverse order (topmost first) */
397     g_list_foreach (pushed, (GFunc) gtk_widget_show, NULL);
398
399     g_list_free (pushed);
400 }
401
402 /**
403  * hildon_window_stack_push:
404  * @stack: A %HildonWindowStack
405  * @win1: The first window to push
406  * @Varargs: A %NULL-terminated list of additional #HildonStackableWindow<!-- -->s to push.
407  *
408  * Pushes all windows to the top of @stack, and shows them. Everything
409  * is done in a single transition, so the user will only see the last
410  * window. None of the windows must be already stacked.
411  *
412  * Since: 2.2
413  **/
414 void
415 hildon_window_stack_push                        (HildonWindowStack     *stack,
416                                                  HildonStackableWindow *win1,
417                                                  ...)
418 {
419     HildonStackableWindow *win = win1;
420     GList *list = NULL;
421     va_list args;
422
423     va_start (args, win1);
424
425     while (win != NULL) {
426         list = g_list_prepend (list, win);
427         win = va_arg (args, HildonStackableWindow *);
428     }
429
430     va_end (args);
431
432     list = g_list_reverse (list);
433
434     hildon_window_stack_push_list (stack, list);
435     g_list_free (list);
436 }
437
438 /**
439  * hildon_window_stack_pop:
440  * @stack: A %HildonWindowStack
441  * @nwindows: Number of windows to pop
442  * @popped_windows: if non-%NULL, the list of popped windows is stored here
443  *
444  * Pops @nwindows windows from @stack, and hides them. Everything is
445  * done in a single transition, so the user will not see any of the
446  * windows being popped in this operation.
447  *
448  * If @popped_windows is not %NULL, the list of popped windows is
449  * stored there (ordered bottom-up). That list must be freed by the
450  * user.
451  *
452  * Since: 2.2
453  **/
454 void
455 hildon_window_stack_pop                         (HildonWindowStack  *stack,
456                                                  gint                nwindows,
457                                                  GList             **popped_windows)
458 {
459     gint i;
460     GList *popped = NULL;
461
462     g_return_if_fail (HILDON_IS_WINDOW_STACK (stack));
463     g_return_if_fail (nwindows > 0);
464     g_return_if_fail (g_list_length (stack->priv->list) >= nwindows);
465
466     /* Pop windows */
467     for (i = 0; i < nwindows; i++) {
468         GtkWidget *win = _hildon_window_stack_do_pop (stack);
469         popped = g_list_prepend (popped, win);
470     }
471
472     /* Hide windows in reverse order (topmost last) */
473     g_list_foreach (popped, (GFunc) gtk_widget_hide, NULL);
474
475     if (popped_windows) {
476         *popped_windows = popped;
477     } else {
478         g_list_free (popped);
479     }
480 }
481
482 /**
483  * hildon_window_stack_pop_and_push_list:
484  * @stack: A %HildonWindowStack
485  * @nwindows: Number of windows to pop.
486  * @popped_windows: if non-%NULL, the list of popped windows is stored here
487  * @list: A list of %HildonStackableWindow<!-- -->s to push
488  *
489  * Pops @nwindows windows from @stack (and hides them), then pushes
490  * all windows in @list (and shows them). Everything is done in a
491  * single transition, so the user will only see the last window from
492  * @list. None of the pushed windows must be already stacked.
493  *
494  * If @popped_windows is not %NULL, the list of popped windows is
495  * stored there (ordered bottom-up). That list must be freed by the
496  * user.
497  *
498  * Since: 2.2
499  **/
500 void
501 hildon_window_stack_pop_and_push_list           (HildonWindowStack  *stack,
502                                                  gint                nwindows,
503                                                  GList             **popped_windows,
504                                                  GList              *list)
505 {
506     gint i;
507     GList *l;
508     GList *popped = NULL;
509     GList *pushed = NULL;
510
511     g_return_if_fail (HILDON_IS_WINDOW_STACK (stack));
512     g_return_if_fail (nwindows > 0);
513     g_return_if_fail (g_list_length (stack->priv->list) >= nwindows);
514
515     /* Pop windows */
516     for (i = 0; i < nwindows; i++) {
517         GtkWidget *win = _hildon_window_stack_do_pop (stack);
518         popped = g_list_prepend (popped, win);
519     }
520
521     /* Push windows */
522     for (l = list; l != NULL; l = g_list_next (l)) {
523         HildonStackableWindow *win = HILDON_STACKABLE_WINDOW (l->data);
524         if (win) {
525             _hildon_window_stack_do_push (stack, win);
526             pushed = g_list_prepend (pushed, win);
527         } else {
528             g_warning ("Trying to stack a non-stackable window!");
529         }
530     }
531
532     /* Show windows in reverse order (topmost first) */
533     g_list_foreach (pushed, (GFunc) gtk_widget_show, NULL);
534
535     /* Hide windows in reverse order (topmost last) */
536     g_list_foreach (popped, (GFunc) gtk_widget_hide, NULL);
537
538     g_list_free (pushed);
539     if (popped_windows) {
540         *popped_windows = popped;
541     } else {
542         g_list_free (popped);
543     }
544 }
545
546 /**
547  * hildon_window_stack_pop_and_push:
548  * @stack: A %HildonWindowStack
549  * @nwindows: Number of windows to pop.
550  * @popped_windows: if non-%NULL, the list of popped windows is stored here
551  * @win1: The first window to push
552  * @Varargs: A %NULL-terminated list of additional #HildonStackableWindow<!-- -->s to push.
553  *
554  * Pops @nwindows windows from @stack (and hides them), then pushes
555  * all passed windows (and shows them). Everything is done in a single
556  * transition, so the user will only see the last pushed window. None
557  * of the pushed windows must be already stacked.
558  *
559  * If @popped_windows is not %NULL, the list of popped windows is
560  * stored there (ordered bottom-up). That list must be freed by the
561  * user.
562  *
563  * Since: 2.2
564  **/
565 void
566 hildon_window_stack_pop_and_push                (HildonWindowStack      *stack,
567                                                  gint                    nwindows,
568                                                  GList                 **popped_windows,
569                                                  HildonStackableWindow  *win1,
570                                                  ...)
571 {
572     HildonStackableWindow *win = win1;
573     GList *list = NULL;
574     va_list args;
575
576     va_start (args, win1);
577
578     while (win != NULL) {
579         list = g_list_prepend (list, win);
580         win = va_arg (args, HildonStackableWindow *);
581     }
582
583     va_end (args);
584
585     list = g_list_reverse (list);
586
587     hildon_window_stack_pop_and_push_list (stack, nwindows, popped_windows, list);
588     g_list_free (list);
589 }
590
591 static void
592 hildon_window_stack_finalize (GObject *object)
593 {
594     HildonWindowStack *stack = HILDON_WINDOW_STACK (object);
595
596     if (stack->priv->list)
597         hildon_window_stack_pop (stack, hildon_window_stack_size (stack), NULL);
598
599     if (stack->priv->group)
600         g_object_unref (stack->priv->group);
601
602     /* Since the default group stack shouldn't be finalized,
603      * it's safe to destroy the X Window group we created. */
604     if (stack->priv->leader)
605         gdk_window_destroy (stack->priv->leader);
606
607     G_OBJECT_CLASS (hildon_window_stack_parent_class)->finalize (object);
608 }
609
610 static void
611 hildon_window_stack_set_property                (GObject      *object,
612                                                  guint         prop_id,
613                                                  const GValue *value,
614                                                  GParamSpec   *pspec)
615 {
616     HildonWindowStack *stack = HILDON_WINDOW_STACK (object);
617
618     switch (prop_id)
619     {
620     case PROP_GROUP:
621         hildon_window_stack_set_window_group (stack, g_value_get_object (value));
622         break;
623     default:
624         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
625         break;
626     }
627 }
628
629 static void
630 hildon_window_stack_get_property                (GObject    *object,
631                                                  guint       prop_id,
632                                                  GValue     *value,
633                                                  GParamSpec *pspec)
634 {
635     HildonWindowStack *stack = HILDON_WINDOW_STACK (object);
636
637     switch (prop_id)
638     {
639     case PROP_GROUP:
640         g_value_set_object (value, hildon_window_stack_get_window_group (stack));
641         break;
642     default:
643         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
644         break;
645     }
646 }
647
648 static void
649 hildon_window_stack_class_init (HildonWindowStackClass *klass)
650 {
651     GObjectClass *gobject_class = (GObjectClass *)klass;
652
653     gobject_class->set_property = hildon_window_stack_set_property;
654     gobject_class->get_property = hildon_window_stack_get_property;
655     gobject_class->finalize = hildon_window_stack_finalize;
656
657     g_object_class_install_property (
658         gobject_class,
659         PROP_GROUP,
660         g_param_spec_object (
661             "window-group",
662             "GtkWindowGroup for this stack",
663             "GtkWindowGroup that all windows on this stack belong to",
664             GTK_TYPE_WINDOW_GROUP,
665             G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
666
667     g_type_class_add_private (klass, sizeof (HildonWindowStackPrivate));
668 }
669
670 static void
671 hildon_window_stack_init (HildonWindowStack *self)
672 {
673     HildonWindowStackPrivate *priv;
674
675     priv = self->priv = HILDON_WINDOW_STACK_GET_PRIVATE (self);
676
677     priv->list = NULL;
678     priv->group = NULL;
679 }