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