New stubs PrivateExtractIconEx[AW], PrivateExtractIconsW,
[wine] / windows / clipboard.c
1 /*
2  * WINE clipboard function handling
3  *
4  * Copyright 1994 Martin Ayotte
5  *           1996 Alex Korobka
6  *
7  */
8
9 #include <stdlib.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <unistd.h>
14 #include <string.h>
15 #include "ts_xlib.h"
16 #include <X11/Xatom.h>
17 #include "windows.h"
18 #include "win.h"
19 #include "heap.h"
20 #include "message.h"
21 #include "clipboard.h"
22 #include "xmalloc.h"
23 #include "debug.h"
24
25 #define  CF_REGFORMATBASE       0xC000
26
27 typedef struct tagCLIPFORMAT {
28     WORD        wFormatID;
29     WORD        wRefCount;
30     WORD        wDataPresent;
31     LPSTR       Name;
32     HANDLE16    hData;
33     DWORD       BufSize;
34     struct tagCLIPFORMAT *PrevFormat;
35     struct tagCLIPFORMAT *NextFormat;
36 } CLIPFORMAT, *LPCLIPFORMAT;
37
38 /* *************************************************************************
39  *                      internal variables
40  */
41
42 static HQUEUE16 hqClipLock   = 0;
43 static BOOL32 bCBHasChanged  = FALSE;
44
45 static HWND32 hWndClipOwner  = 0;   /* current clipboard owner */
46 static HWND32 hWndClipWindow = 0;   /* window that opened clipboard */
47 static HWND32 hWndViewer     = 0;   /* start of viewers chain */
48
49 static WORD LastRegFormat = CF_REGFORMATBASE;
50
51 static Bool   selectionWait = False;
52 static Bool   selectionAcquired = False;
53 static Window selectionWindow = None;
54 static Window selectionPrevWindow = None;
55
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 }
73     };
74
75 static LPCLIPFORMAT __lookup_format( LPCLIPFORMAT lpFormat, WORD wID )
76 {
77     while(TRUE)
78     {
79         if (lpFormat == NULL ||
80             lpFormat->wFormatID == wID) break;
81         lpFormat = lpFormat->NextFormat;
82     }
83     return lpFormat;
84 }
85
86 /**************************************************************************
87  *                      CLIPBOARD_CheckSelection
88  *
89  * Prevent X selection from being lost when a top level window is
90  * destroyed.
91  */
92 static void CLIPBOARD_CheckSelection(WND* pWnd)
93 {
94     TRACE(clipboard,"\tchecking %08x\n", (unsigned)pWnd->window);
95
96     if( selectionAcquired && selectionWindow != None &&
97         pWnd->window == selectionWindow )
98     {
99         selectionPrevWindow = selectionWindow;
100         selectionWindow = None;
101
102         if( pWnd->next ) 
103             selectionWindow = pWnd->next->window;
104         else if( pWnd->parent )
105              if( pWnd->parent->child != pWnd ) 
106                  selectionWindow = pWnd->parent->child->window;
107
108         TRACE(clipboard,"\tswitching selection from %08x to %08x\n", 
109                     (unsigned)selectionPrevWindow, (unsigned)selectionWindow);
110
111         if( selectionWindow != None )
112         {
113             TSXSetSelectionOwner(display, XA_PRIMARY, selectionWindow, CurrentTime);
114             if( TSXGetSelectionOwner(display, XA_PRIMARY) != selectionWindow )
115                 selectionWindow = None;
116         }
117     }
118 }
119
120 /**************************************************************************
121  *                      CLIPBOARD_ResetLock
122  */
123 void CLIPBOARD_ResetLock( HQUEUE16 hqCurrent, HQUEUE16 hqNew )
124 {
125     if( hqClipLock == hqCurrent )
126     {
127         if( hqNew )
128             hqClipLock = hqNew;
129         else
130         {
131             hWndClipOwner = 0;
132             hWndClipWindow = 0;
133             EmptyClipboard32();
134             hqClipLock = 0;
135         }
136     }
137 }
138
139 /**************************************************************************
140  *                      CLIPBOARD_ResetOwner
141  *
142  * Called from DestroyWindow().
143  */
144 void CLIPBOARD_ResetOwner(WND* pWnd)
145 {
146     LPCLIPFORMAT lpFormat = ClipFormats;
147
148     TRACE(clipboard,"clipboard owner = %04x, selection = %08x\n", 
149                                 hWndClipOwner, (unsigned)selectionWindow);
150
151     if( pWnd->hwndSelf == hWndClipOwner)
152     {
153         SendMessage16(hWndClipOwner,WM_RENDERALLFORMATS,0,0L);
154
155         /* check if all formats were rendered */
156
157         while(lpFormat)
158         {
159             if( lpFormat->wDataPresent && !lpFormat->hData )
160             {
161                 TRACE(clipboard,"\tdata missing for clipboard format %i\n", 
162                                    lpFormat->wFormatID); 
163                 lpFormat->wDataPresent = 0;
164             }
165             lpFormat = lpFormat->NextFormat;
166         }
167         hWndClipOwner = 0;
168     }
169
170     /* now try to salvage current selection from being destroyed by X */
171
172     if( pWnd->window ) CLIPBOARD_CheckSelection(pWnd);
173 }
174
175 /**************************************************************************
176  *                      CLIPBOARD_DeleteRecord
177  */
178 static void CLIPBOARD_DeleteRecord(LPCLIPFORMAT lpFormat, BOOL32 bChange)
179 {
180     if( (lpFormat->wFormatID >= CF_GDIOBJFIRST &&
181          lpFormat->wFormatID <= CF_GDIOBJLAST) || lpFormat->wFormatID == CF_BITMAP )
182         DeleteObject32(lpFormat->hData);
183     else if( lpFormat->wFormatID == CF_METAFILEPICT && lpFormat->hData )
184     {
185         DeleteMetaFile16( ((METAFILEPICT16 *)GlobalLock16( lpFormat->hData ))->hMF );
186         GlobalFree16(lpFormat->hData);
187     }
188     else if( lpFormat->hData )
189         GlobalFree16(lpFormat->hData);
190
191     lpFormat->wDataPresent = 0; 
192     lpFormat->hData = 0;
193
194     if( bChange ) bCBHasChanged = TRUE;
195 }
196
197 /**************************************************************************
198  *                      CLIPBOARD_RequestXSelection
199  */
200 static BOOL32 CLIPBOARD_RequestXSelection()
201 {
202     HWND32 hWnd = (hWndClipWindow) ? hWndClipWindow : GetActiveWindow32();
203
204     if( !hWnd ) return FALSE;
205
206     TRACE(clipboard,"Requesting selection...\n");
207
208   /* request data type XA_STRING, later
209    * CLIPBOARD_ReadSelection() will be invoked 
210    * from the SelectionNotify event handler */
211
212     TSXConvertSelection(display,XA_PRIMARY,XA_STRING,
213                       TSXInternAtom(display,"PRIMARY_TEXT",False),
214                       WIN_GetXWindow(hWnd),CurrentTime);
215
216   /* wait until SelectionNotify is processed 
217    *
218    * FIXME: Use TSXCheckTypedWindowEvent() instead ( same in the 
219    *        CLIPBOARD_CheckSelection() ). 
220    */
221
222     selectionWait=True;
223     while(selectionWait) EVENT_WaitNetEvent( TRUE, FALSE );
224
225   /* we treat Unix text as CF_OEMTEXT */
226     TRACE(clipboard,"\tgot CF_OEMTEXT = %i\n", 
227                       ClipFormats[CF_OEMTEXT-1].wDataPresent);
228
229     return (BOOL32)ClipFormats[CF_OEMTEXT-1].wDataPresent;
230 }
231
232 /**************************************************************************
233  *                      CLIPBOARD_IsPresent
234  */
235 BOOL32 CLIPBOARD_IsPresent(WORD wFormat)
236 {
237     /* special case */
238
239     if( wFormat == CF_TEXT || wFormat == CF_OEMTEXT )
240         return ClipFormats[CF_TEXT-1].wDataPresent ||
241                ClipFormats[CF_OEMTEXT-1].wDataPresent;
242     else
243     {
244         LPCLIPFORMAT lpFormat = __lookup_format( ClipFormats, wFormat );
245         if( lpFormat ) return (lpFormat->wDataPresent);
246     }
247     return FALSE;
248 }
249
250 /**************************************************************************
251  *            OpenClipboard16   (USER.137)
252  */
253 BOOL16 WINAPI OpenClipboard16( HWND16 hWnd )
254 {
255     return OpenClipboard32( hWnd );
256 }
257
258
259 /**************************************************************************
260  *            OpenClipboard32   (USER32.407)
261  *
262  * Note: Netscape uses NULL hWnd to open the clipboard.
263  */
264 BOOL32 WINAPI OpenClipboard32( HWND32 hWnd )
265 {
266     BOOL32 bRet;
267
268     TRACE(clipboard,"(%04x)...\n", hWnd);
269
270     if (!hqClipLock)
271     {
272          hqClipLock = GetTaskQueue(0);
273          hWndClipWindow = hWnd;
274          bCBHasChanged = FALSE;
275          bRet = TRUE;
276     }
277     else bRet = FALSE;
278
279     TRACE(clipboard,"   returning %i\n", bRet);
280     return bRet;
281 }
282
283
284 /**************************************************************************
285  *            CloseClipboard16   (USER.138)
286  */
287 BOOL16 WINAPI CloseClipboard16(void)
288 {
289     return CloseClipboard32();
290 }
291
292
293 /**************************************************************************
294  *            CloseClipboard32   (USER32.54)
295  */
296 BOOL32 WINAPI CloseClipboard32(void)
297 {
298     TRACE(clipboard,"!\n");
299
300     if (hqClipLock == GetTaskQueue(0))
301     {
302         hWndClipWindow = 0;
303
304         if (bCBHasChanged && hWndViewer) 
305             SendMessage16(hWndViewer, WM_DRAWCLIPBOARD, 0, 0L);
306         hqClipLock = 0;
307     }
308     return TRUE;
309 }
310
311
312 /**************************************************************************
313  *            EmptyClipboard16   (USER.139)
314  */
315 BOOL16 WINAPI EmptyClipboard16(void)
316 {
317     return EmptyClipboard32();
318 }
319
320
321 /**************************************************************************
322  *            EmptyClipboard32   (USER32.169)
323  */
324 BOOL32 WINAPI EmptyClipboard32(void)
325 {
326     LPCLIPFORMAT lpFormat = ClipFormats; 
327
328     TRACE(clipboard,"(void)\n");
329
330     if (hqClipLock != GetTaskQueue(0)) return FALSE;
331
332     /* destroy private objects */
333
334     if (hWndClipOwner)
335         SendMessage16(hWndClipOwner, WM_DESTROYCLIPBOARD, 0, 0L);
336   
337     while(lpFormat) 
338     {
339         if ( lpFormat->wDataPresent || lpFormat->hData )
340              CLIPBOARD_DeleteRecord( lpFormat, TRUE );
341
342         lpFormat = lpFormat->NextFormat;
343     }
344
345     hWndClipOwner = hWndClipWindow;
346
347     if(selectionAcquired)
348     {
349         selectionAcquired       = False;
350         selectionPrevWindow     = selectionWindow;
351         selectionWindow         = None;
352
353         TRACE(clipboard, "\tgiving up selection (spw = %08x)\n", 
354                                         (unsigned)selectionPrevWindow);
355
356         TSXSetSelectionOwner(display, XA_PRIMARY, None, CurrentTime);
357     }
358     return TRUE;
359 }
360
361
362 /**************************************************************************
363  *            GetClipboardOwner16   (USER.140)
364  */
365 HWND16 WINAPI GetClipboardOwner16(void)
366 {
367     return hWndClipOwner;
368 }
369
370
371 /**************************************************************************
372  *            GetClipboardOwner32   (USER32.225)
373  */
374 HWND32 WINAPI GetClipboardOwner32(void)
375 {
376     return hWndClipOwner;
377 }
378
379
380 /**************************************************************************
381  *            SetClipboardData16   (USER.141)
382  */
383 HANDLE16 WINAPI SetClipboardData16( UINT16 wFormat, HANDLE16 hData )
384 {
385     LPCLIPFORMAT lpFormat = __lookup_format( ClipFormats, wFormat );
386     Window       owner;
387
388     TRACE(clipboard, "(%04X, %04x) !\n", wFormat, hData);
389
390     /* NOTE: If the hData is zero and current owner doesn't match
391      * the window that opened the clipboard then this application
392      * is screwed because WM_RENDERFORMAT will go to the owner
393      * (to become the owner it must call EmptyClipboard() before
394      *  adding new data).
395      */
396
397     if( (hqClipLock != GetTaskQueue(0)) || !lpFormat ||
398         (!hData && (!hWndClipOwner || (hWndClipOwner != hWndClipWindow))) ) return 0; 
399
400     /* Acquire X selection if text format */
401
402     if( !selectionAcquired && 
403         (wFormat == CF_TEXT || wFormat == CF_OEMTEXT) )
404     {
405         owner = WIN_GetXWindow( hWndClipWindow ? hWndClipWindow : AnyPopup32() );
406         TSXSetSelectionOwner(display,XA_PRIMARY,owner,CurrentTime);
407         if( TSXGetSelectionOwner(display,XA_PRIMARY) == owner )
408         {
409             selectionAcquired = True;
410             selectionWindow = owner;
411
412             TRACE(clipboard,"Grabbed X selection, owner=(%08x)\n", 
413                                                 (unsigned) owner);
414         }
415     }
416
417     if ( lpFormat->wDataPresent || lpFormat->hData ) 
418     {
419         CLIPBOARD_DeleteRecord(lpFormat, TRUE);
420
421         /* delete existing CF_TEXT/CF_OEMTEXT aliases */
422
423         if( wFormat == CF_TEXT && ClipFormats[CF_OEMTEXT-1].hData
424             && !ClipFormats[CF_OEMTEXT-1].wDataPresent )
425             CLIPBOARD_DeleteRecord(&ClipFormats[CF_OEMTEXT-1], TRUE);
426         if( wFormat == CF_OEMTEXT && ClipFormats[CF_TEXT-1].hData
427             && !ClipFormats[CF_TEXT-1].wDataPresent )
428             CLIPBOARD_DeleteRecord(&ClipFormats[CF_TEXT-1], TRUE);
429     }
430
431     bCBHasChanged = TRUE;
432     lpFormat->wDataPresent = 1;
433     lpFormat->hData = hData;          /* 0 is legal, see WM_RENDERFORMAT */
434
435     return lpFormat->hData;
436 }
437
438
439 /**************************************************************************
440  *            SetClipboardData32   (USER32.470)
441  */
442 HANDLE32 WINAPI SetClipboardData32( UINT32 wFormat, HANDLE32 hData )
443 {
444     FIXME(clipboard, "(%d,%d): stub\n", wFormat, hData );
445     return 0;
446 }
447
448
449 /**************************************************************************
450  *                      CLIPBOARD_RenderFormat
451  */
452 static BOOL32 CLIPBOARD_RenderFormat(LPCLIPFORMAT lpFormat)
453 {
454    if( lpFormat->wDataPresent && !lpFormat->hData )
455    if( IsWindow32(hWndClipOwner) )
456        SendMessage16(hWndClipOwner,WM_RENDERFORMAT,
457                      (WPARAM16)lpFormat->wFormatID,0L);
458    else
459    {
460        WARN(clipboard, "\thWndClipOwner (%04x) is lost!\n", 
461                                       hWndClipOwner);
462        hWndClipOwner = 0; lpFormat->wDataPresent = 0;
463        return FALSE;
464    }
465    return (lpFormat->hData) ? TRUE : FALSE;
466 }
467
468 /**************************************************************************
469  *                      CLIPBOARD_RenderText
470  *
471  * Convert text between UNIX and DOS formats.
472  */
473 static BOOL32 CLIPBOARD_RenderText(LPCLIPFORMAT lpTarget, LPCLIPFORMAT lpSource)
474 {
475     UINT16 size = GlobalSize16( lpSource->hData );
476     LPCSTR lpstrS = (LPSTR)GlobalLock16(lpSource->hData);
477     LPSTR  lpstrT;
478
479     if( !lpstrS ) return FALSE;
480     TRACE(clipboard,"\tconverting from '%s' to '%s', %i chars\n",
481                                       lpSource->Name, lpTarget->Name, size);
482
483     lpTarget->hData = GlobalAlloc16(GMEM_ZEROINIT, size); 
484     lpstrT = (LPSTR)GlobalLock16(lpTarget->hData);
485
486     if( lpstrT )
487     {
488         if( lpSource->wFormatID == CF_TEXT )
489             CharToOemBuff32A(lpstrS, lpstrT, size);
490         else
491             OemToCharBuff32A(lpstrS, lpstrT, size);
492         TRACE(clipboard,"\tgot %s\n", lpstrT);
493         return TRUE;
494     }
495
496     lpTarget->hData = 0;
497     return FALSE;
498 }
499
500 /**************************************************************************
501  *             GetClipboardData16   (USER.142)
502  */
503 HANDLE16 WINAPI GetClipboardData16( UINT16 wFormat )
504 {
505     LPCLIPFORMAT lpRender = ClipFormats; 
506     LPCLIPFORMAT lpUpdate = NULL;
507
508     if (hqClipLock != GetTaskQueue(0)) return 0;
509
510     TRACE(clipboard,"(%04X)\n", wFormat);
511
512     if( wFormat == CF_TEXT && !lpRender[CF_TEXT-1].wDataPresent 
513                            &&  lpRender[CF_OEMTEXT-1].wDataPresent )
514     {
515         lpRender = &ClipFormats[CF_OEMTEXT-1];
516         lpUpdate = &ClipFormats[CF_TEXT-1];
517
518         TRACE(clipboard,"\tOEMTEXT -> TEXT\n");
519     }
520     else if( wFormat == CF_OEMTEXT && !lpRender[CF_OEMTEXT-1].wDataPresent
521                                    &&  lpRender[CF_TEXT-1].wDataPresent )
522     {
523         lpRender = &ClipFormats[CF_TEXT-1];
524         lpUpdate = &ClipFormats[CF_OEMTEXT-1];
525         
526         TRACE(clipboard,"\tTEXT -> OEMTEXT\n");
527     }
528     else
529     {
530         lpRender = __lookup_format( ClipFormats, wFormat );
531         lpUpdate = lpRender;
532     }
533    
534     if( !lpRender || !CLIPBOARD_RenderFormat(lpRender) ) return 0; 
535     if( lpUpdate != lpRender && !lpUpdate->hData ) 
536         CLIPBOARD_RenderText(lpUpdate, lpRender);
537
538     TRACE(clipboard,"\treturning %04x (type %i)\n", 
539                               lpUpdate->hData, lpUpdate->wFormatID);
540     return lpUpdate->hData;
541 }
542
543
544 /**************************************************************************
545  *             GetClipboardData32   (USER32.222)
546  */
547 HANDLE32 WINAPI GetClipboardData32( UINT32 wFormat )
548 {
549     FIXME(clipboard, "(%d): stub\n", wFormat );
550     return 0;
551 }
552
553 /**************************************************************************
554  *           CountClipboardFormats16   (USER.143)
555  */
556 INT16 WINAPI CountClipboardFormats16(void)
557 {
558     return CountClipboardFormats32();
559 }
560
561
562 /**************************************************************************
563  *           CountClipboardFormats32   (USER32.63)
564  */
565 INT32 WINAPI CountClipboardFormats32(void)
566 {
567     INT32 FormatCount = 0;
568     LPCLIPFORMAT lpFormat = ClipFormats; 
569
570     TRACE(clipboard,"(void)\n");
571
572     if( !selectionAcquired ) CLIPBOARD_RequestXSelection();
573
574     FormatCount += abs(lpFormat[CF_TEXT-1].wDataPresent -
575                        lpFormat[CF_OEMTEXT-1].wDataPresent); 
576
577     while(TRUE) 
578     {
579         if (lpFormat == NULL) break;
580         if (lpFormat->wDataPresent) 
581         {
582             TRACE(clipboard, "\tdata found for format %i\n", lpFormat->wFormatID);
583             FormatCount++;
584         }
585         lpFormat = lpFormat->NextFormat;
586     }
587
588     TRACE(clipboard,"\ttotal %d\n", FormatCount);
589     return FormatCount;
590 }
591
592
593 /**************************************************************************
594  *            EnumClipboardFormats16   (USER.144)
595  */
596 UINT16 WINAPI EnumClipboardFormats16( UINT16 wFormat )
597 {
598     return EnumClipboardFormats32( wFormat );
599 }
600
601
602 /**************************************************************************
603  *            EnumClipboardFormats32   (USER32.179)
604  */
605 UINT32 WINAPI EnumClipboardFormats32( UINT32 wFormat )
606 {
607     LPCLIPFORMAT lpFormat = ClipFormats; 
608
609     TRACE(clipboard,"(%04X)\n", wFormat);
610
611     if( hqClipLock != GetTaskQueue(0) ) return 0;
612
613     if( (!wFormat || wFormat == CF_TEXT || wFormat == CF_OEMTEXT) 
614          && !selectionAcquired) CLIPBOARD_RequestXSelection();
615
616     if (wFormat == 0)
617         if (lpFormat->wDataPresent || ClipFormats[CF_OEMTEXT-1].wDataPresent) 
618             return lpFormat->wFormatID;
619         else 
620             wFormat = lpFormat->wFormatID; /* and CF_TEXT is not available */
621
622     /* walk up to the specified format record */
623
624     if( !(lpFormat = __lookup_format( lpFormat, wFormat )) ) return 0;
625
626     /* find next format with available data */
627
628     lpFormat = lpFormat->NextFormat;
629     while(TRUE) 
630     {
631         if (lpFormat == NULL) return 0;
632         if (lpFormat->wDataPresent || (lpFormat->wFormatID == CF_OEMTEXT && 
633                                        ClipFormats[CF_TEXT-1].wDataPresent)) 
634             break;
635         lpFormat = lpFormat->NextFormat;
636     }
637
638     return lpFormat->wFormatID;
639 }
640
641
642 /**************************************************************************
643  *            RegisterClipboardFormat16  (USER.145)
644  */
645 UINT16 WINAPI RegisterClipboardFormat16( LPCSTR FormatName )
646 {
647     LPCLIPFORMAT lpNewFormat; 
648     LPCLIPFORMAT lpFormat = ClipFormats; 
649
650     if (FormatName == NULL) return 0;
651
652     TRACE(clipboard,"('%s') !\n", FormatName);
653
654     /* walk format chain to see if it's already registered */
655
656     while(TRUE) 
657     {
658         if ( !strcmp(lpFormat->Name,FormatName) )
659         {
660              lpFormat->wRefCount++;
661              return lpFormat->wFormatID;
662         }
663  
664         if ( lpFormat->NextFormat == NULL ) break;
665
666         lpFormat = lpFormat->NextFormat;
667     }
668
669     /* allocate storage for new format entry */
670
671     lpNewFormat = (LPCLIPFORMAT)xmalloc(sizeof(CLIPFORMAT));
672     lpFormat->NextFormat = lpNewFormat;
673     lpNewFormat->wFormatID = LastRegFormat;
674     lpNewFormat->wRefCount = 1;
675
676     lpNewFormat->Name = (LPSTR)xmalloc(strlen(FormatName) + 1);
677     strcpy(lpNewFormat->Name, FormatName);
678
679     lpNewFormat->wDataPresent = 0;
680     lpNewFormat->hData = 0;
681     lpNewFormat->BufSize = 0;
682     lpNewFormat->PrevFormat = lpFormat;
683     lpNewFormat->NextFormat = NULL;
684
685     return LastRegFormat++;
686 }
687
688
689 /**************************************************************************
690  *            RegisterClipboardFormat32A   (USER32.431)
691  */
692 UINT32 WINAPI RegisterClipboardFormat32A( LPCSTR formatName )
693 {
694     return RegisterClipboardFormat16( formatName );
695 }
696
697
698 /**************************************************************************
699  *            RegisterClipboardFormat32W   (USER32.432)
700  */
701 UINT32 WINAPI RegisterClipboardFormat32W( LPCWSTR formatName )
702 {
703     LPSTR aFormat = HEAP_strdupWtoA( GetProcessHeap(), 0, formatName );
704     UINT32 ret = RegisterClipboardFormat32A( aFormat );
705     HeapFree( GetProcessHeap(), 0, aFormat );
706     return ret;
707 }
708
709 /**************************************************************************
710  *            GetClipboardFormatName16   (USER.146)
711  */
712 INT16 WINAPI GetClipboardFormatName16( UINT16 wFormat, LPSTR retStr, INT16 maxlen )
713 {
714     return GetClipboardFormatName32A( wFormat, retStr, maxlen );
715 }
716
717
718 /**************************************************************************
719  *            GetClipboardFormatName32A   (USER32.223)
720  */
721 INT32 WINAPI GetClipboardFormatName32A( UINT32 wFormat, LPSTR retStr, INT32 maxlen )
722 {
723     LPCLIPFORMAT lpFormat = __lookup_format( ClipFormats, wFormat );
724
725     TRACE(clipboard, "(%04X, %p, %d) !\n", wFormat, retStr, maxlen);
726
727     if (lpFormat == NULL || lpFormat->Name == NULL || 
728         lpFormat->wFormatID < CF_REGFORMATBASE) return 0;
729
730     TRACE(clipboard, "Name='%s' !\n", lpFormat->Name);
731
732     lstrcpyn32A( retStr, lpFormat->Name, maxlen );
733     return strlen(retStr);
734 }
735
736
737 /**************************************************************************
738  *            GetClipboardFormatName32W   (USER32.224)
739  */
740 INT32 WINAPI GetClipboardFormatName32W( UINT32 wFormat, LPWSTR retStr, INT32 maxlen )
741 {
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;
747 }
748
749
750 /**************************************************************************
751  *            SetClipboardViewer16   (USER.147)
752  */
753 HWND16 WINAPI SetClipboardViewer16( HWND16 hWnd )
754 {
755     return SetClipboardViewer32( hWnd );
756 }
757
758
759 /**************************************************************************
760  *            SetClipboardViewer32   (USER32.471)
761  */
762 HWND32 WINAPI SetClipboardViewer32( HWND32 hWnd )
763 {
764     HWND32 hwndPrev = hWndViewer;
765
766     TRACE(clipboard,"(%04x): returning %04x\n", hWnd, hwndPrev);
767
768     hWndViewer = hWnd;
769     return hwndPrev;
770 }
771
772
773 /**************************************************************************
774  *           GetClipboardViewer16   (USER.148)
775  */
776 HWND16 WINAPI GetClipboardViewer16(void)
777 {
778     return hWndViewer;
779 }
780
781
782 /**************************************************************************
783  *           GetClipboardViewer32   (USER32.226)
784  */
785 HWND32 WINAPI GetClipboardViewer32(void)
786 {
787     return hWndViewer;
788 }
789
790
791 /**************************************************************************
792  *           ChangeClipboardChain16   (USER.149)
793  */
794 BOOL16 WINAPI ChangeClipboardChain16(HWND16 hWnd, HWND16 hWndNext)
795 {
796     return ChangeClipboardChain32(hWnd, hWndNext);
797 }
798
799 /**************************************************************************
800  *           ChangeClipboardChain32   (USER32.22)
801  */
802 BOOL32 WINAPI ChangeClipboardChain32(HWND32 hWnd, HWND32 hWndNext)
803 {
804     BOOL32 bRet = 0;
805
806     FIXME(clipboard, "(0x%04x, 0x%04x): stub?\n", hWnd, hWndNext);
807
808     if( hWndViewer )
809         bRet = !SendMessage16( hWndViewer, WM_CHANGECBCHAIN,
810                              (WPARAM16)hWnd, (LPARAM)hWndNext);   
811     else
812         WARN(clipboard, "hWndViewer is lost\n");
813
814     if( hWnd == hWndViewer ) hWndViewer = hWndNext;
815
816     return bRet;
817 }
818
819
820
821 /**************************************************************************
822  *           IsClipboardFormatAvailable16   (USER.193)
823  */
824 BOOL16 WINAPI IsClipboardFormatAvailable16( UINT16 wFormat )
825 {
826     return IsClipboardFormatAvailable32( wFormat );
827 }
828
829
830 /**************************************************************************
831  *           IsClipboardFormatAvailable32   (USER32.340)
832  */
833 BOOL32 WINAPI IsClipboardFormatAvailable32( UINT32 wFormat )
834 {
835     TRACE(clipboard,"(%04X) !\n", wFormat);
836
837     if( (wFormat == CF_TEXT || wFormat == CF_OEMTEXT) &&
838         !selectionAcquired ) CLIPBOARD_RequestXSelection();
839
840     return CLIPBOARD_IsPresent(wFormat);
841 }
842
843
844 /**************************************************************************
845  *             GetOpenClipboardWindow16   (USER.248)
846  */
847 HWND16 WINAPI GetOpenClipboardWindow16(void)
848 {
849     return hWndClipWindow;
850 }
851
852
853 /**************************************************************************
854  *             GetOpenClipboardWindow32   (USER32.277)
855  */
856 HWND32 WINAPI GetOpenClipboardWindow32(void)
857 {
858     return hWndClipWindow;
859 }
860
861
862 /**************************************************************************
863  *             GetPriorityClipboardFormat16   (USER.402)
864  */
865 INT16 WINAPI GetPriorityClipboardFormat16( UINT16 *lpPriorityList, INT16 nCount)
866 {
867     FIXME(clipboard, "(%p,%d): stub\n", lpPriorityList, nCount );
868     return 0;
869 }
870
871
872 /**************************************************************************
873  *             GetPriorityClipboardFormat32   (USER32.279)
874  */
875 INT32 WINAPI GetPriorityClipboardFormat32( UINT32 *lpPriorityList, INT32 nCount )
876 {
877     int Counter;
878
879     if(CountClipboardFormats32() == 0) 
880     { 
881         return 0;
882     }
883
884     for(Counter = 0; Counter <= nCount; Counter++)
885     {
886         if(IsClipboardFormatAvailable32(*(lpPriorityList+sizeof(INT32)*Counter)))
887             return *(lpPriorityList+sizeof(INT32)*Counter);
888     }
889
890     return -1;
891 }
892
893
894 /**************************************************************************
895  *                      CLIPBOARD_ReadSelection
896  *
897  * Called from the SelectionNotify event handler. 
898  */
899 void CLIPBOARD_ReadSelection(Window w,Atom prop)
900 {
901     HANDLE16     hText = 0;
902     LPCLIPFORMAT lpFormat = ClipFormats; 
903
904     TRACE(clipboard,"ReadSelection callback\n");
905
906     if(prop != None)
907     {
908         Atom            atype=AnyPropertyType;
909         int             aformat;
910         unsigned long   nitems,remain;
911         unsigned char*  val=NULL;
912
913         TRACE(clipboard,"\tgot property %s\n",TSXGetAtomName(display,prop));
914
915         /* TODO: Properties longer than 64K */
916
917         if(TSXGetWindowProperty(display,w,prop,0,0x3FFF,True,XA_STRING,
918             &atype, &aformat, &nitems, &remain, &val) != Success)
919             WARN(clipboard, "\tcouldn't read property\n");
920         else
921         {
922            TRACE(clipboard,"\tType %s,Format %d,nitems %ld,value %s\n",
923                              TSXGetAtomName(display,atype),aformat,nitems,val);
924
925            if(atype == XA_STRING && aformat == 8)
926            {
927               int       i,inlcount = 0;
928               char*     lpstr;
929
930               TRACE(clipboard,"\tselection is '%s'\n",val);
931
932               for(i=0; i <= nitems; i++)
933                   if( val[i] == '\n' ) inlcount++;
934
935               if( nitems )
936               {
937                 hText=GlobalAlloc16(GMEM_MOVEABLE, nitems + inlcount + 1);
938                 if( (lpstr = (char*)GlobalLock16(hText)) )
939                   for(i=0,inlcount=0; i <= nitems; i++)
940                   {
941                      if( val[i] == '\n' ) lpstr[inlcount++]='\r';
942                      lpstr[inlcount++]=val[i];
943                   }
944                 else hText = 0;
945               }
946            }
947            TSXFree(val);
948         }
949    }
950
951    /* delete previous CF_TEXT and CF_OEMTEXT data */
952
953    if( hText )
954    {
955      lpFormat = &ClipFormats[CF_TEXT-1];
956      if (lpFormat->wDataPresent || lpFormat->hData) 
957          CLIPBOARD_DeleteRecord(lpFormat, !(hWndClipWindow));
958      lpFormat = &ClipFormats[CF_OEMTEXT-1];
959      if (lpFormat->wDataPresent || lpFormat->hData) 
960          CLIPBOARD_DeleteRecord(lpFormat, !(hWndClipWindow));
961
962      lpFormat->wDataPresent = 1;
963      lpFormat->hData = hText;
964    }
965
966    selectionWait=False;
967 }
968
969 /**************************************************************************
970  *                      CLIPBOARD_ReleaseSelection
971  *
972  * Wine might have lost XA_PRIMARY selection because of
973  * EmptyClipboard() or other client. 
974  */
975 void CLIPBOARD_ReleaseSelection(Window w, HWND32 hwnd)
976 {
977     /* w is the window that lost selection,
978      * 
979      * selectionPrevWindow is nonzero if CheckSelection() was called. 
980      */
981
982     TRACE(clipboard,"\tevent->window = %08x (sw = %08x, spw=%08x)\n", 
983           (unsigned)w, (unsigned)selectionWindow, (unsigned)selectionPrevWindow );
984
985     if( selectionAcquired )
986         if( w == selectionWindow || selectionPrevWindow == None)
987         {
988             /* alright, we really lost it */
989
990             selectionAcquired = False;
991             selectionWindow = None; 
992
993             /* but we'll keep existing data for internal use */
994         }
995         else if( w == selectionPrevWindow )
996         {
997             w = TSXGetSelectionOwner(display, XA_PRIMARY);
998             if( w == None )
999                 TSXSetSelectionOwner(display, XA_PRIMARY, selectionWindow, CurrentTime);
1000         }
1001
1002     selectionPrevWindow = None;
1003 }
1004