Release 980329
[wine/multimedia.git] / windows / clipboard.c
blobed04c6dfea50400c544e3c29284ac9da76aa96a5
1 /*
2 * WINE clipboard function handling
4 * Copyright 1994 Martin Ayotte
5 * 1996 Alex Korobka
7 */
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <unistd.h>
15 #include <string.h>
16 #include "ts_xlib.h"
17 #include <X11/Xatom.h>
18 #include "windows.h"
19 #include "win.h"
20 #include "heap.h"
21 #include "message.h"
22 #include "clipboard.h"
23 #include "xmalloc.h"
24 #include "debug.h"
26 #define CF_REGFORMATBASE 0xC000
28 typedef struct tagCLIPFORMAT {
29 WORD wFormatID;
30 WORD wRefCount;
31 WORD wDataPresent;
32 LPSTR Name;
33 HANDLE16 hData;
34 DWORD BufSize;
35 struct tagCLIPFORMAT *PrevFormat;
36 struct tagCLIPFORMAT *NextFormat;
37 } CLIPFORMAT, *LPCLIPFORMAT;
39 /* *************************************************************************
40 * internal variables
43 static HQUEUE16 hqClipLock = 0;
44 static BOOL32 bCBHasChanged = FALSE;
46 static HWND32 hWndClipOwner = 0; /* current clipboard owner */
47 static HWND32 hWndClipWindow = 0; /* window that opened clipboard */
48 static HWND32 hWndViewer = 0; /* start of viewers chain */
50 static WORD LastRegFormat = CF_REGFORMATBASE;
52 static Bool selectionWait = False;
53 static Bool selectionAcquired = False;
54 static Window selectionWindow = None;
55 static Window selectionPrevWindow = None;
57 static CLIPFORMAT ClipFormats[16] = {
58 { CF_TEXT, 1, 0, "Text", (HANDLE16)NULL, 0, NULL, &ClipFormats[1] },
59 { CF_BITMAP, 1, 0, "Bitmap", (HANDLE16)NULL, 0, &ClipFormats[0], &ClipFormats[2] },
60 { CF_METAFILEPICT, 1, 0, "MetaFile Picture", (HANDLE16)NULL, 0, &ClipFormats[1], &ClipFormats[3] },
61 { CF_SYLK, 1, 0, "Sylk", (HANDLE16)NULL, 0, &ClipFormats[2], &ClipFormats[4] },
62 { CF_DIF, 1, 0, "DIF", (HANDLE16)NULL, 0, &ClipFormats[3], &ClipFormats[5] },
63 { CF_TIFF, 1, 0, "TIFF", (HANDLE16)NULL, 0, &ClipFormats[4], &ClipFormats[6] },
64 { CF_OEMTEXT, 1, 0, "OEM Text", (HANDLE16)NULL, 0, &ClipFormats[5], &ClipFormats[7] },
65 { CF_DIB, 1, 0, "DIB", (HANDLE16)NULL, 0, &ClipFormats[6], &ClipFormats[8] },
66 { CF_PALETTE, 1, 0, "Palette", (HANDLE16)NULL, 0, &ClipFormats[7], &ClipFormats[9] },
67 { CF_PENDATA, 1, 0, "PenData", (HANDLE16)NULL, 0, &ClipFormats[8], &ClipFormats[10] },
68 { CF_RIFF, 1, 0, "RIFF", (HANDLE16)NULL, 0, &ClipFormats[9], &ClipFormats[11] },
69 { CF_WAVE, 1, 0, "Wave", (HANDLE16)NULL, 0, &ClipFormats[10], &ClipFormats[12] },
70 { CF_OWNERDISPLAY, 1, 0, "Owner Display", (HANDLE16)NULL, 0, &ClipFormats[11], &ClipFormats[13] },
71 { CF_DSPTEXT, 1, 0, "DSPText", (HANDLE16)NULL, 0, &ClipFormats[12], &ClipFormats[14] },
72 { CF_DSPMETAFILEPICT, 1, 0, "DSPMetaFile Picture", (HANDLE16)NULL, 0, &ClipFormats[13], &ClipFormats[15] },
73 { CF_DSPBITMAP, 1, 0, "DSPBitmap", (HANDLE16)NULL, 0, &ClipFormats[14], NULL }
76 static LPCLIPFORMAT __lookup_format( LPCLIPFORMAT lpFormat, WORD wID )
78 while(TRUE)
80 if (lpFormat == NULL ||
81 lpFormat->wFormatID == wID) break;
82 lpFormat = lpFormat->NextFormat;
84 return lpFormat;
87 /**************************************************************************
88 * CLIPBOARD_CheckSelection
90 * Prevent X selection from being lost when a top level window is
91 * destroyed.
93 static void CLIPBOARD_CheckSelection(WND* pWnd)
95 TRACE(clipboard,"\tchecking %08x\n", (unsigned)pWnd->window);
97 if( selectionAcquired && selectionWindow != None &&
98 pWnd->window == selectionWindow )
100 selectionPrevWindow = selectionWindow;
101 selectionWindow = None;
103 if( pWnd->next )
104 selectionWindow = pWnd->next->window;
105 else if( pWnd->parent )
106 if( pWnd->parent->child != pWnd )
107 selectionWindow = pWnd->parent->child->window;
109 TRACE(clipboard,"\tswitching selection from %08x to %08x\n",
110 (unsigned)selectionPrevWindow, (unsigned)selectionWindow);
112 if( selectionWindow != None )
114 TSXSetSelectionOwner(display, XA_PRIMARY, selectionWindow, CurrentTime);
115 if( TSXGetSelectionOwner(display, XA_PRIMARY) != selectionWindow )
116 selectionWindow = None;
121 /**************************************************************************
122 * CLIPBOARD_ResetLock
124 void CLIPBOARD_ResetLock( HQUEUE16 hqCurrent, HQUEUE16 hqNew )
126 if( hqClipLock == hqCurrent )
128 if( hqNew )
129 hqClipLock = hqNew;
130 else
132 hWndClipOwner = 0;
133 hWndClipWindow = 0;
134 EmptyClipboard32();
135 hqClipLock = 0;
140 /**************************************************************************
141 * CLIPBOARD_ResetOwner
143 * Called from DestroyWindow().
145 void CLIPBOARD_ResetOwner(WND* pWnd)
147 LPCLIPFORMAT lpFormat = ClipFormats;
149 TRACE(clipboard,"clipboard owner = %04x, selection = %08x\n",
150 hWndClipOwner, (unsigned)selectionWindow);
152 if( pWnd->hwndSelf == hWndClipOwner)
154 SendMessage16(hWndClipOwner,WM_RENDERALLFORMATS,0,0L);
156 /* check if all formats were rendered */
158 while(lpFormat)
160 if( lpFormat->wDataPresent && !lpFormat->hData )
162 TRACE(clipboard,"\tdata missing for clipboard format %i\n",
163 lpFormat->wFormatID);
164 lpFormat->wDataPresent = 0;
166 lpFormat = lpFormat->NextFormat;
168 hWndClipOwner = 0;
171 /* now try to salvage current selection from being destroyed by X */
173 if( pWnd->window ) CLIPBOARD_CheckSelection(pWnd);
176 /**************************************************************************
177 * CLIPBOARD_DeleteRecord
179 static void CLIPBOARD_DeleteRecord(LPCLIPFORMAT lpFormat, BOOL32 bChange)
181 if( (lpFormat->wFormatID >= CF_GDIOBJFIRST &&
182 lpFormat->wFormatID <= CF_GDIOBJLAST) || lpFormat->wFormatID == CF_BITMAP )
183 DeleteObject32(lpFormat->hData);
184 else if( lpFormat->wFormatID == CF_METAFILEPICT && lpFormat->hData )
186 DeleteMetaFile16( ((METAFILEPICT16 *)GlobalLock16( lpFormat->hData ))->hMF );
187 GlobalFree16(lpFormat->hData);
189 else if( lpFormat->hData )
190 GlobalFree16(lpFormat->hData);
192 lpFormat->wDataPresent = 0;
193 lpFormat->hData = 0;
195 if( bChange ) bCBHasChanged = TRUE;
198 /**************************************************************************
199 * CLIPBOARD_RequestXSelection
201 static BOOL32 CLIPBOARD_RequestXSelection()
203 HWND32 hWnd = (hWndClipWindow) ? hWndClipWindow : GetActiveWindow32();
205 if( !hWnd ) return FALSE;
207 TRACE(clipboard,"Requesting selection...\n");
209 /* request data type XA_STRING, later
210 * CLIPBOARD_ReadSelection() will be invoked
211 * from the SelectionNotify event handler */
213 TSXConvertSelection(display,XA_PRIMARY,XA_STRING,
214 TSXInternAtom(display,"PRIMARY_TEXT",False),
215 WIN_GetXWindow(hWnd),CurrentTime);
217 /* wait until SelectionNotify is processed
219 * FIXME: Use TSXCheckTypedWindowEvent() instead ( same in the
220 * CLIPBOARD_CheckSelection() ).
223 selectionWait=True;
224 while(selectionWait) EVENT_WaitNetEvent( TRUE, FALSE );
226 /* we treat Unix text as CF_OEMTEXT */
227 TRACE(clipboard,"\tgot CF_OEMTEXT = %i\n",
228 ClipFormats[CF_OEMTEXT-1].wDataPresent);
230 return (BOOL32)ClipFormats[CF_OEMTEXT-1].wDataPresent;
233 /**************************************************************************
234 * CLIPBOARD_IsPresent
236 BOOL32 CLIPBOARD_IsPresent(WORD wFormat)
238 /* special case */
240 if( wFormat == CF_TEXT || wFormat == CF_OEMTEXT )
241 return ClipFormats[CF_TEXT-1].wDataPresent ||
242 ClipFormats[CF_OEMTEXT-1].wDataPresent;
243 else
245 LPCLIPFORMAT lpFormat = __lookup_format( ClipFormats, wFormat );
246 if( lpFormat ) return (lpFormat->wDataPresent);
248 return FALSE;
251 /**************************************************************************
252 * OpenClipboard16 (USER.137)
254 BOOL16 WINAPI OpenClipboard16( HWND16 hWnd )
256 return OpenClipboard32( hWnd );
260 /**************************************************************************
261 * OpenClipboard32 (USER32.407)
263 * Note: Netscape uses NULL hWnd to open the clipboard.
265 BOOL32 WINAPI OpenClipboard32( HWND32 hWnd )
267 BOOL32 bRet;
269 TRACE(clipboard,"(%04x)...\n", hWnd);
271 if (!hqClipLock)
273 hqClipLock = GetTaskQueue(0);
274 hWndClipWindow = hWnd;
275 bCBHasChanged = FALSE;
276 bRet = TRUE;
278 else bRet = FALSE;
280 TRACE(clipboard," returning %i\n", bRet);
281 return bRet;
285 /**************************************************************************
286 * CloseClipboard16 (USER.138)
288 BOOL16 WINAPI CloseClipboard16(void)
290 return CloseClipboard32();
294 /**************************************************************************
295 * CloseClipboard32 (USER32.54)
297 BOOL32 WINAPI CloseClipboard32(void)
299 TRACE(clipboard,"!\n");
301 if (hqClipLock == GetTaskQueue(0))
303 hWndClipWindow = 0;
305 if (bCBHasChanged && hWndViewer)
306 SendMessage16(hWndViewer, WM_DRAWCLIPBOARD, 0, 0L);
307 hqClipLock = 0;
309 return TRUE;
313 /**************************************************************************
314 * EmptyClipboard16 (USER.139)
316 BOOL16 WINAPI EmptyClipboard16(void)
318 return EmptyClipboard32();
322 /**************************************************************************
323 * EmptyClipboard32 (USER32.169)
325 BOOL32 WINAPI EmptyClipboard32(void)
327 LPCLIPFORMAT lpFormat = ClipFormats;
329 TRACE(clipboard,"(void)\n");
331 if (hqClipLock != GetTaskQueue(0)) return FALSE;
333 /* destroy private objects */
335 if (hWndClipOwner)
336 SendMessage16(hWndClipOwner, WM_DESTROYCLIPBOARD, 0, 0L);
338 while(lpFormat)
340 if ( lpFormat->wDataPresent || lpFormat->hData )
341 CLIPBOARD_DeleteRecord( lpFormat, TRUE );
343 lpFormat = lpFormat->NextFormat;
346 hWndClipOwner = hWndClipWindow;
348 if(selectionAcquired)
350 selectionAcquired = False;
351 selectionPrevWindow = selectionWindow;
352 selectionWindow = None;
354 TRACE(clipboard, "\tgiving up selection (spw = %08x)\n",
355 (unsigned)selectionPrevWindow);
357 TSXSetSelectionOwner(display, XA_PRIMARY, None, CurrentTime);
359 return TRUE;
363 /**************************************************************************
364 * GetClipboardOwner16 (USER.140)
366 HWND16 WINAPI GetClipboardOwner16(void)
368 return hWndClipOwner;
372 /**************************************************************************
373 * GetClipboardOwner32 (USER32.225)
375 HWND32 WINAPI GetClipboardOwner32(void)
377 return hWndClipOwner;
381 /**************************************************************************
382 * SetClipboardData16 (USER.141)
384 HANDLE16 WINAPI SetClipboardData16( UINT16 wFormat, HANDLE16 hData )
386 LPCLIPFORMAT lpFormat = __lookup_format( ClipFormats, wFormat );
387 Window owner;
389 TRACE(clipboard, "(%04X, %04x) !\n", wFormat, hData);
391 /* NOTE: If the hData is zero and current owner doesn't match
392 * the window that opened the clipboard then this application
393 * is screwed because WM_RENDERFORMAT will go to the owner
394 * (to become the owner it must call EmptyClipboard() before
395 * adding new data).
398 if( (hqClipLock != GetTaskQueue(0)) || !lpFormat ||
399 (!hData && (!hWndClipOwner || (hWndClipOwner != hWndClipWindow))) ) return 0;
401 /* Acquire X selection if text format */
403 if( !selectionAcquired &&
404 (wFormat == CF_TEXT || wFormat == CF_OEMTEXT) )
406 owner = WIN_GetXWindow( hWndClipWindow ? hWndClipWindow : AnyPopup32() );
407 TSXSetSelectionOwner(display,XA_PRIMARY,owner,CurrentTime);
408 if( TSXGetSelectionOwner(display,XA_PRIMARY) == owner )
410 selectionAcquired = True;
411 selectionWindow = owner;
413 TRACE(clipboard,"Grabbed X selection, owner=(%08x)\n",
414 (unsigned) owner);
418 if ( lpFormat->wDataPresent || lpFormat->hData )
420 CLIPBOARD_DeleteRecord(lpFormat, TRUE);
422 /* delete existing CF_TEXT/CF_OEMTEXT aliases */
424 if( wFormat == CF_TEXT && ClipFormats[CF_OEMTEXT-1].hData
425 && !ClipFormats[CF_OEMTEXT-1].wDataPresent )
426 CLIPBOARD_DeleteRecord(&ClipFormats[CF_OEMTEXT-1], TRUE);
427 if( wFormat == CF_OEMTEXT && ClipFormats[CF_TEXT-1].hData
428 && !ClipFormats[CF_TEXT-1].wDataPresent )
429 CLIPBOARD_DeleteRecord(&ClipFormats[CF_TEXT-1], TRUE);
432 bCBHasChanged = TRUE;
433 lpFormat->wDataPresent = 1;
434 lpFormat->hData = hData; /* 0 is legal, see WM_RENDERFORMAT */
436 return lpFormat->hData;
440 /**************************************************************************
441 * SetClipboardData32 (USER32.470)
443 HANDLE32 WINAPI SetClipboardData32( UINT32 wFormat, HANDLE32 hData )
445 fprintf( stderr, "SetClipboardData: empty stub\n" );
446 return 0;
450 /**************************************************************************
451 * CLIPBOARD_RenderFormat
453 static BOOL32 CLIPBOARD_RenderFormat(LPCLIPFORMAT lpFormat)
455 if( lpFormat->wDataPresent && !lpFormat->hData )
456 if( IsWindow32(hWndClipOwner) )
457 SendMessage16(hWndClipOwner,WM_RENDERFORMAT,
458 (WPARAM16)lpFormat->wFormatID,0L);
459 else
461 WARN(clipboard, "\thWndClipOwner (%04x) is lost!\n",
462 hWndClipOwner);
463 hWndClipOwner = 0; lpFormat->wDataPresent = 0;
464 return FALSE;
466 return (lpFormat->hData) ? TRUE : FALSE;
469 /**************************************************************************
470 * CLIPBOARD_RenderText
472 * Convert text between UNIX and DOS formats.
474 static BOOL32 CLIPBOARD_RenderText(LPCLIPFORMAT lpTarget, LPCLIPFORMAT lpSource)
476 UINT16 size = GlobalSize16( lpSource->hData );
477 LPCSTR lpstrS = (LPSTR)GlobalLock16(lpSource->hData);
478 LPSTR lpstrT;
480 if( !lpstrS ) return FALSE;
481 TRACE(clipboard,"\tconverting from '%s' to '%s', %i chars\n",
482 lpSource->Name, lpTarget->Name, size);
484 lpTarget->hData = GlobalAlloc16(GMEM_ZEROINIT, size);
485 lpstrT = (LPSTR)GlobalLock16(lpTarget->hData);
487 if( lpstrT )
489 if( lpSource->wFormatID == CF_TEXT )
490 CharToOemBuff32A(lpstrS, lpstrT, size);
491 else
492 OemToCharBuff32A(lpstrS, lpstrT, size);
493 TRACE(clipboard,"\tgot %s\n", lpstrT);
494 return TRUE;
497 lpTarget->hData = 0;
498 return FALSE;
501 /**************************************************************************
502 * GetClipboardData16 (USER.142)
504 HANDLE16 WINAPI GetClipboardData16( UINT16 wFormat )
506 LPCLIPFORMAT lpRender = ClipFormats;
507 LPCLIPFORMAT lpUpdate = NULL;
509 if (hqClipLock != GetTaskQueue(0)) return 0;
511 TRACE(clipboard,"(%04X)\n", wFormat);
513 if( wFormat == CF_TEXT && !lpRender[CF_TEXT-1].wDataPresent
514 && lpRender[CF_OEMTEXT-1].wDataPresent )
516 lpRender = &ClipFormats[CF_OEMTEXT-1];
517 lpUpdate = &ClipFormats[CF_TEXT-1];
519 TRACE(clipboard,"\tOEMTEXT -> TEXT\n");
521 else if( wFormat == CF_OEMTEXT && !lpRender[CF_OEMTEXT-1].wDataPresent
522 && lpRender[CF_TEXT-1].wDataPresent )
524 lpRender = &ClipFormats[CF_TEXT-1];
525 lpUpdate = &ClipFormats[CF_OEMTEXT-1];
527 TRACE(clipboard,"\tTEXT -> OEMTEXT\n");
529 else
531 lpRender = __lookup_format( ClipFormats, wFormat );
532 lpUpdate = lpRender;
535 if( !lpRender || !CLIPBOARD_RenderFormat(lpRender) ) return 0;
536 if( lpUpdate != lpRender && !lpUpdate->hData )
537 CLIPBOARD_RenderText(lpUpdate, lpRender);
539 TRACE(clipboard,"\treturning %04x (type %i)\n",
540 lpUpdate->hData, lpUpdate->wFormatID);
541 return lpUpdate->hData;
545 /**************************************************************************
546 * GetClipboardData32 (USER32.222)
548 HANDLE32 WINAPI GetClipboardData32( UINT32 wFormat )
550 fprintf( stderr, "GetClipboardData32: empty stub\n" );
551 return 0;
554 /**************************************************************************
555 * CountClipboardFormats16 (USER.143)
557 INT16 WINAPI CountClipboardFormats16(void)
559 return CountClipboardFormats32();
563 /**************************************************************************
564 * CountClipboardFormats32 (USER32.63)
566 INT32 WINAPI CountClipboardFormats32(void)
568 INT32 FormatCount = 0;
569 LPCLIPFORMAT lpFormat = ClipFormats;
571 TRACE(clipboard,"(void)\n");
573 if( !selectionAcquired ) CLIPBOARD_RequestXSelection();
575 FormatCount += abs(lpFormat[CF_TEXT-1].wDataPresent -
576 lpFormat[CF_OEMTEXT-1].wDataPresent);
578 while(TRUE)
580 if (lpFormat == NULL) break;
581 if (lpFormat->wDataPresent)
583 TRACE(clipboard, "\tdata found for format %i\n", lpFormat->wFormatID);
584 FormatCount++;
586 lpFormat = lpFormat->NextFormat;
589 TRACE(clipboard,"\ttotal %d\n", FormatCount);
590 return FormatCount;
594 /**************************************************************************
595 * EnumClipboardFormats16 (USER.144)
597 UINT16 WINAPI EnumClipboardFormats16( UINT16 wFormat )
599 return EnumClipboardFormats32( wFormat );
603 /**************************************************************************
604 * EnumClipboardFormats32 (USER32.179)
606 UINT32 WINAPI EnumClipboardFormats32( UINT32 wFormat )
608 LPCLIPFORMAT lpFormat = ClipFormats;
610 TRACE(clipboard,"(%04X)\n", wFormat);
612 if( hqClipLock != GetTaskQueue(0) ) return 0;
614 if( (!wFormat || wFormat == CF_TEXT || wFormat == CF_OEMTEXT)
615 && !selectionAcquired) CLIPBOARD_RequestXSelection();
617 if (wFormat == 0)
618 if (lpFormat->wDataPresent || ClipFormats[CF_OEMTEXT-1].wDataPresent)
619 return lpFormat->wFormatID;
620 else
621 wFormat = lpFormat->wFormatID; /* and CF_TEXT is not available */
623 /* walk up to the specified format record */
625 if( !(lpFormat = __lookup_format( lpFormat, wFormat )) ) return 0;
627 /* find next format with available data */
629 lpFormat = lpFormat->NextFormat;
630 while(TRUE)
632 if (lpFormat == NULL) return 0;
633 if (lpFormat->wDataPresent || (lpFormat->wFormatID == CF_OEMTEXT &&
634 ClipFormats[CF_TEXT-1].wDataPresent))
635 break;
636 lpFormat = lpFormat->NextFormat;
639 return lpFormat->wFormatID;
643 /**************************************************************************
644 * RegisterClipboardFormat16 (USER.145)
646 UINT16 WINAPI RegisterClipboardFormat16( LPCSTR FormatName )
648 LPCLIPFORMAT lpNewFormat;
649 LPCLIPFORMAT lpFormat = ClipFormats;
651 if (FormatName == NULL) return 0;
653 TRACE(clipboard,"('%s') !\n", FormatName);
655 /* walk format chain to see if it's already registered */
657 while(TRUE)
659 if ( !strcmp(lpFormat->Name,FormatName) )
661 lpFormat->wRefCount++;
662 return lpFormat->wFormatID;
665 if ( lpFormat->NextFormat == NULL ) break;
667 lpFormat = lpFormat->NextFormat;
670 /* allocate storage for new format entry */
672 lpNewFormat = (LPCLIPFORMAT)xmalloc(sizeof(CLIPFORMAT));
673 lpFormat->NextFormat = lpNewFormat;
674 lpNewFormat->wFormatID = LastRegFormat;
675 lpNewFormat->wRefCount = 1;
677 lpNewFormat->Name = (LPSTR)xmalloc(strlen(FormatName) + 1);
678 strcpy(lpNewFormat->Name, FormatName);
680 lpNewFormat->wDataPresent = 0;
681 lpNewFormat->hData = 0;
682 lpNewFormat->BufSize = 0;
683 lpNewFormat->PrevFormat = lpFormat;
684 lpNewFormat->NextFormat = NULL;
686 return LastRegFormat++;
690 /**************************************************************************
691 * RegisterClipboardFormat32A (USER32.431)
693 UINT32 WINAPI RegisterClipboardFormat32A( LPCSTR formatName )
695 return RegisterClipboardFormat16( formatName );
699 /**************************************************************************
700 * RegisterClipboardFormat32W (USER32.432)
702 UINT32 WINAPI RegisterClipboardFormat32W( LPCWSTR formatName )
704 LPSTR aFormat = HEAP_strdupWtoA( GetProcessHeap(), 0, formatName );
705 UINT32 ret = RegisterClipboardFormat32A( aFormat );
706 HeapFree( GetProcessHeap(), 0, aFormat );
707 return ret;
710 /**************************************************************************
711 * GetClipboardFormatName16 (USER.146)
713 INT16 WINAPI GetClipboardFormatName16( UINT16 wFormat, LPSTR retStr, INT16 maxlen )
715 return GetClipboardFormatName32A( wFormat, retStr, maxlen );
719 /**************************************************************************
720 * GetClipboardFormatName32A (USER32.223)
722 INT32 WINAPI GetClipboardFormatName32A( UINT32 wFormat, LPSTR retStr, INT32 maxlen )
724 LPCLIPFORMAT lpFormat = __lookup_format( ClipFormats, wFormat );
726 TRACE(clipboard, "(%04X, %p, %d) !\n", wFormat, retStr, maxlen);
728 if (lpFormat == NULL || lpFormat->Name == NULL ||
729 lpFormat->wFormatID < CF_REGFORMATBASE) return 0;
731 TRACE(clipboard, "Name='%s' !\n", lpFormat->Name);
733 lstrcpyn32A( retStr, lpFormat->Name, maxlen );
734 return strlen(retStr);
738 /**************************************************************************
739 * GetClipboardFormatName32W (USER32.224)
741 INT32 WINAPI GetClipboardFormatName32W( UINT32 wFormat, LPWSTR retStr, INT32 maxlen )
743 LPSTR p = HEAP_xalloc( GetProcessHeap(), 0, maxlen );
744 INT32 ret = GetClipboardFormatName32A( wFormat, p, maxlen );
745 lstrcpynAtoW( retStr, p, maxlen );
746 HeapFree( GetProcessHeap(), 0, p );
747 return ret;
751 /**************************************************************************
752 * SetClipboardViewer16 (USER.147)
754 HWND16 WINAPI SetClipboardViewer16( HWND16 hWnd )
756 return SetClipboardViewer32( hWnd );
760 /**************************************************************************
761 * SetClipboardViewer32 (USER32.471)
763 HWND32 WINAPI SetClipboardViewer32( HWND32 hWnd )
765 HWND32 hwndPrev = hWndViewer;
767 TRACE(clipboard,"(%04x): returning %04x\n", hWnd, hwndPrev);
769 hWndViewer = hWnd;
770 return hwndPrev;
774 /**************************************************************************
775 * GetClipboardViewer16 (USER.148)
777 HWND16 WINAPI GetClipboardViewer16(void)
779 return hWndViewer;
783 /**************************************************************************
784 * GetClipboardViewer32 (USER32.226)
786 HWND32 WINAPI GetClipboardViewer32(void)
788 return hWndViewer;
792 /**************************************************************************
793 * ChangeClipboardChain16 (USER.149)
795 BOOL16 WINAPI ChangeClipboardChain16(HWND16 hWnd, HWND16 hWndNext)
797 return ChangeClipboardChain32(hWnd, hWndNext);
800 /**************************************************************************
801 * ChangeClipboardChain32 (USER32.22)
803 BOOL32 WINAPI ChangeClipboardChain32(HWND32 hWnd, HWND32 hWndNext)
805 BOOL32 bRet = 0;
807 FIXME(clipboard, "(%04x, %04x) - stub?\n",
808 hWnd, hWndNext);
810 if( hWndViewer )
811 bRet = !SendMessage16( hWndViewer, WM_CHANGECBCHAIN,
812 (WPARAM16)hWnd, (LPARAM)hWndNext);
813 else
814 WARN(clipboard, "hWndViewer is lost\n");
816 if( hWnd == hWndViewer ) hWndViewer = hWndNext;
818 return bRet;
823 /**************************************************************************
824 * IsClipboardFormatAvailable16 (USER.193)
826 BOOL16 WINAPI IsClipboardFormatAvailable16( UINT16 wFormat )
828 return IsClipboardFormatAvailable32( wFormat );
832 /**************************************************************************
833 * IsClipboardFormatAvailable32 (USER32.340)
835 BOOL32 WINAPI IsClipboardFormatAvailable32( UINT32 wFormat )
837 TRACE(clipboard,"(%04X) !\n", wFormat);
839 if( (wFormat == CF_TEXT || wFormat == CF_OEMTEXT) &&
840 !selectionAcquired ) CLIPBOARD_RequestXSelection();
842 return CLIPBOARD_IsPresent(wFormat);
846 /**************************************************************************
847 * GetOpenClipboardWindow16 (USER.248)
849 HWND16 WINAPI GetOpenClipboardWindow16(void)
851 return hWndClipWindow;
855 /**************************************************************************
856 * GetOpenClipboardWindow32 (USER32.277)
858 HWND32 WINAPI GetOpenClipboardWindow32(void)
860 return hWndClipWindow;
864 /**************************************************************************
865 * GetPriorityClipboardFormat16 (USER.402)
867 INT16 WINAPI GetPriorityClipboardFormat16( UINT16 *lpPriorityList, INT16 nCount)
869 fprintf( stderr, "GetPriorityClipboardFormat16(%p, %d): stub\n",
870 lpPriorityList, nCount );
871 return 0;
875 /**************************************************************************
876 * GetPriorityClipboardFormat32 (USER32.279)
878 INT32 WINAPI GetPriorityClipboardFormat32( UINT32 *lpPriorityList, INT32 nCount )
880 int Counter;
882 if(CountClipboardFormats32() == 0)
884 return 0;
887 for(Counter = 0; Counter <= nCount; Counter++)
889 if(IsClipboardFormatAvailable32(*(lpPriorityList+sizeof(INT32)*Counter)))
890 return *(lpPriorityList+sizeof(INT32)*Counter);
893 return -1;
897 /**************************************************************************
898 * CLIPBOARD_ReadSelection
900 * Called from the SelectionNotify event handler.
902 void CLIPBOARD_ReadSelection(Window w,Atom prop)
904 HANDLE16 hText = 0;
905 LPCLIPFORMAT lpFormat = ClipFormats;
907 TRACE(clipboard,"ReadSelection callback\n");
909 if(prop != None)
911 Atom atype=AnyPropertyType;
912 int aformat;
913 unsigned long nitems,remain;
914 unsigned char* val=NULL;
916 TRACE(clipboard,"\tgot property %s\n",TSXGetAtomName(display,prop));
918 /* TODO: Properties longer than 64K */
920 if(TSXGetWindowProperty(display,w,prop,0,0x3FFF,True,XA_STRING,
921 &atype, &aformat, &nitems, &remain, &val) != Success)
922 WARN(clipboard, "\tcouldn't read property\n");
923 else
925 TRACE(clipboard,"\tType %s,Format %d,nitems %ld,value %s\n",
926 TSXGetAtomName(display,atype),aformat,nitems,val);
928 if(atype == XA_STRING && aformat == 8)
930 int i,inlcount = 0;
931 char* lpstr;
933 TRACE(clipboard,"\tselection is '%s'\n",val);
935 for(i=0; i <= nitems; i++)
936 if( val[i] == '\n' ) inlcount++;
938 if( nitems )
940 hText=GlobalAlloc16(GMEM_MOVEABLE, nitems + inlcount + 1);
941 if( (lpstr = (char*)GlobalLock16(hText)) )
942 for(i=0,inlcount=0; i <= nitems; i++)
944 if( val[i] == '\n' ) lpstr[inlcount++]='\r';
945 lpstr[inlcount++]=val[i];
947 else hText = 0;
950 TSXFree(val);
954 /* delete previous CF_TEXT and CF_OEMTEXT data */
956 if( hText )
958 lpFormat = &ClipFormats[CF_TEXT-1];
959 if (lpFormat->wDataPresent || lpFormat->hData)
960 CLIPBOARD_DeleteRecord(lpFormat, !(hWndClipWindow));
961 lpFormat = &ClipFormats[CF_OEMTEXT-1];
962 if (lpFormat->wDataPresent || lpFormat->hData)
963 CLIPBOARD_DeleteRecord(lpFormat, !(hWndClipWindow));
965 lpFormat->wDataPresent = 1;
966 lpFormat->hData = hText;
969 selectionWait=False;
972 /**************************************************************************
973 * CLIPBOARD_ReleaseSelection
975 * Wine might have lost XA_PRIMARY selection because of
976 * EmptyClipboard() or other client.
978 void CLIPBOARD_ReleaseSelection(Window w, HWND32 hwnd)
980 /* w is the window that lost selection,
982 * selectionPrevWindow is nonzero if CheckSelection() was called.
985 TRACE(clipboard,"\tevent->window = %08x (sw = %08x, spw=%08x)\n",
986 (unsigned)w, (unsigned)selectionWindow, (unsigned)selectionPrevWindow );
988 if( selectionAcquired )
989 if( w == selectionWindow || selectionPrevWindow == None)
991 /* alright, we really lost it */
993 selectionAcquired = False;
994 selectionWindow = None;
996 /* but we'll keep existing data for internal use */
998 else if( w == selectionPrevWindow )
1000 w = TSXGetSelectionOwner(display, XA_PRIMARY);
1001 if( w == None )
1002 TSXSetSelectionOwner(display, XA_PRIMARY, selectionWindow, CurrentTime);
1005 selectionPrevWindow = None;