[elm_bubble] merge upstream
[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  * Signals that you can add callbacks for are:
12  *
13  * clicked - This is called when a user has clicked the bubble.
14  */
15
16 typedef struct _Widget_Data Widget_Data;
17
18 #define SWEEP_SUPPORT 1
19
20 struct _Widget_Data
21 {
22    Evas_Object *bbl;
23    Evas_Object *content, *icon;
24    const char *label, *info, *corner;
25 #ifdef SWEEP_SUPPORT
26    Evas_Object *sweep;
27    Eina_Bool down:1;
28    Evas_Coord_Point down_point;
29 #endif
30 };
31
32 #ifdef SWEEP_SUPPORT
33 #define SWEEP_THRESHOLD 100
34 #endif
35
36 static const char *widtype = NULL;
37 static void _del_hook(Evas_Object *obj);
38 static void _theme_hook(Evas_Object *obj);
39 static void _sizing_eval(Evas_Object *obj);
40 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
41 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
42
43 #define SIG_CLICKED "clicked"
44 static const Evas_Smart_Cb_Description _signals[] =
45 {
46   {SIG_CLICKED, ""},
47   {NULL, NULL}
48 };
49
50 static void
51 _del_hook(Evas_Object *obj)
52 {
53    Widget_Data *wd = elm_widget_data_get(obj);
54    if (!wd) return;
55    if (wd->label) eina_stringshare_del(wd->label);
56    if (wd->info) eina_stringshare_del(wd->info);
57    if (wd->corner) eina_stringshare_del(wd->corner);
58    free(wd);
59 }
60
61 static void
62 _theme_hook(Evas_Object *obj)
63 {
64    Widget_Data *wd = elm_widget_data_get(obj);
65    if (!wd) return;
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    edje_object_part_text_set(wd->bbl, "elm.info", wd->info);
70    if (wd->content)
71      {
72         edje_object_part_swallow(wd->bbl, "elm.swallow.content", wd->content);
73         edje_object_message_signal_process(wd->bbl); 
74      }
75    if (wd->icon)
76      edje_object_signal_emit(wd->bbl, "elm,state,icon,visible", "elm");
77    else
78      edje_object_signal_emit(wd->bbl, "elm,state,icon,hidden", "elm");
79    edje_object_scale_set(wd->bbl,
80                          elm_widget_scale_get(obj) * _elm_config->scale);
81    _sizing_eval(obj);
82 }
83
84 static Eina_Bool
85 _elm_bubble_focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next)
86 {
87    Widget_Data *wd = elm_widget_data_get(obj);
88    Evas_Object *cur;
89
90    if ((!wd) || (!wd->content))
91      return EINA_FALSE;
92
93    cur = wd->content;
94
95    /* Try Focus cycle in subitem */
96    return elm_widget_focus_next_get(cur, dir, next);
97 }
98
99 static void
100 _sizing_eval(Evas_Object *obj)
101 {
102    Widget_Data *wd = elm_widget_data_get(obj);
103    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
104    if (!wd) return;
105    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
106    edje_object_size_min_restricted_calc(wd->bbl, &minw, &minh, minw, minh);
107    evas_object_size_hint_min_set(obj, minw, minh);
108    evas_object_size_hint_max_set(obj, maxw, maxh);
109 }
110
111 static void
112 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
113 {
114    Widget_Data *wd = elm_widget_data_get(data);
115    if (!wd) return;
116    _sizing_eval(data);
117 }
118
119 static void
120 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
121 {
122    Widget_Data *wd = elm_widget_data_get(obj);
123    Evas_Object *sub = event_info;
124    if (!wd) return;
125    evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
126                                   _changed_size_hints, obj);
127    if (sub == wd->content) wd->content = NULL;
128    else if (sub == wd->icon)
129      {
130         edje_object_signal_emit(wd->bbl, "elm,state,icon,hidden", "elm");
131         wd->icon = NULL;
132         edje_object_message_signal_process(wd->bbl);
133      }
134 #ifdef SWEEP_SUPPORT
135    else if (sub == wd->sweep) wd->sweep = NULL;
136 #endif
137    _sizing_eval(obj);
138 }
139
140 #ifdef SWEEP_SUPPORT
141 static void
142 _mouse_down(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
143 {
144    Widget_Data *wd = elm_widget_data_get(data);
145    Evas_Event_Mouse_Down *ev = event_info;
146
147    wd->down = EINA_TRUE;
148    wd->down_point.x = ev->canvas.x;
149    wd->down_point.y = ev->canvas.y;
150 }
151 #endif
152
153 static void
154 _mouse_up(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
155 {
156    Evas_Event_Mouse_Up *ev = event_info;
157    Widget_Data *wd = elm_widget_data_get(data);
158    if (!wd->down) return;
159    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
160      {
161 #ifdef SWEEP_SUPPORT 
162         if (ev->canvas.x - wd->down_point.x > SWEEP_THRESHOLD)
163           evas_object_smart_callback_call(data, "sweep,left,right", NULL);
164         else if (wd->down_point.x - ev->canvas.x > SWEEP_THRESHOLD)
165           evas_object_smart_callback_call(data, "sweep,right,left", NULL);
166
167         wd->down = EINA_FALSE;
168         wd->down_point.x = 0;
169         wd->down_point.y = 0;   
170 #endif
171      }
172    else if (!wd->sweep)
173      evas_object_smart_callback_call(data, SIG_CLICKED, NULL);
174 }
175
176 /**
177  * Add a new bubble to the parent
178  *
179  * @param parent The parent object
180  * @return The new object or NULL if it cannot be created
181  *
182  * This function adds a text bubble to the given parent evas object.
183  *
184  * @ingroup Bubble
185  */
186 EAPI Evas_Object *
187 elm_bubble_add(Evas_Object *parent)
188 {
189    Evas_Object *obj;
190    Evas *e;
191    Widget_Data *wd;
192
193    EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
194
195    wd = ELM_NEW(Widget_Data);
196    e = evas_object_evas_get(parent);
197    if (!e) return NULL;
198    obj = elm_widget_add(e);
199    ELM_SET_WIDTYPE(widtype, "bubble");
200    elm_widget_type_set(obj, "bubble");
201    elm_widget_sub_object_add(parent, obj);
202    elm_widget_data_set(obj, wd);
203    elm_widget_del_hook_set(obj, _del_hook);
204    elm_widget_theme_hook_set(obj, _theme_hook);
205    elm_widget_focus_next_hook_set(obj, _elm_bubble_focus_next_hook);
206    elm_widget_can_focus_set(obj, EINA_FALSE);
207
208    wd->corner = eina_stringshare_add("base");
209
210    wd->bbl = edje_object_add(e);
211    _elm_theme_object_set(obj, wd->bbl, "bubble", "base", "default");
212    elm_widget_resize_object_set(obj, wd->bbl);
213
214    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
215    evas_object_event_callback_add(wd->bbl, EVAS_CALLBACK_MOUSE_UP,
216                                   _mouse_up, obj);
217 #ifdef SWEEP_SUPPORT
218    evas_object_event_callback_add(wd->bbl, EVAS_CALLBACK_MOUSE_DOWN,
219                                   _mouse_down, obj);
220
221    wd->down = EINA_FALSE;
222    wd->down_point.x = 0;
223    wd->down_point.y = 0;
224 #endif
225    evas_object_smart_callbacks_descriptions_set(obj, _signals);
226    _sizing_eval(obj);
227    return obj;
228 }
229
230 /**
231  * Set the label of the bubble
232  *
233  * @param obj The bubble object
234  * @param label The string to set in the label
235  *
236  * This function sets the title of the bubble that is shown on top of
237  * the bubble.
238  *
239  * @ingroup Bubble
240  */
241 EAPI void
242 elm_bubble_label_set(Evas_Object *obj, const char *label)
243 {
244    ELM_CHECK_WIDTYPE(obj, widtype);
245    Widget_Data *wd = elm_widget_data_get(obj);
246    if (!wd) return;
247    if (label) edje_object_signal_emit(wd->bbl, "elm,state,label,visible", "elm");
248    else edje_object_signal_emit(wd->bbl, "elm,state,label,hidden", "elm");
249    eina_stringshare_replace(&wd->label, label);
250    edje_object_part_text_set(wd->bbl, "elm.text", label);
251    _sizing_eval(obj);
252 }
253
254 /**
255  * Get the label of the bubble
256  *
257  * @param obj The bubble object
258  * @return The string of set in the label
259  *
260  * This function gets the title of the bubble that is shown on top of
261  * the bubble.
262  *
263  * @ingroup Bubble
264  */
265 EAPI const char*
266 elm_bubble_label_get(const Evas_Object *obj)
267 {
268    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
269    Widget_Data *wd = elm_widget_data_get(obj);
270    if (!wd) return NULL;
271    return wd->label;
272 }
273
274 /**
275  * Set the info of the bubble
276  *
277  * @param obj The bubble object
278  * @param info The given info about the bubble
279  *
280  * This function sets the text shown on the top right of bubble.
281  * In the Anchorblock example of the Elementary tests application it
282  * shows time.
283  *
284  * @ingroup Bubble
285  *
286  */
287 EAPI void
288 elm_bubble_info_set(Evas_Object *obj, const char *info)
289 {
290    ELM_CHECK_WIDTYPE(obj, widtype);
291    Widget_Data *wd = elm_widget_data_get(obj);
292    if (!wd) return;
293    eina_stringshare_replace(&wd->info, info);
294    edje_object_part_text_set(wd->bbl, "elm.info", info);
295    _sizing_eval(obj);
296 }
297
298 /**
299  * Get the info of the bubble
300  *
301  * @param obj The bubble object
302  *
303  * @return The "info" string of the bubble
304  *
305  * This function gets the text set to be displayed at the top right of
306  * the bubble.
307  *
308  * @ingroup Bubble
309  *
310  */
311 EAPI const char *
312 elm_bubble_info_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->info;
318 }
319
320 /**
321  * Set the content to be shown in the bubble
322  *
323  * Once the content object is set, a previously set one will be deleted.
324  * If you want to keep the old content object, use the
325  * elm_bubble_content_unset() function.
326  *
327  * @param obj The bubble object
328  * @param content The given content of the bubble
329  *
330  * This function sets the content shown on the middle of the bubble.
331  * In the Anchorblock example of the Elementary tests application it
332  * shows time.
333  *
334  * @ingroup Bubble
335  */
336 EAPI void
337 elm_bubble_content_set(Evas_Object *obj, Evas_Object *content)
338 {
339    ELM_CHECK_WIDTYPE(obj, widtype);
340    Widget_Data *wd = elm_widget_data_get(obj);
341    if (!wd) return;
342    if (wd->content == content) return;
343    if (wd->content) evas_object_del(wd->content);
344    wd->content = content;
345    if (content)
346      {
347         elm_widget_sub_object_add(obj, content);
348         evas_object_event_callback_add(content, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
349                                        _changed_size_hints, obj);
350         edje_object_part_swallow(wd->bbl, "elm.swallow.content", content);
351      }
352    _sizing_eval(obj);
353 }
354
355 /**
356   * Get the content shown in the bubble 
357   * 
358   * Return the content object which is set for this widget. 
359   * 
360   * @param obj The bubble object 
361   * @return The content that is being used 
362   * 
363   * @ingroup Bubble 
364   */ 
365  EAPI Evas_Object * 
366  elm_bubble_content_get(const Evas_Object *obj) 
367  { 
368     ELM_CHECK_WIDTYPE(obj, widtype) NULL; 
369     Widget_Data *wd = elm_widget_data_get(obj); 
370     if (!wd) return NULL; 
371     return wd->content; 
372  } 
373   
374  /** 
375  * Unset the content shown in the bubble
376  *
377  * Unparent and return the content object which was set for this widget.
378  *
379  * @param obj The bubble object
380  * @return The content that was being used
381  *
382  * @ingroup Bubble
383  */
384 EAPI Evas_Object *
385 elm_bubble_content_unset(Evas_Object *obj)
386 {
387    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
388    Widget_Data *wd = elm_widget_data_get(obj);
389    Evas_Object *content;
390    if (!wd) return NULL;
391    if (!wd->content) return NULL;
392    content = wd->content;
393    elm_widget_sub_object_del(obj, content);
394    edje_object_part_unswallow(wd->bbl, content);
395    wd->content = NULL;
396    return content;
397 }
398
399 /**
400  * Set the icon of the bubble
401  *
402  * Once the icon object is set, a previously set one will be deleted.
403   * If you want to keep the old content object, use the 
404   * elm_icon_content_unset() function. 
405  *
406  * @param obj The bubble object
407  * @param icon The given icon for the bubble
408  *
409  * @ingroup Bubble
410  */
411 EAPI void
412 elm_bubble_icon_set(Evas_Object *obj, Evas_Object *icon)
413 {
414    ELM_CHECK_WIDTYPE(obj, widtype);
415    Widget_Data *wd = elm_widget_data_get(obj);
416    if (!wd) return;
417    if (wd->icon == icon) return;
418    if (wd->icon) evas_object_del(wd->icon);
419    wd->icon = icon;
420    if (icon)
421      {
422         elm_widget_sub_object_add(obj, icon);
423         edje_object_part_swallow(wd->bbl, "elm.swallow.icon", icon);
424         evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
425                                        _changed_size_hints, obj);
426         edje_object_signal_emit(wd->bbl, "elm,state,icon,visible", "elm");
427         edje_object_message_signal_process(wd->bbl);
428      }
429    _sizing_eval(obj);
430 }
431
432 /**
433  * Get the icon of the bubble
434  *
435  * @param obj The bubble object
436  * @return The icon for the bubble
437  *
438  * This function gets the icon shown on the top left of bubble.
439  *
440  * @ingroup Bubble
441  */
442 EAPI Evas_Object *
443 elm_bubble_icon_get(const Evas_Object *obj)
444 {
445    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
446    Widget_Data *wd = elm_widget_data_get(obj);
447    if (!wd) return NULL;
448    return wd->icon;
449 }
450
451  /** 
452   * Unset the icon of the bubble 
453   * 
454   * Unparent and return the icon object which was set for this widget. 
455   * 
456   * @param obj The bubble object 
457   * @return The icon that was being used 
458   * 
459   * @ingroup Bubble 
460   */ 
461  EAPI Evas_Object * 
462  elm_bubble_icon_unset(Evas_Object *obj) 
463  { 
464     ELM_CHECK_WIDTYPE(obj, widtype) NULL; 
465     Widget_Data *wd = elm_widget_data_get(obj); 
466     Evas_Object *icon; 
467     if (!wd) return NULL; 
468     if (!wd->icon) return NULL; 
469     icon = wd->icon; 
470     elm_widget_sub_object_del(obj, icon); 
471     edje_object_part_unswallow(wd->bbl, icon); 
472     wd->icon = NULL; 
473     return icon; 
474  } 
475
476 /**
477  * Set the sweep layout
478  *
479  * @param obj The bubble object
480  * @param content The given content of the bubble
481  *
482  * This function sets the sweep layout when "sweep,left,right"signal is emitted. 
483  *
484  * @ingroup Bubble
485  */
486 EAPI void
487 elm_bubble_sweep_layout_set(Evas_Object *obj, Evas_Object *sweep)
488 {
489    ELM_CHECK_WIDTYPE(obj, widtype);
490    Widget_Data *wd = elm_widget_data_get(obj);
491    if (!wd) return;
492 #ifdef SWEEP_SUPPORT
493    if (wd->sweep == sweep) return;
494    if (wd->sweep) evas_object_del(wd->sweep);
495    wd->sweep = sweep;
496    if (sweep)
497      {
498         elm_widget_sub_object_add(obj, sweep);
499         edje_object_part_swallow(wd->bbl, "elm.swallow.sweep", sweep);
500      }
501 #endif
502 }
503
504 /**
505  * Unset and hide the sweep layout
506  *
507  * @param obj The bubble object
508  * @param content The given content of the bubble
509  *
510  * This function sets the sweep layout when "sweep,right,left"signal is emitted. 
511  *
512  * @ingroup Bubble
513  */
514 EAPI Evas_Object *
515 elm_bubble_sweep_layout_unset(Evas_Object *obj)
516 {
517    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
518    Widget_Data *wd = elm_widget_data_get(obj);
519    Evas_Object *sweep = NULL;
520    if (!wd) return NULL;
521 #ifdef SWEEP_SUPPORT
522    if (!wd->sweep) return NULL;
523    sweep = wd->sweep;
524    elm_widget_sub_object_del(obj, sweep);
525    edje_object_part_unswallow(wd->bbl, sweep);
526    evas_object_hide(sweep);
527    wd->sweep = NULL;
528 #endif
529    return sweep;
530 }
531
532 /**
533  * Set the corner of the bubble
534  *
535  * @param obj The bubble object.
536  * @param corner The given corner for the bubble.
537  *
538  * This function sets the corner of the bubble.
539  * The corner will be used to find the group in the theme
540  * For example, if you set the corner to "bottom_right",
541  * the following group will be searched:
542  * "elm/bubble/bottom_right/default",
543  * considering default style.
544  *
545  * @ingroup Bubble
546  */
547 EAPI void
548 elm_bubble_corner_set(Evas_Object *obj, const char *corner)
549 {
550    ELM_CHECK_WIDTYPE(obj, widtype);
551    Widget_Data *wd = elm_widget_data_get(obj);
552    if (!wd) return;
553    EINA_SAFETY_ON_NULL_RETURN(corner);
554    eina_stringshare_replace(&wd->corner, corner);
555    _theme_hook(obj);
556 }
557
558 /**
559  * Get the corner of the bubble
560  *
561  * @param obj The bubble object.
562  * @return The given corner for the bubble.
563  *
564  * This function gets the corner of the bubble.
565  *
566  * @ingroup Bubble
567  */
568 EAPI const char*
569 elm_bubble_corner_get(const Evas_Object *obj)
570 {
571    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
572    Widget_Data *wd = elm_widget_data_get(obj);
573    if (!wd) return NULL;
574    return wd->corner;
575 }