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);
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) return NULL;
315 if (!wd) return NULL;
320 elm_button_add(Evas_Object *parent)
326 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
328 ELM_SET_WIDTYPE(widtype, "button");
329 elm_widget_type_set(obj, "button");
330 elm_widget_sub_object_add(parent, obj);
331 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
332 elm_widget_data_set(obj, wd);
333 elm_widget_del_hook_set(obj, _del_hook);
334 elm_widget_theme_hook_set(obj, _theme_hook);
335 elm_widget_disable_hook_set(obj, _disable_hook);
336 elm_widget_can_focus_set(obj, EINA_TRUE);
337 elm_widget_activate_hook_set(obj, _activate_hook);
338 elm_widget_event_hook_set(obj, _event_hook);
339 elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
340 elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
341 elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
342 elm_widget_label_set_hook_set(obj, _elm_button_label_set);
343 elm_widget_label_get_hook_set(obj, _elm_button_label_get);
345 wd->btn = edje_object_add(e);
346 _elm_theme_object_set(obj, wd->btn, "button", "base", "default");
347 edje_object_signal_callback_add(wd->btn, "elm,action,click", "",
348 _signal_clicked, obj);
349 edje_object_signal_callback_add(wd->btn, "elm,action,press", "",
350 _signal_pressed, obj);
351 edje_object_signal_callback_add(wd->btn, "elm,action,unpress", "",
352 _signal_unpressed, obj);
353 elm_widget_resize_object_set(obj, wd->btn);
355 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
359 // TODO: convert Elementary to subclassing of Evas_Smart_Class
360 // TODO: and save some bytes, making descriptions per-class and not instance!
361 evas_object_smart_callbacks_descriptions_set(obj, _signals);
366 elm_button_label_set(Evas_Object *obj, const char *label)
368 _elm_button_label_set(obj, NULL, label);
372 elm_button_label_get(const Evas_Object *obj)
374 return _elm_button_label_get(obj, NULL);
378 elm_button_icon_set(Evas_Object *obj, Evas_Object *icon)
380 ELM_CHECK_WIDTYPE(obj, widtype);
381 Widget_Data *wd = elm_widget_data_get(obj);
383 if (wd->icon == icon) return;
384 if (wd->icon) evas_object_del(wd->icon);
388 elm_widget_sub_object_add(obj, icon);
389 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
390 _changed_size_hints, obj);
391 edje_object_part_swallow(wd->btn, "elm.swallow.content", icon);
392 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
393 edje_object_message_signal_process(wd->btn);
399 elm_button_icon_get(const Evas_Object *obj)
401 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
402 Widget_Data *wd = elm_widget_data_get(obj);
403 if (!wd) return NULL;
408 elm_button_icon_unset(Evas_Object *obj)
410 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
411 Widget_Data *wd = elm_widget_data_get(obj);
412 if (!wd) return NULL;
413 if (!wd->icon) return NULL;
414 Evas_Object *icon = wd->icon;
415 elm_widget_sub_object_del(obj, wd->icon);
416 edje_object_part_unswallow(wd->btn, wd->icon);
422 elm_button_autorepeat_set(Evas_Object *obj, Eina_Bool on)
424 ELM_CHECK_WIDTYPE(obj, widtype);
425 Widget_Data *wd = elm_widget_data_get(obj);
429 ecore_timer_del(wd->timer);
433 wd->repeating = EINA_FALSE;
437 elm_button_autorepeat_get(const Evas_Object *obj)
439 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
440 Widget_Data *wd = elm_widget_data_get(obj);
441 if (!wd) return EINA_FALSE;
442 return wd->autorepeat;
446 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj, double t)
448 ELM_CHECK_WIDTYPE(obj, widtype);
449 Widget_Data *wd = elm_widget_data_get(obj);
451 if (wd->ar_threshold == t) return;
454 ecore_timer_del(wd->timer);
457 wd->ar_threshold = t;
461 elm_button_autorepeat_initial_timeout_get(const Evas_Object *obj)
463 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
464 Widget_Data *wd = elm_widget_data_get(obj);
466 return wd->ar_threshold;
470 elm_button_autorepeat_gap_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_interval == t) return;
478 if ((wd->repeating) && (wd->timer)) ecore_timer_interval_set(wd->timer, t);
482 elm_button_autorepeat_gap_timeout_get(const Evas_Object *obj)
484 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
485 Widget_Data *wd = elm_widget_data_get(obj);
487 return wd->ar_interval;