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 if ((_elm_config->access_mode == ELM_ACCESS_MODE_OFF) ||
216 (_elm_access_2nd_click_timeout(obj)))
218 if (_elm_config->access_mode != ELM_ACCESS_MODE_OFF)
219 _elm_access_say(E_("Clicked"));
220 evas_object_smart_callback_call(obj, SIG_CLICKED, NULL);
225 _activate_hook(Evas_Object *obj)
231 _signal_clicked(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
237 _autorepeat_send(void *data)
239 Widget_Data *wd = elm_widget_data_get(data);
240 if (!wd) return ECORE_CALLBACK_CANCEL;
242 evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
246 return ECORE_CALLBACK_CANCEL;
249 return ECORE_CALLBACK_RENEW;
253 _autorepeat_initial_send(void *data)
255 Widget_Data *wd = elm_widget_data_get(data);
256 if (!wd) return ECORE_CALLBACK_CANCEL;
258 if (wd->timer) ecore_timer_del(wd->timer);
259 wd->repeating = EINA_TRUE;
260 _autorepeat_send(data);
261 wd->timer = ecore_timer_add(wd->ar_interval, _autorepeat_send, data);
263 return ECORE_CALLBACK_CANCEL;
267 _signal_pressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
269 Widget_Data *wd = elm_widget_data_get(data);
272 if ((wd->autorepeat) && (!wd->repeating))
274 if (wd->ar_threshold <= 0.0)
275 _autorepeat_initial_send(data); /* call immediately */
277 wd->timer = ecore_timer_add(wd->ar_threshold, _autorepeat_initial_send, data);
280 evas_object_smart_callback_call(data, SIG_PRESSED, NULL);
284 _signal_unpressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
286 Widget_Data *wd = elm_widget_data_get(data);
291 ecore_timer_del(wd->timer);
294 wd->repeating = EINA_FALSE;
295 evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
299 _elm_button_label_set(Evas_Object *obj, const char *item, const char *label)
301 ELM_CHECK_WIDTYPE(obj, widtype);
302 Widget_Data *wd = elm_widget_data_get(obj);
303 if (item && strcmp(item, "default")) return;
305 eina_stringshare_replace(&wd->label, label);
307 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
309 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
310 edje_object_message_signal_process(wd->btn);
311 edje_object_part_text_set(wd->btn, "elm.text", label);
316 _elm_button_label_get(const Evas_Object *obj, const char *item)
318 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
319 Widget_Data *wd = elm_widget_data_get(obj);
320 if (item && strcmp(item, "default")) return NULL;
321 if (!wd) return NULL;
326 _access_info_cb(void *data __UNUSED__, Evas_Object *obj, Elm_Widget_Item *item __UNUSED__)
328 char *txt = (char *)elm_widget_access_info_get(obj);
329 if (!txt) txt = (char *)_elm_button_label_get(obj, NULL);
330 if (txt) return strdup(txt);
335 _access_state_cb(void *data __UNUSED__, Evas_Object *obj, Elm_Widget_Item *item __UNUSED__)
337 if (elm_widget_disabled_get(obj))
338 return strdup(E_("State: Disabled"));
343 elm_button_add(Evas_Object *parent)
349 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
351 ELM_SET_WIDTYPE(widtype, "button");
352 elm_widget_type_set(obj, "button");
353 elm_widget_sub_object_add(parent, obj);
354 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
355 elm_widget_data_set(obj, wd);
356 elm_widget_del_hook_set(obj, _del_hook);
357 elm_widget_theme_hook_set(obj, _theme_hook);
358 elm_widget_disable_hook_set(obj, _disable_hook);
359 elm_widget_can_focus_set(obj, EINA_TRUE);
360 elm_widget_activate_hook_set(obj, _activate_hook);
361 elm_widget_event_hook_set(obj, _event_hook);
362 elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
363 elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
364 elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
365 elm_widget_text_set_hook_set(obj, _elm_button_label_set);
366 elm_widget_text_get_hook_set(obj, _elm_button_label_get);
368 wd->btn = edje_object_add(e);
369 _elm_theme_object_set(obj, wd->btn, "button", "base", "default");
370 edje_object_signal_callback_add(wd->btn, "elm,action,click", "",
371 _signal_clicked, obj);
372 edje_object_signal_callback_add(wd->btn, "elm,action,press", "",
373 _signal_pressed, obj);
374 edje_object_signal_callback_add(wd->btn, "elm,action,unpress", "",
375 _signal_unpressed, obj);
376 elm_widget_resize_object_set(obj, wd->btn);
378 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
382 // TODO: convert Elementary to subclassing of Evas_Smart_Class
383 // TODO: and save some bytes, making descriptions per-class and not instance!
384 evas_object_smart_callbacks_descriptions_set(obj, _signals);
386 _elm_access_object_register(obj, wd->btn);
387 _elm_access_text_set(_elm_access_object_get(obj),
388 ELM_ACCESS_TYPE, E_("Button"));
389 _elm_access_callback_set(_elm_access_object_get(obj),
390 ELM_ACCESS_INFO, _access_info_cb, obj);
391 _elm_access_callback_set(_elm_access_object_get(obj),
392 ELM_ACCESS_STATE, _access_state_cb, obj);
397 elm_button_label_set(Evas_Object *obj, const char *label)
399 _elm_button_label_set(obj, NULL, label);
403 elm_button_label_get(const Evas_Object *obj)
405 return _elm_button_label_get(obj, NULL);
409 elm_button_icon_set(Evas_Object *obj, Evas_Object *icon)
411 ELM_CHECK_WIDTYPE(obj, widtype);
412 Widget_Data *wd = elm_widget_data_get(obj);
414 if (wd->icon == icon) return;
415 if (wd->icon) evas_object_del(wd->icon);
419 elm_widget_sub_object_add(obj, icon);
420 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
421 _changed_size_hints, obj);
422 edje_object_part_swallow(wd->btn, "elm.swallow.content", icon);
423 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
424 edje_object_message_signal_process(wd->btn);
430 elm_button_icon_get(const Evas_Object *obj)
432 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
433 Widget_Data *wd = elm_widget_data_get(obj);
434 if (!wd) return NULL;
439 elm_button_icon_unset(Evas_Object *obj)
441 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
442 Widget_Data *wd = elm_widget_data_get(obj);
443 if (!wd) return NULL;
444 if (!wd->icon) return NULL;
445 Evas_Object *icon = wd->icon;
446 elm_widget_sub_object_del(obj, wd->icon);
447 edje_object_part_unswallow(wd->btn, wd->icon);
453 elm_button_autorepeat_set(Evas_Object *obj, Eina_Bool on)
455 ELM_CHECK_WIDTYPE(obj, widtype);
456 Widget_Data *wd = elm_widget_data_get(obj);
460 ecore_timer_del(wd->timer);
464 wd->repeating = EINA_FALSE;
468 elm_button_autorepeat_get(const Evas_Object *obj)
470 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
471 Widget_Data *wd = elm_widget_data_get(obj);
472 if (!wd) return EINA_FALSE;
473 return wd->autorepeat;
477 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj, double t)
479 ELM_CHECK_WIDTYPE(obj, widtype);
480 Widget_Data *wd = elm_widget_data_get(obj);
482 if (wd->ar_threshold == t) return;
485 ecore_timer_del(wd->timer);
488 wd->ar_threshold = t;
492 elm_button_autorepeat_initial_timeout_get(const Evas_Object *obj)
494 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
495 Widget_Data *wd = elm_widget_data_get(obj);
497 return wd->ar_threshold;
501 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj, double t)
503 ELM_CHECK_WIDTYPE(obj, widtype);
504 Widget_Data *wd = elm_widget_data_get(obj);
506 if (wd->ar_interval == t) return;
509 if ((wd->repeating) && (wd->timer)) ecore_timer_interval_set(wd->timer, t);
513 elm_button_autorepeat_gap_timeout_get(const Evas_Object *obj)
515 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
516 Widget_Data *wd = elm_widget_data_get(obj);
518 return wd->ar_interval;