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