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