Repository navigation
Expand file tree
/
Copy pathcodepad.cpp
More file actions
6874 lines (6022 loc) · 273 KB
/
Copy pathcodepad.cpp
File metadata and controls
6874 lines (6022 loc) · 273 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
/*--------------------------------------------------------------------------
Codepad - Lightweight Win32 Syntax Editor
Copyright (C) 2026 Mincho Georgiev
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
-----------------------------------------------------------------------------*/
#pragma warning(disable: 4005)
#define UNICODE
#define _UNICODE
#include <windows.h>
#include <commctrl.h>
#include <shellapi.h>
#include <commdlg.h>
#include <shlobj.h>
#include <shlwapi.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <stdio.h>
#include <stdint.h>
#include "crypto.h"
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "gdi32.lib")
#pragma comment(lib, "shell32.lib")
#pragma comment(lib, "comdlg32.lib")
#pragma comment(lib, "advapi32.lib")
#pragma comment(lib, "comctl32.lib")
#pragma comment(lib, "shlwapi.lib")
#pragma comment(lib, "ole32.lib")
#ifndef GET_X_LPARAM
#define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
#endif
#ifndef GET_Y_LPARAM
#define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
#endif
#define APPNAME L"Codepad"
#define VERSION L"1.0.067"
#define CUSTOM_EDIT_CLASS L"CustomSyntaxEditor"
#define SETTINGS_DLG_CLASS L"ThemeSettingsDialog"
#define FIND_DLG_CLASS L"FindDialogClass"
#define REPLACE_DLG_CLASS L"ReplaceDialogClass"
#define GOTO_DLG_CLASS L"GotoDialogClass"
#define ABOUT_DLG_CLASS L"AboutDialogClass"
#define PASS_DLG_CLASS L"PasswordDialogClass"
#define GREP_DLG_CLASS L"FindInFilesDialogClass"
#define OUTPUT_PANE_CLASS L"GrepOutputPaneClass"
#define SPLITTER_CLASS L"GrepSplitterClass"
#define TEXT_MARGIN_X 8
#define MAX_UNDO_STEPS 10000
#define IDT_AUTOSCROLL 101
#define IDT_FULLSCREEN 102
#define MIN_ZOOM_POINT_SIZE 6
#define MAX_ZOOM_POINT_SIZE 48
#define MAX_SCROLL_UNITS 30000
// Menu IDs - File
#define IDM_FILE_NEW 1001
#define IDM_FILE_OPEN 1002
#define IDM_FILE_SAVE 1003
#define IDM_FILE_SAVEAS 1004
#define IDM_FILE_EXIT 1005
// Menu IDs - Edit
#define IDM_EDIT_UNDO 1008
#define IDM_EDIT_REDO 1009
#define IDM_EDIT_CUT 1010
#define IDM_EDIT_COPY 1011
#define IDM_EDIT_PASTE 1012
#define IDM_EDIT_DELETE 1013
#define IDM_EDIT_SELECTALL 1014
#define IDM_EDIT_UPPERCASE 1015
#define IDM_EDIT_LOWERCASE 1016
// Menu IDs - Search
#define IDM_SEARCH_FIND 1040
#define IDM_SEARCH_FINDNEXT 1041
#define IDM_SEARCH_FINDPREV 1042
#define IDM_SEARCH_FINDINFILES 1045
#define IDM_SEARCH_REPLACE 1043
#define IDM_SEARCH_GOTO 1044
// Menu IDs - View
#define IDM_VIEW_FULLSCREEN 1020
#define IDM_VIEW_DARK 1021
#define IDM_VIEW_LIGHT 1022
#define IDM_VIEW_FONT 1023
#define IDM_VIEW_THEME_DLG 1024
#define IDM_VIEW_GUIDES 1025
#define IDM_VIEW_LINENUMBERS 1026
#define IDM_VIEW_BRACES 1027
#define IDM_VIEW_STATUSBAR 1028
#define IDM_VIEW_HIGHLIGHT_GUIDES 1029
// Menu IDs - Indentation
#define IDM_INDENT_SIZE_2 1060
#define IDM_INDENT_SIZE_4 1061
#define IDM_INDENT_SIZE_8 1062
#define IDM_INDENT_SPACES 1063
// Menu IDs - Tools
#define IDM_TOOLS_ENCRYPT 1030
#define IDM_TOOLS_DECRYPT 1031
// Menu IDs - Help
#define IDM_HELP_ABOUT 1050
#define IDM_ENC_BASE 1100
// Dialog Controls
#define IDC_DLG_TAB 2000
#define IDC_DLG_ELEMENT_LIST 2001
#define IDC_DLG_CHOOSE_COLOR 2002
#define IDC_DLG_PREVIEW_BOX 2003
#define IDC_DLG_KW_EDIT 2004
#define IDC_DLG_PREPROC_EDIT 2005
#define IDC_DLG_OK 2006
#define IDC_DLG_CANCEL 2007
#define IDC_SAVE_ENC_STATIC 3001
#define IDC_SAVE_ENC_COMBO 3002
#define IDC_STATUSBAR 3003
// Find & Replace Controls
#define IDFINDWHAT 4001
#define IDREPLACEWITH 4002
#define IDWHOLEWORD 4003
#define IDMATCHCASE 4004
#define IDWRAP 4005
#define IDDIRUP 4006
#define IDDIRDOWN 4007
#define IDREPLACE 4008
#define IDREPLACEALL 4009
#define IDREPLACEINSEL 4010
#define IDREPLDONE 4011
#define IDGOTO_LINE 4012
// Find in Files Controls
#define IDFILES 4020
#define IDDIRECTORY 4021
#define IDDOTDOT 4022
#define IDBROWSE 4023
#define IDC_OUTPUT_CLOSE 4024
#define IDC_OUTPUT_LIST 4025
// Password Dialog Controls
#define IDC_CMD_PASS 5001
#define IDC_CMD_OK 5002
// Memory Restrictions:
#define UNDOSTACK_MEMORY (256 * 1024 * 1024)
#define UNDOSTACK_MAX_LINES (1000000)
#define CODEBLOCKS_MAX_LINES (1000000)
// Encoding Definitions
typedef struct {
int codePage;
const wchar_t* name;
int bomType;
} EncodingDesc;
static const EncodingDesc g_encodings[] = {
{ CP_ACP,L"ANSI", 0 },
{ 65001, L"UTF-8", 0 },
{ 65001, L"UTF-8 with BOM", 1 },
{ 1200, L"UTF-16 LE (Unicode)", 2 },
{ 1201, L"UTF-16 BE (Big Endian)", 3 },
{ 1251, L"Windows-1251 (Cyrillic)", 0 },
{ 1252, L"Windows-1252 (Western Latin)", 0 }
};
#define ENCODING_COUNT ((int)(sizeof(g_encodings) / sizeof(g_encodings[0])))
typedef struct {
COLORREF bg;
COLORREF text;
COLORREF keyword;
COLORREF preprocessor;
COLORREF comment;
COLORREF string;
COLORREF number;
COLORREF indentGuide;
COLORREF activeIndentGuide;
COLORREF braceMatchBorder;
COLORREF braceMatchBg;
COLORREF selection;
COLORREF gutterBg;
COLORREF gutterText;
COLORREF gutterLine;
} Theme;
static Theme g_darkTheme = {
RGB(30, 30, 30), // bg: #1E1E1E
RGB(220, 220, 220), // text
RGB(86, 156, 214), // keyword
RGB(155, 155, 155), // preprocessor
RGB(106, 153, 85), // comment
RGB(206, 145, 120), // string
RGB(181, 206, 168), // number
RGB(50, 50, 50), // indentGuide
RGB(0, 150, 255), // activeIndentGuide
RGB(0, 150, 255), // braceMatchBorder
RGB(45, 60, 80), // braceMatchBg
RGB(38, 79, 120), // selection
RGB(37, 37, 38), // gutterBg
RGB(133, 133, 133), // gutterText
RGB(60, 60, 60) // gutterLine
};
static Theme g_lightTheme = {
RGB(255, 255, 255), // bg: #FFFFFF
RGB(0, 0, 0), // text
RGB(0, 0, 127), // keyword
RGB(127, 127, 0), // preprocessor
RGB(0, 127, 0), // comment
RGB(127, 0, 127), // string
RGB(0, 127, 127), // number
RGB(215, 215, 215), // indentGuide
RGB(0, 0, 127), // activeIndentGuide
RGB(0, 0, 255), // braceMatchBorder
RGB(215, 235, 255), // braceMatchBg
RGB(192, 192, 192), // selection
RGB(224, 224, 224), // gutterBg
RGB(128, 128, 128), // gutterText
RGB(192, 192, 192) // gutterLine
};
static wchar_t g_keywordsBuffer[4096] =
L"auto break case char const continue default do double else enum extern float for goto if "
L"int long register return short signed sizeof static struct switch typedef union unsigned void volatile while "
L"class public private protected template typename namespace using new delete true false nullptr virtual override "
L"inline explicit constexpr noexcept decltype static_assert friend mutable";
static wchar_t g_preprocBuffer[2048] =
L"include define undef ifdef ifndef endif elif else if pragma error line warning";
typedef struct {
int64_t col; // 64-bit safe column tracking
int row;
} TextPos;
typedef struct {
wchar_t* text;
size_t length;
size_t capacity;
BOOL inComment;
} Line;
typedef struct {
int openRow;
int openCol;
int closeRow;
int closeCol;
int guideCol;
} CodeBlock;
typedef struct {
wchar_t* text;
TextPos caret;
TextPos selAnchor;
BOOL isModified;
int64_t scrollX;
int scrollY;
} UndoStep;
typedef struct {
UndoStep* steps;
int count;
int capacity;
} UndoStack;
typedef struct {
wchar_t findText[256];
wchar_t replaceText[256];
BOOL matchWholeWord;
BOOL matchCase;
BOOL wrapAround;
BOOL searchDown;
} SearchConfig;
static SearchConfig g_search = { L"", L"", FALSE, FALSE, TRUE, TRUE };
// Find in Files persistent settings
static wchar_t g_grepPatterns[512] = L"*.c *.cpp *.h *.asm *.inc *.cxx *.pas";
static wchar_t g_grepDirectory[MAX_PATH] = L"";
static int g_splitterHeight = -1; // -1 means default 1/4 of window
static BOOL g_showOutputPane = FALSE;
typedef struct {
Line* lines;
int lineCount;
int lineCapacity;
TextPos caret;
TextPos selAnchor;
int64_t preferredCol;
BOOL isMouseDown;
BOOL isGutterSelecting;
BOOL isPendingDrag;
BOOL isDraggingText;
POINT dragStartPt;
TextPos dragSelAnchor;
TextPos dragSelCaret;
TextPos dragDropPos;
DWORD lastDblClickTime;
POINT lastDblClickPt;
BOOL hasBraceMatch;
TextPos brace1;
TextPos brace2;
CodeBlock* blocks;
int blockCount;
int blockCapacity;
UndoStack undoStack;
UndoStack redoStack;
HFONT hFont;
wchar_t fontFace[64];
int pointSize;
int zoomLevel;
BOOL fontBold;
int fontQuality;
int charWidth;
int charHeight;
int gutterWidth;
int64_t scrollX;
int scrollY;
int64_t maxCols;
int visibleLines;
BOOL isDark;
BOOL showIndentGuides;
BOOL highlightIndentGuides;
BOOL showBraceMatch;
BOOL showLineNumbers;
BOOL showStatusBar;
int tabSize;
BOOL useSpaces;
int encodingIdx;
BOOL isModified;
wchar_t filePath[MAX_PATH];
wchar_t fileName[MAX_PATH];
HWND hwndMain;
HWND hwndStatus;
BOOL isCtrlSolo;
} EditorState;
// Global State
static HWND g_hwndEditor = NULL;
static HWND g_hFindDlg = NULL;
static HWND g_hReplaceDlg = NULL;
static HWND g_hGotoDlg = NULL;
static HWND g_hGrepDlg = NULL;
static HWND g_hwndOutputPane = NULL;
static HWND g_hwndOutputList = NULL;
static HWND g_hwndOutputClose = NULL;
static HWND g_hwndSplitter = NULL;
static BOOL g_isSplitting = FALSE;
static BOOL g_isFullscreen = FALSE;
static WINDOWPLACEMENT g_prevPlacement = { sizeof(WINDOWPLACEMENT) };
static DWORD g_prevStyle = 0;
static int g_selectedFontQuality = CLEARTYPE_QUALITY;
static HWND g_hFontQualCombo = NULL;
static int g_selectedDlgEncoding = 0;
static HWND g_hDlgCombo = NULL;
static HCURSOR g_hDragCursor = NULL;
static inline Theme* GetActiveTheme(EditorState* s) {
return (s && s->isDark) ? &g_darkTheme : &g_lightTheme;
}
static void SetCursorPixel(BYTE* andMask, BYTE* xorMask, int x, int y, int type) {
if (x < 0 || x >= 32 || y < 0 || y >= 32) return;
int idx = y * 4 + (x / 8);
BYTE mask = (BYTE)(0x80 >> (x % 8));
andMask[idx] &= ~mask;
if (type == 1) xorMask[idx] |= mask;
else xorMask[idx] &= ~mask;
}
static HCURSOR CreateDragCursor(void) {
BYTE andMask[128];
BYTE xorMask[128];
memset(andMask, 0xFF, sizeof(andMask));
memset(xorMask, 0x00, sizeof(xorMask));
for (int y = 0; y <= 16; y++) SetCursorPixel(andMask, xorMask, 0, y, 0);
for (int y = 0; y <= 11; y++) {
SetCursorPixel(andMask, xorMask, y, y, 0);
for (int x = 1; x < y; x++) SetCursorPixel(andMask, xorMask, x, y, 1);
}
SetCursorPixel(andMask, xorMask, 1, 12, 1); SetCursorPixel(andMask, xorMask, 2, 12, 1); SetCursorPixel(andMask, xorMask, 3, 12, 0);
SetCursorPixel(andMask, xorMask, 1, 13, 1); SetCursorPixel(andMask, xorMask, 2, 13, 0);
SetCursorPixel(andMask, xorMask, 1, 14, 0);
SetCursorPixel(andMask, xorMask, 4, 11, 0); SetCursorPixel(andMask, xorMask, 5, 11, 1); SetCursorPixel(andMask, xorMask, 6, 11, 1); SetCursorPixel(andMask, xorMask, 7, 11, 0);
SetCursorPixel(andMask, xorMask, 6, 12, 0); SetCursorPixel(andMask, xorMask, 7, 12, 1); SetCursorPixel(andMask, xorMask, 8, 12, 0);
SetCursorPixel(andMask, xorMask, 7, 13, 0); SetCursorPixel(andMask, xorMask, 8, 13, 1); SetCursorPixel(andMask, xorMask, 9, 13, 0);
SetCursorPixel(andMask, xorMask, 8, 14, 0); SetCursorPixel(andMask, xorMask, 9, 14, 1); SetCursorPixel(andMask, xorMask, 10, 14, 0);
SetCursorPixel(andMask, xorMask, 9, 15, 0); SetCursorPixel(andMask, xorMask, 10, 15, 0);
for (int x = 13; x <= 23; x++) { SetCursorPixel(andMask, xorMask, x, 13, 0); SetCursorPixel(andMask, xorMask, x, 23, 0); }
for (int y = 13; y <= 23; y++) { SetCursorPixel(andMask, xorMask, 13, y, 0); SetCursorPixel(andMask, xorMask, 23, y, 0); }
for (int y = 14; y <= 22; y++) {
for (int x = 14; x <= 22; x++) SetCursorPixel(andMask, xorMask, x, y, 1);
}
for (int x = 15; x <= 21; x++) SetCursorPixel(andMask, xorMask, x, 16, 0);
for (int x = 15; x <= 21; x++) SetCursorPixel(andMask, xorMask, x, 18, 0);
for (int x = 15; x <= 19; x++) SetCursorPixel(andMask, xorMask, x, 20, 0);
ICONINFO ii;
ZeroMemory(&ii, sizeof(ii));
ii.fIcon = FALSE;
ii.xHotspot = 0;
ii.yHotspot = 0;
ii.hbmMask = CreateBitmap(32, 64, 1, 1, NULL);
BYTE combined[256];
memcpy(combined, andMask, 128);
memcpy(combined + 128, xorMask, 128);
SetBitmapBits(ii.hbmMask, 256, combined);
ii.hbmColor = NULL;
HCURSOR hCur = CreateIconIndirect(&ii);
DeleteObject(ii.hbmMask);
return hCur;
}
// Forward Declarations
static void Tool_Encrypt(HWND hwndEditor, EditorState* s);
static void Tool_Decrypt(HWND hwndEditor, EditorState* s);
static BOOL ShowPasswordDialog(HWND hwndParent, const wchar_t* title, wchar_t* outPass, int maxLen);
static void ShowAboutDialog(HWND hwndParent);
static void RegisterCustomEditControl(HINSTANCE hInstance);
static void PushUndo(EditorState* s);
static void Editor_Undo(HWND hwnd, EditorState* s);
static void Editor_Redo(HWND hwnd, EditorState* s);
static void Editor_ClearAll(EditorState* s);
static void UndoStack_Clear(UndoStack* stack);
static void UpdateEncodingMenuCheck(HWND hwndMain, int activeIdx);
static void UpdateScrollbars(HWND hwnd, EditorState* s);
static void UpdateCaret(HWND hwnd, EditorState* s);
static void UpdateStatusBar(EditorState* s);
static void EnsureCaretVisible(HWND hwnd, EditorState* s);
static void EnsureCaretVisibleEx(HWND hwnd, EditorState* s, float marginRatio);
static void FindMatchingBrace(EditorState* s);
static void UpdateTitle(EditorState* s);
static void RecalculateGutter(EditorState* s);
static void RecalculateMaxCols(EditorState* s);
static void SaveSettingsToIni(HWND hwndMain, EditorState* s);
static void LoadSettingsFromIni(EditorState* s);
static void RecreateEditorFont(HWND hwnd, EditorState* s);
static BOOL PromptSaveIfModified(HWND hwndMain, EditorState* s);
static BOOL DoFileSaveAs(HWND hwndMain, EditorState* s);
static BOOL LoadFileContent(HWND hwnd, EditorState* s, const wchar_t* path);
static void Editor_SetText(EditorState* s, const wchar_t* text);
static void Editor_ConvertIndentation(EditorState* s, int oldTabSize, int newTabSize);
static void UpdateCodeBlocks(EditorState* s);
static void PaintIndentGuides(HDC hdc, EditorState* s, int lineIdx, int y, const Theme* theme, int activeBlockIdx);
static void PaintSyntaxLine(HDC hdc, EditorState* s, int lineIdx, int y, RECT* clientRc, const Theme* theme, int activeBlockIdx);
static void ShowContextMenu(HWND hwnd, EditorState* s, int screenX, int screenY);
static void ShowThemeSettingsDialog(HWND hwndParent, EditorState* s);
static void ShowFindDialog(HWND hwndParent, EditorState* s);
static void ShowReplaceDialog(HWND hwndParent, EditorState* s);
static void ShowGotoDialog(HWND hwndParent, EditorState* s);
static void ShowFindInFilesDialog(HWND hwndParent, EditorState* s);
static BOOL FindNextText(EditorState* s, BOOL searchDown);
static void Editor_MakeSelectionCase(EditorState* s, BOOL makeUpper);
static TextPos ScreenToTextPos(EditorState* s, int x, int y);
static BOOL IsCharInQuoteOrComment(EditorState* s, int r, int64_t c);
static void Line_FreeText(Line* line);
static void Line_Init(Line* line);
static void ClearSelection(EditorState* s);
static void RecalculateAllOutputListExtent(void);
static LRESULT CALLBACK CustomEditProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK ThemeDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK OutputPaneProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK SplitterProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
// -------------------------------------------------------------
// Password & About Dialog Implementations
// -------------------------------------------------------------
typedef struct {
const wchar_t* title;
wchar_t* outPass;
int maxLen;
BOOL confirmed;
HWND hEdit;
} PassDlgState;
static LRESULT CALLBACK PasswordDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
PassDlgState* ds = (PassDlgState*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
switch (msg) {
case WM_NCCREATE: {
CREATESTRUCT* cs = (CREATESTRUCT*)lParam;
ds = (PassDlgState*)cs->lpCreateParams;
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)ds);
return DefWindowProc(hwnd, msg, wParam, lParam);
}
case WM_CREATE: {
HFONT hGuiFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
if (ds && ds->title) SetWindowTextW(hwnd, ds->title);
ds->hEdit = CreateWindowExW(WS_EX_CLIENTEDGE, L"EDIT", L"",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_PASSWORD | ES_AUTOHSCROLL,
20, 18, 248, 22, hwnd, (HMENU)IDC_CMD_PASS, GetModuleHandle(NULL), NULL);
SendMessage(ds->hEdit, WM_SETFONT, (WPARAM)hGuiFont, TRUE);
HWND hOk = CreateWindowW(L"BUTTON", L"OK",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_DEFPUSHBUTTON,
275, 17, 75, 25, hwnd, (HMENU)IDC_CMD_OK, GetModuleHandle(NULL), NULL);
SendMessage(hOk, WM_SETFONT, (WPARAM)hGuiFont, TRUE);
SetFocus(ds->hEdit);
return 0;
}
case WM_COMMAND: {
int id = LOWORD(wParam);
if (id == IDC_CMD_OK || id == IDOK) {
if (ds->outPass && ds->maxLen > 0) GetWindowTextW(ds->hEdit, ds->outPass, ds->maxLen);
ds->confirmed = TRUE;
HWND hParent = GetParent(hwnd);
if (hParent) { EnableWindow(hParent, TRUE); SetForegroundWindow(hParent); }
DestroyWindow(hwnd);
return 0;
} else if (id == IDCANCEL) {
HWND hParent = GetParent(hwnd);
if (hParent) { EnableWindow(hParent, TRUE); SetForegroundWindow(hParent); }
DestroyWindow(hwnd);
return 0;
}
break;
}
case WM_CLOSE: {
HWND hParent = GetParent(hwnd);
if (hParent) { EnableWindow(hParent, TRUE); SetForegroundWindow(hParent); }
DestroyWindow(hwnd);
return 0;
}
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
static BOOL ShowPasswordDialog(HWND hwndParent, const wchar_t* title, wchar_t* outPass, int maxLen) {
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = PasswordDlgProc;
wc.hInstance = GetModuleHandle(NULL);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
wc.lpszClassName = PASS_DLG_CLASS;
RegisterClassEx(&wc);
PassDlgState ds;
ds.title = title;
ds.outPass = outPass;
ds.maxLen = maxLen;
ds.confirmed = FALSE;
ds.hEdit = NULL;
RECT rcParent;
GetWindowRect(hwndParent, &rcParent);
int dlgW = 380, dlgH = 100;
int x = rcParent.left + (rcParent.right - rcParent.left - dlgW) / 2;
int y = rcParent.top + (rcParent.bottom - rcParent.top - dlgH) / 2;
HWND hDlg = CreateWindowExW(
WS_EX_DLGMODALFRAME, PASS_DLG_CLASS, title,
WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_VISIBLE,
x, y, dlgW, dlgH, hwndParent, NULL, GetModuleHandle(NULL), &ds
);
SetWindowTextW(hDlg, title);
EnableWindow(hwndParent, FALSE);
MSG msg;
while (IsWindow(hDlg) && GetMessage(&msg, NULL, 0, 0)) {
if (msg.message == WM_KEYDOWN && msg.wParam == VK_RETURN) {
SendMessage(hDlg, WM_COMMAND, IDC_CMD_OK, 0);
continue;
}
if (msg.message == WM_KEYDOWN && msg.wParam == VK_ESCAPE) {
SendMessage(hDlg, WM_COMMAND, IDCANCEL, 0);
continue;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
EnableWindow(hwndParent, TRUE);
SetForegroundWindow(hwndParent);
SetFocus(hwndParent);
return ds.confirmed;
}
static LRESULT CALLBACK AboutDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CREATE: {
HFONT hGuiFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
HWND hTitle = CreateWindowW(L"STATIC", APPNAME, WS_CHILD | WS_VISIBLE | SS_CENTER, 10, 18, 300, 20, hwnd, NULL, NULL, NULL);
SendMessage(hTitle, WM_SETFONT, (WPARAM)hGuiFont, TRUE);
wchar_t verStr[64];
wsprintf(verStr, L"Version %s", VERSION);
HWND hVer = CreateWindowW(L"STATIC", verStr, WS_CHILD | WS_VISIBLE | SS_CENTER, 10, 42, 300, 18, hwnd, NULL, NULL, NULL);
SendMessage(hVer, WM_SETFONT, (WPARAM)hGuiFont, TRUE);
HWND hSub = CreateWindowW(L"STATIC", L"Lightweight Syntax Editor", WS_CHILD | WS_VISIBLE | SS_CENTER, 10, 64, 300, 18, hwnd, NULL, NULL, NULL);
SendMessage(hSub, WM_SETFONT, (WPARAM)hGuiFont, TRUE);
HWND hOk = CreateWindowW(L"BUTTON", L"OK", WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_DEFPUSHBUTTON, 120, 96, 80, 26, hwnd, (HMENU)IDOK, NULL, NULL);
SendMessage(hOk, WM_SETFONT, (WPARAM)hGuiFont, TRUE);
SetFocus(hOk);
return 0;
}
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
HWND hParent = GetParent(hwnd);
if (hParent) { EnableWindow(hParent, TRUE); SetForegroundWindow(hParent); }
DestroyWindow(hwnd);
return 0;
}
break;
case WM_CLOSE: {
HWND hParent = GetParent(hwnd);
if (hParent) { EnableWindow(hParent, TRUE); SetForegroundWindow(hParent); }
DestroyWindow(hwnd);
return 0;
}
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
static void ShowAboutDialog(HWND hwndParent) {
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = AboutDlgProc;
wc.hInstance = GetModuleHandle(NULL);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
wc.lpszClassName = ABOUT_DLG_CLASS;
RegisterClassEx(&wc);
RECT rcParent;
GetWindowRect(hwndParent, &rcParent);
int dlgW = 335, dlgH = 170;
int x = rcParent.left + (rcParent.right - rcParent.left - dlgW) / 2;
int y = rcParent.top + (rcParent.bottom - rcParent.top - dlgH) / 2;
HWND hDlg = CreateWindowExW(
WS_EX_DLGMODALFRAME, ABOUT_DLG_CLASS, L"About " APPNAME,
WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_VISIBLE,
x, y, dlgW, dlgH, hwndParent, NULL, GetModuleHandle(NULL), NULL
);
EnableWindow(hwndParent, FALSE);
MSG msg;
while (IsWindow(hDlg) && GetMessage(&msg, NULL, 0, 0)) {
if (msg.message == WM_KEYDOWN && (msg.wParam == VK_RETURN || msg.wParam == VK_ESCAPE)) {
SendMessage(hDlg, WM_COMMAND, IDOK, 0);
continue;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
EnableWindow(hwndParent, TRUE);
SetForegroundWindow(hwndParent);
SetFocus(hwndParent);
}
static void SafeFreeDocText(wchar_t* buf) {
if (buf) free(buf);
}
static wchar_t* Editor_GetText(const EditorState* s) {
if (!s || s->lineCount <= 0) return NULL;
size_t totalChars = 0;
for (int r = 0; r < s->lineCount; r++) {
totalChars += s->lines[r].length;
if (r < s->lineCount - 1) totalChars += 2;
}
size_t allocBytes = (totalChars + 1) * sizeof(wchar_t);
wchar_t* buf = (wchar_t*)malloc(allocBytes);
if (!buf) return NULL;
wchar_t* p = buf;
for (int r = 0; r < s->lineCount; r++) {
size_t len = s->lines[r].length;
if (len > 0 && s->lines[r].text) {
memcpy(p, s->lines[r].text, len * sizeof(wchar_t));
p += len;
}
if (r < s->lineCount - 1) {
*p++ = L'\r';
*p++ = L'\n';
}
}
*p = L'\0';
return buf;
}
#ifndef IS_HIGH_SURROGATE
#define IS_HIGH_SURROGATE(wch) (((wch) >= 0xD800) && ((wch) <= 0xDBFF))
#endif
// -------------------------------------------------------------
// 64-bit Safe Chunked Multibyte Conversion (Bypasses Win32 2GB Limit)
// -------------------------------------------------------------
static unsigned char* Chunked_WideToUTF8(const wchar_t* wstr, size_t wlen, size_t* outLen) {
if (!wstr || wlen == 0) {
if (outLen) *outLen = 0;
return NULL;
}
const size_t CHUNK_SIZE = 64 * 1024 * 1024;
size_t offset = 0;
size_t totalBytes = 0;
while (offset < wlen) {
size_t thisChunk = wlen - offset;
if (thisChunk > CHUNK_SIZE) {
thisChunk = CHUNK_SIZE;
if (offset + thisChunk < wlen && IS_HIGH_SURROGATE(wstr[offset + thisChunk - 1])) {
thisChunk--;
}
}
int bytes = WideCharToMultiByte(CP_UTF8, 0, wstr + offset, (int)thisChunk, NULL, 0, NULL, NULL);
if (bytes <= 0) return NULL;
totalBytes += (size_t)bytes;
offset += thisChunk;
}
unsigned char* outBuf = (unsigned char*)malloc(totalBytes + 32);
if (!outBuf) return NULL;
offset = 0;
size_t writeOffset = 0;
while (offset < wlen) {
size_t thisChunk = wlen - offset;
if (thisChunk > CHUNK_SIZE) {
thisChunk = CHUNK_SIZE;
if (offset + thisChunk < wlen && IS_HIGH_SURROGATE(wstr[offset + thisChunk - 1])) {
thisChunk--;
}
}
int bytes = WideCharToMultiByte(CP_UTF8, 0, wstr + offset, (int)thisChunk, (char*)outBuf + writeOffset, (int)(totalBytes - writeOffset + 32), NULL, NULL);
if (bytes <= 0) {
free(outBuf);
return NULL;
}
writeOffset += (size_t)bytes;
offset += thisChunk;
}
outBuf[writeOffset] = '\0';
if (outLen) *outLen = writeOffset;
return outBuf;
}
static wchar_t* Chunked_UTF8ToWide(const unsigned char* utf8, size_t utf8Len, size_t* outWLen) {
if (!utf8 || utf8Len == 0) {
if (outWLen) *outWLen = 0;
return NULL;
}
const size_t CHUNK_SIZE = 64 * 1024 * 1024;
size_t offset = 0;
size_t totalChars = 0;
while (offset < utf8Len) {
size_t thisChunk = utf8Len - offset;
if (thisChunk > CHUNK_SIZE) {
thisChunk = CHUNK_SIZE;
while (thisChunk > 0 && (utf8[offset + thisChunk] & 0xC0) == 0x80) {
thisChunk--;
}
}
int chars = MultiByteToWideChar(CP_UTF8, 0, (const char*)utf8 + offset, (int)thisChunk, NULL, 0);
if (chars <= 0) {
chars = MultiByteToWideChar(CP_ACP, 0, (const char*)utf8 + offset, (int)thisChunk, NULL, 0);
if (chars <= 0) return NULL;
}
totalChars += (size_t)chars;
offset += thisChunk;
}
wchar_t* outBuf = (wchar_t*)malloc((totalChars + 2) * sizeof(wchar_t));
if (!outBuf) return NULL;
offset = 0;
size_t writeOffset = 0;
while (offset < utf8Len) {
size_t thisChunk = utf8Len - offset;
if (thisChunk > CHUNK_SIZE) {
thisChunk = CHUNK_SIZE;
while (thisChunk > 0 && (utf8[offset + thisChunk] & 0xC0) == 0x80) {
thisChunk--;
}
}
int chars = MultiByteToWideChar(CP_UTF8, 0, (const char*)utf8 + offset, (int)thisChunk, outBuf + writeOffset, (int)(totalChars - writeOffset + 2));
if (chars <= 0) {
chars = MultiByteToWideChar(CP_ACP, 0, (const char*)utf8 + offset, (int)thisChunk, outBuf + writeOffset, (int)(totalChars - writeOffset + 2));
if (chars <= 0) {
free(outBuf);
return NULL;
}
}
writeOffset += (size_t)chars;
offset += thisChunk;
}
outBuf[writeOffset] = L'\0';
if (outWLen) *outWLen = writeOffset;
return outBuf;
}
static inline int hex_char_val(wchar_t c) {
if (c >= L'0' && c <= L'9') return (int)(c - L'0');
if (c >= L'a' && c <= L'f') return (int)(c - L'a' + 10);
if (c >= L'A' && c <= L'F') return (int)(c - L'A' + 10);
return -1;
}
static void Tool_Encrypt(HWND hwndEditor, EditorState* s)
{
if (!s) s = (EditorState*)GetWindowLongPtr(hwndEditor, GWLP_USERDATA);
if (!s) return;
if (s->lineCount <= 0 || (s->lineCount == 1 && s->lines[0].length == 0)) return;
wchar_t wpass[256] = { 0 };
char pass[512] = { 0 };
HWND hOwner = (s && s->hwndMain) ? s->hwndMain : hwndEditor;
if (!ShowPasswordDialog(hOwner, L"Encrypt", wpass, 256) || wpass[0] == L'\0') {
SecureZeroMemory(wpass, sizeof(wpass));
return;
}
WideCharToMultiByte(CP_UTF8, 0, wpass, -1, pass, sizeof(pass), NULL, NULL);
SecureZeroMemory(wpass, sizeof(wpass));
wchar_t* text = Editor_GetText(s);
if (!text) {
SecureZeroMemory(pass, sizeof(pass));
MessageBoxW(hOwner, L"Insufficient memory for encryption.", L"Error", MB_OK | MB_ICONERROR);
return;
}
size_t wLen = wcslen(text);
if (wLen == 0) {
SafeFreeDocText(text);
SecureZeroMemory(pass, sizeof(pass));
return;
}
size_t lentext = 0;
unsigned char* bufftext = Chunked_WideToUTF8(text, wLen, &lentext);
SafeFreeDocText(text);
if (!bufftext) {
SecureZeroMemory(pass, sizeof(pass));
MessageBoxW(hOwner, L"Insufficient memory for encryption.", L"Error", MB_OK | MB_ICONERROR);
return;
}
uint32_t lencrypt = 0;
unsigned char* buffcrypt = AES_Encrypt(bufftext, (uint32_t)lentext, pass, &lencrypt);
free(bufftext);
SecureZeroMemory(pass, sizeof(pass));
if (!buffcrypt) {
MessageBoxW(hOwner, L"Insufficient memory for encryption.", L"Error", MB_OK | MB_ICONERROR);
return;
}
uint32_t lenhex = 0;
char* buffhex = hex_encode(buffcrypt, lencrypt, &lenhex);
free(buffcrypt);
if (!buffhex) {
MessageBoxW(hOwner, L"Insufficient memory for encryption.", L"Error", MB_OK | MB_ICONERROR);
return;
}
wchar_t* wOutHex = (wchar_t*)malloc(((size_t)lenhex + 2) * sizeof(wchar_t));
if (!wOutHex) {
free(buffhex);
MessageBoxW(hOwner, L"Insufficient memory for encryption.", L"Error", MB_OK | MB_ICONERROR);
return;
}
for (size_t i = 0; i < (size_t)lenhex; i++) {
wOutHex[i] = (wchar_t)buffhex[i];
}
wOutHex[lenhex] = L'\0';
free(buffhex);
PushUndo(s);
Editor_SetText(s, wOutHex);
free(wOutHex);
s->isModified = TRUE;
UpdateTitle(s);
UpdateScrollbars(hwndEditor, s);
UpdateCaret(hwndEditor, s);
FindMatchingBrace(s);
InvalidateRect(hwndEditor, NULL, FALSE);
UpdateWindow(hwndEditor);
}
static void Tool_Decrypt(HWND hwndEditor, EditorState* s)
{
if (!s) s = (EditorState*)GetWindowLongPtr(hwndEditor, GWLP_USERDATA);
if (!s) return;
if (s->lineCount <= 0 || (s->lineCount == 1 && s->lines[0].length == 0)) return;
size_t wLen = 0;
wchar_t* textAllocated = NULL;
const wchar_t* textPtr = NULL;
if (s->lineCount == 1 && s->lines[0].text) {
wLen = s->lines[0].length;
textPtr = s->lines[0].text;
} else {
textAllocated = Editor_GetText(s);
if (!textAllocated) {
HWND hOwner = (s && s->hwndMain) ? s->hwndMain : hwndEditor;
MessageBoxW(hOwner, L"Insufficient memory for decryption.", L"Error", MB_OK | MB_ICONERROR);
return;
}
wLen = wcslen(textAllocated);
textPtr = textAllocated;
}
size_t start = 0;
while (start < wLen && (textPtr[start] == L' ' || textPtr[start] == L'\t' || textPtr[start] == L'\r' || textPtr[start] == L'\n')) start++;
size_t end = wLen;
while (end > start && (textPtr[end - 1] == L' ' || textPtr[end - 1] == L'\t' || textPtr[end - 1] == L'\r' || textPtr[end - 1] == L'\n')) end--;
size_t hexLen = end - start;
// Minimum header length requirement for AES-GCM payload
if (hexLen < (CRYPTO_HEADER_SIZE * 2) || (hexLen % 2) != 0) {
if (textAllocated) SafeFreeDocText(textAllocated);
return;
}
for (size_t i = start; i < end; i++) {
wchar_t c = textPtr[i];
if (!((c >= L'0' && c <= L'9') || (c >= L'a' && c <= L'f') || (c >= L'A' && c <= L'F'))) {
if (textAllocated) SafeFreeDocText(textAllocated);
return;
}
}
wchar_t wpass[256] = { 0 };
char pass[512] = { 0 };
HWND hOwner = (s && s->hwndMain) ? s->hwndMain : hwndEditor;
if (!ShowPasswordDialog(hOwner, L"Decrypt", wpass, 256) || wpass[0] == L'\0') {
SecureZeroMemory(wpass, sizeof(wpass));
if (textAllocated) SafeFreeDocText(textAllocated);
return;
}