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);
625 is_uri_type_data(Cnp_Selection *sel __UNUSED__, Ecore_X_Event_Selection_Notify *notify)
627 Ecore_X_Selection_Data *data;
631 cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
632 if (data->content == ECORE_X_SELECTION_CONTENT_FILES) return EINA_TRUE;
633 else p = (char *)data->data;
635 if (!p) return EINA_TRUE;
636 cnp_debug("Got %s\n", p);
637 if (strncmp(p, "file://", 7))
639 if (*p != '/') return EINA_FALSE;
646 * Callback to handle a targets response on a selection request:
647 * So pick the format we'd like; and then request it.
650 notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
652 Ecore_X_Selection_Data_Targets *targets;
653 Ecore_X_Atom *atomlist;
657 targets = notify->data;
658 atomlist = (Ecore_X_Atom *)(targets->data.data);
660 for (j = 1; j < CNP_N_ATOMS; j++)
662 cnp_debug("\t%s %d\n", atoms[j].name, atoms[j].atom);
663 if (!(atoms[j].formats & sel->requestformat)) continue;
664 for (i = 0; i < targets->data.length; i++)
666 if ((atoms[j].atom == atomlist[i]) && (atoms[j].notify))
668 if ((j == CNP_ATOM_text_uri) ||
669 (j == CNP_ATOM_text_urilist))
671 if(!is_uri_type_data(sel, notify)) continue;
673 cnp_debug("Atom %s matches\n",atoms[j].name);
679 cnp_debug("Couldn't find anything that matches\n");
680 return ECORE_CALLBACK_PASS_ON;
683 top = elm_widget_top_get(sel->requestwidget);
684 if (!top) top = sel->requestwidget;
685 cnp_debug("Sending request for %s\n", atoms[j].name);
686 sel->request(elm_win_xwindow_get(top), atoms[j].name);
688 return ECORE_CALLBACK_PASS_ON;
692 response_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
694 Ecore_X_Selection_Data_Targets *targets;
695 Ecore_X_Atom *atomlist;
699 targets = notify->data;
700 atomlist = (Ecore_X_Atom *)(targets->data.data);
702 /* Start from 1: Skip targets */
703 for (j = 1 ; j < CNP_N_ATOMS ; j ++)
705 if (!(atoms[j].formats & sel->requestformat)) continue;
706 for (i = 0 ; i < targets->data.length ; i ++)
708 if ((atoms[j].atom == atomlist[i]) && (atoms[j].response))
710 /* Found a match: Use it */
716 if (j == CNP_N_ATOMS)
718 cnp_debug("No matching type found\n");
722 top = elm_widget_top_get(sel->requestwidget);
725 sel->request(elm_win_xwindow_get(top), atoms[j].name);
731 notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
733 Ecore_X_Selection_Data *data;
740 Elm_Selection_Data ddata;
742 ddata.x = ddata.y = 0;
743 ddata.format = ELM_SEL_FORMAT_TEXT;
744 ddata.data = data->data;
745 ddata.len = data->length;
746 sel->datacb(sel->udata, sel->widget, &ddata);
750 cnp_debug("Notify handler text %d %d %p\n", data->format,data->length, data->data);
751 str = mark_up((char *)data->data, data->length, NULL);
752 cnp_debug("String is %s (from %s)\n", str, data->data);
753 elm_entry_entry_insert(sel->requestwidget, str);
760 * So someone is pasting an image into my entry or widget...
763 notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
765 Ecore_X_Selection_Data *data;
766 Ecore_X_Selection_Data_Files *files;
770 cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
771 if (data->content == ECORE_X_SELECTION_CONTENT_FILES)
773 cnp_debug("got a files list\n");
774 files = notify->data;
775 if (files->num_files > 1)
777 /* Don't handle many items */
778 cnp_debug("more then one file: Bailing\n");
785 p = (char *)data->data;
790 cnp_debug("Couldn't find a file\n");
793 cnp_debug("Got %s\n",p);
794 if (strncmp(p, "file://", 7))
796 /* Try and continue if it looks sane */
797 if (*p != '/') return 0;
801 p += strlen("file://");
804 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
805 if (savedtypes.textreq)
807 savedtypes.textreq = 0;
808 savedtypes.imgfile = strdup(p);
812 pasteimage_append(p, sel->requestwidget);
818 * Just receieved an vcard, either through cut and paste, or dnd.
821 vcard_receive(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
825 Ecore_X_Selection_Data *data;
828 cnp_debug("vcard receive\n");
830 if (sel == (selections + ELM_SEL_XDND))
832 Elm_Selection_Data ddata;
834 cnp_debug("drag & drop\n");
835 /* FIXME: this needs to be generic: Used for all receives */
836 EINA_LIST_FOREACH(drops, l, dropable)
838 if (dropable->obj == sel->requestwidget) break;
842 cnp_debug("Unable to find drop object");
843 ecore_x_dnd_send_finished();
846 dropable = eina_list_data_get(l);
847 ddata.x = savedtypes.x;
848 ddata.y = savedtypes.y;
849 ddata.format = ELM_SEL_FORMAT_VCARD;
850 ddata.data = data->data;
851 ddata.len = data->length;
852 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
853 ecore_x_dnd_send_finished();
855 else if (sel->datacb)
857 Elm_Selection_Data ddata;
858 ddata.x = ddata.y = 0;
859 ddata.format = ELM_SEL_FORMAT_IMAGE;
860 ddata.data = data->data;
861 ddata.len = data->length;
862 sel->datacb(sel->udata, sel->widget, &ddata);
866 cnp_debug("Paste request\n");
875 notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
877 Ecore_X_Selection_Data *data;
880 cnp_debug("got a png (or a jpeg)!\n");
883 cnp_debug("Size if %d\n", data->length);
887 Elm_Selection_Data ddata;
889 ddata.x = ddata.y = 0;
890 ddata.format = ELM_SEL_FORMAT_IMAGE;
891 ddata.data = data->data;
892 ddata.len = data->length;
893 sel->datacb(sel->udata, sel->widget, &ddata);
897 /* generate tmp name */
898 tmp = elm_cnp_tempfile_create(data->length);
899 memcpy(tmp->map, data->data, data->length);
900 munmap(tmp->map,data->length);
902 /* FIXME: Add to paste image data to clean up */
903 pasteimage_append(tmp->filename, sel->requestwidget);
911 * Warning: Generic text/html can';t handle it sanely.
912 * Firefox sends ucs2 (i think).
913 * chrome sends utf8... blerg
916 notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
918 Ecore_X_Selection_Data *data;
920 cnp_debug("Got some HTML: Checking encoding is useful\n");
925 Elm_Selection_Data ddata;
926 ddata.x = ddata.y = 0;
927 ddata.format = ELM_SEL_FORMAT_HTML;
928 ddata.data = data->data;
929 ddata.len = data->length;
930 sel->datacb(sel->udata, sel->widget, &ddata);
934 char *stripstr = NULL;
935 stripstr = malloc(sizeof(char) * (data->length + 1));
936 strncpy(stripstr, (char *)data->data, data->length);
937 stripstr[data->length] = '\0';
938 cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
939 elm_entry_entry_insert(sel->requestwidget, stripstr);
946 text_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
950 cnp_debug("text converter\n");
951 sel = selections + *((int *)data);
952 if (!sel->active) return EINA_TRUE;
954 if ((sel->format & ELM_SEL_FORMAT_MARKUP) ||
955 (sel->format & ELM_SEL_FORMAT_HTML))
957 *data_ret = remove_tags(sel->selbuf, size_ret);
959 else if (sel->format & ELM_SEL_FORMAT_TEXT)
961 *data_ret = strdup(sel->selbuf);
962 *size_ret = strlen(sel->selbuf);
964 else if (sel->format & ELM_SEL_FORMAT_IMAGE)
966 cnp_debug("Image %s\n", evas_object_type_get(sel->widget));
967 cnp_debug("Elm type: %s\n", elm_object_widget_type_get(sel->widget));
968 evas_object_image_file_get(elm_photocam_internal_image_get(sel->widget), (const char **)data_ret, NULL);
969 if (!*data_ret) *data_ret = strdup("No file");
970 else *data_ret = strdup(*data_ret);
971 *size_ret = strlen(*data_ret);
977 edje_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
981 sel = selections + *((int *)data);
982 if (data_ret) *data_ret = strdup(sel->selbuf);
983 if (size_ret) *size_ret = strlen(sel->selbuf);
990 html_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
994 sel = selections + *(int *)data;
995 if (data_ret) *data_ret = strdup(sel->selbuf);
996 if (size_ret) *size_ret = strlen(sel->selbuf);
1002 uri_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1005 sel = selections + *((int *)data);
1006 cnp_debug("Uri converter\n");
1007 if (data_ret) *data_ret = strdup(sel->selbuf);
1008 if (size_ret) *size_ret = strlen(sel->selbuf);
1013 pasteimage_append(char *file, Evas_Object *entry)
1017 static const char *tagstring = "<item absize=240x180 href=file://%s></item>";
1019 if (!file) return EINA_FALSE;
1020 if (!entry) return EINA_FALSE;
1022 len = strlen(tagstring)+strlen(file);
1024 entrytag = alloca(len + 1);
1025 snprintf(entrytag, len + 1, tagstring, file);
1026 elm_entry_entry_insert(entry, entrytag);
1032 remove_tags(const char *p, int *len)
1036 if (!p) return NULL;
1038 q = malloc(strlen(p) + 1);
1039 if (!q) return NULL;
1044 if ((*p != '<') && (*p != '&')) *q++ = *p++;
1047 if ((p[1] == 'b') && (p[2] == 'r') &&
1048 ((p[3] == ' ') || (p[3] == '/') || (p[3] == '>')))
1050 while ((*p) && (*p != '>')) p++;
1056 for (i = 0 ; i < N_ESCAPES ; i++)
1058 if (!strncmp(p,escapes[i].escape, strlen(escapes[i].escape)))
1060 p += strlen(escapes[i].escape);
1061 *q = escapes[i].value;
1066 if (i == N_ESCAPES) *q ++= '&';
1070 if (len) *len = q - ret;
1076 mark_up(const char *start, int inlen, int *lenp)
1081 const char *endp = NULL;
1083 if (!start) return NULL;
1084 if (inlen >= 0) endp = start + inlen;
1085 /* First pass: Count characters */
1086 for (l = 0, p = start; ((!endp) || (p < endp)) && (*p); p++)
1088 for (i = 0 ; i < N_ESCAPES ; i ++)
1090 if (*p == escapes[i].value)
1092 l += strlen(escapes[i].escape);
1096 if (i == N_ESCAPES) l++;
1099 q = ret = malloc(l + 1);
1101 /* Second pass: Change characters */
1102 for (p = start; ((!endp) || (p < endp)) && (*p); )
1104 for (i = 0; i < N_ESCAPES; i++)
1106 if (*p == escapes[i].value)
1108 strcpy(q, escapes[i].escape);
1109 q += strlen(escapes[i].escape);
1114 if (i == N_ESCAPES) *q++ = *p++;
1118 if (lenp) *lenp = l;
1124 _dnd_enter(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1126 Ecore_X_Event_Xdnd_Enter *enter = ev;
1130 if ((!enter) || (!enter->num_types) || (!enter->types)) return EINA_TRUE;
1132 cnp_debug("Types\n");
1133 savedtypes.ntypes = enter->num_types;
1134 if (savedtypes.types) free(savedtypes.types);
1135 savedtypes.types = malloc(sizeof(char *) * enter->num_types);
1136 if (!savedtypes.types) return EINA_FALSE;
1138 for (i = 0; i < enter->num_types; i++)
1140 savedtypes.types[i] = eina_stringshare_add(enter->types[i]);
1141 cnp_debug("Type is %s %p %p\n", enter->types[i],
1142 savedtypes.types[i],text_uri);
1143 if (savedtypes.types[i] == text_uri)
1145 /* Request it, so we know what it is */
1146 cnp_debug("Sending uri request\n");
1147 savedtypes.textreq = 1;
1148 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1149 savedtypes.imgfile = NULL;
1150 ecore_x_selection_xdnd_request(enter->win, text_uri);
1154 /* FIXME: Find an object and make it current */
1159 _dnd_drop(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1161 struct _Ecore_X_Event_Xdnd_Drop *drop;
1165 Ecore_X_Window xwin;
1166 Elm_Selection_Data ddata;
1172 // check we still have something to drop
1173 if (!drops) return EINA_TRUE;
1175 /* Find any widget in our window; then work out geometry rel to our window */
1176 for (l = drops; l; l = l->next)
1179 xwin = (Ecore_X_Window)ecore_evas_window_get
1180 (ecore_evas_ecore_evas_get(evas_object_evas_get
1182 if (xwin == drop->win) break;
1184 /* didn't find a window */
1185 if (!l) return EINA_TRUE;
1187 /* Calculate real (widget relative) position */
1188 // - window position
1189 // - widget position
1190 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(dropable->obj));
1191 ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
1192 savedtypes.x = drop->position.x - x;
1193 savedtypes.y = drop->position.y - y;
1195 cnp_debug("Drop position is %d,%d\n", savedtypes.x, savedtypes.y);
1197 for (; l; l = l->next)
1200 evas_object_geometry_get(dropable->obj, &x, &y, &w, &h);
1201 if ((savedtypes.x >= x) && (savedtypes.y >= y) &&
1202 (savedtypes.x < x + w) && (savedtypes.y < y + h))
1206 if (!l) return EINA_TRUE; /* didn't find one */
1208 evas_object_geometry_get(dropable->obj, &x, &y, NULL, NULL);
1212 /* Find our type from the previous list */
1213 for (i = 0; i < CNP_N_ATOMS; i++)
1215 for (j = 0; j < savedtypes.ntypes; j++)
1217 if (!strcmp(savedtypes.types[j], atoms[i].name)) goto found;
1221 cnp_debug("Didn't find a target\n");
1225 cnp_debug("Found a target we'd like: %s\n", atoms[i].name);
1226 cnp_debug("0x%x\n",xwin);
1228 if (i == CNP_ATOM_text_urilist)
1230 cnp_debug("We found a URI... (%scached) %s\n",
1231 savedtypes.imgfile ? "" : "not ",
1232 savedtypes.imgfile);
1233 if (savedtypes.imgfile)
1236 static const char *tagstring = "<item absize=240x180 href="
1237 "file://%s></item>";
1238 ddata.x = savedtypes.x;
1239 ddata.y = savedtypes.y;
1241 /* If it's markup that also supports images */
1242 if ((dropable->types & ELM_SEL_FORMAT_MARKUP) &&
1243 (dropable->types & ELM_SEL_FORMAT_IMAGE))
1246 ddata.format = ELM_SEL_FORMAT_MARKUP;
1248 len = strlen(tagstring) + strlen(savedtypes.imgfile);
1249 entrytag = alloca(len + 1);
1250 snprintf(entrytag, len + 1, tagstring, savedtypes.imgfile);
1251 ddata.data = entrytag;
1252 cnp_debug("Insert %s\n", (char *)ddata.data);
1253 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1254 ecore_x_dnd_send_finished();
1255 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1256 savedtypes.imgfile = NULL;
1260 else if (dropable->types & ELM_SEL_FORMAT_IMAGE)
1262 cnp_debug("Doing image insert (%s)\n", savedtypes.imgfile);
1263 ddata.format = ELM_SEL_FORMAT_IMAGE;
1264 ddata.data = (char *)savedtypes.imgfile;
1265 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1266 ecore_x_dnd_send_finished();
1267 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1268 savedtypes.imgfile = NULL;
1274 cnp_debug("Item doesn't support images... passing\n");
1278 else if (savedtypes.textreq)
1280 /* Already asked: Pretend we asked now, and paste immediately when
1282 savedtypes.textreq = 0;
1283 ecore_x_dnd_send_finished();
1288 cnp_debug("doing a request then\n");
1289 selections[ELM_SEL_XDND].requestwidget = dropable->obj;
1290 selections[ELM_SEL_XDND].requestformat = ELM_SEL_FORMAT_MARKUP;
1291 selections[ELM_SEL_XDND].active = EINA_TRUE;
1293 ecore_x_selection_xdnd_request(xwin, atoms[i].name);
1298 _dnd_position(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1300 struct _Ecore_X_Event_Xdnd_Position *pos;
1301 Ecore_X_Rectangle rect;
1305 /* Need to send a status back */
1306 /* FIXME: Should check I can drop here */
1307 /* FIXME: Should highlight widget */
1308 rect.x = pos->position.x - 5;
1309 rect.y = pos->position.y - 5;
1312 ecore_x_dnd_send_status(EINA_TRUE, EINA_FALSE, rect, pos->action);
1318 * When dragging this is callback response from the destination.
1319 * The important thing we care about: Can we drop; thus update cursor
1323 _dnd_status(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1325 struct _Ecore_X_Event_Xdnd_Status *status = ev;
1327 if (!status) return EINA_TRUE;
1329 /* Only thing we care about: will accept */
1330 if (status->will_accept)
1332 cnp_debug("Will accept\n");
1335 { /* Won't accept */
1336 cnp_debug("Won't accept accept\n");
1342 * Add a widget as drop target.
1345 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
1348 Ecore_X_Window xwin;
1352 if (!obj) return EINA_FALSE;
1353 if (!_elm_cnp_init_count) _elm_cnp_init();
1355 /* Is this the first? */
1356 first = (!drops) ? 1 : 0;
1358 EINA_LIST_FOREACH(drops, item, drop)
1360 if (drop->obj == obj)
1362 /* Update: Not a new one */
1363 drop->dropcb = dropcb;
1364 drop->cbdata = cbdata;
1365 drop->types = format;
1370 /* Create new drop */
1371 drop = calloc(1, sizeof(Dropable));
1372 if (!drop) return EINA_FALSE;
1373 /* FIXME: Check for eina's deranged error method */
1374 drops = eina_list_append(drops, drop);
1376 if (!drops/* || or other error */)
1381 drop->dropcb = dropcb;
1382 drop->cbdata = cbdata;
1383 drop->types = format;
1386 evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL,
1387 /* I love C and varargs */
1388 (Evas_Object_Event_Cb)elm_drop_target_del,
1390 /* FIXME: Handle resizes */
1392 /* If not the first: We're done */
1393 if (!first) return EINA_TRUE;
1395 xwin = (Ecore_X_Window)ecore_evas_window_get
1396 (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
1398 ecore_x_dnd_aware_set(xwin, EINA_TRUE);
1400 cnp_debug("Adding drop target calls\n");
1401 handler_enter = ecore_event_handler_add(ECORE_X_EVENT_XDND_ENTER,
1403 handler_pos = ecore_event_handler_add(ECORE_X_EVENT_XDND_POSITION,
1404 _dnd_position, NULL);
1405 handler_drop = ecore_event_handler_add(ECORE_X_EVENT_XDND_DROP,
1412 elm_drop_target_del(Evas_Object *obj)
1414 Dropable *drop,*del;
1416 Ecore_X_Window xwin;
1419 EINA_LIST_FOREACH(drops, item, drop)
1421 if (drop->obj == obj)
1423 drops = eina_list_remove_list(drops, item);
1428 if (!del) return EINA_FALSE;
1430 evas_object_event_callback_del(obj, EVAS_CALLBACK_FREE,
1431 (Evas_Object_Event_Cb)elm_drop_target_del);
1433 /* If still drops there: All fine.. continue */
1434 if (drops) return EINA_TRUE;
1436 cnp_debug("Disabling DND\n");
1437 xwin = (Ecore_X_Window)ecore_evas_window_get
1438 (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
1439 ecore_x_dnd_aware_set(xwin, EINA_FALSE);
1441 ecore_event_handler_del(handler_pos);
1442 ecore_event_handler_del(handler_drop);
1443 ecore_event_handler_del(handler_enter);
1445 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1446 savedtypes.imgfile = NULL;
1453 _drag_mouse_up(void *un __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *data __UNUSED__)
1455 evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_UP, _drag_mouse_up);
1459 dragdonecb(dragdonecb,selections[ELM_SEL_XDND].widget);
1464 evas_object_del(dragwin);
1470 _drag_move(void *data __UNUSED__, Ecore_X_Xdnd_Position *pos)
1472 evas_object_move(dragwin,
1473 pos->position.x - _dragx,
1474 pos->position.y - _dragy);
1479 elm_drag_start(Evas_Object *obj, Elm_Sel_Format format, const char *data, void (*dragdone) (void *data, Evas_Object *), void *donecbdata)
1481 Ecore_X_Window xwin;
1483 Elm_Sel_Type xdnd = ELM_SEL_XDND;
1485 int x, y, x2, y2, x3, y3;
1489 if (!_elm_cnp_init_count) _elm_cnp_init();
1491 xwin = elm_win_xwindow_get(obj);
1493 cnp_debug("starting drag...\n");
1495 ecore_x_dnd_type_set(xwin, "text/uri-list", 1);
1496 sel = selections + ELM_SEL_XDND;
1499 sel->format = format;
1500 sel->selbuf = data ? strdup(data) : NULL;
1501 dragdonecb = dragdone;
1502 dragdonedata = donecbdata;
1504 ecore_x_dnd_callback_pos_update_set(_drag_move, NULL);
1505 ecore_x_dnd_begin(xwin, (unsigned char *)&xdnd, sizeof(Elm_Sel_Type));
1506 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
1507 _drag_mouse_up, NULL);
1509 handler_status = ecore_event_handler_add(ECORE_X_EVENT_XDND_STATUS,
1512 dragwin = elm_win_add(NULL, "Elm Drag Object", ELM_WIN_UTILITY);
1513 elm_win_override_set(dragwin, 1);
1515 /* FIXME: Images only */
1516 icon = elm_icon_add(dragwin);
1517 elm_icon_file_set(icon, data + 7, NULL); /* 7!? "file://" */
1518 elm_win_resize_object_add(dragwin,icon);
1519 evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1520 evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, EVAS_HINT_FILL);
1522 /* Position subwindow appropriately */
1523 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(obj));
1524 ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
1525 evas_object_geometry_get(obj, &x2, &y2, &w, &h);
1528 evas_object_move(dragwin, x, y);
1529 evas_object_resize(icon, w, h);
1530 evas_object_resize(dragwin, w, h);
1532 evas_object_show(icon);
1533 evas_object_show(dragwin);
1535 evas_pointer_canvas_xy_get(evas_object_evas_get(obj), &x3, &y3);
1543 elm_cnp_tempfile_create(int size)
1546 const char *tmppath;
1549 info = malloc(sizeof(Tmp_Info));
1550 if (!info) return NULL;
1552 tmppath = getenv("TMP");
1553 if (!tmppath) tmppath = P_tmpdir;
1554 if (!tmppath) tmppath = "/tmp";
1555 len = snprintf(NULL, 0, "%s/%sXXXXXX", tmppath, "elmcnpitem-");
1562 info->filename = malloc(len);
1563 if (!info->filename)
1568 snprintf(info->filename,len,"%s/%sXXXXXX", tmppath, "elmcnpitem-");
1570 info->fd = mkstemp(info->filename);
1575 /* And before someone says anything see POSIX 1003.1-2008 page 400 */
1578 pid = (long)getpid();
1579 /* Use pid instead of /proc/self: That way if can be passed around */
1580 len = snprintf(NULL,0,"/proc/%li/fd/%i", pid, info->fd);
1585 snprintf(tmp,len, "/proc/%li/fd/%i", pid, info->fd);
1586 unlink(info->filename);
1587 free(info->filename);
1588 info->filename = tmp;
1593 cnp_debug("filename is %s\n", info->filename);
1596 /* Set map to NULL and return */
1603 if (ftruncate(info->fd, size))
1605 perror("ftruncate");
1611 info->map = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, info->fd, 0);
1612 if (info->map == MAP_FAILED)
1624 tmpinfo_free(Tmp_Info *info)
1626 if (!info) return 0;
1627 free(info->filename);
1633 /* Stubs for windows */
1635 elm_drag_start(Evas_Object *o, Elm_Sel_Format f, const char *d, void (*donecb)(void *, Evas_Object *),void *cbdata)
1641 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
1647 elm_drop_target_del(Evas_Object *o)
1653 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-3f0^-2{2(0W1st0 :*/