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