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