This repository has been archived by the owner on Jul 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Window.cpp
575 lines (478 loc) · 12.5 KB
/
Window.cpp
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
#include "StdAfx.h"
const TCHAR Window::CLASSNAME[] = TEXT("ArcheRipperWndClass");
const COLORREF Window::BGCOLOR = RGB(232, 232, 216);
const COLORREF Window::FGCOLOR = RGB(104, 64, 16);
HBRUSH Window::s_brush;
ATOM Window::s_atom;
Window::Window()
:m_hWnd(NULL),
m_hCtlLv(NULL)
{
}
HRESULT Window::Register()
{
s_brush = CreateSolidBrush(BGCOLOR);
const WNDCLASSEX wcx =
{
sizeof(WNDCLASSEX),
CS_HREDRAW|CS_VREDRAW,
StaticWindowProc,
0, sizeof(Window*),
THIS_HINSTANCE,
LoadIcon(THIS_HINSTANCE, MAKEINTRESOURCE(1)),
LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW)),
s_brush,
NULL,
CLASSNAME,
reinterpret_cast<HICON>(LoadImage(THIS_HINSTANCE, MAKEINTRESOURCE(1), IMAGE_ICON,
GetSystemMetrics(SM_CXSMICON),
GetSystemMetrics(SM_CYSMICON),
LR_SHARED))
};
s_atom = RegisterClassEx(&wcx);
if (s_atom)
return S_OK;
else
return HRESULT_FROM_WIN32(GetLastError());
}
void Window::Unregister()
{
UnregisterClass(MAKEINTATOM(s_atom), THIS_HINSTANCE);
DeleteObject(s_brush);
}
HRESULT Window::Create()
{
if (CreateWindowEx(
0,
CLASSNAME,
TEXT("Archeage Crest Ripper"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
THIS_HINSTANCE,
this))
{
ShowWindow(m_hWnd, SW_SHOWDEFAULT);
UpdateWindow(m_hWnd);
return S_OK;
}
return HRESULT_FROM_WIN32(GetLastError());
}
void Window::Destroy()
{
DestroyWindow(m_hWnd);
}
void Window::Enumerate()
{
auto loader = make_shared<CrestLoader>();
if (FAILED(loader->Initialize()))
return;
m_crests = Crest::Enumerate(loader);
if (!m_crests.empty() &&
SUCCEEDED(m_iml->SetImageCount(m_crests.size())))
SendMessage(m_hCtlLv, LVM_SETITEMCOUNT, m_crests.size(), 0);
}
LRESULT CALLBACK Window::StaticWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
Window* pwnd;
if (uMsg == WM_NCCREATE)
{
pwnd = reinterpret_cast<Window*>(reinterpret_cast<CREATESTRUCT*>(lParam)->lpCreateParams);
if (pwnd)
{
pwnd->m_hWnd = hWnd;
SetWindowLongPtr(hWnd, 0, reinterpret_cast<LONG_PTR>(pwnd));
}
}
else
pwnd = reinterpret_cast<Window*>(GetWindowLongPtr(hWnd, 0));
LRESULT result;
if (pwnd)
result = pwnd->WindowProc(uMsg, wParam, lParam);
else
result = DefWindowProc(hWnd, uMsg, wParam, lParam);
if (uMsg == WM_NCDESTROY)
{
pwnd->m_hWnd = NULL;
SetWindowLongPtr(hWnd, 0, 0);
}
return result;
}
LRESULT Window::WindowProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
return OnCreate(reinterpret_cast<CREATESTRUCT*>(lParam)) ? 0 : -1;
case WM_SIZE:
OnSize();
return 0;
case WM_ACTIVATE:
OnActivate(LOWORD(wParam));
return 0;
case WM_CLOSE:
OnClose();
return 0;
case WM_NOTIFY:
return OnNotify(reinterpret_cast<NMHDR*>(lParam));
case WM_ENSURELOADED:
EnsureCrestLoaded(wParam);
return 0;
default:
return DefWindowProc(m_hWnd, uMsg, wParam, lParam);
}
}
bool Window::OnCreate(CREATESTRUCT* cs)
{
m_hCtlLv = CreateWindowEx(
WS_EX_NOPARENTNOTIFY,
WC_LISTVIEW,
NULL,
WS_CHILD|WS_VISIBLE|WS_VSCROLL|LVS_OWNERDATA|LVS_AUTOARRANGE|LVS_SHAREIMAGELISTS,
0, 0, 0, 0,
m_hWnd,
NULL,
THIS_HINSTANCE,
NULL);
if (!m_hCtlLv)
return false;
DWORD exStyle = LVS_EX_DOUBLEBUFFER|LVS_EX_HIDELABELS;
SendMessage(m_hCtlLv, LVM_SETEXTENDEDLISTVIEWSTYLE, exStyle, exStyle);
ImageList_CoCreateInstance(CLSID_ImageList, NULL, IID_IImageList2, reinterpret_cast<void**>(&m_iml));
int dpiX, dpiY;
HDC hDC = GetDC(m_hWnd);
dpiX = GetDeviceCaps(hDC, LOGPIXELSX);
dpiY = GetDeviceCaps(hDC, LOGPIXELSX);
ReleaseDC(m_hWnd, hDC);
int imgWidth = MulDiv(128, dpiX, 96),
imgHeight = MulDiv(128, dpiY, 96);
m_iml->Initialize(imgWidth, imgWidth, ILC_COLOR32, 0, 0);
SendMessage(m_hCtlLv, LVM_SETIMAGELIST, LVSIL_NORMAL, reinterpret_cast<LPARAM>(m_iml.p));
int itemWidth = MulDiv(imgWidth, 9, 8),
itemHeight = MulDiv(imgHeight, 9, 8);
SendMessage(m_hCtlLv, LVM_SETICONSPACING, 0, MAKELPARAM(itemWidth, itemHeight));
SendMessage(m_hCtlLv, LVM_SETBKCOLOR, 0, BGCOLOR);
SendMessage(m_hCtlLv, LVM_SETTEXTBKCOLOR, 0, BGCOLOR);
SendMessage(m_hCtlLv, LVM_SETTEXTCOLOR, 0, FGCOLOR);
SetWindowTheme(m_hCtlLv, L"Explorer", NULL);
SetFocus(m_hCtlLv);
return true;
}
void Window::OnSize()
{
RECT r;
GetClientRect(m_hWnd, &r);
HDWP hDwp = BeginDeferWindowPos(1);
hDwp = DeferWindowPos(hDwp, m_hCtlLv, NULL, 0, 0, r.right, r.bottom, SWP_NOZORDER|SWP_NOACTIVATE);
EndDeferWindowPos(hDwp);
}
void Window::OnActivate(UINT state)
{
if (state && m_hCtlLv)
SetFocus(m_hCtlLv);
}
void Window::OnClose()
{
PostQuitMessage(0);
}
LRESULT Window::OnNotify(NMHDR* nmh)
{
if (nmh->hwndFrom == m_hCtlLv)
{
switch (nmh->code)
{
case NM_DBLCLK:
OnLvDblClk(reinterpret_cast<NMITEMACTIVATE*>(nmh));
return 0;
case LVN_GETEMPTYMARKUP:
OnLvGetEmptyMarkup(reinterpret_cast<NMLVEMPTYMARKUP*>(nmh));
return 1;
case LVN_GETDISPINFO:
OnLvGetDispInfo(reinterpret_cast<NMLVDISPINFO*>(nmh));
return 0;
case LVN_ODCACHEHINT:
OnLvCacheHint(reinterpret_cast<NMLVCACHEHINT*>(nmh));
return 0;
case LVN_BEGINDRAG:
OnLvBeginDrag(reinterpret_cast<NMLISTVIEW*>(nmh));
return 0;
}
}
return 0;
}
void Window::OnLvDblClk(NMITEMACTIVATE* nmia)
{
HRESULT hr = EnsureCrestLoaded(nmia->iItem);
if (FAILED(hr))
return;
// Create and setup Save As dialog
CComPtr<IFileSaveDialog> dlg;
hr = CoCreateInstance(
CLSID_FileSaveDialog,
NULL, CLSCTX_INPROC,
IID_IFileDialog,
reinterpret_cast<void**>(&dlg));
if (FAILED(hr))
return;
static const COMDLG_FILTERSPEC filter[] = { L"PNG image", L"*.png" };
hr = dlg->SetFileTypes(_countof(filter), filter);
if (FAILED(hr))
return;
hr = dlg->SetFileName(m_crests[nmia->iItem]->GetName().c_str());
if (FAILED(hr))
return;
hr = dlg->SetDefaultExtension(L"png");
if (FAILED(hr))
return;
// Show dialog
hr = dlg->Show(m_hWnd);
if (FAILED(hr))
return;
// Get the selected item
CComPtr<IShellItem> psi;
hr = dlg->GetResult(&psi);
if (FAILED(hr))
return;
// Create a write stream for the selected item
CComPtr<IBindCtx> ctx;
hr = CreateBindCtx(0, &ctx);
if (FAILED(hr))
return;
BIND_OPTS bo =
{
sizeof(BIND_OPTS), 0,
STGM_WRITE|STGM_SHARE_EXCLUSIVE|STGM_CREATE
};
hr = ctx->SetBindOptions(&bo);
if (FAILED(hr))
return;
CComPtr<IStream> strm;
hr = psi->BindToHandler(
ctx, BHID_Stream,
IID_IStream, reinterpret_cast<void**>(&strm));
if (FAILED(hr))
return;
// Save crest in stream
hr = m_crests[nmia->iItem]->Save(strm);
if (FAILED(hr))
{
TCHAR buffer[64];
_sntprintf_s(buffer, _countof(buffer), _TRUNCATE, _T("Saving crest failed.\r\n\r\nError 0x%08X."), hr);
MessageBox(m_hWnd, buffer, NULL, MB_ICONSTOP);
}
}
void Window::OnLvGetEmptyMarkup(NMLVEMPTYMARKUP* nmem)
{
nmem->dwFlags = EMF_CENTERED;
wcscpy_s(nmem->szMarkup, L_MAX_URL_LENGTH, L"No crests were found or were able to be loaded, sorry.");
}
void Window::OnLvGetDispInfo(NMLVDISPINFO* nmdi)
{
LVITEM* lvi = &nmdi->item;
if (lvi->iSubItem)
return;
if (lvi->mask & LVIF_IMAGE)
{
EnsureCrestLoaded(lvi->iItem);
lvi->iImage = lvi->iItem;
}
}
void Window::OnLvCacheHint(NMLVCACHEHINT* nmch)
{
for (int i = nmch->iFrom; i <= nmch->iTo; ++i)
EnsureCrestLoaded(i);
}
void Window::OnLvBeginDrag(NMLISTVIEW* nmlv)
{
int count = SendMessage(m_hCtlLv, LVM_GETSELECTEDCOUNT, 0, 0);
if (!count)
return;
vector<CrestPtr> selected;
map<Crest*, int> idxmap;
selected.reserve(count);
LVITEMINDEX lvii = { -1, 0 };
while (SendMessage(m_hCtlLv, LVM_GETNEXTITEMINDEX, reinterpret_cast<WPARAM>(&lvii), LVNI_SELECTED))
{
selected.push_back(m_crests[lvii.iItem]);
idxmap[m_crests[lvii.iItem].get()] = lvii.iItem;
}
CComPtr<IDropSource> dropSrc(new DropSource());
CComPtr<IDataObject> dataObj(new DataObject(m_hWnd, move(selected), move(idxmap)));
#if 0 // Doesn't work because our IDataObject does not implement SetData
{
CComPtr<IDragSourceHelper> dragHlp;
HRESULT hr = CoCreateInstance(
CLSID_DragDropHelper,
NULL, CLSCTX_ALL,
IID_IDragSourceHelper,
reinterpret_cast<void**>(&dragHlp));
if (SUCCEEDED(hr))
dragHlp->InitializeFromWindow(m_hCtlLv, &nmlv->ptAction, dataObj);
}
#endif
DWORD effect;
DoDragDrop(dataObj, dropSrc, DROPEFFECT_COPY, &effect);
}
HRESULT Window::EnsureCrestLoaded(int i)
{
if (i >= m_crests.size())
return E_INVALIDARG;
auto& crest = m_crests[i];
if (!crest->CanLoad())
return S_FALSE;
HCURSOR hCsrOld = SetCursor(
LoadCursor(NULL, MAKEINTRESOURCE(IDC_APPSTARTING)));
#ifdef _DEBUG
TCHAR buffer[64];
_sntprintf_s(buffer, _countof(buffer), _TRUNCATE, _T("Loading Crest #%d.\r\n"), i);
OutputDebugString(buffer);
#endif
HRESULT hr = crest->Load();
if (SUCCEEDED(hr))
crest->GetImage(m_iml, i);
crest->Disconnect();
SetCursor(hCsrOld);
return hr;
}
STDMETHODIMP Window::DropSource::QueryContinueDrag(BOOL fEscapePressed, DWORD grfKeyState)
{
if (fEscapePressed)
return DRAGDROP_S_CANCEL;
if (!(grfKeyState & (MK_LBUTTON|MK_RBUTTON)))
return DRAGDROP_S_DROP;
return S_OK;
}
STDMETHODIMP Window::DropSource::GiveFeedback(DWORD dwEffect)
{
return DRAGDROP_S_USEDEFAULTCURSORS;
}
Window::DataObject::DataObject(HWND hWnd, vector<CrestPtr>&& crests, map<Crest*, int>&& idxmap)
:m_hWnd(hWnd),
m_crests(crests),
m_idxmap(idxmap)
{
memset(&m_fe, 0, sizeof(m_fe));
m_fe[0].cfFormat = RegisterClipboardFormat(CFSTR_FILEDESCRIPTOR);
m_fe[0].dwAspect = DVASPECT_CONTENT;
m_fe[0].lindex = -1;
m_fe[0].tymed = TYMED_HGLOBAL;
m_fe[1].cfFormat = RegisterClipboardFormat(CFSTR_FILECONTENTS);
m_fe[1].dwAspect = DVASPECT_CONTENT;
m_fe[1].tymed = TYMED_ISTREAM;
}
int Window::DataObject::GetDataIndex(const FORMATETC* fe) const
{
for (int i = 0; i < _countof(m_fe); ++i)
{
const FORMATETC* dfe = m_fe + i;
if (fe->cfFormat == dfe->cfFormat &&
fe->tymed & dfe->tymed &&
fe->dwAspect == dfe->dwAspect &&
(dfe->lindex == -1 ?
(fe->lindex == -1) :
(fe->lindex >= 0 && fe->lindex < m_crests.size())))
return i;
}
return -1;
}
STDMETHODIMP Window::DataObject::GetData(FORMATETC* pformatetcIn, STGMEDIUM* pmedium)
{
memset(pmedium, 0, sizeof(STGMEDIUM));
int index = GetDataIndex(pformatetcIn);
if (index == -1)
return DV_E_FORMATETC;
switch (index)
{
case 0:
{
pmedium->tymed = TYMED_HGLOBAL;
pmedium->hGlobal = GlobalAlloc(
GMEM_ZEROINIT,
FIELD_OFFSET(FILEGROUPDESCRIPTOR, fgd) +
(m_crests.size() * sizeof(FILEDESCRIPTOR)));
auto gdesc = reinterpret_cast<FILEGROUPDESCRIPTOR*>(GlobalLock(pmedium->hGlobal));
gdesc->cItems = m_crests.size();
for (size_t i = 0; i < m_crests.size(); ++i)
{
auto fdesc = gdesc->fgd + i;
auto& crest = m_crests[i];
fdesc->dwFlags = FD_PROGRESSUI;
#ifdef UNICODE
fdesc->dwFlags |= FD_UNICODE;
#endif
_sntprintf_s(fdesc->cFileName,
MAX_PATH, _TRUNCATE,
_T("%s.png"),
crest->GetName().c_str());
}
GlobalUnlock(pmedium->hGlobal);
return S_OK;
}
case 1:
{
if (pformatetcIn->lindex < 0 || pformatetcIn->lindex >= m_crests.size())
return E_INVALIDARG;
CComPtr<IStream> pstm(SHCreateMemStream(NULL, 0));
if (!pstm)
return E_OUTOFMEMORY;
auto& crest = m_crests[pformatetcIn->lindex];
auto it = m_idxmap.find(crest.get());
if (it != m_idxmap.end())
SendMessage(m_hWnd, WM_ENSURELOADED, it->second, 0);
HRESULT hr = crest->Save(pstm);
if (FAILED(hr))
return hr;
pmedium->tymed = TYMED_ISTREAM;
pmedium->pstm = pstm.Detach();
return S_OK;
}
default:
return E_FAIL;
}
}
STDMETHODIMP Window::DataObject::GetDataHere(FORMATETC* pformatetc, STGMEDIUM* pmedium)
{
return E_NOTIMPL;
}
STDMETHODIMP Window::DataObject::QueryGetData(FORMATETC* pformatetc)
{
return GetDataIndex(pformatetc) == -1 ? S_FALSE : S_OK;
}
STDMETHODIMP Window::DataObject::GetCanonicalFormatEtc(FORMATETC* pformatectIn, FORMATETC* pformatetcOut)
{
*pformatetcOut = *pformatectIn;
pformatetcOut->ptd = NULL;
return DATA_S_SAMEFORMATETC;
}
STDMETHODIMP Window::DataObject::SetData(FORMATETC* pformatetc, STGMEDIUM* pmedium, BOOL fRelease)
{
return E_NOTIMPL;
}
STDMETHODIMP Window::DataObject::EnumFormatEtc(DWORD dwDirection, IEnumFORMATETC** ppenumFormatEtc)
{
if (dwDirection == DATADIR_GET)
return SHCreateStdEnumFmtEtc(_countof(m_fe), m_fe, ppenumFormatEtc);
else
{
*ppenumFormatEtc = NULL;
return E_NOTIMPL;
}
}
STDMETHODIMP Window::DataObject::DAdvise(FORMATETC* pformatetc, DWORD advf, IAdviseSink* pAdvSink, DWORD* pdwConnection)
{
*pdwConnection = 0;
return OLE_E_ADVISENOTSUPPORTED;
}
STDMETHODIMP Window::DataObject::DUnadvise(DWORD dwConnection)
{
return OLE_E_ADVISENOTSUPPORTED;
}
STDMETHODIMP Window::DataObject::EnumDAdvise(IEnumSTATDATA** ppenumAdvise)
{
*ppenumAdvise = NULL;
return OLE_E_ADVISENOTSUPPORTED;
}