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