-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathIDropTarget.c
210 lines (187 loc) · 7.75 KB
/
IDropTarget.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
#include <windows.h>
#include <dwmapi.h>
#include <stdio.h>
#include <stdbool.h>
#include <shlobj.h>
#include "treeview_extension.h"
#include "utility.h"
#include "bnk-extract/defs.h"
typedef struct InternalIDropTarget {
IDropTargetVtbl* _vTable;
UINT reference_counter;
bool dragValid;
HWND window;
} InternalIDropTarget;
static HRESULT STDMETHODCALLTYPE QueryInterface(IDropTarget* This, REFIID riid, void** ppvObject)
{
if (!ppvObject) {
return E_POINTER;
}
if (riid == &IID_IUnknown || riid == &IID_IDropTarget) {
*ppvObject = This;
This->lpVtbl->AddRef(This);
return S_OK;
}
*ppvObject = NULL;
return E_NOINTERFACE;
}
static ULONG STDMETHODCALLTYPE AddRef(IDropTarget* This)
{
InternalIDropTarget* idropTarget = (InternalIDropTarget*) This;
idropTarget->reference_counter++;
return idropTarget->reference_counter;
}
static ULONG STDMETHODCALLTYPE Release(IDropTarget* This)
{
InternalIDropTarget* idropTarget = (InternalIDropTarget*) This;
idropTarget->reference_counter--;
UINT reference_counter = idropTarget->reference_counter;
if (idropTarget->reference_counter == 0) {
free(idropTarget->_vTable);
free(idropTarget);
}
return reference_counter;
}
static HRESULT STDMETHODCALLTYPE DragEnter(IDropTarget* This, IDataObject* pDataObj, DWORD grfKeyState, POINTL pt, DWORD* pdwEffect)
{
if (!pdwEffect) return E_INVALIDARG;
(void) grfKeyState;
(void) pt;
InternalIDropTarget* idropTarget = (InternalIDropTarget*) This;
*pdwEffect = DROPEFFECT_NONE;
printf("got into dragenter\n");
IEnumFORMATETC* enumFormatEtc;
if (pDataObj->lpVtbl->EnumFormatEtc(pDataObj, DATADIR_GET, &enumFormatEtc) == S_OK) {
FORMATETC formatEtc;
while (enumFormatEtc->lpVtbl->Next(enumFormatEtc, 1, &formatEtc, NULL) == S_OK) {
CoTaskMemFree(formatEtc.ptd);
if (formatEtc.cfFormat == CF_HDROP) {
idropTarget->dragValid = true;
STGMEDIUM stgMedium;
printf("return value of pDataObj->GetData: %ld\n", pDataObj->lpVtbl->GetData(pDataObj, &formatEtc, &stgMedium));
HDROP hDropInfo = (HDROP)(DROPFILES*) stgMedium.hGlobal;
UINT nFiles = DragQueryFile(hDropInfo, 0xFFFFFFFF, NULL, 0);
printf("nFiles: %u\n", nFiles);
for (UINT index = 0; index < nFiles; index++) {
UINT fileNameSize = DragQueryFile(hDropInfo, index, NULL, 0);
char fileNameBuffer[fileNameSize + 1];
DragQueryFile(hDropInfo, index, fileNameBuffer, fileNameSize + 1);
if (fileNameSize < 4 || memcmp(&fileNameBuffer[fileNameSize-4], ".wem", 4) != 0) {
idropTarget->dragValid = false;
break;
}
}
ReleaseStgMedium(&stgMedium);
break;
}
}
enumFormatEtc->lpVtbl->Release(enumFormatEtc);
}
return S_OK;
}
static HRESULT STDMETHODCALLTYPE DragOver(IDropTarget* This, DWORD grfKeyState, POINTL pt, DWORD* pdwEffect)
{
if (!pdwEffect) return E_INVALIDARG;
(void) grfKeyState;
InternalIDropTarget* idropTarget = (InternalIDropTarget*) This;
*pdwEffect = DROPEFFECT_NONE;
// early return in case DragEnter determined that this data is not drag-drop supported
if (!idropTarget->dragValid) return S_OK;
HTREEITEM hoveredItem = TreeView_PerformHitTest(pt.x, pt.y, NULL);
if (hoveredItem) *pdwEffect = DROPEFFECT_COPY;
return S_OK;
}
static HRESULT STDMETHODCALLTYPE DragLeave(IDropTarget *This)
{
InternalIDropTarget* idropTarget = (InternalIDropTarget*) This;
idropTarget->dragValid = false;
return S_OK;
}
static HRESULT STDMETHODCALLTYPE Drop(IDropTarget* This, IDataObject* pDataObj, DWORD grfKeyState, POINTL pt, DWORD* pdwEffect)
{
if (!pdwEffect) return E_INVALIDARG;
(void) grfKeyState;
InternalIDropTarget* idropTarget = (InternalIDropTarget*) This;
*pdwEffect = DROPEFFECT_COPY;
printf("got into drop\n");
POINT point = {
.x = pt.x,
.y = pt.y
};
ScreenToClient(treeview, &point);
TVHITTESTINFO hitTestInfo = {.pt = point};
HTREEITEM currentItem = TreeView_HitTest(treeview, &hitTestInfo);
printf("current item: %p\n", currentItem);
HTREEITEM rootItem = currentItem;
while ( (currentItem = TreeView_GetParent(treeview, currentItem)) ) {
rootItem = currentItem;
}
printf("root item: %p, current item: %p\n", rootItem, currentItem);
TVITEM tvItem = {
.mask = TVIF_PARAM,
.hItem = rootItem
};
TreeView_GetItem(treeview, &tvItem);
AudioDataList* wemDataList = (AudioDataList*) tvItem.lParam;
IEnumFORMATETC* enumFormatEtc;
if (pDataObj->lpVtbl->EnumFormatEtc(pDataObj, DATADIR_GET, &enumFormatEtc) == S_OK) {
FORMATETC formatEtc;
while (enumFormatEtc->lpVtbl->Next(enumFormatEtc, 1, &formatEtc, NULL) == S_OK) {
CoTaskMemFree(formatEtc.ptd);
if (formatEtc.cfFormat == CF_HDROP) {
STGMEDIUM stgMedium;
if (pDataObj->lpVtbl->GetData(pDataObj, &formatEtc, &stgMedium) != S_OK)
continue;
HDROP hDropInfo = (HDROP)(DROPFILES*) stgMedium.hGlobal;
UINT nFiles = DragQueryFile(hDropInfo, 0xFFFFFFFF, NULL, 0);
printf("nFiles: %u\n", nFiles);
for (UINT index = 0; index < nFiles; index++) {
UINT fileNameSize = DragQueryFile(hDropInfo, index, NULL, 0);
char fileNameBuffer[fileNameSize + 1];
DragQueryFile(hDropInfo, index, fileNameBuffer, fileNameSize + 1);
uint32_t current_file_id = strtol(strrchr(fileNameBuffer, '\\') + 1, NULL, 0);
AudioData* wemData = NULL;
printf("wemDataList: %p\n", wemDataList);
printf("length of list: %llu\n", wemDataList->length);
for (uint32_t i = 0; i < wemDataList->length; i++) {
printf("objects[%d]: %u\n", i, wemDataList->objects[i].id);
}
find_object_s(wemDataList, wemData, id, current_file_id);
printf("wemData: %p\n", wemData);
if (wemData) {
FILE* newDataFile = fopen(fileNameBuffer, "rb");
if (!newDataFile) continue;
fseek(newDataFile, 0, SEEK_END);
wemData->length = ftell(newDataFile);
free(wemData->data);
wemData->data = malloc(wemData->length);
rewind(newDataFile);
fread(wemData->data, wemData->length, 1, newDataFile);
fclose(newDataFile);
}
}
ReleaseStgMedium(&stgMedium);
break;
}
}
enumFormatEtc->lpVtbl->Release(enumFormatEtc);
}
idropTarget->dragValid = false;
return S_OK;
}
IDropTarget* CreateIDropTarget(HWND window)
{
InternalIDropTarget* newIDropTarget = malloc(sizeof(InternalIDropTarget));
newIDropTarget->_vTable = malloc(sizeof(IDropTargetVtbl));
newIDropTarget->_vTable->QueryInterface = QueryInterface;
newIDropTarget->_vTable->AddRef = AddRef;
newIDropTarget->_vTable->Release = Release;
newIDropTarget->_vTable->DragEnter = DragEnter;
newIDropTarget->_vTable->DragOver = DragOver;
newIDropTarget->_vTable->DragLeave = DragLeave;
newIDropTarget->_vTable->Drop = Drop;
newIDropTarget->reference_counter = 1;
newIDropTarget->dragValid = false;
newIDropTarget->window = window;
return (IDropTarget*) newIDropTarget;
}