1f092107be18cf82bca4a6224b713c305f040e3d
[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 -o slideshow_example slideshow_example.c -g `pkg-config --cflags --libs elementary`
9  * @endverbatim
10  */
11
12 #include <Elementary.h>
13 #define PACKAGE_DATA_DIR "../../data"
14
15 static void
16 _on_done(void        *data,
17          Evas_Object *obj,
18          void        *event_info)
19 {
20    elm_exit();
21 }
22
23 static Evas_Object *slideshow, *bt_start, *bt_stop;
24 static Elm_Slideshow_Item_Class itc;
25
26 static const char *img1 = PACKAGE_DATA_DIR "/images/logo.png";
27 static const char *img2 = PACKAGE_DATA_DIR "/images/plant_01.jpg";
28 static const char *img3 = PACKAGE_DATA_DIR "/images/rock_01.jpg";
29 static const char *img4 = PACKAGE_DATA_DIR "/images/rock_02.jpg";
30 static const char *img5 = PACKAGE_DATA_DIR "/images/sky_01.jpg";
31 static const char *img6 = PACKAGE_DATA_DIR "/images/sky_04.jpg";
32 static const char *img7 = PACKAGE_DATA_DIR "/images/wood_01.jpg";
33 static const char *img8 = PACKAGE_DATA_DIR "/images/mystrale.jpg";
34 static const char *img9 = PACKAGE_DATA_DIR "/images/mystrale_2.jpg";
35
36 static void
37 _notify_show(void        *data,
38              Evas        *e,
39              Evas_Object *obj,
40              void        *event_info)
41 {
42    evas_object_show(data);
43 }
44
45 /* jump to next item, cyclically */
46 static void
47 _next(void        *data,
48       Evas_Object *obj,
49       void        *event_info)
50 {
51    elm_slideshow_next(data);
52 }
53
54 static void
55 _previous(void        *data,
56           Evas_Object *obj,
57           void        *event_info)
58 {
59    elm_slideshow_previous(data);
60 }
61
62 static void
63 _first(void        *data,
64        Evas_Object *obj,
65        void        *event_info)
66 {
67    elm_slideshow_item_show(data);
68 }
69
70 static void
71 _last(void        *data,
72       Evas_Object *obj,
73       void        *event_info)
74 {
75    elm_slideshow_item_show(data);
76 }
77
78 static void
79 _mouse_in(void        *data,
80           Evas        *e,
81           Evas_Object *obj,
82           void        *event_info)
83 {
84    elm_notify_timeout_set(data, 0.0);
85    evas_object_show(data);
86 }
87
88 static void
89 _mouse_out(void        *data,
90            Evas        *e,
91            Evas_Object *obj,
92            void        *event_info)
93 {
94    elm_notify_timeout_set(data, 3.0);
95 }
96
97 /* transition changed */
98 static void
99 _transition_select(void        *data,
100            Evas_Object *obj,
101            void        *event_info)
102 {
103    elm_slideshow_transition_set(slideshow, data);
104    elm_object_text_set(obj, data);
105 }
106
107 static void
108 _layout_select(void        *data,
109                Evas_Object *obj,
110                void        *event_info)
111 {
112    elm_slideshow_layout_set(slideshow, data);
113    elm_object_text_set(obj, data);
114 }
115
116 /* start the show! */
117 static void
118 _start(void        *data,
119        Evas_Object *obj,
120        void        *event_info)
121 {
122    elm_slideshow_timeout_set(slideshow, elm_spinner_value_get(data));
123
124    elm_object_disabled_set(bt_start, EINA_TRUE);
125    elm_object_disabled_set(bt_stop, EINA_FALSE);
126 }
127
128 static void
129 _stop(void        *data,
130       Evas_Object *obj,
131       void        *event_info)
132 {
133    elm_slideshow_timeout_set(slideshow, 0.0);
134    elm_object_disabled_set(bt_start, EINA_FALSE);
135    elm_object_disabled_set(bt_stop, EINA_TRUE);
136 }
137
138 /* slideshow transition time has changed */
139 static void
140 _spin(void        *data,
141       Evas_Object *obj,
142       void        *event_info)
143 {
144    if (elm_slideshow_timeout_get(slideshow) > 0)
145      elm_slideshow_timeout_set(slideshow, elm_spinner_value_get(data));
146 }
147
148 /* get our images to make slideshow items */
149 static Evas_Object *
150 _get(void        *data,
151      Evas_Object *obj)
152 {
153    Evas_Object *photo = elm_photo_add(obj);
154    elm_photo_file_set(photo, data);
155    elm_photo_fill_inside_set(photo, EINA_TRUE);
156    elm_object_style_set(photo, "shadow");
157
158    return photo;
159 }
160
161 /* ordering alphabetically */
162 static int
163 _cmp_func(const void *data1,
164           const void *data2)
165 {
166    const char *img_path1, *img_path2;
167
168    const Elm_Object_Item *slide_it1 = data1;
169    const Elm_Object_Item *slide_it2 = data2;
170
171    img_path1 = elm_object_item_data_get(slide_it1);
172    img_path2 = elm_object_item_data_get(slide_it2);
173
174    return strcasecmp(img_path1, img_path2);
175 }
176
177 EAPI_MAIN int
178 elm_main(int    argc,
179          char **argv)
180 {
181    Evas_Object *win, *bg, *notify, *bx, *bt, *hv, *spin;
182    Elm_Object_Item *slide_first, *slide_last, *slide_it;
183    const char *transition, *layout;
184    const Eina_List *l, *list;
185
186    win = elm_win_add(NULL, "slideshow", ELM_WIN_BASIC);
187    elm_win_title_set(win, "Slideshow example");
188    evas_object_smart_callback_add(win, "delete,request", _on_done, NULL);
189    elm_win_autodel_set(win, EINA_TRUE);
190    elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
191    evas_object_resize(win, 600, 400);
192    evas_object_show(win);
193
194    bg = elm_bg_add(win);
195    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
196    elm_win_resize_object_add(win, bg);
197    evas_object_show(bg);
198
199    slideshow = elm_slideshow_add(win);
200    elm_slideshow_loop_set(slideshow, EINA_TRUE);
201    evas_object_size_hint_weight_set(slideshow,
202                                     EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
203    elm_win_resize_object_add(win, slideshow);
204    evas_object_show(slideshow);
205
206    itc.func.get = _get;
207    itc.func.del = NULL;
208
209    slide_first = elm_slideshow_item_sorted_insert(slideshow, &itc, img1, _cmp_func);
210    elm_slideshow_item_sorted_insert(slideshow, &itc, img2, _cmp_func);
211    elm_slideshow_item_sorted_insert(slideshow, &itc, img3, _cmp_func);
212    elm_slideshow_item_sorted_insert(slideshow, &itc, img4, _cmp_func);
213    elm_slideshow_item_sorted_insert(slideshow, &itc, img5, _cmp_func);
214    elm_slideshow_item_sorted_insert(slideshow, &itc, img6, _cmp_func);
215    elm_slideshow_item_sorted_insert(slideshow, &itc, img7, _cmp_func);
216    elm_slideshow_item_sorted_insert(slideshow, &itc, img8, _cmp_func);
217    slide_last = elm_slideshow_item_add(slideshow, &itc, img9);
218
219    list = elm_slideshow_items_get(slideshow);
220    fprintf(stdout, "List of items in the slideshow:\n");
221    EINA_LIST_FOREACH(list, l, slide_it)
222      fprintf(stdout, "\t%s\n",
223              (const char *)elm_object_item_data_get(slide_it));
224
225    notify = elm_notify_add(win);
226    elm_notify_orient_set(notify, ELM_NOTIFY_ORIENT_BOTTOM);
227    evas_object_size_hint_weight_set(notify, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
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    elm_run();
315    elm_shutdown();
316
317    return 0;
318 }
319 ELM_MAIN()