Merge "[genlist] disable previous item focus"
[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) return;
606    if (!strcmp(part, "icon"))
607      _icon_set(obj, content);
608    else if (!strcmp(part, "end"))
609      _end_set(obj, content);
610 }
611
612 static Evas_Object *
613 _content_get_hook(const Evas_Object *obj, const char *part)
614 {
615    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
616    Widget_Data *wd;
617    if (!part) return NULL;
618    wd = elm_widget_data_get(obj);
619    if (!wd) return NULL;
620    if (!strcmp(part, "icon"))
621      return wd->icon;
622    else if (!strcmp(part, "end"))
623      return wd->end;
624    return NULL;
625 }
626
627 static Evas_Object *
628 _content_unset_hook(Evas_Object *obj, const char *part)
629 {
630    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
631    if (!part) return NULL;
632    if (!strcmp(part, "icon"))
633      return _icon_unset(obj);
634    else if (!strcmp(part, "end"))
635      return _end_unset(obj);
636    return NULL;
637 }
638
639 EAPI Evas_Object *
640 elm_slider_add(Evas_Object *parent)
641 {
642    Evas_Object *obj;
643    Evas *e;
644    Widget_Data *wd;
645
646    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
647
648    ELM_SET_WIDTYPE(widtype, "slider");
649    elm_widget_type_set(obj, "slider");
650    elm_widget_sub_object_add(parent, obj);
651    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
652    elm_widget_data_set(obj, wd);
653    elm_widget_del_hook_set(obj, _del_hook);
654    elm_widget_theme_hook_set(obj, _theme_hook);
655    elm_widget_disable_hook_set(obj, _disable_hook);
656    elm_widget_can_focus_set(obj, EINA_TRUE);
657    elm_widget_event_hook_set(obj, _event_hook);
658    elm_widget_text_set_hook_set(obj, _elm_slider_label_set);
659    elm_widget_text_get_hook_set(obj, _elm_slider_label_get);
660    elm_widget_content_set_hook_set(obj, _content_set_hook);
661    elm_widget_content_get_hook_set(obj, _content_get_hook);
662    elm_widget_content_unset_hook_set(obj, _content_unset_hook);
663
664    wd->horizontal = EINA_TRUE;
665    wd->indicator_show = EINA_TRUE;
666    wd->feed_cnt = 0;
667    wd->val = 0.0;
668    wd->val_min = 0.0;
669    wd->val_max = 1.0;
670
671    wd->slider = edje_object_add(e);
672    _elm_theme_object_set(obj, wd->slider, "slider", "horizontal", "default");
673    elm_widget_resize_object_set(obj, wd->slider);
674    edje_object_signal_callback_add(wd->slider, "drag", "*", _drag, obj);
675    edje_object_signal_callback_add(wd->slider, "drag,start", "*", _drag_start, obj);
676    edje_object_signal_callback_add(wd->slider, "drag,stop", "*", _drag_stop, obj);
677    edje_object_signal_callback_add(wd->slider, "drag,step", "*", _drag_step, obj);
678    edje_object_signal_callback_add(wd->slider, "drag,page", "*", _drag_stop, obj);
679    //   edje_object_signal_callback_add(wd->slider, "drag,set", "*", _drag_stop, obj);
680    edje_object_part_drag_value_set(wd->slider, "elm.dragable.slider", 0.0, 0.0);
681
682    wd->spacer = evas_object_rectangle_add(e);
683    evas_object_color_set(wd->spacer, 0, 0, 0, 0);
684    evas_object_pass_events_set(wd->spacer, EINA_TRUE);
685    elm_widget_sub_object_add(obj, wd->spacer);
686    edje_object_part_swallow(wd->slider, "elm.swallow.bar", wd->spacer);
687    evas_object_event_callback_add(wd->spacer, EVAS_CALLBACK_MOUSE_DOWN, _spacer_cb, obj);
688    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
689
690    _mirrored_set(obj, elm_widget_mirrored_get(obj));
691    _sizing_eval(obj);
692
693    // TODO: convert Elementary to subclassing of Evas_Smart_Class
694    // TODO: and save some bytes, making descriptions per-class and not instance!
695    evas_object_smart_callbacks_descriptions_set(obj, _signals);
696    return obj;
697 }
698
699 EAPI void
700 elm_slider_label_set(Evas_Object *obj, const char *label)
701 {
702    _elm_slider_label_set(obj, NULL, label);
703 }
704
705 EAPI const char *
706 elm_slider_label_get(const Evas_Object *obj)
707 {
708    return _elm_slider_label_get(obj, NULL);
709 }
710
711 EAPI void
712 elm_slider_icon_set(Evas_Object *obj, Evas_Object *icon)
713 {
714    _content_set_hook(obj, "icon", icon);
715 }
716
717 EAPI Evas_Object *
718 elm_slider_icon_unset(Evas_Object *obj)
719 {
720    return _content_unset_hook(obj, "icon");
721 }
722
723 EAPI Evas_Object *
724 elm_slider_icon_get(const Evas_Object *obj)
725 {
726    return _content_get_hook(obj, "icon");
727 }
728
729 EAPI void
730 elm_slider_span_size_set(Evas_Object *obj, Evas_Coord size)
731 {
732    ELM_CHECK_WIDTYPE(obj, widtype);
733    Widget_Data *wd = elm_widget_data_get(obj);
734    if (!wd) return;
735    if (wd->size == size) return;
736    wd->size = size;
737    if (wd->horizontal)
738      evas_object_size_hint_min_set(wd->spacer, (double)wd->size * elm_widget_scale_get(obj) * _elm_config->scale, 1);
739    else
740      evas_object_size_hint_min_set(wd->spacer, 1, (double)wd->size * elm_widget_scale_get(obj) * _elm_config->scale);
741    if (wd->indicator_show)
742      edje_object_signal_emit(wd->slider, "elm,state,val,show", "elm");
743    else
744      edje_object_signal_emit(wd->slider, "elm,state,val,hide", "elm");
745    edje_object_part_swallow(wd->slider, "elm.swallow.bar", wd->spacer);
746    _sizing_eval(obj);
747 }
748
749 EAPI Evas_Coord
750 elm_slider_span_size_get(const Evas_Object *obj)
751 {
752    ELM_CHECK_WIDTYPE(obj, widtype) 0;
753    Widget_Data *wd = elm_widget_data_get(obj);
754    if (!wd) return 0;
755    return wd->size;
756 }
757
758 EAPI void
759 elm_slider_unit_format_set(Evas_Object *obj, const char *units)
760 {
761    ELM_CHECK_WIDTYPE(obj, widtype);
762    Widget_Data *wd = elm_widget_data_get(obj);
763    if (!wd) return;
764    eina_stringshare_replace(&wd->units, units);
765    if (units)
766      {
767         edje_object_signal_emit(wd->slider, "elm,state,units,visible", "elm");
768         edje_object_message_signal_process(wd->slider);
769      }
770    else
771      {
772         edje_object_signal_emit(wd->slider, "elm,state,units,hidden", "elm");
773         edje_object_message_signal_process(wd->slider);
774      }
775    _units_set(obj);
776    _sizing_eval(obj);
777 }
778
779 EAPI const char *
780 elm_slider_unit_format_get(const Evas_Object *obj)
781 {
782    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
783    Widget_Data *wd = elm_widget_data_get(obj);
784    if (!wd) return NULL;
785    return wd->units;
786 }
787
788 EAPI void
789 elm_slider_indicator_format_set(Evas_Object *obj, const char *indicator)
790 {
791    ELM_CHECK_WIDTYPE(obj, widtype);
792    Widget_Data *wd = elm_widget_data_get(obj);
793    if (!wd) return;
794    eina_stringshare_replace(&wd->indicator, indicator);
795    _indicator_set(obj);
796 }
797
798 EAPI const char *
799 elm_slider_indicator_format_get(const Evas_Object *obj)
800 {
801    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
802    Widget_Data *wd = elm_widget_data_get(obj);
803    if (!wd) return NULL;
804    return wd->indicator;
805 }
806
807 EAPI void
808 elm_slider_horizontal_set(Evas_Object *obj, Eina_Bool horizontal)
809 {
810    ELM_CHECK_WIDTYPE(obj, widtype);
811    Widget_Data *wd = elm_widget_data_get(obj);
812    if (!wd) return;
813    horizontal = !!horizontal;
814    if (wd->horizontal == horizontal) return;
815    wd->horizontal = horizontal;
816    _theme_hook(obj);
817 }
818
819 EAPI Eina_Bool
820 elm_slider_horizontal_get(const Evas_Object *obj)
821 {
822    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
823    Widget_Data *wd = elm_widget_data_get(obj);
824    if (!wd) return EINA_FALSE;
825    return wd->horizontal;
826 }
827
828 EAPI void
829 elm_slider_min_max_set(Evas_Object *obj, double min, double max)
830 {
831    ELM_CHECK_WIDTYPE(obj, widtype);
832    Widget_Data *wd = elm_widget_data_get(obj);
833    if (!wd) return;
834    if ((wd->val_min == min) && (wd->val_max == max)) return;
835    wd->val_min = min;
836    wd->val_max = max;
837    if (wd->val < wd->val_min) wd->val = wd->val_min;
838    if (wd->val > wd->val_max) wd->val = wd->val_max;
839    _val_set(obj);
840    _units_set(obj);
841    _indicator_set(obj);
842 }
843
844 EAPI void
845 elm_slider_min_max_get(const Evas_Object *obj, double *min, double *max)
846 {
847    if (min) *min = 0.0;
848    if (max) *max = 0.0;
849    ELM_CHECK_WIDTYPE(obj, widtype);
850    Widget_Data *wd = elm_widget_data_get(obj);
851    if (!wd) return;
852    if (min) *min = wd->val_min;
853    if (max) *max = wd->val_max;
854 }
855
856 EAPI void
857 elm_slider_value_set(Evas_Object *obj, double val)
858 {
859    ELM_CHECK_WIDTYPE(obj, widtype);
860    Widget_Data *wd = elm_widget_data_get(obj);
861    if (!wd) return;
862    if (wd->val == val) return;
863    wd->val = val;
864    if (wd->val < wd->val_min) wd->val = wd->val_min;
865    if (wd->val > wd->val_max) wd->val = wd->val_max;
866    edje_object_signal_emit(wd->slider, "elm,state,drag", "elm");
867    _val_set(obj);
868    _units_set(obj);
869    _indicator_set(obj);
870 }
871
872 EAPI double
873 elm_slider_value_get(const Evas_Object *obj)
874 {
875    ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
876    Widget_Data *wd = elm_widget_data_get(obj);
877    if (!wd) return 0.0;
878    return wd->val;
879 }
880
881 EAPI void
882 elm_slider_inverted_set(Evas_Object *obj, Eina_Bool inverted)
883 {
884    ELM_CHECK_WIDTYPE(obj, widtype);
885    Widget_Data *wd = elm_widget_data_get(obj);
886    if (!wd) return;
887    inverted = !!inverted;
888    if (wd->inverted == inverted) return;
889    wd->inverted = inverted;
890    if (wd->inverted)
891      edje_object_signal_emit(wd->slider, "elm,state,inverted,on", "elm");
892    else
893      edje_object_signal_emit(wd->slider, "elm,state,inverted,off", "elm");
894    edje_object_message_signal_process(wd->slider);
895    _val_set(obj);
896    _units_set(obj);
897    _indicator_set(obj);
898 }
899
900 EAPI Eina_Bool
901 elm_slider_inverted_get(const Evas_Object *obj)
902 {
903    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
904    Widget_Data *wd = elm_widget_data_get(obj);
905    if (!wd) return EINA_FALSE;
906    return wd->inverted;
907 }
908
909 EAPI void
910 elm_slider_indicator_format_function_set(Evas_Object *obj, const char *(*func)(double val), void (*free_func)(const char *str))
911 {
912    ELM_CHECK_WIDTYPE(obj, widtype);
913    Widget_Data *wd = elm_widget_data_get(obj);
914    if (!wd) return;
915    wd->indicator_format_func = func;
916    wd->indicator_format_free = free_func;
917    _indicator_set(obj);
918 }
919
920 EAPI void
921 elm_slider_units_format_function_set(Evas_Object *obj, const char *(*func)(double val), void (*free_func)(const char *str))
922 {
923    ELM_CHECK_WIDTYPE(obj, widtype);
924    Widget_Data *wd = elm_widget_data_get(obj);
925    if (!wd) return;
926    wd->units_format_func = func;
927    wd->units_format_free = free_func;
928    _indicator_set(obj);
929 }
930
931 EAPI void
932 elm_slider_end_set(Evas_Object *obj, Evas_Object *end)
933 {
934    _content_set_hook(obj, "end", end);
935 }
936
937 EAPI Evas_Object *
938 elm_slider_end_unset(Evas_Object *obj)
939 {
940    return _content_unset_hook(obj, "end");
941 }
942
943 EAPI Evas_Object *
944 elm_slider_end_get(const Evas_Object *obj)
945 {
946    return _content_get_hook(obj, "end");
947 }
948
949 EAPI void
950 elm_slider_indicator_show_set(Evas_Object *obj, Eina_Bool show)
951 {
952    ELM_CHECK_WIDTYPE(obj, widtype);
953    Widget_Data *wd = elm_widget_data_get(obj);
954    if (show) {
955         wd->indicator_show = EINA_TRUE;
956         edje_object_signal_emit(wd->slider, "elm,state,val,show", "elm");
957    }
958    else {
959         wd->indicator_show = EINA_FALSE;
960         edje_object_signal_emit(wd->slider, "elm,state,val,hide", "elm");
961    }
962 }
963
964 EAPI Eina_Bool
965 elm_slider_indicator_show_get(const Evas_Object *obj)
966 {
967    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
968    Widget_Data *wd = elm_widget_data_get(obj);
969    if (!wd) return EINA_FALSE;
970    return wd->indicator_show;
971 }
972