2 # include "elementary_config.h"
4 #include <Elementary.h>
9 #ifdef HAVE_ELEMENTARY_X
11 #define ARRAYINIT(foo) [foo] =
17 # define cnp_debug(x...) fprintf(stderr, __FILE__": " x)
19 # define cnp_debug(x...)
22 #define PROVIDER_SET "__elm_cnp_provider_set"
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;
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 *);
40 CNP_ATOM_text_urilist,
41 CNP_ATOM_text_x_vcard,
52 CNP_ATOM_text_html_utf8,
57 CNP_ATOM_text_plain_utf8,
76 Evas_Object *requestwidget;
78 Elm_Sel_Format requestformat;
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);
84 Elm_Sel_Format format;
85 Ecore_X_Selection ecore_sel;
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;
122 Eina_Bool textreq: 1;
128 /* FIXME: Cache window */
129 Elm_Sel_Format types;
134 static Tmp_Info *elm_cnp_tempfile_create(int size);
135 static int tmpinfo_free(Tmp_Info *tmp);
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);
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);
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);
155 static int response_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *);
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);
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);
168 /* Optimisation: Turn this into a 256 byte table:
169 * then can lookup in one index, not N checks */
170 static const Escape escapes[] = {
179 #define N_ESCAPES ((int)(sizeof(escapes) / sizeof(escapes[0])))
181 static const char *image_extensions[] =
184 ".jpg", ".jpeg", ".jpe", ".jfif", ".jfi",
187 ".ppm", "pgm", ".pbm", ".pnm",
196 static Cnp_Atom atoms[CNP_N_ATOMS] = {
197 [CNP_ATOM_TARGETS] = {
199 (Elm_Sel_Format) -1, // everything
201 response_handler_targets,
202 notify_handler_targets,
206 "application/x-elementary-markup",
207 ELM_SEL_FORMAT_MARKUP,
213 [CNP_ATOM_text_uri] = {
215 ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_IMAGE, /* Either images or entries */
221 [CNP_ATOM_text_urilist] = {
223 ELM_SEL_FORMAT_IMAGE,
229 [CNP_ATOM_text_x_vcard] = {
231 ELM_SEL_FORMAT_VCARD,
235 [CNP_ATOM_image_png] = {
237 ELM_SEL_FORMAT_IMAGE,
240 notify_handler_image,
243 [CNP_ATOM_image_jpeg] = {
245 ELM_SEL_FORMAT_IMAGE,
248 notify_handler_image,/* Raw image data is the same */
251 [CNP_ATOM_image_bmp] = {
253 ELM_SEL_FORMAT_IMAGE,
256 notify_handler_image,/* Raw image data is the same */
259 [CNP_ATOM_image_gif] = {
261 ELM_SEL_FORMAT_IMAGE,
264 notify_handler_image,/* Raw image data is the same */
267 [CNP_ATOM_image_tiff] = {
269 ELM_SEL_FORMAT_IMAGE,
272 notify_handler_image,/* Raw image data is the same */
275 [CNP_ATOM_image_svg] = {
277 ELM_SEL_FORMAT_IMAGE,
280 notify_handler_image,/* Raw image data is the same */
283 [CNP_ATOM_image_xpm] = {
285 ELM_SEL_FORMAT_IMAGE,
288 notify_handler_image,/* Raw image data is the same */
291 [CNP_ATOM_image_tga] = {
293 ELM_SEL_FORMAT_IMAGE,
296 notify_handler_image,/* Raw image data is the same */
299 [CNP_ATOM_image_ppm] = {
300 "image/x-portable-pixmap",
301 ELM_SEL_FORMAT_IMAGE,
304 notify_handler_image,/* Raw image data is the same */
307 [CNP_ATOM_text_html_utf8] = {
308 "text/html;charset=utf-8",
315 [CNP_ATOM_text_html] = {
320 notify_handler_html, /* No encoding: Webkit only */
323 [CNP_ATOM_UTF8STRING] = {
325 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
331 [CNP_ATOM_STRING] = {
333 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
341 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
347 [CNP_ATOM_text_plain_utf8] = {
348 "text/plain;charset=utf-8",
349 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
355 [CNP_ATOM_text_plain] = {
357 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
365 static Cnp_Selection selections[ELM_SEL_MAX] = {
366 ARRAYINIT(ELM_SEL_PRIMARY) {
368 .ecore_sel = ECORE_X_SELECTION_PRIMARY,
369 .set = ecore_x_selection_primary_set,
370 .clear = ecore_x_selection_primary_clear,
371 .request = ecore_x_selection_primary_request,
373 ARRAYINIT(ELM_SEL_SECONDARY) {
374 .debug = "Secondary",
375 .ecore_sel = ECORE_X_SELECTION_SECONDARY,
376 .set = ecore_x_selection_secondary_set,
377 .clear = ecore_x_selection_secondary_clear,
378 .request = ecore_x_selection_secondary_request,
380 ARRAYINIT(ELM_SEL_CLIPBOARD) {
381 .debug = "Clipboard",
382 .ecore_sel = ECORE_X_SELECTION_CLIPBOARD,
383 .set = ecore_x_selection_clipboard_set,
384 .clear = ecore_x_selection_clipboard_clear,
385 .request = ecore_x_selection_clipboard_request,
387 ARRAYINIT(ELM_SEL_XDND) {
389 .ecore_sel = ECORE_X_SELECTION_XDND,
390 .request = ecore_x_selection_xdnd_request,
394 /* Data for DND in progress */
395 static Saved_Type savedtypes = { NULL, NULL, 0, 0, 0, EINA_FALSE };
397 static void (*dragdonecb) (void *data, Evas_Object *obj) = NULL;
398 static void *dragdonedata = NULL;
400 static int _elm_cnp_init_count = 0;
401 /* FIXME: who left this out of XAtoms.h */
402 static Ecore_X_Atom clipboard_atom;
404 static Eina_List *pastedimages = NULL;
407 * Drag & Drop functions
410 /* FIXME: Way too many globals */
411 static Eina_List *drops = NULL;
412 static Evas_Object *dragwin = NULL;
413 static int _dragx = 0, _dragy = 0;
414 static Ecore_Event_Handler *handler_pos = NULL;
415 static Ecore_Event_Handler *handler_drop = NULL;
416 static Ecore_Event_Handler *handler_enter = NULL;
417 static Ecore_Event_Handler *handler_status = NULL;
421 /* Stringshared, so I can just compare pointers later */
422 static const char *text_uri;
425 elm_selection_set(Elm_Sel_Type selection, Evas_Object *widget, Elm_Sel_Format format, const char *selbuf)
427 #ifdef HAVE_ELEMENTARY_X
430 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
431 if (!_elm_cnp_init_count) _elm_cnp_init();
432 if ((!selbuf) && (format != ELM_SEL_FORMAT_IMAGE))
433 return elm_selection_clear(selection, widget);
435 sel = selections + selection;
438 sel->widget = widget;
440 sel->set(elm_win_xwindow_get(widget),&selection,sizeof(Elm_Sel_Type));
441 sel->format = format;
442 sel->selbuf = selbuf ? strdup(selbuf) : NULL;
451 elm_selection_clear(Elm_Sel_Type selection, Evas_Object *widget)
453 #ifdef HAVE_ELEMENTARY_X
456 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
457 if (!_elm_cnp_init_count) _elm_cnp_init();
459 sel = selections + selection;
461 /* No longer this selection: Consider it gone! */
462 if ((!sel->active) || (sel->widget != widget)) return EINA_TRUE;
475 elm_selection_get(Elm_Sel_Type selection, Elm_Sel_Format format,
476 Evas_Object *widget, Elm_Drop_Cb datacb, void *udata)
478 #ifdef HAVE_ELEMENTARY_X
482 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
483 if (!_elm_cnp_init_count) _elm_cnp_init();
485 sel = selections + selection;
486 top = elm_widget_top_get(widget);
487 if (!top) return EINA_FALSE;
489 sel->requestformat = format;
490 sel->requestwidget = widget;
491 sel->request(elm_win_xwindow_get(top), ECORE_X_SELECTION_TARGET_TARGETS);
492 sel->datacb = datacb;
501 #ifdef HAVE_ELEMENTARY_X
508 if (_elm_cnp_init_count++) return EINA_TRUE;
509 for (i = 0; i < CNP_N_ATOMS; i++)
511 atoms[i].atom = ecore_x_atom_get(atoms[i].name);
512 ecore_x_selection_converter_atom_add(atoms[i].atom,
515 clipboard_atom = ecore_x_atom_get("CLIPBOARD");
517 ecore_event_handler_add(ECORE_X_EVENT_SELECTION_CLEAR, selection_clear, NULL);
518 ecore_event_handler_add(ECORE_X_EVENT_SELECTION_NOTIFY, selection_notify, NULL);
520 text_uri = eina_stringshare_add("text/uri-list");
525 selection_clear(void *udata __UNUSED__, int type __UNUSED__, void *event)
527 Ecore_X_Event_Selection_Clear *ev = event;
531 for (i = 0; i < ELM_SEL_MAX; i++)
533 if (selections[i].ecore_sel == ev->selection) break;
535 cnp_debug("selection %d clear\n", i);
536 /* Not me... Don't care */
537 if (i == ELM_SEL_MAX) return ECORE_CALLBACK_PASS_ON;
539 sel = selections + i;
544 return ECORE_CALLBACK_PASS_ON;
549 * Response to a selection notify:
550 * - So we have asked for the selection list.
551 * - If it's the targets list, parse it, and fire of what we want,
552 * else it's the data we want.
555 selection_notify(void *udata __UNUSED__, int type __UNUSED__, void *event)
557 Ecore_X_Event_Selection_Notify *ev = event;
561 cnp_debug("selection notify callback: %d\n",ev->selection);
562 switch (ev->selection)
564 case ECORE_X_SELECTION_CLIPBOARD:
565 sel = selections + ELM_SEL_CLIPBOARD;
567 case ECORE_X_SELECTION_PRIMARY:
568 sel = selections + ELM_SEL_PRIMARY;
570 case ECORE_X_SELECTION_SECONDARY:
571 sel = selections + ELM_SEL_SECONDARY;
573 case ECORE_X_SELECTION_XDND:
574 sel = selections + ELM_SEL_XDND;
577 return ECORE_CALLBACK_PASS_ON;
579 cnp_debug("Target is %s\n", ev->target);
581 for (i = 0; i < CNP_N_ATOMS; i++)
583 if (!strcmp(ev->target, atoms[i].name))
587 cnp_debug("Found something: %s\n", atoms[i].name);
588 atoms[i].notify(sel, ev);
592 cnp_debug("Ignored: No handler!\n");
597 return ECORE_CALLBACK_PASS_ON;
603 targets_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize)
609 if (!data_ret) return EINA_FALSE;
611 sel = selections + *((int *)data);
613 for (i = 0, count = 0; i < CNP_N_ATOMS ; i++)
615 if (sel->format & atoms[i].formats) count++;
618 aret = malloc(sizeof(Ecore_X_Atom) * count);
619 for (i = 0, count = 0; i < CNP_N_ATOMS; i++)
621 if (sel->format & atoms[i].formats) aret[count ++] = atoms[i].atom;
625 if (typesize) *typesize = 32 /* urk */;
626 if (ttype) *ttype = ECORE_X_ATOM_ATOM;
627 if (size_ret) *size_ret = count;
633 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__)
635 cnp_debug("Image converter called\n");
640 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__)
644 cnp_debug("Vcard send called\n");
646 sel = selections + *((int *)data);
648 if (data_ret) *data_ret = strdup(sel->selbuf);
649 if (size_ret) *size_ret = strlen(sel->selbuf);
654 * Callback to handle a targets response on a selection request:
655 * So pick the format we'd like; and then request it.
658 notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
660 Ecore_X_Selection_Data_Targets *targets;
661 Ecore_X_Atom *atomlist;
664 targets = notify->data;
665 atomlist = (Ecore_X_Atom *)(targets->data.data);
667 for (j = 1; j < CNP_N_ATOMS; j++)
669 cnp_debug("\t%s %d\n", atoms[j].name, atoms[j].atom);
670 if (!(atoms[j].formats & sel->requestformat)) continue;
671 for (i = 0; i < targets->data.length; i++)
673 if ((atoms[j].atom == atomlist[i]) && (atoms[j].notify))
675 cnp_debug("Atom %s matches\n",atoms[j].name);
681 cnp_debug("Couldn't find anything that matches\n");
682 return ECORE_CALLBACK_PASS_ON;
685 cnp_debug("Sending request for %s\n",atoms[j].name);
686 sel->request(elm_win_xwindow_get(sel->requestwidget), atoms[j].name);
688 return ECORE_CALLBACK_PASS_ON;
692 response_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
694 Ecore_X_Selection_Data_Targets *targets;
695 Ecore_X_Atom *atomlist;
699 targets = notify->data;
700 atomlist = (Ecore_X_Atom *)(targets->data.data);
702 /* Start from 1: Skip targets */
703 for (j = 1 ; j < CNP_N_ATOMS ; j ++)
705 if (!(atoms[j].formats & sel->requestformat)) continue;
706 for (i = 0 ; i < targets->data.length ; i ++)
708 if ((atoms[j].atom == atomlist[i]) && (atoms[j].response))
710 /* Found a match: Use it */
716 if (j == CNP_N_ATOMS)
718 cnp_debug("No matching type found\n");
722 top = elm_widget_top_get(sel->requestwidget);
725 sel->request(elm_win_xwindow_get(top), atoms[j].name);
731 notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
733 Ecore_X_Selection_Data *data;
737 cnp_debug("Notify handler text %d %d %p\n", data->format,data->length, data->data);
738 str = mark_up((char *)data->data, data->length, NULL);
739 cnp_debug("String is %s (from %s)\n", str, data->data);
740 elm_entry_entry_insert(sel->requestwidget, str);
747 * So someone is pasting an image into my entry or widget...
750 notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
752 Ecore_X_Selection_Data *data;
753 Ecore_X_Selection_Data_Files *files;
758 cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
759 if (data->content == ECORE_X_SELECTION_CONTENT_FILES)
761 cnp_debug("got a files list\n");
762 files = notify->data;
763 if (files->num_files > 1)
765 /* Don't handle many items */
766 cnp_debug("more then one file: Bailing\n");
773 p = (char *)data->data;
778 cnp_debug("Couldn't find a file\n");
781 cnp_debug("Got %s\n",p);
782 if (strncmp(p, "file://", 7))
784 /* Try and continue if it looks sane */
785 if (*p != '/') return 0;
789 p += strlen("file://");
792 if (savedtypes.pi) pasteimage_free(savedtypes.pi);
793 pi = pasteimage_alloc(p, strlen(p));
794 if (savedtypes.textreq)
796 savedtypes.textreq = 0;
801 pasteimage_append(pi, sel->requestwidget);
802 savedtypes.pi = NULL;
808 * Just receieved an vcard, either through cut and paste, or dnd.
811 vcard_receive(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
815 Ecore_X_Selection_Data *data;
818 cnp_debug("vcard receive\n");
820 if (sel == (selections + ELM_SEL_XDND))
822 Elm_Selection_Data ddata;
824 cnp_debug("drag & drop\n");
825 /* FIXME: this needs to be generic: Used for all receives */
826 EINA_LIST_FOREACH(drops, l, dropable)
828 if (dropable->obj == sel->requestwidget) break;
832 cnp_debug("Unable to find drop object");
833 ecore_x_dnd_send_finished();
836 dropable = eina_list_data_get(l);
837 ddata.x = savedtypes.x;
838 ddata.y = savedtypes.y;
839 ddata.format = ELM_SEL_FORMAT_VCARD;
840 ddata.data = data->data;
841 ddata.len = data->length;
842 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
843 ecore_x_dnd_send_finished();
845 else if (sel->datacb)
847 Elm_Selection_Data ddata;
848 ddata.x = ddata.y = 0;
849 ddata.format = ELM_SEL_FORMAT_IMAGE;
850 ddata.data = data->data;
851 ddata.len = data->length;
852 sel->datacb(sel->udata, sel->widget, &ddata);
856 cnp_debug("Paste request\n");
865 notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
867 Ecore_X_Selection_Data *data;
871 cnp_debug("got a png (or a jpeg)!\n");
874 cnp_debug("Size if %d\n", data->length);
878 Elm_Selection_Data ddata;
880 ddata.x = ddata.y = 0;
881 ddata.format = ELM_SEL_FORMAT_IMAGE;
882 ddata.data = data->data;
883 ddata.len = data->length;
884 sel->datacb(sel->udata, sel->widget, &ddata);
888 /* generate tmp name */
889 tmp = elm_cnp_tempfile_create(data->length);
890 memcpy(tmp->map, data->data, data->length);
891 munmap(tmp->map,data->length);
893 /* FIXME: Add to paste image data to clean up */
894 pi = pasteimage_alloc(tmp->filename, strlen(tmp->filename));
895 pasteimage_append(pi, sel->requestwidget);
903 * Warning: Generic text/html can';t handle it sanely.
904 * Firefox sends ucs2 (i think).
905 * chrome sends utf8... blerg
908 notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
910 Ecore_X_Selection_Data *data;
912 cnp_debug("Got some HTML: Checking encoding is useful\n");
917 Elm_Selection_Data ddata;
918 ddata.x = ddata.y = 0;
919 ddata.format = ELM_SEL_FORMAT_HTML;
920 ddata.data = data->data;
921 ddata.len = data->length;
922 sel->datacb(sel->udata, sel->widget, &ddata);
926 char *stripstr = NULL;
927 stripstr = malloc(sizeof(char) * (data->length + 1));
928 strncpy(stripstr, (char *)data->data, data->length);
929 stripstr[data->length] = '\0';
930 cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
931 elm_entry_entry_insert(sel->requestwidget, stripstr);
938 text_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
942 cnp_debug("text converter\n");
943 sel = selections + *((int *)data);
944 if (!sel->active) return EINA_TRUE;
946 if ((sel->format & ELM_SEL_FORMAT_MARKUP) ||
947 (sel->format & ELM_SEL_FORMAT_HTML))
949 *data_ret = remove_tags(sel->selbuf, size_ret);
951 else if (sel->format & ELM_SEL_FORMAT_TEXT)
953 *data_ret = strdup(sel->selbuf);
954 *size_ret = strlen(sel->selbuf);
956 else if (sel->format & ELM_SEL_FORMAT_IMAGE)
958 cnp_debug("Image %s\n", evas_object_type_get(sel->widget));
959 cnp_debug("Elm type: %s\n", elm_object_widget_type_get(sel->widget));
960 evas_object_image_file_get(elm_photocam_internal_image_get(sel->widget), (const char **)data_ret, NULL);
961 if (!*data_ret) *data_ret = strdup("No file");
962 else *data_ret = strdup(*data_ret);
963 *size_ret = strlen(*data_ret);
969 edje_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
973 sel = selections + *((int *)data);
974 if (data_ret) *data_ret = strdup(sel->selbuf);
975 if (size_ret) *size_ret = strlen(sel->selbuf);
982 html_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
986 sel = selections + *(int *)data;
987 if (data_ret) *data_ret = strdup(sel->selbuf);
988 if (size_ret) *size_ret = strlen(sel->selbuf);
994 uri_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
997 sel = selections + *((int *)data);
998 cnp_debug("Uri converter\n");
999 if (data_ret) *data_ret = strdup(sel->selbuf);
1000 if (size_ret) *size_ret = strlen(sel->selbuf);
1005 * Image paste provide
1008 /* FIXME: Should add provider for each pasted item: Use data to store it
1010 static Evas_Object *
1011 image_provider(void *images __UNUSED__, Evas_Object *entry, const char *item)
1016 cnp_debug("image provider for %s called\n", item);
1017 EINA_LIST_FOREACH(pastedimages, l, pi)
1019 cnp_debug("is it %s?\n",pi->tag);
1020 if (!strcmp(pi->tag, item))
1024 o = evas_object_image_filled_add(evas_object_evas_get(entry));
1025 /* FIXME: Handle eets */
1026 cnp_debug("file is %s (object is %p)\n", pi->file, o);
1027 evas_object_image_file_set(o, pi->file, NULL);
1028 evas_object_show(o);
1036 static Paste_Image *
1037 pasteimage_alloc(const char *file, int pathlen)
1041 char *buf, *filebuf;
1042 int prefixlen = strlen("file://");
1044 pi = calloc(1, sizeof(Paste_Image));
1045 if (!pi) return NULL;
1047 len = snprintf(NULL, 0, "pasteimage-%p", pi);
1055 snprintf(buf, len, "pasteimage-%p", pi);
1060 if (strstr(file,"file://")) file += prefixlen;
1061 filebuf = alloca(pathlen + 1);
1062 strncpy(filebuf, file, pathlen);
1063 filebuf[pathlen] = 0;
1064 pi->file = strdup(filebuf);
1071 pasteimage_free(Paste_Image *pi)
1074 if (pi->file) free((void*)pi->file);
1075 if (pi->tag) free((void*)pi->tag);
1080 pasteimage_provider_set(Evas_Object *entry)
1085 if (!entry) return EINA_FALSE;
1086 type = elm_widget_type_get(entry);
1087 cnp_debug("type is %s\n", type);
1088 if ((!type) || (strcmp(type, "entry"))) return EINA_FALSE;
1090 v = evas_object_data_get(entry, PROVIDER_SET);
1093 evas_object_data_set(entry, PROVIDER_SET, pasteimage_provider_set);
1094 elm_entry_item_provider_append(entry, image_provider, NULL);
1095 evas_object_event_callback_add(entry, EVAS_CALLBACK_FREE,
1096 entry_deleted, NULL);
1103 pasteimage_append(Paste_Image *pi, Evas_Object *entry)
1107 static const char *tagstring = "<item absize=240x180 href=file://%s></item>";
1109 if (!pi) return EINA_FALSE;
1110 if (!entry) return EINA_FALSE;
1112 pasteimage_provider_set(entry);
1114 len = strlen(tagstring)+strlen(pi->file);
1116 pastedimages = eina_list_append(pastedimages, pi);
1117 entrytag = alloca(len + 1);
1118 snprintf(entrytag, len + 1, tagstring, pi->file);
1119 elm_entry_entry_insert(entry, entrytag);
1125 entry_deleted(void *images __UNUSED__, Evas *e __UNUSED__, Evas_Object *entry, void *unused __UNUSED__)
1130 EINA_LIST_FOREACH_SAFE(pastedimages, l, next, pi)
1132 if (pi->entry == entry)
1133 pastedimages = eina_list_remove_list(pastedimages, l);
1139 remove_tags(const char *p, int *len)
1143 if (!p) return NULL;
1145 q = malloc(strlen(p) + 1);
1146 if (!q) return NULL;
1151 if ((*p != '<') && (*p != '&')) *q++ = *p++;
1154 if ((p[1] == 'b') && (p[2] == 'r') &&
1155 ((p[3] == ' ') || (p[3] == '/') || (p[3] == '>')))
1157 while ((*p) && (*p != '>')) p++;
1163 for (i = 0 ; i < N_ESCAPES ; i++)
1165 if (!strncmp(p,escapes[i].escape, strlen(escapes[i].escape)))
1167 p += strlen(escapes[i].escape);
1168 *q = escapes[i].value;
1173 if (i == N_ESCAPES) *q ++= '&';
1177 if (len) *len = q - ret;
1183 mark_up(const char *start, int inlen, int *lenp)
1188 const char *endp = NULL;
1190 if (!start) return NULL;
1191 if (inlen >= 0) endp = start + inlen;
1192 /* First pass: Count characters */
1193 for (l = 0, p = start; ((!endp) || (p < endp)) && (*p); p++)
1195 for (i = 0 ; i < N_ESCAPES ; i ++)
1197 if (*p == escapes[i].value)
1199 l += strlen(escapes[i].escape);
1203 if (i == N_ESCAPES) l++;
1206 q = ret = malloc(l + 1);
1208 /* Second pass: Change characters */
1209 for (p = start; *p; )
1211 for (i = 0; i < N_ESCAPES; i++)
1213 if (*p == escapes[i].value)
1215 strcpy(q, escapes[i].escape);
1216 q += strlen(escapes[i].escape);
1221 if (i == N_ESCAPES) *q++ = *p++;
1225 if (lenp) *lenp = l;
1231 _dnd_enter(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1233 Ecore_X_Event_Xdnd_Enter *enter = ev;
1237 if ((!enter) || (!enter->num_types) || (!enter->types)) return EINA_TRUE;
1239 cnp_debug("Types\n");
1240 savedtypes.ntypes = enter->num_types;
1241 if (savedtypes.types) free(savedtypes.types);
1242 savedtypes.types = malloc(sizeof(char *) * enter->num_types);
1243 if (!savedtypes.types) return EINA_FALSE;
1245 for (i = 0; i < enter->num_types; i++)
1247 savedtypes.types[i] = eina_stringshare_add(enter->types[i]);
1248 cnp_debug("Type is %s %p %p\n", enter->types[i],
1249 savedtypes.types[i],text_uri);
1250 if (savedtypes.types[i] == text_uri)
1252 /* Request it, so we know what it is */
1253 cnp_debug("Sending uri request\n");
1254 savedtypes.textreq = 1;
1255 savedtypes.pi = NULL; /* FIXME: Free? */
1256 ecore_x_selection_xdnd_request(enter->win, text_uri);
1260 /* FIXME: Find an object and make it current */
1265 _dnd_drop(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1267 struct _Ecore_X_Event_Xdnd_Drop *drop;
1271 Ecore_X_Window xwin;
1272 Elm_Selection_Data ddata;
1278 // check we still have something to drop
1279 if (!drops) return EINA_TRUE;
1281 /* Find any widget in our window; then work out geometry rel to our window */
1282 for (l = drops; l; l = l->next)
1285 xwin = (Ecore_X_Window)ecore_evas_window_get
1286 (ecore_evas_ecore_evas_get(evas_object_evas_get
1288 if (xwin == drop->win) break;
1290 /* didn't find a window */
1291 if (!l) return EINA_TRUE;
1293 /* Calculate real (widget relative) position */
1294 // - window position
1295 // - widget position
1296 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(dropable->obj));
1297 ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
1298 savedtypes.x = drop->position.x - x;
1299 savedtypes.y = drop->position.y - y;
1301 cnp_debug("Drop position is %d,%d\n", savedtypes.x, savedtypes.y);
1303 for (; l; l = l->next)
1306 evas_object_geometry_get(dropable->obj, &x, &y, &w, &h);
1307 if ((savedtypes.x >= x) && (savedtypes.y >= y) &&
1308 (savedtypes.x < x + w) && (savedtypes.y < y + h))
1312 if (!l) return EINA_TRUE; /* didn't find one */
1314 evas_object_geometry_get(dropable->obj, &x, &y, NULL, NULL);
1318 /* Find our type from the previous list */
1319 for (i = 0; i < CNP_N_ATOMS; i++)
1321 for (j = 0; j < savedtypes.ntypes; j++)
1323 if (!strcmp(savedtypes.types[j], atoms[i].name)) goto found;
1327 cnp_debug("Didn't find a target\n");
1331 cnp_debug("Found a target we'd like: %s\n", atoms[i].name);
1332 cnp_debug("0x%x\n",xwin);
1334 if (i == CNP_ATOM_text_urilist)
1336 cnp_debug("We found a URI... (%scached) %s\n",
1337 savedtypes.pi ? "" : "not ",
1338 savedtypes.pi->file);
1342 static const char *tagstring = "<item absize=240x180 href="
1343 "file://%s></item>";
1344 ddata.x = savedtypes.x;
1345 ddata.y = savedtypes.y;
1347 /* If it's markup that also supports images */
1348 if ((dropable->types & ELM_SEL_FORMAT_MARKUP) &&
1349 (dropable->types & ELM_SEL_FORMAT_IMAGE))
1352 ddata.format = ELM_SEL_FORMAT_MARKUP;
1353 pasteimage_provider_set(dropable->obj);
1355 pastedimages = eina_list_append(pastedimages, savedtypes.pi);
1356 len = strlen(tagstring) + strlen(savedtypes.pi->file);
1357 entrytag = alloca(len + 1);
1358 snprintf(entrytag, len + 1, tagstring, savedtypes.pi->file);
1359 ddata.data = entrytag;
1360 cnp_debug("Insert %s\n", (char *)ddata.data);
1361 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1362 ecore_x_dnd_send_finished();
1365 else if (dropable->types & ELM_SEL_FORMAT_IMAGE)
1367 cnp_debug("Doing image insert (%s)\n", savedtypes.pi->file);
1368 ddata.format = ELM_SEL_FORMAT_IMAGE;
1369 ddata.data = (char *)savedtypes.pi->file;
1370 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1371 ecore_x_dnd_send_finished();
1373 pasteimage_free(savedtypes.pi);
1374 savedtypes.pi = NULL;
1380 cnp_debug("Item doesn't support images... passing\n");
1381 pasteimage_free(savedtypes.pi);
1385 else if (savedtypes.textreq)
1387 /* Already asked: Pretend we asked now, and paste immediately when
1389 savedtypes.textreq = 0;
1390 ecore_x_dnd_send_finished();
1395 cnp_debug("doing a request then\n");
1396 selections[ELM_SEL_XDND].requestwidget = dropable->obj;
1397 selections[ELM_SEL_XDND].requestformat = ELM_SEL_FORMAT_MARKUP;
1398 selections[ELM_SEL_XDND].active = EINA_TRUE;
1400 ecore_x_selection_xdnd_request(xwin, atoms[i].name);
1405 _dnd_position(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1407 struct _Ecore_X_Event_Xdnd_Position *pos;
1408 Ecore_X_Rectangle rect;
1412 /* Need to send a status back */
1413 /* FIXME: Should check I can drop here */
1414 /* FIXME: Should highlight widget */
1415 rect.x = pos->position.x - 5;
1416 rect.y = pos->position.y - 5;
1419 ecore_x_dnd_send_status(EINA_TRUE, EINA_FALSE, rect, pos->action);
1425 * When dragging this is callback response from the destination.
1426 * The important thing we care about: Can we drop; thus update cursor
1430 _dnd_status(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1432 struct _Ecore_X_Event_Xdnd_Status *status = ev;
1434 if (!status) return EINA_TRUE;
1436 /* Only thing we care about: will accept */
1437 if (status->will_accept)
1439 cnp_debug("Will accept\n");
1442 { /* Won't accept */
1443 cnp_debug("Won't accept accept\n");
1449 * Add a widget as drop target.
1452 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
1455 Ecore_X_Window xwin;
1459 if (!obj) return EINA_FALSE;
1460 if (!_elm_cnp_init_count) _elm_cnp_init();
1462 /* Is this the first? */
1463 first = (!drops) ? 1 : 0;
1465 EINA_LIST_FOREACH(drops, item, drop)
1467 if (drop->obj == obj)
1469 /* Update: Not a new one */
1470 drop->dropcb = dropcb;
1471 drop->cbdata = cbdata;
1472 drop->types = format;
1477 /* Create new drop */
1478 drop = calloc(1, sizeof(Dropable));
1479 if (!drop) return EINA_FALSE;
1480 /* FIXME: Check for eina's deranged error method */
1481 drops = eina_list_append(drops, drop);
1483 if (!drops/* || or other error */)
1488 drop->dropcb = dropcb;
1489 drop->cbdata = cbdata;
1490 drop->types = format;
1493 evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL,
1494 /* I love C and varargs */
1495 (Evas_Object_Event_Cb)elm_drop_target_del,
1497 /* FIXME: Handle resizes */
1499 /* If not the first: We're done */
1500 if (!first) return EINA_TRUE;
1502 xwin = (Ecore_X_Window)ecore_evas_window_get
1503 (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
1505 ecore_x_dnd_aware_set(xwin, EINA_TRUE);
1507 cnp_debug("Adding drop target calls\n");
1508 handler_enter = ecore_event_handler_add(ECORE_X_EVENT_XDND_ENTER,
1510 handler_pos = ecore_event_handler_add(ECORE_X_EVENT_XDND_POSITION,
1511 _dnd_position, NULL);
1512 handler_drop = ecore_event_handler_add(ECORE_X_EVENT_XDND_DROP,
1519 elm_drop_target_del(Evas_Object *obj)
1521 Dropable *drop,*del;
1523 Ecore_X_Window xwin;
1526 EINA_LIST_FOREACH(drops, item, drop)
1528 if (drop->obj == obj)
1530 drops = eina_list_remove_list(drops, item);
1535 if (!del) return EINA_FALSE;
1537 evas_object_event_callback_del(obj, EVAS_CALLBACK_FREE,
1538 (Evas_Object_Event_Cb)elm_drop_target_del);
1540 /* If still drops there: All fine.. continue */
1541 if (drops) return EINA_TRUE;
1543 cnp_debug("Disabling DND\n");
1544 xwin = (Ecore_X_Window)ecore_evas_window_get
1545 (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
1546 ecore_x_dnd_aware_set(xwin, EINA_FALSE);
1548 ecore_event_handler_del(handler_pos);
1549 ecore_event_handler_del(handler_drop);
1550 ecore_event_handler_del(handler_enter);
1554 pasteimage_free(savedtypes.pi);
1555 savedtypes.pi = NULL;
1563 _drag_mouse_up(void *un __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *data __UNUSED__)
1565 evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_UP, _drag_mouse_up);
1569 dragdonecb(dragdonecb,selections[ELM_SEL_XDND].widget);
1574 evas_object_del(dragwin);
1580 _drag_move(void *data __UNUSED__, Ecore_X_Xdnd_Position *pos)
1582 evas_object_move(dragwin,
1583 pos->position.x - _dragx,
1584 pos->position.y - _dragy);
1589 elm_drag_start(Evas_Object *obj, Elm_Sel_Format format, const char *data, void (*dragdone) (void *data, Evas_Object *), void *donecbdata)
1591 Ecore_X_Window xwin;
1593 Elm_Sel_Type xdnd = ELM_SEL_XDND;
1595 int x, y, x2, y2, x3, y3;
1599 if (!_elm_cnp_init_count) _elm_cnp_init();
1601 xwin = elm_win_xwindow_get(obj);
1603 cnp_debug("starting drag...\n");
1605 ecore_x_dnd_type_set(xwin, "text/uri-list", 1);
1606 sel = selections + ELM_SEL_XDND;
1609 sel->format = format;
1610 sel->selbuf = data ? strdup(data) : NULL;
1611 dragdonecb = dragdone;
1612 dragdonedata = donecbdata;
1614 ecore_x_dnd_callback_pos_update_set(_drag_move, NULL);
1615 ecore_x_dnd_begin(xwin, (unsigned char *)&xdnd, sizeof(Elm_Sel_Type));
1616 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
1617 _drag_mouse_up, NULL);
1619 handler_status = ecore_event_handler_add(ECORE_X_EVENT_XDND_STATUS,
1622 dragwin = elm_win_add(NULL, "Elm Drag Object", ELM_WIN_UTILITY);
1623 elm_win_override_set(dragwin, 1);
1625 /* FIXME: Images only */
1626 icon = elm_icon_add(dragwin);
1627 elm_icon_file_set(icon, data + 7, NULL); /* 7!? "file://" */
1628 elm_win_resize_object_add(dragwin,icon);
1629 evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1630 evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, EVAS_HINT_FILL);
1632 /* Position subwindow appropriately */
1633 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(obj));
1634 ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
1635 evas_object_geometry_get(obj, &x2, &y2, &w, &h);
1638 evas_object_move(dragwin, x, y);
1639 evas_object_resize(icon, w, h);
1640 evas_object_resize(dragwin, w, h);
1642 evas_object_show(icon);
1643 evas_object_show(dragwin);
1645 evas_pointer_canvas_xy_get(evas_object_evas_get(obj), &x3, &y3);
1653 elm_cnp_tempfile_create(int size)
1656 const char *tmppath;
1659 info = malloc(sizeof(Tmp_Info));
1660 if (!info) return NULL;
1662 tmppath = getenv("TMP");
1663 if (!tmppath) tmppath = P_tmpdir;
1664 if (!tmppath) tmppath = "/tmp";
1665 len = snprintf(NULL, 0, "%s/%sXXXXXX", tmppath, "elmcnpitem-");
1672 info->filename = malloc(len);
1673 if (!info->filename)
1678 snprintf(info->filename,len,"%s/%sXXXXXX", tmppath, "elmcnpitem-");
1680 info->fd = mkstemp(info->filename);
1685 /* And before someone says anything see POSIX 1003.1-2008 page 400 */
1688 pid = (long)getpid();
1689 /* Use pid instead of /proc/self: That way if can be passed around */
1690 len = snprintf(NULL,0,"/proc/%li/fd/%i", pid, info->fd);
1695 snprintf(tmp,len, "/proc/%li/fd/%i", pid, info->fd);
1696 unlink(info->filename);
1697 free(info->filename);
1698 info->filename = tmp;
1703 cnp_debug("filename is %s\n", info->filename);
1706 /* Set map to NULL and return */
1713 if (ftruncate(info->fd, size))
1715 perror("ftruncate");
1721 info->map = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, info->fd, 0);
1722 if (info->map == MAP_FAILED)
1734 tmpinfo_free(Tmp_Info *info)
1736 if (!info) return 0;
1737 free(info->filename);
1743 /* Stubs for windows */
1745 elm_drag_start(Evas_Object *o, Elm_Sel_Format f, const char *d, void (*donecb)(void *, Evas_Object *),void *cbdata)
1751 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
1757 elm_drop_target_del(Evas_Object *o)
1763 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-2f0^-2{2(0W1st0 :*/