Documentation update.
[wine.git] / windows / clipboard.c
blobb9ec2f009d859b80655407309f0c12bfc3ce30fe
1 /*
2 * WIN32 clipboard implementation
4 * Copyright 1994 Martin Ayotte
5 * 1996 Alex Korobka
6 * 1999 Noel Borthwick
8 * NOTES:
9 * This file contains the implementation for the WIN32 Clipboard API
10 * and Wine's internal clipboard cache.
11 * The actual contents of the clipboard are held in the clipboard cache.
12 * The internal implementation talks to a "clipboard driver" to fill or
13 * expose the cache to the native device. (Currently only the X11 and
14 * TTY clipboard driver are available)
16 * TODO:
20 #include <stdlib.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <string.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "wine/winuser16.h"
32 #include "wine/winbase16.h"
33 #include "heap.h"
34 #include "message.h"
35 #include "task.h"
36 #include "queue.h"
37 #include "user.h"
38 #include "clipboard.h"
39 #include "debugtools.h"
41 DEFAULT_DEBUG_CHANNEL(clipboard);
43 #define CF_REGFORMATBASE 0xC000
45 /**************************************************************************
46 * Clipboard context global variables
49 static HANDLE hClipLock = 0;
50 static BOOL bCBHasChanged = FALSE;
52 HWND hWndClipWindow = 0; /* window that last opened clipboard */
53 HWND hWndClipOwner = 0; /* current clipboard owner */
54 HANDLE16 hTaskClipOwner = 0; /* clipboard owner's task */
55 static HWND hWndViewer = 0; /* start of viewers chain */
57 static WORD LastRegFormat = CF_REGFORMATBASE;
59 /* Clipboard cache initial data.
60 * WARNING: This data ordering is dependendent on the WINE_CLIPFORMAT structure
61 * declared in clipboard.h
63 WINE_CLIPFORMAT ClipFormats[] = {
64 { CF_TEXT, 1, 0, "Text", 0, 0, 0, 0, NULL, &ClipFormats[1]},
65 { CF_BITMAP, 1, 0, "Bitmap", 0, 0, 0, 0, &ClipFormats[0], &ClipFormats[2]},
66 { CF_METAFILEPICT, 1, 0, "MetaFile Picture", 0, 0, 0, 0, &ClipFormats[1], &ClipFormats[3]},
67 { CF_SYLK, 1, 0, "Sylk", 0, 0, 0, 0, &ClipFormats[2], &ClipFormats[4]},
68 { CF_DIF, 1, 0, "DIF", 0, 0, 0, 0, &ClipFormats[3], &ClipFormats[5]},
69 { CF_TIFF, 1, 0, "TIFF", 0, 0, 0, 0, &ClipFormats[4], &ClipFormats[6]},
70 { CF_OEMTEXT, 1, 0, "OEM Text", 0, 0, 0, 0, &ClipFormats[5], &ClipFormats[7]},
71 { CF_DIB, 1, 0, "DIB", 0, 0, 0, 0, &ClipFormats[6], &ClipFormats[8]},
72 { CF_PALETTE, 1, 0, "Palette", 0, 0, 0, 0, &ClipFormats[7], &ClipFormats[9]},
73 { CF_PENDATA, 1, 0, "PenData", 0, 0, 0, 0, &ClipFormats[8], &ClipFormats[10]},
74 { CF_RIFF, 1, 0, "RIFF", 0, 0, 0, 0, &ClipFormats[9], &ClipFormats[11]},
75 { CF_WAVE, 1, 0, "Wave", 0, 0, 0, 0, &ClipFormats[10], &ClipFormats[12]},
76 { CF_UNICODETEXT, 1, 0, "Unicode Text", 0, 0, 0, 0, &ClipFormats[11], &ClipFormats[13]},
77 { CF_OWNERDISPLAY, 1, 0, "Owner Display", 0, 0, 0, 0, &ClipFormats[12], &ClipFormats[14]},
78 { CF_DSPTEXT, 1, 0, "DSPText", 0, 0, 0, 0, &ClipFormats[13], &ClipFormats[15]},
79 { CF_DSPMETAFILEPICT, 1, 0, "DSPMetaFile Picture", 0, 0, 0, 0, &ClipFormats[14], &ClipFormats[16]},
80 { CF_DSPBITMAP, 1, 0, "DSPBitmap", 0, 0, 0, 0, &ClipFormats[15], &ClipFormats[17]},
81 { CF_HDROP, 1, 0, "HDROP", 0, 0, 0, 0, &ClipFormats[16], NULL}
85 /**************************************************************************
86 * Internal Clipboard implementation methods
87 **************************************************************************/
90 /**************************************************************************
91 * CLIPBOARD_LookupFormat
93 static LPWINE_CLIPFORMAT __lookup_format( LPWINE_CLIPFORMAT lpFormat, WORD wID )
95 while(TRUE)
97 if (lpFormat == NULL ||
98 lpFormat->wFormatID == wID) break;
99 lpFormat = lpFormat->NextFormat;
101 return lpFormat;
104 LPWINE_CLIPFORMAT CLIPBOARD_LookupFormat( WORD wID )
106 return __lookup_format( ClipFormats, wID );
109 /**************************************************************************
110 * CLIPBOARD_IsLocked
111 * Check if the clipboard cache is available to the caller
113 BOOL CLIPBOARD_IsLocked()
115 BOOL bIsLocked = TRUE;
116 HANDLE16 hTaskCur = GetCurrentTask();
119 * The clipboard is available:
120 * 1. if the caller's task has opened the clipboard,
121 * or
122 * 2. if the caller is the clipboard owners task, AND is responding to a
123 * WM_RENDERFORMAT message.
125 if ( hClipLock == hTaskCur )
126 bIsLocked = FALSE;
128 else if ( hTaskCur == hTaskClipOwner )
130 /* Check if we're currently executing inside a window procedure
131 * called in response to a WM_RENDERFORMAT message. A WM_RENDERFORMAT
132 * handler is not permitted to open the clipboard since it has been opened
133 * by another client. However the handler must have access to the
134 * clipboard in order to update data in response to this message.
136 MESSAGEQUEUE *queue = QUEUE_Lock( GetFastQueue16() );
138 if ( queue
139 && queue->smWaiting
140 && queue->smWaiting->msg == WM_RENDERFORMAT
141 && queue->smWaiting->hSrcQueue
143 bIsLocked = FALSE;
145 QUEUE_Unlock( queue );
148 return bIsLocked;
151 /**************************************************************************
152 * CLIPBOARD_ReleaseOwner
153 * Gives up ownership of the clipboard
155 void CLIPBOARD_ReleaseOwner()
157 hWndClipOwner = 0;
158 hTaskClipOwner = 0;
161 /**************************************************************************
162 * CLIPBOARD_GlobalFreeProc
164 * This is a callback mechanism to allow HGLOBAL data to be released in
165 * the context of the process which allocated it. We post a WM_TIMER message
166 * to the owner window(in CLIPBOARD_DeleteRecord) and destroy the data(in idEvent)
167 * in this WndProc, which is invoked when the apps message loop calls DispatchMessage.
168 * This technique is discussed in Matt Pietrek's "Under the Hood".
169 * An article describing the same may be found in MSDN by searching for WM_TIMER.
170 * Note that this mechanism will probably stop working when WINE supports
171 * address space separation. When "queue events" are implemented in Wine we
172 * should switch to using that mechanism, since it is more robust and does not
173 * require a procedure address to be passed. See the SetWinEventHook API for
174 * more info on this.
176 VOID CALLBACK CLIPBOARD_GlobalFreeProc( HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime )
178 /* idEvent is the HGLOBAL to be deleted */
179 GlobalFree( (HGLOBAL)idEvent );
182 /**************************************************************************
183 * CLIPBOARD_DeleteRecord
185 void CLIPBOARD_DeleteRecord(LPWINE_CLIPFORMAT lpFormat, BOOL bChange)
187 if( (lpFormat->wFormatID >= CF_GDIOBJFIRST &&
188 lpFormat->wFormatID <= CF_GDIOBJLAST) || lpFormat->wFormatID == CF_BITMAP
189 || lpFormat->wFormatID == CF_PALETTE)
191 if (lpFormat->hData32)
192 DeleteObject(lpFormat->hData32);
193 if (lpFormat->hData16)
194 DeleteObject(lpFormat->hData16);
196 else if( lpFormat->wFormatID == CF_METAFILEPICT )
198 if (lpFormat->hData32)
200 DeleteMetaFile( ((METAFILEPICT *)GlobalLock( lpFormat->hData32 ))->hMF );
201 PostMessageA(hWndClipOwner, WM_TIMER,
202 (WPARAM)lpFormat->hData32, (LPARAM)CLIPBOARD_GlobalFreeProc);
203 if (lpFormat->hDataSrc32)
205 /* Release lpFormat->hData32 in the context of the process which created it.
206 * See CLIPBOARD_GlobalFreeProc for more details about this technique.
207 * GlobalFree(lpFormat->hDataSrc32);
209 PostMessageA(hWndClipOwner, WM_TIMER,
210 (WPARAM)lpFormat->hDataSrc32, (LPARAM)CLIPBOARD_GlobalFreeProc);
213 if (lpFormat->hData16)
214 /* HMETAFILE16 and HMETAFILE32 are apparently the same thing,
215 and a shallow copy is enough to share a METAFILEPICT
216 structure between 16bit and 32bit clipboards. The MetaFile
217 should of course only be deleted once. */
218 GlobalFree16(lpFormat->hData16);
220 if (lpFormat->hData16)
222 DeleteMetaFile16( ((METAFILEPICT16 *)GlobalLock16( lpFormat->hData16 ))->hMF );
223 GlobalFree16(lpFormat->hData16);
226 else
228 if (lpFormat->hData32)
230 /* Release lpFormat->hData32 in the context of the process which created it.
231 * See CLIPBOARD_GlobalFreeProc for more details about this technique.
232 * GlobalFree( lpFormat->hData32 );
234 PostMessageA(hWndClipOwner, WM_TIMER,
235 (WPARAM)lpFormat->hData32, (LPARAM)CLIPBOARD_GlobalFreeProc);
237 if (lpFormat->hDataSrc32)
239 /* Release lpFormat->hData32 in the context of the process which created it.
240 * See CLIPBOARD_GlobalFreeProc for more details about this technique.
241 * GlobalFree(lpFormat->hDataSrc32);
243 PostMessageA(hWndClipOwner, WM_TIMER,
244 (WPARAM)lpFormat->hDataSrc32, (LPARAM)CLIPBOARD_GlobalFreeProc);
246 if (lpFormat->hData16)
247 GlobalFree16(lpFormat->hData16);
250 lpFormat->wDataPresent = 0;
251 lpFormat->hData16 = 0;
252 lpFormat->hData32 = 0;
253 lpFormat->hDataSrc32 = 0;
254 lpFormat->drvData = 0;
256 if( bChange ) bCBHasChanged = TRUE;
259 /**************************************************************************
260 * CLIPBOARD_EmptyCache
262 void CLIPBOARD_EmptyCache( BOOL bChange )
264 LPWINE_CLIPFORMAT lpFormat = ClipFormats;
266 while(lpFormat)
268 if ( lpFormat->wDataPresent || lpFormat->hData16 || lpFormat->hData32 )
269 CLIPBOARD_DeleteRecord( lpFormat, bChange );
271 lpFormat = lpFormat->NextFormat;
275 /**************************************************************************
276 * CLIPBOARD_IsPresent
278 BOOL CLIPBOARD_IsPresent(WORD wFormat)
280 /* special case */
282 if( wFormat == CF_TEXT || wFormat == CF_OEMTEXT || wFormat == CF_UNICODETEXT )
283 return ClipFormats[CF_TEXT-1].wDataPresent ||
284 ClipFormats[CF_OEMTEXT-1].wDataPresent ||
285 ClipFormats[CF_UNICODETEXT-1].wDataPresent;
286 else
288 LPWINE_CLIPFORMAT lpFormat = __lookup_format( ClipFormats, wFormat );
289 if( lpFormat ) return (lpFormat->wDataPresent);
291 return FALSE;
294 /**************************************************************************
295 * CLIPBOARD_IsCacheRendered
296 * Checks if any data needs to be rendered to the clipboard cache
297 * RETURNS:
298 * TRUE - All clipboard data is available in the cache
299 * FALSE - Some data is marked for delayed render and needs rendering
301 BOOL CLIPBOARD_IsCacheRendered()
303 LPWINE_CLIPFORMAT lpFormat = ClipFormats;
305 /* check if all formats were rendered */
306 while(lpFormat)
308 if( lpFormat->wDataPresent && !lpFormat->hData16 && !lpFormat->hData32 )
309 return FALSE;
311 lpFormat = lpFormat->NextFormat;
314 return TRUE;
318 /**************************************************************************
319 * CLIPBOARD_IsMemoryObject
320 * Tests if the clipboard format specifies a memory object
322 BOOL CLIPBOARD_IsMemoryObject( WORD wFormat )
324 switch(wFormat)
326 case CF_BITMAP:
327 case CF_METAFILEPICT:
328 case CF_DSPTEXT:
329 case CF_ENHMETAFILE:
330 case CF_HDROP:
331 case CF_PALETTE:
332 case CF_PENDATA:
333 return FALSE;
334 default:
335 return TRUE;
339 /***********************************************************************
340 * CLIPBOARD_GlobalDupMem( HGLOBAL )
341 * Helper method to duplicate an HGLOBAL chunk of memory into shared memory
343 HGLOBAL CLIPBOARD_GlobalDupMem( HGLOBAL hGlobalSrc )
345 HGLOBAL hGlobalDest;
346 PVOID pGlobalSrc, pGlobalDest;
347 DWORD cBytes;
349 if ( !hGlobalSrc )
350 return 0;
352 cBytes = GlobalSize(hGlobalSrc);
353 if ( 0 == cBytes )
354 return 0;
356 /* Turn on the DDESHARE and _MOVEABLE flags explicitly */
357 hGlobalDest = GlobalAlloc( GlobalFlags(hGlobalSrc) | GMEM_DDESHARE | GMEM_MOVEABLE,
358 cBytes );
359 if ( !hGlobalDest )
360 return 0;
362 pGlobalSrc = GlobalLock(hGlobalSrc);
363 pGlobalDest = GlobalLock(hGlobalDest);
364 if ( !pGlobalSrc || !pGlobalDest )
365 return 0;
367 memcpy(pGlobalDest, pGlobalSrc, cBytes);
369 GlobalUnlock(hGlobalSrc);
370 GlobalUnlock(hGlobalDest);
372 return hGlobalDest;
375 /**************************************************************************
376 * CLIPBOARD_GetFormatName
377 * Gets the format name associated with an ID
379 char * CLIPBOARD_GetFormatName(UINT wFormat)
381 LPWINE_CLIPFORMAT lpFormat = __lookup_format( ClipFormats, wFormat );
382 return (lpFormat) ? lpFormat->Name : NULL;
386 /**************************************************************************
387 * CLIPBOARD_RenderFormat
389 static BOOL CLIPBOARD_RenderFormat(LPWINE_CLIPFORMAT lpFormat)
392 * If WINE is not the selection owner, and the format is available
393 * we must ask the the driver to render the data to the clipboard cache.
395 if ( !USER_Driver.pIsSelectionOwner()
396 && USER_Driver.pIsClipboardFormatAvailable( lpFormat->wFormatID ) )
398 if ( !USER_Driver.pGetClipboardData( lpFormat->wFormatID ) )
399 return FALSE;
402 * If Wine owns the clipboard, and the data is marked for delayed render,
403 * render it now.
405 else if( lpFormat->wDataPresent && !lpFormat->hData16 && !lpFormat->hData32 )
407 if( IsWindow(hWndClipOwner) )
409 /* Send a WM_RENDERFORMAT message to notify the owner to render the
410 * data requested into the clipboard.
412 TRACE("Sending WM_RENDERFORMAT message\n");
413 SendMessage16(hWndClipOwner,WM_RENDERFORMAT,
414 (WPARAM16)lpFormat->wFormatID,0L);
416 else
418 WARN("\thWndClipOwner (%04x) is lost!\n",
419 hWndClipOwner);
420 CLIPBOARD_ReleaseOwner();
421 lpFormat->wDataPresent = 0;
422 return FALSE;
426 return (lpFormat->hData16 || lpFormat->hData32) ? TRUE : FALSE;
429 /**************************************************************************
430 * CLIPBOARD_ConvertText
431 * Returns number of required/converted characters - not bytes!
433 static INT CLIPBOARD_ConvertText(WORD src_fmt, void const *src, INT src_size,
434 WORD dst_fmt, void *dst, INT dst_size)
436 UINT cp;
438 if(src_fmt == CF_UNICODETEXT)
440 switch(dst_fmt)
442 case CF_TEXT:
443 cp = CP_ACP;
444 break;
445 case CF_OEMTEXT:
446 cp = CP_OEMCP;
447 break;
448 default:
449 return 0;
451 return WideCharToMultiByte(cp, 0, src, src_size, dst, dst_size, NULL, NULL);
454 if(dst_fmt == CF_UNICODETEXT)
456 switch(src_fmt)
458 case CF_TEXT:
459 cp = CP_ACP;
460 break;
461 case CF_OEMTEXT:
462 cp = CP_OEMCP;
463 break;
464 default:
465 return 0;
467 return MultiByteToWideChar(cp, 0, src, src_size, dst, dst_size);
470 if(!dst_size) return src_size;
472 if(dst_size > src_size) dst_size = src_size;
474 if(src_fmt == CF_TEXT )
475 CharToOemBuffA(src, dst, dst_size);
476 else
477 OemToCharBuffA(src, dst, dst_size);
479 return dst_size;
482 /**************************************************************************
483 * CLIPBOARD_RenderText
485 * Renders text to the clipboard buffer converting between UNIX and DOS formats.
487 * RETURNS: pointer to the WINE_CLIPFORMAT if successful, NULL otherwise
489 * FIXME: Should be a pair of driver functions that convert between OEM text and Windows.
492 static LPWINE_CLIPFORMAT CLIPBOARD_RenderText( UINT wFormat )
494 LPWINE_CLIPFORMAT lpSource = ClipFormats;
495 LPWINE_CLIPFORMAT lpTarget;
497 /* Asked for CF_TEXT but not available - always attempt to convert
498 from CF_UNICODETEXT or CF_OEMTEXT */
499 if( wFormat == CF_TEXT && !ClipFormats[CF_TEXT-1].wDataPresent )
501 if(ClipFormats[CF_UNICODETEXT-1].wDataPresent)
503 /* Convert UNICODETEXT -> TEXT */
504 lpSource = &ClipFormats[CF_UNICODETEXT-1];
505 lpTarget = &ClipFormats[CF_TEXT-1];
507 TRACE("\tUNICODETEXT -> TEXT\n");
509 else if(ClipFormats[CF_OEMTEXT-1].wDataPresent)
511 /* Convert OEMTEXT -> TEXT */
512 lpSource = &ClipFormats[CF_OEMTEXT-1];
513 lpTarget = &ClipFormats[CF_TEXT-1];
515 TRACE("\tOEMTEXT -> TEXT\n");
517 else
518 lpSource = NULL; /* Conversion format is not available */
520 /* Asked for CF_OEMTEXT but not available - always attempt to convert
521 from CF_UNICODETEXT or CF_TEXT */
522 else if( wFormat == CF_OEMTEXT && !ClipFormats[CF_OEMTEXT-1].wDataPresent )
524 if(ClipFormats[CF_UNICODETEXT-1].wDataPresent)
526 /* Convert UNICODETEXT -> OEMTEXT */
527 lpSource = &ClipFormats[CF_UNICODETEXT-1];
528 lpTarget = &ClipFormats[CF_OEMTEXT-1];
530 TRACE("\tUNICODETEXT -> OEMTEXT\n");
532 else if(ClipFormats[CF_TEXT-1].wDataPresent)
534 /* Convert TEXT -> OEMTEXT */
535 lpSource = &ClipFormats[CF_TEXT-1];
536 lpTarget = &ClipFormats[CF_OEMTEXT-1];
538 TRACE("\tTEXT -> OEMTEXT\n");
540 else
541 lpSource = NULL; /* Conversion format is not available */
543 /* Asked for CF_UNICODETEXT but not available - always attempt to convert
544 from CF_TEXT or CF_OEMTEXT */
545 else if( wFormat == CF_UNICODETEXT && !ClipFormats[CF_UNICODETEXT-1].wDataPresent )
547 if(ClipFormats[CF_TEXT-1].wDataPresent)
549 /* Convert TEXT -> UNICODETEXT */
550 lpSource = &ClipFormats[CF_TEXT-1];
551 lpTarget = &ClipFormats[CF_UNICODETEXT-1];
553 TRACE("\tTEXT -> UNICODETEXT\n");
555 else if(ClipFormats[CF_OEMTEXT-1].wDataPresent)
557 /* Convert OEMTEXT -> UNICODETEXT */
558 lpSource = &ClipFormats[CF_OEMTEXT-1];
559 lpTarget = &ClipFormats[CF_UNICODETEXT-1];
561 TRACE("\tOEMTEXT -> UNICODETEXT\n");
563 else
564 lpSource = NULL; /* Conversion format is not available */
566 /* Text format requested is available - no conversion necessary */
567 else
569 lpSource = __lookup_format( ClipFormats, wFormat );
570 lpTarget = lpSource;
573 /* First render the source text format */
574 if ( !lpSource || !CLIPBOARD_RenderFormat(lpSource) ) return NULL;
576 /* Convert to the desired target text format, if necessary */
577 if( lpTarget != lpSource && !lpTarget->hData16 && !lpTarget->hData32 )
579 INT src_chars, dst_chars, alloc_size;
580 LPCSTR lpstrS;
581 LPSTR lpstrT;
583 if (lpSource->hData32)
585 lpstrS = (LPSTR)GlobalLock(lpSource->hData32);
587 else
589 lpstrS = (LPSTR)GlobalLock16(lpSource->hData16);
592 if( !lpstrS ) return NULL;
594 /* Text always NULL terminated */
595 if(lpSource->wFormatID == CF_UNICODETEXT)
596 src_chars = strlenW((LPCWSTR)lpstrS);
597 else
598 src_chars = strlen(lpstrS);
600 /* Calculate number of characters in the destination buffer */
601 dst_chars = CLIPBOARD_ConvertText(lpSource->wFormatID, lpstrS, src_chars,
602 lpTarget->wFormatID, NULL, 0);
603 if(!dst_chars) return NULL;
605 TRACE("\tconverting from '%s' to '%s', %i chars\n",
606 lpSource->Name, lpTarget->Name, src_chars);
608 /* Convert characters to bytes */
609 if(lpTarget->wFormatID == CF_UNICODETEXT)
610 alloc_size = dst_chars * sizeof(WCHAR);
611 else
612 alloc_size = dst_chars;
614 lpTarget->hData32 = GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE | GMEM_DDESHARE, alloc_size);
615 lpstrT = (LPSTR)GlobalLock(lpTarget->hData32);
617 if( lpstrT )
619 CLIPBOARD_ConvertText(lpSource->wFormatID, lpstrS, src_chars,
620 lpTarget->wFormatID, lpstrT, dst_chars);
621 GlobalUnlock(lpTarget->hData32);
623 else
624 lpTarget->hData32 = 0;
626 /* Unlock source */
627 if (lpSource->hData32)
628 GlobalUnlock(lpSource->hData32);
629 else
630 GlobalUnlock16(lpSource->hData16);
633 return (lpTarget->hData16 || lpTarget->hData32) ? lpTarget : NULL;
636 /**************************************************************************
637 * WIN32 Clipboard implementation
638 **************************************************************************/
640 /**************************************************************************
641 * OpenClipboard16 (USER.137)
643 BOOL16 WINAPI OpenClipboard16( HWND16 hWnd )
645 return OpenClipboard( hWnd );
649 /**************************************************************************
650 * OpenClipboard (USER32.407)
652 * Note: Netscape uses NULL hWnd to open the clipboard.
654 BOOL WINAPI OpenClipboard( HWND hWnd )
656 BOOL bRet;
658 TRACE("(%04x)...\n", hWnd);
660 if (!hClipLock)
662 hClipLock = GetCurrentTask();
664 /* Save current user of the clipboard */
665 hWndClipWindow = hWnd;
666 bCBHasChanged = FALSE;
667 bRet = TRUE;
669 else bRet = FALSE;
671 TRACE(" returning %i\n", bRet);
672 return bRet;
676 /**************************************************************************
677 * CloseClipboard16 (USER.138)
679 BOOL16 WINAPI CloseClipboard16(void)
681 return CloseClipboard();
685 /**************************************************************************
686 * CloseClipboard (USER32.54)
688 BOOL WINAPI CloseClipboard(void)
690 TRACE("()\n");
692 if (hClipLock == GetCurrentTask())
694 hWndClipWindow = 0;
696 if (bCBHasChanged && hWndViewer)
697 SendMessage16(hWndViewer, WM_DRAWCLIPBOARD, 0, 0L);
698 hClipLock = 0;
700 return TRUE;
704 /**************************************************************************
705 * EmptyClipboard16 (USER.139)
707 BOOL16 WINAPI EmptyClipboard16(void)
709 return EmptyClipboard();
713 /**************************************************************************
714 * EmptyClipboard (USER32.169)
715 * Empties and acquires ownership of the clipboard
717 BOOL WINAPI EmptyClipboard(void)
719 TRACE("()\n");
721 if (hClipLock != GetCurrentTask())
723 WARN("Clipboard not opened by calling task!");
724 return FALSE;
727 /* destroy private objects */
729 if (hWndClipOwner)
730 SendMessage16(hWndClipOwner, WM_DESTROYCLIPBOARD, 0, 0L);
732 /* empty the cache */
733 CLIPBOARD_EmptyCache(TRUE);
735 /* Assign ownership of the clipboard to the current client */
736 hWndClipOwner = hWndClipWindow;
738 /* Save the current task */
739 hTaskClipOwner = GetCurrentTask();
741 /* Tell the driver to acquire the selection */
742 USER_Driver.pAcquireClipboard();
744 return TRUE;
748 /**************************************************************************
749 * GetClipboardOwner16 (USER.140)
750 * FIXME: Can't return the owner if the clipbard is owned by an external app
752 HWND16 WINAPI GetClipboardOwner16(void)
754 TRACE("()\n");
755 return hWndClipOwner;
759 /**************************************************************************
760 * GetClipboardOwner (USER32.225)
761 * FIXME: Can't return the owner if the clipbard is owned by an external app
763 HWND WINAPI GetClipboardOwner(void)
765 TRACE("()\n");
766 return hWndClipOwner;
770 /**************************************************************************
771 * SetClipboardData16 (USER.141)
773 HANDLE16 WINAPI SetClipboardData16( UINT16 wFormat, HANDLE16 hData )
775 LPWINE_CLIPFORMAT lpFormat = __lookup_format( ClipFormats, wFormat );
777 TRACE("(%04X, %04x) !\n", wFormat, hData);
779 /* NOTE: If the hData is zero and current owner doesn't match
780 * the window that opened the clipboard then this application
781 * is screwed because WM_RENDERFORMAT will go to the owner.
782 * (to become the owner it must call EmptyClipboard() before
783 * adding new data).
786 if( CLIPBOARD_IsLocked() || !lpFormat ||
787 (!hData && (!hWndClipOwner || (hWndClipOwner != hWndClipWindow))) )
789 WARN("Invalid hData or clipboard not opened by calling task!\n");
790 return 0;
793 /* Pass on the request to the driver */
794 USER_Driver.pSetClipboardData(wFormat);
796 if ( lpFormat->wDataPresent || lpFormat->hData16 || lpFormat->hData32 )
798 CLIPBOARD_DeleteRecord(lpFormat, TRUE);
800 /* delete existing CF_UNICODETEXT/CF_TEXT/CF_OEMTEXT aliases */
801 if(wFormat == CF_UNICODETEXT)
803 CLIPBOARD_DeleteRecord(&ClipFormats[CF_TEXT-1], TRUE);
804 CLIPBOARD_DeleteRecord(&ClipFormats[CF_OEMTEXT-1], TRUE);
806 else if(wFormat == CF_TEXT)
808 CLIPBOARD_DeleteRecord(&ClipFormats[CF_UNICODETEXT-1], TRUE);
809 CLIPBOARD_DeleteRecord(&ClipFormats[CF_OEMTEXT-1], TRUE);
811 else if(wFormat == CF_OEMTEXT)
813 CLIPBOARD_DeleteRecord(&ClipFormats[CF_UNICODETEXT-1], TRUE);
814 CLIPBOARD_DeleteRecord(&ClipFormats[CF_TEXT-1], TRUE);
818 bCBHasChanged = TRUE;
819 lpFormat->wDataPresent = 1;
820 lpFormat->hData16 = hData; /* 0 is legal, see WM_RENDERFORMAT */
821 lpFormat->hData32 = 0;
823 return lpFormat->hData16;
827 /**************************************************************************
828 * SetClipboardData (USER32.470)
830 HANDLE WINAPI SetClipboardData( UINT wFormat, HANDLE hData )
832 LPWINE_CLIPFORMAT lpFormat = __lookup_format( ClipFormats, wFormat );
834 TRACE("(%08X, %08x) !\n", wFormat, hData);
836 /* NOTE: If the hData is zero and current owner doesn't match
837 * the window that opened the clipboard then this application
838 * is screwed because WM_RENDERFORMAT will go to the owner.
839 * (to become the owner it must call EmptyClipboard() before
840 * adding new data).
843 if( CLIPBOARD_IsLocked() || !lpFormat ||
844 (!hData && (!hWndClipOwner || (hWndClipOwner != hWndClipWindow))) )
846 WARN("Invalid hData or clipboard not opened by calling task!\n");
847 return 0;
850 /* Tell the driver to acquire the selection */
851 USER_Driver.pAcquireClipboard();
853 if ( lpFormat->wDataPresent &&
854 (lpFormat->hData16 || lpFormat->hData32) )
856 CLIPBOARD_DeleteRecord(lpFormat, TRUE);
858 /* delete existing CF_UNICODETEXT/CF_TEXT/CF_OEMTEXT aliases */
859 if(wFormat == CF_UNICODETEXT)
861 CLIPBOARD_DeleteRecord(&ClipFormats[CF_TEXT-1], TRUE);
862 CLIPBOARD_DeleteRecord(&ClipFormats[CF_OEMTEXT-1], TRUE);
864 else if(wFormat == CF_TEXT)
866 CLIPBOARD_DeleteRecord(&ClipFormats[CF_UNICODETEXT-1], TRUE);
867 CLIPBOARD_DeleteRecord(&ClipFormats[CF_OEMTEXT-1], TRUE);
869 else if(wFormat == CF_OEMTEXT)
871 CLIPBOARD_DeleteRecord(&ClipFormats[CF_UNICODETEXT-1], TRUE);
872 CLIPBOARD_DeleteRecord(&ClipFormats[CF_TEXT-1], TRUE);
876 bCBHasChanged = TRUE;
877 lpFormat->wDataPresent = 1;
878 lpFormat->hDataSrc32 = hData; /* Save the source handle */
881 * Make a shared duplicate if the memory is not shared
882 * TODO: What should be done for non-memory objects
884 if ( CLIPBOARD_IsMemoryObject(wFormat) && hData && !(GlobalFlags(hData) & GMEM_DDESHARE) )
885 lpFormat->hData32 = CLIPBOARD_GlobalDupMem( hData );
886 else
887 lpFormat->hData32 = hData; /* 0 is legal, see WM_RENDERFORMAT */
889 lpFormat->hData16 = 0;
891 return lpFormat->hData32; /* Should we return lpFormat->hDataSrc32 */
895 /**************************************************************************
896 * GetClipboardData16 (USER.142)
898 HANDLE16 WINAPI GetClipboardData16( UINT16 wFormat )
900 LPWINE_CLIPFORMAT lpRender = ClipFormats;
902 TRACE("(%04X)\n", wFormat);
904 if (CLIPBOARD_IsLocked())
906 WARN("Clipboard not opened by calling task!\n");
907 return 0;
910 if( wFormat == CF_UNICODETEXT || wFormat == CF_TEXT || wFormat == CF_OEMTEXT )
912 lpRender = CLIPBOARD_RenderText(wFormat);
913 if ( !lpRender ) return 0;
915 else
917 lpRender = __lookup_format( ClipFormats, wFormat );
918 if( !lpRender || !CLIPBOARD_RenderFormat(lpRender) ) return 0;
921 /* Convert between 32 -> 16 bit data, if necessary */
922 if( lpRender->hData32 && !lpRender->hData16
923 && CLIPBOARD_IsMemoryObject(wFormat) )
925 int size;
926 if( lpRender->wFormatID == CF_METAFILEPICT )
927 size = sizeof( METAFILEPICT16 );
928 else
929 size = GlobalSize(lpRender->hData32);
931 lpRender->hData16 = GlobalAlloc16(GMEM_ZEROINIT, size);
932 if( !lpRender->hData16 )
933 ERR("(%04X) -- not enough memory in 16b heap\n", wFormat);
934 else
936 if( lpRender->wFormatID == CF_METAFILEPICT )
938 FIXME("\timplement function CopyMetaFilePict32to16\n");
939 FIXME("\tin the appropriate file.\n");
940 #ifdef SOMEONE_IMPLEMENTED_ME
941 CopyMetaFilePict32to16( GlobalLock16(lpRender->hData16),
942 GlobalLock(lpRender->hData32) );
943 #endif
945 else
947 memcpy( GlobalLock16(lpRender->hData16),
948 GlobalLock(lpRender->hData32),
949 size );
951 GlobalUnlock16(lpRender->hData16);
952 GlobalUnlock(lpRender->hData32);
956 TRACE("\treturning %04x (type %i)\n",
957 lpRender->hData16, lpRender->wFormatID);
958 return lpRender->hData16;
962 /**************************************************************************
963 * GetClipboardData (USER32.222)
965 HANDLE WINAPI GetClipboardData( UINT wFormat )
967 LPWINE_CLIPFORMAT lpRender = ClipFormats;
969 TRACE("(%08X)\n", wFormat);
971 if (CLIPBOARD_IsLocked())
973 WARN("Clipboard not opened by calling task!");
974 return 0;
977 if( wFormat == CF_UNICODETEXT || wFormat == CF_TEXT || wFormat == CF_OEMTEXT )
979 lpRender = CLIPBOARD_RenderText(wFormat);
980 if ( !lpRender ) return 0;
982 else
984 lpRender = __lookup_format( ClipFormats, wFormat );
985 if( !lpRender || !CLIPBOARD_RenderFormat(lpRender) ) return 0;
988 /* Convert between 16 -> 32 bit data, if necessary */
989 if( lpRender->hData16 && !lpRender->hData32
990 && CLIPBOARD_IsMemoryObject(wFormat) )
992 int size;
993 if( lpRender->wFormatID == CF_METAFILEPICT )
994 size = sizeof( METAFILEPICT );
995 else
996 size = GlobalSize16(lpRender->hData16);
997 lpRender->hData32 = GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE | GMEM_DDESHARE,
998 size);
999 if( lpRender->wFormatID == CF_METAFILEPICT )
1001 FIXME("\timplement function CopyMetaFilePict16to32\n");
1002 FIXME("\tin the appropriate file.\n");
1003 #ifdef SOMEONE_IMPLEMENTED_ME
1004 CopyMetaFilePict16to32( GlobalLock16(lpRender->hData32),
1005 GlobalLock(lpRender->hData16) );
1006 #endif
1008 else
1010 memcpy( GlobalLock(lpRender->hData32),
1011 GlobalLock16(lpRender->hData16),
1012 size );
1014 GlobalUnlock(lpRender->hData32);
1015 GlobalUnlock16(lpRender->hData16);
1018 TRACE("\treturning %04x (type %i)\n",
1019 lpRender->hData32, lpRender->wFormatID);
1020 return lpRender->hData32;
1024 /**************************************************************************
1025 * CountClipboardFormats16 (USER.143)
1027 INT16 WINAPI CountClipboardFormats16(void)
1029 return CountClipboardFormats();
1033 /**************************************************************************
1034 * CountClipboardFormats (USER32.63)
1036 INT WINAPI CountClipboardFormats(void)
1038 INT FormatCount = 0;
1039 LPWINE_CLIPFORMAT lpFormat = ClipFormats;
1041 TRACE("()\n");
1043 while(TRUE)
1045 if (lpFormat == NULL) break;
1047 if( lpFormat->wFormatID != CF_TEXT ) /* Don't count CF_TEXT */
1050 * The format is available if either:
1051 * 1. The data is already in the cache.
1052 * 2. The selection is not owned by us(WINE) and the data is
1053 * available to the clipboard driver.
1055 if ( lpFormat->wDataPresent ||
1056 ( !USER_Driver.pIsSelectionOwner()
1057 && USER_Driver.pIsClipboardFormatAvailable( lpFormat->wFormatID ) ) )
1059 TRACE("\tdata found for format 0x%04x(%s)\n",
1060 lpFormat->wFormatID, CLIPBOARD_GetFormatName(lpFormat->wFormatID));
1061 FormatCount++;
1065 lpFormat = lpFormat->NextFormat;
1068 /* these are equivalent, adjust the total */
1069 FormatCount += (ClipFormats[CF_UNICODETEXT-1].wDataPresent ||
1070 ClipFormats[CF_TEXT-1].wDataPresent ||
1071 ClipFormats[CF_OEMTEXT-1].wDataPresent) ? 1 : 0;
1073 TRACE("\ttotal %d\n", FormatCount);
1074 return FormatCount;
1078 /**************************************************************************
1079 * EnumClipboardFormats16 (USER.144)
1081 UINT16 WINAPI EnumClipboardFormats16( UINT16 wFormat )
1083 return EnumClipboardFormats( wFormat );
1087 /**************************************************************************
1088 * EnumClipboardFormats (USER32.179)
1090 UINT WINAPI EnumClipboardFormats( UINT wFormat )
1092 LPWINE_CLIPFORMAT lpFormat = ClipFormats;
1093 BOOL bFormatPresent;
1095 TRACE("(%04X)\n", wFormat);
1097 if (CLIPBOARD_IsLocked())
1099 WARN("Clipboard not opened by calling task!");
1100 return 0;
1103 if (wFormat == 0) /* start from the beginning */
1104 lpFormat = ClipFormats;
1105 else
1107 /* walk up to the specified format record */
1109 if( !(lpFormat = __lookup_format( lpFormat, wFormat )) )
1110 return 0;
1111 lpFormat = lpFormat->NextFormat; /* right */
1114 while(TRUE)
1116 if (lpFormat == NULL) return 0;
1118 if(CLIPBOARD_IsPresent(lpFormat->wFormatID))
1119 break;
1121 /* Query the driver if not yet in the cache */
1122 if (!USER_Driver.pIsSelectionOwner())
1124 if(lpFormat->wFormatID == CF_UNICODETEXT ||
1125 lpFormat->wFormatID == CF_TEXT ||
1126 lpFormat->wFormatID == CF_OEMTEXT)
1128 if(USER_Driver.pIsClipboardFormatAvailable(CF_UNICODETEXT) ||
1129 USER_Driver.pIsClipboardFormatAvailable(CF_TEXT) ||
1130 USER_Driver.pIsClipboardFormatAvailable(CF_OEMTEXT))
1131 bFormatPresent = TRUE;
1132 else
1133 bFormatPresent = FALSE;
1135 else
1136 bFormatPresent = USER_Driver.pIsClipboardFormatAvailable(lpFormat->wFormatID);
1138 if(bFormatPresent)
1139 break;
1142 lpFormat = lpFormat->NextFormat;
1145 TRACE("Next available format %d\n", lpFormat->wFormatID);
1147 return lpFormat->wFormatID;
1151 /**************************************************************************
1152 * RegisterClipboardFormat16 (USER.145)
1154 UINT16 WINAPI RegisterClipboardFormat16( LPCSTR FormatName )
1156 LPWINE_CLIPFORMAT lpNewFormat;
1157 LPWINE_CLIPFORMAT lpFormat = ClipFormats;
1159 if (FormatName == NULL) return 0;
1161 TRACE("('%s') !\n", FormatName);
1163 /* walk format chain to see if it's already registered */
1165 while(TRUE)
1167 if ( !strcmp(lpFormat->Name,FormatName) )
1169 lpFormat->wRefCount++;
1170 return lpFormat->wFormatID;
1173 if ( lpFormat->NextFormat == NULL ) break;
1175 lpFormat = lpFormat->NextFormat;
1178 /* allocate storage for new format entry */
1180 lpNewFormat = (LPWINE_CLIPFORMAT)HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_CLIPFORMAT));
1181 if(lpNewFormat == NULL) {
1182 WARN("No more memory for a new format!");
1183 return 0;
1185 lpFormat->NextFormat = lpNewFormat;
1186 lpNewFormat->wFormatID = LastRegFormat;
1187 lpNewFormat->wRefCount = 1;
1189 lpNewFormat->Name = (LPSTR)HEAP_strdupA(GetProcessHeap(), 0, FormatName);
1190 if(lpNewFormat->Name == NULL) {
1191 WARN("No more memory for the new format name!");
1192 HeapFree(GetProcessHeap(), 0, lpNewFormat);
1193 return 0;
1196 lpNewFormat->wDataPresent = 0;
1197 lpNewFormat->hData16 = 0;
1198 lpNewFormat->hDataSrc32 = 0;
1199 lpNewFormat->hData32 = 0;
1200 lpNewFormat->drvData = 0;
1201 lpNewFormat->PrevFormat = lpFormat;
1202 lpNewFormat->NextFormat = NULL;
1204 /* Pass on the registration request to the driver */
1205 USER_Driver.pRegisterClipboardFormat( FormatName );
1207 return LastRegFormat++;
1211 /**************************************************************************
1212 * RegisterClipboardFormatA (USER32.431)
1214 UINT WINAPI RegisterClipboardFormatA( LPCSTR formatName )
1216 return RegisterClipboardFormat16( formatName );
1220 /**************************************************************************
1221 * RegisterClipboardFormatW (USER32.432)
1223 UINT WINAPI RegisterClipboardFormatW( LPCWSTR formatName )
1225 LPSTR aFormat = HEAP_strdupWtoA( GetProcessHeap(), 0, formatName );
1226 UINT ret = RegisterClipboardFormatA( aFormat );
1227 HeapFree( GetProcessHeap(), 0, aFormat );
1228 return ret;
1232 /**************************************************************************
1233 * GetClipboardFormatName16 (USER.146)
1235 INT16 WINAPI GetClipboardFormatName16( UINT16 wFormat, LPSTR retStr, INT16 maxlen )
1237 return GetClipboardFormatNameA( wFormat, retStr, maxlen );
1241 /**************************************************************************
1242 * GetClipboardFormatNameA (USER32.223)
1244 INT WINAPI GetClipboardFormatNameA( UINT wFormat, LPSTR retStr, INT maxlen )
1246 LPWINE_CLIPFORMAT lpFormat = __lookup_format( ClipFormats, wFormat );
1248 TRACE("(%04X, %p, %d) !\n", wFormat, retStr, maxlen);
1250 if (lpFormat == NULL || lpFormat->Name == NULL ||
1251 lpFormat->wFormatID < CF_REGFORMATBASE) return 0;
1253 TRACE("Name='%s' !\n", lpFormat->Name);
1255 lstrcpynA( retStr, lpFormat->Name, maxlen );
1256 return strlen(retStr);
1260 /**************************************************************************
1261 * GetClipboardFormatNameW (USER32.224)
1263 INT WINAPI GetClipboardFormatNameW( UINT wFormat, LPWSTR retStr, INT maxlen )
1265 INT ret;
1266 LPSTR p = HeapAlloc( GetProcessHeap(), 0, maxlen );
1267 if(p == NULL) return 0; /* FIXME: is this the correct failure value? */
1269 ret = GetClipboardFormatNameA( wFormat, p, maxlen );
1271 if (maxlen > 0 && !MultiByteToWideChar( CP_ACP, 0, p, -1, retStr, maxlen ))
1272 retStr[maxlen-1] = 0;
1273 HeapFree( GetProcessHeap(), 0, p );
1274 return ret;
1278 /**************************************************************************
1279 * SetClipboardViewer16 (USER.147)
1281 HWND16 WINAPI SetClipboardViewer16( HWND16 hWnd )
1283 TRACE("(%04x)\n", hWnd);
1284 return SetClipboardViewer( hWnd );
1288 /**************************************************************************
1289 * SetClipboardViewer (USER32.471)
1291 HWND WINAPI SetClipboardViewer( HWND hWnd )
1293 HWND hwndPrev = hWndViewer;
1295 TRACE("(%04x): returning %04x\n", hWnd, hwndPrev);
1297 hWndViewer = hWnd;
1298 return hwndPrev;
1302 /**************************************************************************
1303 * GetClipboardViewer16 (USER.148)
1305 HWND16 WINAPI GetClipboardViewer16(void)
1307 TRACE("()\n");
1308 return hWndViewer;
1312 /**************************************************************************
1313 * GetClipboardViewer (USER32.226)
1315 HWND WINAPI GetClipboardViewer(void)
1317 TRACE("()\n");
1318 return hWndViewer;
1322 /**************************************************************************
1323 * ChangeClipboardChain16 (USER.149)
1325 BOOL16 WINAPI ChangeClipboardChain16(HWND16 hWnd, HWND16 hWndNext)
1327 return ChangeClipboardChain(hWnd, hWndNext);
1331 /**************************************************************************
1332 * ChangeClipboardChain (USER32.22)
1334 BOOL WINAPI ChangeClipboardChain(HWND hWnd, HWND hWndNext)
1336 BOOL bRet = 0;
1338 FIXME("(0x%04x, 0x%04x): stub?\n", hWnd, hWndNext);
1340 if( hWndViewer )
1341 bRet = !SendMessage16( hWndViewer, WM_CHANGECBCHAIN,
1342 (WPARAM16)hWnd, (LPARAM)hWndNext);
1343 else
1344 WARN("hWndViewer is lost\n");
1346 if( hWnd == hWndViewer ) hWndViewer = hWndNext;
1348 return bRet;
1352 /**************************************************************************
1353 * IsClipboardFormatAvailable16 (USER.193)
1355 BOOL16 WINAPI IsClipboardFormatAvailable16( UINT16 wFormat )
1357 return IsClipboardFormatAvailable( wFormat );
1361 /**************************************************************************
1362 * IsClipboardFormatAvailable (USER32.340)
1364 BOOL WINAPI IsClipboardFormatAvailable( UINT wFormat )
1366 BOOL bRet;
1368 if (wFormat == 0) /* Reject this case quickly */
1369 bRet = FALSE;
1370 else
1371 bRet = EnumClipboardFormats(wFormat - 1) == wFormat;
1373 TRACE("(%04X)- ret(%d)\n", wFormat, bRet);
1374 return bRet;
1378 /**************************************************************************
1379 * GetOpenClipboardWindow16 (USER.248)
1380 * FIXME: This wont work if an external app owns the selection
1382 HWND16 WINAPI GetOpenClipboardWindow16(void)
1384 TRACE("()\n");
1385 return hWndClipWindow;
1389 /**************************************************************************
1390 * GetOpenClipboardWindow (USER32.277)
1391 * FIXME: This wont work if an external app owns the selection
1393 HWND WINAPI GetOpenClipboardWindow(void)
1395 TRACE("()\n");
1396 return hWndClipWindow;
1400 /**************************************************************************
1401 * GetPriorityClipboardFormat16 (USER.402)
1403 INT16 WINAPI GetPriorityClipboardFormat16( UINT16 *lpPriorityList, INT16 nCount)
1405 FIXME("(%p,%d): stub\n", lpPriorityList, nCount );
1406 return 0;
1410 /**************************************************************************
1411 * GetPriorityClipboardFormat (USER32.279)
1413 INT WINAPI GetPriorityClipboardFormat( UINT *lpPriorityList, INT nCount )
1415 int Counter;
1416 TRACE("()\n");
1418 if(CountClipboardFormats() == 0)
1420 return 0;
1423 for(Counter = 0; Counter <= nCount; Counter++)
1425 if(IsClipboardFormatAvailable(*(lpPriorityList+sizeof(INT)*Counter)))
1426 return *(lpPriorityList+sizeof(INT)*Counter);
1429 return -1;
1433 /**************************************************************************
1434 * GetClipboardSequenceNumber (USER32)
1435 * Supported on Win2k/Win98
1436 * MSDN: Windows clipboard code keeps a serial number for the clipboard
1437 * for each window station. The number is incremented whenever the
1438 * contents change or are emptied.
1439 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
1441 DWORD WINAPI GetClipboardSequenceNumber(VOID)
1443 FIXME("Returning 0, see windows/clipboard.c\n");
1444 /* FIXME: Use serial numbers */
1445 return 0;