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 );
192 /**************************************************************************
193 * WIN32 Clipboard implementation
194 **************************************************************************/
196 /**************************************************************************
197 * RegisterClipboardFormatW (USER32.@)
199 UINT WINAPI RegisterClipboardFormatW(LPCWSTR FormatName)
201 return USER_Driver->pRegisterClipboardFormat(FormatName);
205 /**************************************************************************
206 * RegisterClipboardFormatA (USER32.@)
208 UINT WINAPI RegisterClipboardFormatA(LPCSTR formatName)
214 len = MultiByteToWideChar(CP_ACP, 0, formatName, -1, NULL, 0);
215 wFormat = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
216 MultiByteToWideChar(CP_ACP, 0, formatName, -1, wFormat, len);
218 ret = RegisterClipboardFormatW(wFormat);
219 HeapFree(GetProcessHeap(), 0, wFormat);
224 /**************************************************************************
225 * GetClipboardFormatNameW (USER32.@)
227 INT WINAPI GetClipboardFormatNameW(UINT wFormat, LPWSTR retStr, INT maxlen)
229 return USER_Driver->pGetClipboardFormatName(wFormat, retStr, maxlen);
233 /**************************************************************************
234 * GetClipboardFormatNameA (USER32.@)
236 INT WINAPI GetClipboardFormatNameA(UINT wFormat, LPSTR retStr, INT maxlen)
239 LPWSTR p = HeapAlloc( GetProcessHeap(), 0, maxlen*sizeof(WCHAR) );
240 if(p == NULL) return 0; /* FIXME: is this the correct failure value? */
242 ret = GetClipboardFormatNameW( wFormat, p, maxlen );
244 if (ret && maxlen > 0 && !WideCharToMultiByte( CP_ACP, 0, p, -1, retStr, maxlen, 0, 0))
245 retStr[maxlen-1] = 0;
246 HeapFree( GetProcessHeap(), 0, p );
251 /**************************************************************************
252 * OpenClipboard (USER32.@)
254 * Note: Netscape uses NULL hWnd to open the clipboard.
256 BOOL WINAPI OpenClipboard( HWND hWnd )
260 TRACE("(%p)...\n", hWnd);
262 bRet = CLIPBOARD_OpenClipboard(hWnd);
264 TRACE(" returning %i\n", bRet);
270 /**************************************************************************
271 * CloseClipboard (USER32.@)
273 BOOL WINAPI CloseClipboard(void)
277 TRACE("(%d)\n", bCBHasChanged);
279 if (CLIPBOARD_CloseClipboard())
283 HWND hWndViewer = GetClipboardViewer();
285 USER_Driver->pEndClipboardUpdate();
288 SendMessageW(hWndViewer, WM_DRAWCLIPBOARD, 0, 0);
290 bCBHasChanged = FALSE;
300 /**************************************************************************
301 * EmptyClipboard (USER32.@)
302 * Empties and acquires ownership of the clipboard
304 BOOL WINAPI EmptyClipboard(void)
306 CLIPBOARDINFO cbinfo;
310 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
311 ~cbinfo.flags & CB_OPEN)
313 WARN("Clipboard not opened by calling task!\n");
314 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
318 /* Destroy private objects */
319 if (cbinfo.hWndOwner)
320 SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
322 /* Tell the driver to acquire the selection. The current owner
323 * will be signaled to delete it's own cache. */
325 /* Assign ownership of the clipboard to the current client. We do
326 * this before acquiring the selection so that when we do acquire the
327 * selection and the selection loser gets notified, it can check if
328 * it has lost the Wine clipboard ownership. If it did then it knows
329 * that a WM_DESTORYCLIPBOARD has already been sent. Otherwise it
330 * lost the selection to a X app and it should send the
331 * WM_DESTROYCLIPBOARD itself. */
332 CLIPBOARD_SetClipboardOwner(cbinfo.hWndOpen);
334 /* Acquire the selection. This will notify the previous owner
335 * to clear it's cache. */
336 USER_Driver->pAcquireClipboard(cbinfo.hWndOpen);
338 /* Empty the local cache */
339 USER_Driver->pEmptyClipboard(FALSE);
341 bCBHasChanged = TRUE;
347 /**************************************************************************
348 * GetClipboardOwner (USER32.@)
349 * FIXME: Can't return the owner if the clipboard is owned by an external X-app
351 HWND WINAPI GetClipboardOwner(void)
355 SERVER_START_REQ( set_clipboard_info )
358 if (!wine_server_call_err( req )) hWndOwner = wine_server_ptr_handle( reply->old_owner );
362 TRACE(" hWndOwner(%p)\n", hWndOwner);
368 /**************************************************************************
369 * GetOpenClipboardWindow (USER32.@)
371 HWND WINAPI GetOpenClipboardWindow(void)
375 SERVER_START_REQ( set_clipboard_info )
378 if (!wine_server_call_err( req )) hWndOpen = wine_server_ptr_handle( reply->old_clipboard );
382 TRACE(" hWndClipWindow(%p)\n", hWndOpen);
388 /**************************************************************************
389 * SetClipboardViewer (USER32.@)
391 HWND WINAPI SetClipboardViewer( HWND hWnd )
395 SERVER_START_REQ( set_clipboard_info )
397 req->flags = SET_CB_VIEWER;
398 req->viewer = wine_server_user_handle( hWnd );
399 if (!wine_server_call_err( req ))
400 hwndPrev = wine_server_ptr_handle( reply->old_viewer );
404 TRACE("(%p): returning %p\n", hWnd, hwndPrev);
410 /**************************************************************************
411 * GetClipboardViewer (USER32.@)
413 HWND WINAPI GetClipboardViewer(void)
417 SERVER_START_REQ( set_clipboard_info )
420 if (!wine_server_call_err( req )) hWndViewer = wine_server_ptr_handle( reply->old_viewer );
424 TRACE(" hWndViewer=%p\n", hWndViewer);
430 /**************************************************************************
431 * ChangeClipboardChain (USER32.@)
433 BOOL WINAPI ChangeClipboardChain(HWND hWnd, HWND hWndNext)
436 HWND hWndViewer = GetClipboardViewer();
440 if (WIN_GetFullHandle(hWnd) == hWndViewer)
441 SetClipboardViewer(WIN_GetFullHandle(hWndNext));
443 bRet = !SendMessageW(hWndViewer, WM_CHANGECBCHAIN, (WPARAM)hWnd, (LPARAM)hWndNext);
446 ERR("hWndViewer is lost\n");
452 /**************************************************************************
453 * SetClipboardData (USER.141)
455 HANDLE16 WINAPI SetClipboardData16(UINT16 wFormat, HANDLE16 hData)
457 CLIPBOARDINFO cbinfo;
458 HANDLE16 hResult = 0;
460 TRACE("(%04X, %04x) !\n", wFormat, hData);
462 /* If it's not owned, data can only be set if the format doesn't exists
463 and its rendering is not delayed */
464 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
465 (!(cbinfo.flags & CB_OWNER) && !hData))
467 WARN("Clipboard not owned by calling task. Operation failed.\n");
471 if (USER_Driver->pSetClipboardData(wFormat, hData, 0, cbinfo.flags & CB_OWNER))
474 bCBHasChanged = TRUE;
481 /**************************************************************************
482 * SetClipboardData (USER32.@)
484 HANDLE WINAPI SetClipboardData(UINT wFormat, HANDLE hData)
486 CLIPBOARDINFO cbinfo;
489 TRACE("(%04X, %p) !\n", wFormat, hData);
491 /* If it's not owned, data can only be set if the format isn't
492 available and its rendering is not delayed */
493 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
494 (!(cbinfo.flags & CB_OWNER) && !hData))
496 WARN("Clipboard not owned by calling task. Operation failed.\n");
500 if (USER_Driver->pSetClipboardData(wFormat, 0, hData, cbinfo.flags & CB_OWNER))
503 bCBHasChanged = TRUE;
510 /**************************************************************************
511 * CountClipboardFormats (USER32.@)
513 INT WINAPI CountClipboardFormats(void)
515 INT count = USER_Driver->pCountClipboardFormats();
516 TRACE("returning %d\n", count);
521 /**************************************************************************
522 * EnumClipboardFormats (USER32.@)
524 UINT WINAPI EnumClipboardFormats(UINT wFormat)
526 CLIPBOARDINFO cbinfo;
528 TRACE("(%04X)\n", wFormat);
530 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
531 (~cbinfo.flags & CB_OPEN))
533 WARN("Clipboard not opened by calling task.\n");
534 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
537 return USER_Driver->pEnumClipboardFormats(wFormat);
541 /**************************************************************************
542 * IsClipboardFormatAvailable (USER32.@)
544 BOOL WINAPI IsClipboardFormatAvailable(UINT wFormat)
546 BOOL bret = USER_Driver->pIsClipboardFormatAvailable(wFormat);
547 TRACE("%04x, returning %d\n", wFormat, bret);
552 /**************************************************************************
553 * GetClipboardData (USER.142)
555 HANDLE16 WINAPI GetClipboardData16(UINT16 wFormat)
558 CLIPBOARDINFO cbinfo;
560 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
561 (~cbinfo.flags & CB_OPEN))
563 WARN("Clipboard not opened by calling task.\n");
564 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
568 if (!USER_Driver->pGetClipboardData(wFormat, &hData, NULL)) hData = 0;
574 /**************************************************************************
575 * GetClipboardData (USER32.@)
577 HANDLE WINAPI GetClipboardData(UINT wFormat)
580 CLIPBOARDINFO cbinfo;
582 TRACE("%04x\n", wFormat);
584 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
585 (~cbinfo.flags & CB_OPEN))
587 WARN("Clipboard not opened by calling task.\n");
588 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
592 if (!USER_Driver->pGetClipboardData(wFormat, NULL, &hData)) hData = 0;
594 TRACE("returning %p\n", hData);
599 /**************************************************************************
600 * GetPriorityClipboardFormat (USER32.@)
602 INT WINAPI GetPriorityClipboardFormat(UINT *list, INT nCount)
608 if(CountClipboardFormats() == 0)
611 for (i = 0; i < nCount; i++)
612 if (IsClipboardFormatAvailable(list[i]))
619 /**************************************************************************
620 * GetClipboardSequenceNumber (USER32.@)
621 * Supported on Win2k/Win98
622 * MSDN: Windows clipboard code keeps a serial number for the clipboard
623 * for each window station. The number is incremented whenever the
624 * contents change or are emptied.
625 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
627 DWORD WINAPI GetClipboardSequenceNumber(VOID)
631 SERVER_START_REQ( set_clipboard_info )
634 if (!wine_server_call_err( req )) seqno = reply->seqno;
638 TRACE("returning %x\n", seqno);