1 #include <Elementary.h>
5 * @defgroup SegmentControl SegmentControl
8 * SegmentControl object is a horizontal control made of multiple segments,
9 * each segment functioning as a discrete button. A segmented control affords a compact means to group together a number of controls.
10 * A segmented control can display a title or an image. The UISegmentedControl object automatically resizes segments to fit proportionally
11 * within their superview unless they have a specific width set. When you add and remove segments,
12 * you can request that the action be animated with sliding and fading effects.
14 typedef struct _Widget_Data Widget_Data;
25 Elm_Segment_Item *ani_it;
28 unsigned int insert_index;
29 unsigned int del_index;
30 unsigned int cur_seg_id;
32 Eina_Bool longpressed : 1;
35 struct _Elm_Segment_Item
41 Eina_Bool delete_me : 1;
43 Ecore_Timer *long_timer;
46 static void _mouse_down(void *data, Evas_Object *obj, const char *emission, const char *source);
47 static void _mouse_up(void *data, Evas_Object *obj, const char *emission, const char *source);
48 static void _signal_segment_on(void *data);
49 static void _theme_hook(Evas_Object *obj);
50 static void _item_free(Evas_Object *obj, Elm_Segment_Item *it);
51 static void _del_hook(Evas_Object *obj);
52 static void _layout(Evas_Object *o, Evas_Object_Box_Data *priv, void *data);
53 static void _update_list(Evas_Object *obj);
54 static void _refresh_segment_ids(Evas_Object *obj);
55 static void _state_value_set(Evas_Object *obj);
57 static Elm_Segment_Item* _item_new(Evas_Object *obj, const char *label, Evas_Object *icon);
58 static Elm_Segment_Item *_item_find(Evas_Object *obj, unsigned int index);
60 static int * _animator_animate_add_cb(Evas_Object *obj);
61 static int * _animator_animate_del_cb(Evas_Object *obj);
64 _on_focus_hook(void *data, Evas_Object *obj)
66 Widget_Data *wd = elm_widget_data_get(obj);
71 if (elm_widget_focus_get(obj))
73 edje_object_signal_emit((Evas_Object *)wd->seg_ctrl, "elm,action,focus", "elm");
74 evas_object_focus_set((Evas_Object *)wd->seg_ctrl, 1);
78 edje_object_signal_emit((Evas_Object *)wd->seg_ctrl, "elm,action,unfocus", "elm");
79 evas_object_focus_set((Evas_Object *)wd->seg_ctrl, 0);
80 EINA_LIST_FOREACH(wd->seg_ctrl, l, it)
82 if (it->segment_id == wd->cur_seg_id) {
83 edje_object_signal_emit(it->base, "elm,action,unfocus", "elm");
84 edje_object_signal_emit(it->base, "elm,state,segment,off", "elm");
85 edje_object_signal_emit(it->base, "elm,state,text,visible", "elm");
93 _mouse_up(void *data, Evas_Object *obj, const char *emission, const char *source)
95 Elm_Segment_Item *item = (Elm_Segment_Item *) data;
96 Widget_Data *wd = elm_widget_data_get(item->obj);
100 if(wd->longpressed == EINA_FALSE)
102 edje_object_signal_emit(item->base, "elm,action,unfocus", "elm");
103 edje_object_signal_emit(item->base, "elm,state,text,visible", "elm");
105 if (item->long_timer)
107 ecore_timer_del(item->long_timer);
108 item->long_timer = NULL;
114 _signal_segment_on(void *data)
116 Elm_Segment_Item *item = (Elm_Segment_Item *) data;
117 Widget_Data *wd = elm_widget_data_get(item->obj);
121 if (item->long_timer)
123 ecore_timer_del(item->long_timer);
124 item->long_timer = NULL;
126 wd->longpressed = EINA_TRUE;
127 edje_object_signal_emit(item->base, "elm,state,segment,on", "elm");
128 edje_object_signal_emit(item->base, "elm,state,text,change", "elm");
130 Elm_Segment_Item *it;
133 if (item->segment_id == wd->cur_seg_id)
135 wd->cur_seg_id = item->segment_id;
136 evas_object_smart_callback_call(item->obj, "changed", (void*)wd->cur_seg_id);
139 EINA_LIST_FOREACH(wd->seg_ctrl, l, it)
141 if (it->segment_id == wd->cur_seg_id)
143 edje_object_signal_emit(it->base, "elm,action,unfocus", "elm");
144 edje_object_signal_emit(it->base, "elm,state,segment,off", "elm");
145 edje_object_signal_emit(it->base, "elm,state,text,visible", "elm");
149 wd->cur_seg_id = item->segment_id;
150 evas_object_smart_callback_call(item->obj, "changed", (void*)wd->cur_seg_id);
155 _mouse_down(void *data, Evas *evas, Evas_Object *obj, void *event_info)
157 Elm_Segment_Item *item = (Elm_Segment_Item *) data;
158 Widget_Data *wd = elm_widget_data_get(item->obj);
161 wd->longpressed = EINA_FALSE;
163 edje_object_signal_emit(item->base, "elm,action,focus", "elm");
164 edje_object_signal_emit(item->base, "elm,state,text,visible", "elm");
166 if (item->long_timer) ecore_timer_del(item->long_timer);
167 item->long_timer = ecore_timer_add(0.5, _signal_segment_on, item);
171 _theme_hook(Evas_Object *obj)
173 _elm_theme_object_set(obj, obj, "segmented-control", "base", elm_widget_style_get(obj));
179 _item_free(Evas_Object *obj, Elm_Segment_Item *it)
181 Widget_Data *wd = elm_widget_data_get(obj);
185 wd->seg_ctrl = eina_list_remove(wd->seg_ctrl, it);
187 if(it->icon) evas_object_del(it->icon);
188 if(it->base) evas_object_del(it->base);
189 if(it->label) eina_stringshare_del(it->label);
190 if (it->long_timer) ecore_timer_del(it->long_timer);
199 _del_hook(Evas_Object *obj)
201 Widget_Data *wd = elm_widget_data_get(obj);
202 Elm_Segment_Item *it;
203 Eina_List *l, *clear = NULL;
205 EINA_LIST_FOREACH(wd->seg_ctrl, l, it) clear = eina_list_append(clear, it);
206 EINA_LIST_FREE(clear, it) _item_free(obj, it);
216 _layout(Evas_Object *o, Evas_Object_Box_Data *priv, void *data)
218 Widget_Data *wd = data;
220 _els_box_layout(o, priv, 1, 0); /* making box layout non homogenous */
226 _segment_resizing(void *data)
228 Widget_Data *wd = elm_widget_data_get((Evas_Object *)data);
230 Evas_Coord w = 0, h = 0;
232 evas_object_geometry_get(wd->base, NULL, NULL, &w, &h);
233 wd->item_width = wd->width = w;
235 _state_value_set((Evas_Object *)data);
238 static void _object_resize(void *data, Evas *e, Evas_Object *obj, void *event_info)
242 wd = elm_widget_data_get((Evas_Object *)data);
245 ecore_job_add(_segment_resizing, (Evas_Object *)data);
249 * Add a new segmentcontrol to the parent
250 * @param parent The parent object
251 * @return The new object or NULL if it cannot be created
253 * @ingroup SegmentControl SegmentControl
256 elm_segment_control_add(Evas_Object *parent)
262 wd = ELM_NEW(Widget_Data);
263 e = evas_object_evas_get(parent);
264 obj = elm_widget_add(e);
265 elm_widget_type_set(obj, "segmented-control");
266 elm_widget_sub_object_add(parent, obj);
267 elm_widget_on_focus_hook_set( obj, _on_focus_hook, NULL );
268 elm_widget_data_set(obj, wd);
269 elm_widget_del_hook_set(obj, _del_hook);
270 elm_widget_theme_hook_set(obj, _theme_hook);
272 wd->base = edje_object_add(e);
273 _elm_theme_object_set(obj, wd->base, "segmented-control", "base", "default");
274 elm_widget_resize_object_set(obj, wd->base);
275 wd->box = evas_object_box_add(e);
276 evas_object_box_layout_set(wd->box, _layout, wd, NULL);
277 elm_widget_sub_object_add(obj, wd->box);
278 edje_object_part_swallow(wd->base, "elm.swallow.content", wd->box);
279 evas_object_show(wd->box);
281 evas_object_event_callback_add(obj, EVAS_CALLBACK_RESIZE, _object_resize, obj);
284 wd->insert_index = 0;
290 static Elm_Segment_Item*
291 _item_new(Evas_Object *obj, const char *label, Evas_Object *icon)
293 Widget_Data *wd = elm_widget_data_get(obj);
294 if (!wd) return NULL;
296 Elm_Segment_Item *it;
297 it = calloc(1, sizeof( Elm_Segment_Item));
298 if (!it) return NULL;
302 if(obj) it->obj = obj;
303 it->delete_me = EINA_FALSE;
304 it->segment_id = wd->id;
306 it->base = edje_object_add(evas_object_evas_get(obj));
307 _elm_theme_object_set(obj, it->base, "segment", "base", elm_object_style_get(it->base));
309 if (it->label) eina_stringshare_del(it->label);
312 it->label = eina_stringshare_add(label);
319 if ((it->icon != icon) && (it->icon))
320 elm_widget_sub_object_del(obj, it->icon);
324 elm_widget_sub_object_add(obj, icon);
325 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
326 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
327 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
329 evas_object_size_hint_weight_set(it->base, 1.0, -1.0);
330 evas_object_size_hint_align_set(it->base, 1.0, -1.0);
331 evas_object_size_hint_min_set(it->base, -1, -1);
332 evas_object_size_hint_max_set(it->base, maxw, maxh);
335 edje_object_size_min_restricted_calc(obj, &mw, &mh, 0, 0);
336 evas_object_size_hint_weight_set(obj, 1.0, 1.0);
337 evas_object_size_hint_align_set(obj, -1.0, -1.0);
343 static void _update_list(Evas_Object *obj)
345 Widget_Data *wd = elm_widget_data_get(obj);
348 Elm_Segment_Item *it;
351 wd->count = eina_list_count(wd->seg_ctrl);
355 it = _item_find(obj, 0);
356 _elm_theme_object_set(obj, it->base, "segment", "base", "single"); edje_object_signal_emit(it->base, "elm,state,segment,on", "elm");
357 edje_object_signal_emit(it->base, "elm,state,text,visible", "elm");
358 edje_object_signal_emit(it->base, "elm,state,text,change", "elm");
359 edje_object_message_signal_process(it->base);
361 edje_object_part_text_set(it->base, "elm.text", it->label);
363 edje_object_part_swallow(it->base, "elm.swallow.content", it->icon);
364 edje_object_signal_emit(it->base, "elm,state,icon,visible", "elm");
368 EINA_LIST_FOREACH(wd->seg_ctrl, l, it)
372 _elm_theme_object_set(obj, it->base, "segment", "base", "first");
374 else if(i==wd->count-1)
376 _elm_theme_object_set(obj, it->base, "segment", "base", "last");
380 _elm_theme_object_set(obj, it->base, "segment", "base", "default");
383 edje_object_signal_emit(it->base, "elm,state,text,visible", "elm");
384 edje_object_message_signal_process(it->base);
386 edje_object_part_text_set(it->base, "elm.text", it->label);
388 edje_object_part_swallow(it->base, "elm.swallow.content", it->icon);
389 edje_object_signal_emit(it->base, "elm,state,icon,visible", "elm");
396 static void _refresh_segment_ids(Evas_Object *obj)
398 Widget_Data *wd = elm_widget_data_get(obj);
400 Elm_Segment_Item *it;
402 if (wd->insert_index && wd->cur_seg_id >= wd->insert_index)
405 wd->insert_index = 0;
409 if (wd->cur_seg_id >= wd->del_index)
414 EINA_LIST_FOREACH(wd->seg_ctrl, l, it)
421 static void _state_value_set(Evas_Object *obj)
423 Widget_Data *wd = elm_widget_data_get(obj);
425 Elm_Segment_Item *it;
427 Evas_Coord mw, mh, x, y;
429 unsigned int count = eina_list_count(wd->seg_ctrl);
431 wd->item_width = wd->width/count;
434 evas_object_geometry_get(wd->ani_it->base, &x, &y, &w1, NULL);
435 if (wd->ani_it->delete_me)
437 w1-=(wd->item_width/15);
442 w1+=(wd->item_width/15);
443 if( w1 > wd->item_width )
446 w2 = (wd->width-w1)/(count -1);
453 EINA_LIST_FOREACH(wd->seg_ctrl, l, it)
455 edje_object_size_min_restricted_calc(it->base, &mw, &mh, 0, 0);
456 evas_object_size_hint_weight_set(it->base, 1.0, 1.0);
457 evas_object_size_hint_align_set(it->base, -1.0, -1.0);
459 if(wd->ani_it && it == wd->ani_it)
461 evas_object_resize(it->base, w1, wd->height);
462 evas_object_size_hint_min_set(it->base, w1, wd->height);
463 evas_object_size_hint_max_set(it->base, w1, wd->height);
467 evas_object_resize(it->base, w2, wd->height);
468 evas_object_size_hint_min_set(it->base, w2, wd->height);
469 evas_object_size_hint_max_set(it->base, w2, wd->height);
479 _animator_animate_add_cb(Evas_Object *obj)
481 Widget_Data *wd = elm_widget_data_get(obj);
485 evas_object_geometry_get(wd->ani_it->base, NULL, NULL, &w, NULL);
486 if( w < wd->item_width )
488 _state_value_set(obj);
489 evas_object_geometry_get(wd->ani_it->base, NULL, NULL, &w, NULL);
490 return ECORE_CALLBACK_RENEW;
494 ecore_animator_del(wd->ani);
497 return ECORE_CALLBACK_CANCEL;
503 _animator_animate_del_cb(Evas_Object *obj)
505 Widget_Data *wd = elm_widget_data_get(obj);
508 evas_object_geometry_get(wd->ani_it->base, NULL, NULL, &w, NULL);
511 _state_value_set(obj);
512 evas_object_geometry_get(wd->ani_it->base, NULL, NULL, &w, NULL);
513 return ECORE_CALLBACK_RENEW;
517 _item_free(obj, wd->ani_it );
518 _refresh_segment_ids(obj);
519 ecore_animator_del(wd->ani);
523 wd->id = eina_list_count(wd->seg_ctrl);
524 return ECORE_CALLBACK_CANCEL;
529 * Add a new segment to segmentcontrol
530 * @param obj The SegmentControl object
531 * @param icon The icon object for added segment
532 * @param label The label for added segment
533 * @param animate If 1 the action be animated with sliding effects default 0.
534 * @return The new segment or NULL if it cannot be created
536 * @ingroup SegmentControl SegmentControl
539 EAPI Elm_Segment_Item *
540 elm_segment_control_add_segment(Evas_Object *obj, Evas_Object *icon, const char *label, Eina_Bool animate)
542 Widget_Data *wd = elm_widget_data_get(obj);
546 Elm_Segment_Item *it;
548 it = _item_new(obj, label, icon);
551 wd->seg_ctrl = eina_list_append(wd->seg_ctrl, it);
552 wd->id = eina_list_count(wd->seg_ctrl);
554 evas_object_event_callback_add(it->base, EVAS_CALLBACK_MOUSE_DOWN, _mouse_down, it);
555 evas_object_event_callback_add(it->base, EVAS_CALLBACK_MOUSE_UP,_mouse_up, it);
556 wd->insert_index = 0;
558 _refresh_segment_ids(obj);
560 if(animate && it->segment_id && wd->ani_it == NULL)
562 evas_object_resize(it->base, 1, wd->height);
564 wd->ani = ecore_animator_add( _animator_animate_add_cb, obj );
567 _state_value_set(obj);
568 evas_object_show( it->base);
570 evas_object_box_append(wd->box, it->base);
571 evas_object_smart_calculate(wd->box);
577 static Elm_Segment_Item *
578 _item_find(Evas_Object *obj, unsigned int index)
580 Widget_Data *wd = elm_widget_data_get(obj);
581 if (!wd) return NULL;
583 Elm_Segment_Item *it;
586 EINA_LIST_FOREACH(wd->seg_ctrl, l, it)
597 static Elm_Segment_Item *
598 _item_search(Evas_Object *obj, Elm_Segment_Item *item)
600 Widget_Data *wd = elm_widget_data_get(obj);
604 Elm_Segment_Item *it;
606 EINA_LIST_FOREACH(wd->seg_ctrl, l, it)
616 * Insert a new segment to segmentcontrol
617 * @param obj The SegmentControl object
618 * @param icon The icon object for added segment
619 * @param label The label for added segment
620 * @param index The position at which segment to be inserted
621 * @param animate If 1 the action be animated with sliding effects default 0.
622 * @return The new segment or NULL if it cannot be created
624 * @ingroup SegmentControl SegmentControl
627 elm_segment_control_insert_segment_at(Evas_Object *obj, Evas_Object *icon, const char *label, unsigned int index, Eina_Bool animate)
629 Widget_Data *wd = elm_widget_data_get(obj);
632 Elm_Segment_Item *it, *it_rel;
634 it = _item_new(obj, label, icon);
635 it_rel = _item_find(obj, index);
638 wd->seg_ctrl = eina_list_append(wd->seg_ctrl, it);
643 wd->seg_ctrl = eina_list_prepend_relative(wd->seg_ctrl, it, it_rel);
645 evas_object_event_callback_add(it->base, EVAS_CALLBACK_MOUSE_DOWN, _mouse_down, it);
646 evas_object_event_callback_add(it->base, EVAS_CALLBACK_MOUSE_UP,_mouse_up, it);
647 wd->insert_index = index;
648 wd->id = eina_list_count(wd->seg_ctrl);
649 _refresh_segment_ids(obj);
652 if(animate && it->segment_id && wd->ani_it == NULL)
655 evas_object_resize(it->base, 1, wd->height);
656 wd->ani = ecore_animator_add( _animator_animate_add_cb, obj );
659 _state_value_set(obj);
661 evas_object_show( it->base);
663 if(index >= wd->id-1)
665 evas_object_box_append(wd->box, it->base);
669 evas_object_box_insert_at(wd->box, it->base, index);
672 evas_object_smart_calculate(wd->box);
677 * Delete a segment to segmentcontrol
678 * @param obj The SegmentControl object
679 * @param item The Segment to be deleted
680 * @param animate If 1 the action be animated with sliding effects default 0.
682 * @ingroup SegmentControl SegmentControl
685 elm_segment_control_delete_segment(Evas_Object *obj, Elm_Segment_Item *item, Eina_Bool animate)
687 Widget_Data *wd = elm_widget_data_get(obj);
692 Elm_Segment_Item *it;
693 it = _item_search(obj, item);
696 wd->del_index = it->segment_id;
697 if(animate && it->segment_id && wd->ani_it == NULL)
699 it->delete_me = EINA_TRUE;
701 wd->ani = ecore_animator_add( _animator_animate_del_cb, obj );
705 evas_object_box_remove(wd->box, it->base);
706 evas_object_smart_calculate(wd->box);
708 _refresh_segment_ids(obj);
709 _state_value_set(obj);
712 wd->id = eina_list_count(wd->seg_ctrl);
717 * Delete a segment to segmentcontrol
718 * @param obj The SegmentControl object
719 * @param index The position at which segment to be deleted
720 * @param animate If 1 the action be animated with sliding effects default 0.
722 * @ingroup SegmentControl SegmentControl
726 elm_segment_control_delete_segment_at(Evas_Object *obj, unsigned int index, Eina_Bool animate)
728 Widget_Data *wd = elm_widget_data_get(obj);
730 Elm_Segment_Item *it, *it_rel;
732 it = _item_find(obj, index);
736 wd->del_index = index;
737 if(animate && it->segment_id)
739 if(wd->ani_it == NULL)
742 it->delete_me = EINA_TRUE;
743 wd->ani = ecore_animator_add( _animator_animate_del_cb, obj );
747 wd->queue = eina_list_append(wd->queue, it);
752 evas_object_box_remove(wd->box, it->base);
753 evas_object_smart_calculate(wd->box);
755 _refresh_segment_ids(obj);
756 _state_value_set(obj);
759 wd->id = eina_list_count(wd->seg_ctrl);
764 * Get the label of a segment of segmentcontrol
765 * @param obj The SegmentControl object
766 * @param index The index of the segment
769 * @ingroup SegmentControl SegmentControl
773 elm_segment_control_get_segment_label_at(Evas_Object *obj, unsigned int index)
775 Elm_Segment_Item *it_rel;
776 Widget_Data *wd = elm_widget_data_get(obj);
779 it_rel = _item_find(obj, index);
781 if(it_rel) return it_rel->label;
787 * Get the icon of a segment of segmentcontrol
788 * @param obj The SegmentControl object
789 * @param index The index of the segment
790 * @return The icon object
792 * @ingroup SegmentControl SegmentControl
796 elm_segment_control_get_segment_icon_at(Evas_Object *obj, unsigned int index)
798 Elm_Segment_Item *seg_rel;
799 Widget_Data *wd = elm_widget_data_get(obj);
802 seg_rel = _item_find(obj, index);
804 if(seg_rel) return seg_rel->icon;
810 * Get the currently selected segment of segmentcontrol
811 * @param obj The SegmentControl object
812 * @param value The current segment id
813 * @return The selected Segment
815 * @ingroup SegmentControl SegmentControl
818 EAPI Elm_Segment_Item *
819 elm_segment_control_selected_segment_get(const Evas_Object *obj, int *value)
821 Widget_Data *wd = elm_widget_data_get(obj);
822 if(!wd || !wd->seg_ctrl) return NULL;
824 Elm_Segment_Item *it;
827 EINA_LIST_FOREACH(wd->seg_ctrl, l, it)
829 if(it->segment_id == wd->cur_seg_id)
831 *value = wd->cur_seg_id;
839 * Get the count of segments of segmentcontrol
840 * @param obj The SegmentControl object
841 * @return The count of Segments
843 * @ingroup SegmentControl SegmentControl
847 elm_segment_control_get_segment_count(Evas_Object *obj)
849 Widget_Data *wd = elm_widget_data_get(obj);
856 * set the size of segmentcontrol
857 * @param obj The SegmentControl object
858 * @param width width of segment control
859 * @param height height of segment control
861 * @ingroup SegmentControl SegmentControl
865 elm_segment_control_set_size(Evas_Object *obj, int width, int height)
867 Widget_Data *wd = elm_widget_data_get(obj);
870 wd->scale_factor = elm_scale_get();
871 if ( wd->scale_factor == 0.0 ) {
872 wd->scale_factor = 1.0;
875 wd->item_width = wd->width = width*wd->scale_factor;
876 wd->height = height*wd->scale_factor;