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)
85 elm_widget_item_free(it);
92 _theme_hook(Evas_Object *obj)
99 wd = elm_widget_data_get(obj);
102 _elm_widget_mirrored_reload(obj);
103 rtl = elm_widget_mirrored_get(obj);
104 edje_object_mirrored_set(wd->base, rtl);
106 _elm_theme_object_set(obj, wd->base, "segment_control", "base",
107 elm_widget_style_get(obj));
108 edje_object_scale_set(wd->base, elm_widget_scale_get(wd->base)
109 *_elm_config->scale);
111 EINA_LIST_FOREACH(wd->seg_items, l, it)
113 _elm_theme_object_set(obj, VIEW(it), "segment_control",
114 "item", elm_widget_style_get(obj));
115 edje_object_scale_set(VIEW(it), elm_widget_scale_get(VIEW(it))
116 *_elm_config->scale);
117 edje_object_mirrored_set(VIEW(it), rtl);
124 _disable_hook(Evas_Object *obj)
128 wd = elm_widget_data_get(obj);
133 // TODO: Elm_widget elm_widget_focus_list_next_get supports only Elm_widget list,
134 // Not the Elm_Widget_item. Focus switching with in widget not supported until
135 // it is supported in elm_widget
138 _elm_list_data_get(const Eina_List *list)
140 Elm_Segment_Item *it = eina_list_data_get(list);
144 edje_object_signal_emit(VIEW(it), "elm,state,segment,selected", "elm");
149 _focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir,
154 const Eina_List *items;
155 void *(*list_data_get) (const Eina_List *list);
157 wd = elm_widget_data_get(obj);
158 if ((!wd)) return EINA_FALSE;
161 /* TODO: Change this to use other chain */
162 if ((items = elm_widget_focus_custom_chain_get(obj)))
163 list_data_get = eina_list_data_get;
166 items = wd->seg_items;
167 list_data_get = _elm_list_data_get;
168 if (!items) return EINA_FALSE;
170 return elm_widget_focus_list_next_get(obj, items, list_data_get, dir, next);
175 _item_free(Elm_Segment_Item *it)
181 wd = elm_widget_item_data_get(it);
184 if (wd->selected_item == it) wd->selected_item = NULL;
185 if (wd->seg_items) wd->seg_items = eina_list_remove(wd->seg_items, it);
187 if (it->icon) evas_object_del(it->icon);
188 if (it->label) eina_stringshare_del(it->label);
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);
438 _item_text_set_hook(Elm_Object_Item *it, const char *part, const char *label)
441 Elm_Segment_Item *item;
443 if (part && strcmp(part, "default")) return;
445 item = (Elm_Segment_Item *)it;
446 wd = elm_widget_item_data_get(item);
449 eina_stringshare_replace(&item->label, label);
451 edje_object_signal_emit(VIEW(item), "elm,state,text,visible", "elm");
453 edje_object_signal_emit(VIEW(item), "elm,state,text,hidden", "elm");
454 edje_object_message_signal_process(VIEW(item));
455 //label can be NULL also.
456 edje_object_part_text_set(VIEW(item), "elm.text", item->label);
460 _item_text_get_hook(const Elm_Object_Item *it, const char *part)
462 if (part && strcmp(part, "default")) return NULL;
463 return ((Elm_Segment_Item *)it)->label;
467 _item_content_set_hook(Elm_Object_Item *it,
469 Evas_Object *content)
471 Elm_Segment_Item *item;
472 if (part && strcmp(part, "icon")) return;
474 item = (Elm_Segment_Item *)it;
475 if (content == item->icon) return;
477 //Remove the existing icon
478 if (item->icon) evas_object_del(item->icon);
479 item->icon = content;
482 elm_widget_sub_object_add(VIEW(item), item->icon);
483 edje_object_part_swallow(VIEW(item), "elm.swallow.icon", item->icon);
484 edje_object_signal_emit(VIEW(item), "elm,state,icon,visible", "elm");
487 edje_object_signal_emit(VIEW(item), "elm,state,icon,hidden", "elm");
491 _item_content_get_hook(const Elm_Object_Item *it, const char *part)
493 if (part && strcmp(part, "icon")) return NULL;
494 return ((Elm_Segment_Item *)it)->icon;
498 _item_del_pre_hook(Elm_Object_Item *it)
501 Elm_Segment_Item *item = (Elm_Segment_Item *)it;
503 wd = elm_widget_item_data_get(item);
504 if (!wd) return EINA_FALSE;
512 static Elm_Segment_Item*
513 _item_new(Evas_Object *obj, Evas_Object *icon, const char *label)
515 Elm_Segment_Item *it;
518 wd = elm_widget_data_get(obj);
519 if (!wd) return NULL;
521 it = elm_widget_item_new(obj, Elm_Segment_Item);
522 if (!it) return NULL;
523 elm_widget_item_data_set(it, wd);
524 elm_widget_item_del_pre_hook_set(it, _item_del_pre_hook);
525 elm_widget_item_text_set_hook_set(it, _item_text_set_hook);
526 elm_widget_item_text_get_hook_set(it, _item_text_get_hook);
527 elm_widget_item_content_set_hook_set(it, _item_content_set_hook);
528 elm_widget_item_content_get_hook_set(it, _item_content_get_hook);
530 VIEW(it) = edje_object_add(evas_object_evas_get(obj));
531 edje_object_scale_set(VIEW(it), elm_widget_scale_get(VIEW(it))
532 *_elm_config->scale);
533 evas_object_smart_member_add(VIEW(it), obj);
534 elm_widget_sub_object_add(obj, VIEW(it));
535 _elm_theme_object_set(obj, VIEW(it), "segment_control", "item",
536 elm_object_style_get(obj));
537 edje_object_mirrored_set(VIEW(it),
538 elm_widget_mirrored_get(WIDGET(it)));
541 eina_stringshare_replace(&it->label, label);
543 edje_object_signal_emit(VIEW(it), "elm,state,text,visible", "elm");
545 edje_object_signal_emit(VIEW(it), "elm,state,text,hidden", "elm");
546 edje_object_message_signal_process(VIEW(it));
547 edje_object_part_text_set(VIEW(it), "elm.text", label);
550 if (it->icon) elm_widget_sub_object_add(VIEW(it), it->icon);
551 evas_object_event_callback_add(VIEW(it), EVAS_CALLBACK_MOUSE_DOWN,
553 evas_object_event_callback_add(VIEW(it), EVAS_CALLBACK_MOUSE_UP,
555 evas_object_show(VIEW(it));
561 elm_segment_control_add(Evas_Object *parent)
567 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
569 ELM_SET_WIDTYPE(widtype, "segment_control");
570 elm_widget_type_set(obj, "segment_control");
571 elm_widget_sub_object_add(parent, obj);
572 elm_widget_data_set(obj, wd);
573 elm_widget_del_hook_set(obj, _del_hook);
574 elm_widget_theme_hook_set(obj, _theme_hook);
575 elm_widget_disable_hook_set(obj, _disable_hook);
577 // TODO: Focus switch support to Elm_widget_Item not supported yet.
579 elm_widget_focus_next_hook_set(obj, _focus_next_hook);
584 wd->base = edje_object_add(e);
585 edje_object_scale_set(wd->base, elm_widget_scale_get(wd->base)
586 *_elm_config->scale);
587 _elm_theme_object_set(obj, wd->base, "segment_control", "base", "default");
588 elm_widget_resize_object_set(obj, wd->base);
590 evas_object_event_callback_add(obj, EVAS_CALLBACK_RESIZE,
591 _on_move_resize, obj);
592 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOVE,
593 _on_move_resize, obj);
595 evas_object_smart_callbacks_descriptions_set(obj, _signals);
600 EAPI Elm_Object_Item *
601 elm_segment_control_item_add(Evas_Object *obj, Evas_Object *icon,
604 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
605 Elm_Segment_Item *it;
608 wd = elm_widget_data_get(obj);
609 if (!wd) return NULL;
611 it = _item_new(obj, icon, label);
612 if (!it) return NULL;
614 wd->seg_items = eina_list_append(wd->seg_items, it);
617 return (Elm_Object_Item *)it;
620 EAPI Elm_Object_Item *
621 elm_segment_control_item_insert_at(Evas_Object *obj, Evas_Object *icon,
622 const char *label, int idx)
624 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
625 Elm_Segment_Item *it, *it_rel;
628 wd = elm_widget_data_get(obj);
629 if (!wd) return NULL;
630 if (idx < 0) idx = 0;
632 it = _item_new(obj, icon, label);
633 if (!it) return NULL;
635 it_rel = _item_find(obj, idx);
637 wd->seg_items = eina_list_prepend_relative(wd->seg_items, it, it_rel);
639 wd->seg_items = eina_list_append(wd->seg_items, it);
642 return (Elm_Object_Item *)it;
646 elm_segment_control_item_del_at(Evas_Object *obj, int idx)
648 ELM_CHECK_WIDTYPE(obj, widtype);
649 Elm_Segment_Item *it;
652 wd = elm_widget_data_get(obj);
655 it = _item_find(obj, idx);
657 elm_object_item_del((Elm_Object_Item *)it);
661 elm_segment_control_item_label_get(const Evas_Object *obj, int idx)
663 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
664 Elm_Segment_Item *it = _item_find(obj, idx);
665 if (it) return it->label;
670 elm_segment_control_item_icon_get(const Evas_Object *obj, int idx)
672 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
673 Elm_Segment_Item *it = _item_find(obj, idx);
674 if (it) return it->icon;
679 elm_segment_control_item_count_get(const Evas_Object *obj)
681 ELM_CHECK_WIDTYPE(obj, widtype) 0;
684 wd = elm_widget_data_get(obj);
687 return eina_list_count(wd->seg_items);
691 elm_segment_control_item_object_get(const Elm_Object_Item *it)
693 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
697 EAPI Elm_Object_Item*
698 elm_segment_control_item_selected_get(const Evas_Object *obj)
700 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
701 Widget_Data *wd = elm_widget_data_get(obj);
702 if (!wd) return NULL;
703 return (Elm_Object_Item *) wd->selected_item;
707 elm_segment_control_item_selected_set(Elm_Object_Item *it, Eina_Bool selected)
709 ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
711 Elm_Segment_Item *item = (Elm_Segment_Item *)it;
713 wd = elm_widget_item_data_get(item);
716 if (item == wd->selected_item)
718 //already in selected state.
719 if (selected) return;
730 EAPI Elm_Object_Item *
731 elm_segment_control_item_get(const Evas_Object *obj, int idx)
733 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
734 return (Elm_Object_Item *) _item_find(obj, idx);
738 elm_segment_control_item_index_get(const Elm_Object_Item *it)
740 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, -1);
741 return ((Elm_Segment_Item *)it)->seg_index;