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) -1, // everything
173 response_handler_targets,
174 notify_handler_targets,
179 (Elm_Sel_Format) -1, // everything
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_MAX] = {
346 ARRAYINIT(ELM_SEL_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_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_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_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_selection_set(Elm_Sel_Type selection, Evas_Object *widget, Elm_Sel_Format format, const char *selbuf)
413 #ifdef HAVE_ELEMENTARY_X
414 Evas_Object *top = elm_widget_top_get(widget);
418 if (top) xwin = elm_win_xwindow_get(top);
419 else xwin = elm_win_xwindow_get(widget);
420 if (!xwin) return EINA_FALSE;
421 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
422 if (!_elm_cnp_init_count) _elm_cnp_init();
423 if ((!selbuf) && (format != ELM_SEL_FORMAT_IMAGE))
424 return elm_selection_clear(selection, widget);
426 sel = selections + selection;
429 sel->widget = widget;
431 sel->set(xwin, &selection, sizeof(Elm_Sel_Type));
432 sel->format = format;
433 sel->selbuf = selbuf ? strdup(selbuf) : NULL;
442 elm_selection_clear(Elm_Sel_Type selection, Evas_Object *widget)
444 #ifdef HAVE_ELEMENTARY_X
447 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
448 if (!_elm_cnp_init_count) _elm_cnp_init();
450 sel = selections + selection;
452 /* No longer this selection: Consider it gone! */
453 if ((!sel->active) || (sel->widget != widget)) return EINA_TRUE;
466 elm_selection_get(Elm_Sel_Type selection, Elm_Sel_Format format,
467 Evas_Object *widget, Elm_Drop_Cb datacb, void *udata)
469 #ifdef HAVE_ELEMENTARY_X
473 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
474 if (!_elm_cnp_init_count) _elm_cnp_init();
476 sel = selections + selection;
477 top = elm_widget_top_get(widget);
478 if (!top) return EINA_FALSE;
480 sel->requestformat = format;
481 sel->requestwidget = widget;
482 sel->request(elm_win_xwindow_get(top), ECORE_X_SELECTION_TARGET_TARGETS);
483 sel->datacb = datacb;
492 #ifdef HAVE_ELEMENTARY_X
499 if (_elm_cnp_init_count++) return EINA_TRUE;
500 for (i = 0; i < CNP_N_ATOMS; i++)
502 atoms[i].atom = ecore_x_atom_get(atoms[i].name);
503 ecore_x_selection_converter_atom_add(atoms[i].atom,
507 ecore_event_handler_add(ECORE_X_EVENT_SELECTION_CLEAR, selection_clear, NULL);
508 ecore_event_handler_add(ECORE_X_EVENT_SELECTION_NOTIFY, selection_notify, NULL);
510 text_uri = eina_stringshare_add("text/uri-list");
515 selection_clear(void *udata __UNUSED__, int type __UNUSED__, void *event)
517 Ecore_X_Event_Selection_Clear *ev = event;
521 for (i = 0; i < ELM_SEL_MAX; i++)
523 if (selections[i].ecore_sel == ev->selection) break;
525 cnp_debug("selection %d clear\n", i);
526 /* Not me... Don't care */
527 if (i == ELM_SEL_MAX) return ECORE_CALLBACK_PASS_ON;
529 sel = selections + i;
534 return ECORE_CALLBACK_PASS_ON;
539 * Response to a selection notify:
540 * - So we have asked for the selection list.
541 * - If it's the targets list, parse it, and fire of what we want,
542 * else it's the data we want.
545 selection_notify(void *udata __UNUSED__, int type __UNUSED__, void *event)
547 Ecore_X_Event_Selection_Notify *ev = event;
551 cnp_debug("selection notify callback: %d\n",ev->selection);
552 switch (ev->selection)
554 case ECORE_X_SELECTION_CLIPBOARD:
555 sel = selections + ELM_SEL_CLIPBOARD;
557 case ECORE_X_SELECTION_PRIMARY:
558 sel = selections + ELM_SEL_PRIMARY;
560 case ECORE_X_SELECTION_SECONDARY:
561 sel = selections + ELM_SEL_SECONDARY;
563 case ECORE_X_SELECTION_XDND:
564 sel = selections + ELM_SEL_XDND;
567 return ECORE_CALLBACK_PASS_ON;
569 cnp_debug("Target is %s\n", ev->target);
571 for (i = 0; i < CNP_N_ATOMS; i++)
573 if (!strcmp(ev->target, atoms[i].name))
577 cnp_debug("Found something: %s\n", atoms[i].name);
578 atoms[i].notify(sel, ev);
582 cnp_debug("Ignored: No handler!\n");
587 return ECORE_CALLBACK_PASS_ON;
593 targets_converter(char *target __UNUSED__, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize)
599 if (!data_ret) return EINA_FALSE;
601 if (size != sizeof(int))
605 *data_ret = malloc(size * sizeof(char) + 1);
606 memcpy(*data_ret, data, size);
607 ((char**)(data_ret))[0][size] = 0;
609 if (size_ret) *size_ret = size;
613 sel = selections + *((int *)data);
615 for (i = 0, count = 0; i < CNP_N_ATOMS ; i++)
617 if (sel->format & atoms[i].formats) count++;
620 aret = malloc(sizeof(Ecore_X_Atom) * count);
621 for (i = 0, count = 0; i < CNP_N_ATOMS; i++)
623 if (sel->format & atoms[i].formats) aret[count ++] = atoms[i].atom;
627 if (typesize) *typesize = 32 /* urk */;
628 if (ttype) *ttype = ECORE_X_ATOM_ATOM;
629 if (size_ret) *size_ret = count;
635 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__)
637 cnp_debug("Image converter called\n");
642 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__)
646 cnp_debug("Vcard send called\n");
648 sel = selections + *((int *)data);
650 if (data_ret) *data_ret = strdup(sel->selbuf);
651 if (size_ret) *size_ret = strlen(sel->selbuf);
657 is_uri_type_data(Cnp_Selection *sel __UNUSED__, Ecore_X_Event_Selection_Notify *notify)
659 Ecore_X_Selection_Data *data;
663 cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
664 if (data->content == ECORE_X_SELECTION_CONTENT_FILES) return EINA_TRUE;
665 else p = (char *)data->data;
667 if (!p) return EINA_TRUE;
668 cnp_debug("Got %s\n", p);
669 if (strncmp(p, "file://", 7))
671 if (*p != '/') return EINA_FALSE;
678 * Callback to handle a targets response on a selection request:
679 * So pick the format we'd like; and then request it.
682 notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
684 Ecore_X_Selection_Data_Targets *targets;
685 Ecore_X_Atom *atomlist;
689 targets = notify->data;
690 atomlist = (Ecore_X_Atom *)(targets->data.data);
692 for (j = (CNP_ATOM_LISTING_ATOMS+1); j < CNP_N_ATOMS; j++)
694 cnp_debug("\t%s %d\n", atoms[j].name, atoms[j].atom);
695 if (!(atoms[j].formats & sel->requestformat)) continue;
696 for (i = 0; i < targets->data.length; i++)
698 if ((atoms[j].atom == atomlist[i]) && (atoms[j].notify))
700 if ((j == CNP_ATOM_text_uri) ||
701 (j == CNP_ATOM_text_urilist))
703 if(!is_uri_type_data(sel, notify)) continue;
705 cnp_debug("Atom %s matches\n",atoms[j].name);
711 cnp_debug("Couldn't find anything that matches\n");
712 return ECORE_CALLBACK_PASS_ON;
715 top = elm_widget_top_get(sel->requestwidget);
716 if (!top) top = sel->requestwidget;
717 cnp_debug("Sending request for %s\n", atoms[j].name);
718 sel->request(elm_win_xwindow_get(top), atoms[j].name);
720 return ECORE_CALLBACK_PASS_ON;
724 response_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
726 Ecore_X_Selection_Data_Targets *targets;
727 Ecore_X_Atom *atomlist;
731 targets = notify->data;
732 atomlist = (Ecore_X_Atom *)(targets->data.data);
734 /* Start from 1: Skip targets */
735 for (j = 1 ; j < CNP_N_ATOMS ; j ++)
737 if (!(atoms[j].formats & sel->requestformat)) continue;
738 for (i = 0 ; i < targets->data.length ; i ++)
740 if ((atoms[j].atom == atomlist[i]) && (atoms[j].response))
742 /* Found a match: Use it */
748 if (j == CNP_N_ATOMS)
750 cnp_debug("No matching type found\n");
754 top = elm_widget_top_get(sel->requestwidget);
757 sel->request(elm_win_xwindow_get(top), atoms[j].name);
763 notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
765 Ecore_X_Selection_Data *data;
772 Elm_Selection_Data ddata;
774 ddata.x = ddata.y = 0;
775 ddata.format = ELM_SEL_FORMAT_TEXT;
776 ddata.data = data->data;
777 ddata.len = data->length;
778 sel->datacb(sel->udata, sel->widget, &ddata);
782 cnp_debug("Notify handler text %d %d %p\n", data->format,data->length, data->data);
783 str = mark_up((char *)data->data, data->length, NULL);
784 cnp_debug("String is %s (from %s)\n", str, data->data);
785 _elm_entry_entry_paste(sel->requestwidget, str);
792 * So someone is pasting an image into my entry or widget...
795 notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
797 Ecore_X_Selection_Data *data;
798 Ecore_X_Selection_Data_Files *files;
802 cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
803 if (data->content == ECORE_X_SELECTION_CONTENT_FILES)
805 cnp_debug("got a files list\n");
806 files = notify->data;
807 if (files->num_files > 1)
809 /* Don't handle many items */
810 cnp_debug("more then one file: Bailing\n");
813 stripstr = p = strdup(files->files[0]);
817 stripstr = p = malloc(data->length * sizeof(char) + 1);
818 memcpy(stripstr, data->data, data->length);
819 stripstr[data->length] = 0;
824 cnp_debug("Couldn't find a file\n");
827 cnp_debug("Got %s\n",p);
828 if (strncmp(p, "file://", 7))
830 /* Try and continue if it looks sane */
839 p += strlen("file://");
842 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
843 if (savedtypes.textreq)
845 savedtypes.textreq = 0;
846 savedtypes.imgfile = strdup(p);
850 pasteimage_append(p, sel->requestwidget);
857 * Just receieved an vcard, either through cut and paste, or dnd.
860 vcard_receive(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
864 Ecore_X_Selection_Data *data;
867 cnp_debug("vcard receive\n");
869 if (sel == (selections + ELM_SEL_XDND))
871 Elm_Selection_Data ddata;
873 cnp_debug("drag & drop\n");
874 /* FIXME: this needs to be generic: Used for all receives */
875 EINA_LIST_FOREACH(drops, l, dropable)
877 if (dropable->obj == sel->requestwidget) break;
881 cnp_debug("Unable to find drop object");
882 ecore_x_dnd_send_finished();
885 dropable = eina_list_data_get(l);
886 ddata.x = savedtypes.x;
887 ddata.y = savedtypes.y;
888 ddata.format = ELM_SEL_FORMAT_VCARD;
889 ddata.data = data->data;
890 ddata.len = data->length;
891 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
892 ecore_x_dnd_send_finished();
894 else if (sel->datacb)
896 Elm_Selection_Data ddata;
897 ddata.x = ddata.y = 0;
898 ddata.format = ELM_SEL_FORMAT_IMAGE;
899 ddata.data = data->data;
900 ddata.len = data->length;
901 sel->datacb(sel->udata, sel->widget, &ddata);
905 cnp_debug("Paste request\n");
914 notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
916 Ecore_X_Selection_Data *data;
919 cnp_debug("got a png (or a jpeg)!\n");
922 cnp_debug("Size if %d\n", data->length);
926 Elm_Selection_Data ddata;
928 ddata.x = ddata.y = 0;
929 ddata.format = ELM_SEL_FORMAT_IMAGE;
930 ddata.data = data->data;
931 ddata.len = data->length;
932 sel->datacb(sel->udata, sel->widget, &ddata);
936 /* generate tmp name */
937 tmp = elm_cnp_tempfile_create(data->length);
938 memcpy(tmp->map, data->data, data->length);
939 munmap(tmp->map,data->length);
941 /* FIXME: Add to paste image data to clean up */
942 pasteimage_append(tmp->filename, sel->requestwidget);
950 * Warning: Generic text/html can';t handle it sanely.
951 * Firefox sends ucs2 (i think).
952 * chrome sends utf8... blerg
955 notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
957 Ecore_X_Selection_Data *data;
959 cnp_debug("Got some HTML: Checking encoding is useful\n");
964 Elm_Selection_Data ddata;
965 ddata.x = ddata.y = 0;
966 ddata.format = ELM_SEL_FORMAT_HTML;
967 ddata.data = data->data;
968 ddata.len = data->length;
969 sel->datacb(sel->udata, sel->widget, &ddata);
973 char *stripstr = NULL;
974 stripstr = malloc(sizeof(char) * (data->length + 1));
975 strncpy(stripstr, (char *)data->data, data->length);
976 stripstr[data->length] = '\0';
977 cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
978 _elm_entry_entry_paste(sel->requestwidget, stripstr);
985 text_converter(char *target __UNUSED__, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
989 cnp_debug("text converter\n");
990 if (size != sizeof(int))
994 *data_ret = malloc(size * sizeof(char) + 1);
995 memcpy(*data_ret, data, size);
996 ((char**)(data_ret))[0][size] = 0;
998 if (size_ret) *size_ret = size;
1001 sel = selections + *((int *)data);
1002 if (!sel->active) return EINA_TRUE;
1004 if ((sel->format & ELM_SEL_FORMAT_MARKUP) ||
1005 (sel->format & ELM_SEL_FORMAT_HTML))
1007 *data_ret = remove_tags(sel->selbuf, size_ret);
1009 else if (sel->format & ELM_SEL_FORMAT_TEXT)
1011 *data_ret = strdup(sel->selbuf);
1012 *size_ret = strlen(sel->selbuf);
1014 else if (sel->format & ELM_SEL_FORMAT_IMAGE)
1016 cnp_debug("Image %s\n", evas_object_type_get(sel->widget));
1017 cnp_debug("Elm type: %s\n", elm_object_widget_type_get(sel->widget));
1018 evas_object_image_file_get(elm_photocam_internal_image_get(sel->widget), (const char **)data_ret, NULL);
1019 if (!*data_ret) *data_ret = strdup("No file");
1020 else *data_ret = strdup(*data_ret);
1021 *size_ret = strlen(*data_ret);
1027 general_converter(char *target __UNUSED__, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1029 if (size == sizeof(int))
1033 sel = selections + *((int *)data);
1034 if (data_ret) *data_ret = strdup(sel->selbuf);
1035 if (size_ret) *size_ret = strlen(sel->selbuf);
1041 *data_ret = malloc(size * sizeof(char) + 1);
1042 memcpy(*data_ret, data, size);
1043 ((char**)(data_ret))[0][size] = 0;
1045 if (size_ret) *size_ret = size;
1051 pasteimage_append(char *file, Evas_Object *entry)
1055 static const char *tagstring = "<item absize=240x180 href=file://%s></item>";
1057 if (!file) return EINA_FALSE;
1058 if (!entry) return EINA_FALSE;
1060 len = strlen(tagstring)+strlen(file);
1062 entrytag = alloca(len + 1);
1063 snprintf(entrytag, len + 1, tagstring, file);
1064 _elm_entry_entry_paste(entry, entrytag);
1070 remove_tags(const char *p, int *len)
1074 if (!p) return NULL;
1076 q = malloc(strlen(p) + 1);
1077 if (!q) return NULL;
1082 Eina_Bool esc = EINA_TRUE;
1087 x = strchr(p + 3, '>');
1091 if (len) *len = strlen(ret);
1094 if (memcmp(p + 1, "br", 2) && memcmp(p + 1, "ps", 2))
1096 strncpy(q, p, x - p + 1);
1101 if (p[i] == '/') i--;
1112 strncpy(q, p, x - p + 1);
1120 strcpy(q, _PARAGRAPH_SEPARATOR);
1121 q += sizeof(_PARAGRAPH_SEPARATOR) - 1;
1126 for (i = 3 ; i < N_ESCAPES ; i++)
1128 if (strncmp(p, escapes[i].escape, strlen(escapes[i].escape)))
1130 p += strlen(escapes[i].escape);
1131 strcpy(q, escapes[i].value);
1132 q += strlen(escapes[i].value);
1135 if (i == N_ESCAPES) *q ++= '&';
1142 if (len) *len = q - ret;
1148 mark_up(const char *start, int inlen, int *lenp)
1153 const char *endp = NULL;
1155 if (!start) return NULL;
1156 if (inlen >= 0) endp = start + inlen;
1157 /* First pass: Count characters */
1158 for (l = 0, p = start; ((!endp) || (p < endp)) && (*p); p++)
1160 for (i = 0 ; i < N_ESCAPES ; i ++)
1162 if (*p == escapes[i].value[0])
1164 if (!strncmp(p, escapes[i].value, strlen(escapes[i].value)))
1165 l += strlen(escapes[i].escape);
1169 if (i == N_ESCAPES) l++;
1172 q = ret = malloc(l + 1);
1174 /* Second pass: Change characters */
1175 for (p = start; ((!endp) || (p < endp)) && (*p); )
1177 for (i = 0; i < N_ESCAPES; i++)
1179 if (*p == escapes[i].value[0])
1181 if (!strncmp(p, escapes[i].value, strlen(escapes[i].value)))
1183 strcpy(q, escapes[i].escape);
1184 q += strlen(escapes[i].escape);
1185 p += strlen(escapes[i].value);
1190 if (i == N_ESCAPES) *q++ = *p++;
1194 if (lenp) *lenp = l;
1200 _dnd_enter(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1202 Ecore_X_Event_Xdnd_Enter *enter = ev;
1206 if ((!enter) || (!enter->num_types) || (!enter->types)) return EINA_TRUE;
1208 cnp_debug("Types\n");
1209 savedtypes.ntypes = enter->num_types;
1210 if (savedtypes.types) free(savedtypes.types);
1211 savedtypes.types = malloc(sizeof(char *) * enter->num_types);
1212 if (!savedtypes.types) return EINA_FALSE;
1214 for (i = 0; i < enter->num_types; i++)
1216 savedtypes.types[i] = eina_stringshare_add(enter->types[i]);
1217 cnp_debug("Type is %s %p %p\n", enter->types[i],
1218 savedtypes.types[i],text_uri);
1219 if (savedtypes.types[i] == text_uri)
1221 /* Request it, so we know what it is */
1222 cnp_debug("Sending uri request\n");
1223 savedtypes.textreq = 1;
1224 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1225 savedtypes.imgfile = NULL;
1226 ecore_x_selection_xdnd_request(enter->win, text_uri);
1230 /* FIXME: Find an object and make it current */
1235 _dnd_drop(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1237 struct _Ecore_X_Event_Xdnd_Drop *drop;
1241 Ecore_X_Window xwin;
1242 Elm_Selection_Data ddata;
1248 // check we still have something to drop
1249 if (!drops) return EINA_TRUE;
1251 /* Find any widget in our window; then work out geometry rel to our window */
1252 for (l = drops; l; l = l->next)
1255 xwin = (Ecore_X_Window)ecore_evas_window_get
1256 (ecore_evas_ecore_evas_get(evas_object_evas_get
1258 if (xwin == drop->win) break;
1260 /* didn't find a window */
1261 if (!l) return EINA_TRUE;
1263 /* Calculate real (widget relative) position */
1264 // - window position
1265 // - widget position
1266 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(dropable->obj));
1267 ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
1268 savedtypes.x = drop->position.x - x;
1269 savedtypes.y = drop->position.y - y;
1271 cnp_debug("Drop position is %d,%d\n", savedtypes.x, savedtypes.y);
1273 for (; l; l = l->next)
1276 evas_object_geometry_get(dropable->obj, &x, &y, &w, &h);
1277 if ((savedtypes.x >= x) && (savedtypes.y >= y) &&
1278 (savedtypes.x < x + w) && (savedtypes.y < y + h))
1282 if (!l) return EINA_TRUE; /* didn't find one */
1284 evas_object_geometry_get(dropable->obj, &x, &y, NULL, NULL);
1288 /* Find our type from the previous list */
1289 for (i = 0; i < CNP_N_ATOMS; i++)
1291 for (j = 0; j < savedtypes.ntypes; j++)
1293 if (!strcmp(savedtypes.types[j], atoms[i].name)) goto found;
1297 cnp_debug("Didn't find a target\n");
1301 cnp_debug("Found a target we'd like: %s\n", atoms[i].name);
1302 cnp_debug("0x%x\n",xwin);
1304 if (i == CNP_ATOM_text_urilist)
1306 cnp_debug("We found a URI... (%scached) %s\n",
1307 savedtypes.imgfile ? "" : "not ",
1308 savedtypes.imgfile);
1309 if (savedtypes.imgfile)
1312 static const char *tagstring = "<item absize=240x180 href="
1313 "file://%s></item>";
1314 ddata.x = savedtypes.x;
1315 ddata.y = savedtypes.y;
1317 /* If it's markup that also supports images */
1318 if ((dropable->types & ELM_SEL_FORMAT_MARKUP) &&
1319 (dropable->types & ELM_SEL_FORMAT_IMAGE))
1322 ddata.format = ELM_SEL_FORMAT_MARKUP;
1324 len = strlen(tagstring) + strlen(savedtypes.imgfile);
1325 entrytag = alloca(len + 1);
1326 snprintf(entrytag, len + 1, tagstring, savedtypes.imgfile);
1327 ddata.data = entrytag;
1328 cnp_debug("Insert %s\n", (char *)ddata.data);
1329 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1330 ecore_x_dnd_send_finished();
1331 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1332 savedtypes.imgfile = NULL;
1336 else if (dropable->types & ELM_SEL_FORMAT_IMAGE)
1338 cnp_debug("Doing image insert (%s)\n", savedtypes.imgfile);
1339 ddata.format = ELM_SEL_FORMAT_IMAGE;
1340 ddata.data = (char *)savedtypes.imgfile;
1341 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1342 ecore_x_dnd_send_finished();
1343 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1344 savedtypes.imgfile = NULL;
1350 cnp_debug("Item doesn't support images... passing\n");
1354 else if (savedtypes.textreq)
1356 /* Already asked: Pretend we asked now, and paste immediately when
1358 savedtypes.textreq = 0;
1359 ecore_x_dnd_send_finished();
1364 cnp_debug("doing a request then\n");
1365 selections[ELM_SEL_XDND].requestwidget = dropable->obj;
1366 selections[ELM_SEL_XDND].requestformat = ELM_SEL_FORMAT_MARKUP;
1367 selections[ELM_SEL_XDND].active = EINA_TRUE;
1369 ecore_x_selection_xdnd_request(xwin, atoms[i].name);
1374 _dnd_position(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1376 struct _Ecore_X_Event_Xdnd_Position *pos;
1377 Ecore_X_Rectangle rect;
1381 /* Need to send a status back */
1382 /* FIXME: Should check I can drop here */
1383 /* FIXME: Should highlight widget */
1384 rect.x = pos->position.x - 5;
1385 rect.y = pos->position.y - 5;
1388 ecore_x_dnd_send_status(EINA_TRUE, EINA_FALSE, rect, pos->action);
1394 * When dragging this is callback response from the destination.
1395 * The important thing we care about: Can we drop; thus update cursor
1399 _dnd_status(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1401 struct _Ecore_X_Event_Xdnd_Status *status = ev;
1403 if (!status) return EINA_TRUE;
1405 /* Only thing we care about: will accept */
1406 if (status->will_accept)
1408 cnp_debug("Will accept\n");
1411 { /* Won't accept */
1412 cnp_debug("Won't accept accept\n");
1418 * Add a widget as drop target.
1421 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
1424 Ecore_X_Window xwin;
1429 if (!obj) return EINA_FALSE;
1430 top = elm_widget_top_get(obj);
1431 if (!top || !elm_win_xwindow_get(top)) return EINA_FALSE;
1432 if (!_elm_cnp_init_count) _elm_cnp_init();
1434 /* Is this the first? */
1435 first = (!drops) ? 1 : 0;
1437 EINA_LIST_FOREACH(drops, item, drop)
1439 if (drop->obj == obj)
1441 /* Update: Not a new one */
1442 drop->dropcb = dropcb;
1443 drop->cbdata = cbdata;
1444 drop->types = format;
1449 /* Create new drop */
1450 drop = calloc(1, sizeof(Dropable));
1451 if (!drop) return EINA_FALSE;
1452 /* FIXME: Check for eina's deranged error method */
1453 drops = eina_list_append(drops, drop);
1455 if (!drops/* || or other error */)
1460 drop->dropcb = dropcb;
1461 drop->cbdata = cbdata;
1462 drop->types = format;
1465 evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL,
1466 /* I love C and varargs */
1467 (Evas_Object_Event_Cb)elm_drop_target_del,
1469 /* FIXME: Handle resizes */
1471 /* If not the first: We're done */
1472 if (!first) return EINA_TRUE;
1474 xwin = (Ecore_X_Window)ecore_evas_window_get
1475 (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
1477 ecore_x_dnd_aware_set(xwin, EINA_TRUE);
1479 cnp_debug("Adding drop target calls\n");
1480 handler_enter = ecore_event_handler_add(ECORE_X_EVENT_XDND_ENTER,
1482 handler_pos = ecore_event_handler_add(ECORE_X_EVENT_XDND_POSITION,
1483 _dnd_position, NULL);
1484 handler_drop = ecore_event_handler_add(ECORE_X_EVENT_XDND_DROP,
1491 elm_drop_target_del(Evas_Object *obj)
1493 Dropable *drop,*del;
1495 Ecore_X_Window xwin;
1498 EINA_LIST_FOREACH(drops, item, drop)
1500 if (drop->obj == obj)
1502 drops = eina_list_remove_list(drops, item);
1507 if (!del) return EINA_FALSE;
1509 evas_object_event_callback_del(obj, EVAS_CALLBACK_FREE,
1510 (Evas_Object_Event_Cb)elm_drop_target_del);
1512 /* If still drops there: All fine.. continue */
1513 if (drops) return EINA_TRUE;
1515 cnp_debug("Disabling DND\n");
1516 xwin = (Ecore_X_Window)ecore_evas_window_get
1517 (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
1518 ecore_x_dnd_aware_set(xwin, EINA_FALSE);
1520 ecore_event_handler_del(handler_pos);
1521 ecore_event_handler_del(handler_drop);
1522 ecore_event_handler_del(handler_enter);
1524 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1525 savedtypes.imgfile = NULL;
1532 _drag_mouse_up(void *un __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *data __UNUSED__)
1534 evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_UP, _drag_mouse_up);
1538 dragdonecb(dragdonecb,selections[ELM_SEL_XDND].widget);
1543 evas_object_del(dragwin);
1549 _drag_move(void *data __UNUSED__, Ecore_X_Xdnd_Position *pos)
1551 evas_object_move(dragwin,
1552 pos->position.x - _dragx,
1553 pos->position.y - _dragy);
1558 elm_drag_start(Evas_Object *obj, Elm_Sel_Format format, const char *data, void (*dragdone) (void *data, Evas_Object *), void *donecbdata)
1560 Ecore_X_Window xwin;
1562 Elm_Sel_Type xdnd = ELM_SEL_XDND;
1564 int x, y, x2, y2, x3, y3;
1568 if (!_elm_cnp_init_count) _elm_cnp_init();
1570 xwin = elm_win_xwindow_get(obj);
1572 cnp_debug("starting drag...\n");
1574 ecore_x_dnd_type_set(xwin, "text/uri-list", 1);
1575 sel = selections + ELM_SEL_XDND;
1578 sel->format = format;
1579 sel->selbuf = data ? strdup(data) : NULL;
1580 dragdonecb = dragdone;
1581 dragdonedata = donecbdata;
1583 ecore_x_dnd_callback_pos_update_set(_drag_move, NULL);
1584 ecore_x_dnd_begin(xwin, (unsigned char *)&xdnd, sizeof(Elm_Sel_Type));
1585 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
1586 _drag_mouse_up, NULL);
1588 handler_status = ecore_event_handler_add(ECORE_X_EVENT_XDND_STATUS,
1591 dragwin = elm_win_add(NULL, "Elm Drag Object", ELM_WIN_UTILITY);
1592 elm_win_override_set(dragwin, 1);
1594 /* FIXME: Images only */
1595 icon = elm_icon_add(dragwin);
1596 elm_icon_file_set(icon, data + 7, NULL); /* 7!? "file://" */
1597 elm_win_resize_object_add(dragwin,icon);
1598 evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1599 evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, EVAS_HINT_FILL);
1601 /* Position subwindow appropriately */
1602 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(obj));
1603 ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
1604 evas_object_geometry_get(obj, &x2, &y2, &w, &h);
1607 evas_object_move(dragwin, x, y);
1608 evas_object_resize(icon, w, h);
1609 evas_object_resize(dragwin, w, h);
1611 evas_object_show(icon);
1612 evas_object_show(dragwin);
1614 evas_pointer_canvas_xy_get(evas_object_evas_get(obj), &x3, &y3);
1622 elm_cnp_tempfile_create(int size)
1626 const char *tmppath;
1629 info = malloc(sizeof(Tmp_Info));
1630 if (!info) return NULL;
1632 tmppath = getenv("TMP");
1633 if (!tmppath) tmppath = P_tmpdir;
1634 if (!tmppath) tmppath = "/tmp";
1635 len = snprintf(NULL, 0, "%s/%sXXXXXX", tmppath, "elmcnpitem-");
1642 info->filename = malloc(len);
1643 if (!info->filename)
1648 snprintf(info->filename,len,"%s/%sXXXXXX", tmppath, "elmcnpitem-");
1650 info->fd = mkstemp(info->filename);
1655 /* And before someone says anything see POSIX 1003.1-2008 page 400 */
1658 pid = (long)getpid();
1659 /* Use pid instead of /proc/self: That way if can be passed around */
1660 len = snprintf(NULL,0,"/proc/%li/fd/%i", pid, info->fd);
1665 snprintf(tmp,len, "/proc/%li/fd/%i", pid, info->fd);
1666 unlink(info->filename);
1667 free(info->filename);
1668 info->filename = tmp;
1673 cnp_debug("filename is %s\n", info->filename);
1676 /* Set map to NULL and return */
1683 if (ftruncate(info->fd, size))
1685 perror("ftruncate");
1691 eina_mmap_safety_enabled_set(EINA_TRUE);
1693 info->map = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, info->fd, 0);
1694 if (info->map == MAP_FAILED)
1710 tmpinfo_free(Tmp_Info *info)
1712 if (!info) return 0;
1713 free(info->filename);
1719 /* Stubs for windows */
1721 elm_drag_start(Evas_Object *o, Elm_Sel_Format f, const char *d, void (*donecb)(void *, Evas_Object *),void *cbdata)
1727 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
1733 elm_drop_target_del(Evas_Object *o)
1739 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-3f0^-2{2(0W1st0 :*/