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 = wine_server_user_handle( hWnd );
92 bRet = !wine_server_call_err( req );
100 /**************************************************************************
101 * CLIPBOARD_GetClipboardInfo
103 static BOOL CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo)
107 SERVER_START_REQ( set_clipboard_info )
111 if (((bRet = !wine_server_call_err( req ))))
113 cbInfo->hWndOpen = wine_server_ptr_handle( reply->old_clipboard );
114 cbInfo->hWndOwner = wine_server_ptr_handle( reply->old_owner );
115 cbInfo->hWndViewer = wine_server_ptr_handle( reply->old_viewer );
116 cbInfo->seqno = reply->seqno;
117 cbInfo->flags = reply->flags;
126 /**************************************************************************
127 * CLIPBOARD_ReleaseOwner
129 BOOL CLIPBOARD_ReleaseOwner(void)
133 SERVER_START_REQ( set_clipboard_info )
135 req->flags = SET_CB_RELOWNER | SET_CB_SEQNO;
137 if (wine_server_call_err( req ))
139 ERR("Failed to set clipboard.\n");
152 /**************************************************************************
153 * CLIPBOARD_OpenClipboard
155 static BOOL CLIPBOARD_OpenClipboard(HWND hWnd)
159 SERVER_START_REQ( set_clipboard_info )
161 req->flags = SET_CB_OPEN;
162 req->clipboard = wine_server_user_handle( hWnd );
163 bRet = !wine_server_call( req );
171 /**************************************************************************
172 * CLIPBOARD_CloseClipboard
174 static BOOL CLIPBOARD_CloseClipboard(void)
178 TRACE(" Changed=%d\n", bCBHasChanged);
180 SERVER_START_REQ( set_clipboard_info )
182 req->flags = SET_CB_CLOSE;
183 if (bCBHasChanged) req->flags |= SET_CB_SEQNO;
184 bRet = !wine_server_call_err( req );
191 static HWND CLIPBOARD_SetClipboardViewer( HWND hWnd )
195 SERVER_START_REQ( set_clipboard_info )
197 req->flags = SET_CB_VIEWER;
198 req->viewer = wine_server_user_handle( hWnd );
199 if (!wine_server_call_err( req ))
200 hwndPrev = wine_server_ptr_handle( reply->old_viewer );
207 /**************************************************************************
208 * WIN32 Clipboard implementation
209 **************************************************************************/
211 /**************************************************************************
212 * RegisterClipboardFormatW (USER32.@)
214 UINT WINAPI RegisterClipboardFormatW(LPCWSTR FormatName)
216 return USER_Driver->pRegisterClipboardFormat(FormatName);
220 /**************************************************************************
221 * RegisterClipboardFormatA (USER32.@)
223 UINT WINAPI RegisterClipboardFormatA(LPCSTR formatName)
229 len = MultiByteToWideChar(CP_ACP, 0, formatName, -1, NULL, 0);
230 wFormat = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
231 MultiByteToWideChar(CP_ACP, 0, formatName, -1, wFormat, len);
233 ret = RegisterClipboardFormatW(wFormat);
234 HeapFree(GetProcessHeap(), 0, wFormat);
239 /**************************************************************************
240 * GetClipboardFormatNameW (USER32.@)
242 INT WINAPI GetClipboardFormatNameW(UINT wFormat, LPWSTR retStr, INT maxlen)
244 return USER_Driver->pGetClipboardFormatName(wFormat, retStr, maxlen);
248 /**************************************************************************
249 * GetClipboardFormatNameA (USER32.@)
251 INT WINAPI GetClipboardFormatNameA(UINT wFormat, LPSTR retStr, INT maxlen)
254 LPWSTR p = HeapAlloc( GetProcessHeap(), 0, maxlen*sizeof(WCHAR) );
255 if(p == NULL) return 0; /* FIXME: is this the correct failure value? */
257 ret = GetClipboardFormatNameW( wFormat, p, maxlen );
259 if (ret && maxlen > 0 && !WideCharToMultiByte( CP_ACP, 0, p, -1, retStr, maxlen, 0, 0))
260 retStr[maxlen-1] = 0;
261 HeapFree( GetProcessHeap(), 0, p );
266 /**************************************************************************
267 * OpenClipboard (USER32.@)
269 * Note: Netscape uses NULL hWnd to open the clipboard.
271 BOOL WINAPI OpenClipboard( HWND hWnd )
275 TRACE("(%p)...\n", hWnd);
277 bRet = CLIPBOARD_OpenClipboard(hWnd);
279 TRACE(" returning %i\n", bRet);
285 /**************************************************************************
286 * CloseClipboard (USER32.@)
288 BOOL WINAPI CloseClipboard(void)
292 TRACE("(%d)\n", bCBHasChanged);
294 if (CLIPBOARD_CloseClipboard())
298 HWND hWndViewer = GetClipboardViewer();
300 USER_Driver->pEndClipboardUpdate();
303 SendMessageW(hWndViewer, WM_DRAWCLIPBOARD, (WPARAM) GetClipboardOwner(), 0);
305 bCBHasChanged = FALSE;
315 /**************************************************************************
316 * EmptyClipboard (USER32.@)
317 * Empties and acquires ownership of the clipboard
319 BOOL WINAPI EmptyClipboard(void)
321 CLIPBOARDINFO cbinfo;
325 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
326 ~cbinfo.flags & CB_OPEN)
328 WARN("Clipboard not opened by calling task!\n");
329 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
333 /* Destroy private objects */
334 if (cbinfo.hWndOwner)
335 SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
337 /* Tell the driver to acquire the selection. The current owner
338 * will be signaled to delete it's own cache. */
340 /* Assign ownership of the clipboard to the current client. We do
341 * this before acquiring the selection so that when we do acquire the
342 * selection and the selection loser gets notified, it can check if
343 * it has lost the Wine clipboard ownership. If it did then it knows
344 * that a WM_DESTORYCLIPBOARD has already been sent. Otherwise it
345 * lost the selection to a X app and it should send the
346 * WM_DESTROYCLIPBOARD itself. */
347 CLIPBOARD_SetClipboardOwner(cbinfo.hWndOpen);
349 /* Acquire the selection. This will notify the previous owner
350 * to clear it's cache. */
351 USER_Driver->pAcquireClipboard(cbinfo.hWndOpen);
353 /* Empty the local cache */
354 USER_Driver->pEmptyClipboard(FALSE);
356 bCBHasChanged = TRUE;
362 /**************************************************************************
363 * GetClipboardOwner (USER32.@)
364 * FIXME: Can't return the owner if the clipboard is owned by an external X-app
366 HWND WINAPI GetClipboardOwner(void)
370 SERVER_START_REQ( set_clipboard_info )
373 if (!wine_server_call_err( req )) hWndOwner = wine_server_ptr_handle( reply->old_owner );
377 TRACE(" hWndOwner(%p)\n", hWndOwner);
383 /**************************************************************************
384 * GetOpenClipboardWindow (USER32.@)
386 HWND WINAPI GetOpenClipboardWindow(void)
390 SERVER_START_REQ( set_clipboard_info )
393 if (!wine_server_call_err( req )) hWndOpen = wine_server_ptr_handle( reply->old_clipboard );
397 TRACE(" hWndClipWindow(%p)\n", hWndOpen);
403 /**************************************************************************
404 * SetClipboardViewer (USER32.@)
406 HWND WINAPI SetClipboardViewer( HWND hWnd )
408 HWND hwndPrev = CLIPBOARD_SetClipboardViewer(hWnd);
411 SendMessageW(hWnd, WM_DRAWCLIPBOARD, (WPARAM) GetClipboardOwner(), 0);
412 TRACE("(%p): returning %p\n", hWnd, hwndPrev);
418 /**************************************************************************
419 * GetClipboardViewer (USER32.@)
421 HWND WINAPI GetClipboardViewer(void)
425 SERVER_START_REQ( set_clipboard_info )
428 if (!wine_server_call_err( req )) hWndViewer = wine_server_ptr_handle( reply->old_viewer );
432 TRACE(" hWndViewer=%p\n", hWndViewer);
438 /**************************************************************************
439 * ChangeClipboardChain (USER32.@)
441 BOOL WINAPI ChangeClipboardChain(HWND hWnd, HWND hWndNext)
444 HWND hWndViewer = GetClipboardViewer();
448 if (WIN_GetFullHandle(hWnd) == hWndViewer)
449 CLIPBOARD_SetClipboardViewer(WIN_GetFullHandle(hWndNext));
451 bRet = !SendMessageW(hWndViewer, WM_CHANGECBCHAIN, (WPARAM)hWnd, (LPARAM)hWndNext);
454 ERR("hWndViewer is lost\n");
460 /**************************************************************************
461 * SetClipboardData (USER.141)
463 HANDLE16 WINAPI SetClipboardData16(UINT16 wFormat, HANDLE16 hData)
465 CLIPBOARDINFO cbinfo;
466 HANDLE16 hResult = 0;
468 TRACE("(%04X, %04x) !\n", wFormat, hData);
470 /* If it's not owned, data can only be set if the format doesn't exists
471 and its rendering is not delayed */
472 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
473 (!(cbinfo.flags & CB_OWNER) && !hData))
475 WARN("Clipboard not owned by calling task. Operation failed.\n");
479 if (USER_Driver->pSetClipboardData(wFormat, hData, 0, cbinfo.flags & CB_OWNER))
482 bCBHasChanged = TRUE;
489 /**************************************************************************
490 * SetClipboardData (USER32.@)
492 HANDLE WINAPI SetClipboardData(UINT wFormat, HANDLE hData)
494 CLIPBOARDINFO cbinfo;
497 TRACE("(%04X, %p) !\n", wFormat, hData);
499 /* If it's not owned, data can only be set if the format isn't
500 available and its rendering is not delayed */
501 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
502 (!(cbinfo.flags & CB_OWNER) && !hData))
504 WARN("Clipboard not owned by calling task. Operation failed.\n");
508 if (USER_Driver->pSetClipboardData(wFormat, 0, hData, cbinfo.flags & CB_OWNER))
511 bCBHasChanged = TRUE;
518 /**************************************************************************
519 * CountClipboardFormats (USER32.@)
521 INT WINAPI CountClipboardFormats(void)
523 INT count = USER_Driver->pCountClipboardFormats();
524 TRACE("returning %d\n", count);
529 /**************************************************************************
530 * EnumClipboardFormats (USER32.@)
532 UINT WINAPI EnumClipboardFormats(UINT wFormat)
534 CLIPBOARDINFO cbinfo;
536 TRACE("(%04X)\n", wFormat);
538 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
539 (~cbinfo.flags & CB_OPEN))
541 WARN("Clipboard not opened by calling task.\n");
542 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
545 return USER_Driver->pEnumClipboardFormats(wFormat);
549 /**************************************************************************
550 * IsClipboardFormatAvailable (USER32.@)
552 BOOL WINAPI IsClipboardFormatAvailable(UINT wFormat)
554 BOOL bret = USER_Driver->pIsClipboardFormatAvailable(wFormat);
555 TRACE("%04x, returning %d\n", wFormat, bret);
560 /**************************************************************************
561 * GetClipboardData (USER.142)
563 HANDLE16 WINAPI GetClipboardData16(UINT16 wFormat)
566 CLIPBOARDINFO cbinfo;
568 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
569 (~cbinfo.flags & CB_OPEN))
571 WARN("Clipboard not opened by calling task.\n");
572 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
576 if (!USER_Driver->pGetClipboardData(wFormat, &hData, NULL)) hData = 0;
582 /**************************************************************************
583 * GetClipboardData (USER32.@)
585 HANDLE WINAPI GetClipboardData(UINT wFormat)
588 CLIPBOARDINFO cbinfo;
590 TRACE("%04x\n", wFormat);
592 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
593 (~cbinfo.flags & CB_OPEN))
595 WARN("Clipboard not opened by calling task.\n");
596 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
600 if (!USER_Driver->pGetClipboardData(wFormat, NULL, &hData)) hData = 0;
602 TRACE("returning %p\n", hData);
607 /**************************************************************************
608 * GetPriorityClipboardFormat (USER32.@)
610 INT WINAPI GetPriorityClipboardFormat(UINT *list, INT nCount)
616 if(CountClipboardFormats() == 0)
619 for (i = 0; i < nCount; i++)
620 if (IsClipboardFormatAvailable(list[i]))
627 /**************************************************************************
628 * GetClipboardSequenceNumber (USER32.@)
629 * Supported on Win2k/Win98
630 * MSDN: Windows clipboard code keeps a serial number for the clipboard
631 * for each window station. The number is incremented whenever the
632 * contents change or are emptied.
633 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
635 DWORD WINAPI GetClipboardSequenceNumber(VOID)
639 SERVER_START_REQ( set_clipboard_info )
642 if (!wine_server_call_err( req )) seqno = reply->seqno;
646 TRACE("returning %x\n", seqno);