1 #include <Elementary.h>
4 typedef struct _Widget_Data Widget_Data;
5 typedef struct _Group Group;
24 static const char *widtype = NULL;
25 static void _state_set(Evas_Object *obj, Eina_Bool state);
26 static void _del_hook(Evas_Object *obj);
27 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
28 static void _theme_hook(Evas_Object *obj);
29 static void _disable_hook(Evas_Object *obj);
30 static void _sizing_eval(Evas_Object *obj);
31 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
32 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
33 static void _signal_radio_on(void *data, Evas_Object *obj, const char *emission, const char *source);
34 static void _on_focus_hook(void *data, Evas_Object *obj);
35 static void _activate(Evas_Object *obj);
36 static void _activate_hook(Evas_Object *obj);
37 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
38 Evas_Callback_Type type, void *event_info);
40 static const char SIG_CHANGED[] = "changed";
41 static const Evas_Smart_Cb_Description _signals[] = {
47 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
49 if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
50 Evas_Event_Key_Down *ev = event_info;
51 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
52 if (elm_widget_disabled_get(obj)) return EINA_FALSE;
54 if ((strcmp(ev->keyname, "Return")) &&
55 (strcmp(ev->keyname, "KP_Enter")) &&
56 (strcmp(ev->keyname, "space")))
59 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
64 _del_hook(Evas_Object *obj)
66 Widget_Data *wd = elm_widget_data_get(obj);
68 if (wd->label) eina_stringshare_del(wd->label);
69 wd->group->radios = eina_list_remove(wd->group->radios, obj);
70 if (!wd->group->radios) free(wd->group);
76 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
78 Widget_Data *wd = elm_widget_data_get(obj);
80 if (elm_widget_focus_get(obj))
82 edje_object_signal_emit(wd->radio, "elm,action,focus", "elm");
83 evas_object_focus_set(wd->radio, EINA_TRUE);
87 edje_object_signal_emit(wd->radio, "elm,action,unfocus", "elm");
88 evas_object_focus_set(wd->radio, EINA_FALSE);
93 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
95 Widget_Data *wd = elm_widget_data_get(obj);
97 edje_object_mirrored_set(wd->radio, rtl);
101 _theme_hook(Evas_Object *obj)
103 Widget_Data *wd = elm_widget_data_get(obj);
105 _elm_widget_mirrored_reload(obj);
106 _mirrored_set(obj, elm_widget_mirrored_get(obj));
107 _elm_theme_object_set(obj, wd->radio, "radio", "base", elm_widget_style_get(obj));
109 edje_object_signal_emit(wd->radio, "elm,state,icon,visible", "elm");
111 edje_object_signal_emit(wd->radio, "elm,state,icon,hidden", "elm");
113 edje_object_signal_emit(wd->radio, "elm,state,radio,on", "elm");
115 edje_object_signal_emit(wd->radio, "elm,state,radio,off", "elm");
117 edje_object_signal_emit(wd->radio, "elm,state,text,visible", "elm");
119 edje_object_signal_emit(wd->radio, "elm,state,text,hidden", "elm");
120 edje_object_part_text_set(wd->radio, "elm.text", wd->label);
121 if (elm_widget_disabled_get(obj))
123 edje_object_signal_emit(wd->radio, "elm,state,disabled", "elm");
124 if (wd->state) _state_set(obj, 0);
126 edje_object_message_signal_process(wd->radio);
127 edje_object_scale_set(wd->radio, elm_widget_scale_get(obj) * _elm_config->scale);
132 _disable_hook(Evas_Object *obj)
134 Widget_Data *wd = elm_widget_data_get(obj);
136 if (elm_widget_disabled_get(obj))
138 edje_object_signal_emit(wd->radio, "elm,state,disabled", "elm");
139 if (wd->state) _state_set(obj, 0);
142 edje_object_signal_emit(wd->radio, "elm,state,enabled", "elm");
146 _sizing_eval(Evas_Object *obj)
148 Widget_Data *wd = elm_widget_data_get(obj);
149 Evas_Coord minw = -1, minh = -1;
151 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
152 edje_object_size_min_restricted_calc(wd->radio, &minw, &minh, minw, minh);
153 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
154 evas_object_size_hint_min_set(obj, minw, minh);
155 evas_object_size_hint_max_set(obj, -1, -1);
159 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
161 Widget_Data *wd = elm_widget_data_get(data);
163 if (obj != wd->icon) return;
168 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
170 Widget_Data *wd = elm_widget_data_get(obj);
171 Evas_Object *sub = event_info;
175 edje_object_signal_emit(wd->radio, "elm,state,icon,hidden", "elm");
176 evas_object_event_callback_del_full
177 (sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
184 _state_set(Evas_Object *obj, Eina_Bool state)
186 Widget_Data *wd = elm_widget_data_get(obj);
188 if ((state != wd->state) && (!elm_widget_disabled_get(obj)))
192 edje_object_signal_emit(wd->radio, "elm,state,radio,on", "elm");
194 edje_object_signal_emit(wd->radio, "elm,state,radio,off", "elm");
199 _state_set_all(Widget_Data *wd)
202 Evas_Object *child, *selected = NULL;
203 Eina_Bool disabled = EINA_FALSE;
204 EINA_LIST_FOREACH(wd->group->radios, l, child)
206 Widget_Data *wd2 = elm_widget_data_get(child);
207 if (wd2->state) selected = child;
208 if (wd2->value == wd->group->value)
210 _state_set(child, 1);
211 if (!wd2->state) disabled = EINA_TRUE;
213 else _state_set(child, 0);
215 if ((disabled) && (selected)) _state_set(selected, 1);
219 _activate(Evas_Object *obj)
221 Widget_Data *wd = elm_widget_data_get(obj);
223 if (wd->group->value == wd->value) return;
224 wd->group->value = wd->value;
225 if (wd->group->valuep) *(wd->group->valuep) = wd->group->value;
227 evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
231 _activate_hook(Evas_Object *obj)
237 _signal_radio_on(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
243 _elm_radio_label_set(Evas_Object *obj, const char *item, const char *label)
245 ELM_CHECK_WIDTYPE(obj, widtype);
246 Widget_Data *wd = elm_widget_data_get(obj);
247 if (item && strcmp(item, "default")) return;
249 eina_stringshare_replace(&wd->label, label);
252 edje_object_signal_emit(wd->radio, "elm,state,text,visible", "elm");
253 edje_object_message_signal_process(wd->radio);
257 edje_object_signal_emit(wd->radio, "elm,state,text,hidden", "elm");
258 edje_object_message_signal_process(wd->radio);
260 edje_object_part_text_set(wd->radio, "elm.text", label);
265 _elm_radio_label_get(const Evas_Object *obj, const char *item)
267 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
268 Widget_Data *wd = elm_widget_data_get(obj);
269 if (item && strcmp(item, "default")) return NULL;
270 if (!wd) return NULL;
275 elm_radio_add(Evas_Object *parent)
281 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
283 ELM_SET_WIDTYPE(widtype, "radio");
284 elm_widget_type_set(obj, "radio");
285 elm_widget_sub_object_add(parent, obj);
286 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
287 elm_widget_data_set(obj, wd);
288 elm_widget_del_hook_set(obj, _del_hook);
289 elm_widget_theme_hook_set(obj, _theme_hook);
290 elm_widget_disable_hook_set(obj, _disable_hook);
291 elm_widget_can_focus_set(obj, EINA_TRUE);
292 elm_widget_activate_hook_set(obj, _activate_hook);
293 elm_widget_event_hook_set(obj, _event_hook);
294 elm_widget_text_set_hook_set(obj, _elm_radio_label_set);
295 elm_widget_text_get_hook_set(obj, _elm_radio_label_get);
297 wd->radio = edje_object_add(e);
298 _elm_theme_object_set(obj, wd->radio, "radio", "base", "default");
299 edje_object_signal_callback_add(wd->radio, "elm,action,radio,on", "", _signal_radio_on, obj);
300 edje_object_signal_callback_add(wd->radio, "elm,action,radio,toggle", "", _signal_radio_on, obj);
301 elm_widget_resize_object_set(obj, wd->radio);
303 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
305 wd->group = calloc(1, sizeof(Group));
306 wd->group->radios = eina_list_append(wd->group->radios, obj);
309 _mirrored_set(obj, elm_widget_mirrored_get(obj));
312 // TODO: convert Elementary to subclassing of Evas_Smart_Class
313 // TODO: and save some bytes, making descriptions per-class and not instance!
314 evas_object_smart_callbacks_descriptions_set(obj, _signals);
319 elm_radio_label_set(Evas_Object *obj, const char *label)
321 _elm_radio_label_set(obj, NULL, label);
325 elm_radio_label_get(const Evas_Object *obj)
327 return _elm_radio_label_get(obj, NULL);
331 elm_radio_icon_set(Evas_Object *obj, Evas_Object *icon)
333 ELM_CHECK_WIDTYPE(obj, widtype);
334 Widget_Data *wd = elm_widget_data_get(obj);
336 if (wd->icon == icon) return;
337 if (wd->icon) evas_object_del(wd->icon);
341 elm_widget_sub_object_add(obj, icon);
342 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
343 _changed_size_hints, obj);
344 edje_object_part_swallow(wd->radio, "elm.swallow.content", icon);
345 edje_object_signal_emit(wd->radio, "elm,state,icon,visible", "elm");
346 edje_object_message_signal_process(wd->radio);
352 elm_radio_icon_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 elm_radio_icon_unset(Evas_Object *obj)
363 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
364 Widget_Data *wd = elm_widget_data_get(obj);
365 if (!wd) return NULL;
366 if (!wd->icon) return NULL;
367 Evas_Object *icon = wd->icon;
368 elm_widget_sub_object_del(obj, wd->icon);
369 edje_object_part_unswallow(wd->radio, wd->icon);
375 elm_radio_group_add(Evas_Object *obj, Evas_Object *group)
377 ELM_CHECK_WIDTYPE(obj, widtype);
378 Widget_Data *wd = elm_widget_data_get(obj);
379 Widget_Data *wd2 = elm_widget_data_get(group);
383 if (eina_list_count(wd->group->radios) == 1)
385 wd->group->radios = eina_list_remove(wd->group->radios, obj);
386 wd->group = calloc(1, sizeof(Group));
387 wd->group->radios = eina_list_append(wd->group->radios, obj);
389 else if (wd->group == wd2->group) return;
392 wd->group->radios = eina_list_remove(wd->group->radios, obj);
393 if (!wd->group->radios) free(wd->group);
394 wd->group = wd2->group;
395 wd->group->radios = eina_list_append(wd->group->radios, obj);
397 if (wd->value == wd->group->value) _state_set(obj, 1);
398 else _state_set(obj, 0);
402 elm_radio_state_value_set(Evas_Object *obj, int value)
404 ELM_CHECK_WIDTYPE(obj, widtype);
405 Widget_Data *wd = elm_widget_data_get(obj);
408 if (wd->value == wd->group->value) _state_set(obj, 1);
409 else _state_set(obj, 0);
413 elm_radio_state_value_get(const Evas_Object *obj)
415 ELM_CHECK_WIDTYPE(obj, widtype) 0;
416 Widget_Data *wd = elm_widget_data_get(obj);
422 elm_radio_value_set(Evas_Object *obj, int value)
424 ELM_CHECK_WIDTYPE(obj, widtype);
425 Widget_Data *wd = elm_widget_data_get(obj);
427 if (value == wd->group->value) return;
428 wd->group->value = value;
429 if (wd->group->valuep) *(wd->group->valuep) = wd->group->value;
434 elm_radio_value_get(const Evas_Object *obj)
436 ELM_CHECK_WIDTYPE(obj, widtype) 0;
437 Widget_Data *wd = elm_widget_data_get(obj);
439 return wd->group->value;
443 elm_radio_value_pointer_set(Evas_Object *obj, int *valuep)
445 ELM_CHECK_WIDTYPE(obj, widtype);
446 Widget_Data *wd = elm_widget_data_get(obj);
450 wd->group->valuep = valuep;
451 if (*(wd->group->valuep) != wd->group->value)
453 wd->group->value = *(wd->group->valuep);
459 wd->group->valuep = NULL;