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"
52 #include "user_private.h"
55 #include "wine/debug.h"
56 #include "wine/unicode.h"
57 #include "wine/server.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(clipboard
);
61 #define CF_REGFORMATBASE 0xC000
70 } CLIPBOARDINFO
, *LPCLIPBOARDINFO
;
73 * Indicates if data has changed since open.
75 static BOOL bCBHasChanged
= FALSE
;
78 /**************************************************************************
79 * CLIPBOARD_SetClipboardOwner
81 BOOL
CLIPBOARD_SetClipboardOwner(HWND hWnd
)
85 TRACE(" hWnd(%p)\n", hWnd
);
87 SERVER_START_REQ( set_clipboard_info
)
89 req
->flags
= SET_CB_OWNER
;
90 req
->owner
= WIN_GetFullHandle( hWnd
);
92 if (wine_server_call_err( req
))
94 ERR("Failed to set clipboard owner to %p\n", hWnd
);
107 /**************************************************************************
108 * CLIPBOARD_GetClipboardInfo
110 static BOOL
CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo
)
114 SERVER_START_REQ( set_clipboard_info
)
118 if (wine_server_call_err( req
))
120 ERR("Failed to get clipboard info\n");
124 cbInfo
->hWndOpen
= reply
->old_clipboard
;
125 cbInfo
->hWndOwner
= reply
->old_owner
;
126 cbInfo
->hWndViewer
= reply
->old_viewer
;
127 cbInfo
->seqno
= reply
->seqno
;
128 cbInfo
->flags
= reply
->flags
;
139 /**************************************************************************
140 * CLIPBOARD_ReleaseOwner
142 BOOL
CLIPBOARD_ReleaseOwner(void)
146 SERVER_START_REQ( set_clipboard_info
)
148 req
->flags
= SET_CB_RELOWNER
| SET_CB_SEQNO
;
150 if (wine_server_call_err( req
))
152 ERR("Failed to set clipboard.\n");
165 /**************************************************************************
166 * CLIPBOARD_OpenClipboard
168 static BOOL
CLIPBOARD_OpenClipboard(HWND hWnd
)
172 SERVER_START_REQ( set_clipboard_info
)
174 req
->flags
= SET_CB_OPEN
;
175 req
->clipboard
= WIN_GetFullHandle( hWnd
);
177 if (!wine_server_call( req
))
186 /**************************************************************************
187 * CLIPBOARD_CloseClipboard
189 static BOOL
CLIPBOARD_CloseClipboard(void)
193 TRACE(" Changed=%d\n", bCBHasChanged
);
195 SERVER_START_REQ( set_clipboard_info
)
197 req
->flags
= SET_CB_CLOSE
;
201 req
->flags
|= SET_CB_SEQNO
;
202 TRACE("Clipboard data changed\n");
205 if (wine_server_call_err( req
))
207 ERR("Failed to set clipboard.\n");
220 /**************************************************************************
221 * WIN32 Clipboard implementation
222 **************************************************************************/
224 /**************************************************************************
225 * RegisterClipboardFormatA (USER32.@)
227 UINT WINAPI
RegisterClipboardFormatA(LPCSTR FormatName
)
231 TRACE("%s\n", debugstr_a(FormatName
));
233 if (USER_Driver
.pRegisterClipboardFormat
)
234 wFormatID
= USER_Driver
.pRegisterClipboardFormat(FormatName
);
240 /**************************************************************************
241 * RegisterClipboardFormatW (USER32.@)
243 UINT WINAPI
RegisterClipboardFormatW(LPCWSTR formatName
)
245 LPSTR aFormat
= HEAP_strdupWtoA( GetProcessHeap(), 0, formatName
);
246 UINT ret
= RegisterClipboardFormatA( aFormat
);
247 HeapFree( GetProcessHeap(), 0, aFormat
);
252 /**************************************************************************
253 * GetClipboardFormatNameA (USER32.@)
255 INT WINAPI
GetClipboardFormatNameA(UINT wFormat
, LPSTR retStr
, INT maxlen
)
259 TRACE("%04x,%p,%d\n", wFormat
, retStr
, maxlen
);
261 if (USER_Driver
.pGetClipboardFormatName
)
262 len
= USER_Driver
.pGetClipboardFormatName(wFormat
, retStr
, maxlen
);
268 /**************************************************************************
269 * GetClipboardFormatNameW (USER32.@)
271 INT WINAPI
GetClipboardFormatNameW(UINT wFormat
, LPWSTR retStr
, INT maxlen
)
274 LPSTR p
= HeapAlloc( GetProcessHeap(), 0, maxlen
);
275 if(p
== NULL
) return 0; /* FIXME: is this the correct failure value? */
277 ret
= GetClipboardFormatNameA( wFormat
, p
, maxlen
);
279 if (maxlen
> 0 && !MultiByteToWideChar( CP_ACP
, 0, p
, -1, retStr
, maxlen
))
280 retStr
[maxlen
-1] = 0;
281 HeapFree( GetProcessHeap(), 0, p
);
286 /**************************************************************************
287 * OpenClipboard (USER32.@)
289 * Note: Netscape uses NULL hWnd to open the clipboard.
291 BOOL WINAPI
OpenClipboard( HWND hWnd
)
295 TRACE("(%p)...\n", hWnd
);
297 bRet
= CLIPBOARD_OpenClipboard(hWnd
);
299 TRACE(" returning %i\n", bRet
);
305 /**************************************************************************
306 * CloseClipboard (USER32.@)
308 BOOL WINAPI
CloseClipboard(void)
312 TRACE("(%d)\n", bCBHasChanged
);
314 if (CLIPBOARD_CloseClipboard())
318 HWND hWndViewer
= GetClipboardViewer();
320 if (USER_Driver
.pEndClipboardUpdate
)
321 USER_Driver
.pEndClipboardUpdate();
324 SendMessageW(hWndViewer
, WM_DRAWCLIPBOARD
, 0, 0);
326 bCBHasChanged
= FALSE
;
336 /**************************************************************************
337 * EmptyClipboard (USER32.@)
338 * Empties and acquires ownership of the clipboard
340 BOOL WINAPI
EmptyClipboard(void)
342 CLIPBOARDINFO cbinfo
;
346 if (!CLIPBOARD_GetClipboardInfo(&cbinfo
) ||
347 ~cbinfo
.flags
& CB_OPEN
)
349 WARN("Clipboard not opened by calling task!\n");
350 SetLastError(ERROR_CLIPBOARD_NOT_OPEN
);
354 /* Destroy private objects */
355 if (cbinfo
.hWndOwner
)
356 SendMessageW(cbinfo
.hWndOwner
, WM_DESTROYCLIPBOARD
, 0, 0);
358 /* Tell the driver to acquire the selection. The current owner
359 * will be signaled to delete it's own cache. */
361 /* Assign ownership of the clipboard to the current client. We do
362 * this before acquiring the selection so that when we do acquire the
363 * selection and the selection loser gets notified, it can check if
364 * it has lost the Wine clipboard ownership. If it did then it knows
365 * that a WM_DESTORYCLIPBOARD has already been sent. Otherwise it
366 * lost the selection to a X app and it should send the
367 * WM_DESTROYCLIPBOARD itself. */
368 CLIPBOARD_SetClipboardOwner(cbinfo
.hWndOpen
);
370 /* Acquire the selection. This will notify the previous owner
371 * to clear it's cache. */
372 if (USER_Driver
.pAcquireClipboard
)
373 USER_Driver
.pAcquireClipboard(cbinfo
.hWndOpen
);
375 /* Empty the local cache */
376 if (USER_Driver
.pEmptyClipboard
)
377 USER_Driver
.pEmptyClipboard(FALSE
);
379 bCBHasChanged
= TRUE
;
385 /**************************************************************************
386 * GetClipboardOwner (USER32.@)
387 * FIXME: Can't return the owner if the clipboard is owned by an external X-app
389 HWND WINAPI
GetClipboardOwner(void)
393 SERVER_START_REQ( set_clipboard_info
)
396 if (!wine_server_call_err( req
)) hWndOwner
= reply
->old_owner
;
400 TRACE(" hWndOwner(%p)\n", hWndOwner
);
406 /**************************************************************************
407 * GetOpenClipboardWindow (USER32.@)
409 HWND WINAPI
GetOpenClipboardWindow(void)
413 SERVER_START_REQ( set_clipboard_info
)
416 if (!wine_server_call_err( req
)) hWndOpen
= reply
->old_clipboard
;
420 TRACE(" hWndClipWindow(%p)\n", hWndOpen
);
426 /**************************************************************************
427 * SetClipboardViewer (USER32.@)
429 HWND WINAPI
SetClipboardViewer( HWND hWnd
)
433 SERVER_START_REQ( set_clipboard_info
)
435 req
->flags
= SET_CB_VIEWER
;
436 req
->viewer
= WIN_GetFullHandle(hWnd
);
438 if (wine_server_call_err( req
))
440 ERR("Failed to set clipboard.\n");
444 hwndPrev
= reply
->old_viewer
;
449 TRACE("(%p): returning %p\n", hWnd
, hwndPrev
);
455 /**************************************************************************
456 * GetClipboardViewer (USER32.@)
458 HWND WINAPI
GetClipboardViewer(void)
462 SERVER_START_REQ( set_clipboard_info
)
465 if (!wine_server_call_err( req
)) hWndViewer
= reply
->old_viewer
;
469 TRACE(" hWndViewer=%p\n", hWndViewer
);
475 /**************************************************************************
476 * ChangeClipboardChain (USER32.@)
478 BOOL WINAPI
ChangeClipboardChain(HWND hWnd
, HWND hWndNext
)
481 HWND hWndViewer
= GetClipboardViewer();
485 if (WIN_GetFullHandle(hWnd
) == hWndViewer
)
486 SetClipboardViewer(WIN_GetFullHandle(hWndNext
));
488 bRet
= !SendMessageW(hWndViewer
, WM_CHANGECBCHAIN
, (WPARAM
)hWnd
, (LPARAM
)hWndNext
);
491 ERR("hWndViewer is lost\n");
497 /**************************************************************************
498 * SetClipboardData (USER.141)
500 HANDLE16 WINAPI
SetClipboardData16(UINT16 wFormat
, HANDLE16 hData
)
502 CLIPBOARDINFO cbinfo
;
503 HANDLE16 hResult
= 0;
505 TRACE("(%04X, %04x) !\n", wFormat
, hData
);
507 /* If it's not owned, data can only be set if the format doesn't exists
508 and its rendering is not delayed */
509 if (!CLIPBOARD_GetClipboardInfo(&cbinfo
) ||
510 (!(cbinfo
.flags
& CB_OWNER
) && !hData
))
512 WARN("Clipboard not owned by calling task. Operation failed.\n");
516 if (USER_Driver
.pSetClipboardData
&&
517 USER_Driver
.pSetClipboardData(wFormat
, hData
, 0, cbinfo
.flags
& CB_OWNER
))
520 bCBHasChanged
= TRUE
;
527 /**************************************************************************
528 * SetClipboardData (USER32.@)
530 HANDLE WINAPI
SetClipboardData(UINT wFormat
, HANDLE hData
)
532 CLIPBOARDINFO cbinfo
;
535 TRACE("(%04X, %p) !\n", wFormat
, hData
);
537 /* If it's not owned, data can only be set if the format isn't
538 available and its rendering is not delayed */
539 if (!CLIPBOARD_GetClipboardInfo(&cbinfo
) ||
540 (!(cbinfo
.flags
& CB_OWNER
) && !hData
))
542 WARN("Clipboard not owned by calling task. Operation failed.\n");
546 if (USER_Driver
.pSetClipboardData
&&
547 USER_Driver
.pSetClipboardData(wFormat
, 0, hData
, cbinfo
.flags
& CB_OWNER
))
550 bCBHasChanged
= TRUE
;
557 /**************************************************************************
558 * CountClipboardFormats (USER32.@)
560 INT WINAPI
CountClipboardFormats(void)
564 if (USER_Driver
.pCountClipboardFormats
)
565 count
= USER_Driver
.pCountClipboardFormats();
567 TRACE("returning %d\n", count
);
572 /**************************************************************************
573 * EnumClipboardFormats (USER32.@)
575 UINT WINAPI
EnumClipboardFormats(UINT wFormat
)
578 CLIPBOARDINFO cbinfo
;
580 TRACE("(%04X)\n", wFormat
);
582 if (!CLIPBOARD_GetClipboardInfo(&cbinfo
) ||
583 (~cbinfo
.flags
& CB_OPEN
))
585 WARN("Clipboard not opened by calling task.\n");
586 SetLastError(ERROR_CLIPBOARD_NOT_OPEN
);
590 if (USER_Driver
.pEnumClipboardFormats
)
591 wFmt
= USER_Driver
.pEnumClipboardFormats(wFormat
);
597 /**************************************************************************
598 * IsClipboardFormatAvailable (USER32.@)
600 BOOL WINAPI
IsClipboardFormatAvailable(UINT wFormat
)
604 if (USER_Driver
.pIsClipboardFormatAvailable
)
605 bret
= USER_Driver
.pIsClipboardFormatAvailable(wFormat
);
607 TRACE("%04x, returning %d\n", wFormat
, bret
);
612 /**************************************************************************
613 * GetClipboardData (USER.142)
615 HANDLE16 WINAPI
GetClipboardData16(UINT16 wFormat
)
618 CLIPBOARDINFO cbinfo
;
620 if (!CLIPBOARD_GetClipboardInfo(&cbinfo
) ||
621 (~cbinfo
.flags
& CB_OPEN
))
623 WARN("Clipboard not opened by calling task.\n");
624 SetLastError(ERROR_CLIPBOARD_NOT_OPEN
);
628 if (USER_Driver
.pGetClipboardData
)
629 USER_Driver
.pGetClipboardData(wFormat
, &hData
, NULL
);
635 /**************************************************************************
636 * GetClipboardData (USER32.@)
638 HANDLE WINAPI
GetClipboardData(UINT wFormat
)
641 CLIPBOARDINFO cbinfo
;
643 TRACE("%04x\n", wFormat
);
645 if (!CLIPBOARD_GetClipboardInfo(&cbinfo
) ||
646 (~cbinfo
.flags
& CB_OPEN
))
648 WARN("Clipboard not opened by calling task.\n");
649 SetLastError(ERROR_CLIPBOARD_NOT_OPEN
);
653 if (USER_Driver
.pGetClipboardData
)
654 USER_Driver
.pGetClipboardData(wFormat
, NULL
, &hData
);
656 TRACE("returning %p\n", hData
);
661 /**************************************************************************
662 * GetPriorityClipboardFormat (USER32.@)
664 INT WINAPI
GetPriorityClipboardFormat(UINT
*list
, INT nCount
)
670 if(CountClipboardFormats() == 0)
673 for (i
= 0; i
< nCount
; i
++)
674 if (IsClipboardFormatAvailable(list
[i
]))
681 /**************************************************************************
682 * GetClipboardSequenceNumber (USER32.@)
683 * Supported on Win2k/Win98
684 * MSDN: Windows clipboard code keeps a serial number for the clipboard
685 * for each window station. The number is incremented whenever the
686 * contents change or are emptied.
687 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
689 DWORD WINAPI
GetClipboardSequenceNumber(VOID
)
693 SERVER_START_REQ( set_clipboard_info
)
696 if (!wine_server_call_err( req
)) seqno
= reply
->seqno
;
700 TRACE("returning %lx\n", seqno
);