Release 970215
[wine/hacks.git] / misc / clipboard.c
blobbc64aacf590c0bcedc0439da597942d9da328d82
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 <X11/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 HWND32 hWndClipOwner = 0; /* current clipboard owner */
45 static HWND32 hWndClipWindow = 0; /* window that opened clipboard */
46 static HWND32 hWndViewer = 0; /* start of viewers chain */
48 static BOOL bClipChanged = FALSE;
49 static WORD LastRegFormat = CF_REGFORMATBASE;
51 static Bool selectionWait = False;
52 static Bool selectionAcquired = False;
53 static Window selectionWindow = None;
54 static Window selectionPrevWindow = None;
56 static CLIPFORMAT ClipFormats[16] = {
57 { CF_TEXT, 1, 0, "Text", (HANDLE16)NULL, 0, NULL, &ClipFormats[1] },
58 { CF_BITMAP, 1, 0, "Bitmap", (HANDLE16)NULL, 0, &ClipFormats[0], &ClipFormats[2] },
59 { CF_METAFILEPICT, 1, 0, "MetaFile Picture", (HANDLE16)NULL, 0, &ClipFormats[1], &ClipFormats[3] },
60 { CF_SYLK, 1, 0, "Sylk", (HANDLE16)NULL, 0, &ClipFormats[2], &ClipFormats[4] },
61 { CF_DIF, 1, 0, "DIF", (HANDLE16)NULL, 0, &ClipFormats[3], &ClipFormats[5] },
62 { CF_TIFF, 1, 0, "TIFF", (HANDLE16)NULL, 0, &ClipFormats[4], &ClipFormats[6] },
63 { CF_OEMTEXT, 1, 0, "OEM Text", (HANDLE16)NULL, 0, &ClipFormats[5], &ClipFormats[7] },
64 { CF_DIB, 1, 0, "DIB", (HANDLE16)NULL, 0, &ClipFormats[6], &ClipFormats[8] },
65 { CF_PALETTE, 1, 0, "Palette", (HANDLE16)NULL, 0, &ClipFormats[7], &ClipFormats[9] },
66 { CF_PENDATA, 1, 0, "PenData", (HANDLE16)NULL, 0, &ClipFormats[8], &ClipFormats[10] },
67 { CF_RIFF, 1, 0, "RIFF", (HANDLE16)NULL, 0, &ClipFormats[9], &ClipFormats[11] },
68 { CF_WAVE, 1, 0, "Wave", (HANDLE16)NULL, 0, &ClipFormats[10], &ClipFormats[12] },
69 { CF_OWNERDISPLAY, 1, 0, "Owner Display", (HANDLE16)NULL, 0, &ClipFormats[11], &ClipFormats[13] },
70 { CF_DSPTEXT, 1, 0, "DSPText", (HANDLE16)NULL, 0, &ClipFormats[12], &ClipFormats[14] },
71 { CF_DSPMETAFILEPICT, 1, 0, "DSPMetaFile Picture", (HANDLE16)NULL, 0, &ClipFormats[13], &ClipFormats[15] },
72 { CF_DSPBITMAP, 1, 0, "DSPBitmap", (HANDLE16)NULL, 0, &ClipFormats[14], NULL }
75 /**************************************************************************
76 * CLIPBOARD_CheckSelection
78 void CLIPBOARD_CheckSelection(WND* pWnd)
80 dprintf_clipboard(stddeb,"\tchecking %08x\n", (unsigned)pWnd->window);
82 if( selectionAcquired && selectionWindow != None &&
83 pWnd->window == selectionWindow )
85 selectionPrevWindow = selectionWindow;
86 selectionWindow = None;
88 if( pWnd->next )
89 selectionWindow = pWnd->next->window;
90 else if( pWnd->parent )
91 if( pWnd->parent->child != pWnd )
92 selectionWindow = pWnd->parent->child->window;
94 dprintf_clipboard(stddeb,"\tswitching selection from %08x to %08x\n",
95 (unsigned)selectionPrevWindow, (unsigned)selectionWindow);
97 if( selectionWindow != None )
99 XSetSelectionOwner(display, XA_PRIMARY, selectionWindow, CurrentTime);
100 if( XGetSelectionOwner(display, XA_PRIMARY) != selectionWindow )
101 selectionWindow = None;
106 /**************************************************************************
107 * CLIPBOARD_DisOwn
109 * Called from DestroyWindow().
111 void CLIPBOARD_DisOwn(WND* pWnd)
113 LPCLIPFORMAT lpFormat = ClipFormats;
115 dprintf_clipboard(stddeb,"DisOwn: clipboard owner = %04x, sw = %08x\n",
116 hWndClipOwner, (unsigned)selectionWindow);
118 if( pWnd->hwndSelf == hWndClipOwner)
120 SendMessage16(hWndClipOwner,WM_RENDERALLFORMATS,0,0L);
122 /* check if all formats were rendered */
124 while(lpFormat)
126 if( lpFormat->wDataPresent && !lpFormat->hData )
128 dprintf_clipboard(stddeb,"\tdata missing for clipboard format %i\n", lpFormat->wFormatID);
129 lpFormat->wDataPresent = 0;
131 lpFormat = lpFormat->NextFormat;
133 hWndClipOwner = 0;
136 /* now try to salvage current selection from being destroyed by X */
138 CLIPBOARD_CheckSelection(pWnd);
141 /**************************************************************************
142 * CLIPBOARD_DeleteRecord
144 void CLIPBOARD_DeleteRecord(LPCLIPFORMAT lpFormat)
146 if( lpFormat->wFormatID >= CF_GDIOBJFIRST &&
147 lpFormat->wFormatID <= CF_GDIOBJLAST )
148 DeleteObject32(lpFormat->hData);
149 else if( lpFormat->hData )
150 GlobalFree16(lpFormat->hData);
152 lpFormat->wDataPresent = 0;
153 lpFormat->hData = 0;
155 bClipChanged = TRUE;
158 /**************************************************************************
159 * CLIPBOARD_RequestXSelection
161 BOOL CLIPBOARD_RequestXSelection()
163 HWND32 hWnd = (hWndClipWindow) ? hWndClipWindow : GetActiveWindow32();
165 if( !hWnd ) return FALSE;
167 dprintf_clipboard(stddeb,"Requesting selection...\n");
169 /* request data type XA_STRING, later
170 * CLIPBOARD_ReadSelection() will be invoked
171 * from the SelectionNotify event handler */
173 XConvertSelection(display,XA_PRIMARY,XA_STRING,
174 XInternAtom(display,"PRIMARY_TEXT",False),
175 WIN_GetXWindow(hWnd),CurrentTime);
177 /* wait until SelectionNotify is processed */
179 selectionWait=True;
180 while(selectionWait)
181 EVENT_WaitXEvent( TRUE, FALSE );
183 /* we treat Unix text as CF_OEMTEXT */
184 dprintf_clipboard(stddeb,"\tgot CF_OEMTEXT = %i\n",
185 ClipFormats[CF_OEMTEXT-1].wDataPresent);
187 return (BOOL)ClipFormats[CF_OEMTEXT-1].wDataPresent;
190 /**************************************************************************
191 * CLIPBOARD_IsPresent
193 BOOL CLIPBOARD_IsPresent(WORD wFormat)
195 LPCLIPFORMAT lpFormat = ClipFormats;
197 /* special case */
199 if( wFormat == CF_TEXT || wFormat == CF_OEMTEXT )
200 return lpFormat[CF_TEXT-1].wDataPresent ||
201 lpFormat[CF_OEMTEXT-1].wDataPresent;
203 while(TRUE) {
204 if (lpFormat == NULL) return FALSE;
205 if (lpFormat->wFormatID == wFormat) break;
206 lpFormat = lpFormat->NextFormat;
209 return (lpFormat->wDataPresent);
212 /**************************************************************************
213 * OpenClipboard16 (USER.137)
215 BOOL16 OpenClipboard16( HWND16 hWnd )
217 return OpenClipboard32( hWnd );
221 /**************************************************************************
222 * OpenClipboard32 (USER32.406)
224 BOOL32 OpenClipboard32( HWND32 hWnd )
226 BOOL32 bRet = FALSE;
227 dprintf_clipboard(stddeb,"OpenClipboard(%04x) = ", hWnd);
229 if (!hWndClipWindow)
231 hWndClipWindow = hWnd;
232 bRet = TRUE;
234 bClipChanged = FALSE;
236 dprintf_clipboard(stddeb,"%i\n", bRet);
237 return bRet;
241 /**************************************************************************
242 * CloseClipboard16 (USER.138)
244 BOOL16 CloseClipboard16(void)
246 return CloseClipboard32();
250 /**************************************************************************
251 * CloseClipboard32 (USER32.53)
253 BOOL32 CloseClipboard32(void)
255 dprintf_clipboard(stddeb,"CloseClipboard(); !\n");
257 if (hWndClipWindow == 0) return FALSE;
258 hWndClipWindow = 0;
260 if (bClipChanged && hWndViewer) SendMessage16(hWndViewer,WM_DRAWCLIPBOARD,0,0L);
262 return TRUE;
266 /**************************************************************************
267 * EmptyClipboard16 (USER.139)
269 BOOL16 EmptyClipboard16(void)
271 return EmptyClipboard32();
275 /**************************************************************************
276 * EmptyClipboard32 (USER32.168)
278 BOOL32 EmptyClipboard32(void)
280 LPCLIPFORMAT lpFormat = ClipFormats;
282 dprintf_clipboard(stddeb,"EmptyClipboard()\n");
284 if (hWndClipWindow == 0) return FALSE;
286 /* destroy private objects */
288 if (hWndClipOwner)
289 SendMessage16(hWndClipOwner,WM_DESTROYCLIPBOARD,0,0L);
291 while(lpFormat)
293 if ( lpFormat->wDataPresent || lpFormat->hData )
294 CLIPBOARD_DeleteRecord( lpFormat );
296 lpFormat = lpFormat->NextFormat;
299 hWndClipOwner = hWndClipWindow;
301 if(selectionAcquired)
303 selectionAcquired = False;
304 selectionPrevWindow = selectionWindow;
305 selectionWindow = None;
307 dprintf_clipboard(stddeb, "\tgiving up selection (spw = %08x)\n",
308 (unsigned)selectionPrevWindow);
310 XSetSelectionOwner(display,XA_PRIMARY,None,CurrentTime);
312 return TRUE;
316 /**************************************************************************
317 * GetClipboardOwner16 (USER.140)
319 HWND16 GetClipboardOwner16(void)
321 return hWndClipOwner;
325 /**************************************************************************
326 * GetClipboardOwner32 (USER32.224)
328 HWND32 GetClipboardOwner32(void)
330 return hWndClipOwner;
334 /**************************************************************************
335 * SetClipboardData [USER.141]
337 HANDLE16 SetClipboardData(WORD wFormat, HANDLE16 hData)
339 LPCLIPFORMAT lpFormat = ClipFormats;
340 Window owner;
342 dprintf_clipboard(stddeb,
343 "SetClipboardData(%04X, %04x) !\n", wFormat, hData);
345 while(TRUE)
347 if (lpFormat == NULL) return 0;
348 if (lpFormat->wFormatID == wFormat) break;
349 lpFormat = lpFormat->NextFormat;
352 /* Acquire X selection if text format */
354 if( !selectionAcquired &&
355 (wFormat == CF_TEXT || wFormat == CF_OEMTEXT) )
357 owner = WIN_GetXWindow(hWndClipWindow);
358 XSetSelectionOwner(display,XA_PRIMARY,owner,CurrentTime);
359 if( XGetSelectionOwner(display,XA_PRIMARY) == owner )
361 selectionAcquired = True;
362 selectionWindow = owner;
364 dprintf_clipboard(stddeb,"Grabbed X selection, owner=(%08x)\n",
365 (unsigned) owner);
369 if ( lpFormat->wDataPresent || lpFormat->hData )
371 CLIPBOARD_DeleteRecord(lpFormat);
373 /* delete existing CF_TEXT/CF_OEMTEXT aliases */
375 if( wFormat == CF_TEXT && ClipFormats[CF_OEMTEXT-1].hData
376 && !ClipFormats[CF_OEMTEXT-1].wDataPresent )
377 CLIPBOARD_DeleteRecord(&ClipFormats[CF_OEMTEXT-1]);
378 if( wFormat == CF_OEMTEXT && ClipFormats[CF_TEXT-1].hData
379 && !ClipFormats[CF_TEXT-1].wDataPresent )
380 CLIPBOARD_DeleteRecord(&ClipFormats[CF_TEXT-1]);
383 bClipChanged = TRUE;
384 lpFormat->wDataPresent = 1;
385 lpFormat->hData = hData; /* 0 is legal, see WM_RENDERFORMAT */
387 return lpFormat->hData;
390 /**************************************************************************
391 * CLIPBOARD_RenderFormat
393 BOOL32 CLIPBOARD_RenderFormat(LPCLIPFORMAT lpFormat)
395 if( lpFormat->wDataPresent && !lpFormat->hData )
396 if( IsWindow(hWndClipOwner) )
397 SendMessage16(hWndClipOwner,WM_RENDERFORMAT,
398 (WPARAM16)lpFormat->wFormatID,0L);
399 else
401 dprintf_clipboard(stddeb,"\thWndClipOwner (%04x) is lost!\n",
402 hWndClipOwner);
403 hWndClipOwner = 0; lpFormat->wDataPresent = 0;
404 return FALSE;
406 return (lpFormat->hData) ? TRUE : FALSE;
409 /**************************************************************************
410 * CLIPBOARD_RenderText
412 BOOL32 CLIPBOARD_RenderText(LPCLIPFORMAT lpTarget, LPCLIPFORMAT lpSource)
414 UINT size = GlobalSize16( lpSource->hData );
415 LPCSTR lpstrS = (LPSTR)GlobalLock16(lpSource->hData);
416 LPSTR lpstrT;
418 if( !lpstrS ) return FALSE;
419 dprintf_clipboard(stddeb,"\tconverting from '%s' to '%s', %i chars\n",
420 lpSource->Name, lpTarget->Name, size);
422 lpTarget->hData = GlobalAlloc16(GMEM_ZEROINIT, size);
423 lpstrT = (LPSTR)GlobalLock16(lpTarget->hData);
425 if( lpstrT )
427 if( lpSource->wFormatID == CF_TEXT )
428 CharToOemBuff32A(lpstrS, lpstrT, size);
429 else
430 OemToCharBuff32A(lpstrS, lpstrT, size);
431 dprintf_clipboard(stddeb,"\tgot %s\n", lpstrT);
432 return TRUE;
435 lpTarget->hData = 0;
436 return FALSE;
439 /**************************************************************************
440 * GetClipboardData [USER.142]
442 HANDLE16 GetClipboardData(WORD wFormat)
444 LPCLIPFORMAT lpRender = ClipFormats;
445 LPCLIPFORMAT lpUpdate = NULL;
447 if (!hWndClipWindow) return 0;
449 dprintf_clipboard(stddeb,"GetClipboardData(%04X)\n", wFormat);
451 if( wFormat == CF_TEXT && !lpRender[CF_TEXT-1].wDataPresent
452 && lpRender[CF_OEMTEXT-1].wDataPresent )
454 lpRender = &ClipFormats[CF_OEMTEXT-1];
455 lpUpdate = &ClipFormats[CF_TEXT-1];
457 dprintf_clipboard(stddeb,"\tOEMTEXT -> TEXT\n");
459 else if( wFormat == CF_OEMTEXT && !lpRender[CF_OEMTEXT-1].wDataPresent
460 && lpRender[CF_TEXT-1].wDataPresent )
462 lpRender = &ClipFormats[CF_TEXT-1];
463 lpUpdate = &ClipFormats[CF_OEMTEXT-1];
465 dprintf_clipboard(stddeb,"\tTEXT -> OEMTEXT\n");
467 else
469 while(TRUE)
471 if (lpRender == NULL) return 0;
472 if (lpRender->wFormatID == wFormat) break;
473 lpRender = lpRender->NextFormat;
475 lpUpdate = lpRender;
478 if( !CLIPBOARD_RenderFormat(lpRender) ) return 0;
479 if( lpUpdate != lpRender &&
480 !lpUpdate->hData ) CLIPBOARD_RenderText(lpUpdate, lpRender);
482 dprintf_clipboard(stddeb,"\treturning %04x (type %i)\n",
483 lpUpdate->hData, lpUpdate->wFormatID);
484 return lpUpdate->hData;
488 /**************************************************************************
489 * CountClipboardFormats16 (USER.143)
491 INT16 CountClipboardFormats16(void)
493 return CountClipboardFormats32();
497 /**************************************************************************
498 * CountClipboardFormats32 (USER32.62)
500 INT32 CountClipboardFormats32(void)
502 INT32 FormatCount = 0;
503 LPCLIPFORMAT lpFormat = ClipFormats;
505 dprintf_clipboard(stddeb,"CountClipboardFormats()\n");
507 if( !selectionAcquired ) CLIPBOARD_RequestXSelection();
509 FormatCount += abs(lpFormat[CF_TEXT-1].wDataPresent -
510 lpFormat[CF_OEMTEXT-1].wDataPresent);
512 while(TRUE) {
513 if (lpFormat == NULL) break;
514 if (lpFormat->wDataPresent)
516 dprintf_clipboard(stddeb, "\tdata found for format %i\n", lpFormat->wFormatID);
518 FormatCount++;
520 lpFormat = lpFormat->NextFormat;
523 dprintf_clipboard(stddeb,"\ttotal %d\n", FormatCount);
524 return FormatCount;
528 /**************************************************************************
529 * EnumClipboardFormats16 (USER.144)
531 UINT16 EnumClipboardFormats16( UINT16 wFormat )
533 return EnumClipboardFormats32( wFormat );
537 /**************************************************************************
538 * EnumClipboardFormats32 (USER32.178)
540 UINT32 EnumClipboardFormats32( UINT32 wFormat )
542 LPCLIPFORMAT lpFormat = ClipFormats;
544 dprintf_clipboard(stddeb,"EnumClipboardFormats(%04X)\n", wFormat);
546 if( !hWndClipWindow ) return 0;
548 if( (!wFormat || wFormat == CF_TEXT || wFormat == CF_OEMTEXT)
549 && !selectionAcquired) CLIPBOARD_RequestXSelection();
551 if (wFormat == 0)
552 if (lpFormat->wDataPresent || ClipFormats[CF_OEMTEXT-1].wDataPresent)
553 return lpFormat->wFormatID;
554 else
555 wFormat = lpFormat->wFormatID; /* and CF_TEXT is not available */
557 /* walk up to the specified format record */
559 while(TRUE) {
560 if (lpFormat == NULL) return 0;
561 if (lpFormat->wFormatID == wFormat) break;
562 lpFormat = lpFormat->NextFormat;
565 /* find next format with available data */
567 lpFormat = lpFormat->NextFormat;
568 while(TRUE) {
569 if (lpFormat == NULL) return 0;
570 if (lpFormat->wDataPresent ||
571 (lpFormat->wFormatID == CF_OEMTEXT &&
572 ClipFormats[CF_TEXT-1].wDataPresent)) break;
573 lpFormat = lpFormat->NextFormat;
576 return lpFormat->wFormatID;
580 /**************************************************************************
581 * RegisterClipboardFormat16 (USER.145)
583 UINT16 RegisterClipboardFormat16( LPCSTR FormatName )
585 LPCLIPFORMAT lpNewFormat;
586 LPCLIPFORMAT lpFormat = ClipFormats;
588 if (FormatName == NULL) return 0;
590 dprintf_clipboard(stddeb,"RegisterClipboardFormat('%s') !\n", FormatName);
592 /* walk format chain to see if it's already registered */
594 while(TRUE) {
595 if ( !strcmp(lpFormat->Name,FormatName) )
597 lpFormat->wRefCount++;
598 return lpFormat->wFormatID;
601 if ( lpFormat->NextFormat == NULL ) break;
603 lpFormat = lpFormat->NextFormat;
606 /* allocate storage for new format entry */
608 lpNewFormat = (LPCLIPFORMAT)xmalloc(sizeof(CLIPFORMAT));
609 lpFormat->NextFormat = lpNewFormat;
610 lpNewFormat->wFormatID = LastRegFormat;
611 lpNewFormat->wRefCount = 1;
613 lpNewFormat->Name = (LPSTR)xmalloc(strlen(FormatName) + 1);
614 strcpy(lpNewFormat->Name, FormatName);
616 lpNewFormat->wDataPresent = 0;
617 lpNewFormat->hData = 0;
618 lpNewFormat->BufSize = 0;
619 lpNewFormat->PrevFormat = lpFormat;
620 lpNewFormat->NextFormat = NULL;
622 return LastRegFormat++;
626 /**************************************************************************
627 * RegisterClipboardFormat32A (USER32.430)
629 UINT32 RegisterClipboardFormat32A( LPCSTR formatName )
631 return RegisterClipboardFormat16( formatName );
635 /**************************************************************************
636 * RegisterClipboardFormat32W (USER32.431)
638 UINT32 RegisterClipboardFormat32W( LPCWSTR formatName )
640 LPSTR aFormat = HEAP_strdupWtoA( GetProcessHeap(), 0, formatName );
641 UINT32 ret = RegisterClipboardFormat32A( aFormat );
642 HeapFree( GetProcessHeap(), 0, aFormat );
643 return ret;
646 /**************************************************************************
647 * GetClipboardFormatName [USER.146]
649 int GetClipboardFormatName(WORD wFormat, LPSTR retStr, short maxlen)
651 LPCLIPFORMAT lpFormat = ClipFormats;
653 dprintf_clipboard(stddeb,
654 "GetClipboardFormat(%04X, %p, %d) !\n", wFormat, retStr, maxlen);
656 while(TRUE) {
657 if (lpFormat == NULL) return 0;
658 if (lpFormat->wFormatID == wFormat) break;
659 lpFormat = lpFormat->NextFormat;
662 if (lpFormat->Name == NULL ||
663 lpFormat->wFormatID < CF_REGFORMATBASE) return 0;
665 dprintf_clipboard(stddeb,
666 "GetClipboardFormat // Name='%s' !\n", lpFormat->Name);
668 strncpy(retStr, lpFormat->Name, maxlen - 1);
669 retStr[maxlen] = 0;
671 return strlen(retStr);
675 /**************************************************************************
676 * SetClipboardViewer16 (USER.147)
678 HWND16 SetClipboardViewer16( HWND16 hWnd )
680 return SetClipboardViewer32( hWnd );
684 /**************************************************************************
685 * SetClipboardViewer32 (USER32.470)
687 HWND32 SetClipboardViewer32( HWND32 hWnd )
689 HWND32 hwndPrev = hWndViewer;
691 dprintf_clipboard(stddeb,"SetClipboardViewer(%04x)\n", hWnd);
693 hWndViewer = hWnd;
694 return hwndPrev;
698 /**************************************************************************
699 * GetClipboardViewer16 (USER.148)
701 HWND16 GetClipboardViewer16(void)
703 return hWndViewer;
707 /**************************************************************************
708 * GetClipboardViewer32 (USER32.225)
710 HWND32 GetClipboardViewer32(void)
712 return hWndViewer;
716 /**************************************************************************
717 * ChangeClipboardChain [USER.149]
719 BOOL16 ChangeClipboardChain16(HWND16 hWnd, HWND16 hWndNext)
721 return ChangeClipboardChain32(hWnd,hWndNext);
724 /**************************************************************************
725 * ChangeClipboardChain [USER32.21]
727 BOOL32 ChangeClipboardChain32(HWND32 hWnd, HWND32 hWndNext)
729 BOOL32 bRet = 0;
731 dprintf_clipboard(stdnimp, "ChangeClipboardChain(%04x, %04x)\n", hWnd, hWndNext);
733 if( hWndViewer )
734 bRet = !SendMessage16( hWndViewer, WM_CHANGECBCHAIN,
735 (WPARAM16)hWnd, (LPARAM)hWndNext);
736 else
737 dprintf_clipboard(stddeb,"ChangeClipboardChain: hWndViewer is lost\n");
739 if( hWnd == hWndViewer ) hWndViewer = hWndNext;
741 return bRet;
746 /**************************************************************************
747 * IsClipboardFormatAvailable16 (USER.193)
749 BOOL16 IsClipboardFormatAvailable16( UINT16 wFormat )
751 return IsClipboardFormatAvailable32( wFormat );
755 /**************************************************************************
756 * IsClipboardFormatAvailable32 (USER32.339)
758 BOOL32 IsClipboardFormatAvailable32( UINT32 wFormat )
760 dprintf_clipboard(stddeb,"IsClipboardFormatAvailable(%04X) !\n", wFormat);
762 if( (wFormat == CF_TEXT || wFormat == CF_OEMTEXT) &&
763 !selectionAcquired ) CLIPBOARD_RequestXSelection();
765 return CLIPBOARD_IsPresent(wFormat);
769 /**************************************************************************
770 * GetOpenClipboardWindow16 (USER.248)
772 HWND16 GetOpenClipboardWindow16(void)
774 return hWndClipWindow;
778 /**************************************************************************
779 * GetOpenClipboardWindow32 (USER32.276)
781 HWND32 GetOpenClipboardWindow32(void)
783 return hWndClipWindow;
787 /**************************************************************************
788 * GetPriorityClipboardFormat [USER.402]
790 int GetPriorityClipboardFormat(WORD *lpPriorityList, short nCount)
792 dprintf_clipboard(stdnimp,
793 "GetPriorityClipboardFormat(%p, %d) !\n", lpPriorityList, nCount);
795 return 0;
799 /**************************************************************************
800 * CLIPBOARD_ReadSelection
802 * Called from the SelectionNotify event handler.
804 void CLIPBOARD_ReadSelection(Window w,Atom prop)
806 HANDLE16 hText = 0;
807 LPCLIPFORMAT lpFormat = ClipFormats;
809 dprintf_clipboard(stddeb,"ReadSelection callback\n");
811 if(prop != None)
813 Atom atype=AnyPropertyType;
814 int aformat;
815 unsigned long nitems,remain;
816 unsigned char* val=NULL;
818 dprintf_clipboard(stddeb,"\tgot property %s\n",XGetAtomName(display,prop));
820 /* TODO: Properties longer than 64K */
822 if(XGetWindowProperty(display,w,prop,0,0x3FFF,True,XA_STRING,
823 &atype, &aformat, &nitems, &remain, &val) != Success)
824 dprintf_clipboard(stddeb,"\tcouldn't read property\n");
825 else
827 dprintf_clipboard(stddeb,"\tType %s,Format %d,nitems %ld,value %s\n",
828 XGetAtomName(display,atype),aformat,nitems,val);
830 if(atype == XA_STRING && aformat == 8)
832 int i,inlcount = 0;
833 char* lpstr;
835 dprintf_clipboard(stddeb,"\tselection is '%s'\n",val);
837 for(i=0; i <= nitems; i++)
838 if( val[i] == '\n' ) inlcount++;
840 if( nitems )
842 hText=GlobalAlloc16(GMEM_MOVEABLE, nitems + inlcount + 1);
843 if( (lpstr = (char*)GlobalLock16(hText)) )
844 for(i=0,inlcount=0; i <= nitems; i++)
846 if( val[i] == '\n' ) lpstr[inlcount++]='\r';
847 lpstr[inlcount++]=val[i];
849 else hText = 0;
852 XFree(val);
856 /* delete previous CF_TEXT and CF_OEMTEXT data */
858 if( hText )
860 lpFormat = &ClipFormats[CF_TEXT-1];
861 if (lpFormat->wDataPresent || lpFormat->hData)
862 CLIPBOARD_DeleteRecord(lpFormat);
863 lpFormat = &ClipFormats[CF_OEMTEXT-1];
864 if (lpFormat->wDataPresent || lpFormat->hData)
865 CLIPBOARD_DeleteRecord(lpFormat);
867 lpFormat->wDataPresent = 1;
868 lpFormat->hData = hText;
871 selectionWait=False;
874 /**************************************************************************
875 * CLIPBOARD_ReleaseSelection
877 * Wine might have lost XA_PRIMARY selection because of
878 * EmptyClipboard() or other client.
880 void CLIPBOARD_ReleaseSelection(Window w, HWND32 hwnd)
882 /* w is the window that lost selection,
884 * selectionPrevWindow is nonzero if CheckSelection() was called.
887 dprintf_clipboard(stddeb,"\tevent->window = %08x (sw = %08x, spw=%08x)\n",
888 (unsigned)w, (unsigned)selectionWindow, (unsigned)selectionPrevWindow );
890 if( selectionAcquired )
891 if( w == selectionWindow || selectionPrevWindow == None)
893 /* alright, we really lost it */
895 selectionAcquired = False;
896 selectionWindow = None;
898 /* but we'll keep existing data for internal use */
900 else if( w == selectionPrevWindow )
902 w = XGetSelectionOwner(display, XA_PRIMARY);
904 if( w == None )
905 XSetSelectionOwner(display, XA_PRIMARY, selectionWindow, CurrentTime);
908 selectionPrevWindow = None;