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