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