Elm entry: Remove useless sizing evals.
[framework/uifw/elementary.git] / src / lib / elm_entry.c
1 #include <Elementary.h>
2 #include <Elementary_Cursor.h>
3 #include "elm_priv.h"
4
5 /**
6  * @defgroup Entry Entry
7  *
8  * An entry is a convenience widget which shows
9  * a box that the user can enter text into.  Unlike a
10  * @ref Scrolled_Entry widget, entries DO NOT scroll with user
11  * input.  Entry widgets are capable of expanding past the
12  * boundaries of the window, thus resizing the window to its
13  * own length.
14  *
15  * You can also insert "items" in the entry with:
16  *
17  * \<item size=16x16 vsize=full href=emoticon/haha\>\</item\>
18  *
19  * for example. sizing can be set bu size=WxH, relsize=WxH or absize=WxH with
20  * vsize=ascent or vsize=full. the href=NAME sets the item name. Entry
21  * supports a list of emoticon names by default. These are:
22  *
23  * - emoticon/angry
24  * - emoticon/angry-shout
25  * - emoticon/crazy-laugh
26  * - emoticon/evil-laugh
27  * - emoticon/evil
28  * - emoticon/goggle-smile
29  * - emoticon/grumpy
30  * - emoticon/grumpy-smile
31  * - emoticon/guilty
32  * - emoticon/guilty-smile
33  * - emoticon/haha
34  * - emoticon/half-smile
35  * - emoticon/happy-panting
36  * - emoticon/happy
37  * - emoticon/indifferent
38  * - emoticon/kiss
39  * - emoticon/knowing-grin
40  * - emoticon/laugh
41  * - emoticon/little-bit-sorry
42  * - emoticon/love-lots
43  * - emoticon/love
44  * - emoticon/minimal-smile
45  * - emoticon/not-happy
46  * - emoticon/not-impressed
47  * - emoticon/omg
48  * - emoticon/opensmile
49  * - emoticon/smile
50  * - emoticon/sorry
51  * - emoticon/squint-laugh
52  * - emoticon/surprised
53  * - emoticon/suspicious
54  * - emoticon/tongue-dangling
55  * - emoticon/tongue-poke
56  * - emoticon/uh
57  * - emoticon/unhappy
58  * - emoticon/very-sorry
59  * - emoticon/what
60  * - emoticon/wink
61  * - emoticon/worried
62  * - emoticon/wtf
63  *
64  * These are built-in currently, but you can add your own item provieer that
65  * can create inlined objects in the text and fill the space allocated to the
66  * item with a custom object of your own.
67  *
68  * See the entry test for some more examples of use of this.
69  *
70  * Entries have functions to load a text file, display it,
71  * allowing editing of it and saving of changes back to the file loaded.
72  * Changes are written back to the original file after a short delay.
73  * The file to load and save to is specified by elm_entry_file_set().
74  *
75  * Signals that you can add callbacks for are:
76  *
77  * "changed" - The text within the entry was changed
78  * "activated" - The entry has had editing finished and changes are to be committed
79                  (generally when enter key is pressed)
80  * "press" - The entry has been clicked
81  * "longpressed" - The entry has been clicked for a couple seconds
82  * "clicked" - The entry has been clicked
83  * "clicked,double" - The entry has been double clicked
84  * "focused" - The entry has received focus
85  * "unfocused" - The entry has lost focus
86  * "selection,paste" - A paste action has occurred
87  * "selection,copy" - A copy action has occurred
88  * "selection,cut" - A cut action has occurred
89  * "selection,start" - A selection has begun
90  * "selection,changed" - The selection has changed
91  * "selection,cleared" - The selection has been cleared
92  * "cursor,changed" - The cursor has changed
93  * "anchor,clicked" - The anchor has been clicked
94  */
95
96 /* Maximum chunk size to be inserted to the entry at once
97  * FIXME: This size is arbitrary, should probably choose a better size.
98  * Possibly also find a way to set it to a low value for weak computers,
99  * and to a big value for better computers. */
100 #define _CHUNK_SIZE 10000
101
102 typedef struct _Mod_Api Mod_Api;
103
104 typedef struct _Widget_Data Widget_Data;
105 typedef struct _Elm_Entry_Context_Menu_Item Elm_Entry_Context_Menu_Item;
106 typedef struct _Elm_Entry_Item_Provider Elm_Entry_Item_Provider;
107 typedef struct _Elm_Entry_Text_Filter Elm_Entry_Text_Filter;
108
109 struct _Widget_Data
110 {
111    Evas_Object *ent;
112    Evas_Object *hoversel;
113    Ecore_Job *deferred_recalc_job;
114    Ecore_Event_Handler *sel_notify_handler;
115    Ecore_Event_Handler *sel_clear_handler;
116    Ecore_Timer *longpress_timer;
117    Ecore_Timer *delay_write;
118    /* for deferred appending */
119    Ecore_Idler *append_text_idler;
120    char *append_text_left;
121    int append_text_position;
122    int append_text_len;
123    /* Only for clipboard */
124    const char *cut_sel;
125    const char *text;
126    const char *file;
127    Elm_Text_Format format;
128    Evas_Coord lastw;
129    Evas_Coord downx, downy;
130    Eina_List *items;
131    Eina_List *item_providers;
132    Eina_List *text_filters;
133    Ecore_Job *hovdeljob;
134    Mod_Api *api; // module api if supplied
135    int cursor_pos;
136    Elm_Wrap_Type linewrap;
137    Eina_Bool changed : 1;
138    Eina_Bool single_line : 1;
139    Eina_Bool password : 1;
140    Eina_Bool editable : 1;
141    Eina_Bool selection_asked : 1;
142    Eina_Bool have_selection : 1;
143    Eina_Bool selmode : 1;
144    Eina_Bool deferred_cur : 1;
145    Eina_Bool disabled : 1;
146    Eina_Bool context_menu : 1;
147    Eina_Bool drag_selection_asked : 1;
148    Eina_Bool can_write : 1;
149    Eina_Bool autosave : 1;
150    Eina_Bool textonly : 1;
151    Eina_Bool usedown : 1;
152 };
153
154 struct _Elm_Entry_Context_Menu_Item
155 {
156    Evas_Object *obj;
157    const char *label;
158    const char *icon_file;
159    const char *icon_group;
160    Elm_Icon_Type icon_type;
161    Evas_Smart_Cb func;
162    void *data;
163 };
164
165 struct _Elm_Entry_Item_Provider
166 {
167    Evas_Object *(*func) (void *data, Evas_Object *entry, const char *item);
168    void *data;
169 };
170
171 struct _Elm_Entry_Text_Filter
172 {
173    void (*func) (void *data, Evas_Object *entry, char **text);
174    void *data;
175 };
176
177 static const char *widtype = NULL;
178
179 #ifdef HAVE_ELEMENTARY_X
180 static Eina_Bool _drag_drop_cb(void *data, Evas_Object *obj, Elm_Selection_Data *);
181 #endif
182 static void _del_hook(Evas_Object *obj);
183 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
184 static void _theme_hook(Evas_Object *obj);
185 static void _disable_hook(Evas_Object *obj);
186 static void _sizing_eval(Evas_Object *obj);
187 static void _on_focus_hook(void *data, Evas_Object *obj);
188 static void _resize(void *data, Evas *e, Evas_Object *obj, void *event_info);
189 static const char *_getbase(Evas_Object *obj);
190 static void _signal_entry_changed(void *data, Evas_Object *obj, const char *emission, const char *source);
191 static void _signal_selection_start(void *data, Evas_Object *obj, const char *emission, const char *source);
192 static void _signal_selection_changed(void *data, Evas_Object *obj, const char *emission, const char *source);
193 static void _signal_selection_cleared(void *data, Evas_Object *obj, const char *emission, const char *source);
194 static void _signal_entry_paste_request(void *data, Evas_Object *obj, const char *emission, const char *source);
195 static void _signal_entry_copy_notify(void *data, Evas_Object *obj, const char *emission, const char *source);
196 static void _signal_entry_cut_notify(void *data, Evas_Object *obj, const char *emission, const char *source);
197 static void _signal_cursor_changed(void *data, Evas_Object *obj, const char *emission, const char *source);
198
199 static const char SIG_CHANGED[] = "changed";
200 static const char SIG_ACTIVATED[] = "activated";
201 static const char SIG_PRESS[] = "press";
202 static const char SIG_LONGPRESSED[] = "longpressed";
203 static const char SIG_CLICKED[] = "clicked";
204 static const char SIG_CLICKED_DOUBLE[] = "clicked,double";
205 static const char SIG_FOCUSED[] = "focused";
206 static const char SIG_UNFOCUSED[] = "unfocused";
207 static const char SIG_SELECTION_PASTE[] = "selection,paste";
208 static const char SIG_SELECTION_COPY[] = "selection,copy";
209 static const char SIG_SELECTION_CUT[] = "selection,cut";
210 static const char SIG_SELECTION_START[] = "selection,start";
211 static const char SIG_SELECTION_CHANGED[] = "selection,changed";
212 static const char SIG_SELECTION_CLEARED[] = "selection,cleared";
213 static const char SIG_CURSOR_CHANGED[] = "cursor,changed";
214 static const char SIG_ANCHOR_CLICKED[] = "anchor,clicked";
215 static const Evas_Smart_Cb_Description _signals[] = {
216        {SIG_CHANGED, ""},
217        {SIG_ACTIVATED, ""},
218        {SIG_PRESS, ""},
219        {SIG_LONGPRESSED, ""},
220        {SIG_CLICKED, ""},
221        {SIG_CLICKED_DOUBLE, ""},
222        {SIG_FOCUSED, ""},
223        {SIG_UNFOCUSED, ""},
224        {SIG_SELECTION_PASTE, ""},
225        {SIG_SELECTION_COPY, ""},
226        {SIG_SELECTION_CUT, ""},
227        {SIG_SELECTION_START, ""},
228        {SIG_SELECTION_CHANGED, ""},
229        {SIG_SELECTION_CLEARED, ""},
230        {SIG_CURSOR_CHANGED, ""},
231        {SIG_ANCHOR_CLICKED, ""},
232        {NULL, NULL}
233 };
234
235 static Eina_List *entries = NULL;
236
237 struct _Mod_Api
238 {
239    void (*obj_hook) (Evas_Object *obj);
240    void (*obj_unhook) (Evas_Object *obj);
241    void (*obj_longpress) (Evas_Object *obj);
242 };
243
244 static Mod_Api *
245 _module(Evas_Object *obj __UNUSED__)
246 {
247    static Elm_Module *m = NULL;
248    if (m) goto ok; // already found - just use
249    if (!(m = _elm_module_find_as("entry/api"))) return NULL;
250    // get module api
251    m->api = malloc(sizeof(Mod_Api));
252    if (!m->api) return NULL;
253    ((Mod_Api *)(m->api)      )->obj_hook = // called on creation
254       _elm_module_symbol_get(m, "obj_hook");
255    ((Mod_Api *)(m->api)      )->obj_unhook = // called on deletion
256       _elm_module_symbol_get(m, "obj_unhook");
257    ((Mod_Api *)(m->api)      )->obj_longpress = // called on long press menu
258       _elm_module_symbol_get(m, "obj_longpress");
259 ok: // ok - return api
260    return m->api;
261 }
262
263 static char *
264 _buf_append(char *buf, const char *str, int *len, int *alloc)
265 {
266    int len2 = strlen(str);
267    if ((*len + len2) >= *alloc)
268      {
269         char *buf2 = realloc(buf, *alloc + len2 + 512);
270         if (!buf2) return NULL;
271         buf = buf2;
272         *alloc += (512 + len2);
273      }
274    strcpy(buf + *len, str);
275    *len += len2;
276    return buf;
277 }
278
279 static char *
280 _load_file(const char *file)
281 {
282    FILE *f;
283    size_t size;
284    int alloc = 0, len = 0;
285    char *text = NULL, buf[16384 + 1];
286
287    f = fopen(file, "rb");
288    if (!f) return NULL;
289    while ((size = fread(buf, 1, sizeof(buf) - 1, f)))
290      {
291         char *tmp_text;
292         buf[size] = 0;
293         tmp_text = _buf_append(text, buf, &len, &alloc);
294         if (!tmp_text) break;
295         text = tmp_text;
296      }
297    fclose(f);
298    return text;
299 }
300
301 static char *
302 _load_plain(const char *file)
303 {
304    char *text;
305
306    text = _load_file(file);
307    if (text)
308      {
309         char *text2;
310
311         text2 = elm_entry_utf8_to_markup(text);
312         free(text);
313         return text2;
314      }
315    return NULL;
316 }
317
318 static void
319 _load(Evas_Object *obj)
320 {
321    Widget_Data *wd = elm_widget_data_get(obj);
322    char *text;
323    if (!wd) return;
324    if (!wd->file)
325      {
326         elm_entry_entry_set(obj, "");
327         return;
328      }
329    switch (wd->format)
330      {
331       case ELM_TEXT_FORMAT_PLAIN_UTF8:
332          text = _load_plain(wd->file);
333          break;
334       case ELM_TEXT_FORMAT_MARKUP_UTF8:
335          text = _load_file(wd->file);
336          break;
337       default:
338          text = NULL;
339          break;
340      }
341    if (text)
342      {
343         elm_entry_entry_set(obj, text);
344         free(text);
345      }
346    else
347      elm_entry_entry_set(obj, "");
348 }
349
350 static void
351 _save_markup_utf8(const char *file, const char *text)
352 {
353    FILE *f;
354
355    if ((!text) || (!text[0]))
356      {
357         ecore_file_unlink(file);
358         return;
359      }
360    f = fopen(file, "wb");
361    if (!f)
362      {
363         // FIXME: report a write error
364         return;
365      }
366    fputs(text, f); // FIXME: catch error
367    fclose(f);
368 }
369
370 static void
371 _save_plain_utf8(const char *file, const char *text)
372 {
373    char *text2;
374
375    text2 = elm_entry_markup_to_utf8(text);
376    if (!text2)
377      return;
378    _save_markup_utf8(file, text2);
379    free(text2);
380 }
381
382 static void
383 _save(Evas_Object *obj)
384 {
385    Widget_Data *wd = elm_widget_data_get(obj);
386    if (!wd) return;
387    if (!wd->file) return;
388    switch (wd->format)
389      {
390       case ELM_TEXT_FORMAT_PLAIN_UTF8:
391          _save_plain_utf8(wd->file, elm_entry_entry_get(obj));
392          break;
393       case ELM_TEXT_FORMAT_MARKUP_UTF8:
394          _save_markup_utf8(wd->file, elm_entry_entry_get(obj));
395          break;
396       default:
397          break;
398      }
399 }
400
401 static Eina_Bool
402 _delay_write(void *data)
403 {
404    Widget_Data *wd = elm_widget_data_get(data);
405    if (!wd) return ECORE_CALLBACK_CANCEL;
406    _save(data);
407    wd->delay_write = NULL;
408    return ECORE_CALLBACK_CANCEL;
409 }
410
411 static Elm_Entry_Text_Filter *
412 _filter_new(void (*func) (void *data, Evas_Object *entry, char **text), void *data)
413 {
414    Elm_Entry_Text_Filter *tf = ELM_NEW(Elm_Entry_Text_Filter);
415    if (!tf) return NULL;
416
417    tf->func = func;
418    if (func == elm_entry_filter_limit_size)
419      {
420         Elm_Entry_Filter_Limit_Size *lim = data, *lim2;
421
422         if (!data)
423           {
424              free(tf);
425              return NULL;
426           }
427         lim2 = malloc(sizeof(Elm_Entry_Filter_Limit_Size));
428         if (!lim2)
429           {
430              free(tf);
431              return NULL;
432           }
433         memcpy(lim2, lim, sizeof(Elm_Entry_Filter_Limit_Size));
434         tf->data = lim2;
435      }
436    else if (func == elm_entry_filter_accept_set)
437      {
438         Elm_Entry_Filter_Accept_Set *as = data, *as2;
439
440         if (!data)
441           {
442              free(tf);
443              return NULL;
444           }
445         as2 = malloc(sizeof(Elm_Entry_Filter_Accept_Set));
446         if (!as2)
447           {
448              free(tf);
449              return NULL;
450           }
451         if (as->accepted)
452           as2->accepted = eina_stringshare_add(as->accepted);
453         else
454           as2->accepted = NULL;
455         if (as->rejected)
456           as2->rejected = eina_stringshare_add(as->rejected);
457         else
458           as2->rejected = NULL;
459         tf->data = as2;
460      }
461    else
462      tf->data = data;
463    return tf;
464 }
465
466 static void
467 _filter_free(Elm_Entry_Text_Filter *tf)
468 {
469    if (tf->func == elm_entry_filter_limit_size)
470      {
471         Elm_Entry_Filter_Limit_Size *lim = tf->data;
472         if (lim) free(lim);
473      }
474    else if (tf->func == elm_entry_filter_accept_set)
475      {
476         Elm_Entry_Filter_Accept_Set *as = tf->data;
477         if (as)
478           {
479              if (as->accepted) eina_stringshare_del(as->accepted);
480              if (as->rejected) eina_stringshare_del(as->rejected);
481              free(as);
482           }
483      }
484    free(tf);
485 }
486
487 static void
488 _del_pre_hook(Evas_Object *obj)
489 {
490    Widget_Data *wd = elm_widget_data_get(obj);
491    if (!wd) return;
492    if (wd->delay_write)
493      {
494         ecore_timer_del(wd->delay_write);
495         wd->delay_write = NULL;
496         if (wd->autosave) _save(obj);
497      }
498 }
499
500 static void
501 _del_hook(Evas_Object *obj)
502 {
503    Widget_Data *wd = elm_widget_data_get(obj);
504    Elm_Entry_Context_Menu_Item *it;
505    Elm_Entry_Item_Provider *ip;
506    Elm_Entry_Text_Filter *tf;
507
508    if (wd->file) eina_stringshare_del(wd->file);
509
510    if (wd->hovdeljob) ecore_job_del(wd->hovdeljob);
511    if ((wd->api) && (wd->api->obj_unhook)) wd->api->obj_unhook(obj); // module - unhook
512
513    entries = eina_list_remove(entries, obj);
514 #ifdef HAVE_ELEMENTARY_X
515    if (wd->sel_notify_handler)
516      ecore_event_handler_del(wd->sel_notify_handler);
517    if (wd->sel_clear_handler)
518      ecore_event_handler_del(wd->sel_clear_handler);
519 #endif
520    if (wd->cut_sel) eina_stringshare_del(wd->cut_sel);
521    if (wd->text) eina_stringshare_del(wd->text);
522    if (wd->deferred_recalc_job) ecore_job_del(wd->deferred_recalc_job);
523    if (wd->append_text_idler)
524      {
525         ecore_idler_del(wd->append_text_idler);
526         free(wd->append_text_left);
527         wd->append_text_left = NULL;
528         wd->append_text_idler = NULL;
529      }
530    if (wd->longpress_timer) ecore_timer_del(wd->longpress_timer);
531    EINA_LIST_FREE(wd->items, it)
532      {
533         eina_stringshare_del(it->label);
534         eina_stringshare_del(it->icon_file);
535         eina_stringshare_del(it->icon_group);
536         free(it);
537      }
538    EINA_LIST_FREE(wd->item_providers, ip)
539      {
540         free(ip);
541      }
542    EINA_LIST_FREE(wd->text_filters, tf)
543      {
544         _filter_free(tf);
545      }
546    free(wd);
547 }
548
549 static void
550 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
551 {
552    Widget_Data *wd = elm_widget_data_get(obj);
553    edje_object_mirrored_set(wd->ent, rtl);
554 }
555
556 static void
557 _theme_hook(Evas_Object *obj)
558 {
559    Widget_Data *wd = elm_widget_data_get(obj);
560    const char *t;
561    _elm_widget_mirrored_reload(obj);
562    _mirrored_set(obj, elm_widget_mirrored_get(obj));
563
564    t = eina_stringshare_add(elm_entry_entry_get(obj));
565    _elm_theme_object_set(obj, wd->ent, "entry", _getbase(obj), elm_widget_style_get(obj));
566    if (_elm_config->desktop_entry)
567      edje_object_part_text_select_allow_set(wd->ent, "elm.text", EINA_TRUE);
568    elm_entry_entry_set(obj, t);
569    eina_stringshare_del(t);
570    if (elm_widget_disabled_get(obj))
571      edje_object_signal_emit(wd->ent, "elm,state,disabled", "elm");
572    elm_entry_cursor_pos_set(obj, wd->cursor_pos);
573    if (elm_widget_focus_get(obj))
574      edje_object_signal_emit(wd->ent, "elm,action,focus", "elm");
575    edje_object_message_signal_process(wd->ent);
576    edje_object_scale_set(wd->ent, elm_widget_scale_get(obj) * _elm_config->scale);
577    _sizing_eval(obj);
578 }
579
580 static void
581 _disable_hook(Evas_Object *obj)
582 {
583    Widget_Data *wd = elm_widget_data_get(obj);
584
585    if (elm_widget_disabled_get(obj))
586      {
587         edje_object_signal_emit(wd->ent, "elm,state,disabled", "elm");
588         wd->disabled = EINA_TRUE;
589      }
590    else
591      {
592         edje_object_signal_emit(wd->ent, "elm,state,enabled", "elm");
593         wd->disabled = EINA_FALSE;
594      }
595 }
596
597 static void
598 _elm_win_recalc_job(void *data)
599 {
600    Widget_Data *wd = elm_widget_data_get(data);
601    Evas_Coord minh = -1, resw = -1, minw = -1;
602    if (!wd) return;
603    wd->deferred_recalc_job = NULL;
604
605    evas_object_geometry_get(wd->ent, NULL, NULL, &resw, NULL);
606    edje_object_size_min_restricted_calc(wd->ent, &minw, &minh, resw, 0);
607    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
608    /* This is a hack to workaround the way min size hints are treated.
609     * If the minimum width is smaller than the restricted width, it means
610     * the mininmum doesn't matter. */
611    if (minw <= resw)
612      {
613         Evas_Coord ominw = -1;
614         evas_object_size_hint_min_get(data, &ominw, NULL);
615         minw = ominw;
616      }
617    evas_object_size_hint_min_set(data, minw, minh);
618    if (wd->single_line)
619      evas_object_size_hint_max_set(data, -1, minh);
620
621    if (wd->deferred_cur)
622      {
623         Evas_Coord cx, cy, cw, ch;
624         edje_object_part_text_cursor_geometry_get(wd->ent, "elm.text",
625               &cx, &cy, &cw, &ch);
626         elm_widget_show_region_set(data, cx, cy, cw, ch);
627      }
628 }
629
630 static void
631 _sizing_eval(Evas_Object *obj)
632 {
633    Widget_Data *wd = elm_widget_data_get(obj);
634    Evas_Coord minw = -1, minh = -1;
635    Evas_Coord resw, resh;
636    if (!wd) return;
637    if (wd->linewrap)
638      {
639         evas_object_geometry_get(wd->ent, NULL, NULL, &resw, &resh);
640         if ((resw == wd->lastw) && (!wd->changed)) return;
641         wd->changed = EINA_FALSE;
642         wd->lastw = resw;
643         if (wd->deferred_recalc_job) ecore_job_del(wd->deferred_recalc_job);
644         wd->deferred_recalc_job = ecore_job_add(_elm_win_recalc_job, obj);
645      }
646    else
647      {
648         edje_object_size_min_calc(wd->ent, &minw, &minh);
649         elm_coords_finger_size_adjust(1, &minw, 1, &minh);
650         evas_object_size_hint_min_set(obj, minw, minh);
651         if (wd->single_line)
652           evas_object_size_hint_max_set(obj, -1, minh);
653      }
654 }
655
656 static void
657 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
658 {
659    Widget_Data *wd = elm_widget_data_get(obj);
660    Evas_Object *top = elm_widget_top_get(obj);
661    if (!wd) return;
662    if (!wd->editable) return;
663    if (elm_widget_focus_get(obj))
664      {
665         evas_object_focus_set(wd->ent, EINA_TRUE);
666         edje_object_signal_emit(wd->ent, "elm,action,focus", "elm");
667         if (top) elm_win_keyboard_mode_set(top, ELM_WIN_KEYBOARD_ON);
668         evas_object_smart_callback_call(obj, SIG_FOCUSED, NULL);
669      }
670    else
671      {
672         edje_object_signal_emit(wd->ent, "elm,action,unfocus", "elm");
673         evas_object_focus_set(wd->ent, EINA_FALSE);
674         if (top) elm_win_keyboard_mode_set(top, ELM_WIN_KEYBOARD_OFF);
675         evas_object_smart_callback_call(obj, SIG_UNFOCUSED, NULL);
676      }
677 }
678
679 static void
680 _signal_emit_hook(Evas_Object *obj, const char *emission, const char *source)
681 {
682    Widget_Data *wd = elm_widget_data_get(obj);
683    if (!wd) return;
684    edje_object_signal_emit(wd->ent, emission, source);
685 }
686
687 static void
688 _signal_callback_add_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
689 {
690    Widget_Data *wd = elm_widget_data_get(obj);
691    if (!wd) return;
692    edje_object_signal_callback_add(wd->ent, emission, source, func_cb, data);
693 }
694
695 static void
696 _signal_callback_del_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
697 {
698    Widget_Data *wd = elm_widget_data_get(obj);
699    edje_object_signal_callback_del_full(wd->ent, emission, source, func_cb,
700                                         data);
701 }
702
703 static void
704 _on_focus_region_hook(const Evas_Object *obj, Evas_Coord *x, Evas_Coord *y, Evas_Coord *w, Evas_Coord *h)
705 {
706    Widget_Data *wd = elm_widget_data_get(obj);
707    edje_object_part_text_cursor_geometry_get(wd->ent, "elm.text", x, y, w, h);
708 }
709
710 static void
711 _hoversel_position(Evas_Object *obj)
712 {
713    Widget_Data *wd = elm_widget_data_get(obj);
714    Evas_Coord cx, cy, cw, ch, x, y, mw, mh;
715    if (!wd) return;
716
717    cx = cy = 0;
718    cw = ch = 1;
719    evas_object_geometry_get(wd->ent, &x, &y, NULL, NULL);
720    if (wd->usedown)
721      {
722         cx = wd->downx - x;
723         cy = wd->downy - y;
724         cw = 1;
725         ch = 1;
726      }
727    else
728      edje_object_part_text_cursor_geometry_get(wd->ent, "elm.text",
729                                                &cx, &cy, &cw, &ch);
730    evas_object_size_hint_min_get(wd->hoversel, &mw, &mh);
731    if (cw < mw)
732      {
733         cx += (cw - mw) / 2;
734         cw = mw;
735      }
736    if (ch < mh)
737      {
738         cy += (ch - mh) / 2;
739         ch = mh;
740      }
741    evas_object_move(wd->hoversel, x + cx, y + cy);
742    evas_object_resize(wd->hoversel, cw, ch);
743 }
744
745 static void
746 _move(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
747 {
748    Widget_Data *wd = elm_widget_data_get(data);
749
750    if (wd->hoversel) _hoversel_position(data);
751 }
752
753 static void
754 _resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
755 {
756    Widget_Data *wd = elm_widget_data_get(data);
757    if (!wd) return;
758    if (wd->linewrap)
759      {
760         _sizing_eval(data);
761      }
762    if (wd->hoversel) _hoversel_position(data);
763    //   Evas_Coord ww, hh;
764    //   evas_object_geometry_get(wd->ent, NULL, NULL, &ww, &hh);
765 }
766
767 static void
768 _hover_del(void *data)
769 {
770    Widget_Data *wd = elm_widget_data_get(data);
771    if (!wd) return;
772
773    if (wd->hoversel)
774      {
775         evas_object_del(wd->hoversel);
776         wd->hoversel = NULL;
777      }
778    wd->hovdeljob = NULL;
779 }
780
781 static void
782 _dismissed(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
783 {
784    Widget_Data *wd = elm_widget_data_get(data);
785    if (!wd) return;
786    wd->usedown = 0;
787    if (wd->hoversel) evas_object_hide(wd->hoversel);
788    if (wd->selmode)
789      {
790         if (!_elm_config->desktop_entry)
791           {
792              if (!wd->password)
793                edje_object_part_text_select_allow_set(wd->ent, "elm.text", EINA_TRUE);
794           }
795      }
796    elm_widget_scroll_freeze_pop(data);
797    if (wd->hovdeljob) ecore_job_del(wd->hovdeljob);
798    wd->hovdeljob = ecore_job_add(_hover_del, data);
799 }
800
801 static void
802 _select(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
803 {
804    Widget_Data *wd = elm_widget_data_get(data);
805    if (!wd) return;
806    wd->selmode = EINA_TRUE;
807    edje_object_part_text_select_none(wd->ent, "elm.text");
808    if (!_elm_config->desktop_entry)
809      {
810         if (!wd->password)
811           edje_object_part_text_select_allow_set(wd->ent, "elm.text", EINA_TRUE);
812      }
813    edje_object_signal_emit(wd->ent, "elm,state,select,on", "elm");
814    if (!_elm_config->desktop_entry)
815      elm_widget_scroll_hold_push(data);
816 }
817
818 static void
819 _paste(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
820 {
821    Widget_Data *wd = elm_widget_data_get(data);
822    if (!wd) return;
823    evas_object_smart_callback_call(data, SIG_SELECTION_PASTE, NULL);
824    if (wd->sel_notify_handler)
825      {
826 #ifdef HAVE_ELEMENTARY_X
827         Elm_Sel_Format formats;
828         wd->selection_asked = EINA_TRUE;
829         formats = ELM_SEL_FORMAT_MARKUP;
830         if (!wd->textonly)
831           formats |= ELM_SEL_FORMAT_IMAGE;
832         elm_selection_get(ELM_SEL_CLIPBOARD, formats, data, NULL, NULL);
833 #endif
834      }
835 }
836
837 static void
838 _store_selection(Elm_Sel_Type seltype, Evas_Object *obj)
839 {
840    Widget_Data *wd = elm_widget_data_get(obj);
841    const char *sel;
842
843    if (!wd) return;
844    sel = edje_object_part_text_selection_get(wd->ent, "elm.text");
845    elm_selection_set(seltype, obj, ELM_SEL_FORMAT_MARKUP, sel);
846    if (seltype == ELM_SEL_CLIPBOARD)
847      eina_stringshare_replace(&wd->cut_sel, sel);
848 }
849
850 static void
851 _cut(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
852 {
853    Widget_Data *wd = elm_widget_data_get(data);
854
855    /* Store it */
856    wd->selmode = EINA_FALSE;
857    if (!_elm_config->desktop_entry)
858      edje_object_part_text_select_allow_set(wd->ent, "elm.text", EINA_FALSE);
859    edje_object_signal_emit(wd->ent, "elm,state,select,off", "elm");
860    if (!_elm_config->desktop_entry)
861      elm_widget_scroll_hold_pop(data);
862    _store_selection(ELM_SEL_CLIPBOARD, data);
863    edje_object_part_text_insert(wd->ent, "elm.text", "");
864    edje_object_part_text_select_none(wd->ent, "elm.text");
865 }
866
867 static void
868 _copy(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
869 {
870    Widget_Data *wd = elm_widget_data_get(data);
871    if (!wd) return;
872    wd->selmode = EINA_FALSE;
873    if (!_elm_config->desktop_entry)
874      {
875         edje_object_part_text_select_allow_set(wd->ent, "elm.text", EINA_FALSE);
876         edje_object_signal_emit(wd->ent, "elm,state,select,off", "elm");
877         elm_widget_scroll_hold_pop(data);
878      }
879    _store_selection(ELM_SEL_CLIPBOARD, data);
880    //   edje_object_part_text_select_none(wd->ent, "elm.text");
881 }
882
883 static void
884 _cancel(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
885 {
886    Widget_Data *wd = elm_widget_data_get(data);
887    if (!wd) return;
888    wd->selmode = EINA_FALSE;
889    if (!_elm_config->desktop_entry)
890      edje_object_part_text_select_allow_set(wd->ent, "elm.text", EINA_FALSE);
891    edje_object_signal_emit(wd->ent, "elm,state,select,off", "elm");
892    if (!_elm_config->desktop_entry)
893      elm_widget_scroll_hold_pop(data);
894    edje_object_part_text_select_none(wd->ent, "elm.text");
895 }
896
897 static void
898 _item_clicked(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
899 {
900    Elm_Entry_Context_Menu_Item *it = data;
901    Evas_Object *obj2 = it->obj;
902    if (it->func) it->func(it->data, obj2, NULL);
903 }
904
905 static void
906 _menu_press(Evas_Object *obj)
907 {
908    Widget_Data *wd = elm_widget_data_get(obj);
909    Evas_Object *top;
910    const Eina_List *l;
911    const Elm_Entry_Context_Menu_Item *it;
912    if (!wd) return;
913    if ((wd->api) && (wd->api->obj_longpress))
914      {
915         wd->api->obj_longpress(obj);
916      }
917    else if (wd->context_menu)
918      {
919         const char *context_menu_orientation;
920
921         if (wd->hoversel) evas_object_del(wd->hoversel);
922         else elm_widget_scroll_freeze_push(obj);
923         wd->hoversel = elm_hoversel_add(obj);
924         context_menu_orientation = edje_object_data_get
925            (wd->ent, "context_menu_orientation");
926         if ((context_menu_orientation) &&
927             (!strcmp(context_menu_orientation, "horizontal")))
928           elm_hoversel_horizontal_set(wd->hoversel, EINA_TRUE);
929         elm_object_style_set(wd->hoversel, "entry");
930         elm_widget_sub_object_add(obj, wd->hoversel);
931         elm_hoversel_label_set(wd->hoversel, "Text");
932         top = elm_widget_top_get(obj);
933         if (top) elm_hoversel_hover_parent_set(wd->hoversel, top);
934         evas_object_smart_callback_add(wd->hoversel, "dismissed", _dismissed, obj);
935         if (wd->have_selection)
936           {
937              if (!wd->password)
938                {
939                   if (wd->have_selection)
940                     {
941                        elm_hoversel_item_add(wd->hoversel, E_("Copy"), NULL, ELM_ICON_NONE,
942                                              _copy, obj);
943                        if (wd->editable)
944                          elm_hoversel_item_add(wd->hoversel, E_("Cut"), NULL, ELM_ICON_NONE,
945                                                _cut, obj);
946                     }
947                   elm_hoversel_item_add(wd->hoversel, E_("Cancel"), NULL, ELM_ICON_NONE,
948                                         _cancel, obj);
949                }
950           }
951         else
952           {
953              if (!wd->selmode)
954                {
955                   if (!_elm_config->desktop_entry)
956                     {
957                        if (!wd->password)
958                          elm_hoversel_item_add(wd->hoversel, E_("Select"), NULL, ELM_ICON_NONE,
959                                                _select, obj);
960                     }
961                   if (1) // need way to detect if someone has a selection
962                     {
963                        if (wd->editable)
964                          elm_hoversel_item_add(wd->hoversel, E_("Paste"), NULL, ELM_ICON_NONE,
965                                                _paste, obj);
966                     }
967                }
968           }
969         EINA_LIST_FOREACH(wd->items, l, it)
970           {
971              elm_hoversel_item_add(wd->hoversel, it->label, it->icon_file,
972                                    it->icon_type, _item_clicked, it);
973           }
974         if (wd->hoversel)
975           {
976              _hoversel_position(obj);
977              evas_object_show(wd->hoversel);
978              elm_hoversel_hover_begin(wd->hoversel);
979           }
980         if (!_elm_config->desktop_entry)
981           {
982              edje_object_part_text_select_allow_set(wd->ent, "elm.text", EINA_FALSE);
983              edje_object_part_text_select_abort(wd->ent, "elm.text");
984           }
985      }
986 }
987
988 static Eina_Bool
989 _long_press(void *data)
990 {
991    Widget_Data *wd = elm_widget_data_get(data);
992    if (!wd) return ECORE_CALLBACK_CANCEL;
993    _menu_press(data);
994    wd->longpress_timer = NULL;
995    evas_object_smart_callback_call(data, SIG_LONGPRESSED, NULL);
996    return ECORE_CALLBACK_CANCEL;
997 }
998
999 static void
1000 _mouse_down(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
1001 {
1002    Widget_Data *wd = elm_widget_data_get(data);
1003    Evas_Event_Mouse_Down *ev = event_info;
1004    if (!wd) return;
1005    if (wd->disabled) return;
1006    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return;
1007    wd->downx = ev->canvas.x;
1008    wd->downy = ev->canvas.y;
1009    if (ev->button == 1)
1010      {
1011         if (wd->longpress_timer) ecore_timer_del(wd->longpress_timer);
1012         wd->longpress_timer = ecore_timer_add(_elm_config->longpress_timeout, _long_press, data);
1013      }
1014 }
1015
1016 static void
1017 _mouse_up(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
1018 {
1019    Widget_Data *wd = elm_widget_data_get(data);
1020    Evas_Event_Mouse_Up *ev = event_info;
1021    if (!wd) return;
1022    if (wd->disabled) return;
1023    if (ev->button == 1)
1024      {
1025         if (wd->longpress_timer)
1026           {
1027              ecore_timer_del(wd->longpress_timer);
1028              wd->longpress_timer = NULL;
1029           }
1030      }
1031    else if (ev->button == 3)
1032      {
1033         wd->usedown = 1;
1034         _menu_press(data);
1035      }
1036 }
1037
1038 static void
1039 _mouse_move(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
1040 {
1041    Widget_Data *wd = elm_widget_data_get(data);
1042    Evas_Event_Mouse_Move *ev = event_info;
1043    if (!wd) return;
1044    if (wd->disabled) return;
1045    if (!wd->selmode)
1046      {
1047         if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
1048           {
1049              if (wd->longpress_timer)
1050                {
1051                   ecore_timer_del(wd->longpress_timer);
1052                   wd->longpress_timer = NULL;
1053                }
1054           }
1055         else if (wd->longpress_timer)
1056           {
1057              Evas_Coord dx, dy;
1058
1059              dx = wd->downx - ev->cur.canvas.x;
1060              dx *= dx;
1061              dy = wd->downy - ev->cur.canvas.y;
1062              dy *= dy;
1063              if ((dx + dy) >
1064                  ((_elm_config->finger_size / 2) *
1065                   (_elm_config->finger_size / 2)))
1066                {
1067                   ecore_timer_del(wd->longpress_timer);
1068                   wd->longpress_timer = NULL;
1069                }
1070           }
1071      }
1072    else if (wd->longpress_timer)
1073      {
1074         Evas_Coord dx, dy;
1075
1076         dx = wd->downx - ev->cur.canvas.x;
1077         dx *= dx;
1078         dy = wd->downy - ev->cur.canvas.y;
1079         dy *= dy;
1080         if ((dx + dy) >
1081             ((_elm_config->finger_size / 2) *
1082              (_elm_config->finger_size / 2)))
1083           {
1084              ecore_timer_del(wd->longpress_timer);
1085              wd->longpress_timer = NULL;
1086           }
1087      }
1088 }
1089
1090 static const char *
1091 _getbase(Evas_Object *obj)
1092 {
1093    Widget_Data *wd = elm_widget_data_get(obj);
1094    if (!wd) return "base";
1095    if (wd->editable)
1096      {
1097         if (wd->password) return "base-password";
1098         else
1099           {
1100              if (wd->single_line) return "base-single";
1101              else
1102                {
1103                   switch (wd->linewrap)
1104                     {
1105                      case ELM_WRAP_CHAR:
1106                         return "base-charwrap";
1107                      case ELM_WRAP_WORD:
1108                         return "base";
1109                      case ELM_WRAP_MIXED:
1110                         return "base-mixedwrap";
1111                      case ELM_WRAP_NONE:
1112                      default:
1113                         return "base-nowrap";
1114                     }
1115                }
1116           }
1117      }
1118    else
1119      {
1120         if (wd->password) return "base-password";
1121         else
1122           {
1123              if (wd->single_line) return "base-single-noedit";
1124              else
1125                {
1126                   switch (wd->linewrap)
1127                     {
1128                      case ELM_WRAP_CHAR:
1129                         return "base-noedit-charwrap";
1130                      case ELM_WRAP_WORD:
1131                         return "base-noedit";
1132                      case ELM_WRAP_MIXED:
1133                         return "base-noedit-mixedwrap";
1134                      case ELM_WRAP_NONE:
1135                      default:
1136                         return "base-nowrap-noedit";
1137                     }
1138                }
1139           }
1140      }
1141    return "base";
1142 }
1143
1144 static void
1145 _signal_entry_changed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1146 {
1147    Widget_Data *wd = elm_widget_data_get(data);
1148    Evas_Coord minh;
1149    if (!wd) return;
1150    wd->changed = EINA_TRUE;
1151    /* Reset the size hints which are no more relevant.
1152     * Keep the height, this is a hack, but doesn't really matter
1153     * cause we'll re-eval in a moment. */
1154    evas_object_size_hint_min_get(data, NULL, &minh);
1155    evas_object_size_hint_min_set(data, -1, minh);
1156    _sizing_eval(data);
1157    if (wd->text) eina_stringshare_del(wd->text);
1158    wd->text = NULL;
1159    evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
1160    if (wd->delay_write)
1161      {
1162         ecore_timer_del(wd->delay_write);
1163         wd->delay_write = NULL;
1164      }
1165    if ((!wd->autosave) || (!wd->file)) return;
1166    wd->delay_write = ecore_timer_add(2.0, _delay_write, data);
1167 }
1168
1169 static void
1170 _signal_selection_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1171 {
1172    Widget_Data *wd = elm_widget_data_get(data);
1173    const Eina_List *l;
1174    Evas_Object *entry;
1175    if (!wd) return;
1176    EINA_LIST_FOREACH(entries, l, entry)
1177      {
1178         if (entry != data) elm_entry_select_none(entry);
1179      }
1180    wd->have_selection = EINA_TRUE;
1181    evas_object_smart_callback_call(data, SIG_SELECTION_START, NULL);
1182 #ifdef HAVE_ELEMENTARY_X
1183    if (wd->sel_notify_handler)
1184      {
1185         const char *txt = elm_entry_selection_get(data);
1186         Evas_Object *top;
1187
1188         top = elm_widget_top_get(data);
1189         if ((top) && (elm_win_xwindow_get(top)))
1190           elm_selection_set(ELM_SEL_PRIMARY, data, ELM_SEL_FORMAT_MARKUP, txt);
1191      }
1192 #endif
1193 }
1194
1195 static void
1196 _signal_selection_changed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1197 {
1198    Widget_Data *wd = elm_widget_data_get(data);
1199    if (!wd) return;
1200    wd->have_selection = EINA_TRUE;
1201    evas_object_smart_callback_call(data, SIG_SELECTION_CHANGED, NULL);
1202    elm_selection_set(ELM_SEL_PRIMARY, obj, ELM_SEL_FORMAT_MARKUP,
1203                      elm_entry_selection_get(data));
1204 }
1205
1206 static void
1207 _signal_selection_cleared(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1208 {
1209    Widget_Data *wd = elm_widget_data_get(data);
1210    if (!wd) return;
1211    if (!wd->have_selection) return;
1212    wd->have_selection = EINA_FALSE;
1213    evas_object_smart_callback_call(data, SIG_SELECTION_CLEARED, NULL);
1214    if (wd->sel_notify_handler)
1215      {
1216         if (wd->cut_sel)
1217           {
1218 #ifdef HAVE_ELEMENTARY_X
1219              Evas_Object *top;
1220
1221              top = elm_widget_top_get(data);
1222              if ((top) && (elm_win_xwindow_get(top)))
1223                elm_selection_set(ELM_SEL_PRIMARY, data, ELM_SEL_FORMAT_MARKUP,
1224                                  wd->cut_sel);
1225 #endif
1226              eina_stringshare_del(wd->cut_sel);
1227              wd->cut_sel = NULL;
1228           }
1229         else
1230           {
1231 #ifdef HAVE_ELEMENTARY_X
1232              Evas_Object *top;
1233
1234              top = elm_widget_top_get(data);
1235              if ((top) && (elm_win_xwindow_get(top)))
1236                elm_selection_clear(ELM_SEL_PRIMARY, data);
1237 #endif
1238           }
1239      }
1240 }
1241
1242 static void
1243 _signal_entry_paste_request(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1244 {
1245    Widget_Data *wd = elm_widget_data_get(data);
1246    if (!wd) return;
1247    evas_object_smart_callback_call(data, SIG_SELECTION_PASTE, NULL);
1248    if (wd->sel_notify_handler)
1249      {
1250 #ifdef HAVE_ELEMENTARY_X
1251         Evas_Object *top;
1252
1253         top = elm_widget_top_get(data);
1254         if ((top) && (elm_win_xwindow_get(top)))
1255           {
1256              wd->selection_asked = EINA_TRUE;
1257              elm_selection_get(ELM_SEL_CLIPBOARD, ELM_SEL_FORMAT_MARKUP, data,
1258                                NULL, NULL);
1259           }
1260 #endif
1261      }
1262 }
1263
1264 static void
1265 _signal_entry_copy_notify(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1266 {
1267    Widget_Data *wd = elm_widget_data_get(data);
1268    if (!wd) return;
1269    evas_object_smart_callback_call(data, SIG_SELECTION_COPY, NULL);
1270    elm_selection_set(ELM_SEL_CLIPBOARD, obj, ELM_SEL_FORMAT_MARKUP,
1271                      elm_entry_selection_get(data));
1272 }
1273
1274 static void
1275 _signal_entry_cut_notify(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1276 {
1277    Widget_Data *wd = elm_widget_data_get(data);
1278    if (!wd) return;
1279    evas_object_smart_callback_call(data, SIG_SELECTION_CUT, NULL);
1280    elm_selection_set(ELM_SEL_CLIPBOARD, obj, ELM_SEL_FORMAT_MARKUP,
1281                      elm_entry_selection_get(data));
1282    edje_object_part_text_insert(wd->ent, "elm.text", "");
1283    wd->changed = EINA_TRUE;
1284    _sizing_eval(data);
1285 }
1286
1287 static void
1288 _signal_cursor_changed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1289 {
1290    Widget_Data *wd = elm_widget_data_get(data);
1291    if (!wd) return;
1292    evas_object_smart_callback_call(data, SIG_CURSOR_CHANGED, NULL);
1293    wd->cursor_pos = edje_object_part_text_cursor_pos_get(wd->ent, "elm.text", EDJE_CURSOR_MAIN);
1294    if (!wd->deferred_recalc_job)
1295      {
1296         Evas_Coord cx, cy, cw, ch;
1297         edje_object_part_text_cursor_geometry_get(wd->ent, "elm.text",
1298               &cx, &cy, &cw, &ch);
1299         elm_widget_show_region_set(data, cx, cy, cw, ch);
1300      }
1301    else
1302      {
1303         wd->deferred_cur = EINA_TRUE;
1304      }
1305 }
1306
1307 static void
1308 _signal_anchor_down(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1309 {
1310    Widget_Data *wd = elm_widget_data_get(data);
1311    if (!wd) return;
1312 }
1313
1314 static void
1315 _signal_anchor_up(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1316 {
1317    Widget_Data *wd = elm_widget_data_get(data);
1318    if (!wd) return;
1319 }
1320
1321 static void
1322 _signal_anchor_clicked(void *data, Evas_Object *obj __UNUSED__, const char *emission, const char *source __UNUSED__)
1323 {
1324    Widget_Data *wd = elm_widget_data_get(data);
1325    Elm_Entry_Anchor_Info ei;
1326    char *buf2, *p, *p2, *n;
1327    if (!wd) return;
1328    p = strrchr(emission, ',');
1329    if (p)
1330      {
1331         const Eina_List *geoms;
1332
1333         n = p + 1;
1334         p2 = p -1;
1335         while (p2 >= emission)
1336           {
1337              if (*p2 == ',') break;
1338              p2--;
1339           }
1340         p2++;
1341         buf2 = alloca(5 + p - p2);
1342         strncpy(buf2, p2, p - p2);
1343         buf2[p - p2] = 0;
1344         ei.name = n;
1345         ei.button = atoi(buf2);
1346         ei.x = ei.y = ei.w = ei.h = 0;
1347         geoms =
1348            edje_object_part_text_anchor_geometry_get(wd->ent, "elm.text", ei.name);
1349         if (geoms)
1350           {
1351              Evas_Textblock_Rectangle *r;
1352              const Eina_List *l;
1353              Evas_Coord px, py, x, y;
1354
1355              evas_object_geometry_get(wd->ent, &x, &y, NULL, NULL);
1356              evas_pointer_output_xy_get(evas_object_evas_get(wd->ent), &px, &py);
1357              EINA_LIST_FOREACH(geoms, l, r)
1358                {
1359                   if (((r->x + x) <= px) && ((r->y + y) <= py) &&
1360                       ((r->x + x + r->w) > px) && ((r->y + y + r->h) > py))
1361                     {
1362                        ei.x = r->x + x;
1363                        ei.y = r->y + y;
1364                        ei.w = r->w;
1365                        ei.h = r->h;
1366                        break;
1367                     }
1368                }
1369           }
1370         if (!wd->disabled)
1371           evas_object_smart_callback_call(data, SIG_ANCHOR_CLICKED, &ei);
1372      }
1373 }
1374
1375 static void
1376 _signal_anchor_move(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1377 {
1378    Widget_Data *wd = elm_widget_data_get(data);
1379    if (!wd) return;
1380 }
1381
1382 static void
1383 _signal_anchor_in(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1384 {
1385    Widget_Data *wd = elm_widget_data_get(data);
1386    if (!wd) return;
1387 }
1388
1389 static void
1390 _signal_anchor_out(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1391 {
1392    Widget_Data *wd = elm_widget_data_get(data);
1393    if (!wd) return;
1394 }
1395
1396 static void
1397 _signal_key_enter(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1398 {
1399    Widget_Data *wd = elm_widget_data_get(data);
1400    if (!wd) return;
1401    evas_object_smart_callback_call(data, SIG_ACTIVATED, NULL);
1402 }
1403
1404 static void
1405 _signal_mouse_down(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1406 {
1407    Widget_Data *wd = elm_widget_data_get(data);
1408    if (!wd) return;
1409    evas_object_smart_callback_call(data, SIG_PRESS, NULL);
1410 }
1411
1412 static void
1413 _signal_mouse_clicked(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1414 {
1415    Widget_Data *wd = elm_widget_data_get(data);
1416    if (!wd) return;
1417    evas_object_smart_callback_call(data, SIG_CLICKED, NULL);
1418 }
1419
1420 static void
1421 _signal_mouse_double(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1422 {
1423    Widget_Data *wd = elm_widget_data_get(data);
1424    if (!wd) return;
1425    evas_object_smart_callback_call(data, SIG_CLICKED_DOUBLE, NULL);
1426 }
1427
1428 #ifdef HAVE_ELEMENTARY_X
1429 static Eina_Bool
1430 _event_selection_notify(void *data, int type __UNUSED__, void *event)
1431 {
1432    Widget_Data *wd = elm_widget_data_get(data);
1433    Ecore_X_Event_Selection_Notify *ev = event;
1434    if (!wd) return ECORE_CALLBACK_PASS_ON;
1435    if ((!wd->selection_asked) && (!wd->drag_selection_asked))
1436      return ECORE_CALLBACK_PASS_ON;
1437
1438    if ((ev->selection == ECORE_X_SELECTION_CLIPBOARD) ||
1439        (ev->selection == ECORE_X_SELECTION_PRIMARY))
1440      {
1441         Ecore_X_Selection_Data_Text *text_data;
1442
1443         text_data = ev->data;
1444         if (text_data->data.content == ECORE_X_SELECTION_CONTENT_TEXT)
1445           {
1446              if (text_data->text)
1447                {
1448                   char *txt = _elm_util_text_to_mkup(text_data->text);
1449
1450                   if (txt)
1451                     {
1452                        elm_entry_entry_insert(data, txt);
1453                        free(txt);
1454                     }
1455                }
1456           }
1457         wd->selection_asked = EINA_FALSE;
1458      }
1459    else if (ev->selection == ECORE_X_SELECTION_XDND)
1460      {
1461         Ecore_X_Selection_Data_Text *text_data;
1462
1463         text_data = ev->data;
1464         if (text_data->data.content == ECORE_X_SELECTION_CONTENT_TEXT)
1465           {
1466              if (text_data->text)
1467                {
1468                   char *txt = _elm_util_text_to_mkup(text_data->text);
1469
1470                   if (txt)
1471                     {
1472                        /* Massive FIXME: this should be at the drag point */
1473                        elm_entry_entry_insert(data, txt);
1474                        free(txt);
1475                     }
1476                }
1477           }
1478         wd->drag_selection_asked = EINA_FALSE;
1479
1480         ecore_x_dnd_send_finished();
1481
1482      }
1483    return ECORE_CALLBACK_PASS_ON;
1484 }
1485
1486 static Eina_Bool
1487 _event_selection_clear(void *data __UNUSED__, int type __UNUSED__, void *event __UNUSED__)
1488 {
1489    Widget_Data *wd = elm_widget_data_get(data);
1490    Ecore_X_Event_Selection_Clear *ev = event;
1491    if (!wd) return ECORE_CALLBACK_PASS_ON;
1492    if (!wd->have_selection) return ECORE_CALLBACK_PASS_ON;
1493    if ((ev->selection == ECORE_X_SELECTION_CLIPBOARD) ||
1494        (ev->selection == ECORE_X_SELECTION_PRIMARY))
1495      {
1496         elm_entry_select_none(data);
1497      }
1498    return ECORE_CALLBACK_PASS_ON;
1499 }
1500
1501 static Eina_Bool
1502 _drag_drop_cb(void *data __UNUSED__, Evas_Object *obj, Elm_Selection_Data *drop)
1503 {
1504    Widget_Data *wd;
1505    Eina_Bool rv;
1506
1507    wd = elm_widget_data_get(obj);
1508    if (!wd) return EINA_FALSE;
1509    printf("Inserting at (%d,%d) %s\n",drop->x,drop->y,(char*)drop->data);
1510
1511    edje_object_part_text_cursor_copy(wd->ent, "elm.text",
1512                                      EDJE_CURSOR_MAIN,/*->*/EDJE_CURSOR_USER);
1513    rv = edje_object_part_text_cursor_coord_set(wd->ent,"elm.text",
1514                                                EDJE_CURSOR_MAIN,drop->x,drop->y);
1515    if (!rv) printf("Warning: Failed to position cursor: paste anyway\n");
1516    elm_entry_entry_insert(obj, drop->data);
1517    edje_object_part_text_cursor_copy(wd->ent, "elm.text",
1518                                      EDJE_CURSOR_USER,/*->*/EDJE_CURSOR_MAIN);
1519
1520    return EINA_TRUE;
1521 }
1522 #endif
1523
1524 static Evas_Object *
1525 _get_item(void *data, Evas_Object *edje __UNUSED__, const char *part __UNUSED__, const char *item)
1526 {
1527    Widget_Data *wd = elm_widget_data_get(data);
1528    Evas_Object *o;
1529    Eina_List *l;
1530    Elm_Entry_Item_Provider *ip;
1531
1532    EINA_LIST_FOREACH(wd->item_providers, l, ip)
1533      {
1534         o = ip->func(ip->data, data, item);
1535         if (o) return o;
1536      }
1537    if (!strncmp(item, "file://", 7))
1538      {
1539         const char *fname = item + 7;
1540
1541         o = evas_object_image_filled_add(evas_object_evas_get(data));
1542         evas_object_image_file_set(o, fname, NULL);
1543         if (evas_object_image_load_error_get(o) == EVAS_LOAD_ERROR_NONE)
1544           {
1545              evas_object_show(o);
1546           }
1547         else
1548           {
1549              evas_object_del(o);
1550              o = edje_object_add(evas_object_evas_get(data));
1551              _elm_theme_object_set(data, o, "entry/emoticon", "wtf", elm_widget_style_get(data));
1552           }
1553         return o;
1554      }
1555    o = edje_object_add(evas_object_evas_get(data));
1556    if (!_elm_theme_object_set(data, o, "entry", item, elm_widget_style_get(data)))
1557      _elm_theme_object_set(data, o, "entry/emoticon", "wtf", elm_widget_style_get(data));
1558    return o;
1559 }
1560
1561 static void
1562 _text_filter(void *data, Evas_Object *edje __UNUSED__, const char *part __UNUSED__, Edje_Text_Filter_Type type, char **text)
1563 {
1564    Widget_Data *wd = elm_widget_data_get(data);
1565    Eina_List *l;
1566    Elm_Entry_Text_Filter *tf;
1567
1568    if (type == EDJE_TEXT_FILTER_FORMAT)
1569      return;
1570
1571    EINA_LIST_FOREACH(wd->text_filters, l, tf)
1572      {
1573         tf->func(tf->data, data, text);
1574         if (!*text)
1575           break;
1576      }
1577 }
1578
1579 /* This function is used to insert text by chunks in jobs */
1580 static Eina_Bool
1581 _text_append_idler(void *data)
1582 {
1583    int start;
1584    char backup;
1585    Evas_Object *obj = (Evas_Object *) data;
1586    Widget_Data *wd = elm_widget_data_get(obj);
1587    if (wd->text) eina_stringshare_del(wd->text);
1588    wd->text = NULL;
1589    wd->changed = EINA_TRUE;
1590
1591    start = wd->append_text_position;
1592    if(start + _CHUNK_SIZE < wd->append_text_len)
1593      {
1594         wd->append_text_position = (start + _CHUNK_SIZE);
1595         /* Go to the start of the nearest codepoint, because we don't want
1596          * to cut it in the middle */
1597         eina_unicode_utf8_get_prev(wd->append_text_left,
1598               &wd->append_text_position);
1599      }
1600    else
1601      {
1602         wd->append_text_position = wd->append_text_len;
1603      }
1604
1605    backup = wd->append_text_left[wd->append_text_position];
1606    wd->append_text_left[wd->append_text_position] = '\0';
1607
1608    edje_object_part_text_append(wd->ent, "elm.text",
1609          wd->append_text_left + start);
1610
1611    wd->append_text_left[wd->append_text_position] = backup;
1612
1613    /* If there's still more to go, renew the idler, else, cleanup */
1614    if (wd->append_text_position < wd->append_text_len)
1615      {
1616         return ECORE_CALLBACK_RENEW;
1617      }
1618    else
1619      {
1620         free(wd->append_text_left);
1621         wd->append_text_left = NULL;
1622         wd->append_text_idler = NULL;
1623         return ECORE_CALLBACK_CANCEL;
1624      }
1625 }
1626
1627 /**
1628  * This adds an entry to @p parent object.
1629  *
1630  * @param parent The parent object
1631  * @return The new object or NULL if it cannot be created
1632  *
1633  * @ingroup Entry
1634  */
1635 EAPI Evas_Object *
1636 elm_entry_add(Evas_Object *parent)
1637 {
1638    Evas_Object *obj, *top;
1639    Evas *e;
1640    Widget_Data *wd;
1641
1642    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
1643
1644    ELM_SET_WIDTYPE(widtype, "entry");
1645    elm_widget_type_set(obj, "entry");
1646    elm_widget_sub_object_add(parent, obj);
1647    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
1648    elm_widget_data_set(obj, wd);
1649    elm_widget_del_hook_set(obj, _del_hook);
1650    elm_widget_del_pre_hook_set(obj, _del_pre_hook);
1651    elm_widget_theme_hook_set(obj, _theme_hook);
1652    elm_widget_disable_hook_set(obj, _disable_hook);
1653    elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
1654    elm_widget_on_focus_region_hook_set(obj, _on_focus_region_hook);
1655    elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
1656    elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
1657    elm_object_cursor_set(obj, ELM_CURSOR_XTERM);
1658    elm_widget_can_focus_set(obj, EINA_TRUE);
1659    elm_widget_highlight_ignore_set(obj, EINA_TRUE);
1660
1661    wd->linewrap     = ELM_WRAP_WORD;
1662    wd->editable     = EINA_TRUE;
1663    wd->disabled     = EINA_FALSE;
1664    wd->context_menu = EINA_TRUE;
1665    wd->autosave     = EINA_TRUE;
1666    wd->textonly     = EINA_FALSE;
1667
1668    wd->ent = edje_object_add(e);
1669    edje_object_item_provider_set(wd->ent, _get_item, obj);
1670    edje_object_text_insert_filter_callback_add(wd->ent,"elm.text", _text_filter, obj);
1671    evas_object_event_callback_add(wd->ent, EVAS_CALLBACK_MOVE, _move, obj);
1672    evas_object_event_callback_add(wd->ent, EVAS_CALLBACK_RESIZE, _resize, obj);
1673    evas_object_event_callback_add(wd->ent, EVAS_CALLBACK_MOUSE_DOWN,
1674                                   _mouse_down, obj);
1675    evas_object_event_callback_add(wd->ent, EVAS_CALLBACK_MOUSE_UP,
1676                                   _mouse_up, obj);
1677    evas_object_event_callback_add(wd->ent, EVAS_CALLBACK_MOUSE_MOVE,
1678                                   _mouse_move, obj);
1679
1680    _elm_theme_object_set(obj, wd->ent, "entry", "base", "default");
1681    edje_object_signal_callback_add(wd->ent, "entry,changed", "elm.text",
1682                                    _signal_entry_changed, obj);
1683    edje_object_signal_callback_add(wd->ent, "selection,start", "elm.text",
1684                                    _signal_selection_start, obj);
1685    edje_object_signal_callback_add(wd->ent, "selection,changed", "elm.text",
1686                                    _signal_selection_changed, obj);
1687    edje_object_signal_callback_add(wd->ent, "selection,cleared", "elm.text",
1688                                    _signal_selection_cleared, obj);
1689    edje_object_signal_callback_add(wd->ent, "entry,paste,request", "elm.text",
1690                                    _signal_entry_paste_request, obj);
1691    edje_object_signal_callback_add(wd->ent, "entry,copy,notify", "elm.text",
1692                                    _signal_entry_copy_notify, obj);
1693    edje_object_signal_callback_add(wd->ent, "entry,cut,notify", "elm.text",
1694                                    _signal_entry_cut_notify, obj);
1695    edje_object_signal_callback_add(wd->ent, "cursor,changed", "elm.text",
1696                                    _signal_cursor_changed, obj);
1697    edje_object_signal_callback_add(wd->ent, "anchor,mouse,down,*", "elm.text",
1698                                    _signal_anchor_down, obj);
1699    edje_object_signal_callback_add(wd->ent, "anchor,mouse,up,*", "elm.text",
1700                                    _signal_anchor_up, obj);
1701    edje_object_signal_callback_add(wd->ent, "anchor,mouse,clicked,*", "elm.text",
1702                                    _signal_anchor_clicked, obj);
1703    edje_object_signal_callback_add(wd->ent, "anchor,mouse,move,*", "elm.text",
1704                                    _signal_anchor_move, obj);
1705    edje_object_signal_callback_add(wd->ent, "anchor,mouse,in,*", "elm.text",
1706                                    _signal_anchor_in, obj);
1707    edje_object_signal_callback_add(wd->ent, "anchor,mouse,out,*", "elm.text",
1708                                    _signal_anchor_out, obj);
1709    edje_object_signal_callback_add(wd->ent, "entry,key,enter", "elm.text",
1710                                    _signal_key_enter, obj);
1711    edje_object_signal_callback_add(wd->ent, "mouse,down,1", "elm.text",
1712                                    _signal_mouse_down, obj);
1713    edje_object_signal_callback_add(wd->ent, "mouse,clicked,1", "elm.text",
1714                                    _signal_mouse_clicked, obj);
1715    edje_object_signal_callback_add(wd->ent, "mouse,down,1,double", "elm.text",
1716                                    _signal_mouse_double, obj);
1717    edje_object_part_text_set(wd->ent, "elm.text", "");
1718    if (_elm_config->desktop_entry)
1719      edje_object_part_text_select_allow_set(wd->ent, "elm.text", EINA_TRUE);
1720    elm_widget_resize_object_set(obj, wd->ent);
1721    _sizing_eval(obj);
1722
1723 #ifdef HAVE_ELEMENTARY_X
1724    top = elm_widget_top_get(obj);
1725    if ((top) && (elm_win_xwindow_get(top)))
1726      {
1727         wd->sel_notify_handler =
1728            ecore_event_handler_add(ECORE_X_EVENT_SELECTION_NOTIFY,
1729                                    _event_selection_notify, obj);
1730         wd->sel_clear_handler =
1731            ecore_event_handler_add(ECORE_X_EVENT_SELECTION_CLEAR,
1732                                    _event_selection_clear, obj);
1733      }
1734
1735    elm_drop_target_add(obj, ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_IMAGE,
1736                        _drag_drop_cb, NULL);
1737 #endif
1738
1739    entries = eina_list_prepend(entries, obj);
1740
1741    // module - find module for entry
1742    wd->api = _module(obj);
1743    // if found - hook in
1744    if ((wd->api) && (wd->api->obj_hook)) wd->api->obj_hook(obj);
1745
1746    _mirrored_set(obj, elm_widget_mirrored_get(obj));
1747    // TODO: convert Elementary to subclassing of Evas_Smart_Class
1748    // TODO: and save some bytes, making descriptions per-class and not instance!
1749    evas_object_smart_callbacks_descriptions_set(obj, _signals);
1750    return obj;
1751 }
1752
1753 /**
1754  * This sets the entry object not to line wrap.  All input will
1755  * be on a single line, and the entry box will extend with user input.
1756  *
1757  * @param obj The entry object
1758  * @param single_line If true, the text in the entry
1759  * will be on a single line.
1760  *
1761  * @ingroup Entry
1762  */
1763 EAPI void
1764 elm_entry_single_line_set(Evas_Object *obj, Eina_Bool single_line)
1765 {
1766    ELM_CHECK_WIDTYPE(obj, widtype);
1767    Widget_Data *wd = elm_widget_data_get(obj);
1768    if (!wd) return;
1769    if (wd->single_line == single_line) return;
1770    wd->single_line = single_line;
1771    wd->linewrap = ELM_WRAP_NONE;
1772    elm_entry_cnp_textonly_set(obj, EINA_TRUE);
1773    _theme_hook(obj);
1774 }
1775
1776 /**
1777  * This returns true if the entry has been set to single line mode.
1778  * See also elm_entry_single_line_set().
1779  *
1780  * @param obj The entry object
1781  * @return single_line If true, the text in the entry is set to display
1782  * on a single line.
1783  *
1784  * @ingroup Entry
1785  */
1786 EAPI Eina_Bool
1787 elm_entry_single_line_get(const Evas_Object *obj)
1788 {
1789    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1790    Widget_Data *wd = elm_widget_data_get(obj);
1791    if (!wd) return EINA_FALSE;
1792    return wd->single_line;
1793 }
1794
1795 /**
1796  * This sets the entry object to password mode.  All text entered
1797  * and/or displayed within the widget will be replaced with asterisks (*).
1798  *
1799  * @param obj The entry object
1800  * @param password If true, password mode is enabled.
1801  *
1802  * @ingroup Entry
1803  */
1804 EAPI void
1805 elm_entry_password_set(Evas_Object *obj, Eina_Bool password)
1806 {
1807    ELM_CHECK_WIDTYPE(obj, widtype);
1808    Widget_Data *wd = elm_widget_data_get(obj);
1809    if (!wd) return;
1810    if (wd->password == password) return;
1811    wd->password = password;
1812    wd->single_line = EINA_TRUE;
1813    wd->linewrap = ELM_WRAP_NONE;
1814    _theme_hook(obj);
1815 }
1816
1817 /**
1818  * This returns whether password mode is enabled.
1819  * See also elm_entry_password_set().
1820  *
1821  * @param obj The entry object
1822  * @return If true, the entry is set to display all characters
1823  * as asterisks (*).
1824  *
1825  * @ingroup Entry
1826  */
1827 EAPI Eina_Bool
1828 elm_entry_password_get(const Evas_Object *obj)
1829 {
1830    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1831    Widget_Data *wd = elm_widget_data_get(obj);
1832    if (!wd) return EINA_FALSE;
1833    return wd->password;
1834 }
1835
1836 /**
1837  * This sets the text displayed within the entry to @p entry.
1838  *
1839  * @param obj The entry object
1840  * @param entry The text to be displayed
1841  *
1842  * @ingroup Entry
1843  */
1844 EAPI void
1845 elm_entry_entry_set(Evas_Object *obj, const char *entry)
1846 {
1847    int len = 0;
1848    ELM_CHECK_WIDTYPE(obj, widtype);
1849    Widget_Data *wd = elm_widget_data_get(obj);
1850    if (!wd) return;
1851    if (!entry) entry = "";
1852    if (wd->text) eina_stringshare_del(wd->text);
1853    wd->text = NULL;
1854    wd->changed = EINA_TRUE;
1855
1856    /* Clear currently pending job if there is one */
1857    if (wd->append_text_idler)
1858      {
1859         ecore_idler_del(wd->append_text_idler);
1860         free(wd->append_text_left);
1861         wd->append_text_left = NULL;
1862         wd->append_text_idler = NULL;
1863      }
1864
1865    len = strlen(entry);
1866    /* Split to ~_CHUNK_SIZE chunks */
1867    if (len <= _CHUNK_SIZE)
1868      {
1869         edje_object_part_text_set(wd->ent, "elm.text", entry);
1870      }
1871    else
1872      {
1873         /* Need to clear the entry first */
1874         edje_object_part_text_set(wd->ent, "elm.text", "");
1875         wd->append_text_left = strndup(entry, len);
1876         wd->append_text_position = 0;
1877         wd->append_text_len = len;
1878         wd->append_text_idler = ecore_idler_add(_text_append_idler, obj);
1879      }
1880 }
1881
1882 /**
1883  * This returns the text currently shown in object @p entry.
1884  * See also elm_entry_entry_set().
1885  *
1886  * @param obj The entry object
1887  * @return The currently displayed text or NULL on failure
1888  *
1889  * @ingroup Entry
1890  */
1891 EAPI const char *
1892 elm_entry_entry_get(const Evas_Object *obj)
1893 {
1894    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1895    Widget_Data *wd = elm_widget_data_get(obj);
1896    const char *text;
1897    if (!wd) return NULL;
1898    if (wd->text) return wd->text;
1899    text = edje_object_part_text_get(wd->ent, "elm.text");
1900    if (!text)
1901      {
1902         ERR("text=NULL for edje %p, part 'elm.text'", wd->ent);
1903         return NULL;
1904      }
1905    eina_stringshare_replace(&wd->text, text);
1906    return wd->text;
1907 }
1908
1909 /**
1910  * This returns EINA_TRUE if the entry is empty/there was an error
1911  * and EINA_FALSE if it is not empty.
1912  *
1913  * @param obj The entry object
1914  * @return If the entry is empty or not.
1915  *
1916  * @ingroup Entry
1917  */
1918 EAPI Eina_Bool
1919 elm_entry_is_empty(const Evas_Object *obj)
1920 {
1921    /* FIXME: until there's support for that in textblock, we just check
1922     * to see if the there is text or not. */
1923    ELM_CHECK_WIDTYPE(obj, widtype) EINA_TRUE;
1924    Widget_Data *wd = elm_widget_data_get(obj);
1925    const Evas_Object *tb;
1926    Evas_Textblock_Cursor *cur;
1927    Eina_Bool ret;
1928    if (!wd) return EINA_TRUE;
1929    /* It's a hack until we get the support suggested above.
1930     * We just create a cursor, point it to the begining, and then
1931     * try to advance it, if it can advance, the tb is not empty,
1932     * otherwise it is. */
1933    tb = edje_object_part_object_get(wd->ent, "elm.text");
1934    cur = evas_object_textblock_cursor_new((Evas_Object *) tb); /* This is
1935                                                                   actually, ok for the time being, thsese hackish stuff will be removed
1936                                                                   once evas 1.0 is out*/
1937    evas_textblock_cursor_pos_set(cur, 0);
1938    ret = evas_textblock_cursor_char_next(cur);
1939    evas_textblock_cursor_free(cur);
1940
1941    return !ret;
1942 }
1943
1944 /**
1945  * This returns all selected text within the entry.
1946  *
1947  * @param obj The entry object
1948  * @return The selected text within the entry or NULL on failure
1949  *
1950  * @ingroup Entry
1951  */
1952 EAPI const char *
1953 elm_entry_selection_get(const Evas_Object *obj)
1954 {
1955    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1956    Widget_Data *wd = elm_widget_data_get(obj);
1957    if (!wd) return NULL;
1958    return edje_object_part_text_selection_get(wd->ent, "elm.text");
1959 }
1960
1961 /**
1962  * This inserts text in @p entry where the current cursor position.
1963  *
1964  * This inserts text at the cursor position is as if it was typed
1965  * by the user (note this also allows markup which a user
1966  * can't just "type" as it would be converted to escaped text, so this
1967  * call can be used to insert things like emoticon items or bold push/pop
1968  * tags, other font and color change tags etc.)
1969  *
1970  * @param obj The entry object
1971  * @param entry The text to insert
1972  *
1973  * @ingroup Entry
1974  */
1975 EAPI void
1976 elm_entry_entry_insert(Evas_Object *obj, const char *entry)
1977 {
1978    ELM_CHECK_WIDTYPE(obj, widtype);
1979    Widget_Data *wd = elm_widget_data_get(obj);
1980    if (!wd) return;
1981    edje_object_part_text_insert(wd->ent, "elm.text", entry);
1982    wd->changed = EINA_TRUE;
1983    _sizing_eval(obj);
1984 }
1985
1986 /**
1987  * This enables word line wrapping in the entry object.  It is the opposite
1988  * of elm_entry_single_line_set().  Additionally, setting this disables
1989  * character line wrapping.
1990  *
1991  * @param obj The entry object
1992  * @param wrap If true, the entry will be wrapped once it reaches the end
1993  * of the object. Wrapping will occur at the end of the word before the end of the
1994  * object.
1995  *
1996  * @ingroup Entry
1997  */
1998 EAPI void
1999 elm_entry_line_wrap_set(Evas_Object *obj, Elm_Wrap_Type wrap)
2000 {
2001    ELM_CHECK_WIDTYPE(obj, widtype);
2002    Widget_Data *wd = elm_widget_data_get(obj);
2003    if (!wd) return;
2004    if (wd->linewrap == wrap) return;
2005
2006    wd->linewrap = wrap;
2007    _theme_hook(obj);
2008 }
2009
2010 /**
2011  * This sets the editable attribute of the entry.
2012  *
2013  * @param obj The entry object
2014  * @param editable If true, the entry will be editable by the user.
2015  * If false, it will be set to the disabled state.
2016  *
2017  * @ingroup Entry
2018  */
2019 EAPI void
2020 elm_entry_editable_set(Evas_Object *obj, Eina_Bool editable)
2021 {
2022    ELM_CHECK_WIDTYPE(obj, widtype);
2023    Widget_Data *wd = elm_widget_data_get(obj);
2024    if (!wd) return;
2025    if (wd->editable == editable) return;
2026    wd->editable = editable;
2027    _theme_hook(obj);
2028
2029 #ifdef HAVE_ELEMENTARY_X
2030    if (editable)
2031      elm_drop_target_add(obj, ELM_SEL_FORMAT_MARKUP, _drag_drop_cb, NULL);
2032    else
2033      elm_drop_target_del(obj);
2034 #endif
2035 }
2036
2037 /**
2038  * This gets the editable attribute of the entry.
2039  * See also elm_entry_editable_set().
2040  *
2041  * @param obj The entry object
2042  * @return If true, the entry is editable by the user.
2043  * If false, it is not editable by the user
2044  *
2045  * @ingroup Entry
2046  */
2047 EAPI Eina_Bool
2048 elm_entry_editable_get(const Evas_Object *obj)
2049 {
2050    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
2051    Widget_Data *wd = elm_widget_data_get(obj);
2052    if (!wd) return EINA_FALSE;
2053    return wd->editable;
2054 }
2055
2056 /**
2057  * This drops any existing text selection within the entry.
2058  *
2059  * @param obj The entry object
2060  *
2061  * @ingroup Entry
2062  */
2063 EAPI void
2064 elm_entry_select_none(Evas_Object *obj)
2065 {
2066    ELM_CHECK_WIDTYPE(obj, widtype);
2067    Widget_Data *wd = elm_widget_data_get(obj);
2068    if (!wd) return;
2069    if (wd->selmode)
2070      {
2071         wd->selmode = EINA_FALSE;
2072         if (!_elm_config->desktop_entry)
2073           edje_object_part_text_select_allow_set(wd->ent, "elm.text", EINA_FALSE);
2074         edje_object_signal_emit(wd->ent, "elm,state,select,off", "elm");
2075      }
2076    wd->have_selection = EINA_FALSE;
2077    edje_object_part_text_select_none(wd->ent, "elm.text");
2078 }
2079
2080 /**
2081  * This selects all text within the entry.
2082  *
2083  * @param obj The entry object
2084  *
2085  * @ingroup Entry
2086  */
2087 EAPI void
2088 elm_entry_select_all(Evas_Object *obj)
2089 {
2090    ELM_CHECK_WIDTYPE(obj, widtype);
2091    Widget_Data *wd = elm_widget_data_get(obj);
2092    if (!wd) return;
2093    if (wd->selmode)
2094      {
2095         wd->selmode = EINA_FALSE;
2096         if (!_elm_config->desktop_entry)
2097           edje_object_part_text_select_allow_set(wd->ent, "elm.text", EINA_FALSE);
2098         edje_object_signal_emit(wd->ent, "elm,state,select,off", "elm");
2099      }
2100    wd->have_selection = EINA_TRUE;
2101    edje_object_part_text_select_all(wd->ent, "elm.text");
2102 }
2103
2104 /**
2105  * This function returns the geometry of the cursor.
2106  *
2107  * It's useful if you want to draw something on the cursor (or where it is),
2108  * or for example in the case of scrolled entry where you want to show the
2109  * cursor.
2110  *
2111  * @param obj The entry object
2112  * @param x returned geometry
2113  * @param y returned geometry
2114  * @param w returned geometry
2115  * @param h returned geometry
2116  * @return EINA_TRUE upon success, EINA_FALSE upon failure
2117  *
2118  * @ingroup Entry
2119  */
2120 EAPI Eina_Bool
2121 elm_entry_cursor_geometry_get(const Evas_Object *obj, Evas_Coord *x, Evas_Coord *y, Evas_Coord *w, Evas_Coord *h)
2122 {
2123    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
2124    Widget_Data *wd = elm_widget_data_get(obj);
2125    if (!wd) return EINA_FALSE;
2126    edje_object_part_text_cursor_geometry_get(wd->ent, "elm.text", x, y, w, h);
2127    return EINA_TRUE;
2128 }
2129
2130 /**
2131  * This moves the cursor one place to the right within the entry.
2132  *
2133  * @param obj The entry object
2134  * @return EINA_TRUE upon success, EINA_FALSE upon failure
2135  *
2136  * @ingroup Entry
2137  */
2138 EAPI Eina_Bool
2139 elm_entry_cursor_next(Evas_Object *obj)
2140 {
2141    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
2142    Widget_Data *wd = elm_widget_data_get(obj);
2143    if (!wd) return EINA_FALSE;
2144    return edje_object_part_text_cursor_next(wd->ent, "elm.text", EDJE_CURSOR_MAIN);
2145 }
2146
2147 /**
2148  * This moves the cursor one place to the left within the entry.
2149  *
2150  * @param obj The entry object
2151  * @return EINA_TRUE upon success, EINA_FALSE upon failure
2152  *
2153  * @ingroup Entry
2154  */
2155 EAPI Eina_Bool
2156 elm_entry_cursor_prev(Evas_Object *obj)
2157 {
2158    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
2159    Widget_Data *wd = elm_widget_data_get(obj);
2160    if (!wd) return EINA_FALSE;
2161    return edje_object_part_text_cursor_prev(wd->ent, "elm.text", EDJE_CURSOR_MAIN);
2162 }
2163
2164 /**
2165  * This moves the cursor one line up within the entry.
2166  *
2167  * @param obj The entry object
2168  * @return EINA_TRUE upon success, EINA_FALSE upon failure
2169  *
2170  * @ingroup Entry
2171  */
2172 EAPI Eina_Bool
2173 elm_entry_cursor_up(Evas_Object *obj)
2174 {
2175    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
2176    Widget_Data *wd = elm_widget_data_get(obj);
2177    if (!wd) return EINA_FALSE;
2178    return edje_object_part_text_cursor_up(wd->ent, "elm.text", EDJE_CURSOR_MAIN);
2179 }
2180
2181 /**
2182  * This moves the cursor one line down within the entry.
2183  *
2184  * @param obj The entry object
2185  * @return EINA_TRUE upon success, EINA_FALSE upon failure
2186  *
2187  * @ingroup Entry
2188  */
2189 EAPI Eina_Bool
2190 elm_entry_cursor_down(Evas_Object *obj)
2191 {
2192    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
2193    Widget_Data *wd = elm_widget_data_get(obj);
2194    if (!wd) return EINA_FALSE;
2195    return edje_object_part_text_cursor_down(wd->ent, "elm.text", EDJE_CURSOR_MAIN);
2196 }
2197
2198 /**
2199  * This moves the cursor to the beginning of the entry.
2200  *
2201  * @param obj The entry object
2202  *
2203  * @ingroup Entry
2204  */
2205 EAPI void
2206 elm_entry_cursor_begin_set(Evas_Object *obj)
2207 {
2208    ELM_CHECK_WIDTYPE(obj, widtype);
2209    Widget_Data *wd = elm_widget_data_get(obj);
2210    if (!wd) return;
2211    edje_object_part_text_cursor_begin_set(wd->ent, "elm.text", EDJE_CURSOR_MAIN);
2212 }
2213
2214 /**
2215  * This moves the cursor to the end of the entry.
2216  *
2217  * @param obj The entry object
2218  *
2219  * @ingroup Entry
2220  */
2221 EAPI void
2222 elm_entry_cursor_end_set(Evas_Object *obj)
2223 {
2224    ELM_CHECK_WIDTYPE(obj, widtype);
2225    Widget_Data *wd = elm_widget_data_get(obj);
2226    if (!wd) return;
2227    edje_object_part_text_cursor_end_set(wd->ent, "elm.text", EDJE_CURSOR_MAIN);
2228 }
2229
2230 /**
2231  * This moves the cursor to the beginning of the current line.
2232  *
2233  * @param obj The entry object
2234  *
2235  * @ingroup Entry
2236  */
2237 EAPI void
2238 elm_entry_cursor_line_begin_set(Evas_Object *obj)
2239 {
2240    ELM_CHECK_WIDTYPE(obj, widtype);
2241    Widget_Data *wd = elm_widget_data_get(obj);
2242    if (!wd) return;
2243    edje_object_part_text_cursor_line_begin_set(wd->ent, "elm.text", EDJE_CURSOR_MAIN);
2244 }
2245
2246 /**
2247  * This moves the cursor to the end of the current line.
2248  *
2249  * @param obj The entry object
2250  *
2251  * @ingroup Entry
2252  */
2253 EAPI void
2254 elm_entry_cursor_line_end_set(Evas_Object *obj)
2255 {
2256    ELM_CHECK_WIDTYPE(obj, widtype);
2257    Widget_Data *wd = elm_widget_data_get(obj);
2258    if (!wd) return;
2259    edje_object_part_text_cursor_line_end_set(wd->ent, "elm.text", EDJE_CURSOR_MAIN);
2260 }
2261
2262 /**
2263  * This begins a selection within the entry as though
2264  * the user were holding down the mouse button to make a selection.
2265  *
2266  * @param obj The entry object
2267  *
2268  * @ingroup Entry
2269  */
2270 EAPI void
2271 elm_entry_cursor_selection_begin(Evas_Object *obj)
2272 {
2273    ELM_CHECK_WIDTYPE(obj, widtype);
2274    Widget_Data *wd = elm_widget_data_get(obj);
2275    if (!wd) return;
2276    edje_object_part_text_select_begin(wd->ent, "elm.text");
2277 }
2278
2279 /**
2280  * This ends a selection within the entry as though
2281  * the user had just released the mouse button while making a selection.
2282  *
2283  * @param obj The entry object
2284  *
2285  * @ingroup Entry
2286  */
2287 EAPI void
2288 elm_entry_cursor_selection_end(Evas_Object *obj)
2289 {
2290    ELM_CHECK_WIDTYPE(obj, widtype);
2291    Widget_Data *wd = elm_widget_data_get(obj);
2292    if (!wd) return;
2293    edje_object_part_text_select_extend(wd->ent, "elm.text");
2294 }
2295
2296 /**
2297  * TODO: fill this in
2298  *
2299  * @param obj The entry object
2300  * @return TODO: fill this in
2301  *
2302  * @ingroup Entry
2303  */
2304 EAPI Eina_Bool
2305 elm_entry_cursor_is_format_get(const Evas_Object *obj)
2306 {
2307    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
2308    Widget_Data *wd = elm_widget_data_get(obj);
2309    if (!wd) return EINA_FALSE;
2310    return edje_object_part_text_cursor_is_format_get(wd->ent, "elm.text", EDJE_CURSOR_MAIN);
2311 }
2312
2313 /**
2314  * This returns whether the cursor is visible.
2315  *
2316  * @param obj The entry object
2317  * @return If true, the cursor is visible.
2318  *
2319  * @ingroup Entry
2320  */
2321 EAPI Eina_Bool
2322 elm_entry_cursor_is_visible_format_get(const Evas_Object *obj)
2323 {
2324    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
2325    Widget_Data *wd = elm_widget_data_get(obj);
2326    if (!wd) return EINA_FALSE;
2327    return edje_object_part_text_cursor_is_visible_format_get(wd->ent, "elm.text", EDJE_CURSOR_MAIN);
2328 }
2329
2330 /**
2331  * TODO: fill this in
2332  *
2333  * @param obj The entry object
2334  * @return TODO: fill this in
2335  *
2336  * @ingroup Entry
2337  */
2338 EAPI const char *
2339 elm_entry_cursor_content_get(const Evas_Object *obj)
2340 {
2341    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
2342    Widget_Data *wd = elm_widget_data_get(obj);
2343    if (!wd) return NULL;
2344    return edje_object_part_text_cursor_content_get(wd->ent, "elm.text", EDJE_CURSOR_MAIN);
2345 }
2346
2347 /**
2348  * Sets the cursor position in the entry to the given value
2349  *
2350  * @param obj The entry object
2351  * @param pos The position of the cursor
2352  *
2353  * @ingroup Entry
2354  */
2355 EAPI void
2356 elm_entry_cursor_pos_set(Evas_Object *obj, int pos)
2357 {
2358    ELM_CHECK_WIDTYPE(obj, widtype);
2359    Widget_Data *wd = elm_widget_data_get(obj);
2360    if (!wd) return;
2361    edje_object_part_text_cursor_pos_set(wd->ent, "elm.text", EDJE_CURSOR_MAIN, pos);
2362    edje_object_message_signal_process(wd->ent);
2363 }
2364
2365 /**
2366  * Retrieves the current position of the cursor in the entry
2367  *
2368  * @param obj The entry object
2369  * @return The cursor position
2370  *
2371  * @ingroup Entry
2372  */
2373 EAPI int
2374 elm_entry_cursor_pos_get(const Evas_Object *obj)
2375 {
2376    ELM_CHECK_WIDTYPE(obj, widtype) 0;
2377    Widget_Data *wd = elm_widget_data_get(obj);
2378    if (!wd) return 0;
2379    return edje_object_part_text_cursor_pos_get(wd->ent, "elm.text", EDJE_CURSOR_MAIN);
2380 }
2381
2382 /**
2383  * This executes a "cut" action on the selected text in the entry.
2384  *
2385  * @param obj The entry object
2386  *
2387  * @ingroup Entry
2388  */
2389 EAPI void
2390 elm_entry_selection_cut(Evas_Object *obj)
2391 {
2392    ELM_CHECK_WIDTYPE(obj, widtype);
2393    Widget_Data *wd = elm_widget_data_get(obj);
2394    if (!wd) return;
2395    _cut(obj, NULL, NULL);
2396 }
2397
2398 /**
2399  * This executes a "copy" action on the selected text in the entry.
2400  *
2401  * @param obj The entry object
2402  *
2403  * @ingroup Entry
2404  */
2405 EAPI void
2406 elm_entry_selection_copy(Evas_Object *obj)
2407 {
2408    ELM_CHECK_WIDTYPE(obj, widtype);
2409    Widget_Data *wd = elm_widget_data_get(obj);
2410    if (!wd) return;
2411    _copy(obj, NULL, NULL);
2412 }
2413
2414 /**
2415  * This executes a "paste" action in the entry.
2416  *
2417  * @param obj The entry object
2418  *
2419  * @ingroup Entry
2420  */
2421 EAPI void
2422 elm_entry_selection_paste(Evas_Object *obj)
2423 {
2424    ELM_CHECK_WIDTYPE(obj, widtype);
2425    Widget_Data *wd = elm_widget_data_get(obj);
2426    if (!wd) return;
2427    _paste(obj, NULL, NULL);
2428 }
2429
2430 /**
2431  * This clears and frees the items in a entry's contextual (right click) menu.
2432  *
2433  * @param obj The entry object
2434  *
2435  * @ingroup Entry
2436  */
2437 EAPI void
2438 elm_entry_context_menu_clear(Evas_Object *obj)
2439 {
2440    ELM_CHECK_WIDTYPE(obj, widtype);
2441    Widget_Data *wd = elm_widget_data_get(obj);
2442    Elm_Entry_Context_Menu_Item *it;
2443    if (!wd) return;
2444    EINA_LIST_FREE(wd->items, it)
2445      {
2446         eina_stringshare_del(it->label);
2447         eina_stringshare_del(it->icon_file);
2448         eina_stringshare_del(it->icon_group);
2449         free(it);
2450      }
2451 }
2452
2453 /**
2454  * This adds an item to the entry's contextual menu.
2455  *
2456  * @param obj The entry object
2457  * @param label The item's text label
2458  * @param icon_file The item's icon file
2459  * @param icon_type The item's icon type
2460  * @param func The callback to execute when the item is clicked
2461  * @param data The data to associate with the item for related functions
2462  *
2463  * @ingroup Entry
2464  */
2465 EAPI void
2466 elm_entry_context_menu_item_add(Evas_Object *obj, const char *label, const char *icon_file, Elm_Icon_Type icon_type, Evas_Smart_Cb func, const void *data)
2467 {
2468    ELM_CHECK_WIDTYPE(obj, widtype);
2469    Widget_Data *wd = elm_widget_data_get(obj);
2470    Elm_Entry_Context_Menu_Item *it;
2471    if (!wd) return;
2472    it = calloc(1, sizeof(Elm_Entry_Context_Menu_Item));
2473    if (!it) return;
2474    wd->items = eina_list_append(wd->items, it);
2475    it->obj = obj;
2476    it->label = eina_stringshare_add(label);
2477    it->icon_file = eina_stringshare_add(icon_file);
2478    it->icon_type = icon_type;
2479    it->func = func;
2480    it->data = (void *)data;
2481 }
2482
2483 /**
2484  * This disables the entry's contextual (right click) menu.
2485  *
2486  * @param obj The entry object
2487  * @param disabled If true, the menu is disabled
2488  *
2489  * @ingroup Entry
2490  */
2491 EAPI void
2492 elm_entry_context_menu_disabled_set(Evas_Object *obj, Eina_Bool disabled)
2493 {
2494    ELM_CHECK_WIDTYPE(obj, widtype);
2495    Widget_Data *wd = elm_widget_data_get(obj);
2496    if (!wd) return;
2497    if (wd->context_menu == !disabled) return;
2498    wd->context_menu = !disabled;
2499 }
2500
2501 /**
2502  * This returns whether the entry's contextual (right click) menu is disabled.
2503  *
2504  * @param obj The entry object
2505  * @return If true, the menu is disabled
2506  *
2507  * @ingroup Entry
2508  */
2509 EAPI Eina_Bool
2510 elm_entry_context_menu_disabled_get(const Evas_Object *obj)
2511 {
2512    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
2513    Widget_Data *wd = elm_widget_data_get(obj);
2514    if (!wd) return EINA_FALSE;
2515    return !wd->context_menu;
2516 }
2517
2518 /**
2519  * This appends a custom item provider to the list for that entry
2520  *
2521  * This appends the given callback. The list is walked from beginning to end
2522  * with each function called given the item href string in the text. If the
2523  * function returns an object handle other than NULL (it should create an
2524  * and object to do this), then this object is used to replace that item. If
2525  * not the next provider is called until one provides an item object, or the
2526  * default provider in entry does.
2527  *
2528  * @param obj The entry object
2529  * @param func The function called to provide the item object
2530  * @param data The data passed to @p func
2531  *
2532  * @ingroup Entry
2533  */
2534 EAPI void
2535 elm_entry_item_provider_append(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *entry, const char *item), void *data)
2536 {
2537    ELM_CHECK_WIDTYPE(obj, widtype);
2538    Widget_Data *wd = elm_widget_data_get(obj);
2539    if (!wd) return;
2540    EINA_SAFETY_ON_NULL_RETURN(func);
2541    Elm_Entry_Item_Provider *ip = calloc(1, sizeof(Elm_Entry_Item_Provider));
2542    if (!ip) return;
2543    ip->func = func;
2544    ip->data = data;
2545    wd->item_providers = eina_list_append(wd->item_providers, ip);
2546 }
2547
2548 /**
2549  * This prepends a custom item provider to the list for that entry
2550  *
2551  * This prepends the given callback. See elm_entry_item_provider_append() for
2552  * more information
2553  *
2554  * @param obj The entry object
2555  * @param func The function called to provide the item object
2556  * @param data The data passed to @p func
2557  *
2558  * @ingroup Entry
2559  */
2560 EAPI void
2561 elm_entry_item_provider_prepend(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *entry, const char *item), void *data)
2562 {
2563    ELM_CHECK_WIDTYPE(obj, widtype);
2564    Widget_Data *wd = elm_widget_data_get(obj);
2565    if (!wd) return;
2566    EINA_SAFETY_ON_NULL_RETURN(func);
2567    Elm_Entry_Item_Provider *ip = calloc(1, sizeof(Elm_Entry_Item_Provider));
2568    if (!ip) return;
2569    ip->func = func;
2570    ip->data = data;
2571    wd->item_providers = eina_list_prepend(wd->item_providers, ip);
2572 }
2573
2574 /**
2575  * This removes a custom item provider to the list for that entry
2576  *
2577  * This removes the given callback. See elm_entry_item_provider_append() for
2578  * more information
2579  *
2580  * @param obj The entry object
2581  * @param func The function called to provide the item object
2582  * @param data The data passed to @p func
2583  *
2584  * @ingroup Entry
2585  */
2586 EAPI void
2587 elm_entry_item_provider_remove(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *entry, const char *item), void *data)
2588 {
2589    ELM_CHECK_WIDTYPE(obj, widtype);
2590    Widget_Data *wd = elm_widget_data_get(obj);
2591    Eina_List *l;
2592    Elm_Entry_Item_Provider *ip;
2593    if (!wd) return;
2594    EINA_SAFETY_ON_NULL_RETURN(func);
2595    EINA_LIST_FOREACH(wd->item_providers, l, ip)
2596      {
2597         if ((ip->func == func) && ((!data) || (ip->data == data)))
2598           {
2599              wd->item_providers = eina_list_remove_list(wd->item_providers, l);
2600              free(ip);
2601              return;
2602           }
2603      }
2604 }
2605
2606 /**
2607  * Append a filter function for text inserted in the entry
2608  *
2609  * Append the given callback to the list. This functions will be called
2610  * whenever any text is inserted into the entry, with the text to be inserted
2611  * as a parameter. The callback function is free to alter the text in any way
2612  * it wants, but it must remember to free the given pointer and update it.
2613  * If the new text is to be discarded, the function can free it and set it text
2614  * parameter to NULL. This will also prevent any following filters from being
2615  * called.
2616  *
2617  * @param obj The entry object
2618  * @param func The function to use as text filter
2619  * @param data User data to pass to @p func
2620  *
2621  * @ingroup Entry
2622  */
2623 EAPI void
2624 elm_entry_text_filter_append(Evas_Object *obj, void (*func) (void *data, Evas_Object *entry, char **text), void *data)
2625 {
2626    Widget_Data *wd;
2627    Elm_Entry_Text_Filter *tf;
2628    ELM_CHECK_WIDTYPE(obj, widtype);
2629
2630    wd = elm_widget_data_get(obj);
2631
2632    EINA_SAFETY_ON_NULL_RETURN(func);
2633
2634    tf = _filter_new(func, data);
2635    if (!tf) return;
2636
2637    wd->text_filters = eina_list_append(wd->text_filters, tf);
2638 }
2639
2640 /**
2641  * Prepend a filter function for text insdrted in the entry
2642  *
2643  * Prepend the given callback to the list. See elm_entry_text_filter_append()
2644  * for more information
2645  *
2646  * @param obj The entry object
2647  * @param func The function to use as text filter
2648  * @param data User data to pass to @p func
2649  *
2650  * @ingroup Entry
2651  */
2652 EAPI void
2653 elm_entry_text_filter_prepend(Evas_Object *obj, void (*func) (void *data, Evas_Object *entry, char **text), void *data)
2654 {
2655    Widget_Data *wd;
2656    Elm_Entry_Text_Filter *tf;
2657    ELM_CHECK_WIDTYPE(obj, widtype);
2658
2659    wd = elm_widget_data_get(obj);
2660
2661    EINA_SAFETY_ON_NULL_RETURN(func);
2662
2663    tf = _filter_new(func, data);
2664    if (!tf) return;
2665
2666    wd->text_filters = eina_list_prepend(wd->text_filters, tf);
2667 }
2668
2669 /**
2670  * Remove a filter from the list
2671  *
2672  * Removes the given callback from the filter list. See elm_entry_text_filter_append()
2673  * for more information.
2674  *
2675  * @param obj The entry object
2676  * @param func The filter function to remove
2677  * @param data The user data passed when adding the function
2678  *
2679  * @ingroup Entry
2680  */
2681 EAPI void
2682 elm_entry_text_filter_remove(Evas_Object *obj, void (*func) (void *data, Evas_Object *entry, char **text), void *data)
2683 {
2684    Widget_Data *wd;
2685    Eina_List *l;
2686    Elm_Entry_Text_Filter *tf;
2687    ELM_CHECK_WIDTYPE(obj, widtype);
2688
2689    wd = elm_widget_data_get(obj);
2690
2691    EINA_SAFETY_ON_NULL_RETURN(func);
2692
2693    EINA_LIST_FOREACH(wd->text_filters, l, tf)
2694      {
2695         if ((tf->func == func) && ((!data) || (tf->data == data)))
2696           {
2697              wd->text_filters = eina_list_remove_list(wd->text_filters, l);
2698              _filter_free(tf);
2699              return;
2700           }
2701      }
2702 }
2703
2704 /**
2705  * This converts a markup (HTML-like) string into UTF-8.
2706  * Returning string is obtained with malloc.
2707  * After use the returned string, it should be freed.
2708  *
2709  * @param s The string (in markup) to be converted
2710  * @return The converted string (in UTF-8). It should be freed.
2711  *
2712  * @ingroup Entry
2713  */
2714 EAPI char *
2715 elm_entry_markup_to_utf8(const char *s)
2716 {
2717    char *ss = _elm_util_mkup_to_text(s);
2718    if (!ss) ss = strdup("");
2719    return ss;
2720 }
2721
2722 /**
2723  * This converts a UTF-8 string into markup (HTML-like).
2724  * Returning string is obtained with malloc.
2725  * After use the returned string, it should be freed.
2726  *
2727  * @param s The string (in UTF-8) to be converted
2728  * @return The converted string (in markup). It should be freed.
2729  *
2730  * @ingroup Entry
2731  */
2732 EAPI char *
2733 elm_entry_utf8_to_markup(const char *s)
2734 {
2735    char *ss = _elm_util_text_to_mkup(s);
2736    if (!ss) ss = strdup("");
2737    return ss;
2738 }
2739
2740 /**
2741  * Filter inserted text based on user defined character and byte limits
2742  *
2743  * Add this filter to an entry to limit the characters that it will accept
2744  * based the the contents of the provided Elm_Entry_Filter_Limit_Size.
2745  * The funtion works on the UTF-8 representation of the string, converting
2746  * it from the set markup, thus not accounting for any format in it.
2747  *
2748  * The user must create an Elm_Entry_Filter_Limit_Size structure and pass
2749  * it as data when setting the filter. In it it's possible to set limits
2750  * by character count or bytes (any of them is disabled if 0), and both can
2751  * be set at the same time. In that case, it first checks for characters,
2752  * then bytes.
2753  *
2754  * The function will cut the inserted text in order to allow only the first
2755  * number of characters that are still allowed. The cut is made in
2756  * characters, even when limiting by bytes, in order to always contain
2757  * valid ones and avoid half unicode characters making it in.
2758  *
2759  * @ingroup Entry
2760  */
2761 EAPI void
2762 elm_entry_filter_limit_size(void *data, Evas_Object *entry, char **text)
2763 {
2764    Elm_Entry_Filter_Limit_Size *lim = data;
2765    char *current;
2766    int len, newlen;
2767    const char *(*text_get)(const Evas_Object *);
2768    const char *widget_type;
2769
2770    EINA_SAFETY_ON_NULL_RETURN(data);
2771    EINA_SAFETY_ON_NULL_RETURN(entry);
2772    EINA_SAFETY_ON_NULL_RETURN(text);
2773
2774    /* hack. I don't want to copy the entire function to work with
2775     * scrolled_entry */
2776    widget_type = elm_widget_type_get(entry);
2777    if (!strcmp(widget_type, "entry"))
2778      text_get = elm_entry_entry_get;
2779    else if (!strcmp(widget_type, "scrolled_entry"))
2780      text_get = elm_scrolled_entry_entry_get;
2781    else /* huh? */
2782      return;
2783
2784    current = elm_entry_markup_to_utf8(text_get(entry));
2785
2786    if (lim->max_char_count > 0)
2787      {
2788         int cut;
2789         len = evas_string_char_len_get(current);
2790         if (len >= lim->max_char_count)
2791           {
2792              free(*text);
2793              free(current);
2794              *text = NULL;
2795              return;
2796           }
2797         newlen = evas_string_char_len_get(*text);
2798         cut = strlen(*text);
2799         while ((len + newlen) > lim->max_char_count)
2800           {
2801              cut = evas_string_char_prev_get(*text, cut, NULL);
2802              newlen--;
2803           }
2804         (*text)[cut] = 0;
2805      }
2806
2807    if (lim->max_byte_count > 0)
2808      {
2809         len = strlen(current);
2810         if (len >= lim->max_byte_count)
2811           {
2812              free(*text);
2813              free(current);
2814              *text = NULL;
2815              return;
2816           }
2817         newlen = strlen(*text);
2818         while ((len + newlen) > lim->max_byte_count)
2819           {
2820              int p = evas_string_char_prev_get(*text, newlen, NULL);
2821              newlen -= (newlen - p);
2822           }
2823         if (newlen)
2824           (*text)[newlen] = 0;
2825         else
2826           {
2827              free(*text);
2828              *text = NULL;
2829           }
2830      }
2831    free(current);
2832 }
2833
2834 /**
2835  * Filter inserted text based on accepted or rejected sets of characters
2836  *
2837  * Add this filter to an entry to restrict the set of accepted characters
2838  * based on the sets in the provided Elm_Entry_Filter_Accept_Set.
2839  * This structure contains both accepted and rejected sets, but they are
2840  * mutually exclusive. If accepted is set, it will be used, otherwise it
2841  * goes on to the rejected set.
2842  */
2843 EAPI void
2844 elm_entry_filter_accept_set(void *data, Evas_Object *entry __UNUSED__, char **text)
2845 {
2846    Elm_Entry_Filter_Accept_Set *as = data;
2847    const char *set;
2848    char *insert;
2849    Eina_Bool goes_in;
2850    int read_idx, last_read_idx = 0, read_char;
2851
2852    EINA_SAFETY_ON_NULL_RETURN(data);
2853    EINA_SAFETY_ON_NULL_RETURN(text);
2854
2855    if ((!as->accepted) && (!as->rejected))
2856      return;
2857
2858    if (as->accepted)
2859      {
2860         set = as->accepted;
2861         goes_in = EINA_TRUE;
2862      }
2863    else
2864      {
2865         set = as->rejected;
2866         goes_in = EINA_FALSE;
2867      }
2868
2869    insert = *text;
2870    read_idx = evas_string_char_next_get(*text, 0, &read_char);
2871    while (read_char)
2872      {
2873         int cmp_idx, cmp_char;
2874         Eina_Bool in_set = EINA_FALSE;
2875
2876         cmp_idx = evas_string_char_next_get(set, 0, &cmp_char);
2877         while (cmp_char)
2878           {
2879              if (read_char == cmp_char)
2880                {
2881                   in_set = EINA_TRUE;
2882                   break;
2883                }
2884              cmp_idx = evas_string_char_next_get(set, cmp_idx, &cmp_char);
2885           }
2886         if (in_set == goes_in)
2887           {
2888              int size = read_idx - last_read_idx;
2889              const char *src = (*text) + last_read_idx;
2890              if (src != insert)
2891                memcpy(insert, *text + last_read_idx, size);
2892              insert += size;
2893           }
2894         last_read_idx = read_idx;
2895         read_idx = evas_string_char_next_get(*text, read_idx, &read_char);
2896      }
2897    *insert = 0;
2898 }
2899
2900 /**
2901  * This sets the file (and implicitly loads it) for the text to display and
2902  * then edit. All changes are written back to the file after a short delay if
2903  * the entry object is set to autosave.
2904  *
2905  * @param obj The entry object
2906  * @param file The path to the file to load and save
2907  * @param format The file format
2908  *
2909  * @ingroup Entry
2910  */
2911 EAPI void
2912 elm_entry_file_set(Evas_Object *obj, const char *file, Elm_Text_Format format)
2913 {
2914    ELM_CHECK_WIDTYPE(obj, widtype);
2915    Widget_Data *wd = elm_widget_data_get(obj);
2916    if (!wd) return;
2917    if (wd->delay_write)
2918      {
2919         ecore_timer_del(wd->delay_write);
2920         wd->delay_write = NULL;
2921      }
2922    if (wd->autosave) _save(obj);
2923    eina_stringshare_replace(&wd->file, file);
2924    wd->format = format;
2925    _load(obj);
2926 }
2927
2928 /**
2929  * Gets the file to load and save and the file format
2930  *
2931  * @param obj The entry object
2932  * @param file The path to the file to load and save
2933  * @param format The file format
2934  *
2935  * @ingroup Entry
2936  */
2937 EAPI void
2938 elm_entry_file_get(const Evas_Object *obj, const char **file, Elm_Text_Format *format)
2939 {
2940    ELM_CHECK_WIDTYPE(obj, widtype);
2941    Widget_Data *wd = elm_widget_data_get(obj);
2942    if (!wd) return;
2943    if (file) *file = wd->file;
2944    if (format) *format = wd->format;
2945 }
2946
2947 /**
2948  * This function writes any changes made to the file set with
2949  * elm_entry_file_set()
2950  *
2951  * @param obj The entry object
2952  *
2953  * @ingroup Entry
2954  */
2955 EAPI void
2956 elm_entry_file_save(Evas_Object *obj)
2957 {
2958    ELM_CHECK_WIDTYPE(obj, widtype);
2959    Widget_Data *wd = elm_widget_data_get(obj);
2960    if (!wd) return;
2961    if (wd->delay_write)
2962      {
2963         ecore_timer_del(wd->delay_write);
2964         wd->delay_write = NULL;
2965      }
2966    _save(obj);
2967    wd->delay_write = ecore_timer_add(2.0, _delay_write, obj);
2968 }
2969
2970 /**
2971  * This sets the entry object to 'autosave' the loaded text file or not.
2972  *
2973  * @param obj The entry object
2974  * @param autosave Autosave the loaded file or not
2975  *
2976  * @ingroup Entry
2977  */
2978 EAPI void
2979 elm_entry_autosave_set(Evas_Object *obj, Eina_Bool autosave)
2980 {
2981    ELM_CHECK_WIDTYPE(obj, widtype);
2982    Widget_Data *wd = elm_widget_data_get(obj);
2983    if (!wd) return;
2984    wd->autosave = !!autosave;
2985 }
2986
2987 /**
2988  * This gets the entry object's 'autosave' status.
2989  *
2990  * @param obj The entry object
2991  * @return Autosave the loaded file or not
2992  *
2993  * @ingroup Entry
2994  */
2995 EAPI Eina_Bool
2996 elm_entry_autosave_get(const Evas_Object *obj)
2997 {
2998    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
2999    Widget_Data *wd = elm_widget_data_get(obj);
3000    if (!wd) return EINA_FALSE;
3001    return wd->autosave;
3002 }
3003
3004 /**
3005  * Control pasting of text and images for the widget.
3006  *
3007  * Normally the entry allows both text and images to be pasted.  By setting
3008  * textonly to be true, this prevents images from being pasted.
3009  *
3010  * Note this only changes the behaviour of text.
3011  *
3012  * @param obj The entry object
3013  * @param textonly paste mode - EINA_TRUE is text only, EINA_FALSE is text+image+other.
3014  *
3015  * @ingroup Entry
3016  */
3017 EAPI void
3018 elm_entry_cnp_textonly_set(Evas_Object *obj, Eina_Bool textonly)
3019 {
3020    Elm_Sel_Format format = ELM_SEL_FORMAT_MARKUP;
3021    ELM_CHECK_WIDTYPE(obj, widtype);
3022    Widget_Data *wd = elm_widget_data_get(obj);
3023    if (!wd) return;
3024    textonly = !!textonly;
3025    if (wd->textonly == textonly) return;
3026    wd->textonly = !!textonly;
3027    if (!textonly) format |= ELM_SEL_FORMAT_IMAGE;
3028 #ifdef HAVE_ELEMENTARY_X
3029    elm_drop_target_add(obj, format, _drag_drop_cb, NULL);
3030 #endif
3031 }
3032
3033 /**
3034  * Getting elm_entry text paste/drop mode.
3035  *
3036  * In textonly mode, only text may be pasted or dropped into the widget.
3037  *
3038  * @param obj The entry object
3039  * @return If the widget only accepts text from pastes.
3040  *
3041  * @ingroup Entry
3042  */
3043 EAPI Eina_Bool
3044 elm_entry_cnp_textonly_get(Evas_Object *obj)
3045 {
3046    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
3047    Widget_Data *wd = elm_widget_data_get(obj);
3048    if (!wd) return EINA_FALSE;
3049    return wd->textonly;
3050 }
3051