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;
28 static const char *widtype = NULL;
29 static void _del_hook(Evas_Object *obj);
30 static void _theme_hook(Evas_Object *obj);
31 static void _disable_hook(Evas_Object *obj);
32 static void _sizing_eval(Evas_Object *obj);
33 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
34 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
35 static void _signal_clicked(void *data, Evas_Object *obj, const char *emission, const char *source);
36 static void _signal_pressed(void *data, Evas_Object *obj, const char *emission, const char *source);
37 static void _signal_unpressed(void *data, Evas_Object *obj, const char *emission, const char *source);
38 static void _on_focus_hook(void *data, Evas_Object *obj);
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 _del_hook(Evas_Object *obj)
53 Widget_Data *wd = elm_widget_data_get(obj);
55 if (wd->label) eina_stringshare_del(wd->label);
60 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
62 Widget_Data *wd = elm_widget_data_get(obj);
64 if (elm_widget_focus_get(obj))
66 edje_object_signal_emit(wd->btn, "elm,action,focus", "elm");
67 evas_object_focus_set(wd->btn, 1);
71 edje_object_signal_emit(wd->btn, "elm,action,unfocus", "elm");
72 evas_object_focus_set(wd->btn, 0);
77 _theme_hook(Evas_Object *obj)
79 Widget_Data *wd = elm_widget_data_get(obj);
81 _elm_theme_object_set(obj, wd->btn, "button", "base", elm_widget_style_get(obj));
83 edje_object_part_swallow(wd->btn, "elm.swallow.content", wd->icon);
85 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
87 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
89 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
91 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
92 edje_object_part_text_set(wd->btn, "elm.text", wd->label);
93 edje_object_message_signal_process(wd->btn);
94 edje_object_scale_set(wd->btn, elm_widget_scale_get(obj) * _elm_config->scale);
99 _disable_hook(Evas_Object *obj)
101 Widget_Data *wd = elm_widget_data_get(obj);
103 if (elm_widget_disabled_get(obj))
104 edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
106 edje_object_signal_emit(wd->btn, "elm,state,enabled", "elm");
110 _sizing_eval(Evas_Object *obj)
112 Widget_Data *wd = elm_widget_data_get(obj);
113 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
116 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
117 edje_object_size_min_restricted_calc(wd->btn, &minw, &minh, minw, minh);
118 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
119 evas_object_size_hint_min_set(obj, minw, minh);
120 evas_object_size_hint_max_set(obj, maxw, maxh);
124 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
126 Widget_Data *wd = elm_widget_data_get(data);
128 if (obj != wd->icon) return;
133 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
135 Widget_Data *wd = elm_widget_data_get(obj);
136 Evas_Object *sub = event_info;
140 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
141 evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
142 _changed_size_hints, obj);
144 edje_object_message_signal_process(wd->btn);
150 _signal_clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
152 Widget_Data *wd = elm_widget_data_get(data);
156 ecore_timer_del(wd->timer);
159 wd->repeating = EINA_FALSE;
160 evas_object_smart_callback_call(data, SIG_CLICKED, NULL);
161 _signal_unpressed(data, obj, emission, source); /* safe guard when the theme does not emit the 'unpress' signal */
165 _autorepeat_send(void *data)
167 Widget_Data *wd = elm_widget_data_get(data);
168 if (!wd) return ECORE_CALLBACK_CANCEL;
170 evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
174 return ECORE_CALLBACK_CANCEL;
177 return ECORE_CALLBACK_RENEW;
181 _autorepeat_initial_send(void *data)
183 Widget_Data *wd = elm_widget_data_get(data);
184 if (!wd) return ECORE_CALLBACK_CANCEL;
186 if (wd->timer) ecore_timer_del(wd->timer);
187 wd->repeating = EINA_TRUE;
188 _autorepeat_send(data);
189 wd->timer = ecore_timer_add(wd->ar_interval, _autorepeat_send, data);
191 return ECORE_CALLBACK_CANCEL;
195 _signal_pressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
197 Widget_Data *wd = elm_widget_data_get(data);
200 if (wd->autorepeat && !wd->repeating)
202 if (wd->ar_threshold <= 0.0)
203 _autorepeat_initial_send(data); /* call immediately */
205 wd->timer = ecore_timer_add(wd->ar_threshold, _autorepeat_initial_send, data);
210 _signal_unpressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
212 Widget_Data *wd = elm_widget_data_get(data);
217 ecore_timer_del(wd->timer);
220 wd->repeating = EINA_FALSE;
221 evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
225 * Add a new button to the parent
226 * @param parent The parent object
227 * @return The new object or NULL if it cannot be created
232 elm_button_add(Evas_Object *parent)
238 wd = ELM_NEW(Widget_Data);
239 e = evas_object_evas_get(parent);
240 obj = elm_widget_add(e);
241 ELM_SET_WIDTYPE(widtype, "button");
242 elm_widget_type_set(obj, "button");
243 elm_widget_sub_object_add(parent, obj);
244 elm_widget_on_focus_hook_set( obj, _on_focus_hook, NULL );
245 elm_widget_data_set(obj, wd);
246 elm_widget_del_hook_set(obj, _del_hook);
247 elm_widget_theme_hook_set(obj, _theme_hook);
248 elm_widget_disable_hook_set(obj, _disable_hook);
249 elm_widget_can_focus_set(obj, 1 );
251 wd->btn = edje_object_add(e);
252 _elm_theme_object_set(obj, wd->btn, "button", "base", "default");
253 edje_object_signal_callback_add(wd->btn, "elm,action,click", "",
254 _signal_clicked, obj);
255 edje_object_signal_callback_add(wd->btn, "elm,action,press", "",
256 _signal_pressed, obj);
257 edje_object_signal_callback_add(wd->btn, "elm,action,unpress", "",
258 _signal_unpressed, obj);
259 elm_widget_resize_object_set(obj, wd->btn);
261 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
265 // TODO: convert Elementary to subclassing of Evas_Smart_Class
266 // TODO: and save some bytes, making descriptions per-class and not instance!
267 evas_object_smart_callbacks_descriptions_set(obj, _signals);
272 * Set the label used in the button
274 * @param obj The button object
275 * @param label The text will be written on the button
280 elm_button_label_set(Evas_Object *obj, const char *label)
282 ELM_CHECK_WIDTYPE(obj, widtype);
283 Widget_Data *wd = elm_widget_data_get(obj);
285 eina_stringshare_replace(&wd->label, label);
287 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
289 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
290 edje_object_message_signal_process(wd->btn);
291 edje_object_part_text_set(wd->btn, "elm.text", label);
296 elm_button_label_get(const Evas_Object *obj)
298 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
299 Widget_Data *wd = elm_widget_data_get(obj);
300 if (!wd) return NULL;
305 * Set the icon used for the button
307 * Once the icon object is set, a previously set one will be deleted
309 * @param obj The button object
310 * @param icon The image for the button
315 elm_button_icon_set(Evas_Object *obj, Evas_Object *icon)
317 ELM_CHECK_WIDTYPE(obj, widtype);
318 Widget_Data *wd = elm_widget_data_get(obj);
320 if (wd->icon == icon) return;
321 if (wd->icon) evas_object_del(wd->icon);
325 elm_widget_sub_object_add(obj, icon);
326 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
327 _changed_size_hints, obj);
328 edje_object_part_swallow(wd->btn, "elm.swallow.content", icon);
329 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
330 edje_object_message_signal_process(wd->btn);
336 * Get the icon used for the button
338 * @param obj The button object
339 * @return The image for the button
344 elm_button_icon_get(const Evas_Object *obj)
346 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
347 Widget_Data *wd = elm_widget_data_get(obj);
348 if (!wd) return NULL;
353 * Turn on/off the autorepeat event generated when the user keeps pressing on the button
355 * @param obj The button object
356 * @param on A bool to turn on/off the event
361 elm_button_autorepeat_set(Evas_Object *obj, Eina_Bool on)
363 ELM_CHECK_WIDTYPE(obj, widtype);
364 Widget_Data *wd = elm_widget_data_get(obj);
368 ecore_timer_del(wd->timer);
372 wd->repeating = EINA_FALSE;
376 * Set the initial timeout before the autorepeat event is generated
378 * @param obj The button object
384 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj, double t)
386 ELM_CHECK_WIDTYPE(obj, widtype);
387 Widget_Data *wd = elm_widget_data_get(obj);
389 if (wd->ar_threshold == t) return;
392 ecore_timer_del(wd->timer);
395 wd->ar_threshold = t;
399 * Set the interval between each generated autorepeat event
401 * @param obj The button object
407 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj, double t)
409 ELM_CHECK_WIDTYPE(obj, widtype);
410 Widget_Data *wd = elm_widget_data_get(obj);
412 if (wd->ar_interval == t) return;
415 if (wd->repeating && wd->timer) ecore_timer_interval_set(wd->timer, t);