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