Release 980201
[wine/multimedia.git] / windows / clipboard.c
blobce992049da9c97092dacbb93e56b2aafe3d5720c
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 "stddebug.h"
25 #include "debug.h"
27 #define CF_REGFORMATBASE 0xC000
29 typedef struct tagCLIPFORMAT {
30 WORD wFormatID;
31 WORD wRefCount;
32 WORD wDataPresent;
33 LPSTR Name;
34 HANDLE16 hData;
35 DWORD BufSize;
36 struct tagCLIPFORMAT *PrevFormat;
37 struct tagCLIPFORMAT *NextFormat;
38 } CLIPFORMAT, *LPCLIPFORMAT;
40 /* *************************************************************************
41 * internal variables
44 static HQUEUE16 hqClipLock = 0;
45 static BOOL32 bCBHasChanged = FALSE;
47 static HWND32 hWndClipOwner = 0; /* current clipboard owner */
48 static HWND32 hWndClipWindow = 0; /* window that opened clipboard */
49 static HWND32 hWndViewer = 0; /* start of viewers chain */
51 static WORD LastRegFormat = CF_REGFORMATBASE;
53 static Bool selectionWait = False;
54 static Bool selectionAcquired = False;
55 static Window selectionWindow = None;
56 static Window selectionPrevWindow = None;
58 static CLIPFORMAT ClipFormats[16] = {
59 { CF_TEXT, 1, 0, "Text", (HANDLE16)NULL, 0, NULL, &ClipFormats[1] },
60 { CF_BITMAP, 1, 0, "Bitmap", (HANDLE16)NULL, 0, &ClipFormats[0], &ClipFormats[2] },
61 { CF_METAFILEPICT, 1, 0, "MetaFile Picture", (HANDLE16)NULL, 0, &ClipFormats[1], &ClipFormats[3] },
62 { CF_SYLK, 1, 0, "Sylk", (HANDLE16)NULL, 0, &ClipFormats[2], &ClipFormats[4] },
63 { CF_DIF, 1, 0, "DIF", (HANDLE16)NULL, 0, &ClipFormats[3], &ClipFormats[5] },
64 { CF_TIFF, 1, 0, "TIFF", (HANDLE16)NULL, 0, &ClipFormats[4], &ClipFormats[6] },
65 { CF_OEMTEXT, 1, 0, "OEM Text", (HANDLE16)NULL, 0, &ClipFormats[5], &ClipFormats[7] },
66 { CF_DIB, 1, 0, "DIB", (HANDLE16)NULL, 0, &ClipFormats[6], &ClipFormats[8] },
67 { CF_PALETTE, 1, 0, "Palette", (HANDLE16)NULL, 0, &ClipFormats[7], &ClipFormats[9] },
68 { CF_PENDATA, 1, 0, "PenData", (HANDLE16)NULL, 0, &ClipFormats[8], &ClipFormats[10] },
69 { CF_RIFF, 1, 0, "RIFF", (HANDLE16)NULL, 0, &ClipFormats[9], &ClipFormats[11] },
70 { CF_WAVE, 1, 0, "Wave", (HANDLE16)NULL, 0, &ClipFormats[10], &ClipFormats[12] },
71 { CF_OWNERDISPLAY, 1, 0, "Owner Display", (HANDLE16)NULL, 0, &ClipFormats[11], &ClipFormats[13] },
72 { CF_DSPTEXT, 1, 0, "DSPText", (HANDLE16)NULL, 0, &ClipFormats[12], &ClipFormats[14] },
73 { CF_DSPMETAFILEPICT, 1, 0, "DSPMetaFile Picture", (HANDLE16)NULL, 0, &ClipFormats[13], &ClipFormats[15] },
74 { CF_DSPBITMAP, 1, 0, "DSPBitmap", (HANDLE16)NULL, 0, &ClipFormats[14], NULL }
77 static LPCLIPFORMAT __lookup_format( LPCLIPFORMAT lpFormat, WORD wID )
79 while(TRUE)
81 if (lpFormat == NULL ||
82 lpFormat->wFormatID == wID) break;
83 lpFormat = lpFormat->NextFormat;
85 return lpFormat;
88 /**************************************************************************
89 * CLIPBOARD_CheckSelection
91 * Prevent X selection from being lost when a top level window is
92 * destroyed.
94 static void CLIPBOARD_CheckSelection(WND* pWnd)
96 dprintf_clipboard(stddeb,"\tchecking %08x\n", (unsigned)pWnd->window);
98 if( selectionAcquired && selectionWindow != None &&
99 pWnd->window == selectionWindow )
101 selectionPrevWindow = selectionWindow;
102 selectionWindow = None;
104 if( pWnd->next )
105 selectionWindow = pWnd->next->window;
106 else if( pWnd->parent )
107 if( pWnd->parent->child != pWnd )
108 selectionWindow = pWnd->parent->child->window;
110 dprintf_clipboard(stddeb,"\tswitching selection from %08x to %08x\n",
111 (unsigned)selectionPrevWindow, (unsigned)selectionWindow);
113 if( selectionWindow != None )
115 TSXSetSelectionOwner(display, XA_PRIMARY, selectionWindow, CurrentTime);
116 if( TSXGetSelectionOwner(display, XA_PRIMARY) != selectionWindow )
117 selectionWindow = None;
122 /**************************************************************************
123 * CLIPBOARD_ResetLock
125 void CLIPBOARD_ResetLock( HQUEUE16 hqCurrent, HQUEUE16 hqNew )
127 if( hqClipLock == hqCurrent )
129 if( hqNew )
130 hqClipLock = hqNew;
131 else
133 hWndClipOwner = 0;
134 hWndClipWindow = 0;
135 EmptyClipboard32();
136 hqClipLock = 0;
141 /**************************************************************************
142 * CLIPBOARD_ResetOwner
144 * Called from DestroyWindow().
146 void CLIPBOARD_ResetOwner(WND* pWnd)
148 LPCLIPFORMAT lpFormat = ClipFormats;
150 dprintf_clipboard(stddeb,"DisOwn: clipboard owner = %04x, selection = %08x\n",
151 hWndClipOwner, (unsigned)selectionWindow);
153 if( pWnd->hwndSelf == hWndClipOwner)
155 SendMessage16(hWndClipOwner,WM_RENDERALLFORMATS,0,0L);
157 /* check if all formats were rendered */
159 while(lpFormat)
161 if( lpFormat->wDataPresent && !lpFormat->hData )
163 dprintf_clipboard( stddeb,"\tdata missing for clipboard format %i\n",
164 lpFormat->wFormatID);
165 lpFormat->wDataPresent = 0;
167 lpFormat = lpFormat->NextFormat;
169 hWndClipOwner = 0;
172 /* now try to salvage current selection from being destroyed by X */
174 if( pWnd->window ) CLIPBOARD_CheckSelection(pWnd);
177 /**************************************************************************
178 * CLIPBOARD_DeleteRecord
180 static void CLIPBOARD_DeleteRecord(LPCLIPFORMAT lpFormat, BOOL32 bChange)
182 if( lpFormat->wFormatID >= CF_GDIOBJFIRST &&
183 lpFormat->wFormatID <= CF_GDIOBJLAST )
184 DeleteObject32(lpFormat->hData);
185 else if( lpFormat->hData )
186 GlobalFree16(lpFormat->hData);
188 lpFormat->wDataPresent = 0;
189 lpFormat->hData = 0;
191 if( bChange ) bCBHasChanged = TRUE;
194 /**************************************************************************
195 * CLIPBOARD_RequestXSelection
197 static BOOL32 CLIPBOARD_RequestXSelection()
199 HWND32 hWnd = (hWndClipWindow) ? hWndClipWindow : GetActiveWindow32();
201 if( !hWnd ) return FALSE;
203 dprintf_clipboard(stddeb,"Requesting selection...\n");
205 /* request data type XA_STRING, later
206 * CLIPBOARD_ReadSelection() will be invoked
207 * from the SelectionNotify event handler */
209 TSXConvertSelection(display,XA_PRIMARY,XA_STRING,
210 TSXInternAtom(display,"PRIMARY_TEXT",False),
211 WIN_GetXWindow(hWnd),CurrentTime);
213 /* wait until SelectionNotify is processed
215 * FIXME: Use TSXCheckTypedWindowEvent() instead ( same in the
216 * CLIPBOARD_CheckSelection() ).
219 selectionWait=True;
220 while(selectionWait) EVENT_WaitNetEvent( TRUE, FALSE );
222 /* we treat Unix text as CF_OEMTEXT */
223 dprintf_clipboard(stddeb,"\tgot CF_OEMTEXT = %i\n",
224 ClipFormats[CF_OEMTEXT-1].wDataPresent);
226 return (BOOL32)ClipFormats[CF_OEMTEXT-1].wDataPresent;
229 /**************************************************************************
230 * CLIPBOARD_IsPresent
232 BOOL32 CLIPBOARD_IsPresent(WORD wFormat)
234 /* special case */
236 if( wFormat == CF_TEXT || wFormat == CF_OEMTEXT )
237 return ClipFormats[CF_TEXT-1].wDataPresent ||
238 ClipFormats[CF_OEMTEXT-1].wDataPresent;
239 else
241 LPCLIPFORMAT lpFormat = __lookup_format( ClipFormats, wFormat );
242 if( lpFormat ) return (lpFormat->wDataPresent);
244 return FALSE;
247 /**************************************************************************
248 * OpenClipboard16 (USER.137)
250 BOOL16 WINAPI OpenClipboard16( HWND16 hWnd )
252 return OpenClipboard32( hWnd );
256 /**************************************************************************
257 * OpenClipboard32 (USER32.406)
259 * Note: Netscape uses NULL hWnd to open the clipboard.
261 BOOL32 WINAPI OpenClipboard32( HWND32 hWnd )
263 BOOL32 bRet;
265 dprintf_clipboard(stddeb,"OpenClipboard(%04x) = ", hWnd);
267 if (!hqClipLock)
269 hqClipLock = GetTaskQueue(0);
270 hWndClipWindow = hWnd;
271 bCBHasChanged = FALSE;
272 bRet = TRUE;
274 else bRet = FALSE;
276 dprintf_clipboard(stddeb,"%i\n", bRet);
277 return bRet;
281 /**************************************************************************
282 * CloseClipboard16 (USER.138)
284 BOOL16 WINAPI CloseClipboard16(void)
286 return CloseClipboard32();
290 /**************************************************************************
291 * CloseClipboard32 (USER32.53)
293 BOOL32 WINAPI CloseClipboard32(void)
295 dprintf_clipboard(stddeb,"CloseClipboard(); !\n");
297 if (hqClipLock == GetTaskQueue(0))
299 hWndClipWindow = 0;
301 if (bCBHasChanged && hWndViewer)
302 SendMessage16(hWndViewer, WM_DRAWCLIPBOARD, 0, 0L);
303 hqClipLock = 0;
305 return TRUE;
309 /**************************************************************************
310 * EmptyClipboard16 (USER.139)
312 BOOL16 WINAPI EmptyClipboard16(void)
314 return EmptyClipboard32();
318 /**************************************************************************
319 * EmptyClipboard32 (USER32.168)
321 BOOL32 WINAPI EmptyClipboard32(void)
323 LPCLIPFORMAT lpFormat = ClipFormats;
325 dprintf_clipboard(stddeb,"EmptyClipboard()\n");
327 if (hqClipLock != GetTaskQueue(0)) return FALSE;
329 /* destroy private objects */
331 if (hWndClipOwner)
332 SendMessage16(hWndClipOwner, WM_DESTROYCLIPBOARD, 0, 0L);
334 while(lpFormat)
336 if ( lpFormat->wDataPresent || lpFormat->hData )
337 CLIPBOARD_DeleteRecord( lpFormat, TRUE );
339 lpFormat = lpFormat->NextFormat;
342 hWndClipOwner = hWndClipWindow;
344 if(selectionAcquired)
346 selectionAcquired = False;
347 selectionPrevWindow = selectionWindow;
348 selectionWindow = None;
350 dprintf_clipboard(stddeb, "\tgiving up selection (spw = %08x)\n",
351 (unsigned)selectionPrevWindow);
353 TSXSetSelectionOwner(display, XA_PRIMARY, None, CurrentTime);
355 return TRUE;
359 /**************************************************************************
360 * GetClipboardOwner16 (USER.140)
362 HWND16 WINAPI GetClipboardOwner16(void)
364 return hWndClipOwner;
368 /**************************************************************************
369 * GetClipboardOwner32 (USER32.224)
371 HWND32 WINAPI GetClipboardOwner32(void)
373 return hWndClipOwner;
377 /**************************************************************************
378 * SetClipboardData16 (USER.141)
380 HANDLE16 WINAPI SetClipboardData16( UINT16 wFormat, HANDLE16 hData )
382 LPCLIPFORMAT lpFormat = __lookup_format( ClipFormats, wFormat );
383 Window owner;
385 dprintf_clipboard(stddeb,
386 "SetClipboardData(%04X, %04x) !\n", wFormat, hData);
388 /* NOTE: If the hData is zero and current owner doesn't match
389 * the window that opened the clipboard then this application
390 * is screwed because WM_RENDERFORMAT will go to the owner
391 * (to become the owner it must call EmptyClipboard() before
392 * adding new data).
395 if( (hqClipLock != GetTaskQueue(0)) || !lpFormat ||
396 (!hData && (!hWndClipOwner || (hWndClipOwner != hWndClipWindow))) ) return 0;
398 /* Acquire X selection if text format */
400 if( !selectionAcquired &&
401 (wFormat == CF_TEXT || wFormat == CF_OEMTEXT) )
403 owner = WIN_GetXWindow( hWndClipWindow ? hWndClipWindow : AnyPopup32() );
404 TSXSetSelectionOwner(display,XA_PRIMARY,owner,CurrentTime);
405 if( TSXGetSelectionOwner(display,XA_PRIMARY) == owner )
407 selectionAcquired = True;
408 selectionWindow = owner;
410 dprintf_clipboard(stddeb,"Grabbed X selection, owner=(%08x)\n",
411 (unsigned) owner);
415 if ( lpFormat->wDataPresent || lpFormat->hData )
417 CLIPBOARD_DeleteRecord(lpFormat, TRUE);
419 /* delete existing CF_TEXT/CF_OEMTEXT aliases */
421 if( wFormat == CF_TEXT && ClipFormats[CF_OEMTEXT-1].hData
422 && !ClipFormats[CF_OEMTEXT-1].wDataPresent )
423 CLIPBOARD_DeleteRecord(&ClipFormats[CF_OEMTEXT-1], TRUE);
424 if( wFormat == CF_OEMTEXT && ClipFormats[CF_TEXT-1].hData
425 && !ClipFormats[CF_TEXT-1].wDataPresent )
426 CLIPBOARD_DeleteRecord(&ClipFormats[CF_TEXT-1], TRUE);
429 bCBHasChanged = TRUE;
430 lpFormat->wDataPresent = 1;
431 lpFormat->hData = hData; /* 0 is legal, see WM_RENDERFORMAT */
433 return lpFormat->hData;
437 /**************************************************************************
438 * SetClipboardData32 (USER32.469)
440 HANDLE32 WINAPI SetClipboardData32( UINT32 wFormat, HANDLE32 hData )
442 fprintf( stderr, "SetClipboardData: empty stub\n" );
443 return 0;
447 /**************************************************************************
448 * CLIPBOARD_RenderFormat
450 static BOOL32 CLIPBOARD_RenderFormat(LPCLIPFORMAT lpFormat)
452 if( lpFormat->wDataPresent && !lpFormat->hData )
453 if( IsWindow32(hWndClipOwner) )
454 SendMessage16(hWndClipOwner,WM_RENDERFORMAT,
455 (WPARAM16)lpFormat->wFormatID,0L);
456 else
458 dprintf_clipboard(stddeb,"\thWndClipOwner (%04x) is lost!\n",
459 hWndClipOwner);
460 hWndClipOwner = 0; lpFormat->wDataPresent = 0;
461 return FALSE;
463 return (lpFormat->hData) ? TRUE : FALSE;
466 /**************************************************************************
467 * CLIPBOARD_RenderText
469 * Convert text between UNIX and DOS formats.
471 static BOOL32 CLIPBOARD_RenderText(LPCLIPFORMAT lpTarget, LPCLIPFORMAT lpSource)
473 UINT16 size = GlobalSize16( lpSource->hData );
474 LPCSTR lpstrS = (LPSTR)GlobalLock16(lpSource->hData);
475 LPSTR lpstrT;
477 if( !lpstrS ) return FALSE;
478 dprintf_clipboard(stddeb,"\tconverting from '%s' to '%s', %i chars\n",
479 lpSource->Name, lpTarget->Name, size);
481 lpTarget->hData = GlobalAlloc16(GMEM_ZEROINIT, size);
482 lpstrT = (LPSTR)GlobalLock16(lpTarget->hData);
484 if( lpstrT )
486 if( lpSource->wFormatID == CF_TEXT )
487 CharToOemBuff32A(lpstrS, lpstrT, size);
488 else
489 OemToCharBuff32A(lpstrS, lpstrT, size);
490 dprintf_clipboard(stddeb,"\tgot %s\n", lpstrT);
491 return TRUE;
494 lpTarget->hData = 0;
495 return FALSE;
498 /**************************************************************************
499 * GetClipboardData16 (USER.142)
501 HANDLE16 WINAPI GetClipboardData16( UINT16 wFormat )
503 LPCLIPFORMAT lpRender = ClipFormats;
504 LPCLIPFORMAT lpUpdate = NULL;
506 if (hqClipLock != GetTaskQueue(0)) return 0;
508 dprintf_clipboard(stddeb,"GetClipboardData(%04X)\n", wFormat);
510 if( wFormat == CF_TEXT && !lpRender[CF_TEXT-1].wDataPresent
511 && lpRender[CF_OEMTEXT-1].wDataPresent )
513 lpRender = &ClipFormats[CF_OEMTEXT-1];
514 lpUpdate = &ClipFormats[CF_TEXT-1];
516 dprintf_clipboard(stddeb,"\tOEMTEXT -> TEXT\n");
518 else if( wFormat == CF_OEMTEXT && !lpRender[CF_OEMTEXT-1].wDataPresent
519 && lpRender[CF_TEXT-1].wDataPresent )
521 lpRender = &ClipFormats[CF_TEXT-1];
522 lpUpdate = &ClipFormats[CF_OEMTEXT-1];
524 dprintf_clipboard(stddeb,"\tTEXT -> OEMTEXT\n");
526 else
528 lpRender = __lookup_format( ClipFormats, wFormat );
529 lpUpdate = lpRender;
532 if( !lpRender || !CLIPBOARD_RenderFormat(lpRender) ) return 0;
533 if( lpUpdate != lpRender && !lpUpdate->hData )
534 CLIPBOARD_RenderText(lpUpdate, lpRender);
536 dprintf_clipboard(stddeb,"\treturning %04x (type %i)\n",
537 lpUpdate->hData, lpUpdate->wFormatID);
538 return lpUpdate->hData;
542 /**************************************************************************
543 * GetClipboardData32 (USER32.221)
545 HANDLE32 WINAPI GetClipboardData32( UINT32 wFormat )
547 fprintf( stderr, "GetClipboardData32: empty stub\n" );
548 return 0;
551 /**************************************************************************
552 * CountClipboardFormats16 (USER.143)
554 INT16 WINAPI CountClipboardFormats16(void)
556 return CountClipboardFormats32();
560 /**************************************************************************
561 * CountClipboardFormats32 (USER32.62)
563 INT32 WINAPI CountClipboardFormats32(void)
565 INT32 FormatCount = 0;
566 LPCLIPFORMAT lpFormat = ClipFormats;
568 dprintf_clipboard(stddeb,"CountClipboardFormats()\n");
570 if( !selectionAcquired ) CLIPBOARD_RequestXSelection();
572 FormatCount += abs(lpFormat[CF_TEXT-1].wDataPresent -
573 lpFormat[CF_OEMTEXT-1].wDataPresent);
575 while(TRUE)
577 if (lpFormat == NULL) break;
578 if (lpFormat->wDataPresent)
580 dprintf_clipboard(stddeb, "\tdata found for format %i\n", lpFormat->wFormatID);
581 FormatCount++;
583 lpFormat = lpFormat->NextFormat;
586 dprintf_clipboard(stddeb,"\ttotal %d\n", FormatCount);
587 return FormatCount;
591 /**************************************************************************
592 * EnumClipboardFormats16 (USER.144)
594 UINT16 WINAPI EnumClipboardFormats16( UINT16 wFormat )
596 return EnumClipboardFormats32( wFormat );
600 /**************************************************************************
601 * EnumClipboardFormats32 (USER32.178)
603 UINT32 WINAPI EnumClipboardFormats32( UINT32 wFormat )
605 LPCLIPFORMAT lpFormat = ClipFormats;
607 dprintf_clipboard(stddeb,"EnumClipboardFormats(%04X)\n", wFormat);
609 if( hqClipLock != GetTaskQueue(0) ) return 0;
611 if( (!wFormat || wFormat == CF_TEXT || wFormat == CF_OEMTEXT)
612 && !selectionAcquired) CLIPBOARD_RequestXSelection();
614 if (wFormat == 0)
615 if (lpFormat->wDataPresent || ClipFormats[CF_OEMTEXT-1].wDataPresent)
616 return lpFormat->wFormatID;
617 else
618 wFormat = lpFormat->wFormatID; /* and CF_TEXT is not available */
620 /* walk up to the specified format record */
622 if( !(lpFormat = __lookup_format( lpFormat, wFormat )) ) return 0;
624 /* find next format with available data */
626 lpFormat = lpFormat->NextFormat;
627 while(TRUE)
629 if (lpFormat == NULL) return 0;
630 if (lpFormat->wDataPresent || (lpFormat->wFormatID == CF_OEMTEXT &&
631 ClipFormats[CF_TEXT-1].wDataPresent))
632 break;
633 lpFormat = lpFormat->NextFormat;
636 return lpFormat->wFormatID;
640 /**************************************************************************
641 * RegisterClipboardFormat16 (USER.145)
643 UINT16 WINAPI RegisterClipboardFormat16( LPCSTR FormatName )
645 LPCLIPFORMAT lpNewFormat;
646 LPCLIPFORMAT lpFormat = ClipFormats;
648 if (FormatName == NULL) return 0;
650 dprintf_clipboard(stddeb,"RegisterClipboardFormat('%s') !\n", FormatName);
652 /* walk format chain to see if it's already registered */
654 while(TRUE)
656 if ( !strcmp(lpFormat->Name,FormatName) )
658 lpFormat->wRefCount++;
659 return lpFormat->wFormatID;
662 if ( lpFormat->NextFormat == NULL ) break;
664 lpFormat = lpFormat->NextFormat;
667 /* allocate storage for new format entry */
669 lpNewFormat = (LPCLIPFORMAT)xmalloc(sizeof(CLIPFORMAT));
670 lpFormat->NextFormat = lpNewFormat;
671 lpNewFormat->wFormatID = LastRegFormat;
672 lpNewFormat->wRefCount = 1;
674 lpNewFormat->Name = (LPSTR)xmalloc(strlen(FormatName) + 1);
675 strcpy(lpNewFormat->Name, FormatName);
677 lpNewFormat->wDataPresent = 0;
678 lpNewFormat->hData = 0;
679 lpNewFormat->BufSize = 0;
680 lpNewFormat->PrevFormat = lpFormat;
681 lpNewFormat->NextFormat = NULL;
683 return LastRegFormat++;
687 /**************************************************************************
688 * RegisterClipboardFormat32A (USER32.430)
690 UINT32 WINAPI RegisterClipboardFormat32A( LPCSTR formatName )
692 return RegisterClipboardFormat16( formatName );
696 /**************************************************************************
697 * RegisterClipboardFormat32W (USER32.431)
699 UINT32 WINAPI RegisterClipboardFormat32W( LPCWSTR formatName )
701 LPSTR aFormat = HEAP_strdupWtoA( GetProcessHeap(), 0, formatName );
702 UINT32 ret = RegisterClipboardFormat32A( aFormat );
703 HeapFree( GetProcessHeap(), 0, aFormat );
704 return ret;
707 /**************************************************************************
708 * GetClipboardFormatName16 (USER.146)
710 INT16 WINAPI GetClipboardFormatName16( UINT16 wFormat, LPSTR retStr, INT16 maxlen )
712 return GetClipboardFormatName32A( wFormat, retStr, maxlen );
716 /**************************************************************************
717 * GetClipboardFormatName32A (USER32.222)
719 INT32 WINAPI GetClipboardFormatName32A( UINT32 wFormat, LPSTR retStr, INT32 maxlen )
721 LPCLIPFORMAT lpFormat = __lookup_format( ClipFormats, wFormat );
723 dprintf_clipboard(stddeb,
724 "GetClipboardFormatName(%04X, %p, %d) !\n", wFormat, retStr, maxlen);
726 if (lpFormat == NULL || lpFormat->Name == NULL ||
727 lpFormat->wFormatID < CF_REGFORMATBASE) return 0;
729 dprintf_clipboard(stddeb,
730 "GetClipboardFormat // Name='%s' !\n", lpFormat->Name);
732 lstrcpyn32A( retStr, lpFormat->Name, maxlen );
733 return strlen(retStr);
737 /**************************************************************************
738 * GetClipboardFormatName32W (USER32.223)
740 INT32 WINAPI GetClipboardFormatName32W( UINT32 wFormat, LPWSTR retStr, INT32 maxlen )
742 LPSTR p = HEAP_xalloc( GetProcessHeap(), 0, maxlen );
743 INT32 ret = GetClipboardFormatName32A( wFormat, p, maxlen );
744 lstrcpynAtoW( retStr, p, maxlen );
745 HeapFree( GetProcessHeap(), 0, p );
746 return ret;
750 /**************************************************************************
751 * SetClipboardViewer16 (USER.147)
753 HWND16 WINAPI SetClipboardViewer16( HWND16 hWnd )
755 return SetClipboardViewer32( hWnd );
759 /**************************************************************************
760 * SetClipboardViewer32 (USER32.470)
762 HWND32 WINAPI SetClipboardViewer32( HWND32 hWnd )
764 HWND32 hwndPrev = hWndViewer;
766 dprintf_clipboard(stddeb,"SetClipboardViewer(%04x): returning %04x\n", hWnd, hwndPrev);
768 hWndViewer = hWnd;
769 return hwndPrev;
773 /**************************************************************************
774 * GetClipboardViewer16 (USER.148)
776 HWND16 WINAPI GetClipboardViewer16(void)
778 return hWndViewer;
782 /**************************************************************************
783 * GetClipboardViewer32 (USER32.225)
785 HWND32 WINAPI GetClipboardViewer32(void)
787 return hWndViewer;
791 /**************************************************************************
792 * ChangeClipboardChain16 (USER.149)
794 BOOL16 WINAPI ChangeClipboardChain16(HWND16 hWnd, HWND16 hWndNext)
796 return ChangeClipboardChain32(hWnd, hWndNext);
799 /**************************************************************************
800 * ChangeClipboardChain32 (USER32.21)
802 BOOL32 WINAPI ChangeClipboardChain32(HWND32 hWnd, HWND32 hWndNext)
804 BOOL32 bRet = 0;
806 dprintf_clipboard(stdnimp, "ChangeClipboardChain(%04x, %04x)\n", hWnd, hWndNext);
808 if( hWndViewer )
809 bRet = !SendMessage16( hWndViewer, WM_CHANGECBCHAIN,
810 (WPARAM16)hWnd, (LPARAM)hWndNext);
811 else
812 dprintf_clipboard(stddeb,"ChangeClipboardChain: hWndViewer is lost\n");
814 if( hWnd == hWndViewer ) hWndViewer = hWndNext;
816 return bRet;
821 /**************************************************************************
822 * IsClipboardFormatAvailable16 (USER.193)
824 BOOL16 WINAPI IsClipboardFormatAvailable16( UINT16 wFormat )
826 return IsClipboardFormatAvailable32( wFormat );
830 /**************************************************************************
831 * IsClipboardFormatAvailable32 (USER32.339)
833 BOOL32 WINAPI IsClipboardFormatAvailable32( UINT32 wFormat )
835 dprintf_clipboard(stddeb,"IsClipboardFormatAvailable(%04X) !\n", wFormat);
837 if( (wFormat == CF_TEXT || wFormat == CF_OEMTEXT) &&
838 !selectionAcquired ) CLIPBOARD_RequestXSelection();
840 return CLIPBOARD_IsPresent(wFormat);
844 /**************************************************************************
845 * GetOpenClipboardWindow16 (USER.248)
847 HWND16 WINAPI GetOpenClipboardWindow16(void)
849 return hWndClipWindow;
853 /**************************************************************************
854 * GetOpenClipboardWindow32 (USER32.276)
856 HWND32 WINAPI GetOpenClipboardWindow32(void)
858 return hWndClipWindow;
862 /**************************************************************************
863 * GetPriorityClipboardFormat16 (USER.402)
865 INT16 WINAPI GetPriorityClipboardFormat16( UINT16 *lpPriorityList, INT16 nCount)
867 fprintf( stderr, "GetPriorityClipboardFormat16(%p, %d): stub\n",
868 lpPriorityList, nCount );
869 return 0;
873 /**************************************************************************
874 * GetPriorityClipboardFormat32 (USER32.279)
876 INT32 WINAPI GetPriorityClipboardFormat32( UINT32 *lpPriorityList, INT32 nCount )
878 int Counter;
880 if(CountClipboardFormats32() == 0)
882 return 0;
885 for(Counter = 0; Counter <= nCount; Counter++)
887 if(IsClipboardFormatAvailable32(*(lpPriorityList+sizeof(INT32)*Counter)))
888 return *(lpPriorityList+sizeof(INT32)*Counter);
891 return -1;
895 /**************************************************************************
896 * CLIPBOARD_ReadSelection
898 * Called from the SelectionNotify event handler.
900 void CLIPBOARD_ReadSelection(Window w,Atom prop)
902 HANDLE16 hText = 0;
903 LPCLIPFORMAT lpFormat = ClipFormats;
905 dprintf_clipboard(stddeb,"ReadSelection callback\n");
907 if(prop != None)
909 Atom atype=AnyPropertyType;
910 int aformat;
911 unsigned long nitems,remain;
912 unsigned char* val=NULL;
914 dprintf_clipboard(stddeb,"\tgot property %s\n",TSXGetAtomName(display,prop));
916 /* TODO: Properties longer than 64K */
918 if(TSXGetWindowProperty(display,w,prop,0,0x3FFF,True,XA_STRING,
919 &atype, &aformat, &nitems, &remain, &val) != Success)
920 dprintf_clipboard(stddeb,"\tcouldn't read property\n");
921 else
923 dprintf_clipboard(stddeb,"\tType %s,Format %d,nitems %ld,value %s\n",
924 TSXGetAtomName(display,atype),aformat,nitems,val);
926 if(atype == XA_STRING && aformat == 8)
928 int i,inlcount = 0;
929 char* lpstr;
931 dprintf_clipboard(stddeb,"\tselection is '%s'\n",val);
933 for(i=0; i <= nitems; i++)
934 if( val[i] == '\n' ) inlcount++;
936 if( nitems )
938 hText=GlobalAlloc16(GMEM_MOVEABLE, nitems + inlcount + 1);
939 if( (lpstr = (char*)GlobalLock16(hText)) )
940 for(i=0,inlcount=0; i <= nitems; i++)
942 if( val[i] == '\n' ) lpstr[inlcount++]='\r';
943 lpstr[inlcount++]=val[i];
945 else hText = 0;
948 TSXFree(val);
952 /* delete previous CF_TEXT and CF_OEMTEXT data */
954 if( hText )
956 lpFormat = &ClipFormats[CF_TEXT-1];
957 if (lpFormat->wDataPresent || lpFormat->hData)
958 CLIPBOARD_DeleteRecord(lpFormat, !(hWndClipWindow));
959 lpFormat = &ClipFormats[CF_OEMTEXT-1];
960 if (lpFormat->wDataPresent || lpFormat->hData)
961 CLIPBOARD_DeleteRecord(lpFormat, !(hWndClipWindow));
963 lpFormat->wDataPresent = 1;
964 lpFormat->hData = hText;
967 selectionWait=False;
970 /**************************************************************************
971 * CLIPBOARD_ReleaseSelection
973 * Wine might have lost XA_PRIMARY selection because of
974 * EmptyClipboard() or other client.
976 void CLIPBOARD_ReleaseSelection(Window w, HWND32 hwnd)
978 /* w is the window that lost selection,
980 * selectionPrevWindow is nonzero if CheckSelection() was called.
983 dprintf_clipboard(stddeb,"\tevent->window = %08x (sw = %08x, spw=%08x)\n",
984 (unsigned)w, (unsigned)selectionWindow, (unsigned)selectionPrevWindow );
986 if( selectionAcquired )
987 if( w == selectionWindow || selectionPrevWindow == None)
989 /* alright, we really lost it */
991 selectionAcquired = False;
992 selectionWindow = None;
994 /* but we'll keep existing data for internal use */
996 else if( w == selectionPrevWindow )
998 w = TSXGetSelectionOwner(display, XA_PRIMARY);
999 if( w == None )
1000 TSXSetSelectionOwner(display, XA_PRIMARY, selectionWindow, CurrentTime);
1003 selectionPrevWindow = None;