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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
38 #include "shlobj.h" /* DROPFILES */
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(xdnd
);
44 /* Maximum wait time for selection notify */
45 #define SELECTION_RETRIES 500 /* wait for .1 seconds */
46 #define SELECTION_WAIT 1000 /* us */
48 typedef struct tagXDNDDATA
54 struct tagXDNDDATA
*next
;
55 } XDNDDATA
, *LPXDNDDATA
;
57 static LPXDNDDATA XDNDData
= NULL
;
58 static POINT XDNDxy
= { 0, 0 };
60 static void X11DRV_XDND_InsertXDNDData(int property
, int format
, void* data
, unsigned int len
);
61 static int X11DRV_XDND_DeconstructTextPlain(int property
, void* data
, int len
);
62 static int X11DRV_XDND_DeconstructTextHTML(int property
, void* data
, int len
);
63 static int X11DRV_XDND_MapFormat(unsigned int property
, unsigned char *data
, int len
);
64 static void X11DRV_XDND_ResolveProperty(Display
*display
, Window xwin
, Time tm
,
65 Atom
*types
, unsigned long *count
);
66 static void X11DRV_XDND_SendDropFiles(HWND hwnd
);
67 static void X11DRV_XDND_FreeDragDropOp(void);
68 static unsigned int X11DRV_XDND_UnixToDos(char** lpdest
, char* lpsrc
, int len
);
69 static DROPFILES
* X11DRV_XDND_BuildDropFiles(char* filename
, unsigned int len
, POINT pt
);
71 static CRITICAL_SECTION xdnd_cs
;
72 static CRITICAL_SECTION_DEBUG critsect_debug
=
75 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
76 0, 0, { (DWORD_PTR
)(__FILE__
": xdnd_cs") }
78 static CRITICAL_SECTION xdnd_cs
= { &critsect_debug
, -1, 0, 0, 0, 0 };
81 /**************************************************************************
82 * X11DRV_XDND_EnterEvent
84 * Handle an XdndEnter event.
86 void X11DRV_XDND_EnterEvent( HWND hWnd
, XClientMessageEvent
*event
)
89 unsigned long count
= 0;
91 TRACE("ver(%ld) check-XdndTypeList(%ld) data=%ld,%ld,%ld,%ld,%ld\n",
92 (event
->data
.l
[1] & 0xFF000000) >> 24, (event
->data
.l
[1] & 1),
93 event
->data
.l
[0], event
->data
.l
[1], event
->data
.l
[2],
94 event
->data
.l
[3], event
->data
.l
[4]);
96 /* If the source supports more than 3 data types we retrieve
98 if (event
->data
.l
[1] & 1)
102 unsigned long bytesret
;
104 /* Request supported formats from source window */
106 XGetWindowProperty(event
->display
, event
->data
.l
[0], x11drv_atom(XdndTypeList
),
107 0, 65535, FALSE
, AnyPropertyType
, &acttype
, &actfmt
, &count
,
108 &bytesret
, (unsigned char**)&xdndtypes
);
114 xdndtypes
= (Atom
*) &event
->data
.l
[2];
122 for (; i
< count
; i
++)
124 if (xdndtypes
[i
] != 0)
126 char * pn
= XGetAtomName(event
->display
, xdndtypes
[i
]);
127 TRACE("XDNDEnterAtom %ld: %s\n", xdndtypes
[i
], pn
);
134 /* Do a one-time data read and cache results */
135 X11DRV_XDND_ResolveProperty(event
->display
, event
->window
,
136 event
->data
.l
[1], xdndtypes
, &count
);
138 if (event
->data
.l
[1] & 1)
142 /**************************************************************************
143 * X11DRV_XDND_PositionEvent
145 * Handle an XdndPosition event.
147 void X11DRV_XDND_PositionEvent( HWND hWnd
, XClientMessageEvent
*event
)
149 XClientMessageEvent e
;
150 int accept
= 0; /* Assume we're not accepting */
152 XDNDxy
.x
= event
->data
.l
[2] >> 16;
153 XDNDxy
.y
= event
->data
.l
[2] & 0xFFFF;
155 /* FIXME: Notify OLE of DragEnter. Result determines if we accept */
157 if (GetWindowLongW( hWnd
, GWL_EXSTYLE
) & WS_EX_ACCEPTFILES
)
160 TRACE("action req: %ld accept(%d) at x(%ld),y(%ld)\n",
161 event
->data
.l
[4], accept
, XDNDxy
.x
, XDNDxy
.y
);
164 * Let source know if we're accepting the drop by
165 * sending a status message.
167 e
.type
= ClientMessage
;
168 e
.display
= event
->display
;
169 e
.window
= event
->data
.l
[0];
170 e
.message_type
= x11drv_atom(XdndStatus
);
172 e
.data
.l
[0] = event
->window
;
173 e
.data
.l
[1] = accept
;
174 e
.data
.l
[2] = 0; /* Empty Rect */
175 e
.data
.l
[3] = 0; /* Empty Rect */
177 e
.data
.l
[4] = event
->data
.l
[4];
181 XSendEvent(event
->display
, event
->data
.l
[0], False
, NoEventMask
, (XEvent
*)&e
);
184 /* FIXME: if drag accepted notify OLE of DragOver */
187 /**************************************************************************
188 * X11DRV_XDND_DropEvent
190 * Handle an XdndDrop event.
192 void X11DRV_XDND_DropEvent( HWND hWnd
, XClientMessageEvent
*event
)
194 XClientMessageEvent e
;
198 /* If we have a HDROP type we send a WM_ACCEPTFILES.*/
199 if (GetWindowLongW( hWnd
, GWL_EXSTYLE
) & WS_EX_ACCEPTFILES
)
200 X11DRV_XDND_SendDropFiles( hWnd
);
202 /* FIXME: Notify OLE of Drop */
203 X11DRV_XDND_FreeDragDropOp();
205 /* Tell the target we are finished. */
206 memset(&e
, 0, sizeof(e
));
207 e
.type
= ClientMessage
;
208 e
.display
= event
->display
;
209 e
.window
= event
->data
.l
[0];
210 e
.message_type
= x11drv_atom(XdndFinished
);
212 e
.data
.l
[0] = event
->window
;
214 XSendEvent(event
->display
, event
->data
.l
[0], False
, NoEventMask
, (XEvent
*)&e
);
218 /**************************************************************************
219 * X11DRV_XDND_LeaveEvent
221 * Handle an XdndLeave event.
223 void X11DRV_XDND_LeaveEvent( HWND hWnd
, XClientMessageEvent
*event
)
225 TRACE("DND Operation canceled\n");
227 X11DRV_XDND_FreeDragDropOp();
229 /* FIXME: Notify OLE of DragLeave */
233 /**************************************************************************
234 * X11DRV_XDND_ResolveProperty
236 * Resolve all MIME types to windows clipboard formats. All data is cached.
238 static void X11DRV_XDND_ResolveProperty(Display
*display
, Window xwin
, Time tm
,
239 Atom
*types
, unsigned long *count
)
246 unsigned long bytesret
, icount
;
248 unsigned char* data
= NULL
;
250 TRACE("count(%ld)\n", *count
);
252 X11DRV_XDND_FreeDragDropOp(); /* Clear previously cached data */
254 for (i
= 0; i
< *count
; i
++)
256 TRACE("requesting atom %ld from xwin %ld\n", types
[i
], xwin
);
262 XConvertSelection(display
, x11drv_atom(XdndSelection
), types
[i
],
263 x11drv_atom(XdndTarget
), xwin
, /*tm*/CurrentTime
);
267 * Wait for SelectionNotify
269 for (j
= 0; j
< SELECTION_RETRIES
; j
++)
272 res
= XCheckTypedWindowEvent(display
, xwin
, SelectionNotify
, &xe
);
274 if (res
&& xe
.xselection
.selection
== x11drv_atom(XdndSelection
)) break;
276 usleep(SELECTION_WAIT
);
279 if (xe
.xselection
.property
== None
)
283 XGetWindowProperty(display
, xwin
, x11drv_atom(XdndTarget
), 0, 65535, FALSE
,
284 AnyPropertyType
, &acttype
, &actfmt
, &icount
, &bytesret
, &data
);
287 entries
+= X11DRV_XDND_MapFormat(types
[i
], data
, icount
* (actfmt
/ 8));
297 /**************************************************************************
298 * X11DRV_XDND_InsertXDNDData
300 * Cache available XDND property
302 static void X11DRV_XDND_InsertXDNDData(int property
, int format
, void* data
, unsigned int len
)
304 LPXDNDDATA current
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(XDNDDATA
));
308 EnterCriticalSection(&xdnd_cs
);
309 current
->next
= XDNDData
;
310 current
->cf_xdnd
= property
;
311 current
->cf_win
= format
;
312 current
->data
= data
;
315 LeaveCriticalSection(&xdnd_cs
);
320 /**************************************************************************
321 * X11DRV_XDND_MapFormat
323 * Map XDND MIME format to windows clipboard format.
325 static int X11DRV_XDND_MapFormat(unsigned int property
, unsigned char *data
, int len
)
330 TRACE("%d: %s\n", property
, data
);
332 /* Always include the raw type */
333 xdata
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
);
334 memcpy(xdata
, data
, len
);
335 X11DRV_XDND_InsertXDNDData(property
, property
, xdata
, len
);
338 if (property
== x11drv_atom(text_plain
))
339 count
+= X11DRV_XDND_DeconstructTextPlain(property
, data
, len
);
340 else if (property
== x11drv_atom(text_html
))
341 count
+= X11DRV_XDND_DeconstructTextHTML(property
, data
, len
);
347 /**************************************************************************
348 * X11DRV_XDND_DeconstructTextPlain
350 * Interpret text/plain Data and add records to <dndfmt> linked list
352 static int X11DRV_XDND_DeconstructTextPlain(int property
, void* data
, int len
)
354 char *p
= (char*) data
;
358 /* Always suppply plain text */
359 X11DRV_XDND_UnixToDos(&dostext
, (char*)data
, len
);
360 X11DRV_XDND_InsertXDNDData(property
, CF_TEXT
, dostext
, strlen(dostext
));
363 TRACE("CF_TEXT (%d): %s\n", CF_TEXT
, dostext
);
365 /* Check for additional mappings */
366 while (*p
!= '\0' && *p
!= ':') /* Advance to end of protocol */
371 if (!strncasecmp(data
, "http", 4))
373 X11DRV_XDND_InsertXDNDData(property
, RegisterClipboardFormatA("UniformResourceLocator"),
374 strdup(dostext
), strlen(dostext
));
377 TRACE("UniformResourceLocator: %s\n", dostext
);
379 else if (!strncasecmp(data
, "file", 4))
383 pdf
= X11DRV_XDND_BuildDropFiles(p
+1, len
- 5, XDNDxy
);
386 unsigned int size
= HeapSize(GetProcessHeap(), 0, pdf
);
388 X11DRV_XDND_InsertXDNDData(property
, CF_HDROP
, pdf
, size
);
392 TRACE("CF_HDROP: %p\n", pdf
);
400 /**************************************************************************
401 * X11DRV_XDND_DeconstructTextHTML
403 * Interpret text/html data and add records to <dndfmt> linked list
405 static int X11DRV_XDND_DeconstructTextHTML(int property
, void* data
, int len
)
409 X11DRV_XDND_UnixToDos(&dostext
, data
, len
);
411 X11DRV_XDND_InsertXDNDData(property
,
412 RegisterClipboardFormatA("UniformResourceLocator"), dostext
, strlen(dostext
));
414 TRACE("UniformResourceLocator: %s\n", dostext
);
420 /**************************************************************************
421 * X11DRV_XDND_SendDropFiles
423 static void X11DRV_XDND_SendDropFiles(HWND hwnd
)
427 EnterCriticalSection(&xdnd_cs
);
431 /* Find CF_HDROP type if any */
432 while (current
!= NULL
)
434 if (current
->cf_win
== CF_HDROP
)
436 current
= current
->next
;
441 DROPFILES
*lpDrop
= (DROPFILES
*) current
->data
;
445 lpDrop
->pt
.x
= XDNDxy
.x
;
446 lpDrop
->pt
.y
= XDNDxy
.y
;
448 TRACE("Sending WM_DROPFILES: hWnd(0x%p) %p(%s)\n", hwnd
,
449 ((char*)lpDrop
) + lpDrop
->pFiles
, ((char*)lpDrop
) + lpDrop
->pFiles
);
451 PostMessageA(hwnd
, WM_DROPFILES
, (WPARAM
)lpDrop
, 0L);
455 LeaveCriticalSection(&xdnd_cs
);
459 /**************************************************************************
460 * X11DRV_XDND_FreeDragDropOp
462 static void X11DRV_XDND_FreeDragDropOp(void)
469 EnterCriticalSection(&xdnd_cs
);
473 /** Free data cache */
474 while (current
!= NULL
)
476 next
= current
->next
;
477 HeapFree(GetProcessHeap(), 0, current
);
482 XDNDxy
.x
= XDNDxy
.y
= 0;
484 LeaveCriticalSection(&xdnd_cs
);
489 /**************************************************************************
490 * X11DRV_XDND_BuildDropFiles
492 static DROPFILES
* X11DRV_XDND_BuildDropFiles(char* filename
, unsigned int len
, POINT pt
)
497 DROPFILES
*lpDrop
= NULL
;
499 /* Advance to last starting slash */
501 while (*pfn
&& (*pfn
== '\\' || *pfn
=='/'))
507 /* Remove any trailing \r or \n */
510 if (*pfn
== '\r' || *pfn
== '\n')
518 TRACE("%s\n", filename
);
520 pathlen
= GetLongPathNameA(filename
, path
, MAX_PATH
);
523 lpDrop
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(DROPFILES
) + pathlen
+ 1);
525 lpDrop
->pFiles
= sizeof(DROPFILES
);
529 lpDrop
->fWide
= FALSE
;
531 strcpy(((char*)lpDrop
)+lpDrop
->pFiles
, path
);
534 TRACE("resolved %s\n", lpDrop
? filename
: NULL
);
540 /**************************************************************************
541 * X11DRV_XDND_UnixToDos
543 static unsigned int X11DRV_XDND_UnixToDos(char** lpdest
, char* lpsrc
, int len
)
546 unsigned int destlen
, lines
;
548 for (i
= 0, lines
= 0; i
<= len
; i
++)
550 if (lpsrc
[i
] == '\n')
554 destlen
= len
+ lines
+ 1;
558 char* lpstr
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, destlen
);
559 for (i
= 0, lines
= 0; i
<= len
; i
++)
561 if (lpsrc
[i
] == '\n')
562 lpstr
[++lines
+ i
] = '\r';
563 lpstr
[lines
+ i
] = lpsrc
[i
];