1 #include <Elementary.h>
5 typedef struct _Widget_Data Widget_Data;
9 Evas_Object *as; // actionslider
10 Evas_Object *drag_button_base;
11 Elm_Actionslider_Pos magnet_position, enabled_position;
12 const char *text_left, *text_right, *text_center;
13 const char *indicator_label;
14 Ecore_Animator *button_animator;
15 double final_position;
16 Eina_Bool mouse_down : 1;
19 static const char *widtype = NULL;
21 static const char SIG_CHANGED[] = "pos_changed";
22 static const char SIG_SELECTED[] = "selected";
24 static const Evas_Smart_Cb_Description _signals[] =
33 _del_hook(Evas_Object *obj)
35 Widget_Data *wd = elm_widget_data_get(obj);
37 if (wd->drag_button_base)
39 evas_object_del(wd->drag_button_base);
40 wd->drag_button_base = NULL;
42 if (wd->text_left) eina_stringshare_del(wd->text_left);
43 if (wd->text_right) eina_stringshare_del(wd->text_right);
44 if (wd->text_center) eina_stringshare_del(wd->text_center);
45 if (wd->indicator_label) eina_stringshare_del(wd->indicator_label);
49 static Elm_Actionslider_Pos
50 _get_pos_by_orientation(const Evas_Object *obj, Elm_Actionslider_Pos pos)
52 if (elm_widget_mirrored_get(obj))
56 case ELM_ACTIONSLIDER_LEFT:
57 pos = ELM_ACTIONSLIDER_RIGHT;
59 case ELM_ACTIONSLIDER_RIGHT:
60 pos = ELM_ACTIONSLIDER_LEFT;
70 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
72 Widget_Data *wd = elm_widget_data_get(obj);
76 if (edje_object_mirrored_get(wd->as) == rtl)
79 edje_object_mirrored_set(wd->as, rtl);
80 if (!elm_widget_mirrored_get(obj))
82 edje_object_part_text_set(wd->as, "elm.text.left", wd->text_left);
83 edje_object_part_text_set(wd->as, "elm.text.right", wd->text_right);
87 edje_object_part_text_set(wd->as, "elm.text.left", wd->text_right);
88 edje_object_part_text_set(wd->as, "elm.text.right", wd->text_left);
90 edje_object_part_drag_value_get(wd->as, "elm.drag_button_base", &pos, NULL);
91 edje_object_part_drag_value_set(wd->as, "elm.drag_button_base", 1.0 - pos, 0.5);
95 _sizing_eval(Evas_Object *obj)
97 Widget_Data *wd = elm_widget_data_get(obj);
98 Evas_Coord minw = -1, minh = -1;
101 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
102 evas_object_size_hint_min_set(wd->drag_button_base, minw, minh);
103 evas_object_size_hint_max_set(wd->drag_button_base, -1, -1);
107 elm_coords_finger_size_adjust(3, &minw, 1, &minh);
108 edje_object_size_min_restricted_calc(wd->as, &minw, &minh, minw, minh);
109 evas_object_size_hint_min_set(obj, minw, minh);
110 evas_object_size_hint_max_set(obj, -1, -1);
114 _theme_hook(Evas_Object *obj)
116 Widget_Data *wd = elm_widget_data_get(obj);
118 _elm_widget_mirrored_reload(obj);
119 if (!edje_object_part_swallow_get(wd->as, "elm.drag_button_base"))
120 edje_object_part_unswallow(wd->as, wd->drag_button_base);
122 _elm_theme_object_set(obj, wd->as, "actionslider",
123 "base", elm_widget_style_get(obj));
124 _elm_theme_object_set(obj, wd->drag_button_base, "actionslider",
125 "drag_button", elm_widget_style_get(obj));
126 edje_object_part_swallow(wd->as, "elm.drag_button_base", wd->drag_button_base);
128 _mirrored_set(obj, elm_widget_mirrored_get(obj));
129 edje_object_part_text_set(wd->as, "elm.text.center", wd->text_center);
130 edje_object_part_text_set(wd->as, "elm.text.indicator", wd->indicator_label);
131 edje_object_message_signal_process(wd->as);
136 _drag_button_down_cb(void *data, Evas_Object *o __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
138 Widget_Data *wd = elm_widget_data_get((Evas_Object *) data);
140 wd->mouse_down = EINA_TRUE;
144 _drag_button_move_cb(void *data, Evas_Object *o __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
146 Evas_Object *obj = (Evas_Object *) data;
147 Widget_Data *wd = elm_widget_data_get(obj);
151 if (!wd->mouse_down) return;
152 edje_object_part_drag_value_get(wd->as, "elm.drag_button_base", &pos, NULL);
154 evas_object_smart_callback_call(obj, SIG_CHANGED,
155 (void *) ((!elm_widget_mirrored_get(obj)) ?
158 evas_object_smart_callback_call(obj, SIG_CHANGED,
159 (void *) ((!elm_widget_mirrored_get(obj)) ?
161 else if (pos >= 0.45 && pos <= 0.55)
162 evas_object_smart_callback_call(obj, SIG_CHANGED, (void *)"center");
166 _button_animation(void *data)
168 Evas_Object *obj = data;
169 Widget_Data *wd = elm_widget_data_get(obj);
170 double cur_position = 0.0, new_position = 0.0;
171 double move_amount = 0.05;
172 Eina_Bool flag_finish_animation = EINA_FALSE;
173 if (!wd) return ECORE_CALLBACK_CANCEL;
175 edje_object_part_drag_value_get(wd->as,
176 "elm.drag_button_base", &cur_position, NULL);
178 double adjusted_final;
179 adjusted_final = (!elm_widget_mirrored_get(obj)) ?
180 wd->final_position : 1.0 - wd->final_position;
181 if ((adjusted_final == 0.0) ||
182 (adjusted_final == 0.5 && cur_position >= adjusted_final))
184 new_position = cur_position - move_amount;
185 if (new_position <= adjusted_final)
187 new_position = adjusted_final;
188 flag_finish_animation = EINA_TRUE;
191 else if ((adjusted_final == 1.0) ||
192 (adjusted_final == 0.5 && cur_position < adjusted_final))
194 new_position = cur_position + move_amount;
195 if (new_position >= adjusted_final)
197 new_position = adjusted_final;
198 flag_finish_animation = EINA_TRUE;
201 edje_object_part_drag_value_set(wd->as,
202 "elm.drag_button_base", new_position, 0.5);
205 if (flag_finish_animation)
207 if ((!wd->final_position) &&
208 (wd->enabled_position & ELM_ACTIONSLIDER_LEFT))
209 evas_object_smart_callback_call(data, SIG_SELECTED,
210 (void *)wd->text_left);
211 else if ((wd->final_position == 0.5) &&
212 (wd->enabled_position & ELM_ACTIONSLIDER_CENTER))
213 evas_object_smart_callback_call(data, SIG_SELECTED,
214 (void *)wd->text_center);
215 else if ((wd->final_position == 1) &&
216 (wd->enabled_position & ELM_ACTIONSLIDER_RIGHT))
217 evas_object_smart_callback_call(data, SIG_SELECTED,
218 (void *)wd->text_right);
219 wd->button_animator = NULL;
220 return ECORE_CALLBACK_CANCEL;
222 return ECORE_CALLBACK_RENEW;
226 _drag_button_up_cb(void *data, Evas_Object *o __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
228 Evas_Object *obj = (Evas_Object *) data;
229 Widget_Data *wd = elm_widget_data_get(obj);
230 double position = 0.0;
233 wd->mouse_down = EINA_FALSE;
235 edje_object_part_drag_value_get(wd->as, "elm.drag_button_base",
238 if ((wd->enabled_position & ELM_ACTIONSLIDER_LEFT) &&
239 ((!elm_widget_mirrored_get(obj) && position == 0.0) ||
240 (elm_widget_mirrored_get(obj) && position == 1.0)))
242 wd->final_position = 0;
243 evas_object_smart_callback_call(data, SIG_SELECTED,
244 (void *) wd->text_left);
247 if (position >= 0.45 && position <= 0.55 &&
248 (wd->enabled_position & ELM_ACTIONSLIDER_CENTER))
250 wd->final_position = 0.5;
251 evas_object_smart_callback_call(data, SIG_SELECTED,
252 (void *)wd->text_center);
253 if (wd->button_animator) ecore_animator_del(wd->button_animator);
254 wd->button_animator = ecore_animator_add(_button_animation, data);
257 if ((wd->enabled_position & ELM_ACTIONSLIDER_RIGHT) &&
258 ((!elm_widget_mirrored_get(obj) && position == 1.0) ||
259 (elm_widget_mirrored_get(obj) && position == 0.0)))
261 wd->final_position = 1;
262 evas_object_smart_callback_call(data, SIG_SELECTED,
263 (void *) wd->text_right);
267 if (wd->magnet_position == ELM_ACTIONSLIDER_NONE) return;
269 #define _FINAL_POS_BY_ORIENTATION(x) (x)
270 #define _POS_BY_ORIENTATION(x) \
271 ((!elm_widget_mirrored_get(obj)) ? \
274 position = _POS_BY_ORIENTATION(position);
278 if (wd->magnet_position & ELM_ACTIONSLIDER_LEFT)
279 wd->final_position = _FINAL_POS_BY_ORIENTATION(0);
280 else if (wd->magnet_position & ELM_ACTIONSLIDER_CENTER)
281 wd->final_position = 0.5;
282 else if (wd->magnet_position & ELM_ACTIONSLIDER_RIGHT)
283 wd->final_position = _FINAL_POS_BY_ORIENTATION(1);
285 else if ((position >= 0.3) && (position <= 0.7))
287 if (wd->magnet_position & ELM_ACTIONSLIDER_CENTER)
288 wd->final_position = 0.5;
289 else if (position < 0.5)
291 if (wd->magnet_position & ELM_ACTIONSLIDER_LEFT)
292 wd->final_position = _FINAL_POS_BY_ORIENTATION(0);
294 wd->final_position = _FINAL_POS_BY_ORIENTATION(1);
298 if (wd->magnet_position & ELM_ACTIONSLIDER_RIGHT)
299 wd->final_position = _FINAL_POS_BY_ORIENTATION(1);
301 wd->final_position = _FINAL_POS_BY_ORIENTATION(0);
306 if (wd->magnet_position & ELM_ACTIONSLIDER_RIGHT)
307 wd->final_position = _FINAL_POS_BY_ORIENTATION(1);
308 else if (wd->magnet_position & ELM_ACTIONSLIDER_CENTER)
309 wd->final_position = 0.5;
311 wd->final_position = _FINAL_POS_BY_ORIENTATION(0);
313 if (wd->button_animator) ecore_animator_del(wd->button_animator);
314 wd->button_animator = ecore_animator_add(_button_animation, data);
316 #undef _FINAL_POS_BY_ORIENTATION
320 _elm_actionslider_label_set(Evas_Object *obj, const char *item, const char *label)
322 ELM_CHECK_WIDTYPE(obj, widtype);
323 Widget_Data *wd = elm_widget_data_get(obj);
326 if (!item || !strcmp(item, "indicator"))
328 eina_stringshare_replace(&wd->indicator_label, label);
329 edje_object_part_text_set(wd->as, "elm.text.indicator",
330 wd->indicator_label);
332 else if (!strcmp(item, "left"))
334 eina_stringshare_replace(&wd->text_left, label);
335 if (!elm_widget_mirrored_get(obj))
336 edje_object_part_text_set(wd->as, "elm.text.left", wd->text_left);
338 edje_object_part_text_set(wd->as, "elm.text.right", wd->text_left);
340 else if (!strcmp(item, "center"))
342 eina_stringshare_replace(&wd->text_center, label);
343 edje_object_part_text_set(wd->as, "elm.text.center", wd->text_center);
345 else if (!strcmp(item, "right"))
347 eina_stringshare_replace(&wd->text_right, label);
348 if (!elm_widget_mirrored_get(obj))
349 edje_object_part_text_set(wd->as, "elm.text.right", wd->text_right);
351 edje_object_part_text_set(wd->as, "elm.text.left", wd->text_right);
356 _elm_actionslider_label_get(const Evas_Object *obj, const char *item)
358 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
359 Widget_Data *wd = elm_widget_data_get(obj);
360 if (!wd) return NULL;
362 if (!item || !strcmp(item, "indicator"))
363 return wd->indicator_label;
364 else if (!strcmp(item, "left"))
365 return wd->text_left;
366 else if (!strcmp(item, "center"))
367 return wd->text_center;
368 else if (!strcmp(item, "right"))
369 return wd->text_right;
375 elm_actionslider_add(Evas_Object *parent)
381 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
383 ELM_SET_WIDTYPE(widtype, "actionslider");
384 elm_widget_type_set(obj, "actionslider");
385 elm_widget_sub_object_add(parent, obj);
386 elm_widget_data_set(obj, wd);
387 elm_widget_del_hook_set(obj, _del_hook);
388 elm_widget_theme_hook_set(obj, _theme_hook);
389 elm_widget_text_set_hook_set(obj, _elm_actionslider_label_set);
390 elm_widget_text_get_hook_set(obj, _elm_actionslider_label_get);
392 wd->mouse_down = EINA_FALSE;
393 wd->enabled_position = ELM_ACTIONSLIDER_ALL;
395 wd->as = edje_object_add(e);
396 _elm_theme_object_set(obj, wd->as, "actionslider", "base", "default");
397 elm_widget_resize_object_set(obj, wd->as);
399 wd->drag_button_base = evas_object_rectangle_add(e);
400 evas_object_color_set(wd->drag_button_base, 0, 0, 0, 0);
401 edje_object_part_swallow(wd->as, "elm.drag_button_base", wd->drag_button_base);
403 edje_object_signal_callback_add(wd->as,
404 "elm.drag_button,mouse,up", "",
405 _drag_button_up_cb, obj);
406 edje_object_signal_callback_add(wd->as,
407 "elm.drag_button,mouse,down", "",
408 _drag_button_down_cb, obj);
409 edje_object_signal_callback_add(wd->as,
410 "elm.drag_button,mouse,move", "",
411 _drag_button_move_cb, obj);
413 evas_object_smart_callbacks_descriptions_set(obj, _signals);
414 _mirrored_set(obj, elm_widget_mirrored_get(obj));
420 elm_actionslider_indicator_pos_set(Evas_Object *obj, Elm_Actionslider_Pos pos)
422 ELM_CHECK_WIDTYPE(obj, widtype);
423 Widget_Data *wd = elm_widget_data_get(obj);
424 double position = 0.0;
426 pos = _get_pos_by_orientation(obj, pos);
427 if (pos == ELM_ACTIONSLIDER_CENTER) position = 0.5;
428 else if (pos == ELM_ACTIONSLIDER_RIGHT) position = 1.0;
429 edje_object_part_drag_value_set(wd->as, "elm.drag_button_base", position, 0.5);
432 EAPI Elm_Actionslider_Pos
433 elm_actionslider_indicator_pos_get(const Evas_Object *obj)
435 ELM_CHECK_WIDTYPE(obj, widtype) ELM_ACTIONSLIDER_NONE;
436 Widget_Data *wd = elm_widget_data_get(obj);
438 if (!wd) return ELM_ACTIONSLIDER_NONE;
440 edje_object_part_drag_value_get(wd->as, "elm.drag_button_base", &position, NULL);
442 return _get_pos_by_orientation(obj, ELM_ACTIONSLIDER_LEFT);
443 else if (position < 0.7)
444 return ELM_ACTIONSLIDER_CENTER;
446 return _get_pos_by_orientation(obj, ELM_ACTIONSLIDER_RIGHT);
450 elm_actionslider_magnet_pos_set(Evas_Object *obj, Elm_Actionslider_Pos pos)
452 ELM_CHECK_WIDTYPE(obj, widtype);
453 Widget_Data *wd = elm_widget_data_get(obj);
455 wd->magnet_position = pos;
458 EAPI Elm_Actionslider_Pos
459 elm_actionslider_magnet_pos_get(const Evas_Object *obj)
461 ELM_CHECK_WIDTYPE(obj, widtype) ELM_ACTIONSLIDER_NONE;
462 Widget_Data *wd = elm_widget_data_get(obj);
463 if (!wd) return ELM_ACTIONSLIDER_NONE;
464 return wd->magnet_position;
468 elm_actionslider_enabled_pos_set(Evas_Object *obj, Elm_Actionslider_Pos pos)
470 ELM_CHECK_WIDTYPE(obj, widtype);
471 Widget_Data *wd = elm_widget_data_get(obj);
473 wd->enabled_position = pos;
476 EAPI Elm_Actionslider_Pos
477 elm_actionslider_enabled_pos_get(const Evas_Object *obj)
479 ELM_CHECK_WIDTYPE(obj, widtype) ELM_ACTIONSLIDER_NONE;
480 Widget_Data *wd = elm_widget_data_get(obj);
481 if (!wd) return ELM_ACTIONSLIDER_NONE;
482 return wd->enabled_position;
486 elm_actionslider_selected_label_get(const Evas_Object *obj)
488 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
489 Widget_Data *wd = elm_widget_data_get(obj);
490 if (!wd) return NULL;
492 if ((wd->final_position == 0.0) &&
493 (wd->enabled_position & ELM_ACTIONSLIDER_LEFT))
494 return wd->text_left;
496 if ((wd->final_position == 0.5) &&
497 (wd->enabled_position & ELM_ACTIONSLIDER_CENTER))
498 return wd->text_center;
500 if ((wd->final_position == 1.0) &&
501 (wd->enabled_position & ELM_ACTIONSLIDER_RIGHT))
502 return wd->text_right;