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;
24 Elm_Segment_Item *ani_it;
27 unsigned int insert_index;
28 unsigned int del_index;
29 unsigned int cur_seg_id;
31 Eina_Bool longpressed : 1;
32 Eina_Bool selected : 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 *evas, Evas_Object *obj, void *event_info);
47 static void _mouse_up(void *data, Evas *e, Evas_Object *obj, void *event_info);
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);
69 if (elm_widget_focus_get(obj))
70 evas_object_focus_set((Evas_Object *)wd->seg_ctrl, 1);
72 evas_object_focus_set((Evas_Object *)wd->seg_ctrl, 0);
76 _mouse_up(void *data, Evas *e, Evas_Object *obj, void *event_info)
78 Elm_Segment_Item *item = (Elm_Segment_Item *) data;
79 Widget_Data *wd = elm_widget_data_get(item->obj);
83 if(wd->selected == EINA_TRUE)
85 wd->selected = EINA_FALSE;
89 if(wd->longpressed == EINA_FALSE)
91 edje_object_signal_emit(item->base, "elm,action,unfocus", "elm");
92 edje_object_signal_emit(item->base, "elm,state,text,visible", "elm");
96 ecore_timer_del(item->long_timer);
97 item->long_timer = NULL;
103 _signal_segment_on(void *data)
105 Elm_Segment_Item *item = (Elm_Segment_Item *) data;
106 Widget_Data *wd = elm_widget_data_get(item->obj);
110 if (item->long_timer)
112 ecore_timer_del(item->long_timer);
113 item->long_timer = NULL;
115 wd->longpressed = EINA_TRUE;
116 edje_object_signal_emit(item->base, "elm,state,segment,on", "elm");
117 edje_object_signal_emit(item->base, "elm,state,text,change", "elm");
119 Elm_Segment_Item *it;
122 if (item->segment_id == wd->cur_seg_id)
124 wd->cur_seg_id = item->segment_id;
125 evas_object_smart_callback_call(item->obj, "changed", (void*)wd->cur_seg_id);
128 EINA_LIST_FOREACH(wd->seg_ctrl, l, it)
130 if (it->segment_id == wd->cur_seg_id)
132 edje_object_signal_emit(it->base, "elm,action,unfocus", "elm");
133 edje_object_signal_emit(it->base, "elm,state,segment,off", "elm");
134 edje_object_signal_emit(it->base, "elm,state,text,visible", "elm");
138 wd->cur_seg_id = item->segment_id;
139 evas_object_smart_callback_call(item->obj, "changed", (void*)wd->cur_seg_id);
144 _mouse_down(void *data, Evas *evas, Evas_Object *obj, void *event_info)
146 Elm_Segment_Item *item = (Elm_Segment_Item *) data;
147 Widget_Data *wd = elm_widget_data_get(item->obj);
150 wd->longpressed = EINA_FALSE;
152 if (item->segment_id == wd->cur_seg_id)
154 wd->selected = EINA_TRUE;
158 edje_object_signal_emit(item->base, "elm,action,focus", "elm");
159 edje_object_signal_emit(item->base, "elm,state,text,visible", "elm");
161 if (item->long_timer) ecore_timer_del(item->long_timer);
162 item->long_timer = ecore_timer_add(0.3, _signal_segment_on, item);
166 _theme_hook(Evas_Object *obj)
168 _elm_theme_object_set(obj, obj, "segmented-control", "base", elm_widget_style_get(obj));
174 _item_free(Evas_Object *obj, Elm_Segment_Item *it)
176 Widget_Data *wd = elm_widget_data_get(obj);
180 wd->seg_ctrl = eina_list_remove(wd->seg_ctrl, it);
182 if(it->icon) evas_object_del(it->icon);
183 if(it->base) evas_object_del(it->base);
184 if(it->label) eina_stringshare_del(it->label);
185 if (it->long_timer) ecore_timer_del(it->long_timer);
194 _del_hook(Evas_Object *obj)
196 Widget_Data *wd = elm_widget_data_get(obj);
197 Elm_Segment_Item *it;
198 Eina_List *l, *clear = NULL;
200 EINA_LIST_FOREACH(wd->seg_ctrl, l, it) clear = eina_list_append(clear, it);
201 EINA_LIST_FREE(clear, it) _item_free(obj, it);
211 _layout(Evas_Object *o, Evas_Object_Box_Data *priv, void *data)
213 Widget_Data *wd = data;
215 _els_box_layout(o, priv, 1, 0); /* making box layout non homogenous */
221 _segment_resizing(void *data)
223 Widget_Data *wd = elm_widget_data_get((Evas_Object *)data);
225 Evas_Coord w = 0, h = 0;
227 evas_object_geometry_get(wd->base, NULL, NULL, &w, &h);
228 wd->item_width = wd->width = w;
230 _state_value_set((Evas_Object *)data);
233 static void _object_resize(void *data, Evas *e, Evas_Object *obj, void *event_info)
237 wd = elm_widget_data_get((Evas_Object *)data);
240 ecore_job_add(_segment_resizing, (Evas_Object *)data);
244 * Add a new segmentcontrol to the parent
245 * @param parent The parent object
246 * @return The new object or NULL if it cannot be created
248 * @ingroup SegmentControl SegmentControl
251 elm_segment_control_add(Evas_Object *parent)
257 wd = ELM_NEW(Widget_Data);
258 e = evas_object_evas_get(parent);
259 obj = elm_widget_add(e);
260 elm_widget_type_set(obj, "segmented-control");
261 elm_widget_sub_object_add(parent, obj);
262 elm_widget_on_focus_hook_set( obj, _on_focus_hook, NULL );
263 elm_widget_data_set(obj, wd);
264 elm_widget_del_hook_set(obj, _del_hook);
265 elm_widget_theme_hook_set(obj, _theme_hook);
267 wd->base = edje_object_add(e);
268 _elm_theme_object_set(obj, wd->base, "segmented-control", "base", "default");
269 elm_widget_resize_object_set(obj, wd->base);
270 wd->box = evas_object_box_add(e);
271 evas_object_box_layout_set(wd->box, _layout, wd, NULL);
272 elm_widget_sub_object_add(obj, wd->box);
273 edje_object_part_swallow(wd->base, "elm.swallow.content", wd->box);
274 evas_object_show(wd->box);
276 evas_object_event_callback_add(obj, EVAS_CALLBACK_RESIZE, _object_resize, obj);
279 wd->insert_index = 0;
281 wd->selected = EINA_FALSE;
286 static Elm_Segment_Item*
287 _item_new(Evas_Object *obj, const char *label, Evas_Object *icon)
289 Widget_Data *wd = elm_widget_data_get(obj);
290 if (!wd) return NULL;
292 Elm_Segment_Item *it;
293 it = calloc(1, sizeof( Elm_Segment_Item));
294 if (!it) return NULL;
298 if(obj) it->obj = obj;
299 it->delete_me = EINA_FALSE;
300 it->segment_id = wd->id;
302 it->base = edje_object_add(evas_object_evas_get(obj));
303 _elm_theme_object_set(obj, it->base, "segment", "base", elm_object_style_get(it->base));
305 if (it->label) eina_stringshare_del(it->label);
308 it->label = eina_stringshare_add(label);
315 if ((it->icon != icon) && (it->icon))
316 elm_widget_sub_object_del(obj, it->icon);
320 elm_widget_sub_object_add(obj, icon);
321 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
322 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
323 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
325 evas_object_size_hint_weight_set(it->base, 1.0, -1.0);
326 evas_object_size_hint_align_set(it->base, 1.0, -1.0);
327 evas_object_size_hint_min_set(it->base, -1, -1);
328 evas_object_size_hint_max_set(it->base, maxw, maxh);
331 edje_object_size_min_restricted_calc(obj, &mw, &mh, 0, 0);
332 evas_object_size_hint_weight_set(obj, 1.0, 1.0);
333 evas_object_size_hint_align_set(obj, -1.0, -1.0);
339 static void _update_list(Evas_Object *obj)
341 Widget_Data *wd = elm_widget_data_get(obj);
344 Elm_Segment_Item *it;
347 wd->count = eina_list_count(wd->seg_ctrl);
351 it = _item_find(obj, 0);
352 _elm_theme_object_set(obj, it->base, "segment", "base", "single"); edje_object_signal_emit(it->base, "elm,state,segment,on", "elm");
353 edje_object_signal_emit(it->base, "elm,state,text,visible", "elm");
354 edje_object_signal_emit(it->base, "elm,state,text,change", "elm");
355 edje_object_message_signal_process(it->base);
357 edje_object_part_text_set(it->base, "elm.text", it->label);
359 edje_object_part_swallow(it->base, "elm.swallow.content", it->icon);
360 edje_object_signal_emit(it->base, "elm,state,icon,visible", "elm");
364 EINA_LIST_FOREACH(wd->seg_ctrl, l, it)
368 _elm_theme_object_set(obj, it->base, "segment", "base", "first");
370 else if(i==wd->count-1)
372 _elm_theme_object_set(obj, it->base, "segment", "base", "last");
376 _elm_theme_object_set(obj, it->base, "segment", "base", "default");
379 edje_object_signal_emit(it->base, "elm,state,text,visible", "elm");
380 edje_object_message_signal_process(it->base);
382 edje_object_part_text_set(it->base, "elm.text", it->label);
384 edje_object_part_swallow(it->base, "elm.swallow.content", it->icon);
385 edje_object_signal_emit(it->base, "elm,state,icon,visible", "elm");
392 static void _refresh_segment_ids(Evas_Object *obj)
394 Widget_Data *wd = elm_widget_data_get(obj);
396 Elm_Segment_Item *it;
398 if (wd->insert_index && wd->cur_seg_id >= wd->insert_index)
401 wd->insert_index = 0;
405 if (wd->cur_seg_id >= wd->del_index)
410 EINA_LIST_FOREACH(wd->seg_ctrl, l, it)
417 static void _state_value_set(Evas_Object *obj)
419 Widget_Data *wd = elm_widget_data_get(obj);
421 Elm_Segment_Item *it;
423 Evas_Coord mw, mh, x, y;
425 unsigned int count = eina_list_count(wd->seg_ctrl);
427 wd->item_width = wd->width/count;
430 evas_object_geometry_get(wd->ani_it->base, &x, &y, &w1, NULL);
431 if (wd->ani_it->delete_me)
433 w1-=(wd->item_width/15);
438 w1+=(wd->item_width/15);
439 if( w1 > wd->item_width )
442 w2 = (wd->width-w1)/(count -1);
449 EINA_LIST_FOREACH(wd->seg_ctrl, l, it)
451 edje_object_size_min_restricted_calc(it->base, &mw, &mh, 0, 0);
452 evas_object_size_hint_weight_set(it->base, 1.0, 1.0);
453 evas_object_size_hint_align_set(it->base, -1.0, -1.0);
455 if(wd->ani_it && it == wd->ani_it)
457 evas_object_resize(it->base, w1, wd->height);
458 evas_object_size_hint_min_set(it->base, w1, wd->height);
459 evas_object_size_hint_max_set(it->base, w1, wd->height);
463 evas_object_resize(it->base, w2, wd->height);
464 evas_object_size_hint_min_set(it->base, w2, wd->height);
465 evas_object_size_hint_max_set(it->base, w2, wd->height);
475 _animator_animate_add_cb(Evas_Object *obj)
477 Widget_Data *wd = elm_widget_data_get(obj);
481 evas_object_geometry_get(wd->ani_it->base, NULL, NULL, &w, NULL);
482 if( w < wd->item_width )
484 _state_value_set(obj);
485 evas_object_geometry_get(wd->ani_it->base, NULL, NULL, &w, NULL);
486 return ECORE_CALLBACK_RENEW;
490 ecore_animator_del(wd->ani);
493 return ECORE_CALLBACK_CANCEL;
499 _animator_animate_del_cb(Evas_Object *obj)
501 Widget_Data *wd = elm_widget_data_get(obj);
504 evas_object_geometry_get(wd->ani_it->base, NULL, NULL, &w, NULL);
507 _state_value_set(obj);
508 evas_object_geometry_get(wd->ani_it->base, NULL, NULL, &w, NULL);
509 return ECORE_CALLBACK_RENEW;
513 _item_free(obj, wd->ani_it );
514 _refresh_segment_ids(obj);
515 ecore_animator_del(wd->ani);
519 wd->id = eina_list_count(wd->seg_ctrl);
520 return ECORE_CALLBACK_CANCEL;
525 * Add a new segment to segmentcontrol
526 * @param obj The SegmentControl object
527 * @param icon The icon object for added segment
528 * @param label The label for added segment
529 * @param animate If 1 the action be animated with sliding effects default 0.
530 * @return The new segment or NULL if it cannot be created
532 * @ingroup SegmentControl SegmentControl
535 EAPI Elm_Segment_Item *
536 elm_segment_control_add_segment(Evas_Object *obj, Evas_Object *icon, const char *label, Eina_Bool animate)
538 Widget_Data *wd = elm_widget_data_get(obj);
541 Elm_Segment_Item *it;
543 it = _item_new(obj, label, icon);
546 wd->seg_ctrl = eina_list_append(wd->seg_ctrl, it);
547 wd->id = eina_list_count(wd->seg_ctrl);
549 evas_object_event_callback_add(it->base, EVAS_CALLBACK_MOUSE_DOWN, _mouse_down, it);
550 evas_object_event_callback_add(it->base, EVAS_CALLBACK_MOUSE_UP, _mouse_up, it);
551 wd->insert_index = 0;
553 _refresh_segment_ids(obj);
555 if(animate && it->segment_id && wd->ani_it == NULL)
557 evas_object_resize(it->base, 1, wd->height);
559 wd->ani = ecore_animator_add( _animator_animate_add_cb, obj );
562 _state_value_set(obj);
563 evas_object_show( it->base);
565 evas_object_box_append(wd->box, it->base);
566 evas_object_smart_calculate(wd->box);
572 static Elm_Segment_Item *
573 _item_find(Evas_Object *obj, unsigned int index)
575 Widget_Data *wd = elm_widget_data_get(obj);
576 if (!wd) return NULL;
578 Elm_Segment_Item *it;
581 EINA_LIST_FOREACH(wd->seg_ctrl, l, it)
592 static Elm_Segment_Item *
593 _item_search(Evas_Object *obj, Elm_Segment_Item *item)
595 Widget_Data *wd = elm_widget_data_get(obj);
599 Elm_Segment_Item *it;
601 EINA_LIST_FOREACH(wd->seg_ctrl, l, it)
611 * Insert a new segment to segmentcontrol
612 * @param obj The SegmentControl object
613 * @param icon The icon object for added segment
614 * @param label The label for added segment
615 * @param index The position at which segment to be inserted
616 * @param animate If 1 the action be animated with sliding effects default 0.
617 * @return The new segment or NULL if it cannot be created
619 * @ingroup SegmentControl SegmentControl
622 elm_segment_control_insert_segment_at(Evas_Object *obj, Evas_Object *icon, const char *label, unsigned int index, Eina_Bool animate)
624 Widget_Data *wd = elm_widget_data_get(obj);
627 Elm_Segment_Item *it, *it_rel;
629 it = _item_new(obj, label, icon);
630 it_rel = _item_find(obj, index);
633 wd->seg_ctrl = eina_list_append(wd->seg_ctrl, it);
638 wd->seg_ctrl = eina_list_prepend_relative(wd->seg_ctrl, it, it_rel);
640 evas_object_event_callback_add(it->base, EVAS_CALLBACK_MOUSE_DOWN, _mouse_down, it);
641 evas_object_event_callback_add(it->base, EVAS_CALLBACK_MOUSE_UP, _mouse_up, it);
642 wd->insert_index = index;
643 wd->id = eina_list_count(wd->seg_ctrl);
644 _refresh_segment_ids(obj);
647 if(animate && it->segment_id && wd->ani_it == NULL)
650 evas_object_resize(it->base, 1, wd->height);
651 wd->ani = ecore_animator_add( _animator_animate_add_cb, obj );
654 _state_value_set(obj);
656 evas_object_show( it->base);
658 if(index >= wd->id-1)
660 evas_object_box_append(wd->box, it->base);
664 evas_object_box_insert_at(wd->box, it->base, index);
667 evas_object_smart_calculate(wd->box);
672 * Delete a segment to segmentcontrol
673 * @param obj The SegmentControl object
674 * @param item The Segment to be deleted
675 * @param animate If 1 the action be animated with sliding effects default 0.
677 * @ingroup SegmentControl SegmentControl
680 elm_segment_control_delete_segment(Evas_Object *obj, Elm_Segment_Item *item, Eina_Bool animate)
682 Widget_Data *wd = elm_widget_data_get(obj);
687 Elm_Segment_Item *it;
688 it = _item_search(obj, item);
691 wd->del_index = it->segment_id;
692 if(animate && it->segment_id && wd->ani_it == NULL)
694 it->delete_me = EINA_TRUE;
696 wd->ani = ecore_animator_add( _animator_animate_del_cb, obj );
700 evas_object_box_remove(wd->box, it->base);
701 evas_object_smart_calculate(wd->box);
703 _refresh_segment_ids(obj);
704 _state_value_set(obj);
707 wd->id = eina_list_count(wd->seg_ctrl);
712 * Delete a segment to segmentcontrol
713 * @param obj The SegmentControl object
714 * @param index The position at which segment to be deleted
715 * @param animate If 1 the action be animated with sliding effects default 0.
717 * @ingroup SegmentControl SegmentControl
721 elm_segment_control_delete_segment_at(Evas_Object *obj, unsigned int index, Eina_Bool animate)
723 Widget_Data *wd = elm_widget_data_get(obj);
725 Elm_Segment_Item *it;
727 it = _item_find(obj, index);
731 wd->del_index = index;
732 if(animate && it->segment_id)
734 if(wd->ani_it == NULL)
737 it->delete_me = EINA_TRUE;
738 wd->ani = ecore_animator_add( _animator_animate_del_cb, obj );
743 evas_object_box_remove(wd->box, it->base);
744 evas_object_smart_calculate(wd->box);
746 _refresh_segment_ids(obj);
747 _state_value_set(obj);
750 wd->id = eina_list_count(wd->seg_ctrl);
755 * Get the label of a segment of segmentcontrol
756 * @param obj The SegmentControl object
757 * @param index The index of the segment
760 * @ingroup SegmentControl SegmentControl
764 elm_segment_control_get_segment_label_at(Evas_Object *obj, unsigned int index)
766 Elm_Segment_Item *it_rel;
767 Widget_Data *wd = elm_widget_data_get(obj);
770 it_rel = _item_find(obj, index);
772 if(it_rel) return it_rel->label;
778 * Get the icon of a segment of segmentcontrol
779 * @param obj The SegmentControl object
780 * @param index The index of the segment
781 * @return The icon object
783 * @ingroup SegmentControl SegmentControl
787 elm_segment_control_get_segment_icon_at(Evas_Object *obj, unsigned int index)
789 Elm_Segment_Item *seg_rel;
790 Widget_Data *wd = elm_widget_data_get(obj);
793 seg_rel = _item_find(obj, index);
795 if(seg_rel) return seg_rel->icon;
801 * Get the currently selected segment of segmentcontrol
802 * @param obj The SegmentControl object
803 * @param value The current segment id
804 * @return The selected Segment
806 * @ingroup SegmentControl SegmentControl
809 EAPI Elm_Segment_Item *
810 elm_segment_control_selected_segment_get(const Evas_Object *obj, int *value)
812 Widget_Data *wd = elm_widget_data_get(obj);
813 if(!wd || !wd->seg_ctrl) return NULL;
815 Elm_Segment_Item *it;
818 EINA_LIST_FOREACH(wd->seg_ctrl, l, it)
820 if(it->segment_id == wd->cur_seg_id)
822 *value = wd->cur_seg_id;
830 * Get the count of segments of segmentcontrol
831 * @param obj The SegmentControl object
832 * @return The count of Segments
834 * @ingroup SegmentControl SegmentControl
838 elm_segment_control_get_segment_count(Evas_Object *obj)
840 Widget_Data *wd = elm_widget_data_get(obj);