elementary/multibuttonentry - deprecated APIs. Now, let's use elm_common APIs.
[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              free(_item);
696              if (wd->current == l)
697                wd->current = NULL;
698              break;
699           }
700      }
701    if (wd->view_state == MULTIBUTTONENTRY_VIEW_SHRINK)
702      _shrink_mode_set(obj, 1);
703
704    if (!eina_list_count(wd->items))
705      _set_vis_guidetext(obj);
706 }
707
708 static void
709 _select_button(Evas_Object *obj, Evas_Object *btn)
710 {
711    Widget_Data *wd = elm_widget_data_get(obj);
712
713    if (!wd) return;
714    if (btn)
715      {
716         _change_current_button(obj, btn);
717         if (elm_widget_focus_get(obj))
718           {
719              elm_object_focus_set(wd->entry, EINA_FALSE);
720              evas_object_focus_set(btn, EINA_TRUE);
721           }
722      }
723    else
724      {
725         _change_current_button_state(obj, MULTIBUTTONENTRY_BUTTON_STATE_DEFAULT);
726         if (elm_widget_focus_get(obj))
727           elm_object_focus_set(wd->entry, EINA_TRUE);
728      }
729 }
730
731 static void
732 _resize_button(Evas_Object *btn, Evas_Coord *realw, Evas_Coord *vieww)
733 {
734    Evas_Coord rw, vw;
735    Evas_Coord w_text, h_btn, padding_outer, padding_inner = 0;
736    Evas_Coord w_btn = 0, button_max_width = 0;
737    const char *size_str;
738
739    size_str = edje_object_data_get(btn, "button_max_size");
740    if (size_str) button_max_width = (Evas_Coord)atoi(size_str);
741
742    // decide the size of button
743    edje_object_part_geometry_get(btn, "elm.base", NULL, NULL, NULL, &h_btn);
744    edje_object_part_geometry_get(btn, "elm.btn.text", NULL, NULL, &w_text, NULL);
745    edje_object_part_geometry_get(btn, "right.padding", NULL, NULL, &padding_outer, NULL);
746    w_btn = w_text + 2*padding_outer + 2*padding_inner;
747
748    rw = w_btn;
749
750    if (button_max_width < w_btn)
751      vw = button_max_width;
752    else
753      vw = w_btn;
754
755    //resize btn
756    evas_object_resize(btn, vw, h_btn);
757    evas_object_size_hint_min_set(btn, vw, h_btn);
758
759    if (realw) *realw = rw;
760    if (vieww) *vieww = vw;
761 }
762
763 static Elm_Multibuttonentry_Item*
764 _add_button_item(Evas_Object *obj, const char *str, Multibuttonentry_Pos pos, const Elm_Multibuttonentry_Item *reference, void *data)
765 {
766    Elm_Multibuttonentry_Item *item;
767    Elm_Multibuttonentry_Item_Filter *item_filter;
768    Eina_List *l;
769    Evas_Object *btn;
770    Evas_Coord width = -1, height = -1;
771    char *str_utf8 = NULL;
772    Widget_Data *wd = elm_widget_data_get(obj);
773
774    if (!wd || !wd->box || !wd->entry) return NULL;
775
776    EINA_LIST_FOREACH(wd->filter_list, l, item_filter)
777      {
778         if (!(item_filter->callback_func(obj, str, data, item_filter->data)))
779           return NULL;
780      }
781    // add button
782    btn = edje_object_add(evas_object_evas_get(obj));
783    str_utf8 = elm_entry_markup_to_utf8(str);
784
785    //entry is cleared when text is made to button
786    elm_object_text_set(wd->entry, "");
787
788    _elm_theme_object_set(obj, btn, "multibuttonentry", "btn", elm_widget_style_get(obj));
789    edje_object_part_text_set(btn, "elm.btn.text", str_utf8);
790    edje_object_part_geometry_get(btn, "elm.btn.text", NULL, NULL, &width, &height);
791
792    evas_object_size_hint_min_set(btn, width, height);
793
794    edje_object_signal_callback_add(btn, "mouse,clicked,1", "*", _button_clicked, obj);
795    evas_object_size_hint_weight_set(btn, 0.0, 0.0);
796    evas_object_show(btn);
797
798    // append item list
799    item = elm_widget_item_new(obj, Elm_Multibuttonentry_Item);
800    if (item)
801      {
802         elm_widget_item_text_set_hook_set(item, _item_text_set_hook);
803         elm_widget_item_text_get_hook_set(item, _item_text_get_hook);
804         elm_widget_item_data_set(item, data);
805         Evas_Coord rw, vw;
806         _resize_button(btn, &rw, &vw);
807         item->button = btn;
808         item->rw = rw;
809         item->vw = vw;
810         item->visible = EINA_TRUE;
811
812         switch (pos)
813           {
814              case MULTIBUTTONENTRY_POS_START:
815                 wd->items = eina_list_prepend(wd->items, item);
816                 if (wd->view_state == MULTIBUTTONENTRY_VIEW_SHRINK)
817                   {
818                      elm_widget_sub_object_add(obj, btn);
819                      _shrink_mode_set(obj, 1);
820                   }
821                 else
822                   {
823                      if (wd->label)
824                        elm_box_pack_after(wd->box, btn, wd->label);
825                      else
826                        elm_box_pack_start(wd->box, btn);
827                      if (wd->view_state == MULTIBUTTONENTRY_VIEW_GUIDETEXT)
828                        _set_vis_guidetext(obj);
829                   }
830                 break;
831              case MULTIBUTTONENTRY_POS_END:
832                 wd->items = eina_list_append(wd->items, item);
833                 if (wd->view_state == MULTIBUTTONENTRY_VIEW_SHRINK)
834                   {
835                      elm_widget_sub_object_add(obj, btn);
836                      evas_object_hide(btn);
837                   }
838                 else
839                   {
840                      if (wd->view_state == MULTIBUTTONENTRY_VIEW_GUIDETEXT)
841                        _set_vis_guidetext(obj);
842                      if (wd->entry)
843                        elm_box_pack_before(wd->box, btn, wd->entry);
844                      else
845                        elm_box_pack_end(wd->box, btn);
846                   }
847                 break;
848              case MULTIBUTTONENTRY_POS_BEFORE:
849                 if (reference)
850                      wd->items = eina_list_prepend_relative(wd->items, item, reference);
851                 else
852                      wd->items = eina_list_append(wd->items, item);
853                 if (wd->view_state == MULTIBUTTONENTRY_VIEW_SHRINK)
854                   {
855                      elm_widget_sub_object_add(obj, btn);
856                      evas_object_hide(btn);
857                      _shrink_mode_set(obj, 1);
858                   }
859                 else
860                   {
861                      if (reference)
862                        elm_box_pack_before(wd->box, btn, reference->button);
863                      else
864                        {
865                           if (wd->view_state == MULTIBUTTONENTRY_VIEW_GUIDETEXT)
866                             _set_vis_guidetext(obj);
867                           if (wd->entry)
868                             elm_box_pack_before(wd->box, btn, wd->entry);
869                           else
870                             elm_box_pack_end(wd->box, btn);
871                        }
872                   }
873                 break;
874              case MULTIBUTTONENTRY_POS_AFTER:
875                 if (reference)
876                      wd->items = eina_list_append_relative(wd->items, item, reference);
877                 else
878                      wd->items = eina_list_append(wd->items, item);
879                 if (wd->view_state == MULTIBUTTONENTRY_VIEW_SHRINK)
880                   {
881                      elm_widget_sub_object_add(obj, btn);
882                      _shrink_mode_set(obj, 1);
883                   }
884                 else
885                   {
886                      if (reference)
887                        elm_box_pack_after(wd->box, btn, reference->button);
888                      else
889                        {
890                           if (wd->view_state == MULTIBUTTONENTRY_VIEW_GUIDETEXT)
891                             _set_vis_guidetext(obj);
892                           if (wd->entry)
893                             elm_box_pack_before(wd->box, btn, wd->entry);
894                           else
895                             elm_box_pack_end(wd->box, btn);
896                        }
897                   }
898                 break;
899              default:
900                 break;
901           }
902      }
903    evas_object_smart_callback_call(obj, "item,added", item);
904
905    free(str_utf8);
906
907    return item;
908 }
909
910 static void
911 _add_button(Evas_Object *obj, const char *str)
912 {
913    Widget_Data *wd = elm_widget_data_get(obj);
914    if (!wd) return;
915
916    _add_button_item(obj, str, MULTIBUTTONENTRY_POS_END, NULL, NULL);
917 }
918
919 static Elm_Multibuttonentry_Item_Filter*
920 _filter_new(Elm_Multibuttonentry_Item_Filter_callback func, void *data)
921 {
922    Elm_Multibuttonentry_Item_Filter *item_filter = ELM_NEW(Elm_Multibuttonentry_Item_Filter);
923    if (!item_filter) return NULL;
924
925    item_filter->callback_func= func;
926    item_filter->data = data;
927
928    return item_filter;
929 }
930
931 static void
932 _filter_free(Elm_Multibuttonentry_Item_Filter *item_filter)
933 {
934    free(item_filter);
935 }
936
937 static void
938 _evas_mbe_key_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
939 {
940    Widget_Data *wd = elm_widget_data_get(data);
941    Elm_Multibuttonentry_Item *item = NULL;
942
943    if (!wd || !wd->base || !wd->box) return;
944
945    Evas_Event_Key_Up *ev = (Evas_Event_Key_Up*)event_info;
946
947    if (wd->last_btn_select)
948      {
949         if (wd->current &&
950             ((strcmp(ev->keyname, "BackSpace") == 0) ||
951              (strcmp(ev->keyname, "BackSpace (") == 0)))
952           {
953              item = eina_list_data_get(wd->current);
954              if (item)
955                {
956                   _del_button_item(item);
957                   elm_object_focus_set(wd->entry, EINA_TRUE);
958                }
959           }
960         else if (((!wd->current && (wd->n_str == 0) &&
961                    (strcmp(ev->keyname, "BackSpace") == 0)) ||
962                   (strcmp(ev->keyname, "BackSpace (") == 0)))
963           {
964              item = eina_list_data_get(eina_list_last(wd->items));
965              if (item)
966                _select_button(data, item->button);
967           }
968      }
969    else
970      wd->last_btn_select = EINA_TRUE;
971 }
972
973 static void
974 _entry_key_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
975 {
976    Widget_Data *wd = elm_widget_data_get(data);
977    Evas_Event_Key_Down *ev = (Evas_Event_Key_Down *)event_info;
978
979    if (!wd) return;
980
981    if ((wd->n_str == 1) && (strcmp(ev->keyname, "BackSpace") == 0 || (strcmp(ev->keyname, "BackSpace (") == 0 )))
982      wd->last_btn_select = EINA_FALSE;
983 }
984
985 static void
986 _entry_key_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
987 {
988    Widget_Data *wd = elm_widget_data_get(data);
989    Evas_Event_Key_Up *ev = (Evas_Event_Key_Up *) event_info;
990    const char *str;
991
992    if (!wd || !wd->base || !wd->box) return;
993
994    str = elm_object_text_get(wd->entry);
995
996    if ((strcmp(str, "") != 0) && (strcmp(ev->keyname, "KP_Enter") == 0 || strcmp(ev->keyname, "Return") == 0 ))
997      {
998         _add_button(data, str);
999         wd->n_str = 0;
1000      }
1001 }
1002
1003 static void
1004 _entry_clicked_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
1005 {
1006    Widget_Data *wd = elm_widget_data_get(data);
1007    if (!wd) return;
1008
1009    _change_current_button_state(data, MULTIBUTTONENTRY_BUTTON_STATE_DEFAULT);
1010    elm_object_focus_set(wd->entry, EINA_TRUE);
1011 }
1012
1013 static void
1014 _entry_focus_in_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
1015 {
1016    Widget_Data *wd = elm_widget_data_get(data);
1017    Elm_Multibuttonentry_Item *item = NULL;
1018
1019    if (!wd) return;
1020
1021    if (wd->current)
1022      {
1023         item = eina_list_data_get(wd->current);
1024         elm_object_focus_set(wd->entry, EINA_FALSE);
1025         evas_object_focus_set(item->button, EINA_TRUE);
1026      }
1027 }
1028
1029 static void
1030 _entry_focus_out_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
1031 {
1032    Widget_Data *wd = elm_widget_data_get(data);
1033    const char *str;
1034
1035    if (!wd) return;
1036
1037    str = elm_object_text_get(wd->entry);
1038    if (strlen(str))
1039      _add_button(data, str);
1040 }
1041
1042 static void
1043 _entry_changed_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
1044 {
1045    Widget_Data *wd = elm_widget_data_get(data);
1046    const char *str;
1047
1048    if (!wd) return;
1049
1050    str = elm_object_text_get(wd->entry);
1051    wd->n_str = strlen(str);
1052 }
1053
1054 static void
1055 _entry_resized_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
1056 {
1057    Evas_Coord en_x, en_y, en_w, en_h;
1058    Evas_Coord bx_x, bx_y;
1059
1060    Widget_Data *wd = elm_widget_data_get(data);
1061    if (!wd) return;
1062
1063    evas_object_geometry_get(wd->entry, &en_x, &en_y, &en_w, &en_h);
1064    evas_object_geometry_get(wd->box, &bx_x, &bx_y, NULL, NULL);
1065
1066    elm_widget_show_region_set(wd->box, en_x - bx_x, en_y - bx_y, en_w, en_h, EINA_TRUE);
1067 }
1068
1069 static void
1070 _view_init(Evas_Object *obj)
1071 {
1072    Widget_Data *wd = elm_widget_data_get(obj);
1073
1074    if (!wd) return;
1075
1076    if (!wd->box)
1077      {
1078         wd->box = elm_box_add (obj);
1079         if (!wd->box) return;
1080         elm_widget_sub_object_add(obj, wd->box);
1081         elm_box_layout_set(wd->box, _box_layout_cb, NULL, NULL);
1082         elm_box_homogeneous_set(wd->box, EINA_FALSE);
1083         edje_object_part_swallow(wd->base, "box.swallow", wd->box);
1084      }
1085    if (!wd->label)
1086      {
1087         wd->label = edje_object_add(evas_object_evas_get(obj));
1088         if (!wd->label) return;
1089         _elm_theme_object_set(obj, wd->label, "multibuttonentry", "label", elm_widget_style_get(obj));
1090         _set_label(obj, "");
1091         elm_widget_sub_object_add(obj, wd->label);
1092      }
1093
1094    if (!wd->entry)
1095      {
1096         wd->entry = elm_entry_add (obj);
1097         if (!wd->entry) return;
1098         elm_entry_single_line_set(wd->entry, EINA_TRUE);
1099         elm_object_text_set(wd->entry, "");
1100         elm_entry_cursor_end_set(wd->entry);
1101         evas_object_size_hint_min_set(wd->entry, MIN_W_ENTRY, 0);
1102         evas_object_size_hint_weight_set(wd->entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1103         evas_object_size_hint_align_set(wd->entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
1104         if (wd->box) elm_box_pack_end (wd->box, wd->entry);
1105         evas_object_show(wd->entry);
1106         wd->view_state = MULTIBUTTONENTRY_VIEW_ENTRY;
1107      }
1108
1109    if (!wd->end)
1110      {
1111         const char *end_type;
1112
1113         end_type = edje_object_data_get(wd->base, "closed_button_type");
1114         if (!end_type || !strcmp(end_type, "label"))
1115           {
1116              wd->end = elm_label_add (obj);
1117              if (!wd->end) return;
1118              elm_object_style_set(wd->end, "extended/multibuttonentry_default");
1119              wd->end_type = MULTIBUTTONENTRY_CLOSED_LABEL;
1120           }
1121         else
1122           {
1123              const char *size_str;
1124              wd->end = edje_object_add(evas_object_evas_get(obj));
1125              if (!wd->end) return;
1126              _elm_theme_object_set(obj, wd->end, "multibuttonentry", "closedbutton", elm_widget_style_get(obj));
1127              Evas_Coord button_min_width = 0;
1128              Evas_Coord button_min_height = 0;
1129
1130              size_str = edje_object_data_get(wd->end, "closed_button_width");
1131              if (size_str) button_min_width = (Evas_Coord)atoi(size_str);
1132              size_str = edje_object_data_get(wd->end, "closed_button_height");
1133              if (size_str) button_min_height = (Evas_Coord)atoi(size_str);
1134
1135              wd->end_type = MULTIBUTTONENTRY_CLOSED_IMAGE;
1136              evas_object_size_hint_min_set(wd->end, button_min_width * elm_scale_get(), button_min_height * elm_scale_get());
1137              elm_widget_sub_object_add(obj, wd->end);
1138           }
1139      }
1140 }
1141
1142 static void
1143 _calculate_box_min_size(Evas_Object *box, Evas_Object_Box_Data *priv)
1144 {
1145    Evas_Coord minw, minh, mnw, mnh, ww;
1146    Evas_Coord w, cw = 0, cmaxh = 0;
1147    const Eina_List *l;
1148    Evas_Object_Box_Option *opt;
1149    double wx;
1150
1151    /* FIXME: need to calc max */
1152    minw = 0;
1153    minh = 0;
1154
1155    evas_object_geometry_get(box, NULL, NULL, &w, NULL);
1156    evas_object_size_hint_min_get(box, &minw, NULL);
1157
1158    EINA_LIST_FOREACH(priv->children, l, opt)
1159      {
1160         evas_object_size_hint_min_get(opt->obj, &mnw, &mnh);
1161         evas_object_size_hint_weight_get(opt->obj, &wx, NULL);
1162
1163         if (wx)
1164           {
1165              if (mnw != -1 && (w - cw) >= mnw)
1166                ww = w - cw;
1167              else
1168                ww = w;
1169           }
1170         else
1171            ww = mnw;
1172
1173         if ((cw + mnw) > w)
1174           {
1175              minh += cmaxh;
1176              cw = 0;
1177              cmaxh = 0;
1178           }
1179         cw += ww;
1180         if (cmaxh < mnh) cmaxh = mnh;
1181      }
1182
1183    minh += cmaxh;
1184
1185    evas_object_size_hint_min_set(box, minw, minh);
1186 }
1187
1188 static Evas_Coord
1189 _calculate_item_max_height(Evas_Object *box, Evas_Object_Box_Data *priv, int obj_index)
1190 {
1191    Evas_Coord mnw, mnh, cw = 0, cmaxh = 0, w, ww;
1192    const Eina_List *l;
1193    Evas_Object_Box_Option *opt;
1194    int index = 0;
1195    double wx;
1196
1197    evas_object_geometry_get(box, NULL, NULL, &w, NULL);
1198
1199    EINA_LIST_FOREACH(priv->children, l, opt)
1200      {
1201         evas_object_size_hint_min_get(opt->obj, &mnw, &mnh);
1202         evas_object_size_hint_weight_get(opt->obj, &wx, NULL);
1203
1204         if (wx)
1205           {
1206              if (mnw != -1 && (w - cw) >= mnw)
1207                 ww = w - cw;
1208              else
1209                 ww = w;
1210           }
1211         else
1212            ww = mnw;
1213
1214         if ((cw + ww) > w)
1215           {
1216              if (index > obj_index) return cmaxh;
1217              cw = 0;
1218              cmaxh = 0;
1219           }
1220
1221         cw += ww;
1222         if (cmaxh < mnh) cmaxh = mnh;
1223
1224         index++;
1225      }
1226
1227    return cmaxh;
1228 }
1229
1230 static void
1231 _box_layout_cb(Evas_Object *o, Evas_Object_Box_Data *priv, void *data __UNUSED__)
1232 {
1233    Evas_Coord x, y, w, h, xx, yy;
1234    const Eina_List *l;
1235    Evas_Object *obj;
1236    Evas_Coord minw, minh;
1237    double ax, ay;
1238    Evas_Object_Box_Option *opt;
1239
1240    _calculate_box_min_size(o, priv);
1241
1242    evas_object_geometry_get(o, &x, &y, &w, &h);
1243
1244    evas_object_size_hint_min_get(o, &minw, &minh);
1245    evas_object_size_hint_align_get(o, &ax, &ay);
1246    if (w < minw)
1247      {
1248         x = x + ((w - minw) * (1.0 - ax));
1249         w = minw;
1250      }
1251    if (h < minh)
1252      {
1253         y = y + ((h - minh) * (1.0 - ay));
1254         h = minh;
1255      }
1256
1257    xx = x;
1258    yy = y;
1259
1260    Evas_Coord cw = 0, ch = 0, cmaxh = 0, obj_index = 0;
1261
1262    EINA_LIST_FOREACH(priv->children, l, opt)
1263      {
1264         Evas_Coord mnw, mnh, mxw, mxh;
1265         double wx, wy;
1266         int fw, fh;
1267
1268         obj = opt->obj;
1269         evas_object_size_hint_align_get(obj, &ax, &ay);
1270         evas_object_size_hint_weight_get(obj, &wx, &wy);
1271         evas_object_size_hint_min_get(obj, &mnw, &mnh);
1272         evas_object_size_hint_max_get(obj, &mxw, &mxh);
1273         fw = fh = 0;
1274         if (ax == -1.0) {fw = 1; ax = 0.5;}
1275         if (ay == -1.0) {fh = 1; ay = 0.5;}
1276         Evas_Coord ww, hh, ow, oh;
1277
1278         if (wx)
1279           {
1280              if (mnw != -1 && (w - cw) >= mnw)
1281                 ww = w - cw;
1282              else
1283                 ww = w;
1284           }
1285         else
1286            ww = mnw;
1287         hh = _calculate_item_max_height(o, priv, obj_index);
1288
1289         ow = mnw;
1290         if (fw) ow = ww;
1291         if ((mxw >= 0) && (mxw < ow)) ow = mxw;
1292         oh = mnh;
1293         if (fh) oh = hh;
1294         if ((mxh >= 0) && (mxh < oh)) oh = mxh;
1295
1296         if ((cw + ww) > w)
1297           {
1298              ch += cmaxh;
1299              cw = 0;
1300              cmaxh = 0;
1301           }
1302
1303         evas_object_move(obj,
1304                          xx + cw + (Evas_Coord)(((double)(ww - ow)) * ax),
1305                          yy + ch + (Evas_Coord)(((double)(hh - oh)) * ay));
1306         evas_object_resize(obj, ow, oh);
1307
1308         cw += ww;
1309         if (cmaxh < hh) cmaxh = hh;
1310
1311         obj_index++;
1312      }
1313 }
1314
1315 static void
1316 _item_text_set_hook(Elm_Object_Item *it, const char *part, const char *label)
1317 {
1318    ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
1319
1320    Elm_Multibuttonentry_Item *item;
1321    if (part && strcmp(part, "default")) return;
1322    if (!label) return;
1323    item = (Elm_Multibuttonentry_Item *) it;
1324    edje_object_part_text_set(item->button, "elm.btn.text", label);
1325    _resize_button(item->button, &item->rw, &item->vw);
1326 }
1327
1328 static const char *
1329 _item_text_get_hook(const Elm_Object_Item *it, const char *part)
1330 {
1331    ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
1332    Elm_Multibuttonentry_Item *item;
1333    if (part && strcmp(part, "default")) return NULL;
1334    item = (Elm_Multibuttonentry_Item *) it;
1335    return edje_object_part_text_get(item->button, "elm.btn.text");
1336 }
1337
1338 static void
1339 _text_set_hook(Evas_Object *obj, const char *part, const char *label)
1340 {
1341    ELM_CHECK_WIDTYPE(obj, widtype);
1342    if (part && strcmp(part, "default")) return;
1343    if (label) _set_label(obj, label);
1344    else  _set_label(obj, "");
1345 }
1346
1347 static const char *
1348 _text_get_hook(const Evas_Object *obj, const char *part)
1349 {
1350    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1351    Widget_Data *wd;
1352    if (part && strcmp(part, "default")) return NULL;
1353    wd = elm_widget_data_get(obj);
1354    if (!wd) return NULL;
1355    if (wd->label) return edje_object_part_text_get(wd->label, "mbe.label");
1356    return NULL;
1357 }
1358
1359 EAPI Evas_Object *
1360 elm_multibuttonentry_add(Evas_Object *parent)
1361 {
1362    Evas_Object *obj;
1363    Evas *e;
1364    Widget_Data *wd;
1365
1366    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
1367
1368    ELM_SET_WIDTYPE(widtype, "multibuttonentry");
1369    elm_widget_type_set(obj, "multibuttonentry");
1370    elm_widget_sub_object_add(parent, obj);
1371    elm_widget_data_set(obj, wd);
1372
1373    elm_widget_del_hook_set(obj, _del_hook);
1374    elm_widget_theme_hook_set(obj, _theme_hook);
1375    elm_widget_event_hook_set(obj, _event_hook);
1376    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
1377    elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
1378    elm_widget_text_set_hook_set(obj, _text_set_hook);
1379    elm_widget_text_get_hook_set(obj, _text_get_hook);
1380
1381    wd->base = edje_object_add(e);
1382    _elm_theme_object_set(obj, wd->base, "multibuttonentry", "base", "default");
1383    elm_widget_resize_object_set(obj, wd->base);
1384    elm_widget_can_focus_set(obj, EINA_TRUE);
1385
1386    wd->view_state = MULTIBUTTONENTRY_VIEW_NONE;
1387    wd->focused = EINA_FALSE;
1388    wd->last_btn_select = EINA_TRUE;
1389    wd->n_str = 0;
1390    wd->rect_for_end= NULL;
1391    wd->add_callback = NULL;
1392    wd->add_callback_data = NULL;
1393
1394    _view_init(obj);
1395    _event_init(obj);
1396
1397    return obj;
1398 }
1399
1400 EAPI Evas_Object *
1401 elm_multibuttonentry_entry_get(const Evas_Object *obj)
1402 {
1403    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1404    Widget_Data *wd = elm_widget_data_get(obj);
1405
1406    if (!wd) return NULL;
1407
1408    return wd->entry;
1409 }
1410
1411 EAPI const char *
1412 elm_multibuttonentry_label_get(const Evas_Object *obj)
1413 {
1414    return _text_get_hook(obj, NULL);
1415 }
1416
1417 EAPI void
1418 elm_multibuttonentry_label_set(Evas_Object *obj, const char *label)
1419 {
1420    _text_set_hook(obj, NULL, label);
1421 }
1422
1423 EAPI const char *
1424 elm_multibuttonentry_guide_text_get(const Evas_Object *obj)
1425 {
1426    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1427    Widget_Data *wd = elm_widget_data_get(obj);
1428
1429    if (!wd) return NULL;
1430    if (wd->guidetext) return edje_object_part_text_get(wd->guidetext, "elm.text");
1431    return NULL;
1432 }
1433
1434 EAPI void
1435 elm_multibuttonentry_guide_text_set(Evas_Object *obj, const char *guidetext)
1436 {
1437    ELM_CHECK_WIDTYPE(obj, widtype);
1438    Widget_Data *wd = elm_widget_data_get(obj);
1439
1440    if (!wd) return;
1441    if (guidetext)
1442      _set_guidetext(obj, guidetext);
1443    else
1444      _set_guidetext(obj, "");
1445 }
1446
1447 EAPI int
1448 elm_multibuttonentry_shrink_mode_get(const Evas_Object *obj)
1449 {
1450    ELM_CHECK_WIDTYPE(obj, widtype) -1;
1451    Widget_Data *wd = elm_widget_data_get(obj);
1452
1453    if (!wd) return -1;
1454    return wd->view_state == MULTIBUTTONENTRY_VIEW_SHRINK ? 1 : 0;
1455 }
1456
1457 EAPI void
1458 elm_multibuttonentry_shrink_mode_set(Evas_Object *obj, int shrink)
1459 {
1460    ELM_CHECK_WIDTYPE(obj, widtype);
1461    Widget_Data *wd = elm_widget_data_get(obj);
1462
1463    if (!wd || !wd->box ||
1464        ((wd->view_state == MULTIBUTTONENTRY_VIEW_SHRINK) ? 1 : 0) == shrink) return;
1465    _shrink_mode_set(obj, shrink);
1466 }
1467
1468 EAPI Elm_Object_Item *
1469 elm_multibuttonentry_item_prepend(Evas_Object *obj, const char *label, void *data)
1470 {
1471    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1472    Elm_Multibuttonentry_Item *item;
1473    if (!label) return NULL;
1474    item = _add_button_item(obj, label, MULTIBUTTONENTRY_POS_START, NULL, data);
1475    return (Elm_Object_Item *) item;
1476 }
1477
1478 EAPI Elm_Object_Item *
1479 elm_multibuttonentry_item_append(Evas_Object *obj, const char *label, void *data)
1480 {
1481    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1482    Elm_Multibuttonentry_Item *item;
1483    if (!label) return NULL;
1484    item = _add_button_item(obj, label, MULTIBUTTONENTRY_POS_END, NULL, data);
1485    return (Elm_Object_Item *) item;
1486 }
1487
1488 EAPI Elm_Object_Item *
1489 elm_multibuttonentry_item_insert_before(Evas_Object *obj, Elm_Object_Item *before, const char *label, void *data)
1490 {
1491    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1492    Elm_Multibuttonentry_Item *item;
1493    if (!label) return NULL;
1494    item = _add_button_item(obj, label, MULTIBUTTONENTRY_POS_BEFORE,
1495                            (Elm_Multibuttonentry_Item *) before, data);
1496    return (Elm_Object_Item *) item;
1497 }
1498
1499 EAPI Elm_Object_Item *
1500 elm_multibuttonentry_item_insert_after(Evas_Object *obj, Elm_Object_Item *after, const char *label, void *data)
1501 {
1502    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1503    Elm_Multibuttonentry_Item *item;
1504    if (!label) return NULL;
1505    item = _add_button_item(obj, label, MULTIBUTTONENTRY_POS_AFTER,
1506                            (Elm_Multibuttonentry_Item *) after, data);
1507    return (Elm_Object_Item *) item;
1508 }
1509
1510 EAPI const Eina_List *
1511 elm_multibuttonentry_items_get(const Evas_Object *obj)
1512 {
1513    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1514    Widget_Data *wd = elm_widget_data_get(obj);
1515    if (!wd) return NULL;
1516    return wd->items;
1517 }
1518
1519 EAPI Elm_Object_Item *
1520 elm_multibuttonentry_first_item_get(const Evas_Object *obj)
1521 {
1522    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1523    Widget_Data *wd = elm_widget_data_get(obj);
1524    if (!wd) return NULL;
1525    return eina_list_data_get(wd->items);
1526 }
1527
1528 EAPI Elm_Object_Item *
1529 elm_multibuttonentry_last_item_get(const Evas_Object *obj)
1530 {
1531    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1532    Widget_Data *wd = elm_widget_data_get(obj);
1533    if (!wd) return NULL;
1534    return eina_list_data_get(eina_list_last(wd->items));
1535 }
1536
1537 EAPI Elm_Object_Item *
1538 elm_multibuttonentry_selected_item_get(const Evas_Object *obj)
1539 {
1540    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1541    Widget_Data *wd = elm_widget_data_get(obj);
1542    if (!wd) return NULL;
1543    return eina_list_data_get(wd->current);
1544 }
1545
1546 EAPI void
1547 elm_multibuttonentry_item_select(Elm_Object_Item *it, Eina_Bool selected)
1548 {
1549    ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
1550    Elm_Multibuttonentry_Item *item = (Elm_Multibuttonentry_Item *) it;
1551    if (selected) _select_button(WIDGET(item), item->button);
1552    else _select_button(WIDGET(item), NULL);
1553 }
1554
1555 EAPI void
1556 elm_multibuttonentry_item_unselect_all(Evas_Object *obj)
1557 {
1558    ELM_CHECK_WIDTYPE(obj, widtype);
1559    Widget_Data *wd = elm_widget_data_get(obj);
1560    if (!wd) return;
1561    _select_button(obj, NULL);
1562 }
1563
1564 EAPI void
1565 elm_multibuttonentry_clear(Evas_Object *obj)
1566 {
1567    ELM_CHECK_WIDTYPE(obj, widtype);
1568    Elm_Multibuttonentry_Item *item;
1569    Widget_Data *wd = elm_widget_data_get(obj);
1570    if (!wd) return;
1571
1572    if (wd->items)
1573      {
1574         EINA_LIST_FREE(wd->items, item)
1575           {
1576              elm_box_unpack(wd->box, item->button);
1577              _del_button_obj(obj, item->button);
1578              free(item);
1579           }
1580         wd->items = NULL;
1581      }
1582    wd->current = NULL;
1583    _view_update(obj);
1584 }
1585
1586 EAPI void
1587 elm_multibuttonentry_item_del(Elm_Object_Item *it)
1588 {
1589    ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
1590    _del_button_item((Elm_Multibuttonentry_Item *) it);
1591 }
1592
1593 EAPI const char *
1594 elm_multibuttonentry_item_label_get(const Elm_Object_Item *it)
1595 {
1596    return _item_text_get_hook(it, NULL);
1597 }
1598
1599 EAPI void
1600 elm_multibuttonentry_item_label_set(Elm_Object_Item *it, const char *str)
1601 {
1602    _item_text_set_hook(it, NULL, str);
1603 }
1604
1605 EAPI Elm_Object_Item *
1606 elm_multibuttonentry_item_prev_get(const Elm_Object_Item *it)
1607 {
1608    ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
1609    Widget_Data *wd;
1610    Eina_List *l;
1611    Elm_Multibuttonentry_Item *_item;
1612
1613    wd = elm_widget_data_get(WIDGET(it));
1614    if (!wd) return NULL;
1615
1616    EINA_LIST_FOREACH(wd->items, l, _item)
1617      {
1618         if (_item == (Elm_Multibuttonentry_Item *) it)
1619           {
1620              l = eina_list_prev(l);
1621              if (!l) return NULL;
1622              return eina_list_data_get(l);
1623           }
1624      }
1625    return NULL;
1626 }
1627
1628 EAPI Elm_Object_Item *
1629 elm_multibuttonentry_item_next_get(const Elm_Object_Item *it)
1630 {
1631    ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
1632    Widget_Data *wd;
1633    Eina_List *l;
1634    Elm_Multibuttonentry_Item *_item;
1635    wd = elm_widget_data_get(WIDGET(it));
1636    if (!wd) return NULL;
1637
1638    EINA_LIST_FOREACH(wd->items, l, _item)
1639      {
1640         if (_item == (Elm_Multibuttonentry_Item *) it)
1641           {
1642              l = eina_list_next(l);
1643              if (!l) return NULL;
1644              return eina_list_data_get(l);
1645           }
1646      }
1647    return NULL;
1648 }
1649
1650 EAPI void *
1651 elm_multibuttonentry_item_data_get(const Elm_Object_Item *it)
1652 {
1653    ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
1654    return elm_widget_item_data_get(it);
1655 }
1656
1657 EAPI void
1658 elm_multibuttonentry_item_data_set(Elm_Object_Item *it, void *data)
1659 {
1660    ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
1661    return elm_widget_item_data_set(it, data);
1662 }
1663
1664 EAPI void
1665 elm_multibuttonentry_item_filter_append(Evas_Object *obj, Elm_Multibuttonentry_Item_Filter_callback func, void *data)
1666 {
1667    Elm_Multibuttonentry_Item_Filter *new_item_filter = NULL;
1668    Elm_Multibuttonentry_Item_Filter *_item_filter = NULL;
1669    Eina_List *l;
1670
1671    Widget_Data *wd = elm_widget_data_get(obj);
1672    if (!wd) return;
1673
1674    ELM_CHECK_WIDTYPE(obj, widtype);
1675    EINA_SAFETY_ON_NULL_RETURN(func);
1676
1677    new_item_filter= _filter_new(func, data);
1678    if (!new_item_filter) return;
1679
1680    EINA_LIST_FOREACH(wd->filter_list, l, _item_filter)
1681      {
1682         if ( _item_filter && ((_item_filter->callback_func == func) && (_item_filter->data == data)))
1683           {
1684              printf("Already Registered this item filter!!!!\n");
1685              return;
1686           }
1687      }
1688    wd->filter_list = eina_list_append(wd->filter_list, new_item_filter);
1689 }
1690
1691 EAPI void
1692 elm_multibuttonentry_item_filter_prepend(Evas_Object *obj, Elm_Multibuttonentry_Item_Filter_callback func, void *data)
1693 {
1694    Elm_Multibuttonentry_Item_Filter *new_item_filter = NULL;
1695    Elm_Multibuttonentry_Item_Filter *_item_filter = NULL;
1696    Eina_List *l;
1697    Widget_Data *wd = elm_widget_data_get(obj);
1698    if (!wd) return;
1699
1700    ELM_CHECK_WIDTYPE(obj, widtype);
1701    EINA_SAFETY_ON_NULL_RETURN(func);
1702
1703    new_item_filter = _filter_new(func, data);
1704    if (!new_item_filter) return;
1705
1706    EINA_LIST_FOREACH(wd->filter_list, l, _item_filter)
1707      {
1708         if (_item_filter && ((_item_filter->callback_func == func) && (_item_filter->data == data)))
1709           {
1710              printf("Already Registered this item filter!!!!\n");
1711              return;
1712           }
1713      }
1714    wd->filter_list = eina_list_prepend(wd->filter_list, new_item_filter);
1715 }
1716
1717 EAPI void
1718 elm_multibuttonentry_item_filter_remove(Evas_Object *obj, Elm_Multibuttonentry_Item_Filter_callback func, void *data)
1719 {
1720    Widget_Data *wd;
1721    Eina_List *l;
1722    Elm_Multibuttonentry_Item_Filter *item_filter;
1723
1724    wd = elm_widget_data_get(obj);
1725
1726    EINA_SAFETY_ON_NULL_RETURN(func);
1727
1728    EINA_LIST_FOREACH(wd->filter_list, l, item_filter)
1729      {
1730         if ((item_filter->callback_func == func) && ((!data) || (item_filter->data == data)))
1731           {
1732              wd->filter_list = eina_list_remove_list(wd->filter_list, l);
1733              _filter_free(item_filter);
1734              return;
1735           }
1736      }
1737 }