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