1 #include <Elementary.h>
4 typedef struct _Widget_Data Widget_Data;
5 typedef struct _Elm_Segment_Item Elm_Segment_Item;
13 Elm_Segment_Item *selected_item;
17 struct _Elm_Segment_Item
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,
46 static const char SIG_CHANGED[] = "changed";
48 static const Evas_Smart_Cb_Description _signals[] = {
54 _sizing_eval(Evas_Object *obj)
57 Evas_Coord minw = -1, minh = -1;
60 wd = elm_widget_data_get(obj);
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);
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);
74 _del_hook(Evas_Object *obj)
79 wd = elm_widget_data_get(obj);
82 EINA_LIST_FREE(wd->seg_items, it) _item_free(it);
88 _theme_hook(Evas_Object *obj)
95 wd = elm_widget_data_get(obj);
98 _elm_widget_mirrored_reload(obj);
99 rtl = elm_widget_mirrored_get(obj);
100 edje_object_mirrored_set(wd->base, rtl);
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);
107 EINA_LIST_FOREACH(wd->seg_items, l, it)
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);
120 _disable_hook(Evas_Object *obj)
124 wd = elm_widget_data_get(obj);
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
134 _elm_list_data_get(const Eina_List *list)
136 Elm_Segment_Item *it = eina_list_data_get(list);
140 edje_object_signal_emit(VIEW(it), "elm,state,segment,selected", "elm");
145 _focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir,
150 const Eina_List *items;
151 void *(*list_data_get) (const Eina_List *list);
153 wd = elm_widget_data_get(obj);
154 if ((!wd)) return EINA_FALSE;
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;
162 items = wd->seg_items;
163 list_data_get = _elm_list_data_get;
164 if (!items) return EINA_FALSE;
166 return elm_widget_focus_list_next_get(obj, items, list_data_get, dir, next);
171 _item_free(Elm_Segment_Item *it)
177 wd = elm_widget_item_data_get(it);
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);
183 elm_widget_item_pre_notify_del(it);
185 if (it->icon) evas_object_del(it->icon);
186 if (it->label) eina_stringshare_del(it->label);
188 elm_widget_item_del(it);
192 _segment_off(Elm_Segment_Item *it)
198 wd = elm_widget_item_data_get(it);
201 edje_object_signal_emit(VIEW(it), "elm,state,segment,normal", "elm");
203 if (wd->selected_item == it) wd->selected_item = NULL;
207 _segment_on(Elm_Segment_Item *it)
213 wd = elm_widget_item_data_get(it);
215 if (it == wd->selected_item) return;
217 if (wd->selected_item) _segment_off(wd->selected_item);
219 edje_object_signal_emit(VIEW(it), "elm,state,segment,selected", "elm");
221 wd->selected_item = it;
222 evas_object_smart_callback_call(wd->obj, SIG_CHANGED, it);
226 _position_items(Widget_Data *wd)
229 Elm_Segment_Item *it;
231 int bx, by, bw, bh, pos;
233 wd->item_count = eina_list_count(wd->seg_items);
234 if (wd->item_count <= 0) return;
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);
241 pos = bx + bw - wd->item_width;
245 EINA_LIST_FOREACH(wd->seg_items, l, it)
247 evas_object_move(VIEW(it), pos, by);
248 evas_object_resize(VIEW(it), wd->item_width, bh);
250 pos -= wd->item_width;
252 pos += wd->item_width;
254 _sizing_eval(wd->obj);
258 _on_move_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__,
259 void *event_info __UNUSED__)
263 wd = elm_widget_data_get(data);
271 _mouse_up(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__,
275 Elm_Segment_Item *it;
276 Evas_Event_Mouse_Up *ev;
277 Evas_Coord x, y, w, h;
282 wd = elm_widget_item_data_get(it);
285 if (elm_widget_disabled_get(wd->obj)) return;
287 if (it == wd->selected_item) return;
290 evas_object_geometry_get(VIEW(it), &x, &y, &w, &h);
292 if ((ev->canvas.x >= x) && (ev->output.x <= (x + w)) && (ev->canvas.y >= y)
293 && (ev->canvas.y <= (y + h)))
296 edje_object_signal_emit(VIEW(it), "elm,state,segment,normal", "elm");
300 _mouse_down(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__,
301 void *event_info __UNUSED__)
304 Elm_Segment_Item *it;
309 wd = elm_widget_item_data_get(it);
312 if (elm_widget_disabled_get(wd->obj)) return;
314 if (it == wd->selected_item) return;
316 edje_object_signal_emit(VIEW(it), "elm,state,segment,pressed", "elm");
320 _swallow_item_objects(Elm_Segment_Item *it)
326 edje_object_part_swallow(VIEW(it), "elm.swallow.icon", it->icon);
327 edje_object_signal_emit(VIEW(it), "elm,state,icon,visible", "elm");
330 edje_object_signal_emit(VIEW(it), "elm,state,icon,hidden", "elm");
334 edje_object_part_text_set(VIEW(it), "elm.text", it->label);
335 edje_object_signal_emit(VIEW(it), "elm,state,text,visible", "elm");
338 edje_object_signal_emit(VIEW(it), "elm,state,text,hidden", "elm");
339 edje_object_message_signal_process(VIEW(it));
343 _update_list(Widget_Data *wd)
346 Elm_Segment_Item *it;
352 if (wd->item_count == 1)
354 it = eina_list_nth(wd->seg_items, 0);
357 //Set the segment type
358 edje_object_signal_emit(VIEW(it),
359 "elm,type,segment,single", "elm");
361 //Set the segment state
362 if (wd->selected_item == it)
363 edje_object_signal_emit(VIEW(it),
364 "elm,state,segment,selected", "elm");
366 edje_object_signal_emit(VIEW(it),
367 "elm,state,segment,normal", "elm");
369 if (elm_widget_disabled_get(wd->obj))
370 edje_object_signal_emit(VIEW(it), "elm,state,disabled", "elm");
372 edje_object_signal_emit(VIEW(it), "elm,state,enabled", "elm");
374 _swallow_item_objects(it);
378 rtl = elm_widget_mirrored_get(wd->obj);
379 EINA_LIST_FOREACH(wd->seg_items, l, it)
383 //Set the segment type
387 edje_object_signal_emit(VIEW(it),
388 "elm,type,segment,right", "elm");
390 edje_object_signal_emit(VIEW(it),
391 "elm,type,segment,left", "elm");
393 else if (idx == (wd->item_count - 1))
396 edje_object_signal_emit(VIEW(it),
397 "elm,type,segment,left", "elm");
399 edje_object_signal_emit(VIEW(it),
400 "elm,type,segment,right", "elm");
403 edje_object_signal_emit(VIEW(it),
404 "elm,type,segment,middle", "elm");
406 //Set the segment state
407 if (wd->selected_item == it)
408 edje_object_signal_emit(VIEW(it),
409 "elm,state,segment,selected", "elm");
411 edje_object_signal_emit(VIEW(it),
412 "elm,state,segment,normal", "elm");
414 if (elm_widget_disabled_get(wd->obj))
415 edje_object_signal_emit(VIEW(it), "elm,state,disabled", "elm");
417 edje_object_signal_emit(VIEW(it), "elm,state,enabled", "elm");
419 _swallow_item_objects(it);
424 static Elm_Segment_Item *
425 _item_find(const Evas_Object *obj, int idx)
428 Elm_Segment_Item *it;
430 wd = elm_widget_data_get(obj);
431 if (!wd) return NULL;
433 it = eina_list_nth(wd->seg_items, idx);
437 static Elm_Segment_Item*
438 _item_new(Evas_Object *obj, Evas_Object *icon, const char *label)
440 Elm_Segment_Item *it;
443 wd = elm_widget_data_get(obj);
444 if (!wd) return NULL;
446 it = elm_widget_item_new(obj, Elm_Segment_Item);
447 if (!it) return NULL;
448 elm_widget_item_data_set(it, wd);
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));
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)));
463 eina_stringshare_replace(&it->label, label);
465 edje_object_signal_emit(VIEW(it), "elm,state,text,visible", "elm");
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);
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,
475 evas_object_event_callback_add(VIEW(it), EVAS_CALLBACK_MOUSE_UP,
477 evas_object_show(VIEW(it));
483 elm_segment_control_add(Evas_Object *parent)
489 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
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);
499 // TODO: Focus switch support to Elm_widget_Item not supported yet.
501 elm_widget_focus_next_hook_set(obj, _focus_next_hook);
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);
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);
517 evas_object_smart_callbacks_descriptions_set(obj, _signals);
522 EAPI Elm_Object_Item *
523 elm_segment_control_item_add(Evas_Object *obj, Evas_Object *icon,
526 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
527 Elm_Segment_Item *it;
530 wd = elm_widget_data_get(obj);
531 if (!wd) return NULL;
533 it = _item_new(obj, icon, label);
534 if (!it) return NULL;
536 wd->seg_items = eina_list_append(wd->seg_items, it);
539 return (Elm_Object_Item *) it;
542 EAPI Elm_Object_Item *
543 elm_segment_control_item_insert_at(Evas_Object *obj, Evas_Object *icon,
544 const char *label, int idx)
546 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
547 Elm_Segment_Item *it, *it_rel;
550 wd = elm_widget_data_get(obj);
551 if (!wd) return NULL;
552 if (idx < 0) idx = 0;
554 it = _item_new(obj, icon, label);
555 if (!it) return NULL;
557 it_rel = _item_find(obj, idx);
559 wd->seg_items = eina_list_prepend_relative(wd->seg_items, it, it_rel);
561 wd->seg_items = eina_list_append(wd->seg_items, it);
564 return (Elm_Object_Item *) it;
568 elm_segment_control_item_del(Elm_Object_Item *it)
570 ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
572 Elm_Segment_Item *item = (Elm_Segment_Item *) it;
574 wd = elm_widget_item_data_get(item);
582 elm_segment_control_item_del_at(Evas_Object *obj, int idx)
584 ELM_CHECK_WIDTYPE(obj, widtype);
585 Elm_Segment_Item *it;
588 wd = elm_widget_data_get(obj);
591 it = _item_find(obj, idx);
598 elm_segment_control_item_label_get(const Evas_Object *obj, int idx)
600 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
601 Elm_Segment_Item *it;
603 it = _item_find(obj, idx);
604 if (it) return it->label;
610 elm_segment_control_item_label_set(Elm_Object_Item* it, const char* label)
612 ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
614 Elm_Segment_Item *item = (Elm_Segment_Item *) it;
616 wd = elm_widget_item_data_get(item);
619 eina_stringshare_replace(&item->label, label);
621 edje_object_signal_emit(VIEW(item), "elm,state,text,visible", "elm");
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);
630 elm_segment_control_item_icon_get(const Evas_Object *obj, int idx)
632 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
633 Elm_Segment_Item *it;
635 it = _item_find(obj, idx);
636 if (it) return it->icon;
642 elm_segment_control_item_icon_set(Elm_Object_Item *it, Evas_Object *icon)
644 ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
645 Elm_Segment_Item *item = (Elm_Segment_Item *) it;
647 //Remove the existing icon
650 edje_object_part_unswallow(VIEW(item), item->icon);
651 evas_object_del(item->icon);
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");
663 edje_object_signal_emit(VIEW(item), "elm,state,icon,hidden", "elm");
667 elm_segment_control_item_count_get(const Evas_Object *obj)
669 ELM_CHECK_WIDTYPE(obj, widtype) 0;
672 wd = elm_widget_data_get(obj);
675 return eina_list_count(wd->seg_items);
679 elm_segment_control_item_object_get(const Elm_Object_Item *it)
681 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
685 EAPI Elm_Object_Item*
686 elm_segment_control_item_selected_get(const Evas_Object *obj)
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;
695 elm_segment_control_item_selected_set(Elm_Object_Item *it, Eina_Bool selected)
697 ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
699 Elm_Segment_Item *item = (Elm_Segment_Item *) it;
701 wd = elm_widget_item_data_get(item);
704 if (item == wd->selected_item)
706 //already in selected state.
707 if (selected) return;
718 EAPI Elm_Object_Item *
719 elm_segment_control_item_get(const Evas_Object *obj, int idx)
721 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
722 return (Elm_Object_Item *) _item_find(obj, idx);
726 elm_segment_control_item_index_get(const Elm_Object_Item *it)
728 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, -1);
729 return ((Elm_Segment_Item *) it)->seg_index;