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);
830 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);
912 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;
1117 width = data->width;
1119 height = data->height;
1120 eina_strbuf_append_printf(html, " absize=%sx%s", width, height);
1122 eina_strbuf_append_printf(html, " href=%s></item>", data->href);
1128 if (_HTMLtoEFLConvertTable[j].dst[0] == '\0')
1130 if (!strcmp(_HTMLtoEFLConvertTable[j].src, "font"))
1132 if (trail->matchTag->tagData)
1134 PFontTagData data = trail->matchTag->tagData;
1139 eina_strbuf_append_printf(html, "</color>");
1141 eina_strbuf_append_printf(html, "</font>");
1150 switch(trail->tagPosType)
1153 eina_strbuf_append(html, " />");
1156 eina_strbuf_append(html, ">");
1161 }/* for(j = 0; j < tableCnt; j++) end */
1164 eina_strbuf_append(html, trail->str);
1167 char *ret = eina_strbuf_string_steal(html);
1168 eina_strbuf_free(html);
1173 elm_selection_set(Elm_Sel_Type selection, Evas_Object *widget, Elm_Sel_Format format, const char *selbuf)
1175 #ifdef HAVE_ELEMENTARY_X
1178 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
1179 if (!_elm_cnp_init_count) _elm_cnp_init();
1180 if ((!selbuf) && (format != ELM_SEL_FORMAT_IMAGE))
1181 return elm_selection_clear(selection, widget);
1183 sel = selections + selection;
1186 sel->widget = widget;
1188 sel->set(elm_win_xwindow_get(widget),&selection,sizeof(Elm_Sel_Type));
1189 sel->format = format;
1190 sel->selbuf = selbuf ? strdup(selbuf) : NULL;
1199 elm_selection_clear(Elm_Sel_Type selection, Evas_Object *widget)
1201 #ifdef HAVE_ELEMENTARY_X
1204 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
1205 if (!_elm_cnp_init_count) _elm_cnp_init();
1207 sel = selections + selection;
1209 /* No longer this selection: Consider it gone! */
1210 if ((!sel->active) || (sel->widget != widget)) return EINA_TRUE;
1223 elm_selection_get(Elm_Sel_Type selection, Elm_Sel_Format format,
1224 Evas_Object *widget, Elm_Drop_Cb datacb, void *udata)
1226 #ifdef HAVE_ELEMENTARY_X
1230 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
1231 if (!_elm_cnp_init_count) _elm_cnp_init();
1233 sel = selections + selection;
1234 top = elm_widget_top_get(widget);
1235 if (!top) return EINA_FALSE;
1237 sel->requestformat = format;
1238 sel->requestwidget = widget;
1239 sel->request(elm_win_xwindow_get(top), ECORE_X_SELECTION_TARGET_TARGETS);
1240 sel->datacb = datacb;
1249 #ifdef HAVE_ELEMENTARY_X
1256 if (_elm_cnp_init_count++) return EINA_TRUE;
1257 for (i = 0; i < CNP_N_ATOMS; i++)
1259 atoms[i].atom = ecore_x_atom_get(atoms[i].name);
1260 ecore_x_selection_converter_atom_add(atoms[i].atom,
1261 atoms[i].converter);
1263 clipboard_atom = ecore_x_atom_get("CLIPBOARD");
1265 ecore_event_handler_add(ECORE_X_EVENT_SELECTION_CLEAR, selection_clear, NULL);
1266 ecore_event_handler_add(ECORE_X_EVENT_SELECTION_NOTIFY, selection_notify, NULL);
1268 text_uri = eina_stringshare_add("text/uri-list");
1273 selection_clear(void *udata __UNUSED__, int type __UNUSED__, void *event)
1275 Ecore_X_Event_Selection_Clear *ev = event;
1279 for (i = 0; i < ELM_SEL_MAX; i++)
1281 if (selections[i].ecore_sel == ev->selection) break;
1283 cnp_debug("selection %d clear\n", i);
1284 /* Not me... Don't care */
1285 if (i == ELM_SEL_MAX) return ECORE_CALLBACK_PASS_ON;
1287 sel = selections + i;
1292 return ECORE_CALLBACK_PASS_ON;
1297 * Response to a selection notify:
1298 * - So we have asked for the selection list.
1299 * - If it's the targets list, parse it, and fire of what we want,
1300 * else it's the data we want.
1303 selection_notify(void *udata __UNUSED__, int type __UNUSED__, void *event)
1305 Ecore_X_Event_Selection_Notify *ev = event;
1309 cnp_debug("selection notify callback: %d\n",ev->selection);
1310 switch (ev->selection)
1312 case ECORE_X_SELECTION_CLIPBOARD:
1313 sel = selections + ELM_SEL_CLIPBOARD;
1315 case ECORE_X_SELECTION_PRIMARY:
1316 sel = selections + ELM_SEL_PRIMARY;
1318 case ECORE_X_SELECTION_SECONDARY:
1319 sel = selections + ELM_SEL_SECONDARY;
1321 case ECORE_X_SELECTION_XDND:
1322 sel = selections + ELM_SEL_XDND;
1325 return ECORE_CALLBACK_PASS_ON;
1327 cnp_debug("Target is %s\n", ev->target);
1329 for (i = 0; i < CNP_N_ATOMS; i++)
1331 if (!strcmp(ev->target, atoms[i].name))
1333 if (atoms[i].notify)
1335 cnp_debug("Found something: %s\n", atoms[i].name);
1336 atoms[i].notify(sel, ev);
1340 cnp_debug("Ignored: No handler!\n");
1345 return ECORE_CALLBACK_PASS_ON;
1351 targets_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize)
1357 if (!data_ret) return EINA_FALSE;
1359 sel = selections + *((int *)data);
1361 for (i = 0, count = 0; i < CNP_N_ATOMS ; i++)
1363 if (sel->format & atoms[i].formats) count++;
1366 aret = malloc(sizeof(Ecore_X_Atom) * count);
1367 for (i = 0, count = 0; i < CNP_N_ATOMS; i++)
1369 if (sel->format & atoms[i].formats) aret[count ++] = atoms[i].atom;
1373 if (typesize) *typesize = 32 /* urk */;
1374 if (ttype) *ttype = ECORE_X_ATOM_ATOM;
1375 if (size_ret) *size_ret = count;
1381 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__)
1383 cnp_debug("Image converter called\n");
1388 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__)
1392 cnp_debug("Vcard send called\n");
1394 sel = selections + *((int *)data);
1396 if (data_ret) *data_ret = strdup(sel->selbuf);
1397 if (size_ret) *size_ret = strlen(sel->selbuf);
1402 * Callback to handle a targets response on a selection request:
1403 * So pick the format we'd like; and then request it.
1406 notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1408 Ecore_X_Selection_Data_Targets *targets;
1409 Ecore_X_Atom *atomlist;
1412 targets = notify->data;
1413 atomlist = (Ecore_X_Atom *)(targets->data.data);
1415 for (j = 1; j < CNP_N_ATOMS; j++)
1417 cnp_debug("\t%s %d\n", atoms[j].name, atoms[j].atom);
1418 if (!(atoms[j].formats & sel->requestformat)) continue;
1419 for (i = 0; i < targets->data.length; i++)
1421 if ((atoms[j].atom == atomlist[i]) && (atoms[j].notify))
1423 cnp_debug("Atom %s matches\n",atoms[j].name);
1429 cnp_debug("Couldn't find anything that matches\n");
1430 return ECORE_CALLBACK_PASS_ON;
1433 cnp_debug("Sending request for %s\n",atoms[j].name);
1434 sel->request(elm_win_xwindow_get(sel->requestwidget), atoms[j].name);
1436 return ECORE_CALLBACK_PASS_ON;
1440 response_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1442 Ecore_X_Selection_Data_Targets *targets;
1443 Ecore_X_Atom *atomlist;
1447 targets = notify->data;
1448 atomlist = (Ecore_X_Atom *)(targets->data.data);
1450 /* Start from 1: Skip targets */
1451 for (j = 1 ; j < CNP_N_ATOMS ; j ++)
1453 if (!(atoms[j].formats & sel->requestformat)) continue;
1454 for (i = 0 ; i < targets->data.length ; i ++)
1456 if ((atoms[j].atom == atomlist[i]) && (atoms[j].response))
1458 /* Found a match: Use it */
1464 if (j == CNP_N_ATOMS)
1466 cnp_debug("No matching type found\n");
1470 top = elm_widget_top_get(sel->requestwidget);
1473 sel->request(elm_win_xwindow_get(top), atoms[j].name);
1479 notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1481 Ecore_X_Selection_Data *data;
1484 data = notify->data;
1488 Elm_Selection_Data ddata;
1490 str = mark_up((char *)data->data, data->length, NULL);
1491 ddata.x = ddata.y = 0;
1492 ddata.format = ELM_SEL_FORMAT_TEXT;
1494 ddata.len = data->length;
1495 sel->datacb(sel->udata, sel->widget, &ddata);
1500 cnp_debug("Notify handler text %d %d %p\n", data->format,data->length, data->data);
1501 str = mark_up((char *)data->data, data->length, NULL);
1502 cnp_debug("String is %s (from %s)\n", str, data->data);
1503 elm_entry_entry_insert(sel->requestwidget, str);
1510 * So someone is pasting an image into my entry or widget...
1513 notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1515 Ecore_X_Selection_Data *data;
1516 Ecore_X_Selection_Data_Files *files;
1520 data = notify->data;
1521 cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
1522 if (data->content == ECORE_X_SELECTION_CONTENT_FILES)
1524 cnp_debug("got a files list\n");
1525 files = notify->data;
1526 if (files->num_files > 1)
1528 /* Don't handle many items */
1529 cnp_debug("more then one file: Bailing\n");
1532 p = files->files[0];
1536 p = (char *)data->data;
1541 cnp_debug("Couldn't find a file\n");
1544 cnp_debug("Got %s\n",p);
1547 Elm_Selection_Data ddata;
1549 ddata.x = ddata.y = 0;
1550 ddata.format = ELM_SEL_FORMAT_MARKUP;
1552 ddata.len = data->length;
1553 sel->datacb(sel->udata, sel->widget, &ddata);
1556 if (strncmp(p, "file://", 7))
1558 /* Try and continue if it looks sane */
1559 if (*p != '/') return 0;
1563 p += strlen("file://");
1566 if (savedtypes.pi) pasteimage_free(savedtypes.pi);
1567 pi = pasteimage_alloc(p, strlen(p));
1568 if (savedtypes.textreq)
1570 savedtypes.textreq = 0;
1575 pasteimage_append(pi, sel->requestwidget);
1576 savedtypes.pi = NULL;
1582 * Just receieved an vcard, either through cut and paste, or dnd.
1585 vcard_receive(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1589 Ecore_X_Selection_Data *data;
1591 data = notify->data;
1592 cnp_debug("vcard receive\n");
1594 if (sel == (selections + ELM_SEL_XDND))
1596 Elm_Selection_Data ddata;
1598 cnp_debug("drag & drop\n");
1599 /* FIXME: this needs to be generic: Used for all receives */
1600 EINA_LIST_FOREACH(drops, l, dropable)
1602 if (dropable->obj == sel->requestwidget) break;
1606 cnp_debug("Unable to find drop object");
1607 ecore_x_dnd_send_finished();
1610 dropable = eina_list_data_get(l);
1611 ddata.x = savedtypes.x;
1612 ddata.y = savedtypes.y;
1613 ddata.format = ELM_SEL_FORMAT_VCARD;
1614 ddata.data = data->data;
1615 ddata.len = data->length;
1616 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1617 ecore_x_dnd_send_finished();
1619 else if (sel->datacb)
1621 Elm_Selection_Data ddata;
1622 ddata.x = ddata.y = 0;
1623 ddata.format = ELM_SEL_FORMAT_IMAGE;
1624 ddata.data = data->data;
1625 ddata.len = data->length;
1626 sel->datacb(sel->udata, sel->widget, &ddata);
1630 cnp_debug("Paste request\n");
1639 notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1641 Ecore_X_Selection_Data *data;
1645 cnp_debug("got a png (or a jpeg)!\n");
1646 data = notify->data;
1648 cnp_debug("Size if %d\n", data->length);
1652 Elm_Selection_Data ddata;
1654 ddata.x = ddata.y = 0;
1655 ddata.format = ELM_SEL_FORMAT_IMAGE;
1656 ddata.data = data->data;
1657 ddata.len = data->length;
1658 sel->datacb(sel->udata, sel->widget, &ddata);
1662 /* generate tmp name */
1663 tmp = elm_cnp_tempfile_create(data->length);
1664 memcpy(tmp->map, data->data, data->length);
1665 munmap(tmp->map,data->length);
1667 /* FIXME: Add to paste image data to clean up */
1668 pi = pasteimage_alloc(tmp->filename, strlen(tmp->filename));
1669 pasteimage_append(pi, sel->requestwidget);
1676 notify_handler_edje(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1678 Ecore_X_Selection_Data *data;
1680 data = notify->data;
1682 char *stripstr = NULL;
1683 stripstr = malloc(sizeof(char) * (data->length + 1));
1684 strncpy(stripstr, (char *)data->data, data->length);
1685 stripstr[data->length] = '\0';
1689 Elm_Selection_Data ddata;
1690 ddata.x = ddata.y = 0;
1691 ddata.format = ELM_SEL_FORMAT_MARKUP;
1692 ddata.data = stripstr;
1693 ddata.len = data->length;
1694 sel->datacb(sel->udata, sel->widget, &ddata);
1697 elm_entry_entry_insert(sel->requestwidget, stripstr);
1699 cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
1704 * Warning: Generic text/html can';t handle it sanely.
1705 * Firefox sends ucs2 (i think).
1706 * chrome sends utf8... blerg
1709 notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1711 Ecore_X_Selection_Data *data;
1713 cnp_debug("Got some HTML: Checking encoding is useful\n");
1714 data = notify->data;
1716 char *stripstr = NULL;
1717 stripstr = malloc(sizeof(char) * (data->length + 1));
1718 strncpy(stripstr, (char *)data->data, data->length);
1719 stripstr[data->length] = '\0';
1723 Elm_Selection_Data ddata;
1724 ddata.x = ddata.y = 0;
1725 ddata.format = ELM_SEL_FORMAT_HTML;
1726 ddata.data = stripstr;
1727 ddata.len = data->length;
1728 sel->datacb(sel->udata, sel->widget, &ddata);
1731 elm_entry_entry_insert(sel->requestwidget, stripstr);
1733 cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
1740 text_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1744 cnp_debug("text converter\n");
1745 sel = selections + *((int *)data);
1746 if (!sel->active) return EINA_TRUE;
1748 if ((sel->format & ELM_SEL_FORMAT_MARKUP) ||
1749 (sel->format & ELM_SEL_FORMAT_HTML))
1751 *data_ret = remove_tags(sel->selbuf, size_ret);
1753 else if (sel->format & ELM_SEL_FORMAT_TEXT)
1755 *data_ret = strdup(sel->selbuf);
1756 *size_ret = strlen(sel->selbuf);
1758 else if (sel->format & ELM_SEL_FORMAT_IMAGE)
1760 cnp_debug("Image %s\n", evas_object_type_get(sel->widget));
1761 cnp_debug("Elm type: %s\n", elm_object_widget_type_get(sel->widget));
1762 evas_object_image_file_get(elm_photocam_internal_image_get(sel->widget), (const char **)data_ret, NULL);
1763 if (!*data_ret) *data_ret = strdup("No file");
1764 else *data_ret = strdup(*data_ret);
1765 *size_ret = strlen(*data_ret);
1771 edje_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1774 sel = selections + *((int *)data);
1775 /* if (data_ret) *data_ret = strdup(sel->selbuf);
1776 if (size_ret) *size_ret = strlen(sel->selbuf);*/
1779 if (data_ret && (sel->format & ELM_SEL_FORMAT_HTML))
1781 Eina_List *nodeList = NULL;
1785 nodeData = _get_start_node(sel->selbuf);
1789 nodeList = eina_list_append(nodeList, nodeData);
1790 nodeData = _get_next_node(nodeData);
1793 _link_match_tags(nodeList);
1795 _set_HTML_tag_data(nodeList);
1798 _dumpNode(nodeList);
1800 edje = _convert_to_edje(nodeList);
1802 cnp_debug("convert edje: %s\n", edje);
1804 EINA_LIST_FOREACH(nodeList, trail, nodeData)
1805 _delete_node(nodeData);
1806 eina_list_free(nodeList);
1814 *data_ret = strdup(sel->selbuf);
1819 *size_ret = strlen(edje);
1821 *size_ret = strlen(sel->selbuf);
1828 html_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1832 sel = selections + *(int *)data;
1835 if (data_ret && (sel->format & ELM_SEL_FORMAT_MARKUP))
1837 Eina_List *nodeList = NULL;
1841 nodeData = _get_start_node(sel->selbuf);
1845 nodeList = eina_list_append(nodeList, nodeData);
1846 nodeData = _get_next_node(nodeData);
1849 _link_match_tags(nodeList);
1851 _set_EFL_tag_data(nodeList);
1854 _dumpNode(nodeList);
1856 html = _convert_to_html(nodeList);
1858 cnp_debug("convert html: %s\n", html);
1860 EINA_LIST_FOREACH(nodeList, trail, nodeData)
1861 _delete_node(nodeData);
1862 eina_list_free(nodeList);
1869 *data_ret = strdup(sel->selbuf);
1875 *size_ret = strlen(html);
1877 *size_ret = strlen(sel->selbuf);
1884 uri_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1887 sel = selections + *((int *)data);
1888 cnp_debug("Uri converter\n");
1889 if (data_ret) *data_ret = strdup(sel->selbuf);
1890 if (size_ret) *size_ret = strlen(sel->selbuf);
1895 * Image paste provide
1898 /* FIXME: Should add provider for each pasted item: Use data to store it
1900 static Evas_Object *
1901 image_provider(void *images __UNUSED__, Evas_Object *entry, const char *item)
1906 cnp_debug("image provider for %s called\n", item);
1907 EINA_LIST_FOREACH(pastedimages, l, pi)
1909 cnp_debug("is it %s?\n",pi->tag);
1910 if (!strcmp(pi->tag, item))
1914 o = evas_object_image_filled_add(evas_object_evas_get(entry));
1915 /* FIXME: Handle eets */
1916 cnp_debug("file is %s (object is %p)\n", pi->file, o);
1917 evas_object_image_file_set(o, pi->file, NULL);
1918 evas_object_show(o);
1926 static Paste_Image *
1927 pasteimage_alloc(const char *file, int pathlen)
1931 char *buf, *filebuf;
1932 int prefixlen = strlen("file://");
1934 pi = calloc(1, sizeof(Paste_Image));
1935 if (!pi) return NULL;
1937 len = snprintf(NULL, 0, "pasteimage-%p", pi);
1945 snprintf(buf, len, "pasteimage-%p", pi);
1950 if (strstr(file,"file://")) file += prefixlen;
1951 filebuf = alloca(pathlen + 1);
1952 strncpy(filebuf, file, pathlen);
1953 filebuf[pathlen] = 0;
1954 pi->file = strdup(filebuf);
1961 pasteimage_free(Paste_Image *pi)
1964 if (pi->file) free((void*)pi->file);
1965 if (pi->tag) free((void*)pi->tag);
1970 pasteimage_provider_set(Evas_Object *entry)
1975 if (!entry) return EINA_FALSE;
1976 type = elm_widget_type_get(entry);
1977 cnp_debug("type is %s\n", type);
1978 if ((!type) || (strcmp(type, "entry"))) return EINA_FALSE;
1980 v = evas_object_data_get(entry, PROVIDER_SET);
1983 evas_object_data_set(entry, PROVIDER_SET, pasteimage_provider_set);
1984 elm_entry_item_provider_append(entry, image_provider, NULL);
1985 evas_object_event_callback_add(entry, EVAS_CALLBACK_FREE,
1986 entry_deleted, NULL);
1993 pasteimage_append(Paste_Image *pi, Evas_Object *entry)
1997 static const char *tagstring = "<item absize=240x180 href=file://%s></item>";
1999 if (!pi) return EINA_FALSE;
2000 if (!entry) return EINA_FALSE;
2002 pasteimage_provider_set(entry);
2004 len = strlen(tagstring)+strlen(pi->file);
2006 pastedimages = eina_list_append(pastedimages, pi);
2007 entrytag = alloca(len + 1);
2008 snprintf(entrytag, len + 1, tagstring, pi->file);
2009 elm_entry_entry_insert(entry, entrytag);
2015 entry_deleted(void *images __UNUSED__, Evas *e __UNUSED__, Evas_Object *entry, void *unused __UNUSED__)
2020 EINA_LIST_FOREACH_SAFE(pastedimages, l, next, pi)
2022 if (pi->entry == entry)
2023 pastedimages = eina_list_remove_list(pastedimages, l);
2029 remove_tags(const char *p, int *len)
2033 if (!p) return NULL;
2035 q = malloc(strlen(p) + 1);
2036 if (!q) return NULL;
2041 if ((*p != '<') && (*p != '&')) *q++ = *p++;
2044 if ((p[1] == 'b') && (p[2] == 'r') &&
2045 ((p[3] == ' ') || (p[3] == '/') || (p[3] == '>')))
2047 while ((*p) && (*p != '>')) p++;
2053 for (i = 0 ; i < N_ESCAPES ; i++)
2055 if (!strncmp(p,escapes[i].escape, strlen(escapes[i].escape)))
2057 p += strlen(escapes[i].escape);
2058 *q = escapes[i].value;
2063 if (i == N_ESCAPES) *q ++= '&';
2067 if (len) *len = q - ret;
2073 mark_up(const char *start, int inlen, int *lenp)
2078 const char *endp = NULL;
2080 if (!start) return NULL;
2081 if (inlen >= 0) endp = start + inlen;
2082 /* First pass: Count characters */
2083 for (l = 0, p = start; ((!endp) || (p < endp)) && (*p); p++)
2085 for (i = 0 ; i < N_ESCAPES ; i ++)
2087 if (*p == escapes[i].value)
2089 if (!iscntrl(escapes[i].value)) l++;
2090 l += strlen(escapes[i].escape);
2094 if (i == N_ESCAPES) l++;
2097 q = ret = malloc(l + 1);
2099 /* Second pass: Change characters */
2100 for (p = start; *p; )
2102 for (i = 0; i < N_ESCAPES; i++)
2104 if (*p == escapes[i].value)
2106 if (!iscntrl(escapes[i].value)) *q++ = '&';
2107 strcpy(q, escapes[i].escape);
2108 q += strlen(escapes[i].escape);
2113 if (i == N_ESCAPES) *q++ = *p++;
2117 if (lenp) *lenp = l;
2123 _dnd_enter(void *data __UNUSED__, int etype __UNUSED__, void *ev)
2125 Ecore_X_Event_Xdnd_Enter *enter = ev;
2129 if ((!enter) || (!enter->num_types) || (!enter->types)) return EINA_TRUE;
2131 cnp_debug("Types\n");
2132 savedtypes.ntypes = enter->num_types;
2133 if (savedtypes.types) free(savedtypes.types);
2134 savedtypes.types = malloc(sizeof(char *) * enter->num_types);
2135 if (!savedtypes.types) return EINA_FALSE;
2137 for (i = 0; i < enter->num_types; i++)
2139 savedtypes.types[i] = eina_stringshare_add(enter->types[i]);
2140 cnp_debug("Type is %s %p %p\n", enter->types[i],
2141 savedtypes.types[i],text_uri);
2142 if (savedtypes.types[i] == text_uri)
2144 /* Request it, so we know what it is */
2145 cnp_debug("Sending uri request\n");
2146 savedtypes.textreq = 1;
2147 savedtypes.pi = NULL; /* FIXME: Free? */
2148 ecore_x_selection_xdnd_request(enter->win, text_uri);
2152 /* FIXME: Find an object and make it current */
2157 _dnd_drop(void *data __UNUSED__, int etype __UNUSED__, void *ev)
2159 struct _Ecore_X_Event_Xdnd_Drop *drop;
2163 Ecore_X_Window xwin;
2164 Elm_Selection_Data ddata;
2170 // check we still have something to drop
2171 if (!drops) return EINA_TRUE;
2173 /* Find any widget in our window; then work out geometry rel to our window */
2174 for (l = drops; l; l = l->next)
2177 xwin = (Ecore_X_Window)ecore_evas_window_get
2178 (ecore_evas_ecore_evas_get(evas_object_evas_get
2180 if (xwin == drop->win) break;
2182 /* didn't find a window */
2183 if (!l) return EINA_TRUE;
2185 /* Calculate real (widget relative) position */
2186 // - window position
2187 // - widget position
2188 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(dropable->obj));
2189 ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
2190 savedtypes.x = drop->position.x - x;
2191 savedtypes.y = drop->position.y - y;
2193 cnp_debug("Drop position is %d,%d\n", savedtypes.x, savedtypes.y);
2195 for (; l; l = l->next)
2198 evas_object_geometry_get(dropable->obj, &x, &y, &w, &h);
2199 if ((savedtypes.x >= x) && (savedtypes.y >= y) &&
2200 (savedtypes.x < x + w) && (savedtypes.y < y + h))
2204 if (!l) return EINA_TRUE; /* didn't find one */
2206 evas_object_geometry_get(dropable->obj, &x, &y, NULL, NULL);
2210 /* Find our type from the previous list */
2211 for (i = 0; i < CNP_N_ATOMS; i++)
2213 for (j = 0; j < savedtypes.ntypes; j++)
2215 if (!strcmp(savedtypes.types[j], atoms[i].name)) goto found;
2219 cnp_debug("Didn't find a target\n");
2223 cnp_debug("Found a target we'd like: %s\n", atoms[i].name);
2224 cnp_debug("0x%x\n",xwin);
2226 if (i == CNP_ATOM_text_urilist)
2228 cnp_debug("We found a URI... (%scached) %s\n",
2229 savedtypes.pi ? "" : "not ",
2230 savedtypes.pi->file);
2234 static const char *tagstring = "<item absize=240x180 href="
2235 "file://%s></item>";
2236 ddata.x = savedtypes.x;
2237 ddata.y = savedtypes.y;
2239 /* If it's markup that also supports images */
2240 if ((dropable->types & ELM_SEL_FORMAT_MARKUP) &&
2241 (dropable->types & ELM_SEL_FORMAT_IMAGE))
2244 ddata.format = ELM_SEL_FORMAT_MARKUP;
2245 pasteimage_provider_set(dropable->obj);
2247 pastedimages = eina_list_append(pastedimages, savedtypes.pi);
2248 len = strlen(tagstring) + strlen(savedtypes.pi->file);
2249 entrytag = alloca(len + 1);
2250 snprintf(entrytag, len + 1, tagstring, savedtypes.pi->file);
2251 ddata.data = entrytag;
2252 cnp_debug("Insert %s\n", (char *)ddata.data);
2253 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
2254 ecore_x_dnd_send_finished();
2257 else if (dropable->types & ELM_SEL_FORMAT_IMAGE)
2259 cnp_debug("Doing image insert (%s)\n", savedtypes.pi->file);
2260 ddata.format = ELM_SEL_FORMAT_IMAGE;
2261 ddata.data = (char *)savedtypes.pi->file;
2262 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
2263 ecore_x_dnd_send_finished();
2265 pasteimage_free(savedtypes.pi);
2266 savedtypes.pi = NULL;
2272 cnp_debug("Item doesn't support images... passing\n");
2273 pasteimage_free(savedtypes.pi);
2277 else if (savedtypes.textreq)
2279 /* Already asked: Pretend we asked now, and paste immediately when
2281 savedtypes.textreq = 0;
2282 ecore_x_dnd_send_finished();
2287 cnp_debug("doing a request then\n");
2288 selections[ELM_SEL_XDND].requestwidget = dropable->obj;
2289 selections[ELM_SEL_XDND].requestformat = ELM_SEL_FORMAT_MARKUP;
2290 selections[ELM_SEL_XDND].active = EINA_TRUE;
2292 ecore_x_selection_xdnd_request(xwin, atoms[i].name);
2297 _dnd_position(void *data __UNUSED__, int etype __UNUSED__, void *ev)
2299 struct _Ecore_X_Event_Xdnd_Position *pos;
2300 Ecore_X_Rectangle rect;
2304 /* Need to send a status back */
2305 /* FIXME: Should check I can drop here */
2306 /* FIXME: Should highlight widget */
2307 rect.x = pos->position.x - 5;
2308 rect.y = pos->position.y - 5;
2311 ecore_x_dnd_send_status(EINA_TRUE, EINA_FALSE, rect, pos->action);
2317 * When dragging this is callback response from the destination.
2318 * The important thing we care about: Can we drop; thus update cursor
2322 _dnd_status(void *data __UNUSED__, int etype __UNUSED__, void *ev)
2324 struct _Ecore_X_Event_Xdnd_Status *status = ev;
2326 if (!status) return EINA_TRUE;
2328 /* Only thing we care about: will accept */
2329 if (status->will_accept)
2331 cnp_debug("Will accept\n");
2334 { /* Won't accept */
2335 cnp_debug("Won't accept accept\n");
2341 * Add a widget as drop target.
2344 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
2347 Ecore_X_Window xwin;
2351 if (!obj) return EINA_FALSE;
2352 if (!_elm_cnp_init_count) _elm_cnp_init();
2354 /* Is this the first? */
2355 first = (!drops) ? 1 : 0;
2357 EINA_LIST_FOREACH(drops, item, drop)
2359 if (drop->obj == obj)
2361 /* Update: Not a new one */
2362 drop->dropcb = dropcb;
2363 drop->cbdata = cbdata;
2364 drop->types = format;
2369 /* Create new drop */
2370 drop = calloc(1, sizeof(Dropable));
2371 if (!drop) return EINA_FALSE;
2372 /* FIXME: Check for eina's deranged error method */
2373 drops = eina_list_append(drops, drop);
2375 if (!drops/* || or other error */)
2380 drop->dropcb = dropcb;
2381 drop->cbdata = cbdata;
2382 drop->types = format;
2385 evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL,
2386 /* I love C and varargs */
2387 (Evas_Object_Event_Cb)elm_drop_target_del,
2389 /* FIXME: Handle resizes */
2391 /* If not the first: We're done */
2392 if (!first) return EINA_TRUE;
2394 xwin = (Ecore_X_Window)ecore_evas_window_get
2395 (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
2397 ecore_x_dnd_aware_set(xwin, EINA_TRUE);
2399 cnp_debug("Adding drop target calls\n");
2400 handler_enter = ecore_event_handler_add(ECORE_X_EVENT_XDND_ENTER,
2402 handler_pos = ecore_event_handler_add(ECORE_X_EVENT_XDND_POSITION,
2403 _dnd_position, NULL);
2404 handler_drop = ecore_event_handler_add(ECORE_X_EVENT_XDND_DROP,
2411 elm_drop_target_del(Evas_Object *obj)
2413 Dropable *drop,*del;
2415 Ecore_X_Window xwin;
2418 EINA_LIST_FOREACH(drops, item, drop)
2420 if (drop->obj == obj)
2422 drops = eina_list_remove_list(drops, item);
2427 if (!del) return EINA_FALSE;
2429 evas_object_event_callback_del(obj, EVAS_CALLBACK_FREE,
2430 (Evas_Object_Event_Cb)elm_drop_target_del);
2432 /* If still drops there: All fine.. continue */
2433 if (drops) return EINA_TRUE;
2435 cnp_debug("Disabling DND\n");
2436 xwin = (Ecore_X_Window)ecore_evas_window_get
2437 (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
2438 ecore_x_dnd_aware_set(xwin, EINA_FALSE);
2440 ecore_event_handler_del(handler_pos);
2441 ecore_event_handler_del(handler_drop);
2442 ecore_event_handler_del(handler_enter);
2446 pasteimage_free(savedtypes.pi);
2447 savedtypes.pi = NULL;
2455 _drag_mouse_up(void *un __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *data __UNUSED__)
2457 evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_UP, _drag_mouse_up);
2461 dragdonecb(dragdonecb,selections[ELM_SEL_XDND].widget);
2466 evas_object_del(dragwin);
2472 _drag_move(void *data __UNUSED__, Ecore_X_Xdnd_Position *pos)
2474 evas_object_move(dragwin,
2475 pos->position.x - _dragx,
2476 pos->position.y - _dragy);
2481 elm_drag_start(Evas_Object *obj, Elm_Sel_Format format, const char *data, void (*dragdone) (void *data, Evas_Object *), void *donecbdata)
2483 Ecore_X_Window xwin;
2485 Elm_Sel_Type xdnd = ELM_SEL_XDND;
2487 int x, y, x2, y2, x3, y3;
2491 if (!_elm_cnp_init_count) _elm_cnp_init();
2493 xwin = elm_win_xwindow_get(obj);
2495 cnp_debug("starting drag...\n");
2497 ecore_x_dnd_type_set(xwin, "text/uri-list", 1);
2498 sel = selections + ELM_SEL_XDND;
2501 sel->format = format;
2502 sel->selbuf = data ? strdup(data) : NULL;
2503 dragdonecb = dragdone;
2504 dragdonedata = donecbdata;
2506 ecore_x_dnd_callback_pos_update_set(_drag_move, NULL);
2507 ecore_x_dnd_begin(xwin, (unsigned char *)&xdnd, sizeof(Elm_Sel_Type));
2508 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
2509 _drag_mouse_up, NULL);
2511 handler_status = ecore_event_handler_add(ECORE_X_EVENT_XDND_STATUS,
2514 dragwin = elm_win_add(NULL, "Elm Drag Object", ELM_WIN_UTILITY);
2515 elm_win_override_set(dragwin, 1);
2517 /* FIXME: Images only */
2518 icon = elm_icon_add(dragwin);
2519 elm_icon_file_set(icon, data + 7, NULL); /* 7!? "file://" */
2520 elm_win_resize_object_add(dragwin,icon);
2521 evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
2522 evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, EVAS_HINT_FILL);
2524 /* Position subwindow appropriately */
2525 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(obj));
2526 ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
2527 evas_object_geometry_get(obj, &x2, &y2, &w, &h);
2530 evas_object_move(dragwin, x, y);
2531 evas_object_resize(icon, w, h);
2532 evas_object_resize(dragwin, w, h);
2534 evas_object_show(icon);
2535 evas_object_show(dragwin);
2537 evas_pointer_canvas_xy_get(evas_object_evas_get(obj), &x3, &y3);
2545 elm_cnp_tempfile_create(int size)
2548 const char *tmppath;
2551 info = malloc(sizeof(Tmp_Info));
2552 if (!info) return NULL;
2554 tmppath = getenv("TMP");
2555 if (!tmppath) tmppath = P_tmpdir;
2556 if (!tmppath) tmppath = "/tmp";
2557 len = snprintf(NULL, 0, "%s/%sXXXXXX", tmppath, "elmcnpitem-");
2564 info->filename = malloc(len);
2565 if (!info->filename)
2570 snprintf(info->filename,len,"%s/%sXXXXXX", tmppath, "elmcnpitem-");
2572 info->fd = mkstemp(info->filename);
2577 /* And before someone says anything see POSIX 1003.1-2008 page 400 */
2580 pid = (long)getpid();
2581 /* Use pid instead of /proc/self: That way if can be passed around */
2582 len = snprintf(NULL,0,"/proc/%li/fd/%i", pid, info->fd);
2587 snprintf(tmp,len, "/proc/%li/fd/%i", pid, info->fd);
2588 unlink(info->filename);
2589 free(info->filename);
2590 info->filename = tmp;
2595 cnp_debug("filename is %s\n", info->filename);
2598 /* Set map to NULL and return */
2605 if (ftruncate(info->fd, size))
2607 perror("ftruncate");
2613 info->map = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, info->fd, 0);
2614 if (info->map == MAP_FAILED)
2626 tmpinfo_free(Tmp_Info *info)
2628 if (!info) return 0;
2629 free(info->filename);
2635 /* Stubs for windows */
2637 elm_drag_start(Evas_Object *o, Elm_Sel_Format f, const char *d, void (*donecb)(void *, Evas_Object *),void *cbdata)
2643 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
2649 elm_drop_target_del(Evas_Object *o)
2655 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-2f0^-2{2(0W1st0 :*/