X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Flib%2Felm_slider.c;h=8f571a50e6eecc64040bd6cb410e17d486fb93b5;hb=e32953011e0194d933f778e93cdae4dd27373eb4;hp=0258746e124a48ab3419e1acf5c864e4d67dba38;hpb=b80c1829ef5115bbe7d3fe8d169e456d9cd64e36;p=framework%2Fuifw%2Felementary.git diff --git a/src/lib/elm_slider.c b/src/lib/elm_slider.c index 0258746..8f571a5 100644 --- a/src/lib/elm_slider.c +++ b/src/lib/elm_slider.c @@ -1,44 +1,6 @@ #include #include "elm_priv.h" -/** - * @defgroup Slider Slider - * - * The slider adds a dragable “slider” widget for selecting the value of - * something within a range. - * - * Signals that you can add callbacks for are: - * - * changed - Whenever the slider value is changed by the user. - * - * delay,changed - A short time after the value is changed by the user. - * This will be called only when the user stops dragging for a very short - * period or when they release their finger/mouse, so it avoids possibly - * expensive reactions to the value change. - * - * slider,drag,start - dragging the slider indicator around has started - * - * slider,drag,stop - dragging the slider indicator around has stopped - * - * A slider can be horizontal or vertical. It can contain an Icon and has a - * primary label as well as a units label (that is formatted with floating - * point values and thus accepts a printf-style format string, like - * “%1.2f units”. There is also an indicator string that may be somewhere - * else (like on the slider itself) that also accepts a format string like - * units. Label, Icon Unit and Indicator strings/objects are optional. - * - * A slider may be inverted which means values invert, with high vales being - * on the left or top and low values on the right or bottom (as opposed to - * normally being low on the left or top and high on the bottom and right). - * - * The slider should have its minimum and maximum values set by the - * application with elm_slider_min_max_set() and value should also be set by - * the application before use with elm_slider_value_set(). The span of the - * slider is its length (horizontally or vertically). This will be scaled by - * the object or applications scaling factor. At any point code can query the - * slider for its value with elm_slider_value_get(). - */ - typedef struct _Widget_Data Widget_Data; struct _Widget_Data @@ -47,16 +9,28 @@ struct _Widget_Data Evas_Object *icon; Evas_Object *end; Evas_Object *spacer; - const char *label; + + Ecore_Timer *delay; + + Eina_Hash *labels; const char *units; const char *indicator; - const char *(*indicator_format_func)(double val); + + char *(*indicator_format_func)(double val); + void (*indicator_format_free)(char *str); + + char *(*units_format_func)(double val); + void (*units_format_free)(char *str); + + double val, val_min, val_max, val2; + Evas_Coord size; + Evas_Coord downx, downy; + Eina_Bool horizontal : 1; Eina_Bool inverted : 1; Eina_Bool indicator_show : 1; - double val, val_min, val_max; - Ecore_Timer *delay; - Evas_Coord size; + Eina_Bool spacer_down : 1; + Eina_Bool frozen : 1; }; #define ELM_SLIDER_INVERTED_FACTOR (-1.0) @@ -79,7 +53,9 @@ static void _drag_down(void *data, Evas_Object *obj, const char *emission, const char *source); static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src, Evas_Callback_Type type, void *event_info); -static void _spacer_cb(void *data, Evas * e, Evas_Object * obj, void *event_info); +static void _spacer_down_cb(void *data, Evas * e, Evas_Object * obj, void *event_info); +static void _spacer_move_cb(void *data, Evas * e, Evas_Object * obj, void *event_info); +static void _spacer_up_cb(void *data, Evas * e, Evas_Object * obj, void *event_info); static const char SIG_CHANGED[] = "changed"; static const char SIG_DELAY_CHANGED[] = "delay,changed"; @@ -119,8 +95,8 @@ _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type ty ev = event_info; if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE; if (elm_widget_disabled_get(obj)) return EINA_FALSE; - if ((!strcmp(ev->keyname, "Left")) - || (!strcmp(ev->keyname, "KP_Left"))) + if ((!strcmp(ev->keyname, "Left")) || + ((!strcmp(ev->keyname, "KP_Left")) && (!ev->string))) { if (!wd->horizontal) return EINA_FALSE; if (!wd->inverted) _drag_down(obj, NULL, NULL, NULL); @@ -128,8 +104,8 @@ _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type ty ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD; return EINA_TRUE; } - else if ((!strcmp(ev->keyname, "Right")) - || (!strcmp(ev->keyname, "KP_Right"))) + else if ((!strcmp(ev->keyname, "Right")) || + ((!strcmp(ev->keyname, "KP_Right")) && (!ev->string))) { if (!wd->horizontal) return EINA_FALSE; if (!wd->inverted) _drag_up(obj, NULL, NULL, NULL); @@ -137,7 +113,8 @@ _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type ty ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD; return EINA_TRUE; } - else if ((!strcmp(ev->keyname, "Up")) || (!strcmp(ev->keyname, "KP_Up"))) + else if ((!strcmp(ev->keyname, "Up")) || + ((!strcmp(ev->keyname, "KP_Up")) && (!ev->string))) { if (wd->horizontal) return EINA_FALSE; if (wd->inverted) _drag_up(obj, NULL, NULL, NULL); @@ -145,7 +122,8 @@ _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type ty ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD; return EINA_TRUE; } - else if ((!strcmp(ev->keyname, "Down")) || (!strcmp(ev->keyname, "KP_Down"))) + else if ((!strcmp(ev->keyname, "Down")) || + ((!strcmp(ev->keyname, "KP_Down")) && (!ev->string))) { if (wd->horizontal) return EINA_FALSE; if (wd->inverted) _drag_down(obj, NULL, NULL, NULL); @@ -161,7 +139,7 @@ _del_hook(Evas_Object *obj) { Widget_Data *wd = elm_widget_data_get(obj); if (!wd) return; - if (wd->label) eina_stringshare_del(wd->label); + if (wd->labels) eina_hash_free(wd->labels); if (wd->indicator) eina_stringshare_del(wd->units); if (wd->delay) ecore_timer_del(wd->delay); free(wd); @@ -192,6 +170,16 @@ _mirrored_set(Evas_Object *obj, Eina_Bool rtl) edje_object_mirrored_set(wd->slider, rtl); } +static Eina_Bool +_labels_foreach_text_set(const Eina_Hash *hash __UNUSED__, const void *key, void *data, void *fdata) +{ + Widget_Data *wd = fdata; + + edje_object_part_text_escaped_set(wd->slider, key, data); + + return 1; +} + static void _theme_hook(Evas_Object *obj) { @@ -203,6 +191,10 @@ _theme_hook(Evas_Object *obj) _elm_theme_object_set(obj, wd->slider, "slider", "horizontal", elm_widget_style_get(obj)); else _elm_theme_object_set(obj, wd->slider, "slider", "vertical", elm_widget_style_get(obj)); + if (elm_widget_disabled_get(obj)) + edje_object_signal_emit(wd->slider, "elm,state,disabled", "elm"); + else + edje_object_signal_emit(wd->slider, "elm,state,enabled", "elm"); if (wd->icon) { edje_object_part_swallow(wd->slider, "elm.swallow.content", wd->icon); @@ -212,10 +204,10 @@ _theme_hook(Evas_Object *obj) edje_object_signal_emit(wd->slider, "elm,state,end,visible", "elm"); else edje_object_signal_emit(wd->slider, "elm,state,end,hidden", "elm"); - if (wd->label) + if (wd->labels) { - edje_object_part_text_set(wd->slider, "elm.text", wd->label); - edje_object_signal_emit(wd->slider, "elm,state,text,visible", "elm"); + eina_hash_foreach(wd->labels, _labels_foreach_text_set, wd); + edje_object_signal_emit(wd->slider, "elm,state,text,visible", "elm"); } if (wd->units) @@ -360,15 +352,22 @@ _units_set(Evas_Object *obj) { Widget_Data *wd = elm_widget_data_get(obj); if (!wd) return; - if (wd->units) + if (wd->units_format_func) + { + char *buf; + buf = wd->units_format_func(wd->val); + edje_object_part_text_escaped_set(wd->slider, "elm.units", buf); + if (wd->units_format_free) wd->units_format_free(buf); + } + else if (wd->units) { char buf[1024]; snprintf(buf, sizeof(buf), wd->units, wd->val); - edje_object_part_text_set(wd->slider, "elm.units", buf); + edje_object_part_text_escaped_set(wd->slider, "elm.units", buf); } else - edje_object_part_text_set(wd->slider, "elm.units", NULL); + edje_object_part_text_escaped_set(wd->slider, "elm.units", NULL); } static void @@ -378,41 +377,51 @@ _indicator_set(Evas_Object *obj) if (!wd) return; if (wd->indicator_format_func) { - const char *buf; + char *buf; buf = wd->indicator_format_func(wd->val); - edje_object_part_text_set(wd->slider, "elm.dragable.slider:elm.indicator", buf); + edje_object_part_text_escaped_set(wd->slider, "elm.dragable.slider:elm.indicator", buf); + if (wd->indicator_format_free) wd->indicator_format_free(buf); } else if (wd->indicator) { char buf[1024]; snprintf(buf, sizeof(buf), wd->indicator, wd->val); - edje_object_part_text_set(wd->slider, "elm.dragable.slider:elm.indicator", buf); + edje_object_part_text_escaped_set(wd->slider, "elm.dragable.slider:elm.indicator", buf); } else - edje_object_part_text_set(wd->slider, "elm.dragable.slider:elm.indicator", NULL); + edje_object_part_text_escaped_set(wd->slider, "elm.dragable.slider:elm.indicator", NULL); } static void _drag(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__) { + Widget_Data *wd = elm_widget_data_get((Evas_Object*)data); + if (elm_widget_disabled_get(data)) return; _val_fetch(data); _units_set(data); _indicator_set(data); + edje_object_signal_emit(wd->slider, "elm,state,drag", "elm"); + edje_object_message_signal_process(wd->slider); } static void _drag_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__) { + Widget_Data *wd = elm_widget_data_get((Evas_Object*)data); + if (elm_widget_disabled_get(data)) return; _val_fetch(data); evas_object_smart_callback_call(data, SIG_DRAG_START, NULL); _units_set(data); _indicator_set(data); elm_widget_scroll_freeze_push(data); + edje_object_signal_emit(wd->slider, "elm,state,drag", "elm"); + edje_object_message_signal_process(wd->slider); } static void _drag_stop(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__) { + if (elm_widget_disabled_get(data)) return; _val_fetch(data); evas_object_smart_callback_call(data, SIG_DRAG_STOP, NULL); _units_set(data); @@ -423,6 +432,7 @@ _drag_stop(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSE static void _drag_step(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__) { + if (elm_widget_disabled_get(data)) return; _val_fetch(data); _units_set(data); _indicator_set(data); @@ -433,6 +443,7 @@ _drag_up(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED_ { double step; Widget_Data *wd; + if (elm_widget_disabled_get(data)) return; wd = elm_widget_data_get(data); step = 0.05; @@ -447,6 +458,7 @@ _drag_down(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSE { double step; Widget_Data *wd; + if (elm_widget_disabled_get(data)) return; wd = elm_widget_data_get(data); step = -0.05; @@ -457,109 +469,155 @@ _drag_down(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSE } static void -_spacer_cb(void *data, Evas *e, Evas_Object *obj __UNUSED__, void *event_info) +_spacer_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info) { Widget_Data *wd = elm_widget_data_get(data); Evas_Event_Mouse_Down *ev = event_info; Evas_Coord x, y, w, h; - double button_x, button_y; + double button_x = 0.0, button_y = 0.0; + if (elm_widget_disabled_get(data)) return; + wd->spacer_down = EINA_TRUE; + wd->val2 = wd->val; evas_object_geometry_get(wd->spacer, &x, &y, &w, &h); + wd->downx = ev->canvas.x - x; + wd->downy = ev->canvas.y - y; edje_object_part_drag_value_get(wd->slider, "elm.dragable.slider", &button_x, &button_y); if (wd->horizontal) { - button_x = ((double)ev->output.x - (double)x) / (double)w; + button_x = ((double)ev->canvas.x - (double)x) / (double)w; if (button_x > 1) button_x = 1; if (button_x < 0) button_x = 0; } else { - button_y = ((double)ev->output.y - (double)y) / (double)h; + button_y = ((double)ev->canvas.y - (double)y) / (double)h; if (button_y > 1) button_y = 1; if (button_y < 0) button_y = 0; } edje_object_part_drag_value_set(wd->slider, "elm.dragable.slider", button_x, button_y); - evas_event_feed_mouse_cancel(e, 0, NULL); - evas_event_feed_mouse_down(e, 1, EVAS_BUTTON_NONE, 0, NULL); + _val_fetch(data); + evas_object_smart_callback_call(data, SIG_DRAG_START, NULL); + _units_set(data); + _indicator_set(data); + edje_object_signal_emit(wd->slider, "elm,state,indicator,show", "elm"); + edje_object_signal_emit(wd->slider, "elm,state,drag", "elm"); + edje_object_message_signal_process(wd->slider); } -/** - * Add a new slider to the parent - * - * @param parent The parent object - * @return The new object or NULL if it cannot be created - * - * @ingroup Slider - */ -EAPI Evas_Object * -elm_slider_add(Evas_Object *parent) +static void +_spacer_move_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info) { - Evas_Object *obj; - Evas *e; - Widget_Data *wd; - - ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL); - - ELM_SET_WIDTYPE(widtype, "slider"); - elm_widget_type_set(obj, "slider"); - elm_widget_sub_object_add(parent, obj); - elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL); - elm_widget_data_set(obj, wd); - elm_widget_del_hook_set(obj, _del_hook); - elm_widget_theme_hook_set(obj, _theme_hook); - elm_widget_disable_hook_set(obj, _disable_hook); - elm_widget_can_focus_set(obj, EINA_TRUE); - elm_widget_event_hook_set(obj, _event_hook); - - wd->horizontal = EINA_TRUE; - wd->indicator_show = EINA_TRUE; - wd->val = 0.0; - wd->val_min = 0.0; - wd->val_max = 1.0; - - wd->slider = edje_object_add(e); - _elm_theme_object_set(obj, wd->slider, "slider", "horizontal", "default"); - elm_widget_resize_object_set(obj, wd->slider); - edje_object_signal_callback_add(wd->slider, "drag", "*", _drag, obj); - edje_object_signal_callback_add(wd->slider, "drag,start", "*", _drag_start, obj); - edje_object_signal_callback_add(wd->slider, "drag,stop", "*", _drag_stop, obj); - edje_object_signal_callback_add(wd->slider, "drag,step", "*", _drag_step, obj); - edje_object_signal_callback_add(wd->slider, "drag,page", "*", _drag_stop, obj); - // edje_object_signal_callback_add(wd->slider, "drag,set", "*", _drag_stop, obj); - edje_object_part_drag_value_set(wd->slider, "elm.dragable.slider", 0.0, 0.0); + Widget_Data *wd = elm_widget_data_get(data); + Evas_Event_Mouse_Move *ev = event_info; + Evas_Coord x, y, w, h; + double button_x = 0.0, button_y = 0.0; + if (elm_widget_disabled_get(data)) return; - wd->spacer = evas_object_rectangle_add(e); - evas_object_color_set(wd->spacer, 0, 0, 0, 0); - evas_object_pass_events_set(wd->spacer, EINA_TRUE); - elm_widget_sub_object_add(obj, wd->spacer); - edje_object_part_swallow(wd->slider, "elm.swallow.bar", wd->spacer); - evas_object_event_callback_add(wd->spacer, EVAS_CALLBACK_MOUSE_DOWN, _spacer_cb, obj); - evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj); + if (wd->spacer_down) + { + Evas_Coord d = 0; + + evas_object_geometry_get(wd->spacer, &x, &y, &w, &h); + if (wd->horizontal) d = abs(ev->cur.canvas.x - x - wd->downx); + else d = abs(ev->cur.canvas.y - y - wd->downy); + if (d > (_elm_config->thumbscroll_threshold - 1)) + { + if (!wd->frozen) + { + elm_widget_scroll_freeze_push(data); + wd->frozen = EINA_TRUE; + } + ev->event_flags &= ~EVAS_EVENT_FLAG_ON_HOLD; + } + + if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) + { + if (wd->spacer_down) wd->spacer_down = EINA_FALSE; + _val_fetch(data); + evas_object_smart_callback_call(data, SIG_DRAG_STOP, NULL); + _units_set(data); + _indicator_set(data); + if (wd->frozen) + { + elm_widget_scroll_freeze_pop(data); + wd->frozen = EINA_FALSE; + } + edje_object_signal_emit(wd->slider, "elm,state,indicator,hide", "elm"); + elm_slider_value_set(data, wd->val2); + return; + } + if (wd->horizontal) + { + button_x = ((double)ev->cur.canvas.x - (double)x) / (double)w; + if (button_x > 1) button_x = 1; + if (button_x < 0) button_x = 0; + } + else + { + button_y = ((double)ev->cur.canvas.y - (double)y) / (double)h; + if (button_y > 1) button_y = 1; + if (button_y < 0) button_y = 0; + } + edje_object_part_drag_value_set(wd->slider, "elm.dragable.slider", button_x, button_y); + _val_fetch(data); + _units_set(data); + _indicator_set(data); + edje_object_signal_emit(wd->slider, "elm,state,drag", "elm"); + edje_object_message_signal_process(wd->slider); + } +} - _mirrored_set(obj, elm_widget_mirrored_get(obj)); - _sizing_eval(obj); +static void +_spacer_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__) +{ + Widget_Data *wd = elm_widget_data_get(data); + if (elm_widget_disabled_get(data)) return; + if (!wd->spacer_down) return; - // TODO: convert Elementary to subclassing of Evas_Smart_Class - // TODO: and save some bytes, making descriptions per-class and not instance! - evas_object_smart_callbacks_descriptions_set(obj, _signals); - return obj; + wd->spacer_down = EINA_FALSE; + _val_fetch(data); + evas_object_smart_callback_call(data, SIG_DRAG_STOP, NULL); + _units_set(data); + _indicator_set(data); + if (wd->frozen) + { + elm_widget_scroll_freeze_pop(data); + wd->frozen = EINA_FALSE; + } + edje_object_signal_emit(wd->slider, "elm,state,indicator,hide", "elm"); } -/** - * Set the label of the slider - * - * @param obj The slider object - * @param label The text label string in UTF-8 - * - * @ingroup Slider - */ -EAPI void -elm_slider_label_set(Evas_Object *obj, const char *label) +static void +_elm_slider_label_set(Evas_Object *obj, const char *part, const char *label) { ELM_CHECK_WIDTYPE(obj, widtype); Widget_Data *wd = elm_widget_data_get(obj); + const char* default_part = "elm.text"; + const char* real_part; + if (!wd) return; - eina_stringshare_replace(&wd->label, label); + + if (!part) + real_part = default_part; + else + real_part = part; + + if (wd->labels) + { + const char* old_label; + + old_label = eina_hash_find(wd->labels, real_part); + if (!old_label) + eina_hash_add(wd->labels, real_part, eina_stringshare_add(label)); + else + { + eina_stringshare_ref(old_label); + eina_hash_modify(wd->labels, real_part, eina_stringshare_add(label)); + eina_stringshare_del(old_label); + } + } + if (label) { edje_object_signal_emit(wd->slider, "elm,state,text,visible", "elm"); @@ -570,46 +628,27 @@ elm_slider_label_set(Evas_Object *obj, const char *label) edje_object_signal_emit(wd->slider, "elm,state,text,hidden", "elm"); edje_object_message_signal_process(wd->slider); } - edje_object_part_text_set(wd->slider, "elm.text", label); + + edje_object_part_text_escaped_set(wd->slider, real_part, label); _sizing_eval(obj); } -/** - * Get the label of the slider - * - * @param obj The slider object - * @return The text label string in UTF-8 - * - * @ingroup Slider - */ -EAPI const char * -elm_slider_label_get(const Evas_Object *obj) +static const char * +_elm_slider_label_get(const Evas_Object *obj, const char *part) { ELM_CHECK_WIDTYPE(obj, widtype) NULL; Widget_Data *wd = elm_widget_data_get(obj); if (!wd) return NULL; - return wd->label; + if (!wd->labels) return NULL; + + if (!part) + return eina_hash_find(wd->labels, "elm.text"); + return eina_hash_find(wd->labels, part); } -/** - * Set the icon object (leftmost widget) of the slider object. - * - * Once the icon object is set, a previously set one will be deleted. - * If you want to keep that old content object, use the - * elm_slider_icon_unset() function. - * - * @param obj The slider object - * @param icon The icon object - * - * @note If the object being set does not have minimum size hints set, - * it won't get properly displayed. - * - * @ingroup Slider - */ -EAPI void -elm_slider_icon_set(Evas_Object *obj, Evas_Object *icon) +static void +_icon_set(Evas_Object *obj, Evas_Object *icon) { - ELM_CHECK_WIDTYPE(obj, widtype); Widget_Data *wd = elm_widget_data_get(obj); if (!wd) return; if (wd->icon == icon) return; @@ -627,28 +666,18 @@ elm_slider_icon_set(Evas_Object *obj, Evas_Object *icon) _sizing_eval(obj); } -/** - * Unset the leftmost widget of the slider, unparenting and - * returning it. - * - * @param obj The slider object - * @return the previously set icon sub-object of this slider, on - * success. - * - * @see elm_slider_icon_set() - * - * @ingroup Slider - */ -EAPI Evas_Object * -elm_slider_icon_unset(Evas_Object *obj) +static Evas_Object * +_icon_unset(Evas_Object *obj) { - ELM_CHECK_WIDTYPE(obj, widtype) NULL; Widget_Data *wd = elm_widget_data_get(obj); Evas_Object *ret = NULL; if (!wd) return NULL; if (wd->icon) { elm_widget_sub_object_del(obj, wd->icon); + evas_object_event_callback_del_full(wd->icon, + EVAS_CALLBACK_CHANGED_SIZE_HINTS, + _changed_size_hints, obj); ret = wd->icon; edje_object_part_unswallow(wd->slider, wd->icon); edje_object_signal_emit(wd->slider, "elm,state,icon,hidden", "elm"); @@ -658,37 +687,186 @@ elm_slider_icon_unset(Evas_Object *obj) return ret; } -/** - * Get the icon object of the slider object. This object is owned by - * the scrolled entry and should not be modified. - * - * @param obj The slider object - * @return The icon object - * - * @ingroup Slider - */ -EAPI Evas_Object * -elm_slider_icon_get(const Evas_Object *obj) +static void +_end_set(Evas_Object *obj, Evas_Object *end) +{ + Widget_Data *wd = elm_widget_data_get(obj); + if (!wd) return; + if (wd->end == end) return; + if (wd->end) evas_object_del(wd->end); + wd->end = end; + if (end) + { + elm_widget_sub_object_add(obj, end); + evas_object_event_callback_add(end, EVAS_CALLBACK_CHANGED_SIZE_HINTS, + _changed_size_hints, obj); + edje_object_part_swallow(wd->slider, "elm.swallow.end", end); + edje_object_signal_emit(wd->slider, "elm,state,end,visible", "elm"); + edje_object_message_signal_process(wd->slider); + } + _sizing_eval(obj); +} + +static Evas_Object * +_end_unset(Evas_Object *obj) { - ELM_CHECK_WIDTYPE(obj, widtype) NULL; Widget_Data *wd = elm_widget_data_get(obj); + Evas_Object *ret = NULL; + if (!wd) return NULL; + if (wd->end) + { + elm_widget_sub_object_del(obj, wd->end); + evas_object_event_callback_del_full(wd->end, + EVAS_CALLBACK_CHANGED_SIZE_HINTS, + _changed_size_hints, obj); + ret = wd->end; + edje_object_part_unswallow(wd->slider, wd->end); + edje_object_signal_emit(wd->slider, "elm,state,end,hidden", "elm"); + wd->end = NULL; + _sizing_eval(obj); + } + return ret; +} + +static void +_content_set_hook(Evas_Object *obj, const char *part, Evas_Object *content) +{ + ELM_CHECK_WIDTYPE(obj, widtype); + if (!part || !strcmp(part, "icon")) + _icon_set(obj, content); + else if (!strcmp(part, "end")) + _end_set(obj, content); +} + +static Evas_Object * +_content_get_hook(const Evas_Object *obj, const char *part) +{ + ELM_CHECK_WIDTYPE(obj, widtype) NULL; + Widget_Data *wd; + wd = elm_widget_data_get(obj); if (!wd) return NULL; - return wd->icon; + if (!part || !strcmp(part, "icon")) + return wd->icon; + else if (!strcmp(part, "end")) + return wd->end; + return NULL; +} + +static Evas_Object * +_content_unset_hook(Evas_Object *obj, const char *part) +{ + ELM_CHECK_WIDTYPE(obj, widtype) NULL; + if (!part || !strcmp(part, "icon")) + return _icon_unset(obj); + else if (!strcmp(part, "end")) + return _end_unset(obj); + return NULL; +} + +static void +_hash_labels_free_cb(void* label) +{ + if (label) + eina_stringshare_del(label); +} + +static void +_min_max_set(Evas_Object *obj) +{ + char *buf_min = NULL; + char *buf_max = NULL; + + Widget_Data *wd = elm_widget_data_get(obj); + if (!wd) return; + if (wd->units_format_func) + { + buf_min = wd->units_format_func(wd->val_min); + buf_max = wd->units_format_func(wd->val_max); + } + else if (wd->units) + { + int length = strlen(wd->units); + + buf_min = alloca(length + 128); + buf_max = alloca(length + 128); + + snprintf((char*) buf_min, length + 128, wd->units, wd->val_min); + snprintf((char*) buf_max, length + 128, wd->units, wd->val_max); + } + + edje_object_part_text_escaped_set(wd->slider, "elm.units.min", buf_min); + edje_object_part_text_escaped_set(wd->slider, "elm.units.max", buf_max); + + if (wd->units_format_func && wd->units_format_free) + { + wd->units_format_free(buf_min); + wd->units_format_free(buf_max); + } +} + + +EAPI Evas_Object * +elm_slider_add(Evas_Object *parent) +{ + Evas_Object *obj; + Evas *e; + Widget_Data *wd; + + ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL); + + ELM_SET_WIDTYPE(widtype, "slider"); + elm_widget_type_set(obj, "slider"); + elm_widget_sub_object_add(parent, obj); + elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL); + elm_widget_data_set(obj, wd); + elm_widget_del_hook_set(obj, _del_hook); + elm_widget_theme_hook_set(obj, _theme_hook); + elm_widget_disable_hook_set(obj, _disable_hook); + elm_widget_can_focus_set(obj, EINA_TRUE); + elm_widget_event_hook_set(obj, _event_hook); + elm_widget_text_set_hook_set(obj, _elm_slider_label_set); + elm_widget_text_get_hook_set(obj, _elm_slider_label_get); + elm_widget_content_set_hook_set(obj, _content_set_hook); + elm_widget_content_get_hook_set(obj, _content_get_hook); + elm_widget_content_unset_hook_set(obj, _content_unset_hook); + + wd->horizontal = EINA_TRUE; + wd->indicator_show = EINA_TRUE; + wd->val = 0.0; + wd->val_min = 0.0; + wd->val_max = 1.0; + wd->labels = eina_hash_string_superfast_new(_hash_labels_free_cb); + + wd->slider = edje_object_add(e); + _elm_theme_object_set(obj, wd->slider, "slider", "horizontal", "default"); + elm_widget_resize_object_set(obj, wd->slider); + edje_object_signal_callback_add(wd->slider, "drag", "*", _drag, obj); + edje_object_signal_callback_add(wd->slider, "drag,start", "*", _drag_start, obj); + edje_object_signal_callback_add(wd->slider, "drag,stop", "*", _drag_stop, obj); + edje_object_signal_callback_add(wd->slider, "drag,step", "*", _drag_step, obj); + edje_object_signal_callback_add(wd->slider, "drag,page", "*", _drag_stop, obj); + // edje_object_signal_callback_add(wd->slider, "drag,set", "*", _drag_stop, obj); + edje_object_part_drag_value_set(wd->slider, "elm.dragable.slider", 0.0, 0.0); + + wd->spacer = evas_object_rectangle_add(e); + evas_object_color_set(wd->spacer, 0, 0, 0, 0); + evas_object_pass_events_set(wd->spacer, EINA_TRUE); + elm_widget_sub_object_add(obj, wd->spacer); + edje_object_part_swallow(wd->slider, "elm.swallow.bar", wd->spacer); + evas_object_event_callback_add(wd->spacer, EVAS_CALLBACK_MOUSE_DOWN, _spacer_down_cb, obj); + evas_object_event_callback_add(wd->spacer, EVAS_CALLBACK_MOUSE_MOVE, _spacer_move_cb, obj); + evas_object_event_callback_add(wd->spacer, EVAS_CALLBACK_MOUSE_UP, _spacer_up_cb, obj); + evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj); + + _mirrored_set(obj, elm_widget_mirrored_get(obj)); + _sizing_eval(obj); + + // TODO: convert Elementary to subclassing of Evas_Smart_Class + // TODO: and save some bytes, making descriptions per-class and not instance! + evas_object_smart_callbacks_descriptions_set(obj, _signals); + return obj; } -/** - * Set the length of the dragable region of the slider - * - * This sets the minimum width or height (depending on orientation) of the - * area of the slider that allows the slider to be dragged around. This in - * turn affects the objects minimum size (along with icon label and unit - * text). Note that this will also get multiplied by the scale factor. - * - * @param obj The slider object - * @param size The length of the slider area - * - * @ingroup Slider - */ EAPI void elm_slider_span_size_set(Evas_Object *obj, Evas_Coord size) { @@ -709,19 +887,6 @@ elm_slider_span_size_set(Evas_Object *obj, Evas_Coord size) _sizing_eval(obj); } -/** - * Get the length of the dragable region of the slider - * - * This gets the minimum width or height (depending on orientation) of - * the area of the slider that allows the slider to be dragged - * around. Note that this will also get multiplied by the scale - * factor. - * - * @param obj The slider object - * @return The length of the slider area - * - * @ingroup Slider - */ EAPI Evas_Coord elm_slider_span_size_get(const Evas_Object *obj) { @@ -731,19 +896,6 @@ elm_slider_span_size_get(const Evas_Object *obj) return wd->size; } -/** - * Set the format string of the unit area - * - * If NULL, this disabls the unit area display. If not it sets the format - * string for the unit text. The unit text is provided a floating point - * value, so the unit text can display up to 1 floating point value. Note that - * this is optional. Use a format string such as "%1.2f meters" for example. - * - * @param obj The slider object - * @param units The format string for the units display - * - * @ingroup Slider - */ EAPI void elm_slider_unit_format_set(Evas_Object *obj, const char *units) { @@ -761,23 +913,11 @@ elm_slider_unit_format_set(Evas_Object *obj, const char *units) edje_object_signal_emit(wd->slider, "elm,state,units,hidden", "elm"); edje_object_message_signal_process(wd->slider); } + _min_max_set(obj); _units_set(obj); _sizing_eval(obj); } -/** - * Get the format string for the unit area - * - * The slider may also display a value (the value of the slider) somewhere - * (for example above the slider knob that is dragged around). This sets the - * format string for this. See elm_slider_unit_format_set() for more - * information on how this works. - * - * @param obj The slider object - * @return The format string for the unit display. - * - * @ingroup Slider - */ EAPI const char * elm_slider_unit_format_get(const Evas_Object *obj) { @@ -787,19 +927,6 @@ elm_slider_unit_format_get(const Evas_Object *obj) return wd->units; } -/** - * Set the format string for the indicator area - * - * The slider may also display a value (the value of the slider) somewhere - * (for example above the slider knob that is dragged around). This sets the - * format string for this. See elm_slider_unit_format_set() for more - * information on how this works. - * - * @param obj The slider object - * @param indicator The format string for the indicator display - * - * @ingroup Slider - */ EAPI void elm_slider_indicator_format_set(Evas_Object *obj, const char *indicator) { @@ -810,19 +937,6 @@ elm_slider_indicator_format_set(Evas_Object *obj, const char *indicator) _indicator_set(obj); } -/** - * Get the format string for the indicator area - * - * The slider may also display a value (the value of the slider) somewhere - * (for example above the slider knob that is dragged around). This sets the - * format string for this. See elm_slider_indicator_format_set() for more - * information on how this works. - * - * @param obj The slider object - * @return The format string for the indicator display. - * - * @ingroup Slider - */ EAPI const char * elm_slider_indicator_format_get(const Evas_Object *obj) { @@ -832,14 +946,6 @@ elm_slider_indicator_format_get(const Evas_Object *obj) return wd->indicator; } -/** - * Set orientation of the slider - * - * @param obj The slider object - * @param horizontal If set, the slider will be horizontal - * - * @ingroup Slider - */ EAPI void elm_slider_horizontal_set(Evas_Object *obj, Eina_Bool horizontal) { @@ -852,14 +958,6 @@ elm_slider_horizontal_set(Evas_Object *obj, Eina_Bool horizontal) _theme_hook(obj); } -/** - * Get orientation of the slider - * - * @param obj The slider object - * @return If @c EINA_TRUE the slider will be horizontal, else it is - * vertical. - * @ingroup Slider - */ EAPI Eina_Bool elm_slider_horizontal_get(const Evas_Object *obj) { @@ -869,17 +967,6 @@ elm_slider_horizontal_get(const Evas_Object *obj) return wd->horizontal; } -/** - * Set the minimum and maximum values for the slider - * - * Maximum mut be greater than minimum. - * - * @param obj The slider object - * @param min The minimum value - * @param max The maximum value - * - * @ingroup Slider - */ EAPI void elm_slider_min_max_set(Evas_Object *obj, double min, double max) { @@ -891,20 +978,12 @@ elm_slider_min_max_set(Evas_Object *obj, double min, double max) wd->val_max = max; if (wd->val < wd->val_min) wd->val = wd->val_min; if (wd->val > wd->val_max) wd->val = wd->val_max; + _min_max_set(obj); _val_set(obj); _units_set(obj); _indicator_set(obj); } -/** - * Get the minimum and maximum values for the slider - * - * @param obj The slider object - * @param min The pointer to store minimum value, may be @c NULL. - * @param max The pointer to store maximum value, may be @c NULL. - * - * @ingroup Slider - */ EAPI void elm_slider_min_max_get(const Evas_Object *obj, double *min, double *max) { @@ -917,14 +996,6 @@ elm_slider_min_max_get(const Evas_Object *obj, double *min, double *max) if (max) *max = wd->val_max; } -/** - * Set the value the slider indicates - * - * @param obj The slider object - * @param val The value (must be between min and max for the slider) - * - * @ingroup Slider - */ EAPI void elm_slider_value_set(Evas_Object *obj, double val) { @@ -935,19 +1006,12 @@ elm_slider_value_set(Evas_Object *obj, double val) wd->val = val; if (wd->val < wd->val_min) wd->val = wd->val_min; if (wd->val > wd->val_max) wd->val = wd->val_max; + edje_object_signal_emit(wd->slider, "elm,state,drag", "elm"); _val_set(obj); _units_set(obj); _indicator_set(obj); } -/** - * Get the value the slider has - * - * @param obj The slider object - * @return The value of the slider - * - * @ingroup Slider - */ EAPI double elm_slider_value_get(const Evas_Object *obj) { @@ -957,19 +1021,6 @@ elm_slider_value_get(const Evas_Object *obj) return wd->val; } -/** - * Invert the slider display - * - * Normally the slider will display and interpret values from low to high - * and when horizontal that is left to right. When vertical that is top - * to bottom. This inverts this (so from right to left or bottom to top) if - * inverted is set to 1. - * - * @param obj The slider object - * @param inverted The inverted flag. 1 == inverted, 0 == normal - * - * @ingroup Slider - */ EAPI void elm_slider_inverted_set(Evas_Object *obj, Eina_Bool inverted) { @@ -989,13 +1040,6 @@ elm_slider_inverted_set(Evas_Object *obj, Eina_Bool inverted) _indicator_set(obj); } -/** - * Get if the slider display is inverted (backwards) - * - * @param obj The slider object - * @return If @c EINA_TRUE the slider will be inverted. - * @ingroup Slider - */ EAPI Eina_Bool elm_slider_inverted_get(const Evas_Object *obj) { @@ -1005,125 +1049,29 @@ elm_slider_inverted_get(const Evas_Object *obj) return wd->inverted; } -/** - * Set the format function pointer for the inducator area - * - * Set the callback function to format the indicator string. - * See elm_slider_indicator_format_set() for more info on how this works. - * - * @param obj The slider object - * @param indicator The format string for the indicator display - * @param func The indicator format function - * - * @ingroup Slider - */ EAPI void -elm_slider_indicator_format_function_set(Evas_Object *obj, const char *(*func)(double val)) +elm_slider_indicator_format_function_set(Evas_Object *obj, char *(*func)(double val), void (*free_func)(char *str)) { ELM_CHECK_WIDTYPE(obj, widtype); Widget_Data *wd = elm_widget_data_get(obj); if (!wd) return; wd->indicator_format_func = func; + wd->indicator_format_free = free_func; _indicator_set(obj); } -/** - * Set the end object (rightmost widget) of the slider object. - * - * Once the end object is set, a previously set one will be deleted. - * If you want to keep that old content object, use the - * elm_button_end_unset() function. - * - * @param obj The slider object - * @param end The end object - * - * @note If the object being set does not have minimum size hints set, - * it won't get properly displayed. - * - * @ingroup Slider - */ EAPI void -elm_slider_end_set(Evas_Object *obj, Evas_Object *end) +elm_slider_units_format_function_set(Evas_Object *obj, char *(*func)(double val), void (*free_func)(char *str)) { ELM_CHECK_WIDTYPE(obj, widtype); Widget_Data *wd = elm_widget_data_get(obj); if (!wd) return; - if (wd->end == end) return; - if (wd->end) evas_object_del(wd->end); - wd->end = end; - if (end) - { - elm_widget_sub_object_add(obj, end); - evas_object_event_callback_add(end, EVAS_CALLBACK_CHANGED_SIZE_HINTS, - _changed_size_hints, obj); - edje_object_part_swallow(wd->slider, "elm.swallow.end", end); - edje_object_signal_emit(wd->slider, "elm,state,end,visible", "elm"); - edje_object_message_signal_process(wd->slider); - } - _sizing_eval(obj); -} - -/** - * Unset the rightmost widget of the slider, unparenting and - * returning it. - * - * @param obj The slider object - * @return the previously set end sub-object of this slider, on - * success. - * - * @see elm_slider_end_set() - * - * @ingroup Slider - */ -EAPI Evas_Object * -elm_slider_end_unset(Evas_Object *obj) -{ - ELM_CHECK_WIDTYPE(obj, widtype) NULL; - Widget_Data *wd = elm_widget_data_get(obj); - Evas_Object *ret = NULL; - if (!wd) return NULL; - if (wd->end) - { - elm_widget_sub_object_del(obj, wd->end); - ret = wd->end; - edje_object_part_unswallow(wd->slider, wd->end); - edje_object_signal_emit(wd->slider, "elm,state,end,hidden", "elm"); - wd->end = NULL; - _sizing_eval(obj); - } - return ret; -} - -/** - * Get the end icon object of the slider object. This object is owned - * by the scrolled entry and should not be modified. - * - * @param obj The slider object - * @return The end icon object - * - * @ingroup Slider - */ -EAPI Evas_Object * -elm_slider_end_get(const Evas_Object *obj) -{ - ELM_CHECK_WIDTYPE(obj, widtype) NULL; - Widget_Data *wd = elm_widget_data_get(obj); - if (!wd) return NULL; - return wd->end; + wd->units_format_func = func; + wd->units_format_free = free_func; + _min_max_set(obj); + _units_set(obj); } -/** - * Set whether to the slider indicator (augmented knob) at all. - * - * @param obj The slider object - * @param show @c EINA_TRUE will make it show it, @c EINA_FALSE will - * let the knob alwayes at default size. - * - * @note It will conflict with elm_slider_indicator_format_set(), if - * you wanted those effects. - * - * @ingroup Slider - */ EAPI void elm_slider_indicator_show_set(Evas_Object *obj, Eina_Bool show) { @@ -1139,16 +1087,6 @@ elm_slider_indicator_show_set(Evas_Object *obj, Eina_Bool show) } } -/** - * Get the state of indicator in the slider (if it's being shown or - * not). - * - * @param obj The slider object - * @return @c EINA_TRUE if the indicator is being shown, @c EINA_FALSE - * otherwise. - * - * @ingroup Slider - */ EAPI Eina_Bool elm_slider_indicator_show_get(const Evas_Object *obj) {