cbc2b451a1fad4fd7f5aabc532fd2562057eeee8
[framework/uifw/elementary.git] / src / lib / elm_bubble.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 typedef struct _Widget_Data Widget_Data;
5
6 struct _Widget_Data
7 {
8    Evas_Object *bbl;
9    Evas_Object *content, *icon;
10    const char *label, *info, *corner;
11 };
12
13 static const char *widtype = NULL;
14 static void _del_hook(Evas_Object *obj);
15 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
16 static void _theme_hook(Evas_Object *obj);
17 static void _sizing_eval(Evas_Object *obj);
18 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
19 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
20
21 static const char SIG_CLICKED[] = "clicked";
22
23 static const Evas_Smart_Cb_Description _signals[] =
24 {
25      {SIG_CLICKED, ""},
26      {NULL, NULL}
27 };
28
29 static void
30 _del_hook(Evas_Object *obj)
31 {
32    Widget_Data *wd = elm_widget_data_get(obj);
33    if (!wd) return;
34    if (wd->label) eina_stringshare_del(wd->label);
35    if (wd->info) eina_stringshare_del(wd->info);
36    if (wd->corner) eina_stringshare_del(wd->corner);
37    free(wd);
38 }
39
40 static void
41 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
42 {
43    Widget_Data *wd = elm_widget_data_get(obj);
44    if (!wd) return;
45    edje_object_mirrored_set(wd->bbl, rtl);
46 }
47
48 static void
49 _theme_hook(Evas_Object *obj)
50 {
51    Widget_Data *wd = elm_widget_data_get(obj);
52    if (!wd) return;
53    _elm_widget_mirrored_reload(obj);
54    _mirrored_set(obj, elm_widget_mirrored_get(obj));
55    _elm_theme_object_set(obj, wd->bbl, "bubble", wd->corner,
56                          elm_widget_style_get(obj));
57    edje_object_part_text_set(wd->bbl, "elm.text", wd->label);
58    if (wd->label) edje_object_signal_emit(wd->bbl, "elm,state,text,visible", "elm");
59    else edje_object_signal_emit(wd->bbl, "elm,state,text,hidden", "elm");
60    edje_object_part_text_set(wd->bbl, "elm.info", wd->info);
61    if (wd->info) edje_object_signal_emit(wd->bbl, "elm,state,info,visible", "elm");
62    else edje_object_signal_emit(wd->bbl, "elm,state,info,hidden", "elm");
63    if (wd->content)
64      {
65         edje_object_part_swallow(wd->bbl, "elm.swallow.content", wd->content);
66         edje_object_message_signal_process(wd->bbl);
67      }
68    if (wd->icon)
69      edje_object_signal_emit(wd->bbl, "elm,state,icon,visible", "elm");
70    else
71      edje_object_signal_emit(wd->bbl, "elm,state,icon,hidden", "elm");
72    edje_object_scale_set(wd->bbl,
73                          elm_widget_scale_get(obj) * _elm_config->scale);
74    _sizing_eval(obj);
75 }
76
77 static Eina_Bool
78 _elm_bubble_focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next)
79 {
80    Widget_Data *wd = elm_widget_data_get(obj);
81    Evas_Object *cur;
82
83    if ((!wd) || (!wd->content))
84      return EINA_FALSE;
85
86    cur = wd->content;
87
88    /* Try Focus cycle in subitem */
89    return elm_widget_focus_next_get(cur, dir, next);
90 }
91
92 static void
93 _sizing_eval(Evas_Object *obj)
94 {
95    Widget_Data *wd = elm_widget_data_get(obj);
96    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
97    if (!wd) return;
98    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
99    edje_object_size_min_restricted_calc(wd->bbl, &minw, &minh, minw, minh);
100    evas_object_size_hint_min_set(obj, minw, minh);
101    evas_object_size_hint_max_set(obj, maxw, maxh);
102 }
103
104 static void
105 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
106 {
107    Widget_Data *wd = elm_widget_data_get(data);
108    if (!wd) return;
109    _sizing_eval(data);
110 }
111
112 static void
113 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
114 {
115    Widget_Data *wd = elm_widget_data_get(obj);
116    Evas_Object *sub = event_info;
117    if (!wd) return;
118    evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
119                                        _changed_size_hints, obj);
120    if (sub == wd->content) wd->content = NULL;
121    else if (sub == wd->icon)
122      {
123         edje_object_signal_emit(wd->bbl, "elm,state,icon,hidden", "elm");
124         wd->icon = NULL;
125         edje_object_message_signal_process(wd->bbl);
126      }
127    _sizing_eval(obj);
128 }
129
130 static void
131 _mouse_up(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
132 {
133    Evas_Event_Mouse_Up *ev = event_info;
134    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
135      return;
136    evas_object_smart_callback_call(data, SIG_CLICKED, NULL);
137 }
138
139 EAPI Evas_Object *
140 elm_bubble_add(Evas_Object *parent)
141 {
142    Evas_Object *obj;
143    Evas *e;
144    Widget_Data *wd;
145
146    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
147
148    ELM_SET_WIDTYPE(widtype, "bubble");
149    elm_widget_type_set(obj, "bubble");
150    elm_widget_sub_object_add(parent, obj);
151    elm_widget_data_set(obj, wd);
152    elm_widget_del_hook_set(obj, _del_hook);
153    elm_widget_theme_hook_set(obj, _theme_hook);
154    elm_widget_focus_next_hook_set(obj, _elm_bubble_focus_next_hook);
155    elm_widget_can_focus_set(obj, EINA_FALSE);
156
157    wd->corner = eina_stringshare_add("base");
158
159    wd->bbl = edje_object_add(e);
160    elm_widget_resize_object_set(obj, wd->bbl);
161
162    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
163    evas_object_event_callback_add(wd->bbl, EVAS_CALLBACK_MOUSE_UP,
164                                   _mouse_up, obj);
165
166    evas_object_smart_callbacks_descriptions_set(obj, _signals);
167    _mirrored_set(obj, elm_widget_mirrored_get(obj));
168    _elm_theme_object_set(obj, wd->bbl, "bubble", wd->corner,
169                          elm_widget_style_get(obj));
170    _sizing_eval(obj);
171    return obj;
172 }
173
174 EAPI void
175 elm_bubble_label_set(Evas_Object *obj, const char *label)
176 {
177    ELM_CHECK_WIDTYPE(obj, widtype);
178    Widget_Data *wd = elm_widget_data_get(obj);
179    if (!wd) return;
180    eina_stringshare_replace(&wd->label, label);
181    edje_object_part_text_set(wd->bbl, "elm.text", label);
182    if (label) edje_object_signal_emit(wd->bbl, "elm,state,text,visible", "elm");
183    else edje_object_signal_emit(wd->bbl, "elm,state,text,hidden", "elm");
184    _sizing_eval(obj);
185 }
186
187 EAPI const char*
188 elm_bubble_label_get(const Evas_Object *obj)
189 {
190    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
191    Widget_Data *wd = elm_widget_data_get(obj);
192    if (!wd) return NULL;
193    return wd->label;
194 }
195
196 EAPI void
197 elm_bubble_info_set(Evas_Object *obj, const char *info)
198 {
199    ELM_CHECK_WIDTYPE(obj, widtype);
200    Widget_Data *wd = elm_widget_data_get(obj);
201    if (!wd) return;
202    eina_stringshare_replace(&wd->info, info);
203    edje_object_part_text_set(wd->bbl, "elm.info", info);
204    if (info) edje_object_signal_emit(wd->bbl, "elm,state,info,visible", "elm");
205    else edje_object_signal_emit(wd->bbl, "elm,state,info,hidden", "elm");
206    _sizing_eval(obj);
207 }
208
209 EAPI const char *
210 elm_bubble_info_get(const Evas_Object *obj)
211 {
212    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
213    Widget_Data *wd = elm_widget_data_get(obj);
214    if (!wd) return NULL;
215    return wd->info;
216 }
217
218 EAPI void
219 elm_bubble_content_set(Evas_Object *obj, Evas_Object *content)
220 {
221    ELM_CHECK_WIDTYPE(obj, widtype);
222    Widget_Data *wd = elm_widget_data_get(obj);
223    if (!wd) return;
224    if (wd->content == content) return;
225    if (wd->content) evas_object_del(wd->content);
226    wd->content = content;
227    if (content)
228      {
229         elm_widget_sub_object_add(obj, content);
230         evas_object_event_callback_add(content, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
231                                        _changed_size_hints, obj);
232         edje_object_part_swallow(wd->bbl, "elm.swallow.content", content);
233      }
234    _sizing_eval(obj);
235 }
236
237 EAPI Evas_Object *
238 elm_bubble_content_get(const Evas_Object *obj)
239 {
240    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
241    Widget_Data *wd = elm_widget_data_get(obj);
242    if (!wd) return NULL;
243    return wd->content;
244 }
245
246 EAPI Evas_Object *
247 elm_bubble_content_unset(Evas_Object *obj)
248 {
249    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
250    Widget_Data *wd = elm_widget_data_get(obj);
251    Evas_Object *content;
252    if (!wd) return NULL;
253    if (!wd->content) return NULL;
254    content = wd->content;
255    elm_widget_sub_object_del(obj, content);
256    edje_object_part_unswallow(wd->bbl, content);
257    wd->content = NULL;
258    return content;
259 }
260
261 EAPI void
262 elm_bubble_icon_set(Evas_Object *obj, Evas_Object *icon)
263 {
264    ELM_CHECK_WIDTYPE(obj, widtype);
265    Widget_Data *wd = elm_widget_data_get(obj);
266    if (!wd) return;
267    if (wd->icon == icon) return;
268    if (wd->icon) evas_object_del(wd->icon);
269    wd->icon = icon;
270    if (icon)
271      {
272         elm_widget_sub_object_add(obj, icon);
273         edje_object_part_swallow(wd->bbl, "elm.swallow.icon", icon);
274         evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
275                                        _changed_size_hints, obj);
276         edje_object_signal_emit(wd->bbl, "elm,state,icon,visible", "elm");
277         edje_object_message_signal_process(wd->bbl);
278      }
279    _sizing_eval(obj);
280 }
281
282 EAPI Evas_Object *
283 elm_bubble_icon_get(const Evas_Object *obj)
284 {
285    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
286    Widget_Data *wd = elm_widget_data_get(obj);
287    if (!wd) return NULL;
288    return wd->icon;
289 }
290
291 EAPI Evas_Object *
292 elm_bubble_icon_unset(Evas_Object *obj)
293 {
294    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
295    Widget_Data *wd = elm_widget_data_get(obj);
296    Evas_Object *icon;
297    if (!wd) return NULL;
298    if (!wd->icon) return NULL;
299    icon = wd->icon;
300    elm_widget_sub_object_del(obj, icon);
301    edje_object_part_unswallow(wd->bbl, icon);
302    wd->icon = NULL;
303    return icon;
304 }
305
306 EAPI void
307 elm_bubble_corner_set(Evas_Object *obj, const char *corner)
308 {
309    ELM_CHECK_WIDTYPE(obj, widtype);
310    Widget_Data *wd = elm_widget_data_get(obj);
311    if (!wd) return;
312    EINA_SAFETY_ON_NULL_RETURN(corner);
313    eina_stringshare_replace(&wd->corner, corner);
314    _theme_hook(obj);
315 }
316
317 EAPI const char*
318 elm_bubble_corner_get(const Evas_Object *obj)
319 {
320    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
321    Widget_Data *wd = elm_widget_data_get(obj);
322    if (!wd) return NULL;
323    return wd->corner;
324 }