Elm: Use 'canvas' API and structure rather than 'output' API and
[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  *
11  * A slider can be horizontal or vertical. It can contain an Icon and has a
12  * primary label as well as a units label (that is formatted with floating
13  * point values and thus accepts a printf-style format string, like
14  * “%1.2f units”. There is also an indicator string that may be somewhere
15  * else (like on the slider itself) that also accepts a format string like
16  * units. Label, Icon Unit and Indicator strings/objects are optional.
17  *
18  * A slider may be inverted which means values invert, with high vales being
19  * on the left or top and low values on the right or bottom (as opposed to
20  * normally being low on the left or top and high on the bottom and right).
21  *
22  * The slider should have its minimum and maximum values set by the
23  * application with  elm_slider_min_max_set() and value should also be set by
24  * the application before use with  elm_slider_value_set(). The span of the
25  * slider is its length (horizontally or vertically). This will be scaled by
26  * the object or applications scaling factor. At any point code can query the
27  * slider for its value with elm_slider_value_get().
28  *
29  * Signals that you can add callbacks for are:
30  *
31  * "changed" - Whenever the slider value is changed by the user.
32  * "slider,drag,start" - dragging the slider indicator around has started
33  * "slider,drag,stop" - dragging the slider indicator around has stopped
34  * "delay,changed" - A short time after the value is changed by the user.
35  *                   This will be called only when the user stops dragging for
36  *                   a very short period or when they release their
37  *                   finger/mouse, so it avoids possibly expensive reactions to
38  *                   the value change.
39  */
40
41 typedef struct _Widget_Data Widget_Data;
42
43 struct _Widget_Data
44 {
45    Evas_Object *slider;
46    Evas_Object *icon;
47    Evas_Object *end;
48    Evas_Object *spacer;
49    const char *label;
50    const char *units;
51    const char *indicator;
52    const char *(*indicator_format_func)(double val);
53    Eina_Bool horizontal : 1;
54    Eina_Bool inverted : 1;
55    Eina_Bool indicator_show : 1;
56    double val, val_min, val_max;
57    Ecore_Timer *delay;
58    Evas_Coord size;
59 };
60
61 #define ELM_SLIDER_INVERTED_FACTOR (-1.0)
62
63 static const char *widtype = NULL;
64 static void _del_hook(Evas_Object *obj);
65 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
66 static void _theme_hook(Evas_Object *obj);
67 static void _disable_hook(Evas_Object *obj);
68 static void _sizing_eval(Evas_Object *obj);
69 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
70 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
71 static void _units_set(Evas_Object *obj);
72 static void _val_set(Evas_Object *obj);
73 static void _indicator_set(Evas_Object *obj);
74 static void _on_focus_hook(void *data, Evas_Object *obj);
75 static void _drag_up(void *data, Evas_Object *obj,
76                     const char *emission, const char *source);
77 static void _drag_down(void *data, Evas_Object *obj,
78                     const char *emission, const char *source);
79 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
80                              Evas_Callback_Type type, void *event_info);
81 static void _spacer_cb(void *data, Evas * e, Evas_Object * obj, void *event_info);
82
83 static const char SIG_CHANGED[] = "changed";
84 static const char SIG_DELAY_CHANGED[] = "delay,changed";
85 static const char SIG_DRAG_START[] = "slider,drag,start";
86 static const char SIG_DRAG_STOP[] = "slider,drag,stop";
87 static const Evas_Smart_Cb_Description _signals[] = {
88   {SIG_CHANGED, ""},
89   {SIG_DELAY_CHANGED, ""},
90   {SIG_DRAG_START, ""},
91   {SIG_DRAG_STOP, ""},
92   {NULL, NULL}
93 };
94
95 static Eina_Bool
96 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
97 {
98    Evas_Event_Mouse_Wheel *mev;
99    Evas_Event_Key_Down *ev;
100    Widget_Data *wd;
101
102    wd = elm_widget_data_get(obj);
103    if (!wd) return EINA_FALSE;
104
105    if (type == EVAS_CALLBACK_KEY_DOWN) goto key_down;
106    else if (type != EVAS_CALLBACK_MOUSE_WHEEL) return EINA_FALSE;
107
108    mev = event_info;
109    if (mev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
110    if (elm_widget_disabled_get(obj)) return EINA_FALSE;
111
112    if (mev->z < 0) _drag_up(obj, NULL, NULL, NULL);
113    else _drag_down(obj, NULL, NULL, NULL);
114    mev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
115    return EINA_TRUE;
116
117   key_down:
118    ev = event_info;
119    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
120    if (elm_widget_disabled_get(obj)) return EINA_FALSE;
121    if ((!strcmp(ev->keyname, "Left"))
122        || (!strcmp(ev->keyname, "KP_Left")))
123      {
124         if (!wd->horizontal) return EINA_FALSE;
125         if (!wd->inverted) _drag_down(obj, NULL, NULL, NULL);
126         else _drag_up(obj, NULL, NULL, NULL);
127         ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
128         return EINA_TRUE;
129      }
130    else if ((!strcmp(ev->keyname, "Right"))
131             || (!strcmp(ev->keyname, "KP_Right")))
132      {
133         if (!wd->horizontal) return EINA_FALSE;
134         if (!wd->inverted) _drag_up(obj, NULL, NULL, NULL);
135         else _drag_down(obj, NULL, NULL, NULL);
136         ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
137         return EINA_TRUE;
138      }
139    else if ((!strcmp(ev->keyname, "Up")) || (!strcmp(ev->keyname, "KP_Up")))
140      {
141         if (wd->horizontal) return EINA_FALSE;
142         if (wd->inverted) _drag_up(obj, NULL, NULL, NULL);
143         else _drag_down(obj, NULL, NULL, NULL);
144         ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
145         return EINA_TRUE;
146      }
147    else if ((!strcmp(ev->keyname, "Down")) || (!strcmp(ev->keyname, "KP_Down")))
148      {
149         if (wd->horizontal) return EINA_FALSE;
150         if (wd->inverted) _drag_down(obj, NULL, NULL, NULL);
151         else _drag_up(obj, NULL, NULL, NULL);
152         ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
153         return EINA_TRUE;
154      }
155    else return EINA_FALSE;
156 }
157
158 static void
159 _del_hook(Evas_Object *obj)
160 {
161    Widget_Data *wd = elm_widget_data_get(obj);
162    if (!wd) return;
163    if (wd->label) eina_stringshare_del(wd->label);
164    if (wd->indicator) eina_stringshare_del(wd->units);
165    if (wd->delay) ecore_timer_del(wd->delay);
166    free(wd);
167 }
168
169 static void
170 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
171 {
172    Widget_Data *wd = elm_widget_data_get(obj);
173    if (!wd) return;
174    if (elm_widget_focus_get(obj))
175      {
176         edje_object_signal_emit(wd->slider, "elm,action,focus", "elm");
177         evas_object_focus_set(wd->slider, EINA_TRUE);
178      }
179    else
180      {
181         edje_object_signal_emit(wd->slider, "elm,action,unfocus", "elm");
182         evas_object_focus_set(wd->slider, EINA_FALSE);
183      }
184 }
185
186 static void
187 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
188 {
189    Widget_Data *wd = elm_widget_data_get(obj);
190    if (!wd) return;
191    edje_object_mirrored_set(wd->slider, rtl);
192 }
193
194 static void
195 _theme_hook(Evas_Object *obj)
196 {
197    Widget_Data *wd = elm_widget_data_get(obj);
198    if (!wd) return;
199    _elm_widget_mirrored_reload(obj);
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_freeze_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_freeze_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->canvas.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->canvas.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    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
501
502    ELM_SET_WIDTYPE(widtype, "slider");
503    elm_widget_type_set(obj, "slider");
504    elm_widget_sub_object_add(parent, obj);
505    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
506    elm_widget_data_set(obj, wd);
507    elm_widget_del_hook_set(obj, _del_hook);
508    elm_widget_theme_hook_set(obj, _theme_hook);
509    elm_widget_disable_hook_set(obj, _disable_hook);
510    elm_widget_can_focus_set(obj, EINA_TRUE);
511    elm_widget_event_hook_set(obj, _event_hook);
512
513    wd->horizontal = EINA_TRUE;
514    wd->indicator_show = EINA_TRUE;
515    wd->val = 0.0;
516    wd->val_min = 0.0;
517    wd->val_max = 1.0;
518
519    wd->slider = edje_object_add(e);
520    _elm_theme_object_set(obj, wd->slider, "slider", "horizontal", "default");
521    elm_widget_resize_object_set(obj, wd->slider);
522    edje_object_signal_callback_add(wd->slider, "drag", "*", _drag, obj);
523    edje_object_signal_callback_add(wd->slider, "drag,start", "*", _drag_start, obj);
524    edje_object_signal_callback_add(wd->slider, "drag,stop", "*", _drag_stop, obj);
525    edje_object_signal_callback_add(wd->slider, "drag,step", "*", _drag_step, obj);
526    edje_object_signal_callback_add(wd->slider, "drag,page", "*", _drag_stop, obj);
527    //   edje_object_signal_callback_add(wd->slider, "drag,set", "*", _drag_stop, obj);
528    edje_object_part_drag_value_set(wd->slider, "elm.dragable.slider", 0.0, 0.0);
529
530    wd->spacer = evas_object_rectangle_add(e);
531    evas_object_color_set(wd->spacer, 0, 0, 0, 0);
532    evas_object_pass_events_set(wd->spacer, EINA_TRUE);
533    elm_widget_sub_object_add(obj, wd->spacer);
534    edje_object_part_swallow(wd->slider, "elm.swallow.bar", wd->spacer);
535    evas_object_event_callback_add(wd->spacer, EVAS_CALLBACK_MOUSE_DOWN, _spacer_cb, obj);
536    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
537
538    _mirrored_set(obj, elm_widget_mirrored_get(obj));
539    _sizing_eval(obj);
540
541    // TODO: convert Elementary to subclassing of Evas_Smart_Class
542    // TODO: and save some bytes, making descriptions per-class and not instance!
543    evas_object_smart_callbacks_descriptions_set(obj, _signals);
544    return obj;
545 }
546
547 /**
548  * Set the label of the slider
549  *
550  * @param obj The slider object
551  * @param label The text label string in UTF-8
552  *
553  * @ingroup Slider
554  */
555 EAPI void
556 elm_slider_label_set(Evas_Object *obj, const char *label)
557 {
558    ELM_CHECK_WIDTYPE(obj, widtype);
559    Widget_Data *wd = elm_widget_data_get(obj);
560    if (!wd) return;
561    eina_stringshare_replace(&wd->label, label);
562    if (label)
563      {
564         edje_object_signal_emit(wd->slider, "elm,state,text,visible", "elm");
565         edje_object_message_signal_process(wd->slider);
566      }
567    else
568      {
569         edje_object_signal_emit(wd->slider, "elm,state,text,hidden", "elm");
570         edje_object_message_signal_process(wd->slider);
571      }
572    edje_object_part_text_set(wd->slider, "elm.text", label);
573    _sizing_eval(obj);
574 }
575
576 /**
577  * Get the label of the slider
578  *
579  * @param obj The slider object
580  * @return The text label string in UTF-8
581  *
582  * @ingroup Slider
583  */
584 EAPI const char *
585 elm_slider_label_get(const Evas_Object *obj)
586 {
587    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
588    Widget_Data *wd = elm_widget_data_get(obj);
589    if (!wd) return NULL;
590    return wd->label;
591 }
592
593 /**
594  * Set the icon object (leftmost widget) of the slider object.
595  *
596  * Once the icon object is set, a previously set one will be deleted.
597  * If you want to keep that old content object, use the
598  * elm_slider_icon_unset() function.
599  *
600  * @param obj The slider object
601  * @param icon The icon object
602  *
603  * @note If the object being set does not have minimum size hints set,
604  * it won't get properly displayed.
605  *
606  * @ingroup Slider
607  */
608 EAPI void
609 elm_slider_icon_set(Evas_Object *obj, Evas_Object *icon)
610 {
611    ELM_CHECK_WIDTYPE(obj, widtype);
612    Widget_Data *wd = elm_widget_data_get(obj);
613    if (!wd) return;
614    if (wd->icon == icon) return;
615    if (wd->icon) evas_object_del(wd->icon);
616    wd->icon = icon;
617    if (icon)
618      {
619         elm_widget_sub_object_add(obj, icon);
620         evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
621                                        _changed_size_hints, obj);
622         edje_object_part_swallow(wd->slider, "elm.swallow.icon", icon);
623         edje_object_signal_emit(wd->slider, "elm,state,icon,visible", "elm");
624         edje_object_message_signal_process(wd->slider);
625      }
626    _sizing_eval(obj);
627 }
628
629 /**
630  * Unset the leftmost widget of the slider, unparenting and
631  * returning it.
632  *
633  * @param obj The slider object
634  * @return the previously set icon sub-object of this slider, on
635  * success.
636  *
637  * @see elm_slider_icon_set()
638  *
639  * @ingroup Slider
640  */
641 EAPI Evas_Object *
642 elm_slider_icon_unset(Evas_Object *obj)
643 {
644    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
645    Widget_Data *wd = elm_widget_data_get(obj);
646    Evas_Object *ret = NULL;
647    if (!wd) return NULL;
648    if (wd->icon)
649      {
650         elm_widget_sub_object_del(obj, wd->icon);
651         ret = wd->icon;
652         edje_object_part_unswallow(wd->slider, wd->icon);
653         edje_object_signal_emit(wd->slider, "elm,state,icon,hidden", "elm");
654         wd->icon = NULL;
655         _sizing_eval(obj);
656      }
657    return ret;
658 }
659
660 /**
661  * Get the icon object of the slider object. This object is owned by
662  * the scrolled entry and should not be modified.
663  *
664  * @param obj The slider object
665  * @return The icon object
666  *
667  * @ingroup Slider
668  */
669 EAPI Evas_Object *
670 elm_slider_icon_get(const Evas_Object *obj)
671 {
672    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
673    Widget_Data *wd = elm_widget_data_get(obj);
674    if (!wd) return NULL;
675    return wd->icon;
676 }
677
678 /**
679  * Set the length of the dragable region of the slider
680  *
681  * This sets the minimum width or height (depending on orientation) of the
682  * area of the slider that allows the slider to be dragged around. This in
683  * turn affects the objects minimum size (along with icon label and unit
684  * text). Note that this will also get multiplied by the scale factor.
685  *
686  * @param obj The slider object
687  * @param size The length of the slider area
688  *
689  * @ingroup Slider
690  */
691 EAPI void
692 elm_slider_span_size_set(Evas_Object *obj, Evas_Coord size)
693 {
694    ELM_CHECK_WIDTYPE(obj, widtype);
695    Widget_Data *wd = elm_widget_data_get(obj);
696    if (!wd) return;
697    if (wd->size == size) return;
698    wd->size = size;
699    if (wd->horizontal)
700      evas_object_size_hint_min_set(wd->spacer, (double)wd->size * elm_widget_scale_get(obj) * _elm_config->scale, 1);
701    else
702      evas_object_size_hint_min_set(wd->spacer, 1, (double)wd->size * elm_widget_scale_get(obj) * _elm_config->scale);
703    if (wd->indicator_show)
704      edje_object_signal_emit(wd->slider, "elm,state,val,show", "elm");
705    else
706      edje_object_signal_emit(wd->slider, "elm,state,val,hide", "elm");
707    edje_object_part_swallow(wd->slider, "elm.swallow.bar", wd->spacer);
708    _sizing_eval(obj);
709 }
710
711 /**
712  * Get the length of the dragable region of the slider
713  *
714  * This gets the minimum width or height (depending on orientation) of
715  * the area of the slider that allows the slider to be dragged
716  * around. Note that this will also get multiplied by the scale
717  * factor.
718  *
719  * @param obj The slider object
720  * @return The length of the slider area
721  *
722  * @ingroup Slider
723  */
724 EAPI Evas_Coord
725 elm_slider_span_size_get(const Evas_Object *obj)
726 {
727    ELM_CHECK_WIDTYPE(obj, widtype) 0;
728    Widget_Data *wd = elm_widget_data_get(obj);
729    if (!wd) return 0;
730    return wd->size;
731 }
732
733 /**
734  * Set the format string of the unit area
735  *
736  * If NULL, this disabls the unit area display. If not it sets the format
737  * string for the unit text. The unit text is provided a floating point
738  * value, so the unit text can display up to 1 floating point value. Note that
739  * this is optional. Use a format string such as "%1.2f meters" for example.
740  *
741  * @param obj The slider object
742  * @param units The format string for the units display
743  *
744  * @ingroup Slider
745  */
746 EAPI void
747 elm_slider_unit_format_set(Evas_Object *obj, const char *units)
748 {
749    ELM_CHECK_WIDTYPE(obj, widtype);
750    Widget_Data *wd = elm_widget_data_get(obj);
751    if (!wd) return;
752    eina_stringshare_replace(&wd->units, units);
753    if (units)
754      {
755         edje_object_signal_emit(wd->slider, "elm,state,units,visible", "elm");
756         edje_object_message_signal_process(wd->slider);
757      }
758    else
759      {
760         edje_object_signal_emit(wd->slider, "elm,state,units,hidden", "elm");
761         edje_object_message_signal_process(wd->slider);
762      }
763    _units_set(obj);
764    _sizing_eval(obj);
765 }
766
767 /**
768  * Get the format string for the unit area
769  *
770  * The slider may also display a value (the value of the slider) somewhere
771  * (for example above the slider knob that is dragged around). This sets the
772  * format string for this. See elm_slider_unit_format_set() for more
773  * information on how this works.
774  *
775  * @param obj The slider object
776  * @return The format string for the unit display.
777  *
778  * @ingroup Slider
779  */
780 EAPI const char *
781 elm_slider_unit_format_get(const Evas_Object *obj)
782 {
783    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
784    Widget_Data *wd = elm_widget_data_get(obj);
785    if (!wd) return NULL;
786    return wd->units;
787 }
788
789 /**
790  * Set the format string for the indicator area
791  *
792  * The slider may also display a value (the value of the slider) somewhere
793  * (for example above the slider knob that is dragged around). This sets the
794  * format string for this. See elm_slider_unit_format_set() for more
795  * information on how this works.
796  *
797  * @param obj The slider object
798  * @param indicator The format string for the indicator display
799  *
800  * @ingroup Slider
801  */
802 EAPI void
803 elm_slider_indicator_format_set(Evas_Object *obj, const char *indicator)
804 {
805    ELM_CHECK_WIDTYPE(obj, widtype);
806    Widget_Data *wd = elm_widget_data_get(obj);
807    if (!wd) return;
808    eina_stringshare_replace(&wd->indicator, indicator);
809    _indicator_set(obj);
810 }
811
812 /**
813  * Get the format string for the indicator area
814  *
815  * The slider may also display a value (the value of the slider) somewhere
816  * (for example above the slider knob that is dragged around). This sets the
817  * format string for this. See elm_slider_indicator_format_set() for more
818  * information on how this works.
819  *
820  * @param obj The slider object
821  * @return The format string for the indicator display.
822  *
823  * @ingroup Slider
824  */
825 EAPI const char *
826 elm_slider_indicator_format_get(const Evas_Object *obj)
827 {
828    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
829    Widget_Data *wd = elm_widget_data_get(obj);
830    if (!wd) return NULL;
831    return wd->indicator;
832 }
833
834 /**
835  * Set orientation of the slider
836  *
837  * @param obj The slider object
838  * @param horizontal If set, the slider will be horizontal
839  *
840  * @ingroup Slider
841  */
842 EAPI void
843 elm_slider_horizontal_set(Evas_Object *obj, Eina_Bool horizontal)
844 {
845    ELM_CHECK_WIDTYPE(obj, widtype);
846    Widget_Data *wd = elm_widget_data_get(obj);
847    if (!wd) return;
848    horizontal = !!horizontal;
849    if (wd->horizontal == horizontal) return;
850    wd->horizontal = horizontal;
851    _theme_hook(obj);
852 }
853
854 /**
855  * Get orientation of the slider
856  *
857  * @param obj The slider object
858  * @return If @c EINA_TRUE the slider will be horizontal, else it is
859  *         vertical.
860  * @ingroup Slider
861  */
862 EAPI Eina_Bool
863 elm_slider_horizontal_get(const Evas_Object *obj)
864 {
865    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
866    Widget_Data *wd = elm_widget_data_get(obj);
867    if (!wd) return EINA_FALSE;
868    return wd->horizontal;
869 }
870
871 /**
872  * Set the minimum and maximum values for the slider
873  *
874  * Maximum mut be greater than minimum.
875  *
876  * @param obj The slider object
877  * @param min The minimum value
878  * @param max The maximum value
879  *
880  * @ingroup Slider
881  */
882 EAPI void
883 elm_slider_min_max_set(Evas_Object *obj, double min, double max)
884 {
885    ELM_CHECK_WIDTYPE(obj, widtype);
886    Widget_Data *wd = elm_widget_data_get(obj);
887    if (!wd) return;
888    if ((wd->val_min == min) && (wd->val_max == max)) return;
889    wd->val_min = min;
890    wd->val_max = max;
891    if (wd->val < wd->val_min) wd->val = wd->val_min;
892    if (wd->val > wd->val_max) wd->val = wd->val_max;
893    _val_set(obj);
894    _units_set(obj);
895    _indicator_set(obj);
896 }
897
898 /**
899  * Get the minimum and maximum values for the slider
900  *
901  * @param obj The slider object
902  * @param min The pointer to store minimum value, may be @c NULL.
903  * @param max The pointer to store maximum value, may be @c NULL.
904  *
905  * @ingroup Slider
906  */
907 EAPI void
908 elm_slider_min_max_get(const Evas_Object *obj, double *min, double *max)
909 {
910    if (min) *min = 0.0;
911    if (max) *max = 0.0;
912    ELM_CHECK_WIDTYPE(obj, widtype);
913    Widget_Data *wd = elm_widget_data_get(obj);
914    if (!wd) return;
915    if (min) *min = wd->val_min;
916    if (max) *max = wd->val_max;
917 }
918
919 /**
920  * Set the value the slider indicates
921  *
922  * @param obj The slider object
923  * @param val The value (must be between min and max for the slider)
924  *
925  * @ingroup Slider
926  */
927 EAPI void
928 elm_slider_value_set(Evas_Object *obj, double val)
929 {
930    ELM_CHECK_WIDTYPE(obj, widtype);
931    Widget_Data *wd = elm_widget_data_get(obj);
932    if (!wd) return;
933    if (wd->val == val) return;
934    wd->val = val;
935    if (wd->val < wd->val_min) wd->val = wd->val_min;
936    if (wd->val > wd->val_max) wd->val = wd->val_max;
937    _val_set(obj);
938    _units_set(obj);
939    _indicator_set(obj);
940 }
941
942 /**
943  * Get the value the slider has
944  *
945  * @param obj The slider object
946  * @return The value of the slider
947  *
948  * @ingroup Slider
949  */
950 EAPI double
951 elm_slider_value_get(const Evas_Object *obj)
952 {
953    ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
954    Widget_Data *wd = elm_widget_data_get(obj);
955    if (!wd) return 0.0;
956    return wd->val;
957 }
958
959 /**
960  * Invert the slider display
961  *
962  * Normally the slider will display and interpret values from low to high
963  * and when horizontal that is left to right. When vertical that is top
964  * to bottom. This inverts this (so from right to left or bottom to top) if
965  * inverted is set to 1.
966  *
967  * @param obj The slider object
968  * @param inverted The inverted flag. 1 == inverted, 0 == normal
969  *
970  * @ingroup Slider
971  */
972 EAPI void
973 elm_slider_inverted_set(Evas_Object *obj, Eina_Bool inverted)
974 {
975    ELM_CHECK_WIDTYPE(obj, widtype);
976    Widget_Data *wd = elm_widget_data_get(obj);
977    if (!wd) return;
978    inverted = !!inverted;
979    if (wd->inverted == inverted) return;
980    wd->inverted = inverted;
981    if (wd->inverted)
982      edje_object_signal_emit(wd->slider, "elm,state,inverted,on", "elm");
983    else
984      edje_object_signal_emit(wd->slider, "elm,state,inverted,off", "elm");
985    edje_object_message_signal_process(wd->slider);
986    _val_set(obj);
987    _units_set(obj);
988    _indicator_set(obj);
989 }
990
991 /**
992  * Get if the slider display is inverted (backwards)
993  *
994  * @param obj The slider object
995  * @return If @c EINA_TRUE the slider will be inverted.
996  * @ingroup Slider
997  */
998 EAPI Eina_Bool
999 elm_slider_inverted_get(const Evas_Object *obj)
1000 {
1001    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1002    Widget_Data *wd = elm_widget_data_get(obj);
1003    if (!wd) return EINA_FALSE;
1004    return wd->inverted;
1005 }
1006
1007 /**
1008  * Set the format function pointer for the inducator area
1009  *
1010  * Set the callback function to format the indicator string.
1011  * See elm_slider_indicator_format_set() for more info on how this works.
1012  *
1013  * @param obj The slider object
1014  * @param indicator The format string for the indicator display
1015  * @param func The indicator format function
1016  *
1017  * @ingroup Slider
1018  */
1019 EAPI void
1020 elm_slider_indicator_format_function_set(Evas_Object *obj, const char *(*func)(double val))
1021 {
1022    ELM_CHECK_WIDTYPE(obj, widtype);
1023    Widget_Data *wd = elm_widget_data_get(obj);
1024    if (!wd) return;
1025    wd->indicator_format_func = func;
1026    _indicator_set(obj);
1027 }
1028
1029 /**
1030  * Set the end object (rightmost widget) of the slider object.
1031  *
1032  * Once the end object is set, a previously set one will be deleted.
1033  * If you want to keep that old content object, use the
1034  * elm_button_end_unset() function.
1035  *
1036  * @param obj The slider object
1037  * @param end The end object
1038  *
1039  * @note If the object being set does not have minimum size hints set,
1040  * it won't get properly displayed.
1041  *
1042  * @ingroup Slider
1043  */
1044 EAPI void
1045 elm_slider_end_set(Evas_Object *obj, Evas_Object *end)
1046 {
1047    ELM_CHECK_WIDTYPE(obj, widtype);
1048    Widget_Data *wd = elm_widget_data_get(obj);
1049    if (!wd) return;
1050    if (wd->end == end) return;
1051    if (wd->end) evas_object_del(wd->end);
1052    wd->end = end;
1053    if (end)
1054      {
1055         elm_widget_sub_object_add(obj, end);
1056         evas_object_event_callback_add(end, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
1057                                        _changed_size_hints, obj);
1058         edje_object_part_swallow(wd->slider, "elm.swallow.end", end);
1059         edje_object_signal_emit(wd->slider, "elm,state,end,visible", "elm");
1060         edje_object_message_signal_process(wd->slider);
1061      }
1062    _sizing_eval(obj);
1063 }
1064
1065 /**
1066  * Unset the rightmost widget of the slider, unparenting and
1067  * returning it.
1068  *
1069  * @param obj The slider object
1070  * @return the previously set end sub-object of this slider, on
1071  * success.
1072  *
1073  * @see elm_slider_end_set()
1074  *
1075  * @ingroup Slider
1076  */
1077 EAPI Evas_Object *
1078 elm_slider_end_unset(Evas_Object *obj)
1079 {
1080    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1081    Widget_Data *wd = elm_widget_data_get(obj);
1082    Evas_Object *ret = NULL;
1083    if (!wd) return NULL;
1084    if (wd->end)
1085      {
1086         elm_widget_sub_object_del(obj, wd->end);
1087         ret = wd->end;
1088         edje_object_part_unswallow(wd->slider, wd->end);
1089         edje_object_signal_emit(wd->slider, "elm,state,end,hidden", "elm");
1090         wd->end = NULL;
1091         _sizing_eval(obj);
1092      }
1093    return ret;
1094 }
1095
1096 /**
1097  * Get the end icon object of the slider object. This object is owned
1098  * by the scrolled entry and should not be modified.
1099  *
1100  * @param obj The slider object
1101  * @return The end icon object
1102  *
1103  * @ingroup Slider
1104  */
1105 EAPI Evas_Object *
1106 elm_slider_end_get(const Evas_Object *obj)
1107 {
1108    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1109    Widget_Data *wd = elm_widget_data_get(obj);
1110    if (!wd) return NULL;
1111    return wd->end;
1112 }
1113
1114 /**
1115  * Set whether to the slider indicator (augmented knob) at all.
1116  *
1117  * @param obj The slider object
1118  * @param show @c EINA_TRUE will make it show it, @c EINA_FALSE will
1119  * let the knob alwayes at default size.
1120  *
1121  * @note It will conflict with elm_slider_indicator_format_set(), if
1122  * you wanted those effects.
1123  *
1124  * @ingroup Slider
1125  */
1126 EAPI void
1127 elm_slider_indicator_show_set(Evas_Object *obj, Eina_Bool show)
1128 {
1129    ELM_CHECK_WIDTYPE(obj, widtype);
1130    Widget_Data *wd = elm_widget_data_get(obj);
1131    if (show) {
1132         wd->indicator_show = EINA_TRUE;
1133         edje_object_signal_emit(wd->slider, "elm,state,val,show", "elm");
1134    }
1135    else {
1136         wd->indicator_show = EINA_FALSE;
1137         edje_object_signal_emit(wd->slider, "elm,state,val,hide", "elm");
1138    }
1139 }
1140
1141 /**
1142  * Get the state of indicator in the slider (if it's being shown or
1143  * not).
1144  *
1145  * @param obj The slider object
1146  * @return @c EINA_TRUE if the indicator is being shown, @c EINA_FALSE
1147  * otherwise.
1148  *
1149  *  @ingroup Slider
1150  */
1151 EAPI Eina_Bool
1152 elm_slider_indicator_show_get(const Evas_Object *obj)
1153 {
1154    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1155    Widget_Data *wd = elm_widget_data_get(obj);
1156    if (!wd) return EINA_FALSE;
1157    return wd->indicator_show;
1158 }
1159