Get rid of the WIN_SetRectangles export from user32.
[wine.git] / windows / clipboard.c
blobb434f6d508e0ce531fcd36dc11486b0f73e99275
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 <stdarg.h>
36 #include <stdlib.h>
37 #include <sys/types.h>
38 #include <fcntl.h>
39 #ifdef HAVE_UNISTD_H
40 # include <unistd.h>
41 #endif
42 #include <string.h>
44 #include "windef.h"
45 #include "winbase.h"
46 #include "wingdi.h"
47 #include "winuser.h"
48 #include "winerror.h"
49 #include "wine/winuser16.h"
50 #include "wine/winbase16.h"
51 #include "heap.h"
52 #include "user.h"
53 #include "win.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
63 typedef struct
65 HWND hWndOpen;
66 HWND hWndOwner;
67 HWND hWndViewer;
68 UINT seqno;
69 UINT flags;
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)
83 BOOL bRet = FALSE;
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);
96 else
98 bRet = TRUE;
101 SERVER_END_REQ;
103 return bRet;
107 /**************************************************************************
108 * CLIPBOARD_GetClipboardInfo
110 static BOOL CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo)
112 BOOL bRet = FALSE;
114 SERVER_START_REQ( set_clipboard_info )
116 req->flags = 0;
118 if (wine_server_call_err( req ))
120 ERR("Failed to get clipboard info\n");
122 else
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;
130 bRet = TRUE;
133 SERVER_END_REQ;
135 return bRet;
139 /**************************************************************************
140 * CLIPBOARD_ReleaseOwner
142 BOOL CLIPBOARD_ReleaseOwner(void)
144 BOOL bRet = FALSE;
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");
154 else
156 bRet = TRUE;
159 SERVER_END_REQ;
161 return bRet;
165 /**************************************************************************
166 * CLIPBOARD_OpenClipboard
168 static BOOL CLIPBOARD_OpenClipboard(HWND hWnd)
170 BOOL bRet = FALSE;
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 ))
178 bRet = TRUE;
180 SERVER_END_REQ;
182 return bRet;
186 /**************************************************************************
187 * CLIPBOARD_CloseClipboard
189 static BOOL CLIPBOARD_CloseClipboard(void)
191 BOOL bRet = FALSE;
193 TRACE(" Changed=%d\n", bCBHasChanged);
195 SERVER_START_REQ( set_clipboard_info )
197 req->flags = SET_CB_CLOSE;
199 if (bCBHasChanged)
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");
209 else
211 bRet = TRUE;
214 SERVER_END_REQ;
216 return bRet;
220 /**************************************************************************
221 * WIN32 Clipboard implementation
222 **************************************************************************/
224 /**************************************************************************
225 * RegisterClipboardFormatA (USER32.@)
227 UINT WINAPI RegisterClipboardFormatA(LPCSTR FormatName)
229 UINT wFormatID = 0;
231 TRACE("%s\n", debugstr_a(FormatName));
233 if (USER_Driver.pRegisterClipboardFormat)
234 wFormatID = USER_Driver.pRegisterClipboardFormat(FormatName);
236 return wFormatID;
240 /**************************************************************************
241 * RegisterClipboardFormat (USER.145)
243 UINT16 WINAPI RegisterClipboardFormat16(LPCSTR FormatName)
245 UINT wFormatID = 0;
247 TRACE("%s\n", debugstr_a(FormatName));
249 if (USER_Driver.pRegisterClipboardFormat)
250 wFormatID = USER_Driver.pRegisterClipboardFormat(FormatName);
252 return wFormatID;
256 /**************************************************************************
257 * RegisterClipboardFormatW (USER32.@)
259 UINT WINAPI RegisterClipboardFormatW(LPCWSTR formatName)
261 LPSTR aFormat = HEAP_strdupWtoA( GetProcessHeap(), 0, formatName );
262 UINT ret = RegisterClipboardFormatA( aFormat );
263 HeapFree( GetProcessHeap(), 0, aFormat );
264 return ret;
268 /**************************************************************************
269 * GetClipboardFormatName (USER.146)
271 INT16 WINAPI GetClipboardFormatName16(UINT16 wFormat, LPSTR retStr, INT16 maxlen)
273 TRACE("%04x,%p,%d\n", wFormat, retStr, maxlen);
275 return GetClipboardFormatNameA(wFormat, retStr, maxlen);
279 /**************************************************************************
280 * GetClipboardFormatNameA (USER32.@)
282 INT WINAPI GetClipboardFormatNameA(UINT wFormat, LPSTR retStr, INT maxlen)
284 INT len = 0;
286 TRACE("%04x,%p,%d\n", wFormat, retStr, maxlen);
288 if (USER_Driver.pGetClipboardFormatName)
289 len = USER_Driver.pGetClipboardFormatName(wFormat, retStr, maxlen);
291 return len;
295 /**************************************************************************
296 * GetClipboardFormatNameW (USER32.@)
298 INT WINAPI GetClipboardFormatNameW(UINT wFormat, LPWSTR retStr, INT maxlen)
300 INT ret;
301 LPSTR p = HeapAlloc( GetProcessHeap(), 0, maxlen );
302 if(p == NULL) return 0; /* FIXME: is this the correct failure value? */
304 ret = GetClipboardFormatNameA( wFormat, p, maxlen );
306 if (maxlen > 0 && !MultiByteToWideChar( CP_ACP, 0, p, -1, retStr, maxlen ))
307 retStr[maxlen-1] = 0;
308 HeapFree( GetProcessHeap(), 0, p );
309 return ret;
313 /**************************************************************************
314 * OpenClipboard (USER32.@)
316 * Note: Netscape uses NULL hWnd to open the clipboard.
318 BOOL WINAPI OpenClipboard( HWND hWnd )
320 BOOL bRet;
322 TRACE("(%p)...\n", hWnd);
324 bRet = CLIPBOARD_OpenClipboard(hWnd);
326 TRACE(" returning %i\n", bRet);
328 return bRet;
332 /**************************************************************************
333 * CloseClipboard (USER.138)
335 BOOL16 WINAPI CloseClipboard16(void)
337 return CloseClipboard();
341 /**************************************************************************
342 * CloseClipboard (USER32.@)
344 BOOL WINAPI CloseClipboard(void)
346 BOOL bRet = FALSE;
348 TRACE("(%d)\n", bCBHasChanged);
350 if (CLIPBOARD_CloseClipboard())
352 if (bCBHasChanged)
354 HWND hWndViewer = GetClipboardViewer();
356 if (USER_Driver.pEndClipboardUpdate)
357 USER_Driver.pEndClipboardUpdate();
359 if (hWndViewer)
360 SendMessageW(hWndViewer, WM_DRAWCLIPBOARD, 0, 0);
362 bCBHasChanged = FALSE;
365 bRet = TRUE;
368 return bRet;
372 /**************************************************************************
373 * EmptyClipboard (USER.139)
375 BOOL16 WINAPI EmptyClipboard16(void)
377 return EmptyClipboard();
381 /**************************************************************************
382 * EmptyClipboard (USER32.@)
383 * Empties and acquires ownership of the clipboard
385 BOOL WINAPI EmptyClipboard(void)
387 CLIPBOARDINFO cbinfo;
389 TRACE("()\n");
391 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
392 ~cbinfo.flags & CB_OPEN)
394 WARN("Clipboard not opened by calling task!\n");
395 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
396 return FALSE;
399 /* Destroy private objects */
400 if (cbinfo.hWndOwner)
401 SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
403 /* Tell the driver to acquire the selection. The current owner
404 * will be signaled to delete it's own cache. */
406 /* Assign ownership of the clipboard to the current client. We do
407 * this before acquiring the selection so that when we do acquire the
408 * selection and the selection loser gets notified, it can check if
409 * it has lost the Wine clipboard ownership. If it did then it knows
410 * that a WM_DESTORYCLIPBOARD has already been sent. Otherwise it
411 * lost the selection to a X app and it should send the
412 * WM_DESTROYCLIPBOARD itself. */
413 CLIPBOARD_SetClipboardOwner(cbinfo.hWndOpen);
415 /* Acquire the selection. This will notify the previous owner
416 * to clear it's cache. */
417 if (USER_Driver.pAcquireClipboard)
418 USER_Driver.pAcquireClipboard(cbinfo.hWndOpen);
420 /* Empty the local cache */
421 if (USER_Driver.pEmptyClipboard)
422 USER_Driver.pEmptyClipboard(FALSE);
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;
438 SERVER_START_REQ( set_clipboard_info )
440 req->flags = 0;
441 if (!wine_server_call_err( req )) hWndOwner = reply->old_owner;
443 SERVER_END_REQ;
445 TRACE(" hWndOwner(%p)\n", hWndOwner);
447 return hWndOwner;
451 /**************************************************************************
452 * GetOpenClipboardWindow (USER32.@)
454 HWND WINAPI GetOpenClipboardWindow(void)
456 HWND hWndOpen = 0;
458 SERVER_START_REQ( set_clipboard_info )
460 req->flags = 0;
461 if (!wine_server_call_err( req )) hWndOpen = reply->old_clipboard;
463 SERVER_END_REQ;
465 TRACE(" hWndClipWindow(%p)\n", hWndOpen);
467 return hWndOpen;
471 /**************************************************************************
472 * SetClipboardViewer (USER32.@)
474 HWND WINAPI SetClipboardViewer( HWND hWnd )
476 HWND hwndPrev = 0;
478 SERVER_START_REQ( set_clipboard_info )
480 req->flags = SET_CB_VIEWER;
481 req->viewer = WIN_GetFullHandle(hWnd);
483 if (wine_server_call_err( req ))
485 ERR("Failed to set clipboard.\n");
487 else
489 hwndPrev = reply->old_viewer;
492 SERVER_END_REQ;
494 TRACE("(%p): returning %p\n", hWnd, hwndPrev);
496 return hwndPrev;
500 /**************************************************************************
501 * GetClipboardViewer (USER32.@)
503 HWND WINAPI GetClipboardViewer(void)
505 HWND hWndViewer = 0;
507 SERVER_START_REQ( set_clipboard_info )
509 req->flags = 0;
510 if (!wine_server_call_err( req )) hWndViewer = reply->old_viewer;
512 SERVER_END_REQ;
514 TRACE(" hWndViewer=%p\n", hWndViewer);
516 return hWndViewer;
520 /**************************************************************************
521 * ChangeClipboardChain (USER32.@)
523 BOOL WINAPI ChangeClipboardChain(HWND hWnd, HWND hWndNext)
525 BOOL bRet = TRUE;
526 HWND hWndViewer = GetClipboardViewer();
528 if (hWndViewer)
530 if (WIN_GetFullHandle(hWnd) == hWndViewer)
531 SetClipboardViewer(WIN_GetFullHandle(hWndNext));
532 else
533 bRet = !SendMessageW(hWndViewer, WM_CHANGECBCHAIN, (WPARAM)hWnd, (LPARAM)hWndNext);
535 else
536 ERR("hWndViewer is lost\n");
538 return bRet;
542 /**************************************************************************
543 * SetClipboardData (USER.141)
545 HANDLE16 WINAPI SetClipboardData16(UINT16 wFormat, HANDLE16 hData)
547 CLIPBOARDINFO cbinfo;
548 HANDLE16 hResult = 0;
550 TRACE("(%04X, %04x) !\n", wFormat, hData);
552 /* If it's not owned, data can only be set if the format doesn't exists
553 and its rendering is not delayed */
554 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
555 (!(cbinfo.flags & CB_OWNER) && !hData))
557 WARN("Clipboard not owned by calling task. Operation failed.\n");
558 return 0;
561 if (USER_Driver.pSetClipboardData &&
562 USER_Driver.pSetClipboardData(wFormat, hData, 0, cbinfo.flags & CB_OWNER))
564 hResult = hData;
565 bCBHasChanged = TRUE;
568 return hResult;
572 /**************************************************************************
573 * SetClipboardData (USER32.@)
575 HANDLE WINAPI SetClipboardData(UINT wFormat, HANDLE hData)
577 CLIPBOARDINFO cbinfo;
578 HANDLE hResult = 0;
580 TRACE("(%04X, %p) !\n", wFormat, hData);
582 /* If it's not owned, data can only be set if the format isn't
583 available and its rendering is not delayed */
584 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
585 (!(cbinfo.flags & CB_OWNER) && !hData))
587 WARN("Clipboard not owned by calling task. Operation failed.\n");
588 return 0;
591 if (USER_Driver.pSetClipboardData &&
592 USER_Driver.pSetClipboardData(wFormat, 0, hData, cbinfo.flags & CB_OWNER))
594 hResult = hData;
595 bCBHasChanged = TRUE;
598 return hResult;
602 /**************************************************************************
603 * CountClipboardFormats (USER.143)
605 INT16 WINAPI CountClipboardFormats16(void)
607 return CountClipboardFormats();
611 /**************************************************************************
612 * CountClipboardFormats (USER32.@)
614 INT WINAPI CountClipboardFormats(void)
616 INT count = 0;
618 if (USER_Driver.pCountClipboardFormats)
619 count = USER_Driver.pCountClipboardFormats();
621 TRACE("returning %d\n", count);
622 return count;
626 /**************************************************************************
627 * EnumClipboardFormats (USER.144)
629 UINT16 WINAPI EnumClipboardFormats16(UINT16 wFormat)
631 return EnumClipboardFormats(wFormat);
635 /**************************************************************************
636 * EnumClipboardFormats (USER32.@)
638 UINT WINAPI EnumClipboardFormats(UINT wFormat)
640 UINT wFmt = 0;
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);
650 return 0;
653 if (USER_Driver.pEnumClipboardFormats)
654 wFmt = USER_Driver.pEnumClipboardFormats(wFormat);
656 return wFmt;
660 /**************************************************************************
661 * IsClipboardFormatAvailable (USER.193)
663 BOOL16 WINAPI IsClipboardFormatAvailable16(UINT16 wFormat)
665 return IsClipboardFormatAvailable(wFormat);
669 /**************************************************************************
670 * IsClipboardFormatAvailable (USER32.@)
672 BOOL WINAPI IsClipboardFormatAvailable(UINT wFormat)
674 BOOL bret = FALSE;
676 if (USER_Driver.pIsClipboardFormatAvailable)
677 bret = USER_Driver.pIsClipboardFormatAvailable(wFormat);
679 TRACE("%04x, returning %d\n", wFormat, bret);
680 return bret;
684 /**************************************************************************
685 * GetClipboardData (USER.142)
687 HANDLE16 WINAPI GetClipboardData16(UINT16 wFormat)
689 HANDLE16 hData = 0;
690 CLIPBOARDINFO cbinfo;
692 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
693 (~cbinfo.flags & CB_OPEN))
695 WARN("Clipboard not opened by calling task.\n");
696 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
697 return 0;
700 if (USER_Driver.pGetClipboardData)
701 USER_Driver.pGetClipboardData(wFormat, &hData, NULL);
703 return hData;
707 /**************************************************************************
708 * GetClipboardData (USER32.@)
710 HANDLE WINAPI GetClipboardData(UINT wFormat)
712 HANDLE hData = 0;
713 CLIPBOARDINFO cbinfo;
715 TRACE("%04x\n", wFormat);
717 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
718 (~cbinfo.flags & CB_OPEN))
720 WARN("Clipboard not opened by calling task.\n");
721 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
722 return 0;
725 if (USER_Driver.pGetClipboardData)
726 USER_Driver.pGetClipboardData(wFormat, NULL, &hData);
728 TRACE("returning %p\n", hData);
729 return hData;
733 /**************************************************************************
734 * GetPriorityClipboardFormat (USER32.@)
736 INT WINAPI GetPriorityClipboardFormat(UINT *list, INT nCount)
738 int i;
740 TRACE("()\n");
742 if(CountClipboardFormats() == 0)
743 return 0;
745 for (i = 0; i < nCount; i++)
746 if (IsClipboardFormatAvailable(list[i]))
747 return list[i];
749 return -1;
753 /**************************************************************************
754 * GetClipboardSequenceNumber (USER32.@)
755 * Supported on Win2k/Win98
756 * MSDN: Windows clipboard code keeps a serial number for the clipboard
757 * for each window station. The number is incremented whenever the
758 * contents change or are emptied.
759 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
761 DWORD WINAPI GetClipboardSequenceNumber(VOID)
763 DWORD seqno = 0;
765 SERVER_START_REQ( set_clipboard_info )
767 req->flags = 0;
768 if (!wine_server_call_err( req )) seqno = reply->seqno;
770 SERVER_END_REQ;
772 TRACE("returning %lx\n", seqno);
773 return seqno;