Initialize Tizen 2.3
[framework/uifw/elementary.git] / mobile / src / lib / elm_grid.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3 #include "elm_widget_grid.h"
4
5 EAPI const char ELM_GRID_SMART_NAME[] = "elm_grid";
6
7 EVAS_SMART_SUBCLASS_NEW
8   (ELM_GRID_SMART_NAME, _elm_grid, Elm_Grid_Smart_Class,
9   Elm_Widget_Smart_Class, elm_widget_smart_class_get, NULL);
10
11 static Eina_Bool
12 _elm_grid_smart_focus_next(const Evas_Object *obj,
13                            Elm_Focus_Direction dir,
14                            Evas_Object **next)
15 {
16    Eina_Bool ret;
17    const Eina_List *items;
18    Eina_List *(*list_free)(Eina_List *list);
19    void *(*list_data_get)(const Eina_List *list);
20
21    ELM_GRID_DATA_GET(obj, sd);
22
23    /* Focus chain */
24    /* TODO: Change this to use other chain */
25    if ((items = elm_widget_focus_custom_chain_get(obj)))
26      {
27         list_data_get = eina_list_data_get;
28         list_free = NULL;
29      }
30    else
31      {
32         items = evas_object_grid_children_get(ELM_WIDGET_DATA(sd)->resize_obj);
33         list_data_get = eina_list_data_get;
34         list_free = eina_list_free;
35
36         if (!items) return EINA_FALSE;
37      }
38
39    ret = elm_widget_focus_list_next_get(obj, items, list_data_get, dir, next);
40
41    if (list_free) list_free((Eina_List *)items);
42
43    return ret;
44 }
45
46 static Eina_Bool
47 _elm_grid_smart_focus_direction(const Evas_Object *obj,
48                                 const Evas_Object *base,
49                                 double degree,
50                                 Evas_Object **direction,
51                                 double *weight)
52 {
53    Eina_Bool ret;
54    const Eina_List *items;
55    Eina_List *(*list_free)(Eina_List *list);
56    void *(*list_data_get)(const Eina_List *list);
57
58    ELM_GRID_DATA_GET(obj, sd);
59
60    /* Focus chain */
61    /* TODO: Change this to use other chain */
62    if ((items = elm_widget_focus_custom_chain_get(obj)))
63      {
64         list_data_get = eina_list_data_get;
65         list_free = NULL;
66      }
67    else
68      {
69         items = evas_object_grid_children_get(ELM_WIDGET_DATA(sd)->resize_obj);
70         list_data_get = eina_list_data_get;
71         list_free = eina_list_free;
72
73         if (!items) return EINA_FALSE;
74      }
75
76    ret = elm_widget_focus_list_direction_get(obj, base, items, list_data_get,
77                                              degree, direction, weight);
78
79    if (list_free) list_free((Eina_List *)items);
80
81    return ret;
82 }
83
84 static void
85 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
86 {
87    ELM_GRID_DATA_GET(obj, sd);
88
89    evas_object_grid_mirrored_set(ELM_WIDGET_DATA(sd)->resize_obj, rtl);
90 }
91
92 static Eina_Bool
93 _elm_grid_smart_theme(Evas_Object *obj)
94 {
95    if (!_elm_grid_parent_sc->theme(obj)) return EINA_FALSE;
96
97    _mirrored_set(obj, elm_widget_mirrored_get(obj));
98
99    return EINA_TRUE;
100 }
101
102 static void
103 _elm_grid_smart_add(Evas_Object *obj)
104 {
105    EVAS_SMART_DATA_ALLOC(obj, Elm_Grid_Smart_Data);
106
107    ELM_WIDGET_DATA(priv)->resize_obj =
108      evas_object_grid_add(evas_object_evas_get(obj));
109    evas_object_grid_size_set(ELM_WIDGET_DATA(priv)->resize_obj, 100, 100);
110
111    _elm_grid_parent_sc->base.add(obj);
112
113    elm_widget_can_focus_set(obj, EINA_FALSE);
114
115    _elm_grid_smart_theme(obj);
116 }
117
118 static void
119 _elm_grid_smart_del(Evas_Object *obj)
120 {
121    Eina_List *l;
122    Evas_Object *child;
123
124    ELM_GRID_DATA_GET(obj, sd);
125
126    /* let's make our grid object the *last* to be processed, since it
127     * may (smart) parent other sub objects here */
128    EINA_LIST_FOREACH(ELM_WIDGET_DATA(sd)->subobjs, l, child)
129      {
130         if (child == ELM_WIDGET_DATA(sd)->resize_obj)
131           {
132              ELM_WIDGET_DATA(sd)->subobjs =
133                eina_list_demote_list(ELM_WIDGET_DATA(sd)->subobjs, l);
134              break;
135           }
136      }
137
138    _elm_grid_parent_sc->base.del(obj);
139 }
140
141 static void
142 _elm_grid_smart_set_user(Elm_Grid_Smart_Class *sc)
143 {
144    ELM_WIDGET_CLASS(sc)->base.add = _elm_grid_smart_add;
145    ELM_WIDGET_CLASS(sc)->base.del = _elm_grid_smart_del;
146
147    ELM_WIDGET_CLASS(sc)->theme = _elm_grid_smart_theme;
148    ELM_WIDGET_CLASS(sc)->focus_next = _elm_grid_smart_focus_next;
149    ELM_WIDGET_CLASS(sc)->focus_direction = _elm_grid_smart_focus_direction;
150 }
151
152 EAPI const Elm_Grid_Smart_Class *
153 elm_grid_smart_class_get(void)
154 {
155    static Elm_Grid_Smart_Class _sc =
156      ELM_GRID_SMART_CLASS_INIT_NAME_VERSION(ELM_GRID_SMART_NAME);
157    static const Elm_Grid_Smart_Class *class = NULL;
158
159    if (class) return class;
160
161    _elm_grid_smart_set(&_sc);
162    class = &_sc;
163
164    return class;
165 }
166
167 EAPI Evas_Object *
168 elm_grid_add(Evas_Object *parent)
169 {
170    Evas_Object *obj;
171
172    EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
173
174    obj = elm_widget_add(_elm_grid_smart_class_new(), parent);
175    if (!obj) return NULL;
176
177    if (!elm_widget_sub_object_add(parent, obj))
178      ERR("could not add %p as sub object of %p", obj, parent);
179
180    return obj;
181 }
182
183 EAPI void
184 elm_grid_size_set(Evas_Object *obj,
185                   Evas_Coord w,
186                   Evas_Coord h)
187 {
188    ELM_GRID_CHECK(obj);
189    ELM_GRID_DATA_GET(obj, sd);
190
191    evas_object_grid_size_set(ELM_WIDGET_DATA(sd)->resize_obj, w, h);
192 }
193
194 EAPI void
195 elm_grid_size_get(const Evas_Object *obj,
196                   Evas_Coord *w,
197                   Evas_Coord *h)
198 {
199    ELM_GRID_CHECK(obj);
200    ELM_GRID_DATA_GET(obj, sd);
201
202    evas_object_grid_size_get(ELM_WIDGET_DATA(sd)->resize_obj, w, h);
203 }
204
205 EAPI void
206 elm_grid_pack(Evas_Object *obj,
207               Evas_Object *subobj,
208               Evas_Coord x,
209               Evas_Coord y,
210               Evas_Coord w,
211               Evas_Coord h)
212 {
213    ELM_GRID_CHECK(obj);
214    ELM_GRID_DATA_GET(obj, sd);
215
216    elm_widget_sub_object_add(obj, subobj);
217    evas_object_grid_pack(ELM_WIDGET_DATA(sd)->resize_obj, subobj, x, y, w, h);
218 }
219
220 EAPI void
221 elm_grid_unpack(Evas_Object *obj,
222                 Evas_Object *subobj)
223 {
224    ELM_GRID_CHECK(obj);
225    ELM_GRID_DATA_GET(obj, sd);
226
227    elm_widget_sub_object_del(obj, subobj);
228    evas_object_grid_unpack(ELM_WIDGET_DATA(sd)->resize_obj, subobj);
229 }
230
231 EAPI void
232 elm_grid_clear(Evas_Object *obj,
233                Eina_Bool clear)
234 {
235    Eina_List *chld;
236    Evas_Object *o;
237
238    ELM_GRID_CHECK(obj);
239    ELM_GRID_DATA_GET(obj, sd);
240
241    if (!clear)
242      {
243         chld = evas_object_grid_children_get(ELM_WIDGET_DATA(sd)->resize_obj);
244         EINA_LIST_FREE (chld, o)
245           elm_widget_sub_object_del(obj, o);
246      }
247
248    evas_object_grid_clear(ELM_WIDGET_DATA(sd)->resize_obj, clear);
249 }
250
251 EAPI void
252 elm_grid_pack_set(Evas_Object *subobj,
253                   Evas_Coord x,
254                   Evas_Coord y,
255                   Evas_Coord w,
256                   Evas_Coord h)
257 {
258    Evas_Object *obj = elm_widget_parent_widget_get(subobj);
259
260    ELM_GRID_CHECK(obj);
261    ELM_GRID_DATA_GET(obj, sd);
262
263    evas_object_grid_pack(ELM_WIDGET_DATA(sd)->resize_obj, subobj, x, y, w, h);
264 }
265
266 EAPI void
267 elm_grid_pack_get(Evas_Object *subobj,
268                   int *x,
269                   int *y,
270                   int *w,
271                   int *h)
272 {
273    Evas_Object *obj = elm_widget_parent_widget_get(subobj);
274
275    ELM_GRID_CHECK(obj);
276    ELM_GRID_DATA_GET(obj, sd);
277
278    evas_object_grid_pack_get
279      (ELM_WIDGET_DATA(sd)->resize_obj, subobj, x, y, w, h);
280 }
281
282 EAPI Eina_List *
283 elm_grid_children_get(const Evas_Object *obj)
284 {
285    ELM_GRID_CHECK(obj) NULL;
286    ELM_GRID_DATA_GET(obj, sd);
287
288    return evas_object_grid_children_get(ELM_WIDGET_DATA(sd)->resize_obj);
289 }