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