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