[elm_index] bug fix: eina_list null check
[framework/uifw/elementary.git] / src / lib / elm_bubble.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3 #include "elm_widget_bubble.h"
4
5 EAPI const char ELM_BUBBLE_SMART_NAME[] = "elm_bubble";
6
7 static const char SIG_CLICKED[] = "clicked";
8
9 static const Elm_Layout_Part_Alias_Description _content_aliases[] =
10 {
11    {"default", "elm.swallow.content"},
12    {"icon", "elm.swallow.icon"},
13    {NULL, NULL}
14 };
15
16 static const Elm_Layout_Part_Alias_Description _text_aliases[] =
17 {
18    {"default", "elm.text"},
19    {"info", "elm.info"},
20    {NULL, NULL}
21 };
22
23 static const Evas_Smart_Cb_Description _smart_callbacks[] =
24 {
25    {SIG_CLICKED, ""},
26    {NULL, NULL}
27 };
28
29 static const char *corner_string[] =
30 {
31    "top_left",
32    "top_right",
33    "bottom_left",
34    "bottom_right"
35 };
36
37 EVAS_SMART_SUBCLASS_NEW
38   (ELM_BUBBLE_SMART_NAME, _elm_bubble, Elm_Bubble_Smart_Class,
39   Elm_Layout_Smart_Class, elm_layout_smart_class_get, _smart_callbacks);
40
41 static void
42 _elm_bubble_smart_sizing_eval(Evas_Object *obj)
43 {
44    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
45
46    ELM_BUBBLE_DATA_GET(obj, sd);
47
48    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
49    edje_object_size_min_restricted_calc
50      (ELM_WIDGET_DATA(sd)->resize_obj, &minw, &minh, minw, minh);
51    evas_object_size_hint_min_set(obj, minw, minh);
52    evas_object_size_hint_max_set(obj, maxw, maxh);
53 }
54
55 static void
56 _on_mouse_up(void *data,
57              Evas *e __UNUSED__,
58              Evas_Object *obj __UNUSED__,
59              void *event_info)
60 {
61    Evas_Event_Mouse_Up *ev = event_info;
62
63    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
64      return;
65
66    evas_object_smart_callback_call(data, SIG_CLICKED, NULL);
67 }
68
69 /* overriding layout's focus_next() in order to just cycle through the
70  * content's tree */
71 static Eina_Bool
72 _elm_bubble_smart_focus_next(const Evas_Object *obj,
73                              Elm_Focus_Direction dir,
74                              Evas_Object **next)
75 {
76    Evas_Object *content;
77
78    ELM_BUBBLE_DATA_GET(obj, sd);
79
80    if ((elm_widget_can_focus_get(obj)) &&
81        (!ELM_WIDGET_DATA(sd)->focused))
82      {
83         // ACCESS
84         *next = (Evas_Object *)obj;
85         return EINA_TRUE;
86      }
87    else
88      {
89         content = elm_layout_content_get(obj, NULL);
90         if (!content) return EINA_FALSE;
91
92         /* attempt to follow focus cycle into sub-object */
93         return elm_widget_focus_next_get(content, dir, next);
94      }
95 }
96
97 static Eina_Bool
98 _elm_bubble_smart_focus_direction(const Evas_Object *obj,
99                                   const Evas_Object *base,
100                                   double degree,
101                                   Evas_Object **direction,
102                                   double *weight)
103 {
104    Evas_Object *content;
105
106    content = elm_layout_content_get(obj, NULL);
107
108    if (!content) return EINA_FALSE;
109
110    /* Try Focus cycle in subitem */
111    return elm_widget_focus_direction_get
112             (content, base, degree, direction, weight);
113 }
114
115 static Eina_Bool
116 _elm_bubble_smart_text_set(Evas_Object *obj,
117                            const char *item,
118                            const char *label)
119 {
120    if (!_elm_bubble_parent_sc->text_set(obj, item, label))
121      return EINA_FALSE;
122
123    if (item && (!strcmp(item, "info") || !strcmp(item, "elm.info")))
124      {
125         if (label)
126           elm_layout_signal_emit(obj, "elm,state,info,visible", "elm");
127         else
128           elm_layout_signal_emit(obj, "elm,state,info,hidden", "elm");
129      }
130
131    elm_layout_sizing_eval(obj);
132
133    return EINA_TRUE;
134 }
135
136 static char *
137 _access_info_cb(void *data __UNUSED__, Evas_Object *obj)
138 {
139    char *ret;
140    Eina_Strbuf *buf;
141    buf = eina_strbuf_new();
142    Evas_Object *content;
143    const char *default_txt = NULL;
144    const char *content_txt = NULL;
145    const char *info_txt = NULL;
146
147    default_txt = elm_widget_access_info_get(obj);
148    if (!default_txt) default_txt = elm_layout_text_get(obj, NULL);
149    if (default_txt) eina_strbuf_append(buf, default_txt);
150
151    content = elm_layout_content_get(obj, NULL);
152    if (content) content_txt = elm_layout_text_get(content, NULL);
153    if (content_txt)
154      {
155         if (!eina_strbuf_length_get(buf))
156           eina_strbuf_append(buf, content_txt);
157         else
158           eina_strbuf_append_printf(buf, ", %s", content_txt);
159      }
160
161
162    info_txt = edje_object_part_text_get(elm_layout_edje_get(obj), "elm.info");
163    if (info_txt)
164      {
165         if (!eina_strbuf_length_get(buf))
166           eina_strbuf_append(buf, info_txt);
167         else
168           eina_strbuf_append_printf(buf, ", %s", info_txt);
169      }
170
171    ret = eina_strbuf_string_steal(buf);
172    eina_strbuf_free(buf);
173    return ret;
174 }
175
176 static void
177 _elm_bubble_smart_add(Evas_Object *obj)
178 {
179    EVAS_SMART_DATA_ALLOC(obj, Elm_Bubble_Smart_Data);
180
181    ELM_WIDGET_CLASS(_elm_bubble_parent_sc)->base.add(obj);
182
183    priv->pos = ELM_BUBBLE_POS_TOP_LEFT; //default
184
185    elm_widget_can_focus_set(obj, EINA_FALSE);
186
187    evas_object_event_callback_add
188      (ELM_WIDGET_DATA(priv)->resize_obj, EVAS_CALLBACK_MOUSE_UP,
189      _on_mouse_up, obj);
190
191    // ACCESS
192    _elm_access_object_register(obj, ELM_WIDGET_DATA(priv)->resize_obj);
193    _elm_access_text_set
194      (_elm_access_object_get(obj), ELM_ACCESS_TYPE, E_("Bubble"));
195    _elm_access_callback_set
196      (_elm_access_object_get(obj), ELM_ACCESS_INFO, _access_info_cb, NULL);
197
198    elm_layout_theme_set(obj, "bubble", "base", elm_widget_style_get(obj));
199
200    elm_layout_sizing_eval(obj);
201
202    if ((_elm_config->access_mode == ELM_ACCESS_MODE_ON))
203      elm_widget_can_focus_set(obj, EINA_TRUE);
204 }
205
206 static void
207 _elm_bubble_smart_access(Evas_Object *obj, Eina_Bool is_access)
208 {
209    ELM_BUBBLE_CHECK(obj);
210
211    if (is_access)
212      elm_widget_can_focus_set(obj, EINA_TRUE);
213    else
214      elm_widget_can_focus_set(obj, EINA_FALSE);
215 }
216
217 static void
218 _elm_bubble_smart_set_user(Elm_Bubble_Smart_Class *sc)
219 {
220    ELM_WIDGET_CLASS(sc)->base.add = _elm_bubble_smart_add;
221
222    ELM_WIDGET_CLASS(sc)->focus_next = _elm_bubble_smart_focus_next;
223    ELM_WIDGET_CLASS(sc)->focus_direction = _elm_bubble_smart_focus_direction;
224    ELM_WIDGET_CLASS(sc)->access = _elm_bubble_smart_access;
225
226    ELM_LAYOUT_CLASS(sc)->text_set = _elm_bubble_smart_text_set;
227    ELM_LAYOUT_CLASS(sc)->sizing_eval = _elm_bubble_smart_sizing_eval;
228
229    ELM_LAYOUT_CLASS(sc)->content_aliases = _content_aliases;
230    ELM_LAYOUT_CLASS(sc)->text_aliases = _text_aliases;
231 }
232
233 EAPI const Elm_Bubble_Smart_Class *
234 elm_bubble_smart_class_get(void)
235 {
236    static Elm_Bubble_Smart_Class _sc =
237      ELM_BUBBLE_SMART_CLASS_INIT_NAME_VERSION(ELM_BUBBLE_SMART_NAME);
238    static const Elm_Bubble_Smart_Class *class = NULL;
239    Evas_Smart_Class *esc = (Evas_Smart_Class *)&_sc;
240
241    if (class)
242      return class;
243
244    _elm_bubble_smart_set(&_sc);
245    esc->callbacks = _smart_callbacks;
246    class = &_sc;
247
248    return class;
249 }
250
251 EAPI Evas_Object *
252 elm_bubble_add(Evas_Object *parent)
253 {
254    Evas_Object *obj;
255
256    EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
257
258    obj = elm_widget_add(_elm_bubble_smart_class_new(), parent);
259    if (!obj) return NULL;
260
261    if (!elm_widget_sub_object_add(parent, obj))
262      ERR("could not add %p as sub object of %p", obj, parent);
263
264    return obj;
265 }
266
267 EAPI void
268 elm_bubble_pos_set(Evas_Object *obj,
269                    Elm_Bubble_Pos pos)
270 {
271    ELM_BUBBLE_CHECK(obj);
272    ELM_BUBBLE_DATA_GET_OR_RETURN(obj, sd);
273
274    if (pos < ELM_BUBBLE_POS_TOP_LEFT || pos > ELM_BUBBLE_POS_BOTTOM_RIGHT)
275      return;
276
277    sd->pos = pos;
278
279    eina_stringshare_replace
280      (&(ELM_LAYOUT_DATA(sd)->group), corner_string[sd->pos]);
281
282    ELM_WIDGET_DATA(sd)->api->theme(obj);
283 }
284
285 EAPI Elm_Bubble_Pos
286 elm_bubble_pos_get(const Evas_Object *obj)
287 {
288    ELM_BUBBLE_CHECK(obj) ELM_BUBBLE_POS_INVALID;
289    ELM_BUBBLE_DATA_GET_OR_RETURN_VAL(obj, sd, ELM_BUBBLE_POS_INVALID);
290
291    return sd->pos;
292 }