Make windows inherit from ModestShellWindow in gtk
[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
45 static gboolean on_zoom_minus_plus_not_implemented (ModestWindow *window);
46 static void modest_shell_window_show_progress (ModestWindow *window,
47                                                  gboolean show);
48 static void modest_shell_window_show_toolbar (ModestWindow *self,
49                                                  gboolean show_toolbar);
50 static void modest_shell_window_add_toolbar (ModestWindow *self,
51                                                GtkToolbar *toolbar);
52 static void modest_shell_window_add_to_menu (ModestWindow *window,
53                                                const gchar *label,
54                                                const gchar *accelerator,
55                                                ModestWindowMenuCallback callback,
56                                                ModestDimmingCallback dimming_callback);
57 static void modest_shell_window_add_item_to_menu (ModestWindow *window,
58                                                     GtkWidget *item,
59                                                     ModestDimmingCallback dimming_callback);
60 static void modest_shell_window_set_title (ModestWindow *self,
61                                              const gchar *title);
62
63 typedef struct _ModestShellWindowPrivate ModestShellWindowPrivate;
64 struct _ModestShellWindowPrivate {
65
66         GtkWidget *shell;
67         ModestDimmingRulesGroup *app_menu_dimming_group;
68         GtkAccelGroup *accel_group;
69
70         GtkWidget *menu;
71
72 };
73 #define MODEST_SHELL_WINDOW_GET_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE((o), \
74                                                                             MODEST_TYPE_SHELL_WINDOW, \
75                                                                             ModestShellWindowPrivate))
76
77 /* globals */
78 static GtkWindowClass *parent_class = NULL;
79
80 /* uncomment the following if you have defined any signals */
81 /* static guint signals[LAST_SIGNAL] = {0}; */
82
83 /************************************************************************/
84
85 GType
86 modest_shell_window_get_type (void)
87 {
88         static GType my_type = 0;
89         if (!my_type) {
90                 static const GTypeInfo my_info = {
91                         sizeof(ModestShellWindowClass),
92                         NULL,           /* base init */
93                         NULL,           /* base finalize */
94                         (GClassInitFunc) modest_shell_window_class_init,
95                         NULL,           /* class finalize */
96                         NULL,           /* class data */
97                         sizeof(ModestShellWindow),
98                         1,              /* n_preallocs */
99                         (GInstanceInitFunc) modest_shell_window_instance_init,
100                         NULL
101                 };
102                 my_type = g_type_register_static (MODEST_TYPE_WINDOW,
103                                                   "ModestShellWindow",
104                                                   &my_info, 0);
105         }
106         return my_type;
107 }
108
109 static void
110 modest_shell_window_class_init (gpointer klass, gpointer class_data)
111 {
112         GObjectClass *gobject_class;
113         gobject_class = (GObjectClass*) klass;
114         ModestWindowClass *modest_window_class = (ModestWindowClass *) klass;
115
116         parent_class            = g_type_class_peek_parent (klass);
117         gobject_class->dispose  = modest_shell_window_dispose;
118
119         g_type_class_add_private (gobject_class, sizeof(ModestShellWindowPrivate));
120         
121         modest_window_class->zoom_minus_func = on_zoom_minus_plus_not_implemented;
122         modest_window_class->zoom_plus_func = on_zoom_minus_plus_not_implemented;
123         modest_window_class->show_toolbar_func = modest_shell_window_show_toolbar;
124         modest_window_class->add_toolbar_func = modest_shell_window_add_toolbar;
125         modest_window_class->add_to_menu_func = modest_shell_window_add_to_menu;
126         modest_window_class->add_item_to_menu_func = modest_shell_window_add_item_to_menu;
127         modest_window_class->set_title_func = modest_shell_window_set_title;
128         modest_window_class->show_progress_func = modest_shell_window_show_progress;
129
130 }
131
132 static void
133 modest_shell_window_dispose (GObject *obj)
134 {
135         ModestShellWindowPrivate *priv;
136
137         priv = MODEST_SHELL_WINDOW_GET_PRIVATE(obj);
138
139         if (priv->app_menu_dimming_group) {
140                 g_object_unref (priv->app_menu_dimming_group);
141                 priv->app_menu_dimming_group = NULL;
142         }
143
144         if (priv->menu) {
145                 gtk_widget_destroy (priv->menu);
146                 priv->menu = NULL;
147         }
148
149         G_OBJECT_CLASS(parent_class)->dispose (obj);
150 }
151
152 static void
153 modest_shell_window_instance_init (GTypeInstance *instance, gpointer g_class)
154 {
155         ModestShellWindow *self = NULL; 
156         ModestWindowPrivate *parent_priv = NULL;
157         ModestShellWindowPrivate *priv = NULL;
158
159         self = (ModestShellWindow *) instance;
160         parent_priv = MODEST_WINDOW_GET_PRIVATE (self);
161         priv = MODEST_SHELL_WINDOW_GET_PRIVATE (self);
162
163         priv->accel_group = gtk_accel_group_new ();
164
165         priv->app_menu_dimming_group = modest_dimming_rules_group_new (MODEST_DIMMING_RULES_MENU, FALSE);
166         gtk_window_add_accel_group (GTK_WINDOW (self), priv->accel_group);
167
168         priv->menu = gtk_menu_new ();
169
170         modest_ui_dimming_manager_insert_rules_group (parent_priv->ui_dimming_manager, 
171                                                       priv->app_menu_dimming_group);
172
173         /* Dont't restore settings here, 
174          * because it requires a gtk_widget_show(), 
175          * and we don't want to do that until later,
176          * so that the UI is not visible for non-menu D-Bus activation.
177          */
178 }
179
180 static gboolean
181 on_zoom_minus_plus_not_implemented (ModestWindow *window)
182 {
183         g_return_val_if_fail (MODEST_IS_SHELL_WINDOW (window), FALSE);
184
185         modest_platform_information_banner (NULL, NULL, _CS("ckct_ib_cannot_zoom_here"));
186         return FALSE;
187 }
188 void 
189 modest_shell_window_add_item_to_menu (ModestWindow *self,
190                                         GtkWidget *button,
191                                         ModestDimmingCallback dimming_callback)
192 {
193         ModestShellWindowPrivate *priv;
194
195         g_return_if_fail (MODEST_IS_SHELL_WINDOW(self));
196         g_return_if_fail (GTK_IS_BUTTON (button));
197         priv = MODEST_SHELL_WINDOW_GET_PRIVATE (self);
198
199         modest_ui_dimming_manager_set_widget_dimming_mode (GTK_WIDGET (button),
200                                                            MODEST_UI_DIMMING_MODE_HIDE);
201
202         if (dimming_callback)
203                 modest_dimming_rules_group_add_widget_rule (priv->app_menu_dimming_group,
204                                                             GTK_WIDGET (button),
205                                                             (GCallback) dimming_callback,
206                                                             MODEST_WINDOW (self));
207         if (priv->menu) {
208                 gtk_menu_shell_append (GTK_MENU_SHELL (priv->menu), button);
209         } else {
210                 gtk_widget_destroy (button);
211         }
212
213         gtk_widget_show (GTK_WIDGET (button));
214 }
215
216 static void
217 modest_shell_window_add_to_menu (ModestWindow *self,
218                                    const gchar *label,
219                                    const gchar *accelerator,
220                                    ModestWindowMenuCallback callback,
221                                    ModestDimmingCallback dimming_callback)
222 {
223         ModestShellWindowPrivate *priv = NULL;
224         GtkWidget *menu_item;
225
226         g_return_if_fail (MODEST_IS_SHELL_WINDOW(self));
227         g_return_if_fail (label && label[0] != '\0');
228         g_return_if_fail (callback != NULL);
229
230         priv = MODEST_SHELL_WINDOW_GET_PRIVATE (self);
231
232         menu_item = gtk_menu_item_new_with_label (label);
233         g_signal_connect_after (G_OBJECT (menu_item), "activate-item",
234                                 G_CALLBACK (callback), (gpointer) self);
235
236         if (accelerator != NULL) {
237                 guint accel_key;
238                 GdkModifierType accel_mods;
239
240                 gtk_accelerator_parse (accelerator, &accel_key, &accel_mods);
241                 gtk_widget_add_accelerator (menu_item, "clicked", priv->accel_group,
242                                             accel_key, accel_mods, 0);
243         }
244
245         if (priv->menu) {
246                 gtk_menu_shell_append (GTK_MENU_SHELL (priv->menu), menu_item);
247         } else {
248                 gtk_widget_destroy (menu_item);
249         }
250 }
251
252 static void
253 modest_shell_window_show_toolbar (ModestWindow *self,
254                                     gboolean show_toolbar)
255 {
256         /* Empty implementation: Hildon 2.2 implementation
257          * doesn't switch toolbar visibility */
258 }
259
260 static void
261 modest_shell_window_add_toolbar (ModestWindow *self,
262                                  GtkToolbar *toolbar)
263 {
264         gtk_box_pack_end (GTK_BOX (self), GTK_WIDGET (toolbar), FALSE, FALSE, 0);
265 }
266
267 static void
268 modest_shell_window_set_title (ModestWindow *self,
269                                const gchar *title)
270 {
271         ModestShellWindowPrivate *priv = NULL;
272
273         priv = MODEST_SHELL_WINDOW_GET_PRIVATE (self);
274         modest_shell_set_title (MODEST_SHELL (priv->shell),
275                                 MODEST_WINDOW (self),
276                                 title);
277 }
278
279 static void
280 modest_shell_window_show_progress (ModestWindow *self,
281                                      gboolean show)
282 {
283         ModestShellWindowPrivate *priv = NULL;
284
285         priv = MODEST_SHELL_WINDOW_GET_PRIVATE (self);
286         modest_shell_show_progress (MODEST_SHELL (priv->shell),
287                                     self,
288                                     show);
289 }
290
291 void
292 modest_shell_window_set_shell (ModestShellWindow *self,
293                                ModestShell *shell)
294 {
295         ModestShellWindowPrivate *priv = NULL;
296
297         priv = MODEST_SHELL_WINDOW_GET_PRIVATE (self);
298
299         if (priv->shell) {
300                 modest_shell_delete_window (MODEST_SHELL (shell), MODEST_WINDOW (self));
301                 g_object_unref (priv->shell);
302         }
303
304         priv->shell = g_object_ref (shell);
305 }
306
307 GtkWidget *
308 modest_shell_window_get_menu (ModestShellWindow *self)
309 {
310         ModestShellWindowPrivate *priv = NULL;
311
312         priv = MODEST_SHELL_WINDOW_GET_PRIVATE (self);
313
314         return priv->menu;
315 }