2 # include "elementary_config.h"
4 #include <Elementary.h>
9 #ifdef HAVE_ELEMENTARY_X
11 #define ARRAYINIT(foo) [foo] =
16 # define cnp_debug(x...) fprintf(stderr, __FILE__": " x)
18 # define cnp_debug(x...)
21 #define PROVIDER_SET "__elm_cnp_provider_set"
23 typedef struct _Paste_Image Paste_Image;
24 typedef struct _Cnp_Selection Cnp_Selection;
25 typedef struct _Escape Escape;
26 typedef struct _Tmp_Info Tmp_Info;
27 typedef struct _Cnp_Atom Cnp_Atom;
28 typedef struct _Saved_Type Saved_Type;
29 typedef struct _Dropable Dropable;
31 typedef Eina_Bool (*Converter_Fn_Cb) (char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
32 typedef int (*Response_Handler_Cb) (Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *);
33 typedef int (*Notify_Handler_Cb) (Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *);
39 CNP_ATOM_text_urilist,
40 CNP_ATOM_text_x_vcard,
51 CNP_ATOM_text_html_utf8,
56 CNP_ATOM_text_plain_utf8,
75 Evas_Object *requestwidget;
77 Elm_Sel_Format requestformat;
79 Eina_Bool (*set) (Ecore_X_Window, const void *data, int size);
80 Eina_Bool (*clear) (void);
81 void (*request) (Ecore_X_Window, const char *target);
83 Elm_Sel_Format format;
84 Ecore_X_Selection ecore_sel;
106 Elm_Sel_Format formats;
107 /* Called by ecore to do conversion */
108 Converter_Fn_Cb converter;
109 Response_Handler_Cb response;
110 Notify_Handler_Cb notify;
121 Eina_Bool textreq: 1;
127 /* FIXME: Cache window */
128 Elm_Sel_Format types;
133 static Tmp_Info *elm_cnp_tempfile_create(int size);
134 static int tmpinfo_free(Tmp_Info *tmp);
136 static Eina_Bool _elm_cnp_init(void);
137 static Eina_Bool selection_clear(void *udata __UNUSED__, int type, void *event);
138 static Eina_Bool selection_notify(void *udata __UNUSED__, int type, void *event);
139 static char *remove_tags(const char *p, int *len);
140 static char *mark_up(const char *start, int inlen, int *lenp);
142 static Evas_Object *image_provider(void *images, Evas_Object *entry, const char *item);
143 static void entry_deleted(void *images, Evas *e, Evas_Object *entry, void *unused);
145 static Eina_Bool targets_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
146 static Eina_Bool text_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
147 static Eina_Bool html_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
148 static Eina_Bool edje_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
149 static Eina_Bool uri_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
150 static Eina_Bool image_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
151 static Eina_Bool vcard_send(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
153 static int response_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *);
155 static int notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
156 static int notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
157 static int notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
158 static int notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
159 static int notify_handler_edje(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
160 static int notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
161 static int vcard_receive(Cnp_Selection *sed, Ecore_X_Event_Selection_Notify *notify);
163 static Paste_Image *pasteimage_alloc(const char *file, int pathlen);
164 static Eina_Bool pasteimage_append(Paste_Image *pi, Evas_Object *entry);
165 static void pasteimage_free(Paste_Image *pi);
166 static void entry_insert_filter(Evas_Object* entry, char* str);
168 /* Optimisation: Turn this into a 256 byte table:
169 * then can lookup in one index, not N checks */
170 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_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;
410 /* For convert EFL to HTML */
412 #define TAGPOS_START 0x00000001
413 #define TAGPOS_END 0x00000002
414 #define TAGPOS_ALONE 0x00000003
416 /* TEXTBLOCK tag using stack but close tag word has no mean maybe bug...
417 * TEXTBLOCK <b>bold<font>font</b>bold</font>
418 * HTML <b>bold<font>font bold</b>font</font> */
420 typedef struct _TagTable {
426 TagTable _EFLtoHTMLConvertTable[] = {
428 {"underline", "del", 0},
429 {"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 eina_str_tolower(&tag);
522 eina_str_tolower(&tag_str);
523 newNode->tag_str = tag_str;
525 newNode->pos_in_ori_str = pos_in_ori_str;
530 _get_start_node(char *str)
532 char *startStr = NULL;
533 if (!str || str[0] == '\0')
538 char *tagStart = strchr(str, '<');
540 startStr = strdup(str);
543 int strLength = tagStart - str;
544 startStr = malloc(sizeof(char) * (strLength + 1));
545 strncpy(startStr, str, strLength);
546 startStr[strLength] = '\0';
550 return _new_tag_node(NULL, NULL, startStr, str);
554 _get_next_node(PTagNode prev)
556 PTagNode retTag = NULL;
559 char *tagNameEnd = NULL;
562 if (prev->tag == NULL)
563 tagStart = strchr(prev->pos_in_ori_str, '<');
565 tagStart = strchr(prev->pos_in_ori_str + 1, '<');
570 tagEnd = strchr(tagStart, '>');
571 nextTagStart = strchr(tagStart + 1, '<');
573 if (!tagEnd || (nextTagStart && (nextTagStart < tagEnd)))
574 return _get_start_node(tagStart + 1);
577 char *spArray[spCnt];
578 spArray[0] = strchr(tagStart, '=');
579 spArray[1] = strchr(tagStart, '_');
580 spArray[2] = strchr(tagStart, ' ');
581 spArray[3] = strchr(tagStart, '\t');
582 spArray[4] = strchr(tagStart, '\n');
586 for (i = 0; i < spCnt; i++)
588 if (spArray[i] && spArray[i] < tagNameEnd)
589 tagNameEnd = spArray[i];
592 int tagLength = tagNameEnd - tagStart - 1;
593 char *tagName = NULL;
594 if (!strncmp(&tagStart[1], "color", tagLength))
595 tagName = strndup("font", 4);
596 else if (!strncmp(&tagStart[1], "/color", tagLength))
597 tagName = strndup("/font", 5);
598 else if (!strncmp(&tagStart[1], "/item", tagLength))
599 tagName = strdup("");
601 tagName = strndup(&tagStart[1], tagLength);
603 int tagStrLength = 0;
607 tagStrLength = tagEnd - tagStart + 1;
608 tagStr = strndup(tagStart, tagStrLength);
611 unsigned int strLength = nextTagStart ? (unsigned int)(nextTagStart - tagEnd - 1) : strlen(&tagEnd[1]);
612 char *str = strndup(&tagEnd[1], strLength);
614 retTag = _new_tag_node(tagName, tagStr, str, tagStart);
620 _delete_node(PTagNode node)
624 SAFEFREE(node->tag_str);
631 if (!strcmp("font", node->tag))
633 PFontTagData data = node->tagData;
634 SAFEFREE(data->name);
635 SAFEFREE(data->color);
636 SAFEFREE(data->size);
637 SAFEFREE(data->bg_color);
639 if (!strcmp("item", node->tag))
641 PItemTagData data = node->tagData;
642 SAFEFREE(data->href);
643 SAFEFREE(data->width);
644 SAFEFREE(data->height);
648 SAFEFREE(node->tagData);
656 _link_match_tags(Eina_List *nodes)
658 Eina_List *stack = NULL;
660 PTagNode trail, popData;
663 EINA_LIST_FOREACH(nodes, l, trail)
665 if (!trail->tag || trail->tag[0] == '\0')
667 if (!strcmp("br", trail->tag))
669 trail->tagPosType = TAGPOS_ALONE;
672 else if (!strcmp("item", trail->tag) || !strcmp("img", trail->tag))
674 trail->tagPosType = TAGPOS_ALONE;
678 if (trail->tag[0] != '/') // PUSH
680 stack = eina_list_append(stack, trail);
681 /* eina_array_push(stack, trail);
682 cnp_debug("stack: %d, tag %s\n", eina_array_count_get(stack), trail->tag);*/
683 cnp_debug("stack: %d, tag %s\n", eina_list_count(stack), trail->tag);
687 if (!eina_list_count(stack))
689 cnp_debug("tag not matched %s\n", trail->tag);
693 EINA_LIST_REVERSE_FOREACH(stack, r, popData)
695 if (popData->tag && !strcmp(popData->tag, &trail->tag[1]))
697 popData->tagPosType = TAGPOS_START;
698 trail->tagPosType = TAGPOS_END;
699 popData->matchTag = trail;
700 trail->matchTag = popData;
701 stack = eina_list_remove_list(stack, r);
705 /* popData = eina_array_pop(stack);
707 popData->tagPosType = TAGPOS_START;
708 trail->tagPosType = TAGPOS_END;
709 popData->matchTag = trail;
710 trail->matchTag = popData;
711 cnp_debug("pop stack: %d, tag %s\n", eina_array_count_get(stack), trail->tag);
716 /* if (eina_array_count_get(stack))
717 cnp_debug("stack state: %d, tag %s\n", eina_array_count_get(stack), trail->tag);*/
719 /* Make Dummy close tag */
720 /* while ((popData = eina_array_pop(stack))) */
722 EINA_LIST_REVERSE_FOREACH(stack, r, popData)
725 int tagLength = strlen(popData->tag);
726 char *tagName = malloc(sizeof(char) * (tagLength + 2));
730 strcat(tagName, popData->tag);
732 newData = _new_tag_node(tagName, NULL, NULL, NULL);
733 popData->tagPosType = TAGPOS_START;
734 newData->tagPosType = TAGPOS_END;
735 popData->matchTag = newData;
736 newData->matchTag = popData;
737 nodes = eina_list_append(nodes, newData);
738 /* cnp_debug("stack: %d, tag %s\n", eina_array_count_get(stack), popData->tag);*/
740 /* cnp_debug("stack_top: %d\n", eina_array_count_get(stack));
741 eina_array_free(stack);*/
742 eina_list_free(stack);
746 _get_tag_value(const char *tag_str, const char *tag_name)
748 if (!tag_name || !tag_str)
752 if ((tag = strstr(tag_str, tag_name)))
754 if (tag[strlen(tag_name)] == '_')
756 char *value = strchr(tag, '=');
762 } while (!isalnum(*value) && *value != '#');
765 char *spArray[spCnt];
766 spArray[0] = strchr(value, ' ');
767 spArray[1] = strchr(value, '>');
768 spArray[2] = strchr(value, '\"');
769 spArray[3] = strchr(value, '\'');
770 spArray[4] = strchr(value, '\t');
771 spArray[5] = strchr(value, '\n');
772 char *valueEnd = strchr(value, '\0');
775 for (i = 0; i < spCnt; i++)
777 if (spArray[i] && spArray[i] < valueEnd)
778 valueEnd = spArray[i];
781 int valueLength = valueEnd - value;
782 return strndup(value, valueLength);
789 _set_EFL_font_data(PFontTagData data, const char *tag_str)
794 data = calloc(1, sizeof(FontTagData));
795 value = _get_tag_value(tag_str, "font_size");
796 freeAndAssign(data->size, value);
797 value = _get_tag_value(tag_str, "color");
798 freeAndAssign(data->color, value);
799 value = _get_tag_value(tag_str, "bgcolor");
800 freeAndAssign(data->bg_color, value);
801 value = _get_tag_value(tag_str, "font");
802 freeAndAssign(data->name, value);
808 _set_EFL_item_data(PItemTagData data, const char *tag_str)
813 data = calloc(1, sizeof(ItemTagData));
814 value = _get_tag_value(tag_str, "href");
817 char *path = strstr(value, "file://");
820 char *modify = malloc(sizeof(char) * (strlen(value) + 1));
821 strncpy(modify, "file://", 7);
824 while (path[1] && path[0] && path[1] == '/' && path[0] == '/')
828 strcat(modify, path);
830 cnp_debug("image href ---%s---\n", data->href);
834 freeAndAssign(data->href, value);
837 value = _get_tag_value(tag_str, "absize");
840 char *xpos = strchr(value, 'x');
843 int absizeLen = strlen(value);
844 freeAndAssign(data->width, strndup(value, xpos - value));
845 freeAndAssign(data->height, strndup(xpos + 1, absizeLen - (xpos - value) - 1));
846 cnp_debug("image width: -%s-, height: -%s-\n", data->width, data->height);
854 _set_EFL_tag_data(Eina_List* nodes)
859 EINA_LIST_FOREACH(nodes, l, trail)
863 if (!strcmp("font", trail->tag))
864 trail->tagData = _set_EFL_font_data(trail->tagData, trail->tag_str);
865 else if (!strcmp("item", trail->tag))
866 trail->tagData = _set_EFL_item_data(trail->tagData, trail->tag_str);
871 _set_HTML_font_data(PFontTagData data, const char *tag_str)
876 data = calloc(1, sizeof(FontTagData));
877 value = _get_tag_value(tag_str, "size");
878 freeAndAssign(data->size, value);
879 value = _get_tag_value(tag_str, "color");
880 freeAndAssign(data->color, value);
881 value = _get_tag_value(tag_str, "bgcolor");
882 freeAndAssign(data->bg_color, value);
883 value = _get_tag_value(tag_str, "face");
884 freeAndAssign(data->name, value);
890 _set_HTML_img_data(PItemTagData data, const char *tag_str)
895 data = calloc(1, sizeof(ItemTagData));
896 value = _get_tag_value(tag_str, "src");
899 char *path = strstr(value, "file://");
902 char *modify = malloc(sizeof(char) * (strlen(value) + 1));
903 strncpy(modify, "file://", 7);
906 while (path[1] && path[0] && path[1] == '/' && path[0] == '/')
910 strcat(modify, path);
912 cnp_debug("image src ---%s---\n", data->href);
916 freeAndAssign(data->href, value);
919 value = _get_tag_value(tag_str, "width");
920 freeAndAssign(data->width, value);
921 value = _get_tag_value(tag_str, "height");
922 freeAndAssign(data->height, value);
927 _set_HTML_tag_data(Eina_List* nodes)
932 EINA_LIST_FOREACH(nodes, l, trail)
936 if (!strcmp("font", trail->tag))
937 trail->tagData = _set_HTML_font_data(trail->tagData, trail->tag_str);
938 else if (!strcmp("img", trail->tag))
939 trail->tagData = _set_HTML_img_data(trail->tagData, trail->tag_str);
945 _dumpNode(Eina_List* nodes)
950 EINA_LIST_FOREACH(nodes, l, trail)
952 cnp_debug("tag: %s, tag_str: %s, str: %s, tagPosType: %d\n",
953 trail->tag, trail->tag_str, trail->str, trail->tagPosType);
954 cnp_debug("matchTag: %x ", (unsigned int)trail->matchTag);
956 cnp_debug("matchTag->tag_str: %s", trail->matchTag->tag_str);
959 if (!strcmp(trail->tag, "font"))
961 PFontTagData data = trail->tagData;
962 cnp_debug(" tagData->name: %s, tagData->color: %s, tagData->size: %s, tagData->bg_color: %s",
963 data->name, data->color, data->size, data->bg_color);
965 else if (!strcmp(trail->tag, "item") || !strcmp(trail->tag, "img"))
967 PItemTagData data = trail->tagData;
968 cnp_debug(" tagData->href: %s, tagData->width: %s, tagData->height: %s",
969 data->href, data->width, data->height);
972 cnp_debug("\nERROR!!!! not need tagData");
980 _convert_to_html(Eina_List* nodes)
985 Eina_Strbuf *html = eina_strbuf_new();
987 int tableCnt = sizeof(_EFLtoHTMLConvertTable) / sizeof(TagTable);
989 EINA_LIST_FOREACH(nodes, l, trail)
993 char *tagName = trail->tagPosType == TAGPOS_END ?
994 trail->matchTag->tag : trail->tag;
996 for(j = 0; j < tableCnt; j++)
998 if (!strcmp(_EFLtoHTMLConvertTable[j].src, tagName))
1000 switch(trail->tagPosType)
1003 eina_strbuf_append(html, "</");
1006 eina_strbuf_append(html, "<");
1010 eina_strbuf_append(html, _EFLtoHTMLConvertTable[j].dst);
1011 if (trail->tagPosType != TAGPOS_END)
1013 if (!strcmp(_EFLtoHTMLConvertTable[j].src, "font"))
1015 PFontTagData data = trail->tagData;
1020 eina_strbuf_append_printf(html, " color=\"%s\"", data->color);
1022 eina_strbuf_append_printf(html, " size=\"%s\"", data->size);
1027 else if (!strcmp(_EFLtoHTMLConvertTable[j].src, "item"))
1029 PItemTagData data = trail->tagData;
1031 eina_strbuf_append_printf(html, " src=\"%s\"", data->href);
1033 eina_strbuf_append_printf(html, " width=\"%s\"", data->width);
1035 eina_strbuf_append_printf(html, " height=\"%s\"", data->height);
1038 switch(trail->tagPosType)
1040 /* closed tag does not need in HTML
1042 eina_strbuf_append(html, " />");
1045 eina_strbuf_append(html, ">");
1053 eina_strbuf_append(html, trail->str);
1056 char *ret = eina_strbuf_string_steal(html);
1057 eina_strbuf_free(html);
1061 #define IMAGE_DEFAULT_WIDTH "240"
1062 #define IMAGE_DEFAULT_HEIGHT "180"
1066 _convert_to_edje(Eina_List* nodes)
1071 Eina_Strbuf *html = eina_strbuf_new();
1073 int tableCnt = sizeof(_HTMLtoEFLConvertTable) / sizeof(TagTable);
1075 EINA_LIST_FOREACH(nodes, l, trail)
1079 char *tagName = trail->tagPosType == TAGPOS_END ?
1080 trail->matchTag->tag : trail->tag;
1082 for(j = 0; j < tableCnt; j++)
1084 if (!strcmp(_HTMLtoEFLConvertTable[j].src, tagName))
1086 if (_HTMLtoEFLConvertTable[j].dst[0] != '\0')
1088 switch(trail->tagPosType)
1091 eina_strbuf_append(html, "</");
1094 eina_strbuf_append(html, "<");
1098 eina_strbuf_append(html, _HTMLtoEFLConvertTable[j].dst);
1100 if (trail->tagPosType != TAGPOS_END)
1102 if (!strcmp(_HTMLtoEFLConvertTable[j].src, "font"))
1104 PFontTagData data = trail->tagData;
1109 eina_strbuf_append_printf(html, "<color=%s>", data->color);
1111 eina_strbuf_append_printf(html, "<font_size=%s>", data->size);
1117 else if (!strcmp(_HTMLtoEFLConvertTable[j].src, "img"))
1119 PItemTagData data = trail->tagData;
1120 char *width = IMAGE_DEFAULT_WIDTH, *height = IMAGE_DEFAULT_HEIGHT;
1122 width = data->width;
1124 height = data->height;
1125 eina_strbuf_append_printf(html, " absize=%sx%s", width, height);
1127 eina_strbuf_append_printf(html, " href=%s></item>", data->href);
1133 if (_HTMLtoEFLConvertTable[j].dst[0] == '\0')
1135 if (!strcmp(_HTMLtoEFLConvertTable[j].src, "font"))
1137 if (trail->matchTag->tagData)
1139 PFontTagData data = trail->matchTag->tagData;
1144 eina_strbuf_append_printf(html, "</color>");
1146 eina_strbuf_append_printf(html, "</font>");
1155 switch(trail->tagPosType)
1157 /* not support in efl
1159 eina_strbuf_append(html, " />");
1163 eina_strbuf_append(html, ">");
1168 }/* for(j = 0; j < tableCnt; j++) end */
1171 eina_strbuf_append(html, trail->str);
1174 char *ret = eina_strbuf_string_steal(html);
1175 eina_strbuf_free(html);
1181 elm_selection_set(Elm_Sel_Type selection, Evas_Object *widget, Elm_Sel_Format format, const char *selbuf)
1183 #ifdef HAVE_ELEMENTARY_X
1184 Evas_Object *top = elm_widget_top_get(widget);
1185 Ecore_X_Window xwin;
1188 if (top) xwin = elm_win_xwindow_get(top);
1189 else xwin = elm_win_xwindow_get(widget);
1190 if (!xwin) return EINA_FALSE;
1191 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
1192 if (!_elm_cnp_init_count) _elm_cnp_init();
1193 if ((!selbuf) && (format != ELM_SEL_FORMAT_IMAGE))
1194 return elm_selection_clear(selection, widget);
1196 sel = selections + selection;
1199 sel->widget = widget;
1201 sel->set(xwin, &selection, sizeof(Elm_Sel_Type));
1202 sel->format = format;
1203 sel->selbuf = selbuf ? strdup(selbuf) : NULL;
1212 elm_selection_clear(Elm_Sel_Type selection, Evas_Object *widget)
1214 #ifdef HAVE_ELEMENTARY_X
1217 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
1218 if (!_elm_cnp_init_count) _elm_cnp_init();
1220 sel = selections + selection;
1222 /* No longer this selection: Consider it gone! */
1223 if ((!sel->active) || (sel->widget != widget)) return EINA_TRUE;
1236 elm_selection_get(Elm_Sel_Type selection, Elm_Sel_Format format,
1237 Evas_Object *widget, Elm_Drop_Cb datacb, void *udata)
1239 #ifdef HAVE_ELEMENTARY_X
1243 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
1244 if (!_elm_cnp_init_count) _elm_cnp_init();
1246 sel = selections + selection;
1247 top = elm_widget_top_get(widget);
1248 if (!top) return EINA_FALSE;
1250 sel->requestformat = format;
1251 sel->requestwidget = widget;
1252 sel->request(elm_win_xwindow_get(top), ECORE_X_SELECTION_TARGET_TARGETS);
1253 sel->datacb = datacb;
1262 #ifdef HAVE_ELEMENTARY_X
1269 if (_elm_cnp_init_count++) return EINA_TRUE;
1270 for (i = 0; i < CNP_N_ATOMS; i++)
1272 atoms[i].atom = ecore_x_atom_get(atoms[i].name);
1273 ecore_x_selection_converter_atom_add(atoms[i].atom,
1274 atoms[i].converter);
1276 clipboard_atom = ecore_x_atom_get("CLIPBOARD");
1278 ecore_event_handler_add(ECORE_X_EVENT_SELECTION_CLEAR, selection_clear, NULL);
1279 ecore_event_handler_add(ECORE_X_EVENT_SELECTION_NOTIFY, selection_notify, NULL);
1281 text_uri = eina_stringshare_add("text/uri-list");
1286 selection_clear(void *udata __UNUSED__, int type __UNUSED__, void *event)
1288 Ecore_X_Event_Selection_Clear *ev = event;
1292 for (i = 0; i < ELM_SEL_MAX; i++)
1294 if (selections[i].ecore_sel == ev->selection) break;
1296 cnp_debug("selection %d clear\n", i);
1297 /* Not me... Don't care */
1298 if (i == ELM_SEL_MAX) return ECORE_CALLBACK_PASS_ON;
1300 sel = selections + i;
1305 return ECORE_CALLBACK_PASS_ON;
1310 * Response to a selection notify:
1311 * - So we have asked for the selection list.
1312 * - If it's the targets list, parse it, and fire of what we want,
1313 * else it's the data we want.
1316 selection_notify(void *udata __UNUSED__, int type __UNUSED__, void *event)
1318 Ecore_X_Event_Selection_Notify *ev = event;
1322 cnp_debug("selection notify callback: %d\n",ev->selection);
1323 switch (ev->selection)
1325 case ECORE_X_SELECTION_CLIPBOARD:
1326 sel = selections + ELM_SEL_CLIPBOARD;
1328 case ECORE_X_SELECTION_PRIMARY:
1329 sel = selections + ELM_SEL_PRIMARY;
1331 case ECORE_X_SELECTION_SECONDARY:
1332 sel = selections + ELM_SEL_SECONDARY;
1334 case ECORE_X_SELECTION_XDND:
1335 sel = selections + ELM_SEL_XDND;
1338 return ECORE_CALLBACK_PASS_ON;
1340 cnp_debug("Target is %s\n", ev->target);
1342 for (i = 0; i < CNP_N_ATOMS; i++)
1344 if (!strcmp(ev->target, atoms[i].name))
1346 if (atoms[i].notify)
1348 cnp_debug("Found something: %s\n", atoms[i].name);
1349 atoms[i].notify(sel, ev);
1353 cnp_debug("Ignored: No handler!\n");
1358 return ECORE_CALLBACK_PASS_ON;
1364 targets_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize)
1370 if (!data_ret) return EINA_FALSE;
1371 if (!data || (*((unsigned int *)data) >= ELM_SEL_MAX))
1374 sel = selections + *((int *)data);
1376 for (i = 0, count = 0; i < CNP_N_ATOMS ; i++)
1378 if (sel->format & atoms[i].formats) count++;
1381 aret = malloc(sizeof(Ecore_X_Atom) * count);
1382 for (i = 0, count = 0; i < CNP_N_ATOMS; i++)
1384 if (sel->format & atoms[i].formats) aret[count ++] = atoms[i].atom;
1388 if (typesize) *typesize = 32 /* urk */;
1389 if (ttype) *ttype = ECORE_X_ATOM_ATOM;
1390 if (size_ret) *size_ret = count;
1396 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__)
1398 cnp_debug("Image converter called\n");
1403 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__)
1407 cnp_debug("Vcard send called\n");
1409 sel = selections + *((int *)data);
1411 if (data_ret) *data_ret = strdup(sel->selbuf);
1412 if (size_ret) *size_ret = strlen(sel->selbuf);
1418 is_uri_type_data(Cnp_Selection *sel __UNUSED__, Ecore_X_Event_Selection_Notify *notify)
1420 Ecore_X_Selection_Data *data;
1423 data = notify->data;
1424 cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
1425 if (data->content == ECORE_X_SELECTION_CONTENT_FILES) return EINA_TRUE;
1426 else p = (char *)data->data;
1428 if (!p) return EINA_TRUE;
1429 cnp_debug("Got %s\n", p);
1430 if (strncmp(p, "file://", 7))
1432 if (*p != '/') return EINA_FALSE;
1439 * Callback to handle a targets response on a selection request:
1440 * So pick the format we'd like; and then request it.
1443 notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1445 Ecore_X_Selection_Data_Targets *targets;
1446 Ecore_X_Atom *atomlist;
1450 targets = notify->data;
1451 atomlist = (Ecore_X_Atom *)(targets->data.data);
1453 for (j = 1; j < CNP_N_ATOMS; j++)
1455 cnp_debug("\t%s %d\n", atoms[j].name, atoms[j].atom);
1456 if (!(atoms[j].formats & sel->requestformat)) continue;
1457 for (i = 0; i < targets->data.length; i++)
1459 if ((atoms[j].atom == atomlist[i]) && (atoms[j].notify))
1461 /* if ((j == CNP_ATOM_text_uri) ||
1462 (j == CNP_ATOM_text_urilist))
1464 if(!is_uri_type_data(sel, notify)) continue;
1466 cnp_debug("Atom %s matches\n",atoms[j].name);
1472 cnp_debug("Couldn't find anything that matches\n");
1473 return ECORE_CALLBACK_PASS_ON;
1476 top = elm_widget_top_get(sel->requestwidget);
1477 if (!top) top = sel->requestwidget;
1478 cnp_debug("Sending request for %s\n", atoms[j].name);
1479 sel->request(elm_win_xwindow_get(top), atoms[j].name);
1481 return ECORE_CALLBACK_PASS_ON;
1485 response_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1487 Ecore_X_Selection_Data_Targets *targets;
1488 Ecore_X_Atom *atomlist;
1492 targets = notify->data;
1493 atomlist = (Ecore_X_Atom *)(targets->data.data);
1495 /* Start from 1: Skip targets */
1496 for (j = 1 ; j < CNP_N_ATOMS ; j ++)
1498 if (!(atoms[j].formats & sel->requestformat)) continue;
1499 for (i = 0 ; i < targets->data.length ; i ++)
1501 if ((atoms[j].atom == atomlist[i]) && (atoms[j].response))
1503 /* Found a match: Use it */
1509 if (j == CNP_N_ATOMS)
1511 cnp_debug("No matching type found\n");
1515 top = elm_widget_top_get(sel->requestwidget);
1518 sel->request(elm_win_xwindow_get(top), atoms[j].name);
1523 entry_insert_filter(Evas_Object* entry, char* str)
1528 char *insertStr = str;
1529 // if entry has text only set then remove item tags
1530 if (elm_entry_cnp_textonly_get(entry))
1534 char *startTag = NULL;
1535 char *endTag = NULL;
1537 startTag = strstr(insertStr, "<item");
1539 startTag = strstr(insertStr, "</item");
1541 endTag = strstr(insertStr, ">");
1544 if (!endTag || startTag > endTag)
1546 cnp_debug("Broken tag: %s\n", str);
1550 size_t sindex = startTag - insertStr;
1551 size_t eindex = endTag - insertStr + 1;
1553 Eina_Strbuf *buf = eina_strbuf_new();
1556 eina_strbuf_append(buf, insertStr);
1557 eina_strbuf_remove(buf, sindex, eindex);
1558 insertStr = eina_strbuf_string_steal(buf);
1559 eina_strbuf_free(buf);
1563 cnp_debug("remove item tag: %s\n", insertStr);
1565 // if entry has single line set then remove <br> & <ps> tags
1566 if (elm_entry_single_line_get(entry))
1568 Eina_Strbuf *buf = eina_strbuf_new();
1571 eina_strbuf_append(buf, insertStr);
1572 eina_strbuf_replace_all(buf, "<br>", "");
1573 eina_strbuf_replace_all(buf, "<ps>", "");
1574 insertStr = eina_strbuf_string_steal(buf);
1575 eina_strbuf_free(buf);
1578 cnp_debug("remove break tag: %s\n", insertStr);
1580 elm_entry_entry_insert(entry, insertStr);
1582 if (insertStr != str)
1587 notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1589 Ecore_X_Selection_Data *data;
1592 data = notify->data;
1596 Elm_Selection_Data ddata;
1598 str = strdup(data->data);
1599 ddata.x = ddata.y = 0;
1600 ddata.format = ELM_SEL_FORMAT_TEXT;
1602 ddata.len = data->length;
1603 sel->datacb(sel->udata, sel->widget, &ddata);
1608 cnp_debug("Notify handler text %d %d %p\n", data->format,data->length, data->data);
1609 str = mark_up((char *)data->data, data->length, NULL);
1610 cnp_debug("String is %s (from %s)\n", str, data->data);
1611 entry_insert_filter(sel->requestwidget, str);
1612 //elm_entry_entry_insert(sel->requestwidget, str);
1619 * So someone is pasting an image into my entry or widget...
1622 notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1624 Ecore_X_Selection_Data *data;
1625 Ecore_X_Selection_Data_Files *files;
1629 data = notify->data;
1630 cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
1631 if (data->content == ECORE_X_SELECTION_CONTENT_FILES)
1633 cnp_debug("got a files list\n");
1634 files = notify->data;
1635 if (files->num_files > 1)
1637 /* Don't handle many items */
1638 cnp_debug("more then one file: Bailing\n");
1641 p = files->files[0];
1645 p = (char *)data->data;
1650 cnp_debug("Couldn't find a file\n");
1653 cnp_debug("Got %s\n",p);
1656 Elm_Selection_Data ddata;
1658 ddata.x = ddata.y = 0;
1659 ddata.format = ELM_SEL_FORMAT_MARKUP;
1661 ddata.len = data->length;
1662 sel->datacb(sel->udata, sel->widget, &ddata);
1665 if (strncmp(p, "file://", 7))
1667 /* Try and continue if it looks sane */
1668 if (*p != '/') return 0;
1672 p += strlen("file://");
1675 if (savedtypes.pi) pasteimage_free(savedtypes.pi);
1676 pi = pasteimage_alloc(p, strlen(p));
1677 if (savedtypes.textreq)
1679 savedtypes.textreq = 0;
1684 pasteimage_append(pi, sel->requestwidget);
1685 savedtypes.pi = NULL;
1691 * Just receieved an vcard, either through cut and paste, or dnd.
1694 vcard_receive(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1698 Ecore_X_Selection_Data *data;
1700 data = notify->data;
1701 cnp_debug("vcard receive\n");
1703 if (sel == (selections + ELM_SEL_XDND))
1705 Elm_Selection_Data ddata;
1707 cnp_debug("drag & drop\n");
1708 /* FIXME: this needs to be generic: Used for all receives */
1709 EINA_LIST_FOREACH(drops, l, dropable)
1711 if (dropable->obj == sel->requestwidget) break;
1715 cnp_debug("Unable to find drop object");
1716 ecore_x_dnd_send_finished();
1719 dropable = eina_list_data_get(l);
1720 ddata.x = savedtypes.x;
1721 ddata.y = savedtypes.y;
1722 ddata.format = ELM_SEL_FORMAT_VCARD;
1723 ddata.data = data->data;
1724 ddata.len = data->length;
1725 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1726 ecore_x_dnd_send_finished();
1728 else if (sel->datacb)
1730 Elm_Selection_Data ddata;
1731 ddata.x = ddata.y = 0;
1732 ddata.format = ELM_SEL_FORMAT_IMAGE;
1733 ddata.data = data->data;
1734 ddata.len = data->length;
1735 sel->datacb(sel->udata, sel->widget, &ddata);
1739 cnp_debug("Paste request\n");
1748 notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1750 Ecore_X_Selection_Data *data;
1754 cnp_debug("got a png (or a jpeg)!\n");
1755 data = notify->data;
1757 cnp_debug("Size if %d\n", data->length);
1761 Elm_Selection_Data ddata;
1763 ddata.x = ddata.y = 0;
1764 ddata.format = ELM_SEL_FORMAT_IMAGE;
1765 ddata.data = data->data;
1766 ddata.len = data->length;
1767 sel->datacb(sel->udata, sel->widget, &ddata);
1771 /* generate tmp name */
1772 tmp = elm_cnp_tempfile_create(data->length);
1773 memcpy(tmp->map, data->data, data->length);
1774 munmap(tmp->map,data->length);
1776 /* FIXME: Add to paste image data to clean up */
1777 pi = pasteimage_alloc(tmp->filename, strlen(tmp->filename));
1778 pasteimage_append(pi, sel->requestwidget);
1785 notify_handler_edje(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1787 Ecore_X_Selection_Data *data;
1789 data = notify->data;
1791 char *stripstr = NULL;
1792 stripstr = malloc(sizeof(char) * (data->length + 1));
1793 strncpy(stripstr, (char *)data->data, data->length);
1794 stripstr[data->length] = '\0';
1798 Elm_Selection_Data ddata;
1799 ddata.x = ddata.y = 0;
1800 ddata.format = ELM_SEL_FORMAT_MARKUP;
1801 ddata.data = stripstr;
1802 ddata.len = data->length;
1803 sel->datacb(sel->udata, sel->widget, &ddata);
1806 entry_insert_filter(sel->requestwidget, stripstr);
1807 //elm_entry_entry_insert(sel->requestwidget, stripstr);
1809 cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
1815 * Warning: Generic text/html can';t handle it sanely.
1816 * Firefox sends ucs2 (i think).
1817 * chrome sends utf8... blerg
1820 notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1822 Ecore_X_Selection_Data *data;
1824 cnp_debug("Got some HTML: Checking encoding is useful\n");
1825 data = notify->data;
1827 char *stripstr = NULL;
1828 stripstr = malloc(sizeof(char) * (data->length + 1));
1829 strncpy(stripstr, (char *)data->data, data->length);
1830 stripstr[data->length] = '\0';
1834 Elm_Selection_Data ddata;
1835 ddata.x = ddata.y = 0;
1836 ddata.format = ELM_SEL_FORMAT_HTML;
1837 ddata.data = stripstr;
1838 ddata.len = data->length;
1839 sel->datacb(sel->udata, sel->widget, &ddata);
1842 entry_insert_filter(sel->requestwidget, stripstr);
1843 //elm_entry_entry_insert(sel->requestwidget, stripstr);
1845 cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
1852 text_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1856 cnp_debug("text converter\n");
1857 sel = selections + *((int *)data);
1858 if (!sel->active) return EINA_TRUE;
1860 if ((sel->format & ELM_SEL_FORMAT_MARKUP) ||
1861 (sel->format & ELM_SEL_FORMAT_HTML))
1863 *data_ret = remove_tags(sel->selbuf, size_ret);
1865 else if (sel->format & ELM_SEL_FORMAT_TEXT)
1867 *data_ret = strdup(sel->selbuf);
1868 *size_ret = strlen(sel->selbuf);
1870 else if (sel->format & ELM_SEL_FORMAT_IMAGE)
1872 cnp_debug("Image %s\n", evas_object_type_get(sel->widget));
1873 cnp_debug("Elm type: %s\n", elm_object_widget_type_get(sel->widget));
1874 evas_object_image_file_get(elm_photocam_internal_image_get(sel->widget), (const char **)data_ret, NULL);
1875 if (!*data_ret) *data_ret = strdup("No file");
1876 else *data_ret = strdup(*data_ret);
1877 *size_ret = strlen(*data_ret);
1883 edje_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 /* if (data_ret) *data_ret = strdup(sel->selbuf);
1888 if (size_ret) *size_ret = strlen(sel->selbuf);*/
1891 if (data_ret && (sel->format == ELM_SEL_FORMAT_TEXT))
1893 if (sel->selbuf && sel->selbuf[0] != '\0')
1894 edje = mark_up(sel->selbuf, strlen(sel->selbuf), NULL);
1896 else if (data_ret && ((sel->format & ELM_SEL_FORMAT_HTML)))
1898 Eina_List *nodeList = NULL;
1902 nodeData = _get_start_node(sel->selbuf);
1906 nodeList = eina_list_append(nodeList, nodeData);
1907 nodeData = _get_next_node(nodeData);
1910 _link_match_tags(nodeList);
1912 _set_HTML_tag_data(nodeList);
1915 _dumpNode(nodeList);
1917 edje = _convert_to_edje(nodeList);
1919 cnp_debug("convert edje: %s\n", edje);
1921 EINA_LIST_FOREACH(nodeList, trail, nodeData)
1922 _delete_node(nodeData);
1923 eina_list_free(nodeList);
1931 *data_ret = strdup(sel->selbuf);
1936 *size_ret = strlen(edje);
1938 *size_ret = strlen(sel->selbuf);
1945 html_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1949 sel = selections + *(int *)data;
1952 char *convert_target = sel->selbuf;
1953 Eina_Bool convert_edje = EINA_FALSE;
1955 if (data_ret && (sel->format == ELM_SEL_FORMAT_TEXT))
1957 if (sel->selbuf && sel->selbuf[0] != '\0')
1959 convert_target = mark_up(sel->selbuf, strlen(sel->selbuf), NULL);
1960 convert_edje = EINA_TRUE;
1964 if (data_ret && ((sel->format & ELM_SEL_FORMAT_MARKUP) || convert_edje))
1966 Eina_List *nodeList = NULL;
1970 nodeData = _get_start_node(convert_target);
1974 nodeList = eina_list_append(nodeList, nodeData);
1975 nodeData = _get_next_node(nodeData);
1978 _link_match_tags(nodeList);
1980 _set_EFL_tag_data(nodeList);
1983 _dumpNode(nodeList);
1985 html = _convert_to_html(nodeList);
1987 cnp_debug("convert html: %s\n", html);
1989 EINA_LIST_FOREACH(nodeList, trail, nodeData)
1990 _delete_node(nodeData);
1991 eina_list_free(nodeList);
1998 *data_ret = strdup(sel->selbuf);
2004 *size_ret = strlen(html);
2006 *size_ret = strlen(sel->selbuf);
2008 if (convert_target != sel->selbuf)
2009 free(convert_target);
2015 uri_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
2018 sel = selections + *((int *)data);
2019 cnp_debug("Uri converter\n");
2020 if (data_ret) *data_ret = strdup(sel->selbuf);
2021 if (size_ret) *size_ret = strlen(sel->selbuf);
2026 * Image paste provide
2029 /* FIXME: Should add provider for each pasted item: Use data to store it
2031 static Evas_Object *
2032 image_provider(void *images __UNUSED__, Evas_Object *entry, const char *item)
2037 cnp_debug("image provider for %s called\n", item);
2038 EINA_LIST_FOREACH(pastedimages, l, pi)
2040 cnp_debug("is it %s?\n",pi->tag);
2041 if (!strcmp(pi->tag, item))
2045 o = evas_object_image_filled_add(evas_object_evas_get(entry));
2046 /* FIXME: Handle eets */
2047 cnp_debug("file is %s (object is %p)\n", pi->file, o);
2048 evas_object_image_file_set(o, pi->file, NULL);
2049 evas_object_show(o);
2057 static Paste_Image *
2058 pasteimage_alloc(const char *file, int pathlen)
2062 char *buf, *filebuf;
2063 int prefixlen = strlen("file://");
2065 pi = calloc(1, sizeof(Paste_Image));
2066 if (!pi) return NULL;
2068 len = snprintf(NULL, 0, "pasteimage-%p", pi);
2076 snprintf(buf, len, "pasteimage-%p", pi);
2081 if (strstr(file,"file://")) file += prefixlen;
2082 filebuf = alloca(pathlen + 1);
2083 strncpy(filebuf, file, pathlen);
2084 filebuf[pathlen] = 0;
2085 pi->file = strdup(filebuf);
2092 pasteimage_free(Paste_Image *pi)
2095 if (pi->file) free((void*)pi->file);
2096 if (pi->tag) free((void*)pi->tag);
2101 pasteimage_provider_set(Evas_Object *entry)
2106 if (!entry) return EINA_FALSE;
2107 type = elm_widget_type_get(entry);
2108 cnp_debug("type is %s\n", type);
2109 if ((!type) || (strcmp(type, "entry"))) return EINA_FALSE;
2111 v = evas_object_data_get(entry, PROVIDER_SET);
2114 evas_object_data_set(entry, PROVIDER_SET, pasteimage_provider_set);
2115 elm_entry_item_provider_append(entry, image_provider, NULL);
2116 evas_object_event_callback_add(entry, EVAS_CALLBACK_FREE,
2117 entry_deleted, NULL);
2124 pasteimage_append(Paste_Image *pi, Evas_Object *entry)
2128 static const char *tagstring = "<item absize=240x180 href=file://%s></item>";
2130 if (!pi) return EINA_FALSE;
2131 if (!entry) return EINA_FALSE;
2132 if (elm_entry_cnp_textonly_get(entry)) return EINA_FALSE;
2134 pasteimage_provider_set(entry);
2136 len = strlen(tagstring)+strlen(pi->file);
2138 pastedimages = eina_list_append(pastedimages, pi);
2139 entrytag = alloca(len + 1);
2140 snprintf(entrytag, len + 1, tagstring, pi->file);
2141 elm_entry_entry_insert(entry, entrytag);
2147 entry_deleted(void *images __UNUSED__, Evas *e __UNUSED__, Evas_Object *entry, void *unused __UNUSED__)
2152 EINA_LIST_FOREACH_SAFE(pastedimages, l, next, pi)
2154 if (pi->entry == entry)
2155 pastedimages = eina_list_remove_list(pastedimages, l);
2161 remove_tags(const char *p, int *len)
2165 if (!p) return NULL;
2167 q = malloc(strlen(p) + 1);
2168 if (!q) return NULL;
2173 if ((*p != '<') && (*p != '&')) *q++ = *p++;
2176 if ((p[1] == 'b') && (p[2] == 'r') &&
2177 ((p[3] == ' ') || (p[3] == '/') || (p[3] == '>')))
2179 while ((*p) && (*p != '>')) p++;
2185 for (i = 0 ; i < N_ESCAPES ; i++)
2187 if (!strncmp(p,escapes[i].escape, strlen(escapes[i].escape)))
2189 p += strlen(escapes[i].escape);
2190 *q = escapes[i].value;
2195 if (i == N_ESCAPES) *q ++= '&';
2199 if (len) *len = q - ret;
2205 mark_up(const char *start, int inlen, int *lenp)
2210 const char *endp = NULL;
2212 if (!start) return NULL;
2213 if (inlen >= 0) endp = start + inlen;
2214 /* First pass: Count characters */
2215 for (l = 0, p = start; ((!endp) || (p < endp)) && (*p); p++)
2217 for (i = 0 ; i < N_ESCAPES ; i ++)
2219 if (*p == escapes[i].value)
2221 if (!iscntrl(escapes[i].value)) l++;
2222 l += strlen(escapes[i].escape);
2226 if (i == N_ESCAPES) l++;
2229 q = ret = malloc(l + 1);
2231 /* Second pass: Change characters */
2232 for (p = start; ((!endp) || (p < endp)) && (*p); )
2234 for (i = 0; i < N_ESCAPES; i++)
2236 if (*p == escapes[i].value)
2238 if (!iscntrl(escapes[i].value)) *q++ = '&';
2239 strcpy(q, escapes[i].escape);
2240 q += strlen(escapes[i].escape);
2245 if (i == N_ESCAPES) *q++ = *p++;
2249 if (lenp) *lenp = l;
2255 _dnd_enter(void *data __UNUSED__, int etype __UNUSED__, void *ev)
2257 Ecore_X_Event_Xdnd_Enter *enter = ev;
2261 if ((!enter) || (!enter->num_types) || (!enter->types)) return EINA_TRUE;
2263 cnp_debug("Types\n");
2264 savedtypes.ntypes = enter->num_types;
2265 if (savedtypes.types) free(savedtypes.types);
2266 savedtypes.types = malloc(sizeof(char *) * enter->num_types);
2267 if (!savedtypes.types) return EINA_FALSE;
2269 for (i = 0; i < enter->num_types; i++)
2271 savedtypes.types[i] = eina_stringshare_add(enter->types[i]);
2272 cnp_debug("Type is %s %p %p\n", enter->types[i],
2273 savedtypes.types[i],text_uri);
2274 if (savedtypes.types[i] == text_uri)
2276 /* Request it, so we know what it is */
2277 cnp_debug("Sending uri request\n");
2278 savedtypes.textreq = 1;
2279 if (savedtypes.pi) pasteimage_free(savedtypes.pi);
2280 savedtypes.pi = NULL; /* FIXME: Free? */
2281 ecore_x_selection_xdnd_request(enter->win, text_uri);
2285 /* FIXME: Find an object and make it current */
2290 _dnd_drop(void *data __UNUSED__, int etype __UNUSED__, void *ev)
2292 struct _Ecore_X_Event_Xdnd_Drop *drop;
2296 Ecore_X_Window xwin;
2297 Elm_Selection_Data ddata;
2303 // check we still have something to drop
2304 if (!drops) return EINA_TRUE;
2306 /* Find any widget in our window; then work out geometry rel to our window */
2307 for (l = drops; l; l = l->next)
2310 xwin = (Ecore_X_Window)ecore_evas_window_get
2311 (ecore_evas_ecore_evas_get(evas_object_evas_get
2313 if (xwin == drop->win) break;
2315 /* didn't find a window */
2316 if (!l) return EINA_TRUE;
2318 /* Calculate real (widget relative) position */
2319 // - window position
2320 // - widget position
2321 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(dropable->obj));
2322 ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
2323 savedtypes.x = drop->position.x - x;
2324 savedtypes.y = drop->position.y - y;
2326 cnp_debug("Drop position is %d,%d\n", savedtypes.x, savedtypes.y);
2328 for (; l; l = l->next)
2331 evas_object_geometry_get(dropable->obj, &x, &y, &w, &h);
2332 if ((savedtypes.x >= x) && (savedtypes.y >= y) &&
2333 (savedtypes.x < x + w) && (savedtypes.y < y + h))
2337 if (!l) return EINA_TRUE; /* didn't find one */
2339 evas_object_geometry_get(dropable->obj, &x, &y, NULL, NULL);
2343 /* Find our type from the previous list */
2344 for (i = 0; i < CNP_N_ATOMS; i++)
2346 for (j = 0; j < savedtypes.ntypes; j++)
2348 if (!strcmp(savedtypes.types[j], atoms[i].name)) goto found;
2352 cnp_debug("Didn't find a target\n");
2356 cnp_debug("Found a target we'd like: %s\n", atoms[i].name);
2357 cnp_debug("0x%x\n",xwin);
2359 if (i == CNP_ATOM_text_urilist)
2361 cnp_debug("We found a URI... (%scached) %s\n",
2362 savedtypes.pi ? "" : "not ",
2363 savedtypes.pi->file);
2367 static const char *tagstring = "<item absize=240x180 href="
2368 "file://%s></item>";
2369 ddata.x = savedtypes.x;
2370 ddata.y = savedtypes.y;
2372 /* If it's markup that also supports images */
2373 if ((dropable->types & ELM_SEL_FORMAT_MARKUP) &&
2374 (dropable->types & ELM_SEL_FORMAT_IMAGE))
2377 ddata.format = ELM_SEL_FORMAT_MARKUP;
2378 pasteimage_provider_set(dropable->obj);
2380 pastedimages = eina_list_append(pastedimages, savedtypes.pi);
2381 len = strlen(tagstring) + strlen(savedtypes.pi->file);
2382 entrytag = alloca(len + 1);
2383 snprintf(entrytag, len + 1, tagstring, savedtypes.pi->file);
2384 ddata.data = entrytag;
2385 cnp_debug("Insert %s\n", (char *)ddata.data);
2386 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
2387 ecore_x_dnd_send_finished();
2389 if (savedtypes.pi) pasteimage_free(savedtypes.pi);
2390 savedtypes.pi = NULL;
2393 else if (dropable->types & ELM_SEL_FORMAT_IMAGE)
2395 cnp_debug("Doing image insert (%s)\n", savedtypes.pi->file);
2396 ddata.format = ELM_SEL_FORMAT_IMAGE;
2397 ddata.data = (char *)savedtypes.pi->file;
2398 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
2399 ecore_x_dnd_send_finished();
2401 if (savedtypes.pi) pasteimage_free(savedtypes.pi);
2402 savedtypes.pi = NULL;
2408 cnp_debug("Item doesn't support images... passing\n");
2409 pasteimage_free(savedtypes.pi);
2413 else if (savedtypes.textreq)
2415 /* Already asked: Pretend we asked now, and paste immediately when
2417 savedtypes.textreq = 0;
2418 ecore_x_dnd_send_finished();
2423 cnp_debug("doing a request then\n");
2424 selections[ELM_SEL_XDND].requestwidget = dropable->obj;
2425 selections[ELM_SEL_XDND].requestformat = ELM_SEL_FORMAT_MARKUP;
2426 selections[ELM_SEL_XDND].active = EINA_TRUE;
2428 ecore_x_selection_xdnd_request(xwin, atoms[i].name);
2433 _dnd_position(void *data __UNUSED__, int etype __UNUSED__, void *ev)
2435 struct _Ecore_X_Event_Xdnd_Position *pos;
2436 Ecore_X_Rectangle rect;
2440 /* Need to send a status back */
2441 /* FIXME: Should check I can drop here */
2442 /* FIXME: Should highlight widget */
2443 rect.x = pos->position.x - 5;
2444 rect.y = pos->position.y - 5;
2447 ecore_x_dnd_send_status(EINA_TRUE, EINA_FALSE, rect, pos->action);
2453 * When dragging this is callback response from the destination.
2454 * The important thing we care about: Can we drop; thus update cursor
2458 _dnd_status(void *data __UNUSED__, int etype __UNUSED__, void *ev)
2460 struct _Ecore_X_Event_Xdnd_Status *status = ev;
2462 if (!status) return EINA_TRUE;
2464 /* Only thing we care about: will accept */
2465 if (status->will_accept)
2467 cnp_debug("Will accept\n");
2470 { /* Won't accept */
2471 cnp_debug("Won't accept accept\n");
2477 * Add a widget as drop target.
2480 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
2483 Ecore_X_Window xwin;
2487 if (!obj) return EINA_FALSE;
2488 if (!_elm_cnp_init_count) _elm_cnp_init();
2490 /* Is this the first? */
2491 first = (!drops) ? 1 : 0;
2493 EINA_LIST_FOREACH(drops, item, drop)
2495 if (drop->obj == obj)
2497 /* Update: Not a new one */
2498 drop->dropcb = dropcb;
2499 drop->cbdata = cbdata;
2500 drop->types = format;
2505 /* Create new drop */
2506 drop = calloc(1, sizeof(Dropable));
2507 if (!drop) return EINA_FALSE;
2508 /* FIXME: Check for eina's deranged error method */
2509 drops = eina_list_append(drops, drop);
2511 if (!drops/* || or other error */)
2516 drop->dropcb = dropcb;
2517 drop->cbdata = cbdata;
2518 drop->types = format;
2521 evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL,
2522 /* I love C and varargs */
2523 (Evas_Object_Event_Cb)elm_drop_target_del,
2525 /* FIXME: Handle resizes */
2527 /* If not the first: We're done */
2528 if (!first) return EINA_TRUE;
2530 xwin = (Ecore_X_Window)ecore_evas_window_get
2531 (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
2533 ecore_x_dnd_aware_set(xwin, EINA_TRUE);
2535 cnp_debug("Adding drop target calls\n");
2536 handler_enter = ecore_event_handler_add(ECORE_X_EVENT_XDND_ENTER,
2538 handler_pos = ecore_event_handler_add(ECORE_X_EVENT_XDND_POSITION,
2539 _dnd_position, NULL);
2540 handler_drop = ecore_event_handler_add(ECORE_X_EVENT_XDND_DROP,
2547 elm_drop_target_del(Evas_Object *obj)
2549 Dropable *drop,*del;
2551 Ecore_X_Window xwin;
2554 EINA_LIST_FOREACH(drops, item, drop)
2556 if (drop->obj == obj)
2558 drops = eina_list_remove_list(drops, item);
2563 if (!del) return EINA_FALSE;
2565 evas_object_event_callback_del(obj, EVAS_CALLBACK_FREE,
2566 (Evas_Object_Event_Cb)elm_drop_target_del);
2568 /* If still drops there: All fine.. continue */
2569 if (drops) return EINA_TRUE;
2571 cnp_debug("Disabling DND\n");
2572 xwin = (Ecore_X_Window)ecore_evas_window_get
2573 (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
2574 ecore_x_dnd_aware_set(xwin, EINA_FALSE);
2576 ecore_event_handler_del(handler_pos);
2577 ecore_event_handler_del(handler_drop);
2578 ecore_event_handler_del(handler_enter);
2582 pasteimage_free(savedtypes.pi);
2583 savedtypes.pi = NULL;
2591 _drag_mouse_up(void *un __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *data __UNUSED__)
2593 evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_UP, _drag_mouse_up);
2597 dragdonecb(dragdonecb,selections[ELM_SEL_XDND].widget);
2602 evas_object_del(dragwin);
2608 _drag_move(void *data __UNUSED__, Ecore_X_Xdnd_Position *pos)
2610 evas_object_move(dragwin,
2611 pos->position.x - _dragx,
2612 pos->position.y - _dragy);
2617 elm_drag_start(Evas_Object *obj, Elm_Sel_Format format, const char *data, void (*dragdone) (void *data, Evas_Object *), void *donecbdata)
2619 Ecore_X_Window xwin;
2621 Elm_Sel_Type xdnd = ELM_SEL_XDND;
2623 int x, y, x2, y2, x3, y3;
2627 if (!_elm_cnp_init_count) _elm_cnp_init();
2629 xwin = elm_win_xwindow_get(obj);
2631 cnp_debug("starting drag...\n");
2633 ecore_x_dnd_type_set(xwin, "text/uri-list", 1);
2634 sel = selections + ELM_SEL_XDND;
2637 sel->format = format;
2638 sel->selbuf = data ? strdup(data) : NULL;
2639 dragdonecb = dragdone;
2640 dragdonedata = donecbdata;
2642 ecore_x_dnd_callback_pos_update_set(_drag_move, NULL);
2643 ecore_x_dnd_begin(xwin, (unsigned char *)&xdnd, sizeof(Elm_Sel_Type));
2644 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
2645 _drag_mouse_up, NULL);
2647 handler_status = ecore_event_handler_add(ECORE_X_EVENT_XDND_STATUS,
2650 dragwin = elm_win_add(NULL, "Elm Drag Object", ELM_WIN_UTILITY);
2651 elm_win_override_set(dragwin, 1);
2653 /* FIXME: Images only */
2654 icon = elm_icon_add(dragwin);
2655 elm_icon_file_set(icon, data + 7, NULL); /* 7!? "file://" */
2656 elm_win_resize_object_add(dragwin,icon);
2657 evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
2658 evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, EVAS_HINT_FILL);
2660 /* Position subwindow appropriately */
2661 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(obj));
2662 ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
2663 evas_object_geometry_get(obj, &x2, &y2, &w, &h);
2666 evas_object_move(dragwin, x, y);
2667 evas_object_resize(icon, w, h);
2668 evas_object_resize(dragwin, w, h);
2670 evas_object_show(icon);
2671 evas_object_show(dragwin);
2673 evas_pointer_canvas_xy_get(evas_object_evas_get(obj), &x3, &y3);
2681 elm_cnp_tempfile_create(int size)
2684 const char *tmppath;
2687 info = malloc(sizeof(Tmp_Info));
2688 if (!info) return NULL;
2690 tmppath = getenv("TMP");
2691 if (!tmppath) tmppath = P_tmpdir;
2692 if (!tmppath) tmppath = "/tmp";
2693 len = snprintf(NULL, 0, "%s/%sXXXXXX", tmppath, "elmcnpitem-");
2700 info->filename = malloc(len);
2701 if (!info->filename)
2706 snprintf(info->filename,len,"%s/%sXXXXXX", tmppath, "elmcnpitem-");
2708 info->fd = mkstemp(info->filename);
2713 /* And before someone says anything see POSIX 1003.1-2008 page 400 */
2716 pid = (long)getpid();
2717 /* Use pid instead of /proc/self: That way if can be passed around */
2718 len = snprintf(NULL,0,"/proc/%li/fd/%i", pid, info->fd);
2723 snprintf(tmp,len, "/proc/%li/fd/%i", pid, info->fd);
2724 unlink(info->filename);
2725 free(info->filename);
2726 info->filename = tmp;
2731 cnp_debug("filename is %s\n", info->filename);
2734 /* Set map to NULL and return */
2741 if (ftruncate(info->fd, size))
2743 perror("ftruncate");
2749 eina_mmap_safety_enabled_set(EINA_TRUE);
2751 info->map = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, info->fd, 0);
2752 if (info->map == MAP_FAILED)
2764 tmpinfo_free(Tmp_Info *info)
2766 if (!info) return 0;
2767 free(info->filename);
2773 /* Stubs for windows */
2775 elm_drag_start(Evas_Object *o, Elm_Sel_Format f, const char *d, void (*donecb)(void *, Evas_Object *),void *cbdata)
2781 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
2787 elm_drop_target_del(Evas_Object *o)
2793 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-2f0^-2{2(0W1st0 :*/