elm: Fixed formatting. Added a space after 'if/while/for'
[framework/uifw/elementary.git] / src / lib / elm_bubble.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 typedef struct _Widget_Data Widget_Data;
5
6 struct _Widget_Data
7 {
8    Evas_Object *bbl;
9    Evas_Object *content, *icon;
10    const char *label, *info, *corner;
11    Elm_Bubble_Pos pos;
12 };
13
14 static const char *widtype = NULL;
15 static void _del_hook(Evas_Object *obj);
16 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
17 static void _content_set_hook(Evas_Object *obj, const char *part, Evas_Object *content);
18 static Evas_Object *_content_get_hook(const Evas_Object *obj, const char *part);
19 static Evas_Object *_content_unset_hook(Evas_Object *obj, const char *part);
20 static void _theme_hook(Evas_Object *obj);
21 static void _sizing_eval(Evas_Object *obj);
22 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
23 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
24
25 static const char SIG_CLICKED[] = "clicked";
26
27 static const Evas_Smart_Cb_Description _signals[] =
28 {
29      {SIG_CLICKED, ""},
30      {NULL, NULL}
31 };
32
33 static const char *corner_string[] =
34 {
35     "top_left",
36     "top_right",
37     "bottom_left",
38     "bottom_right"
39 };
40
41 static void
42 _del_hook(Evas_Object *obj)
43 {
44    Widget_Data *wd = elm_widget_data_get(obj);
45    if (!wd) return;
46    if (wd->label) eina_stringshare_del(wd->label);
47    if (wd->info) eina_stringshare_del(wd->info);
48    if (wd->corner) eina_stringshare_del(wd->corner);
49    free(wd);
50 }
51
52 static void
53 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
54 {
55    Widget_Data *wd = elm_widget_data_get(obj);
56    if (!wd) return;
57    edje_object_mirrored_set(wd->bbl, rtl);
58 }
59
60 static void
61 _theme_hook(Evas_Object *obj)
62 {
63    Widget_Data *wd = elm_widget_data_get(obj);
64    if (!wd) return;
65    _elm_widget_mirrored_reload(obj);
66    _mirrored_set(obj, elm_widget_mirrored_get(obj));
67    _elm_theme_object_set(obj, wd->bbl, "bubble", corner_string[wd->pos],
68                          elm_widget_style_get(obj));
69    edje_object_part_text_set(wd->bbl, "elm.text", wd->label);
70    if (wd->label) edje_object_signal_emit(wd->bbl, "elm,state,text,visible", "elm");
71    else edje_object_signal_emit(wd->bbl, "elm,state,text,hidden", "elm");
72    edje_object_part_text_set(wd->bbl, "elm.info", wd->info);
73    if (wd->info) edje_object_signal_emit(wd->bbl, "elm,state,info,visible", "elm");
74    else edje_object_signal_emit(wd->bbl, "elm,state,info,hidden", "elm");
75    if (wd->content)
76      {
77         edje_object_part_swallow(wd->bbl, "elm.swallow.content", wd->content);
78         edje_object_message_signal_process(wd->bbl);
79      }
80    if (wd->icon)
81      edje_object_signal_emit(wd->bbl, "elm,state,icon,visible", "elm");
82    else
83      edje_object_signal_emit(wd->bbl, "elm,state,icon,hidden", "elm");
84    edje_object_scale_set(wd->bbl,
85                          elm_widget_scale_get(obj) * _elm_config->scale);
86    _sizing_eval(obj);
87 }
88
89 static void
90 _content_set(Evas_Object *obj, Evas_Object *content)
91 {
92    Widget_Data *wd = elm_widget_data_get(obj);
93    if (!wd) return;
94
95    if (wd->content == content) return;
96    if (wd->content) evas_object_del(wd->content);
97    wd->content = content;
98    if (content)
99      {
100         elm_widget_sub_object_add(obj, content);
101         evas_object_event_callback_add(content,
102                                        EVAS_CALLBACK_CHANGED_SIZE_HINTS,
103                                        _changed_size_hints, obj);
104         edje_object_part_swallow(wd->bbl, "elm.swallow.content", content);
105      }
106    _sizing_eval(obj);
107 }
108
109 static Evas_Object *
110 _content_unset(Evas_Object *obj)
111 {
112    Widget_Data *wd = elm_widget_data_get(obj);
113    Evas_Object *content;
114    if (!wd) return NULL;
115    if (!wd->content) return NULL;
116    content = wd->content;
117    elm_widget_sub_object_del(obj, content);
118    evas_object_event_callback_del_full(content,
119                                        EVAS_CALLBACK_CHANGED_SIZE_HINTS,
120                                        _changed_size_hints, obj);
121    edje_object_part_unswallow(wd->bbl, content);
122    wd->content = NULL;
123    return content;
124 }
125
126 static void
127 _icon_set(Evas_Object *obj, Evas_Object* icon)
128 {
129    Widget_Data *wd = elm_widget_data_get(obj);
130    if (!wd) return;
131    if (wd->icon == icon) return;
132    if (wd->icon) evas_object_del(wd->icon);
133    wd->icon = icon;
134    if (icon)
135      {
136         elm_widget_sub_object_add(obj, icon);
137         edje_object_part_swallow(wd->bbl, "elm.swallow.icon", icon);
138         evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
139                                        _changed_size_hints, obj);
140         edje_object_signal_emit(wd->bbl, "elm,state,icon,visible", "elm");
141         edje_object_message_signal_process(wd->bbl);
142      }
143    _sizing_eval(obj);
144 }
145
146 static Evas_Object *
147 _icon_unset(Evas_Object *obj)
148 {
149    Widget_Data *wd = elm_widget_data_get(obj);
150    Evas_Object *icon;
151    if (!wd) return NULL;
152    if (!wd->icon) return NULL;
153    icon = wd->icon;
154    elm_widget_sub_object_del(obj, icon);
155    evas_object_event_callback_del_full(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
156                                        _changed_size_hints, obj);
157    edje_object_part_unswallow(wd->bbl, icon);
158    wd->icon = NULL;
159    return icon;
160 }
161
162 static void
163 _content_set_hook(Evas_Object *obj, const char *part, Evas_Object *content)
164 {
165    ELM_CHECK_WIDTYPE(obj, widtype);
166    Widget_Data *wd = elm_widget_data_get(obj);
167    if (!wd) return;
168
169    if (!part || !strcmp(part, "default"))
170      _content_set(obj, content);
171    else if (!strcmp(part, "icon"))
172      _icon_set(obj, content);
173 }
174
175 static Evas_Object *
176 _content_get_hook(const Evas_Object *obj, const char *part)
177 {
178    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
179    Widget_Data *wd = elm_widget_data_get(obj);
180    if (!wd) return NULL;
181    if (!part || !strcmp(part, "default"))
182      return wd->content;
183    else if (!strcmp(part, "icon"))
184      return wd->icon;
185    return NULL;
186 }
187
188 static Evas_Object *
189 _content_unset_hook(Evas_Object *obj, const char *part)
190 {
191    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
192    Widget_Data *wd = elm_widget_data_get(obj);
193    if (!wd) return NULL;
194    if (!part || !strcmp(part, "default"))
195      return _content_unset(obj);
196    else if (!strcmp(part, "icon"))
197      return _icon_unset(obj);
198    return NULL;
199 }
200
201 static Eina_Bool
202 _elm_bubble_focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next)
203 {
204    Widget_Data *wd = elm_widget_data_get(obj);
205    Evas_Object *cur;
206
207    if ((!wd) || (!wd->content))
208      return EINA_FALSE;
209
210    cur = wd->content;
211
212    /* Try Focus cycle in subitem */
213    return elm_widget_focus_next_get(cur, dir, next);
214 }
215
216 static void
217 _sizing_eval(Evas_Object *obj)
218 {
219    Widget_Data *wd = elm_widget_data_get(obj);
220    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
221    if (!wd) return;
222    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
223    edje_object_size_min_restricted_calc(wd->bbl, &minw, &minh, minw, minh);
224    evas_object_size_hint_min_set(obj, minw, minh);
225    evas_object_size_hint_max_set(obj, maxw, maxh);
226 }
227
228 static void
229 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
230 {
231    Widget_Data *wd = elm_widget_data_get(data);
232    if (!wd) return;
233    _sizing_eval(data);
234 }
235
236 static void
237 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
238 {
239    Widget_Data *wd = elm_widget_data_get(obj);
240    Evas_Object *sub = event_info;
241    if (!wd) return;
242    evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
243                                        _changed_size_hints, obj);
244    if (sub == wd->content) wd->content = NULL;
245    else if (sub == wd->icon)
246      {
247         edje_object_signal_emit(wd->bbl, "elm,state,icon,hidden", "elm");
248         wd->icon = NULL;
249         edje_object_message_signal_process(wd->bbl);
250      }
251    _sizing_eval(obj);
252 }
253
254 static void
255 _mouse_up(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
256 {
257    Evas_Event_Mouse_Up *ev = event_info;
258    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
259      return;
260    evas_object_smart_callback_call(data, SIG_CLICKED, NULL);
261 }
262
263 static void
264 _elm_bubble_label_set(Evas_Object *obj, const char *item, const char *label)
265 {
266    ELM_CHECK_WIDTYPE(obj, widtype);
267    Widget_Data *wd = elm_widget_data_get(obj);
268    if (!wd) return;
269
270    if (!item || !strcmp(item, "default"))
271      {
272         eina_stringshare_replace(&wd->label, label);
273         edje_object_part_text_set(wd->bbl, "elm.text", label);
274         if (label) edje_object_signal_emit(wd->bbl, "elm,state,text,visible",
275               "elm");
276         else edje_object_signal_emit(wd->bbl, "elm,state,text,hidden", "elm");
277         _sizing_eval(obj);
278      }
279    else if (!strcmp(item, "info"))
280      {
281         eina_stringshare_replace(&wd->info, label);
282         edje_object_part_text_set(wd->bbl, "elm.info", label);
283         if (label) edje_object_signal_emit(wd->bbl, "elm,state,info,visible",
284               "elm");
285         else edje_object_signal_emit(wd->bbl, "elm,state,info,hidden", "elm");
286         _sizing_eval(obj);
287      }
288 }
289
290 static const char*
291 _elm_bubble_label_get(const Evas_Object *obj, const char *item)
292 {
293    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
294    Widget_Data *wd = elm_widget_data_get(obj);
295    if (!wd) return NULL;
296
297    if (!item || !strcmp(item, "default"))
298      {
299         return wd->label;
300      }
301    else if (!strcmp(item, "info"))
302      {
303         return wd->info;
304      }
305
306    return NULL;
307 }
308
309 EAPI Evas_Object *
310 elm_bubble_add(Evas_Object *parent)
311 {
312    Evas_Object *obj;
313    Evas *e;
314    Widget_Data *wd;
315
316    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
317
318    ELM_SET_WIDTYPE(widtype, "bubble");
319    elm_widget_type_set(obj, "bubble");
320    elm_widget_sub_object_add(parent, obj);
321    elm_widget_data_set(obj, wd);
322    elm_widget_del_hook_set(obj, _del_hook);
323    elm_widget_theme_hook_set(obj, _theme_hook);
324    elm_widget_focus_next_hook_set(obj, _elm_bubble_focus_next_hook);
325    elm_widget_can_focus_set(obj, EINA_FALSE);
326    elm_widget_text_set_hook_set(obj, _elm_bubble_label_set);
327    elm_widget_text_get_hook_set(obj, _elm_bubble_label_get);
328    elm_widget_content_set_hook_set(obj, _content_set_hook);
329    elm_widget_content_get_hook_set(obj, _content_get_hook);
330    elm_widget_content_unset_hook_set(obj, _content_unset_hook);
331
332    wd->corner = eina_stringshare_add("base");
333    wd->pos = ELM_BUBBLE_POS_TOP_LEFT; //default
334
335    wd->bbl = edje_object_add(e);
336    elm_widget_resize_object_set(obj, wd->bbl);
337
338    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
339    evas_object_event_callback_add(wd->bbl, EVAS_CALLBACK_MOUSE_UP,
340                                   _mouse_up, obj);
341
342    evas_object_smart_callbacks_descriptions_set(obj, _signals);
343    _mirrored_set(obj, elm_widget_mirrored_get(obj));
344    _elm_theme_object_set(obj, wd->bbl, "bubble", wd->corner,
345                          elm_widget_style_get(obj));
346    _sizing_eval(obj);
347    return obj;
348 }
349
350 EAPI void
351 elm_bubble_pos_set(Evas_Object *obj, Elm_Bubble_Pos pos)
352 {
353    ELM_CHECK_WIDTYPE(obj, widtype);
354    Widget_Data *wd = elm_widget_data_get(obj);
355    if (!wd) return;
356    if (pos<ELM_BUBBLE_POS_TOP_LEFT || pos>ELM_BUBBLE_POS_BOTTOM_RIGHT) return;
357    wd->pos = pos;
358    _theme_hook(obj);
359 }
360
361 EAPI Elm_Bubble_Pos
362 elm_bubble_pos_get(const Evas_Object *obj)
363 {
364    ELM_CHECK_WIDTYPE(obj, widtype) ELM_BUBBLE_POS_INVALID;
365    Widget_Data *wd = elm_widget_data_get(obj);
366    if (!wd) return ELM_BUBBLE_POS_INVALID;
367    return wd->pos;
368 }