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 Cnp_Atom atoms[CNP_N_ATOMS] = {
182 [CNP_ATOM_TARGETS] = {
184 (Elm_Sel_Format) -1, // everything
186 response_handler_targets,
187 notify_handler_targets,
191 "application/x-elementary-markup",
192 ELM_SEL_FORMAT_MARKUP,
198 [CNP_ATOM_text_uri] = {
200 ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_IMAGE, /* Either images or entries */
206 [CNP_ATOM_text_urilist] = {
208 ELM_SEL_FORMAT_IMAGE,
214 [CNP_ATOM_text_x_vcard] = {
216 ELM_SEL_FORMAT_VCARD,
220 [CNP_ATOM_image_png] = {
222 ELM_SEL_FORMAT_IMAGE,
225 notify_handler_image,
228 [CNP_ATOM_image_jpeg] = {
230 ELM_SEL_FORMAT_IMAGE,
233 notify_handler_image,/* Raw image data is the same */
236 [CNP_ATOM_image_bmp] = {
238 ELM_SEL_FORMAT_IMAGE,
241 notify_handler_image,/* Raw image data is the same */
244 [CNP_ATOM_image_gif] = {
246 ELM_SEL_FORMAT_IMAGE,
249 notify_handler_image,/* Raw image data is the same */
252 [CNP_ATOM_image_tiff] = {
254 ELM_SEL_FORMAT_IMAGE,
257 notify_handler_image,/* Raw image data is the same */
260 [CNP_ATOM_image_svg] = {
262 ELM_SEL_FORMAT_IMAGE,
265 notify_handler_image,/* Raw image data is the same */
268 [CNP_ATOM_image_xpm] = {
270 ELM_SEL_FORMAT_IMAGE,
273 notify_handler_image,/* Raw image data is the same */
276 [CNP_ATOM_image_tga] = {
278 ELM_SEL_FORMAT_IMAGE,
281 notify_handler_image,/* Raw image data is the same */
284 [CNP_ATOM_image_ppm] = {
285 "image/x-portable-pixmap",
286 ELM_SEL_FORMAT_IMAGE,
289 notify_handler_image,/* Raw image data is the same */
292 [CNP_ATOM_text_html_utf8] = {
293 "text/html;charset=utf-8",
300 [CNP_ATOM_text_html] = {
305 notify_handler_html, /* No encoding: Webkit only */
308 [CNP_ATOM_UTF8STRING] = {
310 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
316 [CNP_ATOM_STRING] = {
318 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
326 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
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,
340 [CNP_ATOM_text_plain] = {
342 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
350 static Cnp_Selection selections[ELM_SEL_MAX] = {
351 ARRAYINIT(ELM_SEL_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,
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,
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,
372 ARRAYINIT(ELM_SEL_XDND) {
374 .ecore_sel = ECORE_X_SELECTION_XDND,
375 .request = ecore_x_selection_xdnd_request,
379 /* Data for DND in progress */
380 static Saved_Type savedtypes = { NULL, NULL, 0, 0, 0, EINA_FALSE };
382 static void (*dragdonecb) (void *data, Evas_Object *obj) = NULL;
383 static void *dragdonedata = NULL;
385 static int _elm_cnp_init_count = 0;
386 /* FIXME: who left this out of XAtoms.h */
387 static Ecore_X_Atom clipboard_atom;
389 static Eina_List *pastedimages = NULL;
392 * Drag & Drop functions
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;
406 /* Stringshared, so I can just compare pointers later */
407 static const char *text_uri;
410 elm_selection_set(Elm_Sel_Type selection, Evas_Object *widget, Elm_Sel_Format format, const char *selbuf)
412 #ifdef HAVE_ELEMENTARY_X
413 Evas_Object *top = elm_widget_top_get(widget);
417 if (top) xwin = elm_win_xwindow_get(top);
418 else xwin = elm_win_xwindow_get(widget);
419 if (!xwin) return EINA_FALSE;
420 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
421 if (!_elm_cnp_init_count) _elm_cnp_init();
422 if ((!selbuf) && (format != ELM_SEL_FORMAT_IMAGE))
423 return elm_selection_clear(selection, widget);
425 sel = selections + selection;
428 sel->widget = widget;
430 sel->set(xwin, &selection, sizeof(Elm_Sel_Type));
431 sel->format = format;
432 sel->selbuf = selbuf ? strdup(selbuf) : NULL;
441 elm_selection_clear(Elm_Sel_Type selection, Evas_Object *widget)
443 #ifdef HAVE_ELEMENTARY_X
446 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
447 if (!_elm_cnp_init_count) _elm_cnp_init();
449 sel = selections + selection;
451 /* No longer this selection: Consider it gone! */
452 if ((!sel->active) || (sel->widget != widget)) return EINA_TRUE;
465 elm_selection_get(Elm_Sel_Type selection, Elm_Sel_Format format,
466 Evas_Object *widget, Elm_Drop_Cb datacb, void *udata)
468 #ifdef HAVE_ELEMENTARY_X
472 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
473 if (!_elm_cnp_init_count) _elm_cnp_init();
475 sel = selections + selection;
476 top = elm_widget_top_get(widget);
477 if (!top) return EINA_FALSE;
479 sel->requestformat = format;
480 sel->requestwidget = widget;
481 sel->request(elm_win_xwindow_get(top), ECORE_X_SELECTION_TARGET_TARGETS);
482 sel->datacb = datacb;
491 #ifdef HAVE_ELEMENTARY_X
498 if (_elm_cnp_init_count++) return EINA_TRUE;
499 for (i = 0; i < CNP_N_ATOMS; i++)
501 atoms[i].atom = ecore_x_atom_get(atoms[i].name);
502 ecore_x_selection_converter_atom_add(atoms[i].atom,
505 clipboard_atom = ecore_x_atom_get("CLIPBOARD");
507 ecore_event_handler_add(ECORE_X_EVENT_SELECTION_CLEAR, selection_clear, NULL);
508 ecore_event_handler_add(ECORE_X_EVENT_SELECTION_NOTIFY, selection_notify, NULL);
510 text_uri = eina_stringshare_add("text/uri-list");
515 selection_clear(void *udata __UNUSED__, int type __UNUSED__, void *event)
517 Ecore_X_Event_Selection_Clear *ev = event;
521 for (i = 0; i < ELM_SEL_MAX; i++)
523 if (selections[i].ecore_sel == ev->selection) break;
525 cnp_debug("selection %d clear\n", i);
526 /* Not me... Don't care */
527 if (i == ELM_SEL_MAX) return ECORE_CALLBACK_PASS_ON;
529 sel = selections + i;
534 return ECORE_CALLBACK_PASS_ON;
539 * Response to a selection notify:
540 * - So we have asked for the selection list.
541 * - If it's the targets list, parse it, and fire of what we want,
542 * else it's the data we want.
545 selection_notify(void *udata __UNUSED__, int type __UNUSED__, void *event)
547 Ecore_X_Event_Selection_Notify *ev = event;
551 cnp_debug("selection notify callback: %d\n",ev->selection);
552 switch (ev->selection)
554 case ECORE_X_SELECTION_CLIPBOARD:
555 sel = selections + ELM_SEL_CLIPBOARD;
557 case ECORE_X_SELECTION_PRIMARY:
558 sel = selections + ELM_SEL_PRIMARY;
560 case ECORE_X_SELECTION_SECONDARY:
561 sel = selections + ELM_SEL_SECONDARY;
563 case ECORE_X_SELECTION_XDND:
564 sel = selections + ELM_SEL_XDND;
567 return ECORE_CALLBACK_PASS_ON;
569 cnp_debug("Target is %s\n", ev->target);
571 for (i = 0; i < CNP_N_ATOMS; i++)
573 if (!strcmp(ev->target, atoms[i].name))
577 cnp_debug("Found something: %s\n", atoms[i].name);
578 atoms[i].notify(sel, ev);
582 cnp_debug("Ignored: No handler!\n");
587 return ECORE_CALLBACK_PASS_ON;
593 targets_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize)
599 if (!data_ret) return EINA_FALSE;
601 sel = selections + *((int *)data);
603 for (i = 0, count = 0; i < CNP_N_ATOMS ; i++)
605 if (sel->format & atoms[i].formats) count++;
608 aret = malloc(sizeof(Ecore_X_Atom) * count);
609 for (i = 0, count = 0; i < CNP_N_ATOMS; i++)
611 if (sel->format & atoms[i].formats) aret[count ++] = atoms[i].atom;
615 if (typesize) *typesize = 32 /* urk */;
616 if (ttype) *ttype = ECORE_X_ATOM_ATOM;
617 if (size_ret) *size_ret = count;
623 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__)
625 cnp_debug("Image converter called\n");
630 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__)
634 cnp_debug("Vcard send called\n");
636 sel = selections + *((int *)data);
638 if (data_ret) *data_ret = strdup(sel->selbuf);
639 if (size_ret) *size_ret = strlen(sel->selbuf);
644 * Callback to handle a targets response on a selection request:
645 * So pick the format we'd like; and then request it.
648 notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
650 Ecore_X_Selection_Data_Targets *targets;
651 Ecore_X_Atom *atomlist;
654 targets = notify->data;
655 atomlist = (Ecore_X_Atom *)(targets->data.data);
657 for (j = 1; j < CNP_N_ATOMS; j++)
659 cnp_debug("\t%s %d\n", atoms[j].name, atoms[j].atom);
660 if (!(atoms[j].formats & sel->requestformat)) continue;
661 for (i = 0; i < targets->data.length; i++)
663 if ((atoms[j].atom == atomlist[i]) && (atoms[j].notify))
665 cnp_debug("Atom %s matches\n",atoms[j].name);
671 cnp_debug("Couldn't find anything that matches\n");
672 return ECORE_CALLBACK_PASS_ON;
675 cnp_debug("Sending request for %s\n",atoms[j].name);
676 sel->request(elm_win_xwindow_get(sel->requestwidget), atoms[j].name);
678 return ECORE_CALLBACK_PASS_ON;
682 response_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
684 Ecore_X_Selection_Data_Targets *targets;
685 Ecore_X_Atom *atomlist;
689 targets = notify->data;
690 atomlist = (Ecore_X_Atom *)(targets->data.data);
692 /* Start from 1: Skip targets */
693 for (j = 1 ; j < CNP_N_ATOMS ; j ++)
695 if (!(atoms[j].formats & sel->requestformat)) continue;
696 for (i = 0 ; i < targets->data.length ; i ++)
698 if ((atoms[j].atom == atomlist[i]) && (atoms[j].response))
700 /* Found a match: Use it */
706 if (j == CNP_N_ATOMS)
708 cnp_debug("No matching type found\n");
712 top = elm_widget_top_get(sel->requestwidget);
715 sel->request(elm_win_xwindow_get(top), atoms[j].name);
721 notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
723 Ecore_X_Selection_Data *data;
727 cnp_debug("Notify handler text %d %d %p\n", data->format,data->length, data->data);
728 str = mark_up((char *)data->data, data->length, NULL);
729 cnp_debug("String is %s (from %s)\n", str, data->data);
730 elm_entry_entry_insert(sel->requestwidget, str);
737 * So someone is pasting an image into my entry or widget...
740 notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
742 Ecore_X_Selection_Data *data;
743 Ecore_X_Selection_Data_Files *files;
748 cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
749 if (data->content == ECORE_X_SELECTION_CONTENT_FILES)
751 cnp_debug("got a files list\n");
752 files = notify->data;
753 if (files->num_files > 1)
755 /* Don't handle many items */
756 cnp_debug("more then one file: Bailing\n");
763 p = (char *)data->data;
768 cnp_debug("Couldn't find a file\n");
771 cnp_debug("Got %s\n",p);
772 if (strncmp(p, "file://", 7))
774 /* Try and continue if it looks sane */
775 if (*p != '/') return 0;
779 p += strlen("file://");
782 if (savedtypes.pi) pasteimage_free(savedtypes.pi);
783 pi = pasteimage_alloc(p, strlen(p));
784 if (savedtypes.textreq)
786 savedtypes.textreq = 0;
791 pasteimage_append(pi, sel->requestwidget);
792 savedtypes.pi = NULL;
798 * Just receieved an vcard, either through cut and paste, or dnd.
801 vcard_receive(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
805 Ecore_X_Selection_Data *data;
808 cnp_debug("vcard receive\n");
810 if (sel == (selections + ELM_SEL_XDND))
812 Elm_Selection_Data ddata;
814 cnp_debug("drag & drop\n");
815 /* FIXME: this needs to be generic: Used for all receives */
816 EINA_LIST_FOREACH(drops, l, dropable)
818 if (dropable->obj == sel->requestwidget) break;
822 cnp_debug("Unable to find drop object");
823 ecore_x_dnd_send_finished();
826 dropable = eina_list_data_get(l);
827 ddata.x = savedtypes.x;
828 ddata.y = savedtypes.y;
829 ddata.format = ELM_SEL_FORMAT_VCARD;
830 ddata.data = data->data;
831 ddata.len = data->length;
832 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
833 ecore_x_dnd_send_finished();
835 else if (sel->datacb)
837 Elm_Selection_Data ddata;
838 ddata.x = ddata.y = 0;
839 ddata.format = ELM_SEL_FORMAT_IMAGE;
840 ddata.data = data->data;
841 ddata.len = data->length;
842 sel->datacb(sel->udata, sel->widget, &ddata);
846 cnp_debug("Paste request\n");
855 notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
857 Ecore_X_Selection_Data *data;
861 cnp_debug("got a png (or a jpeg)!\n");
864 cnp_debug("Size if %d\n", data->length);
868 Elm_Selection_Data ddata;
870 ddata.x = ddata.y = 0;
871 ddata.format = ELM_SEL_FORMAT_IMAGE;
872 ddata.data = data->data;
873 ddata.len = data->length;
874 sel->datacb(sel->udata, sel->widget, &ddata);
878 /* generate tmp name */
879 tmp = elm_cnp_tempfile_create(data->length);
880 memcpy(tmp->map, data->data, data->length);
881 munmap(tmp->map,data->length);
883 /* FIXME: Add to paste image data to clean up */
884 pi = pasteimage_alloc(tmp->filename, strlen(tmp->filename));
885 pasteimage_append(pi, sel->requestwidget);
893 * Warning: Generic text/html can';t handle it sanely.
894 * Firefox sends ucs2 (i think).
895 * chrome sends utf8... blerg
898 notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
900 Ecore_X_Selection_Data *data;
902 cnp_debug("Got some HTML: Checking encoding is useful\n");
907 Elm_Selection_Data ddata;
908 ddata.x = ddata.y = 0;
909 ddata.format = ELM_SEL_FORMAT_HTML;
910 ddata.data = data->data;
911 ddata.len = data->length;
912 sel->datacb(sel->udata, sel->widget, &ddata);
916 char *stripstr = NULL;
917 stripstr = malloc(sizeof(char) * (data->length + 1));
918 strncpy(stripstr, (char *)data->data, data->length);
919 stripstr[data->length] = '\0';
920 cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
921 elm_entry_entry_insert(sel->requestwidget, stripstr);
928 text_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
932 cnp_debug("text converter\n");
933 sel = selections + *((int *)data);
934 if (!sel->active) return EINA_TRUE;
936 if ((sel->format & ELM_SEL_FORMAT_MARKUP) ||
937 (sel->format & ELM_SEL_FORMAT_HTML))
939 *data_ret = remove_tags(sel->selbuf, size_ret);
941 else if (sel->format & ELM_SEL_FORMAT_TEXT)
943 *data_ret = strdup(sel->selbuf);
944 *size_ret = strlen(sel->selbuf);
946 else if (sel->format & ELM_SEL_FORMAT_IMAGE)
948 cnp_debug("Image %s\n", evas_object_type_get(sel->widget));
949 cnp_debug("Elm type: %s\n", elm_object_widget_type_get(sel->widget));
950 evas_object_image_file_get(elm_photocam_internal_image_get(sel->widget), (const char **)data_ret, NULL);
951 if (!*data_ret) *data_ret = strdup("No file");
952 else *data_ret = strdup(*data_ret);
953 *size_ret = strlen(*data_ret);
959 edje_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
963 sel = selections + *((int *)data);
964 if (data_ret) *data_ret = strdup(sel->selbuf);
965 if (size_ret) *size_ret = strlen(sel->selbuf);
972 html_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
976 sel = selections + *(int *)data;
977 if (data_ret) *data_ret = strdup(sel->selbuf);
978 if (size_ret) *size_ret = strlen(sel->selbuf);
984 uri_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
987 sel = selections + *((int *)data);
988 cnp_debug("Uri converter\n");
989 if (data_ret) *data_ret = strdup(sel->selbuf);
990 if (size_ret) *size_ret = strlen(sel->selbuf);
995 * Image paste provide
998 /* FIXME: Should add provider for each pasted item: Use data to store it
1000 static Evas_Object *
1001 image_provider(void *images __UNUSED__, Evas_Object *entry, const char *item)
1006 cnp_debug("image provider for %s called\n", item);
1007 EINA_LIST_FOREACH(pastedimages, l, pi)
1009 cnp_debug("is it %s?\n",pi->tag);
1010 if (!strcmp(pi->tag, item))
1014 o = evas_object_image_filled_add(evas_object_evas_get(entry));
1015 /* FIXME: Handle eets */
1016 cnp_debug("file is %s (object is %p)\n", pi->file, o);
1017 evas_object_image_file_set(o, pi->file, NULL);
1018 evas_object_show(o);
1026 static Paste_Image *
1027 pasteimage_alloc(const char *file, int pathlen)
1031 char *buf, *filebuf;
1032 int prefixlen = strlen("file://");
1034 pi = calloc(1, sizeof(Paste_Image));
1035 if (!pi) return NULL;
1037 len = snprintf(NULL, 0, "pasteimage-%p", pi);
1045 snprintf(buf, len, "pasteimage-%p", pi);
1050 if (strstr(file,"file://")) file += prefixlen;
1051 filebuf = alloca(pathlen + 1);
1052 strncpy(filebuf, file, pathlen);
1053 filebuf[pathlen] = 0;
1054 pi->file = strdup(filebuf);
1061 pasteimage_free(Paste_Image *pi)
1064 if (pi->file) free((void*)pi->file);
1065 if (pi->tag) free((void*)pi->tag);
1070 pasteimage_provider_set(Evas_Object *entry)
1075 if (!entry) return EINA_FALSE;
1076 type = elm_widget_type_get(entry);
1077 cnp_debug("type is %s\n", type);
1078 if ((!type) || (strcmp(type, "entry"))) return EINA_FALSE;
1080 v = evas_object_data_get(entry, PROVIDER_SET);
1083 evas_object_data_set(entry, PROVIDER_SET, pasteimage_provider_set);
1084 elm_entry_item_provider_append(entry, image_provider, NULL);
1085 evas_object_event_callback_add(entry, EVAS_CALLBACK_FREE,
1086 entry_deleted, NULL);
1093 pasteimage_append(Paste_Image *pi, Evas_Object *entry)
1097 static const char *tagstring = "<item absize=240x180 href=file://%s></item>";
1099 if (!pi) return EINA_FALSE;
1100 if (!entry) return EINA_FALSE;
1102 pasteimage_provider_set(entry);
1104 len = strlen(tagstring)+strlen(pi->file);
1106 pastedimages = eina_list_append(pastedimages, pi);
1107 entrytag = alloca(len + 1);
1108 snprintf(entrytag, len + 1, tagstring, pi->file);
1109 elm_entry_entry_insert(entry, entrytag);
1115 entry_deleted(void *images __UNUSED__, Evas *e __UNUSED__, Evas_Object *entry, void *unused __UNUSED__)
1120 EINA_LIST_FOREACH_SAFE(pastedimages, l, next, pi)
1122 if (pi->entry == entry)
1123 pastedimages = eina_list_remove_list(pastedimages, l);
1129 remove_tags(const char *p, int *len)
1133 if (!p) return NULL;
1135 q = malloc(strlen(p) + 1);
1136 if (!q) return NULL;
1141 if ((*p != '<') && (*p != '&')) *q++ = *p++;
1144 if ((p[1] == 'b') && (p[2] == 'r') &&
1145 ((p[3] == ' ') || (p[3] == '/') || (p[3] == '>')))
1147 while ((*p) && (*p != '>')) p++;
1153 for (i = 0 ; i < N_ESCAPES ; i++)
1155 if (!strncmp(p,escapes[i].escape, strlen(escapes[i].escape)))
1157 p += strlen(escapes[i].escape);
1158 *q = escapes[i].value;
1163 if (i == N_ESCAPES) *q ++= '&';
1167 if (len) *len = q - ret;
1173 mark_up(const char *start, int inlen, int *lenp)
1178 const char *endp = NULL;
1180 if (!start) return NULL;
1181 if (inlen >= 0) endp = start + inlen;
1182 /* First pass: Count characters */
1183 for (l = 0, p = start; ((!endp) || (p < endp)) && (*p); p++)
1185 for (i = 0 ; i < N_ESCAPES ; i ++)
1187 if (*p == escapes[i].value)
1189 l += strlen(escapes[i].escape);
1193 if (i == N_ESCAPES) l++;
1196 q = ret = malloc(l + 1);
1198 /* Second pass: Change characters */
1199 for (p = start; *p; )
1201 for (i = 0; i < N_ESCAPES; i++)
1203 if (*p == escapes[i].value)
1205 strcpy(q, escapes[i].escape);
1206 q += strlen(escapes[i].escape);
1211 if (i == N_ESCAPES) *q++ = *p++;
1215 if (lenp) *lenp = l;
1221 _dnd_enter(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1223 Ecore_X_Event_Xdnd_Enter *enter = ev;
1227 if ((!enter) || (!enter->num_types) || (!enter->types)) return EINA_TRUE;
1229 cnp_debug("Types\n");
1230 savedtypes.ntypes = enter->num_types;
1231 if (savedtypes.types) free(savedtypes.types);
1232 savedtypes.types = malloc(sizeof(char *) * enter->num_types);
1233 if (!savedtypes.types) return EINA_FALSE;
1235 for (i = 0; i < enter->num_types; i++)
1237 savedtypes.types[i] = eina_stringshare_add(enter->types[i]);
1238 cnp_debug("Type is %s %p %p\n", enter->types[i],
1239 savedtypes.types[i],text_uri);
1240 if (savedtypes.types[i] == text_uri)
1242 /* Request it, so we know what it is */
1243 cnp_debug("Sending uri request\n");
1244 savedtypes.textreq = 1;
1245 savedtypes.pi = NULL; /* FIXME: Free? */
1246 ecore_x_selection_xdnd_request(enter->win, text_uri);
1250 /* FIXME: Find an object and make it current */
1255 _dnd_drop(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1257 struct _Ecore_X_Event_Xdnd_Drop *drop;
1261 Ecore_X_Window xwin;
1262 Elm_Selection_Data ddata;
1268 // check we still have something to drop
1269 if (!drops) return EINA_TRUE;
1271 /* Find any widget in our window; then work out geometry rel to our window */
1272 for (l = drops; l; l = l->next)
1275 xwin = (Ecore_X_Window)ecore_evas_window_get
1276 (ecore_evas_ecore_evas_get(evas_object_evas_get
1278 if (xwin == drop->win) break;
1280 /* didn't find a window */
1281 if (!l) return EINA_TRUE;
1283 /* Calculate real (widget relative) position */
1284 // - window position
1285 // - widget position
1286 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(dropable->obj));
1287 ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
1288 savedtypes.x = drop->position.x - x;
1289 savedtypes.y = drop->position.y - y;
1291 cnp_debug("Drop position is %d,%d\n", savedtypes.x, savedtypes.y);
1293 for (; l; l = l->next)
1296 evas_object_geometry_get(dropable->obj, &x, &y, &w, &h);
1297 if ((savedtypes.x >= x) && (savedtypes.y >= y) &&
1298 (savedtypes.x < x + w) && (savedtypes.y < y + h))
1302 if (!l) return EINA_TRUE; /* didn't find one */
1304 evas_object_geometry_get(dropable->obj, &x, &y, NULL, NULL);
1308 /* Find our type from the previous list */
1309 for (i = 0; i < CNP_N_ATOMS; i++)
1311 for (j = 0; j < savedtypes.ntypes; j++)
1313 if (!strcmp(savedtypes.types[j], atoms[i].name)) goto found;
1317 cnp_debug("Didn't find a target\n");
1321 cnp_debug("Found a target we'd like: %s\n", atoms[i].name);
1322 cnp_debug("0x%x\n",xwin);
1324 if (i == CNP_ATOM_text_urilist)
1326 cnp_debug("We found a URI... (%scached) %s\n",
1327 savedtypes.pi ? "" : "not ",
1328 savedtypes.pi->file);
1332 static const char *tagstring = "<item absize=240x180 href="
1333 "file://%s></item>";
1334 ddata.x = savedtypes.x;
1335 ddata.y = savedtypes.y;
1337 /* If it's markup that also supports images */
1338 if ((dropable->types & ELM_SEL_FORMAT_MARKUP) &&
1339 (dropable->types & ELM_SEL_FORMAT_IMAGE))
1342 ddata.format = ELM_SEL_FORMAT_MARKUP;
1343 pasteimage_provider_set(dropable->obj);
1345 pastedimages = eina_list_append(pastedimages, savedtypes.pi);
1346 len = strlen(tagstring) + strlen(savedtypes.pi->file);
1347 entrytag = alloca(len + 1);
1348 snprintf(entrytag, len + 1, tagstring, savedtypes.pi->file);
1349 ddata.data = entrytag;
1350 cnp_debug("Insert %s\n", (char *)ddata.data);
1351 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1352 ecore_x_dnd_send_finished();
1355 else if (dropable->types & ELM_SEL_FORMAT_IMAGE)
1357 cnp_debug("Doing image insert (%s)\n", savedtypes.pi->file);
1358 ddata.format = ELM_SEL_FORMAT_IMAGE;
1359 ddata.data = (char *)savedtypes.pi->file;
1360 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1361 ecore_x_dnd_send_finished();
1363 pasteimage_free(savedtypes.pi);
1364 savedtypes.pi = NULL;
1370 cnp_debug("Item doesn't support images... passing\n");
1371 pasteimage_free(savedtypes.pi);
1375 else if (savedtypes.textreq)
1377 /* Already asked: Pretend we asked now, and paste immediately when
1379 savedtypes.textreq = 0;
1380 ecore_x_dnd_send_finished();
1385 cnp_debug("doing a request then\n");
1386 selections[ELM_SEL_XDND].requestwidget = dropable->obj;
1387 selections[ELM_SEL_XDND].requestformat = ELM_SEL_FORMAT_MARKUP;
1388 selections[ELM_SEL_XDND].active = EINA_TRUE;
1390 ecore_x_selection_xdnd_request(xwin, atoms[i].name);
1395 _dnd_position(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1397 struct _Ecore_X_Event_Xdnd_Position *pos;
1398 Ecore_X_Rectangle rect;
1402 /* Need to send a status back */
1403 /* FIXME: Should check I can drop here */
1404 /* FIXME: Should highlight widget */
1405 rect.x = pos->position.x - 5;
1406 rect.y = pos->position.y - 5;
1409 ecore_x_dnd_send_status(EINA_TRUE, EINA_FALSE, rect, pos->action);
1415 * When dragging this is callback response from the destination.
1416 * The important thing we care about: Can we drop; thus update cursor
1420 _dnd_status(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1422 struct _Ecore_X_Event_Xdnd_Status *status = ev;
1424 if (!status) return EINA_TRUE;
1426 /* Only thing we care about: will accept */
1427 if (status->will_accept)
1429 cnp_debug("Will accept\n");
1432 { /* Won't accept */
1433 cnp_debug("Won't accept accept\n");
1439 * Add a widget as drop target.
1442 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
1445 Ecore_X_Window xwin;
1449 if (!obj) return EINA_FALSE;
1450 if (!_elm_cnp_init_count) _elm_cnp_init();
1452 /* Is this the first? */
1453 first = (!drops) ? 1 : 0;
1455 EINA_LIST_FOREACH(drops, item, drop)
1457 if (drop->obj == obj)
1459 /* Update: Not a new one */
1460 drop->dropcb = dropcb;
1461 drop->cbdata = cbdata;
1462 drop->types = format;
1467 /* Create new drop */
1468 drop = calloc(1, sizeof(Dropable));
1469 if (!drop) return EINA_FALSE;
1470 /* FIXME: Check for eina's deranged error method */
1471 drops = eina_list_append(drops, drop);
1473 if (!drops/* || or other error */)
1478 drop->dropcb = dropcb;
1479 drop->cbdata = cbdata;
1480 drop->types = format;
1483 evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL,
1484 /* I love C and varargs */
1485 (Evas_Object_Event_Cb)elm_drop_target_del,
1487 /* FIXME: Handle resizes */
1489 /* If not the first: We're done */
1490 if (!first) return EINA_TRUE;
1492 xwin = (Ecore_X_Window)ecore_evas_window_get
1493 (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
1495 ecore_x_dnd_aware_set(xwin, EINA_TRUE);
1497 cnp_debug("Adding drop target calls\n");
1498 handler_enter = ecore_event_handler_add(ECORE_X_EVENT_XDND_ENTER,
1500 handler_pos = ecore_event_handler_add(ECORE_X_EVENT_XDND_POSITION,
1501 _dnd_position, NULL);
1502 handler_drop = ecore_event_handler_add(ECORE_X_EVENT_XDND_DROP,
1509 elm_drop_target_del(Evas_Object *obj)
1511 Dropable *drop,*del;
1513 Ecore_X_Window xwin;
1516 EINA_LIST_FOREACH(drops, item, drop)
1518 if (drop->obj == obj)
1520 drops = eina_list_remove_list(drops, item);
1525 if (!del) return EINA_FALSE;
1527 evas_object_event_callback_del(obj, EVAS_CALLBACK_FREE,
1528 (Evas_Object_Event_Cb)elm_drop_target_del);
1530 /* If still drops there: All fine.. continue */
1531 if (drops) return EINA_TRUE;
1533 cnp_debug("Disabling DND\n");
1534 xwin = (Ecore_X_Window)ecore_evas_window_get
1535 (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
1536 ecore_x_dnd_aware_set(xwin, EINA_FALSE);
1538 ecore_event_handler_del(handler_pos);
1539 ecore_event_handler_del(handler_drop);
1540 ecore_event_handler_del(handler_enter);
1544 pasteimage_free(savedtypes.pi);
1545 savedtypes.pi = NULL;
1553 _drag_mouse_up(void *un __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *data __UNUSED__)
1555 evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_UP, _drag_mouse_up);
1559 dragdonecb(dragdonecb,selections[ELM_SEL_XDND].widget);
1564 evas_object_del(dragwin);
1570 _drag_move(void *data __UNUSED__, Ecore_X_Xdnd_Position *pos)
1572 evas_object_move(dragwin,
1573 pos->position.x - _dragx,
1574 pos->position.y - _dragy);
1579 elm_drag_start(Evas_Object *obj, Elm_Sel_Format format, const char *data, void (*dragdone) (void *data, Evas_Object *), void *donecbdata)
1581 Ecore_X_Window xwin;
1583 Elm_Sel_Type xdnd = ELM_SEL_XDND;
1585 int x, y, x2, y2, x3, y3;
1589 if (!_elm_cnp_init_count) _elm_cnp_init();
1591 xwin = elm_win_xwindow_get(obj);
1593 cnp_debug("starting drag...\n");
1595 ecore_x_dnd_type_set(xwin, "text/uri-list", 1);
1596 sel = selections + ELM_SEL_XDND;
1599 sel->format = format;
1600 sel->selbuf = data ? strdup(data) : NULL;
1601 dragdonecb = dragdone;
1602 dragdonedata = donecbdata;
1604 ecore_x_dnd_callback_pos_update_set(_drag_move, NULL);
1605 ecore_x_dnd_begin(xwin, (unsigned char *)&xdnd, sizeof(Elm_Sel_Type));
1606 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
1607 _drag_mouse_up, NULL);
1609 handler_status = ecore_event_handler_add(ECORE_X_EVENT_XDND_STATUS,
1612 dragwin = elm_win_add(NULL, "Elm Drag Object", ELM_WIN_UTILITY);
1613 elm_win_override_set(dragwin, 1);
1615 /* FIXME: Images only */
1616 icon = elm_icon_add(dragwin);
1617 elm_icon_file_set(icon, data + 7, NULL); /* 7!? "file://" */
1618 elm_win_resize_object_add(dragwin,icon);
1619 evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1620 evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, EVAS_HINT_FILL);
1622 /* Position subwindow appropriately */
1623 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(obj));
1624 ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
1625 evas_object_geometry_get(obj, &x2, &y2, &w, &h);
1628 evas_object_move(dragwin, x, y);
1629 evas_object_resize(icon, w, h);
1630 evas_object_resize(dragwin, w, h);
1632 evas_object_show(icon);
1633 evas_object_show(dragwin);
1635 evas_pointer_canvas_xy_get(evas_object_evas_get(obj), &x3, &y3);
1643 elm_cnp_tempfile_create(int size)
1646 const char *tmppath;
1649 info = malloc(sizeof(Tmp_Info));
1650 if (!info) return NULL;
1652 tmppath = getenv("TMP");
1653 if (!tmppath) tmppath = P_tmpdir;
1654 if (!tmppath) tmppath = "/tmp";
1655 len = snprintf(NULL, 0, "%s/%sXXXXXX", tmppath, "elmcnpitem-");
1662 info->filename = malloc(len);
1663 if (!info->filename)
1668 snprintf(info->filename,len,"%s/%sXXXXXX", tmppath, "elmcnpitem-");
1670 info->fd = mkstemp(info->filename);
1675 /* And before someone says anything see POSIX 1003.1-2008 page 400 */
1678 pid = (long)getpid();
1679 /* Use pid instead of /proc/self: That way if can be passed around */
1680 len = snprintf(NULL,0,"/proc/%li/fd/%i", pid, info->fd);
1685 snprintf(tmp,len, "/proc/%li/fd/%i", pid, info->fd);
1686 unlink(info->filename);
1687 free(info->filename);
1688 info->filename = tmp;
1693 cnp_debug("filename is %s\n", info->filename);
1696 /* Set map to NULL and return */
1703 if (ftruncate(info->fd, size))
1705 perror("ftruncate");
1711 info->map = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, info->fd, 0);
1712 if (info->map == MAP_FAILED)
1724 tmpinfo_free(Tmp_Info *info)
1726 if (!info) return 0;
1727 free(info->filename);
1733 /* Stubs for windows */
1735 elm_drag_start(Evas_Object *o, Elm_Sel_Format f, const char *d, void (*donecb)(void *, Evas_Object *),void *cbdata)
1741 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
1747 elm_drop_target_del(Evas_Object *o)
1753 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-2f0^-2{2(0W1st0 :*/