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