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