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