Set default size of ModestShell
[modest] / src / gtk / modest-shell.c
1 /* Copyright (c) 2009, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
16  *
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.
28  */
29
30 #include <string.h>
31 #include <modest-shell.h>
32 #include <modest-shell-window.h>
33 #include <modest-icon-names.h>
34 #include <modest-ui-actions.h>
35
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);
40
41 static void update_title (ModestShell *self);
42
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);
47 static gboolean on_key_pressed (GtkWidget *widget, GdkEventKey *event, ModestShell *shell);
48
49
50 typedef struct _ModestShellPrivate ModestShellPrivate;
51 struct _ModestShellPrivate {
52         GtkWidget *main_vbox;
53         GtkWidget *notebook;
54         GtkWidget *top_toolbar;
55         GtkToolItem *new_message_button;
56         GtkToolItem *back_button;
57         GtkToolItem *title_button;
58         GtkWidget *title_label;
59         GtkWidget *subtitle_label;
60
61         GtkWidget *progress_icon;
62         GdkPixbuf **progress_frames;
63         gint next_frame;
64         guint progress_timeout_id;
65 };
66 #define MODEST_SHELL_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
67                                                                       MODEST_TYPE_SHELL, \
68                                                                       ModestShellPrivate))
69 /* globals */
70 static GObjectClass *parent_class = NULL;
71
72 GType
73 modest_shell_get_type (void)
74 {
75         static GType my_type = 0;
76         if (!my_type) {
77                 static const GTypeInfo my_info = {
78                         sizeof(ModestShellClass),
79                         NULL,           /* base init */
80                         NULL,           /* base finalize */
81                         (GClassInitFunc) modest_shell_class_init,
82                         NULL,           /* class finalize */
83                         NULL,           /* class data */
84                         sizeof(ModestShell),
85                         1,              /* n_preallocs */
86                         (GInstanceInitFunc) modest_shell_instance_init,
87                         NULL
88                 };
89                 my_type = g_type_register_static (GTK_TYPE_WINDOW,
90                                                   "ModestShell",
91                                                   &my_info, 0);
92         }
93         return my_type;
94 }
95
96 static void
97 modest_shell_class_init (ModestShellClass *klass)
98 {
99         GObjectClass *gobject_class;
100
101         gobject_class = (GObjectClass*) klass;
102
103         parent_class            = g_type_class_peek_parent (klass);
104         gobject_class->finalize = modest_shell_finalize;
105
106         g_type_class_add_private (gobject_class, sizeof(ModestShellPrivate));
107
108 }
109
110 static void
111 modest_shell_instance_init (ModestShell *obj)
112 {
113         ModestShellPrivate *priv;
114         GtkWidget *title_vbox;
115         GtkWidget *title_arrow;
116         GtkWidget *new_message_icon;
117         GtkToolItem *separator_toolitem;
118         GtkWidget *top_hbox;
119         GtkWidget *separator;
120
121         priv = MODEST_SHELL_GET_PRIVATE(obj);
122         priv->progress_frames = g_malloc0 (sizeof(GdkPixbuf *)*31);
123         priv->progress_timeout_id = 0;
124         priv->next_frame = 0;
125
126         priv->main_vbox = gtk_vbox_new (FALSE, 0);
127         gtk_widget_show (priv->main_vbox);
128
129         top_hbox = gtk_hbox_new (FALSE, 0);
130         gtk_widget_show (top_hbox);
131         gtk_box_pack_start (GTK_BOX (priv->main_vbox), top_hbox, FALSE, FALSE, 0);
132
133         separator = gtk_hseparator_new ();
134         gtk_widget_show (separator);
135         gtk_box_pack_start (GTK_BOX (priv->main_vbox), separator, FALSE, FALSE, 0);
136
137         priv->top_toolbar = gtk_toolbar_new ();
138         gtk_toolbar_set_style (GTK_TOOLBAR (priv->top_toolbar), GTK_TOOLBAR_BOTH_HORIZ);
139         gtk_toolbar_set_show_arrow (GTK_TOOLBAR (priv->top_toolbar), FALSE);
140         gtk_widget_show (priv->top_toolbar);
141         gtk_box_pack_start (GTK_BOX (top_hbox), priv->top_toolbar, TRUE, TRUE, 0);
142
143         priv->progress_icon = gtk_image_new ();
144         gtk_widget_show (priv->progress_icon);
145         gtk_box_pack_start (GTK_BOX (top_hbox), priv->progress_icon, FALSE, FALSE, 0);
146
147         new_message_icon = gtk_image_new_from_icon_name (MODEST_TOOLBAR_ICON_NEW_MAIL, GTK_ICON_SIZE_LARGE_TOOLBAR);
148         gtk_widget_show (new_message_icon);
149         priv->new_message_button = gtk_tool_button_new (new_message_icon, _("mcen_va_new_email"));
150         g_object_set (priv->new_message_button, "is-important", TRUE, NULL);
151         gtk_toolbar_insert (GTK_TOOLBAR (priv->top_toolbar), priv->new_message_button, -1);
152         gtk_widget_show (GTK_WIDGET (priv->new_message_button));
153         g_signal_connect (G_OBJECT (priv->new_message_button), "clicked", G_CALLBACK (on_new_msg_button_clicked), obj);
154
155         priv->back_button = gtk_tool_button_new_from_stock (GTK_STOCK_GO_BACK);
156         g_object_set (priv->back_button, "is-important", TRUE, NULL);
157         gtk_toolbar_insert (GTK_TOOLBAR (priv->top_toolbar), priv->back_button, -1);
158         gtk_widget_show (GTK_WIDGET (priv->back_button));
159         g_signal_connect (G_OBJECT (priv->back_button), "clicked", G_CALLBACK (on_back_button_clicked), obj);
160
161         separator_toolitem = gtk_separator_tool_item_new ();
162         gtk_toolbar_insert (GTK_TOOLBAR (priv->top_toolbar), separator_toolitem, -1);
163         gtk_widget_show (GTK_WIDGET (separator_toolitem));
164
165         title_vbox = gtk_vbox_new (FALSE, 0);
166         priv->title_label = gtk_label_new (NULL);
167         gtk_label_set_ellipsize (GTK_LABEL (priv->title_label), PANGO_ELLIPSIZE_END);
168         gtk_misc_set_alignment (GTK_MISC (priv->title_label), 0.0, 1.0);
169         priv->subtitle_label = gtk_label_new (NULL);
170         gtk_label_set_ellipsize (GTK_LABEL (priv->subtitle_label), PANGO_ELLIPSIZE_START);
171         gtk_misc_set_alignment (GTK_MISC (priv->subtitle_label), 0.0, 0.0);
172         gtk_widget_show (priv->title_label);
173         gtk_widget_show (priv->subtitle_label);
174         gtk_box_pack_start (GTK_BOX (title_vbox), priv->title_label, TRUE, TRUE, 0);
175         gtk_box_pack_start (GTK_BOX (title_vbox), priv->subtitle_label, FALSE, FALSE, 0);
176         gtk_widget_show (title_vbox);
177
178         priv->title_button = gtk_tool_button_new (NULL, NULL);
179         gtk_widget_show (GTK_WIDGET (priv->title_button));
180         title_arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
181         gtk_widget_show (title_arrow);
182         gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (priv->title_button), title_arrow);
183         gtk_tool_button_set_label_widget (GTK_TOOL_BUTTON (priv->title_button), title_vbox);
184         gtk_toolbar_insert (GTK_TOOLBAR (priv->top_toolbar), priv->title_button, -1);
185         gtk_container_child_set (GTK_CONTAINER (priv->top_toolbar), GTK_WIDGET (priv->title_button), "expand", TRUE, NULL);
186         g_object_set (priv->title_button, "is-important", TRUE, NULL);
187         g_signal_connect (G_OBJECT (priv->title_button), "clicked", G_CALLBACK (on_title_button_clicked), obj);
188
189         priv->notebook = gtk_notebook_new ();
190         gtk_notebook_set_show_tabs ((GtkNotebook *)priv->notebook, FALSE);
191         gtk_notebook_set_show_border ((GtkNotebook *)priv->notebook, FALSE);
192         gtk_widget_show (priv->notebook);
193         gtk_box_pack_start (GTK_BOX (priv->main_vbox), priv->notebook, TRUE, TRUE, 0);
194         gtk_container_add (GTK_CONTAINER (obj), priv->main_vbox);
195
196         g_signal_connect (G_OBJECT (obj), "style-set", G_CALLBACK (on_style_set), obj);
197
198         guint accel_key;
199         GdkModifierType accel_mods;
200         GtkAccelGroup *accel_group;
201         accel_group = gtk_accel_group_new ();
202         gtk_accelerator_parse ("<Control>n", &accel_key, &accel_mods);
203         gtk_widget_add_accelerator (GTK_WIDGET (priv->new_message_button), "clicked", accel_group,
204                                     accel_key, accel_mods, 0);
205         gtk_accelerator_parse ("Esc", &accel_key, &accel_mods);
206         gtk_widget_add_accelerator (GTK_WIDGET (priv->back_button), "clicked", accel_group,
207                                     accel_key, accel_mods, 0);
208         gtk_accelerator_parse ("F10", &accel_key, &accel_mods);
209         gtk_widget_add_accelerator (GTK_WIDGET (priv->title_button), "clicked", accel_group,
210                                     accel_key, accel_mods, 0);
211         gtk_window_add_accel_group (GTK_WINDOW (obj), accel_group);
212
213         g_signal_connect (G_OBJECT (obj), 
214                           "key-press-event", 
215                           G_CALLBACK (on_key_pressed), obj);
216
217         gtk_window_set_default_size (GTK_WINDOW (obj), 
218                                      640, 480);
219
220
221 }
222
223 static void
224 modest_shell_finalize (GObject *obj)
225 {
226         ModestShellPrivate *priv;
227         int n;
228
229         priv = MODEST_SHELL_GET_PRIVATE (obj);
230
231         if (priv->progress_timeout_id) {
232                 g_source_remove (priv->progress_timeout_id);
233         }
234         for (n = 0; n < 31; n++) {
235                 if (priv->progress_frames[n]) {
236                         g_object_unref (priv->progress_frames[n]);
237                 }
238         }
239         g_free (priv->progress_frames);
240
241         G_OBJECT_CLASS(parent_class)->finalize (obj);
242 }
243
244 GtkWidget*
245 modest_shell_new (void)
246 {
247         return (GtkWidget *) g_object_new(MODEST_TYPE_SHELL, NULL);
248 }
249
250 ModestWindow *
251 modest_shell_peek_window (ModestShell *shell)
252 {
253         ModestShellPrivate *priv;
254         gint count;
255
256         priv = MODEST_SHELL_GET_PRIVATE (shell);
257         count = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
258
259         if (count > 0) {
260                 return (ModestWindow *) gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), count - 1);
261         } else {
262                 return NULL;
263         }
264 }
265
266 gboolean
267 modest_shell_delete_window (ModestShell *shell, ModestWindow *window)
268 {
269         ModestShellPrivate *priv;
270         gboolean ret_value;
271
272         priv = MODEST_SHELL_GET_PRIVATE (shell);
273         g_signal_emit_by_name (G_OBJECT (window), "delete-event", NULL, &ret_value);
274         if (ret_value == FALSE) {
275                 gint page_num;
276                 
277                 page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook), GTK_WIDGET (window));
278                 if (page_num != -1) {
279                         gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), page_num);
280                 }
281         }
282
283         update_title (shell);
284
285         return ret_value;
286 }
287
288 void
289 modest_shell_add_window (ModestShell *shell, ModestWindow *window)
290 {
291         ModestShellPrivate *priv;
292
293         priv = MODEST_SHELL_GET_PRIVATE (shell);
294         gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook), GTK_WIDGET (window), NULL);
295         gtk_widget_show (GTK_WIDGET (window));
296         gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), -1);
297         modest_shell_window_set_shell (MODEST_SHELL_WINDOW (window), shell);
298         update_title (shell);
299 }
300
301 gint
302 modest_shell_count_windows (ModestShell *shell)
303 {
304         ModestShellPrivate *priv;
305
306         priv = MODEST_SHELL_GET_PRIVATE (shell);
307
308         return gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
309 }
310
311 void
312 modest_shell_set_title (ModestShell *shell, ModestWindow *window, const gchar *title)
313 {
314         ModestShellPrivate *priv;
315
316         priv = MODEST_SHELL_GET_PRIVATE (shell);
317
318         gtk_notebook_set_tab_label_text (GTK_NOTEBOOK (priv->notebook), GTK_WIDGET (window), title);
319
320         update_title (shell);
321 }
322
323 static void
324 show_next_frame (ModestShell *shell)
325 {
326         ModestShellPrivate *priv;
327
328         priv = MODEST_SHELL_GET_PRIVATE (shell);
329
330         gtk_image_set_from_pixbuf (GTK_IMAGE (priv->progress_icon), priv->progress_frames[priv->next_frame]);
331
332         priv->next_frame++;
333         if (priv->next_frame >= 31)
334                 priv->next_frame = 0;
335 }
336
337 static gboolean
338 on_progress_timeout (ModestShell *shell)
339 {
340         show_next_frame (shell);
341         return TRUE;
342 }
343
344 void
345 modest_shell_show_progress (ModestShell *shell, ModestWindow *window, gboolean show)
346 {
347         ModestShellPrivate *priv;
348
349         priv = MODEST_SHELL_GET_PRIVATE (shell);
350
351         if (show) {
352                 if (priv->progress_timeout_id == 0) {
353                         priv->progress_timeout_id = g_timeout_add (100, (GSourceFunc) on_progress_timeout, shell);
354                         show_next_frame (shell);
355                 }
356                 gtk_widget_show (priv->progress_icon);
357         } else {
358                 if (priv->progress_timeout_id) {
359                         g_source_remove (priv->progress_timeout_id);
360                         priv->progress_timeout_id = 0;
361                 }
362                 gtk_widget_hide (priv->progress_icon);
363         }
364 }
365
366 static void
367 update_title (ModestShell *self)
368 {
369         gint n_pages, i;
370         ModestShellPrivate *priv;
371         GtkWidget *child;
372         GString *title_buffer;
373         GString *subtitle_buffer;
374         const gchar *tab_label_text;
375
376         priv = MODEST_SHELL_GET_PRIVATE (self);
377
378         n_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
379         if (n_pages == 0) {
380                 gtk_label_set_text (GTK_LABEL (priv->title_label), "");
381                 gtk_label_set_text (GTK_LABEL (priv->subtitle_label), "");
382                 return;
383         }
384
385         child = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), n_pages - 1);
386         title_buffer = g_string_new ("");
387         title_buffer = g_string_append (title_buffer, "<b>");
388         tab_label_text = gtk_notebook_get_tab_label_text (GTK_NOTEBOOK (priv->notebook), child);
389         if (tab_label_text)
390                 title_buffer = g_string_append (title_buffer, tab_label_text);
391         title_buffer = g_string_append (title_buffer, "</b>");
392         gtk_label_set_markup (GTK_LABEL (priv->title_label), 
393                               title_buffer->str);
394         g_string_free (title_buffer, TRUE);
395
396         subtitle_buffer = g_string_new ("");
397         subtitle_buffer = g_string_append (subtitle_buffer, "<small>");
398         for (i = 0; i < n_pages - 1; i++) {
399         child = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), i);
400                 if (i != 0) {
401                         subtitle_buffer = g_string_append (subtitle_buffer, " / ");
402                 }
403                 subtitle_buffer = g_string_append (subtitle_buffer,
404                                                    gtk_notebook_get_tab_label_text (GTK_NOTEBOOK (priv->notebook), child));
405         }
406         subtitle_buffer = g_string_append (subtitle_buffer, "</small>");
407         gtk_label_set_markup (GTK_LABEL (priv->subtitle_label), 
408                               subtitle_buffer->str);
409         g_string_free (subtitle_buffer, TRUE);
410 }
411
412 static void
413 on_back_button_clicked (GtkToolButton *button, ModestShell *self)
414 {
415         ModestShellPrivate *priv;
416         gint n_pages;
417         gboolean delete_event_retval;
418         GtkWidget *child;
419
420         priv = MODEST_SHELL_GET_PRIVATE (self);
421
422         n_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
423         if (n_pages < 1)
424                 return;
425
426         child = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), -1);
427         g_signal_emit_by_name (G_OBJECT (child), "delete-event", NULL, &delete_event_retval);
428
429         if (!delete_event_retval) {
430                 update_title (self);
431         }
432 }
433
434 static void
435 menu_position_cb (GtkMenu *menu,
436                   gint *x,
437                   gint *y,
438                   gboolean *push_in,
439                   ModestShell *self)
440 {
441         ModestShellPrivate *priv;
442         GtkAllocation *alloc;
443         GdkWindow *parent_window;
444         gint pos_x, pos_y;
445
446         priv = MODEST_SHELL_GET_PRIVATE (self);
447
448         alloc = &(GTK_WIDGET (priv->title_button)->allocation);
449         parent_window = gtk_widget_get_parent_window (GTK_WIDGET (priv->title_button));
450         gdk_window_get_position (parent_window, &pos_x, &pos_y);
451         *x = pos_x + alloc->x;
452         *y = pos_y + alloc->y + alloc->height;
453         *push_in = TRUE;
454         
455 }
456
457 static void
458 on_title_button_clicked (GtkToolButton *button, ModestShell *self)
459 {
460         ModestShellPrivate *priv;
461         gint n_pages;
462         GtkWidget *child;
463         GtkWidget *menu;
464
465         priv = MODEST_SHELL_GET_PRIVATE (self);
466
467         n_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
468         if (n_pages < 1)
469                 return;
470
471         child = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), -1);
472         menu = modest_shell_window_get_menu (MODEST_SHELL_WINDOW (child));
473
474         if (menu) {
475                 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, 
476                                 (GtkMenuPositionFunc) menu_position_cb, (gpointer) self,
477                                 1, gtk_get_current_event_time ());
478         }
479 }
480
481 static void
482 on_new_msg_button_clicked (GtkToolButton *button, ModestShell *self)
483 {
484         ModestShellPrivate *priv;
485         gint n_pages;
486         GtkWidget *child;
487
488         priv = MODEST_SHELL_GET_PRIVATE (self);
489
490         n_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
491         if (n_pages < 1)
492                 return;
493
494         child = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), -1);
495
496         modest_ui_actions_on_new_msg (NULL, MODEST_WINDOW (child));
497 }
498
499 static void
500 on_style_set (GtkWidget *widget,
501               GtkStyle *old_style,
502               ModestShell *self)
503 {
504         ModestShellPrivate *priv;
505         gint icon_w, icon_h;
506         GdkPixbuf *progress_pixbuf;
507         int n;
508
509         priv = MODEST_SHELL_GET_PRIVATE (self);
510
511         if (!gtk_icon_size_lookup (GTK_ICON_SIZE_LARGE_TOOLBAR, &icon_w, &icon_h))
512                 return;
513         progress_pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (), "process-working", icon_w, 0, NULL);
514
515         for (n = 0; n < 31; n++) {
516                 if (priv->progress_frames[n] != NULL) {
517                         g_object_unref (priv->progress_frames[n]);
518                 }
519                 priv->progress_frames[n] = NULL;
520         }
521
522         if (progress_pixbuf) {
523                 gint max_x, max_y;
524                 gint i, j;
525
526                 icon_w = gdk_pixbuf_get_width (progress_pixbuf) / 8;
527
528                 n = 0;
529                 max_x = 8;
530                 max_y = 4;
531                 for (i = 0; i < 4; i++) {
532                         for (j = 0; j < 8; j++) {
533                                         GdkPixbuf *frame;
534
535                                         if ((i == 0) && (j == 0))
536                                                 continue;
537                                         frame = gdk_pixbuf_new_subpixbuf  (progress_pixbuf,
538                                                                            j*icon_w, i*icon_w,
539                                                                            icon_w, icon_w);
540                                         priv->progress_frames[n] = frame;
541                                         n++;
542                                 }
543                         }
544                 g_object_unref (progress_pixbuf);
545         }
546
547 }
548
549 static gboolean
550 on_key_pressed (GtkWidget *widget,
551                 GdkEventKey *event,
552                 ModestShell *shell)
553 {
554         ModestShellPrivate *priv;
555         gboolean retval;
556         GtkWidget *current_window;
557
558         priv = MODEST_SHELL_GET_PRIVATE (shell);
559
560         current_window = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), -1);
561
562         g_signal_emit_by_name (current_window, "key-press-event", event, &retval);
563
564         return retval;
565         
566 }