fix up copy & paste with inlined win and a few other cnp nigglies.
[framework/uifw/elementary.git] / src / lib / elm_cnp_helper.c
1 #ifdef HAVE_CONFIG_H
2 # include "elementary_config.h"
3 #endif
4 #include <Elementary.h>
5 #include "elm_priv.h"
6
7 #include <sys/mman.h>
8
9 #ifdef HAVE_ELEMENTARY_X
10
11 #define ARRAYINIT(foo)  [foo] =
12
13 #define DEBUGON 1
14
15
16 #ifdef DEBUGON
17 # define cnp_debug(x...) fprintf(stderr, __FILE__": " x)
18 #else
19 # define cnp_debug(x...)
20 #endif
21
22 #define PROVIDER_SET "__elm_cnp_provider_set"
23
24 typedef struct _Paste_Image   Paste_Image;
25 typedef struct _Cnp_Selection Cnp_Selection;
26 typedef struct _Escape        Escape;
27 typedef struct _Tmp_Info      Tmp_Info;
28 typedef struct _Cnp_Atom      Cnp_Atom;
29 typedef struct _Saved_Type    Saved_Type;
30 typedef struct _Dropable      Dropable;
31
32 typedef Eina_Bool (*Converter_Fn_Cb)     (char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
33 typedef int       (*Response_Handler_Cb) (Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *);
34 typedef int       (*Notify_Handler_Cb)   (Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *);
35
36 enum
37 {
38    CNP_ATOM_TARGETS = 0,
39    CNP_ATOM_text_uri,
40    CNP_ATOM_text_urilist,
41    CNP_ATOM_text_x_vcard,
42    CNP_ATOM_image_png,
43    CNP_ATOM_image_jpeg,
44    CNP_ATOM_image_bmp,
45    CNP_ATOM_image_gif,
46    CNP_ATOM_image_tiff,
47    CNP_ATOM_image_svg,
48    CNP_ATOM_image_xpm,
49    CNP_ATOM_image_tga,
50    CNP_ATOM_image_ppm,
51    CNP_ATOM_XELM,
52    CNP_ATOM_text_html_utf8,
53    CNP_ATOM_text_html,
54    CNP_ATOM_UTF8STRING,
55    CNP_ATOM_STRING,
56    CNP_ATOM_TEXT,
57    CNP_ATOM_text_plain_utf8,
58    CNP_ATOM_text_plain,
59
60    CNP_N_ATOMS,
61 };
62
63 struct _Paste_Image
64 {
65    Evas_Object *entry;
66    const char  *tag;
67    const char  *file;
68    Evas_Object *img;
69 };
70
71 struct _Cnp_Selection
72 {
73    const char      *debug;
74    Evas_Object     *widget;
75    char            *selbuf;
76    Evas_Object     *requestwidget;
77    void            *udata;
78    Elm_Sel_Format   requestformat;
79    Elm_Drop_Cb      datacb;
80    Eina_Bool      (*set)     (Ecore_X_Window, const void *data, int size);
81    Eina_Bool      (*clear)   (void);
82    void           (*request) (Ecore_X_Window, const char *target);
83
84    Elm_Sel_Format    format;
85    Ecore_X_Selection ecore_sel;
86
87    Eina_Bool         active : 1;
88 };
89
90 struct _Escape
91 {
92    const char *escape;
93    const char  value;
94 };
95
96 struct _Tmp_Info
97 {
98    char *filename;
99    void *map;
100    int   fd;
101    int   len;
102 };
103
104 struct _Cnp_Atom
105 {
106    const char          *name;
107    Elm_Sel_Format       formats;
108    /* Called by ecore to do conversion */
109    Converter_Fn_Cb      converter;
110    Response_Handler_Cb  response;
111    Notify_Handler_Cb    notify;
112    /* Atom */
113    Ecore_X_Atom         atom;
114 };
115
116 struct _Saved_Type
117 {
118    const char  **types;
119    Paste_Image  *pi;
120    int           ntypes;
121    int           x, y;
122    Eina_Bool     textreq: 1;
123 };
124
125 struct _Dropable
126 {
127    Evas_Object     *obj;
128    /* FIXME: Cache window */
129    Elm_Sel_Format   types;
130    Elm_Drop_Cb      dropcb;
131    void            *cbdata;
132 };
133
134 static Tmp_Info *elm_cnp_tempfile_create(int size);
135 static int tmpinfo_free(Tmp_Info *tmp);
136
137 static Eina_Bool _elm_cnp_init(void);
138 static Eina_Bool selection_clear(void *udata __UNUSED__, int type, void *event);
139 static Eina_Bool selection_notify(void *udata __UNUSED__, int type, void *event);
140 static char *remove_tags(const char *p, int *len);
141 static char *mark_up(const char *start, int inlen, int *lenp);
142
143 static Evas_Object *image_provider(void *images, Evas_Object *entry, const char *item);
144 static void entry_deleted(void *images, Evas *e, Evas_Object *entry, void *unused);
145
146
147 static Eina_Bool targets_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
148 static Eina_Bool text_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
149 static Eina_Bool html_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
150 static Eina_Bool edje_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
151 static Eina_Bool uri_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
152 static Eina_Bool image_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
153 static Eina_Bool vcard_send(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
154
155 static int response_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *);
156
157 static int notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
158 static int notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
159 static int notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
160 static int notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
161 static int notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
162 static int vcard_receive(Cnp_Selection *sed, Ecore_X_Event_Selection_Notify *notify);
163
164 static Paste_Image *pasteimage_alloc(const char *file, int pathlen);
165 static Eina_Bool pasteimage_append(Paste_Image *pi, Evas_Object *entry);
166 static void pasteimage_free(Paste_Image *pi);
167
168 /* Optimisation: Turn this into a 256 byte table:
169  *      then can lookup in one index, not N checks */
170 static const Escape escapes[] = {
171        { "<br>",   '\n' },
172        { "<\t>",   '\t' },
173        { "gt;",    '>'  },
174        { "lt;",    '<'  },
175        { "amp;",   '&'  },
176        { "quot;",  '\'' },
177        { "dquot;", '"'  }
178 };
179 #define N_ESCAPES ((int)(sizeof(escapes) / sizeof(escapes[0])))
180
181 static Cnp_Atom atoms[CNP_N_ATOMS] = {
182      [CNP_ATOM_TARGETS] = {
183           "TARGETS",
184           (Elm_Sel_Format) -1, // everything
185           targets_converter,
186           response_handler_targets,
187           notify_handler_targets,
188           0
189      },
190      [CNP_ATOM_XELM] =  {
191           "application/x-elementary-markup",
192           ELM_SEL_FORMAT_MARKUP,
193           edje_converter,
194           NULL,
195           NULL,
196           0
197      },
198      [CNP_ATOM_text_uri] = {
199           "text/uri",
200           ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_IMAGE, /* Either images or entries */
201           uri_converter,
202           NULL,
203           notify_handler_uri,
204           0
205      },
206      [CNP_ATOM_text_urilist] = {
207           "text/uri-list",
208           ELM_SEL_FORMAT_IMAGE,
209           uri_converter,
210           NULL,
211           notify_handler_uri,
212           0
213      },
214      [CNP_ATOM_text_x_vcard] = {
215           "text/x-vcard",
216           ELM_SEL_FORMAT_VCARD,
217           vcard_send, NULL,
218           vcard_receive, 0
219      },
220      [CNP_ATOM_image_png] = {
221           "image/png",
222           ELM_SEL_FORMAT_IMAGE,
223           image_converter,
224           NULL,
225           notify_handler_image,
226           0
227      },
228      [CNP_ATOM_image_jpeg] = {
229           "image/jpeg",
230           ELM_SEL_FORMAT_IMAGE,
231           image_converter,
232           NULL,
233           notify_handler_image,/* Raw image data is the same */
234           0
235      },
236      [CNP_ATOM_image_bmp] = {
237           "image/x-ms-bmp",
238           ELM_SEL_FORMAT_IMAGE,
239           image_converter,
240           NULL,
241           notify_handler_image,/* Raw image data is the same */
242           0
243      },
244      [CNP_ATOM_image_gif] = {
245           "image/gif",
246           ELM_SEL_FORMAT_IMAGE,
247           image_converter,
248           NULL,
249           notify_handler_image,/* Raw image data is the same */
250           0
251      },
252      [CNP_ATOM_image_tiff] = {
253           "image/tiff",
254           ELM_SEL_FORMAT_IMAGE,
255           image_converter,
256           NULL,
257           notify_handler_image,/* Raw image data is the same */
258           0
259      },
260      [CNP_ATOM_image_svg] = {
261           "image/svg+xml",
262           ELM_SEL_FORMAT_IMAGE,
263           image_converter,
264           NULL,
265           notify_handler_image,/* Raw image data is the same */
266           0
267      },
268      [CNP_ATOM_image_xpm] = {
269           "image/x-xpixmap",
270           ELM_SEL_FORMAT_IMAGE,
271           image_converter,
272           NULL,
273           notify_handler_image,/* Raw image data is the same */
274           0
275      },
276      [CNP_ATOM_image_tga] = {
277           "image/x-tga",
278           ELM_SEL_FORMAT_IMAGE,
279           image_converter,
280           NULL,
281           notify_handler_image,/* Raw image data is the same */
282           0
283      },
284      [CNP_ATOM_image_ppm] = {
285           "image/x-portable-pixmap",
286           ELM_SEL_FORMAT_IMAGE,
287           image_converter,
288           NULL,
289           notify_handler_image,/* Raw image data is the same */
290           0
291      },
292      [CNP_ATOM_text_html_utf8] = {
293           "text/html;charset=utf-8",
294           ELM_SEL_FORMAT_HTML,
295           html_converter,
296           NULL,
297           notify_handler_html,
298           0
299      },
300      [CNP_ATOM_text_html] = {
301           "text/html",
302           ELM_SEL_FORMAT_HTML,
303           html_converter,
304           NULL,
305           notify_handler_html, /* No encoding: Webkit only */
306           0
307      },
308      [CNP_ATOM_UTF8STRING] = {
309           "UTF8_STRING",
310           ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
311           text_converter,
312           NULL,
313           notify_handler_text,
314           0
315      },
316      [CNP_ATOM_STRING] = {
317           "STRING",
318           ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
319           text_converter,
320           NULL,
321           notify_handler_text,
322           0
323      },
324      [CNP_ATOM_TEXT] = {
325           "TEXT",
326           ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
327           text_converter,
328           NULL,
329           NULL,
330           0
331      },
332      [CNP_ATOM_text_plain_utf8] = {
333           "text/plain;charset=utf-8",
334           ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
335           text_converter,
336           NULL,
337           NULL,
338           0
339      },
340      [CNP_ATOM_text_plain] = {
341           "text/plain",
342           ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
343           text_converter,
344           NULL,
345           NULL,
346           0
347      },
348 };
349
350 static Cnp_Selection selections[ELM_SEL_MAX] = {
351      ARRAYINIT(ELM_SEL_PRIMARY) {
352           .debug = "Primary",
353           .ecore_sel = ECORE_X_SELECTION_PRIMARY,
354           .set = ecore_x_selection_primary_set,
355           .clear = ecore_x_selection_primary_clear,
356           .request = ecore_x_selection_primary_request,
357      },
358      ARRAYINIT(ELM_SEL_SECONDARY) {
359           .debug = "Secondary",
360           .ecore_sel = ECORE_X_SELECTION_SECONDARY,
361           .set = ecore_x_selection_secondary_set,
362           .clear = ecore_x_selection_secondary_clear,
363           .request = ecore_x_selection_secondary_request,
364      },
365      ARRAYINIT(ELM_SEL_CLIPBOARD) {
366           .debug = "Clipboard",
367           .ecore_sel = ECORE_X_SELECTION_CLIPBOARD,
368           .set = ecore_x_selection_clipboard_set,
369           .clear = ecore_x_selection_clipboard_clear,
370           .request = ecore_x_selection_clipboard_request,
371      },
372      ARRAYINIT(ELM_SEL_XDND) {
373           .debug = "XDnD",
374           .ecore_sel = ECORE_X_SELECTION_XDND,
375           .request = ecore_x_selection_xdnd_request,
376      },
377 };
378
379 /* Data for DND in progress */
380 static Saved_Type savedtypes =  { NULL, NULL, 0, 0, 0, EINA_FALSE };
381
382 static void (*dragdonecb) (void *data, Evas_Object *obj) = NULL;
383 static void *dragdonedata = NULL;
384
385 static int _elm_cnp_init_count = 0;
386 /* FIXME: who left this out of XAtoms.h */
387 static Ecore_X_Atom clipboard_atom;
388
389 static Eina_List *pastedimages = NULL;
390
391 /**
392  * Drag & Drop functions
393  */
394
395 /* FIXME: Way too many globals */
396 static Eina_List *drops = NULL;
397 static Evas_Object *dragwin = NULL;
398 static int _dragx = 0, _dragy = 0;
399 static Ecore_Event_Handler *handler_pos = NULL;
400 static Ecore_Event_Handler *handler_drop = NULL;
401 static Ecore_Event_Handler *handler_enter = NULL;
402 static Ecore_Event_Handler *handler_status = NULL;
403
404 #endif
405
406 /* Stringshared, so I can just compare pointers later */
407 static const char *text_uri;
408
409 Eina_Bool
410 elm_selection_set(Elm_Sel_Type selection, Evas_Object *widget, Elm_Sel_Format format, const char *selbuf)
411 {
412 #ifdef HAVE_ELEMENTARY_X
413    Evas_Object *top = elm_widget_top_get(widget);
414    Ecore_X_Window xwin;
415    Cnp_Selection *sel;
416
417    if (top) xwin = elm_win_xwindow_get(top);
418    else xwin = elm_win_xwindow_get(widget);
419    if (!xwin) return EINA_FALSE;
420    if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
421    if (!_elm_cnp_init_count) _elm_cnp_init();
422    if ((!selbuf) && (format != ELM_SEL_FORMAT_IMAGE))
423      return elm_selection_clear(selection, widget);
424
425    sel = selections + selection;
426
427    sel->active = 1;
428    sel->widget = widget;
429
430    sel->set(xwin, &selection, sizeof(Elm_Sel_Type));
431    sel->format = format;
432    sel->selbuf = selbuf ? strdup(selbuf) : NULL;
433
434    return EINA_TRUE;
435 #else
436    return EINA_FALSE;
437 #endif
438 }
439
440 Eina_Bool
441 elm_selection_clear(Elm_Sel_Type selection, Evas_Object *widget)
442 {
443 #ifdef HAVE_ELEMENTARY_X
444    Cnp_Selection *sel;
445
446    if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
447    if (!_elm_cnp_init_count) _elm_cnp_init();
448
449    sel = selections + selection;
450
451    /* No longer this selection: Consider it gone! */
452    if ((!sel->active) || (sel->widget != widget)) return EINA_TRUE;
453
454    sel->active = 0;
455    sel->widget = NULL;
456    sel->clear();
457
458    return EINA_TRUE;
459 #else
460    return EINA_FALSE;
461 #endif
462 }
463
464 Eina_Bool
465 elm_selection_get(Elm_Sel_Type selection, Elm_Sel_Format format,
466                   Evas_Object *widget, Elm_Drop_Cb datacb, void *udata)
467 {
468 #ifdef HAVE_ELEMENTARY_X
469    Evas_Object *top;
470    Cnp_Selection *sel;
471
472    if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
473    if (!_elm_cnp_init_count) _elm_cnp_init();
474
475    sel = selections + selection;
476    top = elm_widget_top_get(widget);
477    if (!top) return EINA_FALSE;
478
479    sel->requestformat = format;
480    sel->requestwidget = widget;
481    sel->request(elm_win_xwindow_get(top), ECORE_X_SELECTION_TARGET_TARGETS);
482    sel->datacb = datacb;
483    sel->udata = udata;
484
485    return EINA_TRUE;
486 #else
487    return EINA_FALSE;
488 #endif
489 }
490
491 #ifdef HAVE_ELEMENTARY_X
492
493 static Eina_Bool
494 _elm_cnp_init(void)
495 {
496    int i;
497
498    if (_elm_cnp_init_count++) return EINA_TRUE;
499    for (i = 0; i < CNP_N_ATOMS; i++)
500      {
501         atoms[i].atom = ecore_x_atom_get(atoms[i].name);
502         ecore_x_selection_converter_atom_add(atoms[i].atom,
503                                              atoms[i].converter);
504      }
505    clipboard_atom = ecore_x_atom_get("CLIPBOARD");
506
507    ecore_event_handler_add(ECORE_X_EVENT_SELECTION_CLEAR, selection_clear, NULL);
508    ecore_event_handler_add(ECORE_X_EVENT_SELECTION_NOTIFY, selection_notify, NULL);
509
510    text_uri = eina_stringshare_add("text/uri-list");
511    return EINA_TRUE;
512 }
513
514 static Eina_Bool
515 selection_clear(void *udata __UNUSED__, int type __UNUSED__, void *event)
516 {
517    Ecore_X_Event_Selection_Clear *ev = event;
518    Cnp_Selection *sel;
519    int i;
520
521    for (i = 0; i < ELM_SEL_MAX; i++)
522      {
523         if (selections[i].ecore_sel == ev->selection) break;
524      }
525    cnp_debug("selection %d clear\n", i);
526    /* Not me... Don't care */
527    if (i == ELM_SEL_MAX) return ECORE_CALLBACK_PASS_ON;
528
529    sel = selections + i;
530    sel->active = 0;
531    sel->widget = NULL;
532    sel->selbuf = NULL;
533
534    return ECORE_CALLBACK_PASS_ON;
535 }
536
537
538 /*
539  * Response to a selection notify:
540  *      - So we have asked for the selection list.
541  *      - If it's the targets list, parse it, and fire of what we want,
542  *      else it's the data we want.
543  */
544 static Eina_Bool
545 selection_notify(void *udata __UNUSED__, int type __UNUSED__, void *event)
546 {
547    Ecore_X_Event_Selection_Notify *ev = event;
548    Cnp_Selection *sel;
549    int i;
550
551    cnp_debug("selection notify callback: %d\n",ev->selection);
552    switch (ev->selection)
553      {
554       case ECORE_X_SELECTION_CLIPBOARD:
555          sel = selections + ELM_SEL_CLIPBOARD;
556          break;
557       case ECORE_X_SELECTION_PRIMARY:
558          sel = selections + ELM_SEL_PRIMARY;
559          break;
560       case ECORE_X_SELECTION_SECONDARY:
561          sel = selections + ELM_SEL_SECONDARY;
562          break;
563       case ECORE_X_SELECTION_XDND:
564          sel = selections + ELM_SEL_XDND;
565          break;
566       default:
567          return ECORE_CALLBACK_PASS_ON;
568      }
569    cnp_debug("Target is %s\n", ev->target);
570
571    for (i = 0; i < CNP_N_ATOMS; i++)
572      {
573         if (!strcmp(ev->target, atoms[i].name))
574           {
575              if (atoms[i].notify)
576                {
577                   cnp_debug("Found something: %s\n", atoms[i].name);
578                   atoms[i].notify(sel, ev);
579                }
580              else
581                {
582                   cnp_debug("Ignored: No handler!\n");
583                }
584           }
585      }
586
587    return ECORE_CALLBACK_PASS_ON;
588 }
589
590
591
592 static Eina_Bool
593 targets_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize)
594 {
595    int i,count;
596    Ecore_X_Atom *aret;
597    Cnp_Selection *sel;
598
599    if (!data_ret) return EINA_FALSE;
600
601    sel = selections + *((int *)data);
602
603    for (i = 0, count = 0; i < CNP_N_ATOMS ; i++)
604      {
605         if (sel->format & atoms[i].formats) count++;
606      }
607
608    aret = malloc(sizeof(Ecore_X_Atom) * count);
609    for (i = 0, count = 0; i < CNP_N_ATOMS; i++)
610      {
611         if (sel->format & atoms[i].formats) aret[count ++] = atoms[i].atom;
612      }
613
614    *data_ret = aret;
615    if (typesize) *typesize = 32 /* urk */;
616    if (ttype) *ttype = ECORE_X_ATOM_ATOM;
617    if (size_ret) *size_ret = count;
618
619    return EINA_TRUE;
620 }
621
622 static Eina_Bool
623 image_converter(char *target __UNUSED__, void *data __UNUSED__, int size __UNUSED__, void **data_ret __UNUSED__, int *size_ret __UNUSED__, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
624 {
625    cnp_debug("Image converter called\n");
626    return EINA_TRUE;
627 }
628
629 static Eina_Bool
630 vcard_send(char *target __UNUSED__, void *data __UNUSED__, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
631 {
632    Cnp_Selection *sel;
633
634    cnp_debug("Vcard send called\n");
635
636    sel = selections + *((int *)data);
637
638    if (data_ret) *data_ret = strdup(sel->selbuf);
639    if (size_ret) *size_ret = strlen(sel->selbuf);
640
641    return EINA_TRUE;
642 }
643 /*
644  * Callback to handle a targets response on a selection request:
645  * So pick the format we'd like; and then request it.
646  */
647 static int
648 notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
649 {
650    Ecore_X_Selection_Data_Targets *targets;
651    Ecore_X_Atom *atomlist;
652    Evas_Object *top;
653    int i, j;
654
655    targets = notify->data;
656    atomlist = (Ecore_X_Atom *)(targets->data.data);
657
658    for (j = 1; j < CNP_N_ATOMS; j++)
659      {
660         cnp_debug("\t%s %d\n", atoms[j].name, atoms[j].atom);
661         if (!(atoms[j].formats & sel->requestformat)) continue;
662         for (i = 0; i < targets->data.length; i++)
663           {
664              if ((atoms[j].atom == atomlist[i]) && (atoms[j].notify))
665                {
666                   cnp_debug("Atom %s matches\n",atoms[j].name);
667                   goto done;
668                }
669           }
670      }
671
672    cnp_debug("Couldn't find anything that matches\n");
673    return ECORE_CALLBACK_PASS_ON;
674
675 done:
676    top = elm_widget_top_get(sel->requestwidget);
677    if (!top) top = sel->requestwidget;
678    cnp_debug("Sending request for %s\n", atoms[j].name);
679    sel->request(elm_win_xwindow_get(top), atoms[j].name);
680
681    return ECORE_CALLBACK_PASS_ON;
682 }
683
684 static int
685 response_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
686 {
687    Ecore_X_Selection_Data_Targets *targets;
688    Ecore_X_Atom *atomlist;
689    Evas_Object *top;
690    int i,j;
691
692    targets = notify->data;
693    atomlist = (Ecore_X_Atom *)(targets->data.data);
694
695    /* Start from 1: Skip targets */
696    for (j = 1 ; j < CNP_N_ATOMS ; j ++)
697      {
698         if (!(atoms[j].formats & sel->requestformat)) continue;
699         for (i = 0 ; i < targets->data.length ; i ++)
700           {
701              if ((atoms[j].atom == atomlist[i]) && (atoms[j].response))
702                {
703                   /* Found a match: Use it */
704                   goto found;
705                }
706           }
707      }
708 found:
709    if (j == CNP_N_ATOMS)
710      {
711         cnp_debug("No matching type found\n");
712         return 0;
713      }
714
715    top = elm_widget_top_get(sel->requestwidget);
716    if (!top) return 0;
717
718    sel->request(elm_win_xwindow_get(top), atoms[j].name);
719    return 0;
720 }
721
722
723 static int
724 notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
725 {
726    Ecore_X_Selection_Data *data;
727    char *str;
728
729    data = notify->data;
730    cnp_debug("Notify handler text %d %d %p\n", data->format,data->length, data->data);
731    str = mark_up((char *)data->data, data->length, NULL);
732    cnp_debug("String is %s (from %s)\n", str, data->data);
733    elm_entry_entry_insert(sel->requestwidget, str);
734    free(str);
735    return 0;
736 }
737
738
739 /**
740  * So someone is pasting an image into my entry or widget...
741  */
742 static int
743 notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
744 {
745    Ecore_X_Selection_Data *data;
746    Ecore_X_Selection_Data_Files *files;
747    Paste_Image *pi;
748    char *p;
749
750    data = notify->data;
751    cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
752    if (data->content == ECORE_X_SELECTION_CONTENT_FILES)
753      {
754         cnp_debug("got a files list\n");
755         files = notify->data;
756         if (files->num_files > 1)
757           {
758              /* Don't handle many items */
759              cnp_debug("more then one file: Bailing\n");
760              return 0;
761           }
762         p = files->files[0];
763      }
764    else
765      {
766         p = (char *)data->data;
767      }
768
769    if (!p)
770      {
771         cnp_debug("Couldn't find a file\n");
772         return 0;
773      }
774    cnp_debug("Got %s\n",p);
775    if (strncmp(p, "file://", 7))
776      {
777         /* Try and continue if it looks sane */
778         if (*p != '/') return 0;
779      }
780    else
781      {
782         p += strlen("file://");
783      }
784
785    if (savedtypes.pi) pasteimage_free(savedtypes.pi);
786    pi = pasteimage_alloc(p, strlen(p));
787    if (savedtypes.textreq)
788      {
789         savedtypes.textreq = 0;
790         savedtypes.pi = pi;
791      }
792    else
793      {
794         pasteimage_append(pi, sel->requestwidget);
795         savedtypes.pi = NULL;
796      }
797    return 0;
798 }
799
800 /**
801  * Just receieved an vcard, either through cut and paste, or dnd.
802  */
803 static int
804 vcard_receive(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
805 {
806    Dropable *dropable;
807    Eina_List *l;
808    Ecore_X_Selection_Data *data;
809
810    data = notify->data;
811    cnp_debug("vcard receive\n");
812
813    if (sel == (selections + ELM_SEL_XDND))
814      {
815         Elm_Selection_Data ddata;
816
817         cnp_debug("drag & drop\n");
818         /* FIXME: this needs to be generic: Used for all receives */
819         EINA_LIST_FOREACH(drops, l, dropable)
820           {
821              if (dropable->obj == sel->requestwidget) break;
822           }
823         if (!dropable)
824           {
825              cnp_debug("Unable to find drop object");
826              ecore_x_dnd_send_finished();
827              return 0;
828           }
829         dropable = eina_list_data_get(l);
830         ddata.x = savedtypes.x;
831         ddata.y = savedtypes.y;
832         ddata.format = ELM_SEL_FORMAT_VCARD;
833         ddata.data = data->data;
834         ddata.len = data->length;
835         dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
836         ecore_x_dnd_send_finished();
837      }
838    else if (sel->datacb)
839      {
840         Elm_Selection_Data ddata;
841         ddata.x = ddata.y = 0;
842         ddata.format = ELM_SEL_FORMAT_IMAGE;
843         ddata.data = data->data;
844         ddata.len = data->length;
845         sel->datacb(sel->udata, sel->widget, &ddata);
846      }
847    else
848      {
849         cnp_debug("Paste request\n");
850      }
851
852    return 0;
853
854 }
855
856
857 static int
858 notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
859 {
860    Ecore_X_Selection_Data *data;
861    Tmp_Info *tmp;
862    Paste_Image *pi;
863
864    cnp_debug("got a png (or a jpeg)!\n");
865    data = notify->data;
866
867    cnp_debug("Size if %d\n", data->length);
868
869    if (sel->datacb)
870      {
871         Elm_Selection_Data ddata;
872
873         ddata.x = ddata.y = 0;
874         ddata.format = ELM_SEL_FORMAT_IMAGE;
875         ddata.data = data->data;
876         ddata.len = data->length;
877         sel->datacb(sel->udata, sel->widget, &ddata);
878         return 0;
879      }
880
881    /* generate tmp name */
882    tmp = elm_cnp_tempfile_create(data->length);
883    memcpy(tmp->map, data->data, data->length);
884    munmap(tmp->map,data->length);
885
886    /* FIXME: Add to paste image data to clean up */
887    pi = pasteimage_alloc(tmp->filename, strlen(tmp->filename));
888    pasteimage_append(pi, sel->requestwidget);
889
890    tmpinfo_free(tmp);
891    return 0;
892 }
893
894
895 /**
896  *    Warning: Generic text/html can';t handle it sanely.
897  *    Firefox sends ucs2 (i think).
898  *       chrome sends utf8... blerg
899  */
900 static int
901 notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
902 {
903    Ecore_X_Selection_Data *data;
904
905    cnp_debug("Got some HTML: Checking encoding is useful\n");
906    data = notify->data;
907
908    if (sel->datacb)
909      {
910         Elm_Selection_Data ddata;
911         ddata.x = ddata.y = 0;
912         ddata.format = ELM_SEL_FORMAT_HTML;
913         ddata.data = data->data;
914         ddata.len = data->length;
915         sel->datacb(sel->udata, sel->widget, &ddata);
916         return 0;
917      }
918
919    char *stripstr = NULL;
920    stripstr = malloc(sizeof(char) * (data->length + 1));
921    strncpy(stripstr, (char *)data->data, data->length);
922    stripstr[data->length] = '\0';
923    cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
924    elm_entry_entry_insert(sel->requestwidget, stripstr);
925    free(stripstr);
926    return 0;
927 }
928
929
930 static Eina_Bool
931 text_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
932 {
933    Cnp_Selection *sel;
934
935    cnp_debug("text converter\n");
936    sel = selections + *((int *)data);
937    if (!sel->active) return EINA_TRUE;
938
939    if ((sel->format & ELM_SEL_FORMAT_MARKUP) ||
940        (sel->format & ELM_SEL_FORMAT_HTML))
941      {
942         *data_ret = remove_tags(sel->selbuf, size_ret);
943      }
944    else if (sel->format & ELM_SEL_FORMAT_TEXT)
945      {
946         *data_ret = strdup(sel->selbuf);
947         *size_ret = strlen(sel->selbuf);
948      }
949    else if (sel->format & ELM_SEL_FORMAT_IMAGE)
950      {
951         cnp_debug("Image %s\n", evas_object_type_get(sel->widget));
952         cnp_debug("Elm type: %s\n", elm_object_widget_type_get(sel->widget));
953         evas_object_image_file_get(elm_photocam_internal_image_get(sel->widget), (const char **)data_ret, NULL);
954         if (!*data_ret) *data_ret = strdup("No file");
955         else *data_ret = strdup(*data_ret);
956         *size_ret = strlen(*data_ret);
957      }
958    return EINA_TRUE;
959 }
960
961 static Eina_Bool
962 edje_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
963 {
964    Cnp_Selection *sel;
965
966    sel = selections + *((int *)data);
967    if (data_ret) *data_ret = strdup(sel->selbuf);
968    if (size_ret) *size_ret = strlen(sel->selbuf);
969
970    return EINA_TRUE;
971 }
972
973
974 static Eina_Bool
975 html_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
976 {
977    Cnp_Selection *sel;
978
979    sel = selections + *(int *)data;
980    if (data_ret) *data_ret = strdup(sel->selbuf);
981    if (size_ret) *size_ret = strlen(sel->selbuf);
982
983    return EINA_TRUE;
984 }
985
986 static Eina_Bool
987 uri_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
988 {
989    Cnp_Selection *sel;
990    sel = selections + *((int *)data);
991    cnp_debug("Uri converter\n");
992    if (data_ret) *data_ret = strdup(sel->selbuf);
993    if (size_ret) *size_ret = strlen(sel->selbuf);
994    return EINA_TRUE;
995 }
996
997 /*
998  * Image paste provide
999  */
1000
1001 /* FIXME: Should add provider for each pasted item: Use data to store it
1002  * much easier */
1003 static Evas_Object *
1004 image_provider(void *images __UNUSED__, Evas_Object *entry, const char *item)
1005 {
1006    Paste_Image *pi;
1007    Eina_List *l;
1008
1009    cnp_debug("image provider for %s called\n", item);
1010    EINA_LIST_FOREACH(pastedimages, l, pi)
1011      {
1012         cnp_debug("is it %s?\n",pi->tag);
1013         if (!strcmp(pi->tag, item))
1014           {
1015              /* Found it */
1016              Evas_Object *o;
1017              o = evas_object_image_filled_add(evas_object_evas_get(entry));
1018              /* FIXME: Handle eets */
1019              cnp_debug("file is %s (object is %p)\n", pi->file, o);
1020              evas_object_image_file_set(o, pi->file, NULL);
1021              evas_object_show(o);
1022              return o;
1023           }
1024      }
1025    return NULL;
1026 }
1027
1028
1029 static Paste_Image *
1030 pasteimage_alloc(const char *file, int pathlen)
1031 {
1032    Paste_Image *pi;
1033    int len;
1034    char *buf, *filebuf;
1035    int prefixlen = strlen("file://");
1036
1037    pi = calloc(1, sizeof(Paste_Image));
1038    if (!pi) return NULL;
1039
1040    len = snprintf(NULL, 0, "pasteimage-%p", pi);
1041    len++;
1042    buf = malloc(len);
1043    if (!buf)
1044      {
1045         free(pi);
1046         return NULL;
1047      }
1048    snprintf(buf, len, "pasteimage-%p", pi);
1049    pi->tag = buf;
1050
1051    if (file)
1052      {
1053         if (strstr(file,"file://")) file += prefixlen;
1054         filebuf = alloca(pathlen + 1);
1055         strncpy(filebuf, file, pathlen);
1056         filebuf[pathlen] = 0;
1057         pi->file = strdup(filebuf);
1058      }
1059
1060    return pi;
1061 }
1062
1063 static void
1064 pasteimage_free(Paste_Image *pi)
1065 {
1066    if (!pi) return;
1067    if (pi->file) free((void*)pi->file);
1068    if (pi->tag) free((void*)pi->tag);
1069    free(pi);
1070 }
1071
1072 static Eina_Bool
1073 pasteimage_provider_set(Evas_Object *entry)
1074 {
1075    void *v;
1076    const char *type;
1077
1078    if (!entry) return EINA_FALSE;
1079    type = elm_widget_type_get(entry);
1080    cnp_debug("type is %s\n", type);
1081    if ((!type) || (strcmp(type, "entry"))) return EINA_FALSE;
1082
1083    v = evas_object_data_get(entry, PROVIDER_SET);
1084    if (!v)
1085      {
1086         evas_object_data_set(entry, PROVIDER_SET, pasteimage_provider_set);
1087         elm_entry_item_provider_append(entry, image_provider, NULL);
1088         evas_object_event_callback_add(entry, EVAS_CALLBACK_FREE,
1089                                        entry_deleted, NULL);
1090      }
1091    return EINA_TRUE;
1092 }
1093
1094
1095 static Eina_Bool
1096 pasteimage_append(Paste_Image *pi, Evas_Object *entry)
1097 {
1098    char *entrytag;
1099    int len;
1100    static const char *tagstring = "<item absize=240x180 href=file://%s></item>";
1101
1102    if (!pi) return EINA_FALSE;
1103    if (!entry) return EINA_FALSE;
1104
1105    pasteimage_provider_set(entry);
1106
1107    len = strlen(tagstring)+strlen(pi->file);
1108
1109    pastedimages = eina_list_append(pastedimages, pi);
1110    entrytag = alloca(len + 1);
1111    snprintf(entrytag, len + 1, tagstring, pi->file);
1112    elm_entry_entry_insert(entry, entrytag);
1113
1114    return EINA_TRUE;
1115 }
1116
1117 static void
1118 entry_deleted(void *images __UNUSED__, Evas *e __UNUSED__, Evas_Object *entry, void *unused __UNUSED__)
1119 {
1120    Paste_Image *pi;
1121    Eina_List *l,*next;
1122
1123    EINA_LIST_FOREACH_SAFE(pastedimages, l, next, pi)
1124      {
1125         if (pi->entry == entry)
1126           pastedimages = eina_list_remove_list(pastedimages, l);
1127      }
1128 }
1129
1130
1131 static char *
1132 remove_tags(const char *p, int *len)
1133 {
1134    char *q,*ret;
1135    int i;
1136    if (!p) return NULL;
1137
1138    q = malloc(strlen(p) + 1);
1139    if (!q) return NULL;
1140    ret = q;
1141
1142    while (*p)
1143      {
1144         if ((*p != '<') && (*p != '&')) *q++ = *p++;
1145         else if (*p == '<')
1146           {
1147              if ((p[1] == 'b') && (p[2] == 'r') &&
1148                  ((p[3] == ' ') || (p[3] == '/') || (p[3] == '>')))
1149                *q++ = '\n';
1150              while ((*p) && (*p != '>')) p++;
1151              p++;
1152           }
1153         else if (*p == '&')
1154           {
1155              p++;
1156              for (i = 0 ; i < N_ESCAPES ; i++)
1157                {
1158                   if (!strncmp(p,escapes[i].escape, strlen(escapes[i].escape)))
1159                     {
1160                        p += strlen(escapes[i].escape);
1161                        *q = escapes[i].value;
1162                        q++;
1163                        break;
1164                     }
1165                }
1166              if (i == N_ESCAPES) *q ++= '&';
1167           }
1168      }
1169    *q = 0;
1170    if (len) *len = q - ret;
1171    return ret;
1172 }
1173
1174 /* Mark up */
1175 static char *
1176 mark_up(const char *start, int inlen, int *lenp)
1177 {
1178    int l, i;
1179    const char *p;
1180    char *q, *ret;
1181    const char *endp = NULL;
1182
1183    if (!start) return NULL;
1184    if (inlen >= 0) endp = start + inlen;
1185    /* First pass: Count characters */
1186    for (l = 0, p = start; ((!endp) || (p < endp)) && (*p); p++)
1187      {
1188         for (i = 0 ; i < N_ESCAPES ; i ++)
1189           {
1190              if (*p == escapes[i].value)
1191                {
1192                   l += strlen(escapes[i].escape);
1193                   break;
1194                }
1195           }
1196         if (i == N_ESCAPES) l++;
1197      }
1198
1199    q = ret = malloc(l + 1);
1200
1201    /* Second pass: Change characters */
1202    for (p = start; *p; )
1203      {
1204         for (i = 0; i < N_ESCAPES; i++)
1205           {
1206              if (*p == escapes[i].value)
1207                {
1208                   strcpy(q, escapes[i].escape);
1209                   q += strlen(escapes[i].escape);
1210                   p ++;
1211                   break;
1212                }
1213           }
1214         if (i == N_ESCAPES) *q++ = *p++;
1215      }
1216    *q = 0;
1217
1218    if (lenp) *lenp = l;
1219    return ret;
1220 }
1221
1222
1223 static Eina_Bool
1224 _dnd_enter(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1225 {
1226    Ecore_X_Event_Xdnd_Enter *enter = ev;
1227    int i;
1228
1229    /* Skip it */
1230    if ((!enter) || (!enter->num_types) || (!enter->types)) return EINA_TRUE;
1231
1232    cnp_debug("Types\n");
1233    savedtypes.ntypes = enter->num_types;
1234    if (savedtypes.types) free(savedtypes.types);
1235    savedtypes.types = malloc(sizeof(char *) * enter->num_types);
1236    if (!savedtypes.types) return EINA_FALSE;
1237
1238    for (i = 0; i < enter->num_types; i++)
1239      {
1240         savedtypes.types[i] = eina_stringshare_add(enter->types[i]);
1241         cnp_debug("Type is %s %p %p\n", enter->types[i],
1242                   savedtypes.types[i],text_uri);
1243         if (savedtypes.types[i] == text_uri)
1244           {
1245              /* Request it, so we know what it is */
1246              cnp_debug("Sending uri request\n");
1247              savedtypes.textreq = 1;
1248              savedtypes.pi = NULL; /* FIXME: Free? */
1249              ecore_x_selection_xdnd_request(enter->win, text_uri);
1250           }
1251      }
1252
1253    /* FIXME: Find an object and make it current */
1254    return EINA_TRUE;
1255 }
1256
1257 static Eina_Bool
1258 _dnd_drop(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1259 {
1260    struct _Ecore_X_Event_Xdnd_Drop *drop;
1261    Dropable *dropable;
1262    Eina_List *l;
1263    Ecore_Evas *ee;
1264    Ecore_X_Window xwin;
1265    Elm_Selection_Data ddata;
1266    int x, y, w, h;
1267    int i, j;
1268
1269    drop = ev;
1270
1271    // check we still have something to drop
1272    if (!drops) return EINA_TRUE;
1273
1274    /* Find any widget in our window; then work out geometry rel to our window */
1275    for (l = drops; l; l = l->next)
1276      {
1277         dropable = l->data;
1278         xwin = (Ecore_X_Window)ecore_evas_window_get
1279            (ecore_evas_ecore_evas_get(evas_object_evas_get
1280                                       (dropable->obj)));
1281         if (xwin == drop->win) break;
1282      }
1283    /* didn't find a window */
1284    if (!l) return EINA_TRUE;
1285
1286    /* Calculate real (widget relative) position */
1287    // - window position
1288    // - widget position
1289    ee = ecore_evas_ecore_evas_get(evas_object_evas_get(dropable->obj));
1290    ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
1291    savedtypes.x = drop->position.x - x;
1292    savedtypes.y = drop->position.y - y;
1293
1294    cnp_debug("Drop position is %d,%d\n", savedtypes.x, savedtypes.y);
1295
1296    for (; l; l = l->next)
1297      {
1298         dropable = l->data;
1299         evas_object_geometry_get(dropable->obj, &x, &y, &w, &h);
1300         if ((savedtypes.x >= x) && (savedtypes.y >= y) &&
1301             (savedtypes.x < x + w) && (savedtypes.y < y + h))
1302           break; /* found! */
1303      }
1304
1305    if (!l) return EINA_TRUE; /* didn't find one */
1306
1307    evas_object_geometry_get(dropable->obj, &x, &y, NULL, NULL);
1308    savedtypes.x -= x;
1309    savedtypes.y -= y;
1310
1311    /* Find our type from the previous list */
1312    for (i = 0; i < CNP_N_ATOMS; i++)
1313      {
1314         for (j = 0; j < savedtypes.ntypes; j++)
1315           {
1316              if (!strcmp(savedtypes.types[j], atoms[i].name)) goto found;
1317           }
1318      }
1319
1320    cnp_debug("Didn't find a target\n");
1321    return EINA_TRUE;
1322
1323 found:
1324    cnp_debug("Found a target we'd like: %s\n", atoms[i].name);
1325    cnp_debug("0x%x\n",xwin);
1326
1327    if (i == CNP_ATOM_text_urilist)
1328      {
1329         cnp_debug("We found a URI... (%scached) %s\n",
1330                   savedtypes.pi ? "" : "not ",
1331                   savedtypes.pi->file);
1332         if (savedtypes.pi)
1333           {
1334              char *entrytag;
1335              static const char *tagstring = "<item absize=240x180 href="
1336                 "file://%s></item>";
1337              ddata.x = savedtypes.x;
1338              ddata.y = savedtypes.y;
1339
1340              /* If it's markup that also supports images */
1341              if ((dropable->types & ELM_SEL_FORMAT_MARKUP) &&
1342                  (dropable->types & ELM_SEL_FORMAT_IMAGE))
1343                {
1344                   int len;
1345                   ddata.format = ELM_SEL_FORMAT_MARKUP;
1346                   pasteimage_provider_set(dropable->obj);
1347
1348                   pastedimages = eina_list_append(pastedimages, savedtypes.pi);
1349                   len = strlen(tagstring) + strlen(savedtypes.pi->file);
1350                   entrytag = alloca(len + 1);
1351                   snprintf(entrytag, len + 1, tagstring, savedtypes.pi->file);
1352                   ddata.data = entrytag;
1353                   cnp_debug("Insert %s\n", (char *)ddata.data);
1354                   dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1355                   ecore_x_dnd_send_finished();
1356                   return EINA_TRUE;
1357                }
1358              else if (dropable->types & ELM_SEL_FORMAT_IMAGE)
1359                {
1360                   cnp_debug("Doing image insert (%s)\n", savedtypes.pi->file);
1361                   ddata.format = ELM_SEL_FORMAT_IMAGE;
1362                   ddata.data = (char *)savedtypes.pi->file;
1363                   dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1364                   ecore_x_dnd_send_finished();
1365
1366                   pasteimage_free(savedtypes.pi);
1367                   savedtypes.pi = NULL;
1368
1369                   return EINA_TRUE;
1370                }
1371              else
1372                {
1373                   cnp_debug("Item doesn't support images... passing\n");
1374                   pasteimage_free(savedtypes.pi);
1375                   return EINA_TRUE;
1376                }
1377           }
1378         else if (savedtypes.textreq)
1379           {
1380              /* Already asked: Pretend we asked now, and paste immediately when
1381               * it comes in */
1382              savedtypes.textreq = 0;
1383              ecore_x_dnd_send_finished();
1384              return EINA_TRUE;
1385           }
1386      }
1387
1388    cnp_debug("doing a request then\n");
1389    selections[ELM_SEL_XDND].requestwidget = dropable->obj;
1390    selections[ELM_SEL_XDND].requestformat = ELM_SEL_FORMAT_MARKUP;
1391    selections[ELM_SEL_XDND].active = EINA_TRUE;
1392
1393    ecore_x_selection_xdnd_request(xwin, atoms[i].name);
1394
1395    return EINA_TRUE;
1396 }
1397 static Eina_Bool
1398 _dnd_position(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1399 {
1400    struct _Ecore_X_Event_Xdnd_Position *pos;
1401    Ecore_X_Rectangle rect;
1402
1403    pos = ev;
1404
1405    /* Need to send a status back */
1406    /* FIXME: Should check I can drop here */
1407    /* FIXME: Should highlight widget */
1408    rect.x = pos->position.x - 5;
1409    rect.y = pos->position.y - 5;
1410    rect.width = 10;
1411    rect.height = 10;
1412    ecore_x_dnd_send_status(EINA_TRUE, EINA_FALSE, rect, pos->action);
1413
1414    return EINA_TRUE;
1415 }
1416
1417 /**
1418  * When dragging this is callback response from the destination.
1419  * The important thing we care about: Can we drop; thus update cursor
1420  * appropriately.
1421  */
1422 static Eina_Bool
1423 _dnd_status(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1424 {
1425    struct _Ecore_X_Event_Xdnd_Status *status = ev;
1426
1427    if (!status) return EINA_TRUE;
1428
1429    /* Only thing we care about: will accept */
1430    if (status->will_accept)
1431      {
1432         cnp_debug("Will accept\n");
1433      }
1434    else
1435      { /* Won't accept */
1436         cnp_debug("Won't accept accept\n");
1437      }
1438    return EINA_TRUE;
1439 }
1440
1441 /**
1442  * Add a widget as drop target.
1443  */
1444 Eina_Bool
1445 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
1446 {
1447    Dropable *drop;
1448    Ecore_X_Window xwin;
1449    Eina_List *item;
1450    int first;
1451
1452    if (!obj) return EINA_FALSE;
1453    if (!_elm_cnp_init_count) _elm_cnp_init();
1454
1455    /* Is this the first? */
1456    first = (!drops) ? 1 : 0;
1457
1458    EINA_LIST_FOREACH(drops, item, drop)
1459      {
1460         if (drop->obj == obj)
1461           {
1462              /* Update: Not a new one */
1463              drop->dropcb = dropcb;
1464              drop->cbdata = cbdata;
1465              drop->types = format;
1466              return EINA_TRUE;
1467           }
1468      }
1469
1470    /* Create new drop */
1471    drop = calloc(1, sizeof(Dropable));
1472    if (!drop) return EINA_FALSE;
1473    /* FIXME: Check for eina's deranged error method */
1474    drops = eina_list_append(drops, drop);
1475
1476    if (!drops/* || or other error */)
1477      {
1478         free(drop);
1479         return EINA_FALSE;
1480      }
1481    drop->dropcb = dropcb;
1482    drop->cbdata = cbdata;
1483    drop->types = format;
1484    drop->obj = obj;
1485
1486    evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL,
1487                                   /* I love C and varargs */
1488                                   (Evas_Object_Event_Cb)elm_drop_target_del,
1489                                   obj);
1490    /* FIXME: Handle resizes */
1491
1492    /* If not the first: We're done */
1493    if (!first) return EINA_TRUE;
1494
1495    xwin = (Ecore_X_Window)ecore_evas_window_get
1496       (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
1497
1498    ecore_x_dnd_aware_set(xwin, EINA_TRUE);
1499
1500    cnp_debug("Adding drop target calls\n");
1501    handler_enter = ecore_event_handler_add(ECORE_X_EVENT_XDND_ENTER,
1502                                            _dnd_enter, NULL);
1503    handler_pos = ecore_event_handler_add(ECORE_X_EVENT_XDND_POSITION,
1504                                          _dnd_position, NULL);
1505    handler_drop = ecore_event_handler_add(ECORE_X_EVENT_XDND_DROP,
1506                                           _dnd_drop, NULL);
1507
1508    return EINA_TRUE;
1509 }
1510
1511 Eina_Bool
1512 elm_drop_target_del(Evas_Object *obj)
1513 {
1514    Dropable *drop,*del;
1515    Eina_List *item;
1516    Ecore_X_Window xwin;
1517
1518    del = NULL;
1519    EINA_LIST_FOREACH(drops, item, drop)
1520      {
1521         if (drop->obj == obj)
1522           {
1523              drops = eina_list_remove_list(drops, item);
1524              del = drop;
1525              break;
1526           }
1527      }
1528    if (!del) return EINA_FALSE;
1529
1530    evas_object_event_callback_del(obj, EVAS_CALLBACK_FREE,
1531                                   (Evas_Object_Event_Cb)elm_drop_target_del);
1532    free(drop);
1533    /* If still drops there: All fine.. continue */
1534    if (drops) return EINA_TRUE;
1535
1536    cnp_debug("Disabling DND\n");
1537    xwin = (Ecore_X_Window)ecore_evas_window_get
1538       (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
1539    ecore_x_dnd_aware_set(xwin, EINA_FALSE);
1540
1541    ecore_event_handler_del(handler_pos);
1542    ecore_event_handler_del(handler_drop);
1543    ecore_event_handler_del(handler_enter);
1544
1545    if (savedtypes.pi)
1546      {
1547         pasteimage_free(savedtypes.pi);
1548         savedtypes.pi = NULL;
1549      }
1550
1551    return EINA_TRUE;
1552 }
1553
1554
1555 static void
1556 _drag_mouse_up(void *un __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *data __UNUSED__)
1557 {
1558    evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_UP, _drag_mouse_up);
1559    ecore_x_dnd_drop();
1560    if (dragdonecb)
1561      {
1562         dragdonecb(dragdonecb,selections[ELM_SEL_XDND].widget);
1563         dragdonecb = NULL;
1564      }
1565    if (dragwin)
1566      {
1567         evas_object_del(dragwin);
1568         dragwin = NULL;
1569      }
1570 }
1571
1572 static void
1573 _drag_move(void *data __UNUSED__, Ecore_X_Xdnd_Position *pos)
1574 {
1575    evas_object_move(dragwin,
1576                     pos->position.x - _dragx,
1577                     pos->position.y - _dragy);
1578 }
1579
1580
1581 Eina_Bool
1582 elm_drag_start(Evas_Object *obj, Elm_Sel_Format format, const char *data, void (*dragdone) (void *data, Evas_Object *), void *donecbdata)
1583 {
1584    Ecore_X_Window xwin;
1585    Cnp_Selection *sel;
1586    Elm_Sel_Type xdnd = ELM_SEL_XDND;
1587    Ecore_Evas *ee;
1588    int x, y, x2, y2, x3, y3;
1589    Evas_Object *icon;
1590    int w, h;
1591
1592    if (!_elm_cnp_init_count) _elm_cnp_init();
1593
1594    xwin = elm_win_xwindow_get(obj);
1595
1596    cnp_debug("starting drag...\n");
1597
1598    ecore_x_dnd_type_set(xwin, "text/uri-list", 1);
1599    sel = selections + ELM_SEL_XDND;
1600    sel->active = 1;
1601    sel->widget = obj;
1602    sel->format = format;
1603    sel->selbuf = data ? strdup(data) : NULL;
1604    dragdonecb = dragdone;
1605    dragdonedata = donecbdata;
1606
1607    ecore_x_dnd_callback_pos_update_set(_drag_move, NULL);
1608    ecore_x_dnd_begin(xwin, (unsigned char *)&xdnd, sizeof(Elm_Sel_Type));
1609    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
1610                                   _drag_mouse_up, NULL);
1611
1612    handler_status = ecore_event_handler_add(ECORE_X_EVENT_XDND_STATUS,
1613                                             _dnd_status, NULL);
1614
1615    dragwin = elm_win_add(NULL, "Elm Drag Object", ELM_WIN_UTILITY);
1616    elm_win_override_set(dragwin, 1);
1617
1618    /* FIXME: Images only */
1619    icon = elm_icon_add(dragwin);
1620    elm_icon_file_set(icon, data + 7, NULL); /* 7!? "file://" */
1621    elm_win_resize_object_add(dragwin,icon);
1622    evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1623    evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, EVAS_HINT_FILL);
1624
1625    /* Position subwindow appropriately */
1626    ee = ecore_evas_ecore_evas_get(evas_object_evas_get(obj));
1627    ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
1628    evas_object_geometry_get(obj, &x2, &y2, &w, &h);
1629    x += x2;
1630    y += y2;
1631    evas_object_move(dragwin, x, y);
1632    evas_object_resize(icon, w, h);
1633    evas_object_resize(dragwin, w, h);
1634
1635    evas_object_show(icon);
1636    evas_object_show(dragwin);
1637
1638    evas_pointer_canvas_xy_get(evas_object_evas_get(obj), &x3, &y3);
1639    _dragx = x3 - x2;
1640    _dragy = y3 - y2;
1641
1642    return EINA_TRUE;
1643 }
1644
1645 static Tmp_Info *
1646 elm_cnp_tempfile_create(int size)
1647 {
1648    Tmp_Info *info;
1649    const char *tmppath;
1650    int len;
1651
1652    info = malloc(sizeof(Tmp_Info));
1653    if (!info) return NULL;
1654
1655    tmppath = getenv("TMP");
1656    if (!tmppath) tmppath = P_tmpdir;
1657    if (!tmppath) tmppath = "/tmp";
1658    len = snprintf(NULL, 0, "%s/%sXXXXXX", tmppath, "elmcnpitem-");
1659    if (len < 0)
1660      {
1661         free(info);
1662         return NULL;
1663      }
1664    len++;
1665    info->filename = malloc(len);
1666    if (!info->filename)
1667      {
1668         free(info);
1669         return NULL;
1670      }
1671    snprintf(info->filename,len,"%s/%sXXXXXX", tmppath, "elmcnpitem-");
1672
1673    info->fd = mkstemp(info->filename);
1674
1675 # ifdef __linux__
1676      {
1677         char *tmp;
1678         /* And before someone says anything see POSIX 1003.1-2008 page 400 */
1679         long pid;
1680
1681         pid = (long)getpid();
1682         /* Use pid instead of /proc/self: That way if can be passed around */
1683         len = snprintf(NULL,0,"/proc/%li/fd/%i", pid, info->fd);
1684         len++;
1685         tmp = malloc(len);
1686         if (tmp)
1687           {
1688              snprintf(tmp,len, "/proc/%li/fd/%i", pid, info->fd);
1689              unlink(info->filename);
1690              free(info->filename);
1691              info->filename = tmp;
1692           }
1693      }
1694 # endif
1695
1696    cnp_debug("filename is %s\n", info->filename);
1697    if (size < 1)
1698      {
1699         /* Set map to NULL and return */
1700         info->map = NULL;
1701         info->len = 0;
1702         return info;
1703      }
1704
1705    /* Map it in */
1706    if (ftruncate(info->fd, size))
1707      {
1708         perror("ftruncate");
1709         info->map = NULL;
1710         info->len = 0;
1711         return info;
1712      }
1713
1714    info->map = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, info->fd, 0);
1715    if (info->map == MAP_FAILED)
1716      {
1717         perror("mmap");
1718         info->map = NULL;
1719         info->len = 0;
1720      }
1721
1722    return info;
1723 }
1724
1725
1726 static int
1727 tmpinfo_free(Tmp_Info *info)
1728 {
1729    if (!info) return 0;
1730    free(info->filename);
1731    free(info);
1732    return 0;
1733 }
1734
1735 #else
1736 /* Stubs for windows */
1737 Eina_Bool
1738 elm_drag_start(Evas_Object *o, Elm_Sel_Format f, const char *d, void (*donecb)(void *, Evas_Object *),void *cbdata)
1739 {
1740    return EINA_FALSE;
1741 }
1742
1743 Eina_Bool
1744 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
1745 {
1746    return EINA_FALSE;
1747 }
1748
1749 Eina_Bool
1750 elm_drop_target_del(Evas_Object *o)
1751 {
1752    return EINA_TRUE;
1753 }
1754 #endif
1755
1756 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-2f0^-2{2(0W1st0 :*/