- improve WsControl error checking
[wine.git] / windows / clipboard.c
blobe9699d4569aa18ac0a7dbf3bf14ac5a5a34cda2c
1 /*
2 * WIN32 clipboard implementation
4 * Copyright 1994 Martin Ayotte
5 * 1996 Alex Korobka
6 * 1999 Noel Borthwick
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
23 * NOTES:
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)
32 #include "config.h"
33 #include "wine/port.h"
35 #include <stdlib.h>
36 #include <sys/types.h>
37 #include <fcntl.h>
38 #ifdef HAVE_UNISTD_H
39 # include <unistd.h>
40 #endif
41 #include <string.h>
43 #include "windef.h"
44 #include "winbase.h"
45 #include "wingdi.h"
46 #include "winuser.h"
47 #include "winerror.h"
48 #include "wine/winuser16.h"
49 #include "wine/winbase16.h"
50 #include "heap.h"
51 #include "user.h"
52 #include "win.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)
75 BOOL bRet = FALSE;
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");
88 else
90 bRet = TRUE;
93 SERVER_END_REQ;
95 return bRet;
99 /**************************************************************************
100 * CLIPBOARD_GetClipboardInfo
102 BOOL CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo)
104 BOOL bRet = FALSE;
106 SERVER_START_REQ( set_clipboard_info )
108 req->flags = 0;
110 if (wine_server_call_err( req ))
112 ERR("Failed to get clipboard owner.\n");
114 else
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;
122 bRet = TRUE;
125 SERVER_END_REQ;
127 return bRet;
131 /**************************************************************************
132 * CLIPBOARD_ReleaseOwner
134 BOOL CLIPBOARD_ReleaseOwner(void)
136 BOOL bRet = FALSE;
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");
146 else
148 bRet = TRUE;
151 SERVER_END_REQ;
153 return bRet;
157 /**************************************************************************
158 * CLIPBOARD_OpenClipboard
160 static BOOL CLIPBOARD_OpenClipboard(HWND hWnd)
162 BOOL bRet = FALSE;
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");
173 else
175 bRet = TRUE;
178 SERVER_END_REQ;
180 return bRet;
184 /**************************************************************************
185 * CLIPBOARD_CloseClipboard
187 static BOOL CLIPBOARD_CloseClipboard(void)
189 BOOL bRet = FALSE;
191 TRACE(" Changed=%d\n", bCBHasChanged);
193 SERVER_START_REQ( set_clipboard_info )
195 req->flags = SET_CB_CLOSE;
197 if (bCBHasChanged)
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");
207 else
209 bRet = TRUE;
212 SERVER_END_REQ;
214 return bRet;
218 /**************************************************************************
219 * WIN32 Clipboard implementation
220 **************************************************************************/
222 /**************************************************************************
223 * RegisterClipboardFormatA (USER32.@)
225 UINT WINAPI RegisterClipboardFormatA(LPCSTR FormatName)
227 UINT wFormatID = 0;
229 TRACE("%s\n", debugstr_a(FormatName));
231 if (USER_Driver.pRegisterClipboardFormat)
232 wFormatID = USER_Driver.pRegisterClipboardFormat(FormatName);
234 return wFormatID;
238 /**************************************************************************
239 * RegisterClipboardFormat (USER.145)
241 UINT16 WINAPI RegisterClipboardFormat16(LPCSTR FormatName)
243 UINT wFormatID = 0;
245 TRACE("%s\n", debugstr_a(FormatName));
247 if (USER_Driver.pRegisterClipboardFormat)
248 wFormatID = USER_Driver.pRegisterClipboardFormat(FormatName);
250 return wFormatID;
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 );
262 return ret;
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)
282 INT len = 0;
284 TRACE("%04x,%p,%d\n", wFormat, retStr, maxlen);
286 if (USER_Driver.pGetClipboardFormatName)
287 len = USER_Driver.pGetClipboardFormatName(wFormat, retStr, maxlen);
289 return len;
293 /**************************************************************************
294 * GetClipboardFormatNameW (USER32.@)
296 INT WINAPI GetClipboardFormatNameW(UINT wFormat, LPWSTR retStr, INT maxlen)
298 INT ret;
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 );
307 return ret;
311 /**************************************************************************
312 * OpenClipboard (USER32.@)
314 * Note: Netscape uses NULL hWnd to open the clipboard.
316 BOOL WINAPI OpenClipboard( HWND hWnd )
318 BOOL bRet;
320 TRACE("(%p)...\n", hWnd);
322 bRet = CLIPBOARD_OpenClipboard(hWnd);
324 TRACE(" returning %i\n", bRet);
326 return bRet;
330 /**************************************************************************
331 * CloseClipboard (USER.138)
333 BOOL16 WINAPI CloseClipboard16(void)
335 return CloseClipboard();
339 /**************************************************************************
340 * CloseClipboard (USER32.@)
342 BOOL WINAPI CloseClipboard(void)
344 BOOL bRet = FALSE;
346 TRACE("(%d)\n", bCBHasChanged);
348 if (CLIPBOARD_CloseClipboard())
350 if (bCBHasChanged)
352 HWND hWndViewer = GetClipboardViewer();
354 if (USER_Driver.pEndClipboardUpdate)
355 USER_Driver.pEndClipboardUpdate();
357 if (hWndViewer)
358 SendMessageW(hWndViewer, WM_DRAWCLIPBOARD, 0, 0);
360 bCBHasChanged = FALSE;
363 bRet = TRUE;
366 return bRet;
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;
387 TRACE("()\n");
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);
394 return FALSE;
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;
426 return 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)
436 HWND hWndOwner = 0;
437 CLIPBOARDINFO cbinfo;
439 if (CLIPBOARD_GetClipboardInfo(&cbinfo))
440 hWndOwner = cbinfo.hWndOwner;
442 TRACE(" hWndOwner(%p)\n", hWndOwner);
444 return hWndOwner;
448 /**************************************************************************
449 * GetOpenClipboardWindow (USER32.@)
451 HWND WINAPI GetOpenClipboardWindow(void)
453 HWND hWndOpen = 0;
454 CLIPBOARDINFO cbinfo;
456 if (CLIPBOARD_GetClipboardInfo(&cbinfo))
457 hWndOpen = cbinfo.hWndOpen;
459 TRACE(" hWndClipWindow(%p)\n", hWndOpen);
461 return hWndOpen;
465 /**************************************************************************
466 * SetClipboardViewer (USER32.@)
468 HWND WINAPI SetClipboardViewer( HWND hWnd )
470 HWND hwndPrev = 0;
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");
481 else
483 hwndPrev = reply->old_viewer;
486 SERVER_END_REQ;
488 TRACE("(%p): returning %p\n", hWnd, hwndPrev);
490 return hwndPrev;
494 /**************************************************************************
495 * GetClipboardViewer (USER32.@)
497 HWND WINAPI GetClipboardViewer(void)
499 HWND hWndViewer = 0;
500 CLIPBOARDINFO cbinfo;
502 if (CLIPBOARD_GetClipboardInfo(&cbinfo))
503 hWndViewer = cbinfo.hWndViewer;
505 TRACE(" hWndViewer=%p\n", hWndViewer);
507 return hWndViewer;
511 /**************************************************************************
512 * ChangeClipboardChain (USER32.@)
514 BOOL WINAPI ChangeClipboardChain(HWND hWnd, HWND hWndNext)
516 BOOL bRet = TRUE;
517 HWND hWndViewer = GetClipboardViewer();
519 if (hWndViewer)
521 if (WIN_GetFullHandle(hWnd) == hWndViewer)
522 SetClipboardViewer(WIN_GetFullHandle(hWndNext));
523 else
524 bRet = !SendMessageW(hWndViewer, WM_CHANGECBCHAIN, (WPARAM)hWnd, (LPARAM)hWndNext);
526 else
527 ERR("hWndViewer is lost\n");
529 return bRet;
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))
552 hResult = hData;
553 bCBHasChanged = TRUE;
556 return hResult;
560 /**************************************************************************
561 * SetClipboardData (USER32.@)
563 HANDLE WINAPI SetClipboardData(UINT wFormat, HANDLE hData)
565 CLIPBOARDINFO cbinfo;
566 HANDLE hResult = 0;
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))
578 hResult = hData;
579 bCBHasChanged = TRUE;
582 return hResult;
586 /**************************************************************************
587 * CountClipboardFormats (USER.143)
589 INT16 WINAPI CountClipboardFormats16(void)
591 return CountClipboardFormats();
595 /**************************************************************************
596 * CountClipboardFormats (USER32.@)
598 INT WINAPI CountClipboardFormats(void)
600 INT count = 0;
602 if (USER_Driver.pCountClipboardFormats)
603 count = USER_Driver.pCountClipboardFormats();
605 TRACE("returning %d\n", count);
606 return 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)
624 UINT wFmt = 0;
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);
634 return 0;
637 if (USER_Driver.pEnumClipboardFormats)
638 wFmt = USER_Driver.pEnumClipboardFormats(wFormat);
640 return wFmt;
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)
658 BOOL bret = FALSE;
660 if (USER_Driver.pIsClipboardFormatAvailable)
661 bret = USER_Driver.pIsClipboardFormatAvailable(wFormat);
663 TRACE("%04x, returning %d\n", wFormat, bret);
664 return bret;
668 /**************************************************************************
669 * GetClipboardData (USER.142)
671 HANDLE16 WINAPI GetClipboardData16(UINT16 wFormat)
673 HANDLE16 hData = 0;
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);
681 return 0;
684 if (USER_Driver.pGetClipboardData)
685 USER_Driver.pGetClipboardData(wFormat, &hData, NULL);
687 return hData;
691 /**************************************************************************
692 * GetClipboardData (USER32.@)
694 HANDLE WINAPI GetClipboardData(UINT wFormat)
696 HANDLE hData = 0;
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);
706 return 0;
709 if (USER_Driver.pGetClipboardData)
710 USER_Driver.pGetClipboardData(wFormat, NULL, &hData);
712 TRACE("returning %p\n", hData);
713 return hData;
717 /**************************************************************************
718 * GetPriorityClipboardFormat (USER32.@)
720 INT WINAPI GetPriorityClipboardFormat(UINT *list, INT nCount)
722 int i;
724 TRACE("()\n");
726 if(CountClipboardFormats() == 0)
727 return 0;
729 for (i = 0; i < nCount; i++)
730 if (IsClipboardFormatAvailable(list[i]))
731 return list[i];
733 return -1;
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");
752 return 0;
755 TRACE("returning %x\n", cbinfo.seqno);
756 return cbinfo.seqno;