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
240 hildon_window_stack_get_windows (HildonWindowStack *stack)
242 g_return_val_if_fail (HILDON_IS_WINDOW_STACK (stack), NULL);
244 return g_list_copy (stack->priv->list);
248 * hildon_window_stack_peek:
249 * @stack: A %HildonWindowStack
251 * Returns the window on top of @stack. The stack is never modified.
253 * Return value: the window on top of the stack, or %NULL if the stack
259 hildon_window_stack_peek (HildonWindowStack *stack)
261 GtkWidget *win = NULL;
263 g_return_val_if_fail (HILDON_IS_WINDOW_STACK (stack), NULL);
265 if (stack->priv->list != NULL) {
266 win = GTK_WIDGET (stack->priv->list->data);
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)
280 HildonWindowStack *current_stack;
282 g_return_val_if_fail (HILDON_IS_WINDOW_STACK (stack), FALSE);
283 g_return_val_if_fail (HILDON_IS_STACKABLE_WINDOW (win), FALSE);
285 current_stack = hildon_stackable_window_get_stack (win);
287 if (current_stack == NULL) {
288 GtkWidget *parent = hildon_window_stack_peek (stack);
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);
294 /* Make the window part of the same group as its parent */
296 gtk_window_set_transient_for (GTK_WINDOW (win), GTK_WINDOW (parent));
298 gtk_window_group_add_window (stack->priv->group, GTK_WINDOW (win));
301 /* Set win's group after it's been realized. */
302 g_signal_connect (win, "realize",
303 G_CALLBACK (hildon_window_stack_window_realized),
308 g_warning ("Trying to push a window that is already on a stack");
314 _hildon_window_stack_do_pop (HildonWindowStack *stack)
316 GtkWidget *win = hildon_window_stack_peek (stack);
319 hildon_window_stack_remove (HILDON_STACKABLE_WINDOW (win));
325 * hildon_window_stack_push_1:
326 * @stack: A %HildonWindowStack
327 * @win: A %HildonStackableWindow
329 * Adds @win to the top of @stack, and shows it. The window must not
330 * be already stacked.
335 hildon_window_stack_push_1 (HildonWindowStack *stack,
336 HildonStackableWindow *win)
338 if (_hildon_window_stack_do_push (stack, win))
339 gtk_widget_show (GTK_WIDGET (win));
343 * hildon_window_stack_pop_1:
344 * @stack: A %HildonWindowStack
346 * Removes the window on top of @stack, and hides it. If the stack is
347 * empty nothing happens.
349 * Return value: the window on top of the stack, or %NULL if the stack
355 hildon_window_stack_pop_1 (HildonWindowStack *stack)
357 GtkWidget *win = _hildon_window_stack_do_pop (stack);
359 gtk_widget_hide (win);
364 * hildon_window_stack_push_list:
365 * @stack: A %HildonWindowStack
366 * @list: A list of %HildonStackableWindow<!-- -->s to push
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.
376 hildon_window_stack_push_list (HildonWindowStack *stack,
379 HildonStackableWindow *win;
381 GList *pushed = NULL;
383 g_return_if_fail (HILDON_IS_WINDOW_STACK (stack));
385 /* Stack all windows */
386 for (l = list; l != NULL; l = g_list_next (l)) {
387 win = HILDON_STACKABLE_WINDOW (l->data);
389 _hildon_window_stack_do_push (stack, win);
390 pushed = g_list_prepend (pushed, win);
392 g_warning ("Trying to stack a non-stackable window!");
396 /* Show windows in reverse order (topmost first) */
397 g_list_foreach (pushed, (GFunc) gtk_widget_show, NULL);
399 g_list_free (pushed);
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.
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.
415 hildon_window_stack_push (HildonWindowStack *stack,
416 HildonStackableWindow *win1,
419 HildonStackableWindow *win = win1;
423 va_start (args, win1);
425 while (win != NULL) {
426 list = g_list_prepend (list, win);
427 win = va_arg (args, HildonStackableWindow *);
432 list = g_list_reverse (list);
434 hildon_window_stack_push_list (stack, list);
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
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.
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
455 hildon_window_stack_pop (HildonWindowStack *stack,
457 GList **popped_windows)
460 GList *popped = NULL;
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);
467 for (i = 0; i < nwindows; i++) {
468 GtkWidget *win = _hildon_window_stack_do_pop (stack);
469 popped = g_list_prepend (popped, win);
472 /* Hide windows in reverse order (topmost last) */
473 g_list_foreach (popped, (GFunc) gtk_widget_hide, NULL);
475 if (popped_windows) {
476 *popped_windows = popped;
478 g_list_free (popped);
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
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.
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
501 hildon_window_stack_pop_and_push_list (HildonWindowStack *stack,
503 GList **popped_windows,
508 GList *popped = NULL;
509 GList *pushed = NULL;
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);
516 for (i = 0; i < nwindows; i++) {
517 GtkWidget *win = _hildon_window_stack_do_pop (stack);
518 popped = g_list_prepend (popped, win);
522 for (l = list; l != NULL; l = g_list_next (l)) {
523 HildonStackableWindow *win = HILDON_STACKABLE_WINDOW (l->data);
525 _hildon_window_stack_do_push (stack, win);
526 pushed = g_list_prepend (pushed, win);
528 g_warning ("Trying to stack a non-stackable window!");
532 /* Show windows in reverse order (topmost first) */
533 g_list_foreach (pushed, (GFunc) gtk_widget_show, NULL);
535 /* Hide windows in reverse order (topmost last) */
536 g_list_foreach (popped, (GFunc) gtk_widget_hide, NULL);
538 g_list_free (pushed);
539 if (popped_windows) {
540 *popped_windows = popped;
542 g_list_free (popped);
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.
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.
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
566 hildon_window_stack_pop_and_push (HildonWindowStack *stack,
568 GList **popped_windows,
569 HildonStackableWindow *win1,
572 HildonStackableWindow *win = win1;
576 va_start (args, win1);
578 while (win != NULL) {
579 list = g_list_prepend (list, win);
580 win = va_arg (args, HildonStackableWindow *);
585 list = g_list_reverse (list);
587 hildon_window_stack_pop_and_push_list (stack, nwindows, popped_windows, list);
592 hildon_window_stack_finalize (GObject *object)
594 HildonWindowStack *stack = HILDON_WINDOW_STACK (object);
596 if (stack->priv->list)
597 hildon_window_stack_pop (stack, hildon_window_stack_size (stack), NULL);
599 if (stack->priv->group)
600 g_object_unref (stack->priv->group);
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);
607 G_OBJECT_CLASS (hildon_window_stack_parent_class)->finalize (object);
611 hildon_window_stack_set_property (GObject *object,
616 HildonWindowStack *stack = HILDON_WINDOW_STACK (object);
621 hildon_window_stack_set_window_group (stack, g_value_get_object (value));
624 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
630 hildon_window_stack_get_property (GObject *object,
635 HildonWindowStack *stack = HILDON_WINDOW_STACK (object);
640 g_value_set_object (value, hildon_window_stack_get_window_group (stack));
643 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
649 hildon_window_stack_class_init (HildonWindowStackClass *klass)
651 GObjectClass *gobject_class = (GObjectClass *)klass;
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;
657 g_object_class_install_property (
660 g_param_spec_object (
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));
667 g_type_class_add_private (klass, sizeof (HildonWindowStackPrivate));
671 hildon_window_stack_init (HildonWindowStack *self)
673 HildonWindowStackPrivate *priv;
675 priv = self->priv = HILDON_WINDOW_STACK_GET_PRIVATE (self);