Split OpenFile implementation in separate 16- and 32-bit versions, and
[wine.git] / windows / clipboard.c
blobfda151e1b31d3fcee72c5f1dd2f2080af3f7a4d5
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.\n");
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 owner.\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_err( req ))
179 ERR("Failed to set clipboard.\n");
181 else
183 bRet = TRUE;
186 SERVER_END_REQ;
188 return bRet;
192 /**************************************************************************
193 * CLIPBOARD_CloseClipboard
195 static BOOL CLIPBOARD_CloseClipboard(void)
197 BOOL bRet = FALSE;
199 TRACE(" Changed=%d\n", bCBHasChanged);
201 SERVER_START_REQ( set_clipboard_info )
203 req->flags = SET_CB_CLOSE;
205 if (bCBHasChanged)
207 req->flags |= SET_CB_SEQNO;
208 TRACE("Clipboard data changed\n");
211 if (wine_server_call_err( req ))
213 ERR("Failed to set clipboard.\n");
215 else
217 bRet = TRUE;
220 SERVER_END_REQ;
222 return bRet;
226 /**************************************************************************
227 * WIN32 Clipboard implementation
228 **************************************************************************/
230 /**************************************************************************
231 * RegisterClipboardFormatA (USER32.@)
233 UINT WINAPI RegisterClipboardFormatA(LPCSTR FormatName)
235 UINT wFormatID = 0;
237 TRACE("%s\n", debugstr_a(FormatName));
239 if (USER_Driver.pRegisterClipboardFormat)
240 wFormatID = USER_Driver.pRegisterClipboardFormat(FormatName);
242 return wFormatID;
246 /**************************************************************************
247 * RegisterClipboardFormat (USER.145)
249 UINT16 WINAPI RegisterClipboardFormat16(LPCSTR FormatName)
251 UINT wFormatID = 0;
253 TRACE("%s\n", debugstr_a(FormatName));
255 if (USER_Driver.pRegisterClipboardFormat)
256 wFormatID = USER_Driver.pRegisterClipboardFormat(FormatName);
258 return wFormatID;
262 /**************************************************************************
263 * RegisterClipboardFormatW (USER32.@)
265 UINT WINAPI RegisterClipboardFormatW(LPCWSTR formatName)
267 LPSTR aFormat = HEAP_strdupWtoA( GetProcessHeap(), 0, formatName );
268 UINT ret = RegisterClipboardFormatA( aFormat );
269 HeapFree( GetProcessHeap(), 0, aFormat );
270 return ret;
274 /**************************************************************************
275 * GetClipboardFormatName (USER.146)
277 INT16 WINAPI GetClipboardFormatName16(UINT16 wFormat, LPSTR retStr, INT16 maxlen)
279 TRACE("%04x,%p,%d\n", wFormat, retStr, maxlen);
281 return GetClipboardFormatNameA(wFormat, retStr, maxlen);
285 /**************************************************************************
286 * GetClipboardFormatNameA (USER32.@)
288 INT WINAPI GetClipboardFormatNameA(UINT wFormat, LPSTR retStr, INT maxlen)
290 INT len = 0;
292 TRACE("%04x,%p,%d\n", wFormat, retStr, maxlen);
294 if (USER_Driver.pGetClipboardFormatName)
295 len = USER_Driver.pGetClipboardFormatName(wFormat, retStr, maxlen);
297 return len;
301 /**************************************************************************
302 * GetClipboardFormatNameW (USER32.@)
304 INT WINAPI GetClipboardFormatNameW(UINT wFormat, LPWSTR retStr, INT maxlen)
306 INT ret;
307 LPSTR p = HeapAlloc( GetProcessHeap(), 0, maxlen );
308 if(p == NULL) return 0; /* FIXME: is this the correct failure value? */
310 ret = GetClipboardFormatNameA( wFormat, p, maxlen );
312 if (maxlen > 0 && !MultiByteToWideChar( CP_ACP, 0, p, -1, retStr, maxlen ))
313 retStr[maxlen-1] = 0;
314 HeapFree( GetProcessHeap(), 0, p );
315 return ret;
319 /**************************************************************************
320 * OpenClipboard (USER32.@)
322 * Note: Netscape uses NULL hWnd to open the clipboard.
324 BOOL WINAPI OpenClipboard( HWND hWnd )
326 BOOL bRet;
328 TRACE("(%p)...\n", hWnd);
330 bRet = CLIPBOARD_OpenClipboard(hWnd);
332 TRACE(" returning %i\n", bRet);
334 return bRet;
338 /**************************************************************************
339 * CloseClipboard (USER.138)
341 BOOL16 WINAPI CloseClipboard16(void)
343 return CloseClipboard();
347 /**************************************************************************
348 * CloseClipboard (USER32.@)
350 BOOL WINAPI CloseClipboard(void)
352 BOOL bRet = FALSE;
354 TRACE("(%d)\n", bCBHasChanged);
356 if (CLIPBOARD_CloseClipboard())
358 if (bCBHasChanged)
360 HWND hWndViewer = GetClipboardViewer();
362 if (USER_Driver.pEndClipboardUpdate)
363 USER_Driver.pEndClipboardUpdate();
365 if (hWndViewer)
366 SendMessageW(hWndViewer, WM_DRAWCLIPBOARD, 0, 0);
368 bCBHasChanged = FALSE;
371 bRet = TRUE;
374 return bRet;
378 /**************************************************************************
379 * EmptyClipboard (USER.139)
381 BOOL16 WINAPI EmptyClipboard16(void)
383 return EmptyClipboard();
387 /**************************************************************************
388 * EmptyClipboard (USER32.@)
389 * Empties and acquires ownership of the clipboard
391 BOOL WINAPI EmptyClipboard(void)
393 CLIPBOARDINFO cbinfo;
395 TRACE("()\n");
397 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
398 ~cbinfo.flags & CB_OPEN)
400 WARN("Clipboard not opened by calling task!\n");
401 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
402 return FALSE;
405 /* Destroy private objects */
406 if (cbinfo.hWndOwner)
407 SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
409 /* Tell the driver to acquire the selection. The current owner
410 * will be signaled to delete it's own cache. */
411 if (~cbinfo.flags & CB_OWNER)
413 /* Assign ownership of the clipboard to the current client. We do
414 * this before acquiring the selection so that when we do acquire the
415 * selection and the selection loser gets notified, it can check if
416 * it has lost the Wine clipboard ownership. If it did then it knows
417 * that a WM_DESTORYCLIPBOARD has already been sent. Otherwise it
418 * lost the selection to a X app and it should send the
419 * WM_DESTROYCLIPBOARD itself. */
420 CLIPBOARD_SetClipboardOwner(cbinfo.hWndOpen);
422 /* Acquire the selection. This will notify the previous owner
423 * to clear it's cache. */
424 if (USER_Driver.pAcquireClipboard)
425 USER_Driver.pAcquireClipboard(cbinfo.hWndOpen);
428 /* Empty the local cache */
429 if (USER_Driver.pEmptyClipboard)
430 USER_Driver.pEmptyClipboard();
432 bCBHasChanged = TRUE;
434 return TRUE;
438 /**************************************************************************
439 * GetClipboardOwner (USER32.@)
440 * FIXME: Can't return the owner if the clipboard is owned by an external X-app
442 HWND WINAPI GetClipboardOwner(void)
444 HWND hWndOwner = 0;
446 SERVER_START_REQ( set_clipboard_info )
448 req->flags = 0;
449 if (!wine_server_call_err( req )) hWndOwner = reply->old_owner;
451 SERVER_END_REQ;
453 TRACE(" hWndOwner(%p)\n", hWndOwner);
455 return hWndOwner;
459 /**************************************************************************
460 * GetOpenClipboardWindow (USER32.@)
462 HWND WINAPI GetOpenClipboardWindow(void)
464 HWND hWndOpen = 0;
466 SERVER_START_REQ( set_clipboard_info )
468 req->flags = 0;
469 if (!wine_server_call_err( req )) hWndOpen = reply->old_clipboard;
471 SERVER_END_REQ;
473 TRACE(" hWndClipWindow(%p)\n", hWndOpen);
475 return hWndOpen;
479 /**************************************************************************
480 * SetClipboardViewer (USER32.@)
482 HWND WINAPI SetClipboardViewer( HWND hWnd )
484 HWND hwndPrev = 0;
486 SERVER_START_REQ( set_clipboard_info )
488 req->flags = SET_CB_VIEWER;
489 req->viewer = WIN_GetFullHandle(hWnd);
491 if (wine_server_call_err( req ))
493 ERR("Failed to set clipboard.\n");
495 else
497 hwndPrev = reply->old_viewer;
500 SERVER_END_REQ;
502 TRACE("(%p): returning %p\n", hWnd, hwndPrev);
504 return hwndPrev;
508 /**************************************************************************
509 * GetClipboardViewer (USER32.@)
511 HWND WINAPI GetClipboardViewer(void)
513 HWND hWndViewer = 0;
515 SERVER_START_REQ( set_clipboard_info )
517 req->flags = 0;
518 if (!wine_server_call_err( req )) hWndViewer = reply->old_viewer;
520 SERVER_END_REQ;
522 TRACE(" hWndViewer=%p\n", hWndViewer);
524 return hWndViewer;
528 /**************************************************************************
529 * ChangeClipboardChain (USER32.@)
531 BOOL WINAPI ChangeClipboardChain(HWND hWnd, HWND hWndNext)
533 BOOL bRet = TRUE;
534 HWND hWndViewer = GetClipboardViewer();
536 if (hWndViewer)
538 if (WIN_GetFullHandle(hWnd) == hWndViewer)
539 SetClipboardViewer(WIN_GetFullHandle(hWndNext));
540 else
541 bRet = !SendMessageW(hWndViewer, WM_CHANGECBCHAIN, (WPARAM)hWnd, (LPARAM)hWndNext);
543 else
544 ERR("hWndViewer is lost\n");
546 return bRet;
550 /**************************************************************************
551 * SetClipboardData (USER.141)
553 HANDLE16 WINAPI SetClipboardData16(UINT16 wFormat, HANDLE16 hData)
555 CLIPBOARDINFO cbinfo;
556 HANDLE16 hResult = 0;
558 TRACE("(%04X, %04x) !\n", wFormat, hData);
560 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
561 (~cbinfo.flags & CB_OPEN) ||
562 (~cbinfo.flags & CB_OWNER))
564 WARN("Clipboard not opened by calling task!\n");
566 else if (USER_Driver.pSetClipboardData &&
567 USER_Driver.pSetClipboardData(wFormat, hData, 0))
569 hResult = hData;
570 bCBHasChanged = TRUE;
573 return hResult;
577 /**************************************************************************
578 * SetClipboardData (USER32.@)
580 HANDLE WINAPI SetClipboardData(UINT wFormat, HANDLE hData)
582 CLIPBOARDINFO cbinfo;
583 HANDLE hResult = 0;
585 TRACE("(%04X, %p) !\n", wFormat, hData);
587 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
588 (~cbinfo.flags & CB_OWNER))
590 WARN("Clipboard not owned by calling task!\n");
592 else if (USER_Driver.pSetClipboardData &&
593 USER_Driver.pSetClipboardData(wFormat, 0, hData))
595 hResult = hData;
596 bCBHasChanged = TRUE;
599 return hResult;
603 /**************************************************************************
604 * CountClipboardFormats (USER.143)
606 INT16 WINAPI CountClipboardFormats16(void)
608 return CountClipboardFormats();
612 /**************************************************************************
613 * CountClipboardFormats (USER32.@)
615 INT WINAPI CountClipboardFormats(void)
617 INT count = 0;
619 if (USER_Driver.pCountClipboardFormats)
620 count = USER_Driver.pCountClipboardFormats();
622 TRACE("returning %d\n", count);
623 return count;
627 /**************************************************************************
628 * EnumClipboardFormats (USER.144)
630 UINT16 WINAPI EnumClipboardFormats16(UINT16 wFormat)
632 return EnumClipboardFormats(wFormat);
636 /**************************************************************************
637 * EnumClipboardFormats (USER32.@)
639 UINT WINAPI EnumClipboardFormats(UINT wFormat)
641 UINT wFmt = 0;
642 CLIPBOARDINFO cbinfo;
644 TRACE("(%04X)\n", wFormat);
646 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
647 (~cbinfo.flags & CB_OPEN))
649 WARN("Clipboard not opened by calling task.\n");
650 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
651 return 0;
654 if (USER_Driver.pEnumClipboardFormats)
655 wFmt = USER_Driver.pEnumClipboardFormats(wFormat);
657 return wFmt;
661 /**************************************************************************
662 * IsClipboardFormatAvailable (USER.193)
664 BOOL16 WINAPI IsClipboardFormatAvailable16(UINT16 wFormat)
666 return IsClipboardFormatAvailable(wFormat);
670 /**************************************************************************
671 * IsClipboardFormatAvailable (USER32.@)
673 BOOL WINAPI IsClipboardFormatAvailable(UINT wFormat)
675 BOOL bret = FALSE;
677 if (USER_Driver.pIsClipboardFormatAvailable)
678 bret = USER_Driver.pIsClipboardFormatAvailable(wFormat);
680 TRACE("%04x, returning %d\n", wFormat, bret);
681 return bret;
685 /**************************************************************************
686 * GetClipboardData (USER.142)
688 HANDLE16 WINAPI GetClipboardData16(UINT16 wFormat)
690 HANDLE16 hData = 0;
691 CLIPBOARDINFO cbinfo;
693 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
694 (~cbinfo.flags & CB_OPEN))
696 WARN("Clipboard not opened by calling task.\n");
697 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
698 return 0;
701 if (USER_Driver.pGetClipboardData)
702 USER_Driver.pGetClipboardData(wFormat, &hData, NULL);
704 return hData;
708 /**************************************************************************
709 * GetClipboardData (USER32.@)
711 HANDLE WINAPI GetClipboardData(UINT wFormat)
713 HANDLE hData = 0;
714 CLIPBOARDINFO cbinfo;
716 TRACE("%04x\n", wFormat);
718 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
719 (~cbinfo.flags & CB_OPEN))
721 WARN("Clipboard not opened by calling task.\n");
722 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
723 return 0;
726 if (USER_Driver.pGetClipboardData)
727 USER_Driver.pGetClipboardData(wFormat, NULL, &hData);
729 TRACE("returning %p\n", hData);
730 return hData;
734 /**************************************************************************
735 * GetPriorityClipboardFormat (USER32.@)
737 INT WINAPI GetPriorityClipboardFormat(UINT *list, INT nCount)
739 int i;
741 TRACE("()\n");
743 if(CountClipboardFormats() == 0)
744 return 0;
746 for (i = 0; i < nCount; i++)
747 if (IsClipboardFormatAvailable(list[i]))
748 return list[i];
750 return -1;
754 /**************************************************************************
755 * GetClipboardSequenceNumber (USER32.@)
756 * Supported on Win2k/Win98
757 * MSDN: Windows clipboard code keeps a serial number for the clipboard
758 * for each window station. The number is incremented whenever the
759 * contents change or are emptied.
760 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
762 DWORD WINAPI GetClipboardSequenceNumber(VOID)
764 DWORD seqno = 0;
766 SERVER_START_REQ( set_clipboard_info )
768 req->flags = 0;
769 if (!wine_server_call_err( req )) seqno = reply->seqno;
771 SERVER_END_REQ;
773 TRACE("returning %lx\n", seqno);
774 return seqno;