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