EFL migration revision 67547
[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 _content_set_hook(Evas_Object *obj, const char *part, Evas_Object *content);
17 static Evas_Object *_content_get_hook(const Evas_Object *obj, const char *part);
18 static Evas_Object *_content_unset_hook(Evas_Object *obj, const char *part);
19 static void _theme_hook(Evas_Object *obj);
20 static void _sizing_eval(Evas_Object *obj);
21 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
22 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
23
24 static const char SIG_CLICKED[] = "clicked";
25
26 static const Evas_Smart_Cb_Description _signals[] =
27 {
28      {SIG_CLICKED, ""},
29      {NULL, NULL}
30 };
31
32 static void
33 _del_hook(Evas_Object *obj)
34 {
35    Widget_Data *wd = elm_widget_data_get(obj);
36    if (!wd) return;
37    if (wd->label) eina_stringshare_del(wd->label);
38    if (wd->info) eina_stringshare_del(wd->info);
39    if (wd->corner) eina_stringshare_del(wd->corner);
40    free(wd);
41 }
42
43 static void
44 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
45 {
46    Widget_Data *wd = elm_widget_data_get(obj);
47    if (!wd) return;
48    edje_object_mirrored_set(wd->bbl, rtl);
49 }
50
51 static void
52 _theme_hook(Evas_Object *obj)
53 {
54    Widget_Data *wd = elm_widget_data_get(obj);
55    if (!wd) return;
56    _elm_widget_mirrored_reload(obj);
57    _mirrored_set(obj, elm_widget_mirrored_get(obj));
58    _elm_theme_object_set(obj, wd->bbl, "bubble", wd->corner,
59                          elm_widget_style_get(obj));
60    edje_object_part_text_set(wd->bbl, "elm.text", wd->label);
61    if (wd->label) edje_object_signal_emit(wd->bbl, "elm,state,text,visible", "elm");
62    else edje_object_signal_emit(wd->bbl, "elm,state,text,hidden", "elm");
63    edje_object_part_text_set(wd->bbl, "elm.info", wd->info);
64    if (wd->info) edje_object_signal_emit(wd->bbl, "elm,state,info,visible", "elm");
65    else edje_object_signal_emit(wd->bbl, "elm,state,info,hidden", "elm");
66    if (wd->content)
67      {
68         edje_object_part_swallow(wd->bbl, "elm.swallow.content", wd->content);
69         edje_object_message_signal_process(wd->bbl);
70      }
71    if (wd->icon)
72      edje_object_signal_emit(wd->bbl, "elm,state,icon,visible", "elm");
73    else
74      edje_object_signal_emit(wd->bbl, "elm,state,icon,hidden", "elm");
75    edje_object_scale_set(wd->bbl,
76                          elm_widget_scale_get(obj) * _elm_config->scale);
77    _sizing_eval(obj);
78 }
79
80 static void
81 _content_set(Evas_Object *obj, Evas_Object *content)
82 {
83    Widget_Data *wd = elm_widget_data_get(obj);
84    if (!wd) return;
85
86    if (wd->content == content) return;
87    if (wd->content) evas_object_del(wd->content);
88    wd->content = content;
89    if (content)
90      {
91         elm_widget_sub_object_add(obj, content);
92         evas_object_event_callback_add(content,
93                                        EVAS_CALLBACK_CHANGED_SIZE_HINTS,
94                                        _changed_size_hints, obj);
95         edje_object_part_swallow(wd->bbl, "elm.swallow.content", content);
96      }
97    _sizing_eval(obj);
98 }
99
100 static Evas_Object *
101 _content_unset(Evas_Object *obj)
102 {
103    Widget_Data *wd = elm_widget_data_get(obj);
104    Evas_Object *content;
105    if (!wd) return NULL;
106    if (!wd->content) return NULL;
107    content = wd->content;
108    elm_widget_sub_object_del(obj, content);
109    evas_object_event_callback_del_full(content,
110                                        EVAS_CALLBACK_CHANGED_SIZE_HINTS,
111                                        _changed_size_hints, obj);
112    edje_object_part_unswallow(wd->bbl, content);
113    wd->content = NULL;
114    return content;
115 }
116
117 static void
118 _icon_set(Evas_Object *obj, Evas_Object* icon)
119 {
120    Widget_Data *wd = elm_widget_data_get(obj);
121    if (!wd) return;
122    if (wd->icon == icon) return;
123    if (wd->icon) evas_object_del(wd->icon);
124    wd->icon = icon;
125    if (icon)
126      {
127         elm_widget_sub_object_add(obj, icon);
128         edje_object_part_swallow(wd->bbl, "elm.swallow.icon", icon);
129         evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
130                                        _changed_size_hints, obj);
131         edje_object_signal_emit(wd->bbl, "elm,state,icon,visible", "elm");
132         edje_object_message_signal_process(wd->bbl);
133      }
134    _sizing_eval(obj);
135 }
136
137 static Evas_Object *
138 _icon_unset(Evas_Object *obj)
139 {
140    Widget_Data *wd = elm_widget_data_get(obj);
141    Evas_Object *icon;
142    if (!wd) return NULL;
143    if (!wd->icon) return NULL;
144    icon = wd->icon;
145    elm_widget_sub_object_del(obj, icon);
146    evas_object_event_callback_del_full(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
147                                        _changed_size_hints, obj);
148    edje_object_part_unswallow(wd->bbl, icon);
149    wd->icon = NULL;
150    return icon;
151 }
152
153 static void
154 _content_set_hook(Evas_Object *obj, const char *part, Evas_Object *content)
155 {
156    ELM_CHECK_WIDTYPE(obj, widtype);
157    Widget_Data *wd = elm_widget_data_get(obj);
158    if (!wd) return;
159
160    if (!part || !strcmp(part, "default"))
161      _content_set(obj, content);
162    else if(!strcmp(part, "icon"))
163      _icon_set(obj, content);
164 }
165
166 static Evas_Object *
167 _content_get_hook(const Evas_Object *obj, const char *part)
168 {
169    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
170    Widget_Data *wd = elm_widget_data_get(obj);
171    if (!wd) return NULL;
172    if (!part || !strcmp(part, "default"))
173      return wd->content;
174    else if(!strcmp(part, "icon"))
175      return wd->icon;
176    return NULL;
177 }
178
179 static Evas_Object *
180 _content_unset_hook(Evas_Object *obj, const char *part)
181 {
182    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
183    Widget_Data *wd = elm_widget_data_get(obj);
184    if (!wd) return NULL;
185    if (!part || !strcmp(part, "default"))
186      return _content_unset(obj);
187    else if(!strcmp(part, "icon"))
188      return _icon_unset(obj);
189    return NULL;
190 }
191
192 static Eina_Bool
193 _elm_bubble_focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next)
194 {
195    Widget_Data *wd = elm_widget_data_get(obj);
196    Evas_Object *cur;
197
198    if ((!wd) || (!wd->content))
199      return EINA_FALSE;
200
201    cur = wd->content;
202
203    /* Try Focus cycle in subitem */
204    return elm_widget_focus_next_get(cur, dir, next);
205 }
206
207 static void
208 _sizing_eval(Evas_Object *obj)
209 {
210    Widget_Data *wd = elm_widget_data_get(obj);
211    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
212    if (!wd) return;
213    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
214    edje_object_size_min_restricted_calc(wd->bbl, &minw, &minh, minw, minh);
215    evas_object_size_hint_min_set(obj, minw, minh);
216    evas_object_size_hint_max_set(obj, maxw, maxh);
217 }
218
219 static void
220 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
221 {
222    Widget_Data *wd = elm_widget_data_get(data);
223    if (!wd) return;
224    _sizing_eval(data);
225 }
226
227 static void
228 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
229 {
230    Widget_Data *wd = elm_widget_data_get(obj);
231    Evas_Object *sub = event_info;
232    if (!wd) return;
233    evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
234                                        _changed_size_hints, obj);
235    if (sub == wd->content) wd->content = NULL;
236    else if (sub == wd->icon)
237      {
238         edje_object_signal_emit(wd->bbl, "elm,state,icon,hidden", "elm");
239         wd->icon = NULL;
240         edje_object_message_signal_process(wd->bbl);
241      }
242    _sizing_eval(obj);
243 }
244
245 static void
246 _mouse_up(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
247 {
248    Evas_Event_Mouse_Up *ev = event_info;
249    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
250      return;
251    evas_object_smart_callback_call(data, SIG_CLICKED, NULL);
252 }
253
254 static void
255 _elm_bubble_label_set(Evas_Object *obj, const char *item, const char *label)
256 {
257    ELM_CHECK_WIDTYPE(obj, widtype);
258    Widget_Data *wd = elm_widget_data_get(obj);
259    if (!wd) return;
260
261    if (!item || !strcmp(item, "default"))
262      {
263         eina_stringshare_replace(&wd->label, label);
264         edje_object_part_text_set(wd->bbl, "elm.text", label);
265         if (label) edje_object_signal_emit(wd->bbl, "elm,state,text,visible",
266               "elm");
267         else edje_object_signal_emit(wd->bbl, "elm,state,text,hidden", "elm");
268         _sizing_eval(obj);
269      }
270    else if (!strcmp(item, "info"))
271      {
272         eina_stringshare_replace(&wd->info, label);
273         edje_object_part_text_set(wd->bbl, "elm.info", label);
274         if (label) edje_object_signal_emit(wd->bbl, "elm,state,info,visible",
275               "elm");
276         else edje_object_signal_emit(wd->bbl, "elm,state,info,hidden", "elm");
277         _sizing_eval(obj);
278      }
279 }
280
281 static const char*
282 _elm_bubble_label_get(const Evas_Object *obj, const char *item)
283 {
284    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
285    Widget_Data *wd = elm_widget_data_get(obj);
286    if (!wd) return NULL;
287
288    if (!item || !strcmp(item, "default"))
289      {
290         return wd->label;
291      }
292    else if (!strcmp(item, "info"))
293      {
294         return wd->info;
295      }
296
297    return NULL;
298 }
299
300 EAPI Evas_Object *
301 elm_bubble_add(Evas_Object *parent)
302 {
303    Evas_Object *obj;
304    Evas *e;
305    Widget_Data *wd;
306
307    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
308
309    ELM_SET_WIDTYPE(widtype, "bubble");
310    elm_widget_type_set(obj, "bubble");
311    elm_widget_sub_object_add(parent, obj);
312    elm_widget_data_set(obj, wd);
313    elm_widget_del_hook_set(obj, _del_hook);
314    elm_widget_theme_hook_set(obj, _theme_hook);
315    elm_widget_focus_next_hook_set(obj, _elm_bubble_focus_next_hook);
316    elm_widget_can_focus_set(obj, EINA_FALSE);
317    elm_widget_text_set_hook_set(obj, _elm_bubble_label_set);
318    elm_widget_text_get_hook_set(obj, _elm_bubble_label_get);
319    elm_widget_content_set_hook_set(obj, _content_set_hook);
320    elm_widget_content_get_hook_set(obj, _content_get_hook);
321    elm_widget_content_unset_hook_set(obj, _content_unset_hook);
322
323    wd->corner = eina_stringshare_add("base");
324
325    wd->bbl = edje_object_add(e);
326    elm_widget_resize_object_set(obj, wd->bbl);
327
328    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
329    evas_object_event_callback_add(wd->bbl, EVAS_CALLBACK_MOUSE_UP,
330                                   _mouse_up, obj);
331
332    evas_object_smart_callbacks_descriptions_set(obj, _signals);
333    _mirrored_set(obj, elm_widget_mirrored_get(obj));
334    _elm_theme_object_set(obj, wd->bbl, "bubble", wd->corner,
335                          elm_widget_style_get(obj));
336    _sizing_eval(obj);
337    return obj;
338 }
339
340 EAPI void
341 elm_bubble_label_set(Evas_Object *obj, const char *label)
342 {
343    _elm_bubble_label_set(obj, NULL, label);
344 }
345
346 EAPI const char*
347 elm_bubble_label_get(const Evas_Object *obj)
348 {
349    return _elm_bubble_label_get(obj, NULL);
350 }
351
352 EAPI void
353 elm_bubble_info_set(Evas_Object *obj, const char *info)
354 {
355    _elm_bubble_label_set(obj, "info", info);
356 }
357
358 EAPI const char *
359 elm_bubble_info_get(const Evas_Object *obj)
360 {
361    return _elm_bubble_label_get(obj, "info");
362 }
363
364 EAPI void
365 elm_bubble_content_set(Evas_Object *obj, Evas_Object *content)
366 {
367    _content_set_hook(obj, NULL, content);
368 }
369
370 EAPI Evas_Object *
371 elm_bubble_content_get(const Evas_Object *obj)
372 {
373    return _content_get_hook(obj, NULL);
374 }
375
376 EAPI Evas_Object *
377 elm_bubble_content_unset(Evas_Object *obj)
378 {
379    return _content_unset_hook(obj, NULL);
380 }
381
382 EAPI void
383 elm_bubble_icon_set(Evas_Object *obj, Evas_Object *icon)
384 {
385    _content_set_hook(obj, "icon", icon);
386 }
387
388 EAPI Evas_Object *
389 elm_bubble_icon_get(const Evas_Object *obj)
390 {
391    return _content_get_hook(obj, "icon");
392 }
393
394 EAPI Evas_Object *
395 elm_bubble_icon_unset(Evas_Object *obj)
396 {
397    return _content_unset_hook(obj, "icon");
398 }
399
400 EAPI void
401 elm_bubble_corner_set(Evas_Object *obj, const char *corner)
402 {
403    ELM_CHECK_WIDTYPE(obj, widtype);
404    Widget_Data *wd = elm_widget_data_get(obj);
405    if (!wd) return;
406    EINA_SAFETY_ON_NULL_RETURN(corner);
407    eina_stringshare_replace(&wd->corner, corner);
408    _theme_hook(obj);
409 }
410
411 EAPI const char*
412 elm_bubble_corner_get(const Evas_Object *obj)
413 {
414    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
415    Widget_Data *wd = elm_widget_data_get(obj);
416    if (!wd) return NULL;
417    return wd->corner;
418 }