2 # include "elementary_config.h"
4 #include <Elementary.h>
11 #ifdef HAVE_ELEMENTARY_X
13 #define ARRAYINIT(foo) [foo] =
18 # define cnp_debug(x...) fprintf(stderr, __FILE__": " x)
20 # define cnp_debug(x...)
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;
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 *);
38 CNP_ATOM_LISTING_ATOMS = CNP_ATOM_ATOM,
40 CNP_ATOM_text_urilist,
41 CNP_ATOM_text_x_vcard,
52 CNP_ATOM_text_html_utf8,
57 CNP_ATOM_text_plain_utf8,
68 Evas_Object *requestwidget;
70 Elm_Sel_Format requestformat;
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);
76 Elm_Sel_Format format;
77 Ecore_X_Selection ecore_sel;
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;
114 Eina_Bool textreq: 1;
120 /* FIXME: Cache window */
121 Elm_Sel_Format types;
126 static Tmp_Info *elm_cnp_tempfile_create(int size);
127 static int tmpinfo_free(Tmp_Info *tmp);
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 static char *remove_tags(const char *p, int *len);
133 static char *mark_up(const char *start, int inlen, int *lenp);
135 static Eina_Bool targets_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
136 static Eina_Bool text_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
137 static Eina_Bool general_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
138 static Eina_Bool image_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
139 static Eina_Bool vcard_send(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
141 static int response_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *);
143 static int notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
144 static int notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
145 static int notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
146 static int notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
147 static int notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
148 static int vcard_receive(Cnp_Selection *sed, Ecore_X_Event_Selection_Notify *notify);
150 static Eina_Bool pasteimage_append(char *file, Evas_Object *entry);
152 #define _PARAGRAPH_SEPARATOR "\xE2\x80\xA9"
154 /* Optimisation: Turn this into a 256 byte table:
155 * then can lookup in one index, not N checks */
156 static const Escape escapes[] = {
157 { "<ps/>", _PARAGRAPH_SEPARATOR },
166 #define N_ESCAPES ((int)(sizeof(escapes) / sizeof(escapes[0])))
168 static Cnp_Atom atoms[CNP_N_ATOMS] = {
169 [CNP_ATOM_TARGETS] = {
171 ELM_SEL_FORMAT_TARGETS,
173 response_handler_targets,
174 notify_handler_targets,
178 "ATOM", // for opera browser
179 ELM_SEL_FORMAT_TARGETS,
181 response_handler_targets,
182 notify_handler_targets,
186 "application/x-elementary-markup",
187 ELM_SEL_FORMAT_MARKUP,
193 [CNP_ATOM_text_uri] = {
195 ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_IMAGE, /* Either images or entries */
201 [CNP_ATOM_text_urilist] = {
203 ELM_SEL_FORMAT_IMAGE,
209 [CNP_ATOM_text_x_vcard] = {
211 ELM_SEL_FORMAT_VCARD,
215 [CNP_ATOM_image_png] = {
217 ELM_SEL_FORMAT_IMAGE,
220 notify_handler_image,
223 [CNP_ATOM_image_jpeg] = {
225 ELM_SEL_FORMAT_IMAGE,
228 notify_handler_image,/* Raw image data is the same */
231 [CNP_ATOM_image_bmp] = {
233 ELM_SEL_FORMAT_IMAGE,
236 notify_handler_image,/* Raw image data is the same */
239 [CNP_ATOM_image_gif] = {
241 ELM_SEL_FORMAT_IMAGE,
244 notify_handler_image,/* Raw image data is the same */
247 [CNP_ATOM_image_tiff] = {
249 ELM_SEL_FORMAT_IMAGE,
252 notify_handler_image,/* Raw image data is the same */
255 [CNP_ATOM_image_svg] = {
257 ELM_SEL_FORMAT_IMAGE,
260 notify_handler_image,/* Raw image data is the same */
263 [CNP_ATOM_image_xpm] = {
265 ELM_SEL_FORMAT_IMAGE,
268 notify_handler_image,/* Raw image data is the same */
271 [CNP_ATOM_image_tga] = {
273 ELM_SEL_FORMAT_IMAGE,
276 notify_handler_image,/* Raw image data is the same */
279 [CNP_ATOM_image_ppm] = {
280 "image/x-portable-pixmap",
281 ELM_SEL_FORMAT_IMAGE,
284 notify_handler_image,/* Raw image data is the same */
287 [CNP_ATOM_text_html_utf8] = {
288 "text/html;charset=utf-8",
295 [CNP_ATOM_text_html] = {
300 notify_handler_html, /* No encoding: Webkit only */
303 [CNP_ATOM_UTF8STRING] = {
305 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
311 [CNP_ATOM_STRING] = {
313 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
321 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
327 [CNP_ATOM_text_plain_utf8] = {
328 "text/plain;charset=utf-8",
329 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
335 [CNP_ATOM_text_plain] = {
337 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
345 static Cnp_Selection selections[ELM_SEL_TYPE_MAX] = {
346 ARRAYINIT(ELM_SEL_TYPE_PRIMARY) {
348 .ecore_sel = ECORE_X_SELECTION_PRIMARY,
349 .set = ecore_x_selection_primary_set,
350 .clear = ecore_x_selection_primary_clear,
351 .request = ecore_x_selection_primary_request,
353 ARRAYINIT(ELM_SEL_TYPE_SECONDARY) {
354 .debug = "Secondary",
355 .ecore_sel = ECORE_X_SELECTION_SECONDARY,
356 .set = ecore_x_selection_secondary_set,
357 .clear = ecore_x_selection_secondary_clear,
358 .request = ecore_x_selection_secondary_request,
360 ARRAYINIT(ELM_SEL_TYPE_CLIPBOARD) {
361 .debug = "Clipboard",
362 .ecore_sel = ECORE_X_SELECTION_CLIPBOARD,
363 .set = ecore_x_selection_clipboard_set,
364 .clear = ecore_x_selection_clipboard_clear,
365 .request = ecore_x_selection_clipboard_request,
367 ARRAYINIT(ELM_SEL_TYPE_XDND) {
369 .ecore_sel = ECORE_X_SELECTION_XDND,
370 .request = ecore_x_selection_xdnd_request,
374 /* Data for DND in progress */
375 static Saved_Type savedtypes = { NULL, NULL, 0, 0, 0, EINA_FALSE };
377 static void (*dragdonecb) (void *data, Evas_Object *obj) = NULL;
378 static void *dragdonedata = NULL;
380 static int _elm_cnp_init_count = 0;
383 * Drag & Drop functions
386 /* FIXME: Way too many globals */
387 static Eina_List *drops = NULL;
388 static Evas_Object *dragwin = NULL;
389 static int _dragx = 0, _dragy = 0;
390 static Ecore_Event_Handler *handler_pos = NULL;
391 static Ecore_Event_Handler *handler_drop = NULL;
392 static Ecore_Event_Handler *handler_enter = NULL;
393 static Ecore_Event_Handler *handler_status = NULL;
397 /* Stringshared, so I can just compare pointers later */
398 static const char *text_uri;
401 elm_selection_selection_has_owner(void)
403 #ifdef HAVE_ELEMENTARY_X
404 return !!ecore_x_selection_owner_get(ECORE_X_ATOM_SELECTION_CLIPBOARD);
411 elm_cnp_selection_set(Elm_Sel_Type selection, Evas_Object *widget,
412 Elm_Sel_Format format, const void *selbuf, size_t buflen)
414 #ifdef HAVE_ELEMENTARY_X
415 Evas_Object *top = elm_widget_top_get(widget);
419 if (top) xwin = elm_win_xwindow_get(top);
420 else xwin = elm_win_xwindow_get(widget);
421 if ((!xwin) || (selection >= ELM_SEL_TYPE_MAX))
423 if (!_elm_cnp_init_count) _elm_cnp_init();
424 if ((!selbuf) && (format != ELM_SEL_FORMAT_IMAGE))
425 return elm_cnp_selection_clear(selection, widget);
427 sel = selections + selection;
429 sel->active = EINA_TRUE;
430 sel->widget = widget;
431 sel->set(xwin, &selection, sizeof(Elm_Sel_Type));
432 sel->format = format;
436 if (format == ELM_SEL_FORMAT_IMAGE)
438 sel->selbuf = malloc(buflen+1);
439 memcpy(sel->selbuf, selbuf, buflen);
442 sel->selbuf = strdup((char*)selbuf);
454 elm_cnp_selection_clear(Elm_Sel_Type selection, Evas_Object *widget)
456 #ifdef HAVE_ELEMENTARY_X
459 if ((unsigned int)selection >= (unsigned int)ELM_SEL_TYPE_MAX)
461 if (!_elm_cnp_init_count) _elm_cnp_init();
463 sel = selections + selection;
465 /* No longer this selection: Consider it gone! */
466 if ((!sel->active) || (sel->widget != widget)) return EINA_TRUE;
468 sel->active = EINA_FALSE;
484 elm_cnp_selection_get(Elm_Sel_Type selection, Elm_Sel_Format format,
485 Evas_Object *widget, Elm_Drop_Cb datacb, void *udata)
487 #ifdef HAVE_ELEMENTARY_X
491 if (selection >= ELM_SEL_TYPE_MAX)
493 if (!_elm_cnp_init_count) _elm_cnp_init();
495 sel = selections + selection;
496 top = elm_widget_top_get(widget);
497 if (!top) return EINA_FALSE;
499 sel->requestformat = format;
500 sel->requestwidget = widget;
501 sel->request(elm_win_xwindow_get(top), ECORE_X_SELECTION_TARGET_TARGETS);
502 sel->datacb = datacb;
511 #ifdef HAVE_ELEMENTARY_X
518 if (_elm_cnp_init_count++) return EINA_TRUE;
519 for (i = 0; i < CNP_N_ATOMS; i++)
521 atoms[i].atom = ecore_x_atom_get(atoms[i].name);
522 ecore_x_selection_converter_atom_add(atoms[i].atom,
526 ecore_event_handler_add(ECORE_X_EVENT_SELECTION_CLEAR, selection_clear, NULL);
527 ecore_event_handler_add(ECORE_X_EVENT_SELECTION_NOTIFY, selection_notify, NULL);
529 text_uri = eina_stringshare_add("text/uri-list");
534 selection_clear(void *udata __UNUSED__, int type __UNUSED__, void *event)
536 Ecore_X_Event_Selection_Clear *ev = event;
540 for (i = 0; i < ELM_SEL_TYPE_MAX; i++)
542 if (selections[i].ecore_sel == ev->selection) break;
544 cnp_debug("selection %d clear\n", i);
545 /* Not me... Don't care */
546 if (i == ELM_SEL_TYPE_MAX) return ECORE_CALLBACK_PASS_ON;
548 sel = selections + i;
549 sel->active = EINA_FALSE;
557 return ECORE_CALLBACK_PASS_ON;
562 * Response to a selection notify:
563 * - So we have asked for the selection list.
564 * - If it's the targets list, parse it, and fire of what we want,
565 * else it's the data we want.
568 selection_notify(void *udata __UNUSED__, int type __UNUSED__, void *event)
570 Ecore_X_Event_Selection_Notify *ev = event;
574 cnp_debug("selection notify callback: %d\n",ev->selection);
575 switch (ev->selection)
577 case ECORE_X_SELECTION_CLIPBOARD:
578 sel = selections + ELM_SEL_TYPE_CLIPBOARD;
580 case ECORE_X_SELECTION_PRIMARY:
581 sel = selections + ELM_SEL_TYPE_PRIMARY;
583 case ECORE_X_SELECTION_SECONDARY:
584 sel = selections + ELM_SEL_TYPE_SECONDARY;
586 case ECORE_X_SELECTION_XDND:
587 sel = selections + ELM_SEL_TYPE_XDND;
590 return ECORE_CALLBACK_PASS_ON;
592 cnp_debug("Target is %s\n", ev->target);
594 for (i = 0; i < CNP_N_ATOMS; i++)
596 if (!strcmp(ev->target, atoms[i].name))
600 cnp_debug("Found something: %s\n", atoms[i].name);
601 atoms[i].notify(sel, ev);
605 cnp_debug("Ignored: No handler!\n");
610 return ECORE_CALLBACK_PASS_ON;
613 static Elm_Sel_Format
614 _get_selection_type(void *data, int size)
616 if (size == sizeof(Elm_Sel_Type))
618 Cnp_Selection *sel = selections + *((int *)data);
620 (sel->format >= ELM_SEL_FORMAT_TARGETS) &&
621 (sel->format < ELM_SEL_FORMAT_MAX))
624 return ELM_SEL_FORMAT_NONE;
628 targets_converter(char *target __UNUSED__, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize)
633 Elm_Sel_Format seltype;
635 if (!data_ret) return EINA_FALSE;
637 if (_get_selection_type(data, size) == ELM_SEL_FORMAT_NONE)
639 /* TODO : fallback into precise type */
640 seltype = ELM_SEL_FORMAT_TEXT;
644 sel = selections + *((int *)data);
645 seltype = sel->format;
648 for (i = 0, count = 0; i < CNP_N_ATOMS ; i++)
650 if (seltype & atoms[i].formats) count++;
653 aret = malloc(sizeof(Ecore_X_Atom) * count);
654 for (i = 0, count = 0; i < CNP_N_ATOMS; i++)
656 if (seltype & atoms[i].formats) aret[count ++] = atoms[i].atom;
660 if (typesize) *typesize = 32 /* urk */;
661 if (ttype) *ttype = ECORE_X_ATOM_ATOM;
662 if (size_ret) *size_ret = count;
668 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__)
670 cnp_debug("Image converter called\n");
675 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__)
679 cnp_debug("Vcard send called\n");
681 sel = selections + *((int *)data);
683 if (data_ret) *data_ret = strdup(sel->selbuf);
684 if (size_ret) *size_ret = strlen(sel->selbuf);
690 is_uri_type_data(Cnp_Selection *sel __UNUSED__, Ecore_X_Event_Selection_Notify *notify)
692 Ecore_X_Selection_Data *data;
696 cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
697 if (data->content == ECORE_X_SELECTION_CONTENT_FILES) return EINA_TRUE;
698 p = (char *)data->data;
699 if (!p) return EINA_TRUE;
701 cnp_debug("Got %s\n", p);
702 if (strncmp(p, "file://", 7))
704 if (*p != '/') return EINA_FALSE;
711 * Callback to handle a targets response on a selection request:
712 * So pick the format we'd like; and then request it.
715 notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
717 Ecore_X_Selection_Data_Targets *targets;
718 Ecore_X_Atom *atomlist;
722 targets = notify->data;
723 atomlist = (Ecore_X_Atom *)(targets->data.data);
725 for (j = (CNP_ATOM_LISTING_ATOMS+1); j < CNP_N_ATOMS; j++)
727 cnp_debug("\t%s %d\n", atoms[j].name, atoms[j].atom);
728 if (!(atoms[j].formats & sel->requestformat)) continue;
729 for (i = 0; i < targets->data.length; i++)
731 if ((atoms[j].atom == atomlist[i]) && (atoms[j].notify))
733 if ((j == CNP_ATOM_text_uri) ||
734 (j == CNP_ATOM_text_urilist))
736 if(!is_uri_type_data(sel, notify)) continue;
738 cnp_debug("Atom %s matches\n",atoms[j].name);
744 cnp_debug("Couldn't find anything that matches\n");
745 return ECORE_CALLBACK_PASS_ON;
748 top = elm_widget_top_get(sel->requestwidget);
749 if (!top) top = sel->requestwidget;
750 cnp_debug("Sending request for %s\n", atoms[j].name);
751 sel->request(elm_win_xwindow_get(top), atoms[j].name);
753 return ECORE_CALLBACK_PASS_ON;
757 response_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
759 Ecore_X_Selection_Data_Targets *targets;
760 Ecore_X_Atom *atomlist;
764 targets = notify->data;
765 atomlist = (Ecore_X_Atom *)(targets->data.data);
767 for (j = (CNP_ATOM_LISTING_ATOMS+1); j < CNP_N_ATOMS; j++)
769 if (!(atoms[j].formats & sel->requestformat)) continue;
770 for (i = 0 ; i < targets->data.length ; i ++)
772 if ((atoms[j].atom == atomlist[i]) && (atoms[j].response))
774 /* Found a match: Use it */
779 cnp_debug("No matching type found\n");
783 top = elm_widget_top_get(sel->requestwidget);
786 sel->request(elm_win_xwindow_get(top), atoms[j].name);
792 notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
794 Ecore_X_Selection_Data *data;
801 Elm_Selection_Data ddata;
803 ddata.x = ddata.y = 0;
804 ddata.format = ELM_SEL_FORMAT_TEXT;
805 ddata.data = data->data;
806 ddata.len = data->length;
807 sel->datacb(sel->udata, sel->widget, &ddata);
811 cnp_debug("Notify handler text %d %d %p\n", data->format,data->length, data->data);
812 str = mark_up((char *)data->data, data->length, NULL);
813 cnp_debug("String is %s (from %s)\n", str, data->data);
814 _elm_entry_entry_paste(sel->requestwidget, str);
821 * So someone is pasting an image into my entry or widget...
824 notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
826 Ecore_X_Selection_Data *data;
827 Ecore_X_Selection_Data_Files *files;
831 cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
832 if (data->content == ECORE_X_SELECTION_CONTENT_FILES)
834 cnp_debug("got a files list\n");
835 files = notify->data;
836 if (files->num_files > 1)
838 /* Don't handle many items */
839 cnp_debug("more then one file: Bailing\n");
842 stripstr = p = strdup(files->files[0]);
846 stripstr = p = malloc(data->length * sizeof(char) + 1);
847 memcpy(stripstr, data->data, data->length);
848 stripstr[data->length] = 0;
853 cnp_debug("Couldn't find a file\n");
856 cnp_debug("Got %s\n",p);
857 if (strncmp(p, "file://", 7))
859 /* Try and continue if it looks sane */
868 p += strlen("file://");
871 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
872 if (savedtypes.textreq)
874 savedtypes.textreq = 0;
875 savedtypes.imgfile = strdup(p);
879 pasteimage_append(p, sel->requestwidget);
886 * Just receieved an vcard, either through cut and paste, or dnd.
889 vcard_receive(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
893 Ecore_X_Selection_Data *data;
896 cnp_debug("vcard receive\n");
898 if (sel == (selections + ELM_SEL_TYPE_XDND))
900 Elm_Selection_Data ddata;
902 cnp_debug("drag & drop\n");
903 /* FIXME: this needs to be generic: Used for all receives */
904 EINA_LIST_FOREACH(drops, l, dropable)
906 if (dropable->obj == sel->requestwidget) break;
910 cnp_debug("Unable to find drop object");
911 ecore_x_dnd_send_finished();
914 dropable = eina_list_data_get(l);
915 ddata.x = savedtypes.x;
916 ddata.y = savedtypes.y;
917 ddata.format = ELM_SEL_FORMAT_VCARD;
918 ddata.data = data->data;
919 ddata.len = data->length;
920 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
921 ecore_x_dnd_send_finished();
923 else if (sel->datacb)
925 Elm_Selection_Data ddata;
926 ddata.x = ddata.y = 0;
927 ddata.format = ELM_SEL_FORMAT_IMAGE;
928 ddata.data = data->data;
929 ddata.len = data->length;
930 sel->datacb(sel->udata, sel->widget, &ddata);
934 cnp_debug("Paste request\n");
943 notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
945 Ecore_X_Selection_Data *data;
948 cnp_debug("got a image file!\n");
951 cnp_debug("Size if %d\n", data->length);
955 Elm_Selection_Data ddata;
957 ddata.x = ddata.y = 0;
958 ddata.format = ELM_SEL_FORMAT_IMAGE;
959 ddata.data = data->data;
960 ddata.len = data->length;
961 sel->datacb(sel->udata, sel->widget, &ddata);
965 /* generate tmp name */
966 tmp = elm_cnp_tempfile_create(data->length);
967 memcpy(tmp->map, data->data, data->length);
968 munmap(tmp->map, data->length);
970 /* FIXME: Add to paste image data to clean up */
971 pasteimage_append(tmp->filename, sel->requestwidget);
979 * Warning: Generic text/html can';t handle it sanely.
980 * Firefox sends ucs2 (i think).
981 * chrome sends utf8... blerg
984 notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
986 Ecore_X_Selection_Data *data;
988 cnp_debug("Got some HTML: Checking encoding is useful\n");
993 Elm_Selection_Data ddata;
994 ddata.x = ddata.y = 0;
995 ddata.format = ELM_SEL_FORMAT_HTML;
996 ddata.data = data->data;
997 ddata.len = data->length;
998 sel->datacb(sel->udata, sel->widget, &ddata);
1002 char *stripstr = malloc(sizeof(char) * (data->length + 1));
1003 strncpy(stripstr, (char *)data->data, data->length);
1004 stripstr[data->length] = '\0';
1005 cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
1006 _elm_entry_entry_paste(sel->requestwidget, stripstr);
1013 text_converter(char *target __UNUSED__, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1017 cnp_debug("text converter\n");
1018 if (_get_selection_type(data, size) == ELM_SEL_FORMAT_NONE)
1022 *data_ret = malloc(size * sizeof(char) + 1);
1023 memcpy(*data_ret, data, size);
1024 ((char**)(data_ret))[0][size] = 0;
1026 if (size_ret) *size_ret = size;
1029 sel = selections + *((int *)data);
1030 if (!sel->active) return EINA_TRUE;
1032 if ((sel->format & ELM_SEL_FORMAT_MARKUP) ||
1033 (sel->format & ELM_SEL_FORMAT_HTML))
1035 *data_ret = remove_tags(sel->selbuf, size_ret);
1037 else if (sel->format & ELM_SEL_FORMAT_TEXT)
1039 *data_ret = strdup(sel->selbuf);
1040 *size_ret = strlen(sel->selbuf);
1042 else if (sel->format & ELM_SEL_FORMAT_IMAGE)
1044 cnp_debug("Image %s\n", evas_object_type_get(sel->widget));
1045 cnp_debug("Elm type: %s\n", elm_object_widget_type_get(sel->widget));
1046 evas_object_image_file_get(elm_photocam_internal_image_get(sel->widget),
1047 (const char **)data_ret, NULL);
1048 if (!*data_ret) *data_ret = strdup("No file");
1049 else *data_ret = strdup(*data_ret);
1050 *size_ret = strlen(*data_ret);
1056 general_converter(char *target __UNUSED__, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1058 if (_get_selection_type(data, size) == ELM_SEL_FORMAT_NONE)
1062 *data_ret = malloc(size * sizeof(char) + 1);
1063 memcpy(*data_ret, data, size);
1064 ((char**)(data_ret))[0][size] = 0;
1066 if (size_ret) *size_ret = size;
1070 Cnp_Selection *sel = selections + *((int *)data);
1071 if (data_ret) *data_ret = strdup(sel->selbuf);
1072 if (size_ret) *size_ret = strlen(sel->selbuf);
1078 pasteimage_append(char *file, Evas_Object *entry)
1082 static const char *tagstring = "<item absize=240x180 href=file://%s></item>";
1084 if ((!file) || (!entry)) return EINA_FALSE;
1086 len = strlen(tagstring)+strlen(file);
1088 entrytag = alloca(len + 1);
1089 snprintf(entrytag, len + 1, tagstring, file);
1090 _elm_entry_entry_paste(entry, entrytag);
1096 remove_tags(const char *p, int *len)
1100 if (!p) return NULL;
1102 q = malloc(strlen(p) + 1);
1103 if (!q) return NULL;
1108 Eina_Bool esc = EINA_TRUE;
1113 x = strchr(p + 3, '>');
1117 if (len) *len = strlen(ret);
1120 if (memcmp(p + 1, "br", 2) && memcmp(p + 1, "ps", 2))
1122 strncpy(q, p, x - p + 1);
1127 if (p[i] == '/') i--;
1138 strncpy(q, p, x - p + 1);
1146 strcpy(q, _PARAGRAPH_SEPARATOR);
1147 q += sizeof(_PARAGRAPH_SEPARATOR) - 1;
1152 for (i = 3 ; i < N_ESCAPES ; i++)
1154 if (strncmp(p, escapes[i].escape, strlen(escapes[i].escape)))
1156 p += strlen(escapes[i].escape);
1157 strcpy(q, escapes[i].value);
1158 q += strlen(escapes[i].value);
1161 if (i == N_ESCAPES) *q ++= '&';
1168 if (len) *len = q - ret;
1174 mark_up(const char *start, int inlen, int *lenp)
1179 const char *endp = NULL;
1181 if (!start) return NULL;
1182 if (inlen >= 0) endp = start + inlen;
1183 /* First pass: Count characters */
1184 for (l = 0, p = start; ((!endp) || (p < endp)) && (*p); p++)
1186 for (i = 0 ; i < N_ESCAPES ; i ++)
1188 if (*p == escapes[i].value[0])
1190 if (!strncmp(p, escapes[i].value, strlen(escapes[i].value)))
1191 l += strlen(escapes[i].escape);
1195 if (i == N_ESCAPES) l++;
1198 q = ret = malloc(l + 1);
1200 /* Second pass: Change characters */
1201 for (p = start; ((!endp) || (p < endp)) && (*p); )
1203 for (i = 0; i < N_ESCAPES; i++)
1205 if (*p == escapes[i].value[0])
1207 if (!strncmp(p, escapes[i].value, strlen(escapes[i].value)))
1209 strcpy(q, escapes[i].escape);
1210 q += strlen(escapes[i].escape);
1211 p += strlen(escapes[i].value);
1216 if (i == N_ESCAPES) *q++ = *p++;
1220 if (lenp) *lenp = l;
1226 _dnd_enter(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1228 Ecore_X_Event_Xdnd_Enter *enter = ev;
1232 if ((!enter) || (!enter->num_types) || (!enter->types)) return EINA_TRUE;
1234 cnp_debug("Types\n");
1235 savedtypes.ntypes = enter->num_types;
1236 if (savedtypes.types) free(savedtypes.types);
1237 savedtypes.types = malloc(sizeof(char *) * enter->num_types);
1238 if (!savedtypes.types) return EINA_FALSE;
1240 for (i = 0; i < enter->num_types; i++)
1242 savedtypes.types[i] = eina_stringshare_add(enter->types[i]);
1243 cnp_debug("Type is %s %p %p\n", enter->types[i],
1244 savedtypes.types[i],text_uri);
1245 if (savedtypes.types[i] == text_uri)
1247 /* Request it, so we know what it is */
1248 cnp_debug("Sending uri request\n");
1249 savedtypes.textreq = 1;
1250 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1251 savedtypes.imgfile = NULL;
1252 ecore_x_selection_xdnd_request(enter->win, text_uri);
1256 /* FIXME: Find an object and make it current */
1261 _dnd_drop(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1263 struct _Ecore_X_Event_Xdnd_Drop *drop;
1267 Ecore_X_Window xwin;
1268 Elm_Selection_Data ddata;
1274 // check we still have something to drop
1275 if (!drops) return EINA_TRUE;
1277 /* Find any widget in our window; then work out geometry rel to our window */
1278 for (l = drops; l; l = l->next)
1281 xwin = (Ecore_X_Window)ecore_evas_window_get
1282 (ecore_evas_ecore_evas_get(evas_object_evas_get
1284 if (xwin == drop->win) break;
1286 /* didn't find a window */
1287 if (!l) return EINA_TRUE;
1289 /* Calculate real (widget relative) position */
1290 // - window position
1291 // - widget position
1292 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(dropable->obj));
1293 ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
1294 savedtypes.x = drop->position.x - x;
1295 savedtypes.y = drop->position.y - y;
1297 cnp_debug("Drop position is %d,%d\n", savedtypes.x, savedtypes.y);
1299 for (; l; l = l->next)
1302 evas_object_geometry_get(dropable->obj, &x, &y, &w, &h);
1303 if ((savedtypes.x >= x) && (savedtypes.y >= y) &&
1304 (savedtypes.x < x + w) && (savedtypes.y < y + h))
1308 if (!l) return EINA_TRUE; /* didn't find one */
1310 evas_object_geometry_get(dropable->obj, &x, &y, NULL, NULL);
1314 /* Find our type from the previous list */
1315 for (i = 0; i < CNP_N_ATOMS; i++)
1317 for (j = 0; j < savedtypes.ntypes; j++)
1319 if (!strcmp(savedtypes.types[j], atoms[i].name)) goto found;
1323 cnp_debug("Didn't find a target\n");
1327 cnp_debug("Found a target we'd like: %s\n", atoms[i].name);
1328 cnp_debug("0x%x\n",xwin);
1330 if (i == CNP_ATOM_text_urilist)
1332 cnp_debug("We found a URI... (%scached) %s\n",
1333 savedtypes.imgfile ? "" : "not ",
1334 savedtypes.imgfile);
1335 if (savedtypes.imgfile)
1338 static const char *tagstring = "<item absize=240x180 href="
1339 "file://%s></item>";
1340 ddata.x = savedtypes.x;
1341 ddata.y = savedtypes.y;
1343 /* If it's markup that also supports images */
1344 if ((dropable->types & ELM_SEL_FORMAT_MARKUP) &&
1345 (dropable->types & ELM_SEL_FORMAT_IMAGE))
1348 ddata.format = ELM_SEL_FORMAT_MARKUP;
1350 len = strlen(tagstring) + strlen(savedtypes.imgfile);
1351 entrytag = alloca(len + 1);
1352 snprintf(entrytag, len + 1, tagstring, savedtypes.imgfile);
1353 ddata.data = entrytag;
1354 cnp_debug("Insert %s\n", (char *)ddata.data);
1355 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1356 ecore_x_dnd_send_finished();
1357 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1358 savedtypes.imgfile = NULL;
1362 else if (dropable->types & ELM_SEL_FORMAT_IMAGE)
1364 cnp_debug("Doing image insert (%s)\n", savedtypes.imgfile);
1365 ddata.format = ELM_SEL_FORMAT_IMAGE;
1366 ddata.data = (char *)savedtypes.imgfile;
1367 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1368 ecore_x_dnd_send_finished();
1369 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1370 savedtypes.imgfile = NULL;
1376 cnp_debug("Item doesn't support images... passing\n");
1380 else if (savedtypes.textreq)
1382 /* Already asked: Pretend we asked now, and paste immediately when
1384 savedtypes.textreq = 0;
1385 ecore_x_dnd_send_finished();
1390 cnp_debug("doing a request then\n");
1391 selections[ELM_SEL_TYPE_XDND].requestwidget = dropable->obj;
1392 selections[ELM_SEL_TYPE_XDND].requestformat = ELM_SEL_FORMAT_MARKUP;
1393 selections[ELM_SEL_TYPE_XDND].active = EINA_TRUE;
1395 ecore_x_selection_xdnd_request(xwin, atoms[i].name);
1400 _dnd_position(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1402 struct _Ecore_X_Event_Xdnd_Position *pos;
1403 Ecore_X_Rectangle rect;
1407 /* Need to send a status back */
1408 /* FIXME: Should check I can drop here */
1409 /* FIXME: Should highlight widget */
1410 rect.x = pos->position.x - 5;
1411 rect.y = pos->position.y - 5;
1414 ecore_x_dnd_send_status(EINA_TRUE, EINA_FALSE, rect, pos->action);
1420 * When dragging this is callback response from the destination.
1421 * The important thing we care about: Can we drop; thus update cursor
1425 _dnd_status(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1427 struct _Ecore_X_Event_Xdnd_Status *status = ev;
1429 if (!status) return EINA_TRUE;
1431 /* Only thing we care about: will accept */
1432 if (status->will_accept)
1434 cnp_debug("Will accept\n");
1437 { /* Won't accept */
1438 cnp_debug("Won't accept accept\n");
1444 * Add a widget as drop target.
1447 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
1450 Ecore_X_Window xwin;
1455 if (!obj) return EINA_FALSE;
1456 top = elm_widget_top_get(obj);
1457 if (!top || !elm_win_xwindow_get(top)) return EINA_FALSE;
1458 if (!_elm_cnp_init_count) _elm_cnp_init();
1460 /* Is this the first? */
1461 first = (!drops) ? 1 : 0;
1463 EINA_LIST_FOREACH(drops, item, drop)
1465 if (drop->obj == obj)
1467 /* Update: Not a new one */
1468 drop->dropcb = dropcb;
1469 drop->cbdata = cbdata;
1470 drop->types = format;
1475 /* Create new drop */
1476 drop = calloc(1, sizeof(Dropable));
1477 if (!drop) return EINA_FALSE;
1478 /* FIXME: Check for eina's deranged error method */
1479 drops = eina_list_append(drops, drop);
1481 if (!drops/* || or other error */)
1486 drop->dropcb = dropcb;
1487 drop->cbdata = cbdata;
1488 drop->types = format;
1491 evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL,
1492 /* I love C and varargs */
1493 (Evas_Object_Event_Cb)elm_drop_target_del,
1495 /* FIXME: Handle resizes */
1497 /* If not the first: We're done */
1498 if (!first) return EINA_TRUE;
1500 xwin = (Ecore_X_Window)ecore_evas_window_get
1501 (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
1503 ecore_x_dnd_aware_set(xwin, EINA_TRUE);
1505 cnp_debug("Adding drop target calls\n");
1506 handler_enter = ecore_event_handler_add(ECORE_X_EVENT_XDND_ENTER,
1508 handler_pos = ecore_event_handler_add(ECORE_X_EVENT_XDND_POSITION,
1509 _dnd_position, NULL);
1510 handler_drop = ecore_event_handler_add(ECORE_X_EVENT_XDND_DROP,
1517 elm_drop_target_del(Evas_Object *obj)
1519 Dropable *drop,*del;
1521 Ecore_X_Window xwin;
1524 EINA_LIST_FOREACH(drops, item, drop)
1526 if (drop->obj == obj)
1528 drops = eina_list_remove_list(drops, item);
1533 if (!del) return EINA_FALSE;
1535 evas_object_event_callback_del(obj, EVAS_CALLBACK_FREE,
1536 (Evas_Object_Event_Cb)elm_drop_target_del);
1538 /* If still drops there: All fine.. continue */
1539 if (drops) return EINA_TRUE;
1541 cnp_debug("Disabling DND\n");
1542 xwin = (Ecore_X_Window)ecore_evas_window_get
1543 (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
1544 ecore_x_dnd_aware_set(xwin, EINA_FALSE);
1546 ecore_event_handler_del(handler_pos);
1547 ecore_event_handler_del(handler_drop);
1548 ecore_event_handler_del(handler_enter);
1550 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1551 savedtypes.imgfile = NULL;
1558 _drag_mouse_up(void *un __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *data __UNUSED__)
1560 evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_UP, _drag_mouse_up);
1564 dragdonecb(dragdonecb,selections[ELM_SEL_TYPE_XDND].widget);
1569 evas_object_del(dragwin);
1575 _drag_move(void *data __UNUSED__, Ecore_X_Xdnd_Position *pos)
1577 evas_object_move(dragwin,
1578 pos->position.x - _dragx,
1579 pos->position.y - _dragy);
1584 elm_drag_start(Evas_Object *obj, Elm_Sel_Format format, const char *data, void (*dragdone) (void *data, Evas_Object *), void *donecbdata)
1586 Ecore_X_Window xwin;
1588 Elm_Sel_Type xdnd = ELM_SEL_TYPE_XDND;
1590 int x, y, x2, y2, x3, y3;
1594 if (!_elm_cnp_init_count) _elm_cnp_init();
1596 xwin = elm_win_xwindow_get(obj);
1598 cnp_debug("starting drag...\n");
1600 ecore_x_dnd_type_set(xwin, "text/uri-list", 1);
1601 sel = selections + ELM_SEL_TYPE_XDND;
1604 sel->format = format;
1605 sel->selbuf = data ? strdup(data) : NULL;
1606 dragdonecb = dragdone;
1607 dragdonedata = donecbdata;
1609 ecore_x_dnd_callback_pos_update_set(_drag_move, NULL);
1610 ecore_x_dnd_begin(xwin, (unsigned char *)&xdnd, sizeof(Elm_Sel_Type));
1611 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
1612 _drag_mouse_up, NULL);
1614 handler_status = ecore_event_handler_add(ECORE_X_EVENT_XDND_STATUS,
1617 dragwin = elm_win_add(NULL, "Elm Drag Object", ELM_WIN_UTILITY);
1618 elm_win_override_set(dragwin, 1);
1620 /* FIXME: Images only */
1621 icon = elm_icon_add(dragwin);
1622 elm_icon_file_set(icon, data + 7, NULL); /* 7!? "file://" */
1623 elm_win_resize_object_add(dragwin,icon);
1624 evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1625 evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, EVAS_HINT_FILL);
1627 /* Position subwindow appropriately */
1628 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(obj));
1629 ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
1630 evas_object_geometry_get(obj, &x2, &y2, &w, &h);
1633 evas_object_move(dragwin, x, y);
1634 evas_object_resize(icon, w, h);
1635 evas_object_resize(dragwin, w, h);
1637 evas_object_show(icon);
1638 evas_object_show(dragwin);
1640 evas_pointer_canvas_xy_get(evas_object_evas_get(obj), &x3, &y3);
1648 elm_cnp_tempfile_create(int size)
1652 const char *tmppath;
1655 info = malloc(sizeof(Tmp_Info));
1656 if (!info) return NULL;
1658 tmppath = getenv("TMP");
1659 if (!tmppath) tmppath = P_tmpdir;
1660 len = snprintf(NULL, 0, "%s/%sXXXXXX", tmppath, "elmcnpitem-");
1667 info->filename = malloc(len);
1668 if (!info->filename)
1673 snprintf(info->filename,len,"%s/%sXXXXXX", tmppath, "elmcnpitem-");
1675 info->fd = mkstemp(info->filename);
1680 /* And before someone says anything see POSIX 1003.1-2008 page 400 */
1683 pid = (long)getpid();
1684 /* Use pid instead of /proc/self: That way if can be passed around */
1685 len = snprintf(NULL,0,"/proc/%li/fd/%i", pid, info->fd);
1690 snprintf(tmp,len, "/proc/%li/fd/%i", pid, info->fd);
1691 unlink(info->filename);
1692 free(info->filename);
1693 info->filename = tmp;
1698 cnp_debug("filename is %s\n", info->filename);
1701 /* Set map to NULL and return */
1708 if (ftruncate(info->fd, size))
1710 perror("ftruncate");
1716 eina_mmap_safety_enabled_set(EINA_TRUE);
1718 info->map = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, info->fd, 0);
1719 if (info->map == MAP_FAILED)
1735 tmpinfo_free(Tmp_Info *info)
1737 if (!info) return 0;
1738 free(info->filename);
1744 /* Stubs for windows */
1746 elm_drag_start(Evas_Object *o, Elm_Sel_Format f, const char *d, void (*donecb)(void *, Evas_Object *),void *cbdata)
1752 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
1758 elm_drop_target_del(Evas_Object *o)
1764 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-3f0^-2{2(0W1st0 :*/