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