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
);
59 * Indicates if data has changed since open.
61 static BOOL bCBHasChanged
= FALSE
;
64 /**************************************************************************
67 static UINT
get_clipboard_flags(void)
71 SERVER_START_REQ( set_clipboard_info
)
73 if (!wine_server_call_err( req
)) ret
= reply
->flags
;
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
);
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
)
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
;
161 /**************************************************************************
162 * CloseClipboard (USER32.@)
164 BOOL WINAPI
CloseClipboard(void)
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
;
182 if (!ret
) return FALSE
;
186 if (owner
) USER_Driver
->pEndClipboardUpdate();
187 bCBHasChanged
= FALSE
;
188 if (viewer
) SendNotifyMessageW(viewer
, WM_DRAWCLIPBOARD
, (WPARAM
) GetClipboardOwner(), 0);
194 /**************************************************************************
195 * EmptyClipboard (USER32.@)
196 * Empties and acquires ownership of the clipboard
198 BOOL WINAPI
EmptyClipboard(void)
201 HWND owner
= GetClipboardOwner();
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
);
215 USER_Driver
->pEmptyClipboard();
216 bCBHasChanged
= TRUE
;
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)
230 SERVER_START_REQ( get_clipboard_info
)
232 if (!wine_server_call_err( req
)) hWndOwner
= wine_server_ptr_handle( reply
->owner
);
236 TRACE(" hWndOwner(%p)\n", hWndOwner
);
242 /**************************************************************************
243 * GetOpenClipboardWindow (USER32.@)
245 HWND WINAPI
GetOpenClipboardWindow(void)
249 SERVER_START_REQ( get_clipboard_info
)
251 if (!wine_server_call_err( req
)) hWndOpen
= wine_server_ptr_handle( reply
->window
);
255 TRACE(" hWndClipWindow(%p)\n", 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
);
279 if (hwnd
) SendNotifyMessageW( hwnd
, WM_DRAWCLIPBOARD
, (WPARAM
)owner
, 0 );
281 TRACE( "(%p): returning %p\n", hwnd
, prev
);
286 /**************************************************************************
287 * GetClipboardViewer (USER32.@)
289 HWND WINAPI
GetClipboardViewer(void)
293 SERVER_START_REQ( get_clipboard_info
)
295 if (!wine_server_call_err( req
)) hWndViewer
= wine_server_ptr_handle( reply
->viewer
);
299 TRACE(" hWndViewer=%p\n", hWndViewer
);
305 /**************************************************************************
306 * ChangeClipboardChain (USER32.@)
308 BOOL WINAPI
ChangeClipboardChain( HWND hwnd
, HWND next
)
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
);
324 if (status
== STATUS_PENDING
)
325 return !SendMessageW( viewer
, WM_CHANGECBCHAIN
, (WPARAM
)hwnd
, (LPARAM
)next
);
327 if (status
) SetLastError( RtlNtStatusToDosError( status
));
332 /**************************************************************************
333 * SetClipboardData (USER32.@)
335 HANDLE WINAPI
SetClipboardData(UINT wFormat
, HANDLE hData
)
340 TRACE("(%04X, %p) !\n", wFormat
, hData
);
344 SetLastError( ERROR_CLIPBOARD_NOT_OPEN
);
348 flags
= get_clipboard_flags();
349 if (!(flags
& CB_OPEN_ANY
))
351 SetLastError( ERROR_CLIPBOARD_NOT_OPEN
);
355 if (USER_Driver
->pSetClipboardData(wFormat
, hData
, flags
& CB_OWNER
))
358 bCBHasChanged
= TRUE
;
365 /**************************************************************************
366 * CountClipboardFormats (USER32.@)
368 INT WINAPI
CountClipboardFormats(void)
370 INT count
= USER_Driver
->pCountClipboardFormats();
371 TRACE("returning %d\n", 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
);
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
);
404 /**************************************************************************
405 * GetClipboardData (USER32.@)
407 HANDLE WINAPI
GetClipboardData(UINT wFormat
)
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
);
420 hData
= USER_Driver
->pGetClipboardData( wFormat
);
422 TRACE("returning %p\n", hData
);
427 /**************************************************************************
428 * GetPriorityClipboardFormat (USER32.@)
430 INT WINAPI
GetPriorityClipboardFormat(UINT
*list
, INT nCount
)
436 if(CountClipboardFormats() == 0)
439 for (i
= 0; i
< nCount
; i
++)
440 if (IsClipboardFormatAvailable(list
[i
]))
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
)
459 SERVER_START_REQ( get_clipboard_info
)
461 if (!wine_server_call_err( req
)) seqno
= reply
->seqno
;
465 TRACE("returning %x\n", seqno
);
469 /**************************************************************************
470 * AddClipboardFormatListener (USER32.@)
472 BOOL WINAPI
AddClipboardFormatListener(HWND hwnd
)
474 FIXME("%p: stub\n", hwnd
);
478 /**************************************************************************
479 * RemoveClipboardFormatListener (USER32.@)
481 BOOL WINAPI
RemoveClipboardFormatListener(HWND hwnd
)
483 FIXME("%p: stub\n", hwnd
);