summaryrefslogblamecommitdiffstats
path: root/private/oleutest/simpdnd/dxferobj.cpp
blob: c95dbe8bf32bcfd64ee63fb7fd10e8d97a089f8d (plain) (tree)
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


































































































































































































































































































































































































































































































































































































































































































































































                                                                                          
//**********************************************************************
// File name: DXFEROBJ.CPP
//
//      Implementation file for CDataXferObj, data transfer object
//      implementation of IDataObject interface.
//
// Functions:
//
//      See DXFEROBJ.H for class definition
//
// Copyright (c) 1992 - 1993 Microsoft Corporation. All rights reserved.
//**********************************************************************

#include "pre.h"
#include <enumfetc.h>
#include <assert.h>
#include "dxferobj.h"
#include "site.h"

//BUGBUG:  These should be fetched from real header files
#define CF_HDROP 15
typedef struct _dropfilestruct {
    DWORD pFiles;
    POINT pt;
    BOOL fNC;
    BOOL fWide;
} DROPFILESTRUCT;


CLIPFORMAT g_cfEmbeddedObject=RegisterClipboardFormat(CF_EMBEDDEDOBJECT);
CLIPFORMAT g_cfObjectDescriptor=RegisterClipboardFormat(CF_OBJECTDESCRIPTOR);

// List of formats offered by our data transfer object via EnumFormatEtc
static FORMATETC s_arrGetFmtEtcs[] =
{
    { g_cfEmbeddedObject, NULL, DVASPECT_CONTENT, -1, TYMED_ISTORAGE},
    { g_cfObjectDescriptor, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL},
    { CF_METAFILEPICT, NULL, DVASPECT_CONTENT, -1, TYMED_MFPICT}
};


//**********************************************************************
//
// CDataXferObj::Create
//
// Purpose:
//
//      Creation routine for CDataXferObj
//
// Parameters:
//
//      CSimpleSite FAR *lpSite   - Pointer to source CSimpleSite
//                                  this is the container site of the
//                                  source OLE object to be transfered
//
// Return Value:
//
//      None
//
// Function Calls:
//      Function                    Location
//
//      StgCreateDocfile            OLE API
//      assert                      C Runtime
//
// Comments:
//      reference count of CDataXferObj will be 0 on return.
//
//********************************************************************

CDataXferObj FAR * CDataXferObj::Create(
		CSimpleSite FAR *lpSite,
		POINTL FAR* pPointl
)
{
    CDataXferObj FAR * lpTemp = new CDataXferObj();

    if (!lpTemp)
        return NULL;

    // create a sub-storage for the object
    HRESULT hErr = StgCreateDocfile(
    			NULL,
            STGM_READWRITE | STGM_DIRECT | STGM_SHARE_EXCLUSIVE |
            STGM_DELETEONRELEASE,
            0,
            &lpTemp->m_lpObjStorage);

    assert(hErr == NOERROR);

    if (hErr != NOERROR)
    {
        delete lpTemp;
        return NULL;
    }

    // Clone the source object
    if (lpSite->m_lpOleObject)
    {
        // Object is loaded; ask the object to save into the new storage
        LPPERSISTSTORAGE pPersistStorage;

        if (lpSite->m_lpOleObject->QueryInterface(IID_IPersistStorage,
                (LPVOID FAR*)&pPersistStorage) != ResultFromScode(S_OK))
        {
           /* cannot find interface
            */
           return(NULL);
        }
        assert(pPersistStorage);
        if (OleSave(pPersistStorage, lpTemp->m_lpObjStorage, FALSE)
            != ResultFromScode(S_OK))
        {
            TestDebugOut("Fail in OleSave\n");
        }

        // pass NULL so that object application won't forget the real stg
        if (pPersistStorage->SaveCompleted(NULL) != ResultFromScode(S_OK))
        {
          TestDebugOut("Fail in IPersistStorage::SaveCompleted\n");
        }
        pPersistStorage->Release();
    }
    else
    {
        // Object not loaded so use cheaper IStorage CopyTo operation
        lpSite->m_lpObjStorage->CopyTo(0, NULL, NULL, lpTemp->m_lpObjStorage);
    }

    if (OleLoad(lpTemp->m_lpObjStorage, IID_IOleObject, NULL,
                (LPVOID FAR*)&lpTemp->m_lpOleObject)
        != ResultFromScode(S_OK))
    {
       /* we cannot load the embedded/linked object into the memory
        */
       return(NULL);
    }
    assert(lpTemp->m_lpOleObject);

    lpTemp->m_sizel = lpSite->m_sizel;
    if (pPointl)
    	lpTemp->m_pointl = *pPointl;
    else
    	lpTemp->m_pointl.x = lpTemp->m_pointl.y = 0;
    return lpTemp;
}

//**********************************************************************
//
// CDataXferObj::CDataXferObj
//
// Purpose:
//
//      Constructor for CDataXferObj
//
// Parameters:
//
//      CSimpleDoc FAR *lpDoc   - Pointer to CSimpleDoc
//
// Return Value:
//
//      None
//
// Function Calls:
//      Function                    Location
//
//
//********************************************************************

CDataXferObj::CDataXferObj (void)
{
    // clear the reference count
    m_nCount = 0;

    m_lpObjStorage = NULL;
    m_lpOleObject = NULL;
    m_sizel.cx = m_sizel.cy = 0;
    m_pointl.x = m_pointl.y = 0;
}

//**********************************************************************
//
// CDataXferObj::~CDataXferObj
//
// Purpose:
//
//      Destructor for CDataXferObj
//
// Parameters:
//
//      None
//
// Return Value:
//
//      None
//
// Function Calls:
//      Function                                Location
//
//      TestDebugOut                       Windows API
//      IOleObject::Release                     Object
//      IStorage::Release                       OLE API
//
//
//********************************************************************

CDataXferObj::~CDataXferObj ()
{
    TestDebugOut ("In CDataXferObj's Destructor \r\n");

    if (m_lpOleObject)
    {
       m_lpOleObject->Release();
       m_lpOleObject = NULL;

       // Release the storage for this object
       m_lpObjStorage->Release();
       m_lpObjStorage = NULL;
    }
}



//**********************************************************************
//
// CDataXferObj::QueryInterface
//
// Purpose:
//
//      Used for interface negotiation of the CDataXferObj instance
//
// Parameters:
//
//      REFIID riid         -   A reference to the interface that is
//                              being queried.
//
//      LPVOID FAR* ppvObj  -   An out parameter to return a pointer to
//                              the interface.
//
// Return Value:
//
//      S_OK            -   The interface is supported.
//      E_NOINTERFACE   -   The interface is not supported
//
// Function Calls:
//      Function                    Location
//
//      TestDebugOut           Windows API
//      IsEqualIID                  OLE API
//      ResultFromScode             OLE API
//      CDataXferObj::AddRef        DXFEROBJ.CPP
//
//
//********************************************************************

STDMETHODIMP CDataXferObj::QueryInterface(REFIID riid, LPVOID FAR* ppvObj)
{
    TestDebugOut("In CDataXferObj::QueryInterface\r\n");

    if (IsEqualIID( riid, IID_IUnknown) || IsEqualIID(riid, IID_IDataObject))
    {
        AddRef();
        *ppvObj = this;
        return NOERROR;
    }

    // unknown interface requested
    *ppvObj = NULL;     // must set out pointer parameters to NULL
    return ResultFromScode(E_NOINTERFACE);
}

//**********************************************************************
//
// CDataXferObj::AddRef
//
// Purpose:
//
//      Increments the reference count of the CDataXferObj instance
//
// Parameters:
//
//      None
//
// Return Value:
//
//      ULONG   -   The new reference count of the object
//
// Function Calls:
//      Function                    Location
//
//      TestDebugOut           Windows API
//
//
//********************************************************************

STDMETHODIMP_(ULONG) CDataXferObj::AddRef()
{
    TestDebugOut("In CDataXferObj::AddRef\r\n");

    return ++m_nCount;
}

//**********************************************************************
//
// CDataXferObj::Release
//
// Purpose:
//
//      Decrements the reference count of the CDataXferObj object
//
// Parameters:
//
//      None
//
// Return Value:
//
//      ULONG   -   The new reference count of the object.
//
// Function Calls:
//      Function                    Location
//
//      TestDebugOut           Windows API
//
//
//********************************************************************

STDMETHODIMP_(ULONG) CDataXferObj::Release()
{
    TestDebugOut("In CDataXferObj::Release\r\n");

    if (--m_nCount == 0)
    {
        delete this;
        return 0;
    }
    return m_nCount;
}


/********************************************************************
** This IDataObject implementation is used for data transfer.
**
** The following methods are NOT supported for data transfer:
**		IDataObject::SetData	-- return E_NOTIMPL
**		IDataObject::DAdvise -- return OLE_E_ADVISENOTSUPPORTED
**				   ::DUnadvise
**				   ::EnumDAdvise
**		IDataObject::GetCanonicalFormatEtc -- return E_NOTIMPL
**						(NOTE: must set pformatetcOut->ptd = NULL)
*********************************************************************/

    	
//**********************************************************************
//
// CDataXferObj::QueryGetData
//
// Purpose:
//
//      Called to determine if our object supports a particular
//      FORMATETC.
//
// Parameters:
//
//      LPFORMATETC pformatetc  - Pointer to the FORMATETC being queried for.
//
// Return Value:
//
//      DV_E_FORMATETC    - The FORMATETC is not supported
//      S_OK              - The FORMATETC is supported.
//
//
// Function Calls:
//      Function                    Location
//
//      TestDebugOut           Windows API
//      ResultFromScode             OLE API
//
// Comments:
//      we support the following formats:
//          "Embedded Object"
//          "Object Descriptor"
//          CF_METAFILEPICT
//
//********************************************************************
STDMETHODIMP CDataXferObj::QueryGetData (LPFORMATETC pformatetc)
{
    SCODE sc = DV_E_FORMATETC;

    TestDebugOut("In CDataXferObj::QueryGetData\r\n");

    // check the validity of the formatetc.

    if ( (pformatetc->cfFormat == g_cfEmbeddedObject) &&
         (pformatetc->dwAspect == DVASPECT_CONTENT) &&
         (pformatetc->tymed == TYMED_ISTORAGE) )
    {
        sc = S_OK;
    }

    else if ( (pformatetc->cfFormat == g_cfObjectDescriptor) &&
         (pformatetc->dwAspect == DVASPECT_CONTENT) &&
         (pformatetc->tymed == TYMED_HGLOBAL) )
    {
        sc = S_OK;
    }

    else if ( (pformatetc->cfFormat == CF_METAFILEPICT) &&
         (pformatetc->dwAspect == DVASPECT_CONTENT) &&
         (pformatetc->tymed == TYMED_MFPICT) )
    {
        sc = S_OK;
    }
    else if ( (pformatetc->cfFormat == CF_HDROP) &&
	 (pformatetc->dwAspect == DVASPECT_CONTENT) &&
	 (pformatetc->tymed == TYMED_HGLOBAL) )
    {
	sc = S_OK;
    }

    return ResultFromScode(sc);
}

extern BOOL gfUseEmptyEnumerator;

//**********************************************************************
//
// CDataXferObj::EnumFormatEtc
//
// Purpose:
//
//      Enumerates the formats that can be used to store data
//
// Parameters:
//
//      DWORD dwDirection                - format to be enumerated
//
//      LPENUMFORMATETC ppenumFormatEtc  - where to return the
//                                         instantiated enumerator
//
// Return Value:
//
//      S_OK              - if the operation is successful
//      E_OUTOFMEMORY     - if ran out of memory
//      E_NOTIMPL         - if dwDirection is not supported
//
//
// Function Calls:
//      Function                    Location
//
//      TestDebugOut           Windows API
//      ResultFromScode             OLE API
//      OleStdEnumFmtEtc_Create     OLE2UI
//
//**********************************************************************

STDMETHODIMP CDataXferObj::EnumFormatEtc(
		DWORD dwDirection,
		LPENUMFORMATETC FAR* ppenumFormatEtc
)
{
    SCODE sc = E_NOTIMPL;

    TestDebugOut("In CDataXferObj::EnumFormatEtc\r\n");
    *ppenumFormatEtc = NULL;

    if( gfUseEmptyEnumerator == TRUE )
    {
	return E_FAIL;
    }

    if (dwDirection == DATADIR_GET)
    {
        *ppenumFormatEtc = OleStdEnumFmtEtc_Create(
                sizeof(s_arrGetFmtEtcs)/sizeof(s_arrGetFmtEtcs[0]),
                s_arrGetFmtEtcs);
        if (*ppenumFormatEtc == NULL)
            sc = E_OUTOFMEMORY;
        else
        	   sc = S_OK;
    }
    return ResultFromScode(sc);
}


//**********************************************************************
//
// CDataXferObj::GetData
//
// Purpose:
//
//      Returns the data in the format specified in pformatetcIn.
//
// Parameters:
//
//      LPFORMATETC pformatetcIn    -   The format requested by the caller
//
//      LPSTGMEDIUM pmedium         -   The medium requested by the caller
//
// Return Value:
//
//      DV_E_FORMATETC    - Format not supported
//      S_OK              - Success
//
// Function Calls:
//      Function                        Location
//
//      TestDebugOut               Windows API
//      OleStdGetOleObjectData          OLE2UI API
//      OleStdGetMetafilePictFromOleObject OLE2UI API
//      OleStdGetObjectDescriptorDataFromOleObject OLE2UI API
//      ResultFromScode                 OLE API
//      IOleObject::QueryInterface      Object
//
// Comments:
//      we support GetData for the following formats:
//          "Embedded Object"
//          "Object Descriptor"
//          CF_METAFILEPICT
//
//********************************************************************

STDMETHODIMP CDataXferObj::GetData (
		LPFORMATETC pformatetcIn,
		LPSTGMEDIUM pmedium
)
{
    SCODE sc = DV_E_FORMATETC;

    TestDebugOut("In CDataXferObj::GetData\r\n");

    // we must set all out pointer parameters to NULL. */
    pmedium->tymed = TYMED_NULL;
    pmedium->pUnkForRelease = NULL;    // we transfer ownership to caller
    pmedium->hGlobal = NULL;

    // Check the FORMATETC and fill pmedium if valid.
    if ( (pformatetcIn->cfFormat == g_cfEmbeddedObject) &&
         (pformatetcIn->dwAspect == DVASPECT_CONTENT) &&
         (pformatetcIn->tymed == TYMED_ISTORAGE) )
    {
         LPPERSISTSTORAGE pPersistStorage;

         /* render CF_EMBEDDEDOBJECT by asking the object to save
         **    into a temporary, DELETEONRELEASE IStorage allocated by us.
         */
         HRESULT hRes;
         if ((hRes=m_lpOleObject->QueryInterface(
                 IID_IPersistStorage, (LPVOID FAR*)&pPersistStorage))
             != ResultFromScode(S_OK))
         {
             return(hRes);
         }

         assert(pPersistStorage);
         HRESULT hrErr = OleStdGetOleObjectData(
                    pPersistStorage,
                    pformatetcIn,
                    pmedium,
                    FALSE   /* fUseMemory -- (use file-base stg) */
         );
         pPersistStorage->Release();
         sc = GetScode( hrErr );

    }
    else if ( (pformatetcIn->cfFormat == g_cfObjectDescriptor) &&
         (pformatetcIn->dwAspect == DVASPECT_CONTENT) &&
         (pformatetcIn->tymed == TYMED_HGLOBAL) )
    {

         // render CF_OBJECTDESCRIPTOR data
         pmedium->hGlobal = OleStdGetObjectDescriptorDataFromOleObject(
                m_lpOleObject,
                TEXT("Simple OLE 2.0 Container"), //string to identify source
                DVASPECT_CONTENT,
                m_pointl,
                (LPSIZEL)&m_sizel
            );
         if (! pmedium->hGlobal)
             sc = E_OUTOFMEMORY;
         else
         {
             pmedium->tymed = TYMED_HGLOBAL;
             sc = S_OK;
         }

    }
    else if ( (pformatetcIn->cfFormat == CF_METAFILEPICT) &&
            (pformatetcIn->dwAspect == DVASPECT_CONTENT) &&
            (pformatetcIn->tymed == TYMED_MFPICT) )
    {

	// render CF_METAFILEPICT by drawing the object into a metafile DC
	pmedium->hGlobal = OleStdGetMetafilePictFromOleObject(
	     m_lpOleObject, DVASPECT_CONTENT, NULL, pformatetcIn->ptd);
	if (! pmedium->hGlobal)
	   sc = E_OUTOFMEMORY;
	else
	{
	   pmedium->tymed = TYMED_MFPICT;
	   sc = S_OK;
	}
    }
    else if ( (pformatetcIn->cfFormat == CF_HDROP) &&
	    (pformatetcIn->dwAspect == DVASPECT_CONTENT) &&
	    (pformatetcIn->tymed == TYMED_HGLOBAL) )
    {
	// testing for Win3.1 style drag drop.  If we offer
	// CF_HDROP ( basically, a list of filenames) in a data object
	// given to OLE DoDragDrop, then DoDragDrop will translate
	// OLE drag drop into Win3.1 drag drop for Win3.1 drop targets

	ULONG cbFile;
	char szFile[256];
	DROPFILESTRUCT *pdfs;
	char *pfile;


	cbFile = GetEnvironmentVariable("SystemRoot", szFile, sizeof(szFile));

	if( cbFile == 0 )
	{
	    sc = E_FAIL;
	    goto errRtn;
	}


	cbFile += sizeof("\\setup.txt");

	strcat(szFile, "\\setup.txt");
	
	pmedium->tymed = TYMED_HGLOBAL;

	pmedium->hGlobal = GlobalAlloc( GMEM_DDESHARE,
			    sizeof(DROPFILESTRUCT) +
			    cbFile + 3);

	if( pmedium->hGlobal )
	{
	    pdfs = (DROPFILESTRUCT *)GlobalLock(pmedium->hGlobal);

	    if( pdfs )
	    {
		pdfs->pFiles = sizeof(DROPFILESTRUCT);
		pfile = (char *)((BYTE *)pdfs) + sizeof(DROPFILESTRUCT);
		pdfs->fNC = FALSE;
		pdfs->fWide = FALSE;
		pdfs->pt.x = 0;
		pdfs->pt.y = 0;
		strcpy(pfile, szFile);
		// double-NULL terminate it
		pfile[strlen(pfile)+2] = '\0';

		GlobalUnlock(pmedium->hGlobal);

		sc = S_OK;
	    }
	    else
	    {
		sc = E_OUTOFMEMORY;
	    }
	}
	else
	{
	    sc = E_OUTOFMEMORY;
	}
    }

errRtn:

    return ResultFromScode( sc );
}

//**********************************************************************
//
// CDataXferObj::GetDataHere
//
// Purpose:
//
//      Called to get a data format in a caller supplied location
//
// Parameters:
//
//      LPFORMATETC pformatetc  - FORMATETC requested
//
//      LPSTGMEDIUM pmedium     - Medium to return the data
//
// Return Value:
//
//      NOERROR             - if OleStdGetOleObjectData success
//      DATA_E_FORMATETC    - We don't support the requested format
//
// Function Calls:
//      Function                    Location
//
//      TestDebugOut           Windows API
//      OleStdGetOleObjectData      OLE2UI API
//      IOleObject::QueryInterface  Object
//
//
//********************************************************************

STDMETHODIMP CDataXferObj::GetDataHere (
		LPFORMATETC pformatetc,
		LPSTGMEDIUM pmedium
)
{
    SCODE sc = DV_E_FORMATETC;

    TestDebugOut("In CDataXferObj::GetDataHere\r\n");

    // NOTE: pmedium is an IN parameter. we should NOT set
    //           pmedium->pUnkForRelease to NULL

    // Check the FORMATETC and fill pmedium if valid.
    if ( (pformatetc->cfFormat == g_cfEmbeddedObject) &&
         (pformatetc->dwAspect == DVASPECT_CONTENT) &&
         (pformatetc->tymed == TYMED_ISTORAGE) )
    {
         LPPERSISTSTORAGE pPersistStorage;

         /* render CF_EMBEDDEDOBJECT by asking the object to save
         **    into the IStorage allocated by the caller.
         */
         HRESULT hRes;

         if ((hRes=m_lpOleObject->QueryInterface(
                 IID_IPersistStorage, (LPVOID FAR*)&pPersistStorage))
             != ResultFromScode(S_OK))
         {
             return(hRes);
         }
         assert(pPersistStorage);
         HRESULT hrErr = OleStdGetOleObjectData(
                 pPersistStorage, pformatetc, pmedium,0 /*fUseMemory--N/A*/ );
         pPersistStorage->Release();
         sc = GetScode( hrErr );
    }
    return ResultFromScode( sc );
}