ac5e997b3a66d0c06e231a2d21f2f2954ddb0f8e
[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
26 static void
27 _del_pre_hook(Evas_Object *obj)
28 {
29    Widget_Data *wd = elm_widget_data_get(obj);
30    if (!wd) return;
31    evas_object_event_callback_del_full
32      (wd->tbl, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
33    evas_object_del(wd->tbl);
34 }
35
36 static void
37 _del_hook(Evas_Object *obj)
38 {
39    Widget_Data *wd = elm_widget_data_get(obj);
40    if (!wd) return;
41    free(wd);
42 }
43
44 static void
45 _sizing_eval(Evas_Object *obj)
46 {
47    Widget_Data *wd = elm_widget_data_get(obj);
48    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
49    Evas_Coord w, h;
50    if (!wd) return;
51    evas_object_size_hint_min_get(wd->tbl, &minw, &minh);
52    evas_object_size_hint_max_get(wd->tbl, &maxw, &maxh);
53    evas_object_size_hint_min_set(obj, minw, minh);
54    evas_object_size_hint_max_set(obj, maxw, maxh);
55    evas_object_geometry_get(obj, NULL, NULL, &w, &h);
56    if (w < minw) w = minw;
57    if (h < minh) h = minh;
58    if ((maxw >= 0) && (w > maxw)) w = maxw;
59    if ((maxh >= 0) && (h > maxh)) h = maxh;
60    evas_object_resize(obj, w, h);
61 }
62
63 static void
64 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
65 {
66    _sizing_eval(data);
67 }
68
69 static void
70 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
71 {
72    /* We do not add this callback, consequently we do not need to delete it
73    
74    Widget_Data *wd = elm_widget_data_get(obj);
75    evas_Object *sub = event_info;
76
77    evas_object_event_callback_del_full
78      (sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
79      */
80    _sizing_eval(obj);
81 }
82
83 /**
84  * Add a new table to the parent
85  *
86  * @param parent The parent object
87  * @return The new object or NULL if it cannot be created
88  *
89  * @ingroup Table
90  */
91 EAPI Evas_Object *
92 elm_table_add(Evas_Object *parent)
93 {
94    Evas_Object *obj;
95    Evas *e;
96    Widget_Data *wd;
97
98    wd = ELM_NEW(Widget_Data);
99    e = evas_object_evas_get(parent);
100    obj = elm_widget_add(e);
101    ELM_SET_WIDTYPE(widtype, "table");
102    elm_widget_type_set(obj, "table");
103    elm_widget_sub_object_add(parent, obj);
104    elm_widget_data_set(obj, wd);
105    elm_widget_del_hook_set(obj, _del_hook);
106    elm_widget_del_pre_hook_set(obj, _del_pre_hook);
107
108    wd->tbl = evas_object_table_add(e);
109    evas_object_event_callback_add(wd->tbl, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
110                                   _changed_size_hints, obj);
111    elm_widget_resize_object_set(obj, wd->tbl);
112
113    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
114
115    return obj;
116 }
117
118 /**
119  * Set the homogenous layout in the table
120  *
121  * @param obj The layout object
122  * @param homogenous A boolean to set (or no) layout homogenous
123  * in the table
124  * (1 = homogenous,  0 = no homogenous)
125  *
126  * @ingroup Table
127  */
128 EAPI void
129 elm_table_homogenous_set(Evas_Object *obj, Eina_Bool homogenous)
130 {
131    ELM_CHECK_WIDTYPE(obj, widtype);
132    Widget_Data *wd = elm_widget_data_get(obj);
133    if (!wd) return;
134    evas_object_table_homogeneous_set(wd->tbl, homogenous);
135 }
136
137 /**
138  * Set padding between cells.
139  *
140  * @param obj The layout object.
141  * @param horizontal set the horizontal padding. 
142  * @param vertical set the vertical padding.
143  *
144  * @ingroup Table
145  */
146 EAPI void
147 elm_table_padding_set(Evas_Object *obj, Evas_Coord horizontal, Evas_Coord vertical)
148 {
149    ELM_CHECK_WIDTYPE(obj, widtype);
150    Widget_Data *wd = elm_widget_data_get(obj);
151    if (!wd) return;
152    evas_object_table_padding_set(wd->tbl, horizontal, vertical);
153 }
154
155 /**
156  * Add a subobject on the table with the coordinates passed
157  *
158  * @param obj The table object
159  * @param subobj The subobject to be added to the table
160  * @param x Coordinate to X axis
161  * @param y Coordinate to Y axis
162  * @param w Horizontal length
163  * @param h Vertical length
164  *
165  * @ingroup Table
166  */
167 EAPI void
168 elm_table_pack(Evas_Object *obj, Evas_Object *subobj, int x, int y, int w, int h)
169 {
170    ELM_CHECK_WIDTYPE(obj, widtype);
171    Widget_Data *wd = elm_widget_data_get(obj);
172    if (!wd) return;
173    elm_widget_sub_object_add(obj, subobj);
174    evas_object_table_pack(wd->tbl, subobj, x, y, w, h);
175 }
176
177 /**
178  * Remove child from table.
179  *
180  * @param obj The table object
181  * @param subobj The subobject
182  *
183  * @ingroup Table
184  */
185 EAPI void
186 elm_table_unpack(Evas_Object *obj, Evas_Object *subobj)
187 {
188    ELM_CHECK_WIDTYPE(obj, widtype);
189    Widget_Data *wd = elm_widget_data_get(obj);
190    if (!wd) return;
191    elm_widget_sub_object_del(obj, subobj);
192    evas_object_table_unpack(wd->tbl, subobj);
193 }
194
195 /**
196  * Faster way to remove all child objects from a table object.
197  *
198  * @param obj The table object
199  * @param clear If true, it will delete just removed children
200  *
201  * @ingroup Table
202  */
203 EAPI void
204 elm_table_clear(Evas_Object *obj, Eina_Bool clear)
205 {
206    ELM_CHECK_WIDTYPE(obj, widtype);
207    Widget_Data *wd = elm_widget_data_get(obj);
208    if (!wd) return;
209    evas_object_table_clear(wd->tbl, clear);
210 }