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