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