elementary/segment_control - Elm_Segment_Item -> Elm_Object_Item
[framework/uifw/elementary.git] / src / lib / elm_segment_control.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 typedef struct _Widget_Data Widget_Data;
5 typedef struct _Elm_Segment_Item Elm_Segment_Item;
6
7 struct _Widget_Data
8 {
9    Evas_Object *obj;
10    Evas_Object *base;
11    Eina_List *seg_items;
12    int item_count;
13    Elm_Segment_Item *selected_item;
14    int item_width;
15 };
16
17 struct _Elm_Segment_Item
18 {
19    ELM_WIDGET_ITEM;
20    Evas_Object *icon;
21    const char *label;
22    int seg_index;
23 };
24
25 static const char *widtype = NULL;
26 static void _sizing_eval(Evas_Object *obj);
27 static void _del_hook(Evas_Object *obj);
28 static void _theme_hook(Evas_Object *obj);
29 static void _disable_hook(Evas_Object *obj);
30 static void _item_free(Elm_Segment_Item *it);
31 static void _segment_off(Elm_Segment_Item *it);
32 static void _segment_on(Elm_Segment_Item *it);
33 static void _position_items(Widget_Data *wd);
34 static void _on_move_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj
35                             __UNUSED__, void *event_info __UNUSED__);
36 static void _mouse_up(void *data, Evas *e __UNUSED__, Evas_Object *obj
37                       __UNUSED__, void *event_info __UNUSED__);
38 static void _mouse_down(void *data, Evas *e __UNUSED__, Evas_Object *obj
39                         __UNUSED__, void *event_info __UNUSED__);
40 static void _swallow_item_objects(Elm_Segment_Item *it);
41 static void _update_list(Widget_Data *wd);
42 static Elm_Segment_Item * _item_find(const Evas_Object *obj, int index);
43 static Elm_Segment_Item* _item_new(Evas_Object *obj, Evas_Object *icon,
44                                    const char *label);
45
46 static const char SIG_CHANGED[] = "changed";
47
48 static const Evas_Smart_Cb_Description _signals[] = {
49    {SIG_CHANGED, ""},
50    {NULL, NULL}
51 };
52
53 static void
54 _sizing_eval(Evas_Object *obj)
55 {
56    Widget_Data *wd;
57    Evas_Coord minw = -1, minh = -1;
58    Evas_Coord w, h;
59
60    wd = elm_widget_data_get(obj);
61    if (!wd) return;
62
63    elm_coords_finger_size_adjust(wd->item_count, &minw, 1, &minh);
64    edje_object_size_min_restricted_calc(wd->base, &minw, &minh, minw, minh);
65    elm_coords_finger_size_adjust(wd->item_count, &minw, 1, &minh);
66
67    evas_object_size_hint_min_get(obj, &w, &h);
68    if (w > minw) minw = w;
69    if (h > minh) minh = h;
70    evas_object_size_hint_min_set(obj, minw, minh);
71 }
72
73 static void
74 _del_hook(Evas_Object *obj)
75 {
76    Elm_Segment_Item *it;
77    Widget_Data *wd;
78
79    wd = elm_widget_data_get(obj);
80    if (!wd) return;
81
82    EINA_LIST_FREE(wd->seg_items, it) _item_free(it);
83
84    free(wd);
85 }
86
87 static void
88 _theme_hook(Evas_Object *obj)
89 {
90    Eina_List *l;
91    Eina_Bool rtl;
92    Elm_Segment_Item *it;
93    Widget_Data *wd;
94
95    wd = elm_widget_data_get(obj);
96    if (!wd) return;
97
98    _elm_widget_mirrored_reload(obj);
99    rtl = elm_widget_mirrored_get(obj);
100    edje_object_mirrored_set(wd->base, rtl);
101
102    _elm_theme_object_set(obj, wd->base, "segment_control", "base",
103                          elm_widget_style_get(obj));
104    edje_object_scale_set(wd->base, elm_widget_scale_get(wd->base)
105                          *_elm_config->scale);
106
107    EINA_LIST_FOREACH(wd->seg_items, l, it)
108      {
109         _elm_theme_object_set(obj, VIEW(it), "segment_control",
110                               "item", elm_widget_style_get(obj));
111         edje_object_scale_set(VIEW(it), elm_widget_scale_get(VIEW(it))
112                               *_elm_config->scale);
113         edje_object_mirrored_set(VIEW(it), rtl);
114      }
115
116    _update_list(wd);
117 }
118
119 static void
120 _disable_hook(Evas_Object *obj)
121 {
122    Widget_Data *wd;
123
124    wd = elm_widget_data_get(obj);
125    if (!wd) return;
126    _update_list(wd);
127 }
128
129 // TODO:  Elm_widget elm_widget_focus_list_next_get  supports only Elm_widget list,
130 // Not the Elm_Widget_item. Focus switching with in widget not supported until
131 // it is supported in elm_widget
132 #if 0
133 static void *
134 _elm_list_data_get(const Eina_List *list)
135 {
136    Elm_Segment_Item *it = eina_list_data_get(list);
137
138    if (it) return NULL;
139
140    edje_object_signal_emit(VIEW(it), "elm,state,segment,selected", "elm");
141    return VIEW(it);
142 }
143
144 static Eina_Bool
145 _focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir,
146                  Evas_Object **next)
147 {
148    static int count=0;
149    Widget_Data *;
150    const Eina_List *items;
151    void *(*list_data_get) (const Eina_List *list);
152
153    wd = elm_widget_data_get(obj);
154    if ((!wd)) return EINA_FALSE;
155
156    /* Focus chain */
157    /* TODO: Change this to use other chain */
158    if ((items = elm_widget_focus_custom_chain_get(obj)))
159      list_data_get = eina_list_data_get;
160    else
161      {
162         items = wd->seg_items;
163         list_data_get = _elm_list_data_get;
164         if (!items) return EINA_FALSE;
165      }
166    return elm_widget_focus_list_next_get(obj, items, list_data_get, dir, next);
167 }
168 #endif
169
170 static void
171 _item_free(Elm_Segment_Item *it)
172 {
173    Widget_Data *wd;
174
175    if (!it) return;
176
177    wd = elm_widget_item_data_get(it);
178    if (!wd) return;
179
180    if (wd->selected_item == it) wd->selected_item = NULL;
181    if (wd->seg_items) wd->seg_items = eina_list_remove(wd->seg_items, it);
182
183    elm_widget_item_pre_notify_del(it);
184
185    if (it->icon) evas_object_del(it->icon);
186    if (it->label) eina_stringshare_del(it->label);
187
188    elm_widget_item_del(it);
189 }
190
191 static void
192 _segment_off(Elm_Segment_Item *it)
193 {
194    Widget_Data *wd;
195
196    if (!it) return;
197
198    wd = elm_widget_item_data_get(it);
199    if (!wd) return;
200
201    edje_object_signal_emit(VIEW(it), "elm,state,segment,normal", "elm");
202
203    if (wd->selected_item == it) wd->selected_item = NULL;
204 }
205
206 static void
207 _segment_on(Elm_Segment_Item *it)
208 {
209    Widget_Data *wd;
210
211    if (!it) return;
212
213    wd = elm_widget_item_data_get(it);
214    if (!wd) return;
215    if (it == wd->selected_item) return;
216
217    if (wd->selected_item) _segment_off(wd->selected_item);
218
219    edje_object_signal_emit(VIEW(it), "elm,state,segment,selected", "elm");
220
221    wd->selected_item = it;
222    evas_object_smart_callback_call(wd->obj, SIG_CHANGED, it);
223 }
224
225 static void
226 _position_items(Widget_Data *wd)
227 {
228    Eina_List *l;
229    Elm_Segment_Item *it;
230    Eina_Bool rtl;
231    int bx, by, bw, bh, pos;
232
233    wd->item_count = eina_list_count(wd->seg_items);
234    if (wd->item_count <= 0) return;
235
236    evas_object_geometry_get(wd->base, &bx, &by, &bw, &bh);
237    wd->item_width = bw / wd->item_count;
238    rtl = elm_widget_mirrored_get(wd->obj);
239
240    if (rtl)
241      pos = bx + bw - wd->item_width;
242    else
243      pos = bx;
244
245    EINA_LIST_FOREACH(wd->seg_items, l, it)
246      {
247         evas_object_move(VIEW(it), pos, by);
248         evas_object_resize(VIEW(it), wd->item_width, bh);
249         if (rtl)
250           pos -= wd->item_width;
251         else
252           pos += wd->item_width;
253      }
254    _sizing_eval(wd->obj);
255 }
256
257 static void
258 _on_move_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__,
259                 void *event_info __UNUSED__)
260 {
261    Widget_Data *wd;
262
263    wd = elm_widget_data_get(data);
264    if (!wd) return;
265
266    _position_items(wd);
267
268 }
269
270 static void
271 _mouse_up(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__,
272           void *event_info)
273 {
274    Widget_Data *wd;
275    Elm_Segment_Item *it;
276    Evas_Event_Mouse_Up *ev;
277    Evas_Coord x, y, w, h;
278
279    it = data;
280    if (!it) return;
281
282    wd = elm_widget_item_data_get(it);
283    if (!wd) return;
284
285    if (elm_widget_disabled_get(wd->obj)) return;
286
287    if (it == wd->selected_item) return;
288
289    ev = event_info;
290    evas_object_geometry_get(VIEW(it), &x, &y, &w, &h);
291
292    if ((ev->canvas.x >= x) && (ev->output.x <= (x + w)) && (ev->canvas.y >= y)
293        && (ev->canvas.y <= (y + h)))
294      _segment_on(it);
295    else
296      edje_object_signal_emit(VIEW(it), "elm,state,segment,normal", "elm");
297 }
298
299 static void
300 _mouse_down(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__,
301             void *event_info __UNUSED__)
302 {
303    Widget_Data *wd;
304    Elm_Segment_Item *it;
305
306    it = data;
307    if (!it) return;
308
309    wd = elm_widget_item_data_get(it);
310    if (!wd) return;
311
312    if (elm_widget_disabled_get(wd->obj)) return;
313
314    if (it == wd->selected_item) return;
315
316    edje_object_signal_emit(VIEW(it), "elm,state,segment,pressed", "elm");
317 }
318
319 static void
320 _swallow_item_objects(Elm_Segment_Item *it)
321 {
322    if (!it) return;
323
324    if (it->icon)
325      {
326         edje_object_part_swallow(VIEW(it), "elm.swallow.icon", it->icon);
327         edje_object_signal_emit(VIEW(it), "elm,state,icon,visible", "elm");
328      }
329    else
330      edje_object_signal_emit(VIEW(it), "elm,state,icon,hidden", "elm");
331
332    if (it->label)
333      {
334         edje_object_part_text_set(VIEW(it), "elm.text", it->label);
335         edje_object_signal_emit(VIEW(it), "elm,state,text,visible", "elm");
336      }
337    else
338      edje_object_signal_emit(VIEW(it), "elm,state,text,hidden", "elm");
339    edje_object_message_signal_process(VIEW(it));
340 }
341
342 static void
343 _update_list(Widget_Data *wd)
344 {
345    Eina_List *l;
346    Elm_Segment_Item *it;
347    Eina_Bool rtl;
348    int idx = 0;
349
350    _position_items(wd);
351
352    if (wd->item_count == 1)
353      {
354         it = eina_list_nth(wd->seg_items, 0);
355         it->seg_index = 0;
356
357         //Set the segment type
358         edje_object_signal_emit(VIEW(it),
359                                 "elm,type,segment,single", "elm");
360
361         //Set the segment state
362         if (wd->selected_item == it)
363           edje_object_signal_emit(VIEW(it),
364                                   "elm,state,segment,selected", "elm");
365         else
366           edje_object_signal_emit(VIEW(it),
367                                   "elm,state,segment,normal", "elm");
368
369         if (elm_widget_disabled_get(wd->obj))
370           edje_object_signal_emit(VIEW(it), "elm,state,disabled", "elm");
371         else
372           edje_object_signal_emit(VIEW(it), "elm,state,enabled", "elm");
373
374         _swallow_item_objects(it);
375         return;
376      }
377
378    rtl = elm_widget_mirrored_get(wd->obj);
379    EINA_LIST_FOREACH(wd->seg_items, l, it)
380      {
381         it->seg_index = idx;
382
383         //Set the segment type
384         if (idx == 0)
385           {
386              if (rtl)
387                edje_object_signal_emit(VIEW(it),
388                                        "elm,type,segment,right", "elm");
389              else
390                edje_object_signal_emit(VIEW(it),
391                                        "elm,type,segment,left", "elm");
392           }
393         else if (idx == (wd->item_count - 1))
394           {
395              if (rtl)
396                edje_object_signal_emit(VIEW(it),
397                                        "elm,type,segment,left", "elm");
398              else
399                edje_object_signal_emit(VIEW(it),
400                                        "elm,type,segment,right", "elm");
401           }
402         else
403           edje_object_signal_emit(VIEW(it),
404                                   "elm,type,segment,middle", "elm");
405
406         //Set the segment state
407         if (wd->selected_item == it)
408           edje_object_signal_emit(VIEW(it),
409                                   "elm,state,segment,selected", "elm");
410         else
411           edje_object_signal_emit(VIEW(it),
412                                   "elm,state,segment,normal", "elm");
413
414         if (elm_widget_disabled_get(wd->obj))
415           edje_object_signal_emit(VIEW(it), "elm,state,disabled", "elm");
416         else
417           edje_object_signal_emit(VIEW(it), "elm,state,enabled", "elm");
418
419         _swallow_item_objects(it);
420         idx++;
421      }
422 }
423
424 static Elm_Segment_Item *
425 _item_find(const Evas_Object *obj, int idx)
426 {
427    Widget_Data *wd;
428    Elm_Segment_Item *it;
429
430    wd = elm_widget_data_get(obj);
431    if (!wd) return NULL;
432
433    it = eina_list_nth(wd->seg_items, idx);
434    return it;
435 }
436
437 static Elm_Segment_Item*
438 _item_new(Evas_Object *obj, Evas_Object *icon, const char *label)
439 {
440    Elm_Segment_Item *it;
441    Widget_Data *wd;
442
443    wd = elm_widget_data_get(obj);
444    if (!wd) return NULL;
445
446    it = elm_widget_item_new(obj, Elm_Segment_Item);
447    if (!it) return NULL;
448    elm_widget_item_data_set(it, wd);
449
450    VIEW(it) = edje_object_add(evas_object_evas_get(obj));
451    edje_object_scale_set(VIEW(it), elm_widget_scale_get(VIEW(it))
452                          *_elm_config->scale);
453    evas_object_smart_member_add(VIEW(it), obj);
454    elm_widget_sub_object_add(obj, VIEW(it));
455    evas_object_clip_set(VIEW(it), evas_object_clip_get(obj));
456
457    _elm_theme_object_set(obj, VIEW(it), "segment_control", "item",
458                          elm_object_style_get(obj));
459    edje_object_mirrored_set(VIEW(it),
460                             elm_widget_mirrored_get(WIDGET(it)));
461
462    if (label)
463      eina_stringshare_replace(&it->label, label);
464    if (it->label)
465      edje_object_signal_emit(VIEW(it), "elm,state,text,visible", "elm");
466    else
467      edje_object_signal_emit(VIEW(it), "elm,state,text,hidden", "elm");
468    edje_object_message_signal_process(VIEW(it));
469    edje_object_part_text_set(VIEW(it), "elm.text", label);
470
471    it->icon = icon;
472    if (it->icon) elm_widget_sub_object_add(VIEW(it), it->icon);
473    evas_object_event_callback_add(VIEW(it), EVAS_CALLBACK_MOUSE_DOWN,
474                                   _mouse_down, it);
475    evas_object_event_callback_add(VIEW(it), EVAS_CALLBACK_MOUSE_UP,
476                                   _mouse_up, it);
477    evas_object_show(VIEW(it));
478
479    return it;
480 }
481
482 EAPI Evas_Object *
483 elm_segment_control_add(Evas_Object *parent)
484 {
485    Evas_Object *obj;
486    Evas *e;
487    Widget_Data *wd;
488
489    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
490
491    ELM_SET_WIDTYPE(widtype, "segment_control");
492    elm_widget_type_set(obj, "segment_control");
493    elm_widget_sub_object_add(parent, obj);
494    elm_widget_data_set(obj, wd);
495    elm_widget_del_hook_set(obj, _del_hook);
496    elm_widget_theme_hook_set(obj, _theme_hook);
497    elm_widget_disable_hook_set(obj, _disable_hook);
498
499    // TODO: Focus switch support to Elm_widget_Item not supported yet.
500 #if 0
501    elm_widget_focus_next_hook_set(obj, _focus_next_hook);
502 #endif
503
504    wd->obj = obj;
505
506    wd->base = edje_object_add(e);
507    edje_object_scale_set(wd->base, elm_widget_scale_get(wd->base)
508                          *_elm_config->scale);
509    _elm_theme_object_set(obj, wd->base, "segment_control", "base", "default");
510    elm_widget_resize_object_set(obj, wd->base);
511
512    evas_object_event_callback_add(obj, EVAS_CALLBACK_RESIZE,
513                                   _on_move_resize, obj);
514    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOVE,
515                                   _on_move_resize, obj);
516
517    evas_object_smart_callbacks_descriptions_set(obj, _signals);
518
519    return obj;
520 }
521
522 EAPI Elm_Object_Item *
523 elm_segment_control_item_add(Evas_Object *obj, Evas_Object *icon,
524                              const char *label)
525 {
526    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
527    Elm_Segment_Item *it;
528    Widget_Data *wd;
529
530    wd = elm_widget_data_get(obj);
531    if (!wd) return NULL;
532
533    it = _item_new(obj, icon, label);
534    if (!it) return NULL;
535
536    wd->seg_items = eina_list_append(wd->seg_items, it);
537    _update_list(wd);
538
539    return (Elm_Object_Item *) it;
540 }
541
542 EAPI Elm_Object_Item *
543 elm_segment_control_item_insert_at(Evas_Object *obj, Evas_Object *icon,
544                                    const char *label, int idx)
545 {
546    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
547    Elm_Segment_Item *it, *it_rel;
548    Widget_Data *wd;
549
550    wd = elm_widget_data_get(obj);
551    if (!wd) return NULL;
552    if (idx < 0) idx = 0;
553
554    it = _item_new(obj, icon, label);
555    if (!it) return NULL;
556
557    it_rel = _item_find(obj, idx);
558    if (it_rel)
559      wd->seg_items = eina_list_prepend_relative(wd->seg_items, it, it_rel);
560    else
561      wd->seg_items = eina_list_append(wd->seg_items, it);
562
563    _update_list(wd);
564    return (Elm_Object_Item *) it;
565 }
566
567 EAPI void
568 elm_segment_control_item_del(Elm_Object_Item *it)
569 {
570    ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
571    Widget_Data *wd;
572    Elm_Segment_Item *item = (Elm_Segment_Item *) it;
573
574    wd = elm_widget_item_data_get(item);
575    if (!wd) return;
576
577    _item_free(item);
578    _update_list(wd);
579 }
580
581 EAPI void
582 elm_segment_control_item_del_at(Evas_Object *obj, int idx)
583 {
584    ELM_CHECK_WIDTYPE(obj, widtype);
585    Elm_Segment_Item *it;
586    Widget_Data *wd;
587
588    wd = elm_widget_data_get(obj);
589    if (!wd) return;
590
591    it = _item_find(obj, idx);
592    if (!it) return;
593    _item_free(it);
594    _update_list(wd);
595 }
596
597 EAPI const char*
598 elm_segment_control_item_label_get(const Evas_Object *obj, int idx)
599 {
600    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
601    Elm_Segment_Item *it;
602
603    it = _item_find(obj, idx);
604    if (it) return it->label;
605
606    return NULL;
607 }
608
609 EAPI void
610 elm_segment_control_item_label_set(Elm_Object_Item* it, const char* label)
611 {
612    ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
613    Widget_Data *wd;
614    Elm_Segment_Item *item = (Elm_Segment_Item *) it;
615
616    wd = elm_widget_item_data_get(item);
617    if (!wd) return;
618
619    eina_stringshare_replace(&item->label, label);
620    if (item->label)
621      edje_object_signal_emit(VIEW(item), "elm,state,text,visible", "elm");
622    else
623      edje_object_signal_emit(VIEW(item), "elm,state,text,hidden", "elm");
624    edje_object_message_signal_process(VIEW(item));
625    //label can be NULL also.
626    edje_object_part_text_set(VIEW(item), "elm.text", item->label);
627 }
628
629 EAPI Evas_Object *
630 elm_segment_control_item_icon_get(const Evas_Object *obj, int idx)
631 {
632    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
633    Elm_Segment_Item *it;
634
635    it = _item_find(obj, idx);
636    if (it) return it->icon;
637
638    return NULL;
639 }
640
641 EAPI void
642 elm_segment_control_item_icon_set(Elm_Object_Item *it, Evas_Object *icon)
643 {
644    ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
645    Elm_Segment_Item *item = (Elm_Segment_Item *) it;
646
647    //Remove the existing icon
648    if (item->icon)
649      {
650         edje_object_part_unswallow(VIEW(item), item->icon);
651         evas_object_del(item->icon);
652         item->icon = NULL;
653      }
654
655    item->icon = icon;
656    if (item->icon)
657      {
658         elm_widget_sub_object_add(VIEW(item), item->icon);
659         edje_object_part_swallow(VIEW(item), "elm.swallow.icon", item->icon);
660         edje_object_signal_emit(VIEW(item), "elm,state,icon,visible", "elm");
661      }
662    else
663      edje_object_signal_emit(VIEW(item), "elm,state,icon,hidden", "elm");
664 }
665
666 EAPI int
667 elm_segment_control_item_count_get(const Evas_Object *obj)
668 {
669    ELM_CHECK_WIDTYPE(obj, widtype) 0;
670    Widget_Data *wd;
671
672    wd = elm_widget_data_get(obj);
673    if (!wd) return 0;
674
675    return eina_list_count(wd->seg_items);
676 }
677
678 EAPI Evas_Object*
679 elm_segment_control_item_object_get(const Elm_Object_Item *it)
680 {
681    ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
682    return VIEW(it);
683 }
684
685 EAPI Elm_Object_Item*
686 elm_segment_control_item_selected_get(const Evas_Object *obj)
687 {
688    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
689    Widget_Data *wd = elm_widget_data_get(obj);
690    if (!wd) return NULL;
691    return (Elm_Object_Item *) wd->selected_item;
692 }
693
694 EAPI void
695 elm_segment_control_item_selected_set(Elm_Object_Item *it, Eina_Bool selected)
696 {
697    ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
698    Widget_Data *wd;
699    Elm_Segment_Item *item = (Elm_Segment_Item *) it;
700
701    wd = elm_widget_item_data_get(item);
702    if (!wd) return;
703
704    if (item == wd->selected_item)
705      {
706         //already in selected state.
707         if (selected) return;
708
709         //unselect case
710         _segment_off(item);
711      }
712    else if (selected)
713      _segment_on(item);
714
715    return;
716 }
717
718 EAPI Elm_Object_Item *
719 elm_segment_control_item_get(const Evas_Object *obj, int idx)
720 {
721    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
722    return (Elm_Object_Item *) _item_find(obj, idx);
723 }
724
725 EAPI int
726 elm_segment_control_item_index_get(const Elm_Object_Item *it)
727 {
728    ELM_OBJ_ITEM_CHECK_OR_RETURN(it, -1);
729    return ((Elm_Segment_Item *) it)->seg_index;
730 }