Modified webpage: now tinymail repository is in gitorious.
[modest] / src / gtk / modest-shell-window.c
1 /* Copyright (c) 2008, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <modest-platform.h>
31 #include "modest-marshal.h"
32 #include <modest-defs.h>
33 #include <modest-ui-dimming-rules.h>
34 #include <modest-ui-dimming-manager.h>
35 #include <modest-window-priv.h>
36 #include <modest-shell-window.h>
37 #include <modest-ui-actions.h>
38 #include "modest-text-utils.h"
39
40 /* 'private'/'protected' functions */
41 static void modest_shell_window_class_init  (gpointer klass, gpointer class_data);
42 static void modest_shell_window_instance_init (GTypeInstance *instance, gpointer g_class);
43 static void modest_shell_window_dispose     (GObject *obj);
44 static void modest_shell_window_finalize     (GObject *obj);
45
46 static gboolean on_zoom_minus_plus_not_implemented (ModestWindow *window);
47 static void modest_shell_window_show_progress (ModestWindow *window,
48                                                  gboolean show);
49 static void modest_shell_window_show_toolbar (ModestWindow *self,
50                                                  gboolean show_toolbar);
51 static void modest_shell_window_add_toolbar (ModestWindow *self,
52                                                GtkToolbar *toolbar);
53 static void modest_shell_window_add_to_menu (ModestWindow *window,
54                                                const gchar *label,
55                                                const gchar *accelerator,
56                                                ModestWindowMenuCallback callback,
57                                                ModestDimmingCallback dimming_callback);
58 static void modest_shell_window_add_item_to_menu (ModestWindow *window,
59                                                     GtkWidget *item,
60                                                     ModestDimmingCallback dimming_callback);
61 static void modest_shell_window_set_title (ModestWindow *self,
62                                              const gchar *title);
63
64 typedef struct _ModestShellWindowPrivate ModestShellWindowPrivate;
65 struct _ModestShellWindowPrivate {
66
67         GtkWidget *shell;
68         ModestDimmingRulesGroup *app_menu_dimming_group;
69         GtkAccelGroup *accel_group;
70
71         GtkWidget *menu;
72         gchar *title;
73
74 };
75 #define MODEST_SHELL_WINDOW_GET_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE((o), \
76                                                                             MODEST_TYPE_SHELL_WINDOW, \
77                                                                             ModestShellWindowPrivate))
78
79 /* globals */
80 static GtkWindowClass *parent_class = NULL;
81
82 /* uncomment the following if you have defined any signals */
83 /* static guint signals[LAST_SIGNAL] = {0}; */
84
85 /************************************************************************/
86
87 GType
88 modest_shell_window_get_type (void)
89 {
90         static GType my_type = 0;
91         if (!my_type) {
92                 static const GTypeInfo my_info = {
93                         sizeof(ModestShellWindowClass),
94                         NULL,           /* base init */
95                         NULL,           /* base finalize */
96                         (GClassInitFunc) modest_shell_window_class_init,
97                         NULL,           /* class finalize */
98                         NULL,           /* class data */
99                         sizeof(ModestShellWindow),
100                         1,              /* n_preallocs */
101                         (GInstanceInitFunc) modest_shell_window_instance_init,
102                         NULL
103                 };
104                 my_type = g_type_register_static (MODEST_TYPE_WINDOW,
105                                                   "ModestShellWindow",
106                                                   &my_info, 0);
107         }
108         return my_type;
109 }
110
111 static void
112 modest_shell_window_class_init (gpointer klass, gpointer class_data)
113 {
114         GObjectClass *gobject_class;
115         gobject_class = (GObjectClass*) klass;
116         ModestWindowClass *modest_window_class = (ModestWindowClass *) klass;
117
118         parent_class            = g_type_class_peek_parent (klass);
119         gobject_class->dispose  = modest_shell_window_dispose;
120         gobject_class->finalize  = modest_shell_window_finalize;
121
122         g_type_class_add_private (gobject_class, sizeof(ModestShellWindowPrivate));
123         
124         modest_window_class->zoom_minus_func = on_zoom_minus_plus_not_implemented;
125         modest_window_class->zoom_plus_func = on_zoom_minus_plus_not_implemented;
126         modest_window_class->show_toolbar_func = modest_shell_window_show_toolbar;
127         modest_window_class->add_toolbar_func = modest_shell_window_add_toolbar;
128         modest_window_class->add_to_menu_func = modest_shell_window_add_to_menu;
129         modest_window_class->add_item_to_menu_func = modest_shell_window_add_item_to_menu;
130         modest_window_class->set_title_func = modest_shell_window_set_title;
131         modest_window_class->show_progress_func = modest_shell_window_show_progress;
132
133 }
134
135 static void
136 modest_shell_window_dispose (GObject *obj)
137 {
138         ModestShellWindowPrivate *priv;
139
140         priv = MODEST_SHELL_WINDOW_GET_PRIVATE(obj);
141
142         if (priv->shell && priv->app_menu_dimming_group) {
143                 gtk_window_remove_accel_group (GTK_WINDOW (priv->shell),
144                                                priv->accel_group);
145         }
146
147         if (priv->accel_group) {
148                 g_object_unref (priv->accel_group);
149                 priv->accel_group = NULL;
150         }
151
152         if (priv->app_menu_dimming_group) {
153                 g_object_unref (priv->app_menu_dimming_group);
154                 priv->app_menu_dimming_group = NULL;
155         }
156
157         if (priv->menu) {
158                 gtk_widget_destroy (priv->menu);
159                 priv->menu = NULL;
160         }
161
162         G_OBJECT_CLASS(parent_class)->dispose (obj);
163 }
164
165 static void
166 modest_shell_window_finalize (GObject *obj)
167 {
168         ModestShellWindowPrivate *priv;
169
170         priv = MODEST_SHELL_WINDOW_GET_PRIVATE(obj);
171
172         if (priv->title)
173                 g_free (priv->title);
174
175         G_OBJECT_CLASS(parent_class)->finalize (obj);
176 }
177
178 static void
179 modest_shell_window_instance_init (GTypeInstance *instance, gpointer g_class)
180 {
181         ModestShellWindow *self = NULL; 
182         ModestWindowPrivate *parent_priv = NULL;
183         ModestShellWindowPrivate *priv = NULL;
184
185         self = (ModestShellWindow *) instance;
186         parent_priv = MODEST_WINDOW_GET_PRIVATE (self);
187         priv = MODEST_SHELL_WINDOW_GET_PRIVATE (self);
188
189         priv->title = NULL;
190
191         priv->accel_group = gtk_accel_group_new ();
192
193         priv->app_menu_dimming_group = modest_dimming_rules_group_new (MODEST_DIMMING_RULES_MENU, FALSE);
194         if (priv->shell)
195                 gtk_window_add_accel_group (GTK_WINDOW (priv->shell), priv->accel_group);
196
197         priv->menu = gtk_menu_new ();
198
199         modest_ui_dimming_manager_insert_rules_group (parent_priv->ui_dimming_manager, 
200                                                       priv->app_menu_dimming_group);
201
202         /* Dont't restore settings here, 
203          * because it requires a gtk_widget_show(), 
204          * and we don't want to do that until later,
205          * so that the UI is not visible for non-menu D-Bus activation.
206          */
207 }
208
209 static gboolean
210 on_zoom_minus_plus_not_implemented (ModestWindow *window)
211 {
212         g_return_val_if_fail (MODEST_IS_SHELL_WINDOW (window), FALSE);
213
214         modest_platform_information_banner (NULL, NULL, _CS_CANNOT_ZOOM_HERE);
215         return FALSE;
216 }
217
218 void
219 modest_shell_window_add_item_to_menu (ModestWindow *self,
220                                         GtkWidget *item,
221                                         ModestDimmingCallback dimming_callback)
222 {
223         ModestShellWindowPrivate *priv;
224
225         g_return_if_fail (MODEST_IS_SHELL_WINDOW(self));
226         g_return_if_fail (GTK_IS_MENU_ITEM (item));
227         priv = MODEST_SHELL_WINDOW_GET_PRIVATE (self);
228
229         modest_ui_dimming_manager_set_widget_dimming_mode (item,
230                                                            MODEST_UI_DIMMING_MODE_HIDE);
231
232         if (dimming_callback)
233                 modest_dimming_rules_group_add_widget_rule (priv->app_menu_dimming_group,
234                                                             item,
235                                                             (GCallback) dimming_callback,
236                                                             MODEST_WINDOW (self));
237         if (priv->menu) {
238                 gtk_menu_shell_append (GTK_MENU_SHELL (priv->menu), item);
239         } else {
240                 gtk_widget_destroy (item);
241         }
242
243         gtk_widget_show (item);
244 }
245
246 static void
247 modest_shell_window_add_to_menu (ModestWindow *self,
248                                    const gchar *label,
249                                    const gchar *accelerator,
250                                    ModestWindowMenuCallback callback,
251                                    ModestDimmingCallback dimming_callback)
252 {
253         ModestShellWindowPrivate *priv = NULL;
254         GtkWidget *menu_item;
255
256         g_return_if_fail (MODEST_IS_SHELL_WINDOW(self));
257         g_return_if_fail (label && label[0] != '\0');
258         g_return_if_fail (callback != NULL);
259
260         priv = MODEST_SHELL_WINDOW_GET_PRIVATE (self);
261
262         menu_item = gtk_menu_item_new_with_label (label);
263         g_signal_connect (G_OBJECT (menu_item), "activate",
264                           G_CALLBACK (callback), (gpointer) self);
265
266         if (accelerator != NULL) {
267                 guint accel_key;
268                 GdkModifierType accel_mods;
269
270                 gtk_accelerator_parse (accelerator, &accel_key, &accel_mods);
271                 gtk_widget_add_accelerator (menu_item, "activate", priv->accel_group,
272                                             accel_key, accel_mods, 0);
273         }
274
275         modest_window_add_item_to_menu (MODEST_WINDOW (self), menu_item, dimming_callback);
276 }
277
278 static void
279 modest_shell_window_show_toolbar (ModestWindow *self,
280                                     gboolean show_toolbar)
281 {
282         /* Empty implementation: Hildon 2.2 implementation
283          * doesn't switch toolbar visibility */
284 }
285
286 static void
287 modest_shell_window_add_toolbar (ModestWindow *self,
288                                  GtkToolbar *toolbar)
289 {
290         gtk_box_pack_end (GTK_BOX (self), GTK_WIDGET (toolbar), FALSE, FALSE, 0);
291 }
292
293 static void
294 modest_shell_window_set_title (ModestWindow *self,
295                                const gchar *title)
296 {
297         ModestShellWindowPrivate *priv = NULL;
298
299         priv = MODEST_SHELL_WINDOW_GET_PRIVATE (self);
300         if (priv->title)
301                 g_free (priv->title);
302         priv->title = g_strdup (title);
303         if (priv->shell) {
304                 modest_shell_set_title (MODEST_SHELL (priv->shell),
305                                         MODEST_WINDOW (self),
306                                         title);
307         }
308 }
309
310 static void
311 modest_shell_window_show_progress (ModestWindow *self,
312                                      gboolean show)
313 {
314         ModestShellWindowPrivate *priv = NULL;
315
316         priv = MODEST_SHELL_WINDOW_GET_PRIVATE (self);
317         modest_shell_show_progress (MODEST_SHELL (priv->shell),
318                                     self,
319                                     show);
320 }
321
322 void
323 modest_shell_window_set_shell (ModestShellWindow *self,
324                                ModestShell *shell)
325 {
326         ModestShellWindowPrivate *priv = NULL;
327
328         priv = MODEST_SHELL_WINDOW_GET_PRIVATE (self);
329
330         if (priv->shell) {
331                 if (priv->accel_group) {
332                         gtk_window_remove_accel_group (GTK_WINDOW (priv->shell), priv->accel_group);
333                 }
334                 modest_shell_delete_window (MODEST_SHELL (shell), MODEST_WINDOW (self));
335                 g_object_unref (priv->shell);
336         }
337
338         priv->shell = g_object_ref (shell);
339         modest_shell_set_title (MODEST_SHELL (priv->shell),
340                                 MODEST_WINDOW (self),
341                                 priv->title);
342         if (priv->accel_group)
343                 gtk_window_add_accel_group (GTK_WINDOW (priv->shell), priv->accel_group);
344 }
345
346 GtkWidget *
347 modest_shell_window_get_menu (ModestShellWindow *self)
348 {
349         ModestShellWindowPrivate *priv = NULL;
350
351         priv = MODEST_SHELL_WINDOW_GET_PRIVATE (self);
352
353         return priv->menu;
354 }