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