add todo for elc_multibuttonentry.
[framework/uifw/elementary.git] / src / lib / elc_multibuttonentry.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 #define MAX_STR 256
5 #define MIN_W_ENTRY 10
6
7 typedef enum _Multibuttonentry_Pos
8   {
9      MULTIBUTTONENTRY_POS_START,
10      MULTIBUTTONENTRY_POS_END,
11      MULTIBUTTONENTRY_POS_BEFORE,
12      MULTIBUTTONENTRY_POS_AFTER,
13   } Multibuttonentry_Pos;
14
15 typedef enum _Multibuttonentry_Button_State
16   {
17      MULTIBUTTONENTRY_BUTTON_STATE_DEFAULT,
18      MULTIBUTTONENTRY_BUTTON_STATE_SELECTED,
19   } Multibuttonentry_Button_State;
20
21
22 typedef enum _MultiButtonEntry_Closed_Button_Type
23   {
24      MULTIBUTTONENTRY_CLOSED_IMAGE,
25      MULTIBUTTONENTRY_CLOSED_LABEL
26   } MultiButtonEntry_Closed_Button_Type;
27
28 typedef enum _Multibuttonentry_View_State
29   {
30      MULTIBUTTONENTRY_VIEW_NONE,
31      MULTIBUTTONENTRY_VIEW_GUIDETEXT,
32      MULTIBUTTONENTRY_VIEW_ENTRY,
33      MULTIBUTTONENTRY_VIEW_SHRINK
34   } Multibuttonentry_View_State;
35
36 typedef struct _Widget_Data Widget_Data;
37 typedef struct _Multibuttonentry_Item Elm_Multibuttonentry_Item;
38
39 struct _Multibuttonentry_Item
40   {
41      ELM_WIDGET_ITEM;
42      Evas_Object *button;
43      Evas_Coord vw, rw; // vw: visual width, real width
44      Eina_Bool  visible: 1;
45      Evas_Smart_Cb func;
46   };
47
48 typedef struct _Elm_Multibuttonentry_Item_Filter
49   {
50      Elm_Multibuttonentry_Item_Filter_Cb callback_func;
51      void *data;
52   } Elm_Multibuttonentry_Item_Filter;
53
54 struct _Widget_Data
55   {
56      Evas_Object *base;
57      Evas_Object *box;
58      Evas_Object *entry;
59      Evas_Object *label;
60      Evas_Object *guidetext;
61      Evas_Object *end;   // used to represent the total number of invisible buttons
62
63      Evas_Object *rect_for_end;
64      MultiButtonEntry_Closed_Button_Type end_type;
65
66      Eina_List *items;
67      Eina_List *current;
68      Eina_List *filter_list;
69
70      const char *labeltxt, *guidetexttxt;
71
72      int n_str;
73      Multibuttonentry_View_State view_state;
74
75      Evas_Coord w_box, h_box;
76      int  shrink;
77      Eina_Bool focused: 1;
78      Eina_Bool last_btn_select: 1;
79      Elm_Multibuttonentry_Item_Filter_Cb add_callback;
80      void *add_callback_data;
81   };
82
83 static const char *widtype = NULL;
84 static void _del_hook(Evas_Object *obj);
85 static void _theme_hook(Evas_Object *obj);
86 static void _on_focus_hook(void *data __UNUSED__, Evas_Object *obj);
87 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src, Evas_Callback_Type type, void *event_info);
88 static void _sizing_eval(Evas_Object *obj);
89 static void _changed_size_hint_cb(void *data, Evas *evas, Evas_Object *obj, void *event);
90 static void _resize_cb(void *data, Evas *evas, Evas_Object *obj, void *event);
91 static void _event_init(Evas_Object *obj);
92 static void _shrink_mode_set(Evas_Object *obj, Eina_Bool shrink);
93 static void _view_update(Evas_Object *obj);
94 static void _set_label(Evas_Object *obj, const char *str);
95 static void _change_current_button_state(Evas_Object *obj, Multibuttonentry_Button_State state);
96 static void _change_current_button(Evas_Object *obj, Evas_Object *btn);
97 static void _button_clicked(void *data, Evas_Object *obj, const char *emission, const char *source);
98 static void _del_button_obj(Evas_Object *obj, Evas_Object *btn);
99 static void _del_button_item(Elm_Multibuttonentry_Item *item);
100 static void _select_button(Evas_Object *obj, Evas_Object *btn);
101 static Elm_Object_Item *_add_button_item(Evas_Object *obj, const char *str, Multibuttonentry_Pos pos, const void *ref, Evas_Smart_Cb func, void *data);
102 static void _evas_mbe_key_up_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
103 static void _entry_changed_cb(void *data, Evas_Object *obj, void *event_info);
104 static void _entry_key_up_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
105 static void _entry_key_down_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
106 static void _entry_resized_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info);
107 static void _entry_focus_in_cb(void *data, Evas_Object *obj, void *event_info);
108 static void _entry_focus_out_cb(void *data, Evas_Object *obj, void *event_info);
109 static void _entry_clicked_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__);
110 static void _view_init(Evas_Object *obj);
111 static void _set_vis_guidetext(Evas_Object *obj);
112 static void _calculate_box_min_size(Evas_Object *box, Evas_Object_Box_Data *priv);
113 static Evas_Coord _calculate_item_max_height(Evas_Object *box, Evas_Object_Box_Data *priv, int obj_index);
114 static void _box_layout_cb(Evas_Object *o, Evas_Object_Box_Data *priv, void *data);
115 static void _item_text_set_hook(Elm_Object_Item *it,
116                                 const char *part,
117                                 const char *label);
118 static const char *_item_text_get_hook(const Elm_Object_Item *it,
119                                        const char *part);
120
121  /*  TODO
122   *  Change Shrink to Expand.
123   *  Code refactoring
124   *  use evas_object_smart_callback_descriptions_set for smart callbacks.
125  */
126
127 static void
128 _del_hook(Evas_Object *obj)
129 {
130    Widget_Data *wd = elm_widget_data_get(obj);
131
132    if (!wd) return;
133    if (wd->items)
134      {
135         Elm_Multibuttonentry_Item *item;
136         EINA_LIST_FREE(wd->items, item)
137           {
138              _del_button_obj(obj, item->button);
139              free(item);
140           }
141         wd->items = NULL;
142      }
143    wd->current = NULL;
144
145    if (wd->labeltxt) eina_stringshare_del(wd->labeltxt);
146    if (wd->guidetexttxt) eina_stringshare_del(wd->guidetexttxt);
147    if (wd->entry) evas_object_del(wd->entry);
148    if (wd->label) evas_object_del(wd->label);
149    if (wd->guidetext) evas_object_del(wd->guidetext);
150    if (wd->end) evas_object_del(wd->end);
151    if (wd->rect_for_end) evas_object_del(wd->rect_for_end);
152 }
153
154 static void
155 _theme_hook(Evas_Object *obj)
156 {
157    Widget_Data *wd = elm_widget_data_get(obj);
158    Eina_List *l;
159    Elm_Multibuttonentry_Item *item;
160
161    if (!wd) return;
162
163    _elm_theme_object_set(obj, wd->base, "multibuttonentry", "base", elm_widget_style_get(obj));
164    if (wd->box) edje_object_part_swallow(wd->base, "box.swallow", wd->box);
165    edje_object_scale_set(wd->base, elm_widget_scale_get(obj) * _elm_config->scale);
166
167    EINA_LIST_FOREACH(wd->items, l, item)
168      {
169         if (item->button)
170           _elm_theme_object_set(obj, item->button, "multibuttonentry", "btn", elm_widget_style_get(obj));
171         edje_object_scale_set(item->button, elm_widget_scale_get(obj) * _elm_config->scale);
172      }
173
174    _sizing_eval(obj);
175 }
176
177 static void
178 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
179 {
180    Widget_Data *wd = elm_widget_data_get(obj);
181
182    if (!wd) return;
183
184    if (elm_widget_focus_get(obj))
185      {
186         if ((wd->current))
187           {
188              elm_entry_input_panel_show(wd->entry);
189           }
190         else if (((!wd->current) || (!eina_list_count(wd->items))))
191           {
192              if (wd->entry) elm_entry_cursor_end_set(wd->entry);
193              _view_update(obj);
194              elm_entry_input_panel_show(wd->entry);
195           }
196         wd->focused = EINA_TRUE;
197         evas_object_smart_callback_call(obj, "focused", NULL);
198      }
199    else
200      {
201         wd->focused = EINA_FALSE;
202         _view_update(obj);
203
204         elm_entry_input_panel_hide(wd->entry);
205         evas_object_smart_callback_call(obj, "unfocused", NULL);
206      }
207 }
208
209 static Eina_Bool
210 _event_hook(Evas_Object *obj __UNUSED__, Evas_Object *src __UNUSED__, Evas_Callback_Type type __UNUSED__, void *event_info __UNUSED__)
211 {
212    return EINA_TRUE;
213 }
214
215 static void
216 _signal_emit_hook(Evas_Object *obj, const char *emission, const char *source)
217 {
218    Widget_Data *wd = elm_widget_data_get(obj);
219
220    if (!wd) return;
221    edje_object_signal_emit(wd->base, emission, source);
222 }
223
224 static void
225 _sizing_eval(Evas_Object *obj)
226 {
227    Widget_Data *wd = elm_widget_data_get(obj);
228    Evas_Coord minw = -1, minh = -1;
229    Evas_Coord left, right, top, bottom;
230
231    if (!wd) return;
232    evas_object_size_hint_min_get(wd->box, &minw, &minh);
233    edje_object_part_geometry_get(wd->base, "top.left.pad", NULL, NULL, &left, &top);
234    edje_object_part_geometry_get(wd->base, "bottom.right.pad", NULL, NULL, &right, &bottom);
235
236    minw += (left + right);
237    minh += (top + bottom);
238
239    evas_object_size_hint_min_set(obj, minw, minh);
240 }
241
242 static void
243 _signal_mouse_clicked(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
244 {
245    Widget_Data *wd = elm_widget_data_get(data);
246
247    if (!wd || !wd->base) return;
248    wd->focused = EINA_TRUE;
249    _view_update(data);
250
251    elm_entry_input_panel_show(wd->entry);
252
253    evas_object_smart_callback_call(data, "clicked", NULL);
254 }
255
256 static void
257 _changed_size_hint_cb(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event __UNUSED__)
258 {
259    Evas_Object *eo = (Evas_Object *)data;
260    Widget_Data *wd = elm_widget_data_get(data);
261
262    if (!wd) return;
263    _sizing_eval(eo);
264 }
265
266 static void
267 _resize_cb(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event __UNUSED__)
268 {
269    Widget_Data *wd = elm_widget_data_get(data);
270    Evas_Coord w, h;
271
272    if (!wd) return;
273    evas_object_geometry_get(wd->box, NULL, NULL, &w, &h);
274
275    if (wd->h_box < h) evas_object_smart_callback_call(data, "expanded", NULL);
276    else if (wd->h_box > h) evas_object_smart_callback_call(data, "contracted", NULL);
277
278    wd->w_box = w;
279    wd->h_box = h;
280
281    _view_update(data);
282 }
283
284 static void
285 _event_init(Evas_Object *obj)
286 {
287    Widget_Data *wd = elm_widget_data_get(obj);
288
289    if (!wd || !wd->base) return;
290    if (wd->base)
291      {
292         edje_object_signal_callback_add(wd->base, "mouse,clicked,1", "*", _signal_mouse_clicked, obj);
293         evas_object_event_callback_add(wd->base, EVAS_CALLBACK_KEY_UP, _evas_mbe_key_up_cb, obj);
294      }
295
296    if (wd->box)
297      {
298         evas_object_event_callback_add(wd->box, EVAS_CALLBACK_RESIZE, _resize_cb, obj);
299         evas_object_event_callback_add(wd->box, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hint_cb, obj);
300      }
301
302    if (wd->entry)
303      {
304         evas_object_event_callback_add(wd->entry, EVAS_CALLBACK_KEY_UP, _entry_key_up_cb, obj);
305         evas_object_event_callback_add(wd->entry, EVAS_CALLBACK_KEY_DOWN, _entry_key_down_cb, obj);
306         evas_object_event_callback_add(wd->entry, EVAS_CALLBACK_RESIZE, _entry_resized_cb, obj);
307         evas_object_smart_callback_add(wd->entry, "changed", _entry_changed_cb, obj);
308         evas_object_smart_callback_add(wd->entry, "focused", _entry_focus_in_cb, obj);
309         evas_object_smart_callback_add(wd->entry, "unfocused", _entry_focus_out_cb, obj);
310         evas_object_smart_callback_add(wd->entry, "clicked", _entry_clicked_cb, obj);
311      }
312 }
313
314 static void
315 _set_vis_guidetext(Evas_Object *obj)
316 {
317    Widget_Data *wd = elm_widget_data_get(obj);
318
319    if (!wd) return;
320    elm_box_unpack(wd->box, wd->guidetext);
321    elm_box_unpack(wd->box, wd->entry);
322    if (wd->view_state == MULTIBUTTONENTRY_VIEW_SHRINK) return;
323
324    if (wd && (!eina_list_count(wd->items)) && wd->guidetext
325        && (!elm_widget_focus_get(obj)) && (!wd->focused) && (!wd->n_str))
326      {
327         evas_object_hide(wd->entry);
328         elm_box_pack_end(wd->box, wd->guidetext);
329         evas_object_show(wd->guidetext);
330         wd->view_state = MULTIBUTTONENTRY_VIEW_GUIDETEXT;
331      }
332    else
333      {
334         evas_object_hide(wd->guidetext);
335         elm_box_pack_end(wd->box, wd->entry);
336         evas_object_show(wd->entry);
337         if (elm_widget_focus_get(obj) || wd->focused)
338           if (!wd->current)
339             elm_object_focus_set(wd->entry, EINA_TRUE);
340         wd->view_state = MULTIBUTTONENTRY_VIEW_ENTRY;
341      }
342 }
343
344 static void
345 _shrink_mode_set(Evas_Object *obj, Eina_Bool shrink)
346 {
347    Widget_Data *wd = elm_widget_data_get(obj);
348    Eina_List *l;
349    Elm_Multibuttonentry_Item *item;
350
351    if (!wd || !wd->box) return;
352    if (wd->view_state == MULTIBUTTONENTRY_VIEW_ENTRY)
353      evas_object_hide(wd->entry);
354    else if (wd->view_state == MULTIBUTTONENTRY_VIEW_GUIDETEXT)
355      evas_object_hide(wd->guidetext);
356    else if (wd->view_state == MULTIBUTTONENTRY_VIEW_SHRINK)
357      {
358         evas_object_hide(wd->rect_for_end);
359         evas_object_hide(wd->end);
360         wd->view_state = MULTIBUTTONENTRY_VIEW_NONE;
361      }
362
363    if (shrink == EINA_TRUE)
364      {
365         Evas_Coord w = 0, w_tmp = 0;
366         Evas_Coord box_inner_item_width_padding = 0;
367
368         elm_box_padding_get(wd->box, &box_inner_item_width_padding, NULL);
369         // unpack all items and entry
370         elm_box_unpack_all(wd->box);
371         EINA_LIST_FOREACH(wd->items, l, item)
372           {
373              if (item)
374                {
375                   evas_object_hide(item->button);
376                   item->visible = EINA_FALSE;
377                }
378           }
379         // pack buttons only 1line
380         w = wd->w_box;
381
382         if (wd->label)
383           {
384              elm_box_pack_end(wd->box, wd->label);
385              evas_object_size_hint_min_get(wd->label, &w_tmp, NULL);
386              w -= w_tmp;
387              w -= box_inner_item_width_padding;
388           }
389
390         item = NULL;
391         int count = eina_list_count(wd->items);
392         Evas_Coord button_min_width = 0;
393         /* Evas_Coord button_min_height = 0; */
394         if (wd->end_type == MULTIBUTTONENTRY_CLOSED_IMAGE)
395           {
396              const char *size_str;
397              size_str = edje_object_data_get(wd->end, "closed_button_width");
398              if (size_str) button_min_width = (Evas_Coord)atoi(size_str);
399              /* it use for later
400              size_str = edje_object_data_get(wd->end, "closed_button_height");
401              if (size_str) button_min_width = (Evas_Coord)atoi(size_str);
402               */
403           }
404
405         EINA_LIST_FOREACH(wd->items, l, item)
406           {
407              if (item)
408                {
409                   int w_label_count = 0;
410                   char buf[MAX_STR];
411
412                   elm_box_pack_end(wd->box, item->button);
413                   evas_object_show(item->button);
414                   item->visible = EINA_TRUE;
415
416                   w -= item->vw;
417                   w -= box_inner_item_width_padding;
418                   count--;
419
420                   if (wd->end_type == MULTIBUTTONENTRY_CLOSED_LABEL)
421                     {
422                        if (count > 0)
423                          {
424                             snprintf(buf, sizeof(buf), "... + %d", count);
425                             elm_object_text_set(wd->end, buf);
426                             evas_object_size_hint_min_get(wd->end, &w_label_count, NULL);
427                          }
428
429                        if (w < 0 || w < w_label_count)
430                          {
431                             elm_box_unpack(wd->box, item->button);
432                             evas_object_hide(item->button);
433                             item->visible = EINA_FALSE;
434
435                             count++;
436                             snprintf(buf, sizeof(buf), "... + %d", count);
437                             elm_object_text_set(wd->end, buf);
438                             evas_object_size_hint_min_get(wd->end, &w_label_count, NULL);
439
440                             elm_box_pack_end(wd->box, wd->end);
441                             evas_object_show(wd->end);
442
443                             wd->view_state = MULTIBUTTONENTRY_VIEW_SHRINK;
444                             evas_object_smart_callback_call(obj, "shrink,state,changed", (void *)1);
445                             break;
446                          }
447                     }
448                   else if (wd->end_type == MULTIBUTTONENTRY_CLOSED_IMAGE)
449                     {
450                        if (w < button_min_width)
451                          {
452                             Evas_Coord rectSize;
453                             Evas_Coord closed_height = 0;
454                             const char *height_str = edje_object_data_get(wd->base, "closed_height");
455
456                             if (height_str) closed_height = (Evas_Coord)atoi(height_str);
457                             elm_box_unpack(wd->box, item->button);
458                             evas_object_hide(item->button);
459                             item->visible = EINA_FALSE;
460
461                             w += item->vw;
462                             rectSize = w - button_min_width;
463                             if (!wd->rect_for_end)
464                               {
465                                  Evas *e = evas_object_evas_get(obj);
466                                  wd->rect_for_end = evas_object_rectangle_add(e);
467                                  evas_object_color_set(wd->rect_for_end, 0, 0, 0, 0);
468                               }
469                             evas_object_size_hint_min_set(wd->rect_for_end, rectSize, closed_height * _elm_config->scale);
470                             elm_box_pack_end(wd->box, wd->rect_for_end);
471                             evas_object_show(wd->rect_for_end);
472
473                             elm_box_pack_end(wd->box, wd->end);
474                             evas_object_show(wd->end);
475
476                             wd->view_state = MULTIBUTTONENTRY_VIEW_SHRINK;
477                             evas_object_smart_callback_call(obj, "shrink,state,changed", (void *)0);
478                             break;
479                          }
480                     }
481                }
482           }
483      }
484    else
485      {
486         // unpack all items and entry
487         elm_box_unpack_all(wd->box);
488         EINA_LIST_FOREACH(wd->items, l, item)
489           {
490              if (item)
491                {
492                   evas_object_hide(item->button);
493                   item->visible = EINA_FALSE;
494                }
495           }
496         evas_object_hide(wd->end);
497
498         if (wd->rect_for_end) evas_object_hide(wd->rect_for_end);
499
500         // pack buttons only 1line
501
502         if (wd->label) elm_box_pack_end(wd->box, wd->label);
503
504         // pack remain btns
505         item = NULL;
506         EINA_LIST_FOREACH(wd->items, l, item)
507           {
508              if (item)
509                {
510                   elm_box_pack_end(wd->box, item->button);
511                   evas_object_show(item->button);
512                   item->visible = EINA_TRUE;
513                }
514           }
515
516         wd->view_state = MULTIBUTTONENTRY_VIEW_NONE;
517         evas_object_smart_callback_call(obj, "shrink,state,changed", (void *)(long)wd->shrink);
518      }
519    if (wd->view_state != MULTIBUTTONENTRY_VIEW_SHRINK)
520      {
521         _set_vis_guidetext(obj);
522      }
523 }
524
525 static void
526 _view_update(Evas_Object *obj)
527 {
528    Evas_Coord width = 1, height = 1;
529    Widget_Data *wd = elm_widget_data_get(obj);
530
531    if (!wd || !wd->box || !wd->entry || !(wd->w_box > 0)) return;
532
533    // update label
534    if (wd->label)
535      {
536         elm_box_unpack(wd->box, wd->label);
537         elm_box_pack_start(wd->box, wd->label);
538         evas_object_size_hint_min_get(wd->label, &width, &height);
539      }
540
541    if (wd->guidetext)
542      {
543         Evas_Coord guide_text_width = wd->w_box - width;
544         evas_object_size_hint_min_set(wd->guidetext, guide_text_width, height);
545      }
546
547    // update buttons in shrink mode
548    if (wd->view_state == MULTIBUTTONENTRY_VIEW_SHRINK)
549      _shrink_mode_set(obj, EINA_TRUE);
550
551    // update guidetext
552    _set_vis_guidetext(obj);
553 }
554
555 static void
556 _set_label(Evas_Object *obj, const char *str)
557 {
558    Widget_Data *wd = elm_widget_data_get(obj);
559
560    if (!wd || !str) return;
561    eina_stringshare_replace(&wd->labeltxt, str);
562    if (wd->label)
563    {
564       Evas_Coord width, height, sum_width = 0;
565       evas_object_size_hint_min_set(wd->label, 0, 0);
566       evas_object_resize(wd->label, 0, 0);
567       edje_object_part_text_set(wd->label, "mbe.label", str);
568
569       if (!strcmp(str, ""))
570         {
571            /* FIXME: not work yet */
572            edje_object_signal_emit(wd->label, "elm,mbe,clear_text", "");
573            edje_object_part_geometry_get(wd->label, "mbe.label", NULL, NULL, &width, &height);
574            sum_width += width;
575         }
576       else
577         {
578            edje_object_signal_emit(wd->label, "elm,mbe,set_text", "");
579            edje_object_part_geometry_get(wd->label, "mbe.label", NULL, NULL, &width, &height);
580
581            sum_width += width;
582
583            edje_object_part_geometry_get(wd->label, "mbe.label.left.padding", NULL, NULL, &width, NULL);
584            sum_width += width;
585
586            edje_object_part_geometry_get(wd->label, "mbe.label.right.padding", NULL, NULL, &width, NULL);
587            sum_width += width;
588         }
589       evas_object_size_hint_min_set(wd->label, sum_width, height);
590    }
591    evas_object_show(wd->label);
592    _view_update(obj);
593 }
594
595 static void
596 _set_guidetext(Evas_Object *obj, const char *str)
597 {
598    Widget_Data *wd = elm_widget_data_get(obj);
599
600    if (!wd || !str) return;
601
602    eina_stringshare_replace(&wd->guidetexttxt, str);
603    if (wd->guidetext == NULL)
604      wd->guidetext = edje_object_add(evas_object_evas_get(obj));
605
606    if (wd->guidetext)
607      {
608         _elm_theme_object_set(obj, wd->guidetext, "multibuttonentry",
609                        "guidetext", elm_widget_style_get(obj));
610         evas_object_size_hint_weight_set(wd->guidetext, 0.0, EVAS_HINT_EXPAND);
611         evas_object_size_hint_align_set(wd->guidetext, EVAS_HINT_FILL,
612                                                            EVAS_HINT_FILL);
613         edje_object_part_text_set(wd->guidetext, "elm.text", str);
614         _view_update(obj);
615      }
616 }
617
618 static void
619 _change_current_button_state(Evas_Object *obj, Multibuttonentry_Button_State state)
620 {
621    Widget_Data *wd = elm_widget_data_get(obj);
622    Elm_Multibuttonentry_Item *item = NULL;
623
624    if (!wd) return;
625    if (wd->current)
626      item = eina_list_data_get(wd->current);
627
628    if (item && item->button)
629      {
630         switch (state)
631           {
632              case MULTIBUTTONENTRY_BUTTON_STATE_DEFAULT:
633                 edje_object_signal_emit(item->button, "default", "");
634                 wd->current = NULL;
635                 break;
636              case MULTIBUTTONENTRY_BUTTON_STATE_SELECTED:
637                 edje_object_signal_emit(item->button, "focused", "");
638                 evas_object_smart_callback_call(obj, "item,selected", item);
639                 break;
640              default:
641                 edje_object_signal_emit(item->button, "default", "");
642                 wd->current = NULL;
643                 break;
644           }
645      }
646 }
647
648 static void
649 _change_current_button(Evas_Object *obj, Evas_Object *btn)
650 {
651    Widget_Data *wd = elm_widget_data_get(obj);
652    Eina_List *l;
653    Elm_Multibuttonentry_Item *item;
654
655    if (!wd) return;
656
657    // change the state of previous button to "default"
658    _change_current_button_state(obj, MULTIBUTTONENTRY_BUTTON_STATE_DEFAULT);
659
660    // change the current
661    EINA_LIST_FOREACH(wd->items, l, item)
662      {
663         if (item->button == btn)
664           {
665              wd->current = l;
666              break;
667           }
668      }
669    // change the state of current button to "focused"
670    _change_current_button_state(obj, MULTIBUTTONENTRY_BUTTON_STATE_SELECTED);
671 }
672
673 static void
674 _button_clicked(void *data, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__)
675 {
676    Widget_Data *wd = elm_widget_data_get(data);
677
678    Elm_Multibuttonentry_Item *item = NULL;
679    if (!wd || wd->view_state == MULTIBUTTONENTRY_VIEW_SHRINK) return;
680
681    _change_current_button(data, obj);
682
683    if (wd->current)
684      if ((item = eina_list_data_get(wd->current)) != NULL)
685        {
686           evas_object_smart_callback_call(data, "item,clicked", item);
687           _select_button(data, item->button);
688        }
689 }
690
691 static void
692 _del_button_obj(Evas_Object *obj, Evas_Object *btn)
693 {
694    Widget_Data *wd = elm_widget_data_get(obj);
695
696    if (!wd || !btn) return;
697    if (btn)
698      evas_object_del(btn);
699 }
700
701 static void
702 _del_button_item(Elm_Multibuttonentry_Item *item)
703 {
704    Eina_List *l;
705    Elm_Multibuttonentry_Item *_item;
706    if (!item) return;
707    Widget_Data *wd;
708
709    Evas_Object *obj = WIDGET(item);
710    wd = elm_widget_data_get(obj);
711    if (!wd) return;
712    EINA_LIST_FOREACH(wd->items, l, _item)
713      {
714         if (_item == item)
715           {
716              wd->items = eina_list_remove(wd->items, _item);
717              elm_box_unpack(wd->box, _item->button);
718
719              evas_object_smart_callback_call(obj, "item,deleted", _item);
720
721              _del_button_obj(obj, _item->button);
722
723              if (wd->current == l)
724                wd->current = NULL;
725              break;
726           }
727      }
728    if (wd->view_state == MULTIBUTTONENTRY_VIEW_SHRINK)
729      _shrink_mode_set(obj, EINA_TRUE);
730
731    if (!eina_list_count(wd->items))
732      _set_vis_guidetext(obj);
733 }
734
735 static void
736 _select_button(Evas_Object *obj, Evas_Object *btn)
737 {
738    Widget_Data *wd = elm_widget_data_get(obj);
739
740    if (!wd) return;
741    if (btn)
742      {
743         _change_current_button(obj, btn);
744         if (elm_widget_focus_get(obj))
745           {
746              elm_object_focus_set(wd->entry, EINA_FALSE);
747              evas_object_focus_set(btn, EINA_TRUE);
748           }
749      }
750    else
751      {
752         _change_current_button_state(obj, MULTIBUTTONENTRY_BUTTON_STATE_DEFAULT);
753         if (elm_widget_focus_get(obj))
754           elm_object_focus_set(wd->entry, EINA_TRUE);
755      }
756 }
757
758 static void
759 _resize_button(Evas_Object *btn, Evas_Coord *realw, Evas_Coord *vieww)
760 {
761    Evas_Coord rw, vw;
762    Evas_Coord w_text, h_btn, padding_outer, padding_inner = 0;
763    Evas_Coord w_btn = 0, button_max_width = 0;
764    const char *size_str;
765
766    size_str = edje_object_data_get(btn, "button_max_size");
767    if (size_str) button_max_width = (Evas_Coord)atoi(size_str);
768
769    // decide the size of button
770    edje_object_part_geometry_get(btn, "elm.base", NULL, NULL, NULL, &h_btn);
771    edje_object_part_geometry_get(btn, "elm.btn.text", NULL, NULL, &w_text, NULL);
772    edje_object_part_geometry_get(btn, "right.padding", NULL, NULL, &padding_outer, NULL);
773    w_btn = w_text + 2*padding_outer + 2*padding_inner;
774
775    rw = w_btn;
776
777    if (button_max_width < w_btn)
778      vw = button_max_width;
779    else
780      vw = w_btn;
781
782    //resize btn
783    evas_object_resize(btn, vw, h_btn);
784    evas_object_size_hint_min_set(btn, vw, h_btn);
785
786    if (realw) *realw = rw;
787    if (vieww) *vieww = vw;
788 }
789
790 static Eina_Bool
791 _item_del_pre_hook(Elm_Object_Item *it)
792 {
793    _del_button_item((Elm_Multibuttonentry_Item *)it);
794    return EINA_TRUE;
795 }
796
797 static Elm_Object_Item*
798 _add_button_item(Evas_Object *obj, const char *str, Multibuttonentry_Pos pos, const void *ref, Evas_Smart_Cb func, void *data)
799 {
800    Elm_Multibuttonentry_Item *item;
801    Elm_Multibuttonentry_Item_Filter *item_filter;
802    Elm_Multibuttonentry_Item *reference = (Elm_Multibuttonentry_Item *)ref;
803    Eina_List *l;
804    Evas_Object *btn;
805    Evas_Coord width = -1, height = -1;
806    char *str_utf8 = NULL;
807    Widget_Data *wd = elm_widget_data_get(obj);
808
809    if (!wd || !wd->box || !wd->entry) return NULL;
810    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
811    if (!str) return NULL;
812
813    EINA_LIST_FOREACH(wd->filter_list, l, item_filter)
814      {
815         if (!(item_filter->callback_func(obj, str, data, item_filter->data)))
816           return NULL;
817      }
818    // add button
819    btn = edje_object_add(evas_object_evas_get(obj));
820    str_utf8 = elm_entry_markup_to_utf8(str);
821
822    //entry is cleared when text is made to button
823    elm_object_text_set(wd->entry, "");
824
825    _elm_theme_object_set(obj, btn, "multibuttonentry", "btn", elm_widget_style_get(obj));
826    edje_object_part_text_set(btn, "elm.btn.text", str_utf8);
827    edje_object_part_geometry_get(btn, "elm.btn.text", NULL, NULL, &width, &height);
828
829    evas_object_size_hint_min_set(btn, width, height);
830
831    edje_object_signal_callback_add(btn, "mouse,clicked,1", "*", _button_clicked, obj);
832    evas_object_size_hint_weight_set(btn, 0.0, 0.0);
833    evas_object_show(btn);
834
835    // append item list
836    item = elm_widget_item_new(obj, Elm_Multibuttonentry_Item);
837    if (item)
838      {
839         elm_widget_item_del_pre_hook_set(item, _item_del_pre_hook);
840         elm_widget_item_text_set_hook_set(item, _item_text_set_hook);
841         elm_widget_item_text_get_hook_set(item, _item_text_get_hook);
842         elm_widget_item_data_set(item, data);
843         Evas_Coord rw, vw;
844         _resize_button(btn, &rw, &vw);
845         item->button = btn;
846         item->rw = rw;
847         item->vw = vw;
848         item->visible = EINA_TRUE;
849
850         if (func)
851           {
852               item->func = func;
853           }
854
855         switch (pos)
856           {
857              case MULTIBUTTONENTRY_POS_START:
858                 wd->items = eina_list_prepend(wd->items, item);
859                 if (wd->view_state == MULTIBUTTONENTRY_VIEW_SHRINK)
860                   {
861                      elm_widget_sub_object_add(obj, btn);
862                      _shrink_mode_set(obj, EINA_TRUE);
863                   }
864                 else
865                   {
866                      if (wd->label)
867                        elm_box_pack_after(wd->box, btn, wd->label);
868                      else
869                        elm_box_pack_start(wd->box, btn);
870                      if (wd->view_state == MULTIBUTTONENTRY_VIEW_GUIDETEXT)
871                        _set_vis_guidetext(obj);
872                   }
873                 break;
874              case MULTIBUTTONENTRY_POS_END:
875                 wd->items = eina_list_append(wd->items, item);
876                 if (wd->view_state == MULTIBUTTONENTRY_VIEW_SHRINK)
877                   {
878                      elm_widget_sub_object_add(obj, btn);
879                      evas_object_hide(btn);
880                   }
881                 else
882                   {
883                      if (wd->view_state == MULTIBUTTONENTRY_VIEW_GUIDETEXT)
884                        _set_vis_guidetext(obj);
885                      if (wd->entry)
886                        elm_box_pack_before(wd->box, btn, wd->entry);
887                      else
888                        elm_box_pack_end(wd->box, btn);
889                   }
890                 break;
891              case MULTIBUTTONENTRY_POS_BEFORE:
892                 if (reference)
893                      wd->items = eina_list_prepend_relative(wd->items, item, reference);
894                 else
895                      wd->items = eina_list_append(wd->items, item);
896                 if (wd->view_state == MULTIBUTTONENTRY_VIEW_SHRINK)
897                   {
898                      elm_widget_sub_object_add(obj, btn);
899                      evas_object_hide(btn);
900                      _shrink_mode_set(obj, EINA_TRUE);
901                   }
902                 else
903                   {
904                      if (reference)
905                        elm_box_pack_before(wd->box, btn, reference->button);
906                      else
907                        {
908                           if (wd->view_state == MULTIBUTTONENTRY_VIEW_GUIDETEXT)
909                             _set_vis_guidetext(obj);
910                           if (wd->entry)
911                             elm_box_pack_before(wd->box, btn, wd->entry);
912                           else
913                             elm_box_pack_end(wd->box, btn);
914                        }
915                   }
916                 break;
917              case MULTIBUTTONENTRY_POS_AFTER:
918                 if (reference)
919                      wd->items = eina_list_append_relative(wd->items, item, reference);
920                 else
921                      wd->items = eina_list_append(wd->items, item);
922                 if (wd->view_state == MULTIBUTTONENTRY_VIEW_SHRINK)
923                   {
924                      elm_widget_sub_object_add(obj, btn);
925                      _shrink_mode_set(obj, EINA_TRUE);
926                   }
927                 else
928                   {
929                      if (reference)
930                        elm_box_pack_after(wd->box, btn, reference->button);
931                      else
932                        {
933                           if (wd->view_state == MULTIBUTTONENTRY_VIEW_GUIDETEXT)
934                             _set_vis_guidetext(obj);
935                           if (wd->entry)
936                             elm_box_pack_before(wd->box, btn, wd->entry);
937                           else
938                             elm_box_pack_end(wd->box, btn);
939                        }
940                   }
941                 break;
942              default:
943                 break;
944           }
945      }
946    evas_object_smart_callback_call(obj, "item,added", item);
947
948    free(str_utf8);
949
950    return (Elm_Object_Item *)item;
951 }
952
953 static Elm_Multibuttonentry_Item_Filter*
954 _filter_new(Elm_Multibuttonentry_Item_Filter_Cb func, void *data)
955 {
956    Elm_Multibuttonentry_Item_Filter *item_filter = ELM_NEW(Elm_Multibuttonentry_Item_Filter);
957    if (!item_filter) return NULL;
958
959    item_filter->callback_func= func;
960    item_filter->data = data;
961
962    return item_filter;
963 }
964
965 static void
966 _filter_free(Elm_Multibuttonentry_Item_Filter *item_filter)
967 {
968    free(item_filter);
969 }
970
971 static void
972 _evas_mbe_key_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
973 {
974    Widget_Data *wd = elm_widget_data_get(data);
975    Elm_Multibuttonentry_Item *item = NULL;
976
977    if (!wd || !wd->base || !wd->box) return;
978
979    Evas_Event_Key_Up *ev = (Evas_Event_Key_Up*)event_info;
980
981    if (wd->last_btn_select)
982      {
983         if (wd->current &&
984             ((strcmp(ev->keyname, "BackSpace") == 0) ||
985              (strcmp(ev->keyname, "Delete") == 0)))
986           {
987              item = eina_list_data_get(wd->current);
988              if (item)
989                {
990                   _del_button_item(item);
991                   elm_widget_item_free(item);
992                   elm_object_focus_set(wd->entry, EINA_TRUE);
993                }
994           }
995         else if (((!wd->current && (wd->n_str == 0) &&
996                    (strcmp(ev->keyname, "BackSpace") == 0)) ||
997                    (strcmp(ev->keyname, "Delete") == 0)))
998           {
999              item = eina_list_data_get(eina_list_last(wd->items));
1000              if (item)
1001                _select_button(data, item->button);
1002           }
1003      }
1004    else
1005      wd->last_btn_select = EINA_TRUE;
1006 }
1007
1008 static void
1009 _entry_key_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
1010 {
1011    Widget_Data *wd = elm_widget_data_get(data);
1012    Evas_Event_Key_Down *ev = (Evas_Event_Key_Down *)event_info;
1013
1014    if (!wd) return;
1015
1016    if ((wd->n_str == 1) && (strcmp(ev->keyname, "BackSpace") == 0 || (strcmp(ev->keyname, "Delete") == 0 )))
1017      wd->last_btn_select = EINA_FALSE;
1018 }
1019
1020 static void
1021 _entry_key_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
1022 {
1023    Widget_Data *wd = elm_widget_data_get(data);
1024    Evas_Event_Key_Up *ev = (Evas_Event_Key_Up *) event_info;
1025    const char *str;
1026
1027    if (!wd || !wd->base || !wd->box) return;
1028
1029    str = elm_object_text_get(wd->entry);
1030
1031    if ((strcmp(str, "") != 0) && (strcmp(ev->keyname, "KP_Enter") == 0 || strcmp(ev->keyname, "Return") == 0 ))
1032      {
1033         _add_button_item(data, str, MULTIBUTTONENTRY_POS_END, NULL, NULL, NULL);
1034         wd->n_str = 0;
1035      }
1036 }
1037
1038 static void
1039 _entry_clicked_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
1040 {
1041    Widget_Data *wd = elm_widget_data_get(data);
1042    if (!wd) return;
1043
1044    _change_current_button_state(data, MULTIBUTTONENTRY_BUTTON_STATE_DEFAULT);
1045    elm_object_focus_set(wd->entry, EINA_TRUE);
1046 }
1047
1048 static void
1049 _entry_focus_in_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
1050 {
1051    Widget_Data *wd = elm_widget_data_get(data);
1052    Elm_Multibuttonentry_Item *item = NULL;
1053
1054    if (!wd) return;
1055
1056    if (wd->current)
1057      {
1058         item = eina_list_data_get(wd->current);
1059         elm_object_focus_set(wd->entry, EINA_FALSE);
1060         evas_object_focus_set(item->button, EINA_TRUE);
1061      }
1062 }
1063
1064 static void
1065 _entry_focus_out_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
1066 {
1067    Widget_Data *wd = elm_widget_data_get(data);
1068    const char *str;
1069
1070    if (!wd) return;
1071
1072    str = elm_object_text_get(wd->entry);
1073    if (strlen(str))
1074      _add_button_item(data, str, MULTIBUTTONENTRY_POS_END, NULL, NULL, NULL);
1075 }
1076
1077 static void
1078 _entry_changed_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
1079 {
1080    Widget_Data *wd = elm_widget_data_get(data);
1081    const char *str;
1082
1083    if (!wd) return;
1084
1085    str = elm_object_text_get(wd->entry);
1086    wd->n_str = strlen(str);
1087 }
1088
1089 static void
1090 _entry_resized_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
1091 {
1092    Evas_Coord en_x, en_y, en_w, en_h;
1093    Evas_Coord bx_x, bx_y;
1094
1095    Widget_Data *wd = elm_widget_data_get(data);
1096    if (!wd) return;
1097
1098    evas_object_geometry_get(wd->entry, &en_x, &en_y, &en_w, &en_h);
1099    evas_object_geometry_get(wd->box, &bx_x, &bx_y, NULL, NULL);
1100
1101    if (wd->focused)
1102      elm_widget_show_region_set(wd->box, en_x - bx_x, en_y - bx_y, en_w,
1103                                 en_h, EINA_TRUE);
1104 }
1105
1106 static void
1107 _view_init(Evas_Object *obj)
1108 {
1109    Widget_Data *wd = elm_widget_data_get(obj);
1110
1111    if (!wd) return;
1112
1113    if (!wd->box)
1114      {
1115         wd->box = elm_box_add(obj);
1116         if (!wd->box) return;
1117         elm_widget_sub_object_add(obj, wd->box);
1118         elm_box_layout_set(wd->box, _box_layout_cb, NULL, NULL);
1119         elm_box_homogeneous_set(wd->box, EINA_FALSE);
1120         edje_object_part_swallow(wd->base, "box.swallow", wd->box);
1121      }
1122    if (!wd->label)
1123      {
1124         wd->label = edje_object_add(evas_object_evas_get(obj));
1125         if (!wd->label) return;
1126         _elm_theme_object_set(obj, wd->label, "multibuttonentry", "label", elm_widget_style_get(obj));
1127         _set_label(obj, "");
1128         elm_widget_sub_object_add(obj, wd->label);
1129      }
1130
1131    if (!wd->entry)
1132      {
1133         wd->entry = elm_entry_add(obj);
1134         if (!wd->entry) return;
1135         elm_entry_single_line_set(wd->entry, EINA_TRUE);
1136         elm_object_text_set(wd->entry, "");
1137         elm_entry_input_panel_enabled_set(wd->entry, EINA_FALSE);
1138         evas_object_size_hint_min_set(wd->entry, MIN_W_ENTRY, 0);
1139         evas_object_size_hint_weight_set(wd->entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1140         evas_object_size_hint_align_set(wd->entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
1141         if (wd->box) elm_box_pack_end(wd->box, wd->entry);
1142         evas_object_show(wd->entry);
1143         wd->view_state = MULTIBUTTONENTRY_VIEW_ENTRY;
1144      }
1145
1146    if (!wd->end)
1147      {
1148         const char *end_type;
1149
1150         end_type = edje_object_data_get(wd->base, "closed_button_type");
1151         if (!end_type || !strcmp(end_type, "label"))
1152           {
1153              wd->end = elm_label_add(obj);
1154              if (!wd->end) return;
1155              elm_object_style_set(wd->end, "extended/multibuttonentry_default");
1156              wd->end_type = MULTIBUTTONENTRY_CLOSED_LABEL;
1157           }
1158         else
1159           {
1160              const char *size_str;
1161              wd->end = edje_object_add(evas_object_evas_get(obj));
1162              if (!wd->end) return;
1163              _elm_theme_object_set(obj, wd->end, "multibuttonentry", "closedbutton", elm_widget_style_get(obj));
1164              Evas_Coord button_min_width = 0;
1165              Evas_Coord button_min_height = 0;
1166
1167              size_str = edje_object_data_get(wd->end, "closed_button_width");
1168              if (size_str) button_min_width = (Evas_Coord)atoi(size_str);
1169              size_str = edje_object_data_get(wd->end, "closed_button_height");
1170              if (size_str) button_min_height = (Evas_Coord)atoi(size_str);
1171
1172              wd->end_type = MULTIBUTTONENTRY_CLOSED_IMAGE;
1173              evas_object_size_hint_min_set(wd->end, 
1174                                            button_min_width * _elm_config->scale, 
1175                                            button_min_height * _elm_config->scale);
1176              elm_widget_sub_object_add(obj, wd->end);
1177           }
1178      }
1179 }
1180
1181 static void
1182 _calculate_box_min_size(Evas_Object *box, Evas_Object_Box_Data *priv)
1183 {
1184    Evas_Coord minw, minh, mnw, mnh, ww;
1185    Evas_Coord w, cw = 0, cmaxh = 0;
1186    const Eina_List *l;
1187    Evas_Object_Box_Option *opt;
1188    double wx;
1189
1190    /* FIXME: need to calc max */
1191    minw = 0;
1192    minh = 0;
1193
1194    evas_object_geometry_get(box, NULL, NULL, &w, NULL);
1195    evas_object_size_hint_min_get(box, &minw, NULL);
1196
1197    EINA_LIST_FOREACH(priv->children, l, opt)
1198      {
1199         evas_object_size_hint_min_get(opt->obj, &mnw, &mnh);
1200         evas_object_size_hint_weight_get(opt->obj, &wx, NULL);
1201
1202         if (wx)
1203           {
1204              if (mnw != -1 && (w - cw) >= mnw)
1205                ww = w - cw;
1206              else
1207                ww = w;
1208           }
1209         else
1210            ww = mnw;
1211
1212         if ((cw + mnw) > w)
1213           {
1214              minh += cmaxh;
1215              cw = 0;
1216              cmaxh = 0;
1217           }
1218         cw += ww;
1219         if (cmaxh < mnh) cmaxh = mnh;
1220      }
1221
1222    minh += cmaxh;
1223
1224    evas_object_size_hint_min_set(box, minw, minh);
1225 }
1226
1227 static Evas_Coord
1228 _calculate_item_max_height(Evas_Object *box, Evas_Object_Box_Data *priv, int obj_index)
1229 {
1230    Evas_Coord mnw, mnh, cw = 0, cmaxh = 0, w, ww;
1231    const Eina_List *l;
1232    Evas_Object_Box_Option *opt;
1233    int local_index = 0;
1234    double wx;
1235
1236    evas_object_geometry_get(box, NULL, NULL, &w, NULL);
1237
1238    EINA_LIST_FOREACH(priv->children, l, opt)
1239      {
1240         evas_object_size_hint_min_get(opt->obj, &mnw, &mnh);
1241         evas_object_size_hint_weight_get(opt->obj, &wx, NULL);
1242
1243         if (wx)
1244           {
1245              if (mnw != -1 && (w - cw) >= mnw)
1246                 ww = w - cw;
1247              else
1248                 ww = w;
1249           }
1250         else
1251            ww = mnw;
1252
1253         if ((cw + ww) > w)
1254           {
1255              if (local_index > obj_index) return cmaxh;
1256              cw = 0;
1257              cmaxh = 0;
1258           }
1259
1260         cw += ww;
1261         if (cmaxh < mnh) cmaxh = mnh;
1262
1263         local_index++;
1264      }
1265
1266    return cmaxh;
1267 }
1268
1269 static void
1270 _box_layout_cb(Evas_Object *o, Evas_Object_Box_Data *priv, void *data __UNUSED__)
1271 {
1272    Evas_Coord x, y, w, h, xx, yy;
1273    const Eina_List *l;
1274    Evas_Object *obj;
1275    Evas_Coord minw, minh;
1276    double ax, ay;
1277    Evas_Object_Box_Option *opt;
1278
1279    _calculate_box_min_size(o, priv);
1280
1281    evas_object_geometry_get(o, &x, &y, &w, &h);
1282
1283    evas_object_size_hint_min_get(o, &minw, &minh);
1284    evas_object_size_hint_align_get(o, &ax, &ay);
1285    if (w < minw)
1286      {
1287         x = x + ((w - minw) * (1.0 - ax));
1288         w = minw;
1289      }
1290    if (h < minh)
1291      {
1292         y = y + ((h - minh) * (1.0 - ay));
1293         h = minh;
1294      }
1295
1296    xx = x;
1297    yy = y;
1298
1299    Evas_Coord cw = 0, ch = 0, cmaxh = 0, obj_index = 0;
1300
1301    EINA_LIST_FOREACH(priv->children, l, opt)
1302      {
1303         Evas_Coord mnw, mnh, mxw, mxh;
1304         double wx, wy;
1305         int fw, fh;
1306
1307         obj = opt->obj;
1308         evas_object_size_hint_align_get(obj, &ax, &ay);
1309         evas_object_size_hint_weight_get(obj, &wx, &wy);
1310         evas_object_size_hint_min_get(obj, &mnw, &mnh);
1311         evas_object_size_hint_max_get(obj, &mxw, &mxh);
1312         fw = fh = 0;
1313         if (ax == -1.0) {fw = 1; ax = 0.5;}
1314         if (ay == -1.0) {fh = 1; ay = 0.5;}
1315         Evas_Coord ww, hh, ow, oh;
1316
1317         if (wx)
1318           {
1319              if (mnw != -1 && (w - cw) >= mnw)
1320                 ww = w - cw;
1321              else
1322                 ww = w;
1323           }
1324         else
1325            ww = mnw;
1326         hh = _calculate_item_max_height(o, priv, obj_index);
1327
1328         ow = mnw;
1329         if (fw) ow = ww;
1330         if ((mxw >= 0) && (mxw < ow)) ow = mxw;
1331         oh = mnh;
1332         if (fh) oh = hh;
1333         if ((mxh >= 0) && (mxh < oh)) oh = mxh;
1334
1335         if ((cw + ww) > w)
1336           {
1337              ch += cmaxh;
1338              cw = 0;
1339              cmaxh = 0;
1340           }
1341
1342         evas_object_move(obj,
1343                          xx + cw + (Evas_Coord)(((double)(ww - ow)) * ax),
1344                          yy + ch + (Evas_Coord)(((double)(hh - oh)) * ay));
1345         evas_object_resize(obj, ow, oh);
1346
1347         cw += ww;
1348         if (cmaxh < hh) cmaxh = hh;
1349
1350         obj_index++;
1351      }
1352 }
1353
1354 static void
1355 _item_text_set_hook(Elm_Object_Item *it, const char *part, const char *label)
1356 {
1357    Elm_Multibuttonentry_Item *item;
1358    if (part && strcmp(part, "default")) return;
1359    if (!label) return;
1360    item = (Elm_Multibuttonentry_Item *)it;
1361    edje_object_part_text_set(item->button, "elm.btn.text", label);
1362    _resize_button(item->button, &item->rw, &item->vw);
1363 }
1364
1365 static const char *
1366 _item_text_get_hook(const Elm_Object_Item *it, const char *part)
1367 {
1368    Elm_Multibuttonentry_Item *item;
1369    if (part && strcmp(part, "default")) return NULL;
1370    item = (Elm_Multibuttonentry_Item *)it;
1371    return edje_object_part_text_get(item->button, "elm.btn.text");
1372 }
1373
1374 static void
1375 _text_set_hook(Evas_Object *obj, const char *part, const char *label)
1376 {
1377    ELM_CHECK_WIDTYPE(obj, widtype);
1378    if (!part || !strcmp(part, "default"))
1379      {
1380         if (label) _set_label(obj, label);
1381         else _set_label(obj, "");
1382      }
1383    else if (!strcmp(part, "guide"))
1384      {
1385         if (label) _set_guidetext(obj, label);
1386         else _set_guidetext(obj, "");
1387      }
1388 }
1389
1390 static const char *
1391 _text_get_hook(const Evas_Object *obj, const char *part)
1392 {
1393    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1394    Widget_Data *wd = elm_widget_data_get(obj);
1395    if (!wd) return NULL;
1396
1397    if (!part || !strcmp(part, "default"))
1398      {
1399         return wd->labeltxt;
1400      }
1401    else if (!strcmp(part, "guide"))
1402      {
1403         return wd->guidetexttxt;
1404      }
1405    return NULL;
1406 }
1407
1408 EAPI Evas_Object *
1409 elm_multibuttonentry_add(Evas_Object *parent)
1410 {
1411    Evas_Object *obj;
1412    Evas *e;
1413    Widget_Data *wd;
1414
1415    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
1416
1417    ELM_SET_WIDTYPE(widtype, "multibuttonentry");
1418    elm_widget_type_set(obj, "multibuttonentry");
1419    elm_widget_sub_object_add(parent, obj);
1420    elm_widget_data_set(obj, wd);
1421
1422    elm_widget_del_hook_set(obj, _del_hook);
1423    elm_widget_theme_hook_set(obj, _theme_hook);
1424    elm_widget_event_hook_set(obj, _event_hook);
1425    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
1426    elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
1427    elm_widget_text_set_hook_set(obj, _text_set_hook);
1428    elm_widget_text_get_hook_set(obj, _text_get_hook);
1429
1430    wd->base = edje_object_add(e);
1431    _elm_theme_object_set(obj, wd->base, "multibuttonentry", "base", "default");
1432    elm_widget_resize_object_set(obj, wd->base);
1433    elm_widget_can_focus_set(obj, EINA_TRUE);
1434
1435    wd->view_state = MULTIBUTTONENTRY_VIEW_NONE;
1436    wd->focused = EINA_FALSE;
1437    wd->last_btn_select = EINA_TRUE;
1438    wd->n_str = 0;
1439    wd->rect_for_end = NULL;
1440    wd->add_callback = NULL;
1441    wd->add_callback_data = NULL;
1442
1443    _view_init(obj);
1444    _event_init(obj);
1445
1446    return obj;
1447 }
1448
1449 EAPI Evas_Object *
1450 elm_multibuttonentry_entry_get(const Evas_Object *obj)
1451 {
1452    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1453    Widget_Data *wd = elm_widget_data_get(obj);
1454
1455    if (!wd) return NULL;
1456
1457    return wd->entry;
1458 }
1459
1460 EINA_DEPRECATED EAPI const char *
1461 elm_multibuttonentry_label_get(const Evas_Object *obj)
1462 {
1463    return _text_get_hook(obj, NULL);
1464 }
1465
1466 EINA_DEPRECATED EAPI void
1467 elm_multibuttonentry_label_set(Evas_Object *obj, const char *label)
1468 {
1469    _text_set_hook(obj, NULL, label);
1470 }
1471
1472 EINA_DEPRECATED EAPI const char *
1473 elm_multibuttonentry_guide_text_get(const Evas_Object *obj)
1474 {
1475    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1476    Widget_Data *wd = elm_widget_data_get(obj);
1477
1478    if (!wd) return NULL;
1479    if (wd->guidetext) return edje_object_part_text_get(wd->guidetext, "elm.text");
1480    return NULL;
1481 }
1482
1483 EINA_DEPRECATED EAPI void
1484 elm_multibuttonentry_guide_text_set(Evas_Object *obj, const char *guidetext)
1485 {
1486    ELM_CHECK_WIDTYPE(obj, widtype);
1487    Widget_Data *wd = elm_widget_data_get(obj);
1488
1489    if (!wd) return;
1490
1491    _set_guidetext(obj, guidetext);
1492 }
1493
1494 EINA_DEPRECATED EAPI int
1495 elm_multibuttonentry_shrink_mode_get(const Evas_Object *obj)
1496 {
1497    if (elm_multibuttonentry_expanded_get(obj))
1498      return 0;
1499    else
1500      return 1;
1501 }
1502
1503 EAPI Eina_Bool
1504 elm_multibuttonentry_expanded_get(const Evas_Object *obj)
1505 {
1506    ELM_CHECK_WIDTYPE(obj, widtype) -1;
1507    Widget_Data *wd = elm_widget_data_get(obj);
1508
1509    if (!wd) return -1;
1510      return (wd->view_state == MULTIBUTTONENTRY_VIEW_SHRINK ? EINA_FALSE : EINA_TRUE);
1511
1512 }
1513
1514 EINA_DEPRECATED EAPI void
1515 elm_multibuttonentry_shrink_mode_set(Evas_Object *obj, int shrink)
1516 {
1517    if (shrink == 0)
1518      elm_multibuttonentry_expanded_set(obj, EINA_TRUE);
1519
1520    if (shrink == 1)
1521      elm_multibuttonentry_expanded_set(obj, EINA_FALSE);
1522 }
1523
1524 EAPI void
1525 elm_multibuttonentry_expanded_set(Evas_Object *obj, Eina_Bool expanded)
1526 {
1527    ELM_CHECK_WIDTYPE(obj, widtype);
1528    Widget_Data *wd = elm_widget_data_get(obj);
1529
1530    if (!wd || !wd->box ||
1531        ((wd->view_state == MULTIBUTTONENTRY_VIEW_SHRINK) ? EINA_FALSE : EINA_TRUE) == expanded) return;
1532
1533    if (expanded)
1534      _shrink_mode_set(obj, EINA_FALSE);
1535    else
1536      _shrink_mode_set(obj, EINA_TRUE);
1537
1538 }
1539
1540 EAPI Elm_Object_Item *
1541 elm_multibuttonentry_item_prepend(Evas_Object *obj, const char *label, Evas_Smart_Cb func, void *data)
1542 {
1543    return _add_button_item(obj, label, MULTIBUTTONENTRY_POS_START, NULL, func, data);
1544 }
1545
1546 EAPI Elm_Object_Item *
1547 elm_multibuttonentry_item_append(Evas_Object *obj, const char *label, Evas_Smart_Cb func, void *data)
1548 {
1549    return _add_button_item(obj, label, MULTIBUTTONENTRY_POS_END, NULL, func, data);
1550 }
1551
1552 EAPI Elm_Object_Item *
1553 elm_multibuttonentry_item_insert_before(Evas_Object *obj, Elm_Object_Item *before, const char *label, Evas_Smart_Cb func, void *data)
1554 {
1555    return _add_button_item(obj, label, MULTIBUTTONENTRY_POS_BEFORE, before, func, data);
1556 }
1557
1558 EAPI Elm_Object_Item *
1559 elm_multibuttonentry_item_insert_after(Evas_Object *obj, Elm_Object_Item *after, const char *label, Evas_Smart_Cb func, void *data)
1560 {
1561    return _add_button_item(obj, label, MULTIBUTTONENTRY_POS_AFTER, after, func, data);
1562 }
1563
1564 EAPI const Eina_List *
1565 elm_multibuttonentry_items_get(const Evas_Object *obj)
1566 {
1567    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1568    Widget_Data *wd = elm_widget_data_get(obj);
1569    if (!wd) return NULL;
1570    return wd->items;
1571 }
1572
1573 EAPI Elm_Object_Item *
1574 elm_multibuttonentry_first_item_get(const Evas_Object *obj)
1575 {
1576    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1577    Widget_Data *wd = elm_widget_data_get(obj);
1578    if (!wd) return NULL;
1579    return eina_list_data_get(wd->items);
1580 }
1581
1582 EAPI Elm_Object_Item *
1583 elm_multibuttonentry_last_item_get(const Evas_Object *obj)
1584 {
1585    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1586    Widget_Data *wd = elm_widget_data_get(obj);
1587    if (!wd) return NULL;
1588    return eina_list_data_get(eina_list_last(wd->items));
1589 }
1590
1591 EAPI Elm_Object_Item *
1592 elm_multibuttonentry_selected_item_get(const Evas_Object *obj)
1593 {
1594    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1595    Widget_Data *wd = elm_widget_data_get(obj);
1596    if (!wd) return NULL;
1597    return eina_list_data_get(wd->current);
1598 }
1599
1600 EAPI void
1601 elm_multibuttonentry_item_selected_set(Elm_Object_Item *it, Eina_Bool selected)
1602 {
1603    ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
1604    Elm_Multibuttonentry_Item *item = (Elm_Multibuttonentry_Item *)it;
1605    if (selected) _select_button(WIDGET(item), item->button);
1606    else _select_button(WIDGET(item), NULL);
1607 }
1608
1609 EAPI Eina_Bool
1610 elm_multibuttonentry_item_selected_get(const Elm_Object_Item *it)
1611 {
1612    //TODO : To be implemented.
1613    if (!it) return EINA_FALSE;
1614    return EINA_TRUE;
1615 }
1616
1617 EAPI void
1618 elm_multibuttonentry_clear(Evas_Object *obj)
1619 {
1620    ELM_CHECK_WIDTYPE(obj, widtype);
1621    Elm_Multibuttonentry_Item *item;
1622    Widget_Data *wd = elm_widget_data_get(obj);
1623    if (!wd) return;
1624
1625    if (wd->items)
1626      {
1627         EINA_LIST_FREE(wd->items, item)
1628           {
1629              elm_box_unpack(wd->box, item->button);
1630              _del_button_obj(obj, item->button);
1631              free(item);
1632           }
1633         wd->items = NULL;
1634      }
1635    wd->current = NULL;
1636    _view_update(obj);
1637 }
1638
1639 EAPI void
1640 elm_multibuttonentry_item_del(Elm_Object_Item *it)
1641 {
1642    elm_object_item_del(it);
1643 }
1644
1645 EAPI const char *
1646 elm_multibuttonentry_item_label_get(const Elm_Object_Item *it)
1647 {
1648    return _item_text_get_hook(it, NULL);
1649 }
1650
1651 EAPI void
1652 elm_multibuttonentry_item_label_set(Elm_Object_Item *it, const char *str)
1653 {
1654    _item_text_set_hook(it, NULL, str);
1655 }
1656
1657 EAPI Elm_Object_Item *
1658 elm_multibuttonentry_item_prev_get(const Elm_Object_Item *it)
1659 {
1660    ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
1661    Widget_Data *wd;
1662    Eina_List *l;
1663    Elm_Multibuttonentry_Item *_item;
1664
1665    wd = elm_widget_data_get(WIDGET(it));
1666    if (!wd) return NULL;
1667
1668    EINA_LIST_FOREACH(wd->items, l, _item)
1669      {
1670         if (_item == (Elm_Multibuttonentry_Item *)it)
1671           {
1672              l = eina_list_prev(l);
1673              if (!l) return NULL;
1674              return eina_list_data_get(l);
1675           }
1676      }
1677    return NULL;
1678 }
1679
1680 EAPI Elm_Object_Item *
1681 elm_multibuttonentry_item_next_get(const Elm_Object_Item *it)
1682 {
1683    ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
1684    Widget_Data *wd;
1685    Eina_List *l;
1686    Elm_Multibuttonentry_Item *_item;
1687    wd = elm_widget_data_get(WIDGET(it));
1688    if (!wd) return NULL;
1689
1690    EINA_LIST_FOREACH(wd->items, l, _item)
1691      {
1692         if (_item == (Elm_Multibuttonentry_Item *)it)
1693           {
1694              l = eina_list_next(l);
1695              if (!l) return NULL;
1696              return eina_list_data_get(l);
1697           }
1698      }
1699    return NULL;
1700 }
1701
1702 EAPI void *
1703 elm_multibuttonentry_item_data_get(const Elm_Object_Item *it)
1704 {
1705    return elm_widget_item_data_get(it);
1706 }
1707
1708 EAPI void
1709 elm_multibuttonentry_item_data_set(Elm_Object_Item *it, void *data)
1710 {
1711    return elm_widget_item_data_set(it, data);
1712 }
1713
1714 EAPI void
1715 elm_multibuttonentry_item_filter_append(Evas_Object *obj, Elm_Multibuttonentry_Item_Filter_Cb func, void *data)
1716 {
1717    Elm_Multibuttonentry_Item_Filter *new_item_filter = NULL;
1718    Elm_Multibuttonentry_Item_Filter *_item_filter = NULL;
1719    Eina_List *l;
1720
1721    Widget_Data *wd = elm_widget_data_get(obj);
1722    if (!wd) return;
1723
1724    ELM_CHECK_WIDTYPE(obj, widtype);
1725    EINA_SAFETY_ON_NULL_RETURN(func);
1726
1727    new_item_filter= _filter_new(func, data);
1728    if (!new_item_filter) return;
1729
1730    EINA_LIST_FOREACH(wd->filter_list, l, _item_filter)
1731      {
1732         if (_item_filter && ((_item_filter->callback_func == func) && (_item_filter->data == data)))
1733           {
1734              printf("Already Registered this item filter!!!!\n");
1735              return;
1736           }
1737      }
1738    wd->filter_list = eina_list_append(wd->filter_list, new_item_filter);
1739 }
1740
1741 EAPI void
1742 elm_multibuttonentry_item_filter_prepend(Evas_Object *obj, Elm_Multibuttonentry_Item_Filter_Cb func, void *data)
1743 {
1744    Elm_Multibuttonentry_Item_Filter *new_item_filter = NULL;
1745    Elm_Multibuttonentry_Item_Filter *_item_filter = NULL;
1746    Eina_List *l;
1747    Widget_Data *wd = elm_widget_data_get(obj);
1748    if (!wd) return;
1749
1750    ELM_CHECK_WIDTYPE(obj, widtype);
1751    EINA_SAFETY_ON_NULL_RETURN(func);
1752
1753    new_item_filter = _filter_new(func, data);
1754    if (!new_item_filter) return;
1755
1756    EINA_LIST_FOREACH(wd->filter_list, l, _item_filter)
1757      {
1758         if (_item_filter && ((_item_filter->callback_func == func) && (_item_filter->data == data)))
1759           {
1760              printf("Already Registered this item filter!!!!\n");
1761              return;
1762           }
1763      }
1764    wd->filter_list = eina_list_prepend(wd->filter_list, new_item_filter);
1765 }
1766
1767 EAPI void
1768 elm_multibuttonentry_item_filter_remove(Evas_Object *obj, Elm_Multibuttonentry_Item_Filter_Cb func, void *data)
1769 {
1770    Widget_Data *wd;
1771    Eina_List *l;
1772    Elm_Multibuttonentry_Item_Filter *item_filter;
1773
1774    wd = elm_widget_data_get(obj);
1775
1776    EINA_SAFETY_ON_NULL_RETURN(func);
1777
1778    EINA_LIST_FOREACH(wd->filter_list, l, item_filter)
1779      {
1780         if ((item_filter->callback_func == func) && ((!data) || (item_filter->data == data)))
1781           {
1782              wd->filter_list = eina_list_remove_list(wd->filter_list, l);
1783              _filter_free(item_filter);
1784              return;
1785           }
1786      }
1787 }