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
)
183 TRACE("(%p)...\n", hWnd
);
185 SERVER_START_REQ( set_clipboard_info
)
187 req
->flags
= SET_CB_OPEN
;
188 req
->clipboard
= wine_server_user_handle( hWnd
);
189 if ((bRet
= !wine_server_call( req
)))
190 flags
= reply
->flags
;
194 if (bRet
&& !(flags
& CB_PROCESS
))
196 bCBHasChanged
= FALSE
;
199 TRACE(" returning %i\n", bRet
);
205 /**************************************************************************
206 * CloseClipboard (USER32.@)
208 BOOL WINAPI
CloseClipboard(void)
214 TRACE("() Changed=%d\n", bCBHasChanged
);
216 SERVER_START_REQ( set_clipboard_info
)
218 req
->flags
= SET_CB_CLOSE
;
219 if (bCBHasChanged
) req
->flags
|= SET_CB_SEQNO
;
220 if ((ret
= !wine_server_call_err( req
)))
222 viewer
= wine_server_ptr_handle( reply
->old_viewer
);
223 flags
= reply
->flags
;
228 if (!ret
) return FALSE
;
230 if (bCBHasChanged
&& (flags
& CB_PROCESS
))
232 USER_Driver
->pEndClipboardUpdate();
233 if (viewer
) SendNotifyMessageW(viewer
, WM_DRAWCLIPBOARD
, (WPARAM
) GetClipboardOwner(), 0);
235 bCBHasChanged
= FALSE
;
240 /**************************************************************************
241 * EmptyClipboard (USER32.@)
242 * Empties and acquires ownership of the clipboard
244 BOOL WINAPI
EmptyClipboard(void)
247 HWND owner
= GetClipboardOwner();
251 if (owner
) SendMessageTimeoutW( owner
, WM_DESTROYCLIPBOARD
, 0, 0, SMTO_ABORTIFHUNG
, 5000, NULL
);
253 SERVER_START_REQ( empty_clipboard
)
255 ret
= !wine_server_call_err( req
);
261 USER_Driver
->pEmptyClipboard();
262 bCBHasChanged
= TRUE
;
268 /**************************************************************************
269 * GetClipboardOwner (USER32.@)
270 * FIXME: Can't return the owner if the clipboard is owned by an external X-app
272 HWND WINAPI
GetClipboardOwner(void)
276 SERVER_START_REQ( set_clipboard_info
)
279 if (!wine_server_call_err( req
)) hWndOwner
= wine_server_ptr_handle( reply
->old_owner
);
283 TRACE(" hWndOwner(%p)\n", hWndOwner
);
289 /**************************************************************************
290 * GetOpenClipboardWindow (USER32.@)
292 HWND WINAPI
GetOpenClipboardWindow(void)
296 SERVER_START_REQ( set_clipboard_info
)
299 if (!wine_server_call_err( req
)) hWndOpen
= wine_server_ptr_handle( reply
->old_clipboard
);
303 TRACE(" hWndClipWindow(%p)\n", hWndOpen
);
309 /**************************************************************************
310 * SetClipboardViewer (USER32.@)
312 HWND WINAPI
SetClipboardViewer( HWND hWnd
)
314 HWND hwndPrev
= CLIPBOARD_SetClipboardViewer(hWnd
);
317 SendNotifyMessageW(hWnd
, WM_DRAWCLIPBOARD
, (WPARAM
) GetClipboardOwner(), 0);
318 TRACE("(%p): returning %p\n", hWnd
, hwndPrev
);
324 /**************************************************************************
325 * GetClipboardViewer (USER32.@)
327 HWND WINAPI
GetClipboardViewer(void)
331 SERVER_START_REQ( set_clipboard_info
)
334 if (!wine_server_call_err( req
)) hWndViewer
= wine_server_ptr_handle( reply
->old_viewer
);
338 TRACE(" hWndViewer=%p\n", hWndViewer
);
344 /**************************************************************************
345 * ChangeClipboardChain (USER32.@)
347 BOOL WINAPI
ChangeClipboardChain(HWND hWnd
, HWND hWndNext
)
350 HWND hWndViewer
= GetClipboardViewer();
354 if (WIN_GetFullHandle(hWnd
) == hWndViewer
)
355 CLIPBOARD_SetClipboardViewer(WIN_GetFullHandle(hWndNext
));
357 bRet
= !SendMessageW(hWndViewer
, WM_CHANGECBCHAIN
, (WPARAM
)hWnd
, (LPARAM
)hWndNext
);
360 ERR("hWndViewer is lost\n");
366 /**************************************************************************
367 * SetClipboardData (USER32.@)
369 HANDLE WINAPI
SetClipboardData(UINT wFormat
, HANDLE hData
)
371 CLIPBOARDINFO cbinfo
;
374 TRACE("(%04X, %p) !\n", wFormat
, hData
);
378 SetLastError( ERROR_CLIPBOARD_NOT_OPEN
);
382 /* If it's not owned, data can only be set if the format isn't
383 available and its rendering is not delayed */
384 if (!CLIPBOARD_GetClipboardInfo(&cbinfo
) ||
385 (!(cbinfo
.flags
& CB_OWNER
) && !hData
))
387 WARN("Clipboard not owned by calling task. Operation failed.\n");
391 if (USER_Driver
->pSetClipboardData(wFormat
, hData
, cbinfo
.flags
& CB_OWNER
))
394 bCBHasChanged
= TRUE
;
401 /**************************************************************************
402 * CountClipboardFormats (USER32.@)
404 INT WINAPI
CountClipboardFormats(void)
406 INT count
= USER_Driver
->pCountClipboardFormats();
407 TRACE("returning %d\n", count
);
412 /**************************************************************************
413 * EnumClipboardFormats (USER32.@)
415 UINT WINAPI
EnumClipboardFormats(UINT wFormat
)
417 CLIPBOARDINFO cbinfo
;
419 TRACE("(%04X)\n", wFormat
);
421 if (!CLIPBOARD_GetClipboardInfo(&cbinfo
) ||
422 (~cbinfo
.flags
& CB_OPEN
))
424 WARN("Clipboard not opened by calling task.\n");
425 SetLastError(ERROR_CLIPBOARD_NOT_OPEN
);
428 return USER_Driver
->pEnumClipboardFormats(wFormat
);
432 /**************************************************************************
433 * IsClipboardFormatAvailable (USER32.@)
435 BOOL WINAPI
IsClipboardFormatAvailable(UINT wFormat
)
437 BOOL bret
= USER_Driver
->pIsClipboardFormatAvailable(wFormat
);
438 TRACE("%04x, returning %d\n", wFormat
, bret
);
443 /**************************************************************************
444 * GetClipboardData (USER32.@)
446 HANDLE WINAPI
GetClipboardData(UINT wFormat
)
449 CLIPBOARDINFO cbinfo
;
451 TRACE("%04x\n", wFormat
);
453 if (!CLIPBOARD_GetClipboardInfo(&cbinfo
) ||
454 (~cbinfo
.flags
& CB_OPEN
))
456 WARN("Clipboard not opened by calling task.\n");
457 SetLastError(ERROR_CLIPBOARD_NOT_OPEN
);
461 hData
= USER_Driver
->pGetClipboardData( wFormat
);
463 TRACE("returning %p\n", hData
);
468 /**************************************************************************
469 * GetPriorityClipboardFormat (USER32.@)
471 INT WINAPI
GetPriorityClipboardFormat(UINT
*list
, INT nCount
)
477 if(CountClipboardFormats() == 0)
480 for (i
= 0; i
< nCount
; i
++)
481 if (IsClipboardFormatAvailable(list
[i
]))
488 /**************************************************************************
489 * GetClipboardSequenceNumber (USER32.@)
490 * Supported on Win2k/Win98
491 * MSDN: Windows clipboard code keeps a serial number for the clipboard
492 * for each window station. The number is incremented whenever the
493 * contents change or are emptied.
494 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
496 DWORD WINAPI
GetClipboardSequenceNumber(VOID
)
500 SERVER_START_REQ( set_clipboard_info
)
503 if (!wine_server_call_err( req
)) seqno
= reply
->seqno
;
507 TRACE("returning %x\n", seqno
);
511 /**************************************************************************
512 * AddClipboardFormatListener (USER32.@)
514 BOOL WINAPI
AddClipboardFormatListener(HWND hwnd
)
516 FIXME("%p: stub\n", hwnd
);
520 /**************************************************************************
521 * RemoveClipboardFormatListener (USER32.@)
523 BOOL WINAPI
RemoveClipboardFormatListener(HWND hwnd
)
525 FIXME("%p: stub\n", hwnd
);