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