Merge branch 'master' of 165.213.180.234:/git/slp/pkgs/elementary
[framework/uifw/elementary.git] / src / lib / elm_bubble.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup Bubble Bubble
6  * @ingroup Elementary
7  *
8  * The Bubble is an widget used to show a text in a frame as speech is
9  * represented in comics.
10  */
11
12 typedef struct _Widget_Data Widget_Data;
13
14 struct _Widget_Data
15 {
16    Evas_Object *bbl;
17    Evas_Object *content, *icon, *sweep;
18    const char *label, *info;
19    
20    Eina_Bool down:1;
21    Evas_Coord_Point down_point;
22 };
23
24 #define SWEEP_THRESHOLD 100
25
26 static const char *widtype = NULL;
27 static void _del_hook(Evas_Object *obj);
28 static void _theme_hook(Evas_Object *obj);
29 static void _sizing_eval(Evas_Object *obj);
30 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
31 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
32
33 static void
34 _del_hook(Evas_Object *obj)
35 {
36    Widget_Data *wd = elm_widget_data_get(obj);
37    if (!wd) return;
38    if (wd->label) eina_stringshare_del(wd->label);
39    if (wd->info) eina_stringshare_del(wd->info);
40    free(wd);
41 }
42
43 static void
44 _theme_hook(Evas_Object *obj)
45 {
46    Widget_Data *wd = elm_widget_data_get(obj);
47    if (!wd) return;
48    _elm_theme_object_set(obj, wd->bbl, "bubble", "base", elm_widget_style_get(obj));
49    edje_object_part_text_set(wd->bbl, "elm.text", wd->label);
50    edje_object_part_text_set(wd->bbl, "elm.info", wd->info);
51     if (wd->content) 
52       { 
53          edje_object_part_swallow(wd->bbl, "elm.swallow.content", wd->content); 
54          edje_object_message_signal_process(wd->bbl); 
55       } 
56     if (wd->icon)
57         {
58            edje_object_part_swallow(wd->bbl, "elm.swallow.icon", wd->icon);
59            edje_object_signal_emit(wd->bbl, "elm,state,icon,visible", "elm");
60         }
61     else 
62        edje_object_signal_emit(wd->bbl, "elm,state,icon,hidden", "elm"); 
63    edje_object_scale_set(wd->bbl, elm_widget_scale_get(obj) * _elm_config->scale);
64    _sizing_eval(obj);
65 }
66
67 static void
68 _sizing_eval(Evas_Object *obj)
69 {
70    Widget_Data *wd = elm_widget_data_get(obj);
71    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
72    if (!wd) return;
73    edje_object_size_min_calc(wd->bbl, &minw, &minh);
74    evas_object_size_hint_min_set(obj, minw, minh);
75    evas_object_size_hint_max_set(obj, maxw, maxh);
76 }
77
78 static void
79 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
80 {
81    Widget_Data *wd = elm_widget_data_get(data);
82    if (!wd) return;
83    _sizing_eval(data);
84 }
85
86 static void
87 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
88 {
89    Widget_Data *wd = elm_widget_data_get(obj);
90    Evas_Object *sub = event_info;
91    if (!wd) return;
92    evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
93                                   _changed_size_hints, obj);
94    if (sub == wd->content) wd->content = NULL;
95    else if (sub == wd->icon)
96      {
97         edje_object_signal_emit(wd->bbl, "elm,state,icon,hidden", "elm");
98         wd->icon = NULL;
99         edje_object_message_signal_process(wd->bbl);
100      }
101    else if (sub == wd->sweep) wd->sweep = NULL;
102    _sizing_eval(obj);
103 }
104
105 static void
106 _mouse_down(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
107 {
108         Widget_Data *wd = elm_widget_data_get(data);
109         Evas_Event_Mouse_Down *ev = event_info;
110
111         wd->down = EINA_TRUE;
112         wd->down_point.x = ev->canvas.x;
113         wd->down_point.y = ev->canvas.y;
114 }
115
116 static void
117 _mouse_up(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
118 {
119         Widget_Data *wd = elm_widget_data_get(data);
120         Evas_Event_Mouse_Up *ev = event_info;
121
122         if (!wd->down) return;
123
124         if (ev->canvas.x - wd->down_point.x > SWEEP_THRESHOLD)
125                 evas_object_smart_callback_call(data, "sweep,left,right", NULL);
126         else if (wd->down_point.x - ev->canvas.x > SWEEP_THRESHOLD)
127                 evas_object_smart_callback_call(data, "sweep,right,left", NULL);
128
129         wd->down = EINA_FALSE;
130         wd->down_point.x = 0;
131         wd->down_point.y = 0;   
132 }
133
134 /**
135  * Add a new bubble to the parent
136  *
137  * @param parent The parent object
138  * @return The new object or NULL if it cannot be created
139  *
140  * This function adds a text bubble to the given parent evas object.
141  *
142  * @ingroup Bubble
143  */
144 EAPI Evas_Object *
145 elm_bubble_add(Evas_Object *parent)
146 {
147    Evas_Object *obj;
148    Evas *e;
149    Widget_Data *wd;
150
151    wd = ELM_NEW(Widget_Data);
152    e = evas_object_evas_get(parent);
153    obj = elm_widget_add(e);
154    ELM_SET_WIDTYPE(widtype, "bubble");
155    elm_widget_type_set(obj, "bubble");
156    elm_widget_sub_object_add(parent, obj);
157    elm_widget_data_set(obj, wd);
158    elm_widget_del_hook_set(obj, _del_hook);
159    elm_widget_theme_hook_set(obj, _theme_hook);
160
161    wd->bbl = edje_object_add(e);
162    _elm_theme_object_set(obj, wd->bbl, "bubble", "base", "default");
163    elm_widget_resize_object_set(obj, wd->bbl);
164
165    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
166    evas_object_event_callback_add(wd->bbl, EVAS_CALLBACK_MOUSE_UP, _mouse_up, obj);
167    evas_object_event_callback_add(wd->bbl, EVAS_CALLBACK_MOUSE_DOWN, _mouse_down, obj);
168
169    wd->down = EINA_FALSE;
170    wd->down_point.x = 0;
171    wd->down_point.y = 0;
172
173    _sizing_eval(obj);
174    return obj;
175 }
176
177 /**
178  * Set the label of the bubble
179  *
180  * @param obj The bubble object
181  * @param label The string to set in the label
182  *
183  * This function sets the title of the bubble that is shown on top of
184  * the bubble.
185  *
186  * @ingroup Bubble
187  */
188 EAPI void
189 elm_bubble_label_set(Evas_Object *obj, const char *label)
190 {
191    ELM_CHECK_WIDTYPE(obj, widtype);
192    Widget_Data *wd = elm_widget_data_get(obj);
193    if (!wd) return;
194    if (label) edje_object_signal_emit(wd->bbl, "elm,state,label,visible", "elm");
195    else edje_object_signal_emit(wd->bbl, "elm,state,label,hidden", "elm");
196    eina_stringshare_replace(&wd->label, label);
197    edje_object_part_text_set(wd->bbl, "elm.text", label);
198    _sizing_eval(obj);
199 }
200
201 /**
202  * Get the label of the bubble
203  *
204  * @param obj The bubble object
205  * @return The string of set in the label
206  *
207  * This function gets the title of the bubble that is shown on top of
208  * the bubble.
209  *
210  * @ingroup Bubble
211  */
212 EAPI const char*
213 elm_bubble_label_get(const Evas_Object *obj)
214 {
215    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
216    Widget_Data *wd = elm_widget_data_get(obj);
217    if (!wd) return NULL;
218    return wd->label;
219 }
220
221 /**
222  * Set the info of the bubble
223  *
224  * @param obj The bubble object
225  * @param info The given info about the bubble
226  *
227  * This function sets the text shown on the top right of bubble.
228  * In the Anchorblock example of the Elementary tests application it
229  * shows time.
230  *
231  * @ingroup Bubble
232  *
233  */
234 EAPI void
235 elm_bubble_info_set(Evas_Object *obj, const char *info)
236 {
237    ELM_CHECK_WIDTYPE(obj, widtype);
238    Widget_Data *wd = elm_widget_data_get(obj);
239    if (!wd) return;
240    eina_stringshare_replace(&wd->info, info);
241    edje_object_part_text_set(wd->bbl, "elm.info", info);
242    _sizing_eval(obj);
243 }
244
245 /**
246  * Get the info of the bubble
247  *
248  * @param obj The bubble object
249  *
250  * @return The "info" string of the bubble
251  *
252  * This function gets the text set to be displayed at the top right of
253  * the bubble.
254  *
255  * @ingroup Bubble
256  *
257  */
258 EAPI const char *
259 elm_bubble_info_get(const Evas_Object *obj)
260 {
261    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
262    Widget_Data *wd = elm_widget_data_get(obj);
263    if (!wd) return NULL;
264    return wd->info;
265 }
266
267 /**
268  * Set the content to be shown in the bubble
269  *
270  * Once the content object is set, a previously set one will be deleted.
271  * If you want to keep the old content object, use the
272  * elm_bubble_content_unset() function.
273  *
274  * @param obj The bubble object
275  * @param content The given content of the bubble
276  *
277  * This function sets the content shown on the middle of the bubble.
278  * In the Anchorblock example of the Elementary tests application it
279  * shows time.
280  *
281  * @ingroup Bubble
282  */
283 EAPI void
284 elm_bubble_content_set(Evas_Object *obj, Evas_Object *content)
285 {
286    ELM_CHECK_WIDTYPE(obj, widtype);
287    Widget_Data *wd = elm_widget_data_get(obj);
288    if (!wd) return;
289    if (wd->content == content) return;
290    if (wd->content) evas_object_del(wd->content);
291    wd->content = content;
292    if (content)
293      {
294         elm_widget_sub_object_add(obj, content);
295         evas_object_event_callback_add(content, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
296                                        _changed_size_hints, obj);
297         edje_object_part_swallow(wd->bbl, "elm.swallow.content", content);
298      }
299    _sizing_eval(obj);
300 }
301
302 /**
303   * Get the content shown in the bubble 
304   * 
305   * Return the content object which is set for this widget. 
306   * 
307   * @param obj The bubble object 
308   * @return The content that is being used 
309   * 
310   * @ingroup Bubble 
311   */ 
312  EAPI Evas_Object * 
313  elm_bubble_content_get(const Evas_Object *obj) 
314  { 
315     ELM_CHECK_WIDTYPE(obj, widtype) NULL; 
316     Widget_Data *wd = elm_widget_data_get(obj); 
317     if (!wd) return NULL; 
318     return wd->content; 
319  } 
320   
321  /** 
322  * Unset the content shown in the bubble
323  *
324  * Unparent and return the content object which was set for this widget.
325  *
326  * @param obj The bubble object
327  * @return The content that was being used
328  *
329  * @ingroup Bubble
330  */
331 EAPI Evas_Object *
332 elm_bubble_content_unset(Evas_Object *obj)
333 {
334    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
335    Widget_Data *wd = elm_widget_data_get(obj);
336    Evas_Object *content;
337    if (!wd) return NULL;
338    if (!wd->content) return NULL;
339    content = wd->content;
340    elm_widget_sub_object_del(obj, content);
341    edje_object_part_unswallow(wd->bbl, content);
342    wd->content = NULL;
343    return content;
344 }
345
346 /**
347  * Set the icon of the bubble
348  *
349  * Once the icon object is set, a previously set one will be deleted.
350   * If you want to keep the old content object, use the 
351   * elm_icon_content_unset() function. 
352  *
353  * @param obj The bubble object
354  * @param icon The given icon for the bubble
355  *
356  * @ingroup Bubble
357  */
358 EAPI void
359 elm_bubble_icon_set(Evas_Object *obj, Evas_Object *icon)
360 {
361    ELM_CHECK_WIDTYPE(obj, widtype);
362    Widget_Data *wd = elm_widget_data_get(obj);
363    if (!wd) return;
364    if (wd->icon == icon) return;
365    if (wd->icon) evas_object_del(wd->icon);
366    wd->icon = icon;
367    if (icon)
368      {
369         elm_widget_sub_object_add(obj, icon);
370         edje_object_part_swallow(wd->bbl, "elm.swallow.icon", icon);
371         evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
372                                        _changed_size_hints, obj);
373         edje_object_signal_emit(wd->bbl, "elm,state,icon,visible", "elm");
374         edje_object_message_signal_process(wd->bbl);
375      }
376    _sizing_eval(obj);
377 }
378
379 /**
380  * Get the icon of the bubble
381  *
382  * @param obj The bubble object
383  * @return The icon for the bubble
384  *
385  * This function gets the icon shown on the top left of bubble.
386  *
387  * @ingroup Bubble
388  */
389 EAPI Evas_Object *
390 elm_bubble_icon_get(const Evas_Object *obj)
391 {
392    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
393    Widget_Data *wd = elm_widget_data_get(obj);
394    if (!wd) return NULL;
395    return wd->icon;
396 }
397
398  /** 
399   * Unset the icon of the bubble 
400   * 
401   * Unparent and return the icon object which was set for this widget. 
402   * 
403   * @param obj The bubble object 
404   * @return The icon that was being used 
405   * 
406   * @ingroup Bubble 
407   */ 
408  EAPI Evas_Object * 
409  elm_bubble_icon_unset(Evas_Object *obj) 
410  { 
411     ELM_CHECK_WIDTYPE(obj, widtype) NULL; 
412     Widget_Data *wd = elm_widget_data_get(obj); 
413     Evas_Object *icon; 
414     if (!wd) return NULL; 
415     if (!wd->icon) return NULL; 
416     icon = wd->icon; 
417     elm_widget_sub_object_del(obj, icon); 
418     edje_object_part_unswallow(wd->bbl, icon); 
419     wd->icon = NULL; 
420     return icon; 
421  } 
422
423 /**
424  * Set the sweep layout
425  *
426  * @param obj The bubble object
427  * @param content The given content of the bubble
428  *
429  * This function sets the sweep layout when "sweep,left,right"signal is emitted. 
430  *
431  * @ingroup Bubble
432  */
433 EAPI void
434 elm_bubble_sweep_layout_set(Evas_Object *obj, Evas_Object *sweep)
435 {
436    ELM_CHECK_WIDTYPE(obj, widtype);
437    Widget_Data *wd = elm_widget_data_get(obj);
438    if (!wd) return;
439    if (wd->sweep == sweep) return;
440    if (wd->sweep) evas_object_del(wd->sweep);
441    wd->sweep = sweep;
442    if (sweep)
443      {
444         elm_widget_sub_object_add(obj, sweep);
445         edje_object_part_swallow(wd->bbl, "elm.swallow.sweep", sweep);
446      }
447 }
448
449 /**
450  * Unset and hide the sweep layout
451  *
452  * @param obj The bubble object
453  * @param content The given content of the bubble
454  *
455  * This function sets the sweep layout when "sweep,right,left"signal is emitted. 
456  *
457  * @ingroup Bubble
458  */
459 EAPI Evas_Object *
460 elm_bubble_sweep_layout_unset(Evas_Object *obj)
461 {
462    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
463    Widget_Data *wd = elm_widget_data_get(obj);
464    Evas_Object *sweep;
465    if (!wd) return NULL;
466    if (!wd->sweep) return NULL;
467    sweep = wd->sweep;
468    elm_widget_sub_object_del(obj, sweep);
469    edje_object_part_unswallow(wd->bbl, sweep);
470    evas_object_hide(sweep);
471    wd->sweep = NULL;
472    return sweep;
473 }
474
475 /**
476  * Set the corner of the bubble
477  *
478  * @param obj The bubble object.
479  * @param corner The given corner for the bubble.
480  *
481  * This function sets the corner of the bubble.
482  *
483  * @ingroup Bubble
484  */
485 EAPI void
486 elm_bubble_corner_set(Evas_Object *obj, const char *corner)
487 {
488    ELM_CHECK_WIDTYPE(obj, widtype);
489    Widget_Data *wd = elm_widget_data_get(obj);
490    if (!wd) return;
491    _elm_theme_object_set(obj, wd->bbl, "bubble", corner, elm_widget_style_get(obj));
492    if (wd->icon)
493      edje_object_part_swallow(wd->bbl, "elm.swallow.icon", wd->icon);
494    if (wd->content)
495      edje_object_part_swallow(wd->bbl, "elm.swallow.content", wd->content);
496    // FIXME: fix label etc.
497    _sizing_eval(obj);
498 }