Merge "resolve CQ:H0100113254 (index 0 level selected event)"
[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    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 /**
178  * Add a new bubble to the parent
179  *
180  * @param parent The parent object
181  * @return The new object or NULL if it cannot be created
182  *
183  * This function adds a text bubble to the given parent evas object.
184  *
185  * @ingroup Bubble
186  */
187 EAPI Evas_Object *
188 elm_bubble_add(Evas_Object *parent)
189 {
190    Evas_Object *obj;
191    Evas *e;
192    Widget_Data *wd;
193
194    EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
195
196    wd = ELM_NEW(Widget_Data);
197    e = evas_object_evas_get(parent);
198    if (!e) return NULL;
199    obj = elm_widget_add(e);
200    ELM_SET_WIDTYPE(widtype, "bubble");
201    elm_widget_type_set(obj, "bubble");
202    elm_widget_sub_object_add(parent, obj);
203    elm_widget_data_set(obj, wd);
204    elm_widget_del_hook_set(obj, _del_hook);
205    elm_widget_theme_hook_set(obj, _theme_hook);
206    elm_widget_focus_next_hook_set(obj, _elm_bubble_focus_next_hook);
207    elm_widget_can_focus_set(obj, EINA_FALSE);
208
209    wd->corner = eina_stringshare_add("base");
210
211    wd->bbl = edje_object_add(e);
212    _elm_theme_object_set(obj, wd->bbl, "bubble", "base", "default");
213    elm_widget_resize_object_set(obj, wd->bbl);
214
215    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
216    evas_object_event_callback_add(wd->bbl, EVAS_CALLBACK_MOUSE_UP,
217                                   _mouse_up, obj);
218 #ifdef SWEEP_SUPPORT
219    evas_object_event_callback_add(wd->bbl, EVAS_CALLBACK_MOUSE_DOWN,
220                                   _mouse_down, obj);
221
222    wd->down = EINA_FALSE;
223    wd->down_point.x = 0;
224    wd->down_point.y = 0;
225 #endif
226    evas_object_smart_callbacks_descriptions_set(obj, _signals);
227    _sizing_eval(obj);
228    return obj;
229 }
230
231 /**
232  * Set the label of the bubble
233  *
234  * @param obj The bubble object
235  * @param label The string to set in the label
236  *
237  * This function sets the title of the bubble that is shown on top of
238  * the bubble.
239  *
240  * @ingroup Bubble
241  */
242 EAPI void
243 elm_bubble_label_set(Evas_Object *obj, const char *label)
244 {
245    ELM_CHECK_WIDTYPE(obj, widtype);
246    Widget_Data *wd = elm_widget_data_get(obj);
247    if (!wd) return;
248    eina_stringshare_replace(&wd->label, label);
249    edje_object_part_text_set(wd->bbl, "elm.text", label);
250    if (label) edje_object_signal_emit(wd->bbl, "elm,state,text,visible", "elm");
251    else edje_object_signal_emit(wd->bbl, "elm,state,text,hidden", "elm");
252    _sizing_eval(obj);
253 }
254
255 /**
256  * Get the label of the bubble
257  *
258  * @param obj The bubble object
259  * @return The string of set in the label
260  *
261  * This function gets the title of the bubble that is shown on top of
262  * the bubble.
263  *
264  * @ingroup Bubble
265  */
266 EAPI const char*
267 elm_bubble_label_get(const Evas_Object *obj)
268 {
269    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
270    Widget_Data *wd = elm_widget_data_get(obj);
271    if (!wd) return NULL;
272    return wd->label;
273 }
274
275 /**
276  * Set the info of the bubble
277  *
278  * @param obj The bubble object
279  * @param info The given info about the bubble
280  *
281  * This function sets the text shown on the top right of bubble.
282  * In the Anchorblock example of the Elementary tests application it
283  * shows time.
284  *
285  * @ingroup Bubble
286  *
287  */
288 EAPI void
289 elm_bubble_info_set(Evas_Object *obj, const char *info)
290 {
291    ELM_CHECK_WIDTYPE(obj, widtype);
292    Widget_Data *wd = elm_widget_data_get(obj);
293    if (!wd) return;
294    eina_stringshare_replace(&wd->info, info);
295    edje_object_part_text_set(wd->bbl, "elm.info", info);
296    if (info) edje_object_signal_emit(wd->bbl, "elm,state,info,visible", "elm");
297    else edje_object_signal_emit(wd->bbl, "elm,state,info,hidden", "elm");
298    _sizing_eval(obj);
299 }
300
301 /**
302  * Get the info of the bubble
303  *
304  * @param obj The bubble object
305  *
306  * @return The "info" string of the bubble
307  *
308  * This function gets the text set to be displayed at the top right of
309  * the bubble.
310  *
311  * @ingroup Bubble
312  *
313  */
314 EAPI const char *
315 elm_bubble_info_get(const Evas_Object *obj)
316 {
317    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
318    Widget_Data *wd = elm_widget_data_get(obj);
319    if (!wd) return NULL;
320    return wd->info;
321 }
322
323 /**
324  * Set the content to be shown in the bubble
325  *
326  * Once the content object is set, a previously set one will be deleted.
327  * If you want to keep the old content object, use the
328  * elm_bubble_content_unset() function.
329  *
330  * @param obj The bubble object
331  * @param content The given content of the bubble
332  *
333  * This function sets the content shown on the middle of the bubble.
334  * In the Anchorblock example of the Elementary tests application it
335  * shows time.
336  *
337  * @ingroup Bubble
338  */
339 EAPI void
340 elm_bubble_content_set(Evas_Object *obj, Evas_Object *content)
341 {
342    ELM_CHECK_WIDTYPE(obj, widtype);
343    Widget_Data *wd = elm_widget_data_get(obj);
344    if (!wd) return;
345    if (wd->content == content) return;
346    if (wd->content) evas_object_del(wd->content);
347    wd->content = content;
348    if (content)
349      {
350         elm_widget_sub_object_add(obj, content);
351         evas_object_event_callback_add(content, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
352                                        _changed_size_hints, obj);
353         edje_object_part_swallow(wd->bbl, "elm.swallow.content", content);
354      }
355    _sizing_eval(obj);
356 }
357
358 /**
359   * Get the content shown in the bubble 
360   * 
361   * Return the content object which is set for this widget. 
362   * 
363   * @param obj The bubble object 
364   * @return The content that is being used 
365   * 
366   * @ingroup Bubble 
367   */ 
368  EAPI Evas_Object * 
369  elm_bubble_content_get(const Evas_Object *obj) 
370  { 
371     ELM_CHECK_WIDTYPE(obj, widtype) NULL; 
372     Widget_Data *wd = elm_widget_data_get(obj); 
373     if (!wd) return NULL; 
374     return wd->content; 
375  } 
376   
377  /** 
378  * Unset the content shown in the bubble
379  *
380  * Unparent and return the content object which was set for this widget.
381  *
382  * @param obj The bubble object
383  * @return The content that was being used
384  *
385  * @ingroup Bubble
386  */
387 EAPI Evas_Object *
388 elm_bubble_content_unset(Evas_Object *obj)
389 {
390    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
391    Widget_Data *wd = elm_widget_data_get(obj);
392    Evas_Object *content;
393    if (!wd) return NULL;
394    if (!wd->content) return NULL;
395    content = wd->content;
396    elm_widget_sub_object_del(obj, content);
397    edje_object_part_unswallow(wd->bbl, content);
398    wd->content = NULL;
399    return content;
400 }
401
402 /**
403  * Set the icon of the bubble
404  *
405  * Once the icon object is set, a previously set one will be deleted.
406   * If you want to keep the old content object, use the 
407   * elm_icon_content_unset() function. 
408  *
409  * @param obj The bubble object
410  * @param icon The given icon for the bubble
411  *
412  * @ingroup Bubble
413  */
414 EAPI void
415 elm_bubble_icon_set(Evas_Object *obj, Evas_Object *icon)
416 {
417    ELM_CHECK_WIDTYPE(obj, widtype);
418    Widget_Data *wd = elm_widget_data_get(obj);
419    if (!wd) return;
420    if (wd->icon == icon) return;
421    if (wd->icon) evas_object_del(wd->icon);
422    wd->icon = icon;
423    if (icon)
424      {
425         elm_widget_sub_object_add(obj, icon);
426         edje_object_part_swallow(wd->bbl, "elm.swallow.icon", icon);
427         evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
428                                        _changed_size_hints, obj);
429         edje_object_signal_emit(wd->bbl, "elm,state,icon,visible", "elm");
430         edje_object_message_signal_process(wd->bbl);
431      }
432    _sizing_eval(obj);
433 }
434
435 /**
436  * Get the icon of the bubble
437  *
438  * @param obj The bubble object
439  * @return The icon for the bubble
440  *
441  * This function gets the icon shown on the top left of bubble.
442  *
443  * @ingroup Bubble
444  */
445 EAPI Evas_Object *
446 elm_bubble_icon_get(const Evas_Object *obj)
447 {
448    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
449    Widget_Data *wd = elm_widget_data_get(obj);
450    if (!wd) return NULL;
451    return wd->icon;
452 }
453
454  /** 
455   * Unset the icon of the bubble 
456   * 
457   * Unparent and return the icon object which was set for this widget. 
458   * 
459   * @param obj The bubble object 
460   * @return The icon that was being used 
461   * 
462   * @ingroup Bubble 
463   */ 
464  EAPI Evas_Object * 
465  elm_bubble_icon_unset(Evas_Object *obj) 
466  { 
467     ELM_CHECK_WIDTYPE(obj, widtype) NULL; 
468     Widget_Data *wd = elm_widget_data_get(obj); 
469     Evas_Object *icon; 
470     if (!wd) return NULL; 
471     if (!wd->icon) return NULL; 
472     icon = wd->icon; 
473     elm_widget_sub_object_del(obj, icon); 
474     edje_object_part_unswallow(wd->bbl, icon); 
475     wd->icon = NULL; 
476     return icon; 
477  } 
478
479 /**
480  * Set the sweep layout
481  *
482  * @param obj The bubble object
483  * @param content The given content of the bubble
484  *
485  * This function sets the sweep layout when "sweep,left,right"signal is emitted. 
486  *
487  * @ingroup Bubble
488  */
489 EAPI void
490 elm_bubble_sweep_layout_set(Evas_Object *obj, Evas_Object *sweep)
491 {
492    ELM_CHECK_WIDTYPE(obj, widtype);
493    Widget_Data *wd = elm_widget_data_get(obj);
494    if (!wd) return;
495 #ifdef SWEEP_SUPPORT
496    if (wd->sweep == sweep) return;
497    if (wd->sweep) evas_object_del(wd->sweep);
498    wd->sweep = sweep;
499    if (sweep)
500       edje_object_part_swallow(wd->bbl, "elm.swallow.sweep", sweep);
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    edje_object_part_unswallow(wd->bbl, sweep);
525    evas_object_hide(sweep);
526    wd->sweep = NULL;
527 #endif
528    return sweep;
529 }
530
531 /**
532  * Set the corner of the bubble
533  *
534  * @param obj The bubble object.
535  * @param corner The given corner for the bubble.
536  *
537  * This function sets the corner of the bubble.
538  * The corner will be used to find the group in the theme
539  * For example, if you set the corner to "bottom_right",
540  * the following group will be searched:
541  * "elm/bubble/bottom_right/default",
542  * considering default style.
543  *
544  * @ingroup Bubble
545  */
546 EAPI void
547 elm_bubble_corner_set(Evas_Object *obj, const char *corner)
548 {
549    ELM_CHECK_WIDTYPE(obj, widtype);
550    Widget_Data *wd = elm_widget_data_get(obj);
551    if (!wd) return;
552    EINA_SAFETY_ON_NULL_RETURN(corner);
553    eina_stringshare_replace(&wd->corner, corner);
554    _theme_hook(obj);
555 }
556
557 /**
558  * Get the corner of the bubble
559  *
560  * @param obj The bubble object.
561  * @return The given corner for the bubble.
562  *
563  * This function gets the corner of the bubble.
564  *
565  * @ingroup Bubble
566  */
567 EAPI const char*
568 elm_bubble_corner_get(const Evas_Object *obj)
569 {
570    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
571    Widget_Data *wd = elm_widget_data_get(obj);
572    if (!wd) return NULL;
573    return wd->corner;
574 }