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