Elementary: elm_frame documentation.
[framework/uifw/elementary.git] / src / lib / elm_pager.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup Pager Pager
6  *
7  * The pager is an object that allows flipping (with animation) between 1 or
8  * more “pages” of objects, much like a stack of windows within the window.
9  *
10  * Objects can be pushed or popped from the stack or deleted as normal.
11  * Pushes and pops will animate (and a pop will delete the object once the
12  * animation is finished). Any object in the pager can be promoted to the top
13  * (from its current stacking position) as well. Objects are pushed to the
14  * top with elm_pager_content_push() and when the top item is no longer
15  * wanted, simply pop it with elm_pager_content_pop() and it will also be
16  * deleted. Any object you wish to promote to the top that is already in the
17  * pager, simply use elm_pager_content_promote(). If an object is no longer
18  * needed and is not the top item, just delete it as normal. You can query
19  * which objects are the top and bottom with elm_pager_content_bottom_get()
20  * and elm_pager_content_top_get().
21  *
22  * Signals that you can add callbacks for are:
23  *
24  * "hide,finished" - when the previous page is hided
25  *
26  */
27
28 typedef struct _Widget_Data Widget_Data;
29 typedef struct _Item Item;
30
31 struct _Widget_Data
32 {
33    Eina_List *stack;
34    Item *top, *oldtop;
35    Evas_Object *rect, *clip;
36 };
37
38 struct _Item
39 {
40    Evas_Object *obj, *base, *content;
41    Evas_Coord minw, minh;
42    Eina_Bool popme : 1;
43 };
44
45 static const char *widtype = NULL;
46 static void _del_hook(Evas_Object *obj);
47 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
48 static void _theme_hook(Evas_Object *obj);
49 static void _sizing_eval(Evas_Object *obj);
50 static void _changed_size_hints(void *data,
51                                 Evas *e,
52                                 Evas_Object *obj,
53                                 void *event_info);
54 static void _content_del(void *data,
55                          Evas *e,
56                          Evas_Object *obj,
57                          void *event_info);
58 static Eina_List *_item_get(Evas_Object *obj, Evas_Object *content);
59
60 static const char SIG_HIDE_FINISHED[] = "hide,finished";
61
62 static const Evas_Smart_Cb_Description _signals[] = {
63    {SIG_HIDE_FINISHED, ""},
64    {NULL, NULL}
65 };
66
67 static void
68 _del_hook(Evas_Object *obj)
69 {
70    Widget_Data *wd = elm_widget_data_get(obj);
71    if (!wd) return;
72    free(wd);
73 }
74
75 static void
76 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
77 {
78    Widget_Data *wd = elm_widget_data_get(obj);
79    Eina_List *l;
80    Item *it;
81    if (!wd) return;
82    EINA_LIST_FOREACH(wd->stack, l, it)
83       edje_object_mirrored_set(it->base, rtl);
84 }
85
86 static void
87 _theme_hook(Evas_Object *obj)
88 {
89    Widget_Data *wd = elm_widget_data_get(obj);
90    Eina_List *l;
91    Item *it;
92    if (!wd) return;
93    _elm_widget_mirrored_reload(obj);
94    _mirrored_set(obj, elm_widget_mirrored_get(obj));
95    EINA_LIST_FOREACH(wd->stack, l, it)
96      {
97         _elm_theme_object_set(obj, it->base,  "pager", "base",
98                               elm_widget_style_get(obj));
99         edje_object_scale_set(it->base, elm_widget_scale_get(obj) *
100                               _elm_config->scale);
101      }
102    _sizing_eval(obj);
103 }
104
105 static Eina_List *
106 _item_get(Evas_Object *obj, Evas_Object *content)
107 {
108    Widget_Data *wd = elm_widget_data_get(obj);
109    Item *it;
110    Eina_List *l;
111    if (!wd) return NULL;
112
113    EINA_LIST_FOREACH(wd->stack, l, it)
114      {
115         if (it->content == content)
116           return l;
117      }
118
119    return NULL;
120 }
121
122 static Eina_Bool
123 _elm_pager_focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next)
124 {
125    Widget_Data *wd = elm_widget_data_get(obj);
126    Evas_Object *cur;
127
128    if ((!wd) || (!wd->top))
129      return EINA_FALSE;
130
131    cur = wd->top->content;
132
133    /* Try Focus cycle in subitem */
134    return elm_widget_focus_next_get(cur, dir, next);
135 }
136
137 static void
138 _sizing_eval(Evas_Object *obj)
139 {
140    Widget_Data *wd = elm_widget_data_get(obj);
141    Evas_Coord minw = -1, minh = -1;
142    Eina_List *l;
143    Item *it;
144    if (!wd) return;
145    EINA_LIST_FOREACH(wd->stack, l, it)
146      {
147         if (it->minw > minw) minw = it->minw;
148         if (it->minh > minh) minh = it->minh;
149      }
150    evas_object_size_hint_min_set(obj, minw, minh);
151    evas_object_size_hint_max_set(obj, -1, -1);
152 }
153
154 static void
155 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
156 {
157    Item *it = data;
158    Evas_Coord minw = -1, minh = -1;
159    evas_object_size_hint_min_get(it->content, &minw, &minh);
160    // FIXME: why is this needed? how does edje get this unswallowed or
161    // lose its callbacks to edje
162    edje_object_part_swallow(it->base, "elm.swallow.content", it->content);
163    edje_object_size_min_calc(it->base, &it->minw, &it->minh);
164    _sizing_eval(it->obj);
165 }
166
167 static void
168 _eval_top(Evas_Object *obj)
169 {
170    Widget_Data *wd = elm_widget_data_get(obj);
171    Item *ittop;
172    if (!wd) return;
173    if (!wd->stack) return;
174    ittop = eina_list_last(wd->stack)->data;
175    if (ittop != wd->top)
176      {
177         Evas_Object *o;
178         const char *onshow, *onhide;
179
180         if (wd->top)
181           {
182              o = wd->top->base;
183              if (wd->top->popme)
184                {
185                   edje_object_signal_emit(o, "elm,action,pop", "elm");
186                   wd->stack = eina_list_remove(wd->stack, wd->top);
187                }
188              else
189                edje_object_signal_emit(o, "elm,action,hide", "elm");
190              onhide = edje_object_data_get(o, "onhide");
191              if (onhide)
192                {
193                   if (!strcmp(onhide, "raise")) evas_object_raise(o);
194                   else if (!strcmp(onhide, "lower")) evas_object_lower(o);
195                }
196           }
197         wd->oldtop = wd->top;
198         wd->top = ittop;
199         o = wd->top->base;
200         evas_object_show(o);
201         if (wd->oldtop)
202           {
203              if (elm_object_focus_get(wd->oldtop->content))
204                elm_object_focus(wd->top->content);
205              if (wd->oldtop->popme)
206                edje_object_signal_emit(o, "elm,action,show", "elm");
207              else
208                edje_object_signal_emit(o, "elm,action,push", "elm");
209           }
210         else
211           edje_object_signal_emit(o, "elm,action,push", "elm");
212         onshow = edje_object_data_get(o, "onshow");
213         if (onshow)
214           {
215              if (!strcmp(onshow, "raise")) evas_object_raise(o);
216              else if (!strcmp(onshow, "lower")) evas_object_lower(o);
217           }
218      }
219 }
220
221 static void
222 _move(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
223 {
224    Widget_Data *wd = elm_widget_data_get(data);
225    Evas_Coord x, y;
226    Eina_List *l;
227    Item *it;
228    if (!wd) return;
229    evas_object_geometry_get(obj, &x, &y, NULL, NULL);
230    EINA_LIST_FOREACH(wd->stack, l, it)
231      evas_object_move(it->base, x, y);
232 }
233
234 static void
235 _content_del(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
236 {
237    Widget_Data *wd = elm_widget_data_get(data);
238    Eina_List *l;
239    Item *it;
240    if (!wd) return;
241    l = _item_get(data, obj);
242    if (!l) return;
243    it = l->data;
244    wd->stack = eina_list_remove_list(wd->stack, l);
245    evas_object_event_callback_del_full
246       (obj, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, it);
247    evas_object_del(it->base);
248    _eval_top(data);
249    free(it);
250 }
251
252 static void
253 _resize(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
254 {
255    Widget_Data *wd = elm_widget_data_get(data);
256    Evas_Coord w, h;
257    Eina_List *l;
258    Item *it;
259    if (!wd) return;
260    evas_object_geometry_get(obj, NULL, NULL, &w, &h);
261    EINA_LIST_FOREACH(wd->stack, l, it) evas_object_resize(it->base, w, h);
262 }
263
264 static void
265 _signal_hide_finished(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
266 {
267    Item *it = data;
268    Evas_Object *obj2 = it->obj;
269    Evas_Object *content = it->content;
270
271    if (it->popme)
272      {
273         evas_object_del(it->base);
274         evas_object_event_callback_del_full(content,
275                                             EVAS_CALLBACK_CHANGED_SIZE_HINTS,
276                                             _changed_size_hints,
277                                             it);
278         evas_object_event_callback_del(content,
279                                        EVAS_CALLBACK_DEL,
280                                        _content_del);
281         evas_object_del(content);
282         free(it);
283      }
284    else
285      {
286         evas_object_hide(it->base);
287         edje_object_signal_emit(it->base, "elm,action,reset", "elm");
288         edje_object_message_signal_process(it->base);
289         evas_object_hide(content);
290      }
291     evas_object_smart_callback_call(obj2, SIG_HIDE_FINISHED, content);
292     _sizing_eval(obj2);
293 }
294
295 /**
296  * Add a new pager to the parent
297  *
298  * @param parent The parent object
299  * @return The new object or NULL if it cannot be created
300  *
301  * @ingroup Pager
302  */
303 EAPI Evas_Object *
304 elm_pager_add(Evas_Object *parent)
305 {
306    Evas_Object *obj;
307    Evas *e;
308    Widget_Data *wd;
309
310    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
311
312    ELM_SET_WIDTYPE(widtype, "pager");
313    elm_widget_type_set(obj, "pager");
314    elm_widget_sub_object_add(parent, obj);
315    elm_widget_data_set(obj, wd);
316    elm_widget_del_hook_set(obj, _del_hook);
317    elm_widget_theme_hook_set(obj, _theme_hook);
318    elm_widget_focus_next_hook_set(obj, _elm_pager_focus_next_hook);
319    elm_widget_can_focus_set(obj, EINA_FALSE);
320
321    wd->clip = evas_object_rectangle_add(e);
322    elm_widget_resize_object_set(obj, wd->clip);
323    elm_widget_sub_object_add(obj, wd->clip);
324
325    wd->rect = evas_object_rectangle_add(e);
326    elm_widget_sub_object_add(obj, wd->rect);
327    evas_object_color_set(wd->rect, 255, 255, 255, 0);
328    evas_object_clip_set(wd->rect, wd->clip);
329
330    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOVE, _move, obj);
331    evas_object_event_callback_add(obj, EVAS_CALLBACK_RESIZE, _resize, obj);
332
333    evas_object_smart_callbacks_descriptions_set(obj, _signals);
334
335    _mirrored_set(obj, elm_widget_mirrored_get(obj));
336    _sizing_eval(obj);
337    return obj;
338 }
339
340 /**
341  * Push an object to the top of the pager stack (and show it)
342  *
343  * The object pushed becomes a child of the pager and will be controlled
344  * it and deleted when the pager is deleted.
345  *
346  * @param obj The pager object
347  * @param content The object to push
348  *
349  * @ingroup Pager
350  * @warning It will be failed if the content exists on the stack already.
351  */
352 EAPI void
353 elm_pager_content_push(Evas_Object *obj, Evas_Object *content)
354 {
355    ELM_CHECK_WIDTYPE(obj, widtype);
356    Widget_Data *wd = elm_widget_data_get(obj);
357    Evas_Coord x, y, w, h;
358    Item *it;
359
360    if ((!wd) || (!content)) return;
361    if (_item_get(obj, content)) return;
362
363    it = ELM_NEW(Item);
364    if (!it) return;
365    it->obj = obj;
366    it->content = content;
367    it->base = edje_object_add(evas_object_evas_get(obj));
368    evas_object_smart_member_add(it->base, obj);
369    evas_object_geometry_get(obj, &x, &y, &w, &h);
370    evas_object_move(it->base, x, y);
371    evas_object_resize(it->base, w, h);
372    evas_object_clip_set(it->base, wd->clip);
373    elm_widget_sub_object_add(obj, it->base);
374    elm_widget_sub_object_add(obj, it->content);
375    _elm_theme_object_set(obj,
376                          it->base,
377                          "pager",
378                          "base",
379                          elm_widget_style_get(obj));
380    edje_object_signal_callback_add(it->base,
381                                    "elm,action,hide,finished",
382                                    "",
383                                    _signal_hide_finished,
384                                    it);
385    evas_object_event_callback_add(it->content,
386                                   EVAS_CALLBACK_CHANGED_SIZE_HINTS,
387                                   _changed_size_hints,
388                                   it);
389    evas_object_event_callback_add(it->content,
390                                   EVAS_CALLBACK_DEL,
391                                   _content_del,
392                                   obj);
393    edje_object_part_swallow(it->base, "elm.swallow.content", it->content);
394    edje_object_size_min_calc(it->base, &it->minw, &it->minh);
395    evas_object_data_set(it->base, "_elm_leaveme", obj);
396    evas_object_show(it->content);
397    wd->stack = eina_list_append(wd->stack, it);
398    _eval_top(obj);
399    _sizing_eval(obj);
400 }
401
402 /**
403  * Pop the object that is on top of the stack
404  *
405  * This pops the object that is on top (visible) in the pager, makes it
406  * disappear, then deletes the object. The object that was underneath it
407  * on the stack will become visible.
408  *
409  * @param obj The pager object
410  *
411  * @ingroup Pager
412  */
413 EAPI void
414 elm_pager_content_pop(Evas_Object *obj)
415 {
416    ELM_CHECK_WIDTYPE(obj, widtype);
417    Widget_Data *wd = elm_widget_data_get(obj);
418    Eina_List *ll;
419    Item *it;
420    if (!wd) return;
421    if (!wd->stack) return;
422    it = eina_list_last(wd->stack)->data;
423    it->popme = EINA_TRUE;
424    ll = eina_list_last(wd->stack);
425    if (ll)
426      {
427         ll = ll->prev;
428         if (!ll)
429           {
430              Evas_Object *o;
431              const char *onhide;
432
433              wd->top = it;
434              o = wd->top->base;
435              edje_object_signal_emit(o, "elm,action,pop", "elm");
436              wd->stack = eina_list_remove(wd->stack, it);
437              onhide = edje_object_data_get(o, "onhide");
438              if (onhide)
439                {
440                   if (!strcmp(onhide, "raise")) evas_object_raise(o);
441                   else if (!strcmp(onhide, "lower")) evas_object_lower(o);
442                }
443              wd->top = NULL;
444           }
445         else
446           {
447              it = ll->data;
448              elm_pager_content_promote(obj, it->content);
449           }
450      }
451 }
452
453 /**
454  * Promote an object already in the pager stack to the top of the stack
455  *
456  * This will take the indicated object and promote it to the top of the stack
457  * as if it had been pushed there. The object must already be inside the
458  * pager stack to work.
459  *
460  * @param obj The pager object
461  * @param content The object to promote
462  *
463  * @ingroup Pager
464  */
465 EAPI void
466 elm_pager_content_promote(Evas_Object *obj, Evas_Object *content)
467 {
468    ELM_CHECK_WIDTYPE(obj, widtype);
469    Widget_Data *wd = elm_widget_data_get(obj);
470    Eina_List *l;
471    Item *it;
472    if (!wd) return;
473    l = _item_get(obj, content);
474    if (!l) return;
475
476    it = l->data;
477    wd->stack = eina_list_remove_list(wd->stack, l);
478    wd->stack = eina_list_append(wd->stack, it);
479    _eval_top(obj);
480 }
481
482 /**
483  * Return the object at the bottom of the pager stack
484  *
485  * @param obj The pager object
486  * @return The bottom object or NULL if none
487  *
488  * @ingroup Pager
489  */
490 EAPI Evas_Object *
491 elm_pager_content_bottom_get(const Evas_Object *obj)
492 {
493    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
494    Widget_Data *wd = elm_widget_data_get(obj);
495    Item *it;
496    if (!wd) return NULL;
497    if (!wd->stack) return NULL;
498    it = wd->stack->data;
499    return it->content;
500 }
501
502 /**
503  * Return the object at the top of the pager stack
504  *
505  * @param obj The pager object
506  * @return The top object or NULL if none
507  *
508  * @ingroup Pager
509  */
510 EAPI Evas_Object *
511 elm_pager_content_top_get(const Evas_Object *obj)
512 {
513    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
514    Widget_Data *wd = elm_widget_data_get(obj);
515    if (!wd) return NULL;
516    if (!wd->top) return NULL;
517    return wd->top->content;
518 }
519