Renamed src/ to hildon/
[hildon] / hildon / hildon-stackable-window.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2008 Nokia Corporation, all rights reserved.
5  *
6  * Contact: Rodrigo Novo <rodrigo.novo@nokia.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24
25 /**
26  * SECTION:hildon-stackable-window
27  * @short_description: Widget representing a stackable, top-level window in the Hildon framework.
28  * @see_also: #HildonWindowStack, #HildonProgram, #HildonWindow
29  *
30  * The #HildonStackableWindow is a GTK+ widget which represents a
31  * top-level window in the Hildon framework. It is derived from
32  * #HildonWindow. Applications that use stackable windows are organized
33  * in a hierarchical way so users can go from any window back to the
34  * application's root window.
35  *
36  * The user can only see and interact with the window on top of the
37  * stack. Although all other windows are mapped and visible, they are
38  * obscured by the topmost one so in practice they appear as if they
39  * were hidden.
40  *
41  * To add a window to the stack, just use gtk_widget_show(). The
42  * previous one will be obscured by the new one. When the new window
43  * is destroyed, the previous one will appear again.
44  *
45  * Alternatively, you can remove a window from the top of the stack
46  * without destroying it by using hildon_window_stack_pop(). The
47  * window will be automatically hidden and the previous one will
48  * appear.
49  *
50  * For advanced details on stack handling, see #HildonWindowStack
51  *
52  * <example>
53  * <title>Basic HildonStackableWindow example</title>
54  * <programlisting>
55  * static void
56  * show_new_window (void)
57  * {
58  *     GtkWidget *win;
59  * <!-- -->
60  *     win = hildon_stackable_window_new ();
61  * <!-- -->
62  *     // ... configure new window
63  * <!-- -->
64  *     gtk_widget_show (win);
65  * }
66  * <!-- -->
67  * int
68  * main (int argc, char **argv)
69  * {
70  *     GtkWidget *win;
71  *     GtkWidget *button;
72  * <!-- -->
73  *     gtk_init (&amp;argc, &amp;args);
74  * <!-- -->
75  *     win = hildon_stackable_window_new ();
76  *     gtk_window_set_title (GTK_WINDOW (win), "Main window);
77  * <!-- -->
78  *     // ... add some widgets to the window
79  * <!-- -->
80  *     g_signal_connect (button, "clicked", G_CALLBACK (show_new_window), NULL);
81  *     g_signal_connect (win, "destroy", G_CALLBACK (gtk_main_quit), NULL);
82  * <!-- -->
83  *     gtk_widget_show_all (win);
84  *     gtk_main ();
85  * <!-- -->
86  *     return 0;
87  * }
88  * </programlisting>
89  * </example>
90  */
91
92 #include                                        <X11/X.h>
93 #include                                        <X11/Xatom.h>
94 #include                                        <gdk/gdkx.h>
95
96 #include                                        "hildon-stackable-window.h"
97 #include                                        "hildon-stackable-window-private.h"
98 #include                                        "hildon-window-stack.h"
99 #include                                        "hildon-window-stack-private.h"
100
101 G_DEFINE_TYPE (HildonStackableWindow, hildon_stackable_window, HILDON_TYPE_WINDOW);
102
103 void G_GNUC_INTERNAL
104 hildon_stackable_window_set_stack               (HildonStackableWindow *self,
105                                                  HildonWindowStack     *stack,
106                                                  gint                   position)
107 {
108     HildonStackableWindowPrivate *priv = HILDON_STACKABLE_WINDOW_GET_PRIVATE (self);
109
110     if (stack)
111         g_object_ref (stack);
112
113     if (priv->stack)
114         g_object_unref (priv->stack);
115
116     priv->stack = stack;
117     priv->stack_position = position;
118 }
119
120 /**
121  * hildon_stackable_window_get_stack:
122  * @self: a #HildonStackableWindow
123  *
124  * Returns the stack where window @self is on, or %NULL if the window
125  * is not stacked.
126  *
127  * Return value: a #HildonWindowStack, or %NULL
128  *
129  * Since: 2.2
130  **/
131 HildonWindowStack *
132 hildon_stackable_window_get_stack               (HildonStackableWindow *self)
133 {
134     HildonStackableWindowPrivate *priv;
135
136     g_return_val_if_fail (HILDON_IS_STACKABLE_WINDOW (self), NULL);
137
138     priv = HILDON_STACKABLE_WINDOW_GET_PRIVATE (self);
139
140     return priv->stack;
141 }
142
143 /**
144  * hildon_stackable_window_set_main_menu:
145  * @self: a #HildonStackableWindow
146  * @menu: a #HildonAppMenu to be used for this window
147  *
148  * Deprecated: Hildon 2.2: use hildon_window_set_app_menu()
149  **/
150 void
151 hildon_stackable_window_set_main_menu           (HildonStackableWindow *self,
152                                                  HildonAppMenu *menu)
153 {
154     hildon_window_set_app_menu (HILDON_WINDOW (self), menu);
155 }
156
157 static void
158 hildon_stackable_window_map                     (GtkWidget *widget)
159 {
160     GdkDisplay *display;
161     Atom atom;
162     unsigned long val;
163     HildonStackableWindowPrivate *priv = HILDON_STACKABLE_WINDOW_GET_PRIVATE (widget);
164
165     val = priv->stack_position;
166
167     /* Set additional property "_HILDON_STACKABLE_WINDOW", to allow the WM to manage
168        it as a stackable window. */
169     display = gdk_drawable_get_display (widget->window);
170     atom = gdk_x11_get_xatom_by_name_for_display (display, "_HILDON_STACKABLE_WINDOW");
171     XChangeProperty (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XID (widget->window), atom,
172                      XA_INTEGER, 32, PropModeReplace,
173                      (unsigned char *) &val, 1);
174
175     GTK_WIDGET_CLASS (hildon_stackable_window_parent_class)->map (widget);
176 }
177
178 static void
179 hildon_stackable_window_show                    (GtkWidget *widget)
180 {
181     HildonStackableWindowPrivate *priv = HILDON_STACKABLE_WINDOW_GET_PRIVATE (widget);
182
183     /* Stack the window if not already stacked */
184     if (priv->stack == NULL) {
185         HildonWindowStack *stack = hildon_window_stack_get_default ();
186         _hildon_window_stack_do_push (stack, HILDON_STACKABLE_WINDOW (widget));
187     }
188
189     GTK_WIDGET_CLASS (hildon_stackable_window_parent_class)->show (widget);
190 }
191
192 static void
193 hildon_stackable_window_hide                    (GtkWidget *widget)
194 {
195     HildonStackableWindowPrivate *priv = HILDON_STACKABLE_WINDOW_GET_PRIVATE (widget);
196
197     if (priv->stack) {
198         hildon_window_stack_remove (HILDON_STACKABLE_WINDOW (widget));
199     }
200
201     GTK_WIDGET_CLASS (hildon_stackable_window_parent_class)->hide (widget);
202 }
203
204 static gboolean
205 hildon_stackable_window_delete_event            (GtkWidget   *widget,
206                                                  GdkEventAny *event)
207 {
208     HildonStackableWindowPrivate *priv = HILDON_STACKABLE_WINDOW_GET_PRIVATE (widget);
209     gboolean retvalue = FALSE;
210
211     if (priv->stack && hildon_window_stack_peek (priv->stack) != widget) {
212         /* Ignore the delete event if this window is not the topmost one */
213         retvalue = TRUE;
214     } else if (GTK_WIDGET_CLASS (hildon_stackable_window_parent_class)->delete_event) {
215         retvalue = GTK_WIDGET_CLASS (hildon_stackable_window_parent_class)->delete_event (widget, event);
216     }
217
218     return retvalue;
219 }
220
221 static void
222 hildon_stackable_window_class_init              (HildonStackableWindowClass *klass)
223 {
224     GtkWidgetClass    *widget_class = GTK_WIDGET_CLASS (klass);
225
226     widget_class->map               = hildon_stackable_window_map;
227     widget_class->show              = hildon_stackable_window_show;
228     widget_class->hide              = hildon_stackable_window_hide;
229     widget_class->delete_event      = hildon_stackable_window_delete_event;
230
231     g_type_class_add_private (klass, sizeof (HildonStackableWindowPrivate));
232 }
233
234 static void
235 hildon_stackable_window_init                    (HildonStackableWindow *self)
236 {
237     HildonStackableWindowPrivate *priv = HILDON_STACKABLE_WINDOW_GET_PRIVATE (self);
238
239     priv->stack = NULL;
240     priv->stack_position = -1;
241 }
242
243 /**
244  * hildon_stackable_window_new:
245  *
246  * Creates a new #HildonStackableWindow.
247  *
248  * Return value: A #HildonStackableWindow
249  *
250  * Since: 2.2
251  **/
252 GtkWidget*
253 hildon_stackable_window_new                     (void)
254 {
255     HildonStackableWindow *newwindow = g_object_new (HILDON_TYPE_STACKABLE_WINDOW, NULL);
256
257     return GTK_WIDGET (newwindow);
258 }