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');
776 if ((!strncmp(tag_str, "<item", 5) && !strcmp(tag_name, "href")) // EFL img tag
777 || (!strncmp(tag_str, "<img", 4) && !strcmp(tag_name, "src"))) // HTML img tag
780 for (i = start; i < spCnt; i++)
782 if (spArray[i] && spArray[i] < valueEnd)
783 valueEnd = spArray[i];
786 int valueLength = valueEnd - value;
787 return strndup(value, valueLength);
794 _set_EFL_font_data(PFontTagData data, const char *tag_str)
799 data = calloc(1, sizeof(FontTagData));
800 value = _get_tag_value(tag_str, "font_size");
801 freeAndAssign(data->size, value);
802 value = _get_tag_value(tag_str, "color");
803 freeAndAssign(data->color, value);
804 value = _get_tag_value(tag_str, "bgcolor");
805 freeAndAssign(data->bg_color, value);
806 value = _get_tag_value(tag_str, "font");
807 freeAndAssign(data->name, value);
813 _set_EFL_item_data(PItemTagData data, const char *tag_str)
818 data = calloc(1, sizeof(ItemTagData));
819 value = _get_tag_value(tag_str, "href");
822 char *path = strstr(value, "file://");
825 char *modify = malloc(sizeof(char) * (strlen(value) + 1));
826 strncpy(modify, "file://", 8);
828 while (path[1] && path[0] && path[1] == '/' && path[0] == '/')
832 strcat(modify, path);
834 cnp_debug("image href ---%s---\n", data->href);
838 freeAndAssign(data->href, value);
841 value = _get_tag_value(tag_str, "absize");
844 char *xpos = strchr(value, 'x');
847 int absizeLen = strlen(value);
848 freeAndAssign(data->width, strndup(value, xpos - value));
849 freeAndAssign(data->height, strndup(xpos + 1, absizeLen - (xpos - value) - 1));
850 cnp_debug("image width: -%s-, height: -%s-\n", data->width, data->height);
858 _set_EFL_tag_data(Eina_List* nodes)
863 EINA_LIST_FOREACH(nodes, l, trail)
867 if (!strcmp("font", trail->tag))
868 trail->tagData = _set_EFL_font_data(trail->tagData, trail->tag_str);
869 else if (!strcmp("item", trail->tag))
870 trail->tagData = _set_EFL_item_data(trail->tagData, trail->tag_str);
875 _set_HTML_font_data(PFontTagData data, const char *tag_str)
880 data = calloc(1, sizeof(FontTagData));
881 value = _get_tag_value(tag_str, "size");
882 freeAndAssign(data->size, value);
883 value = _get_tag_value(tag_str, "color");
884 freeAndAssign(data->color, value);
885 value = _get_tag_value(tag_str, "bgcolor");
886 freeAndAssign(data->bg_color, value);
887 value = _get_tag_value(tag_str, "face");
888 freeAndAssign(data->name, value);
894 _set_HTML_img_data(PItemTagData data, const char *tag_str)
899 data = calloc(1, sizeof(ItemTagData));
900 value = _get_tag_value(tag_str, "src");
903 char *path = strstr(value, "file://");
906 char *modify = malloc(sizeof(char) * (strlen(value) + 1));
907 strncpy(modify, "file://", 8);
909 while (path[1] && path[0] && path[1] == '/' && path[0] == '/')
913 strcat(modify, path);
915 cnp_debug("image src ---%s---\n", data->href);
919 freeAndAssign(data->href, value);
922 value = _get_tag_value(tag_str, "width");
923 freeAndAssign(data->width, value);
924 value = _get_tag_value(tag_str, "height");
925 freeAndAssign(data->height, value);
930 _set_HTML_tag_data(Eina_List* nodes)
935 EINA_LIST_FOREACH(nodes, l, trail)
939 if (!strcmp("font", trail->tag))
940 trail->tagData = _set_HTML_font_data(trail->tagData, trail->tag_str);
941 else if (!strcmp("img", trail->tag))
942 trail->tagData = _set_HTML_img_data(trail->tagData, trail->tag_str);
948 _dumpNode(Eina_List* nodes)
953 EINA_LIST_FOREACH(nodes, l, trail)
955 cnp_debug("tag: %s, tag_str: %s, str: %s, tagPosType: %d\n",
956 trail->tag, trail->tag_str, trail->str, trail->tagPosType);
957 cnp_debug("matchTag: %x ", (unsigned int)trail->matchTag);
959 cnp_debug("matchTag->tag_str: %s", trail->matchTag->tag_str);
962 if (!strcmp(trail->tag, "font"))
964 PFontTagData data = trail->tagData;
965 cnp_debug(" tagData->name: %s, tagData->color: %s, tagData->size: %s, tagData->bg_color: %s",
966 data->name, data->color, data->size, data->bg_color);
968 else if (!strcmp(trail->tag, "item") || !strcmp(trail->tag, "img"))
970 PItemTagData data = trail->tagData;
971 cnp_debug(" tagData->href: %s, tagData->width: %s, tagData->height: %s",
972 data->href, data->width, data->height);
975 cnp_debug("\nERROR!!!! not need tagData");
983 _convert_to_html(Eina_List* nodes)
988 Eina_Strbuf *html = eina_strbuf_new();
990 int tableCnt = sizeof(_EFLtoHTMLConvertTable) / sizeof(TagTable);
992 EINA_LIST_FOREACH(nodes, l, trail)
996 char *tagName = trail->tagPosType == TAGPOS_END ?
997 trail->matchTag->tag : trail->tag;
999 for(j = 0; j < tableCnt; j++)
1001 if (!strcmp(_EFLtoHTMLConvertTable[j].src, tagName))
1003 switch(trail->tagPosType)
1006 eina_strbuf_append(html, "</");
1009 eina_strbuf_append(html, "<");
1013 eina_strbuf_append(html, _EFLtoHTMLConvertTable[j].dst);
1014 if (trail->tagPosType != TAGPOS_END)
1016 if (!strcmp(_EFLtoHTMLConvertTable[j].src, "font"))
1018 PFontTagData data = trail->tagData;
1023 eina_strbuf_append_printf(html, " color=\"%s\"", data->color);
1025 eina_strbuf_append_printf(html, " size=\"%s\"", data->size);
1030 else if (!strcmp(_EFLtoHTMLConvertTable[j].src, "item"))
1032 PItemTagData data = trail->tagData;
1034 eina_strbuf_append_printf(html, " src=\"%s\"", data->href);
1036 eina_strbuf_append_printf(html, " width=\"%s\"", data->width);
1038 eina_strbuf_append_printf(html, " height=\"%s\"", data->height);
1041 switch(trail->tagPosType)
1043 /* closed tag does not need in HTML
1045 eina_strbuf_append(html, " />");
1048 eina_strbuf_append(html, ">");
1056 eina_strbuf_append(html, trail->str);
1059 char *ret = eina_strbuf_string_steal(html);
1060 eina_strbuf_free(html);
1064 #define IMAGE_DEFAULT_WIDTH "240"
1065 #define IMAGE_DEFAULT_HEIGHT "180"
1069 _convert_to_edje(Eina_List* nodes)
1074 Eina_Strbuf *html = eina_strbuf_new();
1076 int tableCnt = sizeof(_HTMLtoEFLConvertTable) / sizeof(TagTable);
1078 EINA_LIST_FOREACH(nodes, l, trail)
1082 char *tagName = trail->tagPosType == TAGPOS_END ?
1083 trail->matchTag->tag : trail->tag;
1085 for(j = 0; j < tableCnt; j++)
1087 if (!strcmp(_HTMLtoEFLConvertTable[j].src, tagName))
1089 if (_HTMLtoEFLConvertTable[j].dst[0] != '\0')
1091 switch(trail->tagPosType)
1094 eina_strbuf_append(html, "</");
1097 eina_strbuf_append(html, "<");
1101 eina_strbuf_append(html, _HTMLtoEFLConvertTable[j].dst);
1103 if (trail->tagPosType != TAGPOS_END)
1105 if (!strcmp(_HTMLtoEFLConvertTable[j].src, "font"))
1107 PFontTagData data = trail->tagData;
1112 eina_strbuf_append_printf(html, "<color=%s>", data->color);
1114 eina_strbuf_append_printf(html, "<font_size=%s>", data->size);
1120 else if (!strcmp(_HTMLtoEFLConvertTable[j].src, "img"))
1122 PItemTagData data = trail->tagData;
1123 char *width = IMAGE_DEFAULT_WIDTH, *height = IMAGE_DEFAULT_HEIGHT;
1125 width = data->width;
1127 height = data->height;
1128 eina_strbuf_append_printf(html, " absize=%sx%s", width, height);
1130 eina_strbuf_append_printf(html, " href=%s></item>", data->href);
1136 if (_HTMLtoEFLConvertTable[j].dst[0] == '\0')
1138 if (!strcmp(_HTMLtoEFLConvertTable[j].src, "font"))
1140 if (trail->matchTag->tagData)
1142 PFontTagData data = trail->matchTag->tagData;
1147 eina_strbuf_append_printf(html, "</color>");
1149 eina_strbuf_append_printf(html, "</font>");
1158 switch(trail->tagPosType)
1160 /* not support in efl
1162 eina_strbuf_append(html, " />");
1166 eina_strbuf_append(html, ">");
1171 }/* for(j = 0; j < tableCnt; j++) end */
1174 eina_strbuf_append(html, trail->str);
1177 char *ret = eina_strbuf_string_steal(html);
1178 eina_strbuf_free(html);
1184 elm_selection_selection_has_owner(void)
1186 #ifdef HAVE_ELEMENTARY_X
1187 return !!ecore_x_selection_owner_get(clipboard_atom);
1194 elm_selection_set(Elm_Sel_Type selection, Evas_Object *widget, Elm_Sel_Format format, const char *selbuf)
1196 #ifdef HAVE_ELEMENTARY_X
1197 Evas_Object *top = elm_widget_top_get(widget);
1198 Ecore_X_Window xwin;
1201 if (top) xwin = elm_win_xwindow_get(top);
1202 else xwin = elm_win_xwindow_get(widget);
1203 if (!xwin) return EINA_FALSE;
1204 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
1205 if (!_elm_cnp_init_count) _elm_cnp_init();
1206 if ((!selbuf) && (format != ELM_SEL_FORMAT_IMAGE))
1207 return elm_selection_clear(selection, widget);
1209 sel = selections + selection;
1212 sel->widget = widget;
1214 sel->set(xwin, &selection, sizeof(Elm_Sel_Type));
1215 sel->format = format;
1216 sel->selbuf = selbuf ? strdup(selbuf) : NULL;
1225 elm_selection_clear(Elm_Sel_Type selection, Evas_Object *widget)
1227 #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;
1235 /* No longer this selection: Consider it gone! */
1236 if ((!sel->active) || (sel->widget != widget)) return EINA_TRUE;
1249 elm_selection_get(Elm_Sel_Type selection, Elm_Sel_Format format,
1250 Evas_Object *widget, Elm_Drop_Cb datacb, void *udata)
1252 #ifdef HAVE_ELEMENTARY_X
1256 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
1257 if (!_elm_cnp_init_count) _elm_cnp_init();
1259 sel = selections + selection;
1260 top = elm_widget_top_get(widget);
1261 if (!top) return EINA_FALSE;
1263 sel->requestformat = format;
1264 sel->requestwidget = widget;
1265 sel->request(elm_win_xwindow_get(top), ECORE_X_SELECTION_TARGET_TARGETS);
1266 sel->datacb = datacb;
1275 #ifdef HAVE_ELEMENTARY_X
1282 if (_elm_cnp_init_count++) return EINA_TRUE;
1283 for (i = 0; i < CNP_N_ATOMS; i++)
1285 atoms[i].atom = ecore_x_atom_get(atoms[i].name);
1286 ecore_x_selection_converter_atom_add(atoms[i].atom,
1287 atoms[i].converter);
1289 clipboard_atom = ecore_x_atom_get("CLIPBOARD");
1291 ecore_event_handler_add(ECORE_X_EVENT_SELECTION_CLEAR, selection_clear, NULL);
1292 ecore_event_handler_add(ECORE_X_EVENT_SELECTION_NOTIFY, selection_notify, NULL);
1294 text_uri = eina_stringshare_add("text/uri-list");
1299 selection_clear(void *udata __UNUSED__, int type __UNUSED__, void *event)
1301 Ecore_X_Event_Selection_Clear *ev = event;
1305 for (i = 0; i < ELM_SEL_MAX; i++)
1307 if (selections[i].ecore_sel == ev->selection) break;
1309 cnp_debug("selection %d clear\n", i);
1310 /* Not me... Don't care */
1311 if (i == ELM_SEL_MAX) return ECORE_CALLBACK_PASS_ON;
1313 sel = selections + i;
1318 return ECORE_CALLBACK_PASS_ON;
1323 * Response to a selection notify:
1324 * - So we have asked for the selection list.
1325 * - If it's the targets list, parse it, and fire of what we want,
1326 * else it's the data we want.
1329 selection_notify(void *udata __UNUSED__, int type __UNUSED__, void *event)
1331 Ecore_X_Event_Selection_Notify *ev = event;
1335 cnp_debug("selection notify callback: %d\n",ev->selection);
1336 switch (ev->selection)
1338 case ECORE_X_SELECTION_CLIPBOARD:
1339 sel = selections + ELM_SEL_CLIPBOARD;
1341 case ECORE_X_SELECTION_PRIMARY:
1342 sel = selections + ELM_SEL_PRIMARY;
1344 case ECORE_X_SELECTION_SECONDARY:
1345 sel = selections + ELM_SEL_SECONDARY;
1347 case ECORE_X_SELECTION_XDND:
1348 sel = selections + ELM_SEL_XDND;
1351 return ECORE_CALLBACK_PASS_ON;
1353 cnp_debug("Target is %s\n", ev->target);
1355 for (i = 0; i < CNP_N_ATOMS; i++)
1357 if (!strcmp(ev->target, atoms[i].name))
1359 if (atoms[i].notify)
1361 cnp_debug("Found something: %s\n", atoms[i].name);
1362 atoms[i].notify(sel, ev);
1366 cnp_debug("Ignored: No handler!\n");
1371 return ECORE_CALLBACK_PASS_ON;
1377 targets_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize)
1383 if (!data_ret) return EINA_FALSE;
1384 if (!data || (*((unsigned int *)data) >= ELM_SEL_MAX))
1387 sel = selections + *((int *)data);
1389 for (i = 0, count = 0; i < CNP_N_ATOMS ; i++)
1391 if (sel->format & atoms[i].formats) count++;
1394 aret = malloc(sizeof(Ecore_X_Atom) * count);
1395 for (i = 0, count = 0; i < CNP_N_ATOMS; i++)
1397 if (sel->format & atoms[i].formats) aret[count ++] = atoms[i].atom;
1401 if (typesize) *typesize = 32 /* urk */;
1402 if (ttype) *ttype = ECORE_X_ATOM_ATOM;
1403 if (size_ret) *size_ret = count;
1409 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__)
1411 cnp_debug("Image converter called\n");
1416 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__)
1420 cnp_debug("Vcard send called\n");
1422 sel = selections + *((int *)data);
1424 if (data_ret) *data_ret = strdup(sel->selbuf);
1425 if (size_ret) *size_ret = strlen(sel->selbuf);
1431 is_uri_type_data(Cnp_Selection *sel __UNUSED__, Ecore_X_Event_Selection_Notify *notify)
1433 Ecore_X_Selection_Data *data;
1436 data = notify->data;
1437 cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
1438 if (data->content == ECORE_X_SELECTION_CONTENT_FILES) return EINA_TRUE;
1439 else p = (char *)data->data;
1441 if (!p) return EINA_TRUE;
1442 cnp_debug("Got %s\n", p);
1443 if (strncmp(p, "file://", 7))
1445 if (*p != '/') return EINA_FALSE;
1452 * Callback to handle a targets response on a selection request:
1453 * So pick the format we'd like; and then request it.
1456 notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1458 Ecore_X_Selection_Data_Targets *targets;
1459 Ecore_X_Atom *atomlist;
1463 targets = notify->data;
1464 atomlist = (Ecore_X_Atom *)(targets->data.data);
1466 for (j = 1; j < CNP_N_ATOMS; j++)
1468 cnp_debug("\t%s %d\n", atoms[j].name, atoms[j].atom);
1469 if (!(atoms[j].formats & sel->requestformat)) continue;
1470 for (i = 0; i < targets->data.length; i++)
1472 if ((atoms[j].atom == atomlist[i]) && (atoms[j].notify))
1475 if ((j == CNP_ATOM_text_uri) ||
1476 (j == CNP_ATOM_text_urilist))
1478 if(!is_uri_type_data(sel, notify)) continue;
1481 cnp_debug("Atom %s matches\n",atoms[j].name);
1487 cnp_debug("Couldn't find anything that matches\n");
1488 return ECORE_CALLBACK_PASS_ON;
1491 top = elm_widget_top_get(sel->requestwidget);
1492 if (!top) top = sel->requestwidget;
1493 cnp_debug("Sending request for %s\n", atoms[j].name);
1494 sel->request(elm_win_xwindow_get(top), atoms[j].name);
1496 return ECORE_CALLBACK_PASS_ON;
1500 response_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1502 Ecore_X_Selection_Data_Targets *targets;
1503 Ecore_X_Atom *atomlist;
1507 targets = notify->data;
1508 atomlist = (Ecore_X_Atom *)(targets->data.data);
1510 /* Start from 1: Skip targets */
1511 for (j = 1 ; j < CNP_N_ATOMS ; j ++)
1513 if (!(atoms[j].formats & sel->requestformat)) continue;
1514 for (i = 0 ; i < targets->data.length ; i ++)
1516 if ((atoms[j].atom == atomlist[i]) && (atoms[j].response))
1518 /* Found a match: Use it */
1524 if (j == CNP_N_ATOMS)
1526 cnp_debug("No matching type found\n");
1530 top = elm_widget_top_get(sel->requestwidget);
1533 sel->request(elm_win_xwindow_get(top), atoms[j].name);
1538 entry_insert_filter(Evas_Object* entry, char* str)
1543 char *insertStr = str;
1544 // if entry has text only set then remove item tags
1545 if (elm_entry_cnp_mode_get(entry) != ELM_CNP_MODE_MARKUP)
1549 char *startTag = NULL;
1550 char *endTag = NULL;
1552 startTag = strstr(insertStr, "<item");
1554 startTag = strstr(insertStr, "</item");
1556 endTag = strstr(startTag, ">");
1559 if (!endTag || startTag > endTag)
1561 cnp_debug("Broken tag: %s\n", str);
1565 size_t sindex = startTag - insertStr;
1566 size_t eindex = endTag - insertStr + 1;
1568 Eina_Strbuf *buf = eina_strbuf_new();
1571 eina_strbuf_append(buf, insertStr);
1572 eina_strbuf_remove(buf, sindex, eindex);
1573 insertStr = eina_strbuf_string_steal(buf);
1574 eina_strbuf_free(buf);
1578 cnp_debug("remove item tag: %s\n", insertStr);
1580 // if entry has single line set then remove <br> & <ps> tags
1581 if (elm_entry_single_line_get(entry))
1583 Eina_Strbuf *buf = eina_strbuf_new();
1586 eina_strbuf_append(buf, insertStr);
1587 eina_strbuf_replace_all(buf, "<br>", "");
1588 eina_strbuf_replace_all(buf, "<ps>", "");
1589 insertStr = eina_strbuf_string_steal(buf);
1590 eina_strbuf_free(buf);
1593 cnp_debug("remove break tag: %s\n", insertStr);
1595 elm_entry_entry_insert(entry, insertStr);
1597 if (insertStr != str)
1602 notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1604 Ecore_X_Selection_Data *data;
1607 data = notify->data;
1611 Elm_Selection_Data ddata;
1613 str = malloc(data->length);
1614 memcpy(str, data->data, data->length);
1615 ddata.x = ddata.y = 0;
1616 ddata.format = ELM_SEL_FORMAT_TEXT;
1618 ddata.len = data->length;
1619 sel->datacb(sel->udata, sel->widget, &ddata);
1624 cnp_debug("Notify handler text %d %d %p\n", data->format,data->length, data->data);
1625 str = mark_up((char *)data->data, data->length, NULL);
1626 cnp_debug("String is %s (from %s)\n", str, data->data);
1627 entry_insert_filter(sel->requestwidget, str);
1628 //elm_entry_entry_insert(sel->requestwidget, str);
1635 * So someone is pasting an image into my entry or widget...
1638 notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1640 Ecore_X_Selection_Data *data;
1641 Ecore_X_Selection_Data_Files *files;
1645 data = notify->data;
1646 cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
1647 if (data->content == ECORE_X_SELECTION_CONTENT_FILES)
1649 cnp_debug("got a files list\n");
1650 files = notify->data;
1651 if (files->num_files > 1)
1653 /* Don't handle many items */
1654 cnp_debug("more then one file: Bailing\n");
1657 stripstr = p = strdup(files->files[0]);
1661 stripstr = p = strndup((char *)data->data, data->length);
1666 cnp_debug("Couldn't find a file\n");
1669 cnp_debug("Got %s\n",p);
1672 Elm_Selection_Data ddata;
1674 ddata.x = ddata.y = 0;
1675 ddata.format = ELM_SEL_FORMAT_MARKUP;
1677 ddata.len = data->length;
1678 sel->datacb(sel->udata, sel->widget, &ddata);
1682 if (strncmp(p, "file://", 7))
1684 /* Try and continue if it looks sane */
1693 p += strlen("file://");
1696 if (savedtypes.pi) pasteimage_free(savedtypes.pi);
1697 pi = pasteimage_alloc(p, strlen(p));
1698 if (savedtypes.textreq)
1700 savedtypes.textreq = 0;
1705 pasteimage_append(pi, sel->requestwidget);
1706 savedtypes.pi = NULL;
1713 * Just receieved an vcard, either through cut and paste, or dnd.
1716 vcard_receive(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1720 Ecore_X_Selection_Data *data;
1722 data = notify->data;
1723 cnp_debug("vcard receive\n");
1725 if (sel == (selections + ELM_SEL_XDND))
1727 Elm_Selection_Data ddata;
1729 cnp_debug("drag & drop\n");
1730 /* FIXME: this needs to be generic: Used for all receives */
1731 EINA_LIST_FOREACH(drops, l, dropable)
1733 if (dropable->obj == sel->requestwidget) break;
1737 cnp_debug("Unable to find drop object");
1738 ecore_x_dnd_send_finished();
1741 dropable = eina_list_data_get(l);
1742 ddata.x = savedtypes.x;
1743 ddata.y = savedtypes.y;
1744 ddata.format = ELM_SEL_FORMAT_VCARD;
1745 ddata.data = data->data;
1746 ddata.len = data->length;
1747 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1748 ecore_x_dnd_send_finished();
1750 else if (sel->datacb)
1752 Elm_Selection_Data ddata;
1753 ddata.x = ddata.y = 0;
1754 ddata.format = ELM_SEL_FORMAT_IMAGE;
1755 ddata.data = data->data;
1756 ddata.len = data->length;
1757 sel->datacb(sel->udata, sel->widget, &ddata);
1761 cnp_debug("Paste request\n");
1770 notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1772 Ecore_X_Selection_Data *data;
1776 cnp_debug("got a png (or a jpeg)!\n");
1777 data = notify->data;
1779 cnp_debug("Size if %d\n", data->length);
1783 Elm_Selection_Data ddata;
1785 ddata.x = ddata.y = 0;
1786 ddata.format = ELM_SEL_FORMAT_IMAGE;
1787 ddata.data = data->data;
1788 ddata.len = data->length;
1789 sel->datacb(sel->udata, sel->widget, &ddata);
1793 /* generate tmp name */
1794 tmp = elm_cnp_tempfile_create(data->length);
1795 memcpy(tmp->map, data->data, data->length);
1796 munmap(tmp->map,data->length);
1798 /* FIXME: Add to paste image data to clean up */
1799 pi = pasteimage_alloc(tmp->filename, strlen(tmp->filename));
1800 pasteimage_append(pi, sel->requestwidget);
1807 notify_handler_edje(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1809 Ecore_X_Selection_Data *data;
1811 data = notify->data;
1813 char *stripstr = NULL;
1814 stripstr = malloc(sizeof(char) * (data->length + 1));
1815 strncpy(stripstr, (char *)data->data, data->length);
1816 stripstr[data->length] = '\0';
1820 Elm_Selection_Data ddata;
1821 ddata.x = ddata.y = 0;
1822 ddata.format = ELM_SEL_FORMAT_MARKUP;
1823 ddata.data = stripstr;
1824 ddata.len = data->length;
1825 sel->datacb(sel->udata, sel->widget, &ddata);
1828 entry_insert_filter(sel->requestwidget, stripstr);
1829 //elm_entry_entry_insert(sel->requestwidget, stripstr);
1831 cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
1837 * Warning: Generic text/html can';t handle it sanely.
1838 * Firefox sends ucs2 (i think).
1839 * chrome sends utf8... blerg
1842 notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1844 Ecore_X_Selection_Data *data;
1846 cnp_debug("Got some HTML: Checking encoding is useful\n");
1847 data = notify->data;
1849 char *stripstr = NULL;
1850 stripstr = malloc(sizeof(char) * (data->length + 1));
1851 strncpy(stripstr, (char *)data->data, data->length);
1852 stripstr[data->length] = '\0';
1856 Elm_Selection_Data ddata;
1857 ddata.x = ddata.y = 0;
1858 ddata.format = ELM_SEL_FORMAT_HTML;
1859 ddata.data = stripstr;
1860 ddata.len = data->length;
1861 sel->datacb(sel->udata, sel->widget, &ddata);
1864 entry_insert_filter(sel->requestwidget, stripstr);
1865 //elm_entry_entry_insert(sel->requestwidget, stripstr);
1867 cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
1874 text_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1878 cnp_debug("text converter\n");
1879 sel = selections + *((int *)data);
1880 if (!sel->active) return EINA_TRUE;
1882 if ((sel->format & ELM_SEL_FORMAT_MARKUP) ||
1883 (sel->format & ELM_SEL_FORMAT_HTML))
1885 *data_ret = remove_tags(sel->selbuf, size_ret);
1887 else if (sel->format & ELM_SEL_FORMAT_TEXT)
1889 *data_ret = strdup(sel->selbuf);
1890 *size_ret = strlen(sel->selbuf);
1892 else if (sel->format & ELM_SEL_FORMAT_IMAGE)
1894 cnp_debug("Image %s\n", evas_object_type_get(sel->widget));
1895 cnp_debug("Elm type: %s\n", elm_object_widget_type_get(sel->widget));
1896 evas_object_image_file_get(elm_photocam_internal_image_get(sel->widget), (const char **)data_ret, NULL);
1897 if (!*data_ret) *data_ret = strdup("No file");
1898 else *data_ret = strdup(*data_ret);
1899 *size_ret = strlen(*data_ret);
1905 edje_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1908 sel = selections + *((int *)data);
1909 /* if (data_ret) *data_ret = strdup(sel->selbuf);
1910 if (size_ret) *size_ret = strlen(sel->selbuf);*/
1913 if (data_ret && (sel->format == ELM_SEL_FORMAT_TEXT))
1915 if (sel->selbuf && sel->selbuf[0] != '\0')
1916 edje = mark_up(sel->selbuf, strlen(sel->selbuf), NULL);
1918 else if (data_ret && ((sel->format & ELM_SEL_FORMAT_HTML)))
1920 Eina_List *nodeList = NULL;
1924 nodeData = _get_start_node(sel->selbuf);
1928 nodeList = eina_list_append(nodeList, nodeData);
1929 nodeData = _get_next_node(nodeData);
1932 _link_match_tags(nodeList);
1934 _set_HTML_tag_data(nodeList);
1937 _dumpNode(nodeList);
1939 edje = _convert_to_edje(nodeList);
1941 cnp_debug("convert edje: %s\n", edje);
1943 EINA_LIST_FOREACH(nodeList, trail, nodeData)
1944 _delete_node(nodeData);
1945 eina_list_free(nodeList);
1954 *data_ret = strdup(sel->selbuf);
1956 *data_ret = strdup("");
1961 *size_ret = strlen(edje);
1964 *size_ret = strlen(sel->selbuf);
1973 html_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1977 sel = selections + *(int *)data;
1980 char *convert_target = sel->selbuf;
1981 Eina_Bool convert_edje = EINA_FALSE;
1983 if (data_ret && (sel->format == ELM_SEL_FORMAT_TEXT))
1985 if (sel->selbuf && sel->selbuf[0] != '\0')
1987 convert_target = mark_up(sel->selbuf, strlen(sel->selbuf), NULL);
1988 convert_edje = EINA_TRUE;
1992 if (data_ret && ((sel->format & ELM_SEL_FORMAT_MARKUP) || convert_edje))
1994 Eina_List *nodeList = NULL;
1998 nodeData = _get_start_node(convert_target);
2002 nodeList = eina_list_append(nodeList, nodeData);
2003 nodeData = _get_next_node(nodeData);
2006 _link_match_tags(nodeList);
2008 _set_EFL_tag_data(nodeList);
2011 _dumpNode(nodeList);
2013 html = _convert_to_html(nodeList);
2015 cnp_debug("convert html: %s\n", html);
2017 EINA_LIST_FOREACH(nodeList, trail, nodeData)
2018 _delete_node(nodeData);
2019 eina_list_free(nodeList);
2027 *data_ret = strdup(sel->selbuf);
2029 *data_ret = strdup("");
2035 *size_ret = strlen(html);
2038 *size_ret = strlen(sel->selbuf);
2042 if (convert_target != sel->selbuf)
2043 free(convert_target);
2049 uri_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
2052 sel = selections + *((int *)data);
2053 cnp_debug("Uri converter\n");
2054 if (data_ret) *data_ret = strdup(sel->selbuf);
2055 if (size_ret) *size_ret = strlen(sel->selbuf);
2060 * Image paste provide
2063 /* FIXME: Should add provider for each pasted item: Use data to store it
2065 static Evas_Object *
2066 image_provider(void *images __UNUSED__, Evas_Object *entry, const char *item)
2071 cnp_debug("image provider for %s called\n", item);
2072 EINA_LIST_FOREACH(pastedimages, l, pi)
2074 cnp_debug("is it %s?\n",pi->tag);
2075 if (!strcmp(pi->tag, item))
2079 o = evas_object_image_filled_add(evas_object_evas_get(entry));
2080 /* FIXME: Handle eets */
2081 cnp_debug("file is %s (object is %p)\n", pi->file, o);
2082 evas_object_image_file_set(o, pi->file, NULL);
2083 evas_object_show(o);
2091 static Paste_Image *
2092 pasteimage_alloc(const char *file, int pathlen)
2096 char *buf, *filebuf;
2097 int prefixlen = strlen("file://");
2099 pi = calloc(1, sizeof(Paste_Image));
2100 if (!pi) return NULL;
2102 len = snprintf(NULL, 0, "pasteimage-%p", pi);
2110 snprintf(buf, len, "pasteimage-%p", pi);
2115 if (strstr(file,"file://")) file += prefixlen;
2116 filebuf = alloca(pathlen + 1);
2117 strncpy(filebuf, file, pathlen);
2118 filebuf[pathlen] = 0;
2119 pi->file = strdup(filebuf);
2126 pasteimage_free(Paste_Image *pi)
2129 if (pi->file) free((void*)pi->file);
2130 if (pi->tag) free((void*)pi->tag);
2135 pasteimage_provider_set(Evas_Object *entry)
2140 if (!entry) return EINA_FALSE;
2141 type = elm_widget_type_get(entry);
2142 cnp_debug("type is %s\n", type);
2143 if ((!type) || (strcmp(type, "entry"))) return EINA_FALSE;
2145 v = evas_object_data_get(entry, PROVIDER_SET);
2148 evas_object_data_set(entry, PROVIDER_SET, pasteimage_provider_set);
2149 elm_entry_item_provider_append(entry, image_provider, NULL);
2150 evas_object_event_callback_add(entry, EVAS_CALLBACK_FREE,
2151 entry_deleted, NULL);
2158 pasteimage_append(Paste_Image *pi, Evas_Object *entry)
2162 static const char *tagstring = "<item absize=240x180 href=file://%s></item>";
2164 if (!pi) return EINA_FALSE;
2165 if (!entry) return EINA_FALSE;
2166 if (elm_entry_cnp_mode_get(entry) != ELM_CNP_MODE_MARKUP) return EINA_FALSE;
2168 pasteimage_provider_set(entry);
2170 len = strlen(tagstring)+strlen(pi->file);
2172 pastedimages = eina_list_append(pastedimages, pi);
2173 entrytag = alloca(len + 1);
2174 snprintf(entrytag, len + 1, tagstring, pi->file);
2175 elm_entry_entry_insert(entry, entrytag);
2181 entry_deleted(void *images __UNUSED__, Evas *e __UNUSED__, Evas_Object *entry, void *unused __UNUSED__)
2186 EINA_LIST_FOREACH_SAFE(pastedimages, l, next, pi)
2188 if (pi->entry == entry)
2189 pastedimages = eina_list_remove_list(pastedimages, l);
2195 remove_tags(const char *p, int *len)
2199 if (!p) return NULL;
2201 q = malloc(strlen(p) + 1);
2202 if (!q) return NULL;
2207 if ((*p != '<') && (*p != '&')) *q++ = *p++;
2210 if ((p[1] == 'b') && (p[2] == 'r') &&
2211 ((p[3] == ' ') || (p[3] == '/') || (p[3] == '>')))
2213 while ((*p) && (*p != '>')) p++;
2219 for (i = 0 ; i < N_ESCAPES ; i++)
2221 if (!strncmp(p,escapes[i].escape, strlen(escapes[i].escape)))
2223 p += strlen(escapes[i].escape);
2224 *q = escapes[i].value;
2229 if (i == N_ESCAPES) *q ++= '&';
2233 if (len) *len = q - ret;
2239 mark_up(const char *start, int inlen, int *lenp)
2244 const char *endp = NULL;
2246 if (!start) return NULL;
2247 if (inlen >= 0) endp = start + inlen;
2248 /* First pass: Count characters */
2249 for (l = 0, p = start; ((!endp) || (p < endp)) && (*p); p++)
2251 for (i = 0 ; i < N_ESCAPES ; i ++)
2253 if (*p == escapes[i].value)
2255 if (!iscntrl(escapes[i].value)) l++;
2256 l += strlen(escapes[i].escape);
2260 if (i == N_ESCAPES) l++;
2263 q = ret = malloc(l + 1);
2265 /* Second pass: Change characters */
2266 for (p = start; ((!endp) || (p < endp)) && (*p); )
2268 for (i = 0; i < N_ESCAPES; i++)
2270 if (*p == escapes[i].value)
2272 if (!iscntrl(escapes[i].value)) *q++ = '&';
2273 strcpy(q, escapes[i].escape);
2274 q += strlen(escapes[i].escape);
2279 if (i == N_ESCAPES) *q++ = *p++;
2283 if (lenp) *lenp = l;
2289 _dnd_enter(void *data __UNUSED__, int etype __UNUSED__, void *ev)
2291 Ecore_X_Event_Xdnd_Enter *enter = ev;
2295 if ((!enter) || (!enter->num_types) || (!enter->types)) return EINA_TRUE;
2297 cnp_debug("Types\n");
2298 savedtypes.ntypes = enter->num_types;
2299 if (savedtypes.types) free(savedtypes.types);
2300 savedtypes.types = malloc(sizeof(char *) * enter->num_types);
2301 if (!savedtypes.types) return EINA_FALSE;
2303 for (i = 0; i < enter->num_types; i++)
2305 savedtypes.types[i] = eina_stringshare_add(enter->types[i]);
2306 cnp_debug("Type is %s %p %p\n", enter->types[i],
2307 savedtypes.types[i],text_uri);
2308 if (savedtypes.types[i] == text_uri)
2310 /* Request it, so we know what it is */
2311 cnp_debug("Sending uri request\n");
2312 savedtypes.textreq = 1;
2313 if (savedtypes.pi) pasteimage_free(savedtypes.pi);
2314 savedtypes.pi = NULL; /* FIXME: Free? */
2315 ecore_x_selection_xdnd_request(enter->win, text_uri);
2319 /* FIXME: Find an object and make it current */
2324 _dnd_drop(void *data __UNUSED__, int etype __UNUSED__, void *ev)
2326 struct _Ecore_X_Event_Xdnd_Drop *drop;
2330 Ecore_X_Window xwin;
2331 Elm_Selection_Data ddata;
2337 // check we still have something to drop
2338 if (!drops) return EINA_TRUE;
2340 /* Find any widget in our window; then work out geometry rel to our window */
2341 for (l = drops; l; l = l->next)
2344 xwin = (Ecore_X_Window)ecore_evas_window_get
2345 (ecore_evas_ecore_evas_get(evas_object_evas_get
2347 if (xwin == drop->win) break;
2349 /* didn't find a window */
2350 if (!l) return EINA_TRUE;
2352 /* Calculate real (widget relative) position */
2353 // - window position
2354 // - widget position
2355 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(dropable->obj));
2356 ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
2357 savedtypes.x = drop->position.x - x;
2358 savedtypes.y = drop->position.y - y;
2360 cnp_debug("Drop position is %d,%d\n", savedtypes.x, savedtypes.y);
2362 for (; l; l = l->next)
2365 evas_object_geometry_get(dropable->obj, &x, &y, &w, &h);
2366 if ((savedtypes.x >= x) && (savedtypes.y >= y) &&
2367 (savedtypes.x < x + w) && (savedtypes.y < y + h))
2371 if (!l) return EINA_TRUE; /* didn't find one */
2373 evas_object_geometry_get(dropable->obj, &x, &y, NULL, NULL);
2377 /* Find our type from the previous list */
2378 for (i = 0; i < CNP_N_ATOMS; i++)
2380 for (j = 0; j < savedtypes.ntypes; j++)
2382 if (!strcmp(savedtypes.types[j], atoms[i].name)) goto found;
2386 cnp_debug("Didn't find a target\n");
2390 cnp_debug("Found a target we'd like: %s\n", atoms[i].name);
2391 cnp_debug("0x%x\n",xwin);
2393 if (i == CNP_ATOM_text_urilist)
2395 cnp_debug("We found a URI... (%scached) %s\n",
2396 savedtypes.pi ? "" : "not ",
2397 savedtypes.pi->file);
2401 static const char *tagstring = "<item absize=240x180 href="
2402 "file://%s></item>";
2403 ddata.x = savedtypes.x;
2404 ddata.y = savedtypes.y;
2406 /* If it's markup that also supports images */
2407 if ((dropable->types & ELM_SEL_FORMAT_MARKUP) &&
2408 (dropable->types & ELM_SEL_FORMAT_IMAGE))
2411 ddata.format = ELM_SEL_FORMAT_MARKUP;
2412 pasteimage_provider_set(dropable->obj);
2414 pastedimages = eina_list_append(pastedimages, savedtypes.pi);
2415 len = strlen(tagstring) + strlen(savedtypes.pi->file);
2416 entrytag = alloca(len + 1);
2417 snprintf(entrytag, len + 1, tagstring, savedtypes.pi->file);
2418 ddata.data = entrytag;
2419 cnp_debug("Insert %s\n", (char *)ddata.data);
2420 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
2421 ecore_x_dnd_send_finished();
2423 if (savedtypes.pi) pasteimage_free(savedtypes.pi);
2424 savedtypes.pi = NULL;
2427 else if (dropable->types & ELM_SEL_FORMAT_IMAGE)
2429 cnp_debug("Doing image insert (%s)\n", savedtypes.pi->file);
2430 ddata.format = ELM_SEL_FORMAT_IMAGE;
2431 ddata.data = (char *)savedtypes.pi->file;
2432 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
2433 ecore_x_dnd_send_finished();
2435 if (savedtypes.pi) pasteimage_free(savedtypes.pi);
2436 savedtypes.pi = NULL;
2442 cnp_debug("Item doesn't support images... passing\n");
2443 pasteimage_free(savedtypes.pi);
2447 else if (savedtypes.textreq)
2449 /* Already asked: Pretend we asked now, and paste immediately when
2451 savedtypes.textreq = 0;
2452 ecore_x_dnd_send_finished();
2457 cnp_debug("doing a request then\n");
2458 selections[ELM_SEL_XDND].requestwidget = dropable->obj;
2459 selections[ELM_SEL_XDND].requestformat = ELM_SEL_FORMAT_MARKUP;
2460 selections[ELM_SEL_XDND].active = EINA_TRUE;
2462 ecore_x_selection_xdnd_request(xwin, atoms[i].name);
2467 _dnd_position(void *data __UNUSED__, int etype __UNUSED__, void *ev)
2469 struct _Ecore_X_Event_Xdnd_Position *pos;
2470 Ecore_X_Rectangle rect;
2474 /* Need to send a status back */
2475 /* FIXME: Should check I can drop here */
2476 /* FIXME: Should highlight widget */
2477 rect.x = pos->position.x - 5;
2478 rect.y = pos->position.y - 5;
2481 ecore_x_dnd_send_status(EINA_TRUE, EINA_FALSE, rect, pos->action);
2487 * When dragging this is callback response from the destination.
2488 * The important thing we care about: Can we drop; thus update cursor
2492 _dnd_status(void *data __UNUSED__, int etype __UNUSED__, void *ev)
2494 struct _Ecore_X_Event_Xdnd_Status *status = ev;
2496 if (!status) return EINA_TRUE;
2498 /* Only thing we care about: will accept */
2499 if (status->will_accept)
2501 cnp_debug("Will accept\n");
2504 { /* Won't accept */
2505 cnp_debug("Won't accept accept\n");
2511 * Add a widget as drop target.
2514 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
2517 Ecore_X_Window xwin;
2521 if (!obj) return EINA_FALSE;
2522 if (!_elm_cnp_init_count) _elm_cnp_init();
2524 /* Is this the first? */
2525 first = (!drops) ? 1 : 0;
2527 EINA_LIST_FOREACH(drops, item, drop)
2529 if (drop->obj == obj)
2531 /* Update: Not a new one */
2532 drop->dropcb = dropcb;
2533 drop->cbdata = cbdata;
2534 drop->types = format;
2539 /* Create new drop */
2540 drop = calloc(1, sizeof(Dropable));
2541 if (!drop) return EINA_FALSE;
2542 /* FIXME: Check for eina's deranged error method */
2543 drops = eina_list_append(drops, drop);
2545 if (!drops/* || or other error */)
2550 drop->dropcb = dropcb;
2551 drop->cbdata = cbdata;
2552 drop->types = format;
2555 evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL,
2556 /* I love C and varargs */
2557 (Evas_Object_Event_Cb)elm_drop_target_del,
2559 /* FIXME: Handle resizes */
2561 /* If not the first: We're done */
2562 if (!first) return EINA_TRUE;
2564 xwin = (Ecore_X_Window)ecore_evas_window_get
2565 (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
2567 ecore_x_dnd_aware_set(xwin, EINA_TRUE);
2569 cnp_debug("Adding drop target calls\n");
2570 handler_enter = ecore_event_handler_add(ECORE_X_EVENT_XDND_ENTER,
2572 handler_pos = ecore_event_handler_add(ECORE_X_EVENT_XDND_POSITION,
2573 _dnd_position, NULL);
2574 handler_drop = ecore_event_handler_add(ECORE_X_EVENT_XDND_DROP,
2581 elm_drop_target_del(Evas_Object *obj)
2583 Dropable *drop,*del;
2585 Ecore_X_Window xwin;
2588 EINA_LIST_FOREACH(drops, item, drop)
2590 if (drop->obj == obj)
2592 drops = eina_list_remove_list(drops, item);
2597 if (!del) return EINA_FALSE;
2599 evas_object_event_callback_del(obj, EVAS_CALLBACK_FREE,
2600 (Evas_Object_Event_Cb)elm_drop_target_del);
2602 /* If still drops there: All fine.. continue */
2603 if (drops) return EINA_TRUE;
2605 cnp_debug("Disabling DND\n");
2606 xwin = (Ecore_X_Window)ecore_evas_window_get
2607 (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
2608 ecore_x_dnd_aware_set(xwin, EINA_FALSE);
2610 ecore_event_handler_del(handler_pos);
2611 ecore_event_handler_del(handler_drop);
2612 ecore_event_handler_del(handler_enter);
2616 pasteimage_free(savedtypes.pi);
2617 savedtypes.pi = NULL;
2625 _drag_mouse_up(void *un __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *data __UNUSED__)
2627 evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_UP, _drag_mouse_up);
2631 dragdonecb(dragdonecb,selections[ELM_SEL_XDND].widget);
2636 evas_object_del(dragwin);
2642 _drag_move(void *data __UNUSED__, Ecore_X_Xdnd_Position *pos)
2644 evas_object_move(dragwin,
2645 pos->position.x - _dragx,
2646 pos->position.y - _dragy);
2651 elm_drag_start(Evas_Object *obj, Elm_Sel_Format format, const char *data, void (*dragdone) (void *data, Evas_Object *), void *donecbdata)
2653 Ecore_X_Window xwin;
2655 Elm_Sel_Type xdnd = ELM_SEL_XDND;
2657 int x, y, x2, y2, x3, y3;
2661 if (!_elm_cnp_init_count) _elm_cnp_init();
2663 xwin = elm_win_xwindow_get(obj);
2665 cnp_debug("starting drag...\n");
2667 ecore_x_dnd_type_set(xwin, "text/uri-list", 1);
2668 sel = selections + ELM_SEL_XDND;
2671 sel->format = format;
2672 sel->selbuf = data ? strdup(data) : NULL;
2673 dragdonecb = dragdone;
2674 dragdonedata = donecbdata;
2676 ecore_x_dnd_callback_pos_update_set(_drag_move, NULL);
2677 ecore_x_dnd_begin(xwin, (unsigned char *)&xdnd, sizeof(Elm_Sel_Type));
2678 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
2679 _drag_mouse_up, NULL);
2681 handler_status = ecore_event_handler_add(ECORE_X_EVENT_XDND_STATUS,
2684 dragwin = elm_win_add(NULL, "Elm Drag Object", ELM_WIN_UTILITY);
2685 elm_win_override_set(dragwin, 1);
2687 /* FIXME: Images only */
2688 icon = elm_icon_add(dragwin);
2689 elm_icon_file_set(icon, data + 7, NULL); /* 7!? "file://" */
2690 elm_win_resize_object_add(dragwin,icon);
2691 evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
2692 evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, EVAS_HINT_FILL);
2694 /* Position subwindow appropriately */
2695 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(obj));
2696 ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
2697 evas_object_geometry_get(obj, &x2, &y2, &w, &h);
2700 evas_object_move(dragwin, x, y);
2701 evas_object_resize(icon, w, h);
2702 evas_object_resize(dragwin, w, h);
2704 evas_object_show(icon);
2705 evas_object_show(dragwin);
2707 evas_pointer_canvas_xy_get(evas_object_evas_get(obj), &x3, &y3);
2715 elm_cnp_tempfile_create(int size)
2718 const char *tmppath;
2721 info = malloc(sizeof(Tmp_Info));
2722 if (!info) return NULL;
2724 tmppath = getenv("TMP");
2725 if (!tmppath) tmppath = P_tmpdir;
2726 len = snprintf(NULL, 0, "%s/%sXXXXXX", tmppath, "elmcnpitem-");
2733 info->filename = malloc(len);
2734 if (!info->filename)
2739 snprintf(info->filename,len,"%s/%sXXXXXX", tmppath, "elmcnpitem-");
2741 info->fd = mkstemp(info->filename);
2746 /* And before someone says anything see POSIX 1003.1-2008 page 400 */
2749 pid = (long)getpid();
2750 /* Use pid instead of /proc/self: That way if can be passed around */
2751 len = snprintf(NULL,0,"/proc/%li/fd/%i", pid, info->fd);
2756 snprintf(tmp,len, "/proc/%li/fd/%i", pid, info->fd);
2757 unlink(info->filename);
2758 free(info->filename);
2759 info->filename = tmp;
2764 cnp_debug("filename is %s\n", info->filename);
2767 /* Set map to NULL and return */
2774 if (ftruncate(info->fd, size))
2776 perror("ftruncate");
2782 eina_mmap_safety_enabled_set(EINA_TRUE);
2784 info->map = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, info->fd, 0);
2785 if (info->map == MAP_FAILED)
2797 tmpinfo_free(Tmp_Info *info)
2799 if (!info) return 0;
2800 free(info->filename);
2806 /* Stubs for windows */
2808 elm_drag_start(Evas_Object *o, Elm_Sel_Format f, const char *d, void (*donecb)(void *, Evas_Object *),void *cbdata)
2814 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
2820 elm_drop_target_del(Evas_Object *o)
2826 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-2f0^-2{2(0W1st0 :*/