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.
32 * Signals that you can add callbacks for are:
34 * "changed" - when the radio status is changed
38 typedef struct _Widget_Data Widget_Data;
39 typedef struct _Group Group;
58 static const char *widtype = NULL;
59 static void _state_set(Evas_Object *obj, Eina_Bool state);
60 static void _del_hook(Evas_Object *obj);
61 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
62 static void _theme_hook(Evas_Object *obj);
63 static void _disable_hook(Evas_Object *obj);
64 static void _sizing_eval(Evas_Object *obj);
65 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
66 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
67 static void _signal_radio_on(void *data, Evas_Object *obj, const char *emission, const char *source);
68 static void _on_focus_hook(void *data, Evas_Object *obj);
69 static void _activate(Evas_Object *obj);
70 static void _activate_hook(Evas_Object *obj);
71 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
72 Evas_Callback_Type type, void *event_info);
74 static const char SIG_CHANGED[] = "changed";
75 static const Evas_Smart_Cb_Description _signals[] = {
81 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
83 if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
84 Evas_Event_Key_Down *ev = event_info;
85 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
86 if (elm_widget_disabled_get(obj)) return EINA_FALSE;
88 if ((strcmp(ev->keyname, "Return")) &&
89 (strcmp(ev->keyname, "KP_Enter")) &&
90 (strcmp(ev->keyname, "space")))
93 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
98 _del_hook(Evas_Object *obj)
100 Widget_Data *wd = elm_widget_data_get(obj);
102 if (wd->label) eina_stringshare_del(wd->label);
103 wd->group->radios = eina_list_remove(wd->group->radios, obj);
104 if (!wd->group->radios) free(wd->group);
110 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
112 Widget_Data *wd = elm_widget_data_get(obj);
114 if (elm_widget_focus_get(obj))
116 edje_object_signal_emit(wd->radio, "elm,action,focus", "elm");
117 evas_object_focus_set(wd->radio, EINA_TRUE);
121 edje_object_signal_emit(wd->radio, "elm,action,unfocus", "elm");
122 evas_object_focus_set(wd->radio, EINA_FALSE);
127 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
129 Widget_Data *wd = elm_widget_data_get(obj);
131 edje_object_mirrored_set(wd->radio, rtl);
135 _theme_hook(Evas_Object *obj)
137 Widget_Data *wd = elm_widget_data_get(obj);
139 _elm_widget_mirrored_reload(obj);
140 _mirrored_set(obj, elm_widget_mirrored_get(obj));
141 _elm_theme_object_set(obj, wd->radio, "radio", "base", elm_widget_style_get(obj));
143 edje_object_signal_emit(wd->radio, "elm,state,icon,visible", "elm");
145 edje_object_signal_emit(wd->radio, "elm,state,icon,hidden", "elm");
147 edje_object_signal_emit(wd->radio, "elm,state,radio,on", "elm");
149 edje_object_signal_emit(wd->radio, "elm,state,radio,off", "elm");
151 edje_object_signal_emit(wd->radio, "elm,state,text,visible", "elm");
153 edje_object_signal_emit(wd->radio, "elm,state,text,hidden", "elm");
154 edje_object_part_text_set(wd->radio, "elm.text", wd->label);
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);
160 edje_object_message_signal_process(wd->radio);
161 edje_object_scale_set(wd->radio, elm_widget_scale_get(obj) * _elm_config->scale);
166 _disable_hook(Evas_Object *obj)
168 Widget_Data *wd = elm_widget_data_get(obj);
170 if (elm_widget_disabled_get(obj))
172 edje_object_signal_emit(wd->radio, "elm,state,disabled", "elm");
173 if (wd->state) _state_set(obj, 0);
176 edje_object_signal_emit(wd->radio, "elm,state,enabled", "elm");
180 _sizing_eval(Evas_Object *obj)
182 Widget_Data *wd = elm_widget_data_get(obj);
183 Evas_Coord minw = -1, minh = -1;
185 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
186 edje_object_size_min_restricted_calc(wd->radio, &minw, &minh, minw, minh);
187 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
188 evas_object_size_hint_min_set(obj, minw, minh);
189 evas_object_size_hint_max_set(obj, -1, -1);
193 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
195 Widget_Data *wd = elm_widget_data_get(data);
197 if (obj != wd->icon) return;
202 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
204 Widget_Data *wd = elm_widget_data_get(obj);
205 Evas_Object *sub = event_info;
209 edje_object_signal_emit(wd->radio, "elm,state,icon,hidden", "elm");
210 evas_object_event_callback_del_full
211 (sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
218 _state_set(Evas_Object *obj, Eina_Bool state)
220 Widget_Data *wd = elm_widget_data_get(obj);
222 if ((state != wd->state) && (!elm_widget_disabled_get(obj)))
226 edje_object_signal_emit(wd->radio, "elm,state,radio,on", "elm");
228 edje_object_signal_emit(wd->radio, "elm,state,radio,off", "elm");
233 _state_set_all(Widget_Data *wd)
236 Evas_Object *child, *selected = NULL;
237 Eina_Bool disabled = EINA_FALSE;
238 EINA_LIST_FOREACH(wd->group->radios, l, child)
240 Widget_Data *wd2 = elm_widget_data_get(child);
241 if (wd2->state) selected = child;
242 if (wd2->value == wd->group->value)
244 _state_set(child, 1);
245 if (!wd2->state) disabled = EINA_TRUE;
247 else _state_set(child, 0);
249 if ((disabled) && (selected)) _state_set(selected, 1);
253 _activate(Evas_Object *obj)
255 Widget_Data *wd = elm_widget_data_get(obj);
257 if (wd->group->value == wd->value) return;
258 wd->group->value = wd->value;
259 if (wd->group->valuep) *(wd->group->valuep) = wd->group->value;
261 evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
265 _activate_hook(Evas_Object *obj)
271 _signal_radio_on(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
277 _elm_radio_label_set(Evas_Object *obj, const char *item, const char *label)
279 ELM_CHECK_WIDTYPE(obj, widtype);
280 Widget_Data *wd = elm_widget_data_get(obj);
281 if (item && strcmp(item, "default")) return;
283 eina_stringshare_replace(&wd->label, label);
286 edje_object_signal_emit(wd->radio, "elm,state,text,visible", "elm");
287 edje_object_message_signal_process(wd->radio);
291 edje_object_signal_emit(wd->radio, "elm,state,text,hidden", "elm");
292 edje_object_message_signal_process(wd->radio);
294 edje_object_part_text_set(wd->radio, "elm.text", label);
299 _elm_radio_label_get(const Evas_Object *obj, const char *item)
301 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
302 Widget_Data *wd = elm_widget_data_get(obj);
303 if (item && strcmp(item, "default")) return NULL;
304 if (!wd) return NULL;
309 * Add a new radio to the parent
311 * @param parent The parent object
312 * @return The new object or NULL if it cannot be created
317 elm_radio_add(Evas_Object *parent)
323 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
325 ELM_SET_WIDTYPE(widtype, "radio");
326 elm_widget_type_set(obj, "radio");
327 elm_widget_sub_object_add(parent, obj);
328 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
329 elm_widget_data_set(obj, wd);
330 elm_widget_del_hook_set(obj, _del_hook);
331 elm_widget_theme_hook_set(obj, _theme_hook);
332 elm_widget_disable_hook_set(obj, _disable_hook);
333 elm_widget_can_focus_set(obj, EINA_TRUE);
334 elm_widget_activate_hook_set(obj, _activate_hook);
335 elm_widget_event_hook_set(obj, _event_hook);
336 elm_widget_text_set_hook_set(obj, _elm_radio_label_set);
337 elm_widget_text_get_hook_set(obj, _elm_radio_label_get);
339 wd->radio = edje_object_add(e);
340 _elm_theme_object_set(obj, wd->radio, "radio", "base", "default");
341 edje_object_signal_callback_add(wd->radio, "elm,action,radio,on", "", _signal_radio_on, obj);
342 edje_object_signal_callback_add(wd->radio, "elm,action,radio,toggle", "", _signal_radio_on, obj);
343 elm_widget_resize_object_set(obj, wd->radio);
345 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
347 wd->group = calloc(1, sizeof(Group));
348 wd->group->radios = eina_list_append(wd->group->radios, obj);
351 _mirrored_set(obj, elm_widget_mirrored_get(obj));
354 // TODO: convert Elementary to subclassing of Evas_Smart_Class
355 // TODO: and save some bytes, making descriptions per-class and not instance!
356 evas_object_smart_callbacks_descriptions_set(obj, _signals);
361 * Set the text label of the radio object
363 * @param obj The radio object
364 * @param label The text label string in UTF-8
367 * @deprecated use elm_object_text_set() instead.
370 elm_radio_label_set(Evas_Object *obj, const char *label)
372 _elm_radio_label_set(obj, NULL, label);
376 * Get the text label of the radio object
378 * @param obj The radio object
379 * @return The text label string in UTF-8
382 * @deprecated use elm_object_text_set() instead.
385 elm_radio_label_get(const Evas_Object *obj)
387 return _elm_radio_label_get(obj, NULL);
391 * Set the icon object of the radio object
393 * Once the icon object is set, a previously set one will be deleted.
394 * If you want to keep that old content object, use the
395 * elm_radio_icon_unset() function.
397 * @param obj The radio object
398 * @param icon The icon object
403 elm_radio_icon_set(Evas_Object *obj, Evas_Object *icon)
405 ELM_CHECK_WIDTYPE(obj, widtype);
406 Widget_Data *wd = elm_widget_data_get(obj);
408 if (wd->icon == icon) return;
409 if (wd->icon) evas_object_del(wd->icon);
413 elm_widget_sub_object_add(obj, icon);
414 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
415 _changed_size_hints, obj);
416 edje_object_part_swallow(wd->radio, "elm.swallow.content", icon);
417 edje_object_signal_emit(wd->radio, "elm,state,icon,visible", "elm");
418 edje_object_message_signal_process(wd->radio);
424 * Get the icon object of the radio object
426 * @param obj The radio object
427 * @return The icon object
432 elm_radio_icon_get(const Evas_Object *obj)
434 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
435 Widget_Data *wd = elm_widget_data_get(obj);
436 if (!wd) return NULL;
441 * Unset the icon used for the radio object
443 * Unparent and return the icon object which was set for this widget.
445 * @param obj The radio object
446 * @return The icon object that was being used
451 elm_radio_icon_unset(Evas_Object *obj)
453 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
454 Widget_Data *wd = elm_widget_data_get(obj);
455 if (!wd) return NULL;
456 if (!wd->icon) return NULL;
457 Evas_Object *icon = wd->icon;
458 elm_widget_sub_object_del(obj, wd->icon);
459 edje_object_part_unswallow(wd->radio, wd->icon);
465 * Add this radio to a group of other radio objects
467 * Radio objects work in groups. Each member should have a different integer
468 * value assigned. In order ro have them work as a group, they need to know
469 * about eacthother. This adds the given radio object to the group of which
470 * the group object indicated is a member.
472 * @param obj The radio object
473 * @param group The object whose group the object is to join
478 elm_radio_group_add(Evas_Object *obj, Evas_Object *group)
480 ELM_CHECK_WIDTYPE(obj, widtype);
481 Widget_Data *wd = elm_widget_data_get(obj);
482 Widget_Data *wd2 = elm_widget_data_get(group);
486 if (eina_list_count(wd->group->radios) == 1)
488 wd->group->radios = eina_list_remove(wd->group->radios, obj);
489 wd->group = calloc(1, sizeof(Group));
490 wd->group->radios = eina_list_append(wd->group->radios, obj);
492 else if (wd->group == wd2->group) return;
495 wd->group->radios = eina_list_remove(wd->group->radios, obj);
496 if (!wd->group->radios) free(wd->group);
497 wd->group = wd2->group;
498 wd->group->radios = eina_list_append(wd->group->radios, obj);
500 if (wd->value == wd->group->value) _state_set(obj, 1);
501 else _state_set(obj, 0);
505 * Set the integer value that this radio object represents
507 * This sets the value of the radio.
509 * @param obj The radio object
510 * @param value The value to use if this radio object is selected
515 elm_radio_state_value_set(Evas_Object *obj, int value)
517 ELM_CHECK_WIDTYPE(obj, widtype);
518 Widget_Data *wd = elm_widget_data_get(obj);
521 if (wd->value == wd->group->value) _state_set(obj, 1);
522 else _state_set(obj, 0);
526 * Get the integer value that this radio object represents
528 * This gets the value of the radio.
530 * @param obj The radio object
531 * @return The value used if this radio object is selected
536 elm_radio_state_value_get(const Evas_Object *obj)
538 ELM_CHECK_WIDTYPE(obj, widtype) 0;
539 Widget_Data *wd = elm_widget_data_get(obj);
545 * Set the value of the radio.
547 * This sets the value of the radio group and will also set the value if
548 * pointed to, to the value supplied, but will not call any callbacks.
550 * @param obj The radio object
551 * @param value The value to use for the group
556 elm_radio_value_set(Evas_Object *obj, int value)
558 ELM_CHECK_WIDTYPE(obj, widtype);
559 Widget_Data *wd = elm_widget_data_get(obj);
561 if (value == wd->group->value) return;
562 wd->group->value = value;
563 if (wd->group->valuep) *(wd->group->valuep) = wd->group->value;
568 * Get the state of the radio object
570 * @param obj The radio object
571 * @return The integer state
576 elm_radio_value_get(const Evas_Object *obj)
578 ELM_CHECK_WIDTYPE(obj, widtype) 0;
579 Widget_Data *wd = elm_widget_data_get(obj);
581 return wd->group->value;
585 * Set a convenience pointer to a integer to change
587 * This sets a pointer to a integer, that, in addition to the radio objects
588 * state will also be modified directly. To stop setting the object pointed
589 * to simply use NULL as the valuep parameter. If valuep is not NULL, then
590 * when this is called, the radio objects state will also be modified to
591 * reflect the value of the integer valuep points to, just like calling
592 * elm_radio_value_set().
594 * @param obj The radio object
595 * @param valuep Pointer to the integer to modify
600 elm_radio_value_pointer_set(Evas_Object *obj, int *valuep)
602 ELM_CHECK_WIDTYPE(obj, widtype);
603 Widget_Data *wd = elm_widget_data_get(obj);
607 wd->group->valuep = valuep;
608 if (*(wd->group->valuep) != wd->group->value)
610 wd->group->value = *(wd->group->valuep);
616 wd->group->valuep = NULL;