strmbase: Implement BaseControlWindow.
[wine/multimedia.git] / dlls / user32 / clipboard.c
blob0985e6f962ed94e8039b1ae1f2a374b83cadc479
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 HWND hWndViewer;
65 UINT seqno;
66 UINT flags;
67 } CLIPBOARDINFO, *LPCLIPBOARDINFO;
70 * Indicates if data has changed since open.
72 static BOOL bCBHasChanged = FALSE;
75 /**************************************************************************
76 * CLIPBOARD_SetClipboardOwner
78 * Set the global wineserver clipboard owner. The current process will
79 * be the owner and <hWnd> will get the render notifications.
81 static BOOL CLIPBOARD_SetClipboardOwner(HWND hWnd)
83 BOOL bRet;
85 TRACE(" hWnd(%p)\n", hWnd);
87 SERVER_START_REQ( set_clipboard_info )
89 req->flags = SET_CB_OWNER;
90 req->owner = wine_server_user_handle( hWnd );
91 bRet = !wine_server_call_err( req );
93 SERVER_END_REQ;
95 return bRet;
99 /**************************************************************************
100 * CLIPBOARD_GetClipboardInfo
102 static BOOL CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo)
104 BOOL bRet;
106 SERVER_START_REQ( set_clipboard_info )
108 req->flags = 0;
110 if (((bRet = !wine_server_call_err( req ))))
112 cbInfo->hWndOpen = wine_server_ptr_handle( reply->old_clipboard );
113 cbInfo->hWndOwner = wine_server_ptr_handle( reply->old_owner );
114 cbInfo->hWndViewer = wine_server_ptr_handle( reply->old_viewer );
115 cbInfo->seqno = reply->seqno;
116 cbInfo->flags = reply->flags;
119 SERVER_END_REQ;
121 return bRet;
125 /**************************************************************************
126 * CLIPBOARD_ReleaseOwner
128 BOOL CLIPBOARD_ReleaseOwner(void)
130 BOOL bRet = FALSE;
132 SERVER_START_REQ( set_clipboard_info )
134 req->flags = SET_CB_RELOWNER | SET_CB_SEQNO;
136 if (wine_server_call_err( req ))
138 ERR("Failed to set clipboard.\n");
140 else
142 bRet = TRUE;
145 SERVER_END_REQ;
147 return bRet;
151 /**************************************************************************
152 * CLIPBOARD_OpenClipboard
154 static BOOL CLIPBOARD_OpenClipboard(HWND hWnd)
156 BOOL bRet;
158 SERVER_START_REQ( set_clipboard_info )
160 req->flags = SET_CB_OPEN;
161 req->clipboard = wine_server_user_handle( hWnd );
162 bRet = !wine_server_call( req );
164 SERVER_END_REQ;
166 return bRet;
170 /**************************************************************************
171 * CLIPBOARD_CloseClipboard
173 static BOOL CLIPBOARD_CloseClipboard(void)
175 BOOL bRet;
177 TRACE(" Changed=%d\n", bCBHasChanged);
179 SERVER_START_REQ( set_clipboard_info )
181 req->flags = SET_CB_CLOSE;
182 if (bCBHasChanged) req->flags |= SET_CB_SEQNO;
183 bRet = !wine_server_call_err( req );
185 SERVER_END_REQ;
187 return bRet;
190 /**************************************************************************
191 * CLIPBOARD_SetClipboardViewer
193 static HWND CLIPBOARD_SetClipboardViewer( HWND hWnd )
195 HWND hwndPrev = 0;
197 SERVER_START_REQ( set_clipboard_info )
199 req->flags = SET_CB_VIEWER;
200 req->viewer = wine_server_user_handle( hWnd );
201 if (!wine_server_call_err( req ))
202 hwndPrev = wine_server_ptr_handle( reply->old_viewer );
204 SERVER_END_REQ;
206 return hwndPrev;
209 /**************************************************************************
210 * WIN32 Clipboard implementation
211 **************************************************************************/
213 /**************************************************************************
214 * RegisterClipboardFormatW (USER32.@)
216 UINT WINAPI RegisterClipboardFormatW( LPCWSTR name )
218 return GlobalAddAtomW( name );
222 /**************************************************************************
223 * RegisterClipboardFormatA (USER32.@)
225 UINT WINAPI RegisterClipboardFormatA( LPCSTR name )
227 return GlobalAddAtomA( name );
231 /**************************************************************************
232 * GetClipboardFormatNameW (USER32.@)
234 INT WINAPI GetClipboardFormatNameW(UINT wFormat, LPWSTR retStr, INT maxlen)
236 if (wFormat < MAXINTATOM) return 0;
237 return GlobalGetAtomNameW( wFormat, retStr, maxlen );
241 /**************************************************************************
242 * GetClipboardFormatNameA (USER32.@)
244 INT WINAPI GetClipboardFormatNameA(UINT wFormat, LPSTR retStr, INT maxlen)
246 if (wFormat < MAXINTATOM) return 0;
247 return GlobalGetAtomNameA( wFormat, retStr, maxlen );
251 /**************************************************************************
252 * OpenClipboard (USER32.@)
254 * Note: Netscape uses NULL hWnd to open the clipboard.
256 BOOL WINAPI OpenClipboard( HWND hWnd )
258 BOOL bRet;
260 TRACE("(%p)...\n", hWnd);
262 bRet = CLIPBOARD_OpenClipboard(hWnd);
264 TRACE(" returning %i\n", bRet);
266 return bRet;
270 /**************************************************************************
271 * CloseClipboard (USER32.@)
273 BOOL WINAPI CloseClipboard(void)
275 BOOL bRet = FALSE;
277 TRACE("() Changed=%d\n", bCBHasChanged);
279 if (CLIPBOARD_CloseClipboard())
281 if (bCBHasChanged)
283 HWND hWndViewer = GetClipboardViewer();
285 USER_Driver->pEndClipboardUpdate();
287 bCBHasChanged = FALSE;
289 if (hWndViewer)
290 SendMessageW(hWndViewer, WM_DRAWCLIPBOARD, (WPARAM) GetClipboardOwner(), 0);
293 bRet = TRUE;
296 return bRet;
300 /**************************************************************************
301 * EmptyClipboard (USER32.@)
302 * Empties and acquires ownership of the clipboard
304 BOOL WINAPI EmptyClipboard(void)
306 CLIPBOARDINFO cbinfo;
308 TRACE("()\n");
310 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
311 ~cbinfo.flags & CB_OPEN)
313 WARN("Clipboard not opened by calling task!\n");
314 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
315 return FALSE;
318 /* Destroy private objects */
319 if (cbinfo.hWndOwner)
320 SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
322 /* Tell the driver to acquire the selection. The current owner
323 * will be signaled to delete it's own cache. */
325 /* Assign ownership of the clipboard to the current client. We do
326 * this before acquiring the selection so that when we do acquire the
327 * selection and the selection loser gets notified, it can check if
328 * it has lost the Wine clipboard ownership. If it did then it knows
329 * that a WM_DESTORYCLIPBOARD has already been sent. Otherwise it
330 * lost the selection to a X app and it should send the
331 * WM_DESTROYCLIPBOARD itself. */
332 CLIPBOARD_SetClipboardOwner(cbinfo.hWndOpen);
334 /* Acquire the selection. This will notify the previous owner
335 * to clear it's cache. */
336 USER_Driver->pAcquireClipboard(cbinfo.hWndOpen);
338 /* Empty the local cache */
339 USER_Driver->pEmptyClipboard(FALSE);
341 bCBHasChanged = TRUE;
343 return TRUE;
347 /**************************************************************************
348 * GetClipboardOwner (USER32.@)
349 * FIXME: Can't return the owner if the clipboard is owned by an external X-app
351 HWND WINAPI GetClipboardOwner(void)
353 HWND hWndOwner = 0;
355 SERVER_START_REQ( set_clipboard_info )
357 req->flags = 0;
358 if (!wine_server_call_err( req )) hWndOwner = wine_server_ptr_handle( reply->old_owner );
360 SERVER_END_REQ;
362 TRACE(" hWndOwner(%p)\n", hWndOwner);
364 return hWndOwner;
368 /**************************************************************************
369 * GetOpenClipboardWindow (USER32.@)
371 HWND WINAPI GetOpenClipboardWindow(void)
373 HWND hWndOpen = 0;
375 SERVER_START_REQ( set_clipboard_info )
377 req->flags = 0;
378 if (!wine_server_call_err( req )) hWndOpen = wine_server_ptr_handle( reply->old_clipboard );
380 SERVER_END_REQ;
382 TRACE(" hWndClipWindow(%p)\n", hWndOpen);
384 return hWndOpen;
388 /**************************************************************************
389 * SetClipboardViewer (USER32.@)
391 HWND WINAPI SetClipboardViewer( HWND hWnd )
393 HWND hwndPrev = CLIPBOARD_SetClipboardViewer(hWnd);
395 if (hWnd)
396 SendMessageW(hWnd, WM_DRAWCLIPBOARD, (WPARAM) GetClipboardOwner(), 0);
397 TRACE("(%p): returning %p\n", hWnd, hwndPrev);
399 return hwndPrev;
403 /**************************************************************************
404 * GetClipboardViewer (USER32.@)
406 HWND WINAPI GetClipboardViewer(void)
408 HWND hWndViewer = 0;
410 SERVER_START_REQ( set_clipboard_info )
412 req->flags = 0;
413 if (!wine_server_call_err( req )) hWndViewer = wine_server_ptr_handle( reply->old_viewer );
415 SERVER_END_REQ;
417 TRACE(" hWndViewer=%p\n", hWndViewer);
419 return hWndViewer;
423 /**************************************************************************
424 * ChangeClipboardChain (USER32.@)
426 BOOL WINAPI ChangeClipboardChain(HWND hWnd, HWND hWndNext)
428 BOOL bRet = TRUE;
429 HWND hWndViewer = GetClipboardViewer();
431 if (hWndViewer)
433 if (WIN_GetFullHandle(hWnd) == hWndViewer)
434 CLIPBOARD_SetClipboardViewer(WIN_GetFullHandle(hWndNext));
435 else
436 bRet = !SendMessageW(hWndViewer, WM_CHANGECBCHAIN, (WPARAM)hWnd, (LPARAM)hWndNext);
438 else
439 ERR("hWndViewer is lost\n");
441 return bRet;
445 /**************************************************************************
446 * SetClipboardData (USER32.@)
448 HANDLE WINAPI SetClipboardData(UINT wFormat, HANDLE hData)
450 CLIPBOARDINFO cbinfo;
451 HANDLE hResult = 0;
453 TRACE("(%04X, %p) !\n", wFormat, hData);
455 /* If it's not owned, data can only be set if the format isn't
456 available and its rendering is not delayed */
457 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
458 (!(cbinfo.flags & CB_OWNER) && !hData))
460 WARN("Clipboard not owned by calling task. Operation failed.\n");
461 return 0;
464 if (USER_Driver->pSetClipboardData(wFormat, hData, cbinfo.flags & CB_OWNER))
466 hResult = hData;
467 bCBHasChanged = TRUE;
470 return hResult;
474 /**************************************************************************
475 * CountClipboardFormats (USER32.@)
477 INT WINAPI CountClipboardFormats(void)
479 INT count = USER_Driver->pCountClipboardFormats();
480 TRACE("returning %d\n", count);
481 return count;
485 /**************************************************************************
486 * EnumClipboardFormats (USER32.@)
488 UINT WINAPI EnumClipboardFormats(UINT wFormat)
490 CLIPBOARDINFO cbinfo;
492 TRACE("(%04X)\n", wFormat);
494 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
495 (~cbinfo.flags & CB_OPEN))
497 WARN("Clipboard not opened by calling task.\n");
498 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
499 return 0;
501 return USER_Driver->pEnumClipboardFormats(wFormat);
505 /**************************************************************************
506 * IsClipboardFormatAvailable (USER32.@)
508 BOOL WINAPI IsClipboardFormatAvailable(UINT wFormat)
510 BOOL bret = USER_Driver->pIsClipboardFormatAvailable(wFormat);
511 TRACE("%04x, returning %d\n", wFormat, bret);
512 return bret;
516 /**************************************************************************
517 * GetClipboardData (USER32.@)
519 HANDLE WINAPI GetClipboardData(UINT wFormat)
521 HANDLE hData = 0;
522 CLIPBOARDINFO cbinfo;
524 TRACE("%04x\n", wFormat);
526 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
527 (~cbinfo.flags & CB_OPEN))
529 WARN("Clipboard not opened by calling task.\n");
530 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
531 return 0;
534 hData = USER_Driver->pGetClipboardData( wFormat );
536 TRACE("returning %p\n", hData);
537 return hData;
541 /**************************************************************************
542 * GetPriorityClipboardFormat (USER32.@)
544 INT WINAPI GetPriorityClipboardFormat(UINT *list, INT nCount)
546 int i;
548 TRACE("()\n");
550 if(CountClipboardFormats() == 0)
551 return 0;
553 for (i = 0; i < nCount; i++)
554 if (IsClipboardFormatAvailable(list[i]))
555 return list[i];
557 return -1;
561 /**************************************************************************
562 * GetClipboardSequenceNumber (USER32.@)
563 * Supported on Win2k/Win98
564 * MSDN: Windows clipboard code keeps a serial number for the clipboard
565 * for each window station. The number is incremented whenever the
566 * contents change or are emptied.
567 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
569 DWORD WINAPI GetClipboardSequenceNumber(VOID)
571 DWORD seqno = 0;
573 SERVER_START_REQ( set_clipboard_info )
575 req->flags = 0;
576 if (!wine_server_call_err( req )) seqno = reply->seqno;
578 SERVER_END_REQ;
580 TRACE("returning %x\n", seqno);
581 return seqno;