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