Fix adding accelerators from menu items in shell windows
[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 void 
218 modest_shell_window_add_item_to_menu (ModestWindow *self,
219                                         GtkWidget *button,
220                                         ModestDimmingCallback dimming_callback)
221 {
222         ModestShellWindowPrivate *priv;
223
224         g_return_if_fail (MODEST_IS_SHELL_WINDOW(self));
225         priv = MODEST_SHELL_WINDOW_GET_PRIVATE (self);
226
227         modest_ui_dimming_manager_set_widget_dimming_mode (GTK_WIDGET (button),
228                                                            MODEST_UI_DIMMING_MODE_HIDE);
229
230         if (dimming_callback)
231                 modest_dimming_rules_group_add_widget_rule (priv->app_menu_dimming_group,
232                                                             GTK_WIDGET (button),
233                                                             (GCallback) dimming_callback,
234                                                             MODEST_WINDOW (self));
235         if (priv->menu) {
236                 gtk_menu_shell_append (GTK_MENU_SHELL (priv->menu), button);
237         } else {
238                 gtk_widget_destroy (button);
239         }
240
241         gtk_widget_show (GTK_WIDGET (button));
242 }
243
244 static void
245 modest_shell_window_add_to_menu (ModestWindow *self,
246                                    const gchar *label,
247                                    const gchar *accelerator,
248                                    ModestWindowMenuCallback callback,
249                                    ModestDimmingCallback dimming_callback)
250 {
251         ModestShellWindowPrivate *priv = NULL;
252         GtkWidget *menu_item;
253
254         g_return_if_fail (MODEST_IS_SHELL_WINDOW(self));
255         g_return_if_fail (label && label[0] != '\0');
256         g_return_if_fail (callback != NULL);
257
258         priv = MODEST_SHELL_WINDOW_GET_PRIVATE (self);
259
260         menu_item = gtk_menu_item_new_with_label (label);
261         g_signal_connect (G_OBJECT (menu_item), "activate",
262                           G_CALLBACK (callback), (gpointer) self);
263
264         if (accelerator != NULL) {
265                 guint accel_key;
266                 GdkModifierType accel_mods;
267
268                 gtk_accelerator_parse (accelerator, &accel_key, &accel_mods);
269                 gtk_widget_add_accelerator (menu_item, "activate", priv->accel_group,
270                                             accel_key, accel_mods, 0);
271         }
272
273         if (priv->menu) {
274                 gtk_menu_shell_append (GTK_MENU_SHELL (priv->menu), menu_item);
275         } else {
276                 gtk_widget_destroy (menu_item);
277         }
278         gtk_widget_show (menu_item);
279 }
280
281 static void
282 modest_shell_window_show_toolbar (ModestWindow *self,
283                                     gboolean show_toolbar)
284 {
285         /* Empty implementation: Hildon 2.2 implementation
286          * doesn't switch toolbar visibility */
287 }
288
289 static void
290 modest_shell_window_add_toolbar (ModestWindow *self,
291                                  GtkToolbar *toolbar)
292 {
293         gtk_box_pack_end (GTK_BOX (self), GTK_WIDGET (toolbar), FALSE, FALSE, 0);
294 }
295
296 static void
297 modest_shell_window_set_title (ModestWindow *self,
298                                const gchar *title)
299 {
300         ModestShellWindowPrivate *priv = NULL;
301
302         priv = MODEST_SHELL_WINDOW_GET_PRIVATE (self);
303         if (priv->title)
304                 g_free (priv->title);
305         priv->title = g_strdup (title);
306         if (priv->shell) {
307                 modest_shell_set_title (MODEST_SHELL (priv->shell),
308                                         MODEST_WINDOW (self),
309                                         title);
310         }
311 }
312
313 static void
314 modest_shell_window_show_progress (ModestWindow *self,
315                                      gboolean show)
316 {
317         ModestShellWindowPrivate *priv = NULL;
318
319         priv = MODEST_SHELL_WINDOW_GET_PRIVATE (self);
320         modest_shell_show_progress (MODEST_SHELL (priv->shell),
321                                     self,
322                                     show);
323 }
324
325 void
326 modest_shell_window_set_shell (ModestShellWindow *self,
327                                ModestShell *shell)
328 {
329         ModestShellWindowPrivate *priv = NULL;
330
331         priv = MODEST_SHELL_WINDOW_GET_PRIVATE (self);
332
333         if (priv->shell) {
334                 if (priv->accel_group) {
335                         gtk_window_remove_accel_group (GTK_WINDOW (priv->shell), priv->accel_group);
336                 }
337                 modest_shell_delete_window (MODEST_SHELL (shell), MODEST_WINDOW (self));
338                 g_object_unref (priv->shell);
339         }
340
341         priv->shell = g_object_ref (shell);
342         modest_shell_set_title (MODEST_SHELL (priv->shell),
343                                 MODEST_WINDOW (self),
344                                 priv->title);
345         if (priv->accel_group)
346                 gtk_window_add_accel_group (GTK_WINDOW (priv->shell), priv->accel_group);
347 }
348
349 GtkWidget *
350 modest_shell_window_get_menu (ModestShellWindow *self)
351 {
352         ModestShellWindowPrivate *priv = NULL;
353
354         priv = MODEST_SHELL_WINDOW_GET_PRIVATE (self);
355
356         return priv->menu;
357 }