Add modest_entry API for refactoring access to hildon entry
[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 #endif
34 #include "modest-toolkit-factory.h"
35
36 #ifndef MODEST_TOOLKIT_HILDON2
37 #define USE_SCROLLED_WINDOW
38 #define USE_GTK_FIND_TOOLBAR
39 #define USE_GTK_CHECK_BUTTON
40 #define USE_GTK_CHECK_MENU
41 #define USE_GTK_ENTRY
42 #endif
43
44 #ifdef USE_SCROLLED_WINDOW
45 #include <modest-scrolled-window-scrollable.h>
46 #else
47 #include <modest-hildon-pannable-area-scrollable.h>
48 #endif
49
50 #ifdef USE_GTK_TOOLBAR
51 #include <modest-find-toolbar.h>
52 #else
53 #include <modest-hildon-find-toolbar.h>
54 #endif
55
56 static void modest_toolkit_factory_class_init (ModestToolkitFactoryClass *klass);
57 static void modest_toolkit_factory_init (ModestToolkitFactory *self);
58
59 /* GObject interface */
60 static GtkWidget * modest_toolkit_factory_create_scrollable_default (ModestToolkitFactory *self);
61 static GtkWidget * modest_toolkit_factory_create_check_button_default (ModestToolkitFactory *self, const gchar *label);
62 static GtkWidget * modest_toolkit_factory_create_check_menu_default (ModestToolkitFactory *self, const gchar *label);
63 static GtkWidget * modest_toolkit_factory_create_isearch_toolbar_default (ModestToolkitFactory *self, const gchar *label);
64 static GtkWidget * modest_toolkit_factory_create_entry_default (ModestToolkitFactory *self);
65 /* globals */
66 static GObjectClass *parent_class = NULL;
67
68 G_DEFINE_TYPE    (ModestToolkitFactory,
69                   modest_toolkit_factory,
70                   G_TYPE_OBJECT);
71
72 ModestToolkitFactory *
73 modest_toolkit_factory_get_instance                            (void)
74 {
75     GObject* self = g_object_new (MODEST_TYPE_TOOLKIT_FACTORY, NULL);
76
77     return (ModestToolkitFactory *) self;
78 }
79
80 static void
81 modest_toolkit_factory_class_init (ModestToolkitFactoryClass *klass)
82 {
83         parent_class = g_type_class_peek_parent (klass);
84
85         klass->create_scrollable = modest_toolkit_factory_create_scrollable_default;
86         klass->create_check_button = modest_toolkit_factory_create_check_button_default;
87         klass->create_check_menu = modest_toolkit_factory_create_check_menu_default;
88         klass->create_isearch_toolbar = modest_toolkit_factory_create_isearch_toolbar_default;
89         klass->create_entry = modest_toolkit_factory_create_entry_default;
90 }
91
92 static void
93 modest_toolkit_factory_init (ModestToolkitFactory *self)
94 {
95 }
96
97 GtkWidget *
98 modest_toolkit_factory_create_scrollable (ModestToolkitFactory *self)
99 {
100         return MODEST_TOOLKIT_FACTORY_GET_CLASS (self)->create_scrollable (self);
101 }
102
103 static GtkWidget *
104 modest_toolkit_factory_create_scrollable_default (ModestToolkitFactory *self)
105 {
106 #ifdef USE_SCROLLED_WINDOW
107         return modest_scrolled_window_scrollable_new ();
108 #else
109         return modest_hildon_pannable_area_scrollable_new ();
110 #endif
111 }
112
113 GtkWidget *
114 modest_toolkit_factory_create_check_button (ModestToolkitFactory *self, const gchar *label)
115 {
116         return MODEST_TOOLKIT_FACTORY_GET_CLASS (self)->create_check_button (self, label);
117 }
118
119 static GtkWidget *
120 modest_toolkit_factory_create_check_button_default (ModestToolkitFactory *self, const gchar *label)
121 {
122         GtkWidget *result;
123 #ifdef USE_GTK_CHECK_BUTTON
124         result = gtk_check_button_new_with_label (label);
125 #else
126         result = hildon_check_button_new (HILDON_SIZE_FINGER_HEIGHT);
127         gtk_button_set_label (GTK_BUTTON (result), label);
128         gtk_button_set_alignment (GTK_BUTTON (result), 0.0, 0.5);
129 #endif
130         return result;
131 }
132
133 GtkWidget *
134 modest_toolkit_factory_create_check_menu (ModestToolkitFactory *self, const gchar *label)
135 {
136         return MODEST_TOOLKIT_FACTORY_GET_CLASS (self)->create_check_menu (self, label);
137 }
138
139 static GtkWidget *
140 modest_toolkit_factory_create_check_menu_default (ModestToolkitFactory *self, const gchar *label)
141 {
142         GtkWidget *result;
143 #ifdef USE_GTK_CHECK_MENU
144         result = gtk_check_menu_item_new_with_label (label);
145 #else
146         result = hildon_check_button_new (0);
147         gtk_button_set_label (GTK_BUTTON (result), label);
148         gtk_button_set_alignment (GTK_BUTTON (result), 0.5, 0.5);
149 #endif
150         return result;
151 }
152
153 gboolean
154 modest_togglable_get_active (GtkWidget *widget)
155 {
156         if (GTK_IS_CHECK_MENU_ITEM (widget)) {
157                 return gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget));
158         } else if (GTK_IS_TOGGLE_BUTTON (widget)) {
159                 return gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
160 #ifdef MODEST_TOOLKIT_HILDON2
161         } else if (HILDON_IS_CHECK_BUTTON (widget)) {
162                 return hildon_check_button_get_active (HILDON_CHECK_BUTTON (widget));
163 #endif
164         } else {
165                 g_return_val_if_reached (FALSE);
166         }
167 }
168
169 void
170 modest_togglable_set_active (GtkWidget *widget, gboolean active)
171 {
172         if (GTK_IS_CHECK_MENU_ITEM (widget)) {
173                 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (widget), active);
174         } else if (GTK_IS_TOGGLE_BUTTON (widget)) {
175                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), active);
176 #ifdef MODEST_TOOLKIT_HILDON2
177         } else if (HILDON_IS_CHECK_BUTTON (widget)) {
178                 hildon_check_button_set_active (HILDON_CHECK_BUTTON (widget), active);
179 #endif
180         }
181 }
182
183 gboolean
184 modest_is_togglable (GtkWidget *widget)
185 {
186         return GTK_IS_CHECK_MENU_ITEM (widget) 
187                 || GTK_IS_TOGGLE_BUTTON (widget)
188 #ifdef MODEST_TOOLKIT_HILDON2
189                 || HILDON_IS_CHECK_BUTTON (widget)
190 #endif
191                 ;
192 }
193
194 GtkWidget *
195 modest_toolkit_factory_create_isearch_toolbar (ModestToolkitFactory *self, const gchar *label)
196 {
197         return MODEST_TOOLKIT_FACTORY_GET_CLASS (self)->create_isearch_toolbar (self, label);
198 }
199
200 static GtkWidget *
201 modest_toolkit_factory_create_isearch_toolbar_default (ModestToolkitFactory *self, const gchar *label)
202 {
203         GtkWidget *result;
204 #ifdef USE_GTK_FIND_TOOLBAR
205         result = modest_find_toolbar_new (label);
206 #else
207         result = modest_hildon_find_toolbar_new (label);
208 #endif
209         return result;
210 }
211
212 GtkWidget *
213 modest_toolkit_factory_create_entry (ModestToolkitFactory *self)
214 {
215         return MODEST_TOOLKIT_FACTORY_GET_CLASS (self)->create_entry (self);
216 }
217
218 static GtkWidget *
219 modest_toolkit_factory_create_entry_default (ModestToolkitFactory *self)
220 {
221 #ifdef USE_GTK_ENTRY
222         return gtk_entry_new ();
223 #else
224         return hildon_entry_new (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
225 #endif
226 }
227
228 void
229 modest_entry_set_text (GtkWidget *widget, const gchar *text)
230 {
231 #ifdef MODEST_TOOLKIT_HILDON2
232         hildon_entry_set_text (HILDON_ENTRY (widget), text);
233 #else
234         gtk_entry_set_text (GTK_ENTRY (widget), text);
235 #endif
236 }
237
238 const gchar *
239 modest_entry_get_text (GtkWidget *widget)
240 {
241 #ifdef MODEST_TOOLKIT_HILDON2
242         return hildon_entry_get_text (HILDON_ENTRY (widget));
243 #else
244         return gtk_entry_set_text (GTK_ENTRY (widget));
245 #endif
246 }
247
248 void 
249 modest_entry_set_hint (GtkWidget *widget, const gchar *hint)
250 {
251 #ifdef MODEST_TOOLKIT_HILDON2
252         hildon_entry_set_placeholder (HILDON_ENTRY (widget), hint);
253 #else
254         gtk_widget_set_tooltip_text (widget, hint);
255 #endif
256 }
257
258 gboolean
259 modest_is_entry (GtkWidget *widget)
260 {
261 #ifdef MODEST_TOOLKIT_HILDON2
262         return HILDON_IS_ENTRY (widget);
263 #else
264         return GTK_IS_ENTRY (widget);
265 #endif
266 }
267