Merge branch 'master' of jae.hwan.kim@165.213.180.234:/git/slp2.0/slp2.0-pkgs/EFL...
[framework/uifw/elementary.git] / src / lib / elm_box.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup Box Box
6  * @ingroup Elementary
7  *
8  * A box object arranges objects in a single row within a box. Sub objects can
9  * be added at the start, end or before or after any existing object in the
10  * box already. It can have its orientation changed too. How a child object is
11  * sized and otherwise arranged within the box depends on evas hints.
12  * evas_object_size_hint_align_set() will set either the alignment within its
13  * region if the region allocated is bigger than the object size. If you want
14  * the sub object sized up to fill the allocated region, use -1.0 for the
15  * apporpriate horizontal or vertical axes. evas_object_size_hint_weight_set()
16  * will set the packing weight. The weights of all items being packed are added
17  * up and if items are to be sized up to fit, those with the higher weights get
18  * proportionally more space.
19  *
20  * NOTE: Objects should not be added to box objects using _add() calls.
21  */
22 typedef struct _Widget_Data Widget_Data;
23
24 struct _Widget_Data
25 {
26    Evas_Object *box;
27    Eina_Bool horizontal:1;
28    Eina_Bool homogeneous:1;
29 };
30
31 static const char *widtype = NULL;
32 static void _del_hook(Evas_Object *obj);
33 static void _sizing_eval(Evas_Object *obj);
34 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
35 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
36
37 static void
38 _del_pre_hook(Evas_Object *obj)
39 {
40     Widget_Data *wd = elm_widget_data_get(obj);
41    if (!wd) return;
42     evas_object_event_callback_del_full
43         (wd->box, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
44     evas_object_box_remove_all(wd->box, 0);
45 }
46
47 static void
48 _del_hook(Evas_Object *obj)
49 {
50    Widget_Data *wd = elm_widget_data_get(obj);
51    if (!wd) return;
52    free(wd);
53 }
54
55 static void
56 _sizing_eval(Evas_Object *obj)
57 {
58    Widget_Data *wd = elm_widget_data_get(obj);
59    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
60    Evas_Coord w, h;
61    if (!wd) return;
62    evas_object_size_hint_min_get(wd->box, &minw, &minh);
63    evas_object_size_hint_max_get(wd->box, &maxw, &maxh);
64    evas_object_size_hint_min_set(obj, minw, minh);
65    evas_object_size_hint_max_set(obj, maxw, maxh);
66    evas_object_geometry_get(obj, NULL, NULL, &w, &h);
67    if (w < minw) w = minw;
68    if (h < minh) h = minh;
69    if ((maxw >= 0) && (w > maxw)) w = maxw;
70    if ((maxh >= 0) && (h > maxh)) h = maxh;
71    evas_object_resize(obj, w, h);
72 }
73
74 static void
75 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
76 {
77    _sizing_eval(data);
78 }
79
80 static void
81 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
82 {
83    _sizing_eval(obj);
84 }
85
86 static void
87 _layout(Evas_Object *o, Evas_Object_Box_Data *priv, void *data)
88 {
89    Widget_Data *wd = data;
90    if (!wd) return;
91    _els_box_layout(o, priv, wd->horizontal, wd->homogeneous);
92 }
93
94
95 /**
96  * Add a new box to the parent
97  *
98  * @param parent The parent object
99  * @return The new object or NULL if it cannot be created
100  *
101  * @ingroup Box
102  */
103 EAPI Evas_Object *
104 elm_box_add(Evas_Object *parent)
105 {
106    Evas_Object *obj;
107    Evas *e;
108    Widget_Data *wd;
109
110    wd = ELM_NEW(Widget_Data);
111    e = evas_object_evas_get(parent);
112    obj = elm_widget_add(e);
113    ELM_SET_WIDTYPE(widtype, "box");
114    elm_widget_type_set(obj, "box");
115    elm_widget_sub_object_add(parent, obj);
116    elm_widget_data_set(obj, wd);
117    elm_widget_del_hook_set(obj, _del_hook);
118    elm_widget_del_pre_hook_set(obj, _del_pre_hook);
119
120    wd->box = evas_object_box_add(e);
121    /*evas_object_box_layout_set(wd->box, evas_object_box_layout_vertical,
122                               NULL, NULL);*/
123    evas_object_box_layout_set(wd->box, _layout, wd, NULL);
124
125    evas_object_event_callback_add(wd->box, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
126                                   _changed_size_hints, obj);
127    elm_widget_resize_object_set(obj, wd->box);
128
129    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
130
131    return obj;
132 }
133
134 /**
135  * Set the horizontal orientation
136  *
137  * By default box object arrange their contents vertically from top to bottom.
138  * By calling this and providing @p orizontal as true, the box will become
139  * horizontal arranging contents left to right.
140  *
141  * @param obj The box object
142  * @param horizontal The horizontal flag (1 = horizontal, 0 = vertical)
143  *
144  * @ingroup Box
145  */
146 EAPI void
147 elm_box_horizontal_set(Evas_Object *obj, Eina_Bool horizontal)
148 {
149    ELM_CHECK_WIDTYPE(obj, widtype);
150    Widget_Data *wd = elm_widget_data_get(obj);
151    if (!wd) return;
152    wd->horizontal = !!horizontal;
153    evas_object_smart_calculate(wd->box);
154    /*if (wd->horizontal)
155      {
156         if (wd->homogeneous)
157           evas_object_box_layout_set(wd->box,
158                 evas_object_box_layout_homogeneous_horizontal, NULL, NULL);
159         else
160           evas_object_box_layout_set(wd->box, evas_object_box_layout_horizontal,
161                                      NULL, NULL);
162      }
163    else
164      {
165         if (wd->homogeneous)
166           evas_object_box_layout_set(wd->box,
167                 evas_object_box_layout_homogeneous_vertical, NULL, NULL);
168         else
169           evas_object_box_layout_set(wd->box, evas_object_box_layout_horizontal,
170                                      NULL, NULL);
171      }*/
172 }
173
174 /**
175  * Set homogenous layout
176  *
177  * If enabled, homogenous layout makes all items the same size. This size is
178  * of course governed by the size of the largest item in the box.
179  *
180  * @param obj The box object
181  * @param homogenous The homogenous flag (1 = on, 2 = off)
182  *
183  * @ingroup Box
184  */
185 EAPI void
186 elm_box_homogenous_set(Evas_Object *obj, Eina_Bool homogenous)
187 {
188    ELM_CHECK_WIDTYPE(obj, widtype);
189    Widget_Data *wd = elm_widget_data_get(obj);
190    if (!wd) return;
191    wd->homogeneous = !!homogenous;
192    evas_object_smart_calculate(wd->box);
193    /*if (wd->horizontal)
194      {
195         if (wd->homogeneous)
196           evas_object_box_layout_set(wd->box,
197                 evas_object_box_layout_homogeneous_horizontal, NULL, NULL);
198         else
199           evas_object_box_layout_set(wd->box, evas_object_box_layout_horizontal,
200                                      NULL, NULL);
201      }
202    else
203      {
204         if (wd->homogeneous)
205           evas_object_box_layout_set(wd->box,
206                 evas_object_box_layout_homogeneous_vertical, NULL, NULL);
207         else
208           evas_object_box_layout_set(wd->box, evas_object_box_layout_horizontal,
209                                      NULL, NULL);
210      }*/
211 }
212
213 /**
214  * This adds a box at the start of the box (top or left based on orientation)
215  *
216  * This will add the @p subobj to the box object indicated at the beginning
217  * of the box (the left or top end).
218  *
219  * @param obj The box object
220  * @param subobj The object to add to the box
221  *
222  * @ingroup Box
223  */
224 EAPI void
225 elm_box_pack_start(Evas_Object *obj, Evas_Object *subobj)
226 {
227    ELM_CHECK_WIDTYPE(obj, widtype);
228    Widget_Data *wd = elm_widget_data_get(obj);
229    if (!wd) return;
230    elm_widget_sub_object_add(obj, subobj);
231    evas_object_box_prepend(wd->box, subobj);
232 }
233
234 /**
235  * This adds a box at the end of the box (bottom or right based on orientation)
236  *
237  * This will add the @p subobj to the box object indicated at the end
238  * of the box (the right or bottom end).
239  *
240  * @param obj The box object
241  * @param subobj The object to add to the box
242  *
243  * @ingroup Box
244  */
245 EAPI void
246 elm_box_pack_end(Evas_Object *obj, Evas_Object *subobj)
247 {
248    ELM_CHECK_WIDTYPE(obj, widtype);
249    Widget_Data *wd = elm_widget_data_get(obj);
250    if (!wd) return;
251    elm_widget_sub_object_add(obj, subobj);
252    evas_object_box_append(wd->box, subobj);
253 }
254
255 /**
256  * This adds adds an object to the box before the indicated object
257  *
258  * This will add the @p subobj to the box indicated before the object
259  * indicated with @p before. If @p before is not already in the box, results
260  * are undefined. Before means either to the left of the indicated object or
261  * above it depending on orientation.
262  *
263  * @param obj The box object
264  * @param subobj The object to add to the box
265  * @param before The object before which to add it
266  *
267  * @ingroup Box
268  */
269 EAPI void
270 elm_box_pack_before(Evas_Object *obj, Evas_Object *subobj, Evas_Object *before)
271 {
272    ELM_CHECK_WIDTYPE(obj, widtype);
273    Widget_Data *wd = elm_widget_data_get(obj);
274    if (!wd) return;
275    elm_widget_sub_object_add(obj, subobj);
276    evas_object_box_insert_before(wd->box, subobj, before);
277 }
278
279 /**
280  * This adds adds an object to the box after the indicated object
281  *
282  * This will add the @p subobj to the box indicated after the object
283  * indicated with @p after. If @p after is not already in the box, results
284  * are undefined. After means either to the right of the indicated object or
285  * below it depending on orientation.
286  *
287  * @param obj The box object
288  * @param subobj The object to add to the box
289  * @param after The object after which to add it
290  *
291  * @ingroup Box
292  */
293 EAPI void
294 elm_box_pack_after(Evas_Object *obj, Evas_Object *subobj, Evas_Object *after)
295 {
296    ELM_CHECK_WIDTYPE(obj, widtype);
297    Widget_Data *wd = elm_widget_data_get(obj);
298    if (!wd) return;
299    elm_widget_sub_object_add(obj, subobj);
300    evas_object_box_insert_after(wd->box, subobj, after);
301 }
302
303 /**
304  * This clears the box items
305  *
306  * This delete all members of the box object, but not the box itself.
307  *
308  * @param obj The box object
309  *
310  * @ingroup Box
311  */
312 EAPI void
313 elm_box_clear(Evas_Object *obj)
314 {
315    ELM_CHECK_WIDTYPE(obj, widtype);
316    Widget_Data *wd = elm_widget_data_get(obj);
317    if (!wd) return;
318    evas_object_box_remove_all(wd->box, 1);
319 }
320
321 /**
322  * This unpack a box item
323  *
324  * This unpack the selected member from the box object, but does not delete
325  * the box itself or the packed items.
326  *
327  * @param obj The box object
328  *
329  * @ingroup Box
330  */
331 EAPI void
332 elm_box_unpack(Evas_Object *obj, Evas_Object *subobj)
333 {
334    ELM_CHECK_WIDTYPE(obj, widtype);
335    Widget_Data *wd = elm_widget_data_get(obj);
336    if (!wd) return;
337    evas_object_box_remove(wd->box, subobj);
338 }
339
340 /**
341  * This unpack the box items
342  *
343  * This unpack all members from the box object, but does not delete
344  * the box itself or the packed items.
345  *
346  * @param obj The box object
347  *
348  * @ingroup Box
349  */
350 EAPI void
351 elm_box_unpack_all(Evas_Object *obj)
352 {
353    ELM_CHECK_WIDTYPE(obj, widtype);
354    Widget_Data *wd = elm_widget_data_get(obj);
355    if (!wd) return;
356    evas_object_box_remove_all(wd->box, 0);
357 }