1 #include <Elementary.h>
5 * @defgroup Slider Slider
7 * The slider adds a dragable “slider” widget for selecting the value of
8 * something within a range.
10 * Signals that you can add callbacks for are:
12 * changed - Whenever the slider value is changed by the user.
14 * delay,changed - A short time after the value is changed by the user.
15 * This will be called only when the user stops dragging for a very short
16 * period or when they release their finger/mouse, so it avoids possibly
17 * expensive reactions to the value change.
19 * slider,drag,start - dragging the slider indicator around has started
21 * slider,drag,stop - dragging the slider indicator around has stopped
23 * A slider can be horizontal or vertical. It can contain an Icon and has a
24 * primary label as well as a units label (that is formatted with floating
25 * point values and thus accepts a printf-style format string, like
26 * “%1.2f units”. There is also an indicator string that may be somewhere
27 * else (like on the slider itself) that also accepts a format string like
28 * units. Label, Icon Unit and Indicator strings/objects are optional.
30 * A slider may be inverted which means values invert, with high vales being
31 * on the left or top and low values on the right or bottom (as opposed to
32 * normally being low on the left or top and high on the bottom and right).
34 * The slider should have its minimum and maximum values set by the
35 * application with elm_slider_min_max_set() and value should also be set by
36 * the application before use with elm_slider_value_set(). The span of the
37 * slider is its length (horizontally or vertically). This will be scaled by
38 * the object or applications scaling factor. At any point code can query the
39 * slider for its value with elm_slider_value_get().
42 typedef struct _Widget_Data Widget_Data;
51 const char *indicator;
52 const char *(*indicator_format_func)(double val);
53 Eina_Bool horizontal : 1;
54 Eina_Bool inverted : 1;
55 double val, val_min, val_max;
60 static const char *widtype = NULL;
61 static void _del_hook(Evas_Object *obj);
62 static void _theme_hook(Evas_Object *obj);
63 static void _sizing_eval(Evas_Object *obj);
64 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
65 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
66 static void _units_set(Evas_Object *obj);
67 static void _indicator_set(Evas_Object *obj);
69 static const char SIG_CHANGED[] = "changed";
70 static const char SIG_DELAY_CHANGED[] = "delay,changed";
71 static const char SIG_DRAG_START[] = "slider,drag,start";
72 static const char SIG_DRAG_STOP[] = "slider,drag,stop";
73 static const Evas_Smart_Cb_Description _signals[] = {
75 {SIG_DELAY_CHANGED, ""},
82 _del_hook(Evas_Object *obj)
84 Widget_Data *wd = elm_widget_data_get(obj);
86 if (wd->label) eina_stringshare_del(wd->label);
87 if (wd->indicator) eina_stringshare_del(wd->units);
88 if (wd->delay) ecore_timer_del(wd->delay);
93 _theme_hook(Evas_Object *obj)
95 Widget_Data *wd = elm_widget_data_get(obj);
98 _elm_theme_object_set(obj, wd->slider, "slider", "horizontal", elm_widget_style_get(obj));
100 _elm_theme_object_set(obj, wd->slider, "slider", "vertical", elm_widget_style_get(obj));
102 edje_object_signal_emit(wd->slider, "elm,state,inverted,on", "elm");
104 edje_object_signal_emit(wd->slider, "elm,state,inverted,off", "elm");
106 edje_object_signal_emit(wd->slider, "elm,state,icon,visible", "elm");
108 edje_object_signal_emit(wd->slider, "elm,state,icon,hidden", "elm");
110 edje_object_signal_emit(wd->slider, "elm,state,text,visible", "elm");
112 edje_object_signal_emit(wd->slider, "elm,state,text,hidden", "elm");
113 edje_object_part_text_set(wd->slider, "elm.text", wd->label);
115 edje_object_signal_emit(wd->slider, "elm,state,units,visible", "elm");
117 edje_object_signal_emit(wd->slider, "elm,state,units,hidden", "elm");
119 evas_object_size_hint_min_set(wd->spacer, (double)wd->size * elm_widget_scale_get(obj) * _elm_config->scale, 1);
121 evas_object_size_hint_min_set(wd->spacer, 1, (double)wd->size * elm_widget_scale_get(obj) * _elm_config->scale);
122 edje_object_part_swallow(wd->slider, "elm.swallow.bar", wd->spacer);
124 edje_object_message_signal_process(wd->slider);
125 edje_object_scale_set(wd->slider, elm_widget_scale_get(obj) * _elm_config->scale);
130 _sizing_eval(Evas_Object *obj)
132 Widget_Data *wd = elm_widget_data_get(obj);
133 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
135 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
136 edje_object_size_min_restricted_calc(wd->slider, &minw, &minh, minw, minh);
137 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
138 evas_object_size_hint_min_set(obj, minw, minh);
139 evas_object_size_hint_max_set(obj, maxw, maxh);
143 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
145 Widget_Data *wd = elm_widget_data_get(data);
147 if (obj != wd->icon) return;
152 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
154 Widget_Data *wd = elm_widget_data_get(obj);
155 Evas_Object *sub = event_info;
159 edje_object_signal_emit(wd->slider, "elm,state,icon,hidden", "elm");
160 evas_object_event_callback_del_full
161 (sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
168 _delay_change(void *data)
170 Widget_Data *wd = elm_widget_data_get(data);
173 evas_object_smart_callback_call(data, SIG_DELAY_CHANGED, NULL);
178 _val_fetch(Evas_Object *obj)
180 Widget_Data *wd = elm_widget_data_get(obj);
181 double posx = 0.0, posy = 0.0, pos = 0.0, val;
183 edje_object_part_drag_value_get(wd->slider, "elm.dragable.slider",
185 if (wd->horizontal) pos = posx;
187 if (wd->inverted) pos = 1.0 - pos;
188 val = (pos * (wd->val_max - wd->val_min)) + wd->val_min;
192 evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
193 if (wd->delay) ecore_timer_del(wd->delay);
194 wd->delay = ecore_timer_add(0.2, _delay_change, obj);
199 _val_set(Evas_Object *obj)
201 Widget_Data *wd = elm_widget_data_get(obj);
204 if (wd->val_max > wd->val_min)
205 pos = (wd->val - wd->val_min) / (wd->val_max - wd->val_min);
208 if (pos < 0.0) pos = 0.0;
209 else if (pos > 1.0) pos = 1.0;
210 if (wd->inverted) pos = 1.0 - pos;
211 edje_object_part_drag_value_set(wd->slider, "elm.dragable.slider", pos, pos);
215 _units_set(Evas_Object *obj)
217 Widget_Data *wd = elm_widget_data_get(obj);
223 snprintf(buf, sizeof(buf), wd->units, wd->val);
224 edje_object_part_text_set(wd->slider, "elm.units", buf);
227 edje_object_part_text_set(wd->slider, "elm.units", NULL);
231 _indicator_set(Evas_Object *obj)
233 Widget_Data *wd = elm_widget_data_get(obj);
235 if (wd->indicator_format_func)
238 buf = wd->indicator_format_func(wd->val);
239 edje_object_part_text_set(wd->slider, "elm.indicator", buf);
241 else if (wd->indicator)
244 snprintf(buf, sizeof(buf), wd->indicator, wd->val);
245 edje_object_part_text_set(wd->slider, "elm.indicator", buf);
248 edje_object_part_text_set(wd->slider, "elm.indicator", NULL);
252 _drag(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
256 _indicator_set(data);
260 _drag_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
263 evas_object_smart_callback_call(data, SIG_DRAG_START, NULL);
265 _indicator_set(data);
269 _drag_stop(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
272 evas_object_smart_callback_call(data, SIG_DRAG_STOP, NULL);
274 _indicator_set(data);
278 _drag_up(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
280 Widget_Data *wd = elm_widget_data_get(data);
281 edje_object_part_drag_step(wd->slider, "elm.dragable.slider", -0.05, -0.05);
285 _drag_down(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
287 Widget_Data *wd = elm_widget_data_get(data);
288 edje_object_part_drag_step(wd->slider, "elm.dragable.slider", 0.05, 0.05);
292 * Add a new slider to the parent
294 * @param parent The parent object
295 * @return The new object or NULL if it cannot be created
300 elm_slider_add(Evas_Object *parent)
306 wd = ELM_NEW(Widget_Data);
307 e = evas_object_evas_get(parent);
308 obj = elm_widget_add(e);
309 ELM_SET_WIDTYPE(widtype, "slider");
310 elm_widget_type_set(obj, "slider");
311 elm_widget_sub_object_add(parent, obj);
312 elm_widget_data_set(obj, wd);
313 elm_widget_del_hook_set(obj, _del_hook);
314 elm_widget_theme_hook_set(obj, _theme_hook);
316 wd->horizontal = EINA_TRUE;
321 wd->slider = edje_object_add(e);
322 _elm_theme_object_set(obj, wd->slider, "slider", "horizontal", "default");
323 elm_widget_resize_object_set(obj, wd->slider);
324 edje_object_signal_callback_add(wd->slider, "drag", "*", _drag, obj);
325 edje_object_signal_callback_add(wd->slider, "drag,start", "*", _drag_start, obj);
326 edje_object_signal_callback_add(wd->slider, "drag,stop", "*", _drag_stop, obj);
327 edje_object_signal_callback_add(wd->slider, "drag,step", "*", _drag_stop, obj);
328 edje_object_signal_callback_add(wd->slider, "drag,page", "*", _drag_stop, obj);
329 // edje_object_signal_callback_add(wd->slider, "drag,set", "*", _drag_stop, obj);
330 edje_object_signal_callback_add(wd->slider, "mouse,wheel,0,-1", "*", _drag_up, obj);
331 edje_object_signal_callback_add(wd->slider, "mouse,wheel,0,1", "*", _drag_down, obj);
332 edje_object_part_drag_value_set(wd->slider, "elm.dragable.slider", 0.0, 0.0);
334 wd->spacer = evas_object_rectangle_add(e);
335 evas_object_color_set(wd->spacer, 0, 0, 0, 0);
336 evas_object_pass_events_set(wd->spacer, 1);
337 elm_widget_sub_object_add(obj, wd->spacer);
338 edje_object_part_swallow(wd->slider, "elm.swallow.bar", wd->spacer);
340 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
344 // TODO: convert Elementary to subclassing of Evas_Smart_Class
345 // TODO: and save some bytes, making descriptions per-class and not instance!
346 evas_object_smart_callbacks_descriptions_set(obj, _signals);
351 * Set the label of the slider
353 * @param obj The slider object
354 * @param label The text label string in UTF-8
359 elm_slider_label_set(Evas_Object *obj, const char *label)
361 ELM_CHECK_WIDTYPE(obj, widtype);
362 Widget_Data *wd = elm_widget_data_get(obj);
364 eina_stringshare_replace(&wd->label, label);
367 edje_object_signal_emit(wd->slider, "elm,state,text,visible", "elm");
368 edje_object_message_signal_process(wd->slider);
372 edje_object_signal_emit(wd->slider, "elm,state,text,hidden", "elm");
373 edje_object_message_signal_process(wd->slider);
375 edje_object_part_text_set(wd->slider, "elm.text", label);
380 * Get the label of the slider
382 * @param obj The slider object
383 * @return The text label string in UTF-8
388 elm_slider_label_get(const Evas_Object *obj)
390 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
391 Widget_Data *wd = elm_widget_data_get(obj);
392 if (!wd) return NULL;
397 * Set the icon object of the slider object
399 * Once the icon object is set, it will become a child of the slider object and
400 * be deleted when the slider object is deleted. If another icon object is set
401 * then the previous one becomes orophaned and will no longer be deleted along
404 * @param obj The slider object
405 * @param icon The icon object
410 elm_slider_icon_set(Evas_Object *obj, Evas_Object *icon)
412 ELM_CHECK_WIDTYPE(obj, widtype);
413 Widget_Data *wd = elm_widget_data_get(obj);
415 if ((wd->icon != icon) && (wd->icon))
416 elm_widget_sub_object_del(obj, wd->icon);
420 elm_widget_sub_object_add(obj, icon);
421 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
422 _changed_size_hints, obj);
423 edje_object_part_swallow(wd->slider, "elm.swallow.content", icon);
424 edje_object_signal_emit(wd->slider, "elm,state,icon,visible", "elm");
430 * Get the icon object of the slider object
432 * @param obj The slider object
433 * @return The icon object
438 elm_slider_icon_get(const Evas_Object *obj)
440 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
441 Widget_Data *wd = elm_widget_data_get(obj);
442 if (!wd) return NULL;
447 * Set the length of the dragable region of the slider
449 * This sets the minimum width or height (depending on orientation) of the
450 * area of the slider that allows the slider to be dragged around. This in
451 * turn affects the objects minimum size (along with icon label and unit
452 * text). Note that this will also get multiplied by the scale factor.
454 * @param obj The slider object
455 * @param size The length of the slider area
460 elm_slider_span_size_set(Evas_Object *obj, Evas_Coord size)
462 ELM_CHECK_WIDTYPE(obj, widtype);
463 Widget_Data *wd = elm_widget_data_get(obj);
465 if (wd->size == size) return;
468 evas_object_size_hint_min_set(wd->spacer, (double)wd->size * elm_widget_scale_get(obj) * _elm_config->scale, 1);
470 evas_object_size_hint_min_set(wd->spacer, 1, (double)wd->size * elm_widget_scale_get(obj) * _elm_config->scale);
471 edje_object_part_swallow(wd->slider, "elm.swallow.bar", wd->spacer);
476 * Get the length of the dragable region of the slider
478 * This gets the minimum width or height (depending on orientation) of
479 * the area of the slider that allows the slider to be dragged
480 * around. Note that this will also get multiplied by the scale
483 * @param obj The slider object
484 * @return The length of the slider area
489 elm_slider_span_size_get(const Evas_Object *obj)
491 ELM_CHECK_WIDTYPE(obj, widtype) 0;
492 Widget_Data *wd = elm_widget_data_get(obj);
498 * Set the format string of the unit area
500 * If NULL, this disabls the unit area display. If not it sets the format
501 * string for the unit text. The unit text is provided a floating point
502 * value, so the unit text can display up to 1 floating point value. Note that
503 * this is optional. Use a format string such as "%1.2f meters" for example.
505 * @param obj The slider object
506 * @param units The format string for the units display
511 elm_slider_unit_format_set(Evas_Object *obj, const char *units)
513 ELM_CHECK_WIDTYPE(obj, widtype);
514 Widget_Data *wd = elm_widget_data_get(obj);
516 eina_stringshare_replace(&wd->units, units);
519 edje_object_signal_emit(wd->slider, "elm,state,units,visible", "elm");
520 edje_object_message_signal_process(wd->slider);
524 edje_object_signal_emit(wd->slider, "elm,state,units,hidden", "elm");
525 edje_object_message_signal_process(wd->slider);
532 * Get the format string for the unit area
534 * The slider may also display a value (the value of the slider) somewhere
535 * (for example above the slider knob that is dragged around). This sets the
536 * format string for this. See elm_slider_unit_format_set() for more
537 * information on how this works.
539 * @param obj The slider object
540 * @return The format string for the unit display.
545 elm_slider_unit_format_get(const Evas_Object *obj)
547 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
548 Widget_Data *wd = elm_widget_data_get(obj);
549 if (!wd) return NULL;
554 * Set the format string for the indicator area
556 * The slider may also display a value (the value of the slider) somewhere
557 * (for example above the slider knob that is dragged around). This sets the
558 * format string for this. See elm_slider_unit_format_set() for more
559 * information on how this works.
561 * @param obj The slider object
562 * @param indicator The format string for the indicator display
567 elm_slider_indicator_format_set(Evas_Object *obj, const char *indicator)
569 ELM_CHECK_WIDTYPE(obj, widtype);
570 Widget_Data *wd = elm_widget_data_get(obj);
572 eina_stringshare_replace(&wd->indicator, indicator);
577 * Get the format string for the indicator area
579 * The slider may also display a value (the value of the slider) somewhere
580 * (for example above the slider knob that is dragged around). This sets the
581 * format string for this. See elm_slider_indicator_format_set() for more
582 * information on how this works.
584 * @param obj The slider object
585 * @return The format string for the indicator display.
590 elm_slider_indicator_format_get(const Evas_Object *obj)
592 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
593 Widget_Data *wd = elm_widget_data_get(obj);
594 if (!wd) return NULL;
595 return wd->indicator;
599 * Set orientation of the slider
601 * @param obj The slider object
602 * @param horizontal If set, the slider will be horizontal
607 elm_slider_horizontal_set(Evas_Object *obj, Eina_Bool horizontal)
609 ELM_CHECK_WIDTYPE(obj, widtype);
610 Widget_Data *wd = elm_widget_data_get(obj);
612 horizontal = !!horizontal;
613 if (wd->horizontal == horizontal) return;
614 wd->horizontal = horizontal;
619 * Get orientation of the slider
621 * @param obj The slider object
622 * @return If @c EINA_TRUE the slider will be horizontal, else it is
627 elm_slider_horizontal_get(const Evas_Object *obj)
629 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
630 Widget_Data *wd = elm_widget_data_get(obj);
631 if (!wd) return EINA_FALSE;
632 return wd->horizontal;
636 * Set the minimum and maximum values for the slider
638 * Maximum mut be greater than minimum.
640 * @param obj The slider object
641 * @param min The minimum value
642 * @param max The maximum value
647 elm_slider_min_max_set(Evas_Object *obj, double min, double max)
649 ELM_CHECK_WIDTYPE(obj, widtype);
650 Widget_Data *wd = elm_widget_data_get(obj);
652 if ((wd->val_min == min) && (wd->val_max == max)) return;
655 if (wd->val < wd->val_min) wd->val = wd->val_min;
656 if (wd->val > wd->val_max) wd->val = wd->val_max;
663 * Get the minimum and maximum values for the slider
665 * @param obj The slider object
666 * @param min The pointer to store minimum value, may be @c NULL.
667 * @param max The pointer to store maximum value, may be @c NULL.
672 elm_slider_min_max_get(const Evas_Object *obj, double *min, double *max)
676 ELM_CHECK_WIDTYPE(obj, widtype);
677 Widget_Data *wd = elm_widget_data_get(obj);
679 if (min) *min = wd->val_min;
680 if (max) *max = wd->val_max;
684 * Set the value the slider indicates
686 * @param obj The slider object
687 * @param val The value (must be beween min and max for the slider)
692 elm_slider_value_set(Evas_Object *obj, double val)
694 ELM_CHECK_WIDTYPE(obj, widtype);
695 Widget_Data *wd = elm_widget_data_get(obj);
697 if (wd->val == val) return;
699 if (wd->val < wd->val_min) wd->val = wd->val_min;
700 if (wd->val > wd->val_max) wd->val = wd->val_max;
707 * Get the value the slider has
709 * @param obj The slider object
710 * @return The value of the slider
715 elm_slider_value_get(const Evas_Object *obj)
717 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
718 Widget_Data *wd = elm_widget_data_get(obj);
724 * Invert the slider display
726 * Normally the slider will display and interpret values from low to high
727 * and when horizontal that is left to right. When vertical that is top
728 * to bottom. This inverts this (so from right to left or bottom to top) if
729 * inverted is set to 1.
731 * @param obj The slider object
732 * @param inverted The inverted flag. 1 == inverted, 0 == normal
737 elm_slider_inverted_set(Evas_Object *obj, Eina_Bool inverted)
739 ELM_CHECK_WIDTYPE(obj, widtype);
740 Widget_Data *wd = elm_widget_data_get(obj);
742 inverted = !!inverted;
743 if (wd->inverted == inverted) return;
744 wd->inverted = inverted;
746 edje_object_signal_emit(wd->slider, "elm,state,inverted,on", "elm");
748 edje_object_signal_emit(wd->slider, "elm,state,inverted,off", "elm");
749 edje_object_message_signal_process(wd->slider);
756 * Get if the slider display is inverted (backwards)
758 * @param obj The slider object
759 * @return If @c EINA_TRUE the slider will be inverted.
763 elm_slider_inverted_get(const Evas_Object *obj)
765 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
766 Widget_Data *wd = elm_widget_data_get(obj);
767 if (!wd) return EINA_FALSE;
772 * Set the format function pointer for the inducator area
774 * Set the callback function to format the indicator string.
775 * See elm_slider_indicator_format_set() for more info on how this works.
777 * @param obj The slider object
778 * @param indicator The format string for the indicator display
779 * @param func The indicator format function
784 elm_slider_indicator_format_function_set(Evas_Object *obj, const char *(*func)(double val))
786 ELM_CHECK_WIDTYPE(obj, widtype);
787 Widget_Data *wd = elm_widget_data_get(obj);
789 wd->indicator_format_func = func;