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);
63 static void _on_focus_hook(void *data, Evas_Object *obj);
64 static void _activate(Evas_Object *obj);
65 static void _activate_hook(Evas_Object *obj);
66 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
67 Evas_Callback_Type type, void *event_info);
69 static const char SIG_CHANGED[] = "changed";
70 static const Evas_Smart_Cb_Description _signals[] = {
76 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
78 if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
79 Evas_Event_Key_Down *ev = event_info;
80 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
81 if (elm_widget_disabled_get(obj)) return EINA_FALSE;
83 if ((strcmp(ev->keyname, "Return")) &&
84 (strcmp(ev->keyname, "KP_Enter")) &&
85 (strcmp(ev->keyname, "space")))
88 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
93 _del_hook(Evas_Object *obj)
95 Widget_Data *wd = elm_widget_data_get(obj);
97 if (wd->label) eina_stringshare_del(wd->label);
98 wd->group->radios = eina_list_remove(wd->group->radios, obj);
99 if (!wd->group->radios) free(wd->group);
105 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
107 Widget_Data *wd = elm_widget_data_get(obj);
109 if (elm_widget_focus_get(obj))
111 edje_object_signal_emit(wd->radio, "elm,action,focus", "elm");
112 evas_object_focus_set(wd->radio, EINA_TRUE);
116 edje_object_signal_emit(wd->radio, "elm,action,unfocus", "elm");
117 evas_object_focus_set(wd->radio, EINA_FALSE);
122 _theme_hook(Evas_Object *obj)
124 Widget_Data *wd = elm_widget_data_get(obj);
126 _elm_theme_object_set(obj, wd->radio, "radio", "base", elm_widget_style_get(obj));
128 edje_object_signal_emit(wd->radio, "elm,state,icon,visible", "elm");
130 edje_object_signal_emit(wd->radio, "elm,state,icon,hidden", "elm");
132 edje_object_signal_emit(wd->radio, "elm,state,radio,on", "elm");
134 edje_object_signal_emit(wd->radio, "elm,state,radio,off", "elm");
136 edje_object_signal_emit(wd->radio, "elm,state,text,visible", "elm");
138 edje_object_signal_emit(wd->radio, "elm,state,text,hidden", "elm");
139 edje_object_part_text_set(wd->radio, "elm.text", wd->label);
140 if (elm_widget_disabled_get(obj))
142 edje_object_signal_emit(wd->radio, "elm,state,disabled", "elm");
143 if (wd->state) _state_set(obj, 0);
145 edje_object_message_signal_process(wd->radio);
146 edje_object_scale_set(wd->radio, elm_widget_scale_get(obj) * _elm_config->scale);
151 _disable_hook(Evas_Object *obj)
153 Widget_Data *wd = elm_widget_data_get(obj);
155 if (elm_widget_disabled_get(obj))
157 edje_object_signal_emit(wd->radio, "elm,state,disabled", "elm");
158 if (wd->state) _state_set(obj, 0);
161 edje_object_signal_emit(wd->radio, "elm,state,enabled", "elm");
165 _sizing_eval(Evas_Object *obj)
167 Widget_Data *wd = elm_widget_data_get(obj);
168 Evas_Coord minw = -1, minh = -1;
170 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
171 edje_object_size_min_restricted_calc(wd->radio, &minw, &minh, minw, minh);
172 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
173 evas_object_size_hint_min_set(obj, minw, minh);
174 evas_object_size_hint_max_set(obj, -1, -1);
178 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
180 Widget_Data *wd = elm_widget_data_get(data);
182 if (obj != wd->icon) return;
187 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
189 Widget_Data *wd = elm_widget_data_get(obj);
190 Evas_Object *sub = event_info;
194 edje_object_signal_emit(wd->radio, "elm,state,icon,hidden", "elm");
195 evas_object_event_callback_del_full
196 (sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
203 _state_set(Evas_Object *obj, Eina_Bool state)
205 Widget_Data *wd = elm_widget_data_get(obj);
207 if ((state != wd->state) && (!elm_widget_disabled_get(obj)))
211 edje_object_signal_emit(wd->radio, "elm,state,radio,on", "elm");
213 edje_object_signal_emit(wd->radio, "elm,state,radio,off", "elm");
218 _state_set_all(Widget_Data *wd)
221 Evas_Object *child, *selected = NULL;
222 Eina_Bool disabled = EINA_FALSE;
223 EINA_LIST_FOREACH(wd->group->radios, l, child)
225 Widget_Data *wd2 = elm_widget_data_get(child);
226 if (wd2->state) selected = child;
227 if (wd2->value == wd->group->value)
229 _state_set(child, 1);
230 if (!wd2->state) disabled = EINA_TRUE;
232 else _state_set(child, 0);
234 if ((disabled) && (selected)) _state_set(selected, 1);
238 _activate(Evas_Object *obj)
240 Widget_Data *wd = elm_widget_data_get(obj);
242 if (wd->group->value == wd->value) return;
243 wd->group->value = wd->value;
244 if (wd->group->valuep) *(wd->group->valuep) = wd->group->value;
246 evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
250 _activate_hook(Evas_Object *obj)
256 _signal_radio_on(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
262 * Add a new radio to the parent
264 * @param parent The parent object
265 * @return The new object or NULL if it cannot be created
270 elm_radio_add(Evas_Object *parent)
276 EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
278 wd = ELM_NEW(Widget_Data);
279 e = evas_object_evas_get(parent);
281 obj = elm_widget_add(e);
282 ELM_SET_WIDTYPE(widtype, "radio");
283 elm_widget_type_set(obj, "radio");
284 elm_widget_sub_object_add(parent, obj);
285 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
286 elm_widget_data_set(obj, wd);
287 elm_widget_del_hook_set(obj, _del_hook);
288 elm_widget_theme_hook_set(obj, _theme_hook);
289 elm_widget_disable_hook_set(obj, _disable_hook);
290 elm_widget_can_focus_set(obj, EINA_TRUE);
291 elm_widget_activate_hook_set(obj, _activate_hook);
292 elm_widget_event_hook_set(obj, _event_hook);
294 wd->radio = edje_object_add(e);
295 _elm_theme_object_set(obj, wd->radio, "radio", "base", "default");
296 edje_object_signal_callback_add(wd->radio, "elm,action,radio,on", "", _signal_radio_on, obj);
297 edje_object_signal_callback_add(wd->radio, "elm,action,radio,toggle", "", _signal_radio_on, obj);
298 elm_widget_resize_object_set(obj, wd->radio);
300 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
302 wd->group = calloc(1, sizeof(Group));
303 wd->group->radios = eina_list_append(wd->group->radios, obj);
308 // TODO: convert Elementary to subclassing of Evas_Smart_Class
309 // TODO: and save some bytes, making descriptions per-class and not instance!
310 evas_object_smart_callbacks_descriptions_set(obj, _signals);
315 * Set the text label of the radio object
317 * @param obj The radio object
318 * @param label The text label string in UTF-8
323 elm_radio_label_set(Evas_Object *obj, const char *label)
325 ELM_CHECK_WIDTYPE(obj, widtype);
326 Widget_Data *wd = elm_widget_data_get(obj);
328 eina_stringshare_replace(&wd->label, label);
331 edje_object_signal_emit(wd->radio, "elm,state,text,visible", "elm");
332 edje_object_message_signal_process(wd->radio);
336 edje_object_signal_emit(wd->radio, "elm,state,text,hidden", "elm");
337 edje_object_message_signal_process(wd->radio);
339 edje_object_part_text_set(wd->radio, "elm.text", label);
344 * Get the text label of the radio object
346 * @param obj The radio object
347 * @return The text label string in UTF-8
352 elm_radio_label_get(const Evas_Object *obj)
354 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
355 Widget_Data *wd = elm_widget_data_get(obj);
356 if (!wd) return NULL;
361 * Set the icon object of the radio object
363 * Once the icon object is set, a previously set one will be deleted.
364 * If you want to keep that old content object, use the
365 * elm_radio_icon_unset() function.
367 * @param obj The radio object
368 * @param icon The icon object
373 elm_radio_icon_set(Evas_Object *obj, Evas_Object *icon)
375 ELM_CHECK_WIDTYPE(obj, widtype);
376 Widget_Data *wd = elm_widget_data_get(obj);
378 if (wd->icon == icon) return;
379 if (wd->icon) evas_object_del(wd->icon);
383 elm_widget_sub_object_add(obj, icon);
384 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
385 _changed_size_hints, obj);
386 edje_object_part_swallow(wd->radio, "elm.swallow.content", icon);
387 edje_object_signal_emit(wd->radio, "elm,state,icon,visible", "elm");
388 edje_object_message_signal_process(wd->radio);
394 * Get the icon object of the radio object
396 * @param obj The radio object
397 * @return The icon object
402 elm_radio_icon_get(const Evas_Object *obj)
404 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
405 Widget_Data *wd = elm_widget_data_get(obj);
406 if (!wd) return NULL;
411 * Unset the icon used for the radio object
413 * Unparent and return the icon object which was set for this widget.
415 * @param obj The radio object
416 * @return The icon object that was being used
421 elm_radio_icon_unset(Evas_Object *obj)
423 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
424 Widget_Data *wd = elm_widget_data_get(obj);
425 if (!wd) return NULL;
426 if (!wd->icon) return NULL;
427 Evas_Object *icon = wd->icon;
428 elm_widget_sub_object_del(obj, wd->icon);
429 edje_object_part_unswallow(wd->radio, wd->icon);
435 * Add this radio to a group of other radio objects
437 * Radio objects work in groups. Each member should have a different integer
438 * value assigned. In order ro have them work as a group, they need to know
439 * about eacthother. This adds the given radio object to the group of which
440 * the group object indicated is a member.
442 * @param obj The radio object
443 * @param group The object whose group the object is to join
448 elm_radio_group_add(Evas_Object *obj, Evas_Object *group)
450 ELM_CHECK_WIDTYPE(obj, widtype);
451 Widget_Data *wd = elm_widget_data_get(obj);
452 Widget_Data *wd2 = elm_widget_data_get(group);
456 if (eina_list_count(wd->group->radios) == 1)
458 wd->group->radios = eina_list_remove(wd->group->radios, obj);
459 wd->group = calloc(1, sizeof(Group));
460 wd->group->radios = eina_list_append(wd->group->radios, obj);
462 else if (wd->group == wd2->group) return;
465 wd->group->radios = eina_list_remove(wd->group->radios, obj);
466 if (!wd->group->radios) free(wd->group);
467 wd->group = wd2->group;
468 wd->group->radios = eina_list_append(wd->group->radios, obj);
470 if (wd->value == wd->group->value) _state_set(obj, 1);
471 else _state_set(obj, 0);
475 * Set the integer value that this radio object represents
477 * This sets the value of the radio.
479 * @param obj The radio object
480 * @param value The value to use if this radio object is selected
485 elm_radio_state_value_set(Evas_Object *obj, int value)
487 ELM_CHECK_WIDTYPE(obj, widtype);
488 Widget_Data *wd = elm_widget_data_get(obj);
491 if (wd->value == wd->group->value) _state_set(obj, 1);
492 else _state_set(obj, 0);
496 * Get the integer value that this radio object represents
498 * This gets the value of the radio.
500 * @param obj The radio object
501 * @return The value used if this radio object is selected
506 elm_radio_state_value_get(const Evas_Object *obj)
508 ELM_CHECK_WIDTYPE(obj, widtype) 0;
509 Widget_Data *wd = elm_widget_data_get(obj);
515 * Set the value of the radio.
517 * This sets the value of the radio group and will also set the value if
518 * pointed to, to the value supplied, but will not call any callbacks.
520 * @param obj The radio object
521 * @param value The value to use for the group
526 elm_radio_value_set(Evas_Object *obj, int value)
528 ELM_CHECK_WIDTYPE(obj, widtype);
529 Widget_Data *wd = elm_widget_data_get(obj);
531 if (value == wd->group->value) return;
532 wd->group->value = value;
533 if (wd->group->valuep) *(wd->group->valuep) = wd->group->value;
538 * Get the state of the radio object
540 * @param obj The radio object
541 * @return The integer state
546 elm_radio_value_get(const Evas_Object *obj)
548 ELM_CHECK_WIDTYPE(obj, widtype) 0;
549 Widget_Data *wd = elm_widget_data_get(obj);
551 return wd->group->value;
555 * Set a convenience pointer to a integer to change
557 * This sets a pointer to a integer, that, in addition to the radio objects
558 * state will also be modified directly. To stop setting the object pointed
559 * to simply use NULL as the valuep parameter. If valuep is not NULL, then
560 * when this is called, the radio objects state will also be modified to
561 * reflect the value of the integer valuep points to, just like calling
562 * elm_radio_value_set().
564 * @param obj The radio object
565 * @param valuep Pointer to the integer to modify
570 elm_radio_value_pointer_set(Evas_Object *obj, int *valuep)
572 ELM_CHECK_WIDTYPE(obj, widtype);
573 Widget_Data *wd = elm_widget_data_get(obj);
577 wd->group->valuep = valuep;
578 if (*(wd->group->valuep) != wd->group->value)
580 wd->group->value = *(wd->group->valuep);
586 wd->group->valuep = NULL;