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_GetClipboardInfo
76 static BOOL
CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo
)
80 SERVER_START_REQ( set_clipboard_info
)
84 if (((bRet
= !wine_server_call_err( req
))))
86 cbInfo
->hWndOpen
= wine_server_ptr_handle( reply
->old_clipboard
);
87 cbInfo
->hWndOwner
= wine_server_ptr_handle( reply
->old_owner
);
88 cbInfo
->flags
= reply
->flags
;
97 /**************************************************************************
98 * CLIPBOARD_ReleaseOwner
100 void CLIPBOARD_ReleaseOwner( HWND hwnd
)
102 SERVER_START_REQ( set_clipboard_info
)
104 req
->flags
= SET_CB_RELOWNER
| SET_CB_SEQNO
;
105 req
->owner
= wine_server_user_handle( hwnd
);
106 wine_server_call( req
);
112 /**************************************************************************
113 * WIN32 Clipboard implementation
114 **************************************************************************/
116 /**************************************************************************
117 * RegisterClipboardFormatW (USER32.@)
119 UINT WINAPI
RegisterClipboardFormatW( LPCWSTR name
)
121 return GlobalAddAtomW( name
);
125 /**************************************************************************
126 * RegisterClipboardFormatA (USER32.@)
128 UINT WINAPI
RegisterClipboardFormatA( LPCSTR name
)
130 return GlobalAddAtomA( name
);
134 /**************************************************************************
135 * GetClipboardFormatNameW (USER32.@)
137 INT WINAPI
GetClipboardFormatNameW(UINT wFormat
, LPWSTR retStr
, INT maxlen
)
139 if (wFormat
< MAXINTATOM
) return 0;
140 return GlobalGetAtomNameW( wFormat
, retStr
, maxlen
);
144 /**************************************************************************
145 * GetClipboardFormatNameA (USER32.@)
147 INT WINAPI
GetClipboardFormatNameA(UINT wFormat
, LPSTR retStr
, INT maxlen
)
149 if (wFormat
< MAXINTATOM
) return 0;
150 return GlobalGetAtomNameA( wFormat
, retStr
, maxlen
);
154 /**************************************************************************
155 * OpenClipboard (USER32.@)
157 BOOL WINAPI
OpenClipboard( HWND hwnd
)
161 TRACE( "%p\n", hwnd
);
163 SERVER_START_REQ( open_clipboard
)
165 req
->window
= wine_server_user_handle( hwnd
);
166 if ((ret
= !wine_server_call( req
)))
168 if (!reply
->owner
) bCBHasChanged
= FALSE
;
177 /**************************************************************************
178 * CloseClipboard (USER32.@)
180 BOOL WINAPI
CloseClipboard(void)
183 BOOL ret
, owner
= FALSE
;
185 TRACE("() Changed=%d\n", bCBHasChanged
);
187 SERVER_START_REQ( close_clipboard
)
189 req
->changed
= bCBHasChanged
;
190 if ((ret
= !wine_server_call_err( req
)))
192 viewer
= wine_server_ptr_handle( reply
->viewer
);
193 owner
= reply
->owner
;
198 if (!ret
) return FALSE
;
200 if (bCBHasChanged
&& owner
)
202 USER_Driver
->pEndClipboardUpdate();
203 if (viewer
) SendNotifyMessageW(viewer
, WM_DRAWCLIPBOARD
, (WPARAM
) GetClipboardOwner(), 0);
205 bCBHasChanged
= FALSE
;
210 /**************************************************************************
211 * EmptyClipboard (USER32.@)
212 * Empties and acquires ownership of the clipboard
214 BOOL WINAPI
EmptyClipboard(void)
217 HWND owner
= GetClipboardOwner();
221 if (owner
) SendMessageTimeoutW( owner
, WM_DESTROYCLIPBOARD
, 0, 0, SMTO_ABORTIFHUNG
, 5000, NULL
);
223 SERVER_START_REQ( empty_clipboard
)
225 ret
= !wine_server_call_err( req
);
231 USER_Driver
->pEmptyClipboard();
232 bCBHasChanged
= TRUE
;
238 /**************************************************************************
239 * GetClipboardOwner (USER32.@)
240 * FIXME: Can't return the owner if the clipboard is owned by an external X-app
242 HWND WINAPI
GetClipboardOwner(void)
246 SERVER_START_REQ( set_clipboard_info
)
249 if (!wine_server_call_err( req
)) hWndOwner
= wine_server_ptr_handle( reply
->old_owner
);
253 TRACE(" hWndOwner(%p)\n", hWndOwner
);
259 /**************************************************************************
260 * GetOpenClipboardWindow (USER32.@)
262 HWND WINAPI
GetOpenClipboardWindow(void)
266 SERVER_START_REQ( set_clipboard_info
)
269 if (!wine_server_call_err( req
)) hWndOpen
= wine_server_ptr_handle( reply
->old_clipboard
);
273 TRACE(" hWndClipWindow(%p)\n", hWndOpen
);
279 /**************************************************************************
280 * SetClipboardViewer (USER32.@)
282 HWND WINAPI
SetClipboardViewer( HWND hwnd
)
284 HWND prev
= 0, owner
= 0;
286 SERVER_START_REQ( set_clipboard_viewer
)
288 req
->viewer
= wine_server_user_handle( hwnd
);
289 if (!wine_server_call_err( req
))
291 prev
= wine_server_ptr_handle( reply
->old_viewer
);
292 owner
= wine_server_ptr_handle( reply
->owner
);
297 if (hwnd
) SendNotifyMessageW( hwnd
, WM_DRAWCLIPBOARD
, (WPARAM
)owner
, 0 );
299 TRACE( "(%p): returning %p\n", hwnd
, prev
);
304 /**************************************************************************
305 * GetClipboardViewer (USER32.@)
307 HWND WINAPI
GetClipboardViewer(void)
311 SERVER_START_REQ( set_clipboard_info
)
314 if (!wine_server_call_err( req
)) hWndViewer
= wine_server_ptr_handle( reply
->old_viewer
);
318 TRACE(" hWndViewer=%p\n", hWndViewer
);
324 /**************************************************************************
325 * ChangeClipboardChain (USER32.@)
327 BOOL WINAPI
ChangeClipboardChain( HWND hwnd
, HWND next
)
332 if (!hwnd
) return FALSE
;
334 SERVER_START_REQ( set_clipboard_viewer
)
336 req
->viewer
= wine_server_user_handle( next
);
337 req
->previous
= wine_server_user_handle( hwnd
);
338 status
= wine_server_call( req
);
339 viewer
= wine_server_ptr_handle( reply
->old_viewer
);
343 if (status
== STATUS_PENDING
)
344 return !SendMessageW( viewer
, WM_CHANGECBCHAIN
, (WPARAM
)hwnd
, (LPARAM
)next
);
346 if (status
) SetLastError( RtlNtStatusToDosError( status
));
351 /**************************************************************************
352 * SetClipboardData (USER32.@)
354 HANDLE WINAPI
SetClipboardData(UINT wFormat
, HANDLE hData
)
356 CLIPBOARDINFO cbinfo
;
359 TRACE("(%04X, %p) !\n", wFormat
, hData
);
363 SetLastError( ERROR_CLIPBOARD_NOT_OPEN
);
367 /* If it's not owned, data can only be set if the format isn't
368 available and its rendering is not delayed */
369 if (!CLIPBOARD_GetClipboardInfo(&cbinfo
) ||
370 (!(cbinfo
.flags
& CB_OWNER
) && !hData
))
372 WARN("Clipboard not owned by calling task. Operation failed.\n");
376 if (USER_Driver
->pSetClipboardData(wFormat
, hData
, cbinfo
.flags
& CB_OWNER
))
379 bCBHasChanged
= TRUE
;
386 /**************************************************************************
387 * CountClipboardFormats (USER32.@)
389 INT WINAPI
CountClipboardFormats(void)
391 INT count
= USER_Driver
->pCountClipboardFormats();
392 TRACE("returning %d\n", count
);
397 /**************************************************************************
398 * EnumClipboardFormats (USER32.@)
400 UINT WINAPI
EnumClipboardFormats(UINT wFormat
)
402 CLIPBOARDINFO cbinfo
;
404 TRACE("(%04X)\n", wFormat
);
406 if (!CLIPBOARD_GetClipboardInfo(&cbinfo
) ||
407 (~cbinfo
.flags
& CB_OPEN
))
409 WARN("Clipboard not opened by calling task.\n");
410 SetLastError(ERROR_CLIPBOARD_NOT_OPEN
);
413 return USER_Driver
->pEnumClipboardFormats(wFormat
);
417 /**************************************************************************
418 * IsClipboardFormatAvailable (USER32.@)
420 BOOL WINAPI
IsClipboardFormatAvailable(UINT wFormat
)
422 BOOL bret
= USER_Driver
->pIsClipboardFormatAvailable(wFormat
);
423 TRACE("%04x, returning %d\n", wFormat
, bret
);
428 /**************************************************************************
429 * GetClipboardData (USER32.@)
431 HANDLE WINAPI
GetClipboardData(UINT wFormat
)
434 CLIPBOARDINFO cbinfo
;
436 TRACE("%04x\n", wFormat
);
438 if (!CLIPBOARD_GetClipboardInfo(&cbinfo
) ||
439 (~cbinfo
.flags
& CB_OPEN
))
441 WARN("Clipboard not opened by calling task.\n");
442 SetLastError(ERROR_CLIPBOARD_NOT_OPEN
);
446 hData
= USER_Driver
->pGetClipboardData( wFormat
);
448 TRACE("returning %p\n", hData
);
453 /**************************************************************************
454 * GetPriorityClipboardFormat (USER32.@)
456 INT WINAPI
GetPriorityClipboardFormat(UINT
*list
, INT nCount
)
462 if(CountClipboardFormats() == 0)
465 for (i
= 0; i
< nCount
; i
++)
466 if (IsClipboardFormatAvailable(list
[i
]))
473 /**************************************************************************
474 * GetClipboardSequenceNumber (USER32.@)
475 * Supported on Win2k/Win98
476 * MSDN: Windows clipboard code keeps a serial number for the clipboard
477 * for each window station. The number is incremented whenever the
478 * contents change or are emptied.
479 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
481 DWORD WINAPI
GetClipboardSequenceNumber(VOID
)
485 SERVER_START_REQ( set_clipboard_info
)
488 if (!wine_server_call_err( req
)) seqno
= reply
->seqno
;
492 TRACE("returning %x\n", seqno
);
496 /**************************************************************************
497 * AddClipboardFormatListener (USER32.@)
499 BOOL WINAPI
AddClipboardFormatListener(HWND hwnd
)
501 FIXME("%p: stub\n", hwnd
);
505 /**************************************************************************
506 * RemoveClipboardFormatListener (USER32.@)
508 BOOL WINAPI
RemoveClipboardFormatListener(HWND hwnd
)
510 FIXME("%p: stub\n", hwnd
);