1 #include <Elementary.h>
5 * @defgroup Radio Radio
7 * The radio button allows for 1 or more selectors to be created to select 1
10 * Signals that you can add callbacks for are:
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.
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.
33 typedef struct _Widget_Data Widget_Data;
34 typedef struct _Group Group;
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);
63 static const char SIG_CHANGED[] = "changed";
64 static const Evas_Smart_Cb_Description _signals[] = {
70 _del_hook(Evas_Object *obj)
72 Widget_Data *wd = elm_widget_data_get(obj);
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);
82 _theme_hook(Evas_Object *obj)
84 Widget_Data *wd = elm_widget_data_get(obj);
86 _elm_theme_object_set(obj, wd->chk, "radio", "base", elm_widget_style_get(obj));
88 edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
90 edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
92 edje_object_signal_emit(wd->chk, "elm,state,radio,on", "elm");
94 edje_object_signal_emit(wd->chk, "elm,state,radio,off", "elm");
96 edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
98 edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
99 edje_object_part_text_set(wd->chk, "elm.text", wd->label);
100 edje_object_message_signal_process(wd->chk);
101 edje_object_scale_set(wd->chk, elm_widget_scale_get(obj) * _elm_config->scale);
106 _disable_hook(Evas_Object *obj)
108 Widget_Data *wd = elm_widget_data_get(obj);
110 if (elm_widget_disabled_get(obj))
112 edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
113 if (wd->state) _state_set(obj, 0);
116 edje_object_signal_emit(wd->chk, "elm,state,enabled", "elm");
120 _sizing_eval(Evas_Object *obj)
122 Widget_Data *wd = elm_widget_data_get(obj);
123 Evas_Coord minw = -1, minh = -1;
125 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
126 edje_object_size_min_restricted_calc(wd->chk, &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);
133 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
135 Widget_Data *wd = elm_widget_data_get(data);
137 if (obj != wd->icon) return;
142 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
144 Widget_Data *wd = elm_widget_data_get(obj);
145 Evas_Object *sub = event_info;
149 edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
150 evas_object_event_callback_del_full
151 (sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
158 _state_set(Evas_Object *obj, Eina_Bool state)
160 Widget_Data *wd = elm_widget_data_get(obj);
162 if ((state != wd->state) && (!elm_widget_disabled_get(obj)))
166 edje_object_signal_emit(wd->chk, "elm,state,radio,on", "elm");
168 edje_object_signal_emit(wd->chk, "elm,state,radio,off", "elm");
173 _state_set_all(Widget_Data *wd)
176 Evas_Object *child, *selected = NULL;
177 Eina_Bool disabled = EINA_FALSE;
178 EINA_LIST_FOREACH(wd->group->radios, l, child)
180 Widget_Data *wd2 = elm_widget_data_get(child);
181 if (wd2->state) selected = child;
182 if (wd2->value == wd->group->value)
184 _state_set(child, 1);
185 if (!wd2->state) disabled = EINA_TRUE;
187 else _state_set(child, 0);
189 if (disabled && selected) _state_set(selected, 1);
193 _signal_radio_on(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
195 Widget_Data *wd = elm_widget_data_get(data);
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;
201 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
205 * Add a new radio to the parent
207 * @param parent The parent object
208 * @return The new object or NULL if it cannot be created
213 elm_radio_add(Evas_Object *parent)
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);
230 wd->chk = edje_object_add(e);
231 _elm_theme_object_set(obj, wd->chk, "radio", "base", "default");
232 edje_object_signal_callback_add(wd->chk, "elm,action,radio,on", "", _signal_radio_on, obj);
233 edje_object_signal_callback_add(wd->chk, "elm,action,radio,toggle", "", _signal_radio_on, obj);
234 elm_widget_resize_object_set(obj, wd->chk);
236 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
238 wd->group = calloc(1, sizeof(Group));
239 wd->group->radios = eina_list_append(wd->group->radios, obj);
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);
251 * Set the text label of the radio object
253 * @param obj The radio object
254 * @param label The text label string in UTF-8
259 elm_radio_label_set(Evas_Object *obj, const char *label)
261 ELM_CHECK_WIDTYPE(obj, widtype);
262 Widget_Data *wd = elm_widget_data_get(obj);
264 eina_stringshare_replace(&wd->label, label);
267 edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
268 edje_object_message_signal_process(wd->chk);
272 edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
273 edje_object_message_signal_process(wd->chk);
275 edje_object_part_text_set(wd->chk, "elm.text", label);
280 * Get the text label of the radio object
282 * @param obj The radio object
283 * @return The text label string in UTF-8
288 elm_radio_label_get(const Evas_Object *obj)
290 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
291 Widget_Data *wd = elm_widget_data_get(obj);
292 if (!wd) return NULL;
297 * Set the icon object of the radio object
299 * Once the icon object is set, it will become a child of the radio object and
300 * be deleted when the radio object is deleted. If another icon object is set
301 * then the previous one becomes orophaned and will no longer be deleted along
304 * @param obj The radio object
305 * @param icon The icon object
310 elm_radio_icon_set(Evas_Object *obj, Evas_Object *icon)
312 ELM_CHECK_WIDTYPE(obj, widtype);
313 Widget_Data *wd = elm_widget_data_get(obj);
315 if ((wd->icon != icon) && (wd->icon))
316 elm_widget_sub_object_del(obj, wd->icon);
320 elm_widget_sub_object_add(obj, icon);
321 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
322 _changed_size_hints, obj);
323 edje_object_part_swallow(wd->chk, "elm.swallow.content", icon);
324 edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
330 * Get the icon object of the radio object
332 * @param obj The radio object
333 * @return The icon object
338 elm_radio_icon_get(const Evas_Object *obj)
340 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
341 Widget_Data *wd = elm_widget_data_get(obj);
342 if (!wd) return NULL;
347 * Add this radio to a group of other radio objects
349 * Radio objects work in groups. Each member should have a different integer
350 * value assigned. In order ro have them work as a group, they need to know
351 * about eacthother. This adds the given radio object to the group of which
352 * the group object indicated is a member.
354 * @param obj The radio object
355 * @param group The object whose group the object is to join
360 elm_radio_group_add(Evas_Object *obj, Evas_Object *group)
362 ELM_CHECK_WIDTYPE(obj, widtype);
363 Widget_Data *wd = elm_widget_data_get(obj);
364 Widget_Data *wd2 = elm_widget_data_get(group);
368 if (eina_list_count(wd->group->radios) == 1)
370 wd->group->radios = eina_list_remove(wd->group->radios, obj);
371 wd->group = calloc(1, sizeof(Group));
372 wd->group->radios = eina_list_append(wd->group->radios, obj);
374 else if (wd->group == wd2->group) return;
377 wd->group->radios = eina_list_remove(wd->group->radios, obj);
378 if (!wd->group->radios) free(wd->group);
379 wd->group = wd2->group;
380 wd->group->radios = eina_list_append(wd->group->radios, obj);
382 if (wd->value == wd->group->value) _state_set(obj, 1);
383 else _state_set(obj, 0);
387 * Set the integer value that this radio object represents
389 * This sets the value of the radio.
391 * @param obj The radio object
392 * @param value The value to use if this radio object is selected
397 elm_radio_state_value_set(Evas_Object *obj, int value)
399 ELM_CHECK_WIDTYPE(obj, widtype);
400 Widget_Data *wd = elm_widget_data_get(obj);
403 if (wd->value == wd->group->value) _state_set(obj, 1);
404 else _state_set(obj, 0);
408 * Set the value of the radio.
410 * This sets the value of the radio group and will also set the value if
411 * pointed to, to the value supplied, but will not call any callbacks.
413 * @param obj The radio object
414 * @param value The value to use for the group
419 elm_radio_value_set(Evas_Object *obj, int value)
421 ELM_CHECK_WIDTYPE(obj, widtype);
422 Widget_Data *wd = elm_widget_data_get(obj);
424 if (value == wd->group->value) return;
425 wd->group->value = value;
426 if (wd->group->valuep) *(wd->group->valuep) = wd->group->value;
431 * Get the state of the radio object
433 * @param obj The radio object
434 * @return The integer state
439 elm_radio_value_get(const Evas_Object *obj)
441 ELM_CHECK_WIDTYPE(obj, widtype) 0;
442 Widget_Data *wd = elm_widget_data_get(obj);
444 return wd->group->value;
448 * Set a convenience pointer to a integer to change
450 * This sets a pointer to a integer, that, in addition to the radio objects
451 * state will also be modified directly. To stop setting the object pointed
452 * to simply use NULL as the valuep parameter. If valuep is not NULL, then
453 * when this is called, the radio objects state will also be modified to
454 * reflect the value of the integer valuep points to, just like calling
455 * elm_radio_value_set().
457 * @param obj The radio object
458 * @param valuep Pointer to the integer to modify
463 elm_radio_value_pointer_set(Evas_Object *obj, int *valuep)
465 ELM_CHECK_WIDTYPE(obj, widtype);
466 Widget_Data *wd = elm_widget_data_get(obj);
470 wd->group->valuep = valuep;
471 if (*(wd->group->valuep) != wd->group->value)
473 wd->group->value = *(wd->group->valuep);
479 wd->group->valuep = NULL;