wined3d: Validate (2D) texture dimensions in texture_init().
[wine.git] / dlls / user32 / clipboard.c
blobdec738afa75d36b41ca4c064bf4d343d2fcb9cc7
1 /*
2 * WIN32 clipboard implementation
4 * Copyright 1994 Martin Ayotte
5 * 1996 Alex Korobka
6 * 1999 Noel Borthwick
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
23 * NOTES:
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)
32 #include "config.h"
33 #include "wine/port.h"
35 #include <stdarg.h>
36 #include <stdlib.h>
37 #include <sys/types.h>
38 #include <fcntl.h>
39 #ifdef HAVE_UNISTD_H
40 # include <unistd.h>
41 #endif
42 #include <string.h>
44 #include "windef.h"
45 #include "winbase.h"
46 #include "wingdi.h"
47 #include "winuser.h"
48 #include "winerror.h"
49 #include "user_private.h"
50 #include "win.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
60 typedef struct
62 HWND hWndOpen;
63 HWND hWndOwner;
64 UINT flags;
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)
78 BOOL bRet;
80 SERVER_START_REQ( set_clipboard_info )
82 req->flags = 0;
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;
91 SERVER_END_REQ;
93 return bRet;
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 );
108 SERVER_END_REQ;
112 /**************************************************************************
113 * CLIPBOARD_SetClipboardViewer
115 static HWND CLIPBOARD_SetClipboardViewer( HWND hWnd )
117 HWND hwndPrev = 0;
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 );
126 SERVER_END_REQ;
128 return hwndPrev;
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 )
180 BOOL bRet;
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 );
190 SERVER_END_REQ;
192 TRACE(" returning %i\n", bRet);
194 return bRet;
198 /**************************************************************************
199 * CloseClipboard (USER32.@)
201 BOOL WINAPI CloseClipboard(void)
203 HWND viewer = 0;
204 BOOL ret;
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 );
215 SERVER_END_REQ;
217 if (!ret) return FALSE;
219 if (bCBHasChanged)
221 USER_Driver->pEndClipboardUpdate();
222 bCBHasChanged = FALSE;
223 if (viewer) SendNotifyMessageW(viewer, WM_DRAWCLIPBOARD, (WPARAM) GetClipboardOwner(), 0);
225 return TRUE;
229 /**************************************************************************
230 * EmptyClipboard (USER32.@)
231 * Empties and acquires ownership of the clipboard
233 BOOL WINAPI EmptyClipboard(void)
235 BOOL ret;
236 HWND owner = GetClipboardOwner();
238 TRACE("()\n");
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 );
246 SERVER_END_REQ;
248 if (ret)
250 USER_Driver->pEmptyClipboard();
251 bCBHasChanged = TRUE;
253 return ret;
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)
263 HWND hWndOwner = 0;
265 SERVER_START_REQ( set_clipboard_info )
267 req->flags = 0;
268 if (!wine_server_call_err( req )) hWndOwner = wine_server_ptr_handle( reply->old_owner );
270 SERVER_END_REQ;
272 TRACE(" hWndOwner(%p)\n", hWndOwner);
274 return hWndOwner;
278 /**************************************************************************
279 * GetOpenClipboardWindow (USER32.@)
281 HWND WINAPI GetOpenClipboardWindow(void)
283 HWND hWndOpen = 0;
285 SERVER_START_REQ( set_clipboard_info )
287 req->flags = 0;
288 if (!wine_server_call_err( req )) hWndOpen = wine_server_ptr_handle( reply->old_clipboard );
290 SERVER_END_REQ;
292 TRACE(" hWndClipWindow(%p)\n", hWndOpen);
294 return hWndOpen;
298 /**************************************************************************
299 * SetClipboardViewer (USER32.@)
301 HWND WINAPI SetClipboardViewer( HWND hWnd )
303 HWND hwndPrev = CLIPBOARD_SetClipboardViewer(hWnd);
305 if (hWnd)
306 SendNotifyMessageW(hWnd, WM_DRAWCLIPBOARD, (WPARAM) GetClipboardOwner(), 0);
307 TRACE("(%p): returning %p\n", hWnd, hwndPrev);
309 return hwndPrev;
313 /**************************************************************************
314 * GetClipboardViewer (USER32.@)
316 HWND WINAPI GetClipboardViewer(void)
318 HWND hWndViewer = 0;
320 SERVER_START_REQ( set_clipboard_info )
322 req->flags = 0;
323 if (!wine_server_call_err( req )) hWndViewer = wine_server_ptr_handle( reply->old_viewer );
325 SERVER_END_REQ;
327 TRACE(" hWndViewer=%p\n", hWndViewer);
329 return hWndViewer;
333 /**************************************************************************
334 * ChangeClipboardChain (USER32.@)
336 BOOL WINAPI ChangeClipboardChain(HWND hWnd, HWND hWndNext)
338 BOOL bRet = TRUE;
339 HWND hWndViewer = GetClipboardViewer();
341 if (hWndViewer)
343 if (WIN_GetFullHandle(hWnd) == hWndViewer)
344 CLIPBOARD_SetClipboardViewer(WIN_GetFullHandle(hWndNext));
345 else
346 bRet = !SendMessageW(hWndViewer, WM_CHANGECBCHAIN, (WPARAM)hWnd, (LPARAM)hWndNext);
348 else
349 ERR("hWndViewer is lost\n");
351 return bRet;
355 /**************************************************************************
356 * SetClipboardData (USER32.@)
358 HANDLE WINAPI SetClipboardData(UINT wFormat, HANDLE hData)
360 CLIPBOARDINFO cbinfo;
361 HANDLE hResult = 0;
363 TRACE("(%04X, %p) !\n", wFormat, hData);
365 if (!wFormat)
367 SetLastError( ERROR_CLIPBOARD_NOT_OPEN );
368 return 0;
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");
377 return 0;
380 if (USER_Driver->pSetClipboardData(wFormat, hData, cbinfo.flags & CB_OWNER))
382 hResult = hData;
383 bCBHasChanged = TRUE;
386 return hResult;
390 /**************************************************************************
391 * CountClipboardFormats (USER32.@)
393 INT WINAPI CountClipboardFormats(void)
395 INT count = USER_Driver->pCountClipboardFormats();
396 TRACE("returning %d\n", count);
397 return 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);
415 return 0;
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);
428 return bret;
432 /**************************************************************************
433 * GetClipboardData (USER32.@)
435 HANDLE WINAPI GetClipboardData(UINT wFormat)
437 HANDLE hData = 0;
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);
447 return 0;
450 hData = USER_Driver->pGetClipboardData( wFormat );
452 TRACE("returning %p\n", hData);
453 return hData;
457 /**************************************************************************
458 * GetPriorityClipboardFormat (USER32.@)
460 INT WINAPI GetPriorityClipboardFormat(UINT *list, INT nCount)
462 int i;
464 TRACE("()\n");
466 if(CountClipboardFormats() == 0)
467 return 0;
469 for (i = 0; i < nCount; i++)
470 if (IsClipboardFormatAvailable(list[i]))
471 return list[i];
473 return -1;
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)
487 DWORD seqno = 0;
489 SERVER_START_REQ( set_clipboard_info )
491 req->flags = 0;
492 if (!wine_server_call_err( req )) seqno = reply->seqno;
494 SERVER_END_REQ;
496 TRACE("returning %x\n", seqno);
497 return seqno;
500 /**************************************************************************
501 * AddClipboardFormatListener (USER32.@)
503 BOOL WINAPI AddClipboardFormatListener(HWND hwnd)
505 FIXME("%p: stub\n", hwnd);
506 return TRUE;
509 /**************************************************************************
510 * RemoveClipboardFormatListener (USER32.@)
512 BOOL WINAPI RemoveClipboardFormatListener(HWND hwnd)
514 FIXME("%p: stub\n", hwnd);
515 return TRUE;