2 * WIN32 clipboard implementation
4 * Copyright 1994 Martin Ayotte
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)
21 #include <sys/types.h>
31 #include "wine/winuser16.h"
32 #include "wine/winbase16.h"
37 #include "clipboard.h"
38 #include "debugtools.h"
40 DEFAULT_DEBUG_CHANNEL(clipboard
);
42 #define CF_REGFORMATBASE 0xC000
44 /**************************************************************************
45 * Clipboard context global variables
48 static HANDLE hClipLock
= 0;
49 static BOOL bCBHasChanged
= FALSE
;
51 HWND hWndClipWindow
= 0; /* window that last opened clipboard */
52 HWND hWndClipOwner
= 0; /* current clipboard owner */
53 HANDLE16 hTaskClipOwner
= 0; /* clipboard owner's task */
54 static HWND hWndViewer
= 0; /* start of viewers chain */
56 static WORD LastRegFormat
= CF_REGFORMATBASE
;
58 /* Clipboard cache initial data.
59 * WARNING: This data ordering is dependent on the WINE_CLIPFORMAT structure
60 * declared in clipboard.h
62 WINE_CLIPFORMAT ClipFormats
[] = {
63 { CF_TEXT
, 1, 0, "Text", 0, 0, 0, 0, NULL
, &ClipFormats
[1]},
64 { CF_BITMAP
, 1, 0, "Bitmap", 0, 0, 0, 0, &ClipFormats
[0], &ClipFormats
[2]},
65 { CF_METAFILEPICT
, 1, 0, "MetaFile Picture", 0, 0, 0, 0, &ClipFormats
[1], &ClipFormats
[3]},
66 { CF_SYLK
, 1, 0, "Sylk", 0, 0, 0, 0, &ClipFormats
[2], &ClipFormats
[4]},
67 { CF_DIF
, 1, 0, "DIF", 0, 0, 0, 0, &ClipFormats
[3], &ClipFormats
[5]},
68 { CF_TIFF
, 1, 0, "TIFF", 0, 0, 0, 0, &ClipFormats
[4], &ClipFormats
[6]},
69 { CF_OEMTEXT
, 1, 0, "OEM Text", 0, 0, 0, 0, &ClipFormats
[5], &ClipFormats
[7]},
70 { CF_DIB
, 1, 0, "DIB", 0, 0, 0, 0, &ClipFormats
[6], &ClipFormats
[8]},
71 { CF_PALETTE
, 1, 0, "Palette", 0, 0, 0, 0, &ClipFormats
[7], &ClipFormats
[9]},
72 { CF_PENDATA
, 1, 0, "PenData", 0, 0, 0, 0, &ClipFormats
[8], &ClipFormats
[10]},
73 { CF_RIFF
, 1, 0, "RIFF", 0, 0, 0, 0, &ClipFormats
[9], &ClipFormats
[11]},
74 { CF_WAVE
, 1, 0, "Wave", 0, 0, 0, 0, &ClipFormats
[10], &ClipFormats
[12]},
75 { CF_UNICODETEXT
, 1, 0, "Unicode Text", 0, 0, 0, 0, &ClipFormats
[11], &ClipFormats
[13]},
76 { CF_OWNERDISPLAY
, 1, 0, "Owner Display", 0, 0, 0, 0, &ClipFormats
[12], &ClipFormats
[14]},
77 { CF_DSPTEXT
, 1, 0, "DSPText", 0, 0, 0, 0, &ClipFormats
[13], &ClipFormats
[15]},
78 { CF_DSPMETAFILEPICT
, 1, 0, "DSPMetaFile Picture", 0, 0, 0, 0, &ClipFormats
[14], &ClipFormats
[16]},
79 { CF_DSPBITMAP
, 1, 0, "DSPBitmap", 0, 0, 0, 0, &ClipFormats
[15], &ClipFormats
[17]},
80 { CF_HDROP
, 1, 0, "HDROP", 0, 0, 0, 0, &ClipFormats
[16], NULL
}
84 /**************************************************************************
85 * Internal Clipboard implementation methods
86 **************************************************************************/
89 /**************************************************************************
90 * CLIPBOARD_LookupFormat
92 static LPWINE_CLIPFORMAT
__lookup_format( LPWINE_CLIPFORMAT lpFormat
, WORD wID
)
96 if (lpFormat
== NULL
||
97 lpFormat
->wFormatID
== wID
) break;
98 lpFormat
= lpFormat
->NextFormat
;
103 LPWINE_CLIPFORMAT
CLIPBOARD_LookupFormat( WORD wID
)
105 return __lookup_format( ClipFormats
, wID
);
108 /**************************************************************************
110 * Check if the clipboard cache is available to the caller
112 BOOL
CLIPBOARD_IsLocked()
114 BOOL bIsLocked
= TRUE
;
115 HANDLE16 hTaskCur
= GetCurrentTask();
118 * The clipboard is available:
119 * 1. if the caller's task has opened the clipboard,
121 * 2. if the caller is the clipboard owners task, AND is responding to a
122 * WM_RENDERFORMAT message.
124 if ( hClipLock
== hTaskCur
)
127 else if ( hTaskCur
== hTaskClipOwner
)
129 /* Check if we're currently executing inside a window procedure
130 * called in response to a WM_RENDERFORMAT message. A WM_RENDERFORMAT
131 * handler is not permitted to open the clipboard since it has been opened
132 * by another client. However the handler must have access to the
133 * clipboard in order to update data in response to this message.
135 MESSAGEQUEUE
*queue
= QUEUE_Lock( GetFastQueue16() );
139 && queue
->smWaiting
->msg
== WM_RENDERFORMAT
140 && queue
->smWaiting
->hSrcQueue
144 QUEUE_Unlock( queue
);
150 /**************************************************************************
151 * CLIPBOARD_ReleaseOwner
152 * Gives up ownership of the clipboard
154 void CLIPBOARD_ReleaseOwner()
160 /**************************************************************************
161 * CLIPBOARD_GlobalFreeProc
163 * This is a callback mechanism to allow HGLOBAL data to be released in
164 * the context of the process which allocated it. We post a WM_TIMER message
165 * to the owner window(in CLIPBOARD_DeleteRecord) and destroy the data(in idEvent)
166 * in this WndProc, which is invoked when the apps message loop calls DispatchMessage.
167 * This technique is discussed in Matt Pietrek's "Under the Hood".
168 * An article describing the same may be found in MSDN by searching for WM_TIMER.
169 * Note that this mechanism will probably stop working when WINE supports
170 * address space separation. When "queue events" are implemented in Wine we
171 * should switch to using that mechanism, since it is more robust and does not
172 * require a procedure address to be passed. See the SetWinEventHook API for
175 VOID CALLBACK
CLIPBOARD_GlobalFreeProc( HWND hwnd
, UINT uMsg
, UINT idEvent
, DWORD dwTime
)
177 /* idEvent is the HGLOBAL to be deleted */
178 GlobalFree( (HGLOBAL
)idEvent
);
181 /**************************************************************************
182 * CLIPBOARD_DeleteRecord
184 void CLIPBOARD_DeleteRecord(LPWINE_CLIPFORMAT lpFormat
, BOOL bChange
)
186 if( (lpFormat
->wFormatID
>= CF_GDIOBJFIRST
&&
187 lpFormat
->wFormatID
<= CF_GDIOBJLAST
) || lpFormat
->wFormatID
== CF_BITMAP
188 || lpFormat
->wFormatID
== CF_PALETTE
)
190 if (lpFormat
->hData32
)
191 DeleteObject(lpFormat
->hData32
);
192 if (lpFormat
->hData16
)
193 DeleteObject(lpFormat
->hData16
);
195 else if( lpFormat
->wFormatID
== CF_METAFILEPICT
)
197 if (lpFormat
->hData32
)
199 DeleteMetaFile( ((METAFILEPICT
*)GlobalLock( lpFormat
->hData32
))->hMF
);
200 PostMessageA(hWndClipOwner
, WM_TIMER
,
201 (WPARAM
)lpFormat
->hData32
, (LPARAM
)CLIPBOARD_GlobalFreeProc
);
202 if (lpFormat
->hDataSrc32
)
204 /* Release lpFormat->hData32 in the context of the process which created it.
205 * See CLIPBOARD_GlobalFreeProc for more details about this technique.
206 * GlobalFree(lpFormat->hDataSrc32);
208 PostMessageA(hWndClipOwner
, WM_TIMER
,
209 (WPARAM
)lpFormat
->hDataSrc32
, (LPARAM
)CLIPBOARD_GlobalFreeProc
);
212 if (lpFormat
->hData16
)
213 /* HMETAFILE16 and HMETAFILE32 are apparently the same thing,
214 and a shallow copy is enough to share a METAFILEPICT
215 structure between 16bit and 32bit clipboards. The MetaFile
216 should of course only be deleted once. */
217 GlobalFree16(lpFormat
->hData16
);
219 if (lpFormat
->hData16
)
221 DeleteMetaFile16( ((METAFILEPICT16
*)GlobalLock16( lpFormat
->hData16
))->hMF
);
222 GlobalFree16(lpFormat
->hData16
);
227 if (lpFormat
->hData32
)
229 /* Release lpFormat->hData32 in the context of the process which created it.
230 * See CLIPBOARD_GlobalFreeProc for more details about this technique.
231 * GlobalFree( lpFormat->hData32 );
233 PostMessageA(hWndClipOwner
, WM_TIMER
,
234 (WPARAM
)lpFormat
->hData32
, (LPARAM
)CLIPBOARD_GlobalFreeProc
);
236 if (lpFormat
->hDataSrc32
)
238 /* Release lpFormat->hData32 in the context of the process which created it.
239 * See CLIPBOARD_GlobalFreeProc for more details about this technique.
240 * GlobalFree(lpFormat->hDataSrc32);
242 PostMessageA(hWndClipOwner
, WM_TIMER
,
243 (WPARAM
)lpFormat
->hDataSrc32
, (LPARAM
)CLIPBOARD_GlobalFreeProc
);
245 if (lpFormat
->hData16
)
246 GlobalFree16(lpFormat
->hData16
);
249 lpFormat
->wDataPresent
= 0;
250 lpFormat
->hData16
= 0;
251 lpFormat
->hData32
= 0;
252 lpFormat
->hDataSrc32
= 0;
253 lpFormat
->drvData
= 0;
255 if( bChange
) bCBHasChanged
= TRUE
;
258 /**************************************************************************
259 * CLIPBOARD_EmptyCache
261 void CLIPBOARD_EmptyCache( BOOL bChange
)
263 LPWINE_CLIPFORMAT lpFormat
= ClipFormats
;
267 if ( lpFormat
->wDataPresent
|| lpFormat
->hData16
|| lpFormat
->hData32
)
268 CLIPBOARD_DeleteRecord( lpFormat
, bChange
);
270 lpFormat
= lpFormat
->NextFormat
;
274 /**************************************************************************
275 * CLIPBOARD_IsPresent
277 BOOL
CLIPBOARD_IsPresent(WORD wFormat
)
281 if( wFormat
== CF_TEXT
|| wFormat
== CF_OEMTEXT
|| wFormat
== CF_UNICODETEXT
)
282 return ClipFormats
[CF_TEXT
-1].wDataPresent
||
283 ClipFormats
[CF_OEMTEXT
-1].wDataPresent
||
284 ClipFormats
[CF_UNICODETEXT
-1].wDataPresent
;
287 LPWINE_CLIPFORMAT lpFormat
= __lookup_format( ClipFormats
, wFormat
);
288 if( lpFormat
) return (lpFormat
->wDataPresent
);
293 /**************************************************************************
294 * CLIPBOARD_IsCacheRendered
295 * Checks if any data needs to be rendered to the clipboard cache
297 * TRUE - All clipboard data is available in the cache
298 * FALSE - Some data is marked for delayed render and needs rendering
300 BOOL
CLIPBOARD_IsCacheRendered()
302 LPWINE_CLIPFORMAT lpFormat
= ClipFormats
;
304 /* check if all formats were rendered */
307 if( lpFormat
->wDataPresent
&& !lpFormat
->hData16
&& !lpFormat
->hData32
)
310 lpFormat
= lpFormat
->NextFormat
;
317 /**************************************************************************
318 * CLIPBOARD_IsMemoryObject
319 * Tests if the clipboard format specifies a memory object
321 BOOL
CLIPBOARD_IsMemoryObject( WORD wFormat
)
326 case CF_METAFILEPICT
:
338 /***********************************************************************
339 * CLIPBOARD_GlobalDupMem( HGLOBAL )
340 * Helper method to duplicate an HGLOBAL chunk of memory into shared memory
342 HGLOBAL
CLIPBOARD_GlobalDupMem( HGLOBAL hGlobalSrc
)
345 PVOID pGlobalSrc
, pGlobalDest
;
351 cBytes
= GlobalSize(hGlobalSrc
);
355 /* Turn on the DDESHARE and _MOVEABLE flags explicitly */
356 hGlobalDest
= GlobalAlloc( GlobalFlags(hGlobalSrc
) | GMEM_DDESHARE
| GMEM_MOVEABLE
,
361 pGlobalSrc
= GlobalLock(hGlobalSrc
);
362 pGlobalDest
= GlobalLock(hGlobalDest
);
363 if ( !pGlobalSrc
|| !pGlobalDest
)
366 memcpy(pGlobalDest
, pGlobalSrc
, cBytes
);
368 GlobalUnlock(hGlobalSrc
);
369 GlobalUnlock(hGlobalDest
);
374 /**************************************************************************
375 * CLIPBOARD_GetFormatName
376 * Gets the format name associated with an ID
378 char * CLIPBOARD_GetFormatName(UINT wFormat
)
380 LPWINE_CLIPFORMAT lpFormat
= __lookup_format( ClipFormats
, wFormat
);
381 return (lpFormat
) ? lpFormat
->Name
: NULL
;
385 /**************************************************************************
386 * CLIPBOARD_RenderFormat
388 static BOOL
CLIPBOARD_RenderFormat(LPWINE_CLIPFORMAT lpFormat
)
391 * If WINE is not the selection owner, and the format is available
392 * we must ask the driver to render the data to the clipboard cache.
394 TRACE("enter format=%d\n", lpFormat
->wFormatID
);
395 if ( !USER_Driver
.pIsSelectionOwner()
396 && USER_Driver
.pIsClipboardFormatAvailable( lpFormat
->wFormatID
) )
398 if ( !USER_Driver
.pGetClipboardData( lpFormat
->wFormatID
) )
402 * If Wine owns the clipboard, and the data is marked for delayed render,
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);
418 WARN("\thWndClipOwner (%04x) is lost!\n",
420 CLIPBOARD_ReleaseOwner();
421 lpFormat
->wDataPresent
= 0;
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
)
438 if(src_fmt
== CF_UNICODETEXT
)
451 return WideCharToMultiByte(cp
, 0, src
, src_size
, dst
, dst_size
, NULL
, NULL
);
454 if(dst_fmt
== CF_UNICODETEXT
)
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
);
477 OemToCharBuffA(src
, dst
, 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
= NULL
;
496 BOOL foundData
= FALSE
;
498 /* Asked for CF_TEXT but not available - always attempt to convert
499 from CF_UNICODETEXT or CF_OEMTEXT */
500 if( wFormat
== CF_TEXT
&& !ClipFormats
[CF_TEXT
-1].wDataPresent
)
502 if(ClipFormats
[CF_UNICODETEXT
-1].wDataPresent
)
504 /* Convert UNICODETEXT -> TEXT */
505 lpSource
= &ClipFormats
[CF_UNICODETEXT
-1];
506 lpTarget
= &ClipFormats
[CF_TEXT
-1];
508 TRACE("\tUNICODETEXT -> TEXT\n");
510 else if(ClipFormats
[CF_OEMTEXT
-1].wDataPresent
)
512 /* Convert OEMTEXT -> TEXT */
513 lpSource
= &ClipFormats
[CF_OEMTEXT
-1];
514 lpTarget
= &ClipFormats
[CF_TEXT
-1];
516 TRACE("\tOEMTEXT -> TEXT\n");
519 /* Asked for CF_OEMTEXT but not available - always attempt to convert
520 from CF_UNICODETEXT or CF_TEXT */
521 else if( wFormat
== CF_OEMTEXT
&& !ClipFormats
[CF_OEMTEXT
-1].wDataPresent
)
523 if(ClipFormats
[CF_UNICODETEXT
-1].wDataPresent
)
525 /* Convert UNICODETEXT -> OEMTEXT */
526 lpSource
= &ClipFormats
[CF_UNICODETEXT
-1];
527 lpTarget
= &ClipFormats
[CF_OEMTEXT
-1];
529 TRACE("\tUNICODETEXT -> OEMTEXT\n");
531 else if(ClipFormats
[CF_TEXT
-1].wDataPresent
)
533 /* Convert TEXT -> OEMTEXT */
534 lpSource
= &ClipFormats
[CF_TEXT
-1];
535 lpTarget
= &ClipFormats
[CF_OEMTEXT
-1];
537 TRACE("\tTEXT -> OEMTEXT\n");
540 /* Asked for CF_UNICODETEXT but not available - always attempt to convert
541 from CF_TEXT or CF_OEMTEXT */
542 else if( wFormat
== CF_UNICODETEXT
&& !ClipFormats
[CF_UNICODETEXT
-1].wDataPresent
)
544 if(ClipFormats
[CF_TEXT
-1].wDataPresent
)
546 /* Convert TEXT -> UNICODETEXT */
547 lpSource
= &ClipFormats
[CF_TEXT
-1];
548 lpTarget
= &ClipFormats
[CF_UNICODETEXT
-1];
550 TRACE("\tTEXT -> UNICODETEXT\n");
552 else if(ClipFormats
[CF_OEMTEXT
-1].wDataPresent
)
554 /* Convert OEMTEXT -> UNICODETEXT */
555 lpSource
= &ClipFormats
[CF_OEMTEXT
-1];
556 lpTarget
= &ClipFormats
[CF_UNICODETEXT
-1];
558 TRACE("\tOEMTEXT -> UNICODETEXT\n");
563 if ((wFormat
== CF_TEXT
) || (wFormat
== CF_OEMTEXT
))
565 lpSource
= &ClipFormats
[CF_UNICODETEXT
-1];
566 lpTarget
= __lookup_format( ClipFormats
, wFormat
);
570 lpSource
= __lookup_format( ClipFormats
, wFormat
);
575 /* First render the source text format */
576 if ( !lpSource
|| !CLIPBOARD_RenderFormat(lpSource
) ) return NULL
;
578 /* Convert to the desired target text format, if necessary */
579 if( lpTarget
!= lpSource
&& !lpTarget
->hData16
&& !lpTarget
->hData32
)
581 INT src_chars
, dst_chars
, alloc_size
;
585 if (lpSource
->hData32
)
587 lpstrS
= (LPSTR
)GlobalLock(lpSource
->hData32
);
591 lpstrS
= (LPSTR
)GlobalLock16(lpSource
->hData16
);
594 if( !lpstrS
) return NULL
;
596 /* Text always NULL terminated */
597 if(lpSource
->wFormatID
== CF_UNICODETEXT
)
598 src_chars
= strlenW((LPCWSTR
)lpstrS
);
600 src_chars
= strlen(lpstrS
);
602 /* Calculate number of characters in the destination buffer */
603 dst_chars
= CLIPBOARD_ConvertText(lpSource
->wFormatID
, lpstrS
, src_chars
,
604 lpTarget
->wFormatID
, NULL
, 0);
605 if(!dst_chars
) return NULL
;
607 TRACE("\tconverting from '%s' to '%s', %i chars\n",
608 lpSource
->Name
, lpTarget
->Name
, src_chars
);
610 /* Convert characters to bytes */
611 if(lpTarget
->wFormatID
== CF_UNICODETEXT
)
612 alloc_size
= dst_chars
* sizeof(WCHAR
);
614 alloc_size
= dst_chars
;
616 lpTarget
->hData32
= GlobalAlloc(GMEM_ZEROINIT
| GMEM_MOVEABLE
| GMEM_DDESHARE
, alloc_size
);
617 lpstrT
= (LPSTR
)GlobalLock(lpTarget
->hData32
);
621 CLIPBOARD_ConvertText(lpSource
->wFormatID
, lpstrS
, src_chars
,
622 lpTarget
->wFormatID
, lpstrT
, dst_chars
);
623 GlobalUnlock(lpTarget
->hData32
);
626 lpTarget
->hData32
= 0;
629 if (lpSource
->hData32
)
630 GlobalUnlock(lpSource
->hData32
);
632 GlobalUnlock16(lpSource
->hData16
);
635 return (lpTarget
->hData16
|| lpTarget
->hData32
) ? lpTarget
: NULL
;
638 /**************************************************************************
639 * CLIPBOARD_EnumClipboardFormats (internal)
641 static UINT
CLIPBOARD_EnumClipboardFormats( UINT wFormat
)
643 LPWINE_CLIPFORMAT lpFormat
= ClipFormats
;
646 if (wFormat
== 0) /* start from the beginning */
647 lpFormat
= ClipFormats
;
650 /* walk up to the specified format record */
652 if( !(lpFormat
= __lookup_format( lpFormat
, wFormat
)) )
654 lpFormat
= lpFormat
->NextFormat
; /* right */
659 if (lpFormat
== NULL
) return 0;
661 if(CLIPBOARD_IsPresent(lpFormat
->wFormatID
))
664 /* Query the driver if not yet in the cache */
665 if (!USER_Driver
.pIsSelectionOwner())
667 if(lpFormat
->wFormatID
== CF_UNICODETEXT
||
668 lpFormat
->wFormatID
== CF_TEXT
||
669 lpFormat
->wFormatID
== CF_OEMTEXT
)
671 if(USER_Driver
.pIsClipboardFormatAvailable(CF_UNICODETEXT
) ||
672 USER_Driver
.pIsClipboardFormatAvailable(CF_TEXT
) ||
673 USER_Driver
.pIsClipboardFormatAvailable(CF_OEMTEXT
))
674 bFormatPresent
= TRUE
;
676 bFormatPresent
= FALSE
;
679 bFormatPresent
= USER_Driver
.pIsClipboardFormatAvailable(lpFormat
->wFormatID
);
685 lpFormat
= lpFormat
->NextFormat
;
688 TRACE("Next available format %d\n", lpFormat
->wFormatID
);
690 return lpFormat
->wFormatID
;
694 /**************************************************************************
695 * WIN32 Clipboard implementation
696 **************************************************************************/
698 /**************************************************************************
699 * OpenClipboard (USER.137)
701 BOOL16 WINAPI
OpenClipboard16( HWND16 hWnd
)
703 return OpenClipboard( hWnd
);
707 /**************************************************************************
708 * OpenClipboard (USER32.@)
710 * Note: Netscape uses NULL hWnd to open the clipboard.
712 BOOL WINAPI
OpenClipboard( HWND hWnd
)
716 TRACE("(%04x)...\n", hWnd
);
720 hClipLock
= GetCurrentTask();
722 /* Save current user of the clipboard */
723 hWndClipWindow
= hWnd
;
724 bCBHasChanged
= FALSE
;
729 TRACE(" returning %i\n", bRet
);
734 /**************************************************************************
735 * CloseClipboard (USER.138)
737 BOOL16 WINAPI
CloseClipboard16(void)
739 return CloseClipboard();
743 /**************************************************************************
744 * CloseClipboard (USER32.@)
746 BOOL WINAPI
CloseClipboard(void)
750 if (hClipLock
== GetCurrentTask())
754 if (bCBHasChanged
&& hWndViewer
)
755 SendMessage16(hWndViewer
, WM_DRAWCLIPBOARD
, 0, 0L);
762 /**************************************************************************
763 * EmptyClipboard16 (USER.139)
765 BOOL16 WINAPI
EmptyClipboard16(void)
767 return EmptyClipboard();
771 /**************************************************************************
772 * EmptyClipboard (USER32.@)
773 * Empties and acquires ownership of the clipboard
775 BOOL WINAPI
EmptyClipboard(void)
779 if (hClipLock
!= GetCurrentTask())
781 WARN("Clipboard not opened by calling task!");
785 /* destroy private objects */
788 SendMessage16(hWndClipOwner
, WM_DESTROYCLIPBOARD
, 0, 0L);
790 /* empty the cache */
791 CLIPBOARD_EmptyCache(TRUE
);
793 /* Assign ownership of the clipboard to the current client */
794 hWndClipOwner
= hWndClipWindow
;
796 /* Save the current task */
797 hTaskClipOwner
= GetCurrentTask();
799 /* Tell the driver to acquire the selection */
800 USER_Driver
.pAcquireClipboard();
806 /**************************************************************************
807 * GetClipboardOwner (USER.140)
808 * FIXME: Can't return the owner if the clipbard is owned by an external app
810 HWND16 WINAPI
GetClipboardOwner16(void)
813 return hWndClipOwner
;
817 /**************************************************************************
818 * GetClipboardOwner (USER32.@)
819 * FIXME: Can't return the owner if the clipbard is owned by an external app
821 HWND WINAPI
GetClipboardOwner(void)
824 return hWndClipOwner
;
828 /**************************************************************************
829 * SetClipboardData (USER.141)
831 HANDLE16 WINAPI
SetClipboardData16( UINT16 wFormat
, HANDLE16 hData
)
833 LPWINE_CLIPFORMAT lpFormat
= __lookup_format( ClipFormats
, wFormat
);
835 TRACE("(%04X, %04x) !\n", wFormat
, hData
);
837 /* NOTE: If the hData is zero and current owner doesn't match
838 * the window that opened the clipboard then this application
839 * is screwed because WM_RENDERFORMAT will go to the owner.
840 * (to become the owner it must call EmptyClipboard() before
844 if( CLIPBOARD_IsLocked() || !lpFormat
||
845 (!hData
&& (!hWndClipOwner
|| (hWndClipOwner
!= hWndClipWindow
))) )
847 WARN("Invalid hData or clipboard not opened by calling task!\n");
851 /* Pass on the request to the driver */
852 USER_Driver
.pSetClipboardData(wFormat
);
854 if ( lpFormat
->wDataPresent
|| 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
->hData16
= hData
; /* 0 is legal, see WM_RENDERFORMAT */
879 lpFormat
->hData32
= 0;
881 return lpFormat
->hData16
;
885 /**************************************************************************
886 * SetClipboardData (USER32.@)
888 HANDLE WINAPI
SetClipboardData( UINT wFormat
, HANDLE hData
)
890 LPWINE_CLIPFORMAT lpFormat
= __lookup_format( ClipFormats
, wFormat
);
892 TRACE("(%08X, %08x) !\n", wFormat
, hData
);
894 /* NOTE: If the hData is zero and current owner doesn't match
895 * the window that opened the clipboard then this application
896 * is screwed because WM_RENDERFORMAT will go to the owner.
897 * (to become the owner it must call EmptyClipboard() before
901 if( CLIPBOARD_IsLocked() || !lpFormat
||
902 (!hData
&& (!hWndClipOwner
|| (hWndClipOwner
!= hWndClipWindow
))) )
904 WARN("Invalid hData or clipboard not opened by calling task!\n");
908 /* Tell the driver to acquire the selection */
909 USER_Driver
.pAcquireClipboard();
911 if ( lpFormat
->wDataPresent
&&
912 (lpFormat
->hData16
|| lpFormat
->hData32
) )
914 CLIPBOARD_DeleteRecord(lpFormat
, TRUE
);
916 /* delete existing CF_UNICODETEXT/CF_TEXT/CF_OEMTEXT aliases */
917 if(wFormat
== CF_UNICODETEXT
)
919 CLIPBOARD_DeleteRecord(&ClipFormats
[CF_TEXT
-1], TRUE
);
920 CLIPBOARD_DeleteRecord(&ClipFormats
[CF_OEMTEXT
-1], TRUE
);
922 else if(wFormat
== CF_TEXT
)
924 CLIPBOARD_DeleteRecord(&ClipFormats
[CF_UNICODETEXT
-1], TRUE
);
925 CLIPBOARD_DeleteRecord(&ClipFormats
[CF_OEMTEXT
-1], TRUE
);
927 else if(wFormat
== CF_OEMTEXT
)
929 CLIPBOARD_DeleteRecord(&ClipFormats
[CF_UNICODETEXT
-1], TRUE
);
930 CLIPBOARD_DeleteRecord(&ClipFormats
[CF_TEXT
-1], TRUE
);
934 bCBHasChanged
= TRUE
;
935 lpFormat
->wDataPresent
= 1;
936 lpFormat
->hDataSrc32
= hData
; /* Save the source handle */
939 * Make a shared duplicate if the memory is not shared
940 * TODO: What should be done for non-memory objects
942 if ( CLIPBOARD_IsMemoryObject(wFormat
) && hData
&& !(GlobalFlags(hData
) & GMEM_DDESHARE
) )
943 lpFormat
->hData32
= CLIPBOARD_GlobalDupMem( hData
);
945 lpFormat
->hData32
= hData
; /* 0 is legal, see WM_RENDERFORMAT */
947 lpFormat
->hData16
= 0;
949 return lpFormat
->hData32
; /* Should we return lpFormat->hDataSrc32 */
953 /**************************************************************************
954 * GetClipboardData (USER.142)
956 HANDLE16 WINAPI
GetClipboardData16( UINT16 wFormat
)
958 LPWINE_CLIPFORMAT lpRender
= ClipFormats
;
960 TRACE("(%04X)\n", wFormat
);
962 if (CLIPBOARD_IsLocked())
964 WARN("Clipboard not opened by calling task!\n");
968 if( wFormat
== CF_UNICODETEXT
|| wFormat
== CF_TEXT
|| wFormat
== CF_OEMTEXT
)
970 lpRender
= CLIPBOARD_RenderText(wFormat
);
971 if ( !lpRender
) return 0;
975 lpRender
= __lookup_format( ClipFormats
, wFormat
);
976 if( !lpRender
|| !CLIPBOARD_RenderFormat(lpRender
) ) return 0;
979 /* Convert between 32 -> 16 bit data, if necessary */
980 if( lpRender
->hData32
&& !lpRender
->hData16
981 && CLIPBOARD_IsMemoryObject(wFormat
) )
984 if( lpRender
->wFormatID
== CF_METAFILEPICT
)
985 size
= sizeof( METAFILEPICT16
);
987 size
= GlobalSize(lpRender
->hData32
);
989 lpRender
->hData16
= GlobalAlloc16(GMEM_ZEROINIT
, size
);
990 if( !lpRender
->hData16
)
991 ERR("(%04X) -- not enough memory in 16b heap\n", wFormat
);
994 if( lpRender
->wFormatID
== CF_METAFILEPICT
)
996 FIXME("\timplement function CopyMetaFilePict32to16\n");
997 FIXME("\tin the appropriate file.\n");
998 #ifdef SOMEONE_IMPLEMENTED_ME
999 CopyMetaFilePict32to16( GlobalLock16(lpRender
->hData16
),
1000 GlobalLock(lpRender
->hData32
) );
1005 memcpy( GlobalLock16(lpRender
->hData16
),
1006 GlobalLock(lpRender
->hData32
),
1009 GlobalUnlock16(lpRender
->hData16
);
1010 GlobalUnlock(lpRender
->hData32
);
1014 TRACE("\treturning %04x (type %i)\n",
1015 lpRender
->hData16
, lpRender
->wFormatID
);
1016 return lpRender
->hData16
;
1020 /**************************************************************************
1021 * GetClipboardData (USER32.@)
1023 HANDLE WINAPI
GetClipboardData( UINT wFormat
)
1025 LPWINE_CLIPFORMAT lpRender
= ClipFormats
;
1027 TRACE("(%08X)\n", wFormat
);
1029 if (CLIPBOARD_IsLocked())
1031 WARN("Clipboard not opened by calling task!");
1035 if( wFormat
== CF_UNICODETEXT
|| wFormat
== CF_TEXT
|| wFormat
== CF_OEMTEXT
)
1037 lpRender
= CLIPBOARD_RenderText(wFormat
);
1038 if ( !lpRender
) return 0;
1042 lpRender
= __lookup_format( ClipFormats
, wFormat
);
1043 if( !lpRender
|| !CLIPBOARD_RenderFormat(lpRender
) ) return 0;
1046 /* Convert between 16 -> 32 bit data, if necessary */
1047 if( lpRender
->hData16
&& !lpRender
->hData32
1048 && CLIPBOARD_IsMemoryObject(wFormat
) )
1051 if( lpRender
->wFormatID
== CF_METAFILEPICT
)
1052 size
= sizeof( METAFILEPICT
);
1054 size
= GlobalSize16(lpRender
->hData16
);
1055 lpRender
->hData32
= GlobalAlloc(GMEM_ZEROINIT
| GMEM_MOVEABLE
| GMEM_DDESHARE
,
1057 if( lpRender
->wFormatID
== CF_METAFILEPICT
)
1059 FIXME("\timplement function CopyMetaFilePict16to32\n");
1060 FIXME("\tin the appropriate file.\n");
1061 #ifdef SOMEONE_IMPLEMENTED_ME
1062 CopyMetaFilePict16to32( GlobalLock16(lpRender
->hData32
),
1063 GlobalLock(lpRender
->hData16
) );
1068 memcpy( GlobalLock(lpRender
->hData32
),
1069 GlobalLock16(lpRender
->hData16
),
1072 GlobalUnlock(lpRender
->hData32
);
1073 GlobalUnlock16(lpRender
->hData16
);
1076 TRACE("\treturning %04x (type %i)\n",
1077 lpRender
->hData32
, lpRender
->wFormatID
);
1078 return lpRender
->hData32
;
1082 /**************************************************************************
1083 * CountClipboardFormats (USER.143)
1085 INT16 WINAPI
CountClipboardFormats16(void)
1087 return CountClipboardFormats();
1091 /**************************************************************************
1092 * CountClipboardFormats (USER32.@)
1094 INT WINAPI
CountClipboardFormats(void)
1096 INT FormatCount
= 0;
1097 LPWINE_CLIPFORMAT lpFormat
= ClipFormats
;
1103 if (lpFormat
== NULL
) break;
1105 if( lpFormat
->wFormatID
!= CF_TEXT
) /* Don't count CF_TEXT */
1108 * The format is available if either:
1109 * 1. The data is already in the cache.
1110 * 2. The selection is not owned by us(WINE) and the data is
1111 * available to the clipboard driver.
1113 if ( lpFormat
->wDataPresent
||
1114 ( !USER_Driver
.pIsSelectionOwner()
1115 && USER_Driver
.pIsClipboardFormatAvailable( lpFormat
->wFormatID
) ) )
1117 TRACE("\tdata found for format 0x%04x(%s)\n",
1118 lpFormat
->wFormatID
, CLIPBOARD_GetFormatName(lpFormat
->wFormatID
));
1123 lpFormat
= lpFormat
->NextFormat
;
1126 /* these are equivalent, adjust the total */
1127 FormatCount
+= (ClipFormats
[CF_UNICODETEXT
-1].wDataPresent
||
1128 ClipFormats
[CF_TEXT
-1].wDataPresent
||
1129 ClipFormats
[CF_OEMTEXT
-1].wDataPresent
) ? 1 : 0;
1131 TRACE("\ttotal %d\n", FormatCount
);
1135 /**************************************************************************
1136 * EnumClipboardFormats (USER.144)
1138 UINT16 WINAPI
EnumClipboardFormats16( UINT16 wFormat
)
1140 return EnumClipboardFormats( wFormat
);
1144 /**************************************************************************
1145 * EnumClipboardFormats (USER32.@)
1147 UINT WINAPI
EnumClipboardFormats( UINT wFormat
)
1149 TRACE("(%04X)\n", wFormat
);
1151 if (CLIPBOARD_IsLocked())
1153 WARN("Clipboard not opened by calling task!");
1157 return CLIPBOARD_EnumClipboardFormats(wFormat
);
1161 /**************************************************************************
1162 * RegisterClipboardFormat (USER.145)
1164 UINT16 WINAPI
RegisterClipboardFormat16( LPCSTR FormatName
)
1166 LPWINE_CLIPFORMAT lpNewFormat
;
1167 LPWINE_CLIPFORMAT lpFormat
= ClipFormats
;
1169 if (FormatName
== NULL
) return 0;
1171 TRACE("('%s') !\n", FormatName
);
1173 /* walk format chain to see if it's already registered */
1177 if ( !strcmp(lpFormat
->Name
,FormatName
) )
1179 lpFormat
->wRefCount
++;
1180 return lpFormat
->wFormatID
;
1183 if ( lpFormat
->NextFormat
== NULL
) break;
1185 lpFormat
= lpFormat
->NextFormat
;
1188 /* allocate storage for new format entry */
1190 lpNewFormat
= (LPWINE_CLIPFORMAT
)HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_CLIPFORMAT
));
1191 if(lpNewFormat
== NULL
) {
1192 WARN("No more memory for a new format!");
1195 lpFormat
->NextFormat
= lpNewFormat
;
1196 lpNewFormat
->wFormatID
= LastRegFormat
;
1197 lpNewFormat
->wRefCount
= 1;
1199 lpNewFormat
->Name
= (LPSTR
)HEAP_strdupA(GetProcessHeap(), 0, FormatName
);
1200 if(lpNewFormat
->Name
== NULL
) {
1201 WARN("No more memory for the new format name!");
1202 HeapFree(GetProcessHeap(), 0, lpNewFormat
);
1206 lpNewFormat
->wDataPresent
= 0;
1207 lpNewFormat
->hData16
= 0;
1208 lpNewFormat
->hDataSrc32
= 0;
1209 lpNewFormat
->hData32
= 0;
1210 lpNewFormat
->drvData
= 0;
1211 lpNewFormat
->PrevFormat
= lpFormat
;
1212 lpNewFormat
->NextFormat
= NULL
;
1214 /* Pass on the registration request to the driver */
1215 USER_Driver
.pRegisterClipboardFormat( FormatName
);
1217 return LastRegFormat
++;
1221 /**************************************************************************
1222 * RegisterClipboardFormatA (USER32.@)
1224 UINT WINAPI
RegisterClipboardFormatA( LPCSTR formatName
)
1226 return RegisterClipboardFormat16( formatName
);
1230 /**************************************************************************
1231 * RegisterClipboardFormatW (USER32.@)
1233 UINT WINAPI
RegisterClipboardFormatW( LPCWSTR formatName
)
1235 LPSTR aFormat
= HEAP_strdupWtoA( GetProcessHeap(), 0, formatName
);
1236 UINT ret
= RegisterClipboardFormatA( aFormat
);
1237 HeapFree( GetProcessHeap(), 0, aFormat
);
1242 /**************************************************************************
1243 * GetClipboardFormatName (USER.146)
1245 INT16 WINAPI
GetClipboardFormatName16( UINT16 wFormat
, LPSTR retStr
, INT16 maxlen
)
1247 return GetClipboardFormatNameA( wFormat
, retStr
, maxlen
);
1251 /**************************************************************************
1252 * GetClipboardFormatNameA (USER32.@)
1254 INT WINAPI
GetClipboardFormatNameA( UINT wFormat
, LPSTR retStr
, INT maxlen
)
1256 LPWINE_CLIPFORMAT lpFormat
= __lookup_format( ClipFormats
, wFormat
);
1258 TRACE("(%04X, %p, %d) !\n", wFormat
, retStr
, maxlen
);
1260 if (lpFormat
== NULL
|| lpFormat
->Name
== NULL
||
1261 lpFormat
->wFormatID
< CF_REGFORMATBASE
) return 0;
1263 TRACE("Name='%s' !\n", lpFormat
->Name
);
1265 lstrcpynA( retStr
, lpFormat
->Name
, maxlen
);
1266 return strlen(retStr
);
1270 /**************************************************************************
1271 * GetClipboardFormatNameW (USER32.@)
1273 INT WINAPI
GetClipboardFormatNameW( UINT wFormat
, LPWSTR retStr
, INT maxlen
)
1276 LPSTR p
= HeapAlloc( GetProcessHeap(), 0, maxlen
);
1277 if(p
== NULL
) return 0; /* FIXME: is this the correct failure value? */
1279 ret
= GetClipboardFormatNameA( wFormat
, p
, maxlen
);
1281 if (maxlen
> 0 && !MultiByteToWideChar( CP_ACP
, 0, p
, -1, retStr
, maxlen
))
1282 retStr
[maxlen
-1] = 0;
1283 HeapFree( GetProcessHeap(), 0, p
);
1288 /**************************************************************************
1289 * SetClipboardViewer (USER.147)
1291 HWND16 WINAPI
SetClipboardViewer16( HWND16 hWnd
)
1293 TRACE("(%04x)\n", hWnd
);
1294 return SetClipboardViewer( hWnd
);
1298 /**************************************************************************
1299 * SetClipboardViewer (USER32.@)
1301 HWND WINAPI
SetClipboardViewer( HWND hWnd
)
1303 HWND hwndPrev
= hWndViewer
;
1305 TRACE("(%04x): returning %04x\n", hWnd
, hwndPrev
);
1312 /**************************************************************************
1313 * GetClipboardViewer (USER.148)
1315 HWND16 WINAPI
GetClipboardViewer16(void)
1322 /**************************************************************************
1323 * GetClipboardViewer (USER32.@)
1325 HWND WINAPI
GetClipboardViewer(void)
1332 /**************************************************************************
1333 * ChangeClipboardChain (USER.149)
1335 BOOL16 WINAPI
ChangeClipboardChain16(HWND16 hWnd
, HWND16 hWndNext
)
1337 return ChangeClipboardChain(hWnd
, hWndNext
);
1341 /**************************************************************************
1342 * ChangeClipboardChain (USER32.@)
1344 BOOL WINAPI
ChangeClipboardChain(HWND hWnd
, HWND hWndNext
)
1348 FIXME("(0x%04x, 0x%04x): stub?\n", hWnd
, hWndNext
);
1351 bRet
= !SendMessage16( hWndViewer
, WM_CHANGECBCHAIN
,
1352 (WPARAM16
)hWnd
, (LPARAM
)hWndNext
);
1354 WARN("hWndViewer is lost\n");
1356 if( hWnd
== hWndViewer
) hWndViewer
= hWndNext
;
1362 /**************************************************************************
1363 * IsClipboardFormatAvailable16 (USER.193)
1365 BOOL16 WINAPI
IsClipboardFormatAvailable16( UINT16 wFormat
)
1367 return IsClipboardFormatAvailable( wFormat
);
1371 /**************************************************************************
1372 * IsClipboardFormatAvailable (USER32.@)
1374 BOOL WINAPI
IsClipboardFormatAvailable( UINT wFormat
)
1378 if (wFormat
== 0) /* Reject this case quickly */
1382 UINT iret
= CLIPBOARD_EnumClipboardFormats(wFormat
- 1);
1383 if ((wFormat
== CF_TEXT
) || (wFormat
== CF_OEMTEXT
) || (wFormat
== CF_UNICODETEXT
))
1384 bRet
= ((iret
== CF_TEXT
) || (iret
== CF_OEMTEXT
) || (iret
== CF_UNICODETEXT
));
1386 bRet
= iret
== wFormat
;
1388 TRACE("(%04X)- ret(%d)\n", wFormat
, bRet
);
1393 /**************************************************************************
1394 * GetOpenClipboardWindow (USER.248)
1395 * FIXME: This wont work if an external app owns the selection
1397 HWND16 WINAPI
GetOpenClipboardWindow16(void)
1400 return hWndClipWindow
;
1404 /**************************************************************************
1405 * GetOpenClipboardWindow (USER32.@)
1406 * FIXME: This wont work if an external app owns the selection
1408 HWND WINAPI
GetOpenClipboardWindow(void)
1411 return hWndClipWindow
;
1415 /**************************************************************************
1416 * GetPriorityClipboardFormat (USER.402)
1418 INT16 WINAPI
GetPriorityClipboardFormat16( UINT16
*lpPriorityList
, INT16 nCount
)
1420 FIXME("(%p,%d): stub\n", lpPriorityList
, nCount
);
1425 /**************************************************************************
1426 * GetPriorityClipboardFormat (USER32.@)
1428 INT WINAPI
GetPriorityClipboardFormat( UINT
*lpPriorityList
, INT nCount
)
1433 if(CountClipboardFormats() == 0)
1438 for(Counter
= 0; Counter
<= nCount
; Counter
++)
1440 if(IsClipboardFormatAvailable(*(lpPriorityList
+sizeof(INT
)*Counter
)))
1441 return *(lpPriorityList
+sizeof(INT
)*Counter
);
1448 /**************************************************************************
1449 * GetClipboardSequenceNumber (USER32.@)
1450 * Supported on Win2k/Win98
1451 * MSDN: Windows clipboard code keeps a serial number for the clipboard
1452 * for each window station. The number is incremented whenever the
1453 * contents change or are emptied.
1454 * If you do not have WINSTA_ACCESSCLIPBOARD then the function returns 0
1456 DWORD WINAPI
GetClipboardSequenceNumber(VOID
)
1458 FIXME("Returning 0, see windows/clipboard.c\n");
1459 /* FIXME: Use serial numbers */