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