1 /* Copyright (c) 2008, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
30 #include <hildon/hildon-banner.h>
31 #include <modest-platform.h>
32 #include <hildon/hildon-program.h>
33 #include "modest-marshal.h"
34 #include <modest-maemo-utils.h>
35 #include <modest-defs.h>
36 #include <modest-ui-dimming-rules.h>
37 #include <modest-ui-dimming-manager.h>
38 #include <modest-window-priv.h>
39 #include <modest-hildon2-window.h>
40 #include <modest-ui-actions.h>
41 #include <hildon/hildon-edit-toolbar.h>
43 typedef struct _EditModeRegister {
47 GtkSelectionMode mode;
48 ModestHildon2EditModeCallback action;
51 /* 'private'/'protected' functions */
52 static void modest_hildon2_window_class_init (gpointer klass, gpointer class_data);
53 static void modest_hildon2_window_instance_init (GTypeInstance *instance, gpointer g_class);
54 static void modest_hildon2_window_finalize (GObject *obj);
56 static gboolean on_zoom_minus_plus_not_implemented (ModestWindow *window);
57 static void setup_menu (ModestHildon2Window *self);
59 static void modest_hildon2_window_show_toolbar (ModestWindow *self,
60 gboolean show_toolbar);
61 static gboolean modest_hildon2_window_toggle_menu (HildonWindow *window,
64 static void modest_hildon2_window_pack_toolbar_not_implemented (ModestHildon2Window *self,
65 GtkPackType pack_type,
67 static EditModeRegister *edit_mode_register_new (const gchar *description,
68 const gchar *button_label,
69 GtkTreeView *tree_view,
70 GtkSelectionMode mode,
71 ModestHildon2EditModeCallback action);
72 static void edit_mode_register_destroy (gpointer data);
73 static void edit_toolbar_button_clicked (HildonEditToolbar *toolbar,
74 ModestHildon2Window *self);
75 static void edit_toolbar_arrow_clicked (HildonEditToolbar *toolbar,
76 ModestHildon2Window *self);
78 typedef struct _ModestHildon2WindowPrivate ModestHildon2WindowPrivate;
79 struct _ModestHildon2WindowPrivate {
82 ModestDimmingRulesGroup *app_menu_dimming_group;
83 GtkAccelGroup *accel_group;
85 /* Edit mode support */
88 GtkWidget *edit_toolbar;
89 GtkWidget *current_edit_tree_view;
90 GHashTable *edit_mode_registry;
92 #define MODEST_HILDON2_WINDOW_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
93 MODEST_TYPE_HILDON2_WINDOW, \
94 ModestHildon2WindowPrivate))
98 EDIT_MODE_CHANGED_SIGNAL,
103 static GtkWindowClass *parent_class = NULL;
105 /* uncomment the following if you have defined any signals */
106 static guint signals[LAST_SIGNAL] = {0};
108 /************************************************************************/
111 modest_hildon2_window_get_type (void)
113 static GType my_type = 0;
115 static const GTypeInfo my_info = {
116 sizeof(ModestHildon2WindowClass),
117 NULL, /* base init */
118 NULL, /* base finalize */
119 (GClassInitFunc) modest_hildon2_window_class_init,
120 NULL, /* class finalize */
121 NULL, /* class data */
122 sizeof(ModestHildon2Window),
124 (GInstanceInitFunc) modest_hildon2_window_instance_init,
127 my_type = g_type_register_static (MODEST_TYPE_WINDOW,
128 "ModestHildon2Window",
135 modest_hildon2_window_class_init (gpointer klass, gpointer class_data)
137 GObjectClass *gobject_class;
138 gobject_class = (GObjectClass*) klass;
139 ModestWindowClass *modest_window_class = (ModestWindowClass *) klass;
140 HildonWindowClass *hildon_window_class = (HildonWindowClass *) klass;
141 ModestHildon2WindowClass *modest_hildon2_window_class = (ModestHildon2WindowClass *) klass;
143 parent_class = g_type_class_peek_parent (klass);
144 gobject_class->finalize = modest_hildon2_window_finalize;
146 signals[EDIT_MODE_CHANGED_SIGNAL] =
147 g_signal_new ("edit-mode-changed",
148 G_TYPE_FROM_CLASS (gobject_class),
150 G_STRUCT_OFFSET (ModestHildon2WindowClass, edit_mode_changed),
152 modest_marshal_VOID__INT_BOOLEAN,
153 G_TYPE_NONE, 2, G_TYPE_INT, G_TYPE_BOOLEAN);
155 g_type_class_add_private (gobject_class, sizeof(ModestHildon2WindowPrivate));
157 hildon_window_class->toggle_menu = modest_hildon2_window_toggle_menu;
159 modest_window_class->zoom_minus_func = on_zoom_minus_plus_not_implemented;
160 modest_window_class->zoom_plus_func = on_zoom_minus_plus_not_implemented;
161 modest_window_class->show_toolbar_func = modest_hildon2_window_show_toolbar;
163 modest_hildon2_window_class->pack_toolbar_func = modest_hildon2_window_pack_toolbar_not_implemented;
167 modest_hildon2_window_finalize (GObject *obj)
169 ModestHildon2WindowPrivate *priv;
171 priv = MODEST_HILDON2_WINDOW_GET_PRIVATE(obj);
173 g_object_unref (priv->app_menu_dimming_group);
174 priv->app_menu_dimming_group = NULL;
176 g_hash_table_destroy (priv->edit_mode_registry);
177 priv->edit_mode_registry = NULL;
179 G_OBJECT_CLASS(parent_class)->finalize (obj);
183 modest_hildon2_window_instance_init (GTypeInstance *instance, gpointer g_class)
185 ModestHildon2Window *self = NULL;
186 ModestWindowPrivate *parent_priv = NULL;
187 ModestHildon2WindowPrivate *priv = NULL;
189 self = (ModestHildon2Window *) instance;
190 parent_priv = MODEST_WINDOW_GET_PRIVATE (self);
191 priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
193 priv->accel_group = gtk_accel_group_new ();
195 priv->edit_mode = FALSE;
196 priv->edit_toolbar = NULL;
197 priv->current_edit_tree_view = NULL;
198 priv->edit_command = MODEST_HILDON2_WINDOW_EDIT_MODE_NONE;
199 priv->edit_mode_registry = g_hash_table_new_full (g_direct_hash, g_direct_equal,
200 NULL, edit_mode_register_destroy);
202 parent_priv->ui_dimming_manager = modest_ui_dimming_manager_new();
203 priv->app_menu_dimming_group = modest_dimming_rules_group_new (MODEST_DIMMING_RULES_MENU, FALSE);
204 gtk_window_add_accel_group (GTK_WINDOW (self), priv->accel_group);
208 modest_ui_dimming_manager_insert_rules_group (parent_priv->ui_dimming_manager,
209 priv->app_menu_dimming_group);
211 /* Dont't restore settings here,
212 * because it requires a gtk_widget_show(),
213 * and we don't want to do that until later,
214 * so that the UI is not visible for non-menu D-Bus activation.
219 on_zoom_minus_plus_not_implemented (ModestWindow *window)
221 g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW (window), FALSE);
223 hildon_banner_show_information (NULL, NULL, dgettext("hildon-common-strings", "ckct_ib_cannot_zoom_here"));
229 modest_hildon2_window_pack_toolbar_not_implemented (ModestHildon2Window *self,
230 GtkPackType pack_type,
233 g_return_if_fail (MODEST_IS_HILDON2_WINDOW (self));
235 g_warning ("%s not implemented", __FUNCTION__);
239 modest_hildon2_window_pack_toolbar (ModestHildon2Window *self,
240 GtkPackType pack_type,
243 g_return_if_fail (MODEST_IS_HILDON2_WINDOW (self));
245 MODEST_HILDON2_WINDOW_GET_CLASS (self)->pack_toolbar_func (self, pack_type, toolbar);
249 modest_hildon2_window_add_button_to_menu (ModestHildon2Window *self,
251 ModestDimmingCallback dimming_callback)
253 ModestHildon2WindowPrivate *priv;
255 g_return_if_fail (MODEST_IS_HILDON2_WINDOW(self));
256 g_return_if_fail (GTK_IS_BUTTON (button));
257 priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
259 if (dimming_callback)
260 modest_dimming_rules_group_add_widget_rule (priv->app_menu_dimming_group,
262 (GCallback) dimming_callback,
263 MODEST_WINDOW (self));
264 hildon_app_menu_append (HILDON_APP_MENU (priv->app_menu), GTK_BUTTON (button));
268 modest_hildon2_window_add_to_menu (ModestHildon2Window *self,
270 const gchar *accelerator,
271 ModestHildon2AppMenuCallback callback,
272 ModestDimmingCallback dimming_callback)
274 ModestHildon2WindowPrivate *priv = NULL;
277 g_return_if_fail (MODEST_IS_HILDON2_WINDOW(self));
278 g_return_if_fail (label && label[0] != '\0');
279 g_return_if_fail (callback != NULL);
281 priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
283 button = gtk_button_new_with_label (label);
284 g_signal_connect_after (G_OBJECT (button), "clicked",
285 G_CALLBACK (callback), (gpointer) self);
287 if (accelerator != NULL) {
289 GdkModifierType accel_mods;
291 gtk_accelerator_parse (accelerator, &accel_key, &accel_mods);
292 gtk_widget_add_accelerator (button, "clicked", priv->accel_group,
293 accel_key, accel_mods, 0);
296 modest_hildon2_window_add_button_to_menu (self, GTK_BUTTON (button), dimming_callback);
299 static void setup_menu (ModestHildon2Window *self)
301 ModestHildon2WindowPrivate *priv = NULL;
303 g_return_if_fail (MODEST_IS_HILDON2_WINDOW(self));
305 priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
307 priv->app_menu = hildon_app_menu_new ();
309 /* we expect that the app menu is filled in children using the expected
310 * add_to_menu methods */
312 hildon_stackable_window_set_main_menu (HILDON_STACKABLE_WINDOW (self),
313 HILDON_APP_MENU (priv->app_menu));
317 modest_hildon2_window_toggle_menu (HildonWindow *window,
321 ModestHildon2WindowPrivate *priv = NULL;
323 priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (window);
325 modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (window));
327 gtk_widget_queue_resize (priv->app_menu);
329 return HILDON_WINDOW_CLASS (parent_class)->toggle_menu (window, button, time);
333 modest_hildon2_window_show_toolbar (ModestWindow *self,
334 gboolean show_toolbar)
336 /* Empty implementation: Hildon 2.2 implementation
337 * doesn't switch toolbar visibility */
341 modest_hildon2_window_register_edit_mode (ModestHildon2Window *self,
343 const gchar *description,
344 const gchar *button_label,
345 GtkTreeView *tree_view,
346 GtkSelectionMode mode,
347 ModestHildon2EditModeCallback action)
349 ModestHildon2WindowPrivate *priv = NULL;
350 EditModeRegister *reg;
352 g_return_if_fail (MODEST_IS_HILDON2_WINDOW (self));
353 g_return_if_fail (edit_mode_id >= 0);
354 g_return_if_fail (description);
355 g_return_if_fail (button_label);
356 g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));
358 priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
360 reg = (EditModeRegister *) g_hash_table_lookup (priv->edit_mode_registry, GINT_TO_POINTER (edit_mode_id));
361 g_return_if_fail (reg == NULL);
363 reg = edit_mode_register_new (description, button_label, tree_view, mode, action);
364 g_hash_table_insert (priv->edit_mode_registry, GINT_TO_POINTER (edit_mode_id), (gpointer) reg);
368 modest_hildon2_window_set_edit_mode (ModestHildon2Window *self,
371 ModestHildon2WindowPrivate *priv = NULL;
372 EditModeRegister *reg;
373 GtkTreeSelection *selection;
375 g_return_if_fail (MODEST_IS_HILDON2_WINDOW (self));
376 g_return_if_fail (edit_mode_id >= 0);
378 priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
379 reg = (EditModeRegister *) g_hash_table_lookup (priv->edit_mode_registry, GINT_TO_POINTER (edit_mode_id));
380 g_return_if_fail (reg != NULL);
382 if (priv->edit_mode) {
383 modest_hildon2_window_unset_edit_mode (self);
386 priv->edit_mode = TRUE;
387 priv->edit_command = edit_mode_id;
389 priv->current_edit_tree_view = reg->tree_view;
390 g_object_set (G_OBJECT (priv->current_edit_tree_view),
391 "hildon-ui-mode", HILDON_UI_MODE_EDIT,
394 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->current_edit_tree_view));
395 gtk_tree_selection_set_mode (selection, reg->mode);
396 gtk_tree_selection_unselect_all (selection);
398 priv->edit_toolbar = hildon_edit_toolbar_new ();
399 hildon_edit_toolbar_set_label (HILDON_EDIT_TOOLBAR (priv->edit_toolbar),
401 hildon_edit_toolbar_set_button_label (HILDON_EDIT_TOOLBAR (priv->edit_toolbar),
403 modest_hildon2_window_pack_toolbar (self, GTK_PACK_START,
406 g_signal_connect (G_OBJECT (priv->edit_toolbar), "button-clicked",
407 G_CALLBACK (edit_toolbar_button_clicked), (gpointer) self);
408 g_signal_connect (G_OBJECT (priv->edit_toolbar), "arrow-clicked",
409 G_CALLBACK (edit_toolbar_arrow_clicked), (gpointer) self);
411 gtk_widget_show (priv->edit_toolbar);
412 gtk_widget_queue_resize (priv->current_edit_tree_view);
413 gtk_window_fullscreen (GTK_WINDOW (self));
415 g_signal_emit (G_OBJECT (self), signals[EDIT_MODE_CHANGED_SIGNAL], 0,
416 priv->edit_command, priv->edit_mode);
420 modest_hildon2_window_unset_edit_mode (ModestHildon2Window *self)
422 ModestHildon2WindowPrivate *priv = NULL;
424 g_return_if_fail (MODEST_IS_HILDON2_WINDOW (self));
425 priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
427 if (priv->edit_toolbar) {
428 gtk_widget_destroy (priv->edit_toolbar);
429 priv->edit_toolbar = NULL;
432 if (priv->edit_mode) {
433 priv->edit_mode = FALSE;
434 if (priv->current_edit_tree_view) {
435 g_object_set (G_OBJECT (priv->current_edit_tree_view),
436 "hildon-ui-mode", HILDON_UI_MODE_NORMAL,
438 gtk_widget_queue_resize (priv->current_edit_tree_view);
439 priv->current_edit_tree_view = NULL;
441 gtk_window_unfullscreen (GTK_WINDOW (self));
442 g_signal_emit (G_OBJECT (self), signals[EDIT_MODE_CHANGED_SIGNAL], 0,
443 priv->edit_command, priv->edit_mode);
444 priv->edit_command = MODEST_HILDON2_WINDOW_EDIT_MODE_NONE;
448 static EditModeRegister *
449 edit_mode_register_new (const gchar *description,
450 const gchar *button_label,
451 GtkTreeView *tree_view,
452 GtkSelectionMode mode,
453 ModestHildon2EditModeCallback action)
455 EditModeRegister *reg;
457 reg = g_slice_new (EditModeRegister);
459 reg->description = g_strdup (description);
460 reg->button_label = g_strdup (button_label);
461 reg->tree_view = g_object_ref (tree_view);
463 reg->action = action;
469 edit_mode_register_destroy (gpointer data)
471 EditModeRegister *reg = (EditModeRegister *) data;
473 g_free (reg->description);
474 g_free (reg->button_label);
475 g_object_unref (reg->tree_view);
477 g_slice_free (EditModeRegister, reg);
481 edit_toolbar_button_clicked (HildonEditToolbar *toolbar,
482 ModestHildon2Window *self)
484 ModestHildon2WindowPrivate *priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
485 EditModeRegister *reg;
487 g_return_if_fail (MODEST_IS_HILDON2_WINDOW (self));
489 reg = (EditModeRegister *) g_hash_table_lookup (priv->edit_mode_registry,
490 GINT_TO_POINTER (priv->edit_command));
492 modest_hildon2_window_unset_edit_mode (self);
494 if ((reg->action == NULL) || reg->action (self))
495 modest_hildon2_window_unset_edit_mode (self);
500 edit_toolbar_arrow_clicked (HildonEditToolbar *toolbar,
501 ModestHildon2Window *self)
503 g_return_if_fail (MODEST_IS_HILDON2_WINDOW (self));
505 modest_hildon2_window_unset_edit_mode (self);