server: Add a separate request to set the clipboard viewer.
[wine.git] / dlls / user32 / clipboard.c
blobdcb317210a9592368d3afa0605ae65bcfc267430
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 * WIN32 Clipboard implementation
114 **************************************************************************/
116 /**************************************************************************
117 * RegisterClipboardFormatW (USER32.@)
119 UINT WINAPI RegisterClipboardFormatW( LPCWSTR name )
121 return GlobalAddAtomW( name );
125 /**************************************************************************
126 * RegisterClipboardFormatA (USER32.@)
128 UINT WINAPI RegisterClipboardFormatA( LPCSTR name )
130 return GlobalAddAtomA( name );
134 /**************************************************************************
135 * GetClipboardFormatNameW (USER32.@)
137 INT WINAPI GetClipboardFormatNameW(UINT wFormat, LPWSTR retStr, INT maxlen)
139 if (wFormat < MAXINTATOM) return 0;
140 return GlobalGetAtomNameW( wFormat, retStr, maxlen );
144 /**************************************************************************
145 * GetClipboardFormatNameA (USER32.@)
147 INT WINAPI GetClipboardFormatNameA(UINT wFormat, LPSTR retStr, INT maxlen)
149 if (wFormat < MAXINTATOM) return 0;
150 return GlobalGetAtomNameA( wFormat, retStr, maxlen );
154 /**************************************************************************
155 * OpenClipboard (USER32.@)
157 BOOL WINAPI OpenClipboard( HWND hwnd )
159 BOOL ret;
161 TRACE( "%p\n", hwnd );
163 SERVER_START_REQ( open_clipboard )
165 req->window = wine_server_user_handle( hwnd );
166 if ((ret = !wine_server_call( req )))
168 if (!reply->owner) bCBHasChanged = FALSE;
171 SERVER_END_REQ;
173 return ret;
177 /**************************************************************************
178 * CloseClipboard (USER32.@)
180 BOOL WINAPI CloseClipboard(void)
182 HWND viewer = 0;
183 BOOL ret, owner = FALSE;
185 TRACE("() Changed=%d\n", bCBHasChanged);
187 SERVER_START_REQ( close_clipboard )
189 req->changed = bCBHasChanged;
190 if ((ret = !wine_server_call_err( req )))
192 viewer = wine_server_ptr_handle( reply->viewer );
193 owner = reply->owner;
196 SERVER_END_REQ;
198 if (!ret) return FALSE;
200 if (bCBHasChanged && owner)
202 USER_Driver->pEndClipboardUpdate();
203 if (viewer) SendNotifyMessageW(viewer, WM_DRAWCLIPBOARD, (WPARAM) GetClipboardOwner(), 0);
205 bCBHasChanged = FALSE;
206 return TRUE;
210 /**************************************************************************
211 * EmptyClipboard (USER32.@)
212 * Empties and acquires ownership of the clipboard
214 BOOL WINAPI EmptyClipboard(void)
216 BOOL ret;
217 HWND owner = GetClipboardOwner();
219 TRACE("()\n");
221 if (owner) SendMessageTimeoutW( owner, WM_DESTROYCLIPBOARD, 0, 0, SMTO_ABORTIFHUNG, 5000, NULL );
223 SERVER_START_REQ( empty_clipboard )
225 ret = !wine_server_call_err( req );
227 SERVER_END_REQ;
229 if (ret)
231 USER_Driver->pEmptyClipboard();
232 bCBHasChanged = TRUE;
234 return ret;
238 /**************************************************************************
239 * GetClipboardOwner (USER32.@)
240 * FIXME: Can't return the owner if the clipboard is owned by an external X-app
242 HWND WINAPI GetClipboardOwner(void)
244 HWND hWndOwner = 0;
246 SERVER_START_REQ( set_clipboard_info )
248 req->flags = 0;
249 if (!wine_server_call_err( req )) hWndOwner = wine_server_ptr_handle( reply->old_owner );
251 SERVER_END_REQ;
253 TRACE(" hWndOwner(%p)\n", hWndOwner);
255 return hWndOwner;
259 /**************************************************************************
260 * GetOpenClipboardWindow (USER32.@)
262 HWND WINAPI GetOpenClipboardWindow(void)
264 HWND hWndOpen = 0;
266 SERVER_START_REQ( set_clipboard_info )
268 req->flags = 0;
269 if (!wine_server_call_err( req )) hWndOpen = wine_server_ptr_handle( reply->old_clipboard );
271 SERVER_END_REQ;
273 TRACE(" hWndClipWindow(%p)\n", hWndOpen);
275 return hWndOpen;
279 /**************************************************************************
280 * SetClipboardViewer (USER32.@)
282 HWND WINAPI SetClipboardViewer( HWND hwnd )
284 HWND prev = 0, owner = 0;
286 SERVER_START_REQ( set_clipboard_viewer )
288 req->viewer = wine_server_user_handle( hwnd );
289 if (!wine_server_call_err( req ))
291 prev = wine_server_ptr_handle( reply->old_viewer );
292 owner = wine_server_ptr_handle( reply->owner );
295 SERVER_END_REQ;
297 if (hwnd) SendNotifyMessageW( hwnd, WM_DRAWCLIPBOARD, (WPARAM)owner, 0 );
299 TRACE( "(%p): returning %p\n", hwnd, prev );
300 return prev;
304 /**************************************************************************
305 * GetClipboardViewer (USER32.@)
307 HWND WINAPI GetClipboardViewer(void)
309 HWND hWndViewer = 0;
311 SERVER_START_REQ( set_clipboard_info )
313 req->flags = 0;
314 if (!wine_server_call_err( req )) hWndViewer = wine_server_ptr_handle( reply->old_viewer );
316 SERVER_END_REQ;
318 TRACE(" hWndViewer=%p\n", hWndViewer);
320 return hWndViewer;
324 /**************************************************************************
325 * ChangeClipboardChain (USER32.@)
327 BOOL WINAPI ChangeClipboardChain( HWND hwnd, HWND next )
329 NTSTATUS status;
330 HWND viewer;
332 if (!hwnd) return FALSE;
334 SERVER_START_REQ( set_clipboard_viewer )
336 req->viewer = wine_server_user_handle( next );
337 req->previous = wine_server_user_handle( hwnd );
338 status = wine_server_call( req );
339 viewer = wine_server_ptr_handle( reply->old_viewer );
341 SERVER_END_REQ;
343 if (status == STATUS_PENDING)
344 return !SendMessageW( viewer, WM_CHANGECBCHAIN, (WPARAM)hwnd, (LPARAM)next );
346 if (status) SetLastError( RtlNtStatusToDosError( status ));
347 return !status;
351 /**************************************************************************
352 * SetClipboardData (USER32.@)
354 HANDLE WINAPI SetClipboardData(UINT wFormat, HANDLE hData)
356 CLIPBOARDINFO cbinfo;
357 HANDLE hResult = 0;
359 TRACE("(%04X, %p) !\n", wFormat, hData);
361 if (!wFormat)
363 SetLastError( ERROR_CLIPBOARD_NOT_OPEN );
364 return 0;
367 /* If it's not owned, data can only be set if the format isn't
368 available and its rendering is not delayed */
369 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
370 (!(cbinfo.flags & CB_OWNER) && !hData))
372 WARN("Clipboard not owned by calling task. Operation failed.\n");
373 return 0;
376 if (USER_Driver->pSetClipboardData(wFormat, hData, cbinfo.flags & CB_OWNER))
378 hResult = hData;
379 bCBHasChanged = TRUE;
382 return hResult;
386 /**************************************************************************
387 * CountClipboardFormats (USER32.@)
389 INT WINAPI CountClipboardFormats(void)
391 INT count = USER_Driver->pCountClipboardFormats();
392 TRACE("returning %d\n", count);
393 return count;
397 /**************************************************************************
398 * EnumClipboardFormats (USER32.@)
400 UINT WINAPI EnumClipboardFormats(UINT wFormat)
402 CLIPBOARDINFO cbinfo;
404 TRACE("(%04X)\n", wFormat);
406 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
407 (~cbinfo.flags & CB_OPEN))
409 WARN("Clipboard not opened by calling task.\n");
410 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
411 return 0;
413 return USER_Driver->pEnumClipboardFormats(wFormat);
417 /**************************************************************************
418 * IsClipboardFormatAvailable (USER32.@)
420 BOOL WINAPI IsClipboardFormatAvailable(UINT wFormat)
422 BOOL bret = USER_Driver->pIsClipboardFormatAvailable(wFormat);
423 TRACE("%04x, returning %d\n", wFormat, bret);
424 return bret;
428 /**************************************************************************
429 * GetClipboardData (USER32.@)
431 HANDLE WINAPI GetClipboardData(UINT wFormat)
433 HANDLE hData = 0;
434 CLIPBOARDINFO cbinfo;
436 TRACE("%04x\n", wFormat);
438 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
439 (~cbinfo.flags & CB_OPEN))
441 WARN("Clipboard not opened by calling task.\n");
442 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
443 return 0;
446 hData = USER_Driver->pGetClipboardData( wFormat );
448 TRACE("returning %p\n", hData);
449 return hData;
453 /**************************************************************************
454 * GetPriorityClipboardFormat (USER32.@)
456 INT WINAPI GetPriorityClipboardFormat(UINT *list, INT nCount)
458 int i;
460 TRACE("()\n");
462 if(CountClipboardFormats() == 0)
463 return 0;
465 for (i = 0; i < nCount; i++)
466 if (IsClipboardFormatAvailable(list[i]))
467 return list[i];
469 return -1;
473 /**************************************************************************
474 * GetClipboardSequenceNumber (USER32.@)
475 * Supported on Win2k/Win98
476 * MSDN: Windows clipboard code keeps a serial number for the clipboard
477 * for each window station. The number is incremented whenever the
478 * contents change or are emptied.
479 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
481 DWORD WINAPI GetClipboardSequenceNumber(VOID)
483 DWORD seqno = 0;
485 SERVER_START_REQ( set_clipboard_info )
487 req->flags = 0;
488 if (!wine_server_call_err( req )) seqno = reply->seqno;
490 SERVER_END_REQ;
492 TRACE("returning %x\n", seqno);
493 return seqno;
496 /**************************************************************************
497 * AddClipboardFormatListener (USER32.@)
499 BOOL WINAPI AddClipboardFormatListener(HWND hwnd)
501 FIXME("%p: stub\n", hwnd);
502 return TRUE;
505 /**************************************************************************
506 * RemoveClipboardFormatListener (USER32.@)
508 BOOL WINAPI RemoveClipboardFormatListener(HWND hwnd)
510 FIXME("%p: stub\n", hwnd);
511 return TRUE;