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")) && (strcmp(ev->keyname, "space")))
62 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
63 edje_object_signal_emit(wd->btn, "elm,anim,activate", "elm");
68 _del_hook(Evas_Object *obj)
70 Widget_Data *wd = elm_widget_data_get(obj);
72 if (wd->label) eina_stringshare_del(wd->label);
77 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
79 Widget_Data *wd = elm_widget_data_get(obj);
81 if (elm_widget_focus_get(obj))
83 edje_object_signal_emit(wd->btn, "elm,action,focus", "elm");
84 evas_object_focus_set(wd->btn, EINA_TRUE);
88 edje_object_signal_emit(wd->btn, "elm,action,unfocus", "elm");
89 evas_object_focus_set(wd->btn, EINA_FALSE);
94 _theme_hook(Evas_Object *obj)
96 Widget_Data *wd = elm_widget_data_get(obj);
99 _elm_theme_object_set(obj, wd->btn, "button", "base", elm_widget_style_get(obj));
101 edje_object_part_swallow(wd->btn, "elm.swallow.content", wd->icon);
103 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
105 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
107 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
109 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
110 edje_object_part_text_set(wd->btn, "elm.text", wd->label);
111 if (elm_object_disabled_get(obj))
112 edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
113 edje_object_message_signal_process(wd->btn);
114 edje_object_scale_set(wd->btn, elm_widget_scale_get(obj) * _elm_config->scale);
115 str = edje_object_data_get(wd->btn, "focus_highlight");
116 if ((str) && (!strcmp(str, "on")))
117 elm_widget_highlight_in_theme_set(obj, EINA_TRUE);
119 elm_widget_highlight_in_theme_set(obj, EINA_FALSE);
124 _disable_hook(Evas_Object *obj)
126 Widget_Data *wd = elm_widget_data_get(obj);
128 if (elm_widget_disabled_get(obj))
129 edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
131 edje_object_signal_emit(wd->btn, "elm,state,enabled", "elm");
135 _signal_emit_hook(Evas_Object *obj, const char *emission, const char *source)
137 Widget_Data *wd = elm_widget_data_get(obj);
139 edje_object_signal_emit(wd->btn, emission, source);
143 _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)
145 Widget_Data *wd = elm_widget_data_get(obj);
147 edje_object_signal_callback_add(wd->btn, emission, source, func_cb, data);
151 _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))
153 Widget_Data *wd = elm_widget_data_get(obj);
154 if (!wd) return NULL;
155 return edje_object_signal_callback_del(wd->btn, emission, source,
160 _sizing_eval(Evas_Object *obj)
162 Widget_Data *wd = elm_widget_data_get(obj);
163 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
166 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
167 edje_object_size_min_restricted_calc(wd->btn, &minw, &minh, minw, minh);
168 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
169 evas_object_size_hint_min_set(obj, minw, minh);
170 evas_object_size_hint_max_set(obj, maxw, maxh);
174 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
176 Widget_Data *wd = elm_widget_data_get(data);
178 if (obj != wd->icon) return;
183 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
185 Widget_Data *wd = elm_widget_data_get(obj);
186 Evas_Object *sub = event_info;
190 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
191 evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
192 _changed_size_hints, obj);
194 edje_object_message_signal_process(wd->btn);
200 _activate(Evas_Object *obj)
202 Widget_Data *wd = elm_widget_data_get(obj);
206 ecore_timer_del(wd->timer);
209 wd->repeating = EINA_FALSE;
210 evas_object_smart_callback_call(obj, SIG_CLICKED, NULL);
211 _signal_unpressed(obj, wd->btn, NULL, NULL); /* safe guard when the theme does not emit the 'unpress' signal */
215 _activate_hook(Evas_Object *obj)
221 _signal_clicked(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
227 _autorepeat_send(void *data)
229 Widget_Data *wd = elm_widget_data_get(data);
230 if (!wd) return ECORE_CALLBACK_CANCEL;
232 evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
236 return ECORE_CALLBACK_CANCEL;
239 return ECORE_CALLBACK_RENEW;
243 _autorepeat_initial_send(void *data)
245 Widget_Data *wd = elm_widget_data_get(data);
246 if (!wd) return ECORE_CALLBACK_CANCEL;
248 if (wd->timer) ecore_timer_del(wd->timer);
249 wd->repeating = EINA_TRUE;
250 _autorepeat_send(data);
251 wd->timer = ecore_timer_add(wd->ar_interval, _autorepeat_send, data);
253 return ECORE_CALLBACK_CANCEL;
257 _signal_pressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
259 Widget_Data *wd = elm_widget_data_get(data);
262 if ((wd->autorepeat) && (!wd->repeating))
264 if (wd->ar_threshold <= 0.0)
265 _autorepeat_initial_send(data); /* call immediately */
267 wd->timer = ecore_timer_add(wd->ar_threshold, _autorepeat_initial_send, data);
272 _signal_unpressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
274 Widget_Data *wd = elm_widget_data_get(data);
279 ecore_timer_del(wd->timer);
282 wd->repeating = EINA_FALSE;
283 evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
287 * Add a new button to the parent
288 * @param parent The parent object
289 * @return The new object or NULL if it cannot be created
294 elm_button_add(Evas_Object *parent)
300 wd = ELM_NEW(Widget_Data);
301 e = evas_object_evas_get(parent);
302 obj = elm_widget_add(e);
303 ELM_SET_WIDTYPE(widtype, "button");
304 elm_widget_type_set(obj, "button");
305 elm_widget_sub_object_add(parent, obj);
306 elm_widget_on_focus_hook_set( obj, _on_focus_hook, NULL );
307 elm_widget_data_set(obj, wd);
308 elm_widget_del_hook_set(obj, _del_hook);
309 elm_widget_theme_hook_set(obj, _theme_hook);
310 elm_widget_disable_hook_set(obj, _disable_hook);
311 elm_widget_can_focus_set(obj, EINA_TRUE);
312 elm_widget_activate_hook_set(obj, _activate_hook);
313 elm_widget_event_hook_set(obj, _event_hook);
314 elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
315 elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
316 elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
318 wd->btn = edje_object_add(e);
319 _elm_theme_object_set(obj, wd->btn, "button", "base", "default");
320 edje_object_signal_callback_add(wd->btn, "elm,action,click", "",
321 _signal_clicked, obj);
322 edje_object_signal_callback_add(wd->btn, "elm,action,press", "",
323 _signal_pressed, obj);
324 edje_object_signal_callback_add(wd->btn, "elm,action,unpress", "",
325 _signal_unpressed, obj);
326 elm_widget_resize_object_set(obj, wd->btn);
328 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
332 // TODO: convert Elementary to subclassing of Evas_Smart_Class
333 // TODO: and save some bytes, making descriptions per-class and not instance!
334 evas_object_smart_callbacks_descriptions_set(obj, _signals);
339 * Set the label used in the button
341 * @param obj The button object
342 * @param label The text will be written on the button
347 elm_button_label_set(Evas_Object *obj, const char *label)
349 ELM_CHECK_WIDTYPE(obj, widtype);
350 Widget_Data *wd = elm_widget_data_get(obj);
352 eina_stringshare_replace(&wd->label, label);
354 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
356 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
357 edje_object_message_signal_process(wd->btn);
358 edje_object_part_text_set(wd->btn, "elm.text", label);
363 elm_button_label_get(const Evas_Object *obj)
365 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
366 Widget_Data *wd = elm_widget_data_get(obj);
367 if (!wd) return NULL;
372 * Set the icon used for the button
374 * Once the icon object is set, a previously set one will be deleted
375 * If you want to keep that old content object, use the
376 * elm_button_icon_unset() function.
378 * @param obj The button object
379 * @param icon The icon object for the button
384 elm_button_icon_set(Evas_Object *obj, Evas_Object *icon)
386 ELM_CHECK_WIDTYPE(obj, widtype);
387 Widget_Data *wd = elm_widget_data_get(obj);
389 if (wd->icon == icon) return;
390 if (wd->icon) evas_object_del(wd->icon);
394 elm_widget_sub_object_add(obj, icon);
395 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
396 _changed_size_hints, obj);
397 edje_object_part_swallow(wd->btn, "elm.swallow.content", icon);
398 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
399 edje_object_message_signal_process(wd->btn);
405 * Get the icon used for the button
407 * Return the icon object which is set for this widget.
409 * @param obj The button object
410 * @return The icon object that is being used
415 elm_button_icon_get(const Evas_Object *obj)
417 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
418 Widget_Data *wd = elm_widget_data_get(obj);
419 if (!wd) return NULL;
424 * Unset the icon used for the button
426 * Unparent and return the icon object which was set for this widget.
428 * @param obj The button object
429 * @return The icon object that was being used
434 elm_button_icon_unset(Evas_Object *obj)
436 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
437 Widget_Data *wd = elm_widget_data_get(obj);
438 if (!wd) return NULL;
439 if (!wd->icon) return NULL;
440 Evas_Object *icon = wd->icon;
441 elm_widget_sub_object_del(obj, wd->icon);
442 edje_object_part_unswallow(wd->btn, wd->icon);
448 * Turn on/off the autorepeat event generated when the user keeps pressing on the button
450 * @param obj The button object
451 * @param on A bool to turn on/off the event
456 elm_button_autorepeat_set(Evas_Object *obj, Eina_Bool on)
458 ELM_CHECK_WIDTYPE(obj, widtype);
459 Widget_Data *wd = elm_widget_data_get(obj);
463 ecore_timer_del(wd->timer);
467 wd->repeating = EINA_FALSE;
471 * Get if autorepeat event is on
473 * @param obj The button object
474 * @return If autorepeat is on
479 elm_button_autorepeat_get(const Evas_Object *obj)
481 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
482 Widget_Data *wd = elm_widget_data_get(obj);
483 if (!wd) return EINA_FALSE;
484 return wd->autorepeat;
488 * Set the initial timeout before the autorepeat event is generated
490 * @param obj The button object
496 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj, double t)
498 ELM_CHECK_WIDTYPE(obj, widtype);
499 Widget_Data *wd = elm_widget_data_get(obj);
501 if (wd->ar_threshold == t) return;
504 ecore_timer_del(wd->timer);
507 wd->ar_threshold = t;
511 * Get the initial timeout before the autorepeat event is generated
513 * @param obj The button object
519 elm_button_autorepeat_initial_timeout_get(const Evas_Object *obj)
521 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
522 Widget_Data *wd = elm_widget_data_get(obj);
524 return wd->ar_threshold;
528 * Set the interval between each generated autorepeat event
530 * @param obj The button object
536 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj, double t)
538 ELM_CHECK_WIDTYPE(obj, widtype);
539 Widget_Data *wd = elm_widget_data_get(obj);
541 if (wd->ar_interval == t) return;
544 if ((wd->repeating) && (wd->timer)) ecore_timer_interval_set(wd->timer, t);
548 * Get the interval between each generated autorepeat event
550 * @param obj The button object
556 elm_button_autorepeat_gap_timeout_get(const Evas_Object *obj)
558 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
559 Widget_Data *wd = elm_widget_data_get(obj);
561 return wd->ar_interval;