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