Initialize Tizen 2.3
[framework/uifw/elementary.git] / mobile / src / lib / elm_actionslider.c
1 #include <Elementary.h>
2 #include <math.h>
3 #include "elm_priv.h"
4 #include "elm_widget_actionslider.h"
5
6 EAPI const char ELM_ACTIONSLIDER_SMART_NAME[] = "elm_actionslider";
7
8 static const Elm_Layout_Part_Alias_Description _text_aliases[] =
9 {
10    {"indicator", "elm.text.indicator"},
11    {"left", "elm.text.left"},
12    {"right", "elm.text.right"},
13    {"center", "elm.text.center"},
14    {NULL, NULL}
15 };
16
17 static const char SIG_CHANGED[] = "pos_changed";
18 static const char SIG_SELECTED[] = "selected";
19 static const Evas_Smart_Cb_Description _smart_callbacks[] =
20 {
21    {SIG_CHANGED, ""},
22    {SIG_SELECTED, ""},
23    {NULL, NULL}
24 };
25
26 EVAS_SMART_SUBCLASS_NEW
27   (ELM_ACTIONSLIDER_SMART_NAME, _elm_actionslider,
28   Elm_Actionslider_Smart_Class, Elm_Layout_Smart_Class,
29   elm_layout_smart_class_get, _smart_callbacks);
30
31 static Elm_Actionslider_Pos
32 _get_pos_by_orientation(const Evas_Object *obj,
33                         Elm_Actionslider_Pos pos)
34 {
35    if (elm_widget_mirrored_get(obj))
36      {
37         switch (pos)
38           {
39            case ELM_ACTIONSLIDER_LEFT:
40              pos = ELM_ACTIONSLIDER_RIGHT;
41              break;
42
43            case ELM_ACTIONSLIDER_RIGHT:
44              pos = ELM_ACTIONSLIDER_LEFT;
45              break;
46
47            default:
48              break;
49           }
50      }
51    return pos;
52 }
53
54 static void
55 _elm_actionslider_smart_sizing_eval(Evas_Object *obj)
56 {
57    Evas_Coord minw = -1, minh = -1;
58
59    ELM_ACTIONSLIDER_DATA_GET(obj, sd);
60
61    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
62    evas_object_size_hint_min_set(sd->drag_button_base, minw, minh);
63    evas_object_size_hint_max_set(sd->drag_button_base, -1, -1);
64
65    minw = -1;
66    minh = -1;
67    elm_coords_finger_size_adjust(3, &minw, 1, &minh);
68    edje_object_size_min_restricted_calc
69      (ELM_WIDGET_DATA(sd)->resize_obj, &minw, &minh, minw, minh);
70    evas_object_size_hint_min_set(obj, minw, minh);
71    evas_object_size_hint_max_set(obj, -1, -1);
72 }
73
74 static void
75 _mirroredness_change_eval(Evas_Object *obj)
76 {
77    double pos;
78    char *left;
79
80    ELM_ACTIONSLIDER_DATA_GET(obj, sd);
81
82    left = (char *)elm_layout_text_get(obj, "elm.text.left");
83    if (left) left = strdup(left);
84
85    elm_layout_text_set
86      (obj, "elm.text.left", elm_layout_text_get(obj, "elm.text.right"));
87    elm_layout_text_set(obj, "elm.text.right", left);
88
89    free(left);
90
91    edje_object_part_drag_value_get
92      (ELM_WIDGET_DATA(sd)->resize_obj, "elm.drag_button_base", &pos, NULL);
93    edje_object_part_drag_value_set
94      (ELM_WIDGET_DATA(sd)->resize_obj, "elm.drag_button_base", 1.0 - pos, 0.5);
95 }
96
97 static Eina_Bool
98 _elm_actionslider_smart_theme(Evas_Object *obj)
99 {
100    Eina_Bool mirrored;
101
102    ELM_ACTIONSLIDER_DATA_GET(obj, sd);
103
104    mirrored = elm_object_mirrored_get(obj);
105
106    if (!ELM_WIDGET_CLASS(_elm_actionslider_parent_sc)->theme(obj))
107      return EINA_FALSE;
108
109    if (elm_object_mirrored_get(obj) != mirrored)
110      _mirroredness_change_eval(obj);
111
112    edje_object_message_signal_process(ELM_WIDGET_DATA(sd)->resize_obj);
113
114    return EINA_TRUE;
115 }
116
117 static void
118 _drag_button_down_cb(void *data,
119                      Evas_Object *o __UNUSED__,
120                      const char *emission __UNUSED__,
121                      const char *source __UNUSED__)
122 {
123    Elm_Actionslider_Smart_Data *sd = data;
124
125    sd->mouse_down = EINA_TRUE;
126 }
127
128 static void
129 _drag_button_move_cb(void *data,
130                      Evas_Object *o __UNUSED__,
131                      const char *emission __UNUSED__,
132                      const char *source __UNUSED__)
133 {
134    Evas_Object *obj = data;
135    double pos = 0.0;
136
137    ELM_ACTIONSLIDER_DATA_GET(obj, sd);
138
139    if (!sd->mouse_down) return;
140
141    edje_object_part_drag_value_get
142      (ELM_WIDGET_DATA(sd)->resize_obj, "elm.drag_button_base", &pos, NULL);
143    if (pos == 0.0)
144      evas_object_smart_callback_call
145        (obj, SIG_CHANGED, !elm_widget_mirrored_get(obj) ? "left" : "right");
146    else if (pos == 1.0)
147      evas_object_smart_callback_call
148        (obj, SIG_CHANGED, !elm_widget_mirrored_get(obj) ? "right" : "left");
149    else if (pos >= 0.45 && pos <= 0.55)
150      evas_object_smart_callback_call(obj, SIG_CHANGED, "center");
151 }
152
153 static void
154 _text_get(const Evas_Object *obj,
155           const char **left,
156           const char **right,
157           const char **center)
158 {
159    /* mirroredness already evaluated by the two calls below */
160    *left = elm_layout_text_get(obj, "left");
161    *right = elm_layout_text_get(obj, "right");
162    *center = elm_layout_text_get(obj, "center");
163 }
164
165 static Eina_Bool
166 _button_animator(void *data)
167 {
168    Evas_Object *obj = data;
169    double move_amount = 0.05;
170    Eina_Bool flag_finish_animation = EINA_FALSE;
171    double cur_position = 0.0, new_position = 0.0;
172
173    ELM_ACTIONSLIDER_DATA_GET(obj, sd);
174
175    edje_object_part_drag_value_get
176      (ELM_WIDGET_DATA(sd)->resize_obj, "elm.drag_button_base",
177      &cur_position, NULL);
178    {
179       double adjusted_final;
180
181       adjusted_final = (!elm_widget_mirrored_get(obj)) ?
182         sd->final_position : 1.0 - sd->final_position;
183
184       if ((adjusted_final == 0.0) ||
185           (adjusted_final == 0.5 && cur_position >= adjusted_final))
186         {
187            new_position = cur_position - move_amount;
188
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
200            if (new_position >= adjusted_final)
201              {
202                 new_position = adjusted_final;
203                 flag_finish_animation = EINA_TRUE;
204              }
205         }
206       edje_object_part_drag_value_set
207         (ELM_WIDGET_DATA(sd)->resize_obj, "elm.drag_button_base",
208         new_position, 0.5);
209    }
210
211    if (flag_finish_animation)
212      {
213         const char *left, *right, *center;
214
215         _text_get(obj, &left, &right, &center);
216
217         if ((!sd->final_position) &&
218             (sd->enabled_position & ELM_ACTIONSLIDER_LEFT))
219           evas_object_smart_callback_call(obj, SIG_SELECTED, (char *)left);
220         else if ((sd->final_position == 0.5) &&
221                  (sd->enabled_position & ELM_ACTIONSLIDER_CENTER))
222           evas_object_smart_callback_call(obj, SIG_SELECTED, (char *)center);
223         else if ((sd->final_position == 1) &&
224                  (sd->enabled_position & ELM_ACTIONSLIDER_RIGHT))
225           evas_object_smart_callback_call(obj, SIG_SELECTED, (char *)right);
226
227         sd->button_animator = NULL;
228
229         return ECORE_CALLBACK_CANCEL;
230      }
231
232    return ECORE_CALLBACK_RENEW;
233 }
234
235 static void
236 _drag_button_up_cb(void *data,
237                    Evas_Object *o __UNUSED__,
238                    const char *emission __UNUSED__,
239                    const char *source __UNUSED__)
240 {
241    Evas_Object *obj = data;
242    double position = 0.0;
243
244    ELM_ACTIONSLIDER_DATA_GET(obj, sd);
245
246    sd->mouse_down = EINA_FALSE;
247
248    edje_object_part_drag_value_get
249      (ELM_WIDGET_DATA(sd)->resize_obj, "elm.drag_button_base", &position,
250      NULL);
251
252    const char *left, *right, *center;
253
254    _text_get(obj, &left, &right, &center);
255
256    if ((sd->enabled_position & ELM_ACTIONSLIDER_LEFT) &&
257        ((!elm_widget_mirrored_get(obj) && position == 0.0) ||
258         (elm_widget_mirrored_get(obj) && position == 1.0)))
259      {
260         sd->final_position = 0;
261         evas_object_smart_callback_call(obj, SIG_SELECTED, (char *)left);
262
263         return;
264      }
265
266    if (position >= 0.45 && position <= 0.55 &&
267        (sd->enabled_position & ELM_ACTIONSLIDER_CENTER))
268      {
269         sd->final_position = 0.5;
270         evas_object_smart_callback_call(obj, SIG_SELECTED, (char *)center);
271
272         if (sd->button_animator) ecore_animator_del(sd->button_animator);
273         sd->button_animator = ecore_animator_add(_button_animator, obj);
274
275         return;
276      }
277
278    if ((sd->enabled_position & ELM_ACTIONSLIDER_RIGHT) &&
279        ((!elm_widget_mirrored_get(obj) && position == 1.0) ||
280         (elm_widget_mirrored_get(obj) && position == 0.0)))
281      {
282         sd->final_position = 1;
283         evas_object_smart_callback_call(obj, SIG_SELECTED, (char *)right);
284         return;
285      }
286
287    if (sd->magnet_position == ELM_ACTIONSLIDER_NONE) return;
288
289 #define _FIX_POS_ON_MIRROREDNESS(x) \
290   ((!elm_widget_mirrored_get(obj)) ? x : 1.0 - x)
291
292    position = _FIX_POS_ON_MIRROREDNESS(position);
293
294    if (position < 0.3)
295      {
296         if (sd->magnet_position & ELM_ACTIONSLIDER_LEFT)
297           sd->final_position = 0;
298         else if (sd->magnet_position & ELM_ACTIONSLIDER_CENTER)
299           sd->final_position = 0.5;
300         else if (sd->magnet_position & ELM_ACTIONSLIDER_RIGHT)
301           sd->final_position = 1;
302      }
303    else if ((position >= 0.3) && (position <= 0.7))
304      {
305         if (sd->magnet_position & ELM_ACTIONSLIDER_CENTER)
306           sd->final_position = 0.5;
307         else if (position < 0.5)
308           {
309              if (sd->magnet_position & ELM_ACTIONSLIDER_LEFT)
310                sd->final_position = 0;
311              else
312                sd->final_position = 1;
313           }
314         else
315           {
316              if (sd->magnet_position & ELM_ACTIONSLIDER_RIGHT)
317                sd->final_position = 1;
318              else
319                sd->final_position = 0;
320           }
321      }
322    else
323      {
324         if (sd->magnet_position & ELM_ACTIONSLIDER_RIGHT)
325           sd->final_position = 1;
326         else if (sd->magnet_position & ELM_ACTIONSLIDER_CENTER)
327           sd->final_position = 0.5;
328         else
329           sd->final_position = 0;
330      }
331    if (sd->button_animator) ecore_animator_del(sd->button_animator);
332    sd->button_animator = ecore_animator_add(_button_animator, obj);
333
334 #undef _FINAL_FIX_POS_ON_MIRROREDNESS
335 }
336
337 static void
338 _mirrored_part_fix(const Evas_Object *obj,
339                    const char **part)
340 {
341    if (elm_widget_mirrored_get(obj))
342      {
343         /* exchange left and right */
344         if (!strcmp(*part, "left")) *part = "right";
345         else if (!strcmp(*part, "right"))
346           *part = "left";
347         else if (!strcmp(*part, "elm.text.right"))
348           *part = "elm.text.left";
349         else if (!strcmp(*part, "elm.text.left"))
350           *part = "elm.text.right";
351      }
352 }
353
354 static Eina_Bool
355 _elm_actionslider_smart_text_set(Evas_Object *obj,
356                                  const char *item,
357                                  const char *label)
358 {
359    _mirrored_part_fix(obj, &item);
360
361    return _elm_actionslider_parent_sc->text_set(obj, item, label);
362 }
363
364 static const char *
365 _elm_actionslider_smart_text_get(const Evas_Object *obj,
366                                  const char *item)
367 {
368    _mirrored_part_fix(obj, &item);
369
370    return _elm_actionslider_parent_sc->text_get(obj, item);
371 }
372
373 static void
374 _elm_actionslider_smart_add(Evas_Object *obj)
375 {
376    EVAS_SMART_DATA_ALLOC(obj, Elm_Actionslider_Smart_Data);
377
378    ELM_WIDGET_CLASS(_elm_actionslider_parent_sc)->base.add(obj);
379
380    priv->mouse_down = EINA_FALSE;
381    priv->enabled_position = ELM_ACTIONSLIDER_ALL;
382
383    priv->drag_button_base =
384      evas_object_rectangle_add(evas_object_evas_get(obj));
385    evas_object_color_set(priv->drag_button_base, 0, 0, 0, 0);
386
387    edje_object_signal_callback_add
388      (ELM_WIDGET_DATA(priv)->resize_obj, "elm.drag_button,mouse,up", "",
389      _drag_button_up_cb, obj);
390    edje_object_signal_callback_add
391      (ELM_WIDGET_DATA(priv)->resize_obj, "elm.drag_button,mouse,down", "",
392      _drag_button_down_cb, priv);
393    edje_object_signal_callback_add
394      (ELM_WIDGET_DATA(priv)->resize_obj, "elm.drag_button,mouse,move", "",
395      _drag_button_move_cb, obj);
396
397    elm_layout_theme_set
398      (obj, "actionslider", "base", elm_widget_style_get(obj));
399
400    elm_layout_content_set(obj, "elm.drag_button_base", priv->drag_button_base);
401
402    elm_layout_sizing_eval(obj);
403 }
404
405 static void
406 _elm_actionslider_smart_set_user(Elm_Actionslider_Smart_Class *sc)
407 {
408    ELM_WIDGET_CLASS(sc)->base.add = _elm_actionslider_smart_add;
409
410    /* not a 'focus chain manager' */
411    ELM_WIDGET_CLASS(sc)->focus_next = NULL;
412    ELM_WIDGET_CLASS(sc)->focus_direction = NULL;
413
414    ELM_WIDGET_CLASS(sc)->theme = _elm_actionslider_smart_theme;
415
416    ELM_LAYOUT_CLASS(sc)->sizing_eval = _elm_actionslider_smart_sizing_eval;
417    ELM_LAYOUT_CLASS(sc)->text_set = _elm_actionslider_smart_text_set;
418    ELM_LAYOUT_CLASS(sc)->text_get = _elm_actionslider_smart_text_get;
419
420    ELM_LAYOUT_CLASS(sc)->text_aliases = _text_aliases;
421 }
422
423 EAPI const Elm_Actionslider_Smart_Class *
424 elm_actionslider_smart_class_get(void)
425 {
426    static Elm_Actionslider_Smart_Class _sc =
427      ELM_ACTIONSLIDER_SMART_CLASS_INIT_NAME_VERSION
428        (ELM_ACTIONSLIDER_SMART_NAME);
429    static const Elm_Actionslider_Smart_Class *class = NULL;
430    Evas_Smart_Class *esc = (Evas_Smart_Class *)&_sc;
431
432    if (class)
433      return class;
434
435    _elm_actionslider_smart_set(&_sc);
436    esc->callbacks = _smart_callbacks;
437    class = &_sc;
438
439    return class;
440 }
441
442 EAPI Evas_Object *
443 elm_actionslider_add(Evas_Object *parent)
444 {
445    Evas_Object *obj;
446
447    EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
448
449    obj = elm_widget_add(_elm_actionslider_smart_class_new(), parent);
450    if (!obj) return NULL;
451
452    if (!elm_widget_sub_object_add(parent, obj))
453      ERR("could not add %p as sub object of %p", obj, parent);
454
455    return obj;
456 }
457
458 EAPI void
459 elm_actionslider_indicator_pos_set(Evas_Object *obj,
460                                    Elm_Actionslider_Pos pos)
461 {
462    double position = 0.0;
463
464    ELM_ACTIONSLIDER_CHECK(obj);
465    ELM_ACTIONSLIDER_DATA_GET(obj, sd);
466
467    pos = _get_pos_by_orientation(obj, pos);
468    if (pos == ELM_ACTIONSLIDER_CENTER) position = 0.5;
469    else if (pos == ELM_ACTIONSLIDER_RIGHT)
470      position = 1.0;
471
472    edje_object_part_drag_value_set
473      (ELM_WIDGET_DATA(sd)->resize_obj, "elm.drag_button_base", position, 0.5);
474 }
475
476 EAPI Elm_Actionslider_Pos
477 elm_actionslider_indicator_pos_get(const Evas_Object *obj)
478 {
479    double position;
480
481    ELM_ACTIONSLIDER_CHECK(obj) ELM_ACTIONSLIDER_NONE;
482    ELM_ACTIONSLIDER_DATA_GET(obj, sd);
483
484    edje_object_part_drag_value_get
485      (ELM_WIDGET_DATA(sd)->resize_obj, "elm.drag_button_base", &position,
486      NULL);
487    if (position < 0.3)
488      return _get_pos_by_orientation(obj, ELM_ACTIONSLIDER_LEFT);
489    else if (position < 0.7)
490      return ELM_ACTIONSLIDER_CENTER;
491    else
492      return _get_pos_by_orientation(obj, ELM_ACTIONSLIDER_RIGHT);
493 }
494
495 EAPI void
496 elm_actionslider_magnet_pos_set(Evas_Object *obj,
497                                 Elm_Actionslider_Pos pos)
498 {
499    ELM_ACTIONSLIDER_CHECK(obj);
500    ELM_ACTIONSLIDER_DATA_GET(obj, sd);
501
502    sd->magnet_position = pos;
503 }
504
505 EAPI Elm_Actionslider_Pos
506 elm_actionslider_magnet_pos_get(const Evas_Object *obj)
507 {
508    ELM_ACTIONSLIDER_CHECK(obj) ELM_ACTIONSLIDER_NONE;
509    ELM_ACTIONSLIDER_DATA_GET(obj, sd);
510
511    return sd->magnet_position;
512 }
513
514 EAPI void
515 elm_actionslider_enabled_pos_set(Evas_Object *obj,
516                                  Elm_Actionslider_Pos pos)
517 {
518    ELM_ACTIONSLIDER_CHECK(obj);
519    ELM_ACTIONSLIDER_DATA_GET(obj, sd);
520
521    sd->enabled_position = pos;
522 }
523
524 EAPI Elm_Actionslider_Pos
525 elm_actionslider_enabled_pos_get(const Evas_Object *obj)
526 {
527    ELM_ACTIONSLIDER_CHECK(obj) ELM_ACTIONSLIDER_NONE;
528    ELM_ACTIONSLIDER_DATA_GET(obj, sd);
529
530    return sd->enabled_position;
531 }
532
533 EAPI const char *
534 elm_actionslider_selected_label_get(const Evas_Object *obj)
535 {
536    const char *left, *right, *center;
537
538    ELM_ACTIONSLIDER_CHECK(obj) NULL;
539    ELM_ACTIONSLIDER_DATA_GET(obj, sd);
540
541    _text_get(obj, &left, &right, &center);
542
543    if ((sd->final_position == 0.0) &&
544        (sd->enabled_position & ELM_ACTIONSLIDER_LEFT))
545      return left;
546
547    if ((sd->final_position == 0.5) &&
548        (sd->enabled_position & ELM_ACTIONSLIDER_CENTER))
549      return center;
550
551    if ((sd->final_position == 1.0) &&
552        (sd->enabled_position & ELM_ACTIONSLIDER_RIGHT))
553      return right;
554
555    return NULL;
556 }