summaryrefslogtreecommitdiffstats
path: root/private/mvdm/wow32/wuclip.c
blob: b33d49a06e885b20dead77cc749adc6f17afe58e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
/*++
 *
 *  WOW v1.0
 *
 *  Copyright (c) 1991, Microsoft Corporation
 *
 *  WUCLIP.C
 *  WOW32 16-bit User API support
 *
 *  History:
 *  WOW Clipboard functionality designed and developed by ChandanC
 *
--*/


#include "precomp.h"
#pragma hdrstop

struct _CBFORMATS  ClipboardFormats;

DLLENTRYPOINTS  OleStringConversion[WOW_OLESTRINGCONVERSION_COUNT] =
                                    {"ConvertObjDescriptor", NULL};



UINT CFOLEObjectDescriptor;
UINT CFOLELinkSrcDescriptor;


MODNAME(wuclip.c);


/*++
    BOOL ChangeClipboardChain(<hwnd>, <hwndNext>)
    HWND <hwnd>;
    HWND <hwndNext>;

    The %ChangeClipboardChain% function removes the window specified by the
    <hwnd> parameter from the chain of clipboard viewers and makes the window
    specified by the <hwndNext> parameter the descendant of the <hwnd>
    parameter's ancestor in the chain.

    <hwnd>
        Identifies the window that is to be removed from the chain. The handle
        must previously have been passed to the SetClipboardViewer function.

    <hwndNext>
        Identifies the window that follows <hwnd> in the clipboard-viewer
        chain (this is the handle returned by the %SetClipboardViewer% function,
        unless the sequence was changed in response to a WM_CHANGECBCHAIN
        message).

    The return value specifies the status of the <hwnd> window. It is TRUE if
    the window is found and removed. Otherwise, it is FALSE.
--*/

ULONG FASTCALL WU32ChangeClipboardChain(PVDMFRAME pFrame)
{
    ULONG ul;
    register PCHANGECLIPBOARDCHAIN16 parg16;

    GETARGPTR(pFrame, sizeof(CHANGECLIPBOARDCHAIN16), parg16);

    ul = GETBOOL16(ChangeClipboardChain(
            HWND32(parg16->f1),
            HWND32(parg16->f2)
    ));

    FREEARGPTR(parg16);
    RETURN(ul);
}


/*++
    BOOL CloseClipboard(VOID)

    The %CloseClipboard% function closes the clipboard. The %CloseClipboard%
    function should be called when a window has finished examining or changing
    the clipboard. It lets other applications access the clipboard.

    This function has no parameters.

    The return value specifies whether the clipboard is closed. It is TRUE if
    the clipboard is closed. Otherwise, it is FALSE.
--*/

ULONG FASTCALL WU32CloseClipboard(PVDMFRAME pFrame)
{
    ULONG ul;

    UNREFERENCED_PARAMETER(pFrame);

    ul = GETBOOL16(CloseClipboard());

    RETURN(ul);
}


/*++
    No REF header file
--*/

ULONG FASTCALL WU32CountClipboardFormats(PVDMFRAME pFrame)
{
    ULONG ul;

    UNREFERENCED_PARAMETER(pFrame);

    ul = GETINT16(CountClipboardFormats());

    RETURN(ul);
}


/*++
    BOOL EmptyClipboard(VOID)

    The %EmptyClipboard% function empties the clipboard and frees handles to
    data in the clipboard. It then assigns ownership of the clipboard to the
    window that currently has the clipboard open.

    This function has no parameters.

    The return value specifies the status of the clipboard. It is TRUE if the
    clipboard is emptied. It is FALSE if an error occurs.

    The clipboard must be open when the %EmptyClipboard% function is called.
--*/

ULONG FASTCALL WU32EmptyClipboard(PVDMFRAME pFrame)
{
    ULONG ul;

    UNREFERENCED_PARAMETER(pFrame);

    ul = GETBOOL16(EmptyClipboard());

    W32EmptyClipboard ();

    RETURN(ul);
}


/*++
    WORD EnumClipboardFormats(<wFormat>)
    WORD <wFormat>;

    The %EnumClipboardFormats% function enumerates the formats found in a list
    of available formats that belong to the clipboard. On each call to this
    function, the <wFormat> parameter specifies a known available format, and
    the function returns the format that appears next in the list. The first
    format in the list can be retrieved by setting <wFormat> to zero.

    <wFormat>
        Specifies a known format.

    The return value specifies the next known clipboard data format. It is zero
    if <wFormat> specifies the last format in the list of available formats. It
    is zero if the clipboard is not open.

    Before it enumerates the formats by using the %EnumClipboardFormats%
    function, an application must open the clipboard by using the
    %OpenClipboard% function.

    The order that an application uses for putting alternative formats for the
    same data into the clipboard is the same order that the enumerator uses when
    returning them to the pasting application. The pasting application should
    use the first format enumerated that it can handle. This gives the donor a
    chance to recommend formats that involve the least loss of data.
--*/

ULONG FASTCALL WU32EnumClipboardFormats(PVDMFRAME pFrame)
{
    ULONG ul;
    register PENUMCLIPBOARDFORMATS16 parg16;

    GETARGPTR(pFrame, sizeof(ENUMCLIPBOARDFORMATS16), parg16);

    ul = GETWORD16(EnumClipboardFormats(
            WORD32(parg16->f1)
    ));

    FREEARGPTR(parg16);
    RETURN(ul);
}


/*++
    HANDLE GetClipboardData(<wFormat>)
    WORD <wFormat>;

    The %GetClipboardData% function retrieves data from the clipboard in the
    format given by the <wFormat> parameter. The clipboard must have been opened
    previously.

    <wFormat>
        Specifies a data format. For a description of the data formats, see the
        SetClipboardData function, later in this chapter.

    The return value identifies the memory block that contains the data from the
    clipboard. The handle type depends on the type of data specified by the
    <wFormat> parameter. It is NULL if there is an error.

    The available formats can be enumerated in advance by using the
    %EnumClipboardFormats% function.

    The data handle returned by %GetClipboardData% is controlled by the
    clipboard, not by the application. The application should copy the data
    immediately, instead of relying on the data handle for long-term use. The
    application should not free the data handle or leave it locked.

    Windows supports two formats for text, CF_TEXT and CF_OEMTEXT. CF_TEXT is
    the default Windows text clipboard format, while Windows uses the CF_OEMTEXT
    format for text in non-Windows applications. If you call %GetClipboardData%
    to retrieve data in one text format and the other text format is the only
    available text format, Windows automatically converts the text to the
    requested format before supplying it to your application.

    If the clipboard contains data in the CF_PALETTE (logical color palette)
    format, the application should assume that any other data in the clipboard
    is realized against that logical palette.
--*/

ULONG FASTCALL WU32GetClipboardData(PVDMFRAME pFrame)
{
    ULONG ul = 0;
    HANDLE  hMem32, hMeta32 = 0;
    HMEM16  hMem16=0, hMeta16 = 0;
    VPVOID  vp;
    LPBYTE  lpMem32;
    LPBYTE  lpMem16;
    int     cb;
    register PGETCLIPBOARDDATA16 parg16;

    GETARGPTR(pFrame, sizeof(GETCLIPBOARDDATA16), parg16);

    LOGDEBUG(6, ("WOW::WUICBGetClipboardData(): CF_FORMAT is %04x\n", parg16->f1));

    switch (parg16->f1) {

        // This is intentional to let it thru to the "case statements".
        // ChandanC 5/11/92.

        default:
            if ((parg16->f1 == CFOLEObjectDescriptor) || (parg16->f1 == CFOLELinkSrcDescriptor)) {
                hMem32 = GetClipboardData(WORD32(parg16->f1));
                if (hMem32) {
                    hMem16 = (HMEM16) W32ConvertObjDescriptor(hMem32, CFOLE_UNICODE_TO_ANSI);
                }
                WU32ICBStoreHandle(parg16->f1, hMem16);
                break;
            }

        case CF_DIB:
        case CF_TEXT:
        case CF_DSPTEXT:
        case CF_SYLK:
        case CF_DIF:
        case CF_TIFF:
        case CF_OEMTEXT:
        case CF_PENDATA:
        case CF_RIFF:
        case CF_WAVE:
        case CF_OWNERDISPLAY:
            hMem16 = WU32ICBGetHandle(parg16->f1);
            if (!hMem16) {
                hMem32 = GetClipboardData(WORD32(parg16->f1));

                if (hMem16 = WU32ICBGetHandle(parg16->f1)) {

                    //
                    // We couldn't find the hMem16 using WU32ICBGetHandle
                    // before we called Win32 GetClipboardData, but we can
                    // now, so that means it was cut/copied from a task in
                    // this WOW using delayed rendering, so that the actual
                    // non-NULL hMem16 wasn't SetClipboardData until we
                    // just called GetClipboardData.  Since we now have
                    // a valid cached copy of the data in 16-bit land,
                    // we can just return that.
                    //

                    break;
                }

                if (hMem32) {
                    lpMem32 = GlobalLock(hMem32);
                    cb = GlobalSize(hMem32);
		    vp = GlobalAllocLock16(GMEM_MOVEABLE | GMEM_DDESHARE, cb, &hMem16);
		    // 16-bit memory may have moved - refresh flat pointers
		    FREEARGPTR(parg16);
		    FREEVDMPTR(pFrame);
		    GETFRAMEPTR(((PTD)CURRENTPTD())->vpStack, pFrame);
		    GETARGPTR(pFrame, sizeof(GETCLIPBOARDDATA16), parg16);
                    if (vp) {
                        GETMISCPTR(vp, lpMem16);
                        RtlCopyMemory(lpMem16, lpMem32, cb);
                        GlobalUnlock16(hMem16);
                        FLUSHVDMPTR(vp, cb, lpMem16);
                        FREEMISCPTR(lpMem16);
                    }
                    GlobalUnlock(hMem32);
                }

                WU32ICBStoreHandle(parg16->f1, hMem16);
            }
            break;

        case CF_HDROP:
            // This is the case when app is retrieving cf_hdrop from the 
            // clipboard, thus we will convert the dropfiles structure
            // from 32 to 16-bit one
            hMem16 = WU32ICBGetHandle(parg16->f1);
            if (!hMem16) {
                hMem32 = GetClipboardData(WORD32(parg16->f1));
                if (hMem32) {
                    hMem16 = CopyDropFilesFrom32(hMem32);
                }
                WU32ICBStoreHandle(parg16->f1, hMem16);
            }
            break;

        case CF_DSPBITMAP:
        case CF_BITMAP:
            hMem16 = GETHBITMAP16(GetClipboardData(WORD32(parg16->f1)));
            break;

        case CF_PALETTE:
            hMem16 = GETHPALETTE16(GetClipboardData(WORD32(parg16->f1)));
            break;

        case CF_DSPMETAFILEPICT:
        case CF_METAFILEPICT:
            hMem16 = WU32ICBGetHandle(parg16->f1);
            if (!(hMem16)) {
                hMem32 = GetClipboardData(WORD32(parg16->f1));

                if (hMem16 = WU32ICBGetHandle(parg16->f1)) {

                    //
                    // We couldn't find the hMem16 using WU32ICBGetHandle
                    // before we called Win32 GetClipboardData, but we can
                    // now, so that means it was cut/copied from a task in
                    // this WOW using delayed rendering, so that the actual
                    // non-NULL hMem16 wasn't SetClipboardData until we
                    // just called GetClipboardData.  Since we now have
                    // a valid cached copy of the data in 16-bit land,
                    // we can just return that.
                    //

                    break;
                }

                if (hMem32) {
                    lpMem32 = GlobalLock(hMem32);
                    vp = GlobalAllocLock16(GMEM_MOVEABLE | GMEM_DDESHARE, sizeof(METAFILEPICT16), &hMem16);
		    // 16-bit memory may have moved - refresh flat pointers
		    FREEARGPTR(parg16);
		    FREEVDMPTR(pFrame);
		    GETFRAMEPTR(((PTD)CURRENTPTD())->vpStack, pFrame);
		    GETARGPTR(pFrame, sizeof(GETCLIPBOARDDATA16), parg16);
		    if (vp) {
                        GETMISCPTR(vp, lpMem16);
                        FixMetafile32To16 ((LPMETAFILEPICT) lpMem32, (LPMETAFILEPICT16) lpMem16);
                        FREEMISCPTR(lpMem16);

                        hMeta32 = ((LPMETAFILEPICT) lpMem32)->hMF;
                        if (hMeta32) {
			    hMeta16 = WinMetaFileFromHMF(hMeta32, FALSE);
			    // 16-bit memory may have moved
			    FREEARGPTR(parg16);
			    FREEVDMPTR(pFrame);
			    GETFRAMEPTR(((PTD)CURRENTPTD())->vpStack, pFrame);
			    GETARGPTR(pFrame, sizeof(GETCLIPBOARDDATA16), parg16);
			}

                        GETMISCPTR(vp, lpMem16);
                        STOREWORD(((LPMETAFILEPICT16) lpMem16)->hMF, hMeta16);
                        GlobalUnlock16(hMem16);
                        FLUSHVDMPTR(vp, sizeof(METAFILEPICT16), lpMem16);
                        FREEMISCPTR(lpMem16);
                    }
                    GlobalUnlock(hMem32);
                }
                WU32ICBStoreHandle(parg16->f1, hMem16);
            }
            break;
    }

    ul = (ULONG) hMem16;

    FREEARGPTR(parg16);
    RETURN(ul);
}


/*++
    int GetClipboardFormatName(<wFormat>, <lpFormatName>, <nMaxCount>)
    WORD <wFormat>;
    LPSTR <lpFormatName>;
    int <nMaxCount>;

    The %GetClipboardFormatName% function retrieves from the clipboard the name
    of the registered format specified by the <wFormat> parameter. The name is
    copied to the buffer pointed to by the <lpFormatName> parameter.

    <wFormat>
        Specifies the type of format to be retrieved. It must not specify any of
        the predefined clipboard formats.

    <lpFormatName>
        Points to the buffer that is to receive the format name.

    <nMaxCount>
        Specifies the maximum length (in bytes) of the string to be copied
        to the buffer. If the actual name is longer, it is truncated.

    The return value specifies the actual length of the string copied to the
    buffer. It is zero if the requested format does not exist or is a predefined
    format.
--*/

ULONG FASTCALL WU32GetClipboardFormatName(PVDMFRAME pFrame)
{
    ULONG ul;
    PSZ psz2 = NULL;
    register PGETCLIPBOARDFORMATNAME16 parg16;

    GETARGPTR(pFrame, sizeof(GETCLIPBOARDFORMATNAME16), parg16);
    ALLOCVDMPTR(parg16->f2, parg16->f3, psz2);

    ul = GETINT16(GetClipboardFormatName(
            WORD32(parg16->f1),
            psz2,
            INT32(parg16->f3)
    ));

    FLUSHVDMPTR(parg16->f2, strlen(psz2)+1, psz2);
    FREEVDMPTR(psz2);
    FREEARGPTR(parg16);
    RETURN(ul);
}


/*++
    HWND GetClipboardOwner(VOID)

    The %GetClipboardOwner% function retrieves the window handle of the current
    owner of the clipboard.

    This function has no parameters.

    The return value identifies the window that owns the clipboard. It is NULL
    if the clipboard is not owned.

    The clipboard can still contain data even if the clipboard is not currently
    owned.
--*/

ULONG FASTCALL WU32GetClipboardOwner(PVDMFRAME pFrame)
{
    ULONG ul;

    UNREFERENCED_PARAMETER(pFrame);

    ul = GETHWND16(GetClipboardOwner());

    RETURN(ul);
}


/*++
    HWND GetClipboardViewer(VOID)

    The %GetClipboardViewer% function retrieves the window handle of the first
    window in the clipboard-viewer chain.

    This function has no parameters.

    The return value identifies the window currently responsible for displaying
    the clipboard. It is NULL if there is no viewer.
--*/

ULONG FASTCALL WU32GetClipboardViewer(PVDMFRAME pFrame)
{
    ULONG ul;

    UNREFERENCED_PARAMETER(pFrame);

    ul = GETHWND16(GetClipboardViewer());

    RETURN(ul);
}


/*++
    int GetPriorityClipboardFormat(<lpPriorityList>, <cEntries>)
    LPWORD <lpPriorityList>;
    int <cEntries>;

    The %GetPriorityClipboardFormat% function returns the first clipboard format
    in a list for which data exist in the clipboard.

    <lpPriorityList>
        Points to an integer array that contains a list of clipboard formats in
        priority order. For a description of the data formats, see the
        SetClipboardData function later in this chapter.

    <cEntries>
        Specifies the number of entries in <lpPriorityList>. This value
        must not be greater than the actual number of entries in the list.

    The return value is the highest priority clipboard format in the list for
    which data exist. If no data exist in the clipboard, this function returns
    NULL. If data exist in the clipboard which did not match any format in the
    list, the return value is -1.
--*/

ULONG FASTCALL WU32GetPriorityClipboardFormat(PVDMFRAME pFrame)
{
    ULONG ul;
    UINT *pu1;
    register PGETPRIORITYCLIPBOARDFORMAT16 parg16;
    INT      BufferT[256]; // comfortably large array


    GETARGPTR(pFrame, sizeof(GETPRIORITYCLIPBOARDFORMAT16), parg16);
    pu1 = STACKORHEAPALLOC(parg16->f2 * sizeof(INT), sizeof(BufferT), BufferT);
    getuintarray16(parg16->f1, parg16->f2, pu1);

    if (pu1) {
        ul = GETINT16(GetPriorityClipboardFormat(
             pu1,
             INT32(parg16->f2)
        ));
    } else {
        ul = (ULONG)-1;
    }

    STACKORHEAPFREE(pu1, BufferT);
    FREEARGPTR(parg16);
    RETURN(ul);
}


/*++
    BOOL IsClipboardFormatAvailable(<wFormat>)
    WORD <wFormat>;

    The %IsClipboardFormatAvailable% function specifies whether data of a
    certain type exist in the clipboard.

    <wFormat>
        Specifies a registered clipboard format. For information on clipboard
        formats, see the description of the SetClipboardData function, later in
        this chapter.

    The return value specifies the outcome of the function. It is TRUE if data
    having the specified format are present. Otherwise, it is FALSE.

    This function is typically called during processing of the WM_INITMENU or
    WM_INITMENUPOPUP message to determine whether the clipboard contains data
    that the application can paste. If such data are present, the application
    typically enables the Paste command (in its Edit menu).
--*/

ULONG FASTCALL WU32IsClipboardFormatAvailable(PVDMFRAME pFrame)
{
    ULONG ul;
    register PISCLIPBOARDFORMATAVAILABLE16 parg16;

    GETARGPTR(pFrame, sizeof(ISCLIPBOARDFORMATAVAILABLE16), parg16);

    // Hack-a-roo!  PhotoShop 2.5 has a bug in its code for handling large DIB's
    // on the clipboard and will fault if it encounters one.  On WFW, it usually
    // won't encounter one because most apps (in this case alt-Prtscrn button)
    // copy BITMAPS, not DIBS, to the clipboard.  On NT, anytime an app writes
    // a BITMAP to the clipboard it gets converted to a DIB & vice versa-making
    // more clipboard data formats available to inquiring apps.  Unfortunately,
    // Photoshop checks for DIBS before BITMAPS and finds one on Win 
    // Versions >= 4.0.                                             a-craigj

    // if this is a DIB check && the app is PhotoShop...
    if((WORD32(parg16->f1) == CF_DIB) && 
       (CURRENTPTD()->dwWOWCompatFlagsEx & WOWCFEX_NODIBSHERE)) {

        // ...see if there is a bitmap format available too
        if(IsClipboardFormatAvailable(CF_BITMAP)) {

            // if so return FALSE which will cause Photoshop to ask for a
            // BITMAP format next
            ul = FALSE;
        }

        // otherwise we'll check for a DIB anyway & hope it's a small one
        else {
            ul = GETBOOL16(IsClipboardFormatAvailable(CF_DIB));
        }
    }

    // no hack path
    else {
        ul = GETBOOL16(IsClipboardFormatAvailable(WORD32(parg16->f1)));
    }

    FREEARGPTR(parg16);
    RETURN(ul);
}


/*++
    BOOL OpenClipboard(<hwnd>)
    HWND <hwnd>;

    The %OpenClipboard% function opens the clipboard. Other applications will
    not be able to modify the clipboard until the %CloseClipboard% function is
    called.

    <hwnd>
        Identifies the window to be associated with the open clipboard.

    The return value is TRUE if the clipboard is opened, or FALSE if another
    application or window has the clipboard opened.

    The window specified by the <hwnd> parameter will not become the owner of
    the clipboard until the %EmptyCLipboard% function is called.
--*/

ULONG FASTCALL WU32OpenClipboard(PVDMFRAME pFrame)
{
    ULONG ul;
    register POPENCLIPBOARD16 parg16;

    GETARGPTR(pFrame, sizeof(OPENCLIPBOARD16), parg16);

    ul = GETBOOL16(OpenClipboard(
            HWND32(parg16->f1)
    ));

    FREEARGPTR(parg16);
    RETURN(ul);
}


/*++
    WORD RegisterClipboardFormat(<lpFormatName>)
    LPSTR <lpFormatName>;

    The %RegisterClipboardFormat% function registers a new clipboard format
    whose name is pointed to by the <lpFormatName> parameter. The registered
    format can be used in subsequent clipboard functions as a valid format in
    which to render data, and it will appear in the clipboard's list of
    formats.

    <lpFormatName>
        Points to a character string that names the new format. The string must
        be a null-terminated string.

    The return value specifies the newly registered format. If the identical
    format name has been registered before, even by a different application, the
    format's reference count is increased and the same value is returned as when
    the format was originally registered. The return value is zero if the format
    cannot be registered.

    The format value returned by the %RegisterClipboardFormat% function is
    within the range of 0xC000 to 0xFFFF.
--*/

ULONG FASTCALL WU32RegisterClipboardFormat(PVDMFRAME pFrame)
{
    ULONG ul;
    PSZ psz1;
    register PREGISTERCLIPBOARDFORMAT16 parg16;

    GETARGPTR(pFrame, sizeof(REGISTERCLIPBOARDFORMAT16), parg16);
    GETPSZPTR(parg16->f1, psz1);

    ul = GETWORD16(RegisterClipboardFormat(
            psz1
    ));

    FREEPSZPTR(psz1);
    FREEARGPTR(parg16);
    RETURN(ul);
}


/*++
    HANDLE SetClipboardData(<wFormat>, <hData>)
    WORD <wFormat>;
    HANDLE <hData>;

    The %SetClipboardData% function sets the data in the clipboard. The
    application must have called the %OpenClipboard% function before calling
    the %SetClipboardData% function.

    <wFormat>
        Specifies the format of the data. It can be any one of the
        system-defined formats, or a format registered by the
        %RegisterClipboardFormat% function. For a list of system-defined
        formats,

    <hData>
        Identifies the data to be placed into the clipboard. For all formats
        except CF_BITMAP and CF_PALETTE, this parameter must be a handle to
        memory allocated by the %GlobalAlloc% function. For CF_BITMAP format,
        the <hData> parameter is a handle to a bitmap (see %LoadBitmap%). For
        CF_PALETTE format, the <hData> parameter is a handle to a palette (see
        %CreatePalette%).

        If this parameter is NULL, the owner of the clipboard will be sent a
        WM_RENDERFORMAT message when it needs to supply the data.

    The return value is a handle to the data if the function is succesful, or
    NULL if an error occurred.

    If the <hData> parameter contains a handle to memory allocated by the
    %GlobalAlloc% function, the application must not use this handle once it
    has called the %SetClipboardData% function.

    The following list contains the system-defined clipboard formats:

    CF_BITMAP
        The data is a bitmap.

    CF_DIB
        The data is a memory block containing a %BITMAPINFO% structure followed
        by the bitmap data.

    CF_DIF
        The data is in Software Arts' Data Interchange Format.

    CF_DSPBITMAP
        The data is a bitmap representation of a private format. This data is
        displayed in bitmap format in lieu of the privately formatted data.

    CF_DSPMETAFILEPICT
        The data is a metafile representation of a private data format. This
        data is displayed in metafile-picture format in lieu of the privately
        formatted data.

    CF_DSPTEXT
        The data is a textual representation of a private data format. This data
        is displayed in text format in lieu of the privately formatted data.

    CF_METAFILEPICT
        The data is a metafile (see description of the %METAFILEPICT%
        structure).

    CF_OEMTEXT
        The data is an array of text characters in the OEM character set. Each
        line ends with a carriage return/linefeed (CR-LF) combination. A null
        character signals the end of the data.

    CF_OWNERDISPLAY
        The data is in a private format that the clipboard owner must display.

    CF_PALETTE
        The data is a color palette.

    CF_SYLK
        The data is in Microsoft Symbolic Link (SYLK) format.

    CF_TEXT
        The data is an array of text characters. Each line ends with a carriage
        return/linefeed (CR-LF) combination. A null character signals the end of
        the data.

    CF_TIFF
        The data is in Tag Image File Format.

    Private data formats in the range of CF_PRIVATEFIRST to CF_PRIVATELAST are
    not automatically freed when the data is deleted from the clipboard. Data
    handles associated with these formats should be freed upon receiving a
    WM_DESTROYCLIPBOARD message.

    Private data formats in the range of CF_GDIOBJFIRST to CF_GDIOBJLAST will
    be automatically deleted with a call to %DeleteObject% when the data is
    deleted from the clipboard.

    If the Windows clipboard application is running, it will not update its
    window to show the data placed in the clipboard by the %SetClipboardData%
    until after the %CloseClipboard% function is called.

    31-Oct-1990 [ralphw] Miscelanious material, needs to be moved to other
    function descriptions/overviews.

        Whenever an application places data in the clipboard that depends on or
        assumes a color palette, it should also place the palette in the
        clipboard as well.

        If the clipboard contains data in the CF_PALETTE (logical color palette)
        format, the application should assume that any other data in the
        clipboard is realized against that logical palette.

        The clipboard-viewer application (CLIPBRD.EXE) always uses as its
        current palette any object in CF_PALETTE format that is in the clipboard
        when it displays the other formats in the clipboard.

        Windows supports two formats for text, CF_TEXT and CF_OEMTEXT. CF_TEXT
        is the default Windows text clipboard format, while Windows uses the
        CF_OEMTEXT format for text in non-Windows applications. If you call
        %GetClipboardData% to retrieve data in one text format and the other
        text format is the only available text format, Windows automatically
        converts the text to the requested format before supplying it to your
        application.

        An application registers other standard formats, such as Rich Text
        Format (RTF), by name using the %RegisterClipboardFormat% function
        rather than by a symbolic constant. For information on these external
        formats, see the README.TXT file.
--*/

ULONG FASTCALL WU32SetClipboardData(PVDMFRAME pFrame)
{
    ULONG ul = 0;
    HANDLE hMem32 = NULL, hMF32 = NULL;
    HAND16 hMem16, hMeta16 = 0;
    LPBYTE lpMem16, lpMem32;
    INT     cb;
    VPVOID  vp;


    register PSETCLIPBOARDDATA16 parg16;

    GETARGPTR(pFrame, sizeof(SETCLIPBOARDDATA16), parg16);

    LOGDEBUG(6, ("WOW::WUICBSetClipboardData(): CF_FORMAT is %04x\n", parg16->f1));

    switch (parg16->f1) {

        default:
            if ((parg16->f1 == CFOLEObjectDescriptor) || (parg16->f1 == CFOLELinkSrcDescriptor)) {
                if (parg16->f2) {
                    hMem32 = W32ConvertObjDescriptor((HANDLE) parg16->f2, CFOLE_ANSI_TO_UNICODE);
                }
                ul = (ULONG) SetClipboardData(WORD32(parg16->f1), hMem32);
		WU32ICBStoreHandle(parg16->f1, parg16->f2);
                break;
            }

        // It is intentional to let it thru to the "case statements".
        // ChandanC 5/11/92.

        case CF_DIB:
        case CF_TEXT:
        case CF_DSPTEXT:
        case CF_SYLK:
        case CF_DIF:
        case CF_TIFF:
        case CF_OEMTEXT:
        case CF_PENDATA:
        case CF_RIFF:
        case CF_WAVE:
        case CF_OWNERDISPLAY:
            hMem16 = parg16->f2;
            if (hMem16) {
                vp = GlobalLock16(hMem16, &cb);
                if (vp) {
                    GETMISCPTR(vp, lpMem16);
                    hMem32 = Copyh16Toh32 (cb, lpMem16);
                    GlobalUnlock16(hMem16);
                    FREEMISCPTR(lpMem16);
                }
            }

            ul = (ULONG) SetClipboardData(WORD32(parg16->f1), hMem32);

            WU32ICBStoreHandle(parg16->f1, hMem16);
            break;

        case CF_HDROP:
            // support cf_hdrop format by converting the dropfiles structure
            hMem16 = parg16->f2;
            if (hMem16) {
                hMem32 = CopyDropFilesFrom16(hMem16);
            }
            ul = (ULONG)SetClipboardData(WORD32(parg16->f1), hMem32);
            WU32ICBStoreHandle(parg16->f1, hMem16);
            break;

        case CF_DSPBITMAP:
        case CF_BITMAP:
            ul = (ULONG) SetClipboardData(WORD32(parg16->f1),
                        HBITMAP32(parg16->f2));
            break;


        case CF_PALETTE:
            ul = (ULONG) SetClipboardData(WORD32(parg16->f1),
                        HPALETTE32(parg16->f2));
            break;

        case CF_DSPMETAFILEPICT:
        case CF_METAFILEPICT:
            hMem16 = parg16->f2;
            if (hMem16) {
                vp = GlobalLock16(hMem16, &cb);
                if (vp) {
                    GETMISCPTR(vp, lpMem16);
                    hMem32 = GlobalAlloc(GMEM_DDESHARE, sizeof(METAFILEPICT));
                    WOW32ASSERT(hMem32);
                    if (hMem32) {
                        lpMem32 = GlobalLock(hMem32);
                        ((LPMETAFILEPICT) lpMem32)->mm = FETCHSHORT(((LPMETAFILEPICT16) lpMem16)->mm);
                        ((LPMETAFILEPICT) lpMem32)->xExt = (LONG) FETCHSHORT(((LPMETAFILEPICT16) lpMem16)->xExt);
                        ((LPMETAFILEPICT) lpMem32)->yExt = (LONG) FETCHSHORT(((LPMETAFILEPICT16) lpMem16)->yExt);
                        hMeta16 = FETCHWORD(((LPMETAFILEPICT16) lpMem16)->hMF);
                        if (hMeta16) {
                            hMF32 = (HMETAFILE) HMFFromWinMetaFile(hMeta16, FALSE);
                        }
                        ((LPMETAFILEPICT) lpMem32)->hMF = hMF32;
                        GlobalUnlock(hMem32);
                    }
                    GlobalUnlock16(hMem16);
                    FREEMISCPTR(lpMem16);
                }

            }

            ul = (ULONG) SetClipboardData(WORD32(parg16->f1), hMem32);

            WU32ICBStoreHandle(parg16->f1, hMem16);
            break;

    }

    if (parg16->f2) {
        ul = parg16->f2;
    }

    FREEARGPTR(parg16);
    RETURN(ul);
}



/*++
    HWND SetClipboardViewer(<hwnd>)
    HWND <hwnd>;

    The %SetClipboardViewer% function adds the window specified by the <hwnd>
    parameter to the chain of windows that are notified (via the
    WM_DRAWCLIPBOARD message) whenever the contents of the clipboard are
    changed.

    <hwnd>
        Identifies the window to receive clipboard-viewer chain messages.

    The return value identifies the next window in the clipboard-viewer chain.
    This handle should be saved in static memory and used in responding to
    clipboard-viewer chain messages.

    Windows that are part of the clipboard-viewer chain must respond to
    WM_CHANGECBCHAIN, WM_DRAWCLIPBOARD, and WM_DESTROY messages.

    If an application wishes to remove itself from the clipboard-viewer chain,
    it must call the %ChangeClipboardChain% function.
--*/

ULONG FASTCALL WU32SetClipboardViewer(PVDMFRAME pFrame)
{
    ULONG ul;
    register PSETCLIPBOARDVIEWER16 parg16;

    GETARGPTR(pFrame, sizeof(SETCLIPBOARDVIEWER16), parg16);

    ul = GETHWND16(SetClipboardViewer(HWND32(parg16->f1)));

    FREEARGPTR(parg16);
    RETURN(ul);
}


VOID WU32ICBStoreHandle(WORD wFormat, HMEM16 hMem16)
{
    HAND16  h16, hMeta16;
    PCBNODE Temp, Temp1;
    LPBYTE  lpMem16;
    VPVOID  vp;
    int     cb;


    if ((wFormat == CF_METAFILEPICT) || (wFormat == CF_DSPMETAFILEPICT)) {
        if (wFormat == CF_METAFILEPICT) {
            h16 = ClipboardFormats.Pre1[wFormat];
        }
        else {
            h16 = ClipboardFormats.Pre2[3];
        }

        if (h16) {
            vp = GlobalLock16(h16, &cb);
            if (vp) {
                GETMISCPTR(vp, lpMem16);
                hMeta16 = FETCHWORD(((LPMETAFILEPICT16) lpMem16)->hMF);
                GlobalUnlockFree16(GlobalLock16(hMeta16, NULL));
            }
            GlobalUnlockFree16(vp);

            if (wFormat == CF_METAFILEPICT) {
                ClipboardFormats.Pre1[wFormat] = 0;
            }
            else {
                ClipboardFormats.Pre2[3] = 0;
            }
        }
    }

    if ((wFormat >= CF_TEXT ) && (wFormat <= CF_WAVE)) {
        if (ClipboardFormats.Pre1[wFormat]) {
            GlobalUnlockFree16(GlobalLock16(ClipboardFormats.Pre1[wFormat], NULL));
        }
        ClipboardFormats.Pre1[wFormat] = hMem16;
    }
    else if ((wFormat >= CF_OWNERDISPLAY) && (wFormat <= CF_DSPMETAFILEPICT)) {
        wFormat = (wFormat & (WORD) 3);

        if (ClipboardFormats.Pre2[wFormat]) {
            GlobalUnlockFree16(GlobalLock16(ClipboardFormats.Pre2[wFormat], NULL));
        }

        ClipboardFormats.Pre2[wFormat] = hMem16;
    }
    else if (wFormat == CF_HDROP) {

        if (ClipboardFormats.hmem16Drop) {
            GlobalUnlockFree16(GlobalLock16(ClipboardFormats.hmem16Drop, NULL));
        }
        ClipboardFormats.hmem16Drop = hMem16;
    }
    else {
        Temp = ClipboardFormats.NewFormats;
        if (Temp) {
            while ((Temp->Next) && (Temp->Id != wFormat)) {
                Temp = Temp->Next;
            }

            if (Temp->Id == wFormat) {

                // free a previous handle if it exists
                if (Temp->hMem16) {
                    GlobalUnlockFree16(GlobalLock16(Temp->hMem16, NULL));
                }

                Temp->hMem16 = hMem16;
            }
            else {
                Temp1 = (PCBNODE) malloc_w (sizeof(CBNODE));
                if (Temp1) {
                    Temp->Next = Temp1;
                    Temp1->Id = wFormat;
                    Temp1->hMem16 = hMem16;
                    Temp1->Next = NULL;

                    LOGDEBUG(6,("WOW::WU32ICBStoreHandle: Adding a new node for private clipboard data format %04lx\n", wFormat));
                }
            }
        }
        else {
            Temp = (PCBNODE) malloc_w (sizeof(CBNODE));
            if (Temp) {
                ClipboardFormats.NewFormats = Temp;
                Temp->Id = wFormat;
                Temp->hMem16 = hMem16;
                Temp->Next = NULL;

                LOGDEBUG(6,("WOW::WU32ICBStoreHandle: Adding the FIRST node for private clipboard data format %04lx\n", wFormat));
            }
        }
    }
}



HMEM16 WU32ICBGetHandle(WORD wFormat)
{
    HMEM16 hMem16 = 0;
    PCBNODE Temp;

    if ((wFormat >= CF_TEXT) && (wFormat <= CF_WAVE)) {
        hMem16 = ClipboardFormats.Pre1[wFormat];
    }
    else if ((wFormat >= CF_OWNERDISPLAY) && (wFormat <= CF_DSPMETAFILEPICT)) {
        wFormat = (wFormat & (WORD) 3);
        hMem16 = ClipboardFormats.Pre2[wFormat];
    }
    else if (wFormat == CF_HDROP) {
        hMem16 = ClipboardFormats.hmem16Drop;
    }
    else {
        Temp = ClipboardFormats.NewFormats;
        if (Temp) {
            while ((Temp->Next) && (Temp->Id != wFormat)) {
                Temp = Temp->Next;
            }

            if (Temp->Id == wFormat) {
                hMem16 = Temp->hMem16;
            }
            else {
                LOGDEBUG(6,("WOW::WU32ICBGetHandle: Cann't find private clipboard data format %04lx\n", wFormat));
                hMem16 = (WORD) NULL;
            }
        }
    }

    return (hMem16);
}



VOID W32EmptyClipboard ()
{
    PCBNODE Temp, Temp1;
    int wFormat, cb;
    HAND16 hMem16, hMeta16;
    LPBYTE lpMem16;
    VPVOID vp;

    // Empty CF_METAFILEPICT

    hMem16 = ClipboardFormats.Pre1[CF_METAFILEPICT];
    if (hMem16) {
        vp = GlobalLock16(hMem16, &cb);
        if (vp) {
            GETMISCPTR(vp, lpMem16);
            hMeta16 = FETCHWORD(((LPMETAFILEPICT16) lpMem16)->hMF);
            GlobalUnlockFree16(GlobalLock16(hMeta16, NULL));
        }
        GlobalUnlockFree16(vp);
        ClipboardFormats.Pre1[CF_METAFILEPICT] = 0;
    }

    // Empty CF_DSPMETAFILEPICT

    hMem16 = ClipboardFormats.Pre2[3];
    if (hMem16) {
        vp = GlobalLock16(hMem16, &cb);
        if (vp) {
            GETMISCPTR(vp, lpMem16);
            hMeta16 = FETCHWORD(((LPMETAFILEPICT16) lpMem16)->hMF);
            GlobalUnlockFree16(GlobalLock16(hMeta16, NULL));
        }
        GlobalUnlockFree16(vp);
        ClipboardFormats.Pre2[3] = 0;
    }

    // Empty rest of the formats

    for (wFormat=0; wFormat <= CF_WAVE ; wFormat++) {
        if (ClipboardFormats.Pre1[wFormat]) {
            GlobalUnlockFree16(GlobalLock16(ClipboardFormats.Pre1[wFormat], NULL));
            ClipboardFormats.Pre1[wFormat] = 0;
        }
    }

    for (wFormat=0; wFormat < 4 ; wFormat++) {
        if (ClipboardFormats.Pre2[wFormat]) {
            GlobalUnlockFree16(GlobalLock16(ClipboardFormats.Pre2[wFormat], NULL));
            ClipboardFormats.Pre2[wFormat] = 0;
        }
    }

    if (ClipboardFormats.hmem16Drop) {
        GlobalUnlockFree16(GlobalLock16(ClipboardFormats.hmem16Drop, NULL));
    }
    ClipboardFormats.hmem16Drop = 0;


    // These are the private registered data formats. This list is purged when
    // 32 bit USER purges its clipboard cache.

    Temp = ClipboardFormats.NewFormats;
    ClipboardFormats.NewFormats = NULL;

    while (Temp) {

        Temp1 = Temp->Next;

        if (Temp->hMem16) {
            GlobalUnlockFree16(GlobalLock16(Temp->hMem16, NULL));
        }

        free_w(Temp);

        Temp = Temp1;
    }

}


VOID InitCBFormats ()

{
    int wFormat;

    for (wFormat = 0 ; wFormat <= CF_WAVE ; wFormat++) {
        ClipboardFormats.Pre1[wFormat] = 0;
    }

    for (wFormat=0; wFormat < 4; wFormat++) {
        ClipboardFormats.Pre2[wFormat] = 0;
    }

    ClipboardFormats.hmem16Drop = 0;

    // These are the private registered data formats.

    ClipboardFormats.NewFormats = NULL;


    CFOLEObjectDescriptor = RegisterClipboardFormat ("Object Descriptor");
    CFOLELinkSrcDescriptor = RegisterClipboardFormat ("Link Source Descriptor");

#ifdef DEBUG

    //
    // This would assert in LoadLibraryAndGetProcAddresses if the function
    // or DLL name has changed.
    //

    if (!(OleStringConversion[WOW_OLE_STRINGCONVERSION].lpfn)) {
        LoadLibraryAndGetProcAddresses("OLETHK32.DLL", OleStringConversion, WOW_OLESTRINGCONVERSION_COUNT);
    }

#endif


}


HGLOBAL W32ConvertObjDescriptor(HANDLE hMem, UINT flag)
{
    HANDLE hMemOut;

    if (!(OleStringConversion[WOW_OLE_STRINGCONVERSION].lpfn)) {
        if (!LoadLibraryAndGetProcAddresses("OLETHK32.DLL", OleStringConversion, WOW_OLESTRINGCONVERSION_COUNT)) {
            return (0);
        }
    }

    hMemOut = (HANDLE) (*OleStringConversion[WOW_OLE_STRINGCONVERSION].lpfn) (hMem, flag);

    return (hMemOut);
}