2 * @addtogroup Actionslider Actionslider
4 * A magnet slider is a switcher for 3 labels with customizable
5 * magnet properties. When the position is set with magnet, the knob
6 * will be moved to it if it's nearest the magnetized position.
9 * "selected" - when user selects a position (the label is passed as
11 * "pos_changed" - when a button reaches to the special position like
12 * "left", "right" and "center".
15 #include <Elementary.h>
19 typedef struct _Widget_Data Widget_Data;
23 Evas_Object *as; // actionslider
24 Evas_Object *drag_button_base;
25 Elm_Actionslider_Pos magnet_position, enabled_position;
26 const char *text_left, *text_right, *text_center;
27 const char *indicator_label;
28 Ecore_Animator *button_animator;
29 double final_position;
30 Eina_Bool mouse_down : 1;
33 static const char *widtype = NULL;
35 #define SIG_CHANGED "pos_changed"
36 #define SIG_SELECTED "selected"
38 static const Evas_Smart_Cb_Description _signals[] =
47 _del_hook(Evas_Object *obj)
49 Widget_Data *wd = elm_widget_data_get(obj);
51 if (wd->drag_button_base)
53 evas_object_del(wd->drag_button_base);
54 wd->drag_button_base = NULL;
56 if (wd->text_left) eina_stringshare_del(wd->text_left);
57 if (wd->text_right) eina_stringshare_del(wd->text_right);
58 if (wd->text_center) eina_stringshare_del(wd->text_center);
59 if (wd->indicator_label) eina_stringshare_del(wd->indicator_label);
63 static Elm_Actionslider_Pos
64 _get_pos_by_orientation(const Evas_Object *obj, Elm_Actionslider_Pos pos)
66 if (elm_widget_mirrored_get(obj))
70 case ELM_ACTIONSLIDER_LEFT:
71 pos = ELM_ACTIONSLIDER_RIGHT;
73 case ELM_ACTIONSLIDER_RIGHT:
74 pos = ELM_ACTIONSLIDER_LEFT;
84 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
86 Widget_Data *wd = elm_widget_data_get(obj);
90 if (edje_object_mirrored_get(wd->as) == rtl)
93 edje_object_mirrored_set(wd->as, rtl);
94 if (!elm_widget_mirrored_get(obj))
96 edje_object_part_text_set(wd->as, "elm.text.left", wd->text_left);
97 edje_object_part_text_set(wd->as, "elm.text.right", wd->text_right);
101 edje_object_part_text_set(wd->as, "elm.text.left", wd->text_right);
102 edje_object_part_text_set(wd->as, "elm.text.right", wd->text_left);
104 edje_object_part_drag_value_get(wd->as, "elm.drag_button_base", &pos, NULL);
105 edje_object_part_drag_value_set(wd->as, "elm.drag_button_base", 1.0 - pos, 0.5);
109 _sizing_eval(Evas_Object *obj)
111 Widget_Data *wd = elm_widget_data_get(obj);
112 Evas_Coord minw = -1, minh = -1;
115 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
116 evas_object_size_hint_min_set(wd->drag_button_base, minw, minh);
117 evas_object_size_hint_max_set(wd->drag_button_base, -1, -1);
121 elm_coords_finger_size_adjust(3, &minw, 1, &minh);
122 edje_object_size_min_restricted_calc(wd->as, &minw, &minh, minw, minh);
123 evas_object_size_hint_min_set(obj, minw, minh);
124 evas_object_size_hint_max_set(obj, -1, -1);
128 _theme_hook(Evas_Object *obj)
130 Widget_Data *wd = elm_widget_data_get(obj);
132 if (!edje_object_part_swallow_get(wd->as, "elm.drag_button_base"))
133 edje_object_part_unswallow(wd->as, wd->drag_button_base);
135 _elm_theme_object_set(obj, wd->as, "actionslider",
136 "base", elm_widget_style_get(obj));
137 _elm_theme_object_set(obj, wd->drag_button_base, "actionslider",
138 "drag_button", elm_widget_style_get(obj));
139 edje_object_part_swallow(wd->as, "elm.drag_button_base", wd->drag_button_base);
141 _mirrored_set(obj, elm_widget_mirrored_get(obj));
142 edje_object_part_text_set(wd->as, "elm.text.center", wd->text_center);
143 edje_object_part_text_set(wd->as, "elm.text.indicator", wd->indicator_label);
144 edje_object_message_signal_process(wd->as);
149 _drag_button_down_cb(void *data, Evas_Object *o __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
151 Widget_Data *wd = elm_widget_data_get((Evas_Object *) data);
153 wd->mouse_down = EINA_TRUE;
157 _drag_button_move_cb(void *data, Evas_Object *o __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
159 Evas_Object *obj = (Evas_Object *) data;
160 Widget_Data *wd = elm_widget_data_get(obj);
164 if (!wd->mouse_down) return;
165 edje_object_part_drag_value_get(wd->as, "elm.drag_button_base", &pos, NULL);
167 evas_object_smart_callback_call(obj, SIG_CHANGED,
168 (void *) ((!elm_widget_mirrored_get(obj)) ?
171 evas_object_smart_callback_call(obj, SIG_CHANGED,
172 (void *) ((!elm_widget_mirrored_get(obj)) ?
174 else if (pos >= 0.45 && pos <= 0.55)
175 evas_object_smart_callback_call(obj, SIG_CHANGED, (void *)"center");
179 _button_animation(void *data)
181 Evas_Object *obj = data;
182 Widget_Data *wd = elm_widget_data_get(obj);
183 double cur_position = 0.0, new_position = 0.0;
184 double move_amount = 0.05;
185 Eina_Bool flag_finish_animation = EINA_FALSE;
186 if (!wd) return EINA_FALSE;
188 edje_object_part_drag_value_get(wd->as,
189 "elm.drag_button_base", &cur_position, NULL);
191 double adjusted_final;
192 adjusted_final = (!elm_widget_mirrored_get(obj)) ?
193 wd->final_position : 1.0 - wd->final_position;
194 if ((adjusted_final == 0.0) ||
195 (adjusted_final == 0.5 && cur_position >= adjusted_final))
197 new_position = cur_position - move_amount;
198 if (new_position <= adjusted_final)
200 new_position = adjusted_final;
201 flag_finish_animation = EINA_TRUE;
204 else if ((adjusted_final == 1.0) ||
205 (adjusted_final == 0.5 && cur_position < adjusted_final))
207 new_position = cur_position + move_amount;
208 if (new_position >= adjusted_final)
210 new_position = adjusted_final;
211 flag_finish_animation = EINA_TRUE;
214 edje_object_part_drag_value_set(wd->as,
215 "elm.drag_button_base", new_position, 0.5);
218 if (flag_finish_animation)
220 if ((!wd->final_position) &&
221 (wd->enabled_position & ELM_ACTIONSLIDER_LEFT))
222 evas_object_smart_callback_call(data, SIG_SELECTED,
223 (void *)wd->text_left);
224 else if ((wd->final_position == 0.5) &&
225 (wd->enabled_position & ELM_ACTIONSLIDER_CENTER))
226 evas_object_smart_callback_call(data, SIG_SELECTED,
227 (void *)wd->text_center);
228 else if ((wd->final_position == 1) &&
229 (wd->enabled_position & ELM_ACTIONSLIDER_RIGHT))
230 evas_object_smart_callback_call(data, SIG_SELECTED,
231 (void *)wd->text_right);
238 _drag_button_up_cb(void *data, Evas_Object *o __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
240 Evas_Object *obj = (Evas_Object *) data;
241 Widget_Data *wd = elm_widget_data_get(obj);
242 double position = 0.0;
245 wd->mouse_down = EINA_FALSE;
247 edje_object_part_drag_value_get(wd->as, "elm.drag_button_base",
250 if ((wd->enabled_position & ELM_ACTIONSLIDER_LEFT) &&
251 ((!elm_widget_mirrored_get(obj) && position == 0.0) ||
252 (elm_widget_mirrored_get(obj) && position == 1.0)))
254 wd->final_position = 0;
255 evas_object_smart_callback_call(data, SIG_SELECTED,
256 (void *) wd->text_left);
259 if (position >= 0.45 && position <= 0.55 &&
260 (wd->enabled_position & ELM_ACTIONSLIDER_CENTER))
262 wd->final_position = 0.5;
263 evas_object_smart_callback_call(data, SIG_SELECTED,
264 (void *)wd->text_center);
267 if ((wd->enabled_position & ELM_ACTIONSLIDER_RIGHT) &&
268 ((!elm_widget_mirrored_get(obj) && position == 1.0) ||
269 (elm_widget_mirrored_get(obj) && position == 0.0)))
271 wd->final_position = 1;
272 evas_object_smart_callback_call(data, SIG_SELECTED,
273 (void *) wd->text_right);
277 if (wd->magnet_position == ELM_ACTIONSLIDER_NONE) return;
279 #define _FINAL_POS_BY_ORIENTATION(x) (x)
280 #define _POS_BY_ORIENTATION(x) \
281 ((!elm_widget_mirrored_get(obj)) ? \
284 position = _POS_BY_ORIENTATION(position);
288 if (wd->magnet_position & ELM_ACTIONSLIDER_LEFT)
289 wd->final_position = _FINAL_POS_BY_ORIENTATION(0);
290 else if (wd->magnet_position & ELM_ACTIONSLIDER_CENTER)
291 wd->final_position = 0.5;
292 else if (wd->magnet_position & ELM_ACTIONSLIDER_RIGHT)
293 wd->final_position = _FINAL_POS_BY_ORIENTATION(1);
295 else if ((position >= 0.3) && (position <= 0.7))
297 if (wd->magnet_position & ELM_ACTIONSLIDER_CENTER)
298 wd->final_position = 0.5;
299 else if (position < 0.5)
301 if (wd->magnet_position & ELM_ACTIONSLIDER_LEFT)
302 wd->final_position = _FINAL_POS_BY_ORIENTATION(0);
304 wd->final_position = _FINAL_POS_BY_ORIENTATION(1);
308 if (wd->magnet_position & ELM_ACTIONSLIDER_RIGHT)
309 wd->final_position = _FINAL_POS_BY_ORIENTATION(1);
311 wd->final_position = _FINAL_POS_BY_ORIENTATION(0);
316 if (wd->magnet_position & ELM_ACTIONSLIDER_RIGHT)
317 wd->final_position = _FINAL_POS_BY_ORIENTATION(1);
318 else if (wd->magnet_position & ELM_ACTIONSLIDER_CENTER)
319 wd->final_position = 0.5;
321 wd->final_position = _FINAL_POS_BY_ORIENTATION(0);
323 wd->button_animator = ecore_animator_add(_button_animation, data);
325 #undef _FINAL_POS_BY_ORIENTATION
329 * Add a new actionslider to the parent.
331 * @param parent The parent object
332 * @return The new actionslider object or NULL if it cannot be created
334 * @ingroup Actionslider
337 elm_actionslider_add(Evas_Object *parent)
343 EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
345 wd = ELM_NEW(Widget_Data);
346 e = evas_object_evas_get(parent);
348 obj = elm_widget_add(e);
349 ELM_SET_WIDTYPE(widtype, "actionslider");
350 elm_widget_type_set(obj, "actionslider");
351 elm_widget_sub_object_add(parent, obj);
352 elm_widget_data_set(obj, wd);
353 elm_widget_del_hook_set(obj, _del_hook);
354 elm_widget_theme_hook_set(obj, _theme_hook);
356 wd->mouse_down = EINA_FALSE;
357 wd->enabled_position = ELM_ACTIONSLIDER_ALL;
359 wd->as = edje_object_add(e);
360 _elm_theme_object_set(obj, wd->as, "actionslider", "base", "default");
361 elm_widget_resize_object_set(obj, wd->as);
363 wd->drag_button_base = evas_object_rectangle_add(e);
364 evas_object_color_set(wd->drag_button_base, 0, 0, 0, 0);
365 edje_object_part_swallow(wd->as, "elm.drag_button_base", wd->drag_button_base);
367 edje_object_signal_callback_add(wd->as,
368 "elm.drag_button,mouse,up", "",
369 _drag_button_up_cb, obj);
370 edje_object_signal_callback_add(wd->as,
371 "elm.drag_button,mouse,down", "",
372 _drag_button_down_cb, obj);
373 edje_object_signal_callback_add(wd->as,
374 "elm.drag_button,mouse,move", "",
375 _drag_button_move_cb, obj);
377 evas_object_smart_callbacks_descriptions_set(obj, _signals);
378 _mirrored_set(obj, elm_widget_mirrored_get(obj));
384 * Set actionslider indicator position.
386 * @param obj The actionslider object.
387 * @param pos The position of the indicator.
389 * @ingroup Actionslider
392 elm_actionslider_indicator_pos_set(Evas_Object *obj, Elm_Actionslider_Pos pos)
394 ELM_CHECK_WIDTYPE(obj, widtype);
395 Widget_Data *wd = elm_widget_data_get(obj);
396 double position = 0.0;
398 pos = _get_pos_by_orientation(obj, pos);
399 if (pos == ELM_ACTIONSLIDER_CENTER) position = 0.5;
400 else if (pos == ELM_ACTIONSLIDER_RIGHT) position = 1.0;
401 edje_object_part_drag_value_set(wd->as, "elm.drag_button_base", position, 0.5);
405 * Get actionslider indicator position.
407 * @param obj The actionslider object.
408 * @return The position of the indicator.
410 * @ingroup Actionslider
412 EAPI Elm_Actionslider_Pos
413 elm_actionslider_indicator_pos_get(const Evas_Object *obj)
415 ELM_CHECK_WIDTYPE(obj, widtype) ELM_ACTIONSLIDER_NONE;
416 Widget_Data *wd = elm_widget_data_get(obj);
418 if (!wd) return ELM_ACTIONSLIDER_NONE;
420 edje_object_part_drag_value_get(wd->as, "elm.drag_button_base", &position, NULL);
422 return _get_pos_by_orientation(obj, ELM_ACTIONSLIDER_LEFT);
423 else if (position < 0.7)
424 return ELM_ACTIONSLIDER_CENTER;
426 return _get_pos_by_orientation(obj, ELM_ACTIONSLIDER_RIGHT);
430 * Set actionslider magnet position.
432 * @param obj The actionslider object.
433 * @param pos Bit mask indicating the magnet positions.
434 * Example: use (ELM_ACTIONSLIDER_LEFT | ELM_ACTIONSLIDER_RIGHT)
435 * to put magnet property on both positions
437 * @ingroup Actionslider
440 elm_actionslider_magnet_pos_set(Evas_Object *obj, Elm_Actionslider_Pos pos)
442 ELM_CHECK_WIDTYPE(obj, widtype);
443 Widget_Data *wd = elm_widget_data_get(obj);
445 wd->magnet_position = pos;
449 * Get actionslider magnet position.
451 * @param obj The actionslider object.
452 * @return The positions with magnet property.
454 * @ingroup Actionslider
456 EAPI Elm_Actionslider_Pos
457 elm_actionslider_magnet_pos_get(const Evas_Object *obj)
459 ELM_CHECK_WIDTYPE(obj, widtype) ELM_ACTIONSLIDER_NONE;
460 Widget_Data *wd = elm_widget_data_get(obj);
461 if (!wd) return ELM_ACTIONSLIDER_NONE;
462 return wd->magnet_position;
466 * Set actionslider enabled position.
468 * All the positions are enabled by default.
470 * @param obj The actionslider object.
471 * @param pos Bit mask indicating the enabled positions.
472 * Example: use (ELM_ACTIONSLIDER_LEFT | ELM_ACTIONSLIDER_RIGHT)
473 * to enable both positions, so the user can select it.
475 * @ingroup Actionslider
478 elm_actionslider_enabled_pos_set(Evas_Object *obj, Elm_Actionslider_Pos pos)
480 ELM_CHECK_WIDTYPE(obj, widtype);
481 Widget_Data *wd = elm_widget_data_get(obj);
483 wd->enabled_position = pos;
487 * Get actionslider enabled position.
489 * All the positions are enabled by default.
491 * @param obj The actionslider object.
492 * @return The enabled positions.
494 * @ingroup Actionslider
496 EAPI Elm_Actionslider_Pos
497 elm_actionslider_enabled_pos_get(const Evas_Object *obj)
499 ELM_CHECK_WIDTYPE(obj, widtype) ELM_ACTIONSLIDER_NONE;
500 Widget_Data *wd = elm_widget_data_get(obj);
501 if (!wd) return ELM_ACTIONSLIDER_NONE;
502 return wd->enabled_position;
506 * Set actionslider labels.
508 * @param obj The actionslider object
509 * @param left_label The label which is going to be set.
510 * @param center_label The label which is going to be set.
511 * @param right_label The label which is going to be set.
513 * @ingroup Actionslider
516 elm_actionslider_labels_set(Evas_Object *obj, const char *left_label, const char *center_label, const char *right_label)
518 ELM_CHECK_WIDTYPE(obj, widtype);
519 Widget_Data *wd = elm_widget_data_get(obj);
522 eina_stringshare_replace(&wd->text_left, left_label);
523 eina_stringshare_replace(&wd->text_center, center_label);
524 eina_stringshare_replace(&wd->text_right, right_label);
525 if (!elm_widget_mirrored_get(obj))
527 edje_object_part_text_set(wd->as, "elm.text.left", wd->text_left);
528 edje_object_part_text_set(wd->as, "elm.text.right", wd->text_right);
532 edje_object_part_text_set(wd->as, "elm.text.left", wd->text_right);
533 edje_object_part_text_set(wd->as, "elm.text.right", wd->text_left);
535 edje_object_part_text_set(wd->as, "elm.text.center", center_label);
539 * Get actionslider labels.
541 * @param obj The actionslider object
542 * @param left_label A char** to place the left_label of @p obj into
543 * @param center_label A char** to place the center_label of @p obj into
544 * @param right_label A char** to place the right_label of @p obj into
546 * @ingroup Actionslider
549 elm_actionslider_labels_get(const Evas_Object *obj, const char **left_label, const char **center_label, const char **right_label)
551 if (left_label) *left_label= NULL;
552 if (center_label) *center_label= NULL;
553 if (right_label) *right_label= NULL;
554 ELM_CHECK_WIDTYPE(obj, widtype);
555 Widget_Data *wd = elm_widget_data_get(obj);
557 if (left_label) *left_label = wd->text_left;
558 if (center_label) *center_label = wd->text_center;
559 if (right_label) *right_label = wd->text_right;
563 * Get actionslider selected label.
565 * @param obj The actionslider object
566 * @return The selected label
568 * @ingroup Actionslider
571 elm_actionslider_selected_label_get(const Evas_Object *obj)
573 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
574 Widget_Data *wd = elm_widget_data_get(obj);
575 if (!wd) return NULL;
577 if ((wd->final_position == 0.0) &&
578 (wd->enabled_position & ELM_ACTIONSLIDER_LEFT))
579 return wd->text_left;
581 if ((wd->final_position == 0.5) &&
582 (wd->enabled_position & ELM_ACTIONSLIDER_CENTER))
583 return wd->text_center;
585 if ((wd->final_position == 1.0) &&
586 (wd->enabled_position & ELM_ACTIONSLIDER_RIGHT))
587 return wd->text_right;
593 * Set the label used on the indicator object.
595 * @param obj The actionslider object
596 * @param label The label which is going to be set.
598 * @ingroup Actionslider
601 elm_actionslider_indicator_label_set(Evas_Object *obj, const char *label)
603 ELM_CHECK_WIDTYPE(obj, widtype);
604 Widget_Data *wd = elm_widget_data_get(obj);
607 eina_stringshare_replace(&wd->indicator_label, label);
608 edje_object_part_text_set(wd->as, "elm.text.indicator", wd->indicator_label);
612 * Get the label used on the indicator object.
614 * @param obj The actionslider object
615 * @return The indicator label
617 * @ingroup Actionslider
620 elm_actionslider_indicator_label_get(Evas_Object *obj)
622 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
623 Widget_Data *wd = elm_widget_data_get(obj);
624 if (!wd) return NULL;
625 return wd->indicator_label;