[SLP Merge] Thu Jul 7 13:20:56 2011 +0900
[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 static void
192 _elm_progressbar_label_set(Evas_Object *obj, const char *item, const char *label)
193 {
194    ELM_CHECK_WIDTYPE(obj, widtype);
195    Widget_Data *wd = elm_widget_data_get(obj);
196    if (item && strcmp(item, "default")) return;
197    if (!wd) return;
198    eina_stringshare_replace(&wd->label, label);
199    if (label)
200      {
201         edje_object_signal_emit(wd->progressbar, "elm,state,text,visible", "elm");
202         edje_object_message_signal_process(wd->progressbar);
203      }
204    else
205      {
206         edje_object_signal_emit(wd->progressbar, "elm,state,text,hidden", "elm");
207         edje_object_message_signal_process(wd->progressbar);
208      }
209    edje_object_part_text_set(wd->progressbar, "elm.text", label);
210    _sizing_eval(obj);
211 }
212
213 static const char *
214 _elm_progressbar_label_get(const Evas_Object *obj, const char *item)
215 {
216    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
217    Widget_Data *wd = elm_widget_data_get(obj);
218    if (item && strcmp(item, "default")) return NULL;
219    if (!wd) return NULL;
220    return wd->label;
221 }
222
223 /**
224  * Add a new progressbar to the parent
225  *
226  * @param parent The parent object
227  * @return The new object or NULL if it cannot be created
228  *
229  * @ingroup Progressbar
230  */
231 EAPI Evas_Object *
232 elm_progressbar_add(Evas_Object *parent)
233 {
234    Evas_Object *obj;
235    Evas *e;
236    Widget_Data *wd;
237
238    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
239
240    ELM_SET_WIDTYPE(widtype, "progressbar");
241    elm_widget_type_set(obj, "progressbar");
242    elm_widget_sub_object_add(parent, obj);
243    elm_widget_data_set(obj, wd);
244    elm_widget_del_hook_set(obj, _del_hook);
245    elm_widget_theme_hook_set(obj, _theme_hook);
246    elm_widget_can_focus_set(obj, EINA_FALSE);
247    elm_widget_text_set_hook_set(obj, _elm_progressbar_label_set);
248    elm_widget_text_get_hook_set(obj, _elm_progressbar_label_get);
249
250    wd->horizontal = EINA_TRUE;
251    wd->inverted = EINA_FALSE;
252    wd->pulse = EINA_FALSE;
253    wd->pulse_state = EINA_FALSE;
254    wd->units = eina_stringshare_add("%.0f %%");
255    wd->val = MIN_RATIO_LVL;
256
257    wd->progressbar = edje_object_add(e);
258    _elm_theme_object_set(obj, wd->progressbar, "progressbar", "horizontal", "default");
259    elm_widget_resize_object_set(obj, wd->progressbar);
260
261    wd->spacer = evas_object_rectangle_add(e);
262    evas_object_color_set(wd->spacer, 0, 0, 0, 0);
263    evas_object_pass_events_set(wd->spacer, EINA_TRUE);
264    elm_widget_sub_object_add(obj, wd->spacer);
265    edje_object_part_swallow(wd->progressbar, "elm.swallow.bar", wd->spacer);
266
267    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
268    _units_set(obj);
269    _val_set(obj);
270    _mirrored_set(obj, elm_widget_mirrored_get(obj));
271    _sizing_eval(obj);
272    return obj;
273 }
274
275 /**
276  * Normally the progressbar will display and interpret values from low to high.
277  * This display a progressbar for jobs with unknow state of progression,
278  * (the cursor pulse right to left and left to right, and loop) if pulse is set to 1.
279  *
280  * @param obj The progressbar object
281  * @param pulse The pulse flag. 1 == pulse, 0 == normal
282  *
283  * @ingroup Progressbar
284  */
285 EAPI void
286 elm_progressbar_pulse_set(Evas_Object *obj, Eina_Bool pulse)
287 {
288    ELM_CHECK_WIDTYPE(obj, widtype);
289    Widget_Data *wd = elm_widget_data_get(obj);
290    if (!wd) return;
291    pulse = !!pulse;
292    if (wd->pulse == pulse) return;
293    wd->pulse = pulse;
294    _theme_hook(obj);
295 }
296
297 /**
298  * Normally the progressbar will display and interpret values from low to high.
299  * This display a progressbar for jobs with unknow state of progression,
300  * (the cursor pulse right to left and left to right, and loop) if pulse is set to 1.
301  *
302  * @param obj The progressbar object
303  * @return The pulse flag
304  * (1 == pulse, 0 == normal)
305  *
306  * @ingroup Progressbar
307  */
308 EAPI Eina_Bool
309 elm_progressbar_pulse_get(const Evas_Object *obj)
310 {
311    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
312    Widget_Data *wd = elm_widget_data_get(obj);
313    if (!wd) return EINA_FALSE;
314    return wd->pulse;
315 }
316
317 /**
318  * Stat/Stop de pulse action
319  *
320  * @param obj The progressbar object
321  * @param state The pulse flag. 1 == start pulse, 0 == stop pulse
322  *
323  * @ingroup Progressbar
324  */
325 EAPI void
326 elm_progressbar_pulse(Evas_Object *obj, Eina_Bool state)
327 {
328    ELM_CHECK_WIDTYPE(obj, widtype);
329    Widget_Data *wd = elm_widget_data_get(obj);
330    if (!wd) return;
331    state = !!state;
332    if ((!wd->pulse) && (wd->pulse_state == state)) return;
333    wd->pulse_state = state;
334    if (wd->pulse_state)
335      edje_object_signal_emit(wd->progressbar, "elm,state,pulse,start", "elm");
336    else
337      edje_object_signal_emit(wd->progressbar, "elm,state,pulse,stop", "elm");
338 }
339
340 /**
341  * Set the value the progressbar indicates
342  *
343  * @param obj The progressbar object
344  * @param val The fraction value (must be between 0.0 and 1.0)
345  *
346  * @ingroup Progressbar
347  */
348 EAPI void
349 elm_progressbar_value_set(Evas_Object *obj, double val)
350 {
351    ELM_CHECK_WIDTYPE(obj, widtype);
352    Widget_Data *wd = elm_widget_data_get(obj);
353    if (!wd) return;
354    if (wd->val == val) return;
355    wd->val = val;
356    if (wd->val < MIN_RATIO_LVL) wd->val = MIN_RATIO_LVL;
357    if (wd->val > MAX_RATIO_LVL) wd->val = MAX_RATIO_LVL;
358    _val_set(obj);
359    _units_set(obj);
360 }
361
362
363 /**
364  * Get the value the progressbar has
365  *
366  * @param obj The progressbar object
367  * @return The value of the progressbar
368  *
369  * @ingroup Progressbar
370  */
371 EAPI double
372 elm_progressbar_value_get(const Evas_Object *obj)
373 {
374    ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
375    Widget_Data *wd = elm_widget_data_get(obj);
376    if (!wd) return 0.0;
377    return wd->val;
378 }
379
380 /**
381  * Set the label of the progressbar
382  *
383  * @param obj The progressbar object
384  * @param label The text label string in UTF-8
385  *
386  * @ingroup Progressbar
387  * @deprecated use elm_object_text_set() instead.
388  */
389 EAPI void
390 elm_progressbar_label_set(Evas_Object *obj, const char *label)
391 {
392    _elm_progressbar_label_set(obj, NULL, label);
393 }
394
395 /**
396  * Get the label of the progressbar
397  *
398  * @param obj The progressbar object
399  * @return The text label string in UTF-8
400  *
401  * @ingroup Progressbar
402  * @deprecated use elm_object_text_set() instead.
403  */
404 EAPI const char *
405 elm_progressbar_label_get(const Evas_Object *obj)
406 {
407    return _elm_progressbar_label_get(obj, NULL);
408 }
409
410 /**
411  * Set the icon object of the progressbar object
412  *
413  * Once the icon object is set, a previously set one will be deleted.
414  * If you want to keep that old content object, use the
415  * elm_progressbar_icon_unset() function.
416  *
417  * @param obj The progressbar object
418  * @param icon The icon object
419  *
420  * @ingroup Progressbar
421  */
422 EAPI void
423 elm_progressbar_icon_set(Evas_Object *obj, Evas_Object *icon)
424 {
425    ELM_CHECK_WIDTYPE(obj, widtype);
426    Widget_Data *wd = elm_widget_data_get(obj);
427    if (!wd) return;
428    if (wd->icon == icon) return;
429    if (wd->icon) evas_object_del(wd->icon);
430    wd->icon = icon;
431    if (icon)
432      {
433         elm_widget_sub_object_add(obj, icon);
434         evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
435                                        _changed_size_hints, obj);
436         edje_object_part_swallow(wd->progressbar, "elm.swallow.content", icon);
437         edje_object_signal_emit(wd->progressbar, "elm,state,icon,visible", "elm");
438         edje_object_message_signal_process(wd->progressbar);
439      }
440    _sizing_eval(obj);
441 }
442
443 /**
444  * Get the icon object of the progressbar object
445  *
446  * @param obj The progressbar object
447  * @return The icon object
448  *
449  * @ingroup Progressbar
450  */
451 EAPI Evas_Object *
452 elm_progressbar_icon_get(const Evas_Object *obj)
453 {
454    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
455    Widget_Data *wd = elm_widget_data_get(obj);
456    if (!wd) return NULL;
457    return wd->icon;
458 }
459
460 /**
461  * Unset the icon used for the progressbar object
462  *
463  * Unparent and return the icon object which was set for this widget.
464  *
465  * @param obj The progressbar object
466  * @return The icon object that was being used
467  *
468  * @ingroup Progressbar
469  */
470 EAPI Evas_Object *
471 elm_progressbar_icon_unset(Evas_Object *obj)
472 {
473    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
474    Widget_Data *wd = elm_widget_data_get(obj);
475    if (!wd) return NULL;
476    if (!wd->icon) return NULL;
477    Evas_Object *icon = wd->icon;
478    elm_widget_sub_object_del(obj, wd->icon);
479    edje_object_part_unswallow(wd->progressbar, wd->icon);
480    wd->icon = NULL;
481    return icon;
482 }
483
484 /**
485  * Set the length of the progression region of the progressbar
486  *
487  * This sets the minimum width or height (depending on orientation) of the
488  * area of the progressbar that allows the progressbar to be dragged around. This in
489  * turn affects the objects minimum size (along with icon label and unit
490  * text).
491  *
492  * @param obj The progressbar object
493  * @param size The length of the progressbar area
494  *
495  * @ingroup Progressbar
496  */
497 EAPI void
498 elm_progressbar_span_size_set(Evas_Object *obj, Evas_Coord size)
499 {
500    ELM_CHECK_WIDTYPE(obj, widtype);
501    Widget_Data *wd = elm_widget_data_get(obj);
502    if (!wd) return;
503    if (wd->size == size) return;
504    wd->size = size;
505    if (wd->horizontal)
506      evas_object_size_hint_min_set(wd->spacer, (double)wd->size * elm_widget_scale_get(obj) * _elm_config->scale, 1);
507    else
508      evas_object_size_hint_min_set(wd->spacer, 1, (double)wd->size * elm_widget_scale_get(obj) * _elm_config->scale);
509    edje_object_part_swallow(wd->progressbar, "elm.swallow.bar", wd->spacer);
510    _sizing_eval(obj);
511 }
512
513 /**
514  * Get the length of the progression region of the progressbar
515  *
516  * @param obj The progressbar object
517  * @return The length of the progressbar area
518  *
519  * @ingroup Progressbar
520  */
521 EAPI Evas_Coord
522 elm_progressbar_span_size_get(const Evas_Object *obj)
523 {
524    ELM_CHECK_WIDTYPE(obj, widtype) 0;
525    Widget_Data *wd = elm_widget_data_get(obj);
526    if (!wd) return 0;
527    return wd->size;
528 }
529
530 /**
531  * Set the format string of the unit area
532  *
533  * If NULL, this disabls the unit area display. If not it sets the format
534  * string for the unit text. The unit text is provided a floating point
535  * value, so the unit text can display up to 1 floating point falue. Note that
536  * this is optional. Use a format string such as "%1.2f meters" for example.
537  *
538  * @param obj The progressbar object
539  * @param units The format string for the units display
540  *
541  * @ingroup Progressbar
542  */
543 EAPI void
544 elm_progressbar_unit_format_set(Evas_Object *obj, const char *units)
545 {
546    ELM_CHECK_WIDTYPE(obj, widtype);
547    Widget_Data *wd = elm_widget_data_get(obj);
548    if (!wd) return;
549    eina_stringshare_replace(&wd->units, units);
550    if (units)
551      {
552         edje_object_signal_emit(wd->progressbar, "elm,state,units,visible", "elm");
553         edje_object_message_signal_process(wd->progressbar);
554      }
555    else
556      {
557         edje_object_signal_emit(wd->progressbar, "elm,state,units,hidden", "elm");
558         edje_object_message_signal_process(wd->progressbar);
559      }
560    _units_set(obj);
561    _sizing_eval(obj);
562 }
563
564 /**
565  * Get the format string of the unit area
566  *
567  * @param obj The progressbar object
568  * @return The format string for the units display
569  *
570  * @ingroup Progressbar
571  */
572 EAPI const char *
573 elm_progressbar_unit_format_get(const Evas_Object *obj)
574 {
575    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
576    Widget_Data *wd = elm_widget_data_get(obj);
577    if (!wd) return NULL;
578    return wd->units;
579 }
580
581 /**
582  * Set orientation of the progressbar
583  *
584  * @param obj The progressbar object
585  * @param horizontal If set, the progressbar will be horizontal
586  *
587  * @ingroup Progressbar
588  */
589 EAPI void
590 elm_progressbar_horizontal_set(Evas_Object *obj, Eina_Bool horizontal)
591 {
592    ELM_CHECK_WIDTYPE(obj, widtype);
593    Widget_Data *wd = elm_widget_data_get(obj);
594    if (!wd) return;
595    horizontal = !!horizontal;
596    if (wd->horizontal == horizontal) return;
597    wd->horizontal = horizontal;
598    _theme_hook(obj);
599 }
600
601 /**
602  * Gets orientation of the progressbar
603  *
604  * @param obj The progressbar object
605  * @return The orientation
606  * (0 = vertical, 1 = horizontal)
607  *
608  * @ingroup Progressbar
609  */
610 EAPI Eina_Bool
611 elm_progressbar_horizontal_get(const Evas_Object *obj)
612 {
613    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
614    Widget_Data *wd = elm_widget_data_get(obj);
615    if (!wd) return EINA_FALSE;
616    return wd->horizontal;
617 }
618
619 /**
620  * Invert the progressbar display
621  *
622  * Normally the progressbar will display and interpret values from low to high
623  * and when horizontal that is left to right. When vertical that is top
624  * to bottom. This inverts this (so from right to left or bottom to top) if
625  * inverted is set to 1.
626  *
627  * @param obj The progressbar object
628  * @param inverted The inverted flag. 1 == inverted, 0 == normal
629  *
630  * @ingroup Progressbar
631  */
632 EAPI void
633 elm_progressbar_inverted_set(Evas_Object *obj, Eina_Bool inverted)
634 {
635    ELM_CHECK_WIDTYPE(obj, widtype);
636    Widget_Data *wd = elm_widget_data_get(obj);
637    if (!wd) return;
638    inverted = !!inverted;
639    if (wd->inverted == inverted) return;
640    wd->inverted = inverted;
641    if (wd->inverted)
642      edje_object_signal_emit(wd->progressbar, "elm,state,inverted,on", "elm");
643    else
644      edje_object_signal_emit(wd->progressbar, "elm,state,inverted,off", "elm");
645    edje_object_message_signal_process(wd->progressbar);
646    _val_set(obj);
647    _units_set(obj);
648 }
649
650 /**
651  * Gets if the progressbar will displayed inverted
652  *
653  * @param obj The progressbar object
654  * @return The inverted flag
655  * (1 == inverted, 0 == normal)
656  *
657  * @ingroup Progressbar
658  */
659 EAPI Eina_Bool
660 elm_progressbar_inverted_get(const Evas_Object *obj)
661 {
662    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
663    Widget_Data *wd = elm_widget_data_get(obj);
664    if (!wd) return EINA_FALSE;
665    return wd->inverted;
666 }