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