Create ModestShellWindow and integrate with ModestShell (work in progress)
[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 setup_menu (ModestShellWindow *self);
49
50 static void modest_shell_window_show_toolbar (ModestWindow *self,
51                                                  gboolean show_toolbar);
52 static void modest_shell_window_add_toolbar (ModestWindow *self,
53                                                GtkToolbar *toolbar);
54 static void modest_shell_window_add_to_menu (ModestWindow *window,
55                                                const gchar *label,
56                                                const gchar *accelerator,
57                                                ModestWindowMenuCallback callback,
58                                                ModestDimmingCallback dimming_callback);
59 static void modest_shell_window_add_item_to_menu (ModestWindow *window,
60                                                     GtkWidget *item,
61                                                     ModestDimmingCallback dimming_callback);
62 static void modest_shell_window_set_title (ModestWindow *self,
63                                              const gchar *title);
64
65 typedef struct _ModestShellWindowPrivate ModestShellWindowPrivate;
66 struct _ModestShellWindowPrivate {
67
68         GtkWidget *shell;
69         ModestDimmingRulesGroup *app_menu_dimming_group;
70         GtkAccelGroup *accel_group;
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         HildonWindowClass *hildon_window_class = (HildonWindowClass *) klass;
116
117         parent_class            = g_type_class_peek_parent (klass);
118         gobject_class->dispose  = modest_shell_window_dispose;
119
120         g_type_class_add_private (gobject_class, sizeof(ModestShellWindowPrivate));
121         
122         modest_window_class->zoom_minus_func = on_zoom_minus_plus_not_implemented;
123         modest_window_class->zoom_plus_func = on_zoom_minus_plus_not_implemented;
124         modest_window_class->show_toolbar_func = modest_shell_window_show_toolbar;
125         modest_window_class->add_toolbar_func = modest_shell_window_add_toolbar;
126         modest_window_class->add_to_menu_func = modest_shell_window_add_to_menu;
127         modest_window_class->add_item_to_menu_func = modest_shell_window_add_item_to_menu;
128         modest_window_class->set_title_func = modest_shell_window_set_title;
129         modest_window_class->show_progress_func = modest_shell_window_show_progress;
130
131 }
132
133 static void
134 modest_shell_window_dispose (GObject *obj)
135 {
136         ModestShellWindowPrivate *priv;
137
138         priv = MODEST_SHELL_WINDOW_GET_PRIVATE(obj);
139
140         if (priv->app_menu_dimming_group) {
141                 g_object_unref (priv->app_menu_dimming_group);
142                 priv->app_menu_dimming_group = NULL;
143         }
144
145         G_OBJECT_CLASS(parent_class)->dispose (obj);
146 }
147
148 static void
149 modest_shell_window_instance_init (GTypeInstance *instance, gpointer g_class)
150 {
151         ModestShellWindow *self = NULL; 
152         ModestWindowPrivate *parent_priv = NULL;
153         ModestShellWindowPrivate *priv = NULL;
154
155         self = (ModestShellWindow *) instance;
156         parent_priv = MODEST_WINDOW_GET_PRIVATE (self);
157         priv = MODEST_SHELL_WINDOW_GET_PRIVATE (self);
158
159         priv->accel_group = gtk_accel_group_new ();
160
161         priv->app_menu_dimming_group = modest_dimming_rules_group_new (MODEST_DIMMING_RULES_MENU, FALSE);
162         gtk_window_add_accel_group (GTK_WINDOW (self), priv->accel_group);
163
164         setup_menu (self);
165
166         modest_ui_dimming_manager_insert_rules_group (parent_priv->ui_dimming_manager, 
167                                                       priv->app_menu_dimming_group);
168
169         /* Dont't restore settings here, 
170          * because it requires a gtk_widget_show(), 
171          * and we don't want to do that until later,
172          * so that the UI is not visible for non-menu D-Bus activation.
173          */
174 }
175
176 static gboolean
177 on_zoom_minus_plus_not_implemented (ModestWindow *window)
178 {
179         g_return_val_if_fail (MODEST_IS_SHELL_WINDOW (window), FALSE);
180
181         modest_platform_information_banner (NULL, NULL, _CS("ckct_ib_cannot_zoom_here"));
182         return FALSE;
183 }
184 void 
185 modest_shell_window_add_item_to_menu (ModestWindow *self,
186                                         GtkWidget *button,
187                                         ModestDimmingCallback dimming_callback)
188 {
189         ModestShellWindowPrivate *priv;
190         GtkWidget *menu;
191
192         g_return_if_fail (MODEST_IS_SHELL_WINDOW(self));
193         g_return_if_fail (GTK_IS_BUTTON (button));
194         priv = MODEST_SHELL_WINDOW_GET_PRIVATE (self);
195
196         modest_ui_dimming_manager_set_widget_dimming_mode (GTK_WIDGET (button),
197                                                            MODEST_UI_DIMMING_MODE_HIDE);
198
199         if (dimming_callback)
200                 modest_dimming_rules_group_add_widget_rule (priv->app_menu_dimming_group,
201                                                             GTK_WIDGET (button),
202                                                             (GCallback) dimming_callback,
203                                                             MODEST_WINDOW (self));
204         menu = modest_shell_get_menu (MODEST_SHELL (priv->shell), self);
205         gtk_menu_shell_append (GTK_MENU_SHELL (menu), button);
206
207         gtk_widget_show (GTK_WIDGET (button));
208 }
209
210 static void
211 modest_shell_window_add_to_menu (ModestWindow *self,
212                                    const gchar *label,
213                                    const gchar *accelerator,
214                                    ModestWindowMenuCallback callback,
215                                    ModestDimmingCallback dimming_callback)
216 {
217         ModestShellWindowPrivate *priv = NULL;
218         GtkWidget *menu_item;
219         GtkWidget *menu;
220
221         g_return_if_fail (MODEST_IS_SHELL_WINDOW(self));
222         g_return_if_fail (label && label[0] != '\0');
223         g_return_if_fail (callback != NULL);
224
225         priv = MODEST_SHELL_WINDOW_GET_PRIVATE (self);
226
227         menu_item = gtk_menu_item_new_with_label (label);
228         g_signal_connect_after (G_OBJECT (button), "activate-item",
229                                 G_CALLBACK (callback), (gpointer) self);
230
231         if (accelerator != NULL) {
232                 guint accel_key;
233                 GdkModifierType accel_mods;
234
235                 gtk_accelerator_parse (accelerator, &accel_key, &accel_mods);
236                 gtk_widget_add_accelerator (menu_item, "clicked", priv->accel_group,
237                                             accel_key, accel_mods, 0);
238         }
239
240         menu = modest_shell_get_menu (MODEST_SHELL (priv->shell), MODEST_WINDOW (self));
241         gtk_menu_shell_append (GTK_MENU (menu), menu_item);
242 }
243
244 static void
245 modest_shell_window_show_toolbar (ModestWindow *self,
246                                     gboolean show_toolbar)
247 {
248         /* Empty implementation: Hildon 2.2 implementation
249          * doesn't switch toolbar visibility */
250 }
251
252 static void
253 modest_shell_window_add_toolbar (ModestWindow *self,
254                                    GtkToolbar *toolbar)
255 {
256         gtk_box_pack_end (GTK_BOX (self), toolbar, FALSE, FALSE, 0);
257 }
258
259 static void
260 modest_shell_window_set_title (ModestWindow *self,
261                                const gchar *title)
262 {
263         ModestShellWindowPrivate *priv = NULL;
264
265         priv = MODEST_SHELL_WINDOW_GET_PRIVATE (self);
266         modest_shell_set_title (MODEST_SHELL (priv->shell),
267                                 MODEST_WINDOW (self),
268                                 title);
269 }
270
271 static void
272 modest_shell_window_show_progress (ModestWindow *self,
273                                      gboolean show)
274 {
275         modest_shell_show_progress (MODEST_SHELL (priv->shell),
276                                     self,
277                                     show);
278 }
279
280 void
281 modest_shell_window_set_shell (ModestShellWindow *self,
282                                ModestShell *shell)
283 {
284         ModestShellWindowPrivate *priv = NULL;
285
286         priv = MODEST_SHELL_WINDOW_GET_PRIVATE (self);
287
288         if (priv->shell) {
289                 modest_shell_delete_window (MODEST_SHELL (shell), MODEST_WINDOW (self));
290                 g_object_unref (priv->shell);
291         }
292
293         priv->shell = g_object_ref (shell);
294 }