[multibuttonentry]items aren't packed when view_mode is set in Calendar app
[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                           else
878                             elm_box_pack_end(wd->box, btn);
879                        }
880                      else
881                        elm_box_pack_end(wd->box, btn);
882                   }
883                 break;
884              case MULTIBUTTONENTRY_POS_BEFORE:
885                 if (reference)
886                      wd->items = eina_list_prepend_relative(wd->items, item, reference);
887                 else
888                      wd->items = eina_list_append(wd->items, item);
889                 if (wd->view_state == MULTIBUTTONENTRY_VIEW_SHRINK)
890                   {
891                      elm_widget_sub_object_add(obj, btn);
892                      evas_object_hide(btn);
893                      _shrink_mode_set(obj, 1);
894                   }
895                 else
896                   {
897                      if (reference)
898                        elm_box_pack_before(wd->box, btn, reference->button);
899                      else
900                        {
901                           if (wd->view_state == MULTIBUTTONENTRY_VIEW_GUIDETEXT)
902                             _set_vis_guidetext(obj);
903                           if (wd->entry)
904                             {
905                                if (!wd->view_mode)
906                                  elm_box_pack_before(wd->box, btn, wd->entry);
907                                else
908                                  elm_box_pack_end(wd->box, btn);
909                             }
910                           else
911                             elm_box_pack_end(wd->box, btn);
912                        }
913                   }
914                 break;
915              case MULTIBUTTONENTRY_POS_AFTER:
916                 if (reference)
917                      wd->items = eina_list_append_relative(wd->items, item, reference);
918                 else
919                      wd->items = eina_list_append(wd->items, item);
920                 if (wd->view_state == MULTIBUTTONENTRY_VIEW_SHRINK)
921                   {
922                      elm_widget_sub_object_add(obj, btn);
923                      _shrink_mode_set(obj, 1);
924                   }
925                 else
926                   {
927                      if (reference)
928                        elm_box_pack_after(wd->box, btn, reference->button);
929                      else
930                        {
931                           if (wd->view_state == MULTIBUTTONENTRY_VIEW_GUIDETEXT)
932                             _set_vis_guidetext(obj);
933                           if (wd->entry)
934                             {
935                                if (!wd->view_mode)
936                                  elm_box_pack_before(wd->box, btn, wd->entry);
937                                else
938                                  elm_box_pack_end(wd->box, btn);
939                             }
940                           else
941                             elm_box_pack_end(wd->box, btn);
942                        }
943                   }
944                 break;
945              default:
946                 break;
947           }
948      }
949    evas_object_smart_callback_call(obj, "item,added", item);
950
951    free(str_utf8);
952
953    return item;
954 }
955
956 static void
957 _add_button(Evas_Object *obj, const char *str)
958 {
959    Widget_Data *wd = elm_widget_data_get(obj);
960    if (!wd) return;
961
962    _add_button_item(obj, str, MULTIBUTTONENTRY_POS_END, NULL, NULL);
963 }
964
965 static Elm_Multibuttonentry_Item_Filter*
966 _filter_new(Elm_Multibuttonentry_Item_Filter_callback func, void *data)
967 {
968    Elm_Multibuttonentry_Item_Filter *item_filter = ELM_NEW(Elm_Multibuttonentry_Item_Filter);
969    if (!item_filter) return NULL;
970
971    item_filter->callback_func= func;
972    item_filter->data = data;
973
974    return item_filter;
975 }
976
977 static void
978 _filter_free(Elm_Multibuttonentry_Item_Filter *item_filter)
979 {
980    free(item_filter);
981 }
982
983 static void
984 _evas_mbe_key_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
985 {
986    Widget_Data *wd = elm_widget_data_get(data);
987    Elm_Multibuttonentry_Item *item = NULL;
988
989    if (!wd || !wd->base || !wd->box) return;
990
991    Evas_Event_Key_Up *ev = (Evas_Event_Key_Up*)event_info;
992
993    if (wd->last_btn_select)
994      {
995         if (wd->current &&
996             ((strcmp(ev->keyname, "BackSpace") == 0) ||
997              (strcmp(ev->keyname, "BackSpace (") == 0)))
998           {
999              item = eina_list_data_get(wd->current);
1000              if (item)
1001                {
1002                   _del_button_item(item);
1003                   elm_widget_item_free(item);
1004                   elm_object_focus_set(wd->entry, EINA_TRUE);
1005                }
1006           }
1007         else if (((!wd->current && (wd->n_str == 0) &&
1008                    (strcmp(ev->keyname, "BackSpace") == 0)) ||
1009                   (strcmp(ev->keyname, "BackSpace (") == 0)))
1010           {
1011              item = eina_list_data_get(eina_list_last(wd->items));
1012              if (item)
1013                _select_button(data, item->button);
1014           }
1015      }
1016    else
1017      wd->last_btn_select = EINA_TRUE;
1018 }
1019
1020 static void
1021 _entry_key_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
1022 {
1023    Widget_Data *wd = elm_widget_data_get(data);
1024    Evas_Event_Key_Down *ev = (Evas_Event_Key_Down *)event_info;
1025
1026    if (!wd) return;
1027
1028    if ((wd->n_str == 1) && (strcmp(ev->keyname, "BackSpace") == 0 || (strcmp(ev->keyname, "BackSpace (") == 0 )))
1029      wd->last_btn_select = EINA_FALSE;
1030 }
1031
1032 static void
1033 _entry_key_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
1034 {
1035    Widget_Data *wd = elm_widget_data_get(data);
1036    Evas_Event_Key_Up *ev = (Evas_Event_Key_Up *) event_info;
1037    const char *str;
1038
1039    if (!wd || !wd->base || !wd->box) return;
1040
1041    str = elm_object_text_get(wd->entry);
1042
1043    if ((strcmp(str, "") != 0) && (strcmp(ev->keyname, "KP_Enter") == 0 || strcmp(ev->keyname, "Return") == 0 ))
1044      {
1045         _add_button(data, str);
1046         wd->n_str = 0;
1047      }
1048 }
1049
1050 static void
1051 _entry_clicked_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
1052 {
1053    Widget_Data *wd = elm_widget_data_get(data);
1054    if (!wd) return;
1055
1056    _change_current_button_state(data, MULTIBUTTONENTRY_BUTTON_STATE_DEFAULT);
1057    elm_object_focus_set(wd->entry, EINA_TRUE);
1058 }
1059
1060 static void
1061 _entry_focus_in_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
1062 {
1063    Widget_Data *wd = elm_widget_data_get(data);
1064    Elm_Multibuttonentry_Item *item = NULL;
1065
1066    if (!wd) return;
1067
1068    if (wd->current)
1069      {
1070         item = eina_list_data_get(wd->current);
1071         elm_object_focus_set(wd->entry, EINA_FALSE);
1072         evas_object_focus_set(item->button, EINA_TRUE);
1073      }
1074 }
1075
1076 static void
1077 _entry_focus_out_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
1078 {
1079    Widget_Data *wd = elm_widget_data_get(data);
1080    const char *str;
1081
1082    if (!wd) return;
1083
1084    str = elm_object_text_get(wd->entry);
1085    if (strlen(str))
1086      _add_button(data, str);
1087 }
1088
1089 static void
1090 _entry_changed_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
1091 {
1092    Widget_Data *wd = elm_widget_data_get(data);
1093    const char *str;
1094
1095    if (!wd) return;
1096
1097    str = elm_object_text_get(wd->entry);
1098    wd->n_str = strlen(str);
1099 }
1100
1101 static void
1102 _entry_resized_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
1103 {
1104    Evas_Coord en_x, en_y, en_w, en_h;
1105
1106    Widget_Data *wd = elm_widget_data_get(data);
1107    if (!wd) return;
1108
1109    evas_object_geometry_get(wd->entry, &en_x, &en_y, &en_w, &en_h);
1110
1111    if (wd->focused)
1112      elm_widget_show_region_set(wd->entry, en_x, en_y, en_w, en_h, EINA_TRUE);
1113 }
1114
1115 static void
1116 _view_init(Evas_Object *obj)
1117 {
1118    Widget_Data *wd = elm_widget_data_get(obj);
1119
1120    if (!wd) return;
1121
1122    if (!wd->box)
1123      {
1124         wd->box = elm_box_add (obj);
1125         if (!wd->box) return;
1126         elm_widget_sub_object_add(obj, wd->box);
1127         elm_box_layout_set(wd->box, _box_layout_cb, NULL, NULL);
1128         elm_box_homogeneous_set(wd->box, EINA_FALSE);
1129         edje_object_part_swallow(wd->base, "box.swallow", wd->box);
1130      }
1131    if (!wd->label)
1132      {
1133         wd->label = edje_object_add(evas_object_evas_get(obj));
1134         if (!wd->label) return;
1135         _elm_theme_object_set(obj, wd->label, "multibuttonentry", "label", elm_widget_style_get(obj));
1136         _set_label(obj, "");
1137         elm_widget_sub_object_add(obj, wd->label);
1138      }
1139
1140    if (!wd->entry)
1141      {
1142         wd->entry = elm_entry_add (obj);
1143         if (!wd->entry) return;
1144         elm_entry_scrollable_set(wd->entry, EINA_TRUE);
1145         elm_entry_single_line_set(wd->entry, EINA_TRUE);
1146         elm_object_text_set(wd->entry, "");
1147         elm_entry_input_panel_enabled_set(wd->entry, EINA_FALSE);
1148         evas_object_size_hint_min_set(wd->entry, MIN_W_ENTRY, 0);
1149         evas_object_size_hint_weight_set(wd->entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1150         evas_object_size_hint_align_set(wd->entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
1151         if (wd->box) elm_box_pack_end (wd->box, wd->entry);
1152         evas_object_show(wd->entry);
1153         wd->view_state = MULTIBUTTONENTRY_VIEW_ENTRY;
1154      }
1155
1156    if (!wd->end)
1157      {
1158         const char *end_type;
1159
1160         end_type = edje_object_data_get(wd->base, "closed_button_type");
1161         if (!end_type || !strcmp(end_type, "label"))
1162           {
1163              wd->end = elm_label_add (obj);
1164              if (!wd->end) return;
1165              elm_object_style_set(wd->end, "extended/multibuttonentry_default");
1166              wd->end_type = MULTIBUTTONENTRY_CLOSED_LABEL;
1167           }
1168         else
1169           {
1170              const char *size_str;
1171              wd->end = edje_object_add(evas_object_evas_get(obj));
1172              if (!wd->end) return;
1173              _elm_theme_object_set(obj, wd->end, "multibuttonentry", "closedbutton", elm_widget_style_get(obj));
1174              Evas_Coord button_min_width = 0;
1175              Evas_Coord button_min_height = 0;
1176
1177              size_str = edje_object_data_get(wd->end, "closed_button_width");
1178              if (size_str) button_min_width = (Evas_Coord)atoi(size_str);
1179              size_str = edje_object_data_get(wd->end, "closed_button_height");
1180              if (size_str) button_min_height = (Evas_Coord)atoi(size_str);
1181
1182              wd->end_type = MULTIBUTTONENTRY_CLOSED_IMAGE;
1183              evas_object_size_hint_min_set(wd->end, button_min_width * elm_scale_get(), button_min_height * elm_scale_get());
1184              elm_widget_sub_object_add(obj, wd->end);
1185           }
1186      }
1187 }
1188
1189 static void
1190 _calculate_box_min_size(Evas_Object *box, Evas_Object_Box_Data *priv)
1191 {
1192    Evas_Coord minw, minh, mnw, mnh, ww;
1193    Evas_Coord w, cw = 0, cmaxh = 0;
1194    const Eina_List *l;
1195    Evas_Object_Box_Option *opt;
1196    double wx;
1197
1198    /* FIXME: need to calc max */
1199    minw = 0;
1200    minh = 0;
1201
1202    evas_object_geometry_get(box, NULL, NULL, &w, NULL);
1203    evas_object_size_hint_min_get(box, &minw, NULL);
1204
1205    EINA_LIST_FOREACH(priv->children, l, opt)
1206      {
1207         evas_object_size_hint_min_get(opt->obj, &mnw, &mnh);
1208         evas_object_size_hint_weight_get(opt->obj, &wx, NULL);
1209
1210         if (wx)
1211           {
1212              if ((elm_widget_is(opt->obj)) && (!(strcmp(elm_widget_type_get(opt->obj), "entry"))) && (mnw == -1))
1213                mnw = MIN_W_ENTRY;
1214
1215              if (mnw != -1 && (w - cw) >= mnw)
1216                ww = w - cw;
1217              else
1218                ww = w;
1219           }
1220         else
1221           ww = mnw;
1222
1223         if ((cw + mnw) > w)
1224           {
1225              minh += cmaxh;
1226              cw = 0;
1227              cmaxh = 0;
1228           }
1229         cw += ww;
1230         if (cmaxh < mnh) cmaxh = mnh;
1231      }
1232
1233    minh += cmaxh;
1234
1235    evas_object_size_hint_min_set(box, minw, minh);
1236 }
1237
1238 static Evas_Coord
1239 _calculate_item_max_height(Evas_Object *box, Evas_Object_Box_Data *priv, int obj_index)
1240 {
1241    Evas_Coord mnw, mnh, cw = 0, cmaxh = 0, w, ww;
1242    const Eina_List *l;
1243    Evas_Object_Box_Option *opt;
1244    int index = 0;
1245    double wx;
1246
1247    evas_object_geometry_get(box, NULL, NULL, &w, NULL);
1248
1249    EINA_LIST_FOREACH(priv->children, l, opt)
1250      {
1251         evas_object_size_hint_min_get(opt->obj, &mnw, &mnh);
1252         evas_object_size_hint_weight_get(opt->obj, &wx, NULL);
1253
1254         if (wx)
1255           {
1256              if ((elm_widget_is(opt->obj)) && (!(strcmp(elm_widget_type_get(opt->obj), "entry"))) && (mnw == -1))
1257                mnw = MIN_W_ENTRY;
1258
1259              if (mnw != -1 && (w - cw) >= mnw)
1260                ww = w - cw;
1261              else
1262                ww = w;
1263           }
1264         else
1265           ww = mnw;
1266
1267         if ((cw + ww) > w)
1268           {
1269              if (index > obj_index) return cmaxh;
1270              cw = 0;
1271              cmaxh = 0;
1272           }
1273
1274         cw += ww;
1275         if (cmaxh < mnh) cmaxh = mnh;
1276
1277         index++;
1278      }
1279
1280    return cmaxh;
1281 }
1282
1283 static void
1284 _box_layout_cb(Evas_Object *o, Evas_Object_Box_Data *priv, void *data __UNUSED__)
1285 {
1286    Evas_Coord x, y, w, h, xx, yy;
1287    const Eina_List *l;
1288    Evas_Object *obj;
1289    Evas_Coord minw, minh;
1290    double ax, ay;
1291    Evas_Object_Box_Option *opt;
1292
1293    _calculate_box_min_size(o, priv);
1294
1295    evas_object_geometry_get(o, &x, &y, &w, &h);
1296
1297    evas_object_size_hint_min_get(o, &minw, &minh);
1298    evas_object_size_hint_align_get(o, &ax, &ay);
1299    if (w < minw)
1300      {
1301         x = x + ((w - minw) * (1.0 - ax));
1302         w = minw;
1303      }
1304    if (h < minh)
1305      {
1306         y = y + ((h - minh) * (1.0 - ay));
1307         h = minh;
1308      }
1309
1310    xx = x;
1311    yy = y;
1312
1313    Evas_Coord cw = 0, ch = 0, cmaxh = 0, obj_index = 0;
1314
1315    EINA_LIST_FOREACH(priv->children, l, opt)
1316      {
1317         Evas_Coord mnw, mnh, mxw, mxh;
1318         double wx, wy;
1319         int fw, fh;
1320
1321         obj = opt->obj;
1322         evas_object_size_hint_align_get(obj, &ax, &ay);
1323         evas_object_size_hint_weight_get(obj, &wx, &wy);
1324         evas_object_size_hint_min_get(obj, &mnw, &mnh);
1325         evas_object_size_hint_max_get(obj, &mxw, &mxh);
1326         fw = fh = 0;
1327         if (ax == -1.0) {fw = 1; ax = 0.5;}
1328         if (ay == -1.0) {fh = 1; ay = 0.5;}
1329         Evas_Coord ww, hh, ow, oh;
1330
1331         if (wx)
1332           {
1333              if ((elm_widget_is(obj)) && (!(strcmp(elm_widget_type_get(obj), "entry"))) && (mnw == -1))
1334                mnw = MIN_W_ENTRY;
1335
1336              if (mnw != -1 && (w - cw) >= mnw)
1337                ww = w - cw;
1338              else
1339                ww = w;
1340           }
1341         else
1342           ww = mnw;
1343         hh = _calculate_item_max_height(o, priv, obj_index);
1344
1345         ow = mnw;
1346         if (fw) ow = ww;
1347         if ((mxw >= 0) && (mxw < ow)) ow = mxw;
1348         oh = mnh;
1349         if (fh) oh = hh;
1350         if ((mxh >= 0) && (mxh < oh)) oh = mxh;
1351
1352         if ((cw + ww) > w)
1353           {
1354              ch += cmaxh;
1355              cw = 0;
1356              cmaxh = 0;
1357           }
1358
1359         evas_object_move(obj,
1360                          xx + cw + (Evas_Coord)(((double)(ww - ow)) * ax),
1361                          yy + ch + (Evas_Coord)(((double)(hh - oh)) * ay));
1362         evas_object_resize(obj, ow, oh);
1363
1364         cw += ww;
1365         if (cmaxh < hh) cmaxh = hh;
1366
1367         obj_index++;
1368      }
1369 }
1370
1371 static void
1372 _item_text_set_hook(Elm_Object_Item *it, const char *part, const char *label)
1373 {
1374    ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
1375
1376    Elm_Multibuttonentry_Item *item;
1377    if (part && strcmp(part, "default")) return;
1378    if (!label) return;
1379    item = (Elm_Multibuttonentry_Item *) it;
1380    edje_object_part_text_set(item->button, "elm.btn.text", label);
1381    _resize_button(item->button, &item->rw, &item->vw);
1382 }
1383
1384 static const char *
1385 _item_text_get_hook(const Elm_Object_Item *it, const char *part)
1386 {
1387    ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
1388    Elm_Multibuttonentry_Item *item;
1389    if (part && strcmp(part, "default")) return NULL;
1390    item = (Elm_Multibuttonentry_Item *) it;
1391    return edje_object_part_text_get(item->button, "elm.btn.text");
1392 }
1393
1394 static void
1395 _text_set_hook(Evas_Object *obj, const char *part, const char *label)
1396 {
1397    ELM_CHECK_WIDTYPE(obj, widtype);
1398    if (part && strcmp(part, "default")) return;
1399    if (label) _set_label(obj, label);
1400    else  _set_label(obj, "");
1401 }
1402
1403 static const char *
1404 _text_get_hook(const Evas_Object *obj, const char *part)
1405 {
1406    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1407    Widget_Data *wd;
1408    if (part && strcmp(part, "default")) return NULL;
1409    wd = elm_widget_data_get(obj);
1410    if (!wd) return NULL;
1411    if (wd->label) return edje_object_part_text_get(wd->label, "mbe.label");
1412    return NULL;
1413 }
1414
1415 EAPI Evas_Object *
1416 elm_multibuttonentry_add(Evas_Object *parent)
1417 {
1418    Evas_Object *obj;
1419    Evas *e;
1420    Widget_Data *wd;
1421
1422    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
1423
1424    ELM_SET_WIDTYPE(widtype, "multibuttonentry");
1425    elm_widget_type_set(obj, "multibuttonentry");
1426    elm_widget_sub_object_add(parent, obj);
1427    elm_widget_data_set(obj, wd);
1428
1429    elm_widget_del_hook_set(obj, _del_hook);
1430    elm_widget_theme_hook_set(obj, _theme_hook);
1431    elm_widget_event_hook_set(obj, _event_hook);
1432    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
1433    elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
1434    elm_widget_text_set_hook_set(obj, _text_set_hook);
1435    elm_widget_text_get_hook_set(obj, _text_get_hook);
1436
1437    wd->base = edje_object_add(e);
1438    _elm_theme_object_set(obj, wd->base, "multibuttonentry", "base", "default");
1439    elm_widget_resize_object_set(obj, wd->base);
1440    elm_widget_can_focus_set(obj, EINA_TRUE);
1441
1442    wd->view_state = MULTIBUTTONENTRY_VIEW_NONE;
1443    wd->focused = EINA_FALSE;
1444    wd->last_btn_select = EINA_TRUE;
1445    wd->n_str = 0;
1446    wd->rect_for_end= NULL;
1447    wd->add_callback = NULL;
1448    wd->add_callback_data = NULL;
1449
1450    _view_init(obj);
1451    _event_init(obj);
1452
1453    return obj;
1454 }
1455
1456 EAPI Evas_Object *
1457 elm_multibuttonentry_entry_get(const Evas_Object *obj)
1458 {
1459    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1460    Widget_Data *wd = elm_widget_data_get(obj);
1461
1462    if (!wd) return NULL;
1463
1464    return wd->entry;
1465 }
1466
1467 EAPI const char *
1468 elm_multibuttonentry_label_get(const Evas_Object *obj)
1469 {
1470    return _text_get_hook(obj, NULL);
1471 }
1472
1473 EAPI void
1474 elm_multibuttonentry_label_set(Evas_Object *obj, const char *label)
1475 {
1476    _text_set_hook(obj, NULL, label);
1477 }
1478
1479 EAPI const char *
1480 elm_multibuttonentry_guide_text_get(const Evas_Object *obj)
1481 {
1482    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1483    Widget_Data *wd = elm_widget_data_get(obj);
1484
1485    if (!wd) return NULL;
1486    if (wd->guidetext) return edje_object_part_text_get(wd->guidetext, "elm.text");
1487    return NULL;
1488 }
1489
1490 EAPI void
1491 elm_multibuttonentry_guide_text_set(Evas_Object *obj, const char *guidetext)
1492 {
1493    ELM_CHECK_WIDTYPE(obj, widtype);
1494    Widget_Data *wd = elm_widget_data_get(obj);
1495
1496    if (!wd) return;
1497    if (guidetext)
1498      _set_guidetext(obj, guidetext);
1499    else
1500      _set_guidetext(obj, "");
1501 }
1502
1503 EAPI int
1504 elm_multibuttonentry_shrink_mode_get(const Evas_Object *obj)
1505 {
1506    ELM_CHECK_WIDTYPE(obj, widtype) -1;
1507    Widget_Data *wd = elm_widget_data_get(obj);
1508
1509    if (!wd) return -1;
1510    return wd->view_state == MULTIBUTTONENTRY_VIEW_SHRINK ? 1 : 0;
1511 }
1512
1513 EAPI void
1514 elm_multibuttonentry_shrink_mode_set(Evas_Object *obj, int shrink)
1515 {
1516    ELM_CHECK_WIDTYPE(obj, widtype);
1517    Widget_Data *wd = elm_widget_data_get(obj);
1518
1519    if (!wd || !wd->box ||
1520        ((wd->view_state == MULTIBUTTONENTRY_VIEW_SHRINK) ? 1 : 0) == shrink) return;
1521    _shrink_mode_set(obj, shrink);
1522 }
1523
1524 EAPI Elm_Object_Item *
1525 elm_multibuttonentry_item_prepend(Evas_Object *obj, const char *label, void *data)
1526 {
1527    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1528    Elm_Multibuttonentry_Item *item;
1529    if (!label) return NULL;
1530    item = _add_button_item(obj, label, MULTIBUTTONENTRY_POS_START, NULL, data);
1531    return (Elm_Object_Item *) item;
1532 }
1533
1534 EAPI Elm_Object_Item *
1535 elm_multibuttonentry_item_append(Evas_Object *obj, const char *label, void *data)
1536 {
1537    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1538    Elm_Multibuttonentry_Item *item;
1539    if (!label) return NULL;
1540    item = _add_button_item(obj, label, MULTIBUTTONENTRY_POS_END, NULL, data);
1541    return (Elm_Object_Item *) item;
1542 }
1543
1544 EAPI Elm_Object_Item *
1545 elm_multibuttonentry_item_insert_before(Evas_Object *obj, Elm_Object_Item *before, const char *label, void *data)
1546 {
1547    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1548    Elm_Multibuttonentry_Item *item;
1549    if (!label) return NULL;
1550    item = _add_button_item(obj, label, MULTIBUTTONENTRY_POS_BEFORE,
1551                            (Elm_Multibuttonentry_Item *) before, data);
1552    return (Elm_Object_Item *) item;
1553 }
1554
1555 EAPI Elm_Object_Item *
1556 elm_multibuttonentry_item_insert_after(Evas_Object *obj, Elm_Object_Item *after, const char *label, void *data)
1557 {
1558    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1559    Elm_Multibuttonentry_Item *item;
1560    if (!label) return NULL;
1561    item = _add_button_item(obj, label, MULTIBUTTONENTRY_POS_AFTER,
1562                            (Elm_Multibuttonentry_Item *) after, data);
1563    return (Elm_Object_Item *) item;
1564 }
1565
1566 EAPI const Eina_List *
1567 elm_multibuttonentry_items_get(const Evas_Object *obj)
1568 {
1569    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1570    Widget_Data *wd = elm_widget_data_get(obj);
1571    if (!wd) return NULL;
1572    return wd->items;
1573 }
1574
1575 EAPI Elm_Object_Item *
1576 elm_multibuttonentry_first_item_get(const Evas_Object *obj)
1577 {
1578    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1579    Widget_Data *wd = elm_widget_data_get(obj);
1580    if (!wd) return NULL;
1581    return eina_list_data_get(wd->items);
1582 }
1583
1584 EAPI Elm_Object_Item *
1585 elm_multibuttonentry_last_item_get(const Evas_Object *obj)
1586 {
1587    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1588    Widget_Data *wd = elm_widget_data_get(obj);
1589    if (!wd) return NULL;
1590    return eina_list_data_get(eina_list_last(wd->items));
1591 }
1592
1593 EAPI Elm_Object_Item *
1594 elm_multibuttonentry_selected_item_get(const Evas_Object *obj)
1595 {
1596    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1597    Widget_Data *wd = elm_widget_data_get(obj);
1598    if (!wd) return NULL;
1599    return eina_list_data_get(wd->current);
1600 }
1601
1602 EAPI void
1603 elm_multibuttonentry_item_select(Elm_Object_Item *it, Eina_Bool selected)
1604 {
1605    ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
1606    Elm_Multibuttonentry_Item *item = (Elm_Multibuttonentry_Item *) it;
1607    if (selected) _select_button(WIDGET(item), item->button);
1608    else _select_button(WIDGET(item), NULL);
1609 }
1610
1611 EAPI void
1612 elm_multibuttonentry_item_unselect_all(Evas_Object *obj)
1613 {
1614    ELM_CHECK_WIDTYPE(obj, widtype);
1615    Widget_Data *wd = elm_widget_data_get(obj);
1616    if (!wd) return;
1617    _select_button(obj, NULL);
1618 }
1619
1620 EAPI void
1621 elm_multibuttonentry_clear(Evas_Object *obj)
1622 {
1623    ELM_CHECK_WIDTYPE(obj, widtype);
1624    Elm_Multibuttonentry_Item *item;
1625    Widget_Data *wd = elm_widget_data_get(obj);
1626    if (!wd) return;
1627
1628    if (wd->items)
1629      {
1630         EINA_LIST_FREE(wd->items, item)
1631           {
1632              elm_box_unpack(wd->box, item->button);
1633              _del_button_obj(obj, item->button);
1634              free(item);
1635           }
1636         wd->items = NULL;
1637      }
1638    wd->current = NULL;
1639    _view_update(obj);
1640 }
1641
1642 EAPI void
1643 elm_multibuttonentry_item_del(Elm_Object_Item *it)
1644 {
1645    elm_object_item_del(it);
1646 }
1647
1648 EAPI const char *
1649 elm_multibuttonentry_item_label_get(const Elm_Object_Item *it)
1650 {
1651    return _item_text_get_hook(it, NULL);
1652 }
1653
1654 EAPI void
1655 elm_multibuttonentry_item_label_set(Elm_Object_Item *it, const char *str)
1656 {
1657    _item_text_set_hook(it, NULL, str);
1658 }
1659
1660 EAPI Elm_Object_Item *
1661 elm_multibuttonentry_item_prev_get(const Elm_Object_Item *it)
1662 {
1663    ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
1664    Widget_Data *wd;
1665    Eina_List *l;
1666    Elm_Multibuttonentry_Item *_item;
1667
1668    wd = elm_widget_data_get(WIDGET(it));
1669    if (!wd) return NULL;
1670
1671    EINA_LIST_FOREACH(wd->items, l, _item)
1672      {
1673         if (_item == (Elm_Multibuttonentry_Item *) it)
1674           {
1675              l = eina_list_prev(l);
1676              if (!l) return NULL;
1677              return eina_list_data_get(l);
1678           }
1679      }
1680    return NULL;
1681 }
1682
1683 EAPI Elm_Object_Item *
1684 elm_multibuttonentry_item_next_get(const Elm_Object_Item *it)
1685 {
1686    ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
1687    Widget_Data *wd;
1688    Eina_List *l;
1689    Elm_Multibuttonentry_Item *_item;
1690    wd = elm_widget_data_get(WIDGET(it));
1691    if (!wd) return NULL;
1692
1693    EINA_LIST_FOREACH(wd->items, l, _item)
1694      {
1695         if (_item == (Elm_Multibuttonentry_Item *) it)
1696           {
1697              l = eina_list_next(l);
1698              if (!l) return NULL;
1699              return eina_list_data_get(l);
1700           }
1701      }
1702    return NULL;
1703 }
1704
1705 EAPI void *
1706 elm_multibuttonentry_item_data_get(const Elm_Object_Item *it)
1707 {
1708    ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
1709    return elm_widget_item_data_get(it);
1710 }
1711
1712 EAPI void
1713 elm_multibuttonentry_item_data_set(Elm_Object_Item *it, void *data)
1714 {
1715    ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
1716    return elm_widget_item_data_set(it, data);
1717 }
1718
1719 EAPI void
1720 elm_multibuttonentry_item_filter_append(Evas_Object *obj, Elm_Multibuttonentry_Item_Filter_callback func, void *data)
1721 {
1722    Elm_Multibuttonentry_Item_Filter *new_item_filter = NULL;
1723    Elm_Multibuttonentry_Item_Filter *_item_filter = NULL;
1724    Eina_List *l;
1725
1726    Widget_Data *wd = elm_widget_data_get(obj);
1727    if (!wd) return;
1728
1729    ELM_CHECK_WIDTYPE(obj, widtype);
1730    EINA_SAFETY_ON_NULL_RETURN(func);
1731
1732    new_item_filter= _filter_new(func, data);
1733    if (!new_item_filter) return;
1734
1735    EINA_LIST_FOREACH(wd->filter_list, l, _item_filter)
1736      {
1737         if ( _item_filter && ((_item_filter->callback_func == func) && (_item_filter->data == data)))
1738           {
1739              printf("Already Registered this item filter!!!!\n");
1740              return;
1741           }
1742      }
1743    wd->filter_list = eina_list_append(wd->filter_list, new_item_filter);
1744 }
1745
1746 EAPI void
1747 elm_multibuttonentry_item_filter_prepend(Evas_Object *obj, Elm_Multibuttonentry_Item_Filter_callback func, void *data)
1748 {
1749    Elm_Multibuttonentry_Item_Filter *new_item_filter = NULL;
1750    Elm_Multibuttonentry_Item_Filter *_item_filter = NULL;
1751    Eina_List *l;
1752    Widget_Data *wd = elm_widget_data_get(obj);
1753    if (!wd) return;
1754
1755    ELM_CHECK_WIDTYPE(obj, widtype);
1756    EINA_SAFETY_ON_NULL_RETURN(func);
1757
1758    new_item_filter = _filter_new(func, data);
1759    if (!new_item_filter) return;
1760
1761    EINA_LIST_FOREACH(wd->filter_list, l, _item_filter)
1762      {
1763         if (_item_filter && ((_item_filter->callback_func == func) && (_item_filter->data == data)))
1764           {
1765              printf("Already Registered this item filter!!!!\n");
1766              return;
1767           }
1768      }
1769    wd->filter_list = eina_list_prepend(wd->filter_list, new_item_filter);
1770 }
1771
1772 EAPI void
1773 elm_multibuttonentry_item_filter_remove(Evas_Object *obj, Elm_Multibuttonentry_Item_Filter_callback func, void *data)
1774 {
1775    Widget_Data *wd;
1776    Eina_List *l;
1777    Elm_Multibuttonentry_Item_Filter *item_filter;
1778
1779    wd = elm_widget_data_get(obj);
1780
1781    EINA_SAFETY_ON_NULL_RETURN(func);
1782
1783    EINA_LIST_FOREACH(wd->filter_list, l, item_filter)
1784      {
1785         if ((item_filter->callback_func == func) && ((!data) || (item_filter->data == data)))
1786           {
1787              wd->filter_list = eina_list_remove_list(wd->filter_list, l);
1788              _filter_free(item_filter);
1789              return;
1790           }
1791      }
1792 }
1793
1794 EAPI void
1795 elm_multibuttonentry_view_mode(Evas_Object *obj, Eina_Bool view_mode)
1796 {
1797    ELM_CHECK_WIDTYPE(obj, widtype);
1798    Widget_Data *wd = elm_widget_data_get(obj);
1799    wd->view_mode = view_mode;
1800
1801    if (view_mode)
1802      {
1803         elm_box_unpack(wd->box, wd->entry);
1804         evas_object_hide(wd->entry);
1805      }
1806    else
1807      _view_update(obj);
1808 }