1 #include <Elementary.h>
5 * @defgroup Radio Radio
8 * The radio button allows for 1 or more selectors to be created to select 1
11 * Signals that you can add callbacks for are:
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.
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.
34 typedef struct _Widget_Data Widget_Data;
35 typedef struct _Group Group;
54 static const char *widtype = NULL;
55 static void _state_set(Evas_Object *obj, Eina_Bool state);
56 static void _del_hook(Evas_Object *obj);
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);
64 static const char SIG_CHANGED[] = "changed";
65 static const Evas_Smart_Cb_Description _signals[] = {
71 _del_hook(Evas_Object *obj)
73 Widget_Data *wd = elm_widget_data_get(obj);
75 if (wd->label) eina_stringshare_del(wd->label);
76 wd->group->radios = eina_list_remove(wd->group->radios, obj);
77 if (!wd->group->radios) free(wd->group);
83 _theme_hook(Evas_Object *obj)
85 Widget_Data *wd = elm_widget_data_get(obj);
87 _elm_theme_object_set(obj, wd->chk, "radio", "base", elm_widget_style_get(obj));
89 edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
91 edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
93 edje_object_signal_emit(wd->chk, "elm,state,radio,on", "elm");
95 edje_object_signal_emit(wd->chk, "elm,state,radio,off", "elm");
97 edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
99 edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
100 edje_object_part_text_set(wd->chk, "elm.text", wd->label);
101 edje_object_message_signal_process(wd->chk);
102 edje_object_scale_set(wd->chk, elm_widget_scale_get(obj) * _elm_config->scale);
107 _disable_hook(Evas_Object *obj)
109 Widget_Data *wd = elm_widget_data_get(obj);
111 if (elm_widget_disabled_get(obj))
113 edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
114 if (wd->state) _state_set(obj, 0);
117 edje_object_signal_emit(wd->chk, "elm,state,enabled", "elm");
121 _sizing_eval(Evas_Object *obj)
123 Widget_Data *wd = elm_widget_data_get(obj);
124 Evas_Coord minw = -1, minh = -1;
126 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
127 edje_object_size_min_restricted_calc(wd->chk, &minw, &minh, minw, minh);
128 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
129 evas_object_size_hint_min_set(obj, minw, minh);
130 evas_object_size_hint_max_set(obj, -1, -1);
134 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
136 Widget_Data *wd = elm_widget_data_get(data);
138 if (obj != wd->icon) return;
143 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
145 Widget_Data *wd = elm_widget_data_get(obj);
146 Evas_Object *sub = event_info;
150 edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
151 evas_object_event_callback_del_full
152 (sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
159 _state_set(Evas_Object *obj, Eina_Bool state)
161 Widget_Data *wd = elm_widget_data_get(obj);
163 if ((state != wd->state) && (!elm_widget_disabled_get(obj)))
167 edje_object_signal_emit(wd->chk, "elm,state,radio,on", "elm");
169 edje_object_signal_emit(wd->chk, "elm,state,radio,off", "elm");
174 _state_set_all(Widget_Data *wd)
177 Evas_Object *child, *selected = NULL;
178 Eina_Bool disabled = EINA_FALSE;
179 EINA_LIST_FOREACH(wd->group->radios, l, child)
181 Widget_Data *wd2 = elm_widget_data_get(child);
182 if (wd2->state) selected = child;
183 if (wd2->value == wd->group->value)
185 _state_set(child, 1);
186 if (!wd2->state) disabled = EINA_TRUE;
188 else _state_set(child, 0);
190 if (disabled && selected) _state_set(selected, 1);
194 _signal_radio_on(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
196 Widget_Data *wd = elm_widget_data_get(data);
198 if (wd->group->value == wd->value) return;
199 wd->group->value = wd->value;
200 if (wd->group->valuep) *(wd->group->valuep) = wd->group->value;
202 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
206 * Add a new radio to the parent
208 * @param parent The parent object
209 * @return The new object or NULL if it cannot be created
214 elm_radio_add(Evas_Object *parent)
220 wd = ELM_NEW(Widget_Data);
221 e = evas_object_evas_get(parent);
222 obj = elm_widget_add(e);
223 ELM_SET_WIDTYPE(widtype, "radio");
224 elm_widget_type_set(obj, "radio");
225 elm_widget_sub_object_add(parent, obj);
226 elm_widget_data_set(obj, wd);
227 elm_widget_del_hook_set(obj, _del_hook);
228 elm_widget_theme_hook_set(obj, _theme_hook);
229 elm_widget_disable_hook_set(obj, _disable_hook);
231 wd->chk = edje_object_add(e);
232 _elm_theme_object_set(obj, wd->chk, "radio", "base", "default");
233 edje_object_signal_callback_add(wd->chk, "elm,action,radio,on", "", _signal_radio_on, obj);
234 edje_object_signal_callback_add(wd->chk, "elm,action,radio,toggle", "", _signal_radio_on, obj);
235 elm_widget_resize_object_set(obj, wd->chk);
237 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
239 wd->group = calloc(1, sizeof(Group));
240 wd->group->radios = eina_list_append(wd->group->radios, obj);
245 // TODO: convert Elementary to subclassing of Evas_Smart_Class
246 // TODO: and save some bytes, making descriptions per-class and not instance!
247 evas_object_smart_callbacks_descriptions_set(obj, _signals);
252 * Set the text label of the radio object
254 * @param obj The radio object
255 * @param label The text label string in UTF-8
260 elm_radio_label_set(Evas_Object *obj, const char *label)
262 ELM_CHECK_WIDTYPE(obj, widtype);
263 Widget_Data *wd = elm_widget_data_get(obj);
265 eina_stringshare_replace(&wd->label, label);
268 edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
269 edje_object_message_signal_process(wd->chk);
273 edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
274 edje_object_message_signal_process(wd->chk);
276 edje_object_part_text_set(wd->chk, "elm.text", label);
281 * Get the text label of the radio object
283 * @param obj The radio object
284 * @return The text label string in UTF-8
289 elm_radio_label_get(const Evas_Object *obj)
291 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
292 Widget_Data *wd = elm_widget_data_get(obj);
293 if (!wd) return NULL;
298 * Set the icon object of the radio object
300 * Once the icon object is set, it will become a child of the radio object and
301 * be deleted when the radio object is deleted. If another icon object is set
302 * then the previous one becomes orophaned and will no longer be deleted along
305 * @param obj The radio object
306 * @param icon The icon object
311 elm_radio_icon_set(Evas_Object *obj, Evas_Object *icon)
313 ELM_CHECK_WIDTYPE(obj, widtype);
314 Widget_Data *wd = elm_widget_data_get(obj);
316 if ((wd->icon != icon) && (wd->icon))
317 elm_widget_sub_object_del(obj, wd->icon);
321 elm_widget_sub_object_add(obj, icon);
322 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
323 _changed_size_hints, obj);
324 edje_object_part_swallow(wd->chk, "elm.swallow.content", icon);
325 edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
331 * Get the icon object of the radio object
333 * @param obj The radio object
334 * @return The icon object
339 elm_radio_icon_get(const Evas_Object *obj)
341 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
342 Widget_Data *wd = elm_widget_data_get(obj);
343 if (!wd) return NULL;
348 * Add this radio to a group of other radio objects
350 * Radio objects work in groups. Each member should have a different integer
351 * value assigned. In order ro have them work as a group, they need to know
352 * about eacthother. This adds the given radio object to the group of which
353 * the group object indicated is a member.
355 * @param obj The radio object
356 * @param group The object whose group the object is to join
361 elm_radio_group_add(Evas_Object *obj, Evas_Object *group)
363 ELM_CHECK_WIDTYPE(obj, widtype);
364 Widget_Data *wd = elm_widget_data_get(obj);
365 Widget_Data *wd2 = elm_widget_data_get(group);
369 if (eina_list_count(wd->group->radios) == 1)
371 wd->group->radios = eina_list_remove(wd->group->radios, obj);
372 wd->group = calloc(1, sizeof(Group));
373 wd->group->radios = eina_list_append(wd->group->radios, obj);
375 else if (wd->group == wd2->group) return;
378 wd->group->radios = eina_list_remove(wd->group->radios, obj);
379 if (!wd->group->radios) free(wd->group);
380 wd->group = wd2->group;
381 wd->group->radios = eina_list_append(wd->group->radios, obj);
383 if (wd->value == wd->group->value) _state_set(obj, 1);
384 else _state_set(obj, 0);
388 * Set the integer value that this radio object represents
390 * This sets the value of the radio.
392 * @param obj The radio object
393 * @param value The value to use if this radio object is selected
398 elm_radio_state_value_set(Evas_Object *obj, int value)
400 ELM_CHECK_WIDTYPE(obj, widtype);
401 Widget_Data *wd = elm_widget_data_get(obj);
404 if (wd->value == wd->group->value) _state_set(obj, 1);
405 else _state_set(obj, 0);
409 * Set the value of the radio.
411 * This sets the value of the radio group and will also set the value if
412 * pointed to, to the value supplied, but will not call any callbacks.
414 * @param obj The radio object
415 * @param value The value to use for the group
420 elm_radio_value_set(Evas_Object *obj, int value)
422 ELM_CHECK_WIDTYPE(obj, widtype);
423 Widget_Data *wd = elm_widget_data_get(obj);
425 if (value == wd->group->value) return;
426 wd->group->value = value;
427 if (wd->group->valuep) *(wd->group->valuep) = wd->group->value;
432 * Get the state of the radio object
434 * @param obj The radio object
435 * @return The integer state
440 elm_radio_value_get(const Evas_Object *obj)
442 ELM_CHECK_WIDTYPE(obj, widtype) 0;
443 Widget_Data *wd = elm_widget_data_get(obj);
445 return wd->group->value;
449 * Set a convenience pointer to a integer to change
451 * This sets a pointer to a integer, that, in addition to the radio objects
452 * state will also be modified directly. To stop setting the object pointed
453 * to simply use NULL as the valuep parameter. If valuep is not NULL, then
454 * when this is called, the radio objects state will also be modified to
455 * reflect the value of the integer valuep points to, just like calling
456 * elm_radio_value_set().
458 * @param obj The radio object
459 * @param valuep Pointer to the integer to modify
464 elm_radio_value_pointer_set(Evas_Object *obj, int *valuep)
466 ELM_CHECK_WIDTYPE(obj, widtype);
467 Widget_Data *wd = elm_widget_data_get(obj);
471 wd->group->valuep = valuep;
472 if (*(wd->group->valuep) != wd->group->value)
474 wd->group->value = *(wd->group->valuep);
480 wd->group->valuep = NULL;