Refactor check conditions for building modest toolkit factory specific
[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 #endif
42
43 #ifdef USE_SCROLLED_WINDOW
44 #include <modest-scrolled-window-scrollable.h>
45 #else
46 #include <modest-hildon-pannable-area-scrollable.h>
47 #endif
48
49 #ifdef USE_GTK_TOOLBAR
50 #include <modest-find-toolbar.h>
51 #else
52 #include <modest-hildon-find-toolbar.h>
53 #endif
54
55 static void modest_toolkit_factory_class_init (ModestToolkitFactoryClass *klass);
56 static void modest_toolkit_factory_init (ModestToolkitFactory *self);
57
58 /* GObject interface */
59 static GtkWidget * modest_toolkit_factory_create_scrollable_default (ModestToolkitFactory *self);
60 static GtkWidget * modest_toolkit_factory_create_check_button_default (ModestToolkitFactory *self, const gchar *label);
61 static GtkWidget * modest_toolkit_factory_create_check_menu_default (ModestToolkitFactory *self, const gchar *label);
62 static GtkWidget * modest_toolkit_factory_create_isearch_toolbar_default (ModestToolkitFactory *self, const gchar *label);
63 /* globals */
64 static GObjectClass *parent_class = NULL;
65
66 G_DEFINE_TYPE    (ModestToolkitFactory,
67                   modest_toolkit_factory,
68                   G_TYPE_OBJECT);
69
70 ModestToolkitFactory *
71 modest_toolkit_factory_get_instance                            (void)
72 {
73     GObject* self = g_object_new (MODEST_TYPE_TOOLKIT_FACTORY, NULL);
74
75     return (ModestToolkitFactory *) self;
76 }
77
78 static void
79 modest_toolkit_factory_class_init (ModestToolkitFactoryClass *klass)
80 {
81         parent_class = g_type_class_peek_parent (klass);
82
83         klass->create_scrollable = modest_toolkit_factory_create_scrollable_default;
84         klass->create_check_button = modest_toolkit_factory_create_check_button_default;
85         klass->create_check_menu = modest_toolkit_factory_create_check_menu_default;
86         klass->create_isearch_toolbar = modest_toolkit_factory_create_isearch_toolbar_default;
87 }
88
89 static void
90 modest_toolkit_factory_init (ModestToolkitFactory *self)
91 {
92 }
93
94 GtkWidget *
95 modest_toolkit_factory_create_scrollable (ModestToolkitFactory *self)
96 {
97         return MODEST_TOOLKIT_FACTORY_GET_CLASS (self)->create_scrollable (self);
98 }
99
100 static GtkWidget *
101 modest_toolkit_factory_create_scrollable_default (ModestToolkitFactory *self)
102 {
103 #ifdef USE_SCROLLED_WINDOW
104         return modest_scrolled_window_scrollable_new ();
105 #else
106         return modest_hildon_pannable_area_scrollable_new ();
107 #endif
108 }
109
110 GtkWidget *
111 modest_toolkit_factory_create_check_button (ModestToolkitFactory *self, const gchar *label)
112 {
113         return MODEST_TOOLKIT_FACTORY_GET_CLASS (self)->create_check_button (self, label);
114 }
115
116 static GtkWidget *
117 modest_toolkit_factory_create_check_button_default (ModestToolkitFactory *self, const gchar *label)
118 {
119         GtkWidget *result;
120 #ifdef USE_GTK_CHECK_BUTTON
121         result = gtk_check_button_new_with_label (label);
122 #else
123         result = hildon_check_button_new (HILDON_SIZE_FINGER_HEIGHT);
124         gtk_button_set_label (GTK_BUTTON (result), label);
125         gtk_button_set_alignment (GTK_BUTTON (result), 0.0, 0.5);
126 #endif
127         return result;
128 }
129
130 GtkWidget *
131 modest_toolkit_factory_create_check_menu (ModestToolkitFactory *self, const gchar *label)
132 {
133         return MODEST_TOOLKIT_FACTORY_GET_CLASS (self)->create_check_menu (self, label);
134 }
135
136 static GtkWidget *
137 modest_toolkit_factory_create_check_menu_default (ModestToolkitFactory *self, const gchar *label)
138 {
139         GtkWidget *result;
140 #ifdef USE_GTK_CHECK_MENU
141         result = gtk_check_menu_item_new_with_label (label);
142 #else
143         result = hildon_check_button_new (0);
144         gtk_button_set_label (GTK_BUTTON (result), label);
145         gtk_button_set_alignment (GTK_BUTTON (result), 0.5, 0.5);
146 #endif
147         return result;
148 }
149
150 gboolean
151 modest_togglable_get_active (GtkWidget *widget)
152 {
153         if (GTK_IS_CHECK_MENU_ITEM (widget)) {
154                 return gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget));
155         } else if (GTK_IS_TOGGLE_BUTTON (widget)) {
156                 return gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
157 #ifdef MODEST_TOOLKIT_HILDON2
158         } else if (HILDON_IS_CHECK_BUTTON (widget)) {
159                 return hildon_check_button_get_active (HILDON_CHECK_BUTTON (widget));
160 #endif
161         } else {
162                 g_return_val_if_reached (FALSE);
163         }
164 }
165
166 void
167 modest_togglable_set_active (GtkWidget *widget, gboolean active)
168 {
169         if (GTK_IS_CHECK_MENU_ITEM (widget)) {
170                 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (widget), active);
171         } else if (GTK_IS_TOGGLE_BUTTON (widget)) {
172                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), active);
173 #ifdef MODEST_TOOLKIT_HILDON2
174         } else if (HILDON_IS_CHECK_BUTTON (widget)) {
175                 hildon_check_button_set_active (HILDON_CHECK_BUTTON (widget), active);
176 #endif
177         }
178 }
179
180 gboolean
181 modest_is_togglable (GtkWidget *widget)
182 {
183         return GTK_IS_CHECK_MENU_ITEM (widget) 
184                 || GTK_IS_TOGGLE_BUTTON (widget)
185 #ifdef MODEST_TOOLKIT_HILDON2
186                 || HILDON_IS_CHECK_BUTTON (widget)
187 #endif
188                 ;
189 }
190
191 GtkWidget *
192 modest_toolkit_factory_create_isearch_toolbar (ModestToolkitFactory *self, const gchar *label)
193 {
194         return MODEST_TOOLKIT_FACTORY_GET_CLASS (self)->create_isearch_toolbar (self, label);
195 }
196
197 static GtkWidget *
198 modest_toolkit_factory_create_isearch_toolbar_default (ModestToolkitFactory *self, const gchar *label)
199 {
200         GtkWidget *result;
201 #ifdef USE_GTK_FIND_TOOLBAR
202         result = modest_find_toolbar_new (label);
203 #else
204         result = modest_hildon_find_toolbar_new (label);
205 #endif
206         return result;
207 }
208