2 # include "elementary_config.h"
4 #include <Elementary.h>
9 #ifdef HAVE_ELEMENTARY_X
11 #define ARRAYINIT(foo) [foo] =
16 # define cnp_debug(x...) fprintf(stderr, __FILE__": " x)
18 # define cnp_debug(x...)
21 typedef struct _Cnp_Selection Cnp_Selection;
22 typedef struct _Escape Escape;
23 typedef struct _Tmp_Info Tmp_Info;
24 typedef struct _Cnp_Atom Cnp_Atom;
25 typedef struct _Saved_Type Saved_Type;
26 typedef struct _Dropable Dropable;
28 typedef Eina_Bool (*Converter_Fn_Cb) (char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
29 typedef int (*Response_Handler_Cb) (Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *);
30 typedef int (*Notify_Handler_Cb) (Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *);
36 CNP_ATOM_text_urilist,
37 CNP_ATOM_text_x_vcard,
48 CNP_ATOM_text_html_utf8,
53 CNP_ATOM_text_plain_utf8,
64 Evas_Object *requestwidget;
66 Elm_Sel_Format requestformat;
68 Eina_Bool (*set) (Ecore_X_Window, const void *data, int size);
69 Eina_Bool (*clear) (void);
70 void (*request) (Ecore_X_Window, const char *target);
72 Elm_Sel_Format format;
73 Ecore_X_Selection ecore_sel;
95 Elm_Sel_Format formats;
96 /* Called by ecore to do conversion */
97 Converter_Fn_Cb converter;
98 Response_Handler_Cb response;
99 Notify_Handler_Cb notify;
110 Eina_Bool textreq: 1;
116 /* FIXME: Cache window */
117 Elm_Sel_Format types;
122 static Tmp_Info *elm_cnp_tempfile_create(int size);
123 static int tmpinfo_free(Tmp_Info *tmp);
125 static Eina_Bool _elm_cnp_init(void);
126 static Eina_Bool selection_clear(void *udata __UNUSED__, int type, void *event);
127 static Eina_Bool selection_notify(void *udata __UNUSED__, int type, void *event);
128 static char *remove_tags(const char *p, int *len);
129 static char *mark_up(const char *start, int inlen, int *lenp);
131 static Eina_Bool targets_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
132 static Eina_Bool text_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
133 static Eina_Bool html_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
134 static Eina_Bool edje_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
135 static Eina_Bool uri_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
136 static Eina_Bool image_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
137 static Eina_Bool vcard_send(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
139 static int response_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *);
141 static int notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
142 static int notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
143 static int notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
144 static int notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
145 static int notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
146 static int vcard_receive(Cnp_Selection *sed, Ecore_X_Event_Selection_Notify *notify);
148 static Eina_Bool pasteimage_append(char *file, Evas_Object *entry);
150 /* Optimisation: Turn this into a 256 byte table:
151 * then can lookup in one index, not N checks */
152 static const Escape escapes[] = {
161 #define N_ESCAPES ((int)(sizeof(escapes) / sizeof(escapes[0])))
163 static Cnp_Atom atoms[CNP_N_ATOMS] = {
164 [CNP_ATOM_TARGETS] = {
166 (Elm_Sel_Format) -1, // everything
168 response_handler_targets,
169 notify_handler_targets,
173 "application/x-elementary-markup",
174 ELM_SEL_FORMAT_MARKUP,
180 [CNP_ATOM_text_uri] = {
182 ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_IMAGE, /* Either images or entries */
188 [CNP_ATOM_text_urilist] = {
190 ELM_SEL_FORMAT_IMAGE,
196 [CNP_ATOM_text_x_vcard] = {
198 ELM_SEL_FORMAT_VCARD,
202 [CNP_ATOM_image_png] = {
204 ELM_SEL_FORMAT_IMAGE,
207 notify_handler_image,
210 [CNP_ATOM_image_jpeg] = {
212 ELM_SEL_FORMAT_IMAGE,
215 notify_handler_image,/* Raw image data is the same */
218 [CNP_ATOM_image_bmp] = {
220 ELM_SEL_FORMAT_IMAGE,
223 notify_handler_image,/* Raw image data is the same */
226 [CNP_ATOM_image_gif] = {
228 ELM_SEL_FORMAT_IMAGE,
231 notify_handler_image,/* Raw image data is the same */
234 [CNP_ATOM_image_tiff] = {
236 ELM_SEL_FORMAT_IMAGE,
239 notify_handler_image,/* Raw image data is the same */
242 [CNP_ATOM_image_svg] = {
244 ELM_SEL_FORMAT_IMAGE,
247 notify_handler_image,/* Raw image data is the same */
250 [CNP_ATOM_image_xpm] = {
252 ELM_SEL_FORMAT_IMAGE,
255 notify_handler_image,/* Raw image data is the same */
258 [CNP_ATOM_image_tga] = {
260 ELM_SEL_FORMAT_IMAGE,
263 notify_handler_image,/* Raw image data is the same */
266 [CNP_ATOM_image_ppm] = {
267 "image/x-portable-pixmap",
268 ELM_SEL_FORMAT_IMAGE,
271 notify_handler_image,/* Raw image data is the same */
274 [CNP_ATOM_text_html_utf8] = {
275 "text/html;charset=utf-8",
282 [CNP_ATOM_text_html] = {
287 notify_handler_html, /* No encoding: Webkit only */
290 [CNP_ATOM_UTF8STRING] = {
292 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
298 [CNP_ATOM_STRING] = {
300 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
308 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
314 [CNP_ATOM_text_plain_utf8] = {
315 "text/plain;charset=utf-8",
316 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
322 [CNP_ATOM_text_plain] = {
324 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
332 static Cnp_Selection selections[ELM_SEL_MAX] = {
333 ARRAYINIT(ELM_SEL_PRIMARY) {
335 .ecore_sel = ECORE_X_SELECTION_PRIMARY,
336 .set = ecore_x_selection_primary_set,
337 .clear = ecore_x_selection_primary_clear,
338 .request = ecore_x_selection_primary_request,
340 ARRAYINIT(ELM_SEL_SECONDARY) {
341 .debug = "Secondary",
342 .ecore_sel = ECORE_X_SELECTION_SECONDARY,
343 .set = ecore_x_selection_secondary_set,
344 .clear = ecore_x_selection_secondary_clear,
345 .request = ecore_x_selection_secondary_request,
347 ARRAYINIT(ELM_SEL_CLIPBOARD) {
348 .debug = "Clipboard",
349 .ecore_sel = ECORE_X_SELECTION_CLIPBOARD,
350 .set = ecore_x_selection_clipboard_set,
351 .clear = ecore_x_selection_clipboard_clear,
352 .request = ecore_x_selection_clipboard_request,
354 ARRAYINIT(ELM_SEL_XDND) {
356 .ecore_sel = ECORE_X_SELECTION_XDND,
357 .request = ecore_x_selection_xdnd_request,
361 /* Data for DND in progress */
362 static Saved_Type savedtypes = { NULL, NULL, 0, 0, 0, EINA_FALSE };
364 static void (*dragdonecb) (void *data, Evas_Object *obj) = NULL;
365 static void *dragdonedata = NULL;
367 static int _elm_cnp_init_count = 0;
368 /* FIXME: who left this out of XAtoms.h */
369 static Ecore_X_Atom clipboard_atom;
372 * Drag & Drop functions
375 /* FIXME: Way too many globals */
376 static Eina_List *drops = NULL;
377 static Evas_Object *dragwin = NULL;
378 static int _dragx = 0, _dragy = 0;
379 static Ecore_Event_Handler *handler_pos = NULL;
380 static Ecore_Event_Handler *handler_drop = NULL;
381 static Ecore_Event_Handler *handler_enter = NULL;
382 static Ecore_Event_Handler *handler_status = NULL;
386 /* Stringshared, so I can just compare pointers later */
387 static const char *text_uri;
390 elm_selection_set(Elm_Sel_Type selection, Evas_Object *widget, Elm_Sel_Format format, const char *selbuf)
392 #ifdef HAVE_ELEMENTARY_X
393 Evas_Object *top = elm_widget_top_get(widget);
397 if (top) xwin = elm_win_xwindow_get(top);
398 else xwin = elm_win_xwindow_get(widget);
399 if (!xwin) return EINA_FALSE;
400 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
401 if (!_elm_cnp_init_count) _elm_cnp_init();
402 if ((!selbuf) && (format != ELM_SEL_FORMAT_IMAGE))
403 return elm_selection_clear(selection, widget);
405 sel = selections + selection;
408 sel->widget = widget;
410 sel->set(xwin, &selection, sizeof(Elm_Sel_Type));
411 sel->format = format;
412 sel->selbuf = selbuf ? strdup(selbuf) : NULL;
421 elm_selection_clear(Elm_Sel_Type selection, Evas_Object *widget)
423 #ifdef HAVE_ELEMENTARY_X
426 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
427 if (!_elm_cnp_init_count) _elm_cnp_init();
429 sel = selections + selection;
431 /* No longer this selection: Consider it gone! */
432 if ((!sel->active) || (sel->widget != widget)) return EINA_TRUE;
445 elm_selection_get(Elm_Sel_Type selection, Elm_Sel_Format format,
446 Evas_Object *widget, Elm_Drop_Cb datacb, void *udata)
448 #ifdef HAVE_ELEMENTARY_X
452 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
453 if (!_elm_cnp_init_count) _elm_cnp_init();
455 sel = selections + selection;
456 top = elm_widget_top_get(widget);
457 if (!top) return EINA_FALSE;
459 sel->requestformat = format;
460 sel->requestwidget = widget;
461 sel->request(elm_win_xwindow_get(top), ECORE_X_SELECTION_TARGET_TARGETS);
462 sel->datacb = datacb;
471 #ifdef HAVE_ELEMENTARY_X
478 if (_elm_cnp_init_count++) return EINA_TRUE;
479 for (i = 0; i < CNP_N_ATOMS; i++)
481 atoms[i].atom = ecore_x_atom_get(atoms[i].name);
482 ecore_x_selection_converter_atom_add(atoms[i].atom,
485 clipboard_atom = ecore_x_atom_get("CLIPBOARD");
487 ecore_event_handler_add(ECORE_X_EVENT_SELECTION_CLEAR, selection_clear, NULL);
488 ecore_event_handler_add(ECORE_X_EVENT_SELECTION_NOTIFY, selection_notify, NULL);
490 text_uri = eina_stringshare_add("text/uri-list");
495 selection_clear(void *udata __UNUSED__, int type __UNUSED__, void *event)
497 Ecore_X_Event_Selection_Clear *ev = event;
501 for (i = 0; i < ELM_SEL_MAX; i++)
503 if (selections[i].ecore_sel == ev->selection) break;
505 cnp_debug("selection %d clear\n", i);
506 /* Not me... Don't care */
507 if (i == ELM_SEL_MAX) return ECORE_CALLBACK_PASS_ON;
509 sel = selections + i;
514 return ECORE_CALLBACK_PASS_ON;
519 * Response to a selection notify:
520 * - So we have asked for the selection list.
521 * - If it's the targets list, parse it, and fire of what we want,
522 * else it's the data we want.
525 selection_notify(void *udata __UNUSED__, int type __UNUSED__, void *event)
527 Ecore_X_Event_Selection_Notify *ev = event;
531 cnp_debug("selection notify callback: %d\n",ev->selection);
532 switch (ev->selection)
534 case ECORE_X_SELECTION_CLIPBOARD:
535 sel = selections + ELM_SEL_CLIPBOARD;
537 case ECORE_X_SELECTION_PRIMARY:
538 sel = selections + ELM_SEL_PRIMARY;
540 case ECORE_X_SELECTION_SECONDARY:
541 sel = selections + ELM_SEL_SECONDARY;
543 case ECORE_X_SELECTION_XDND:
544 sel = selections + ELM_SEL_XDND;
547 return ECORE_CALLBACK_PASS_ON;
549 cnp_debug("Target is %s\n", ev->target);
551 for (i = 0; i < CNP_N_ATOMS; i++)
553 if (!strcmp(ev->target, atoms[i].name))
557 cnp_debug("Found something: %s\n", atoms[i].name);
558 atoms[i].notify(sel, ev);
562 cnp_debug("Ignored: No handler!\n");
567 return ECORE_CALLBACK_PASS_ON;
573 targets_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize)
579 if (!data_ret) return EINA_FALSE;
581 sel = selections + *((int *)data);
583 for (i = 0, count = 0; i < CNP_N_ATOMS ; i++)
585 if (sel->format & atoms[i].formats) count++;
588 aret = malloc(sizeof(Ecore_X_Atom) * count);
589 for (i = 0, count = 0; i < CNP_N_ATOMS; i++)
591 if (sel->format & atoms[i].formats) aret[count ++] = atoms[i].atom;
595 if (typesize) *typesize = 32 /* urk */;
596 if (ttype) *ttype = ECORE_X_ATOM_ATOM;
597 if (size_ret) *size_ret = count;
603 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__)
605 cnp_debug("Image converter called\n");
610 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__)
614 cnp_debug("Vcard send called\n");
616 sel = selections + *((int *)data);
618 if (data_ret) *data_ret = strdup(sel->selbuf);
619 if (size_ret) *size_ret = strlen(sel->selbuf);
624 * Callback to handle a targets response on a selection request:
625 * So pick the format we'd like; and then request it.
628 notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
630 Ecore_X_Selection_Data_Targets *targets;
631 Ecore_X_Atom *atomlist;
635 targets = notify->data;
636 atomlist = (Ecore_X_Atom *)(targets->data.data);
638 for (j = 1; j < CNP_N_ATOMS; j++)
640 cnp_debug("\t%s %d\n", atoms[j].name, atoms[j].atom);
641 if (!(atoms[j].formats & sel->requestformat)) continue;
642 for (i = 0; i < targets->data.length; i++)
644 if ((atoms[j].atom == atomlist[i]) && (atoms[j].notify))
646 cnp_debug("Atom %s matches\n",atoms[j].name);
652 cnp_debug("Couldn't find anything that matches\n");
653 return ECORE_CALLBACK_PASS_ON;
656 top = elm_widget_top_get(sel->requestwidget);
657 if (!top) top = sel->requestwidget;
658 cnp_debug("Sending request for %s\n", atoms[j].name);
659 sel->request(elm_win_xwindow_get(top), atoms[j].name);
661 return ECORE_CALLBACK_PASS_ON;
665 response_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
667 Ecore_X_Selection_Data_Targets *targets;
668 Ecore_X_Atom *atomlist;
672 targets = notify->data;
673 atomlist = (Ecore_X_Atom *)(targets->data.data);
675 /* Start from 1: Skip targets */
676 for (j = 1 ; j < CNP_N_ATOMS ; j ++)
678 if (!(atoms[j].formats & sel->requestformat)) continue;
679 for (i = 0 ; i < targets->data.length ; i ++)
681 if ((atoms[j].atom == atomlist[i]) && (atoms[j].response))
683 /* Found a match: Use it */
689 if (j == CNP_N_ATOMS)
691 cnp_debug("No matching type found\n");
695 top = elm_widget_top_get(sel->requestwidget);
698 sel->request(elm_win_xwindow_get(top), atoms[j].name);
704 notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
706 Ecore_X_Selection_Data *data;
713 Elm_Selection_Data ddata;
715 ddata.x = ddata.y = 0;
716 ddata.format = ELM_SEL_FORMAT_TEXT;
717 ddata.data = data->data;
718 ddata.len = data->length;
719 sel->datacb(sel->udata, sel->widget, &ddata);
723 cnp_debug("Notify handler text %d %d %p\n", data->format,data->length, data->data);
724 str = mark_up((char *)data->data, data->length, NULL);
725 cnp_debug("String is %s (from %s)\n", str, data->data);
726 elm_entry_entry_insert(sel->requestwidget, str);
733 * So someone is pasting an image into my entry or widget...
736 notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
738 Ecore_X_Selection_Data *data;
739 Ecore_X_Selection_Data_Files *files;
743 cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
744 if (data->content == ECORE_X_SELECTION_CONTENT_FILES)
746 cnp_debug("got a files list\n");
747 files = notify->data;
748 if (files->num_files > 1)
750 /* Don't handle many items */
751 cnp_debug("more then one file: Bailing\n");
758 p = (char *)data->data;
763 cnp_debug("Couldn't find a file\n");
766 cnp_debug("Got %s\n",p);
767 if (strncmp(p, "file://", 7))
769 /* Try and continue if it looks sane */
770 if (*p != '/') return 0;
774 p += strlen("file://");
777 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
778 if (savedtypes.textreq)
780 savedtypes.textreq = 0;
781 savedtypes.imgfile = strdup(p);
785 pasteimage_append(p, sel->requestwidget);
791 * Just receieved an vcard, either through cut and paste, or dnd.
794 vcard_receive(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
798 Ecore_X_Selection_Data *data;
801 cnp_debug("vcard receive\n");
803 if (sel == (selections + ELM_SEL_XDND))
805 Elm_Selection_Data ddata;
807 cnp_debug("drag & drop\n");
808 /* FIXME: this needs to be generic: Used for all receives */
809 EINA_LIST_FOREACH(drops, l, dropable)
811 if (dropable->obj == sel->requestwidget) break;
815 cnp_debug("Unable to find drop object");
816 ecore_x_dnd_send_finished();
819 dropable = eina_list_data_get(l);
820 ddata.x = savedtypes.x;
821 ddata.y = savedtypes.y;
822 ddata.format = ELM_SEL_FORMAT_VCARD;
823 ddata.data = data->data;
824 ddata.len = data->length;
825 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
826 ecore_x_dnd_send_finished();
828 else if (sel->datacb)
830 Elm_Selection_Data ddata;
831 ddata.x = ddata.y = 0;
832 ddata.format = ELM_SEL_FORMAT_IMAGE;
833 ddata.data = data->data;
834 ddata.len = data->length;
835 sel->datacb(sel->udata, sel->widget, &ddata);
839 cnp_debug("Paste request\n");
848 notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
850 Ecore_X_Selection_Data *data;
853 cnp_debug("got a png (or a jpeg)!\n");
856 cnp_debug("Size if %d\n", data->length);
860 Elm_Selection_Data ddata;
862 ddata.x = ddata.y = 0;
863 ddata.format = ELM_SEL_FORMAT_IMAGE;
864 ddata.data = data->data;
865 ddata.len = data->length;
866 sel->datacb(sel->udata, sel->widget, &ddata);
870 /* generate tmp name */
871 tmp = elm_cnp_tempfile_create(data->length);
872 memcpy(tmp->map, data->data, data->length);
873 munmap(tmp->map,data->length);
875 /* FIXME: Add to paste image data to clean up */
876 pasteimage_append(tmp->filename, sel->requestwidget);
884 * Warning: Generic text/html can';t handle it sanely.
885 * Firefox sends ucs2 (i think).
886 * chrome sends utf8... blerg
889 notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
891 Ecore_X_Selection_Data *data;
893 cnp_debug("Got some HTML: Checking encoding is useful\n");
898 Elm_Selection_Data ddata;
899 ddata.x = ddata.y = 0;
900 ddata.format = ELM_SEL_FORMAT_HTML;
901 ddata.data = data->data;
902 ddata.len = data->length;
903 sel->datacb(sel->udata, sel->widget, &ddata);
907 char *stripstr = NULL;
908 stripstr = malloc(sizeof(char) * (data->length + 1));
909 strncpy(stripstr, (char *)data->data, data->length);
910 stripstr[data->length] = '\0';
911 cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
912 elm_entry_entry_insert(sel->requestwidget, stripstr);
919 text_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
923 cnp_debug("text converter\n");
924 sel = selections + *((int *)data);
925 if (!sel->active) return EINA_TRUE;
927 if ((sel->format & ELM_SEL_FORMAT_MARKUP) ||
928 (sel->format & ELM_SEL_FORMAT_HTML))
930 *data_ret = remove_tags(sel->selbuf, size_ret);
932 else if (sel->format & ELM_SEL_FORMAT_TEXT)
934 *data_ret = strdup(sel->selbuf);
935 *size_ret = strlen(sel->selbuf);
937 else if (sel->format & ELM_SEL_FORMAT_IMAGE)
939 cnp_debug("Image %s\n", evas_object_type_get(sel->widget));
940 cnp_debug("Elm type: %s\n", elm_object_widget_type_get(sel->widget));
941 evas_object_image_file_get(elm_photocam_internal_image_get(sel->widget), (const char **)data_ret, NULL);
942 if (!*data_ret) *data_ret = strdup("No file");
943 else *data_ret = strdup(*data_ret);
944 *size_ret = strlen(*data_ret);
950 edje_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
954 sel = selections + *((int *)data);
955 if (data_ret) *data_ret = strdup(sel->selbuf);
956 if (size_ret) *size_ret = strlen(sel->selbuf);
963 html_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
967 sel = selections + *(int *)data;
968 if (data_ret) *data_ret = strdup(sel->selbuf);
969 if (size_ret) *size_ret = strlen(sel->selbuf);
975 uri_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
978 sel = selections + *((int *)data);
979 cnp_debug("Uri converter\n");
980 if (data_ret) *data_ret = strdup(sel->selbuf);
981 if (size_ret) *size_ret = strlen(sel->selbuf);
986 pasteimage_append(char *file, Evas_Object *entry)
990 static const char *tagstring = "<item absize=240x180 href=file://%s></item>";
992 if (!file) return EINA_FALSE;
993 if (!entry) return EINA_FALSE;
995 len = strlen(tagstring)+strlen(file);
997 entrytag = alloca(len + 1);
998 snprintf(entrytag, len + 1, tagstring, file);
999 elm_entry_entry_insert(entry, entrytag);
1005 remove_tags(const char *p, int *len)
1009 if (!p) return NULL;
1011 q = malloc(strlen(p) + 1);
1012 if (!q) return NULL;
1017 if ((*p != '<') && (*p != '&')) *q++ = *p++;
1020 if ((p[1] == 'b') && (p[2] == 'r') &&
1021 ((p[3] == ' ') || (p[3] == '/') || (p[3] == '>')))
1023 while ((*p) && (*p != '>')) p++;
1029 for (i = 0 ; i < N_ESCAPES ; i++)
1031 if (!strncmp(p,escapes[i].escape, strlen(escapes[i].escape)))
1033 p += strlen(escapes[i].escape);
1034 *q = escapes[i].value;
1039 if (i == N_ESCAPES) *q ++= '&';
1043 if (len) *len = q - ret;
1049 mark_up(const char *start, int inlen, int *lenp)
1054 const char *endp = NULL;
1056 if (!start) return NULL;
1057 if (inlen >= 0) endp = start + inlen;
1058 /* First pass: Count characters */
1059 for (l = 0, p = start; ((!endp) || (p < endp)) && (*p); p++)
1061 for (i = 0 ; i < N_ESCAPES ; i ++)
1063 if (*p == escapes[i].value)
1065 l += strlen(escapes[i].escape);
1069 if (i == N_ESCAPES) l++;
1072 q = ret = malloc(l + 1);
1074 /* Second pass: Change characters */
1075 for (p = start; ((!endp) || (p < endp)) && (*p); )
1077 for (i = 0; i < N_ESCAPES; i++)
1079 if (*p == escapes[i].value)
1081 strcpy(q, escapes[i].escape);
1082 q += strlen(escapes[i].escape);
1087 if (i == N_ESCAPES) *q++ = *p++;
1091 if (lenp) *lenp = l;
1097 _dnd_enter(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1099 Ecore_X_Event_Xdnd_Enter *enter = ev;
1103 if ((!enter) || (!enter->num_types) || (!enter->types)) return EINA_TRUE;
1105 cnp_debug("Types\n");
1106 savedtypes.ntypes = enter->num_types;
1107 if (savedtypes.types) free(savedtypes.types);
1108 savedtypes.types = malloc(sizeof(char *) * enter->num_types);
1109 if (!savedtypes.types) return EINA_FALSE;
1111 for (i = 0; i < enter->num_types; i++)
1113 savedtypes.types[i] = eina_stringshare_add(enter->types[i]);
1114 cnp_debug("Type is %s %p %p\n", enter->types[i],
1115 savedtypes.types[i],text_uri);
1116 if (savedtypes.types[i] == text_uri)
1118 /* Request it, so we know what it is */
1119 cnp_debug("Sending uri request\n");
1120 savedtypes.textreq = 1;
1121 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1122 savedtypes.imgfile = NULL;
1123 ecore_x_selection_xdnd_request(enter->win, text_uri);
1127 /* FIXME: Find an object and make it current */
1132 _dnd_drop(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1134 struct _Ecore_X_Event_Xdnd_Drop *drop;
1138 Ecore_X_Window xwin;
1139 Elm_Selection_Data ddata;
1145 // check we still have something to drop
1146 if (!drops) return EINA_TRUE;
1148 /* Find any widget in our window; then work out geometry rel to our window */
1149 for (l = drops; l; l = l->next)
1152 xwin = (Ecore_X_Window)ecore_evas_window_get
1153 (ecore_evas_ecore_evas_get(evas_object_evas_get
1155 if (xwin == drop->win) break;
1157 /* didn't find a window */
1158 if (!l) return EINA_TRUE;
1160 /* Calculate real (widget relative) position */
1161 // - window position
1162 // - widget position
1163 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(dropable->obj));
1164 ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
1165 savedtypes.x = drop->position.x - x;
1166 savedtypes.y = drop->position.y - y;
1168 cnp_debug("Drop position is %d,%d\n", savedtypes.x, savedtypes.y);
1170 for (; l; l = l->next)
1173 evas_object_geometry_get(dropable->obj, &x, &y, &w, &h);
1174 if ((savedtypes.x >= x) && (savedtypes.y >= y) &&
1175 (savedtypes.x < x + w) && (savedtypes.y < y + h))
1179 if (!l) return EINA_TRUE; /* didn't find one */
1181 evas_object_geometry_get(dropable->obj, &x, &y, NULL, NULL);
1185 /* Find our type from the previous list */
1186 for (i = 0; i < CNP_N_ATOMS; i++)
1188 for (j = 0; j < savedtypes.ntypes; j++)
1190 if (!strcmp(savedtypes.types[j], atoms[i].name)) goto found;
1194 cnp_debug("Didn't find a target\n");
1198 cnp_debug("Found a target we'd like: %s\n", atoms[i].name);
1199 cnp_debug("0x%x\n",xwin);
1201 if (i == CNP_ATOM_text_urilist)
1203 cnp_debug("We found a URI... (%scached) %s\n",
1204 savedtypes.imgfile ? "" : "not ",
1205 savedtypes.imgfile);
1206 if (savedtypes.imgfile)
1209 static const char *tagstring = "<item absize=240x180 href="
1210 "file://%s></item>";
1211 ddata.x = savedtypes.x;
1212 ddata.y = savedtypes.y;
1214 /* If it's markup that also supports images */
1215 if ((dropable->types & ELM_SEL_FORMAT_MARKUP) &&
1216 (dropable->types & ELM_SEL_FORMAT_IMAGE))
1219 ddata.format = ELM_SEL_FORMAT_MARKUP;
1221 len = strlen(tagstring) + strlen(savedtypes.imgfile);
1222 entrytag = alloca(len + 1);
1223 snprintf(entrytag, len + 1, tagstring, savedtypes.imgfile);
1224 ddata.data = entrytag;
1225 cnp_debug("Insert %s\n", (char *)ddata.data);
1226 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1227 ecore_x_dnd_send_finished();
1228 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1229 savedtypes.imgfile = NULL;
1233 else if (dropable->types & ELM_SEL_FORMAT_IMAGE)
1235 cnp_debug("Doing image insert (%s)\n", savedtypes.imgfile);
1236 ddata.format = ELM_SEL_FORMAT_IMAGE;
1237 ddata.data = (char *)savedtypes.imgfile;
1238 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1239 ecore_x_dnd_send_finished();
1240 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1241 savedtypes.imgfile = NULL;
1247 cnp_debug("Item doesn't support images... passing\n");
1251 else if (savedtypes.textreq)
1253 /* Already asked: Pretend we asked now, and paste immediately when
1255 savedtypes.textreq = 0;
1256 ecore_x_dnd_send_finished();
1261 cnp_debug("doing a request then\n");
1262 selections[ELM_SEL_XDND].requestwidget = dropable->obj;
1263 selections[ELM_SEL_XDND].requestformat = ELM_SEL_FORMAT_MARKUP;
1264 selections[ELM_SEL_XDND].active = EINA_TRUE;
1266 ecore_x_selection_xdnd_request(xwin, atoms[i].name);
1271 _dnd_position(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1273 struct _Ecore_X_Event_Xdnd_Position *pos;
1274 Ecore_X_Rectangle rect;
1278 /* Need to send a status back */
1279 /* FIXME: Should check I can drop here */
1280 /* FIXME: Should highlight widget */
1281 rect.x = pos->position.x - 5;
1282 rect.y = pos->position.y - 5;
1285 ecore_x_dnd_send_status(EINA_TRUE, EINA_FALSE, rect, pos->action);
1291 * When dragging this is callback response from the destination.
1292 * The important thing we care about: Can we drop; thus update cursor
1296 _dnd_status(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1298 struct _Ecore_X_Event_Xdnd_Status *status = ev;
1300 if (!status) return EINA_TRUE;
1302 /* Only thing we care about: will accept */
1303 if (status->will_accept)
1305 cnp_debug("Will accept\n");
1308 { /* Won't accept */
1309 cnp_debug("Won't accept accept\n");
1315 * Add a widget as drop target.
1318 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
1321 Ecore_X_Window xwin;
1325 if (!obj) return EINA_FALSE;
1326 if (!_elm_cnp_init_count) _elm_cnp_init();
1328 /* Is this the first? */
1329 first = (!drops) ? 1 : 0;
1331 EINA_LIST_FOREACH(drops, item, drop)
1333 if (drop->obj == obj)
1335 /* Update: Not a new one */
1336 drop->dropcb = dropcb;
1337 drop->cbdata = cbdata;
1338 drop->types = format;
1343 /* Create new drop */
1344 drop = calloc(1, sizeof(Dropable));
1345 if (!drop) return EINA_FALSE;
1346 /* FIXME: Check for eina's deranged error method */
1347 drops = eina_list_append(drops, drop);
1349 if (!drops/* || or other error */)
1354 drop->dropcb = dropcb;
1355 drop->cbdata = cbdata;
1356 drop->types = format;
1359 evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL,
1360 /* I love C and varargs */
1361 (Evas_Object_Event_Cb)elm_drop_target_del,
1363 /* FIXME: Handle resizes */
1365 /* If not the first: We're done */
1366 if (!first) return EINA_TRUE;
1368 xwin = (Ecore_X_Window)ecore_evas_window_get
1369 (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
1371 ecore_x_dnd_aware_set(xwin, EINA_TRUE);
1373 cnp_debug("Adding drop target calls\n");
1374 handler_enter = ecore_event_handler_add(ECORE_X_EVENT_XDND_ENTER,
1376 handler_pos = ecore_event_handler_add(ECORE_X_EVENT_XDND_POSITION,
1377 _dnd_position, NULL);
1378 handler_drop = ecore_event_handler_add(ECORE_X_EVENT_XDND_DROP,
1385 elm_drop_target_del(Evas_Object *obj)
1387 Dropable *drop,*del;
1389 Ecore_X_Window xwin;
1392 EINA_LIST_FOREACH(drops, item, drop)
1394 if (drop->obj == obj)
1396 drops = eina_list_remove_list(drops, item);
1401 if (!del) return EINA_FALSE;
1403 evas_object_event_callback_del(obj, EVAS_CALLBACK_FREE,
1404 (Evas_Object_Event_Cb)elm_drop_target_del);
1406 /* If still drops there: All fine.. continue */
1407 if (drops) return EINA_TRUE;
1409 cnp_debug("Disabling DND\n");
1410 xwin = (Ecore_X_Window)ecore_evas_window_get
1411 (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
1412 ecore_x_dnd_aware_set(xwin, EINA_FALSE);
1414 ecore_event_handler_del(handler_pos);
1415 ecore_event_handler_del(handler_drop);
1416 ecore_event_handler_del(handler_enter);
1418 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1419 savedtypes.imgfile = NULL;
1426 _drag_mouse_up(void *un __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *data __UNUSED__)
1428 evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_UP, _drag_mouse_up);
1432 dragdonecb(dragdonecb,selections[ELM_SEL_XDND].widget);
1437 evas_object_del(dragwin);
1443 _drag_move(void *data __UNUSED__, Ecore_X_Xdnd_Position *pos)
1445 evas_object_move(dragwin,
1446 pos->position.x - _dragx,
1447 pos->position.y - _dragy);
1452 elm_drag_start(Evas_Object *obj, Elm_Sel_Format format, const char *data, void (*dragdone) (void *data, Evas_Object *), void *donecbdata)
1454 Ecore_X_Window xwin;
1456 Elm_Sel_Type xdnd = ELM_SEL_XDND;
1458 int x, y, x2, y2, x3, y3;
1462 if (!_elm_cnp_init_count) _elm_cnp_init();
1464 xwin = elm_win_xwindow_get(obj);
1466 cnp_debug("starting drag...\n");
1468 ecore_x_dnd_type_set(xwin, "text/uri-list", 1);
1469 sel = selections + ELM_SEL_XDND;
1472 sel->format = format;
1473 sel->selbuf = data ? strdup(data) : NULL;
1474 dragdonecb = dragdone;
1475 dragdonedata = donecbdata;
1477 ecore_x_dnd_callback_pos_update_set(_drag_move, NULL);
1478 ecore_x_dnd_begin(xwin, (unsigned char *)&xdnd, sizeof(Elm_Sel_Type));
1479 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
1480 _drag_mouse_up, NULL);
1482 handler_status = ecore_event_handler_add(ECORE_X_EVENT_XDND_STATUS,
1485 dragwin = elm_win_add(NULL, "Elm Drag Object", ELM_WIN_UTILITY);
1486 elm_win_override_set(dragwin, 1);
1488 /* FIXME: Images only */
1489 icon = elm_icon_add(dragwin);
1490 elm_icon_file_set(icon, data + 7, NULL); /* 7!? "file://" */
1491 elm_win_resize_object_add(dragwin,icon);
1492 evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1493 evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, EVAS_HINT_FILL);
1495 /* Position subwindow appropriately */
1496 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(obj));
1497 ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
1498 evas_object_geometry_get(obj, &x2, &y2, &w, &h);
1501 evas_object_move(dragwin, x, y);
1502 evas_object_resize(icon, w, h);
1503 evas_object_resize(dragwin, w, h);
1505 evas_object_show(icon);
1506 evas_object_show(dragwin);
1508 evas_pointer_canvas_xy_get(evas_object_evas_get(obj), &x3, &y3);
1516 elm_cnp_tempfile_create(int size)
1519 const char *tmppath;
1522 info = malloc(sizeof(Tmp_Info));
1523 if (!info) return NULL;
1525 tmppath = getenv("TMP");
1526 if (!tmppath) tmppath = P_tmpdir;
1527 if (!tmppath) tmppath = "/tmp";
1528 len = snprintf(NULL, 0, "%s/%sXXXXXX", tmppath, "elmcnpitem-");
1535 info->filename = malloc(len);
1536 if (!info->filename)
1541 snprintf(info->filename,len,"%s/%sXXXXXX", tmppath, "elmcnpitem-");
1543 info->fd = mkstemp(info->filename);
1548 /* And before someone says anything see POSIX 1003.1-2008 page 400 */
1551 pid = (long)getpid();
1552 /* Use pid instead of /proc/self: That way if can be passed around */
1553 len = snprintf(NULL,0,"/proc/%li/fd/%i", pid, info->fd);
1558 snprintf(tmp,len, "/proc/%li/fd/%i", pid, info->fd);
1559 unlink(info->filename);
1560 free(info->filename);
1561 info->filename = tmp;
1566 cnp_debug("filename is %s\n", info->filename);
1569 /* Set map to NULL and return */
1576 if (ftruncate(info->fd, size))
1578 perror("ftruncate");
1584 info->map = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, info->fd, 0);
1585 if (info->map == MAP_FAILED)
1597 tmpinfo_free(Tmp_Info *info)
1599 if (!info) return 0;
1600 free(info->filename);
1606 /* Stubs for windows */
1608 elm_drag_start(Evas_Object *o, Elm_Sel_Format f, const char *d, void (*donecb)(void *, Evas_Object *),void *cbdata)
1614 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
1620 elm_drop_target_del(Evas_Object *o)
1626 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-2f0^-2{2(0W1st0 :*/