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