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