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