Merge branch 'master' into svn_merge
[framework/uifw/elementary.git] / src / lib / elm_table.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup Table Table
6  * @ingroup Elementary
7  *
8  * Arranges widgets in a table where items can also span multiple
9  * columns or rows - even overlap (and then be raised or lowered
10  * accordingly to adjust stacking if they do overlap).
11  */
12
13 typedef struct _Widget_Data Widget_Data;
14
15 struct _Widget_Data
16 {
17    Evas_Object *tbl;
18 };
19
20 static const char *widtype = NULL;
21 static void _del_hook(Evas_Object *obj);
22 static void _sizing_eval(Evas_Object *obj);
23 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
24 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
25 static void _theme_hook(Evas_Object *obj);
26
27 static void
28 _del_pre_hook(Evas_Object *obj)
29 {
30    Widget_Data *wd = elm_widget_data_get(obj);
31    if (!wd) return;
32    evas_object_event_callback_del_full
33      (wd->tbl, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
34    evas_object_del(wd->tbl);
35 }
36
37 static void
38 _del_hook(Evas_Object *obj)
39 {
40    Widget_Data *wd = elm_widget_data_get(obj);
41    if (!wd) return;
42    free(wd);
43 }
44
45 static Eina_Bool
46 _elm_table_focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next)
47 {
48    Widget_Data *wd = elm_widget_data_get(obj);
49    const Eina_List *items;
50    void *(*list_data_get) (const Eina_List *list);
51    Eina_List *(*list_free) (Eina_List *list);
52
53    if ((!wd) || (!wd->tbl))
54      return EINA_FALSE;
55
56    /* Focus chain */
57    /* TODO: Change this to use other chain */
58    if ((items = elm_widget_focus_custom_chain_get(obj)))
59      {
60         list_data_get = eina_list_data_get;
61         list_free = NULL;
62      }
63    else
64      {
65         items = evas_object_table_children_get(wd->tbl);
66         list_data_get = eina_list_data_get;
67         list_free = eina_list_free;
68
69         if (!items) return EINA_FALSE;
70      }
71
72    Eina_Bool ret = elm_widget_focus_list_next_get(obj, items, list_data_get,
73                                                    dir, next);
74
75    if (list_free)
76      list_free((Eina_List *)items);
77
78    return ret;
79 }
80
81 static void
82 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
83 {
84    Widget_Data *wd = elm_widget_data_get(obj);
85    if ((!wd) || (!wd->tbl))
86      return;
87
88    evas_object_table_mirrored_set(wd->tbl, rtl);
89 }
90
91 static void
92 _theme_hook(Evas_Object *obj)
93 {
94    _elm_widget_mirrored_reload(obj);
95    _mirrored_set(obj, elm_widget_mirrored_get(obj));
96 }
97
98 static void
99 _sizing_eval(Evas_Object *obj)
100 {
101    Widget_Data *wd = elm_widget_data_get(obj);
102    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
103    Evas_Coord w, h;
104    if (!wd) return;
105    evas_object_size_hint_min_get(wd->tbl, &minw, &minh);
106    evas_object_size_hint_max_get(wd->tbl, &maxw, &maxh);
107    evas_object_size_hint_min_set(obj, minw, minh);
108    evas_object_size_hint_max_set(obj, maxw, maxh);
109    evas_object_geometry_get(obj, NULL, NULL, &w, &h);
110    if (w < minw) w = minw;
111    if (h < minh) h = minh;
112    if ((maxw >= 0) && (w > maxw)) w = maxw;
113    if ((maxh >= 0) && (h > maxh)) h = maxh;
114    evas_object_resize(obj, w, h);
115 }
116
117 static void
118 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
119 {
120    _sizing_eval(data);
121 }
122
123 static void
124 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
125 {
126    _sizing_eval(obj);
127 }
128
129 /**
130  * Add a new table to the parent
131  *
132  * @param parent The parent object
133  * @return The new object or NULL if it cannot be created
134  *
135  * @ingroup Table
136  */
137 EAPI Evas_Object *
138 elm_table_add(Evas_Object *parent)
139 {
140    Evas_Object *obj;
141    Evas *e;
142    Widget_Data *wd;
143
144    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
145
146    ELM_SET_WIDTYPE(widtype, "table");
147    elm_widget_type_set(obj, "table");
148    elm_widget_sub_object_add(parent, obj);
149    elm_widget_data_set(obj, wd);
150    elm_widget_del_hook_set(obj, _del_hook);
151    elm_widget_del_pre_hook_set(obj, _del_pre_hook);
152    elm_widget_focus_next_hook_set(obj, _elm_table_focus_next_hook);
153    elm_widget_can_focus_set(obj, EINA_FALSE);
154    elm_widget_highlight_ignore_set(obj, EINA_FALSE);
155    elm_widget_theme_hook_set(obj, _theme_hook);
156
157    wd->tbl = evas_object_table_add(e);
158    evas_object_event_callback_add(wd->tbl, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
159                                   _changed_size_hints, obj);
160    elm_widget_resize_object_set(obj, wd->tbl);
161
162    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
163
164    _mirrored_set(obj, elm_widget_mirrored_get(obj));
165    return obj;
166 }
167
168 /**
169  * Set the homogeneous layout in the table
170  *
171  * @param obj The layout object
172  * @param homogeneous A boolean to set (or no) layout homogeneous
173  * in the table
174  * (1 = homogeneous,  0 = no homogeneous)
175  *
176  * @ingroup Table
177  */
178 EAPI void
179 elm_table_homogeneous_set(Evas_Object *obj, Eina_Bool homogeneous)
180 {
181    ELM_CHECK_WIDTYPE(obj, widtype);
182    Widget_Data *wd = elm_widget_data_get(obj);
183    if (!wd) return;
184    evas_object_table_homogeneous_set(wd->tbl, homogeneous);
185 }
186
187 EINA_DEPRECATED EAPI void
188 elm_table_homogenous_set(Evas_Object *obj, Eina_Bool homogenous)
189 {
190    elm_table_homogeneous_set(obj, homogenous);
191 }
192
193 /**
194  * Get the current table homogeneous mode.
195  *
196  * @param obj The table object
197  * @return a boolean to set (or no) layout homogeneous in the table
198  * (1 = homogeneous,  0 = no homogeneous)
199  *
200  * @ingroup Table
201  */
202 EAPI Eina_Bool
203 elm_table_homogeneous_get(const Evas_Object *obj)
204 {
205    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
206    Widget_Data *wd = elm_widget_data_get(obj);
207    if (!wd) return EINA_FALSE;
208    return evas_object_table_homogeneous_get(wd->tbl);
209 }
210
211 EINA_DEPRECATED EAPI Eina_Bool
212 elm_table_homogenous_get(const Evas_Object *obj)
213 {
214    return elm_table_homogeneous_get(obj);
215 }
216
217 /**
218  * Set padding between cells.
219  *
220  * @param obj The layout object.
221  * @param horizontal set the horizontal padding.
222  * @param vertical set the vertical padding.
223  *
224  * @ingroup Table
225  */
226 EAPI void
227 elm_table_padding_set(Evas_Object *obj, Evas_Coord horizontal, Evas_Coord vertical)
228 {
229    ELM_CHECK_WIDTYPE(obj, widtype);
230    Widget_Data *wd = elm_widget_data_get(obj);
231    if (!wd) return;
232    evas_object_table_padding_set(wd->tbl, horizontal, vertical);
233 }
234
235 /**
236  * Get padding between cells.
237  *
238  * @param obj The layout object.
239  * @param horizontal set the horizontal padding.
240  * @param vertical set the vertical padding.
241  *
242  * @ingroup Table
243  */
244 EAPI void
245 elm_table_padding_get(const Evas_Object *obj, Evas_Coord *horizontal, Evas_Coord *vertical)
246 {
247    ELM_CHECK_WIDTYPE(obj, widtype);
248    Widget_Data *wd = elm_widget_data_get(obj);
249    if (!wd) return;
250    evas_object_table_padding_get(wd->tbl, horizontal, vertical);
251 }
252
253 /**
254  * Add a subobject on the table with the coordinates passed
255  *
256  * @param obj The table object
257  * @param subobj The subobject to be added to the table
258  * @param x Coordinate to X axis
259  * @param y Coordinate to Y axis
260  * @param w Horizontal length
261  * @param h Vertical length
262  *
263  * @ingroup Table
264  */
265 EAPI void
266 elm_table_pack(Evas_Object *obj, Evas_Object *subobj, int x, int y, int w, int h)
267 {
268    ELM_CHECK_WIDTYPE(obj, widtype);
269    Widget_Data *wd = elm_widget_data_get(obj);
270    if (!wd) return;
271    elm_widget_sub_object_add(obj, subobj);
272    evas_object_table_pack(wd->tbl, subobj, x, y, w, h);
273 }
274
275 /**
276  * Remove child from table.
277  *
278  * @param obj The table object
279  * @param subobj The subobject
280  *
281  * @ingroup Table
282  */
283 EAPI void
284 elm_table_unpack(Evas_Object *obj, Evas_Object *subobj)
285 {
286    ELM_CHECK_WIDTYPE(obj, widtype);
287    Widget_Data *wd = elm_widget_data_get(obj);
288    if (!wd) return;
289    elm_widget_sub_object_del(obj, subobj);
290    evas_object_table_unpack(wd->tbl, subobj);
291 }
292
293 /**
294  * Set the packing location of an existing child of the table
295  *
296  * @param subobj The subobject to be modified in the table
297  * @param x Coordinate to X axis
298  * @param y Coordinate to Y axis
299  * @param w Horizontal length
300  * @param h Vertical length
301  *
302  * @ingroup Table
303  */
304 EAPI void
305 elm_table_pack_set(Evas_Object *subobj, int x, int y, int w, int h)
306 {
307    Evas_Object *obj = elm_widget_parent_widget_get(subobj);
308    ELM_CHECK_WIDTYPE(obj, widtype);
309    Widget_Data *wd = elm_widget_data_get(obj);
310    if (!wd) return;
311    evas_object_table_pack(wd->tbl, subobj, x, y, w, h);
312 }
313
314 /**
315  * Set the packing location of an existing child of the table
316  *
317  * @param subobj The subobject to be modified in the table
318  * @param x Coordinate to X axis
319  * @param y Coordinate to Y axis
320  * @param w Horizontal length
321  * @param h Vertical length
322  *
323  * @ingroup Table
324  */
325 EAPI void
326 elm_table_pack_get(Evas_Object *subobj, int *x, int *y, int *w, int *h)
327 {
328    Evas_Object *obj = elm_widget_parent_widget_get(subobj);
329    unsigned short ix, iy, iw, ih;
330    ELM_CHECK_WIDTYPE(obj, widtype);
331    Widget_Data *wd = elm_widget_data_get(obj);
332    if (!wd) return;
333    evas_object_table_pack_get(wd->tbl, subobj, &ix, &iy, &iw, &ih);
334    if (x) *x = ix;
335    if (y) *y = iy;
336    if (w) *w = iw;
337    if (h) *h = ih;
338 }
339
340 /**
341  * Faster way to remove all child objects from a table object.
342  *
343  * @param obj The table object
344  * @param clear If true, it will delete just removed children
345  *
346  * @ingroup Table
347  */
348 EAPI void
349 elm_table_clear(Evas_Object *obj, Eina_Bool clear)
350 {
351    Eina_List *chld;
352    Evas_Object *o;
353    ELM_CHECK_WIDTYPE(obj, widtype);
354    Widget_Data *wd = elm_widget_data_get(obj);
355    if (!wd) return;
356    chld = evas_object_table_children_get(wd->tbl);
357    EINA_LIST_FREE(chld, o) elm_widget_sub_object_del(obj, o);
358    evas_object_table_clear(wd->tbl, clear);
359 }