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.
33 * Signals that you can add callbacks for are:
35 * "changed" - when the radio status is changed
39 typedef struct _Widget_Data Widget_Data;
40 typedef struct _Group Group;
59 static const char *widtype = NULL;
60 static void _state_set(Evas_Object *obj, Eina_Bool state);
61 static void _del_hook(Evas_Object *obj);
62 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
63 static void _theme_hook(Evas_Object *obj);
64 static void _disable_hook(Evas_Object *obj);
65 static void _sizing_eval(Evas_Object *obj);
66 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
67 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
68 static void _signal_radio_on(void *data, Evas_Object *obj, const char *emission, const char *source);
69 static void _on_focus_hook(void *data, Evas_Object *obj);
70 static void _activate(Evas_Object *obj);
71 static void _activate_hook(Evas_Object *obj);
72 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
73 Evas_Callback_Type type, void *event_info);
75 static const char SIG_CHANGED[] = "changed";
76 static const Evas_Smart_Cb_Description _signals[] = {
82 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
84 if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
85 Evas_Event_Key_Down *ev = event_info;
86 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
87 if (elm_widget_disabled_get(obj)) return EINA_FALSE;
89 if ((strcmp(ev->keyname, "Return")) &&
90 (strcmp(ev->keyname, "KP_Enter")) &&
91 (strcmp(ev->keyname, "space")))
94 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
99 _del_hook(Evas_Object *obj)
101 Widget_Data *wd = elm_widget_data_get(obj);
103 if (wd->label) eina_stringshare_del(wd->label);
104 wd->group->radios = eina_list_remove(wd->group->radios, obj);
105 if (!wd->group->radios) free(wd->group);
111 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
113 Widget_Data *wd = elm_widget_data_get(obj);
115 if (elm_widget_focus_get(obj))
117 edje_object_signal_emit(wd->radio, "elm,action,focus", "elm");
118 evas_object_focus_set(wd->radio, EINA_TRUE);
122 edje_object_signal_emit(wd->radio, "elm,action,unfocus", "elm");
123 evas_object_focus_set(wd->radio, EINA_FALSE);
128 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
130 Widget_Data *wd = elm_widget_data_get(obj);
132 edje_object_mirrored_set(wd->radio, rtl);
136 _theme_hook(Evas_Object *obj)
138 Widget_Data *wd = elm_widget_data_get(obj);
140 _elm_widget_mirrored_reload(obj);
141 _mirrored_set(obj, elm_widget_mirrored_get(obj));
142 _elm_theme_object_set(obj, wd->radio, "radio", "base", elm_widget_style_get(obj));
144 edje_object_signal_emit(wd->radio, "elm,state,icon,visible", "elm");
146 edje_object_signal_emit(wd->radio, "elm,state,icon,hidden", "elm");
148 edje_object_signal_emit(wd->radio, "elm,state,radio,on", "elm");
150 edje_object_signal_emit(wd->radio, "elm,state,radio,off", "elm");
152 edje_object_signal_emit(wd->radio, "elm,state,text,visible", "elm");
154 edje_object_signal_emit(wd->radio, "elm,state,text,hidden", "elm");
155 edje_object_part_text_set(wd->radio, "elm.text", wd->label);
156 if (elm_widget_disabled_get(obj))
158 edje_object_signal_emit(wd->radio, "elm,state,disabled", "elm");
159 if (wd->state) _state_set(obj, 0);
161 edje_object_message_signal_process(wd->radio);
162 edje_object_scale_set(wd->radio, elm_widget_scale_get(obj) * _elm_config->scale);
167 _disable_hook(Evas_Object *obj)
169 Widget_Data *wd = elm_widget_data_get(obj);
171 if (elm_widget_disabled_get(obj))
173 edje_object_signal_emit(wd->radio, "elm,state,disabled", "elm");
174 if (wd->state) _state_set(obj, 0);
177 edje_object_signal_emit(wd->radio, "elm,state,enabled", "elm");
181 _sizing_eval(Evas_Object *obj)
183 Widget_Data *wd = elm_widget_data_get(obj);
184 Evas_Coord minw = -1, minh = -1;
186 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
187 edje_object_size_min_restricted_calc(wd->radio, &minw, &minh, minw, minh);
188 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
189 evas_object_size_hint_min_set(obj, minw, minh);
190 evas_object_size_hint_max_set(obj, -1, -1);
194 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
196 Widget_Data *wd = elm_widget_data_get(data);
198 if (obj != wd->icon) return;
203 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
205 Widget_Data *wd = elm_widget_data_get(obj);
206 Evas_Object *sub = event_info;
210 edje_object_signal_emit(wd->radio, "elm,state,icon,hidden", "elm");
211 evas_object_event_callback_del_full
212 (sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
219 _state_set(Evas_Object *obj, Eina_Bool state)
221 Widget_Data *wd = elm_widget_data_get(obj);
223 if ((state != wd->state) && (!elm_widget_disabled_get(obj)))
227 edje_object_signal_emit(wd->radio, "elm,state,radio,on", "elm");
229 edje_object_signal_emit(wd->radio, "elm,state,radio,off", "elm");
234 _state_set_all(Widget_Data *wd)
237 Evas_Object *child, *selected = NULL;
238 Eina_Bool disabled = EINA_FALSE;
239 EINA_LIST_FOREACH(wd->group->radios, l, child)
241 Widget_Data *wd2 = elm_widget_data_get(child);
242 if (wd2->state) selected = child;
243 if (wd2->value == wd->group->value)
245 _state_set(child, 1);
246 if (!wd2->state) disabled = EINA_TRUE;
248 else _state_set(child, 0);
250 if ((disabled) && (selected)) _state_set(selected, 1);
254 _activate(Evas_Object *obj)
256 Widget_Data *wd = elm_widget_data_get(obj);
258 if (wd->group->value == wd->value) return;
259 wd->group->value = wd->value;
260 if (wd->group->valuep) *(wd->group->valuep) = wd->group->value;
262 evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
266 _activate_hook(Evas_Object *obj)
272 _signal_radio_on(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
278 _elm_radio_label_set(Evas_Object *obj, const char *item, const char *label)
280 ELM_CHECK_WIDTYPE(obj, widtype);
281 Widget_Data *wd = elm_widget_data_get(obj);
282 if (item && strcmp(item, "default")) return;
284 eina_stringshare_replace(&wd->label, label);
287 edje_object_signal_emit(wd->radio, "elm,state,text,visible", "elm");
288 edje_object_message_signal_process(wd->radio);
292 edje_object_signal_emit(wd->radio, "elm,state,text,hidden", "elm");
293 edje_object_message_signal_process(wd->radio);
295 edje_object_part_text_set(wd->radio, "elm.text", label);
300 _elm_radio_label_get(const Evas_Object *obj, const char *item)
302 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
303 Widget_Data *wd = elm_widget_data_get(obj);
304 if (item && strcmp(item, "default")) return NULL;
305 if (!wd) return NULL;
310 * Add a new radio to the parent
312 * @param parent The parent object
313 * @return The new object or NULL if it cannot be created
318 elm_radio_add(Evas_Object *parent)
324 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
326 ELM_SET_WIDTYPE(widtype, "radio");
327 elm_widget_type_set(obj, "radio");
328 elm_widget_sub_object_add(parent, obj);
329 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
330 elm_widget_data_set(obj, wd);
331 elm_widget_del_hook_set(obj, _del_hook);
332 elm_widget_theme_hook_set(obj, _theme_hook);
333 elm_widget_disable_hook_set(obj, _disable_hook);
334 elm_widget_can_focus_set(obj, EINA_TRUE);
335 elm_widget_activate_hook_set(obj, _activate_hook);
336 elm_widget_event_hook_set(obj, _event_hook);
337 elm_widget_text_set_hook_set(obj, _elm_radio_label_set);
338 elm_widget_text_get_hook_set(obj, _elm_radio_label_get);
340 wd->radio = edje_object_add(e);
341 _elm_theme_object_set(obj, wd->radio, "radio", "base", "default");
342 edje_object_signal_callback_add(wd->radio, "elm,action,radio,on", "", _signal_radio_on, obj);
343 edje_object_signal_callback_add(wd->radio, "elm,action,radio,toggle", "", _signal_radio_on, obj);
344 elm_widget_resize_object_set(obj, wd->radio);
346 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
348 wd->group = calloc(1, sizeof(Group));
349 wd->group->radios = eina_list_append(wd->group->radios, obj);
352 _mirrored_set(obj, elm_widget_mirrored_get(obj));
355 // TODO: convert Elementary to subclassing of Evas_Smart_Class
356 // TODO: and save some bytes, making descriptions per-class and not instance!
357 evas_object_smart_callbacks_descriptions_set(obj, _signals);
362 * Set the text label of the radio object
364 * @param obj The radio object
365 * @param label The text label string in UTF-8
368 * @deprecated use elm_object_text_set() instead.
371 elm_radio_label_set(Evas_Object *obj, const char *label)
373 _elm_radio_label_set(obj, NULL, label);
377 * Get the text label of the radio object
379 * @param obj The radio object
380 * @return The text label string in UTF-8
383 * @deprecated use elm_object_text_set() instead.
386 elm_radio_label_get(const Evas_Object *obj)
388 return _elm_radio_label_get(obj, NULL);
392 * Set the icon object of the radio object
394 * Once the icon object is set, a previously set one will be deleted.
395 * If you want to keep that old content object, use the
396 * elm_radio_icon_unset() function.
398 * @param obj The radio object
399 * @param icon The icon object
404 elm_radio_icon_set(Evas_Object *obj, Evas_Object *icon)
406 ELM_CHECK_WIDTYPE(obj, widtype);
407 Widget_Data *wd = elm_widget_data_get(obj);
409 if (wd->icon == icon) return;
410 if (wd->icon) evas_object_del(wd->icon);
414 elm_widget_sub_object_add(obj, icon);
415 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
416 _changed_size_hints, obj);
417 edje_object_part_swallow(wd->radio, "elm.swallow.content", icon);
418 edje_object_signal_emit(wd->radio, "elm,state,icon,visible", "elm");
419 edje_object_message_signal_process(wd->radio);
425 * Get the icon object of the radio object
427 * @param obj The radio object
428 * @return The icon object
433 elm_radio_icon_get(const Evas_Object *obj)
435 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
436 Widget_Data *wd = elm_widget_data_get(obj);
437 if (!wd) return NULL;
442 * Unset the icon used for the radio object
444 * Unparent and return the icon object which was set for this widget.
446 * @param obj The radio object
447 * @return The icon object that was being used
452 elm_radio_icon_unset(Evas_Object *obj)
454 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
455 Widget_Data *wd = elm_widget_data_get(obj);
456 if (!wd) return NULL;
457 if (!wd->icon) return NULL;
458 Evas_Object *icon = wd->icon;
459 elm_widget_sub_object_del(obj, wd->icon);
460 edje_object_part_unswallow(wd->radio, wd->icon);
466 * Add this radio to a group of other radio objects
468 * Radio objects work in groups. Each member should have a different integer
469 * value assigned. In order ro have them work as a group, they need to know
470 * about eacthother. This adds the given radio object to the group of which
471 * the group object indicated is a member.
473 * @param obj The radio object
474 * @param group The object whose group the object is to join
479 elm_radio_group_add(Evas_Object *obj, Evas_Object *group)
481 ELM_CHECK_WIDTYPE(obj, widtype);
482 Widget_Data *wd = elm_widget_data_get(obj);
483 Widget_Data *wd2 = elm_widget_data_get(group);
487 if (eina_list_count(wd->group->radios) == 1)
489 wd->group->radios = eina_list_remove(wd->group->radios, obj);
490 wd->group = calloc(1, sizeof(Group));
491 wd->group->radios = eina_list_append(wd->group->radios, obj);
493 else if (wd->group == wd2->group) return;
496 wd->group->radios = eina_list_remove(wd->group->radios, obj);
497 if (!wd->group->radios) free(wd->group);
498 wd->group = wd2->group;
499 wd->group->radios = eina_list_append(wd->group->radios, obj);
501 if (wd->value == wd->group->value) _state_set(obj, 1);
502 else _state_set(obj, 0);
506 * Set the integer value that this radio object represents
508 * This sets the value of the radio.
510 * @param obj The radio object
511 * @param value The value to use if this radio object is selected
516 elm_radio_state_value_set(Evas_Object *obj, int value)
518 ELM_CHECK_WIDTYPE(obj, widtype);
519 Widget_Data *wd = elm_widget_data_get(obj);
522 if (wd->value == wd->group->value) _state_set(obj, 1);
523 else _state_set(obj, 0);
527 * Get the integer value that this radio object represents
529 * This gets the value of the radio.
531 * @param obj The radio object
532 * @return The value used if this radio object is selected
537 elm_radio_state_value_get(const Evas_Object *obj)
539 ELM_CHECK_WIDTYPE(obj, widtype) 0;
540 Widget_Data *wd = elm_widget_data_get(obj);
546 * Set the value of the radio.
548 * This sets the value of the radio group and will also set the value if
549 * pointed to, to the value supplied, but will not call any callbacks.
551 * @param obj The radio object
552 * @param value The value to use for the group
557 elm_radio_value_set(Evas_Object *obj, int value)
559 ELM_CHECK_WIDTYPE(obj, widtype);
560 Widget_Data *wd = elm_widget_data_get(obj);
562 if (value == wd->group->value) return;
563 wd->group->value = value;
564 if (wd->group->valuep) *(wd->group->valuep) = wd->group->value;
569 * Get the state of the radio object
571 * @param obj The radio object
572 * @return The integer state
577 elm_radio_value_get(const Evas_Object *obj)
579 ELM_CHECK_WIDTYPE(obj, widtype) 0;
580 Widget_Data *wd = elm_widget_data_get(obj);
582 return wd->group->value;
586 * Set a convenience pointer to a integer to change
588 * This sets a pointer to a integer, that, in addition to the radio objects
589 * state will also be modified directly. To stop setting the object pointed
590 * to simply use NULL as the valuep parameter. If valuep is not NULL, then
591 * when this is called, the radio objects state will also be modified to
592 * reflect the value of the integer valuep points to, just like calling
593 * elm_radio_value_set().
595 * @param obj The radio object
596 * @param valuep Pointer to the integer to modify
601 elm_radio_value_pointer_set(Evas_Object *obj, int *valuep)
603 ELM_CHECK_WIDTYPE(obj, widtype);
604 Widget_Data *wd = elm_widget_data_get(obj);
608 wd->group->valuep = valuep;
609 if (*(wd->group->valuep) != wd->group->value)
611 wd->group->value = *(wd->group->valuep);
617 wd->group->valuep = NULL;