2 # include "elementary_config.h"
4 #include <Elementary.h>
9 #ifdef HAVE_ELEMENTARY_X
11 #define ARRAYINIT(foo) [foo] =
17 # define cnp_debug(x...) fprintf(stderr, __FILE__": " x)
19 # define cnp_debug(x...)
22 #define PROVIDER_SET "__elm_cnp_provider_set"
24 typedef struct _Paste_Image Paste_Image;
25 typedef struct _Cnp_Selection Cnp_Selection;
26 typedef struct _Escape Escape;
27 typedef struct _Tmp_Info Tmp_Info;
28 typedef struct _Cnp_Atom Cnp_Atom;
29 typedef struct _Saved_Type Saved_Type;
30 typedef struct _Dropable Dropable;
32 typedef Eina_Bool (*Converter_Fn_Cb) (char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
33 typedef int (*Response_Handler_Cb) (Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *);
34 typedef int (*Notify_Handler_Cb) (Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *);
40 CNP_ATOM_text_urilist,
41 CNP_ATOM_text_x_vcard,
52 CNP_ATOM_text_html_utf8,
57 CNP_ATOM_text_plain_utf8,
76 Evas_Object *requestwidget;
78 Elm_Sel_Format requestformat;
80 Eina_Bool (*set) (Ecore_X_Window, const void *data, int size);
81 Eina_Bool (*clear) (void);
82 void (*request) (Ecore_X_Window, const char *target);
84 Elm_Sel_Format format;
85 Ecore_X_Selection ecore_sel;
107 Elm_Sel_Format formats;
108 /* Called by ecore to do conversion */
109 Converter_Fn_Cb converter;
110 Response_Handler_Cb response;
111 Notify_Handler_Cb notify;
122 Eina_Bool textreq: 1;
128 /* FIXME: Cache window */
129 Elm_Sel_Format types;
134 static Tmp_Info *elm_cnp_tempfile_create(int size);
135 static int tmpinfo_free(Tmp_Info *tmp);
137 static Eina_Bool _elm_cnp_init(void);
138 static Eina_Bool selection_clear(void *udata __UNUSED__, int type, void *event);
139 static Eina_Bool selection_notify(void *udata __UNUSED__, int type, void *event);
140 static char *remove_tags(const char *p, int *len);
141 static char *mark_up(const char *start, int inlen, int *lenp);
143 static Evas_Object *image_provider(void *images, Evas_Object *entry, const char *item);
144 static void entry_deleted(void *images, Evas *e, Evas_Object *entry, void *unused);
147 static Eina_Bool targets_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
148 static Eina_Bool text_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
149 static Eina_Bool html_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
150 static Eina_Bool edje_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
151 static Eina_Bool uri_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
152 static Eina_Bool image_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
153 static Eina_Bool vcard_send(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
155 static int response_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *);
157 static int notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
158 static int notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
159 static int notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
160 static int notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
161 static int notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
162 static int vcard_receive(Cnp_Selection *sed, Ecore_X_Event_Selection_Notify *notify);
164 static Paste_Image *pasteimage_alloc(const char *file, int pathlen);
165 static Eina_Bool pasteimage_append(Paste_Image *pi, Evas_Object *entry);
166 static void pasteimage_free(Paste_Image *pi);
168 /* Optimisation: Turn this into a 256 byte table:
169 * then can lookup in one index, not N checks */
170 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,
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;
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},
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 newNode->tag_str = tag_str;
520 newNode->pos_in_ori_str = pos_in_ori_str;
525 _get_start_node(char *str)
527 char *startStr = NULL;
528 if (!str || str[0] == '\0')
533 char *tagStart = strchr(str, '<');
535 startStr = strdup(str);
538 int strLength = tagStart - str;
539 startStr = malloc(sizeof(char) * (strLength + 1));
540 strncpy(startStr, str, strLength);
541 startStr[strLength] = '\0';
545 return _new_tag_node(NULL, NULL, startStr, str);
549 _get_next_node(PTagNode prev)
551 PTagNode retTag = NULL;
554 char *tagNameEnd = NULL;
557 if (prev->tag == NULL)
558 tagStart = strchr(prev->pos_in_ori_str, '<');
560 tagStart = strchr(prev->pos_in_ori_str + 1, '<');
565 tagEnd = strchr(tagStart, '>');
566 nextTagStart = strchr(tagStart + 1, '<');
568 if (!tagEnd || (nextTagStart && (nextTagStart < tagEnd)))
569 return _get_start_node(tagStart + 1);
572 char *spArray[spCnt];
573 spArray[0] = strchr(tagStart, '=');
574 spArray[1] = strchr(tagStart, '_');
575 spArray[2] = strchr(tagStart, ' ');
576 spArray[3] = strchr(tagStart, '\t');
577 spArray[4] = strchr(tagStart, '\n');
581 for (i = 0; i < spCnt; i++)
583 if (spArray[i] && spArray[i] < tagNameEnd)
584 tagNameEnd = spArray[i];
587 int tagLength = tagNameEnd - tagStart - 1;
588 char *tagName = NULL;
589 if (!strncmp(&tagStart[1], "color", tagLength))
590 tagName = strndup("font", 4);
591 else if (!strncmp(&tagStart[1], "/color", tagLength))
592 tagName = strndup("/font", 5);
593 else if (!strncmp(&tagStart[1], "/item", tagLength))
594 tagName = strdup("");
596 tagName = strndup(&tagStart[1], tagLength);
598 int tagStrLength = 0;
602 tagStrLength = tagEnd - tagStart + 1;
603 tagStr = strndup(tagStart, tagStrLength);
606 unsigned int strLength = nextTagStart ? (unsigned int)(nextTagStart - tagEnd - 1) : strlen(&tagEnd[1]);
607 char *str = strndup(&tagEnd[1], strLength);
609 retTag = _new_tag_node(tagName, tagStr, str, tagStart);
615 _delete_node(PTagNode node)
619 SAFEFREE(node->tag_str);
626 if (!strcmp("font", node->tag))
628 PFontTagData data = node->tagData;
629 SAFEFREE(data->name);
630 SAFEFREE(data->color);
631 SAFEFREE(data->size);
632 SAFEFREE(data->bg_color);
634 if (!strcmp("item", node->tag))
636 PItemTagData data = node->tagData;
637 SAFEFREE(data->href);
638 SAFEFREE(data->width);
639 SAFEFREE(data->height);
643 SAFEFREE(node->tagData);
651 _link_match_tags(Eina_List *nodes)
653 Eina_List *stack = NULL;
655 PTagNode trail, popData;
658 EINA_LIST_FOREACH(nodes, l, trail)
660 if (!trail->tag || trail->tag[0] == '\0')
662 if (!strcmp("br", trail->tag))
664 trail->tagPosType = TAGPOS_ALONE;
667 else if (!strcmp("item", trail->tag) || !strcmp("img", trail->tag))
669 trail->tagPosType = TAGPOS_ALONE;
673 if (trail->tag[0] != '/') // PUSH
675 stack = eina_list_append(stack, trail);
676 /* eina_array_push(stack, trail);
677 cnp_debug("stack: %d, tag %s\n", eina_array_count_get(stack), trail->tag);*/
678 cnp_debug("stack: %d, tag %s\n", eina_list_count(stack), trail->tag);
682 if (!eina_list_count(stack))
684 cnp_debug("tag not matched %s\n", trail->tag);
688 EINA_LIST_REVERSE_FOREACH(stack, r, popData)
690 if (popData->tag && !strcmp(popData->tag, &trail->tag[1]))
692 popData->tagPosType = TAGPOS_START;
693 trail->tagPosType = TAGPOS_END;
694 popData->matchTag = trail;
695 trail->matchTag = popData;
696 stack = eina_list_remove_list(stack, r);
700 /* popData = eina_array_pop(stack);
702 popData->tagPosType = TAGPOS_START;
703 trail->tagPosType = TAGPOS_END;
704 popData->matchTag = trail;
705 trail->matchTag = popData;
706 cnp_debug("pop stack: %d, tag %s\n", eina_array_count_get(stack), trail->tag);
711 /* if (eina_array_count_get(stack))
712 cnp_debug("stack state: %d, tag %s\n", eina_array_count_get(stack), trail->tag);*/
714 /* Make Dummy close tag */
715 /* while ((popData = eina_array_pop(stack))) */
717 EINA_LIST_REVERSE_FOREACH(stack, r, popData)
720 int tagLength = strlen(popData->tag);
721 char *tagName = malloc(sizeof(char) * (tagLength + 2));
725 strcat(tagName, popData->tag);
727 newData = _new_tag_node(tagName, NULL, NULL, NULL);
728 popData->tagPosType = TAGPOS_START;
729 newData->tagPosType = TAGPOS_END;
730 popData->matchTag = newData;
731 newData->matchTag = popData;
732 nodes = eina_list_append(nodes, newData);
733 /* cnp_debug("stack: %d, tag %s\n", eina_array_count_get(stack), popData->tag);*/
735 /* cnp_debug("stack_top: %d\n", eina_array_count_get(stack));
736 eina_array_free(stack);*/
737 eina_list_free(stack);
741 _get_tag_value(const char *tag_str, const char *tag_name)
743 if (!tag_name || !tag_str)
747 if ((tag = strstr(tag_str, tag_name)))
749 if (tag[strlen(tag_name)] == '_')
751 char *value = strchr(tag, '=');
757 } while (!isalnum(*value) && *value != '#');
760 char *spArray[spCnt];
761 spArray[0] = strchr(value, ' ');
762 spArray[1] = strchr(value, '>');
763 spArray[2] = strchr(value, '\"');
764 spArray[3] = strchr(value, '\'');
765 spArray[4] = strchr(value, '\t');
766 spArray[5] = strchr(value, '\n');
767 char *valueEnd = strchr(value, '\0');
770 for (i = 0; i < spCnt; i++)
772 if (spArray[i] && spArray[i] < valueEnd)
773 valueEnd = spArray[i];
776 int valueLength = valueEnd - value;
777 return strndup(value, valueLength);
784 _set_EFL_font_data(PFontTagData data, const char *tag_str)
789 data = calloc(1, sizeof(FontTagData));
790 value = _get_tag_value(tag_str, "font_size");
791 freeAndAssign(data->size, value);
792 value = _get_tag_value(tag_str, "color");
793 freeAndAssign(data->color, value);
794 value = _get_tag_value(tag_str, "bgcolor");
795 freeAndAssign(data->bg_color, value);
796 value = _get_tag_value(tag_str, "font");
797 freeAndAssign(data->name, value);
803 _set_EFL_item_data(PItemTagData data, const char *tag_str)
808 data = calloc(1, sizeof(ItemTagData));
809 value = _get_tag_value(tag_str, "href");
812 char *path = strstr(value, "file://");
815 char *modify = malloc(sizeof(char) * (strlen(value) + 1));
816 strncpy(modify, "file://", 7);
819 while (path[1] && path[0] && path[1] == '/' && path[0] == '/')
823 strcat(modify, path);
825 cnp_debug("image href ---%s---\n", data->href);
828 freeAndAssign(data->href, value);
832 value = _get_tag_value(tag_str, "absize");
835 char *xpos = strchr(value, 'x');
838 int absizeLen = strlen(value);
839 freeAndAssign(data->width, strndup(value, xpos - value));
840 freeAndAssign(data->height, strndup(xpos + 1, absizeLen - (xpos - value) - 1));
841 cnp_debug("image width: -%s-, height: -%s-\n", data->width, data->height);
849 _set_EFL_tag_data(Eina_List* nodes)
854 EINA_LIST_FOREACH(nodes, l, trail)
858 if (!strcmp("font", trail->tag))
859 trail->tagData = _set_EFL_font_data(trail->tagData, trail->tag_str);
860 else if (!strcmp("item", trail->tag))
861 trail->tagData = _set_EFL_item_data(trail->tagData, trail->tag_str);
866 _set_HTML_font_data(PFontTagData data, const char *tag_str)
871 data = calloc(1, sizeof(FontTagData));
872 value = _get_tag_value(tag_str, "size");
873 freeAndAssign(data->size, value);
874 value = _get_tag_value(tag_str, "color");
875 freeAndAssign(data->color, value);
876 value = _get_tag_value(tag_str, "bgcolor");
877 freeAndAssign(data->bg_color, value);
878 value = _get_tag_value(tag_str, "face");
879 freeAndAssign(data->name, value);
885 _set_HTML_img_data(PItemTagData data, const char *tag_str)
890 data = calloc(1, sizeof(ItemTagData));
891 value = _get_tag_value(tag_str, "src");
894 char *path = strstr(value, "file://");
897 char *modify = malloc(sizeof(char) * (strlen(value) + 1));
898 strncpy(modify, "file://", 7);
901 while (path[1] && path[0] && path[1] == '/' && path[0] == '/')
905 strcat(modify, path);
907 cnp_debug("image src ---%s---\n", data->href);
910 freeAndAssign(data->href, value);
914 value = _get_tag_value(tag_str, "width");
915 freeAndAssign(data->width, value);
916 value = _get_tag_value(tag_str, "height");
917 freeAndAssign(data->height, value);
922 _set_HTML_tag_data(Eina_List* nodes)
927 EINA_LIST_FOREACH(nodes, l, trail)
931 if (!strcmp("font", trail->tag))
932 trail->tagData = _set_HTML_font_data(trail->tagData, trail->tag_str);
933 else if (!strcmp("img", trail->tag))
934 trail->tagData = _set_HTML_img_data(trail->tagData, trail->tag_str);
940 _dumpNode(Eina_List* nodes)
945 EINA_LIST_FOREACH(nodes, l, trail)
947 cnp_debug("tag: %s, tag_str: %s, str: %s, tagPosType: %d\n",
948 trail->tag, trail->tag_str, trail->str, trail->tagPosType);
949 cnp_debug("matchTag: %x ", (unsigned int)trail->matchTag);
951 cnp_debug("matchTag->tag_str: %s", trail->matchTag->tag_str);
954 if (!strcmp(trail->tag, "font"))
956 PFontTagData data = trail->tagData;
957 cnp_debug(" tagData->name: %s, tagData->color: %s, tagData->size: %s, tagData->bg_color: %s",
958 data->name, data->color, data->size, data->bg_color);
960 else if (!strcmp(trail->tag, "item") || !strcmp(trail->tag, "img"))
962 PItemTagData data = trail->tagData;
963 cnp_debug(" tagData->href: %s, tagData->width: %s, tagData->height: %s",
964 data->href, data->width, data->height);
967 cnp_debug("\nERROR!!!! not need tagData");
975 _convert_to_html(Eina_List* nodes)
980 Eina_Strbuf *html = eina_strbuf_new();
982 int tableCnt = sizeof(_EFLtoHTMLConvertTable) / sizeof(TagTable);
984 EINA_LIST_FOREACH(nodes, l, trail)
988 char *tagName = trail->tagPosType == TAGPOS_END ?
989 trail->matchTag->tag : trail->tag;
991 for(j = 0; j < tableCnt; j++)
993 if (!strcmp(_EFLtoHTMLConvertTable[j].src, tagName))
995 switch(trail->tagPosType)
998 eina_strbuf_append(html, "</");
1001 eina_strbuf_append(html, "<");
1005 eina_strbuf_append(html, _EFLtoHTMLConvertTable[j].dst);
1006 if (trail->tagPosType != TAGPOS_END)
1008 if (!strcmp(_EFLtoHTMLConvertTable[j].src, "font"))
1010 PFontTagData data = trail->tagData;
1015 eina_strbuf_append_printf(html, " color=\"%s\"", data->color);
1017 eina_strbuf_append_printf(html, " size=\"%s\"", data->size);
1022 else if (!strcmp(_EFLtoHTMLConvertTable[j].src, "item"))
1024 PItemTagData data = trail->tagData;
1026 eina_strbuf_append_printf(html, " src=\"%s\"", data->href);
1028 eina_strbuf_append_printf(html, " width=\"%s\"", data->width);
1030 eina_strbuf_append_printf(html, " height=\"%s\"", data->height);
1033 switch(trail->tagPosType)
1036 eina_strbuf_append(html, " />");
1039 eina_strbuf_append(html, ">");
1047 eina_strbuf_append(html, trail->str);
1050 char *ret = eina_strbuf_string_steal(html);
1051 eina_strbuf_free(html);
1055 #define IMAGE_DEFAULT_WIDTH "240"
1056 #define IMAGE_DEFAULT_HEIGHT "180"
1060 _convert_to_edje(Eina_List* nodes)
1065 Eina_Strbuf *html = eina_strbuf_new();
1067 int tableCnt = sizeof(_HTMLtoEFLConvertTable) / sizeof(TagTable);
1069 EINA_LIST_FOREACH(nodes, l, trail)
1073 char *tagName = trail->tagPosType == TAGPOS_END ?
1074 trail->matchTag->tag : trail->tag;
1076 for(j = 0; j < tableCnt; j++)
1078 if (!strcmp(_HTMLtoEFLConvertTable[j].src, tagName))
1080 if (_HTMLtoEFLConvertTable[j].dst[0] != '\0')
1082 switch(trail->tagPosType)
1085 eina_strbuf_append(html, "</");
1088 eina_strbuf_append(html, "<");
1092 eina_strbuf_append(html, _HTMLtoEFLConvertTable[j].dst);
1094 if (trail->tagPosType != TAGPOS_END)
1096 if (!strcmp(_HTMLtoEFLConvertTable[j].src, "font"))
1098 PFontTagData data = trail->tagData;
1103 eina_strbuf_append_printf(html, "<color=%s>", data->color);
1105 eina_strbuf_append_printf(html, "<font_size=%s>", data->size);
1111 else if (!strcmp(_HTMLtoEFLConvertTable[j].src, "img"))
1113 PItemTagData data = trail->tagData;
1114 char *width = IMAGE_DEFAULT_WIDTH, *height = IMAGE_DEFAULT_HEIGHT;
1116 eina_strbuf_append_printf(html, " href=%s", data->href);
1118 width = data->width;
1120 height = data->height;
1121 eina_strbuf_append_printf(html, " absize=%sx%s></item>", width, height);
1127 if (_HTMLtoEFLConvertTable[j].dst[0] == '\0')
1129 if (!strcmp(_HTMLtoEFLConvertTable[j].src, "font"))
1131 if (trail->matchTag->tagData)
1133 PFontTagData data = trail->matchTag->tagData;
1138 eina_strbuf_append_printf(html, "</color>");
1140 eina_strbuf_append_printf(html, "</font>");
1149 switch(trail->tagPosType)
1152 eina_strbuf_append(html, " />");
1155 eina_strbuf_append(html, ">");
1160 }/* for(j = 0; j < tableCnt; j++) end */
1163 eina_strbuf_append(html, trail->str);
1166 char *ret = eina_strbuf_string_steal(html);
1167 eina_strbuf_free(html);
1172 elm_selection_set(Elm_Sel_Type selection, Evas_Object *widget, Elm_Sel_Format format, const char *selbuf)
1174 #ifdef HAVE_ELEMENTARY_X
1177 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
1178 if (!_elm_cnp_init_count) _elm_cnp_init();
1179 if ((!selbuf) && (format != ELM_SEL_FORMAT_IMAGE))
1180 return elm_selection_clear(selection, widget);
1182 sel = selections + selection;
1185 sel->widget = widget;
1187 sel->set(elm_win_xwindow_get(widget),&selection,sizeof(Elm_Sel_Type));
1188 sel->format = format;
1189 sel->selbuf = selbuf ? strdup(selbuf) : NULL;
1198 elm_selection_clear(Elm_Sel_Type selection, Evas_Object *widget)
1200 #ifdef HAVE_ELEMENTARY_X
1203 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
1204 if (!_elm_cnp_init_count) _elm_cnp_init();
1206 sel = selections + selection;
1208 /* No longer this selection: Consider it gone! */
1209 if ((!sel->active) || (sel->widget != widget)) return EINA_TRUE;
1222 elm_selection_get(Elm_Sel_Type selection, Elm_Sel_Format format,
1223 Evas_Object *widget, Elm_Drop_Cb datacb, void *udata)
1225 #ifdef HAVE_ELEMENTARY_X
1229 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
1230 if (!_elm_cnp_init_count) _elm_cnp_init();
1232 sel = selections + selection;
1233 top = elm_widget_top_get(widget);
1234 if (!top) return EINA_FALSE;
1236 sel->requestformat = format;
1237 sel->requestwidget = widget;
1238 sel->request(elm_win_xwindow_get(top), ECORE_X_SELECTION_TARGET_TARGETS);
1239 sel->datacb = datacb;
1248 #ifdef HAVE_ELEMENTARY_X
1255 if (_elm_cnp_init_count++) return EINA_TRUE;
1256 for (i = 0; i < CNP_N_ATOMS; i++)
1258 atoms[i].atom = ecore_x_atom_get(atoms[i].name);
1259 ecore_x_selection_converter_atom_add(atoms[i].atom,
1260 atoms[i].converter);
1262 clipboard_atom = ecore_x_atom_get("CLIPBOARD");
1264 ecore_event_handler_add(ECORE_X_EVENT_SELECTION_CLEAR, selection_clear, NULL);
1265 ecore_event_handler_add(ECORE_X_EVENT_SELECTION_NOTIFY, selection_notify, NULL);
1267 text_uri = eina_stringshare_add("text/uri-list");
1272 selection_clear(void *udata __UNUSED__, int type __UNUSED__, void *event)
1274 Ecore_X_Event_Selection_Clear *ev = event;
1278 for (i = 0; i < ELM_SEL_MAX; i++)
1280 if (selections[i].ecore_sel == ev->selection) break;
1282 cnp_debug("selection %d clear\n", i);
1283 /* Not me... Don't care */
1284 if (i == ELM_SEL_MAX) return ECORE_CALLBACK_PASS_ON;
1286 sel = selections + i;
1291 return ECORE_CALLBACK_PASS_ON;
1296 * Response to a selection notify:
1297 * - So we have asked for the selection list.
1298 * - If it's the targets list, parse it, and fire of what we want,
1299 * else it's the data we want.
1302 selection_notify(void *udata __UNUSED__, int type __UNUSED__, void *event)
1304 Ecore_X_Event_Selection_Notify *ev = event;
1308 cnp_debug("selection notify callback: %d\n",ev->selection);
1309 switch (ev->selection)
1311 case ECORE_X_SELECTION_CLIPBOARD:
1312 sel = selections + ELM_SEL_CLIPBOARD;
1314 case ECORE_X_SELECTION_PRIMARY:
1315 sel = selections + ELM_SEL_PRIMARY;
1317 case ECORE_X_SELECTION_SECONDARY:
1318 sel = selections + ELM_SEL_SECONDARY;
1320 case ECORE_X_SELECTION_XDND:
1321 sel = selections + ELM_SEL_XDND;
1324 return ECORE_CALLBACK_PASS_ON;
1326 cnp_debug("Target is %s\n", ev->target);
1328 for (i = 0; i < CNP_N_ATOMS; i++)
1330 if (!strcmp(ev->target, atoms[i].name))
1332 if (atoms[i].notify)
1334 cnp_debug("Found something: %s\n", atoms[i].name);
1335 atoms[i].notify(sel, ev);
1339 cnp_debug("Ignored: No handler!\n");
1344 return ECORE_CALLBACK_PASS_ON;
1350 targets_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize)
1356 if (!data_ret) return EINA_FALSE;
1358 sel = selections + *((int *)data);
1360 for (i = 0, count = 0; i < CNP_N_ATOMS ; i++)
1362 if (sel->format & atoms[i].formats) count++;
1365 aret = malloc(sizeof(Ecore_X_Atom) * count);
1366 for (i = 0, count = 0; i < CNP_N_ATOMS; i++)
1368 if (sel->format & atoms[i].formats) aret[count ++] = atoms[i].atom;
1372 if (typesize) *typesize = 32 /* urk */;
1373 if (ttype) *ttype = ECORE_X_ATOM_ATOM;
1374 if (size_ret) *size_ret = count;
1380 image_converter(char *target __UNUSED__, void *data __UNUSED__, int size __UNUSED__, void **data_ret __UNUSED__, int *size_ret __UNUSED__, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1382 cnp_debug("Image converter called\n");
1387 vcard_send(char *target __UNUSED__, void *data __UNUSED__, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1391 cnp_debug("Vcard send called\n");
1393 sel = selections + *((int *)data);
1395 if (data_ret) *data_ret = strdup(sel->selbuf);
1396 if (size_ret) *size_ret = strlen(sel->selbuf);
1401 * Callback to handle a targets response on a selection request:
1402 * So pick the format we'd like; and then request it.
1405 notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1407 Ecore_X_Selection_Data_Targets *targets;
1408 Ecore_X_Atom *atomlist;
1411 targets = notify->data;
1412 atomlist = (Ecore_X_Atom *)(targets->data.data);
1414 for (j = 1; j < CNP_N_ATOMS; j++)
1416 cnp_debug("\t%s %d\n", atoms[j].name, atoms[j].atom);
1417 if (!(atoms[j].formats & sel->requestformat)) continue;
1418 for (i = 0; i < targets->data.length; i++)
1420 if ((atoms[j].atom == atomlist[i]) && (atoms[j].notify))
1422 cnp_debug("Atom %s matches\n",atoms[j].name);
1428 cnp_debug("Couldn't find anything that matches\n");
1429 return ECORE_CALLBACK_PASS_ON;
1432 cnp_debug("Sending request for %s\n",atoms[j].name);
1433 sel->request(elm_win_xwindow_get(sel->requestwidget), atoms[j].name);
1435 return ECORE_CALLBACK_PASS_ON;
1439 response_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1441 Ecore_X_Selection_Data_Targets *targets;
1442 Ecore_X_Atom *atomlist;
1446 targets = notify->data;
1447 atomlist = (Ecore_X_Atom *)(targets->data.data);
1449 /* Start from 1: Skip targets */
1450 for (j = 1 ; j < CNP_N_ATOMS ; j ++)
1452 if (!(atoms[j].formats & sel->requestformat)) continue;
1453 for (i = 0 ; i < targets->data.length ; i ++)
1455 if ((atoms[j].atom == atomlist[i]) && (atoms[j].response))
1457 /* Found a match: Use it */
1463 if (j == CNP_N_ATOMS)
1465 cnp_debug("No matching type found\n");
1469 top = elm_widget_top_get(sel->requestwidget);
1472 sel->request(elm_win_xwindow_get(top), atoms[j].name);
1478 notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1480 Ecore_X_Selection_Data *data;
1483 data = notify->data;
1487 Elm_Selection_Data ddata;
1489 str = mark_up((char *)data->data, data->length, NULL);
1490 ddata.x = ddata.y = 0;
1491 ddata.format = ELM_SEL_FORMAT_TEXT;
1493 ddata.len = data->length;
1494 sel->datacb(sel->udata, sel->widget, &ddata);
1499 cnp_debug("Notify handler text %d %d %p\n", data->format,data->length, data->data);
1500 str = mark_up((char *)data->data, data->length, NULL);
1501 cnp_debug("String is %s (from %s)\n", str, data->data);
1502 elm_entry_entry_insert(sel->requestwidget, str);
1509 * So someone is pasting an image into my entry or widget...
1512 notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1514 Ecore_X_Selection_Data *data;
1515 Ecore_X_Selection_Data_Files *files;
1519 data = notify->data;
1520 cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
1521 if (data->content == ECORE_X_SELECTION_CONTENT_FILES)
1523 cnp_debug("got a files list\n");
1524 files = notify->data;
1525 if (files->num_files > 1)
1527 /* Don't handle many items */
1528 cnp_debug("more then one file: Bailing\n");
1531 p = files->files[0];
1535 p = (char *)data->data;
1540 cnp_debug("Couldn't find a file\n");
1543 cnp_debug("Got %s\n",p);
1546 Elm_Selection_Data ddata;
1548 ddata.x = ddata.y = 0;
1549 ddata.format = ELM_SEL_FORMAT_MARKUP;
1551 ddata.len = data->length;
1552 sel->datacb(sel->udata, sel->widget, &ddata);
1555 if (strncmp(p, "file://", 7))
1557 /* Try and continue if it looks sane */
1558 if (*p != '/') return 0;
1562 p += strlen("file://");
1565 if (savedtypes.pi) pasteimage_free(savedtypes.pi);
1566 pi = pasteimage_alloc(p, strlen(p));
1567 if (savedtypes.textreq)
1569 savedtypes.textreq = 0;
1574 pasteimage_append(pi, sel->requestwidget);
1575 savedtypes.pi = NULL;
1581 * Just receieved an vcard, either through cut and paste, or dnd.
1584 vcard_receive(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1588 Ecore_X_Selection_Data *data;
1590 data = notify->data;
1591 cnp_debug("vcard receive\n");
1593 if (sel == (selections + ELM_SEL_XDND))
1595 Elm_Selection_Data ddata;
1597 cnp_debug("drag & drop\n");
1598 /* FIXME: this needs to be generic: Used for all receives */
1599 EINA_LIST_FOREACH(drops, l, dropable)
1601 if (dropable->obj == sel->requestwidget) break;
1605 cnp_debug("Unable to find drop object");
1606 ecore_x_dnd_send_finished();
1609 dropable = eina_list_data_get(l);
1610 ddata.x = savedtypes.x;
1611 ddata.y = savedtypes.y;
1612 ddata.format = ELM_SEL_FORMAT_VCARD;
1613 ddata.data = data->data;
1614 ddata.len = data->length;
1615 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1616 ecore_x_dnd_send_finished();
1618 else if (sel->datacb)
1620 Elm_Selection_Data ddata;
1621 ddata.x = ddata.y = 0;
1622 ddata.format = ELM_SEL_FORMAT_IMAGE;
1623 ddata.data = data->data;
1624 ddata.len = data->length;
1625 sel->datacb(sel->udata, sel->widget, &ddata);
1629 cnp_debug("Paste request\n");
1638 notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1640 Ecore_X_Selection_Data *data;
1644 cnp_debug("got a png (or a jpeg)!\n");
1645 data = notify->data;
1647 cnp_debug("Size if %d\n", data->length);
1651 Elm_Selection_Data ddata;
1653 ddata.x = ddata.y = 0;
1654 ddata.format = ELM_SEL_FORMAT_IMAGE;
1655 ddata.data = data->data;
1656 ddata.len = data->length;
1657 sel->datacb(sel->udata, sel->widget, &ddata);
1661 /* generate tmp name */
1662 tmp = elm_cnp_tempfile_create(data->length);
1663 memcpy(tmp->map, data->data, data->length);
1664 munmap(tmp->map,data->length);
1666 /* FIXME: Add to paste image data to clean up */
1667 pi = pasteimage_alloc(tmp->filename, strlen(tmp->filename));
1668 pasteimage_append(pi, sel->requestwidget);
1676 * Warning: Generic text/html can';t handle it sanely.
1677 * Firefox sends ucs2 (i think).
1678 * chrome sends utf8... blerg
1681 notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1683 Ecore_X_Selection_Data *data;
1685 cnp_debug("Got some HTML: Checking encoding is useful\n");
1686 data = notify->data;
1690 Elm_Selection_Data ddata;
1691 ddata.x = ddata.y = 0;
1692 ddata.format = ELM_SEL_FORMAT_HTML;
1693 ddata.data = data->data;
1694 ddata.len = data->length;
1695 sel->datacb(sel->udata, sel->widget, &ddata);
1699 char *stripstr = NULL;
1700 stripstr = malloc(sizeof(char) * (data->length + 1));
1701 strncpy(stripstr, (char *)data->data, data->length);
1702 stripstr[data->length] = '\0';
1703 cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
1704 elm_entry_entry_insert(sel->requestwidget, stripstr);
1711 text_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1715 cnp_debug("text converter\n");
1716 sel = selections + *((int *)data);
1717 if (!sel->active) return EINA_TRUE;
1719 if ((sel->format & ELM_SEL_FORMAT_MARKUP) ||
1720 (sel->format & ELM_SEL_FORMAT_HTML))
1722 *data_ret = remove_tags(sel->selbuf, size_ret);
1724 else if (sel->format & ELM_SEL_FORMAT_TEXT)
1726 *data_ret = strdup(sel->selbuf);
1727 *size_ret = strlen(sel->selbuf);
1729 else if (sel->format & ELM_SEL_FORMAT_IMAGE)
1731 cnp_debug("Image %s\n", evas_object_type_get(sel->widget));
1732 cnp_debug("Elm type: %s\n", elm_object_widget_type_get(sel->widget));
1733 evas_object_image_file_get(elm_photocam_internal_image_get(sel->widget), (const char **)data_ret, NULL);
1734 if (!*data_ret) *data_ret = strdup("No file");
1735 else *data_ret = strdup(*data_ret);
1736 *size_ret = strlen(*data_ret);
1742 edje_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1746 sel = selections + *((int *)data);
1747 /* if (data_ret) *data_ret = strdup(sel->selbuf);
1748 if (size_ret) *size_ret = strlen(sel->selbuf);*/
1751 if (data_ret && (sel->format & ELM_SEL_FORMAT_HTML))
1753 Eina_List *nodeList = NULL;
1757 nodeData = _get_start_node(sel->selbuf);
1761 nodeList = eina_list_append(nodeList, nodeData);
1762 nodeData = _get_next_node(nodeData);
1765 _link_match_tags(nodeList);
1767 _set_HTML_tag_data(nodeList);
1770 _dumpNode(nodeList);
1772 edje = _convert_to_edje(nodeList);
1774 cnp_debug("convert edje: %s\n", edje);
1776 EINA_LIST_FOREACH(nodeList, trail, nodeData)
1777 _delete_node(nodeData);
1778 eina_list_free(nodeList);
1786 *data_ret = strdup(sel->selbuf);
1791 *size_ret = strlen(edje);
1793 *size_ret = strlen(sel->selbuf);
1800 html_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1804 sel = selections + *(int *)data;
1807 if (data_ret && (sel->format & ELM_SEL_FORMAT_MARKUP))
1809 Eina_List *nodeList = NULL;
1813 nodeData = _get_start_node(sel->selbuf);
1817 nodeList = eina_list_append(nodeList, nodeData);
1818 nodeData = _get_next_node(nodeData);
1821 _link_match_tags(nodeList);
1823 _set_EFL_tag_data(nodeList);
1826 _dumpNode(nodeList);
1828 html = _convert_to_html(nodeList);
1830 cnp_debug("convert html: %s\n", html);
1832 EINA_LIST_FOREACH(nodeList, trail, nodeData)
1833 _delete_node(nodeData);
1834 eina_list_free(nodeList);
1841 *data_ret = strdup(sel->selbuf);
1847 *size_ret = strlen(html);
1849 *size_ret = strlen(sel->selbuf);
1856 uri_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1859 sel = selections + *((int *)data);
1860 cnp_debug("Uri converter\n");
1861 if (data_ret) *data_ret = strdup(sel->selbuf);
1862 if (size_ret) *size_ret = strlen(sel->selbuf);
1867 * Image paste provide
1870 /* FIXME: Should add provider for each pasted item: Use data to store it
1872 static Evas_Object *
1873 image_provider(void *images __UNUSED__, Evas_Object *entry, const char *item)
1878 cnp_debug("image provider for %s called\n", item);
1879 EINA_LIST_FOREACH(pastedimages, l, pi)
1881 cnp_debug("is it %s?\n",pi->tag);
1882 if (!strcmp(pi->tag, item))
1886 o = evas_object_image_filled_add(evas_object_evas_get(entry));
1887 /* FIXME: Handle eets */
1888 cnp_debug("file is %s (object is %p)\n", pi->file, o);
1889 evas_object_image_file_set(o, pi->file, NULL);
1890 evas_object_show(o);
1898 static Paste_Image *
1899 pasteimage_alloc(const char *file, int pathlen)
1903 char *buf, *filebuf;
1904 int prefixlen = strlen("file://");
1906 pi = calloc(1, sizeof(Paste_Image));
1907 if (!pi) return NULL;
1909 len = snprintf(NULL, 0, "pasteimage-%p", pi);
1917 snprintf(buf, len, "pasteimage-%p", pi);
1922 if (strstr(file,"file://")) file += prefixlen;
1923 filebuf = alloca(pathlen + 1);
1924 strncpy(filebuf, file, pathlen);
1925 filebuf[pathlen] = 0;
1926 pi->file = strdup(filebuf);
1933 pasteimage_free(Paste_Image *pi)
1936 if (pi->file) free((void*)pi->file);
1937 if (pi->tag) free((void*)pi->tag);
1942 pasteimage_provider_set(Evas_Object *entry)
1947 if (!entry) return EINA_FALSE;
1948 type = elm_widget_type_get(entry);
1949 cnp_debug("type is %s\n", type);
1950 if ((!type) || (strcmp(type, "entry"))) return EINA_FALSE;
1952 v = evas_object_data_get(entry, PROVIDER_SET);
1955 evas_object_data_set(entry, PROVIDER_SET, pasteimage_provider_set);
1956 elm_entry_item_provider_append(entry, image_provider, NULL);
1957 evas_object_event_callback_add(entry, EVAS_CALLBACK_FREE,
1958 entry_deleted, NULL);
1965 pasteimage_append(Paste_Image *pi, Evas_Object *entry)
1969 static const char *tagstring = "<item absize=240x180 href=file://%s></item>";
1971 if (!pi) return EINA_FALSE;
1972 if (!entry) return EINA_FALSE;
1974 pasteimage_provider_set(entry);
1976 len = strlen(tagstring)+strlen(pi->file);
1978 pastedimages = eina_list_append(pastedimages, pi);
1979 entrytag = alloca(len + 1);
1980 snprintf(entrytag, len + 1, tagstring, pi->file);
1981 elm_entry_entry_insert(entry, entrytag);
1987 entry_deleted(void *images __UNUSED__, Evas *e __UNUSED__, Evas_Object *entry, void *unused __UNUSED__)
1992 EINA_LIST_FOREACH_SAFE(pastedimages, l, next, pi)
1994 if (pi->entry == entry)
1995 pastedimages = eina_list_remove_list(pastedimages, l);
2001 remove_tags(const char *p, int *len)
2005 if (!p) return NULL;
2007 q = malloc(strlen(p) + 1);
2008 if (!q) return NULL;
2013 if ((*p != '<') && (*p != '&')) *q++ = *p++;
2016 if ((p[1] == 'b') && (p[2] == 'r') &&
2017 ((p[3] == ' ') || (p[3] == '/') || (p[3] == '>')))
2019 while ((*p) && (*p != '>')) p++;
2025 for (i = 0 ; i < N_ESCAPES ; i++)
2027 if (!strncmp(p,escapes[i].escape, strlen(escapes[i].escape)))
2029 p += strlen(escapes[i].escape);
2030 *q = escapes[i].value;
2035 if (i == N_ESCAPES) *q ++= '&';
2039 if (len) *len = q - ret;
2045 mark_up(const char *start, int inlen, int *lenp)
2050 const char *endp = NULL;
2052 if (!start) return NULL;
2053 if (inlen >= 0) endp = start + inlen;
2054 /* First pass: Count characters */
2055 for (l = 0, p = start; ((!endp) || (p < endp)) && (*p); p++)
2057 for (i = 0 ; i < N_ESCAPES ; i ++)
2059 if (*p == escapes[i].value)
2061 l += strlen(escapes[i].escape);
2065 if (i == N_ESCAPES) l++;
2068 q = ret = malloc(l + 1);
2070 /* Second pass: Change characters */
2071 for (p = start; *p; )
2073 for (i = 0; i < N_ESCAPES; i++)
2075 if (*p == escapes[i].value)
2077 strcpy(q, escapes[i].escape);
2078 q += strlen(escapes[i].escape);
2083 if (i == N_ESCAPES) *q++ = *p++;
2087 if (lenp) *lenp = l;
2093 _dnd_enter(void *data __UNUSED__, int etype __UNUSED__, void *ev)
2095 Ecore_X_Event_Xdnd_Enter *enter = ev;
2099 if ((!enter) || (!enter->num_types) || (!enter->types)) return EINA_TRUE;
2101 cnp_debug("Types\n");
2102 savedtypes.ntypes = enter->num_types;
2103 if (savedtypes.types) free(savedtypes.types);
2104 savedtypes.types = malloc(sizeof(char *) * enter->num_types);
2105 if (!savedtypes.types) return EINA_FALSE;
2107 for (i = 0; i < enter->num_types; i++)
2109 savedtypes.types[i] = eina_stringshare_add(enter->types[i]);
2110 cnp_debug("Type is %s %p %p\n", enter->types[i],
2111 savedtypes.types[i],text_uri);
2112 if (savedtypes.types[i] == text_uri)
2114 /* Request it, so we know what it is */
2115 cnp_debug("Sending uri request\n");
2116 savedtypes.textreq = 1;
2117 savedtypes.pi = NULL; /* FIXME: Free? */
2118 ecore_x_selection_xdnd_request(enter->win, text_uri);
2122 /* FIXME: Find an object and make it current */
2127 _dnd_drop(void *data __UNUSED__, int etype __UNUSED__, void *ev)
2129 struct _Ecore_X_Event_Xdnd_Drop *drop;
2133 Ecore_X_Window xwin;
2134 Elm_Selection_Data ddata;
2140 // check we still have something to drop
2141 if (!drops) return EINA_TRUE;
2143 /* Find any widget in our window; then work out geometry rel to our window */
2144 for (l = drops; l; l = l->next)
2147 xwin = (Ecore_X_Window)ecore_evas_window_get
2148 (ecore_evas_ecore_evas_get(evas_object_evas_get
2150 if (xwin == drop->win) break;
2152 /* didn't find a window */
2153 if (!l) return EINA_TRUE;
2155 /* Calculate real (widget relative) position */
2156 // - window position
2157 // - widget position
2158 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(dropable->obj));
2159 ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
2160 savedtypes.x = drop->position.x - x;
2161 savedtypes.y = drop->position.y - y;
2163 cnp_debug("Drop position is %d,%d\n", savedtypes.x, savedtypes.y);
2165 for (; l; l = l->next)
2168 evas_object_geometry_get(dropable->obj, &x, &y, &w, &h);
2169 if ((savedtypes.x >= x) && (savedtypes.y >= y) &&
2170 (savedtypes.x < x + w) && (savedtypes.y < y + h))
2174 if (!l) return EINA_TRUE; /* didn't find one */
2176 evas_object_geometry_get(dropable->obj, &x, &y, NULL, NULL);
2180 /* Find our type from the previous list */
2181 for (i = 0; i < CNP_N_ATOMS; i++)
2183 for (j = 0; j < savedtypes.ntypes; j++)
2185 if (!strcmp(savedtypes.types[j], atoms[i].name)) goto found;
2189 cnp_debug("Didn't find a target\n");
2193 cnp_debug("Found a target we'd like: %s\n", atoms[i].name);
2194 cnp_debug("0x%x\n",xwin);
2196 if (i == CNP_ATOM_text_urilist)
2198 cnp_debug("We found a URI... (%scached) %s\n",
2199 savedtypes.pi ? "" : "not ",
2200 savedtypes.pi->file);
2204 static const char *tagstring = "<item absize=240x180 href="
2205 "file://%s></item>";
2206 ddata.x = savedtypes.x;
2207 ddata.y = savedtypes.y;
2209 /* If it's markup that also supports images */
2210 if ((dropable->types & ELM_SEL_FORMAT_MARKUP) &&
2211 (dropable->types & ELM_SEL_FORMAT_IMAGE))
2214 ddata.format = ELM_SEL_FORMAT_MARKUP;
2215 pasteimage_provider_set(dropable->obj);
2217 pastedimages = eina_list_append(pastedimages, savedtypes.pi);
2218 len = strlen(tagstring) + strlen(savedtypes.pi->file);
2219 entrytag = alloca(len + 1);
2220 snprintf(entrytag, len + 1, tagstring, savedtypes.pi->file);
2221 ddata.data = entrytag;
2222 cnp_debug("Insert %s\n", (char *)ddata.data);
2223 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
2224 ecore_x_dnd_send_finished();
2227 else if (dropable->types & ELM_SEL_FORMAT_IMAGE)
2229 cnp_debug("Doing image insert (%s)\n", savedtypes.pi->file);
2230 ddata.format = ELM_SEL_FORMAT_IMAGE;
2231 ddata.data = (char *)savedtypes.pi->file;
2232 dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
2233 ecore_x_dnd_send_finished();
2235 pasteimage_free(savedtypes.pi);
2236 savedtypes.pi = NULL;
2242 cnp_debug("Item doesn't support images... passing\n");
2243 pasteimage_free(savedtypes.pi);
2247 else if (savedtypes.textreq)
2249 /* Already asked: Pretend we asked now, and paste immediately when
2251 savedtypes.textreq = 0;
2252 ecore_x_dnd_send_finished();
2257 cnp_debug("doing a request then\n");
2258 selections[ELM_SEL_XDND].requestwidget = dropable->obj;
2259 selections[ELM_SEL_XDND].requestformat = ELM_SEL_FORMAT_MARKUP;
2260 selections[ELM_SEL_XDND].active = EINA_TRUE;
2262 ecore_x_selection_xdnd_request(xwin, atoms[i].name);
2267 _dnd_position(void *data __UNUSED__, int etype __UNUSED__, void *ev)
2269 struct _Ecore_X_Event_Xdnd_Position *pos;
2270 Ecore_X_Rectangle rect;
2274 /* Need to send a status back */
2275 /* FIXME: Should check I can drop here */
2276 /* FIXME: Should highlight widget */
2277 rect.x = pos->position.x - 5;
2278 rect.y = pos->position.y - 5;
2281 ecore_x_dnd_send_status(EINA_TRUE, EINA_FALSE, rect, pos->action);
2287 * When dragging this is callback response from the destination.
2288 * The important thing we care about: Can we drop; thus update cursor
2292 _dnd_status(void *data __UNUSED__, int etype __UNUSED__, void *ev)
2294 struct _Ecore_X_Event_Xdnd_Status *status = ev;
2296 if (!status) return EINA_TRUE;
2298 /* Only thing we care about: will accept */
2299 if (status->will_accept)
2301 cnp_debug("Will accept\n");
2304 { /* Won't accept */
2305 cnp_debug("Won't accept accept\n");
2311 * Add a widget as drop target.
2314 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
2317 Ecore_X_Window xwin;
2321 if (!obj) return EINA_FALSE;
2322 if (!_elm_cnp_init_count) _elm_cnp_init();
2324 /* Is this the first? */
2325 first = (!drops) ? 1 : 0;
2327 EINA_LIST_FOREACH(drops, item, drop)
2329 if (drop->obj == obj)
2331 /* Update: Not a new one */
2332 drop->dropcb = dropcb;
2333 drop->cbdata = cbdata;
2334 drop->types = format;
2339 /* Create new drop */
2340 drop = calloc(1, sizeof(Dropable));
2341 if (!drop) return EINA_FALSE;
2342 /* FIXME: Check for eina's deranged error method */
2343 drops = eina_list_append(drops, drop);
2345 if (!drops/* || or other error */)
2350 drop->dropcb = dropcb;
2351 drop->cbdata = cbdata;
2352 drop->types = format;
2355 evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL,
2356 /* I love C and varargs */
2357 (Evas_Object_Event_Cb)elm_drop_target_del,
2359 /* FIXME: Handle resizes */
2361 /* If not the first: We're done */
2362 if (!first) return EINA_TRUE;
2364 xwin = (Ecore_X_Window)ecore_evas_window_get
2365 (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
2367 ecore_x_dnd_aware_set(xwin, EINA_TRUE);
2369 cnp_debug("Adding drop target calls\n");
2370 handler_enter = ecore_event_handler_add(ECORE_X_EVENT_XDND_ENTER,
2372 handler_pos = ecore_event_handler_add(ECORE_X_EVENT_XDND_POSITION,
2373 _dnd_position, NULL);
2374 handler_drop = ecore_event_handler_add(ECORE_X_EVENT_XDND_DROP,
2381 elm_drop_target_del(Evas_Object *obj)
2383 Dropable *drop,*del;
2385 Ecore_X_Window xwin;
2388 EINA_LIST_FOREACH(drops, item, drop)
2390 if (drop->obj == obj)
2392 drops = eina_list_remove_list(drops, item);
2397 if (!del) return EINA_FALSE;
2399 evas_object_event_callback_del(obj, EVAS_CALLBACK_FREE,
2400 (Evas_Object_Event_Cb)elm_drop_target_del);
2402 /* If still drops there: All fine.. continue */
2403 if (drops) return EINA_TRUE;
2405 cnp_debug("Disabling DND\n");
2406 xwin = (Ecore_X_Window)ecore_evas_window_get
2407 (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
2408 ecore_x_dnd_aware_set(xwin, EINA_FALSE);
2410 ecore_event_handler_del(handler_pos);
2411 ecore_event_handler_del(handler_drop);
2412 ecore_event_handler_del(handler_enter);
2416 pasteimage_free(savedtypes.pi);
2417 savedtypes.pi = NULL;
2425 _drag_mouse_up(void *un __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *data __UNUSED__)
2427 evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_UP, _drag_mouse_up);
2431 dragdonecb(dragdonecb,selections[ELM_SEL_XDND].widget);
2436 evas_object_del(dragwin);
2442 _drag_move(void *data __UNUSED__, Ecore_X_Xdnd_Position *pos)
2444 evas_object_move(dragwin,
2445 pos->position.x - _dragx,
2446 pos->position.y - _dragy);
2451 elm_drag_start(Evas_Object *obj, Elm_Sel_Format format, const char *data, void (*dragdone) (void *data, Evas_Object *), void *donecbdata)
2453 Ecore_X_Window xwin;
2455 Elm_Sel_Type xdnd = ELM_SEL_XDND;
2457 int x, y, x2, y2, x3, y3;
2461 if (!_elm_cnp_init_count) _elm_cnp_init();
2463 xwin = elm_win_xwindow_get(obj);
2465 cnp_debug("starting drag...\n");
2467 ecore_x_dnd_type_set(xwin, "text/uri-list", 1);
2468 sel = selections + ELM_SEL_XDND;
2471 sel->format = format;
2472 sel->selbuf = data ? strdup(data) : NULL;
2473 dragdonecb = dragdone;
2474 dragdonedata = donecbdata;
2476 ecore_x_dnd_callback_pos_update_set(_drag_move, NULL);
2477 ecore_x_dnd_begin(xwin, (unsigned char *)&xdnd, sizeof(Elm_Sel_Type));
2478 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
2479 _drag_mouse_up, NULL);
2481 handler_status = ecore_event_handler_add(ECORE_X_EVENT_XDND_STATUS,
2484 dragwin = elm_win_add(NULL, "Elm Drag Object", ELM_WIN_UTILITY);
2485 elm_win_override_set(dragwin, 1);
2487 /* FIXME: Images only */
2488 icon = elm_icon_add(dragwin);
2489 elm_icon_file_set(icon, data + 7, NULL); /* 7!? "file://" */
2490 elm_win_resize_object_add(dragwin,icon);
2491 evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
2492 evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, EVAS_HINT_FILL);
2494 /* Position subwindow appropriately */
2495 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(obj));
2496 ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
2497 evas_object_geometry_get(obj, &x2, &y2, &w, &h);
2500 evas_object_move(dragwin, x, y);
2501 evas_object_resize(icon, w, h);
2502 evas_object_resize(dragwin, w, h);
2504 evas_object_show(icon);
2505 evas_object_show(dragwin);
2507 evas_pointer_canvas_xy_get(evas_object_evas_get(obj), &x3, &y3);
2515 elm_cnp_tempfile_create(int size)
2518 const char *tmppath;
2521 info = malloc(sizeof(Tmp_Info));
2522 if (!info) return NULL;
2524 tmppath = getenv("TMP");
2525 if (!tmppath) tmppath = P_tmpdir;
2526 if (!tmppath) tmppath = "/tmp";
2527 len = snprintf(NULL, 0, "%s/%sXXXXXX", tmppath, "elmcnpitem-");
2534 info->filename = malloc(len);
2535 if (!info->filename)
2540 snprintf(info->filename,len,"%s/%sXXXXXX", tmppath, "elmcnpitem-");
2542 info->fd = mkstemp(info->filename);
2547 /* And before someone says anything see POSIX 1003.1-2008 page 400 */
2550 pid = (long)getpid();
2551 /* Use pid instead of /proc/self: That way if can be passed around */
2552 len = snprintf(NULL,0,"/proc/%li/fd/%i", pid, info->fd);
2557 snprintf(tmp,len, "/proc/%li/fd/%i", pid, info->fd);
2558 unlink(info->filename);
2559 free(info->filename);
2560 info->filename = tmp;
2565 cnp_debug("filename is %s\n", info->filename);
2568 /* Set map to NULL and return */
2575 if (ftruncate(info->fd, size))
2577 perror("ftruncate");
2583 info->map = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, info->fd, 0);
2584 if (info->map == MAP_FAILED)
2596 tmpinfo_free(Tmp_Info *info)
2598 if (!info) return 0;
2599 free(info->filename);
2605 /* Stubs for windows */
2607 elm_drag_start(Evas_Object *o, Elm_Sel_Format f, const char *d, void (*donecb)(void *, Evas_Object *),void *cbdata)
2613 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
2619 elm_drop_target_del(Evas_Object *o)
2625 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-2f0^-2{2(0W1st0 :*/