Merge branch 'svn_merge'
[framework/uifw/elementary.git] / src / lib / elm_radio.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup Radio Radio
6  * @ingroup Elementary
7  *
8  * The radio button allows for 1 or more selectors to be created to select 1
9  * of a set of options.
10  *
11  * Signals that you can add callbacks for are:
12  *
13  * changed - This is called whenever the user changes the state of one of the
14  * radio objects within the group of radio objects that work together.
15  *
16  * A radio object contains an indicator, an optional Label and an optional
17  * icon object. They work normally in groups of 2 or more. When you create a
18  * radio (if it is not the first member of the group), simply add it to the
19  * group by adding it to any other member of the group that already exists
20  * (or the first member) with elm_radio_group_add() with the second parameter
21  * being the existing group member. The radio object(s) will select from one
22  * of a set of integer values, so any value they are configuring needs to be
23  * mapped to a set of integers. To configure what value that radio object
24  * represents, use  elm_radio_state_value_set() to set the integer it
25  * represents. To set the value the whole group is to indicate use
26  * elm_radio_value_set() on any group member, and to get the groups value use
27  * elm_radio_value_get(). For convenience the radio objects are also able to
28  * directly set an integer (int) to the value that is selected. To specify
29  * the pointer to this integer to modify, use elm_radio_value_pointer_set().
30  * The radio objects will modify this directly. That implies the pointer must
31  * point to valid memory for as long as the radio objects exist.
32  *
33  * Signals that you can add callbacks for are:
34  *
35  * "changed" - when the radio status is changed
36  *
37  */
38
39 typedef struct _Widget_Data Widget_Data;
40 typedef struct _Group Group;
41
42 struct _Group
43 {
44    int value;
45    int *valuep;
46    Eina_List *radios;
47 };
48
49 struct _Widget_Data
50 {
51    Evas_Object *radio;
52    Evas_Object *icon;
53    int value;
54    const char *label;
55    Eina_Bool state;
56    Group *group;
57 };
58
59 static const char *widtype = NULL;
60 static void _state_set(Evas_Object *obj, Eina_Bool state);
61 static void _del_hook(Evas_Object *obj);
62 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
63 static void _theme_hook(Evas_Object *obj);
64 static void _disable_hook(Evas_Object *obj);
65 static void _sizing_eval(Evas_Object *obj);
66 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
67 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
68 static void _signal_radio_on(void *data, Evas_Object *obj, const char *emission, const char *source);
69 static void _on_focus_hook(void *data, Evas_Object *obj);
70 static void _activate(Evas_Object *obj);
71 static void _activate_hook(Evas_Object *obj);
72 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
73                              Evas_Callback_Type type, void *event_info);
74
75 static const char SIG_CHANGED[] = "changed";
76 static const Evas_Smart_Cb_Description _signals[] = {
77   {SIG_CHANGED, ""},
78   {NULL, NULL}
79 };
80
81 static Eina_Bool
82 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
83 {
84    if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
85    Evas_Event_Key_Down *ev = event_info;
86    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
87    if (elm_widget_disabled_get(obj)) return EINA_FALSE;
88
89    if ((strcmp(ev->keyname, "Return")) &&
90        (strcmp(ev->keyname, "KP_Enter")) &&
91        (strcmp(ev->keyname, "space")))
92      return EINA_FALSE;
93    _activate(obj);
94    ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
95    return EINA_TRUE;
96 }
97
98 static void
99 _del_hook(Evas_Object *obj)
100 {
101    Widget_Data *wd = elm_widget_data_get(obj);
102    if (!wd) return;
103    if (wd->label) eina_stringshare_del(wd->label);
104    wd->group->radios = eina_list_remove(wd->group->radios, obj);
105    if (!wd->group->radios) free(wd->group);
106    wd->group = NULL;
107    free(wd);
108 }
109
110 static void
111 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
112 {
113    Widget_Data *wd = elm_widget_data_get(obj);
114    if (!wd) return;
115    if (elm_widget_focus_get(obj))
116      {
117         edje_object_signal_emit(wd->radio, "elm,action,focus", "elm");
118         evas_object_focus_set(wd->radio, EINA_TRUE);
119      }
120    else
121      {
122         edje_object_signal_emit(wd->radio, "elm,action,unfocus", "elm");
123         evas_object_focus_set(wd->radio, EINA_FALSE);
124      }
125 }
126
127 static void
128 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
129 {
130    Widget_Data *wd = elm_widget_data_get(obj);
131    if (!wd) return;
132    edje_object_mirrored_set(wd->radio, rtl);
133 }
134
135 static void
136 _theme_hook(Evas_Object *obj)
137 {
138    Widget_Data *wd = elm_widget_data_get(obj);
139    if (!wd) return;
140    _elm_widget_mirrored_reload(obj);
141    _mirrored_set(obj, elm_widget_mirrored_get(obj));
142    _elm_theme_object_set(obj, wd->radio, "radio", "base", elm_widget_style_get(obj));
143    if (wd->icon)
144      edje_object_signal_emit(wd->radio, "elm,state,icon,visible", "elm");
145    else
146      edje_object_signal_emit(wd->radio, "elm,state,icon,hidden", "elm");
147    if (wd->state)
148      edje_object_signal_emit(wd->radio, "elm,state,radio,on", "elm");
149    else
150      edje_object_signal_emit(wd->radio, "elm,state,radio,off", "elm");
151    if (wd->label)
152      edje_object_signal_emit(wd->radio, "elm,state,text,visible", "elm");
153    else
154      edje_object_signal_emit(wd->radio, "elm,state,text,hidden", "elm");
155    edje_object_part_text_set(wd->radio, "elm.text", wd->label);
156    if (elm_widget_disabled_get(obj))
157      {
158         edje_object_signal_emit(wd->radio, "elm,state,disabled", "elm");
159         if (wd->state) _state_set(obj, 0);
160      }
161    edje_object_message_signal_process(wd->radio);
162    edje_object_scale_set(wd->radio, elm_widget_scale_get(obj) * _elm_config->scale);
163    _sizing_eval(obj);
164 }
165
166 static void
167 _disable_hook(Evas_Object *obj)
168 {
169    Widget_Data *wd = elm_widget_data_get(obj);
170    if (!wd) return;
171    if (elm_widget_disabled_get(obj))
172      {
173         edje_object_signal_emit(wd->radio, "elm,state,disabled", "elm");
174         if (wd->state) _state_set(obj, 0);
175      }
176    else
177      edje_object_signal_emit(wd->radio, "elm,state,enabled", "elm");
178 }
179
180 static void
181 _sizing_eval(Evas_Object *obj)
182 {
183    Widget_Data *wd = elm_widget_data_get(obj);
184    Evas_Coord minw = -1, minh = -1;
185    if (!wd) return;
186    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
187    edje_object_size_min_restricted_calc(wd->radio, &minw, &minh, minw, minh);
188    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
189    evas_object_size_hint_min_set(obj, minw, minh);
190    evas_object_size_hint_max_set(obj, -1, -1);
191 }
192
193 static void
194 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
195 {
196    Widget_Data *wd = elm_widget_data_get(data);
197    if (!wd) return;
198    if (obj != wd->icon) return;
199    _sizing_eval(data);
200 }
201
202 static void
203 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
204 {
205    Widget_Data *wd = elm_widget_data_get(obj);
206    Evas_Object *sub = event_info;
207    if (!wd) return;
208    if (sub == wd->icon)
209      {
210         edje_object_signal_emit(wd->radio, "elm,state,icon,hidden", "elm");
211         evas_object_event_callback_del_full
212            (sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
213         wd->icon = NULL;
214         _sizing_eval(obj);
215      }
216 }
217
218 static void
219 _state_set(Evas_Object *obj, Eina_Bool state)
220 {
221    Widget_Data *wd = elm_widget_data_get(obj);
222    if (!wd) return;
223    if ((state != wd->state) && (!elm_widget_disabled_get(obj)))
224      {
225         wd->state = state;
226         if (wd->state)
227           edje_object_signal_emit(wd->radio, "elm,state,radio,on", "elm");
228         else
229           edje_object_signal_emit(wd->radio, "elm,state,radio,off", "elm");
230      }
231 }
232
233 static void
234 _state_set_all(Widget_Data *wd)
235 {
236    const Eina_List *l;
237    Evas_Object *child, *selected = NULL;
238    Eina_Bool disabled = EINA_FALSE;
239    EINA_LIST_FOREACH(wd->group->radios, l, child)
240      {
241         Widget_Data *wd2 = elm_widget_data_get(child);
242         if (wd2->state) selected = child;
243         if (wd2->value == wd->group->value)
244           {
245              _state_set(child, 1);
246              if (!wd2->state) disabled = EINA_TRUE;
247           }
248         else _state_set(child, 0);
249      }
250    if ((disabled) && (selected)) _state_set(selected, 1);
251 }
252
253 static void
254 _activate(Evas_Object *obj)
255 {
256    Widget_Data *wd = elm_widget_data_get(obj);
257    if (!wd) return;
258    if (wd->group->value == wd->value) return;
259    wd->group->value = wd->value;
260    if (wd->group->valuep) *(wd->group->valuep) = wd->group->value;
261    _state_set_all(wd);
262    evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
263 }
264
265 static void
266 _activate_hook(Evas_Object *obj)
267 {
268    _activate(obj);
269 }
270
271 static void
272 _signal_radio_on(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
273 {
274    _activate(data);
275 }
276
277 static void
278 _elm_radio_label_set(Evas_Object *obj, const char *item, const char *label)
279 {
280    ELM_CHECK_WIDTYPE(obj, widtype);
281    Widget_Data *wd = elm_widget_data_get(obj);
282    if (item && strcmp(item, "default")) return;
283    if (!wd) return;
284    eina_stringshare_replace(&wd->label, label);
285    if (label)
286      {
287         edje_object_signal_emit(wd->radio, "elm,state,text,visible", "elm");
288         edje_object_message_signal_process(wd->radio);
289      }
290    else
291      {
292         edje_object_signal_emit(wd->radio, "elm,state,text,hidden", "elm");
293         edje_object_message_signal_process(wd->radio);
294      }
295    edje_object_part_text_set(wd->radio, "elm.text", label);
296    _sizing_eval(obj);
297 }
298
299 static const char *
300 _elm_radio_label_get(const Evas_Object *obj, const char *item)
301 {
302    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
303    Widget_Data *wd = elm_widget_data_get(obj);
304    if (item && strcmp(item, "default")) return NULL;
305    if (!wd) return NULL;
306    return wd->label;
307 }
308
309 /**
310  * Add a new radio to the parent
311  *
312  * @param parent The parent object
313  * @return The new object or NULL if it cannot be created
314  *
315  * @ingroup Radio
316  */
317 EAPI Evas_Object *
318 elm_radio_add(Evas_Object *parent)
319 {
320    Evas_Object *obj;
321    Evas *e;
322    Widget_Data *wd;
323
324    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
325
326    ELM_SET_WIDTYPE(widtype, "radio");
327    elm_widget_type_set(obj, "radio");
328    elm_widget_sub_object_add(parent, obj);
329    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
330    elm_widget_data_set(obj, wd);
331    elm_widget_del_hook_set(obj, _del_hook);
332    elm_widget_theme_hook_set(obj, _theme_hook);
333    elm_widget_disable_hook_set(obj, _disable_hook);
334    elm_widget_can_focus_set(obj, EINA_TRUE);
335    elm_widget_activate_hook_set(obj, _activate_hook);
336    elm_widget_event_hook_set(obj, _event_hook);
337    elm_widget_text_set_hook_set(obj, _elm_radio_label_set);
338    elm_widget_text_get_hook_set(obj, _elm_radio_label_get);
339
340    wd->radio = edje_object_add(e);
341    _elm_theme_object_set(obj, wd->radio, "radio", "base", "default");
342    edje_object_signal_callback_add(wd->radio, "elm,action,radio,on", "", _signal_radio_on, obj);
343    edje_object_signal_callback_add(wd->radio, "elm,action,radio,toggle", "", _signal_radio_on, obj);
344    elm_widget_resize_object_set(obj, wd->radio);
345
346    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
347
348    wd->group = calloc(1, sizeof(Group));
349    wd->group->radios = eina_list_append(wd->group->radios, obj);
350    wd->state = 0;
351
352    _mirrored_set(obj, elm_widget_mirrored_get(obj));
353    _sizing_eval(obj);
354
355    // TODO: convert Elementary to subclassing of Evas_Smart_Class
356    // TODO: and save some bytes, making descriptions per-class and not instance!
357    evas_object_smart_callbacks_descriptions_set(obj, _signals);
358    return obj;
359 }
360
361 /**
362  * Set the text label of the radio object
363  *
364  * @param obj The radio object
365  * @param label The text label string in UTF-8
366  *
367  * @ingroup Radio
368  * @deprecated use elm_object_text_set() instead.
369  */
370 EAPI void
371 elm_radio_label_set(Evas_Object *obj, const char *label)
372 {
373    _elm_radio_label_set(obj, NULL, label);
374 }
375
376 /**
377  * Get the text label of the radio object
378  *
379  * @param obj The radio object
380  * @return The text label string in UTF-8
381  *
382  * @ingroup Radio
383  * @deprecated use elm_object_text_set() instead.
384  */
385 EAPI const char *
386 elm_radio_label_get(const Evas_Object *obj)
387 {
388    return _elm_radio_label_get(obj, NULL);
389 }
390
391 /**
392  * Set the icon object of the radio object
393  *
394  * Once the icon object is set, a previously set one will be deleted.
395  * If you want to keep that old content object, use the
396  * elm_radio_icon_unset() function.
397  *
398  * @param obj The radio object
399  * @param icon The icon object
400  *
401  * @ingroup Radio
402  */
403 EAPI void
404 elm_radio_icon_set(Evas_Object *obj, Evas_Object *icon)
405 {
406    ELM_CHECK_WIDTYPE(obj, widtype);
407    Widget_Data *wd = elm_widget_data_get(obj);
408    if (!wd) return;
409    if (wd->icon == icon) return;
410    if (wd->icon) evas_object_del(wd->icon);
411    wd->icon = icon;
412    if (icon)
413      {
414         elm_widget_sub_object_add(obj, icon);
415         evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
416                                        _changed_size_hints, obj);
417         edje_object_part_swallow(wd->radio, "elm.swallow.content", icon);
418         edje_object_signal_emit(wd->radio, "elm,state,icon,visible", "elm");
419         edje_object_message_signal_process(wd->radio);
420      }
421    _sizing_eval(obj);
422 }
423
424 /**
425  * Get the icon object of the radio object
426  *
427  * @param obj The radio object
428  * @return The icon object
429  *
430  * @ingroup Radio
431  */
432 EAPI Evas_Object *
433 elm_radio_icon_get(const Evas_Object *obj)
434 {
435    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
436    Widget_Data *wd = elm_widget_data_get(obj);
437    if (!wd) return NULL;
438    return wd->icon;
439 }
440
441 /**
442  * Unset the icon used for the radio object
443  *
444  * Unparent and return the icon object which was set for this widget.
445  *
446  * @param obj The radio object
447  * @return The icon object that was being used
448  *
449  * @ingroup Radio
450  */
451 EAPI Evas_Object *
452 elm_radio_icon_unset(Evas_Object *obj)
453 {
454    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
455    Widget_Data *wd = elm_widget_data_get(obj);
456    if (!wd) return NULL;
457    if (!wd->icon) return NULL;
458    Evas_Object *icon = wd->icon;
459    elm_widget_sub_object_del(obj, wd->icon);
460    edje_object_part_unswallow(wd->radio, wd->icon);
461    wd->icon = NULL;
462    return icon;
463 }
464
465 /**
466  * Add this radio to a group of other radio objects
467  *
468  * Radio objects work in groups. Each member should have a different integer
469  * value assigned. In order ro have them work as a group, they need to know
470  * about eacthother. This adds the given radio object to the group of which
471  * the group object indicated is a member.
472  *
473  * @param obj The radio object
474  * @param group The object whose group the object is to join
475  *
476  * @ingroup Radio
477  */
478 EAPI void
479 elm_radio_group_add(Evas_Object *obj, Evas_Object *group)
480 {
481    ELM_CHECK_WIDTYPE(obj, widtype);
482    Widget_Data *wd = elm_widget_data_get(obj);
483    Widget_Data *wd2 = elm_widget_data_get(group);
484    if (!wd) return;
485    if (!wd2)
486      {
487         if (eina_list_count(wd->group->radios) == 1)
488           return;
489         wd->group->radios = eina_list_remove(wd->group->radios, obj);
490         wd->group = calloc(1, sizeof(Group));
491         wd->group->radios = eina_list_append(wd->group->radios, obj);
492      }
493    else if (wd->group == wd2->group) return;
494    else
495      {
496         wd->group->radios = eina_list_remove(wd->group->radios, obj);
497         if (!wd->group->radios) free(wd->group);
498         wd->group = wd2->group;
499         wd->group->radios = eina_list_append(wd->group->radios, obj);
500      }
501    if (wd->value == wd->group->value) _state_set(obj, 1);
502    else _state_set(obj, 0);
503 }
504
505 /**
506  * Set the integer value that this radio object represents
507  *
508  * This sets the value of the radio.
509  *
510  * @param obj The radio object
511  * @param value The value to use if this radio object is selected
512  *
513  * @ingroup Radio
514  */
515 EAPI void
516 elm_radio_state_value_set(Evas_Object *obj, int value)
517 {
518    ELM_CHECK_WIDTYPE(obj, widtype);
519    Widget_Data *wd = elm_widget_data_get(obj);
520    if (!wd) return;
521    wd->value = value;
522    if (wd->value == wd->group->value) _state_set(obj, 1);
523    else _state_set(obj, 0);
524 }
525
526 /**
527  * Get the integer value that this radio object represents
528  *
529  * This gets the value of the radio.
530  *
531  * @param obj The radio object
532  * @return The value used if this radio object is selected
533  *
534  * @ingroup Radio
535  */
536 EAPI int
537 elm_radio_state_value_get(const Evas_Object *obj)
538 {
539    ELM_CHECK_WIDTYPE(obj, widtype) 0;
540    Widget_Data *wd = elm_widget_data_get(obj);
541    if (!wd) return 0;
542    return wd->value;
543 }
544
545 /**
546  * Set the value of the radio.
547  *
548  * This sets the value of the radio group and will also set the value if
549  * pointed to, to the value supplied, but will not call any callbacks.
550  *
551  * @param obj The radio object
552  * @param value The value to use for the group
553  *
554  * @ingroup Radio
555  */
556 EAPI void
557 elm_radio_value_set(Evas_Object *obj, int value)
558 {
559    ELM_CHECK_WIDTYPE(obj, widtype);
560    Widget_Data *wd = elm_widget_data_get(obj);
561    if (!wd) return;
562    if (value == wd->group->value) return;
563    wd->group->value = value;
564    if (wd->group->valuep) *(wd->group->valuep) = wd->group->value;
565    _state_set_all(wd);
566 }
567
568 /**
569  * Get the state of the radio object
570  *
571  * @param obj The radio object
572  * @return The integer state
573  *
574  * @ingroup Radio
575  */
576 EAPI int
577 elm_radio_value_get(const Evas_Object *obj)
578 {
579    ELM_CHECK_WIDTYPE(obj, widtype) 0;
580    Widget_Data *wd = elm_widget_data_get(obj);
581    if (!wd) return 0;
582    return wd->group->value;
583 }
584
585 /**
586  * Set a convenience pointer to a integer to change
587  *
588  * This sets a pointer to a integer, that, in addition to the radio objects
589  * state will also be modified directly. To stop setting the object pointed
590  * to simply use NULL as the valuep parameter. If valuep is not NULL, then
591  * when this is called, the radio objects state will also be modified to
592  * reflect the value of the integer valuep points to, just like calling
593  * elm_radio_value_set().
594  *
595  * @param obj The radio object
596  * @param valuep Pointer to the integer to modify
597  *
598  * @ingroup Radio
599  */
600 EAPI void
601 elm_radio_value_pointer_set(Evas_Object *obj, int *valuep)
602 {
603    ELM_CHECK_WIDTYPE(obj, widtype);
604    Widget_Data *wd = elm_widget_data_get(obj);
605    if (!wd) return;
606    if (valuep)
607      {
608         wd->group->valuep = valuep;
609         if (*(wd->group->valuep) != wd->group->value)
610           {
611              wd->group->value = *(wd->group->valuep);
612              _state_set_all(wd);
613           }
614      }
615    else
616      {
617         wd->group->valuep = NULL;
618      }
619 }