Elementary grid: fix focus_next_hook
[framework/uifw/elementary.git] / src / lib / elm_grid.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup Grid Grid
6  *
7  * The grid is a grid layout widget that lays out a series of children as a
8  * fixed "grid" of widgets using a given percentage of the grid width and
9  * height each using the child object.
10  *
11  * The Grid uses a "Virtual resolution" that is stretched to fill the grid
12  * widgets size itself. The default is 100 x 100, so that means the
13  * position and sizes of children will effectively be percentages (0 to 100)
14  * of the width or height of the grid widget
15  *
16  */
17
18 typedef struct _Widget_Data Widget_Data;
19
20 struct _Widget_Data
21 {
22    Evas_Object *obj, *grd;
23 };
24
25 static const char *widtype = NULL;
26 static void _del_hook(Evas_Object *obj);
27 static void _theme_hook(Evas_Object *obj);
28
29 static void
30 _del_hook(Evas_Object *obj)
31 {
32    Widget_Data *wd = elm_widget_data_get(obj);
33    if (!wd) return;
34    free(wd);
35 }
36
37 static Eina_Bool
38 _elm_grid_focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next)
39 {
40    Widget_Data *wd = elm_widget_data_get(obj);
41    const Eina_List *items;
42    void *(*list_data_get) (const Eina_List *list);
43    Eina_List *(*list_free) (Eina_List *list);
44    
45    if ((!wd) || (!wd->grd))
46       return EINA_FALSE;
47    
48    /* Focus chain */
49    /* TODO: Change this to use other chain */
50    if ((items = elm_widget_focus_custom_chain_get(obj)))
51      {
52         list_data_get = eina_list_data_get;
53         list_free = NULL;
54      }
55    else
56      {
57         items = evas_object_table_children_get(wd->grd);
58         list_data_get = eina_list_data_get;
59         list_free = eina_list_free;
60         
61         if (!items) return EINA_FALSE;
62      }
63    
64    Eina_Bool ret = elm_widget_focus_list_next_get(obj, items, list_data_get,
65                                                   dir, next);
66    
67    if (list_free)
68       list_free((Eina_List *)items);
69    
70    return ret;
71 }
72
73 static void
74 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
75 {
76    Widget_Data *wd = elm_widget_data_get(obj);
77    if ((!wd) || (!wd->grd)) return;
78    evas_object_grid_mirrored_set(wd->grd, rtl);
79 }
80
81 static void
82 _theme_hook(Evas_Object *obj)
83 {
84    _elm_widget_mirrored_reload(obj);
85    _mirrored_set(obj, elm_widget_mirrored_get(obj));
86 }
87
88 /**
89  * Add a new grid to the parent
90  *
91  * @param parent The parent object
92  * @return The new object or NULL if it cannot be created
93  *
94  * @ingroup Grid
95  */
96 EAPI Evas_Object *
97 elm_grid_add(Evas_Object *parent)
98 {
99    Evas_Object *obj;
100    Evas *e;
101    Widget_Data *wd;
102
103    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
104
105    ELM_SET_WIDTYPE(widtype, "grid");
106    wd->obj = obj;
107    elm_widget_type_set(obj, "grid");
108    elm_widget_sub_object_add(parent, obj);
109    elm_widget_data_set(obj, wd);
110    elm_widget_del_hook_set(obj, _del_hook);
111    elm_widget_focus_next_hook_set(obj, _elm_grid_focus_next_hook);
112    elm_widget_can_focus_set(obj, EINA_FALSE);
113    elm_widget_theme_hook_set(obj, _theme_hook);
114
115    wd->grd = evas_object_grid_add(e);
116    evas_object_grid_size_set(wd->grd, 100, 100);
117    elm_widget_resize_object_set(obj, wd->grd);
118    
119    _mirrored_set(obj, elm_widget_mirrored_get(obj));
120    return obj;
121 }
122
123 /**
124  * Set the virtual size of the grid
125  *
126  * @param obj The grid object
127  * @param w The virtual width of the grid
128  * @param h The virtual height of the grid
129  *
130  * @ingroup Grid
131  */
132 EAPI void
133 elm_grid_size_set(Evas_Object *obj, int w, int h)
134 {
135    ELM_CHECK_WIDTYPE(obj, widtype);
136    Widget_Data *wd = elm_widget_data_get(obj);
137    if (!wd) return;
138    evas_object_grid_size_set(wd->grd, w, h);
139 }
140
141 /**
142  * Get the virtual size of the grid
143  *
144  * @param obj The grid object
145  * @param w Pointer to integer to store the virtual width of the grid
146  * @param h Pointer to integer to store the virtual height of the grid
147  *
148  * @ingroup Grid
149  */
150 EAPI void
151 elm_grid_size_get(Evas_Object *obj, int *w, int *h)
152 {
153    ELM_CHECK_WIDTYPE(obj, widtype);
154    Widget_Data *wd = elm_widget_data_get(obj);
155    if (!wd) return;
156    evas_object_grid_size_get(wd->grd, w, h);
157 }
158
159 /**
160  * Pack child at given position and size
161  *
162  * @param obj The grid object
163  * @param subobj The child to pack
164  * @param x The virtual x coord at which to pack it
165  * @param y The virtual y coord at which to pack it
166  * @param w The virtual width at which to pack it
167  * @param h The virtual height at which to pack it
168  *
169  * @ingroup Grid
170  */
171 EAPI void
172 elm_grid_pack(Evas_Object *obj, Evas_Object *subobj, int x, int y, int w, int h)
173 {
174    ELM_CHECK_WIDTYPE(obj, widtype);
175    Widget_Data *wd = elm_widget_data_get(obj);
176    if (!wd) return;
177    elm_widget_sub_object_add(obj, subobj);
178    evas_object_grid_pack(wd->grd, subobj, x, y, w, h);
179 }
180
181 /**
182  * Unpack a child from a grid object
183  *
184  * @param obj The grid object
185  * @param subobj The child to unpack
186  *
187  * @ingroup Grid
188  */
189 EAPI void
190 elm_grid_unpack(Evas_Object *obj, Evas_Object *subobj)
191 {
192    ELM_CHECK_WIDTYPE(obj, widtype);
193    Widget_Data *wd = elm_widget_data_get(obj);
194    if (!wd) return;
195    elm_widget_sub_object_del(obj, subobj);
196    evas_object_grid_unpack(wd->grd, subobj);
197 }
198
199 /**
200  * Faster way to remove all child objects from a grid object.
201  *
202  * @param obj The grid object
203  * @param clear If true, it will delete just removed children
204  *
205  * @ingroup Grid
206  */
207 EAPI void
208 elm_grid_clear(Evas_Object *obj, Eina_Bool clear)
209 {
210    Eina_List *chld;
211    Evas_Object *o;
212    ELM_CHECK_WIDTYPE(obj, widtype);
213    Widget_Data *wd = elm_widget_data_get(obj);
214    if (!wd) return;
215    chld = evas_object_grid_children_get(wd->grd);
216    EINA_LIST_FREE(chld, o) elm_widget_sub_object_del(obj, o);
217    evas_object_grid_clear(wd->grd, clear);
218 }
219
220 /**
221  * Set packing of an existing child at to position and size
222  *
223  * @param subobj The child to set packing of
224  * @param x The virtual x coord at which to pack it
225  * @param y The virtual y coord at which to pack it
226  * @param w The virtual width at which to pack it
227  * @param h The virtual height at which to pack it
228  *
229  * @ingroup Grid
230  */
231 EAPI void
232 elm_grid_pack_set(Evas_Object *subobj, int x, int y, int w, int h)
233 {
234    Evas_Object *obj = elm_widget_parent_widget_get(subobj);
235    ELM_CHECK_WIDTYPE(obj, widtype);
236    Widget_Data *wd = elm_widget_data_get(obj);
237    if (!wd) return;
238    evas_object_grid_pack(wd->grd, subobj, x, y, w, h);
239 }
240
241 /**
242  * get packing of a child
243  *
244  * @param subobj The child to query
245  * @param x Pointer to integer to store the virtual x coord
246  * @param y Pointer to integer to store the virtual y coord
247  * @param w Pointer to integer to store the virtual width
248  * @param h Pointer to integer to store the virtual height
249  *
250  * @ingroup Grid
251  */
252 EAPI void
253 elm_grid_pack_get(Evas_Object *subobj, int *x, int *y, int *w, int *h)
254 {
255    Evas_Object *obj = elm_widget_parent_widget_get(subobj);
256    ELM_CHECK_WIDTYPE(obj, widtype);
257    Widget_Data *wd = elm_widget_data_get(obj);
258    if (!wd) return;
259    evas_object_grid_pack_get(wd->grd, subobj, x, y, w, h);
260 }