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);
74 * hildon_window_stack_get_default:
76 * Returns the default window stack. This stack always exists and
77 * doesn't need to be created by the application.
79 * Return value: the default #HildonWindowStack
84 hildon_window_stack_get_default (void)
86 static HildonWindowStack *stack = NULL;
87 if (G_UNLIKELY (stack == NULL)) {
88 stack = g_object_new (HILDON_TYPE_WINDOW_STACK, NULL);
89 stack->priv->group = gtk_window_get_group (NULL);
95 * hildon_window_stack_new:
97 * Creates a new #HildonWindowStack. The stack is initially empty.
99 * Return value: a new #HildonWindowStack
104 hildon_window_stack_new (void)
106 HildonWindowStack *stack = g_object_new (HILDON_TYPE_WINDOW_STACK, NULL);
107 stack->priv->group = gtk_window_group_new ();
112 * hildon_window_stack_size:
113 * @stack: A #HildonWindowStack
115 * Returns the number of windows in @stack
117 * Return value: Number of windows in @stack
122 hildon_window_stack_size (HildonWindowStack *stack)
124 g_return_val_if_fail (HILDON_IS_WINDOW_STACK (stack), 0);
126 return g_list_length (stack->priv->list);
130 hildon_window_stack_get_leader_window (HildonWindowStack *stack,
133 /* Create the X Window group (leader) if we haven't. */
134 if (!stack->priv->leader) {
135 if (stack == hildon_window_stack_get_default ()) {
138 /* We're the default stack, use the default group. */
139 dpy = gtk_widget_get_display (win);
140 stack->priv->leader = gdk_display_get_default_group (dpy);
142 static GdkWindowAttr attr = {
143 .window_type = GDK_WINDOW_TOPLEVEL,
144 .x = 10, .y = 10, .width = 10, .height = 10,
145 .wclass = GDK_INPUT_OUTPUT, .event_mask = 0,
149 /* Create a new X Window group. */
150 root = gtk_widget_get_root_window (win);
151 stack->priv->leader = gdk_window_new (root, &attr, GDK_WA_X | GDK_WA_Y);
155 return stack->priv->leader;
158 /* Set the X Window group of a window when it is realized. */
160 hildon_window_stack_window_realized (GtkWidget *win,
161 HildonWindowStack *stack)
163 GdkWindow *leader = hildon_window_stack_get_leader_window (stack, win);
164 gdk_window_set_group (win->window, leader);
167 /* Remove a window from its stack, no matter its position */
169 hildon_window_stack_remove (HildonStackableWindow *win)
171 HildonWindowStack *stack = hildon_stackable_window_get_stack (win);
173 /* If the window is stacked */
175 hildon_stackable_window_set_stack (win, NULL, -1);
176 stack->priv->list = g_list_remove (stack->priv->list, win);
177 gtk_window_set_transient_for (GTK_WINDOW (win), NULL);
178 if (GTK_WIDGET (win)->window) {
179 gdk_window_set_group (GTK_WIDGET (win)->window, NULL);
181 g_signal_handlers_disconnect_by_func (win, hildon_window_stack_window_realized, stack);
186 * hildon_window_stack_peek:
187 * @stack: A %HildonWindowStack
189 * Returns the window on top of @stack. The stack is never modified.
191 * Return value: the window on top of the stack, or %NULL if the stack
197 hildon_window_stack_peek (HildonWindowStack *stack)
199 GtkWidget *win = NULL;
201 g_return_val_if_fail (HILDON_IS_WINDOW_STACK (stack), NULL);
203 if (stack->priv->list != NULL) {
204 win = GTK_WIDGET (stack->priv->list->data);
211 _hildon_window_stack_do_push (HildonWindowStack *stack,
212 HildonStackableWindow *win)
214 HildonWindowStack *current_stack;
216 g_return_val_if_fail (HILDON_IS_WINDOW_STACK (stack), FALSE);
217 g_return_val_if_fail (HILDON_IS_STACKABLE_WINDOW (win), FALSE);
219 current_stack = hildon_stackable_window_get_stack (win);
221 if (current_stack == NULL) {
222 GtkWidget *parent = hildon_window_stack_peek (stack);
224 /* Push the window */
225 hildon_stackable_window_set_stack (win, stack, g_list_length (stack->priv->list));
226 stack->priv->list = g_list_prepend (stack->priv->list, win);
228 /* Make the window part of the same group as its parent */
230 gtk_window_set_transient_for (GTK_WINDOW (win), GTK_WINDOW (parent));
232 gtk_window_group_add_window (stack->priv->group, GTK_WINDOW (win));
235 /* Set win's group after it's been realized. */
236 g_signal_connect (win, "realize",
237 G_CALLBACK (hildon_window_stack_window_realized),
242 g_warning ("Trying to push a window that is already on a stack");
248 _hildon_window_stack_do_pop (HildonWindowStack *stack)
250 GtkWidget *win = hildon_window_stack_peek (stack);
253 hildon_window_stack_remove (HILDON_STACKABLE_WINDOW (win));
259 * hildon_window_stack_push_1:
260 * @stack: A %HildonWindowStack
261 * @win: A %HildonStackableWindow
263 * Adds @win to the top of @stack, and shows it. The window must not
264 * be already stacked.
269 hildon_window_stack_push_1 (HildonWindowStack *stack,
270 HildonStackableWindow *win)
272 if (_hildon_window_stack_do_push (stack, win))
273 gtk_widget_show (GTK_WIDGET (win));
277 * hildon_window_stack_pop_1:
278 * @stack: A %HildonWindowStack
280 * Removes the window on top of @stack, and hides it. If the stack is
281 * empty nothing happens.
283 * Return value: the window on top of the stack, or %NULL if the stack
289 hildon_window_stack_pop_1 (HildonWindowStack *stack)
291 GtkWidget *win = _hildon_window_stack_do_pop (stack);
293 gtk_widget_hide (win);
298 * hildon_window_stack_push_list:
299 * @stack: A %HildonWindowStack
300 * @list: A list of %HildonStackableWindow<!-- -->s to push
302 * Pushes all windows in @list to the top of @stack, and shows
303 * them. Everything is done in a single transition, so the user will
304 * only see the last window in @list during this operation. None of
305 * the windows must be already stacked.
310 hildon_window_stack_push_list (HildonWindowStack *stack,
313 HildonStackableWindow *win;
315 GList *pushed = NULL;
317 g_return_if_fail (HILDON_IS_WINDOW_STACK (stack));
319 /* Stack all windows */
320 for (l = list; l != NULL; l = g_list_next (l)) {
321 win = HILDON_STACKABLE_WINDOW (l->data);
323 _hildon_window_stack_do_push (stack, win);
324 pushed = g_list_prepend (pushed, win);
326 g_warning ("Trying to stack a non-stackable window!");
330 /* Show windows in reverse order (topmost first) */
331 g_list_foreach (pushed, (GFunc) gtk_widget_show, NULL);
333 g_list_free (pushed);
337 * hildon_window_stack_push:
338 * @stack: A %HildonWindowStack
339 * @win1: The first window to push
340 * @Varargs: A %NULL-terminated list of additional #HildonStackableWindow<!-- -->s to push.
342 * Pushes all windows to the top of @stack, and shows them. Everything
343 * is done in a single transition, so the user will only see the last
344 * window. None of the windows must be already stacked.
349 hildon_window_stack_push (HildonWindowStack *stack,
350 HildonStackableWindow *win1,
353 HildonStackableWindow *win;
357 va_start (args, win1);
358 win = va_arg (args, HildonStackableWindow *);
360 while (win != NULL) {
361 list = g_list_prepend (list, win);
362 win = va_arg (args, HildonStackableWindow *);
367 hildon_window_stack_push_list (stack, list);
372 * hildon_window_stack_pop:
373 * @stack: A %HildonWindowStack
374 * @nwindows: Number of windows to pop
375 * @popped_windows: if non-%NULL, the list of popped windows is stored here
377 * Pops @nwindows windows from @stack, and hides them. Everything is
378 * done in a single transition, so the user will not see any of the
379 * windows being popped in this operation.
381 * If @popped_windows is not %NULL, the list of popped windows is
382 * stored there (ordered bottom-up). That list must be freed by the
388 hildon_window_stack_pop (HildonWindowStack *stack,
390 GList **popped_windows)
393 GList *popped = NULL;
395 g_return_if_fail (HILDON_IS_WINDOW_STACK (stack));
396 g_return_if_fail (nwindows > 0);
397 g_return_if_fail (g_list_length (stack->priv->list) >= nwindows);
400 for (i = 0; i < nwindows; i++) {
401 GtkWidget *win = _hildon_window_stack_do_pop (stack);
402 popped = g_list_prepend (popped, win);
405 /* Hide windows in reverse order (topmost last) */
406 g_list_foreach (popped, (GFunc) gtk_widget_hide, NULL);
408 if (popped_windows) {
409 *popped_windows = popped;
411 g_list_free (popped);
416 * hildon_window_stack_pop_and_push_list:
417 * @stack: A %HildonWindowStack
418 * @nwindows: Number of windows to pop.
419 * @popped_windows: if non-%NULL, the list of popped windows is stored here
420 * @list: A list of %HildonStackableWindow<!-- -->s to push
422 * Pops @nwindows windows from @stack (and hides them), then pushes
423 * all windows in @list (and shows them). Everything is done in a
424 * single transition, so the user will only see the last window from
425 * @list. None of the pushed windows must be already stacked.
427 * If @popped_windows is not %NULL, the list of popped windows is
428 * stored there (ordered bottom-up). That list must be freed by the
434 hildon_window_stack_pop_and_push_list (HildonWindowStack *stack,
436 GList **popped_windows,
441 GList *popped = NULL;
442 GList *pushed = NULL;
444 g_return_if_fail (HILDON_IS_WINDOW_STACK (stack));
445 g_return_if_fail (nwindows > 0);
446 g_return_if_fail (g_list_length (stack->priv->list) >= nwindows);
449 for (i = 0; i < nwindows; i++) {
450 GtkWidget *win = _hildon_window_stack_do_pop (stack);
451 popped = g_list_prepend (popped, win);
455 for (l = list; l != NULL; l = g_list_next (l)) {
456 HildonStackableWindow *win = HILDON_STACKABLE_WINDOW (l->data);
458 _hildon_window_stack_do_push (stack, win);
459 pushed = g_list_prepend (pushed, win);
461 g_warning ("Trying to stack a non-stackable window!");
465 /* Show windows in reverse order (topmost first) */
466 g_list_foreach (pushed, (GFunc) gtk_widget_show, NULL);
468 /* Hide windows in reverse order (topmost last) */
469 g_list_foreach (popped, (GFunc) gtk_widget_hide, NULL);
471 g_list_free (pushed);
472 if (popped_windows) {
473 *popped_windows = popped;
475 g_list_free (popped);
480 * hildon_window_stack_pop_and_push:
481 * @stack: A %HildonWindowStack
482 * @nwindows: Number of windows to pop.
483 * @popped_windows: if non-%NULL, the list of popped windows is stored here
484 * @win1: The first window to push
485 * @Varargs: A %NULL-terminated list of additional #HildonStackableWindow<!-- -->s to push.
487 * Pops @nwindows windows from @stack (and hides them), then pushes
488 * all passed windows (and shows them). Everything is done in a single
489 * transition, so the user will only see the last pushed window. None
490 * 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 (HildonWindowStack *stack,
501 GList **popped_windows,
502 HildonStackableWindow *win1,
505 HildonStackableWindow *win;
509 va_start (args, win1);
510 win = va_arg (args, HildonStackableWindow *);
512 while (win != NULL) {
513 list = g_list_prepend (list, win);
514 win = va_arg (args, HildonStackableWindow *);
519 hildon_window_stack_pop_and_push_list (stack, nwindows, popped_windows, list);
524 hildon_window_stack_finalize (GObject *object)
526 HildonWindowStack *stack = HILDON_WINDOW_STACK (object);
528 if (stack->priv->list)
529 hildon_window_stack_pop (stack, hildon_window_stack_size (stack), NULL);
531 if (stack->priv->group)
532 g_object_unref (stack->priv->group);
534 /* Since the default group stack shouldn't be finalized,
535 * it's safe to destroy the X Window group we created. */
536 if (stack->priv->leader)
537 gdk_window_destroy (stack->priv->leader);
539 G_OBJECT_CLASS (hildon_window_stack_parent_class)->finalize (object);
543 hildon_window_stack_class_init (HildonWindowStackClass *klass)
545 GObjectClass *gobject_class = (GObjectClass *)klass;
547 gobject_class->finalize = hildon_window_stack_finalize;
549 g_type_class_add_private (klass, sizeof (HildonWindowStackPrivate));
553 hildon_window_stack_init (HildonWindowStack *self)
555 HildonWindowStackPrivate *priv;
557 priv = self->priv = HILDON_WINDOW_STACK_GET_PRIVATE (self);