1 #include <Elementary.h>
4 typedef struct _Widget_Data Widget_Data;
8 Evas_Object *btn, *icon;
17 static const char *widtype = NULL;
18 static void _del_hook(Evas_Object *obj);
19 static void _theme_hook(Evas_Object *obj);
20 static void _disable_hook(Evas_Object *obj);
21 static void _sizing_eval(Evas_Object *obj);
22 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
23 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
24 static void _signal_clicked(void *data, Evas_Object *obj, const char *emission, const char *source);
25 static void _signal_pressed(void *data, Evas_Object *obj, const char *emission, const char *source);
26 static void _signal_unpressed(void *data, Evas_Object *obj, const char *emission, const char *source);
27 static void _on_focus_hook(void *data, Evas_Object *obj);
28 static void _activate(Evas_Object *obj);
29 static void _activate_hook(Evas_Object *obj);
30 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
31 Evas_Callback_Type type, void *event_info);
33 static const char SIG_CLICKED[] = "clicked";
34 static const char SIG_REPEATED[] = "repeated";
35 static const char SIG_PRESSED[] = "pressed";
36 static const char SIG_UNPRESSED[] = "unpressed";
37 static const Evas_Smart_Cb_Description _signals[] = {
46 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
48 if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
49 Evas_Event_Key_Down *ev = event_info;
50 Widget_Data *wd = elm_widget_data_get(obj);
51 if (!wd) return EINA_FALSE;
52 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
53 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;
60 edje_object_signal_emit(wd->btn, "elm,anim,activate", "elm");
65 _del_hook(Evas_Object *obj)
67 Widget_Data *wd = elm_widget_data_get(obj);
69 if (wd->label) eina_stringshare_del(wd->label);
74 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
76 Widget_Data *wd = elm_widget_data_get(obj);
78 if (elm_widget_focus_get(obj))
80 edje_object_signal_emit(wd->btn, "elm,action,focus", "elm");
81 evas_object_focus_set(wd->btn, EINA_TRUE);
85 edje_object_signal_emit(wd->btn, "elm,action,unfocus", "elm");
86 evas_object_focus_set(wd->btn, EINA_FALSE);
91 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
93 Widget_Data *wd = elm_widget_data_get(obj);
95 edje_object_mirrored_set(wd->btn, rtl);
99 _theme_hook(Evas_Object *obj)
101 Widget_Data *wd = elm_widget_data_get(obj);
104 _elm_widget_mirrored_reload(obj);
105 _mirrored_set(obj, elm_widget_mirrored_get(obj));
106 _elm_theme_object_set(obj, wd->btn, "button", "base", elm_widget_style_get(obj));
108 edje_object_part_swallow(wd->btn, "elm.swallow.content", wd->icon);
110 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
112 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
114 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
116 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
117 edje_object_part_text_set(wd->btn, "elm.text", wd->label);
118 if (elm_object_disabled_get(obj))
119 edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
120 edje_object_message_signal_process(wd->btn);
121 edje_object_scale_set(wd->btn, elm_widget_scale_get(obj) * _elm_config->scale);
122 str = edje_object_data_get(wd->btn, "focus_highlight");
123 if ((str) && (!strcmp(str, "on")))
124 elm_widget_highlight_in_theme_set(obj, EINA_TRUE);
126 elm_widget_highlight_in_theme_set(obj, EINA_FALSE);
131 _disable_hook(Evas_Object *obj)
133 Widget_Data *wd = elm_widget_data_get(obj);
135 if (elm_widget_disabled_get(obj))
136 edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
138 edje_object_signal_emit(wd->btn, "elm,state,enabled", "elm");
142 _signal_emit_hook(Evas_Object *obj, const char *emission, const char *source)
144 Widget_Data *wd = elm_widget_data_get(obj);
146 edje_object_signal_emit(wd->btn, emission, source);
150 _signal_callback_add_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
152 Widget_Data *wd = elm_widget_data_get(obj);
154 edje_object_signal_callback_add(wd->btn, emission, source, func_cb, data);
158 _signal_callback_del_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
160 Widget_Data *wd = elm_widget_data_get(obj);
161 edje_object_signal_callback_del_full(wd->btn, emission, source, func_cb,
166 _sizing_eval(Evas_Object *obj)
168 Widget_Data *wd = elm_widget_data_get(obj);
169 Evas_Coord minw = -1, minh = -1;
172 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
173 edje_object_size_min_restricted_calc(wd->btn, &minw, &minh, minw, minh);
174 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
175 evas_object_size_hint_min_set(obj, minw, minh);
179 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
181 Widget_Data *wd = elm_widget_data_get(data);
183 if (obj != wd->icon) return;
188 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
190 Widget_Data *wd = elm_widget_data_get(obj);
191 Evas_Object *sub = event_info;
195 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
196 evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
197 _changed_size_hints, obj);
199 edje_object_message_signal_process(wd->btn);
205 _activate(Evas_Object *obj)
207 Widget_Data *wd = elm_widget_data_get(obj);
211 ecore_timer_del(wd->timer);
214 wd->repeating = EINA_FALSE;
215 evas_object_smart_callback_call(obj, SIG_CLICKED, NULL);
219 _activate_hook(Evas_Object *obj)
225 _signal_clicked(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
231 _autorepeat_send(void *data)
233 Widget_Data *wd = elm_widget_data_get(data);
234 if (!wd) return ECORE_CALLBACK_CANCEL;
236 evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
240 return ECORE_CALLBACK_CANCEL;
243 return ECORE_CALLBACK_RENEW;
247 _autorepeat_initial_send(void *data)
249 Widget_Data *wd = elm_widget_data_get(data);
250 if (!wd) return ECORE_CALLBACK_CANCEL;
252 if (wd->timer) ecore_timer_del(wd->timer);
253 wd->repeating = EINA_TRUE;
254 _autorepeat_send(data);
255 wd->timer = ecore_timer_add(wd->ar_interval, _autorepeat_send, data);
257 return ECORE_CALLBACK_CANCEL;
261 _signal_pressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
263 Widget_Data *wd = elm_widget_data_get(data);
266 if ((wd->autorepeat) && (!wd->repeating))
268 if (wd->ar_threshold <= 0.0)
269 _autorepeat_initial_send(data); /* call immediately */
271 wd->timer = ecore_timer_add(wd->ar_threshold, _autorepeat_initial_send, data);
274 evas_object_smart_callback_call(data, SIG_PRESSED, NULL);
278 _signal_unpressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
280 Widget_Data *wd = elm_widget_data_get(data);
285 ecore_timer_del(wd->timer);
288 wd->repeating = EINA_FALSE;
289 evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
293 _elm_button_label_set(Evas_Object *obj, const char *item, const char *label)
295 ELM_CHECK_WIDTYPE(obj, widtype);
296 Widget_Data *wd = elm_widget_data_get(obj);
297 if (item && strcmp(item, "default")) return;
299 eina_stringshare_replace(&wd->label, label);
301 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
303 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
304 edje_object_message_signal_process(wd->btn);
305 edje_object_part_text_set(wd->btn, "elm.text", label);
310 _elm_button_label_get(const Evas_Object *obj, const char *item)
312 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
313 Widget_Data *wd = elm_widget_data_get(obj);
314 if (item && strcmp(item, "default")) return NULL;
315 if (!wd) return NULL;
320 _access_info_cb(const void *data __UNUSED__, Evas_Object *obj, Elm_Widget_Item *item __UNUSED__)
322 char *txt = (char *)_elm_button_label_get(obj, NULL);
323 if (txt) return strdup(txt);
328 _access_state_cb(const void *data __UNUSED__, Evas_Object *obj, Elm_Widget_Item *item __UNUSED__)
330 if (elm_widget_disabled_get(obj))
331 return strdup(E_("State: Disabled"));
336 elm_button_add(Evas_Object *parent)
342 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
344 ELM_SET_WIDTYPE(widtype, "button");
345 elm_widget_type_set(obj, "button");
346 elm_widget_sub_object_add(parent, obj);
347 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
348 elm_widget_data_set(obj, wd);
349 elm_widget_del_hook_set(obj, _del_hook);
350 elm_widget_theme_hook_set(obj, _theme_hook);
351 elm_widget_disable_hook_set(obj, _disable_hook);
352 elm_widget_can_focus_set(obj, EINA_TRUE);
353 elm_widget_activate_hook_set(obj, _activate_hook);
354 elm_widget_event_hook_set(obj, _event_hook);
355 elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
356 elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
357 elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
358 elm_widget_text_set_hook_set(obj, _elm_button_label_set);
359 elm_widget_text_get_hook_set(obj, _elm_button_label_get);
361 wd->btn = edje_object_add(e);
362 _elm_theme_object_set(obj, wd->btn, "button", "base", "default");
363 edje_object_signal_callback_add(wd->btn, "elm,action,click", "",
364 _signal_clicked, obj);
365 edje_object_signal_callback_add(wd->btn, "elm,action,press", "",
366 _signal_pressed, obj);
367 edje_object_signal_callback_add(wd->btn, "elm,action,unpress", "",
368 _signal_unpressed, obj);
369 elm_widget_resize_object_set(obj, wd->btn);
371 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
375 // TODO: convert Elementary to subclassing of Evas_Smart_Class
376 // TODO: and save some bytes, making descriptions per-class and not instance!
377 evas_object_smart_callbacks_descriptions_set(obj, _signals);
379 _elm_access_object_register(obj, wd->btn);
380 _elm_access_text_set(_elm_access_object_get(obj),
381 ELM_ACCESS_TYPE, E_("Button"));
382 _elm_access_callback_set(_elm_access_object_get(obj),
383 ELM_ACCESS_INFO, _access_info_cb, obj);
384 _elm_access_callback_set(_elm_access_object_get(obj),
385 ELM_ACCESS_STATE, _access_state_cb, obj);
390 elm_button_label_set(Evas_Object *obj, const char *label)
392 _elm_button_label_set(obj, NULL, label);
396 elm_button_label_get(const Evas_Object *obj)
398 return _elm_button_label_get(obj, NULL);
402 elm_button_icon_set(Evas_Object *obj, Evas_Object *icon)
404 ELM_CHECK_WIDTYPE(obj, widtype);
405 Widget_Data *wd = elm_widget_data_get(obj);
407 if (wd->icon == icon) return;
408 if (wd->icon) evas_object_del(wd->icon);
412 elm_widget_sub_object_add(obj, icon);
413 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
414 _changed_size_hints, obj);
415 edje_object_part_swallow(wd->btn, "elm.swallow.content", icon);
416 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
417 edje_object_message_signal_process(wd->btn);
423 elm_button_icon_get(const Evas_Object *obj)
425 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
426 Widget_Data *wd = elm_widget_data_get(obj);
427 if (!wd) return NULL;
432 elm_button_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->btn, wd->icon);
446 elm_button_autorepeat_set(Evas_Object *obj, Eina_Bool on)
448 ELM_CHECK_WIDTYPE(obj, widtype);
449 Widget_Data *wd = elm_widget_data_get(obj);
453 ecore_timer_del(wd->timer);
457 wd->repeating = EINA_FALSE;
461 elm_button_autorepeat_get(const Evas_Object *obj)
463 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
464 Widget_Data *wd = elm_widget_data_get(obj);
465 if (!wd) return EINA_FALSE;
466 return wd->autorepeat;
470 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj, double t)
472 ELM_CHECK_WIDTYPE(obj, widtype);
473 Widget_Data *wd = elm_widget_data_get(obj);
475 if (wd->ar_threshold == t) return;
478 ecore_timer_del(wd->timer);
481 wd->ar_threshold = t;
485 elm_button_autorepeat_initial_timeout_get(const Evas_Object *obj)
487 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
488 Widget_Data *wd = elm_widget_data_get(obj);
490 return wd->ar_threshold;
494 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj, double t)
496 ELM_CHECK_WIDTYPE(obj, widtype);
497 Widget_Data *wd = elm_widget_data_get(obj);
499 if (wd->ar_interval == t) return;
502 if ((wd->repeating) && (wd->timer)) ecore_timer_interval_set(wd->timer, t);
506 elm_button_autorepeat_gap_timeout_get(const Evas_Object *obj)
508 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
509 Widget_Data *wd = elm_widget_data_get(obj);
511 return wd->ar_interval;