gdiplus: Implement ResetWorldTransform metafile playback/recording.
[wine.git] / dlls / user32 / clipboard.c
blob47647845ee1d872a011c49e089bab8abc9fa3c63
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 "user_private.h"
50 #include "win.h"
52 #include "wine/debug.h"
53 #include "wine/unicode.h"
54 #include "wine/server.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(clipboard);
58 #define CF_REGFORMATBASE 0xC000
60 typedef struct
62 HWND hWndOpen;
63 HWND hWndOwner;
64 UINT flags;
65 } CLIPBOARDINFO, *LPCLIPBOARDINFO;
68 * Indicates if data has changed since open.
70 static BOOL bCBHasChanged = FALSE;
73 /**************************************************************************
74 * CLIPBOARD_GetClipboardInfo
76 static BOOL CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo)
78 BOOL bRet;
80 SERVER_START_REQ( set_clipboard_info )
82 req->flags = 0;
84 if (((bRet = !wine_server_call_err( req ))))
86 cbInfo->hWndOpen = wine_server_ptr_handle( reply->old_clipboard );
87 cbInfo->hWndOwner = wine_server_ptr_handle( reply->old_owner );
88 cbInfo->flags = reply->flags;
91 SERVER_END_REQ;
93 return bRet;
97 /**************************************************************************
98 * CLIPBOARD_ReleaseOwner
100 void CLIPBOARD_ReleaseOwner( HWND hwnd )
102 SERVER_START_REQ( set_clipboard_info )
104 req->flags = SET_CB_RELOWNER | SET_CB_SEQNO;
105 req->owner = wine_server_user_handle( hwnd );
106 wine_server_call( req );
108 SERVER_END_REQ;
112 /**************************************************************************
113 * CLIPBOARD_SetClipboardViewer
115 static HWND CLIPBOARD_SetClipboardViewer( HWND hWnd )
117 HWND hwndPrev = 0;
119 SERVER_START_REQ( set_clipboard_info )
121 req->flags = SET_CB_VIEWER;
122 req->viewer = wine_server_user_handle( hWnd );
123 if (!wine_server_call_err( req ))
124 hwndPrev = wine_server_ptr_handle( reply->old_viewer );
126 SERVER_END_REQ;
128 return hwndPrev;
131 /**************************************************************************
132 * WIN32 Clipboard implementation
133 **************************************************************************/
135 /**************************************************************************
136 * RegisterClipboardFormatW (USER32.@)
138 UINT WINAPI RegisterClipboardFormatW( LPCWSTR name )
140 return GlobalAddAtomW( name );
144 /**************************************************************************
145 * RegisterClipboardFormatA (USER32.@)
147 UINT WINAPI RegisterClipboardFormatA( LPCSTR name )
149 return GlobalAddAtomA( name );
153 /**************************************************************************
154 * GetClipboardFormatNameW (USER32.@)
156 INT WINAPI GetClipboardFormatNameW(UINT wFormat, LPWSTR retStr, INT maxlen)
158 if (wFormat < MAXINTATOM) return 0;
159 return GlobalGetAtomNameW( wFormat, retStr, maxlen );
163 /**************************************************************************
164 * GetClipboardFormatNameA (USER32.@)
166 INT WINAPI GetClipboardFormatNameA(UINT wFormat, LPSTR retStr, INT maxlen)
168 if (wFormat < MAXINTATOM) return 0;
169 return GlobalGetAtomNameA( wFormat, retStr, maxlen );
173 /**************************************************************************
174 * OpenClipboard (USER32.@)
176 * Note: Netscape uses NULL hWnd to open the clipboard.
178 BOOL WINAPI OpenClipboard( HWND hWnd )
180 BOOL bRet;
181 UINT flags;
183 TRACE("(%p)...\n", hWnd);
185 SERVER_START_REQ( set_clipboard_info )
187 req->flags = SET_CB_OPEN;
188 req->clipboard = wine_server_user_handle( hWnd );
189 if ((bRet = !wine_server_call( req )))
190 flags = reply->flags;
192 SERVER_END_REQ;
194 if (bRet && !(flags & CB_PROCESS))
196 bCBHasChanged = FALSE;
199 TRACE(" returning %i\n", bRet);
201 return bRet;
205 /**************************************************************************
206 * CloseClipboard (USER32.@)
208 BOOL WINAPI CloseClipboard(void)
210 HWND viewer = 0;
211 UINT flags;
212 BOOL ret;
214 TRACE("() Changed=%d\n", bCBHasChanged);
216 SERVER_START_REQ( set_clipboard_info )
218 req->flags = SET_CB_CLOSE;
219 if (bCBHasChanged) req->flags |= SET_CB_SEQNO;
220 if ((ret = !wine_server_call_err( req )))
222 viewer = wine_server_ptr_handle( reply->old_viewer );
223 flags = reply->flags;
226 SERVER_END_REQ;
228 if (!ret) return FALSE;
230 if (bCBHasChanged && (flags & CB_PROCESS))
232 USER_Driver->pEndClipboardUpdate();
233 if (viewer) SendNotifyMessageW(viewer, WM_DRAWCLIPBOARD, (WPARAM) GetClipboardOwner(), 0);
235 bCBHasChanged = FALSE;
236 return TRUE;
240 /**************************************************************************
241 * EmptyClipboard (USER32.@)
242 * Empties and acquires ownership of the clipboard
244 BOOL WINAPI EmptyClipboard(void)
246 BOOL ret;
247 HWND owner = GetClipboardOwner();
249 TRACE("()\n");
251 if (owner) SendMessageTimeoutW( owner, WM_DESTROYCLIPBOARD, 0, 0, SMTO_ABORTIFHUNG, 5000, NULL );
253 SERVER_START_REQ( empty_clipboard )
255 ret = !wine_server_call_err( req );
257 SERVER_END_REQ;
259 if (ret)
261 USER_Driver->pEmptyClipboard();
262 bCBHasChanged = TRUE;
264 return ret;
268 /**************************************************************************
269 * GetClipboardOwner (USER32.@)
270 * FIXME: Can't return the owner if the clipboard is owned by an external X-app
272 HWND WINAPI GetClipboardOwner(void)
274 HWND hWndOwner = 0;
276 SERVER_START_REQ( set_clipboard_info )
278 req->flags = 0;
279 if (!wine_server_call_err( req )) hWndOwner = wine_server_ptr_handle( reply->old_owner );
281 SERVER_END_REQ;
283 TRACE(" hWndOwner(%p)\n", hWndOwner);
285 return hWndOwner;
289 /**************************************************************************
290 * GetOpenClipboardWindow (USER32.@)
292 HWND WINAPI GetOpenClipboardWindow(void)
294 HWND hWndOpen = 0;
296 SERVER_START_REQ( set_clipboard_info )
298 req->flags = 0;
299 if (!wine_server_call_err( req )) hWndOpen = wine_server_ptr_handle( reply->old_clipboard );
301 SERVER_END_REQ;
303 TRACE(" hWndClipWindow(%p)\n", hWndOpen);
305 return hWndOpen;
309 /**************************************************************************
310 * SetClipboardViewer (USER32.@)
312 HWND WINAPI SetClipboardViewer( HWND hWnd )
314 HWND hwndPrev = CLIPBOARD_SetClipboardViewer(hWnd);
316 if (hWnd)
317 SendNotifyMessageW(hWnd, WM_DRAWCLIPBOARD, (WPARAM) GetClipboardOwner(), 0);
318 TRACE("(%p): returning %p\n", hWnd, hwndPrev);
320 return hwndPrev;
324 /**************************************************************************
325 * GetClipboardViewer (USER32.@)
327 HWND WINAPI GetClipboardViewer(void)
329 HWND hWndViewer = 0;
331 SERVER_START_REQ( set_clipboard_info )
333 req->flags = 0;
334 if (!wine_server_call_err( req )) hWndViewer = wine_server_ptr_handle( reply->old_viewer );
336 SERVER_END_REQ;
338 TRACE(" hWndViewer=%p\n", hWndViewer);
340 return hWndViewer;
344 /**************************************************************************
345 * ChangeClipboardChain (USER32.@)
347 BOOL WINAPI ChangeClipboardChain(HWND hWnd, HWND hWndNext)
349 BOOL bRet = TRUE;
350 HWND hWndViewer = GetClipboardViewer();
352 if (hWndViewer)
354 if (WIN_GetFullHandle(hWnd) == hWndViewer)
355 CLIPBOARD_SetClipboardViewer(WIN_GetFullHandle(hWndNext));
356 else
357 bRet = !SendMessageW(hWndViewer, WM_CHANGECBCHAIN, (WPARAM)hWnd, (LPARAM)hWndNext);
359 else
360 ERR("hWndViewer is lost\n");
362 return bRet;
366 /**************************************************************************
367 * SetClipboardData (USER32.@)
369 HANDLE WINAPI SetClipboardData(UINT wFormat, HANDLE hData)
371 CLIPBOARDINFO cbinfo;
372 HANDLE hResult = 0;
374 TRACE("(%04X, %p) !\n", wFormat, hData);
376 if (!wFormat)
378 SetLastError( ERROR_CLIPBOARD_NOT_OPEN );
379 return 0;
382 /* If it's not owned, data can only be set if the format isn't
383 available and its rendering is not delayed */
384 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
385 (!(cbinfo.flags & CB_OWNER) && !hData))
387 WARN("Clipboard not owned by calling task. Operation failed.\n");
388 return 0;
391 if (USER_Driver->pSetClipboardData(wFormat, hData, cbinfo.flags & CB_OWNER))
393 hResult = hData;
394 bCBHasChanged = TRUE;
397 return hResult;
401 /**************************************************************************
402 * CountClipboardFormats (USER32.@)
404 INT WINAPI CountClipboardFormats(void)
406 INT count = USER_Driver->pCountClipboardFormats();
407 TRACE("returning %d\n", count);
408 return count;
412 /**************************************************************************
413 * EnumClipboardFormats (USER32.@)
415 UINT WINAPI EnumClipboardFormats(UINT wFormat)
417 CLIPBOARDINFO cbinfo;
419 TRACE("(%04X)\n", wFormat);
421 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
422 (~cbinfo.flags & CB_OPEN))
424 WARN("Clipboard not opened by calling task.\n");
425 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
426 return 0;
428 return USER_Driver->pEnumClipboardFormats(wFormat);
432 /**************************************************************************
433 * IsClipboardFormatAvailable (USER32.@)
435 BOOL WINAPI IsClipboardFormatAvailable(UINT wFormat)
437 BOOL bret = USER_Driver->pIsClipboardFormatAvailable(wFormat);
438 TRACE("%04x, returning %d\n", wFormat, bret);
439 return bret;
443 /**************************************************************************
444 * GetClipboardData (USER32.@)
446 HANDLE WINAPI GetClipboardData(UINT wFormat)
448 HANDLE hData = 0;
449 CLIPBOARDINFO cbinfo;
451 TRACE("%04x\n", wFormat);
453 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
454 (~cbinfo.flags & CB_OPEN))
456 WARN("Clipboard not opened by calling task.\n");
457 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
458 return 0;
461 hData = USER_Driver->pGetClipboardData( wFormat );
463 TRACE("returning %p\n", hData);
464 return hData;
468 /**************************************************************************
469 * GetPriorityClipboardFormat (USER32.@)
471 INT WINAPI GetPriorityClipboardFormat(UINT *list, INT nCount)
473 int i;
475 TRACE("()\n");
477 if(CountClipboardFormats() == 0)
478 return 0;
480 for (i = 0; i < nCount; i++)
481 if (IsClipboardFormatAvailable(list[i]))
482 return list[i];
484 return -1;
488 /**************************************************************************
489 * GetClipboardSequenceNumber (USER32.@)
490 * Supported on Win2k/Win98
491 * MSDN: Windows clipboard code keeps a serial number for the clipboard
492 * for each window station. The number is incremented whenever the
493 * contents change or are emptied.
494 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
496 DWORD WINAPI GetClipboardSequenceNumber(VOID)
498 DWORD seqno = 0;
500 SERVER_START_REQ( set_clipboard_info )
502 req->flags = 0;
503 if (!wine_server_call_err( req )) seqno = reply->seqno;
505 SERVER_END_REQ;
507 TRACE("returning %x\n", seqno);
508 return seqno;
511 /**************************************************************************
512 * AddClipboardFormatListener (USER32.@)
514 BOOL WINAPI AddClipboardFormatListener(HWND hwnd)
516 FIXME("%p: stub\n", hwnd);
517 return TRUE;
520 /**************************************************************************
521 * RemoveClipboardFormatListener (USER32.@)
523 BOOL WINAPI RemoveClipboardFormatListener(HWND hwnd)
525 FIXME("%p: stub\n", hwnd);
526 return TRUE;