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