Elementary / genlist, slider, radio, win toolbar, thumb, toggle, scroller, slideshow...
[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 /**
277  * Add a new radio to the parent
278  *
279  * @param parent The parent object
280  * @return The new object or NULL if it cannot be created
281  *
282  * @ingroup Radio
283  */
284 EAPI Evas_Object *
285 elm_radio_add(Evas_Object *parent)
286 {
287    Evas_Object *obj;
288    Evas *e;
289    Widget_Data *wd;
290
291    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
292
293    ELM_SET_WIDTYPE(widtype, "radio");
294    elm_widget_type_set(obj, "radio");
295    elm_widget_sub_object_add(parent, obj);
296    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
297    elm_widget_data_set(obj, wd);
298    elm_widget_del_hook_set(obj, _del_hook);
299    elm_widget_theme_hook_set(obj, _theme_hook);
300    elm_widget_disable_hook_set(obj, _disable_hook);
301    elm_widget_can_focus_set(obj, EINA_TRUE);
302    elm_widget_activate_hook_set(obj, _activate_hook);
303    elm_widget_event_hook_set(obj, _event_hook);
304
305    wd->radio = edje_object_add(e);
306    _elm_theme_object_set(obj, wd->radio, "radio", "base", "default");
307    edje_object_signal_callback_add(wd->radio, "elm,action,radio,on", "", _signal_radio_on, obj);
308    edje_object_signal_callback_add(wd->radio, "elm,action,radio,toggle", "", _signal_radio_on, obj);
309    elm_widget_resize_object_set(obj, wd->radio);
310
311    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
312
313    wd->group = calloc(1, sizeof(Group));
314    wd->group->radios = eina_list_append(wd->group->radios, obj);
315    wd->state = 0;
316
317    _mirrored_set(obj, elm_widget_mirrored_get(obj));
318    _sizing_eval(obj);
319
320    // TODO: convert Elementary to subclassing of Evas_Smart_Class
321    // TODO: and save some bytes, making descriptions per-class and not instance!
322    evas_object_smart_callbacks_descriptions_set(obj, _signals);
323    return obj;
324 }
325
326 /**
327  * Set the text label of the radio object
328  *
329  * @param obj The radio object
330  * @param label The text label string in UTF-8
331  *
332  * @ingroup Radio
333  */
334 EAPI void
335 elm_radio_label_set(Evas_Object *obj, const char *label)
336 {
337    ELM_CHECK_WIDTYPE(obj, widtype);
338    Widget_Data *wd = elm_widget_data_get(obj);
339    if (!wd) return;
340    eina_stringshare_replace(&wd->label, label);
341    if (label)
342      {
343         edje_object_signal_emit(wd->radio, "elm,state,text,visible", "elm");
344         edje_object_message_signal_process(wd->radio);
345      }
346    else
347      {
348         edje_object_signal_emit(wd->radio, "elm,state,text,hidden", "elm");
349         edje_object_message_signal_process(wd->radio);
350      }
351    edje_object_part_text_set(wd->radio, "elm.text", label);
352    _sizing_eval(obj);
353 }
354
355 /**
356  * Get the text label of the radio object
357  *
358  * @param obj The radio object
359  * @return The text label string in UTF-8
360  *
361  * @ingroup Radio
362  */
363 EAPI const char *
364 elm_radio_label_get(const Evas_Object *obj)
365 {
366    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
367    Widget_Data *wd = elm_widget_data_get(obj);
368    if (!wd) return NULL;
369    return wd->label;
370 }
371
372 /**
373  * Set the icon object of the radio object
374  *
375  * Once the icon object is set, a previously set one will be deleted.
376  * If you want to keep that old content object, use the
377  * elm_radio_icon_unset() function.
378  *
379  * @param obj The radio object
380  * @param icon The icon object
381  *
382  * @ingroup Radio
383  */
384 EAPI void
385 elm_radio_icon_set(Evas_Object *obj, Evas_Object *icon)
386 {
387    ELM_CHECK_WIDTYPE(obj, widtype);
388    Widget_Data *wd = elm_widget_data_get(obj);
389    if (!wd) return;
390    if (wd->icon == icon) return;
391    if (wd->icon) evas_object_del(wd->icon);
392    wd->icon = icon;
393    if (icon)
394      {
395         elm_widget_sub_object_add(obj, icon);
396         evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
397                                        _changed_size_hints, obj);
398         edje_object_part_swallow(wd->radio, "elm.swallow.content", icon);
399         edje_object_signal_emit(wd->radio, "elm,state,icon,visible", "elm");
400         edje_object_message_signal_process(wd->radio);
401      }
402    _sizing_eval(obj);
403 }
404
405 /**
406  * Get the icon object of the radio object
407  *
408  * @param obj The radio object
409  * @return The icon object
410  *
411  * @ingroup Radio
412  */
413 EAPI Evas_Object *
414 elm_radio_icon_get(const Evas_Object *obj)
415 {
416    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
417    Widget_Data *wd = elm_widget_data_get(obj);
418    if (!wd) return NULL;
419    return wd->icon;
420 }
421
422 /**
423  * Unset the icon used for the radio object
424  *
425  * Unparent and return the icon object which was set for this widget.
426  *
427  * @param obj The radio object
428  * @return The icon object that was being used
429  *
430  * @ingroup Radio
431  */
432 EAPI Evas_Object *
433 elm_radio_icon_unset(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    if (!wd->icon) return NULL;
439    Evas_Object *icon = wd->icon;
440    elm_widget_sub_object_del(obj, wd->icon);
441    edje_object_part_unswallow(wd->radio, wd->icon);
442    wd->icon = NULL;
443    return icon;
444 }
445
446 /**
447  * Add this radio to a group of other radio objects
448  *
449  * Radio objects work in groups. Each member should have a different integer
450  * value assigned. In order ro have them work as a group, they need to know
451  * about eacthother. This adds the given radio object to the group of which
452  * the group object indicated is a member.
453  *
454  * @param obj The radio object
455  * @param group The object whose group the object is to join
456  *
457  * @ingroup Radio
458  */
459 EAPI void
460 elm_radio_group_add(Evas_Object *obj, Evas_Object *group)
461 {
462    ELM_CHECK_WIDTYPE(obj, widtype);
463    Widget_Data *wd = elm_widget_data_get(obj);
464    Widget_Data *wd2 = elm_widget_data_get(group);
465    if (!wd) return;
466    if (!wd2)
467      {
468         if (eina_list_count(wd->group->radios) == 1)
469           return;
470         wd->group->radios = eina_list_remove(wd->group->radios, obj);
471         wd->group = calloc(1, sizeof(Group));
472         wd->group->radios = eina_list_append(wd->group->radios, obj);
473      }
474    else if (wd->group == wd2->group) return;
475    else
476      {
477         wd->group->radios = eina_list_remove(wd->group->radios, obj);
478         if (!wd->group->radios) free(wd->group);
479         wd->group = wd2->group;
480         wd->group->radios = eina_list_append(wd->group->radios, obj);
481      }
482    if (wd->value == wd->group->value) _state_set(obj, 1);
483    else _state_set(obj, 0);
484 }
485
486 /**
487  * Set the integer value that this radio object represents
488  *
489  * This sets the value of the radio.
490  *
491  * @param obj The radio object
492  * @param value The value to use if this radio object is selected
493  *
494  * @ingroup Radio
495  */
496 EAPI void
497 elm_radio_state_value_set(Evas_Object *obj, int value)
498 {
499    ELM_CHECK_WIDTYPE(obj, widtype);
500    Widget_Data *wd = elm_widget_data_get(obj);
501    if (!wd) return;
502    wd->value = value;
503    if (wd->value == wd->group->value) _state_set(obj, 1);
504    else _state_set(obj, 0);
505 }
506
507 /**
508  * Get the integer value that this radio object represents
509  *
510  * This gets the value of the radio.
511  *
512  * @param obj The radio object
513  * @return The value used if this radio object is selected
514  *
515  * @ingroup Radio
516  */
517 EAPI int
518 elm_radio_state_value_get(const Evas_Object *obj)
519 {
520    ELM_CHECK_WIDTYPE(obj, widtype) 0;
521    Widget_Data *wd = elm_widget_data_get(obj);
522    if (!wd) return 0;
523    return wd->value;
524 }
525
526 /**
527  * Set the value of the radio.
528  *
529  * This sets the value of the radio group and will also set the value if
530  * pointed to, to the value supplied, but will not call any callbacks.
531  *
532  * @param obj The radio object
533  * @param value The value to use for the group
534  *
535  * @ingroup Radio
536  */
537 EAPI void
538 elm_radio_value_set(Evas_Object *obj, int value)
539 {
540    ELM_CHECK_WIDTYPE(obj, widtype);
541    Widget_Data *wd = elm_widget_data_get(obj);
542    if (!wd) return;
543    if (value == wd->group->value) return;
544    wd->group->value = value;
545    if (wd->group->valuep) *(wd->group->valuep) = wd->group->value;
546    _state_set_all(wd);
547 }
548
549 /**
550  * Get the state of the radio object
551  *
552  * @param obj The radio object
553  * @return The integer state
554  *
555  * @ingroup Radio
556  */
557 EAPI int
558 elm_radio_value_get(const Evas_Object *obj)
559 {
560    ELM_CHECK_WIDTYPE(obj, widtype) 0;
561    Widget_Data *wd = elm_widget_data_get(obj);
562    if (!wd) return 0;
563    return wd->group->value;
564 }
565
566 /**
567  * Set a convenience pointer to a integer to change
568  *
569  * This sets a pointer to a integer, that, in addition to the radio objects
570  * state will also be modified directly. To stop setting the object pointed
571  * to simply use NULL as the valuep parameter. If valuep is not NULL, then
572  * when this is called, the radio objects state will also be modified to
573  * reflect the value of the integer valuep points to, just like calling
574  * elm_radio_value_set().
575  *
576  * @param obj The radio object
577  * @param valuep Pointer to the integer to modify
578  *
579  * @ingroup Radio
580  */
581 EAPI void
582 elm_radio_value_pointer_set(Evas_Object *obj, int *valuep)
583 {
584    ELM_CHECK_WIDTYPE(obj, widtype);
585    Widget_Data *wd = elm_widget_data_get(obj);
586    if (!wd) return;
587    if (valuep)
588      {
589         wd->group->valuep = valuep;
590         if (*(wd->group->valuep) != wd->group->value)
591           {
592              wd->group->value = *(wd->group->valuep);
593              _state_set_all(wd);
594           }
595      }
596    else
597      {
598         wd->group->valuep = NULL;
599      }
600 }