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