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/winuser16.h"
50 #include "wine/winbase16.h"
51 #include "user_private.h"
54 #include "wine/debug.h"
55 #include "wine/unicode.h"
56 #include "wine/server.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(clipboard);
60 #define CF_REGFORMATBASE 0xC000
69 } CLIPBOARDINFO, *LPCLIPBOARDINFO;
72 * Indicates if data has changed since open.
74 static BOOL bCBHasChanged = FALSE;
77 /**************************************************************************
78 * CLIPBOARD_SetClipboardOwner
80 * Set the global wineserver clipboard owner. The current process will
81 * be the owner and <hWnd> will get the render notifications.
83 static BOOL CLIPBOARD_SetClipboardOwner(HWND hWnd)
87 TRACE(" hWnd(%p)\n", hWnd);
89 SERVER_START_REQ( set_clipboard_info )
91 req->flags = SET_CB_OWNER;
92 req->owner = WIN_GetFullHandle( hWnd );
94 if (wine_server_call_err( req ))
96 ERR("Failed to set clipboard owner to %p\n", hWnd);
109 /**************************************************************************
110 * CLIPBOARD_GetClipboardInfo
112 static BOOL CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo)
116 SERVER_START_REQ( set_clipboard_info )
120 if (wine_server_call_err( req ))
122 ERR("Failed to get clipboard info\n");
126 cbInfo->hWndOpen = reply->old_clipboard;
127 cbInfo->hWndOwner = reply->old_owner;
128 cbInfo->hWndViewer = reply->old_viewer;
129 cbInfo->seqno = reply->seqno;
130 cbInfo->flags = reply->flags;
141 /**************************************************************************
142 * CLIPBOARD_ReleaseOwner
144 BOOL CLIPBOARD_ReleaseOwner(void)
148 SERVER_START_REQ( set_clipboard_info )
150 req->flags = SET_CB_RELOWNER | SET_CB_SEQNO;
152 if (wine_server_call_err( req ))
154 ERR("Failed to set clipboard.\n");
167 /**************************************************************************
168 * CLIPBOARD_OpenClipboard
170 static BOOL CLIPBOARD_OpenClipboard(HWND hWnd)
174 SERVER_START_REQ( set_clipboard_info )
176 req->flags = SET_CB_OPEN;
177 req->clipboard = WIN_GetFullHandle( hWnd );
179 if (!wine_server_call( req ))
188 /**************************************************************************
189 * CLIPBOARD_CloseClipboard
191 static BOOL CLIPBOARD_CloseClipboard(void)
195 TRACE(" Changed=%d\n", bCBHasChanged);
197 SERVER_START_REQ( set_clipboard_info )
199 req->flags = SET_CB_CLOSE;
203 req->flags |= SET_CB_SEQNO;
204 TRACE("Clipboard data changed\n");
207 if (wine_server_call_err( req ))
209 ERR("Failed to set clipboard.\n");
222 /**************************************************************************
223 * WIN32 Clipboard implementation
224 **************************************************************************/
226 /**************************************************************************
227 * RegisterClipboardFormatW (USER32.@)
229 UINT WINAPI RegisterClipboardFormatW(LPCWSTR FormatName)
231 return USER_Driver->pRegisterClipboardFormat(FormatName);
235 /**************************************************************************
236 * RegisterClipboardFormatA (USER32.@)
238 UINT WINAPI RegisterClipboardFormatA(LPCSTR formatName)
244 len = MultiByteToWideChar(CP_ACP, 0, formatName, -1, NULL, 0);
245 wFormat = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
246 MultiByteToWideChar(CP_ACP, 0, formatName, -1, wFormat, len);
248 ret = RegisterClipboardFormatW(wFormat);
249 HeapFree(GetProcessHeap(), 0, wFormat);
254 /**************************************************************************
255 * GetClipboardFormatNameW (USER32.@)
257 INT WINAPI GetClipboardFormatNameW(UINT wFormat, LPWSTR retStr, INT maxlen)
259 return USER_Driver->pGetClipboardFormatName(wFormat, retStr, maxlen);
263 /**************************************************************************
264 * GetClipboardFormatNameA (USER32.@)
266 INT WINAPI GetClipboardFormatNameA(UINT wFormat, LPSTR retStr, INT maxlen)
269 LPWSTR p = HeapAlloc( GetProcessHeap(), 0, maxlen*sizeof(WCHAR) );
270 if(p == NULL) return 0; /* FIXME: is this the correct failure value? */
272 ret = GetClipboardFormatNameW( wFormat, p, maxlen );
274 if (ret && maxlen > 0 && !WideCharToMultiByte( CP_ACP, 0, p, -1, retStr, maxlen, 0, 0))
275 retStr[maxlen-1] = 0;
276 HeapFree( GetProcessHeap(), 0, p );
281 /**************************************************************************
282 * OpenClipboard (USER32.@)
284 * Note: Netscape uses NULL hWnd to open the clipboard.
286 BOOL WINAPI OpenClipboard( HWND hWnd )
290 TRACE("(%p)...\n", hWnd);
292 bRet = CLIPBOARD_OpenClipboard(hWnd);
294 TRACE(" returning %i\n", bRet);
300 /**************************************************************************
301 * CloseClipboard (USER32.@)
303 BOOL WINAPI CloseClipboard(void)
307 TRACE("(%d)\n", bCBHasChanged);
309 if (CLIPBOARD_CloseClipboard())
313 HWND hWndViewer = GetClipboardViewer();
315 USER_Driver->pEndClipboardUpdate();
318 SendMessageW(hWndViewer, WM_DRAWCLIPBOARD, 0, 0);
320 bCBHasChanged = FALSE;
330 /**************************************************************************
331 * EmptyClipboard (USER32.@)
332 * Empties and acquires ownership of the clipboard
334 BOOL WINAPI EmptyClipboard(void)
336 CLIPBOARDINFO cbinfo;
340 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
341 ~cbinfo.flags & CB_OPEN)
343 WARN("Clipboard not opened by calling task!\n");
344 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
348 /* Destroy private objects */
349 if (cbinfo.hWndOwner)
350 SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
352 /* Tell the driver to acquire the selection. The current owner
353 * will be signaled to delete it's own cache. */
355 /* Assign ownership of the clipboard to the current client. We do
356 * this before acquiring the selection so that when we do acquire the
357 * selection and the selection loser gets notified, it can check if
358 * it has lost the Wine clipboard ownership. If it did then it knows
359 * that a WM_DESTORYCLIPBOARD has already been sent. Otherwise it
360 * lost the selection to a X app and it should send the
361 * WM_DESTROYCLIPBOARD itself. */
362 CLIPBOARD_SetClipboardOwner(cbinfo.hWndOpen);
364 /* Acquire the selection. This will notify the previous owner
365 * to clear it's cache. */
366 USER_Driver->pAcquireClipboard(cbinfo.hWndOpen);
368 /* Empty the local cache */
369 USER_Driver->pEmptyClipboard(FALSE);
371 bCBHasChanged = TRUE;
377 /**************************************************************************
378 * GetClipboardOwner (USER32.@)
379 * FIXME: Can't return the owner if the clipboard is owned by an external X-app
381 HWND WINAPI GetClipboardOwner(void)
385 SERVER_START_REQ( set_clipboard_info )
388 if (!wine_server_call_err( req )) hWndOwner = reply->old_owner;
392 TRACE(" hWndOwner(%p)\n", hWndOwner);
398 /**************************************************************************
399 * GetOpenClipboardWindow (USER32.@)
401 HWND WINAPI GetOpenClipboardWindow(void)
405 SERVER_START_REQ( set_clipboard_info )
408 if (!wine_server_call_err( req )) hWndOpen = reply->old_clipboard;
412 TRACE(" hWndClipWindow(%p)\n", hWndOpen);
418 /**************************************************************************
419 * SetClipboardViewer (USER32.@)
421 HWND WINAPI SetClipboardViewer( HWND hWnd )
425 SERVER_START_REQ( set_clipboard_info )
427 req->flags = SET_CB_VIEWER;
428 req->viewer = WIN_GetFullHandle(hWnd);
430 if (wine_server_call_err( req ))
432 ERR("Failed to set clipboard.\n");
436 hwndPrev = reply->old_viewer;
441 TRACE("(%p): returning %p\n", hWnd, hwndPrev);
447 /**************************************************************************
448 * GetClipboardViewer (USER32.@)
450 HWND WINAPI GetClipboardViewer(void)
454 SERVER_START_REQ( set_clipboard_info )
457 if (!wine_server_call_err( req )) hWndViewer = reply->old_viewer;
461 TRACE(" hWndViewer=%p\n", hWndViewer);
467 /**************************************************************************
468 * ChangeClipboardChain (USER32.@)
470 BOOL WINAPI ChangeClipboardChain(HWND hWnd, HWND hWndNext)
473 HWND hWndViewer = GetClipboardViewer();
477 if (WIN_GetFullHandle(hWnd) == hWndViewer)
478 SetClipboardViewer(WIN_GetFullHandle(hWndNext));
480 bRet = !SendMessageW(hWndViewer, WM_CHANGECBCHAIN, (WPARAM)hWnd, (LPARAM)hWndNext);
483 ERR("hWndViewer is lost\n");
489 /**************************************************************************
490 * SetClipboardData (USER.141)
492 HANDLE16 WINAPI SetClipboardData16(UINT16 wFormat, HANDLE16 hData)
494 CLIPBOARDINFO cbinfo;
495 HANDLE16 hResult = 0;
497 TRACE("(%04X, %04x) !\n", wFormat, hData);
499 /* If it's not owned, data can only be set if the format doesn't exists
500 and its rendering is not delayed */
501 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
502 (!(cbinfo.flags & CB_OWNER) && !hData))
504 WARN("Clipboard not owned by calling task. Operation failed.\n");
508 if (USER_Driver->pSetClipboardData(wFormat, hData, 0, cbinfo.flags & CB_OWNER))
511 bCBHasChanged = TRUE;
518 /**************************************************************************
519 * SetClipboardData (USER32.@)
521 HANDLE WINAPI SetClipboardData(UINT wFormat, HANDLE hData)
523 CLIPBOARDINFO cbinfo;
526 TRACE("(%04X, %p) !\n", wFormat, hData);
528 /* If it's not owned, data can only be set if the format isn't
529 available and its rendering is not delayed */
530 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
531 (!(cbinfo.flags & CB_OWNER) && !hData))
533 WARN("Clipboard not owned by calling task. Operation failed.\n");
537 if (USER_Driver->pSetClipboardData(wFormat, 0, hData, cbinfo.flags & CB_OWNER))
540 bCBHasChanged = TRUE;
547 /**************************************************************************
548 * CountClipboardFormats (USER32.@)
550 INT WINAPI CountClipboardFormats(void)
552 INT count = USER_Driver->pCountClipboardFormats();
553 TRACE("returning %d\n", count);
558 /**************************************************************************
559 * EnumClipboardFormats (USER32.@)
561 UINT WINAPI EnumClipboardFormats(UINT wFormat)
563 CLIPBOARDINFO cbinfo;
565 TRACE("(%04X)\n", wFormat);
567 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
568 (~cbinfo.flags & CB_OPEN))
570 WARN("Clipboard not opened by calling task.\n");
571 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
574 return USER_Driver->pEnumClipboardFormats(wFormat);
578 /**************************************************************************
579 * IsClipboardFormatAvailable (USER32.@)
581 BOOL WINAPI IsClipboardFormatAvailable(UINT wFormat)
583 BOOL bret = USER_Driver->pIsClipboardFormatAvailable(wFormat);
584 TRACE("%04x, returning %d\n", wFormat, bret);
589 /**************************************************************************
590 * GetClipboardData (USER.142)
592 HANDLE16 WINAPI GetClipboardData16(UINT16 wFormat)
595 CLIPBOARDINFO cbinfo;
597 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
598 (~cbinfo.flags & CB_OPEN))
600 WARN("Clipboard not opened by calling task.\n");
601 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
605 if (!USER_Driver->pGetClipboardData(wFormat, &hData, NULL)) hData = 0;
611 /**************************************************************************
612 * GetClipboardData (USER32.@)
614 HANDLE WINAPI GetClipboardData(UINT wFormat)
617 CLIPBOARDINFO cbinfo;
619 TRACE("%04x\n", wFormat);
621 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
622 (~cbinfo.flags & CB_OPEN))
624 WARN("Clipboard not opened by calling task.\n");
625 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
629 if (!USER_Driver->pGetClipboardData(wFormat, NULL, &hData)) hData = 0;
631 TRACE("returning %p\n", hData);
636 /**************************************************************************
637 * GetPriorityClipboardFormat (USER32.@)
639 INT WINAPI GetPriorityClipboardFormat(UINT *list, INT nCount)
645 if(CountClipboardFormats() == 0)
648 for (i = 0; i < nCount; i++)
649 if (IsClipboardFormatAvailable(list[i]))
656 /**************************************************************************
657 * GetClipboardSequenceNumber (USER32.@)
658 * Supported on Win2k/Win98
659 * MSDN: Windows clipboard code keeps a serial number for the clipboard
660 * for each window station. The number is incremented whenever the
661 * contents change or are emptied.
662 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
664 DWORD WINAPI GetClipboardSequenceNumber(VOID)
668 SERVER_START_REQ( set_clipboard_info )
671 if (!wine_server_call_err( req )) seqno = reply->seqno;
675 TRACE("returning %lx\n", seqno);