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_peek:
229 * @stack: A %HildonWindowStack
231 * Returns the window on top of @stack. The stack is never modified.
233 * Return value: the window on top of the stack, or %NULL if the stack
239 hildon_window_stack_peek (HildonWindowStack *stack)
241 GtkWidget *win = NULL;
243 g_return_val_if_fail (HILDON_IS_WINDOW_STACK (stack), NULL);
245 if (stack->priv->list != NULL) {
246 win = GTK_WIDGET (stack->priv->list->data);
252 /* This function does everything to push a window to the stack _but_
253 * actually calling gtk_widget_show().
254 * It's up to each specific push function to decide the order in which
255 * to show windows. */
256 gboolean G_GNUC_INTERNAL
257 _hildon_window_stack_do_push (HildonWindowStack *stack,
258 HildonStackableWindow *win)
260 HildonWindowStack *current_stack;
262 g_return_val_if_fail (HILDON_IS_WINDOW_STACK (stack), FALSE);
263 g_return_val_if_fail (HILDON_IS_STACKABLE_WINDOW (win), FALSE);
265 current_stack = hildon_stackable_window_get_stack (win);
267 if (current_stack == NULL) {
268 GtkWidget *parent = hildon_window_stack_peek (stack);
270 /* Push the window */
271 hildon_stackable_window_set_stack (win, stack, g_list_length (stack->priv->list));
272 stack->priv->list = g_list_prepend (stack->priv->list, win);
274 /* Make the window part of the same group as its parent */
276 gtk_window_set_transient_for (GTK_WINDOW (win), GTK_WINDOW (parent));
278 gtk_window_group_add_window (stack->priv->group, GTK_WINDOW (win));
281 /* Set win's group after it's been realized. */
282 g_signal_connect (win, "realize",
283 G_CALLBACK (hildon_window_stack_window_realized),
288 g_warning ("Trying to push a window that is already on a stack");
294 _hildon_window_stack_do_pop (HildonWindowStack *stack)
296 GtkWidget *win = hildon_window_stack_peek (stack);
299 hildon_window_stack_remove (HILDON_STACKABLE_WINDOW (win));
305 * hildon_window_stack_push_1:
306 * @stack: A %HildonWindowStack
307 * @win: A %HildonStackableWindow
309 * Adds @win to the top of @stack, and shows it. The window must not
310 * be already stacked.
315 hildon_window_stack_push_1 (HildonWindowStack *stack,
316 HildonStackableWindow *win)
318 if (_hildon_window_stack_do_push (stack, win))
319 gtk_widget_show (GTK_WIDGET (win));
323 * hildon_window_stack_pop_1:
324 * @stack: A %HildonWindowStack
326 * Removes the window on top of @stack, and hides it. If the stack is
327 * empty nothing happens.
329 * Return value: the window on top of the stack, or %NULL if the stack
335 hildon_window_stack_pop_1 (HildonWindowStack *stack)
337 GtkWidget *win = _hildon_window_stack_do_pop (stack);
339 gtk_widget_hide (win);
344 * hildon_window_stack_push_list:
345 * @stack: A %HildonWindowStack
346 * @list: A list of %HildonStackableWindow<!-- -->s to push
348 * Pushes all windows in @list to the top of @stack, and shows
349 * them. Everything is done in a single transition, so the user will
350 * only see the last window in @list during this operation. None of
351 * the windows must be already stacked.
356 hildon_window_stack_push_list (HildonWindowStack *stack,
359 HildonStackableWindow *win;
361 GList *pushed = NULL;
363 g_return_if_fail (HILDON_IS_WINDOW_STACK (stack));
365 /* Stack all windows */
366 for (l = list; l != NULL; l = g_list_next (l)) {
367 win = HILDON_STACKABLE_WINDOW (l->data);
369 _hildon_window_stack_do_push (stack, win);
370 pushed = g_list_prepend (pushed, win);
372 g_warning ("Trying to stack a non-stackable window!");
376 /* Show windows in reverse order (topmost first) */
377 g_list_foreach (pushed, (GFunc) gtk_widget_show, NULL);
379 g_list_free (pushed);
383 * hildon_window_stack_push:
384 * @stack: A %HildonWindowStack
385 * @win1: The first window to push
386 * @Varargs: A %NULL-terminated list of additional #HildonStackableWindow<!-- -->s to push.
388 * Pushes all windows to the top of @stack, and shows them. Everything
389 * is done in a single transition, so the user will only see the last
390 * window. None of the windows must be already stacked.
395 hildon_window_stack_push (HildonWindowStack *stack,
396 HildonStackableWindow *win1,
399 HildonStackableWindow *win = win1;
403 va_start (args, win1);
405 while (win != NULL) {
406 list = g_list_prepend (list, win);
407 win = va_arg (args, HildonStackableWindow *);
412 list = g_list_reverse (list);
414 hildon_window_stack_push_list (stack, list);
419 * hildon_window_stack_pop:
420 * @stack: A %HildonWindowStack
421 * @nwindows: Number of windows to pop
422 * @popped_windows: if non-%NULL, the list of popped windows is stored here
424 * Pops @nwindows windows from @stack, and hides them. Everything is
425 * done in a single transition, so the user will not see any of the
426 * windows being popped in this operation.
428 * If @popped_windows is not %NULL, the list of popped windows is
429 * stored there (ordered bottom-up). That list must be freed by the
435 hildon_window_stack_pop (HildonWindowStack *stack,
437 GList **popped_windows)
440 GList *popped = NULL;
442 g_return_if_fail (HILDON_IS_WINDOW_STACK (stack));
443 g_return_if_fail (nwindows > 0);
444 g_return_if_fail (g_list_length (stack->priv->list) >= nwindows);
447 for (i = 0; i < nwindows; i++) {
448 GtkWidget *win = _hildon_window_stack_do_pop (stack);
449 popped = g_list_prepend (popped, win);
452 /* Hide windows in reverse order (topmost last) */
453 g_list_foreach (popped, (GFunc) gtk_widget_hide, NULL);
455 if (popped_windows) {
456 *popped_windows = popped;
458 g_list_free (popped);
463 * hildon_window_stack_pop_and_push_list:
464 * @stack: A %HildonWindowStack
465 * @nwindows: Number of windows to pop.
466 * @popped_windows: if non-%NULL, the list of popped windows is stored here
467 * @list: A list of %HildonStackableWindow<!-- -->s to push
469 * Pops @nwindows windows from @stack (and hides them), then pushes
470 * all windows in @list (and shows them). Everything is done in a
471 * single transition, so the user will only see the last window from
472 * @list. None of the pushed windows must be already stacked.
474 * If @popped_windows is not %NULL, the list of popped windows is
475 * stored there (ordered bottom-up). That list must be freed by the
481 hildon_window_stack_pop_and_push_list (HildonWindowStack *stack,
483 GList **popped_windows,
488 GList *popped = NULL;
489 GList *pushed = NULL;
491 g_return_if_fail (HILDON_IS_WINDOW_STACK (stack));
492 g_return_if_fail (nwindows > 0);
493 g_return_if_fail (g_list_length (stack->priv->list) >= nwindows);
496 for (i = 0; i < nwindows; i++) {
497 GtkWidget *win = _hildon_window_stack_do_pop (stack);
498 popped = g_list_prepend (popped, win);
502 for (l = list; l != NULL; l = g_list_next (l)) {
503 HildonStackableWindow *win = HILDON_STACKABLE_WINDOW (l->data);
505 _hildon_window_stack_do_push (stack, win);
506 pushed = g_list_prepend (pushed, win);
508 g_warning ("Trying to stack a non-stackable window!");
512 /* Show windows in reverse order (topmost first) */
513 g_list_foreach (pushed, (GFunc) gtk_widget_show, NULL);
515 /* Hide windows in reverse order (topmost last) */
516 g_list_foreach (popped, (GFunc) gtk_widget_hide, NULL);
518 g_list_free (pushed);
519 if (popped_windows) {
520 *popped_windows = popped;
522 g_list_free (popped);
527 * hildon_window_stack_pop_and_push:
528 * @stack: A %HildonWindowStack
529 * @nwindows: Number of windows to pop.
530 * @popped_windows: if non-%NULL, the list of popped windows is stored here
531 * @win1: The first window to push
532 * @Varargs: A %NULL-terminated list of additional #HildonStackableWindow<!-- -->s to push.
534 * Pops @nwindows windows from @stack (and hides them), then pushes
535 * all passed windows (and shows them). Everything is done in a single
536 * transition, so the user will only see the last pushed window. None
537 * of the pushed windows must be already stacked.
539 * If @popped_windows is not %NULL, the list of popped windows is
540 * stored there (ordered bottom-up). That list must be freed by the
546 hildon_window_stack_pop_and_push (HildonWindowStack *stack,
548 GList **popped_windows,
549 HildonStackableWindow *win1,
552 HildonStackableWindow *win = win1;
556 va_start (args, win1);
558 while (win != NULL) {
559 list = g_list_prepend (list, win);
560 win = va_arg (args, HildonStackableWindow *);
565 list = g_list_reverse (list);
567 hildon_window_stack_pop_and_push_list (stack, nwindows, popped_windows, list);
572 hildon_window_stack_finalize (GObject *object)
574 HildonWindowStack *stack = HILDON_WINDOW_STACK (object);
576 if (stack->priv->list)
577 hildon_window_stack_pop (stack, hildon_window_stack_size (stack), NULL);
579 if (stack->priv->group)
580 g_object_unref (stack->priv->group);
582 /* Since the default group stack shouldn't be finalized,
583 * it's safe to destroy the X Window group we created. */
584 if (stack->priv->leader)
585 gdk_window_destroy (stack->priv->leader);
587 G_OBJECT_CLASS (hildon_window_stack_parent_class)->finalize (object);
591 hildon_window_stack_set_property (GObject *object,
596 HildonWindowStack *stack = HILDON_WINDOW_STACK (object);
601 hildon_window_stack_set_window_group (stack, g_value_get_object (value));
604 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
610 hildon_window_stack_get_property (GObject *object,
615 HildonWindowStack *stack = HILDON_WINDOW_STACK (object);
620 g_value_set_object (value, hildon_window_stack_get_window_group (stack));
623 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
629 hildon_window_stack_class_init (HildonWindowStackClass *klass)
631 GObjectClass *gobject_class = (GObjectClass *)klass;
633 gobject_class->set_property = hildon_window_stack_set_property;
634 gobject_class->get_property = hildon_window_stack_get_property;
635 gobject_class->finalize = hildon_window_stack_finalize;
637 g_object_class_install_property (
640 g_param_spec_object (
642 "GtkWindowGroup for this stack",
643 "GtkWindowGroup that all windows on this stack belong to",
644 GTK_TYPE_WINDOW_GROUP,
645 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
647 g_type_class_add_private (klass, sizeof (HildonWindowStackPrivate));
651 hildon_window_stack_init (HildonWindowStack *self)
653 HildonWindowStackPrivate *priv;
655 priv = self->priv = HILDON_WINDOW_STACK_GET_PRIVATE (self);