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 _elm_widget_mirrored_reload(obj);
133 if (!edje_object_part_swallow_get(wd->as, "elm.drag_button_base"))
134 edje_object_part_unswallow(wd->as, wd->drag_button_base);
136 _elm_theme_object_set(obj, wd->as, "actionslider",
137 "base", elm_widget_style_get(obj));
138 _elm_theme_object_set(obj, wd->drag_button_base, "actionslider",
139 "drag_button", elm_widget_style_get(obj));
140 edje_object_part_swallow(wd->as, "elm.drag_button_base", wd->drag_button_base);
142 _mirrored_set(obj, elm_widget_mirrored_get(obj));
143 edje_object_part_text_set(wd->as, "elm.text.center", wd->text_center);
144 edje_object_part_text_set(wd->as, "elm.text.indicator", wd->indicator_label);
145 edje_object_message_signal_process(wd->as);
150 _drag_button_down_cb(void *data, Evas_Object *o __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
152 Widget_Data *wd = elm_widget_data_get((Evas_Object *) data);
154 wd->mouse_down = EINA_TRUE;
158 _drag_button_move_cb(void *data, Evas_Object *o __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
160 Evas_Object *obj = (Evas_Object *) data;
161 Widget_Data *wd = elm_widget_data_get(obj);
165 if (!wd->mouse_down) return;
166 edje_object_part_drag_value_get(wd->as, "elm.drag_button_base", &pos, NULL);
168 evas_object_smart_callback_call(obj, SIG_CHANGED,
169 (void *) ((!elm_widget_mirrored_get(obj)) ?
172 evas_object_smart_callback_call(obj, SIG_CHANGED,
173 (void *) ((!elm_widget_mirrored_get(obj)) ?
175 else if (pos >= 0.45 && pos <= 0.55)
176 evas_object_smart_callback_call(obj, SIG_CHANGED, (void *)"center");
180 _button_animation(void *data)
182 Evas_Object *obj = data;
183 Widget_Data *wd = elm_widget_data_get(obj);
184 double cur_position = 0.0, new_position = 0.0;
185 double move_amount = 0.05;
186 Eina_Bool flag_finish_animation = EINA_FALSE;
187 if (!wd) return EINA_FALSE;
189 edje_object_part_drag_value_get(wd->as,
190 "elm.drag_button_base", &cur_position, NULL);
192 double adjusted_final;
193 adjusted_final = (!elm_widget_mirrored_get(obj)) ?
194 wd->final_position : 1.0 - wd->final_position;
195 if ((adjusted_final == 0.0) ||
196 (adjusted_final == 0.5 && cur_position >= adjusted_final))
198 new_position = cur_position - move_amount;
199 if (new_position <= adjusted_final)
201 new_position = adjusted_final;
202 flag_finish_animation = EINA_TRUE;
205 else if ((adjusted_final == 1.0) ||
206 (adjusted_final == 0.5 && cur_position < adjusted_final))
208 new_position = cur_position + move_amount;
209 if (new_position >= adjusted_final)
211 new_position = adjusted_final;
212 flag_finish_animation = EINA_TRUE;
215 edje_object_part_drag_value_set(wd->as,
216 "elm.drag_button_base", new_position, 0.5);
219 if (flag_finish_animation)
221 if ((!wd->final_position) &&
222 (wd->enabled_position & ELM_ACTIONSLIDER_LEFT))
223 evas_object_smart_callback_call(data, SIG_SELECTED,
224 (void *)wd->text_left);
225 else if ((wd->final_position == 0.5) &&
226 (wd->enabled_position & ELM_ACTIONSLIDER_CENTER))
227 evas_object_smart_callback_call(data, SIG_SELECTED,
228 (void *)wd->text_center);
229 else if ((wd->final_position == 1) &&
230 (wd->enabled_position & ELM_ACTIONSLIDER_RIGHT))
231 evas_object_smart_callback_call(data, SIG_SELECTED,
232 (void *)wd->text_right);
239 _drag_button_up_cb(void *data, Evas_Object *o __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
241 Evas_Object *obj = (Evas_Object *) data;
242 Widget_Data *wd = elm_widget_data_get(obj);
243 double position = 0.0;
246 wd->mouse_down = EINA_FALSE;
248 edje_object_part_drag_value_get(wd->as, "elm.drag_button_base",
251 if ((wd->enabled_position & ELM_ACTIONSLIDER_LEFT) &&
252 ((!elm_widget_mirrored_get(obj) && position == 0.0) ||
253 (elm_widget_mirrored_get(obj) && position == 1.0)))
255 wd->final_position = 0;
256 evas_object_smart_callback_call(data, SIG_SELECTED,
257 (void *) wd->text_left);
260 if (position >= 0.45 && position <= 0.55 &&
261 (wd->enabled_position & ELM_ACTIONSLIDER_CENTER))
263 wd->final_position = 0.5;
264 evas_object_smart_callback_call(data, SIG_SELECTED,
265 (void *)wd->text_center);
268 if ((wd->enabled_position & ELM_ACTIONSLIDER_RIGHT) &&
269 ((!elm_widget_mirrored_get(obj) && position == 1.0) ||
270 (elm_widget_mirrored_get(obj) && position == 0.0)))
272 wd->final_position = 1;
273 evas_object_smart_callback_call(data, SIG_SELECTED,
274 (void *) wd->text_right);
278 if (wd->magnet_position == ELM_ACTIONSLIDER_NONE) return;
280 #define _FINAL_POS_BY_ORIENTATION(x) (x)
281 #define _POS_BY_ORIENTATION(x) \
282 ((!elm_widget_mirrored_get(obj)) ? \
285 position = _POS_BY_ORIENTATION(position);
289 if (wd->magnet_position & ELM_ACTIONSLIDER_LEFT)
290 wd->final_position = _FINAL_POS_BY_ORIENTATION(0);
291 else if (wd->magnet_position & ELM_ACTIONSLIDER_CENTER)
292 wd->final_position = 0.5;
293 else if (wd->magnet_position & ELM_ACTIONSLIDER_RIGHT)
294 wd->final_position = _FINAL_POS_BY_ORIENTATION(1);
296 else if ((position >= 0.3) && (position <= 0.7))
298 if (wd->magnet_position & ELM_ACTIONSLIDER_CENTER)
299 wd->final_position = 0.5;
300 else if (position < 0.5)
302 if (wd->magnet_position & ELM_ACTIONSLIDER_LEFT)
303 wd->final_position = _FINAL_POS_BY_ORIENTATION(0);
305 wd->final_position = _FINAL_POS_BY_ORIENTATION(1);
309 if (wd->magnet_position & ELM_ACTIONSLIDER_RIGHT)
310 wd->final_position = _FINAL_POS_BY_ORIENTATION(1);
312 wd->final_position = _FINAL_POS_BY_ORIENTATION(0);
317 if (wd->magnet_position & ELM_ACTIONSLIDER_RIGHT)
318 wd->final_position = _FINAL_POS_BY_ORIENTATION(1);
319 else if (wd->magnet_position & ELM_ACTIONSLIDER_CENTER)
320 wd->final_position = 0.5;
322 wd->final_position = _FINAL_POS_BY_ORIENTATION(0);
324 wd->button_animator = ecore_animator_add(_button_animation, data);
326 #undef _FINAL_POS_BY_ORIENTATION
330 * Add a new actionslider to the parent.
332 * @param parent The parent object
333 * @return The new actionslider object or NULL if it cannot be created
335 * @ingroup Actionslider
338 elm_actionslider_add(Evas_Object *parent)
344 EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
346 wd = ELM_NEW(Widget_Data);
347 e = evas_object_evas_get(parent);
349 obj = elm_widget_add(e);
350 ELM_SET_WIDTYPE(widtype, "actionslider");
351 elm_widget_type_set(obj, "actionslider");
352 elm_widget_sub_object_add(parent, obj);
353 elm_widget_data_set(obj, wd);
354 elm_widget_del_hook_set(obj, _del_hook);
355 elm_widget_theme_hook_set(obj, _theme_hook);
357 wd->mouse_down = EINA_FALSE;
358 wd->enabled_position = ELM_ACTIONSLIDER_ALL;
360 wd->as = edje_object_add(e);
361 _elm_theme_object_set(obj, wd->as, "actionslider", "base", "default");
362 elm_widget_resize_object_set(obj, wd->as);
364 wd->drag_button_base = evas_object_rectangle_add(e);
365 evas_object_color_set(wd->drag_button_base, 0, 0, 0, 0);
366 edje_object_part_swallow(wd->as, "elm.drag_button_base", wd->drag_button_base);
368 edje_object_signal_callback_add(wd->as,
369 "elm.drag_button,mouse,up", "",
370 _drag_button_up_cb, obj);
371 edje_object_signal_callback_add(wd->as,
372 "elm.drag_button,mouse,down", "",
373 _drag_button_down_cb, obj);
374 edje_object_signal_callback_add(wd->as,
375 "elm.drag_button,mouse,move", "",
376 _drag_button_move_cb, obj);
378 evas_object_smart_callbacks_descriptions_set(obj, _signals);
379 _mirrored_set(obj, elm_widget_mirrored_get(obj));
385 * Set actionslider indicator position.
387 * @param obj The actionslider object.
388 * @param pos The position of the indicator.
390 * @ingroup Actionslider
393 elm_actionslider_indicator_pos_set(Evas_Object *obj, Elm_Actionslider_Pos pos)
395 ELM_CHECK_WIDTYPE(obj, widtype);
396 Widget_Data *wd = elm_widget_data_get(obj);
397 double position = 0.0;
399 pos = _get_pos_by_orientation(obj, pos);
400 if (pos == ELM_ACTIONSLIDER_CENTER) position = 0.5;
401 else if (pos == ELM_ACTIONSLIDER_RIGHT) position = 1.0;
402 edje_object_part_drag_value_set(wd->as, "elm.drag_button_base", position, 0.5);
406 * Get actionslider indicator position.
408 * @param obj The actionslider object.
409 * @return The position of the indicator.
411 * @ingroup Actionslider
413 EAPI Elm_Actionslider_Pos
414 elm_actionslider_indicator_pos_get(const Evas_Object *obj)
416 ELM_CHECK_WIDTYPE(obj, widtype) ELM_ACTIONSLIDER_NONE;
417 Widget_Data *wd = elm_widget_data_get(obj);
419 if (!wd) return ELM_ACTIONSLIDER_NONE;
421 edje_object_part_drag_value_get(wd->as, "elm.drag_button_base", &position, NULL);
423 return _get_pos_by_orientation(obj, ELM_ACTIONSLIDER_LEFT);
424 else if (position < 0.7)
425 return ELM_ACTIONSLIDER_CENTER;
427 return _get_pos_by_orientation(obj, ELM_ACTIONSLIDER_RIGHT);
431 * Set actionslider magnet position.
433 * @param obj The actionslider object.
434 * @param pos Bit mask indicating the magnet positions.
435 * Example: use (ELM_ACTIONSLIDER_LEFT | ELM_ACTIONSLIDER_RIGHT)
436 * to put magnet property on both positions
438 * @ingroup Actionslider
441 elm_actionslider_magnet_pos_set(Evas_Object *obj, Elm_Actionslider_Pos pos)
443 ELM_CHECK_WIDTYPE(obj, widtype);
444 Widget_Data *wd = elm_widget_data_get(obj);
446 wd->magnet_position = pos;
450 * Get actionslider magnet position.
452 * @param obj The actionslider object.
453 * @return The positions with magnet property.
455 * @ingroup Actionslider
457 EAPI Elm_Actionslider_Pos
458 elm_actionslider_magnet_pos_get(const Evas_Object *obj)
460 ELM_CHECK_WIDTYPE(obj, widtype) ELM_ACTIONSLIDER_NONE;
461 Widget_Data *wd = elm_widget_data_get(obj);
462 if (!wd) return ELM_ACTIONSLIDER_NONE;
463 return wd->magnet_position;
467 * Set actionslider enabled position.
469 * All the positions are enabled by default.
471 * @param obj The actionslider object.
472 * @param pos Bit mask indicating the enabled positions.
473 * Example: use (ELM_ACTIONSLIDER_LEFT | ELM_ACTIONSLIDER_RIGHT)
474 * to enable both positions, so the user can select it.
476 * @ingroup Actionslider
479 elm_actionslider_enabled_pos_set(Evas_Object *obj, Elm_Actionslider_Pos pos)
481 ELM_CHECK_WIDTYPE(obj, widtype);
482 Widget_Data *wd = elm_widget_data_get(obj);
484 wd->enabled_position = pos;
488 * Get actionslider enabled position.
490 * All the positions are enabled by default.
492 * @param obj The actionslider object.
493 * @return The enabled positions.
495 * @ingroup Actionslider
497 EAPI Elm_Actionslider_Pos
498 elm_actionslider_enabled_pos_get(const Evas_Object *obj)
500 ELM_CHECK_WIDTYPE(obj, widtype) ELM_ACTIONSLIDER_NONE;
501 Widget_Data *wd = elm_widget_data_get(obj);
502 if (!wd) return ELM_ACTIONSLIDER_NONE;
503 return wd->enabled_position;
507 * Set actionslider labels.
509 * @param obj The actionslider object
510 * @param left_label The label which is going to be set.
511 * @param center_label The label which is going to be set.
512 * @param right_label The label which is going to be set.
514 * @ingroup Actionslider
517 elm_actionslider_labels_set(Evas_Object *obj, const char *left_label, const char *center_label, const char *right_label)
519 ELM_CHECK_WIDTYPE(obj, widtype);
520 Widget_Data *wd = elm_widget_data_get(obj);
523 eina_stringshare_replace(&wd->text_left, left_label);
524 eina_stringshare_replace(&wd->text_center, center_label);
525 eina_stringshare_replace(&wd->text_right, right_label);
526 if (!elm_widget_mirrored_get(obj))
528 edje_object_part_text_set(wd->as, "elm.text.left", wd->text_left);
529 edje_object_part_text_set(wd->as, "elm.text.right", wd->text_right);
533 edje_object_part_text_set(wd->as, "elm.text.left", wd->text_right);
534 edje_object_part_text_set(wd->as, "elm.text.right", wd->text_left);
536 edje_object_part_text_set(wd->as, "elm.text.center", center_label);
540 * Get actionslider labels.
542 * @param obj The actionslider object
543 * @param left_label A char** to place the left_label of @p obj into
544 * @param center_label A char** to place the center_label of @p obj into
545 * @param right_label A char** to place the right_label of @p obj into
547 * @ingroup Actionslider
550 elm_actionslider_labels_get(const Evas_Object *obj, const char **left_label, const char **center_label, const char **right_label)
552 if (left_label) *left_label= NULL;
553 if (center_label) *center_label= NULL;
554 if (right_label) *right_label= NULL;
555 ELM_CHECK_WIDTYPE(obj, widtype);
556 Widget_Data *wd = elm_widget_data_get(obj);
558 if (left_label) *left_label = wd->text_left;
559 if (center_label) *center_label = wd->text_center;
560 if (right_label) *right_label = wd->text_right;
564 * Get actionslider selected label.
566 * @param obj The actionslider object
567 * @return The selected label
569 * @ingroup Actionslider
572 elm_actionslider_selected_label_get(const Evas_Object *obj)
574 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
575 Widget_Data *wd = elm_widget_data_get(obj);
576 if (!wd) return NULL;
578 if ((wd->final_position == 0.0) &&
579 (wd->enabled_position & ELM_ACTIONSLIDER_LEFT))
580 return wd->text_left;
582 if ((wd->final_position == 0.5) &&
583 (wd->enabled_position & ELM_ACTIONSLIDER_CENTER))
584 return wd->text_center;
586 if ((wd->final_position == 1.0) &&
587 (wd->enabled_position & ELM_ACTIONSLIDER_RIGHT))
588 return wd->text_right;
594 * Set the label used on the indicator object.
596 * @param obj The actionslider object
597 * @param label The label which is going to be set.
599 * @ingroup Actionslider
602 elm_actionslider_indicator_label_set(Evas_Object *obj, const char *label)
604 ELM_CHECK_WIDTYPE(obj, widtype);
605 Widget_Data *wd = elm_widget_data_get(obj);
608 eina_stringshare_replace(&wd->indicator_label, label);
609 edje_object_part_text_set(wd->as, "elm.text.indicator", wd->indicator_label);
613 * Get the label used on the indicator object.
615 * @param obj The actionslider object
616 * @return The indicator label
618 * @ingroup Actionslider
621 elm_actionslider_indicator_label_get(Evas_Object *obj)
623 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
624 Widget_Data *wd = elm_widget_data_get(obj);
625 if (!wd) return NULL;
626 return wd->indicator_label;