elementary: make it possible to have more than one text in a slider.
[framework/uifw/elementary.git] / src / lib / elm_slider.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 typedef struct _Widget_Data Widget_Data;
5
6 struct _Widget_Data
7 {
8    Evas_Object *slider;
9    Evas_Object *icon;
10    Evas_Object *end;
11    Evas_Object *spacer;
12
13    Ecore_Timer *delay;
14
15    Eina_Hash  *labels;
16    const char *units;
17    const char *indicator;
18
19    const char *(*indicator_format_func)(double val);
20    void (*indicator_format_free)(const char *str);
21
22    const char *(*units_format_func)(double val);
23    void (*units_format_free)(const char *str);
24
25    double val, val_min, val_max;
26    Evas_Coord size;
27
28    Eina_Bool horizontal : 1;
29    Eina_Bool inverted : 1;
30    Eina_Bool indicator_show : 1;
31 };
32
33 #define ELM_SLIDER_INVERTED_FACTOR (-1.0)
34
35 static const char *widtype = NULL;
36 static void _del_hook(Evas_Object *obj);
37 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
38 static void _theme_hook(Evas_Object *obj);
39 static void _disable_hook(Evas_Object *obj);
40 static void _sizing_eval(Evas_Object *obj);
41 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
42 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
43 static void _units_set(Evas_Object *obj);
44 static void _val_set(Evas_Object *obj);
45 static void _indicator_set(Evas_Object *obj);
46 static void _on_focus_hook(void *data, Evas_Object *obj);
47 static void _drag_up(void *data, Evas_Object *obj,
48                     const char *emission, const char *source);
49 static void _drag_down(void *data, Evas_Object *obj,
50                     const char *emission, const char *source);
51 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
52                              Evas_Callback_Type type, void *event_info);
53 static void _spacer_cb(void *data, Evas * e, Evas_Object * obj, void *event_info);
54
55 static const char SIG_CHANGED[] = "changed";
56 static const char SIG_DELAY_CHANGED[] = "delay,changed";
57 static const char SIG_DRAG_START[] = "slider,drag,start";
58 static const char SIG_DRAG_STOP[] = "slider,drag,stop";
59 static const Evas_Smart_Cb_Description _signals[] = {
60   {SIG_CHANGED, ""},
61   {SIG_DELAY_CHANGED, ""},
62   {SIG_DRAG_START, ""},
63   {SIG_DRAG_STOP, ""},
64   {NULL, NULL}
65 };
66
67 static Eina_Bool
68 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
69 {
70    Evas_Event_Mouse_Wheel *mev;
71    Evas_Event_Key_Down *ev;
72    Widget_Data *wd;
73
74    wd = elm_widget_data_get(obj);
75    if (!wd) return EINA_FALSE;
76
77    if (type == EVAS_CALLBACK_KEY_DOWN) goto key_down;
78    else if (type != EVAS_CALLBACK_MOUSE_WHEEL) return EINA_FALSE;
79
80    mev = event_info;
81    if (mev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
82    if (elm_widget_disabled_get(obj)) return EINA_FALSE;
83
84    if (mev->z < 0) _drag_up(obj, NULL, NULL, NULL);
85    else _drag_down(obj, NULL, NULL, NULL);
86    mev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
87    return EINA_TRUE;
88
89   key_down:
90    ev = event_info;
91    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
92    if (elm_widget_disabled_get(obj)) return EINA_FALSE;
93    if ((!strcmp(ev->keyname, "Left"))
94        || (!strcmp(ev->keyname, "KP_Left")))
95      {
96         if (!wd->horizontal) return EINA_FALSE;
97         if (!wd->inverted) _drag_down(obj, NULL, NULL, NULL);
98         else _drag_up(obj, NULL, NULL, NULL);
99         ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
100         return EINA_TRUE;
101      }
102    else if ((!strcmp(ev->keyname, "Right"))
103             || (!strcmp(ev->keyname, "KP_Right")))
104      {
105         if (!wd->horizontal) return EINA_FALSE;
106         if (!wd->inverted) _drag_up(obj, NULL, NULL, NULL);
107         else _drag_down(obj, NULL, NULL, NULL);
108         ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
109         return EINA_TRUE;
110      }
111    else if ((!strcmp(ev->keyname, "Up")) || (!strcmp(ev->keyname, "KP_Up")))
112      {
113         if (wd->horizontal) return EINA_FALSE;
114         if (wd->inverted) _drag_up(obj, NULL, NULL, NULL);
115         else _drag_down(obj, NULL, NULL, NULL);
116         ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
117         return EINA_TRUE;
118      }
119    else if ((!strcmp(ev->keyname, "Down")) || (!strcmp(ev->keyname, "KP_Down")))
120      {
121         if (wd->horizontal) return EINA_FALSE;
122         if (wd->inverted) _drag_down(obj, NULL, NULL, NULL);
123         else _drag_up(obj, NULL, NULL, NULL);
124         ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
125         return EINA_TRUE;
126      }
127    else return EINA_FALSE;
128 }
129
130 static void
131 _del_hook(Evas_Object *obj)
132 {
133    Widget_Data *wd = elm_widget_data_get(obj);
134    if (!wd) return;
135    if (wd->labels) eina_hash_free(wd->labels);
136    if (wd->indicator) eina_stringshare_del(wd->units);
137    if (wd->delay) ecore_timer_del(wd->delay);
138    free(wd);
139 }
140
141 static void
142 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
143 {
144    Widget_Data *wd = elm_widget_data_get(obj);
145    if (!wd) return;
146    if (elm_widget_focus_get(obj))
147      {
148         edje_object_signal_emit(wd->slider, "elm,action,focus", "elm");
149         evas_object_focus_set(wd->slider, EINA_TRUE);
150      }
151    else
152      {
153         edje_object_signal_emit(wd->slider, "elm,action,unfocus", "elm");
154         evas_object_focus_set(wd->slider, EINA_FALSE);
155      }
156 }
157
158 static void
159 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
160 {
161    Widget_Data *wd = elm_widget_data_get(obj);
162    if (!wd) return;
163    edje_object_mirrored_set(wd->slider, rtl);
164 }
165
166 static Eina_Bool
167 _labels_foreach_text_set(const Eina_Hash *hash __UNUSED__, const void *key, void *data, void *fdata)
168 {
169   Widget_Data *wd = fdata;
170
171   edje_object_part_text_set(wd->slider, key, data);
172
173   return 1;
174 }
175
176 static void
177 _theme_hook(Evas_Object *obj)
178 {
179    Widget_Data *wd = elm_widget_data_get(obj);
180    if (!wd) return;
181    _elm_widget_mirrored_reload(obj);
182    _mirrored_set(obj, elm_widget_mirrored_get(obj));
183    if (wd->horizontal)
184      _elm_theme_object_set(obj, wd->slider, "slider", "horizontal", elm_widget_style_get(obj));
185    else
186      _elm_theme_object_set(obj, wd->slider, "slider", "vertical", elm_widget_style_get(obj));
187    if (wd->icon)
188      {
189         edje_object_part_swallow(wd->slider, "elm.swallow.content", wd->icon);
190         edje_object_signal_emit(wd->slider, "elm,state,icon,visible", "elm");
191      }
192    if (wd->end)
193      edje_object_signal_emit(wd->slider, "elm,state,end,visible", "elm");
194    else
195      edje_object_signal_emit(wd->slider, "elm,state,end,hidden", "elm");
196    if (wd->labels)
197      {
198         eina_hash_foreach(wd->labels, _labels_foreach_text_set, wd);
199         edje_object_signal_emit(wd->slider, "elm,state,text,visible", "elm");
200      }
201
202    if (wd->units)
203      edje_object_signal_emit(wd->slider, "elm,state,units,visible", "elm");
204
205    if (wd->horizontal)
206      evas_object_size_hint_min_set(wd->spacer, (double)wd->size * elm_widget_scale_get(obj) * _elm_config->scale, 1);
207    else
208      evas_object_size_hint_min_set(wd->spacer, 1, (double)wd->size * elm_widget_scale_get(obj) * _elm_config->scale);
209
210    if (wd->inverted)
211      edje_object_signal_emit(wd->slider, "elm,state,inverted,on", "elm");
212
213    edje_object_part_swallow(wd->slider, "elm.swallow.bar", wd->spacer);
214    _units_set(obj);
215    _indicator_set(obj);
216    edje_object_message_signal_process(wd->slider);
217    edje_object_scale_set(wd->slider, elm_widget_scale_get(obj) * _elm_config->scale);
218    _val_set(obj);
219    _sizing_eval(obj);
220 }
221
222 static void
223 _disable_hook(Evas_Object *obj)
224 {
225    Widget_Data *wd = elm_widget_data_get(obj);
226    if (!wd) return;
227    if (elm_widget_disabled_get(obj))
228      edje_object_signal_emit(wd->slider, "elm,state,disabled", "elm");
229    else
230      edje_object_signal_emit(wd->slider, "elm,state,enabled", "elm");
231 }
232
233 static void
234 _sizing_eval(Evas_Object *obj)
235 {
236    Widget_Data *wd = elm_widget_data_get(obj);
237    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
238    if (!wd) return;
239    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
240    edje_object_size_min_restricted_calc(wd->slider, &minw, &minh, minw, minh);
241    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
242    evas_object_size_hint_min_set(obj, minw, minh);
243    evas_object_size_hint_max_set(obj, maxw, maxh);
244 }
245
246 static void
247 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
248 {
249    Widget_Data *wd = elm_widget_data_get(data);
250    if (!wd) return;
251    if ((obj != wd->icon) && (obj != wd->end)) return;
252    _sizing_eval(data);
253 }
254
255 static void
256 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
257 {
258    Widget_Data *wd = elm_widget_data_get(obj);
259    Evas_Object *sub = event_info;
260    if (!wd) return;
261    if (sub == wd->icon)
262      {
263         edje_object_signal_emit(wd->slider, "elm,state,icon,hidden", "elm");
264         evas_object_event_callback_del_full
265            (sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
266         wd->icon = NULL;
267         edje_object_message_signal_process(wd->slider);
268         _sizing_eval(obj);
269      }
270    if (sub == wd->end)
271      {
272         edje_object_signal_emit(wd->slider, "elm,state,end,hidden", "elm");
273         evas_object_event_callback_del_full(sub,
274                                             EVAS_CALLBACK_CHANGED_SIZE_HINTS,
275                                             _changed_size_hints, obj);
276         wd->end = NULL;
277         edje_object_message_signal_process(wd->slider);
278         _sizing_eval(obj);
279      }
280 }
281
282 static Eina_Bool
283 _delay_change(void *data)
284 {
285    Widget_Data *wd = elm_widget_data_get(data);
286    if (!wd) return ECORE_CALLBACK_CANCEL;
287    wd->delay = NULL;
288    evas_object_smart_callback_call(data, SIG_DELAY_CHANGED, NULL);
289    return ECORE_CALLBACK_CANCEL;
290 }
291
292 static void
293 _val_fetch(Evas_Object *obj)
294 {
295    Eina_Bool rtl;
296    Widget_Data *wd = elm_widget_data_get(obj);
297    double posx = 0.0, posy = 0.0, pos = 0.0, val;
298    if (!wd) return;
299    edje_object_part_drag_value_get(wd->slider, "elm.dragable.slider",
300                                    &posx, &posy);
301    if (wd->horizontal) pos = posx;
302    else pos = posy;
303
304    rtl = elm_widget_mirrored_get(obj);
305    if ((!rtl && wd->inverted) || (rtl &&
306                                   ((!wd->horizontal && wd->inverted) ||
307                                    (wd->horizontal && !wd->inverted)))) pos = 1.0 - pos;
308    val = (pos * (wd->val_max - wd->val_min)) + wd->val_min;
309    if (val != wd->val)
310      {
311         wd->val = val;
312         evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
313         if (wd->delay) ecore_timer_del(wd->delay);
314         wd->delay = ecore_timer_add(0.2, _delay_change, obj);
315      }
316 }
317
318 static void
319 _val_set(Evas_Object *obj)
320 {
321    Eina_Bool rtl;
322    Widget_Data *wd = elm_widget_data_get(obj);
323    double pos;
324    if (!wd) return;
325    if (wd->val_max > wd->val_min)
326      pos = (wd->val - wd->val_min) / (wd->val_max - wd->val_min);
327    else
328      pos = 0.0;
329    if (pos < 0.0) pos = 0.0;
330    else if (pos > 1.0) pos = 1.0;
331
332    rtl = elm_widget_mirrored_get(obj);
333    if ((!rtl && wd->inverted) || (rtl &&
334                                   ((!wd->horizontal && wd->inverted) ||
335                                    (wd->horizontal && !wd->inverted)))) pos = 1.0 - pos;
336    edje_object_part_drag_value_set(wd->slider, "elm.dragable.slider", pos, pos);
337 }
338
339 static void
340 _units_set(Evas_Object *obj)
341 {
342    Widget_Data *wd = elm_widget_data_get(obj);
343    if (!wd) return;
344    if (wd->units_format_func)
345      {
346         const char *buf;
347         buf = wd->units_format_func(wd->val);
348         edje_object_part_text_set(wd->slider, "elm.units", buf);
349         if (wd->units_format_free) wd->units_format_free(buf);
350      }
351    else if (wd->units)
352      {
353         char buf[1024];
354
355         snprintf(buf, sizeof(buf), wd->units, wd->val);
356         edje_object_part_text_set(wd->slider, "elm.units", buf);
357      }
358    else
359      edje_object_part_text_set(wd->slider, "elm.units", NULL);
360 }
361
362 static void
363 _indicator_set(Evas_Object *obj)
364 {
365    Widget_Data *wd = elm_widget_data_get(obj);
366    if (!wd) return;
367    if (wd->indicator_format_func)
368      {
369         const char *buf;
370         buf = wd->indicator_format_func(wd->val);
371         edje_object_part_text_set(wd->slider, "elm.dragable.slider:elm.indicator", buf);
372         if (wd->indicator_format_free) wd->indicator_format_free(buf);
373      }
374    else if (wd->indicator)
375      {
376         char buf[1024];
377         snprintf(buf, sizeof(buf), wd->indicator, wd->val);
378         edje_object_part_text_set(wd->slider, "elm.dragable.slider:elm.indicator", buf);
379      }
380    else
381      edje_object_part_text_set(wd->slider, "elm.dragable.slider:elm.indicator", NULL);
382 }
383
384 static void
385 _drag(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
386 {
387    _val_fetch(data);
388    _units_set(data);
389    _indicator_set(data);
390 }
391
392 static void
393 _drag_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
394 {
395    _val_fetch(data);
396    evas_object_smart_callback_call(data, SIG_DRAG_START, NULL);
397    _units_set(data);
398    _indicator_set(data);
399    elm_widget_scroll_freeze_push(data);
400 }
401
402 static void
403 _drag_stop(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
404 {
405    _val_fetch(data);
406    evas_object_smart_callback_call(data, SIG_DRAG_STOP, NULL);
407    _units_set(data);
408    _indicator_set(data);
409    elm_widget_scroll_freeze_pop(data);
410 }
411
412 static void
413 _drag_step(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
414 {
415    _val_fetch(data);
416    _units_set(data);
417    _indicator_set(data);
418 }
419
420 static void
421 _drag_up(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
422 {
423    double step;
424    Widget_Data *wd;
425
426    wd = elm_widget_data_get(data);
427    step = 0.05;
428
429    if (wd->inverted) step *= ELM_SLIDER_INVERTED_FACTOR;
430
431    edje_object_part_drag_step(wd->slider, "elm.dragable.slider", step, step);
432 }
433
434 static void
435 _drag_down(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
436 {
437    double step;
438    Widget_Data *wd;
439
440    wd = elm_widget_data_get(data);
441    step = -0.05;
442
443    if (wd->inverted) step *= ELM_SLIDER_INVERTED_FACTOR;
444
445    edje_object_part_drag_step(wd->slider, "elm.dragable.slider", step, step);
446 }
447
448 static void
449 _spacer_cb(void *data, Evas *e, Evas_Object *obj __UNUSED__, void *event_info)
450 {
451    Widget_Data *wd = elm_widget_data_get(data);
452    Evas_Event_Mouse_Down *ev = event_info;
453    Evas_Coord x, y, w, h;
454    double button_x, button_y;
455
456    evas_object_geometry_get(wd->spacer, &x, &y, &w, &h);
457    edje_object_part_drag_value_get(wd->slider, "elm.dragable.slider", &button_x, &button_y);
458    if (wd->horizontal)
459      {
460         button_x = ((double)ev->canvas.x - (double)x) / (double)w;
461         if (button_x > 1) button_x = 1;
462         if (button_x < 0) button_x = 0;
463      }
464    else
465      {
466         button_y = ((double)ev->canvas.y - (double)y) / (double)h;
467         if (button_y > 1) button_y = 1;
468         if (button_y < 0) button_y = 0;
469      }
470    edje_object_part_drag_value_set(wd->slider, "elm.dragable.slider", button_x, button_y);
471    evas_event_feed_mouse_cancel(e, 0, NULL);
472    evas_event_feed_mouse_down(e, 1, EVAS_BUTTON_NONE, 0, NULL);
473 }
474
475 static void
476 _elm_slider_label_set(Evas_Object *obj, const char *part, const char *label)
477 {
478    ELM_CHECK_WIDTYPE(obj, widtype);
479    Widget_Data *wd = elm_widget_data_get(obj);
480    const char* default_part = "elm.text";
481    const char* real_part;
482
483    if (!wd) return;
484
485    if (!part)
486      real_part = default_part;
487    else
488      real_part = part;
489
490    if (wd->labels)
491      {
492        const char* old_label;
493
494        old_label = eina_hash_find(wd->labels, real_part);
495        if (!old_label)
496            eina_hash_add(wd->labels, real_part, eina_stringshare_add(label));
497        else
498          {
499            eina_stringshare_ref(old_label);
500            eina_hash_modify(wd->labels, real_part, eina_stringshare_add(label));
501            eina_stringshare_del(old_label);
502          }
503      }
504
505    if (label)
506      {
507         edje_object_signal_emit(wd->slider, "elm,state,text,visible", "elm");
508         edje_object_message_signal_process(wd->slider);
509      }
510    else
511      {
512         edje_object_signal_emit(wd->slider, "elm,state,text,hidden", "elm");
513         edje_object_message_signal_process(wd->slider);
514      }
515
516    edje_object_part_text_set(wd->slider, real_part, label);
517    _sizing_eval(obj);
518 }
519
520 static const char *
521 _elm_slider_label_get(const Evas_Object *obj, const char *part)
522 {
523    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
524    Widget_Data *wd = elm_widget_data_get(obj);
525    if (!wd) return NULL;
526    if (!wd->labels) return NULL;
527
528    if (!part) 
529      return eina_hash_find(wd->labels, "elm.text");
530    return eina_hash_find(wd->labels, part);
531 }
532
533 static void
534 _icon_set(Evas_Object *obj, Evas_Object *icon)
535 {
536    Widget_Data *wd = elm_widget_data_get(obj);
537    if (!wd) return;
538    if (wd->icon == icon) return;
539    if (wd->icon) evas_object_del(wd->icon);
540    wd->icon = icon;
541    if (icon)
542      {
543         elm_widget_sub_object_add(obj, icon);
544         evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
545                                        _changed_size_hints, obj);
546         edje_object_part_swallow(wd->slider, "elm.swallow.icon", icon);
547         edje_object_signal_emit(wd->slider, "elm,state,icon,visible", "elm");
548         edje_object_message_signal_process(wd->slider);
549      }
550    _sizing_eval(obj);
551 }
552
553 static Evas_Object *
554 _icon_unset(Evas_Object *obj)
555 {
556    Widget_Data *wd = elm_widget_data_get(obj);
557    Evas_Object *ret = NULL;
558    if (!wd) return NULL;
559    if (wd->icon)
560      {
561         elm_widget_sub_object_del(obj, wd->icon);
562         evas_object_event_callback_del_full(wd->icon,
563                                             EVAS_CALLBACK_CHANGED_SIZE_HINTS,
564                                             _changed_size_hints, obj);
565         ret = wd->icon;
566         edje_object_part_unswallow(wd->slider, wd->icon);
567         edje_object_signal_emit(wd->slider, "elm,state,icon,hidden", "elm");
568         wd->icon = NULL;
569         _sizing_eval(obj);
570      }
571    return ret;
572 }
573
574 static void
575 _end_set(Evas_Object *obj, Evas_Object *end)
576 {
577    Widget_Data *wd = elm_widget_data_get(obj);
578    if (!wd) return;
579    if (wd->end == end) return;
580    if (wd->end) evas_object_del(wd->end);
581    wd->end = end;
582    if (end)
583      {
584         elm_widget_sub_object_add(obj, end);
585         evas_object_event_callback_add(end, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
586                                        _changed_size_hints, obj);
587         edje_object_part_swallow(wd->slider, "elm.swallow.end", end);
588         edje_object_signal_emit(wd->slider, "elm,state,end,visible", "elm");
589         edje_object_message_signal_process(wd->slider);
590      }
591    _sizing_eval(obj);
592 }
593
594 static Evas_Object *
595 _end_unset(Evas_Object *obj)
596 {
597    Widget_Data *wd = elm_widget_data_get(obj);
598    Evas_Object *ret = NULL;
599    if (!wd) return NULL;
600    if (wd->end)
601      {
602         elm_widget_sub_object_del(obj, wd->end);
603         evas_object_event_callback_del_full(wd->end,
604                                             EVAS_CALLBACK_CHANGED_SIZE_HINTS,
605                                             _changed_size_hints, obj);
606         ret = wd->end;
607         edje_object_part_unswallow(wd->slider, wd->end);
608         edje_object_signal_emit(wd->slider, "elm,state,end,hidden", "elm");
609         wd->end = NULL;
610         _sizing_eval(obj);
611      }
612    return ret;
613 }
614
615 static void
616 _content_set_hook(Evas_Object *obj, const char *part, Evas_Object *content)
617 {
618    ELM_CHECK_WIDTYPE(obj, widtype);
619    if (!part || !strcmp(part, "icon"))
620      _icon_set(obj, content);
621    else if (!strcmp(part, "end"))
622      _end_set(obj, content);
623 }
624
625 static Evas_Object *
626 _content_get_hook(const Evas_Object *obj, const char *part)
627 {
628    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
629    Widget_Data *wd;
630    wd = elm_widget_data_get(obj);
631    if (!wd) return NULL;
632    if (!part || !strcmp(part, "icon"))
633      return wd->icon;
634    else if (!strcmp(part, "end"))
635      return wd->end;
636    return NULL;
637 }
638
639 static Evas_Object *
640 _content_unset_hook(Evas_Object *obj, const char *part)
641 {
642    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
643    if (!part || !strcmp(part, "icon"))
644      return _icon_unset(obj);
645    else if (!strcmp(part, "end"))
646      return _end_unset(obj);
647    return NULL;
648 }
649
650 static void
651 _hash_labels_free_cb(void* label)
652 {
653   if (label)
654     eina_stringshare_del(label);
655 }
656
657 EAPI Evas_Object *
658 elm_slider_add(Evas_Object *parent)
659 {
660    Evas_Object *obj;
661    Evas *e;
662    Widget_Data *wd;
663
664    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
665
666    ELM_SET_WIDTYPE(widtype, "slider");
667    elm_widget_type_set(obj, "slider");
668    elm_widget_sub_object_add(parent, obj);
669    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
670    elm_widget_data_set(obj, wd);
671    elm_widget_del_hook_set(obj, _del_hook);
672    elm_widget_theme_hook_set(obj, _theme_hook);
673    elm_widget_disable_hook_set(obj, _disable_hook);
674    elm_widget_can_focus_set(obj, EINA_TRUE);
675    elm_widget_event_hook_set(obj, _event_hook);
676    elm_widget_text_set_hook_set(obj, _elm_slider_label_set);
677    elm_widget_text_get_hook_set(obj, _elm_slider_label_get);
678    elm_widget_content_set_hook_set(obj, _content_set_hook);
679    elm_widget_content_get_hook_set(obj, _content_get_hook);
680    elm_widget_content_unset_hook_set(obj, _content_unset_hook);
681
682    wd->horizontal = EINA_TRUE;
683    wd->indicator_show = EINA_TRUE;
684    wd->val = 0.0;
685    wd->val_min = 0.0;
686    wd->val_max = 1.0;
687    wd->labels = eina_hash_string_superfast_new(_hash_labels_free_cb);
688
689    wd->slider = edje_object_add(e);
690    _elm_theme_object_set(obj, wd->slider, "slider", "horizontal", "default");
691    elm_widget_resize_object_set(obj, wd->slider);
692    edje_object_signal_callback_add(wd->slider, "drag", "*", _drag, obj);
693    edje_object_signal_callback_add(wd->slider, "drag,start", "*", _drag_start, obj);
694    edje_object_signal_callback_add(wd->slider, "drag,stop", "*", _drag_stop, obj);
695    edje_object_signal_callback_add(wd->slider, "drag,step", "*", _drag_step, obj);
696    edje_object_signal_callback_add(wd->slider, "drag,page", "*", _drag_stop, obj);
697    //   edje_object_signal_callback_add(wd->slider, "drag,set", "*", _drag_stop, obj);
698    edje_object_part_drag_value_set(wd->slider, "elm.dragable.slider", 0.0, 0.0);
699
700    wd->spacer = evas_object_rectangle_add(e);
701    evas_object_color_set(wd->spacer, 0, 0, 0, 0);
702    evas_object_pass_events_set(wd->spacer, EINA_TRUE);
703    elm_widget_sub_object_add(obj, wd->spacer);
704    edje_object_part_swallow(wd->slider, "elm.swallow.bar", wd->spacer);
705    evas_object_event_callback_add(wd->spacer, EVAS_CALLBACK_MOUSE_DOWN, _spacer_cb, obj);
706    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
707
708    _mirrored_set(obj, elm_widget_mirrored_get(obj));
709    _sizing_eval(obj);
710
711    // TODO: convert Elementary to subclassing of Evas_Smart_Class
712    // TODO: and save some bytes, making descriptions per-class and not instance!
713    evas_object_smart_callbacks_descriptions_set(obj, _signals);
714    return obj;
715 }
716
717 EAPI void
718 elm_slider_label_set(Evas_Object *obj, const char *label)
719 {
720    _elm_slider_label_set(obj, NULL, label);
721 }
722
723 EAPI const char *
724 elm_slider_label_get(const Evas_Object *obj)
725 {
726    return _elm_slider_label_get(obj, NULL);
727 }
728
729 EAPI void
730 elm_slider_icon_set(Evas_Object *obj, Evas_Object *icon)
731 {
732    _content_set_hook(obj, "icon", icon);
733 }
734
735 EAPI Evas_Object *
736 elm_slider_icon_unset(Evas_Object *obj)
737 {
738    return _content_unset_hook(obj, "icon");
739 }
740
741 EAPI Evas_Object *
742 elm_slider_icon_get(const Evas_Object *obj)
743 {
744    return _content_get_hook(obj, "icon");
745 }
746
747 EAPI void
748 elm_slider_span_size_set(Evas_Object *obj, Evas_Coord size)
749 {
750    ELM_CHECK_WIDTYPE(obj, widtype);
751    Widget_Data *wd = elm_widget_data_get(obj);
752    if (!wd) return;
753    if (wd->size == size) return;
754    wd->size = size;
755    if (wd->horizontal)
756      evas_object_size_hint_min_set(wd->spacer, (double)wd->size * elm_widget_scale_get(obj) * _elm_config->scale, 1);
757    else
758      evas_object_size_hint_min_set(wd->spacer, 1, (double)wd->size * elm_widget_scale_get(obj) * _elm_config->scale);
759    if (wd->indicator_show)
760      edje_object_signal_emit(wd->slider, "elm,state,val,show", "elm");
761    else
762      edje_object_signal_emit(wd->slider, "elm,state,val,hide", "elm");
763    edje_object_part_swallow(wd->slider, "elm.swallow.bar", wd->spacer);
764    _sizing_eval(obj);
765 }
766
767 EAPI Evas_Coord
768 elm_slider_span_size_get(const Evas_Object *obj)
769 {
770    ELM_CHECK_WIDTYPE(obj, widtype) 0;
771    Widget_Data *wd = elm_widget_data_get(obj);
772    if (!wd) return 0;
773    return wd->size;
774 }
775
776 EAPI void
777 elm_slider_unit_format_set(Evas_Object *obj, const char *units)
778 {
779    ELM_CHECK_WIDTYPE(obj, widtype);
780    Widget_Data *wd = elm_widget_data_get(obj);
781    if (!wd) return;
782    eina_stringshare_replace(&wd->units, units);
783    if (units)
784      {
785         edje_object_signal_emit(wd->slider, "elm,state,units,visible", "elm");
786         edje_object_message_signal_process(wd->slider);
787      }
788    else
789      {
790         edje_object_signal_emit(wd->slider, "elm,state,units,hidden", "elm");
791         edje_object_message_signal_process(wd->slider);
792      }
793    _units_set(obj);
794    _sizing_eval(obj);
795 }
796
797 EAPI const char *
798 elm_slider_unit_format_get(const Evas_Object *obj)
799 {
800    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
801    Widget_Data *wd = elm_widget_data_get(obj);
802    if (!wd) return NULL;
803    return wd->units;
804 }
805
806 EAPI void
807 elm_slider_indicator_format_set(Evas_Object *obj, const char *indicator)
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->indicator, indicator);
813    _indicator_set(obj);
814 }
815
816 EAPI const char *
817 elm_slider_indicator_format_get(const Evas_Object *obj)
818 {
819    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
820    Widget_Data *wd = elm_widget_data_get(obj);
821    if (!wd) return NULL;
822    return wd->indicator;
823 }
824
825 EAPI void
826 elm_slider_horizontal_set(Evas_Object *obj, Eina_Bool horizontal)
827 {
828    ELM_CHECK_WIDTYPE(obj, widtype);
829    Widget_Data *wd = elm_widget_data_get(obj);
830    if (!wd) return;
831    horizontal = !!horizontal;
832    if (wd->horizontal == horizontal) return;
833    wd->horizontal = horizontal;
834    _theme_hook(obj);
835 }
836
837 EAPI Eina_Bool
838 elm_slider_horizontal_get(const Evas_Object *obj)
839 {
840    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
841    Widget_Data *wd = elm_widget_data_get(obj);
842    if (!wd) return EINA_FALSE;
843    return wd->horizontal;
844 }
845
846 EAPI void
847 elm_slider_min_max_set(Evas_Object *obj, double min, double max)
848 {
849    ELM_CHECK_WIDTYPE(obj, widtype);
850    Widget_Data *wd = elm_widget_data_get(obj);
851    if (!wd) return;
852    if ((wd->val_min == min) && (wd->val_max == max)) return;
853    wd->val_min = min;
854    wd->val_max = max;
855    if (wd->val < wd->val_min) wd->val = wd->val_min;
856    if (wd->val > wd->val_max) wd->val = wd->val_max;
857    _val_set(obj);
858    _units_set(obj);
859    _indicator_set(obj);
860 }
861
862 EAPI void
863 elm_slider_min_max_get(const Evas_Object *obj, double *min, double *max)
864 {
865    if (min) *min = 0.0;
866    if (max) *max = 0.0;
867    ELM_CHECK_WIDTYPE(obj, widtype);
868    Widget_Data *wd = elm_widget_data_get(obj);
869    if (!wd) return;
870    if (min) *min = wd->val_min;
871    if (max) *max = wd->val_max;
872 }
873
874 EAPI void
875 elm_slider_value_set(Evas_Object *obj, double val)
876 {
877    ELM_CHECK_WIDTYPE(obj, widtype);
878    Widget_Data *wd = elm_widget_data_get(obj);
879    if (!wd) return;
880    if (wd->val == val) return;
881    wd->val = val;
882    if (wd->val < wd->val_min) wd->val = wd->val_min;
883    if (wd->val > wd->val_max) wd->val = wd->val_max;
884    _val_set(obj);
885    _units_set(obj);
886    _indicator_set(obj);
887 }
888
889 EAPI double
890 elm_slider_value_get(const Evas_Object *obj)
891 {
892    ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
893    Widget_Data *wd = elm_widget_data_get(obj);
894    if (!wd) return 0.0;
895    return wd->val;
896 }
897
898 EAPI void
899 elm_slider_inverted_set(Evas_Object *obj, Eina_Bool inverted)
900 {
901    ELM_CHECK_WIDTYPE(obj, widtype);
902    Widget_Data *wd = elm_widget_data_get(obj);
903    if (!wd) return;
904    inverted = !!inverted;
905    if (wd->inverted == inverted) return;
906    wd->inverted = inverted;
907    if (wd->inverted)
908      edje_object_signal_emit(wd->slider, "elm,state,inverted,on", "elm");
909    else
910      edje_object_signal_emit(wd->slider, "elm,state,inverted,off", "elm");
911    edje_object_message_signal_process(wd->slider);
912    _val_set(obj);
913    _units_set(obj);
914    _indicator_set(obj);
915 }
916
917 EAPI Eina_Bool
918 elm_slider_inverted_get(const Evas_Object *obj)
919 {
920    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
921    Widget_Data *wd = elm_widget_data_get(obj);
922    if (!wd) return EINA_FALSE;
923    return wd->inverted;
924 }
925
926 EAPI void
927 elm_slider_indicator_format_function_set(Evas_Object *obj, const char *(*func)(double val), void (*free_func)(const char *str))
928 {
929    ELM_CHECK_WIDTYPE(obj, widtype);
930    Widget_Data *wd = elm_widget_data_get(obj);
931    if (!wd) return;
932    wd->indicator_format_func = func;
933    wd->indicator_format_free = free_func;
934    _indicator_set(obj);
935 }
936
937 EAPI void
938 elm_slider_units_format_function_set(Evas_Object *obj, const char *(*func)(double val), void (*free_func)(const char *str))
939 {
940    ELM_CHECK_WIDTYPE(obj, widtype);
941    Widget_Data *wd = elm_widget_data_get(obj);
942    if (!wd) return;
943    wd->units_format_func = func;
944    wd->units_format_free = free_func;
945    _indicator_set(obj);
946 }
947
948 EAPI void
949 elm_slider_end_set(Evas_Object *obj, Evas_Object *end)
950 {
951    _content_set_hook(obj, "end", end);
952 }
953
954 EAPI Evas_Object *
955 elm_slider_end_unset(Evas_Object *obj)
956 {
957    return _content_unset_hook(obj, "end");
958 }
959
960 EAPI Evas_Object *
961 elm_slider_end_get(const Evas_Object *obj)
962 {
963    return _content_get_hook(obj, "end");
964 }
965
966 EAPI void
967 elm_slider_indicator_show_set(Evas_Object *obj, Eina_Bool show)
968 {
969    ELM_CHECK_WIDTYPE(obj, widtype);
970    Widget_Data *wd = elm_widget_data_get(obj);
971    if (show) {
972         wd->indicator_show = EINA_TRUE;
973         edje_object_signal_emit(wd->slider, "elm,state,val,show", "elm");
974    }
975    else {
976         wd->indicator_show = EINA_FALSE;
977         edje_object_signal_emit(wd->slider, "elm,state,val,hide", "elm");
978    }
979 }
980
981 EAPI Eina_Bool
982 elm_slider_indicator_show_get(const Evas_Object *obj)
983 {
984    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
985    Widget_Data *wd = elm_widget_data_get(obj);
986    if (!wd) return EINA_FALSE;
987    return wd->indicator_show;
988 }
989