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