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