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