c74ff21b16691c4c4f4afdb925e328413442898f
[framework/uifw/elementary.git] / src / lib / elm_slideshow.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup Slideshow slideshow
6  * @ingroup Elementary
7  *
8  * This object display a list of object (generally a list of images) and some actions like
9  * next/previous are used to navigate. The animations are defined in the theme,
10  * consequently new animations can be added without having to update the
11  * applications.
12  *
13  * The slideshow use 2 callbacks to create and delete the objects displayed. When an item
14  * is displayed the function itc->func.get() is called. This function should create the object,
15  * for example the object can be an evas_object_image or a photocam. When a object is no more
16  * displayed the function itc->func.del() is called, the user can delete the dana associated to the item.
17  */
18
19 typedef struct _Widget_Data Widget_Data;
20
21 struct _Elm_Slideshow_Item
22 {
23    Evas_Object *obj;
24
25    Eina_List *l, *l_built;
26
27    const void *data;
28    const Elm_Slideshow_Item_Class *itc;
29
30    Evas_Object *o;
31 };
32
33 struct _Widget_Data
34 {
35    Evas_Object *slideshow;
36
37    // list of Elm_Slideshow_Item*
38    Eina_List *items;
39    Eina_List *items_built;
40
41    Elm_Slideshow_Item *current;
42    Elm_Slideshow_Item *previous;
43
44    Eina_List *transitions;
45    const char *transition;
46
47    Ecore_Timer *timer;
48    int timeout;
49    Eina_Bool loop:1;
50
51    struct {
52            const char *current;
53            Eina_List *list; //list of const char *
54    } layout;
55 };
56
57 static const char *widtype = NULL;
58 static void _del_hook(Evas_Object *obj);
59 static void _theme_hook(Evas_Object *obj);
60 static void _sizing_eval(Evas_Object *obj);
61 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
62 static Eina_Bool _timer_cb(void *data);
63
64 static void
65 _del_hook(Evas_Object *obj)
66 {
67    const char *layout;
68    Widget_Data *wd = elm_widget_data_get(obj);
69    if (!wd) return;
70    elm_slideshow_clear(obj);
71    elm_widget_stringlist_free(wd->transitions);
72    if (wd->timer) ecore_timer_del(wd->timer);
73    EINA_LIST_FREE(wd->layout.list, layout)
74            eina_stringshare_del(layout);
75    free(wd);
76 }
77
78 static void
79 _theme_hook(Evas_Object *obj)
80 {
81    Widget_Data *wd = elm_widget_data_get(obj);
82    if (!wd) return;
83    _elm_theme_object_set(obj, wd->slideshow, "slideshow", "base", elm_widget_style_get(obj));
84    edje_object_scale_set(wd->slideshow, elm_widget_scale_get(obj) *
85                          _elm_config->scale);
86    _sizing_eval(obj);
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    if (!wd) return;
95    edje_object_size_min_calc(wd->slideshow, &minw, &minh);
96    evas_object_size_hint_min_set(obj, minw, minh);
97    evas_object_size_hint_max_set(obj, minw, minh);
98 }
99
100
101 static Elm_Slideshow_Item* _item_prev_get(Elm_Slideshow_Item* item)
102 {
103         Widget_Data *wd = elm_widget_data_get(item->obj);
104         Elm_Slideshow_Item* prev = eina_list_data_get(eina_list_prev(item->l));
105         if(!prev && wd->loop)
106                 prev = eina_list_data_get(eina_list_last(item->l));
107         return prev;
108 }
109 static Elm_Slideshow_Item* _item_next_get(Elm_Slideshow_Item* item)
110 {
111         Widget_Data *wd = elm_widget_data_get(item->obj);
112         Elm_Slideshow_Item* next = eina_list_data_get(eina_list_next(item->l));
113         if(!next && wd->loop)
114                 next = eina_list_data_get(wd->items);
115         return next;
116 }
117
118
119 static void
120 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
121 {
122    _sizing_eval(data);
123 }
124
125 static void
126 _sub_del(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
127 {
128    ;
129 }
130
131 static void
132 _item_realize(Elm_Slideshow_Item *item)
133 {
134    Elm_Slideshow_Item *_item;
135    Evas_Object *obj = item->obj;
136    Widget_Data *wd = elm_widget_data_get(obj);
137
138    if (!wd) return;
139    if ((!item->o) && (item->itc->func.get))
140      {
141         item->o = item->itc->func.get((void*)item->data, obj);
142         evas_object_smart_member_add(item->o, obj);
143         item->l_built = eina_list_append(NULL, item);
144         wd->items_built = eina_list_merge(wd->items_built, item->l_built);
145         evas_object_hide(item->o);
146      }
147    else if (item->l_built)
148      wd->items_built = eina_list_demote_list(wd->items_built, item->l_built);
149
150    //pre-create previous and next item
151    _item = _item_next_get(item);
152    if ((_item) && (!_item->o) && (_item->itc->func.get))
153      {
154         _item->o = _item->itc->func.get((void*)_item->data, obj);
155         evas_object_smart_member_add(_item->o, obj);
156         _item->l_built = eina_list_append(NULL, _item);
157         wd->items_built = eina_list_merge(wd->items_built, _item->l_built);
158         evas_object_hide(_item->o);
159      }
160    else if ((_item) && (_item->l_built))
161      wd->items_built = eina_list_demote_list(wd->items_built, _item->l_built);
162
163    _item = _item_prev_get(item);
164    if ((_item) && (!_item->o) && (_item->itc->func.get))
165      {
166         _item->o = _item->itc->func.get((void*)_item->data, obj);
167         evas_object_smart_member_add(_item->o, obj);
168         _item->l_built = eina_list_append(NULL, _item);
169         wd->items_built = eina_list_merge(wd->items_built, _item->l_built);
170         evas_object_hide(_item->o);
171      }
172    else if ((_item) && (_item->l_built))
173      wd->items_built = eina_list_demote_list(wd->items_built, _item->l_built);
174
175    //delete unused items
176    while (eina_list_count(wd->items_built) > 3)
177      {
178         _item = eina_list_data_get(wd->items_built);
179         wd->items_built = eina_list_remove_list(wd->items_built, wd->items_built);
180         if(item->itc->func.del)
181           item->itc->func.del((void*)item->data, _item->o);
182         evas_object_del(_item->o);
183         _item->o = NULL;
184      }
185 }
186
187 static void
188 _end(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
189 {
190         Elm_Slideshow_Item *item;
191         Widget_Data *wd = elm_widget_data_get(data);
192         if (!wd) return;
193
194         item = wd->previous;
195         if(item)
196         {
197                 edje_object_part_unswallow(NULL, item->o);
198                 evas_object_hide(item->o);
199                 wd->previous = NULL;
200         }
201
202
203         item = wd->current;
204         if (!item || !item->o) return;
205
206         _item_realize(item);
207         edje_object_part_unswallow(NULL, item->o);
208         evas_object_show(item->o);
209
210         edje_object_signal_emit(wd->slideshow, "anim,end", "slideshow");
211         edje_object_part_swallow(wd->slideshow, "elm.swallow.1", item->o);
212 }
213
214
215 static Eina_Bool
216 _timer_cb(void *data)
217 {
218    Evas_Object *obj = data;
219    Widget_Data *wd = elm_widget_data_get(obj);
220    if (!wd) return ECORE_CALLBACK_CANCEL;
221    wd->timer = NULL;
222    elm_slideshow_next(obj);
223    return ECORE_CALLBACK_CANCEL;
224 }
225
226
227
228 /**
229  * Add a new slideshow to the parent
230  *
231  * @param parent The parent object
232  * @return The new object or NULL if it cannot be created
233  *
234  * @ingroup Slideshow
235  */
236 EAPI Evas_Object *
237 elm_slideshow_add(Evas_Object *parent)
238 {
239    Evas_Object *obj;
240    Evas *e;
241    Widget_Data *wd;
242
243    wd = ELM_NEW(Widget_Data);
244    e = evas_object_evas_get(parent);
245    obj = elm_widget_add(e);
246    ELM_SET_WIDTYPE(widtype, "slideshow");
247    elm_widget_type_set(obj, "slideshow");
248    elm_widget_sub_object_add(parent, obj);
249    elm_widget_data_set(obj, wd);
250    elm_widget_del_hook_set(obj, _del_hook);
251    elm_widget_theme_hook_set(obj, _theme_hook);
252
253    wd->current = NULL;
254    wd->previous = NULL;
255
256    wd->slideshow = edje_object_add(e);
257    _elm_theme_object_set(obj, wd->slideshow, "slideshow", "base", "default");
258    evas_object_smart_member_add(wd->slideshow, obj);
259    elm_widget_resize_object_set(obj, wd->slideshow);
260    evas_object_show(wd->slideshow);
261
262    wd->transitions = elm_widget_stringlist_get(edje_object_data_get(wd->slideshow, "transitions"));
263    if (eina_list_count(wd->transitions) > 0)
264      wd->transition = eina_stringshare_add(eina_list_data_get(wd->transitions));
265
266    wd->layout.list = elm_widget_stringlist_get(edje_object_data_get(wd->slideshow, "layouts"));
267    if (eina_list_count(wd->layout.list) > 0)
268        wd->layout.current = eina_list_data_get(wd->layout.list);
269
270    edje_object_signal_callback_add(wd->slideshow, "end", "slideshow", _end, obj);
271
272    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
273    evas_object_event_callback_add(obj, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
274
275    _sizing_eval(obj);
276    return obj;
277 }
278
279 /**
280  * Add a object in the list. The object can be a evas object image or a elm photo for example.
281  *
282  * @param obj The slideshow object
283  * @aram itc Callbacks used to create the object and delete the data associated when the item is deleted.
284  * @param data Data used by the user to identified the item
285  * @return Returns The slideshow item
286  *
287  * @ingroup Slideshow
288  */
289 EAPI Elm_Slideshow_Item*
290 elm_slideshow_item_add(Evas_Object *obj, const Elm_Slideshow_Item_Class *itc, const void *data)
291 {
292    Elm_Slideshow_Item *item;
293    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
294    Widget_Data *wd = elm_widget_data_get(obj);
295
296    if (!wd) return NULL;
297    item = calloc(1, sizeof(Elm_Slideshow_Item));
298    item->data = data;
299    item->itc = itc;
300    item->obj = obj;
301    item->l = eina_list_append(item->l, item);
302
303    wd->items = eina_list_merge(wd->items, item->l);
304
305    if (!wd->current) elm_slideshow_show(item);
306
307    return item;
308 }
309
310 /**
311  * Go to the item
312  *
313  * @param obj The slideshow object
314  * @param item The item
315  *
316  * @ingroup Slideshow
317  */
318 EAPI void
319 elm_slideshow_show(Elm_Slideshow_Item *item)
320 {
321    char buf[1024];
322    Elm_Slideshow_Item *next = NULL;
323    Widget_Data *wd;
324    if (!item)
325      return;
326    ELM_CHECK_WIDTYPE(item->obj, widtype);
327    wd = elm_widget_data_get(item->obj);
328    if (!wd)
329      return;
330    if (item == wd->current)
331      return;
332
333    next = item;
334    _end(item->obj, item->obj, NULL, NULL);
335
336    if (wd->timer) ecore_timer_del(wd->timer);
337    if (wd->timeout > 0)
338            wd->timer = ecore_timer_add(wd->timeout, _timer_cb, item->obj);
339    _item_realize(next);
340    edje_object_part_swallow(wd->slideshow, "elm.swallow.2", next->o);
341    evas_object_show(next->o);
342    snprintf(buf, sizeof(buf), "%s,next", wd->transition);
343    edje_object_signal_emit(wd->slideshow, buf, "slideshow");
344    wd->previous = wd->current;
345    wd->current = next;
346
347 }
348
349 /**
350  * Go to the next item
351  *
352  * @param obj The slideshow object
353  *
354  * @ingroup Slideshow
355  */
356 EAPI void
357 elm_slideshow_next(Evas_Object *obj)
358 {
359    char buf[1024];
360    Elm_Slideshow_Item *next = NULL;
361    ELM_CHECK_WIDTYPE(obj, widtype);
362    Widget_Data *wd = elm_widget_data_get(obj);
363
364    if (!wd) return;
365    
366    if (wd->current)
367            next = _item_next_get(wd->current);
368
369    if (!next || next == wd->current) return;
370    
371
372    _end(obj, obj, NULL, NULL);
373
374    if (wd->timer) ecore_timer_del(wd->timer);
375    if (wd->timeout > 0)
376            wd->timer = ecore_timer_add(wd->timeout, _timer_cb, obj);
377
378    _item_realize(next);
379
380    edje_object_part_swallow(wd->slideshow, "elm.swallow.2", next->o);
381    evas_object_show(next->o);
382
383    snprintf(buf, sizeof(buf), "%s,next", wd->transition);
384    edje_object_signal_emit(wd->slideshow, buf, "slideshow");
385
386    wd->previous = wd->current;
387    wd->current = next;
388 }
389
390 /**
391  * Go to the previous item
392  *
393  * @param obj The slideshow object
394  *
395  * @ingroup Slideshow
396  */
397 EAPI void
398 elm_slideshow_previous(Evas_Object *obj)
399 {
400    char buf[1024];
401    Elm_Slideshow_Item *prev = NULL;
402    ELM_CHECK_WIDTYPE(obj, widtype);
403    Widget_Data *wd = elm_widget_data_get(obj);
404
405    if (!wd) return;
406
407    if (wd->current)
408      prev = _item_prev_get(wd->current);
409
410    if (!prev ||  prev == wd->current) return;
411
412    _end(obj, obj, NULL, NULL);
413
414    if (wd->timer) ecore_timer_del(wd->timer);
415    if (wd->timeout > 0)
416            wd->timer = ecore_timer_add(wd->timeout, _timer_cb, obj);
417
418    _item_realize(prev);
419
420    edje_object_part_swallow(wd->slideshow, "elm.swallow.2", prev->o);
421    evas_object_show(prev->o);
422
423    snprintf(buf, 1024, "%s,previous", wd->transition);
424    edje_object_signal_emit(wd->slideshow, buf, "slideshow");
425
426    wd->previous = wd->current;
427    wd->current = prev;
428 }
429
430 /**
431  * Returns the list of transitions available.
432  *
433  * @param obj The slideshow object
434  * @return Returns the list of transitions (list of const char*)
435  *
436  * @ingroup Slideshow
437  */
438 const Eina_List *
439 elm_slideshow_transitions_get(const Evas_Object *obj)
440 {
441    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
442    Widget_Data *wd = elm_widget_data_get(obj);
443    if (!wd) return NULL;
444    return wd->transitions;
445 }
446
447 /**
448  * Returns the list of layouts available.
449  *
450  * @param obj The slideshow object
451  * @return Returns the list of layout (list of const char*)
452  *
453  * @ingroup Slideshow
454  */
455 const Eina_List *
456 elm_slideshow_layouts_get(const Evas_Object *obj)
457 {
458    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
459    Widget_Data *wd = elm_widget_data_get(obj);
460    if (!wd) return NULL;
461    return wd->layout.list;
462 }
463
464 /**
465  * Set the transition to use
466  *
467  * @param obj The slideshow object
468  * @param transition the new transition
469  *
470  * @ingroup Slideshow
471  */
472 EAPI void
473 elm_slideshow_transition_set(Evas_Object *obj, const char *transition)
474 {
475    ELM_CHECK_WIDTYPE(obj, widtype);
476    Widget_Data *wd = elm_widget_data_get(obj);
477    if (!wd) return;
478    eina_stringshare_replace(&wd->transition, transition);
479 }
480
481 /**
482  * Returns the transition to use
483  *
484  * @param obj The slideshow object
485  * @return the transition set
486  *
487  * @ingroup Slideshow
488  */
489 EAPI const char *
490 elm_slideshow_transition_get(const Evas_Object *obj)
491 {
492    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
493    Widget_Data *wd = elm_widget_data_get(obj);
494    if (!wd) return NULL;
495    return wd->transition;
496 }
497
498 /**
499  * The slideshow can go to the next item automatically after a few seconds.
500  * This method set the timeout to use. A timeout <=0 disable the timer.
501  *
502  * @param obj The slideshow object
503  * @param timeout The new timeout
504  *
505  * @ingroup Slideshow
506  */
507 EAPI void
508 elm_slideshow_timeout_set(Evas_Object *obj ,int timeout)
509 {
510    ELM_CHECK_WIDTYPE(obj, widtype);
511    Widget_Data *wd = elm_widget_data_get(obj);
512    if (!wd) return;
513    wd->timeout = timeout;
514    if (wd->timer) ecore_timer_del(wd->timer);
515    wd->timer = NULL;
516    if (timeout > 0)
517      wd->timer = ecore_timer_add(timeout, _timer_cb, obj);
518 }
519
520 /**
521  * Returns the timeout value
522  *
523  * @param obj The slideshow object
524  * @return Returns the timeout
525  *
526  * @ingroup Slideshow
527  */
528 EAPI int
529 elm_slideshow_timeout_get(const Evas_Object *obj)
530 {
531    ELM_CHECK_WIDTYPE(obj, widtype) -1;
532    Widget_Data *wd = elm_widget_data_get(obj);
533    if (!wd) return -1;
534    return wd->timeout;
535 }
536
537 /**
538  * Set if the first item should follow the last and vice versa
539  *
540  * @param obj The slideshow object
541  * @param loop if EINA_TRUE, the first item will follow the last and vice versa
542  *
543  * @ingroup Slideshow
544  */
545 EAPI void
546 elm_slideshow_loop_set(Evas_Object *obj, Eina_Bool loop)
547 {
548    ELM_CHECK_WIDTYPE(obj, widtype);
549    Widget_Data *wd = elm_widget_data_get(obj);
550    if (!wd) return;
551    wd->loop = loop;
552 }
553
554 /**
555  * Returns the current layout name
556  *
557  * @param obj The slideshow object
558  * @returns Returns the layout name
559  *
560  * @ingroup Slideshow
561  */
562 EAPI const char *
563 elm_slideshow_layout_get(const Evas_Object *obj)
564 {
565    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
566    Widget_Data *wd = elm_widget_data_get(obj);
567    if (!wd) return EINA_FALSE;
568    return wd->layout.current;
569 }
570
571 /**
572  * Set the layout
573  *
574  * @param obj The slideshow object
575  * @param layout the new layout
576  *
577  * @ingroup Slideshow
578  */
579 EAPI void
580 elm_slideshow_layout_set(Evas_Object *obj, const char *layout)
581 {
582    char buf[PATH_MAX];
583    ELM_CHECK_WIDTYPE(obj, widtype);
584    Widget_Data *wd = elm_widget_data_get(obj);
585    if (!wd) return;
586
587    wd->layout.current = layout;
588    snprintf(buf, sizeof(buf), "layout,%s", layout);
589    edje_object_signal_emit(wd->slideshow, buf, "slideshow");
590 }
591
592 /**
593  * Return if the first item should follow the last and vice versa
594  *
595  * @param obj The slideshow object
596  * @returns Returns the loop flag
597  *
598  * @ingroup Slideshow
599  */
600 EAPI Eina_Bool
601 elm_slideshow_loop_get(const Evas_Object *obj)
602 {
603    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
604    Widget_Data *wd = elm_widget_data_get(obj);
605    if (!wd) return EINA_FALSE;
606    return wd->loop;
607 }
608
609 /**
610  * Delete all the items
611  *
612  * @param obj The slideshow object
613  *
614  * @ingroup Slideshow
615  */
616 EAPI void
617 elm_slideshow_clear(Evas_Object *obj)
618 {
619         Elm_Slideshow_Item *item;
620         ELM_CHECK_WIDTYPE(obj, widtype);
621         Widget_Data *wd = elm_widget_data_get(obj);
622         if (!wd) return;
623         wd->previous = NULL;
624         wd->current = NULL;
625         EINA_LIST_FREE(wd->items_built, item)
626         {
627                 if (item->itc->func.del)
628                         item->itc->func.del((void*)item->data, item->o);
629                 evas_object_del(item->o);
630         }
631
632         EINA_LIST_FREE(wd->items, item)
633         {
634                 free(item);
635         }
636 }
637
638
639 /**
640  * Delete the item
641  *
642  * @param item The slideshow item
643  *
644  * @ingroup Slideshow
645  */
646 EAPI void
647 elm_slideshow_item_del(Elm_Slideshow_Item *item)
648 {
649         if (!item) return;
650         Widget_Data *wd = elm_widget_data_get(item->obj);
651         if (!wd) return;
652         if (wd->previous == item) wd->previous = NULL;
653         if (wd->current == item)
654         {
655                 Eina_List *l = eina_list_data_find_list(wd->items, item);
656                 Eina_List *l2 = eina_list_next(l);
657                 wd->current = NULL;
658                 if (!l2)
659                         l2 = eina_list_nth_list(wd->items, eina_list_count(wd->items) - 1);
660                 if (l2)
661                         elm_slideshow_show(eina_list_data_get(l2));
662         }
663
664         wd->items = eina_list_remove_list(wd->items, item->l);
665         wd->items_built = eina_list_remove_list(wd->items_built, item->l_built);
666
667         if (item->o && item->itc->func.del)
668                 item->itc->func.del((void*)item->data, wd->previous->o);
669         if (item->o)
670                 evas_object_del(item->o);
671         free(item);
672 }
673
674 /**
675  * Returns the list of items
676  * @param obj The slideshow object
677  * @return Returns the list of items (list of Elm_Slideshow_Item).
678  *
679  * @ingroup Slideshow
680  */
681 EAPI const Eina_List *
682 elm_slideshow_items_get(const Evas_Object *obj)
683 {
684    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
685    Widget_Data *wd = elm_widget_data_get(obj);
686    if (!wd) return NULL;
687    return wd->items;
688 }
689
690
691 /**
692  * Returns the current item displayed
693  *
694  * @param obj The slideshow object
695  * @return Returns the current item displayed
696  *
697  * @ingroup Slideshow
698  */
699 EAPI Elm_Slideshow_Item *
700 elm_slideshow_item_current_get(const Evas_Object *obj)
701 {
702    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
703    Widget_Data *wd = elm_widget_data_get(obj);
704    if (!wd) return NULL;
705    return wd->current;
706 }
707
708 /**
709  * Returns the evas object associated to an item
710  *
711  * @param item The slideshow item
712  * @return Returns the evas object associated to this item
713  *
714  * @ingroup Slideshow
715  */
716 EAPI Evas_Object *
717 elm_slideshow_item_object_get(Elm_Slideshow_Item * item)
718 {
719    if (!item) return NULL;
720    return item->o;
721 }
722
723 /**
724  * Returns the data associated to an item
725  *
726  * @param item The slideshow item
727  * @return Returns the data associated to this item
728  *
729  * @ingroup Slideshow
730  */
731 EAPI void *
732 elm_slideshow_item_data_get(Elm_Slideshow_Item * item)
733 {
734    if (!item) return NULL;
735    return (void *)item->data;
736 }