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