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