2 * WIN32 clipboard implementation
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 * This file contains the implementation for the WIN32 Clipboard API
25 * and Wine's internal clipboard cache.
26 * The actual contents of the clipboard are held in the clipboard cache.
27 * The internal implementation talks to a "clipboard driver" to fill or
28 * expose the cache to the native device. (Currently only the X11 and
29 * TTY clipboard driver are available)
33 #include "wine/port.h"
37 #include <sys/types.h>
49 #include "user_private.h"
52 #include "wine/debug.h"
53 #include "wine/unicode.h"
54 #include "wine/server.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(clipboard
);
58 #define CF_REGFORMATBASE 0xC000
65 } CLIPBOARDINFO
, *LPCLIPBOARDINFO
;
68 * Indicates if data has changed since open.
70 static BOOL bCBHasChanged
= FALSE
;
73 /**************************************************************************
74 * CLIPBOARD_SetClipboardOwner
76 * Set the global wineserver clipboard owner. The current process will
77 * be the owner and <hWnd> will get the render notifications.
79 static BOOL
CLIPBOARD_SetClipboardOwner(HWND hWnd
)
83 TRACE(" hWnd(%p)\n", hWnd
);
85 SERVER_START_REQ( set_clipboard_info
)
87 req
->flags
= SET_CB_OWNER
;
88 req
->owner
= wine_server_user_handle( hWnd
);
89 bRet
= !wine_server_call_err( req
);
97 /**************************************************************************
98 * CLIPBOARD_GetClipboardInfo
100 static BOOL
CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo
)
104 SERVER_START_REQ( set_clipboard_info
)
108 if (((bRet
= !wine_server_call_err( req
))))
110 cbInfo
->hWndOpen
= wine_server_ptr_handle( reply
->old_clipboard
);
111 cbInfo
->hWndOwner
= wine_server_ptr_handle( reply
->old_owner
);
112 cbInfo
->flags
= reply
->flags
;
121 /**************************************************************************
122 * CLIPBOARD_ReleaseOwner
124 BOOL
CLIPBOARD_ReleaseOwner(void)
128 SERVER_START_REQ( set_clipboard_info
)
130 req
->flags
= SET_CB_RELOWNER
| SET_CB_SEQNO
;
132 if (wine_server_call_err( req
))
134 ERR("Failed to set clipboard.\n");
147 /**************************************************************************
148 * CLIPBOARD_SetClipboardViewer
150 static HWND
CLIPBOARD_SetClipboardViewer( HWND hWnd
)
154 SERVER_START_REQ( set_clipboard_info
)
156 req
->flags
= SET_CB_VIEWER
;
157 req
->viewer
= wine_server_user_handle( hWnd
);
158 if (!wine_server_call_err( req
))
159 hwndPrev
= wine_server_ptr_handle( reply
->old_viewer
);
166 /**************************************************************************
167 * WIN32 Clipboard implementation
168 **************************************************************************/
170 /**************************************************************************
171 * RegisterClipboardFormatW (USER32.@)
173 UINT WINAPI
RegisterClipboardFormatW( LPCWSTR name
)
175 return GlobalAddAtomW( name
);
179 /**************************************************************************
180 * RegisterClipboardFormatA (USER32.@)
182 UINT WINAPI
RegisterClipboardFormatA( LPCSTR name
)
184 return GlobalAddAtomA( name
);
188 /**************************************************************************
189 * GetClipboardFormatNameW (USER32.@)
191 INT WINAPI
GetClipboardFormatNameW(UINT wFormat
, LPWSTR retStr
, INT maxlen
)
193 if (wFormat
< MAXINTATOM
) return 0;
194 return GlobalGetAtomNameW( wFormat
, retStr
, maxlen
);
198 /**************************************************************************
199 * GetClipboardFormatNameA (USER32.@)
201 INT WINAPI
GetClipboardFormatNameA(UINT wFormat
, LPSTR retStr
, INT maxlen
)
203 if (wFormat
< MAXINTATOM
) return 0;
204 return GlobalGetAtomNameA( wFormat
, retStr
, maxlen
);
208 /**************************************************************************
209 * OpenClipboard (USER32.@)
211 * Note: Netscape uses NULL hWnd to open the clipboard.
213 BOOL WINAPI
OpenClipboard( HWND hWnd
)
217 TRACE("(%p)...\n", hWnd
);
219 SERVER_START_REQ( set_clipboard_info
)
221 req
->flags
= SET_CB_OPEN
;
222 req
->clipboard
= wine_server_user_handle( hWnd
);
223 bRet
= !wine_server_call( req
);
227 TRACE(" returning %i\n", bRet
);
233 /**************************************************************************
234 * CloseClipboard (USER32.@)
236 BOOL WINAPI
CloseClipboard(void)
241 TRACE("() Changed=%d\n", bCBHasChanged
);
243 SERVER_START_REQ( set_clipboard_info
)
245 req
->flags
= SET_CB_CLOSE
;
246 if (bCBHasChanged
) req
->flags
|= SET_CB_SEQNO
;
247 if ((ret
= !wine_server_call_err( req
)))
248 viewer
= wine_server_ptr_handle( reply
->old_viewer
);
252 if (!ret
) return FALSE
;
256 USER_Driver
->pEndClipboardUpdate();
257 bCBHasChanged
= FALSE
;
258 if (viewer
) SendNotifyMessageW(viewer
, WM_DRAWCLIPBOARD
, (WPARAM
) GetClipboardOwner(), 0);
264 /**************************************************************************
265 * EmptyClipboard (USER32.@)
266 * Empties and acquires ownership of the clipboard
268 BOOL WINAPI
EmptyClipboard(void)
270 CLIPBOARDINFO cbinfo
;
274 if (!CLIPBOARD_GetClipboardInfo(&cbinfo
) ||
275 ~cbinfo
.flags
& CB_OPEN
)
277 WARN("Clipboard not opened by calling task!\n");
278 SetLastError(ERROR_CLIPBOARD_NOT_OPEN
);
282 /* Destroy private objects */
283 if (cbinfo
.hWndOwner
)
284 SendMessageW(cbinfo
.hWndOwner
, WM_DESTROYCLIPBOARD
, 0, 0);
286 /* Tell the driver to acquire the selection. The current owner
287 * will be signaled to delete its own cache. */
289 /* Assign ownership of the clipboard to the current client. We do
290 * this before acquiring the selection so that when we do acquire the
291 * selection and the selection loser gets notified, it can check if
292 * it has lost the Wine clipboard ownership. If it did then it knows
293 * that a WM_DESTROYCLIPBOARD has already been sent. Otherwise it
294 * lost the selection to a X app and it should send the
295 * WM_DESTROYCLIPBOARD itself. */
296 CLIPBOARD_SetClipboardOwner(cbinfo
.hWndOpen
);
298 /* Acquire the selection. This will notify the previous owner
299 * to clear its cache. */
300 USER_Driver
->pAcquireClipboard(cbinfo
.hWndOpen
);
302 USER_Driver
->pEmptyClipboard();
304 bCBHasChanged
= TRUE
;
310 /**************************************************************************
311 * GetClipboardOwner (USER32.@)
312 * FIXME: Can't return the owner if the clipboard is owned by an external X-app
314 HWND WINAPI
GetClipboardOwner(void)
318 SERVER_START_REQ( set_clipboard_info
)
321 if (!wine_server_call_err( req
)) hWndOwner
= wine_server_ptr_handle( reply
->old_owner
);
325 TRACE(" hWndOwner(%p)\n", hWndOwner
);
331 /**************************************************************************
332 * GetOpenClipboardWindow (USER32.@)
334 HWND WINAPI
GetOpenClipboardWindow(void)
338 SERVER_START_REQ( set_clipboard_info
)
341 if (!wine_server_call_err( req
)) hWndOpen
= wine_server_ptr_handle( reply
->old_clipboard
);
345 TRACE(" hWndClipWindow(%p)\n", hWndOpen
);
351 /**************************************************************************
352 * SetClipboardViewer (USER32.@)
354 HWND WINAPI
SetClipboardViewer( HWND hWnd
)
356 HWND hwndPrev
= CLIPBOARD_SetClipboardViewer(hWnd
);
359 SendNotifyMessageW(hWnd
, WM_DRAWCLIPBOARD
, (WPARAM
) GetClipboardOwner(), 0);
360 TRACE("(%p): returning %p\n", hWnd
, hwndPrev
);
366 /**************************************************************************
367 * GetClipboardViewer (USER32.@)
369 HWND WINAPI
GetClipboardViewer(void)
373 SERVER_START_REQ( set_clipboard_info
)
376 if (!wine_server_call_err( req
)) hWndViewer
= wine_server_ptr_handle( reply
->old_viewer
);
380 TRACE(" hWndViewer=%p\n", hWndViewer
);
386 /**************************************************************************
387 * ChangeClipboardChain (USER32.@)
389 BOOL WINAPI
ChangeClipboardChain(HWND hWnd
, HWND hWndNext
)
392 HWND hWndViewer
= GetClipboardViewer();
396 if (WIN_GetFullHandle(hWnd
) == hWndViewer
)
397 CLIPBOARD_SetClipboardViewer(WIN_GetFullHandle(hWndNext
));
399 bRet
= !SendMessageW(hWndViewer
, WM_CHANGECBCHAIN
, (WPARAM
)hWnd
, (LPARAM
)hWndNext
);
402 ERR("hWndViewer is lost\n");
408 /**************************************************************************
409 * SetClipboardData (USER32.@)
411 HANDLE WINAPI
SetClipboardData(UINT wFormat
, HANDLE hData
)
413 CLIPBOARDINFO cbinfo
;
416 TRACE("(%04X, %p) !\n", wFormat
, hData
);
418 /* If it's not owned, data can only be set if the format isn't
419 available and its rendering is not delayed */
420 if (!CLIPBOARD_GetClipboardInfo(&cbinfo
) ||
421 (!(cbinfo
.flags
& CB_OWNER
) && !hData
))
423 WARN("Clipboard not owned by calling task. Operation failed.\n");
427 if (USER_Driver
->pSetClipboardData(wFormat
, hData
, cbinfo
.flags
& CB_OWNER
))
430 bCBHasChanged
= TRUE
;
437 /**************************************************************************
438 * CountClipboardFormats (USER32.@)
440 INT WINAPI
CountClipboardFormats(void)
442 INT count
= USER_Driver
->pCountClipboardFormats();
443 TRACE("returning %d\n", count
);
448 /**************************************************************************
449 * EnumClipboardFormats (USER32.@)
451 UINT WINAPI
EnumClipboardFormats(UINT wFormat
)
453 CLIPBOARDINFO cbinfo
;
455 TRACE("(%04X)\n", wFormat
);
457 if (!CLIPBOARD_GetClipboardInfo(&cbinfo
) ||
458 (~cbinfo
.flags
& CB_OPEN
))
460 WARN("Clipboard not opened by calling task.\n");
461 SetLastError(ERROR_CLIPBOARD_NOT_OPEN
);
464 return USER_Driver
->pEnumClipboardFormats(wFormat
);
468 /**************************************************************************
469 * IsClipboardFormatAvailable (USER32.@)
471 BOOL WINAPI
IsClipboardFormatAvailable(UINT wFormat
)
473 BOOL bret
= USER_Driver
->pIsClipboardFormatAvailable(wFormat
);
474 TRACE("%04x, returning %d\n", wFormat
, bret
);
479 /**************************************************************************
480 * GetClipboardData (USER32.@)
482 HANDLE WINAPI
GetClipboardData(UINT wFormat
)
485 CLIPBOARDINFO cbinfo
;
487 TRACE("%04x\n", wFormat
);
489 if (!CLIPBOARD_GetClipboardInfo(&cbinfo
) ||
490 (~cbinfo
.flags
& CB_OPEN
))
492 WARN("Clipboard not opened by calling task.\n");
493 SetLastError(ERROR_CLIPBOARD_NOT_OPEN
);
497 hData
= USER_Driver
->pGetClipboardData( wFormat
);
499 TRACE("returning %p\n", hData
);
504 /**************************************************************************
505 * GetPriorityClipboardFormat (USER32.@)
507 INT WINAPI
GetPriorityClipboardFormat(UINT
*list
, INT nCount
)
513 if(CountClipboardFormats() == 0)
516 for (i
= 0; i
< nCount
; i
++)
517 if (IsClipboardFormatAvailable(list
[i
]))
524 /**************************************************************************
525 * GetClipboardSequenceNumber (USER32.@)
526 * Supported on Win2k/Win98
527 * MSDN: Windows clipboard code keeps a serial number for the clipboard
528 * for each window station. The number is incremented whenever the
529 * contents change or are emptied.
530 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
532 DWORD WINAPI
GetClipboardSequenceNumber(VOID
)
536 SERVER_START_REQ( set_clipboard_info
)
539 if (!wine_server_call_err( req
)) seqno
= reply
->seqno
;
543 TRACE("returning %x\n", seqno
);
547 /**************************************************************************
548 * AddClipboardFormatListener (USER32.@)
550 BOOL WINAPI
AddClipboardFormatListener(HWND hwnd
)
552 FIXME("%p: stub\n", hwnd
);
556 /**************************************************************************
557 * RemoveClipboardFormatListener (USER32.@)
559 BOOL WINAPI
RemoveClipboardFormatListener(HWND hwnd
)
561 FIXME("%p: stub\n", hwnd
);