b97d8592339b3395d778d42816462d9b97d34c63
[framework/uifw/elementary.git] / src / examples / slideshow_example.c
1 /**
2  * Simple Elementary's <b>slide show widget</b> example, illustrating its
3  * usage and API.
4  *
5  * See stdout/stderr for output. Compile with:
6  *
7  * @verbatim
8  * gcc -g `pkg-config --cflags --libs elementary` slideshow_example.c -o slideshow_example
9  * @endverbatim
10  */
11
12 #include <Elementary.h>
13 #ifdef HAVE_CONFIG_H
14 # include "elementary_config.h"
15 #else
16 # define __UNUSED__
17 # define PACKAGE_DATA_DIR "../../data"
18 #endif
19
20 static void
21 _on_done(void        *data __UNUSED__,
22          Evas_Object *obj __UNUSED__,
23          void        *event_info __UNUSED__)
24 {
25    elm_exit();
26 }
27
28 static Evas_Object *slideshow, *bt_start, *bt_stop;
29 static Elm_Slideshow_Item_Class itc;
30
31 static const char *img1 = PACKAGE_DATA_DIR "/images/logo.png";
32 static const char *img2 = PACKAGE_DATA_DIR "/images/plant_01.jpg";
33 static const char *img3 = PACKAGE_DATA_DIR "/images/rock_01.jpg";
34 static const char *img4 = PACKAGE_DATA_DIR "/images/rock_02.jpg";
35 static const char *img5 = PACKAGE_DATA_DIR "/images/sky_01.jpg";
36 static const char *img6 = PACKAGE_DATA_DIR "/images/sky_04.jpg";
37 static const char *img7 = PACKAGE_DATA_DIR "/images/wood_01.jpg";
38 static const char *img8 = PACKAGE_DATA_DIR "/images/mystrale.jpg";
39 static const char *img9 = PACKAGE_DATA_DIR "/images/mystrale_2.jpg";
40
41 static void
42 _notify_show(void        *data,
43              Evas        *e __UNUSED__,
44              Evas_Object *obj __UNUSED__,
45              void        *event_info __UNUSED__)
46 {
47    evas_object_show(data);
48 }
49
50 /* jump to next item, cyclically */
51 static void
52 _next(void        *data,
53       Evas_Object *obj __UNUSED__,
54       void        *event_info __UNUSED__)
55 {
56    elm_slideshow_next(data);
57 }
58
59 static void
60 _previous(void        *data,
61           Evas_Object *obj __UNUSED__,
62           void        *event_info __UNUSED__)
63 {
64    elm_slideshow_previous(data);
65 }
66
67 static void
68 _first(void        *data,
69        Evas_Object *obj __UNUSED__,
70        void        *event_info __UNUSED__)
71 {
72    elm_slideshow_item_show(data);
73 }
74
75 static void
76 _last(void        *data,
77       Evas_Object *obj __UNUSED__,
78       void        *event_info __UNUSED__)
79 {
80    elm_slideshow_item_show(data);
81 }
82
83 static void
84 _mouse_in(void        *data,
85           Evas        *e __UNUSED__,
86           Evas_Object *obj __UNUSED__,
87           void        *event_info __UNUSED__)
88 {
89    elm_notify_timeout_set(data, 0.0);
90    evas_object_show(data);
91 }
92
93 static void
94 _mouse_out(void        *data,
95            Evas        *e __UNUSED__,
96            Evas_Object *obj __UNUSED__,
97            void        *event_info __UNUSED__)
98 {
99    elm_notify_timeout_set(data, 3.0);
100 }
101
102 /* transition changed */
103 static void
104 _transition_select(void        *data,
105            Evas_Object *obj,
106            void        *event_info __UNUSED__)
107 {
108    elm_slideshow_transition_set(slideshow, data);
109    elm_object_text_set(obj, data);
110 }
111
112 static void
113 _layout_select(void        *data,
114                Evas_Object *obj,
115                void        *event_info __UNUSED__)
116 {
117    elm_slideshow_layout_set(slideshow, data);
118    elm_object_text_set(obj, data);
119 }
120
121 /* start the show! */
122 static void
123 _start(void        *data,
124        Evas_Object *obj __UNUSED__,
125        void        *event_info __UNUSED__)
126 {
127    elm_slideshow_timeout_set(slideshow, elm_spinner_value_get(data));
128
129    elm_object_disabled_set(bt_start, EINA_TRUE);
130    elm_object_disabled_set(bt_stop, EINA_FALSE);
131 }
132
133 static void
134 _stop(void        *data __UNUSED__,
135       Evas_Object *obj __UNUSED__,
136       void        *event_info __UNUSED__)
137 {
138    elm_slideshow_timeout_set(slideshow, 0.0);
139    elm_object_disabled_set(bt_start, EINA_FALSE);
140    elm_object_disabled_set(bt_stop, EINA_TRUE);
141 }
142
143 /* slideshow transition time has changed */
144 static void
145 _spin(void        *data,
146       Evas_Object *obj __UNUSED__,
147       void        *event_info __UNUSED__)
148 {
149    if (elm_slideshow_timeout_get(slideshow) > 0)
150      elm_slideshow_timeout_set(slideshow, elm_spinner_value_get(data));
151 }
152
153 /* get our images to make slideshow items */
154 static Evas_Object *
155 _get(void        *data,
156      Evas_Object *obj)
157 {
158    Evas_Object *photo = elm_photo_add(obj);
159    elm_photo_file_set(photo, data);
160    elm_photo_fill_inside_set(photo, EINA_TRUE);
161    elm_object_style_set(photo, "shadow");
162
163    return photo;
164 }
165
166 /* ordering alphabetically */
167 static int
168 _cmp_func(const void *data1,
169           const void *data2)
170 {
171    const char *img_path1, *img_path2;
172
173    const Elm_Object_Item *slide_it1 = data1;
174    const Elm_Object_Item *slide_it2 = data2;
175
176    img_path1 = elm_object_item_data_get(slide_it1);
177    img_path2 = elm_object_item_data_get(slide_it2);
178
179    return strcasecmp(img_path1, img_path2);
180 }
181
182 int
183 elm_main(int    argc __UNUSED__,
184          char **argv __UNUSED__)
185 {
186    Evas_Object *win, *bg, *notify, *bx, *bt, *hv, *spin;
187    Elm_Object_Item *slide_first, *slide_last, *slide_it;
188    const char *transition, *layout;
189    const Eina_List *l, *list;
190
191    win = elm_win_add(NULL, "slideshow", ELM_WIN_BASIC);
192    elm_win_title_set(win, "Slideshow example");
193    evas_object_smart_callback_add(win, "delete,request", _on_done, NULL);
194
195    bg = elm_bg_add(win);
196    elm_win_resize_object_add(win, bg);
197    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
198    evas_object_show(bg);
199
200    slideshow = elm_slideshow_add(win);
201    elm_slideshow_loop_set(slideshow, EINA_TRUE);
202    elm_win_resize_object_add(win, slideshow);
203    evas_object_size_hint_weight_set(slideshow,
204                                     EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
205    evas_object_show(slideshow);
206
207    itc.func.get = _get;
208    itc.func.del = NULL;
209
210    slide_first = elm_slideshow_item_sorted_insert(slideshow, &itc, img1, _cmp_func);
211    elm_slideshow_item_sorted_insert(slideshow, &itc, img2, _cmp_func);
212    elm_slideshow_item_sorted_insert(slideshow, &itc, img3, _cmp_func);
213    elm_slideshow_item_sorted_insert(slideshow, &itc, img4, _cmp_func);
214    elm_slideshow_item_sorted_insert(slideshow, &itc, img5, _cmp_func);
215    elm_slideshow_item_sorted_insert(slideshow, &itc, img6, _cmp_func);
216    elm_slideshow_item_sorted_insert(slideshow, &itc, img7, _cmp_func);
217    elm_slideshow_item_sorted_insert(slideshow, &itc, img8, _cmp_func);
218    slide_last = elm_slideshow_item_add(slideshow, &itc, img9);
219
220    list = elm_slideshow_items_get(slideshow);
221    fprintf(stdout, "List of items in the slideshow:\n");
222    EINA_LIST_FOREACH(list, l, slide_it)
223      fprintf(stdout, "\t%s\n",
224              (const char *)elm_object_item_data_get(slide_it));
225
226    notify = elm_notify_add(win);
227    elm_notify_orient_set(notify, ELM_NOTIFY_ORIENT_BOTTOM);
228    elm_win_resize_object_add(win, notify);
229    elm_notify_timeout_set(notify, 3.0);
230
231    bx = elm_box_add(win);
232    elm_box_horizontal_set(bx, EINA_TRUE);
233    elm_object_content_set(notify, bx);
234    evas_object_show(bx);
235
236    evas_object_event_callback_add(bx, EVAS_CALLBACK_MOUSE_IN, _mouse_in,
237                                   notify);
238    evas_object_event_callback_add(bx, EVAS_CALLBACK_MOUSE_OUT, _mouse_out,
239                                   notify);
240
241    bt = elm_button_add(win);
242    elm_object_text_set(bt, "Previous");
243    evas_object_smart_callback_add(bt, "clicked", _previous, slideshow);
244    elm_box_pack_end(bx, bt);
245    evas_object_show(bt);
246
247    bt = elm_button_add(win);
248    elm_object_text_set(bt, "Next");
249    evas_object_smart_callback_add(bt, "clicked", _next, slideshow);
250    elm_box_pack_end(bx, bt);
251    evas_object_show(bt);
252
253    bt = elm_button_add(win);
254    elm_object_text_set(bt, "First");
255    evas_object_smart_callback_add(bt, "clicked", _first, slide_first);
256    elm_box_pack_end(bx, bt);
257    evas_object_show(bt);
258
259    bt = elm_button_add(win);
260    elm_object_text_set(bt, "Last");
261    evas_object_smart_callback_add(bt, "clicked", _last, slide_last);
262    elm_box_pack_end(bx, bt);
263    evas_object_show(bt);
264
265    hv = elm_hoversel_add(win);
266    elm_box_pack_end(bx, hv);
267    elm_hoversel_hover_parent_set(hv, win);
268    EINA_LIST_FOREACH(elm_slideshow_transitions_get(slideshow), l, transition)
269      elm_hoversel_item_add(hv, transition, NULL, 0, _transition_select,
270                            transition);
271    elm_object_text_set(hv, eina_list_data_get(
272                          elm_slideshow_transitions_get(slideshow)));
273    evas_object_show(hv);
274
275    hv = elm_hoversel_add(win);
276    elm_box_pack_end(bx, hv);
277    elm_hoversel_hover_parent_set(hv, win);
278    EINA_LIST_FOREACH(elm_slideshow_layouts_get(slideshow), l, layout)
279      elm_hoversel_item_add(hv, layout, NULL, 0, _layout_select, layout);
280    elm_object_text_set(hv, elm_slideshow_layout_get(slideshow));
281    evas_object_show(hv);
282
283    spin = elm_spinner_add(win);
284    elm_spinner_label_format_set(spin, "%2.0f s");
285    evas_object_smart_callback_add(spin, "changed", _spin, spin);
286    elm_spinner_step_set(spin, 1);
287    elm_spinner_min_max_set(spin, 1, 30);
288    elm_spinner_value_set(spin, 3);
289    elm_box_pack_end(bx, spin);
290    evas_object_show(spin);
291
292    bt = elm_button_add(win);
293    bt_start = bt;
294    elm_object_text_set(bt, "Start");
295    evas_object_smart_callback_add(bt, "clicked", _start, spin);
296    elm_box_pack_end(bx, bt);
297    evas_object_show(bt);
298
299    bt = elm_button_add(win);
300    bt_stop = bt;
301    elm_object_text_set(bt, "Stop");
302    evas_object_smart_callback_add(bt, "clicked", _stop, spin);
303    elm_box_pack_end(bx, bt);
304    elm_object_disabled_set(bt, EINA_TRUE);
305    evas_object_show(bt);
306
307    evas_object_event_callback_add(slideshow, EVAS_CALLBACK_MOUSE_UP,
308                                   _notify_show, notify);
309    evas_object_event_callback_add(slideshow, EVAS_CALLBACK_MOUSE_MOVE,
310                                   _notify_show, notify);
311
312    _notify_show(notify, NULL, NULL, NULL);
313
314    evas_object_resize(win, 600, 400);
315    evas_object_show(win);
316
317    elm_run();
318    return 0;
319 }
320
321 ELM_MAIN()