forked from nakkag/CLCL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOleDragDrop.c
803 lines (684 loc) · 23.9 KB
/
OleDragDrop.c
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
/*
* CLCL
*
* OleDragDrop.c
*
* Copyright (C) 1996-2019 by Ohno Tomoaki. All rights reserved.
* https://www.nakka.com/
*/
/* Include Files */
#define _INC_OLE
#include <windows.h>
#undef _INC_OLE
#include <oleidl.h>
#include <objidl.h>
#include "Memory.h"
#include "OleDragDrop.h"
/* Clipboard format から Type of storage medium を取得 */
static DWORD FormatToTymed(const UINT cfFormat)
{
switch (cfFormat) {
case CF_BITMAP:
return TYMED_GDI;
case CF_METAFILEPICT:
return TYMED_MFPICT;
case CF_ENHMETAFILE:
return TYMED_ENHMF;
}
return TYMED_HGLOBAL;
}
/*
* IDropTarget
*/
static HRESULT STDMETHODCALLTYPE OLE_IDropTarget_QueryInterface(LPDROPTARGET pThis, REFIID riid, LPVOID *ppvObject);
static ULONG STDMETHODCALLTYPE OLE_IDropTarget_AddRef(LPDROPTARGET pThis);
static ULONG STDMETHODCALLTYPE OLE_IDropTarget_Release(LPDROPTARGET pThis);
static HRESULT STDMETHODCALLTYPE OLE_IDropTarget_DragEnter(LPDROPTARGET pThis, LPDATAOBJECT pdo, DWORD grfKeyState, POINTL pt, LPDWORD pdwEffect);
static HRESULT STDMETHODCALLTYPE OLE_IDropTarget_DragOver(LPDROPTARGET pThis, DWORD grfKeyState, POINTL pt, LPDWORD pdwEffect);
static HRESULT STDMETHODCALLTYPE OLE_IDropTarget_DragLeave(LPDROPTARGET pThis);
static HRESULT STDMETHODCALLTYPE OLE_IDropTarget_Drop(LPDROPTARGET pThis, LPDATAOBJECT pdo, DWORD grfKeyState, POINTL pt, LPDWORD pdwEffect);
static HRESULT APIPRIVATE OLE_IDropTarget_Internal_SendMessage(LPDROPTARGET pThis, UINT uNotify, LPDATAOBJECT pdo, DWORD grfKeyState, POINTL *ppt, LPDWORD pdwEffect);
/* IDropTarget Virtual Table */
static IDropTargetVtbl dtv = {
OLE_IDropTarget_QueryInterface,
OLE_IDropTarget_AddRef,
OLE_IDropTarget_Release,
OLE_IDropTarget_DragEnter,
OLE_IDropTarget_DragOver,
OLE_IDropTarget_DragLeave,
OLE_IDropTarget_Drop
};
typedef struct _IDROPTARGET_INTERNAL{
LPVOID lpVtbl;
ULONG m_refCnt;
HWND hWnd;
UINT uCallbackMessage;
UINT *cFormat;
int cfcnt;
IDROPTARGET_NOTIFY dtn;
}IDROPTARGET_INTERNAL , *LPIDROPTARGET_INTERNAL;
/* OLEのドロップターゲットとして登録する */
BOOL APIPRIVATE OLE_IDropTarget_RegisterDragDrop(HWND hWnd, UINT uCallbackMessage, UINT *cFormat, int cfcnt)
{
static IDROPTARGET_INTERNAL *pdti;
if ((pdti = mem_calloc(sizeof(IDROPTARGET_INTERNAL))) == NULL) {
return FALSE;
}
pdti->lpVtbl = (LPVOID)&dtv;
pdti->m_refCnt = 0;
pdti->hWnd = hWnd; /* メッセージを受け取るウィンドウ */
pdti->uCallbackMessage = uCallbackMessage; /* メッセージ */
if ((pdti->cFormat = (UINT *)mem_calloc(sizeof(UINT) * cfcnt)) == NULL) {
mem_free(&pdti);
return FALSE;
}
CopyMemory(pdti->cFormat, cFormat, sizeof(UINT) * cfcnt);
pdti->cfcnt = cfcnt; /* クリップボードフォーマットの数 */
return (S_OK == RegisterDragDrop(hWnd, (LPDROPTARGET)pdti));
}
/* OLEのドロップターゲットを解除する */
void APIPRIVATE OLE_IDropTarget_RevokeDragDrop(HWND hWnd)
{
RevokeDragDrop(hWnd);
}
static HRESULT STDMETHODCALLTYPE OLE_IDropTarget_QueryInterface(LPDROPTARGET pThis, REFIID riid, PVOID *ppvObject)
{
// 要求されたIIDと同じ場合はオブジェクトを返す
if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDropTarget)) {
*ppvObject = (LPVOID)pThis;
((LPUNKNOWN)*ppvObject)->lpVtbl->AddRef((LPUNKNOWN)*ppvObject);
return S_OK;
}
*ppvObject = NULL;
return ResultFromScode(E_NOINTERFACE);
}
static ULONG STDMETHODCALLTYPE OLE_IDropTarget_AddRef(LPDROPTARGET pThis)
{
CONST LPIDROPTARGET_INTERNAL pdti = (LPIDROPTARGET_INTERNAL)pThis;
/* reference countをインクリメントする */
pdti->m_refCnt++;
return pdti->m_refCnt;
}
static ULONG STDMETHODCALLTYPE OLE_IDropTarget_Release(LPDROPTARGET pThis)
{
CONST LPIDROPTARGET_INTERNAL pdti = (LPIDROPTARGET_INTERNAL)pThis;
/* reference countをデクリメントする */
pdti->m_refCnt--;
/* reference countが 0 になった場合はオブジェクトの解放を行う */
if (pdti->m_refCnt == 0L) {
if (pdti->cFormat != NULL) {
mem_free(&pdti->cFormat);
}
mem_free((void **)&pdti);
return 0L;
}
return pdti->m_refCnt;
}
static HRESULT STDMETHODCALLTYPE OLE_IDropTarget_DragEnter(LPDROPTARGET pThis, LPDATAOBJECT pdo, DWORD grfKeyState, POINTL pt, LPDWORD pdwEffect)
{
return OLE_IDropTarget_Internal_SendMessage(pThis, IDROPTARGET_NOTIFY_DRAGENTER, pdo, grfKeyState, &pt, pdwEffect);
}
static HRESULT STDMETHODCALLTYPE OLE_IDropTarget_DragOver(LPDROPTARGET pThis, DWORD grfKeyState, POINTL pt, LPDWORD pdwEffect)
{
return OLE_IDropTarget_Internal_SendMessage(pThis, IDROPTARGET_NOTIFY_DRAGOVER, NULL, grfKeyState, &pt, pdwEffect);
}
static HRESULT STDMETHODCALLTYPE OLE_IDropTarget_DragLeave(LPDROPTARGET pThis)
{
return OLE_IDropTarget_Internal_SendMessage(pThis, IDROPTARGET_NOTIFY_DRAGLEAVE, NULL, 0, NULL, NULL);
}
static HRESULT STDMETHODCALLTYPE OLE_IDropTarget_Drop(LPDROPTARGET pThis, LPDATAOBJECT pdo, DWORD grfKeyState, POINTL pt, LPDWORD pdwEffect)
{
return OLE_IDropTarget_Internal_SendMessage(pThis, IDROPTARGET_NOTIFY_DROP, pdo, grfKeyState, &pt, pdwEffect);
}
static HRESULT APIPRIVATE OLE_IDropTarget_Internal_SendMessage(LPDROPTARGET pThis, UINT uNotify, LPDATAOBJECT pdo, DWORD grfKeyState, POINTL *ppt, LPDWORD pdwEffect)
{
CONST LPIDROPTARGET_INTERNAL pdti = (LPIDROPTARGET_INTERNAL)pThis;
CONST LPIDROPTARGET_NOTIFY pdtn = &(pdti->dtn);
STGMEDIUM sm = { 0 };
UINT cfFormat = 0;
int i;
if (pdo) {
/* 対応しているクリップボードフォーマットがあるか調べる */
for (i = 0; i < pdti->cfcnt; i++) {
if (DropTarget_QueryGetData(pdo, pdti->cFormat[i]) == S_OK) {
cfFormat = pdti->cFormat[i];
break;
}
}
/* クリップボードフォーマットからデータを取得する */
if (cfFormat != 0 && DropTarget_GetData(pdo, cfFormat, &sm) != S_OK) {
cfFormat = 0;
}
}
pdtn->ppt = ppt; /* マウスポインタの位置 */
pdtn->grfKeyState = grfKeyState; /* キー、マウスボタンの状態 */
pdtn->cfFormat = cfFormat; /* クリップボードフォーマット */
if (pdo) {
pdtn->hMem = sm.hGlobal; /* 実データ */
}
pdtn->pdo = pdo; /* IDataObject */
/* ウィンドウにイベントを通知する */
SendMessage(pdti->hWnd, pdti->uCallbackMessage, (WPARAM)uNotify, (LPARAM)pdtn);
/* クリップボード形式のデータの解放 */
if (cfFormat) {
ReleaseStgMedium(&sm);
}
/* 効果の設定 */
if (pdwEffect) {
*pdwEffect &= pdtn->dwEffect;
if ((*pdwEffect & DROPEFFECT_MOVE) && (*pdwEffect & DROPEFFECT_COPY)) {
*pdwEffect ^= DROPEFFECT_COPY;
pdtn->dwEffect ^= DROPEFFECT_COPY;
}
}
return S_OK;
}
HRESULT APIPRIVATE DropTarget_GetData(LPDATAOBJECT pdo, UINT cfFormat, LPSTGMEDIUM psm)
{
FORMATETC fmt;
/* IDataObjectにクリップボード形式のデータを要求する */
fmt.cfFormat = cfFormat;
fmt.ptd = NULL;
fmt.dwAspect = DVASPECT_CONTENT;
fmt.lindex = -1;
fmt.tymed = FormatToTymed(cfFormat);
return pdo->lpVtbl->GetData(pdo, &fmt, psm);
}
HRESULT APIPRIVATE DropTarget_QueryGetData(LPDATAOBJECT pdo, UINT cfFormat)
{
FORMATETC fmt;
/* IDataObjectに指定のクリップボードフォーマットが存在するか問い合わせる */
fmt.cfFormat = cfFormat;
fmt.ptd = NULL;
fmt.dwAspect = DVASPECT_CONTENT;
fmt.lindex = -1;
fmt.tymed = FormatToTymed(cfFormat);
return pdo->lpVtbl->QueryGetData(pdo, &fmt);
}
int APIPRIVATE DropTarget_EnumFormatEtc(LPDATAOBJECT pdo, UINT *fmt_list)
{
IEnumFORMATETC *ppenumFormatetc;
LPENUMFORMATETC lpef;
FORMATETC fmt;
int i;
if (pdo->lpVtbl->EnumFormatEtc(pdo, DATADIR_GET, &ppenumFormatetc) != S_OK) {
return 0;
}
lpef = ((LPENUMFORMATETC)ppenumFormatetc);
lpef->lpVtbl->Reset(lpef);
i = 0;
while (lpef->lpVtbl->Next(lpef, 1, &fmt, NULL) == S_OK) {
if (fmt_list != NULL) {
fmt_list[i] = fmt.cfFormat;
}
i++;
}
lpef->lpVtbl->Release(lpef);
return i;
}
/*
* IEnumFORMATETC
*/
static HRESULT STDMETHODCALLTYPE OLE_IEnumFORMATETC_QueryInterface(LPENUMFORMATETC lpThis, REFIID riid, LPVOID FAR* lplpvObj);
static ULONG STDMETHODCALLTYPE OLE_IEnumFORMATETC_AddRef(LPENUMFORMATETC lpThis);
static ULONG STDMETHODCALLTYPE OLE_IEnumFORMATETC_Release(LPENUMFORMATETC lpThis);
static HRESULT STDMETHODCALLTYPE OLE_IEnumFORMATETC_Next(LPENUMFORMATETC lpThis, ULONG celt, FORMATETC *rgelt, ULONG *pceltFetched);
static HRESULT STDMETHODCALLTYPE OLE_IEnumFORMATETC_Skip(LPENUMFORMATETC lpThis, ULONG celt);
static HRESULT STDMETHODCALLTYPE OLE_IEnumFORMATETC_Reset(LPENUMFORMATETC lpThis);
static HRESULT STDMETHODCALLTYPE OLE_IEnumFORMATETC_Clone(LPENUMFORMATETC lpThis, IEnumFORMATETC **ppenum);
/* IEnumFORMATETC Virtual Table */
static IEnumFORMATETCVtbl efv = {
OLE_IEnumFORMATETC_QueryInterface,
OLE_IEnumFORMATETC_AddRef,
OLE_IEnumFORMATETC_Release,
OLE_IEnumFORMATETC_Next,
OLE_IEnumFORMATETC_Skip,
OLE_IEnumFORMATETC_Reset,
OLE_IEnumFORMATETC_Clone
};
typedef struct _IENUMFORMATETC_INTERNAL{
LPVOID lpVtbl;
ULONG m_refCnt;
LPUNKNOWN m_pUnknownObj;
ULONG m_currElement;
ULONG m_numFormats;
LPFORMATETC m_formatList;
}IENUMFORMATETC_INTERNAL , *LPIENUMFORMATETC_INTERNAL;
static HRESULT STDMETHODCALLTYPE OLE_IEnumFORMATETC_QueryInterface(LPENUMFORMATETC lpThis, REFIID riid, LPVOID FAR* lplpvObj)
{
// 要求されたIIDと同じ場合はオブジェクトを返す
if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IEnumFORMATETC)) {
*lplpvObj = (LPVOID) lpThis;
((LPUNKNOWN)*lplpvObj)->lpVtbl->AddRef(((LPUNKNOWN)*lplpvObj));
return S_OK;
}
*lplpvObj = NULL;
return ResultFromScode(E_NOINTERFACE);
}
static ULONG STDMETHODCALLTYPE OLE_IEnumFORMATETC_AddRef(LPENUMFORMATETC lpThis)
{
CONST LPIENUMFORMATETC_INTERNAL pefi = (LPIENUMFORMATETC_INTERNAL)lpThis;
/* reference countをインクリメントする */
pefi->m_refCnt++;
/* 親オブジェクトのreference countを加える */
pefi->m_pUnknownObj->lpVtbl->AddRef(pefi->m_pUnknownObj);
return pefi->m_refCnt;
}
static ULONG STDMETHODCALLTYPE OLE_IEnumFORMATETC_Release(LPENUMFORMATETC lpThis)
{
CONST LPIENUMFORMATETC_INTERNAL pefi = (LPIENUMFORMATETC_INTERNAL)lpThis;
/* reference countをデクリメントする */
pefi->m_refCnt--;
/* 親オブジェクトのreference countを減らす */
pefi->m_pUnknownObj->lpVtbl->Release(pefi->m_pUnknownObj);
/* reference countが 0 になった場合はオブジェクトの解放を行う */
if (pefi->m_refCnt == 0L) {
if (pefi->m_formatList != NULL) {
mem_free(&pefi->m_formatList);
}
mem_free((void **)&pefi);
return 0L;
}
return pefi->m_refCnt;
}
static HRESULT STDMETHODCALLTYPE OLE_IEnumFORMATETC_Next(LPENUMFORMATETC lpThis, ULONG celt, FORMATETC *rgelt, ULONG *pceltFetched)
{
ULONG i;
ULONG cReturn=0L;
LPIENUMFORMATETC_INTERNAL lpefi = ((LPIENUMFORMATETC_INTERNAL)lpThis);
if (pceltFetched) {
*pceltFetched = 0L;
}
if (lpefi->m_formatList == NULL) {
return ResultFromScode(S_FALSE);
}
if (rgelt == NULL) {
if (celt == 1) {
return ResultFromScode(S_FALSE);
} else {
return ResultFromScode(E_POINTER);
}
}
if (lpefi->m_currElement >= lpefi->m_numFormats) {
return ResultFromScode(S_FALSE);
}
for (i = 0; i < celt && lpefi->m_currElement < lpefi->m_numFormats; i++, lpefi->m_currElement++) {
*rgelt = lpefi->m_formatList[lpefi->m_currElement];
rgelt++;
}
if (pceltFetched != NULL) {
*pceltFetched = i;
}
return S_OK;
}
static HRESULT STDMETHODCALLTYPE OLE_IEnumFORMATETC_Skip(LPENUMFORMATETC lpThis, ULONG celt)
{
LPIENUMFORMATETC_INTERNAL lpefi = ((LPIENUMFORMATETC_INTERNAL)lpThis);
lpefi->m_currElement += celt;
if (lpefi->m_currElement > lpefi->m_numFormats) {
lpefi->m_currElement = lpefi->m_numFormats;
return ResultFromScode(S_FALSE);
}
return S_OK;
}
static HRESULT STDMETHODCALLTYPE OLE_IEnumFORMATETC_Reset(LPENUMFORMATETC lpThis)
{
LPIENUMFORMATETC_INTERNAL lpefi = ((LPIENUMFORMATETC_INTERNAL)lpThis);
lpefi->m_currElement = 0L;
return S_OK;
}
static HRESULT STDMETHODCALLTYPE OLE_IEnumFORMATETC_Clone(LPENUMFORMATETC lpThis, IEnumFORMATETC **ppenum)
{
LPIENUMFORMATETC_INTERNAL pNew;
LPIENUMFORMATETC_INTERNAL lpefi = ((LPIENUMFORMATETC_INTERNAL)lpThis);
UINT i;
/* IEnumFORMATETCを作成する */
pNew = mem_calloc(sizeof(IENUMFORMATETC_INTERNAL));
if (pNew == NULL) {
return ResultFromScode(E_OUTOFMEMORY);
}
pNew->lpVtbl = (LPVOID)&efv;
pNew->m_refCnt = 0;
pNew->m_currElement = 0;
pNew->m_pUnknownObj = lpefi->m_pUnknownObj;
pNew->m_numFormats = lpefi->m_numFormats;
/* クリップボードフォーマットのリストをコピーする */
pNew->m_formatList = mem_calloc(sizeof(FORMATETC) * pNew->m_numFormats);
if (pNew->m_formatList != NULL) {
for (i = 0; i < pNew->m_numFormats; i++) {
pNew->m_formatList[i] = lpefi->m_formatList[i];
}
}
*ppenum = (struct IEnumFORMATETC *)pNew;
if (pNew == NULL) {
return ResultFromScode(E_OUTOFMEMORY);
}
((LPENUMFORMATETC)pNew)->lpVtbl->AddRef(((LPENUMFORMATETC)pNew));
pNew->m_currElement = lpefi->m_currElement;
return S_OK;
}
/*
* IDataObject
*/
static HRESULT STDMETHODCALLTYPE OLE_IDataObject_QueryInterface(LPDATAOBJECT lpThis, REFIID riid, LPVOID FAR *lplpvObj);
static ULONG STDMETHODCALLTYPE OLE_IDataObject_AddRef(LPDATAOBJECT lpThis);
static ULONG STDMETHODCALLTYPE OLE_IDataObject_Release(LPDATAOBJECT lpThis);
static HRESULT STDMETHODCALLTYPE OLE_IDataObject_GetData(LPDATAOBJECT lpThis, FORMATETC *pFormatetc, STGMEDIUM *pmedium);
static HRESULT STDMETHODCALLTYPE OLE_IDataObject_GetDataHere(LPDATAOBJECT lpThis, FORMATETC *pFormatetc, STGMEDIUM *pmedium);
static HRESULT STDMETHODCALLTYPE OLE_IDataObject_QueryGetData(LPDATAOBJECT lpThis, FORMATETC *pFormatetc);
static HRESULT STDMETHODCALLTYPE OLE_IDataObject_GetCanonicalFormatEtc(LPDATAOBJECT lpThis, FORMATETC *pFormatetcIn, FORMATETC *pFormatetcOut);
static HRESULT STDMETHODCALLTYPE OLE_IDataObject_SetData(LPDATAOBJECT lpThis, FORMATETC *pFormatetc, STGMEDIUM *pmedium, BOOL fRelease);
static HRESULT STDMETHODCALLTYPE OLE_IDataObject_EnumFormatEtc(LPDATAOBJECT lpThis, DWORD dwDirection, IEnumFORMATETC **ppenumFormatetc);
static HRESULT STDMETHODCALLTYPE OLE_IDataObject_DAdvise(LPDATAOBJECT lpThis, FORMATETC *pFormatetc, DWORD advf, IAdviseSink *pAdvSink, DWORD *pdwConnection);
static HRESULT STDMETHODCALLTYPE OLE_IDataObject_DUnadvise(LPDATAOBJECT lpThis, DWORD dwConnection);
static HRESULT STDMETHODCALLTYPE OLE_IDataObject_EnumDAdvise(LPDATAOBJECT lpThis, IEnumSTATDATA **ppenumAdvise);
/* IDataObject Virtual Table */
static IDataObjectVtbl dov = {
OLE_IDataObject_QueryInterface,
OLE_IDataObject_AddRef,
OLE_IDataObject_Release,
OLE_IDataObject_GetData,
OLE_IDataObject_GetDataHere,
OLE_IDataObject_QueryGetData,
OLE_IDataObject_GetCanonicalFormatEtc,
OLE_IDataObject_SetData,
OLE_IDataObject_EnumFormatEtc,
OLE_IDataObject_DAdvise,
OLE_IDataObject_DUnadvise,
OLE_IDataObject_EnumDAdvise
};
typedef struct _IDATAOBJECT_INTERNAL{
LPVOID lpVtbl;
ULONG m_refCnt;
UINT m_numTypes;
UINT m_maxTypes;
FORMATETC *m_typeList;
HWND hWnd;
UINT uCallbackMessage;
}IDATAOBJECT_INTERNAL , *LPIDATAOBJECT_INTERNAL;
static HRESULT STDMETHODCALLTYPE OLE_IDataObject_QueryInterface(LPDATAOBJECT lpThis, REFIID riid, LPVOID FAR *lplpvObj)
{
// 要求されたIIDと同じ場合はオブジェクトを返す
if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDataObject)) {
*lplpvObj = lpThis;
((LPUNKNOWN)*lplpvObj)->lpVtbl->AddRef(((LPUNKNOWN)*lplpvObj));
return S_OK;
}
*lplpvObj = NULL;
return ResultFromScode(E_NOINTERFACE);
}
static ULONG STDMETHODCALLTYPE OLE_IDataObject_AddRef(LPDATAOBJECT lpThis)
{
CONST LPIDATAOBJECT_INTERNAL pdoi = (LPIDATAOBJECT_INTERNAL)lpThis;
/* reference countをインクリメントする */
pdoi->m_refCnt++;
return pdoi->m_refCnt;
}
static ULONG STDMETHODCALLTYPE OLE_IDataObject_Release(LPDATAOBJECT lpThis)
{
CONST LPIDATAOBJECT_INTERNAL pdoi = (LPIDATAOBJECT_INTERNAL)lpThis;
/* reference countをデクリメントする */
pdoi->m_refCnt--;
/* reference countが 0 になった場合はオブジェクトの解放を行う */
if (pdoi->m_refCnt == 0L) {
if (pdoi->m_typeList != NULL) {
mem_free(&pdoi->m_typeList);
}
mem_free((void **)&pdoi);
return 0L;
}
return pdoi->m_refCnt;
}
static HRESULT STDMETHODCALLTYPE OLE_IDataObject_GetData(LPDATAOBJECT lpThis, FORMATETC *pFormatetc, STGMEDIUM *pmedium)
{
CONST LPIDATAOBJECT_INTERNAL pdoi = (LPIDATAOBJECT_INTERNAL)lpThis;
HGLOBAL hMem;
UINT i;
/* 要求されたクリップボードフォーマットが存在するか調べる */
for (i = 0; i < pdoi->m_numTypes; i++) {
if (pdoi->m_typeList[i].cfFormat == pFormatetc->cfFormat) {
break;
}
}
if (i == pdoi->m_numTypes) {
/* 要求されたクリップボードフォーマットをサポートしてない場合 */
return ResultFromScode(DV_E_FORMATETC);
}
/* ウィンドウにデータの要求を行う */
SendMessage(pdoi->hWnd, pdoi->uCallbackMessage, (WPARAM)pdoi->m_typeList[i].cfFormat, (LPARAM)&hMem);
if (hMem == NULL) {
return ResultFromScode(STG_E_MEDIUMFULL);
}
pmedium->hGlobal = hMem;
pmedium->tymed = FormatToTymed(pFormatetc->cfFormat);
pmedium->pUnkForRelease = NULL;
return S_OK;
}
static HRESULT STDMETHODCALLTYPE OLE_IDataObject_GetDataHere(LPDATAOBJECT lpThis, FORMATETC *pFormatetc, STGMEDIUM *pmedium)
{
return ResultFromScode(E_NOTIMPL);
}
static HRESULT STDMETHODCALLTYPE OLE_IDataObject_QueryGetData(LPDATAOBJECT lpThis, FORMATETC *pFormatetc)
{
CONST LPIDATAOBJECT_INTERNAL pdoi = (LPIDATAOBJECT_INTERNAL)lpThis;
UINT i;
/* 要求されたクリップボードフォーマットが存在するか調べる */
for (i = 0; i < pdoi->m_numTypes; i++) {
if (pdoi->m_typeList[i].cfFormat == pFormatetc->cfFormat) {
return S_OK;
}
}
return ResultFromScode(DV_E_FORMATETC);
}
static HRESULT STDMETHODCALLTYPE OLE_IDataObject_GetCanonicalFormatEtc(LPDATAOBJECT lpThis, FORMATETC *pFormatetcIn, FORMATETC *pFormatetcOut)
{
return ResultFromScode(E_NOTIMPL);
}
static HRESULT STDMETHODCALLTYPE OLE_IDataObject_SetData(LPDATAOBJECT lpThis, FORMATETC *pFormatetc, STGMEDIUM *pmedium, BOOL fRelease)
{
return ResultFromScode(E_NOTIMPL);
}
static HRESULT STDMETHODCALLTYPE OLE_IDataObject_EnumFormatEtc(LPDATAOBJECT lpThis, DWORD dwDirection, IEnumFORMATETC **ppenumFormatetc)
{
CONST LPIDATAOBJECT_INTERNAL pdoi = (LPIDATAOBJECT_INTERNAL)lpThis;
static IENUMFORMATETC_INTERNAL *pefi;
UINT i;
if (ppenumFormatetc == NULL) {
return ResultFromScode(E_INVALIDARG);
}
if (dwDirection != DATADIR_GET) {
*ppenumFormatetc = NULL;
return ResultFromScode(E_NOTIMPL);
}
/* IEnumFORMATETCを作成する */
if ((pefi = mem_calloc(sizeof(IENUMFORMATETC_INTERNAL))) == NULL) {
return E_OUTOFMEMORY;
}
pefi->lpVtbl = (LPVOID)&efv;
pefi->m_refCnt = 0;
pefi->m_currElement = 0;
pefi->m_pUnknownObj = (struct IUnknown *)lpThis;
pefi->m_numFormats = pdoi->m_numTypes;
/* クリップボードフォーマットのリストをコピーする */
pefi->m_formatList = mem_calloc(sizeof(FORMATETC) * pefi->m_numFormats);
if (pefi->m_formatList != NULL) {
for (i = 0; i < pefi->m_numFormats; i++) {
pefi->m_formatList[i] = pdoi->m_typeList[i];
}
}
((LPENUMFORMATETC)pefi)->lpVtbl->AddRef(((LPENUMFORMATETC)pefi));
if ((*ppenumFormatetc = (struct IEnumFORMATETC *)pefi) == NULL) {
return E_OUTOFMEMORY;
}
return S_OK;
}
static HRESULT STDMETHODCALLTYPE OLE_IDataObject_DAdvise(LPDATAOBJECT lpThis, FORMATETC *pFormatetc, DWORD advf, IAdviseSink *pAdvSink, DWORD *pdwConnection)
{
return ResultFromScode(OLE_E_ADVISENOTSUPPORTED);
}
static HRESULT STDMETHODCALLTYPE OLE_IDataObject_DUnadvise(LPDATAOBJECT lpThis, DWORD dwConnection)
{
return ResultFromScode(OLE_E_NOCONNECTION);
}
static HRESULT STDMETHODCALLTYPE OLE_IDataObject_EnumDAdvise(LPDATAOBJECT lpThis, IEnumSTATDATA **ppenumAdvise)
{
return ResultFromScode(OLE_E_ADVISENOTSUPPORTED);
}
/*
* IDropSource
*/
static HRESULT STDMETHODCALLTYPE OLE_IDropSource_QueryInterface(LPDROPSOURCE lpThis, REFIID riid, LPVOID FAR* lplpvObj);
static ULONG STDMETHODCALLTYPE OLE_IDropSource_AddRef(LPDROPSOURCE lpThis);
static ULONG STDMETHODCALLTYPE OLE_IDropSource_Release(LPDROPSOURCE lpThis);
static HRESULT STDMETHODCALLTYPE OLE_IDropSource_QueryContinueDrag(LPDROPSOURCE lpThis, BOOL fEscapePressed, DWORD grfKeyState);
static HRESULT STDMETHODCALLTYPE OLE_IDropSource_GiveFeedback(LPDROPSOURCE lpThis, DWORD dwEffect);
/* IDropSource Virtual Table */
static IDropSourceVtbl dsv = {
OLE_IDropSource_QueryInterface,
OLE_IDropSource_AddRef,
OLE_IDropSource_Release,
OLE_IDropSource_QueryContinueDrag,
OLE_IDropSource_GiveFeedback,
};
typedef struct _IDROPSOURCE_INTERNAL{
LPVOID lpVtbl;
ULONG m_refCnt;
DWORD m_keyState;
DWORD m_button;
}IDROPSOURCE_INTERNAL , *LPIDROPSOURCE_INTERNAL;
static HRESULT STDMETHODCALLTYPE OLE_IDropSource_QueryInterface(LPDROPSOURCE lpThis, REFIID riid, LPVOID FAR *lplpvObj)
{
// 要求されたIIDと同じ場合はオブジェクトを返す
if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDropSource)) {
*lplpvObj = (LPVOID) lpThis;
((LPUNKNOWN)*lplpvObj)->lpVtbl->AddRef(((LPUNKNOWN)*lplpvObj));
return S_OK;
}
*lplpvObj = NULL;
return ResultFromScode(E_NOINTERFACE);
}
static ULONG STDMETHODCALLTYPE OLE_IDropSource_AddRef(LPDROPSOURCE lpThis)
{
CONST LPIDROPSOURCE_INTERNAL pdsi = (LPIDROPSOURCE_INTERNAL)lpThis;
/* reference countをインクリメントする */
pdsi->m_refCnt++;
return pdsi->m_refCnt;
}
static ULONG STDMETHODCALLTYPE OLE_IDropSource_Release(LPDROPSOURCE lpThis)
{
CONST LPIDROPSOURCE_INTERNAL pdsi = (LPIDROPSOURCE_INTERNAL)lpThis;
/* reference countをデクリメントする */
pdsi->m_refCnt--;
/* reference countが 0 になった場合はオブジェクトの解放を行う */
if (pdsi->m_refCnt == 0L) {
mem_free((void **)&pdsi);
return 0L;
}
return pdsi->m_refCnt;
}
static HRESULT STDMETHODCALLTYPE OLE_IDropSource_QueryContinueDrag(LPDROPSOURCE lpThis, BOOL fEscapePressed, DWORD grfKeyState)
{
CONST LPIDROPSOURCE_INTERNAL pdsi = (LPIDROPSOURCE_INTERNAL)lpThis;
if (fEscapePressed) {
/* エスケープが押された場合はキャンセルにする */
return ResultFromScode(DRAGDROP_S_CANCEL);
}
/* 指定のキーやマウスが離された場合はドロップにする */
if (pdsi->m_button == 0) {
if (grfKeyState != pdsi->m_keyState) {
return ResultFromScode(DRAGDROP_S_DROP);
}
} else {
if (!(grfKeyState & pdsi->m_button)) {
return ResultFromScode(DRAGDROP_S_DROP);
}
}
return S_OK;
}
static HRESULT STDMETHODCALLTYPE OLE_IDropSource_GiveFeedback(LPDROPSOURCE lpThis, DWORD dwEffect)
{
return ResultFromScode(DRAGDROP_S_USEDEFAULTCURSORS);
}
/* ドラッグ&ドロップの開始 */
int APIPRIVATE OLE_IDropSource_Start(HWND hWnd, UINT uCallbackMessage, UINT *ClipFormtList, int cfcnt, int Effect)
{
static IDATAOBJECT_INTERNAL *pdoi;
static IDROPSOURCE_INTERNAL *pdsi;
DWORD lpdwEffect;
DWORD keyState;
int i;
int ret;
/* IDataObjectの作成 */
if ((pdoi = mem_calloc(sizeof(IDATAOBJECT_INTERNAL))) == NULL) {
return -1;
}
pdoi->lpVtbl = (LPVOID)&dov;
pdoi->m_refCnt = 0;
pdoi->m_numTypes = cfcnt;
pdoi->m_maxTypes = cfcnt;
/* 有効なクリップボードフォーマットを設定する */
if ((pdoi->m_typeList = mem_calloc(sizeof(FORMATETC) * cfcnt)) == NULL) {
mem_free(&pdoi);
return -1;
}
for (i = 0; i < cfcnt; i++) {
pdoi->m_typeList[i].cfFormat = ClipFormtList[i];
pdoi->m_typeList[i].ptd = NULL;
pdoi->m_typeList[i].dwAspect = DVASPECT_CONTENT;
pdoi->m_typeList[i].lindex = -1;
pdoi->m_typeList[i].tymed = FormatToTymed(ClipFormtList[i]);
}
pdoi->hWnd = hWnd;
pdoi->uCallbackMessage = uCallbackMessage;
((LPDATAOBJECT)pdoi)->lpVtbl->AddRef((LPDATAOBJECT)pdoi);
/* IDropSourceの作成 */
if ((pdsi = mem_calloc(sizeof(IDROPSOURCE_INTERNAL))) == NULL) {
/* IDataObjectを解放する */
((LPDATAOBJECT)pdoi)->lpVtbl->Release((LPDATAOBJECT)pdoi);
return -1;
}
pdsi->lpVtbl = (LPVOID)&dsv;
pdsi->m_refCnt = 0;
/* 有効なキーとマウスの状態 */
if (GetKeyState(VK_RBUTTON) & 0x8000) {
pdsi->m_button = MK_RBUTTON;
} else {
pdsi->m_button = MK_LBUTTON;
}
/* 現在のキーとマウスの状態 */
keyState = 0;
if (GetKeyState(VK_SHIFT) & 0x8000) {
keyState |= MK_SHIFT;
}
if (GetKeyState(VK_CONTROL) & 0x8000) {
keyState |= MK_CONTROL;
}
if (GetKeyState(VK_MENU) & 0x8000) {
keyState |= MK_ALT;
}
if (GetKeyState(VK_LBUTTON) & 0x8000) {
keyState |= MK_LBUTTON;
}
if (GetKeyState(VK_MBUTTON) & 0x8000) {
keyState |= MK_MBUTTON;
}
if (GetKeyState(VK_RBUTTON) & 0x8000) {
keyState |= MK_RBUTTON;
}
pdsi->m_keyState = keyState;
((LPDROPSOURCE)pdsi)->lpVtbl->AddRef((LPDROPSOURCE)pdsi);
lpdwEffect = 0;
/* ドラッグ&ドロップの開始 */
ret = DoDragDrop((LPDATAOBJECT)pdoi, (LPDROPSOURCE)pdsi, Effect, &lpdwEffect);
/* IDataObjectを解放する */
((LPDATAOBJECT)pdoi)->lpVtbl->Release((LPDATAOBJECT)pdoi);
/* IDropSourceを解放する */
((LPDROPSOURCE)pdsi)->lpVtbl->Release((LPDROPSOURCE)pdsi);
if (ret == DRAGDROP_S_DROP) {
/* ドロップ先のアプリケーションが設定した効果を返す */
return lpdwEffect;
}
return -1;
}
/* End of source */