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 * Set the global wineserver clipboard owner. The current process will
82 * be the owner and <hWnd> will get the render notifications.
84 BOOL
CLIPBOARD_SetClipboardOwner(HWND hWnd
)
88 TRACE(" hWnd(%p)\n", hWnd
);
90 SERVER_START_REQ( set_clipboard_info
)
92 req
->flags
= SET_CB_OWNER
;
93 req
->owner
= WIN_GetFullHandle( hWnd
);
95 if (wine_server_call_err( req
))
97 ERR("Failed to set clipboard owner to %p\n", hWnd
);
110 /**************************************************************************
111 * CLIPBOARD_GetClipboardInfo
113 static BOOL
CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo
)
117 SERVER_START_REQ( set_clipboard_info
)
121 if (wine_server_call_err( req
))
123 ERR("Failed to get clipboard info\n");
127 cbInfo
->hWndOpen
= reply
->old_clipboard
;
128 cbInfo
->hWndOwner
= reply
->old_owner
;
129 cbInfo
->hWndViewer
= reply
->old_viewer
;
130 cbInfo
->seqno
= reply
->seqno
;
131 cbInfo
->flags
= reply
->flags
;
142 /**************************************************************************
143 * CLIPBOARD_ReleaseOwner
145 BOOL
CLIPBOARD_ReleaseOwner(void)
149 SERVER_START_REQ( set_clipboard_info
)
151 req
->flags
= SET_CB_RELOWNER
| SET_CB_SEQNO
;
153 if (wine_server_call_err( req
))
155 ERR("Failed to set clipboard.\n");
168 /**************************************************************************
169 * CLIPBOARD_OpenClipboard
171 static BOOL
CLIPBOARD_OpenClipboard(HWND hWnd
)
175 SERVER_START_REQ( set_clipboard_info
)
177 req
->flags
= SET_CB_OPEN
;
178 req
->clipboard
= WIN_GetFullHandle( hWnd
);
180 if (!wine_server_call( req
))
189 /**************************************************************************
190 * CLIPBOARD_CloseClipboard
192 static BOOL
CLIPBOARD_CloseClipboard(void)
196 TRACE(" Changed=%d\n", bCBHasChanged
);
198 SERVER_START_REQ( set_clipboard_info
)
200 req
->flags
= SET_CB_CLOSE
;
204 req
->flags
|= SET_CB_SEQNO
;
205 TRACE("Clipboard data changed\n");
208 if (wine_server_call_err( req
))
210 ERR("Failed to set clipboard.\n");
223 /**************************************************************************
224 * WIN32 Clipboard implementation
225 **************************************************************************/
227 /**************************************************************************
228 * RegisterClipboardFormatA (USER32.@)
230 UINT WINAPI
RegisterClipboardFormatA(LPCSTR FormatName
)
234 TRACE("%s\n", debugstr_a(FormatName
));
236 if (USER_Driver
.pRegisterClipboardFormat
)
237 wFormatID
= USER_Driver
.pRegisterClipboardFormat(FormatName
);
243 /**************************************************************************
244 * RegisterClipboardFormatW (USER32.@)
246 UINT WINAPI
RegisterClipboardFormatW(LPCWSTR formatName
)
248 LPSTR aFormat
= HEAP_strdupWtoA( GetProcessHeap(), 0, formatName
);
249 UINT ret
= RegisterClipboardFormatA( aFormat
);
250 HeapFree( GetProcessHeap(), 0, aFormat
);
255 /**************************************************************************
256 * GetClipboardFormatNameA (USER32.@)
258 INT WINAPI
GetClipboardFormatNameA(UINT wFormat
, LPSTR retStr
, INT maxlen
)
262 TRACE("%04x,%p,%d\n", wFormat
, retStr
, maxlen
);
264 if (USER_Driver
.pGetClipboardFormatName
)
265 len
= USER_Driver
.pGetClipboardFormatName(wFormat
, retStr
, maxlen
);
271 /**************************************************************************
272 * GetClipboardFormatNameW (USER32.@)
274 INT WINAPI
GetClipboardFormatNameW(UINT wFormat
, LPWSTR retStr
, INT maxlen
)
277 LPSTR p
= HeapAlloc( GetProcessHeap(), 0, maxlen
);
278 if(p
== NULL
) return 0; /* FIXME: is this the correct failure value? */
280 ret
= GetClipboardFormatNameA( wFormat
, p
, maxlen
);
282 if (maxlen
> 0 && !MultiByteToWideChar( CP_ACP
, 0, p
, -1, retStr
, maxlen
))
283 retStr
[maxlen
-1] = 0;
284 HeapFree( GetProcessHeap(), 0, p
);
289 /**************************************************************************
290 * OpenClipboard (USER32.@)
292 * Note: Netscape uses NULL hWnd to open the clipboard.
294 BOOL WINAPI
OpenClipboard( HWND hWnd
)
298 TRACE("(%p)...\n", hWnd
);
300 bRet
= CLIPBOARD_OpenClipboard(hWnd
);
302 TRACE(" returning %i\n", bRet
);
308 /**************************************************************************
309 * CloseClipboard (USER32.@)
311 BOOL WINAPI
CloseClipboard(void)
315 TRACE("(%d)\n", bCBHasChanged
);
317 if (CLIPBOARD_CloseClipboard())
321 HWND hWndViewer
= GetClipboardViewer();
323 if (USER_Driver
.pEndClipboardUpdate
)
324 USER_Driver
.pEndClipboardUpdate();
327 SendMessageW(hWndViewer
, WM_DRAWCLIPBOARD
, 0, 0);
329 bCBHasChanged
= FALSE
;
339 /**************************************************************************
340 * EmptyClipboard (USER32.@)
341 * Empties and acquires ownership of the clipboard
343 BOOL WINAPI
EmptyClipboard(void)
345 CLIPBOARDINFO cbinfo
;
349 if (!CLIPBOARD_GetClipboardInfo(&cbinfo
) ||
350 ~cbinfo
.flags
& CB_OPEN
)
352 WARN("Clipboard not opened by calling task!\n");
353 SetLastError(ERROR_CLIPBOARD_NOT_OPEN
);
357 /* Destroy private objects */
358 if (cbinfo
.hWndOwner
)
359 SendMessageW(cbinfo
.hWndOwner
, WM_DESTROYCLIPBOARD
, 0, 0);
361 /* Tell the driver to acquire the selection. The current owner
362 * will be signaled to delete it's own cache. */
364 /* Assign ownership of the clipboard to the current client. We do
365 * this before acquiring the selection so that when we do acquire the
366 * selection and the selection loser gets notified, it can check if
367 * it has lost the Wine clipboard ownership. If it did then it knows
368 * that a WM_DESTORYCLIPBOARD has already been sent. Otherwise it
369 * lost the selection to a X app and it should send the
370 * WM_DESTROYCLIPBOARD itself. */
371 CLIPBOARD_SetClipboardOwner(cbinfo
.hWndOpen
);
373 /* Acquire the selection. This will notify the previous owner
374 * to clear it's cache. */
375 if (USER_Driver
.pAcquireClipboard
)
376 USER_Driver
.pAcquireClipboard(cbinfo
.hWndOpen
);
378 /* Empty the local cache */
379 if (USER_Driver
.pEmptyClipboard
)
380 USER_Driver
.pEmptyClipboard(FALSE
);
382 bCBHasChanged
= TRUE
;
388 /**************************************************************************
389 * GetClipboardOwner (USER32.@)
390 * FIXME: Can't return the owner if the clipboard is owned by an external X-app
392 HWND WINAPI
GetClipboardOwner(void)
396 SERVER_START_REQ( set_clipboard_info
)
399 if (!wine_server_call_err( req
)) hWndOwner
= reply
->old_owner
;
403 TRACE(" hWndOwner(%p)\n", hWndOwner
);
409 /**************************************************************************
410 * GetOpenClipboardWindow (USER32.@)
412 HWND WINAPI
GetOpenClipboardWindow(void)
416 SERVER_START_REQ( set_clipboard_info
)
419 if (!wine_server_call_err( req
)) hWndOpen
= reply
->old_clipboard
;
423 TRACE(" hWndClipWindow(%p)\n", hWndOpen
);
429 /**************************************************************************
430 * SetClipboardViewer (USER32.@)
432 HWND WINAPI
SetClipboardViewer( HWND hWnd
)
436 SERVER_START_REQ( set_clipboard_info
)
438 req
->flags
= SET_CB_VIEWER
;
439 req
->viewer
= WIN_GetFullHandle(hWnd
);
441 if (wine_server_call_err( req
))
443 ERR("Failed to set clipboard.\n");
447 hwndPrev
= reply
->old_viewer
;
452 TRACE("(%p): returning %p\n", hWnd
, hwndPrev
);
458 /**************************************************************************
459 * GetClipboardViewer (USER32.@)
461 HWND WINAPI
GetClipboardViewer(void)
465 SERVER_START_REQ( set_clipboard_info
)
468 if (!wine_server_call_err( req
)) hWndViewer
= reply
->old_viewer
;
472 TRACE(" hWndViewer=%p\n", hWndViewer
);
478 /**************************************************************************
479 * ChangeClipboardChain (USER32.@)
481 BOOL WINAPI
ChangeClipboardChain(HWND hWnd
, HWND hWndNext
)
484 HWND hWndViewer
= GetClipboardViewer();
488 if (WIN_GetFullHandle(hWnd
) == hWndViewer
)
489 SetClipboardViewer(WIN_GetFullHandle(hWndNext
));
491 bRet
= !SendMessageW(hWndViewer
, WM_CHANGECBCHAIN
, (WPARAM
)hWnd
, (LPARAM
)hWndNext
);
494 ERR("hWndViewer is lost\n");
500 /**************************************************************************
501 * SetClipboardData (USER.141)
503 HANDLE16 WINAPI
SetClipboardData16(UINT16 wFormat
, HANDLE16 hData
)
505 CLIPBOARDINFO cbinfo
;
506 HANDLE16 hResult
= 0;
508 TRACE("(%04X, %04x) !\n", wFormat
, hData
);
510 /* If it's not owned, data can only be set if the format doesn't exists
511 and its rendering is not delayed */
512 if (!CLIPBOARD_GetClipboardInfo(&cbinfo
) ||
513 (!(cbinfo
.flags
& CB_OWNER
) && !hData
))
515 WARN("Clipboard not owned by calling task. Operation failed.\n");
519 if (USER_Driver
.pSetClipboardData
&&
520 USER_Driver
.pSetClipboardData(wFormat
, hData
, 0, cbinfo
.flags
& CB_OWNER
))
523 bCBHasChanged
= TRUE
;
530 /**************************************************************************
531 * SetClipboardData (USER32.@)
533 HANDLE WINAPI
SetClipboardData(UINT wFormat
, HANDLE hData
)
535 CLIPBOARDINFO cbinfo
;
538 TRACE("(%04X, %p) !\n", wFormat
, hData
);
540 /* If it's not owned, data can only be set if the format isn't
541 available and its rendering is not delayed */
542 if (!CLIPBOARD_GetClipboardInfo(&cbinfo
) ||
543 (!(cbinfo
.flags
& CB_OWNER
) && !hData
))
545 WARN("Clipboard not owned by calling task. Operation failed.\n");
549 if (USER_Driver
.pSetClipboardData
&&
550 USER_Driver
.pSetClipboardData(wFormat
, 0, hData
, cbinfo
.flags
& CB_OWNER
))
553 bCBHasChanged
= TRUE
;
560 /**************************************************************************
561 * CountClipboardFormats (USER32.@)
563 INT WINAPI
CountClipboardFormats(void)
567 if (USER_Driver
.pCountClipboardFormats
)
568 count
= USER_Driver
.pCountClipboardFormats();
570 TRACE("returning %d\n", count
);
575 /**************************************************************************
576 * EnumClipboardFormats (USER32.@)
578 UINT WINAPI
EnumClipboardFormats(UINT wFormat
)
581 CLIPBOARDINFO cbinfo
;
583 TRACE("(%04X)\n", wFormat
);
585 if (!CLIPBOARD_GetClipboardInfo(&cbinfo
) ||
586 (~cbinfo
.flags
& CB_OPEN
))
588 WARN("Clipboard not opened by calling task.\n");
589 SetLastError(ERROR_CLIPBOARD_NOT_OPEN
);
593 if (USER_Driver
.pEnumClipboardFormats
)
594 wFmt
= USER_Driver
.pEnumClipboardFormats(wFormat
);
600 /**************************************************************************
601 * IsClipboardFormatAvailable (USER32.@)
603 BOOL WINAPI
IsClipboardFormatAvailable(UINT wFormat
)
607 if (USER_Driver
.pIsClipboardFormatAvailable
)
608 bret
= USER_Driver
.pIsClipboardFormatAvailable(wFormat
);
610 TRACE("%04x, returning %d\n", wFormat
, bret
);
615 /**************************************************************************
616 * GetClipboardData (USER.142)
618 HANDLE16 WINAPI
GetClipboardData16(UINT16 wFormat
)
621 CLIPBOARDINFO cbinfo
;
623 if (!CLIPBOARD_GetClipboardInfo(&cbinfo
) ||
624 (~cbinfo
.flags
& CB_OPEN
))
626 WARN("Clipboard not opened by calling task.\n");
627 SetLastError(ERROR_CLIPBOARD_NOT_OPEN
);
631 if (USER_Driver
.pGetClipboardData
)
632 USER_Driver
.pGetClipboardData(wFormat
, &hData
, NULL
);
638 /**************************************************************************
639 * GetClipboardData (USER32.@)
641 HANDLE WINAPI
GetClipboardData(UINT wFormat
)
644 CLIPBOARDINFO cbinfo
;
646 TRACE("%04x\n", wFormat
);
648 if (!CLIPBOARD_GetClipboardInfo(&cbinfo
) ||
649 (~cbinfo
.flags
& CB_OPEN
))
651 WARN("Clipboard not opened by calling task.\n");
652 SetLastError(ERROR_CLIPBOARD_NOT_OPEN
);
656 if (USER_Driver
.pGetClipboardData
)
657 USER_Driver
.pGetClipboardData(wFormat
, NULL
, &hData
);
659 TRACE("returning %p\n", hData
);
664 /**************************************************************************
665 * GetPriorityClipboardFormat (USER32.@)
667 INT WINAPI
GetPriorityClipboardFormat(UINT
*list
, INT nCount
)
673 if(CountClipboardFormats() == 0)
676 for (i
= 0; i
< nCount
; i
++)
677 if (IsClipboardFormatAvailable(list
[i
]))
684 /**************************************************************************
685 * GetClipboardSequenceNumber (USER32.@)
686 * Supported on Win2k/Win98
687 * MSDN: Windows clipboard code keeps a serial number for the clipboard
688 * for each window station. The number is incremented whenever the
689 * contents change or are emptied.
690 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
692 DWORD WINAPI
GetClipboardSequenceNumber(VOID
)
696 SERVER_START_REQ( set_clipboard_info
)
699 if (!wine_server_call_err( req
)) seqno
= reply
->seqno
;
703 TRACE("returning %lx\n", seqno
);