1 #include <Elementary.h>
5 * @defgroup Button Button
7 * This is a push-button. Press it and run some function. It can contain
8 * a simple label and icon object.
11 typedef struct _Widget_Data Widget_Data;
15 Evas_Object *btn, *icon;
24 static const char *widtype = NULL;
25 static void _del_hook(Evas_Object *obj);
26 static void _theme_hook(Evas_Object *obj);
27 static void _disable_hook(Evas_Object *obj);
28 static void _sizing_eval(Evas_Object *obj);
29 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
30 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
31 static void _signal_clicked(void *data, Evas_Object *obj, const char *emission, const char *source);
32 static void _signal_pressed(void *data, Evas_Object *obj, const char *emission, const char *source);
33 static void _signal_unpressed(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_CLICKED[] = "clicked";
41 static const char SIG_REPEATED[] = "repeated";
42 static const char SIG_UNPRESSED[] = "unpressed";
43 static const Evas_Smart_Cb_Description _signals[] = {
51 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
53 if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
54 Evas_Event_Key_Down *ev = event_info;
55 Widget_Data *wd = elm_widget_data_get(obj);
56 if (!wd) return EINA_FALSE;
57 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
58 if (elm_widget_disabled_get(obj)) return EINA_FALSE;
59 if ((strcmp(ev->keyname, "Return")) &&
60 (strcmp(ev->keyname, "KP_Enter")) &&
61 (strcmp(ev->keyname, "space")))
64 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
65 edje_object_signal_emit(wd->btn, "elm,anim,activate", "elm");
70 _del_hook(Evas_Object *obj)
72 Widget_Data *wd = elm_widget_data_get(obj);
74 if (wd->label) eina_stringshare_del(wd->label);
79 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
81 Widget_Data *wd = elm_widget_data_get(obj);
83 if (elm_widget_focus_get(obj))
85 edje_object_signal_emit(wd->btn, "elm,action,focus", "elm");
86 evas_object_focus_set(wd->btn, EINA_TRUE);
90 edje_object_signal_emit(wd->btn, "elm,action,unfocus", "elm");
91 evas_object_focus_set(wd->btn, EINA_FALSE);
96 _theme_hook(Evas_Object *obj)
98 Widget_Data *wd = elm_widget_data_get(obj);
101 _elm_theme_object_set(obj, wd->btn, "button", "base", elm_widget_style_get(obj));
103 edje_object_part_swallow(wd->btn, "elm.swallow.content", wd->icon);
105 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
107 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
109 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
111 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
112 edje_object_part_text_set(wd->btn, "elm.text", wd->label);
113 if (elm_object_disabled_get(obj))
114 edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
115 edje_object_message_signal_process(wd->btn);
116 edje_object_scale_set(wd->btn, elm_widget_scale_get(obj) * _elm_config->scale);
117 str = edje_object_data_get(wd->btn, "focus_highlight");
118 if ((str) && (!strcmp(str, "on")))
119 elm_widget_highlight_in_theme_set(obj, EINA_TRUE);
121 elm_widget_highlight_in_theme_set(obj, EINA_FALSE);
126 _disable_hook(Evas_Object *obj)
128 Widget_Data *wd = elm_widget_data_get(obj);
130 if (elm_widget_disabled_get(obj))
131 edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
133 edje_object_signal_emit(wd->btn, "elm,state,enabled", "elm");
137 _signal_emit_hook(Evas_Object *obj, const char *emission, const char *source)
139 Widget_Data *wd = elm_widget_data_get(obj);
141 edje_object_signal_emit(wd->btn, emission, source);
145 _signal_callback_add_hook(Evas_Object *obj, const char *emission, const char *source, void (*func_cb) (void *data, Evas_Object *o, const char *emission, const char *source), void *data)
147 Widget_Data *wd = elm_widget_data_get(obj);
149 edje_object_signal_callback_add(wd->btn, emission, source, func_cb, data);
153 _signal_callback_del_hook(Evas_Object *obj, const char *emission, const char *source, void (*func_cb) (void *data, Evas_Object *o, const char *emission, const char *source), void *data)
155 Widget_Data *wd = elm_widget_data_get(obj);
156 edje_object_signal_callback_del_full(wd->btn, emission, source, func_cb,
161 _sizing_eval(Evas_Object *obj)
163 Widget_Data *wd = elm_widget_data_get(obj);
164 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
167 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
168 edje_object_size_min_restricted_calc(wd->btn, &minw, &minh, minw, minh);
169 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
170 evas_object_size_hint_min_set(obj, minw, minh);
171 evas_object_size_hint_max_set(obj, maxw, maxh);
175 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
177 Widget_Data *wd = elm_widget_data_get(data);
179 if (obj != wd->icon) return;
184 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
186 Widget_Data *wd = elm_widget_data_get(obj);
187 Evas_Object *sub = event_info;
191 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
192 evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
193 _changed_size_hints, obj);
195 edje_object_message_signal_process(wd->btn);
201 _activate(Evas_Object *obj)
203 Widget_Data *wd = elm_widget_data_get(obj);
207 ecore_timer_del(wd->timer);
210 wd->repeating = EINA_FALSE;
211 evas_object_smart_callback_call(obj, SIG_CLICKED, NULL);
212 _signal_unpressed(obj, wd->btn, NULL, NULL); /* safe guard when the theme does not emit the 'unpress' signal */
216 _activate_hook(Evas_Object *obj)
222 _signal_clicked(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
228 _autorepeat_send(void *data)
230 Widget_Data *wd = elm_widget_data_get(data);
231 if (!wd) return ECORE_CALLBACK_CANCEL;
233 evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
237 return ECORE_CALLBACK_CANCEL;
240 return ECORE_CALLBACK_RENEW;
244 _autorepeat_initial_send(void *data)
246 Widget_Data *wd = elm_widget_data_get(data);
247 if (!wd) return ECORE_CALLBACK_CANCEL;
249 if (wd->timer) ecore_timer_del(wd->timer);
250 wd->repeating = EINA_TRUE;
251 _autorepeat_send(data);
252 wd->timer = ecore_timer_add(wd->ar_interval, _autorepeat_send, data);
254 return ECORE_CALLBACK_CANCEL;
258 _signal_pressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
260 Widget_Data *wd = elm_widget_data_get(data);
263 if ((wd->autorepeat) && (!wd->repeating))
265 if (wd->ar_threshold <= 0.0)
266 _autorepeat_initial_send(data); /* call immediately */
268 wd->timer = ecore_timer_add(wd->ar_threshold, _autorepeat_initial_send, data);
273 _signal_unpressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
275 Widget_Data *wd = elm_widget_data_get(data);
280 ecore_timer_del(wd->timer);
283 wd->repeating = EINA_FALSE;
284 evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
288 * Add a new button to the parent
289 * @param parent The parent object
290 * @return The new object or NULL if it cannot be created
295 elm_button_add(Evas_Object *parent)
301 EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
303 wd = ELM_NEW(Widget_Data);
304 e = evas_object_evas_get(parent);
306 obj = elm_widget_add(e);
307 ELM_SET_WIDTYPE(widtype, "button");
308 elm_widget_type_set(obj, "button");
309 elm_widget_sub_object_add(parent, obj);
310 elm_widget_on_focus_hook_set( obj, _on_focus_hook, NULL );
311 elm_widget_data_set(obj, wd);
312 elm_widget_del_hook_set(obj, _del_hook);
313 elm_widget_theme_hook_set(obj, _theme_hook);
314 elm_widget_disable_hook_set(obj, _disable_hook);
315 elm_widget_can_focus_set(obj, EINA_TRUE);
316 elm_widget_activate_hook_set(obj, _activate_hook);
317 elm_widget_event_hook_set(obj, _event_hook);
318 elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
319 elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
320 elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
322 wd->btn = edje_object_add(e);
323 _elm_theme_object_set(obj, wd->btn, "button", "base", "default");
324 edje_object_signal_callback_add(wd->btn, "elm,action,click", "",
325 _signal_clicked, obj);
326 edje_object_signal_callback_add(wd->btn, "elm,action,press", "",
327 _signal_pressed, obj);
328 edje_object_signal_callback_add(wd->btn, "elm,action,unpress", "",
329 _signal_unpressed, obj);
330 elm_widget_resize_object_set(obj, wd->btn);
332 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
336 // TODO: convert Elementary to subclassing of Evas_Smart_Class
337 // TODO: and save some bytes, making descriptions per-class and not instance!
338 evas_object_smart_callbacks_descriptions_set(obj, _signals);
343 * Set the label used in the button
345 * @param obj The button object
346 * @param label The text will be written on the button
351 elm_button_label_set(Evas_Object *obj, const char *label)
353 ELM_CHECK_WIDTYPE(obj, widtype);
354 Widget_Data *wd = elm_widget_data_get(obj);
356 eina_stringshare_replace(&wd->label, label);
358 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
360 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
361 edje_object_message_signal_process(wd->btn);
362 edje_object_part_text_set(wd->btn, "elm.text", label);
367 elm_button_label_get(const Evas_Object *obj)
369 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
370 Widget_Data *wd = elm_widget_data_get(obj);
371 if (!wd) return NULL;
376 * Set the icon used for the button
378 * Once the icon object is set, a previously set one will be deleted
379 * If you want to keep that old content object, use the
380 * elm_button_icon_unset() function.
382 * @param obj The button object
383 * @param icon The icon object for the button
388 elm_button_icon_set(Evas_Object *obj, Evas_Object *icon)
390 ELM_CHECK_WIDTYPE(obj, widtype);
391 Widget_Data *wd = elm_widget_data_get(obj);
393 if (wd->icon == icon) return;
394 if (wd->icon) evas_object_del(wd->icon);
398 elm_widget_sub_object_add(obj, icon);
399 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
400 _changed_size_hints, obj);
401 edje_object_part_swallow(wd->btn, "elm.swallow.content", icon);
402 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
403 edje_object_message_signal_process(wd->btn);
409 * Get the icon used for the button
411 * Return the icon object which is set for this widget.
413 * @param obj The button object
414 * @return The icon object that is being used
419 elm_button_icon_get(const Evas_Object *obj)
421 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
422 Widget_Data *wd = elm_widget_data_get(obj);
423 if (!wd) return NULL;
428 * Unset the icon used for the button
430 * Unparent and return the icon object which was set for this widget.
432 * @param obj The button object
433 * @return The icon object that was being used
438 elm_button_icon_unset(Evas_Object *obj)
440 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
441 Widget_Data *wd = elm_widget_data_get(obj);
442 if (!wd) return NULL;
443 if (!wd->icon) return NULL;
444 Evas_Object *icon = wd->icon;
445 elm_widget_sub_object_del(obj, wd->icon);
446 edje_object_part_unswallow(wd->btn, wd->icon);
452 * Turn on/off the autorepeat event generated when the user keeps pressing on the button
454 * @param obj The button object
455 * @param on A bool to turn on/off the event
460 elm_button_autorepeat_set(Evas_Object *obj, Eina_Bool on)
462 ELM_CHECK_WIDTYPE(obj, widtype);
463 Widget_Data *wd = elm_widget_data_get(obj);
467 ecore_timer_del(wd->timer);
471 wd->repeating = EINA_FALSE;
475 * Get if autorepeat event is on
477 * @param obj The button object
478 * @return If autorepeat is on
483 elm_button_autorepeat_get(const Evas_Object *obj)
485 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
486 Widget_Data *wd = elm_widget_data_get(obj);
487 if (!wd) return EINA_FALSE;
488 return wd->autorepeat;
492 * Set the initial timeout before the autorepeat event is generated
494 * @param obj The button object
500 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj, double t)
502 ELM_CHECK_WIDTYPE(obj, widtype);
503 Widget_Data *wd = elm_widget_data_get(obj);
505 if (wd->ar_threshold == t) return;
508 ecore_timer_del(wd->timer);
511 wd->ar_threshold = t;
515 * Get the initial timeout before the autorepeat event is generated
517 * @param obj The button object
523 elm_button_autorepeat_initial_timeout_get(const Evas_Object *obj)
525 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
526 Widget_Data *wd = elm_widget_data_get(obj);
528 return wd->ar_threshold;
532 * Set the interval between each generated autorepeat event
534 * @param obj The button object
540 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj, double t)
542 ELM_CHECK_WIDTYPE(obj, widtype);
543 Widget_Data *wd = elm_widget_data_get(obj);
545 if (wd->ar_interval == t) return;
548 if ((wd->repeating) && (wd->timer)) ecore_timer_interval_set(wd->timer, t);
552 * Get the interval between each generated autorepeat event
554 * @param obj The button object
560 elm_button_autorepeat_gap_timeout_get(const Evas_Object *obj)
562 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
563 Widget_Data *wd = elm_widget_data_get(obj);
565 return wd->ar_interval;