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