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