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"
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 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)
233 TRACE("%s\n", debugstr_w(FormatName));
235 if (USER_Driver.pRegisterClipboardFormat)
236 wFormatID = USER_Driver.pRegisterClipboardFormat(FormatName);
242 /**************************************************************************
243 * RegisterClipboardFormatA (USER32.@)
245 UINT WINAPI RegisterClipboardFormatA(LPCSTR formatName)
251 len = MultiByteToWideChar(CP_ACP, 0, formatName, -1, NULL, 0);
252 wFormat = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
253 MultiByteToWideChar(CP_ACP, 0, formatName, -1, wFormat, len);
255 ret = RegisterClipboardFormatW(wFormat);
256 HeapFree(GetProcessHeap(), 0, wFormat);
261 /**************************************************************************
262 * GetClipboardFormatNameW (USER32.@)
264 INT WINAPI GetClipboardFormatNameW(UINT wFormat, LPWSTR retStr, INT maxlen)
268 TRACE("%04x,%p,%d\n", wFormat, retStr, maxlen);
270 if (USER_Driver.pGetClipboardFormatName)
271 len = USER_Driver.pGetClipboardFormatName(wFormat, retStr, maxlen);
277 /**************************************************************************
278 * GetClipboardFormatNameA (USER32.@)
280 INT WINAPI GetClipboardFormatNameA(UINT wFormat, LPSTR retStr, INT maxlen)
283 LPWSTR p = HeapAlloc( GetProcessHeap(), 0, maxlen*sizeof(WCHAR) );
284 if(p == NULL) return 0; /* FIXME: is this the correct failure value? */
286 ret = GetClipboardFormatNameW( wFormat, p, maxlen );
288 if (maxlen > 0 && !WideCharToMultiByte( CP_ACP, 0, p, -1, retStr, maxlen, 0, 0))
289 retStr[maxlen-1] = 0;
290 HeapFree( GetProcessHeap(), 0, p );
295 /**************************************************************************
296 * OpenClipboard (USER32.@)
298 * Note: Netscape uses NULL hWnd to open the clipboard.
300 BOOL WINAPI OpenClipboard( HWND hWnd )
304 TRACE("(%p)...\n", hWnd);
306 bRet = CLIPBOARD_OpenClipboard(hWnd);
308 TRACE(" returning %i\n", bRet);
314 /**************************************************************************
315 * CloseClipboard (USER32.@)
317 BOOL WINAPI CloseClipboard(void)
321 TRACE("(%d)\n", bCBHasChanged);
323 if (CLIPBOARD_CloseClipboard())
327 HWND hWndViewer = GetClipboardViewer();
329 if (USER_Driver.pEndClipboardUpdate)
330 USER_Driver.pEndClipboardUpdate();
333 SendMessageW(hWndViewer, WM_DRAWCLIPBOARD, 0, 0);
335 bCBHasChanged = FALSE;
345 /**************************************************************************
346 * EmptyClipboard (USER32.@)
347 * Empties and acquires ownership of the clipboard
349 BOOL WINAPI EmptyClipboard(void)
351 CLIPBOARDINFO cbinfo;
355 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
356 ~cbinfo.flags & CB_OPEN)
358 WARN("Clipboard not opened by calling task!\n");
359 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
363 /* Destroy private objects */
364 if (cbinfo.hWndOwner)
365 SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
367 /* Tell the driver to acquire the selection. The current owner
368 * will be signaled to delete it's own cache. */
370 /* Assign ownership of the clipboard to the current client. We do
371 * this before acquiring the selection so that when we do acquire the
372 * selection and the selection loser gets notified, it can check if
373 * it has lost the Wine clipboard ownership. If it did then it knows
374 * that a WM_DESTORYCLIPBOARD has already been sent. Otherwise it
375 * lost the selection to a X app and it should send the
376 * WM_DESTROYCLIPBOARD itself. */
377 CLIPBOARD_SetClipboardOwner(cbinfo.hWndOpen);
379 /* Acquire the selection. This will notify the previous owner
380 * to clear it's cache. */
381 if (USER_Driver.pAcquireClipboard)
382 USER_Driver.pAcquireClipboard(cbinfo.hWndOpen);
384 /* Empty the local cache */
385 if (USER_Driver.pEmptyClipboard)
386 USER_Driver.pEmptyClipboard(FALSE);
388 bCBHasChanged = TRUE;
394 /**************************************************************************
395 * GetClipboardOwner (USER32.@)
396 * FIXME: Can't return the owner if the clipboard is owned by an external X-app
398 HWND WINAPI GetClipboardOwner(void)
402 SERVER_START_REQ( set_clipboard_info )
405 if (!wine_server_call_err( req )) hWndOwner = reply->old_owner;
409 TRACE(" hWndOwner(%p)\n", hWndOwner);
415 /**************************************************************************
416 * GetOpenClipboardWindow (USER32.@)
418 HWND WINAPI GetOpenClipboardWindow(void)
422 SERVER_START_REQ( set_clipboard_info )
425 if (!wine_server_call_err( req )) hWndOpen = reply->old_clipboard;
429 TRACE(" hWndClipWindow(%p)\n", hWndOpen);
435 /**************************************************************************
436 * SetClipboardViewer (USER32.@)
438 HWND WINAPI SetClipboardViewer( HWND hWnd )
442 SERVER_START_REQ( set_clipboard_info )
444 req->flags = SET_CB_VIEWER;
445 req->viewer = WIN_GetFullHandle(hWnd);
447 if (wine_server_call_err( req ))
449 ERR("Failed to set clipboard.\n");
453 hwndPrev = reply->old_viewer;
458 TRACE("(%p): returning %p\n", hWnd, hwndPrev);
464 /**************************************************************************
465 * GetClipboardViewer (USER32.@)
467 HWND WINAPI GetClipboardViewer(void)
471 SERVER_START_REQ( set_clipboard_info )
474 if (!wine_server_call_err( req )) hWndViewer = reply->old_viewer;
478 TRACE(" hWndViewer=%p\n", hWndViewer);
484 /**************************************************************************
485 * ChangeClipboardChain (USER32.@)
487 BOOL WINAPI ChangeClipboardChain(HWND hWnd, HWND hWndNext)
490 HWND hWndViewer = GetClipboardViewer();
494 if (WIN_GetFullHandle(hWnd) == hWndViewer)
495 SetClipboardViewer(WIN_GetFullHandle(hWndNext));
497 bRet = !SendMessageW(hWndViewer, WM_CHANGECBCHAIN, (WPARAM)hWnd, (LPARAM)hWndNext);
500 ERR("hWndViewer is lost\n");
506 /**************************************************************************
507 * SetClipboardData (USER.141)
509 HANDLE16 WINAPI SetClipboardData16(UINT16 wFormat, HANDLE16 hData)
511 CLIPBOARDINFO cbinfo;
512 HANDLE16 hResult = 0;
514 TRACE("(%04X, %04x) !\n", wFormat, hData);
516 /* If it's not owned, data can only be set if the format doesn't exists
517 and its rendering is not delayed */
518 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
519 (!(cbinfo.flags & CB_OWNER) && !hData))
521 WARN("Clipboard not owned by calling task. Operation failed.\n");
525 if (USER_Driver.pSetClipboardData &&
526 USER_Driver.pSetClipboardData(wFormat, hData, 0, cbinfo.flags & CB_OWNER))
529 bCBHasChanged = TRUE;
536 /**************************************************************************
537 * SetClipboardData (USER32.@)
539 HANDLE WINAPI SetClipboardData(UINT wFormat, HANDLE hData)
541 CLIPBOARDINFO cbinfo;
544 TRACE("(%04X, %p) !\n", wFormat, hData);
546 /* If it's not owned, data can only be set if the format isn't
547 available and its rendering is not delayed */
548 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
549 (!(cbinfo.flags & CB_OWNER) && !hData))
551 WARN("Clipboard not owned by calling task. Operation failed.\n");
555 if (USER_Driver.pSetClipboardData &&
556 USER_Driver.pSetClipboardData(wFormat, 0, hData, cbinfo.flags & CB_OWNER))
559 bCBHasChanged = TRUE;
566 /**************************************************************************
567 * CountClipboardFormats (USER32.@)
569 INT WINAPI CountClipboardFormats(void)
573 if (USER_Driver.pCountClipboardFormats)
574 count = USER_Driver.pCountClipboardFormats();
576 TRACE("returning %d\n", count);
581 /**************************************************************************
582 * EnumClipboardFormats (USER32.@)
584 UINT WINAPI EnumClipboardFormats(UINT wFormat)
587 CLIPBOARDINFO cbinfo;
589 TRACE("(%04X)\n", wFormat);
591 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
592 (~cbinfo.flags & CB_OPEN))
594 WARN("Clipboard not opened by calling task.\n");
595 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
599 if (USER_Driver.pEnumClipboardFormats)
600 wFmt = USER_Driver.pEnumClipboardFormats(wFormat);
606 /**************************************************************************
607 * IsClipboardFormatAvailable (USER32.@)
609 BOOL WINAPI IsClipboardFormatAvailable(UINT wFormat)
613 if (USER_Driver.pIsClipboardFormatAvailable)
614 bret = USER_Driver.pIsClipboardFormatAvailable(wFormat);
616 TRACE("%04x, returning %d\n", wFormat, bret);
621 /**************************************************************************
622 * GetClipboardData (USER.142)
624 HANDLE16 WINAPI GetClipboardData16(UINT16 wFormat)
627 CLIPBOARDINFO cbinfo;
629 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
630 (~cbinfo.flags & CB_OPEN))
632 WARN("Clipboard not opened by calling task.\n");
633 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
637 if (USER_Driver.pGetClipboardData)
638 USER_Driver.pGetClipboardData(wFormat, &hData, NULL);
644 /**************************************************************************
645 * GetClipboardData (USER32.@)
647 HANDLE WINAPI GetClipboardData(UINT wFormat)
650 CLIPBOARDINFO cbinfo;
652 TRACE("%04x\n", wFormat);
654 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
655 (~cbinfo.flags & CB_OPEN))
657 WARN("Clipboard not opened by calling task.\n");
658 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
662 if (USER_Driver.pGetClipboardData)
663 USER_Driver.pGetClipboardData(wFormat, NULL, &hData);
665 TRACE("returning %p\n", hData);
670 /**************************************************************************
671 * GetPriorityClipboardFormat (USER32.@)
673 INT WINAPI GetPriorityClipboardFormat(UINT *list, INT nCount)
679 if(CountClipboardFormats() == 0)
682 for (i = 0; i < nCount; i++)
683 if (IsClipboardFormatAvailable(list[i]))
690 /**************************************************************************
691 * GetClipboardSequenceNumber (USER32.@)
692 * Supported on Win2k/Win98
693 * MSDN: Windows clipboard code keeps a serial number for the clipboard
694 * for each window station. The number is incremented whenever the
695 * contents change or are emptied.
696 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
698 DWORD WINAPI GetClipboardSequenceNumber(VOID)
702 SERVER_START_REQ( set_clipboard_info )
705 if (!wine_server_call_err( req )) seqno = reply->seqno;
709 TRACE("returning %lx\n", seqno);