2 * X11 clipboard windows driver
4 * Copyright 1994 Martin Ayotte
7 * 2003 Ulrich Czekalla for CodeWeavers
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * This file contains the X specific implementation for the windows
27 * Wine's internal clipboard is exposed to external apps via the X
28 * selection mechanism.
29 * Currently the driver asserts ownership via two selection atoms:
30 * 1. PRIMARY(XA_PRIMARY)
33 * In our implementation, the CLIPBOARD selection takes precedence over PRIMARY,
34 * i.e. if a CLIPBOARD selection is available, it is used instead of PRIMARY.
35 * When Wine takes ownership of the clipboard, it takes ownership of BOTH selections.
36 * While giving up selection ownership, if the CLIPBOARD selection is lost,
37 * it will lose both PRIMARY and CLIPBOARD and empty the clipboard.
38 * However if only PRIMARY is lost, it will continue to hold the CLIPBOARD selection
39 * (leaving the clipboard cache content unaffected).
41 * Every format exposed via a windows clipboard format is also exposed through
42 * a corresponding X selection target. A selection target atom is synthesized
43 * whenever a new Windows clipboard format is registered via RegisterClipboardFormat,
44 * or when a built-in format is used for the first time.
45 * Windows native format are exposed by prefixing the format name with "<WCF>"
46 * This allows us to uniquely identify windows native formats exposed by other
49 * In order to allow external applications to query WINE for supported formats,
50 * we respond to the "TARGETS" selection target. (See EVENT_SelectionRequest
51 * for implementation) We use the same mechanism to query external clients for
52 * availability of a particular format, by caching the list of available targets
53 * by using the clipboard cache's "delayed render" mechanism. If a selection client
54 * does not support the "TARGETS" selection target, we actually attempt to retrieve
55 * the format requested as a fallback mechanism.
57 * Certain Windows native formats are automatically converted to X native formats
58 * and vice versa. If a native format is available in the selection, it takes
59 * precedence, in order to avoid unnecessary conversions.
61 * FIXME: global format list needs a critical section
65 #include "wine/port.h"
81 #include "wine/wingdi16.h"
83 #include "wine/debug.h"
84 #include "wine/unicode.h"
85 #include "wine/server.h"
87 WINE_DEFAULT_DEBUG_CHANNEL(clipboard
);
89 #define HGDIOBJ_32(handle16) ((HGDIOBJ)(ULONG_PTR)(handle16))
91 /* Maximum wait time for selection notify */
92 #define SELECTION_RETRIES 500 /* wait for .1 seconds */
93 #define SELECTION_WAIT 1000 /* us */
94 /* Minimum seconds that must lapse between owner queries */
95 #define OWNERQUERYLAPSETIME 1
98 #define S_NOSELECTION 0
100 #define S_CLIPBOARD 2
109 } CLIPBOARDINFO
, *LPCLIPBOARDINFO
;
111 struct tagWINE_CLIPDATA
; /* Forward */
113 typedef HANDLE (*DRVEXPORTFUNC
)(Window requestor
, Atom aTarget
, Atom rprop
,
114 struct tagWINE_CLIPDATA
* lpData
, LPDWORD lpBytes
);
115 typedef HANDLE (*DRVIMPORTFUNC
)(Window w
, Atom prop
);
117 typedef struct tagWINE_CLIPFORMAT
{
122 DRVIMPORTFUNC lpDrvImportFunc
;
123 DRVEXPORTFUNC lpDrvExportFunc
;
124 struct tagWINE_CLIPFORMAT
*PrevFormat
;
125 struct tagWINE_CLIPFORMAT
*NextFormat
;
126 } WINE_CLIPFORMAT
, *LPWINE_CLIPFORMAT
;
128 typedef struct tagWINE_CLIPDATA
{
134 LPWINE_CLIPFORMAT lpFormat
;
135 struct tagWINE_CLIPDATA
*PrevData
;
136 struct tagWINE_CLIPDATA
*NextData
;
137 } WINE_CLIPDATA
, *LPWINE_CLIPDATA
;
139 #define CF_FLAG_BUILTINFMT 0x0001 /* Built-in windows format */
140 #define CF_FLAG_UNOWNED 0x0002 /* cached data is not owned */
141 #define CF_FLAG_SYNTHESIZED 0x0004 /* Implicitly converted data */
142 #define CF_FLAG_UNICODE 0x0008 /* Data is in unicode */
144 static int selectionAcquired
= 0; /* Contains the current selection masks */
145 static Window selectionWindow
= None
; /* The top level X window which owns the selection */
146 static Atom selectionCacheSrc
= XA_PRIMARY
; /* The selection source from which the clipboard cache was filled */
148 void X11DRV_EmptyClipboard(BOOL keepunowned
);
149 void X11DRV_EndClipboardUpdate(void);
150 static HANDLE
X11DRV_CLIPBOARD_ImportClipboardData(Window w
, Atom prop
);
151 static HANDLE
X11DRV_CLIPBOARD_ImportEnhMetaFile(Window w
, Atom prop
);
152 static HANDLE
X11DRV_CLIPBOARD_ImportMetaFilePict(Window w
, Atom prop
);
153 static HANDLE
X11DRV_CLIPBOARD_ImportXAPIXMAP(Window w
, Atom prop
);
154 static HANDLE
X11DRV_CLIPBOARD_ImportXAString(Window w
, Atom prop
);
155 static HANDLE
X11DRV_CLIPBOARD_ImportUTF8(Window w
, Atom prop
);
156 static HANDLE
X11DRV_CLIPBOARD_ImportCompoundText(Window w
, Atom prop
);
157 static HANDLE
X11DRV_CLIPBOARD_ExportClipboardData(Window requestor
, Atom aTarget
,
158 Atom rprop
, LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
);
159 static HANDLE
X11DRV_CLIPBOARD_ExportString(Window requestor
, Atom aTarget
,
160 Atom rprop
, LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
);
161 static HANDLE
X11DRV_CLIPBOARD_ExportXAPIXMAP(Window requestor
, Atom aTarget
,
162 Atom rprop
, LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
);
163 static HANDLE
X11DRV_CLIPBOARD_ExportMetaFilePict(Window requestor
, Atom aTarget
,
164 Atom rprop
, LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
);
165 static HANDLE
X11DRV_CLIPBOARD_ExportEnhMetaFile(Window requestor
, Atom aTarget
,
166 Atom rprop
, LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
);
167 static WINE_CLIPFORMAT
*X11DRV_CLIPBOARD_InsertClipboardFormat(LPCWSTR FormatName
, Atom prop
);
168 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedText(UINT wFormatID
);
169 static void X11DRV_CLIPBOARD_FreeData(LPWINE_CLIPDATA lpData
);
170 static BOOL
X11DRV_CLIPBOARD_IsSelectionOwner(void);
171 static int X11DRV_CLIPBOARD_QueryAvailableData(LPCLIPBOARDINFO lpcbinfo
);
172 static BOOL
X11DRV_CLIPBOARD_ReadSelectionData(LPWINE_CLIPDATA lpData
);
173 static BOOL
X11DRV_CLIPBOARD_ReadProperty(Window w
, Atom prop
,
174 unsigned char** data
, unsigned long* datasize
);
175 static BOOL
X11DRV_CLIPBOARD_RenderFormat(LPWINE_CLIPDATA lpData
);
176 static HANDLE
X11DRV_CLIPBOARD_SerializeMetafile(INT wformat
, HANDLE hdata
, LPDWORD lpcbytes
, BOOL out
);
177 static BOOL
X11DRV_CLIPBOARD_SynthesizeData(UINT wFormatID
);
178 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedFormat(LPWINE_CLIPDATA lpData
);
179 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedDIB(void);
180 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedBitmap(void);
181 static void X11DRV_HandleSelectionRequest( HWND hWnd
, XSelectionRequestEvent
*event
, BOOL bIsMultiple
);
184 * WARNING: This data ordering is dependent on the WINE_CLIPFORMAT structure
185 * declared in clipboard.h
187 static const WCHAR wszCF_TEXT
[] = {'W','C','F','_','T','E','X','T',0};
188 static const WCHAR wszCF_BITMAP
[] = {'W','C','F','_','B','I','T','M','A','P',0};
189 static const WCHAR wszCF_METAFILEPICT
[] = {'W','C','F','_','M','E','T','A','F','I','L','E','P','I','C','T',0};
190 static const WCHAR wszCF_SYLK
[] = {'W','C','F','_','S','Y','L','K',0};
191 static const WCHAR wszCF_DIF
[] = {'W','C','F','_','D','I','F',0};
192 static const WCHAR wszCF_TIFF
[] = {'W','C','F','_','T','I','F','F',0};
193 static const WCHAR wszCF_OEMTEXT
[] = {'W','C','F','_','O','E','M','T','E','X','T',0};
194 static const WCHAR wszCF_DIB
[] = {'W','C','F','_','D','I','B',0};
195 static const WCHAR wszCF_PALETTE
[] = {'W','C','F','_','P','A','L','E','T','T','E',0};
196 static const WCHAR wszCF_PENDATA
[] = {'W','C','F','_','P','E','N','D','A','T','A',0};
197 static const WCHAR wszCF_RIFF
[] = {'W','C','F','_','R','I','F','F',0};
198 static const WCHAR wszCF_WAVE
[] = {'W','C','F','_','W','A','V','E',0};
199 static const WCHAR wszCOMPOUNDTEXT
[] = {'C','O','M','P','O','U','N','D','_','T','E','X','T',0};
200 static const WCHAR wszUTF8STRING
[] = {'U','T','F','8','_','S','T','R','I','N','G',0};
201 static const WCHAR wszCF_ENHMETAFILE
[] = {'W','C','F','_','E','N','H','M','E','T','A','F','I','L','E',0};
202 static const WCHAR wszCF_HDROP
[] = {'W','C','F','_','H','D','R','O','P',0};
203 static const WCHAR wszCF_LOCALE
[] = {'W','C','F','_','L','O','C','A','L','E',0};
204 static const WCHAR wszCF_DIBV5
[] = {'W','C','F','_','D','I','B','V','5',0};
205 static const WCHAR wszCF_OWNERDISPLAY
[] = {'W','C','F','_','O','W','N','E','R','D','I','S','P','L','A','Y',0};
206 static const WCHAR wszCF_DSPTEXT
[] = {'W','C','F','_','D','S','P','T','E','X','T',0};
207 static const WCHAR wszCF_DSPBITMAP
[] = {'W','C','F','_','D','S','P','B','I','T','M','A','P',0};
208 static const WCHAR wszCF_DSPMETAFILEPICT
[] = {'W','C','F','_','D','S','P','M','E','T','A','F','I','L','E','P','I','C','T',0};
209 static const WCHAR wszCF_DSPENHMETAFILE
[] = {'W','C','F','_','D','S','P','E','N','H','M','E','T','A','F','I','L','E',0};
211 static WINE_CLIPFORMAT ClipFormats
[] =
213 { CF_TEXT
, wszCF_TEXT
, XA_STRING
, CF_FLAG_BUILTINFMT
, X11DRV_CLIPBOARD_ImportXAString
,
214 X11DRV_CLIPBOARD_ExportString
, NULL
, &ClipFormats
[1]},
216 { CF_BITMAP
, wszCF_BITMAP
, 0, CF_FLAG_BUILTINFMT
, X11DRV_CLIPBOARD_ImportClipboardData
,
217 NULL
, &ClipFormats
[0], &ClipFormats
[2]},
219 { CF_METAFILEPICT
, wszCF_METAFILEPICT
, 0, CF_FLAG_BUILTINFMT
, X11DRV_CLIPBOARD_ImportMetaFilePict
,
220 X11DRV_CLIPBOARD_ExportMetaFilePict
, &ClipFormats
[1], &ClipFormats
[3]},
222 { CF_SYLK
, wszCF_SYLK
, 0, CF_FLAG_BUILTINFMT
, X11DRV_CLIPBOARD_ImportClipboardData
,
223 X11DRV_CLIPBOARD_ExportClipboardData
, &ClipFormats
[2], &ClipFormats
[4]},
225 { CF_DIF
, wszCF_DIF
, 0, CF_FLAG_BUILTINFMT
, X11DRV_CLIPBOARD_ImportClipboardData
,
226 X11DRV_CLIPBOARD_ExportClipboardData
, &ClipFormats
[3], &ClipFormats
[5]},
228 { CF_TIFF
, wszCF_TIFF
, 0, CF_FLAG_BUILTINFMT
, X11DRV_CLIPBOARD_ImportClipboardData
,
229 X11DRV_CLIPBOARD_ExportClipboardData
, &ClipFormats
[4], &ClipFormats
[6]},
231 { CF_OEMTEXT
, wszCF_OEMTEXT
, 0, CF_FLAG_BUILTINFMT
, X11DRV_CLIPBOARD_ImportClipboardData
,
232 X11DRV_CLIPBOARD_ExportClipboardData
, &ClipFormats
[5], &ClipFormats
[7]},
234 { CF_DIB
, wszCF_DIB
, XA_PIXMAP
, CF_FLAG_BUILTINFMT
, X11DRV_CLIPBOARD_ImportXAPIXMAP
,
235 X11DRV_CLIPBOARD_ExportXAPIXMAP
, &ClipFormats
[6], &ClipFormats
[8]},
237 { CF_PALETTE
, wszCF_PALETTE
, 0, CF_FLAG_BUILTINFMT
, X11DRV_CLIPBOARD_ImportClipboardData
,
238 X11DRV_CLIPBOARD_ExportClipboardData
, &ClipFormats
[7], &ClipFormats
[9]},
240 { CF_PENDATA
, wszCF_PENDATA
, 0, CF_FLAG_BUILTINFMT
, X11DRV_CLIPBOARD_ImportClipboardData
,
241 X11DRV_CLIPBOARD_ExportClipboardData
, &ClipFormats
[8], &ClipFormats
[10]},
243 { CF_RIFF
, wszCF_RIFF
, 0, CF_FLAG_BUILTINFMT
, X11DRV_CLIPBOARD_ImportClipboardData
,
244 X11DRV_CLIPBOARD_ExportClipboardData
, &ClipFormats
[9], &ClipFormats
[11]},
246 { CF_WAVE
, wszCF_WAVE
, 0, CF_FLAG_BUILTINFMT
, X11DRV_CLIPBOARD_ImportClipboardData
,
247 X11DRV_CLIPBOARD_ExportClipboardData
, &ClipFormats
[10], &ClipFormats
[12]},
249 { CF_UNICODETEXT
, wszUTF8STRING
, 0, CF_FLAG_BUILTINFMT
, X11DRV_CLIPBOARD_ImportUTF8
,
250 X11DRV_CLIPBOARD_ExportString
, &ClipFormats
[11], &ClipFormats
[13]},
252 /* If UTF8_STRING is not available, attempt COMPUND_TEXT */
253 { CF_UNICODETEXT
, wszCOMPOUNDTEXT
, 0, CF_FLAG_BUILTINFMT
, X11DRV_CLIPBOARD_ImportCompoundText
,
254 X11DRV_CLIPBOARD_ExportString
, &ClipFormats
[12], &ClipFormats
[14]},
256 { CF_ENHMETAFILE
, wszCF_ENHMETAFILE
, 0, CF_FLAG_BUILTINFMT
, X11DRV_CLIPBOARD_ImportEnhMetaFile
,
257 X11DRV_CLIPBOARD_ExportEnhMetaFile
, &ClipFormats
[13], &ClipFormats
[15]},
259 { CF_HDROP
, wszCF_HDROP
, 0, CF_FLAG_BUILTINFMT
, X11DRV_CLIPBOARD_ImportClipboardData
,
260 X11DRV_CLIPBOARD_ExportClipboardData
, &ClipFormats
[14], &ClipFormats
[16]},
262 { CF_LOCALE
, wszCF_LOCALE
, 0, CF_FLAG_BUILTINFMT
, X11DRV_CLIPBOARD_ImportClipboardData
,
263 X11DRV_CLIPBOARD_ExportClipboardData
, &ClipFormats
[15], &ClipFormats
[17]},
265 { CF_DIBV5
, wszCF_DIBV5
, 0, CF_FLAG_BUILTINFMT
, X11DRV_CLIPBOARD_ImportClipboardData
,
266 X11DRV_CLIPBOARD_ExportClipboardData
, &ClipFormats
[16], &ClipFormats
[18]},
268 { CF_OWNERDISPLAY
, wszCF_OWNERDISPLAY
, 0, CF_FLAG_BUILTINFMT
, X11DRV_CLIPBOARD_ImportClipboardData
,
269 X11DRV_CLIPBOARD_ExportClipboardData
, &ClipFormats
[17], &ClipFormats
[19]},
271 { CF_DSPTEXT
, wszCF_DSPTEXT
, 0, CF_FLAG_BUILTINFMT
, X11DRV_CLIPBOARD_ImportClipboardData
,
272 X11DRV_CLIPBOARD_ExportClipboardData
, &ClipFormats
[18], &ClipFormats
[20]},
274 { CF_DSPBITMAP
, wszCF_DSPBITMAP
, 0, CF_FLAG_BUILTINFMT
, X11DRV_CLIPBOARD_ImportClipboardData
,
275 X11DRV_CLIPBOARD_ExportClipboardData
, &ClipFormats
[19], &ClipFormats
[21]},
277 { CF_DSPMETAFILEPICT
, wszCF_DSPMETAFILEPICT
, 0, CF_FLAG_BUILTINFMT
, X11DRV_CLIPBOARD_ImportClipboardData
,
278 X11DRV_CLIPBOARD_ExportClipboardData
, &ClipFormats
[20], &ClipFormats
[22]},
280 { CF_DSPENHMETAFILE
, wszCF_DSPENHMETAFILE
, 0, CF_FLAG_BUILTINFMT
, X11DRV_CLIPBOARD_ImportClipboardData
,
281 X11DRV_CLIPBOARD_ExportClipboardData
, &ClipFormats
[21], NULL
}
284 #define GET_ATOM(prop) (((prop) < FIRST_XATOM) ? (Atom)(prop) : X11DRV_Atoms[(prop) - FIRST_XATOM])
286 /* Maps X properties to Windows formats */
287 static const WCHAR wszRichTextFormat
[] = {'R','i','c','h',' ','T','e','x','t',' ','F','o','r','m','a','t',0};
288 static const WCHAR wszGIF
[] = {'G','I','F',0};
289 static const WCHAR wszHTMLFormat
[] = {'H','T','M','L',' ','F','o','r','m','a','t',0};
290 static const WCHAR wszRICHHTMLFormat
[] = {'H','T','M','L',' ','F','o','r','m','a','t',0};
295 } PropertyFormatMap
[] =
297 { wszRichTextFormat
, XATOM_text_rtf
},
298 { wszRichTextFormat
, XATOM_text_richtext
},
299 { wszGIF
, XATOM_image_gif
},
300 { wszHTMLFormat
, XATOM_text_html
},
304 /* Maps equivalent X properties. It is assumed that lpszProperty must already
305 be in ClipFormats or PropertyFormatMap. */
308 UINT drvDataProperty
;
310 } PropertyAliasMap
[] =
312 /* DataProperty, DataAlias */
317 * Cached clipboard data.
319 static LPWINE_CLIPDATA ClipData
= NULL
;
320 static UINT ClipDataCount
= 0;
323 * Clipboard sequence number
325 static UINT wSeqNo
= 0;
327 /**************************************************************************
328 * Internal Clipboard implementation methods
329 **************************************************************************/
331 static Window
thread_selection_wnd(void)
333 Window w
= x11drv_thread_data()->selection_wnd
;
338 w
= XCreateWindow(thread_display(), root_window
, 0, 0, 1, 1, 0, screen_depth
,
339 InputOutput
, CopyFromParent
, 0, NULL
);
343 x11drv_thread_data()->selection_wnd
= w
;
345 FIXME("Failed to create window. Fetching selection data will fail.\n");
351 /**************************************************************************
352 * X11DRV_InitClipboard
354 void X11DRV_InitClipboard(void)
358 /* Register known mapping between window formats and X properties */
359 for (i
= 0; i
< sizeof(PropertyFormatMap
)/sizeof(PropertyFormatMap
[0]); i
++)
360 X11DRV_CLIPBOARD_InsertClipboardFormat(PropertyFormatMap
[i
].lpszFormat
, GET_ATOM(PropertyFormatMap
[i
].prop
));
364 /**************************************************************************
367 * Intern atoms for formats that don't have one yet.
369 static void intern_atoms(void)
371 LPWINE_CLIPFORMAT format
;
376 for (format
= ClipFormats
, count
= 0; format
; format
= format
->NextFormat
)
377 if (!format
->drvData
) count
++;
380 names
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(*names
) );
381 atoms
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(*atoms
) );
383 for (format
= ClipFormats
, i
= 0; format
; format
= format
->NextFormat
) {
384 if (!format
->drvData
) {
385 len
= WideCharToMultiByte(CP_UNIXCP
, 0, format
->Name
, -1, NULL
, 0, NULL
, NULL
);
386 names
[i
] = HeapAlloc(GetProcessHeap(), 0, len
);
387 WideCharToMultiByte(CP_UNIXCP
, 0, format
->Name
, -1, names
[i
++], len
, NULL
, NULL
);
392 XInternAtoms( thread_display(), names
, count
, False
, atoms
);
395 for (format
= ClipFormats
, i
= 0; format
; format
= format
->NextFormat
) {
396 if (!format
->drvData
) {
397 HeapFree(GetProcessHeap(), 0, names
[i
]);
398 format
->drvData
= atoms
[i
++];
402 HeapFree( GetProcessHeap(), 0, names
);
403 HeapFree( GetProcessHeap(), 0, atoms
);
407 /**************************************************************************
410 * Register a custom X clipboard format.
412 static WINE_CLIPFORMAT
*register_format( LPCWSTR FormatName
, Atom prop
)
414 LPWINE_CLIPFORMAT lpFormat
= ClipFormats
;
416 TRACE("%s\n", debugstr_w(FormatName
));
418 /* walk format chain to see if it's already registered */
421 if (CompareStringW(LOCALE_SYSTEM_DEFAULT
, NORM_IGNORECASE
, lpFormat
->Name
, -1, FormatName
, -1) == CSTR_EQUAL
422 && (lpFormat
->wFlags
& CF_FLAG_BUILTINFMT
) == 0)
424 lpFormat
= lpFormat
->NextFormat
;
427 return X11DRV_CLIPBOARD_InsertClipboardFormat(FormatName
, prop
);
431 /**************************************************************************
432 * X11DRV_CLIPBOARD_LookupFormat
434 static LPWINE_CLIPFORMAT
X11DRV_CLIPBOARD_LookupFormat(WORD wID
)
436 LPWINE_CLIPFORMAT lpFormat
= ClipFormats
;
440 if (lpFormat
->wFormatID
== wID
)
443 lpFormat
= lpFormat
->NextFormat
;
445 if (lpFormat
&& !lpFormat
->drvData
) intern_atoms();
450 /**************************************************************************
451 * X11DRV_CLIPBOARD_LookupProperty
453 static LPWINE_CLIPFORMAT
X11DRV_CLIPBOARD_LookupProperty(LPWINE_CLIPFORMAT current
, UINT drvData
)
457 LPWINE_CLIPFORMAT lpFormat
= ClipFormats
;
458 BOOL need_intern
= FALSE
;
461 lpFormat
= current
->NextFormat
;
465 if (lpFormat
->drvData
== drvData
) return lpFormat
;
466 if (!lpFormat
->drvData
) need_intern
= TRUE
;
467 lpFormat
= lpFormat
->NextFormat
;
469 if (!need_intern
) return NULL
;
471 /* restart the search for the new atoms */
476 /**************************************************************************
477 * X11DRV_CLIPBOARD_LookupData
479 static LPWINE_CLIPDATA
X11DRV_CLIPBOARD_LookupData(DWORD wID
)
481 LPWINE_CLIPDATA lpData
= ClipData
;
487 if (lpData
->wFormatID
== wID
)
490 lpData
= lpData
->NextData
;
492 while(lpData
!= ClipData
);
494 if (lpData
->wFormatID
!= wID
)
502 /**************************************************************************
503 * InsertClipboardFormat
505 static WINE_CLIPFORMAT
*X11DRV_CLIPBOARD_InsertClipboardFormat(LPCWSTR FormatName
, Atom prop
)
507 LPWINE_CLIPFORMAT lpFormat
;
508 LPWINE_CLIPFORMAT lpNewFormat
;
510 /* allocate storage for new format entry */
511 lpNewFormat
= HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_CLIPFORMAT
));
513 if(lpNewFormat
== NULL
)
515 WARN("No more memory for a new format!\n");
519 if (!(lpNewFormat
->Name
= HeapAlloc(GetProcessHeap(), 0, (strlenW(FormatName
)+1)*sizeof(WCHAR
))))
521 WARN("No more memory for the new format name!\n");
522 HeapFree(GetProcessHeap(), 0, lpNewFormat
);
526 strcpyW((LPWSTR
)lpNewFormat
->Name
, FormatName
);
527 lpNewFormat
->wFlags
= 0;
528 lpNewFormat
->wFormatID
= GlobalAddAtomW(lpNewFormat
->Name
);
529 lpNewFormat
->drvData
= prop
;
530 lpNewFormat
->lpDrvImportFunc
= X11DRV_CLIPBOARD_ImportClipboardData
;
531 lpNewFormat
->lpDrvExportFunc
= X11DRV_CLIPBOARD_ExportClipboardData
;
534 lpFormat
= ClipFormats
;
536 while(lpFormat
->NextFormat
) /* Move to last entry */
537 lpFormat
= lpFormat
->NextFormat
;
539 lpNewFormat
->NextFormat
= NULL
;
540 lpFormat
->NextFormat
= lpNewFormat
;
541 lpNewFormat
->PrevFormat
= lpFormat
;
543 TRACE("Registering format(%d): %s drvData %d\n",
544 lpNewFormat
->wFormatID
, debugstr_w(FormatName
), lpNewFormat
->drvData
);
552 /**************************************************************************
553 * X11DRV_CLIPBOARD_GetClipboardInfo
555 static BOOL
X11DRV_CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo
)
559 SERVER_START_REQ( set_clipboard_info
)
563 if (wine_server_call_err( req
))
565 ERR("Failed to get clipboard owner.\n");
569 cbInfo
->hWndOpen
= reply
->old_clipboard
;
570 cbInfo
->hWndOwner
= reply
->old_owner
;
571 cbInfo
->hWndViewer
= reply
->old_viewer
;
572 cbInfo
->seqno
= reply
->seqno
;
573 cbInfo
->flags
= reply
->flags
;
584 /**************************************************************************
585 * X11DRV_CLIPBOARD_ReleaseOwnership
587 static BOOL
X11DRV_CLIPBOARD_ReleaseOwnership(void)
591 SERVER_START_REQ( set_clipboard_info
)
593 req
->flags
= SET_CB_RELOWNER
| SET_CB_SEQNO
;
595 if (wine_server_call_err( req
))
597 ERR("Failed to set clipboard.\n");
611 /**************************************************************************
612 * X11DRV_CLIPBOARD_InsertClipboardData
614 * Caller *must* have the clipboard open and be the owner.
616 static BOOL
X11DRV_CLIPBOARD_InsertClipboardData(UINT wFormatID
, HANDLE16 hData16
,
617 HANDLE hData32
, DWORD flags
, LPWINE_CLIPFORMAT lpFormat
, BOOL override
)
619 LPWINE_CLIPDATA lpData
= X11DRV_CLIPBOARD_LookupData(wFormatID
);
621 TRACE("format=%d lpData=%p hData16=%08x hData32=%p flags=0x%08lx lpFormat=%p override=%d\n",
622 wFormatID
, lpData
, hData16
, hData32
, flags
, lpFormat
, override
);
624 if (lpData
&& !override
)
629 X11DRV_CLIPBOARD_FreeData(lpData
);
631 lpData
->hData16
= hData16
; /* 0 is legal, see WM_RENDERFORMAT */
632 lpData
->hData32
= hData32
;
636 lpData
= HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_CLIPDATA
));
638 lpData
->wFormatID
= wFormatID
;
639 lpData
->hData16
= hData16
; /* 0 is legal, see WM_RENDERFORMAT */
640 lpData
->hData32
= hData32
;
641 lpData
->lpFormat
= lpFormat
;
646 LPWINE_CLIPDATA lpPrevData
= ClipData
->PrevData
;
648 lpData
->PrevData
= lpPrevData
;
649 lpData
->NextData
= ClipData
;
651 lpPrevData
->NextData
= lpData
;
652 ClipData
->PrevData
= lpData
;
656 lpData
->NextData
= lpData
;
657 lpData
->PrevData
= lpData
;
664 lpData
->wFlags
= flags
;
670 /**************************************************************************
671 * X11DRV_CLIPBOARD_FreeData
673 * Free clipboard data handle.
675 static void X11DRV_CLIPBOARD_FreeData(LPWINE_CLIPDATA lpData
)
677 TRACE("%d\n", lpData
->wFormatID
);
679 if ((lpData
->wFormatID
>= CF_GDIOBJFIRST
&&
680 lpData
->wFormatID
<= CF_GDIOBJLAST
) ||
681 lpData
->wFormatID
== CF_BITMAP
||
682 lpData
->wFormatID
== CF_DIB
||
683 lpData
->wFormatID
== CF_PALETTE
)
686 DeleteObject(lpData
->hData32
);
689 DeleteObject(HGDIOBJ_32(lpData
->hData16
));
691 if ((lpData
->wFormatID
== CF_DIB
) && lpData
->drvData
)
692 XFreePixmap(gdi_display
, lpData
->drvData
);
694 else if (lpData
->wFormatID
== CF_METAFILEPICT
)
698 DeleteMetaFile(((METAFILEPICT
*)GlobalLock( lpData
->hData32
))->hMF
);
699 GlobalFree(lpData
->hData32
);
702 /* HMETAFILE16 and HMETAFILE32 are apparently the same thing,
703 and a shallow copy is enough to share a METAFILEPICT
704 structure between 16bit and 32bit clipboards. The MetaFile
705 should of course only be deleted once. */
706 GlobalFree16(lpData
->hData16
);
711 METAFILEPICT16
* lpMetaPict
= (METAFILEPICT16
*) GlobalLock16(lpData
->hData16
);
715 /* To delete 16-bit meta file, we just need to free the associated
716 handle. See DeleteMetaFile16() in dlls/gdi/metafile.c. */
717 GlobalFree16(lpMetaPict
->hMF
);
721 GlobalFree16(lpData
->hData16
);
724 else if (lpData
->wFormatID
== CF_ENHMETAFILE
)
727 DeleteEnhMetaFile(lpData
->hData32
);
729 else if (lpData
->wFormatID
< CF_PRIVATEFIRST
||
730 lpData
->wFormatID
> CF_PRIVATELAST
)
733 GlobalFree(lpData
->hData32
);
736 GlobalFree16(lpData
->hData16
);
745 /**************************************************************************
746 * X11DRV_CLIPBOARD_UpdateCache
748 static BOOL
X11DRV_CLIPBOARD_UpdateCache(LPCLIPBOARDINFO lpcbinfo
)
752 if (!X11DRV_CLIPBOARD_IsSelectionOwner())
754 if (!X11DRV_CLIPBOARD_GetClipboardInfo(lpcbinfo
))
756 ERR("Failed to retrieve clipboard information.\n");
759 else if (wSeqNo
< lpcbinfo
->seqno
)
761 X11DRV_EmptyClipboard(TRUE
);
763 if (X11DRV_CLIPBOARD_QueryAvailableData(lpcbinfo
) < 0)
765 ERR("Failed to cache clipboard data owned by another process.\n");
770 X11DRV_EndClipboardUpdate();
773 wSeqNo
= lpcbinfo
->seqno
;
781 /**************************************************************************
782 * X11DRV_CLIPBOARD_RenderFormat
784 static BOOL
X11DRV_CLIPBOARD_RenderFormat(LPWINE_CLIPDATA lpData
)
788 TRACE(" 0x%04x hData32(%p) hData16(0x%08x)\n",
789 lpData
->wFormatID
, lpData
->hData32
, lpData
->hData16
);
791 if (lpData
->hData32
|| lpData
->hData16
)
792 return bret
; /* Already rendered */
794 if (lpData
->wFlags
& CF_FLAG_SYNTHESIZED
)
795 bret
= X11DRV_CLIPBOARD_RenderSynthesizedFormat(lpData
);
796 else if (!X11DRV_CLIPBOARD_IsSelectionOwner())
798 if (!X11DRV_CLIPBOARD_ReadSelectionData(lpData
))
800 ERR("Failed to cache clipboard data owned by another process. Format=%d\n",
807 CLIPBOARDINFO cbInfo
;
809 if (X11DRV_CLIPBOARD_GetClipboardInfo(&cbInfo
) && cbInfo
.hWndOwner
)
811 /* Send a WM_RENDERFORMAT message to notify the owner to render the
812 * data requested into the clipboard.
814 TRACE("Sending WM_RENDERFORMAT message to hwnd(%p)\n", cbInfo
.hWndOwner
);
815 SendMessageW(cbInfo
.hWndOwner
, WM_RENDERFORMAT
, (WPARAM
)lpData
->wFormatID
, 0);
817 if (!lpData
->hData32
&& !lpData
->hData16
)
822 ERR("hWndClipOwner is lost!\n");
831 /**************************************************************************
832 * CLIPBOARD_ConvertText
833 * Returns number of required/converted characters - not bytes!
835 static INT
CLIPBOARD_ConvertText(WORD src_fmt
, void const *src
, INT src_size
,
836 WORD dst_fmt
, void *dst
, INT dst_size
)
840 if(src_fmt
== CF_UNICODETEXT
)
853 return WideCharToMultiByte(cp
, 0, src
, src_size
, dst
, dst_size
, NULL
, NULL
);
856 if(dst_fmt
== CF_UNICODETEXT
)
869 return MultiByteToWideChar(cp
, 0, src
, src_size
, dst
, dst_size
);
872 if(!dst_size
) return src_size
;
874 if(dst_size
> src_size
) dst_size
= src_size
;
876 if(src_fmt
== CF_TEXT
)
877 CharToOemBuffA(src
, dst
, dst_size
);
879 OemToCharBuffA(src
, dst
, dst_size
);
885 /**************************************************************************
886 * X11DRV_CLIPBOARD_RenderSynthesizedFormat
888 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedFormat(LPWINE_CLIPDATA lpData
)
894 if (lpData
->wFlags
& CF_FLAG_SYNTHESIZED
)
896 UINT wFormatID
= lpData
->wFormatID
;
898 if (wFormatID
== CF_UNICODETEXT
|| wFormatID
== CF_TEXT
|| wFormatID
== CF_OEMTEXT
)
899 bret
= X11DRV_CLIPBOARD_RenderSynthesizedText(wFormatID
);
905 bret
= X11DRV_CLIPBOARD_RenderSynthesizedDIB();
909 bret
= X11DRV_CLIPBOARD_RenderSynthesizedBitmap();
913 case CF_METAFILEPICT
:
914 FIXME("Synthesizing wFormatID(0x%08x) not implemented\n", wFormatID
);
918 FIXME("Called to synthesize unknown format\n");
923 lpData
->wFlags
&= ~CF_FLAG_SYNTHESIZED
;
930 /**************************************************************************
931 * X11DRV_CLIPBOARD_RenderSynthesizedText
933 * Renders synthesized text
935 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedText(UINT wFormatID
)
940 INT src_chars
, dst_chars
, alloc_size
;
941 LPWINE_CLIPDATA lpSource
= NULL
;
943 TRACE(" %d\n", wFormatID
);
945 if ((lpSource
= X11DRV_CLIPBOARD_LookupData(wFormatID
)) &&
949 /* Look for rendered source or non-synthesized source */
950 if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT
)) &&
951 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData32
))
953 TRACE("UNICODETEXT -> %d\n", wFormatID
);
955 else if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_TEXT
)) &&
956 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData32
))
958 TRACE("TEXT -> %d\n", wFormatID
);
960 else if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT
)) &&
961 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData32
))
963 TRACE("OEMTEXT -> %d\n", wFormatID
);
966 if (!lpSource
|| (lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
&&
970 /* Ask the clipboard owner to render the source text if necessary */
971 if (!lpSource
->hData32
&& !X11DRV_CLIPBOARD_RenderFormat(lpSource
))
974 if (lpSource
->hData32
)
976 lpstrS
= (LPSTR
)GlobalLock(lpSource
->hData32
);
980 lpstrS
= (LPSTR
)GlobalLock16(lpSource
->hData16
);
986 /* Text always NULL terminated */
987 if(lpSource
->wFormatID
== CF_UNICODETEXT
)
988 src_chars
= strlenW((LPCWSTR
)lpstrS
) + 1;
990 src_chars
= strlen(lpstrS
) + 1;
992 /* Calculate number of characters in the destination buffer */
993 dst_chars
= CLIPBOARD_ConvertText(lpSource
->wFormatID
, lpstrS
,
994 src_chars
, wFormatID
, NULL
, 0);
999 TRACE("Converting from '%d' to '%d', %i chars\n",
1000 lpSource
->wFormatID
, wFormatID
, src_chars
);
1002 /* Convert characters to bytes */
1003 if(wFormatID
== CF_UNICODETEXT
)
1004 alloc_size
= dst_chars
* sizeof(WCHAR
);
1006 alloc_size
= dst_chars
;
1008 hData32
= GlobalAlloc(GMEM_ZEROINIT
| GMEM_MOVEABLE
|
1009 GMEM_DDESHARE
, alloc_size
);
1011 lpstrT
= (LPSTR
)GlobalLock(hData32
);
1015 CLIPBOARD_ConvertText(lpSource
->wFormatID
, lpstrS
, src_chars
,
1016 wFormatID
, lpstrT
, dst_chars
);
1017 GlobalUnlock(hData32
);
1021 if (lpSource
->hData32
)
1022 GlobalUnlock(lpSource
->hData32
);
1024 GlobalUnlock16(lpSource
->hData16
);
1026 return X11DRV_CLIPBOARD_InsertClipboardData(wFormatID
, 0, hData32
, 0, NULL
, TRUE
);
1030 /**************************************************************************
1031 * X11DRV_CLIPBOARD_RenderSynthesizedDIB
1033 * Renders synthesized DIB
1035 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedDIB()
1038 LPWINE_CLIPDATA lpSource
= NULL
;
1042 if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_DIB
)) && lpSource
->hData32
)
1046 /* If we have a bitmap and it's not synthesized or it has been rendered */
1047 else if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_BITMAP
)) &&
1048 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData32
))
1050 /* Render source if required */
1051 if (lpSource
->hData32
|| X11DRV_CLIPBOARD_RenderFormat(lpSource
))
1057 hData32
= X11DRV_DIB_CreateDIBFromBitmap(hdc
, lpSource
->hData32
);
1058 ReleaseDC(NULL
, hdc
);
1062 X11DRV_CLIPBOARD_InsertClipboardData(CF_DIB
, 0, hData32
, 0, NULL
, TRUE
);
1072 /**************************************************************************
1073 * X11DRV_CLIPBOARD_RenderSynthesizedBitmap
1075 * Renders synthesized bitmap
1077 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedBitmap()
1080 LPWINE_CLIPDATA lpSource
= NULL
;
1084 if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_BITMAP
)) && lpSource
->hData32
)
1088 /* If we have a dib and it's not synthesized or it has been rendered */
1089 else if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_DIB
)) &&
1090 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData32
))
1092 /* Render source if required */
1093 if (lpSource
->hData32
|| X11DRV_CLIPBOARD_RenderFormat(lpSource
))
1097 unsigned int offset
;
1098 LPBITMAPINFOHEADER lpbmih
;
1101 lpbmih
= (LPBITMAPINFOHEADER
) GlobalLock(lpSource
->hData32
);
1103 offset
= sizeof(BITMAPINFOHEADER
)
1104 + ((lpbmih
->biBitCount
<= 8) ? (sizeof(RGBQUAD
) *
1105 (1 << lpbmih
->biBitCount
)) : 0);
1107 hData32
= CreateDIBitmap(hdc
, lpbmih
, CBM_INIT
, (LPBYTE
)lpbmih
+
1108 offset
, (LPBITMAPINFO
) lpbmih
, DIB_RGB_COLORS
);
1110 GlobalUnlock(lpSource
->hData32
);
1111 ReleaseDC(NULL
, hdc
);
1115 X11DRV_CLIPBOARD_InsertClipboardData(CF_BITMAP
, 0, hData32
, 0, NULL
, TRUE
);
1125 /**************************************************************************
1126 * X11DRV_CLIPBOARD_ImportXAString
1128 * Import XA_STRING, converting the string to CF_TEXT.
1130 HANDLE
X11DRV_CLIPBOARD_ImportXAString(Window w
, Atom prop
)
1133 unsigned long cbytes
;
1135 unsigned long i
, inlcount
= 0;
1138 if (!X11DRV_CLIPBOARD_ReadProperty(w
, prop
, &lpdata
, &cbytes
))
1141 for (i
= 0; i
<= cbytes
; i
++)
1143 if (lpdata
[i
] == '\n')
1147 if ((hText
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, cbytes
+ inlcount
+ 1)))
1149 lpstr
= GlobalLock(hText
);
1151 for (i
= 0, inlcount
= 0; i
<= cbytes
; i
++)
1153 if (lpdata
[i
] == '\n')
1154 lpstr
[inlcount
++] = '\r';
1156 lpstr
[inlcount
++] = lpdata
[i
];
1159 GlobalUnlock(hText
);
1162 /* Free the retrieved property data */
1163 HeapFree(GetProcessHeap(), 0, lpdata
);
1169 /**************************************************************************
1170 * X11DRV_CLIPBOARD_ImportUTF8
1172 * Import XA_STRING, converting the string to CF_UNICODE.
1174 HANDLE
X11DRV_CLIPBOARD_ImportUTF8(Window w
, Atom prop
)
1177 unsigned long cbytes
;
1179 unsigned long i
, inlcount
= 0;
1180 HANDLE hUnicodeText
= 0;
1182 if (!X11DRV_CLIPBOARD_ReadProperty(w
, prop
, &lpdata
, &cbytes
))
1185 for (i
= 0; i
<= cbytes
; i
++)
1187 if (lpdata
[i
] == '\n')
1191 if ((lpstr
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, cbytes
+ inlcount
+ 1)))
1195 for (i
= 0, inlcount
= 0; i
<= cbytes
; i
++)
1197 if (lpdata
[i
] == '\n')
1198 lpstr
[inlcount
++] = '\r';
1200 lpstr
[inlcount
++] = lpdata
[i
];
1203 count
= MultiByteToWideChar(CP_UTF8
, 0, lpstr
, -1, NULL
, 0);
1204 hUnicodeText
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, count
* sizeof(WCHAR
));
1208 WCHAR
*textW
= GlobalLock(hUnicodeText
);
1209 MultiByteToWideChar(CP_UTF8
, 0, lpstr
, -1, textW
, count
);
1210 GlobalUnlock(hUnicodeText
);
1213 HeapFree(GetProcessHeap(), 0, lpstr
);
1216 /* Free the retrieved property data */
1217 HeapFree(GetProcessHeap(), 0, lpdata
);
1219 return hUnicodeText
;
1223 /**************************************************************************
1224 * X11DRV_CLIPBOARD_ImportCompoundText
1226 * Import COMPOUND_TEXT to CF_UNICODE
1228 static HANDLE
X11DRV_CLIPBOARD_ImportCompoundText(Window w
, Atom prop
)
1230 Display
*display
= thread_display();
1234 int srclen
, destlen
;
1235 HANDLE hUnicodeText
;
1236 XTextProperty txtprop
;
1239 if (!XGetTextProperty(display
, w
, &txtprop
, prop
))
1241 wine_tsx11_unlock();
1245 XmbTextPropertyToTextList(display
, &txtprop
, &srcstr
, &count
);
1246 wine_tsx11_unlock();
1248 TRACE("Importing %d line(s)\n", count
);
1250 /* Compute number of lines */
1251 srclen
= strlen(srcstr
[0]);
1252 for (i
= 0, lcount
= 0; i
<= srclen
; i
++)
1254 if (srcstr
[0][i
] == '\n')
1258 destlen
= MultiByteToWideChar(CP_UNIXCP
, 0, srcstr
[0], -1, NULL
, 0);
1260 TRACE("lcount = %d, destlen=%d, srcstr %s\n", lcount
, destlen
, srcstr
[0]);
1262 if ((hUnicodeText
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, (destlen
+ lcount
+ 1) * sizeof(WCHAR
))))
1264 WCHAR
*deststr
= GlobalLock(hUnicodeText
);
1265 MultiByteToWideChar(CP_UNIXCP
, 0, srcstr
[0], -1, deststr
, destlen
);
1269 for (i
= destlen
- 1, j
= destlen
+ lcount
- 1; i
>= 0; i
--, j
--)
1271 deststr
[j
] = deststr
[i
];
1273 if (deststr
[i
] == '\n')
1274 deststr
[--j
] = '\r';
1278 GlobalUnlock(hUnicodeText
);
1282 XFreeStringList(srcstr
);
1283 XFree(txtprop
.value
);
1284 wine_tsx11_unlock();
1286 return hUnicodeText
;
1290 /**************************************************************************
1291 * X11DRV_CLIPBOARD_ImportXAPIXMAP
1293 * Import XA_PIXMAP, converting the image to CF_DIB.
1295 HANDLE
X11DRV_CLIPBOARD_ImportXAPIXMAP(Window w
, Atom prop
)
1300 unsigned long cbytes
;
1302 HANDLE hClipData
= 0;
1304 if (X11DRV_CLIPBOARD_ReadProperty(w
, prop
, &lpdata
, &cbytes
))
1306 pPixmap
= (Pixmap
*) lpdata
;
1308 hwnd
= GetOpenClipboardWindow();
1311 hClipData
= X11DRV_DIB_CreateDIBFromPixmap(*pPixmap
, hdc
);
1312 ReleaseDC(hwnd
, hdc
);
1314 /* Free the retrieved property data */
1315 HeapFree(GetProcessHeap(), 0, lpdata
);
1322 /**************************************************************************
1323 * X11DRV_CLIPBOARD_ImportMetaFilePict
1325 * Import MetaFilePict.
1327 HANDLE
X11DRV_CLIPBOARD_ImportMetaFilePict(Window w
, Atom prop
)
1330 unsigned long cbytes
;
1331 HANDLE hClipData
= 0;
1333 if (X11DRV_CLIPBOARD_ReadProperty(w
, prop
, &lpdata
, &cbytes
))
1336 hClipData
= X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT
, (HANDLE
)lpdata
, (LPDWORD
)&cbytes
, FALSE
);
1338 /* Free the retrieved property data */
1339 HeapFree(GetProcessHeap(), 0, lpdata
);
1346 /**************************************************************************
1347 * X11DRV_ImportEnhMetaFile
1349 * Import EnhMetaFile.
1351 HANDLE
X11DRV_CLIPBOARD_ImportEnhMetaFile(Window w
, Atom prop
)
1354 unsigned long cbytes
;
1355 HANDLE hClipData
= 0;
1357 if (X11DRV_CLIPBOARD_ReadProperty(w
, prop
, &lpdata
, &cbytes
))
1360 hClipData
= X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE
, (HANDLE
)lpdata
, (LPDWORD
)&cbytes
, FALSE
);
1362 /* Free the retrieved property data */
1363 HeapFree(GetProcessHeap(), 0, lpdata
);
1370 /**************************************************************************
1371 * X11DRV_ImportClipbordaData
1373 * Generic import clipboard data routine.
1375 HANDLE
X11DRV_CLIPBOARD_ImportClipboardData(Window w
, Atom prop
)
1379 unsigned long cbytes
;
1380 HANDLE hClipData
= 0;
1382 if (X11DRV_CLIPBOARD_ReadProperty(w
, prop
, &lpdata
, &cbytes
))
1386 /* Turn on the DDESHARE flag to enable shared 32 bit memory */
1387 hClipData
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, cbytes
);
1391 if ((lpClipData
= GlobalLock(hClipData
)))
1393 memcpy(lpClipData
, lpdata
, cbytes
);
1394 GlobalUnlock(hClipData
);
1398 GlobalFree(hClipData
);
1403 /* Free the retrieved property data */
1404 HeapFree(GetProcessHeap(), 0, lpdata
);
1411 /**************************************************************************
1412 X11DRV_CLIPBOARD_ExportClipboardData
1414 * Generic export clipboard data routine.
1416 HANDLE
X11DRV_CLIPBOARD_ExportClipboardData(Window requestor
, Atom aTarget
,
1417 Atom rprop
, LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1421 HANDLE hClipData
= 0;
1423 *lpBytes
= 0; /* Assume failure */
1425 if (!X11DRV_CLIPBOARD_RenderFormat(lpData
))
1426 ERR("Failed to export %d format\n", lpData
->wFormatID
);
1429 datasize
= GlobalSize(lpData
->hData32
);
1431 hClipData
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, datasize
);
1432 if (hClipData
== 0) return NULL
;
1434 if ((lpClipData
= GlobalLock(hClipData
)))
1436 LPVOID lpdata
= GlobalLock(lpData
->hData32
);
1438 memcpy(lpClipData
, lpdata
, datasize
);
1439 *lpBytes
= datasize
;
1441 GlobalUnlock(lpData
->hData32
);
1442 GlobalUnlock(hClipData
);
1444 GlobalFree(hClipData
);
1453 /**************************************************************************
1454 * X11DRV_CLIPBOARD_ExportXAString
1456 * Export CF_TEXT converting the string to XA_STRING.
1457 * Helper function for X11DRV_CLIPBOARD_ExportString.
1459 static HANDLE
X11DRV_CLIPBOARD_ExportXAString(LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1463 LPSTR text
, lpstr
= NULL
;
1465 *lpBytes
= 0; /* Assume return has zero bytes */
1467 text
= GlobalLock(lpData
->hData32
);
1468 size
= strlen(text
);
1470 /* remove carriage returns */
1471 lpstr
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, size
+ 1);
1475 for (i
= 0,j
= 0; i
< size
&& text
[i
]; i
++)
1477 if (text
[i
] == '\r' && (text
[i
+1] == '\n' || text
[i
+1] == '\0'))
1479 lpstr
[j
++] = text
[i
];
1483 *lpBytes
= j
; /* Number of bytes in string */
1486 HeapFree(GetProcessHeap(), 0, text
);
1487 GlobalUnlock(lpData
->hData32
);
1493 /**************************************************************************
1494 * X11DRV_CLIPBOARD_ExportUTF8String
1496 * Export CF_UNICODE converting the string to UTF8.
1497 * Helper function for X11DRV_CLIPBOARD_ExportString.
1499 static HANDLE
X11DRV_CLIPBOARD_ExportUTF8String(LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1504 LPSTR text
, lpstr
= NULL
;
1506 *lpBytes
= 0; /* Assume return has zero bytes */
1508 uni_text
= GlobalLock(lpData
->hData32
);
1510 size
= WideCharToMultiByte(CP_UTF8
, 0, uni_text
, -1, NULL
, 0, NULL
, NULL
);
1512 text
= HeapAlloc(GetProcessHeap(), 0, size
);
1515 WideCharToMultiByte(CP_UTF8
, 0, uni_text
, -1, text
, size
, NULL
, NULL
);
1517 /* remove carriage returns */
1518 lpstr
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, size
--);
1522 for (i
= 0,j
= 0; i
< size
&& text
[i
]; i
++)
1524 if (text
[i
] == '\r' && (text
[i
+1] == '\n' || text
[i
+1] == '\0'))
1526 lpstr
[j
++] = text
[i
];
1530 *lpBytes
= j
; /* Number of bytes in string */
1533 HeapFree(GetProcessHeap(), 0, text
);
1534 GlobalUnlock(lpData
->hData32
);
1541 /**************************************************************************
1542 * X11DRV_CLIPBOARD_ExportCompoundText
1544 * Export CF_UNICODE to COMPOUND_TEXT
1545 * Helper function for X11DRV_CLIPBOARD_ExportString.
1547 static HANDLE
X11DRV_CLIPBOARD_ExportCompoundText(Window requestor
, Atom aTarget
, Atom rprop
,
1548 LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1550 Display
*display
= thread_display();
1553 XICCEncodingStyle style
;
1558 uni_text
= GlobalLock(lpData
->hData32
);
1560 size
= WideCharToMultiByte(CP_UNIXCP
, 0, uni_text
, -1, NULL
, 0, NULL
, NULL
);
1561 lpstr
= HeapAlloc(GetProcessHeap(), 0, size
);
1565 WideCharToMultiByte(CP_UNIXCP
, 0, uni_text
, -1, lpstr
, size
, NULL
, NULL
);
1567 /* remove carriage returns */
1568 for (i
= 0, j
= 0; i
< size
&& lpstr
[i
]; i
++)
1570 if (lpstr
[i
] == '\r' && (lpstr
[i
+1] == '\n' || lpstr
[i
+1] == '\0'))
1572 lpstr
[j
++] = lpstr
[i
];
1576 GlobalUnlock(lpData
->hData32
);
1578 if (aTarget
== x11drv_atom(COMPOUND_TEXT
))
1579 style
= XCompoundTextStyle
;
1581 style
= XStdICCTextStyle
;
1583 /* Update the X property */
1585 if (XmbTextListToTextProperty(display
, &lpstr
, 1, style
, &prop
) == Success
)
1587 XSetTextProperty(display
, requestor
, &prop
, rprop
);
1590 wine_tsx11_unlock();
1592 HeapFree(GetProcessHeap(), 0, lpstr
);
1597 /**************************************************************************
1598 * X11DRV_CLIPBOARD_ExportString
1602 HANDLE
X11DRV_CLIPBOARD_ExportString(Window requestor
, Atom aTarget
, Atom rprop
,
1603 LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1605 if (X11DRV_CLIPBOARD_RenderFormat(lpData
))
1607 if (aTarget
== XA_STRING
)
1608 return X11DRV_CLIPBOARD_ExportXAString(lpData
, lpBytes
);
1609 else if (aTarget
== x11drv_atom(COMPOUND_TEXT
) || aTarget
== x11drv_atom(TEXT
))
1610 return X11DRV_CLIPBOARD_ExportCompoundText(requestor
, aTarget
,
1611 rprop
, lpData
, lpBytes
);
1614 TRACE("Exporting target %ld to default UTF8_STRING\n", aTarget
);
1615 return X11DRV_CLIPBOARD_ExportUTF8String(lpData
, lpBytes
);
1619 ERR("Failed to render %d format\n", lpData
->wFormatID
);
1625 /**************************************************************************
1626 * X11DRV_CLIPBOARD_ExportXAPIXMAP
1628 * Export CF_DIB to XA_PIXMAP.
1630 HANDLE
X11DRV_CLIPBOARD_ExportXAPIXMAP(Window requestor
, Atom aTarget
, Atom rprop
,
1631 LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
1635 unsigned char* lpData
;
1637 if (!X11DRV_CLIPBOARD_RenderFormat(lpdata
))
1639 ERR("Failed to export %d format\n", lpdata
->wFormatID
);
1643 if (!lpdata
->drvData
) /* If not already rendered */
1645 /* For convert from packed DIB to Pixmap */
1647 lpdata
->drvData
= (UINT
) X11DRV_DIB_CreatePixmapFromDIB(lpdata
->hData32
, hdc
);
1651 *lpBytes
= sizeof(Pixmap
); /* pixmap is a 32bit value */
1653 /* Wrap pixmap so we can return a handle */
1654 hData
= GlobalAlloc(0, *lpBytes
);
1655 lpData
= GlobalLock(hData
);
1656 memcpy(lpData
, &lpdata
->drvData
, *lpBytes
);
1657 GlobalUnlock(hData
);
1659 return (HANDLE
) hData
;
1663 /**************************************************************************
1664 * X11DRV_CLIPBOARD_ExportMetaFilePict
1666 * Export MetaFilePict.
1668 HANDLE
X11DRV_CLIPBOARD_ExportMetaFilePict(Window requestor
, Atom aTarget
, Atom rprop
,
1669 LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
1671 if (!X11DRV_CLIPBOARD_RenderFormat(lpdata
))
1673 ERR("Failed to export %d format\n", lpdata
->wFormatID
);
1677 return X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT
, (HANDLE
)lpdata
->hData32
,
1682 /**************************************************************************
1683 * X11DRV_CLIPBOARD_ExportEnhMetaFile
1685 * Export EnhMetaFile.
1687 HANDLE
X11DRV_CLIPBOARD_ExportEnhMetaFile(Window requestor
, Atom aTarget
, Atom rprop
,
1688 LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
1690 if (!X11DRV_CLIPBOARD_RenderFormat(lpdata
))
1692 ERR("Failed to export %d format\n", lpdata
->wFormatID
);
1696 return X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE
, (HANDLE
)lpdata
->hData32
,
1701 /**************************************************************************
1702 * X11DRV_CLIPBOARD_QueryTargets
1704 static BOOL
X11DRV_CLIPBOARD_QueryTargets(Display
*display
, Window w
, Atom selection
,
1705 Atom target
, XEvent
*xe
)
1711 XConvertSelection(display
, selection
, target
,
1712 x11drv_atom(SELECTION_DATA
), w
, CurrentTime
);
1713 wine_tsx11_unlock();
1716 * Wait until SelectionNotify is received
1718 for (i
= 0; i
< SELECTION_RETRIES
; i
++)
1721 res
= XCheckTypedWindowEvent(display
, w
, SelectionNotify
, xe
);
1722 wine_tsx11_unlock();
1723 if (res
&& xe
->xselection
.selection
== selection
) break;
1725 usleep(SELECTION_WAIT
);
1728 /* Verify that the selection returned a valid TARGETS property */
1729 if ((xe
->xselection
.target
!= target
) || (xe
->xselection
.property
== None
))
1731 /* Selection owner failed to respond or we missed the SelectionNotify */
1732 WARN("Failed to retrieve TARGETS for selection %ld.\n", selection
);
1740 /**************************************************************************
1741 * X11DRV_CLIPBOARD_InsertSelectionProperties
1743 * Mark properties available for future retrieval.
1745 static VOID
X11DRV_CLIPBOARD_InsertSelectionProperties(Display
*display
, Atom
* properties
, UINT count
)
1747 UINT i
, nb_atoms
= 0;
1750 /* Cache these formats in the clipboard cache */
1751 for (i
= 0; i
< count
; i
++)
1753 LPWINE_CLIPFORMAT lpFormat
= X11DRV_CLIPBOARD_LookupProperty(NULL
, properties
[i
]);
1757 /* We found at least one Window's format that mapps to the property.
1758 * Continue looking for more.
1760 * If more than one property map to a Window's format then we use the first
1761 * one and ignore the rest.
1765 TRACE("Atom#%d Property(%d): --> FormatID(%d) %s\n",
1766 i
, lpFormat
->drvData
, lpFormat
->wFormatID
, debugstr_w(lpFormat
->Name
));
1767 X11DRV_CLIPBOARD_InsertClipboardData(lpFormat
->wFormatID
, 0, 0, 0, lpFormat
, FALSE
);
1768 lpFormat
= X11DRV_CLIPBOARD_LookupProperty(lpFormat
, properties
[i
]);
1773 /* add it to the list of atoms that we don't know about yet */
1774 if (!atoms
) atoms
= HeapAlloc( GetProcessHeap(), 0,
1775 (count
- i
) * sizeof(*atoms
) );
1776 if (atoms
) atoms
[nb_atoms
++] = properties
[i
];
1780 /* query all unknown atoms in one go */
1783 char **names
= HeapAlloc( GetProcessHeap(), 0, nb_atoms
* sizeof(*names
) );
1787 XGetAtomNames( display
, atoms
, nb_atoms
, names
);
1788 wine_tsx11_unlock();
1789 for (i
= 0; i
< nb_atoms
; i
++)
1791 WINE_CLIPFORMAT
*lpFormat
;
1793 int len
= MultiByteToWideChar(CP_UNIXCP
, 0, names
[i
], -1, NULL
, 0);
1794 wname
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
1795 MultiByteToWideChar(CP_UNIXCP
, 0, names
[i
], -1, wname
, len
);
1797 lpFormat
= register_format( wname
, atoms
[i
] );
1798 HeapFree(GetProcessHeap(), 0, wname
);
1801 ERR("Failed to register %s property. Type will not be cached.\n", names
[i
]);
1804 TRACE("Atom#%d Property(%d): --> FormatID(%d) %s\n",
1805 i
, lpFormat
->drvData
, lpFormat
->wFormatID
, debugstr_w(lpFormat
->Name
));
1806 X11DRV_CLIPBOARD_InsertClipboardData(lpFormat
->wFormatID
, 0, 0, 0, lpFormat
, FALSE
);
1809 for (i
= 0; i
< nb_atoms
; i
++) XFree( names
[i
] );
1810 wine_tsx11_unlock();
1811 HeapFree( GetProcessHeap(), 0, names
);
1813 HeapFree( GetProcessHeap(), 0, atoms
);
1818 /**************************************************************************
1819 * X11DRV_CLIPBOARD_QueryAvailableData
1821 * Caches the list of data formats available from the current selection.
1822 * This queries the selection owner for the TARGETS property and saves all
1823 * reported property types.
1825 static int X11DRV_CLIPBOARD_QueryAvailableData(LPCLIPBOARDINFO lpcbinfo
)
1827 Display
*display
= thread_display();
1829 Atom atype
=AnyPropertyType
;
1831 unsigned long remain
;
1832 Atom
* targetList
=NULL
;
1834 unsigned long cSelectionTargets
= 0;
1836 if (selectionAcquired
& (S_PRIMARY
| S_CLIPBOARD
))
1838 ERR("Received request to cache selection but process is owner=(%08x)\n",
1839 (unsigned) selectionWindow
);
1840 return -1; /* Prevent self request */
1843 w
= thread_selection_wnd();
1846 ERR("No window available to retrieve selection!\n");
1851 * Query the selection owner for the TARGETS property
1854 if ((use_primary_selection
&& XGetSelectionOwner(display
,XA_PRIMARY
)) ||
1855 XGetSelectionOwner(display
,x11drv_atom(CLIPBOARD
)))
1857 wine_tsx11_unlock();
1858 if (use_primary_selection
&& (X11DRV_CLIPBOARD_QueryTargets(display
, w
, XA_PRIMARY
, x11drv_atom(TARGETS
), &xe
)))
1859 selectionCacheSrc
= XA_PRIMARY
;
1860 else if (X11DRV_CLIPBOARD_QueryTargets(display
, w
, x11drv_atom(CLIPBOARD
), x11drv_atom(TARGETS
), &xe
))
1861 selectionCacheSrc
= x11drv_atom(CLIPBOARD
);
1864 Atom xstr
= XA_PRIMARY
;
1866 /* Selection Owner doesn't understand TARGETS, try retrieving XA_STRING */
1867 if (X11DRV_CLIPBOARD_QueryTargets(display
, w
, XA_PRIMARY
, XA_STRING
, &xe
))
1869 X11DRV_CLIPBOARD_InsertSelectionProperties(display
, &xstr
, 1);
1870 selectionCacheSrc
= XA_PRIMARY
;
1873 else if (X11DRV_CLIPBOARD_QueryTargets(display
, w
, x11drv_atom(CLIPBOARD
), XA_STRING
, &xe
))
1875 X11DRV_CLIPBOARD_InsertSelectionProperties(display
, &xstr
, 1);
1876 selectionCacheSrc
= x11drv_atom(CLIPBOARD
);
1881 WARN("Failed to query selection owner for available data.\n");
1886 else /* No selection owner so report 0 targets available */
1888 wine_tsx11_unlock();
1892 /* Read the TARGETS property contents */
1894 if(XGetWindowProperty(display
, xe
.xselection
.requestor
, xe
.xselection
.property
,
1895 0, 0x3FFF, True
, AnyPropertyType
/*XA_ATOM*/, &atype
, &aformat
, &cSelectionTargets
,
1896 &remain
, (unsigned char**)&targetList
) != Success
)
1898 wine_tsx11_unlock();
1899 WARN("Failed to read TARGETS property\n");
1903 wine_tsx11_unlock();
1904 TRACE("Type %lx,Format %d,nItems %ld, Remain %ld\n",
1905 atype
, aformat
, cSelectionTargets
, remain
);
1907 * The TARGETS property should have returned us a list of atoms
1908 * corresponding to each selection target format supported.
1910 if ((atype
== XA_ATOM
|| atype
== x11drv_atom(TARGETS
)) && aformat
== 32)
1911 X11DRV_CLIPBOARD_InsertSelectionProperties(display
, targetList
, cSelectionTargets
);
1913 /* Free the list of targets */
1916 wine_tsx11_unlock();
1919 return cSelectionTargets
;
1923 /**************************************************************************
1924 * X11DRV_CLIPBOARD_ReadSelectionData
1926 * This method is invoked only when we DO NOT own the X selection
1928 * We always get the data from the selection client each time,
1929 * since we have no way of determining if the data in our cache is stale.
1931 static BOOL
X11DRV_CLIPBOARD_ReadSelectionData(LPWINE_CLIPDATA lpData
)
1933 Display
*display
= thread_display();
1939 TRACE("%d\n", lpData
->wFormatID
);
1941 if (!lpData
->lpFormat
)
1943 ERR("Requesting format %d but no source format linked to data.\n",
1948 if (!selectionAcquired
)
1950 Window w
= thread_selection_wnd();
1953 ERR("No window available to read selection data!\n");
1957 TRACE("Requesting conversion of %s property (%d) from selection type %08x\n",
1958 debugstr_w(lpData
->lpFormat
->Name
), lpData
->lpFormat
->drvData
, (UINT
)selectionCacheSrc
);
1961 XConvertSelection(display
, selectionCacheSrc
, lpData
->lpFormat
->drvData
,
1962 x11drv_atom(SELECTION_DATA
), w
, CurrentTime
);
1963 wine_tsx11_unlock();
1965 /* wait until SelectionNotify is received */
1966 for (i
= 0; i
< SELECTION_RETRIES
; i
++)
1969 res
= XCheckTypedWindowEvent(display
, w
, SelectionNotify
, &xe
);
1970 wine_tsx11_unlock();
1971 if (res
&& xe
.xselection
.selection
== selectionCacheSrc
) break;
1973 usleep(SELECTION_WAIT
);
1976 /* Verify that the selection returned a valid TARGETS property */
1977 if (xe
.xselection
.property
!= None
)
1980 * Read the contents of the X selection property
1981 * into WINE's clipboard cache and converting the
1982 * data format if necessary.
1984 HANDLE hData
= lpData
->lpFormat
->lpDrvImportFunc(xe
.xselection
.requestor
,
1985 xe
.xselection
.property
);
1987 bRet
= X11DRV_CLIPBOARD_InsertClipboardData(lpData
->wFormatID
, 0, hData
, 0, lpData
->lpFormat
, TRUE
);
1991 TRACE("Failed to convert selection\n");
1996 ERR("Received request to cache selection data but process is owner\n");
1999 TRACE("Returning %d\n", bRet
);
2005 /**************************************************************************
2006 * X11DRV_CLIPBOARD_ReadProperty
2007 * Reads the contents of the X selection property.
2009 static BOOL
X11DRV_CLIPBOARD_ReadProperty(Window w
, Atom prop
,
2010 unsigned char** data
, unsigned long* datasize
)
2012 Display
*display
= thread_display();
2013 Atom atype
= AnyPropertyType
;
2015 unsigned long total
, nitems
, remain
, val_cnt
;
2018 unsigned char* buffer
;
2023 TRACE("Reading property %d from X window %d\n",
2024 (unsigned int)prop
, (unsigned int)w
);
2027 * First request a zero length in order to figure out the request size.
2030 if(XGetWindowProperty(display
,w
,prop
,0,0,False
, AnyPropertyType
,
2031 &atype
, &aformat
, &nitems
, &remain
, &buffer
) != Success
)
2033 wine_tsx11_unlock();
2034 WARN("Failed to get property size\n");
2038 /* Free zero length return data if any */
2046 reqlen
= remain
* bwc
;
2048 TRACE("Retrieving %ld bytes\n", reqlen
);
2050 val
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, reqlen
);
2052 /* Read property in 4K blocks */
2053 for (total
= 0, val_cnt
= 0; remain
;)
2055 if (XGetWindowProperty(display
, w
, prop
, (total
/ 4), 4096, False
,
2056 AnyPropertyType
, &atype
, &aformat
, &nitems
, &remain
, &buffer
) != Success
)
2058 wine_tsx11_unlock();
2059 WARN("Failed to read property\n");
2060 HeapFree(GetProcessHeap(), 0, val
);
2065 memcpy(&val
[val_cnt
], buffer
, nitems
* bwc
);
2066 val_cnt
+= nitems
* bwc
;
2067 total
+= nitems
*bwc
;
2071 /* Delete the property on the window now that we are done
2072 * This will send a PropertyNotify event to the selection owner. */
2073 XDeleteProperty(display
, w
, prop
);
2075 wine_tsx11_unlock();
2084 /**************************************************************************
2085 * CLIPBOARD_SerializeMetafile
2087 static HANDLE
X11DRV_CLIPBOARD_SerializeMetafile(INT wformat
, HANDLE hdata
, LPDWORD lpcbytes
, BOOL out
)
2091 TRACE(" wFormat=%d hdata=%p out=%d\n", wformat
, hdata
, out
);
2093 if (out
) /* Serialize out, caller should free memory */
2095 *lpcbytes
= 0; /* Assume failure */
2097 if (wformat
== CF_METAFILEPICT
)
2099 LPMETAFILEPICT lpmfp
= (LPMETAFILEPICT
) GlobalLock(hdata
);
2100 unsigned int size
= GetMetaFileBitsEx(lpmfp
->hMF
, 0, NULL
);
2102 h
= GlobalAlloc(0, size
+ sizeof(METAFILEPICT
));
2105 char *pdata
= GlobalLock(h
);
2107 memcpy(pdata
, lpmfp
, sizeof(METAFILEPICT
));
2108 GetMetaFileBitsEx(lpmfp
->hMF
, size
, pdata
+ sizeof(METAFILEPICT
));
2110 *lpcbytes
= size
+ sizeof(METAFILEPICT
);
2115 GlobalUnlock(hdata
);
2117 else if (wformat
== CF_ENHMETAFILE
)
2119 int size
= GetEnhMetaFileBits(hdata
, 0, NULL
);
2121 h
= GlobalAlloc(0, size
);
2124 LPVOID pdata
= GlobalLock(h
);
2126 GetEnhMetaFileBits(hdata
, size
, pdata
);
2135 if (wformat
== CF_METAFILEPICT
)
2137 h
= GlobalAlloc(0, sizeof(METAFILEPICT
));
2140 unsigned int wiresize
, size
;
2141 LPMETAFILEPICT lpmfp
= (LPMETAFILEPICT
) GlobalLock(h
);
2143 memcpy(lpmfp
, (LPVOID
)hdata
, sizeof(METAFILEPICT
));
2144 wiresize
= *lpcbytes
- sizeof(METAFILEPICT
);
2145 lpmfp
->hMF
= SetMetaFileBitsEx(wiresize
,
2146 ((const BYTE
*)hdata
) + sizeof(METAFILEPICT
));
2147 size
= GetMetaFileBitsEx(lpmfp
->hMF
, 0, NULL
);
2151 else if (wformat
== CF_ENHMETAFILE
)
2153 h
= SetEnhMetaFileBits(*lpcbytes
, (LPVOID
)hdata
);
2161 /**************************************************************************
2162 * X11DRV_CLIPBOARD_ReleaseSelection
2164 * Release XA_CLIPBOARD and XA_PRIMARY in response to a SelectionClear event.
2166 static void X11DRV_CLIPBOARD_ReleaseSelection(Atom selType
, Window w
, HWND hwnd
, Time time
)
2168 Display
*display
= thread_display();
2170 /* w is the window that lost the selection
2172 TRACE("event->window = %08x (selectionWindow = %08x) selectionAcquired=0x%08x\n",
2173 (unsigned)w
, (unsigned)selectionWindow
, (unsigned)selectionAcquired
);
2175 if (selectionAcquired
&& (w
== selectionWindow
))
2177 CLIPBOARDINFO cbinfo
;
2179 /* completely give up the selection */
2180 TRACE("Lost CLIPBOARD (+PRIMARY) selection\n");
2182 X11DRV_CLIPBOARD_GetClipboardInfo(&cbinfo
);
2184 if (cbinfo
.flags
& CB_PROCESS
)
2186 /* Since we're still the owner, this wasn't initiated by
2187 another Wine process */
2188 if (OpenClipboard(hwnd
))
2190 /* Destroy private objects */
2191 SendMessageW(cbinfo
.hWndOwner
, WM_DESTROYCLIPBOARD
, 0, 0);
2193 /* Give up ownership of the windows clipboard */
2194 X11DRV_CLIPBOARD_ReleaseOwnership();
2199 if ((selType
== x11drv_atom(CLIPBOARD
)) && (selectionAcquired
& S_PRIMARY
))
2201 TRACE("Lost clipboard. Check if we need to release PRIMARY\n");
2204 if (selectionWindow
== XGetSelectionOwner(display
, XA_PRIMARY
))
2206 TRACE("We still own PRIMARY. Releasing PRIMARY.\n");
2207 XSetSelectionOwner(display
, XA_PRIMARY
, None
, time
);
2210 TRACE("We no longer own PRIMARY\n");
2211 wine_tsx11_unlock();
2213 else if ((selType
== XA_PRIMARY
) && (selectionAcquired
& S_CLIPBOARD
))
2215 TRACE("Lost PRIMARY. Check if we need to release CLIPBOARD\n");
2218 if (selectionWindow
== XGetSelectionOwner(display
,x11drv_atom(CLIPBOARD
)))
2220 TRACE("We still own CLIPBOARD. Releasing CLIPBOARD.\n");
2221 XSetSelectionOwner(display
, x11drv_atom(CLIPBOARD
), None
, time
);
2224 TRACE("We no longer own CLIPBOARD\n");
2225 wine_tsx11_unlock();
2228 selectionWindow
= None
;
2230 X11DRV_EmptyClipboard(FALSE
);
2232 /* Reset the selection flags now that we are done */
2233 selectionAcquired
= S_NOSELECTION
;
2238 /**************************************************************************
2239 * IsSelectionOwner (X11DRV.@)
2241 * Returns: TRUE if the selection is owned by this process, FALSE otherwise
2243 static BOOL
X11DRV_CLIPBOARD_IsSelectionOwner(void)
2245 return selectionAcquired
;
2249 /**************************************************************************
2250 * X11DRV Clipboard Exports
2251 **************************************************************************/
2254 /**************************************************************************
2255 * RegisterClipboardFormat (X11DRV.@)
2257 * Registers a custom X clipboard format
2258 * Returns: Format id or 0 on failure
2260 UINT
X11DRV_RegisterClipboardFormat(LPCWSTR FormatName
)
2262 LPWINE_CLIPFORMAT lpFormat
;
2264 if (FormatName
== NULL
) return 0;
2265 if (!(lpFormat
= register_format( FormatName
, 0 ))) return 0;
2266 return lpFormat
->wFormatID
;
2270 /**************************************************************************
2271 * X11DRV_GetClipboardFormatName
2273 INT
X11DRV_GetClipboardFormatName(UINT wFormat
, LPWSTR retStr
, INT maxlen
)
2275 LPWINE_CLIPFORMAT lpFormat
;
2277 TRACE("(%04X, %p, %d) !\n", wFormat
, retStr
, maxlen
);
2279 if (wFormat
< 0xc000)
2281 SetLastError(ERROR_INVALID_PARAMETER
);
2285 lpFormat
= X11DRV_CLIPBOARD_LookupFormat(wFormat
);
2287 if (!lpFormat
|| (lpFormat
->wFlags
& CF_FLAG_BUILTINFMT
))
2289 TRACE("Unknown format 0x%08x!\n", wFormat
);
2290 SetLastError(ERROR_INVALID_HANDLE
);
2294 lstrcpynW(retStr
, lpFormat
->Name
, maxlen
);
2296 return strlenW(retStr
);
2300 /**************************************************************************
2301 * AcquireClipboard (X11DRV.@)
2303 void X11DRV_AcquireClipboard(HWND hWndClipWindow
)
2305 Display
*display
= thread_display();
2308 * Acquire X selection if we don't already own it.
2309 * Note that we only acquire the selection if it hasn't been already
2310 * acquired by us, and ignore the fact that another X window may be
2311 * asserting ownership. The reason for this is we need *any* top level
2312 * X window to hold selection ownership. The actual clipboard data requests
2313 * are made via GetClipboardData from EVENT_SelectionRequest and this
2314 * ensures that the real HWND owner services the request.
2315 * If the owning X window gets destroyed the selection ownership is
2316 * re-cycled to another top level X window in X11DRV_CLIPBOARD_ResetOwner.
2319 if (!(selectionAcquired
== (S_PRIMARY
| S_CLIPBOARD
)))
2323 if (!hWndClipWindow
)
2324 hWndClipWindow
= GetActiveWindow();
2326 hWndClipWindow
= GetAncestor(hWndClipWindow
, GA_ROOT
);
2328 if (GetCurrentThreadId() != GetWindowThreadProcessId(hWndClipWindow
, NULL
))
2330 TRACE("Thread %lx is acquiring selection with thread %lx's window %p\n",
2331 GetCurrentThreadId(),
2332 GetWindowThreadProcessId(hWndClipWindow
, NULL
),
2334 if (!SendMessageW(hWndClipWindow
, WM_X11DRV_ACQUIRE_SELECTION
, 0, 0))
2335 ERR("Failed to acquire selection\n");
2339 owner
= X11DRV_get_whole_window(hWndClipWindow
);
2342 /* Grab PRIMARY selection if not owned */
2343 if (use_primary_selection
&& !(selectionAcquired
& S_PRIMARY
))
2344 XSetSelectionOwner(display
, XA_PRIMARY
, owner
, CurrentTime
);
2346 /* Grab CLIPBOARD selection if not owned */
2347 if (!(selectionAcquired
& S_CLIPBOARD
))
2348 XSetSelectionOwner(display
, x11drv_atom(CLIPBOARD
), owner
, CurrentTime
);
2350 if (use_primary_selection
&& XGetSelectionOwner(display
,XA_PRIMARY
) == owner
)
2351 selectionAcquired
|= S_PRIMARY
;
2353 if (XGetSelectionOwner(display
,x11drv_atom(CLIPBOARD
)) == owner
)
2354 selectionAcquired
|= S_CLIPBOARD
;
2355 wine_tsx11_unlock();
2357 if (selectionAcquired
)
2359 selectionWindow
= owner
;
2360 TRACE("Grabbed X selection, owner=(%08x)\n", (unsigned) owner
);
2365 ERR("Received request to acquire selection but process is already owner=(%08x)\n", (unsigned) selectionWindow
);
2370 /**************************************************************************
2371 * X11DRV_EmptyClipboard
2373 * Empty cached clipboard data.
2375 void X11DRV_EmptyClipboard(BOOL keepunowned
)
2379 LPWINE_CLIPDATA lpData
, lpStart
;
2380 LPWINE_CLIPDATA lpNext
= ClipData
;
2382 TRACE(" called with %d entries in cache.\n", ClipDataCount
);
2388 lpNext
= lpData
->NextData
;
2390 if (!keepunowned
|| !(lpData
->wFlags
& CF_FLAG_UNOWNED
))
2392 lpData
->PrevData
->NextData
= lpData
->NextData
;
2393 lpData
->NextData
->PrevData
= lpData
->PrevData
;
2395 if (lpData
== ClipData
)
2396 ClipData
= lpNext
!= lpData
? lpNext
: NULL
;
2398 X11DRV_CLIPBOARD_FreeData(lpData
);
2399 HeapFree(GetProcessHeap(), 0, lpData
);
2403 } while (lpNext
!= lpStart
);
2406 TRACE(" %d entries remaining in cache.\n", ClipDataCount
);
2411 /**************************************************************************
2412 * X11DRV_SetClipboardData
2414 BOOL
X11DRV_SetClipboardData(UINT wFormat
, HANDLE16 hData16
, HANDLE hData32
, BOOL owner
)
2417 BOOL bResult
= TRUE
;
2419 /* If it's not owned, data can only be set if the format data is not already owned
2420 and its rendering is not delayed */
2423 CLIPBOARDINFO cbinfo
;
2424 LPWINE_CLIPDATA lpRender
;
2426 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
2428 if ((!hData16
&& !hData32
) ||
2429 ((lpRender
= X11DRV_CLIPBOARD_LookupData(wFormat
)) &&
2430 !(lpRender
->wFlags
& CF_FLAG_UNOWNED
)))
2433 flags
= CF_FLAG_UNOWNED
;
2436 bResult
&= X11DRV_CLIPBOARD_InsertClipboardData(wFormat
, hData16
, hData32
, flags
, NULL
, TRUE
);
2442 /**************************************************************************
2443 * CountClipboardFormats
2445 INT
X11DRV_CountClipboardFormats(void)
2447 CLIPBOARDINFO cbinfo
;
2449 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
2451 TRACE(" count=%d\n", ClipDataCount
);
2453 return ClipDataCount
;
2457 /**************************************************************************
2458 * X11DRV_EnumClipboardFormats
2460 UINT
X11DRV_EnumClipboardFormats(UINT wFormat
)
2462 CLIPBOARDINFO cbinfo
;
2463 UINT wNextFormat
= 0;
2465 TRACE("(%04X)\n", wFormat
);
2467 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
2472 wNextFormat
= ClipData
->wFormatID
;
2476 LPWINE_CLIPDATA lpData
= X11DRV_CLIPBOARD_LookupData(wFormat
);
2478 if (lpData
&& lpData
->NextData
!= ClipData
)
2479 wNextFormat
= lpData
->NextData
->wFormatID
;
2486 /**************************************************************************
2487 * X11DRV_IsClipboardFormatAvailable
2489 BOOL
X11DRV_IsClipboardFormatAvailable(UINT wFormat
)
2492 CLIPBOARDINFO cbinfo
;
2494 TRACE("(%04X)\n", wFormat
);
2496 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
2498 if (wFormat
!= 0 && X11DRV_CLIPBOARD_LookupData(wFormat
))
2501 TRACE("(%04X)- ret(%d)\n", wFormat
, bRet
);
2507 /**************************************************************************
2508 * GetClipboardData (USER.142)
2510 BOOL
X11DRV_GetClipboardData(UINT wFormat
, HANDLE16
* phData16
, HANDLE
* phData32
)
2512 CLIPBOARDINFO cbinfo
;
2513 LPWINE_CLIPDATA lpRender
;
2515 TRACE("(%04X)\n", wFormat
);
2517 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
2519 if ((lpRender
= X11DRV_CLIPBOARD_LookupData(wFormat
)))
2521 if ( !lpRender
->hData32
)
2522 X11DRV_CLIPBOARD_RenderFormat(lpRender
);
2524 /* Convert between 32 -> 16 bit data, if necessary */
2525 if (lpRender
->hData32
&& !lpRender
->hData16
)
2529 if (lpRender
->wFormatID
== CF_METAFILEPICT
)
2530 size
= sizeof(METAFILEPICT16
);
2532 size
= GlobalSize(lpRender
->hData32
);
2534 lpRender
->hData16
= GlobalAlloc16(GMEM_ZEROINIT
, size
);
2536 if (!lpRender
->hData16
)
2537 ERR("(%04X) -- not enough memory in 16b heap\n", wFormat
);
2540 if (lpRender
->wFormatID
== CF_METAFILEPICT
)
2542 FIXME("\timplement function CopyMetaFilePict32to16\n");
2543 FIXME("\tin the appropriate file.\n");
2544 #ifdef SOMEONE_IMPLEMENTED_ME
2545 CopyMetaFilePict32to16(GlobalLock16(lpRender
->hData16
),
2546 GlobalLock(lpRender
->hData32
));
2551 memcpy(GlobalLock16(lpRender
->hData16
),
2552 GlobalLock(lpRender
->hData32
), size
);
2555 GlobalUnlock16(lpRender
->hData16
);
2556 GlobalUnlock(lpRender
->hData32
);
2560 /* Convert between 32 -> 16 bit data, if necessary */
2561 if (lpRender
->hData16
&& !lpRender
->hData32
)
2565 if (lpRender
->wFormatID
== CF_METAFILEPICT
)
2566 size
= sizeof(METAFILEPICT16
);
2568 size
= GlobalSize(lpRender
->hData32
);
2570 lpRender
->hData32
= GlobalAlloc(GMEM_ZEROINIT
| GMEM_MOVEABLE
|
2571 GMEM_DDESHARE
, size
);
2573 if (lpRender
->wFormatID
== CF_METAFILEPICT
)
2575 FIXME("\timplement function CopyMetaFilePict16to32\n");
2576 FIXME("\tin the appropriate file.\n");
2577 #ifdef SOMEONE_IMPLEMENTED_ME
2578 CopyMetaFilePict16to32(GlobalLock16(lpRender
->hData32
),
2579 GlobalLock(lpRender
->hData16
));
2584 memcpy(GlobalLock(lpRender
->hData32
),
2585 GlobalLock16(lpRender
->hData16
), size
);
2588 GlobalUnlock(lpRender
->hData32
);
2589 GlobalUnlock16(lpRender
->hData16
);
2593 *phData16
= lpRender
->hData16
;
2596 *phData32
= lpRender
->hData32
;
2598 TRACE(" returning hData16(%04x) hData32(%p) (type %d)\n",
2599 lpRender
->hData16
, lpRender
->hData32
, lpRender
->wFormatID
);
2601 return lpRender
->hData16
|| lpRender
->hData32
;
2608 /**************************************************************************
2609 * ResetSelectionOwner (X11DRV.@)
2611 * Called from DestroyWindow() to prevent X selection from being lost when
2612 * a top level window is destroyed, by switching ownership to another top
2614 * Any top level window can own the selection. See X11DRV_CLIPBOARD_Acquire
2615 * for a more detailed description of this.
2617 void X11DRV_ResetSelectionOwner(HWND hwnd
, BOOL bFooBar
)
2619 Display
*display
= thread_display();
2620 HWND hWndClipOwner
= 0;
2622 Window XWnd
= X11DRV_get_whole_window(hwnd
);
2623 BOOL bLostSelection
= FALSE
;
2624 Window selectionPrevWindow
;
2626 /* There is nothing to do if we don't own the selection,
2627 * or if the X window which currently owns the selection is different
2628 * from the one passed in.
2630 if (!selectionAcquired
|| XWnd
!= selectionWindow
2631 || selectionWindow
== None
)
2634 if ((bFooBar
&& XWnd
) || (!bFooBar
&& !XWnd
))
2637 hWndClipOwner
= GetClipboardOwner();
2639 TRACE("clipboard owner = %p, selection window = %08x\n",
2640 hWndClipOwner
, (unsigned)selectionWindow
);
2642 /* now try to salvage current selection from being destroyed by X */
2643 TRACE("checking %08x\n", (unsigned) XWnd
);
2645 selectionPrevWindow
= selectionWindow
;
2646 selectionWindow
= None
;
2648 if (!(tmp
= GetWindow(hwnd
, GW_HWNDNEXT
)))
2649 tmp
= GetWindow(hwnd
, GW_HWNDFIRST
);
2651 if (tmp
&& tmp
!= hwnd
)
2652 selectionWindow
= X11DRV_get_whole_window(tmp
);
2654 if (selectionWindow
!= None
)
2656 /* We must pretend that we don't own the selection while making the switch
2657 * since a SelectionClear event will be sent to the last owner.
2658 * If there is no owner X11DRV_CLIPBOARD_ReleaseSelection will do nothing.
2660 int saveSelectionState
= selectionAcquired
;
2661 selectionAcquired
= S_NOSELECTION
;
2663 TRACE("\tswitching selection from %08x to %08x\n",
2664 (unsigned)selectionPrevWindow
, (unsigned)selectionWindow
);
2668 /* Assume ownership for the PRIMARY and CLIPBOARD selection */
2669 if (saveSelectionState
& S_PRIMARY
)
2670 XSetSelectionOwner(display
, XA_PRIMARY
, selectionWindow
, CurrentTime
);
2672 XSetSelectionOwner(display
, x11drv_atom(CLIPBOARD
), selectionWindow
, CurrentTime
);
2674 /* Restore the selection masks */
2675 selectionAcquired
= saveSelectionState
;
2677 /* Lose the selection if something went wrong */
2678 if (((saveSelectionState
& S_PRIMARY
) &&
2679 (XGetSelectionOwner(display
, XA_PRIMARY
) != selectionWindow
)) ||
2680 (XGetSelectionOwner(display
, x11drv_atom(CLIPBOARD
)) != selectionWindow
))
2682 bLostSelection
= TRUE
;
2684 wine_tsx11_unlock();
2688 bLostSelection
= TRUE
;
2693 TRACE("Lost the selection!\n");
2695 X11DRV_CLIPBOARD_ReleaseOwnership();
2696 selectionAcquired
= S_NOSELECTION
;
2697 selectionWindow
= 0;
2702 /**************************************************************************
2703 * X11DRV_CLIPBOARD_SynthesizeData
2705 static BOOL
X11DRV_CLIPBOARD_SynthesizeData(UINT wFormatID
)
2708 LPWINE_CLIPDATA lpSource
= NULL
;
2710 TRACE(" %d\n", wFormatID
);
2712 /* Don't need to synthesize if it already exists */
2713 if (X11DRV_CLIPBOARD_LookupData(wFormatID
))
2716 if (wFormatID
== CF_UNICODETEXT
|| wFormatID
== CF_TEXT
|| wFormatID
== CF_OEMTEXT
)
2718 bsyn
= ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT
)) &&
2719 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) ||
2720 ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_TEXT
)) &&
2721 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) ||
2722 ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT
)) &&
2723 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
);
2725 else if (wFormatID
== CF_ENHMETAFILE
)
2727 bsyn
= (lpSource
= X11DRV_CLIPBOARD_LookupData(CF_METAFILEPICT
)) &&
2728 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
;
2730 else if (wFormatID
== CF_METAFILEPICT
)
2732 bsyn
= (lpSource
= X11DRV_CLIPBOARD_LookupData(CF_METAFILEPICT
)) &&
2733 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
;
2735 else if (wFormatID
== CF_DIB
)
2737 bsyn
= (lpSource
= X11DRV_CLIPBOARD_LookupData(CF_BITMAP
)) &&
2738 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
;
2740 else if (wFormatID
== CF_BITMAP
)
2742 bsyn
= (lpSource
= X11DRV_CLIPBOARD_LookupData(CF_DIB
)) &&
2743 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
;
2747 X11DRV_CLIPBOARD_InsertClipboardData(wFormatID
, 0, 0, CF_FLAG_SYNTHESIZED
, NULL
, TRUE
);
2754 /**************************************************************************
2755 * X11DRV_EndClipboardUpdate
2757 * Add locale if it hasn't already been added
2759 void X11DRV_EndClipboardUpdate(void)
2761 INT count
= ClipDataCount
;
2763 /* Do Unicode <-> Text <-> OEM mapping */
2764 X11DRV_CLIPBOARD_SynthesizeData(CF_UNICODETEXT
);
2765 X11DRV_CLIPBOARD_SynthesizeData(CF_TEXT
);
2766 X11DRV_CLIPBOARD_SynthesizeData(CF_OEMTEXT
);
2768 /* Enhmetafile <-> MetafilePict mapping */
2769 X11DRV_CLIPBOARD_SynthesizeData(CF_ENHMETAFILE
);
2770 X11DRV_CLIPBOARD_SynthesizeData(CF_METAFILEPICT
);
2772 /* DIB <-> Bitmap mapping */
2773 X11DRV_CLIPBOARD_SynthesizeData(CF_DIB
);
2774 X11DRV_CLIPBOARD_SynthesizeData(CF_BITMAP
);
2776 TRACE("%d formats added to cached data\n", ClipDataCount
- count
);
2780 /***********************************************************************
2781 * X11DRV_SelectionRequest_TARGETS
2782 * Service a TARGETS selection request event
2784 static Atom
X11DRV_SelectionRequest_TARGETS( Display
*display
, Window requestor
,
2785 Atom target
, Atom rprop
)
2790 LPWINE_CLIPFORMAT lpFormats
;
2791 LPWINE_CLIPDATA lpData
;
2794 * Count the number of items we wish to expose as selection targets.
2796 cTargets
= 1; /* Include TARGETS */
2798 if (!(lpData
= ClipData
)) return None
;
2802 lpFormats
= ClipFormats
;
2806 if ((lpFormats
->wFormatID
== lpData
->wFormatID
) &&
2807 lpFormats
->lpDrvExportFunc
)
2810 lpFormats
= lpFormats
->NextFormat
;
2813 lpData
= lpData
->NextData
;
2815 while (lpData
!= ClipData
);
2817 TRACE(" found %ld formats\n", cTargets
);
2819 /* Allocate temp buffer */
2820 targets
= HeapAlloc( GetProcessHeap(), 0, cTargets
* sizeof(Atom
));
2826 targets
[i
++] = x11drv_atom(TARGETS
);
2830 lpFormats
= ClipFormats
;
2834 if ((lpFormats
->wFormatID
== lpData
->wFormatID
) &&
2835 lpFormats
->lpDrvExportFunc
)
2836 targets
[i
++] = lpFormats
->drvData
;
2838 lpFormats
= lpFormats
->NextFormat
;
2841 lpData
= lpData
->NextData
;
2843 while (lpData
!= ClipData
);
2847 if (TRACE_ON(clipboard
))
2850 for ( i
= 0; i
< cTargets
; i
++)
2854 char *itemFmtName
= XGetAtomName(display
, targets
[i
]);
2855 TRACE("\tAtom# %d: Property %ld Type %s\n", i
, targets
[i
], itemFmtName
);
2861 /* We may want to consider setting the type to xaTargets instead,
2862 * in case some apps expect this instead of XA_ATOM */
2863 XChangeProperty(display
, requestor
, rprop
, XA_ATOM
, 32,
2864 PropModeReplace
, (unsigned char *)targets
, cTargets
);
2865 wine_tsx11_unlock();
2867 HeapFree(GetProcessHeap(), 0, targets
);
2873 /***********************************************************************
2874 * X11DRV_SelectionRequest_MULTIPLE
2875 * Service a MULTIPLE selection request event
2876 * rprop contains a list of (target,property) atom pairs.
2877 * The first atom names a target and the second names a property.
2878 * The effect is as if we have received a sequence of SelectionRequest events
2879 * (one for each atom pair) except that:
2880 * 1. We reply with a SelectionNotify only when all the requested conversions
2881 * have been performed.
2882 * 2. If we fail to convert the target named by an atom in the MULTIPLE property,
2883 * we replace the atom in the property by None.
2885 static Atom
X11DRV_SelectionRequest_MULTIPLE( HWND hWnd
, XSelectionRequestEvent
*pevent
)
2887 Display
*display
= pevent
->display
;
2889 Atom atype
=AnyPropertyType
;
2891 unsigned long remain
;
2892 Atom
* targetPropList
=NULL
;
2893 unsigned long cTargetPropList
= 0;
2895 /* If the specified property is None the requestor is an obsolete client.
2896 * We support these by using the specified target atom as the reply property.
2898 rprop
= pevent
->property
;
2900 rprop
= pevent
->target
;
2904 /* Read the MULTIPLE property contents. This should contain a list of
2905 * (target,property) atom pairs.
2908 if(XGetWindowProperty(display
, pevent
->requestor
, rprop
,
2909 0, 0x3FFF, False
, AnyPropertyType
, &atype
,&aformat
,
2910 &cTargetPropList
, &remain
,
2911 (unsigned char**)&targetPropList
) != Success
)
2913 wine_tsx11_unlock();
2914 TRACE("\tCouldn't read MULTIPLE property\n");
2918 TRACE("\tType %s,Format %d,nItems %ld, Remain %ld\n",
2919 XGetAtomName(display
, atype
), aformat
, cTargetPropList
, remain
);
2920 wine_tsx11_unlock();
2923 * Make sure we got what we expect.
2924 * NOTE: According to the X-ICCCM Version 2.0 documentation the property sent
2925 * in a MULTIPLE selection request should be of type ATOM_PAIR.
2926 * However some X apps(such as XPaint) are not compliant with this and return
2927 * a user defined atom in atype when XGetWindowProperty is called.
2928 * The data *is* an atom pair but is not denoted as such.
2930 if(aformat
== 32 /* atype == xAtomPair */ )
2934 /* Iterate through the ATOM_PAIR list and execute a SelectionRequest
2935 * for each (target,property) pair */
2937 for (i
= 0; i
< cTargetPropList
; i
+=2)
2939 XSelectionRequestEvent event
;
2941 if (TRACE_ON(clipboard
))
2943 char *targetName
, *propName
;
2945 targetName
= XGetAtomName(display
, targetPropList
[i
]);
2946 propName
= XGetAtomName(display
, targetPropList
[i
+1]);
2947 TRACE("MULTIPLE(%d): Target='%s' Prop='%s'\n",
2948 i
/2, targetName
, propName
);
2951 wine_tsx11_unlock();
2954 /* We must have a non "None" property to service a MULTIPLE target atom */
2955 if ( !targetPropList
[i
+1] )
2957 TRACE("\tMULTIPLE(%d): Skipping target with empty property!\n", i
);
2961 /* Set up an XSelectionRequestEvent for this (target,property) pair */
2962 memcpy( &event
, pevent
, sizeof(XSelectionRequestEvent
) );
2963 event
.target
= targetPropList
[i
];
2964 event
.property
= targetPropList
[i
+1];
2966 /* Fire a SelectionRequest, informing the handler that we are processing
2967 * a MULTIPLE selection request event.
2969 X11DRV_HandleSelectionRequest( hWnd
, &event
, TRUE
);
2973 /* Free the list of targets/properties */
2975 XFree(targetPropList
);
2976 wine_tsx11_unlock();
2983 /***********************************************************************
2984 * X11DRV_HandleSelectionRequest
2985 * Process an event selection request event.
2986 * The bIsMultiple flag is used to signal when EVENT_SelectionRequest is called
2987 * recursively while servicing a "MULTIPLE" selection target.
2989 * Note: We only receive this event when WINE owns the X selection
2991 static void X11DRV_HandleSelectionRequest( HWND hWnd
, XSelectionRequestEvent
*event
, BOOL bIsMultiple
)
2993 Display
*display
= event
->display
;
2994 XSelectionEvent result
;
2996 Window request
= event
->requestor
;
3001 * We can only handle the selection request if :
3002 * The selection is PRIMARY or CLIPBOARD, AND we can successfully open the clipboard.
3003 * Don't do these checks or open the clipboard while recursively processing MULTIPLE,
3004 * since this has been already done.
3008 if (((event
->selection
!= XA_PRIMARY
) && (event
->selection
!= x11drv_atom(CLIPBOARD
))))
3012 /* If the specified property is None the requestor is an obsolete client.
3013 * We support these by using the specified target atom as the reply property.
3015 rprop
= event
->property
;
3017 rprop
= event
->target
;
3019 if(event
->target
== x11drv_atom(TARGETS
)) /* Return a list of all supported targets */
3021 /* TARGETS selection request */
3022 rprop
= X11DRV_SelectionRequest_TARGETS( display
, request
, event
->target
, rprop
);
3024 else if(event
->target
== x11drv_atom(MULTIPLE
)) /* rprop contains a list of (target, property) atom pairs */
3026 /* MULTIPLE selection request */
3027 rprop
= X11DRV_SelectionRequest_MULTIPLE( hWnd
, event
);
3031 LPWINE_CLIPFORMAT lpFormat
= X11DRV_CLIPBOARD_LookupProperty(NULL
, event
->target
);
3033 if (lpFormat
&& lpFormat
->lpDrvExportFunc
)
3035 LPWINE_CLIPDATA lpData
= X11DRV_CLIPBOARD_LookupData(lpFormat
->wFormatID
);
3039 unsigned char* lpClipData
;
3041 HANDLE hClipData
= lpFormat
->lpDrvExportFunc(request
, event
->target
,
3042 rprop
, lpData
, &cBytes
);
3044 if (hClipData
&& (lpClipData
= GlobalLock(hClipData
)))
3046 TRACE("\tUpdating property %s, %ld bytes\n", debugstr_w(lpFormat
->Name
), cBytes
);
3049 XChangeProperty(display
, request
, rprop
, event
->target
,
3050 8, PropModeReplace
, (unsigned char *)lpClipData
, cBytes
);
3051 wine_tsx11_unlock();
3053 GlobalUnlock(hClipData
);
3054 GlobalFree(hClipData
);
3062 * SelectionNotify should be sent only at the end of a MULTIPLE request
3066 result
.type
= SelectionNotify
;
3067 result
.display
= display
;
3068 result
.requestor
= request
;
3069 result
.selection
= event
->selection
;
3070 result
.property
= rprop
;
3071 result
.target
= event
->target
;
3072 result
.time
= event
->time
;
3073 TRACE("Sending SelectionNotify event...\n");
3075 XSendEvent(display
,event
->requestor
,False
,NoEventMask
,(XEvent
*)&result
);
3076 wine_tsx11_unlock();
3081 /***********************************************************************
3082 * X11DRV_SelectionRequest
3084 void X11DRV_SelectionRequest( HWND hWnd
, XEvent
*event
)
3087 X11DRV_HandleSelectionRequest( hWnd
, &event
->xselectionrequest
, FALSE
);
3091 /***********************************************************************
3092 * X11DRV_SelectionClear
3094 void X11DRV_SelectionClear( HWND hWnd
, XEvent
*xev
)
3096 XSelectionClearEvent
*event
= &xev
->xselectionclear
;
3098 if (event
->selection
== XA_PRIMARY
|| event
->selection
== x11drv_atom(CLIPBOARD
))
3099 X11DRV_CLIPBOARD_ReleaseSelection( event
->selection
, event
->window
, hWnd
, event
->time
);