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 = WIN_GetFullHandle( hWnd );
93 if (wine_server_call_err( req ))
95 ERR("Failed to set clipboard owner to %p\n", hWnd);
108 /**************************************************************************
109 * CLIPBOARD_GetClipboardInfo
111 static BOOL CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo)
115 SERVER_START_REQ( set_clipboard_info )
119 if (wine_server_call_err( req ))
121 ERR("Failed to get clipboard info\n");
125 cbInfo->hWndOpen = reply->old_clipboard;
126 cbInfo->hWndOwner = reply->old_owner;
127 cbInfo->hWndViewer = reply->old_viewer;
128 cbInfo->seqno = reply->seqno;
129 cbInfo->flags = reply->flags;
140 /**************************************************************************
141 * CLIPBOARD_ReleaseOwner
143 BOOL CLIPBOARD_ReleaseOwner(void)
147 SERVER_START_REQ( set_clipboard_info )
149 req->flags = SET_CB_RELOWNER | SET_CB_SEQNO;
151 if (wine_server_call_err( req ))
153 ERR("Failed to set clipboard.\n");
166 /**************************************************************************
167 * CLIPBOARD_OpenClipboard
169 static BOOL CLIPBOARD_OpenClipboard(HWND hWnd)
173 SERVER_START_REQ( set_clipboard_info )
175 req->flags = SET_CB_OPEN;
176 req->clipboard = WIN_GetFullHandle( hWnd );
178 if (!wine_server_call( req ))
187 /**************************************************************************
188 * CLIPBOARD_CloseClipboard
190 static BOOL CLIPBOARD_CloseClipboard(void)
194 TRACE(" Changed=%d\n", bCBHasChanged);
196 SERVER_START_REQ( set_clipboard_info )
198 req->flags = SET_CB_CLOSE;
202 req->flags |= SET_CB_SEQNO;
203 TRACE("Clipboard data changed\n");
206 if (wine_server_call_err( req ))
208 ERR("Failed to set clipboard.\n");
221 /**************************************************************************
222 * WIN32 Clipboard implementation
223 **************************************************************************/
225 /**************************************************************************
226 * RegisterClipboardFormatW (USER32.@)
228 UINT WINAPI RegisterClipboardFormatW(LPCWSTR FormatName)
230 return USER_Driver->pRegisterClipboardFormat(FormatName);
234 /**************************************************************************
235 * RegisterClipboardFormatA (USER32.@)
237 UINT WINAPI RegisterClipboardFormatA(LPCSTR formatName)
243 len = MultiByteToWideChar(CP_ACP, 0, formatName, -1, NULL, 0);
244 wFormat = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
245 MultiByteToWideChar(CP_ACP, 0, formatName, -1, wFormat, len);
247 ret = RegisterClipboardFormatW(wFormat);
248 HeapFree(GetProcessHeap(), 0, wFormat);
253 /**************************************************************************
254 * GetClipboardFormatNameW (USER32.@)
256 INT WINAPI GetClipboardFormatNameW(UINT wFormat, LPWSTR retStr, INT maxlen)
258 return USER_Driver->pGetClipboardFormatName(wFormat, retStr, maxlen);
262 /**************************************************************************
263 * GetClipboardFormatNameA (USER32.@)
265 INT WINAPI GetClipboardFormatNameA(UINT wFormat, LPSTR retStr, INT maxlen)
268 LPWSTR p = HeapAlloc( GetProcessHeap(), 0, maxlen*sizeof(WCHAR) );
269 if(p == NULL) return 0; /* FIXME: is this the correct failure value? */
271 ret = GetClipboardFormatNameW( wFormat, p, maxlen );
273 if (ret && maxlen > 0 && !WideCharToMultiByte( CP_ACP, 0, p, -1, retStr, maxlen, 0, 0))
274 retStr[maxlen-1] = 0;
275 HeapFree( GetProcessHeap(), 0, p );
280 /**************************************************************************
281 * OpenClipboard (USER32.@)
283 * Note: Netscape uses NULL hWnd to open the clipboard.
285 BOOL WINAPI OpenClipboard( HWND hWnd )
289 TRACE("(%p)...\n", hWnd);
291 bRet = CLIPBOARD_OpenClipboard(hWnd);
293 TRACE(" returning %i\n", bRet);
299 /**************************************************************************
300 * CloseClipboard (USER32.@)
302 BOOL WINAPI CloseClipboard(void)
306 TRACE("(%d)\n", bCBHasChanged);
308 if (CLIPBOARD_CloseClipboard())
312 HWND hWndViewer = GetClipboardViewer();
314 USER_Driver->pEndClipboardUpdate();
317 SendMessageW(hWndViewer, WM_DRAWCLIPBOARD, 0, 0);
319 bCBHasChanged = FALSE;
329 /**************************************************************************
330 * EmptyClipboard (USER32.@)
331 * Empties and acquires ownership of the clipboard
333 BOOL WINAPI EmptyClipboard(void)
335 CLIPBOARDINFO cbinfo;
339 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
340 ~cbinfo.flags & CB_OPEN)
342 WARN("Clipboard not opened by calling task!\n");
343 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
347 /* Destroy private objects */
348 if (cbinfo.hWndOwner)
349 SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
351 /* Tell the driver to acquire the selection. The current owner
352 * will be signaled to delete it's own cache. */
354 /* Assign ownership of the clipboard to the current client. We do
355 * this before acquiring the selection so that when we do acquire the
356 * selection and the selection loser gets notified, it can check if
357 * it has lost the Wine clipboard ownership. If it did then it knows
358 * that a WM_DESTORYCLIPBOARD has already been sent. Otherwise it
359 * lost the selection to a X app and it should send the
360 * WM_DESTROYCLIPBOARD itself. */
361 CLIPBOARD_SetClipboardOwner(cbinfo.hWndOpen);
363 /* Acquire the selection. This will notify the previous owner
364 * to clear it's cache. */
365 USER_Driver->pAcquireClipboard(cbinfo.hWndOpen);
367 /* Empty the local cache */
368 USER_Driver->pEmptyClipboard(FALSE);
370 bCBHasChanged = TRUE;
376 /**************************************************************************
377 * GetClipboardOwner (USER32.@)
378 * FIXME: Can't return the owner if the clipboard is owned by an external X-app
380 HWND WINAPI GetClipboardOwner(void)
384 SERVER_START_REQ( set_clipboard_info )
387 if (!wine_server_call_err( req )) hWndOwner = reply->old_owner;
391 TRACE(" hWndOwner(%p)\n", hWndOwner);
397 /**************************************************************************
398 * GetOpenClipboardWindow (USER32.@)
400 HWND WINAPI GetOpenClipboardWindow(void)
404 SERVER_START_REQ( set_clipboard_info )
407 if (!wine_server_call_err( req )) hWndOpen = reply->old_clipboard;
411 TRACE(" hWndClipWindow(%p)\n", hWndOpen);
417 /**************************************************************************
418 * SetClipboardViewer (USER32.@)
420 HWND WINAPI SetClipboardViewer( HWND hWnd )
424 SERVER_START_REQ( set_clipboard_info )
426 req->flags = SET_CB_VIEWER;
427 req->viewer = WIN_GetFullHandle(hWnd);
429 if (wine_server_call_err( req ))
431 ERR("Failed to set clipboard.\n");
435 hwndPrev = reply->old_viewer;
440 TRACE("(%p): returning %p\n", hWnd, hwndPrev);
446 /**************************************************************************
447 * GetClipboardViewer (USER32.@)
449 HWND WINAPI GetClipboardViewer(void)
453 SERVER_START_REQ( set_clipboard_info )
456 if (!wine_server_call_err( req )) hWndViewer = reply->old_viewer;
460 TRACE(" hWndViewer=%p\n", hWndViewer);
466 /**************************************************************************
467 * ChangeClipboardChain (USER32.@)
469 BOOL WINAPI ChangeClipboardChain(HWND hWnd, HWND hWndNext)
472 HWND hWndViewer = GetClipboardViewer();
476 if (WIN_GetFullHandle(hWnd) == hWndViewer)
477 SetClipboardViewer(WIN_GetFullHandle(hWndNext));
479 bRet = !SendMessageW(hWndViewer, WM_CHANGECBCHAIN, (WPARAM)hWnd, (LPARAM)hWndNext);
482 ERR("hWndViewer is lost\n");
488 /**************************************************************************
489 * SetClipboardData (USER32.@)
491 HANDLE WINAPI SetClipboardData(UINT wFormat, HANDLE hData)
493 CLIPBOARDINFO cbinfo;
496 TRACE("(%04X, %p) !\n", wFormat, hData);
498 /* If it's not owned, data can only be set if the format isn't
499 available and its rendering is not delayed */
500 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
501 (!(cbinfo.flags & CB_OWNER) && !hData))
503 WARN("Clipboard not owned by calling task. Operation failed.\n");
507 if (USER_Driver->pSetClipboardData(wFormat, 0, hData, cbinfo.flags & CB_OWNER))
510 bCBHasChanged = TRUE;
517 /**************************************************************************
518 * CountClipboardFormats (USER32.@)
520 INT WINAPI CountClipboardFormats(void)
522 INT count = USER_Driver->pCountClipboardFormats();
523 TRACE("returning %d\n", count);
528 /**************************************************************************
529 * EnumClipboardFormats (USER32.@)
531 UINT WINAPI EnumClipboardFormats(UINT wFormat)
533 CLIPBOARDINFO cbinfo;
535 TRACE("(%04X)\n", wFormat);
537 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
538 (~cbinfo.flags & CB_OPEN))
540 WARN("Clipboard not opened by calling task.\n");
541 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
544 return USER_Driver->pEnumClipboardFormats(wFormat);
548 /**************************************************************************
549 * IsClipboardFormatAvailable (USER32.@)
551 BOOL WINAPI IsClipboardFormatAvailable(UINT wFormat)
553 BOOL bret = USER_Driver->pIsClipboardFormatAvailable(wFormat);
554 TRACE("%04x, returning %d\n", wFormat, bret);
559 /**************************************************************************
560 * GetClipboardData (USER32.@)
562 HANDLE WINAPI GetClipboardData(UINT wFormat)
565 CLIPBOARDINFO cbinfo;
567 TRACE("%04x\n", wFormat);
569 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
570 (~cbinfo.flags & CB_OPEN))
572 WARN("Clipboard not opened by calling task.\n");
573 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
577 if (!USER_Driver->pGetClipboardData(wFormat, NULL, &hData)) hData = 0;
579 TRACE("returning %p\n", hData);
584 /**************************************************************************
585 * GetPriorityClipboardFormat (USER32.@)
587 INT WINAPI GetPriorityClipboardFormat(UINT *list, INT nCount)
593 if(CountClipboardFormats() == 0)
596 for (i = 0; i < nCount; i++)
597 if (IsClipboardFormatAvailable(list[i]))
604 /**************************************************************************
605 * GetClipboardSequenceNumber (USER32.@)
606 * Supported on Win2k/Win98
607 * MSDN: Windows clipboard code keeps a serial number for the clipboard
608 * for each window station. The number is incremented whenever the
609 * contents change or are emptied.
610 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
612 DWORD WINAPI GetClipboardSequenceNumber(VOID)
616 SERVER_START_REQ( set_clipboard_info )
619 if (!wine_server_call_err( req )) seqno = reply->seqno;
623 TRACE("returning %x\n", seqno);