user32: Get rid of the unused parameter in the EmptyClipboard driver entry point.
[wine.git] / dlls / user32 / clipboard.c
blob666acf006c101f4ab18c9716a36b08d78483ac06
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 BOOL CLIPBOARD_ReleaseOwner(void)
126 BOOL bRet = FALSE;
128 SERVER_START_REQ( set_clipboard_info )
130 req->flags = SET_CB_RELOWNER | SET_CB_SEQNO;
132 if (wine_server_call_err( req ))
134 ERR("Failed to set clipboard.\n");
136 else
138 bRet = TRUE;
141 SERVER_END_REQ;
143 return bRet;
147 /**************************************************************************
148 * CLIPBOARD_SetClipboardViewer
150 static HWND CLIPBOARD_SetClipboardViewer( HWND hWnd )
152 HWND hwndPrev = 0;
154 SERVER_START_REQ( set_clipboard_info )
156 req->flags = SET_CB_VIEWER;
157 req->viewer = wine_server_user_handle( hWnd );
158 if (!wine_server_call_err( req ))
159 hwndPrev = wine_server_ptr_handle( reply->old_viewer );
161 SERVER_END_REQ;
163 return hwndPrev;
166 /**************************************************************************
167 * WIN32 Clipboard implementation
168 **************************************************************************/
170 /**************************************************************************
171 * RegisterClipboardFormatW (USER32.@)
173 UINT WINAPI RegisterClipboardFormatW( LPCWSTR name )
175 return GlobalAddAtomW( name );
179 /**************************************************************************
180 * RegisterClipboardFormatA (USER32.@)
182 UINT WINAPI RegisterClipboardFormatA( LPCSTR name )
184 return GlobalAddAtomA( name );
188 /**************************************************************************
189 * GetClipboardFormatNameW (USER32.@)
191 INT WINAPI GetClipboardFormatNameW(UINT wFormat, LPWSTR retStr, INT maxlen)
193 if (wFormat < MAXINTATOM) return 0;
194 return GlobalGetAtomNameW( wFormat, retStr, maxlen );
198 /**************************************************************************
199 * GetClipboardFormatNameA (USER32.@)
201 INT WINAPI GetClipboardFormatNameA(UINT wFormat, LPSTR retStr, INT maxlen)
203 if (wFormat < MAXINTATOM) return 0;
204 return GlobalGetAtomNameA( wFormat, retStr, maxlen );
208 /**************************************************************************
209 * OpenClipboard (USER32.@)
211 * Note: Netscape uses NULL hWnd to open the clipboard.
213 BOOL WINAPI OpenClipboard( HWND hWnd )
215 BOOL bRet;
217 TRACE("(%p)...\n", hWnd);
219 SERVER_START_REQ( set_clipboard_info )
221 req->flags = SET_CB_OPEN;
222 req->clipboard = wine_server_user_handle( hWnd );
223 bRet = !wine_server_call( req );
225 SERVER_END_REQ;
227 TRACE(" returning %i\n", bRet);
229 return bRet;
233 /**************************************************************************
234 * CloseClipboard (USER32.@)
236 BOOL WINAPI CloseClipboard(void)
238 HWND viewer = 0;
239 BOOL ret;
241 TRACE("() Changed=%d\n", bCBHasChanged);
243 SERVER_START_REQ( set_clipboard_info )
245 req->flags = SET_CB_CLOSE;
246 if (bCBHasChanged) req->flags |= SET_CB_SEQNO;
247 if ((ret = !wine_server_call_err( req )))
248 viewer = wine_server_ptr_handle( reply->old_viewer );
250 SERVER_END_REQ;
252 if (!ret) return FALSE;
254 if (bCBHasChanged)
256 USER_Driver->pEndClipboardUpdate();
257 bCBHasChanged = FALSE;
258 if (viewer) SendNotifyMessageW(viewer, WM_DRAWCLIPBOARD, (WPARAM) GetClipboardOwner(), 0);
260 return TRUE;
264 /**************************************************************************
265 * EmptyClipboard (USER32.@)
266 * Empties and acquires ownership of the clipboard
268 BOOL WINAPI EmptyClipboard(void)
270 CLIPBOARDINFO cbinfo;
272 TRACE("()\n");
274 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
275 ~cbinfo.flags & CB_OPEN)
277 WARN("Clipboard not opened by calling task!\n");
278 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
279 return FALSE;
282 /* Destroy private objects */
283 if (cbinfo.hWndOwner)
284 SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
286 /* Tell the driver to acquire the selection. The current owner
287 * will be signaled to delete its own cache. */
289 /* Assign ownership of the clipboard to the current client. We do
290 * this before acquiring the selection so that when we do acquire the
291 * selection and the selection loser gets notified, it can check if
292 * it has lost the Wine clipboard ownership. If it did then it knows
293 * that a WM_DESTROYCLIPBOARD has already been sent. Otherwise it
294 * lost the selection to a X app and it should send the
295 * WM_DESTROYCLIPBOARD itself. */
296 CLIPBOARD_SetClipboardOwner(cbinfo.hWndOpen);
298 /* Acquire the selection. This will notify the previous owner
299 * to clear its cache. */
300 USER_Driver->pAcquireClipboard(cbinfo.hWndOpen);
302 USER_Driver->pEmptyClipboard();
304 bCBHasChanged = TRUE;
306 return TRUE;
310 /**************************************************************************
311 * GetClipboardOwner (USER32.@)
312 * FIXME: Can't return the owner if the clipboard is owned by an external X-app
314 HWND WINAPI GetClipboardOwner(void)
316 HWND hWndOwner = 0;
318 SERVER_START_REQ( set_clipboard_info )
320 req->flags = 0;
321 if (!wine_server_call_err( req )) hWndOwner = wine_server_ptr_handle( reply->old_owner );
323 SERVER_END_REQ;
325 TRACE(" hWndOwner(%p)\n", hWndOwner);
327 return hWndOwner;
331 /**************************************************************************
332 * GetOpenClipboardWindow (USER32.@)
334 HWND WINAPI GetOpenClipboardWindow(void)
336 HWND hWndOpen = 0;
338 SERVER_START_REQ( set_clipboard_info )
340 req->flags = 0;
341 if (!wine_server_call_err( req )) hWndOpen = wine_server_ptr_handle( reply->old_clipboard );
343 SERVER_END_REQ;
345 TRACE(" hWndClipWindow(%p)\n", hWndOpen);
347 return hWndOpen;
351 /**************************************************************************
352 * SetClipboardViewer (USER32.@)
354 HWND WINAPI SetClipboardViewer( HWND hWnd )
356 HWND hwndPrev = CLIPBOARD_SetClipboardViewer(hWnd);
358 if (hWnd)
359 SendNotifyMessageW(hWnd, WM_DRAWCLIPBOARD, (WPARAM) GetClipboardOwner(), 0);
360 TRACE("(%p): returning %p\n", hWnd, hwndPrev);
362 return hwndPrev;
366 /**************************************************************************
367 * GetClipboardViewer (USER32.@)
369 HWND WINAPI GetClipboardViewer(void)
371 HWND hWndViewer = 0;
373 SERVER_START_REQ( set_clipboard_info )
375 req->flags = 0;
376 if (!wine_server_call_err( req )) hWndViewer = wine_server_ptr_handle( reply->old_viewer );
378 SERVER_END_REQ;
380 TRACE(" hWndViewer=%p\n", hWndViewer);
382 return hWndViewer;
386 /**************************************************************************
387 * ChangeClipboardChain (USER32.@)
389 BOOL WINAPI ChangeClipboardChain(HWND hWnd, HWND hWndNext)
391 BOOL bRet = TRUE;
392 HWND hWndViewer = GetClipboardViewer();
394 if (hWndViewer)
396 if (WIN_GetFullHandle(hWnd) == hWndViewer)
397 CLIPBOARD_SetClipboardViewer(WIN_GetFullHandle(hWndNext));
398 else
399 bRet = !SendMessageW(hWndViewer, WM_CHANGECBCHAIN, (WPARAM)hWnd, (LPARAM)hWndNext);
401 else
402 ERR("hWndViewer is lost\n");
404 return bRet;
408 /**************************************************************************
409 * SetClipboardData (USER32.@)
411 HANDLE WINAPI SetClipboardData(UINT wFormat, HANDLE hData)
413 CLIPBOARDINFO cbinfo;
414 HANDLE hResult = 0;
416 TRACE("(%04X, %p) !\n", wFormat, hData);
418 /* If it's not owned, data can only be set if the format isn't
419 available and its rendering is not delayed */
420 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
421 (!(cbinfo.flags & CB_OWNER) && !hData))
423 WARN("Clipboard not owned by calling task. Operation failed.\n");
424 return 0;
427 if (USER_Driver->pSetClipboardData(wFormat, hData, cbinfo.flags & CB_OWNER))
429 hResult = hData;
430 bCBHasChanged = TRUE;
433 return hResult;
437 /**************************************************************************
438 * CountClipboardFormats (USER32.@)
440 INT WINAPI CountClipboardFormats(void)
442 INT count = USER_Driver->pCountClipboardFormats();
443 TRACE("returning %d\n", count);
444 return count;
448 /**************************************************************************
449 * EnumClipboardFormats (USER32.@)
451 UINT WINAPI EnumClipboardFormats(UINT wFormat)
453 CLIPBOARDINFO cbinfo;
455 TRACE("(%04X)\n", wFormat);
457 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
458 (~cbinfo.flags & CB_OPEN))
460 WARN("Clipboard not opened by calling task.\n");
461 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
462 return 0;
464 return USER_Driver->pEnumClipboardFormats(wFormat);
468 /**************************************************************************
469 * IsClipboardFormatAvailable (USER32.@)
471 BOOL WINAPI IsClipboardFormatAvailable(UINT wFormat)
473 BOOL bret = USER_Driver->pIsClipboardFormatAvailable(wFormat);
474 TRACE("%04x, returning %d\n", wFormat, bret);
475 return bret;
479 /**************************************************************************
480 * GetClipboardData (USER32.@)
482 HANDLE WINAPI GetClipboardData(UINT wFormat)
484 HANDLE hData = 0;
485 CLIPBOARDINFO cbinfo;
487 TRACE("%04x\n", wFormat);
489 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
490 (~cbinfo.flags & CB_OPEN))
492 WARN("Clipboard not opened by calling task.\n");
493 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
494 return 0;
497 hData = USER_Driver->pGetClipboardData( wFormat );
499 TRACE("returning %p\n", hData);
500 return hData;
504 /**************************************************************************
505 * GetPriorityClipboardFormat (USER32.@)
507 INT WINAPI GetPriorityClipboardFormat(UINT *list, INT nCount)
509 int i;
511 TRACE("()\n");
513 if(CountClipboardFormats() == 0)
514 return 0;
516 for (i = 0; i < nCount; i++)
517 if (IsClipboardFormatAvailable(list[i]))
518 return list[i];
520 return -1;
524 /**************************************************************************
525 * GetClipboardSequenceNumber (USER32.@)
526 * Supported on Win2k/Win98
527 * MSDN: Windows clipboard code keeps a serial number for the clipboard
528 * for each window station. The number is incremented whenever the
529 * contents change or are emptied.
530 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
532 DWORD WINAPI GetClipboardSequenceNumber(VOID)
534 DWORD seqno = 0;
536 SERVER_START_REQ( set_clipboard_info )
538 req->flags = 0;
539 if (!wine_server_call_err( req )) seqno = reply->seqno;
541 SERVER_END_REQ;
543 TRACE("returning %x\n", seqno);
544 return seqno;
547 /**************************************************************************
548 * AddClipboardFormatListener (USER32.@)
550 BOOL WINAPI AddClipboardFormatListener(HWND hwnd)
552 FIXME("%p: stub\n", hwnd);
553 return TRUE;
556 /**************************************************************************
557 * RemoveClipboardFormatListener (USER32.@)
559 BOOL WINAPI RemoveClipboardFormatListener(HWND hwnd)
561 FIXME("%p: stub\n", hwnd);
562 return TRUE;