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_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
146 static int vcard_receive(Cnp_Selection *sed, Ecore_X_Event_Selection_Notify *notify);
148 static Eina_Bool pasteimage_append(char *file, Evas_Object *entry);
150 static Cnp_Atom atoms[CNP_N_ATOMS] = {
151 [CNP_ATOM_TARGETS] = {
153 ELM_SEL_FORMAT_TARGETS,
155 response_handler_targets,
156 notify_handler_targets,
160 "ATOM", // for opera browser
161 ELM_SEL_FORMAT_TARGETS,
163 response_handler_targets,
164 notify_handler_targets,
168 "application/x-elementary-markup",
169 ELM_SEL_FORMAT_MARKUP,
175 [CNP_ATOM_text_uri] = {
177 ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_IMAGE, /* Either images or entries */
183 [CNP_ATOM_text_urilist] = {
185 ELM_SEL_FORMAT_IMAGE,
191 [CNP_ATOM_text_x_vcard] = {
193 ELM_SEL_FORMAT_VCARD,
197 [CNP_ATOM_image_png] = {
199 ELM_SEL_FORMAT_IMAGE,
202 notify_handler_image,
205 [CNP_ATOM_image_jpeg] = {
207 ELM_SEL_FORMAT_IMAGE,
210 notify_handler_image,/* Raw image data is the same */
213 [CNP_ATOM_image_bmp] = {
215 ELM_SEL_FORMAT_IMAGE,
218 notify_handler_image,/* Raw image data is the same */
221 [CNP_ATOM_image_gif] = {
223 ELM_SEL_FORMAT_IMAGE,
226 notify_handler_image,/* Raw image data is the same */
229 [CNP_ATOM_image_tiff] = {
231 ELM_SEL_FORMAT_IMAGE,
234 notify_handler_image,/* Raw image data is the same */
237 [CNP_ATOM_image_svg] = {
239 ELM_SEL_FORMAT_IMAGE,
242 notify_handler_image,/* Raw image data is the same */
245 [CNP_ATOM_image_xpm] = {
247 ELM_SEL_FORMAT_IMAGE,
250 notify_handler_image,/* Raw image data is the same */
253 [CNP_ATOM_image_tga] = {
255 ELM_SEL_FORMAT_IMAGE,
258 notify_handler_image,/* Raw image data is the same */
261 [CNP_ATOM_image_ppm] = {
262 "image/x-portable-pixmap",
263 ELM_SEL_FORMAT_IMAGE,
266 notify_handler_image,/* Raw image data is the same */
269 [CNP_ATOM_text_html_utf8] = {
270 "text/html;charset=utf-8",
277 [CNP_ATOM_text_html] = {
282 notify_handler_html, /* No encoding: Webkit only */
285 [CNP_ATOM_UTF8STRING] = {
287 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
293 [CNP_ATOM_STRING] = {
295 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
303 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
309 [CNP_ATOM_text_plain_utf8] = {
310 "text/plain;charset=utf-8",
311 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
317 [CNP_ATOM_text_plain] = {
319 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
327 static Cnp_Selection selections[ELM_SEL_TYPE_CLIPBOARD + 1] = {
328 ARRAYINIT(ELM_SEL_TYPE_PRIMARY) {
330 .ecore_sel = ECORE_X_SELECTION_PRIMARY,
331 .set = ecore_x_selection_primary_set,
332 .clear = ecore_x_selection_primary_clear,
333 .request = ecore_x_selection_primary_request,
335 ARRAYINIT(ELM_SEL_TYPE_SECONDARY) {
336 .debug = "Secondary",
337 .ecore_sel = ECORE_X_SELECTION_SECONDARY,
338 .set = ecore_x_selection_secondary_set,
339 .clear = ecore_x_selection_secondary_clear,
340 .request = ecore_x_selection_secondary_request,
342 ARRAYINIT(ELM_SEL_TYPE_XDND) {
344 .ecore_sel = ECORE_X_SELECTION_XDND,
345 .request = ecore_x_selection_xdnd_request,
347 ARRAYINIT(ELM_SEL_TYPE_CLIPBOARD) {
348 .debug = "Clipboard",
349 .ecore_sel = ECORE_X_SELECTION_CLIPBOARD,
350 .set = ecore_x_selection_clipboard_set,
351 .clear = ecore_x_selection_clipboard_clear,
352 .request = ecore_x_selection_clipboard_request,
356 /* Data for DND in progress */
357 static Saved_Type savedtypes = { NULL, NULL, 0, 0, 0, EINA_FALSE };
359 static void (*dragdonecb) (void *data, Evas_Object *obj) = NULL;
360 static void *dragdonedata = NULL;
362 static int _elm_cnp_init_count = 0;
365 * Drag & Drop functions
368 /* FIXME: Way too many globals */
369 static Eina_List *drops = NULL;
370 static Evas_Object *dragwin = NULL;
371 static int _dragx = 0, _dragy = 0;
372 static Ecore_Event_Handler *handler_pos = NULL;
373 static Ecore_Event_Handler *handler_drop = NULL;
374 static Ecore_Event_Handler *handler_enter = NULL;
375 static Ecore_Event_Handler *handler_status = NULL;
379 /* Stringshared, so I can just compare pointers later */
380 static const char *text_uri;
383 elm_selection_selection_has_owner(void)
385 #ifdef HAVE_ELEMENTARY_X
386 return !!ecore_x_selection_owner_get(ECORE_X_ATOM_SELECTION_CLIPBOARD);
393 elm_cnp_selection_set(Evas_Object *obj, Elm_Sel_Type selection,
394 Elm_Sel_Format format, const void *selbuf, size_t buflen)
396 #ifdef HAVE_ELEMENTARY_X
397 Evas_Object *top = elm_widget_top_get(obj);
401 if (top) xwin = elm_win_xwindow_get(top);
402 else xwin = elm_win_xwindow_get(obj);
403 if ((!xwin) || (selection > ELM_SEL_TYPE_CLIPBOARD))
405 if (!_elm_cnp_init_count) _elm_cnp_init();
406 if ((!selbuf) && (format != ELM_SEL_FORMAT_IMAGE))
407 return elm_object_cnp_selection_clear(obj, selection);
409 sel = selections + selection;
411 sel->active = EINA_TRUE;
413 sel->set(xwin, &selection, sizeof(Elm_Sel_Type));
414 sel->format = format;
418 if (format == ELM_SEL_FORMAT_IMAGE)
420 sel->selbuf = malloc(buflen+1);
421 memcpy(sel->selbuf, selbuf, buflen);
424 sel->selbuf = strdup((char*)selbuf);
436 elm_object_cnp_selection_clear(Evas_Object *obj, Elm_Sel_Type selection)
438 #ifdef HAVE_ELEMENTARY_X
441 if ((unsigned int)selection > (unsigned int)ELM_SEL_TYPE_CLIPBOARD)
443 if (!_elm_cnp_init_count) _elm_cnp_init();
445 sel = selections + selection;
447 /* No longer this selection: Consider it gone! */
448 if ((!sel->active) || (sel->widget != obj)) return EINA_TRUE;
450 sel->active = EINA_FALSE;
466 elm_cnp_selection_get(Evas_Object *obj, Elm_Sel_Type selection,
467 Elm_Sel_Format format, Elm_Drop_Cb datacb, void *udata)
469 #ifdef HAVE_ELEMENTARY_X
473 if (selection > ELM_SEL_TYPE_CLIPBOARD)
475 if (!_elm_cnp_init_count) _elm_cnp_init();
477 sel = selections + selection;
478 top = elm_widget_top_get(obj);
479 if (!top) return EINA_FALSE;
481 sel->requestformat = format;
482 sel->requestwidget = obj;
483 sel->request(elm_win_xwindow_get(top), ECORE_X_SELECTION_TARGET_TARGETS);
484 sel->datacb = datacb;
493 #ifdef HAVE_ELEMENTARY_X
500 if (_elm_cnp_init_count++) return EINA_TRUE;
501 for (i = 0; i < CNP_N_ATOMS; i++)
503 atoms[i].atom = ecore_x_atom_get(atoms[i].name);
504 ecore_x_selection_converter_atom_add(atoms[i].atom,
508 ecore_event_handler_add(ECORE_X_EVENT_SELECTION_CLEAR, selection_clear, NULL);
509 ecore_event_handler_add(ECORE_X_EVENT_SELECTION_NOTIFY, selection_notify, NULL);
511 text_uri = eina_stringshare_add("text/uri-list");
516 selection_clear(void *udata __UNUSED__, int type __UNUSED__, void *event)
518 Ecore_X_Event_Selection_Clear *ev = event;
522 for (i = 0; i <= ELM_SEL_TYPE_CLIPBOARD; i++)
524 if (selections[i].ecore_sel == ev->selection) break;
526 cnp_debug("selection %d clear\n", i);
527 /* Not me... Don't care */
528 if (i > ELM_SEL_TYPE_CLIPBOARD) return ECORE_CALLBACK_PASS_ON;
530 sel = selections + i;
531 sel->active = EINA_FALSE;
539 return ECORE_CALLBACK_PASS_ON;
544 * Response to a selection notify:
545 * - So we have asked for the selection list.
546 * - If it's the targets list, parse it, and fire of what we want,
547 * else it's the data we want.
550 selection_notify(void *udata __UNUSED__, int type __UNUSED__, void *event)
552 Ecore_X_Event_Selection_Notify *ev = event;
556 cnp_debug("selection notify callback: %d\n",ev->selection);
557 switch (ev->selection)
559 case ECORE_X_SELECTION_PRIMARY:
560 sel = selections + ELM_SEL_TYPE_PRIMARY;
562 case ECORE_X_SELECTION_SECONDARY:
563 sel = selections + ELM_SEL_TYPE_SECONDARY;
565 case ECORE_X_SELECTION_XDND:
566 sel = selections + ELM_SEL_TYPE_XDND;
568 case ECORE_X_SELECTION_CLIPBOARD:
569 sel = selections + ELM_SEL_TYPE_CLIPBOARD;
572 return ECORE_CALLBACK_PASS_ON;
574 cnp_debug("Target is %s\n", ev->target);
576 for (i = 0; i < CNP_N_ATOMS; i++)
578 if (!strcmp(ev->target, atoms[i].name))
582 cnp_debug("Found something: %s\n", atoms[i].name);
583 atoms[i].notify(sel, ev);
587 cnp_debug("Ignored: No handler!\n");
592 return ECORE_CALLBACK_PASS_ON;
595 static Elm_Sel_Format
596 _get_selection_type(void *data, int size)
598 if (size == sizeof(Elm_Sel_Type))
600 Cnp_Selection *sel = selections + *((int *)data);
602 (sel->format >= ELM_SEL_FORMAT_TARGETS) &&
603 (sel->format <= ELM_SEL_FORMAT_HTML))
606 return ELM_SEL_FORMAT_NONE;
610 targets_converter(char *target __UNUSED__, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize)
615 Elm_Sel_Format seltype;
617 if (!data_ret) return EINA_FALSE;
619 if (_get_selection_type(data, size) == ELM_SEL_FORMAT_NONE)
621 /* TODO : fallback into precise type */
622 seltype = ELM_SEL_FORMAT_TEXT;
626 sel = selections + *((int *)data);
627 seltype = sel->format;
630 for (i = 0, count = 0; i < CNP_N_ATOMS ; i++)
632 if (seltype & atoms[i].formats) count++;
635 aret = malloc(sizeof(Ecore_X_Atom) * count);
636 for (i = 0, count = 0; i < CNP_N_ATOMS; i++)
638 if (seltype & atoms[i].formats) aret[count ++] = atoms[i].atom;
642 if (typesize) *typesize = 32 /* urk */;
643 if (ttype) *ttype = ECORE_X_ATOM_ATOM;
644 if (size_ret) *size_ret = count;
650 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__)
652 cnp_debug("Image converter called\n");
657 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__)
661 cnp_debug("Vcard send called\n");
663 sel = selections + *((int *)data);
665 if (data_ret) *data_ret = strdup(sel->selbuf);
666 if (size_ret) *size_ret = strlen(sel->selbuf);
672 is_uri_type_data(Cnp_Selection *sel __UNUSED__, Ecore_X_Event_Selection_Notify *notify)
674 Ecore_X_Selection_Data *data;
678 cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
679 if (data->content == ECORE_X_SELECTION_CONTENT_FILES) return EINA_TRUE;
680 p = (char *)data->data;
681 if (!p) return EINA_TRUE;
683 cnp_debug("Got %s\n", p);
684 if (strncmp(p, "file://", 7))
686 if (*p != '/') return EINA_FALSE;
693 * Callback to handle a targets response on a selection request:
694 * So pick the format we'd like; and then request it.
697 notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
699 Ecore_X_Selection_Data_Targets *targets;
700 Ecore_X_Atom *atomlist;
704 targets = notify->data;
705 atomlist = (Ecore_X_Atom *)(targets->data.data);
707 for (j = (CNP_ATOM_LISTING_ATOMS+1); j < CNP_N_ATOMS; j++)
709 cnp_debug("\t%s %d\n", atoms[j].name, atoms[j].atom);
710 if (!(atoms[j].formats & sel->requestformat)) continue;
711 for (i = 0; i < targets->data.length; i++)
713 if ((atoms[j].atom == atomlist[i]) && (atoms[j].notify))
715 if ((j == CNP_ATOM_text_uri) ||
716 (j == CNP_ATOM_text_urilist))
718 if (!is_uri_type_data(sel, notify)) continue;
720 cnp_debug("Atom %s matches\n",atoms[j].name);
726 cnp_debug("Couldn't find anything that matches\n");
727 return ECORE_CALLBACK_PASS_ON;
730 top = elm_widget_top_get(sel->requestwidget);
731 if (!top) top = sel->requestwidget;
732 cnp_debug("Sending request for %s\n", atoms[j].name);
733 sel->request(elm_win_xwindow_get(top), atoms[j].name);
735 return ECORE_CALLBACK_PASS_ON;
739 response_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
741 Ecore_X_Selection_Data_Targets *targets;
742 Ecore_X_Atom *atomlist;
746 targets = notify->data;
747 atomlist = (Ecore_X_Atom *)(targets->data.data);
749 for (j = (CNP_ATOM_LISTING_ATOMS+1); j < CNP_N_ATOMS; j++)
751 if (!(atoms[j].formats & sel->requestformat)) continue;
752 for (i = 0 ; i < targets->data.length ; i ++)
754 if ((atoms[j].atom == atomlist[i]) && (atoms[j].response))
756 /* Found a match: Use it */
761 cnp_debug("No matching type found\n");
765 top = elm_widget_top_get(sel->requestwidget);
768 sel->request(elm_win_xwindow_get(top), atoms[j].name);
774 notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
776 Ecore_X_Selection_Data *data;
781 str = malloc(sizeof(char) * (data->length + 1));
782 strncpy(str, (char *)data->data, data->length);
783 str[data->length] = '\0';
787 Elm_Selection_Data ddata;
789 ddata.x = ddata.y = 0;
790 ddata.format = ELM_SEL_FORMAT_TEXT;
791 ddata.data = data->data;
792 ddata.len = data->length;
793 sel->datacb(sel->udata, sel->widget, &ddata);
797 cnp_debug("Notify handler text %d %d %p\n", data->format,data->length, data->data);
798 mkupstr = _elm_util_text_to_mkup((const char *) str);
799 cnp_debug("String is %s (from %s)\n", str, data->data);
800 _elm_entry_entry_paste(sel->requestwidget, mkupstr);
808 * So someone is pasting an image into my entry or widget...
811 notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
813 Ecore_X_Selection_Data *data;
814 Ecore_X_Selection_Data_Files *files;
818 cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
819 if (data->content == ECORE_X_SELECTION_CONTENT_FILES)
821 cnp_debug("got a files list\n");
822 files = notify->data;
823 if (files->num_files > 1)
825 /* Don't handle many items */
826 cnp_debug("more then one file: Bailing\n");
829 stripstr = p = strdup(files->files[0]);
833 stripstr = p = malloc(data->length * sizeof(char) + 1);
834 memcpy(stripstr, data->data, data->length);
835 stripstr[data->length] = 0;
840 cnp_debug("Couldn't find a file\n");
843 cnp_debug("Got %s\n",p);
844 if (strncmp(p, "file://", 7))
846 /* Try and continue if it looks sane */
855 p += strlen("file://");
858 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
859 if (savedtypes.textreq)
861 savedtypes.textreq = 0;
862 savedtypes.imgfile = strdup(p);
866 pasteimage_append(p, sel->requestwidget);
873 * Just receieved an vcard, either through cut and paste, or dnd.
876 vcard_receive(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
880 Ecore_X_Selection_Data *data;
883 cnp_debug("vcard receive\n");
885 if (sel == (selections + ELM_SEL_TYPE_XDND))
887 Elm_Selection_Data ddata;
889 cnp_debug("drag & drop\n");
890 /* FIXME: this needs to be generic: Used for all receives */
891 EINA_LIST_FOREACH(drops, l, dropable)
893 if (dropable->obj == sel->requestwidget) break;
897 cnp_debug("Unable to find drop object");
898 ecore_x_dnd_send_finished();
901 dropable = eina_list_data_get(l);
902 ddata.x = savedtypes.x;
903 ddata.y = savedtypes.y;
904 ddata.format = ELM_SEL_FORMAT_VCARD;
905 ddata.data = data->data;
906 ddata.len = data->length;
907 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
908 ecore_x_dnd_send_finished();
910 else if (sel->datacb)
912 Elm_Selection_Data ddata;
913 ddata.x = ddata.y = 0;
914 ddata.format = ELM_SEL_FORMAT_IMAGE;
915 ddata.data = data->data;
916 ddata.len = data->length;
917 sel->datacb(sel->udata, sel->widget, &ddata);
921 cnp_debug("Paste request\n");
930 notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
932 Ecore_X_Selection_Data *data;
935 cnp_debug("got a image file!\n");
938 cnp_debug("Size if %d\n", data->length);
942 Elm_Selection_Data ddata;
944 ddata.x = ddata.y = 0;
945 ddata.format = ELM_SEL_FORMAT_IMAGE;
946 ddata.data = data->data;
947 ddata.len = data->length;
948 sel->datacb(sel->udata, sel->widget, &ddata);
952 /* generate tmp name */
953 tmp = elm_cnp_tempfile_create(data->length);
954 memcpy(tmp->map, data->data, data->length);
955 munmap(tmp->map, data->length);
957 /* FIXME: Add to paste image data to clean up */
958 pasteimage_append(tmp->filename, sel->requestwidget);
966 * Warning: Generic text/html can';t handle it sanely.
967 * Firefox sends ucs2 (i think).
968 * chrome sends utf8... blerg
971 notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
973 Ecore_X_Selection_Data *data;
975 cnp_debug("Got some HTML: Checking encoding is useful\n");
980 Elm_Selection_Data ddata;
981 ddata.x = ddata.y = 0;
982 ddata.format = ELM_SEL_FORMAT_HTML;
983 ddata.data = data->data;
984 ddata.len = data->length;
985 sel->datacb(sel->udata, sel->widget, &ddata);
989 char *stripstr = malloc(sizeof(char) * (data->length + 1));
990 strncpy(stripstr, (char *)data->data, data->length);
991 stripstr[data->length] = '\0';
992 cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
993 _elm_entry_entry_paste(sel->requestwidget, stripstr);
1000 text_converter(char *target __UNUSED__, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1004 cnp_debug("text converter\n");
1005 if (_get_selection_type(data, size) == ELM_SEL_FORMAT_NONE)
1009 *data_ret = malloc(size * sizeof(char) + 1);
1010 memcpy(*data_ret, data, size);
1011 ((char**)(data_ret))[0][size] = 0;
1013 if (size_ret) *size_ret = size;
1016 sel = selections + *((int *)data);
1017 if (!sel->active) return EINA_TRUE;
1019 if ((sel->format & ELM_SEL_FORMAT_MARKUP) ||
1020 (sel->format & ELM_SEL_FORMAT_HTML))
1022 *data_ret = _elm_util_mkup_to_text(sel->selbuf);
1023 if (size_ret) *size_ret = strlen(*data_ret);
1025 else if (sel->format & ELM_SEL_FORMAT_TEXT)
1027 *data_ret = strdup(sel->selbuf);
1028 *size_ret = strlen(sel->selbuf);
1030 else if (sel->format & ELM_SEL_FORMAT_IMAGE)
1032 cnp_debug("Image %s\n", evas_object_type_get(sel->widget));
1033 cnp_debug("Elm type: %s\n", elm_object_widget_type_get(sel->widget));
1034 evas_object_image_file_get(elm_photocam_internal_image_get(sel->widget),
1035 (const char **)data_ret, NULL);
1036 if (!*data_ret) *data_ret = strdup("No file");
1037 else *data_ret = strdup(*data_ret);
1038 *size_ret = strlen(*data_ret);
1044 general_converter(char *target __UNUSED__, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1046 if (_get_selection_type(data, size) == ELM_SEL_FORMAT_NONE)
1050 *data_ret = malloc(size * sizeof(char) + 1);
1051 memcpy(*data_ret, data, size);
1052 ((char**)(data_ret))[0][size] = 0;
1054 if (size_ret) *size_ret = size;
1058 Cnp_Selection *sel = selections + *((int *)data);
1059 if (data_ret) *data_ret = strdup(sel->selbuf);
1060 if (size_ret) *size_ret = strlen(sel->selbuf);
1066 pasteimage_append(char *file, Evas_Object *entry)
1070 static const char *tagstring = "<item absize=240x180 href=file://%s></item>";
1072 if ((!file) || (!entry)) return EINA_FALSE;
1074 len = strlen(tagstring)+strlen(file);
1076 entrytag = alloca(len + 1);
1077 snprintf(entrytag, len + 1, tagstring, file);
1078 _elm_entry_entry_paste(entry, entrytag);
1084 _dnd_enter(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1086 Ecore_X_Event_Xdnd_Enter *enter = ev;
1090 if ((!enter) || (!enter->num_types) || (!enter->types)) return EINA_TRUE;
1092 cnp_debug("Types\n");
1093 savedtypes.ntypes = enter->num_types;
1094 if (savedtypes.types) free(savedtypes.types);
1095 savedtypes.types = malloc(sizeof(char *) * enter->num_types);
1096 if (!savedtypes.types) return EINA_FALSE;
1098 for (i = 0; i < enter->num_types; i++)
1100 savedtypes.types[i] = eina_stringshare_add(enter->types[i]);
1101 cnp_debug("Type is %s %p %p\n", enter->types[i],
1102 savedtypes.types[i],text_uri);
1103 if (savedtypes.types[i] == text_uri)
1105 /* Request it, so we know what it is */
1106 cnp_debug("Sending uri request\n");
1107 savedtypes.textreq = 1;
1108 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1109 savedtypes.imgfile = NULL;
1110 ecore_x_selection_xdnd_request(enter->win, text_uri);
1114 /* FIXME: Find an object and make it current */
1119 _dnd_drop(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1121 struct _Ecore_X_Event_Xdnd_Drop *drop;
1125 Ecore_X_Window xwin;
1126 Elm_Selection_Data ddata;
1132 // check we still have something to drop
1133 if (!drops) return EINA_TRUE;
1135 /* Find any widget in our window; then work out geometry rel to our window */
1136 for (l = drops; l; l = l->next)
1139 xwin = (Ecore_X_Window)ecore_evas_window_get
1140 (ecore_evas_ecore_evas_get(evas_object_evas_get
1142 if (xwin == drop->win) break;
1144 /* didn't find a window */
1145 if (!l) return EINA_TRUE;
1147 /* Calculate real (widget relative) position */
1148 // - window position
1149 // - widget position
1150 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(dropable->obj));
1151 ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
1152 savedtypes.x = drop->position.x - x;
1153 savedtypes.y = drop->position.y - y;
1155 cnp_debug("Drop position is %d,%d\n", savedtypes.x, savedtypes.y);
1157 for (; l; l = l->next)
1160 evas_object_geometry_get(dropable->obj, &x, &y, &w, &h);
1161 if ((savedtypes.x >= x) && (savedtypes.y >= y) &&
1162 (savedtypes.x < x + w) && (savedtypes.y < y + h))
1166 if (!l) return EINA_TRUE; /* didn't find one */
1168 evas_object_geometry_get(dropable->obj, &x, &y, NULL, NULL);
1172 /* Find our type from the previous list */
1173 for (i = 0; i < CNP_N_ATOMS; i++)
1175 for (j = 0; j < savedtypes.ntypes; j++)
1177 if (!strcmp(savedtypes.types[j], atoms[i].name)) goto found;
1181 cnp_debug("Didn't find a target\n");
1185 cnp_debug("Found a target we'd like: %s\n", atoms[i].name);
1186 cnp_debug("0x%x\n",xwin);
1188 if (i == CNP_ATOM_text_urilist)
1190 cnp_debug("We found a URI... (%scached) %s\n",
1191 savedtypes.imgfile ? "" : "not ",
1192 savedtypes.imgfile);
1193 if (savedtypes.imgfile)
1196 static const char *tagstring = "<item absize=240x180 href="
1197 "file://%s></item>";
1198 ddata.x = savedtypes.x;
1199 ddata.y = savedtypes.y;
1201 /* If it's markup that also supports images */
1202 if ((dropable->types & ELM_SEL_FORMAT_MARKUP) &&
1203 (dropable->types & ELM_SEL_FORMAT_IMAGE))
1206 ddata.format = ELM_SEL_FORMAT_MARKUP;
1208 len = strlen(tagstring) + strlen(savedtypes.imgfile);
1209 entrytag = alloca(len + 1);
1210 snprintf(entrytag, len + 1, tagstring, savedtypes.imgfile);
1211 ddata.data = entrytag;
1212 cnp_debug("Insert %s\n", (char *)ddata.data);
1213 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1214 ecore_x_dnd_send_finished();
1215 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1216 savedtypes.imgfile = NULL;
1220 else if (dropable->types & ELM_SEL_FORMAT_IMAGE)
1222 cnp_debug("Doing image insert (%s)\n", savedtypes.imgfile);
1223 ddata.format = ELM_SEL_FORMAT_IMAGE;
1224 ddata.data = (char *)savedtypes.imgfile;
1225 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1226 ecore_x_dnd_send_finished();
1227 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1228 savedtypes.imgfile = NULL;
1234 cnp_debug("Item doesn't support images... passing\n");
1238 else if (savedtypes.textreq)
1240 /* Already asked: Pretend we asked now, and paste immediately when
1242 savedtypes.textreq = 0;
1243 ecore_x_dnd_send_finished();
1248 cnp_debug("doing a request then\n");
1249 selections[ELM_SEL_TYPE_XDND].requestwidget = dropable->obj;
1250 selections[ELM_SEL_TYPE_XDND].requestformat = ELM_SEL_FORMAT_MARKUP;
1251 selections[ELM_SEL_TYPE_XDND].active = EINA_TRUE;
1253 ecore_x_selection_xdnd_request(xwin, atoms[i].name);
1258 _dnd_position(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1260 struct _Ecore_X_Event_Xdnd_Position *pos;
1261 Ecore_X_Rectangle rect;
1265 /* Need to send a status back */
1266 /* FIXME: Should check I can drop here */
1267 /* FIXME: Should highlight widget */
1268 rect.x = pos->position.x - 5;
1269 rect.y = pos->position.y - 5;
1272 ecore_x_dnd_send_status(EINA_TRUE, EINA_FALSE, rect, pos->action);
1278 * When dragging this is callback response from the destination.
1279 * The important thing we care about: Can we drop; thus update cursor
1283 _dnd_status(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1285 struct _Ecore_X_Event_Xdnd_Status *status = ev;
1287 if (!status) return EINA_TRUE;
1289 /* Only thing we care about: will accept */
1290 if (status->will_accept)
1292 cnp_debug("Will accept\n");
1295 { /* Won't accept */
1296 cnp_debug("Won't accept accept\n");
1302 * Add a widget as drop target.
1305 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
1308 Ecore_X_Window xwin;
1313 if (!obj) return EINA_FALSE;
1314 top = elm_widget_top_get(obj);
1315 if (!top || !elm_win_xwindow_get(top)) return EINA_FALSE;
1316 if (!_elm_cnp_init_count) _elm_cnp_init();
1318 /* Is this the first? */
1319 first = (!drops) ? 1 : 0;
1321 EINA_LIST_FOREACH(drops, item, drop)
1323 if (drop->obj == obj)
1325 /* Update: Not a new one */
1326 drop->dropcb = dropcb;
1327 drop->cbdata = cbdata;
1328 drop->types = format;
1333 /* Create new drop */
1334 drop = calloc(1, sizeof(Dropable));
1335 if (!drop) return EINA_FALSE;
1336 /* FIXME: Check for eina's deranged error method */
1337 drops = eina_list_append(drops, drop);
1339 if (!drops/* || or other error */)
1344 drop->dropcb = dropcb;
1345 drop->cbdata = cbdata;
1346 drop->types = format;
1349 evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL,
1350 /* I love C and varargs */
1351 (Evas_Object_Event_Cb)elm_drop_target_del,
1353 /* FIXME: Handle resizes */
1355 /* If not the first: We're done */
1356 if (!first) return EINA_TRUE;
1358 xwin = (Ecore_X_Window)ecore_evas_window_get
1359 (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
1361 ecore_x_dnd_aware_set(xwin, EINA_TRUE);
1363 cnp_debug("Adding drop target calls\n");
1364 handler_enter = ecore_event_handler_add(ECORE_X_EVENT_XDND_ENTER,
1366 handler_pos = ecore_event_handler_add(ECORE_X_EVENT_XDND_POSITION,
1367 _dnd_position, NULL);
1368 handler_drop = ecore_event_handler_add(ECORE_X_EVENT_XDND_DROP,
1375 elm_drop_target_del(Evas_Object *obj)
1377 Dropable *drop,*del;
1379 Ecore_X_Window xwin;
1382 EINA_LIST_FOREACH(drops, item, drop)
1384 if (drop->obj == obj)
1386 drops = eina_list_remove_list(drops, item);
1391 if (!del) return EINA_FALSE;
1393 evas_object_event_callback_del(obj, EVAS_CALLBACK_FREE,
1394 (Evas_Object_Event_Cb)elm_drop_target_del);
1396 /* If still drops there: All fine.. continue */
1397 if (drops) return EINA_TRUE;
1399 cnp_debug("Disabling DND\n");
1400 xwin = (Ecore_X_Window)ecore_evas_window_get
1401 (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
1402 ecore_x_dnd_aware_set(xwin, EINA_FALSE);
1404 ecore_event_handler_del(handler_pos);
1405 ecore_event_handler_del(handler_drop);
1406 ecore_event_handler_del(handler_enter);
1408 if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1409 savedtypes.imgfile = NULL;
1416 _drag_mouse_up(void *un __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *data)
1418 Ecore_X_Window xwin = *((Ecore_X_Window *)data);
1419 evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_UP, _drag_mouse_up);
1421 ecore_x_dnd_aware_set(xwin, EINA_FALSE);
1424 dragdonecb(dragdonedata, selections[ELM_SEL_TYPE_XDND].widget);
1429 evas_object_del(dragwin);
1435 _drag_move(void *data __UNUSED__, Ecore_X_Xdnd_Position *pos)
1437 evas_object_move(dragwin,
1438 pos->position.x - _dragx,
1439 pos->position.y - _dragy);
1444 elm_drag_start(Evas_Object *obj, Elm_Sel_Format format, const char *data, void (*dragdone) (void *data, Evas_Object *), void *donecbdata)
1446 Ecore_X_Window xwin;
1448 Elm_Sel_Type xdnd = ELM_SEL_TYPE_XDND;
1450 int x, y, x2, y2, x3, y3;
1454 if (!_elm_cnp_init_count) _elm_cnp_init();
1456 xwin = elm_win_xwindow_get(obj);
1458 cnp_debug("starting drag...\n");
1462 cnp_debug("another obj is dragging...\n");
1466 ecore_x_dnd_type_set(xwin, "text/uri-list", EINA_TRUE);
1467 sel = selections + ELM_SEL_TYPE_XDND;
1468 sel->active = EINA_TRUE;
1470 sel->format = format;
1471 sel->selbuf = data ? strdup(data) : NULL;
1472 dragdonecb = dragdone;
1473 dragdonedata = donecbdata;
1475 ecore_x_dnd_aware_set(xwin, EINA_TRUE);
1476 ecore_x_dnd_callback_pos_update_set(_drag_move, NULL);
1477 ecore_x_dnd_begin(xwin, (unsigned char *)&xdnd, sizeof(Elm_Sel_Type));
1478 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
1479 _drag_mouse_up, (void *)(long)xwin);
1481 handler_status = ecore_event_handler_add(ECORE_X_EVENT_XDND_STATUS,
1484 dragwin = elm_win_add(NULL, "Elm Drag Object", ELM_WIN_UTILITY);
1485 elm_win_override_set(dragwin, EINA_TRUE);
1487 /* FIXME: Images only */
1488 icon = elm_icon_add(dragwin);
1489 elm_icon_file_set(icon, data + 7, NULL); /* 7!? "file://" */
1490 elm_win_resize_object_add(dragwin, icon);
1491 evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1492 evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, EVAS_HINT_FILL);
1494 /* Position subwindow appropriately */
1495 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(obj));
1496 ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
1497 evas_object_geometry_get(obj, &x2, &y2, &w, &h);
1500 evas_object_move(dragwin, x, y);
1501 evas_object_resize(icon, w, h);
1502 evas_object_resize(dragwin, w, h);
1504 evas_object_show(icon);
1505 evas_object_show(dragwin);
1507 evas_pointer_canvas_xy_get(evas_object_evas_get(obj), &x3, &y3);
1515 elm_cnp_tempfile_create(int size)
1519 const char *tmppath;
1522 info = malloc(sizeof(Tmp_Info));
1523 if (!info) return NULL;
1525 tmppath = getenv("TMP");
1526 if (!tmppath) tmppath = P_tmpdir;
1527 len = snprintf(NULL, 0, "%s/%sXXXXXX", tmppath, "elmcnpitem-");
1534 info->filename = malloc(len);
1535 if (!info->filename)
1540 snprintf(info->filename,len,"%s/%sXXXXXX", tmppath, "elmcnpitem-");
1542 info->fd = mkstemp(info->filename);
1547 /* And before someone says anything see POSIX 1003.1-2008 page 400 */
1550 pid = (long)getpid();
1551 /* Use pid instead of /proc/self: That way if can be passed around */
1552 len = snprintf(NULL,0,"/proc/%li/fd/%i", pid, info->fd);
1557 snprintf(tmp,len, "/proc/%li/fd/%i", pid, info->fd);
1558 unlink(info->filename);
1559 free(info->filename);
1560 info->filename = tmp;
1565 cnp_debug("filename is %s\n", info->filename);
1568 /* Set map to NULL and return */
1575 if (ftruncate(info->fd, size))
1577 perror("ftruncate");
1583 eina_mmap_safety_enabled_set(EINA_TRUE);
1585 info->map = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, info->fd, 0);
1586 if (info->map == MAP_FAILED)
1602 tmpinfo_free(Tmp_Info *info)
1604 if (!info) return 0;
1605 free(info->filename);
1611 /* Stubs for windows */
1613 elm_drag_start(Evas_Object *o, Elm_Sel_Format f, const char *d, void (*donecb)(void *, Evas_Object *),void *cbdata)
1619 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
1625 elm_drop_target_del(Evas_Object *o)
1631 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-3f0^-2{2(0W1st0 :*/