Elementary: Rename elm_object_label_* -> elm_object_text_part_*.
[framework/uifw/elementary.git] / src / lib / elm_actionslider.c
1 #include <Elementary.h>
2 #include <math.h>
3 #include "elm_priv.h"
4
5 typedef struct _Widget_Data Widget_Data;
6
7 struct _Widget_Data
8 {
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;
17 };
18
19 static const char *widtype = NULL;
20
21 static const char SIG_CHANGED[] = "pos_changed";
22 static const char SIG_SELECTED[] = "selected";
23
24 static const Evas_Smart_Cb_Description _signals[] =
25 {
26      {SIG_CHANGED, ""},
27      {SIG_SELECTED, ""},
28      {NULL, NULL}
29 };
30
31
32 static void
33 _del_hook(Evas_Object *obj)
34 {
35    Widget_Data *wd = elm_widget_data_get(obj);
36    if (!wd) return;
37    if (wd->drag_button_base)
38      {
39         evas_object_del(wd->drag_button_base);
40         wd->drag_button_base = NULL;
41      }
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);
46    free(wd);
47 }
48
49 static Elm_Actionslider_Pos
50 _get_pos_by_orientation(const Evas_Object *obj, Elm_Actionslider_Pos pos)
51 {
52    if (elm_widget_mirrored_get(obj))
53      {
54         switch (pos)
55           {
56            case ELM_ACTIONSLIDER_LEFT:
57               pos = ELM_ACTIONSLIDER_RIGHT;
58               break;
59            case ELM_ACTIONSLIDER_RIGHT:
60               pos = ELM_ACTIONSLIDER_LEFT;
61               break;
62            default:
63               break;
64           }
65      }
66    return pos;
67 }
68
69 static void
70 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
71 {
72    Widget_Data *wd = elm_widget_data_get(obj);
73    double pos;
74
75    if (!wd) return;
76    if (edje_object_mirrored_get(wd->as) == rtl)
77      return;
78
79    edje_object_mirrored_set(wd->as, rtl);
80    if (!elm_widget_mirrored_get(obj))
81      {
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);
84      }
85    else
86      {
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);
89      }
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);
92 }
93
94 static void
95 _sizing_eval(Evas_Object *obj)
96 {
97    Widget_Data *wd = elm_widget_data_get(obj);
98    Evas_Coord minw = -1, minh = -1;
99
100    if (!wd) return;
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);
104
105    minw = -1;
106    minh = -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);
111 }
112
113 static void
114 _theme_hook(Evas_Object *obj)
115 {
116    Widget_Data *wd = elm_widget_data_get(obj);
117    if (!wd) return;
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);
121
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);
127
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);
132    _sizing_eval(obj);
133 }
134
135 static void
136 _drag_button_down_cb(void *data, Evas_Object *o __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
137 {
138    Widget_Data *wd = elm_widget_data_get((Evas_Object *) data);
139    if (!wd) return;
140    wd->mouse_down = EINA_TRUE;
141 }
142
143 static void
144 _drag_button_move_cb(void *data, Evas_Object *o __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
145 {
146    Evas_Object *obj = (Evas_Object *) data;
147    Widget_Data *wd = elm_widget_data_get(obj);
148    double pos = 0.0;
149    if (!wd) return;
150
151    if (!wd->mouse_down) return;
152    edje_object_part_drag_value_get(wd->as, "elm.drag_button_base", &pos, NULL);
153    if (pos == 0.0)
154      evas_object_smart_callback_call(obj, SIG_CHANGED,
155                                      (void *) ((!elm_widget_mirrored_get(obj)) ?
156                                                "left" : "right"));
157    else if (pos == 1.0)
158      evas_object_smart_callback_call(obj, SIG_CHANGED,
159                                      (void *) ((!elm_widget_mirrored_get(obj)) ?
160                                                "right" : "left"));
161    else if (pos >= 0.45 && pos <= 0.55)
162      evas_object_smart_callback_call(obj, SIG_CHANGED, (void *)"center");
163 }
164
165 static Eina_Bool
166 _button_animation(void *data)
167 {
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)
174      {
175         wd->button_animator = NULL;
176         return ECORE_CALLBACK_CANCEL;
177      }
178
179    edje_object_part_drag_value_get(wd->as,
180                                    "elm.drag_button_base", &cur_position, NULL);
181      {
182         double adjusted_final;
183         adjusted_final = (!elm_widget_mirrored_get(obj)) ?
184            wd->final_position : 1.0 - wd->final_position;
185         if ((adjusted_final == 0.0) ||
186             (adjusted_final == 0.5 && cur_position >= adjusted_final))
187           {
188              new_position = cur_position - move_amount;
189              if (new_position <= adjusted_final)
190                {
191                   new_position = adjusted_final;
192                   flag_finish_animation = EINA_TRUE;
193                }
194           }
195         else if ((adjusted_final == 1.0) ||
196                  (adjusted_final == 0.5 && cur_position < adjusted_final))
197           {
198              new_position = cur_position + move_amount;
199              if (new_position >= adjusted_final)
200                {
201                   new_position = adjusted_final;
202                   flag_finish_animation = EINA_TRUE;
203                }
204           }
205         edje_object_part_drag_value_set(wd->as,
206                                         "elm.drag_button_base", new_position, 0.5);
207      }
208
209    if (flag_finish_animation)
210      {
211         if ((!wd->final_position) &&
212             (wd->enabled_position & ELM_ACTIONSLIDER_LEFT))
213           evas_object_smart_callback_call(data, SIG_SELECTED,
214                                           (void *)wd->text_left);
215         else if ((wd->final_position == 0.5) &&
216                  (wd->enabled_position & ELM_ACTIONSLIDER_CENTER))
217           evas_object_smart_callback_call(data, SIG_SELECTED,
218                                           (void *)wd->text_center);
219         else if ((wd->final_position == 1) &&
220                  (wd->enabled_position & ELM_ACTIONSLIDER_RIGHT))
221           evas_object_smart_callback_call(data, SIG_SELECTED,
222                                           (void *)wd->text_right);
223         wd->button_animator = NULL;
224         return ECORE_CALLBACK_CANCEL;
225      }
226    return ECORE_CALLBACK_RENEW;
227 }
228
229 static void
230 _drag_button_up_cb(void *data, Evas_Object *o __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
231 {
232    Evas_Object *obj = (Evas_Object *) data;
233    Widget_Data *wd = elm_widget_data_get(obj);
234    double position = 0.0;
235    if (!wd) return;
236
237    wd->mouse_down = EINA_FALSE;
238
239    edje_object_part_drag_value_get(wd->as, "elm.drag_button_base",
240                                    &position, NULL);
241
242    if ((wd->enabled_position & ELM_ACTIONSLIDER_LEFT) &&
243        ((!elm_widget_mirrored_get(obj) && position == 0.0) ||
244         (elm_widget_mirrored_get(obj) && position == 1.0)))
245      {
246         wd->final_position = 0;
247         evas_object_smart_callback_call(data, SIG_SELECTED,
248                                         (void *) wd->text_left);
249         return;
250      }
251    if (position >= 0.45 && position <= 0.55 &&
252        (wd->enabled_position & ELM_ACTIONSLIDER_CENTER))
253      {
254         wd->final_position = 0.5;
255         evas_object_smart_callback_call(data, SIG_SELECTED,
256                                         (void *)wd->text_center);
257         if (wd->button_animator) ecore_animator_del(wd->button_animator);
258         wd->button_animator = ecore_animator_add(_button_animation, data);
259         return;
260      }
261    if ((wd->enabled_position & ELM_ACTIONSLIDER_RIGHT) &&
262        ((!elm_widget_mirrored_get(obj) && position == 1.0) ||
263         (elm_widget_mirrored_get(obj) && position == 0.0)))
264      {
265         wd->final_position = 1;
266         evas_object_smart_callback_call(data, SIG_SELECTED,
267                                         (void *) wd->text_right);
268         return;
269      }
270
271    if (wd->magnet_position == ELM_ACTIONSLIDER_NONE) return;
272
273 #define _FINAL_POS_BY_ORIENTATION(x) (x)
274 #define _POS_BY_ORIENTATION(x) \
275    ((!elm_widget_mirrored_get(obj)) ? \
276     x : 1.0 - x)
277
278    position = _POS_BY_ORIENTATION(position);
279
280    if (position < 0.3)
281      {
282         if (wd->magnet_position & ELM_ACTIONSLIDER_LEFT)
283           wd->final_position = _FINAL_POS_BY_ORIENTATION(0);
284         else if (wd->magnet_position & ELM_ACTIONSLIDER_CENTER)
285           wd->final_position = 0.5;
286         else if (wd->magnet_position & ELM_ACTIONSLIDER_RIGHT)
287           wd->final_position = _FINAL_POS_BY_ORIENTATION(1);
288      }
289    else if ((position >= 0.3) && (position <= 0.7))
290      {
291         if (wd->magnet_position & ELM_ACTIONSLIDER_CENTER)
292           wd->final_position = 0.5;
293         else if (position < 0.5)
294           {
295              if (wd->magnet_position & ELM_ACTIONSLIDER_LEFT)
296                wd->final_position = _FINAL_POS_BY_ORIENTATION(0);
297              else
298                wd->final_position = _FINAL_POS_BY_ORIENTATION(1);
299           }
300         else
301           {
302              if (wd->magnet_position & ELM_ACTIONSLIDER_RIGHT)
303                wd->final_position = _FINAL_POS_BY_ORIENTATION(1);
304              else
305                wd->final_position = _FINAL_POS_BY_ORIENTATION(0);
306           }
307      }
308    else
309      {
310         if (wd->magnet_position & ELM_ACTIONSLIDER_RIGHT)
311           wd->final_position = _FINAL_POS_BY_ORIENTATION(1);
312         else if (wd->magnet_position & ELM_ACTIONSLIDER_CENTER)
313           wd->final_position = 0.5;
314         else
315           wd->final_position = _FINAL_POS_BY_ORIENTATION(0);
316      }
317    if (wd->button_animator) ecore_animator_del(wd->button_animator);
318    wd->button_animator = ecore_animator_add(_button_animation, data);
319
320 #undef _FINAL_POS_BY_ORIENTATION
321 }
322
323 static void
324 _elm_actionslider_label_set(Evas_Object *obj, const char *item, const char *label)
325 {
326    ELM_CHECK_WIDTYPE(obj, widtype);
327    Widget_Data *wd = elm_widget_data_get(obj);
328    if (!wd) return;
329
330    if (!item)
331      {
332         eina_stringshare_replace(&wd->indicator_label, label);
333         edje_object_part_text_set(wd->as, "elm.text.indicator",
334               wd->indicator_label);
335      }
336    else if (!strcmp(item, "left"))
337      {
338         eina_stringshare_replace(&wd->text_left, label);
339         if (!elm_widget_mirrored_get(obj))
340           {
341              edje_object_part_text_set(wd->as, "elm.text.left", wd->text_left);
342           }
343         else
344           {
345              edje_object_part_text_set(wd->as, "elm.text.right", wd->text_left);
346           }
347      }
348    else if (!strcmp(item, "center"))
349      {
350         eina_stringshare_replace(&wd->text_center, label);
351         edje_object_part_text_set(wd->as, "elm.text.center", wd->text_center);
352      }
353    else if (!strcmp(item, "right"))
354      {
355         eina_stringshare_replace(&wd->text_right, label);
356         if (!elm_widget_mirrored_get(obj))
357           {
358              edje_object_part_text_set(wd->as, "elm.text.right", wd->text_right);
359           }
360         else
361           {
362              edje_object_part_text_set(wd->as, "elm.text.left", wd->text_right);
363           }
364      }
365 }
366
367 static const char *
368 _elm_actionslider_label_get(const Evas_Object *obj, const char *item)
369 {
370    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
371    Widget_Data *wd = elm_widget_data_get(obj);
372    if (!wd) return NULL;
373
374    if (!item)
375      {
376         return wd->indicator_label;
377      }
378    else if (!strcmp(item, "left"))
379      {
380         return wd->text_left;
381      }
382    else if (!strcmp(item, "center"))
383      {
384         return wd->text_center;
385      }
386    else if (!strcmp(item, "right"))
387      {
388         return wd->text_right;
389      }
390
391    return NULL;
392 }
393
394 EAPI Evas_Object *
395 elm_actionslider_add(Evas_Object *parent)
396 {
397    Evas_Object *obj;
398    Widget_Data *wd;
399    Evas *e;
400
401    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
402
403    ELM_SET_WIDTYPE(widtype, "actionslider");
404    elm_widget_type_set(obj, "actionslider");
405    elm_widget_sub_object_add(parent, obj);
406    elm_widget_data_set(obj, wd);
407    elm_widget_del_hook_set(obj, _del_hook);
408    elm_widget_theme_hook_set(obj, _theme_hook);
409    elm_widget_text_set_hook_set(obj, _elm_actionslider_label_set);
410    elm_widget_text_get_hook_set(obj, _elm_actionslider_label_get);
411
412    wd->mouse_down = EINA_FALSE;
413    wd->enabled_position = ELM_ACTIONSLIDER_ALL;
414
415    wd->as = edje_object_add(e);
416    _elm_theme_object_set(obj, wd->as, "actionslider", "base", "default");
417    elm_widget_resize_object_set(obj, wd->as);
418
419    wd->drag_button_base = evas_object_rectangle_add(e);
420    evas_object_color_set(wd->drag_button_base, 0, 0, 0, 0);
421    edje_object_part_swallow(wd->as, "elm.drag_button_base", wd->drag_button_base);
422
423    edje_object_signal_callback_add(wd->as,
424                                    "elm.drag_button,mouse,up", "",
425                                    _drag_button_up_cb, obj);
426    edje_object_signal_callback_add(wd->as,
427                                    "elm.drag_button,mouse,down", "",
428                                    _drag_button_down_cb, obj);
429    edje_object_signal_callback_add(wd->as,
430                                    "elm.drag_button,mouse,move", "",
431                                    _drag_button_move_cb, obj);
432
433    evas_object_smart_callbacks_descriptions_set(obj, _signals);
434    _mirrored_set(obj, elm_widget_mirrored_get(obj));
435    _sizing_eval(obj);
436    return obj;
437 }
438
439 EAPI void
440 elm_actionslider_indicator_pos_set(Evas_Object *obj, Elm_Actionslider_Pos pos)
441 {
442    ELM_CHECK_WIDTYPE(obj, widtype);
443    Widget_Data *wd = elm_widget_data_get(obj);
444    double position = 0.0;
445    if (!wd) return;
446    pos = _get_pos_by_orientation(obj, pos);
447    if (pos == ELM_ACTIONSLIDER_CENTER) position = 0.5;
448    else if (pos == ELM_ACTIONSLIDER_RIGHT) position = 1.0;
449    edje_object_part_drag_value_set(wd->as, "elm.drag_button_base", position, 0.5);
450 }
451
452 EAPI Elm_Actionslider_Pos
453 elm_actionslider_indicator_pos_get(const Evas_Object *obj)
454 {
455    ELM_CHECK_WIDTYPE(obj, widtype) ELM_ACTIONSLIDER_NONE;
456    Widget_Data *wd = elm_widget_data_get(obj);
457    double position;
458    if (!wd) return ELM_ACTIONSLIDER_NONE;
459
460    edje_object_part_drag_value_get(wd->as, "elm.drag_button_base", &position, NULL);
461    if (position < 0.3)
462      return _get_pos_by_orientation(obj, ELM_ACTIONSLIDER_LEFT);
463    else if (position < 0.7)
464      return ELM_ACTIONSLIDER_CENTER;
465    else
466      return _get_pos_by_orientation(obj, ELM_ACTIONSLIDER_RIGHT);
467 }
468
469 EAPI void
470 elm_actionslider_magnet_pos_set(Evas_Object *obj, Elm_Actionslider_Pos pos)
471 {
472    ELM_CHECK_WIDTYPE(obj, widtype);
473    Widget_Data *wd = elm_widget_data_get(obj);
474    if (!wd) return;
475    wd->magnet_position = pos;
476 }
477
478 EAPI Elm_Actionslider_Pos
479 elm_actionslider_magnet_pos_get(const Evas_Object *obj)
480 {
481    ELM_CHECK_WIDTYPE(obj, widtype) ELM_ACTIONSLIDER_NONE;
482    Widget_Data *wd = elm_widget_data_get(obj);
483    if (!wd) return ELM_ACTIONSLIDER_NONE;
484    return wd->magnet_position;
485 }
486
487 EAPI void
488 elm_actionslider_enabled_pos_set(Evas_Object *obj, Elm_Actionslider_Pos pos)
489 {
490    ELM_CHECK_WIDTYPE(obj, widtype);
491    Widget_Data *wd = elm_widget_data_get(obj);
492    if (!wd) return;
493    wd->enabled_position = pos;
494 }
495
496 EAPI Elm_Actionslider_Pos
497 elm_actionslider_enabled_pos_get(const Evas_Object *obj)
498 {
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;
503 }
504
505 EAPI void
506 elm_actionslider_labels_set(Evas_Object *obj, const char *left_label, const char *center_label, const char *right_label)
507 {
508    ELM_CHECK_WIDTYPE(obj, widtype);
509    Widget_Data *wd = elm_widget_data_get(obj);
510    if (!wd) return;
511
512    _elm_actionslider_label_set(obj, "left", left_label);
513    _elm_actionslider_label_set(obj, "center", center_label);
514    _elm_actionslider_label_set(obj, "right", right_label);
515 }
516
517 EAPI void
518 elm_actionslider_labels_get(const Evas_Object *obj, const char **left_label, const char **center_label, const char **right_label)
519 {
520    if (left_label) *left_label= NULL;
521    if (center_label) *center_label= NULL;
522    if (right_label) *right_label= NULL;
523    ELM_CHECK_WIDTYPE(obj, widtype);
524    Widget_Data *wd = elm_widget_data_get(obj);
525    if (!wd) return;
526    if (left_label) *left_label = _elm_actionslider_label_get(obj, "left");
527    if (center_label) *center_label = _elm_actionslider_label_get(obj, "center");
528
529    if (right_label) *right_label = _elm_actionslider_label_get(obj, "right");
530
531 }
532
533 EAPI const char *
534 elm_actionslider_selected_label_get(const Evas_Object *obj)
535 {
536    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
537    Widget_Data *wd = elm_widget_data_get(obj);
538    if (!wd) return NULL;
539
540    if ((wd->final_position == 0.0) &&
541        (wd->enabled_position & ELM_ACTIONSLIDER_LEFT))
542      return wd->text_left;
543
544    if ((wd->final_position == 0.5) &&
545        (wd->enabled_position & ELM_ACTIONSLIDER_CENTER))
546      return wd->text_center;
547
548    if ((wd->final_position == 1.0) &&
549        (wd->enabled_position & ELM_ACTIONSLIDER_RIGHT))
550      return wd->text_right;
551
552    return NULL;
553 }
554
555 EAPI void
556 elm_actionslider_indicator_label_set(Evas_Object *obj, const char *label)
557 {
558    _elm_actionslider_label_set(obj, NULL, label);
559 }
560
561 EAPI const char *
562 elm_actionslider_indicator_label_get(Evas_Object *obj)
563 {
564    return _elm_actionslider_label_get(obj, NULL);
565 }