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