urlmon: Remove 'recursive registry key delete' function.
[wine/multimedia.git] / dlls / user32 / clipboard.c
blob7076ef258015dd40cbc3aa4a170263764d75b669
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 static 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 return USER_Driver->pRegisterClipboardFormat(FormatName);
235 /**************************************************************************
236 * RegisterClipboardFormatA (USER32.@)
238 UINT WINAPI RegisterClipboardFormatA(LPCSTR formatName)
240 int len;
241 LPWSTR wFormat;
242 UINT ret;
244 len = MultiByteToWideChar(CP_ACP, 0, formatName, -1, NULL, 0);
245 wFormat = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
246 MultiByteToWideChar(CP_ACP, 0, formatName, -1, wFormat, len);
248 ret = RegisterClipboardFormatW(wFormat);
249 HeapFree(GetProcessHeap(), 0, wFormat);
250 return ret;
254 /**************************************************************************
255 * GetClipboardFormatNameW (USER32.@)
257 INT WINAPI GetClipboardFormatNameW(UINT wFormat, LPWSTR retStr, INT maxlen)
259 return USER_Driver->pGetClipboardFormatName(wFormat, retStr, maxlen);
263 /**************************************************************************
264 * GetClipboardFormatNameA (USER32.@)
266 INT WINAPI GetClipboardFormatNameA(UINT wFormat, LPSTR retStr, INT maxlen)
268 INT ret;
269 LPWSTR p = HeapAlloc( GetProcessHeap(), 0, maxlen*sizeof(WCHAR) );
270 if(p == NULL) return 0; /* FIXME: is this the correct failure value? */
272 ret = GetClipboardFormatNameW( wFormat, p, maxlen );
274 if (ret && maxlen > 0 && !WideCharToMultiByte( CP_ACP, 0, p, -1, retStr, maxlen, 0, 0))
275 retStr[maxlen-1] = 0;
276 HeapFree( GetProcessHeap(), 0, p );
277 return ret;
281 /**************************************************************************
282 * OpenClipboard (USER32.@)
284 * Note: Netscape uses NULL hWnd to open the clipboard.
286 BOOL WINAPI OpenClipboard( HWND hWnd )
288 BOOL bRet;
290 TRACE("(%p)...\n", hWnd);
292 bRet = CLIPBOARD_OpenClipboard(hWnd);
294 TRACE(" returning %i\n", bRet);
296 return bRet;
300 /**************************************************************************
301 * CloseClipboard (USER32.@)
303 BOOL WINAPI CloseClipboard(void)
305 BOOL bRet = FALSE;
307 TRACE("(%d)\n", bCBHasChanged);
309 if (CLIPBOARD_CloseClipboard())
311 if (bCBHasChanged)
313 HWND hWndViewer = GetClipboardViewer();
315 USER_Driver->pEndClipboardUpdate();
317 if (hWndViewer)
318 SendMessageW(hWndViewer, WM_DRAWCLIPBOARD, 0, 0);
320 bCBHasChanged = FALSE;
323 bRet = TRUE;
326 return bRet;
330 /**************************************************************************
331 * EmptyClipboard (USER32.@)
332 * Empties and acquires ownership of the clipboard
334 BOOL WINAPI EmptyClipboard(void)
336 CLIPBOARDINFO cbinfo;
338 TRACE("()\n");
340 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
341 ~cbinfo.flags & CB_OPEN)
343 WARN("Clipboard not opened by calling task!\n");
344 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
345 return FALSE;
348 /* Destroy private objects */
349 if (cbinfo.hWndOwner)
350 SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
352 /* Tell the driver to acquire the selection. The current owner
353 * will be signaled to delete it's own cache. */
355 /* Assign ownership of the clipboard to the current client. We do
356 * this before acquiring the selection so that when we do acquire the
357 * selection and the selection loser gets notified, it can check if
358 * it has lost the Wine clipboard ownership. If it did then it knows
359 * that a WM_DESTORYCLIPBOARD has already been sent. Otherwise it
360 * lost the selection to a X app and it should send the
361 * WM_DESTROYCLIPBOARD itself. */
362 CLIPBOARD_SetClipboardOwner(cbinfo.hWndOpen);
364 /* Acquire the selection. This will notify the previous owner
365 * to clear it's cache. */
366 USER_Driver->pAcquireClipboard(cbinfo.hWndOpen);
368 /* Empty the local cache */
369 USER_Driver->pEmptyClipboard(FALSE);
371 bCBHasChanged = TRUE;
373 return TRUE;
377 /**************************************************************************
378 * GetClipboardOwner (USER32.@)
379 * FIXME: Can't return the owner if the clipboard is owned by an external X-app
381 HWND WINAPI GetClipboardOwner(void)
383 HWND hWndOwner = 0;
385 SERVER_START_REQ( set_clipboard_info )
387 req->flags = 0;
388 if (!wine_server_call_err( req )) hWndOwner = reply->old_owner;
390 SERVER_END_REQ;
392 TRACE(" hWndOwner(%p)\n", hWndOwner);
394 return hWndOwner;
398 /**************************************************************************
399 * GetOpenClipboardWindow (USER32.@)
401 HWND WINAPI GetOpenClipboardWindow(void)
403 HWND hWndOpen = 0;
405 SERVER_START_REQ( set_clipboard_info )
407 req->flags = 0;
408 if (!wine_server_call_err( req )) hWndOpen = reply->old_clipboard;
410 SERVER_END_REQ;
412 TRACE(" hWndClipWindow(%p)\n", hWndOpen);
414 return hWndOpen;
418 /**************************************************************************
419 * SetClipboardViewer (USER32.@)
421 HWND WINAPI SetClipboardViewer( HWND hWnd )
423 HWND hwndPrev = 0;
425 SERVER_START_REQ( set_clipboard_info )
427 req->flags = SET_CB_VIEWER;
428 req->viewer = WIN_GetFullHandle(hWnd);
430 if (wine_server_call_err( req ))
432 ERR("Failed to set clipboard.\n");
434 else
436 hwndPrev = reply->old_viewer;
439 SERVER_END_REQ;
441 TRACE("(%p): returning %p\n", hWnd, hwndPrev);
443 return hwndPrev;
447 /**************************************************************************
448 * GetClipboardViewer (USER32.@)
450 HWND WINAPI GetClipboardViewer(void)
452 HWND hWndViewer = 0;
454 SERVER_START_REQ( set_clipboard_info )
456 req->flags = 0;
457 if (!wine_server_call_err( req )) hWndViewer = reply->old_viewer;
459 SERVER_END_REQ;
461 TRACE(" hWndViewer=%p\n", hWndViewer);
463 return hWndViewer;
467 /**************************************************************************
468 * ChangeClipboardChain (USER32.@)
470 BOOL WINAPI ChangeClipboardChain(HWND hWnd, HWND hWndNext)
472 BOOL bRet = TRUE;
473 HWND hWndViewer = GetClipboardViewer();
475 if (hWndViewer)
477 if (WIN_GetFullHandle(hWnd) == hWndViewer)
478 SetClipboardViewer(WIN_GetFullHandle(hWndNext));
479 else
480 bRet = !SendMessageW(hWndViewer, WM_CHANGECBCHAIN, (WPARAM)hWnd, (LPARAM)hWndNext);
482 else
483 ERR("hWndViewer is lost\n");
485 return bRet;
489 /**************************************************************************
490 * SetClipboardData (USER.141)
492 HANDLE16 WINAPI SetClipboardData16(UINT16 wFormat, HANDLE16 hData)
494 CLIPBOARDINFO cbinfo;
495 HANDLE16 hResult = 0;
497 TRACE("(%04X, %04x) !\n", wFormat, hData);
499 /* If it's not owned, data can only be set if the format doesn't exists
500 and its rendering is not delayed */
501 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
502 (!(cbinfo.flags & CB_OWNER) && !hData))
504 WARN("Clipboard not owned by calling task. Operation failed.\n");
505 return 0;
508 if (USER_Driver->pSetClipboardData(wFormat, hData, 0, cbinfo.flags & CB_OWNER))
510 hResult = hData;
511 bCBHasChanged = TRUE;
514 return hResult;
518 /**************************************************************************
519 * SetClipboardData (USER32.@)
521 HANDLE WINAPI SetClipboardData(UINT wFormat, HANDLE hData)
523 CLIPBOARDINFO cbinfo;
524 HANDLE hResult = 0;
526 TRACE("(%04X, %p) !\n", wFormat, hData);
528 /* If it's not owned, data can only be set if the format isn't
529 available and its rendering is not delayed */
530 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
531 (!(cbinfo.flags & CB_OWNER) && !hData))
533 WARN("Clipboard not owned by calling task. Operation failed.\n");
534 return 0;
537 if (USER_Driver->pSetClipboardData(wFormat, 0, hData, cbinfo.flags & CB_OWNER))
539 hResult = hData;
540 bCBHasChanged = TRUE;
543 return hResult;
547 /**************************************************************************
548 * CountClipboardFormats (USER32.@)
550 INT WINAPI CountClipboardFormats(void)
552 INT count = USER_Driver->pCountClipboardFormats();
553 TRACE("returning %d\n", count);
554 return count;
558 /**************************************************************************
559 * EnumClipboardFormats (USER32.@)
561 UINT WINAPI EnumClipboardFormats(UINT wFormat)
563 CLIPBOARDINFO cbinfo;
565 TRACE("(%04X)\n", wFormat);
567 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
568 (~cbinfo.flags & CB_OPEN))
570 WARN("Clipboard not opened by calling task.\n");
571 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
572 return 0;
574 return USER_Driver->pEnumClipboardFormats(wFormat);
578 /**************************************************************************
579 * IsClipboardFormatAvailable (USER32.@)
581 BOOL WINAPI IsClipboardFormatAvailable(UINT wFormat)
583 BOOL bret = USER_Driver->pIsClipboardFormatAvailable(wFormat);
584 TRACE("%04x, returning %d\n", wFormat, bret);
585 return bret;
589 /**************************************************************************
590 * GetClipboardData (USER.142)
592 HANDLE16 WINAPI GetClipboardData16(UINT16 wFormat)
594 HANDLE16 hData = 0;
595 CLIPBOARDINFO cbinfo;
597 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
598 (~cbinfo.flags & CB_OPEN))
600 WARN("Clipboard not opened by calling task.\n");
601 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
602 return 0;
605 if (!USER_Driver->pGetClipboardData(wFormat, &hData, NULL)) hData = 0;
607 return hData;
611 /**************************************************************************
612 * GetClipboardData (USER32.@)
614 HANDLE WINAPI GetClipboardData(UINT wFormat)
616 HANDLE hData = 0;
617 CLIPBOARDINFO cbinfo;
619 TRACE("%04x\n", wFormat);
621 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
622 (~cbinfo.flags & CB_OPEN))
624 WARN("Clipboard not opened by calling task.\n");
625 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
626 return 0;
629 if (!USER_Driver->pGetClipboardData(wFormat, NULL, &hData)) hData = 0;
631 TRACE("returning %p\n", hData);
632 return hData;
636 /**************************************************************************
637 * GetPriorityClipboardFormat (USER32.@)
639 INT WINAPI GetPriorityClipboardFormat(UINT *list, INT nCount)
641 int i;
643 TRACE("()\n");
645 if(CountClipboardFormats() == 0)
646 return 0;
648 for (i = 0; i < nCount; i++)
649 if (IsClipboardFormatAvailable(list[i]))
650 return list[i];
652 return -1;
656 /**************************************************************************
657 * GetClipboardSequenceNumber (USER32.@)
658 * Supported on Win2k/Win98
659 * MSDN: Windows clipboard code keeps a serial number for the clipboard
660 * for each window station. The number is incremented whenever the
661 * contents change or are emptied.
662 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
664 DWORD WINAPI GetClipboardSequenceNumber(VOID)
666 DWORD seqno = 0;
668 SERVER_START_REQ( set_clipboard_info )
670 req->flags = 0;
671 if (!wine_server_call_err( req )) seqno = reply->seqno;
673 SERVER_END_REQ;
675 TRACE("returning %x\n", seqno);
676 return seqno;