2 * WIN32 clipboard implementation
4 * Copyright 1994 Martin Ayotte
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
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)
33 #include "wine/port.h"
37 #include <sys/types.h>
49 #include "user_private.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
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
)
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
);
97 /**************************************************************************
98 * CLIPBOARD_GetClipboardInfo
100 static BOOL
CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo
)
104 SERVER_START_REQ( set_clipboard_info
)
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
;
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
);
136 /**************************************************************************
137 * CLIPBOARD_SetClipboardViewer
139 static HWND
CLIPBOARD_SetClipboardViewer( HWND hWnd
)
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
);
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
)
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
);
216 TRACE(" returning %i\n", bRet
);
222 /**************************************************************************
223 * CloseClipboard (USER32.@)
225 BOOL WINAPI
CloseClipboard(void)
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
);
241 if (!ret
) return FALSE
;
245 USER_Driver
->pEndClipboardUpdate();
246 bCBHasChanged
= FALSE
;
247 if (viewer
) SendNotifyMessageW(viewer
, WM_DRAWCLIPBOARD
, (WPARAM
) GetClipboardOwner(), 0);
253 /**************************************************************************
254 * EmptyClipboard (USER32.@)
255 * Empties and acquires ownership of the clipboard
257 BOOL WINAPI
EmptyClipboard(void)
259 CLIPBOARDINFO cbinfo
;
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
);
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
;
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)
303 SERVER_START_REQ( set_clipboard_info
)
306 if (!wine_server_call_err( req
)) hWndOwner
= wine_server_ptr_handle( reply
->old_owner
);
310 TRACE(" hWndOwner(%p)\n", hWndOwner
);
316 /**************************************************************************
317 * GetOpenClipboardWindow (USER32.@)
319 HWND WINAPI
GetOpenClipboardWindow(void)
323 SERVER_START_REQ( set_clipboard_info
)
326 if (!wine_server_call_err( req
)) hWndOpen
= wine_server_ptr_handle( reply
->old_clipboard
);
330 TRACE(" hWndClipWindow(%p)\n", hWndOpen
);
336 /**************************************************************************
337 * SetClipboardViewer (USER32.@)
339 HWND WINAPI
SetClipboardViewer( HWND hWnd
)
341 HWND hwndPrev
= CLIPBOARD_SetClipboardViewer(hWnd
);
344 SendNotifyMessageW(hWnd
, WM_DRAWCLIPBOARD
, (WPARAM
) GetClipboardOwner(), 0);
345 TRACE("(%p): returning %p\n", hWnd
, hwndPrev
);
351 /**************************************************************************
352 * GetClipboardViewer (USER32.@)
354 HWND WINAPI
GetClipboardViewer(void)
358 SERVER_START_REQ( set_clipboard_info
)
361 if (!wine_server_call_err( req
)) hWndViewer
= wine_server_ptr_handle( reply
->old_viewer
);
365 TRACE(" hWndViewer=%p\n", hWndViewer
);
371 /**************************************************************************
372 * ChangeClipboardChain (USER32.@)
374 BOOL WINAPI
ChangeClipboardChain(HWND hWnd
, HWND hWndNext
)
377 HWND hWndViewer
= GetClipboardViewer();
381 if (WIN_GetFullHandle(hWnd
) == hWndViewer
)
382 CLIPBOARD_SetClipboardViewer(WIN_GetFullHandle(hWndNext
));
384 bRet
= !SendMessageW(hWndViewer
, WM_CHANGECBCHAIN
, (WPARAM
)hWnd
, (LPARAM
)hWndNext
);
387 ERR("hWndViewer is lost\n");
393 /**************************************************************************
394 * SetClipboardData (USER32.@)
396 HANDLE WINAPI
SetClipboardData(UINT wFormat
, HANDLE hData
)
398 CLIPBOARDINFO cbinfo
;
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");
412 if (USER_Driver
->pSetClipboardData(wFormat
, hData
, cbinfo
.flags
& CB_OWNER
))
415 bCBHasChanged
= TRUE
;
422 /**************************************************************************
423 * CountClipboardFormats (USER32.@)
425 INT WINAPI
CountClipboardFormats(void)
427 INT count
= USER_Driver
->pCountClipboardFormats();
428 TRACE("returning %d\n", 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
);
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
);
464 /**************************************************************************
465 * GetClipboardData (USER32.@)
467 HANDLE WINAPI
GetClipboardData(UINT wFormat
)
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
);
482 hData
= USER_Driver
->pGetClipboardData( wFormat
);
484 TRACE("returning %p\n", hData
);
489 /**************************************************************************
490 * GetPriorityClipboardFormat (USER32.@)
492 INT WINAPI
GetPriorityClipboardFormat(UINT
*list
, INT nCount
)
498 if(CountClipboardFormats() == 0)
501 for (i
= 0; i
< nCount
; i
++)
502 if (IsClipboardFormatAvailable(list
[i
]))
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
)
521 SERVER_START_REQ( set_clipboard_info
)
524 if (!wine_server_call_err( req
)) seqno
= reply
->seqno
;
528 TRACE("returning %x\n", seqno
);
532 /**************************************************************************
533 * AddClipboardFormatListener (USER32.@)
535 BOOL WINAPI
AddClipboardFormatListener(HWND hwnd
)
537 FIXME("%p: stub\n", hwnd
);
541 /**************************************************************************
542 * RemoveClipboardFormatListener (USER32.@)
544 BOOL WINAPI
RemoveClipboardFormatListener(HWND hwnd
)
546 FIXME("%p: stub\n", hwnd
);