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