b1c029917be9aa9313daa10c01bb652dd3afeda5
[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 he 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
23 typedef struct _Widget_Data Widget_Data;
24 typedef struct _Item Item;
25
26 struct _Widget_Data
27 {
28    Eina_List *stack;
29    Item *top, *oldtop;
30    Evas_Object *rect, *clip;
31 };
32
33 struct _Item
34 {
35    Evas_Object *obj, *base, *content;
36    Evas_Coord minw, minh;
37    Eina_Bool popme : 1;
38 };
39
40 static const char *widtype = NULL;
41 static void _del_hook(Evas_Object *obj);
42 static void _theme_hook(Evas_Object *obj);
43 static void _sizing_eval(Evas_Object *obj);
44 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
45 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
46
47 static void
48 _del_hook(Evas_Object *obj)
49 {
50    Widget_Data *wd = elm_widget_data_get(obj);
51    if (!wd) return;
52    free(wd);
53 }
54
55 static void
56 _theme_hook(Evas_Object *obj)
57 {
58    Widget_Data *wd = elm_widget_data_get(obj);
59    Eina_List *l;
60    Item *it;
61    if (!wd) return;
62    EINA_LIST_FOREACH(wd->stack, l, it)
63      edje_object_scale_set(it->base, elm_widget_scale_get(obj) * 
64                            _elm_config->scale);
65    _sizing_eval(obj);
66 }
67
68 static void
69 _sizing_eval(Evas_Object *obj)
70 {
71    Widget_Data *wd = elm_widget_data_get(obj);
72    Evas_Coord minw = -1, minh = -1;
73    Eina_List *l;
74    Item *it;
75    if (!wd) return;
76    EINA_LIST_FOREACH(wd->stack, l, it)
77      {
78         if (it->minw > minw) minw = it->minw;
79         if (it->minh > minh) minh = it->minh;
80      }
81    evas_object_size_hint_min_set(obj, minw, minh);
82    evas_object_size_hint_max_set(obj, -1, -1);
83 }
84
85 static void
86 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
87 {
88    Item *it = data;
89    Evas_Coord minw = -1, minh = -1;
90    evas_object_size_hint_min_get(it->content, &minw, &minh);
91    // FIXME: why is this needed? how does edje get this unswallowed or
92    // lose its callbacks to edje
93    edje_object_part_swallow(it->base, "elm.swallow.content", it->content);
94    edje_object_size_min_calc(it->base, &it->minw, &it->minh);
95    _sizing_eval(it->obj);
96 }
97
98 static void
99 _eval_top(Evas_Object *obj)
100 {
101    Widget_Data *wd = elm_widget_data_get(obj);
102    Item *ittop;
103    if (!wd) return;
104    if (!wd->stack) return;
105    ittop = eina_list_last(wd->stack)->data;
106    if (ittop != wd->top)
107      {
108         Evas_Object *o;
109         const char *onshow, *onhide;
110
111         if (wd->top)
112           {
113              o = wd->top->base;
114              edje_object_signal_emit(o, "elm,action,hide", "elm");
115              onhide = edje_object_data_get(o, "onhide");
116              if (onhide)
117                {
118                   if (!strcmp(onhide, "raise")) evas_object_raise(o);
119                   else if (!strcmp(onhide, "lower")) evas_object_lower(o);
120                }
121           }
122         wd->top = ittop;
123         o = wd->top->base;
124         evas_object_show(o);
125         edje_object_signal_emit(o, "elm,action,show", "elm");
126         onshow = edje_object_data_get(o, "onshow");
127         if (onshow)
128           {
129              if (!strcmp(onshow, "raise")) evas_object_raise(o);
130              else if (!strcmp(onshow, "lower")) evas_object_lower(o);
131           }
132      }
133 }
134
135 static void
136 _move(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
137 {
138    Widget_Data *wd = elm_widget_data_get(data);
139    Evas_Coord x, y;
140    Eina_List *l;
141    Item *it;
142    if (!wd) return;
143    evas_object_geometry_get(obj, &x, &y, NULL, NULL);
144    EINA_LIST_FOREACH(wd->stack, l, it)
145      evas_object_move(it->base, x, y);
146 }
147
148 static void
149 _sub_del(void *data, Evas_Object *obj __UNUSED__, void *event_info)
150 {
151    Widget_Data *wd = elm_widget_data_get(data);
152    Evas_Object *sub = event_info;
153    Eina_List *l;
154    Item *it;
155    if (!wd) return;
156    EINA_LIST_FOREACH(wd->stack, l, it)
157      {
158         if (it->content == sub)
159           {
160              wd->stack = eina_list_remove_list(wd->stack, l);
161              evas_object_event_callback_del_full
162                (sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, it);
163              evas_object_del(it->base);
164              _eval_top(it->obj);
165              free(it);
166              return;
167           }
168      }
169 }
170
171 static void
172 _resize(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
173 {   
174    Widget_Data *wd = elm_widget_data_get(data);
175    Evas_Coord w, h;
176    Eina_List *l;
177    Item *it;
178    if (!wd) return;
179    evas_object_geometry_get(obj, NULL, NULL, &w, &h);
180    EINA_LIST_FOREACH(wd->stack, l, it) evas_object_resize(it->base, w, h);
181 }
182
183 static void
184 _signal_hide_finished(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
185 {
186    Item *it = data;
187    Evas_Object *obj2 = it->obj;
188    evas_object_hide(it->base);
189    edje_object_signal_emit(it->base, "elm,action,reset", "elm");
190    evas_object_smart_callback_call(obj2, "hide,finished", it->content);
191    edje_object_message_signal_process(it->base);
192    if (it->popme) evas_object_del(it->content);
193    _sizing_eval(obj2);
194 }
195
196 /**
197  * Add a new pager to the parent
198  *
199  * @param parent The parent object
200  * @return The new object or NULL if it cannot be created
201  *
202  * @ingroup Pager
203  */
204 EAPI Evas_Object *
205 elm_pager_add(Evas_Object *parent)
206 {
207    Evas_Object *obj;
208    Evas *e;
209    Widget_Data *wd;
210
211    wd = ELM_NEW(Widget_Data);
212    e = evas_object_evas_get(parent);
213    obj = elm_widget_add(e);
214    ELM_SET_WIDTYPE(widtype, "pager");
215    elm_widget_type_set(obj, "pager");
216    elm_widget_sub_object_add(parent, obj);
217    elm_widget_data_set(obj, wd);
218    elm_widget_del_hook_set(obj, _del_hook);
219    elm_widget_theme_hook_set(obj, _theme_hook);
220
221    wd->clip = evas_object_rectangle_add(e);
222    elm_widget_resize_object_set(obj, wd->clip);
223    elm_widget_sub_object_add(obj, wd->clip);
224
225    wd->rect = evas_object_rectangle_add(e);
226    elm_widget_sub_object_add(obj, wd->rect);
227    evas_object_color_set(wd->rect, 255, 255, 255, 0); 
228    evas_object_clip_set(wd->rect, wd->clip);
229
230    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOVE, _move, obj);
231    evas_object_event_callback_add(obj, EVAS_CALLBACK_RESIZE, _resize, obj);
232
233    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
234
235    _sizing_eval(obj);
236    return obj;
237 }
238
239 /**
240  * Push an object to the top of the pager stack (and show it)
241  *
242  * The object pushed becomes a child of the pager and will be controlled
243  * it and deleted when the pager is deleted.
244  *
245  * @param obj The pager object
246  * @param content The object to push
247  *
248  * @ingroup Pager
249  */
250 EAPI void
251 elm_pager_content_push(Evas_Object *obj, Evas_Object *content)
252 {
253    ELM_CHECK_WIDTYPE(obj, widtype);
254    Widget_Data *wd = elm_widget_data_get(obj);
255    Item *it = ELM_NEW(Item);
256    Evas_Coord x, y, w, h;
257    if (!wd) return;
258    if (!it) return;
259    it->obj = obj;
260    it->content = content;
261    it->base = edje_object_add(evas_object_evas_get(obj));
262    evas_object_smart_member_add(it->base, obj);
263    evas_object_geometry_get(obj, &x, &y, &w, &h);
264    evas_object_move(it->base, x, y);
265    evas_object_resize(it->base, w, h);
266    evas_object_clip_set(it->base, wd->clip);
267    elm_widget_sub_object_add(obj, it->base);
268    elm_widget_sub_object_add(obj, it->content);
269    _elm_theme_object_set(obj, it->base,  "pager", "base", elm_widget_style_get(obj));
270    edje_object_signal_callback_add(it->base, "elm,action,hide,finished", "", 
271                                    _signal_hide_finished, it);
272    evas_object_event_callback_add(it->content,
273                                   EVAS_CALLBACK_CHANGED_SIZE_HINTS,
274                                   _changed_size_hints, it);
275    edje_object_part_swallow(it->base, "elm.swallow.content", it->content);
276    edje_object_size_min_calc(it->base, &it->minw, &it->minh);
277    evas_object_show(it->content);
278    wd->stack = eina_list_append(wd->stack, it);
279    _eval_top(obj);
280    _sizing_eval(obj);
281 }
282
283 /**
284  * Pop the object that is on top of the stack
285  *
286  * This pops the object that is on top (visible) in the pager, makes it
287  * disappear, then deletes the object. The object that was underneath it
288  * on the stack will become visible.
289  *
290  * @param obj The pager object
291  *
292  * @ingroup Pager
293  */
294 EAPI void
295 elm_pager_content_pop(Evas_Object *obj)
296 {
297    ELM_CHECK_WIDTYPE(obj, widtype);
298    Widget_Data *wd = elm_widget_data_get(obj);
299    Eina_List *ll;
300    Item *it;
301    if (!wd) return;
302    if (!wd->stack) return;
303    it = eina_list_last(wd->stack)->data;
304    it->popme = EINA_TRUE;
305    ll = eina_list_last(wd->stack);
306    if (ll)
307      {
308         ll = ll->prev;
309         if (!ll)
310           {
311              Evas_Object *o;
312              const char *onhide;
313
314              wd->top = it;
315              o = wd->top->base;
316              edje_object_signal_emit(o, "elm,action,hide", "elm");
317              onhide = edje_object_data_get(o, "onhide");
318              if (onhide)
319                {
320                   if (!strcmp(onhide, "raise")) evas_object_raise(o);
321                   else if (!strcmp(onhide, "lower")) evas_object_lower(o);
322                }
323              wd->top = NULL;
324           }
325         else
326           {
327              it = ll->data;
328              elm_pager_content_promote(obj, it->content);
329           }
330      }
331 }
332
333 /**
334  * Promote an object already in the pager stack to the top of the stack
335  *
336  * This will take the indicated object and promote it to the top of the stack
337  * as if it had been pushed there. The object must already be inside the
338  * pager stack to work.
339  *
340  * @param obj The pager object
341  * @param content The object to promote
342  *
343  * @ingroup Pager
344  */
345 EAPI void
346 elm_pager_content_promote(Evas_Object *obj, Evas_Object *content)
347 {
348    ELM_CHECK_WIDTYPE(obj, widtype);
349    Widget_Data *wd = elm_widget_data_get(obj);
350    Eina_List *l;
351    Item *it;
352    if (!wd) return;
353    EINA_LIST_FOREACH(wd->stack, l, it)
354      {
355         if (it->content == content)
356           {
357              wd->stack = eina_list_remove_list(wd->stack, l);
358              wd->stack = eina_list_append(wd->stack, it);
359              _eval_top(obj);
360              return;
361           }
362      }
363 }
364
365 /**
366  * Return the object at the bottom of the pager stack
367  *
368  * @param obj The pager object
369  * @return The bottom object or NULL if none
370  *
371  * @ingroup Pager
372  */
373 EAPI Evas_Object *
374 elm_pager_content_bottom_get(const Evas_Object *obj)
375 {
376    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
377    Widget_Data *wd = elm_widget_data_get(obj);
378    Item *it;
379    if (!wd) return NULL;
380    if (!wd->stack) return NULL;
381    it = wd->stack->data;
382    return it->content;
383 }
384
385 /**
386  * Return the object at the top of the pager stack
387  *
388  * @param obj The pager object
389  * @return The top object or NULL if none
390  *
391  * @ingroup Pager
392  */
393 EAPI Evas_Object *
394 elm_pager_content_top_get(const Evas_Object *obj)
395 {
396    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
397    Widget_Data *wd = elm_widget_data_get(obj);
398    Item *it;
399    if (!wd) return NULL;
400    if (!wd->stack) return NULL;
401    it = eina_list_last(wd->stack)->data;
402    return it->content;
403 }
404