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