2 # include "elementary_config.h"
4 #include <Elementary.h>
9 #ifdef HAVE_ELEMENTARY_X
11 #define ARRAYINIT(foo) [foo] =
17 # define cnp_debug(x...) fprintf(stderr, __FILE__": " x)
19 # define cnp_debug(x...)
22 #define PROVIDER_SET "__elm_cnp_provider_set"
24 typedef struct _Paste_Image Paste_Image;
25 typedef struct _Cnp_Selection Cnp_Selection;
26 typedef struct _Escape Escape;
27 typedef struct _Tmp_Info Tmp_Info;
28 typedef struct _Cnp_Atom Cnp_Atom;
29 typedef struct _Saved_Type Saved_Type;
30 typedef struct _Dropable Dropable;
32 typedef Eina_Bool (*Converter_Fn_Cb) (char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
33 typedef int (*Response_Handler_Cb) (Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *);
34 typedef int (*Notify_Handler_Cb) (Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *);
40 CNP_ATOM_text_urilist,
41 CNP_ATOM_text_x_vcard,
52 CNP_ATOM_text_html_utf8,
57 CNP_ATOM_text_plain_utf8,
76 Evas_Object *requestwidget;
78 Elm_Sel_Format requestformat;
80 Eina_Bool (*set) (Ecore_X_Window, const void *data, int size);
81 Eina_Bool (*clear) (void);
82 void (*request) (Ecore_X_Window, const char *target);
84 Elm_Sel_Format format;
85 Ecore_X_Selection ecore_sel;
107 Elm_Sel_Format formats;
108 /* Called by ecore to do conversion */
109 Converter_Fn_Cb converter;
110 Response_Handler_Cb response;
111 Notify_Handler_Cb notify;
122 Eina_Bool textreq: 1;
128 /* FIXME: Cache window */
129 Elm_Sel_Format types;
134 static Tmp_Info *elm_cnp_tempfile_create(int size);
135 static int tmpinfo_free(Tmp_Info *tmp);
137 static Eina_Bool _elm_cnp_init(void);
138 static Eina_Bool selection_clear(void *udata __UNUSED__, int type, void *event);
139 static Eina_Bool selection_notify(void *udata __UNUSED__, int type, void *event);
140 static char *remove_tags(const char *p, int *len);
141 static char *mark_up(const char *start, int inlen, int *lenp);
143 static Evas_Object *image_provider(void *images, Evas_Object *entry, const char *item);
144 static void entry_deleted(void *images, Evas *e, Evas_Object *entry, void *unused);
147 static Eina_Bool targets_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
148 static Eina_Bool text_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
149 static Eina_Bool html_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
150 static Eina_Bool edje_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
151 static Eina_Bool uri_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
152 static Eina_Bool image_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
153 static Eina_Bool vcard_send(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
155 static int response_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *);
157 static int notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
158 static int notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
159 static int notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
160 static int notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
161 static int notify_handler_edje(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
162 static int notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
163 static int vcard_receive(Cnp_Selection *sed, Ecore_X_Event_Selection_Notify *notify);
165 static Paste_Image *pasteimage_alloc(const char *file, int pathlen);
166 static Eina_Bool pasteimage_append(Paste_Image *pi, Evas_Object *entry);
167 static void pasteimage_free(Paste_Image *pi);
169 /* Optimisation: Turn this into a 256 byte table:
170 * then can lookup in one index, not N checks */
171 static const Escape escapes[] = {
180 #define N_ESCAPES ((int)(sizeof(escapes) / sizeof(escapes[0])))
182 static Cnp_Atom atoms[CNP_N_ATOMS] = {
183 [CNP_ATOM_TARGETS] = {
185 (Elm_Sel_Format) -1, // everything
187 response_handler_targets,
188 notify_handler_targets,
192 "application/x-elementary-markup",
193 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
199 [CNP_ATOM_text_uri] = {
201 ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_IMAGE, /* Either images or entries */
207 [CNP_ATOM_text_urilist] = {
209 ELM_SEL_FORMAT_IMAGE,
215 [CNP_ATOM_text_x_vcard] = {
217 ELM_SEL_FORMAT_VCARD,
221 [CNP_ATOM_image_png] = {
223 ELM_SEL_FORMAT_IMAGE,
226 notify_handler_image,
229 [CNP_ATOM_image_jpeg] = {
231 ELM_SEL_FORMAT_IMAGE,
234 notify_handler_image,/* Raw image data is the same */
237 [CNP_ATOM_image_bmp] = {
239 ELM_SEL_FORMAT_IMAGE,
242 notify_handler_image,/* Raw image data is the same */
245 [CNP_ATOM_image_gif] = {
247 ELM_SEL_FORMAT_IMAGE,
250 notify_handler_image,/* Raw image data is the same */
253 [CNP_ATOM_image_tiff] = {
255 ELM_SEL_FORMAT_IMAGE,
258 notify_handler_image,/* Raw image data is the same */
261 [CNP_ATOM_image_svg] = {
263 ELM_SEL_FORMAT_IMAGE,
266 notify_handler_image,/* Raw image data is the same */
269 [CNP_ATOM_image_xpm] = {
271 ELM_SEL_FORMAT_IMAGE,
274 notify_handler_image,/* Raw image data is the same */
277 [CNP_ATOM_image_tga] = {
279 ELM_SEL_FORMAT_IMAGE,
282 notify_handler_image,/* Raw image data is the same */
285 [CNP_ATOM_image_ppm] = {
286 "image/x-portable-pixmap",
287 ELM_SEL_FORMAT_IMAGE,
290 notify_handler_image,/* Raw image data is the same */
293 [CNP_ATOM_text_html_utf8] = {
294 "text/html;charset=utf-8",
301 [CNP_ATOM_text_html] = {
306 notify_handler_html, /* No encoding: Webkit only */
309 [CNP_ATOM_UTF8STRING] = {
311 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
317 [CNP_ATOM_STRING] = {
319 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
327 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
333 [CNP_ATOM_text_plain_utf8] = {
334 "text/plain;charset=utf-8",
335 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
341 [CNP_ATOM_text_plain] = {
343 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
351 static Cnp_Selection selections[ELM_SEL_MAX] = {
352 ARRAYINIT(ELM_SEL_PRIMARY) {
354 .ecore_sel = ECORE_X_SELECTION_PRIMARY,
355 .set = ecore_x_selection_primary_set,
356 .clear = ecore_x_selection_primary_clear,
357 .request = ecore_x_selection_primary_request,
359 ARRAYINIT(ELM_SEL_SECONDARY) {
360 .debug = "Secondary",
361 .ecore_sel = ECORE_X_SELECTION_SECONDARY,
362 .set = ecore_x_selection_secondary_set,
363 .clear = ecore_x_selection_secondary_clear,
364 .request = ecore_x_selection_secondary_request,
366 ARRAYINIT(ELM_SEL_CLIPBOARD) {
367 .debug = "Clipboard",
368 .ecore_sel = ECORE_X_SELECTION_CLIPBOARD,
369 .set = ecore_x_selection_clipboard_set,
370 .clear = ecore_x_selection_clipboard_clear,
371 .request = ecore_x_selection_clipboard_request,
373 ARRAYINIT(ELM_SEL_XDND) {
375 .ecore_sel = ECORE_X_SELECTION_XDND,
376 .request = ecore_x_selection_xdnd_request,
380 /* Data for DND in progress */
381 static Saved_Type savedtypes = { NULL, NULL, 0, 0, 0, EINA_FALSE };
383 static void (*dragdonecb) (void *data, Evas_Object *obj) = NULL;
384 static void *dragdonedata = NULL;
386 static int _elm_cnp_init_count = 0;
387 /* FIXME: who left this out of XAtoms.h */
388 static Ecore_X_Atom clipboard_atom;
390 static Eina_List *pastedimages = NULL;
393 * Drag & Drop functions
396 /* FIXME: Way too many globals */
397 static Eina_List *drops = NULL;
398 static Evas_Object *dragwin = NULL;
399 static int _dragx = 0, _dragy = 0;
400 static Ecore_Event_Handler *handler_pos = NULL;
401 static Ecore_Event_Handler *handler_drop = NULL;
402 static Ecore_Event_Handler *handler_enter = NULL;
403 static Ecore_Event_Handler *handler_status = NULL;
407 /* Stringshared, so I can just compare pointers later */
408 static const char *text_uri;
411 /* For convert EFL to HTML */
413 #define TAGPOS_START 0x00000001
414 #define TAGPOS_END 0x00000002
415 #define TAGPOS_ALONE 0x00000003
417 /* TEXTBLOCK tag using stack but close tag word has no mean maybe bug...
418 * TEXTBLOCK <b>bold<font>font</b>bold</font>
419 * HTML <b>bold<font>font bold</b>font</font> */
421 typedef struct _TagTable {
427 TagTable _EFLtoHTMLConvertTable[] = {
429 {"underline", "del", 0},
430 {"strikethrough", "ins", 0},
436 TagTable _HTMLtoEFLConvertTable[] = {
438 {"del", "underline", 0},
439 {"u", "underline", 0},
440 {"ins", "strikethrough", 0},
441 {"s", "strikethrough", 0},
449 typedef struct _TagNode TagNode, *PTagNode;
451 char *tag; //EINA_STRINGSHARE if NULL just str
454 char *pos_in_ori_str;
457 unsigned char tagPosType;
460 typedef struct _FontTagData FontTagData, *PFontTagData;
461 struct _FontTagData {
469 typedef struct _ItemTagData ItemTagData, *PItemTagData;
470 struct _ItemTagData {
476 #define SAFEFREE(ptr) \
484 #define freeAndAssign(dst, value) \
495 static PTagNode _new_tag_node(char *tag, char *tag_str, char* str, char *pos_in_ori_str);
496 static PTagNode _get_start_node(char *str);
497 static PTagNode _get_next_node(PTagNode prev);
498 static void _delete_node(PTagNode node);
499 static void _link_match_tags(Eina_List *nodes);
500 static char *_get_tag_value(const char *tag_str, const char *tag_name);
501 static char *_convert_to_html(Eina_List* nodes);
502 static void _set_EFL_tag_data(Eina_List* nodes);
503 static char *_convert_to_edje(Eina_List* nodes);
504 static void _set_HTML_tag_data(Eina_List* nodes);
505 static PFontTagData _set_EFL_font_data(PFontTagData data, const char *tag_str);
506 static PItemTagData _set_EFL_item_data(PItemTagData data, const char *tag_str);
507 static PFontTagData _set_HTML_font_data(PFontTagData data, const char *tag_str);
508 static PItemTagData _set_HTML_img_data(PItemTagData data, const char *tag_str);
511 static void _dumpNode(Eina_List* nodes);
515 _new_tag_node(char *tag, char *tag_str, char* str, char *pos_in_ori_str)
517 PTagNode newNode = calloc(1, sizeof(TagNode));
519 newNode->tag_str = tag_str;
521 newNode->pos_in_ori_str = pos_in_ori_str;
526 _get_start_node(char *str)
528 char *startStr = NULL;
529 if (!str || str[0] == '\0')
534 char *tagStart = strchr(str, '<');
536 startStr = strdup(str);
539 int strLength = tagStart - str;
540 startStr = malloc(sizeof(char) * (strLength + 1));
541 strncpy(startStr, str, strLength);
542 startStr[strLength] = '\0';
546 return _new_tag_node(NULL, NULL, startStr, str);
550 _get_next_node(PTagNode prev)
552 PTagNode retTag = NULL;
555 char *tagNameEnd = NULL;
558 if (prev->tag == NULL)
559 tagStart = strchr(prev->pos_in_ori_str, '<');
561 tagStart = strchr(prev->pos_in_ori_str + 1, '<');
566 tagEnd = strchr(tagStart, '>');
567 nextTagStart = strchr(tagStart + 1, '<');
569 if (!tagEnd || (nextTagStart && (nextTagStart < tagEnd)))
570 return _get_start_node(tagStart + 1);
573 char *spArray[spCnt];
574 spArray[0] = strchr(tagStart, '=');
575 spArray[1] = strchr(tagStart, '_');
576 spArray[2] = strchr(tagStart, ' ');
577 spArray[3] = strchr(tagStart, '\t');
578 spArray[4] = strchr(tagStart, '\n');
582 for (i = 0; i < spCnt; i++)
584 if (spArray[i] && spArray[i] < tagNameEnd)
585 tagNameEnd = spArray[i];
588 int tagLength = tagNameEnd - tagStart - 1;
589 char *tagName = NULL;
590 if (!strncmp(&tagStart[1], "color", tagLength))
591 tagName = strndup("font", 4);
592 else if (!strncmp(&tagStart[1], "/color", tagLength))
593 tagName = strndup("/font", 5);
594 else if (!strncmp(&tagStart[1], "/item", tagLength))
595 tagName = strdup("");
597 tagName = strndup(&tagStart[1], tagLength);
599 int tagStrLength = 0;
603 tagStrLength = tagEnd - tagStart + 1;
604 tagStr = strndup(tagStart, tagStrLength);
607 unsigned int strLength = nextTagStart ? (unsigned int)(nextTagStart - tagEnd - 1) : strlen(&tagEnd[1]);
608 char *str = strndup(&tagEnd[1], strLength);
610 retTag = _new_tag_node(tagName, tagStr, str, tagStart);
616 _delete_node(PTagNode node)
620 SAFEFREE(node->tag_str);
627 if (!strcmp("font", node->tag))
629 PFontTagData data = node->tagData;
630 SAFEFREE(data->name);
631 SAFEFREE(data->color);
632 SAFEFREE(data->size);
633 SAFEFREE(data->bg_color);
635 if (!strcmp("item", node->tag))
637 PItemTagData data = node->tagData;
638 SAFEFREE(data->href);
639 SAFEFREE(data->width);
640 SAFEFREE(data->height);
644 SAFEFREE(node->tagData);
652 _link_match_tags(Eina_List *nodes)
654 Eina_List *stack = NULL;
656 PTagNode trail, popData;
659 EINA_LIST_FOREACH(nodes, l, trail)
661 if (!trail->tag || trail->tag[0] == '\0')
663 if (!strcmp("br", trail->tag))
665 trail->tagPosType = TAGPOS_ALONE;
668 else if (!strcmp("item", trail->tag) || !strcmp("img", trail->tag))
670 trail->tagPosType = TAGPOS_ALONE;
674 if (trail->tag[0] != '/') // PUSH
676 stack = eina_list_append(stack, trail);
677 /* eina_array_push(stack, trail);
678 cnp_debug("stack: %d, tag %s\n", eina_array_count_get(stack), trail->tag);*/
679 cnp_debug("stack: %d, tag %s\n", eina_list_count(stack), trail->tag);
683 if (!eina_list_count(stack))
685 cnp_debug("tag not matched %s\n", trail->tag);
689 EINA_LIST_REVERSE_FOREACH(stack, r, popData)
691 if (popData->tag && !strcmp(popData->tag, &trail->tag[1]))
693 popData->tagPosType = TAGPOS_START;
694 trail->tagPosType = TAGPOS_END;
695 popData->matchTag = trail;
696 trail->matchTag = popData;
697 stack = eina_list_remove_list(stack, r);
701 /* popData = eina_array_pop(stack);
703 popData->tagPosType = TAGPOS_START;
704 trail->tagPosType = TAGPOS_END;
705 popData->matchTag = trail;
706 trail->matchTag = popData;
707 cnp_debug("pop stack: %d, tag %s\n", eina_array_count_get(stack), trail->tag);
712 /* if (eina_array_count_get(stack))
713 cnp_debug("stack state: %d, tag %s\n", eina_array_count_get(stack), trail->tag);*/
715 /* Make Dummy close tag */
716 /* while ((popData = eina_array_pop(stack))) */
718 EINA_LIST_REVERSE_FOREACH(stack, r, popData)
721 int tagLength = strlen(popData->tag);
722 char *tagName = malloc(sizeof(char) * (tagLength + 2));
726 strcat(tagName, popData->tag);
728 newData = _new_tag_node(tagName, NULL, NULL, NULL);
729 popData->tagPosType = TAGPOS_START;
730 newData->tagPosType = TAGPOS_END;
731 popData->matchTag = newData;
732 newData->matchTag = popData;
733 nodes = eina_list_append(nodes, newData);
734 /* cnp_debug("stack: %d, tag %s\n", eina_array_count_get(stack), popData->tag);*/
736 /* cnp_debug("stack_top: %d\n", eina_array_count_get(stack));
737 eina_array_free(stack);*/
738 eina_list_free(stack);
742 _get_tag_value(const char *tag_str, const char *tag_name)
744 if (!tag_name || !tag_str)
748 if ((tag = strstr(tag_str, tag_name)))
750 if (tag[strlen(tag_name)] == '_')
752 char *value = strchr(tag, '=');
758 } while (!isalnum(*value) && *value != '#');
761 char *spArray[spCnt];
762 spArray[0] = strchr(value, ' ');
763 spArray[1] = strchr(value, '>');
764 spArray[2] = strchr(value, '\"');
765 spArray[3] = strchr(value, '\'');
766 spArray[4] = strchr(value, '\t');
767 spArray[5] = strchr(value, '\n');
768 char *valueEnd = strchr(value, '\0');
771 for (i = 0; i < spCnt; i++)
773 if (spArray[i] && spArray[i] < valueEnd)
774 valueEnd = spArray[i];
777 int valueLength = valueEnd - value;
778 return strndup(value, valueLength);
785 _set_EFL_font_data(PFontTagData data, const char *tag_str)
790 data = calloc(1, sizeof(FontTagData));
791 value = _get_tag_value(tag_str, "font_size");
792 freeAndAssign(data->size, value);
793 value = _get_tag_value(tag_str, "color");
794 freeAndAssign(data->color, value);
795 value = _get_tag_value(tag_str, "bgcolor");
796 freeAndAssign(data->bg_color, value);
797 value = _get_tag_value(tag_str, "font");
798 freeAndAssign(data->name, value);
804 _set_EFL_item_data(PItemTagData data, const char *tag_str)
809 data = calloc(1, sizeof(ItemTagData));
810 value = _get_tag_value(tag_str, "href");
813 char *path = strstr(value, "file://");
816 char *modify = malloc(sizeof(char) * (strlen(value) + 1));
817 strncpy(modify, "file://", 7);
820 while (path[1] && path[0] && path[1] == '/' && path[0] == '/')
824 strcat(modify, path);
826 cnp_debug("image href ---%s---\n", data->href);
829 freeAndAssign(data->href, value);
833 value = _get_tag_value(tag_str, "absize");
836 char *xpos = strchr(value, 'x');
839 int absizeLen = strlen(value);
840 freeAndAssign(data->width, strndup(value, xpos - value));
841 freeAndAssign(data->height, strndup(xpos + 1, absizeLen - (xpos - value) - 1));
842 cnp_debug("image width: -%s-, height: -%s-\n", data->width, data->height);
850 _set_EFL_tag_data(Eina_List* nodes)
855 EINA_LIST_FOREACH(nodes, l, trail)
859 if (!strcmp("font", trail->tag))
860 trail->tagData = _set_EFL_font_data(trail->tagData, trail->tag_str);
861 else if (!strcmp("item", trail->tag))
862 trail->tagData = _set_EFL_item_data(trail->tagData, trail->tag_str);
867 _set_HTML_font_data(PFontTagData data, const char *tag_str)
872 data = calloc(1, sizeof(FontTagData));
873 value = _get_tag_value(tag_str, "size");
874 freeAndAssign(data->size, value);
875 value = _get_tag_value(tag_str, "color");
876 freeAndAssign(data->color, value);
877 value = _get_tag_value(tag_str, "bgcolor");
878 freeAndAssign(data->bg_color, value);
879 value = _get_tag_value(tag_str, "face");
880 freeAndAssign(data->name, value);
886 _set_HTML_img_data(PItemTagData data, const char *tag_str)
891 data = calloc(1, sizeof(ItemTagData));
892 value = _get_tag_value(tag_str, "src");
895 char *path = strstr(value, "file://");
898 char *modify = malloc(sizeof(char) * (strlen(value) + 1));
899 strncpy(modify, "file://", 7);
902 while (path[1] && path[0] && path[1] == '/' && path[0] == '/')
906 strcat(modify, path);
908 cnp_debug("image src ---%s---\n", data->href);
911 freeAndAssign(data->href, value);
915 value = _get_tag_value(tag_str, "width");
916 freeAndAssign(data->width, value);
917 value = _get_tag_value(tag_str, "height");
918 freeAndAssign(data->height, value);
923 _set_HTML_tag_data(Eina_List* nodes)
928 EINA_LIST_FOREACH(nodes, l, trail)
932 if (!strcmp("font", trail->tag))
933 trail->tagData = _set_HTML_font_data(trail->tagData, trail->tag_str);
934 else if (!strcmp("img", trail->tag))
935 trail->tagData = _set_HTML_img_data(trail->tagData, trail->tag_str);
941 _dumpNode(Eina_List* nodes)
946 EINA_LIST_FOREACH(nodes, l, trail)
948 cnp_debug("tag: %s, tag_str: %s, str: %s, tagPosType: %d\n",
949 trail->tag, trail->tag_str, trail->str, trail->tagPosType);
950 cnp_debug("matchTag: %x ", (unsigned int)trail->matchTag);
952 cnp_debug("matchTag->tag_str: %s", trail->matchTag->tag_str);
955 if (!strcmp(trail->tag, "font"))
957 PFontTagData data = trail->tagData;
958 cnp_debug(" tagData->name: %s, tagData->color: %s, tagData->size: %s, tagData->bg_color: %s",
959 data->name, data->color, data->size, data->bg_color);
961 else if (!strcmp(trail->tag, "item") || !strcmp(trail->tag, "img"))
963 PItemTagData data = trail->tagData;
964 cnp_debug(" tagData->href: %s, tagData->width: %s, tagData->height: %s",
965 data->href, data->width, data->height);
968 cnp_debug("\nERROR!!!! not need tagData");
976 _convert_to_html(Eina_List* nodes)
981 Eina_Strbuf *html = eina_strbuf_new();
983 int tableCnt = sizeof(_EFLtoHTMLConvertTable) / sizeof(TagTable);
985 EINA_LIST_FOREACH(nodes, l, trail)
989 char *tagName = trail->tagPosType == TAGPOS_END ?
990 trail->matchTag->tag : trail->tag;
992 for(j = 0; j < tableCnt; j++)
994 if (!strcmp(_EFLtoHTMLConvertTable[j].src, tagName))
996 switch(trail->tagPosType)
999 eina_strbuf_append(html, "</");
1002 eina_strbuf_append(html, "<");
1006 eina_strbuf_append(html, _EFLtoHTMLConvertTable[j].dst);
1007 if (trail->tagPosType != TAGPOS_END)
1009 if (!strcmp(_EFLtoHTMLConvertTable[j].src, "font"))
1011 PFontTagData data = trail->tagData;
1016 eina_strbuf_append_printf(html, " color=\"%s\"", data->color);
1018 eina_strbuf_append_printf(html, " size=\"%s\"", data->size);
1023 else if (!strcmp(_EFLtoHTMLConvertTable[j].src, "item"))
1025 PItemTagData data = trail->tagData;
1027 eina_strbuf_append_printf(html, " src=\"%s\"", data->href);
1029 eina_strbuf_append_printf(html, " width=\"%s\"", data->width);
1031 eina_strbuf_append_printf(html, " height=\"%s\"", data->height);
1034 switch(trail->tagPosType)
1037 eina_strbuf_append(html, " />");
1040 eina_strbuf_append(html, ">");
1048 eina_strbuf_append(html, trail->str);
1051 char *ret = eina_strbuf_string_steal(html);
1052 eina_strbuf_free(html);
1056 #define IMAGE_DEFAULT_WIDTH "240"
1057 #define IMAGE_DEFAULT_HEIGHT "180"
1061 _convert_to_edje(Eina_List* nodes)
1066 Eina_Strbuf *html = eina_strbuf_new();
1068 int tableCnt = sizeof(_HTMLtoEFLConvertTable) / sizeof(TagTable);
1070 EINA_LIST_FOREACH(nodes, l, trail)
1074 char *tagName = trail->tagPosType == TAGPOS_END ?
1075 trail->matchTag->tag : trail->tag;
1077 for(j = 0; j < tableCnt; j++)
1079 if (!strcmp(_HTMLtoEFLConvertTable[j].src, tagName))
1081 if (_HTMLtoEFLConvertTable[j].dst[0] != '\0')
1083 switch(trail->tagPosType)
1086 eina_strbuf_append(html, "</");
1089 eina_strbuf_append(html, "<");
1093 eina_strbuf_append(html, _HTMLtoEFLConvertTable[j].dst);
1095 if (trail->tagPosType != TAGPOS_END)
1097 if (!strcmp(_HTMLtoEFLConvertTable[j].src, "font"))
1099 PFontTagData data = trail->tagData;
1104 eina_strbuf_append_printf(html, "<color=%s>", data->color);
1106 eina_strbuf_append_printf(html, "<font_size=%s>", data->size);
1112 else if (!strcmp(_HTMLtoEFLConvertTable[j].src, "img"))
1114 PItemTagData data = trail->tagData;
1115 char *width = IMAGE_DEFAULT_WIDTH, *height = IMAGE_DEFAULT_HEIGHT; if (data->width)
1116 width = data->width;
1118 height = data->height;
1119 eina_strbuf_append_printf(html, " absize=%sx%s", width, height);
1121 eina_strbuf_append_printf(html, " href=%s></item>", data->href);
1127 if (_HTMLtoEFLConvertTable[j].dst[0] == '\0')
1129 if (!strcmp(_HTMLtoEFLConvertTable[j].src, "font"))
1131 if (trail->matchTag->tagData)
1133 PFontTagData data = trail->matchTag->tagData;
1138 eina_strbuf_append_printf(html, "</color>");
1140 eina_strbuf_append_printf(html, "</font>");
1149 switch(trail->tagPosType)
1152 eina_strbuf_append(html, " />");
1155 eina_strbuf_append(html, ">");
1160 }/* for(j = 0; j < tableCnt; j++) end */
1163 eina_strbuf_append(html, trail->str);
1166 char *ret = eina_strbuf_string_steal(html);
1167 eina_strbuf_free(html);
1172 elm_selection_set(Elm_Sel_Type selection, Evas_Object *widget, Elm_Sel_Format format, const char *selbuf)
1174 #ifdef HAVE_ELEMENTARY_X
1177 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
1178 if (!_elm_cnp_init_count) _elm_cnp_init();
1179 if ((!selbuf) && (format != ELM_SEL_FORMAT_IMAGE))
1180 return elm_selection_clear(selection, widget);
1182 sel = selections + selection;
1185 sel->widget = widget;
1187 sel->set(elm_win_xwindow_get(widget),&selection,sizeof(Elm_Sel_Type));
1188 sel->format = format;
1189 sel->selbuf = selbuf ? strdup(selbuf) : NULL;
1198 elm_selection_clear(Elm_Sel_Type selection, Evas_Object *widget)
1200 #ifdef HAVE_ELEMENTARY_X
1203 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
1204 if (!_elm_cnp_init_count) _elm_cnp_init();
1206 sel = selections + selection;
1208 /* No longer this selection: Consider it gone! */
1209 if ((!sel->active) || (sel->widget != widget)) return EINA_TRUE;
1222 elm_selection_get(Elm_Sel_Type selection, Elm_Sel_Format format,
1223 Evas_Object *widget, Elm_Drop_Cb datacb, void *udata)
1225 #ifdef HAVE_ELEMENTARY_X
1229 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
1230 if (!_elm_cnp_init_count) _elm_cnp_init();
1232 sel = selections + selection;
1233 top = elm_widget_top_get(widget);
1234 if (!top) return EINA_FALSE;
1236 sel->requestformat = format;
1237 sel->requestwidget = widget;
1238 sel->request(elm_win_xwindow_get(top), ECORE_X_SELECTION_TARGET_TARGETS);
1239 sel->datacb = datacb;
1248 #ifdef HAVE_ELEMENTARY_X
1255 if (_elm_cnp_init_count++) return EINA_TRUE;
1256 for (i = 0; i < CNP_N_ATOMS; i++)
1258 atoms[i].atom = ecore_x_atom_get(atoms[i].name);
1259 ecore_x_selection_converter_atom_add(atoms[i].atom,
1260 atoms[i].converter);
1262 clipboard_atom = ecore_x_atom_get("CLIPBOARD");
1264 ecore_event_handler_add(ECORE_X_EVENT_SELECTION_CLEAR, selection_clear, NULL);
1265 ecore_event_handler_add(ECORE_X_EVENT_SELECTION_NOTIFY, selection_notify, NULL);
1267 text_uri = eina_stringshare_add("text/uri-list");
1272 selection_clear(void *udata __UNUSED__, int type __UNUSED__, void *event)
1274 Ecore_X_Event_Selection_Clear *ev = event;
1278 for (i = 0; i < ELM_SEL_MAX; i++)
1280 if (selections[i].ecore_sel == ev->selection) break;
1282 cnp_debug("selection %d clear\n", i);
1283 /* Not me... Don't care */
1284 if (i == ELM_SEL_MAX) return ECORE_CALLBACK_PASS_ON;
1286 sel = selections + i;
1291 return ECORE_CALLBACK_PASS_ON;
1296 * Response to a selection notify:
1297 * - So we have asked for the selection list.
1298 * - If it's the targets list, parse it, and fire of what we want,
1299 * else it's the data we want.
1302 selection_notify(void *udata __UNUSED__, int type __UNUSED__, void *event)
1304 Ecore_X_Event_Selection_Notify *ev = event;
1308 cnp_debug("selection notify callback: %d\n",ev->selection);
1309 switch (ev->selection)
1311 case ECORE_X_SELECTION_CLIPBOARD:
1312 sel = selections + ELM_SEL_CLIPBOARD;
1314 case ECORE_X_SELECTION_PRIMARY:
1315 sel = selections + ELM_SEL_PRIMARY;
1317 case ECORE_X_SELECTION_SECONDARY:
1318 sel = selections + ELM_SEL_SECONDARY;
1320 case ECORE_X_SELECTION_XDND:
1321 sel = selections + ELM_SEL_XDND;
1324 return ECORE_CALLBACK_PASS_ON;
1326 cnp_debug("Target is %s\n", ev->target);
1328 for (i = 0; i < CNP_N_ATOMS; i++)
1330 if (!strcmp(ev->target, atoms[i].name))
1332 if (atoms[i].notify)
1334 cnp_debug("Found something: %s\n", atoms[i].name);
1335 atoms[i].notify(sel, ev);
1339 cnp_debug("Ignored: No handler!\n");
1344 return ECORE_CALLBACK_PASS_ON;
1350 targets_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize)
1356 if (!data_ret) return EINA_FALSE;
1358 sel = selections + *((int *)data);
1360 for (i = 0, count = 0; i < CNP_N_ATOMS ; i++)
1362 if (sel->format & atoms[i].formats) count++;
1365 aret = malloc(sizeof(Ecore_X_Atom) * count);
1366 for (i = 0, count = 0; i < CNP_N_ATOMS; i++)
1368 if (sel->format & atoms[i].formats) aret[count ++] = atoms[i].atom;
1372 if (typesize) *typesize = 32 /* urk */;
1373 if (ttype) *ttype = ECORE_X_ATOM_ATOM;
1374 if (size_ret) *size_ret = count;
1380 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__)
1382 cnp_debug("Image converter called\n");
1387 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__)
1391 cnp_debug("Vcard send called\n");
1393 sel = selections + *((int *)data);
1395 if (data_ret) *data_ret = strdup(sel->selbuf);
1396 if (size_ret) *size_ret = strlen(sel->selbuf);
1401 * Callback to handle a targets response on a selection request:
1402 * So pick the format we'd like; and then request it.
1405 notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1407 Ecore_X_Selection_Data_Targets *targets;
1408 Ecore_X_Atom *atomlist;
1411 targets = notify->data;
1412 atomlist = (Ecore_X_Atom *)(targets->data.data);
1414 for (j = 1; j < CNP_N_ATOMS; j++)
1416 cnp_debug("\t%s %d\n", atoms[j].name, atoms[j].atom);
1417 if (!(atoms[j].formats & sel->requestformat)) continue;
1418 for (i = 0; i < targets->data.length; i++)
1420 if ((atoms[j].atom == atomlist[i]) && (atoms[j].notify))
1422 cnp_debug("Atom %s matches\n",atoms[j].name);
1428 cnp_debug("Couldn't find anything that matches\n");
1429 return ECORE_CALLBACK_PASS_ON;
1432 cnp_debug("Sending request for %s\n",atoms[j].name);
1433 sel->request(elm_win_xwindow_get(sel->requestwidget), atoms[j].name);
1435 return ECORE_CALLBACK_PASS_ON;
1439 response_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1441 Ecore_X_Selection_Data_Targets *targets;
1442 Ecore_X_Atom *atomlist;
1446 targets = notify->data;
1447 atomlist = (Ecore_X_Atom *)(targets->data.data);
1449 /* Start from 1: Skip targets */
1450 for (j = 1 ; j < CNP_N_ATOMS ; j ++)
1452 if (!(atoms[j].formats & sel->requestformat)) continue;
1453 for (i = 0 ; i < targets->data.length ; i ++)
1455 if ((atoms[j].atom == atomlist[i]) && (atoms[j].response))
1457 /* Found a match: Use it */
1463 if (j == CNP_N_ATOMS)
1465 cnp_debug("No matching type found\n");
1469 top = elm_widget_top_get(sel->requestwidget);
1472 sel->request(elm_win_xwindow_get(top), atoms[j].name);
1478 notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1480 Ecore_X_Selection_Data *data;
1483 data = notify->data;
1487 Elm_Selection_Data ddata;
1489 str = mark_up((char *)data->data, data->length, NULL);
1490 ddata.x = ddata.y = 0;
1491 ddata.format = ELM_SEL_FORMAT_TEXT;
1493 ddata.len = data->length;
1494 sel->datacb(sel->udata, sel->widget, &ddata);
1499 cnp_debug("Notify handler text %d %d %p\n", data->format,data->length, data->data);
1500 str = mark_up((char *)data->data, data->length, NULL);
1501 cnp_debug("String is %s (from %s)\n", str, data->data);
1502 elm_entry_entry_insert(sel->requestwidget, str);
1509 * So someone is pasting an image into my entry or widget...
1512 notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1514 Ecore_X_Selection_Data *data;
1515 Ecore_X_Selection_Data_Files *files;
1519 data = notify->data;
1520 cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
1521 if (data->content == ECORE_X_SELECTION_CONTENT_FILES)
1523 cnp_debug("got a files list\n");
1524 files = notify->data;
1525 if (files->num_files > 1)
1527 /* Don't handle many items */
1528 cnp_debug("more then one file: Bailing\n");
1531 p = files->files[0];
1535 p = (char *)data->data;
1540 cnp_debug("Couldn't find a file\n");
1543 cnp_debug("Got %s\n",p);
1546 Elm_Selection_Data ddata;
1548 ddata.x = ddata.y = 0;
1549 ddata.format = ELM_SEL_FORMAT_MARKUP;
1551 ddata.len = data->length;
1552 sel->datacb(sel->udata, sel->widget, &ddata);
1555 if (strncmp(p, "file://", 7))
1557 /* Try and continue if it looks sane */
1558 if (*p != '/') return 0;
1562 p += strlen("file://");
1565 if (savedtypes.pi) pasteimage_free(savedtypes.pi);
1566 pi = pasteimage_alloc(p, strlen(p));
1567 if (savedtypes.textreq)
1569 savedtypes.textreq = 0;
1574 pasteimage_append(pi, sel->requestwidget);
1575 savedtypes.pi = NULL;
1581 * Just receieved an vcard, either through cut and paste, or dnd.
1584 vcard_receive(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1588 Ecore_X_Selection_Data *data;
1590 data = notify->data;
1591 cnp_debug("vcard receive\n");
1593 if (sel == (selections + ELM_SEL_XDND))
1595 Elm_Selection_Data ddata;
1597 cnp_debug("drag & drop\n");
1598 /* FIXME: this needs to be generic: Used for all receives */
1599 EINA_LIST_FOREACH(drops, l, dropable)
1601 if (dropable->obj == sel->requestwidget) break;
1605 cnp_debug("Unable to find drop object");
1606 ecore_x_dnd_send_finished();
1609 dropable = eina_list_data_get(l);
1610 ddata.x = savedtypes.x;
1611 ddata.y = savedtypes.y;
1612 ddata.format = ELM_SEL_FORMAT_VCARD;
1613 ddata.data = data->data;
1614 ddata.len = data->length;
1615 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1616 ecore_x_dnd_send_finished();
1618 else if (sel->datacb)
1620 Elm_Selection_Data ddata;
1621 ddata.x = ddata.y = 0;
1622 ddata.format = ELM_SEL_FORMAT_IMAGE;
1623 ddata.data = data->data;
1624 ddata.len = data->length;
1625 sel->datacb(sel->udata, sel->widget, &ddata);
1629 cnp_debug("Paste request\n");
1638 notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1640 Ecore_X_Selection_Data *data;
1644 cnp_debug("got a png (or a jpeg)!\n");
1645 data = notify->data;
1647 cnp_debug("Size if %d\n", data->length);
1651 Elm_Selection_Data ddata;
1653 ddata.x = ddata.y = 0;
1654 ddata.format = ELM_SEL_FORMAT_IMAGE;
1655 ddata.data = data->data;
1656 ddata.len = data->length;
1657 sel->datacb(sel->udata, sel->widget, &ddata);
1661 /* generate tmp name */
1662 tmp = elm_cnp_tempfile_create(data->length);
1663 memcpy(tmp->map, data->data, data->length);
1664 munmap(tmp->map,data->length);
1666 /* FIXME: Add to paste image data to clean up */
1667 pi = pasteimage_alloc(tmp->filename, strlen(tmp->filename));
1668 pasteimage_append(pi, sel->requestwidget);
1675 notify_handler_edje(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1677 Ecore_X_Selection_Data *data;
1679 data = notify->data;
1681 char *stripstr = NULL;
1682 stripstr = malloc(sizeof(char) * (data->length + 1));
1683 strncpy(stripstr, (char *)data->data, data->length);
1684 stripstr[data->length] = '\0';
1688 Elm_Selection_Data ddata;
1689 ddata.x = ddata.y = 0;
1690 ddata.format = ELM_SEL_FORMAT_MARKUP;
1691 ddata.data = stripstr;
1692 ddata.len = data->length;
1693 sel->datacb(sel->udata, sel->widget, &ddata);
1696 elm_entry_entry_insert(sel->requestwidget, stripstr);
1698 cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
1703 * Warning: Generic text/html can';t handle it sanely.
1704 * Firefox sends ucs2 (i think).
1705 * chrome sends utf8... blerg
1708 notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1710 Ecore_X_Selection_Data *data;
1712 cnp_debug("Got some HTML: Checking encoding is useful\n");
1713 data = notify->data;
1715 char *stripstr = NULL;
1716 stripstr = malloc(sizeof(char) * (data->length + 1));
1717 strncpy(stripstr, (char *)data->data, data->length);
1718 stripstr[data->length] = '\0';
1722 Elm_Selection_Data ddata;
1723 ddata.x = ddata.y = 0;
1724 ddata.format = ELM_SEL_FORMAT_HTML;
1725 ddata.data = stripstr;
1726 ddata.len = data->length;
1727 sel->datacb(sel->udata, sel->widget, &ddata);
1730 elm_entry_entry_insert(sel->requestwidget, stripstr);
1732 cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
1739 text_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1743 cnp_debug("text converter\n");
1744 sel = selections + *((int *)data);
1745 if (!sel->active) return EINA_TRUE;
1747 if ((sel->format & ELM_SEL_FORMAT_MARKUP) ||
1748 (sel->format & ELM_SEL_FORMAT_HTML))
1750 *data_ret = remove_tags(sel->selbuf, size_ret);
1752 else if (sel->format & ELM_SEL_FORMAT_TEXT)
1754 *data_ret = strdup(sel->selbuf);
1755 *size_ret = strlen(sel->selbuf);
1757 else if (sel->format & ELM_SEL_FORMAT_IMAGE)
1759 cnp_debug("Image %s\n", evas_object_type_get(sel->widget));
1760 cnp_debug("Elm type: %s\n", elm_object_widget_type_get(sel->widget));
1761 evas_object_image_file_get(elm_photocam_internal_image_get(sel->widget), (const char **)data_ret, NULL);
1762 if (!*data_ret) *data_ret = strdup("No file");
1763 else *data_ret = strdup(*data_ret);
1764 *size_ret = strlen(*data_ret);
1770 edje_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1773 sel = selections + *((int *)data);
1774 /* if (data_ret) *data_ret = strdup(sel->selbuf);
1775 if (size_ret) *size_ret = strlen(sel->selbuf);*/
1778 if (data_ret && (sel->format & ELM_SEL_FORMAT_HTML))
1780 Eina_List *nodeList = NULL;
1784 nodeData = _get_start_node(sel->selbuf);
1788 nodeList = eina_list_append(nodeList, nodeData);
1789 nodeData = _get_next_node(nodeData);
1792 _link_match_tags(nodeList);
1794 _set_HTML_tag_data(nodeList);
1797 _dumpNode(nodeList);
1799 edje = _convert_to_edje(nodeList);
1801 cnp_debug("convert edje: %s\n", edje);
1803 EINA_LIST_FOREACH(nodeList, trail, nodeData)
1804 _delete_node(nodeData);
1805 eina_list_free(nodeList);
1813 *data_ret = strdup(sel->selbuf);
1818 *size_ret = strlen(edje);
1820 *size_ret = strlen(sel->selbuf);
1827 html_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1831 sel = selections + *(int *)data;
1834 if (data_ret && (sel->format & ELM_SEL_FORMAT_MARKUP))
1836 Eina_List *nodeList = NULL;
1840 nodeData = _get_start_node(sel->selbuf);
1844 nodeList = eina_list_append(nodeList, nodeData);
1845 nodeData = _get_next_node(nodeData);
1848 _link_match_tags(nodeList);
1850 _set_EFL_tag_data(nodeList);
1853 _dumpNode(nodeList);
1855 html = _convert_to_html(nodeList);
1857 cnp_debug("convert html: %s\n", html);
1859 EINA_LIST_FOREACH(nodeList, trail, nodeData)
1860 _delete_node(nodeData);
1861 eina_list_free(nodeList);
1868 *data_ret = strdup(sel->selbuf);
1874 *size_ret = strlen(html);
1876 *size_ret = strlen(sel->selbuf);
1883 uri_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1886 sel = selections + *((int *)data);
1887 cnp_debug("Uri converter\n");
1888 if (data_ret) *data_ret = strdup(sel->selbuf);
1889 if (size_ret) *size_ret = strlen(sel->selbuf);
1894 * Image paste provide
1897 /* FIXME: Should add provider for each pasted item: Use data to store it
1899 static Evas_Object *
1900 image_provider(void *images __UNUSED__, Evas_Object *entry, const char *item)
1905 cnp_debug("image provider for %s called\n", item);
1906 EINA_LIST_FOREACH(pastedimages, l, pi)
1908 cnp_debug("is it %s?\n",pi->tag);
1909 if (!strcmp(pi->tag, item))
1913 o = evas_object_image_filled_add(evas_object_evas_get(entry));
1914 /* FIXME: Handle eets */
1915 cnp_debug("file is %s (object is %p)\n", pi->file, o);
1916 evas_object_image_file_set(o, pi->file, NULL);
1917 evas_object_show(o);
1925 static Paste_Image *
1926 pasteimage_alloc(const char *file, int pathlen)
1930 char *buf, *filebuf;
1931 int prefixlen = strlen("file://");
1933 pi = calloc(1, sizeof(Paste_Image));
1934 if (!pi) return NULL;
1936 len = snprintf(NULL, 0, "pasteimage-%p", pi);
1944 snprintf(buf, len, "pasteimage-%p", pi);
1949 if (strstr(file,"file://")) file += prefixlen;
1950 filebuf = alloca(pathlen + 1);
1951 strncpy(filebuf, file, pathlen);
1952 filebuf[pathlen] = 0;
1953 pi->file = strdup(filebuf);
1960 pasteimage_free(Paste_Image *pi)
1963 if (pi->file) free((void*)pi->file);
1964 if (pi->tag) free((void*)pi->tag);
1969 pasteimage_provider_set(Evas_Object *entry)
1974 if (!entry) return EINA_FALSE;
1975 type = elm_widget_type_get(entry);
1976 cnp_debug("type is %s\n", type);
1977 if ((!type) || (strcmp(type, "entry"))) return EINA_FALSE;
1979 v = evas_object_data_get(entry, PROVIDER_SET);
1982 evas_object_data_set(entry, PROVIDER_SET, pasteimage_provider_set);
1983 elm_entry_item_provider_append(entry, image_provider, NULL);
1984 evas_object_event_callback_add(entry, EVAS_CALLBACK_FREE,
1985 entry_deleted, NULL);
1992 pasteimage_append(Paste_Image *pi, Evas_Object *entry)
1996 static const char *tagstring = "<item absize=240x180 href=file://%s></item>";
1998 if (!pi) return EINA_FALSE;
1999 if (!entry) return EINA_FALSE;
2001 pasteimage_provider_set(entry);
2003 len = strlen(tagstring)+strlen(pi->file);
2005 pastedimages = eina_list_append(pastedimages, pi);
2006 entrytag = alloca(len + 1);
2007 snprintf(entrytag, len + 1, tagstring, pi->file);
2008 elm_entry_entry_insert(entry, entrytag);
2014 entry_deleted(void *images __UNUSED__, Evas *e __UNUSED__, Evas_Object *entry, void *unused __UNUSED__)
2019 EINA_LIST_FOREACH_SAFE(pastedimages, l, next, pi)
2021 if (pi->entry == entry)
2022 pastedimages = eina_list_remove_list(pastedimages, l);
2028 remove_tags(const char *p, int *len)
2032 if (!p) return NULL;
2034 q = malloc(strlen(p) + 1);
2035 if (!q) return NULL;
2040 if ((*p != '<') && (*p != '&')) *q++ = *p++;
2043 if ((p[1] == 'b') && (p[2] == 'r') &&
2044 ((p[3] == ' ') || (p[3] == '/') || (p[3] == '>')))
2046 while ((*p) && (*p != '>')) p++;
2052 for (i = 0 ; i < N_ESCAPES ; i++)
2054 if (!strncmp(p,escapes[i].escape, strlen(escapes[i].escape)))
2056 p += strlen(escapes[i].escape);
2057 *q = escapes[i].value;
2062 if (i == N_ESCAPES) *q ++= '&';
2066 if (len) *len = q - ret;
2072 mark_up(const char *start, int inlen, int *lenp)
2077 const char *endp = NULL;
2079 if (!start) return NULL;
2080 if (inlen >= 0) endp = start + inlen;
2081 /* First pass: Count characters */
2082 for (l = 0, p = start; ((!endp) || (p < endp)) && (*p); p++)
2084 for (i = 0 ; i < N_ESCAPES ; i ++)
2086 if (*p == escapes[i].value)
2088 l += strlen(escapes[i].escape);
2092 if (i == N_ESCAPES) l++;
2095 q = ret = malloc(l + 1);
2097 /* Second pass: Change characters */
2098 for (p = start; *p; )
2100 for (i = 0; i < N_ESCAPES; i++)
2102 if (*p == escapes[i].value)
2104 strcpy(q, escapes[i].escape);
2105 q += strlen(escapes[i].escape);
2110 if (i == N_ESCAPES) *q++ = *p++;
2114 if (lenp) *lenp = l;
2120 _dnd_enter(void *data __UNUSED__, int etype __UNUSED__, void *ev)
2122 Ecore_X_Event_Xdnd_Enter *enter = ev;
2126 if ((!enter) || (!enter->num_types) || (!enter->types)) return EINA_TRUE;
2128 cnp_debug("Types\n");
2129 savedtypes.ntypes = enter->num_types;
2130 if (savedtypes.types) free(savedtypes.types);
2131 savedtypes.types = malloc(sizeof(char *) * enter->num_types);
2132 if (!savedtypes.types) return EINA_FALSE;
2134 for (i = 0; i < enter->num_types; i++)
2136 savedtypes.types[i] = eina_stringshare_add(enter->types[i]);
2137 cnp_debug("Type is %s %p %p\n", enter->types[i],
2138 savedtypes.types[i],text_uri);
2139 if (savedtypes.types[i] == text_uri)
2141 /* Request it, so we know what it is */
2142 cnp_debug("Sending uri request\n");
2143 savedtypes.textreq = 1;
2144 savedtypes.pi = NULL; /* FIXME: Free? */
2145 ecore_x_selection_xdnd_request(enter->win, text_uri);
2149 /* FIXME: Find an object and make it current */
2154 _dnd_drop(void *data __UNUSED__, int etype __UNUSED__, void *ev)
2156 struct _Ecore_X_Event_Xdnd_Drop *drop;
2160 Ecore_X_Window xwin;
2161 Elm_Selection_Data ddata;
2167 // check we still have something to drop
2168 if (!drops) return EINA_TRUE;
2170 /* Find any widget in our window; then work out geometry rel to our window */
2171 for (l = drops; l; l = l->next)
2174 xwin = (Ecore_X_Window)ecore_evas_window_get
2175 (ecore_evas_ecore_evas_get(evas_object_evas_get
2177 if (xwin == drop->win) break;
2179 /* didn't find a window */
2180 if (!l) return EINA_TRUE;
2182 /* Calculate real (widget relative) position */
2183 // - window position
2184 // - widget position
2185 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(dropable->obj));
2186 ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
2187 savedtypes.x = drop->position.x - x;
2188 savedtypes.y = drop->position.y - y;
2190 cnp_debug("Drop position is %d,%d\n", savedtypes.x, savedtypes.y);
2192 for (; l; l = l->next)
2195 evas_object_geometry_get(dropable->obj, &x, &y, &w, &h);
2196 if ((savedtypes.x >= x) && (savedtypes.y >= y) &&
2197 (savedtypes.x < x + w) && (savedtypes.y < y + h))
2201 if (!l) return EINA_TRUE; /* didn't find one */
2203 evas_object_geometry_get(dropable->obj, &x, &y, NULL, NULL);
2207 /* Find our type from the previous list */
2208 for (i = 0; i < CNP_N_ATOMS; i++)
2210 for (j = 0; j < savedtypes.ntypes; j++)
2212 if (!strcmp(savedtypes.types[j], atoms[i].name)) goto found;
2216 cnp_debug("Didn't find a target\n");
2220 cnp_debug("Found a target we'd like: %s\n", atoms[i].name);
2221 cnp_debug("0x%x\n",xwin);
2223 if (i == CNP_ATOM_text_urilist)
2225 cnp_debug("We found a URI... (%scached) %s\n",
2226 savedtypes.pi ? "" : "not ",
2227 savedtypes.pi->file);
2231 static const char *tagstring = "<item absize=240x180 href="
2232 "file://%s></item>";
2233 ddata.x = savedtypes.x;
2234 ddata.y = savedtypes.y;
2236 /* If it's markup that also supports images */
2237 if ((dropable->types & ELM_SEL_FORMAT_MARKUP) &&
2238 (dropable->types & ELM_SEL_FORMAT_IMAGE))
2241 ddata.format = ELM_SEL_FORMAT_MARKUP;
2242 pasteimage_provider_set(dropable->obj);
2244 pastedimages = eina_list_append(pastedimages, savedtypes.pi);
2245 len = strlen(tagstring) + strlen(savedtypes.pi->file);
2246 entrytag = alloca(len + 1);
2247 snprintf(entrytag, len + 1, tagstring, savedtypes.pi->file);
2248 ddata.data = entrytag;
2249 cnp_debug("Insert %s\n", (char *)ddata.data);
2250 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
2251 ecore_x_dnd_send_finished();
2254 else if (dropable->types & ELM_SEL_FORMAT_IMAGE)
2256 cnp_debug("Doing image insert (%s)\n", savedtypes.pi->file);
2257 ddata.format = ELM_SEL_FORMAT_IMAGE;
2258 ddata.data = (char *)savedtypes.pi->file;
2259 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
2260 ecore_x_dnd_send_finished();
2262 pasteimage_free(savedtypes.pi);
2263 savedtypes.pi = NULL;
2269 cnp_debug("Item doesn't support images... passing\n");
2270 pasteimage_free(savedtypes.pi);
2274 else if (savedtypes.textreq)
2276 /* Already asked: Pretend we asked now, and paste immediately when
2278 savedtypes.textreq = 0;
2279 ecore_x_dnd_send_finished();
2284 cnp_debug("doing a request then\n");
2285 selections[ELM_SEL_XDND].requestwidget = dropable->obj;
2286 selections[ELM_SEL_XDND].requestformat = ELM_SEL_FORMAT_MARKUP;
2287 selections[ELM_SEL_XDND].active = EINA_TRUE;
2289 ecore_x_selection_xdnd_request(xwin, atoms[i].name);
2294 _dnd_position(void *data __UNUSED__, int etype __UNUSED__, void *ev)
2296 struct _Ecore_X_Event_Xdnd_Position *pos;
2297 Ecore_X_Rectangle rect;
2301 /* Need to send a status back */
2302 /* FIXME: Should check I can drop here */
2303 /* FIXME: Should highlight widget */
2304 rect.x = pos->position.x - 5;
2305 rect.y = pos->position.y - 5;
2308 ecore_x_dnd_send_status(EINA_TRUE, EINA_FALSE, rect, pos->action);
2314 * When dragging this is callback response from the destination.
2315 * The important thing we care about: Can we drop; thus update cursor
2319 _dnd_status(void *data __UNUSED__, int etype __UNUSED__, void *ev)
2321 struct _Ecore_X_Event_Xdnd_Status *status = ev;
2323 if (!status) return EINA_TRUE;
2325 /* Only thing we care about: will accept */
2326 if (status->will_accept)
2328 cnp_debug("Will accept\n");
2331 { /* Won't accept */
2332 cnp_debug("Won't accept accept\n");
2338 * Add a widget as drop target.
2341 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
2344 Ecore_X_Window xwin;
2348 if (!obj) return EINA_FALSE;
2349 if (!_elm_cnp_init_count) _elm_cnp_init();
2351 /* Is this the first? */
2352 first = (!drops) ? 1 : 0;
2354 EINA_LIST_FOREACH(drops, item, drop)
2356 if (drop->obj == obj)
2358 /* Update: Not a new one */
2359 drop->dropcb = dropcb;
2360 drop->cbdata = cbdata;
2361 drop->types = format;
2366 /* Create new drop */
2367 drop = calloc(1, sizeof(Dropable));
2368 if (!drop) return EINA_FALSE;
2369 /* FIXME: Check for eina's deranged error method */
2370 drops = eina_list_append(drops, drop);
2372 if (!drops/* || or other error */)
2377 drop->dropcb = dropcb;
2378 drop->cbdata = cbdata;
2379 drop->types = format;
2382 evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL,
2383 /* I love C and varargs */
2384 (Evas_Object_Event_Cb)elm_drop_target_del,
2386 /* FIXME: Handle resizes */
2388 /* If not the first: We're done */
2389 if (!first) return EINA_TRUE;
2391 xwin = (Ecore_X_Window)ecore_evas_window_get
2392 (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
2394 ecore_x_dnd_aware_set(xwin, EINA_TRUE);
2396 cnp_debug("Adding drop target calls\n");
2397 handler_enter = ecore_event_handler_add(ECORE_X_EVENT_XDND_ENTER,
2399 handler_pos = ecore_event_handler_add(ECORE_X_EVENT_XDND_POSITION,
2400 _dnd_position, NULL);
2401 handler_drop = ecore_event_handler_add(ECORE_X_EVENT_XDND_DROP,
2408 elm_drop_target_del(Evas_Object *obj)
2410 Dropable *drop,*del;
2412 Ecore_X_Window xwin;
2415 EINA_LIST_FOREACH(drops, item, drop)
2417 if (drop->obj == obj)
2419 drops = eina_list_remove_list(drops, item);
2424 if (!del) return EINA_FALSE;
2426 evas_object_event_callback_del(obj, EVAS_CALLBACK_FREE,
2427 (Evas_Object_Event_Cb)elm_drop_target_del);
2429 /* If still drops there: All fine.. continue */
2430 if (drops) return EINA_TRUE;
2432 cnp_debug("Disabling DND\n");
2433 xwin = (Ecore_X_Window)ecore_evas_window_get
2434 (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
2435 ecore_x_dnd_aware_set(xwin, EINA_FALSE);
2437 ecore_event_handler_del(handler_pos);
2438 ecore_event_handler_del(handler_drop);
2439 ecore_event_handler_del(handler_enter);
2443 pasteimage_free(savedtypes.pi);
2444 savedtypes.pi = NULL;
2452 _drag_mouse_up(void *un __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *data __UNUSED__)
2454 evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_UP, _drag_mouse_up);
2458 dragdonecb(dragdonecb,selections[ELM_SEL_XDND].widget);
2463 evas_object_del(dragwin);
2469 _drag_move(void *data __UNUSED__, Ecore_X_Xdnd_Position *pos)
2471 evas_object_move(dragwin,
2472 pos->position.x - _dragx,
2473 pos->position.y - _dragy);
2478 elm_drag_start(Evas_Object *obj, Elm_Sel_Format format, const char *data, void (*dragdone) (void *data, Evas_Object *), void *donecbdata)
2480 Ecore_X_Window xwin;
2482 Elm_Sel_Type xdnd = ELM_SEL_XDND;
2484 int x, y, x2, y2, x3, y3;
2488 if (!_elm_cnp_init_count) _elm_cnp_init();
2490 xwin = elm_win_xwindow_get(obj);
2492 cnp_debug("starting drag...\n");
2494 ecore_x_dnd_type_set(xwin, "text/uri-list", 1);
2495 sel = selections + ELM_SEL_XDND;
2498 sel->format = format;
2499 sel->selbuf = data ? strdup(data) : NULL;
2500 dragdonecb = dragdone;
2501 dragdonedata = donecbdata;
2503 ecore_x_dnd_callback_pos_update_set(_drag_move, NULL);
2504 ecore_x_dnd_begin(xwin, (unsigned char *)&xdnd, sizeof(Elm_Sel_Type));
2505 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
2506 _drag_mouse_up, NULL);
2508 handler_status = ecore_event_handler_add(ECORE_X_EVENT_XDND_STATUS,
2511 dragwin = elm_win_add(NULL, "Elm Drag Object", ELM_WIN_UTILITY);
2512 elm_win_override_set(dragwin, 1);
2514 /* FIXME: Images only */
2515 icon = elm_icon_add(dragwin);
2516 elm_icon_file_set(icon, data + 7, NULL); /* 7!? "file://" */
2517 elm_win_resize_object_add(dragwin,icon);
2518 evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
2519 evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, EVAS_HINT_FILL);
2521 /* Position subwindow appropriately */
2522 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(obj));
2523 ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
2524 evas_object_geometry_get(obj, &x2, &y2, &w, &h);
2527 evas_object_move(dragwin, x, y);
2528 evas_object_resize(icon, w, h);
2529 evas_object_resize(dragwin, w, h);
2531 evas_object_show(icon);
2532 evas_object_show(dragwin);
2534 evas_pointer_canvas_xy_get(evas_object_evas_get(obj), &x3, &y3);
2542 elm_cnp_tempfile_create(int size)
2545 const char *tmppath;
2548 info = malloc(sizeof(Tmp_Info));
2549 if (!info) return NULL;
2551 tmppath = getenv("TMP");
2552 if (!tmppath) tmppath = P_tmpdir;
2553 if (!tmppath) tmppath = "/tmp";
2554 len = snprintf(NULL, 0, "%s/%sXXXXXX", tmppath, "elmcnpitem-");
2561 info->filename = malloc(len);
2562 if (!info->filename)
2567 snprintf(info->filename,len,"%s/%sXXXXXX", tmppath, "elmcnpitem-");
2569 info->fd = mkstemp(info->filename);
2574 /* And before someone says anything see POSIX 1003.1-2008 page 400 */
2577 pid = (long)getpid();
2578 /* Use pid instead of /proc/self: That way if can be passed around */
2579 len = snprintf(NULL,0,"/proc/%li/fd/%i", pid, info->fd);
2584 snprintf(tmp,len, "/proc/%li/fd/%i", pid, info->fd);
2585 unlink(info->filename);
2586 free(info->filename);
2587 info->filename = tmp;
2592 cnp_debug("filename is %s\n", info->filename);
2595 /* Set map to NULL and return */
2602 if (ftruncate(info->fd, size))
2604 perror("ftruncate");
2610 info->map = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, info->fd, 0);
2611 if (info->map == MAP_FAILED)
2623 tmpinfo_free(Tmp_Info *info)
2625 if (!info) return 0;
2626 free(info->filename);
2632 /* Stubs for windows */
2634 elm_drag_start(Evas_Object *o, Elm_Sel_Format f, const char *d, void (*donecb)(void *, Evas_Object *),void *cbdata)
2640 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
2646 elm_drop_target_del(Evas_Object *o)
2652 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-2f0^-2{2(0W1st0 :*/