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_debug ("%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 modest_ui_dimming_manager_set_widget_dimming_mode (GTK_WIDGET (button),
260 MODEST_UI_DIMMING_MODE_HIDE);
262 if (dimming_callback)
263 modest_dimming_rules_group_add_widget_rule (priv->app_menu_dimming_group,
265 (GCallback) dimming_callback,
266 MODEST_WINDOW (self));
267 hildon_app_menu_append (HILDON_APP_MENU (priv->app_menu), GTK_BUTTON (button));
268 gtk_widget_show (GTK_WIDGET (button));
272 modest_hildon2_window_add_to_menu (ModestHildon2Window *self,
274 const gchar *accelerator,
275 ModestHildon2AppMenuCallback callback,
276 ModestDimmingCallback dimming_callback)
278 ModestHildon2WindowPrivate *priv = NULL;
281 g_return_if_fail (MODEST_IS_HILDON2_WINDOW(self));
282 g_return_if_fail (label && label[0] != '\0');
283 g_return_if_fail (callback != NULL);
285 priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
287 button = gtk_button_new_with_label (label);
288 g_signal_connect_after (G_OBJECT (button), "clicked",
289 G_CALLBACK (callback), (gpointer) self);
291 if (accelerator != NULL) {
293 GdkModifierType accel_mods;
295 gtk_accelerator_parse (accelerator, &accel_key, &accel_mods);
296 gtk_widget_add_accelerator (button, "clicked", priv->accel_group,
297 accel_key, accel_mods, 0);
300 modest_hildon2_window_add_button_to_menu (self, GTK_BUTTON (button), dimming_callback);
303 static void setup_menu (ModestHildon2Window *self)
305 ModestHildon2WindowPrivate *priv = NULL;
307 g_return_if_fail (MODEST_IS_HILDON2_WINDOW(self));
309 priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
311 priv->app_menu = hildon_app_menu_new ();
313 /* we expect that the app menu is filled in children using the expected
314 * add_to_menu methods */
316 hildon_stackable_window_set_main_menu (HILDON_STACKABLE_WINDOW (self),
317 HILDON_APP_MENU (priv->app_menu));
321 modest_hildon2_window_toggle_menu (HildonWindow *window,
325 ModestHildon2WindowPrivate *priv = NULL;
327 priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (window);
329 modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (window));
331 gtk_widget_queue_resize (priv->app_menu);
333 return HILDON_WINDOW_CLASS (parent_class)->toggle_menu (window, button, time);
337 modest_hildon2_window_show_toolbar (ModestWindow *self,
338 gboolean show_toolbar)
340 /* Empty implementation: Hildon 2.2 implementation
341 * doesn't switch toolbar visibility */
345 modest_hildon2_window_register_edit_mode (ModestHildon2Window *self,
347 const gchar *description,
348 const gchar *button_label,
349 GtkTreeView *tree_view,
350 GtkSelectionMode mode,
351 ModestHildon2EditModeCallback action)
353 ModestHildon2WindowPrivate *priv = NULL;
354 EditModeRegister *reg;
356 g_return_if_fail (MODEST_IS_HILDON2_WINDOW (self));
357 g_return_if_fail (edit_mode_id >= 0);
358 g_return_if_fail (description);
359 g_return_if_fail (button_label);
360 g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));
362 priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
364 reg = (EditModeRegister *) g_hash_table_lookup (priv->edit_mode_registry, GINT_TO_POINTER (edit_mode_id));
365 g_return_if_fail (reg == NULL);
367 reg = edit_mode_register_new (description, button_label, tree_view, mode, action);
368 g_hash_table_insert (priv->edit_mode_registry, GINT_TO_POINTER (edit_mode_id), (gpointer) reg);
372 modest_hildon2_window_set_edit_mode (ModestHildon2Window *self,
375 ModestHildon2WindowPrivate *priv = NULL;
376 EditModeRegister *reg;
377 GtkTreeSelection *selection;
379 g_return_if_fail (MODEST_IS_HILDON2_WINDOW (self));
380 g_return_if_fail (edit_mode_id >= 0);
382 priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
383 reg = (EditModeRegister *) g_hash_table_lookup (priv->edit_mode_registry, GINT_TO_POINTER (edit_mode_id));
384 g_return_if_fail (reg != NULL);
386 if (priv->edit_mode) {
387 modest_hildon2_window_unset_edit_mode (self);
390 priv->edit_mode = TRUE;
391 priv->edit_command = edit_mode_id;
393 priv->current_edit_tree_view = reg->tree_view;
394 g_object_set (G_OBJECT (priv->current_edit_tree_view),
395 "hildon-ui-mode", HILDON_UI_MODE_EDIT,
398 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->current_edit_tree_view));
399 gtk_tree_selection_set_mode (selection, reg->mode);
400 if (reg->mode == GTK_SELECTION_SINGLE || reg->mode == GTK_SELECTION_BROWSE) {
404 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->current_edit_tree_view));
405 if (gtk_tree_model_get_iter_first (model, &iter)) {
406 gtk_tree_view_scroll_to_point (GTK_TREE_VIEW (priv->current_edit_tree_view), 0, 0);
407 gtk_tree_selection_select_iter (selection, &iter);
410 gtk_tree_selection_unselect_all (selection);
413 priv->edit_toolbar = hildon_edit_toolbar_new ();
414 hildon_edit_toolbar_set_label (HILDON_EDIT_TOOLBAR (priv->edit_toolbar),
416 hildon_edit_toolbar_set_button_label (HILDON_EDIT_TOOLBAR (priv->edit_toolbar),
418 modest_hildon2_window_pack_toolbar (self, GTK_PACK_START,
421 g_signal_connect (G_OBJECT (priv->edit_toolbar), "button-clicked",
422 G_CALLBACK (edit_toolbar_button_clicked), (gpointer) self);
423 g_signal_connect (G_OBJECT (priv->edit_toolbar), "arrow-clicked",
424 G_CALLBACK (edit_toolbar_arrow_clicked), (gpointer) self);
426 gtk_widget_show (priv->edit_toolbar);
427 gtk_widget_queue_resize (priv->current_edit_tree_view);
428 gtk_window_fullscreen (GTK_WINDOW (self));
430 g_signal_emit (G_OBJECT (self), signals[EDIT_MODE_CHANGED_SIGNAL], 0,
431 priv->edit_command, priv->edit_mode);
435 modest_hildon2_window_unset_edit_mode (ModestHildon2Window *self)
437 ModestHildon2WindowPrivate *priv = NULL;
439 g_return_if_fail (MODEST_IS_HILDON2_WINDOW (self));
440 priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
442 if (priv->edit_toolbar) {
443 gtk_widget_destroy (priv->edit_toolbar);
444 priv->edit_toolbar = NULL;
447 if (priv->edit_mode) {
448 priv->edit_mode = FALSE;
449 if (priv->current_edit_tree_view) {
450 g_object_set (G_OBJECT (priv->current_edit_tree_view),
451 "hildon-ui-mode", HILDON_UI_MODE_NORMAL,
453 gtk_widget_queue_resize (priv->current_edit_tree_view);
454 priv->current_edit_tree_view = NULL;
456 gtk_window_unfullscreen (GTK_WINDOW (self));
457 g_signal_emit (G_OBJECT (self), signals[EDIT_MODE_CHANGED_SIGNAL], 0,
458 priv->edit_command, priv->edit_mode);
459 priv->edit_command = MODEST_HILDON2_WINDOW_EDIT_MODE_NONE;
463 static EditModeRegister *
464 edit_mode_register_new (const gchar *description,
465 const gchar *button_label,
466 GtkTreeView *tree_view,
467 GtkSelectionMode mode,
468 ModestHildon2EditModeCallback action)
470 EditModeRegister *reg;
472 reg = g_slice_new (EditModeRegister);
474 reg->description = g_strdup (description);
475 reg->button_label = g_strdup (button_label);
476 reg->tree_view = g_object_ref (tree_view);
478 reg->action = action;
484 edit_mode_register_destroy (gpointer data)
486 EditModeRegister *reg = (EditModeRegister *) data;
488 g_free (reg->description);
489 g_free (reg->button_label);
490 g_object_unref (reg->tree_view);
492 g_slice_free (EditModeRegister, reg);
496 edit_toolbar_button_clicked (HildonEditToolbar *toolbar,
497 ModestHildon2Window *self)
499 ModestHildon2WindowPrivate *priv = MODEST_HILDON2_WINDOW_GET_PRIVATE (self);
500 EditModeRegister *reg;
502 g_return_if_fail (MODEST_IS_HILDON2_WINDOW (self));
504 reg = (EditModeRegister *) g_hash_table_lookup (priv->edit_mode_registry,
505 GINT_TO_POINTER (priv->edit_command));
508 if ((reg->action == NULL) || reg->action (self))
509 modest_hildon2_window_unset_edit_mode (self);
511 modest_hildon2_window_unset_edit_mode (self);
516 edit_toolbar_arrow_clicked (HildonEditToolbar *toolbar,
517 ModestHildon2Window *self)
519 g_return_if_fail (MODEST_IS_HILDON2_WINDOW (self));
521 modest_hildon2_window_unset_edit_mode (self);