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