svn update: 48945 (latest:48959)
[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  *
7  * The progressbar adds a widget for representing current progress
8  * of a job status
9  *
10  * A progressbar can be horizontal or vertical. It can contain an Icon and has a
11  * primary label as well as a units label (that is formatted with floating
12  * point values and thus accepts a printf-style format string, like
13  * “%1.2f units”.
14  *
15  *  Label, Icon and Unit strings/objects are optional.
16  *
17  * A progressbar may be inverted which means values invert, with high vales being
18  * on the left or top and low values on the right or bottom (as opposed to
19  * normally being low on the left or top and high on the bottom and right).
20  *
21  * The span of the progressbar is its length (horizontally or vertically).
22  * This will be scaled by the object or applications scaling factor. At any point
23  * code can query the progressbar for its value with elm_progressbar_value_get().
24  */
25
26 #define MIN_RATIO_LVL 0.0
27 #define MAX_RATIO_LVL 1.0
28
29 typedef struct _Widget_Data Widget_Data;
30
31 struct _Widget_Data
32 {
33    Evas_Object *progressbar;
34    Evas_Object *spacer;
35    Evas_Object *icon;
36    Evas_Coord size;
37    Eina_Bool horizontal : 1;
38    Eina_Bool inverted : 1;
39    Eina_Bool pulse : 1;
40    Eina_Bool pulse_state : 1;
41    const char *units;
42    const char *label;
43    double val;
44 };
45
46 static const char *widtype = NULL;
47 static void _del_hook(Evas_Object *obj);
48 static void _theme_hook(Evas_Object *obj);
49 static void _sizing_eval(Evas_Object *obj);
50 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
51 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
52 static void _units_set(Evas_Object *obj);
53 static void _val_set(Evas_Object *obj);
54
55 static void
56 _del_hook(Evas_Object *obj)
57 {
58    Widget_Data *wd = elm_widget_data_get(obj);
59    if (!wd) return;
60    if (wd->label) eina_stringshare_del(wd->label);
61    if (wd->units) eina_stringshare_del(wd->units);
62    free(wd);
63 }
64
65 static void
66 _theme_hook(Evas_Object *obj)
67 {
68    Widget_Data *wd = elm_widget_data_get(obj);
69    if (!wd) return;
70    edje_object_part_unswallow(NULL, wd->spacer);
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    if (wd->inverted)
76      edje_object_signal_emit(wd->progressbar, "elm,state,inverted,on", "elm");
77    else
78      edje_object_signal_emit(wd->progressbar, "elm,state,inverted,off", "elm");
79    if (wd->icon)
80      edje_object_signal_emit(wd->progressbar, "elm,state,icon,visible", "elm");
81    else
82      edje_object_signal_emit(wd->progressbar, "elm,state,icon,hidden", "elm");
83    if (wd->label)
84      edje_object_signal_emit(wd->progressbar, "elm,state,text,visible", "elm");
85    else
86      edje_object_signal_emit(wd->progressbar, "elm,state,text,hidden", "elm");
87    edje_object_part_text_set(wd->progressbar, "elm.text", wd->label);
88    if (wd->pulse)
89      edje_object_signal_emit(wd->progressbar, "elm,state,pulse", "elm");
90    else
91      edje_object_signal_emit(wd->progressbar, "elm,state,fraction", "elm");
92    if (wd->units && !wd->pulse)
93      edje_object_signal_emit(wd->progressbar, "elm,state,units,visible", "elm");
94    else
95      edje_object_signal_emit(wd->progressbar, "elm,state,units,hidden", "elm");
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    _units_set(obj);
104    edje_object_message_signal_process(wd->progressbar);
105    edje_object_scale_set(wd->progressbar, elm_widget_scale_get(obj) * _elm_config->scale);
106    _val_set(obj);
107    _sizing_eval(obj);
108 }
109
110 static void
111 _sizing_eval(Evas_Object *obj)
112 {
113    Widget_Data *wd = elm_widget_data_get(obj);
114    Evas_Coord minw = -1, minh = -1;
115    if (!wd) return;
116    edje_object_size_min_restricted_calc(wd->progressbar, &minw, &minh, minw, minh);
117    evas_object_size_hint_min_set(obj, minw, minh);
118    evas_object_size_hint_max_set(obj, -1, -1);
119 }
120
121 static void
122 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
123 {
124    Widget_Data *wd = elm_widget_data_get(data);
125    if (!wd) return;
126    if (obj != wd->icon) return;
127    _sizing_eval(data);
128 }
129
130 static void
131 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
132 {
133    Widget_Data *wd = elm_widget_data_get(obj);
134    Evas_Object *sub = event_info;
135    if (!wd) return;
136    if (sub == wd->icon)
137      {
138         edje_object_signal_emit(wd->progressbar, "elm,state,icon,hidden", "elm");
139         evas_object_event_callback_del_full
140           (sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
141         wd->icon = NULL;
142         _sizing_eval(obj);
143      }
144 }
145
146 static void
147 _val_set(Evas_Object *obj)
148 {
149    Widget_Data *wd = elm_widget_data_get(obj);
150    double pos;
151    if (!wd) return;
152    pos = wd->val;
153    if (wd->inverted) pos = MAX_RATIO_LVL - pos;
154    edje_object_part_drag_value_set(wd->progressbar, "elm.cur.progressbar", pos, pos);
155 }
156
157 static void
158 _units_set(Evas_Object *obj)
159 {
160    Widget_Data *wd = elm_widget_data_get(obj);
161    if (!wd) return;
162    if (wd->units)
163      {
164         char buf[1024];
165         snprintf(buf, sizeof(buf), wd->units, 100 * wd->val);
166         edje_object_part_text_set(wd->progressbar, "elm.text.status", buf);
167      }
168    else
169      edje_object_part_text_set(wd->progressbar, "elm.text.status", NULL);
170 }
171
172 /**
173  * Add a new progressbar to the parent
174  *
175  * @param parent The parent object
176  * @return The new object or NULL if it cannot be created
177  *
178  * @ingroup Progressbar
179  */
180 EAPI Evas_Object *
181 elm_progressbar_add(Evas_Object *parent)
182 {
183    Evas_Object *obj;
184    Evas *e;
185    Widget_Data *wd;
186
187    wd = ELM_NEW(Widget_Data);
188    e = evas_object_evas_get(parent);
189    obj = elm_widget_add(e);
190    ELM_SET_WIDTYPE(widtype, "progressbar");
191    elm_widget_type_set(obj, "progressbar");
192    elm_widget_sub_object_add(parent, obj);
193    elm_widget_data_set(obj, wd);
194    elm_widget_del_hook_set(obj, _del_hook);
195    elm_widget_theme_hook_set(obj, _theme_hook);
196
197    wd->horizontal = EINA_TRUE;
198    wd->inverted = EINA_FALSE;
199    wd->pulse = EINA_FALSE;
200    wd->pulse_state = EINA_FALSE;
201    wd->units = eina_stringshare_add("%.0f %%");
202    wd->val = MIN_RATIO_LVL;
203
204    wd->progressbar = edje_object_add(e);
205    _elm_theme_object_set(obj, wd->progressbar, "progressbar", "horizontal", "default");
206    elm_widget_resize_object_set(obj, wd->progressbar);
207
208    wd->spacer = evas_object_rectangle_add(e);
209    evas_object_color_set(wd->spacer, 0, 0, 0, 0);
210    evas_object_pass_events_set(wd->spacer, 1);
211    elm_widget_sub_object_add(obj, wd->spacer);
212    edje_object_part_swallow(wd->progressbar, "elm.swallow.bar", wd->spacer);
213
214    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
215    _units_set(obj);
216    _val_set(obj);
217    _sizing_eval(obj);
218    return obj;
219 }
220
221 /**
222  * Normally the progressbar will display and interpret values from low to high.
223  * This display a progressbar for jobs with unknow state of progression,
224  * (the cursor pulse right to left and left to right, and loop) if pulse is set to 1.
225  *
226  * @param obj The progressbar object
227  * @param pulse The pulse flag. 1 == pulse, 0 == normal
228  *
229  * @ingroup Progressbar
230  */
231 EAPI void
232 elm_progressbar_pulse_set(Evas_Object *obj, Eina_Bool pulse)
233 {
234    ELM_CHECK_WIDTYPE(obj, widtype);
235    Widget_Data *wd = elm_widget_data_get(obj);
236    if (!wd) return;
237    pulse = !!pulse;
238    if (wd->pulse == pulse) return;
239    wd->pulse = pulse;
240    _theme_hook(obj);
241 }
242
243 /**
244  * Normally the progressbar will display and interpret values from low to high.
245  * This display a progressbar for jobs with unknow state of progression,
246  * (the cursor pulse right to left and left to right, and loop) if pulse is set to 1.
247  *
248  * @param obj The progressbar object
249  * @return The pulse flag
250  * (1 == pulse, 0 == normal)
251  *
252  * @ingroup Progressbar
253  */
254 EAPI Eina_Bool
255 elm_progressbar_pulse_get(const Evas_Object *obj)
256 {
257    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
258    Widget_Data *wd = elm_widget_data_get(obj);
259    if (!wd) return EINA_FALSE;
260    return wd->pulse;
261 }
262
263 /**
264  * Stat/Stop de pulse action
265  *
266  * @param obj The progressbar object
267  * @param state The pulse flag. 1 == start pulse, 0 == stop pulse
268  *
269  * @ingroup Progressbar
270  */
271 EAPI void
272 elm_progressbar_pulse(Evas_Object *obj, Eina_Bool state)
273 {
274    ELM_CHECK_WIDTYPE(obj, widtype);
275    Widget_Data *wd = elm_widget_data_get(obj);
276    if (!wd) return;
277    state = !!state;
278    if (!wd->pulse && wd->pulse_state == state) return;
279    wd->pulse_state = state;
280    if (wd->pulse_state)
281      edje_object_signal_emit(wd->progressbar, "elm,state,pulse,start", "elm");
282    else
283      edje_object_signal_emit(wd->progressbar, "elm,state,pulse,stop", "elm");
284 }
285
286 /**
287  * Set the value the progressbar indicates
288  *
289  * @param obj The progressbar object
290  * @param val The fraction value (must be beween 0.0 and 1.0)
291  *
292  * @ingroup Progressbar
293  */
294 EAPI void
295 elm_progressbar_value_set(Evas_Object *obj, double val)
296 {
297    ELM_CHECK_WIDTYPE(obj, widtype);
298    Widget_Data *wd = elm_widget_data_get(obj);
299    if (!wd) return;
300    if (wd->val == val) return;
301    wd->val = val;
302    if (wd->val < MIN_RATIO_LVL) wd->val = MIN_RATIO_LVL;
303    if (wd->val > MAX_RATIO_LVL) wd->val = MAX_RATIO_LVL;
304    _val_set(obj);
305    _units_set(obj);
306 }
307
308
309 /**
310  * Get the value the progressbar has
311  *
312  * @param obj The progressbar object
313  * @return The value of the progressbar
314  *
315  * @ingroup Progressbar
316  */
317 EAPI double
318 elm_progressbar_value_get(const Evas_Object *obj)
319 {
320    ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
321    Widget_Data *wd = elm_widget_data_get(obj);
322    if (!wd) return 0.0;
323    return wd->val;
324 }
325
326 /**
327  * Set the label of the progressbar
328  *
329  * @param obj The progressbar object
330  * @param label The text label string in UTF-8
331  *
332  * @ingroup Progressbar
333  */
334 EAPI void
335 elm_progressbar_label_set(Evas_Object *obj, const char *label)
336 {
337    ELM_CHECK_WIDTYPE(obj, widtype);
338    Widget_Data *wd = elm_widget_data_get(obj);
339    if (!wd) return;
340    eina_stringshare_replace(&wd->label, label);
341    if (label)
342      {
343         edje_object_signal_emit(wd->progressbar, "elm,state,text,visible", "elm");
344         edje_object_message_signal_process(wd->progressbar);
345      }
346    else
347      {
348         edje_object_signal_emit(wd->progressbar, "elm,state,text,hidden", "elm");
349         edje_object_message_signal_process(wd->progressbar);
350      }
351    edje_object_part_text_set(wd->progressbar, "elm.text", label);
352    _sizing_eval(obj);
353 }
354
355 /**
356  * Get the label of the progressbar
357  *
358  * @param obj The progressbar object
359  * @return The text label string in UTF-8
360  *
361  * @ingroup Progressbar
362  */
363 EAPI const char *
364 elm_progressbar_label_get(const Evas_Object *obj)
365 {
366    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
367    Widget_Data *wd = elm_widget_data_get(obj);
368    if (!wd) return NULL;
369    return wd->label;
370 }
371
372 /**
373  * Set the icon object of the progressbar object
374  *
375  * Once the icon object is set, it will become a child of the progressbar object and
376  * be deleted when the progressbar object is deleted. If another icon object is set
377  * then the previous one becomes orophaned and will no longer be deleted along
378  * with the progressbar.
379  *
380  * @param obj The progressbar object
381  * @param icon The icon object
382  *
383  * @ingroup Progressbar
384  */
385 EAPI void
386 elm_progressbar_icon_set(Evas_Object *obj, Evas_Object *icon)
387 {
388    ELM_CHECK_WIDTYPE(obj, widtype);
389    Widget_Data *wd = elm_widget_data_get(obj);
390    if (!wd) return;
391    if ((wd->icon != icon) && (wd->icon))
392      elm_widget_sub_object_del(obj, wd->icon);
393    wd->icon = icon;
394    if (icon)
395      {
396         elm_widget_sub_object_add(obj, icon);
397         evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
398                                        _changed_size_hints, obj);
399         edje_object_part_swallow(wd->progressbar, "elm.swallow.content", icon);
400         edje_object_signal_emit(wd->progressbar, "elm,state,icon,visible", "elm");
401         _sizing_eval(obj);
402      }
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 }