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"
36 #include <sys/types.h>
48 #include "wine/winuser16.h"
49 #include "wine/winbase16.h"
53 #include "clipboard.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
65 * Indicates if data has changed since open.
67 static BOOL bCBHasChanged
= FALSE
;
70 /**************************************************************************
71 * CLIPBOARD_SetClipboardOwner
73 BOOL
CLIPBOARD_SetClipboardOwner(HWND hWnd
)
77 TRACE(" hWnd(%p)\n", hWnd
);
79 SERVER_START_REQ( set_clipboard_info
)
81 req
->flags
= SET_CB_OWNER
;
82 req
->owner
= WIN_GetFullHandle( hWnd
);
84 if (wine_server_call_err( req
))
86 ERR("Failed to set clipboard.\n");
99 /**************************************************************************
100 * CLIPBOARD_GetClipboardInfo
102 BOOL
CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo
)
106 SERVER_START_REQ( set_clipboard_info
)
110 if (wine_server_call_err( req
))
112 ERR("Failed to get clipboard owner.\n");
116 cbInfo
->hWndOpen
= reply
->old_clipboard
;
117 cbInfo
->hWndOwner
= reply
->old_owner
;
118 cbInfo
->hWndViewer
= reply
->old_viewer
;
119 cbInfo
->seqno
= reply
->seqno
;
120 cbInfo
->flags
= reply
->flags
;
131 /**************************************************************************
132 * CLIPBOARD_ReleaseOwner
134 BOOL
CLIPBOARD_ReleaseOwner(void)
138 SERVER_START_REQ( set_clipboard_info
)
140 req
->flags
= SET_CB_RELOWNER
| SET_CB_SEQNO
;
142 if (wine_server_call_err( req
))
144 ERR("Failed to set clipboard.\n");
157 /**************************************************************************
158 * CLIPBOARD_OpenClipboard
160 static BOOL
CLIPBOARD_OpenClipboard(HWND hWnd
)
164 SERVER_START_REQ( set_clipboard_info
)
166 req
->flags
= SET_CB_OPEN
;
167 req
->clipboard
= WIN_GetFullHandle( hWnd
);
169 if (wine_server_call_err( req
))
171 ERR("Failed to set clipboard.\n");
184 /**************************************************************************
185 * CLIPBOARD_CloseClipboard
187 static BOOL
CLIPBOARD_CloseClipboard(void)
191 TRACE(" Changed=%d\n", bCBHasChanged
);
193 SERVER_START_REQ( set_clipboard_info
)
195 req
->flags
= SET_CB_CLOSE
;
199 req
->flags
|= SET_CB_SEQNO
;
200 TRACE("Clipboard data changed\n");
203 if (wine_server_call_err( req
))
205 ERR("Failed to set clipboard.\n");
218 /**************************************************************************
219 * WIN32 Clipboard implementation
220 **************************************************************************/
222 /**************************************************************************
223 * RegisterClipboardFormatA (USER32.@)
225 UINT WINAPI
RegisterClipboardFormatA(LPCSTR FormatName
)
229 TRACE("%s\n", debugstr_a(FormatName
));
231 if (USER_Driver
.pRegisterClipboardFormat
)
232 wFormatID
= USER_Driver
.pRegisterClipboardFormat(FormatName
);
238 /**************************************************************************
239 * RegisterClipboardFormat (USER.145)
241 UINT16 WINAPI
RegisterClipboardFormat16(LPCSTR FormatName
)
245 TRACE("%s\n", debugstr_a(FormatName
));
247 if (USER_Driver
.pRegisterClipboardFormat
)
248 wFormatID
= USER_Driver
.pRegisterClipboardFormat(FormatName
);
254 /**************************************************************************
255 * RegisterClipboardFormatW (USER32.@)
257 UINT WINAPI
RegisterClipboardFormatW(LPCWSTR formatName
)
259 LPSTR aFormat
= HEAP_strdupWtoA( GetProcessHeap(), 0, formatName
);
260 UINT ret
= RegisterClipboardFormatA( aFormat
);
261 HeapFree( GetProcessHeap(), 0, aFormat
);
266 /**************************************************************************
267 * GetClipboardFormatName (USER.146)
269 INT16 WINAPI
GetClipboardFormatName16(UINT16 wFormat
, LPSTR retStr
, INT16 maxlen
)
271 TRACE("%04x,%p,%d\n", wFormat
, retStr
, maxlen
);
273 return GetClipboardFormatNameA(wFormat
, retStr
, maxlen
);
277 /**************************************************************************
278 * GetClipboardFormatNameA (USER32.@)
280 INT WINAPI
GetClipboardFormatNameA(UINT wFormat
, LPSTR retStr
, INT maxlen
)
284 TRACE("%04x,%p,%d\n", wFormat
, retStr
, maxlen
);
286 if (USER_Driver
.pGetClipboardFormatName
)
287 len
= USER_Driver
.pGetClipboardFormatName(wFormat
, retStr
, maxlen
);
293 /**************************************************************************
294 * GetClipboardFormatNameW (USER32.@)
296 INT WINAPI
GetClipboardFormatNameW(UINT wFormat
, LPWSTR retStr
, INT maxlen
)
299 LPSTR p
= HeapAlloc( GetProcessHeap(), 0, maxlen
);
300 if(p
== NULL
) return 0; /* FIXME: is this the correct failure value? */
302 ret
= GetClipboardFormatNameA( wFormat
, p
, maxlen
);
304 if (maxlen
> 0 && !MultiByteToWideChar( CP_ACP
, 0, p
, -1, retStr
, maxlen
))
305 retStr
[maxlen
-1] = 0;
306 HeapFree( GetProcessHeap(), 0, p
);
311 /**************************************************************************
312 * OpenClipboard (USER32.@)
314 * Note: Netscape uses NULL hWnd to open the clipboard.
316 BOOL WINAPI
OpenClipboard( HWND hWnd
)
320 TRACE("(%p)...\n", hWnd
);
322 bRet
= CLIPBOARD_OpenClipboard(hWnd
);
324 TRACE(" returning %i\n", bRet
);
330 /**************************************************************************
331 * CloseClipboard (USER.138)
333 BOOL16 WINAPI
CloseClipboard16(void)
335 return CloseClipboard();
339 /**************************************************************************
340 * CloseClipboard (USER32.@)
342 BOOL WINAPI
CloseClipboard(void)
346 TRACE("(%d)\n", bCBHasChanged
);
348 if (CLIPBOARD_CloseClipboard())
352 HWND hWndViewer
= GetClipboardViewer();
354 if (USER_Driver
.pEndClipboardUpdate
)
355 USER_Driver
.pEndClipboardUpdate();
358 SendMessageW(hWndViewer
, WM_DRAWCLIPBOARD
, 0, 0);
360 bCBHasChanged
= FALSE
;
370 /**************************************************************************
371 * EmptyClipboard (USER.139)
373 BOOL16 WINAPI
EmptyClipboard16(void)
375 return EmptyClipboard();
379 /**************************************************************************
380 * EmptyClipboard (USER32.@)
381 * Empties and acquires ownership of the clipboard
383 BOOL WINAPI
EmptyClipboard(void)
385 CLIPBOARDINFO cbinfo
;
389 if (!CLIPBOARD_GetClipboardInfo(&cbinfo
) ||
390 ~cbinfo
.flags
& CB_OPEN
)
392 WARN("Clipboard not opened by calling task!\n");
393 SetLastError(ERROR_CLIPBOARD_NOT_OPEN
);
397 /* Destroy private objects */
398 if (cbinfo
.hWndOwner
)
399 SendMessageW(cbinfo
.hWndOwner
, WM_DESTROYCLIPBOARD
, 0, 0);
401 /* Tell the driver to acquire the selection. The current owner
402 * will be signaled to delete it's own cache. */
403 if (~cbinfo
.flags
& CB_OWNER
)
405 /* Assign ownership of the clipboard to the current client. We do
406 * this before acquiring the selection so that when we do acquire the
407 * selection and the selection loser gets notified, it can check if
408 * it has lost the Wine clipboard ownership. If it did then it knows
409 * that a WM_DESTORYCLIPBOARD has already been sent. Otherwise it
410 * lost the selection to a X app and it should send the
411 * WM_DESTROYCLIPBOARD itself. */
412 CLIPBOARD_SetClipboardOwner(cbinfo
.hWndOpen
);
414 /* Acquire the selection. This will notify the previous owner
415 * to clear it's cache. */
416 if (USER_Driver
.pAcquireClipboard
)
417 USER_Driver
.pAcquireClipboard(cbinfo
.hWndOpen
);
420 /* Empty the local cache */
421 if (USER_Driver
.pEmptyClipboard
)
422 USER_Driver
.pEmptyClipboard();
424 bCBHasChanged
= TRUE
;
430 /**************************************************************************
431 * GetClipboardOwner (USER32.@)
432 * FIXME: Can't return the owner if the clipboard is owned by an external X-app
434 HWND WINAPI
GetClipboardOwner(void)
437 CLIPBOARDINFO cbinfo
;
439 if (CLIPBOARD_GetClipboardInfo(&cbinfo
))
440 hWndOwner
= cbinfo
.hWndOwner
;
442 TRACE(" hWndOwner(%p)\n", hWndOwner
);
448 /**************************************************************************
449 * GetOpenClipboardWindow (USER32.@)
451 HWND WINAPI
GetOpenClipboardWindow(void)
454 CLIPBOARDINFO cbinfo
;
456 if (CLIPBOARD_GetClipboardInfo(&cbinfo
))
457 hWndOpen
= cbinfo
.hWndOpen
;
459 TRACE(" hWndClipWindow(%p)\n", hWndOpen
);
465 /**************************************************************************
466 * SetClipboardViewer (USER32.@)
468 HWND WINAPI
SetClipboardViewer( HWND hWnd
)
472 SERVER_START_REQ( set_clipboard_info
)
474 req
->flags
= SET_CB_VIEWER
;
475 req
->viewer
= WIN_GetFullHandle(hWnd
);
477 if (wine_server_call_err( req
))
479 ERR("Failed to set clipboard.\n");
483 hwndPrev
= reply
->old_viewer
;
488 TRACE("(%p): returning %p\n", hWnd
, hwndPrev
);
494 /**************************************************************************
495 * GetClipboardViewer (USER32.@)
497 HWND WINAPI
GetClipboardViewer(void)
500 CLIPBOARDINFO cbinfo
;
502 if (CLIPBOARD_GetClipboardInfo(&cbinfo
))
503 hWndViewer
= cbinfo
.hWndViewer
;
505 TRACE(" hWndViewer=%p\n", hWndViewer
);
511 /**************************************************************************
512 * ChangeClipboardChain (USER32.@)
514 BOOL WINAPI
ChangeClipboardChain(HWND hWnd
, HWND hWndNext
)
517 HWND hWndViewer
= GetClipboardViewer();
521 if (WIN_GetFullHandle(hWnd
) == hWndViewer
)
522 SetClipboardViewer(WIN_GetFullHandle(hWndNext
));
524 bRet
= !SendMessageW(hWndViewer
, WM_CHANGECBCHAIN
, (WPARAM
)hWnd
, (LPARAM
)hWndNext
);
527 ERR("hWndViewer is lost\n");
533 /**************************************************************************
534 * SetClipboardData (USER.141)
536 HANDLE16 WINAPI
SetClipboardData16(UINT16 wFormat
, HANDLE16 hData
)
538 CLIPBOARDINFO cbinfo
;
539 HANDLE16 hResult
= 0;
541 TRACE("(%04X, %04x) !\n", wFormat
, hData
);
543 if (!CLIPBOARD_GetClipboardInfo(&cbinfo
) ||
544 (~cbinfo
.flags
& CB_OPEN
) ||
545 (~cbinfo
.flags
& CB_OWNER
))
547 WARN("Clipboard not opened by calling task!\n");
549 else if (USER_Driver
.pSetClipboardData
&&
550 USER_Driver
.pSetClipboardData(wFormat
, hData
, 0))
553 bCBHasChanged
= TRUE
;
560 /**************************************************************************
561 * SetClipboardData (USER32.@)
563 HANDLE WINAPI
SetClipboardData(UINT wFormat
, HANDLE hData
)
565 CLIPBOARDINFO cbinfo
;
568 TRACE("(%04X, %p) !\n", wFormat
, hData
);
570 if (!CLIPBOARD_GetClipboardInfo(&cbinfo
) ||
571 (~cbinfo
.flags
& CB_OWNER
))
573 WARN("Clipboard not owned by calling task!\n");
575 else if (USER_Driver
.pSetClipboardData
&&
576 USER_Driver
.pSetClipboardData(wFormat
, 0, hData
))
579 bCBHasChanged
= TRUE
;
586 /**************************************************************************
587 * CountClipboardFormats (USER.143)
589 INT16 WINAPI
CountClipboardFormats16(void)
591 return CountClipboardFormats();
595 /**************************************************************************
596 * CountClipboardFormats (USER32.@)
598 INT WINAPI
CountClipboardFormats(void)
602 if (USER_Driver
.pCountClipboardFormats
)
603 count
= USER_Driver
.pCountClipboardFormats();
605 TRACE("returning %d\n", count
);
610 /**************************************************************************
611 * EnumClipboardFormats (USER.144)
613 UINT16 WINAPI
EnumClipboardFormats16(UINT16 wFormat
)
615 return EnumClipboardFormats(wFormat
);
619 /**************************************************************************
620 * EnumClipboardFormats (USER32.@)
622 UINT WINAPI
EnumClipboardFormats(UINT wFormat
)
625 CLIPBOARDINFO cbinfo
;
627 TRACE("(%04X)\n", wFormat
);
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
.pEnumClipboardFormats
)
638 wFmt
= USER_Driver
.pEnumClipboardFormats(wFormat
);
644 /**************************************************************************
645 * IsClipboardFormatAvailable (USER.193)
647 BOOL16 WINAPI
IsClipboardFormatAvailable16(UINT16 wFormat
)
649 return IsClipboardFormatAvailable(wFormat
);
653 /**************************************************************************
654 * IsClipboardFormatAvailable (USER32.@)
656 BOOL WINAPI
IsClipboardFormatAvailable(UINT wFormat
)
660 if (USER_Driver
.pIsClipboardFormatAvailable
)
661 bret
= USER_Driver
.pIsClipboardFormatAvailable(wFormat
);
663 TRACE("%04x, returning %d\n", wFormat
, bret
);
668 /**************************************************************************
669 * GetClipboardData (USER.142)
671 HANDLE16 WINAPI
GetClipboardData16(UINT16 wFormat
)
674 CLIPBOARDINFO cbinfo
;
676 if (!CLIPBOARD_GetClipboardInfo(&cbinfo
) ||
677 (~cbinfo
.flags
& CB_OPEN
))
679 WARN("Clipboard not opened by calling task.\n");
680 SetLastError(ERROR_CLIPBOARD_NOT_OPEN
);
684 if (USER_Driver
.pGetClipboardData
)
685 USER_Driver
.pGetClipboardData(wFormat
, &hData
, NULL
);
691 /**************************************************************************
692 * GetClipboardData (USER32.@)
694 HANDLE WINAPI
GetClipboardData(UINT wFormat
)
697 CLIPBOARDINFO cbinfo
;
699 TRACE("%04x\n", wFormat
);
701 if (!CLIPBOARD_GetClipboardInfo(&cbinfo
) ||
702 (~cbinfo
.flags
& CB_OPEN
))
704 WARN("Clipboard not opened by calling task.\n");
705 SetLastError(ERROR_CLIPBOARD_NOT_OPEN
);
709 if (USER_Driver
.pGetClipboardData
)
710 USER_Driver
.pGetClipboardData(wFormat
, NULL
, &hData
);
712 TRACE("returning %p\n", hData
);
717 /**************************************************************************
718 * GetPriorityClipboardFormat (USER32.@)
720 INT WINAPI
GetPriorityClipboardFormat(UINT
*list
, INT nCount
)
726 if(CountClipboardFormats() == 0)
729 for (i
= 0; i
< nCount
; i
++)
730 if (IsClipboardFormatAvailable(list
[i
]))
737 /**************************************************************************
738 * GetClipboardSequenceNumber (USER32.@)
739 * Supported on Win2k/Win98
740 * MSDN: Windows clipboard code keeps a serial number for the clipboard
741 * for each window station. The number is incremented whenever the
742 * contents change or are emptied.
743 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
745 DWORD WINAPI
GetClipboardSequenceNumber(VOID
)
747 CLIPBOARDINFO cbinfo
;
749 if (!CLIPBOARD_GetClipboardInfo(&cbinfo
))
751 ERR("Failed to get clipboard information.\n");
755 TRACE("returning %x\n", cbinfo
.seqno
);