user32: Notify the clipboard viewer on close even if we are not the owner.
[wine.git] / dlls / user32 / clipboard.c
blob6d62dfd53299faaf98f1567bee2612e259a2ca7c
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);
59 * Indicates if data has changed since open.
61 static BOOL bCBHasChanged = FALSE;
64 /**************************************************************************
65 * get_clipboard_flags
67 static UINT get_clipboard_flags(void)
69 UINT ret = 0;
71 SERVER_START_REQ( set_clipboard_info )
73 if (!wine_server_call_err( req )) ret = reply->flags;
75 SERVER_END_REQ;
77 return ret;
81 /**************************************************************************
82 * CLIPBOARD_ReleaseOwner
84 void CLIPBOARD_ReleaseOwner( HWND hwnd )
86 SERVER_START_REQ( set_clipboard_info )
88 req->flags = SET_CB_RELOWNER | SET_CB_SEQNO;
89 req->owner = wine_server_user_handle( hwnd );
90 wine_server_call( req );
92 SERVER_END_REQ;
96 /**************************************************************************
97 * WIN32 Clipboard implementation
98 **************************************************************************/
100 /**************************************************************************
101 * RegisterClipboardFormatW (USER32.@)
103 UINT WINAPI RegisterClipboardFormatW( LPCWSTR name )
105 return GlobalAddAtomW( name );
109 /**************************************************************************
110 * RegisterClipboardFormatA (USER32.@)
112 UINT WINAPI RegisterClipboardFormatA( LPCSTR name )
114 return GlobalAddAtomA( name );
118 /**************************************************************************
119 * GetClipboardFormatNameW (USER32.@)
121 INT WINAPI GetClipboardFormatNameW(UINT wFormat, LPWSTR retStr, INT maxlen)
123 if (wFormat < MAXINTATOM) return 0;
124 return GlobalGetAtomNameW( wFormat, retStr, maxlen );
128 /**************************************************************************
129 * GetClipboardFormatNameA (USER32.@)
131 INT WINAPI GetClipboardFormatNameA(UINT wFormat, LPSTR retStr, INT maxlen)
133 if (wFormat < MAXINTATOM) return 0;
134 return GlobalGetAtomNameA( wFormat, retStr, maxlen );
138 /**************************************************************************
139 * OpenClipboard (USER32.@)
141 BOOL WINAPI OpenClipboard( HWND hwnd )
143 BOOL ret;
145 TRACE( "%p\n", hwnd );
147 SERVER_START_REQ( open_clipboard )
149 req->window = wine_server_user_handle( hwnd );
150 if ((ret = !wine_server_call( req )))
152 if (!reply->owner) bCBHasChanged = FALSE;
155 SERVER_END_REQ;
157 return ret;
161 /**************************************************************************
162 * CloseClipboard (USER32.@)
164 BOOL WINAPI CloseClipboard(void)
166 HWND viewer = 0;
167 BOOL ret, owner = FALSE;
169 TRACE("() Changed=%d\n", bCBHasChanged);
171 SERVER_START_REQ( close_clipboard )
173 req->changed = bCBHasChanged;
174 if ((ret = !wine_server_call_err( req )))
176 viewer = wine_server_ptr_handle( reply->viewer );
177 owner = reply->owner;
180 SERVER_END_REQ;
182 if (!ret) return FALSE;
184 if (bCBHasChanged)
186 if (owner) USER_Driver->pEndClipboardUpdate();
187 bCBHasChanged = FALSE;
188 if (viewer) SendNotifyMessageW(viewer, WM_DRAWCLIPBOARD, (WPARAM) GetClipboardOwner(), 0);
190 return TRUE;
194 /**************************************************************************
195 * EmptyClipboard (USER32.@)
196 * Empties and acquires ownership of the clipboard
198 BOOL WINAPI EmptyClipboard(void)
200 BOOL ret;
201 HWND owner = GetClipboardOwner();
203 TRACE("()\n");
205 if (owner) SendMessageTimeoutW( owner, WM_DESTROYCLIPBOARD, 0, 0, SMTO_ABORTIFHUNG, 5000, NULL );
207 SERVER_START_REQ( empty_clipboard )
209 ret = !wine_server_call_err( req );
211 SERVER_END_REQ;
213 if (ret)
215 USER_Driver->pEmptyClipboard();
216 bCBHasChanged = TRUE;
218 return ret;
222 /**************************************************************************
223 * GetClipboardOwner (USER32.@)
224 * FIXME: Can't return the owner if the clipboard is owned by an external X-app
226 HWND WINAPI GetClipboardOwner(void)
228 HWND hWndOwner = 0;
230 SERVER_START_REQ( get_clipboard_info )
232 if (!wine_server_call_err( req )) hWndOwner = wine_server_ptr_handle( reply->owner );
234 SERVER_END_REQ;
236 TRACE(" hWndOwner(%p)\n", hWndOwner);
238 return hWndOwner;
242 /**************************************************************************
243 * GetOpenClipboardWindow (USER32.@)
245 HWND WINAPI GetOpenClipboardWindow(void)
247 HWND hWndOpen = 0;
249 SERVER_START_REQ( get_clipboard_info )
251 if (!wine_server_call_err( req )) hWndOpen = wine_server_ptr_handle( reply->window );
253 SERVER_END_REQ;
255 TRACE(" hWndClipWindow(%p)\n", hWndOpen);
257 return hWndOpen;
261 /**************************************************************************
262 * SetClipboardViewer (USER32.@)
264 HWND WINAPI SetClipboardViewer( HWND hwnd )
266 HWND prev = 0, owner = 0;
268 SERVER_START_REQ( set_clipboard_viewer )
270 req->viewer = wine_server_user_handle( hwnd );
271 if (!wine_server_call_err( req ))
273 prev = wine_server_ptr_handle( reply->old_viewer );
274 owner = wine_server_ptr_handle( reply->owner );
277 SERVER_END_REQ;
279 if (hwnd) SendNotifyMessageW( hwnd, WM_DRAWCLIPBOARD, (WPARAM)owner, 0 );
281 TRACE( "(%p): returning %p\n", hwnd, prev );
282 return prev;
286 /**************************************************************************
287 * GetClipboardViewer (USER32.@)
289 HWND WINAPI GetClipboardViewer(void)
291 HWND hWndViewer = 0;
293 SERVER_START_REQ( get_clipboard_info )
295 if (!wine_server_call_err( req )) hWndViewer = wine_server_ptr_handle( reply->viewer );
297 SERVER_END_REQ;
299 TRACE(" hWndViewer=%p\n", hWndViewer);
301 return hWndViewer;
305 /**************************************************************************
306 * ChangeClipboardChain (USER32.@)
308 BOOL WINAPI ChangeClipboardChain( HWND hwnd, HWND next )
310 NTSTATUS status;
311 HWND viewer;
313 if (!hwnd) return FALSE;
315 SERVER_START_REQ( set_clipboard_viewer )
317 req->viewer = wine_server_user_handle( next );
318 req->previous = wine_server_user_handle( hwnd );
319 status = wine_server_call( req );
320 viewer = wine_server_ptr_handle( reply->old_viewer );
322 SERVER_END_REQ;
324 if (status == STATUS_PENDING)
325 return !SendMessageW( viewer, WM_CHANGECBCHAIN, (WPARAM)hwnd, (LPARAM)next );
327 if (status) SetLastError( RtlNtStatusToDosError( status ));
328 return !status;
332 /**************************************************************************
333 * SetClipboardData (USER32.@)
335 HANDLE WINAPI SetClipboardData(UINT wFormat, HANDLE hData)
337 HANDLE hResult = 0;
338 UINT flags;
340 TRACE("(%04X, %p) !\n", wFormat, hData);
342 if (!wFormat)
344 SetLastError( ERROR_CLIPBOARD_NOT_OPEN );
345 return 0;
348 flags = get_clipboard_flags();
349 if (!(flags & CB_OPEN_ANY))
351 SetLastError( ERROR_CLIPBOARD_NOT_OPEN );
352 return 0;
355 if (USER_Driver->pSetClipboardData(wFormat, hData, flags & CB_OWNER))
357 hResult = hData;
358 bCBHasChanged = TRUE;
361 return hResult;
365 /**************************************************************************
366 * CountClipboardFormats (USER32.@)
368 INT WINAPI CountClipboardFormats(void)
370 INT count = USER_Driver->pCountClipboardFormats();
371 TRACE("returning %d\n", count);
372 return count;
376 /**************************************************************************
377 * EnumClipboardFormats (USER32.@)
379 UINT WINAPI EnumClipboardFormats(UINT wFormat)
381 TRACE("(%04X)\n", wFormat);
383 if (!(get_clipboard_flags() & CB_OPEN))
385 WARN("Clipboard not opened by calling task.\n");
386 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
387 return 0;
389 return USER_Driver->pEnumClipboardFormats(wFormat);
393 /**************************************************************************
394 * IsClipboardFormatAvailable (USER32.@)
396 BOOL WINAPI IsClipboardFormatAvailable(UINT wFormat)
398 BOOL bret = USER_Driver->pIsClipboardFormatAvailable(wFormat);
399 TRACE("%04x, returning %d\n", wFormat, bret);
400 return bret;
404 /**************************************************************************
405 * GetClipboardData (USER32.@)
407 HANDLE WINAPI GetClipboardData(UINT wFormat)
409 HANDLE hData = 0;
411 TRACE("%04x\n", wFormat);
413 if (!(get_clipboard_flags() & CB_OPEN))
415 WARN("Clipboard not opened by calling task.\n");
416 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
417 return 0;
420 hData = USER_Driver->pGetClipboardData( wFormat );
422 TRACE("returning %p\n", hData);
423 return hData;
427 /**************************************************************************
428 * GetPriorityClipboardFormat (USER32.@)
430 INT WINAPI GetPriorityClipboardFormat(UINT *list, INT nCount)
432 int i;
434 TRACE("()\n");
436 if(CountClipboardFormats() == 0)
437 return 0;
439 for (i = 0; i < nCount; i++)
440 if (IsClipboardFormatAvailable(list[i]))
441 return list[i];
443 return -1;
447 /**************************************************************************
448 * GetClipboardSequenceNumber (USER32.@)
449 * Supported on Win2k/Win98
450 * MSDN: Windows clipboard code keeps a serial number for the clipboard
451 * for each window station. The number is incremented whenever the
452 * contents change or are emptied.
453 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
455 DWORD WINAPI GetClipboardSequenceNumber(VOID)
457 DWORD seqno = 0;
459 SERVER_START_REQ( get_clipboard_info )
461 if (!wine_server_call_err( req )) seqno = reply->seqno;
463 SERVER_END_REQ;
465 TRACE("returning %x\n", seqno);
466 return seqno;
469 /**************************************************************************
470 * AddClipboardFormatListener (USER32.@)
472 BOOL WINAPI AddClipboardFormatListener(HWND hwnd)
474 FIXME("%p: stub\n", hwnd);
475 return TRUE;
478 /**************************************************************************
479 * RemoveClipboardFormatListener (USER32.@)
481 BOOL WINAPI RemoveClipboardFormatListener(HWND hwnd)
483 FIXME("%p: stub\n", hwnd);
484 return TRUE;