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