Some more recursive include fixes/optimizations.
[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 "winuser.h"
16 #include "heap.h"
17 #include "task.h"
18 #include "message.h"
19 #include "clipboard.h"
20 #include "xmalloc.h"
21 #include "debug.h"
22
23 #ifndef X_DISPLAY_MISSING
24 extern CLIPBOARD_DRIVER X11DRV_CLIPBOARD_Driver;
25 #else /* X_DISPLAY_MISSING */
26 extern CLIPBOARD_DRIVER TTYDRV_CLIPBOARD_Driver;
27 #endif /* X_DISPLAY_MISSING */
28
29 #define  CF_REGFORMATBASE       0xC000
30
31 /**************************************************************************
32  *                      internal variables
33  */
34
35 static HQUEUE16 hqClipLock   = 0;
36 static BOOL32 bCBHasChanged  = FALSE;
37
38 HWND32 hWndClipOwner  = 0;   /* current clipboard owner */
39 HWND32 hWndClipWindow = 0;   /* window that opened clipboard */
40 static HWND32 hWndViewer     = 0;   /* start of viewers chain */
41
42 static WORD LastRegFormat = CF_REGFORMATBASE;
43
44 CLIPFORMAT ClipFormats[16]  = {
45     { CF_TEXT, 1, 0, "Text", (HANDLE32)NULL, 0, NULL, &ClipFormats[1] , (HANDLE16)NULL},
46     { CF_BITMAP, 1, 0, "Bitmap", (HANDLE32)NULL, 0, &ClipFormats[0], &ClipFormats[2] , (HANDLE16)NULL},
47     { CF_METAFILEPICT, 1, 0, "MetaFile Picture", (HANDLE32)NULL, 0, &ClipFormats[1], &ClipFormats[3] , (HANDLE16)NULL},
48     { CF_SYLK, 1, 0, "Sylk", (HANDLE32)NULL, 0, &ClipFormats[2], &ClipFormats[4] , (HANDLE16)NULL},
49     { CF_DIF, 1, 0, "DIF", (HANDLE32)NULL, 0, &ClipFormats[3], &ClipFormats[5] , (HANDLE16)NULL},
50     { CF_TIFF, 1, 0, "TIFF", (HANDLE32)NULL, 0, &ClipFormats[4], &ClipFormats[6] , (HANDLE16)NULL},
51     { CF_OEMTEXT, 1, 0, "OEM Text", (HANDLE32)NULL, 0, &ClipFormats[5], &ClipFormats[7] , (HANDLE16)NULL},
52     { CF_DIB, 1, 0, "DIB", (HANDLE32)NULL, 0, &ClipFormats[6], &ClipFormats[8] , (HANDLE16)NULL},
53     { CF_PALETTE, 1, 0, "Palette", (HANDLE32)NULL, 0, &ClipFormats[7], &ClipFormats[9] , (HANDLE16)NULL},
54     { CF_PENDATA, 1, 0, "PenData", (HANDLE32)NULL, 0, &ClipFormats[8], &ClipFormats[10] , (HANDLE16)NULL},
55     { CF_RIFF, 1, 0, "RIFF", (HANDLE32)NULL, 0, &ClipFormats[9], &ClipFormats[11] , (HANDLE16)NULL},
56     { CF_WAVE, 1, 0, "Wave", (HANDLE32)NULL, 0, &ClipFormats[10], &ClipFormats[12] , (HANDLE16)NULL},
57     { CF_OWNERDISPLAY, 1, 0, "Owner Display", (HANDLE32)NULL, 0, &ClipFormats[11], &ClipFormats[13] , (HANDLE16)NULL},
58     { CF_DSPTEXT, 1, 0, "DSPText", (HANDLE32)NULL, 0, &ClipFormats[12], &ClipFormats[14] , (HANDLE16)NULL},
59     { CF_DSPMETAFILEPICT, 1, 0, "DSPMetaFile Picture", (HANDLE32)NULL, 0, &ClipFormats[13], &ClipFormats[15] , (HANDLE16)NULL},
60     { CF_DSPBITMAP, 1, 0, "DSPBitmap", (HANDLE32)NULL, 0, &ClipFormats[14], NULL , (HANDLE16)NULL}
61     };
62
63 static LPCLIPFORMAT __lookup_format( LPCLIPFORMAT lpFormat, WORD wID )
64 {
65     while(TRUE)
66     {
67         if (lpFormat == NULL ||
68             lpFormat->wFormatID == wID) break;
69         lpFormat = lpFormat->NextFormat;
70     }
71     return lpFormat;
72 }
73
74
75 /**************************************************************************
76  *              CLIPBOARD_GetDriver
77  */
78 CLIPBOARD_DRIVER *CLIPBOARD_GetDriver()
79 {
80 #ifndef X_DISPLAY_MISSING
81   return &X11DRV_CLIPBOARD_Driver;
82 #else /* X_DISPLAY_MISSING */
83   return &TTYDRV_CLIPBOARD_Driver;
84 #endif /* X_DISPLAY_MISSING */
85 };
86
87 /**************************************************************************
88  *                      CLIPBOARD_ResetLock
89  */
90 void CLIPBOARD_ResetLock( HQUEUE16 hqCurrent, HQUEUE16 hqNew )
91 {
92     if( hqClipLock == hqCurrent )
93     {
94         if( hqNew )
95             hqClipLock = hqNew;
96         else
97         {
98             hWndClipOwner = 0;
99             hWndClipWindow = 0;
100             EmptyClipboard32();
101             hqClipLock = 0;
102         }
103     }
104 }
105
106
107 /**************************************************************************
108  *                      CLIPBOARD_DeleteRecord
109  */
110 void CLIPBOARD_DeleteRecord(LPCLIPFORMAT lpFormat, BOOL32 bChange)
111 {
112     if( (lpFormat->wFormatID >= CF_GDIOBJFIRST &&
113          lpFormat->wFormatID <= CF_GDIOBJLAST) || lpFormat->wFormatID == CF_BITMAP )
114     {
115       if (lpFormat->hData32)
116         DeleteObject32(lpFormat->hData32);
117       if (lpFormat->hData16)
118         DeleteObject16(lpFormat->hData16);
119     }
120     else if( lpFormat->wFormatID == CF_METAFILEPICT )
121     {
122       if (lpFormat->hData32)
123       {
124         DeleteMetaFile32( ((METAFILEPICT32 *)GlobalLock32( lpFormat->hData32 ))->hMF );
125         GlobalFree32(lpFormat->hData32);
126         if (lpFormat->hData16)
127           /* HMETAFILE16 and HMETAFILE32 are apparently the same thing, 
128              and a shallow copy is enough to share a METAFILEPICT
129              structure between 16bit and 32bit clipboards.  The MetaFile
130              should of course only be deleted once. */
131           GlobalFree16(lpFormat->hData16);
132       }
133       else if (lpFormat->hData16)
134       {
135         DeleteMetaFile16( ((METAFILEPICT16 *)GlobalLock16( lpFormat->hData16 ))->hMF );
136         GlobalFree16(lpFormat->hData16);
137       }
138     }
139     else 
140     {
141       if (lpFormat->hData32)
142         GlobalFree32(lpFormat->hData32);
143       if (lpFormat->hData16)
144         GlobalFree16(lpFormat->hData16);
145     }
146
147     lpFormat->wDataPresent = 0; 
148     lpFormat->hData16 = 0;
149     lpFormat->hData32 = 0;
150
151     if( bChange ) bCBHasChanged = TRUE;
152 }
153
154 /**************************************************************************
155  *                      CLIPBOARD_IsPresent
156  */
157 BOOL32 CLIPBOARD_IsPresent(WORD wFormat)
158 {
159     /* special case */
160
161     if( wFormat == CF_TEXT || wFormat == CF_OEMTEXT )
162         return ClipFormats[CF_TEXT-1].wDataPresent ||
163                ClipFormats[CF_OEMTEXT-1].wDataPresent;
164     else
165     {
166         LPCLIPFORMAT lpFormat = __lookup_format( ClipFormats, wFormat );
167         if( lpFormat ) return (lpFormat->wDataPresent);
168     }
169     return FALSE;
170 }
171
172 /**************************************************************************
173  *            OpenClipboard16   (USER.137)
174  */
175 BOOL16 WINAPI OpenClipboard16( HWND16 hWnd )
176 {
177     return OpenClipboard32( hWnd );
178 }
179
180
181 /**************************************************************************
182  *            OpenClipboard32   (USER32.407)
183  *
184  * Note: Netscape uses NULL hWnd to open the clipboard.
185  */
186 BOOL32 WINAPI OpenClipboard32( HWND32 hWnd )
187 {
188     BOOL32 bRet;
189
190     TRACE(clipboard,"(%04x)...\n", hWnd);
191
192     if (!hqClipLock)
193     {
194          hqClipLock = GetFastQueue();
195          hWndClipWindow = hWnd;
196          bCBHasChanged = FALSE;
197          bRet = TRUE;
198     }
199     else bRet = FALSE;
200
201     TRACE(clipboard,"   returning %i\n", bRet);
202     return bRet;
203 }
204
205
206 /**************************************************************************
207  *            CloseClipboard16   (USER.138)
208  */
209 BOOL16 WINAPI CloseClipboard16(void)
210 {
211     return CloseClipboard32();
212 }
213
214
215 /**************************************************************************
216  *            CloseClipboard32   (USER32.54)
217  */
218 BOOL32 WINAPI CloseClipboard32(void)
219 {
220     TRACE(clipboard,"!\n");
221
222     if (hqClipLock == GetFastQueue())
223     {
224         hWndClipWindow = 0;
225
226         if (bCBHasChanged && hWndViewer) 
227             SendMessage16(hWndViewer, WM_DRAWCLIPBOARD, 0, 0L);
228         hqClipLock = 0;
229     }
230     return TRUE;
231 }
232
233
234 /**************************************************************************
235  *            EmptyClipboard16   (USER.139)
236  */
237 BOOL16 WINAPI EmptyClipboard16(void)
238 {
239     return EmptyClipboard32();
240 }
241
242
243 /**************************************************************************
244  *            EmptyClipboard32   (USER32.169)
245  */
246 BOOL32 WINAPI EmptyClipboard32(void)
247 {
248     LPCLIPFORMAT lpFormat = ClipFormats; 
249
250     TRACE(clipboard,"(void)\n");
251
252     if (hqClipLock != GetFastQueue()) return FALSE;
253
254     /* destroy private objects */
255
256     if (hWndClipOwner)
257         SendMessage16(hWndClipOwner, WM_DESTROYCLIPBOARD, 0, 0L);
258   
259     while(lpFormat) 
260     {
261         if ( lpFormat->wDataPresent || lpFormat->hData16 || lpFormat->hData32 )
262              CLIPBOARD_DeleteRecord( lpFormat, TRUE );
263
264         lpFormat = lpFormat->NextFormat;
265     }
266
267     hWndClipOwner = hWndClipWindow;
268
269     CLIPBOARD_GetDriver()->pEmptyClipboard();
270
271     return TRUE;
272 }
273
274
275 /**************************************************************************
276  *            GetClipboardOwner16   (USER.140)
277  */
278 HWND16 WINAPI GetClipboardOwner16(void)
279 {
280     return hWndClipOwner;
281 }
282
283
284 /**************************************************************************
285  *            GetClipboardOwner32   (USER32.225)
286  */
287 HWND32 WINAPI GetClipboardOwner32(void)
288 {
289     return hWndClipOwner;
290 }
291
292
293 /**************************************************************************
294  *            SetClipboardData16   (USER.141)
295  */
296 HANDLE16 WINAPI SetClipboardData16( UINT16 wFormat, HANDLE16 hData )
297 {
298     LPCLIPFORMAT lpFormat = __lookup_format( ClipFormats, wFormat );
299
300     TRACE(clipboard, "(%04X, %04x) !\n", wFormat, hData);
301
302     /* NOTE: If the hData is zero and current owner doesn't match
303      * the window that opened the clipboard then this application
304      * is screwed because WM_RENDERFORMAT will go to the owner
305      * (to become the owner it must call EmptyClipboard() before
306      *  adding new data).
307      */
308
309     if( (hqClipLock != GetFastQueue()) || !lpFormat ||
310         (!hData && (!hWndClipOwner || (hWndClipOwner != hWndClipWindow))) ) return 0; 
311
312     CLIPBOARD_GetDriver()->pSetClipboardData(wFormat);
313
314     if ( lpFormat->wDataPresent || lpFormat->hData16 || lpFormat->hData32 ) 
315     {
316         CLIPBOARD_DeleteRecord(lpFormat, TRUE);
317
318         /* delete existing CF_TEXT/CF_OEMTEXT aliases */
319
320         if( wFormat == CF_TEXT 
321             && ( ClipFormats[CF_OEMTEXT-1].hData16 
322                  ||  ClipFormats[CF_OEMTEXT-1].hData32 )
323             && !ClipFormats[CF_OEMTEXT-1].wDataPresent )
324             CLIPBOARD_DeleteRecord(&ClipFormats[CF_OEMTEXT-1], TRUE);
325         if( wFormat == CF_OEMTEXT 
326             && ( ClipFormats[CF_OEMTEXT-1].hData16 
327                  ||  ClipFormats[CF_OEMTEXT-1].hData32 )
328             && !ClipFormats[CF_TEXT-1].wDataPresent )
329             CLIPBOARD_DeleteRecord(&ClipFormats[CF_TEXT-1], TRUE);
330     }
331
332     bCBHasChanged = TRUE;
333     lpFormat->wDataPresent = 1;
334     lpFormat->hData16 = hData;          /* 0 is legal, see WM_RENDERFORMAT */
335     lpFormat->hData32 = 0;
336
337     return lpFormat->hData16;
338 }
339
340
341 /**************************************************************************
342  *            SetClipboardData32   (USER32.470)
343  */
344 HANDLE32 WINAPI SetClipboardData32( UINT32 wFormat, HANDLE32 hData )
345 {
346     LPCLIPFORMAT lpFormat = __lookup_format( ClipFormats, wFormat );
347
348     TRACE(clipboard, "(%08X, %08x) !\n", wFormat, hData);
349
350     /* NOTE: If the hData is zero and current owner doesn't match
351      * the window that opened the clipboard then this application
352      * is screwed because WM_RENDERFORMAT will go to the owner
353      * (to become the owner it must call EmptyClipboard() before
354      *  adding new data).
355      */
356
357     if( (hqClipLock != GetFastQueue()) || !lpFormat ||
358         (!hData && (!hWndClipOwner || (hWndClipOwner != hWndClipWindow))) ) return 0; 
359
360     CLIPBOARD_GetDriver()->pSetClipboardData(wFormat);
361
362     if ( lpFormat->wDataPresent || lpFormat->hData16 || lpFormat->hData32 ) 
363     {
364         CLIPBOARD_DeleteRecord(lpFormat, TRUE);
365
366         /* delete existing CF_TEXT/CF_OEMTEXT aliases */
367
368         if( wFormat == CF_TEXT 
369             && ( ClipFormats[CF_OEMTEXT-1].hData16 
370                  ||  ClipFormats[CF_OEMTEXT-1].hData32 )
371             && !ClipFormats[CF_OEMTEXT-1].wDataPresent )
372             CLIPBOARD_DeleteRecord(&ClipFormats[CF_OEMTEXT-1], TRUE);
373         if( wFormat == CF_OEMTEXT 
374             && ( ClipFormats[CF_OEMTEXT-1].hData16 
375                  ||  ClipFormats[CF_OEMTEXT-1].hData32 )
376             && !ClipFormats[CF_TEXT-1].wDataPresent )
377             CLIPBOARD_DeleteRecord(&ClipFormats[CF_TEXT-1], TRUE);
378     }
379
380     bCBHasChanged = TRUE;
381     lpFormat->wDataPresent = 1;
382     lpFormat->hData32 = hData;          /* 0 is legal, see WM_RENDERFORMAT */
383     lpFormat->hData16 = 0;
384
385     return lpFormat->hData32;
386 }
387
388
389 /**************************************************************************
390  *                      CLIPBOARD_RenderFormat
391  */
392 static BOOL32 CLIPBOARD_RenderFormat(LPCLIPFORMAT lpFormat)
393 {
394   if( lpFormat->wDataPresent && !lpFormat->hData16 && !lpFormat->hData32 )
395   {
396     if( IsWindow32(hWndClipOwner) )
397       SendMessage16(hWndClipOwner,WM_RENDERFORMAT,
398                     (WPARAM16)lpFormat->wFormatID,0L);
399     else
400     {
401       WARN(clipboard, "\thWndClipOwner (%04x) is lost!\n", 
402            hWndClipOwner);
403       hWndClipOwner = 0; lpFormat->wDataPresent = 0;
404       return FALSE;
405     }
406   }
407   return (lpFormat->hData16 || lpFormat->hData32) ? TRUE : FALSE;
408 }
409
410 /**************************************************************************
411  *                      CLIPBOARD_RenderText
412  *
413  * Convert text between UNIX and DOS formats.
414  */
415 static BOOL32 CLIPBOARD_RenderText(LPCLIPFORMAT lpTarget, LPCLIPFORMAT lpSource)
416 {
417     UINT16 size;
418     LPCSTR lpstrS; 
419     LPSTR  lpstrT;
420
421     if (lpSource->hData32)
422     {
423       size = GlobalSize32( lpSource->hData32 );
424       lpstrS = (LPSTR)GlobalLock32(lpSource->hData32);
425     }
426     else
427     {
428       size = GlobalSize16( lpSource->hData16 );
429       lpstrS = (LPSTR)GlobalLock16(lpSource->hData16);
430     }
431
432     if( !lpstrS ) return FALSE;
433     TRACE(clipboard,"\tconverting from '%s' to '%s', %i chars\n",
434                                       lpSource->Name, lpTarget->Name, size);
435
436     lpTarget->hData32 = GlobalAlloc32(GMEM_ZEROINIT, size); 
437     lpstrT = (LPSTR)GlobalLock32(lpTarget->hData32);
438
439     if( lpstrT )
440     {
441         if( lpSource->wFormatID == CF_TEXT )
442             CharToOemBuff32A(lpstrS, lpstrT, size);
443         else
444             OemToCharBuff32A(lpstrS, lpstrT, size);
445         TRACE(clipboard,"\tgot %s\n", lpstrT);
446         GlobalUnlock32(lpTarget->hData32);
447         if (lpSource->hData32)
448           GlobalUnlock32(lpSource->hData32);
449         else
450           GlobalUnlock16(lpSource->hData16);
451         return TRUE;
452     }
453
454     lpTarget->hData32 = 0;
455     if (lpSource->hData32)
456       GlobalUnlock32(lpSource->hData32);
457     else
458       GlobalUnlock16(lpSource->hData16);
459     return FALSE;
460 }
461
462 /**************************************************************************
463  *             GetClipboardData16   (USER.142)
464  */
465 HANDLE16 WINAPI GetClipboardData16( UINT16 wFormat )
466 {
467     LPCLIPFORMAT lpRender = ClipFormats; 
468     LPCLIPFORMAT lpUpdate = NULL;
469
470     if (hqClipLock != GetFastQueue()) return 0;
471
472     TRACE(clipboard,"(%04X)\n", wFormat);
473
474     if( wFormat == CF_TEXT && !lpRender[CF_TEXT-1].wDataPresent 
475                            &&  lpRender[CF_OEMTEXT-1].wDataPresent )
476     {
477         lpRender = &ClipFormats[CF_OEMTEXT-1];
478         lpUpdate = &ClipFormats[CF_TEXT-1];
479
480         TRACE(clipboard,"\tOEMTEXT -> TEXT\n");
481     }
482     else if( wFormat == CF_OEMTEXT && !lpRender[CF_OEMTEXT-1].wDataPresent
483                                    &&  lpRender[CF_TEXT-1].wDataPresent )
484     {
485         lpRender = &ClipFormats[CF_TEXT-1];
486         lpUpdate = &ClipFormats[CF_OEMTEXT-1];
487         
488         TRACE(clipboard,"\tTEXT -> OEMTEXT\n");
489     }
490     else
491     {
492         lpRender = __lookup_format( ClipFormats, wFormat );
493         lpUpdate = lpRender;
494     }
495    
496     if( !lpRender || !CLIPBOARD_RenderFormat(lpRender) ) return 0; 
497     if( lpUpdate != lpRender && !lpUpdate->hData16 && !lpUpdate->hData32 ) 
498         CLIPBOARD_RenderText(lpUpdate, lpRender);
499
500     if( lpUpdate->hData32 && !lpUpdate->hData16 )
501     {
502       int size;
503       if( lpUpdate->wFormatID == CF_METAFILEPICT )
504         size = sizeof( METAFILEPICT16 );
505       else
506         size = GlobalSize32(lpUpdate->hData32);
507       lpUpdate->hData16 = GlobalAlloc16(GMEM_ZEROINIT, size);
508       if( !lpUpdate->hData16 )
509         ERR(clipboard, "(%04X) -- not enough memory in 16b heap\n", wFormat);
510       else
511       {
512       if( lpUpdate->wFormatID == CF_METAFILEPICT )
513       {
514         FIXME(clipboard,"\timplement function CopyMetaFilePict32to16\n");
515         FIXME(clipboard,"\tin the appropriate file.\n");
516 #ifdef SOMEONE_IMPLEMENTED_ME
517         CopyMetaFilePict32to16( GlobalLock16(lpUpdate->hData16), 
518                                 GlobalLock32(lpUpdate->hData32) );
519 #endif
520       }
521       else
522       {
523         memcpy( GlobalLock16(lpUpdate->hData16), 
524                 GlobalLock32(lpUpdate->hData32), 
525                 size );
526       }
527         GlobalUnlock16(lpUpdate->hData16);
528         GlobalUnlock32(lpUpdate->hData32);
529       }
530     }
531
532     TRACE(clipboard,"\treturning %04x (type %i)\n", 
533                               lpUpdate->hData16, lpUpdate->wFormatID);
534     return lpUpdate->hData16;
535 }
536
537
538 /**************************************************************************
539  *             GetClipboardData32   (USER32.222)
540  */
541 HANDLE32 WINAPI GetClipboardData32( UINT32 wFormat )
542 {
543     LPCLIPFORMAT lpRender = ClipFormats; 
544     LPCLIPFORMAT lpUpdate = NULL;
545
546     if (hqClipLock != GetFastQueue()) return 0;
547
548     TRACE(clipboard,"(%08X)\n", wFormat);
549
550     if( wFormat == CF_TEXT && !lpRender[CF_TEXT-1].wDataPresent 
551                            &&  lpRender[CF_OEMTEXT-1].wDataPresent )
552     {
553         lpRender = &ClipFormats[CF_OEMTEXT-1];
554         lpUpdate = &ClipFormats[CF_TEXT-1];
555
556         TRACE(clipboard,"\tOEMTEXT -> TEXT\n");
557     }
558     else if( wFormat == CF_OEMTEXT && !lpRender[CF_OEMTEXT-1].wDataPresent
559                                    &&  lpRender[CF_TEXT-1].wDataPresent )
560     {
561         lpRender = &ClipFormats[CF_TEXT-1];
562         lpUpdate = &ClipFormats[CF_OEMTEXT-1];
563         
564         TRACE(clipboard,"\tTEXT -> OEMTEXT\n");
565     }
566     else
567     {
568         lpRender = __lookup_format( ClipFormats, wFormat );
569         lpUpdate = lpRender;
570     }
571    
572     if( !lpRender || !CLIPBOARD_RenderFormat(lpRender) ) return 0; 
573     if( lpUpdate != lpRender && !lpUpdate->hData16 && !lpUpdate->hData32 ) 
574         CLIPBOARD_RenderText(lpUpdate, lpRender);
575
576     if( lpUpdate->hData16 && !lpUpdate->hData32 )
577     {
578       int size;
579       if( lpUpdate->wFormatID == CF_METAFILEPICT )
580         size = sizeof( METAFILEPICT32 );
581       else
582         size = GlobalSize16(lpUpdate->hData16);
583       lpUpdate->hData32 = GlobalAlloc32(GMEM_ZEROINIT, size); 
584       if( lpUpdate->wFormatID == CF_METAFILEPICT )
585       {
586         FIXME(clipboard,"\timplement function CopyMetaFilePict16to32\n");
587         FIXME(clipboard,"\tin the appropriate file.\n");
588 #ifdef SOMEONE_IMPLEMENTED_ME
589         CopyMetaFilePict16to32( GlobalLock16(lpUpdate->hData32), 
590                                 GlobalLock32(lpUpdate->hData16) );
591 #endif
592       }
593       else
594       {
595         memcpy( GlobalLock32(lpUpdate->hData32), 
596                 GlobalLock16(lpUpdate->hData16), 
597                 size );
598       }
599       GlobalUnlock32(lpUpdate->hData32);
600       GlobalUnlock16(lpUpdate->hData16);
601     }
602
603     TRACE(clipboard,"\treturning %04x (type %i)\n", 
604                               lpUpdate->hData32, lpUpdate->wFormatID);
605     return lpUpdate->hData32;
606 }
607
608 /**************************************************************************
609  *           CountClipboardFormats16   (USER.143)
610  */
611 INT16 WINAPI CountClipboardFormats16(void)
612 {
613     return CountClipboardFormats32();
614 }
615
616
617 /**************************************************************************
618  *           CountClipboardFormats32   (USER32.63)
619  */
620 INT32 WINAPI CountClipboardFormats32(void)
621 {
622     INT32 FormatCount = 0;
623     LPCLIPFORMAT lpFormat = ClipFormats; 
624
625     TRACE(clipboard,"(void)\n");
626
627     /* FIXME: Returns BOOL32 */
628     CLIPBOARD_GetDriver()->pRequestSelection();
629
630     FormatCount += abs(lpFormat[CF_TEXT-1].wDataPresent -
631                        lpFormat[CF_OEMTEXT-1].wDataPresent); 
632
633     while(TRUE) 
634     {
635         if (lpFormat == NULL) break;
636         if (lpFormat->wDataPresent) 
637         {
638             TRACE(clipboard, "\tdata found for format %i\n", lpFormat->wFormatID);
639             FormatCount++;
640         }
641         lpFormat = lpFormat->NextFormat;
642     }
643
644     TRACE(clipboard,"\ttotal %d\n", FormatCount);
645     return FormatCount;
646 }
647
648
649 /**************************************************************************
650  *            EnumClipboardFormats16   (USER.144)
651  */
652 UINT16 WINAPI EnumClipboardFormats16( UINT16 wFormat )
653 {
654     return EnumClipboardFormats32( wFormat );
655 }
656
657
658 /**************************************************************************
659  *            EnumClipboardFormats32   (USER32.179)
660  */
661 UINT32 WINAPI EnumClipboardFormats32( UINT32 wFormat )
662 {
663     LPCLIPFORMAT lpFormat = ClipFormats; 
664
665     TRACE(clipboard,"(%04X)\n", wFormat);
666
667     if( hqClipLock != GetFastQueue() ) return 0;
668
669     if( (!wFormat || wFormat == CF_TEXT || wFormat == CF_OEMTEXT) ) 
670         CLIPBOARD_GetDriver()->pRequestSelection();
671
672     if (wFormat == 0)
673     {
674         if (lpFormat->wDataPresent || ClipFormats[CF_OEMTEXT-1].wDataPresent) 
675             return lpFormat->wFormatID;
676         else 
677             wFormat = lpFormat->wFormatID; /* and CF_TEXT is not available */
678     }
679
680     /* walk up to the specified format record */
681
682     if( !(lpFormat = __lookup_format( lpFormat, wFormat )) ) return 0;
683
684     /* find next format with available data */
685
686     lpFormat = lpFormat->NextFormat;
687     while(TRUE) 
688     {
689         if (lpFormat == NULL) return 0;
690         if (lpFormat->wDataPresent || (lpFormat->wFormatID == CF_OEMTEXT && 
691                                        ClipFormats[CF_TEXT-1].wDataPresent)) 
692             break;
693         lpFormat = lpFormat->NextFormat;
694     }
695
696     return lpFormat->wFormatID;
697 }
698
699
700 /**************************************************************************
701  *            RegisterClipboardFormat16  (USER.145)
702  */
703 UINT16 WINAPI RegisterClipboardFormat16( LPCSTR FormatName )
704 {
705     LPCLIPFORMAT lpNewFormat; 
706     LPCLIPFORMAT lpFormat = ClipFormats; 
707
708     if (FormatName == NULL) return 0;
709
710     TRACE(clipboard,"('%s') !\n", FormatName);
711
712     /* walk format chain to see if it's already registered */
713
714     while(TRUE) 
715     {
716         if ( !strcmp(lpFormat->Name,FormatName) )
717         {
718              lpFormat->wRefCount++;
719              return lpFormat->wFormatID;
720         }
721  
722         if ( lpFormat->NextFormat == NULL ) break;
723
724         lpFormat = lpFormat->NextFormat;
725     }
726
727     /* allocate storage for new format entry */
728
729     lpNewFormat = (LPCLIPFORMAT)xmalloc(sizeof(CLIPFORMAT));
730     lpFormat->NextFormat = lpNewFormat;
731     lpNewFormat->wFormatID = LastRegFormat;
732     lpNewFormat->wRefCount = 1;
733
734     lpNewFormat->Name = (LPSTR)xmalloc(strlen(FormatName) + 1);
735     strcpy(lpNewFormat->Name, FormatName);
736
737     lpNewFormat->wDataPresent = 0;
738     lpNewFormat->hData16 = 0;
739     lpNewFormat->hData32 = 0;
740     lpNewFormat->BufSize = 0;
741     lpNewFormat->PrevFormat = lpFormat;
742     lpNewFormat->NextFormat = NULL;
743
744     return LastRegFormat++;
745 }
746
747
748 /**************************************************************************
749  *            RegisterClipboardFormat32A   (USER32.431)
750  */
751 UINT32 WINAPI RegisterClipboardFormat32A( LPCSTR formatName )
752 {
753     return RegisterClipboardFormat16( formatName );
754 }
755
756
757 /**************************************************************************
758  *            RegisterClipboardFormat32W   (USER32.432)
759  */
760 UINT32 WINAPI RegisterClipboardFormat32W( LPCWSTR formatName )
761 {
762     LPSTR aFormat = HEAP_strdupWtoA( GetProcessHeap(), 0, formatName );
763     UINT32 ret = RegisterClipboardFormat32A( aFormat );
764     HeapFree( GetProcessHeap(), 0, aFormat );
765     return ret;
766 }
767
768 /**************************************************************************
769  *            GetClipboardFormatName16   (USER.146)
770  */
771 INT16 WINAPI GetClipboardFormatName16( UINT16 wFormat, LPSTR retStr, INT16 maxlen )
772 {
773     return GetClipboardFormatName32A( wFormat, retStr, maxlen );
774 }
775
776
777 /**************************************************************************
778  *            GetClipboardFormatName32A   (USER32.223)
779  */
780 INT32 WINAPI GetClipboardFormatName32A( UINT32 wFormat, LPSTR retStr, INT32 maxlen )
781 {
782     LPCLIPFORMAT lpFormat = __lookup_format( ClipFormats, wFormat );
783
784     TRACE(clipboard, "(%04X, %p, %d) !\n", wFormat, retStr, maxlen);
785
786     if (lpFormat == NULL || lpFormat->Name == NULL || 
787         lpFormat->wFormatID < CF_REGFORMATBASE) return 0;
788
789     TRACE(clipboard, "Name='%s' !\n", lpFormat->Name);
790
791     lstrcpyn32A( retStr, lpFormat->Name, maxlen );
792     return strlen(retStr);
793 }
794
795
796 /**************************************************************************
797  *            GetClipboardFormatName32W   (USER32.224)
798  */
799 INT32 WINAPI GetClipboardFormatName32W( UINT32 wFormat, LPWSTR retStr, INT32 maxlen )
800 {
801     LPSTR p = HEAP_xalloc( GetProcessHeap(), 0, maxlen );
802     INT32 ret = GetClipboardFormatName32A( wFormat, p, maxlen );
803     lstrcpynAtoW( retStr, p, maxlen );
804     HeapFree( GetProcessHeap(), 0, p );
805     return ret;
806 }
807
808
809 /**************************************************************************
810  *            SetClipboardViewer16   (USER.147)
811  */
812 HWND16 WINAPI SetClipboardViewer16( HWND16 hWnd )
813 {
814     return SetClipboardViewer32( hWnd );
815 }
816
817
818 /**************************************************************************
819  *            SetClipboardViewer32   (USER32.471)
820  */
821 HWND32 WINAPI SetClipboardViewer32( HWND32 hWnd )
822 {
823     HWND32 hwndPrev = hWndViewer;
824
825     TRACE(clipboard,"(%04x): returning %04x\n", hWnd, hwndPrev);
826
827     hWndViewer = hWnd;
828     return hwndPrev;
829 }
830
831
832 /**************************************************************************
833  *           GetClipboardViewer16   (USER.148)
834  */
835 HWND16 WINAPI GetClipboardViewer16(void)
836 {
837     return hWndViewer;
838 }
839
840
841 /**************************************************************************
842  *           GetClipboardViewer32   (USER32.226)
843  */
844 HWND32 WINAPI GetClipboardViewer32(void)
845 {
846     return hWndViewer;
847 }
848
849
850 /**************************************************************************
851  *           ChangeClipboardChain16   (USER.149)
852  */
853 BOOL16 WINAPI ChangeClipboardChain16(HWND16 hWnd, HWND16 hWndNext)
854 {
855     return ChangeClipboardChain32(hWnd, hWndNext);
856 }
857
858 /**************************************************************************
859  *           ChangeClipboardChain32   (USER32.22)
860  */
861 BOOL32 WINAPI ChangeClipboardChain32(HWND32 hWnd, HWND32 hWndNext)
862 {
863     BOOL32 bRet = 0;
864
865     FIXME(clipboard, "(0x%04x, 0x%04x): stub?\n", hWnd, hWndNext);
866
867     if( hWndViewer )
868         bRet = !SendMessage16( hWndViewer, WM_CHANGECBCHAIN,
869                              (WPARAM16)hWnd, (LPARAM)hWndNext);   
870     else
871         WARN(clipboard, "hWndViewer is lost\n");
872
873     if( hWnd == hWndViewer ) hWndViewer = hWndNext;
874
875     return bRet;
876 }
877
878
879
880 /**************************************************************************
881  *           IsClipboardFormatAvailable16   (USER.193)
882  */
883 BOOL16 WINAPI IsClipboardFormatAvailable16( UINT16 wFormat )
884 {
885     return IsClipboardFormatAvailable32( wFormat );
886 }
887
888
889 /**************************************************************************
890  *           IsClipboardFormatAvailable32   (USER32.340)
891  */
892 BOOL32 WINAPI IsClipboardFormatAvailable32( UINT32 wFormat )
893 {
894     TRACE(clipboard,"(%04X) !\n", wFormat);
895
896     if( (wFormat == CF_TEXT || wFormat == CF_OEMTEXT) )
897         CLIPBOARD_GetDriver()->pRequestSelection();
898
899     return CLIPBOARD_IsPresent(wFormat);
900 }
901
902
903 /**************************************************************************
904  *             GetOpenClipboardWindow16   (USER.248)
905  */
906 HWND16 WINAPI GetOpenClipboardWindow16(void)
907 {
908     return hWndClipWindow;
909 }
910
911
912 /**************************************************************************
913  *             GetOpenClipboardWindow32   (USER32.277)
914  */
915 HWND32 WINAPI GetOpenClipboardWindow32(void)
916 {
917     return hWndClipWindow;
918 }
919
920
921 /**************************************************************************
922  *             GetPriorityClipboardFormat16   (USER.402)
923  */
924 INT16 WINAPI GetPriorityClipboardFormat16( UINT16 *lpPriorityList, INT16 nCount)
925 {
926     FIXME(clipboard, "(%p,%d): stub\n", lpPriorityList, nCount );
927     return 0;
928 }
929
930
931 /**************************************************************************
932  *             GetPriorityClipboardFormat32   (USER32.279)
933  */
934 INT32 WINAPI GetPriorityClipboardFormat32( UINT32 *lpPriorityList, INT32 nCount )
935 {
936     int Counter;
937
938     if(CountClipboardFormats32() == 0) 
939     { 
940         return 0;
941     }
942
943     for(Counter = 0; Counter <= nCount; Counter++)
944     {
945         if(IsClipboardFormatAvailable32(*(lpPriorityList+sizeof(INT32)*Counter)))
946             return *(lpPriorityList+sizeof(INT32)*Counter);
947     }
948
949     return -1;
950 }
951
952
953