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_finalize (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->finalize = modest_hildon2_window_finalize;
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_finalize (GObject *obj)
170 ModestHildon2WindowPrivate *priv;
172 priv = MODEST_HILDON2_WINDOW_GET_PRIVATE(obj);
174 g_object_unref (priv->app_menu_dimming_group);
175 priv->app_menu_dimming_group = NULL;
177 g_hash_table_destroy (priv->edit_mode_registry);
178 priv->edit_mode_registry = NULL;
180 G_OBJECT_CLASS(parent_class)->finalize (obj);
184 modest_hildon2_window_instance_init (GTypeInstance *instance, gpointer g_class)
186 ModestHildon2Window *self = NULL;
187 ModestWindowPrivate *parent_priv = NULL;
188 ModestHildon2WindowPrivate *priv = NULL;
190 self = (ModestHildon2Window *) instance;
191 parent_priv = MODEST_WINDOW_GET_PRIVATE (self);
192 priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
194 priv->accel_group = gtk_accel_group_new ();
196 priv->edit_mode = FALSE;
197 priv->edit_toolbar = NULL;
198 priv->current_edit_tree_view = NULL;
199 priv->edit_command = MODEST_HILDON2_WINDOW_EDIT_MODE_NONE;
200 priv->edit_mode_registry = g_hash_table_new_full (g_direct_hash, g_direct_equal,
201 NULL, edit_mode_register_destroy);
203 parent_priv->ui_dimming_manager = modest_ui_dimming_manager_new();
204 priv->app_menu_dimming_group = modest_dimming_rules_group_new (MODEST_DIMMING_RULES_MENU, FALSE);
205 gtk_window_add_accel_group (GTK_WINDOW (self), priv->accel_group);
209 modest_ui_dimming_manager_insert_rules_group (parent_priv->ui_dimming_manager,
210 priv->app_menu_dimming_group);
212 /* Dont't restore settings here,
213 * because it requires a gtk_widget_show(),
214 * and we don't want to do that until later,
215 * so that the UI is not visible for non-menu D-Bus activation.
220 on_zoom_minus_plus_not_implemented (ModestWindow *window)
222 g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW (window), FALSE);
224 hildon_banner_show_information (NULL, NULL, _CS("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));
265 gtk_widget_show (GTK_WIDGET (button));
269 modest_hildon2_window_add_to_menu (ModestHildon2Window *self,
271 const gchar *accelerator,
272 ModestHildon2AppMenuCallback callback,
273 ModestDimmingCallback dimming_callback)
275 ModestHildon2WindowPrivate *priv = NULL;
278 g_return_if_fail (MODEST_IS_HILDON2_WINDOW(self));
279 g_return_if_fail (label && label[0] != '\0');
280 g_return_if_fail (callback != NULL);
282 priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
284 button = gtk_button_new_with_label (label);
285 g_signal_connect_after (G_OBJECT (button), "clicked",
286 G_CALLBACK (callback), (gpointer) self);
288 if (accelerator != NULL) {
290 GdkModifierType accel_mods;
292 gtk_accelerator_parse (accelerator, &accel_key, &accel_mods);
293 gtk_widget_add_accelerator (button, "clicked", priv->accel_group,
294 accel_key, accel_mods, 0);
297 modest_hildon2_window_add_button_to_menu (self, GTK_BUTTON (button), dimming_callback);
300 static void setup_menu (ModestHildon2Window *self)
302 ModestHildon2WindowPrivate *priv = NULL;
304 g_return_if_fail (MODEST_IS_HILDON2_WINDOW(self));
306 priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
308 priv->app_menu = hildon_app_menu_new ();
310 /* we expect that the app menu is filled in children using the expected
311 * add_to_menu methods */
313 hildon_stackable_window_set_main_menu (HILDON_STACKABLE_WINDOW (self),
314 HILDON_APP_MENU (priv->app_menu));
318 modest_hildon2_window_toggle_menu (HildonWindow *window,
322 ModestHildon2WindowPrivate *priv = NULL;
324 priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (window);
326 modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (window));
328 gtk_widget_queue_resize (priv->app_menu);
330 return HILDON_WINDOW_CLASS (parent_class)->toggle_menu (window, button, time);
334 modest_hildon2_window_show_toolbar (ModestWindow *self,
335 gboolean show_toolbar)
337 /* Empty implementation: Hildon 2.2 implementation
338 * doesn't switch toolbar visibility */
342 modest_hildon2_window_register_edit_mode (ModestHildon2Window *self,
344 const gchar *description,
345 const gchar *button_label,
346 GtkTreeView *tree_view,
347 GtkSelectionMode mode,
348 ModestHildon2EditModeCallback action)
350 ModestHildon2WindowPrivate *priv = NULL;
351 EditModeRegister *reg;
353 g_return_if_fail (MODEST_IS_HILDON2_WINDOW (self));
354 g_return_if_fail (edit_mode_id >= 0);
355 g_return_if_fail (description);
356 g_return_if_fail (button_label);
357 g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));
359 priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
361 reg = (EditModeRegister *) g_hash_table_lookup (priv->edit_mode_registry, GINT_TO_POINTER (edit_mode_id));
362 g_return_if_fail (reg == NULL);
364 reg = edit_mode_register_new (description, button_label, tree_view, mode, action);
365 g_hash_table_insert (priv->edit_mode_registry, GINT_TO_POINTER (edit_mode_id), (gpointer) reg);
369 modest_hildon2_window_set_edit_mode (ModestHildon2Window *self,
372 ModestHildon2WindowPrivate *priv = NULL;
373 EditModeRegister *reg;
374 GtkTreeSelection *selection;
376 g_return_if_fail (MODEST_IS_HILDON2_WINDOW (self));
377 g_return_if_fail (edit_mode_id >= 0);
379 priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
380 reg = (EditModeRegister *) g_hash_table_lookup (priv->edit_mode_registry, GINT_TO_POINTER (edit_mode_id));
381 g_return_if_fail (reg != NULL);
383 if (priv->edit_mode) {
384 modest_hildon2_window_unset_edit_mode (self);
387 priv->edit_mode = TRUE;
388 priv->edit_command = edit_mode_id;
390 priv->current_edit_tree_view = reg->tree_view;
391 g_object_set (G_OBJECT (priv->current_edit_tree_view),
392 "hildon-ui-mode", HILDON_UI_MODE_EDIT,
395 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->current_edit_tree_view));
396 gtk_tree_selection_set_mode (selection, reg->mode);
397 if (reg->mode == GTK_SELECTION_SINGLE || reg->mode == GTK_SELECTION_BROWSE) {
401 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->current_edit_tree_view));
402 if (gtk_tree_model_get_iter_first (model, &iter)) {
403 gtk_tree_view_scroll_to_point (GTK_TREE_VIEW (priv->current_edit_tree_view), 0, 0);
404 gtk_tree_selection_select_iter (selection, &iter);
407 gtk_tree_selection_unselect_all (selection);
410 priv->edit_toolbar = hildon_edit_toolbar_new ();
411 hildon_edit_toolbar_set_label (HILDON_EDIT_TOOLBAR (priv->edit_toolbar),
413 hildon_edit_toolbar_set_button_label (HILDON_EDIT_TOOLBAR (priv->edit_toolbar),
415 modest_hildon2_window_pack_toolbar (self, GTK_PACK_START,
418 g_signal_connect (G_OBJECT (priv->edit_toolbar), "button-clicked",
419 G_CALLBACK (edit_toolbar_button_clicked), (gpointer) self);
420 g_signal_connect (G_OBJECT (priv->edit_toolbar), "arrow-clicked",
421 G_CALLBACK (edit_toolbar_arrow_clicked), (gpointer) self);
423 gtk_widget_show (priv->edit_toolbar);
424 gtk_widget_queue_resize (priv->current_edit_tree_view);
425 gtk_window_fullscreen (GTK_WINDOW (self));
427 g_signal_emit (G_OBJECT (self), signals[EDIT_MODE_CHANGED_SIGNAL], 0,
428 priv->edit_command, priv->edit_mode);
432 modest_hildon2_window_unset_edit_mode (ModestHildon2Window *self)
434 ModestHildon2WindowPrivate *priv = NULL;
436 g_return_if_fail (MODEST_IS_HILDON2_WINDOW (self));
437 priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
439 if (priv->edit_toolbar) {
440 gtk_widget_destroy (priv->edit_toolbar);
441 priv->edit_toolbar = NULL;
444 if (priv->edit_mode) {
445 priv->edit_mode = FALSE;
446 if (priv->current_edit_tree_view) {
447 g_object_set (G_OBJECT (priv->current_edit_tree_view),
448 "hildon-ui-mode", HILDON_UI_MODE_NORMAL,
450 gtk_widget_queue_resize (priv->current_edit_tree_view);
451 priv->current_edit_tree_view = NULL;
453 gtk_window_unfullscreen (GTK_WINDOW (self));
454 g_signal_emit (G_OBJECT (self), signals[EDIT_MODE_CHANGED_SIGNAL], 0,
455 priv->edit_command, priv->edit_mode);
456 priv->edit_command = MODEST_HILDON2_WINDOW_EDIT_MODE_NONE;
460 static EditModeRegister *
461 edit_mode_register_new (const gchar *description,
462 const gchar *button_label,
463 GtkTreeView *tree_view,
464 GtkSelectionMode mode,
465 ModestHildon2EditModeCallback action)
467 EditModeRegister *reg;
469 reg = g_slice_new (EditModeRegister);
471 reg->description = g_strdup (description);
472 reg->button_label = g_strdup (button_label);
473 reg->tree_view = g_object_ref (tree_view);
475 reg->action = action;
481 edit_mode_register_destroy (gpointer data)
483 EditModeRegister *reg = (EditModeRegister *) data;
485 g_free (reg->description);
486 g_free (reg->button_label);
487 g_object_unref (reg->tree_view);
489 g_slice_free (EditModeRegister, reg);
493 edit_toolbar_button_clicked (HildonEditToolbar *toolbar,
494 ModestHildon2Window *self)
496 ModestHildon2WindowPrivate *priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
497 EditModeRegister *reg;
499 g_return_if_fail (MODEST_IS_HILDON2_WINDOW (self));
501 reg = (EditModeRegister *) g_hash_table_lookup (priv->edit_mode_registry,
502 GINT_TO_POINTER (priv->edit_command));
505 if ((reg->action == NULL) || reg->action (self))
506 modest_hildon2_window_unset_edit_mode (self);
508 modest_hildon2_window_unset_edit_mode (self);
513 edit_toolbar_arrow_clicked (HildonEditToolbar *toolbar,
514 ModestHildon2Window *self)
516 g_return_if_fail (MODEST_IS_HILDON2_WINDOW (self));
518 modest_hildon2_window_unset_edit_mode (self);