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->radio, "radio", "base", elm_widget_style_get(obj));
89 edje_object_signal_emit(wd->radio, "elm,state,icon,visible", "elm");
91 edje_object_signal_emit(wd->radio, "elm,state,icon,hidden", "elm");
93 edje_object_signal_emit(wd->radio, "elm,state,radio,on", "elm");
95 edje_object_signal_emit(wd->radio, "elm,state,radio,off", "elm");
97 edje_object_signal_emit(wd->radio, "elm,state,text,visible", "elm");
99 edje_object_signal_emit(wd->radio, "elm,state,text,hidden", "elm");
100 edje_object_part_text_set(wd->radio, "elm.text", wd->label);
101 edje_object_message_signal_process(wd->radio);
102 edje_object_scale_set(wd->radio, 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->radio, "elm,state,disabled", "elm");
114 if (wd->state) _state_set(obj, 0);
117 edje_object_signal_emit(wd->radio, "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->radio, &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->radio, "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->radio, "elm,state,radio,on", "elm");
169 edje_object_signal_emit(wd->radio, "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[in] 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->radio = edje_object_add(e);
232 _elm_theme_object_set(obj, wd->radio, "radio", "base", "default");
233 edje_object_signal_callback_add(wd->radio, "elm,action,radio,on", "", _signal_radio_on, obj);
234 edje_object_signal_callback_add(wd->radio, "elm,action,radio,toggle", "", _signal_radio_on, obj);
235 elm_widget_resize_object_set(obj, wd->radio);
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[in] obj The radio object
255 * @param[in] 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->radio, "elm,state,text,visible", "elm");
269 edje_object_message_signal_process(wd->radio);
273 edje_object_signal_emit(wd->radio, "elm,state,text,hidden", "elm");
274 edje_object_message_signal_process(wd->radio);
276 edje_object_part_text_set(wd->radio, "elm.text", label);
281 * Get the text label of the radio object
283 * @param[in] 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, a previously set one will be deleted.
302 * @param[in] obj The radio object
303 * @param[in] icon The icon object
308 elm_radio_icon_set(Evas_Object *obj, Evas_Object *icon)
310 ELM_CHECK_WIDTYPE(obj, widtype);
311 Widget_Data *wd = elm_widget_data_get(obj);
313 if (wd->icon == icon) return;
314 if (wd->icon) evas_object_del(wd->icon);
318 elm_widget_sub_object_add(obj, icon);
319 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
320 _changed_size_hints, obj);
321 edje_object_part_swallow(wd->radio, "elm.swallow.content", icon);
322 edje_object_signal_emit(wd->radio, "elm,state,icon,visible", "elm");
323 edje_object_message_signal_process(wd->radio);
329 * Get the icon object of the radio object
331 * @param[in] obj The radio object
332 * @return The icon object
337 elm_radio_icon_get(const Evas_Object *obj)
339 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
340 Widget_Data *wd = elm_widget_data_get(obj);
341 if (!wd) return NULL;
346 * Add this radio to a group of other radio objects
348 * Radio objects work in groups. Each member should have a different integer
349 * value assigned. In order ro have them work as a group, they need to know
350 * about eacthother. This adds the given radio object to the group of which
351 * the group object indicated is a member.
353 * @param[in] obj The radio object
354 * @param[in] group The object whose group the object is to join
359 elm_radio_group_add(Evas_Object *obj, Evas_Object *group)
361 ELM_CHECK_WIDTYPE(obj, widtype);
362 Widget_Data *wd = elm_widget_data_get(obj);
363 Widget_Data *wd2 = elm_widget_data_get(group);
367 if (eina_list_count(wd->group->radios) == 1)
369 wd->group->radios = eina_list_remove(wd->group->radios, obj);
370 wd->group = calloc(1, sizeof(Group));
371 wd->group->radios = eina_list_append(wd->group->radios, obj);
373 else if (wd->group == wd2->group) return;
376 wd->group->radios = eina_list_remove(wd->group->radios, obj);
377 if (!wd->group->radios) free(wd->group);
378 wd->group = wd2->group;
379 wd->group->radios = eina_list_append(wd->group->radios, obj);
381 if (wd->value == wd->group->value) _state_set(obj, 1);
382 else _state_set(obj, 0);
386 * Set the integer value that this radio object represents
388 * This sets the value of the radio.
390 * @param[in] obj The radio object
391 * @param[in] value The value to use if this radio object is selected
396 elm_radio_state_value_set(Evas_Object *obj, int value)
398 ELM_CHECK_WIDTYPE(obj, widtype);
399 Widget_Data *wd = elm_widget_data_get(obj);
402 if (wd->value == wd->group->value) _state_set(obj, 1);
403 else _state_set(obj, 0);
407 * Set the value of the radio.
409 * This sets the value of the radio group and will also set the value if
410 * pointed to, to the value supplied, but will not call any callbacks.
412 * @param[in] obj The radio object
413 * @param[in] value The value to use for the group
418 elm_radio_value_set(Evas_Object *obj, int value)
420 ELM_CHECK_WIDTYPE(obj, widtype);
421 Widget_Data *wd = elm_widget_data_get(obj);
423 if (value == wd->group->value) return;
424 wd->group->value = value;
425 if (wd->group->valuep) *(wd->group->valuep) = wd->group->value;
430 * Get the state of the radio object
432 * @param[in] obj The radio object
433 * @return The integer state
438 elm_radio_value_get(const Evas_Object *obj)
440 ELM_CHECK_WIDTYPE(obj, widtype) 0;
441 Widget_Data *wd = elm_widget_data_get(obj);
443 return wd->group->value;
447 * Set a convenience pointer to a integer to change
449 * This sets a pointer to a integer, that, in addition to the radio objects
450 * state will also be modified directly. To stop setting the object pointed
451 * to simply use NULL as the valuep parameter. If valuep is not NULL, then
452 * when this is called, the radio objects state will also be modified to
453 * reflect the value of the integer valuep points to, just like calling
454 * elm_radio_value_set().
456 * @param[in] obj The radio object
457 * @param[in] valuep Pointer to the integer to modify
462 elm_radio_value_pointer_set(Evas_Object *obj, int *valuep)
464 ELM_CHECK_WIDTYPE(obj, widtype);
465 Widget_Data *wd = elm_widget_data_get(obj);
469 wd->group->valuep = valuep;
470 if (*(wd->group->valuep) != wd->group->value)
472 wd->group->value = *(wd->group->valuep);
478 wd->group->valuep = NULL;