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);
62 static void _on_focus_hook(void *data, Evas_Object *obj);
63 static void _activate(Evas_Object *obj);
64 static void _activate_hook(Evas_Object *obj);
65 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
66 Evas_Callback_Type type, void *event_info);
68 static const char SIG_CHANGED[] = "changed";
69 static const Evas_Smart_Cb_Description _signals[] = {
75 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
77 if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
78 Evas_Event_Key_Down *ev = event_info;
79 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
80 if (elm_widget_disabled_get(obj)) return EINA_FALSE;
82 if ((strcmp(ev->keyname, "Return")) &&
83 (strcmp(ev->keyname, "KP_Enter")) &&
84 (strcmp(ev->keyname, "space")))
87 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
92 _del_hook(Evas_Object *obj)
94 Widget_Data *wd = elm_widget_data_get(obj);
96 if (wd->label) eina_stringshare_del(wd->label);
97 wd->group->radios = eina_list_remove(wd->group->radios, obj);
98 if (!wd->group->radios) free(wd->group);
104 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
106 Widget_Data *wd = elm_widget_data_get(obj);
108 if (elm_widget_focus_get(obj))
110 edje_object_signal_emit(wd->radio, "elm,action,focus", "elm");
111 evas_object_focus_set(wd->radio, EINA_TRUE);
115 edje_object_signal_emit(wd->radio, "elm,action,unfocus", "elm");
116 evas_object_focus_set(wd->radio, EINA_FALSE);
121 _theme_hook(Evas_Object *obj)
123 Widget_Data *wd = elm_widget_data_get(obj);
125 _elm_theme_object_set(obj, wd->radio, "radio", "base", elm_widget_style_get(obj));
127 edje_object_signal_emit(wd->radio, "elm,state,icon,visible", "elm");
129 edje_object_signal_emit(wd->radio, "elm,state,icon,hidden", "elm");
131 edje_object_signal_emit(wd->radio, "elm,state,radio,on", "elm");
133 edje_object_signal_emit(wd->radio, "elm,state,radio,off", "elm");
135 edje_object_signal_emit(wd->radio, "elm,state,text,visible", "elm");
137 edje_object_signal_emit(wd->radio, "elm,state,text,hidden", "elm");
138 edje_object_part_text_set(wd->radio, "elm.text", wd->label);
139 if (elm_widget_disabled_get(obj))
141 edje_object_signal_emit(wd->radio, "elm,state,disabled", "elm");
142 if (wd->state) _state_set(obj, 0);
144 edje_object_message_signal_process(wd->radio);
145 edje_object_scale_set(wd->radio, elm_widget_scale_get(obj) * _elm_config->scale);
150 _disable_hook(Evas_Object *obj)
152 Widget_Data *wd = elm_widget_data_get(obj);
154 if (elm_widget_disabled_get(obj))
156 edje_object_signal_emit(wd->radio, "elm,state,disabled", "elm");
157 if (wd->state) _state_set(obj, 0);
160 edje_object_signal_emit(wd->radio, "elm,state,enabled", "elm");
164 _sizing_eval(Evas_Object *obj)
166 Widget_Data *wd = elm_widget_data_get(obj);
167 Evas_Coord minw = -1, minh = -1;
169 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
170 edje_object_size_min_restricted_calc(wd->radio, &minw, &minh, minw, minh);
171 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
172 evas_object_size_hint_min_set(obj, minw, minh);
173 evas_object_size_hint_max_set(obj, -1, -1);
177 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
179 Widget_Data *wd = elm_widget_data_get(data);
181 if (obj != wd->icon) return;
186 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
188 Widget_Data *wd = elm_widget_data_get(obj);
189 Evas_Object *sub = event_info;
193 edje_object_signal_emit(wd->radio, "elm,state,icon,hidden", "elm");
194 evas_object_event_callback_del_full
195 (sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
202 _state_set(Evas_Object *obj, Eina_Bool state)
204 Widget_Data *wd = elm_widget_data_get(obj);
206 if ((state != wd->state) && (!elm_widget_disabled_get(obj)))
210 edje_object_signal_emit(wd->radio, "elm,state,radio,on", "elm");
212 edje_object_signal_emit(wd->radio, "elm,state,radio,off", "elm");
217 _state_set_all(Widget_Data *wd)
220 Evas_Object *child, *selected = NULL;
221 Eina_Bool disabled = EINA_FALSE;
222 EINA_LIST_FOREACH(wd->group->radios, l, child)
224 Widget_Data *wd2 = elm_widget_data_get(child);
225 if (wd2->state) selected = child;
226 if (wd2->value == wd->group->value)
228 _state_set(child, 1);
229 if (!wd2->state) disabled = EINA_TRUE;
231 else _state_set(child, 0);
233 if ((disabled) && (selected)) _state_set(selected, 1);
237 _activate(Evas_Object *obj)
239 Widget_Data *wd = elm_widget_data_get(obj);
241 if (wd->group->value == wd->value) return;
242 wd->group->value = wd->value;
243 if (wd->group->valuep) *(wd->group->valuep) = wd->group->value;
245 evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
249 _activate_hook(Evas_Object *obj)
255 _signal_radio_on(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
261 * Add a new radio to the parent
263 * @param parent The parent object
264 * @return The new object or NULL if it cannot be created
269 elm_radio_add(Evas_Object *parent)
275 EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
277 wd = ELM_NEW(Widget_Data);
278 e = evas_object_evas_get(parent);
280 obj = elm_widget_add(e);
281 ELM_SET_WIDTYPE(widtype, "radio");
282 elm_widget_type_set(obj, "radio");
283 elm_widget_sub_object_add(parent, obj);
284 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
285 elm_widget_data_set(obj, wd);
286 elm_widget_del_hook_set(obj, _del_hook);
287 elm_widget_theme_hook_set(obj, _theme_hook);
288 elm_widget_disable_hook_set(obj, _disable_hook);
289 elm_widget_can_focus_set(obj, EINA_TRUE);
290 elm_widget_activate_hook_set(obj, _activate_hook);
291 elm_widget_event_hook_set(obj, _event_hook);
293 wd->radio = edje_object_add(e);
294 _elm_theme_object_set(obj, wd->radio, "radio", "base", "default");
295 edje_object_signal_callback_add(wd->radio, "elm,action,radio,on", "", _signal_radio_on, obj);
296 edje_object_signal_callback_add(wd->radio, "elm,action,radio,toggle", "", _signal_radio_on, obj);
297 elm_widget_resize_object_set(obj, wd->radio);
299 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
301 wd->group = calloc(1, sizeof(Group));
302 wd->group->radios = eina_list_append(wd->group->radios, obj);
307 // TODO: convert Elementary to subclassing of Evas_Smart_Class
308 // TODO: and save some bytes, making descriptions per-class and not instance!
309 evas_object_smart_callbacks_descriptions_set(obj, _signals);
314 * Set the text label of the radio object
316 * @param obj The radio object
317 * @param label The text label string in UTF-8
322 elm_radio_label_set(Evas_Object *obj, const char *label)
324 ELM_CHECK_WIDTYPE(obj, widtype);
325 Widget_Data *wd = elm_widget_data_get(obj);
327 eina_stringshare_replace(&wd->label, label);
330 edje_object_signal_emit(wd->radio, "elm,state,text,visible", "elm");
331 edje_object_message_signal_process(wd->radio);
335 edje_object_signal_emit(wd->radio, "elm,state,text,hidden", "elm");
336 edje_object_message_signal_process(wd->radio);
338 edje_object_part_text_set(wd->radio, "elm.text", label);
343 * Get the text label of the radio object
345 * @param obj The radio object
346 * @return The text label string in UTF-8
351 elm_radio_label_get(const Evas_Object *obj)
353 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
354 Widget_Data *wd = elm_widget_data_get(obj);
355 if (!wd) return NULL;
360 * Set the icon object of the radio object
362 * Once the icon object is set, a previously set one will be deleted.
363 * If you want to keep that old content object, use the
364 * elm_radio_icon_unset() function.
366 * @param obj The radio object
367 * @param icon The icon object
372 elm_radio_icon_set(Evas_Object *obj, Evas_Object *icon)
374 ELM_CHECK_WIDTYPE(obj, widtype);
375 Widget_Data *wd = elm_widget_data_get(obj);
377 if (wd->icon == icon) return;
378 if (wd->icon) evas_object_del(wd->icon);
382 elm_widget_sub_object_add(obj, icon);
383 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
384 _changed_size_hints, obj);
385 edje_object_part_swallow(wd->radio, "elm.swallow.content", icon);
386 edje_object_signal_emit(wd->radio, "elm,state,icon,visible", "elm");
387 edje_object_message_signal_process(wd->radio);
393 * Get the icon object of the radio object
395 * @param obj The radio object
396 * @return The icon object
401 elm_radio_icon_get(const Evas_Object *obj)
403 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
404 Widget_Data *wd = elm_widget_data_get(obj);
405 if (!wd) return NULL;
410 * Unset the icon used for the radio object
412 * Unparent and return the icon object which was set for this widget.
414 * @param obj The radio object
415 * @return The icon object that was being used
420 elm_radio_icon_unset(Evas_Object *obj)
422 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
423 Widget_Data *wd = elm_widget_data_get(obj);
424 if (!wd) return NULL;
425 if (!wd->icon) return NULL;
426 Evas_Object *icon = wd->icon;
427 elm_widget_sub_object_del(obj, wd->icon);
428 edje_object_part_unswallow(wd->radio, wd->icon);
434 * Add this radio to a group of other radio objects
436 * Radio objects work in groups. Each member should have a different integer
437 * value assigned. In order ro have them work as a group, they need to know
438 * about eacthother. This adds the given radio object to the group of which
439 * the group object indicated is a member.
441 * @param obj The radio object
442 * @param group The object whose group the object is to join
447 elm_radio_group_add(Evas_Object *obj, Evas_Object *group)
449 ELM_CHECK_WIDTYPE(obj, widtype);
450 Widget_Data *wd = elm_widget_data_get(obj);
451 Widget_Data *wd2 = elm_widget_data_get(group);
455 if (eina_list_count(wd->group->radios) == 1)
457 wd->group->radios = eina_list_remove(wd->group->radios, obj);
458 wd->group = calloc(1, sizeof(Group));
459 wd->group->radios = eina_list_append(wd->group->radios, obj);
461 else if (wd->group == wd2->group) return;
464 wd->group->radios = eina_list_remove(wd->group->radios, obj);
465 if (!wd->group->radios) free(wd->group);
466 wd->group = wd2->group;
467 wd->group->radios = eina_list_append(wd->group->radios, obj);
469 if (wd->value == wd->group->value) _state_set(obj, 1);
470 else _state_set(obj, 0);
474 * Set the integer value that this radio object represents
476 * This sets the value of the radio.
478 * @param obj The radio object
479 * @param value The value to use if this radio object is selected
484 elm_radio_state_value_set(Evas_Object *obj, int value)
486 ELM_CHECK_WIDTYPE(obj, widtype);
487 Widget_Data *wd = elm_widget_data_get(obj);
490 if (wd->value == wd->group->value) _state_set(obj, 1);
491 else _state_set(obj, 0);
495 * Get the integer value that this radio object represents
497 * This gets the value of the radio.
499 * @param obj The radio object
500 * @return The value used if this radio object is selected
505 elm_radio_state_value_get(const Evas_Object *obj)
507 ELM_CHECK_WIDTYPE(obj, widtype) 0;
508 Widget_Data *wd = elm_widget_data_get(obj);
514 * Set the value of the radio.
516 * This sets the value of the radio group and will also set the value if
517 * pointed to, to the value supplied, but will not call any callbacks.
519 * @param obj The radio object
520 * @param value The value to use for the group
525 elm_radio_value_set(Evas_Object *obj, int value)
527 ELM_CHECK_WIDTYPE(obj, widtype);
528 Widget_Data *wd = elm_widget_data_get(obj);
530 if (value == wd->group->value) return;
531 wd->group->value = value;
532 if (wd->group->valuep) *(wd->group->valuep) = wd->group->value;
537 * Get the state of the radio object
539 * @param obj The radio object
540 * @return The integer state
545 elm_radio_value_get(const Evas_Object *obj)
547 ELM_CHECK_WIDTYPE(obj, widtype) 0;
548 Widget_Data *wd = elm_widget_data_get(obj);
550 return wd->group->value;
554 * Set a convenience pointer to a integer to change
556 * This sets a pointer to a integer, that, in addition to the radio objects
557 * state will also be modified directly. To stop setting the object pointed
558 * to simply use NULL as the valuep parameter. If valuep is not NULL, then
559 * when this is called, the radio objects state will also be modified to
560 * reflect the value of the integer valuep points to, just like calling
561 * elm_radio_value_set().
563 * @param obj The radio object
564 * @param valuep Pointer to the integer to modify
569 elm_radio_value_pointer_set(Evas_Object *obj, int *valuep)
571 ELM_CHECK_WIDTYPE(obj, widtype);
572 Widget_Data *wd = elm_widget_data_get(obj);
576 wd->group->valuep = valuep;
577 if (*(wd->group->valuep) != wd->group->value)
579 wd->group->value = *(wd->group->valuep);
585 wd->group->valuep = NULL;