push 161279dfb9970ca3f986ab3c7f0e305cdc7200d2
[wine/hacks.git] / dlls / user32 / clipboard.c
blob860e73ff21fb55b96f4f88d287ae90165c8c60eb
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;
86 TRACE(" hWnd(%p)\n", hWnd);
88 SERVER_START_REQ( set_clipboard_info )
90 req->flags = SET_CB_OWNER;
91 req->owner = wine_server_user_handle( hWnd );
92 bRet = !wine_server_call_err( req );
94 SERVER_END_REQ;
96 return bRet;
100 /**************************************************************************
101 * CLIPBOARD_GetClipboardInfo
103 static BOOL CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo)
105 BOOL bRet;
107 SERVER_START_REQ( set_clipboard_info )
109 req->flags = 0;
111 if (((bRet = !wine_server_call_err( req ))))
113 cbInfo->hWndOpen = wine_server_ptr_handle( reply->old_clipboard );
114 cbInfo->hWndOwner = wine_server_ptr_handle( reply->old_owner );
115 cbInfo->hWndViewer = wine_server_ptr_handle( reply->old_viewer );
116 cbInfo->seqno = reply->seqno;
117 cbInfo->flags = reply->flags;
120 SERVER_END_REQ;
122 return bRet;
126 /**************************************************************************
127 * CLIPBOARD_ReleaseOwner
129 BOOL CLIPBOARD_ReleaseOwner(void)
131 BOOL bRet = FALSE;
133 SERVER_START_REQ( set_clipboard_info )
135 req->flags = SET_CB_RELOWNER | SET_CB_SEQNO;
137 if (wine_server_call_err( req ))
139 ERR("Failed to set clipboard.\n");
141 else
143 bRet = TRUE;
146 SERVER_END_REQ;
148 return bRet;
152 /**************************************************************************
153 * CLIPBOARD_OpenClipboard
155 static BOOL CLIPBOARD_OpenClipboard(HWND hWnd)
157 BOOL bRet;
159 SERVER_START_REQ( set_clipboard_info )
161 req->flags = SET_CB_OPEN;
162 req->clipboard = wine_server_user_handle( hWnd );
163 bRet = !wine_server_call( req );
165 SERVER_END_REQ;
167 return bRet;
171 /**************************************************************************
172 * CLIPBOARD_CloseClipboard
174 static BOOL CLIPBOARD_CloseClipboard(void)
176 BOOL bRet;
178 TRACE(" Changed=%d\n", bCBHasChanged);
180 SERVER_START_REQ( set_clipboard_info )
182 req->flags = SET_CB_CLOSE;
183 if (bCBHasChanged) req->flags |= SET_CB_SEQNO;
184 bRet = !wine_server_call_err( req );
186 SERVER_END_REQ;
188 return bRet;
191 /**************************************************************************
192 * CLIPBOARD_SetClipboardViewer
194 static HWND CLIPBOARD_SetClipboardViewer( HWND hWnd )
196 HWND hwndPrev = 0;
198 SERVER_START_REQ( set_clipboard_info )
200 req->flags = SET_CB_VIEWER;
201 req->viewer = wine_server_user_handle( hWnd );
202 if (!wine_server_call_err( req ))
203 hwndPrev = wine_server_ptr_handle( reply->old_viewer );
205 SERVER_END_REQ;
207 return hwndPrev;
210 /**************************************************************************
211 * WIN32 Clipboard implementation
212 **************************************************************************/
214 /**************************************************************************
215 * RegisterClipboardFormatW (USER32.@)
217 UINT WINAPI RegisterClipboardFormatW(LPCWSTR FormatName)
219 return USER_Driver->pRegisterClipboardFormat(FormatName);
223 /**************************************************************************
224 * RegisterClipboardFormatA (USER32.@)
226 UINT WINAPI RegisterClipboardFormatA(LPCSTR formatName)
228 int len;
229 LPWSTR wFormat;
230 UINT ret;
232 len = MultiByteToWideChar(CP_ACP, 0, formatName, -1, NULL, 0);
233 wFormat = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
234 MultiByteToWideChar(CP_ACP, 0, formatName, -1, wFormat, len);
236 ret = RegisterClipboardFormatW(wFormat);
237 HeapFree(GetProcessHeap(), 0, wFormat);
238 return ret;
242 /**************************************************************************
243 * GetClipboardFormatNameW (USER32.@)
245 INT WINAPI GetClipboardFormatNameW(UINT wFormat, LPWSTR retStr, INT maxlen)
247 return USER_Driver->pGetClipboardFormatName(wFormat, retStr, maxlen);
251 /**************************************************************************
252 * GetClipboardFormatNameA (USER32.@)
254 INT WINAPI GetClipboardFormatNameA(UINT wFormat, LPSTR retStr, INT maxlen)
256 INT ret;
257 LPWSTR p = HeapAlloc( GetProcessHeap(), 0, maxlen*sizeof(WCHAR) );
258 if(p == NULL) return 0; /* FIXME: is this the correct failure value? */
260 ret = GetClipboardFormatNameW( wFormat, p, maxlen );
262 if (ret && maxlen > 0 && !WideCharToMultiByte( CP_ACP, 0, p, -1, retStr, maxlen, 0, 0))
263 retStr[maxlen-1] = 0;
264 HeapFree( GetProcessHeap(), 0, p );
265 return ret;
269 /**************************************************************************
270 * OpenClipboard (USER32.@)
272 * Note: Netscape uses NULL hWnd to open the clipboard.
274 BOOL WINAPI OpenClipboard( HWND hWnd )
276 BOOL bRet;
278 TRACE("(%p)...\n", hWnd);
280 bRet = CLIPBOARD_OpenClipboard(hWnd);
282 TRACE(" returning %i\n", bRet);
284 return bRet;
288 /**************************************************************************
289 * CloseClipboard (USER32.@)
291 BOOL WINAPI CloseClipboard(void)
293 BOOL bRet = FALSE;
295 TRACE("(%d)\n", bCBHasChanged);
297 if (CLIPBOARD_CloseClipboard())
299 if (bCBHasChanged)
301 HWND hWndViewer = GetClipboardViewer();
303 USER_Driver->pEndClipboardUpdate();
305 if (hWndViewer)
306 SendMessageW(hWndViewer, WM_DRAWCLIPBOARD, (WPARAM) GetClipboardOwner(), 0);
308 bCBHasChanged = FALSE;
311 bRet = TRUE;
314 return bRet;
318 /**************************************************************************
319 * EmptyClipboard (USER32.@)
320 * Empties and acquires ownership of the clipboard
322 BOOL WINAPI EmptyClipboard(void)
324 CLIPBOARDINFO cbinfo;
326 TRACE("()\n");
328 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
329 ~cbinfo.flags & CB_OPEN)
331 WARN("Clipboard not opened by calling task!\n");
332 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
333 return FALSE;
336 /* Destroy private objects */
337 if (cbinfo.hWndOwner)
338 SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
340 /* Tell the driver to acquire the selection. The current owner
341 * will be signaled to delete it's own cache. */
343 /* Assign ownership of the clipboard to the current client. We do
344 * this before acquiring the selection so that when we do acquire the
345 * selection and the selection loser gets notified, it can check if
346 * it has lost the Wine clipboard ownership. If it did then it knows
347 * that a WM_DESTORYCLIPBOARD has already been sent. Otherwise it
348 * lost the selection to a X app and it should send the
349 * WM_DESTROYCLIPBOARD itself. */
350 CLIPBOARD_SetClipboardOwner(cbinfo.hWndOpen);
352 /* Acquire the selection. This will notify the previous owner
353 * to clear it's cache. */
354 USER_Driver->pAcquireClipboard(cbinfo.hWndOpen);
356 /* Empty the local cache */
357 USER_Driver->pEmptyClipboard(FALSE);
359 bCBHasChanged = TRUE;
361 return TRUE;
365 /**************************************************************************
366 * GetClipboardOwner (USER32.@)
367 * FIXME: Can't return the owner if the clipboard is owned by an external X-app
369 HWND WINAPI GetClipboardOwner(void)
371 HWND hWndOwner = 0;
373 SERVER_START_REQ( set_clipboard_info )
375 req->flags = 0;
376 if (!wine_server_call_err( req )) hWndOwner = wine_server_ptr_handle( reply->old_owner );
378 SERVER_END_REQ;
380 TRACE(" hWndOwner(%p)\n", hWndOwner);
382 return hWndOwner;
386 /**************************************************************************
387 * GetOpenClipboardWindow (USER32.@)
389 HWND WINAPI GetOpenClipboardWindow(void)
391 HWND hWndOpen = 0;
393 SERVER_START_REQ( set_clipboard_info )
395 req->flags = 0;
396 if (!wine_server_call_err( req )) hWndOpen = wine_server_ptr_handle( reply->old_clipboard );
398 SERVER_END_REQ;
400 TRACE(" hWndClipWindow(%p)\n", hWndOpen);
402 return hWndOpen;
406 /**************************************************************************
407 * SetClipboardViewer (USER32.@)
409 HWND WINAPI SetClipboardViewer( HWND hWnd )
411 HWND hwndPrev = CLIPBOARD_SetClipboardViewer(hWnd);
413 if (hWnd)
414 SendMessageW(hWnd, WM_DRAWCLIPBOARD, (WPARAM) GetClipboardOwner(), 0);
415 TRACE("(%p): returning %p\n", hWnd, hwndPrev);
417 return hwndPrev;
421 /**************************************************************************
422 * GetClipboardViewer (USER32.@)
424 HWND WINAPI GetClipboardViewer(void)
426 HWND hWndViewer = 0;
428 SERVER_START_REQ( set_clipboard_info )
430 req->flags = 0;
431 if (!wine_server_call_err( req )) hWndViewer = wine_server_ptr_handle( reply->old_viewer );
433 SERVER_END_REQ;
435 TRACE(" hWndViewer=%p\n", hWndViewer);
437 return hWndViewer;
441 /**************************************************************************
442 * ChangeClipboardChain (USER32.@)
444 BOOL WINAPI ChangeClipboardChain(HWND hWnd, HWND hWndNext)
446 BOOL bRet = TRUE;
447 HWND hWndViewer = GetClipboardViewer();
449 if (hWndViewer)
451 if (WIN_GetFullHandle(hWnd) == hWndViewer)
452 CLIPBOARD_SetClipboardViewer(WIN_GetFullHandle(hWndNext));
453 else
454 bRet = !SendMessageW(hWndViewer, WM_CHANGECBCHAIN, (WPARAM)hWnd, (LPARAM)hWndNext);
456 else
457 ERR("hWndViewer is lost\n");
459 return bRet;
463 /**************************************************************************
464 * SetClipboardData (USER.141)
466 HANDLE16 WINAPI SetClipboardData16(UINT16 wFormat, HANDLE16 hData)
468 CLIPBOARDINFO cbinfo;
469 HANDLE16 hResult = 0;
471 TRACE("(%04X, %04x) !\n", wFormat, hData);
473 /* If it's not owned, data can only be set if the format doesn't exists
474 and its rendering is not delayed */
475 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
476 (!(cbinfo.flags & CB_OWNER) && !hData))
478 WARN("Clipboard not owned by calling task. Operation failed.\n");
479 return 0;
482 if (USER_Driver->pSetClipboardData(wFormat, hData, 0, cbinfo.flags & CB_OWNER))
484 hResult = hData;
485 bCBHasChanged = TRUE;
488 return hResult;
492 /**************************************************************************
493 * SetClipboardData (USER32.@)
495 HANDLE WINAPI SetClipboardData(UINT wFormat, HANDLE hData)
497 CLIPBOARDINFO cbinfo;
498 HANDLE hResult = 0;
500 TRACE("(%04X, %p) !\n", wFormat, hData);
502 /* If it's not owned, data can only be set if the format isn't
503 available and its rendering is not delayed */
504 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
505 (!(cbinfo.flags & CB_OWNER) && !hData))
507 WARN("Clipboard not owned by calling task. Operation failed.\n");
508 return 0;
511 if (USER_Driver->pSetClipboardData(wFormat, 0, hData, cbinfo.flags & CB_OWNER))
513 hResult = hData;
514 bCBHasChanged = TRUE;
517 return hResult;
521 /**************************************************************************
522 * CountClipboardFormats (USER32.@)
524 INT WINAPI CountClipboardFormats(void)
526 INT count = USER_Driver->pCountClipboardFormats();
527 TRACE("returning %d\n", count);
528 return count;
532 /**************************************************************************
533 * EnumClipboardFormats (USER32.@)
535 UINT WINAPI EnumClipboardFormats(UINT wFormat)
537 CLIPBOARDINFO cbinfo;
539 TRACE("(%04X)\n", wFormat);
541 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
542 (~cbinfo.flags & CB_OPEN))
544 WARN("Clipboard not opened by calling task.\n");
545 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
546 return 0;
548 return USER_Driver->pEnumClipboardFormats(wFormat);
552 /**************************************************************************
553 * IsClipboardFormatAvailable (USER32.@)
555 BOOL WINAPI IsClipboardFormatAvailable(UINT wFormat)
557 BOOL bret = USER_Driver->pIsClipboardFormatAvailable(wFormat);
558 TRACE("%04x, returning %d\n", wFormat, bret);
559 return bret;
563 /**************************************************************************
564 * GetClipboardData (USER.142)
566 HANDLE16 WINAPI GetClipboardData16(UINT16 wFormat)
568 HANDLE16 hData = 0;
569 CLIPBOARDINFO cbinfo;
571 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
572 (~cbinfo.flags & CB_OPEN))
574 WARN("Clipboard not opened by calling task.\n");
575 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
576 return 0;
579 if (!USER_Driver->pGetClipboardData(wFormat, &hData, NULL)) hData = 0;
581 return hData;
585 /**************************************************************************
586 * GetClipboardData (USER32.@)
588 HANDLE WINAPI GetClipboardData(UINT wFormat)
590 HANDLE hData = 0;
591 CLIPBOARDINFO cbinfo;
593 TRACE("%04x\n", wFormat);
595 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
596 (~cbinfo.flags & CB_OPEN))
598 WARN("Clipboard not opened by calling task.\n");
599 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
600 return 0;
603 if (!USER_Driver->pGetClipboardData(wFormat, NULL, &hData)) hData = 0;
605 TRACE("returning %p\n", hData);
606 return hData;
610 /**************************************************************************
611 * GetPriorityClipboardFormat (USER32.@)
613 INT WINAPI GetPriorityClipboardFormat(UINT *list, INT nCount)
615 int i;
617 TRACE("()\n");
619 if(CountClipboardFormats() == 0)
620 return 0;
622 for (i = 0; i < nCount; i++)
623 if (IsClipboardFormatAvailable(list[i]))
624 return list[i];
626 return -1;
630 /**************************************************************************
631 * GetClipboardSequenceNumber (USER32.@)
632 * Supported on Win2k/Win98
633 * MSDN: Windows clipboard code keeps a serial number for the clipboard
634 * for each window station. The number is incremented whenever the
635 * contents change or are emptied.
636 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
638 DWORD WINAPI GetClipboardSequenceNumber(VOID)
640 DWORD seqno = 0;
642 SERVER_START_REQ( set_clipboard_info )
644 req->flags = 0;
645 if (!wine_server_call_err( req )) seqno = reply->seqno;
647 SERVER_END_REQ;
649 TRACE("returning %x\n", seqno);
650 return seqno;