Release 961215
[wine/multimedia.git] / misc / clipboard.c
blob47304e330a26b405e7ea28e8ca096c9b3e77f854
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 "message.h"
21 #include "clipboard.h"
22 #include "xmalloc.h"
23 #include "stddebug.h"
24 #include "debug.h"
25 #include "string32.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 AnsiToOemBuff(lpstrS, lpstrT, size);
395 else
396 OemToAnsiBuff(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;
589 UINT32 ret;
590 aFormat = STRING32_DupUniToAnsi(formatName);
591 ret = RegisterClipboardFormat32A(aFormat);
592 free(aFormat);
593 return ret;
596 /**************************************************************************
597 * GetClipboardFormatName [USER.146]
599 int GetClipboardFormatName(WORD wFormat, LPSTR retStr, short maxlen)
601 LPCLIPFORMAT lpFormat = ClipFormats;
603 dprintf_clipboard(stddeb,
604 "GetClipboardFormat(%04X, %p, %d) !\n", wFormat, retStr, maxlen);
606 while(TRUE) {
607 if (lpFormat == NULL) return 0;
608 if (lpFormat->wFormatID == wFormat) break;
609 lpFormat = lpFormat->NextFormat;
612 if (lpFormat->Name == NULL ||
613 lpFormat->wFormatID < CF_REGFORMATBASE) return 0;
615 dprintf_clipboard(stddeb,
616 "GetClipboardFormat // Name='%s' !\n", lpFormat->Name);
618 strncpy(retStr, lpFormat->Name, maxlen - 1);
619 retStr[maxlen] = 0;
621 return strlen(retStr);
625 /**************************************************************************
626 * SetClipboardViewer [USER.147]
628 HWND SetClipboardViewer(HWND hWnd)
630 HWND hwndPrev = hWndViewer;
632 dprintf_clipboard(stddeb,"SetClipboardViewer(%04x)\n", hWnd);
634 hWndViewer = hWnd;
635 return hwndPrev;
639 /**************************************************************************
640 * GetClipboardViewer [USER.148]
642 HWND GetClipboardViewer()
644 dprintf_clipboard(stddeb,"GetClipboardFormat() = %04x\n", hWndViewer);
646 return hWndViewer;
650 /**************************************************************************
651 * ChangeClipboardChain [USER.149]
653 BOOL ChangeClipboardChain(HWND hWnd, HWND hWndNext)
655 BOOL bRet = 0;
657 dprintf_clipboard(stdnimp, "ChangeClipboardChain(%04x, %04x)\n", hWnd, hWndNext);
659 if( hWndViewer )
660 bRet = !SendMessage16( hWndViewer, WM_CHANGECBCHAIN,
661 (WPARAM16)hWnd, (LPARAM)hWndNext);
662 else
663 dprintf_clipboard(stddeb,"ChangeClipboardChain: hWndViewer is lost\n");
665 if( hWnd == hWndViewer ) hWndViewer = hWndNext;
667 return 0;
671 /**************************************************************************
672 * IsClipboardFormatAvailable [USER.193]
674 BOOL IsClipboardFormatAvailable(WORD wFormat)
676 dprintf_clipboard(stddeb,"IsClipboardFormatAvailable(%04X) !\n", wFormat);
678 if( (wFormat == CF_TEXT || wFormat == CF_OEMTEXT) &&
679 !selectionAcquired ) CLIPBOARD_RequestXSelection();
681 return CLIPBOARD_IsPresent(wFormat);
685 /**************************************************************************
686 * GetOpenClipboardWindow [USER.248]
688 HWND GetOpenClipboardWindow()
690 dprintf_clipboard(stddeb,
691 "GetOpenClipboardWindow() = %04x\n", hWndClipWindow);
692 return hWndClipWindow;
696 /**************************************************************************
697 * GetPriorityClipboardFormat [USER.402]
699 int GetPriorityClipboardFormat(WORD *lpPriorityList, short nCount)
701 dprintf_clipboard(stdnimp,
702 "GetPriorityClipboardFormat(%p, %d) !\n", lpPriorityList, nCount);
704 return 0;
708 /**************************************************************************
709 * CLIPBOARD_ReadSelection
711 * Called from the SelectionNotify event handler.
713 void CLIPBOARD_ReadSelection(Window w,Atom prop)
715 HANDLE16 hText = 0;
716 LPCLIPFORMAT lpFormat = ClipFormats;
718 dprintf_clipboard(stddeb,"ReadSelection callback\n");
720 if(prop != None)
722 Atom atype=AnyPropertyType;
723 int aformat;
724 unsigned long nitems,remain;
725 unsigned char* val=NULL;
727 dprintf_clipboard(stddeb,"\tgot property %s\n",XGetAtomName(display,prop));
729 /* TODO: Properties longer than 64K */
731 if(XGetWindowProperty(display,w,prop,0,0x3FFF,True,XA_STRING,
732 &atype, &aformat, &nitems, &remain, &val) != Success)
733 dprintf_clipboard(stddeb,"\tcouldn't read property\n");
734 else
736 dprintf_clipboard(stddeb,"\tType %s,Format %d,nitems %ld,value %s\n",
737 XGetAtomName(display,atype),aformat,nitems,val);
739 if(atype == XA_STRING && aformat == 8)
741 int i,inlcount = 0;
742 char* lpstr;
744 dprintf_clipboard(stddeb,"\tselection is '%s'\n",val);
746 for(i=0; i <= nitems; i++)
747 if( val[i] == '\n' ) inlcount++;
749 if( nitems )
751 hText=GlobalAlloc16(GMEM_MOVEABLE, nitems + inlcount + 1);
752 if( (lpstr = (char*)GlobalLock16(hText)) )
753 for(i=0,inlcount=0; i <= nitems; i++)
755 if( val[i] == '\n' ) lpstr[inlcount++]='\r';
756 lpstr[inlcount++]=val[i];
758 else hText = 0;
761 XFree(val);
765 /* delete previous CF_TEXT and CF_OEMTEXT data */
767 if( hText )
769 lpFormat = &ClipFormats[CF_TEXT-1];
770 if (lpFormat->wDataPresent || lpFormat->hData)
771 CLIPBOARD_DeleteRecord(lpFormat);
772 lpFormat = &ClipFormats[CF_OEMTEXT-1];
773 if (lpFormat->wDataPresent || lpFormat->hData)
774 CLIPBOARD_DeleteRecord(lpFormat);
776 lpFormat->wDataPresent = 1;
777 lpFormat->hData = hText;
780 selectionWait=False;
783 /**************************************************************************
784 * CLIPBOARD_ReleaseSelection
786 * Wine might have lost XA_PRIMARY selection because of
787 * EmptyClipboard() or other client.
789 void CLIPBOARD_ReleaseSelection(Window w, HWND hwnd)
791 /* w is the window that lost selection,
793 * selectionPrevWindow is nonzero if CheckSelection() was called.
796 dprintf_clipboard(stddeb,"\tevent->window = %08x (sw = %08x, spw=%08x)\n",
797 (unsigned)w, (unsigned)selectionWindow, (unsigned)selectionPrevWindow );
799 if( selectionAcquired )
800 if( w == selectionWindow || selectionPrevWindow == None)
802 /* alright, we really lost it */
804 selectionAcquired = False;
805 selectionWindow = None;
807 /* but we'll keep existing data for internal use */
809 else if( w == selectionPrevWindow )
811 w = XGetSelectionOwner(display, XA_PRIMARY);
813 if( w == None )
814 XSetSelectionOwner(display, XA_PRIMARY, selectionWindow, CurrentTime);
817 selectionPrevWindow = None;