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 * CLIPBOARD_SetClipboardViewer
115 static HWND
CLIPBOARD_SetClipboardViewer( HWND hWnd
)
119 SERVER_START_REQ( set_clipboard_info
)
121 req
->flags
= SET_CB_VIEWER
;
122 req
->viewer
= wine_server_user_handle( hWnd
);
123 if (!wine_server_call_err( req
))
124 hwndPrev
= wine_server_ptr_handle( reply
->old_viewer
);
131 /**************************************************************************
132 * WIN32 Clipboard implementation
133 **************************************************************************/
135 /**************************************************************************
136 * RegisterClipboardFormatW (USER32.@)
138 UINT WINAPI
RegisterClipboardFormatW( LPCWSTR name
)
140 return GlobalAddAtomW( name
);
144 /**************************************************************************
145 * RegisterClipboardFormatA (USER32.@)
147 UINT WINAPI
RegisterClipboardFormatA( LPCSTR name
)
149 return GlobalAddAtomA( name
);
153 /**************************************************************************
154 * GetClipboardFormatNameW (USER32.@)
156 INT WINAPI
GetClipboardFormatNameW(UINT wFormat
, LPWSTR retStr
, INT maxlen
)
158 if (wFormat
< MAXINTATOM
) return 0;
159 return GlobalGetAtomNameW( wFormat
, retStr
, maxlen
);
163 /**************************************************************************
164 * GetClipboardFormatNameA (USER32.@)
166 INT WINAPI
GetClipboardFormatNameA(UINT wFormat
, LPSTR retStr
, INT maxlen
)
168 if (wFormat
< MAXINTATOM
) return 0;
169 return GlobalGetAtomNameA( wFormat
, retStr
, maxlen
);
173 /**************************************************************************
174 * OpenClipboard (USER32.@)
176 * Note: Netscape uses NULL hWnd to open the clipboard.
178 BOOL WINAPI
OpenClipboard( HWND hWnd
)
182 TRACE("(%p)...\n", hWnd
);
184 SERVER_START_REQ( set_clipboard_info
)
186 req
->flags
= SET_CB_OPEN
;
187 req
->clipboard
= wine_server_user_handle( hWnd
);
188 bRet
= !wine_server_call( req
);
192 TRACE(" returning %i\n", bRet
);
198 /**************************************************************************
199 * CloseClipboard (USER32.@)
201 BOOL WINAPI
CloseClipboard(void)
206 TRACE("() Changed=%d\n", bCBHasChanged
);
208 SERVER_START_REQ( set_clipboard_info
)
210 req
->flags
= SET_CB_CLOSE
;
211 if (bCBHasChanged
) req
->flags
|= SET_CB_SEQNO
;
212 if ((ret
= !wine_server_call_err( req
)))
213 viewer
= wine_server_ptr_handle( reply
->old_viewer
);
217 if (!ret
) return FALSE
;
221 USER_Driver
->pEndClipboardUpdate();
222 bCBHasChanged
= FALSE
;
223 if (viewer
) SendNotifyMessageW(viewer
, WM_DRAWCLIPBOARD
, (WPARAM
) GetClipboardOwner(), 0);
229 /**************************************************************************
230 * EmptyClipboard (USER32.@)
231 * Empties and acquires ownership of the clipboard
233 BOOL WINAPI
EmptyClipboard(void)
236 HWND owner
= GetClipboardOwner();
240 if (owner
) SendMessageTimeoutW( owner
, WM_DESTROYCLIPBOARD
, 0, 0, SMTO_ABORTIFHUNG
, 5000, NULL
);
242 SERVER_START_REQ( empty_clipboard
)
244 ret
= !wine_server_call_err( req
);
250 USER_Driver
->pEmptyClipboard();
251 bCBHasChanged
= TRUE
;
257 /**************************************************************************
258 * GetClipboardOwner (USER32.@)
259 * FIXME: Can't return the owner if the clipboard is owned by an external X-app
261 HWND WINAPI
GetClipboardOwner(void)
265 SERVER_START_REQ( set_clipboard_info
)
268 if (!wine_server_call_err( req
)) hWndOwner
= wine_server_ptr_handle( reply
->old_owner
);
272 TRACE(" hWndOwner(%p)\n", hWndOwner
);
278 /**************************************************************************
279 * GetOpenClipboardWindow (USER32.@)
281 HWND WINAPI
GetOpenClipboardWindow(void)
285 SERVER_START_REQ( set_clipboard_info
)
288 if (!wine_server_call_err( req
)) hWndOpen
= wine_server_ptr_handle( reply
->old_clipboard
);
292 TRACE(" hWndClipWindow(%p)\n", hWndOpen
);
298 /**************************************************************************
299 * SetClipboardViewer (USER32.@)
301 HWND WINAPI
SetClipboardViewer( HWND hWnd
)
303 HWND hwndPrev
= CLIPBOARD_SetClipboardViewer(hWnd
);
306 SendNotifyMessageW(hWnd
, WM_DRAWCLIPBOARD
, (WPARAM
) GetClipboardOwner(), 0);
307 TRACE("(%p): returning %p\n", hWnd
, hwndPrev
);
313 /**************************************************************************
314 * GetClipboardViewer (USER32.@)
316 HWND WINAPI
GetClipboardViewer(void)
320 SERVER_START_REQ( set_clipboard_info
)
323 if (!wine_server_call_err( req
)) hWndViewer
= wine_server_ptr_handle( reply
->old_viewer
);
327 TRACE(" hWndViewer=%p\n", hWndViewer
);
333 /**************************************************************************
334 * ChangeClipboardChain (USER32.@)
336 BOOL WINAPI
ChangeClipboardChain(HWND hWnd
, HWND hWndNext
)
339 HWND hWndViewer
= GetClipboardViewer();
343 if (WIN_GetFullHandle(hWnd
) == hWndViewer
)
344 CLIPBOARD_SetClipboardViewer(WIN_GetFullHandle(hWndNext
));
346 bRet
= !SendMessageW(hWndViewer
, WM_CHANGECBCHAIN
, (WPARAM
)hWnd
, (LPARAM
)hWndNext
);
349 ERR("hWndViewer is lost\n");
355 /**************************************************************************
356 * SetClipboardData (USER32.@)
358 HANDLE WINAPI
SetClipboardData(UINT wFormat
, HANDLE hData
)
360 CLIPBOARDINFO cbinfo
;
363 TRACE("(%04X, %p) !\n", wFormat
, hData
);
367 SetLastError( ERROR_CLIPBOARD_NOT_OPEN
);
371 /* If it's not owned, data can only be set if the format isn't
372 available and its rendering is not delayed */
373 if (!CLIPBOARD_GetClipboardInfo(&cbinfo
) ||
374 (!(cbinfo
.flags
& CB_OWNER
) && !hData
))
376 WARN("Clipboard not owned by calling task. Operation failed.\n");
380 if (USER_Driver
->pSetClipboardData(wFormat
, hData
, cbinfo
.flags
& CB_OWNER
))
383 bCBHasChanged
= TRUE
;
390 /**************************************************************************
391 * CountClipboardFormats (USER32.@)
393 INT WINAPI
CountClipboardFormats(void)
395 INT count
= USER_Driver
->pCountClipboardFormats();
396 TRACE("returning %d\n", count
);
401 /**************************************************************************
402 * EnumClipboardFormats (USER32.@)
404 UINT WINAPI
EnumClipboardFormats(UINT wFormat
)
406 CLIPBOARDINFO cbinfo
;
408 TRACE("(%04X)\n", wFormat
);
410 if (!CLIPBOARD_GetClipboardInfo(&cbinfo
) ||
411 (~cbinfo
.flags
& CB_OPEN
))
413 WARN("Clipboard not opened by calling task.\n");
414 SetLastError(ERROR_CLIPBOARD_NOT_OPEN
);
417 return USER_Driver
->pEnumClipboardFormats(wFormat
);
421 /**************************************************************************
422 * IsClipboardFormatAvailable (USER32.@)
424 BOOL WINAPI
IsClipboardFormatAvailable(UINT wFormat
)
426 BOOL bret
= USER_Driver
->pIsClipboardFormatAvailable(wFormat
);
427 TRACE("%04x, returning %d\n", wFormat
, bret
);
432 /**************************************************************************
433 * GetClipboardData (USER32.@)
435 HANDLE WINAPI
GetClipboardData(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
);
450 hData
= USER_Driver
->pGetClipboardData( wFormat
);
452 TRACE("returning %p\n", hData
);
457 /**************************************************************************
458 * GetPriorityClipboardFormat (USER32.@)
460 INT WINAPI
GetPriorityClipboardFormat(UINT
*list
, INT nCount
)
466 if(CountClipboardFormats() == 0)
469 for (i
= 0; i
< nCount
; i
++)
470 if (IsClipboardFormatAvailable(list
[i
]))
477 /**************************************************************************
478 * GetClipboardSequenceNumber (USER32.@)
479 * Supported on Win2k/Win98
480 * MSDN: Windows clipboard code keeps a serial number for the clipboard
481 * for each window station. The number is incremented whenever the
482 * contents change or are emptied.
483 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
485 DWORD WINAPI
GetClipboardSequenceNumber(VOID
)
489 SERVER_START_REQ( set_clipboard_info
)
492 if (!wine_server_call_err( req
)) seqno
= reply
->seqno
;
496 TRACE("returning %x\n", seqno
);
500 /**************************************************************************
501 * AddClipboardFormatListener (USER32.@)
503 BOOL WINAPI
AddClipboardFormatListener(HWND hwnd
)
505 FIXME("%p: stub\n", hwnd
);
509 /**************************************************************************
510 * RemoveClipboardFormatListener (USER32.@)
512 BOOL WINAPI
RemoveClipboardFormatListener(HWND hwnd
)
514 FIXME("%p: stub\n", hwnd
);