forked from fdwr/TextLayoutSampler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.ixx
More file actions
3341 lines (2773 loc) · 124 KB
/
Copy pathMainWindow.ixx
File metadata and controls
3341 lines (2773 loc) · 124 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
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
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
todo:::
-Custom font fallback
-Custom font collection
-EDIT/RichEdit
-Show red attribute on parse error
?Character glyph map
?App translucency
+Rendering mode
+GDI AddFontMemResource
+Add font open dialog for font file
+Get all characters
+Add font selection dialog
+Hit test canvas
+Pan scroll canvas
+Font fallback enable/disable
+Store settings
+Load settings
+Transform drawable objects
+Context menu on drawable objects right-click
+Drawable object absolute placement
+Draw labels
+Reflow drawable objects
+GDI+ DrawString/MeasureString
+GDI+ DrawDriverString
+Show red box on drawing error
+Draw object background colors and object together
+Fix user32 DrawText for vertical
+Save selected font file
*/
//----------------------------------------------------------------------------
// History: 2015-06-19 Dwayne Robinson - Created
//----------------------------------------------------------------------------
#include "precomp.h"
#include "resource.h"
#include "Application.macros.h"
#include "WindowUtility.macros.h"
#if USE_CPP_MODULES
import Common.ArrayRef;
import Common.String;
import Common.AutoResource;
import Common.AutoResource.Windows;
import Common.ListSubstringPrioritizer;
import Common.FastVector;
import MessageBoxShaded;
import FileHelpers;
import DrawingCanvasControl;
import DrawableObject;
import Application;
import DrawableObjectAndValues;
import TextTreeParser; // for DrawableObjectAndValues
#else
#include "Common.ArrayRef.h"
#include "Common.String.h"
#include "Common.AutoResource.h"
#include "Common.AutoResource.Windows.h"
#include "Common.FastVector.h"
#include "Common.ListSubstringPrioritizer.h"
#include "MessageBoxShaded.h"
#include "FileHelpers.h"
#include "DrawingCanvasControl.h"
#include "DrawableObject.h"
#include "Application.h"
#include "DrawableObjectAndValues.h"
#include "TextTreeParser.h"
#endif
#pragma comment(lib, "ComCtl32.lib")
MODULE(MainWindow)
EXPORT_BEGIN
#include "MainWindow.h"
EXPORT_END
////////////////////////////////////////
#define DEBUG_SHOW_WINDOWS_MESSAGES 0
////////////////////////////////////////
// UI related
#pragma prefast(disable:__WARNING_HARD_CODED_STRING_TO_UI_FN, "It's an internal test program.")
HACCEL MainWindow::g_accelTable;
int const g_smallMouseWheelStep = 20;
char16_t const* g_openSaveFiltersList = u"All supported text layout sampler files\0" u"*.TextLayoutSamplerSettings;*.txt\0"
u"Layout Sampler Settings (*.TextLayoutSamplerSettings)\0" u"*.TextLayoutSamplerSettings\0"
u"Text files (*.txt)\0" u"*.txt\0"
u"All files (*)\0" u"*\0";
////////////////////////////////////////
int APIENTRY wWinMain(
__in HINSTANCE hInstance,
__in_opt HINSTANCE hPrevInstance,
__in LPWSTR commandLine,
__in int nCmdShow
)
{
Application::g_hModule = hInstance;
_wsetlocale(LC_ALL, L""); // Unicode, not ANSI!
bool wantBlankCanvas = false;
////////////////////
// Read command line parameters.
std::u16string trimmedCommandLine(ToChar16(commandLine));
TrimSpaces(IN OUT trimmedCommandLine);
if (!trimmedCommandLine.empty())
{
if (_wcsicmp(ToWChar(trimmedCommandLine.c_str()), L"/?" ) == 0
|| _wcsicmp(ToWChar(trimmedCommandLine.c_str()), L"/help" ) == 0
|| _wcsicmp(ToWChar(trimmedCommandLine.c_str()), L"-h" ) == 0
|| _wcsicmp(ToWChar(trimmedCommandLine.c_str()), L"--help") == 0
)
{
MessageBox(nullptr, L"TextLayoutSampler.exe [SomeFile.TextLayoutSamplerSettings].", APPLICATION_TITLE, MB_OK);
return (int)0;
}
else if (_wcsicmp(ToWChar(trimmedCommandLine.c_str()), L"/blank") == 0)
{
wantBlankCanvas = true;
trimmedCommandLine.clear();
}
else if (trimmedCommandLine[0] == '/')
{
Application::Fail(trimmedCommandLine.c_str(), u"Unknown command line option.\r\n\r\n\"%s\"", 0);
}
UnquoteString(IN OUT trimmedCommandLine);
// Else just pass parse command line as a settings file name.
}
////////////////////
// Create user interface elements.
Application::g_mainHwnd = MainWindow::Create();
if (Application::g_mainHwnd == nullptr)
{
Application::Fail(u"Could not create main window.", u"%s = %08X", HRESULT_FROM_WIN32(GetLastError()));
}
ShowWindow(Application::g_mainHwnd, SW_SHOWNORMAL);
SendMessage(Application::g_mainHwnd, WM_CHANGEUISTATE, UIS_CLEAR | UISF_HIDEACCEL | UISF_HIDEFOCUS, (LPARAM)nullptr); // Always shows the focus rectangle.
MainWindow& mainWindow = *MainWindow::GetClass(Application::g_mainHwnd);
if (!trimmedCommandLine.empty())
{
mainWindow.LoadDrawableObjectsSettings(trimmedCommandLine.data());
}
else if (!wantBlankCanvas)
{
mainWindow.InitializeDefaultDrawableObjects();
}
while (GetMessage(&Application::g_msg, nullptr, 0, 0) > 0)
{
Application::Dispatch();
}
return static_cast<int>(Application::g_msg.wParam);
}
HWND MainWindow::Create()
{
RegisterCustomClasses();
g_accelTable = LoadAccelerators(Application::g_hModule, MAKEINTRESOURCE(IdaMainWindow));
return CreateDialog(Application::g_hModule, MAKEINTRESOURCE(IddMainWindow), nullptr, &MainWindow::StaticDialogProc);
}
void MainWindow::RegisterCustomClasses()
{
// Ensure that the common control DLL is loaded.
// (probably not needed, but do it anyway)
INITCOMMONCONTROLSEX icex;
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_LISTVIEW_CLASSES;
InitCommonControlsEx(&icex);
DrawingCanvasControl::RegisterWindowClass(Application::g_hModule);
}
HWND MainWindow::GetHwnd() const
{
return hwnd_;
}
MainWindow* MainWindow::GetClass(HWND hwnd)
{
return reinterpret_cast<MainWindow*>(::GetWindowLongPtr(hwnd, DWLP_USER));
}
INT_PTR CALLBACK MainWindow::StaticDialogProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
MainWindow* window = GetClassFromDialog<MainWindow>(hwnd);
if (window == nullptr)
{
window = new(std::nothrow) MainWindow(hwnd);
if (window == nullptr)
{
return -1; // failed creation
}
SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)window);
}
DialogProcResult result = {FALSE, 0};
try
{
result = window->DialogProc(hwnd, message, wParam, lParam);
if (result.handled)
{
::SetWindowLongPtr(hwnd, DWLP_MSGRESULT, result.value);
}
}
catch (...)
{
}
return result.handled;
}
MainWindow::DialogProcResult CALLBACK MainWindow::DialogProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (isRecursing_)
return false;
#if DEBUG_SHOW_WINDOWS_MESSAGES
isRecursing_ = true;
switch (message)
{
case WM_PAINT:
case WM_GETICON:
case WM_GETOBJECT:
case WM_MOUSEMOVE:
case WM_SETCURSOR:
case CB_GETCOMBOBOXINFO: // Why are we getting this message!?
case WM_CHANGEUISTATE:
break;
default:
AppendLog(L"time=%d DLGMessage=%X, wParam=%08X, lParam=%08X\r\n", GetTickCount(), message, wParam, lParam);
break;
}
isRecursing_ = false;
#endif
switch (message)
{
case WM_INITDIALOG:
return !!InitializeMainDialog();
HANDLE_MSG(hwnd, WM_COMMAND, OnCommand);
HANDLE_MSG(hwnd, WM_NOTIFY, OnNotification);
case WM_SIZE:
Resize(IddMainWindow);
break;
case WM_KEYDOWN:
TranslateAccelerator(hwnd, g_accelTable, &Application::g_msg);
break;
case WM_ENTERIDLE:
case WM_TIMER:
if (wParam == IdcUpdateUi)
{
KillTimer(hwnd, wParam);
UpdateUi();
}
#if 0
else if (wParam == IdcUpdateUi+1)// hack for debugging animating objects.
{
RepaintDrawableObjects();
}
#endif
else
{
return false;
}
break;
case WM_MOVE:
break;
case WM_DESTROY:
break;
case WM_DROPFILES:
return OnDragAndDrop(hwnd, message, wParam, lParam);
default:
return false;
}
return true;
}
LRESULT CALLBACK AttributeValueEditProcedure(
HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam,
UINT_PTR subclassId,
DWORD_PTR data
)
{
// Subclass procedure for the attribute value edit, so that the edit
// behaves like a combo box.
MainWindow* window = reinterpret_cast<MainWindow*>(data);
switch (message)
{
case WM_KEYDOWN:
{
// Forward certain keys to the list if single line edit.
auto windowStyle = GetWindowStyle(hwnd);
if (windowStyle & ES_MULTILINE)
{
break;
}
switch (wParam)
{
case VK_RETURN: // Enter key pressed.
SetFocus(GetWindowFromId(window->GetHwnd(), IdcAttributesList));
return 0;
case VK_PRIOR:
case VK_NEXT:
case VK_UP:
case VK_DOWN:
return SendMessage(GetWindowFromId(window->GetHwnd(), IdcAttributeValuesList), message, wParam, lParam);
}
}
break;
case WM_NCDESTROY:
RemoveWindowSubclass(hwnd, &AttributeValueEditProcedure, 0);
break;
case WM_PRINTCLIENT:
case WM_PAINT:
{
// Draw the cue banner text if no text is typed.
auto textLength = GetWindowTextLength(hwnd);
if (textLength == 0)// && GetFocus() != hwnd)
{
// Get the needed DC with DCX_INTERSECTUPDATE before the EDIT
// control's WM_PAINT handler calls BeginPaint/EndPaint, which
// validates the update rect and would otherwise lead to drawing
// nothing later because the region is empty.
HDC hdc = (message == WM_PRINTCLIENT)
? reinterpret_cast<HDC>(wParam)
: GetDCEx(hwnd, nullptr, DCX_INTERSECTUPDATE | DCX_CACHE | DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS);
// Call the EDIT control so that the caret is properly handled,
// no litter left on the screen after tabbing away.
auto result = DefSubclassProc(hwnd, message, wParam, lParam);
// Get the font and margin so the cue banner text has a
// consistent appearance and placement with existing text.
HFONT font = GetWindowFont(hwnd);
RECT editRect;
Edit_GetRect(hwnd, OUT &editRect);
// Ideally we would call Edit_GetCueBannerText, but since that message
// returns nothing when ES_MULTILINE, use a window property instead.
auto* cueBannerText = reinterpret_cast<char16_t*>(GetProp(hwnd, L"CueBannerText"));
HFONT previousFont = SelectFont(hdc, font);
SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT));
SetBkMode(hdc, TRANSPARENT);
DrawText(hdc, ToWChar(cueBannerText), int(wcslen(ToWChar(cueBannerText))), &editRect, DT_TOP| DT_LEFT|DT_NOPREFIX|DT_NOCLIP);
SelectFont(hdc, previousFont);
ReleaseDC(hwnd, hdc);
// Return the EDIT's result (could just return zero here,but
// will relay whatever value came from the edit).
return result;
}
}
break;
}
return DefSubclassProc(hwnd, message, wParam, lParam);
}
LRESULT CALLBACK AttributesEditProcedure(
HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam,
UINT_PTR subclassId,
DWORD_PTR data
)
{
// Subclass procedure for the attribute value edit, so that the edit
// behaves like a combo box.
MainWindow* window = reinterpret_cast<MainWindow*>(data);
switch (message)
{
case WM_KEYDOWN:
{
switch (wParam)
{
case VK_PRIOR:
case VK_NEXT:
case VK_UP:
case VK_DOWN:
return SendMessage(GetWindowFromId(window->GetHwnd(), IdcAttributesList), message, wParam, lParam);
}
}
break;
case WM_NCDESTROY:
RemoveWindowSubclass(hwnd, &AttributesEditProcedure, 0);
break;
}
return DefSubclassProc(hwnd, message, wParam, lParam);
}
INT_PTR MainWindow::InitializeMainDialog()
{
////////////////////
// Re-enable drag and drop, even if running process as admin.
// Then set icon and title.
#ifndef WM_COPYGLOBALDATA
#define WM_COPYGLOBALDATA 0x0049
#endif
ChangeWindowMessageFilterEx(hwnd_, WM_DROPFILES, MSGFLT_ALLOW, nullptr);
ChangeWindowMessageFilterEx(hwnd_, WM_COPYDATA, MSGFLT_ALLOW, nullptr);
ChangeWindowMessageFilterEx(hwnd_, WM_COPYGLOBALDATA, MSGFLT_ALLOW, nullptr);
DefWindowProc(hwnd_, WM_SETICON, ICON_BIG, LPARAM(LoadIcon(Application::g_hModule, MAKEINTRESOURCE(1))));
SetWindowText(hwnd_, BUILD_TITLE_STRING);
Edit_LimitText(GetWindowFromId(hwnd_, IdcLog), 1048576);
// Subclass the values edit box for a few reasons.
// - Forward up/down arrow key presses to the listview under it.
// - Dynamically change between multiline and single-line behavior for return keypress.
// - Display the cue banner text because cue banners do not work correctly for ES_MULTILINE.
auto attributeValuesEdit = GetWindowFromId(hwnd_, IdcAttributeValuesEdit);
SetWindowSubclass(attributeValuesEdit, &AttributeValueEditProcedure, 0, reinterpret_cast<DWORD_PTR>(this));
SetWindowStyle(attributeValuesEdit, GetWindowStyle(attributeValuesEdit) & ~ES_MULTILINE);
// Not only do multiline edit controls fail to display the cue banner text,
// but they also ignore the Edit_SetCueBannerText call, meaning we can't
// just call GetCueBannerText in the subclassed function. So store it as
// a window property instead.
SetProp(attributeValuesEdit, L"CueBannerText", L"<no attributes selected>");
auto attributesEdit = GetWindowFromId(hwnd_, IdcAttributesFilterEdit);
Edit_SetCueBannerText(attributesEdit, L"<type attribute filter here>");
SetWindowSubclass(attributesEdit, &AttributesEditProcedure, 0, reinterpret_cast<DWORD_PTR>(this));
ChangeSettingsVisibility(settingsVisibility_);
InitializeDrawableObjectsListView();
InitializeAttributesListView();
InitializeAttributeValuesListView();
Button_SetCheck(GetWindowFromId(hwnd_, settingsVisibility_ + IdcSettingsFirst), true);
Resize(IddMainWindow);
// Indirectly called already by ChangeSettingsVisibility:
// UpdateDrawableObjectsListView();
// UpdateAttributesListView();
// UpdateAttributeValuesListView();
AppendLog(u"Mouse wheel scrolls, middle button drag pans, right click opens menu, left click selects object (ctrl to toggle). "
u"You can select multiple objects in the list to change an attribute across all.\r\n"
u"Double click cell to edit. Press delete key to delete selected object. Press delete in attribute list to clear value.\r\n"
);
uint32_t controlIdToSetFocusTo = 0;
static_assert(SettingsVisibilityTotal == 3, "");
switch (settingsVisibility_)
{
case SettingsVisibilityHidden: controlIdToSetFocusTo = IdcDrawingCanvas; break;
case SettingsVisibilityLight: controlIdToSetFocusTo = IdcEditText; break;
case SettingsVisibilityFull: controlIdToSetFocusTo = IdcDrawableObjectsList; break;
}
SetFocus(GetWindowFromId(hwnd_, controlIdToSetFocusTo));
#if 0 // hack for debugging animating objects.
SetTimer(hwnd_, IdcUpdateUi + 1, 10, nullptr);
#endif
return false; // do not permit user32 code to set focus
}
namespace
{
struct ListViewColumnInfo
{
int attributeType;
int width;
const char16_t* name;
};
void InitializeSysListView(
HWND hwnd,
__in_ecount(columnInfoCount) const ListViewColumnInfo* columnInfo,
size_t columnInfoCount
)
{
// Columns
LVCOLUMN lc;
lc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
for (size_t i = 0; i < columnInfoCount; ++i)
{
lc.iSubItem = columnInfo[i].attributeType;
lc.cx = columnInfo[i].width;
lc.pszText = (LPWSTR)columnInfo[i].name;
ListView_InsertColumn(hwnd, lc.iSubItem, &lc);
}
}
bool HandleSysListViewEmptyText(LPARAM lParam, _In_z_ char16_t const* text)
{
LV_DISPINFO& di = *(LV_DISPINFO*)lParam;
if (di.hdr.code != LVN_GETEMPTYTEXT)
return false;
if (di.item.mask & LVIF_TEXT)
{
StringCchCopyW(
di.item.pszText,
di.item.cchTextMax,
ToWChar(text)
);
return true;
}
return false;
}
}
void MainWindow::InitializeDrawableObjectsListView()
{
HWND listViewHwnd = GetWindowFromId(hwnd_, IdcDrawableObjectsList);
ListView_SetExtendedListViewStyle(listViewHwnd, LVS_EX_LABELTIP | LVS_EX_DOUBLEBUFFER | LVS_EX_HEADERDRAGDROP | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_HEADERINALLVIEWS);
// Columns
LVCOLUMN lc;
lc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
for (auto const& attribute : DrawableObject::attributeList)
{
// Determine column width from the type and semantic, giving wider
// columns to data arrays (including strings) and enumerations than
// simple numeric values.
lc.iSubItem = attribute.id;
lc.cx = (attribute.IsTypeArray() || attribute.id == 0) ? 120 : 80;
lc.pszText = const_cast<LPWSTR>(ToWChar(attribute.display));
ListView_InsertColumn(listViewHwnd, lc.iSubItem, &lc);
}
}
void MainWindow::UpdateDrawableObjectsListView()
{
HWND drawableObjectsListHwnd = GetWindowFromId(hwnd_, IdcDrawableObjectsList);
if (!IsWindowVisible(drawableObjectsListHwnd))
{
return; // Shortcut, since control not displayed anyway.
}
std::u16string truncatedString;
ListViewWriter lw(drawableObjectsListHwnd);
lw.DisableDrawing();
isRecursing_ = true;
size_t itemCount = lw.GetItemCount();
size_t const totalDrawableObjects = drawableObjects_.size();
if (itemCount > totalDrawableObjects)
{
lw.DeleteAllItems();
itemCount = 0;
}
lw.ReserveItemCount(int(totalDrawableObjects));
isRecursing_ = false;
for (auto const& drawableObject : drawableObjects_)
{
if (lw.iItem >= int(itemCount))
{
isRecursing_ = true; // Stop pointless LVN_ITEMCHANGED messages.
lw.InsertItem(u"");
isRecursing_ = false;
++itemCount;
}
isRecursing_ = true; // Stop pointless LVN_ITEMCHANGED messages.
ListView_SetItemState(lw.hwnd, lw.iItem, drawableObject.flags_ & drawableObject.FlagsSelected ? LVIS_SELECTED : 0, LVIS_SELECTED);
isRecursing_ = false;
for (uint32_t i = 0; i < DrawableObjectAttributeTotal; ++i)
{
auto* text = &drawableObject.values_[i].stringValue;
if (text->size() > 256)
{
truncatedString.assign(text->c_str(), 256);
text = &truncatedString;
}
isRecursing_ = true; // Stop pointless LVN_ITEMCHANGED messages.
lw.SetItemText(i, text->c_str());
isRecursing_ = false;
}
lw.AdvanceItem();
}
lw.EnableDrawing();
InvalidateRect(lw.hwnd, nullptr, false);
}
void MainWindow::InitializeAttributesListView()
{
const static ListViewColumnInfo columnInfo[] = {
{ 0, 120, u"Attribute" },
{ 1, 400, u"Value" },
};
auto listViewHwnd = GetWindowFromId(hwnd_, IdcAttributesList);
ListView_SetExtendedListViewStyle(listViewHwnd, LVS_EX_LABELTIP | LVS_EX_DOUBLEBUFFER | LVS_EX_HEADERDRAGDROP | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_HEADERINALLVIEWS);
InitializeSysListView(listViewHwnd, columnInfo, ARRAYSIZE(columnInfo));
FillAttributesListView();
}
void MainWindow::FillAttributesListView()
{
// Add items to the list, respecting any filters.
HWND attributesListHwnd = GetWindowFromId(hwnd_, IdcAttributesList);
if (!IsWindowVisible(attributesListHwnd))
{
return; // Shortcut, since control not displayed anyway.
}
ListViewWriter lw(attributesListHwnd);
isRecursing_ = true;
lw.DisableDrawing();
lw.DeleteAllItems();
lw.mask |= LVIF_PARAM;
uint32_t listIndices[countof(DrawableObject::attributeList)];
ListSubstringPrioritizer substringPrioritizer(attributeFilter_, static_cast<uint32_t>(countof(DrawableObject::attributeList)));
// Get the filtered list.
for (uint32_t i = 0; i < countof(DrawableObject::attributeList); ++i)
{
auto& attribute = DrawableObject::attributeList[i];
auto weight = substringPrioritizer.GetStringWeight(ToChar16ArrayRef(attribute.display));
substringPrioritizer.SetItemWeight(i, weight);
}
auto clampedListIndices = substringPrioritizer.GetItemIndices(OUT listIndices, /*excludeMismatches*/true);
// Add matching items to the ListView.
for (auto& index : clampedListIndices)
{
auto& attribute = DrawableObject::attributeList[index];
lw.lParam = attribute.id;
lw.InsertItem(attribute.display);
lw.AdvanceItem();
}
lw.EnableDrawing();
isRecursing_ = false;
}
void MainWindow::InitializeAttributeValuesListView()
{
const static ListViewColumnInfo columnInfo[] = {
{ 0, 120, u"Name" },
{ 1, 400, u"Value" },
};
auto listViewHwnd = GetWindowFromId(hwnd_, IdcAttributeValuesList);
ListView_SetExtendedListViewStyle(listViewHwnd, LVS_EX_LABELTIP | LVS_EX_DOUBLEBUFFER | LVS_EX_HEADERDRAGDROP | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_HEADERINALLVIEWS);
InitializeSysListView(listViewHwnd, columnInfo, ARRAYSIZE(columnInfo));
}
void MainWindow::UpdateAttributesListView()
{
HWND attributesListHwnd = GetWindowFromId(hwnd_, IdcAttributesList);
if (!IsWindowVisible(attributesListHwnd))
{
return; // Shortcut, since control not displayed anyway.
}
// Update the text in the value column. This expects the items and attribute labels are already filled.
ListViewWriter lw(attributesListHwnd);
std::vector<uint32_t> selectedDrawableObjectIndices = GetSelectedDrawableObjectIndices();
// Get all the attribute indices.
uint32_t attributeIndicesBuffer[DrawableObjectAttributeTotal];
int attributeListItemCount = ListView_GetItemCount(lw.hwnd);
array_ref<uint32_t> attributeIndices(attributeIndicesBuffer, std::min(countof(attributeIndicesBuffer), size_t(attributeListItemCount)));
std::iota(OUT attributeIndices.begin(), OUT attributeIndices.end(), 0);
ListViewRemapIndicesToLparam(lw.hwnd, IN OUT attributeIndices);
lw.DisableDrawing();
size_t const totalDrawableObjects = drawableObjects_.size();
for (auto attributeIndex : attributeIndices)
{
char16_t const* stringValue = DrawableObjectAndValues::GetStringValue(
drawableObjects_,
selectedDrawableObjectIndices,
attributeIndex,
u"<mixed values>"
);
isRecursing_ = true; // Stop pointless LVN_ITEMCHANGED messages.
lw.SetItemText(/*column*/1, stringValue);
isRecursing_ = false;
lw.AdvanceItem();
}
lw.EnableDrawing();
InvalidateRect(lw.hwnd, nullptr, false);
}
void MainWindow::UpdateAttributeValuesListView()
{
HWND attributesValuesListHwnd = GetWindowFromId(hwnd_, IdcAttributeValuesList);
if (!IsWindowVisible(attributesValuesListHwnd))
{
return; // Shortcut, since control not displayed anyway.
}
ListViewWriter lw(attributesValuesListHwnd);
lw.DisableDrawing();
isRecursing_ = true;
lw.DeleteAllItems();
isRecursing_ = false;
std::u16string stringValueBuffer;
// Nop if selectedAttributeIndex_ invalid (no attribute selected).
if (selectedAttributeIndex_ < DrawableObjectAttributeTotal)
{
Attribute const& attribute = DrawableObject::attributeList[selectedAttributeIndex_];
uint32_t const predefinedValuesCount = static_cast<uint32_t>(attribute.predefinedValues.size());
fast_vector<uint32_t, 20, false> orderedIndices(predefinedValuesCount);
if (attribute.semantic == Attribute::SemanticLongText || !isTypingAttributeValueToFilter_)
{
// Just include all the values in order.
std::iota(orderedIndices.begin(), orderedIndices.end(), 0);
}
else
{
// For single line, reorder the list by best match typed so far
// (if recently editing the value).
attribute.GetPredefinedValueIndices(selectedAttributeValue_.c_str(), OUT orderedIndices);
}
isTypingAttributeValueToFilter_ = false; // Reset once used.
int selectedItem = -1; // Keep track of which value to select, if any.
for (uint32_t i = 0; i < predefinedValuesCount; ++i)
{
// Get the name and value for each column.
uint32_t valueIndex = orderedIndices[i];
auto& valueMapping = attribute.predefinedValues[valueIndex];
auto* name = attribute.GetPredefinedValueName(valueIndex);
auto* stringValue = attribute.GetPredefinedValue(
valueIndex,
attribute.GetPredefinedValueFlagsString | attribute.GetPredefinedValueFlagsInteger,
OUT stringValueBuffer
);
// Add entry.
lw.lParam = valueIndex;
lw.mask |= LVIF_PARAM; // Use the lparam to keep track of attribute value index, regardless of order.
lw.InsertItem(name != nullptr ? name : u"");
lw.mask &= ~LVIF_PARAM;
if (stringValue != nullptr)
{
lw.SetItemText(/*column*/1, stringValue);
}
lw.AdvanceItem();
// Keep track of the selection if the strings exactly matched.
if (!selectedAttributeValue_.empty())
{
if (_wcsicmp(ToWChar(name), ToWChar(selectedAttributeValue_.c_str())) == 0
|| (stringValue != nullptr && _wcsicmp(ToWChar(stringValue), ToWChar(selectedAttributeValue_.c_str())) == 0))
{
selectedItem = i;
}
}
}
if (selectedItem != -1)
{
isRecursing_ = true;
lw.SelectAndFocusItem(selectedItem);
lw.iItem = selectedItem;
lw.EnsureVisible();
isRecursing_ = false;
}
}
lw.EnableDrawing();
::InvalidateRect(lw.hwnd, nullptr, false);
}
void MainWindow::UpdateAttributeValuesEdit()
{
char16_t const* stringValue = u"";
char16_t const* cueBannerText = u"<no attributes selected>";
bool hasMultilineText = false;
std::vector<uint32_t> attributeIndices = GetSelectedAttributeIndices();
std::vector<uint32_t> drawableObjectIndices = GetSelectedDrawableObjectIndices();
// Set the edit control's cue banner text based on which attributes are selected.
if (!attributeIndices.empty())
{
cueBannerText = u"<multiple attributes selected>";
if (attributeIndices.size() == 1 && selectedAttributeIndex_ < DrawableObjectAttributeTotal)
{
auto* text = DrawableObject::attributeList[selectedAttributeIndex_].description;
if (text == nullptr) text = DrawableObject::attributeList[selectedAttributeIndex_].display;
if (text != nullptr) cueBannerText = text;
}
}
// Check if any text boxes are multi-line.
for (auto index : attributeIndices)
{
ThrowIf(index >= countof(DrawableObject::attributeList), "Index is beyond DrawableObject::attributeList size!");
if (DrawableObject::attributeList[index].semantic == Attribute::SemanticLongText)
{
hasMultilineText = true;
}
}
// Get the right string for the edit based on which attribute and drawable
// objects are selected. If mixed values are set, leave empty.
bool isFirstItem = true;
for (auto index : attributeIndices)
{
// Check if any text boxes are multi-line.
char16_t const* currentStringValue = DrawableObjectAndValues::GetStringValue(
drawableObjects_,
drawableObjectIndices,
index,
u"" // Empty string if mixed settings between options. Display cue banner text instead.
);
if (isFirstItem)
{
stringValue = currentStringValue;
isFirstItem = false;
}
else if (wcscmp(ToWChar(currentStringValue), ToWChar(stringValue)) != 0)
{
stringValue = u"";
break;
}
}
selectedAttributeValue_.assign(stringValue);
// Update the edit's text, style, and size depending on whether multiline.
// Note that ES_MULTILINE doesn't actually seem to have any effect after
// creation, but it still sets the flag anyway, which allows other code
// to inspect the flag and relay keypresses accordingly.
{
HWND attributeValueEdit = GetWindowFromId(hwnd_, IdcAttributeValuesEdit);
// Update the cue banner text according to the attribute.
SetProp(attributeValueEdit, L"CueBannerText", reinterpret_cast<HANDLE>(const_cast<char16_t*>(cueBannerText)));
// Update the new size.
RECT oldWindowRect;
RECT newWindowRect = { 0,0,172,hasMultilineText ? 48 : 12};
MapDialogRect(hwnd_, IN OUT &newWindowRect);
GetWindowRect(attributeValueEdit, OUT &oldWindowRect);
// Update the text and style, for scroll bars and return key presses.
isRecursing_ = true;
SetWindowText(attributeValueEdit, ToWChar(stringValue));
DWORD oldStyle = GetWindowStyle(attributeValueEdit);
DWORD newStyle = oldStyle & ~(ES_MULTILINE | ES_WANTRETURN | WS_VSCROLL);
if (hasMultilineText)
newStyle |= ES_MULTILINE | ES_WANTRETURN | WS_VSCROLL;
SetWindowStyle(attributeValueEdit, newStyle);
isRecursing_ = false;
// If style change, then reflow the main window.
if (((newStyle ^ oldStyle) & ES_MULTILINE) == ES_MULTILINE)
{
Resize(IddMainWindow);
}
}
}
void MainWindow::UpdateTextEdit()
{
HWND editHwnd = GetWindowFromId(hwnd_, IdcEditText);
if (!IsWindowVisible(editHwnd))
return;
std::vector<uint32_t> drawableObjectIndices = GetSelectedDrawableObjectIndices();
char16_t const* stringValue = DrawableObjectAndValues::GetStringValue(
drawableObjects_,
drawableObjectIndices,
DrawableObjectAttributeText,
u"<mixed values>"
);
std::u16string text;
if (textEscapeMode_ != TextEscapeModeNone)
{
text.assign(stringValue);
EscapeText(IN OUT text);
stringValue = text.c_str();
}
SetWindowText(editHwnd, ToWChar(stringValue));
}
void MainWindow::UpdateAttributeValuesSlider()
{
#if 0 // todo: finish
auto slider = GetWindowFromId(hwnd_, IdcAttributeValueSlider);
if (selectedAttributeIndex_ < DrawableObjectAttributeTotal)
{
auto& attribute = DrawableObject::attributeList[selectedAttributeIndex_];
if (attribute.IsTypeNumeric(attribute.type)
&& !attribute.IsTypeArray(attribute.type)
&& attribute.semantic != Attribute::SemanticEnum
&& attribute.semantic != Attribute::SemanticEnumExclusive
)
{
// todo: respond to TB_ENDTRACK
SendMessage(slider, TBM_SETRANGE, (WPARAM)TRUE, (LPARAM)MAKELONG(0, 20)); // min. & max. positions
SendMessage(slider, TBM_SETPAGESIZE, 0, (LPARAM)4); // new page size
SendMessage(slider, TBM_SETPOS, (WPARAM)TRUE, (LPARAM)7);
}
}
#endif
}
void MainWindow::DeferUpdateUi(NeededUiUpdate neededUiUpdate, uint32_t timeOut)
{
neededUiUpdate_ |= neededUiUpdate;
if (neededUiUpdate_)
{
SetTimer(hwnd_, IdcUpdateUi, timeOut, nullptr);
}
}
void MainWindow::UpdateUi()
{
auto neededUiUpdate = neededUiUpdate_;
neededUiUpdate_ = NeededUiUpdateNone;
if (neededUiUpdate & NeededUiUpdateDrawableObjectsCanvas)
{