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;
819 str = malloc(sizeof(char) * (data->length + 1));
822 strncpy(str, (char *)data->data, data->length);
823 str[data->length] = '\0';
830 Elm_Selection_Data ddata;
832 ddata.x = ddata.y = 0;
833 ddata.format = ELM_SEL_FORMAT_TEXT;
835 ddata.len = data->length;
836 sel->datacb(sel->udata, sel->widget, &ddata);
840 cnp_debug("Notify handler text %d %d %p\n", data->format, data->length, data->data);
841 mkupstr = _elm_util_text_to_mkup((const char *) str);
844 entry_insert_filter(sel->requestwidget, mkupstr);
848 entry_insert_filter(sel->requestwidget, str);
850 cnp_debug("String is %s (from %s)\n", str, data->data);
851 //_elm_entry_entry_paste(sel->requestwidget, str);
854 if (str != data->data)
861 * So someone is pasting an image into my entry or widget...
864 notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
866 Ecore_X_Selection_Data *data;
867 Ecore_X_Selection_Data_Files *files;
871 cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
872 if (data->content == ECORE_X_SELECTION_CONTENT_FILES)
874 cnp_debug("got a files list\n");
875 files = notify->data;
876 if (files->num_files > 1)
878 /* Don't handle many items */
879 cnp_debug("more then one file: Bailing\n");
882 stripstr = p = strdup(files->files[0]);
886 stripstr = p = malloc(data->length * sizeof(char) + 1);
887 memcpy(stripstr, data->data, data->length);
888 stripstr[data->length] = 0;
893 cnp_debug("Couldn't find a file\n");
896 cnp_debug("Got %s\n",p);
899 Elm_Selection_Data ddata;
901 ddata.x = ddata.y = 0;
902 ddata.format = ELM_SEL_FORMAT_IMAGE;
904 ddata.len = data->length;
905 sel->datacb(sel->udata, sel->widget, &ddata);
909 if (strncmp(p, "file://", 7))
911 /* Try and continue if it looks sane */
920 p += strlen("file://");
923 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
924 if (savedtypes.textreq)
926 savedtypes.textreq = 0;
927 savedtypes.imgfile = strdup(p);
931 pasteimage_append(p, sel->requestwidget);
938 * Just receieved an vcard, either through cut and paste, or dnd.
941 vcard_receive(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
945 Ecore_X_Selection_Data *data;
948 cnp_debug("vcard receive\n");
950 if (sel == (selections + ELM_SEL_TYPE_XDND))
952 Elm_Selection_Data ddata;
954 cnp_debug("drag & drop\n");
955 /* FIXME: this needs to be generic: Used for all receives */
956 EINA_LIST_FOREACH(drops, l, dropable)
958 if (dropable->obj == sel->requestwidget) break;
962 cnp_debug("Unable to find drop object");
963 ecore_x_dnd_send_finished();
966 dropable = eina_list_data_get(l);
967 ddata.x = savedtypes.x;
968 ddata.y = savedtypes.y;
969 ddata.format = ELM_SEL_FORMAT_VCARD;
970 ddata.data = data->data;
971 ddata.len = data->length;
972 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
973 ecore_x_dnd_send_finished();
975 else if (sel->datacb)
977 Elm_Selection_Data ddata;
978 ddata.x = ddata.y = 0;
979 ddata.format = ELM_SEL_FORMAT_IMAGE;
980 ddata.data = data->data;
981 ddata.len = data->length;
982 sel->datacb(sel->udata, sel->widget, &ddata);
986 cnp_debug("Paste request\n");
995 notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
997 Ecore_X_Selection_Data *data;
1000 cnp_debug("got a image file!\n");
1001 data = notify->data;
1003 cnp_debug("Size if %d\n", data->length);
1007 Elm_Selection_Data ddata;
1009 ddata.x = ddata.y = 0;
1010 ddata.format = ELM_SEL_FORMAT_IMAGE;
1011 ddata.data = data->data;
1012 ddata.len = data->length;
1013 sel->datacb(sel->udata, sel->widget, &ddata);
1017 /* generate tmp name */
1018 tmp = elm_cnp_tempfile_create(data->length);
1019 memcpy(tmp->map, data->data, data->length);
1020 munmap(tmp->map, data->length);
1022 /* FIXME: Add to paste image data to clean up */
1023 pasteimage_append(tmp->filename, sel->requestwidget);
1030 notify_handler_edje(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1032 Ecore_X_Selection_Data *data;
1034 data = notify->data;
1036 char *stripstr = NULL;
1037 stripstr = malloc(sizeof(char) * (data->length + 1));
1038 strncpy(stripstr, (char *)data->data, data->length);
1039 stripstr[data->length] = '\0';
1043 Elm_Selection_Data ddata;
1044 ddata.x = ddata.y = 0;
1045 ddata.format = ELM_SEL_FORMAT_MARKUP;
1046 ddata.data = stripstr;
1047 ddata.len = data->length;
1048 sel->datacb(sel->udata, sel->widget, &ddata);
1051 entry_insert_filter(sel->requestwidget, stripstr);
1052 //_elm_entry_entry_paste(sel->requestwidget, stripstr);
1054 cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
1060 * Warning: Generic text/html can';t handle it sanely.
1061 * Firefox sends ucs2 (i think).
1062 * chrome sends utf8... blerg
1065 notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1067 Ecore_X_Selection_Data *data;
1069 cnp_debug("Got some HTML: Checking encoding is useful\n");
1070 data = notify->data;
1072 char *stripstr = NULL;
1073 stripstr = malloc(sizeof(char) * (data->length + 1));
1074 strncpy(stripstr, (char *)data->data, data->length);
1075 stripstr[data->length] = '\0';
1079 Elm_Selection_Data ddata;
1080 ddata.x = ddata.y = 0;
1081 ddata.format = ELM_SEL_FORMAT_HTML;
1082 ddata.data = stripstr;
1083 ddata.len = data->length;
1084 sel->datacb(sel->udata, sel->widget, &ddata);
1087 entry_insert_filter(sel->requestwidget, stripstr);
1088 //_elm_entry_entry_paste(sel->requestwidget, stripstr);
1090 cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
1097 text_converter(char *target __UNUSED__, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1101 cnp_debug("text converter\n");
1102 if (!data || (*((unsigned int *)data) > ELM_SEL_TYPE_CLIPBOARD))
1104 if (_get_selection_type(data, size) == ELM_SEL_FORMAT_NONE)
1108 *data_ret = malloc(size * sizeof(char) + 1);
1109 memcpy(*data_ret, data, size);
1110 ((char**)(data_ret))[0][size] = 0;
1112 if (size_ret) *size_ret = size;
1115 sel = selections + *((int *)data);
1116 if (!sel->active) return EINA_TRUE;
1118 if (sel->format & ELM_SEL_FORMAT_MARKUP)
1119 *data_ret = _elm_util_mkup_to_text(sel->selbuf);
1120 else if (sel->format & ELM_SEL_FORMAT_HTML)
1123 Eina_Strbuf *buf = eina_strbuf_new();
1126 eina_strbuf_append(buf, sel->selbuf);
1127 eina_strbuf_replace_all(buf, " ", " ");
1128 text = eina_strbuf_string_steal(buf);
1129 eina_strbuf_free(buf);
1130 *data_ret = _elm_util_mkup_to_text(text);
1134 *data_ret = _elm_util_mkup_to_text(sel->selbuf);
1136 else if (sel->format & ELM_SEL_FORMAT_TEXT)
1138 *data_ret = strdup(sel->selbuf);
1139 *size_ret = strlen(sel->selbuf);
1141 else if (sel->format & ELM_SEL_FORMAT_IMAGE)
1143 cnp_debug("Image %s\n", evas_object_type_get(sel->widget));
1144 cnp_debug("Elm type: %s\n", elm_object_widget_type_get(sel->widget));
1145 evas_object_image_file_get(elm_photocam_internal_image_get(sel->widget),
1146 (const char **)data_ret, NULL);
1147 if (!*data_ret) *data_ret = strdup("No file");
1148 else *data_ret = strdup(*data_ret);
1149 *size_ret = strlen(*data_ret);
1155 general_converter(char *target __UNUSED__, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1157 if (_get_selection_type(data, size) == ELM_SEL_FORMAT_NONE)
1161 *data_ret = malloc(size * sizeof(char) + 1);
1162 memcpy(*data_ret, data, size);
1163 ((char**)(data_ret))[0][size] = 0;
1165 if (size_ret) *size_ret = size;
1170 if (!data || (*((unsigned int *)data) > ELM_SEL_TYPE_CLIPBOARD))
1173 sel = selections + *((int *)data);
1174 if (data_ret) *data_ret = strdup(sel->selbuf);
1175 if (size_ret) *size_ret = strlen(sel->selbuf);
1181 pasteimage_append(char *file, Evas_Object *entry)
1185 static const char *tagstring = "<item absize=240x180 href=file://%s></item>";
1187 if ((!file) || (!entry)) return EINA_FALSE;
1189 len = strlen(tagstring)+strlen(file);
1191 entrytag = alloca(len + 1);
1192 snprintf(entrytag, len + 1, tagstring, file);
1193 _elm_entry_entry_paste(entry, entrytag);
1199 _dnd_enter(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1201 Ecore_X_Event_Xdnd_Enter *enter = ev;
1205 if ((!enter) || (!enter->num_types) || (!enter->types)) return EINA_TRUE;
1207 cnp_debug("Types\n");
1208 savedtypes.ntypes = enter->num_types;
1209 if (savedtypes.types) free(savedtypes.types);
1210 savedtypes.types = malloc(sizeof(char *) * enter->num_types);
1211 if (!savedtypes.types) return EINA_FALSE;
1213 for (i = 0; i < enter->num_types; i++)
1215 savedtypes.types[i] = eina_stringshare_add(enter->types[i]);
1216 cnp_debug("Type is %s %p %p\n", enter->types[i],
1217 savedtypes.types[i],text_uri);
1218 if (savedtypes.types[i] == text_uri)
1220 /* Request it, so we know what it is */
1221 cnp_debug("Sending uri request\n");
1222 savedtypes.textreq = 1;
1223 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1224 savedtypes.imgfile = NULL;
1225 ecore_x_selection_xdnd_request(enter->win, text_uri);
1229 /* FIXME: Find an object and make it current */
1234 _dnd_drop(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1236 struct _Ecore_X_Event_Xdnd_Drop *drop;
1240 Ecore_X_Window xwin;
1241 Elm_Selection_Data ddata;
1247 // check we still have something to drop
1248 if (!drops) return EINA_TRUE;
1250 /* Find any widget in our window; then work out geometry rel to our window */
1251 for (l = drops; l; l = l->next)
1254 xwin = (Ecore_X_Window)ecore_evas_window_get
1255 (ecore_evas_ecore_evas_get(evas_object_evas_get
1257 if (xwin == drop->win) break;
1259 /* didn't find a window */
1260 if (!l) return EINA_TRUE;
1262 /* Calculate real (widget relative) position */
1263 // - window position
1264 // - widget position
1265 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(dropable->obj));
1266 ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
1267 savedtypes.x = drop->position.x - x;
1268 savedtypes.y = drop->position.y - y;
1270 cnp_debug("Drop position is %d,%d\n", savedtypes.x, savedtypes.y);
1272 for (; l; l = l->next)
1275 evas_object_geometry_get(dropable->obj, &x, &y, &w, &h);
1276 if ((savedtypes.x >= x) && (savedtypes.y >= y) &&
1277 (savedtypes.x < x + w) && (savedtypes.y < y + h))
1281 if (!l) return EINA_TRUE; /* didn't find one */
1283 evas_object_geometry_get(dropable->obj, &x, &y, NULL, NULL);
1287 /* Find our type from the previous list */
1288 for (i = 0; i < CNP_N_ATOMS; i++)
1290 for (j = 0; j < savedtypes.ntypes; j++)
1292 if (!strcmp(savedtypes.types[j], atoms[i].name)) goto found;
1296 cnp_debug("Didn't find a target\n");
1300 cnp_debug("Found a target we'd like: %s\n", atoms[i].name);
1301 cnp_debug("0x%x\n",xwin);
1303 if (i == CNP_ATOM_text_urilist)
1305 cnp_debug("We found a URI... (%scached) %s\n",
1306 savedtypes.imgfile ? "" : "not ",
1307 savedtypes.imgfile);
1308 if (savedtypes.imgfile)
1311 static const char *tagstring = "<item absize=240x180 href="
1312 "file://%s></item>";
1313 ddata.x = savedtypes.x;
1314 ddata.y = savedtypes.y;
1316 /* If it's markup that also supports images */
1317 if ((dropable->types & ELM_SEL_FORMAT_MARKUP) &&
1318 (dropable->types & ELM_SEL_FORMAT_IMAGE))
1321 ddata.format = ELM_SEL_FORMAT_MARKUP;
1323 len = strlen(tagstring) + strlen(savedtypes.imgfile);
1324 entrytag = alloca(len + 1);
1325 snprintf(entrytag, len + 1, tagstring, savedtypes.imgfile);
1326 ddata.data = entrytag;
1327 cnp_debug("Insert %s\n", (char *)ddata.data);
1328 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1329 ecore_x_dnd_send_finished();
1330 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1331 savedtypes.imgfile = NULL;
1335 else if (dropable->types & ELM_SEL_FORMAT_IMAGE)
1337 cnp_debug("Doing image insert (%s)\n", savedtypes.imgfile);
1338 ddata.format = ELM_SEL_FORMAT_IMAGE;
1339 ddata.data = (char *)savedtypes.imgfile;
1340 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1341 ecore_x_dnd_send_finished();
1342 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1343 savedtypes.imgfile = NULL;
1349 cnp_debug("Item doesn't support images... passing\n");
1353 else if (savedtypes.textreq)
1355 /* Already asked: Pretend we asked now, and paste immediately when
1357 savedtypes.textreq = 0;
1358 ecore_x_dnd_send_finished();
1363 cnp_debug("doing a request then\n");
1364 selections[ELM_SEL_TYPE_XDND].requestwidget = dropable->obj;
1365 selections[ELM_SEL_TYPE_XDND].requestformat = ELM_SEL_FORMAT_MARKUP;
1366 selections[ELM_SEL_TYPE_XDND].active = EINA_TRUE;
1368 ecore_x_selection_xdnd_request(xwin, atoms[i].name);
1373 _dnd_position(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1375 struct _Ecore_X_Event_Xdnd_Position *pos;
1376 Ecore_X_Rectangle rect;
1380 /* Need to send a status back */
1381 /* FIXME: Should check I can drop here */
1382 /* FIXME: Should highlight widget */
1383 rect.x = pos->position.x - 5;
1384 rect.y = pos->position.y - 5;
1387 ecore_x_dnd_send_status(EINA_TRUE, EINA_FALSE, rect, pos->action);
1393 * When dragging this is callback response from the destination.
1394 * The important thing we care about: Can we drop; thus update cursor
1398 _dnd_status(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1400 struct _Ecore_X_Event_Xdnd_Status *status = ev;
1402 if (!status) return EINA_TRUE;
1404 /* Only thing we care about: will accept */
1405 if (status->will_accept)
1407 cnp_debug("Will accept\n");
1410 { /* Won't accept */
1411 cnp_debug("Won't accept accept\n");
1417 * Add a widget as drop target.
1420 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
1423 Ecore_X_Window xwin;
1428 if (!obj) return EINA_FALSE;
1429 top = elm_widget_top_get(obj);
1430 if (!top || !elm_win_xwindow_get(top)) return EINA_FALSE;
1431 if (!_elm_cnp_init_count) _elm_cnp_init();
1433 /* Is this the first? */
1434 first = (!drops) ? 1 : 0;
1436 EINA_LIST_FOREACH(drops, item, drop)
1438 if (drop->obj == obj)
1440 /* Update: Not a new one */
1441 drop->dropcb = dropcb;
1442 drop->cbdata = cbdata;
1443 drop->types = format;
1448 /* Create new drop */
1449 drop = calloc(1, sizeof(Dropable));
1450 if (!drop) return EINA_FALSE;
1451 /* FIXME: Check for eina's deranged error method */
1452 drops = eina_list_append(drops, drop);
1454 if (!drops/* || or other error */)
1459 drop->dropcb = dropcb;
1460 drop->cbdata = cbdata;
1461 drop->types = format;
1464 evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL,
1465 /* I love C and varargs */
1466 (Evas_Object_Event_Cb)elm_drop_target_del,
1468 /* FIXME: Handle resizes */
1470 /* If not the first: We're done */
1471 if (!first) return EINA_TRUE;
1473 xwin = (Ecore_X_Window)ecore_evas_window_get
1474 (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
1476 ecore_x_dnd_aware_set(xwin, EINA_TRUE);
1478 cnp_debug("Adding drop target calls\n");
1479 handler_enter = ecore_event_handler_add(ECORE_X_EVENT_XDND_ENTER,
1481 handler_pos = ecore_event_handler_add(ECORE_X_EVENT_XDND_POSITION,
1482 _dnd_position, NULL);
1483 handler_drop = ecore_event_handler_add(ECORE_X_EVENT_XDND_DROP,
1490 elm_drop_target_del(Evas_Object *obj)
1492 Dropable *drop,*del;
1494 Ecore_X_Window xwin;
1497 EINA_LIST_FOREACH(drops, item, drop)
1499 if (drop->obj == obj)
1501 drops = eina_list_remove_list(drops, item);
1506 if (!del) return EINA_FALSE;
1508 evas_object_event_callback_del(obj, EVAS_CALLBACK_FREE,
1509 (Evas_Object_Event_Cb)elm_drop_target_del);
1511 /* If still drops there: All fine.. continue */
1512 if (drops) return EINA_TRUE;
1514 cnp_debug("Disabling DND\n");
1515 xwin = (Ecore_X_Window)ecore_evas_window_get
1516 (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
1517 ecore_x_dnd_aware_set(xwin, EINA_FALSE);
1519 ecore_event_handler_del(handler_pos);
1520 ecore_event_handler_del(handler_drop);
1521 ecore_event_handler_del(handler_enter);
1523 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1524 savedtypes.imgfile = NULL;
1531 _drag_mouse_up(void *un __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *data)
1533 Ecore_X_Window xwin = *((Ecore_X_Window *)data);
1534 evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_UP, _drag_mouse_up);
1536 ecore_x_dnd_aware_set(xwin, EINA_FALSE);
1539 dragdonecb(dragdonedata, selections[ELM_SEL_TYPE_XDND].widget);
1544 evas_object_del(dragwin);
1550 _drag_move(void *data __UNUSED__, Ecore_X_Xdnd_Position *pos)
1552 evas_object_move(dragwin,
1553 pos->position.x - _dragx,
1554 pos->position.y - _dragy);
1559 elm_drag_start(Evas_Object *obj, Elm_Sel_Format format, const char *data, void (*dragdone) (void *data, Evas_Object *), void *donecbdata)
1561 Ecore_X_Window xwin;
1563 Elm_Sel_Type xdnd = ELM_SEL_TYPE_XDND;
1565 int x, y, x2, y2, x3, y3;
1569 if (!_elm_cnp_init_count) _elm_cnp_init();
1571 xwin = elm_win_xwindow_get(obj);
1573 cnp_debug("starting drag...\n");
1577 cnp_debug("another obj is dragging...\n");
1581 ecore_x_dnd_type_set(xwin, "text/uri-list", EINA_TRUE);
1582 sel = selections + ELM_SEL_TYPE_XDND;
1583 sel->active = EINA_TRUE;
1585 sel->format = format;
1586 sel->selbuf = data ? strdup(data) : NULL;
1587 dragdonecb = dragdone;
1588 dragdonedata = donecbdata;
1590 ecore_x_dnd_aware_set(xwin, EINA_TRUE);
1591 ecore_x_dnd_callback_pos_update_set(_drag_move, NULL);
1592 ecore_x_dnd_begin(xwin, (unsigned char *)&xdnd, sizeof(Elm_Sel_Type));
1593 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
1594 _drag_mouse_up, (void *)(long)xwin);
1596 handler_status = ecore_event_handler_add(ECORE_X_EVENT_XDND_STATUS,
1599 dragwin = elm_win_add(NULL, "Elm Drag Object", ELM_WIN_UTILITY);
1600 elm_win_override_set(dragwin, EINA_TRUE);
1602 /* FIXME: Images only */
1603 icon = elm_icon_add(dragwin);
1604 elm_icon_file_set(icon, data + 7, NULL); /* 7!? "file://" */
1605 elm_win_resize_object_add(dragwin, icon);
1606 evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1607 evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, EVAS_HINT_FILL);
1609 /* Position subwindow appropriately */
1610 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(obj));
1611 ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
1612 evas_object_geometry_get(obj, &x2, &y2, &w, &h);
1615 evas_object_move(dragwin, x, y);
1616 evas_object_resize(icon, w, h);
1617 evas_object_resize(dragwin, w, h);
1619 evas_object_show(icon);
1620 evas_object_show(dragwin);
1622 evas_pointer_canvas_xy_get(evas_object_evas_get(obj), &x3, &y3);
1630 elm_cnp_tempfile_create(int size)
1634 const char *tmppath;
1637 info = malloc(sizeof(Tmp_Info));
1638 if (!info) return NULL;
1640 tmppath = getenv("TMP");
1641 if (!tmppath) tmppath = P_tmpdir;
1642 len = snprintf(NULL, 0, "%s/%sXXXXXX", tmppath, "elmcnpitem-");
1649 info->filename = malloc(len);
1650 if (!info->filename)
1655 snprintf(info->filename,len,"%s/%sXXXXXX", tmppath, "elmcnpitem-");
1657 info->fd = mkstemp(info->filename);
1662 /* And before someone says anything see POSIX 1003.1-2008 page 400 */
1665 pid = (long)getpid();
1666 /* Use pid instead of /proc/self: That way if can be passed around */
1667 len = snprintf(NULL,0,"/proc/%li/fd/%i", pid, info->fd);
1672 snprintf(tmp,len, "/proc/%li/fd/%i", pid, info->fd);
1673 unlink(info->filename);
1674 free(info->filename);
1675 info->filename = tmp;
1680 cnp_debug("filename is %s\n", info->filename);
1683 /* Set map to NULL and return */
1690 if (ftruncate(info->fd, size))
1692 perror("ftruncate");
1698 eina_mmap_safety_enabled_set(EINA_TRUE);
1700 info->map = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, info->fd, 0);
1701 if (info->map == MAP_FAILED)
1717 tmpinfo_free(Tmp_Info *info)
1719 if (!info) return 0;
1720 free(info->filename);
1726 /* Stubs for windows */
1728 elm_drag_start(Evas_Object *o, Elm_Sel_Format f, const char *d, void (*donecb)(void *, Evas_Object *),void *cbdata)
1734 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
1740 elm_drop_target_del(Evas_Object *o)
1746 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-3f0^-2{2(0W1st0 :*/