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