Janitorial: C booleans must not be compared against TRUE.
[wine/multimedia.git] / windows / clipboard.c
blobdd06c9d35015e19bc4a327b8b1619e0d749e4e0e
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "wine/winuser16.h"
50 #include "wine/winbase16.h"
51 #include "heap.h"
52 #include "user_private.h"
53 #include "win.h"
55 #include "wine/debug.h"
56 #include "wine/unicode.h"
57 #include "wine/server.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(clipboard);
61 #define CF_REGFORMATBASE 0xC000
63 typedef struct
65 HWND hWndOpen;
66 HWND hWndOwner;
67 HWND hWndViewer;
68 UINT seqno;
69 UINT flags;
70 } CLIPBOARDINFO, *LPCLIPBOARDINFO;
73 * Indicates if data has changed since open.
75 static BOOL bCBHasChanged = FALSE;
78 /**************************************************************************
79 * CLIPBOARD_SetClipboardOwner
81 BOOL CLIPBOARD_SetClipboardOwner(HWND hWnd)
83 BOOL bRet = FALSE;
85 TRACE(" hWnd(%p)\n", hWnd);
87 SERVER_START_REQ( set_clipboard_info )
89 req->flags = SET_CB_OWNER;
90 req->owner = WIN_GetFullHandle( hWnd );
92 if (wine_server_call_err( req ))
94 ERR("Failed to set clipboard owner to %p\n", hWnd);
96 else
98 bRet = TRUE;
101 SERVER_END_REQ;
103 return bRet;
107 /**************************************************************************
108 * CLIPBOARD_GetClipboardInfo
110 static BOOL CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo)
112 BOOL bRet = FALSE;
114 SERVER_START_REQ( set_clipboard_info )
116 req->flags = 0;
118 if (wine_server_call_err( req ))
120 ERR("Failed to get clipboard info\n");
122 else
124 cbInfo->hWndOpen = reply->old_clipboard;
125 cbInfo->hWndOwner = reply->old_owner;
126 cbInfo->hWndViewer = reply->old_viewer;
127 cbInfo->seqno = reply->seqno;
128 cbInfo->flags = reply->flags;
130 bRet = TRUE;
133 SERVER_END_REQ;
135 return bRet;
139 /**************************************************************************
140 * CLIPBOARD_ReleaseOwner
142 BOOL CLIPBOARD_ReleaseOwner(void)
144 BOOL bRet = FALSE;
146 SERVER_START_REQ( set_clipboard_info )
148 req->flags = SET_CB_RELOWNER | SET_CB_SEQNO;
150 if (wine_server_call_err( req ))
152 ERR("Failed to set clipboard.\n");
154 else
156 bRet = TRUE;
159 SERVER_END_REQ;
161 return bRet;
165 /**************************************************************************
166 * CLIPBOARD_OpenClipboard
168 static BOOL CLIPBOARD_OpenClipboard(HWND hWnd)
170 BOOL bRet = FALSE;
172 SERVER_START_REQ( set_clipboard_info )
174 req->flags = SET_CB_OPEN;
175 req->clipboard = WIN_GetFullHandle( hWnd );
177 if (!wine_server_call( req ))
178 bRet = TRUE;
180 SERVER_END_REQ;
182 return bRet;
186 /**************************************************************************
187 * CLIPBOARD_CloseClipboard
189 static BOOL CLIPBOARD_CloseClipboard(void)
191 BOOL bRet = FALSE;
193 TRACE(" Changed=%d\n", bCBHasChanged);
195 SERVER_START_REQ( set_clipboard_info )
197 req->flags = SET_CB_CLOSE;
199 if (bCBHasChanged)
201 req->flags |= SET_CB_SEQNO;
202 TRACE("Clipboard data changed\n");
205 if (wine_server_call_err( req ))
207 ERR("Failed to set clipboard.\n");
209 else
211 bRet = TRUE;
214 SERVER_END_REQ;
216 return bRet;
220 /**************************************************************************
221 * WIN32 Clipboard implementation
222 **************************************************************************/
224 /**************************************************************************
225 * RegisterClipboardFormatA (USER32.@)
227 UINT WINAPI RegisterClipboardFormatA(LPCSTR FormatName)
229 UINT wFormatID = 0;
231 TRACE("%s\n", debugstr_a(FormatName));
233 if (USER_Driver.pRegisterClipboardFormat)
234 wFormatID = USER_Driver.pRegisterClipboardFormat(FormatName);
236 return wFormatID;
240 /**************************************************************************
241 * RegisterClipboardFormatW (USER32.@)
243 UINT WINAPI RegisterClipboardFormatW(LPCWSTR formatName)
245 LPSTR aFormat = HEAP_strdupWtoA( GetProcessHeap(), 0, formatName );
246 UINT ret = RegisterClipboardFormatA( aFormat );
247 HeapFree( GetProcessHeap(), 0, aFormat );
248 return ret;
252 /**************************************************************************
253 * GetClipboardFormatNameA (USER32.@)
255 INT WINAPI GetClipboardFormatNameA(UINT wFormat, LPSTR retStr, INT maxlen)
257 INT len = 0;
259 TRACE("%04x,%p,%d\n", wFormat, retStr, maxlen);
261 if (USER_Driver.pGetClipboardFormatName)
262 len = USER_Driver.pGetClipboardFormatName(wFormat, retStr, maxlen);
264 return len;
268 /**************************************************************************
269 * GetClipboardFormatNameW (USER32.@)
271 INT WINAPI GetClipboardFormatNameW(UINT wFormat, LPWSTR retStr, INT maxlen)
273 INT ret;
274 LPSTR p = HeapAlloc( GetProcessHeap(), 0, maxlen );
275 if(p == NULL) return 0; /* FIXME: is this the correct failure value? */
277 ret = GetClipboardFormatNameA( wFormat, p, maxlen );
279 if (maxlen > 0 && !MultiByteToWideChar( CP_ACP, 0, p, -1, retStr, maxlen ))
280 retStr[maxlen-1] = 0;
281 HeapFree( GetProcessHeap(), 0, p );
282 return ret;
286 /**************************************************************************
287 * OpenClipboard (USER32.@)
289 * Note: Netscape uses NULL hWnd to open the clipboard.
291 BOOL WINAPI OpenClipboard( HWND hWnd )
293 BOOL bRet;
295 TRACE("(%p)...\n", hWnd);
297 bRet = CLIPBOARD_OpenClipboard(hWnd);
299 TRACE(" returning %i\n", bRet);
301 return bRet;
305 /**************************************************************************
306 * CloseClipboard (USER32.@)
308 BOOL WINAPI CloseClipboard(void)
310 BOOL bRet = FALSE;
312 TRACE("(%d)\n", bCBHasChanged);
314 if (CLIPBOARD_CloseClipboard())
316 if (bCBHasChanged)
318 HWND hWndViewer = GetClipboardViewer();
320 if (USER_Driver.pEndClipboardUpdate)
321 USER_Driver.pEndClipboardUpdate();
323 if (hWndViewer)
324 SendMessageW(hWndViewer, WM_DRAWCLIPBOARD, 0, 0);
326 bCBHasChanged = FALSE;
329 bRet = TRUE;
332 return bRet;
336 /**************************************************************************
337 * EmptyClipboard (USER32.@)
338 * Empties and acquires ownership of the clipboard
340 BOOL WINAPI EmptyClipboard(void)
342 CLIPBOARDINFO cbinfo;
344 TRACE("()\n");
346 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
347 ~cbinfo.flags & CB_OPEN)
349 WARN("Clipboard not opened by calling task!\n");
350 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
351 return FALSE;
354 /* Destroy private objects */
355 if (cbinfo.hWndOwner)
356 SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
358 /* Tell the driver to acquire the selection. The current owner
359 * will be signaled to delete it's own cache. */
361 /* Assign ownership of the clipboard to the current client. We do
362 * this before acquiring the selection so that when we do acquire the
363 * selection and the selection loser gets notified, it can check if
364 * it has lost the Wine clipboard ownership. If it did then it knows
365 * that a WM_DESTORYCLIPBOARD has already been sent. Otherwise it
366 * lost the selection to a X app and it should send the
367 * WM_DESTROYCLIPBOARD itself. */
368 CLIPBOARD_SetClipboardOwner(cbinfo.hWndOpen);
370 /* Acquire the selection. This will notify the previous owner
371 * to clear it's cache. */
372 if (USER_Driver.pAcquireClipboard)
373 USER_Driver.pAcquireClipboard(cbinfo.hWndOpen);
375 /* Empty the local cache */
376 if (USER_Driver.pEmptyClipboard)
377 USER_Driver.pEmptyClipboard(FALSE);
379 bCBHasChanged = TRUE;
381 return TRUE;
385 /**************************************************************************
386 * GetClipboardOwner (USER32.@)
387 * FIXME: Can't return the owner if the clipboard is owned by an external X-app
389 HWND WINAPI GetClipboardOwner(void)
391 HWND hWndOwner = 0;
393 SERVER_START_REQ( set_clipboard_info )
395 req->flags = 0;
396 if (!wine_server_call_err( req )) hWndOwner = reply->old_owner;
398 SERVER_END_REQ;
400 TRACE(" hWndOwner(%p)\n", hWndOwner);
402 return hWndOwner;
406 /**************************************************************************
407 * GetOpenClipboardWindow (USER32.@)
409 HWND WINAPI GetOpenClipboardWindow(void)
411 HWND hWndOpen = 0;
413 SERVER_START_REQ( set_clipboard_info )
415 req->flags = 0;
416 if (!wine_server_call_err( req )) hWndOpen = reply->old_clipboard;
418 SERVER_END_REQ;
420 TRACE(" hWndClipWindow(%p)\n", hWndOpen);
422 return hWndOpen;
426 /**************************************************************************
427 * SetClipboardViewer (USER32.@)
429 HWND WINAPI SetClipboardViewer( HWND hWnd )
431 HWND hwndPrev = 0;
433 SERVER_START_REQ( set_clipboard_info )
435 req->flags = SET_CB_VIEWER;
436 req->viewer = WIN_GetFullHandle(hWnd);
438 if (wine_server_call_err( req ))
440 ERR("Failed to set clipboard.\n");
442 else
444 hwndPrev = reply->old_viewer;
447 SERVER_END_REQ;
449 TRACE("(%p): returning %p\n", hWnd, hwndPrev);
451 return hwndPrev;
455 /**************************************************************************
456 * GetClipboardViewer (USER32.@)
458 HWND WINAPI GetClipboardViewer(void)
460 HWND hWndViewer = 0;
462 SERVER_START_REQ( set_clipboard_info )
464 req->flags = 0;
465 if (!wine_server_call_err( req )) hWndViewer = reply->old_viewer;
467 SERVER_END_REQ;
469 TRACE(" hWndViewer=%p\n", hWndViewer);
471 return hWndViewer;
475 /**************************************************************************
476 * ChangeClipboardChain (USER32.@)
478 BOOL WINAPI ChangeClipboardChain(HWND hWnd, HWND hWndNext)
480 BOOL bRet = TRUE;
481 HWND hWndViewer = GetClipboardViewer();
483 if (hWndViewer)
485 if (WIN_GetFullHandle(hWnd) == hWndViewer)
486 SetClipboardViewer(WIN_GetFullHandle(hWndNext));
487 else
488 bRet = !SendMessageW(hWndViewer, WM_CHANGECBCHAIN, (WPARAM)hWnd, (LPARAM)hWndNext);
490 else
491 ERR("hWndViewer is lost\n");
493 return bRet;
497 /**************************************************************************
498 * SetClipboardData (USER.141)
500 HANDLE16 WINAPI SetClipboardData16(UINT16 wFormat, HANDLE16 hData)
502 CLIPBOARDINFO cbinfo;
503 HANDLE16 hResult = 0;
505 TRACE("(%04X, %04x) !\n", wFormat, hData);
507 /* If it's not owned, data can only be set if the format doesn't exists
508 and its rendering is not delayed */
509 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
510 (!(cbinfo.flags & CB_OWNER) && !hData))
512 WARN("Clipboard not owned by calling task. Operation failed.\n");
513 return 0;
516 if (USER_Driver.pSetClipboardData &&
517 USER_Driver.pSetClipboardData(wFormat, hData, 0, cbinfo.flags & CB_OWNER))
519 hResult = hData;
520 bCBHasChanged = TRUE;
523 return hResult;
527 /**************************************************************************
528 * SetClipboardData (USER32.@)
530 HANDLE WINAPI SetClipboardData(UINT wFormat, HANDLE hData)
532 CLIPBOARDINFO cbinfo;
533 HANDLE hResult = 0;
535 TRACE("(%04X, %p) !\n", wFormat, hData);
537 /* If it's not owned, data can only be set if the format isn't
538 available and its rendering is not delayed */
539 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
540 (!(cbinfo.flags & CB_OWNER) && !hData))
542 WARN("Clipboard not owned by calling task. Operation failed.\n");
543 return 0;
546 if (USER_Driver.pSetClipboardData &&
547 USER_Driver.pSetClipboardData(wFormat, 0, hData, cbinfo.flags & CB_OWNER))
549 hResult = hData;
550 bCBHasChanged = TRUE;
553 return hResult;
557 /**************************************************************************
558 * CountClipboardFormats (USER32.@)
560 INT WINAPI CountClipboardFormats(void)
562 INT count = 0;
564 if (USER_Driver.pCountClipboardFormats)
565 count = USER_Driver.pCountClipboardFormats();
567 TRACE("returning %d\n", count);
568 return count;
572 /**************************************************************************
573 * EnumClipboardFormats (USER32.@)
575 UINT WINAPI EnumClipboardFormats(UINT wFormat)
577 UINT wFmt = 0;
578 CLIPBOARDINFO cbinfo;
580 TRACE("(%04X)\n", wFormat);
582 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
583 (~cbinfo.flags & CB_OPEN))
585 WARN("Clipboard not opened by calling task.\n");
586 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
587 return 0;
590 if (USER_Driver.pEnumClipboardFormats)
591 wFmt = USER_Driver.pEnumClipboardFormats(wFormat);
593 return wFmt;
597 /**************************************************************************
598 * IsClipboardFormatAvailable (USER32.@)
600 BOOL WINAPI IsClipboardFormatAvailable(UINT wFormat)
602 BOOL bret = FALSE;
604 if (USER_Driver.pIsClipboardFormatAvailable)
605 bret = USER_Driver.pIsClipboardFormatAvailable(wFormat);
607 TRACE("%04x, returning %d\n", wFormat, bret);
608 return bret;
612 /**************************************************************************
613 * GetClipboardData (USER.142)
615 HANDLE16 WINAPI GetClipboardData16(UINT16 wFormat)
617 HANDLE16 hData = 0;
618 CLIPBOARDINFO cbinfo;
620 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
621 (~cbinfo.flags & CB_OPEN))
623 WARN("Clipboard not opened by calling task.\n");
624 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
625 return 0;
628 if (USER_Driver.pGetClipboardData)
629 USER_Driver.pGetClipboardData(wFormat, &hData, NULL);
631 return hData;
635 /**************************************************************************
636 * GetClipboardData (USER32.@)
638 HANDLE WINAPI GetClipboardData(UINT wFormat)
640 HANDLE hData = 0;
641 CLIPBOARDINFO cbinfo;
643 TRACE("%04x\n", wFormat);
645 if (!CLIPBOARD_GetClipboardInfo(&cbinfo) ||
646 (~cbinfo.flags & CB_OPEN))
648 WARN("Clipboard not opened by calling task.\n");
649 SetLastError(ERROR_CLIPBOARD_NOT_OPEN);
650 return 0;
653 if (USER_Driver.pGetClipboardData)
654 USER_Driver.pGetClipboardData(wFormat, NULL, &hData);
656 TRACE("returning %p\n", hData);
657 return hData;
661 /**************************************************************************
662 * GetPriorityClipboardFormat (USER32.@)
664 INT WINAPI GetPriorityClipboardFormat(UINT *list, INT nCount)
666 int i;
668 TRACE("()\n");
670 if(CountClipboardFormats() == 0)
671 return 0;
673 for (i = 0; i < nCount; i++)
674 if (IsClipboardFormatAvailable(list[i]))
675 return list[i];
677 return -1;
681 /**************************************************************************
682 * GetClipboardSequenceNumber (USER32.@)
683 * Supported on Win2k/Win98
684 * MSDN: Windows clipboard code keeps a serial number for the clipboard
685 * for each window station. The number is incremented whenever the
686 * contents change or are emptied.
687 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
689 DWORD WINAPI GetClipboardSequenceNumber(VOID)
691 DWORD seqno = 0;
693 SERVER_START_REQ( set_clipboard_info )
695 req->flags = 0;
696 if (!wine_server_call_err( req )) seqno = reply->seqno;
698 SERVER_END_REQ;
700 TRACE("returning %lx\n", seqno);
701 return seqno;