93f0243abd89bbeff2b73a9f3e91c29ca6fa2a0a
[framework/uifw/elementary.git] / src / lib / elm_slider.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup Slider Slider
6  * @ingroup Elementary
7  *
8  * The slider adds a dragable “slider” widget for selecting the value of
9  * something within a range.
10  *
11  * Signals that you can add callbacks for are:
12  *
13  * changed - Whenever the slider value is changed by the user.
14  *
15  * delay,changed - A short time after the value is changed by the user.
16  * This will be called only when the user stops dragging for a very short
17  * period or when they release their finger/mouse, so it avoids possibly
18  * expensive reactions to the value change.
19  *
20  * slider,drag,start - dragging the slider indicator around has started
21  *
22  * slider,drag,stop - dragging the slider indicator around has stopped
23  *
24  * A slider can be horizontal or vertical. It can contain an Icon and has a
25  * primary label as well as a units label (that is formatted with floating
26  * point values and thus accepts a printf-style format string, like
27  * “%1.2f units”. There is also an indicator string that may be somewhere
28  * else (like on the slider itself) that also accepts a format string like
29  * units. Label, Icon Unit and Indicator strings/objects are optional.
30  *
31  * A slider may be inverted which means values invert, with high vales being
32  * on the left or top and low values on the right or bottom (as opposed to
33  * normally being low on the left or top and high on the bottom and right).
34  *
35  * The slider should have its minimum and maximum values set by the
36  * application with  elm_slider_min_max_set() and value should also be set by
37  * the application before use with  elm_slider_value_set(). The span of the
38  * slider is its length (horizontally or vertically). This will be scaled by
39  * the object or applications scaling factor. At any point code can query the
40  * slider for its value with elm_slider_value_get().
41  */
42
43 typedef struct _Widget_Data Widget_Data;
44
45 struct _Widget_Data
46 {
47    Evas_Object *slider;
48    Evas_Object *icon;
49    Evas_Object *end;
50    Evas_Object *spacer;
51    Evas_Object *indicator_label;
52    const char *label;
53    const char *units;
54    const char *indicator;
55    const char *(*indicator_format_func)(double val);
56    Eina_Bool horizontal : 1;
57    Eina_Bool inverted : 1;
58    Eina_Bool indicator_show : 1;
59    int feed_cnt;
60    double val, val_min, val_max;
61    Ecore_Timer *delay;
62    Evas_Coord size;
63 };
64
65 #define ELM_SLIDER_INVERTED_FACTOR (-1.0)
66
67 static const char *widtype = NULL;
68 static void _del_hook(Evas_Object *obj);
69 static void _theme_hook(Evas_Object *obj);
70 static void _disable_hook(Evas_Object *obj);
71 static void _sizing_eval(Evas_Object *obj);
72 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
73 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
74 static void _units_set(Evas_Object *obj);
75 static void _val_set(Evas_Object *obj);
76 static void _indicator_set(Evas_Object *obj);
77 static void _on_focus_hook(void *data, Evas_Object *obj);
78 static void _drag_up(void *data, Evas_Object *obj,
79                     const char *emission, const char *source);
80 static void _drag_down(void *data, Evas_Object *obj,
81                     const char *emission, const char *source);
82 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
83                              Evas_Callback_Type type, void *event_info);
84 static void _spacer_cb(void *data, Evas * e, Evas_Object * obj, void *event_info);
85
86 static const char SIG_CHANGED[] = "changed";
87 static const char SIG_DELAY_CHANGED[] = "delay,changed";
88 static const char SIG_DRAG_START[] = "slider,drag,start";
89 static const char SIG_DRAG_STOP[] = "slider,drag,stop";
90 static const Evas_Smart_Cb_Description _signals[] = {
91   {SIG_CHANGED, ""},
92   {SIG_DELAY_CHANGED, ""},
93   {SIG_DRAG_START, ""},
94   {SIG_DRAG_STOP, ""},
95   {NULL, NULL}
96 };
97
98 static Eina_Bool
99 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
100 {
101    Evas_Event_Mouse_Wheel *mev;
102    Evas_Event_Key_Down *ev;
103    Widget_Data *wd;
104
105    wd = elm_widget_data_get(obj);
106    if (!wd) return EINA_FALSE;
107    if (elm_widget_disabled_get(obj)) return EINA_FALSE;
108
109    if (type == EVAS_CALLBACK_KEY_DOWN) goto key_down;
110    else if (type != EVAS_CALLBACK_MOUSE_WHEEL) return EINA_FALSE;
111
112    mev = event_info;
113    if (mev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
114    if (elm_widget_disabled_get(obj)) return EINA_FALSE;
115
116    if (mev->z < 0) _drag_up(obj, NULL, NULL, NULL);
117    else _drag_down(obj, NULL, NULL, NULL);
118    mev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
119    return EINA_TRUE;
120
121   key_down:
122    ev = event_info;
123    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
124    if (elm_widget_disabled_get(obj)) return EINA_FALSE;
125    if ((!strcmp(ev->keyname, "Left"))
126        || (!strcmp(ev->keyname, "KP_Left")))
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 if ((!strcmp(ev->keyname, "Right"))
135             || (!strcmp(ev->keyname, "KP_Right")))
136      {
137         if (!wd->horizontal) return EINA_FALSE;
138         if (!wd->inverted) _drag_up(obj, NULL, NULL, NULL);
139         else _drag_down(obj, NULL, NULL, NULL);
140         ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
141         return EINA_TRUE;
142      }
143    else if ((!strcmp(ev->keyname, "Up")) || (!strcmp(ev->keyname, "KP_Up")))
144      {
145         if (wd->horizontal) return EINA_FALSE;
146         if (wd->inverted) _drag_up(obj, NULL, NULL, NULL);
147         else _drag_down(obj, NULL, NULL, NULL);
148         ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
149         return EINA_TRUE;
150      }
151    else if ((!strcmp(ev->keyname, "Down")) || (!strcmp(ev->keyname, "KP_Down")))
152      {
153         if (wd->horizontal) return EINA_FALSE;
154         if (wd->inverted) _drag_down(obj, NULL, NULL, NULL);
155         else _drag_up(obj, NULL, NULL, NULL);
156         ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
157         return EINA_TRUE;
158      }
159    else return EINA_FALSE;
160 }
161
162 static void
163 _del_hook(Evas_Object *obj)
164 {
165    Widget_Data *wd = elm_widget_data_get(obj);
166    if (!wd) return;
167    if (wd->label) eina_stringshare_del(wd->label);
168    if (wd->indicator) eina_stringshare_del(wd->units);
169    if (wd->delay) ecore_timer_del(wd->delay);
170    free(wd);
171 }
172
173 static void
174 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
175 {
176    Widget_Data *wd = elm_widget_data_get(obj);
177    if (!wd) return;
178    if (elm_widget_focus_get(obj))
179      {
180         edje_object_signal_emit(wd->slider, "elm,action,focus", "elm");
181         evas_object_focus_set(wd->slider, EINA_TRUE);
182      }
183    else
184      {
185         edje_object_signal_emit(wd->slider, "elm,action,unfocus", "elm");
186         evas_object_focus_set(wd->slider, EINA_FALSE);
187      }
188 }
189
190 static void
191 _theme_hook(Evas_Object *obj)
192 {
193    Evas_Coord indicator_width;
194    Widget_Data *wd = elm_widget_data_get(obj);
195    if (!wd) return;
196    if (wd->horizontal)
197      _elm_theme_object_set(obj, wd->slider, "slider", "horizontal", elm_widget_style_get(obj));
198    else
199      _elm_theme_object_set(obj, wd->slider, "slider", "vertical", elm_widget_style_get(obj));
200    if (elm_widget_disabled_get(data))
201      edje_object_signal_emit(wd->slider, "elm,state,disabled", "elm");
202    else
203      edje_object_signal_emit(wd->slider, "elm,state,enabled", "elm");
204    if (wd->icon)
205      {
206         edje_object_part_swallow(wd->slider, "elm.swallow.content", wd->icon);
207         edje_object_signal_emit(wd->slider, "elm,state,icon,visible", "elm");
208      }
209    if (wd->end)
210      edje_object_signal_emit(wd->slider, "elm,state,end,visible", "elm");
211    else
212      edje_object_signal_emit(wd->slider, "elm,state,end,hidden", "elm");
213    if (wd->label)
214      {
215         edje_object_part_text_set(wd->slider, "elm.text", wd->label);
216         edje_object_signal_emit(wd->slider, "elm,state,text,visible", "elm");
217      }
218    
219    if (wd->units)
220      edje_object_signal_emit(wd->slider, "elm,state,units,visible", "elm");
221    
222    if (wd->horizontal)
223      evas_object_size_hint_min_set(wd->spacer, (double)wd->size * elm_widget_scale_get(obj) * _elm_config->scale, 1);
224    else
225      evas_object_size_hint_min_set(wd->spacer, 1, (double)wd->size * elm_widget_scale_get(obj) * _elm_config->scale);
226    
227    if (wd->inverted)
228       edje_object_signal_emit(wd->slider, "elm,state,inverted,on", "elm");
229    
230    edje_object_part_swallow(wd->slider, "elm.swallow.bar", wd->spacer);
231
232    edje_object_part_swallow(wd->slider, "elm.indicator", wd->indicator_label);
233    edje_object_part_geometry_get(wd->slider, "elm.dragable.slider", NULL, NULL, &indicator_width, NULL);
234    elm_label_wrap_width_set(wd->indicator_label, indicator_width);
235    elm_label_text_align_set(wd->indicator_label, "center");
236    elm_label_ellipsis_set(wd->indicator_label, EINA_TRUE);
237    evas_object_show(wd->indicator_label);
238
239    _units_set(obj);
240    _indicator_set(obj);
241    edje_object_message_signal_process(wd->slider);
242    edje_object_scale_set(wd->slider, elm_widget_scale_get(obj) * _elm_config->scale);
243    _val_set(obj);
244    _sizing_eval(obj);
245 }
246
247 static void
248 _disable_hook(Evas_Object *obj)
249 {
250    Widget_Data *wd = elm_widget_data_get(obj);
251    if (!wd) return;
252    if (elm_widget_disabled_get(obj))
253       edje_object_signal_emit(wd->slider, "elm,state,disabled", "elm");
254    else
255       edje_object_signal_emit(wd->slider, "elm,state,enabled", "elm");
256 }
257
258 static void
259 _sizing_eval(Evas_Object *obj)
260 {   
261    Widget_Data *wd = elm_widget_data_get(obj);
262    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
263    if (!wd) return;
264    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
265    edje_object_size_min_restricted_calc(wd->slider, &minw, &minh, minw, minh);
266    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
267    evas_object_size_hint_min_set(obj, minw, minh);
268    evas_object_size_hint_max_set(obj, maxw, maxh);
269 }
270
271 static void
272 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
273 {
274    Widget_Data *wd = elm_widget_data_get(data);
275    if (!wd) return;
276    if ((obj != wd->icon) && (obj != wd->end)) return;
277    _sizing_eval(data);
278 }
279
280 static void
281 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
282 {
283    Widget_Data *wd = elm_widget_data_get(obj);
284    Evas_Object *sub = event_info;
285    if (!wd) return;
286    if (sub == wd->icon)
287      {
288         edje_object_signal_emit(wd->slider, "elm,state,icon,hidden", "elm");
289         evas_object_event_callback_del_full (sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
290         wd->icon = NULL;
291         edje_object_message_signal_process(wd->slider);
292         _sizing_eval(obj);
293      }
294    if (sub == wd->end)
295      {
296         edje_object_signal_emit(wd->slider, "elm,state,end,hidden", "elm");
297         evas_object_event_callback_del_full(sub,
298                                             EVAS_CALLBACK_CHANGED_SIZE_HINTS,
299                                             _changed_size_hints, obj);
300         wd->end = NULL;
301         edje_object_message_signal_process(wd->slider);
302         _sizing_eval(obj);
303      }
304 }
305
306 static Eina_Bool
307 _delay_change(void *data)
308 {
309    Widget_Data *wd = elm_widget_data_get(data);
310    if (!wd) return ECORE_CALLBACK_CANCEL;
311    wd->delay = NULL;
312    evas_object_smart_callback_call(data, SIG_DELAY_CHANGED, NULL);
313    return ECORE_CALLBACK_CANCEL;
314 }
315
316 static void
317 _val_fetch(Evas_Object *obj)
318 {
319    Widget_Data *wd = elm_widget_data_get(obj);
320    double posx = 0.0, posy = 0.0, pos = 0.0, val;
321    if (!wd) return;
322    edje_object_part_drag_value_get(wd->slider, "elm.dragable.slider",&posx, &posy);
323    if (wd->horizontal) pos = posx;
324    else pos = posy;
325    if (wd->inverted) pos = 1.0 - pos;
326    val = (pos * (wd->val_max - wd->val_min)) + wd->val_min;
327    if (val != wd->val)
328      {
329         wd->val = val;
330         evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
331         if (wd->delay) ecore_timer_del(wd->delay);
332         wd->delay = ecore_timer_add(0.2, _delay_change, obj);
333      }
334 }
335
336 static void
337 _val_set(Evas_Object *obj)
338 {
339    Widget_Data *wd = elm_widget_data_get(obj);
340    double pos;
341    if (!wd) return;
342    if (wd->val_max > wd->val_min)
343      pos = (wd->val - wd->val_min) / (wd->val_max - wd->val_min);
344    else
345      pos = 0.0;
346    if (pos < 0.0) pos = 0.0;
347    else if (pos > 1.0) pos = 1.0;
348    if (wd->inverted) pos = 1.0 - pos;
349    edje_object_part_drag_value_set(wd->slider, "elm.dragable.slider", pos, pos);
350 }
351
352 static void
353 _units_set(Evas_Object *obj)
354 {
355    Widget_Data *wd = elm_widget_data_get(obj);
356    if (!wd) return;
357    if (wd->units)
358      {
359         char buf[1024];
360
361         snprintf(buf, sizeof(buf), wd->units, wd->val);
362         edje_object_part_text_set(wd->slider, "elm.units", buf);
363      }
364    else
365      edje_object_part_text_set(wd->slider, "elm.units", NULL);
366 }
367
368 static void
369 _indicator_set(Evas_Object *obj)
370 {
371    Widget_Data *wd = elm_widget_data_get(obj);
372    if (!wd) return;
373    if (wd->indicator_format_func)
374      {
375         const char *buf;
376         buf = wd->indicator_format_func(wd->val);
377         elm_label_label_set(wd->indicator_label, buf);
378      }
379    else if (wd->indicator)
380      {
381         char buf[1024];
382         snprintf(buf, sizeof(buf), wd->indicator, wd->val);
383         elm_label_label_set(wd->indicator_label, buf);
384      }
385    else
386      elm_label_label_set(wd->indicator_label, NULL);
387 }
388
389 static void
390 _drag(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
391 {
392    Widget_Data *wd = elm_widget_data_get((Evas_Object*)data);
393    if (elm_widget_disabled_get(data)) return;
394    edje_object_signal_emit(wd->slider, "elm,state,drag", "elm");
395    edje_object_message_signal_process(wd->slider);
396    _val_fetch(data);
397    _units_set(data);
398    _indicator_set(data);
399 }
400
401 static void
402 _drag_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
403 {
404    Evas_Coord indicator_width;
405    Widget_Data *wd = elm_widget_data_get((Evas_Object*)data);
406    if (elm_widget_disabled_get(data)) return;
407    _val_fetch(data);
408    evas_object_smart_callback_call(data, SIG_DRAG_START, NULL);
409    edje_object_signal_emit(wd->slider, "elm,state,drag", "elm");
410    edje_object_message_signal_process(wd->slider); 
411    elm_object_style_set(wd->indicator_label,"slider_move");
412    edje_object_part_geometry_get(wd->slider, "popup", NULL, NULL, &indicator_width, NULL);
413    elm_label_wrap_width_set(wd->indicator_label, indicator_width);
414    _units_set(data);
415    _indicator_set(data);
416    elm_widget_scroll_hold_push(data);
417 }
418
419 static void
420 _drag_stop(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
421 {
422    Evas_Coord indicator_width;
423    Widget_Data *wd = elm_widget_data_get((Evas_Object*)data);
424    if (elm_widget_disabled_get(data)) return;
425    _val_fetch(data);
426    evas_object_smart_callback_call(data, SIG_DRAG_STOP, NULL);
427    elm_object_style_set(wd->indicator_label,"slider_normal");
428    edje_object_part_geometry_get(wd->slider, "button0", NULL, NULL, &indicator_width, NULL);
429    elm_label_wrap_width_set(wd->indicator_label, indicator_width);
430    _units_set(data);
431    _indicator_set(data);
432    elm_widget_scroll_hold_pop(data);
433 }
434
435 static void
436 _drag_step(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
437 {
438    if (elm_widget_disabled_get(data)) return;
439    _val_fetch(data);
440    _units_set(data);
441    _indicator_set(data);
442 }
443
444 static void
445 _drag_up(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
446 {
447    double step;
448    Widget_Data *wd;
449    if (elm_widget_disabled_get(data)) return;
450
451    wd = elm_widget_data_get(data);
452    step = 0.05;
453
454    if (wd->inverted) step *= ELM_SLIDER_INVERTED_FACTOR;
455
456    edje_object_part_drag_step(wd->slider, "elm.dragable.slider", step, step);
457 }
458
459 static void
460 _drag_down(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
461 {
462    double step;
463    Widget_Data *wd;
464    if (elm_widget_disabled_get(data)) return;
465
466    wd = elm_widget_data_get(data);
467    step = -0.05;
468
469    if (wd->inverted) step *= ELM_SLIDER_INVERTED_FACTOR;
470
471    edje_object_part_drag_step(wd->slider, "elm.dragable.slider", step, step);
472 }
473
474 static void
475 _spacer_cb(void *data, Evas *e, Evas_Object *obj __UNUSED__, void *event_info)
476 {
477    Widget_Data *wd = elm_widget_data_get(data);
478    Evas_Event_Mouse_Down *ev = event_info;
479    Evas_Coord x, y, w, h;
480    double button_x, button_y;   
481    if (elm_widget_disabled_get(data)) return;
482
483    evas_object_geometry_get(wd->spacer, &x, &y, &w, &h);
484    edje_object_part_drag_value_get(wd->slider, "elm.dragable.slider", &button_x, &button_y);
485    if (wd->horizontal)
486      {
487         button_x = ((double)ev->output.x - (double)x) / (double)w;
488         if (button_x > 1) button_x = 1;
489         if (button_x < 0) button_x = 0;
490      }
491    else 
492      {
493         button_y = ((double)ev->output.y - (double)y) / (double)h;
494         if (button_y > 1) button_y = 1;
495         if (button_y < 0) button_y = 0;
496      }
497    edje_object_part_drag_value_set(wd->slider, "elm.dragable.slider", button_x, button_y);
498    evas_event_feed_mouse_cancel(e, 0, NULL);
499    wd->feed_cnt ++;
500    if(wd->feed_cnt < 3)
501      evas_event_feed_mouse_down(e, 1, EVAS_BUTTON_NONE, 0, NULL);  
502    wd->feed_cnt = 0;
503 }
504
505 /**
506  * Add a new slider to the parent
507  *
508  * @param parent The parent object
509  * @return The new object or NULL if it cannot be created
510  *
511  * @ingroup Slider
512  */
513 EAPI Evas_Object *
514 elm_slider_add(Evas_Object *parent)
515 {
516    Evas_Object *obj;
517    Evas *e;
518    Widget_Data *wd;
519    Evas_Coord indicator_width;
520
521    EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
522
523    wd = ELM_NEW(Widget_Data);
524    e = evas_object_evas_get(parent);
525    if (!e) return NULL;
526    obj = elm_widget_add(e);
527    ELM_SET_WIDTYPE(widtype, "slider");
528    elm_widget_type_set(obj, "slider");
529    elm_widget_sub_object_add(parent, obj);
530    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
531    elm_widget_data_set(obj, wd);
532    elm_widget_del_hook_set(obj, _del_hook);
533    elm_widget_theme_hook_set(obj, _theme_hook);
534    elm_widget_disable_hook_set(obj, _disable_hook);
535    elm_widget_can_focus_set(obj, EINA_TRUE);
536    elm_widget_event_hook_set(obj, _event_hook);
537
538    wd->horizontal = EINA_TRUE;
539    wd->indicator_show = EINA_TRUE;
540    wd->feed_cnt = 0;
541    wd->val = 0.0;
542    wd->val_min = 0.0;
543    wd->val_max = 1.0;
544
545    wd->slider = edje_object_add(e);
546    _elm_theme_object_set(obj, wd->slider, "slider", "horizontal", "default");
547    elm_widget_resize_object_set(obj, wd->slider);
548    edje_object_signal_callback_add(wd->slider, "drag", "*", _drag, obj);
549    edje_object_signal_callback_add(wd->slider, "drag,start", "*", _drag_start, obj);
550    edje_object_signal_callback_add(wd->slider, "drag,stop", "*", _drag_stop, obj);
551    edje_object_signal_callback_add(wd->slider, "drag,step", "*", _drag_step, obj);
552    edje_object_signal_callback_add(wd->slider, "drag,page", "*", _drag_stop, obj);
553 //   edje_object_signal_callback_add(wd->slider, "drag,set", "*", _drag_stop, obj);
554    edje_object_part_drag_value_set(wd->slider, "elm.dragable.slider", 0.0, 0.0);
555
556    wd->spacer = evas_object_rectangle_add(e);
557    evas_object_color_set(wd->spacer, 0, 0, 0, 0);
558    evas_object_pass_events_set(wd->spacer, EINA_TRUE);
559    elm_widget_sub_object_add(obj, wd->spacer);
560    edje_object_part_swallow(wd->slider, "elm.swallow.bar", wd->spacer);
561
562    wd->indicator_label = elm_label_add(obj);
563    elm_object_style_set(wd->indicator_label,"slider_normal");
564    elm_widget_sub_object_add(obj, wd->indicator_label);
565    edje_object_part_swallow(wd->slider, "elm.indicator", wd->indicator_label);
566    edje_object_part_geometry_get(wd->slider, "button0", NULL, NULL, &indicator_width, NULL);
567    elm_label_wrap_width_set(wd->indicator_label, indicator_width);
568    elm_label_text_align_set(wd->indicator_label, "center");
569    elm_label_ellipsis_set(wd->indicator_label, EINA_TRUE);
570    evas_object_show(wd->indicator_label);
571
572    evas_object_event_callback_add(wd->spacer, EVAS_CALLBACK_MOUSE_DOWN, _spacer_cb, obj);
573    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
574
575    _sizing_eval(obj);
576
577    // TODO: convert Elementary to subclassing of Evas_Smart_Class
578    // TODO: and save some bytes, making descriptions per-class and not instance!
579    evas_object_smart_callbacks_descriptions_set(obj, _signals);
580    return obj;
581 }
582
583 /**
584  * Set the label of the slider
585  *
586  * @param obj The slider object
587  * @param label The text label string in UTF-8
588  *
589  * @ingroup Slider
590  */
591 EAPI void
592 elm_slider_label_set(Evas_Object *obj, const char *label)
593 {
594    ELM_CHECK_WIDTYPE(obj, widtype);
595    Widget_Data *wd = elm_widget_data_get(obj);
596    if (!wd) return;
597    eina_stringshare_replace(&wd->label, label);
598    if (label)
599      {
600         edje_object_signal_emit(wd->slider, "elm,state,text,visible", "elm");
601         edje_object_message_signal_process(wd->slider);
602      }
603    else
604      {
605         edje_object_signal_emit(wd->slider, "elm,state,text,hidden", "elm");
606         edje_object_message_signal_process(wd->slider);
607      }
608    edje_object_part_text_set(wd->slider, "elm.text", label);
609    _sizing_eval(obj);
610 }
611
612 /**
613  * Get the label of the slider
614  *
615  * @param obj The slider object
616  * @return The text label string in UTF-8
617  *
618  * @ingroup Slider
619  */
620 EAPI const char *
621 elm_slider_label_get(const Evas_Object *obj)
622 {
623    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
624    Widget_Data *wd = elm_widget_data_get(obj);
625    if (!wd) return NULL;
626    return wd->label;
627 }
628
629 /**
630  * Set the icon object (leftmost widget) of the slider object.
631  *
632  * Once the icon object is set, a previously set one will be deleted.
633  * If you want to keep that old content object, use the
634  * elm_slider_icon_unset() function.
635  *
636  * @param obj The slider object
637  * @param icon The icon object
638  *
639  * @note If the object being set does not have minimum size hints set,
640  * it won't get properly displayed.
641  *
642  * @ingroup Slider
643  */
644 EAPI void
645 elm_slider_icon_set(Evas_Object *obj, Evas_Object *icon)
646 {
647    ELM_CHECK_WIDTYPE(obj, widtype);
648    Widget_Data *wd = elm_widget_data_get(obj);
649    if (!wd) return;
650    if (wd->icon == icon) return;
651    if (wd->icon) evas_object_del(wd->icon);
652    wd->icon = icon;
653    if (icon)
654      {
655         elm_widget_sub_object_add(obj, icon);
656         evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,_changed_size_hints, obj);
657         edje_object_part_swallow(wd->slider, "elm.swallow.icon", icon);
658         edje_object_signal_emit(wd->slider, "elm,state,icon,visible", "elm");
659         edje_object_message_signal_process(wd->slider);
660      }
661    _sizing_eval(obj);
662 }
663
664 /**
665  * Unset the leftmost widget of the slider, unparenting and
666  * returning it.
667  *
668  * @param obj The slider object
669  * @return the previously set icon sub-object of this slider, on
670  * success.
671  *
672  * @see elm_slider_icon_set()
673  *
674  * @ingroup Slider
675  */
676 EAPI Evas_Object *
677 elm_slider_icon_unset(Evas_Object *obj)
678 {
679    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
680    Widget_Data *wd = elm_widget_data_get(obj);
681    Evas_Object *ret = NULL;
682    if (!wd) return NULL;
683    if (wd->icon)
684      {
685         elm_widget_sub_object_del(obj, wd->icon);
686         ret = wd->icon;
687         edje_object_part_unswallow(wd->slider, wd->icon);
688         edje_object_signal_emit(wd->slider, "elm,state,icon,hidden", "elm");
689         wd->icon = NULL;
690         _sizing_eval(obj);
691      }
692    return ret;
693 }
694
695 /**
696  * Get the icon object of the slider object. This object is owned by
697  * the scrolled entry and should not be modified.
698  *
699  * @param obj The slider object
700  * @return The icon object
701  *
702  * @ingroup Slider
703  */
704 EAPI Evas_Object *
705 elm_slider_icon_get(const Evas_Object *obj)
706 {
707    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
708    Widget_Data *wd = elm_widget_data_get(obj);
709    if (!wd) return NULL;
710    return wd->icon;
711 }
712
713 /**
714  * Set the length of the dragable region of the slider
715  *
716  * This sets the minimum width or height (depending on orientation) of the
717  * area of the slider that allows the slider to be dragged around. This in
718  * turn affects the objects minimum size (along with icon label and unit
719  * text). Note that this will also get multiplied by the scale factor.
720  *
721  * @param obj The slider object
722  * @param size The length of the slider area
723  *
724  * @ingroup Slider
725  */
726 EAPI void
727 elm_slider_span_size_set(Evas_Object *obj, Evas_Coord size)
728 {
729    ELM_CHECK_WIDTYPE(obj, widtype);
730    Widget_Data *wd = elm_widget_data_get(obj);
731    if (!wd) return;
732    if (wd->size == size) return;
733    wd->size = size;
734    if (wd->horizontal)
735      evas_object_size_hint_min_set(wd->spacer, (double)wd->size * elm_widget_scale_get(obj) * _elm_config->scale, 1);
736    else
737      evas_object_size_hint_min_set(wd->spacer, 1, (double)wd->size * elm_widget_scale_get(obj) * _elm_config->scale);
738    if (wd->indicator_show)
739        edje_object_signal_emit(wd->slider, "elm,state,val,show", "elm");
740    else
741        edje_object_signal_emit(wd->slider, "elm,state,val,hide", "elm");
742    edje_object_part_swallow(wd->slider, "elm.swallow.bar", wd->spacer);
743    _sizing_eval(obj);
744 }
745
746 /**
747  * Get the length of the dragable region of the slider
748  *
749  * This gets the minimum width or height (depending on orientation) of
750  * the area of the slider that allows the slider to be dragged
751  * around. Note that this will also get multiplied by the scale
752  * factor.
753  *
754  * @param obj The slider object
755  * @return The length of the slider area
756  *
757  * @ingroup Slider
758  */
759 EAPI Evas_Coord
760 elm_slider_span_size_get(const Evas_Object *obj)
761 {
762    ELM_CHECK_WIDTYPE(obj, widtype) 0;
763    Widget_Data *wd = elm_widget_data_get(obj);
764    if (!wd) return 0;
765    return wd->size;
766 }
767
768 /**
769  * Set the format string of the unit area
770  *
771  * If NULL, this disabls the unit area display. If not it sets the format
772  * string for the unit text. The unit text is provided a floating point
773  * value, so the unit text can display up to 1 floating point value. Note that
774  * this is optional. Use a format string such as "%1.2f meters" for example.
775  *
776  * @param obj The slider object
777  * @param units The format string for the units display
778  *
779  * @ingroup Slider
780  */
781 EAPI void
782 elm_slider_unit_format_set(Evas_Object *obj, const char *units)
783 {
784    ELM_CHECK_WIDTYPE(obj, widtype);
785    Widget_Data *wd = elm_widget_data_get(obj);
786    if (!wd) return;
787    eina_stringshare_replace(&wd->units, units);
788    if (units)
789      {
790         edje_object_signal_emit(wd->slider, "elm,state,units,visible", "elm");
791         edje_object_message_signal_process(wd->slider);
792      }
793    else
794      {
795         edje_object_signal_emit(wd->slider, "elm,state,units,hidden", "elm");
796         edje_object_message_signal_process(wd->slider);
797      }
798    _units_set(obj);
799    _sizing_eval(obj);
800 }
801
802 /**
803  * Get the format string for the unit area
804  *
805  * The slider may also display a value (the value of the slider) somewhere
806  * (for example above the slider knob that is dragged around). This sets the
807  * format string for this. See elm_slider_unit_format_set() for more
808  * information on how this works.
809  *
810  * @param obj The slider object
811  * @return The format string for the unit display.
812  *
813  * @ingroup Slider
814  */
815 EAPI const char *
816 elm_slider_unit_format_get(const Evas_Object *obj)
817 {
818    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
819    Widget_Data *wd = elm_widget_data_get(obj);
820    if (!wd) return NULL;
821    return wd->units;
822 }
823
824 /**
825  * Set the format string for the indicator area
826  *
827  * The slider may also display a value (the value of the slider) somewhere
828  * (for example above the slider knob that is dragged around). This sets the
829  * format string for this. See elm_slider_unit_format_set() for more
830  * information on how this works.
831  *
832  * @param obj The slider object
833  * @param indicator The format string for the indicator display
834  *
835  * @ingroup Slider
836  */
837 EAPI void
838 elm_slider_indicator_format_set(Evas_Object *obj, const char *indicator)
839 {
840    ELM_CHECK_WIDTYPE(obj, widtype);
841    Widget_Data *wd = elm_widget_data_get(obj);
842    if (!wd) return;
843    eina_stringshare_replace(&wd->indicator, indicator);
844    _indicator_set(obj);
845 }
846
847 /**
848  * Get the format string for the indicator area
849  *
850  * The slider may also display a value (the value of the slider) somewhere
851  * (for example above the slider knob that is dragged around). This sets the
852  * format string for this. See elm_slider_indicator_format_set() for more
853  * information on how this works.
854  *
855  * @param obj The slider object
856  * @return The format string for the indicator display.
857  *
858  * @ingroup Slider
859  */
860 EAPI const char *
861 elm_slider_indicator_format_get(const Evas_Object *obj)
862 {
863    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
864    Widget_Data *wd = elm_widget_data_get(obj);
865    if (!wd) return NULL;
866    return wd->indicator;
867 }
868
869 /**
870  * Set orientation of the slider
871  *
872  * @param obj The slider object
873  * @param horizontal If set, the slider will be horizontal
874  *
875  * @ingroup Slider
876  */
877 EAPI void
878 elm_slider_horizontal_set(Evas_Object *obj, Eina_Bool horizontal)
879 {
880    ELM_CHECK_WIDTYPE(obj, widtype);
881    Widget_Data *wd = elm_widget_data_get(obj);
882    if (!wd) return;
883    horizontal = !!horizontal;
884    if (wd->horizontal == horizontal) return;
885    wd->horizontal = horizontal;
886    _theme_hook(obj);
887 }
888
889 /**
890  * Get orientation of the slider
891  *
892  * @param obj The slider object
893  * @return If @c EINA_TRUE the slider will be horizontal, else it is
894  *         vertical.
895  * @ingroup Slider
896  */
897 EAPI Eina_Bool
898 elm_slider_horizontal_get(const Evas_Object *obj)
899 {
900    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
901    Widget_Data *wd = elm_widget_data_get(obj);
902    if (!wd) return EINA_FALSE;
903    return wd->horizontal;
904 }
905
906 /**
907  * Set the minimum and maximum values for the slider
908  *
909  * Maximum mut be greater than minimum.
910  *
911  * @param obj The slider object
912  * @param min The minimum value
913  * @param max The maximum value
914  *
915  * @ingroup Slider
916  */
917 EAPI void
918 elm_slider_min_max_set(Evas_Object *obj, double min, double max)
919 {
920    ELM_CHECK_WIDTYPE(obj, widtype);
921    Widget_Data *wd = elm_widget_data_get(obj);
922    if (!wd) return;
923    if ((wd->val_min == min) && (wd->val_max == max)) return;
924    wd->val_min = min;
925    wd->val_max = max;
926    if (wd->val < wd->val_min) wd->val = wd->val_min;
927    if (wd->val > wd->val_max) wd->val = wd->val_max;
928    _val_set(obj);
929    _units_set(obj);
930    _indicator_set(obj);
931 }
932
933 /**
934  * Get the minimum and maximum values for the slider
935  *
936  * @param obj The slider object
937  * @param min The pointer to store minimum value, may be @c NULL.
938  * @param max The pointer to store maximum value, may be @c NULL.
939  *
940  * @ingroup Slider
941  */
942 EAPI void
943 elm_slider_min_max_get(const Evas_Object *obj, double *min, double *max)
944 {
945    if (min) *min = 0.0;
946    if (max) *max = 0.0;
947    ELM_CHECK_WIDTYPE(obj, widtype);
948    Widget_Data *wd = elm_widget_data_get(obj);
949    if (!wd) return;
950    if (min) *min = wd->val_min;
951    if (max) *max = wd->val_max;
952 }
953
954 /**
955  * Set the value the slider indicates
956  *
957  * @param obj The slider object
958  * @param val The value (must be between min and max for the slider)
959  *
960  * @ingroup Slider
961  */
962 EAPI void
963 elm_slider_value_set(Evas_Object *obj, double val)
964 {
965    ELM_CHECK_WIDTYPE(obj, widtype);
966    Widget_Data *wd = elm_widget_data_get(obj);
967    if (!wd) return;
968    if (wd->val == val) return;
969    wd->val = val;
970    if (wd->val < wd->val_min) wd->val = wd->val_min;
971    if (wd->val > wd->val_max) wd->val = wd->val_max;
972    edje_object_signal_emit(wd->slider, "elm,state,drag", "elm");
973    _val_set(obj);
974    _units_set(obj);
975    _indicator_set(obj);
976 }
977
978 /**
979  * Get the value the slider has
980  *
981  * @param obj The slider object
982  * @return The value of the slider
983  *
984  * @ingroup Slider
985  */
986 EAPI double
987 elm_slider_value_get(const Evas_Object *obj)
988 {
989    ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
990    Widget_Data *wd = elm_widget_data_get(obj);
991    if (!wd) return 0.0;
992    return wd->val;
993 }
994
995 /**
996  * Invert the slider display
997  *
998  * Normally the slider will display and interpret values from low to high
999  * and when horizontal that is left to right. When vertical that is top
1000  * to bottom. This inverts this (so from right to left or bottom to top) if
1001  * inverted is set to 1.
1002  *
1003  * @param obj The slider object
1004  * @param inverted The inverted flag. 1 == inverted, 0 == normal
1005  *
1006  * @ingroup Slider
1007  */
1008 EAPI void
1009 elm_slider_inverted_set(Evas_Object *obj, Eina_Bool inverted)
1010 {
1011    ELM_CHECK_WIDTYPE(obj, widtype);
1012    Widget_Data *wd = elm_widget_data_get(obj);
1013    if (!wd) return;
1014    inverted = !!inverted;
1015    if (wd->inverted == inverted) return;
1016    wd->inverted = inverted;
1017    if (wd->inverted)
1018      edje_object_signal_emit(wd->slider, "elm,state,inverted,on", "elm");
1019    else
1020      edje_object_signal_emit(wd->slider, "elm,state,inverted,off", "elm");
1021    edje_object_message_signal_process(wd->slider);
1022    _val_set(obj);
1023    _units_set(obj);
1024    _indicator_set(obj);
1025 }
1026
1027 /**
1028  * Get if the slider display is inverted (backwards)
1029  *
1030  * @param obj The slider object
1031  * @return If @c EINA_TRUE the slider will be inverted.
1032  * @ingroup Slider
1033  */
1034 EAPI Eina_Bool
1035 elm_slider_inverted_get(const Evas_Object *obj)
1036 {
1037    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1038    Widget_Data *wd = elm_widget_data_get(obj);
1039    if (!wd) return EINA_FALSE;
1040    return wd->inverted;
1041 }
1042
1043 /**
1044  * Set the format function pointer for the inducator area
1045  *
1046  * Set the callback function to format the indicator string.
1047  * See elm_slider_indicator_format_set() for more info on how this works.
1048  *
1049  * @param obj The slider object
1050  * @param indicator The format string for the indicator display
1051  * @param func The indicator format function
1052  *
1053  * @ingroup Slider
1054  */
1055 EAPI void
1056 elm_slider_indicator_format_function_set(Evas_Object *obj, const char *(*func)(double val))
1057 {
1058    ELM_CHECK_WIDTYPE(obj, widtype);
1059    Widget_Data *wd = elm_widget_data_get(obj);
1060    if (!wd) return;
1061    wd->indicator_format_func = func;
1062    _indicator_set(obj);
1063 }
1064
1065 /**
1066  * Set the end object (rightmost widget) of the slider object.
1067  *
1068  * Once the end object is set, a previously set one will be deleted.
1069  * If you want to keep that old content object, use the
1070  * elm_button_end_unset() function.
1071  *
1072  * @param obj The slider object
1073  * @param end The end object
1074  *
1075  * @note If the object being set does not have minimum size hints set,
1076  * it won't get properly displayed.
1077  *
1078  * @ingroup Slider
1079  */
1080 EAPI void
1081 elm_slider_end_set(Evas_Object *obj, Evas_Object *end)
1082 {
1083    ELM_CHECK_WIDTYPE(obj, widtype);
1084    Widget_Data *wd = elm_widget_data_get(obj);
1085    if (!wd) return;
1086    if (wd->end == end) return;
1087    if (wd->end) evas_object_del(wd->end);
1088    wd->end = end;
1089    if (end)
1090      {
1091         elm_widget_sub_object_add(obj, end);
1092         evas_object_event_callback_add(end, EVAS_CALLBACK_CHANGED_SIZE_HINTS,_changed_size_hints, obj);
1093         edje_object_part_swallow(wd->slider, "elm.swallow.end", end);
1094         edje_object_signal_emit(wd->slider, "elm,state,end,visible", "elm");
1095         edje_object_message_signal_process(wd->slider);
1096      }
1097    _sizing_eval(obj);
1098 }
1099
1100 /**
1101  * Unset the rightmost widget of the slider, unparenting and
1102  * returning it.
1103  *
1104  * @param obj The slider object
1105  * @return the previously set end sub-object of this slider, on
1106  * success.
1107  *
1108  * @see elm_slider_end_set()
1109  *
1110  * @ingroup Slider
1111  */
1112 EAPI Evas_Object *
1113 elm_slider_end_unset(Evas_Object *obj)
1114 {
1115    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1116    Widget_Data *wd = elm_widget_data_get(obj);
1117    Evas_Object *ret = NULL;
1118    if (!wd) return NULL;
1119    if (wd->end)
1120      {
1121         elm_widget_sub_object_del(obj, wd->end);
1122         ret = wd->end;
1123         edje_object_part_unswallow(wd->slider, wd->end);
1124         edje_object_signal_emit(wd->slider, "elm,state,end,hidden", "elm");
1125         wd->end = NULL;
1126         _sizing_eval(obj);
1127      }
1128    return ret;
1129 }
1130
1131 /**
1132  * Get the end icon object of the slider object. This object is owned
1133  * by the scrolled entry and should not be modified.
1134  *
1135  * @param obj The slider object
1136  * @return The end icon object
1137  *
1138  * @ingroup Slider
1139  */
1140 EAPI Evas_Object *
1141 elm_slider_end_get(const Evas_Object *obj)
1142 {
1143    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1144    Widget_Data *wd = elm_widget_data_get(obj);
1145    if (!wd) return NULL;
1146    return wd->end;
1147 }
1148
1149 /**
1150  * Set whether to the slider indicator (augmented knob) at all.
1151  *
1152  * @param obj The slider object
1153  * @param show @c EINA_TRUE will make it show it, @c EINA_FALSE will
1154  * let the knob alwayes at default size.
1155  *
1156  * @note It will conflict with elm_slider_indicator_format_set(), if
1157  * you wanted those effects.
1158  *
1159  * @ingroup Slider
1160  */
1161 EAPI void
1162 elm_slider_indicator_show_set(Evas_Object *obj, Eina_Bool show)
1163 {
1164    ELM_CHECK_WIDTYPE(obj, widtype);
1165    Widget_Data *wd = elm_widget_data_get(obj);
1166    if (show) {
1167       wd->indicator_show = EINA_TRUE;
1168       edje_object_signal_emit(wd->slider, "elm,state,val,show", "elm");
1169    }
1170    else {
1171       wd->indicator_show = EINA_FALSE;
1172       edje_object_signal_emit(wd->slider, "elm,state,val,hide", "elm");
1173    }
1174 }
1175
1176 /**
1177  * Get the state of indicator in the slider (if it's being shown or
1178  * not).
1179  *
1180  * @param obj The slider object
1181  * @return @c EINA_TRUE if the indicator is being shown, @c EINA_FALSE
1182  * otherwise.
1183  *
1184  *  @ingroup Slider
1185  */
1186 EAPI Eina_Bool
1187 elm_slider_indicator_show_get(const Evas_Object *obj)
1188 {
1189    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1190    Widget_Data *wd = elm_widget_data_get(obj);
1191    if (!wd) return EINA_FALSE;
1192    return wd->indicator_show;
1193 }