[elm_slider] modified for fixing bugs
[framework/uifw/elementary.git] / src / lib / elm_slider.c
1 /*
2  * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
3  */
4 #include <Elementary.h>
5 #include "elm_priv.h"
6
7 /**
8  * @defgroup Slider Slider
9  * @ingroup Elementary
10  *
11  * The slider adds a dragable “slider” widget for selecting the value of
12  * something within a range.
13  *
14  * Signals that you can add callbacks for are:
15  *
16  * changed - Whenever the slider value is changed by the user.
17  *
18  * delay,changed - A short time after the value is changed by the user.
19  * This will be called only when the user stops dragging for a very short
20  * period or when they release their finger/mouse, so it avoids possibly
21  * expensive reactions to the value change.
22  *
23  * slider,drag,start - dragging the slider indicator around has started
24  *
25  * slider,drag,stop - dragging the slider indicator around has stopped
26  *
27  * A slider can be horizontal or vertical. It can contain an Icon and has a
28  * primary label as well as a units label (that is formatted with floating
29  * point values and thus accepts a printf-style format string, like
30  * “%1.2f units”. There is also an indicator string that may be somewhere
31  * else (like on the slider itself) that also accepts a format string like
32  * units. Label, Icon Unit and Indicator strings/objects are optional.
33  *
34  * A slider may be inverted which means values invert, with high vales being
35  * on the left or top and low values on the right or bottom (as opposed to
36  * normally being low on the left or top and high on the bottom and right).
37  *
38  * The slider should have its minimum and maximum values set by the
39  * application with  elm_slider_min_max_set() and value should also be set by
40  * the application before use with  elm_slider_value_set(). The span of the
41  * slider is its length (horizontally or vertically). This will be scaled by
42  * the object or applications scaling factor. At any point code can query the
43  * slider for its value with elm_slider_value_get().
44  */
45
46 typedef struct _Widget_Data Widget_Data;
47
48 struct _Widget_Data
49 {
50    Evas_Object *slider;
51    Evas_Object *icon;   
52    Evas_Object *spacer;
53    const char *label;
54    const char *e_label;
55    const char *units;
56    const char *indicator;
57    const char *(*indicator_format_func)(double val);
58    Eina_Bool horizontal : 1;
59    Eina_Bool inverted : 1;
60    double val, val_min, val_max;
61    Ecore_Timer *delay;
62    Evas_Coord size;
63    Ecore_Timer *mv_timer;
64    Evas_Object *e_icon;
65    double src_val;
66    double des_val;
67    double mv_step;
68 };
69
70 #define SLIDER_THUMB_MOVE_STEP 100
71
72 static void _del_hook(Evas_Object *obj);
73 static void _theme_hook(Evas_Object *obj);
74 static void _sizing_eval(Evas_Object *obj);
75 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
76 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
77 static void _units_set(Evas_Object *obj);
78 static void _indicator_set(Evas_Object *obj);
79 static void _spacer_cb(void *data, Evas * e, Evas_Object * obj, void *event_info);
80
81
82 static Eina_Bool _mv_timer_cb(void *data);
83
84 static const char SIG_CHANGED[] = "changed";
85 static const char SIG_DELAY_CHANGED[] = "delay,changed";
86 static const char SIG_DRAG_START[] = "slider,drag,start";
87 static const char SIG_DRAG_STOP[] = "slider,drag,stop";
88 static const Evas_Smart_Cb_Description _signals[] = {
89   {SIG_CHANGED, ""},
90   {SIG_DELAY_CHANGED, ""},
91   {SIG_DRAG_START, ""},
92   {SIG_DRAG_STOP, ""},
93   {NULL, NULL}
94 };
95
96 static void
97 _del_hook(Evas_Object *obj)
98 {
99    Widget_Data *wd = elm_widget_data_get(obj);
100    if (!wd) return;
101    if (wd->label) eina_stringshare_del(wd->label);
102    if (wd->e_label) eina_stringshare_del(wd->label);
103    if (wd->indicator) eina_stringshare_del(wd->units);
104    if (wd->delay) ecore_timer_del(wd->delay);
105    free(wd);
106 }
107
108 static void
109 _theme_hook(Evas_Object *obj)
110 {
111    Widget_Data *wd = elm_widget_data_get(obj);
112    if (!wd) return;
113    if (wd->horizontal)
114      _elm_theme_object_set(obj, wd->slider, "slider", "horizontal", elm_widget_style_get(obj));
115    else
116      _elm_theme_object_set(obj, wd->slider, "slider", "vertical", elm_widget_style_get(obj));
117    if (wd->inverted)
118      edje_object_signal_emit(wd->slider, "elm,state,inverted,on", "elm");
119    else
120      edje_object_signal_emit(wd->slider, "elm,state,inverted,off", "elm");
121    if (wd->icon)
122      edje_object_signal_emit(wd->slider, "elm,state,icon,visible", "elm");
123    else
124      edje_object_signal_emit(wd->slider, "elm,state,icon,hidden", "elm");
125    if (wd->e_icon)
126      edje_object_signal_emit(wd->slider, "elm,state,eicon,visible", "elm");
127    else
128      edje_object_signal_emit(wd->slider, "elm,state,eicon,hidden", "elm");
129    if (wd->label)
130      edje_object_signal_emit(wd->slider, "elm,state,text,visible", "elm");
131    else
132      edje_object_signal_emit(wd->slider, "elm,state,text,hidden", "elm");
133    edje_object_part_text_set(wd->slider, "elm.text", wd->label);
134    if (wd->e_label)
135      edje_object_signal_emit(wd->slider, "elm,state,units,visible", "elm");
136    else
137      edje_object_signal_emit(wd->slider, "elm,state,units,hidden", "elm");
138    edje_object_part_text_set(wd->slider, "elm.units", wd->e_label);
139    if (wd->units)
140      edje_object_signal_emit(wd->slider, "elm,state,units,visible", "elm");
141    else
142      edje_object_signal_emit(wd->slider, "elm,state,units,hidden", "elm");
143    if (wd->horizontal)
144      evas_object_size_hint_min_set(wd->spacer, (double)wd->size * elm_widget_scale_get(obj) * _elm_config->scale, 1);
145    else
146      evas_object_size_hint_min_set(wd->spacer, 1, (double)wd->size * elm_widget_scale_get(obj) * _elm_config->scale);
147    edje_object_part_swallow(wd->slider, "elm.swallow.bar", wd->spacer);
148    _units_set(obj);
149    edje_object_message_signal_process(wd->slider);
150    edje_object_scale_set(wd->slider, elm_widget_scale_get(obj) * _elm_config->scale);
151    _sizing_eval(obj);
152 }
153
154 static void
155 _sizing_eval(Evas_Object *obj)
156 {   
157    Widget_Data *wd = elm_widget_data_get(obj);
158    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
159    if (!wd) return;
160    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
161    edje_object_size_min_restricted_calc(wd->slider, &minw, &minh, minw, minh);
162    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
163    evas_object_size_hint_min_set(obj, minw, minh);
164    evas_object_size_hint_max_set(obj, maxw, maxh);
165 }
166
167 static void
168 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
169 {
170    Widget_Data *wd = elm_widget_data_get(data);
171    if (!wd) return;
172
173    if (obj != wd->icon && obj != wd->e_icon) return;
174
175    _sizing_eval(data);
176 }
177
178 static void
179 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
180 {
181    Widget_Data *wd = elm_widget_data_get(obj);
182    Evas_Object *sub = event_info;
183    if (!wd) return;
184    if (sub == wd->icon)
185      {
186         edje_object_signal_emit(wd->slider, "elm,state,icon,hidden", "elm");
187         evas_object_event_callback_del_full
188            (sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
189         wd->icon = NULL;
190         _sizing_eval(obj);
191      }
192    if (sub == wd->e_icon)
193      {
194         edje_object_signal_emit(wd->slider, "elm,state,eicon,hidden", "elm");
195         evas_object_event_callback_del_full
196            (sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
197         wd->e_icon = NULL;
198         _sizing_eval(obj);
199      }
200 }
201
202 static int
203 _delay_change(void *data)
204 {
205    Widget_Data *wd = elm_widget_data_get(data);
206    if (!wd) return 0;
207    wd->delay = NULL;
208    evas_object_smart_callback_call(data, SIG_DELAY_CHANGED, NULL);
209    return 0;
210 }
211
212 static void
213 _val_fetch(Evas_Object *obj)
214 {
215    Widget_Data *wd = elm_widget_data_get(obj);
216    double posx = 0.0, posy = 0.0, pos = 0.0, val;
217    if (!wd) return;
218    edje_object_part_drag_value_get(wd->slider, "elm.dragable.slider",
219                                    &posx, &posy);
220    if (wd->horizontal) pos = posx;
221    else pos = posy;
222    if (wd->inverted) pos = 1.0 - pos;
223    val = (pos * (wd->val_max - wd->val_min)) + wd->val_min;
224    if (val != wd->val)
225      {
226         wd->val = val;
227         evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
228         if (wd->delay) ecore_timer_del(wd->delay);
229         wd->delay = ecore_timer_add(0.2, _delay_change, obj);
230      }
231 }
232
233 static void
234 _val_set(Evas_Object *obj)
235 {
236    Widget_Data *wd = elm_widget_data_get(obj);
237    double pos;
238    if (!wd) return;
239    if (wd->val_max > wd->val_min)
240      pos = (wd->val - wd->val_min) / (wd->val_max - wd->val_min);
241    else
242      pos = 0.0;
243    if (pos < 0.0) pos = 0.0;
244    else if (pos > 1.0) pos = 1.0;
245    if (wd->inverted) pos = 1.0 - pos;
246    edje_object_part_drag_value_set(wd->slider, "elm.dragable.slider", pos, pos);
247 }
248
249 static void
250 _units_set(Evas_Object *obj)
251 {
252    Widget_Data *wd = elm_widget_data_get(obj);
253    if (!wd) return;
254    if (wd->units)
255      {
256         char buf[1024];
257
258         snprintf(buf, sizeof(buf), wd->units, wd->val);
259         edje_object_part_text_set(wd->slider, "elm.units", buf);
260      }
261    else if (wd->e_label)
262      edje_object_part_text_set(wd->slider, "elm.units", wd->e_label);
263    else
264      edje_object_part_text_set(wd->slider, "elm.units", NULL);
265 }
266
267 static void
268 _indicator_set(Evas_Object *obj)
269 {
270    Widget_Data *wd = elm_widget_data_get(obj);
271    if (!wd) return;
272    if (wd->indicator_format_func)
273      {
274         const char *buf;
275         buf = wd->indicator_format_func(wd->val);
276         edje_object_part_text_set(wd->slider, "elm.indicator", buf);
277      }
278    else if (wd->indicator)
279      {
280         char buf[1024];
281         snprintf(buf, sizeof(buf), wd->indicator, wd->val);
282         edje_object_part_text_set(wd->slider, "elm.indicator", buf);
283      }
284    else
285      edje_object_part_text_set(wd->slider, "elm.indicator", NULL);
286 }
287
288 static void
289 _drag(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
290 {
291     Widget_Data *wd = elm_widget_data_get((Evas_Object*)data);
292     /* delete thumb move timer when drag event occured to the moving thumb */
293     if(wd->mv_timer)
294       {
295          ecore_timer_del(wd->mv_timer);
296          wd->mv_timer = NULL;
297       }
298     edje_object_signal_emit(wd->slider, "elm,state,drag", "elm");
299     edje_object_message_signal_process(wd->slider);
300     _val_fetch(data);
301     _units_set(data);
302     _indicator_set(data);
303 }
304
305 static void
306 _drag_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
307 {
308    Widget_Data *wd = elm_widget_data_get((Evas_Object*)data);
309    /*  delete thumb move timer when drag event occured to the moving thumb */
310    if(wd->mv_timer)
311      {
312         ecore_timer_del(wd->mv_timer);
313         wd->mv_timer = NULL;
314      }
315    elm_widget_scroll_hold_push(data);
316    _val_fetch(data);
317    evas_object_smart_callback_call(data, SIG_DRAG_START, NULL);
318    edje_object_signal_emit(wd->slider, "elm,state,drag", "elm");
319     edje_object_message_signal_process(wd->slider); 
320    _units_set(data);
321    _indicator_set(data);
322 }
323
324 static void
325 _drag_stop(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
326 {
327    elm_widget_scroll_hold_pop(data);
328    _val_fetch(data);
329    evas_object_smart_callback_call(data, SIG_DRAG_STOP, NULL);
330    _units_set(data);
331    _indicator_set(data);
332 }
333
334 static void
335 _drag_up(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
336 {
337    Widget_Data *wd = elm_widget_data_get(data);
338    edje_object_part_drag_step(wd->slider, "elm.dragable.slider", -0.05, -0.05);
339 }
340
341 static void
342 _drag_down(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
343 {
344    Widget_Data *wd = elm_widget_data_get(data);
345    edje_object_part_drag_step(wd->slider, "elm.dragable.slider", 0.05, 0.05);
346 }
347
348 static void
349 _spacer_cb(void *data, Evas * e, Evas_Object * obj, void *event_info)
350 {
351    Widget_Data *wd = elm_widget_data_get(data);
352    Evas_Event_Mouse_Down *ev = event_info;
353    Evas_Coord x, y, w, h;
354    double button_x, button_y;
355
356    evas_object_geometry_get(wd->spacer, &x, &y, &w, &h);
357    edje_object_part_drag_value_get(wd->slider, "elm.dragable.slider", &button_x, &button_y);
358    if(wd->horizontal){
359      button_x = ((double)ev->output.x - (double)x) / (double)w;
360      if (button_x > 1)
361        button_x = 1;
362      if (button_x < 0)
363        button_x = 0;
364    }
365    else {
366      button_y = ((double)ev->output.y - (double)y) / (double)h;
367      if (button_y > 1)
368        button_y = 1;
369      if (button_y < 0)
370        button_y = 0;
371    }
372    edje_object_part_drag_value_set(wd->slider, "elm.dragable.slider", button_x, button_y);
373 //   evas_object_smart_callback_call(cp->parent, "clicked", NULL);
374    evas_event_feed_mouse_cancel(e, 0, NULL);
375    evas_event_feed_mouse_down(e, 1, EVAS_BUTTON_NONE, 0, NULL);
376 }
377
378 static const char*widtype = NULL;
379
380 /**
381  * Add a new slider to the parent
382  *
383  * @param parent The parent object
384  * @return The new object or NULL if it cannot be created
385  *
386  * @ingroup Slider
387  */
388 EAPI Evas_Object *
389 elm_slider_add(Evas_Object *parent)
390 {
391    Evas_Object *obj;
392    Evas *e;
393    Widget_Data *wd;
394
395    wd = ELM_NEW(Widget_Data);
396    e = evas_object_evas_get(parent);
397    obj = elm_widget_add(e);
398    ELM_SET_WIDTYPE(widtype, "slider");
399    elm_widget_type_set(obj, "slider");
400    elm_widget_sub_object_add(parent, obj);
401    elm_widget_data_set(obj, wd);
402    elm_widget_del_hook_set(obj, _del_hook);
403    elm_widget_theme_hook_set(obj, _theme_hook);
404
405    wd->horizontal = EINA_TRUE;
406    wd->val = 0.0;
407    wd->val_min = 0.0;
408    wd->val_max = 1.0;
409
410     wd->mv_step = (double)((wd->val_max - wd->val_min) / (double)SLIDER_THUMB_MOVE_STEP);
411
412    wd->slider = edje_object_add(e);
413    _elm_theme_object_set(obj, wd->slider, "slider", "horizontal", "default");
414    elm_widget_resize_object_set(obj, wd->slider);
415    edje_object_signal_callback_add(wd->slider, "drag", "*", _drag, obj);
416    edje_object_signal_callback_add(wd->slider, "drag,start", "*", _drag_start, obj);
417    edje_object_signal_callback_add(wd->slider, "drag,stop", "*", _drag_stop, obj);
418    edje_object_signal_callback_add(wd->slider, "drag,step", "*", _drag_stop, obj);
419    edje_object_signal_callback_add(wd->slider, "drag,page", "*", _drag_stop, obj);
420 //   edje_object_signal_callback_add(wd->slider, "drag,set", "*", _drag_stop, obj);
421    edje_object_signal_callback_add(wd->slider, "mouse,wheel,0,-1", "*", _drag_up, obj);
422    edje_object_signal_callback_add(wd->slider, "mouse,wheel,0,1", "*", _drag_down, obj);
423    edje_object_part_drag_value_set(wd->slider, "elm.dragable.slider", 0.0, 0.0);
424
425    wd->spacer = evas_object_rectangle_add(e);
426    evas_object_color_set(wd->spacer, 0, 0, 0, 0);
427    evas_object_pass_events_set(wd->spacer, 1);
428    elm_widget_sub_object_add(obj, wd->spacer);
429    edje_object_part_swallow(wd->slider, "elm.swallow.bar", wd->spacer);
430    evas_object_event_callback_add(wd->spacer, EVAS_CALLBACK_MOUSE_DOWN, _spacer_cb, obj);
431
432    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
433
434    _sizing_eval(obj);
435
436    // TODO: convert Elementary to subclassing of Evas_Smart_Class
437    // TODO: and save some bytes, making descriptions per-class and not instance!
438    evas_object_smart_callbacks_descriptions_set(obj, _signals);
439    return obj;
440 }
441
442 /**
443  * Set the label of the slider
444  *
445  * @param obj The slider object
446  * @param label The text label string in UTF-8
447  *
448  * @ingroup Slider
449  */
450 EAPI void
451 elm_slider_label_set(Evas_Object *obj, const char *label)
452 {
453    ELM_CHECK_WIDTYPE(obj, widtype);
454    Widget_Data *wd = elm_widget_data_get(obj);
455    if (!wd) return;
456    eina_stringshare_replace(&wd->label, label);
457    if (label)
458      {
459         edje_object_signal_emit(wd->slider, "elm,state,text,visible", "elm");
460         edje_object_message_signal_process(wd->slider);
461      }
462    else
463      {
464         edje_object_signal_emit(wd->slider, "elm,state,text,hidden", "elm");
465         edje_object_message_signal_process(wd->slider);
466      }
467    edje_object_part_text_set(wd->slider, "elm.text", label);
468    _sizing_eval(obj);
469 }
470
471 /**
472  * Get the label of the slider
473  *
474  * @param obj The slider object
475  * @return The text label string in UTF-8
476  *
477  * @ingroup Slider
478  */
479 EAPI const char *
480 elm_slider_label_get(const Evas_Object *obj)
481 {
482    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
483    Widget_Data *wd = elm_widget_data_get(obj);
484    if (!wd) return NULL;
485    return wd->label;
486 }
487
488 /**
489  * Set the icon object of the slider object
490  *
491  * Once the icon object is set, it will become a child of the slider object and
492  * be deleted when the slider object is deleted. If another icon object is set
493  * then the previous one becomes orophaned and will no longer be deleted along
494  * with the slider.
495  *
496  * @param obj The slider object
497  * @param icon The icon object
498  *
499  * @ingroup Slider
500  */
501 EAPI void
502 elm_slider_icon_set(Evas_Object *obj, Evas_Object *icon)
503 {
504    ELM_CHECK_WIDTYPE(obj, widtype);
505    Widget_Data *wd = elm_widget_data_get(obj);
506    if (!wd) return;
507    if ((wd->icon != icon) && (wd->icon))
508      elm_widget_sub_object_del(obj, wd->icon);
509    wd->icon = icon;
510    if (icon)
511      {
512         elm_widget_sub_object_add(obj, icon);
513         evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
514                                        _changed_size_hints, obj);
515         edje_object_part_swallow(wd->slider, "elm.swallow.content", icon);
516         edje_object_signal_emit(wd->slider, "elm,state,icon,visible", "elm");
517         _sizing_eval(obj);
518      }
519 }
520
521 /**
522  * Get the icon object of the slider object
523  *
524  * @param obj The slider object
525  * @return The icon object
526  *
527  * @ingroup Slider
528  */
529 EAPI Evas_Object *
530 elm_slider_icon_get(const Evas_Object *obj)
531 {
532    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
533    Widget_Data *wd = elm_widget_data_get(obj);
534    if (!wd) return NULL;
535    return wd->icon;
536 }
537
538 /**
539  * Set the length of the dragable region of the slider
540  *
541  * This sets the minimum width or height (depending on orientation) of the
542  * area of the slider that allows the slider to be dragged around. This in
543  * turn affects the objects minimum size (along with icon label and unit
544  * text). Note that this will also get multiplied by the scale factor.
545  *
546  * @param obj The slider object
547  * @param size The length of the slider area
548  *
549  * @ingroup Slider
550  */
551 EAPI void
552 elm_slider_span_size_set(Evas_Object *obj, Evas_Coord size)
553 {
554    ELM_CHECK_WIDTYPE(obj, widtype);
555    Widget_Data *wd = elm_widget_data_get(obj);
556    if (!wd) return;
557    if (wd->size == size) return;
558    wd->size = size;
559    if (wd->horizontal)
560      evas_object_size_hint_min_set(wd->spacer, (double)wd->size * elm_widget_scale_get(obj) * _elm_config->scale, 1);
561    else
562      evas_object_size_hint_min_set(wd->spacer, 1, (double)wd->size * elm_widget_scale_get(obj) * _elm_config->scale);
563    edje_object_part_swallow(wd->slider, "elm.swallow.bar", wd->spacer);
564    _sizing_eval(obj);
565 }
566
567 /**
568  * Get the length of the dragable region of the slider
569  *
570  * This gets the minimum width or height (depending on orientation) of
571  * the area of the slider that allows the slider to be dragged
572  * around. Note that this will also get multiplied by the scale
573  * factor.
574  *
575  * @param obj The slider object
576  * @return The length of the slider area
577  *
578  * @ingroup Slider
579  */
580 EAPI Evas_Coord
581 elm_slider_span_size_get(const Evas_Object *obj)
582 {
583    ELM_CHECK_WIDTYPE(obj, widtype) 0;
584    Widget_Data *wd = elm_widget_data_get(obj);
585    if (!wd) return 0;
586    return wd->size;
587 }
588
589 /**
590  * Set the format string of the unit area
591  *
592  * If NULL, this disabls the unit area display. If not it sets the format
593  * string for the unit text. The unit text is provided a floating point
594  * value, so the unit text can display up to 1 floating point value. Note that
595  * this is optional. Use a format string such as "%1.2f meters" for example.
596  *
597  * @param obj The slider object
598  * @param units The format string for the units display
599  *
600  * @ingroup Slider
601  */
602 EAPI void
603 elm_slider_unit_format_set(Evas_Object *obj, const char *units)
604 {
605    ELM_CHECK_WIDTYPE(obj, widtype);
606    Widget_Data *wd = elm_widget_data_get(obj);
607    if (!wd) return;
608    eina_stringshare_replace(&wd->units, units);
609    if (units)
610      {
611         edje_object_signal_emit(wd->slider, "elm,state,units,visible", "elm");
612         edje_object_message_signal_process(wd->slider);
613      }
614    else
615      {
616         edje_object_signal_emit(wd->slider, "elm,state,units,hidden", "elm");
617         edje_object_message_signal_process(wd->slider);
618      }
619    _units_set(obj);
620    _sizing_eval(obj);
621 }
622
623 /**
624  * Get the format string for the unit area
625  *
626  * The slider may also display a value (the value of the slider) somewhere
627  * (for example above the slider knob that is dragged around). This sets the
628  * format string for this. See elm_slider_unit_format_set() for more
629  * information on how this works.
630  *
631  * @param obj The slider object
632  * @return The format string for the unit display.
633  *
634  * @ingroup Slider
635  */
636 EAPI const char *
637 elm_slider_unit_format_get(const Evas_Object *obj)
638 {
639    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
640    Widget_Data *wd = elm_widget_data_get(obj);
641    if (!wd) return NULL;
642    return wd->units;
643 }
644
645 /**
646  * Set the format string for the indicator area
647  *
648  * The slider may also display a value (the value of the slider) somewhere
649  * (for example above the slider knob that is dragged around). This sets the
650  * format string for this. See elm_slider_unit_format_set() for more
651  * information on how this works.
652  *
653  * @param obj The slider object
654  * @param indicator The format string for the indicator display
655  *
656  * @ingroup Slider
657  */
658 EAPI void
659 elm_slider_indicator_format_set(Evas_Object *obj, const char *indicator)
660 {
661    ELM_CHECK_WIDTYPE(obj, widtype);
662    Widget_Data *wd = elm_widget_data_get(obj);
663    if (!wd) return;
664    eina_stringshare_replace(&wd->indicator, indicator);
665    _indicator_set(obj);
666 }
667
668 /**
669  * Get the format string for the indicator area
670  *
671  * The slider may also display a value (the value of the slider) somewhere
672  * (for example above the slider knob that is dragged around). This sets the
673  * format string for this. See elm_slider_indicator_format_set() for more
674  * information on how this works.
675  *
676  * @param obj The slider object
677  * @return The format string for the indicator display.
678  *
679  * @ingroup Slider
680  */
681 EAPI const char *
682 elm_slider_indicator_format_get(const Evas_Object *obj)
683 {
684    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
685    Widget_Data *wd = elm_widget_data_get(obj);
686    if (!wd) return NULL;
687    return wd->indicator;
688 }
689
690 /**
691  * Set orientation of the slider
692  *
693  * @param obj The slider object
694  * @param horizontal If set, the slider will be horizontal
695  *
696  * @ingroup Slider
697  */
698 EAPI void
699 elm_slider_horizontal_set(Evas_Object *obj, Eina_Bool horizontal)
700 {
701    ELM_CHECK_WIDTYPE(obj, widtype);
702    Widget_Data *wd = elm_widget_data_get(obj);
703    if (!wd) return;
704    horizontal = !!horizontal;
705    if (wd->horizontal == horizontal) return;
706    wd->horizontal = horizontal;
707    _theme_hook(obj);
708 }
709
710 /**
711  * Get orientation of the slider
712  *
713  * @param obj The slider object
714  * @return If @c EINA_TRUE the slider will be horizontal, else it is
715  *         vertical.
716  * @ingroup Slider
717  */
718 EAPI Eina_Bool
719 elm_slider_horizontal_get(const Evas_Object *obj)
720 {
721    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
722    Widget_Data *wd = elm_widget_data_get(obj);
723    if (!wd) return EINA_FALSE;
724    return wd->horizontal;
725 }
726
727 /**
728  * Set the minimum and maximum values for the slider
729  *
730  * Maximum mut be greater than minimum.
731  *
732  * @param obj The slider object
733  * @param min The minimum value
734  * @param max The maximum value
735  *
736  * @ingroup Slider
737  */
738 EAPI void
739 elm_slider_min_max_set(Evas_Object *obj, double min, double max)
740 {
741    ELM_CHECK_WIDTYPE(obj, widtype);
742    Widget_Data *wd = elm_widget_data_get(obj);
743    if (!wd) return;
744    if ((wd->val_min == min) && (wd->val_max == max)) return;
745    wd->val_min = min;
746    wd->val_max = max;
747
748    wd->mv_step = (double)((wd->val_max - wd->val_min) / (double)SLIDER_THUMB_MOVE_STEP);
749
750    if (wd->val < wd->val_min) wd->val = wd->val_min;
751    if (wd->val > wd->val_max) wd->val = wd->val_max;
752    _val_set(obj);
753    _units_set(obj);
754    _indicator_set(obj);
755 }
756
757 /**
758  * Get the minimum and maximum values for the slider
759  *
760  * @param obj The slider object
761  * @param min The pointer to store minimum value, may be @c NULL.
762  * @param max The pointer to store maximum value, may be @c NULL.
763  *
764  * @ingroup Slider
765  */
766 EAPI void
767 elm_slider_min_max_get(const Evas_Object *obj, double *min, double *max)
768 {
769    if (min) *min = 0.0;
770    if (max) *max = 0.0;
771    ELM_CHECK_WIDTYPE(obj, widtype);
772    Widget_Data *wd = elm_widget_data_get(obj);
773    if (!wd) return;
774    if (min) *min = wd->val_min;
775    if (max) *max = wd->val_max;
776 }
777
778 /**
779  * Set the value the slider indicates
780  *
781  * @param obj The slider object
782  * @param val The value (must be beween min and max for the slider)
783  *
784  * @ingroup Slider
785  */
786 EAPI void
787 elm_slider_value_set(Evas_Object *obj, double val)
788 {
789    ELM_CHECK_WIDTYPE(obj, widtype);
790    Widget_Data *wd = elm_widget_data_get(obj);
791    if (!wd) return;
792    if (wd->val == val) return;
793    wd->val = val;
794    if (wd->val < wd->val_min) wd->val = wd->val_min;
795    if (wd->val > wd->val_max) wd->val = wd->val_max;
796    edje_object_signal_emit(wd->slider, "elm,state,drag", "elm");
797    _val_set(obj);
798    _units_set(obj);
799    _indicator_set(obj);
800 }
801
802 /**
803  * Get the value the slider has
804  *
805  * @param obj The slider object
806  * @return The value of the slider
807  *
808  * @ingroup Slider
809  */
810 EAPI double
811 elm_slider_value_get(const Evas_Object *obj)
812 {
813    ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
814    Widget_Data *wd = elm_widget_data_get(obj);
815    if (!wd) return 0.0;
816    return wd->val;
817 }
818
819 /**
820  * Invert the slider display
821  *
822  * Normally the slider will display and interpret values from low to high
823  * and when horizontal that is left to right. When vertical that is top
824  * to bottom. This inverts this (so from right to left or bottom to top) if
825  * inverted is set to 1.
826  *
827  * @param obj The slider object
828  * @param inverted The inverted flag. 1 == inverted, 0 == normal
829  *
830  * @ingroup Slider
831  */
832 EAPI void
833 elm_slider_inverted_set(Evas_Object *obj, Eina_Bool inverted)
834 {
835    ELM_CHECK_WIDTYPE(obj, widtype);
836    Widget_Data *wd = elm_widget_data_get(obj);
837    if (!wd) return;
838    inverted = !!inverted;
839    if (wd->inverted == inverted) return;
840    wd->inverted = inverted;
841    if (wd->inverted)
842      edje_object_signal_emit(wd->slider, "elm,state,inverted,on", "elm");
843    else
844      edje_object_signal_emit(wd->slider, "elm,state,inverted,off", "elm");
845    edje_object_message_signal_process(wd->slider);
846    _val_set(obj);
847    _units_set(obj);
848    _indicator_set(obj);
849 }
850
851 /**
852  * Get if the slider display is inverted (backwards)
853  *
854  * @param obj The slider object
855  * @return If @c EINA_TRUE the slider will be inverted.
856  * @ingroup Slider
857  */
858 EAPI Eina_Bool
859 elm_slider_inverted_get(const Evas_Object *obj)
860 {
861    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
862    Widget_Data *wd = elm_widget_data_get(obj);
863    if (!wd) return EINA_FALSE;
864    return wd->inverted;
865 }
866
867 /**
868  * Set the format function pointer for the inducator area
869  *
870  * Set the callback function to format the indicator string.
871  * See elm_slider_indicator_format_set() for more info on how this works.
872  *
873  * @param obj The slider object
874  * @param indicator The format string for the indicator display
875  * @param func The indicator format function
876  *
877  * @ingroup Slider
878  */
879 EAPI void
880 elm_slider_indicator_format_function_set(Evas_Object *obj, const char *(*func)(double val))
881 {
882    ELM_CHECK_WIDTYPE(obj, widtype);
883    Widget_Data *wd = elm_widget_data_get(obj);
884    if (!wd) return;
885    wd->indicator_format_func = func;
886    _indicator_set(obj);
887 }
888
889
890 //////////////////////////////////////////////////////////////////////////////////////////////////////
891 ////////////////////////  supporting beat feature  ///////////////////////////////////////////////////
892 //////////////////////////////////////////////////////////////////////////////////////////////////////
893
894
895
896 /**
897  * Set the right icon object of the slider object
898  *
899  * Once the right icon object is set, it will become a child of the slider object and
900  * be deleted when the slider object is deleted. If another icon object is set
901  * then the previous one becomes orophaned and will no longer be deleted along
902  * with the slider.
903  *
904  * @param obj The slider object
905  * @param icon The icon object
906  * 
907  * @return 1 if icon set succeed, 0 if there is no part for right icon 
908  * 
909  * @ingroup Slider
910  */
911 EAPI Eina_Bool 
912 elm_slider_end_icon_set(Evas_Object *obj, Evas_Object *icon)
913 {
914    Widget_Data *wd = elm_widget_data_get(obj);
915
916    if ((wd->e_icon != icon) && (wd->e_icon))
917      elm_widget_sub_object_del(obj, wd->e_icon);
918
919    if (icon)
920      {
921         if ( !(edje_object_part_swallow(wd->slider, "end_icon", icon)) )
922           return EINA_FALSE;            
923         wd->e_icon = icon;
924         elm_widget_sub_object_add(obj, icon);
925         evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
926               _changed_size_hints, obj);
927         edje_object_signal_emit(wd->slider, "elm,state,eicon,visible", "elm");
928         _sizing_eval(obj);
929      }
930
931    return EINA_TRUE;
932 }
933
934
935 /**
936  * Get the right icon object of the slider object
937  *
938  * @param obj The slider object
939  * @return The right icon object
940  *
941  * @ingroup Slider
942  */
943 EAPI Evas_Object *
944 elm_slider_end_icon_get(Evas_Object *obj)
945 {
946    Widget_Data *wd = elm_widget_data_get(obj);
947    if (!wd) return NULL;
948    return wd->e_icon;
949 }
950
951
952 /**
953  * Set whether showing the number(indicator) or not.
954  *
955  * @param obj The slider object
956  * @param show 1 will show the number, 0 will not.
957  *
958  * @ingroup Slider
959  */
960 EAPI void
961 elm_slider_indicator_show_set(Evas_Object *obj, Eina_Bool show)
962 {
963    Widget_Data *wd = elm_widget_data_get(obj);
964    if(show)
965      edje_object_signal_emit(wd->slider, "elm,state,val,show", "elm");
966    else
967      edje_object_signal_emit(wd->slider, "elm,state,val,hide", "elm");                  
968 }
969
970 static Eina_Bool _mv_timer_cb(void *data)
971 {
972    Evas_Object* obj = (Evas_Object*)data;
973    Widget_Data *wd = elm_widget_data_get(obj);
974
975    if (!wd) return EINA_TRUE;
976
977    if(wd->src_val < wd->des_val) 
978      {
979         wd->src_val += wd->mv_step;
980         if(wd-> src_val > wd->des_val)  
981           wd->src_val = wd->des_val;
982      }  
983    else if (wd->src_val > wd->des_val) 
984      {
985         wd->src_val -= wd->mv_step;
986         if(wd->src_val < wd->des_val)   
987           wd->src_val = wd->des_val;
988      }  
989
990    elm_slider_value_set(obj, wd->src_val);
991    evas_object_smart_callback_call(obj, "changed", NULL);
992
993    if (wd->val == wd->des_val) 
994      {
995         if(wd->mv_timer)
996           {
997              ecore_timer_del(wd->mv_timer);
998              wd->mv_timer = NULL;
999           }     
1000         return EINA_FALSE;
1001      }
1002    else
1003      return EINA_TRUE;
1004 }
1005
1006
1007 /**
1008  * Move the thumb to the specified value.
1009  *
1010  * This is different with elm_slider_value_set() in animated moving. 
1011  * 
1012  * @param obj The slider object
1013  * @param val thumb's destination value.
1014  *
1015  * @ingroup Slider
1016  */
1017 EAPI void
1018 elm_slider_value_animated_set(Evas_Object *obj, double val)
1019 {
1020    Widget_Data *wd = elm_widget_data_get(obj);
1021
1022    if (wd->val == val) return;
1023
1024    wd->src_val = wd->val;
1025    wd->des_val = val;
1026    if (wd->des_val < wd->val_min) wd->des_val = wd->val_min;    
1027    if (wd->des_val > wd->val_max) wd->des_val = wd->val_max;
1028
1029    if(wd->mv_timer)
1030      {
1031         ecore_timer_del(wd->mv_timer);
1032         wd->mv_timer = NULL;
1033      }
1034
1035    wd->mv_timer = ecore_timer_add(0.005, _mv_timer_cb, obj);    
1036 }
1037
1038
1039 /**
1040  * Set the label of the slider
1041  *
1042  * @param obj The slider object
1043  * @param label The text label string in UTF-8
1044  *
1045  * @ingroup Slider
1046  */
1047 EAPI void
1048 elm_slider_end_label_set(Evas_Object *obj, const char *label)
1049 {
1050    ELM_CHECK_WIDTYPE(obj, widtype);
1051    Widget_Data *wd = elm_widget_data_get(obj);
1052    if (!wd) return;
1053    eina_stringshare_replace(&wd->e_label, label);
1054    if (label)
1055      {
1056         edje_object_signal_emit(wd->slider, "elm,state,units,visible", "elm");
1057         edje_object_message_signal_process(wd->slider);
1058      }
1059    else
1060      {
1061         edje_object_signal_emit(wd->slider, "elm,state,units,hidden", "elm");
1062         edje_object_message_signal_process(wd->slider);
1063      }
1064    edje_object_part_text_set(wd->slider, "elm.units", label);
1065    if(wd->units)
1066      wd->units = NULL;
1067    _sizing_eval(obj);
1068 }
1069
1070 /**
1071  * Get the label of the slider
1072  *
1073  * @param obj The slider object
1074  * @return The text label string in UTF-8
1075  *
1076  * @ingroup Slider
1077  */
1078 EAPI const char *
1079 elm_slider_end_label_get(const Evas_Object *obj)
1080 {
1081    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1082    Widget_Data *wd = elm_widget_data_get(obj);
1083    if (!wd) return NULL;
1084    return wd->e_label;
1085 }