New ModestCountryComboBox for running modest in gtk.
[modest] / src / widgets / modest-toolkit-factory.c
1 /* Copyright (c) 2009, Igalia
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/gi18n.h>
31 #ifdef MODEST_TOOLKIT_HILDON2
32 #include <hildon/hildon.h>
33 #include <hildon/hildon-file-chooser-dialog.h>
34 #include <modest-number-editor.h>
35 #include <modest-ui-constants.h>
36 #endif
37 #include "modest-toolkit-factory.h"
38
39 #ifndef MODEST_TOOLKIT_HILDON2
40 #define USE_SCROLLED_WINDOW
41 #define USE_GTK_FIND_TOOLBAR
42 #define USE_GTK_CHECK_BUTTON
43 #define USE_GTK_CHECK_MENU
44 #define USE_GTK_ENTRY
45 #define USE_GTK_FILE_CHOOSER
46 #define USE_COUNTRY_COMBOBOX
47 #endif
48
49 #ifdef USE_SCROLLED_WINDOW
50 #include <modest-scrolled-window-scrollable.h>
51 #else
52 #include <modest-hildon-pannable-area-scrollable.h>
53 #endif
54
55 #ifdef USE_GTK_TOOLBAR
56 #include <modest-find-toolbar.h>
57 #else
58 #include <modest-hildon-find-toolbar.h>
59 #endif
60
61 #ifdef USE_COUNTRY_COMBOBOX
62 #include <modest-country-combo-box.h>
63 #else
64 #include <modest-country-picker.h>
65 #endif
66
67 static void modest_toolkit_factory_class_init (ModestToolkitFactoryClass *klass);
68 static void modest_toolkit_factory_init (ModestToolkitFactory *self);
69
70 /* GObject interface */
71 static GtkWidget * modest_toolkit_factory_create_scrollable_default           (ModestToolkitFactory *self);
72 static GtkWidget * modest_toolkit_factory_create_check_button_default         (ModestToolkitFactory *self,
73                                                                                const gchar *label);
74 static GtkWidget * modest_toolkit_factory_create_check_menu_default           (ModestToolkitFactory *self,
75                                                                                const gchar *label);
76 static GtkWidget * modest_toolkit_factory_create_isearch_toolbar_default      (ModestToolkitFactory *self,
77                                                                                const gchar *label);
78 static GtkWidget * modest_toolkit_factory_create_entry_default                (ModestToolkitFactory *self);
79 static GtkWidget * modest_toolkit_factory_create_number_entry_default         (ModestToolkitFactory *self,
80                                                                                gint min,
81                                                                                gint max);
82 static GtkWidget * modest_toolkit_factory_create_file_chooser_dialog_default  (ModestToolkitFactory *self,
83                                                                                const gchar *title,
84                                                                                GtkWindow *parent,
85                                                                                GtkFileChooserAction action);
86 static GtkWidget * modest_toolkit_factory_create_country_selector_default     (ModestToolkitFactory *self);
87 /* globals */
88 static GObjectClass *parent_class = NULL;
89
90 G_DEFINE_TYPE    (ModestToolkitFactory,
91                   modest_toolkit_factory,
92                   G_TYPE_OBJECT);
93
94 ModestToolkitFactory *
95 modest_toolkit_factory_get_instance                            (void)
96 {
97     GObject* self = g_object_new (MODEST_TYPE_TOOLKIT_FACTORY, NULL);
98
99     return (ModestToolkitFactory *) self;
100 }
101
102 static void
103 modest_toolkit_factory_class_init (ModestToolkitFactoryClass *klass)
104 {
105         parent_class = g_type_class_peek_parent (klass);
106
107         klass->create_scrollable = modest_toolkit_factory_create_scrollable_default;
108         klass->create_check_button = modest_toolkit_factory_create_check_button_default;
109         klass->create_check_menu = modest_toolkit_factory_create_check_menu_default;
110         klass->create_isearch_toolbar = modest_toolkit_factory_create_isearch_toolbar_default;
111         klass->create_entry = modest_toolkit_factory_create_entry_default;
112         klass->create_number_entry = modest_toolkit_factory_create_number_entry_default;
113         klass->create_file_chooser_dialog = modest_toolkit_factory_create_file_chooser_dialog_default;
114         klass->create_country_selector = modest_toolkit_factory_create_country_selector_default;
115 }
116
117 static void
118 modest_toolkit_factory_init (ModestToolkitFactory *self)
119 {
120 }
121
122 GtkWidget *
123 modest_toolkit_factory_create_scrollable (ModestToolkitFactory *self)
124 {
125         return MODEST_TOOLKIT_FACTORY_GET_CLASS (self)->create_scrollable (self);
126 }
127
128 static GtkWidget *
129 modest_toolkit_factory_create_scrollable_default (ModestToolkitFactory *self)
130 {
131 #ifdef USE_SCROLLED_WINDOW
132         return modest_scrolled_window_scrollable_new ();
133 #else
134         return modest_hildon_pannable_area_scrollable_new ();
135 #endif
136 }
137
138 GtkWidget *
139 modest_toolkit_factory_create_check_button (ModestToolkitFactory *self, const gchar *label)
140 {
141         return MODEST_TOOLKIT_FACTORY_GET_CLASS (self)->create_check_button (self, label);
142 }
143
144 static GtkWidget *
145 modest_toolkit_factory_create_check_button_default (ModestToolkitFactory *self, const gchar *label)
146 {
147         GtkWidget *result;
148 #ifdef USE_GTK_CHECK_BUTTON
149         result = gtk_check_button_new_with_label (label);
150 #else
151         result = hildon_check_button_new (HILDON_SIZE_FINGER_HEIGHT);
152         gtk_button_set_label (GTK_BUTTON (result), label);
153         gtk_button_set_alignment (GTK_BUTTON (result), 0.0, 0.5);
154 #endif
155         return result;
156 }
157
158 GtkWidget *
159 modest_toolkit_factory_create_check_menu (ModestToolkitFactory *self, const gchar *label)
160 {
161         return MODEST_TOOLKIT_FACTORY_GET_CLASS (self)->create_check_menu (self, label);
162 }
163
164 static GtkWidget *
165 modest_toolkit_factory_create_check_menu_default (ModestToolkitFactory *self, const gchar *label)
166 {
167         GtkWidget *result;
168 #ifdef USE_GTK_CHECK_MENU
169         result = gtk_check_menu_item_new_with_label (label);
170 #else
171         result = hildon_check_button_new (0);
172         gtk_button_set_label (GTK_BUTTON (result), label);
173         gtk_button_set_alignment (GTK_BUTTON (result), 0.5, 0.5);
174 #endif
175         return result;
176 }
177
178 gboolean
179 modest_togglable_get_active (GtkWidget *widget)
180 {
181         if (GTK_IS_CHECK_MENU_ITEM (widget)) {
182                 return gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget));
183         } else if (GTK_IS_TOGGLE_BUTTON (widget)) {
184                 return gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
185 #ifdef MODEST_TOOLKIT_HILDON2
186         } else if (HILDON_IS_CHECK_BUTTON (widget)) {
187                 return hildon_check_button_get_active (HILDON_CHECK_BUTTON (widget));
188 #endif
189         } else {
190                 g_return_val_if_reached (FALSE);
191         }
192 }
193
194 void
195 modest_togglable_set_active (GtkWidget *widget, gboolean active)
196 {
197         if (GTK_IS_CHECK_MENU_ITEM (widget)) {
198                 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (widget), active);
199         } else if (GTK_IS_TOGGLE_BUTTON (widget)) {
200                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), active);
201 #ifdef MODEST_TOOLKIT_HILDON2
202         } else if (HILDON_IS_CHECK_BUTTON (widget)) {
203                 hildon_check_button_set_active (HILDON_CHECK_BUTTON (widget), active);
204 #endif
205         }
206 }
207
208 gboolean
209 modest_is_togglable (GtkWidget *widget)
210 {
211         return GTK_IS_CHECK_MENU_ITEM (widget) 
212                 || GTK_IS_TOGGLE_BUTTON (widget)
213 #ifdef MODEST_TOOLKIT_HILDON2
214                 || HILDON_IS_CHECK_BUTTON (widget)
215 #endif
216                 ;
217 }
218
219 GtkWidget *
220 modest_toolkit_factory_create_isearch_toolbar (ModestToolkitFactory *self, const gchar *label)
221 {
222         return MODEST_TOOLKIT_FACTORY_GET_CLASS (self)->create_isearch_toolbar (self, label);
223 }
224
225 static GtkWidget *
226 modest_toolkit_factory_create_isearch_toolbar_default (ModestToolkitFactory *self, const gchar *label)
227 {
228         GtkWidget *result;
229 #ifdef USE_GTK_FIND_TOOLBAR
230         result = modest_find_toolbar_new (label);
231 #else
232         result = modest_hildon_find_toolbar_new (label);
233 #endif
234         return result;
235 }
236
237 GtkWidget *
238 modest_toolkit_factory_create_entry (ModestToolkitFactory *self)
239 {
240         return MODEST_TOOLKIT_FACTORY_GET_CLASS (self)->create_entry (self);
241 }
242
243 static GtkWidget *
244 modest_toolkit_factory_create_entry_default (ModestToolkitFactory *self)
245 {
246 #ifdef USE_GTK_ENTRY
247         return gtk_entry_new ();
248 #else
249         return hildon_entry_new (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
250 #endif
251 }
252
253 void
254 modest_entry_set_text (GtkWidget *widget, const gchar *text)
255 {
256 #ifdef MODEST_TOOLKIT_HILDON2
257         hildon_entry_set_text (HILDON_ENTRY (widget), text);
258 #else
259         gtk_entry_set_text (GTK_ENTRY (widget), text);
260 #endif
261 }
262
263 const gchar *
264 modest_entry_get_text (GtkWidget *widget)
265 {
266 #ifdef MODEST_TOOLKIT_HILDON2
267         return hildon_entry_get_text (HILDON_ENTRY (widget));
268 #else
269         return gtk_entry_set_text (GTK_ENTRY (widget));
270 #endif
271 }
272
273 void 
274 modest_entry_set_hint (GtkWidget *widget, const gchar *hint)
275 {
276 #ifdef MODEST_TOOLKIT_HILDON2
277         hildon_entry_set_placeholder (HILDON_ENTRY (widget), hint);
278 #else
279         gtk_widget_set_tooltip_text (widget, hint);
280 #endif
281 }
282
283 gboolean
284 modest_is_entry (GtkWidget *widget)
285 {
286 #ifdef MODEST_TOOLKIT_HILDON2
287         return HILDON_IS_ENTRY (widget);
288 #else
289         return GTK_IS_ENTRY (widget);
290 #endif
291 }
292
293 GtkWidget *
294 modest_toolkit_factory_create_number_entry (ModestToolkitFactory *self, gint min, gint max)
295 {
296         return MODEST_TOOLKIT_FACTORY_GET_CLASS (self)->create_number_entry (self, min, max);
297 }
298
299 static GtkWidget *
300 modest_toolkit_factory_create_number_entry_default (ModestToolkitFactory *self, gint min, gint max)
301 {
302         GtkWidget *result;
303 #ifdef USE_GTK_SPIN_BUTTON
304         result = gtk_spin_button_new_with_range (min, max, 1.0);
305         gtk_spin_button_set_digits (GTK_SPIN_BUTTON (result), 0);
306 #else
307         result = modest_number_editor_new (min, max);
308 #endif
309         return result;
310 }
311
312 void
313 modest_number_entry_set_value (GtkWidget *widget, gint value)
314 {
315 #ifdef USE_GTK_SPIN_BUTTON
316         gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), value);
317 #else
318         modest_number_editor_set_value (MODEST_NUMBER_EDITOR (widget), value);
319 #endif
320 }
321
322 gint
323 modest_number_entry_get_value (GtkWidget *widget)
324 {
325 #ifdef USE_GTK_SPIN_BUTTON
326         return gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (widget));
327 #else
328         return modest_number_editor_get_value (MODEST_NUMBER_EDITOR (widget));
329 #endif
330 }
331
332 gboolean 
333 modest_number_entry_is_valid (GtkWidget *widget)
334 {
335 #ifdef USE_GTK_SPIN_BUTTON
336         return TRUE;
337 #else
338         return modest_number_editor_is_valid (MODEST_NUMBER_EDITOR (widget));
339 #endif
340 }
341
342 gboolean
343 modest_is_number_entry (GtkWidget *widget)
344 {
345 #ifdef USE_GTK_SPIN_BUTTON
346         return GTK_IS_SPIN_BUTTON (widget);
347 #else
348         return MODEST_IS_NUMBER_EDITOR (widget);
349 #endif
350 }
351
352 GtkWidget *
353 modest_toolkit_factory_create_file_chooser_dialog (ModestToolkitFactory *self,
354                                                    const gchar *title,
355                                                    GtkWindow *parent,
356                                                    GtkFileChooserAction action)
357 {
358         return MODEST_TOOLKIT_FACTORY_GET_CLASS (self)->create_file_chooser_dialog (self, title, parent, action);
359 }
360
361 static GtkWidget *
362 modest_toolkit_factory_create_file_chooser_dialog_default (ModestToolkitFactory *self,
363                                                            const gchar *title,
364                                                            GtkWindow *parent,
365                                                            GtkFileChooserAction action)
366 {
367         GtkWidget *result;
368 #ifdef USE_GTK_FILE_CHOOSER
369         result = gtk_file_chooser_dialog_new (title, parent, action,
370                                               (action == GTK_FILE_CHOOSER_ACTION_OPEN) ? GTK_STOCK_OPEN : GTK_STOCK_SAVE,
371                                               GTK_RESPONSE_OK,
372                                               GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
373                                               NULL);
374 #else
375         result = hildon_file_chooser_dialog_new (parent, action);
376         gtk_window_set_title ((GtkWindow *) result, title);
377 #endif
378         return result;
379 }
380
381 GtkWidget *
382 modest_toolkit_factory_create_country_selector (ModestToolkitFactory *self)
383 {
384         return MODEST_TOOLKIT_FACTORY_GET_CLASS (self)->create_country_selector (self);
385 }
386
387 static GtkWidget *
388 modest_toolkit_factory_create_country_selector_default (ModestToolkitFactory *self)
389 {
390         GtkWidget *result;
391 #ifdef USE_COUNTRY_COMBOBOX
392         result = modest_country_combo_box_new ();
393 #else
394         result = GTK_WIDGET (modest_country_picker_new (MODEST_EDITABLE_SIZE, 
395                                                         HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
396 #endif
397         return result;
398 }
399
400 gint
401 modest_country_selector_get_active_country_mcc (GtkWidget *widget)
402 {
403 #ifdef USE_COUNTRY_COMBOBOX
404         return modest_country_combo_box_get_active_country_mcc (MODEST_COUNTRY_COMBO_BOX (widget));
405 #else
406         return modest_country_picker_get_active_country_mcc (MODEST_COUNTRY_PICKER (widget));
407 #endif
408 }
409
410 void
411 modest_country_selector_load_data (GtkWidget *widget)
412 {
413 #ifdef USE_COUNTRY_COMBOBOX
414         modest_country_combo_box_load_data (MODEST_COUNTRY_COMBO_BOX (widget));
415 #else
416         modest_country_picker_load_data (MODEST_COUNTRY_PICKER (widget));
417 #endif
418 }
419
420 gboolean
421 modest_country_selector_set_active_country_locale (GtkWidget *widget)
422 {
423 #ifdef USE_COUNTRY_COMBOBOX
424         return modest_country_combo_box_set_active_country_locale (MODEST_COUNTRY_COMBO_BOX (widget));
425 #else
426         return modest_country_picker_set_active_country_locale (MODEST_COUNTRY_PICKER (widget));
427 #endif
428 }