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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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"
55 #include "wine/debug.h"
56 #include "wine/unicode.h"
57 #include "wine/server.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(clipboard);
61 #define CF_REGFORMATBASE 0xC000
70 } CLIPBOARDINFO, *LPCLIPBOARDINFO;
73 * Indicates if data has changed since open.
75 static BOOL bCBHasChanged = FALSE;
78 /**************************************************************************
79 * CLIPBOARD_SetClipboardOwner
81 BOOL CLIPBOARD_SetClipboardOwner(HWND hWnd)
85 TRACE(" hWnd(%p)\n", hWnd);
87 SERVER_START_REQ( set_clipboard_info )
89 req->flags = SET_CB_OWNER;
90 req->owner = WIN_GetFullHandle( hWnd );
92 if (wine_server_call_err( req ))
94 ERR("Failed to set clipboard owner to %p\n", hWnd);
107 /**************************************************************************
108 * CLIPBOARD_GetClipboardInfo
110 static BOOL CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo)
114 SERVER_START_REQ( set_clipboard_info )
118 if (wine_server_call_err( req ))
120 ERR("Failed to get clipboard info\n");
124 cbInfo->hWndOpen = reply->old_clipboard;
125 cbInfo->hWndOwner = reply->old_owner;
126 cbInfo->hWndViewer = reply->old_viewer;
127 cbInfo->seqno = reply->seqno;
128 cbInfo->flags = reply->flags;
139 /**************************************************************************
140 * CLIPBOARD_ReleaseOwner
142 BOOL CLIPBOARD_ReleaseOwner(void)
146 SERVER_START_REQ( set_clipboard_info )
148 req->flags = SET_CB_RELOWNER | SET_CB_SEQNO;
150 if (wine_server_call_err( req ))
152 ERR("Failed to set clipboard.\n");
165 /**************************************************************************
166 * CLIPBOARD_OpenClipboard
168 static BOOL CLIPBOARD_OpenClipboard(HWND hWnd)
172 SERVER_START_REQ( set_clipboard_info )
174 req->flags = SET_CB_OPEN;
175 req->clipboard = WIN_GetFullHandle( hWnd );
177 if (!wine_server_call( req ))
186 /**************************************************************************
187 * CLIPBOARD_CloseClipboard
189 static BOOL CLIPBOARD_CloseClipboard(void)
193 TRACE(" Changed=%d\n", bCBHasChanged);
195 SERVER_START_REQ( set_clipboard_info )
197 req->flags = SET_CB_CLOSE;
201 req->flags |= SET_CB_SEQNO;
202 TRACE("Clipboard data changed\n");
205 if (wine_server_call_err( req ))
207 ERR("Failed to set clipboard.\n");
220 /**************************************************************************
221 * WIN32 Clipboard implementation
222 **************************************************************************/
224 /**************************************************************************
225 * RegisterClipboardFormatA (USER32.@)
227 UINT WINAPI RegisterClipboardFormatA(LPCSTR FormatName)
231 TRACE("%s\n", debugstr_a(FormatName));
233 if (USER_Driver.pRegisterClipboardFormat)
234 wFormatID = USER_Driver.pRegisterClipboardFormat(FormatName);
240 /**************************************************************************
241 * RegisterClipboardFormat (USER.145)
243 UINT16 WINAPI RegisterClipboardFormat16(LPCSTR FormatName)
247 TRACE("%s\n", debugstr_a(FormatName));
249 if (USER_Driver.pRegisterClipboardFormat)
250 wFormatID = USER_Driver.pRegisterClipboardFormat(FormatName);
256 /**************************************************************************
257 * RegisterClipboardFormatW (USER32.@)
259 UINT WINAPI RegisterClipboardFormatW(LPCWSTR formatName)
261 LPSTR aFormat = HEAP_strdupWtoA( GetProcessHeap(), 0, formatName );
262 UINT ret = RegisterClipboardFormatA( aFormat );
263 HeapFree( GetProcessHeap(), 0, aFormat );
268 /**************************************************************************
269 * GetClipboardFormatName (USER.146)
271 INT16 WINAPI GetClipboardFormatName16(UINT16 wFormat, LPSTR retStr, INT16 maxlen)
273 TRACE("%04x,%p,%d\n", wFormat, retStr, maxlen);
275 return GetClipboardFormatNameA(wFormat, retStr, maxlen);
279 /**************************************************************************
280 * GetClipboardFormatNameA (USER32.@)
282 INT WINAPI GetClipboardFormatNameA(UINT wFormat, LPSTR retStr, INT maxlen)
286 TRACE("%04x,%p,%d\n", wFormat, retStr, maxlen);
288 if (USER_Driver.pGetClipboardFormatName)
289 len = USER_Driver.pGetClipboardFormatName(wFormat, retStr, maxlen);
295 /**************************************************************************
296 * GetClipboardFormatNameW (USER32.@)
298 INT WINAPI GetClipboardFormatNameW(UINT wFormat, LPWSTR retStr, INT maxlen)
301 LPSTR p = HeapAlloc( GetProcessHeap(), 0, maxlen );
302 if(p == NULL) return 0; /* FIXME: is this the correct failure value? */
304 ret = GetClipboardFormatNameA( wFormat, p, maxlen );
306 if (maxlen > 0 && !MultiByteToWideChar( CP_ACP, 0, p, -1, retStr, maxlen ))
307 retStr[maxlen-1] = 0;
308 HeapFree( GetProcessHeap(), 0, p );
313 /**************************************************************************
314 * OpenClipboard (USER32.@)
316 * Note: Netscape uses NULL hWnd to open the clipboard.
318 BOOL WINAPI OpenClipboard( HWND hWnd )
322 TRACE("(%p)...\n", hWnd);
324 bRet = CLIPBOARD_OpenClipboard(hWnd);
326 TRACE(" returning %i\n", bRet);
332 /**************************************************************************
333 * CloseClipboard (USER.138)
335 BOOL16 WINAPI CloseClipboard16(void)
337 return CloseClipboard();
341 /**************************************************************************
342 * CloseClipboard (USER32.@)
344 BOOL WINAPI CloseClipboard(void)
348 TRACE("(%d)\n", bCBHasChanged);
350 if (CLIPBOARD_CloseClipboard())
354 HWND hWndViewer = GetClipboardViewer();
356 if (USER_Driver.pEndClipboardUpdate)
357 USER_Driver.pEndClipboardUpdate();
360 SendMessageW(hWndViewer, WM_DRAWCLIPBOARD, 0, 0);
362 bCBHasChanged = FALSE;
372 /**************************************************************************
373 * EmptyClipboard (USER.139)
375 BOOL16 WINAPI EmptyClipboard16(void)
377 return EmptyClipboard();
381 /**************************************************************************
382 * EmptyClipboard (USER32.@)
383 * Empties and acquires ownership of the clipboard
385 BOOL WINAPI EmptyClipboard(void)
387 CLIPBOARDINFO cbinfo;
391 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
392 ~cbinfo.flags & CB_OPEN)
394 WARN("Clipboard not opened by calling task!\n");
395 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
399 /* Destroy private objects */
400 if (cbinfo.hWndOwner)
401 SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
403 /* Tell the driver to acquire the selection. The current owner
404 * will be signaled to delete it's own cache. */
406 /* Assign ownership of the clipboard to the current client. We do
407 * this before acquiring the selection so that when we do acquire the
408 * selection and the selection loser gets notified, it can check if
409 * it has lost the Wine clipboard ownership. If it did then it knows
410 * that a WM_DESTORYCLIPBOARD has already been sent. Otherwise it
411 * lost the selection to a X app and it should send the
412 * WM_DESTROYCLIPBOARD itself. */
413 CLIPBOARD_SetClipboardOwner(cbinfo.hWndOpen);
415 /* Acquire the selection. This will notify the previous owner
416 * to clear it's cache. */
417 if (USER_Driver.pAcquireClipboard)
418 USER_Driver.pAcquireClipboard(cbinfo.hWndOpen);
420 /* Empty the local cache */
421 if (USER_Driver.pEmptyClipboard)
422 USER_Driver.pEmptyClipboard(FALSE);
424 bCBHasChanged = TRUE;
430 /**************************************************************************
431 * GetClipboardOwner (USER32.@)
432 * FIXME: Can't return the owner if the clipboard is owned by an external X-app
434 HWND WINAPI GetClipboardOwner(void)
438 SERVER_START_REQ( set_clipboard_info )
441 if (!wine_server_call_err( req )) hWndOwner = reply->old_owner;
445 TRACE(" hWndOwner(%p)\n", hWndOwner);
451 /**************************************************************************
452 * GetOpenClipboardWindow (USER32.@)
454 HWND WINAPI GetOpenClipboardWindow(void)
458 SERVER_START_REQ( set_clipboard_info )
461 if (!wine_server_call_err( req )) hWndOpen = reply->old_clipboard;
465 TRACE(" hWndClipWindow(%p)\n", hWndOpen);
471 /**************************************************************************
472 * SetClipboardViewer (USER32.@)
474 HWND WINAPI SetClipboardViewer( HWND hWnd )
478 SERVER_START_REQ( set_clipboard_info )
480 req->flags = SET_CB_VIEWER;
481 req->viewer = WIN_GetFullHandle(hWnd);
483 if (wine_server_call_err( req ))
485 ERR("Failed to set clipboard.\n");
489 hwndPrev = reply->old_viewer;
494 TRACE("(%p): returning %p\n", hWnd, hwndPrev);
500 /**************************************************************************
501 * GetClipboardViewer (USER32.@)
503 HWND WINAPI GetClipboardViewer(void)
507 SERVER_START_REQ( set_clipboard_info )
510 if (!wine_server_call_err( req )) hWndViewer = reply->old_viewer;
514 TRACE(" hWndViewer=%p\n", hWndViewer);
520 /**************************************************************************
521 * ChangeClipboardChain (USER32.@)
523 BOOL WINAPI ChangeClipboardChain(HWND hWnd, HWND hWndNext)
526 HWND hWndViewer = GetClipboardViewer();
530 if (WIN_GetFullHandle(hWnd) == hWndViewer)
531 SetClipboardViewer(WIN_GetFullHandle(hWndNext));
533 bRet = !SendMessageW(hWndViewer, WM_CHANGECBCHAIN, (WPARAM)hWnd, (LPARAM)hWndNext);
536 ERR("hWndViewer is lost\n");
542 /**************************************************************************
543 * SetClipboardData (USER.141)
545 HANDLE16 WINAPI SetClipboardData16(UINT16 wFormat, HANDLE16 hData)
547 CLIPBOARDINFO cbinfo;
548 HANDLE16 hResult = 0;
550 TRACE("(%04X, %04x) !\n", wFormat, hData);
552 /* If it's not owned, data can only be set if the format doesn't exists
553 and its rendering is not delayed */
554 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
555 (!(cbinfo.flags & CB_OWNER) && !hData))
557 WARN("Clipboard not owned by calling task. Operation failed.\n");
561 if (USER_Driver.pSetClipboardData &&
562 USER_Driver.pSetClipboardData(wFormat, hData, 0, cbinfo.flags & CB_OWNER))
565 bCBHasChanged = TRUE;
572 /**************************************************************************
573 * SetClipboardData (USER32.@)
575 HANDLE WINAPI SetClipboardData(UINT wFormat, HANDLE hData)
577 CLIPBOARDINFO cbinfo;
580 TRACE("(%04X, %p) !\n", wFormat, hData);
582 /* If it's not owned, data can only be set if the format isn't
583 available and its rendering is not delayed */
584 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
585 (!(cbinfo.flags & CB_OWNER) && !hData))
587 WARN("Clipboard not owned by calling task. Operation failed.\n");
591 if (USER_Driver.pSetClipboardData &&
592 USER_Driver.pSetClipboardData(wFormat, 0, hData, cbinfo.flags & CB_OWNER))
595 bCBHasChanged = TRUE;
602 /**************************************************************************
603 * CountClipboardFormats (USER.143)
605 INT16 WINAPI CountClipboardFormats16(void)
607 return CountClipboardFormats();
611 /**************************************************************************
612 * CountClipboardFormats (USER32.@)
614 INT WINAPI CountClipboardFormats(void)
618 if (USER_Driver.pCountClipboardFormats)
619 count = USER_Driver.pCountClipboardFormats();
621 TRACE("returning %d\n", count);
626 /**************************************************************************
627 * EnumClipboardFormats (USER.144)
629 UINT16 WINAPI EnumClipboardFormats16(UINT16 wFormat)
631 return EnumClipboardFormats(wFormat);
635 /**************************************************************************
636 * EnumClipboardFormats (USER32.@)
638 UINT WINAPI EnumClipboardFormats(UINT wFormat)
641 CLIPBOARDINFO cbinfo;
643 TRACE("(%04X)\n", wFormat);
645 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
646 (~cbinfo.flags & CB_OPEN))
648 WARN("Clipboard not opened by calling task.\n");
649 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
653 if (USER_Driver.pEnumClipboardFormats)
654 wFmt = USER_Driver.pEnumClipboardFormats(wFormat);
660 /**************************************************************************
661 * IsClipboardFormatAvailable (USER.193)
663 BOOL16 WINAPI IsClipboardFormatAvailable16(UINT16 wFormat)
665 return IsClipboardFormatAvailable(wFormat);
669 /**************************************************************************
670 * IsClipboardFormatAvailable (USER32.@)
672 BOOL WINAPI IsClipboardFormatAvailable(UINT wFormat)
676 if (USER_Driver.pIsClipboardFormatAvailable)
677 bret = USER_Driver.pIsClipboardFormatAvailable(wFormat);
679 TRACE("%04x, returning %d\n", wFormat, bret);
684 /**************************************************************************
685 * GetClipboardData (USER.142)
687 HANDLE16 WINAPI GetClipboardData16(UINT16 wFormat)
690 CLIPBOARDINFO cbinfo;
692 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
693 (~cbinfo.flags & CB_OPEN))
695 WARN("Clipboard not opened by calling task.\n");
696 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
700 if (USER_Driver.pGetClipboardData)
701 USER_Driver.pGetClipboardData(wFormat, &hData, NULL);
707 /**************************************************************************
708 * GetClipboardData (USER32.@)
710 HANDLE WINAPI GetClipboardData(UINT wFormat)
713 CLIPBOARDINFO cbinfo;
715 TRACE("%04x\n", wFormat);
717 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
718 (~cbinfo.flags & CB_OPEN))
720 WARN("Clipboard not opened by calling task.\n");
721 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
725 if (USER_Driver.pGetClipboardData)
726 USER_Driver.pGetClipboardData(wFormat, NULL, &hData);
728 TRACE("returning %p\n", hData);
733 /**************************************************************************
734 * GetPriorityClipboardFormat (USER32.@)
736 INT WINAPI GetPriorityClipboardFormat(UINT *list, INT nCount)
742 if(CountClipboardFormats() == 0)
745 for (i = 0; i < nCount; i++)
746 if (IsClipboardFormatAvailable(list[i]))
753 /**************************************************************************
754 * GetClipboardSequenceNumber (USER32.@)
755 * Supported on Win2k/Win98
756 * MSDN: Windows clipboard code keeps a serial number for the clipboard
757 * for each window station. The number is incremented whenever the
758 * contents change or are emptied.
759 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
761 DWORD WINAPI GetClipboardSequenceNumber(VOID)
765 SERVER_START_REQ( set_clipboard_info )
768 if (!wine_server_call_err( req )) seqno = reply->seqno;
772 TRACE("returning %lx\n", seqno);