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 _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
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 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
124 Widget_Data *wd = elm_widget_data_get(obj);
126 edje_object_mirrored_set(wd->radio, rtl);
130 _theme_hook(Evas_Object *obj)
132 Widget_Data *wd = elm_widget_data_get(obj);
134 _elm_widget_mirrored_reload(obj);
135 _mirrored_set(obj, elm_widget_mirrored_get(obj));
136 _elm_theme_object_set(obj, wd->radio, "radio", "base", elm_widget_style_get(obj));
138 edje_object_signal_emit(wd->radio, "elm,state,icon,visible", "elm");
140 edje_object_signal_emit(wd->radio, "elm,state,icon,hidden", "elm");
142 edje_object_signal_emit(wd->radio, "elm,state,radio,on", "elm");
144 edje_object_signal_emit(wd->radio, "elm,state,radio,off", "elm");
146 edje_object_signal_emit(wd->radio, "elm,state,text,visible", "elm");
148 edje_object_signal_emit(wd->radio, "elm,state,text,hidden", "elm");
149 edje_object_part_text_set(wd->radio, "elm.text", wd->label);
150 if (elm_widget_disabled_get(obj))
152 edje_object_signal_emit(wd->radio, "elm,state,disabled", "elm");
153 if (wd->state) _state_set(obj, 0);
155 edje_object_message_signal_process(wd->radio);
156 edje_object_scale_set(wd->radio, elm_widget_scale_get(obj) * _elm_config->scale);
161 _disable_hook(Evas_Object *obj)
163 Widget_Data *wd = elm_widget_data_get(obj);
165 if (elm_widget_disabled_get(obj))
167 edje_object_signal_emit(wd->radio, "elm,state,disabled", "elm");
168 if (wd->state) _state_set(obj, 0);
171 edje_object_signal_emit(wd->radio, "elm,state,enabled", "elm");
175 _sizing_eval(Evas_Object *obj)
177 Widget_Data *wd = elm_widget_data_get(obj);
178 Evas_Coord minw = -1, minh = -1;
180 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
181 edje_object_size_min_restricted_calc(wd->radio, &minw, &minh, minw, minh);
182 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
183 evas_object_size_hint_min_set(obj, minw, minh);
184 evas_object_size_hint_max_set(obj, -1, -1);
188 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
190 Widget_Data *wd = elm_widget_data_get(data);
192 if (obj != wd->icon) return;
197 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
199 Widget_Data *wd = elm_widget_data_get(obj);
200 Evas_Object *sub = event_info;
204 edje_object_signal_emit(wd->radio, "elm,state,icon,hidden", "elm");
205 evas_object_event_callback_del_full
206 (sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
213 _state_set(Evas_Object *obj, Eina_Bool state)
215 Widget_Data *wd = elm_widget_data_get(obj);
217 if ((state != wd->state) && (!elm_widget_disabled_get(obj)))
221 edje_object_signal_emit(wd->radio, "elm,state,radio,on", "elm");
223 edje_object_signal_emit(wd->radio, "elm,state,radio,off", "elm");
228 _state_set_all(Widget_Data *wd)
231 Evas_Object *child, *selected = NULL;
232 Eina_Bool disabled = EINA_FALSE;
233 EINA_LIST_FOREACH(wd->group->radios, l, child)
235 Widget_Data *wd2 = elm_widget_data_get(child);
236 if (wd2->state) selected = child;
237 if (wd2->value == wd->group->value)
239 _state_set(child, 1);
240 if (!wd2->state) disabled = EINA_TRUE;
242 else _state_set(child, 0);
244 if ((disabled) && (selected)) _state_set(selected, 1);
248 _activate(Evas_Object *obj)
250 Widget_Data *wd = elm_widget_data_get(obj);
252 if (wd->group->value == wd->value) return;
253 wd->group->value = wd->value;
254 if (wd->group->valuep) *(wd->group->valuep) = wd->group->value;
256 evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
260 _activate_hook(Evas_Object *obj)
266 _signal_radio_on(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
272 * Add a new radio to the parent
274 * @param parent The parent object
275 * @return The new object or NULL if it cannot be created
280 elm_radio_add(Evas_Object *parent)
286 EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
288 wd = ELM_NEW(Widget_Data);
289 e = evas_object_evas_get(parent);
291 obj = elm_widget_add(e);
292 ELM_SET_WIDTYPE(widtype, "radio");
293 elm_widget_type_set(obj, "radio");
294 elm_widget_sub_object_add(parent, obj);
295 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
296 elm_widget_data_set(obj, wd);
297 elm_widget_del_hook_set(obj, _del_hook);
298 elm_widget_theme_hook_set(obj, _theme_hook);
299 elm_widget_disable_hook_set(obj, _disable_hook);
300 elm_widget_can_focus_set(obj, EINA_TRUE);
301 elm_widget_activate_hook_set(obj, _activate_hook);
302 elm_widget_event_hook_set(obj, _event_hook);
304 wd->radio = edje_object_add(e);
305 _elm_theme_object_set(obj, wd->radio, "radio", "base", "default");
306 edje_object_signal_callback_add(wd->radio, "elm,action,radio,on", "", _signal_radio_on, obj);
307 edje_object_signal_callback_add(wd->radio, "elm,action,radio,toggle", "", _signal_radio_on, obj);
308 elm_widget_resize_object_set(obj, wd->radio);
310 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
312 wd->group = calloc(1, sizeof(Group));
313 wd->group->radios = eina_list_append(wd->group->radios, obj);
316 _mirrored_set(obj, elm_widget_mirrored_get(obj));
319 // TODO: convert Elementary to subclassing of Evas_Smart_Class
320 // TODO: and save some bytes, making descriptions per-class and not instance!
321 evas_object_smart_callbacks_descriptions_set(obj, _signals);
326 * Set the text label of the radio object
328 * @param obj The radio object
329 * @param label The text label string in UTF-8
334 elm_radio_label_set(Evas_Object *obj, const char *label)
336 ELM_CHECK_WIDTYPE(obj, widtype);
337 Widget_Data *wd = elm_widget_data_get(obj);
339 eina_stringshare_replace(&wd->label, label);
342 edje_object_signal_emit(wd->radio, "elm,state,text,visible", "elm");
343 edje_object_message_signal_process(wd->radio);
347 edje_object_signal_emit(wd->radio, "elm,state,text,hidden", "elm");
348 edje_object_message_signal_process(wd->radio);
350 edje_object_part_text_set(wd->radio, "elm.text", label);
355 * Get the text label of the radio object
357 * @param obj The radio object
358 * @return The text label string in UTF-8
363 elm_radio_label_get(const Evas_Object *obj)
365 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
366 Widget_Data *wd = elm_widget_data_get(obj);
367 if (!wd) return NULL;
372 * Set the icon object of the radio object
374 * Once the icon object is set, a previously set one will be deleted.
375 * If you want to keep that old content object, use the
376 * elm_radio_icon_unset() function.
378 * @param obj The radio object
379 * @param icon The icon object
384 elm_radio_icon_set(Evas_Object *obj, Evas_Object *icon)
386 ELM_CHECK_WIDTYPE(obj, widtype);
387 Widget_Data *wd = elm_widget_data_get(obj);
389 if (wd->icon == icon) return;
390 if (wd->icon) evas_object_del(wd->icon);
394 elm_widget_sub_object_add(obj, icon);
395 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
396 _changed_size_hints, obj);
397 edje_object_part_swallow(wd->radio, "elm.swallow.content", icon);
398 edje_object_signal_emit(wd->radio, "elm,state,icon,visible", "elm");
399 edje_object_message_signal_process(wd->radio);
405 * Get the icon object of the radio object
407 * @param obj The radio object
408 * @return The icon object
413 elm_radio_icon_get(const Evas_Object *obj)
415 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
416 Widget_Data *wd = elm_widget_data_get(obj);
417 if (!wd) return NULL;
422 * Unset the icon used for the radio object
424 * Unparent and return the icon object which was set for this widget.
426 * @param obj The radio object
427 * @return The icon object that was being used
432 elm_radio_icon_unset(Evas_Object *obj)
434 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
435 Widget_Data *wd = elm_widget_data_get(obj);
436 if (!wd) return NULL;
437 if (!wd->icon) return NULL;
438 Evas_Object *icon = wd->icon;
439 elm_widget_sub_object_del(obj, wd->icon);
440 edje_object_part_unswallow(wd->radio, wd->icon);
446 * Add this radio to a group of other radio objects
448 * Radio objects work in groups. Each member should have a different integer
449 * value assigned. In order ro have them work as a group, they need to know
450 * about eacthother. This adds the given radio object to the group of which
451 * the group object indicated is a member.
453 * @param obj The radio object
454 * @param group The object whose group the object is to join
459 elm_radio_group_add(Evas_Object *obj, Evas_Object *group)
461 ELM_CHECK_WIDTYPE(obj, widtype);
462 Widget_Data *wd = elm_widget_data_get(obj);
463 Widget_Data *wd2 = elm_widget_data_get(group);
467 if (eina_list_count(wd->group->radios) == 1)
469 wd->group->radios = eina_list_remove(wd->group->radios, obj);
470 wd->group = calloc(1, sizeof(Group));
471 wd->group->radios = eina_list_append(wd->group->radios, obj);
473 else if (wd->group == wd2->group) return;
476 wd->group->radios = eina_list_remove(wd->group->radios, obj);
477 if (!wd->group->radios) free(wd->group);
478 wd->group = wd2->group;
479 wd->group->radios = eina_list_append(wd->group->radios, obj);
481 if (wd->value == wd->group->value) _state_set(obj, 1);
482 else _state_set(obj, 0);
486 * Set the integer value that this radio object represents
488 * This sets the value of the radio.
490 * @param obj The radio object
491 * @param value The value to use if this radio object is selected
496 elm_radio_state_value_set(Evas_Object *obj, int value)
498 ELM_CHECK_WIDTYPE(obj, widtype);
499 Widget_Data *wd = elm_widget_data_get(obj);
502 if (wd->value == wd->group->value) _state_set(obj, 1);
503 else _state_set(obj, 0);
507 * Get the integer value that this radio object represents
509 * This gets the value of the radio.
511 * @param obj The radio object
512 * @return The value used if this radio object is selected
517 elm_radio_state_value_get(const Evas_Object *obj)
519 ELM_CHECK_WIDTYPE(obj, widtype) 0;
520 Widget_Data *wd = elm_widget_data_get(obj);
526 * Set the value of the radio.
528 * This sets the value of the radio group and will also set the value if
529 * pointed to, to the value supplied, but will not call any callbacks.
531 * @param obj The radio object
532 * @param value The value to use for the group
537 elm_radio_value_set(Evas_Object *obj, int value)
539 ELM_CHECK_WIDTYPE(obj, widtype);
540 Widget_Data *wd = elm_widget_data_get(obj);
542 if (value == wd->group->value) return;
543 wd->group->value = value;
544 if (wd->group->valuep) *(wd->group->valuep) = wd->group->value;
549 * Get the state of the radio object
551 * @param obj The radio object
552 * @return The integer state
557 elm_radio_value_get(const Evas_Object *obj)
559 ELM_CHECK_WIDTYPE(obj, widtype) 0;
560 Widget_Data *wd = elm_widget_data_get(obj);
562 return wd->group->value;
566 * Set a convenience pointer to a integer to change
568 * This sets a pointer to a integer, that, in addition to the radio objects
569 * state will also be modified directly. To stop setting the object pointed
570 * to simply use NULL as the valuep parameter. If valuep is not NULL, then
571 * when this is called, the radio objects state will also be modified to
572 * reflect the value of the integer valuep points to, just like calling
573 * elm_radio_value_set().
575 * @param obj The radio object
576 * @param valuep Pointer to the integer to modify
581 elm_radio_value_pointer_set(Evas_Object *obj, int *valuep)
583 ELM_CHECK_WIDTYPE(obj, widtype);
584 Widget_Data *wd = elm_widget_data_get(obj);
588 wd->group->valuep = valuep;
589 if (*(wd->group->valuep) != wd->group->value)
591 wd->group->value = *(wd->group->valuep);
597 wd->group->valuep = NULL;