1 #include <Elementary.h>
4 typedef struct _Widget_Data Widget_Data;
16 Evas_Object *btn, *icon;
21 const char *statelabel[4];
23 Eina_Bool autorepeat : 1;
24 Eina_Bool repeating : 1;
25 Eina_Bool delete_me : 1;
28 static const char *widtype = NULL;
29 static void _del_hook(Evas_Object *obj);
30 static void _del_pre_hook(Evas_Object *obj);
31 static void _theme_hook(Evas_Object *obj);
32 static void _disable_hook(Evas_Object *obj);
33 static void _content_set_hook(Evas_Object *obj, const char *part, Evas_Object *content);
34 static Evas_Object *_content_get_hook(const Evas_Object *obj, const char *part);
35 static Evas_Object *_content_unset_hook(Evas_Object *obj, const char *part);
36 static void _sizing_eval(Evas_Object *obj);
37 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
38 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
39 static void _signal_clicked(void *data, Evas_Object *obj, const char *emission, const char *source);
40 static void _signal_pressed(void *data, Evas_Object *obj, const char *emission, const char *source);
41 static void _signal_unpressed(void *data, Evas_Object *obj, const char *emission, const char *source);
42 static void _on_focus_hook(void *data, Evas_Object *obj);
43 static void _activate(Evas_Object *obj);
44 static void _activate_hook(Evas_Object *obj);
45 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
46 Evas_Callback_Type type, void *event_info);
48 static void _set_label(Evas_Object *obj, const char *label);
49 static void _signal_default_text_set(void *data, Evas_Object *obj, const char *emission, const char *source);
51 static const char SIG_CLICKED[] = "clicked";
52 static const char SIG_REPEATED[] = "repeated";
53 static const char SIG_PRESSED[] = "pressed";
54 static const char SIG_UNPRESSED[] = "unpressed";
55 static const Evas_Smart_Cb_Description _signals[] = {
64 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
66 if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
67 Evas_Event_Key_Down *ev = event_info;
68 Widget_Data *wd = elm_widget_data_get(obj);
69 if (!wd) return EINA_FALSE;
70 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
71 if (elm_widget_disabled_get(obj)) return EINA_FALSE;
72 if ((strcmp(ev->keyname, "Return")) &&
73 (strcmp(ev->keyname, "KP_Enter")) &&
74 (strcmp(ev->keyname, "space")))
77 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
78 edje_object_signal_emit(wd->btn, "elm,anim,activate", "elm");
83 _del_pre_hook(Evas_Object *obj)
85 Widget_Data *wd = elm_widget_data_get(obj);
87 wd->delete_me = EINA_TRUE;
91 _del_hook(Evas_Object *obj)
93 Widget_Data *wd = elm_widget_data_get(obj);
95 if (wd->label) eina_stringshare_del(wd->label);
96 if (wd->statelabel[DEFAULT]) eina_stringshare_del(wd->statelabel[DEFAULT]);
97 if (wd->statelabel[HIGHLIGHTED]) eina_stringshare_del(wd->statelabel[HIGHLIGHTED]);
98 if (wd->statelabel[FOCUSED]) eina_stringshare_del(wd->statelabel[FOCUSED]);
99 if (wd->statelabel[DISABLED]) eina_stringshare_del(wd->statelabel[DISABLED]);
104 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
106 Widget_Data *wd = elm_widget_data_get(obj);
108 if (elm_widget_focus_get(obj))
110 if (wd->statelabel[FOCUSED])
112 _set_label(obj, wd->statelabel[FOCUSED]);
114 edje_object_signal_emit(wd->btn, "elm,action,focus", "elm");
115 evas_object_focus_set(wd->btn, EINA_TRUE);
119 if (wd->statelabel[DEFAULT])
120 _set_label(obj, wd->statelabel[DEFAULT]);
121 edje_object_signal_emit(wd->btn, "elm,action,unfocus", "elm");
122 evas_object_focus_set(wd->btn, EINA_FALSE);
127 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
129 Widget_Data *wd = elm_widget_data_get(obj);
131 edje_object_mirrored_set(wd->btn, rtl);
135 _theme_hook(Evas_Object *obj)
137 Widget_Data *wd = elm_widget_data_get(obj);
140 _elm_widget_mirrored_reload(obj);
141 _mirrored_set(obj, elm_widget_mirrored_get(obj));
142 _elm_theme_object_set(obj, wd->btn, "button", "base", elm_widget_style_get(obj));
143 if (elm_object_disabled_get(obj))
144 edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
146 edje_object_part_swallow(wd->btn, "elm.swallow.content", wd->icon);
148 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
150 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
152 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
154 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
155 edje_object_part_text_set(wd->btn, "elm.text", wd->label);
156 edje_object_message_signal_process(wd->btn);
157 edje_object_scale_set(wd->btn, elm_widget_scale_get(obj) * _elm_config->scale);
158 str = edje_object_data_get(wd->btn, "focus_highlight");
159 if ((str) && (!strcmp(str, "on")))
160 elm_widget_highlight_in_theme_set(obj, EINA_TRUE);
162 elm_widget_highlight_in_theme_set(obj, EINA_FALSE);
167 _disable_hook(Evas_Object *obj)
169 Widget_Data *wd = elm_widget_data_get(obj);
171 if (elm_widget_disabled_get(obj))
173 if (wd->statelabel[DISABLED] )
175 _set_label(obj, wd->statelabel[DISABLED]);
177 edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
181 if (wd->statelabel[DEFAULT])
182 _set_label(obj, wd->statelabel[DEFAULT]);
183 edje_object_signal_emit(wd->btn, "elm,state,enabled", "elm");
188 _signal_emit_hook(Evas_Object *obj, const char *emission, const char *source)
190 Widget_Data *wd = elm_widget_data_get(obj);
192 edje_object_signal_emit(wd->btn, emission, source);
196 _signal_callback_add_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
198 Widget_Data *wd = elm_widget_data_get(obj);
200 edje_object_signal_callback_add(wd->btn, emission, source, func_cb, data);
204 _signal_callback_del_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
206 Widget_Data *wd = elm_widget_data_get(obj);
207 edje_object_signal_callback_del_full(wd->btn, emission, source, func_cb,
212 _content_set_hook(Evas_Object *obj, const char *part __UNUSED__, Evas_Object *content)
214 ELM_CHECK_WIDTYPE(obj, widtype);
215 Widget_Data *wd = elm_widget_data_get(obj);
217 if (wd->icon == content) return;
218 if (wd->icon) evas_object_del(wd->icon);
222 elm_widget_sub_object_add(obj, content);
223 evas_object_event_callback_add(content,
224 EVAS_CALLBACK_CHANGED_SIZE_HINTS,
225 _changed_size_hints, obj);
226 edje_object_part_swallow(wd->btn, "elm.swallow.content", content);
227 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
228 edje_object_message_signal_process(wd->btn);
234 _content_get_hook(const Evas_Object *obj, const char *part __UNUSED__)
236 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
237 Widget_Data *wd = elm_widget_data_get(obj);
238 if (!wd) return NULL;
243 _content_unset_hook(Evas_Object *obj, const char *part __UNUSED__)
245 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
246 Widget_Data *wd = elm_widget_data_get(obj);
247 if (!wd) return NULL;
248 if (!wd->icon) return NULL;
249 Evas_Object *icon = wd->icon;
250 elm_widget_sub_object_del(obj, wd->icon);
251 evas_object_event_callback_del_full(wd->icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
252 _changed_size_hints, obj);
253 edje_object_part_unswallow(wd->btn, wd->icon);
259 _sizing_eval(Evas_Object *obj)
261 Widget_Data *wd = elm_widget_data_get(obj);
262 Evas_Coord minw = -1, minh = -1;
265 if (wd->delete_me) return;
266 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
267 edje_object_size_min_restricted_calc(wd->btn, &minw, &minh, minw, minh);
268 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
269 evas_object_size_hint_min_set(obj, minw, minh);
273 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
275 Widget_Data *wd = elm_widget_data_get(data);
277 if (obj != wd->icon) return;
282 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
284 Widget_Data *wd = elm_widget_data_get(obj);
285 Evas_Object *sub = event_info;
289 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
290 evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
291 _changed_size_hints, obj);
293 edje_object_message_signal_process(wd->btn);
299 _activate(Evas_Object *obj)
301 Widget_Data *wd = elm_widget_data_get(obj);
305 ecore_timer_del(wd->timer);
308 wd->repeating = EINA_FALSE;
309 evas_object_smart_callback_call(obj, SIG_CLICKED, NULL);
313 _activate_hook(Evas_Object *obj)
319 _signal_clicked(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
325 _autorepeat_send(void *data)
327 Widget_Data *wd = elm_widget_data_get(data);
328 if (!wd) return ECORE_CALLBACK_CANCEL;
330 evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
334 return ECORE_CALLBACK_CANCEL;
337 return ECORE_CALLBACK_RENEW;
341 _autorepeat_initial_send(void *data)
343 Widget_Data *wd = elm_widget_data_get(data);
344 if (!wd) return ECORE_CALLBACK_CANCEL;
346 if (wd->timer) ecore_timer_del(wd->timer);
347 wd->repeating = EINA_TRUE;
348 _autorepeat_send(data);
349 wd->timer = ecore_timer_add(wd->ar_interval, _autorepeat_send, data);
351 return ECORE_CALLBACK_CANCEL;
355 _signal_pressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
357 Widget_Data *wd = elm_widget_data_get(data);
360 if (wd->statelabel[HIGHLIGHTED])
362 _set_label(data, wd->statelabel[HIGHLIGHTED]);
364 if ((wd->autorepeat) && (!wd->repeating))
366 if (wd->ar_threshold <= 0.0)
367 _autorepeat_initial_send(data); /* call immediately */
369 wd->timer = ecore_timer_add(wd->ar_threshold, _autorepeat_initial_send, data);
372 evas_object_smart_callback_call(data, SIG_PRESSED, NULL);
376 _signal_unpressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
378 Widget_Data *wd = elm_widget_data_get(data);
380 if (wd->statelabel[DEFAULT])
381 _set_label(data, wd->statelabel[DEFAULT]);
385 ecore_timer_del(wd->timer);
388 wd->repeating = EINA_FALSE;
389 evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
393 _signal_default_text_set(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
395 Widget_Data *wd = elm_widget_data_get(data);
397 if (wd->statelabel[DEFAULT])
398 _set_label(data, wd->statelabel[DEFAULT]);
402 _elm_button_label_set(Evas_Object *obj, const char *item, const char *label)
404 ELM_CHECK_WIDTYPE(obj, widtype);
405 Widget_Data *wd = elm_widget_data_get(obj);
406 if (item && strcmp(item, "default")) return;
408 eina_stringshare_replace(&wd->label, label);
410 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
412 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
413 edje_object_message_signal_process(wd->btn);
414 edje_object_part_text_set(wd->btn, "elm.text", label);
419 _elm_button_label_get(const Evas_Object *obj, const char *item)
421 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
422 Widget_Data *wd = elm_widget_data_get(obj);
423 if (item && strcmp(item, "default")) return NULL;
424 if (!wd) return NULL;
429 elm_button_add(Evas_Object *parent)
435 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
437 ELM_SET_WIDTYPE(widtype, "button");
438 elm_widget_type_set(obj, "button");
439 elm_widget_sub_object_add(parent, obj);
440 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
441 elm_widget_data_set(obj, wd);
442 elm_widget_del_hook_set(obj, _del_hook);
443 elm_widget_del_pre_hook_set(obj, _del_pre_hook);
444 elm_widget_theme_hook_set(obj, _theme_hook);
445 elm_widget_disable_hook_set(obj, _disable_hook);
446 elm_widget_can_focus_set(obj, EINA_TRUE);
447 elm_widget_activate_hook_set(obj, _activate_hook);
448 elm_widget_event_hook_set(obj, _event_hook);
449 elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
450 elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
451 elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
452 elm_widget_text_set_hook_set(obj, _elm_button_label_set);
453 elm_widget_text_get_hook_set(obj, _elm_button_label_get);
454 elm_widget_content_set_hook_set(obj, _content_set_hook);
455 elm_widget_content_get_hook_set(obj, _content_get_hook);
456 elm_widget_content_unset_hook_set(obj, _content_unset_hook);
458 wd->btn = edje_object_add(e);
459 _elm_theme_object_set(obj, wd->btn, "button", "base", "default");
460 wd->statetype[DEFAULT] = 0;
461 wd->statetype[HIGHLIGHTED] = 0;
462 wd->statetype[FOCUSED] = 0;
463 wd->statetype[DISABLED] = 0;
464 wd->statelabel[DEFAULT] = 0;
465 wd->statelabel[HIGHLIGHTED] = 0;
466 wd->statelabel[FOCUSED] = 0;
467 wd->statelabel[DISABLED] = 0;
468 edje_object_signal_callback_add(wd->btn, "elm,action,click", "",
469 _signal_clicked, obj);
470 edje_object_signal_callback_add(wd->btn, "elm,action,press", "",
471 _signal_pressed, obj);
472 edje_object_signal_callback_add(wd->btn, "elm,action,unpress", "",
473 _signal_unpressed, obj);
474 edje_object_signal_callback_add(wd->btn, "elm,action,default,text,set", "",
475 _signal_default_text_set, obj);
476 elm_widget_resize_object_set(obj, wd->btn);
478 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
482 // TODO: convert Elementary to subclassing of Evas_Smart_Class
483 // TODO: and save some bytes, making descriptions per-class and not instance!
484 evas_object_smart_callbacks_descriptions_set(obj, _signals);
489 _set_label(Evas_Object *obj, const char *label)
491 Widget_Data *wd = elm_widget_data_get(obj);
493 edje_object_part_text_set(wd->btn, "elm.text", label);
498 elm_button_label_set_for_state(Evas_Object *obj, const char *label, UIControlState state)
500 Widget_Data *wd = elm_widget_data_get(obj);
503 if (label == NULL) return;
505 if (state == UIControlStateDefault)
507 wd->statetype[DEFAULT] = UIControlStateDefault;
508 eina_stringshare_replace(&wd->statelabel[DEFAULT], label);
511 if (state == UIControlStateHighlighted)
513 wd->statetype[HIGHLIGHTED] = UIControlStateHighlighted;
514 eina_stringshare_replace(&wd->statelabel[HIGHLIGHTED], label);
517 if (state == UIControlStateFocused)
519 wd->statetype[FOCUSED] = UIControlStateFocused;
520 eina_stringshare_replace(&wd->statelabel[FOCUSED], label);
523 if (state == UIControlStateDisabled)
525 wd->statetype[DISABLED] = UIControlStateDisabled;
526 eina_stringshare_replace(&wd->statelabel[DISABLED], label);
532 elm_button_label_get_for_state(const Evas_Object *obj, UIControlState state)
534 Widget_Data *wd = elm_widget_data_get(obj);
535 if (!wd) return NULL;
537 if (state == UIControlStateDefault)
538 return wd->statelabel[DEFAULT];
539 else if (state == UIControlStateHighlighted)
540 return wd->statelabel[HIGHLIGHTED];
541 else if (state == UIControlStateFocused)
542 return wd->statelabel[FOCUSED];
543 else if (state == UIControlStateDisabled)
544 return wd->statelabel[DISABLED];
550 elm_button_label_set(Evas_Object *obj, const char *label)
552 _elm_button_label_set(obj, NULL, label);
556 elm_button_label_get(const Evas_Object *obj)
558 return _elm_button_label_get(obj, NULL);
563 elm_button_icon_set(Evas_Object *obj, Evas_Object *icon)
565 _content_set_hook(obj, NULL, icon);
569 elm_button_icon_get(const Evas_Object *obj)
571 return _content_get_hook(obj, NULL);
575 elm_button_icon_unset(Evas_Object *obj)
577 return _content_unset_hook(obj, NULL);
581 elm_button_autorepeat_set(Evas_Object *obj, Eina_Bool on)
583 ELM_CHECK_WIDTYPE(obj, widtype);
584 Widget_Data *wd = elm_widget_data_get(obj);
588 ecore_timer_del(wd->timer);
592 wd->repeating = EINA_FALSE;
596 elm_button_autorepeat_get(const Evas_Object *obj)
598 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
599 Widget_Data *wd = elm_widget_data_get(obj);
600 if (!wd) return EINA_FALSE;
601 return wd->autorepeat;
605 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj, double t)
607 ELM_CHECK_WIDTYPE(obj, widtype);
608 Widget_Data *wd = elm_widget_data_get(obj);
610 if (wd->ar_threshold == t) return;
613 ecore_timer_del(wd->timer);
616 wd->ar_threshold = t;
620 elm_button_autorepeat_initial_timeout_get(const Evas_Object *obj)
622 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
623 Widget_Data *wd = elm_widget_data_get(obj);
625 return wd->ar_threshold;
629 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj, double t)
631 ELM_CHECK_WIDTYPE(obj, widtype);
632 Widget_Data *wd = elm_widget_data_get(obj);
634 if (wd->ar_interval == t) return;
637 if ((wd->repeating) && (wd->timer)) ecore_timer_interval_set(wd->timer, t);
641 elm_button_autorepeat_gap_timeout_get(const Evas_Object *obj)
643 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
644 Widget_Data *wd = elm_widget_data_get(obj);
646 return wd->ar_interval;