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);
167 /* Optimisation: Turn this into a 256 byte table:
168 * then can lookup in one index, not N checks */
169 static const Escape escapes[] = {
179 #define N_ESCAPES ((int)(sizeof(escapes) / sizeof(escapes[0])))
181 static Cnp_Atom atoms[CNP_N_ATOMS] = {
182 [CNP_ATOM_TARGETS] = {
184 (Elm_Sel_Format) -1, // everything
186 response_handler_targets,
187 notify_handler_targets,
191 "application/x-elementary-markup",
192 ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
198 [CNP_ATOM_text_uri] = {
200 ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_IMAGE, /* Either images or entries */
206 [CNP_ATOM_text_urilist] = {
208 ELM_SEL_FORMAT_IMAGE,
214 [CNP_ATOM_text_x_vcard] = {
216 ELM_SEL_FORMAT_VCARD,
220 [CNP_ATOM_image_png] = {
222 ELM_SEL_FORMAT_IMAGE,
225 notify_handler_image,
228 [CNP_ATOM_image_jpeg] = {
230 ELM_SEL_FORMAT_IMAGE,
233 notify_handler_image,/* Raw image data is the same */
236 [CNP_ATOM_image_bmp] = {
238 ELM_SEL_FORMAT_IMAGE,
241 notify_handler_image,/* Raw image data is the same */
244 [CNP_ATOM_image_gif] = {
246 ELM_SEL_FORMAT_IMAGE,
249 notify_handler_image,/* Raw image data is the same */
252 [CNP_ATOM_image_tiff] = {
254 ELM_SEL_FORMAT_IMAGE,
257 notify_handler_image,/* Raw image data is the same */
260 [CNP_ATOM_image_svg] = {
262 ELM_SEL_FORMAT_IMAGE,
265 notify_handler_image,/* Raw image data is the same */
268 [CNP_ATOM_image_xpm] = {
270 ELM_SEL_FORMAT_IMAGE,
273 notify_handler_image,/* Raw image data is the same */
276 [CNP_ATOM_image_tga] = {
278 ELM_SEL_FORMAT_IMAGE,
281 notify_handler_image,/* Raw image data is the same */
284 [CNP_ATOM_image_ppm] = {
285 "image/x-portable-pixmap",
286 ELM_SEL_FORMAT_IMAGE,
289 notify_handler_image,/* Raw image data is the same */
292 [CNP_ATOM_text_html_utf8] = {
293 "text/html;charset=utf-8",
300 [CNP_ATOM_text_html] = {
305 notify_handler_html, /* No encoding: Webkit only */
308 [CNP_ATOM_UTF8STRING] = {
310 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
316 [CNP_ATOM_STRING] = {
318 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
326 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
332 [CNP_ATOM_text_plain_utf8] = {
333 "text/plain;charset=utf-8",
334 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
340 [CNP_ATOM_text_plain] = {
342 ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
350 static Cnp_Selection selections[ELM_SEL_MAX] = {
351 ARRAYINIT(ELM_SEL_PRIMARY) {
353 .ecore_sel = ECORE_X_SELECTION_PRIMARY,
354 .set = ecore_x_selection_primary_set,
355 .clear = ecore_x_selection_primary_clear,
356 .request = ecore_x_selection_primary_request,
358 ARRAYINIT(ELM_SEL_SECONDARY) {
359 .debug = "Secondary",
360 .ecore_sel = ECORE_X_SELECTION_SECONDARY,
361 .set = ecore_x_selection_secondary_set,
362 .clear = ecore_x_selection_secondary_clear,
363 .request = ecore_x_selection_secondary_request,
365 ARRAYINIT(ELM_SEL_CLIPBOARD) {
366 .debug = "Clipboard",
367 .ecore_sel = ECORE_X_SELECTION_CLIPBOARD,
368 .set = ecore_x_selection_clipboard_set,
369 .clear = ecore_x_selection_clipboard_clear,
370 .request = ecore_x_selection_clipboard_request,
372 ARRAYINIT(ELM_SEL_XDND) {
374 .ecore_sel = ECORE_X_SELECTION_XDND,
375 .request = ecore_x_selection_xdnd_request,
379 /* Data for DND in progress */
380 static Saved_Type savedtypes = { NULL, NULL, 0, 0, 0, EINA_FALSE };
382 static void (*dragdonecb) (void *data, Evas_Object *obj) = NULL;
383 static void *dragdonedata = NULL;
385 static int _elm_cnp_init_count = 0;
386 /* FIXME: who left this out of XAtoms.h */
387 static Ecore_X_Atom clipboard_atom;
389 static Eina_List *pastedimages = NULL;
392 * Drag & Drop functions
395 /* FIXME: Way too many globals */
396 static Eina_List *drops = NULL;
397 static Evas_Object *dragwin = NULL;
398 static int _dragx = 0, _dragy = 0;
399 static Ecore_Event_Handler *handler_pos = NULL;
400 static Ecore_Event_Handler *handler_drop = NULL;
401 static Ecore_Event_Handler *handler_enter = NULL;
402 static Ecore_Event_Handler *handler_status = NULL;
406 /* Stringshared, so I can just compare pointers later */
407 static const char *text_uri;
409 /* For convert EFL to HTML */
411 #define TAGPOS_START 0x00000001
412 #define TAGPOS_END 0x00000002
413 #define TAGPOS_ALONE 0x00000003
415 /* TEXTBLOCK tag using stack but close tag word has no mean maybe bug...
416 * TEXTBLOCK <b>bold<font>font</b>bold</font>
417 * HTML <b>bold<font>font bold</b>font</font> */
419 typedef struct _TagTable {
425 TagTable _EFLtoHTMLConvertTable[] = {
427 {"underline", "del", 0},
428 {"strikethrough", "ins", 0},
435 TagTable _HTMLtoEFLConvertTable[] = {
437 {"del", "underline", 0},
438 {"u", "underline", 0},
439 {"ins", "strikethrough", 0},
440 {"s", "strikethrough", 0},
448 typedef struct _TagNode TagNode, *PTagNode;
450 char *tag; //EINA_STRINGSHARE if NULL just str
453 char *pos_in_ori_str;
456 unsigned char tagPosType;
459 typedef struct _FontTagData FontTagData, *PFontTagData;
460 struct _FontTagData {
468 typedef struct _ItemTagData ItemTagData, *PItemTagData;
469 struct _ItemTagData {
475 #define SAFEFREE(ptr) \
483 #define freeAndAssign(dst, value) \
494 static PTagNode _new_tag_node(char *tag, char *tag_str, char* str, char *pos_in_ori_str);
495 static PTagNode _get_start_node(char *str);
496 static PTagNode _get_next_node(PTagNode prev);
497 static void _delete_node(PTagNode node);
498 static void _link_match_tags(Eina_List *nodes);
499 static char *_get_tag_value(const char *tag_str, const char *tag_name);
500 static char *_convert_to_html(Eina_List* nodes);
501 static void _set_EFL_tag_data(Eina_List* nodes);
502 static char *_convert_to_edje(Eina_List* nodes);
503 static void _set_HTML_tag_data(Eina_List* nodes);
504 static PFontTagData _set_EFL_font_data(PFontTagData data, const char *tag_str);
505 static PItemTagData _set_EFL_item_data(PItemTagData data, const char *tag_str);
506 static PFontTagData _set_HTML_font_data(PFontTagData data, const char *tag_str);
507 static PItemTagData _set_HTML_img_data(PItemTagData data, const char *tag_str);
510 static void _dumpNode(Eina_List* nodes);
514 _new_tag_node(char *tag, char *tag_str, char* str, char *pos_in_ori_str)
516 PTagNode newNode = calloc(1, sizeof(TagNode));
518 eina_str_tolower(&tag);
521 eina_str_tolower(&tag_str);
522 newNode->tag_str = tag_str;
524 newNode->pos_in_ori_str = pos_in_ori_str;
529 _get_start_node(char *str)
531 char *startStr = NULL;
532 if (!str || str[0] == '\0')
537 char *tagStart = strchr(str, '<');
539 startStr = strdup(str);
542 int strLength = tagStart - str;
543 startStr = malloc(sizeof(char) * (strLength + 1));
544 strncpy(startStr, str, strLength);
545 startStr[strLength] = '\0';
549 return _new_tag_node(NULL, NULL, startStr, str);
553 _get_next_node(PTagNode prev)
555 PTagNode retTag = NULL;
558 char *tagNameEnd = NULL;
561 if (prev->tag == NULL)
562 tagStart = strchr(prev->pos_in_ori_str, '<');
564 tagStart = strchr(prev->pos_in_ori_str + 1, '<');
569 tagEnd = strchr(tagStart, '>');
570 nextTagStart = strchr(tagStart + 1, '<');
572 if (!tagEnd || (nextTagStart && (nextTagStart < tagEnd)))
573 return _get_start_node(tagStart + 1);
576 char *spArray[spCnt];
577 spArray[0] = strchr(tagStart, '=');
578 spArray[1] = strchr(tagStart, '_');
579 spArray[2] = strchr(tagStart, ' ');
580 spArray[3] = strchr(tagStart, '\t');
581 spArray[4] = strchr(tagStart, '\n');
585 for (i = 0; i < spCnt; i++)
587 if (spArray[i] && spArray[i] < tagNameEnd)
588 tagNameEnd = spArray[i];
591 int tagLength = tagNameEnd - tagStart - 1;
592 char *tagName = NULL;
593 if (!strncmp(&tagStart[1], "color", tagLength))
594 tagName = strndup("font", 4);
595 else if (!strncmp(&tagStart[1], "/color", tagLength))
596 tagName = strndup("/font", 5);
597 else if (!strncmp(&tagStart[1], "/item", tagLength))
598 tagName = strdup("");
600 tagName = strndup(&tagStart[1], tagLength);
602 int tagStrLength = 0;
606 tagStrLength = tagEnd - tagStart + 1;
607 tagStr = strndup(tagStart, tagStrLength);
610 unsigned int strLength = nextTagStart ? (unsigned int)(nextTagStart - tagEnd - 1) : strlen(&tagEnd[1]);
611 char *str = strndup(&tagEnd[1], strLength);
613 retTag = _new_tag_node(tagName, tagStr, str, tagStart);
619 _delete_node(PTagNode node)
623 SAFEFREE(node->tag_str);
630 if (!strcmp("font", node->tag))
632 PFontTagData data = node->tagData;
633 SAFEFREE(data->name);
634 SAFEFREE(data->color);
635 SAFEFREE(data->size);
636 SAFEFREE(data->bg_color);
638 if (!strcmp("item", node->tag))
640 PItemTagData data = node->tagData;
641 SAFEFREE(data->href);
642 SAFEFREE(data->width);
643 SAFEFREE(data->height);
647 SAFEFREE(node->tagData);
655 _link_match_tags(Eina_List *nodes)
657 Eina_List *stack = NULL;
659 PTagNode trail, popData;
662 EINA_LIST_FOREACH(nodes, l, trail)
664 if (!trail->tag || trail->tag[0] == '\0')
666 if (!strcmp("br", trail->tag))
668 trail->tagPosType = TAGPOS_ALONE;
671 else if (!strcmp("item", trail->tag) || !strcmp("img", trail->tag))
673 trail->tagPosType = TAGPOS_ALONE;
677 if (trail->tag[0] != '/') // PUSH
679 stack = eina_list_append(stack, trail);
680 /* eina_array_push(stack, trail);
681 cnp_debug("stack: %d, tag %s\n", eina_array_count_get(stack), trail->tag);*/
682 cnp_debug("stack: %d, tag %s\n", eina_list_count(stack), trail->tag);
686 if (!eina_list_count(stack))
688 cnp_debug("tag not matched %s\n", trail->tag);
692 EINA_LIST_REVERSE_FOREACH(stack, r, popData)
694 if (popData->tag && !strcmp(popData->tag, &trail->tag[1]))
696 popData->tagPosType = TAGPOS_START;
697 trail->tagPosType = TAGPOS_END;
698 popData->matchTag = trail;
699 trail->matchTag = popData;
700 stack = eina_list_remove_list(stack, r);
704 /* popData = eina_array_pop(stack);
706 popData->tagPosType = TAGPOS_START;
707 trail->tagPosType = TAGPOS_END;
708 popData->matchTag = trail;
709 trail->matchTag = popData;
710 cnp_debug("pop stack: %d, tag %s\n", eina_array_count_get(stack), trail->tag);
715 /* if (eina_array_count_get(stack))
716 cnp_debug("stack state: %d, tag %s\n", eina_array_count_get(stack), trail->tag);*/
718 /* Make Dummy close tag */
719 /* while ((popData = eina_array_pop(stack))) */
721 EINA_LIST_REVERSE_FOREACH(stack, r, popData)
724 int tagLength = strlen(popData->tag);
725 char *tagName = malloc(sizeof(char) * (tagLength + 2));
729 strcat(tagName, popData->tag);
731 newData = _new_tag_node(tagName, NULL, NULL, NULL);
732 popData->tagPosType = TAGPOS_START;
733 newData->tagPosType = TAGPOS_END;
734 popData->matchTag = newData;
735 newData->matchTag = popData;
736 nodes = eina_list_append(nodes, newData);
737 /* cnp_debug("stack: %d, tag %s\n", eina_array_count_get(stack), popData->tag);*/
739 /* cnp_debug("stack_top: %d\n", eina_array_count_get(stack));
740 eina_array_free(stack);*/
741 eina_list_free(stack);
745 _get_tag_value(const char *tag_str, const char *tag_name)
747 if (!tag_name || !tag_str)
751 if ((tag = strstr(tag_str, tag_name)))
753 if (tag[strlen(tag_name)] == '_')
755 char *value = strchr(tag, '=');
761 } while (!isalnum(*value) && *value != '#');
764 char *spArray[spCnt];
765 spArray[0] = strchr(value, ' ');
766 spArray[1] = strchr(value, '>');
767 spArray[2] = strchr(value, '\"');
768 spArray[3] = strchr(value, '\'');
769 spArray[4] = strchr(value, '\t');
770 spArray[5] = strchr(value, '\n');
771 char *valueEnd = strchr(value, '\0');
774 for (i = 0; i < spCnt; i++)
776 if (spArray[i] && spArray[i] < valueEnd)
777 valueEnd = spArray[i];
780 int valueLength = valueEnd - value;
781 return strndup(value, valueLength);
788 _set_EFL_font_data(PFontTagData data, const char *tag_str)
793 data = calloc(1, sizeof(FontTagData));
794 value = _get_tag_value(tag_str, "font_size");
795 freeAndAssign(data->size, value);
796 value = _get_tag_value(tag_str, "color");
797 freeAndAssign(data->color, value);
798 value = _get_tag_value(tag_str, "bgcolor");
799 freeAndAssign(data->bg_color, value);
800 value = _get_tag_value(tag_str, "font");
801 freeAndAssign(data->name, value);
807 _set_EFL_item_data(PItemTagData data, const char *tag_str)
812 data = calloc(1, sizeof(ItemTagData));
813 value = _get_tag_value(tag_str, "href");
816 char *path = strstr(value, "file://");
819 char *modify = malloc(sizeof(char) * (strlen(value) + 1));
820 strncpy(modify, "file://", 7);
823 while (path[1] && path[0] && path[1] == '/' && path[0] == '/')
827 strcat(modify, path);
829 cnp_debug("image href ---%s---\n", data->href);
833 freeAndAssign(data->href, value);
836 value = _get_tag_value(tag_str, "absize");
839 char *xpos = strchr(value, 'x');
842 int absizeLen = strlen(value);
843 freeAndAssign(data->width, strndup(value, xpos - value));
844 freeAndAssign(data->height, strndup(xpos + 1, absizeLen - (xpos - value) - 1));
845 cnp_debug("image width: -%s-, height: -%s-\n", data->width, data->height);
853 _set_EFL_tag_data(Eina_List* nodes)
858 EINA_LIST_FOREACH(nodes, l, trail)
862 if (!strcmp("font", trail->tag))
863 trail->tagData = _set_EFL_font_data(trail->tagData, trail->tag_str);
864 else if (!strcmp("item", trail->tag))
865 trail->tagData = _set_EFL_item_data(trail->tagData, trail->tag_str);
870 _set_HTML_font_data(PFontTagData data, const char *tag_str)
875 data = calloc(1, sizeof(FontTagData));
876 value = _get_tag_value(tag_str, "size");
877 freeAndAssign(data->size, value);
878 value = _get_tag_value(tag_str, "color");
879 freeAndAssign(data->color, value);
880 value = _get_tag_value(tag_str, "bgcolor");
881 freeAndAssign(data->bg_color, value);
882 value = _get_tag_value(tag_str, "face");
883 freeAndAssign(data->name, value);
889 _set_HTML_img_data(PItemTagData data, const char *tag_str)
894 data = calloc(1, sizeof(ItemTagData));
895 value = _get_tag_value(tag_str, "src");
898 char *path = strstr(value, "file://");
901 char *modify = malloc(sizeof(char) * (strlen(value) + 1));
902 strncpy(modify, "file://", 7);
905 while (path[1] && path[0] && path[1] == '/' && path[0] == '/')
909 strcat(modify, path);
911 cnp_debug("image src ---%s---\n", data->href);
915 freeAndAssign(data->href, value);
918 value = _get_tag_value(tag_str, "width");
919 freeAndAssign(data->width, value);
920 value = _get_tag_value(tag_str, "height");
921 freeAndAssign(data->height, value);
926 _set_HTML_tag_data(Eina_List* nodes)
931 EINA_LIST_FOREACH(nodes, l, trail)
935 if (!strcmp("font", trail->tag))
936 trail->tagData = _set_HTML_font_data(trail->tagData, trail->tag_str);
937 else if (!strcmp("img", trail->tag))
938 trail->tagData = _set_HTML_img_data(trail->tagData, trail->tag_str);
944 _dumpNode(Eina_List* nodes)
949 EINA_LIST_FOREACH(nodes, l, trail)
951 cnp_debug("tag: %s, tag_str: %s, str: %s, tagPosType: %d\n",
952 trail->tag, trail->tag_str, trail->str, trail->tagPosType);
953 cnp_debug("matchTag: %x ", (unsigned int)trail->matchTag);
955 cnp_debug("matchTag->tag_str: %s", trail->matchTag->tag_str);
958 if (!strcmp(trail->tag, "font"))
960 PFontTagData data = trail->tagData;
961 cnp_debug(" tagData->name: %s, tagData->color: %s, tagData->size: %s, tagData->bg_color: %s",
962 data->name, data->color, data->size, data->bg_color);
964 else if (!strcmp(trail->tag, "item") || !strcmp(trail->tag, "img"))
966 PItemTagData data = trail->tagData;
967 cnp_debug(" tagData->href: %s, tagData->width: %s, tagData->height: %s",
968 data->href, data->width, data->height);
971 cnp_debug("\nERROR!!!! not need tagData");
979 _convert_to_html(Eina_List* nodes)
984 Eina_Strbuf *html = eina_strbuf_new();
986 int tableCnt = sizeof(_EFLtoHTMLConvertTable) / sizeof(TagTable);
988 EINA_LIST_FOREACH(nodes, l, trail)
992 char *tagName = trail->tagPosType == TAGPOS_END ?
993 trail->matchTag->tag : trail->tag;
995 for(j = 0; j < tableCnt; j++)
997 if (!strcmp(_EFLtoHTMLConvertTable[j].src, tagName))
999 switch(trail->tagPosType)
1002 eina_strbuf_append(html, "</");
1005 eina_strbuf_append(html, "<");
1009 eina_strbuf_append(html, _EFLtoHTMLConvertTable[j].dst);
1010 if (trail->tagPosType != TAGPOS_END)
1012 if (!strcmp(_EFLtoHTMLConvertTable[j].src, "font"))
1014 PFontTagData data = trail->tagData;
1019 eina_strbuf_append_printf(html, " color=\"%s\"", data->color);
1021 eina_strbuf_append_printf(html, " size=\"%s\"", data->size);
1026 else if (!strcmp(_EFLtoHTMLConvertTable[j].src, "item"))
1028 PItemTagData data = trail->tagData;
1030 eina_strbuf_append_printf(html, " src=\"%s\"", data->href);
1032 eina_strbuf_append_printf(html, " width=\"%s\"", data->width);
1034 eina_strbuf_append_printf(html, " height=\"%s\"", data->height);
1037 switch(trail->tagPosType)
1040 eina_strbuf_append(html, " />");
1043 eina_strbuf_append(html, ">");
1051 eina_strbuf_append(html, trail->str);
1054 char *ret = eina_strbuf_string_steal(html);
1055 eina_strbuf_free(html);
1059 #define IMAGE_DEFAULT_WIDTH "240"
1060 #define IMAGE_DEFAULT_HEIGHT "180"
1064 _convert_to_edje(Eina_List* nodes)
1069 Eina_Strbuf *html = eina_strbuf_new();
1071 int tableCnt = sizeof(_HTMLtoEFLConvertTable) / sizeof(TagTable);
1073 EINA_LIST_FOREACH(nodes, l, trail)
1077 char *tagName = trail->tagPosType == TAGPOS_END ?
1078 trail->matchTag->tag : trail->tag;
1080 for(j = 0; j < tableCnt; j++)
1082 if (!strcmp(_HTMLtoEFLConvertTable[j].src, tagName))
1084 if (_HTMLtoEFLConvertTable[j].dst[0] != '\0')
1086 switch(trail->tagPosType)
1089 eina_strbuf_append(html, "</");
1092 eina_strbuf_append(html, "<");
1096 eina_strbuf_append(html, _HTMLtoEFLConvertTable[j].dst);
1098 if (trail->tagPosType != TAGPOS_END)
1100 if (!strcmp(_HTMLtoEFLConvertTable[j].src, "font"))
1102 PFontTagData data = trail->tagData;
1107 eina_strbuf_append_printf(html, "<color=%s>", data->color);
1109 eina_strbuf_append_printf(html, "<font_size=%s>", data->size);
1115 else if (!strcmp(_HTMLtoEFLConvertTable[j].src, "img"))
1117 PItemTagData data = trail->tagData;
1118 char *width = IMAGE_DEFAULT_WIDTH, *height = IMAGE_DEFAULT_HEIGHT;
1120 width = data->width;
1122 height = data->height;
1123 eina_strbuf_append_printf(html, " absize=%sx%s", width, height);
1125 eina_strbuf_append_printf(html, " href=%s></item>", data->href);
1131 if (_HTMLtoEFLConvertTable[j].dst[0] == '\0')
1133 if (!strcmp(_HTMLtoEFLConvertTable[j].src, "font"))
1135 if (trail->matchTag->tagData)
1137 PFontTagData data = trail->matchTag->tagData;
1142 eina_strbuf_append_printf(html, "</color>");
1144 eina_strbuf_append_printf(html, "</font>");
1153 switch(trail->tagPosType)
1155 /* not support in efl
1157 eina_strbuf_append(html, " />");
1161 eina_strbuf_append(html, ">");
1166 }/* for(j = 0; j < tableCnt; j++) end */
1169 eina_strbuf_append(html, trail->str);
1172 char *ret = eina_strbuf_string_steal(html);
1173 eina_strbuf_free(html);
1179 elm_selection_set(Elm_Sel_Type selection, Evas_Object *widget, Elm_Sel_Format format, const char *selbuf)
1181 #ifdef HAVE_ELEMENTARY_X
1182 Evas_Object *top = elm_widget_top_get(widget);
1183 Ecore_X_Window xwin;
1186 if (top) xwin = elm_win_xwindow_get(top);
1187 else xwin = elm_win_xwindow_get(widget);
1188 if (!xwin) return EINA_FALSE;
1189 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
1190 if (!_elm_cnp_init_count) _elm_cnp_init();
1191 if ((!selbuf) && (format != ELM_SEL_FORMAT_IMAGE))
1192 return elm_selection_clear(selection, widget);
1194 sel = selections + selection;
1197 sel->widget = widget;
1199 sel->set(xwin, &selection, sizeof(Elm_Sel_Type));
1200 sel->format = format;
1201 sel->selbuf = selbuf ? strdup(selbuf) : NULL;
1210 elm_selection_clear(Elm_Sel_Type selection, Evas_Object *widget)
1212 #ifdef HAVE_ELEMENTARY_X
1215 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
1216 if (!_elm_cnp_init_count) _elm_cnp_init();
1218 sel = selections + selection;
1220 /* No longer this selection: Consider it gone! */
1221 if ((!sel->active) || (sel->widget != widget)) return EINA_TRUE;
1234 elm_selection_get(Elm_Sel_Type selection, Elm_Sel_Format format,
1235 Evas_Object *widget, Elm_Drop_Cb datacb, void *udata)
1237 #ifdef HAVE_ELEMENTARY_X
1241 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
1242 if (!_elm_cnp_init_count) _elm_cnp_init();
1244 sel = selections + selection;
1245 top = elm_widget_top_get(widget);
1246 if (!top) return EINA_FALSE;
1248 sel->requestformat = format;
1249 sel->requestwidget = widget;
1250 sel->request(elm_win_xwindow_get(top), ECORE_X_SELECTION_TARGET_TARGETS);
1251 sel->datacb = datacb;
1260 #ifdef HAVE_ELEMENTARY_X
1267 if (_elm_cnp_init_count++) return EINA_TRUE;
1268 for (i = 0; i < CNP_N_ATOMS; i++)
1270 atoms[i].atom = ecore_x_atom_get(atoms[i].name);
1271 ecore_x_selection_converter_atom_add(atoms[i].atom,
1272 atoms[i].converter);
1274 clipboard_atom = ecore_x_atom_get("CLIPBOARD");
1276 ecore_event_handler_add(ECORE_X_EVENT_SELECTION_CLEAR, selection_clear, NULL);
1277 ecore_event_handler_add(ECORE_X_EVENT_SELECTION_NOTIFY, selection_notify, NULL);
1279 text_uri = eina_stringshare_add("text/uri-list");
1284 selection_clear(void *udata __UNUSED__, int type __UNUSED__, void *event)
1286 Ecore_X_Event_Selection_Clear *ev = event;
1290 for (i = 0; i < ELM_SEL_MAX; i++)
1292 if (selections[i].ecore_sel == ev->selection) break;
1294 cnp_debug("selection %d clear\n", i);
1295 /* Not me... Don't care */
1296 if (i == ELM_SEL_MAX) return ECORE_CALLBACK_PASS_ON;
1298 sel = selections + i;
1303 return ECORE_CALLBACK_PASS_ON;
1308 * Response to a selection notify:
1309 * - So we have asked for the selection list.
1310 * - If it's the targets list, parse it, and fire of what we want,
1311 * else it's the data we want.
1314 selection_notify(void *udata __UNUSED__, int type __UNUSED__, void *event)
1316 Ecore_X_Event_Selection_Notify *ev = event;
1320 cnp_debug("selection notify callback: %d\n",ev->selection);
1321 switch (ev->selection)
1323 case ECORE_X_SELECTION_CLIPBOARD:
1324 sel = selections + ELM_SEL_CLIPBOARD;
1326 case ECORE_X_SELECTION_PRIMARY:
1327 sel = selections + ELM_SEL_PRIMARY;
1329 case ECORE_X_SELECTION_SECONDARY:
1330 sel = selections + ELM_SEL_SECONDARY;
1332 case ECORE_X_SELECTION_XDND:
1333 sel = selections + ELM_SEL_XDND;
1336 return ECORE_CALLBACK_PASS_ON;
1338 cnp_debug("Target is %s\n", ev->target);
1340 for (i = 0; i < CNP_N_ATOMS; i++)
1342 if (!strcmp(ev->target, atoms[i].name))
1344 if (atoms[i].notify)
1346 cnp_debug("Found something: %s\n", atoms[i].name);
1347 atoms[i].notify(sel, ev);
1351 cnp_debug("Ignored: No handler!\n");
1356 return ECORE_CALLBACK_PASS_ON;
1362 targets_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize)
1368 if (!data_ret) return EINA_FALSE;
1370 sel = selections + *((int *)data);
1372 for (i = 0, count = 0; i < CNP_N_ATOMS ; i++)
1374 if (sel->format & atoms[i].formats) count++;
1377 aret = malloc(sizeof(Ecore_X_Atom) * count);
1378 for (i = 0, count = 0; i < CNP_N_ATOMS; i++)
1380 if (sel->format & atoms[i].formats) aret[count ++] = atoms[i].atom;
1384 if (typesize) *typesize = 32 /* urk */;
1385 if (ttype) *ttype = ECORE_X_ATOM_ATOM;
1386 if (size_ret) *size_ret = count;
1392 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__)
1394 cnp_debug("Image converter called\n");
1399 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__)
1403 cnp_debug("Vcard send called\n");
1405 sel = selections + *((int *)data);
1407 if (data_ret) *data_ret = strdup(sel->selbuf);
1408 if (size_ret) *size_ret = strlen(sel->selbuf);
1414 is_uri_type_data(Cnp_Selection *sel __UNUSED__, Ecore_X_Event_Selection_Notify *notify)
1416 Ecore_X_Selection_Data *data;
1419 data = notify->data;
1420 cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
1421 if (data->content == ECORE_X_SELECTION_CONTENT_FILES) return EINA_TRUE;
1422 else p = (char *)data->data;
1424 if (!p) return EINA_TRUE;
1425 cnp_debug("Got %s\n", p);
1426 if (strncmp(p, "file://", 7))
1428 /* elm_selection_set send target notify->data just "x" */
1429 if (*p == 'x') return EINA_TRUE;
1430 if (*p != '/') return EINA_FALSE;
1437 * Callback to handle a targets response on a selection request:
1438 * So pick the format we'd like; and then request it.
1441 notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1443 Ecore_X_Selection_Data_Targets *targets;
1444 Ecore_X_Atom *atomlist;
1448 targets = notify->data;
1449 atomlist = (Ecore_X_Atom *)(targets->data.data);
1451 for (j = 1; j < CNP_N_ATOMS; j++)
1453 cnp_debug("\t%s %d\n", atoms[j].name, atoms[j].atom);
1454 if (!(atoms[j].formats & sel->requestformat)) continue;
1455 for (i = 0; i < targets->data.length; i++)
1457 if ((atoms[j].atom == atomlist[i]) && (atoms[j].notify))
1459 if ((j == CNP_ATOM_text_uri) ||
1460 (j == CNP_ATOM_text_urilist))
1462 if(!is_uri_type_data(sel, notify)) continue;
1464 cnp_debug("Atom %s matches\n",atoms[j].name);
1470 cnp_debug("Couldn't find anything that matches\n");
1471 return ECORE_CALLBACK_PASS_ON;
1474 top = elm_widget_top_get(sel->requestwidget);
1475 if (!top) top = sel->requestwidget;
1476 cnp_debug("Sending request for %s\n", atoms[j].name);
1477 sel->request(elm_win_xwindow_get(top), atoms[j].name);
1479 return ECORE_CALLBACK_PASS_ON;
1483 response_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1485 Ecore_X_Selection_Data_Targets *targets;
1486 Ecore_X_Atom *atomlist;
1490 targets = notify->data;
1491 atomlist = (Ecore_X_Atom *)(targets->data.data);
1493 /* Start from 1: Skip targets */
1494 for (j = 1 ; j < CNP_N_ATOMS ; j ++)
1496 if (!(atoms[j].formats & sel->requestformat)) continue;
1497 for (i = 0 ; i < targets->data.length ; i ++)
1499 if ((atoms[j].atom == atomlist[i]) && (atoms[j].response))
1501 /* Found a match: Use it */
1507 if (j == CNP_N_ATOMS)
1509 cnp_debug("No matching type found\n");
1513 top = elm_widget_top_get(sel->requestwidget);
1516 sel->request(elm_win_xwindow_get(top), atoms[j].name);
1522 notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1524 Ecore_X_Selection_Data *data;
1527 data = notify->data;
1531 Elm_Selection_Data ddata;
1533 str = mark_up((char *)data->data, data->length, NULL);
1534 ddata.x = ddata.y = 0;
1535 ddata.format = ELM_SEL_FORMAT_TEXT;
1537 ddata.len = data->length;
1538 sel->datacb(sel->udata, sel->widget, &ddata);
1543 cnp_debug("Notify handler text %d %d %p\n", data->format,data->length, data->data);
1544 str = mark_up((char *)data->data, data->length, NULL);
1545 cnp_debug("String is %s (from %s)\n", str, data->data);
1546 elm_entry_entry_insert(sel->requestwidget, str);
1553 * So someone is pasting an image into my entry or widget...
1556 notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1558 Ecore_X_Selection_Data *data;
1559 Ecore_X_Selection_Data_Files *files;
1563 data = notify->data;
1564 cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
1565 if (data->content == ECORE_X_SELECTION_CONTENT_FILES)
1567 cnp_debug("got a files list\n");
1568 files = notify->data;
1569 if (files->num_files > 1)
1571 /* Don't handle many items */
1572 cnp_debug("more then one file: Bailing\n");
1575 p = files->files[0];
1579 p = (char *)data->data;
1584 cnp_debug("Couldn't find a file\n");
1587 cnp_debug("Got %s\n",p);
1590 Elm_Selection_Data ddata;
1592 ddata.x = ddata.y = 0;
1593 ddata.format = ELM_SEL_FORMAT_MARKUP;
1595 ddata.len = data->length;
1596 sel->datacb(sel->udata, sel->widget, &ddata);
1599 if (strncmp(p, "file://", 7))
1601 /* Try and continue if it looks sane */
1602 if (*p != '/') return 0;
1606 p += strlen("file://");
1609 if (savedtypes.pi) pasteimage_free(savedtypes.pi);
1610 pi = pasteimage_alloc(p, strlen(p));
1611 if (savedtypes.textreq)
1613 savedtypes.textreq = 0;
1618 pasteimage_append(pi, sel->requestwidget);
1619 savedtypes.pi = NULL;
1625 * Just receieved an vcard, either through cut and paste, or dnd.
1628 vcard_receive(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1632 Ecore_X_Selection_Data *data;
1634 data = notify->data;
1635 cnp_debug("vcard receive\n");
1637 if (sel == (selections + ELM_SEL_XDND))
1639 Elm_Selection_Data ddata;
1641 cnp_debug("drag & drop\n");
1642 /* FIXME: this needs to be generic: Used for all receives */
1643 EINA_LIST_FOREACH(drops, l, dropable)
1645 if (dropable->obj == sel->requestwidget) break;
1649 cnp_debug("Unable to find drop object");
1650 ecore_x_dnd_send_finished();
1653 dropable = eina_list_data_get(l);
1654 ddata.x = savedtypes.x;
1655 ddata.y = savedtypes.y;
1656 ddata.format = ELM_SEL_FORMAT_VCARD;
1657 ddata.data = data->data;
1658 ddata.len = data->length;
1659 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1660 ecore_x_dnd_send_finished();
1662 else if (sel->datacb)
1664 Elm_Selection_Data ddata;
1665 ddata.x = ddata.y = 0;
1666 ddata.format = ELM_SEL_FORMAT_IMAGE;
1667 ddata.data = data->data;
1668 ddata.len = data->length;
1669 sel->datacb(sel->udata, sel->widget, &ddata);
1673 cnp_debug("Paste request\n");
1682 notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1684 Ecore_X_Selection_Data *data;
1688 cnp_debug("got a png (or a jpeg)!\n");
1689 data = notify->data;
1691 cnp_debug("Size if %d\n", data->length);
1695 Elm_Selection_Data ddata;
1697 ddata.x = ddata.y = 0;
1698 ddata.format = ELM_SEL_FORMAT_IMAGE;
1699 ddata.data = data->data;
1700 ddata.len = data->length;
1701 sel->datacb(sel->udata, sel->widget, &ddata);
1705 /* generate tmp name */
1706 tmp = elm_cnp_tempfile_create(data->length);
1707 memcpy(tmp->map, data->data, data->length);
1708 munmap(tmp->map,data->length);
1710 /* FIXME: Add to paste image data to clean up */
1711 pi = pasteimage_alloc(tmp->filename, strlen(tmp->filename));
1712 pasteimage_append(pi, sel->requestwidget);
1719 notify_handler_edje(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1721 Ecore_X_Selection_Data *data;
1723 data = notify->data;
1725 char *stripstr = NULL;
1726 stripstr = malloc(sizeof(char) * (data->length + 1));
1727 strncpy(stripstr, (char *)data->data, data->length);
1728 stripstr[data->length] = '\0';
1732 Elm_Selection_Data ddata;
1733 ddata.x = ddata.y = 0;
1734 ddata.format = ELM_SEL_FORMAT_MARKUP;
1735 ddata.data = stripstr;
1736 ddata.len = data->length;
1737 sel->datacb(sel->udata, sel->widget, &ddata);
1740 elm_entry_entry_insert(sel->requestwidget, stripstr);
1742 cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
1748 * Warning: Generic text/html can';t handle it sanely.
1749 * Firefox sends ucs2 (i think).
1750 * chrome sends utf8... blerg
1753 notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1755 Ecore_X_Selection_Data *data;
1757 cnp_debug("Got some HTML: Checking encoding is useful\n");
1758 data = notify->data;
1760 char *stripstr = NULL;
1761 stripstr = malloc(sizeof(char) * (data->length + 1));
1762 strncpy(stripstr, (char *)data->data, data->length);
1763 stripstr[data->length] = '\0';
1767 Elm_Selection_Data ddata;
1768 ddata.x = ddata.y = 0;
1769 ddata.format = ELM_SEL_FORMAT_HTML;
1770 ddata.data = stripstr;
1771 ddata.len = data->length;
1772 sel->datacb(sel->udata, sel->widget, &ddata);
1775 elm_entry_entry_insert(sel->requestwidget, stripstr);
1777 cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
1784 text_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1788 cnp_debug("text converter\n");
1789 sel = selections + *((int *)data);
1790 if (!sel->active) return EINA_TRUE;
1792 if ((sel->format & ELM_SEL_FORMAT_MARKUP) ||
1793 (sel->format & ELM_SEL_FORMAT_HTML))
1795 *data_ret = remove_tags(sel->selbuf, size_ret);
1797 else if (sel->format & ELM_SEL_FORMAT_TEXT)
1799 *data_ret = strdup(sel->selbuf);
1800 *size_ret = strlen(sel->selbuf);
1802 else if (sel->format & ELM_SEL_FORMAT_IMAGE)
1804 cnp_debug("Image %s\n", evas_object_type_get(sel->widget));
1805 cnp_debug("Elm type: %s\n", elm_object_widget_type_get(sel->widget));
1806 evas_object_image_file_get(elm_photocam_internal_image_get(sel->widget), (const char **)data_ret, NULL);
1807 if (!*data_ret) *data_ret = strdup("No file");
1808 else *data_ret = strdup(*data_ret);
1809 *size_ret = strlen(*data_ret);
1815 edje_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1818 sel = selections + *((int *)data);
1819 /* if (data_ret) *data_ret = strdup(sel->selbuf);
1820 if (size_ret) *size_ret = strlen(sel->selbuf);*/
1823 if (data_ret && (sel->format & ELM_SEL_FORMAT_HTML))
1825 Eina_List *nodeList = NULL;
1829 nodeData = _get_start_node(sel->selbuf);
1833 nodeList = eina_list_append(nodeList, nodeData);
1834 nodeData = _get_next_node(nodeData);
1837 _link_match_tags(nodeList);
1839 _set_HTML_tag_data(nodeList);
1842 _dumpNode(nodeList);
1844 edje = _convert_to_edje(nodeList);
1846 cnp_debug("convert edje: %s\n", edje);
1848 EINA_LIST_FOREACH(nodeList, trail, nodeData)
1849 _delete_node(nodeData);
1850 eina_list_free(nodeList);
1858 *data_ret = strdup(sel->selbuf);
1863 *size_ret = strlen(edje);
1865 *size_ret = strlen(sel->selbuf);
1872 html_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1876 sel = selections + *(int *)data;
1879 if (data_ret && (sel->format & ELM_SEL_FORMAT_MARKUP))
1881 Eina_List *nodeList = NULL;
1885 nodeData = _get_start_node(sel->selbuf);
1889 nodeList = eina_list_append(nodeList, nodeData);
1890 nodeData = _get_next_node(nodeData);
1893 _link_match_tags(nodeList);
1895 _set_EFL_tag_data(nodeList);
1898 _dumpNode(nodeList);
1900 html = _convert_to_html(nodeList);
1902 cnp_debug("convert html: %s\n", html);
1904 EINA_LIST_FOREACH(nodeList, trail, nodeData)
1905 _delete_node(nodeData);
1906 eina_list_free(nodeList);
1913 *data_ret = strdup(sel->selbuf);
1919 *size_ret = strlen(html);
1921 *size_ret = strlen(sel->selbuf);
1928 uri_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1931 sel = selections + *((int *)data);
1932 cnp_debug("Uri converter\n");
1933 if (data_ret) *data_ret = strdup(sel->selbuf);
1934 if (size_ret) *size_ret = strlen(sel->selbuf);
1939 * Image paste provide
1942 /* FIXME: Should add provider for each pasted item: Use data to store it
1944 static Evas_Object *
1945 image_provider(void *images __UNUSED__, Evas_Object *entry, const char *item)
1950 cnp_debug("image provider for %s called\n", item);
1951 EINA_LIST_FOREACH(pastedimages, l, pi)
1953 cnp_debug("is it %s?\n",pi->tag);
1954 if (!strcmp(pi->tag, item))
1958 o = evas_object_image_filled_add(evas_object_evas_get(entry));
1959 /* FIXME: Handle eets */
1960 cnp_debug("file is %s (object is %p)\n", pi->file, o);
1961 evas_object_image_file_set(o, pi->file, NULL);
1962 evas_object_show(o);
1970 static Paste_Image *
1971 pasteimage_alloc(const char *file, int pathlen)
1975 char *buf, *filebuf;
1976 int prefixlen = strlen("file://");
1978 pi = calloc(1, sizeof(Paste_Image));
1979 if (!pi) return NULL;
1981 len = snprintf(NULL, 0, "pasteimage-%p", pi);
1989 snprintf(buf, len, "pasteimage-%p", pi);
1994 if (strstr(file,"file://")) file += prefixlen;
1995 filebuf = alloca(pathlen + 1);
1996 strncpy(filebuf, file, pathlen);
1997 filebuf[pathlen] = 0;
1998 pi->file = strdup(filebuf);
2005 pasteimage_free(Paste_Image *pi)
2008 if (pi->file) free((void*)pi->file);
2009 if (pi->tag) free((void*)pi->tag);
2014 pasteimage_provider_set(Evas_Object *entry)
2019 if (!entry) return EINA_FALSE;
2020 type = elm_widget_type_get(entry);
2021 cnp_debug("type is %s\n", type);
2022 if ((!type) || (strcmp(type, "entry"))) return EINA_FALSE;
2024 v = evas_object_data_get(entry, PROVIDER_SET);
2027 evas_object_data_set(entry, PROVIDER_SET, pasteimage_provider_set);
2028 elm_entry_item_provider_append(entry, image_provider, NULL);
2029 evas_object_event_callback_add(entry, EVAS_CALLBACK_FREE,
2030 entry_deleted, NULL);
2037 pasteimage_append(Paste_Image *pi, Evas_Object *entry)
2041 static const char *tagstring = "<item absize=240x180 href=file://%s></item>";
2043 if (!pi) return EINA_FALSE;
2044 if (!entry) return EINA_FALSE;
2046 pasteimage_provider_set(entry);
2048 len = strlen(tagstring)+strlen(pi->file);
2050 pastedimages = eina_list_append(pastedimages, pi);
2051 entrytag = alloca(len + 1);
2052 snprintf(entrytag, len + 1, tagstring, pi->file);
2053 elm_entry_entry_insert(entry, entrytag);
2059 entry_deleted(void *images __UNUSED__, Evas *e __UNUSED__, Evas_Object *entry, void *unused __UNUSED__)
2064 EINA_LIST_FOREACH_SAFE(pastedimages, l, next, pi)
2066 if (pi->entry == entry)
2067 pastedimages = eina_list_remove_list(pastedimages, l);
2073 remove_tags(const char *p, int *len)
2077 if (!p) return NULL;
2079 q = malloc(strlen(p) + 1);
2080 if (!q) return NULL;
2085 if ((*p != '<') && (*p != '&')) *q++ = *p++;
2088 if ((p[1] == 'b') && (p[2] == 'r') &&
2089 ((p[3] == ' ') || (p[3] == '/') || (p[3] == '>')))
2091 while ((*p) && (*p != '>')) p++;
2097 for (i = 0 ; i < N_ESCAPES ; i++)
2099 if (!strncmp(p,escapes[i].escape, strlen(escapes[i].escape)))
2101 p += strlen(escapes[i].escape);
2102 *q = escapes[i].value;
2107 if (i == N_ESCAPES) *q ++= '&';
2111 if (len) *len = q - ret;
2117 mark_up(const char *start, int inlen, int *lenp)
2122 const char *endp = NULL;
2124 if (!start) return NULL;
2125 if (inlen >= 0) endp = start + inlen;
2126 /* First pass: Count characters */
2127 for (l = 0, p = start; ((!endp) || (p < endp)) && (*p); p++)
2129 for (i = 0 ; i < N_ESCAPES ; i ++)
2131 if (*p == escapes[i].value)
2133 if (!iscntrl(escapes[i].value)) l++;
2134 l += strlen(escapes[i].escape);
2138 if (i == N_ESCAPES) l++;
2141 q = ret = malloc(l + 1);
2143 /* Second pass: Change characters */
2144 for (p = start; ((!endp) || (p < endp)) && (*p); )
2146 for (i = 0; i < N_ESCAPES; i++)
2148 if (*p == escapes[i].value)
2150 if (!iscntrl(escapes[i].value)) *q++ = '&';
2151 strcpy(q, escapes[i].escape);
2152 q += strlen(escapes[i].escape);
2157 if (i == N_ESCAPES) *q++ = *p++;
2161 if (lenp) *lenp = l;
2167 _dnd_enter(void *data __UNUSED__, int etype __UNUSED__, void *ev)
2169 Ecore_X_Event_Xdnd_Enter *enter = ev;
2173 if ((!enter) || (!enter->num_types) || (!enter->types)) return EINA_TRUE;
2175 cnp_debug("Types\n");
2176 savedtypes.ntypes = enter->num_types;
2177 if (savedtypes.types) free(savedtypes.types);
2178 savedtypes.types = malloc(sizeof(char *) * enter->num_types);
2179 if (!savedtypes.types) return EINA_FALSE;
2181 for (i = 0; i < enter->num_types; i++)
2183 savedtypes.types[i] = eina_stringshare_add(enter->types[i]);
2184 cnp_debug("Type is %s %p %p\n", enter->types[i],
2185 savedtypes.types[i],text_uri);
2186 if (savedtypes.types[i] == text_uri)
2188 /* Request it, so we know what it is */
2189 cnp_debug("Sending uri request\n");
2190 savedtypes.textreq = 1;
2191 if (savedtypes.pi) pasteimage_free(savedtypes.pi);
2192 savedtypes.pi = NULL; /* FIXME: Free? */
2193 ecore_x_selection_xdnd_request(enter->win, text_uri);
2197 /* FIXME: Find an object and make it current */
2202 _dnd_drop(void *data __UNUSED__, int etype __UNUSED__, void *ev)
2204 struct _Ecore_X_Event_Xdnd_Drop *drop;
2208 Ecore_X_Window xwin;
2209 Elm_Selection_Data ddata;
2215 // check we still have something to drop
2216 if (!drops) return EINA_TRUE;
2218 /* Find any widget in our window; then work out geometry rel to our window */
2219 for (l = drops; l; l = l->next)
2222 xwin = (Ecore_X_Window)ecore_evas_window_get
2223 (ecore_evas_ecore_evas_get(evas_object_evas_get
2225 if (xwin == drop->win) break;
2227 /* didn't find a window */
2228 if (!l) return EINA_TRUE;
2230 /* Calculate real (widget relative) position */
2231 // - window position
2232 // - widget position
2233 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(dropable->obj));
2234 ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
2235 savedtypes.x = drop->position.x - x;
2236 savedtypes.y = drop->position.y - y;
2238 cnp_debug("Drop position is %d,%d\n", savedtypes.x, savedtypes.y);
2240 for (; l; l = l->next)
2243 evas_object_geometry_get(dropable->obj, &x, &y, &w, &h);
2244 if ((savedtypes.x >= x) && (savedtypes.y >= y) &&
2245 (savedtypes.x < x + w) && (savedtypes.y < y + h))
2249 if (!l) return EINA_TRUE; /* didn't find one */
2251 evas_object_geometry_get(dropable->obj, &x, &y, NULL, NULL);
2255 /* Find our type from the previous list */
2256 for (i = 0; i < CNP_N_ATOMS; i++)
2258 for (j = 0; j < savedtypes.ntypes; j++)
2260 if (!strcmp(savedtypes.types[j], atoms[i].name)) goto found;
2264 cnp_debug("Didn't find a target\n");
2268 cnp_debug("Found a target we'd like: %s\n", atoms[i].name);
2269 cnp_debug("0x%x\n",xwin);
2271 if (i == CNP_ATOM_text_urilist)
2273 cnp_debug("We found a URI... (%scached) %s\n",
2274 savedtypes.pi ? "" : "not ",
2275 savedtypes.pi->file);
2279 static const char *tagstring = "<item absize=240x180 href="
2280 "file://%s></item>";
2281 ddata.x = savedtypes.x;
2282 ddata.y = savedtypes.y;
2284 /* If it's markup that also supports images */
2285 if ((dropable->types & ELM_SEL_FORMAT_MARKUP) &&
2286 (dropable->types & ELM_SEL_FORMAT_IMAGE))
2289 ddata.format = ELM_SEL_FORMAT_MARKUP;
2290 pasteimage_provider_set(dropable->obj);
2292 pastedimages = eina_list_append(pastedimages, savedtypes.pi);
2293 len = strlen(tagstring) + strlen(savedtypes.pi->file);
2294 entrytag = alloca(len + 1);
2295 snprintf(entrytag, len + 1, tagstring, savedtypes.pi->file);
2296 ddata.data = entrytag;
2297 cnp_debug("Insert %s\n", (char *)ddata.data);
2298 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
2299 ecore_x_dnd_send_finished();
2301 if (savedtypes.pi) pasteimage_free(savedtypes.pi);
2302 savedtypes.pi = NULL;
2305 else if (dropable->types & ELM_SEL_FORMAT_IMAGE)
2307 cnp_debug("Doing image insert (%s)\n", savedtypes.pi->file);
2308 ddata.format = ELM_SEL_FORMAT_IMAGE;
2309 ddata.data = (char *)savedtypes.pi->file;
2310 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
2311 ecore_x_dnd_send_finished();
2313 if (savedtypes.pi) pasteimage_free(savedtypes.pi);
2314 savedtypes.pi = NULL;
2320 cnp_debug("Item doesn't support images... passing\n");
2321 pasteimage_free(savedtypes.pi);
2325 else if (savedtypes.textreq)
2327 /* Already asked: Pretend we asked now, and paste immediately when
2329 savedtypes.textreq = 0;
2330 ecore_x_dnd_send_finished();
2335 cnp_debug("doing a request then\n");
2336 selections[ELM_SEL_XDND].requestwidget = dropable->obj;
2337 selections[ELM_SEL_XDND].requestformat = ELM_SEL_FORMAT_MARKUP;
2338 selections[ELM_SEL_XDND].active = EINA_TRUE;
2340 ecore_x_selection_xdnd_request(xwin, atoms[i].name);
2345 _dnd_position(void *data __UNUSED__, int etype __UNUSED__, void *ev)
2347 struct _Ecore_X_Event_Xdnd_Position *pos;
2348 Ecore_X_Rectangle rect;
2352 /* Need to send a status back */
2353 /* FIXME: Should check I can drop here */
2354 /* FIXME: Should highlight widget */
2355 rect.x = pos->position.x - 5;
2356 rect.y = pos->position.y - 5;
2359 ecore_x_dnd_send_status(EINA_TRUE, EINA_FALSE, rect, pos->action);
2365 * When dragging this is callback response from the destination.
2366 * The important thing we care about: Can we drop; thus update cursor
2370 _dnd_status(void *data __UNUSED__, int etype __UNUSED__, void *ev)
2372 struct _Ecore_X_Event_Xdnd_Status *status = ev;
2374 if (!status) return EINA_TRUE;
2376 /* Only thing we care about: will accept */
2377 if (status->will_accept)
2379 cnp_debug("Will accept\n");
2382 { /* Won't accept */
2383 cnp_debug("Won't accept accept\n");
2389 * Add a widget as drop target.
2392 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
2395 Ecore_X_Window xwin;
2399 if (!obj) return EINA_FALSE;
2400 if (!_elm_cnp_init_count) _elm_cnp_init();
2402 /* Is this the first? */
2403 first = (!drops) ? 1 : 0;
2405 EINA_LIST_FOREACH(drops, item, drop)
2407 if (drop->obj == obj)
2409 /* Update: Not a new one */
2410 drop->dropcb = dropcb;
2411 drop->cbdata = cbdata;
2412 drop->types = format;
2417 /* Create new drop */
2418 drop = calloc(1, sizeof(Dropable));
2419 if (!drop) return EINA_FALSE;
2420 /* FIXME: Check for eina's deranged error method */
2421 drops = eina_list_append(drops, drop);
2423 if (!drops/* || or other error */)
2428 drop->dropcb = dropcb;
2429 drop->cbdata = cbdata;
2430 drop->types = format;
2433 evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL,
2434 /* I love C and varargs */
2435 (Evas_Object_Event_Cb)elm_drop_target_del,
2437 /* FIXME: Handle resizes */
2439 /* If not the first: We're done */
2440 if (!first) return EINA_TRUE;
2442 xwin = (Ecore_X_Window)ecore_evas_window_get
2443 (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
2445 ecore_x_dnd_aware_set(xwin, EINA_TRUE);
2447 cnp_debug("Adding drop target calls\n");
2448 handler_enter = ecore_event_handler_add(ECORE_X_EVENT_XDND_ENTER,
2450 handler_pos = ecore_event_handler_add(ECORE_X_EVENT_XDND_POSITION,
2451 _dnd_position, NULL);
2452 handler_drop = ecore_event_handler_add(ECORE_X_EVENT_XDND_DROP,
2459 elm_drop_target_del(Evas_Object *obj)
2461 Dropable *drop,*del;
2463 Ecore_X_Window xwin;
2466 EINA_LIST_FOREACH(drops, item, drop)
2468 if (drop->obj == obj)
2470 drops = eina_list_remove_list(drops, item);
2475 if (!del) return EINA_FALSE;
2477 evas_object_event_callback_del(obj, EVAS_CALLBACK_FREE,
2478 (Evas_Object_Event_Cb)elm_drop_target_del);
2480 /* If still drops there: All fine.. continue */
2481 if (drops) return EINA_TRUE;
2483 cnp_debug("Disabling DND\n");
2484 xwin = (Ecore_X_Window)ecore_evas_window_get
2485 (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
2486 ecore_x_dnd_aware_set(xwin, EINA_FALSE);
2488 ecore_event_handler_del(handler_pos);
2489 ecore_event_handler_del(handler_drop);
2490 ecore_event_handler_del(handler_enter);
2494 pasteimage_free(savedtypes.pi);
2495 savedtypes.pi = NULL;
2503 _drag_mouse_up(void *un __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *data __UNUSED__)
2505 evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_UP, _drag_mouse_up);
2509 dragdonecb(dragdonecb,selections[ELM_SEL_XDND].widget);
2514 evas_object_del(dragwin);
2520 _drag_move(void *data __UNUSED__, Ecore_X_Xdnd_Position *pos)
2522 evas_object_move(dragwin,
2523 pos->position.x - _dragx,
2524 pos->position.y - _dragy);
2529 elm_drag_start(Evas_Object *obj, Elm_Sel_Format format, const char *data, void (*dragdone) (void *data, Evas_Object *), void *donecbdata)
2531 Ecore_X_Window xwin;
2533 Elm_Sel_Type xdnd = ELM_SEL_XDND;
2535 int x, y, x2, y2, x3, y3;
2539 if (!_elm_cnp_init_count) _elm_cnp_init();
2541 xwin = elm_win_xwindow_get(obj);
2543 cnp_debug("starting drag...\n");
2545 ecore_x_dnd_type_set(xwin, "text/uri-list", 1);
2546 sel = selections + ELM_SEL_XDND;
2549 sel->format = format;
2550 sel->selbuf = data ? strdup(data) : NULL;
2551 dragdonecb = dragdone;
2552 dragdonedata = donecbdata;
2554 ecore_x_dnd_callback_pos_update_set(_drag_move, NULL);
2555 ecore_x_dnd_begin(xwin, (unsigned char *)&xdnd, sizeof(Elm_Sel_Type));
2556 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
2557 _drag_mouse_up, NULL);
2559 handler_status = ecore_event_handler_add(ECORE_X_EVENT_XDND_STATUS,
2562 dragwin = elm_win_add(NULL, "Elm Drag Object", ELM_WIN_UTILITY);
2563 elm_win_override_set(dragwin, 1);
2565 /* FIXME: Images only */
2566 icon = elm_icon_add(dragwin);
2567 elm_icon_file_set(icon, data + 7, NULL); /* 7!? "file://" */
2568 elm_win_resize_object_add(dragwin,icon);
2569 evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
2570 evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, EVAS_HINT_FILL);
2572 /* Position subwindow appropriately */
2573 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(obj));
2574 ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
2575 evas_object_geometry_get(obj, &x2, &y2, &w, &h);
2578 evas_object_move(dragwin, x, y);
2579 evas_object_resize(icon, w, h);
2580 evas_object_resize(dragwin, w, h);
2582 evas_object_show(icon);
2583 evas_object_show(dragwin);
2585 evas_pointer_canvas_xy_get(evas_object_evas_get(obj), &x3, &y3);
2593 elm_cnp_tempfile_create(int size)
2596 const char *tmppath;
2599 info = malloc(sizeof(Tmp_Info));
2600 if (!info) return NULL;
2602 tmppath = getenv("TMP");
2603 if (!tmppath) tmppath = P_tmpdir;
2604 if (!tmppath) tmppath = "/tmp";
2605 len = snprintf(NULL, 0, "%s/%sXXXXXX", tmppath, "elmcnpitem-");
2612 info->filename = malloc(len);
2613 if (!info->filename)
2618 snprintf(info->filename,len,"%s/%sXXXXXX", tmppath, "elmcnpitem-");
2620 info->fd = mkstemp(info->filename);
2625 /* And before someone says anything see POSIX 1003.1-2008 page 400 */
2628 pid = (long)getpid();
2629 /* Use pid instead of /proc/self: That way if can be passed around */
2630 len = snprintf(NULL,0,"/proc/%li/fd/%i", pid, info->fd);
2635 snprintf(tmp,len, "/proc/%li/fd/%i", pid, info->fd);
2636 unlink(info->filename);
2637 free(info->filename);
2638 info->filename = tmp;
2643 cnp_debug("filename is %s\n", info->filename);
2646 /* Set map to NULL and return */
2653 if (ftruncate(info->fd, size))
2655 perror("ftruncate");
2661 //eina_mmap_safety_enabled_set(EINA_TRUE); // NOTE: Temporary comment
2663 info->map = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, info->fd, 0);
2664 if (info->map == MAP_FAILED)
2676 tmpinfo_free(Tmp_Info *info)
2678 if (!info) return 0;
2679 free(info->filename);
2685 /* Stubs for windows */
2687 elm_drag_start(Evas_Object *o, Elm_Sel_Format f, const char *d, void (*donecb)(void *, Evas_Object *),void *cbdata)
2693 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
2699 elm_drop_target_del(Evas_Object *o)
2705 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-2f0^-2{2(0W1st0 :*/