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 "modest-text-utils.h"
42 #include <hildon/hildon-edit-toolbar.h>
44 typedef struct _EditModeRegister {
48 GtkSelectionMode mode;
49 ModestHildon2EditModeCallback action;
52 /* 'private'/'protected' functions */
53 static void modest_hildon2_window_class_init (gpointer klass, gpointer class_data);
54 static void modest_hildon2_window_instance_init (GTypeInstance *instance, gpointer g_class);
55 static void modest_hildon2_window_dispose (GObject *obj);
57 static gboolean on_zoom_minus_plus_not_implemented (ModestWindow *window);
58 static void setup_menu (ModestHildon2Window *self);
60 static void modest_hildon2_window_show_toolbar (ModestWindow *self,
61 gboolean show_toolbar);
62 static gboolean modest_hildon2_window_toggle_menu (HildonWindow *window,
65 static void modest_hildon2_window_pack_toolbar_not_implemented (ModestHildon2Window *self,
66 GtkPackType pack_type,
68 static EditModeRegister *edit_mode_register_new (const gchar *description,
69 const gchar *button_label,
70 GtkTreeView *tree_view,
71 GtkSelectionMode mode,
72 ModestHildon2EditModeCallback action);
73 static void edit_mode_register_destroy (gpointer data);
74 static void edit_toolbar_button_clicked (HildonEditToolbar *toolbar,
75 ModestHildon2Window *self);
76 static void edit_toolbar_arrow_clicked (HildonEditToolbar *toolbar,
77 ModestHildon2Window *self);
79 typedef struct _ModestHildon2WindowPrivate ModestHildon2WindowPrivate;
80 struct _ModestHildon2WindowPrivate {
83 ModestDimmingRulesGroup *app_menu_dimming_group;
84 GtkAccelGroup *accel_group;
86 /* Edit mode support */
89 GtkWidget *edit_toolbar;
90 GtkWidget *current_edit_tree_view;
91 GHashTable *edit_mode_registry;
93 #define MODEST_HILDON2_WINDOW_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
94 MODEST_TYPE_HILDON2_WINDOW, \
95 ModestHildon2WindowPrivate))
99 EDIT_MODE_CHANGED_SIGNAL,
104 static GtkWindowClass *parent_class = NULL;
106 /* uncomment the following if you have defined any signals */
107 static guint signals[LAST_SIGNAL] = {0};
109 /************************************************************************/
112 modest_hildon2_window_get_type (void)
114 static GType my_type = 0;
116 static const GTypeInfo my_info = {
117 sizeof(ModestHildon2WindowClass),
118 NULL, /* base init */
119 NULL, /* base finalize */
120 (GClassInitFunc) modest_hildon2_window_class_init,
121 NULL, /* class finalize */
122 NULL, /* class data */
123 sizeof(ModestHildon2Window),
125 (GInstanceInitFunc) modest_hildon2_window_instance_init,
128 my_type = g_type_register_static (MODEST_TYPE_WINDOW,
129 "ModestHildon2Window",
136 modest_hildon2_window_class_init (gpointer klass, gpointer class_data)
138 GObjectClass *gobject_class;
139 gobject_class = (GObjectClass*) klass;
140 ModestWindowClass *modest_window_class = (ModestWindowClass *) klass;
141 HildonWindowClass *hildon_window_class = (HildonWindowClass *) klass;
142 ModestHildon2WindowClass *modest_hildon2_window_class = (ModestHildon2WindowClass *) klass;
144 parent_class = g_type_class_peek_parent (klass);
145 gobject_class->dispose = modest_hildon2_window_dispose;
147 signals[EDIT_MODE_CHANGED_SIGNAL] =
148 g_signal_new ("edit-mode-changed",
149 G_TYPE_FROM_CLASS (gobject_class),
151 G_STRUCT_OFFSET (ModestHildon2WindowClass, edit_mode_changed),
153 modest_marshal_VOID__INT_BOOLEAN,
154 G_TYPE_NONE, 2, G_TYPE_INT, G_TYPE_BOOLEAN);
156 g_type_class_add_private (gobject_class, sizeof(ModestHildon2WindowPrivate));
158 hildon_window_class->toggle_menu = modest_hildon2_window_toggle_menu;
160 modest_window_class->zoom_minus_func = on_zoom_minus_plus_not_implemented;
161 modest_window_class->zoom_plus_func = on_zoom_minus_plus_not_implemented;
162 modest_window_class->show_toolbar_func = modest_hildon2_window_show_toolbar;
164 modest_hildon2_window_class->pack_toolbar_func = modest_hildon2_window_pack_toolbar_not_implemented;
168 modest_hildon2_window_dispose (GObject *obj)
170 ModestHildon2WindowPrivate *priv;
172 priv = MODEST_HILDON2_WINDOW_GET_PRIVATE(obj);
174 if (priv->app_menu_dimming_group) {
175 g_object_unref (priv->app_menu_dimming_group);
176 priv->app_menu_dimming_group = NULL;
179 if (priv->edit_mode_registry) {
180 g_hash_table_unref (priv->edit_mode_registry);
181 priv->edit_mode_registry = NULL;
184 G_OBJECT_CLASS(parent_class)->dispose (obj);
188 modest_hildon2_window_instance_init (GTypeInstance *instance, gpointer g_class)
190 ModestHildon2Window *self = NULL;
191 ModestWindowPrivate *parent_priv = NULL;
192 ModestHildon2WindowPrivate *priv = NULL;
194 self = (ModestHildon2Window *) instance;
195 parent_priv = MODEST_WINDOW_GET_PRIVATE (self);
196 priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
198 priv->accel_group = gtk_accel_group_new ();
200 priv->edit_mode = FALSE;
201 priv->edit_toolbar = NULL;
202 priv->current_edit_tree_view = NULL;
203 priv->edit_command = MODEST_HILDON2_WINDOW_EDIT_MODE_NONE;
204 priv->edit_mode_registry = g_hash_table_new_full (g_direct_hash, g_direct_equal,
205 NULL, edit_mode_register_destroy);
207 parent_priv->ui_dimming_manager = modest_ui_dimming_manager_new();
208 priv->app_menu_dimming_group = modest_dimming_rules_group_new (MODEST_DIMMING_RULES_MENU, FALSE);
209 gtk_window_add_accel_group (GTK_WINDOW (self), priv->accel_group);
213 modest_ui_dimming_manager_insert_rules_group (parent_priv->ui_dimming_manager,
214 priv->app_menu_dimming_group);
216 /* Dont't restore settings here,
217 * because it requires a gtk_widget_show(),
218 * and we don't want to do that until later,
219 * so that the UI is not visible for non-menu D-Bus activation.
224 on_zoom_minus_plus_not_implemented (ModestWindow *window)
226 g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW (window), FALSE);
228 hildon_banner_show_information (NULL, NULL, _CS("ckct_ib_cannot_zoom_here"));
233 modest_hildon2_window_pack_toolbar_not_implemented (ModestHildon2Window *self,
234 GtkPackType pack_type,
237 g_return_if_fail (MODEST_IS_HILDON2_WINDOW (self));
239 g_debug ("%s not implemented", __FUNCTION__);
243 modest_hildon2_window_pack_toolbar (ModestHildon2Window *self,
244 GtkPackType pack_type,
247 g_return_if_fail (MODEST_IS_HILDON2_WINDOW (self));
249 MODEST_HILDON2_WINDOW_GET_CLASS (self)->pack_toolbar_func (self, pack_type, toolbar);
253 modest_hildon2_window_add_button_to_menu (ModestHildon2Window *self,
255 ModestDimmingCallback dimming_callback)
257 ModestHildon2WindowPrivate *priv;
259 g_return_if_fail (MODEST_IS_HILDON2_WINDOW(self));
260 g_return_if_fail (GTK_IS_BUTTON (button));
261 priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
263 modest_ui_dimming_manager_set_widget_dimming_mode (GTK_WIDGET (button),
264 MODEST_UI_DIMMING_MODE_HIDE);
266 if (dimming_callback)
267 modest_dimming_rules_group_add_widget_rule (priv->app_menu_dimming_group,
269 (GCallback) dimming_callback,
270 MODEST_WINDOW (self));
271 hildon_app_menu_append (HILDON_APP_MENU (priv->app_menu), GTK_BUTTON (button));
272 gtk_widget_show (GTK_WIDGET (button));
276 modest_hildon2_window_add_to_menu (ModestHildon2Window *self,
278 const gchar *accelerator,
279 ModestHildon2AppMenuCallback callback,
280 ModestDimmingCallback dimming_callback)
282 ModestHildon2WindowPrivate *priv = NULL;
285 g_return_if_fail (MODEST_IS_HILDON2_WINDOW(self));
286 g_return_if_fail (label && label[0] != '\0');
287 g_return_if_fail (callback != NULL);
289 priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
291 button = gtk_button_new_with_label (label);
292 g_signal_connect_after (G_OBJECT (button), "clicked",
293 G_CALLBACK (callback), (gpointer) self);
295 if (accelerator != NULL) {
297 GdkModifierType accel_mods;
299 gtk_accelerator_parse (accelerator, &accel_key, &accel_mods);
300 gtk_widget_add_accelerator (button, "clicked", priv->accel_group,
301 accel_key, accel_mods, 0);
304 modest_hildon2_window_add_button_to_menu (self, GTK_BUTTON (button), dimming_callback);
307 static void setup_menu (ModestHildon2Window *self)
309 ModestHildon2WindowPrivate *priv = NULL;
311 g_return_if_fail (MODEST_IS_HILDON2_WINDOW(self));
313 priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
315 priv->app_menu = hildon_app_menu_new ();
317 /* we expect that the app menu is filled in children using the expected
318 * add_to_menu methods */
320 hildon_stackable_window_set_main_menu (HILDON_STACKABLE_WINDOW (self),
321 HILDON_APP_MENU (priv->app_menu));
325 modest_hildon2_window_toggle_menu (HildonWindow *window,
329 ModestHildon2WindowPrivate *priv = NULL;
331 priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (window);
333 modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (window));
335 gtk_widget_queue_resize (priv->app_menu);
337 return HILDON_WINDOW_CLASS (parent_class)->toggle_menu (window, button, time);
341 modest_hildon2_window_show_toolbar (ModestWindow *self,
342 gboolean show_toolbar)
344 /* Empty implementation: Hildon 2.2 implementation
345 * doesn't switch toolbar visibility */
349 modest_hildon2_window_register_edit_mode (ModestHildon2Window *self,
351 const gchar *description,
352 const gchar *button_label,
353 GtkTreeView *tree_view,
354 GtkSelectionMode mode,
355 ModestHildon2EditModeCallback action)
357 ModestHildon2WindowPrivate *priv = NULL;
358 EditModeRegister *reg;
360 g_return_if_fail (MODEST_IS_HILDON2_WINDOW (self));
361 g_return_if_fail (edit_mode_id >= 0);
362 g_return_if_fail (description);
363 g_return_if_fail (button_label);
364 g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));
366 priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
368 reg = (EditModeRegister *) g_hash_table_lookup (priv->edit_mode_registry, GINT_TO_POINTER (edit_mode_id));
369 g_return_if_fail (reg == NULL);
371 reg = edit_mode_register_new (description, button_label, tree_view, mode, action);
372 g_hash_table_insert (priv->edit_mode_registry, GINT_TO_POINTER (edit_mode_id), (gpointer) reg);
376 modest_hildon2_window_set_edit_mode (ModestHildon2Window *self,
379 ModestHildon2WindowPrivate *priv = NULL;
380 EditModeRegister *reg;
381 GtkTreeSelection *selection;
383 g_return_if_fail (MODEST_IS_HILDON2_WINDOW (self));
384 g_return_if_fail (edit_mode_id >= 0);
386 priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
387 reg = (EditModeRegister *) g_hash_table_lookup (priv->edit_mode_registry, GINT_TO_POINTER (edit_mode_id));
388 g_return_if_fail (reg != NULL);
390 if (priv->edit_mode) {
391 modest_hildon2_window_unset_edit_mode (self);
394 priv->edit_mode = TRUE;
395 priv->edit_command = edit_mode_id;
397 priv->current_edit_tree_view = reg->tree_view;
398 g_object_set (G_OBJECT (priv->current_edit_tree_view),
399 "hildon-ui-mode", HILDON_UI_MODE_EDIT,
402 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->current_edit_tree_view));
403 gtk_tree_selection_set_mode (selection, reg->mode);
404 if (reg->mode == GTK_SELECTION_SINGLE || reg->mode == GTK_SELECTION_BROWSE) {
408 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->current_edit_tree_view));
409 if (gtk_tree_model_get_iter_first (model, &iter)) {
410 gtk_tree_view_scroll_to_point (GTK_TREE_VIEW (priv->current_edit_tree_view), 0, 0);
411 gtk_tree_selection_select_iter (selection, &iter);
414 gtk_tree_selection_unselect_all (selection);
417 priv->edit_toolbar = hildon_edit_toolbar_new ();
418 hildon_edit_toolbar_set_label (HILDON_EDIT_TOOLBAR (priv->edit_toolbar),
420 hildon_edit_toolbar_set_button_label (HILDON_EDIT_TOOLBAR (priv->edit_toolbar),
422 modest_hildon2_window_pack_toolbar (self, GTK_PACK_START,
425 g_signal_connect (G_OBJECT (priv->edit_toolbar), "button-clicked",
426 G_CALLBACK (edit_toolbar_button_clicked), (gpointer) self);
427 g_signal_connect (G_OBJECT (priv->edit_toolbar), "arrow-clicked",
428 G_CALLBACK (edit_toolbar_arrow_clicked), (gpointer) self);
430 gtk_widget_show (priv->edit_toolbar);
431 gtk_widget_queue_resize (priv->current_edit_tree_view);
432 gtk_window_fullscreen (GTK_WINDOW (self));
434 g_signal_emit (G_OBJECT (self), signals[EDIT_MODE_CHANGED_SIGNAL], 0,
435 priv->edit_command, priv->edit_mode);
439 modest_hildon2_window_unset_edit_mode (ModestHildon2Window *self)
441 ModestHildon2WindowPrivate *priv = NULL;
443 g_return_if_fail (MODEST_IS_HILDON2_WINDOW (self));
444 priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
446 if (priv->edit_toolbar) {
447 gtk_widget_destroy (priv->edit_toolbar);
448 priv->edit_toolbar = NULL;
451 if (priv->edit_mode) {
452 priv->edit_mode = FALSE;
453 if (priv->current_edit_tree_view) {
454 g_object_set (G_OBJECT (priv->current_edit_tree_view),
455 "hildon-ui-mode", HILDON_UI_MODE_NORMAL,
457 gtk_widget_queue_resize (priv->current_edit_tree_view);
458 priv->current_edit_tree_view = NULL;
460 gtk_window_unfullscreen (GTK_WINDOW (self));
461 g_signal_emit (G_OBJECT (self), signals[EDIT_MODE_CHANGED_SIGNAL], 0,
462 priv->edit_command, priv->edit_mode);
463 priv->edit_command = MODEST_HILDON2_WINDOW_EDIT_MODE_NONE;
467 static EditModeRegister *
468 edit_mode_register_new (const gchar *description,
469 const gchar *button_label,
470 GtkTreeView *tree_view,
471 GtkSelectionMode mode,
472 ModestHildon2EditModeCallback action)
474 EditModeRegister *reg;
476 reg = g_slice_new (EditModeRegister);
478 reg->description = g_strdup (description);
479 reg->button_label = g_strdup (button_label);
480 reg->tree_view = g_object_ref (tree_view);
482 reg->action = action;
488 edit_mode_register_destroy (gpointer data)
490 EditModeRegister *reg = (EditModeRegister *) data;
492 g_free (reg->description);
493 g_free (reg->button_label);
494 g_object_unref (reg->tree_view);
496 g_slice_free (EditModeRegister, reg);
500 edit_toolbar_button_clicked (HildonEditToolbar *toolbar,
501 ModestHildon2Window *self)
503 ModestHildon2WindowPrivate *priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
504 EditModeRegister *reg;
506 g_return_if_fail (MODEST_IS_HILDON2_WINDOW (self));
508 reg = (EditModeRegister *) g_hash_table_lookup (priv->edit_mode_registry,
509 GINT_TO_POINTER (priv->edit_command));
512 if ((reg->action == NULL) || reg->action (self))
513 modest_hildon2_window_unset_edit_mode (self);
515 modest_hildon2_window_unset_edit_mode (self);
520 edit_toolbar_arrow_clicked (HildonEditToolbar *toolbar,
521 ModestHildon2Window *self)
523 g_return_if_fail (MODEST_IS_HILDON2_WINDOW (self));
525 modest_hildon2_window_unset_edit_mode (self);