[SVN Migration r61148]
[framework/uifw/elementary.git] / src / lib / elm_check.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup Check Check
6  * @ingroup Elementary
7  *
8  * The check widget allows for toggling a value between true or false (1 or 0).
9  *
10  * Check objects are a lot like radio objects in layout and functionality
11  * except they do not work as a group, but independently and only toggle the
12  * value of a boolean from false to true (0 or 1). elm_check_state_set() sets
13  * the boolean state (1 for true, 0 for false), and elm_check_state_get()
14  * returns the current state. For convenience, like the radio objects, you
15  * can set a pointer to a boolean directly with elm_check_state_pointer_set()
16  * for it to modify.
17  *
18  * Signals that you can add callbacks for are:
19  *
20  * "changed" - This is called whenever the user changes the state of one of the
21  *             check object.
22  */
23 typedef struct _Widget_Data Widget_Data;
24
25 struct _Widget_Data
26 {
27    Evas_Object *chk, *icon;
28    Eina_Bool state;
29    Eina_Bool *statep;
30    const char *label;
31 };
32
33 static const char *widtype = NULL;
34 static void _del_hook(Evas_Object *obj);
35 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
36 static void _theme_hook(Evas_Object *obj);
37 static void _disable_hook(Evas_Object *obj);
38 static void _sizing_eval(Evas_Object *obj);
39 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
40 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
41 static void _signal_check_off(void *data, Evas_Object *obj, const char *emission, const char *source);
42 static void _signal_check_on(void *data, Evas_Object *obj, const char *emission, const char *source);
43 static void _signal_check_toggle(void *data, Evas_Object *obj, const char *emission, const char *source);
44 static void _on_focus_hook(void *data, Evas_Object *obj);
45 static void _activate_hook(Evas_Object *obj);
46 static void _activate(Evas_Object *obj);
47 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
48                              Evas_Callback_Type type, void *event_info);
49
50 static const char SIG_CHANGED[] = "changed";
51 static const Evas_Smart_Cb_Description _signals[] = {
52        {SIG_CHANGED, ""},
53        {NULL, NULL}
54 };
55
56 static Eina_Bool
57 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
58 {
59    if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
60    Evas_Event_Key_Down *ev = event_info;
61    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
62    if (elm_widget_disabled_get(obj)) return EINA_FALSE;
63    if ((strcmp(ev->keyname, "Return")) &&
64        (strcmp(ev->keyname, "KP_Enter")) &&
65        (strcmp(ev->keyname, "space")))
66      return EINA_FALSE;
67    _activate(obj);
68    ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
69    return EINA_TRUE;
70 }
71
72
73 static void
74 _del_hook(Evas_Object *obj)
75 {
76    Widget_Data *wd = elm_widget_data_get(obj);
77    if (!wd) return;
78    if (wd->label) eina_stringshare_del(wd->label);
79    free(wd);
80 }
81
82 static void
83 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
84 {
85    Widget_Data *wd = elm_widget_data_get(obj);
86    if (!wd) return;
87    if (elm_widget_focus_get(obj))
88      {
89         edje_object_signal_emit(wd->chk, "elm,action,focus", "elm");
90         evas_object_focus_set(wd->chk, EINA_TRUE);
91      }
92    else
93      {
94         edje_object_signal_emit(wd->chk, "elm,action,unfocus", "elm");
95         evas_object_focus_set(wd->chk, EINA_FALSE);
96      }
97 }
98
99 static void
100 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
101 {
102    Widget_Data *wd = elm_widget_data_get(obj);
103    if (!wd) return;
104    edje_object_mirrored_set(wd->chk, rtl);
105 }
106
107 static void
108 _theme_hook(Evas_Object *obj)
109 {
110    unsigned int counter = 0;
111    unsigned int i = 1;
112    unsigned int length = 0;
113    char *str = NULL;
114    char labels[128] ;
115    char buffer[PATH_MAX]={'\0',};
116    char s1[PATH_MAX] = {'\0',};
117    Widget_Data *wd = elm_widget_data_get(obj);
118    if (!wd) return;
119
120    _elm_widget_mirrored_reload(obj);
121    _mirrored_set(obj, elm_widget_mirrored_get(obj));
122    _elm_theme_object_set(obj, wd->chk, "check", "base", elm_widget_style_get(obj));
123    if (wd->icon)
124      edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
125    else
126      edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
127    if (wd->state)
128      edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
129    else
130      edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
131    if (wd->label)
132      edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
133    else
134      edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
135    edje_object_part_text_set(wd->chk, "elm.text", wd->label);
136    if (elm_widget_disabled_get(obj))
137      edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
138    edje_object_message_signal_process(wd->chk);
139    edje_object_scale_set(wd->chk, elm_widget_scale_get(obj) * _elm_config->scale);
140
141    //introduced internationalization of additional text parts used in style
142    while (1)
143      {
144         // s1 is  used  to store part name while buffer is used to store the part's value string
145         snprintf(labels,sizeof(labels),"label_%d",i++);
146         str = edje_object_data_get(wd->chk,labels);
147         if (!str) break;
148         length = strlen(str);
149         while ((str[counter]!= ' ') && (counter < length))
150           counter++;
151         if (counter == length)
152           continue;
153         strncpy(s1, str, counter);
154         s1[counter] = '\0';
155         strncpy(buffer, str + counter, sizeof(buffer));
156         edje_object_part_text_set(wd->chk, s1, E_(buffer));
157      }
158    _sizing_eval(obj);
159 }
160
161 static void
162 _disable_hook(Evas_Object *obj)
163 {
164    Widget_Data *wd = elm_widget_data_get(obj);
165    if (!wd) return;
166    if (elm_widget_disabled_get(obj))
167      edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
168    else
169      edje_object_signal_emit(wd->chk, "elm,state,enabled", "elm");
170 }
171
172 static void
173 _sizing_eval(Evas_Object *obj)
174 {
175    Widget_Data *wd = elm_widget_data_get(obj);
176    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
177    if (!wd) return;
178    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
179    edje_object_size_min_restricted_calc(wd->chk, &minw, &minh, minw, minh);
180    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
181    evas_object_size_hint_min_set(obj, minw, minh);
182    evas_object_size_hint_max_set(obj, maxw, maxh);
183 }
184
185 static void
186 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
187 {
188    Widget_Data *wd = elm_widget_data_get(data);
189    if (!wd) return;
190    if (obj != wd->icon) return;
191    Evas_Coord mw, mh;
192    evas_object_size_hint_min_get(obj, &mw, &mh);
193    _sizing_eval(data);
194 }
195
196 static void
197 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
198 {
199    Widget_Data *wd = elm_widget_data_get(obj);
200    Evas_Object *sub = event_info;
201    if (!wd) return;
202    if (sub == wd->icon)
203      {
204         edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
205         evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
206                                             _changed_size_hints, obj);
207         wd->icon = NULL;
208         _sizing_eval(obj);
209         edje_object_message_signal_process(wd->chk);
210      }
211 }
212
213 static void
214 _signal_check_off(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
215 {
216    Widget_Data *wd = elm_widget_data_get(data);
217    if (!wd) return;
218    wd->state = EINA_FALSE;
219    if (wd->statep) *wd->statep = wd->state;
220    edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
221    evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
222 }
223
224 static void
225 _signal_check_on(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
226 {
227    Widget_Data *wd = elm_widget_data_get(data);
228    if (!wd) return;
229    wd->state = EINA_TRUE;
230    if (wd->statep) *wd->statep = wd->state;
231    edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
232    evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
233 }
234
235 static void
236 _signal_check_toggle(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
237 {
238    _activate(data);
239 }
240
241 static void
242 _activate_hook(Evas_Object *obj)
243 {
244    _activate(obj);
245 }
246
247 static void
248 _activate(Evas_Object *obj)
249 {
250    Widget_Data *wd = elm_widget_data_get(obj);
251    if (!wd) return;
252    wd->state = !wd->state;
253    if (wd->statep) *wd->statep = wd->state;
254    if (wd->state)
255      edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
256    else
257      edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
258    evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
259 }
260
261 static void
262 _elm_check_label_set(Evas_Object *obj, const char *item, const char *label)
263 {
264    ELM_CHECK_WIDTYPE(obj, widtype);
265    Widget_Data *wd = elm_widget_data_get(obj);
266    if (item && strcmp(item, "default")) return;
267    if (!wd) return;
268    eina_stringshare_replace(&wd->label, label);
269    if (label)
270      edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
271    else
272      edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
273    edje_object_message_signal_process(wd->chk);
274    edje_object_part_text_set(wd->chk, "elm.text", label);
275    _sizing_eval(obj);
276 }
277
278 static const char *
279 _elm_check_label_get(const Evas_Object *obj, const char *item)
280 {
281    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
282    Widget_Data *wd = elm_widget_data_get(obj);
283    if (item && strcmp(item, "default")) return NULL;
284    if (!wd) return NULL;
285    return wd->label;
286 }
287
288 /**
289  * Add a new Check object
290  *
291  * @param parent The parent object
292  * @return The new object or NULL if it cannot be created
293  *
294  * @ingroup Check
295  */
296 EAPI Evas_Object *
297 elm_check_add(Evas_Object *parent)
298 {
299    Evas_Object *obj;
300    Evas *e;
301    Widget_Data *wd;
302
303    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
304
305    ELM_SET_WIDTYPE(widtype, "check");
306    elm_widget_type_set(obj, "check");
307    elm_widget_sub_object_add(parent, obj);
308    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
309    elm_widget_data_set(obj, wd);
310    elm_widget_del_hook_set(obj, _del_hook);
311    elm_widget_theme_hook_set(obj, _theme_hook);
312    elm_widget_disable_hook_set(obj, _disable_hook);
313    elm_widget_can_focus_set(obj, EINA_TRUE);
314    elm_widget_activate_hook_set(obj, _activate_hook);
315    elm_widget_event_hook_set(obj, _event_hook);
316    elm_widget_text_set_hook_set(obj, _elm_check_label_set);
317    elm_widget_text_get_hook_set(obj, _elm_check_label_get);
318
319    wd->chk = edje_object_add(e);
320    _elm_theme_object_set(obj, wd->chk, "check", "base", "default");
321    edje_object_signal_callback_add(wd->chk, "elm,action,check,on", "",
322                                    _signal_check_on, obj);
323    edje_object_signal_callback_add(wd->chk, "elm,action,check,off", "",
324                                    _signal_check_off, obj);
325    edje_object_signal_callback_add(wd->chk, "elm,action,check,toggle", "",
326                                    _signal_check_toggle, obj);
327    elm_widget_resize_object_set(obj, wd->chk);
328
329    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
330
331    _mirrored_set(obj, elm_widget_mirrored_get(obj));
332    _sizing_eval(obj);
333
334    // TODO: convert Elementary to subclassing of Evas_Smart_Class
335    // TODO: and save some bytes, making descriptions per-class and not instance!
336    evas_object_smart_callbacks_descriptions_set(obj, _signals);
337    return obj;
338 }
339
340 /**
341  * Set the text label of the check object
342  *
343  * @param obj The check object
344  * @param label The text label string in UTF-8
345  *
346  * @ingroup Check
347  * @deprecated use elm_object_text_set() instead.
348  */
349 EAPI void
350 elm_check_label_set(Evas_Object *obj, const char *label)
351 {
352    _elm_check_label_set(obj, NULL, label);
353 }
354
355 /**
356  * Get the text label of the check object
357  *
358  * @param obj The check object
359  * @return The text label string in UTF-8
360  *
361  * @ingroup Check
362  * @deprecated use elm_object_text_set() instead.
363  */
364 EAPI const char *
365 elm_check_label_get(const Evas_Object *obj)
366 {
367    return _elm_check_label_get(obj, NULL);
368 }
369
370 /**
371  * Set the icon object of the check object
372  *
373  * Once the icon object is set, a previously set one will be deleted.
374  * If you want to keep that old content object, use the
375  * elm_check_icon_unset() function.
376  *
377  * @param obj The check object
378  * @param icon The icon object
379  *
380  * @ingroup Check
381  */
382 EAPI void
383 elm_check_icon_set(Evas_Object *obj, Evas_Object *icon)
384 {
385    ELM_CHECK_WIDTYPE(obj, widtype);
386    Widget_Data *wd = elm_widget_data_get(obj);
387    if (!wd) return;
388    if (wd->icon == icon) return;
389    if (wd->icon) evas_object_del(wd->icon);
390    wd->icon = icon;
391    if (icon)
392      {
393         elm_widget_sub_object_add(obj, icon);
394         evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
395                                        _changed_size_hints, obj);
396         edje_object_part_swallow(wd->chk, "elm.swallow.content", icon);
397         edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
398         edje_object_message_signal_process(wd->chk);
399      }
400    _sizing_eval(obj);
401 }
402
403 /**
404  * Get the icon object of the check object
405  *
406  * @param obj The check object
407  * @return The icon object
408  *
409  * @ingroup Check
410  */
411 EAPI Evas_Object *
412 elm_check_icon_get(const Evas_Object *obj)
413 {
414    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
415    Widget_Data *wd = elm_widget_data_get(obj);
416    if (!wd) return NULL;
417    return wd->icon;
418 }
419
420 /**
421  * Unset the icon used for the check object
422  *
423  * Unparent and return the icon object which was set for this widget.
424  *
425  * @param obj The check object
426  * @return The icon object that was being used
427  *
428  * @ingroup Check
429  */
430 EAPI Evas_Object *
431 elm_check_icon_unset(Evas_Object *obj)
432 {
433    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
434    Widget_Data *wd = elm_widget_data_get(obj);
435    if (!wd) return NULL;
436    if (!wd->icon) return NULL;
437    Evas_Object *icon = wd->icon;
438    elm_widget_sub_object_del(obj, wd->icon);
439    edje_object_part_unswallow(wd->chk, wd->icon);
440    wd->icon = NULL;
441    return icon;
442 }
443
444 /**
445  * Set the on/off state of the check object
446  *
447  * This sets the state of the check and will also set the value if pointed to
448  * to the state supplied, but will not call any callbacks.
449  *
450  * @param obj The check object
451  * @param state The state to use (1 == on, 0 == off)
452  *
453  * @ingroup Check
454  */
455 EAPI void
456 elm_check_state_set(Evas_Object *obj, Eina_Bool state)
457 {
458    ELM_CHECK_WIDTYPE(obj, widtype);
459    Widget_Data *wd = elm_widget_data_get(obj);
460    if (!wd) return;
461    if (state != wd->state)
462      {
463         wd->state = state;
464         if (wd->statep) *wd->statep = wd->state;
465         if (wd->state)
466           edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
467         else
468           edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
469      }
470 }
471
472 /**
473  * Get the state of the check object
474  *
475  * @param obj The check object
476  * @return The boolean state
477  *
478  * @ingroup Check
479  */
480 EAPI Eina_Bool
481 elm_check_state_get(const Evas_Object *obj)
482 {
483    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
484    Widget_Data *wd = elm_widget_data_get(obj);
485    if (!wd) return EINA_FALSE;
486    return wd->state;
487 }
488
489 /**
490  * Set a convenience pointer to a boolean to change
491  *
492  * This sets a pointer to a boolean, that, in addition to the check objects
493  * state will also be modified directly. To stop setting the object pointed
494  * to simply use NULL as the statep parameter. If statep is not NULL, then
495  * when this is called, the check objects state will also be modified to
496  * reflect the value of the boolean statep points to, just like calling
497  * elm_check_state_set().
498  *
499  * @param obj The check object
500  * @param statep Pointer to the boolean to modify
501  *
502  * @ingroup Check
503  */
504 EAPI void
505 elm_check_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
506 {
507    ELM_CHECK_WIDTYPE(obj, widtype);
508    Widget_Data *wd = elm_widget_data_get(obj);
509    if (!wd) return;
510    if (statep)
511      {
512         wd->statep = statep;
513         if (*wd->statep != wd->state)
514           {
515              wd->state = *wd->statep;
516              if (wd->state)
517                edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
518              else
519                edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
520           }
521      }
522    else
523      wd->statep = NULL;
524 }