4 * Copyright 2003 Ulrich Czekalla
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
37 #include "shlobj.h" /* DROPFILES */
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(xdnd
);
43 /* Maximum wait time for selection notify */
44 #define SELECTION_RETRIES 500 /* wait for .1 seconds */
45 #define SELECTION_WAIT 1000 /* us */
47 typedef struct tagXDNDDATA
53 struct tagXDNDDATA
*next
;
54 } XDNDDATA
, *LPXDNDDATA
;
56 static LPXDNDDATA XDNDData
= NULL
;
57 static POINT XDNDxy
= { 0, 0 };
59 static void X11DRV_XDND_InsertXDNDData(int property
, int format
, void* data
, unsigned int len
);
60 static int X11DRV_XDND_DeconstructTextPlain(int property
, void* data
, int len
);
61 static int X11DRV_XDND_DeconstructTextHTML(int property
, void* data
, int len
);
62 static int X11DRV_XDND_MapFormat(unsigned int property
, unsigned char *data
, int len
);
63 static void X11DRV_XDND_ResolveProperty(Display
*display
, Window xwin
, Time tm
,
64 Atom
*types
, unsigned long *count
);
65 static void X11DRV_XDND_SendDropFiles(HWND hwnd
);
66 static void X11DRV_XDND_FreeDragDropOp(void);
67 static unsigned int X11DRV_XDND_UnixToDos(char** lpdest
, char* lpsrc
, int len
);
68 static DROPFILES
* X11DRV_XDND_BuildDropFiles(char* filename
, unsigned int len
, POINT pt
);
70 static CRITICAL_SECTION xdnd_cs
;
71 static CRITICAL_SECTION_DEBUG critsect_debug
=
74 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
75 0, 0, { (DWORD_PTR
)(__FILE__
": xdnd_cs") }
77 static CRITICAL_SECTION xdnd_cs
= { &critsect_debug
, -1, 0, 0, 0, 0 };
80 /**************************************************************************
81 * X11DRV_XDND_EnterEvent
83 * Handle an XdndEnter event.
85 void X11DRV_XDND_EnterEvent( HWND hWnd
, XClientMessageEvent
*event
)
88 unsigned long count
= 0;
90 TRACE("ver(%ld) check-XdndTypeList(%ld) data=%ld,%ld,%ld,%ld,%ld\n",
91 (event
->data
.l
[1] & 0xFF000000) >> 24, (event
->data
.l
[1] & 1),
92 event
->data
.l
[0], event
->data
.l
[1], event
->data
.l
[2],
93 event
->data
.l
[3], event
->data
.l
[4]);
95 /* If the source supports more than 3 data types we retrieve
97 if (event
->data
.l
[1] & 1)
101 unsigned long bytesret
;
103 /* Request supported formats from source window */
105 XGetWindowProperty(event
->display
, event
->data
.l
[0], x11drv_atom(XdndTypeList
),
106 0, 65535, FALSE
, AnyPropertyType
, &acttype
, &actfmt
, &count
,
107 &bytesret
, (unsigned char**)&xdndtypes
);
113 xdndtypes
= (Atom
*) &event
->data
.l
[2];
121 for (; i
< count
; i
++)
123 if (xdndtypes
[i
] != 0)
125 char * pn
= XGetAtomName(event
->display
, xdndtypes
[i
]);
126 TRACE("XDNDEnterAtom %ld: %s\n", xdndtypes
[i
], pn
);
133 /* Do a one-time data read and cache results */
134 X11DRV_XDND_ResolveProperty(event
->display
, event
->window
,
135 event
->data
.l
[1], xdndtypes
, &count
);
137 if (event
->data
.l
[1] & 1)
141 /**************************************************************************
142 * X11DRV_XDND_PositionEvent
144 * Handle an XdndPosition event.
146 void X11DRV_XDND_PositionEvent( HWND hWnd
, XClientMessageEvent
*event
)
148 XClientMessageEvent e
;
149 int accept
= 0; /* Assume we're not accepting */
151 XDNDxy
.x
= event
->data
.l
[2] >> 16;
152 XDNDxy
.y
= event
->data
.l
[2] & 0xFFFF;
154 /* FIXME: Notify OLE of DragEnter. Result determines if we accept */
156 if (GetWindowLongW( hWnd
, GWL_EXSTYLE
) & WS_EX_ACCEPTFILES
)
159 TRACE("action req: %ld accept(%d) at x(%d),y(%d)\n",
160 event
->data
.l
[4], accept
, XDNDxy
.x
, XDNDxy
.y
);
163 * Let source know if we're accepting the drop by
164 * sending a status message.
166 e
.type
= ClientMessage
;
167 e
.display
= event
->display
;
168 e
.window
= event
->data
.l
[0];
169 e
.message_type
= x11drv_atom(XdndStatus
);
171 e
.data
.l
[0] = event
->window
;
172 e
.data
.l
[1] = accept
;
173 e
.data
.l
[2] = 0; /* Empty Rect */
174 e
.data
.l
[3] = 0; /* Empty Rect */
176 e
.data
.l
[4] = event
->data
.l
[4];
180 XSendEvent(event
->display
, event
->data
.l
[0], False
, NoEventMask
, (XEvent
*)&e
);
183 /* FIXME: if drag accepted notify OLE of DragOver */
186 /**************************************************************************
187 * X11DRV_XDND_DropEvent
189 * Handle an XdndDrop event.
191 void X11DRV_XDND_DropEvent( HWND hWnd
, XClientMessageEvent
*event
)
193 XClientMessageEvent e
;
197 /* If we have a HDROP type we send a WM_ACCEPTFILES.*/
198 if (GetWindowLongW( hWnd
, GWL_EXSTYLE
) & WS_EX_ACCEPTFILES
)
199 X11DRV_XDND_SendDropFiles( hWnd
);
201 /* FIXME: Notify OLE of Drop */
202 X11DRV_XDND_FreeDragDropOp();
204 /* Tell the target we are finished. */
205 memset(&e
, 0, sizeof(e
));
206 e
.type
= ClientMessage
;
207 e
.display
= event
->display
;
208 e
.window
= event
->data
.l
[0];
209 e
.message_type
= x11drv_atom(XdndFinished
);
211 e
.data
.l
[0] = event
->window
;
213 XSendEvent(event
->display
, event
->data
.l
[0], False
, NoEventMask
, (XEvent
*)&e
);
217 /**************************************************************************
218 * X11DRV_XDND_LeaveEvent
220 * Handle an XdndLeave event.
222 void X11DRV_XDND_LeaveEvent( HWND hWnd
, XClientMessageEvent
*event
)
224 TRACE("DND Operation canceled\n");
226 X11DRV_XDND_FreeDragDropOp();
228 /* FIXME: Notify OLE of DragLeave */
232 /**************************************************************************
233 * X11DRV_XDND_ResolveProperty
235 * Resolve all MIME types to windows clipboard formats. All data is cached.
237 static void X11DRV_XDND_ResolveProperty(Display
*display
, Window xwin
, Time tm
,
238 Atom
*types
, unsigned long *count
)
245 unsigned long bytesret
, icount
;
247 unsigned char* data
= NULL
;
249 TRACE("count(%ld)\n", *count
);
251 X11DRV_XDND_FreeDragDropOp(); /* Clear previously cached data */
253 for (i
= 0; i
< *count
; i
++)
255 TRACE("requesting atom %ld from xwin %ld\n", types
[i
], xwin
);
261 XConvertSelection(display
, x11drv_atom(XdndSelection
), types
[i
],
262 x11drv_atom(XdndTarget
), xwin
, /*tm*/CurrentTime
);
266 * Wait for SelectionNotify
268 for (j
= 0; j
< SELECTION_RETRIES
; j
++)
271 res
= XCheckTypedWindowEvent(display
, xwin
, SelectionNotify
, &xe
);
273 if (res
&& xe
.xselection
.selection
== x11drv_atom(XdndSelection
)) break;
275 usleep(SELECTION_WAIT
);
278 if (xe
.xselection
.property
== None
)
282 XGetWindowProperty(display
, xwin
, x11drv_atom(XdndTarget
), 0, 65535, FALSE
,
283 AnyPropertyType
, &acttype
, &actfmt
, &icount
, &bytesret
, &data
);
286 entries
+= X11DRV_XDND_MapFormat(types
[i
], data
, icount
* (actfmt
/ 8));
296 /**************************************************************************
297 * X11DRV_XDND_InsertXDNDData
299 * Cache available XDND property
301 static void X11DRV_XDND_InsertXDNDData(int property
, int format
, void* data
, unsigned int len
)
303 LPXDNDDATA current
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(XDNDDATA
));
307 EnterCriticalSection(&xdnd_cs
);
308 current
->next
= XDNDData
;
309 current
->cf_xdnd
= property
;
310 current
->cf_win
= format
;
311 current
->data
= data
;
314 LeaveCriticalSection(&xdnd_cs
);
319 /**************************************************************************
320 * X11DRV_XDND_MapFormat
322 * Map XDND MIME format to windows clipboard format.
324 static int X11DRV_XDND_MapFormat(unsigned int property
, unsigned char *data
, int len
)
329 TRACE("%d: %s\n", property
, data
);
331 /* Always include the raw type */
332 xdata
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
);
333 memcpy(xdata
, data
, len
);
334 X11DRV_XDND_InsertXDNDData(property
, property
, xdata
, len
);
337 if (property
== x11drv_atom(text_plain
))
338 count
+= X11DRV_XDND_DeconstructTextPlain(property
, data
, len
);
339 else if (property
== x11drv_atom(text_html
))
340 count
+= X11DRV_XDND_DeconstructTextHTML(property
, data
, len
);
346 /**************************************************************************
347 * X11DRV_XDND_DeconstructTextPlain
349 * Interpret text/plain Data and add records to <dndfmt> linked list
351 static int X11DRV_XDND_DeconstructTextPlain(int property
, void* data
, int len
)
353 char *p
= (char*) data
;
357 /* Always suppply plain text */
358 X11DRV_XDND_UnixToDos(&dostext
, (char*)data
, len
);
359 X11DRV_XDND_InsertXDNDData(property
, CF_TEXT
, dostext
, strlen(dostext
));
362 TRACE("CF_TEXT (%d): %s\n", CF_TEXT
, dostext
);
364 /* Check for additional mappings */
365 while (*p
!= '\0' && *p
!= ':') /* Advance to end of protocol */
370 if (!strncasecmp(data
, "http", 4))
372 X11DRV_XDND_InsertXDNDData(property
, RegisterClipboardFormatA("UniformResourceLocator"),
373 strdup(dostext
), strlen(dostext
));
376 TRACE("UniformResourceLocator: %s\n", dostext
);
378 else if (!strncasecmp(data
, "file", 4))
382 pdf
= X11DRV_XDND_BuildDropFiles(p
+1, len
- 5, XDNDxy
);
385 unsigned int size
= HeapSize(GetProcessHeap(), 0, pdf
);
387 X11DRV_XDND_InsertXDNDData(property
, CF_HDROP
, pdf
, size
);
391 TRACE("CF_HDROP: %p\n", pdf
);
399 /**************************************************************************
400 * X11DRV_XDND_DeconstructTextHTML
402 * Interpret text/html data and add records to <dndfmt> linked list
404 static int X11DRV_XDND_DeconstructTextHTML(int property
, void* data
, int len
)
408 X11DRV_XDND_UnixToDos(&dostext
, data
, len
);
410 X11DRV_XDND_InsertXDNDData(property
,
411 RegisterClipboardFormatA("UniformResourceLocator"), dostext
, strlen(dostext
));
413 TRACE("UniformResourceLocator: %s\n", dostext
);
419 /**************************************************************************
420 * X11DRV_XDND_SendDropFiles
422 static void X11DRV_XDND_SendDropFiles(HWND hwnd
)
426 EnterCriticalSection(&xdnd_cs
);
430 /* Find CF_HDROP type if any */
431 while (current
!= NULL
)
433 if (current
->cf_win
== CF_HDROP
)
435 current
= current
->next
;
440 DROPFILES
*lpDrop
= (DROPFILES
*) current
->data
;
444 lpDrop
->pt
.x
= XDNDxy
.x
;
445 lpDrop
->pt
.y
= XDNDxy
.y
;
447 TRACE("Sending WM_DROPFILES: hWnd(0x%p) %p(%s)\n", hwnd
,
448 ((char*)lpDrop
) + lpDrop
->pFiles
, ((char*)lpDrop
) + lpDrop
->pFiles
);
450 PostMessageA(hwnd
, WM_DROPFILES
, (WPARAM
)lpDrop
, 0L);
454 LeaveCriticalSection(&xdnd_cs
);
458 /**************************************************************************
459 * X11DRV_XDND_FreeDragDropOp
461 static void X11DRV_XDND_FreeDragDropOp(void)
468 EnterCriticalSection(&xdnd_cs
);
472 /** Free data cache */
473 while (current
!= NULL
)
475 next
= current
->next
;
476 HeapFree(GetProcessHeap(), 0, current
);
481 XDNDxy
.x
= XDNDxy
.y
= 0;
483 LeaveCriticalSection(&xdnd_cs
);
488 /**************************************************************************
489 * X11DRV_XDND_BuildDropFiles
491 static DROPFILES
* X11DRV_XDND_BuildDropFiles(char* filename
, unsigned int len
, POINT pt
)
496 DROPFILES
*lpDrop
= NULL
;
498 /* Advance to last starting slash */
500 while (*pfn
&& (*pfn
== '\\' || *pfn
=='/'))
506 /* Remove any trailing \r or \n */
509 if (*pfn
== '\r' || *pfn
== '\n')
517 TRACE("%s\n", filename
);
519 pathlen
= GetLongPathNameA(filename
, path
, MAX_PATH
);
522 lpDrop
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(DROPFILES
) + pathlen
+ 1);
524 lpDrop
->pFiles
= sizeof(DROPFILES
);
528 lpDrop
->fWide
= FALSE
;
530 strcpy(((char*)lpDrop
)+lpDrop
->pFiles
, path
);
533 TRACE("resolved %s\n", lpDrop
? filename
: NULL
);
539 /**************************************************************************
540 * X11DRV_XDND_UnixToDos
542 static unsigned int X11DRV_XDND_UnixToDos(char** lpdest
, char* lpsrc
, int len
)
545 unsigned int destlen
, lines
;
547 for (i
= 0, lines
= 0; i
<= len
; i
++)
549 if (lpsrc
[i
] == '\n')
553 destlen
= len
+ lines
+ 1;
557 char* lpstr
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, destlen
);
558 for (i
= 0, lines
= 0; i
<= len
; i
++)
560 if (lpsrc
[i
] == '\n')
561 lpstr
[++lines
+ i
] = '\r';
562 lpstr
[lines
+ i
] = lpsrc
[i
];