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 "wine/winbase16.h"
50 #include "user_private.h"
53 #include "wine/debug.h"
54 #include "wine/unicode.h"
55 #include "wine/server.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(clipboard);
59 #define CF_REGFORMATBASE 0xC000
68 } CLIPBOARDINFO, *LPCLIPBOARDINFO;
71 * Indicates if data has changed since open.
73 static BOOL bCBHasChanged = FALSE;
76 /**************************************************************************
77 * CLIPBOARD_SetClipboardOwner
79 * Set the global wineserver clipboard owner. The current process will
80 * be the owner and <hWnd> will get the render notifications.
82 static BOOL CLIPBOARD_SetClipboardOwner(HWND hWnd)
86 TRACE(" hWnd(%p)\n", hWnd);
88 SERVER_START_REQ( set_clipboard_info )
90 req->flags = SET_CB_OWNER;
91 req->owner = wine_server_user_handle( hWnd );
92 bRet = !wine_server_call_err( req );
100 /**************************************************************************
101 * CLIPBOARD_GetClipboardInfo
103 static BOOL CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo)
107 SERVER_START_REQ( set_clipboard_info )
111 if (((bRet = !wine_server_call_err( req ))))
113 cbInfo->hWndOpen = wine_server_ptr_handle( reply->old_clipboard );
114 cbInfo->hWndOwner = wine_server_ptr_handle( reply->old_owner );
115 cbInfo->hWndViewer = wine_server_ptr_handle( reply->old_viewer );
116 cbInfo->seqno = reply->seqno;
117 cbInfo->flags = reply->flags;
126 /**************************************************************************
127 * CLIPBOARD_ReleaseOwner
129 BOOL CLIPBOARD_ReleaseOwner(void)
133 SERVER_START_REQ( set_clipboard_info )
135 req->flags = SET_CB_RELOWNER | SET_CB_SEQNO;
137 if (wine_server_call_err( req ))
139 ERR("Failed to set clipboard.\n");
152 /**************************************************************************
153 * CLIPBOARD_OpenClipboard
155 static BOOL CLIPBOARD_OpenClipboard(HWND hWnd)
159 SERVER_START_REQ( set_clipboard_info )
161 req->flags = SET_CB_OPEN;
162 req->clipboard = wine_server_user_handle( hWnd );
163 bRet = !wine_server_call( req );
171 /**************************************************************************
172 * CLIPBOARD_CloseClipboard
174 static BOOL CLIPBOARD_CloseClipboard(void)
178 TRACE(" Changed=%d\n", bCBHasChanged);
180 SERVER_START_REQ( set_clipboard_info )
182 req->flags = SET_CB_CLOSE;
183 if (bCBHasChanged) req->flags |= SET_CB_SEQNO;
184 bRet = !wine_server_call_err( req );
191 /**************************************************************************
192 * CLIPBOARD_SetClipboardViewer
194 static HWND CLIPBOARD_SetClipboardViewer( HWND hWnd )
198 SERVER_START_REQ( set_clipboard_info )
200 req->flags = SET_CB_VIEWER;
201 req->viewer = wine_server_user_handle( hWnd );
202 if (!wine_server_call_err( req ))
203 hwndPrev = wine_server_ptr_handle( reply->old_viewer );
210 /**************************************************************************
211 * WIN32 Clipboard implementation
212 **************************************************************************/
214 /**************************************************************************
215 * RegisterClipboardFormatW (USER32.@)
217 UINT WINAPI RegisterClipboardFormatW(LPCWSTR FormatName)
219 return USER_Driver->pRegisterClipboardFormat(FormatName);
223 /**************************************************************************
224 * RegisterClipboardFormatA (USER32.@)
226 UINT WINAPI RegisterClipboardFormatA(LPCSTR formatName)
232 len = MultiByteToWideChar(CP_ACP, 0, formatName, -1, NULL, 0);
233 wFormat = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
234 MultiByteToWideChar(CP_ACP, 0, formatName, -1, wFormat, len);
236 ret = RegisterClipboardFormatW(wFormat);
237 HeapFree(GetProcessHeap(), 0, wFormat);
242 /**************************************************************************
243 * GetClipboardFormatNameW (USER32.@)
245 INT WINAPI GetClipboardFormatNameW(UINT wFormat, LPWSTR retStr, INT maxlen)
247 return USER_Driver->pGetClipboardFormatName(wFormat, retStr, maxlen);
251 /**************************************************************************
252 * GetClipboardFormatNameA (USER32.@)
254 INT WINAPI GetClipboardFormatNameA(UINT wFormat, LPSTR retStr, INT maxlen)
257 LPWSTR p = HeapAlloc( GetProcessHeap(), 0, maxlen*sizeof(WCHAR) );
258 if(p == NULL) return 0; /* FIXME: is this the correct failure value? */
260 ret = GetClipboardFormatNameW( wFormat, p, maxlen );
262 if (ret && maxlen > 0 && !WideCharToMultiByte( CP_ACP, 0, p, -1, retStr, maxlen, 0, 0))
263 retStr[maxlen-1] = 0;
264 HeapFree( GetProcessHeap(), 0, p );
269 /**************************************************************************
270 * OpenClipboard (USER32.@)
272 * Note: Netscape uses NULL hWnd to open the clipboard.
274 BOOL WINAPI OpenClipboard( HWND hWnd )
278 TRACE("(%p)...\n", hWnd);
280 bRet = CLIPBOARD_OpenClipboard(hWnd);
282 TRACE(" returning %i\n", bRet);
288 /**************************************************************************
289 * CloseClipboard (USER32.@)
291 BOOL WINAPI CloseClipboard(void)
295 TRACE("(%d)\n", bCBHasChanged);
297 if (CLIPBOARD_CloseClipboard())
301 HWND hWndViewer = GetClipboardViewer();
303 USER_Driver->pEndClipboardUpdate();
306 SendMessageW(hWndViewer, WM_DRAWCLIPBOARD, (WPARAM) GetClipboardOwner(), 0);
308 bCBHasChanged = FALSE;
318 /**************************************************************************
319 * EmptyClipboard (USER32.@)
320 * Empties and acquires ownership of the clipboard
322 BOOL WINAPI EmptyClipboard(void)
324 CLIPBOARDINFO cbinfo;
328 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
329 ~cbinfo.flags & CB_OPEN)
331 WARN("Clipboard not opened by calling task!\n");
332 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
336 /* Destroy private objects */
337 if (cbinfo.hWndOwner)
338 SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
340 /* Tell the driver to acquire the selection. The current owner
341 * will be signaled to delete it's own cache. */
343 /* Assign ownership of the clipboard to the current client. We do
344 * this before acquiring the selection so that when we do acquire the
345 * selection and the selection loser gets notified, it can check if
346 * it has lost the Wine clipboard ownership. If it did then it knows
347 * that a WM_DESTORYCLIPBOARD has already been sent. Otherwise it
348 * lost the selection to a X app and it should send the
349 * WM_DESTROYCLIPBOARD itself. */
350 CLIPBOARD_SetClipboardOwner(cbinfo.hWndOpen);
352 /* Acquire the selection. This will notify the previous owner
353 * to clear it's cache. */
354 USER_Driver->pAcquireClipboard(cbinfo.hWndOpen);
356 /* Empty the local cache */
357 USER_Driver->pEmptyClipboard(FALSE);
359 bCBHasChanged = TRUE;
365 /**************************************************************************
366 * GetClipboardOwner (USER32.@)
367 * FIXME: Can't return the owner if the clipboard is owned by an external X-app
369 HWND WINAPI GetClipboardOwner(void)
373 SERVER_START_REQ( set_clipboard_info )
376 if (!wine_server_call_err( req )) hWndOwner = wine_server_ptr_handle( reply->old_owner );
380 TRACE(" hWndOwner(%p)\n", hWndOwner);
386 /**************************************************************************
387 * GetOpenClipboardWindow (USER32.@)
389 HWND WINAPI GetOpenClipboardWindow(void)
393 SERVER_START_REQ( set_clipboard_info )
396 if (!wine_server_call_err( req )) hWndOpen = wine_server_ptr_handle( reply->old_clipboard );
400 TRACE(" hWndClipWindow(%p)\n", hWndOpen);
406 /**************************************************************************
407 * SetClipboardViewer (USER32.@)
409 HWND WINAPI SetClipboardViewer( HWND hWnd )
411 HWND hwndPrev = CLIPBOARD_SetClipboardViewer(hWnd);
414 SendMessageW(hWnd, WM_DRAWCLIPBOARD, (WPARAM) GetClipboardOwner(), 0);
415 TRACE("(%p): returning %p\n", hWnd, hwndPrev);
421 /**************************************************************************
422 * GetClipboardViewer (USER32.@)
424 HWND WINAPI GetClipboardViewer(void)
428 SERVER_START_REQ( set_clipboard_info )
431 if (!wine_server_call_err( req )) hWndViewer = wine_server_ptr_handle( reply->old_viewer );
435 TRACE(" hWndViewer=%p\n", hWndViewer);
441 /**************************************************************************
442 * ChangeClipboardChain (USER32.@)
444 BOOL WINAPI ChangeClipboardChain(HWND hWnd, HWND hWndNext)
447 HWND hWndViewer = GetClipboardViewer();
451 if (WIN_GetFullHandle(hWnd) == hWndViewer)
452 CLIPBOARD_SetClipboardViewer(WIN_GetFullHandle(hWndNext));
454 bRet = !SendMessageW(hWndViewer, WM_CHANGECBCHAIN, (WPARAM)hWnd, (LPARAM)hWndNext);
457 ERR("hWndViewer is lost\n");
463 /**************************************************************************
464 * SetClipboardData (USER.141)
466 HANDLE16 WINAPI SetClipboardData16(UINT16 wFormat, HANDLE16 hData)
468 CLIPBOARDINFO cbinfo;
469 HANDLE16 hResult = 0;
471 TRACE("(%04X, %04x) !\n", wFormat, hData);
473 /* If it's not owned, data can only be set if the format doesn't exists
474 and its rendering is not delayed */
475 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
476 (!(cbinfo.flags & CB_OWNER) && !hData))
478 WARN("Clipboard not owned by calling task. Operation failed.\n");
482 if (USER_Driver->pSetClipboardData(wFormat, hData, 0, cbinfo.flags & CB_OWNER))
485 bCBHasChanged = TRUE;
492 /**************************************************************************
493 * SetClipboardData (USER32.@)
495 HANDLE WINAPI SetClipboardData(UINT wFormat, HANDLE hData)
497 CLIPBOARDINFO cbinfo;
500 TRACE("(%04X, %p) !\n", wFormat, hData);
502 /* If it's not owned, data can only be set if the format isn't
503 available and its rendering is not delayed */
504 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
505 (!(cbinfo.flags & CB_OWNER) && !hData))
507 WARN("Clipboard not owned by calling task. Operation failed.\n");
511 if (USER_Driver->pSetClipboardData(wFormat, 0, hData, cbinfo.flags & CB_OWNER))
514 bCBHasChanged = TRUE;
521 /**************************************************************************
522 * CountClipboardFormats (USER32.@)
524 INT WINAPI CountClipboardFormats(void)
526 INT count = USER_Driver->pCountClipboardFormats();
527 TRACE("returning %d\n", count);
532 /**************************************************************************
533 * EnumClipboardFormats (USER32.@)
535 UINT WINAPI EnumClipboardFormats(UINT wFormat)
537 CLIPBOARDINFO cbinfo;
539 TRACE("(%04X)\n", wFormat);
541 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
542 (~cbinfo.flags & CB_OPEN))
544 WARN("Clipboard not opened by calling task.\n");
545 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
548 return USER_Driver->pEnumClipboardFormats(wFormat);
552 /**************************************************************************
553 * IsClipboardFormatAvailable (USER32.@)
555 BOOL WINAPI IsClipboardFormatAvailable(UINT wFormat)
557 BOOL bret = USER_Driver->pIsClipboardFormatAvailable(wFormat);
558 TRACE("%04x, returning %d\n", wFormat, bret);
563 /**************************************************************************
564 * GetClipboardData (USER.142)
566 HANDLE16 WINAPI GetClipboardData16(UINT16 wFormat)
569 CLIPBOARDINFO cbinfo;
571 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
572 (~cbinfo.flags & CB_OPEN))
574 WARN("Clipboard not opened by calling task.\n");
575 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
579 if (!USER_Driver->pGetClipboardData(wFormat, &hData, NULL)) hData = 0;
585 /**************************************************************************
586 * GetClipboardData (USER32.@)
588 HANDLE WINAPI GetClipboardData(UINT wFormat)
591 CLIPBOARDINFO cbinfo;
593 TRACE("%04x\n", wFormat);
595 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
596 (~cbinfo.flags & CB_OPEN))
598 WARN("Clipboard not opened by calling task.\n");
599 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
603 if (!USER_Driver->pGetClipboardData(wFormat, NULL, &hData)) hData = 0;
605 TRACE("returning %p\n", hData);
610 /**************************************************************************
611 * GetPriorityClipboardFormat (USER32.@)
613 INT WINAPI GetPriorityClipboardFormat(UINT *list, INT nCount)
619 if(CountClipboardFormats() == 0)
622 for (i = 0; i < nCount; i++)
623 if (IsClipboardFormatAvailable(list[i]))
630 /**************************************************************************
631 * GetClipboardSequenceNumber (USER32.@)
632 * Supported on Win2k/Win98
633 * MSDN: Windows clipboard code keeps a serial number for the clipboard
634 * for each window station. The number is incremented whenever the
635 * contents change or are emptied.
636 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
638 DWORD WINAPI GetClipboardSequenceNumber(VOID)
642 SERVER_START_REQ( set_clipboard_info )
645 if (!wine_server_call_err( req )) seqno = reply->seqno;
649 TRACE("returning %x\n", seqno);