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