2 * This file is a part of hildon
4 * Copyright (C) 2008 Nokia Corporation, all rights reserved.
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.
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.
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
24 * SECTION:hildon-window-stack
25 * @short_description: Object representing a stack of windows in the Hildon framework
26 * @see_also: #HildonStackableWindow
28 * The #HildonWindowStack is an object used to represent a stack of
29 * windows in the Hildon framework.
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.
36 * Each window can only be in one stack at a time. All stacked windows
37 * are visible and all visible windows are stacked.
39 * Each application has a default stack, and windows are automatically
40 * added to it when they are shown with gtk_widget_show().
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).
47 * To remove a window from a stack use hildon_window_stack_pop_1(), or
48 * simply gtk_widget_hide().
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.
56 #include "hildon-window-stack.h"
57 #include "hildon-window-stack-private.h"
58 #include "hildon-stackable-window-private.h"
60 struct _HildonWindowStackPrivate
63 GtkWindowGroup *group;
64 GdkWindow *leader; /* X Window group hint for all windows in a group */
67 #define HILDON_WINDOW_STACK_GET_PRIVATE(obj) \
68 (G_TYPE_INSTANCE_GET_PRIVATE ((obj),\
69 HILDON_TYPE_WINDOW_STACK, HildonWindowStackPrivate))
71 G_DEFINE_TYPE (HildonWindowStack, hildon_window_stack, G_TYPE_OBJECT);
78 hildon_window_stack_set_window_group (HildonWindowStack *stack,
79 GtkWindowGroup *group)
81 g_return_if_fail (HILDON_IS_WINDOW_STACK (stack));
82 g_return_if_fail (!group || GTK_IS_WINDOW_GROUP (group));
84 /* The window group is only to be set once during construction */
85 g_return_if_fail (stack->priv->group == NULL);
88 group = gtk_window_group_new ();
90 stack->priv->group = group;
93 static GtkWindowGroup *
94 hildon_window_stack_get_window_group (HildonWindowStack *stack)
96 g_return_val_if_fail (HILDON_IS_WINDOW_STACK (stack), NULL);
98 return stack->priv->group;
102 * hildon_window_stack_get_default:
104 * Returns the default window stack. This stack always exists and
105 * doesn't need to be created by the application.
107 * Return value: the default #HildonWindowStack
112 hildon_window_stack_get_default (void)
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),
124 * hildon_window_stack_new:
126 * Creates a new #HildonWindowStack. The stack is initially empty.
128 * Return value: a new #HildonWindowStack
133 hildon_window_stack_new (void)
135 HildonWindowStack *stack = g_object_new (HILDON_TYPE_WINDOW_STACK, NULL);
140 * hildon_window_stack_size:
141 * @stack: A #HildonWindowStack
143 * Returns the number of windows in @stack
145 * Return value: Number of windows in @stack
150 hildon_window_stack_size (HildonWindowStack *stack)
152 g_return_val_if_fail (HILDON_IS_WINDOW_STACK (stack), 0);
154 return g_list_length (stack->priv->list);
158 hildon_window_stack_get_leader_window (HildonWindowStack *stack,
161 /* Create the X Window group (leader) if we haven't. */
162 if (!stack->priv->leader) {
163 if (stack == hildon_window_stack_get_default ()) {
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);
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,
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);
183 return stack->priv->leader;
186 /* Set the X Window group of a window when it is realized. */
188 hildon_window_stack_window_realized (GtkWidget *win,
189 HildonWindowStack *stack)
191 GdkWindow *leader = hildon_window_stack_get_leader_window (stack, win);
192 gdk_window_set_group (win->window, leader);
195 /* Remove a window from its stack, no matter its position */
197 hildon_window_stack_remove (HildonStackableWindow *win)
199 HildonWindowStack *stack = hildon_stackable_window_get_stack (win);
201 /* If the window is stacked */
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);
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);
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);
221 stack->priv->list = g_list_remove (stack->priv->list, win);
223 g_signal_handlers_disconnect_by_func (win, hildon_window_stack_window_realized, stack);
228 * hildon_window_stack_get_windows:
229 * @stack: a #HildonWindowStack
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().
235 * Returns: a newly-allocated list of #HildonStackableWindow<!-- -->s
238 hildon_window_stack_get_windows (HildonWindowStack *stack)
240 g_return_val_if_fail (HILDON_IS_WINDOW_STACK (stack), NULL);
242 return g_list_copy (stack->priv->list);
246 * hildon_window_stack_peek:
247 * @stack: A %HildonWindowStack
249 * Returns the window on top of @stack. The stack is never modified.
251 * Return value: the window on top of the stack, or %NULL if the stack
257 hildon_window_stack_peek (HildonWindowStack *stack)
259 GtkWidget *win = NULL;
261 g_return_val_if_fail (HILDON_IS_WINDOW_STACK (stack), NULL);
263 if (stack->priv->list != NULL) {
264 win = GTK_WIDGET (stack->priv->list->data);
270 /* This function does everything to push a window to the stack _but_
271 * actually calling gtk_widget_show().
272 * It's up to each specific push function to decide the order in which
273 * to show windows. */
274 gboolean G_GNUC_INTERNAL
275 _hildon_window_stack_do_push (HildonWindowStack *stack,
276 HildonStackableWindow *win)
278 HildonWindowStack *current_stack;
280 g_return_val_if_fail (HILDON_IS_WINDOW_STACK (stack), FALSE);
281 g_return_val_if_fail (HILDON_IS_STACKABLE_WINDOW (win), FALSE);
283 current_stack = hildon_stackable_window_get_stack (win);
285 if (current_stack == NULL) {
286 GtkWidget *parent = hildon_window_stack_peek (stack);
288 /* Push the window */
289 hildon_stackable_window_set_stack (win, stack, g_list_length (stack->priv->list));
290 stack->priv->list = g_list_prepend (stack->priv->list, win);
292 /* Make the window part of the same group as its parent */
294 gtk_window_set_transient_for (GTK_WINDOW (win), GTK_WINDOW (parent));
296 gtk_window_group_add_window (stack->priv->group, GTK_WINDOW (win));
299 /* Set win's group after it's been realized. */
300 g_signal_connect (win, "realize",
301 G_CALLBACK (hildon_window_stack_window_realized),
306 g_warning ("Trying to push a window that is already on a stack");
312 _hildon_window_stack_do_pop (HildonWindowStack *stack)
314 GtkWidget *win = hildon_window_stack_peek (stack);
317 hildon_window_stack_remove (HILDON_STACKABLE_WINDOW (win));
323 * hildon_window_stack_push_1:
324 * @stack: A %HildonWindowStack
325 * @win: A %HildonStackableWindow
327 * Adds @win to the top of @stack, and shows it. The window must not
328 * be already stacked.
333 hildon_window_stack_push_1 (HildonWindowStack *stack,
334 HildonStackableWindow *win)
336 if (_hildon_window_stack_do_push (stack, win))
337 gtk_widget_show (GTK_WIDGET (win));
341 * hildon_window_stack_pop_1:
342 * @stack: A %HildonWindowStack
344 * Removes the window on top of @stack, and hides it. If the stack is
345 * empty nothing happens.
347 * Return value: the window on top of the stack, or %NULL if the stack
353 hildon_window_stack_pop_1 (HildonWindowStack *stack)
355 GtkWidget *win = _hildon_window_stack_do_pop (stack);
357 gtk_widget_hide (win);
362 * hildon_window_stack_push_list:
363 * @stack: A %HildonWindowStack
364 * @list: A list of %HildonStackableWindow<!-- -->s to push
366 * Pushes all windows in @list to the top of @stack, and shows
367 * them. Everything is done in a single transition, so the user will
368 * only see the last window in @list during this operation. None of
369 * the windows must be already stacked.
374 hildon_window_stack_push_list (HildonWindowStack *stack,
377 HildonStackableWindow *win;
379 GList *pushed = NULL;
381 g_return_if_fail (HILDON_IS_WINDOW_STACK (stack));
383 /* Stack all windows */
384 for (l = list; l != NULL; l = g_list_next (l)) {
385 win = HILDON_STACKABLE_WINDOW (l->data);
387 _hildon_window_stack_do_push (stack, win);
388 pushed = g_list_prepend (pushed, win);
390 g_warning ("Trying to stack a non-stackable window!");
394 /* Show windows in reverse order (topmost first) */
395 g_list_foreach (pushed, (GFunc) gtk_widget_show, NULL);
397 g_list_free (pushed);
401 * hildon_window_stack_push:
402 * @stack: A %HildonWindowStack
403 * @win1: The first window to push
404 * @Varargs: A %NULL-terminated list of additional #HildonStackableWindow<!-- -->s to push.
406 * Pushes all windows to the top of @stack, and shows them. Everything
407 * is done in a single transition, so the user will only see the last
408 * window. None of the windows must be already stacked.
413 hildon_window_stack_push (HildonWindowStack *stack,
414 HildonStackableWindow *win1,
417 HildonStackableWindow *win = win1;
421 va_start (args, win1);
423 while (win != NULL) {
424 list = g_list_prepend (list, win);
425 win = va_arg (args, HildonStackableWindow *);
430 list = g_list_reverse (list);
432 hildon_window_stack_push_list (stack, list);
437 * hildon_window_stack_pop:
438 * @stack: A %HildonWindowStack
439 * @nwindows: Number of windows to pop
440 * @popped_windows: if non-%NULL, the list of popped windows is stored here
442 * Pops @nwindows windows from @stack, and hides them. Everything is
443 * done in a single transition, so the user will not see any of the
444 * windows being popped in this operation.
446 * If @popped_windows is not %NULL, the list of popped windows is
447 * stored there (ordered bottom-up). That list must be freed by the
453 hildon_window_stack_pop (HildonWindowStack *stack,
455 GList **popped_windows)
458 GList *popped = NULL;
460 g_return_if_fail (HILDON_IS_WINDOW_STACK (stack));
461 g_return_if_fail (nwindows > 0);
462 g_return_if_fail (g_list_length (stack->priv->list) >= nwindows);
465 for (i = 0; i < nwindows; i++) {
466 GtkWidget *win = _hildon_window_stack_do_pop (stack);
467 popped = g_list_prepend (popped, win);
470 /* Hide windows in reverse order (topmost last) */
471 g_list_foreach (popped, (GFunc) gtk_widget_hide, NULL);
473 if (popped_windows) {
474 *popped_windows = popped;
476 g_list_free (popped);
481 * hildon_window_stack_pop_and_push_list:
482 * @stack: A %HildonWindowStack
483 * @nwindows: Number of windows to pop.
484 * @popped_windows: if non-%NULL, the list of popped windows is stored here
485 * @list: A list of %HildonStackableWindow<!-- -->s to push
487 * Pops @nwindows windows from @stack (and hides them), then pushes
488 * all windows in @list (and shows them). Everything is done in a
489 * single transition, so the user will only see the last window from
490 * @list. None of the pushed windows must be already stacked.
492 * If @popped_windows is not %NULL, the list of popped windows is
493 * stored there (ordered bottom-up). That list must be freed by the
499 hildon_window_stack_pop_and_push_list (HildonWindowStack *stack,
501 GList **popped_windows,
506 GList *popped = NULL;
507 GList *pushed = NULL;
509 g_return_if_fail (HILDON_IS_WINDOW_STACK (stack));
510 g_return_if_fail (nwindows > 0);
511 g_return_if_fail (g_list_length (stack->priv->list) >= nwindows);
514 for (i = 0; i < nwindows; i++) {
515 GtkWidget *win = _hildon_window_stack_do_pop (stack);
516 popped = g_list_prepend (popped, win);
520 for (l = list; l != NULL; l = g_list_next (l)) {
521 HildonStackableWindow *win = HILDON_STACKABLE_WINDOW (l->data);
523 _hildon_window_stack_do_push (stack, win);
524 pushed = g_list_prepend (pushed, win);
526 g_warning ("Trying to stack a non-stackable window!");
530 /* Show windows in reverse order (topmost first) */
531 g_list_foreach (pushed, (GFunc) gtk_widget_show, NULL);
533 /* Hide windows in reverse order (topmost last) */
534 g_list_foreach (popped, (GFunc) gtk_widget_hide, NULL);
536 g_list_free (pushed);
537 if (popped_windows) {
538 *popped_windows = popped;
540 g_list_free (popped);
545 * hildon_window_stack_pop_and_push:
546 * @stack: A %HildonWindowStack
547 * @nwindows: Number of windows to pop.
548 * @popped_windows: if non-%NULL, the list of popped windows is stored here
549 * @win1: The first window to push
550 * @Varargs: A %NULL-terminated list of additional #HildonStackableWindow<!-- -->s to push.
552 * Pops @nwindows windows from @stack (and hides them), then pushes
553 * all passed windows (and shows them). Everything is done in a single
554 * transition, so the user will only see the last pushed window. None
555 * of the pushed windows must be already stacked.
557 * If @popped_windows is not %NULL, the list of popped windows is
558 * stored there (ordered bottom-up). That list must be freed by the
564 hildon_window_stack_pop_and_push (HildonWindowStack *stack,
566 GList **popped_windows,
567 HildonStackableWindow *win1,
570 HildonStackableWindow *win = win1;
574 va_start (args, win1);
576 while (win != NULL) {
577 list = g_list_prepend (list, win);
578 win = va_arg (args, HildonStackableWindow *);
583 list = g_list_reverse (list);
585 hildon_window_stack_pop_and_push_list (stack, nwindows, popped_windows, list);
590 hildon_window_stack_finalize (GObject *object)
592 HildonWindowStack *stack = HILDON_WINDOW_STACK (object);
594 if (stack->priv->list)
595 hildon_window_stack_pop (stack, hildon_window_stack_size (stack), NULL);
597 if (stack->priv->group)
598 g_object_unref (stack->priv->group);
600 /* Since the default group stack shouldn't be finalized,
601 * it's safe to destroy the X Window group we created. */
602 if (stack->priv->leader)
603 gdk_window_destroy (stack->priv->leader);
605 G_OBJECT_CLASS (hildon_window_stack_parent_class)->finalize (object);
609 hildon_window_stack_set_property (GObject *object,
614 HildonWindowStack *stack = HILDON_WINDOW_STACK (object);
619 hildon_window_stack_set_window_group (stack, g_value_get_object (value));
622 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
628 hildon_window_stack_get_property (GObject *object,
633 HildonWindowStack *stack = HILDON_WINDOW_STACK (object);
638 g_value_set_object (value, hildon_window_stack_get_window_group (stack));
641 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
647 hildon_window_stack_class_init (HildonWindowStackClass *klass)
649 GObjectClass *gobject_class = (GObjectClass *)klass;
651 gobject_class->set_property = hildon_window_stack_set_property;
652 gobject_class->get_property = hildon_window_stack_get_property;
653 gobject_class->finalize = hildon_window_stack_finalize;
655 g_object_class_install_property (
658 g_param_spec_object (
660 "GtkWindowGroup for this stack",
661 "GtkWindowGroup that all windows on this stack belong to",
662 GTK_TYPE_WINDOW_GROUP,
663 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
665 g_type_class_add_private (klass, sizeof (HildonWindowStackPrivate));
669 hildon_window_stack_init (HildonWindowStack *self)
671 HildonWindowStackPrivate *priv;
673 priv = self->priv = HILDON_WINDOW_STACK_GET_PRIVATE (self);