Authors: Mike McCormack <mike@codeweavers.com>, Aric Stewart <aric@codeweavers.com...
[wine/multimedia.git] / windows / clipboard.c
blob178b714142cc6a2168ac8a59838dd2908a58b3ce
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 "user_private.h"
52 #include "win.h"
54 #include "wine/debug.h"
55 #include "wine/unicode.h"
56 #include "wine/server.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(clipboard);
60 #define CF_REGFORMATBASE 0xC000
62 typedef struct
64 HWND hWndOpen;
65 HWND hWndOwner;
66 HWND hWndViewer;
67 UINT seqno;
68 UINT flags;
69 } CLIPBOARDINFO, *LPCLIPBOARDINFO;
72 * Indicates if data has changed since open.
74 static BOOL bCBHasChanged = FALSE;
77 /**************************************************************************
78 * CLIPBOARD_SetClipboardOwner
80 * Set the global wineserver clipboard owner. The current process will
81 * be the owner and <hWnd> will get the render notifications.
83 BOOL CLIPBOARD_SetClipboardOwner(HWND hWnd)
85 BOOL bRet = FALSE;
87 TRACE(" hWnd(%p)\n", hWnd);
89 SERVER_START_REQ( set_clipboard_info )
91 req->flags = SET_CB_OWNER;
92 req->owner = WIN_GetFullHandle( hWnd );
94 if (wine_server_call_err( req ))
96 ERR("Failed to set clipboard owner to %p\n", hWnd);
98 else
100 bRet = TRUE;
103 SERVER_END_REQ;
105 return bRet;
109 /**************************************************************************
110 * CLIPBOARD_GetClipboardInfo
112 static BOOL CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo)
114 BOOL bRet = FALSE;
116 SERVER_START_REQ( set_clipboard_info )
118 req->flags = 0;
120 if (wine_server_call_err( req ))
122 ERR("Failed to get clipboard info\n");
124 else
126 cbInfo->hWndOpen = reply->old_clipboard;
127 cbInfo->hWndOwner = reply->old_owner;
128 cbInfo->hWndViewer = reply->old_viewer;
129 cbInfo->seqno = reply->seqno;
130 cbInfo->flags = reply->flags;
132 bRet = TRUE;
135 SERVER_END_REQ;
137 return bRet;
141 /**************************************************************************
142 * CLIPBOARD_ReleaseOwner
144 BOOL CLIPBOARD_ReleaseOwner(void)
146 BOOL bRet = FALSE;
148 SERVER_START_REQ( set_clipboard_info )
150 req->flags = SET_CB_RELOWNER | SET_CB_SEQNO;
152 if (wine_server_call_err( req ))
154 ERR("Failed to set clipboard.\n");
156 else
158 bRet = TRUE;
161 SERVER_END_REQ;
163 return bRet;
167 /**************************************************************************
168 * CLIPBOARD_OpenClipboard
170 static BOOL CLIPBOARD_OpenClipboard(HWND hWnd)
172 BOOL bRet = FALSE;
174 SERVER_START_REQ( set_clipboard_info )
176 req->flags = SET_CB_OPEN;
177 req->clipboard = WIN_GetFullHandle( hWnd );
179 if (!wine_server_call( req ))
180 bRet = TRUE;
182 SERVER_END_REQ;
184 return bRet;
188 /**************************************************************************
189 * CLIPBOARD_CloseClipboard
191 static BOOL CLIPBOARD_CloseClipboard(void)
193 BOOL bRet = FALSE;
195 TRACE(" Changed=%d\n", bCBHasChanged);
197 SERVER_START_REQ( set_clipboard_info )
199 req->flags = SET_CB_CLOSE;
201 if (bCBHasChanged)
203 req->flags |= SET_CB_SEQNO;
204 TRACE("Clipboard data changed\n");
207 if (wine_server_call_err( req ))
209 ERR("Failed to set clipboard.\n");
211 else
213 bRet = TRUE;
216 SERVER_END_REQ;
218 return bRet;
222 /**************************************************************************
223 * WIN32 Clipboard implementation
224 **************************************************************************/
226 /**************************************************************************
227 * RegisterClipboardFormatW (USER32.@)
229 UINT WINAPI RegisterClipboardFormatW(LPCWSTR FormatName)
231 UINT wFormatID = 0;
233 TRACE("%s\n", debugstr_w(FormatName));
235 if (USER_Driver.pRegisterClipboardFormat)
236 wFormatID = USER_Driver.pRegisterClipboardFormat(FormatName);
238 return wFormatID;
242 /**************************************************************************
243 * RegisterClipboardFormatA (USER32.@)
245 UINT WINAPI RegisterClipboardFormatA(LPCSTR formatName)
247 int len;
248 LPWSTR wFormat;
249 UINT ret;
251 len = MultiByteToWideChar(CP_ACP, 0, formatName, -1, NULL, 0);
252 wFormat = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
253 MultiByteToWideChar(CP_ACP, 0, formatName, -1, wFormat, len);
255 ret = RegisterClipboardFormatW(wFormat);
256 HeapFree(GetProcessHeap(), 0, wFormat);
257 return ret;
261 /**************************************************************************
262 * GetClipboardFormatNameW (USER32.@)
264 INT WINAPI GetClipboardFormatNameW(UINT wFormat, LPWSTR retStr, INT maxlen)
266 INT len = 0;
268 TRACE("%04x,%p,%d\n", wFormat, retStr, maxlen);
270 if (USER_Driver.pGetClipboardFormatName)
271 len = USER_Driver.pGetClipboardFormatName(wFormat, retStr, maxlen);
273 return len;
277 /**************************************************************************
278 * GetClipboardFormatNameA (USER32.@)
280 INT WINAPI GetClipboardFormatNameA(UINT wFormat, LPSTR retStr, INT maxlen)
282 INT ret;
283 LPWSTR p = HeapAlloc( GetProcessHeap(), 0, maxlen*sizeof(WCHAR) );
284 if(p == NULL) return 0; /* FIXME: is this the correct failure value? */
286 ret = GetClipboardFormatNameW( wFormat, p, maxlen );
288 if (maxlen > 0 && !WideCharToMultiByte( CP_ACP, 0, p, -1, retStr, maxlen, 0, 0))
289 retStr[maxlen-1] = 0;
290 HeapFree( GetProcessHeap(), 0, p );
291 return ret;
295 /**************************************************************************
296 * OpenClipboard (USER32.@)
298 * Note: Netscape uses NULL hWnd to open the clipboard.
300 BOOL WINAPI OpenClipboard( HWND hWnd )
302 BOOL bRet;
304 TRACE("(%p)...\n", hWnd);
306 bRet = CLIPBOARD_OpenClipboard(hWnd);
308 TRACE(" returning %i\n", bRet);
310 return bRet;
314 /**************************************************************************
315 * CloseClipboard (USER32.@)
317 BOOL WINAPI CloseClipboard(void)
319 BOOL bRet = FALSE;
321 TRACE("(%d)\n", bCBHasChanged);
323 if (CLIPBOARD_CloseClipboard())
325 if (bCBHasChanged)
327 HWND hWndViewer = GetClipboardViewer();
329 if (USER_Driver.pEndClipboardUpdate)
330 USER_Driver.pEndClipboardUpdate();
332 if (hWndViewer)
333 SendMessageW(hWndViewer, WM_DRAWCLIPBOARD, 0, 0);
335 bCBHasChanged = FALSE;
338 bRet = TRUE;
341 return bRet;
345 /**************************************************************************
346 * EmptyClipboard (USER32.@)
347 * Empties and acquires ownership of the clipboard
349 BOOL WINAPI EmptyClipboard(void)
351 CLIPBOARDINFO cbinfo;
353 TRACE("()\n");
355 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
356 ~cbinfo.flags & CB_OPEN)
358 WARN("Clipboard not opened by calling task!\n");
359 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
360 return FALSE;
363 /* Destroy private objects */
364 if (cbinfo.hWndOwner)
365 SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
367 /* Tell the driver to acquire the selection. The current owner
368 * will be signaled to delete it's own cache. */
370 /* Assign ownership of the clipboard to the current client. We do
371 * this before acquiring the selection so that when we do acquire the
372 * selection and the selection loser gets notified, it can check if
373 * it has lost the Wine clipboard ownership. If it did then it knows
374 * that a WM_DESTORYCLIPBOARD has already been sent. Otherwise it
375 * lost the selection to a X app and it should send the
376 * WM_DESTROYCLIPBOARD itself. */
377 CLIPBOARD_SetClipboardOwner(cbinfo.hWndOpen);
379 /* Acquire the selection. This will notify the previous owner
380 * to clear it's cache. */
381 if (USER_Driver.pAcquireClipboard)
382 USER_Driver.pAcquireClipboard(cbinfo.hWndOpen);
384 /* Empty the local cache */
385 if (USER_Driver.pEmptyClipboard)
386 USER_Driver.pEmptyClipboard(FALSE);
388 bCBHasChanged = TRUE;
390 return TRUE;
394 /**************************************************************************
395 * GetClipboardOwner (USER32.@)
396 * FIXME: Can't return the owner if the clipboard is owned by an external X-app
398 HWND WINAPI GetClipboardOwner(void)
400 HWND hWndOwner = 0;
402 SERVER_START_REQ( set_clipboard_info )
404 req->flags = 0;
405 if (!wine_server_call_err( req )) hWndOwner = reply->old_owner;
407 SERVER_END_REQ;
409 TRACE(" hWndOwner(%p)\n", hWndOwner);
411 return hWndOwner;
415 /**************************************************************************
416 * GetOpenClipboardWindow (USER32.@)
418 HWND WINAPI GetOpenClipboardWindow(void)
420 HWND hWndOpen = 0;
422 SERVER_START_REQ( set_clipboard_info )
424 req->flags = 0;
425 if (!wine_server_call_err( req )) hWndOpen = reply->old_clipboard;
427 SERVER_END_REQ;
429 TRACE(" hWndClipWindow(%p)\n", hWndOpen);
431 return hWndOpen;
435 /**************************************************************************
436 * SetClipboardViewer (USER32.@)
438 HWND WINAPI SetClipboardViewer( HWND hWnd )
440 HWND hwndPrev = 0;
442 SERVER_START_REQ( set_clipboard_info )
444 req->flags = SET_CB_VIEWER;
445 req->viewer = WIN_GetFullHandle(hWnd);
447 if (wine_server_call_err( req ))
449 ERR("Failed to set clipboard.\n");
451 else
453 hwndPrev = reply->old_viewer;
456 SERVER_END_REQ;
458 TRACE("(%p): returning %p\n", hWnd, hwndPrev);
460 return hwndPrev;
464 /**************************************************************************
465 * GetClipboardViewer (USER32.@)
467 HWND WINAPI GetClipboardViewer(void)
469 HWND hWndViewer = 0;
471 SERVER_START_REQ( set_clipboard_info )
473 req->flags = 0;
474 if (!wine_server_call_err( req )) hWndViewer = reply->old_viewer;
476 SERVER_END_REQ;
478 TRACE(" hWndViewer=%p\n", hWndViewer);
480 return hWndViewer;
484 /**************************************************************************
485 * ChangeClipboardChain (USER32.@)
487 BOOL WINAPI ChangeClipboardChain(HWND hWnd, HWND hWndNext)
489 BOOL bRet = TRUE;
490 HWND hWndViewer = GetClipboardViewer();
492 if (hWndViewer)
494 if (WIN_GetFullHandle(hWnd) == hWndViewer)
495 SetClipboardViewer(WIN_GetFullHandle(hWndNext));
496 else
497 bRet = !SendMessageW(hWndViewer, WM_CHANGECBCHAIN, (WPARAM)hWnd, (LPARAM)hWndNext);
499 else
500 ERR("hWndViewer is lost\n");
502 return bRet;
506 /**************************************************************************
507 * SetClipboardData (USER.141)
509 HANDLE16 WINAPI SetClipboardData16(UINT16 wFormat, HANDLE16 hData)
511 CLIPBOARDINFO cbinfo;
512 HANDLE16 hResult = 0;
514 TRACE("(%04X, %04x) !\n", wFormat, hData);
516 /* If it's not owned, data can only be set if the format doesn't exists
517 and its rendering is not delayed */
518 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
519 (!(cbinfo.flags & CB_OWNER) && !hData))
521 WARN("Clipboard not owned by calling task. Operation failed.\n");
522 return 0;
525 if (USER_Driver.pSetClipboardData &&
526 USER_Driver.pSetClipboardData(wFormat, hData, 0, cbinfo.flags & CB_OWNER))
528 hResult = hData;
529 bCBHasChanged = TRUE;
532 return hResult;
536 /**************************************************************************
537 * SetClipboardData (USER32.@)
539 HANDLE WINAPI SetClipboardData(UINT wFormat, HANDLE hData)
541 CLIPBOARDINFO cbinfo;
542 HANDLE hResult = 0;
544 TRACE("(%04X, %p) !\n", wFormat, hData);
546 /* If it's not owned, data can only be set if the format isn't
547 available and its rendering is not delayed */
548 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
549 (!(cbinfo.flags & CB_OWNER) && !hData))
551 WARN("Clipboard not owned by calling task. Operation failed.\n");
552 return 0;
555 if (USER_Driver.pSetClipboardData &&
556 USER_Driver.pSetClipboardData(wFormat, 0, hData, cbinfo.flags & CB_OWNER))
558 hResult = hData;
559 bCBHasChanged = TRUE;
562 return hResult;
566 /**************************************************************************
567 * CountClipboardFormats (USER32.@)
569 INT WINAPI CountClipboardFormats(void)
571 INT count = 0;
573 if (USER_Driver.pCountClipboardFormats)
574 count = USER_Driver.pCountClipboardFormats();
576 TRACE("returning %d\n", count);
577 return count;
581 /**************************************************************************
582 * EnumClipboardFormats (USER32.@)
584 UINT WINAPI EnumClipboardFormats(UINT wFormat)
586 UINT wFmt = 0;
587 CLIPBOARDINFO cbinfo;
589 TRACE("(%04X)\n", wFormat);
591 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
592 (~cbinfo.flags & CB_OPEN))
594 WARN("Clipboard not opened by calling task.\n");
595 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
596 return 0;
599 if (USER_Driver.pEnumClipboardFormats)
600 wFmt = USER_Driver.pEnumClipboardFormats(wFormat);
602 return wFmt;
606 /**************************************************************************
607 * IsClipboardFormatAvailable (USER32.@)
609 BOOL WINAPI IsClipboardFormatAvailable(UINT wFormat)
611 BOOL bret = FALSE;
613 if (USER_Driver.pIsClipboardFormatAvailable)
614 bret = USER_Driver.pIsClipboardFormatAvailable(wFormat);
616 TRACE("%04x, returning %d\n", wFormat, bret);
617 return bret;
621 /**************************************************************************
622 * GetClipboardData (USER.142)
624 HANDLE16 WINAPI GetClipboardData16(UINT16 wFormat)
626 HANDLE16 hData = 0;
627 CLIPBOARDINFO cbinfo;
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.pGetClipboardData)
638 USER_Driver.pGetClipboardData(wFormat, &hData, NULL);
640 return hData;
644 /**************************************************************************
645 * GetClipboardData (USER32.@)
647 HANDLE WINAPI GetClipboardData(UINT wFormat)
649 HANDLE hData = 0;
650 CLIPBOARDINFO cbinfo;
652 TRACE("%04x\n", wFormat);
654 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
655 (~cbinfo.flags & CB_OPEN))
657 WARN("Clipboard not opened by calling task.\n");
658 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
659 return 0;
662 if (USER_Driver.pGetClipboardData)
663 USER_Driver.pGetClipboardData(wFormat, NULL, &hData);
665 TRACE("returning %p\n", hData);
666 return hData;
670 /**************************************************************************
671 * GetPriorityClipboardFormat (USER32.@)
673 INT WINAPI GetPriorityClipboardFormat(UINT *list, INT nCount)
675 int i;
677 TRACE("()\n");
679 if(CountClipboardFormats() == 0)
680 return 0;
682 for (i = 0; i < nCount; i++)
683 if (IsClipboardFormatAvailable(list[i]))
684 return list[i];
686 return -1;
690 /**************************************************************************
691 * GetClipboardSequenceNumber (USER32.@)
692 * Supported on Win2k/Win98
693 * MSDN: Windows clipboard code keeps a serial number for the clipboard
694 * for each window station. The number is incremented whenever the
695 * contents change or are emptied.
696 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
698 DWORD WINAPI GetClipboardSequenceNumber(VOID)
700 DWORD seqno = 0;
702 SERVER_START_REQ( set_clipboard_info )
704 req->flags = 0;
705 if (!wine_server_call_err( req )) seqno = reply->seqno;
707 SERVER_END_REQ;
709 TRACE("returning %lx\n", seqno);
710 return seqno;