Forgotten files
[modest] / src / widgets / modest-toolkit-utils.c
1 /* Copyright (c) 2006, 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 <glib.h>
31 #include <glib/gstdio.h>
32 #include <errno.h>
33 #include <string.h> /* for strlen */
34 #include <modest-runtime.h>
35
36 #include <modest-defs.h>
37 #include "modest-toolkit-utils.h"
38 #include "modest-ui-constants.h"
39 #ifdef MODEST_TOOLKIT_HILDON2
40 #include <hildon/hildon.h>
41 #endif
42
43 /* Label child of a captioned */
44 #define CAPTIONED_LABEL_CHILD "captioned-label"
45
46
47 GtkWidget *
48 modest_toolkit_utils_get_manager_menubar_as_menu (GtkUIManager *manager,
49                                                   const gchar *item_name)
50 {
51         GtkWidget *new_menu;
52         GtkWidget *menubar;
53         GList *children, *iter;
54
55         menubar = gtk_ui_manager_get_widget (manager, item_name);
56         new_menu = gtk_menu_new ();
57
58         children = gtk_container_get_children (GTK_CONTAINER (menubar));
59         for (iter = children; iter != NULL; iter = g_list_next (iter)) {
60                 GtkWidget *menu;
61
62                 menu = GTK_WIDGET (iter->data);
63                 gtk_widget_reparent (menu, new_menu);
64         }
65         
66         g_list_free (children);
67
68         return new_menu;
69 }
70
71 /**
72  * modest_toolkit_utils_create_captioned:
73  * @title_size_group: a #GtkSizeGroup
74  * @value_size_group: a #GtkSizeGroup
75  * @title: a string
76  * @control: a #GtkWidget
77  *
78  * this creates a widget (a #GtkHBox) with a control, and a label
79  * (@string) captioning it. It also uses the proper size groups for title
80  * and control.
81  *
82  * Returns: a widget containing the control and a proper label.
83  */
84 GtkWidget *
85 modest_toolkit_utils_create_captioned    (GtkSizeGroup *title_size_group,
86                                           GtkSizeGroup *value_size_group,
87                                           const gchar *title,
88                                           gboolean use_markup,
89                                           GtkWidget *control)
90 {
91         return modest_toolkit_utils_create_captioned_with_size_type (title_size_group,
92                                                                      value_size_group,
93                                                                      title,
94                                                                      use_markup,
95                                                                      control,
96                                                                      0);
97 }
98
99 GtkWidget *
100 modest_toolkit_utils_create_vcaptioned    (GtkSizeGroup *size_group,
101                                            const gchar *title,
102                                            gboolean use_markup,
103                                            GtkWidget *control)
104 {
105         return modest_toolkit_utils_create_vcaptioned_with_size_type (size_group,
106                                                                       title,
107                                                                       use_markup,
108                                                                       control,
109                                                                       0);
110 }
111
112 /**
113  * modest_toolkit_utils_create_captioned_with_size_type:
114  * @title_size_group: a #GtkSizeGroup
115  * @value_size_group: a #GtkSizeGroup
116  * @title: a string
117  * @control: a #GtkWidget
118  * @size_type: a #HildonSizeType
119  *
120  * this creates a widget (a #GtkHBox) with a control, and a label
121  * (@string) captioning it. It also uses the proper size groups for title
122  * and control.
123  *
124  * Returns: a widget containing the control and a proper label.
125  */
126 GtkWidget *
127 modest_toolkit_utils_create_captioned_with_size_type    (GtkSizeGroup *title_size_group,
128                                                          GtkSizeGroup *value_size_group,
129                                                          const gchar *title,
130                                                          gboolean use_markup,
131                                                          GtkWidget *control,
132                                                          ModestToolkitSizeType size_type)
133 {
134         GtkWidget *label;
135         GtkWidget *align;
136         GtkWidget *box;
137   
138         if (use_markup) {
139                 label = gtk_label_new (NULL);
140                 gtk_label_set_markup (GTK_LABEL (label), title);
141         } else {
142                 label = gtk_label_new (title);
143         }
144         align = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
145         gtk_alignment_set_padding (GTK_ALIGNMENT (align), 0, 0, MODEST_MARGIN_DOUBLE, MODEST_MARGIN_DOUBLE);
146
147         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
148 #ifdef MAEMO_CHANGES
149         hildon_gtk_widget_set_theme_size (label, HILDON_SIZE_FINGER_HEIGHT);
150 #endif
151         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
152         gtk_widget_show (label);
153         gtk_widget_show (align);
154         box = gtk_hbox_new (FALSE, 0);
155         gtk_container_add (GTK_CONTAINER (align), label);
156         gtk_box_pack_start (GTK_BOX (box), align, FALSE, FALSE, 0);
157         gtk_box_pack_start (GTK_BOX (box), control, TRUE, TRUE, 0);
158         if (title_size_group)
159                 gtk_size_group_add_widget (title_size_group, label);
160         if (value_size_group)
161                 gtk_size_group_add_widget (value_size_group, control);
162
163 #ifdef MAEMO_CHANGES
164         hildon_gtk_widget_set_theme_size (control, size_type);
165 #endif
166
167         g_object_set_data (G_OBJECT (box), CAPTIONED_LABEL_CHILD, label);
168
169         return box;
170 }
171
172 GtkWidget *
173 modest_toolkit_utils_create_vcaptioned_with_size_type    (GtkSizeGroup *size_group,
174                                                           const gchar *title,
175                                                           gboolean use_markup,
176                                                           GtkWidget *control,
177                                                           ModestToolkitSizeType size_type)
178 {
179         GtkWidget *label;
180         GtkWidget *align;
181         GtkWidget *box;
182   
183         if (use_markup) {
184                 label = gtk_label_new (NULL);
185                 gtk_label_set_markup (GTK_LABEL (label), title);
186         } else {
187                 label = gtk_label_new (title);
188         }
189         align = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
190         gtk_alignment_set_padding (GTK_ALIGNMENT (align), 0, 0, MODEST_MARGIN_DOUBLE, MODEST_MARGIN_DOUBLE);
191
192         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
193 #ifdef MAEMO_CHANGES
194         hildon_gtk_widget_set_theme_size (label, HILDON_SIZE_FINGER_HEIGHT);
195 #endif
196         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
197         gtk_widget_show (label);
198         gtk_widget_show (align);
199         box = gtk_vbox_new (FALSE, 0);
200         gtk_container_add (GTK_CONTAINER (align), label);
201         gtk_box_pack_start (GTK_BOX (box), align, FALSE, FALSE, 0);
202         gtk_box_pack_start (GTK_BOX (box), control, TRUE, TRUE, 0);
203         if (size_group) {
204                 gtk_size_group_add_widget (size_group, label);
205                 gtk_size_group_add_widget (size_group, control);
206         }
207
208 #ifdef MAEMO_CHANGES
209         hildon_gtk_widget_set_theme_size (control, size_type);
210 #endif
211
212         g_object_set_data (G_OBJECT (box), CAPTIONED_LABEL_CHILD, label);
213
214         return box;
215 }
216
217 /**
218  * modest_toolkit_utils_captioned_set_label:
219  * @captioned: a #GtkWidget built as captioned
220  * @new_label: a string
221  * @use_markup: a #gboolean
222  *
223  * set a new label for the captioned
224  */
225 void
226 modest_toolkit_utils_captioned_set_label (GtkWidget *captioned,
227                                           const gchar *new_label,
228                                           gboolean use_markup)
229 {
230         GtkWidget *label;
231
232         g_return_if_fail (GTK_IS_WIDGET (captioned));
233
234         label = g_object_get_data (G_OBJECT (captioned), CAPTIONED_LABEL_CHILD);
235         g_return_if_fail (GTK_IS_LABEL (label));
236
237         if (use_markup) {
238                 gtk_label_set_markup (GTK_LABEL (label), new_label);
239         } else {
240                 gtk_label_set_text (GTK_LABEL (label), new_label);
241         }
242 }
243
244 /**
245  * modest_toolkit_utils_captioned_get_label_widget:
246  * @captioned: a #GtkWidget built as captioned
247  *
248  * obtains the label widget for the captioned
249  *
250  * Returns: a #GtkLabel
251  */
252 GtkWidget *
253 modest_toolkit_utils_captioned_get_label_widget (GtkWidget *captioned)
254 {
255         GtkWidget *label;
256
257         g_return_val_if_fail (GTK_IS_WIDGET (captioned), NULL);
258
259         label = g_object_get_data (G_OBJECT (captioned), CAPTIONED_LABEL_CHILD);
260         g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
261
262         return label;
263
264 }
265
266 /**
267  * modest_toolkit_utils_set_hbutton_layout:
268  * @title_sizegroup: a #GtkSizeGroup, or %NULL
269  * @value_sizegroup: a #GtkSizeGroup, or %NULL
270  * @title: a string
271  * @button: a #HildonButton
272  *
273  * Configures the alignment and layout of @button. If @title_sizegroup is provided,
274  * the title will be aligned to the left using it. If @value_sizegroup is provided,
275  * the value will be aligned to the left using it. It also sets the title
276  * of the button.
277  *
278  * The alignment is left for the title and for the value.
279  */
280 void
281 modest_toolkit_utils_set_hbutton_layout (GtkSizeGroup *title_sizegroup, 
282                                        GtkSizeGroup *value_sizegroup,
283                                        const gchar *title, 
284                                        GtkWidget *button)
285 {
286 #ifdef MODEST_TOOLKIT_HILDON2
287         hildon_button_set_title (HILDON_BUTTON (button), title);
288         if (title_sizegroup)
289                 hildon_button_add_title_size_group (HILDON_BUTTON (button), title_sizegroup);
290         if (value_sizegroup)
291                 hildon_button_add_value_size_group (HILDON_BUTTON (button), value_sizegroup);
292         hildon_button_set_alignment (HILDON_BUTTON (button), 0.0, 0.5, 1.0, 0.0);
293         hildon_button_set_title_alignment (HILDON_BUTTON (button), 0.0, 0.5);
294         hildon_button_set_value_alignment (HILDON_BUTTON (button), 0.0, 0.5);
295 #endif
296 }
297
298 void
299 modest_toolkit_utils_set_vbutton_layout (GtkSizeGroup *sizegroup, 
300                                          const gchar *title, 
301                                          GtkWidget *button)
302 {
303 #ifdef MODEST_TOOLKIT_HILDON2
304         hildon_button_set_title (HILDON_BUTTON (button), title);
305         if (sizegroup) {
306                 hildon_button_add_title_size_group (HILDON_BUTTON (button), sizegroup);
307                 hildon_button_add_value_size_group (HILDON_BUTTON (button), sizegroup);
308         }
309         hildon_button_set_alignment (HILDON_BUTTON (button), 0.0, 0.5, 1.0, 0.0);
310         hildon_button_set_title_alignment (HILDON_BUTTON (button), 0.0, 0.5);
311         hildon_button_set_value_alignment (HILDON_BUTTON (button), 0.0, 0.5);
312 #endif
313 }
314
315 GtkWidget *
316 modest_toolkit_utils_create_group_box (const gchar *label_text, GtkWidget *contents)
317 {
318         GtkWidget *label;
319         GtkWidget *box;
320
321         label = gtk_label_new (label_text);
322         gtk_widget_show (label);
323
324         box = gtk_vbox_new (FALSE, MODEST_MARGIN_HALF);
325         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
326         gtk_box_pack_start (GTK_BOX (box), contents, TRUE, TRUE, 0);
327         gtk_widget_show (box);
328
329         return box;
330 }