push cc8bc80451cc24f4d7cf75168b569f0ebfe19547
[wine/hacks.git] / dlls / user32 / clipboard.c
blobb0ff10d4964fa75fb9fb59524ce7eb8bd391d031
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;
192 /**************************************************************************
193 * WIN32 Clipboard implementation
194 **************************************************************************/
196 /**************************************************************************
197 * RegisterClipboardFormatW (USER32.@)
199 UINT WINAPI RegisterClipboardFormatW(LPCWSTR FormatName)
201 return USER_Driver->pRegisterClipboardFormat(FormatName);
205 /**************************************************************************
206 * RegisterClipboardFormatA (USER32.@)
208 UINT WINAPI RegisterClipboardFormatA(LPCSTR formatName)
210 int len;
211 LPWSTR wFormat;
212 UINT ret;
214 len = MultiByteToWideChar(CP_ACP, 0, formatName, -1, NULL, 0);
215 wFormat = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
216 MultiByteToWideChar(CP_ACP, 0, formatName, -1, wFormat, len);
218 ret = RegisterClipboardFormatW(wFormat);
219 HeapFree(GetProcessHeap(), 0, wFormat);
220 return ret;
224 /**************************************************************************
225 * GetClipboardFormatNameW (USER32.@)
227 INT WINAPI GetClipboardFormatNameW(UINT wFormat, LPWSTR retStr, INT maxlen)
229 return USER_Driver->pGetClipboardFormatName(wFormat, retStr, maxlen);
233 /**************************************************************************
234 * GetClipboardFormatNameA (USER32.@)
236 INT WINAPI GetClipboardFormatNameA(UINT wFormat, LPSTR retStr, INT maxlen)
238 INT ret;
239 LPWSTR p = HeapAlloc( GetProcessHeap(), 0, maxlen*sizeof(WCHAR) );
240 if(p == NULL) return 0; /* FIXME: is this the correct failure value? */
242 ret = GetClipboardFormatNameW( wFormat, p, maxlen );
244 if (ret && maxlen > 0 && !WideCharToMultiByte( CP_ACP, 0, p, -1, retStr, maxlen, 0, 0))
245 retStr[maxlen-1] = 0;
246 HeapFree( GetProcessHeap(), 0, p );
247 return ret;
251 /**************************************************************************
252 * OpenClipboard (USER32.@)
254 * Note: Netscape uses NULL hWnd to open the clipboard.
256 BOOL WINAPI OpenClipboard( HWND hWnd )
258 BOOL bRet;
260 TRACE("(%p)...\n", hWnd);
262 bRet = CLIPBOARD_OpenClipboard(hWnd);
264 TRACE(" returning %i\n", bRet);
266 return bRet;
270 /**************************************************************************
271 * CloseClipboard (USER32.@)
273 BOOL WINAPI CloseClipboard(void)
275 BOOL bRet = FALSE;
277 TRACE("(%d)\n", bCBHasChanged);
279 if (CLIPBOARD_CloseClipboard())
281 if (bCBHasChanged)
283 HWND hWndViewer = GetClipboardViewer();
285 USER_Driver->pEndClipboardUpdate();
287 if (hWndViewer)
288 SendMessageW(hWndViewer, WM_DRAWCLIPBOARD, 0, 0);
290 bCBHasChanged = FALSE;
293 bRet = TRUE;
296 return bRet;
300 /**************************************************************************
301 * EmptyClipboard (USER32.@)
302 * Empties and acquires ownership of the clipboard
304 BOOL WINAPI EmptyClipboard(void)
306 CLIPBOARDINFO cbinfo;
308 TRACE("()\n");
310 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
311 ~cbinfo.flags & CB_OPEN)
313 WARN("Clipboard not opened by calling task!\n");
314 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
315 return FALSE;
318 /* Destroy private objects */
319 if (cbinfo.hWndOwner)
320 SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
322 /* Tell the driver to acquire the selection. The current owner
323 * will be signaled to delete it's own cache. */
325 /* Assign ownership of the clipboard to the current client. We do
326 * this before acquiring the selection so that when we do acquire the
327 * selection and the selection loser gets notified, it can check if
328 * it has lost the Wine clipboard ownership. If it did then it knows
329 * that a WM_DESTORYCLIPBOARD has already been sent. Otherwise it
330 * lost the selection to a X app and it should send the
331 * WM_DESTROYCLIPBOARD itself. */
332 CLIPBOARD_SetClipboardOwner(cbinfo.hWndOpen);
334 /* Acquire the selection. This will notify the previous owner
335 * to clear it's cache. */
336 USER_Driver->pAcquireClipboard(cbinfo.hWndOpen);
338 /* Empty the local cache */
339 USER_Driver->pEmptyClipboard(FALSE);
341 bCBHasChanged = TRUE;
343 return TRUE;
347 /**************************************************************************
348 * GetClipboardOwner (USER32.@)
349 * FIXME: Can't return the owner if the clipboard is owned by an external X-app
351 HWND WINAPI GetClipboardOwner(void)
353 HWND hWndOwner = 0;
355 SERVER_START_REQ( set_clipboard_info )
357 req->flags = 0;
358 if (!wine_server_call_err( req )) hWndOwner = wine_server_ptr_handle( reply->old_owner );
360 SERVER_END_REQ;
362 TRACE(" hWndOwner(%p)\n", hWndOwner);
364 return hWndOwner;
368 /**************************************************************************
369 * GetOpenClipboardWindow (USER32.@)
371 HWND WINAPI GetOpenClipboardWindow(void)
373 HWND hWndOpen = 0;
375 SERVER_START_REQ( set_clipboard_info )
377 req->flags = 0;
378 if (!wine_server_call_err( req )) hWndOpen = wine_server_ptr_handle( reply->old_clipboard );
380 SERVER_END_REQ;
382 TRACE(" hWndClipWindow(%p)\n", hWndOpen);
384 return hWndOpen;
388 /**************************************************************************
389 * SetClipboardViewer (USER32.@)
391 HWND WINAPI SetClipboardViewer( HWND hWnd )
393 HWND hwndPrev = 0;
395 SERVER_START_REQ( set_clipboard_info )
397 req->flags = SET_CB_VIEWER;
398 req->viewer = wine_server_user_handle( hWnd );
399 if (!wine_server_call_err( req ))
400 hwndPrev = wine_server_ptr_handle( reply->old_viewer );
402 SERVER_END_REQ;
404 TRACE("(%p): returning %p\n", hWnd, hwndPrev);
406 return hwndPrev;
410 /**************************************************************************
411 * GetClipboardViewer (USER32.@)
413 HWND WINAPI GetClipboardViewer(void)
415 HWND hWndViewer = 0;
417 SERVER_START_REQ( set_clipboard_info )
419 req->flags = 0;
420 if (!wine_server_call_err( req )) hWndViewer = wine_server_ptr_handle( reply->old_viewer );
422 SERVER_END_REQ;
424 TRACE(" hWndViewer=%p\n", hWndViewer);
426 return hWndViewer;
430 /**************************************************************************
431 * ChangeClipboardChain (USER32.@)
433 BOOL WINAPI ChangeClipboardChain(HWND hWnd, HWND hWndNext)
435 BOOL bRet = TRUE;
436 HWND hWndViewer = GetClipboardViewer();
438 if (hWndViewer)
440 if (WIN_GetFullHandle(hWnd) == hWndViewer)
441 SetClipboardViewer(WIN_GetFullHandle(hWndNext));
442 else
443 bRet = !SendMessageW(hWndViewer, WM_CHANGECBCHAIN, (WPARAM)hWnd, (LPARAM)hWndNext);
445 else
446 ERR("hWndViewer is lost\n");
448 return bRet;
452 /**************************************************************************
453 * SetClipboardData (USER.141)
455 HANDLE16 WINAPI SetClipboardData16(UINT16 wFormat, HANDLE16 hData)
457 CLIPBOARDINFO cbinfo;
458 HANDLE16 hResult = 0;
460 TRACE("(%04X, %04x) !\n", wFormat, hData);
462 /* If it's not owned, data can only be set if the format doesn't exists
463 and its rendering is not delayed */
464 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
465 (!(cbinfo.flags & CB_OWNER) && !hData))
467 WARN("Clipboard not owned by calling task. Operation failed.\n");
468 return 0;
471 if (USER_Driver->pSetClipboardData(wFormat, hData, 0, cbinfo.flags & CB_OWNER))
473 hResult = hData;
474 bCBHasChanged = TRUE;
477 return hResult;
481 /**************************************************************************
482 * SetClipboardData (USER32.@)
484 HANDLE WINAPI SetClipboardData(UINT wFormat, HANDLE hData)
486 CLIPBOARDINFO cbinfo;
487 HANDLE hResult = 0;
489 TRACE("(%04X, %p) !\n", wFormat, hData);
491 /* If it's not owned, data can only be set if the format isn't
492 available and its rendering is not delayed */
493 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
494 (!(cbinfo.flags & CB_OWNER) && !hData))
496 WARN("Clipboard not owned by calling task. Operation failed.\n");
497 return 0;
500 if (USER_Driver->pSetClipboardData(wFormat, 0, hData, cbinfo.flags & CB_OWNER))
502 hResult = hData;
503 bCBHasChanged = TRUE;
506 return hResult;
510 /**************************************************************************
511 * CountClipboardFormats (USER32.@)
513 INT WINAPI CountClipboardFormats(void)
515 INT count = USER_Driver->pCountClipboardFormats();
516 TRACE("returning %d\n", count);
517 return count;
521 /**************************************************************************
522 * EnumClipboardFormats (USER32.@)
524 UINT WINAPI EnumClipboardFormats(UINT wFormat)
526 CLIPBOARDINFO cbinfo;
528 TRACE("(%04X)\n", wFormat);
530 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
531 (~cbinfo.flags & CB_OPEN))
533 WARN("Clipboard not opened by calling task.\n");
534 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
535 return 0;
537 return USER_Driver->pEnumClipboardFormats(wFormat);
541 /**************************************************************************
542 * IsClipboardFormatAvailable (USER32.@)
544 BOOL WINAPI IsClipboardFormatAvailable(UINT wFormat)
546 BOOL bret = USER_Driver->pIsClipboardFormatAvailable(wFormat);
547 TRACE("%04x, returning %d\n", wFormat, bret);
548 return bret;
552 /**************************************************************************
553 * GetClipboardData (USER.142)
555 HANDLE16 WINAPI GetClipboardData16(UINT16 wFormat)
557 HANDLE16 hData = 0;
558 CLIPBOARDINFO cbinfo;
560 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
561 (~cbinfo.flags & CB_OPEN))
563 WARN("Clipboard not opened by calling task.\n");
564 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
565 return 0;
568 if (!USER_Driver->pGetClipboardData(wFormat, &hData, NULL)) hData = 0;
570 return hData;
574 /**************************************************************************
575 * GetClipboardData (USER32.@)
577 HANDLE WINAPI GetClipboardData(UINT wFormat)
579 HANDLE hData = 0;
580 CLIPBOARDINFO cbinfo;
582 TRACE("%04x\n", wFormat);
584 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
585 (~cbinfo.flags & CB_OPEN))
587 WARN("Clipboard not opened by calling task.\n");
588 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
589 return 0;
592 if (!USER_Driver->pGetClipboardData(wFormat, NULL, &hData)) hData = 0;
594 TRACE("returning %p\n", hData);
595 return hData;
599 /**************************************************************************
600 * GetPriorityClipboardFormat (USER32.@)
602 INT WINAPI GetPriorityClipboardFormat(UINT *list, INT nCount)
604 int i;
606 TRACE("()\n");
608 if(CountClipboardFormats() == 0)
609 return 0;
611 for (i = 0; i < nCount; i++)
612 if (IsClipboardFormatAvailable(list[i]))
613 return list[i];
615 return -1;
619 /**************************************************************************
620 * GetClipboardSequenceNumber (USER32.@)
621 * Supported on Win2k/Win98
622 * MSDN: Windows clipboard code keeps a serial number for the clipboard
623 * for each window station. The number is incremented whenever the
624 * contents change or are emptied.
625 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
627 DWORD WINAPI GetClipboardSequenceNumber(VOID)
629 DWORD seqno = 0;
631 SERVER_START_REQ( set_clipboard_info )
633 req->flags = 0;
634 if (!wine_server_call_err( req )) seqno = reply->seqno;
636 SERVER_END_REQ;
638 TRACE("returning %x\n", seqno);
639 return seqno;