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 = WIN_GetFullHandle( hWnd );
93 if (wine_server_call_err( req ))
95 ERR("Failed to set clipboard owner to %p\n", hWnd);
108 /**************************************************************************
109 * CLIPBOARD_GetClipboardInfo
111 static BOOL CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo)
115 SERVER_START_REQ( set_clipboard_info )
119 if (wine_server_call_err( req ))
121 ERR("Failed to get clipboard info\n");
125 cbInfo->hWndOpen = reply->old_clipboard;
126 cbInfo->hWndOwner = reply->old_owner;
127 cbInfo->hWndViewer = reply->old_viewer;
128 cbInfo->seqno = reply->seqno;
129 cbInfo->flags = reply->flags;
140 /**************************************************************************
141 * CLIPBOARD_ReleaseOwner
143 BOOL CLIPBOARD_ReleaseOwner(void)
147 SERVER_START_REQ( set_clipboard_info )
149 req->flags = SET_CB_RELOWNER | SET_CB_SEQNO;
151 if (wine_server_call_err( req ))
153 ERR("Failed to set clipboard.\n");
166 /**************************************************************************
167 * CLIPBOARD_OpenClipboard
169 static BOOL CLIPBOARD_OpenClipboard(HWND hWnd)
173 SERVER_START_REQ( set_clipboard_info )
175 req->flags = SET_CB_OPEN;
176 req->clipboard = WIN_GetFullHandle( hWnd );
178 if (!wine_server_call( req ))
187 /**************************************************************************
188 * CLIPBOARD_CloseClipboard
190 static BOOL CLIPBOARD_CloseClipboard(void)
194 TRACE(" Changed=%d\n", bCBHasChanged);
196 SERVER_START_REQ( set_clipboard_info )
198 req->flags = SET_CB_CLOSE;
202 req->flags |= SET_CB_SEQNO;
203 TRACE("Clipboard data changed\n");
206 if (wine_server_call_err( req ))
208 ERR("Failed to set clipboard.\n");
221 /**************************************************************************
222 * WIN32 Clipboard implementation
223 **************************************************************************/
225 /**************************************************************************
226 * RegisterClipboardFormatW (USER32.@)
228 UINT WINAPI RegisterClipboardFormatW(LPCWSTR FormatName)
230 return USER_Driver->pRegisterClipboardFormat(FormatName);
234 /**************************************************************************
235 * RegisterClipboardFormatA (USER32.@)
237 UINT WINAPI RegisterClipboardFormatA(LPCSTR formatName)
243 len = MultiByteToWideChar(CP_ACP, 0, formatName, -1, NULL, 0);
244 wFormat = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
245 MultiByteToWideChar(CP_ACP, 0, formatName, -1, wFormat, len);
247 ret = RegisterClipboardFormatW(wFormat);
248 HeapFree(GetProcessHeap(), 0, wFormat);
253 /**************************************************************************
254 * GetClipboardFormatNameW (USER32.@)
256 INT WINAPI GetClipboardFormatNameW(UINT wFormat, LPWSTR retStr, INT maxlen)
258 return USER_Driver->pGetClipboardFormatName(wFormat, retStr, maxlen);
262 /**************************************************************************
263 * GetClipboardFormatNameA (USER32.@)
265 INT WINAPI GetClipboardFormatNameA(UINT wFormat, LPSTR retStr, INT maxlen)
268 LPWSTR p = HeapAlloc( GetProcessHeap(), 0, maxlen*sizeof(WCHAR) );
269 if(p == NULL) return 0; /* FIXME: is this the correct failure value? */
271 ret = GetClipboardFormatNameW( wFormat, p, maxlen );
273 if (ret && maxlen > 0 && !WideCharToMultiByte( CP_ACP, 0, p, -1, retStr, maxlen, 0, 0))
274 retStr[maxlen-1] = 0;
275 HeapFree( GetProcessHeap(), 0, p );
280 /**************************************************************************
281 * OpenClipboard (USER32.@)
283 * Note: Netscape uses NULL hWnd to open the clipboard.
285 BOOL WINAPI OpenClipboard( HWND hWnd )
289 TRACE("(%p)...\n", hWnd);
291 bRet = CLIPBOARD_OpenClipboard(hWnd);
293 TRACE(" returning %i\n", bRet);
299 /**************************************************************************
300 * CloseClipboard (USER32.@)
302 BOOL WINAPI CloseClipboard(void)
306 TRACE("(%d)\n", bCBHasChanged);
308 if (CLIPBOARD_CloseClipboard())
312 HWND hWndViewer = GetClipboardViewer();
314 USER_Driver->pEndClipboardUpdate();
317 SendMessageW(hWndViewer, WM_DRAWCLIPBOARD, 0, 0);
319 bCBHasChanged = FALSE;
329 /**************************************************************************
330 * EmptyClipboard (USER32.@)
331 * Empties and acquires ownership of the clipboard
333 BOOL WINAPI EmptyClipboard(void)
335 CLIPBOARDINFO cbinfo;
339 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
340 ~cbinfo.flags & CB_OPEN)
342 WARN("Clipboard not opened by calling task!\n");
343 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
347 /* Destroy private objects */
348 if (cbinfo.hWndOwner)
349 SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
351 /* Tell the driver to acquire the selection. The current owner
352 * will be signaled to delete it's own cache. */
354 /* Assign ownership of the clipboard to the current client. We do
355 * this before acquiring the selection so that when we do acquire the
356 * selection and the selection loser gets notified, it can check if
357 * it has lost the Wine clipboard ownership. If it did then it knows
358 * that a WM_DESTORYCLIPBOARD has already been sent. Otherwise it
359 * lost the selection to a X app and it should send the
360 * WM_DESTROYCLIPBOARD itself. */
361 CLIPBOARD_SetClipboardOwner(cbinfo.hWndOpen);
363 /* Acquire the selection. This will notify the previous owner
364 * to clear it's cache. */
365 USER_Driver->pAcquireClipboard(cbinfo.hWndOpen);
367 /* Empty the local cache */
368 USER_Driver->pEmptyClipboard(FALSE);
370 bCBHasChanged = TRUE;
376 /**************************************************************************
377 * GetClipboardOwner (USER32.@)
378 * FIXME: Can't return the owner if the clipboard is owned by an external X-app
380 HWND WINAPI GetClipboardOwner(void)
384 SERVER_START_REQ( set_clipboard_info )
387 if (!wine_server_call_err( req )) hWndOwner = reply->old_owner;
391 TRACE(" hWndOwner(%p)\n", hWndOwner);
397 /**************************************************************************
398 * GetOpenClipboardWindow (USER32.@)
400 HWND WINAPI GetOpenClipboardWindow(void)
404 SERVER_START_REQ( set_clipboard_info )
407 if (!wine_server_call_err( req )) hWndOpen = reply->old_clipboard;
411 TRACE(" hWndClipWindow(%p)\n", hWndOpen);
417 /**************************************************************************
418 * SetClipboardViewer (USER32.@)
420 HWND WINAPI SetClipboardViewer( HWND hWnd )
424 SERVER_START_REQ( set_clipboard_info )
426 req->flags = SET_CB_VIEWER;
427 req->viewer = WIN_GetFullHandle(hWnd);
429 if (wine_server_call_err( req ))
431 ERR("Failed to set clipboard.\n");
435 hwndPrev = reply->old_viewer;
440 TRACE("(%p): returning %p\n", hWnd, hwndPrev);
446 /**************************************************************************
447 * GetClipboardViewer (USER32.@)
449 HWND WINAPI GetClipboardViewer(void)
453 SERVER_START_REQ( set_clipboard_info )
456 if (!wine_server_call_err( req )) hWndViewer = reply->old_viewer;
460 TRACE(" hWndViewer=%p\n", hWndViewer);
466 /**************************************************************************
467 * ChangeClipboardChain (USER32.@)
469 BOOL WINAPI ChangeClipboardChain(HWND hWnd, HWND hWndNext)
472 HWND hWndViewer = GetClipboardViewer();
476 if (WIN_GetFullHandle(hWnd) == hWndViewer)
477 SetClipboardViewer(WIN_GetFullHandle(hWndNext));
479 bRet = !SendMessageW(hWndViewer, WM_CHANGECBCHAIN, (WPARAM)hWnd, (LPARAM)hWndNext);
482 ERR("hWndViewer is lost\n");
488 /**************************************************************************
489 * SetClipboardData (USER.141)
491 HANDLE16 WINAPI SetClipboardData16(UINT16 wFormat, HANDLE16 hData)
493 CLIPBOARDINFO cbinfo;
494 HANDLE16 hResult = 0;
496 TRACE("(%04X, %04x) !\n", wFormat, hData);
498 /* If it's not owned, data can only be set if the format doesn't exists
499 and its rendering is not delayed */
500 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
501 (!(cbinfo.flags & CB_OWNER) && !hData))
503 WARN("Clipboard not owned by calling task. Operation failed.\n");
507 if (USER_Driver->pSetClipboardData(wFormat, hData, 0, cbinfo.flags & CB_OWNER))
510 bCBHasChanged = TRUE;
517 /**************************************************************************
518 * SetClipboardData (USER32.@)
520 HANDLE WINAPI SetClipboardData(UINT wFormat, HANDLE hData)
522 CLIPBOARDINFO cbinfo;
525 TRACE("(%04X, %p) !\n", wFormat, hData);
527 /* If it's not owned, data can only be set if the format isn't
528 available and its rendering is not delayed */
529 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
530 (!(cbinfo.flags & CB_OWNER) && !hData))
532 WARN("Clipboard not owned by calling task. Operation failed.\n");
536 if (USER_Driver->pSetClipboardData(wFormat, 0, hData, cbinfo.flags & CB_OWNER))
539 bCBHasChanged = TRUE;
546 /**************************************************************************
547 * CountClipboardFormats (USER32.@)
549 INT WINAPI CountClipboardFormats(void)
551 INT count = USER_Driver->pCountClipboardFormats();
552 TRACE("returning %d\n", count);
557 /**************************************************************************
558 * EnumClipboardFormats (USER32.@)
560 UINT WINAPI EnumClipboardFormats(UINT wFormat)
562 CLIPBOARDINFO cbinfo;
564 TRACE("(%04X)\n", wFormat);
566 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
567 (~cbinfo.flags & CB_OPEN))
569 WARN("Clipboard not opened by calling task.\n");
570 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
573 return USER_Driver->pEnumClipboardFormats(wFormat);
577 /**************************************************************************
578 * IsClipboardFormatAvailable (USER32.@)
580 BOOL WINAPI IsClipboardFormatAvailable(UINT wFormat)
582 BOOL bret = USER_Driver->pIsClipboardFormatAvailable(wFormat);
583 TRACE("%04x, returning %d\n", wFormat, bret);
588 /**************************************************************************
589 * GetClipboardData (USER.142)
591 HANDLE16 WINAPI GetClipboardData16(UINT16 wFormat)
594 CLIPBOARDINFO cbinfo;
596 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
597 (~cbinfo.flags & CB_OPEN))
599 WARN("Clipboard not opened by calling task.\n");
600 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
604 if (!USER_Driver->pGetClipboardData(wFormat, &hData, NULL)) hData = 0;
610 /**************************************************************************
611 * GetClipboardData (USER32.@)
613 HANDLE WINAPI GetClipboardData(UINT wFormat)
616 CLIPBOARDINFO cbinfo;
618 TRACE("%04x\n", wFormat);
620 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
621 (~cbinfo.flags & CB_OPEN))
623 WARN("Clipboard not opened by calling task.\n");
624 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
628 if (!USER_Driver->pGetClipboardData(wFormat, NULL, &hData)) hData = 0;
630 TRACE("returning %p\n", hData);
635 /**************************************************************************
636 * GetPriorityClipboardFormat (USER32.@)
638 INT WINAPI GetPriorityClipboardFormat(UINT *list, INT nCount)
644 if(CountClipboardFormats() == 0)
647 for (i = 0; i < nCount; i++)
648 if (IsClipboardFormatAvailable(list[i]))
655 /**************************************************************************
656 * GetClipboardSequenceNumber (USER32.@)
657 * Supported on Win2k/Win98
658 * MSDN: Windows clipboard code keeps a serial number for the clipboard
659 * for each window station. The number is incremented whenever the
660 * contents change or are emptied.
661 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
663 DWORD WINAPI GetClipboardSequenceNumber(VOID)
667 SERVER_START_REQ( set_clipboard_info )
670 if (!wine_server_call_err( req )) seqno = reply->seqno;
674 TRACE("returning %x\n", seqno);