Added missing GDI_ReleaseObj() to GetDCBrushColor & GetDCPenColor.
[wine.git] / windows / clipboard.c
blobd1bbce444c07f389746d2f195738677cb7c91e7a
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"
54 #include "clipboard.h"
56 #include "wine/debug.h"
57 #include "wine/unicode.h"
58 #include "wine/server.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(clipboard);
62 #define CF_REGFORMATBASE 0xC000
66 * Indicates if data has changed since open.
68 static BOOL bCBHasChanged = FALSE;
71 /**************************************************************************
72 * CLIPBOARD_SetClipboardOwner
74 BOOL CLIPBOARD_SetClipboardOwner(HWND hWnd)
76 BOOL bRet = FALSE;
78 TRACE(" hWnd(%p)\n", hWnd);
80 SERVER_START_REQ( set_clipboard_info )
82 req->flags = SET_CB_OWNER;
83 req->owner = WIN_GetFullHandle( hWnd );
85 if (wine_server_call_err( req ))
87 ERR("Failed to set clipboard.\n");
89 else
91 bRet = TRUE;
94 SERVER_END_REQ;
96 return bRet;
100 /**************************************************************************
101 * CLIPBOARD_GetClipboardInfo
103 BOOL CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo)
105 BOOL bRet = FALSE;
107 SERVER_START_REQ( set_clipboard_info )
109 req->flags = 0;
111 if (wine_server_call_err( req ))
113 ERR("Failed to get clipboard owner.\n");
115 else
117 cbInfo->hWndOpen = reply->old_clipboard;
118 cbInfo->hWndOwner = reply->old_owner;
119 cbInfo->hWndViewer = reply->old_viewer;
120 cbInfo->seqno = reply->seqno;
121 cbInfo->flags = reply->flags;
123 bRet = TRUE;
126 SERVER_END_REQ;
128 return bRet;
132 /**************************************************************************
133 * CLIPBOARD_ReleaseOwner
135 BOOL CLIPBOARD_ReleaseOwner(void)
137 BOOL bRet = FALSE;
139 SERVER_START_REQ( set_clipboard_info )
141 req->flags = SET_CB_RELOWNER | SET_CB_SEQNO;
143 if (wine_server_call_err( req ))
145 ERR("Failed to set clipboard.\n");
147 else
149 bRet = TRUE;
152 SERVER_END_REQ;
154 return bRet;
158 /**************************************************************************
159 * CLIPBOARD_OpenClipboard
161 static BOOL CLIPBOARD_OpenClipboard(HWND hWnd)
163 BOOL bRet = FALSE;
165 SERVER_START_REQ( set_clipboard_info )
167 req->flags = SET_CB_OPEN;
168 req->clipboard = WIN_GetFullHandle( hWnd );
170 if (wine_server_call_err( req ))
172 ERR("Failed to set clipboard.\n");
174 else
176 bRet = TRUE;
179 SERVER_END_REQ;
181 return bRet;
185 /**************************************************************************
186 * CLIPBOARD_CloseClipboard
188 static BOOL CLIPBOARD_CloseClipboard(void)
190 BOOL bRet = FALSE;
192 TRACE(" Changed=%d\n", bCBHasChanged);
194 SERVER_START_REQ( set_clipboard_info )
196 req->flags = SET_CB_CLOSE;
198 if (bCBHasChanged)
200 req->flags |= SET_CB_SEQNO;
201 TRACE("Clipboard data changed\n");
204 if (wine_server_call_err( req ))
206 ERR("Failed to set clipboard.\n");
208 else
210 bRet = TRUE;
213 SERVER_END_REQ;
215 return bRet;
219 /**************************************************************************
220 * WIN32 Clipboard implementation
221 **************************************************************************/
223 /**************************************************************************
224 * RegisterClipboardFormatA (USER32.@)
226 UINT WINAPI RegisterClipboardFormatA(LPCSTR FormatName)
228 UINT wFormatID = 0;
230 TRACE("%s\n", debugstr_a(FormatName));
232 if (USER_Driver.pRegisterClipboardFormat)
233 wFormatID = USER_Driver.pRegisterClipboardFormat(FormatName);
235 return wFormatID;
239 /**************************************************************************
240 * RegisterClipboardFormat (USER.145)
242 UINT16 WINAPI RegisterClipboardFormat16(LPCSTR FormatName)
244 UINT wFormatID = 0;
246 TRACE("%s\n", debugstr_a(FormatName));
248 if (USER_Driver.pRegisterClipboardFormat)
249 wFormatID = USER_Driver.pRegisterClipboardFormat(FormatName);
251 return wFormatID;
255 /**************************************************************************
256 * RegisterClipboardFormatW (USER32.@)
258 UINT WINAPI RegisterClipboardFormatW(LPCWSTR formatName)
260 LPSTR aFormat = HEAP_strdupWtoA( GetProcessHeap(), 0, formatName );
261 UINT ret = RegisterClipboardFormatA( aFormat );
262 HeapFree( GetProcessHeap(), 0, aFormat );
263 return ret;
267 /**************************************************************************
268 * GetClipboardFormatName (USER.146)
270 INT16 WINAPI GetClipboardFormatName16(UINT16 wFormat, LPSTR retStr, INT16 maxlen)
272 TRACE("%04x,%p,%d\n", wFormat, retStr, maxlen);
274 return GetClipboardFormatNameA(wFormat, retStr, maxlen);
278 /**************************************************************************
279 * GetClipboardFormatNameA (USER32.@)
281 INT WINAPI GetClipboardFormatNameA(UINT wFormat, LPSTR retStr, INT maxlen)
283 INT len = 0;
285 TRACE("%04x,%p,%d\n", wFormat, retStr, maxlen);
287 if (USER_Driver.pGetClipboardFormatName)
288 len = USER_Driver.pGetClipboardFormatName(wFormat, retStr, maxlen);
290 return len;
294 /**************************************************************************
295 * GetClipboardFormatNameW (USER32.@)
297 INT WINAPI GetClipboardFormatNameW(UINT wFormat, LPWSTR retStr, INT maxlen)
299 INT ret;
300 LPSTR p = HeapAlloc( GetProcessHeap(), 0, maxlen );
301 if(p == NULL) return 0; /* FIXME: is this the correct failure value? */
303 ret = GetClipboardFormatNameA( wFormat, p, maxlen );
305 if (maxlen > 0 && !MultiByteToWideChar( CP_ACP, 0, p, -1, retStr, maxlen ))
306 retStr[maxlen-1] = 0;
307 HeapFree( GetProcessHeap(), 0, p );
308 return ret;
312 /**************************************************************************
313 * OpenClipboard (USER32.@)
315 * Note: Netscape uses NULL hWnd to open the clipboard.
317 BOOL WINAPI OpenClipboard( HWND hWnd )
319 BOOL bRet;
321 TRACE("(%p)...\n", hWnd);
323 bRet = CLIPBOARD_OpenClipboard(hWnd);
325 TRACE(" returning %i\n", bRet);
327 return bRet;
331 /**************************************************************************
332 * CloseClipboard (USER.138)
334 BOOL16 WINAPI CloseClipboard16(void)
336 return CloseClipboard();
340 /**************************************************************************
341 * CloseClipboard (USER32.@)
343 BOOL WINAPI CloseClipboard(void)
345 BOOL bRet = FALSE;
347 TRACE("(%d)\n", bCBHasChanged);
349 if (CLIPBOARD_CloseClipboard())
351 if (bCBHasChanged)
353 HWND hWndViewer = GetClipboardViewer();
355 if (USER_Driver.pEndClipboardUpdate)
356 USER_Driver.pEndClipboardUpdate();
358 if (hWndViewer)
359 SendMessageW(hWndViewer, WM_DRAWCLIPBOARD, 0, 0);
361 bCBHasChanged = FALSE;
364 bRet = TRUE;
367 return bRet;
371 /**************************************************************************
372 * EmptyClipboard (USER.139)
374 BOOL16 WINAPI EmptyClipboard16(void)
376 return EmptyClipboard();
380 /**************************************************************************
381 * EmptyClipboard (USER32.@)
382 * Empties and acquires ownership of the clipboard
384 BOOL WINAPI EmptyClipboard(void)
386 CLIPBOARDINFO cbinfo;
388 TRACE("()\n");
390 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
391 ~cbinfo.flags & CB_OPEN)
393 WARN("Clipboard not opened by calling task!\n");
394 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
395 return FALSE;
398 /* Destroy private objects */
399 if (cbinfo.hWndOwner)
400 SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
402 /* Tell the driver to acquire the selection. The current owner
403 * will be signaled to delete it's own cache. */
404 if (~cbinfo.flags & CB_OWNER)
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);
421 /* Empty the local cache */
422 if (USER_Driver.pEmptyClipboard)
423 USER_Driver.pEmptyClipboard();
425 bCBHasChanged = TRUE;
427 return TRUE;
431 /**************************************************************************
432 * GetClipboardOwner (USER32.@)
433 * FIXME: Can't return the owner if the clipboard is owned by an external X-app
435 HWND WINAPI GetClipboardOwner(void)
437 HWND hWndOwner = 0;
438 CLIPBOARDINFO cbinfo;
440 if (CLIPBOARD_GetClipboardInfo(&cbinfo))
441 hWndOwner = cbinfo.hWndOwner;
443 TRACE(" hWndOwner(%p)\n", hWndOwner);
445 return hWndOwner;
449 /**************************************************************************
450 * GetOpenClipboardWindow (USER32.@)
452 HWND WINAPI GetOpenClipboardWindow(void)
454 HWND hWndOpen = 0;
455 CLIPBOARDINFO cbinfo;
457 if (CLIPBOARD_GetClipboardInfo(&cbinfo))
458 hWndOpen = cbinfo.hWndOpen;
460 TRACE(" hWndClipWindow(%p)\n", hWndOpen);
462 return hWndOpen;
466 /**************************************************************************
467 * SetClipboardViewer (USER32.@)
469 HWND WINAPI SetClipboardViewer( HWND hWnd )
471 HWND hwndPrev = 0;
473 SERVER_START_REQ( set_clipboard_info )
475 req->flags = SET_CB_VIEWER;
476 req->viewer = WIN_GetFullHandle(hWnd);
478 if (wine_server_call_err( req ))
480 ERR("Failed to set clipboard.\n");
482 else
484 hwndPrev = reply->old_viewer;
487 SERVER_END_REQ;
489 TRACE("(%p): returning %p\n", hWnd, hwndPrev);
491 return hwndPrev;
495 /**************************************************************************
496 * GetClipboardViewer (USER32.@)
498 HWND WINAPI GetClipboardViewer(void)
500 HWND hWndViewer = 0;
501 CLIPBOARDINFO cbinfo;
503 if (CLIPBOARD_GetClipboardInfo(&cbinfo))
504 hWndViewer = cbinfo.hWndViewer;
506 TRACE(" hWndViewer=%p\n", hWndViewer);
508 return hWndViewer;
512 /**************************************************************************
513 * ChangeClipboardChain (USER32.@)
515 BOOL WINAPI ChangeClipboardChain(HWND hWnd, HWND hWndNext)
517 BOOL bRet = TRUE;
518 HWND hWndViewer = GetClipboardViewer();
520 if (hWndViewer)
522 if (WIN_GetFullHandle(hWnd) == hWndViewer)
523 SetClipboardViewer(WIN_GetFullHandle(hWndNext));
524 else
525 bRet = !SendMessageW(hWndViewer, WM_CHANGECBCHAIN, (WPARAM)hWnd, (LPARAM)hWndNext);
527 else
528 ERR("hWndViewer is lost\n");
530 return bRet;
534 /**************************************************************************
535 * SetClipboardData (USER.141)
537 HANDLE16 WINAPI SetClipboardData16(UINT16 wFormat, HANDLE16 hData)
539 CLIPBOARDINFO cbinfo;
540 HANDLE16 hResult = 0;
542 TRACE("(%04X, %04x) !\n", wFormat, hData);
544 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
545 (~cbinfo.flags & CB_OPEN) ||
546 (~cbinfo.flags & CB_OWNER))
548 WARN("Clipboard not opened by calling task!\n");
550 else if (USER_Driver.pSetClipboardData &&
551 USER_Driver.pSetClipboardData(wFormat, hData, 0))
553 hResult = hData;
554 bCBHasChanged = TRUE;
557 return hResult;
561 /**************************************************************************
562 * SetClipboardData (USER32.@)
564 HANDLE WINAPI SetClipboardData(UINT wFormat, HANDLE hData)
566 CLIPBOARDINFO cbinfo;
567 HANDLE hResult = 0;
569 TRACE("(%04X, %p) !\n", wFormat, hData);
571 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
572 (~cbinfo.flags & CB_OWNER))
574 WARN("Clipboard not owned by calling task!\n");
576 else if (USER_Driver.pSetClipboardData &&
577 USER_Driver.pSetClipboardData(wFormat, 0, hData))
579 hResult = hData;
580 bCBHasChanged = TRUE;
583 return hResult;
587 /**************************************************************************
588 * CountClipboardFormats (USER.143)
590 INT16 WINAPI CountClipboardFormats16(void)
592 return CountClipboardFormats();
596 /**************************************************************************
597 * CountClipboardFormats (USER32.@)
599 INT WINAPI CountClipboardFormats(void)
601 INT count = 0;
603 if (USER_Driver.pCountClipboardFormats)
604 count = USER_Driver.pCountClipboardFormats();
606 TRACE("returning %d\n", count);
607 return count;
611 /**************************************************************************
612 * EnumClipboardFormats (USER.144)
614 UINT16 WINAPI EnumClipboardFormats16(UINT16 wFormat)
616 return EnumClipboardFormats(wFormat);
620 /**************************************************************************
621 * EnumClipboardFormats (USER32.@)
623 UINT WINAPI EnumClipboardFormats(UINT wFormat)
625 UINT wFmt = 0;
626 CLIPBOARDINFO cbinfo;
628 TRACE("(%04X)\n", wFormat);
630 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
631 (~cbinfo.flags & CB_OPEN))
633 WARN("Clipboard not opened by calling task.\n");
634 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
635 return 0;
638 if (USER_Driver.pEnumClipboardFormats)
639 wFmt = USER_Driver.pEnumClipboardFormats(wFormat);
641 return wFmt;
645 /**************************************************************************
646 * IsClipboardFormatAvailable (USER.193)
648 BOOL16 WINAPI IsClipboardFormatAvailable16(UINT16 wFormat)
650 return IsClipboardFormatAvailable(wFormat);
654 /**************************************************************************
655 * IsClipboardFormatAvailable (USER32.@)
657 BOOL WINAPI IsClipboardFormatAvailable(UINT wFormat)
659 BOOL bret = FALSE;
661 if (USER_Driver.pIsClipboardFormatAvailable)
662 bret = USER_Driver.pIsClipboardFormatAvailable(wFormat);
664 TRACE("%04x, returning %d\n", wFormat, bret);
665 return bret;
669 /**************************************************************************
670 * GetClipboardData (USER.142)
672 HANDLE16 WINAPI GetClipboardData16(UINT16 wFormat)
674 HANDLE16 hData = 0;
675 CLIPBOARDINFO cbinfo;
677 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
678 (~cbinfo.flags & CB_OPEN))
680 WARN("Clipboard not opened by calling task.\n");
681 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
682 return 0;
685 if (USER_Driver.pGetClipboardData)
686 USER_Driver.pGetClipboardData(wFormat, &hData, NULL);
688 return hData;
692 /**************************************************************************
693 * GetClipboardData (USER32.@)
695 HANDLE WINAPI GetClipboardData(UINT wFormat)
697 HANDLE hData = 0;
698 CLIPBOARDINFO cbinfo;
700 TRACE("%04x\n", wFormat);
702 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
703 (~cbinfo.flags & CB_OPEN))
705 WARN("Clipboard not opened by calling task.\n");
706 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
707 return 0;
710 if (USER_Driver.pGetClipboardData)
711 USER_Driver.pGetClipboardData(wFormat, NULL, &hData);
713 TRACE("returning %p\n", hData);
714 return hData;
718 /**************************************************************************
719 * GetPriorityClipboardFormat (USER32.@)
721 INT WINAPI GetPriorityClipboardFormat(UINT *list, INT nCount)
723 int i;
725 TRACE("()\n");
727 if(CountClipboardFormats() == 0)
728 return 0;
730 for (i = 0; i < nCount; i++)
731 if (IsClipboardFormatAvailable(list[i]))
732 return list[i];
734 return -1;
738 /**************************************************************************
739 * GetClipboardSequenceNumber (USER32.@)
740 * Supported on Win2k/Win98
741 * MSDN: Windows clipboard code keeps a serial number for the clipboard
742 * for each window station. The number is incremented whenever the
743 * contents change or are emptied.
744 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
746 DWORD WINAPI GetClipboardSequenceNumber(VOID)
748 CLIPBOARDINFO cbinfo;
750 if (!CLIPBOARD_GetClipboardInfo(&cbinfo))
752 ERR("Failed to get clipboard information.\n");
753 return 0;
756 TRACE("returning %x\n", cbinfo.seqno);
757 return cbinfo.seqno;