elm elm_widget.c: Fixed build break. Thanks for the report Rajeev.
[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              sel->selbuf = malloc(buflen+1);
421              memcpy(sel->selbuf, selbuf, buflen);
422           }
423         else
424           sel->selbuf = strdup((char*)selbuf);
425      }
426    else
427      sel->selbuf = NULL;
428
429    return EINA_TRUE;
430 #else
431    return EINA_FALSE;
432 #endif
433 }
434
435 EAPI Eina_Bool
436 elm_object_cnp_selection_clear(Evas_Object *obj, Elm_Sel_Type selection)
437 {
438 #ifdef HAVE_ELEMENTARY_X
439    Cnp_Selection *sel;
440
441    if ((unsigned int)selection > (unsigned int)ELM_SEL_TYPE_CLIPBOARD)
442      return EINA_FALSE;
443    if (!_elm_cnp_init_count) _elm_cnp_init();
444
445    sel = selections + selection;
446
447    /* No longer this selection: Consider it gone! */
448    if ((!sel->active) || (sel->widget != obj)) return EINA_TRUE;
449
450    sel->active = EINA_FALSE;
451    sel->widget = NULL;
452    if (sel->selbuf)
453      {
454         free(sel->selbuf);
455         sel->selbuf = NULL;
456      }
457    sel->clear();
458
459    return EINA_TRUE;
460 #else
461    return EINA_FALSE;
462 #endif
463 }
464
465 EAPI Eina_Bool
466 elm_cnp_selection_get(Evas_Object *obj, Elm_Sel_Type selection,
467                       Elm_Sel_Format format, Elm_Drop_Cb datacb, void *udata)
468 {
469 #ifdef HAVE_ELEMENTARY_X
470    Evas_Object *top;
471    Cnp_Selection *sel;
472
473    if (selection > ELM_SEL_TYPE_CLIPBOARD)
474      return EINA_FALSE;
475    if (!_elm_cnp_init_count) _elm_cnp_init();
476
477    sel = selections + selection;
478    top = elm_widget_top_get(obj);
479    if (!top) return EINA_FALSE;
480
481    sel->requestformat = format;
482    sel->requestwidget = obj;
483    sel->request(elm_win_xwindow_get(top), ECORE_X_SELECTION_TARGET_TARGETS);
484    sel->datacb = datacb;
485    sel->udata = udata;
486
487    return EINA_TRUE;
488 #else
489    return EINA_FALSE;
490 #endif
491 }
492
493 #ifdef HAVE_ELEMENTARY_X
494
495 static Eina_Bool
496 _elm_cnp_init(void)
497 {
498    int i;
499
500    if (_elm_cnp_init_count++) return EINA_TRUE;
501    for (i = 0; i < CNP_N_ATOMS; i++)
502      {
503         atoms[i].atom = ecore_x_atom_get(atoms[i].name);
504         ecore_x_selection_converter_atom_add(atoms[i].atom,
505                                              atoms[i].converter);
506      }
507
508    ecore_event_handler_add(ECORE_X_EVENT_SELECTION_CLEAR, selection_clear, NULL);
509    ecore_event_handler_add(ECORE_X_EVENT_SELECTION_NOTIFY, selection_notify, NULL);
510
511    text_uri = eina_stringshare_add("text/uri-list");
512    return EINA_TRUE;
513 }
514
515 static Eina_Bool
516 selection_clear(void *udata __UNUSED__, int type __UNUSED__, void *event)
517 {
518    Ecore_X_Event_Selection_Clear *ev = event;
519    Cnp_Selection *sel;
520    int i;
521
522    for (i = 0; i <= ELM_SEL_TYPE_CLIPBOARD; i++)
523      {
524         if (selections[i].ecore_sel == ev->selection) break;
525      }
526    cnp_debug("selection %d clear\n", i);
527    /* Not me... Don't care */
528    if (i > ELM_SEL_TYPE_CLIPBOARD) return ECORE_CALLBACK_PASS_ON;
529
530    sel = selections + i;
531    sel->active = EINA_FALSE;
532    sel->widget = NULL;
533    if (sel->selbuf)
534      {
535         free(sel->selbuf);
536         sel->selbuf = NULL;
537      }
538
539    return ECORE_CALLBACK_PASS_ON;
540 }
541
542
543 /*
544  * Response to a selection notify:
545  *      - So we have asked for the selection list.
546  *      - If it's the targets list, parse it, and fire of what we want,
547  *      else it's the data we want.
548  */
549 static Eina_Bool
550 selection_notify(void *udata __UNUSED__, int type __UNUSED__, void *event)
551 {
552    Ecore_X_Event_Selection_Notify *ev = event;
553    Cnp_Selection *sel;
554    int i;
555
556    cnp_debug("selection notify callback: %d\n",ev->selection);
557    switch (ev->selection)
558      {
559       case ECORE_X_SELECTION_PRIMARY:
560          sel = selections + ELM_SEL_TYPE_PRIMARY;
561          break;
562       case ECORE_X_SELECTION_SECONDARY:
563          sel = selections + ELM_SEL_TYPE_SECONDARY;
564          break;
565       case ECORE_X_SELECTION_XDND:
566          sel = selections + ELM_SEL_TYPE_XDND;
567          break;
568       case ECORE_X_SELECTION_CLIPBOARD:
569          sel = selections + ELM_SEL_TYPE_CLIPBOARD;
570          break;
571       default:
572          return ECORE_CALLBACK_PASS_ON;
573      }
574    cnp_debug("Target is %s\n", ev->target);
575
576    for (i = 0; i < CNP_N_ATOMS; i++)
577      {
578         if (!strcmp(ev->target, atoms[i].name))
579           {
580              if (atoms[i].notify)
581                {
582                   cnp_debug("Found something: %s\n", atoms[i].name);
583                   atoms[i].notify(sel, ev);
584                }
585              else
586                {
587                   cnp_debug("Ignored: No handler!\n");
588                }
589           }
590      }
591
592    return ECORE_CALLBACK_PASS_ON;
593 }
594
595 static Elm_Sel_Format
596 _get_selection_type(void *data, int size)
597 {
598    if (size == sizeof(Elm_Sel_Type))
599      {
600         Cnp_Selection *sel = selections + *((int *)data);
601         if (sel->active &&
602             (sel->format >= ELM_SEL_FORMAT_TARGETS) &&
603             (sel->format <= ELM_SEL_FORMAT_HTML))
604           return sel->format;
605      }
606    return ELM_SEL_FORMAT_NONE;
607 }
608
609 static Eina_Bool
610 targets_converter(char *target __UNUSED__, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize)
611 {
612    int i,count;
613    Ecore_X_Atom *aret;
614    Cnp_Selection *sel;
615    Elm_Sel_Format seltype;
616
617    if (!data_ret) return EINA_FALSE;
618
619    if (_get_selection_type(data, size) == ELM_SEL_FORMAT_NONE)
620      {
621         /* TODO : fallback into precise type */
622         seltype = ELM_SEL_FORMAT_TEXT;
623      }
624    else
625      {
626         sel = selections + *((int *)data);
627         seltype = sel->format;
628      }
629
630    for (i = 0, count = 0; i < CNP_N_ATOMS ; i++)
631      {
632         if (seltype & atoms[i].formats) count++;
633      }
634
635    aret = malloc(sizeof(Ecore_X_Atom) * count);
636    for (i = 0, count = 0; i < CNP_N_ATOMS; i++)
637      {
638         if (seltype & atoms[i].formats) aret[count ++] = atoms[i].atom;
639      }
640
641    *data_ret = aret;
642    if (typesize) *typesize = 32 /* urk */;
643    if (ttype) *ttype = ECORE_X_ATOM_ATOM;
644    if (size_ret) *size_ret = count;
645
646    return EINA_TRUE;
647 }
648
649 static Eina_Bool
650 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__)
651 {
652    cnp_debug("Image converter called\n");
653    return EINA_TRUE;
654 }
655
656 static Eina_Bool
657 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__)
658 {
659    Cnp_Selection *sel;
660
661    cnp_debug("Vcard send called\n");
662
663    sel = selections + *((int *)data);
664
665    if (data_ret) *data_ret = strdup(sel->selbuf);
666    if (size_ret) *size_ret = strlen(sel->selbuf);
667
668    return EINA_TRUE;
669 }
670
671 static Eina_Bool
672 is_uri_type_data(Cnp_Selection *sel __UNUSED__, Ecore_X_Event_Selection_Notify *notify)
673 {
674    Ecore_X_Selection_Data *data;
675    char *p;
676
677    data = notify->data;
678    cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
679    if (data->content == ECORE_X_SELECTION_CONTENT_FILES) return EINA_TRUE;
680    p = (char *)data->data;
681    if (!p) return EINA_TRUE;
682
683    cnp_debug("Got %s\n", p);
684    if (strncmp(p, "file://", 7))
685      {
686         if (*p != '/') return EINA_FALSE;
687      }
688
689    return EINA_TRUE;
690 }
691
692 /*
693  * Callback to handle a targets response on a selection request:
694  * So pick the format we'd like; and then request it.
695  */
696 static int
697 notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
698 {
699    Ecore_X_Selection_Data_Targets *targets;
700    Ecore_X_Atom *atomlist;
701    Evas_Object *top;
702    int i, j;
703
704    targets = notify->data;
705    atomlist = (Ecore_X_Atom *)(targets->data.data);
706
707    for (j = (CNP_ATOM_LISTING_ATOMS+1); j < CNP_N_ATOMS; j++)
708      {
709         cnp_debug("\t%s %d\n", atoms[j].name, atoms[j].atom);
710         if (!(atoms[j].formats & sel->requestformat)) continue;
711         for (i = 0; i < targets->data.length; i++)
712           {
713              if ((atoms[j].atom == atomlist[i]) && (atoms[j].notify))
714                {
715                   if ((j == CNP_ATOM_text_uri) ||
716                       (j == CNP_ATOM_text_urilist))
717                     {
718                       if (!is_uri_type_data(sel, notify)) continue;
719                     }
720                   cnp_debug("Atom %s matches\n",atoms[j].name);
721                   goto done;
722                }
723           }
724      }
725
726    cnp_debug("Couldn't find anything that matches\n");
727    return ECORE_CALLBACK_PASS_ON;
728
729 done:
730    top = elm_widget_top_get(sel->requestwidget);
731    if (!top) top = sel->requestwidget;
732    cnp_debug("Sending request for %s\n", atoms[j].name);
733    sel->request(elm_win_xwindow_get(top), atoms[j].name);
734
735    return ECORE_CALLBACK_PASS_ON;
736 }
737
738 static int
739 response_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
740 {
741    Ecore_X_Selection_Data_Targets *targets;
742    Ecore_X_Atom *atomlist;
743    Evas_Object *top;
744    int i,j;
745
746    targets = notify->data;
747    atomlist = (Ecore_X_Atom *)(targets->data.data);
748
749    for (j = (CNP_ATOM_LISTING_ATOMS+1); j < CNP_N_ATOMS; j++)
750      {
751         if (!(atoms[j].formats & sel->requestformat)) continue;
752         for (i = 0 ; i < targets->data.length ; i ++)
753           {
754              if ((atoms[j].atom == atomlist[i]) && (atoms[j].response))
755                {
756                   /* Found a match: Use it */
757                   goto found;
758                }
759           }
760      }
761    cnp_debug("No matching type found\n");
762    return 0;
763
764 found:
765    top = elm_widget_top_get(sel->requestwidget);
766    if (!top) return 0;
767
768    sel->request(elm_win_xwindow_get(top), atoms[j].name);
769    return 0;
770 }
771
772
773 static int
774 notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
775 {
776    Ecore_X_Selection_Data *data;
777    char *str;
778    char *mkupstr;
779
780    data = notify->data;
781    str = malloc(sizeof(char) * (data->length + 1));
782    if (!str) return 0;
783    strncpy(str, (char *)data->data, data->length);
784    str[data->length] = '\0';
785
786    if (sel->datacb)
787      {
788         Elm_Selection_Data ddata;
789
790         ddata.x = ddata.y = 0;
791         ddata.format = ELM_SEL_FORMAT_TEXT;
792         ddata.data = data->data;
793         ddata.len = data->length;
794         sel->datacb(sel->udata, sel->widget, &ddata);
795         free(str);
796         return 0;
797      }
798
799    cnp_debug("Notify handler text %d %d %p\n", data->format,
800              data->length, data->data);
801    mkupstr = _elm_util_text_to_mkup((const char *)str);
802    cnp_debug("String is %s (from %s)\n", str, data->data);
803    _elm_entry_entry_paste(sel->requestwidget, mkupstr);
804    free(str);
805    free(mkupstr);
806    return 0;
807 }
808
809
810 /**
811  * So someone is pasting an image into my entry or widget...
812  */
813 static int
814 notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
815 {
816    Ecore_X_Selection_Data *data;
817    Ecore_X_Selection_Data_Files *files;
818    char *p, *stripstr;
819
820    data = notify->data;
821    cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
822    if (data->content == ECORE_X_SELECTION_CONTENT_FILES)
823      {
824         cnp_debug("got a files list\n");
825         files = notify->data;
826         if (files->num_files > 1)
827           {
828              /* Don't handle many items */
829              cnp_debug("more then one file: Bailing\n");
830              return 0;
831           }
832         stripstr = p = strdup(files->files[0]);
833      }
834    else
835      {
836         stripstr = p = malloc(data->length * sizeof(char) + 1);
837         memcpy(stripstr, data->data, data->length);
838         stripstr[data->length] = 0;
839      }
840
841    if (!p)
842      {
843         cnp_debug("Couldn't find a file\n");
844         return 0;
845      }
846    cnp_debug("Got %s\n",p);
847    if (strncmp(p, "file://", 7))
848      {
849         /* Try and continue if it looks sane */
850         if (*p != '/')
851           {
852              free(p);
853              return 0;
854           }
855      }
856    else
857      {
858         p += strlen("file://");
859      }
860
861    if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
862    if (savedtypes.textreq)
863      {
864         savedtypes.textreq = 0;
865         savedtypes.imgfile = strdup(p);
866      }
867    else
868      {
869         pasteimage_append(p, sel->requestwidget);
870      }
871    free(stripstr);
872    return 0;
873 }
874
875 /**
876  * Just receieved an vcard, either through cut and paste, or dnd.
877  */
878 static int
879 vcard_receive(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
880 {
881    Dropable *dropable;
882    Eina_List *l;
883    Ecore_X_Selection_Data *data;
884
885    data = notify->data;
886    cnp_debug("vcard receive\n");
887
888    if (sel == (selections + ELM_SEL_TYPE_XDND))
889      {
890         Elm_Selection_Data ddata;
891
892         cnp_debug("drag & drop\n");
893         /* FIXME: this needs to be generic: Used for all receives */
894         EINA_LIST_FOREACH(drops, l, dropable)
895           {
896              if (dropable->obj == sel->requestwidget) break;
897           }
898         if (!dropable)
899           {
900              cnp_debug("Unable to find drop object");
901              ecore_x_dnd_send_finished();
902              return 0;
903           }
904         dropable = eina_list_data_get(l);
905         ddata.x = savedtypes.x;
906         ddata.y = savedtypes.y;
907         ddata.format = ELM_SEL_FORMAT_VCARD;
908         ddata.data = data->data;
909         ddata.len = data->length;
910         dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
911         ecore_x_dnd_send_finished();
912      }
913    else if (sel->datacb)
914      {
915         Elm_Selection_Data ddata;
916         ddata.x = ddata.y = 0;
917         ddata.format = ELM_SEL_FORMAT_IMAGE;
918         ddata.data = data->data;
919         ddata.len = data->length;
920         sel->datacb(sel->udata, sel->widget, &ddata);
921      }
922    else
923      {
924         cnp_debug("Paste request\n");
925      }
926
927    return 0;
928
929 }
930
931
932 static int
933 notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
934 {
935    Ecore_X_Selection_Data *data;
936    Tmp_Info *tmp;
937
938    cnp_debug("got a image file!\n");
939    data = notify->data;
940
941    cnp_debug("Size if %d\n", data->length);
942
943    if (sel->datacb)
944      {
945         Elm_Selection_Data ddata;
946
947         ddata.x = ddata.y = 0;
948         ddata.format = ELM_SEL_FORMAT_IMAGE;
949         ddata.data = data->data;
950         ddata.len = data->length;
951         sel->datacb(sel->udata, sel->widget, &ddata);
952         return 0;
953      }
954
955    /* generate tmp name */
956    tmp = elm_cnp_tempfile_create(data->length);
957    memcpy(tmp->map, data->data, data->length);
958    munmap(tmp->map, data->length);
959
960    /* FIXME: Add to paste image data to clean up */
961    pasteimage_append(tmp->filename, sel->requestwidget);
962
963    tmpinfo_free(tmp);
964    return 0;
965 }
966
967
968 /**
969  *    Warning: Generic text/html can';t handle it sanely.
970  *    Firefox sends ucs2 (i think).
971  *       chrome sends utf8... blerg
972  */
973 static int
974 notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
975 {
976    Ecore_X_Selection_Data *data;
977
978    cnp_debug("Got some HTML: Checking encoding is useful\n");
979    data = notify->data;
980
981    if (sel->datacb)
982      {
983         Elm_Selection_Data ddata;
984         ddata.x = ddata.y = 0;
985         ddata.format = ELM_SEL_FORMAT_HTML;
986         ddata.data = data->data;
987         ddata.len = data->length;
988         sel->datacb(sel->udata, sel->widget, &ddata);
989         return 0;
990      }
991
992    char *stripstr = malloc(sizeof(char) * (data->length + 1));
993    strncpy(stripstr, (char *)data->data, data->length);
994    stripstr[data->length] = '\0';
995    cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
996    _elm_entry_entry_paste(sel->requestwidget, stripstr);
997    free(stripstr);
998    return 0;
999 }
1000
1001
1002 static Eina_Bool
1003 text_converter(char *target __UNUSED__, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1004 {
1005    Cnp_Selection *sel;
1006
1007    cnp_debug("text converter\n");
1008    if (_get_selection_type(data, size) == ELM_SEL_FORMAT_NONE)
1009      {
1010         if (data_ret)
1011           {
1012              *data_ret = malloc(size * sizeof(char) + 1);
1013              memcpy(*data_ret, data, size);
1014              ((char**)(data_ret))[0][size] = 0;
1015           }
1016         if (size_ret) *size_ret = size;
1017         return EINA_TRUE;
1018      }
1019    sel = selections + *((int *)data);
1020    if (!sel->active) return EINA_TRUE;
1021
1022    if ((sel->format & ELM_SEL_FORMAT_MARKUP) ||
1023        (sel->format & ELM_SEL_FORMAT_HTML))
1024      {
1025         *data_ret = _elm_util_mkup_to_text(sel->selbuf);
1026         if (size_ret) *size_ret = strlen(*data_ret);
1027      }
1028    else if (sel->format & ELM_SEL_FORMAT_TEXT)
1029      {
1030         *data_ret = strdup(sel->selbuf);
1031         *size_ret = strlen(sel->selbuf);
1032      }
1033    else if (sel->format & ELM_SEL_FORMAT_IMAGE)
1034      {
1035         cnp_debug("Image %s\n", evas_object_type_get(sel->widget));
1036         cnp_debug("Elm type: %s\n", elm_object_widget_type_get(sel->widget));
1037         evas_object_image_file_get(elm_photocam_internal_image_get(sel->widget),
1038                                    (const char **)data_ret, NULL);
1039         if (!*data_ret) *data_ret = strdup("No file");
1040         else *data_ret = strdup(*data_ret);
1041         *size_ret = strlen(*data_ret);
1042      }
1043    return EINA_TRUE;
1044 }
1045
1046 static Eina_Bool
1047 general_converter(char *target __UNUSED__, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1048 {
1049    if (_get_selection_type(data, size) == ELM_SEL_FORMAT_NONE)
1050      {
1051         if (data_ret)
1052           {
1053              *data_ret = malloc(size * sizeof(char) + 1);
1054              memcpy(*data_ret, data, size);
1055              ((char**)(data_ret))[0][size] = 0;
1056           }
1057         if (size_ret) *size_ret = size;
1058      }
1059    else
1060      {
1061         Cnp_Selection *sel = selections + *((int *)data);
1062         if (data_ret) *data_ret = strdup(sel->selbuf);
1063         if (size_ret) *size_ret = strlen(sel->selbuf);
1064      }
1065    return EINA_TRUE;
1066 }
1067
1068 static Eina_Bool
1069 pasteimage_append(char *file, Evas_Object *entry)
1070 {
1071    char *entrytag;
1072    int len;
1073    static const char *tagstring = "<item absize=240x180 href=file://%s></item>";
1074
1075    if ((!file) || (!entry)) return EINA_FALSE;
1076
1077    len = strlen(tagstring)+strlen(file);
1078
1079    entrytag = alloca(len + 1);
1080    snprintf(entrytag, len + 1, tagstring, file);
1081    _elm_entry_entry_paste(entry, entrytag);
1082
1083    return EINA_TRUE;
1084 }
1085
1086 static Eina_Bool
1087 _dnd_enter(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1088 {
1089    Ecore_X_Event_Xdnd_Enter *enter = ev;
1090    int i;
1091
1092    /* Skip it */
1093    if ((!enter) || (!enter->num_types) || (!enter->types)) return EINA_TRUE;
1094
1095    cnp_debug("Types\n");
1096    savedtypes.ntypes = enter->num_types;
1097    if (savedtypes.types) free(savedtypes.types);
1098    savedtypes.types = malloc(sizeof(char *) * enter->num_types);
1099    if (!savedtypes.types) return EINA_FALSE;
1100
1101    for (i = 0; i < enter->num_types; i++)
1102      {
1103         savedtypes.types[i] = eina_stringshare_add(enter->types[i]);
1104         cnp_debug("Type is %s %p %p\n", enter->types[i],
1105                   savedtypes.types[i],text_uri);
1106         if (savedtypes.types[i] == text_uri)
1107           {
1108              /* Request it, so we know what it is */
1109              cnp_debug("Sending uri request\n");
1110              savedtypes.textreq = 1;
1111              if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1112              savedtypes.imgfile = NULL;
1113              ecore_x_selection_xdnd_request(enter->win, text_uri);
1114           }
1115      }
1116
1117    /* FIXME: Find an object and make it current */
1118    return EINA_TRUE;
1119 }
1120
1121 static Eina_Bool
1122 _dnd_drop(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1123 {
1124    struct _Ecore_X_Event_Xdnd_Drop *drop;
1125    Dropable *dropable;
1126    Eina_List *l;
1127    Ecore_Evas *ee;
1128    Ecore_X_Window xwin;
1129    Elm_Selection_Data ddata;
1130    int x, y, w, h;
1131    int i, j;
1132
1133    drop = ev;
1134
1135    // check we still have something to drop
1136    if (!drops) return EINA_TRUE;
1137
1138    /* Find any widget in our window; then work out geometry rel to our window */
1139    for (l = drops; l; l = l->next)
1140      {
1141         dropable = l->data;
1142         xwin = (Ecore_X_Window)ecore_evas_window_get
1143            (ecore_evas_ecore_evas_get(evas_object_evas_get
1144                                       (dropable->obj)));
1145         if (xwin == drop->win) break;
1146      }
1147    /* didn't find a window */
1148    if (!l) return EINA_TRUE;
1149
1150    /* Calculate real (widget relative) position */
1151    // - window position
1152    // - widget position
1153    ee = ecore_evas_ecore_evas_get(evas_object_evas_get(dropable->obj));
1154    ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
1155    savedtypes.x = drop->position.x - x;
1156    savedtypes.y = drop->position.y - y;
1157
1158    cnp_debug("Drop position is %d,%d\n", savedtypes.x, savedtypes.y);
1159
1160    for (; l; l = l->next)
1161      {
1162         dropable = l->data;
1163         evas_object_geometry_get(dropable->obj, &x, &y, &w, &h);
1164         if ((savedtypes.x >= x) && (savedtypes.y >= y) &&
1165             (savedtypes.x < x + w) && (savedtypes.y < y + h))
1166           break; /* found! */
1167      }
1168
1169    if (!l) return EINA_TRUE; /* didn't find one */
1170
1171    evas_object_geometry_get(dropable->obj, &x, &y, NULL, NULL);
1172    savedtypes.x -= x;
1173    savedtypes.y -= y;
1174
1175    /* Find our type from the previous list */
1176    for (i = 0; i < CNP_N_ATOMS; i++)
1177      {
1178         for (j = 0; j < savedtypes.ntypes; j++)
1179           {
1180              if (!strcmp(savedtypes.types[j], atoms[i].name)) goto found;
1181           }
1182      }
1183
1184    cnp_debug("Didn't find a target\n");
1185    return EINA_TRUE;
1186
1187 found:
1188    cnp_debug("Found a target we'd like: %s\n", atoms[i].name);
1189    cnp_debug("0x%x\n",xwin);
1190
1191    if (i == CNP_ATOM_text_urilist)
1192      {
1193         cnp_debug("We found a URI... (%scached) %s\n",
1194                   savedtypes.imgfile ? "" : "not ",
1195                   savedtypes.imgfile);
1196         if (savedtypes.imgfile)
1197           {
1198              char *entrytag;
1199              static const char *tagstring = "<item absize=240x180 href="
1200                 "file://%s></item>";
1201              ddata.x = savedtypes.x;
1202              ddata.y = savedtypes.y;
1203
1204              /* If it's markup that also supports images */
1205              if ((dropable->types & ELM_SEL_FORMAT_MARKUP) &&
1206                  (dropable->types & ELM_SEL_FORMAT_IMAGE))
1207                {
1208                   int len;
1209                   ddata.format = ELM_SEL_FORMAT_MARKUP;
1210
1211                   len = strlen(tagstring) + strlen(savedtypes.imgfile);
1212                   entrytag = alloca(len + 1);
1213                   snprintf(entrytag, len + 1, tagstring, savedtypes.imgfile);
1214                   ddata.data = entrytag;
1215                   cnp_debug("Insert %s\n", (char *)ddata.data);
1216                   dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1217                   ecore_x_dnd_send_finished();
1218                   if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1219                   savedtypes.imgfile = NULL;
1220
1221                   return EINA_TRUE;
1222                }
1223              else if (dropable->types & ELM_SEL_FORMAT_IMAGE)
1224                {
1225                   cnp_debug("Doing image insert (%s)\n", savedtypes.imgfile);
1226                   ddata.format = ELM_SEL_FORMAT_IMAGE;
1227                   ddata.data = (char *)savedtypes.imgfile;
1228                   dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1229                   ecore_x_dnd_send_finished();
1230                   if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1231                   savedtypes.imgfile = NULL;
1232
1233                   return EINA_TRUE;
1234                }
1235              else
1236                {
1237                   cnp_debug("Item doesn't support images... passing\n");
1238                   return EINA_TRUE;
1239                }
1240           }
1241         else if (savedtypes.textreq)
1242           {
1243              /* Already asked: Pretend we asked now, and paste immediately when
1244               * it comes in */
1245              savedtypes.textreq = 0;
1246              ecore_x_dnd_send_finished();
1247              return EINA_TRUE;
1248           }
1249      }
1250
1251    cnp_debug("doing a request then\n");
1252    selections[ELM_SEL_TYPE_XDND].requestwidget = dropable->obj;
1253    selections[ELM_SEL_TYPE_XDND].requestformat = ELM_SEL_FORMAT_MARKUP;
1254    selections[ELM_SEL_TYPE_XDND].active = EINA_TRUE;
1255
1256    ecore_x_selection_xdnd_request(xwin, atoms[i].name);
1257
1258    return EINA_TRUE;
1259 }
1260 static Eina_Bool
1261 _dnd_position(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1262 {
1263    struct _Ecore_X_Event_Xdnd_Position *pos;
1264    Ecore_X_Rectangle rect;
1265
1266    pos = ev;
1267
1268    /* Need to send a status back */
1269    /* FIXME: Should check I can drop here */
1270    /* FIXME: Should highlight widget */
1271    rect.x = pos->position.x - 5;
1272    rect.y = pos->position.y - 5;
1273    rect.width = 10;
1274    rect.height = 10;
1275    ecore_x_dnd_send_status(EINA_TRUE, EINA_FALSE, rect, pos->action);
1276
1277    return EINA_TRUE;
1278 }
1279
1280 /**
1281  * When dragging this is callback response from the destination.
1282  * The important thing we care about: Can we drop; thus update cursor
1283  * appropriately.
1284  */
1285 static Eina_Bool
1286 _dnd_status(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1287 {
1288    struct _Ecore_X_Event_Xdnd_Status *status = ev;
1289
1290    if (!status) return EINA_TRUE;
1291
1292    /* Only thing we care about: will accept */
1293    if (status->will_accept)
1294      {
1295         cnp_debug("Will accept\n");
1296      }
1297    else
1298      { /* Won't accept */
1299         cnp_debug("Won't accept accept\n");
1300      }
1301    return EINA_TRUE;
1302 }
1303
1304 /**
1305  * Add a widget as drop target.
1306  */
1307 Eina_Bool
1308 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
1309 {
1310    Dropable *drop;
1311    Ecore_X_Window xwin;
1312    Eina_List *item;
1313    int first;
1314    Evas_Object *top;
1315
1316    if (!obj) return EINA_FALSE;
1317    top = elm_widget_top_get(obj);
1318    if (!top || !elm_win_xwindow_get(top)) return EINA_FALSE;
1319    if (!_elm_cnp_init_count) _elm_cnp_init();
1320
1321    /* Is this the first? */
1322    first = (!drops) ? 1 : 0;
1323
1324    EINA_LIST_FOREACH(drops, item, drop)
1325      {
1326         if (drop->obj == obj)
1327           {
1328              /* Update: Not a new one */
1329              drop->dropcb = dropcb;
1330              drop->cbdata = cbdata;
1331              drop->types = format;
1332              return EINA_TRUE;
1333           }
1334      }
1335
1336    /* Create new drop */
1337    drop = calloc(1, sizeof(Dropable));
1338    if (!drop) return EINA_FALSE;
1339    /* FIXME: Check for eina's deranged error method */
1340    drops = eina_list_append(drops, drop);
1341
1342    if (!drops/* || or other error */)
1343      {
1344         free(drop);
1345         return EINA_FALSE;
1346      }
1347    drop->dropcb = dropcb;
1348    drop->cbdata = cbdata;
1349    drop->types = format;
1350    drop->obj = obj;
1351
1352    evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL,
1353                                   /* I love C and varargs */
1354                                   (Evas_Object_Event_Cb)elm_drop_target_del,
1355                                   obj);
1356    /* FIXME: Handle resizes */
1357
1358    /* If not the first: We're done */
1359    if (!first) return EINA_TRUE;
1360
1361    xwin = (Ecore_X_Window)ecore_evas_window_get
1362       (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
1363
1364    ecore_x_dnd_aware_set(xwin, EINA_TRUE);
1365
1366    cnp_debug("Adding drop target calls\n");
1367    handler_enter = ecore_event_handler_add(ECORE_X_EVENT_XDND_ENTER,
1368                                            _dnd_enter, NULL);
1369    handler_pos = ecore_event_handler_add(ECORE_X_EVENT_XDND_POSITION,
1370                                          _dnd_position, NULL);
1371    handler_drop = ecore_event_handler_add(ECORE_X_EVENT_XDND_DROP,
1372                                           _dnd_drop, NULL);
1373
1374    return EINA_TRUE;
1375 }
1376
1377 Eina_Bool
1378 elm_drop_target_del(Evas_Object *obj)
1379 {
1380    Dropable *drop,*del;
1381    Eina_List *item;
1382    Ecore_X_Window xwin;
1383
1384    del = NULL;
1385    EINA_LIST_FOREACH(drops, item, drop)
1386      {
1387         if (drop->obj == obj)
1388           {
1389              drops = eina_list_remove_list(drops, item);
1390              del = drop;
1391              break;
1392           }
1393      }
1394    if (!del) return EINA_FALSE;
1395
1396    evas_object_event_callback_del(obj, EVAS_CALLBACK_FREE,
1397                                   (Evas_Object_Event_Cb)elm_drop_target_del);
1398    free(drop);
1399    /* If still drops there: All fine.. continue */
1400    if (drops) return EINA_TRUE;
1401
1402    cnp_debug("Disabling DND\n");
1403    xwin = (Ecore_X_Window)ecore_evas_window_get
1404       (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
1405    ecore_x_dnd_aware_set(xwin, EINA_FALSE);
1406
1407    ecore_event_handler_del(handler_pos);
1408    ecore_event_handler_del(handler_drop);
1409    ecore_event_handler_del(handler_enter);
1410
1411    if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1412    savedtypes.imgfile = NULL;
1413
1414    return EINA_TRUE;
1415 }
1416
1417
1418 static void
1419 _drag_mouse_up(void *un __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *data)
1420 {
1421    Ecore_X_Window xwin = *((Ecore_X_Window *)data);
1422    evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_UP, _drag_mouse_up);
1423    ecore_x_dnd_drop();
1424    ecore_x_dnd_aware_set(xwin, EINA_FALSE);
1425    if (dragdonecb)
1426      {
1427         dragdonecb(dragdonedata, selections[ELM_SEL_TYPE_XDND].widget);
1428         dragdonecb = NULL;
1429      }
1430    if (dragwin)
1431      {
1432         evas_object_del(dragwin);
1433         dragwin = NULL;
1434      }
1435 }
1436
1437 static void
1438 _drag_move(void *data __UNUSED__, Ecore_X_Xdnd_Position *pos)
1439 {
1440    evas_object_move(dragwin,
1441                     pos->position.x - _dragx,
1442                     pos->position.y - _dragy);
1443 }
1444
1445
1446 Eina_Bool
1447 elm_drag_start(Evas_Object *obj, Elm_Sel_Format format, const char *data, void (*dragdone) (void *data, Evas_Object *), void *donecbdata)
1448 {
1449    Ecore_X_Window xwin;
1450    Cnp_Selection *sel;
1451    Elm_Sel_Type xdnd = ELM_SEL_TYPE_XDND;
1452    Ecore_Evas *ee;
1453    int x, y, x2, y2, x3, y3;
1454    Evas_Object *icon;
1455    int w, h;
1456
1457    if (!_elm_cnp_init_count) _elm_cnp_init();
1458
1459    xwin = elm_win_xwindow_get(obj);
1460
1461    cnp_debug("starting drag...\n");
1462
1463    if (dragwin)
1464      {
1465         cnp_debug("another obj is dragging...\n");
1466         return EINA_FALSE;
1467      }
1468
1469    ecore_x_dnd_type_set(xwin, "text/uri-list", EINA_TRUE);
1470    sel = selections + ELM_SEL_TYPE_XDND;
1471    sel->active = EINA_TRUE;
1472    sel->widget = obj;
1473    sel->format = format;
1474    sel->selbuf = data ? strdup(data) : NULL;
1475    dragdonecb = dragdone;
1476    dragdonedata = donecbdata;
1477
1478    ecore_x_dnd_aware_set(xwin, EINA_TRUE);
1479    ecore_x_dnd_callback_pos_update_set(_drag_move, NULL);
1480    ecore_x_dnd_begin(xwin, (unsigned char *)&xdnd, sizeof(Elm_Sel_Type));
1481    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
1482                                   _drag_mouse_up, (void *)(long)xwin);
1483
1484    handler_status = ecore_event_handler_add(ECORE_X_EVENT_XDND_STATUS,
1485                                             _dnd_status, NULL);
1486
1487    dragwin = elm_win_add(NULL, "Elm Drag Object", ELM_WIN_UTILITY);
1488    elm_win_override_set(dragwin, EINA_TRUE);
1489
1490    /* FIXME: Images only */
1491    icon = elm_icon_add(dragwin);
1492    elm_icon_file_set(icon, data + 7, NULL); /* 7!? "file://" */
1493    elm_win_resize_object_add(dragwin, icon);
1494    evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1495    evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, EVAS_HINT_FILL);
1496
1497    /* Position subwindow appropriately */
1498    ee = ecore_evas_ecore_evas_get(evas_object_evas_get(obj));
1499    ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
1500    evas_object_geometry_get(obj, &x2, &y2, &w, &h);
1501    x += x2;
1502    y += y2;
1503    evas_object_move(dragwin, x, y);
1504    evas_object_resize(icon, w, h);
1505    evas_object_resize(dragwin, w, h);
1506
1507    evas_object_show(icon);
1508    evas_object_show(dragwin);
1509
1510    evas_pointer_canvas_xy_get(evas_object_evas_get(obj), &x3, &y3);
1511    _dragx = x3 - x2;
1512    _dragy = y3 - y2;
1513
1514    return EINA_TRUE;
1515 }
1516
1517 static Tmp_Info *
1518 elm_cnp_tempfile_create(int size)
1519 {
1520 #ifdef HAVE_MMAN_H
1521    Tmp_Info *info;
1522    const char *tmppath;
1523    int len;
1524
1525    info = malloc(sizeof(Tmp_Info));
1526    if (!info) return NULL;
1527
1528    tmppath = getenv("TMP");
1529    if (!tmppath) tmppath = P_tmpdir;
1530    len = snprintf(NULL, 0, "%s/%sXXXXXX", tmppath, "elmcnpitem-");
1531    if (len < 0)
1532      {
1533         free(info);
1534         return NULL;
1535      }
1536    len++;
1537    info->filename = malloc(len);
1538    if (!info->filename)
1539      {
1540         free(info);
1541         return NULL;
1542      }
1543    snprintf(info->filename,len,"%s/%sXXXXXX", tmppath, "elmcnpitem-");
1544
1545    info->fd = mkstemp(info->filename);
1546
1547 # ifdef __linux__
1548      {
1549         char *tmp;
1550         /* And before someone says anything see POSIX 1003.1-2008 page 400 */
1551         long pid;
1552
1553         pid = (long)getpid();
1554         /* Use pid instead of /proc/self: That way if can be passed around */
1555         len = snprintf(NULL,0,"/proc/%li/fd/%i", pid, info->fd);
1556         len++;
1557         tmp = malloc(len);
1558         if (tmp)
1559           {
1560              snprintf(tmp,len, "/proc/%li/fd/%i", pid, info->fd);
1561              unlink(info->filename);
1562              free(info->filename);
1563              info->filename = tmp;
1564           }
1565      }
1566 # endif
1567
1568    cnp_debug("filename is %s\n", info->filename);
1569    if (size < 1)
1570      {
1571         /* Set map to NULL and return */
1572         info->map = NULL;
1573         info->len = 0;
1574         return info;
1575      }
1576
1577    /* Map it in */
1578    if (ftruncate(info->fd, size))
1579      {
1580         perror("ftruncate");
1581         info->map = NULL;
1582         info->len = 0;
1583         return info;
1584      }
1585
1586    eina_mmap_safety_enabled_set(EINA_TRUE);
1587
1588    info->map = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, info->fd, 0);
1589    if (info->map == MAP_FAILED)
1590      {
1591         perror("mmap");
1592         info->map = NULL;
1593         info->len = 0;
1594      }
1595
1596    return info;
1597 #else
1598    (void) size;
1599    return NULL;
1600 #endif
1601 }
1602
1603
1604 static int
1605 tmpinfo_free(Tmp_Info *info)
1606 {
1607    if (!info) return 0;
1608    free(info->filename);
1609    free(info);
1610    return 0;
1611 }
1612
1613 #else
1614 /* Stubs for windows */
1615 Eina_Bool
1616 elm_drag_start(Evas_Object *o, Elm_Sel_Format f, const char *d, void (*donecb)(void *, Evas_Object *),void *cbdata)
1617 {
1618    return EINA_FALSE;
1619 }
1620
1621 Eina_Bool
1622 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
1623 {
1624    return EINA_FALSE;
1625 }
1626
1627 Eina_Bool
1628 elm_drop_target_del(Evas_Object *o)
1629 {
1630    return EINA_TRUE;
1631 }
1632 #endif
1633
1634 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-3f0^-2{2(0W1st0 :*/