Elementary: Fixed indentation and white spaces. Patch by Jihoon Kim
[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 (wd->disabled) return;
926    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return;
927    if (ev->button != 1) return;
928    //   if (ev->flags & EVAS_BUTTON_DOUBLE_CLICK)
929    if (wd->longpress_timer) ecore_timer_del(wd->longpress_timer);
930    wd->longpress_timer = ecore_timer_add(_elm_config->longpress_timeout, _long_press, data);
931    wd->downx = ev->canvas.x;
932    wd->downy = ev->canvas.y;
933 }
934
935 static void
936 _mouse_up(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
937 {
938    Widget_Data *wd = elm_widget_data_get(data);
939    Evas_Event_Mouse_Up *ev = event_info;
940    if (!wd) return;
941    if (wd->disabled) return;
942    if (ev->button != 1) return;
943    if (wd->longpress_timer)
944      {
945         ecore_timer_del(wd->longpress_timer);
946         wd->longpress_timer = NULL;
947      }
948 }
949
950 static void
951 _mouse_move(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
952 {
953    Widget_Data *wd = elm_widget_data_get(data);
954    Evas_Event_Mouse_Move *ev = event_info;
955    if (!wd) return;
956    if (wd->disabled) return;
957    if (!wd->selmode)
958      {
959         if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
960           {
961              if (wd->longpress_timer)
962                {
963                   ecore_timer_del(wd->longpress_timer);
964                   wd->longpress_timer = NULL;
965                }
966           }
967         else if (wd->longpress_timer)
968           {
969              Evas_Coord dx, dy;
970
971              dx = wd->downx - ev->cur.canvas.x;
972              dx *= dx;
973              dy = wd->downy - ev->cur.canvas.y;
974              dy *= dy;
975              if ((dx + dy) >
976                  ((_elm_config->finger_size / 2) *
977                   (_elm_config->finger_size / 2)))
978                {
979                   ecore_timer_del(wd->longpress_timer);
980                   wd->longpress_timer = NULL;
981                }
982           }
983      }
984    else if (wd->longpress_timer)
985      {
986         Evas_Coord dx, dy;
987
988         dx = wd->downx - ev->cur.canvas.x;
989         dx *= dx;
990         dy = wd->downy - ev->cur.canvas.y;
991         dy *= dy;
992         if ((dx + dy) >
993             ((_elm_config->finger_size / 2) *
994              (_elm_config->finger_size / 2)))
995           {
996              ecore_timer_del(wd->longpress_timer);
997              wd->longpress_timer = NULL;
998           }
999      }
1000 }
1001
1002 static const char *
1003 _getbase(Evas_Object *obj)
1004 {
1005    Widget_Data *wd = elm_widget_data_get(obj);
1006    if (!wd) return "base";
1007    if (wd->editable)
1008      {
1009         if (wd->password) return "base-password";
1010         else
1011           {
1012              if (wd->single_line) return "base-single";
1013              else
1014                {
1015                   if (wd->linewrap) return "base";
1016                   else if (wd->char_linewrap) return "base-charwrap";
1017                   else  return "base-nowrap";
1018                }
1019           }
1020      }
1021    else
1022      {
1023         if (wd->password) return "base-password";
1024         else
1025           {
1026              if (wd->single_line) return "base-single-noedit";
1027              else
1028                {
1029                   if (wd->linewrap) return "base-noedit";
1030                   else if (wd->char_linewrap) return "base-noedit-charwrap";
1031                   else  return "base-nowrap-noedit";
1032                }
1033           }
1034      }
1035    return "base";
1036 }
1037
1038 static void
1039 _signal_entry_changed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1040 {
1041    Widget_Data *wd = elm_widget_data_get(data);
1042    if (!wd) return;
1043    wd->changed = EINA_TRUE;
1044    _sizing_eval(data);
1045    if (wd->text) eina_stringshare_del(wd->text);
1046    wd->text = NULL;
1047    evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
1048    if (wd->delay_write)
1049      {
1050         ecore_timer_del(wd->delay_write);
1051         wd->delay_write = NULL;
1052      }
1053    if ((!wd->autosave) || (!wd->file)) return;
1054    wd->delay_write = ecore_timer_add(2.0, _delay_write, data);
1055 }
1056
1057 static void
1058 _signal_selection_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1059 {
1060    Widget_Data *wd = elm_widget_data_get(data);
1061    const Eina_List *l;
1062    Evas_Object *entry;
1063    if (!wd) return;
1064    EINA_LIST_FOREACH(entries, l, entry)
1065      {
1066         if (entry != data) elm_entry_select_none(entry);
1067      }
1068    wd->have_selection = EINA_TRUE;
1069    evas_object_smart_callback_call(data, SIG_SELECTION_START, NULL);
1070 #ifdef HAVE_ELEMENTARY_X
1071    if (wd->sel_notify_handler)
1072      {
1073         const char *txt = elm_entry_selection_get(data);
1074         Evas_Object *top;
1075
1076         top = elm_widget_top_get(data);
1077         if ((top) && (elm_win_xwindow_get(top)))
1078           elm_selection_set(ELM_SEL_PRIMARY, data, ELM_SEL_FORMAT_MARKUP, txt);
1079      }
1080 #endif
1081 }
1082
1083 static void
1084 _signal_selection_changed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1085 {
1086    Widget_Data *wd = elm_widget_data_get(data);
1087    if (!wd) return;
1088    wd->have_selection = EINA_TRUE;
1089    evas_object_smart_callback_call(data, SIG_SELECTION_CHANGED, NULL);
1090    elm_selection_set(ELM_SEL_PRIMARY, obj, ELM_SEL_FORMAT_MARKUP,
1091                      elm_entry_selection_get(data));
1092 }
1093
1094 static void
1095 _signal_selection_cleared(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1096 {
1097    Widget_Data *wd = elm_widget_data_get(data);
1098    if (!wd) return;
1099    if (!wd->have_selection) return;
1100    wd->have_selection = EINA_FALSE;
1101    evas_object_smart_callback_call(data, SIG_SELECTION_CLEARED, NULL);
1102    if (wd->sel_notify_handler)
1103      {
1104         if (wd->cut_sel)
1105           {
1106 #ifdef HAVE_ELEMENTARY_X
1107              Evas_Object *top;
1108
1109              top = elm_widget_top_get(data);
1110              if ((top) && (elm_win_xwindow_get(top)))
1111                elm_selection_set(ELM_SEL_PRIMARY, data, ELM_SEL_FORMAT_MARKUP,
1112                                  wd->cut_sel);
1113 #endif
1114              eina_stringshare_del(wd->cut_sel);
1115              wd->cut_sel = NULL;
1116           }
1117         else
1118           {
1119 #ifdef HAVE_ELEMENTARY_X
1120              Evas_Object *top;
1121
1122              top = elm_widget_top_get(data);
1123              if ((top) && (elm_win_xwindow_get(top)))
1124                elm_selection_clear(ELM_SEL_PRIMARY, data);
1125 #endif
1126           }
1127      }
1128 }
1129
1130 static void
1131 _signal_entry_paste_request(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1132 {
1133    Widget_Data *wd = elm_widget_data_get(data);
1134    if (!wd) return;
1135    evas_object_smart_callback_call(data, SIG_SELECTION_PASTE, NULL);
1136    if (wd->sel_notify_handler)
1137      {
1138 #ifdef HAVE_ELEMENTARY_X
1139         Evas_Object *top;
1140
1141         top = elm_widget_top_get(data);
1142         if ((top) && (elm_win_xwindow_get(top)))
1143           {
1144              wd->selection_asked = EINA_TRUE;
1145              elm_selection_get(ELM_SEL_CLIPBOARD, ELM_SEL_FORMAT_MARKUP, data,
1146                                NULL, NULL);
1147           }
1148 #endif
1149      }
1150 }
1151
1152 static void
1153 _signal_entry_copy_notify(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1154 {
1155    Widget_Data *wd = elm_widget_data_get(data);
1156    if (!wd) return;
1157    evas_object_smart_callback_call(data, SIG_SELECTION_COPY, NULL);
1158    elm_selection_set(ELM_SEL_CLIPBOARD, obj, ELM_SEL_FORMAT_MARKUP,
1159                      elm_entry_selection_get(data));
1160 }
1161
1162 static void
1163 _signal_entry_cut_notify(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1164 {
1165    Widget_Data *wd = elm_widget_data_get(data);
1166    if (!wd) return;
1167    evas_object_smart_callback_call(data, SIG_SELECTION_CUT, NULL);
1168    elm_selection_set(ELM_SEL_CLIPBOARD, obj, ELM_SEL_FORMAT_MARKUP,
1169                      elm_entry_selection_get(data));
1170    edje_object_part_text_insert(wd->ent, "elm.text", "");
1171    wd->changed = EINA_TRUE;
1172    _sizing_eval(data);
1173 }
1174
1175 static void
1176 _signal_cursor_changed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1177 {
1178    Widget_Data *wd = elm_widget_data_get(data);
1179    Evas_Coord cx, cy, cw, ch;
1180    if (!wd) return;
1181    evas_object_smart_callback_call(data, SIG_CURSOR_CHANGED, NULL);
1182    edje_object_part_text_cursor_geometry_get(wd->ent, "elm.text",
1183                                              &cx, &cy, &cw, &ch);
1184    if (!wd->deferred_recalc_job)
1185      elm_widget_show_region_set(data, cx, cy, cw, ch);
1186    else
1187      {
1188         wd->deferred_cur = EINA_TRUE;
1189         wd->cx = cx;
1190         wd->cy = cy;
1191         wd->cw = cw;
1192         wd->ch = ch;
1193      }
1194 }
1195
1196 static void
1197 _signal_anchor_down(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1198 {
1199    Widget_Data *wd = elm_widget_data_get(data);
1200    if (!wd) return;
1201 }
1202
1203 static void
1204 _signal_anchor_up(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1205 {
1206    Widget_Data *wd = elm_widget_data_get(data);
1207    if (!wd) return;
1208 }
1209
1210 static void
1211 _signal_anchor_clicked(void *data, Evas_Object *obj __UNUSED__, const char *emission, const char *source __UNUSED__)
1212 {
1213    Widget_Data *wd = elm_widget_data_get(data);
1214    Elm_Entry_Anchor_Info ei;
1215    char *buf2, *p, *p2, *n;
1216    if (!wd) return;
1217    p = strrchr(emission, ',');
1218    if (p)
1219      {
1220         const Eina_List *geoms;
1221
1222         n = p + 1;
1223         p2 = p -1;
1224         while (p2 >= emission)
1225           {
1226              if (*p2 == ',') break;
1227              p2--;
1228           }
1229         p2++;
1230         buf2 = alloca(5 + p - p2);
1231         strncpy(buf2, p2, p - p2);
1232         buf2[p - p2] = 0;
1233         ei.name = n;
1234         ei.button = atoi(buf2);
1235         ei.x = ei.y = ei.w = ei.h = 0;
1236         geoms =
1237            edje_object_part_text_anchor_geometry_get(wd->ent, "elm.text", ei.name);
1238         if (geoms)
1239           {
1240              Evas_Textblock_Rectangle *r;
1241              const Eina_List *l;
1242              Evas_Coord px, py, x, y;
1243
1244              evas_object_geometry_get(wd->ent, &x, &y, NULL, NULL);
1245              evas_pointer_output_xy_get(evas_object_evas_get(wd->ent), &px, &py);
1246              EINA_LIST_FOREACH(geoms, l, r)
1247                {
1248                   if (((r->x + x) <= px) && ((r->y + y) <= py) &&
1249                       ((r->x + x + r->w) > px) && ((r->y + y + r->h) > py))
1250                     {
1251                        ei.x = r->x + x;
1252                        ei.y = r->y + y;
1253                        ei.w = r->w;
1254                        ei.h = r->h;
1255                        break;
1256                     }
1257                }
1258           }
1259         if (!wd->disabled)
1260           evas_object_smart_callback_call(data, SIG_ANCHOR_CLICKED, &ei);
1261      }
1262 }
1263
1264 static void
1265 _signal_anchor_move(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1266 {
1267    Widget_Data *wd = elm_widget_data_get(data);
1268    if (!wd) return;
1269 }
1270
1271 static void
1272 _signal_anchor_in(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1273 {
1274    Widget_Data *wd = elm_widget_data_get(data);
1275    if (!wd) return;
1276 }
1277
1278 static void
1279 _signal_anchor_out(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1280 {
1281    Widget_Data *wd = elm_widget_data_get(data);
1282    if (!wd) return;
1283 }
1284
1285 static void
1286 _signal_key_enter(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1287 {
1288    Widget_Data *wd = elm_widget_data_get(data);
1289    if (!wd) return;
1290    evas_object_smart_callback_call(data, SIG_ACTIVATED, NULL);
1291 }
1292
1293 static void
1294 _signal_mouse_down(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1295 {
1296    Widget_Data *wd = elm_widget_data_get(data);
1297    if (!wd) return;
1298    evas_object_smart_callback_call(data, SIG_PRESS, NULL);
1299 }
1300
1301 static void
1302 _signal_mouse_clicked(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1303 {
1304    Widget_Data *wd = elm_widget_data_get(data);
1305    if (!wd) return;
1306    evas_object_smart_callback_call(data, SIG_CLICKED, NULL);
1307 }
1308
1309 static void
1310 _signal_mouse_double(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1311 {
1312    Widget_Data *wd = elm_widget_data_get(data);
1313    if (!wd) return;
1314    evas_object_smart_callback_call(data, SIG_CLICKED_DOUBLE, NULL);
1315 }
1316
1317 #ifdef HAVE_ELEMENTARY_X
1318 static Eina_Bool
1319 _event_selection_notify(void *data, int type __UNUSED__, void *event)
1320 {
1321    Widget_Data *wd = elm_widget_data_get(data);
1322    Ecore_X_Event_Selection_Notify *ev = event;
1323    if (!wd) return ECORE_CALLBACK_PASS_ON;
1324    if ((!wd->selection_asked) && (!wd->drag_selection_asked))
1325       return ECORE_CALLBACK_PASS_ON;
1326
1327    if ((ev->selection == ECORE_X_SELECTION_CLIPBOARD) ||
1328        (ev->selection == ECORE_X_SELECTION_PRIMARY))
1329      {
1330         Ecore_X_Selection_Data_Text *text_data;
1331
1332         text_data = ev->data;
1333         if (text_data->data.content == ECORE_X_SELECTION_CONTENT_TEXT)
1334           {
1335              if (text_data->text)
1336                {
1337                   char *txt = _elm_util_text_to_mkup(text_data->text);
1338
1339                   if (txt)
1340                     {
1341                        elm_entry_entry_insert(data, txt);
1342                        free(txt);
1343                     }
1344                }
1345           }
1346         wd->selection_asked = EINA_FALSE;
1347      }
1348    else if (ev->selection == ECORE_X_SELECTION_XDND)
1349      {
1350         Ecore_X_Selection_Data_Text *text_data;
1351
1352         text_data = ev->data;
1353         if (text_data->data.content == ECORE_X_SELECTION_CONTENT_TEXT)
1354           {
1355              if (text_data->text)
1356                {
1357                   char *txt = _elm_util_text_to_mkup(text_data->text);
1358
1359                   if (txt)
1360                     {
1361                      /* Massive FIXME: this should be at the drag point */
1362                        elm_entry_entry_insert(data, txt);
1363                        free(txt);
1364                     }
1365                }
1366           }
1367         wd->drag_selection_asked = EINA_FALSE;
1368
1369         ecore_x_dnd_send_finished();
1370
1371      }
1372    return ECORE_CALLBACK_PASS_ON;
1373 }
1374
1375 static Eina_Bool
1376 _event_selection_clear(void *data __UNUSED__, int type __UNUSED__, void *event __UNUSED__)
1377 {
1378 /*
1379    Widget_Data *wd = elm_widget_data_get(data);
1380    Ecore_X_Event_Selection_Clear *ev = event;
1381    if (!wd) return ECORE_CALLBACK_PASS_ON;
1382    if (!wd->have_selection) return ECORE_CALLBACK_PASS_ON;
1383    if ((ev->selection == ECORE_X_SELECTION_CLIPBOARD) ||
1384        (ev->selection == ECORE_X_SELECTION_PRIMARY))
1385      {
1386         elm_entry_select_none(data);
1387      }
1388    return 1;*/
1389    return ECORE_CALLBACK_PASS_ON;
1390 }
1391
1392
1393 static Eina_Bool
1394 _drag_drop_cb(void *data __UNUSED__, Evas_Object *obj, Elm_Selection_Data *drop)
1395 {
1396    Widget_Data *wd;
1397    Eina_Bool rv;
1398
1399    wd = elm_widget_data_get(obj);
1400
1401    if (!wd) return EINA_FALSE;
1402    printf("Inserting at (%d,%d) %s\n",drop->x,drop->y,(char*)drop->data);
1403
1404    edje_object_part_text_cursor_copy(wd->ent, "elm.text",
1405                                      EDJE_CURSOR_MAIN,/*->*/EDJE_CURSOR_USER);
1406    rv = edje_object_part_text_cursor_coord_set(wd->ent,"elm.text",
1407                                           EDJE_CURSOR_MAIN,drop->x,drop->y);
1408    if (!rv) printf("Warning: Failed to position cursor: paste anyway\n");
1409    elm_entry_entry_insert(obj, drop->data);
1410    edje_object_part_text_cursor_copy(wd->ent, "elm.text",
1411                                      EDJE_CURSOR_USER,/*->*/EDJE_CURSOR_MAIN);
1412
1413    return EINA_TRUE;
1414 }
1415 #endif
1416
1417 static Evas_Object *
1418 _get_item(void *data, Evas_Object *edje __UNUSED__, const char *part __UNUSED__, const char *item)
1419 {
1420    Widget_Data *wd = elm_widget_data_get(data);
1421    Evas_Object *o;
1422    Eina_List *l;
1423    Elm_Entry_Item_Provider *ip;
1424
1425    EINA_LIST_FOREACH(wd->item_providers, l, ip)
1426      {
1427         o = ip->func(ip->data, data, item);
1428         if (o) return o;
1429      }
1430    if (!strncmp(item, "file://", 7))
1431      {
1432         const char *fname = item + 7;
1433        
1434         o = evas_object_image_filled_add(evas_object_evas_get(data));
1435         evas_object_image_file_set(o, fname, NULL);
1436         if (evas_object_image_load_error_get(o) == EVAS_LOAD_ERROR_NONE)
1437           {
1438              evas_object_show(o);
1439           }
1440         else
1441           {
1442              evas_object_del(o);
1443              o = edje_object_add(evas_object_evas_get(data));
1444              _elm_theme_object_set(data, o, "entry/emoticon", "wtf", elm_widget_style_get(data));
1445           }
1446         return o;
1447      }
1448    o = edje_object_add(evas_object_evas_get(data));
1449    if (!_elm_theme_object_set(data, o, "entry", item, elm_widget_style_get(data)))
1450      _elm_theme_object_set(data, o, "entry/emoticon", "wtf", elm_widget_style_get(data));
1451    return o;
1452 }
1453
1454 static void
1455 _text_filter(void *data, Evas_Object *edje __UNUSED__, const char *part __UNUSED__, Edje_Text_Filter_Type type, char **text)
1456 {
1457    Widget_Data *wd = elm_widget_data_get(data);
1458    Eina_List *l;
1459    Elm_Entry_Text_Filter *tf;
1460
1461    if (type == EDJE_TEXT_FILTER_FORMAT)
1462      return;
1463
1464    EINA_LIST_FOREACH(wd->text_filters, l, tf)
1465      {
1466         tf->func(tf->data, data, text);
1467         if (!*text)
1468            break;
1469      }
1470 }
1471
1472 /**
1473  * This adds an entry to @p parent object.
1474  *
1475  * @param parent The parent object
1476  * @return The new object or NULL if it cannot be created
1477  *
1478  * @ingroup Entry
1479  */
1480 EAPI Evas_Object *
1481 elm_entry_add(Evas_Object *parent)
1482 {
1483    Evas_Object *obj, *top;
1484    Evas *e;
1485    Widget_Data *wd;
1486
1487    EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
1488
1489    wd = ELM_NEW(Widget_Data);
1490    e = evas_object_evas_get(parent);
1491    if (!e) return NULL;
1492    obj = elm_widget_add(e);
1493    ELM_SET_WIDTYPE(widtype, "entry");
1494    elm_widget_type_set(obj, "entry");
1495    elm_widget_sub_object_add(parent, obj);
1496    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
1497    elm_widget_data_set(obj, wd);
1498    elm_widget_del_hook_set(obj, _del_hook);
1499    elm_widget_del_pre_hook_set(obj, _del_pre_hook);
1500    elm_widget_theme_hook_set(obj, _theme_hook);
1501    elm_widget_disable_hook_set(obj, _disable_hook);
1502    elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
1503    elm_widget_on_focus_region_hook_set(obj, _on_focus_region_hook);
1504    elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
1505    elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
1506    elm_object_cursor_set(obj, ELM_CURSOR_XTERM);
1507    elm_widget_can_focus_set(obj, EINA_TRUE);
1508    elm_widget_highlight_ignore_set(obj, EINA_TRUE);
1509
1510    wd->linewrap     = EINA_TRUE;
1511    wd->char_linewrap= EINA_FALSE;
1512    wd->editable     = EINA_TRUE;
1513    wd->disabled     = EINA_FALSE;
1514    wd->context_menu = EINA_TRUE;
1515    wd->autosave     = EINA_TRUE;
1516    wd->textonly     = EINA_FALSE;
1517
1518    wd->ent = edje_object_add(e);
1519    edje_object_item_provider_set(wd->ent, _get_item, obj);
1520    edje_object_text_insert_filter_callback_add(wd->ent,"elm.text", _text_filter, obj);
1521    evas_object_event_callback_add(wd->ent, EVAS_CALLBACK_MOVE, _move, obj);
1522    evas_object_event_callback_add(wd->ent, EVAS_CALLBACK_RESIZE, _resize, obj);
1523    evas_object_event_callback_add(wd->ent, EVAS_CALLBACK_MOUSE_DOWN,
1524                                   _mouse_down, obj);
1525    evas_object_event_callback_add(wd->ent, EVAS_CALLBACK_MOUSE_UP,
1526                                   _mouse_up, obj);
1527    evas_object_event_callback_add(wd->ent, EVAS_CALLBACK_MOUSE_MOVE,
1528                                   _mouse_move, obj);
1529
1530    _elm_theme_object_set(obj, wd->ent, "entry", "base", "default");
1531    edje_object_signal_callback_add(wd->ent, "entry,changed", "elm.text",
1532                                    _signal_entry_changed, obj);
1533    edje_object_signal_callback_add(wd->ent, "selection,start", "elm.text",
1534                                    _signal_selection_start, obj);
1535    edje_object_signal_callback_add(wd->ent, "selection,changed", "elm.text",
1536                                    _signal_selection_changed, obj);
1537    edje_object_signal_callback_add(wd->ent, "selection,cleared", "elm.text",
1538                                    _signal_selection_cleared, obj);
1539    edje_object_signal_callback_add(wd->ent, "entry,paste,request", "elm.text",
1540                                    _signal_entry_paste_request, obj);
1541    edje_object_signal_callback_add(wd->ent, "entry,copy,notify", "elm.text",
1542                                    _signal_entry_copy_notify, obj);
1543    edje_object_signal_callback_add(wd->ent, "entry,cut,notify", "elm.text",
1544                                    _signal_entry_cut_notify, obj);
1545    edje_object_signal_callback_add(wd->ent, "cursor,changed", "elm.text",
1546                                    _signal_cursor_changed, obj);
1547    edje_object_signal_callback_add(wd->ent, "anchor,mouse,down,*", "elm.text",
1548                                    _signal_anchor_down, obj);
1549    edje_object_signal_callback_add(wd->ent, "anchor,mouse,up,*", "elm.text",
1550                                    _signal_anchor_up, obj);
1551    edje_object_signal_callback_add(wd->ent, "anchor,mouse,clicked,*", "elm.text",
1552                                    _signal_anchor_clicked, obj);
1553    edje_object_signal_callback_add(wd->ent, "anchor,mouse,move,*", "elm.text",
1554                                    _signal_anchor_move, obj);
1555    edje_object_signal_callback_add(wd->ent, "anchor,mouse,in,*", "elm.text",
1556                                    _signal_anchor_in, obj);
1557    edje_object_signal_callback_add(wd->ent, "anchor,mouse,out,*", "elm.text",
1558                                    _signal_anchor_out, obj);
1559    edje_object_signal_callback_add(wd->ent, "entry,key,enter", "elm.text",
1560                                    _signal_key_enter, obj);
1561    edje_object_signal_callback_add(wd->ent, "mouse,down,1", "elm.text",
1562                                    _signal_mouse_down, obj);
1563    edje_object_signal_callback_add(wd->ent, "mouse,clicked,1", "elm.text",
1564                                    _signal_mouse_clicked, obj);
1565    edje_object_signal_callback_add(wd->ent, "mouse,down,1,double", "elm.text",
1566                                    _signal_mouse_double, obj);
1567    edje_object_part_text_set(wd->ent, "elm.text", "");
1568    elm_widget_resize_object_set(obj, wd->ent);
1569    _sizing_eval(obj);
1570
1571 #ifdef HAVE_ELEMENTARY_X
1572    top = elm_widget_top_get(obj);
1573    if ((top) && (elm_win_xwindow_get(top)))
1574      {
1575         wd->sel_notify_handler =
1576           ecore_event_handler_add(ECORE_X_EVENT_SELECTION_NOTIFY,
1577                                   _event_selection_notify, obj);
1578         wd->sel_clear_handler =
1579           ecore_event_handler_add(ECORE_X_EVENT_SELECTION_CLEAR,
1580                                   _event_selection_clear, obj);
1581      }
1582
1583    elm_drop_target_add(obj, ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_IMAGE,
1584                    _drag_drop_cb, NULL);
1585 #endif
1586
1587    entries = eina_list_prepend(entries, obj);
1588
1589    // module - find module for entry
1590    wd->api = _module(obj);
1591    // if found - hook in
1592    if ((wd->api) && (wd->api->obj_hook)) wd->api->obj_hook(obj);
1593
1594    _mirrored_set(obj, elm_widget_mirrored_get(obj));
1595    // TODO: convert Elementary to subclassing of Evas_Smart_Class
1596    // TODO: and save some bytes, making descriptions per-class and not instance!
1597    evas_object_smart_callbacks_descriptions_set(obj, _signals);
1598    return obj;
1599 }
1600
1601
1602 /**
1603  * This sets the entry object not to line wrap.  All input will
1604  * be on a single line, and the entry box will extend with user input.
1605  *
1606  * @param obj The entry object
1607  * @param single_line If true, the text in the entry
1608  * will be on a single line.
1609  *
1610  * @ingroup Entry
1611  */
1612 EAPI void
1613 elm_entry_single_line_set(Evas_Object *obj, Eina_Bool single_line)
1614 {
1615    ELM_CHECK_WIDTYPE(obj, widtype);
1616    Widget_Data *wd = elm_widget_data_get(obj);
1617    const char *t;
1618    if (!wd) return;
1619    if (wd->single_line == single_line) return;
1620    wd->single_line = single_line;
1621    wd->linewrap = EINA_FALSE;
1622    wd->char_linewrap = EINA_FALSE;
1623    elm_entry_cnp_textonly_set(obj, EINA_TRUE);
1624    t = eina_stringshare_add(elm_entry_entry_get(obj));
1625    _elm_theme_object_set(obj, wd->ent, "entry", _getbase(obj), elm_widget_style_get(obj));
1626    elm_entry_entry_set(obj, t);
1627    eina_stringshare_del(t);
1628    _sizing_eval(obj);
1629 }
1630
1631 /**
1632  * This returns true if the entry has been set to single line mode.
1633  * See also elm_entry_single_line_set().
1634  *
1635  * @param obj The entry object
1636  * @return single_line If true, the text in the entry is set to display
1637  * on a single line.
1638  *
1639  * @ingroup Entry
1640  */
1641 EAPI Eina_Bool
1642 elm_entry_single_line_get(const Evas_Object *obj)
1643 {
1644    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1645    Widget_Data *wd = elm_widget_data_get(obj);
1646    if (!wd) return EINA_FALSE;
1647    return wd->single_line;
1648 }
1649
1650 /**
1651  * This sets the entry object to password mode.  All text entered
1652  * and/or displayed within the widget will be replaced with asterisks (*).
1653  *
1654  * @param obj The entry object
1655  * @param password If true, password mode is enabled.
1656  *
1657  * @ingroup Entry
1658  */
1659 EAPI void
1660 elm_entry_password_set(Evas_Object *obj, Eina_Bool password)
1661 {
1662    ELM_CHECK_WIDTYPE(obj, widtype);
1663    Widget_Data *wd = elm_widget_data_get(obj);
1664    const char *t;
1665    if (!wd) return;
1666    if (wd->password == password) return;
1667    wd->password = password;
1668    wd->single_line = EINA_TRUE;
1669    wd->linewrap = EINA_FALSE;
1670    wd->char_linewrap = EINA_FALSE;
1671    t = eina_stringshare_add(elm_entry_entry_get(obj));
1672    _elm_theme_object_set(obj, wd->ent, "entry", _getbase(obj), elm_widget_style_get(obj));
1673    elm_entry_entry_set(obj, t);
1674    eina_stringshare_del(t);
1675    _sizing_eval(obj);
1676 }
1677
1678
1679 /**
1680  * This returns whether password mode is enabled.
1681  * See also elm_entry_password_set().
1682  *
1683  * @param obj The entry object
1684  * @return If true, the entry is set to display all characters
1685  * as asterisks (*).
1686  *
1687  * @ingroup Entry
1688  */
1689 EAPI Eina_Bool
1690 elm_entry_password_get(const Evas_Object *obj)
1691 {
1692    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1693    Widget_Data *wd = elm_widget_data_get(obj);
1694    if (!wd) return EINA_FALSE;
1695    return wd->password;
1696 }
1697
1698 /**
1699  * This sets the text displayed within the entry to @p entry.
1700  *
1701  * @param obj The entry object
1702  * @param entry The text to be displayed
1703  *
1704  * @ingroup Entry
1705  */
1706 EAPI void
1707 elm_entry_entry_set(Evas_Object *obj, const char *entry)
1708 {
1709    ELM_CHECK_WIDTYPE(obj, widtype);
1710    Widget_Data *wd = elm_widget_data_get(obj);
1711    if (!wd) return;
1712    if (!entry) entry = "";
1713    edje_object_part_text_set(wd->ent, "elm.text", entry);
1714    if (wd->text) eina_stringshare_del(wd->text);
1715    wd->text = NULL;
1716    wd->changed = EINA_TRUE;
1717    _sizing_eval(obj);
1718 }
1719
1720 /**
1721  * This returns the text currently shown in object @p entry.
1722  * See also elm_entry_entry_set().
1723  *
1724  * @param obj The entry object
1725  * @return The currently displayed text or NULL on failure
1726  *
1727  * @ingroup Entry
1728  */
1729 EAPI const char *
1730 elm_entry_entry_get(const Evas_Object *obj)
1731 {
1732    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1733    Widget_Data *wd = elm_widget_data_get(obj);
1734    const char *text;
1735    if (!wd) return NULL;
1736    if (wd->text) return wd->text;
1737    text = edje_object_part_text_get(wd->ent, "elm.text");
1738    if (!text)
1739      {
1740         ERR("text=NULL for edje %p, part 'elm.text'", wd->ent);
1741         return NULL;
1742      }
1743    eina_stringshare_replace(&wd->text, text);
1744    return wd->text;
1745 }
1746
1747
1748 /**
1749  * This returns EINA_TRUE if the entry is empty/there was an error
1750  * and EINA_FALSE if it is not empty.
1751  *
1752  * @param obj The entry object
1753  * @return If the entry is empty or not.
1754  *
1755  * @ingroup Entry
1756  */
1757 EAPI Eina_Bool
1758 elm_entry_is_empty(const Evas_Object *obj)
1759 {
1760    /* FIXME: until there's support for that in textblock, we just check
1761     * to see if the there is text or not. */
1762    ELM_CHECK_WIDTYPE(obj, widtype) EINA_TRUE;
1763    Widget_Data *wd = elm_widget_data_get(obj);
1764    const Evas_Object *tb;
1765    Evas_Textblock_Cursor *cur;
1766    Eina_Bool ret;
1767    if (!wd) return EINA_TRUE;
1768    /* It's a hack until we get the support suggested above.
1769     * We just create a cursor, point it to the begining, and then
1770     * try to advance it, if it can advance, the tb is not empty,
1771     * otherwise it is. */
1772    tb = edje_object_part_object_get(wd->ent, "elm.text");
1773    cur = evas_object_textblock_cursor_new((Evas_Object *) tb); /* This is
1774       actually, ok for the time being, thsese hackish stuff will be removed
1775       once evas 1.0 is out*/
1776    evas_textblock_cursor_pos_set(cur, 0);
1777    ret = evas_textblock_cursor_char_next(cur);
1778    evas_textblock_cursor_free(cur);
1779
1780    return !ret;
1781 }
1782
1783 /**
1784  * This returns all selected text within the entry.
1785  *
1786  * @param obj The entry object
1787  * @return The selected text within the entry or NULL on failure
1788  *
1789  * @ingroup Entry
1790  */
1791 EAPI const char *
1792 elm_entry_selection_get(const Evas_Object *obj)
1793 {
1794    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1795    Widget_Data *wd = elm_widget_data_get(obj);
1796    if (!wd) return NULL;
1797    return edje_object_part_text_selection_get(wd->ent, "elm.text");
1798 }
1799
1800 /**
1801  * This inserts text in @p entry where the current cursor position.
1802  * 
1803  * This inserts text at the cursor position is as if it was typed 
1804  * by the user (note this also allows markup which a user
1805  * can't just "type" as it would be converted to escaped text, so this
1806  * call can be used to insert things like emoticon items or bold push/pop
1807  * tags, other font and color change tags etc.)
1808  *
1809  * @param obj The entry object
1810  * @param entry The text to insert
1811  *
1812  * @ingroup Entry
1813  */
1814 EAPI void
1815 elm_entry_entry_insert(Evas_Object *obj, const char *entry)
1816 {
1817    ELM_CHECK_WIDTYPE(obj, widtype);
1818    Widget_Data *wd = elm_widget_data_get(obj);
1819    if (!wd) return;
1820    edje_object_part_text_insert(wd->ent, "elm.text", entry);
1821    wd->changed = EINA_TRUE;
1822    _sizing_eval(obj);
1823 }
1824
1825 /**
1826  * This enables word line wrapping in the entry object.  It is the opposite
1827  * of elm_entry_single_line_set().  Additionally, setting this disables
1828  * character line wrapping.
1829  * See also elm_entry_line_char_wrap_set().
1830  *
1831  * @param obj The entry object
1832  * @param wrap If true, the entry will be wrapped once it reaches the end
1833  * of the object. Wrapping will occur at the end of the word before the end of the
1834  * object.
1835  *
1836  * @ingroup Entry
1837  */
1838 EAPI void
1839 elm_entry_line_wrap_set(Evas_Object *obj, Eina_Bool wrap)
1840 {
1841    ELM_CHECK_WIDTYPE(obj, widtype);
1842    Widget_Data *wd = elm_widget_data_get(obj);
1843    const char *t;
1844    if (!wd) return;
1845    if (wd->linewrap == wrap) return;
1846    wd->linewrap = wrap;
1847    if(wd->linewrap)
1848        wd->char_linewrap = EINA_FALSE;
1849    t = eina_stringshare_add(elm_entry_entry_get(obj));
1850    _elm_theme_object_set(obj, wd->ent, "entry", _getbase(obj), elm_widget_style_get(obj));
1851    elm_entry_entry_set(obj, t);
1852    eina_stringshare_del(t);
1853    _sizing_eval(obj);
1854 }
1855
1856 /**
1857  * This enables character line wrapping in the entry object.  It is the opposite
1858  * of elm_entry_single_line_set().  Additionally, setting this disables
1859  * word line wrapping.
1860  * See also elm_entry_line_wrap_set().
1861  *
1862  * @param obj The entry object
1863  * @param wrap If true, the entry will be wrapped once it reaches the end
1864  * of the object. Wrapping will occur immediately upon reaching the end of the object.
1865  *
1866  * @ingroup Entry
1867  */
1868 EAPI void
1869 elm_entry_line_char_wrap_set(Evas_Object *obj, Eina_Bool wrap)
1870 {
1871    ELM_CHECK_WIDTYPE(obj, widtype);
1872    Widget_Data *wd = elm_widget_data_get(obj);
1873    const char *t;
1874    if (!wd) return;
1875    if (wd->char_linewrap == wrap) return;
1876    wd->char_linewrap = wrap;
1877    if(wd->char_linewrap)
1878        wd->linewrap = EINA_FALSE;
1879    t = eina_stringshare_add(elm_entry_entry_get(obj));
1880    _elm_theme_object_set(obj, wd->ent, "entry", _getbase(obj), elm_widget_style_get(obj));
1881    elm_entry_entry_set(obj, t);
1882    eina_stringshare_del(t);
1883    _sizing_eval(obj);
1884 }
1885
1886 /**
1887  * This sets the editable attribute of the entry.
1888  *
1889  * @param obj The entry object
1890  * @param editable If true, the entry will be editable by the user.
1891  * If false, it will be set to the disabled state.
1892  *
1893  * @ingroup Entry
1894  */
1895 EAPI void
1896 elm_entry_editable_set(Evas_Object *obj, Eina_Bool editable)
1897 {
1898    ELM_CHECK_WIDTYPE(obj, widtype);
1899    Widget_Data *wd = elm_widget_data_get(obj);
1900    const char *t;
1901    if (!wd) return;
1902    if (wd->editable == editable) return;
1903    wd->editable = editable;
1904    t = eina_stringshare_add(elm_entry_entry_get(obj));
1905    _elm_theme_object_set(obj, wd->ent, "entry", _getbase(obj), elm_widget_style_get(obj));
1906    elm_entry_entry_set(obj, t);
1907    eina_stringshare_del(t);
1908    _sizing_eval(obj);
1909
1910 #ifdef HAVE_ELEMENTARY_X
1911    if (editable)
1912       elm_drop_target_add(obj, ELM_SEL_FORMAT_MARKUP, _drag_drop_cb, NULL);
1913    else
1914       elm_drop_target_del(obj);
1915 #endif
1916 }
1917
1918 /**
1919  * This gets the editable attribute of the entry.
1920  * See also elm_entry_editable_set().
1921  *
1922  * @param obj The entry object
1923  * @return If true, the entry is editable by the user.
1924  * If false, it is not editable by the user
1925  *
1926  * @ingroup Entry
1927  */
1928 EAPI Eina_Bool
1929 elm_entry_editable_get(const Evas_Object *obj)
1930 {
1931    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1932    Widget_Data *wd = elm_widget_data_get(obj);
1933    if (!wd) return EINA_FALSE;
1934    return wd->editable;
1935 }
1936
1937 /**
1938  * This drops any existing text selection within the entry.
1939  *
1940  * @param obj The entry object
1941  *
1942  * @ingroup Entry
1943  */
1944 EAPI void
1945 elm_entry_select_none(Evas_Object *obj)
1946 {
1947    ELM_CHECK_WIDTYPE(obj, widtype);
1948    Widget_Data *wd = elm_widget_data_get(obj);
1949    if (!wd) return;
1950    if (wd->selmode)
1951      {
1952         wd->selmode = EINA_FALSE;
1953         edje_object_part_text_select_allow_set(wd->ent, "elm.text", 0);
1954         edje_object_signal_emit(wd->ent, "elm,state,select,off", "elm");
1955      }
1956    wd->have_selection = EINA_FALSE;
1957    edje_object_part_text_select_none(wd->ent, "elm.text");
1958 }
1959
1960 /**
1961  * This selects all text within the entry.
1962  *
1963  * @param obj The entry object
1964  *
1965  * @ingroup Entry
1966  */
1967 EAPI void
1968 elm_entry_select_all(Evas_Object *obj)
1969 {
1970    ELM_CHECK_WIDTYPE(obj, widtype);
1971    Widget_Data *wd = elm_widget_data_get(obj);
1972    if (!wd) return;
1973    if (wd->selmode)
1974      {
1975         wd->selmode = EINA_FALSE;
1976         edje_object_part_text_select_allow_set(wd->ent, "elm.text", 0);
1977         edje_object_signal_emit(wd->ent, "elm,state,select,off", "elm");
1978      }
1979    wd->have_selection = EINA_TRUE;
1980    edje_object_part_text_select_all(wd->ent, "elm.text");
1981 }
1982
1983 /**
1984  * This function returns the geometry of the cursor.
1985  *
1986  * It's useful if you want to draw something on the cursor (or where it is),
1987  * or for example in the case of scrolled entry where you want to show the
1988  * cursor.
1989  *
1990  * @param obj The entry object
1991  * @param x returned geometry
1992  * @param y returned geometry
1993  * @param w returned geometry
1994  * @param h returned geometry
1995  * @return EINA_TRUE upon success, EINA_FALSE upon failure
1996  *
1997  * @ingroup Entry
1998  */
1999 EAPI Eina_Bool
2000 elm_entry_cursor_geometry_get(const Evas_Object *obj, Evas_Coord *x, Evas_Coord *y, Evas_Coord *w, Evas_Coord *h)
2001 {
2002    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
2003    Widget_Data *wd = elm_widget_data_get(obj);
2004    if (!wd) return EINA_FALSE;
2005    edje_object_part_text_cursor_geometry_get(wd->ent, "elm.text", x, y, w, h);
2006    return EINA_TRUE;
2007 }
2008
2009 /**
2010  * This moves the cursor one place to the right within the entry.
2011  *
2012  * @param obj The entry object
2013  * @return EINA_TRUE upon success, EINA_FALSE upon failure
2014  *
2015  * @ingroup Entry
2016  */
2017 EAPI Eina_Bool
2018 elm_entry_cursor_next(Evas_Object *obj)
2019 {
2020    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
2021    Widget_Data *wd = elm_widget_data_get(obj);
2022    if (!wd) return EINA_FALSE;
2023    return edje_object_part_text_cursor_next(wd->ent, "elm.text", EDJE_CURSOR_MAIN);
2024 }
2025
2026 /**
2027  * This moves the cursor one place to the left within the entry.
2028  *
2029  * @param obj The entry object
2030  * @return EINA_TRUE upon success, EINA_FALSE upon failure
2031  *
2032  * @ingroup Entry
2033  */
2034 EAPI Eina_Bool
2035 elm_entry_cursor_prev(Evas_Object *obj)
2036 {
2037    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
2038    Widget_Data *wd = elm_widget_data_get(obj);
2039    if (!wd) return EINA_FALSE;
2040    return edje_object_part_text_cursor_prev(wd->ent, "elm.text", EDJE_CURSOR_MAIN);
2041 }
2042
2043 /**
2044  * This moves the cursor one line up within the entry.
2045  *
2046  * @param obj The entry object
2047  * @return EINA_TRUE upon success, EINA_FALSE upon failure
2048  *
2049  * @ingroup Entry
2050  */
2051 EAPI Eina_Bool
2052 elm_entry_cursor_up(Evas_Object *obj)
2053 {
2054    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
2055    Widget_Data *wd = elm_widget_data_get(obj);
2056    if (!wd) return EINA_FALSE;
2057    return edje_object_part_text_cursor_up(wd->ent, "elm.text", EDJE_CURSOR_MAIN);
2058 }
2059
2060 /**
2061  * This moves the cursor one line down within the entry.
2062  *
2063  * @param obj The entry object
2064  * @return EINA_TRUE upon success, EINA_FALSE upon failure
2065  *
2066  * @ingroup Entry
2067  */
2068 EAPI Eina_Bool
2069 elm_entry_cursor_down(Evas_Object *obj)
2070 {
2071    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
2072    Widget_Data *wd = elm_widget_data_get(obj);
2073    if (!wd) return EINA_FALSE;
2074    return edje_object_part_text_cursor_down(wd->ent, "elm.text", EDJE_CURSOR_MAIN);
2075 }
2076
2077 /**
2078  * This moves the cursor to the beginning of the entry.
2079  *
2080  * @param obj The entry object
2081  *
2082  * @ingroup Entry
2083  */
2084 EAPI void
2085 elm_entry_cursor_begin_set(Evas_Object *obj)
2086 {
2087    ELM_CHECK_WIDTYPE(obj, widtype);
2088    Widget_Data *wd = elm_widget_data_get(obj);
2089    if (!wd) return;
2090    edje_object_part_text_cursor_begin_set(wd->ent, "elm.text", EDJE_CURSOR_MAIN);
2091 }
2092
2093 /**
2094  * This moves the cursor to the end of the entry.
2095  *
2096  * @param obj The entry object
2097  *
2098  * @ingroup Entry
2099  */
2100 EAPI void
2101 elm_entry_cursor_end_set(Evas_Object *obj)
2102 {
2103    ELM_CHECK_WIDTYPE(obj, widtype);
2104    Widget_Data *wd = elm_widget_data_get(obj);
2105    if (!wd) return;
2106    edje_object_part_text_cursor_end_set(wd->ent, "elm.text", EDJE_CURSOR_MAIN);
2107 }
2108
2109 /**
2110  * This moves the cursor to the beginning of the current line.
2111  *
2112  * @param obj The entry object
2113  *
2114  * @ingroup Entry
2115  */
2116 EAPI void
2117 elm_entry_cursor_line_begin_set(Evas_Object *obj)
2118 {
2119    ELM_CHECK_WIDTYPE(obj, widtype);
2120    Widget_Data *wd = elm_widget_data_get(obj);
2121    if (!wd) return;
2122    edje_object_part_text_cursor_line_begin_set(wd->ent, "elm.text", EDJE_CURSOR_MAIN);
2123 }
2124
2125 /**
2126  * This moves the cursor to the end of the current line.
2127  *
2128  * @param obj The entry object
2129  *
2130  * @ingroup Entry
2131  */
2132 EAPI void
2133 elm_entry_cursor_line_end_set(Evas_Object *obj)
2134 {
2135    ELM_CHECK_WIDTYPE(obj, widtype);
2136    Widget_Data *wd = elm_widget_data_get(obj);
2137    if (!wd) return;
2138    edje_object_part_text_cursor_line_end_set(wd->ent, "elm.text", EDJE_CURSOR_MAIN);
2139 }
2140
2141 /**
2142  * This begins a selection within the entry as though
2143  * the user were holding down the mouse button to make a selection.
2144  *
2145  * @param obj The entry object
2146  *
2147  * @ingroup Entry
2148  */
2149 EAPI void
2150 elm_entry_cursor_selection_begin(Evas_Object *obj)
2151 {
2152    ELM_CHECK_WIDTYPE(obj, widtype);
2153    Widget_Data *wd = elm_widget_data_get(obj);
2154    if (!wd) return;
2155    edje_object_part_text_select_begin(wd->ent, "elm.text");
2156 }
2157
2158 /**
2159  * This ends a selection within the entry as though
2160  * the user had just released the mouse button while making a selection.
2161  *
2162  * @param obj The entry object
2163  *
2164  * @ingroup Entry
2165  */
2166 EAPI void
2167 elm_entry_cursor_selection_end(Evas_Object *obj)
2168 {
2169    ELM_CHECK_WIDTYPE(obj, widtype);
2170    Widget_Data *wd = elm_widget_data_get(obj);
2171    if (!wd) return;
2172    edje_object_part_text_select_extend(wd->ent, "elm.text");
2173 }
2174
2175 /**
2176  * TODO: fill this in
2177  *
2178  * @param obj The entry object
2179  * @return TODO: fill this in
2180  *
2181  * @ingroup Entry
2182  */
2183 EAPI Eina_Bool
2184 elm_entry_cursor_is_format_get(const Evas_Object *obj)
2185 {
2186    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
2187    Widget_Data *wd = elm_widget_data_get(obj);
2188    if (!wd) return EINA_FALSE;
2189    return edje_object_part_text_cursor_is_format_get(wd->ent, "elm.text", EDJE_CURSOR_MAIN);
2190 }
2191
2192 /**
2193  * This returns whether the cursor is visible.
2194  *
2195  * @param obj The entry object
2196  * @return If true, the cursor is visible.
2197  *
2198  * @ingroup Entry
2199  */
2200 EAPI Eina_Bool
2201 elm_entry_cursor_is_visible_format_get(const Evas_Object *obj)
2202 {
2203    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
2204    Widget_Data *wd = elm_widget_data_get(obj);
2205    if (!wd) return EINA_FALSE;
2206    return edje_object_part_text_cursor_is_visible_format_get(wd->ent, "elm.text", EDJE_CURSOR_MAIN);
2207 }
2208
2209 /**
2210  * TODO: fill this in
2211  *
2212  * @param obj The entry object
2213  * @return TODO: fill this in
2214  *
2215  * @ingroup Entry
2216  */
2217 EAPI const char *
2218 elm_entry_cursor_content_get(const Evas_Object *obj)
2219 {
2220    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
2221    Widget_Data *wd = elm_widget_data_get(obj);
2222    if (!wd) return NULL;
2223    return edje_object_part_text_cursor_content_get(wd->ent, "elm.text", EDJE_CURSOR_MAIN);
2224 }
2225
2226 /**
2227  * Sets the cursor position in the entry to the given value
2228  *
2229  * @param obj The entry object
2230  * @param pos The position of the cursor
2231  *
2232  * @ingroup Entry
2233  */
2234 EAPI void
2235 elm_entry_cursor_pos_set(Evas_Object *obj, int pos)
2236 {
2237    ELM_CHECK_WIDTYPE(obj, widtype);
2238    Widget_Data *wd = elm_widget_data_get(obj);
2239    if (!wd) return;
2240    edje_object_part_text_cursor_pos_set(wd->ent, "elm.text", EDJE_CURSOR_MAIN, pos);
2241 }
2242
2243 /**
2244  * Retrieves the current position of the cursor in the entry
2245  *
2246  * @param obj The entry object
2247  * @return The cursor position
2248  *
2249  * @ingroup Entry
2250  */
2251 EAPI int
2252 elm_entry_cursor_pos_get(const Evas_Object *obj)
2253 {
2254    ELM_CHECK_WIDTYPE(obj, widtype) 0;
2255    Widget_Data *wd = elm_widget_data_get(obj);
2256    if (!wd) return 0;
2257    return edje_object_part_text_cursor_pos_get(wd->ent, "elm.text", EDJE_CURSOR_MAIN);
2258 }
2259
2260 /**
2261  * This executes a "cut" action on the selected text in the entry.
2262  *
2263  * @param obj The entry object
2264  *
2265  * @ingroup Entry
2266  */
2267 EAPI void
2268 elm_entry_selection_cut(Evas_Object *obj)
2269 {
2270    ELM_CHECK_WIDTYPE(obj, widtype);
2271    Widget_Data *wd = elm_widget_data_get(obj);
2272    if (!wd) return;
2273    _cut(obj, NULL, NULL);
2274 }
2275
2276 /**
2277  * This executes a "copy" action on the selected text in the entry.
2278  *
2279  * @param obj The entry object
2280  *
2281  * @ingroup Entry
2282  */
2283 EAPI void
2284 elm_entry_selection_copy(Evas_Object *obj)
2285 {
2286    ELM_CHECK_WIDTYPE(obj, widtype);
2287    Widget_Data *wd = elm_widget_data_get(obj);
2288    if (!wd) return;
2289    _copy(obj, NULL, NULL);
2290 }
2291
2292 /**
2293  * This executes a "paste" action in the entry.
2294  *
2295  * @param obj The entry object
2296  *
2297  * @ingroup Entry
2298  */
2299 EAPI void
2300 elm_entry_selection_paste(Evas_Object *obj)
2301 {
2302    ELM_CHECK_WIDTYPE(obj, widtype);
2303    Widget_Data *wd = elm_widget_data_get(obj);
2304    if (!wd) return;
2305    _paste(obj, NULL, NULL);
2306 }
2307
2308 /**
2309  * This clears and frees the items in a entry's contextual (right click) menu.
2310  *
2311  * @param obj The entry object
2312  *
2313  * @ingroup Entry
2314  */
2315 EAPI void
2316 elm_entry_context_menu_clear(Evas_Object *obj)
2317 {
2318    ELM_CHECK_WIDTYPE(obj, widtype);
2319    Widget_Data *wd = elm_widget_data_get(obj);
2320    Elm_Entry_Context_Menu_Item *it;
2321    if (!wd) return;
2322    EINA_LIST_FREE(wd->items, it)
2323      {
2324         eina_stringshare_del(it->label);
2325         eina_stringshare_del(it->icon_file);
2326         eina_stringshare_del(it->icon_group);
2327         free(it);
2328      }
2329 }
2330
2331 /**
2332  * This adds an item to the entry's contextual menu.
2333  *
2334  * @param obj The entry object
2335  * @param label The item's text label
2336  * @param icon_file The item's icon file
2337  * @param icon_type The item's icon type
2338  * @param func The callback to execute when the item is clicked
2339  * @param data The data to associate with the item for related functions
2340  *
2341  * @ingroup Entry
2342  */
2343 EAPI void
2344 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)
2345 {
2346    ELM_CHECK_WIDTYPE(obj, widtype);
2347    Widget_Data *wd = elm_widget_data_get(obj);
2348    Elm_Entry_Context_Menu_Item *it;
2349    if (!wd) return;
2350    it = calloc(1, sizeof(Elm_Entry_Context_Menu_Item));
2351    if (!it) return;
2352    wd->items = eina_list_append(wd->items, it);
2353    it->obj = obj;
2354    it->label = eina_stringshare_add(label);
2355    it->icon_file = eina_stringshare_add(icon_file);
2356    it->icon_type = icon_type;
2357    it->func = func;
2358    it->data = (void *)data;
2359 }
2360
2361 /**
2362  * This disables the entry's contextual (right click) menu.
2363  *
2364  * @param obj The entry object
2365  * @param disabled If true, the menu is disabled
2366  *
2367  * @ingroup Entry
2368  */
2369 EAPI void
2370 elm_entry_context_menu_disabled_set(Evas_Object *obj, Eina_Bool disabled)
2371 {
2372    ELM_CHECK_WIDTYPE(obj, widtype);
2373    Widget_Data *wd = elm_widget_data_get(obj);
2374    if (!wd) return;
2375    if (wd->context_menu == !disabled) return;
2376    wd->context_menu = !disabled;
2377 }
2378
2379 /**
2380  * This returns whether the entry's contextual (right click) menu is disabled.
2381  *
2382  * @param obj The entry object
2383  * @return If true, the menu is disabled
2384  *
2385  * @ingroup Entry
2386  */
2387 EAPI Eina_Bool
2388 elm_entry_context_menu_disabled_get(const Evas_Object *obj)
2389 {
2390    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
2391    Widget_Data *wd = elm_widget_data_get(obj);
2392    if (!wd) return EINA_FALSE;
2393    return !wd->context_menu;
2394 }
2395
2396 /**
2397  * This appends a custom item provider to the list for that entry
2398  *
2399  * This appends the given callback. The list is walked from beginning to end
2400  * with each function called given the item href string in the text. If the
2401  * function returns an object handle other than NULL (it should create an
2402  * and object to do this), then this object is used to replace that item. If
2403  * not the next provider is called until one provides an item object, or the
2404  * default provider in entry does.
2405  * 
2406  * @param obj The entry object
2407  * @param func The function called to provide the item object
2408  * @param data The data passed to @p func
2409  *
2410  * @ingroup Entry
2411  */
2412 EAPI void
2413 elm_entry_item_provider_append(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *entry, const char *item), void *data)
2414 {
2415    ELM_CHECK_WIDTYPE(obj, widtype);
2416    Widget_Data *wd = elm_widget_data_get(obj);
2417    if (!wd) return;
2418    EINA_SAFETY_ON_NULL_RETURN(func);
2419    Elm_Entry_Item_Provider *ip = calloc(1, sizeof(Elm_Entry_Item_Provider));
2420    if (!ip) return;
2421    ip->func = func;
2422    ip->data = data;
2423    wd->item_providers = eina_list_append(wd->item_providers, ip);
2424 }
2425
2426 /**
2427  * This prepends a custom item provider to the list for that entry
2428  *
2429  * This prepends the given callback. See elm_entry_item_provider_append() for
2430  * more information
2431  * 
2432  * @param obj The entry object
2433  * @param func The function called to provide the item object
2434  * @param data The data passed to @p func
2435  *
2436  * @ingroup Entry
2437  */
2438 EAPI void
2439 elm_entry_item_provider_prepend(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *entry, const char *item), void *data)
2440 {
2441    ELM_CHECK_WIDTYPE(obj, widtype);
2442    Widget_Data *wd = elm_widget_data_get(obj);
2443    if (!wd) return;
2444    EINA_SAFETY_ON_NULL_RETURN(func);
2445    Elm_Entry_Item_Provider *ip = calloc(1, sizeof(Elm_Entry_Item_Provider));
2446    if (!ip) return;
2447    ip->func = func;
2448    ip->data = data;
2449    wd->item_providers = eina_list_prepend(wd->item_providers, ip);
2450 }
2451
2452 /**
2453  * This removes a custom item provider to the list for that entry
2454  *
2455  * This removes the given callback. See elm_entry_item_provider_append() for
2456  * more information
2457  * 
2458  * @param obj The entry object
2459  * @param func The function called to provide the item object
2460  * @param data The data passed to @p func
2461  *
2462  * @ingroup Entry
2463  */
2464 EAPI void
2465 elm_entry_item_provider_remove(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *entry, const char *item), void *data)
2466 {
2467    ELM_CHECK_WIDTYPE(obj, widtype);
2468    Widget_Data *wd = elm_widget_data_get(obj);
2469    Eina_List *l;
2470    Elm_Entry_Item_Provider *ip;
2471    if (!wd) return;
2472    EINA_SAFETY_ON_NULL_RETURN(func);
2473    EINA_LIST_FOREACH(wd->item_providers, l, ip)
2474      {
2475         if ((ip->func == func) && ((!data) || (ip->data == data)))
2476           {
2477              wd->item_providers = eina_list_remove_list(wd->item_providers, l);
2478              free(ip);
2479              return;
2480           }
2481      }
2482 }
2483
2484 /**
2485  * Append a filter function for text inserted in the entry
2486  *
2487  * Append the given callback to the list. This functions will be called
2488  * whenever any text is inserted into the entry, with the text to be inserted
2489  * as a parameter. The callback function is free to alter the text in any way
2490  * it wants, but it must remember to free the given pointer and update it.
2491  * If the new text is to be discarded, the function can free it and set it text
2492  * parameter to NULL. This will also prevent any following filters from being
2493  * called.
2494  *
2495  * @param obj The entry object
2496  * @param func The function to use as text filter
2497  * @param data User data to pass to @p func
2498  *
2499  * @ingroup Entry
2500  */
2501 EAPI void
2502 elm_entry_text_filter_append(Evas_Object *obj, void (*func) (void *data, Evas_Object *entry, char **text), void *data)
2503 {
2504    Widget_Data *wd;
2505    Elm_Entry_Text_Filter *tf;
2506    ELM_CHECK_WIDTYPE(obj, widtype);
2507
2508    wd = elm_widget_data_get(obj);
2509
2510    EINA_SAFETY_ON_NULL_RETURN(func);
2511    
2512    tf = _filter_new(func, data);
2513    if (!tf) return;
2514    
2515    wd->text_filters = eina_list_append(wd->text_filters, tf);
2516 }
2517
2518 /**
2519  * Prepend a filter function for text insdrted in the entry
2520  *
2521  * Prepend the given callback to the list. See elm_entry_text_filter_append()
2522  * for more information
2523  *
2524  * @param obj The entry object
2525  * @param func The function to use as text filter
2526  * @param data User data to pass to @p func
2527  *
2528  * @ingroup Entry
2529  */
2530 EAPI void
2531 elm_entry_text_filter_prepend(Evas_Object *obj, void (*func) (void *data, Evas_Object *entry, char **text), void *data)
2532 {
2533    Widget_Data *wd;
2534    Elm_Entry_Text_Filter *tf;
2535    ELM_CHECK_WIDTYPE(obj, widtype);
2536
2537    wd = elm_widget_data_get(obj);
2538
2539    EINA_SAFETY_ON_NULL_RETURN(func);
2540
2541    tf = _filter_new(func, data);
2542    if (!tf) return;
2543    
2544    wd->text_filters = eina_list_prepend(wd->text_filters, tf);
2545 }
2546
2547 /**
2548  * Remove a filter from the list
2549  *
2550  * Removes the given callback from the filter list. See elm_entry_text_filter_append()
2551  * for more information.
2552  *
2553  * @param obj The entry object
2554  * @param func The filter function to remove
2555  * @param data The user data passed when adding the function
2556  *
2557  * @ingroup Entry
2558  */
2559 EAPI void
2560 elm_entry_text_filter_remove(Evas_Object *obj, void (*func) (void *data, Evas_Object *entry, char **text), void *data)
2561 {
2562    Widget_Data *wd;
2563    Eina_List *l;
2564    Elm_Entry_Text_Filter *tf;
2565    ELM_CHECK_WIDTYPE(obj, widtype);
2566
2567    wd = elm_widget_data_get(obj);
2568
2569    EINA_SAFETY_ON_NULL_RETURN(func);
2570
2571    EINA_LIST_FOREACH(wd->text_filters, l, tf)
2572      {
2573         if ((tf->func == func) && ((!data) || (tf->data == data)))
2574           {
2575              wd->text_filters = eina_list_remove_list(wd->text_filters, l);
2576              _filter_free(tf);
2577              return;
2578           }
2579      }
2580 }
2581
2582 /**
2583  * This converts a markup (HTML-like) string into UTF-8.
2584  * Returning string is obtained with malloc.
2585  * After use the returned string, it should be freed.
2586  *
2587  * @param s The string (in markup) to be converted
2588  * @return The converted string (in UTF-8). It should be freed.
2589  *
2590  * @ingroup Entry
2591  */
2592 EAPI char *
2593 elm_entry_markup_to_utf8(const char *s)
2594 {
2595    char *ss = _elm_util_mkup_to_text(s);
2596    if (!ss) ss = strdup("");
2597    return ss;
2598 }
2599
2600 /**
2601  * This converts a UTF-8 string into markup (HTML-like).
2602  * Returning string is obtained with malloc.
2603  * After use the returned string, it should be freed.
2604  *
2605  * @param s The string (in UTF-8) to be converted
2606  * @return The converted string (in markup). It should be freed.
2607  *
2608  * @ingroup Entry
2609  */
2610 EAPI char *
2611 elm_entry_utf8_to_markup(const char *s)
2612 {
2613    char *ss = _elm_util_text_to_mkup(s);
2614    if (!ss) ss = strdup("");
2615    return ss;
2616 }
2617
2618 /**
2619  * Filter inserted text based on user defined character and byte limits
2620  *
2621  * Add this filter to an entry to limit the characters that it will accept
2622  * based the the contents of the provided Elm_Entry_Filter_Limit_Size.
2623  * The funtion works on the UTF-8 representation of the string, converting
2624  * it from the set markup, thus not accounting for any format in it.
2625  *
2626  * The user must create an Elm_Entry_Filter_Limit_Size structure and pass
2627  * it as data when setting the filter. In it it's possible to set limits
2628  * by character count or bytes (any of them is disabled if 0), and both can
2629  * be set at the same time. In that case, it first checks for characters,
2630  * then bytes.
2631  *
2632  * The function will cut the inserted text in order to allow only the first
2633  * number of characters that are still allowed. The cut is made in
2634  * characters, even when limiting by bytes, in order to always contain
2635  * valid ones and avoid half unicode characters making it in.
2636  *
2637  * @ingroup Entry
2638  */
2639 EAPI void
2640 elm_entry_filter_limit_size(void *data, Evas_Object *entry, char **text)
2641 {
2642    Elm_Entry_Filter_Limit_Size *lim = data;
2643    char *current;
2644    int len, newlen;
2645    const char *(*text_get)(const Evas_Object *);
2646    const char *widget_type;
2647
2648    EINA_SAFETY_ON_NULL_RETURN(data);
2649    EINA_SAFETY_ON_NULL_RETURN(entry);
2650    EINA_SAFETY_ON_NULL_RETURN(text);
2651
2652    /* hack. I don't want to copy the entire function to work with
2653     * scrolled_entry */
2654    widget_type = elm_widget_type_get(entry);
2655    if (!strcmp(widget_type, "entry"))
2656       text_get = elm_entry_entry_get;
2657    else if (!strcmp(widget_type, "scrolled_entry"))
2658       text_get = elm_scrolled_entry_entry_get;
2659    else /* huh? */
2660       return;
2661
2662    current = elm_entry_markup_to_utf8(text_get(entry));
2663
2664    if (lim->max_char_count > 0)
2665      {
2666         int cut;
2667         len = evas_string_char_len_get(current);
2668         if (len >= lim->max_char_count)
2669           {
2670              free(*text);
2671              free(current);
2672              *text = NULL;
2673              return;
2674           }
2675         newlen = evas_string_char_len_get(*text);
2676         cut = strlen(*text);
2677         while ((len + newlen) > lim->max_char_count)
2678           {
2679              cut = evas_string_char_prev_get(*text, cut, NULL);
2680              newlen--;
2681           }
2682         (*text)[cut] = 0;
2683      }
2684
2685    if (lim->max_byte_count > 0)
2686      {
2687         len = strlen(current);
2688         if (len >= lim->max_byte_count)
2689           {
2690              free(*text);
2691              free(current);
2692              *text = NULL;
2693              return;
2694           }
2695         newlen = strlen(*text);
2696         while ((len + newlen) > lim->max_byte_count)
2697           {
2698              int p = evas_string_char_prev_get(*text, newlen, NULL);
2699              newlen -= (newlen - p);
2700           }
2701         if (newlen)
2702            (*text)[newlen] = 0;
2703         else
2704           {
2705              free(*text);
2706              *text = NULL;
2707           }
2708      }
2709    free(current);
2710 }
2711
2712 /**
2713  * Filter inserted text based on accepted or rejected sets of characters
2714  *
2715  * Add this filter to an entry to restrict the set of accepted characters
2716  * based on the sets in the provided Elm_Entry_Filter_Accept_Set.
2717  * This structure contains both accepted and rejected sets, but they are
2718  * mutually exclusive. If accepted is set, it will be used, otherwise it
2719  * goes on to the rejected set.
2720  */
2721 EAPI void
2722 elm_entry_filter_accept_set(void *data, Evas_Object *entry __UNUSED__, char **text)
2723 {
2724    Elm_Entry_Filter_Accept_Set *as = data;
2725    const char *set;
2726    char *insert;
2727    Eina_Bool goes_in;
2728    int read_idx, last_read_idx = 0, read_char;
2729
2730    EINA_SAFETY_ON_NULL_RETURN(data);
2731    EINA_SAFETY_ON_NULL_RETURN(text);
2732
2733    if ((!as->accepted) && (!as->rejected))
2734       return;
2735
2736    if (as->accepted)
2737      {
2738         set = as->accepted;
2739         goes_in = EINA_TRUE;
2740      }
2741    else
2742      {
2743         set = as->rejected;
2744         goes_in = EINA_FALSE;
2745      }
2746
2747    insert = *text;
2748    read_idx = evas_string_char_next_get(*text, 0, &read_char);
2749    while (read_char)
2750      {
2751         int cmp_idx, cmp_char;
2752         Eina_Bool in_set = EINA_FALSE;
2753
2754         cmp_idx = evas_string_char_next_get(set, 0, &cmp_char);
2755         while (cmp_char)
2756           {
2757              if (read_char == cmp_char)
2758                {
2759                   in_set = EINA_TRUE;
2760                   break;
2761                }
2762              cmp_idx = evas_string_char_next_get(set, cmp_idx, &cmp_char);
2763           }
2764         if (in_set == goes_in)
2765           {
2766              int size = read_idx - last_read_idx;
2767              const char *src = (*text) + last_read_idx;
2768              if (src != insert)
2769                 memcpy(insert, *text + last_read_idx, size);
2770              insert += size;
2771           }
2772         last_read_idx = read_idx;
2773         read_idx = evas_string_char_next_get(*text, read_idx, &read_char);
2774      }
2775    *insert = 0;
2776 }
2777
2778 /**
2779  * This sets the file (and implicitly loads it) for the text to display and
2780  * then edit. All changes are written back to the file after a short delay if
2781  * the entry object is set to autosave.
2782  *
2783  * @param obj The entry object
2784  * @param file The path to the file to load and save
2785  * @param format The file format
2786  *
2787  * @ingroup Entry
2788  */
2789 EAPI void
2790 elm_entry_file_set(Evas_Object *obj, const char *file, Elm_Text_Format format)
2791 {
2792    ELM_CHECK_WIDTYPE(obj, widtype);
2793    Widget_Data *wd = elm_widget_data_get(obj);
2794    if (!wd) return;
2795    if (wd->delay_write)
2796      {
2797         ecore_timer_del(wd->delay_write);
2798         wd->delay_write = NULL;
2799      }
2800    if (wd->autosave) _save(obj);
2801    eina_stringshare_replace(&wd->file, file);
2802    wd->format = format;
2803    _load(obj);
2804 }
2805
2806 /**
2807  * Gets the file to load and save and the file format
2808  *
2809  * @param obj The entry object
2810  * @param file The path to the file to load and save
2811  * @param format The file format
2812  *
2813  * @ingroup Entry
2814  */
2815 EAPI void
2816 elm_entry_file_get(const Evas_Object *obj, const char **file, Elm_Text_Format *format)
2817 {
2818    ELM_CHECK_WIDTYPE(obj, widtype);
2819    Widget_Data *wd = elm_widget_data_get(obj);
2820    if (!wd) return;
2821    if (file) *file = wd->file;
2822    if (format) *format = wd->format;
2823 }
2824
2825 /**
2826  * This function writes any changes made to the file set with
2827  * elm_entry_file_set()
2828  *
2829  * @param obj The entry object
2830  *
2831  * @ingroup Entry
2832  */
2833 EAPI void
2834 elm_entry_file_save(Evas_Object *obj)
2835 {
2836    ELM_CHECK_WIDTYPE(obj, widtype);
2837    Widget_Data *wd = elm_widget_data_get(obj);
2838    if (!wd) return;
2839    if (wd->delay_write)
2840      {
2841         ecore_timer_del(wd->delay_write);
2842         wd->delay_write = NULL;
2843      }
2844    _save(obj);
2845    wd->delay_write = ecore_timer_add(2.0, _delay_write, obj);
2846 }
2847
2848 /**
2849  * This sets the entry object to 'autosave' the loaded text file or not.
2850  *
2851  * @param obj The entry object
2852  * @param autosave Autosave the loaded file or not
2853  *
2854  * @ingroup Entry
2855  */
2856 EAPI void
2857 elm_entry_autosave_set(Evas_Object *obj, Eina_Bool autosave)
2858 {
2859    ELM_CHECK_WIDTYPE(obj, widtype);
2860    Widget_Data *wd = elm_widget_data_get(obj);
2861    if (!wd) return;
2862    wd->autosave = !!autosave;
2863 }
2864
2865 /**
2866  * This gets the entry object's 'autosave' status.
2867  *
2868  * @param obj The entry object
2869  * @return Autosave the loaded file or not
2870  *
2871  * @ingroup Entry
2872  */
2873 EAPI Eina_Bool
2874 elm_entry_autosave_get(const Evas_Object *obj)
2875 {
2876    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
2877    Widget_Data *wd = elm_widget_data_get(obj);
2878    if (!wd) return EINA_FALSE;
2879    return wd->autosave;
2880 }
2881
2882
2883 /**
2884  * Control pasting of text and images for the widget.
2885  *
2886  * Normally the entry allows both text and images to be pasted.  By setting
2887  * textonly to be true, this prevents images from being pasted.
2888  *
2889  * Note this only changes the behaviour of text.
2890  *
2891  * @param obj The entry object
2892  * @param textonly paste mode - EINA_TRUE is text only, EINA_FALSE is text+image+other.
2893  *
2894  * @ingroup Entry
2895  */
2896 EAPI void
2897 elm_entry_cnp_textonly_set(Evas_Object *obj, Eina_Bool textonly)
2898 {
2899    Elm_Sel_Format format = ELM_SEL_FORMAT_MARKUP;
2900    ELM_CHECK_WIDTYPE(obj, widtype);
2901    Widget_Data *wd = elm_widget_data_get(obj);
2902    if (!wd) return;
2903    textonly = !!textonly;
2904    if (wd->textonly == textonly) return;
2905    wd->textonly = !!textonly;
2906    if (!textonly) format |= ELM_SEL_FORMAT_IMAGE;
2907 #ifdef HAVE_ELEMENTARY_X
2908    elm_drop_target_add(obj, format, _drag_drop_cb, NULL);
2909 #endif
2910 }
2911
2912 /**
2913  * Getting elm_entry text paste/drop mode.
2914  *
2915  * In textonly mode, only text may be pasted or dropped into the widget.
2916  *
2917  * @param obj The entry object
2918  * @return If the widget only accepts text from pastes.
2919  *
2920  * @ingroup Entry
2921  */
2922 EAPI Eina_Bool
2923 elm_entry_cnp_textonly_get(Evas_Object *obj)
2924 {
2925    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
2926    Widget_Data *wd = elm_widget_data_get(obj);
2927    if (!wd) return EINA_FALSE;
2928    return wd->textonly;
2929 }
2930