f1fedb12b20cdab6fadc49e4b8c28e99cc5bbb1e
[modest] / src / hildon2 / modest-hildon2-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 <hildon/hildon-banner.h>
31 #include <modest-platform.h>
32 #include <hildon/hildon-program.h>
33 #include <modest-maemo-utils.h>
34 #include <modest-defs.h>
35 #include <modest-ui-dimming-rules.h>
36 #include <modest-ui-dimming-manager.h>
37 #include <modest-window-priv.h>
38 #include <modest-hildon2-window.h>
39 #include <modest-ui-actions.h>
40
41 /* 'private'/'protected' functions */
42 static void modest_hildon2_window_class_init  (gpointer klass, gpointer class_data);
43 static void modest_hildon2_window_instance_init (GTypeInstance *instance, gpointer g_class);
44 static void modest_hildon2_window_finalize    (GObject *obj);
45
46 static gboolean on_zoom_minus_plus_not_implemented (ModestWindow *window);
47 static void setup_menu (ModestHildon2Window *self);
48
49 static void modest_hildon2_window_show_toolbar (ModestWindow *self,
50                                                  gboolean show_toolbar);
51 static gboolean modest_hildon2_window_toggle_menu (HildonWindow *window,
52                                                     guint button,
53                                                     guint32 time);
54 static void modest_hildon2_window_pack_toolbar_not_implemented (ModestHildon2Window *self,
55                                                                 GtkPackType pack_type,
56                                                                 GtkWidget *toolbar);
57
58 typedef struct _ModestHildon2WindowPrivate ModestHildon2WindowPrivate;
59 struct _ModestHildon2WindowPrivate {
60
61         GtkWidget *app_menu;
62         ModestDimmingRulesGroup *app_menu_dimming_group;
63
64 };
65 #define MODEST_HILDON2_WINDOW_GET_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE((o), \
66                                                                             MODEST_TYPE_HILDON2_WINDOW, \
67                                                                             ModestHildon2WindowPrivate))
68
69 /* globals */
70 static GtkWindowClass *parent_class = NULL;
71
72 /************************************************************************/
73
74 GType
75 modest_hildon2_window_get_type (void)
76 {
77         static GType my_type = 0;
78         if (!my_type) {
79                 static const GTypeInfo my_info = {
80                         sizeof(ModestHildon2WindowClass),
81                         NULL,           /* base init */
82                         NULL,           /* base finalize */
83                         (GClassInitFunc) modest_hildon2_window_class_init,
84                         NULL,           /* class finalize */
85                         NULL,           /* class data */
86                         sizeof(ModestHildon2Window),
87                         1,              /* n_preallocs */
88                         (GInstanceInitFunc) modest_hildon2_window_instance_init,
89                         NULL
90                 };
91                 my_type = g_type_register_static (MODEST_TYPE_WINDOW,
92                                                   "ModestHildon2Window",
93                                                   &my_info, 0);
94         }
95         return my_type;
96 }
97
98 static void
99 modest_hildon2_window_class_init (gpointer klass, gpointer class_data)
100 {
101         GObjectClass *gobject_class;
102         gobject_class = (GObjectClass*) klass;
103         ModestWindowClass *modest_window_class = (ModestWindowClass *) klass;
104         HildonWindowClass *hildon_window_class = (HildonWindowClass *) klass;
105         ModestHildon2WindowClass *modest_hildon2_window_class = (ModestHildon2WindowClass *) klass;
106
107         parent_class            = g_type_class_peek_parent (klass);
108         gobject_class->finalize = modest_hildon2_window_finalize;
109
110         g_type_class_add_private (gobject_class, sizeof(ModestHildon2WindowPrivate));
111         
112         hildon_window_class->toggle_menu = modest_hildon2_window_toggle_menu;
113
114         modest_window_class->zoom_minus_func = on_zoom_minus_plus_not_implemented;
115         modest_window_class->zoom_plus_func = on_zoom_minus_plus_not_implemented;
116         modest_window_class->show_toolbar_func = modest_hildon2_window_show_toolbar;
117
118         modest_hildon2_window_class->pack_toolbar_func = modest_hildon2_window_pack_toolbar_not_implemented;
119 }
120
121 static void
122 modest_hildon2_window_finalize (GObject *obj)
123 {
124         ModestHildon2WindowPrivate *priv;
125
126         priv = MODEST_HILDON2_WINDOW_GET_PRIVATE(obj);
127
128         g_object_unref (priv->app_menu_dimming_group);
129         priv->app_menu_dimming_group = NULL;
130
131         G_OBJECT_CLASS(parent_class)->finalize (obj);
132 }
133
134 static void
135 modest_hildon2_window_instance_init (GTypeInstance *instance, gpointer g_class)
136 {
137         ModestHildon2Window *self = NULL;       
138         ModestWindowPrivate *parent_priv = NULL;
139         ModestHildon2WindowPrivate *priv = NULL;
140
141         self = (ModestHildon2Window *) instance;
142         parent_priv = MODEST_WINDOW_GET_PRIVATE (self);
143         priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
144
145         parent_priv->ui_dimming_manager = modest_ui_dimming_manager_new();
146         priv->app_menu_dimming_group = modest_dimming_rules_group_new (MODEST_DIMMING_RULES_MENU, FALSE);
147
148         setup_menu (self);
149
150         modest_ui_dimming_manager_insert_rules_group (parent_priv->ui_dimming_manager, 
151                                                       priv->app_menu_dimming_group);
152
153         /* Dont't restore settings here, 
154          * because it requires a gtk_widget_show(), 
155          * and we don't want to do that until later,
156          * so that the UI is not visible for non-menu D-Bus activation.
157          */
158 }
159
160 static gboolean
161 on_zoom_minus_plus_not_implemented (ModestWindow *window)
162 {
163         g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW (window), FALSE);
164
165         hildon_banner_show_information (NULL, NULL, dgettext("hildon-common-strings", "ckct_ib_cannot_zoom_here"));
166         return FALSE;
167
168 }
169
170 static void 
171 modest_hildon2_window_pack_toolbar_not_implemented (ModestHildon2Window *self,
172                                                     GtkPackType pack_type,
173                                                     GtkWidget *toolbar)
174 {
175         g_return_if_fail (MODEST_IS_HILDON2_WINDOW (self));
176
177         g_warning ("%s not implemented", __FUNCTION__);
178 }
179
180 void 
181 modest_hildon2_window_add_button_to_menu (ModestHildon2Window *self,
182                                           GtkButton *button,
183                                           ModestDimmingCallback dimming_callback)
184 {
185         ModestHildon2WindowPrivate *priv;
186
187         g_return_if_fail (MODEST_IS_HILDON2_WINDOW(self));
188         g_return_if_fail (GTK_IS_BUTTON (button));
189         priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
190
191         if (dimming_callback)
192                 modest_dimming_rules_group_add_widget_rule (priv->app_menu_dimming_group,
193                                                             GTK_WIDGET (button),
194                                                             (GCallback) dimming_callback,
195                                                             MODEST_WINDOW (self));
196         hildon_app_menu_append (HILDON_APP_MENU (priv->app_menu), GTK_BUTTON (button));
197 }
198
199 void 
200 modest_hildon2_window_add_to_menu (ModestHildon2Window *self,
201                                    gchar *label,
202                                    ModestHildon2AppMenuCallback callback,
203                                    ModestDimmingCallback dimming_callback)
204 {
205         ModestHildon2WindowPrivate *priv = NULL;
206         GtkWidget *button;
207
208         g_return_if_fail (MODEST_IS_HILDON2_WINDOW(self));
209         g_return_if_fail (label && label[0] != '\0');
210         g_return_if_fail (callback != NULL);
211
212         priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
213
214         button = gtk_button_new_with_label (label);
215         g_signal_connect_after (G_OBJECT (button), "clicked",
216                                 G_CALLBACK (callback), (gpointer) self);
217
218         modest_hildon2_window_add_button_to_menu (self, GTK_BUTTON (button), dimming_callback);
219 }
220
221 static void setup_menu (ModestHildon2Window *self)
222 {
223         ModestHildon2WindowPrivate *priv = NULL;
224
225         g_return_if_fail (MODEST_IS_HILDON2_WINDOW(self));
226
227         priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
228
229         priv->app_menu = hildon_app_menu_new ();
230
231         /* we expect that the app menu is filled in children using the expected 
232          * add_to_menu methods */
233
234         hildon_stackable_window_set_main_menu (HILDON_STACKABLE_WINDOW (self), 
235                                                HILDON_APP_MENU (priv->app_menu));
236 }
237
238 static gboolean 
239 modest_hildon2_window_toggle_menu (HildonWindow *window,
240                                     guint button,
241                                     guint32 time)
242 {
243         ModestHildon2WindowPrivate *priv = NULL;
244
245         priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (window);
246
247         modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (window));
248
249         gtk_widget_queue_resize (priv->app_menu);
250
251         return HILDON_WINDOW_CLASS (parent_class)->toggle_menu (window, button, time);
252 }
253
254 static void
255 modest_hildon2_window_show_toolbar (ModestWindow *self,
256                                     gboolean show_toolbar)
257 {
258         /* Empty implementation: Hildon 2.2 implementation
259          * doesn't switch toolbar visibility */
260 }