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