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