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