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"
54 #include "clipboard.h"
56 #include "wine/debug.h"
57 #include "wine/unicode.h"
58 #include "wine/server.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(clipboard);
62 #define CF_REGFORMATBASE 0xC000
66 * Indicates if data has changed since open.
68 static BOOL bCBHasChanged = FALSE;
71 /**************************************************************************
72 * CLIPBOARD_SetClipboardOwner
74 BOOL CLIPBOARD_SetClipboardOwner(HWND hWnd)
78 TRACE(" hWnd(%p)\n", hWnd);
80 SERVER_START_REQ( set_clipboard_info )
82 req->flags = SET_CB_OWNER;
83 req->owner = WIN_GetFullHandle( hWnd );
85 if (wine_server_call_err( req ))
87 ERR("Failed to set clipboard.\n");
100 /**************************************************************************
101 * CLIPBOARD_GetClipboardInfo
103 BOOL CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo)
107 SERVER_START_REQ( set_clipboard_info )
111 if (wine_server_call_err( req ))
113 ERR("Failed to get clipboard owner.\n");
117 cbInfo->hWndOpen = reply->old_clipboard;
118 cbInfo->hWndOwner = reply->old_owner;
119 cbInfo->hWndViewer = reply->old_viewer;
120 cbInfo->seqno = reply->seqno;
121 cbInfo->flags = reply->flags;
132 /**************************************************************************
133 * CLIPBOARD_ReleaseOwner
135 BOOL CLIPBOARD_ReleaseOwner(void)
139 SERVER_START_REQ( set_clipboard_info )
141 req->flags = SET_CB_RELOWNER | SET_CB_SEQNO;
143 if (wine_server_call_err( req ))
145 ERR("Failed to set clipboard.\n");
158 /**************************************************************************
159 * CLIPBOARD_OpenClipboard
161 static BOOL CLIPBOARD_OpenClipboard(HWND hWnd)
165 SERVER_START_REQ( set_clipboard_info )
167 req->flags = SET_CB_OPEN;
168 req->clipboard = WIN_GetFullHandle( hWnd );
170 if (wine_server_call_err( req ))
172 ERR("Failed to set clipboard.\n");
185 /**************************************************************************
186 * CLIPBOARD_CloseClipboard
188 static BOOL CLIPBOARD_CloseClipboard(void)
192 TRACE(" Changed=%d\n", bCBHasChanged);
194 SERVER_START_REQ( set_clipboard_info )
196 req->flags = SET_CB_CLOSE;
200 req->flags |= SET_CB_SEQNO;
201 TRACE("Clipboard data changed\n");
204 if (wine_server_call_err( req ))
206 ERR("Failed to set clipboard.\n");
219 /**************************************************************************
220 * WIN32 Clipboard implementation
221 **************************************************************************/
223 /**************************************************************************
224 * RegisterClipboardFormatA (USER32.@)
226 UINT WINAPI RegisterClipboardFormatA(LPCSTR FormatName)
230 TRACE("%s\n", debugstr_a(FormatName));
232 if (USER_Driver.pRegisterClipboardFormat)
233 wFormatID = USER_Driver.pRegisterClipboardFormat(FormatName);
239 /**************************************************************************
240 * RegisterClipboardFormat (USER.145)
242 UINT16 WINAPI RegisterClipboardFormat16(LPCSTR FormatName)
246 TRACE("%s\n", debugstr_a(FormatName));
248 if (USER_Driver.pRegisterClipboardFormat)
249 wFormatID = USER_Driver.pRegisterClipboardFormat(FormatName);
255 /**************************************************************************
256 * RegisterClipboardFormatW (USER32.@)
258 UINT WINAPI RegisterClipboardFormatW(LPCWSTR formatName)
260 LPSTR aFormat = HEAP_strdupWtoA( GetProcessHeap(), 0, formatName );
261 UINT ret = RegisterClipboardFormatA( aFormat );
262 HeapFree( GetProcessHeap(), 0, aFormat );
267 /**************************************************************************
268 * GetClipboardFormatName (USER.146)
270 INT16 WINAPI GetClipboardFormatName16(UINT16 wFormat, LPSTR retStr, INT16 maxlen)
272 TRACE("%04x,%p,%d\n", wFormat, retStr, maxlen);
274 return GetClipboardFormatNameA(wFormat, retStr, maxlen);
278 /**************************************************************************
279 * GetClipboardFormatNameA (USER32.@)
281 INT WINAPI GetClipboardFormatNameA(UINT wFormat, LPSTR retStr, INT maxlen)
285 TRACE("%04x,%p,%d\n", wFormat, retStr, maxlen);
287 if (USER_Driver.pGetClipboardFormatName)
288 len = USER_Driver.pGetClipboardFormatName(wFormat, retStr, maxlen);
294 /**************************************************************************
295 * GetClipboardFormatNameW (USER32.@)
297 INT WINAPI GetClipboardFormatNameW(UINT wFormat, LPWSTR retStr, INT maxlen)
300 LPSTR p = HeapAlloc( GetProcessHeap(), 0, maxlen );
301 if(p == NULL) return 0; /* FIXME: is this the correct failure value? */
303 ret = GetClipboardFormatNameA( wFormat, p, maxlen );
305 if (maxlen > 0 && !MultiByteToWideChar( CP_ACP, 0, p, -1, retStr, maxlen ))
306 retStr[maxlen-1] = 0;
307 HeapFree( GetProcessHeap(), 0, p );
312 /**************************************************************************
313 * OpenClipboard (USER32.@)
315 * Note: Netscape uses NULL hWnd to open the clipboard.
317 BOOL WINAPI OpenClipboard( HWND hWnd )
321 TRACE("(%p)...\n", hWnd);
323 bRet = CLIPBOARD_OpenClipboard(hWnd);
325 TRACE(" returning %i\n", bRet);
331 /**************************************************************************
332 * CloseClipboard (USER.138)
334 BOOL16 WINAPI CloseClipboard16(void)
336 return CloseClipboard();
340 /**************************************************************************
341 * CloseClipboard (USER32.@)
343 BOOL WINAPI CloseClipboard(void)
347 TRACE("(%d)\n", bCBHasChanged);
349 if (CLIPBOARD_CloseClipboard())
353 HWND hWndViewer = GetClipboardViewer();
355 if (USER_Driver.pEndClipboardUpdate)
356 USER_Driver.pEndClipboardUpdate();
359 SendMessageW(hWndViewer, WM_DRAWCLIPBOARD, 0, 0);
361 bCBHasChanged = FALSE;
371 /**************************************************************************
372 * EmptyClipboard (USER.139)
374 BOOL16 WINAPI EmptyClipboard16(void)
376 return EmptyClipboard();
380 /**************************************************************************
381 * EmptyClipboard (USER32.@)
382 * Empties and acquires ownership of the clipboard
384 BOOL WINAPI EmptyClipboard(void)
386 CLIPBOARDINFO cbinfo;
390 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
391 ~cbinfo.flags & CB_OPEN)
393 WARN("Clipboard not opened by calling task!\n");
394 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
398 /* Destroy private objects */
399 if (cbinfo.hWndOwner)
400 SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
402 /* Tell the driver to acquire the selection. The current owner
403 * will be signaled to delete it's own cache. */
404 if (~cbinfo.flags & CB_OWNER)
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);
421 /* Empty the local cache */
422 if (USER_Driver.pEmptyClipboard)
423 USER_Driver.pEmptyClipboard();
425 bCBHasChanged = TRUE;
431 /**************************************************************************
432 * GetClipboardOwner (USER32.@)
433 * FIXME: Can't return the owner if the clipboard is owned by an external X-app
435 HWND WINAPI GetClipboardOwner(void)
438 CLIPBOARDINFO cbinfo;
440 if (CLIPBOARD_GetClipboardInfo(&cbinfo))
441 hWndOwner = cbinfo.hWndOwner;
443 TRACE(" hWndOwner(%p)\n", hWndOwner);
449 /**************************************************************************
450 * GetOpenClipboardWindow (USER32.@)
452 HWND WINAPI GetOpenClipboardWindow(void)
455 CLIPBOARDINFO cbinfo;
457 if (CLIPBOARD_GetClipboardInfo(&cbinfo))
458 hWndOpen = cbinfo.hWndOpen;
460 TRACE(" hWndClipWindow(%p)\n", hWndOpen);
466 /**************************************************************************
467 * SetClipboardViewer (USER32.@)
469 HWND WINAPI SetClipboardViewer( HWND hWnd )
473 SERVER_START_REQ( set_clipboard_info )
475 req->flags = SET_CB_VIEWER;
476 req->viewer = WIN_GetFullHandle(hWnd);
478 if (wine_server_call_err( req ))
480 ERR("Failed to set clipboard.\n");
484 hwndPrev = reply->old_viewer;
489 TRACE("(%p): returning %p\n", hWnd, hwndPrev);
495 /**************************************************************************
496 * GetClipboardViewer (USER32.@)
498 HWND WINAPI GetClipboardViewer(void)
501 CLIPBOARDINFO cbinfo;
503 if (CLIPBOARD_GetClipboardInfo(&cbinfo))
504 hWndViewer = cbinfo.hWndViewer;
506 TRACE(" hWndViewer=%p\n", hWndViewer);
512 /**************************************************************************
513 * ChangeClipboardChain (USER32.@)
515 BOOL WINAPI ChangeClipboardChain(HWND hWnd, HWND hWndNext)
518 HWND hWndViewer = GetClipboardViewer();
522 if (WIN_GetFullHandle(hWnd) == hWndViewer)
523 SetClipboardViewer(WIN_GetFullHandle(hWndNext));
525 bRet = !SendMessageW(hWndViewer, WM_CHANGECBCHAIN, (WPARAM)hWnd, (LPARAM)hWndNext);
528 ERR("hWndViewer is lost\n");
534 /**************************************************************************
535 * SetClipboardData (USER.141)
537 HANDLE16 WINAPI SetClipboardData16(UINT16 wFormat, HANDLE16 hData)
539 CLIPBOARDINFO cbinfo;
540 HANDLE16 hResult = 0;
542 TRACE("(%04X, %04x) !\n", wFormat, hData);
544 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
545 (~cbinfo.flags & CB_OPEN) ||
546 (~cbinfo.flags & CB_OWNER))
548 WARN("Clipboard not opened by calling task!\n");
550 else if (USER_Driver.pSetClipboardData &&
551 USER_Driver.pSetClipboardData(wFormat, hData, 0))
554 bCBHasChanged = TRUE;
561 /**************************************************************************
562 * SetClipboardData (USER32.@)
564 HANDLE WINAPI SetClipboardData(UINT wFormat, HANDLE hData)
566 CLIPBOARDINFO cbinfo;
569 TRACE("(%04X, %p) !\n", wFormat, hData);
571 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
572 (~cbinfo.flags & CB_OWNER))
574 WARN("Clipboard not owned by calling task!\n");
576 else if (USER_Driver.pSetClipboardData &&
577 USER_Driver.pSetClipboardData(wFormat, 0, hData))
580 bCBHasChanged = TRUE;
587 /**************************************************************************
588 * CountClipboardFormats (USER.143)
590 INT16 WINAPI CountClipboardFormats16(void)
592 return CountClipboardFormats();
596 /**************************************************************************
597 * CountClipboardFormats (USER32.@)
599 INT WINAPI CountClipboardFormats(void)
603 if (USER_Driver.pCountClipboardFormats)
604 count = USER_Driver.pCountClipboardFormats();
606 TRACE("returning %d\n", count);
611 /**************************************************************************
612 * EnumClipboardFormats (USER.144)
614 UINT16 WINAPI EnumClipboardFormats16(UINT16 wFormat)
616 return EnumClipboardFormats(wFormat);
620 /**************************************************************************
621 * EnumClipboardFormats (USER32.@)
623 UINT WINAPI EnumClipboardFormats(UINT wFormat)
626 CLIPBOARDINFO cbinfo;
628 TRACE("(%04X)\n", wFormat);
630 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
631 (~cbinfo.flags & CB_OPEN))
633 WARN("Clipboard not opened by calling task.\n");
634 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
638 if (USER_Driver.pEnumClipboardFormats)
639 wFmt = USER_Driver.pEnumClipboardFormats(wFormat);
645 /**************************************************************************
646 * IsClipboardFormatAvailable (USER.193)
648 BOOL16 WINAPI IsClipboardFormatAvailable16(UINT16 wFormat)
650 return IsClipboardFormatAvailable(wFormat);
654 /**************************************************************************
655 * IsClipboardFormatAvailable (USER32.@)
657 BOOL WINAPI IsClipboardFormatAvailable(UINT wFormat)
661 if (USER_Driver.pIsClipboardFormatAvailable)
662 bret = USER_Driver.pIsClipboardFormatAvailable(wFormat);
664 TRACE("%04x, returning %d\n", wFormat, bret);
669 /**************************************************************************
670 * GetClipboardData (USER.142)
672 HANDLE16 WINAPI GetClipboardData16(UINT16 wFormat)
675 CLIPBOARDINFO cbinfo;
677 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
678 (~cbinfo.flags & CB_OPEN))
680 WARN("Clipboard not opened by calling task.\n");
681 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
685 if (USER_Driver.pGetClipboardData)
686 USER_Driver.pGetClipboardData(wFormat, &hData, NULL);
692 /**************************************************************************
693 * GetClipboardData (USER32.@)
695 HANDLE WINAPI GetClipboardData(UINT wFormat)
698 CLIPBOARDINFO cbinfo;
700 TRACE("%04x\n", wFormat);
702 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
703 (~cbinfo.flags & CB_OPEN))
705 WARN("Clipboard not opened by calling task.\n");
706 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
710 if (USER_Driver.pGetClipboardData)
711 USER_Driver.pGetClipboardData(wFormat, NULL, &hData);
713 TRACE("returning %p\n", hData);
718 /**************************************************************************
719 * GetPriorityClipboardFormat (USER32.@)
721 INT WINAPI GetPriorityClipboardFormat(UINT *list, INT nCount)
727 if(CountClipboardFormats() == 0)
730 for (i = 0; i < nCount; i++)
731 if (IsClipboardFormatAvailable(list[i]))
738 /**************************************************************************
739 * GetClipboardSequenceNumber (USER32.@)
740 * Supported on Win2k/Win98
741 * MSDN: Windows clipboard code keeps a serial number for the clipboard
742 * for each window station. The number is incremented whenever the
743 * contents change or are emptied.
744 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
746 DWORD WINAPI GetClipboardSequenceNumber(VOID)
748 CLIPBOARDINFO cbinfo;
750 if (!CLIPBOARD_GetClipboardInfo(&cbinfo))
752 ERR("Failed to get clipboard information.\n");
756 TRACE("returning %x\n", cbinfo.seqno);