MenuShell menu button work:
[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
34 /* 'private'/'protected' functions */
35 static void modest_shell_class_init (ModestShellClass *klass);
36 static void modest_shell_instance_init (ModestShell *obj);
37 static void modest_shell_finalize   (GObject *obj);
38
39 static void update_title (ModestShell *self);
40
41
42 typedef struct _ModestShellPrivate ModestShellPrivate;
43 struct _ModestShellPrivate {
44         GtkWidget *main_vbox;
45         GtkWidget *notebook;
46         GtkWidget *top_toolbar;
47         GtkToolItem *back_button;
48         GtkToolItem *title_button;
49         GtkWidget *title_label;
50         GtkWidget *subtitle_label;
51 };
52 #define MODEST_SHELL_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
53                                                                       MODEST_TYPE_SHELL, \
54                                                                       ModestShellPrivate))
55 /* globals */
56 static GObjectClass *parent_class = NULL;
57
58 GType
59 modest_shell_get_type (void)
60 {
61         static GType my_type = 0;
62         if (!my_type) {
63                 static const GTypeInfo my_info = {
64                         sizeof(ModestShellClass),
65                         NULL,           /* base init */
66                         NULL,           /* base finalize */
67                         (GClassInitFunc) modest_shell_class_init,
68                         NULL,           /* class finalize */
69                         NULL,           /* class data */
70                         sizeof(ModestShell),
71                         1,              /* n_preallocs */
72                         (GInstanceInitFunc) modest_shell_instance_init,
73                         NULL
74                 };
75                 my_type = g_type_register_static (GTK_TYPE_WINDOW,
76                                                   "ModestShell",
77                                                   &my_info, 0);
78         }
79         return my_type;
80 }
81
82 static void
83 modest_shell_class_init (ModestShellClass *klass)
84 {
85         GObjectClass *gobject_class;
86
87         gobject_class = (GObjectClass*) klass;
88
89         parent_class            = g_type_class_peek_parent (klass);
90         gobject_class->finalize = modest_shell_finalize;
91
92         g_type_class_add_private (gobject_class, sizeof(ModestShellPrivate));
93
94 }
95
96 static void
97 modest_shell_instance_init (ModestShell *obj)
98 {
99         ModestShellPrivate *priv;
100         GtkWidget *title_vbox;
101
102         priv = MODEST_SHELL_GET_PRIVATE(obj);
103
104         priv->main_vbox = gtk_vbox_new (FALSE, 0);
105         gtk_widget_show (priv->main_vbox);
106
107         priv->top_toolbar = gtk_toolbar_new ();
108         gtk_toolbar_set_style (GTK_TOOLBAR (priv->top_toolbar), GTK_TOOLBAR_BOTH_HORIZ);
109         gtk_widget_show (priv->top_toolbar);
110         gtk_box_pack_start (GTK_BOX (priv->main_vbox), priv->top_toolbar, FALSE, FALSE, 0);
111
112         priv->back_button = gtk_tool_button_new_from_stock (GTK_STOCK_GO_BACK);
113         gtk_toolbar_insert (GTK_TOOLBAR (priv->top_toolbar), priv->back_button, -1);
114         gtk_widget_show (GTK_WIDGET (priv->back_button));
115
116         title_vbox = gtk_vbox_new (FALSE, 0);
117         priv->title_label = gtk_label_new (NULL);
118         gtk_misc_set_alignment (GTK_MISC (priv->title_label), 0.0, 1.0);
119         priv->subtitle_label = gtk_label_new (NULL);
120         gtk_misc_set_alignment (GTK_MISC (priv->subtitle_label), 0.0, 0.0);
121         gtk_widget_show (priv->title_label);
122         gtk_widget_show (priv->subtitle_label);
123         gtk_box_pack_start (GTK_BOX (title_vbox), priv->title_label, TRUE, TRUE, 0);
124         gtk_box_pack_start (GTK_BOX (title_vbox), priv->subtitle_label, FALSE, FALSE, 0);
125         gtk_widget_show (title_vbox);
126
127         priv->title_button = gtk_tool_button_new (NULL, NULL);
128         gtk_widget_show (GTK_WIDGET (priv->title_button));
129         gtk_tool_button_set_label_widget (GTK_TOOL_BUTTON (priv->title_button), title_vbox);
130         gtk_toolbar_insert (GTK_TOOLBAR (priv->top_toolbar), priv->title_button, -1);
131         gtk_container_child_set (GTK_CONTAINER (priv->top_toolbar), GTK_WIDGET (priv->title_button), "expand", TRUE, NULL);
132         g_object_set (priv->title_button, "is-important", TRUE, NULL);
133
134         priv->notebook = gtk_notebook_new ();
135         gtk_widget_show (priv->notebook);
136         gtk_box_pack_start (GTK_BOX (priv->main_vbox), priv->notebook, TRUE, TRUE, 0);
137         gtk_container_add (GTK_CONTAINER (obj), priv->main_vbox);
138
139 }
140
141 static void
142 modest_shell_finalize (GObject *obj)
143 {
144         G_OBJECT_CLASS(parent_class)->finalize (obj);
145 }
146
147 GtkWidget*
148 modest_shell_new (void)
149 {
150         return (GtkWidget *) g_object_new(MODEST_TYPE_SHELL, NULL);
151 }
152
153 ModestWindow *
154 modest_shell_peek_window (ModestShell *shell)
155 {
156         ModestShellPrivate *priv;
157         gint count;
158
159         priv = MODEST_SHELL_GET_PRIVATE (shell);
160         count = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
161
162         if (count > 0) {
163                 return (ModestWindow *) gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), count - 1);
164         } else {
165                 return NULL;
166         }
167 }
168
169 gboolean
170 modest_shell_delete_window (ModestShell *shell, ModestWindow *window)
171 {
172         ModestShellPrivate *priv;
173         gboolean ret_value;
174
175         priv = MODEST_SHELL_GET_PRIVATE (shell);
176         g_signal_emit_by_name (G_OBJECT (window), "delete-event", NULL, &ret_value);
177         if (ret_value == FALSE) {
178                 gint page_num;
179                 
180                 page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook), GTK_WIDGET (window));
181                 if (page_num != -1) {
182                         gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), page_num);
183                 }
184         }
185
186         update_title (shell);
187
188         return ret_value;
189 }
190
191 void
192 modest_shell_add_window (ModestShell *shell, ModestWindow *window)
193 {
194         ModestShellPrivate *priv;
195
196         priv = MODEST_SHELL_GET_PRIVATE (shell);
197         gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook), GTK_WIDGET (window), NULL);
198         modest_shell_window_set_shell (MODEST_SHELL_WINDOW (window), shell);
199         update_title (shell);
200 }
201
202 gint
203 modest_shell_count_windows (ModestShell *shell)
204 {
205         ModestShellPrivate *priv;
206
207         priv = MODEST_SHELL_GET_PRIVATE (shell);
208
209         return gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
210 }
211
212 void
213 modest_shell_set_title (ModestShell *shell, ModestWindow *window, const gchar *title)
214 {
215         ModestShellPrivate *priv;
216
217         priv = MODEST_SHELL_GET_PRIVATE (shell);
218
219         gtk_notebook_set_tab_label_text (GTK_NOTEBOOK (priv->notebook), GTK_WIDGET (window), title);
220
221         update_title (shell);
222 }
223
224 void
225 modest_shell_show_progress (ModestShell *shell, ModestWindow *window, gboolean show)
226 {
227 }
228
229 static void
230 update_title (ModestShell *self)
231 {
232         gint n_pages, i;
233         ModestShellPrivate *priv;
234         GtkWidget *child;
235         GString *title_buffer;
236         GString *subtitle_buffer;
237
238         priv = MODEST_SHELL_GET_PRIVATE (self);
239
240         n_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
241         if (n_pages == 0) {
242                 gtk_label_set_text (GTK_LABEL (priv->title_label), "");
243                 gtk_label_set_text (GTK_LABEL (priv->subtitle_label), "");
244                 return;
245         }
246
247         child = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), n_pages - 1);
248         title_buffer = g_string_new ("");
249         title_buffer = g_string_append (title_buffer, "<b>");
250         title_buffer = g_string_append (title_buffer, gtk_notebook_get_tab_label_text (GTK_NOTEBOOK (priv->notebook), child));
251         title_buffer = g_string_append (title_buffer, "</b>");
252         gtk_label_set_markup (GTK_LABEL (priv->title_label), 
253                               title_buffer->str);
254         g_string_free (title_buffer, TRUE);
255
256         subtitle_buffer = g_string_new ("");
257         subtitle_buffer = g_string_append (subtitle_buffer, "<small>");
258         for (i = 0; i < n_pages - 1; i++) {
259         child = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), i);
260                 if (i != 0) {
261                         subtitle_buffer = g_string_append (subtitle_buffer, " / ");
262                 }
263                 subtitle_buffer = g_string_append (subtitle_buffer,
264                                                    gtk_notebook_get_tab_label_text (GTK_NOTEBOOK (priv->notebook), child));
265         }
266         subtitle_buffer = g_string_append (subtitle_buffer, "</small>");
267         gtk_label_set_markup (GTK_LABEL (priv->subtitle_label), 
268                               subtitle_buffer->str);
269         g_string_free (subtitle_buffer, TRUE);
270 }