Drop usage of HildonCheckButton in settings dialogs.
[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 <modest-hildon-pannable-area-scrollable.h>
33 #include <hildon/hildon.h>
34 #else
35 #include <modest-scrolled-window-scrollable.h>
36 #endif
37 #include "modest-toolkit-factory.h"
38
39 static void modest_toolkit_factory_class_init (ModestToolkitFactoryClass *klass);
40 static void modest_toolkit_factory_init (ModestToolkitFactory *self);
41
42 /* GObject interface */
43 static GtkWidget * modest_toolkit_factory_create_scrollable_default (ModestToolkitFactory *self);
44 static GtkWidget * modest_toolkit_factory_create_check_button_default (ModestToolkitFactory *self, const gchar *label);
45 static GtkWidget * modest_toolkit_factory_create_check_menu_default (ModestToolkitFactory *self, const gchar *label);
46 /* globals */
47 static GObjectClass *parent_class = NULL;
48
49 G_DEFINE_TYPE    (ModestToolkitFactory,
50                   modest_toolkit_factory,
51                   G_TYPE_OBJECT);
52
53 ModestToolkitFactory *
54 modest_toolkit_factory_get_instance                            (void)
55 {
56     GObject* self = g_object_new (MODEST_TYPE_TOOLKIT_FACTORY, NULL);
57
58     return (ModestToolkitFactory *) self;
59 }
60
61 static void
62 modest_toolkit_factory_class_init (ModestToolkitFactoryClass *klass)
63 {
64         parent_class = g_type_class_peek_parent (klass);
65
66         klass->create_scrollable = modest_toolkit_factory_create_scrollable_default;
67         klass->create_check_button = modest_toolkit_factory_create_check_button_default;
68         klass->create_check_menu = modest_toolkit_factory_create_check_menu_default;
69 }
70
71 static void
72 modest_toolkit_factory_init (ModestToolkitFactory *self)
73 {
74 }
75
76 GtkWidget *
77 modest_toolkit_factory_create_scrollable (ModestToolkitFactory *self)
78 {
79         return MODEST_TOOLKIT_FACTORY_GET_CLASS (self)->create_scrollable (self);
80 }
81
82 static GtkWidget *
83 modest_toolkit_factory_create_scrollable_default (ModestToolkitFactory *self)
84 {
85 #ifdef MODEST_TOOLKIT_HILDON2
86         return modest_hildon_pannable_area_scrollable_new ();
87 #else
88         return modest_scrolled_window_scrollable_new ();
89 #endif
90 }
91
92 GtkWidget *
93 modest_toolkit_factory_create_check_button (ModestToolkitFactory *self, const gchar *label)
94 {
95         return MODEST_TOOLKIT_FACTORY_GET_CLASS (self)->create_check_button (self, label);
96 }
97
98 static GtkWidget *
99 modest_toolkit_factory_create_check_button_default (ModestToolkitFactory *self, const gchar *label)
100 {
101         GtkWidget *result;
102 #ifdef MODEST_TOOLKIT_HILDON2
103         result = hildon_check_button_new (HILDON_SIZE_FINGER_HEIGHT);
104         gtk_button_set_label (GTK_BUTTON (result), label);
105         gtk_button_set_alignment (GTK_BUTTON (result), 0.0, 0.5);
106 #else
107         result = gtk_check_button_new_with_label (label);
108 #endif
109         return result;
110 }
111 GtkWidget *
112 modest_toolkit_factory_create_check_menu (ModestToolkitFactory *self, const gchar *label)
113 {
114         return MODEST_TOOLKIT_FACTORY_GET_CLASS (self)->create_check_menu (self, label);
115 }
116
117 static GtkWidget *
118 modest_toolkit_factory_create_check_menu_default (ModestToolkitFactory *self, const gchar *label)
119 {
120         GtkWidget *result;
121 #ifdef MODEST_TOOLKIT_HILDON2
122         result = hildon_check_button_new (0);
123         gtk_button_set_label (GTK_BUTTON (result), label);
124         gtk_button_set_alignment (GTK_BUTTON (result), 0.5, 0.5);
125 #else
126         result = gtk_check_menu_item_new_with_label (label);
127 #endif
128         return result;
129 }
130
131 gboolean
132 modest_togglable_get_active (GtkWidget *widget)
133 {
134 #ifdef MODEST_TOOLKIT_HILDON2
135         return hildon_check_button_get_active (HILDON_CHECK_BUTTON (widget));
136 #else
137         if (GTK_IS_CHECK_MENU_ITEM (widget)) {
138                 return gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget));
139         } else if (GTK_IS_TOGGLE_BUTTON (widget)) {
140                 return gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
141         }
142 #endif
143 }
144
145 void
146 modest_togglable_set_active (GtkWidget *widget, gboolean active)
147 {
148 #ifdef MODEST_TOOLKIT_HILDON2
149         hildon_check_button_set_active (HILDON_CHECK_BUTTON (widget), active);
150 #else
151         if (GTK_IS_CHECK_MENU_ITEM (widget)) {
152                 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (widget), active);
153         } else if (GTK_IS_TOGGLE_BUTTON (widget)) {
154                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), active);
155         }
156 #endif
157 }
158
159 gboolean
160 modest_is_togglable (GtkWidget *widget)
161 {
162 #ifdef MODEST_TOOLKIT_HILDON2
163         return HILDON_IS_CHECK_BUTTON (widget);
164 #else
165         return GTK_IS_CHECK_MENU_ITEM (widget) || GTK_IS_TOGGLE_BUTTON (widget);
166 #endif
167 }