3 * - move to stringshare for stuff when I can
9 #include <stdio.h> // debug
11 #include <Elementary.h>
15 #ifdef HAVE_ELEMENTARY_X
18 # include <X11/Xatom.h>
21 # define ARRAYINIT(foo) [foo]=
26 #define cnp_debug(x...) printf(__FILE__": " x)
28 #define cnp_debug(x...)
32 #define PROVIDER_SET "__elm_cnp_provider_set"
37 CNP_ATOM_text_urilist,
38 CNP_ATOM_text_x_vcard,
42 CNP_ATOM_text_html_utf8,
47 CNP_ATOM_text_plain_utf8,
60 struct _elm_cnp_selection {
64 Elm_Sel_Format format;
67 unsigned int active : 1;
69 Ecore_X_Selection ecore_sel;
71 Evas_Object *requestwidget;
72 Elm_Sel_Format requestformat;
77 Eina_Bool (*set)(Ecore_X_Window, const void *data, int size);
78 Eina_Bool (*clear)(void);
79 void (*request)(Ecore_X_Window, const char *target);
82 /* Optimisation: Turn this into a 256 byte table:
83 * then can lookup in one index, not N checks */
84 static const struct escapes {
97 #define N_ESCAPES ((int)(sizeof(escapes)/sizeof(escapes[0])))
107 static struct tmpinfo *elm_cnp_tempfile_create(int size);
108 static int tmpinfo_free(struct tmpinfo *tmp);
110 static Eina_Bool _elm_cnp_init(void);
111 static Eina_Bool selection_clear(void *udata __UNUSED__, int type, void *event);
112 //static Eina_Bool selection_request(void *udata __UNUSED__, int type, void *event);
113 static Eina_Bool selection_notify(void *udata __UNUSED__, int type, void *event);
114 static char *remove_tags(const char *p, int *len);
115 static char *mark_up(const char *start, int inlen, int *lenp);
117 static Evas_Object *image_provider(void *images, Evas_Object *entry, const char *item);
119 entry_deleted(void *images, Evas *e, Evas_Object *entry, void *unused);
122 typedef Eina_Bool (*converter_fn)(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
124 static Eina_Bool targets_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
125 static Eina_Bool text_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
126 static Eina_Bool html_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
127 static Eina_Bool edje_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
128 static Eina_Bool uri_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
129 static Eina_Bool png_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
130 static Eina_Bool vcard_send(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
132 /* FIXME: Which way should this be: Notify or response */
133 typedef int (*response_handler)(struct _elm_cnp_selection *sel,
134 Ecore_X_Event_Selection_Notify *);
135 static int response_handler_targets(struct _elm_cnp_selection *sel,
136 Ecore_X_Event_Selection_Notify *);
138 typedef int (*notify_handler)(struct _elm_cnp_selection *sel,
139 Ecore_X_Event_Selection_Notify *);
140 static int notify_handler_targets(struct _elm_cnp_selection *sel,
141 Ecore_X_Event_Selection_Notify *notify);
142 static int notify_handler_text(struct _elm_cnp_selection *sel,
143 Ecore_X_Event_Selection_Notify *notify);
144 static int notify_handler_png(struct _elm_cnp_selection *sel,
145 Ecore_X_Event_Selection_Notify *notify);
146 static int notify_handler_uri(struct _elm_cnp_selection *sel,
147 Ecore_X_Event_Selection_Notify *notify);
148 static int notify_handler_html(struct _elm_cnp_selection *sel,
149 Ecore_X_Event_Selection_Notify *notify);
150 static int vcard_receive(struct _elm_cnp_selection *sed,
151 Ecore_X_Event_Selection_Notify *notify);
153 static struct pasteimage *pasteimage_alloc(const char *file, int pathlen);
154 static bool pasteimage_append(struct pasteimage *pi, Evas_Object *entry);
155 static void pasteimage_free(struct pasteimage *pi);
159 Elm_Sel_Format formats;
160 /* Called by ecore to do conversion */
161 converter_fn converter;
162 response_handler response;
163 notify_handler notify;
166 } atoms[CNP_N_ATOMS] = {
167 [CNP_ATOM_TARGETS] = {
169 (Elm_Sel_Format)-1, // everything
171 response_handler_targets,
172 notify_handler_targets,
176 "application/x-elementary-markup",
177 ELM_SEL_FORMAT_MARKUP,
183 [CNP_ATOM_text_uri] = {
185 ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_IMAGE, /* Either images or entries */
191 [CNP_ATOM_text_urilist] = {
193 ELM_SEL_FORMAT_IMAGE | ELM_SEL_FORMAT_MARKUP,
199 [CNP_ATOM_text_x_vcard] = {
201 ELM_SEL_FORMAT_VCARD,
205 [CNP_ATOM_image_png] = {
207 ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_IMAGE,
213 [CNP_ATOM_image_jpeg] = {
215 ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_IMAGE,
218 notify_handler_png,/* Raw image data is the same */
221 [CNP_ATOM_text_html_utf8] = {
222 "text/html;charset=utf-8",
223 ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
229 [CNP_ATOM_text_html] = {
234 notify_handler_html, /* No encoding: Webkit only */
237 [CNP_ATOM_UTF8STRING] = {
239 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
245 [CNP_ATOM_STRING] = {
247 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
255 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
261 [CNP_ATOM_text_plain_utf8] = {
262 "text/plain;charset=ut-8",
263 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
269 [CNP_ATOM_text_plain] = {
271 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
279 static struct _elm_cnp_selection selections[ELM_SEL_MAX] = {
280 ARRAYINIT(ELM_SEL_PRIMARY) {
282 .ecore_sel = ECORE_X_SELECTION_PRIMARY,
283 .set = ecore_x_selection_primary_set,
284 .clear = ecore_x_selection_primary_clear,
285 .request = ecore_x_selection_primary_request,
287 ARRAYINIT(ELM_SEL_SECONDARY) {
288 .debug = "Secondary",
289 .ecore_sel = ECORE_X_SELECTION_SECONDARY,
290 .set = ecore_x_selection_secondary_set,
291 .clear = ecore_x_selection_secondary_clear,
292 .request = ecore_x_selection_secondary_request,
294 ARRAYINIT(ELM_SEL_CLIPBOARD) {
295 .debug = "Clipboard",
296 .ecore_sel = ECORE_X_SELECTION_CLIPBOARD,
297 .set = ecore_x_selection_clipboard_set,
298 .clear = ecore_x_selection_clipboard_clear,
299 .request = ecore_x_selection_clipboard_request,
301 ARRAYINIT(ELM_SEL_XDND) {
303 .ecore_sel = ECORE_X_SELECTION_XDND,
304 .request = ecore_x_selection_xdnd_request,
308 /* Data for DND in progress */
309 /* FIXME: BEtter name */
313 unsigned int textreq: 1;
314 struct pasteimage *pi;
316 } savedtypes = { 0, NULL, 0, NULL, 0, 0 };
318 static void (*dragdonecb)(void *,Evas_Object*);
319 static void *dragdonedata;
321 static int _elm_cnp_init_count = 0;
322 /* Gah... who left this out of XAtoms.h */
323 static Ecore_X_Atom clipboard_atom;
325 Eina_List *pastedimages;
326 Eina_List *providedobjs;
329 * Drag & Drop functions
333 /* FIXME: Cache window */
334 Elm_Sel_Format types;
338 /* FIXME: Way too many globals */
339 Eina_List *drops = NULL;
340 Evas_Object *dragwin;
342 Ecore_Event_Handler *handler_pos, *handler_drop, *handler_enter,
345 struct dropable *cur;
350 /* Stringshared, so I can just compare pointers later */
351 static const char *text_uri;
354 elm_selection_set(Elm_Sel_Type selection, Evas_Object *widget,
355 Elm_Sel_Format format, const char *selbuf)
357 #ifdef HAVE_ELEMENTARY_X
358 struct _elm_cnp_selection *sel;
360 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
361 if (!_elm_cnp_init_count) _elm_cnp_init();
362 if ((!selbuf) && (format != ELM_SEL_FORMAT_IMAGE))
363 return elm_selection_clear(selection, widget);
365 sel = selections + selection;
368 sel->widget = widget;
370 sel->set(elm_win_xwindow_get(widget),&selection,sizeof(Elm_Sel_Type));
371 sel->format = format;
372 sel->selbuf = selbuf ? strdup(selbuf) : NULL;
381 elm_selection_clear(Elm_Sel_Type selection, Evas_Object *widget)
383 #ifdef HAVE_ELEMENTARY_X
384 struct _elm_cnp_selection *sel;
386 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
387 if (!_elm_cnp_init_count) _elm_cnp_init();
389 sel = selections + selection;
391 /* No longer this selection: Consider it gone! */
392 if ((!sel->active) || (sel->widget != widget)) return EINA_TRUE;
405 elm_selection_get(Elm_Sel_Type selection, Elm_Sel_Format format,
406 Evas_Object *widget, Elm_Drop_Cb datacb, void *udata)
408 #ifdef HAVE_ELEMENTARY_X
410 struct _elm_cnp_selection *sel;
412 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
413 if (!_elm_cnp_init_count) _elm_cnp_init();
415 sel = selections + selection;
416 top = elm_widget_top_get(widget);
417 if (!top) return EINA_FALSE;
419 sel->requestformat = format;
420 sel->requestwidget = widget;
421 sel->request(elm_win_xwindow_get(top), ECORE_X_SELECTION_TARGET_TARGETS);
422 sel->datacb = datacb;
431 #ifdef HAVE_ELEMENTARY_X
436 if (_elm_cnp_init_count ++) return EINA_TRUE;
438 for (i = 0 ; i < CNP_N_ATOMS ; i ++)
440 atoms[i].atom = ecore_x_atom_get(atoms[i].name);
441 ecore_x_selection_converter_atom_add(atoms[i].atom,
444 clipboard_atom = ecore_x_atom_get("CLIPBOARD");
446 ecore_event_handler_add(ECORE_X_EVENT_SELECTION_CLEAR, selection_clear,NULL);
447 ecore_event_handler_add(ECORE_X_EVENT_SELECTION_NOTIFY,selection_notify,NULL);
449 text_uri = eina_stringshare_add("text/uri-list");
454 selection_clear(void *udata __UNUSED__, int type __UNUSED__, void *event){
455 Ecore_X_Event_Selection_Clear *ev = event;
456 struct _elm_cnp_selection *sel;
459 for (i = 0 ; i < ELM_SEL_MAX ; i ++)
461 if (selections[i].ecore_sel == ev->selection) break;
463 cnp_debug("selection %d clear\n",i);
464 /* Not me... Don't care */
465 if (i == ELM_SEL_MAX) return ECORE_CALLBACK_PASS_ON;
467 sel = selections + i;
472 return ECORE_CALLBACK_PASS_ON;
477 * Response to a selection notify:
478 * - So we have asked for the selection list.
479 * - If it's the targets list, parse it, and fire of what we want,
480 * else it's the data we want.
483 selection_notify(void *udata __UNUSED__, int type __UNUSED__, void *event){
484 Ecore_X_Event_Selection_Notify *ev = event;
485 struct _elm_cnp_selection *sel;
488 cnp_debug("selection notify callback: %d\n",ev->selection);
489 switch (ev->selection){
490 case ECORE_X_SELECTION_CLIPBOARD:
491 sel = selections + ELM_SEL_CLIPBOARD;
493 case ECORE_X_SELECTION_PRIMARY:
494 sel = selections + ELM_SEL_PRIMARY;
496 case ECORE_X_SELECTION_SECONDARY:
497 sel = selections + ELM_SEL_SECONDARY;
499 case ECORE_X_SELECTION_XDND:
500 sel = selections + ELM_SEL_XDND;
503 return ECORE_CALLBACK_PASS_ON;
505 cnp_debug("Target is %s\n",ev->target);
507 for (i = 0 ; i < CNP_N_ATOMS ; i ++)
509 if (!strcmp(ev->target, atoms[i].name))
511 if (atoms[i].notify){
512 cnp_debug("Found something: %s\n", atoms[i].name);
513 atoms[i].notify(sel, ev);
515 printf("Ignored: No handler!\n");
520 return ECORE_CALLBACK_PASS_ON;
526 targets_converter(char *target __UNUSED__, void *data, int size __UNUSED__,
527 void **data_ret, int *size_ret,
528 Ecore_X_Atom *ttype, int *typesize){
531 struct _elm_cnp_selection *sel;
533 if (!data_ret) return EINA_FALSE;
535 sel = selections + *(int*)data;
537 for (i = 0, count = 0 ; i < CNP_N_ATOMS ; i ++)
538 if (sel->format & atoms[i].formats)
541 aret = malloc(sizeof(Ecore_X_Atom) * count);
542 for (i = 0, count = 0 ; i < CNP_N_ATOMS ; i ++)
543 if (sel->format & atoms[i].formats)
544 aret[count ++] = atoms[i].atom;
547 if (typesize) *typesize = 32 /* urk */;
548 if (ttype) *ttype = XA_ATOM;
549 if (size_ret) *size_ret = count;
555 png_converter(char *target __UNUSED__, void *data __UNUSED__,
556 int size __UNUSED__, void **data_ret __UNUSED__,
557 int *size_ret __UNUSED__, Ecore_X_Atom *ttype __UNUSED__,
558 int *typesize __UNUSED__)
560 cnp_debug("Png converter called\n");
565 vcard_send(char *target __UNUSED__, void *data __UNUSED__, int size __UNUSED__,
566 void **data_ret, int *size_ret,
567 Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
569 struct _elm_cnp_selection *sel;
571 cnp_debug("vcard send called\n");
573 sel = selections + *(int*)data;
575 if (data_ret) *data_ret = strdup(sel->selbuf);
576 if (size_ret) *size_ret = strlen(sel->selbuf);
581 * Callback to handle a targets response on a selection request:
582 * So pick the format we'd like; and then request it.
585 notify_handler_targets(struct _elm_cnp_selection *sel,
586 Ecore_X_Event_Selection_Notify *notify)
588 Ecore_X_Selection_Data_Targets *targets;
589 Ecore_X_Atom *atomlist;
592 targets = notify->data;
593 atomlist = (Ecore_X_Atom *)(targets->data.data);
595 for (j = 1; j < CNP_N_ATOMS ; j ++)
597 cnp_debug("\t%s %d\n",atoms[j].name, atoms[j].atom);
598 if (!(atoms[j].formats & sel->requestformat)) continue;
599 for (i = 0 ; i < targets->data.length ; i ++)
601 if ((atoms[j].atom == atomlist[i]) && (atoms[j].notify))
603 cnp_debug("Atom %s matches\n",atoms[j].name);
609 cnp_debug("Couldn't find anything that matches\n");
610 return ECORE_CALLBACK_PASS_ON;
613 cnp_debug("Sending request for %s\n",atoms[j].name);
614 sel->request(elm_win_xwindow_get(sel->requestwidget), atoms[j].name);
616 return ECORE_CALLBACK_PASS_ON;
620 response_handler_targets(struct _elm_cnp_selection *sel,
621 Ecore_X_Event_Selection_Notify *notify)
623 Ecore_X_Selection_Data_Targets *targets;
624 Ecore_X_Atom *atomlist;
628 targets = notify->data;
629 atomlist = (Ecore_X_Atom *)(targets->data.data);
631 /* Start from 1: Skip targets */
632 for (j = 1 ; j < CNP_N_ATOMS ; j ++)
634 if (!(atoms[j].formats & sel->requestformat)) continue;
635 for (i = 0 ; i < targets->data.length ; i ++)
637 if ((atoms[j].atom == atomlist[i]) && (atoms[j].response)){
638 /* Found a match: Use it */
644 if (j == CNP_N_ATOMS)
646 cnp_debug("No matching type found\n");
650 top = elm_widget_top_get(sel->requestwidget);
653 sel->request(elm_win_xwindow_get(top), atoms[j].name);
659 notify_handler_text(struct _elm_cnp_selection *sel,
660 Ecore_X_Event_Selection_Notify *notify)
662 Ecore_X_Selection_Data *data;
666 cnp_debug("Notify handler text %d %d %p\n",data->format,data->length,data->data);
667 str = mark_up((char*)data->data, data->length, NULL);
668 cnp_debug("String is %s (from %s)\n",str,data->data);
669 elm_entry_entry_insert(sel->requestwidget, str);
677 * So someone is pasting an image into my entry or widget...
680 notify_handler_uri(struct _elm_cnp_selection *sel,
681 Ecore_X_Event_Selection_Notify *notify)
683 Ecore_X_Selection_Data *data;
684 Ecore_X_Selection_Data_Files *files;
685 struct pasteimage *pi;
689 printf("data->format is %d %p %p\n",data->format,notify,data);
690 if (data->content == ECORE_X_SELECTION_CONTENT_FILES)
692 cnp_debug("got a files list\n");
693 files = notify->data;
694 if (files->num_files > 1)
696 /* Don't handle many items */
697 cnp_debug("more then one file: Bailing\n");
703 p = (char *)data->data;
706 cnp_debug("Couldn't find a file\n");
709 cnp_debug("Got %s\n",p);
710 if (strncmp(p, "file://", 7))
712 /* Try and continue if it looks sane */
713 if (*p != '/') return 0;
716 p += strlen("file://");
718 if ((!strcasestr(p,".png")) && (!strcasestr(p,".jpg")) && (!strcasestr(p,".bmp")))
720 cnp_debug("No image(png, jpg and bmp), ignoring\n");
721 if (savedtypes.textreq) savedtypes.textreq = 0;
725 if (savedtypes.pi) pasteimage_free(savedtypes.pi);
727 pi = pasteimage_alloc(p, data->length);
729 if (savedtypes.textreq)
731 savedtypes.textreq = 0;
736 pasteimage_append(pi, sel->requestwidget);
737 savedtypes.pi = NULL;
744 * Just receieved an vcard, either through cut and paste, or dnd.
747 vcard_receive(struct _elm_cnp_selection *sel,
748 Ecore_X_Event_Selection_Notify *notify)
750 struct dropable *dropable;
752 Ecore_X_Selection_Data *data;
755 cnp_debug("vcard receive\n");
757 if (sel == selections + ELM_SEL_XDND)
759 Elm_Selection_Data ddata;
760 cnp_debug("drag & drop\n");
761 /* FIXME: this needs to be generic: Used for all receives */
762 EINA_LIST_FOREACH(drops, l, dropable)
763 if (dropable->obj == sel->requestwidget)
767 cnp_debug("Unable to find drop object");
768 ecore_x_dnd_send_finished();
771 dropable = eina_list_data_get(l);
772 ddata.x = savedtypes.x;
773 ddata.y = savedtypes.y;
774 ddata.format = ELM_SEL_FORMAT_VCARD;
775 ddata.data = data->data;
776 ddata.len = data->length;
777 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
778 ecore_x_dnd_send_finished();
780 else if (sel->datacb)
782 Elm_Selection_Data ddata;
783 ddata.x = ddata.y = 0;
784 ddata.format = ELM_SEL_FORMAT_IMAGE;
785 ddata.data = data->data;
786 ddata.len = data->length;
787 sel->datacb(sel->udata, sel->widget, &ddata);
791 cnp_debug("Paste request\n");
800 notify_handler_png(struct _elm_cnp_selection *sel,
801 Ecore_X_Event_Selection_Notify *notify)
803 Ecore_X_Selection_Data *data;
805 struct pasteimage *pi;
807 cnp_debug("got a png (or a jpeg)!\n");
810 cnp_debug("Size if %d\n",data->length);
814 Elm_Selection_Data ddata;
815 ddata.x = ddata.y = 0;
816 ddata.format = ELM_SEL_FORMAT_IMAGE;
817 ddata.data = data->data;
818 ddata.len = data->length;
819 sel->datacb(sel->udata, sel->widget, &ddata);
824 /* generate tmp name */
825 tmp = elm_cnp_tempfile_create(data->length);
827 memcpy(tmp->map, data->data, data->length);
829 munmap(tmp->map,data->length);
831 /* FIXME: Add to paste image data to clean up */
833 pi = pasteimage_alloc(tmp->filename, data->length);
834 pasteimage_append(pi, sel->requestwidget);
843 * Warning: Generic text/html can';t handle it sanely.
844 * Firefox sends ucs2 (i think).
845 * chrome sends utf8... blerg
848 notify_handler_html(struct _elm_cnp_selection *sel,
849 Ecore_X_Event_Selection_Notify *notify){
850 Ecore_X_Selection_Data *data;
852 cnp_debug("Got some HTML: Checking encoding is useful\n");
857 Elm_Selection_Data ddata;
858 ddata.x = ddata.y = 0;
859 ddata.format = ELM_SEL_FORMAT_HTML;
860 ddata.data = data->data;
861 ddata.len = data->length;
862 sel->datacb(sel->udata, sel->widget, &ddata);
866 char *stripstr = NULL;
867 stripstr = malloc(sizeof(char)*(data->length+1));
868 strncpy(stripstr, data->data, data->length);
869 stripstr[data->length] = '\0';
870 cnp_debug("String is %s (%d bytes)\n",stripstr,data->length);
871 elm_entry_entry_insert(sel->requestwidget, stripstr);
879 text_converter(char *target __UNUSED__, void *data, int size __UNUSED__,
880 void **data_ret, int *size_ret,
881 Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
883 struct _elm_cnp_selection *sel;
885 cnp_debug("text converter\n");
886 sel = selections + *(int *)data;
887 if (!sel->active) return EINA_TRUE;
889 if (sel->format == ELM_SEL_FORMAT_MARKUP ||
890 sel->format == ELM_SEL_FORMAT_HTML)
892 *data_ret = remove_tags(sel->selbuf, size_ret);
894 else if (sel->format == ELM_SEL_FORMAT_TEXT)
896 *data_ret = strdup(sel->selbuf);
897 *size_ret = strlen(sel->selbuf);
899 else if (sel->format == ELM_SEL_FORMAT_IMAGE)
901 cnp_debug("Image %s\n",evas_object_type_get(sel->widget));
902 cnp_debug("Elm type: %s\n",elm_object_widget_type_get(sel->widget));
903 evas_object_image_file_get(elm_photocam_internal_image_get(sel->widget), (const char **)data_ret, NULL);
904 if (!*data_ret) *data_ret = strdup("No file");
905 else *data_ret = strdup(*data_ret);
906 *size_ret = strlen(*data_ret);
912 edje_converter(char *target __UNUSED__, void *data, int size __UNUSED__,
913 void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__,
914 int *typesize __UNUSED__)
916 struct _elm_cnp_selection *sel;
918 sel = selections + *(int *)data;
919 if (data_ret) *data_ret = strdup(sel->selbuf);
920 if (size_ret) *size_ret = strlen(sel->selbuf);
927 html_converter(char *target __UNUSED__, void *data, int size __UNUSED__,
928 void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__,
929 int *typesize __UNUSED__)
931 struct _elm_cnp_selection *sel;
933 sel = selections + *(int *)data;
934 if (data_ret) *data_ret = strdup(sel->selbuf);
935 if (size_ret) *size_ret = strlen(sel->selbuf);
941 uri_converter(char *target __UNUSED__, void *data, int size __UNUSED__,
942 void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__,
943 int *typesize __UNUSED__)
945 struct _elm_cnp_selection *sel;
946 sel = selections + *(int *)data;
947 cnp_debug("Uri converter\n");
948 if (data_ret) *data_ret = strdup(sel->selbuf);
949 if (size_ret) *size_ret = strlen(sel->selbuf);
955 * Image paste provide
958 /* FIXME: Should add provider for each pasted item: Use data to store it
961 image_provider(void *images __UNUSED__, Evas_Object *entry, const char *item)
963 struct pasteimage *pi;
966 cnp_debug("image provider for %s called\n",item);
967 EINA_LIST_FOREACH(pastedimages, l, pi)
969 cnp_debug("is it %s?\n",pi->tag);
970 if (!strcmp(pi->tag, item)){
973 o = evas_object_image_filled_add(evas_object_evas_get(entry));
974 /* FIXME: Handle eets */
975 cnp_debug("file is %s (object is %p)\n",pi->file,o);
976 evas_object_image_file_set(o, pi->file, NULL);
985 static struct pasteimage *
986 pasteimage_alloc(const char *file, int pathlen)
988 struct pasteimage *pi;
991 int prefixlen = strlen("file://");
993 pi = calloc(1,sizeof(struct pasteimage));
994 if (!pi) return NULL;
996 len = snprintf(NULL, 0, "pasteimage-%p",pi);
1004 snprintf(buf, len, "pasteimage-%p",pi);
1009 if (strstr(file,"file://")) file += prefixlen;
1010 filebuf = malloc(sizeof(char)*(pathlen-prefixlen+1));
1011 strncpy(filebuf, file, pathlen-prefixlen);
1012 filebuf[pathlen-prefixlen] = '\0';
1013 pi->file = strdup(filebuf);
1021 pasteimage_free(struct pasteimage *pi)
1024 if (pi->file) free((void*)pi->file);
1025 if (pi->tag) free((void*)pi->tag);
1030 pasteimage_provider_set(Evas_Object *entry)
1034 if (!entry) return false;
1035 type = elm_widget_type_get(entry);
1036 printf("type is %s\n",type);
1037 if ((!type) || (strcmp(type, "entry"))) return false;
1039 v = evas_object_data_get(entry, PROVIDER_SET);
1042 evas_object_data_set(entry, PROVIDER_SET, pasteimage_provider_set);
1043 elm_entry_item_provider_append(entry, image_provider, NULL);
1044 evas_object_event_callback_add(entry, EVAS_CALLBACK_FREE,
1045 entry_deleted, NULL);
1052 pasteimage_append(struct pasteimage *pi, Evas_Object *entry)
1056 if (!pi) return false;
1057 if (!entry) return false;
1059 pasteimage_provider_set(entry);
1061 pastedimages = eina_list_append(pastedimages, pi);
1062 snprintf(entrytag, sizeof(entrytag),"<item absize=240x180 href=%s>",pi->tag);
1063 elm_entry_entry_insert(entry, entrytag);
1069 entry_deleted(void *images __UNUSED__, Evas *e __UNUSED__, Evas_Object *entry, void *unused __UNUSED__)
1071 struct pasteimage *pi;
1074 EINA_LIST_FOREACH_SAFE(pastedimages, l, next, pi)
1076 if (pi->entry == entry){
1077 pastedimages = eina_list_remove_list(pastedimages, l);
1084 remove_tags(const char *p, int *len){
1087 if (!p) return NULL;
1089 q = malloc(strlen(p) + 1);
1090 if (!q) return NULL;
1095 if ((*p != '<') && (*p != '&')){
1097 } else if (*p == '<') {
1098 if ((p[1] == 'b') && (p[2] == 'r') &&
1099 ((p[3] == ' ') || (p[3] == '/') || (p[3] == '>')))
1101 while ((*p) && (*p != '>')) p ++;
1103 } else if (*p == '&') {
1105 for (i = 0 ; i < N_ESCAPES ; i ++){
1106 if (!strncmp(p,escapes[i].escape, strlen(escapes[i].escape))){
1107 p += strlen(escapes[i].escape);
1108 *q = escapes[i].value;
1118 if (len) *len = q - ret;
1124 mark_up(const char *start, int inlen, int *lenp){
1128 const char *endp = NULL;
1130 if (!start) return NULL;
1132 endp = start + inlen;
1133 /* First pass: Count characters */
1134 for (l = 0, p = start ; ((!endp) || (p < endp)) && (*p) ; p ++)
1136 for (i = 0 ; i < N_ESCAPES ; i ++)
1138 if (*p == escapes[i].value)
1140 l += strlen(escapes[i].escape);
1148 q = ret = malloc(l + 1);
1150 /* Second pass: Change characters */
1151 for (p = start ; *p ; )
1153 for (i = 0 ; i < N_ESCAPES ; i ++)
1155 if (*p == escapes[i].value)
1157 strcpy(q, escapes[i].escape);
1158 q += strlen(escapes[i].escape);
1168 if (lenp) *lenp = l;
1174 _dnd_enter(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1176 Ecore_X_Event_Xdnd_Enter *enter = ev;
1180 if ((!enter) || (!enter->num_types) || (!enter->types)) return EINA_TRUE;
1182 cnp_debug("Types\n");
1183 savedtypes.ntypes = enter->num_types;
1184 if (savedtypes.types) free(savedtypes.types);
1185 savedtypes.types = malloc(sizeof(char *) * enter->num_types);
1186 if (!savedtypes.types)
1189 for (i = 0 ; i < enter->num_types ; i ++)
1191 savedtypes.types[i] = eina_stringshare_add(enter->types[i]);
1192 cnp_debug("Type is %s %p %p\n",enter->types[i],
1193 savedtypes.types[i],text_uri);
1194 if (savedtypes.types[i] == text_uri)
1196 /* Request it, so we know what it is */
1197 cnp_debug("Sending uri request\n");
1198 savedtypes.textreq = 1;
1199 savedtypes.pi = NULL; /* FIXME: Free? */
1200 ecore_x_selection_xdnd_request(enter->win, text_uri);
1204 /* FIXME: Find an object and make it current */
1210 _dnd_drop(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1212 struct _Ecore_X_Event_Xdnd_Drop *drop;
1213 struct dropable *dropable;
1216 Ecore_X_Window xwin;
1217 Elm_Selection_Data ddata;
1224 // check we still have something to drop
1225 if (!drops) return true;
1227 /* Find any widget in our window; then work out geometry rel to our window */
1228 for (l = drops ; l ; l = l->next)
1231 xwin = (Ecore_X_Window)ecore_evas_window_get(ecore_evas_ecore_evas_get(
1232 evas_object_evas_get(
1234 if (xwin == drop->win)
1237 /* didn't find a window */
1242 /* Calculate real (widget relative) position */
1243 // - window position
1244 // - widget position
1245 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(dropable->obj));
1246 ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
1247 savedtypes.x = drop->position.x - x;
1248 savedtypes.y = drop->position.y - y;
1250 cnp_debug("Drop position is %d,%d\n",savedtypes.x,savedtypes.y);
1252 for ( ; l ; l = l->next)
1255 evas_object_geometry_get(dropable->obj, &x, &y, &w, &h);
1256 if ((savedtypes.x >= x) && (savedtypes.y >= y) &&
1257 (savedtypes.x < x + w) && (savedtypes.y < y + h))
1261 if (!l) /* didn't find one */
1264 evas_object_geometry_get(dropable->obj, &x, &y, NULL, NULL);
1268 /* Find our type from the previous list */
1269 for (i = 0 ; i < CNP_N_ATOMS ; i ++)
1271 for (j = 0 ; j < savedtypes.ntypes ; j ++)
1273 if (!strcmp(savedtypes.types[j], atoms[i].name))
1280 cnp_debug("Didn't find a target\n");
1284 printf("Found a target we'd like: %s\n",atoms[i].name);
1285 cnp_debug("0x%x\n",xwin);
1287 if (i == CNP_ATOM_text_urilist)
1289 cnp_debug("We found a URI... (%scached)\n",savedtypes.pi?"":"not ");
1294 ddata.x = savedtypes.x;
1295 ddata.y = savedtypes.y;
1297 if (dropable->types & ELM_SEL_FORMAT_IMAGE)
1299 cnp_debug("Doing image insert (%s)\n",savedtypes.pi->file);
1300 ddata.format = ELM_SEL_FORMAT_IMAGE;
1301 ddata.data = (char *)savedtypes.pi->file;
1302 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1303 ecore_x_dnd_send_finished();
1305 pasteimage_free(savedtypes.pi);
1306 savedtypes.pi = NULL;
1310 else if (dropable->types & ELM_SEL_FORMAT_MARKUP)
1312 ddata.format = ELM_SEL_FORMAT_MARKUP;
1313 pasteimage_provider_set(dropable->obj);
1315 pastedimages = eina_list_append(pastedimages, savedtypes.pi);
1316 snprintf(entrytag, sizeof(entrytag),
1317 "<item absize=240x180 href=%s>",savedtypes.pi->tag);
1318 ddata.data = entrytag;
1319 cnp_debug("Insert %s\n",(char *)ddata.data);
1320 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1321 ecore_x_dnd_send_finished();
1325 else if (savedtypes.textreq)
1327 /* Already asked: Pretend we asked now, and paste immediately when
1329 savedtypes.textreq = 0;
1330 ecore_x_dnd_send_finished();
1335 cnp_debug("doing a request then\n");
1336 selections[ELM_SEL_XDND].requestwidget = dropable->obj;
1337 selections[ELM_SEL_XDND].requestformat = ELM_SEL_FORMAT_MARKUP;
1338 selections[ELM_SEL_XDND].active = true;
1340 ecore_x_selection_xdnd_request(xwin, atoms[i].name);
1345 _dnd_position(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1347 struct _Ecore_X_Event_Xdnd_Position *pos;
1348 Ecore_X_Rectangle rect;
1352 /* Need to send a status back */
1353 /* FIXME: Should check I can drop here */
1354 /* FIXME: Should highlight widget */
1355 rect.x = pos->position.x - 5;
1356 rect.y = pos->position.y - 5;
1359 ecore_x_dnd_send_status(true, false, rect, pos->action);
1365 * When dragging this is callback response from the destination.
1366 * The important thing we care about: Can we drop; thus update cursor
1370 _dnd_status(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1372 struct _Ecore_X_Event_Xdnd_Status *status = ev;
1374 if (!status) return true;
1376 /* Only thing we care about: will accept */
1377 if (status->will_accept)
1379 printf("Will accept\n");
1382 { /* Won't accept */
1383 printf("Won't accept accept\n");
1389 * Add a widget as drop target.
1392 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format,
1393 Elm_Drop_Cb dropcb, void *cbdata)
1395 struct dropable *drop;
1396 Ecore_X_Window xwin;
1399 if (!obj) return false;
1400 if (!_elm_cnp_init_count) _elm_cnp_init();
1402 /* Is this the first? */
1403 first = (!drops) ? 1 : 0;
1405 drop = calloc(1,sizeof(struct dropable));
1406 if (!drop) return false;
1407 drop->dropcb = dropcb;
1408 drop->cbdata = cbdata;
1410 /* FIXME: Check it's not already there */
1412 /* FIXME: Check for eina's deranged error method */
1413 drops = eina_list_append(drops, drop);
1415 if (!drops/* || or other error */)
1422 /* Something for now */
1423 drop->types = format;
1425 evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL,
1426 /* I love C and varargs */
1427 (Evas_Object_Event_Cb)elm_drop_target_del,
1429 /* FIXME: Handle resizes */
1431 /* If not the first: We're done */
1432 if (!first) return true;
1434 xwin = (Ecore_X_Window)ecore_evas_window_get(ecore_evas_ecore_evas_get(
1435 evas_object_evas_get(obj)));
1436 ecore_x_dnd_aware_set(xwin, true);
1438 printf("Adding drop target calls\n");
1439 handler_enter = ecore_event_handler_add(ECORE_X_EVENT_XDND_ENTER,
1441 handler_pos = ecore_event_handler_add(ECORE_X_EVENT_XDND_POSITION,
1442 _dnd_position, NULL);
1443 handler_drop = ecore_event_handler_add(ECORE_X_EVENT_XDND_DROP,
1450 elm_drop_target_del(Evas_Object *obj)
1452 struct dropable *drop,*del;
1454 Ecore_X_Window xwin;
1457 EINA_LIST_FOREACH(drops, item, drop)
1459 if (drop->obj == obj)
1461 drops = eina_list_remove_list(drops, item);
1466 if (!del) return false;
1468 evas_object_event_callback_del(obj, EVAS_CALLBACK_FREE,
1469 (Evas_Object_Event_Cb)elm_drop_target_del);
1471 /* If still drops there: All fine.. continue */
1472 if (drops) return true;
1474 cnp_debug("Disabling DND\n");
1475 xwin = (Ecore_X_Window)ecore_evas_window_get(ecore_evas_ecore_evas_get(
1476 evas_object_evas_get(obj)));
1477 ecore_x_dnd_aware_set(xwin, false);
1479 ecore_event_handler_del(handler_pos);
1480 ecore_event_handler_del(handler_drop);
1481 ecore_event_handler_del(handler_enter);
1485 pasteimage_free(savedtypes.pi);
1486 savedtypes.pi = NULL;
1494 _drag_mouse_up(void *un __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *data __UNUSED__)
1496 evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_UP, _drag_mouse_up);
1500 dragdonecb(dragdonecb,selections[ELM_SEL_XDND].widget);
1505 evas_object_del(dragwin);
1512 _drag_move(void *data __UNUSED__, Ecore_X_Xdnd_Position *pos){
1513 evas_object_move(dragwin, pos->position.x - _dragx,
1514 pos->position.y - _dragy);
1519 elm_drag_start(Evas_Object *obj, Elm_Sel_Format format, const char *data,
1520 void (*dragdone)(void *data, Evas_Object *),void *donecbdata)
1522 Ecore_X_Window xwin;
1523 struct _elm_cnp_selection *sel;
1524 Elm_Sel_Type xdnd = ELM_SEL_XDND;
1526 int x,y,x2,y2,x3,y3;
1530 if (!_elm_cnp_init_count) _elm_cnp_init();
1532 xwin = elm_win_xwindow_get(obj);
1534 cnp_debug("starting drag...\n");
1536 ecore_x_dnd_type_set(xwin, "text/uri-list", 1);
1537 sel = selections + ELM_SEL_XDND;
1540 sel->format = format;
1541 sel->selbuf = data ? strdup(data) : NULL;
1542 dragdonecb = dragdone;
1543 dragdonedata = donecbdata;
1545 ecore_x_dnd_callback_pos_update_set(_drag_move, NULL);
1546 ecore_x_dnd_begin(xwin, (unsigned char *)&xdnd, sizeof(Elm_Sel_Type));
1547 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
1548 _drag_mouse_up, NULL);
1550 handler_status = ecore_event_handler_add(ECORE_X_EVENT_XDND_STATUS,
1553 dragwin = elm_win_add(NULL, "Elm Drag Object",ELM_WIN_UTILITY);
1554 elm_win_override_set(dragwin,1);
1556 /* FIXME: Images only */
1557 icon = elm_icon_add(dragwin);
1558 elm_icon_file_set(icon, data + 7, NULL); /* 7!? */
1559 elm_win_resize_object_add(dragwin,icon);
1560 evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND,EVAS_HINT_EXPAND);
1561 evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, EVAS_HINT_FILL);
1563 /* Position subwindow appropriately */
1564 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(obj));
1565 ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
1566 evas_object_geometry_get(obj, &x2, &y2, &w, &h);
1569 evas_object_move(dragwin, x, y);
1570 evas_object_resize(icon,w,h);
1571 evas_object_resize(dragwin,w,h);
1573 evas_object_show(icon);
1574 evas_object_show(dragwin);
1576 evas_pointer_canvas_xy_get(evas_object_evas_get(obj), &x3, &y3);
1583 static struct tmpinfo *
1584 elm_cnp_tempfile_create(int size){
1585 struct tmpinfo *info;
1586 const char *tmppath;
1589 info = malloc(sizeof(struct tmpinfo));
1590 if (!info) return NULL;
1592 tmppath = getenv("TMP");
1593 if (!tmppath) tmppath = P_tmpdir;
1594 if (!tmppath) tmppath = "/tmp";
1595 len = snprintf(NULL,0,"%s/%sXXXXXX",tmppath, "elmcnpitem-");
1602 info->filename = malloc(len);
1603 if (!info->filename)
1608 snprintf(info->filename,len,"%s/%sXXXXXX",tmppath, "elmcnpitem-");
1610 info->fd = mkstemp(info->filename);
1615 /* And before someone says anything see POSIX 1003.1-2008 page 400 */
1617 pid = (long)getpid();
1618 /* Use pid instead of /proc/self: That way if can be passed around */
1619 len = snprintf(NULL,0,"/proc/%ld/fd/%d",pid, info->fd);
1624 snprintf(tmp,len, "/proc/%ld/fd/%d",pid, info->fd);
1625 unlink(info->filename);
1626 free(info->filename);
1627 info->filename = tmp;
1632 printf("filename is %s\n",info->filename);
1635 /* Set map to NULL and return */
1642 if (ftruncate(info->fd, size))
1644 perror("ftruncate");
1650 info->map = mmap(NULL,size, PROT_READ|PROT_WRITE, MAP_SHARED, info->fd, 0);
1651 if (info->map == MAP_FAILED)
1663 tmpinfo_free(struct tmpinfo *info)
1665 if (!info) return 0;
1666 free(info->filename);
1672 /* Stubs for windows */
1674 elm_drag_start(Evas_Object *o, Elm_Sel_Format f, const char *d,
1675 void (*donecb)(void *, Evas_Object *),void *cbdata)
1680 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format,
1681 Elm_Drop_Cb dropcb, void *cbdata){
1686 elm_drop_target_del(Evas_Object *o)
1692 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-2f0^-2{2(0W1st0 :*/