Release 961222
[wine/multimedia.git] / misc / clipboard.c
blobe3d5a61fcce87f872863383cdbcf0cdaf68fd134
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 HWND hWndClipOwner = 0; /* current clipboard owner */
45 static HWND hWndClipWindow = 0; /* window that opened clipboard */
46 static HWND 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 HWND hWnd = (hWndClipWindow) ? hWndClipWindow : GetActiveWindow();
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 * OpenClipboard [USER.137]
215 BOOL OpenClipboard(HWND hWnd)
217 BOOL bRet = FALSE;
218 dprintf_clipboard(stddeb,"OpenClipboard(%04x) = ", hWnd);
220 if (!hWndClipWindow)
222 hWndClipWindow = hWnd;
223 bRet = TRUE;
225 bClipChanged = FALSE;
227 dprintf_clipboard(stddeb,"%i\n", bRet);
228 return bRet;
232 /**************************************************************************
233 * CloseClipboard [USER.138]
235 BOOL CloseClipboard()
237 dprintf_clipboard(stddeb,"CloseClipboard(); !\n");
239 if (hWndClipWindow == 0) return FALSE;
240 hWndClipWindow = 0;
242 if (bClipChanged && hWndViewer) SendMessage16(hWndViewer,WM_DRAWCLIPBOARD,0,0L);
244 return TRUE;
248 /**************************************************************************
249 * EmptyClipboard [USER.139]
251 BOOL EmptyClipboard()
253 LPCLIPFORMAT lpFormat = ClipFormats;
255 dprintf_clipboard(stddeb,"EmptyClipboard()\n");
257 if (hWndClipWindow == 0) return FALSE;
259 /* destroy private objects */
261 if (hWndClipOwner)
262 SendMessage16(hWndClipOwner,WM_DESTROYCLIPBOARD,0,0L);
264 while(lpFormat)
266 if ( lpFormat->wDataPresent || lpFormat->hData )
267 CLIPBOARD_DeleteRecord( lpFormat );
269 lpFormat = lpFormat->NextFormat;
272 hWndClipOwner = hWndClipWindow;
274 if(selectionAcquired)
276 selectionAcquired = False;
277 selectionPrevWindow = selectionWindow;
278 selectionWindow = None;
280 dprintf_clipboard(stddeb, "\tgiving up selection (spw = %08x)\n",
281 (unsigned)selectionPrevWindow);
283 XSetSelectionOwner(display,XA_PRIMARY,None,CurrentTime);
285 return TRUE;
289 /**************************************************************************
290 * GetClipboardOwner [USER.140]
292 HWND GetClipboardOwner()
294 dprintf_clipboard(stddeb,
295 "GetClipboardOwner() = %04x !\n", hWndClipOwner);
296 return hWndClipOwner;
300 /**************************************************************************
301 * SetClipboardData [USER.141]
303 HANDLE16 SetClipboardData(WORD wFormat, HANDLE16 hData)
305 LPCLIPFORMAT lpFormat = ClipFormats;
306 Window owner;
308 dprintf_clipboard(stddeb,
309 "SetClipboardData(%04X, %04x) !\n", wFormat, hData);
311 while(TRUE)
313 if (lpFormat == NULL) return 0;
314 if (lpFormat->wFormatID == wFormat) break;
315 lpFormat = lpFormat->NextFormat;
318 /* Acquire X selection if text format */
320 if( !selectionAcquired &&
321 (wFormat == CF_TEXT || wFormat == CF_OEMTEXT) )
323 owner = WIN_GetXWindow(hWndClipWindow);
324 XSetSelectionOwner(display,XA_PRIMARY,owner,CurrentTime);
325 if( XGetSelectionOwner(display,XA_PRIMARY) == owner )
327 selectionAcquired = True;
328 selectionWindow = owner;
330 dprintf_clipboard(stddeb,"Grabbed X selection, owner=(%08x)\n",
331 (unsigned) owner);
335 if ( lpFormat->wDataPresent || lpFormat->hData )
337 CLIPBOARD_DeleteRecord(lpFormat);
339 /* delete existing CF_TEXT/CF_OEMTEXT aliases */
341 if( wFormat == CF_TEXT && ClipFormats[CF_OEMTEXT-1].hData
342 && !ClipFormats[CF_OEMTEXT-1].wDataPresent )
343 CLIPBOARD_DeleteRecord(&ClipFormats[CF_OEMTEXT-1]);
344 if( wFormat == CF_OEMTEXT && ClipFormats[CF_TEXT-1].hData
345 && !ClipFormats[CF_TEXT-1].wDataPresent )
346 CLIPBOARD_DeleteRecord(&ClipFormats[CF_TEXT-1]);
349 bClipChanged = TRUE;
350 lpFormat->wDataPresent = 1;
351 lpFormat->hData = hData; /* 0 is legal, see WM_RENDERFORMAT */
353 return lpFormat->hData;
356 /**************************************************************************
357 * CLIPBOARD_RenderFormat
359 BOOL32 CLIPBOARD_RenderFormat(LPCLIPFORMAT lpFormat)
361 if( lpFormat->wDataPresent && !lpFormat->hData )
362 if( IsWindow(hWndClipOwner) )
363 SendMessage16(hWndClipOwner,WM_RENDERFORMAT,
364 (WPARAM16)lpFormat->wFormatID,0L);
365 else
367 dprintf_clipboard(stddeb,"\thWndClipOwner (%04x) is lost!\n",
368 hWndClipOwner);
369 hWndClipOwner = 0; lpFormat->wDataPresent = 0;
370 return FALSE;
372 return (lpFormat->hData) ? TRUE : FALSE;
375 /**************************************************************************
376 * CLIPBOARD_RenderText
378 BOOL32 CLIPBOARD_RenderText(LPCLIPFORMAT lpTarget, LPCLIPFORMAT lpSource)
380 UINT size = GlobalSize16( lpSource->hData );
381 LPCSTR lpstrS = (LPSTR)GlobalLock16(lpSource->hData);
382 LPSTR lpstrT;
384 if( !lpstrS ) return FALSE;
385 dprintf_clipboard(stddeb,"\tconverting from '%s' to '%s', %i chars\n",
386 lpSource->Name, lpTarget->Name, size);
388 lpTarget->hData = GlobalAlloc16(GMEM_ZEROINIT, size);
389 lpstrT = (LPSTR)GlobalLock16(lpTarget->hData);
391 if( lpstrT )
393 if( lpSource->wFormatID == CF_TEXT )
394 CharToOemBuff32A(lpstrS, lpstrT, size);
395 else
396 OemToCharBuff32A(lpstrS, lpstrT, size);
397 dprintf_clipboard(stddeb,"\tgot %s\n", lpstrT);
398 return TRUE;
401 lpTarget->hData = 0;
402 return FALSE;
405 /**************************************************************************
406 * GetClipboardData [USER.142]
408 HANDLE16 GetClipboardData(WORD wFormat)
410 LPCLIPFORMAT lpRender = ClipFormats;
411 LPCLIPFORMAT lpUpdate = NULL;
413 if (!hWndClipWindow) return 0;
415 dprintf_clipboard(stddeb,"GetClipboardData(%04X)\n", wFormat);
417 if( wFormat == CF_TEXT && !lpRender[CF_TEXT-1].wDataPresent
418 && lpRender[CF_OEMTEXT-1].wDataPresent )
420 lpRender = &ClipFormats[CF_OEMTEXT-1];
421 lpUpdate = &ClipFormats[CF_TEXT-1];
423 dprintf_clipboard(stddeb,"\tOEMTEXT -> TEXT\n");
425 else if( wFormat == CF_OEMTEXT && !lpRender[CF_OEMTEXT-1].wDataPresent
426 && lpRender[CF_TEXT-1].wDataPresent )
428 lpRender = &ClipFormats[CF_TEXT-1];
429 lpUpdate = &ClipFormats[CF_OEMTEXT-1];
431 dprintf_clipboard(stddeb,"\tTEXT -> OEMTEXT\n");
433 else
435 while(TRUE)
437 if (lpRender == NULL) return 0;
438 if (lpRender->wFormatID == wFormat) break;
439 lpRender = lpRender->NextFormat;
441 lpUpdate = lpRender;
444 if( !CLIPBOARD_RenderFormat(lpRender) ) return 0;
445 if( lpUpdate != lpRender &&
446 !lpUpdate->hData ) CLIPBOARD_RenderText(lpUpdate, lpRender);
448 dprintf_clipboard(stddeb,"\treturning %04x (type %i)\n",
449 lpUpdate->hData, lpUpdate->wFormatID);
450 return lpUpdate->hData;
454 /**************************************************************************
455 * CountClipboardFormats [USER.143]
457 INT CountClipboardFormats()
459 int FormatCount = 0;
460 LPCLIPFORMAT lpFormat = ClipFormats;
462 dprintf_clipboard(stddeb,"CountClipboardFormats()\n");
464 if( !selectionAcquired ) CLIPBOARD_RequestXSelection();
466 FormatCount += abs(lpFormat[CF_TEXT-1].wDataPresent -
467 lpFormat[CF_OEMTEXT-1].wDataPresent);
469 while(TRUE) {
470 if (lpFormat == NULL) break;
471 if (lpFormat->wDataPresent)
473 dprintf_clipboard(stddeb, "\tdata found for format %i\n", lpFormat->wFormatID);
475 FormatCount++;
477 lpFormat = lpFormat->NextFormat;
480 dprintf_clipboard(stddeb,"\ttotal %d\n", FormatCount);
481 return FormatCount;
485 /**************************************************************************
486 * EnumClipboardFormats [USER.144]
488 UINT16 EnumClipboardFormats(UINT16 wFormat)
490 LPCLIPFORMAT lpFormat = ClipFormats;
492 dprintf_clipboard(stddeb,"EnumClipboardFormats(%04X)\n", wFormat);
494 if( !hWndClipWindow ) return 0;
496 if( (!wFormat || wFormat == CF_TEXT || wFormat == CF_OEMTEXT)
497 && !selectionAcquired) CLIPBOARD_RequestXSelection();
499 if (wFormat == 0)
500 if (lpFormat->wDataPresent || ClipFormats[CF_OEMTEXT-1].wDataPresent)
501 return lpFormat->wFormatID;
502 else
503 wFormat = lpFormat->wFormatID; /* and CF_TEXT is not available */
505 /* walk up to the specified format record */
507 while(TRUE) {
508 if (lpFormat == NULL) return 0;
509 if (lpFormat->wFormatID == wFormat) break;
510 lpFormat = lpFormat->NextFormat;
513 /* find next format with available data */
515 lpFormat = lpFormat->NextFormat;
516 while(TRUE) {
517 if (lpFormat == NULL) return 0;
518 if (lpFormat->wDataPresent ||
519 (lpFormat->wFormatID == CF_OEMTEXT &&
520 ClipFormats[CF_TEXT-1].wDataPresent)) break;
521 lpFormat = lpFormat->NextFormat;
524 return lpFormat->wFormatID;
528 /**************************************************************************
529 * RegisterClipboardFormat16 (USER.145)
531 UINT16 RegisterClipboardFormat16( LPCSTR FormatName )
533 LPCLIPFORMAT lpNewFormat;
534 LPCLIPFORMAT lpFormat = ClipFormats;
536 if (FormatName == NULL) return 0;
538 dprintf_clipboard(stddeb,"RegisterClipboardFormat('%s') !\n", FormatName);
540 /* walk format chain to see if it's already registered */
542 while(TRUE) {
543 if ( !strcmp(lpFormat->Name,FormatName) )
545 lpFormat->wRefCount++;
546 return lpFormat->wFormatID;
549 if ( lpFormat->NextFormat == NULL ) break;
551 lpFormat = lpFormat->NextFormat;
554 /* allocate storage for new format entry */
556 lpNewFormat = (LPCLIPFORMAT)xmalloc(sizeof(CLIPFORMAT));
557 lpFormat->NextFormat = lpNewFormat;
558 lpNewFormat->wFormatID = LastRegFormat;
559 lpNewFormat->wRefCount = 1;
561 lpNewFormat->Name = (LPSTR)xmalloc(strlen(FormatName) + 1);
562 strcpy(lpNewFormat->Name, FormatName);
564 lpNewFormat->wDataPresent = 0;
565 lpNewFormat->hData = 0;
566 lpNewFormat->BufSize = 0;
567 lpNewFormat->PrevFormat = lpFormat;
568 lpNewFormat->NextFormat = NULL;
570 return LastRegFormat++;
574 /**************************************************************************
575 * RegisterClipboardFormat32A (USER32.430)
577 UINT32 RegisterClipboardFormat32A( LPCSTR formatName )
579 return RegisterClipboardFormat16( formatName );
583 /**************************************************************************
584 * RegisterClipboardFormat32W (USER32.431)
586 UINT32 RegisterClipboardFormat32W( LPCWSTR formatName )
588 LPSTR aFormat = HEAP_strdupWtoA( GetProcessHeap(), 0, formatName );
589 UINT32 ret = RegisterClipboardFormat32A( aFormat );
590 HeapFree( GetProcessHeap(), 0, aFormat );
591 return ret;
594 /**************************************************************************
595 * GetClipboardFormatName [USER.146]
597 int GetClipboardFormatName(WORD wFormat, LPSTR retStr, short maxlen)
599 LPCLIPFORMAT lpFormat = ClipFormats;
601 dprintf_clipboard(stddeb,
602 "GetClipboardFormat(%04X, %p, %d) !\n", wFormat, retStr, maxlen);
604 while(TRUE) {
605 if (lpFormat == NULL) return 0;
606 if (lpFormat->wFormatID == wFormat) break;
607 lpFormat = lpFormat->NextFormat;
610 if (lpFormat->Name == NULL ||
611 lpFormat->wFormatID < CF_REGFORMATBASE) return 0;
613 dprintf_clipboard(stddeb,
614 "GetClipboardFormat // Name='%s' !\n", lpFormat->Name);
616 strncpy(retStr, lpFormat->Name, maxlen - 1);
617 retStr[maxlen] = 0;
619 return strlen(retStr);
623 /**************************************************************************
624 * SetClipboardViewer [USER.147]
626 HWND SetClipboardViewer(HWND hWnd)
628 HWND hwndPrev = hWndViewer;
630 dprintf_clipboard(stddeb,"SetClipboardViewer(%04x)\n", hWnd);
632 hWndViewer = hWnd;
633 return hwndPrev;
637 /**************************************************************************
638 * GetClipboardViewer [USER.148]
640 HWND GetClipboardViewer()
642 dprintf_clipboard(stddeb,"GetClipboardFormat() = %04x\n", hWndViewer);
644 return hWndViewer;
648 /**************************************************************************
649 * ChangeClipboardChain [USER.149]
651 BOOL ChangeClipboardChain(HWND hWnd, HWND hWndNext)
653 BOOL bRet = 0;
655 dprintf_clipboard(stdnimp, "ChangeClipboardChain(%04x, %04x)\n", hWnd, hWndNext);
657 if( hWndViewer )
658 bRet = !SendMessage16( hWndViewer, WM_CHANGECBCHAIN,
659 (WPARAM16)hWnd, (LPARAM)hWndNext);
660 else
661 dprintf_clipboard(stddeb,"ChangeClipboardChain: hWndViewer is lost\n");
663 if( hWnd == hWndViewer ) hWndViewer = hWndNext;
665 return 0;
669 /**************************************************************************
670 * IsClipboardFormatAvailable [USER.193]
672 BOOL IsClipboardFormatAvailable(WORD wFormat)
674 dprintf_clipboard(stddeb,"IsClipboardFormatAvailable(%04X) !\n", wFormat);
676 if( (wFormat == CF_TEXT || wFormat == CF_OEMTEXT) &&
677 !selectionAcquired ) CLIPBOARD_RequestXSelection();
679 return CLIPBOARD_IsPresent(wFormat);
683 /**************************************************************************
684 * GetOpenClipboardWindow [USER.248]
686 HWND GetOpenClipboardWindow()
688 dprintf_clipboard(stddeb,
689 "GetOpenClipboardWindow() = %04x\n", hWndClipWindow);
690 return hWndClipWindow;
694 /**************************************************************************
695 * GetPriorityClipboardFormat [USER.402]
697 int GetPriorityClipboardFormat(WORD *lpPriorityList, short nCount)
699 dprintf_clipboard(stdnimp,
700 "GetPriorityClipboardFormat(%p, %d) !\n", lpPriorityList, nCount);
702 return 0;
706 /**************************************************************************
707 * CLIPBOARD_ReadSelection
709 * Called from the SelectionNotify event handler.
711 void CLIPBOARD_ReadSelection(Window w,Atom prop)
713 HANDLE16 hText = 0;
714 LPCLIPFORMAT lpFormat = ClipFormats;
716 dprintf_clipboard(stddeb,"ReadSelection callback\n");
718 if(prop != None)
720 Atom atype=AnyPropertyType;
721 int aformat;
722 unsigned long nitems,remain;
723 unsigned char* val=NULL;
725 dprintf_clipboard(stddeb,"\tgot property %s\n",XGetAtomName(display,prop));
727 /* TODO: Properties longer than 64K */
729 if(XGetWindowProperty(display,w,prop,0,0x3FFF,True,XA_STRING,
730 &atype, &aformat, &nitems, &remain, &val) != Success)
731 dprintf_clipboard(stddeb,"\tcouldn't read property\n");
732 else
734 dprintf_clipboard(stddeb,"\tType %s,Format %d,nitems %ld,value %s\n",
735 XGetAtomName(display,atype),aformat,nitems,val);
737 if(atype == XA_STRING && aformat == 8)
739 int i,inlcount = 0;
740 char* lpstr;
742 dprintf_clipboard(stddeb,"\tselection is '%s'\n",val);
744 for(i=0; i <= nitems; i++)
745 if( val[i] == '\n' ) inlcount++;
747 if( nitems )
749 hText=GlobalAlloc16(GMEM_MOVEABLE, nitems + inlcount + 1);
750 if( (lpstr = (char*)GlobalLock16(hText)) )
751 for(i=0,inlcount=0; i <= nitems; i++)
753 if( val[i] == '\n' ) lpstr[inlcount++]='\r';
754 lpstr[inlcount++]=val[i];
756 else hText = 0;
759 XFree(val);
763 /* delete previous CF_TEXT and CF_OEMTEXT data */
765 if( hText )
767 lpFormat = &ClipFormats[CF_TEXT-1];
768 if (lpFormat->wDataPresent || lpFormat->hData)
769 CLIPBOARD_DeleteRecord(lpFormat);
770 lpFormat = &ClipFormats[CF_OEMTEXT-1];
771 if (lpFormat->wDataPresent || lpFormat->hData)
772 CLIPBOARD_DeleteRecord(lpFormat);
774 lpFormat->wDataPresent = 1;
775 lpFormat->hData = hText;
778 selectionWait=False;
781 /**************************************************************************
782 * CLIPBOARD_ReleaseSelection
784 * Wine might have lost XA_PRIMARY selection because of
785 * EmptyClipboard() or other client.
787 void CLIPBOARD_ReleaseSelection(Window w, HWND hwnd)
789 /* w is the window that lost selection,
791 * selectionPrevWindow is nonzero if CheckSelection() was called.
794 dprintf_clipboard(stddeb,"\tevent->window = %08x (sw = %08x, spw=%08x)\n",
795 (unsigned)w, (unsigned)selectionWindow, (unsigned)selectionPrevWindow );
797 if( selectionAcquired )
798 if( w == selectionWindow || selectionPrevWindow == None)
800 /* alright, we really lost it */
802 selectionAcquired = False;
803 selectionWindow = None;
805 /* but we'll keep existing data for internal use */
807 else if( w == selectionPrevWindow )
809 w = XGetSelectionOwner(display, XA_PRIMARY);
811 if( w == None )
812 XSetSelectionOwner(display, XA_PRIMARY, selectionWindow, CurrentTime);
815 selectionPrevWindow = None;