elm grid obj++
[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    evas_object_grid_pack(wd->grd, subobj, x, y, w, h);
139 }
140
141 /**
142  * Unpack a child from a grid object
143  * 
144  * @param obj The grid object
145  * @param subobj The child to unpack
146  * 
147  * @ingroup Grid
148  */ 
149 EAPI void
150 elm_grid_unpack(Evas_Object *obj, Evas_Object *subobj)
151 {
152    ELM_CHECK_WIDTYPE(obj, widtype);
153    Widget_Data *wd = elm_widget_data_get(obj);
154    if (!wd) return;
155    elm_widget_sub_object_del(obj, subobj);
156    evas_object_grid_unpack(wd->grd, subobj);
157 }
158
159 /**
160  * Faster way to remove all child objects from a grid object.
161  *
162  * @param obj The grid object
163  * @param clear If true, it will delete just removed children
164  *
165  * @ingroup Grid
166  */
167 EAPI void
168 elm_grid_clear(Evas_Object *obj, Eina_Bool clear)
169 {
170    Eina_List *chld;
171    Evas_Object *o;
172    ELM_CHECK_WIDTYPE(obj, widtype);
173    Widget_Data *wd = elm_widget_data_get(obj);
174    if (!wd) return;
175    chld = evas_object_grid_children_get(wd->grd);
176    EINA_LIST_FREE(chld, o) elm_widget_sub_object_del(obj, o);
177    evas_object_grid_clear(wd->grd, clear);
178 }
179
180 /**
181  * Set packing of an existing child at to position and size
182  *
183  * @param subobj The child to set packing of
184  * @param x The virtual x coord at which to pack it
185  * @param y The virtual y coord at which to pack it
186  * @param w The virtual width at which to pack it
187  * @param h The virtual height at which to pack it
188  *
189  * @ingroup Grid
190  */
191 EAPI void
192 elm_grid_pack_set(Evas_Object *subobj, int x, int y, int w, int h)
193 {
194    Evas_Object *obj = elm_widget_parent_widget_get(subobj);
195    ELM_CHECK_WIDTYPE(obj, widtype);
196    Widget_Data *wd = elm_widget_data_get(obj);
197    if (!wd) return;
198    evas_object_grid_pack(wd->grd, subobj, x, y, w, h);
199 }
200
201 /**
202  * get packing of a child
203  *
204  * @param subobj The child to query
205  * @param x Pointer to integer to store the virtual x coord
206  * @param y Pointer to integer to store the virtual y coord
207  * @param w Pointer to integer to store the virtual width
208  * @param h Pointer to integer to store the virtual height
209  *
210  * @ingroup Grid
211  */
212 EAPI void
213 elm_grid_pack_get(Evas_Object *subobj, int *x, int *y, int *w, int *h)
214 {
215    Evas_Object *obj = elm_widget_parent_widget_get(subobj);
216    ELM_CHECK_WIDTYPE(obj, widtype);
217    Widget_Data *wd = elm_widget_data_get(obj);
218    if (!wd) return;
219    evas_object_grid_pack_get(wd->grd, subobj, x, y, w, h);
220 }