remove excution permission for source files
[framework/uifw/elementary.git] / src / lib / elm_progressbar.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup Progressbar Progressbar
6  * @ingroup Elementary
7  *
8  * The progressbar adds a widget for representing current progress
9  * of a job status
10  *
11  * A progressbar can be horizontal or vertical. It can contain an Icon and has a
12  * primary label as well as a units label (that is formatted with floating
13  * point values and thus accepts a printf-style format string, like
14  * “%1.2f units”.
15  *
16  *  Label, Icon and Unit strings/objects are optional.
17  *
18  * A progressbar may be inverted which means values invert, with high vales being
19  * on the left or top and low values on the right or bottom (as opposed to
20  * normally being low on the left or top and high on the bottom and right).
21  *
22  * The span of the progressbar is its length (horizontally or vertically).
23  * This will be scaled by the object or applications scaling factor. At any point
24  * code can query the progressbar for its value with elm_progressbar_value_get().
25  */
26
27 #define MIN_RATIO_LVL 0.0
28 #define MAX_RATIO_LVL 1.0
29
30 typedef struct _Widget_Data Widget_Data;
31
32 struct _Widget_Data
33 {
34    Evas_Object *progressbar;
35    Evas_Object *spacer;
36    Evas_Object *icon;
37    Evas_Coord size;
38    Eina_Bool horizontal : 1;
39    Eina_Bool inverted : 1;
40    Eina_Bool pulse : 1;
41    Eina_Bool pulse_state : 1;
42    const char *units;
43    const char *label;
44    double val;
45 };
46
47 static const char *widtype = NULL;
48 static void _del_hook(Evas_Object *obj);
49 static void _theme_hook(Evas_Object *obj);
50 static void _sizing_eval(Evas_Object *obj);
51 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
52 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
53 static void _units_set(Evas_Object *obj);
54 static void _val_set(Evas_Object *obj);
55
56 static void
57 _del_hook(Evas_Object *obj)
58 {
59    Widget_Data *wd = elm_widget_data_get(obj);
60    if (!wd) return;
61    if (wd->label) eina_stringshare_del(wd->label);
62    if (wd->units) eina_stringshare_del(wd->units);
63    free(wd);
64 }
65
66 static void
67 _theme_hook(Evas_Object *obj)
68 {
69    Widget_Data *wd = elm_widget_data_get(obj);
70    if (!wd) return;
71    edje_object_part_unswallow(NULL, wd->spacer);
72    if (wd->horizontal)
73      _elm_theme_object_set(obj, wd->progressbar, "progressbar", "horizontal", elm_widget_style_get(obj));
74    else
75      _elm_theme_object_set(obj, wd->progressbar, "progressbar", "vertical", elm_widget_style_get(obj));
76    if (wd->inverted)
77      edje_object_signal_emit(wd->progressbar, "elm,state,inverted,on", "elm");
78    else
79      edje_object_signal_emit(wd->progressbar, "elm,state,inverted,off", "elm");
80    if (wd->icon)
81      edje_object_signal_emit(wd->progressbar, "elm,state,icon,visible", "elm");
82    else
83      edje_object_signal_emit(wd->progressbar, "elm,state,icon,hidden", "elm");
84    if (wd->label)
85      edje_object_signal_emit(wd->progressbar, "elm,state,text,visible", "elm");
86    else
87      edje_object_signal_emit(wd->progressbar, "elm,state,text,hidden", "elm");
88    edje_object_part_text_set(wd->progressbar, "elm.text", wd->label);
89    if (wd->pulse)
90      edje_object_signal_emit(wd->progressbar, "elm,state,pulse", "elm");
91    else
92      edje_object_signal_emit(wd->progressbar, "elm,state,fraction", "elm");
93    if (wd->units && !wd->pulse)
94      edje_object_signal_emit(wd->progressbar, "elm,state,units,visible", "elm");
95    else
96      edje_object_signal_emit(wd->progressbar, "elm,state,units,hidden", "elm");
97    if (wd->horizontal)
98      evas_object_size_hint_min_set(wd->spacer, (double)wd->size * elm_widget_scale_get(obj) * _elm_config->scale, 1);
99    else
100      evas_object_size_hint_min_set(wd->spacer, 1, (double)wd->size * elm_widget_scale_get(obj) * _elm_config->scale);
101
102    edje_object_part_swallow(wd->progressbar, "elm.swallow.bar", wd->spacer);
103
104    _units_set(obj);
105    edje_object_message_signal_process(wd->progressbar);
106    edje_object_scale_set(wd->progressbar, elm_widget_scale_get(obj) * _elm_config->scale);
107    _val_set(obj);
108    _sizing_eval(obj);
109 }
110
111 static void
112 _sizing_eval(Evas_Object *obj)
113 {
114    Widget_Data *wd = elm_widget_data_get(obj);
115    Evas_Coord minw = -1, minh = -1;
116    if (!wd) return;
117    edje_object_size_min_restricted_calc(wd->progressbar, &minw, &minh, minw, minh);
118    evas_object_size_hint_min_set(obj, minw, minh);
119    evas_object_size_hint_max_set(obj, -1, -1);
120 }
121
122 static void
123 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
124 {
125    Widget_Data *wd = elm_widget_data_get(data);
126    if (!wd) return;
127    if (obj != wd->icon) return;
128    _sizing_eval(data);
129 }
130
131 static void
132 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
133 {
134    Widget_Data *wd = elm_widget_data_get(obj);
135    Evas_Object *sub = event_info;
136    if (!wd) return;
137    if (sub == wd->icon)
138      {
139         edje_object_signal_emit(wd->progressbar, "elm,state,icon,hidden", "elm");
140         evas_object_event_callback_del_full
141           (sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
142         wd->icon = NULL;
143         _sizing_eval(obj);
144      }
145 }
146
147 static void
148 _val_set(Evas_Object *obj)
149 {
150    Widget_Data *wd = elm_widget_data_get(obj);
151    double pos;
152    if (!wd) return;
153    pos = wd->val;
154    if (wd->inverted) pos = MAX_RATIO_LVL - pos;
155    edje_object_part_drag_value_set(wd->progressbar, "elm.cur.progressbar", pos, pos);
156 }
157
158 static void
159 _units_set(Evas_Object *obj)
160 {
161    Widget_Data *wd = elm_widget_data_get(obj);
162    if (!wd) return;
163    if (wd->units)
164      {
165         char buf[1024];
166         snprintf(buf, sizeof(buf), wd->units, 100 * wd->val);
167         edje_object_part_text_set(wd->progressbar, "elm.text.status", buf);
168      }
169    else
170      edje_object_part_text_set(wd->progressbar, "elm.text.status", NULL);
171 }
172
173 /**
174  * Add a new progressbar to the parent
175  *
176  * @param parent The parent object
177  * @return The new object or NULL if it cannot be created
178  *
179  * @ingroup Progressbar
180  */
181 EAPI Evas_Object *
182 elm_progressbar_add(Evas_Object *parent)
183 {
184    Evas_Object *obj;
185    Evas *e;
186    Widget_Data *wd;
187
188    wd = ELM_NEW(Widget_Data);
189    e = evas_object_evas_get(parent);
190    obj = elm_widget_add(e);
191    ELM_SET_WIDTYPE(widtype, "progressbar");
192    elm_widget_type_set(obj, "progressbar");
193    elm_widget_sub_object_add(parent, obj);
194    elm_widget_data_set(obj, wd);
195    elm_widget_del_hook_set(obj, _del_hook);
196    elm_widget_theme_hook_set(obj, _theme_hook);
197
198    wd->horizontal = EINA_TRUE;
199    wd->inverted = EINA_FALSE;
200    wd->pulse = EINA_FALSE;
201    wd->pulse_state = EINA_FALSE;
202    wd->units = eina_stringshare_add("%.0f %%");
203    wd->val = MIN_RATIO_LVL;
204
205    wd->progressbar = edje_object_add(e);
206    _elm_theme_object_set(obj, wd->progressbar, "progressbar", "horizontal", "default");
207    elm_widget_resize_object_set(obj, wd->progressbar);
208
209    wd->spacer = evas_object_rectangle_add(e);
210    evas_object_color_set(wd->spacer, 0, 0, 0, 0);
211    evas_object_pass_events_set(wd->spacer, 1);
212    elm_widget_sub_object_add(obj, wd->spacer);
213    edje_object_part_swallow(wd->progressbar, "elm.swallow.bar", wd->spacer);
214
215    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
216    _units_set(obj);
217    _val_set(obj);
218    _sizing_eval(obj);
219    return obj;
220 }
221
222 /**
223  * Normally the progressbar will display and interpret values from low to high.
224  * This display a progressbar for jobs with unknow state of progression,
225  * (the cursor pulse right to left and left to right, and loop) if pulse is set to 1.
226  *
227  * @param obj The progressbar object
228  * @param pulse The pulse flag. 1 == pulse, 0 == normal
229  *
230  * @ingroup Progressbar
231  */
232 EAPI void
233 elm_progressbar_pulse_set(Evas_Object *obj, Eina_Bool pulse)
234 {
235    ELM_CHECK_WIDTYPE(obj, widtype);
236    Widget_Data *wd = elm_widget_data_get(obj);
237    if (!wd) return;
238    pulse = !!pulse;
239    if (wd->pulse == pulse) return;
240    wd->pulse = pulse;
241    _theme_hook(obj);
242 }
243
244 /**
245  * Normally the progressbar will display and interpret values from low to high.
246  * This display a progressbar for jobs with unknow state of progression,
247  * (the cursor pulse right to left and left to right, and loop) if pulse is set to 1.
248  *
249  * @param obj The progressbar object
250  * @return The pulse flag
251  * (1 == pulse, 0 == normal)
252  *
253  * @ingroup Progressbar
254  */
255 EAPI Eina_Bool
256 elm_progressbar_pulse_get(const Evas_Object *obj)
257 {
258    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
259    Widget_Data *wd = elm_widget_data_get(obj);
260    if (!wd) return EINA_FALSE;
261    return wd->pulse;
262 }
263
264 /**
265  * Stat/Stop de pulse action
266  *
267  * @param obj The progressbar object
268  * @param state The pulse flag. 1 == start pulse, 0 == stop pulse
269  *
270  * @ingroup Progressbar
271  */
272 EAPI void
273 elm_progressbar_pulse(Evas_Object *obj, Eina_Bool state)
274 {
275    ELM_CHECK_WIDTYPE(obj, widtype);
276    Widget_Data *wd = elm_widget_data_get(obj);
277    if (!wd) return;
278    state = !!state;
279    if (!wd->pulse && wd->pulse_state == state) return;
280    wd->pulse_state = state;
281    if (wd->pulse_state)
282      edje_object_signal_emit(wd->progressbar, "elm,state,pulse,start", "elm");
283    else
284      edje_object_signal_emit(wd->progressbar, "elm,state,pulse,stop", "elm");
285 }
286
287 /**
288  * Set the value the progressbar indicates
289  *
290  * @param obj The progressbar object
291  * @param val The fraction value (must be beween 0.0 and 1.0)
292  *
293  * @ingroup Progressbar
294  */
295 EAPI void
296 elm_progressbar_value_set(Evas_Object *obj, double val)
297 {
298    ELM_CHECK_WIDTYPE(obj, widtype);
299    Widget_Data *wd = elm_widget_data_get(obj);
300    if (!wd) return;
301    if (wd->val == val) return;
302    wd->val = val;
303    if (wd->val < MIN_RATIO_LVL) wd->val = MIN_RATIO_LVL;
304    if (wd->val > MAX_RATIO_LVL) wd->val = MAX_RATIO_LVL;
305    _val_set(obj);
306    _units_set(obj);
307 }
308
309
310 /**
311  * Get the value the progressbar has
312  *
313  * @param obj The progressbar object
314  * @return The value of the progressbar
315  *
316  * @ingroup Progressbar
317  */
318 EAPI double
319 elm_progressbar_value_get(const Evas_Object *obj)
320 {
321    ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
322    Widget_Data *wd = elm_widget_data_get(obj);
323    if (!wd) return 0.0;
324    return wd->val;
325 }
326
327 /**
328  * Set the label of the progressbar
329  *
330  * @param obj The progressbar object
331  * @param label The text label string in UTF-8
332  *
333  * @ingroup Progressbar
334  */
335 EAPI void
336 elm_progressbar_label_set(Evas_Object *obj, const char *label)
337 {
338    ELM_CHECK_WIDTYPE(obj, widtype);
339    Widget_Data *wd = elm_widget_data_get(obj);
340    if (!wd) return;
341    eina_stringshare_replace(&wd->label, label);
342    if (label)
343      {
344         edje_object_signal_emit(wd->progressbar, "elm,state,text,visible", "elm");
345         edje_object_message_signal_process(wd->progressbar);
346      }
347    else
348      {
349         edje_object_signal_emit(wd->progressbar, "elm,state,text,hidden", "elm");
350         edje_object_message_signal_process(wd->progressbar);
351      }
352    edje_object_part_text_set(wd->progressbar, "elm.text", label);
353    _sizing_eval(obj);
354 }
355
356 /**
357  * Get the label of the progressbar
358  *
359  * @param obj The progressbar object
360  * @return The text label string in UTF-8
361  *
362  * @ingroup Progressbar
363  */
364 EAPI const char *
365 elm_progressbar_label_get(const Evas_Object *obj)
366 {
367    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
368    Widget_Data *wd = elm_widget_data_get(obj);
369    if (!wd) return NULL;
370    return wd->label;
371 }
372
373 /**
374  * Set the icon object of the progressbar object
375  *
376  * Once the icon object is set, it will become a child of the progressbar object and
377  * be deleted when the progressbar object is deleted. If another icon object is set
378  * then the previous one becomes orophaned and will no longer be deleted along
379  * with the progressbar.
380  *
381  * @param obj The progressbar object
382  * @param icon The icon object
383  *
384  * @ingroup Progressbar
385  */
386 EAPI void
387 elm_progressbar_icon_set(Evas_Object *obj, Evas_Object *icon)
388 {
389    ELM_CHECK_WIDTYPE(obj, widtype);
390    Widget_Data *wd = elm_widget_data_get(obj);
391    if (!wd) return;
392    if ((wd->icon != icon) && (wd->icon))
393      elm_widget_sub_object_del(obj, wd->icon);
394    wd->icon = icon;
395    if (icon)
396      {
397         elm_widget_sub_object_add(obj, icon);
398         evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
399                                        _changed_size_hints, obj);
400         edje_object_part_swallow(wd->progressbar, "elm.swallow.content", icon);
401         edje_object_signal_emit(wd->progressbar, "elm,state,icon,visible", "elm");
402         _sizing_eval(obj);
403      }
404 }
405
406 /**
407  * Get the icon object of the progressbar object
408  *
409  * @param obj The progressbar object
410  * @return The icon object
411  *
412  * @ingroup Progressbar
413  */
414 EAPI Evas_Object *
415 elm_progressbar_icon_get(const Evas_Object *obj)
416 {
417    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
418    Widget_Data *wd = elm_widget_data_get(obj);
419    if (!wd) return NULL;
420    return wd->icon;
421 }
422
423 /**
424  * Set the length of the progression region of the progressbar
425  *
426  * This sets the minimum width or height (depending on orientation) of the
427  * area of the progressbar that allows the progressbar to be dragged around. This in
428  * turn affects the objects minimum size (along with icon label and unit
429  * text).
430  *
431  * @param obj The progressbar object
432  * @param size The length of the progressbar area
433  *
434  * @ingroup Progressbar
435  */
436 EAPI void
437 elm_progressbar_span_size_set(Evas_Object *obj, Evas_Coord size)
438 {
439    ELM_CHECK_WIDTYPE(obj, widtype);
440    Widget_Data *wd = elm_widget_data_get(obj);
441    if (!wd) return;
442    if (wd->size == size) return;
443    wd->size = size;
444    if (wd->horizontal)
445      evas_object_size_hint_min_set(wd->spacer, (double)wd->size * elm_widget_scale_get(obj) * _elm_config->scale, 1);
446    else
447      evas_object_size_hint_min_set(wd->spacer, 1, (double)wd->size * elm_widget_scale_get(obj) * _elm_config->scale);
448    edje_object_part_swallow(wd->progressbar, "elm.swallow.bar", wd->spacer);
449    _sizing_eval(obj);
450 }
451
452 /**
453  * Get the length of the progression region of the progressbar
454  *
455  * @param obj The progressbar object
456  * @return The length of the progressbar area
457  *
458  * @ingroup Progressbar
459  */
460 EAPI Evas_Coord
461 elm_progressbar_span_size_get(const Evas_Object *obj)
462 {
463    ELM_CHECK_WIDTYPE(obj, widtype) 0;
464    Widget_Data *wd = elm_widget_data_get(obj);
465    if (!wd) return 0;
466    return wd->size;
467 }
468
469 /**
470  * Set the format string of the unit area
471  *
472  * If NULL, this disabls the unit area display. If not it sets the format
473  * string for the unit text. The unit text is provided a floating point
474  * value, so the unit text can display up to 1 floating point falue. Note that
475  * this is optional. Use a format string such as "%1.2f meters" for example.
476  *
477  * @param obj The progressbar object
478  * @param units The format string for the units display
479  *
480  * @ingroup Progressbar
481  */
482 EAPI void
483 elm_progressbar_unit_format_set(Evas_Object *obj, const char *units)
484 {
485    ELM_CHECK_WIDTYPE(obj, widtype);
486    Widget_Data *wd = elm_widget_data_get(obj);
487    if (!wd) return;
488    eina_stringshare_replace(&wd->units, units);
489    if (units)
490      {
491         edje_object_signal_emit(wd->progressbar, "elm,state,units,visible", "elm");
492         edje_object_message_signal_process(wd->progressbar);
493      }
494    else
495      {
496         edje_object_signal_emit(wd->progressbar, "elm,state,units,hidden", "elm");
497         edje_object_message_signal_process(wd->progressbar);
498      }
499    _units_set(obj);
500    _sizing_eval(obj);
501 }
502
503 /**
504  * Get the format string of the unit area
505  *
506  * @param obj The progressbar object
507  * @return The format string for the units display
508  *
509  * @ingroup Progressbar
510  */
511 EAPI const char *
512 elm_progressbar_unit_format_get(const Evas_Object *obj)
513 {
514    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
515    Widget_Data *wd = elm_widget_data_get(obj);
516    if (!wd) return NULL;
517    return wd->units;
518 }
519
520 /**
521  * Set orientation of the progressbar
522  *
523  * @param obj The progressbar object
524  * @param horizontal If set, the progressbar will be horizontal
525  *
526  * @ingroup Progressbar
527  */
528 EAPI void
529 elm_progressbar_horizontal_set(Evas_Object *obj, Eina_Bool horizontal)
530 {
531    ELM_CHECK_WIDTYPE(obj, widtype);
532    Widget_Data *wd = elm_widget_data_get(obj);
533    if (!wd) return;
534    horizontal = !!horizontal;
535    if (wd->horizontal == horizontal) return;
536    wd->horizontal = horizontal;
537    _theme_hook(obj);
538 }
539
540 /**
541  * Gets orientation of the progressbar
542  *
543  * @param obj The progressbar object
544  * @return The orientation
545  * (0 = vertical, 1 = horizontal)
546  *
547  * @ingroup Progressbar
548  */
549 EAPI Eina_Bool
550 elm_progressbar_horizontal_get(const Evas_Object *obj)
551 {
552    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
553    Widget_Data *wd = elm_widget_data_get(obj);
554    if (!wd) return EINA_FALSE;
555    return wd->horizontal;
556 }
557
558 /**
559  * Invert the progressbar display
560  *
561  * Normally the progressbar will display and interpret values from low to high
562  * and when horizontal that is left to right. When vertical that is top
563  * to bottom. This inverts this (so from right to left or bottom to top) if
564  * inverted is set to 1.
565  *
566  * @param obj The progressbar object
567  * @param inverted The inverted flag. 1 == inverted, 0 == normal
568  *
569  * @ingroup Progressbar
570  */
571 EAPI void
572 elm_progressbar_inverted_set(Evas_Object *obj, Eina_Bool inverted)
573 {
574    ELM_CHECK_WIDTYPE(obj, widtype);
575    Widget_Data *wd = elm_widget_data_get(obj);
576    if (!wd) return;
577    inverted = !!inverted;
578    if (wd->inverted == inverted) return;
579    wd->inverted = inverted;
580    if (wd->inverted)
581      edje_object_signal_emit(wd->progressbar, "elm,state,inverted,on", "elm");
582    else
583      edje_object_signal_emit(wd->progressbar, "elm,state,inverted,off", "elm");
584    edje_object_message_signal_process(wd->progressbar);
585    _val_set(obj);
586    _units_set(obj);
587 }
588
589 /**
590  * Gets if the progressbar will displayed inverted
591  *
592  * @param obj The progressbar object
593  * @return The inverted flag
594  * (1 == inverted, 0 == normal)
595  *
596  * @ingroup Progressbar
597  */
598 EAPI Eina_Bool
599 elm_progressbar_inverted_get(const Evas_Object *obj)
600 {
601    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
602    Widget_Data *wd = elm_widget_data_get(obj);
603    if (!wd) return EINA_FALSE;
604    return wd->inverted;
605 }