[Elm_genlist] Added check in order to avoid unnecessary error/warning.
[framework/uifw/elementary.git] / src / examples / progressbar_example.c
1 /**
2  * Simple Elementary's <b>progress bar widget</b> example, illustrating its
3  * usage and API.
4  *
5  * See stdout/stderr for output. Compile with:
6  *
7  * @verbatim
8  * gcc -g progressbar_example.c -o progressbar_example `pkg-config --cflags --libs elementary`
9  * @endverbatim
10  */
11
12 #include <Elementary.h>
13
14 #include <time.h>
15
16 typedef struct Progressbar_Example
17 {
18    Evas_Object *pb1;
19    Evas_Object *pb2; /* pulsing */
20    Evas_Object *pb3;
21    Evas_Object *pb4;
22    Evas_Object *pb5;
23    Evas_Object *pb6; /* pulsing */
24    Evas_Object *pb7;
25    Evas_Object *pb8; /* pulsing */
26
27    Eina_Bool    run;
28    Ecore_Timer *timer;
29 } Progressbar_Example;
30
31 static Progressbar_Example example_data;
32
33 static Eina_Bool
34 _progressbar_example_value_set(void *data)
35 {
36    double progress;
37
38    progress = elm_progressbar_value_get(example_data.pb1);
39    if (progress < 1.0) progress += 0.0123;
40    else progress = 0.0;
41
42    /* just the non-pulsing ones need an update */
43    elm_progressbar_value_set(example_data.pb1, progress);
44    elm_progressbar_value_set(example_data.pb3, progress);
45    elm_progressbar_value_set(example_data.pb4, progress);
46    elm_progressbar_value_set(example_data.pb5, progress);
47    elm_progressbar_value_set(example_data.pb7, progress);
48
49    if (progress < 1.0) return ECORE_CALLBACK_RENEW;
50
51    example_data.run = 0;
52    return ECORE_CALLBACK_CANCEL;
53 }
54
55 static void
56 _progressbar_example_start(void        *data,
57                            Evas_Object *obj,
58                            void        *event_info)
59 {
60    elm_progressbar_pulse(example_data.pb2, EINA_TRUE);
61    elm_progressbar_pulse(example_data.pb6, EINA_TRUE);
62    elm_progressbar_pulse(example_data.pb8, EINA_TRUE);
63
64    if (!example_data.run)
65      {
66         example_data.timer = ecore_timer_add(
67             0.1, _progressbar_example_value_set, NULL);
68         example_data.run = EINA_TRUE;
69      }
70 }
71
72 /* end of show */
73 static void
74 _progressbar_example_stop(void        *data,
75                           Evas_Object *obj,
76                           void        *event_info)
77 {
78    elm_progressbar_pulse(example_data.pb2, EINA_FALSE);
79    elm_progressbar_pulse(example_data.pb6, EINA_FALSE);
80    elm_progressbar_pulse(example_data.pb8, EINA_FALSE);
81
82    if (example_data.run)
83      {
84         ecore_timer_del(example_data.timer);
85         example_data.run = EINA_FALSE;
86      }
87 }
88
89 /* Format callback */
90 static char *
91 _progress_format_cb(double val)
92 {
93    static char buf[30];
94    int files = (1-val)*14000;
95    if (snprintf(buf, 30, "%i files left", files) > 0)
96      return strdup(buf);
97    return NULL;
98 }
99
100 static void
101 _progress_format_free(char *str)
102 {
103    free(str);
104 }
105
106 /* Callback for "changed" signal */
107 static void
108 _on_changed(void        *data,
109             Evas_Object *obj,
110             void        *event_info)
111 {
112    static char buf[30];
113    static time_t tstart = 0;
114    static double eta = 0;
115    time_t tdiff;
116    double val;
117    Evas_Object *label =  (Evas_Object *)data;
118
119    val = elm_progressbar_value_get(obj);
120    if (val == 0)
121      {
122         tstart = 0;
123         elm_object_text_set(label, "ETA: N/A");
124         return;
125      }
126
127    /* First invocation */
128    if (tstart == 0)
129      {
130         tstart = time(NULL);
131      }
132
133    /* Calculate ETA and update */
134    tdiff = time(NULL) - tstart;
135    eta = 0.3*eta + 0.7*(tdiff/val)*(1-val);
136    snprintf(buf, 30, "ETA: %.0fs", eta);
137    elm_object_text_set(label, buf);
138 }
139
140 static void
141 _on_done(void        *data,
142          Evas_Object *obj,
143          void        *event_info)
144 {
145    _progressbar_example_stop(NULL, NULL, NULL);
146    elm_exit();
147 }
148
149 EAPI_MAIN int
150 elm_main(int    argc,
151          char **argv)
152 {
153    Evas_Object *win, *bg, *pb, *bx, *hbx, *bt, *bt_bx, *ic1, *ic2, *label;
154    char buf[PATH_MAX];
155
156    elm_app_info_set(elm_main, "elementary", "images/logo_small.png");
157    win = elm_win_add(NULL, "progressbar", ELM_WIN_BASIC);
158    elm_win_title_set(win, "Progress bar example");
159    evas_object_smart_callback_add(win, "delete,request", _on_done, NULL);
160
161    bg = elm_bg_add(win);
162    elm_win_resize_object_add(win, bg);
163    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
164    evas_object_show(bg);
165
166    bx = elm_box_add(win);
167    elm_win_resize_object_add(win, bx);
168    evas_object_size_hint_weight_set(bx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
169    evas_object_show(bx);
170
171    /* pb with no label, default unit label and no icon */
172    pb = elm_progressbar_add(win);
173    evas_object_size_hint_weight_set(pb, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
174    evas_object_size_hint_align_set(pb, EVAS_HINT_FILL, 0.5);
175    elm_box_pack_end(bx, pb);
176    evas_object_show(pb);
177    example_data.pb1 = pb;
178
179    /* pb with label, and set to pulse */
180    pb = elm_progressbar_add(win);
181    evas_object_size_hint_align_set(pb, EVAS_HINT_FILL, 0.5);
182    evas_object_size_hint_weight_set(pb, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
183    elm_object_text_set(pb, "Infinite bounce");
184    elm_progressbar_pulse_set(pb, EINA_TRUE);
185    elm_box_pack_end(bx, pb);
186    evas_object_show(pb);
187    example_data.pb2 = pb;
188
189    ic1 = elm_icon_add(win);
190    snprintf(buf, sizeof(buf), "%s/images/logo_small.png", elm_app_data_dir_get());
191    elm_image_file_set(ic1, buf, NULL);
192    evas_object_size_hint_aspect_set(ic1, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
193
194    /* pb with label, icon, custom unit label function and span size set */
195    pb = elm_progressbar_add(win);
196    elm_object_text_set(pb, "Label");
197    elm_object_part_content_set(pb, "icon", ic1);
198    elm_progressbar_inverted_set(pb, EINA_TRUE);
199    elm_progressbar_unit_format_function_set(pb, _progress_format_cb,
200                                             _progress_format_free);
201    elm_progressbar_span_size_set(pb, 200);
202    evas_object_size_hint_align_set(pb, EVAS_HINT_FILL, 0.5);
203    evas_object_size_hint_weight_set(pb, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
204    elm_box_pack_end(bx, pb);
205    evas_object_show(ic1);
206    evas_object_show(pb);
207    example_data.pb3 = pb;
208
209    /* pb with label and changed trigger  */
210    pb = elm_progressbar_add(win);
211    elm_object_text_set(pb, "Label");
212    evas_object_size_hint_align_set(pb, EVAS_HINT_FILL, 0.5);
213    evas_object_size_hint_weight_set(pb, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
214    elm_box_pack_end(bx, pb);
215    evas_object_show(pb);
216
217    label = elm_label_add(win);
218    elm_object_text_set(label, "ETA: N/A");
219    evas_object_size_hint_align_set(label, 0.5, 0.5);
220    evas_object_size_hint_weight_set(label, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
221    elm_box_pack_end(bx, label);
222    evas_object_show(label);
223
224    evas_object_smart_callback_add(pb, "changed", _on_changed, label);
225    example_data.pb4 = pb;
226
227    hbx = elm_box_add(win);
228    elm_box_horizontal_set(hbx, EINA_TRUE);
229    evas_object_size_hint_weight_set(hbx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
230    evas_object_size_hint_align_set(hbx, EVAS_HINT_FILL, EVAS_HINT_FILL);
231    elm_box_pack_end(bx, hbx);
232    evas_object_show(hbx);
233
234    /* vertical pb */
235    pb = elm_progressbar_add(win);
236    elm_progressbar_horizontal_set(pb, EINA_FALSE);
237    evas_object_size_hint_align_set(pb, EVAS_HINT_FILL, EVAS_HINT_FILL);
238    evas_object_size_hint_weight_set(pb, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
239    elm_box_pack_end(hbx, pb);
240    elm_object_text_set(pb, "percent");
241    evas_object_show(pb);
242    example_data.pb5 = pb;
243
244    /* vertical pb, with pulse and custom (small) span size */
245    pb = elm_progressbar_add(win);
246    elm_progressbar_horizontal_set(pb, EINA_FALSE);
247    evas_object_size_hint_align_set(pb, EVAS_HINT_FILL, 0.5);
248    evas_object_size_hint_weight_set(pb, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
249    elm_progressbar_span_size_set(pb, 80);
250    elm_progressbar_pulse_set(pb, EINA_TRUE);
251    elm_progressbar_unit_format_set(pb, NULL);
252    elm_object_text_set(pb, "Infinite bounce");
253    elm_box_pack_end(hbx, pb);
254    evas_object_show(pb);
255    example_data.pb6 = pb;
256
257    ic2 = elm_icon_add(win);
258    elm_image_file_set(ic2, buf, NULL);
259    evas_object_size_hint_aspect_set(ic2, EVAS_ASPECT_CONTROL_HORIZONTAL, 1, 1);
260
261    /* vertical pb, inverted, with custom unit format and icon*/
262    pb = elm_progressbar_add(win);
263    elm_progressbar_horizontal_set(pb, EINA_FALSE);
264    elm_object_text_set(pb, "Label");
265    elm_object_part_content_set(pb, "icon", ic2);
266    elm_progressbar_inverted_set(pb, EINA_TRUE);
267    elm_progressbar_unit_format_set(pb, "%1.2f%%");
268    elm_progressbar_span_size_set(pb, 200);
269    evas_object_size_hint_align_set(pb, EVAS_HINT_FILL, 0.5);
270    evas_object_size_hint_weight_set(pb, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
271    elm_box_pack_end(hbx, pb);
272    evas_object_show(ic2);
273    evas_object_show(pb);
274    example_data.pb7 = pb;
275
276    /* "wheel" style progress bar */
277    pb = elm_progressbar_add(win);
278    elm_object_style_set(pb, "wheel");
279    elm_object_text_set(pb, "Style: wheel");
280    evas_object_size_hint_align_set(pb, EVAS_HINT_FILL, 0.5);
281    evas_object_size_hint_weight_set(pb, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
282    elm_box_pack_end(bx, pb);
283    evas_object_show(pb);
284    example_data.pb8 = pb;
285
286    bt_bx = elm_box_add(win);
287    elm_box_horizontal_set(bt_bx, EINA_TRUE);
288    evas_object_size_hint_weight_set(bt_bx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
289    elm_box_pack_end(bx, bt_bx);
290    evas_object_show(bt_bx);
291
292    bt = elm_button_add(win);
293    elm_object_text_set(bt, "Start");
294    evas_object_smart_callback_add(bt, "clicked", _progressbar_example_start,
295                                   NULL);
296    elm_box_pack_end(bt_bx, bt);
297    evas_object_show(bt);
298
299    bt = elm_button_add(win);
300    elm_object_text_set(bt, "Stop");
301    evas_object_smart_callback_add(bt, "clicked", _progressbar_example_stop,
302                                   NULL);
303    elm_box_pack_end(bt_bx, bt);
304    evas_object_show(bt);
305
306    evas_object_show(win);
307
308    elm_run();
309    elm_shutdown();
310
311    return 0;
312 }
313 ELM_MAIN()