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);
163 edje_object_message_signal_process(wd->slider);
169 _delay_change(void *data)
171 Widget_Data *wd = elm_widget_data_get(data);
172 if (!wd) return ECORE_CALLBACK_CANCEL;
174 evas_object_smart_callback_call(data, SIG_DELAY_CHANGED, NULL);
175 return ECORE_CALLBACK_CANCEL;
179 _val_fetch(Evas_Object *obj)
181 Widget_Data *wd = elm_widget_data_get(obj);
182 double posx = 0.0, posy = 0.0, pos = 0.0, val;
184 edje_object_part_drag_value_get(wd->slider, "elm.dragable.slider",
186 if (wd->horizontal) pos = posx;
188 if (wd->inverted) pos = 1.0 - pos;
189 val = (pos * (wd->val_max - wd->val_min)) + wd->val_min;
193 evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
194 if (wd->delay) ecore_timer_del(wd->delay);
195 wd->delay = ecore_timer_add(0.2, _delay_change, obj);
200 _val_set(Evas_Object *obj)
202 Widget_Data *wd = elm_widget_data_get(obj);
205 if (wd->val_max > wd->val_min)
206 pos = (wd->val - wd->val_min) / (wd->val_max - wd->val_min);
209 if (pos < 0.0) pos = 0.0;
210 else if (pos > 1.0) pos = 1.0;
211 if (wd->inverted) pos = 1.0 - pos;
212 edje_object_part_drag_value_set(wd->slider, "elm.dragable.slider", pos, pos);
216 _units_set(Evas_Object *obj)
218 Widget_Data *wd = elm_widget_data_get(obj);
224 snprintf(buf, sizeof(buf), wd->units, wd->val);
225 edje_object_part_text_set(wd->slider, "elm.units", buf);
228 edje_object_part_text_set(wd->slider, "elm.units", NULL);
232 _indicator_set(Evas_Object *obj)
234 Widget_Data *wd = elm_widget_data_get(obj);
236 if (wd->indicator_format_func)
239 buf = wd->indicator_format_func(wd->val);
240 edje_object_part_text_set(wd->slider, "elm.indicator", buf);
242 else if (wd->indicator)
245 snprintf(buf, sizeof(buf), wd->indicator, wd->val);
246 edje_object_part_text_set(wd->slider, "elm.indicator", buf);
249 edje_object_part_text_set(wd->slider, "elm.indicator", NULL);
253 _drag(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
257 _indicator_set(data);
261 _drag_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
264 evas_object_smart_callback_call(data, SIG_DRAG_START, NULL);
266 _indicator_set(data);
270 _drag_stop(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
273 evas_object_smart_callback_call(data, SIG_DRAG_STOP, NULL);
275 _indicator_set(data);
279 _drag_up(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
281 Widget_Data *wd = elm_widget_data_get(data);
282 edje_object_part_drag_step(wd->slider, "elm.dragable.slider", -0.05, -0.05);
286 _drag_down(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
288 Widget_Data *wd = elm_widget_data_get(data);
289 edje_object_part_drag_step(wd->slider, "elm.dragable.slider", 0.05, 0.05);
293 * Add a new slider to the parent
295 * @param parent The parent object
296 * @return The new object or NULL if it cannot be created
301 elm_slider_add(Evas_Object *parent)
307 wd = ELM_NEW(Widget_Data);
308 e = evas_object_evas_get(parent);
309 obj = elm_widget_add(e);
310 ELM_SET_WIDTYPE(widtype, "slider");
311 elm_widget_type_set(obj, "slider");
312 elm_widget_sub_object_add(parent, obj);
313 elm_widget_data_set(obj, wd);
314 elm_widget_del_hook_set(obj, _del_hook);
315 elm_widget_theme_hook_set(obj, _theme_hook);
317 wd->horizontal = EINA_TRUE;
322 wd->slider = edje_object_add(e);
323 _elm_theme_object_set(obj, wd->slider, "slider", "horizontal", "default");
324 elm_widget_resize_object_set(obj, wd->slider);
325 edje_object_signal_callback_add(wd->slider, "drag", "*", _drag, obj);
326 edje_object_signal_callback_add(wd->slider, "drag,start", "*", _drag_start, obj);
327 edje_object_signal_callback_add(wd->slider, "drag,stop", "*", _drag_stop, obj);
328 edje_object_signal_callback_add(wd->slider, "drag,step", "*", _drag_stop, obj);
329 edje_object_signal_callback_add(wd->slider, "drag,page", "*", _drag_stop, obj);
330 // edje_object_signal_callback_add(wd->slider, "drag,set", "*", _drag_stop, obj);
331 edje_object_signal_callback_add(wd->slider, "mouse,wheel,0,-1", "*", _drag_up, obj);
332 edje_object_signal_callback_add(wd->slider, "mouse,wheel,0,1", "*", _drag_down, obj);
333 edje_object_part_drag_value_set(wd->slider, "elm.dragable.slider", 0.0, 0.0);
335 wd->spacer = evas_object_rectangle_add(e);
336 evas_object_color_set(wd->spacer, 0, 0, 0, 0);
337 evas_object_pass_events_set(wd->spacer, 1);
338 elm_widget_sub_object_add(obj, wd->spacer);
339 edje_object_part_swallow(wd->slider, "elm.swallow.bar", wd->spacer);
341 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
345 // TODO: convert Elementary to subclassing of Evas_Smart_Class
346 // TODO: and save some bytes, making descriptions per-class and not instance!
347 evas_object_smart_callbacks_descriptions_set(obj, _signals);
352 * Set the label of the slider
354 * @param obj The slider object
355 * @param label The text label string in UTF-8
360 elm_slider_label_set(Evas_Object *obj, const char *label)
362 ELM_CHECK_WIDTYPE(obj, widtype);
363 Widget_Data *wd = elm_widget_data_get(obj);
365 eina_stringshare_replace(&wd->label, label);
368 edje_object_signal_emit(wd->slider, "elm,state,text,visible", "elm");
369 edje_object_message_signal_process(wd->slider);
373 edje_object_signal_emit(wd->slider, "elm,state,text,hidden", "elm");
374 edje_object_message_signal_process(wd->slider);
376 edje_object_part_text_set(wd->slider, "elm.text", label);
381 * Get the label of the slider
383 * @param obj The slider object
384 * @return The text label string in UTF-8
389 elm_slider_label_get(const Evas_Object *obj)
391 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
392 Widget_Data *wd = elm_widget_data_get(obj);
393 if (!wd) return NULL;
398 * Set the icon object of the slider object
400 * Once the icon object is set, a previously set one will be deleted.
402 * @param obj The slider object
403 * @param icon The icon object
408 elm_slider_icon_set(Evas_Object *obj, Evas_Object *icon)
410 ELM_CHECK_WIDTYPE(obj, widtype);
411 Widget_Data *wd = elm_widget_data_get(obj);
413 if (wd->icon == icon) return;
414 if (wd->icon) evas_object_del(wd->icon);
418 elm_widget_sub_object_add(obj, icon);
419 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
420 _changed_size_hints, obj);
421 edje_object_part_swallow(wd->slider, "elm.swallow.content", icon);
422 edje_object_signal_emit(wd->slider, "elm,state,icon,visible", "elm");
423 edje_object_message_signal_process(wd->slider);
429 * Get the icon object of the slider object
431 * @param obj The slider object
432 * @return The icon object
437 elm_slider_icon_get(const Evas_Object *obj)
439 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
440 Widget_Data *wd = elm_widget_data_get(obj);
441 if (!wd) return NULL;
446 * Set the length of the dragable region of the slider
448 * This sets the minimum width or height (depending on orientation) of the
449 * area of the slider that allows the slider to be dragged around. This in
450 * turn affects the objects minimum size (along with icon label and unit
451 * text). Note that this will also get multiplied by the scale factor.
453 * @param obj The slider object
454 * @param size The length of the slider area
459 elm_slider_span_size_set(Evas_Object *obj, Evas_Coord size)
461 ELM_CHECK_WIDTYPE(obj, widtype);
462 Widget_Data *wd = elm_widget_data_get(obj);
464 if (wd->size == size) return;
467 evas_object_size_hint_min_set(wd->spacer, (double)wd->size * elm_widget_scale_get(obj) * _elm_config->scale, 1);
469 evas_object_size_hint_min_set(wd->spacer, 1, (double)wd->size * elm_widget_scale_get(obj) * _elm_config->scale);
470 edje_object_part_swallow(wd->slider, "elm.swallow.bar", wd->spacer);
475 * Get the length of the dragable region of the slider
477 * This gets the minimum width or height (depending on orientation) of
478 * the area of the slider that allows the slider to be dragged
479 * around. Note that this will also get multiplied by the scale
482 * @param obj The slider object
483 * @return The length of the slider area
488 elm_slider_span_size_get(const Evas_Object *obj)
490 ELM_CHECK_WIDTYPE(obj, widtype) 0;
491 Widget_Data *wd = elm_widget_data_get(obj);
497 * Set the format string of the unit area
499 * If NULL, this disabls the unit area display. If not it sets the format
500 * string for the unit text. The unit text is provided a floating point
501 * value, so the unit text can display up to 1 floating point value. Note that
502 * this is optional. Use a format string such as "%1.2f meters" for example.
504 * @param obj The slider object
505 * @param units The format string for the units display
510 elm_slider_unit_format_set(Evas_Object *obj, const char *units)
512 ELM_CHECK_WIDTYPE(obj, widtype);
513 Widget_Data *wd = elm_widget_data_get(obj);
515 eina_stringshare_replace(&wd->units, units);
518 edje_object_signal_emit(wd->slider, "elm,state,units,visible", "elm");
519 edje_object_message_signal_process(wd->slider);
523 edje_object_signal_emit(wd->slider, "elm,state,units,hidden", "elm");
524 edje_object_message_signal_process(wd->slider);
531 * Get the format string for the unit area
533 * The slider may also display a value (the value of the slider) somewhere
534 * (for example above the slider knob that is dragged around). This sets the
535 * format string for this. See elm_slider_unit_format_set() for more
536 * information on how this works.
538 * @param obj The slider object
539 * @return The format string for the unit display.
544 elm_slider_unit_format_get(const Evas_Object *obj)
546 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
547 Widget_Data *wd = elm_widget_data_get(obj);
548 if (!wd) return NULL;
553 * Set the format string for the indicator area
555 * The slider may also display a value (the value of the slider) somewhere
556 * (for example above the slider knob that is dragged around). This sets the
557 * format string for this. See elm_slider_unit_format_set() for more
558 * information on how this works.
560 * @param obj The slider object
561 * @param indicator The format string for the indicator display
566 elm_slider_indicator_format_set(Evas_Object *obj, const char *indicator)
568 ELM_CHECK_WIDTYPE(obj, widtype);
569 Widget_Data *wd = elm_widget_data_get(obj);
571 eina_stringshare_replace(&wd->indicator, indicator);
576 * Get the format string for the indicator area
578 * The slider may also display a value (the value of the slider) somewhere
579 * (for example above the slider knob that is dragged around). This sets the
580 * format string for this. See elm_slider_indicator_format_set() for more
581 * information on how this works.
583 * @param obj The slider object
584 * @return The format string for the indicator display.
589 elm_slider_indicator_format_get(const Evas_Object *obj)
591 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
592 Widget_Data *wd = elm_widget_data_get(obj);
593 if (!wd) return NULL;
594 return wd->indicator;
598 * Set orientation of the slider
600 * @param obj The slider object
601 * @param horizontal If set, the slider will be horizontal
606 elm_slider_horizontal_set(Evas_Object *obj, Eina_Bool horizontal)
608 ELM_CHECK_WIDTYPE(obj, widtype);
609 Widget_Data *wd = elm_widget_data_get(obj);
611 horizontal = !!horizontal;
612 if (wd->horizontal == horizontal) return;
613 wd->horizontal = horizontal;
618 * Get orientation of the slider
620 * @param obj The slider object
621 * @return If @c EINA_TRUE the slider will be horizontal, else it is
626 elm_slider_horizontal_get(const Evas_Object *obj)
628 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
629 Widget_Data *wd = elm_widget_data_get(obj);
630 if (!wd) return EINA_FALSE;
631 return wd->horizontal;
635 * Set the minimum and maximum values for the slider
637 * Maximum mut be greater than minimum.
639 * @param obj The slider object
640 * @param min The minimum value
641 * @param max The maximum value
646 elm_slider_min_max_set(Evas_Object *obj, double min, double max)
648 ELM_CHECK_WIDTYPE(obj, widtype);
649 Widget_Data *wd = elm_widget_data_get(obj);
651 if ((wd->val_min == min) && (wd->val_max == max)) return;
654 if (wd->val < wd->val_min) wd->val = wd->val_min;
655 if (wd->val > wd->val_max) wd->val = wd->val_max;
662 * Get the minimum and maximum values for the slider
664 * @param obj The slider object
665 * @param min The pointer to store minimum value, may be @c NULL.
666 * @param max The pointer to store maximum value, may be @c NULL.
671 elm_slider_min_max_get(const Evas_Object *obj, double *min, double *max)
675 ELM_CHECK_WIDTYPE(obj, widtype);
676 Widget_Data *wd = elm_widget_data_get(obj);
678 if (min) *min = wd->val_min;
679 if (max) *max = wd->val_max;
683 * Set the value the slider indicates
685 * @param obj The slider object
686 * @param val The value (must be beween min and max for the slider)
691 elm_slider_value_set(Evas_Object *obj, double val)
693 ELM_CHECK_WIDTYPE(obj, widtype);
694 Widget_Data *wd = elm_widget_data_get(obj);
696 if (wd->val == val) return;
698 if (wd->val < wd->val_min) wd->val = wd->val_min;
699 if (wd->val > wd->val_max) wd->val = wd->val_max;
706 * Get the value the slider has
708 * @param obj The slider object
709 * @return The value of the slider
714 elm_slider_value_get(const Evas_Object *obj)
716 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
717 Widget_Data *wd = elm_widget_data_get(obj);
723 * Invert the slider display
725 * Normally the slider will display and interpret values from low to high
726 * and when horizontal that is left to right. When vertical that is top
727 * to bottom. This inverts this (so from right to left or bottom to top) if
728 * inverted is set to 1.
730 * @param obj The slider object
731 * @param inverted The inverted flag. 1 == inverted, 0 == normal
736 elm_slider_inverted_set(Evas_Object *obj, Eina_Bool inverted)
738 ELM_CHECK_WIDTYPE(obj, widtype);
739 Widget_Data *wd = elm_widget_data_get(obj);
741 inverted = !!inverted;
742 if (wd->inverted == inverted) return;
743 wd->inverted = inverted;
745 edje_object_signal_emit(wd->slider, "elm,state,inverted,on", "elm");
747 edje_object_signal_emit(wd->slider, "elm,state,inverted,off", "elm");
748 edje_object_message_signal_process(wd->slider);
755 * Get if the slider display is inverted (backwards)
757 * @param obj The slider object
758 * @return If @c EINA_TRUE the slider will be inverted.
762 elm_slider_inverted_get(const Evas_Object *obj)
764 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
765 Widget_Data *wd = elm_widget_data_get(obj);
766 if (!wd) return EINA_FALSE;
771 * Set the format function pointer for the inducator area
773 * Set the callback function to format the indicator string.
774 * See elm_slider_indicator_format_set() for more info on how this works.
776 * @param obj The slider object
777 * @param indicator The format string for the indicator display
778 * @param func The indicator format function
783 elm_slider_indicator_format_function_set(Evas_Object *obj, const char *(*func)(double val))
785 ELM_CHECK_WIDTYPE(obj, widtype);
786 Widget_Data *wd = elm_widget_data_get(obj);
788 wd->indicator_format_func = func;