Tizen 2.1 release
[platform/core/uifw/e17.git] / src / bin / e_widget_check.c
1 #include "e.h"
2
3 typedef struct _E_Widget_Data E_Widget_Data;
4 struct _E_Widget_Data
5 {
6    Evas_Object *o_check;
7    Evas_Object *o_icon;
8    int         *valptr;
9 };
10
11 static void _e_wid_del_hook(Evas_Object *obj);
12 static void _e_wid_focus_hook(Evas_Object *obj);
13 static void _e_wid_do(Evas_Object *obj);
14 static void _e_wid_activate_hook(Evas_Object *obj);
15 static void _e_wid_disable_hook(Evas_Object *obj);
16 static void _e_wid_signal_cb1(void *data, Evas_Object *obj, const char *emission, const char *source);
17 static void _e_wid_focus_steal(void *data, Evas *e, Evas_Object *obj, void *event_info);
18
19 /* local subsystem functions */
20
21 /* externally accessible functions */
22
23 /**  
24  * Creates a check box widget
25  *
26  * @param evas pointer
27  * @param label the text to asign to check widget
28  * @param val pointer to int where changes are stored
29  * @return the newly created evas object
30  */
31 EAPI Evas_Object *
32 e_widget_check_add(Evas *evas, const char *label, int *val)
33 {
34    Evas_Object *obj, *o;
35    E_Widget_Data *wd;
36    Evas_Coord mw, mh;
37
38    obj = e_widget_add(evas);
39
40    e_widget_del_hook_set(obj, _e_wid_del_hook);
41    e_widget_focus_hook_set(obj, _e_wid_focus_hook);
42    e_widget_activate_hook_set(obj, _e_wid_activate_hook);
43    e_widget_disable_hook_set(obj, _e_wid_disable_hook);
44    wd = calloc(1, sizeof(E_Widget_Data));
45    wd->valptr = val;
46    e_widget_data_set(obj, wd);
47
48    o = edje_object_add(evas);
49    wd->o_check = o;
50    e_theme_edje_object_set(o, "base/theme/widgets",
51                            "e/widgets/check");
52    edje_object_signal_callback_add(o, "e,action,toggle", "*", _e_wid_signal_cb1, obj);
53    edje_object_part_text_set(o, "e.text.label", label);
54    evas_object_show(o);
55    edje_object_size_min_calc(o, &mw, &mh);
56    e_widget_size_min_set(obj, mw, mh);
57    if (wd->valptr)
58      {
59         if (*(wd->valptr)) edje_object_signal_emit(o, "e,state,checked", "e");
60      }
61
62    e_widget_sub_object_add(obj, o);
63    evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_DOWN, _e_wid_focus_steal, obj);
64    e_widget_resize_object_set(obj, o);
65
66    return obj;
67 }
68
69 /**  
70  * Sets the value of the check box
71  *
72  * @param check the check box widget
73  * @param checked the value to set the widget too
74  */
75 EAPI void
76 e_widget_check_checked_set(Evas_Object *check, int checked)
77 {
78    E_Widget_Data *wd;
79
80    wd = e_widget_data_get(check);
81    if (wd->valptr)
82      *(wd->valptr) = checked;
83    if (checked)
84      edje_object_signal_emit(wd->o_check, "e,state,checked", "e");
85    else
86      edje_object_signal_emit(wd->o_check, "e,state,unchecked", "e");
87 }
88
89 /**  
90  * Sets the value of the check box
91  *
92  * @param check the check box widget
93  * @param val pointer to int where changes are stored
94  */
95 EAPI void
96 e_widget_check_valptr_set(Evas_Object *check, int *val)
97 {
98    E_Widget_Data *wd;
99    int state;
100
101    wd = e_widget_data_get(check);
102    state = wd->valptr ? *wd->valptr : 0;
103    wd->valptr = val;
104    if (state == (wd->valptr && *wd->valptr)) return;
105    if (wd->valptr && *wd->valptr)
106      edje_object_signal_emit(wd->o_check, "e,state,checked", "e");
107    else
108      edje_object_signal_emit(wd->o_check, "e,state,unchecked", "e");
109 }
110
111 /**  
112  *Get the value of the check box
113  *
114  * @param check the check box widget
115  * @return the value of the check box
116  */
117 EAPI int
118 e_widget_check_checked_get(Evas_Object *check)
119 {
120    E_Widget_Data *wd;
121    int ret;
122
123    wd = e_widget_data_get(check);
124    if (wd->valptr)
125      ret = *(wd->valptr);
126    else
127      ret = -1;
128
129    return ret;
130 }
131
132 /**  
133  * Creates a check box widget with icon
134  *
135  * @param evas pointer
136  * @param label the text to asign to check widget
137  * @param icon the path to the icon file
138  * @param icon_w the minimum wdith of the icon
139  * @param icon_h the minumum height of the icon
140  * @param val pointer to int where changes are stored
141  * @return the newly created evas object
142  */
143 EAPI Evas_Object *
144 e_widget_check_icon_add(Evas *evas, const char *label, const char *icon, int icon_w, int icon_h, int *val)
145 {
146    Evas_Object *obj, *o, *o2;
147    E_Widget_Data *wd;
148    Evas_Coord mw, mh;
149
150    obj = e_widget_add(evas);
151
152    e_widget_del_hook_set(obj, _e_wid_del_hook);
153    e_widget_focus_hook_set(obj, _e_wid_focus_hook);
154    e_widget_activate_hook_set(obj, _e_wid_activate_hook);
155    e_widget_disable_hook_set(obj, _e_wid_disable_hook);
156    wd = calloc(1, sizeof(E_Widget_Data));
157    wd->valptr = val;
158    e_widget_data_set(obj, wd);
159
160    o = edje_object_add(evas);
161    wd->o_check = o;
162    e_theme_edje_object_set(o, "base/theme/widgets",
163                            "e/widgets/check_icon");
164    edje_object_signal_callback_add(o, "e,action,toggle", "*", _e_wid_signal_cb1, obj);
165    edje_object_part_text_set(o, "e.text.label", label);
166    evas_object_show(o);
167    if (label)
168      {
169         edje_object_signal_emit(o, "e,state,labeled", "e");
170         edje_object_message_signal_process(o);
171      }
172    if (icon)
173      {
174         if (icon[0] == '/')
175           {
176              o2 = e_icon_add(evas);
177              e_icon_file_set(o2, icon);
178           }
179         else
180           {
181              o2 = edje_object_add(evas);
182              e_util_edje_icon_set(o2, icon);
183           }
184         edje_extern_object_min_size_set(o2, icon_w, icon_h);
185         edje_object_part_swallow(wd->o_check, "e.swallow.icon", o2);
186         evas_object_show(o2);
187         e_widget_sub_object_add(obj, o2);
188         wd->o_icon = o2;
189      }
190
191    edje_object_size_min_calc(o, &mw, &mh);
192    e_widget_size_min_set(obj, mw, mh);
193    if (wd->valptr)
194      {
195         if (*(wd->valptr)) edje_object_signal_emit(o, "e,state,checked", "e");
196      }
197
198    e_widget_sub_object_add(obj, o);
199    evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_DOWN, _e_wid_focus_steal, obj);
200    e_widget_resize_object_set(obj, o);
201
202    return obj;
203 }
204
205 static void
206 _e_wid_del_hook(Evas_Object *obj)
207 {
208    E_Widget_Data *wd;
209
210    wd = e_widget_data_get(obj);
211    free(wd);
212 }
213
214 static void
215 _e_wid_focus_hook(Evas_Object *obj)
216 {
217    E_Widget_Data *wd;
218
219    wd = e_widget_data_get(obj);
220    if (e_widget_focus_get(obj))
221      {
222         edje_object_signal_emit(wd->o_check, "e,state,focused", "e");
223         evas_object_focus_set(wd->o_check, 1);
224      }
225    else
226      {
227         edje_object_signal_emit(wd->o_check, "e,state,unfocused", "e");
228         evas_object_focus_set(wd->o_check, 0);
229      }
230 }
231
232 static void
233 _e_wid_do(Evas_Object *obj)
234 {
235    E_Widget_Data *wd;
236
237    if (e_widget_disabled_get(obj)) return;
238
239    wd = e_widget_data_get(obj);
240    if (wd->valptr)
241      {
242         if (*(wd->valptr) == 0)
243           {
244              *(wd->valptr) = 1;
245              edje_object_signal_emit(wd->o_check, "e,state,checked", "e");
246           }
247         else
248           {
249              *(wd->valptr) = 0;
250              edje_object_signal_emit(wd->o_check, "e,state,unchecked", "e");
251           }
252      }
253    evas_object_smart_callback_call(obj, "changed", NULL);
254    e_widget_change(obj);
255 }
256
257 static void
258 _e_wid_activate_hook(Evas_Object *obj)
259 {
260    _e_wid_do(obj);
261 }
262
263 static void
264 _e_wid_disable_hook(Evas_Object *obj)
265 {
266    E_Widget_Data *wd;
267
268    wd = e_widget_data_get(obj);
269    if (e_widget_disabled_get(obj))
270      edje_object_signal_emit(wd->o_check, "e,state,disabled", "e");
271    else
272      edje_object_signal_emit(wd->o_check, "e,state,enabled", "e");
273 }
274
275 static void
276 _e_wid_signal_cb1(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
277 {
278    _e_wid_do(data);
279 }
280
281 static void
282 _e_wid_focus_steal(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
283 {
284    e_widget_focus_steal(data);
285 }
286