2ee7a2e5eb08c669d6fd7876a8e1ce1486653d23
[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  *
7  * The Bubble is an widget used to show a text in a frame as speech is
8  * represented in comics.
9  *
10  * Signals that you can add callbacks for are:
11  *
12  * "clicked" - This is called when a user has clicked the bubble.
13  */
14
15 typedef struct _Widget_Data Widget_Data;
16
17 struct _Widget_Data
18 {
19    Evas_Object *bbl;
20    Evas_Object *content, *icon;
21    const char *label, *info, *corner;
22 };
23
24 static const char *widtype = NULL;
25 static void _del_hook(Evas_Object *obj);
26 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
27 static void _theme_hook(Evas_Object *obj);
28 static void _sizing_eval(Evas_Object *obj);
29 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
30 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
31
32 static const char SIG_CLICKED[] = "clicked";
33
34 static const Evas_Smart_Cb_Description _signals[] =
35 {
36      {SIG_CLICKED, ""},
37      {NULL, NULL}
38 };
39
40 static void
41 _del_hook(Evas_Object *obj)
42 {
43    Widget_Data *wd = elm_widget_data_get(obj);
44    if (!wd) return;
45    if (wd->label) eina_stringshare_del(wd->label);
46    if (wd->info) eina_stringshare_del(wd->info);
47    if (wd->corner) eina_stringshare_del(wd->corner);
48    free(wd);
49 }
50
51 static void
52 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
53 {
54    Widget_Data *wd = elm_widget_data_get(obj);
55    if (!wd) return;
56    edje_object_mirrored_set(wd->bbl, rtl);
57 }
58
59 static void
60 _theme_hook(Evas_Object *obj)
61 {
62    Widget_Data *wd = elm_widget_data_get(obj);
63    if (!wd) return;
64    _elm_widget_mirrored_reload(obj);
65    _mirrored_set(obj, elm_widget_mirrored_get(obj));
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 static void
142 _mouse_up(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
143 {
144    Evas_Event_Mouse_Up *ev = event_info;
145    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
146      return;
147    evas_object_smart_callback_call(data, SIG_CLICKED, NULL);
148 }
149
150 /**
151  * Add a new bubble to the parent
152  *
153  * @param parent The parent object
154  * @return The new object or NULL if it cannot be created
155  *
156  * This function adds a text bubble to the given parent evas object.
157  *
158  * @ingroup Bubble
159  */
160 EAPI Evas_Object *
161 elm_bubble_add(Evas_Object *parent)
162 {
163    Evas_Object *obj;
164    Evas *e;
165    Widget_Data *wd;
166
167    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
168
169    ELM_SET_WIDTYPE(widtype, "bubble");
170    elm_widget_type_set(obj, "bubble");
171    elm_widget_sub_object_add(parent, obj);
172    elm_widget_data_set(obj, wd);
173    elm_widget_del_hook_set(obj, _del_hook);
174    elm_widget_theme_hook_set(obj, _theme_hook);
175    elm_widget_focus_next_hook_set(obj, _elm_bubble_focus_next_hook);
176    elm_widget_can_focus_set(obj, EINA_FALSE);
177
178    wd->corner = eina_stringshare_add("base");
179
180    wd->bbl = edje_object_add(e);
181    elm_widget_resize_object_set(obj, wd->bbl);
182
183    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
184    evas_object_event_callback_add(wd->bbl, EVAS_CALLBACK_MOUSE_UP,
185                                   _mouse_up, obj);
186
187    evas_object_smart_callbacks_descriptions_set(obj, _signals);
188    _mirrored_set(obj, elm_widget_mirrored_get(obj));
189    _elm_theme_object_set(obj, wd->bbl, "bubble", wd->corner,
190                          elm_widget_style_get(obj));
191    _sizing_eval(obj);
192    return obj;
193 }
194
195 /**
196  * Set the label of the bubble
197  *
198  * @param obj The bubble object
199  * @param label The string to set in the label
200  *
201  * This function sets the title of the bubble that is shown on top of
202  * the bubble.
203  *
204  * @ingroup Bubble
205  */
206 EAPI void
207 elm_bubble_label_set(Evas_Object *obj, const char *label)
208 {
209    ELM_CHECK_WIDTYPE(obj, widtype);
210    Widget_Data *wd = elm_widget_data_get(obj);
211    if (!wd) return;
212    eina_stringshare_replace(&wd->label, label);
213    edje_object_part_text_set(wd->bbl, "elm.text", label);
214    if (label) edje_object_signal_emit(wd->bbl, "elm,state,text,visible", "elm");
215    else edje_object_signal_emit(wd->bbl, "elm,state,text,hidden", "elm");
216    _sizing_eval(obj);
217 }
218
219 /**
220  * Get the label of the bubble
221  *
222  * @param obj The bubble object
223  * @return The string of set in the label
224  *
225  * This function gets the title of the bubble that is shown on top of
226  * the bubble.
227  *
228  * @ingroup Bubble
229  */
230 EAPI const char*
231 elm_bubble_label_get(const Evas_Object *obj)
232 {
233    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
234    Widget_Data *wd = elm_widget_data_get(obj);
235    if (!wd) return NULL;
236    return wd->label;
237 }
238
239 /**
240  * Set the info of the bubble
241  *
242  * @param obj The bubble object
243  * @param info The given info about the bubble
244  *
245  * This function sets the text shown on the top right of bubble.
246  * In the Anchorblock example of the Elementary tests application it
247  * shows time.
248  *
249  * @ingroup Bubble
250  *
251  */
252 EAPI void
253 elm_bubble_info_set(Evas_Object *obj, const char *info)
254 {
255    ELM_CHECK_WIDTYPE(obj, widtype);
256    Widget_Data *wd = elm_widget_data_get(obj);
257    if (!wd) return;
258    eina_stringshare_replace(&wd->info, info);
259    edje_object_part_text_set(wd->bbl, "elm.info", info);
260    if (info) edje_object_signal_emit(wd->bbl, "elm,state,info,visible", "elm");
261    else edje_object_signal_emit(wd->bbl, "elm,state,info,hidden", "elm");
262    _sizing_eval(obj);
263 }
264
265 /**
266  * Get the info of the bubble
267  *
268  * @param obj The bubble object
269  *
270  * @return The "info" string of the bubble
271  *
272  * This function gets the text set to be displayed at the top right of
273  * the bubble.
274  *
275  * @ingroup Bubble
276  *
277  */
278 EAPI const char *
279 elm_bubble_info_get(const Evas_Object *obj)
280 {
281    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
282    Widget_Data *wd = elm_widget_data_get(obj);
283    if (!wd) return NULL;
284    return wd->info;
285 }
286
287 /**
288  * Set the content to be shown in the bubble
289  *
290  * Once the content object is set, a previously set one will be deleted.
291  * If you want to keep the old content object, use the
292  * elm_bubble_content_unset() function.
293  *
294  * @param obj The bubble object
295  * @param content The given content of the bubble
296  *
297  * This function sets the content shown on the middle of the bubble.
298  * In the Anchorblock example of the Elementary tests application it
299  * shows time.
300  *
301  * @ingroup Bubble
302  */
303 EAPI void
304 elm_bubble_content_set(Evas_Object *obj, Evas_Object *content)
305 {
306    ELM_CHECK_WIDTYPE(obj, widtype);
307    Widget_Data *wd = elm_widget_data_get(obj);
308    if (!wd) return;
309    if (wd->content == content) return;
310    if (wd->content) evas_object_del(wd->content);
311    wd->content = content;
312    if (content)
313      {
314         elm_widget_sub_object_add(obj, content);
315         evas_object_event_callback_add(content, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
316                                        _changed_size_hints, obj);
317         edje_object_part_swallow(wd->bbl, "elm.swallow.content", content);
318      }
319    _sizing_eval(obj);
320 }
321
322 /**
323  * Get the content shown in the bubble
324  *
325  * Return the content object which is set for this widget.
326  *
327  * @param obj The bubble object
328  * @return The content that is being used
329  *
330  * @ingroup Bubble
331  */
332 EAPI Evas_Object *
333 elm_bubble_content_get(const Evas_Object *obj)
334 {
335    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
336    Widget_Data *wd = elm_widget_data_get(obj);
337    if (!wd) return NULL;
338    return wd->content;
339 }
340
341 /**
342  * Unset the content shown in the bubble
343  *
344  * Unparent and return the content object which was set for this widget.
345  *
346  * @param obj The bubble object
347  * @return The content that was being used
348  *
349  * @ingroup Bubble
350  */
351 EAPI Evas_Object *
352 elm_bubble_content_unset(Evas_Object *obj)
353 {
354    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
355    Widget_Data *wd = elm_widget_data_get(obj);
356    Evas_Object *content;
357    if (!wd) return NULL;
358    if (!wd->content) return NULL;
359    content = wd->content;
360    elm_widget_sub_object_del(obj, content);
361    edje_object_part_unswallow(wd->bbl, content);
362    wd->content = NULL;
363    return content;
364 }
365
366 /**
367  * Set the icon of the bubble
368  *
369  * Once the icon object is set, a previously set one will be deleted.
370  * If you want to keep the old content object, use the
371  * elm_icon_content_unset() function.
372  *
373  * @param obj The bubble object
374  * @param icon The given icon for the bubble
375  *
376  * @ingroup Bubble
377  */
378 EAPI void
379 elm_bubble_icon_set(Evas_Object *obj, Evas_Object *icon)
380 {
381    ELM_CHECK_WIDTYPE(obj, widtype);
382    Widget_Data *wd = elm_widget_data_get(obj);
383    if (!wd) return;
384    if (wd->icon == icon) return;
385    if (wd->icon) evas_object_del(wd->icon);
386    wd->icon = icon;
387    if (icon)
388      {
389         elm_widget_sub_object_add(obj, icon);
390         edje_object_part_swallow(wd->bbl, "elm.swallow.icon", icon);
391         evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
392                                        _changed_size_hints, obj);
393         edje_object_signal_emit(wd->bbl, "elm,state,icon,visible", "elm");
394         edje_object_message_signal_process(wd->bbl);
395      }
396    _sizing_eval(obj);
397 }
398
399 /**
400  * Get the icon of the bubble
401  *
402  * @param obj The bubble object
403  * @return The icon for the bubble
404  *
405  * This function gets the icon shown on the top left of bubble.
406  *
407  * @ingroup Bubble
408  */
409 EAPI Evas_Object *
410 elm_bubble_icon_get(const Evas_Object *obj)
411 {
412    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
413    Widget_Data *wd = elm_widget_data_get(obj);
414    if (!wd) return NULL;
415    return wd->icon;
416 }
417
418 /**
419  * Unset the icon of the bubble
420  *
421  * Unparent and return the icon object which was set for this widget.
422  *
423  * @param obj The bubble object
424  * @return The icon that was being used
425  *
426  * @ingroup Bubble
427  */
428 EAPI Evas_Object *
429 elm_bubble_icon_unset(Evas_Object *obj)
430 {
431    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
432    Widget_Data *wd = elm_widget_data_get(obj);
433    Evas_Object *icon;
434    if (!wd) return NULL;
435    if (!wd->icon) return NULL;
436    icon = wd->icon;
437    elm_widget_sub_object_del(obj, icon);
438    edje_object_part_unswallow(wd->bbl, icon);
439    wd->icon = NULL;
440    return icon;
441 }
442
443 /**
444  * Set the corner of the bubble
445  *
446  * @param obj The bubble object.
447  * @param corner The given corner for the bubble.
448  *
449  * This function sets the corner of the bubble.
450  * The corner will be used to find the group in the theme
451  * For example, if you set the corner to "bottom_right",
452  * the following group will be searched:
453  * "elm/bubble/bottom_right/default",
454  * considering default style.
455  *
456  * @ingroup Bubble
457  */
458 EAPI void
459 elm_bubble_corner_set(Evas_Object *obj, const char *corner)
460 {
461    ELM_CHECK_WIDTYPE(obj, widtype);
462    Widget_Data *wd = elm_widget_data_get(obj);
463    if (!wd) return;
464    EINA_SAFETY_ON_NULL_RETURN(corner);
465    eina_stringshare_replace(&wd->corner, corner);
466    _theme_hook(obj);
467 }
468
469 /**
470  * Get the corner of the bubble
471  *
472  * @param obj The bubble object.
473  * @return The given corner for the bubble.
474  *
475  * This function gets the corner of the bubble.
476  *
477  * @ingroup Bubble
478  */
479 EAPI const char*
480 elm_bubble_corner_get(const Evas_Object *obj)
481 {
482    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
483    Widget_Data *wd = elm_widget_data_get(obj);
484    if (!wd) return NULL;
485    return wd->corner;
486 }