oops lost the elm widget sub ob add call. fixed.
[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 void 
38 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
39 {
40    Widget_Data *wd = elm_widget_data_get(obj);
41    if ((!wd) || (!wd->grd)) return;
42    evas_object_grid_mirrored_set(wd->grd, rtl);
43 }
44
45 static void
46 _theme_hook(Evas_Object *obj)
47 {
48    _elm_widget_mirrored_reload(obj);
49    _mirrored_set(obj, elm_widget_mirrored_get(obj));
50 }
51
52 /**
53  * Add a new grid to the parent
54  *
55  * @param parent The parent object
56  * @return The new object or NULL if it cannot be created
57  *
58  * @ingroup Grid
59  */
60 EAPI Evas_Object *
61 elm_grid_add(Evas_Object *parent)
62 {
63    Evas_Object *obj;
64    Evas *e;
65    Widget_Data *wd;
66
67    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
68
69    ELM_SET_WIDTYPE(widtype, "grid");
70    wd->obj = obj;
71    elm_widget_type_set(obj, "grid");
72    elm_widget_sub_object_add(parent, obj);
73    elm_widget_data_set(obj, wd);
74    elm_widget_del_hook_set(obj, _del_hook);
75    elm_widget_theme_hook_set(obj, _theme_hook);
76    elm_widget_can_focus_set(obj, EINA_FALSE);
77
78    wd->grd = evas_object_grid_add(e);
79    evas_object_grid_size_set(wd->grd, 100, 100);
80    elm_widget_resize_object_set(obj, wd->grd);
81    return obj;
82 }
83
84 /**
85  * Set the virtual size of the grid
86  *
87  * @param obj The grid object
88  * @param w The virtual width of the grid
89  * @param h The virtual height of the grid
90  *
91  * @ingroup Grid
92  */
93 EAPI void
94 elm_grid_size_set(Evas_Object *obj, int w, int h)
95 {
96    ELM_CHECK_WIDTYPE(obj, widtype);
97    Widget_Data *wd = elm_widget_data_get(obj);
98    if (!wd) return;
99    evas_object_grid_size_set(wd->grd, w, h);
100 }
101
102 /**
103  * Get the virtual size of the grid
104  * 
105  * @param obj The grid object
106  * @param w Pointer to integer to store the virtual width of the grid
107  * @param h Pointer to integer to store the virtual height of the grid
108  *
109  * @ingroup Grid
110  */
111 EAPI void
112 elm_grid_size_get(Evas_Object *obj, int *w, int *h)
113 {
114    ELM_CHECK_WIDTYPE(obj, widtype);
115    Widget_Data *wd = elm_widget_data_get(obj);
116    if (!wd) return;
117    evas_object_grid_size_get(wd->grd, w, h);
118 }
119
120 /**
121  * Pack child at given position and size
122  *
123  * @param obj The grid object
124  * @param subobj The child to pack
125  * @param x The virtual x coord at which to pack it
126  * @param y The virtual y coord at which to pack it
127  * @param w The virtual width at which to pack it
128  * @param h The virtual height at which to pack it
129  *
130  * @ingroup Grid
131  */
132 EAPI void
133 elm_grid_pack(Evas_Object *obj, Evas_Object *subobj, int x, int y, 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    elm_widget_sub_object_add(obj, subobj);
139    evas_object_grid_pack(wd->grd, subobj, x, y, w, h);
140 }
141
142 /**
143  * Unpack a child from a grid object
144  * 
145  * @param obj The grid object
146  * @param subobj The child to unpack
147  * 
148  * @ingroup Grid
149  */ 
150 EAPI void
151 elm_grid_unpack(Evas_Object *obj, Evas_Object *subobj)
152 {
153    ELM_CHECK_WIDTYPE(obj, widtype);
154    Widget_Data *wd = elm_widget_data_get(obj);
155    if (!wd) return;
156    elm_widget_sub_object_del(obj, subobj);
157    evas_object_grid_unpack(wd->grd, subobj);
158 }
159
160 /**
161  * Faster way to remove all child objects from a grid object.
162  *
163  * @param obj The grid object
164  * @param clear If true, it will delete just removed children
165  *
166  * @ingroup Grid
167  */
168 EAPI void
169 elm_grid_clear(Evas_Object *obj, Eina_Bool clear)
170 {
171    Eina_List *chld;
172    Evas_Object *o;
173    ELM_CHECK_WIDTYPE(obj, widtype);
174    Widget_Data *wd = elm_widget_data_get(obj);
175    if (!wd) return;
176    chld = evas_object_grid_children_get(wd->grd);
177    EINA_LIST_FREE(chld, o) elm_widget_sub_object_del(obj, o);
178    evas_object_grid_clear(wd->grd, clear);
179 }
180
181 /**
182  * Set packing of an existing child at to position and size
183  *
184  * @param subobj The child to set packing of
185  * @param x The virtual x coord at which to pack it
186  * @param y The virtual y coord at which to pack it
187  * @param w The virtual width at which to pack it
188  * @param h The virtual height at which to pack it
189  *
190  * @ingroup Grid
191  */
192 EAPI void
193 elm_grid_pack_set(Evas_Object *subobj, int x, int y, int w, int h)
194 {
195    Evas_Object *obj = elm_widget_parent_widget_get(subobj);
196    ELM_CHECK_WIDTYPE(obj, widtype);
197    Widget_Data *wd = elm_widget_data_get(obj);
198    if (!wd) return;
199    evas_object_grid_pack(wd->grd, subobj, x, y, w, h);
200 }
201
202 /**
203  * get packing of a child
204  *
205  * @param subobj The child to query
206  * @param x Pointer to integer to store the virtual x coord
207  * @param y Pointer to integer to store the virtual y coord
208  * @param w Pointer to integer to store the virtual width
209  * @param h Pointer to integer to store the virtual height
210  *
211  * @ingroup Grid
212  */
213 EAPI void
214 elm_grid_pack_get(Evas_Object *subobj, int *x, int *y, int *w, int *h)
215 {
216    Evas_Object *obj = elm_widget_parent_widget_get(subobj);
217    ELM_CHECK_WIDTYPE(obj, widtype);
218    Widget_Data *wd = elm_widget_data_get(obj);
219    if (!wd) return;
220    evas_object_grid_pack_get(wd->grd, subobj, x, y, w, h);
221 }