user32: Convert source files to utf-8.
[wine/multimedia.git] / dlls / user32 / clipboard.c
blob137ace9bfaa5abc31e39a68c30311ad9bbffe0c4
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/winbase16.h"
50 #include "user_private.h"
51 #include "win.h"
53 #include "wine/debug.h"
54 #include "wine/unicode.h"
55 #include "wine/server.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(clipboard);
59 #define CF_REGFORMATBASE 0xC000
61 typedef struct
63 HWND hWndOpen;
64 HWND hWndOwner;
65 HWND hWndViewer;
66 UINT seqno;
67 UINT flags;
68 } CLIPBOARDINFO, *LPCLIPBOARDINFO;
71 * Indicates if data has changed since open.
73 static BOOL bCBHasChanged = FALSE;
76 /**************************************************************************
77 * CLIPBOARD_SetClipboardOwner
79 * Set the global wineserver clipboard owner. The current process will
80 * be the owner and <hWnd> will get the render notifications.
82 static BOOL CLIPBOARD_SetClipboardOwner(HWND hWnd)
84 BOOL bRet = FALSE;
86 TRACE(" hWnd(%p)\n", hWnd);
88 SERVER_START_REQ( set_clipboard_info )
90 req->flags = SET_CB_OWNER;
91 req->owner = WIN_GetFullHandle( hWnd );
93 if (wine_server_call_err( req ))
95 ERR("Failed to set clipboard owner to %p\n", hWnd);
97 else
99 bRet = TRUE;
102 SERVER_END_REQ;
104 return bRet;
108 /**************************************************************************
109 * CLIPBOARD_GetClipboardInfo
111 static BOOL CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo)
113 BOOL bRet = FALSE;
115 SERVER_START_REQ( set_clipboard_info )
117 req->flags = 0;
119 if (wine_server_call_err( req ))
121 ERR("Failed to get clipboard info\n");
123 else
125 cbInfo->hWndOpen = reply->old_clipboard;
126 cbInfo->hWndOwner = reply->old_owner;
127 cbInfo->hWndViewer = reply->old_viewer;
128 cbInfo->seqno = reply->seqno;
129 cbInfo->flags = reply->flags;
131 bRet = TRUE;
134 SERVER_END_REQ;
136 return bRet;
140 /**************************************************************************
141 * CLIPBOARD_ReleaseOwner
143 BOOL CLIPBOARD_ReleaseOwner(void)
145 BOOL bRet = FALSE;
147 SERVER_START_REQ( set_clipboard_info )
149 req->flags = SET_CB_RELOWNER | SET_CB_SEQNO;
151 if (wine_server_call_err( req ))
153 ERR("Failed to set clipboard.\n");
155 else
157 bRet = TRUE;
160 SERVER_END_REQ;
162 return bRet;
166 /**************************************************************************
167 * CLIPBOARD_OpenClipboard
169 static BOOL CLIPBOARD_OpenClipboard(HWND hWnd)
171 BOOL bRet = FALSE;
173 SERVER_START_REQ( set_clipboard_info )
175 req->flags = SET_CB_OPEN;
176 req->clipboard = WIN_GetFullHandle( hWnd );
178 if (!wine_server_call( req ))
179 bRet = TRUE;
181 SERVER_END_REQ;
183 return bRet;
187 /**************************************************************************
188 * CLIPBOARD_CloseClipboard
190 static BOOL CLIPBOARD_CloseClipboard(void)
192 BOOL bRet = FALSE;
194 TRACE(" Changed=%d\n", bCBHasChanged);
196 SERVER_START_REQ( set_clipboard_info )
198 req->flags = SET_CB_CLOSE;
200 if (bCBHasChanged)
202 req->flags |= SET_CB_SEQNO;
203 TRACE("Clipboard data changed\n");
206 if (wine_server_call_err( req ))
208 ERR("Failed to set clipboard.\n");
210 else
212 bRet = TRUE;
215 SERVER_END_REQ;
217 return bRet;
221 /**************************************************************************
222 * WIN32 Clipboard implementation
223 **************************************************************************/
225 /**************************************************************************
226 * RegisterClipboardFormatW (USER32.@)
228 UINT WINAPI RegisterClipboardFormatW(LPCWSTR FormatName)
230 return USER_Driver->pRegisterClipboardFormat(FormatName);
234 /**************************************************************************
235 * RegisterClipboardFormatA (USER32.@)
237 UINT WINAPI RegisterClipboardFormatA(LPCSTR formatName)
239 int len;
240 LPWSTR wFormat;
241 UINT ret;
243 len = MultiByteToWideChar(CP_ACP, 0, formatName, -1, NULL, 0);
244 wFormat = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
245 MultiByteToWideChar(CP_ACP, 0, formatName, -1, wFormat, len);
247 ret = RegisterClipboardFormatW(wFormat);
248 HeapFree(GetProcessHeap(), 0, wFormat);
249 return ret;
253 /**************************************************************************
254 * GetClipboardFormatNameW (USER32.@)
256 INT WINAPI GetClipboardFormatNameW(UINT wFormat, LPWSTR retStr, INT maxlen)
258 return USER_Driver->pGetClipboardFormatName(wFormat, retStr, maxlen);
262 /**************************************************************************
263 * GetClipboardFormatNameA (USER32.@)
265 INT WINAPI GetClipboardFormatNameA(UINT wFormat, LPSTR retStr, INT maxlen)
267 INT ret;
268 LPWSTR p = HeapAlloc( GetProcessHeap(), 0, maxlen*sizeof(WCHAR) );
269 if(p == NULL) return 0; /* FIXME: is this the correct failure value? */
271 ret = GetClipboardFormatNameW( wFormat, p, maxlen );
273 if (ret && maxlen > 0 && !WideCharToMultiByte( CP_ACP, 0, p, -1, retStr, maxlen, 0, 0))
274 retStr[maxlen-1] = 0;
275 HeapFree( GetProcessHeap(), 0, p );
276 return ret;
280 /**************************************************************************
281 * OpenClipboard (USER32.@)
283 * Note: Netscape uses NULL hWnd to open the clipboard.
285 BOOL WINAPI OpenClipboard( HWND hWnd )
287 BOOL bRet;
289 TRACE("(%p)...\n", hWnd);
291 bRet = CLIPBOARD_OpenClipboard(hWnd);
293 TRACE(" returning %i\n", bRet);
295 return bRet;
299 /**************************************************************************
300 * CloseClipboard (USER32.@)
302 BOOL WINAPI CloseClipboard(void)
304 BOOL bRet = FALSE;
306 TRACE("(%d)\n", bCBHasChanged);
308 if (CLIPBOARD_CloseClipboard())
310 if (bCBHasChanged)
312 HWND hWndViewer = GetClipboardViewer();
314 USER_Driver->pEndClipboardUpdate();
316 if (hWndViewer)
317 SendMessageW(hWndViewer, WM_DRAWCLIPBOARD, 0, 0);
319 bCBHasChanged = FALSE;
322 bRet = TRUE;
325 return bRet;
329 /**************************************************************************
330 * EmptyClipboard (USER32.@)
331 * Empties and acquires ownership of the clipboard
333 BOOL WINAPI EmptyClipboard(void)
335 CLIPBOARDINFO cbinfo;
337 TRACE("()\n");
339 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
340 ~cbinfo.flags & CB_OPEN)
342 WARN("Clipboard not opened by calling task!\n");
343 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
344 return FALSE;
347 /* Destroy private objects */
348 if (cbinfo.hWndOwner)
349 SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
351 /* Tell the driver to acquire the selection. The current owner
352 * will be signaled to delete it's own cache. */
354 /* Assign ownership of the clipboard to the current client. We do
355 * this before acquiring the selection so that when we do acquire the
356 * selection and the selection loser gets notified, it can check if
357 * it has lost the Wine clipboard ownership. If it did then it knows
358 * that a WM_DESTORYCLIPBOARD has already been sent. Otherwise it
359 * lost the selection to a X app and it should send the
360 * WM_DESTROYCLIPBOARD itself. */
361 CLIPBOARD_SetClipboardOwner(cbinfo.hWndOpen);
363 /* Acquire the selection. This will notify the previous owner
364 * to clear it's cache. */
365 USER_Driver->pAcquireClipboard(cbinfo.hWndOpen);
367 /* Empty the local cache */
368 USER_Driver->pEmptyClipboard(FALSE);
370 bCBHasChanged = TRUE;
372 return TRUE;
376 /**************************************************************************
377 * GetClipboardOwner (USER32.@)
378 * FIXME: Can't return the owner if the clipboard is owned by an external X-app
380 HWND WINAPI GetClipboardOwner(void)
382 HWND hWndOwner = 0;
384 SERVER_START_REQ( set_clipboard_info )
386 req->flags = 0;
387 if (!wine_server_call_err( req )) hWndOwner = reply->old_owner;
389 SERVER_END_REQ;
391 TRACE(" hWndOwner(%p)\n", hWndOwner);
393 return hWndOwner;
397 /**************************************************************************
398 * GetOpenClipboardWindow (USER32.@)
400 HWND WINAPI GetOpenClipboardWindow(void)
402 HWND hWndOpen = 0;
404 SERVER_START_REQ( set_clipboard_info )
406 req->flags = 0;
407 if (!wine_server_call_err( req )) hWndOpen = reply->old_clipboard;
409 SERVER_END_REQ;
411 TRACE(" hWndClipWindow(%p)\n", hWndOpen);
413 return hWndOpen;
417 /**************************************************************************
418 * SetClipboardViewer (USER32.@)
420 HWND WINAPI SetClipboardViewer( HWND hWnd )
422 HWND hwndPrev = 0;
424 SERVER_START_REQ( set_clipboard_info )
426 req->flags = SET_CB_VIEWER;
427 req->viewer = WIN_GetFullHandle(hWnd);
429 if (wine_server_call_err( req ))
431 ERR("Failed to set clipboard.\n");
433 else
435 hwndPrev = reply->old_viewer;
438 SERVER_END_REQ;
440 TRACE("(%p): returning %p\n", hWnd, hwndPrev);
442 return hwndPrev;
446 /**************************************************************************
447 * GetClipboardViewer (USER32.@)
449 HWND WINAPI GetClipboardViewer(void)
451 HWND hWndViewer = 0;
453 SERVER_START_REQ( set_clipboard_info )
455 req->flags = 0;
456 if (!wine_server_call_err( req )) hWndViewer = reply->old_viewer;
458 SERVER_END_REQ;
460 TRACE(" hWndViewer=%p\n", hWndViewer);
462 return hWndViewer;
466 /**************************************************************************
467 * ChangeClipboardChain (USER32.@)
469 BOOL WINAPI ChangeClipboardChain(HWND hWnd, HWND hWndNext)
471 BOOL bRet = TRUE;
472 HWND hWndViewer = GetClipboardViewer();
474 if (hWndViewer)
476 if (WIN_GetFullHandle(hWnd) == hWndViewer)
477 SetClipboardViewer(WIN_GetFullHandle(hWndNext));
478 else
479 bRet = !SendMessageW(hWndViewer, WM_CHANGECBCHAIN, (WPARAM)hWnd, (LPARAM)hWndNext);
481 else
482 ERR("hWndViewer is lost\n");
484 return bRet;
488 /**************************************************************************
489 * SetClipboardData (USER.141)
491 HANDLE16 WINAPI SetClipboardData16(UINT16 wFormat, HANDLE16 hData)
493 CLIPBOARDINFO cbinfo;
494 HANDLE16 hResult = 0;
496 TRACE("(%04X, %04x) !\n", wFormat, hData);
498 /* If it's not owned, data can only be set if the format doesn't exists
499 and its rendering is not delayed */
500 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
501 (!(cbinfo.flags & CB_OWNER) && !hData))
503 WARN("Clipboard not owned by calling task. Operation failed.\n");
504 return 0;
507 if (USER_Driver->pSetClipboardData(wFormat, hData, 0, cbinfo.flags & CB_OWNER))
509 hResult = hData;
510 bCBHasChanged = TRUE;
513 return hResult;
517 /**************************************************************************
518 * SetClipboardData (USER32.@)
520 HANDLE WINAPI SetClipboardData(UINT wFormat, HANDLE hData)
522 CLIPBOARDINFO cbinfo;
523 HANDLE hResult = 0;
525 TRACE("(%04X, %p) !\n", wFormat, hData);
527 /* If it's not owned, data can only be set if the format isn't
528 available and its rendering is not delayed */
529 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
530 (!(cbinfo.flags & CB_OWNER) && !hData))
532 WARN("Clipboard not owned by calling task. Operation failed.\n");
533 return 0;
536 if (USER_Driver->pSetClipboardData(wFormat, 0, hData, cbinfo.flags & CB_OWNER))
538 hResult = hData;
539 bCBHasChanged = TRUE;
542 return hResult;
546 /**************************************************************************
547 * CountClipboardFormats (USER32.@)
549 INT WINAPI CountClipboardFormats(void)
551 INT count = USER_Driver->pCountClipboardFormats();
552 TRACE("returning %d\n", count);
553 return count;
557 /**************************************************************************
558 * EnumClipboardFormats (USER32.@)
560 UINT WINAPI EnumClipboardFormats(UINT wFormat)
562 CLIPBOARDINFO cbinfo;
564 TRACE("(%04X)\n", wFormat);
566 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
567 (~cbinfo.flags & CB_OPEN))
569 WARN("Clipboard not opened by calling task.\n");
570 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
571 return 0;
573 return USER_Driver->pEnumClipboardFormats(wFormat);
577 /**************************************************************************
578 * IsClipboardFormatAvailable (USER32.@)
580 BOOL WINAPI IsClipboardFormatAvailable(UINT wFormat)
582 BOOL bret = USER_Driver->pIsClipboardFormatAvailable(wFormat);
583 TRACE("%04x, returning %d\n", wFormat, bret);
584 return bret;
588 /**************************************************************************
589 * GetClipboardData (USER.142)
591 HANDLE16 WINAPI GetClipboardData16(UINT16 wFormat)
593 HANDLE16 hData = 0;
594 CLIPBOARDINFO cbinfo;
596 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
597 (~cbinfo.flags & CB_OPEN))
599 WARN("Clipboard not opened by calling task.\n");
600 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
601 return 0;
604 if (!USER_Driver->pGetClipboardData(wFormat, &hData, NULL)) hData = 0;
606 return hData;
610 /**************************************************************************
611 * GetClipboardData (USER32.@)
613 HANDLE WINAPI GetClipboardData(UINT wFormat)
615 HANDLE hData = 0;
616 CLIPBOARDINFO cbinfo;
618 TRACE("%04x\n", wFormat);
620 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
621 (~cbinfo.flags & CB_OPEN))
623 WARN("Clipboard not opened by calling task.\n");
624 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
625 return 0;
628 if (!USER_Driver->pGetClipboardData(wFormat, NULL, &hData)) hData = 0;
630 TRACE("returning %p\n", hData);
631 return hData;
635 /**************************************************************************
636 * GetPriorityClipboardFormat (USER32.@)
638 INT WINAPI GetPriorityClipboardFormat(UINT *list, INT nCount)
640 int i;
642 TRACE("()\n");
644 if(CountClipboardFormats() == 0)
645 return 0;
647 for (i = 0; i < nCount; i++)
648 if (IsClipboardFormatAvailable(list[i]))
649 return list[i];
651 return -1;
655 /**************************************************************************
656 * GetClipboardSequenceNumber (USER32.@)
657 * Supported on Win2k/Win98
658 * MSDN: Windows clipboard code keeps a serial number for the clipboard
659 * for each window station. The number is incremented whenever the
660 * contents change or are emptied.
661 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
663 DWORD WINAPI GetClipboardSequenceNumber(VOID)
665 DWORD seqno = 0;
667 SERVER_START_REQ( set_clipboard_info )
669 req->flags = 0;
670 if (!wine_server_call_err( req )) seqno = reply->seqno;
672 SERVER_END_REQ;
674 TRACE("returning %x\n", seqno);
675 return seqno;