1 #include <Elementary.h>
5 * @defgroup Button Button
8 * This is a push-button. Press it and run some function. It can contain
9 * a simple label and icon object.
12 typedef struct _Widget_Data Widget_Data;
24 Evas_Object *btn, *icon;
31 const char *statelabel[4];
35 static const char *widtype = NULL;
36 static void _del_hook(Evas_Object *obj);
37 static void _theme_hook(Evas_Object *obj);
38 static void _disable_hook(Evas_Object *obj);
39 static void _sizing_eval(Evas_Object *obj);
40 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
41 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
42 static void _signal_clicked(void *data, Evas_Object *obj, const char *emission, const char *source);
43 static void _signal_pressed(void *data, Evas_Object *obj, const char *emission, const char *source);
44 static void _signal_unpressed(void *data, Evas_Object *obj, const char *emission, const char *source);
45 static void _on_focus_hook(void *data, Evas_Object *obj);
46 static void _set_label(Evas_Object *obj, const char *label);
47 static void _signal_default_text_set(void *data, Evas_Object *obj, const char *emission, const char *source);
49 static const char SIG_CLICKED[] = "clicked";
50 static const char SIG_REPEATED[] = "repeated";
51 static const char SIG_UNPRESSED[] = "unpressed";
52 static const Evas_Smart_Cb_Description _signals[] = {
60 _del_hook(Evas_Object *obj)
62 Widget_Data *wd = elm_widget_data_get(obj);
64 if (wd->label) eina_stringshare_del(wd->label);
65 if (wd->statelabel[DEFAULT]) eina_stringshare_del(wd->statelabel[DEFAULT]);
66 if (wd->statelabel[HIGHLIGHTED]) eina_stringshare_del(wd->statelabel[HIGHLIGHTED]);
67 if (wd->statelabel[FOCUSED]) eina_stringshare_del(wd->statelabel[FOCUSED]);
68 if (wd->statelabel[DISABLED]) eina_stringshare_del(wd->statelabel[DISABLED]);
73 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
75 Widget_Data *wd = elm_widget_data_get(obj);
77 if (elm_widget_focus_get(obj))
79 if(wd->statelabel[FOCUSED])
81 _set_label(obj, wd->statelabel[FOCUSED]);
83 edje_object_signal_emit(wd->btn, "elm,action,focus", "elm");
84 evas_object_focus_set(wd->btn, EINA_TRUE);
88 if(wd->statelabel[DEFAULT])
89 _set_label(obj, wd->statelabel[DEFAULT]);
92 _set_label(obj, wd->label);
94 edje_object_signal_emit(wd->btn, "elm,action,unfocus", "elm");
95 evas_object_focus_set(wd->btn, EINA_FALSE);
100 _theme_hook(Evas_Object *obj)
102 Widget_Data *wd = elm_widget_data_get(obj);
104 _elm_theme_object_set(obj, wd->btn, "button", "base", elm_widget_style_get(obj));
107 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");
115 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
117 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
119 edje_object_part_text_set(wd->btn, "elm.text", wd->label);
120 if (elm_object_disabled_get(obj))
121 edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
122 edje_object_message_signal_process(wd->btn);
123 edje_object_scale_set(wd->btn, elm_widget_scale_get(obj) * _elm_config->scale);
128 _disable_hook(Evas_Object *obj)
130 Widget_Data *wd = elm_widget_data_get(obj);
132 if (elm_widget_disabled_get(obj))
134 if(wd->statelabel[DISABLED] )
136 _set_label(obj, wd->statelabel[DISABLED]);
138 edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
142 if(wd->statelabel[DEFAULT])
143 _set_label(obj, wd->statelabel[DEFAULT]);
146 _set_label(obj, wd->label);
148 edje_object_signal_emit(wd->btn, "elm,state,enabled", "elm");
153 _sizing_eval(Evas_Object *obj)
155 Widget_Data *wd = elm_widget_data_get(obj);
156 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
160 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
161 edje_object_size_min_restricted_calc(wd->btn, &minw, &minh, minw, minh);
162 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
163 //Commenting to sync with open source and able to resize based on text change
164 evas_object_size_hint_min_get(obj, &w, &h);
165 //if (w > minw) minw = w;
166 if (h > minh) minh = h;
168 evas_object_size_hint_min_set(obj, minw, minh);
169 //evas_object_size_hint_max_set(obj, maxw, maxh);
173 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
175 Widget_Data *wd = elm_widget_data_get(data);
177 if (obj != wd->icon) return;
182 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
184 Widget_Data *wd = elm_widget_data_get(obj);
185 Evas_Object *sub = event_info;
189 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
190 evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
191 _changed_size_hints, obj);
193 edje_object_message_signal_process(wd->btn);
199 _signal_clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
201 Widget_Data *wd = elm_widget_data_get(data);
205 ecore_timer_del(wd->timer);
208 wd->repeating = EINA_FALSE;
209 evas_object_smart_callback_call(data, SIG_CLICKED, NULL);
211 _signal_unpressed(data, obj, emission, source); /* safe guard when the theme does not emit the 'unpress' signal */
216 _autorepeat_send(void *data)
218 Widget_Data *wd = elm_widget_data_get(data);
219 if (!wd) return ECORE_CALLBACK_CANCEL;
221 evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
225 return ECORE_CALLBACK_CANCEL;
228 return ECORE_CALLBACK_RENEW;
232 _autorepeat_initial_send(void *data)
234 Widget_Data *wd = elm_widget_data_get(data);
235 if (!wd) return ECORE_CALLBACK_CANCEL;
237 if (wd->timer) ecore_timer_del(wd->timer);
238 wd->repeating = EINA_TRUE;
239 _autorepeat_send(data);
240 wd->timer = ecore_timer_add(wd->ar_interval, _autorepeat_send, data);
242 return ECORE_CALLBACK_CANCEL;
246 _signal_pressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
248 Widget_Data *wd = elm_widget_data_get(data);
251 if(wd->statelabel[HIGHLIGHTED])
253 _set_label(data, wd->statelabel[HIGHLIGHTED]);
255 if (wd->autorepeat && !wd->repeating)
257 if (wd->ar_threshold <= 0.0)
258 _autorepeat_initial_send(data); /* call immediately */
260 wd->timer = ecore_timer_add(wd->ar_threshold, _autorepeat_initial_send, data);
265 _signal_default_text_set(void *data, Evas_Object *obj, const char *emission, const char *source)
267 Widget_Data *wd = elm_widget_data_get(data);
269 if(wd->statelabel[DEFAULT])
270 _set_label(data, wd->statelabel[DEFAULT]);
273 _set_label(data, wd->label);
279 _signal_unpressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
281 Widget_Data *wd = elm_widget_data_get(data);
283 if(wd->statelabel[DEFAULT])
284 _set_label(data, wd->statelabel[DEFAULT]);
287 _set_label(data, wd->label);
292 ecore_timer_del(wd->timer);
295 wd->repeating = EINA_FALSE;
296 evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
300 * Add a new button to the parent
301 * @param[in] parent The parent object
302 * @return The new object or NULL if it cannot be created
307 elm_button_add(Evas_Object *parent)
313 wd = ELM_NEW(Widget_Data);
314 e = evas_object_evas_get(parent);
315 obj = elm_widget_add(e);
316 ELM_SET_WIDTYPE(widtype, "button");
317 elm_widget_type_set(obj, "button");
318 elm_widget_can_focus_set(obj, EINA_TRUE);
319 elm_widget_sub_object_add(parent, obj);
320 elm_widget_on_focus_hook_set( obj, _on_focus_hook, NULL );
321 elm_widget_data_set(obj, wd);
322 elm_widget_del_hook_set(obj, _del_hook);
323 elm_widget_theme_hook_set(obj, _theme_hook);
324 elm_widget_disable_hook_set(obj, _disable_hook);
326 wd->btn = edje_object_add(e);
327 _elm_theme_object_set(obj, wd->btn, "button", "base", "default");
328 wd->statetype[DEFAULT] = 0;
329 wd->statetype[HIGHLIGHTED] = 0;
330 wd->statetype[FOCUSED] = 0;
331 wd->statetype[DISABLED] = 0;
332 wd->statelabel[DEFAULT] = 0;
333 wd->statelabel[HIGHLIGHTED] = 0;
334 wd->statelabel[FOCUSED] = 0;
335 wd->statelabel[DISABLED] = 0;
336 edje_object_signal_callback_add(wd->btn, "elm,action,click", "",
337 _signal_clicked, obj);
338 edje_object_signal_callback_add(wd->btn, "elm,action,press", "",
339 _signal_pressed, obj);
340 edje_object_signal_callback_add(wd->btn, "elm,action,unpress", "",
341 _signal_unpressed, obj);
342 edje_object_signal_callback_add(wd->btn, "elm,action,default,text,set", "",
343 _signal_default_text_set, obj);
344 elm_widget_resize_object_set(obj, wd->btn);
346 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
350 // TODO: convert Elementary to subclassing of Evas_Smart_Class
351 // TODO: and save some bytes, making descriptions per-class and not instance!
352 evas_object_smart_callbacks_descriptions_set(obj, _signals);
357 * Set the label used in the button
359 * @param[in] obj The button object
360 * @param[in] label The text will be written on the button
365 elm_button_label_set(Evas_Object *obj, const char *label)
367 ELM_CHECK_WIDTYPE(obj, widtype);
368 Widget_Data *wd = elm_widget_data_get(obj);
370 eina_stringshare_replace(&wd->label, label);
372 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
374 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
375 edje_object_message_signal_process(wd->btn);
376 edje_object_part_text_set(wd->btn, "elm.text", label);
381 _set_label(Evas_Object *obj, const char *label)
383 Widget_Data *wd = elm_widget_data_get(obj);
385 edje_object_message_signal_process(wd->btn);
386 edje_object_part_text_set(wd->btn, "elm.text", label);
390 * Set the label for each state of button
392 * @param[in] obj The button object
393 * @param[in] label The text will be written on the button
394 * @param[in] state The state of button
399 elm_button_label_set_for_state(Evas_Object *obj, const char *label, UIControlState state)
401 Widget_Data *wd = elm_widget_data_get(obj);
404 if(label == NULL) return;
406 if(state == UIControlStateDefault)
408 wd->statetype[DEFAULT] = UIControlStateDefault;
409 eina_stringshare_replace(&wd->statelabel[DEFAULT], label);
412 if(state == UIControlStateHighlighted)
414 wd->statetype[HIGHLIGHTED] = UIControlStateHighlighted;
415 eina_stringshare_replace(&wd->statelabel[HIGHLIGHTED], label);
418 if(state == UIControlStateFocused)
420 wd->statetype[FOCUSED] = UIControlStateFocused;
421 eina_stringshare_replace(&wd->statelabel[FOCUSED], label);
424 if(state == UIControlStateDisabled)
426 wd->statetype[DISABLED] = UIControlStateDisabled;
427 eina_stringshare_replace(&wd->statelabel[DISABLED], label);
433 * Get the label of button
435 * @param[in] obj The button object
436 * @return The title of button
441 elm_button_label_get(const Evas_Object *obj)
443 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
444 Widget_Data *wd = elm_widget_data_get(obj);
445 if (!wd) return NULL;
449 * Get the label of button for each state
451 * @param[in] obj The button object
452 * @param[in] state The state of button
453 * @return The title of button for state
458 elm_button_label_get_for_state(const Evas_Object *obj, UIControlState state)
460 Widget_Data *wd = elm_widget_data_get(obj);
461 if (!wd) return NULL;
463 if(state == UIControlStateDefault)
464 return wd->statelabel[DEFAULT];
465 else if(state == UIControlStateHighlighted)
466 return wd->statelabel[HIGHLIGHTED];
467 else if(state == UIControlStateFocused)
468 return wd->statelabel[FOCUSED];
469 else if(state == UIControlStateDisabled)
470 return wd->statelabel[DISABLED];
476 * Set the icon used for the button
478 * Once the icon object is set, a previously set one will be deleted
480 * @param[in] obj The button object
481 * @param[in] icon The image for the button
486 elm_button_icon_set(Evas_Object *obj, Evas_Object *icon)
488 ELM_CHECK_WIDTYPE(obj, widtype);
489 Widget_Data *wd = elm_widget_data_get(obj);
491 if (wd->icon == icon) return;
492 if (wd->icon) evas_object_del(wd->icon);
496 elm_widget_sub_object_add(obj, icon);
497 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
498 _changed_size_hints, obj);
499 edje_object_part_swallow(wd->btn, "elm.swallow.content", icon);
500 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
501 edje_object_message_signal_process(wd->btn);
507 * Get the icon used for the button
509 * @param[in] obj The button object
510 * @return The image for the button
515 elm_button_icon_get(const Evas_Object *obj)
517 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
518 Widget_Data *wd = elm_widget_data_get(obj);
519 if (!wd) return NULL;
524 * Turn on/off the autorepeat event generated when the user keeps pressing on the button
526 * @param[in] obj The button object
527 * @param[in] on A bool to turn on/off the event
532 elm_button_autorepeat_set(Evas_Object *obj, Eina_Bool on)
534 ELM_CHECK_WIDTYPE(obj, widtype);
535 Widget_Data *wd = elm_widget_data_get(obj);
539 ecore_timer_del(wd->timer);
543 wd->repeating = EINA_FALSE;
547 * Set the initial timeout before the autorepeat event is generated
549 * @param[in] obj The button object
550 * @param[in] t Timeout
555 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj, double t)
557 ELM_CHECK_WIDTYPE(obj, widtype);
558 Widget_Data *wd = elm_widget_data_get(obj);
560 if (wd->ar_threshold == t) return;
563 ecore_timer_del(wd->timer);
566 wd->ar_threshold = t;
570 * Set the interval between each generated autorepeat event
572 * @param[in] obj The button object
573 * @param[in] t Interval
578 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj, double t)
580 ELM_CHECK_WIDTYPE(obj, widtype);
581 Widget_Data *wd = elm_widget_data_get(obj);
583 if (wd->ar_interval == t) return;
586 if (wd->repeating && wd->timer) ecore_timer_interval_set(wd->timer, t);