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