Merge "[Password]: New design based changes, a new style removed password mode contro...
[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 homogenous layout in the table
170  *
171  * @param obj The layout object
172  * @param homogenous A boolean to set (or no) layout homogenous
173  * in the table
174  * (1 = homogenous,  0 = no homogenous)
175  *
176  * @ingroup Table
177  */
178 EAPI void
179 elm_table_homogenous_set(Evas_Object *obj, Eina_Bool homogenous)
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, homogenous);
185 }
186
187 /**
188  * Get the current table homogenous mode.
189  *
190  * @param obj The table object
191  * @return a boolean to set (or no) layout homogenous in the table
192  * (1 = homogenous,  0 = no homogenous)
193  *
194  * @ingroup Table
195  */
196 EAPI Eina_Bool
197 elm_table_homogenous_get(const Evas_Object *obj)
198 {
199    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
200    Widget_Data *wd = elm_widget_data_get(obj);
201    if (!wd) return EINA_FALSE;
202    return evas_object_table_homogeneous_get(wd->tbl);
203 }
204
205 /**
206  * Set padding between cells.
207  *
208  * @param obj The layout object.
209  * @param horizontal set the horizontal padding.
210  * @param vertical set the vertical padding.
211  *
212  * @ingroup Table
213  */
214 EAPI void
215 elm_table_padding_set(Evas_Object *obj, Evas_Coord horizontal, Evas_Coord vertical)
216 {
217    ELM_CHECK_WIDTYPE(obj, widtype);
218    Widget_Data *wd = elm_widget_data_get(obj);
219    if (!wd) return;
220    evas_object_table_padding_set(wd->tbl, horizontal, vertical);
221 }
222
223 /**
224  * Get padding between cells.
225  *
226  * @param obj The layout object.
227  * @param horizontal set the horizontal padding.
228  * @param vertical set the vertical padding.
229  *
230  * @ingroup Table
231  */
232 EAPI void
233 elm_table_padding_get(const Evas_Object *obj, Evas_Coord *horizontal, Evas_Coord *vertical)
234 {
235    ELM_CHECK_WIDTYPE(obj, widtype);
236    Widget_Data *wd = elm_widget_data_get(obj);
237    if (!wd) return;
238    evas_object_table_padding_get(wd->tbl, horizontal, vertical);
239 }
240
241 /**
242  * Add a subobject on the table with the coordinates passed
243  *
244  * @param obj The table object
245  * @param subobj The subobject to be added to the table
246  * @param x Coordinate to X axis
247  * @param y Coordinate to Y axis
248  * @param w Horizontal length
249  * @param h Vertical length
250  *
251  * @ingroup Table
252  */
253 EAPI void
254 elm_table_pack(Evas_Object *obj, Evas_Object *subobj, int x, int y, int w, int h)
255 {
256    ELM_CHECK_WIDTYPE(obj, widtype);
257    Widget_Data *wd = elm_widget_data_get(obj);
258    if (!wd) return;
259    elm_widget_sub_object_add(obj, subobj);
260    evas_object_table_pack(wd->tbl, subobj, x, y, w, h);
261 }
262
263 /**
264  * Remove child from table.
265  *
266  * @param obj The table object
267  * @param subobj The subobject
268  *
269  * @ingroup Table
270  */
271 EAPI void
272 elm_table_unpack(Evas_Object *obj, Evas_Object *subobj)
273 {
274    ELM_CHECK_WIDTYPE(obj, widtype);
275    Widget_Data *wd = elm_widget_data_get(obj);
276    if (!wd) return;
277    elm_widget_sub_object_del(obj, subobj);
278    evas_object_table_unpack(wd->tbl, subobj);
279 }
280
281 /**
282  * Faster way to remove all child objects from a table object.
283  *
284  * @param obj The table object
285  * @param clear If true, it will delete just removed children
286  *
287  * @ingroup Table
288  */
289 EAPI void
290 elm_table_clear(Evas_Object *obj, Eina_Bool clear)
291 {
292    ELM_CHECK_WIDTYPE(obj, widtype);
293    Widget_Data *wd = elm_widget_data_get(obj);
294    if (!wd) return;
295    evas_object_table_clear(wd->tbl, clear);
296 }