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