Merge branch 'master' into svn_merge
[framework/uifw/elementary.git] / src / lib / elm_box.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 #define SIG_CHILD_ADDED "child,added"
5 #define SIG_CHILD_REMOVED "child,removed"
6
7 typedef struct _Widget_Data Widget_Data;
8 typedef struct _Transition_Animation_Data Transition_Animation_Data;
9
10 struct _Widget_Data
11 {
12    Evas_Object *box;
13    Eina_Bool horizontal:1;
14    Eina_Bool homogeneous:1;
15 };
16
17 struct _Elm_Box_Transition
18 {
19    double initial_time;
20    double duration;
21    Eina_Bool animation_ended:1;
22    Eina_Bool recalculate:1;
23    Ecore_Animator *animator;
24
25    struct
26      {
27         Evas_Object_Box_Layout layout;
28         void *data;
29         void(*free_data)(void *data);
30      } start, end;
31
32    void(*transition_end_cb)(void *data);
33    void *transition_end_data;
34    void (*transition_end_free_data)(void *data);
35    Eina_List *objs;
36    Evas_Object *box;
37 };
38
39 struct _Transition_Animation_Data
40 {
41    Evas_Object *obj;
42    struct
43      {
44         Evas_Coord x, y, w, h;
45      } start, end;
46 };
47
48 static const char *widtype = NULL;
49 static void _del_hook(Evas_Object *obj);
50 static void _sizing_eval(Evas_Object *obj);
51 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
52 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
53
54 static void
55 _del_pre_hook(Evas_Object *obj)
56 {
57    Widget_Data *wd = elm_widget_data_get(obj);
58    if (!wd) return;
59    evas_object_event_callback_del_full
60       (wd->box, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
61    evas_object_box_remove_all(wd->box, EINA_FALSE);
62 }
63
64 static void
65 _del_hook(Evas_Object *obj)
66 {
67    Widget_Data *wd = elm_widget_data_get(obj);
68    if (!wd) return;
69    free(wd);
70 }
71
72 static void *
73 _elm_box_list_data_get(const Eina_List *list)
74 {
75    Evas_Object_Box_Option *opt = eina_list_data_get(list);
76    return opt->obj;
77 }
78
79 static Eina_Bool
80 _elm_box_focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next)
81 {
82    Widget_Data *wd = elm_widget_data_get(obj);
83    const Eina_List *items;
84    void *(*list_data_get) (const Eina_List *list);
85
86    if ((!wd) || (!wd->box))
87      return EINA_FALSE;
88
89    /* Focus chain */
90    /* TODO: Change this to use other chain */
91    if ((items = elm_widget_focus_custom_chain_get(obj)))
92      list_data_get = eina_list_data_get;
93    else
94      {
95         Evas_Object_Box_Data *bd = evas_object_smart_data_get(wd->box);
96         items = bd->children;
97         list_data_get = _elm_box_list_data_get;
98
99         if (!items) return EINA_FALSE;
100      }
101
102    return elm_widget_focus_list_next_get(obj, items, list_data_get, dir, next);
103 }
104
105 static void
106 _theme_hook(Evas_Object *obj)
107 {
108    Widget_Data *wd = elm_widget_data_get(obj);
109    if (!wd) return;
110    _elm_widget_mirrored_reload(obj);
111    evas_object_smart_calculate(wd->box);
112 }
113
114 static void
115 _sizing_eval(Evas_Object *obj)
116 {
117    Widget_Data *wd = elm_widget_data_get(obj);
118    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
119    Evas_Coord w, h;
120    if (!wd) return;
121    evas_object_size_hint_min_get(wd->box, &minw, &minh);
122    evas_object_size_hint_max_get(wd->box, &maxw, &maxh);
123    evas_object_size_hint_min_set(obj, minw, minh);
124    evas_object_size_hint_max_set(obj, maxw, maxh);
125    evas_object_geometry_get(obj, NULL, NULL, &w, &h);
126    if (w < minw) w = minw;
127    if (h < minh) h = minh;
128    if ((maxw >= 0) && (w > maxw)) w = maxw;
129    if ((maxh >= 0) && (h > maxh)) h = maxh;
130    evas_object_resize(obj, w, h);
131 }
132
133 static void
134 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
135 {
136    _sizing_eval(data);
137 }
138
139 static void
140 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
141 {
142    _sizing_eval(obj);
143 }
144
145 static void
146 _layout(Evas_Object *o, Evas_Object_Box_Data *priv, void *data)
147 {
148    Evas_Object *obj = (Evas_Object *) data;
149    Widget_Data *wd = elm_widget_data_get(obj);
150    if (!wd) return;
151    _els_box_layout(o, priv, wd->horizontal, wd->homogeneous,
152                    elm_widget_mirrored_get(obj));
153 }
154
155 static Eina_Bool
156 _transition_animation(void *data)
157 {
158    evas_object_smart_changed(data);
159    return ECORE_CALLBACK_RENEW;
160 }
161
162 static void
163 _transition_layout_child_added(void *data, Evas_Object *obj __UNUSED__, void *event_info)
164 {
165    Transition_Animation_Data *tad;
166    Evas_Object_Box_Option *opt = event_info;
167    Elm_Box_Transition *layout_data = data;
168
169    tad = calloc(1, sizeof(Transition_Animation_Data));
170    if (!tad) return;
171    tad->obj = opt->obj;
172    layout_data->objs = eina_list_append(layout_data->objs, tad);
173    layout_data->recalculate = EINA_TRUE;
174 }
175
176 static void
177 _transition_layout_child_removed(void *data, Evas_Object *obj __UNUSED__, void *event_info)
178 {
179    Eina_List *l;
180    Transition_Animation_Data *tad;
181    Elm_Box_Transition *layout_data = data;
182
183    EINA_LIST_FOREACH(layout_data->objs, l, tad)
184      {
185         if (tad->obj == event_info)
186           {
187              free(eina_list_data_get(l));
188              layout_data->objs = eina_list_remove_list(layout_data->objs, l);
189              layout_data->recalculate = EINA_TRUE;
190              break;
191           }
192      }
193 }
194
195 static void
196 _transition_layout_obj_resize_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
197 {
198    Elm_Box_Transition *layout_data = data;
199    layout_data->recalculate = EINA_TRUE;
200 }
201
202 static void
203 _transition_layout_calculate_coords(Evas_Object *obj, Evas_Object_Box_Data *priv,
204                                     Elm_Box_Transition *layout_data)
205 {
206    Eina_List *l;
207    Transition_Animation_Data *tad;
208    Evas_Coord x, y, w, h;
209    const double curtime = ecore_loop_time_get();
210
211    layout_data->duration =
212       layout_data->duration - (curtime - layout_data->initial_time);
213    layout_data->initial_time = curtime;
214
215    evas_object_geometry_get(obj, &x, &y, &w, &h);
216    EINA_LIST_FOREACH(layout_data->objs, l, tad)
217      {
218         evas_object_geometry_get(tad->obj, &tad->start.x, &tad->start.y,
219                                  &tad->start.w, &tad->start.h);
220         tad->start.x = tad->start.x - x;
221         tad->start.y = tad->start.y - y;
222      }
223    layout_data->end.layout(obj, priv, layout_data->end.data);
224    EINA_LIST_FOREACH(layout_data->objs, l, tad)
225      {
226         evas_object_geometry_get(tad->obj, &tad->end.x, &tad->end.y,
227                                  &tad->end.w, &tad->end.h);
228         tad->end.x = tad->end.x - x;
229         tad->end.y = tad->end.y - y;
230      }
231 }
232
233 static Eina_Bool
234 _transition_layout_load_children_list(Evas_Object_Box_Data *priv,
235                                       Elm_Box_Transition *layout_data)
236 {
237    Eina_List *l;
238    Evas_Object_Box_Option *opt;
239    Transition_Animation_Data *tad;
240
241    EINA_LIST_FREE(layout_data->objs, tad)
242       free(tad);
243
244    EINA_LIST_FOREACH(priv->children, l, opt)
245      {
246         tad = calloc(1, sizeof(Transition_Animation_Data));
247         if (!tad)
248           {
249              EINA_LIST_FREE(layout_data->objs, tad)
250                 free(tad);
251              layout_data->objs = NULL;
252              return EINA_FALSE;
253           }
254         tad->obj = opt->obj;
255         layout_data->objs = eina_list_append(layout_data->objs, tad);
256      }
257    return EINA_TRUE;
258 }
259
260 static Eina_Bool
261 _transition_layout_animation_start(Evas_Object *obj, Evas_Object_Box_Data *priv,
262                                    Elm_Box_Transition *layout_data, Eina_Bool(*transition_animation_cb)(void *data))
263 {
264    layout_data->start.layout(obj, priv, layout_data->start.data);
265    layout_data->box = obj;
266    layout_data->initial_time = ecore_loop_time_get();
267
268    if (!_transition_layout_load_children_list(priv, layout_data))
269      return EINA_FALSE;
270    _transition_layout_calculate_coords(obj, priv, layout_data);
271
272    evas_object_event_callback_add(obj, EVAS_CALLBACK_RESIZE,
273                                   _transition_layout_obj_resize_cb, layout_data);
274    evas_object_smart_callback_add(obj, SIG_CHILD_ADDED,
275                                   _transition_layout_child_added, layout_data);
276    evas_object_smart_callback_add(obj, SIG_CHILD_REMOVED,
277                                   _transition_layout_child_removed, layout_data);
278    if (!layout_data->animator)
279      layout_data->animator = ecore_animator_add(transition_animation_cb, obj);
280    layout_data->animation_ended = EINA_FALSE;
281    return EINA_TRUE;
282 }
283
284 static void
285 _transition_layout_animation_stop(Elm_Box_Transition *layout_data)
286 {
287    layout_data->animation_ended = EINA_TRUE;
288    if (layout_data->animator)
289      {
290         ecore_animator_del(layout_data->animator);
291         layout_data->animator = NULL;
292      }
293
294    if (layout_data->transition_end_cb)
295      layout_data->transition_end_cb(layout_data->transition_end_data);
296 }
297
298 static void
299 _transition_layout_animation_exec(Evas_Object *obj, Evas_Object_Box_Data *priv __UNUSED__,
300                                   Elm_Box_Transition *layout_data, const double curtime)
301 {
302    Eina_List *l;
303    Transition_Animation_Data *tad;
304    Evas_Coord x, y, w, h;
305    Evas_Coord cur_x, cur_y, cur_w, cur_h;
306    double progress = 0.0;
307
308    progress = (curtime - layout_data->initial_time) / layout_data->duration;
309    evas_object_geometry_get(obj, &x, &y, &w, &h);
310
311    EINA_LIST_FOREACH(layout_data->objs, l, tad)
312      {
313         cur_x = x + tad->start.x + ((tad->end.x - tad->start.x) * progress);
314         cur_y = y + tad->start.y + ((tad->end.y - tad->start.y) * progress);
315         cur_w = tad->start.w + ((tad->end.w - tad->start.w) * progress);
316         cur_h = tad->start.h + ((tad->end.h - tad->start.h) * progress);
317         evas_object_move(tad->obj, cur_x, cur_y);
318         evas_object_resize(tad->obj, cur_w, cur_h);
319      }
320 }
321
322 EAPI Evas_Object *
323 elm_box_add(Evas_Object *parent)
324 {
325    Evas_Object *obj;
326    Evas *e;
327    Widget_Data *wd;
328
329    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
330
331    ELM_SET_WIDTYPE(widtype, "box");
332    elm_widget_type_set(obj, "box");
333    elm_widget_sub_object_add(parent, obj);
334    elm_widget_data_set(obj, wd);
335    elm_widget_del_hook_set(obj, _del_hook);
336    elm_widget_del_pre_hook_set(obj, _del_pre_hook);
337    elm_widget_focus_next_hook_set(obj, _elm_box_focus_next_hook);
338    elm_widget_can_focus_set(obj, EINA_FALSE);
339    elm_widget_highlight_ignore_set(obj, EINA_TRUE);
340    elm_widget_theme_hook_set(obj, _theme_hook);
341
342    wd->box = evas_object_box_add(e);
343    /*evas_object_box_layout_set(wd->box, evas_object_box_layout_vertical,
344      NULL, NULL);*/
345    evas_object_box_layout_set(wd->box, _layout, obj, NULL);
346
347    evas_object_event_callback_add(wd->box, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
348                                   _changed_size_hints, obj);
349    elm_widget_resize_object_set(obj, wd->box);
350
351    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
352
353    return obj;
354 }
355
356 EAPI void
357 elm_box_horizontal_set(Evas_Object *obj, Eina_Bool horizontal)
358 {
359    ELM_CHECK_WIDTYPE(obj, widtype);
360    Widget_Data *wd = elm_widget_data_get(obj);
361    if (!wd) return;
362    wd->horizontal = !!horizontal;
363    evas_object_smart_calculate(wd->box);
364  /*if (wd->horizontal)
365      {
366         if (wd->homogeneous)
367           evas_object_box_layout_set(wd->box,
368                                      evas_object_box_layout_homogeneous_horizontal, NULL, NULL);
369         else
370           evas_object_box_layout_set(wd->box, evas_object_box_layout_horizontal,
371                                      NULL, NULL);
372      }
373    else
374      {
375         if (wd->homogeneous)
376           evas_object_box_layout_set(wd->box,
377                                      evas_object_box_layout_homogeneous_vertical, NULL, NULL);
378         else
379           evas_object_box_layout_set(wd->box, evas_object_box_layout_horizontal,
380                                      NULL, NULL);
381      } */
382 }
383
384 EAPI Eina_Bool
385 elm_box_horizontal_get(const Evas_Object *obj)
386 {
387    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
388    Widget_Data *wd = elm_widget_data_get(obj);
389    if (!wd) return EINA_FALSE;
390    return wd->horizontal;
391 }
392
393 EAPI void
394 elm_box_homogeneous_set(Evas_Object *obj, Eina_Bool homogeneous)
395 {
396    ELM_CHECK_WIDTYPE(obj, widtype);
397    Widget_Data *wd = elm_widget_data_get(obj);
398    if (!wd) return;
399    wd->homogeneous = !!homogeneous;
400    evas_object_smart_calculate(wd->box);
401  /*if (wd->horizontal)
402      {
403         if (wd->homogeneous)
404           evas_object_box_layout_set(wd->box,
405                                      evas_object_box_layout_homogeneous_horizontal, NULL, NULL);
406         else
407           evas_object_box_layout_set(wd->box, evas_object_box_layout_horizontal,
408                                      NULL, NULL);
409      }
410    else
411      {
412         if (wd->homogeneous)
413           evas_object_box_layout_set(wd->box,
414                                      evas_object_box_layout_homogeneous_vertical, NULL, NULL);
415         else
416           evas_object_box_layout_set(wd->box, evas_object_box_layout_horizontal,
417                                      NULL, NULL);
418      } */
419 }
420
421 EINA_DEPRECATED EAPI void
422 elm_box_homogenous_set(Evas_Object *obj, Eina_Bool homogenous)
423 {
424    elm_box_homogeneous_set(obj, homogenous);
425 }
426
427 EAPI Eina_Bool
428 elm_box_homogeneous_get(const Evas_Object *obj)
429 {
430    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
431    Widget_Data *wd = elm_widget_data_get(obj);
432    if (!wd) return EINA_FALSE;
433    return wd->homogeneous;
434 }
435
436 EINA_DEPRECATED EAPI Eina_Bool
437 elm_box_homogenous_get(const Evas_Object *obj)
438 {
439    return elm_box_homogeneous_get(obj);
440 }
441
442 EAPI void
443 elm_box_pack_start(Evas_Object *obj, Evas_Object *subobj)
444 {
445    ELM_CHECK_WIDTYPE(obj, widtype);
446    Widget_Data *wd = elm_widget_data_get(obj);
447    if (!wd) return;
448    elm_widget_sub_object_add(obj, subobj);
449    evas_object_box_prepend(wd->box, subobj);
450 }
451
452 EAPI void
453 elm_box_pack_end(Evas_Object *obj, Evas_Object *subobj)
454 {
455    ELM_CHECK_WIDTYPE(obj, widtype);
456    Widget_Data *wd = elm_widget_data_get(obj);
457    if (!wd) return;
458    elm_widget_sub_object_add(obj, subobj);
459    evas_object_box_append(wd->box, subobj);
460 }
461
462 EAPI void
463 elm_box_pack_before(Evas_Object *obj, Evas_Object *subobj, Evas_Object *before)
464 {
465    ELM_CHECK_WIDTYPE(obj, widtype);
466    Widget_Data *wd = elm_widget_data_get(obj);
467    if (!wd) return;
468    elm_widget_sub_object_add(obj, subobj);
469    evas_object_box_insert_before(wd->box, subobj, before);
470 }
471
472 EAPI void
473 elm_box_pack_after(Evas_Object *obj, Evas_Object *subobj, Evas_Object *after)
474 {
475    ELM_CHECK_WIDTYPE(obj, widtype);
476    Widget_Data *wd = elm_widget_data_get(obj);
477    if (!wd) return;
478    elm_widget_sub_object_add(obj, subobj);
479    evas_object_box_insert_after(wd->box, subobj, after);
480 }
481
482 EAPI void
483 elm_box_clear(Evas_Object *obj)
484 {
485    ELM_CHECK_WIDTYPE(obj, widtype);
486    Widget_Data *wd = elm_widget_data_get(obj);
487    if (!wd) return;
488    evas_object_box_remove_all(wd->box, EINA_TRUE);
489 }
490
491 EAPI void
492 elm_box_unpack(Evas_Object *obj, Evas_Object *subobj)
493 {
494    ELM_CHECK_WIDTYPE(obj, widtype);
495    Widget_Data *wd = elm_widget_data_get(obj);
496    if (!wd) return;
497    evas_object_box_remove(wd->box, subobj);
498 }
499
500 EAPI void
501 elm_box_unpack_all(Evas_Object *obj)
502 {
503    ELM_CHECK_WIDTYPE(obj, widtype);
504    Widget_Data *wd = elm_widget_data_get(obj);
505    if (!wd) return;
506    evas_object_box_remove_all(wd->box, EINA_FALSE);
507 }
508
509 EAPI void
510 elm_box_layout_set(Evas_Object *obj, Evas_Object_Box_Layout cb, const void *data, void (*free_data)(void *data))
511 {
512    ELM_CHECK_WIDTYPE(obj, widtype);
513    Widget_Data *wd = elm_widget_data_get(obj);
514    if (!wd) return;
515
516    if (cb)
517      evas_object_box_layout_set(wd->box, cb, data, free_data);
518    else
519      evas_object_box_layout_set(wd->box, _layout, obj, NULL);
520 }
521
522 EAPI void
523 elm_box_layout_transition(Evas_Object *obj, Evas_Object_Box_Data *priv, void *data)
524 {
525    Elm_Box_Transition *box_data = data;
526    const double curtime = ecore_loop_time_get();
527
528    if (box_data->animation_ended)
529      {
530         box_data->end.layout(obj, priv, box_data->end.data);
531         return;
532      }
533
534    if (!box_data->animator)
535      {
536         if (!_transition_layout_animation_start(obj, priv, box_data,
537                                                 _transition_animation))
538           return;
539      }
540    else
541      {
542         if (box_data->recalculate)
543           {
544              _transition_layout_calculate_coords(obj, priv, box_data);
545              box_data->recalculate = EINA_FALSE;
546           }
547      }
548
549    if ((curtime >= box_data->duration + box_data->initial_time))
550      _transition_layout_animation_stop(box_data);
551    else
552      _transition_layout_animation_exec(obj, priv, box_data, curtime);
553 }
554
555 EAPI Elm_Box_Transition *
556 elm_box_transition_new(const double duration,
557                        Evas_Object_Box_Layout start_layout, void *start_layout_data,
558                        void(*start_layout_free_data)(void *data),
559                        Evas_Object_Box_Layout end_layout, void *end_layout_data,
560                        void(*end_layout_free_data)(void *data),
561                        void(*transition_end_cb)(void *data),
562                        void *transition_end_data)
563 {
564    Elm_Box_Transition *box_data;
565
566    EINA_SAFETY_ON_NULL_RETURN_VAL(start_layout, NULL);
567    EINA_SAFETY_ON_NULL_RETURN_VAL(end_layout, NULL);
568
569    box_data = calloc(1, sizeof(Elm_Box_Transition));
570    if (!box_data)
571      return NULL;
572
573    box_data->start.layout = start_layout;
574    box_data->start.data = start_layout_data;
575    box_data->start.free_data = start_layout_free_data;
576    box_data->end.layout = end_layout;
577    box_data->end.data = end_layout_data;
578    box_data->end.free_data = end_layout_free_data;
579    box_data->duration = duration;
580    box_data->transition_end_cb = transition_end_cb;
581    box_data->transition_end_data = transition_end_data;
582    return box_data;
583 }
584
585 EAPI void
586 elm_box_transition_free(void *data)
587 {
588    EINA_SAFETY_ON_NULL_RETURN(data);
589
590    Transition_Animation_Data *tad;
591    Elm_Box_Transition *box_data = data;
592    if ((box_data->start.free_data) && (box_data->start.data))
593      box_data->start.free_data(box_data->start.data);
594    if ((box_data->end.free_data) && (box_data->end.data))
595      box_data->end.free_data(box_data->end.data);
596    EINA_LIST_FREE(box_data->objs, tad)
597       free(tad);
598    evas_object_event_callback_del(box_data->box, EVAS_CALLBACK_RESIZE, _transition_layout_obj_resize_cb);
599    evas_object_smart_callback_del(box_data->box, SIG_CHILD_ADDED, _transition_layout_child_added);
600    evas_object_smart_callback_del(box_data->box, SIG_CHILD_REMOVED, _transition_layout_child_removed);
601    if (box_data->animator)
602      {
603         ecore_animator_del(box_data->animator);
604         box_data->animator = NULL;
605      }
606    free(data);
607 }
608
609 EAPI const Eina_List *
610 elm_box_children_get(const Evas_Object *obj)
611 {
612    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
613    Widget_Data *wd = elm_widget_data_get(obj);
614    if (!wd) return NULL;
615    return evas_object_box_children_get(wd->box);
616 }
617
618 EAPI void
619 elm_box_padding_set(Evas_Object *obj, Evas_Coord horizontal, Evas_Coord vertical)
620 {
621    ELM_CHECK_WIDTYPE(obj, widtype);
622    Widget_Data *wd = elm_widget_data_get(obj);
623    if (!wd) return;
624    evas_object_box_padding_set(wd->box, horizontal, vertical);
625 }
626
627 EAPI void
628 elm_box_padding_get(const Evas_Object *obj, Evas_Coord *horizontal, Evas_Coord *vertical)
629 {
630    ELM_CHECK_WIDTYPE(obj, widtype);
631    Widget_Data *wd = elm_widget_data_get(obj);
632    if (!wd) return;
633    evas_object_box_padding_get(wd->box, horizontal, vertical);
634 }
635
636 EAPI void
637 elm_box_align_set(Evas_Object *obj, double horizontal, double vertical)
638 {
639    ELM_CHECK_WIDTYPE(obj, widtype);
640    Widget_Data *wd = elm_widget_data_get(obj);
641    if (!wd) return;
642    evas_object_box_align_set(wd->box, horizontal, vertical);
643 }
644
645 EAPI void
646 elm_box_align_get(const Evas_Object *obj, double *horizontal, double *vertical)
647 {
648    ELM_CHECK_WIDTYPE(obj, widtype);
649    Widget_Data *wd = elm_widget_data_get(obj);
650    if (!wd) return;
651    evas_object_box_align_get(wd->box, horizontal, vertical);
652 }