add note about allocation
[framework/uifw/elementary.git] / src / lib / elm_radio.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 typedef struct _Widget_Data Widget_Data;
5 typedef struct _Group Group;
6
7 struct _Group
8 {
9    int value;
10    int *valuep;
11    Eina_List *radios;
12 };
13
14 struct _Widget_Data
15 {
16    Evas_Object *radio;
17    Evas_Object *icon;
18    int value;
19    const char *label;
20    Eina_Bool state;
21    Group *group;
22 };
23
24 static const char *widtype = NULL;
25 static void _state_set(Evas_Object *obj, Eina_Bool state);
26 static void _del_hook(Evas_Object *obj);
27 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
28 static void _theme_hook(Evas_Object *obj);
29 static void _disable_hook(Evas_Object *obj);
30 static void _sizing_eval(Evas_Object *obj);
31 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
32 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
33 static void _signal_radio_on(void *data, Evas_Object *obj, const char *emission, const char *source);
34 static void _on_focus_hook(void *data, Evas_Object *obj);
35 static void _activate(Evas_Object *obj);
36 static void _activate_hook(Evas_Object *obj);
37 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
38                              Evas_Callback_Type type, void *event_info);
39
40 static const char SIG_CHANGED[] = "changed";
41 static const Evas_Smart_Cb_Description _signals[] = {
42   {SIG_CHANGED, ""},
43   {NULL, NULL}
44 };
45
46 static Eina_Bool
47 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
48 {
49    if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
50    Evas_Event_Key_Down *ev = event_info;
51    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
52    if (elm_widget_disabled_get(obj)) return EINA_FALSE;
53
54    if ((strcmp(ev->keyname, "Return")) &&
55        (strcmp(ev->keyname, "KP_Enter")) &&
56        (strcmp(ev->keyname, "space")))
57      return EINA_FALSE;
58    _activate(obj);
59    ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
60    return EINA_TRUE;
61 }
62
63 static void
64 _del_hook(Evas_Object *obj)
65 {
66    Widget_Data *wd = elm_widget_data_get(obj);
67    if (!wd) return;
68    if (wd->label) eina_stringshare_del(wd->label);
69    wd->group->radios = eina_list_remove(wd->group->radios, obj);
70    if (!wd->group->radios) free(wd->group);
71    wd->group = NULL;
72    free(wd);
73 }
74
75 static void
76 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
77 {
78    Widget_Data *wd = elm_widget_data_get(obj);
79    if (!wd) return;
80    if (elm_widget_focus_get(obj))
81      {
82         edje_object_signal_emit(wd->radio, "elm,action,focus", "elm");
83         evas_object_focus_set(wd->radio, EINA_TRUE);
84      }
85    else
86      {
87         edje_object_signal_emit(wd->radio, "elm,action,unfocus", "elm");
88         evas_object_focus_set(wd->radio, EINA_FALSE);
89      }
90 }
91
92 static void
93 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
94 {
95    Widget_Data *wd = elm_widget_data_get(obj);
96    if (!wd) return;
97    edje_object_mirrored_set(wd->radio, rtl);
98 }
99
100 static void
101 _theme_hook(Evas_Object *obj)
102 {
103    Widget_Data *wd = elm_widget_data_get(obj);
104    if (!wd) return;
105    _elm_widget_mirrored_reload(obj);
106    _mirrored_set(obj, elm_widget_mirrored_get(obj));
107    _elm_theme_object_set(obj, wd->radio, "radio", "base", elm_widget_style_get(obj));
108    if (wd->icon)
109      edje_object_signal_emit(wd->radio, "elm,state,icon,visible", "elm");
110    else
111      edje_object_signal_emit(wd->radio, "elm,state,icon,hidden", "elm");
112    if (wd->state)
113      edje_object_signal_emit(wd->radio, "elm,state,radio,on", "elm");
114    else
115      edje_object_signal_emit(wd->radio, "elm,state,radio,off", "elm");
116    if (wd->label)
117      edje_object_signal_emit(wd->radio, "elm,state,text,visible", "elm");
118    else
119      edje_object_signal_emit(wd->radio, "elm,state,text,hidden", "elm");
120    edje_object_part_text_set(wd->radio, "elm.text", wd->label);
121    if (elm_widget_disabled_get(obj))
122      {
123         edje_object_signal_emit(wd->radio, "elm,state,disabled", "elm");
124         if (wd->state) _state_set(obj, 0);
125      }
126    edje_object_message_signal_process(wd->radio);
127    edje_object_scale_set(wd->radio, elm_widget_scale_get(obj) * _elm_config->scale);
128    _sizing_eval(obj);
129 }
130
131 static void
132 _disable_hook(Evas_Object *obj)
133 {
134    Widget_Data *wd = elm_widget_data_get(obj);
135    if (!wd) return;
136    if (elm_widget_disabled_get(obj))
137      {
138         edje_object_signal_emit(wd->radio, "elm,state,disabled", "elm");
139         if (wd->state) _state_set(obj, 0);
140      }
141    else
142      edje_object_signal_emit(wd->radio, "elm,state,enabled", "elm");
143 }
144
145 static void
146 _sizing_eval(Evas_Object *obj)
147 {
148    Widget_Data *wd = elm_widget_data_get(obj);
149    Evas_Coord minw = -1, minh = -1;
150    if (!wd) return;
151    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
152    edje_object_size_min_restricted_calc(wd->radio, &minw, &minh, minw, minh);
153    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
154    evas_object_size_hint_min_set(obj, minw, minh);
155    evas_object_size_hint_max_set(obj, -1, -1);
156 }
157
158 static void
159 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
160 {
161    Widget_Data *wd = elm_widget_data_get(data);
162    if (!wd) return;
163    if (obj != wd->icon) return;
164    _sizing_eval(data);
165 }
166
167 static void
168 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
169 {
170    Widget_Data *wd = elm_widget_data_get(obj);
171    Evas_Object *sub = event_info;
172    if (!wd) return;
173    if (sub == wd->icon)
174      {
175         edje_object_signal_emit(wd->radio, "elm,state,icon,hidden", "elm");
176         evas_object_event_callback_del_full
177            (sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
178         wd->icon = NULL;
179         _sizing_eval(obj);
180      }
181 }
182
183 static void
184 _state_set(Evas_Object *obj, Eina_Bool state)
185 {
186    Widget_Data *wd = elm_widget_data_get(obj);
187    if (!wd) return;
188    if ((state != wd->state) && (!elm_widget_disabled_get(obj)))
189      {
190         wd->state = state;
191         if (wd->state)
192           edje_object_signal_emit(wd->radio, "elm,state,radio,on", "elm");
193         else
194           edje_object_signal_emit(wd->radio, "elm,state,radio,off", "elm");
195      }
196 }
197
198 static void
199 _state_set_all(Widget_Data *wd)
200 {
201    const Eina_List *l;
202    Evas_Object *child, *selected = NULL;
203    Eina_Bool disabled = EINA_FALSE;
204    EINA_LIST_FOREACH(wd->group->radios, l, child)
205      {
206         Widget_Data *wd2 = elm_widget_data_get(child);
207         if (wd2->state) selected = child;
208         if (wd2->value == wd->group->value)
209           {
210              _state_set(child, 1);
211              if (!wd2->state) disabled = EINA_TRUE;
212           }
213         else _state_set(child, 0);
214      }
215    if ((disabled) && (selected)) _state_set(selected, 1);
216 }
217
218 static void
219 _activate(Evas_Object *obj)
220 {
221    Widget_Data *wd = elm_widget_data_get(obj);
222    if (!wd) return;
223    if (wd->group->value == wd->value) return;
224    if ((_elm_config->access_mode == ELM_ACCESS_MODE_OFF) ||
225        (_elm_access_2nd_click_timeout(obj)))
226      {
227         wd->group->value = wd->value;
228         if (wd->group->valuep) *(wd->group->valuep) = wd->group->value;
229         _state_set_all(wd);
230         if (_elm_config->access_mode != ELM_ACCESS_MODE_OFF)
231           _elm_access_say(E_("State: On"));
232         evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
233      }
234 }
235
236 static void
237 _activate_hook(Evas_Object *obj)
238 {
239    _activate(obj);
240 }
241
242 static void
243 _signal_radio_on(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
244 {
245    _activate(data);
246 }
247
248 static void
249 _elm_radio_label_set(Evas_Object *obj, const char *item, const char *label)
250 {
251    ELM_CHECK_WIDTYPE(obj, widtype);
252    Widget_Data *wd = elm_widget_data_get(obj);
253    if (item && strcmp(item, "default")) return;
254    if (!wd) return;
255    eina_stringshare_replace(&wd->label, label);
256    if (label)
257      {
258         edje_object_signal_emit(wd->radio, "elm,state,text,visible", "elm");
259         edje_object_message_signal_process(wd->radio);
260      }
261    else
262      {
263         edje_object_signal_emit(wd->radio, "elm,state,text,hidden", "elm");
264         edje_object_message_signal_process(wd->radio);
265      }
266    edje_object_part_text_set(wd->radio, "elm.text", label);
267    _sizing_eval(obj);
268 }
269
270 static const char *
271 _elm_radio_label_get(const Evas_Object *obj, const char *item)
272 {
273    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
274    Widget_Data *wd = elm_widget_data_get(obj);
275    if (item && strcmp(item, "default")) return NULL;
276    if (!wd) return NULL;
277    return wd->label;
278 }
279
280 static char *
281 _access_info_cb(void *data __UNUSED__, Evas_Object *obj, Elm_Widget_Item *item __UNUSED__)
282 {
283    const char *txt = elm_widget_access_info_get(obj);
284    if (!txt) txt = _elm_radio_label_get(obj, NULL);
285    if (txt) return strdup(txt);
286    return NULL;
287 }
288
289 static char *
290 _access_state_cb(void *data __UNUSED__, Evas_Object *obj, Elm_Widget_Item *item __UNUSED__)
291 {
292    Widget_Data *wd = elm_widget_data_get(obj);
293    if (!wd) return NULL;
294    if (elm_widget_disabled_get(obj))
295      return strdup(E_("State: Disabled"));
296    if (wd->state)
297      return strdup(E_("State: On"));
298    return strdup(E_("State: Off"));
299 }
300
301 static void
302 _content_set_hook(Evas_Object *obj, const char *part, Evas_Object *content)
303 {
304    ELM_CHECK_WIDTYPE(obj, widtype);
305    Widget_Data *wd;
306
307    if (part && strcmp(part, "icon")) return;
308    wd = elm_widget_data_get(obj);
309    if (!wd) return;
310    if (wd->icon == content) return;
311    if (wd->icon) evas_object_del(wd->icon);
312    wd->icon = content;
313    if (content)
314      {
315         elm_widget_sub_object_add(obj, content);
316         evas_object_event_callback_add(content,
317                                        EVAS_CALLBACK_CHANGED_SIZE_HINTS,
318                                        _changed_size_hints, obj);
319         edje_object_part_swallow(wd->radio, "elm.swallow.content", content);
320         edje_object_signal_emit(wd->radio, "elm,state,icon,visible", "elm");
321         edje_object_message_signal_process(wd->radio);
322      }
323    _sizing_eval(obj);
324 }
325
326 static Evas_Object *
327 _content_get_hook(const Evas_Object *obj, const char *part)
328 {
329    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
330    Widget_Data *wd;
331
332    if (part && strcmp(part, "icon")) return NULL;
333    wd = elm_widget_data_get(obj);
334    if (!wd) return NULL;
335    return wd->icon;
336 }
337
338 static Evas_Object *
339 _content_unset_hook(Evas_Object *obj, const char *part)
340 {
341    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
342    Widget_Data *wd;
343    Evas_Object *icon;
344    if (part && strcmp(part, "icon")) return NULL;
345    wd = elm_widget_data_get(obj);
346    if (!wd) return NULL;
347    if (!wd->icon) return NULL;
348    icon = wd->icon;
349    elm_widget_sub_object_del(obj, wd->icon);
350    evas_object_event_callback_del_full(wd->icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
351                                        _changed_size_hints, obj);
352    edje_object_part_unswallow(wd->radio, wd->icon);
353    wd->icon = NULL;
354    return icon;
355 }
356
357 EAPI Evas_Object *
358 elm_radio_add(Evas_Object *parent)
359 {
360    Evas_Object *obj;
361    Evas *e;
362    Widget_Data *wd;
363
364    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
365
366    ELM_SET_WIDTYPE(widtype, "radio");
367    elm_widget_type_set(obj, "radio");
368    elm_widget_sub_object_add(parent, obj);
369    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
370    elm_widget_data_set(obj, wd);
371    elm_widget_del_hook_set(obj, _del_hook);
372    elm_widget_theme_hook_set(obj, _theme_hook);
373    elm_widget_disable_hook_set(obj, _disable_hook);
374    elm_widget_can_focus_set(obj, EINA_TRUE);
375    elm_widget_activate_hook_set(obj, _activate_hook);
376    elm_widget_event_hook_set(obj, _event_hook);
377    elm_widget_text_set_hook_set(obj, _elm_radio_label_set);
378    elm_widget_text_get_hook_set(obj, _elm_radio_label_get);
379    elm_widget_content_set_hook_set(obj, _content_set_hook);
380    elm_widget_content_get_hook_set(obj, _content_get_hook);
381    elm_widget_content_unset_hook_set(obj, _content_unset_hook);
382
383    wd->radio = edje_object_add(e);
384    _elm_theme_object_set(obj, wd->radio, "radio", "base", "default");
385    edje_object_signal_callback_add(wd->radio, "elm,action,radio,on", "", _signal_radio_on, obj);
386    edje_object_signal_callback_add(wd->radio, "elm,action,radio,toggle", "", _signal_radio_on, obj);
387    elm_widget_resize_object_set(obj, wd->radio);
388
389    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
390
391    wd->group = calloc(1, sizeof(Group));
392    wd->group->radios = eina_list_append(wd->group->radios, obj);
393    wd->state = 0;
394
395    _mirrored_set(obj, elm_widget_mirrored_get(obj));
396    _sizing_eval(obj);
397
398    // TODO: convert Elementary to subclassing of Evas_Smart_Class
399    // TODO: and save some bytes, making descriptions per-class and not instance!
400    evas_object_smart_callbacks_descriptions_set(obj, _signals);
401
402    _elm_access_object_register(obj, wd->radio);
403    _elm_access_text_set(_elm_access_object_get(obj),
404                         ELM_ACCESS_TYPE, E_("Radio"));
405    _elm_access_callback_set(_elm_access_object_get(obj),
406                             ELM_ACCESS_INFO, _access_info_cb, obj);
407    _elm_access_callback_set(_elm_access_object_get(obj),
408                             ELM_ACCESS_STATE, _access_state_cb, obj);
409
410    return obj;
411 }
412
413 EAPI void
414 elm_radio_label_set(Evas_Object *obj, const char *label)
415 {
416    _elm_radio_label_set(obj, NULL, label);
417 }
418
419 EAPI const char *
420 elm_radio_label_get(const Evas_Object *obj)
421 {
422    return _elm_radio_label_get(obj, NULL);
423 }
424
425 EAPI void
426 elm_radio_icon_set(Evas_Object *obj, Evas_Object *icon)
427 {
428    _content_set_hook(obj, "icon", icon);
429 }
430
431 EAPI Evas_Object *
432 elm_radio_icon_get(const Evas_Object *obj)
433 {
434    return _content_get_hook(obj, "icon");
435 }
436
437 EAPI Evas_Object *
438 elm_radio_icon_unset(Evas_Object *obj)
439 {
440    return _content_unset_hook(obj, "icon");
441 }
442
443 EAPI void
444 elm_radio_group_add(Evas_Object *obj, Evas_Object *group)
445 {
446    ELM_CHECK_WIDTYPE(obj, widtype);
447    Widget_Data *wd = elm_widget_data_get(obj);
448    Widget_Data *wd2 = elm_widget_data_get(group);
449    if (!wd) return;
450    if (!wd2)
451      {
452         if (eina_list_count(wd->group->radios) == 1)
453           return;
454         wd->group->radios = eina_list_remove(wd->group->radios, obj);
455         wd->group = calloc(1, sizeof(Group));
456         wd->group->radios = eina_list_append(wd->group->radios, obj);
457      }
458    else if (wd->group == wd2->group) return;
459    else
460      {
461         wd->group->radios = eina_list_remove(wd->group->radios, obj);
462         if (!wd->group->radios) free(wd->group);
463         wd->group = wd2->group;
464         wd->group->radios = eina_list_append(wd->group->radios, obj);
465      }
466    if (wd->value == wd->group->value) _state_set(obj, 1);
467    else _state_set(obj, 0);
468 }
469
470 EAPI void
471 elm_radio_state_value_set(Evas_Object *obj, int value)
472 {
473    ELM_CHECK_WIDTYPE(obj, widtype);
474    Widget_Data *wd = elm_widget_data_get(obj);
475    if (!wd) return;
476    wd->value = value;
477    if (wd->value == wd->group->value) _state_set(obj, 1);
478    else _state_set(obj, 0);
479 }
480
481 EAPI int
482 elm_radio_state_value_get(const Evas_Object *obj)
483 {
484    ELM_CHECK_WIDTYPE(obj, widtype) 0;
485    Widget_Data *wd = elm_widget_data_get(obj);
486    if (!wd) return 0;
487    return wd->value;
488 }
489
490 EAPI void
491 elm_radio_value_set(Evas_Object *obj, int value)
492 {
493    ELM_CHECK_WIDTYPE(obj, widtype);
494    Widget_Data *wd = elm_widget_data_get(obj);
495    if (!wd) return;
496    if (value == wd->group->value) return;
497    wd->group->value = value;
498    if (wd->group->valuep) *(wd->group->valuep) = wd->group->value;
499    _state_set_all(wd);
500 }
501
502 EAPI int
503 elm_radio_value_get(const Evas_Object *obj)
504 {
505    ELM_CHECK_WIDTYPE(obj, widtype) 0;
506    Widget_Data *wd = elm_widget_data_get(obj);
507    if (!wd) return 0;
508    return wd->group->value;
509 }
510
511 EAPI void
512 elm_radio_value_pointer_set(Evas_Object *obj, int *valuep)
513 {
514    ELM_CHECK_WIDTYPE(obj, widtype);
515    Widget_Data *wd = elm_widget_data_get(obj);
516    if (!wd) return;
517    if (valuep)
518      {
519         wd->group->valuep = valuep;
520         if (*(wd->group->valuep) != wd->group->value)
521           {
522              wd->group->value = *(wd->group->valuep);
523              _state_set_all(wd);
524           }
525      }
526    else
527      {
528         wd->group->valuep = NULL;
529      }
530 }
531
532 EAPI Evas_Object *
533 elm_radio_selected_object_get(Evas_Object *obj)
534 {
535    Eina_List *l;
536    Evas_Object *child;
537    Widget_Data *wd2;
538
539    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
540    Widget_Data *wd = elm_widget_data_get(obj);
541    if (!wd) return NULL;
542
543    EINA_LIST_FOREACH(wd->group->radios, l, child)
544      {
545         wd2  = elm_widget_data_get(child);
546         if (wd2->value == wd->group->value)
547            return child;
548      }
549    return NULL;
550 }