-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBetterFloatingFormList.cpp
More file actions
500 lines (424 loc) · 11.8 KB
/
BetterFloatingFormList.cpp
File metadata and controls
500 lines (424 loc) · 11.8 KB
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
#include "BetterFloatingFormList.h"
#include <commctrl.h>
#include "GameObjects.h"
#include "GECKUtility.h"
#include "GameAPI.h"
#include "Utilities.h"
#include <iostream>
#include <fstream>
#include <sstream>
namespace BetterFloatingFormList
{
bool IsFormInListView(HWND hListView, TESForm* form)
{
LVFINDINFO lvFindInfo;
lvFindInfo.flags = LVFI_PARAM;
lvFindInfo.lParam = (LPARAM)form;
int index = ListView_FindItem(hListView, -1, &lvFindInfo);
return index != -1;
}
LRESULT CALLBACK SubclassedListViewProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
switch (uMsg)
{
case WM_KEYDOWN:
if (wParam == VK_DELETE)
{
tList<TESForm> deletedForms;
deletedForms.Init();
int iSelected = -1;
SetDeferListUpdate(hWnd, true);
while ((iSelected = ListView_GetNextItem(hWnd, iSelected, LVNI_SELECTED)) != -1)
{
if (auto form = GetNthListForm(hWnd, iSelected))
{
deletedForms.Insert(form);
}
ListView_DeleteItem(hWnd, iSelected);
iSelected--; // because the indices shift after deletion, decrement iSelected to ensure no item is skipped
}
SendMessageA(GetParent(hWnd), BFL_DELETED_ITEMS, (WPARAM)&deletedForms, (LPARAM)hWnd);
SetDeferListUpdate(hWnd, false);
deletedForms.RemoveAll();
}
else if (wParam == 'A' && GetAsyncKeyState(VK_CONTROL) < 0)
{
SelectAllItemsInListView(hWnd);
}
break;
}
return DefSubclassProc(hWnd, uMsg, wParam, lParam);
}
void SubclassListView(HWND hListView)
{
SetWindowSubclass(hListView, SubclassedListViewProc, 0, 0);
}
WNDPROC originalWindowCallback;
LRESULT CALLBACK BaseWindowCallback(HWND Hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch (Message)
{
case WM_INITDIALOG:
{
auto listView = GetDlgItem(Hwnd, 1018);
SubclassListView(listView);
ListView_SetExtendedListViewStyleEx(listView, LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);
LONG_PTR style = GetWindowLongPtr(listView, GWL_STYLE);
SetWindowLongPtr(listView, GWL_STYLE, style & ~LVS_EDITLABELS);
DragAcceptFiles(Hwnd, TRUE);
// allow the window to be closed from the 'Open Windows' menu
auto openWindowsList = OpenWindows::GetWindowList();
openWindowsList->Append((HWND*)Hwnd);
break;
}
case BSMsg_AcceptsDropType:
{
SetWindowLong(Hwnd, DWL_MSGRESULT, wParam != kFormType_TESObjectREFR);
return TRUE;
}
case BSMsg_HandleDrop:
{
auto selected = RenderWindow::SelectedData::GetSelected();
auto listView = GetDlgItem(Hwnd, 1018);
if (auto selectedFormIter = selected->selectedForms)
{
tList<TESForm> addedForms;
addedForms.Init();
int numItemsAdded = 0;
do
{
if (auto form = selectedFormIter->ref)
{
if (!IsFormInListView(listView, form))
{
addedForms.Insert(form);
++numItemsAdded;
}
}
} while (selectedFormIter = selectedFormIter->next);
if (numItemsAdded)
{
SetDeferListUpdate(listView, true);
AddFormsToListView(listView, &addedForms);
SendMessageA(Hwnd, BFL_ADDED_ITEMS, (WPARAM)&addedForms, (LPARAM)listView);
SetDeferListUpdate(listView, false);
addedForms.RemoveAll();
}
}
break;
}
}
return CallWindowProc(originalWindowCallback, Hwnd, Message, wParam, lParam);
}
bool ShowOpenFileDialog(HWND hWnd, char* dst, size_t dstLen, bool bSave = false) {
OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hWnd;
ofn.lpstrFile = dst;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = dstLen;
ofn.lpstrFilter = "All files (*.*)\0*.*\0Text files (*.txt)\0*.TXT\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = "Data\\nvse\\plugins\\GeckExtender";
ofn.Flags = OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | (bSave ? OFN_OVERWRITEPROMPT : OFN_FILEMUSTEXIST);
return bSave ? GetSaveFileName(&ofn) : GetOpenFileName(&ofn);
}
bool ShowSaveFileDialog(HWND hWnd, char* dst, size_t dstLen)
{
return ShowOpenFileDialog(hWnd, dst, dstLen, true);
}
void SetWindowTitleToFileName(HWND listView, const char* path)
{
const char* fileName = strrchr(path, '\\');
if (fileName)
{
fileName++; // skip the backslash
}
else
{
fileName = path; // no backslash found, the path is the file name
}
char fileNameNoExt[MAX_PATH];
strcpy(fileNameNoExt, fileName);
char* lastDot = strrchr(fileNameNoExt, '.');
if (lastDot)
{
*lastDot = '\0';
}
SendMessageA(GetParent(listView), WM_SETTEXT, 0, (LPARAM)fileNameNoExt);
}
void LoadFormListFromFile(HWND listView, const char* path)
{
std::ifstream file(path);
if (!file.is_open())
{
return;
}
tList<TESForm> forms;
forms.Init();
std::string line;
while (getline(file, line))
{
std::stringstream ss(line);
std::string formEditorId;
while (getline(ss, formEditorId, ','))
{
if (auto form = LookupFormByName(formEditorId.c_str()))
{
forms.Append(form);
}
}
}
file.close();
if (!forms.IsEmpty())
{
SetDeferListUpdate(listView, true);
SendMessageA(listView, LVM_DELETEALLITEMS, 0, 0);
AddFormsToListView(listView, &forms);
SetDeferListUpdate(listView, false);
}
forms.RemoveAll();
SetWindowTitleToFileName(listView, path);
}
void SaveFormListToFile(HWND listView, const char* path)
{
std::stringstream concatenatedIDs;
bool isFirstItem = true;
int iIndex = -1;
while ((iIndex = ListView_GetNextItem(listView, iIndex, LVNI_ALL)) != -1)
{
if (auto form = GetNthListForm(listView, iIndex))
{
if (!isFirstItem)
{
concatenatedIDs << ',';
}
isFirstItem = false;
concatenatedIDs << (form->GetEditorID());
}
}
std::ofstream outFile(path);
if (!outFile)
{
Console_Print("Failed to open file for writing: %s", path);
return;
}
outFile << concatenatedIDs.str();
outFile.close();
if (outFile.fail())
{
Console_Print("Error occurred when writing to the file: %s", path);
return;
}
SetWindowTitleToFileName(listView, path);
}
void LoadFormList(HWND listView)
{
char buf[MAX_PATH]; *buf = '\0';
if (ShowOpenFileDialog(listView, buf, sizeof(buf)))
{
LoadFormListFromFile(listView, buf);
}
}
void SaveFormList(HWND listView)
{
char buf[MAX_PATH]; *buf = '\0';
if (ShowSaveFileDialog(listView, buf, sizeof(buf)))
{
SaveFormListToFile(listView, buf);
}
}
constexpr int IDM_LOAD = 0x1001;
constexpr int IDM_SAVE = 0x1002;
constexpr int IDC_TOOLBAR = 0x1003;
HWND CreateToolbar(HWND hParent)
{
HWND hWndToolbar = CreateWindowEx(0, TOOLBARCLASSNAME, NULL,
WS_CHILD | TBSTYLE_WRAPABLE, 5, 0, 0, 0,
hParent, (HMENU)IDC_TOOLBAR, GetModuleHandle(NULL), NULL);
SendMessage(hWndToolbar, TB_SETBITMAPSIZE, 0, MAKELONG(16, 4));
SendMessage(hWndToolbar, TB_SETBUTTONSIZE, 0, MAKELONG(24, 8));
SendMessage(hWndToolbar, TB_SETPADDING, 0, MAKELPARAM(4, 2));
TBBUTTON tbButtons[] =
{
{ MAKELONG(0, 0), IDM_LOAD, TBSTATE_ENABLED, BTNS_AUTOSIZE, {0}, 0, (INT_PTR)"Load" },
{ MAKELONG(1, 0), IDM_SAVE, TBSTATE_ENABLED, BTNS_AUTOSIZE, {0}, 0, (INT_PTR)"Save" },
};
SendMessage(hWndToolbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
SendMessage(hWndToolbar, TB_ADDBUTTONS, (WPARAM)sizeof(tbButtons) / sizeof(TBBUTTON), (LPARAM)&tbButtons);
SendMessage(hWndToolbar, TB_AUTOSIZE, 0, 0);
ShowWindow(hWndToolbar, SW_SHOW);
return hWndToolbar;
}
void UpdateToolbarAndListviewPositions(HWND Hwnd)
{
RECT rcClient;
GetClientRect(Hwnd, &rcClient);
// Resize the toolbar and let it auto-adjust its height
auto hWndToolbar = GetDlgItem(Hwnd, IDC_TOOLBAR);
SendMessage(hWndToolbar, TB_AUTOSIZE, 0, 0);
RECT rcToolbar;
GetWindowRect(hWndToolbar, &rcToolbar);
int toolbarHeight = rcToolbar.bottom - rcToolbar.top;
// Calculate the new height and position for the list view
int listViewHeight = rcClient.bottom - toolbarHeight;
auto listView = GetDlgItem(Hwnd, 1018);
SetWindowPos(listView, NULL, 0, toolbarHeight, rcClient.right, listViewHeight, SWP_NOZORDER);
}
LRESULT CALLBACK WindowCallback(HWND Hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch (Message)
{
case WM_INITDIALOG:
{
CreateToolbar(Hwnd);
UpdateToolbarAndListviewPositions(Hwnd);
break;
}
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case IDM_LOAD:
LoadFormList(GetDlgItem(Hwnd, 1018));
break;
case IDM_SAVE:
SaveFormList(GetDlgItem(Hwnd, 1018));
break;
}
break;
}
case WM_SIZE:
{
UpdateToolbarAndListviewPositions(Hwnd);
break;
}
}
return BaseWindowCallback(Hwnd, Message, wParam, lParam);
}
void __fastcall OnSelectForm(TESForm* form, HWND parent)
{
if (form)
{
OpenForm(form, parent);
}
}
__HOOK OnSelectFormHook()
{
_asm
{
pop ecx
mov edx, esi // hDlg
mov ecx, eax // form
push 0x483B3D
jmp OnSelectForm
}
}
void __cdecl InitFormNameColumn(HWND hWnd, WPARAM index, char* pszText, int width, int format)
{
width = 200;
CdeclCall(0x419F50, hWnd, index, pszText, width, format);
}
enum
{
COLUMN_TYPE = 3
};
void __cdecl InitFormIdNameAndFormTypeColumns(HWND hWnd, WPARAM index, char* pszText, int width, int format)
{
width = 80;
CdeclCall(0x419F50, hWnd, index, pszText, width, format);
CdeclCall(0x419F50, hWnd, 2, "Name", width, format);
CdeclCall(0x419F50, hWnd, COLUMN_TYPE, "Type", width, format);
}
const char* FormToTypeStr(TESForm* form)
{
auto formTypeNames = (const char**)0xE94404;
return formTypeNames[form->typeID * 3];
}
int CompareTypes(TESForm* a1, TESForm* a2)
{
return _stricmp(FormToTypeStr(a1), FormToTypeStr(a2));
}
int __stdcall CompareForms(TESForm* a1, TESForm* a2, int a3)
{
int column = abs(a3) - 1;
if (column == COLUMN_TYPE)
{
int result = CompareTypes(a1, a2);
return (a3 < 0) ? -result : result;
}
else
{
return StdCall<int>(0x47B1E0, a1, a2, a3);
}
}
void __fastcall SetupRow(TESForm* form, void* edx, LPNMLVDISPINFOA displayInfo)
{
if (displayInfo->item.mask & 1)
{
if (displayInfo->item.iSubItem == COLUMN_TYPE)
{
stbsp_sprintf(displayInfo->item.pszText, FormToTypeStr(form));
}
}
return form->SetupListViewDisplayInfo(displayInfo);
}
const char* GetSelectedTreeViewItemFullPath()
{
auto hSelection = TreeView_GetSelection(ObjectsView::GetTreeView());
TVITEM tvItem;
tvItem.mask = TVIF_HANDLE;
tvItem.hItem = hSelection;
if (SendMessageA(ObjectsView::GetTreeView(), TVM_GETITEM, 0, (LPARAM)&tvItem))
{
if (auto item = (ObjectsView::TreeViewItem*)tvItem.lParam)
{
return item->fullPath;
}
}
return nullptr;
}
bool __cdecl FormPathMatches(TESForm* form, const char* filterData)
{
String str;
str.m_bufLen = 0;
str.m_data = 0;
str.m_dataLen = 0;
form->GetFormPath(&str);
auto result = !strnicmp(str.CStr(), filterData, strlen(filterData));
str.Set(nullptr);
return result;
}
void __cdecl OnInitListViewForms(HWND listView, tList<TESForm>* list, bool(__cdecl* filterFn)(TESForm*, const char*), const char* filterData)
{
if (auto fullPath = GetSelectedTreeViewItemFullPath())
{
if (*fullPath)
{
filterFn = FormPathMatches;
filterData = fullPath;
}
}
CdeclCall(0x47E410, listView, list, filterFn, filterData);
}
void Init()
{
// don't close the list view when clicking on an item
WriteRelJump(0x483B32, UInt32(OnSelectFormHook));
originalWindowCallback = *(WNDPROC*)0x44BB19;
SafeWrite32(0x44BB19, UInt32(WindowCallback));
// adjust default column sizes
WriteRelCall(0x48387D, UInt32(InitFormNameColumn));
WriteRelCall(0x48389C, UInt32(InitFormIdNameAndFormTypeColumns));
// add support for sorting the 'Type' column
SafeWrite32(0x48368A, UInt32(CompareForms));
// support displaying the type column
SafeWrite8(0x4836AD, 0xB8);
SafeWrite32(0x4836AD + 1, UInt32(SetupRow));
SafeWrite8(0x4836AD + 1 + 4, 0x90);
WriteRelCall(0x4838C1, UInt32(OnInitListViewForms));
}
}