3 * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
5 #include <Elementary.h>
9 * @defgroup Button Button
11 * This is a push-button. Press it and run some function. It can contain
12 * a simple label and icon object.
15 typedef struct _Widget_Data Widget_Data;
19 Evas_Object *btn, *icon;
29 static const char *widtype = NULL;
30 static void _del_hook(Evas_Object *obj);
31 static void _theme_hook(Evas_Object *obj);
32 static void _disable_hook(Evas_Object *obj);
33 static void _sizing_eval(Evas_Object *obj);
34 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
35 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
36 static void _signal_clicked(void *data, Evas_Object *obj, const char *emission, const char *source);
37 static void _signal_pressed(void *data, Evas_Object *obj, const char *emission, const char *source);
38 static void _signal_unpressed(void *data, Evas_Object *obj, const char *emission, const char *source);
39 static void _on_focus_hook(void *data, Evas_Object *obj);
41 static const char SIG_CLICKED[] = "clicked";
42 static const char SIG_REPEATED[] = "repeated";
43 static const char SIG_UNPRESSED[] = "unpressed";
44 static const Evas_Smart_Cb_Description _signals[] = {
52 _del_hook(Evas_Object *obj)
54 Widget_Data *wd = elm_widget_data_get(obj);
56 if (wd->label) eina_stringshare_del(wd->label);
61 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
63 Widget_Data *wd = elm_widget_data_get(obj);
65 if (elm_widget_focus_get(obj))
67 edje_object_signal_emit(wd->btn, "elm,action,focus", "elm");
68 evas_object_focus_set(wd->btn, 1);
72 edje_object_signal_emit(wd->btn, "elm,action,unfocus", "elm");
73 evas_object_focus_set(wd->btn, 0);
78 _theme_hook(Evas_Object *obj)
80 Widget_Data *wd = elm_widget_data_get(obj);
82 _elm_theme_set(wd->btn, "button", "base", elm_widget_style_get(obj));
84 edje_object_part_swallow(wd->btn, "elm.swallow.content", wd->icon);
86 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
88 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
90 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
92 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
93 edje_object_part_text_set(wd->btn, "elm.text", wd->label);
94 edje_object_message_signal_process(wd->btn);
95 edje_object_scale_set(wd->btn, elm_widget_scale_get(obj) * _elm_config->scale);
100 _disable_hook(Evas_Object *obj)
102 Widget_Data *wd = elm_widget_data_get(obj);
104 if (elm_widget_disabled_get(obj))
105 edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
107 edje_object_signal_emit(wd->btn, "elm,state,enabled", "elm");
111 _sizing_eval(Evas_Object *obj)
113 Widget_Data *wd = elm_widget_data_get(obj);
114 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
117 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
118 edje_object_size_min_restricted_calc(wd->btn, &minw, &minh, minw, minh);
119 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
120 evas_object_size_hint_min_set(obj, minw, minh);
121 evas_object_size_hint_max_set(obj, maxw, maxh);
125 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
127 Widget_Data *wd = elm_widget_data_get(data);
129 if (obj != wd->icon) return;
134 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
136 Widget_Data *wd = elm_widget_data_get(obj);
137 Evas_Object *sub = event_info;
141 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
142 evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
143 _changed_size_hints, obj);
145 edje_object_message_signal_process(wd->btn);
151 _signal_clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
153 Widget_Data *wd = elm_widget_data_get(data);
157 ecore_timer_del(wd->timer);
160 wd->repeating = EINA_FALSE;
161 evas_object_smart_callback_call(data, SIG_CLICKED, NULL);
162 _signal_unpressed(data, obj, emission, source); /* safe guard when the theme does not emit the 'unpress' signal */
166 _autorepeat_send(void *data)
168 Widget_Data *wd = elm_widget_data_get(data);
169 if (!wd) return ECORE_CALLBACK_CANCEL;
171 evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
175 return ECORE_CALLBACK_CANCEL;
178 return ECORE_CALLBACK_RENEW;
182 _autorepeat_initial_send(void *data)
184 Widget_Data *wd = elm_widget_data_get(data);
185 if (!wd) return ECORE_CALLBACK_CANCEL;
187 if (wd->timer) ecore_timer_del(wd->timer);
188 wd->repeating = EINA_TRUE;
189 _autorepeat_send(data);
190 wd->timer = ecore_timer_add(wd->ar_interval, _autorepeat_send, data);
192 return ECORE_CALLBACK_CANCEL;
196 _signal_pressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
198 Widget_Data *wd = elm_widget_data_get(data);
201 if (wd->autorepeat && !wd->repeating)
203 if (wd->ar_threshold <= 0.0)
204 _autorepeat_initial_send(data); /* call immediately */
206 wd->timer = ecore_timer_add(wd->ar_threshold, _autorepeat_initial_send, data);
211 _signal_unpressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
213 Widget_Data *wd = elm_widget_data_get(data);
218 ecore_timer_del(wd->timer);
221 wd->repeating = EINA_FALSE;
222 evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
226 * Add a new button to the parent
227 * @param parent The parent object
228 * @return The new object or NULL if it cannot be created
233 elm_button_add(Evas_Object *parent)
239 wd = ELM_NEW(Widget_Data);
240 e = evas_object_evas_get(parent);
241 obj = elm_widget_add(e);
242 ELM_SET_WIDTYPE(widtype, "button");
243 elm_widget_type_set(obj, "button");
244 elm_widget_sub_object_add(parent, obj);
245 elm_widget_on_focus_hook_set( obj, _on_focus_hook, NULL );
246 elm_widget_data_set(obj, wd);
247 elm_widget_del_hook_set(obj, _del_hook);
248 elm_widget_theme_hook_set(obj, _theme_hook);
249 elm_widget_disable_hook_set(obj, _disable_hook);
250 elm_widget_can_focus_set(obj, 1 );
252 wd->btn = edje_object_add(e);
253 _elm_theme_set(wd->btn, "button", "base", "default");
254 edje_object_signal_callback_add(wd->btn, "elm,action,click", "",
255 _signal_clicked, obj);
256 edje_object_signal_callback_add(wd->btn, "elm,action,press", "",
257 _signal_pressed, obj);
258 edje_object_signal_callback_add(wd->btn, "elm,action,unpress", "",
259 _signal_unpressed, obj);
260 elm_widget_resize_object_set(obj, wd->btn);
262 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
266 // TODO: convert Elementary to subclassing of Evas_Smart_Class
267 // TODO: and save some bytes, making descriptions per-class and not instance!
268 evas_object_smart_callbacks_descriptions_set(obj, _signals);
273 * Set the label used in the button
275 * @param obj The button object
276 * @param label The text will be written on the button
281 elm_button_label_set(Evas_Object *obj, const char *label)
283 ELM_CHECK_WIDTYPE(obj, widtype);
284 Widget_Data *wd = elm_widget_data_get(obj);
286 eina_stringshare_replace(&wd->label, label);
288 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
290 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
291 edje_object_message_signal_process(wd->btn);
292 edje_object_part_text_set(wd->btn, "elm.text", label);
297 elm_button_label_get(const Evas_Object *obj)
299 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
300 Widget_Data *wd = elm_widget_data_get(obj);
301 if (!wd) return NULL;
306 * Set the icon used for the button
308 * @param obj The button object
309 * @param icon The image for the button
314 elm_button_icon_set(Evas_Object *obj, Evas_Object *icon)
316 ELM_CHECK_WIDTYPE(obj, widtype);
317 Widget_Data *wd = elm_widget_data_get(obj);
319 if ((wd->icon != icon) && (wd->icon))
320 elm_widget_sub_object_del(obj, wd->icon);
321 if ((icon) && (wd->icon != icon))
324 elm_widget_sub_object_add(obj, icon);
325 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
326 _changed_size_hints, obj);
327 edje_object_part_swallow(wd->btn, "elm.swallow.content", icon);
328 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
329 edje_object_message_signal_process(wd->btn);
337 * Get the icon used for the button
339 * @param obj The button object
340 * @return The image for the button
345 elm_button_icon_get(const Evas_Object *obj)
347 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
348 Widget_Data *wd = elm_widget_data_get(obj);
349 if (!wd) return NULL;
354 * Turn on/off the autorepeat event generated when the user keeps pressing on the button
356 * @param obj The button object
357 * @param on A bool to turn on/off the event
362 elm_button_autorepeat_set(Evas_Object *obj, Eina_Bool on)
364 ELM_CHECK_WIDTYPE(obj, widtype);
365 Widget_Data *wd = elm_widget_data_get(obj);
369 ecore_timer_del(wd->timer);
373 wd->repeating = EINA_FALSE;
377 * Set the initial timeout before the autorepeat event is generated
379 * @param obj The button object
385 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj, double t)
387 ELM_CHECK_WIDTYPE(obj, widtype);
388 Widget_Data *wd = elm_widget_data_get(obj);
390 if (wd->ar_threshold == t) return;
393 ecore_timer_del(wd->timer);
396 wd->ar_threshold = t;
400 * Set the interval between each generated autorepeat event
402 * @param obj The button object
408 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj, double t)
410 ELM_CHECK_WIDTYPE(obj, widtype);
411 Widget_Data *wd = elm_widget_data_get(obj);
413 if (wd->ar_interval == t) return;
416 if (wd->repeating && wd->timer) ecore_timer_interval_set(wd->timer, t);