1 /* Copyright (c) 2009, 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.
31 #include <modest-shell.h>
32 #include <modest-shell-window.h>
33 #include <modest-icon-names.h>
34 #include <modest-ui-actions.h>
36 /* 'private'/'protected' functions */
37 static void modest_shell_class_init (ModestShellClass *klass);
38 static void modest_shell_instance_init (ModestShell *obj);
39 static void modest_shell_finalize (GObject *obj);
41 static void update_title (ModestShell *self);
43 static void on_back_button_clicked (GtkToolButton *button, ModestShell *self);
44 static void on_title_button_clicked (GtkToolButton *button, ModestShell *self);
45 static void on_new_msg_button_clicked (GtkToolButton *button, ModestShell *self);
46 static void on_style_set (GtkWidget *widget, GtkStyle *old_style, ModestShell *shell);
49 typedef struct _ModestShellPrivate ModestShellPrivate;
50 struct _ModestShellPrivate {
53 GtkWidget *top_toolbar;
54 GtkToolItem *new_message_button;
55 GtkToolItem *back_button;
56 GtkToolItem *title_button;
57 GtkWidget *title_label;
58 GtkWidget *subtitle_label;
60 GtkWidget *progress_icon;
61 GdkPixbuf **progress_frames;
63 guint progress_timeout_id;
65 #define MODEST_SHELL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
69 static GObjectClass *parent_class = NULL;
72 modest_shell_get_type (void)
74 static GType my_type = 0;
76 static const GTypeInfo my_info = {
77 sizeof(ModestShellClass),
79 NULL, /* base finalize */
80 (GClassInitFunc) modest_shell_class_init,
81 NULL, /* class finalize */
82 NULL, /* class data */
85 (GInstanceInitFunc) modest_shell_instance_init,
88 my_type = g_type_register_static (GTK_TYPE_WINDOW,
96 modest_shell_class_init (ModestShellClass *klass)
98 GObjectClass *gobject_class;
100 gobject_class = (GObjectClass*) klass;
102 parent_class = g_type_class_peek_parent (klass);
103 gobject_class->finalize = modest_shell_finalize;
105 g_type_class_add_private (gobject_class, sizeof(ModestShellPrivate));
110 modest_shell_instance_init (ModestShell *obj)
112 ModestShellPrivate *priv;
113 GtkWidget *title_vbox;
114 GtkWidget *title_arrow;
115 GtkWidget *new_message_icon;
116 GtkToolItem *separator_toolitem;
119 priv = MODEST_SHELL_GET_PRIVATE(obj);
120 priv->progress_frames = g_malloc0 (sizeof(GdkPixbuf *)*31);
121 priv->progress_timeout_id = 0;
122 priv->next_frame = 0;
124 priv->main_vbox = gtk_vbox_new (FALSE, 0);
125 gtk_widget_show (priv->main_vbox);
127 top_hbox = gtk_hbox_new (FALSE, 0);
128 gtk_widget_show (top_hbox);
129 gtk_box_pack_start (GTK_BOX (priv->main_vbox), top_hbox, FALSE, FALSE, 0);
131 priv->top_toolbar = gtk_toolbar_new ();
132 gtk_toolbar_set_style (GTK_TOOLBAR (priv->top_toolbar), GTK_TOOLBAR_BOTH_HORIZ);
133 gtk_toolbar_set_show_arrow (GTK_TOOLBAR (priv->top_toolbar), FALSE);
134 gtk_widget_show (priv->top_toolbar);
135 gtk_box_pack_start (GTK_BOX (top_hbox), priv->top_toolbar, TRUE, TRUE, 0);
137 priv->progress_icon = gtk_image_new ();
138 gtk_widget_show (priv->progress_icon);
139 gtk_box_pack_start (GTK_BOX (top_hbox), priv->progress_icon, FALSE, FALSE, 0);
141 new_message_icon = gtk_image_new_from_icon_name (MODEST_TOOLBAR_ICON_NEW_MAIL, GTK_ICON_SIZE_LARGE_TOOLBAR);
142 gtk_widget_show (new_message_icon);
143 priv->new_message_button = gtk_tool_button_new (new_message_icon, _("mcen_va_new_email"));
144 g_object_set (priv->new_message_button, "is-important", TRUE, NULL);
145 gtk_toolbar_insert (GTK_TOOLBAR (priv->top_toolbar), priv->new_message_button, -1);
146 gtk_widget_show (GTK_WIDGET (priv->new_message_button));
147 g_signal_connect (G_OBJECT (priv->new_message_button), "clicked", G_CALLBACK (on_new_msg_button_clicked), obj);
149 priv->back_button = gtk_tool_button_new_from_stock (GTK_STOCK_GO_BACK);
150 g_object_set (priv->back_button, "is-important", TRUE, NULL);
151 gtk_toolbar_insert (GTK_TOOLBAR (priv->top_toolbar), priv->back_button, -1);
152 gtk_widget_show (GTK_WIDGET (priv->back_button));
153 g_signal_connect (G_OBJECT (priv->back_button), "clicked", G_CALLBACK (on_back_button_clicked), obj);
155 separator_toolitem = gtk_separator_tool_item_new ();
156 gtk_toolbar_insert (GTK_TOOLBAR (priv->top_toolbar), separator_toolitem, -1);
157 gtk_widget_show (GTK_WIDGET (separator_toolitem));
159 title_vbox = gtk_vbox_new (FALSE, 0);
160 priv->title_label = gtk_label_new (NULL);
161 gtk_label_set_ellipsize (GTK_LABEL (priv->title_label), PANGO_ELLIPSIZE_END);
162 gtk_misc_set_alignment (GTK_MISC (priv->title_label), 0.0, 1.0);
163 priv->subtitle_label = gtk_label_new (NULL);
164 gtk_label_set_ellipsize (GTK_LABEL (priv->subtitle_label), PANGO_ELLIPSIZE_START);
165 gtk_misc_set_alignment (GTK_MISC (priv->subtitle_label), 0.0, 0.0);
166 gtk_widget_show (priv->title_label);
167 gtk_widget_show (priv->subtitle_label);
168 gtk_box_pack_start (GTK_BOX (title_vbox), priv->title_label, TRUE, TRUE, 0);
169 gtk_box_pack_start (GTK_BOX (title_vbox), priv->subtitle_label, FALSE, FALSE, 0);
170 gtk_widget_show (title_vbox);
172 priv->title_button = gtk_tool_button_new (NULL, NULL);
173 gtk_widget_show (GTK_WIDGET (priv->title_button));
174 title_arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
175 gtk_widget_show (title_arrow);
176 gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (priv->title_button), title_arrow);
177 gtk_tool_button_set_label_widget (GTK_TOOL_BUTTON (priv->title_button), title_vbox);
178 gtk_toolbar_insert (GTK_TOOLBAR (priv->top_toolbar), priv->title_button, -1);
179 gtk_container_child_set (GTK_CONTAINER (priv->top_toolbar), GTK_WIDGET (priv->title_button), "expand", TRUE, NULL);
180 g_object_set (priv->title_button, "is-important", TRUE, NULL);
181 g_signal_connect (G_OBJECT (priv->title_button), "clicked", G_CALLBACK (on_title_button_clicked), obj);
183 priv->notebook = gtk_notebook_new ();
184 gtk_notebook_set_show_tabs ((GtkNotebook *)priv->notebook, FALSE);
185 gtk_notebook_set_show_border ((GtkNotebook *)priv->notebook, FALSE);
186 gtk_widget_show (priv->notebook);
187 gtk_box_pack_start (GTK_BOX (priv->main_vbox), priv->notebook, TRUE, TRUE, 0);
188 gtk_container_add (GTK_CONTAINER (obj), priv->main_vbox);
190 g_signal_connect (G_OBJECT (obj), "style-set", G_CALLBACK (on_style_set), obj);
193 GdkModifierType accel_mods;
194 GtkAccelGroup *accel_group;
195 accel_group = gtk_accel_group_new ();
196 gtk_accelerator_parse ("<Control>n", &accel_key, &accel_mods);
197 gtk_widget_add_accelerator (GTK_WIDGET (priv->new_message_button), "clicked", accel_group,
198 accel_key, accel_mods, 0);
199 gtk_accelerator_parse ("Esc", &accel_key, &accel_mods);
200 gtk_widget_add_accelerator (GTK_WIDGET (priv->back_button), "clicked", accel_group,
201 accel_key, accel_mods, 0);
202 gtk_accelerator_parse ("F10", &accel_key, &accel_mods);
203 gtk_widget_add_accelerator (GTK_WIDGET (priv->title_button), "clicked", accel_group,
204 accel_key, accel_mods, 0);
205 gtk_window_add_accel_group (GTK_WINDOW (obj), accel_group);
210 modest_shell_finalize (GObject *obj)
212 ModestShellPrivate *priv;
215 priv = MODEST_SHELL_GET_PRIVATE (obj);
217 if (priv->progress_timeout_id) {
218 g_source_remove (priv->progress_timeout_id);
220 for (n = 0; n < 31; n++) {
221 if (priv->progress_frames[n]) {
222 g_object_unref (priv->progress_frames[n]);
225 g_free (priv->progress_frames);
227 G_OBJECT_CLASS(parent_class)->finalize (obj);
231 modest_shell_new (void)
233 return (GtkWidget *) g_object_new(MODEST_TYPE_SHELL, NULL);
237 modest_shell_peek_window (ModestShell *shell)
239 ModestShellPrivate *priv;
242 priv = MODEST_SHELL_GET_PRIVATE (shell);
243 count = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
246 return (ModestWindow *) gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), count - 1);
253 modest_shell_delete_window (ModestShell *shell, ModestWindow *window)
255 ModestShellPrivate *priv;
258 priv = MODEST_SHELL_GET_PRIVATE (shell);
259 g_signal_emit_by_name (G_OBJECT (window), "delete-event", NULL, &ret_value);
260 if (ret_value == FALSE) {
263 page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook), GTK_WIDGET (window));
264 if (page_num != -1) {
265 gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), page_num);
269 update_title (shell);
275 modest_shell_add_window (ModestShell *shell, ModestWindow *window)
277 ModestShellPrivate *priv;
279 priv = MODEST_SHELL_GET_PRIVATE (shell);
280 gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook), GTK_WIDGET (window), NULL);
281 gtk_widget_show (GTK_WIDGET (window));
282 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), -1);
283 modest_shell_window_set_shell (MODEST_SHELL_WINDOW (window), shell);
284 update_title (shell);
288 modest_shell_count_windows (ModestShell *shell)
290 ModestShellPrivate *priv;
292 priv = MODEST_SHELL_GET_PRIVATE (shell);
294 return gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
298 modest_shell_set_title (ModestShell *shell, ModestWindow *window, const gchar *title)
300 ModestShellPrivate *priv;
302 priv = MODEST_SHELL_GET_PRIVATE (shell);
304 gtk_notebook_set_tab_label_text (GTK_NOTEBOOK (priv->notebook), GTK_WIDGET (window), title);
306 update_title (shell);
310 show_next_frame (ModestShell *shell)
312 ModestShellPrivate *priv;
314 priv = MODEST_SHELL_GET_PRIVATE (shell);
316 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->progress_icon), priv->progress_frames[priv->next_frame]);
319 if (priv->next_frame >= 31)
320 priv->next_frame = 0;
324 on_progress_timeout (ModestShell *shell)
326 show_next_frame (shell);
331 modest_shell_show_progress (ModestShell *shell, ModestWindow *window, gboolean show)
333 ModestShellPrivate *priv;
335 priv = MODEST_SHELL_GET_PRIVATE (shell);
338 if (priv->progress_timeout_id == 0) {
339 priv->progress_timeout_id = g_timeout_add (100, (GSourceFunc) on_progress_timeout, shell);
340 show_next_frame (shell);
342 gtk_widget_show (priv->progress_icon);
344 if (priv->progress_timeout_id) {
345 g_source_remove (priv->progress_timeout_id);
346 priv->progress_timeout_id = 0;
348 gtk_widget_hide (priv->progress_icon);
353 update_title (ModestShell *self)
356 ModestShellPrivate *priv;
358 GString *title_buffer;
359 GString *subtitle_buffer;
360 const gchar *tab_label_text;
362 priv = MODEST_SHELL_GET_PRIVATE (self);
364 n_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
366 gtk_label_set_text (GTK_LABEL (priv->title_label), "");
367 gtk_label_set_text (GTK_LABEL (priv->subtitle_label), "");
371 child = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), n_pages - 1);
372 title_buffer = g_string_new ("");
373 title_buffer = g_string_append (title_buffer, "<b>");
374 tab_label_text = gtk_notebook_get_tab_label_text (GTK_NOTEBOOK (priv->notebook), child);
376 title_buffer = g_string_append (title_buffer, tab_label_text);
377 title_buffer = g_string_append (title_buffer, "</b>");
378 gtk_label_set_markup (GTK_LABEL (priv->title_label),
380 g_string_free (title_buffer, TRUE);
382 subtitle_buffer = g_string_new ("");
383 subtitle_buffer = g_string_append (subtitle_buffer, "<small>");
384 for (i = 0; i < n_pages - 1; i++) {
385 child = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), i);
387 subtitle_buffer = g_string_append (subtitle_buffer, " / ");
389 subtitle_buffer = g_string_append (subtitle_buffer,
390 gtk_notebook_get_tab_label_text (GTK_NOTEBOOK (priv->notebook), child));
392 subtitle_buffer = g_string_append (subtitle_buffer, "</small>");
393 gtk_label_set_markup (GTK_LABEL (priv->subtitle_label),
394 subtitle_buffer->str);
395 g_string_free (subtitle_buffer, TRUE);
399 on_back_button_clicked (GtkToolButton *button, ModestShell *self)
401 ModestShellPrivate *priv;
403 gboolean delete_event_retval;
406 priv = MODEST_SHELL_GET_PRIVATE (self);
408 n_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
412 child = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), -1);
413 g_signal_emit_by_name (G_OBJECT (child), "delete-event", NULL, &delete_event_retval);
415 if (!delete_event_retval) {
421 menu_position_cb (GtkMenu *menu,
427 ModestShellPrivate *priv;
428 GtkAllocation *alloc;
429 GdkWindow *parent_window;
432 priv = MODEST_SHELL_GET_PRIVATE (self);
434 alloc = &(GTK_WIDGET (priv->title_button)->allocation);
435 parent_window = gtk_widget_get_parent_window (GTK_WIDGET (priv->title_button));
436 gdk_window_get_position (parent_window, &pos_x, &pos_y);
437 *x = pos_x + alloc->x;
438 *y = pos_y + alloc->y + alloc->height;
444 on_title_button_clicked (GtkToolButton *button, ModestShell *self)
446 ModestShellPrivate *priv;
451 priv = MODEST_SHELL_GET_PRIVATE (self);
453 n_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
457 child = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), -1);
458 menu = modest_shell_window_get_menu (MODEST_SHELL_WINDOW (child));
461 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
462 (GtkMenuPositionFunc) menu_position_cb, (gpointer) self,
463 1, gtk_get_current_event_time ());
468 on_new_msg_button_clicked (GtkToolButton *button, ModestShell *self)
470 ModestShellPrivate *priv;
474 priv = MODEST_SHELL_GET_PRIVATE (self);
476 n_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
480 child = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), -1);
482 modest_ui_actions_on_new_msg (NULL, MODEST_WINDOW (child));
486 on_style_set (GtkWidget *widget,
490 ModestShellPrivate *priv;
492 GdkPixbuf *progress_pixbuf;
495 priv = MODEST_SHELL_GET_PRIVATE (self);
497 if (!gtk_icon_size_lookup (GTK_ICON_SIZE_LARGE_TOOLBAR, &icon_w, &icon_h))
499 progress_pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (), "process-working", icon_w, 0, NULL);
501 for (n = 0; n < 31; n++) {
502 if (priv->progress_frames[n] != NULL) {
503 g_object_unref (priv->progress_frames[n]);
505 priv->progress_frames[n] = NULL;
508 if (progress_pixbuf) {
512 icon_w = gdk_pixbuf_get_width (progress_pixbuf) / 8;
517 for (i = 0; i < 4; i++) {
518 for (j = 0; j < 8; j++) {
521 if ((i == 0) && (j == 0))
523 frame = gdk_pixbuf_new_subpixbuf (progress_pixbuf,
526 priv->progress_frames[n] = frame;
530 g_object_unref (progress_pixbuf);