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