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);
133 static Eina_Bool targets_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
134 static Eina_Bool text_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
135 static Eina_Bool general_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_edje(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
146 static int notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
147 static int vcard_receive(Cnp_Selection *sed, Ecore_X_Event_Selection_Notify *notify);
149 static Eina_Bool pasteimage_append(char *file, Evas_Object *entry);
150 static void entry_insert_filter(Evas_Object* entry, char* str);
152 static Cnp_Atom atoms[CNP_N_ATOMS] = {
153 [CNP_ATOM_TARGETS] = {
155 ELM_SEL_FORMAT_TARGETS,
157 response_handler_targets,
158 notify_handler_targets,
162 "ATOM", // for opera browser
163 ELM_SEL_FORMAT_TARGETS,
165 response_handler_targets,
166 notify_handler_targets,
170 "application/x-elementary-markup",
171 ELM_SEL_FORMAT_MARKUP,
177 [CNP_ATOM_text_uri] = {
179 ELM_SEL_FORMAT_IMAGE, /* Either images or entries */
185 [CNP_ATOM_text_urilist] = {
187 ELM_SEL_FORMAT_IMAGE,
193 [CNP_ATOM_text_x_vcard] = {
195 ELM_SEL_FORMAT_VCARD,
199 [CNP_ATOM_image_png] = {
201 ELM_SEL_FORMAT_IMAGE,
204 notify_handler_image,
207 [CNP_ATOM_image_jpeg] = {
209 ELM_SEL_FORMAT_IMAGE,
212 notify_handler_image,/* Raw image data is the same */
215 [CNP_ATOM_image_bmp] = {
217 ELM_SEL_FORMAT_IMAGE,
220 notify_handler_image,/* Raw image data is the same */
223 [CNP_ATOM_image_gif] = {
225 ELM_SEL_FORMAT_IMAGE,
228 notify_handler_image,/* Raw image data is the same */
231 [CNP_ATOM_image_tiff] = {
233 ELM_SEL_FORMAT_IMAGE,
236 notify_handler_image,/* Raw image data is the same */
239 [CNP_ATOM_image_svg] = {
241 ELM_SEL_FORMAT_IMAGE,
244 notify_handler_image,/* Raw image data is the same */
247 [CNP_ATOM_image_xpm] = {
249 ELM_SEL_FORMAT_IMAGE,
252 notify_handler_image,/* Raw image data is the same */
255 [CNP_ATOM_image_tga] = {
257 ELM_SEL_FORMAT_IMAGE,
260 notify_handler_image,/* Raw image data is the same */
263 [CNP_ATOM_image_ppm] = {
264 "image/x-portable-pixmap",
265 ELM_SEL_FORMAT_IMAGE,
268 notify_handler_image,/* Raw image data is the same */
271 [CNP_ATOM_text_html_utf8] = {
272 "text/html;charset=utf-8",
279 [CNP_ATOM_text_html] = {
284 notify_handler_html, /* No encoding: Webkit only */
287 [CNP_ATOM_UTF8STRING] = {
289 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
295 [CNP_ATOM_STRING] = {
297 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
305 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
311 [CNP_ATOM_text_plain_utf8] = {
312 "text/plain;charset=utf-8",
313 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
319 [CNP_ATOM_text_plain] = {
321 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
329 static Cnp_Selection selections[ELM_SEL_TYPE_CLIPBOARD + 1] = {
330 ARRAYINIT(ELM_SEL_TYPE_PRIMARY) {
332 .ecore_sel = ECORE_X_SELECTION_PRIMARY,
333 .set = ecore_x_selection_primary_set,
334 .clear = ecore_x_selection_primary_clear,
335 .request = ecore_x_selection_primary_request,
337 ARRAYINIT(ELM_SEL_TYPE_SECONDARY) {
338 .debug = "Secondary",
339 .ecore_sel = ECORE_X_SELECTION_SECONDARY,
340 .set = ecore_x_selection_secondary_set,
341 .clear = ecore_x_selection_secondary_clear,
342 .request = ecore_x_selection_secondary_request,
344 ARRAYINIT(ELM_SEL_TYPE_XDND) {
346 .ecore_sel = ECORE_X_SELECTION_XDND,
347 .request = ecore_x_selection_xdnd_request,
349 ARRAYINIT(ELM_SEL_TYPE_CLIPBOARD) {
350 .debug = "Clipboard",
351 .ecore_sel = ECORE_X_SELECTION_CLIPBOARD,
352 .set = ecore_x_selection_clipboard_set,
353 .clear = ecore_x_selection_clipboard_clear,
354 .request = ecore_x_selection_clipboard_request,
358 /* Data for DND in progress */
359 static Saved_Type savedtypes = { NULL, NULL, 0, 0, 0, EINA_FALSE };
361 static void (*dragdonecb) (void *data, Evas_Object *obj) = NULL;
362 static void *dragdonedata = NULL;
364 static int _elm_cnp_init_count = 0;
367 * Drag & Drop functions
370 /* FIXME: Way too many globals */
371 static Eina_List *drops = NULL;
372 static Evas_Object *dragwin = NULL;
373 static int _dragx = 0, _dragy = 0;
374 static Ecore_Event_Handler *handler_pos = NULL;
375 static Ecore_Event_Handler *handler_drop = NULL;
376 static Ecore_Event_Handler *handler_enter = NULL;
377 static Ecore_Event_Handler *handler_status = NULL;
381 /* Stringshared, so I can just compare pointers later */
382 static const char *text_uri;
385 elm_selection_selection_has_owner(void)
387 #ifdef HAVE_ELEMENTARY_X
388 return !!ecore_x_selection_owner_get(ECORE_X_ATOM_SELECTION_CLIPBOARD);
395 elm_cnp_selection_set(Evas_Object *obj, Elm_Sel_Type selection,
396 Elm_Sel_Format format, const void *selbuf, size_t buflen)
398 #ifdef HAVE_ELEMENTARY_X
399 Evas_Object *top = elm_widget_top_get(obj);
403 if (top) xwin = elm_win_xwindow_get(top);
404 else xwin = elm_win_xwindow_get(obj);
405 if ((!xwin) || (selection > ELM_SEL_TYPE_CLIPBOARD))
407 if (!_elm_cnp_init_count) _elm_cnp_init();
408 if ((!selbuf) && (format != ELM_SEL_FORMAT_IMAGE))
409 return elm_object_cnp_selection_clear(obj, selection);
411 sel = selections + selection;
413 sel->active = EINA_TRUE;
415 sel->set(xwin, &selection, sizeof(Elm_Sel_Type));
416 sel->format = format;
420 if (format == ELM_SEL_FORMAT_IMAGE)
422 sel->selbuf = malloc(buflen+1);
423 memcpy(sel->selbuf, selbuf, buflen);
426 sel->selbuf = strdup((char*)selbuf);
438 elm_object_cnp_selection_clear(Evas_Object *obj, Elm_Sel_Type selection)
440 #ifdef HAVE_ELEMENTARY_X
443 if ((unsigned int)selection > (unsigned int)ELM_SEL_TYPE_CLIPBOARD)
445 if (!_elm_cnp_init_count) _elm_cnp_init();
447 sel = selections + selection;
449 /* No longer this selection: Consider it gone! */
450 if ((!sel->active) || (sel->widget != obj)) return EINA_TRUE;
452 sel->active = EINA_FALSE;
468 elm_cnp_selection_get(Evas_Object *obj, Elm_Sel_Type selection,
469 Elm_Sel_Format format, Elm_Drop_Cb datacb, void *udata)
471 #ifdef HAVE_ELEMENTARY_X
475 if (selection > ELM_SEL_TYPE_CLIPBOARD)
477 if (!_elm_cnp_init_count) _elm_cnp_init();
479 sel = selections + selection;
480 top = elm_widget_top_get(obj);
481 if (!top) return EINA_FALSE;
483 sel->requestformat = format;
484 sel->requestwidget = obj;
485 sel->request(elm_win_xwindow_get(top), ECORE_X_SELECTION_TARGET_TARGETS);
486 sel->datacb = datacb;
495 #ifdef HAVE_ELEMENTARY_X
502 if (_elm_cnp_init_count++) return EINA_TRUE;
503 for (i = 0; i < CNP_N_ATOMS; i++)
505 atoms[i].atom = ecore_x_atom_get(atoms[i].name);
506 ecore_x_selection_converter_atom_add(atoms[i].atom,
510 ecore_event_handler_add(ECORE_X_EVENT_SELECTION_CLEAR, selection_clear, NULL);
511 ecore_event_handler_add(ECORE_X_EVENT_SELECTION_NOTIFY, selection_notify, NULL);
513 text_uri = eina_stringshare_add("text/uri-list");
518 selection_clear(void *udata __UNUSED__, int type __UNUSED__, void *event)
520 Ecore_X_Event_Selection_Clear *ev = event;
524 for (i = 0; i <= ELM_SEL_TYPE_CLIPBOARD; i++)
526 if (selections[i].ecore_sel == ev->selection) break;
528 cnp_debug("selection %d clear\n", i);
529 /* Not me... Don't care */
530 if (i > ELM_SEL_TYPE_CLIPBOARD) return ECORE_CALLBACK_PASS_ON;
532 sel = selections + i;
533 sel->active = EINA_FALSE;
541 return ECORE_CALLBACK_PASS_ON;
546 * Response to a selection notify:
547 * - So we have asked for the selection list.
548 * - If it's the targets list, parse it, and fire of what we want,
549 * else it's the data we want.
552 selection_notify(void *udata __UNUSED__, int type __UNUSED__, void *event)
554 Ecore_X_Event_Selection_Notify *ev = event;
558 cnp_debug("selection notify callback: %d\n",ev->selection);
559 switch (ev->selection)
561 case ECORE_X_SELECTION_PRIMARY:
562 sel = selections + ELM_SEL_TYPE_PRIMARY;
564 case ECORE_X_SELECTION_SECONDARY:
565 sel = selections + ELM_SEL_TYPE_SECONDARY;
567 case ECORE_X_SELECTION_XDND:
568 sel = selections + ELM_SEL_TYPE_XDND;
570 case ECORE_X_SELECTION_CLIPBOARD:
571 sel = selections + ELM_SEL_TYPE_CLIPBOARD;
574 return ECORE_CALLBACK_PASS_ON;
576 cnp_debug("Target is %s\n", ev->target);
578 for (i = 0; i < CNP_N_ATOMS; i++)
580 if (!strcmp(ev->target, atoms[i].name))
584 cnp_debug("Found something: %s\n", atoms[i].name);
585 atoms[i].notify(sel, ev);
589 cnp_debug("Ignored: No handler!\n");
594 return ECORE_CALLBACK_PASS_ON;
597 static Elm_Sel_Format
598 _get_selection_type(void *data, int size)
600 if (size == sizeof(Elm_Sel_Type))
602 Cnp_Selection *sel = selections + *((int *)data);
604 (sel->format >= ELM_SEL_FORMAT_TARGETS) &&
605 (sel->format <= ELM_SEL_FORMAT_HTML))
608 return ELM_SEL_FORMAT_NONE;
612 targets_converter(char *target __UNUSED__, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize)
617 Elm_Sel_Format seltype;
619 if (!data_ret) return EINA_FALSE;
621 if (!data || (*((unsigned int *)data) > ELM_SEL_TYPE_CLIPBOARD))
624 if (_get_selection_type(data, size) == ELM_SEL_FORMAT_NONE)
626 /* TODO : fallback into precise type */
627 seltype = ELM_SEL_FORMAT_TEXT;
631 sel = selections + *((int *)data);
632 seltype = sel->format;
635 for (i = 0, count = 0; i < CNP_N_ATOMS ; i++)
637 if (seltype & atoms[i].formats) count++;
640 aret = malloc(sizeof(Ecore_X_Atom) * count);
641 for (i = 0, count = 0; i < CNP_N_ATOMS; i++)
643 if (seltype & atoms[i].formats) aret[count ++] = atoms[i].atom;
647 if (typesize) *typesize = 32 /* urk */;
648 if (ttype) *ttype = ECORE_X_ATOM_ATOM;
649 if (size_ret) *size_ret = count;
655 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__)
657 cnp_debug("Image converter called\n");
662 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__)
666 cnp_debug("Vcard send called\n");
668 if (!data || (*((unsigned int *)data) > ELM_SEL_TYPE_CLIPBOARD))
670 sel = selections + *((int *)data);
672 if (data_ret) *data_ret = strdup(sel->selbuf);
673 if (size_ret) *size_ret = strlen(sel->selbuf);
680 is_uri_type_data(Cnp_Selection *sel __UNUSED__, Ecore_X_Event_Selection_Notify *notify)
682 Ecore_X_Selection_Data *data;
686 cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
687 if (data->content == ECORE_X_SELECTION_CONTENT_FILES) return EINA_TRUE;
688 p = (char *)data->data;
689 if (!p) return EINA_TRUE;
691 cnp_debug("Got %s\n", p);
692 if (strncmp(p, "file://", 7))
694 if (*p != '/') return EINA_FALSE;
701 * Callback to handle a targets response on a selection request:
702 * So pick the format we'd like; and then request it.
705 notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
707 Ecore_X_Selection_Data_Targets *targets;
708 Ecore_X_Atom *atomlist;
712 targets = notify->data;
713 atomlist = (Ecore_X_Atom *)(targets->data.data);
715 for (j = (CNP_ATOM_LISTING_ATOMS+1); j < CNP_N_ATOMS; j++)
717 cnp_debug("\t%s %d\n", atoms[j].name, atoms[j].atom);
718 if (!(atoms[j].formats & sel->requestformat)) continue;
719 for (i = 0; i < targets->data.length; i++)
721 if ((atoms[j].atom == atomlist[i]) && (atoms[j].notify))
724 if ((j == CNP_ATOM_text_uri) ||
725 (j == CNP_ATOM_text_urilist))
727 if(!is_uri_type_data(sel, notify)) continue;
730 cnp_debug("Atom %s matches\n",atoms[j].name);
736 cnp_debug("Couldn't find anything that matches\n");
737 return ECORE_CALLBACK_PASS_ON;
740 top = elm_widget_top_get(sel->requestwidget);
741 if (!top) top = sel->requestwidget;
742 cnp_debug("Sending request for %s\n", atoms[j].name);
743 sel->request(elm_win_xwindow_get(top), atoms[j].name);
745 return ECORE_CALLBACK_PASS_ON;
749 response_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
751 Ecore_X_Selection_Data_Targets *targets;
752 Ecore_X_Atom *atomlist;
756 targets = notify->data;
757 atomlist = (Ecore_X_Atom *)(targets->data.data);
759 for (j = (CNP_ATOM_LISTING_ATOMS+1); j < CNP_N_ATOMS; j++)
761 if (!(atoms[j].formats & sel->requestformat)) continue;
762 for (i = 0 ; i < targets->data.length ; i ++)
764 if ((atoms[j].atom == atomlist[i]) && (atoms[j].response))
766 /* Found a match: Use it */
771 cnp_debug("No matching type found\n");
775 top = elm_widget_top_get(sel->requestwidget);
778 sel->request(elm_win_xwindow_get(top), atoms[j].name);
783 entry_insert_filter(Evas_Object* entry, char* str)
788 char *insertStr = str;
790 // if entry has single line set then remove <br> & <ps> tags
791 if (elm_entry_single_line_get(entry))
793 Eina_Strbuf *buf = eina_strbuf_new();
796 eina_strbuf_append(buf, insertStr);
797 eina_strbuf_replace_all(buf, "<br>", "");
798 eina_strbuf_replace_all(buf, "<ps>", "");
799 insertStr = eina_strbuf_string_steal(buf);
800 eina_strbuf_free(buf);
803 cnp_debug("remove break tag: %s\n", insertStr);
805 _elm_entry_entry_paste(entry, insertStr);
807 if (insertStr != str)
812 notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
814 Ecore_X_Selection_Data *data;
821 Elm_Selection_Data ddata;
822 str = malloc(sizeof(char) * (data->length + 1));
823 strncpy(str, (char *)data->data, data->length);
824 str[data->length] = '\0';
826 ddata.x = ddata.y = 0;
827 ddata.format = ELM_SEL_FORMAT_TEXT;
829 ddata.len = data->length;
830 sel->datacb(sel->udata, sel->widget, &ddata);
835 cnp_debug("Notify handler text %d %d %p\n", data->format,data->length, data->data);
836 str = _elm_util_text_to_mkup((const char *) data->data);
837 cnp_debug("String is %s (from %s)\n", str, data->data);
838 entry_insert_filter(sel->requestwidget, str);
839 //_elm_entry_entry_paste(sel->requestwidget, str);
846 * So someone is pasting an image into my entry or widget...
849 notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
851 Ecore_X_Selection_Data *data;
852 Ecore_X_Selection_Data_Files *files;
856 cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
857 if (data->content == ECORE_X_SELECTION_CONTENT_FILES)
859 cnp_debug("got a files list\n");
860 files = notify->data;
861 if (files->num_files > 1)
863 /* Don't handle many items */
864 cnp_debug("more then one file: Bailing\n");
867 stripstr = p = strdup(files->files[0]);
871 stripstr = p = malloc(data->length * sizeof(char) + 1);
872 memcpy(stripstr, data->data, data->length);
873 stripstr[data->length] = 0;
878 cnp_debug("Couldn't find a file\n");
881 cnp_debug("Got %s\n",p);
884 Elm_Selection_Data ddata;
886 ddata.x = ddata.y = 0;
887 ddata.format = ELM_SEL_FORMAT_IMAGE;
889 ddata.len = data->length;
890 sel->datacb(sel->udata, sel->widget, &ddata);
894 if (strncmp(p, "file://", 7))
896 /* Try and continue if it looks sane */
905 p += strlen("file://");
908 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
909 if (savedtypes.textreq)
911 savedtypes.textreq = 0;
912 savedtypes.imgfile = strdup(p);
916 pasteimage_append(p, sel->requestwidget);
923 * Just receieved an vcard, either through cut and paste, or dnd.
926 vcard_receive(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
930 Ecore_X_Selection_Data *data;
933 cnp_debug("vcard receive\n");
935 if (sel == (selections + ELM_SEL_TYPE_XDND))
937 Elm_Selection_Data ddata;
939 cnp_debug("drag & drop\n");
940 /* FIXME: this needs to be generic: Used for all receives */
941 EINA_LIST_FOREACH(drops, l, dropable)
943 if (dropable->obj == sel->requestwidget) break;
947 cnp_debug("Unable to find drop object");
948 ecore_x_dnd_send_finished();
951 dropable = eina_list_data_get(l);
952 ddata.x = savedtypes.x;
953 ddata.y = savedtypes.y;
954 ddata.format = ELM_SEL_FORMAT_VCARD;
955 ddata.data = data->data;
956 ddata.len = data->length;
957 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
958 ecore_x_dnd_send_finished();
960 else if (sel->datacb)
962 Elm_Selection_Data ddata;
963 ddata.x = ddata.y = 0;
964 ddata.format = ELM_SEL_FORMAT_IMAGE;
965 ddata.data = data->data;
966 ddata.len = data->length;
967 sel->datacb(sel->udata, sel->widget, &ddata);
971 cnp_debug("Paste request\n");
980 notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
982 Ecore_X_Selection_Data *data;
985 cnp_debug("got a image file!\n");
988 cnp_debug("Size if %d\n", data->length);
992 Elm_Selection_Data ddata;
994 ddata.x = ddata.y = 0;
995 ddata.format = ELM_SEL_FORMAT_IMAGE;
996 ddata.data = data->data;
997 ddata.len = data->length;
998 sel->datacb(sel->udata, sel->widget, &ddata);
1002 /* generate tmp name */
1003 tmp = elm_cnp_tempfile_create(data->length);
1004 memcpy(tmp->map, data->data, data->length);
1005 munmap(tmp->map, data->length);
1007 /* FIXME: Add to paste image data to clean up */
1008 pasteimage_append(tmp->filename, sel->requestwidget);
1015 notify_handler_edje(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1017 Ecore_X_Selection_Data *data;
1019 data = notify->data;
1021 char *stripstr = NULL;
1022 stripstr = malloc(sizeof(char) * (data->length + 1));
1023 strncpy(stripstr, (char *)data->data, data->length);
1024 stripstr[data->length] = '\0';
1028 Elm_Selection_Data ddata;
1029 ddata.x = ddata.y = 0;
1030 ddata.format = ELM_SEL_FORMAT_MARKUP;
1031 ddata.data = stripstr;
1032 ddata.len = data->length;
1033 sel->datacb(sel->udata, sel->widget, &ddata);
1036 entry_insert_filter(sel->requestwidget, stripstr);
1037 //_elm_entry_entry_paste(sel->requestwidget, stripstr);
1039 cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
1045 * Warning: Generic text/html can';t handle it sanely.
1046 * Firefox sends ucs2 (i think).
1047 * chrome sends utf8... blerg
1050 notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1052 Ecore_X_Selection_Data *data;
1054 cnp_debug("Got some HTML: Checking encoding is useful\n");
1055 data = notify->data;
1057 char *stripstr = NULL;
1058 stripstr = malloc(sizeof(char) * (data->length + 1));
1059 strncpy(stripstr, (char *)data->data, data->length);
1060 stripstr[data->length] = '\0';
1064 Elm_Selection_Data ddata;
1065 ddata.x = ddata.y = 0;
1066 ddata.format = ELM_SEL_FORMAT_HTML;
1067 ddata.data = stripstr;
1068 ddata.len = data->length;
1069 sel->datacb(sel->udata, sel->widget, &ddata);
1072 entry_insert_filter(sel->requestwidget, stripstr);
1073 //_elm_entry_entry_paste(sel->requestwidget, stripstr);
1075 cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
1082 text_converter(char *target __UNUSED__, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1086 cnp_debug("text converter\n");
1087 if (!data || (*((unsigned int *)data) > ELM_SEL_TYPE_CLIPBOARD))
1089 if (_get_selection_type(data, size) == ELM_SEL_FORMAT_NONE)
1093 *data_ret = malloc(size * sizeof(char) + 1);
1094 memcpy(*data_ret, data, size);
1095 ((char**)(data_ret))[0][size] = 0;
1097 if (size_ret) *size_ret = size;
1100 sel = selections + *((int *)data);
1101 if (!sel->active) return EINA_TRUE;
1103 if (sel->format & ELM_SEL_FORMAT_MARKUP)
1104 *data_ret = _elm_util_mkup_to_text(sel->selbuf);
1105 else if (sel->format & ELM_SEL_FORMAT_HTML)
1108 Eina_Strbuf *buf = eina_strbuf_new();
1111 eina_strbuf_append(buf, sel->selbuf);
1112 eina_strbuf_replace_all(buf, " ", " ");
1113 text = eina_strbuf_string_steal(buf);
1114 eina_strbuf_free(buf);
1115 *data_ret = _elm_util_mkup_to_text(text);
1119 *data_ret = _elm_util_mkup_to_text(sel->selbuf);
1121 else if (sel->format & ELM_SEL_FORMAT_TEXT)
1123 *data_ret = strdup(sel->selbuf);
1124 *size_ret = strlen(sel->selbuf);
1126 else if (sel->format & ELM_SEL_FORMAT_IMAGE)
1128 cnp_debug("Image %s\n", evas_object_type_get(sel->widget));
1129 cnp_debug("Elm type: %s\n", elm_object_widget_type_get(sel->widget));
1130 evas_object_image_file_get(elm_photocam_internal_image_get(sel->widget),
1131 (const char **)data_ret, NULL);
1132 if (!*data_ret) *data_ret = strdup("No file");
1133 else *data_ret = strdup(*data_ret);
1134 *size_ret = strlen(*data_ret);
1140 general_converter(char *target __UNUSED__, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1142 if (_get_selection_type(data, size) == ELM_SEL_FORMAT_NONE)
1146 *data_ret = malloc(size * sizeof(char) + 1);
1147 memcpy(*data_ret, data, size);
1148 ((char**)(data_ret))[0][size] = 0;
1150 if (size_ret) *size_ret = size;
1155 if (!data || (*((unsigned int *)data) > ELM_SEL_TYPE_CLIPBOARD))
1158 sel = selections + *((int *)data);
1159 if (data_ret) *data_ret = strdup(sel->selbuf);
1160 if (size_ret) *size_ret = strlen(sel->selbuf);
1166 pasteimage_append(char *file, Evas_Object *entry)
1170 static const char *tagstring = "<item absize=240x180 href=file://%s></item>";
1172 if ((!file) || (!entry)) return EINA_FALSE;
1174 len = strlen(tagstring)+strlen(file);
1176 entrytag = alloca(len + 1);
1177 snprintf(entrytag, len + 1, tagstring, file);
1178 _elm_entry_entry_paste(entry, entrytag);
1184 _dnd_enter(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1186 Ecore_X_Event_Xdnd_Enter *enter = ev;
1190 if ((!enter) || (!enter->num_types) || (!enter->types)) return EINA_TRUE;
1192 cnp_debug("Types\n");
1193 savedtypes.ntypes = enter->num_types;
1194 if (savedtypes.types) free(savedtypes.types);
1195 savedtypes.types = malloc(sizeof(char *) * enter->num_types);
1196 if (!savedtypes.types) return EINA_FALSE;
1198 for (i = 0; i < enter->num_types; i++)
1200 savedtypes.types[i] = eina_stringshare_add(enter->types[i]);
1201 cnp_debug("Type is %s %p %p\n", enter->types[i],
1202 savedtypes.types[i],text_uri);
1203 if (savedtypes.types[i] == text_uri)
1205 /* Request it, so we know what it is */
1206 cnp_debug("Sending uri request\n");
1207 savedtypes.textreq = 1;
1208 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1209 savedtypes.imgfile = NULL;
1210 ecore_x_selection_xdnd_request(enter->win, text_uri);
1214 /* FIXME: Find an object and make it current */
1219 _dnd_drop(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1221 struct _Ecore_X_Event_Xdnd_Drop *drop;
1225 Ecore_X_Window xwin;
1226 Elm_Selection_Data ddata;
1232 // check we still have something to drop
1233 if (!drops) return EINA_TRUE;
1235 /* Find any widget in our window; then work out geometry rel to our window */
1236 for (l = drops; l; l = l->next)
1239 xwin = (Ecore_X_Window)ecore_evas_window_get
1240 (ecore_evas_ecore_evas_get(evas_object_evas_get
1242 if (xwin == drop->win) break;
1244 /* didn't find a window */
1245 if (!l) return EINA_TRUE;
1247 /* Calculate real (widget relative) position */
1248 // - window position
1249 // - widget position
1250 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(dropable->obj));
1251 ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
1252 savedtypes.x = drop->position.x - x;
1253 savedtypes.y = drop->position.y - y;
1255 cnp_debug("Drop position is %d,%d\n", savedtypes.x, savedtypes.y);
1257 for (; l; l = l->next)
1260 evas_object_geometry_get(dropable->obj, &x, &y, &w, &h);
1261 if ((savedtypes.x >= x) && (savedtypes.y >= y) &&
1262 (savedtypes.x < x + w) && (savedtypes.y < y + h))
1266 if (!l) return EINA_TRUE; /* didn't find one */
1268 evas_object_geometry_get(dropable->obj, &x, &y, NULL, NULL);
1272 /* Find our type from the previous list */
1273 for (i = 0; i < CNP_N_ATOMS; i++)
1275 for (j = 0; j < savedtypes.ntypes; j++)
1277 if (!strcmp(savedtypes.types[j], atoms[i].name)) goto found;
1281 cnp_debug("Didn't find a target\n");
1285 cnp_debug("Found a target we'd like: %s\n", atoms[i].name);
1286 cnp_debug("0x%x\n",xwin);
1288 if (i == CNP_ATOM_text_urilist)
1290 cnp_debug("We found a URI... (%scached) %s\n",
1291 savedtypes.imgfile ? "" : "not ",
1292 savedtypes.imgfile);
1293 if (savedtypes.imgfile)
1296 static const char *tagstring = "<item absize=240x180 href="
1297 "file://%s></item>";
1298 ddata.x = savedtypes.x;
1299 ddata.y = savedtypes.y;
1301 /* If it's markup that also supports images */
1302 if ((dropable->types & ELM_SEL_FORMAT_MARKUP) &&
1303 (dropable->types & ELM_SEL_FORMAT_IMAGE))
1306 ddata.format = ELM_SEL_FORMAT_MARKUP;
1308 len = strlen(tagstring) + strlen(savedtypes.imgfile);
1309 entrytag = alloca(len + 1);
1310 snprintf(entrytag, len + 1, tagstring, savedtypes.imgfile);
1311 ddata.data = entrytag;
1312 cnp_debug("Insert %s\n", (char *)ddata.data);
1313 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1314 ecore_x_dnd_send_finished();
1315 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1316 savedtypes.imgfile = NULL;
1320 else if (dropable->types & ELM_SEL_FORMAT_IMAGE)
1322 cnp_debug("Doing image insert (%s)\n", savedtypes.imgfile);
1323 ddata.format = ELM_SEL_FORMAT_IMAGE;
1324 ddata.data = (char *)savedtypes.imgfile;
1325 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1326 ecore_x_dnd_send_finished();
1327 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1328 savedtypes.imgfile = NULL;
1334 cnp_debug("Item doesn't support images... passing\n");
1338 else if (savedtypes.textreq)
1340 /* Already asked: Pretend we asked now, and paste immediately when
1342 savedtypes.textreq = 0;
1343 ecore_x_dnd_send_finished();
1348 cnp_debug("doing a request then\n");
1349 selections[ELM_SEL_TYPE_XDND].requestwidget = dropable->obj;
1350 selections[ELM_SEL_TYPE_XDND].requestformat = ELM_SEL_FORMAT_MARKUP;
1351 selections[ELM_SEL_TYPE_XDND].active = EINA_TRUE;
1353 ecore_x_selection_xdnd_request(xwin, atoms[i].name);
1358 _dnd_position(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1360 struct _Ecore_X_Event_Xdnd_Position *pos;
1361 Ecore_X_Rectangle rect;
1365 /* Need to send a status back */
1366 /* FIXME: Should check I can drop here */
1367 /* FIXME: Should highlight widget */
1368 rect.x = pos->position.x - 5;
1369 rect.y = pos->position.y - 5;
1372 ecore_x_dnd_send_status(EINA_TRUE, EINA_FALSE, rect, pos->action);
1378 * When dragging this is callback response from the destination.
1379 * The important thing we care about: Can we drop; thus update cursor
1383 _dnd_status(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1385 struct _Ecore_X_Event_Xdnd_Status *status = ev;
1387 if (!status) return EINA_TRUE;
1389 /* Only thing we care about: will accept */
1390 if (status->will_accept)
1392 cnp_debug("Will accept\n");
1395 { /* Won't accept */
1396 cnp_debug("Won't accept accept\n");
1402 * Add a widget as drop target.
1405 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
1408 Ecore_X_Window xwin;
1413 if (!obj) return EINA_FALSE;
1414 top = elm_widget_top_get(obj);
1415 if (!top || !elm_win_xwindow_get(top)) return EINA_FALSE;
1416 if (!_elm_cnp_init_count) _elm_cnp_init();
1418 /* Is this the first? */
1419 first = (!drops) ? 1 : 0;
1421 EINA_LIST_FOREACH(drops, item, drop)
1423 if (drop->obj == obj)
1425 /* Update: Not a new one */
1426 drop->dropcb = dropcb;
1427 drop->cbdata = cbdata;
1428 drop->types = format;
1433 /* Create new drop */
1434 drop = calloc(1, sizeof(Dropable));
1435 if (!drop) return EINA_FALSE;
1436 /* FIXME: Check for eina's deranged error method */
1437 drops = eina_list_append(drops, drop);
1439 if (!drops/* || or other error */)
1444 drop->dropcb = dropcb;
1445 drop->cbdata = cbdata;
1446 drop->types = format;
1449 evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL,
1450 /* I love C and varargs */
1451 (Evas_Object_Event_Cb)elm_drop_target_del,
1453 /* FIXME: Handle resizes */
1455 /* If not the first: We're done */
1456 if (!first) return EINA_TRUE;
1458 xwin = (Ecore_X_Window)ecore_evas_window_get
1459 (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
1461 ecore_x_dnd_aware_set(xwin, EINA_TRUE);
1463 cnp_debug("Adding drop target calls\n");
1464 handler_enter = ecore_event_handler_add(ECORE_X_EVENT_XDND_ENTER,
1466 handler_pos = ecore_event_handler_add(ECORE_X_EVENT_XDND_POSITION,
1467 _dnd_position, NULL);
1468 handler_drop = ecore_event_handler_add(ECORE_X_EVENT_XDND_DROP,
1475 elm_drop_target_del(Evas_Object *obj)
1477 Dropable *drop,*del;
1479 Ecore_X_Window xwin;
1482 EINA_LIST_FOREACH(drops, item, drop)
1484 if (drop->obj == obj)
1486 drops = eina_list_remove_list(drops, item);
1491 if (!del) return EINA_FALSE;
1493 evas_object_event_callback_del(obj, EVAS_CALLBACK_FREE,
1494 (Evas_Object_Event_Cb)elm_drop_target_del);
1496 /* If still drops there: All fine.. continue */
1497 if (drops) return EINA_TRUE;
1499 cnp_debug("Disabling DND\n");
1500 xwin = (Ecore_X_Window)ecore_evas_window_get
1501 (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
1502 ecore_x_dnd_aware_set(xwin, EINA_FALSE);
1504 ecore_event_handler_del(handler_pos);
1505 ecore_event_handler_del(handler_drop);
1506 ecore_event_handler_del(handler_enter);
1508 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1509 savedtypes.imgfile = NULL;
1516 _drag_mouse_up(void *un __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *data)
1518 Ecore_X_Window xwin = *((Ecore_X_Window *)data);
1519 evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_UP, _drag_mouse_up);
1521 ecore_x_dnd_aware_set(xwin, EINA_FALSE);
1524 dragdonecb(dragdonedata, selections[ELM_SEL_TYPE_XDND].widget);
1529 evas_object_del(dragwin);
1535 _drag_move(void *data __UNUSED__, Ecore_X_Xdnd_Position *pos)
1537 evas_object_move(dragwin,
1538 pos->position.x - _dragx,
1539 pos->position.y - _dragy);
1544 elm_drag_start(Evas_Object *obj, Elm_Sel_Format format, const char *data, void (*dragdone) (void *data, Evas_Object *), void *donecbdata)
1546 Ecore_X_Window xwin;
1548 Elm_Sel_Type xdnd = ELM_SEL_TYPE_XDND;
1550 int x, y, x2, y2, x3, y3;
1554 if (!_elm_cnp_init_count) _elm_cnp_init();
1556 xwin = elm_win_xwindow_get(obj);
1558 cnp_debug("starting drag...\n");
1562 cnp_debug("another obj is dragging...\n");
1566 ecore_x_dnd_type_set(xwin, "text/uri-list", EINA_TRUE);
1567 sel = selections + ELM_SEL_TYPE_XDND;
1568 sel->active = EINA_TRUE;
1570 sel->format = format;
1571 sel->selbuf = data ? strdup(data) : NULL;
1572 dragdonecb = dragdone;
1573 dragdonedata = donecbdata;
1575 ecore_x_dnd_aware_set(xwin, EINA_TRUE);
1576 ecore_x_dnd_callback_pos_update_set(_drag_move, NULL);
1577 ecore_x_dnd_begin(xwin, (unsigned char *)&xdnd, sizeof(Elm_Sel_Type));
1578 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
1579 _drag_mouse_up, (void *)(long)xwin);
1581 handler_status = ecore_event_handler_add(ECORE_X_EVENT_XDND_STATUS,
1584 dragwin = elm_win_add(NULL, "Elm Drag Object", ELM_WIN_UTILITY);
1585 elm_win_override_set(dragwin, EINA_TRUE);
1587 /* FIXME: Images only */
1588 icon = elm_icon_add(dragwin);
1589 elm_icon_file_set(icon, data + 7, NULL); /* 7!? "file://" */
1590 elm_win_resize_object_add(dragwin, icon);
1591 evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1592 evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, EVAS_HINT_FILL);
1594 /* Position subwindow appropriately */
1595 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(obj));
1596 ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
1597 evas_object_geometry_get(obj, &x2, &y2, &w, &h);
1600 evas_object_move(dragwin, x, y);
1601 evas_object_resize(icon, w, h);
1602 evas_object_resize(dragwin, w, h);
1604 evas_object_show(icon);
1605 evas_object_show(dragwin);
1607 evas_pointer_canvas_xy_get(evas_object_evas_get(obj), &x3, &y3);
1615 elm_cnp_tempfile_create(int size)
1619 const char *tmppath;
1622 info = malloc(sizeof(Tmp_Info));
1623 if (!info) return NULL;
1625 tmppath = getenv("TMP");
1626 if (!tmppath) tmppath = P_tmpdir;
1627 len = snprintf(NULL, 0, "%s/%sXXXXXX", tmppath, "elmcnpitem-");
1634 info->filename = malloc(len);
1635 if (!info->filename)
1640 snprintf(info->filename,len,"%s/%sXXXXXX", tmppath, "elmcnpitem-");
1642 info->fd = mkstemp(info->filename);
1647 /* And before someone says anything see POSIX 1003.1-2008 page 400 */
1650 pid = (long)getpid();
1651 /* Use pid instead of /proc/self: That way if can be passed around */
1652 len = snprintf(NULL,0,"/proc/%li/fd/%i", pid, info->fd);
1657 snprintf(tmp,len, "/proc/%li/fd/%i", pid, info->fd);
1658 unlink(info->filename);
1659 free(info->filename);
1660 info->filename = tmp;
1665 cnp_debug("filename is %s\n", info->filename);
1668 /* Set map to NULL and return */
1675 if (ftruncate(info->fd, size))
1677 perror("ftruncate");
1683 eina_mmap_safety_enabled_set(EINA_TRUE);
1685 info->map = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, info->fd, 0);
1686 if (info->map == MAP_FAILED)
1702 tmpinfo_free(Tmp_Info *info)
1704 if (!info) return 0;
1705 free(info->filename);
1711 /* Stubs for windows */
1713 elm_drag_start(Evas_Object *o, Elm_Sel_Format f, const char *d, void (*donecb)(void *, Evas_Object *),void *cbdata)
1719 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
1725 elm_drop_target_del(Evas_Object *o)
1731 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-3f0^-2{2(0W1st0 :*/