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;
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);
172 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
174 Widget_Data *wd = elm_widget_data_get(data);
176 if (obj != wd->icon) return;
181 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
183 Widget_Data *wd = elm_widget_data_get(obj);
184 Evas_Object *sub = event_info;
188 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
189 evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
190 _changed_size_hints, obj);
192 edje_object_message_signal_process(wd->btn);
198 _signal_clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
200 Widget_Data *wd = elm_widget_data_get(data);
204 ecore_timer_del(wd->timer);
207 wd->repeating = EINA_FALSE;
208 evas_object_smart_callback_call(data, SIG_CLICKED, NULL);
212 _autorepeat_send(void *data)
214 Widget_Data *wd = elm_widget_data_get(data);
215 if (!wd) return ECORE_CALLBACK_CANCEL;
217 evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
221 return ECORE_CALLBACK_CANCEL;
223 return ECORE_CALLBACK_RENEW;
227 _autorepeat_initial_send(void *data)
229 Widget_Data *wd = elm_widget_data_get(data);
230 if (!wd) return ECORE_CALLBACK_CANCEL;
232 if (wd->timer) ecore_timer_del(wd->timer);
233 wd->repeating = EINA_TRUE;
234 _autorepeat_send(data);
235 wd->timer = ecore_timer_add(wd->ar_interval, _autorepeat_send, data);
237 return ECORE_CALLBACK_CANCEL;
241 _signal_pressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
243 Widget_Data *wd = elm_widget_data_get(data);
246 if (wd->statelabel[HIGHLIGHTED])
248 _set_label(data, wd->statelabel[HIGHLIGHTED]);
250 if (wd->autorepeat && !wd->repeating)
252 if (wd->ar_threshold <= 0.0)
253 _autorepeat_initial_send(data); /* call immediately */
255 wd->timer = ecore_timer_add(wd->ar_threshold, _autorepeat_initial_send, data);
260 _signal_default_text_set(void *data, Evas_Object *obj, const char *emission, const char *source)
262 Widget_Data *wd = elm_widget_data_get(data);
264 if (wd->statelabel[DEFAULT])
265 _set_label(data, wd->statelabel[DEFAULT]);
268 _set_label(data, wd->label);
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);
278 if (wd->statelabel[DEFAULT])
279 _set_label(data, wd->statelabel[DEFAULT]);
282 _set_label(data, wd->label);
287 ecore_timer_del(wd->timer);
290 wd->repeating = EINA_FALSE;
291 evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
295 * Add a new button to the parent
296 * @param[in] parent The parent object
297 * @return The new object or NULL if it cannot be created
302 elm_button_add(Evas_Object *parent)
308 wd = ELM_NEW(Widget_Data);
309 e = evas_object_evas_get(parent);
310 obj = elm_widget_add(e);
311 ELM_SET_WIDTYPE(widtype, "button");
312 elm_widget_type_set(obj, "button");
313 elm_widget_can_focus_set(obj, EINA_TRUE);
314 elm_widget_sub_object_add(parent, obj);
315 elm_widget_on_focus_hook_set( obj, _on_focus_hook, NULL );
316 elm_widget_data_set(obj, wd);
317 elm_widget_del_hook_set(obj, _del_hook);
318 elm_widget_theme_hook_set(obj, _theme_hook);
319 elm_widget_disable_hook_set(obj, _disable_hook);
321 wd->btn = edje_object_add(e);
322 _elm_theme_object_set(obj, wd->btn, "button", "base", "default");
323 wd->statetype[DEFAULT] = 0;
324 wd->statetype[HIGHLIGHTED] = 0;
325 wd->statetype[FOCUSED] = 0;
326 wd->statetype[DISABLED] = 0;
327 wd->statelabel[DEFAULT] = 0;
328 wd->statelabel[HIGHLIGHTED] = 0;
329 wd->statelabel[FOCUSED] = 0;
330 wd->statelabel[DISABLED] = 0;
331 edje_object_signal_callback_add(wd->btn, "elm,action,click", "",
332 _signal_clicked, obj);
333 edje_object_signal_callback_add(wd->btn, "elm,action,press", "",
334 _signal_pressed, obj);
335 edje_object_signal_callback_add(wd->btn, "elm,action,unpress", "",
336 _signal_unpressed, obj);
337 edje_object_signal_callback_add(wd->btn, "elm,action,default,text,set", "",
338 _signal_default_text_set, obj);
339 elm_widget_resize_object_set(obj, wd->btn);
341 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
345 // TODO: convert Elementary to subclassing of Evas_Smart_Class
346 // TODO: and save some bytes, making descriptions per-class and not instance!
347 evas_object_smart_callbacks_descriptions_set(obj, _signals);
352 * Set the label used in the button
354 * @param[in] obj The button object
355 * @param[in] label The text will be written on the button
360 elm_button_label_set(Evas_Object *obj, const char *label)
362 ELM_CHECK_WIDTYPE(obj, widtype);
363 Widget_Data *wd = elm_widget_data_get(obj);
365 eina_stringshare_replace(&wd->label, label);
367 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
369 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
370 edje_object_message_signal_process(wd->btn);
371 edje_object_part_text_set(wd->btn, "elm.text", label);
376 _set_label(Evas_Object *obj, const char *label)
378 Widget_Data *wd = elm_widget_data_get(obj);
380 edje_object_part_text_set(wd->btn, "elm.text", label);
384 * Set the label for each state of button
386 * @param[in] obj The button object
387 * @param[in] label The text will be written on the button
388 * @param[in] state The state of button
393 elm_button_label_set_for_state(Evas_Object *obj, const char *label, UIControlState state)
395 Widget_Data *wd = elm_widget_data_get(obj);
398 if (label == NULL) return;
400 if (state == UIControlStateDefault)
402 wd->statetype[DEFAULT] = UIControlStateDefault;
403 eina_stringshare_replace(&wd->statelabel[DEFAULT], label);
406 if (state == UIControlStateHighlighted)
408 wd->statetype[HIGHLIGHTED] = UIControlStateHighlighted;
409 eina_stringshare_replace(&wd->statelabel[HIGHLIGHTED], label);
412 if (state == UIControlStateFocused)
414 wd->statetype[FOCUSED] = UIControlStateFocused;
415 eina_stringshare_replace(&wd->statelabel[FOCUSED], label);
418 if (state == UIControlStateDisabled)
420 wd->statetype[DISABLED] = UIControlStateDisabled;
421 eina_stringshare_replace(&wd->statelabel[DISABLED], label);
427 * Get the label of button
429 * @param[in] obj The button object
430 * @return The title of button
435 elm_button_label_get(const Evas_Object *obj)
437 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
438 Widget_Data *wd = elm_widget_data_get(obj);
439 if (!wd) return NULL;
443 * Get the label of button for each state
445 * @param[in] obj The button object
446 * @param[in] state The state of button
447 * @return The title of button for state
452 elm_button_label_get_for_state(const Evas_Object *obj, UIControlState state)
454 Widget_Data *wd = elm_widget_data_get(obj);
455 if (!wd) return NULL;
457 if (state == UIControlStateDefault)
458 return wd->statelabel[DEFAULT];
459 else if (state == UIControlStateHighlighted)
460 return wd->statelabel[HIGHLIGHTED];
461 else if (state == UIControlStateFocused)
462 return wd->statelabel[FOCUSED];
463 else if (state == UIControlStateDisabled)
464 return wd->statelabel[DISABLED];
470 * Set the icon used for the button
472 * Once the icon object is set, a previously set one will be deleted
474 * @param[in] obj The button object
475 * @param[in] icon The image for the button
480 elm_button_icon_set(Evas_Object *obj, Evas_Object *icon)
482 ELM_CHECK_WIDTYPE(obj, widtype);
483 Widget_Data *wd = elm_widget_data_get(obj);
485 if (wd->icon == icon) return;
486 if (wd->icon) evas_object_del(wd->icon);
490 elm_widget_sub_object_add(obj, icon);
491 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
492 _changed_size_hints, obj);
493 edje_object_part_swallow(wd->btn, "elm.swallow.content", icon);
494 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
495 edje_object_message_signal_process(wd->btn);
501 * Get the icon used for the button
503 * @param[in] obj The button object
504 * @return The image for the button
509 elm_button_icon_get(const Evas_Object *obj)
511 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
512 Widget_Data *wd = elm_widget_data_get(obj);
513 if (!wd) return NULL;
518 * Turn on/off the autorepeat event generated when the user keeps pressing on the button
520 * @param[in] obj The button object
521 * @param[in] on A bool to turn on/off the event
526 elm_button_autorepeat_set(Evas_Object *obj, Eina_Bool on)
528 ELM_CHECK_WIDTYPE(obj, widtype);
529 Widget_Data *wd = elm_widget_data_get(obj);
533 ecore_timer_del(wd->timer);
537 wd->repeating = EINA_FALSE;
541 * Set the initial timeout before the autorepeat event is generated
543 * @param[in] obj The button object
544 * @param[in] t Timeout
549 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj, double t)
551 ELM_CHECK_WIDTYPE(obj, widtype);
552 Widget_Data *wd = elm_widget_data_get(obj);
554 if (wd->ar_threshold == t) return;
557 ecore_timer_del(wd->timer);
560 wd->ar_threshold = t;
564 * Set the interval between each generated autorepeat event
566 * @param[in] obj The button object
567 * @param[in] t Interval
572 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj, double t)
574 ELM_CHECK_WIDTYPE(obj, widtype);
575 Widget_Data *wd = elm_widget_data_get(obj);
577 if (wd->ar_interval == t) return;
580 if (wd->repeating && wd->timer) ecore_timer_interval_set(wd->timer, t);