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