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.\n");
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 owner.\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_err( req ))
179 ERR("Failed to set clipboard.\n");
192 /**************************************************************************
193 * CLIPBOARD_CloseClipboard
195 static BOOL CLIPBOARD_CloseClipboard(void)
199 TRACE(" Changed=%d\n", bCBHasChanged);
201 SERVER_START_REQ( set_clipboard_info )
203 req->flags = SET_CB_CLOSE;
207 req->flags |= SET_CB_SEQNO;
208 TRACE("Clipboard data changed\n");
211 if (wine_server_call_err( req ))
213 ERR("Failed to set clipboard.\n");
226 /**************************************************************************
227 * WIN32 Clipboard implementation
228 **************************************************************************/
230 /**************************************************************************
231 * RegisterClipboardFormatA (USER32.@)
233 UINT WINAPI RegisterClipboardFormatA(LPCSTR FormatName)
237 TRACE("%s\n", debugstr_a(FormatName));
239 if (USER_Driver.pRegisterClipboardFormat)
240 wFormatID = USER_Driver.pRegisterClipboardFormat(FormatName);
246 /**************************************************************************
247 * RegisterClipboardFormat (USER.145)
249 UINT16 WINAPI RegisterClipboardFormat16(LPCSTR FormatName)
253 TRACE("%s\n", debugstr_a(FormatName));
255 if (USER_Driver.pRegisterClipboardFormat)
256 wFormatID = USER_Driver.pRegisterClipboardFormat(FormatName);
262 /**************************************************************************
263 * RegisterClipboardFormatW (USER32.@)
265 UINT WINAPI RegisterClipboardFormatW(LPCWSTR formatName)
267 LPSTR aFormat = HEAP_strdupWtoA( GetProcessHeap(), 0, formatName );
268 UINT ret = RegisterClipboardFormatA( aFormat );
269 HeapFree( GetProcessHeap(), 0, aFormat );
274 /**************************************************************************
275 * GetClipboardFormatName (USER.146)
277 INT16 WINAPI GetClipboardFormatName16(UINT16 wFormat, LPSTR retStr, INT16 maxlen)
279 TRACE("%04x,%p,%d\n", wFormat, retStr, maxlen);
281 return GetClipboardFormatNameA(wFormat, retStr, maxlen);
285 /**************************************************************************
286 * GetClipboardFormatNameA (USER32.@)
288 INT WINAPI GetClipboardFormatNameA(UINT wFormat, LPSTR retStr, INT maxlen)
292 TRACE("%04x,%p,%d\n", wFormat, retStr, maxlen);
294 if (USER_Driver.pGetClipboardFormatName)
295 len = USER_Driver.pGetClipboardFormatName(wFormat, retStr, maxlen);
301 /**************************************************************************
302 * GetClipboardFormatNameW (USER32.@)
304 INT WINAPI GetClipboardFormatNameW(UINT wFormat, LPWSTR retStr, INT maxlen)
307 LPSTR p = HeapAlloc( GetProcessHeap(), 0, maxlen );
308 if(p == NULL) return 0; /* FIXME: is this the correct failure value? */
310 ret = GetClipboardFormatNameA( wFormat, p, maxlen );
312 if (maxlen > 0 && !MultiByteToWideChar( CP_ACP, 0, p, -1, retStr, maxlen ))
313 retStr[maxlen-1] = 0;
314 HeapFree( GetProcessHeap(), 0, p );
319 /**************************************************************************
320 * OpenClipboard (USER32.@)
322 * Note: Netscape uses NULL hWnd to open the clipboard.
324 BOOL WINAPI OpenClipboard( HWND hWnd )
328 TRACE("(%p)...\n", hWnd);
330 bRet = CLIPBOARD_OpenClipboard(hWnd);
332 TRACE(" returning %i\n", bRet);
338 /**************************************************************************
339 * CloseClipboard (USER.138)
341 BOOL16 WINAPI CloseClipboard16(void)
343 return CloseClipboard();
347 /**************************************************************************
348 * CloseClipboard (USER32.@)
350 BOOL WINAPI CloseClipboard(void)
354 TRACE("(%d)\n", bCBHasChanged);
356 if (CLIPBOARD_CloseClipboard())
360 HWND hWndViewer = GetClipboardViewer();
362 if (USER_Driver.pEndClipboardUpdate)
363 USER_Driver.pEndClipboardUpdate();
366 SendMessageW(hWndViewer, WM_DRAWCLIPBOARD, 0, 0);
368 bCBHasChanged = FALSE;
378 /**************************************************************************
379 * EmptyClipboard (USER.139)
381 BOOL16 WINAPI EmptyClipboard16(void)
383 return EmptyClipboard();
387 /**************************************************************************
388 * EmptyClipboard (USER32.@)
389 * Empties and acquires ownership of the clipboard
391 BOOL WINAPI EmptyClipboard(void)
393 CLIPBOARDINFO cbinfo;
397 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
398 ~cbinfo.flags & CB_OPEN)
400 WARN("Clipboard not opened by calling task!\n");
401 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
405 /* Destroy private objects */
406 if (cbinfo.hWndOwner)
407 SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
409 /* Tell the driver to acquire the selection. The current owner
410 * will be signaled to delete it's own cache. */
411 if (~cbinfo.flags & CB_OWNER)
413 /* Assign ownership of the clipboard to the current client. We do
414 * this before acquiring the selection so that when we do acquire the
415 * selection and the selection loser gets notified, it can check if
416 * it has lost the Wine clipboard ownership. If it did then it knows
417 * that a WM_DESTORYCLIPBOARD has already been sent. Otherwise it
418 * lost the selection to a X app and it should send the
419 * WM_DESTROYCLIPBOARD itself. */
420 CLIPBOARD_SetClipboardOwner(cbinfo.hWndOpen);
422 /* Acquire the selection. This will notify the previous owner
423 * to clear it's cache. */
424 if (USER_Driver.pAcquireClipboard)
425 USER_Driver.pAcquireClipboard(cbinfo.hWndOpen);
428 /* Empty the local cache */
429 if (USER_Driver.pEmptyClipboard)
430 USER_Driver.pEmptyClipboard();
432 bCBHasChanged = TRUE;
438 /**************************************************************************
439 * GetClipboardOwner (USER32.@)
440 * FIXME: Can't return the owner if the clipboard is owned by an external X-app
442 HWND WINAPI GetClipboardOwner(void)
446 SERVER_START_REQ( set_clipboard_info )
449 if (!wine_server_call_err( req )) hWndOwner = reply->old_owner;
453 TRACE(" hWndOwner(%p)\n", hWndOwner);
459 /**************************************************************************
460 * GetOpenClipboardWindow (USER32.@)
462 HWND WINAPI GetOpenClipboardWindow(void)
466 SERVER_START_REQ( set_clipboard_info )
469 if (!wine_server_call_err( req )) hWndOpen = reply->old_clipboard;
473 TRACE(" hWndClipWindow(%p)\n", hWndOpen);
479 /**************************************************************************
480 * SetClipboardViewer (USER32.@)
482 HWND WINAPI SetClipboardViewer( HWND hWnd )
486 SERVER_START_REQ( set_clipboard_info )
488 req->flags = SET_CB_VIEWER;
489 req->viewer = WIN_GetFullHandle(hWnd);
491 if (wine_server_call_err( req ))
493 ERR("Failed to set clipboard.\n");
497 hwndPrev = reply->old_viewer;
502 TRACE("(%p): returning %p\n", hWnd, hwndPrev);
508 /**************************************************************************
509 * GetClipboardViewer (USER32.@)
511 HWND WINAPI GetClipboardViewer(void)
515 SERVER_START_REQ( set_clipboard_info )
518 if (!wine_server_call_err( req )) hWndViewer = reply->old_viewer;
522 TRACE(" hWndViewer=%p\n", hWndViewer);
528 /**************************************************************************
529 * ChangeClipboardChain (USER32.@)
531 BOOL WINAPI ChangeClipboardChain(HWND hWnd, HWND hWndNext)
534 HWND hWndViewer = GetClipboardViewer();
538 if (WIN_GetFullHandle(hWnd) == hWndViewer)
539 SetClipboardViewer(WIN_GetFullHandle(hWndNext));
541 bRet = !SendMessageW(hWndViewer, WM_CHANGECBCHAIN, (WPARAM)hWnd, (LPARAM)hWndNext);
544 ERR("hWndViewer is lost\n");
550 /**************************************************************************
551 * SetClipboardData (USER.141)
553 HANDLE16 WINAPI SetClipboardData16(UINT16 wFormat, HANDLE16 hData)
555 CLIPBOARDINFO cbinfo;
556 HANDLE16 hResult = 0;
558 TRACE("(%04X, %04x) !\n", wFormat, hData);
560 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
561 (~cbinfo.flags & CB_OPEN) ||
562 (~cbinfo.flags & CB_OWNER))
564 WARN("Clipboard not opened by calling task!\n");
566 else if (USER_Driver.pSetClipboardData &&
567 USER_Driver.pSetClipboardData(wFormat, hData, 0))
570 bCBHasChanged = TRUE;
577 /**************************************************************************
578 * SetClipboardData (USER32.@)
580 HANDLE WINAPI SetClipboardData(UINT wFormat, HANDLE hData)
582 CLIPBOARDINFO cbinfo;
585 TRACE("(%04X, %p) !\n", wFormat, hData);
587 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
588 (~cbinfo.flags & CB_OWNER))
590 WARN("Clipboard not owned by calling task!\n");
592 else if (USER_Driver.pSetClipboardData &&
593 USER_Driver.pSetClipboardData(wFormat, 0, hData))
596 bCBHasChanged = TRUE;
603 /**************************************************************************
604 * CountClipboardFormats (USER.143)
606 INT16 WINAPI CountClipboardFormats16(void)
608 return CountClipboardFormats();
612 /**************************************************************************
613 * CountClipboardFormats (USER32.@)
615 INT WINAPI CountClipboardFormats(void)
619 if (USER_Driver.pCountClipboardFormats)
620 count = USER_Driver.pCountClipboardFormats();
622 TRACE("returning %d\n", count);
627 /**************************************************************************
628 * EnumClipboardFormats (USER.144)
630 UINT16 WINAPI EnumClipboardFormats16(UINT16 wFormat)
632 return EnumClipboardFormats(wFormat);
636 /**************************************************************************
637 * EnumClipboardFormats (USER32.@)
639 UINT WINAPI EnumClipboardFormats(UINT wFormat)
642 CLIPBOARDINFO cbinfo;
644 TRACE("(%04X)\n", wFormat);
646 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
647 (~cbinfo.flags & CB_OPEN))
649 WARN("Clipboard not opened by calling task.\n");
650 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
654 if (USER_Driver.pEnumClipboardFormats)
655 wFmt = USER_Driver.pEnumClipboardFormats(wFormat);
661 /**************************************************************************
662 * IsClipboardFormatAvailable (USER.193)
664 BOOL16 WINAPI IsClipboardFormatAvailable16(UINT16 wFormat)
666 return IsClipboardFormatAvailable(wFormat);
670 /**************************************************************************
671 * IsClipboardFormatAvailable (USER32.@)
673 BOOL WINAPI IsClipboardFormatAvailable(UINT wFormat)
677 if (USER_Driver.pIsClipboardFormatAvailable)
678 bret = USER_Driver.pIsClipboardFormatAvailable(wFormat);
680 TRACE("%04x, returning %d\n", wFormat, bret);
685 /**************************************************************************
686 * GetClipboardData (USER.142)
688 HANDLE16 WINAPI GetClipboardData16(UINT16 wFormat)
691 CLIPBOARDINFO cbinfo;
693 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
694 (~cbinfo.flags & CB_OPEN))
696 WARN("Clipboard not opened by calling task.\n");
697 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
701 if (USER_Driver.pGetClipboardData)
702 USER_Driver.pGetClipboardData(wFormat, &hData, NULL);
708 /**************************************************************************
709 * GetClipboardData (USER32.@)
711 HANDLE WINAPI GetClipboardData(UINT wFormat)
714 CLIPBOARDINFO cbinfo;
716 TRACE("%04x\n", wFormat);
718 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
719 (~cbinfo.flags & CB_OPEN))
721 WARN("Clipboard not opened by calling task.\n");
722 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
726 if (USER_Driver.pGetClipboardData)
727 USER_Driver.pGetClipboardData(wFormat, NULL, &hData);
729 TRACE("returning %p\n", hData);
734 /**************************************************************************
735 * GetPriorityClipboardFormat (USER32.@)
737 INT WINAPI GetPriorityClipboardFormat(UINT *list, INT nCount)
743 if(CountClipboardFormats() == 0)
746 for (i = 0; i < nCount; i++)
747 if (IsClipboardFormatAvailable(list[i]))
754 /**************************************************************************
755 * GetClipboardSequenceNumber (USER32.@)
756 * Supported on Win2k/Win98
757 * MSDN: Windows clipboard code keeps a serial number for the clipboard
758 * for each window station. The number is incremented whenever the
759 * contents change or are emptied.
760 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
762 DWORD WINAPI GetClipboardSequenceNumber(VOID)
766 SERVER_START_REQ( set_clipboard_info )
769 if (!wine_server_call_err( req )) seqno = reply->seqno;
773 TRACE("returning %lx\n", seqno);