server: Check for the current owner window on the server side for releases.
[wine.git] / dlls / user32 / clipboard.c
blobcd3fdd75a4ccb82d9edfdaed42ffea7eed18cc9c
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_SetClipboardOwner
76 * Set the global wineserver clipboard owner. The current process will
77 * be the owner and <hWnd> will get the render notifications.
79 static BOOL CLIPBOARD_SetClipboardOwner(HWND hWnd)
81 BOOL bRet;
83 TRACE(" hWnd(%p)\n", hWnd);
85 SERVER_START_REQ( set_clipboard_info )
87 req->flags = SET_CB_OWNER;
88 req->owner = wine_server_user_handle( hWnd );
89 bRet = !wine_server_call_err( req );
91 SERVER_END_REQ;
93 return bRet;
97 /**************************************************************************
98 * CLIPBOARD_GetClipboardInfo
100 static BOOL CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo)
102 BOOL bRet;
104 SERVER_START_REQ( set_clipboard_info )
106 req->flags = 0;
108 if (((bRet = !wine_server_call_err( req ))))
110 cbInfo->hWndOpen = wine_server_ptr_handle( reply->old_clipboard );
111 cbInfo->hWndOwner = wine_server_ptr_handle( reply->old_owner );
112 cbInfo->flags = reply->flags;
115 SERVER_END_REQ;
117 return bRet;
121 /**************************************************************************
122 * CLIPBOARD_ReleaseOwner
124 void CLIPBOARD_ReleaseOwner( HWND hwnd )
126 SERVER_START_REQ( set_clipboard_info )
128 req->flags = SET_CB_RELOWNER | SET_CB_SEQNO;
129 req->owner = wine_server_user_handle( hwnd );
130 wine_server_call( req );
132 SERVER_END_REQ;
136 /**************************************************************************
137 * CLIPBOARD_SetClipboardViewer
139 static HWND CLIPBOARD_SetClipboardViewer( HWND hWnd )
141 HWND hwndPrev = 0;
143 SERVER_START_REQ( set_clipboard_info )
145 req->flags = SET_CB_VIEWER;
146 req->viewer = wine_server_user_handle( hWnd );
147 if (!wine_server_call_err( req ))
148 hwndPrev = wine_server_ptr_handle( reply->old_viewer );
150 SERVER_END_REQ;
152 return hwndPrev;
155 /**************************************************************************
156 * WIN32 Clipboard implementation
157 **************************************************************************/
159 /**************************************************************************
160 * RegisterClipboardFormatW (USER32.@)
162 UINT WINAPI RegisterClipboardFormatW( LPCWSTR name )
164 return GlobalAddAtomW( name );
168 /**************************************************************************
169 * RegisterClipboardFormatA (USER32.@)
171 UINT WINAPI RegisterClipboardFormatA( LPCSTR name )
173 return GlobalAddAtomA( name );
177 /**************************************************************************
178 * GetClipboardFormatNameW (USER32.@)
180 INT WINAPI GetClipboardFormatNameW(UINT wFormat, LPWSTR retStr, INT maxlen)
182 if (wFormat < MAXINTATOM) return 0;
183 return GlobalGetAtomNameW( wFormat, retStr, maxlen );
187 /**************************************************************************
188 * GetClipboardFormatNameA (USER32.@)
190 INT WINAPI GetClipboardFormatNameA(UINT wFormat, LPSTR retStr, INT maxlen)
192 if (wFormat < MAXINTATOM) return 0;
193 return GlobalGetAtomNameA( wFormat, retStr, maxlen );
197 /**************************************************************************
198 * OpenClipboard (USER32.@)
200 * Note: Netscape uses NULL hWnd to open the clipboard.
202 BOOL WINAPI OpenClipboard( HWND hWnd )
204 BOOL bRet;
206 TRACE("(%p)...\n", hWnd);
208 SERVER_START_REQ( set_clipboard_info )
210 req->flags = SET_CB_OPEN;
211 req->clipboard = wine_server_user_handle( hWnd );
212 bRet = !wine_server_call( req );
214 SERVER_END_REQ;
216 TRACE(" returning %i\n", bRet);
218 return bRet;
222 /**************************************************************************
223 * CloseClipboard (USER32.@)
225 BOOL WINAPI CloseClipboard(void)
227 HWND viewer = 0;
228 BOOL ret;
230 TRACE("() Changed=%d\n", bCBHasChanged);
232 SERVER_START_REQ( set_clipboard_info )
234 req->flags = SET_CB_CLOSE;
235 if (bCBHasChanged) req->flags |= SET_CB_SEQNO;
236 if ((ret = !wine_server_call_err( req )))
237 viewer = wine_server_ptr_handle( reply->old_viewer );
239 SERVER_END_REQ;
241 if (!ret) return FALSE;
243 if (bCBHasChanged)
245 USER_Driver->pEndClipboardUpdate();
246 bCBHasChanged = FALSE;
247 if (viewer) SendNotifyMessageW(viewer, WM_DRAWCLIPBOARD, (WPARAM) GetClipboardOwner(), 0);
249 return TRUE;
253 /**************************************************************************
254 * EmptyClipboard (USER32.@)
255 * Empties and acquires ownership of the clipboard
257 BOOL WINAPI EmptyClipboard(void)
259 CLIPBOARDINFO cbinfo;
261 TRACE("()\n");
263 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
264 ~cbinfo.flags & CB_OPEN)
266 WARN("Clipboard not opened by calling task!\n");
267 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
268 return FALSE;
271 /* Destroy private objects */
272 if (cbinfo.hWndOwner)
273 SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
275 /* Tell the driver to acquire the selection. The current owner
276 * will be signaled to delete its own cache. */
278 /* Assign ownership of the clipboard to the current client. We do
279 * this before acquiring the selection so that when we do acquire the
280 * selection and the selection loser gets notified, it can check if
281 * it has lost the Wine clipboard ownership. If it did then it knows
282 * that a WM_DESTROYCLIPBOARD has already been sent. Otherwise it
283 * lost the selection to a X app and it should send the
284 * WM_DESTROYCLIPBOARD itself. */
285 CLIPBOARD_SetClipboardOwner(cbinfo.hWndOpen);
287 USER_Driver->pEmptyClipboard();
289 bCBHasChanged = TRUE;
291 return TRUE;
295 /**************************************************************************
296 * GetClipboardOwner (USER32.@)
297 * FIXME: Can't return the owner if the clipboard is owned by an external X-app
299 HWND WINAPI GetClipboardOwner(void)
301 HWND hWndOwner = 0;
303 SERVER_START_REQ( set_clipboard_info )
305 req->flags = 0;
306 if (!wine_server_call_err( req )) hWndOwner = wine_server_ptr_handle( reply->old_owner );
308 SERVER_END_REQ;
310 TRACE(" hWndOwner(%p)\n", hWndOwner);
312 return hWndOwner;
316 /**************************************************************************
317 * GetOpenClipboardWindow (USER32.@)
319 HWND WINAPI GetOpenClipboardWindow(void)
321 HWND hWndOpen = 0;
323 SERVER_START_REQ( set_clipboard_info )
325 req->flags = 0;
326 if (!wine_server_call_err( req )) hWndOpen = wine_server_ptr_handle( reply->old_clipboard );
328 SERVER_END_REQ;
330 TRACE(" hWndClipWindow(%p)\n", hWndOpen);
332 return hWndOpen;
336 /**************************************************************************
337 * SetClipboardViewer (USER32.@)
339 HWND WINAPI SetClipboardViewer( HWND hWnd )
341 HWND hwndPrev = CLIPBOARD_SetClipboardViewer(hWnd);
343 if (hWnd)
344 SendNotifyMessageW(hWnd, WM_DRAWCLIPBOARD, (WPARAM) GetClipboardOwner(), 0);
345 TRACE("(%p): returning %p\n", hWnd, hwndPrev);
347 return hwndPrev;
351 /**************************************************************************
352 * GetClipboardViewer (USER32.@)
354 HWND WINAPI GetClipboardViewer(void)
356 HWND hWndViewer = 0;
358 SERVER_START_REQ( set_clipboard_info )
360 req->flags = 0;
361 if (!wine_server_call_err( req )) hWndViewer = wine_server_ptr_handle( reply->old_viewer );
363 SERVER_END_REQ;
365 TRACE(" hWndViewer=%p\n", hWndViewer);
367 return hWndViewer;
371 /**************************************************************************
372 * ChangeClipboardChain (USER32.@)
374 BOOL WINAPI ChangeClipboardChain(HWND hWnd, HWND hWndNext)
376 BOOL bRet = TRUE;
377 HWND hWndViewer = GetClipboardViewer();
379 if (hWndViewer)
381 if (WIN_GetFullHandle(hWnd) == hWndViewer)
382 CLIPBOARD_SetClipboardViewer(WIN_GetFullHandle(hWndNext));
383 else
384 bRet = !SendMessageW(hWndViewer, WM_CHANGECBCHAIN, (WPARAM)hWnd, (LPARAM)hWndNext);
386 else
387 ERR("hWndViewer is lost\n");
389 return bRet;
393 /**************************************************************************
394 * SetClipboardData (USER32.@)
396 HANDLE WINAPI SetClipboardData(UINT wFormat, HANDLE hData)
398 CLIPBOARDINFO cbinfo;
399 HANDLE hResult = 0;
401 TRACE("(%04X, %p) !\n", wFormat, hData);
403 /* If it's not owned, data can only be set if the format isn't
404 available and its rendering is not delayed */
405 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
406 (!(cbinfo.flags & CB_OWNER) && !hData))
408 WARN("Clipboard not owned by calling task. Operation failed.\n");
409 return 0;
412 if (USER_Driver->pSetClipboardData(wFormat, hData, cbinfo.flags & CB_OWNER))
414 hResult = hData;
415 bCBHasChanged = TRUE;
418 return hResult;
422 /**************************************************************************
423 * CountClipboardFormats (USER32.@)
425 INT WINAPI CountClipboardFormats(void)
427 INT count = USER_Driver->pCountClipboardFormats();
428 TRACE("returning %d\n", count);
429 return count;
433 /**************************************************************************
434 * EnumClipboardFormats (USER32.@)
436 UINT WINAPI EnumClipboardFormats(UINT wFormat)
438 CLIPBOARDINFO cbinfo;
440 TRACE("(%04X)\n", wFormat);
442 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
443 (~cbinfo.flags & CB_OPEN))
445 WARN("Clipboard not opened by calling task.\n");
446 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
447 return 0;
449 return USER_Driver->pEnumClipboardFormats(wFormat);
453 /**************************************************************************
454 * IsClipboardFormatAvailable (USER32.@)
456 BOOL WINAPI IsClipboardFormatAvailable(UINT wFormat)
458 BOOL bret = USER_Driver->pIsClipboardFormatAvailable(wFormat);
459 TRACE("%04x, returning %d\n", wFormat, bret);
460 return bret;
464 /**************************************************************************
465 * GetClipboardData (USER32.@)
467 HANDLE WINAPI GetClipboardData(UINT wFormat)
469 HANDLE hData = 0;
470 CLIPBOARDINFO cbinfo;
472 TRACE("%04x\n", wFormat);
474 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
475 (~cbinfo.flags & CB_OPEN))
477 WARN("Clipboard not opened by calling task.\n");
478 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
479 return 0;
482 hData = USER_Driver->pGetClipboardData( wFormat );
484 TRACE("returning %p\n", hData);
485 return hData;
489 /**************************************************************************
490 * GetPriorityClipboardFormat (USER32.@)
492 INT WINAPI GetPriorityClipboardFormat(UINT *list, INT nCount)
494 int i;
496 TRACE("()\n");
498 if(CountClipboardFormats() == 0)
499 return 0;
501 for (i = 0; i < nCount; i++)
502 if (IsClipboardFormatAvailable(list[i]))
503 return list[i];
505 return -1;
509 /**************************************************************************
510 * GetClipboardSequenceNumber (USER32.@)
511 * Supported on Win2k/Win98
512 * MSDN: Windows clipboard code keeps a serial number for the clipboard
513 * for each window station. The number is incremented whenever the
514 * contents change or are emptied.
515 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
517 DWORD WINAPI GetClipboardSequenceNumber(VOID)
519 DWORD seqno = 0;
521 SERVER_START_REQ( set_clipboard_info )
523 req->flags = 0;
524 if (!wine_server_call_err( req )) seqno = reply->seqno;
526 SERVER_END_REQ;
528 TRACE("returning %x\n", seqno);
529 return seqno;
532 /**************************************************************************
533 * AddClipboardFormatListener (USER32.@)
535 BOOL WINAPI AddClipboardFormatListener(HWND hwnd)
537 FIXME("%p: stub\n", hwnd);
538 return TRUE;
541 /**************************************************************************
542 * RemoveClipboardFormatListener (USER32.@)
544 BOOL WINAPI RemoveClipboardFormatListener(HWND hwnd)
546 FIXME("%p: stub\n", hwnd);
547 return TRUE;