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_UNPRESSED[] = "unpressed";
36 static const Evas_Smart_Cb_Description _signals[] = {
44 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
46 if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
47 Evas_Event_Key_Down *ev = event_info;
48 Widget_Data *wd = elm_widget_data_get(obj);
49 if (!wd) return EINA_FALSE;
50 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
51 if (elm_widget_disabled_get(obj)) return EINA_FALSE;
52 if ((strcmp(ev->keyname, "Return")) &&
53 (strcmp(ev->keyname, "KP_Enter")) &&
54 (strcmp(ev->keyname, "space")))
57 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
58 edje_object_signal_emit(wd->btn, "elm,anim,activate", "elm");
63 _del_hook(Evas_Object *obj)
65 Widget_Data *wd = elm_widget_data_get(obj);
67 if (wd->label) eina_stringshare_del(wd->label);
72 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
74 Widget_Data *wd = elm_widget_data_get(obj);
76 if (elm_widget_focus_get(obj))
78 edje_object_signal_emit(wd->btn, "elm,action,focus", "elm");
79 evas_object_focus_set(wd->btn, EINA_TRUE);
83 edje_object_signal_emit(wd->btn, "elm,action,unfocus", "elm");
84 evas_object_focus_set(wd->btn, EINA_FALSE);
89 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
91 Widget_Data *wd = elm_widget_data_get(obj);
93 edje_object_mirrored_set(wd->btn, rtl);
97 _theme_hook(Evas_Object *obj)
99 Widget_Data *wd = elm_widget_data_get(obj);
102 _elm_widget_mirrored_reload(obj);
103 _mirrored_set(obj, elm_widget_mirrored_get(obj));
104 _elm_theme_object_set(obj, wd->btn, "button", "base", elm_widget_style_get(obj));
106 edje_object_part_swallow(wd->btn, "elm.swallow.content", wd->icon);
108 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
110 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
112 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
114 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
115 edje_object_part_text_set(wd->btn, "elm.text", wd->label);
116 if (elm_object_disabled_get(obj))
117 edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
118 edje_object_message_signal_process(wd->btn);
119 edje_object_scale_set(wd->btn, elm_widget_scale_get(obj) * _elm_config->scale);
120 str = edje_object_data_get(wd->btn, "focus_highlight");
121 if ((str) && (!strcmp(str, "on")))
122 elm_widget_highlight_in_theme_set(obj, EINA_TRUE);
124 elm_widget_highlight_in_theme_set(obj, EINA_FALSE);
129 _disable_hook(Evas_Object *obj)
131 Widget_Data *wd = elm_widget_data_get(obj);
133 if (elm_widget_disabled_get(obj))
134 edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
136 edje_object_signal_emit(wd->btn, "elm,state,enabled", "elm");
140 _signal_emit_hook(Evas_Object *obj, const char *emission, const char *source)
142 Widget_Data *wd = elm_widget_data_get(obj);
144 edje_object_signal_emit(wd->btn, emission, source);
148 _signal_callback_add_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
150 Widget_Data *wd = elm_widget_data_get(obj);
152 edje_object_signal_callback_add(wd->btn, emission, source, func_cb, data);
156 _signal_callback_del_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
158 Widget_Data *wd = elm_widget_data_get(obj);
159 edje_object_signal_callback_del_full(wd->btn, emission, source, func_cb,
164 _sizing_eval(Evas_Object *obj)
166 Widget_Data *wd = elm_widget_data_get(obj);
167 Evas_Coord minw = -1, minh = -1;
170 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
171 edje_object_size_min_restricted_calc(wd->btn, &minw, &minh, minw, minh);
172 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
173 evas_object_size_hint_min_set(obj, minw, minh);
177 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
179 Widget_Data *wd = elm_widget_data_get(data);
181 if (obj != wd->icon) return;
186 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
188 Widget_Data *wd = elm_widget_data_get(obj);
189 Evas_Object *sub = event_info;
193 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
194 evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
195 _changed_size_hints, obj);
197 edje_object_message_signal_process(wd->btn);
203 _activate(Evas_Object *obj)
205 Widget_Data *wd = elm_widget_data_get(obj);
209 ecore_timer_del(wd->timer);
212 wd->repeating = EINA_FALSE;
213 evas_object_smart_callback_call(obj, SIG_CLICKED, NULL);
217 _activate_hook(Evas_Object *obj)
223 _signal_clicked(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
229 _autorepeat_send(void *data)
231 Widget_Data *wd = elm_widget_data_get(data);
232 if (!wd) return ECORE_CALLBACK_CANCEL;
234 evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
238 return ECORE_CALLBACK_CANCEL;
241 return ECORE_CALLBACK_RENEW;
245 _autorepeat_initial_send(void *data)
247 Widget_Data *wd = elm_widget_data_get(data);
248 if (!wd) return ECORE_CALLBACK_CANCEL;
250 if (wd->timer) ecore_timer_del(wd->timer);
251 wd->repeating = EINA_TRUE;
252 _autorepeat_send(data);
253 wd->timer = ecore_timer_add(wd->ar_interval, _autorepeat_send, data);
255 return ECORE_CALLBACK_CANCEL;
259 _signal_pressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
261 Widget_Data *wd = elm_widget_data_get(data);
264 if ((wd->autorepeat) && (!wd->repeating))
266 if (wd->ar_threshold <= 0.0)
267 _autorepeat_initial_send(data); /* call immediately */
269 wd->timer = ecore_timer_add(wd->ar_threshold, _autorepeat_initial_send, data);
274 _signal_unpressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
276 Widget_Data *wd = elm_widget_data_get(data);
281 ecore_timer_del(wd->timer);
284 wd->repeating = EINA_FALSE;
285 evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
289 elm_button_add(Evas_Object *parent)
295 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
297 ELM_SET_WIDTYPE(widtype, "button");
298 elm_widget_type_set(obj, "button");
299 elm_widget_sub_object_add(parent, obj);
300 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
301 elm_widget_data_set(obj, wd);
302 elm_widget_del_hook_set(obj, _del_hook);
303 elm_widget_theme_hook_set(obj, _theme_hook);
304 elm_widget_disable_hook_set(obj, _disable_hook);
305 elm_widget_can_focus_set(obj, EINA_TRUE);
306 elm_widget_activate_hook_set(obj, _activate_hook);
307 elm_widget_event_hook_set(obj, _event_hook);
308 elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
309 elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
310 elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
312 wd->btn = edje_object_add(e);
313 _elm_theme_object_set(obj, wd->btn, "button", "base", "default");
314 edje_object_signal_callback_add(wd->btn, "elm,action,click", "",
315 _signal_clicked, obj);
316 edje_object_signal_callback_add(wd->btn, "elm,action,press", "",
317 _signal_pressed, obj);
318 edje_object_signal_callback_add(wd->btn, "elm,action,unpress", "",
319 _signal_unpressed, obj);
320 elm_widget_resize_object_set(obj, wd->btn);
322 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
326 // TODO: convert Elementary to subclassing of Evas_Smart_Class
327 // TODO: and save some bytes, making descriptions per-class and not instance!
328 evas_object_smart_callbacks_descriptions_set(obj, _signals);
333 elm_button_label_set(Evas_Object *obj, const char *label)
335 ELM_CHECK_WIDTYPE(obj, widtype);
336 Widget_Data *wd = elm_widget_data_get(obj);
338 eina_stringshare_replace(&wd->label, label);
340 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
342 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
343 edje_object_message_signal_process(wd->btn);
344 edje_object_part_text_set(wd->btn, "elm.text", label);
349 elm_button_label_get(const Evas_Object *obj)
351 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
352 Widget_Data *wd = elm_widget_data_get(obj);
353 if (!wd) return NULL;
358 elm_button_icon_set(Evas_Object *obj, Evas_Object *icon)
360 ELM_CHECK_WIDTYPE(obj, widtype);
361 Widget_Data *wd = elm_widget_data_get(obj);
363 if (wd->icon == icon) return;
364 if (wd->icon) evas_object_del(wd->icon);
368 elm_widget_sub_object_add(obj, icon);
369 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
370 _changed_size_hints, obj);
371 edje_object_part_swallow(wd->btn, "elm.swallow.content", icon);
372 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
373 edje_object_message_signal_process(wd->btn);
379 elm_button_icon_get(const Evas_Object *obj)
381 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
382 Widget_Data *wd = elm_widget_data_get(obj);
383 if (!wd) return NULL;
388 elm_button_icon_unset(Evas_Object *obj)
390 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
391 Widget_Data *wd = elm_widget_data_get(obj);
392 if (!wd) return NULL;
393 if (!wd->icon) return NULL;
394 Evas_Object *icon = wd->icon;
395 elm_widget_sub_object_del(obj, wd->icon);
396 edje_object_part_unswallow(wd->btn, wd->icon);
402 elm_button_autorepeat_set(Evas_Object *obj, Eina_Bool on)
404 ELM_CHECK_WIDTYPE(obj, widtype);
405 Widget_Data *wd = elm_widget_data_get(obj);
409 ecore_timer_del(wd->timer);
413 wd->repeating = EINA_FALSE;
417 elm_button_autorepeat_get(const Evas_Object *obj)
419 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
420 Widget_Data *wd = elm_widget_data_get(obj);
421 if (!wd) return EINA_FALSE;
422 return wd->autorepeat;
426 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj, double t)
428 ELM_CHECK_WIDTYPE(obj, widtype);
429 Widget_Data *wd = elm_widget_data_get(obj);
431 if (wd->ar_threshold == t) return;
434 ecore_timer_del(wd->timer);
437 wd->ar_threshold = t;
441 elm_button_autorepeat_initial_timeout_get(const Evas_Object *obj)
443 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
444 Widget_Data *wd = elm_widget_data_get(obj);
446 return wd->ar_threshold;
450 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj, double t)
452 ELM_CHECK_WIDTYPE(obj, widtype);
453 Widget_Data *wd = elm_widget_data_get(obj);
455 if (wd->ar_interval == t) return;
458 if ((wd->repeating) && (wd->timer)) ecore_timer_interval_set(wd->timer, t);
462 elm_button_autorepeat_gap_timeout_get(const Evas_Object *obj)
464 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
465 Widget_Data *wd = elm_widget_data_get(obj);
467 return wd->ar_interval;