-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathretropad.c
More file actions
815 lines (729 loc) · 26.7 KB
/
retropad.c
File metadata and controls
815 lines (729 loc) · 26.7 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
// retropad - a Petzold-style Win32 notepad clone implemented in mostly plain C.
// Keeps the classic menus/accelerators, word wrap, status bar, find/replace,
// font picker, and basic file load/save with BOM detection.
#include <windows.h>
#include <commdlg.h>
#include <commctrl.h>
#include <shellapi.h>
#include <strsafe.h>
#include "resource.h"
#include "file_io.h"
#define APP_TITLE L"retropad"
#define UNTITLED_NAME L"Untitled"
#define MAX_PATH_BUFFER 1024
#define DEFAULT_WIDTH 640
#define DEFAULT_HEIGHT 480
typedef struct AppState {
HWND hwndMain;
HWND hwndEdit;
HWND hwndStatus;
HFONT hFont;
WCHAR currentPath[MAX_PATH_BUFFER];
BOOL wordWrap;
BOOL statusVisible;
BOOL statusBeforeWrap;
BOOL modified;
TextEncoding encoding;
FINDREPLACEW find;
HWND hFindDlg;
HWND hReplaceDlg;
UINT findFlags;
WCHAR findText[128];
WCHAR replaceText[128];
} AppState;
static AppState g_app = {0};
static HINSTANCE g_hInst = NULL;
static UINT g_findMsg = 0;
static void UpdateTitle(HWND hwnd);
static void CreateEditControl(HWND hwnd);
static void UpdateLayout(HWND hwnd);
static BOOL PromptSaveChanges(HWND hwnd);
static BOOL DoFileOpen(HWND hwnd);
static BOOL DoFileSave(HWND hwnd, BOOL saveAs);
static void DoFileNew(HWND hwnd);
static void SetWordWrap(HWND hwnd, BOOL enabled);
static void ToggleStatusBar(HWND hwnd, BOOL visible);
static void UpdateStatusBar(HWND hwnd);
static void ShowFindDialog(HWND hwnd);
static void ShowReplaceDialog(HWND hwnd);
static BOOL DoFindNext(BOOL reverse);
static void DoSelectFont(HWND hwnd);
static void InsertTimeDate(HWND hwnd);
static void HandleFindReplace(LPFINDREPLACE lpfr);
static BOOL LoadDocumentFromPath(HWND hwnd, LPCWSTR path);
static INT_PTR CALLBACK GoToDlgProc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam);
static INT_PTR CALLBACK AboutDlgProc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam);
static BOOL GetEditText(HWND hwndEdit, WCHAR **bufferOut, int *lengthOut) {
int length = GetWindowTextLengthW(hwndEdit);
WCHAR *buffer = (WCHAR *)HeapAlloc(GetProcessHeap(), 0, (length + 1) * sizeof(WCHAR));
if (!buffer) return FALSE;
GetWindowTextW(hwndEdit, buffer, length + 1);
if (lengthOut) *lengthOut = length;
*bufferOut = buffer;
return TRUE;
}
static BOOL FindInEdit(HWND hwndEdit, const WCHAR *needle, BOOL matchCase, BOOL searchDown, DWORD startPos, DWORD *outStart, DWORD *outEnd) {
if (!needle || needle[0] == L'\0') return FALSE;
WCHAR *text = NULL;
int len = 0;
if (!GetEditText(hwndEdit, &text, &len)) return FALSE;
size_t needleLen = wcslen(needle);
WCHAR *haystack = text;
WCHAR *needleBuf = (WCHAR *)HeapAlloc(GetProcessHeap(), 0, (needleLen + 1) * sizeof(WCHAR));
if (!needleBuf) {
HeapFree(GetProcessHeap(), 0, text);
return FALSE;
}
StringCchCopyW(needleBuf, needleLen + 1, needle);
if (!matchCase) {
CharLowerBuffW(haystack, len);
CharLowerBuffW(needleBuf, (DWORD)needleLen);
}
if (startPos > (DWORD)len) startPos = (DWORD)len;
WCHAR *found = NULL;
if (searchDown) {
found = wcsstr(haystack + startPos, needleBuf);
if (!found && startPos > 0) {
found = wcsstr(haystack, needleBuf);
}
} else {
WCHAR *p = haystack;
while ((p = wcsstr(p, needleBuf)) != NULL) {
DWORD idx = (DWORD)(p - haystack);
if (idx < startPos) {
found = p;
p++;
} else {
break;
}
}
if (!found && startPos < (DWORD)len) {
p = haystack + startPos;
while ((p = wcsstr(p, needleBuf)) != NULL) {
found = p;
p++;
}
}
}
BOOL result = FALSE;
if (found) {
DWORD pos = (DWORD)(found - haystack);
*outStart = pos;
*outEnd = pos + (DWORD)needleLen;
result = TRUE;
}
HeapFree(GetProcessHeap(), 0, text);
HeapFree(GetProcessHeap(), 0, needleBuf);
return result;
}
static int ReplaceAllOccurrences(HWND hwndEdit, const WCHAR *needle, const WCHAR *replacement, BOOL matchCase) {
if (!needle || needle[0] == L'\0') return 0;
WCHAR *text = NULL;
int len = 0;
if (!GetEditText(hwndEdit, &text, &len)) return 0;
size_t needleLen = wcslen(needle);
size_t replLen = replacement ? wcslen(replacement) : 0;
WCHAR *searchBuf = (WCHAR *)HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
WCHAR *needleBuf = (WCHAR *)HeapAlloc(GetProcessHeap(), 0, (needleLen + 1) * sizeof(WCHAR));
if (!searchBuf || !needleBuf) {
HeapFree(GetProcessHeap(), 0, text);
if (searchBuf) HeapFree(GetProcessHeap(), 0, searchBuf);
if (needleBuf) HeapFree(GetProcessHeap(), 0, needleBuf);
return 0;
}
StringCchCopyW(searchBuf, len + 1, text);
StringCchCopyW(needleBuf, needleLen + 1, needle);
if (!matchCase) {
CharLowerBuffW(searchBuf, len);
CharLowerBuffW(needleBuf, (DWORD)needleLen);
}
int count = 0;
WCHAR *p = searchBuf;
while ((p = wcsstr(p, needleBuf)) != NULL) {
count++;
p += needleLen;
}
if (count == 0) {
HeapFree(GetProcessHeap(), 0, text);
HeapFree(GetProcessHeap(), 0, searchBuf);
HeapFree(GetProcessHeap(), 0, needleBuf);
return 0;
}
size_t newLen = (size_t)len - (size_t)count * needleLen + (size_t)count * replLen;
WCHAR *result = (WCHAR *)HeapAlloc(GetProcessHeap(), 0, (newLen + 1) * sizeof(WCHAR));
if (!result) {
HeapFree(GetProcessHeap(), 0, text);
HeapFree(GetProcessHeap(), 0, searchBuf);
HeapFree(GetProcessHeap(), 0, needleBuf);
return 0;
}
WCHAR *dst = result;
WCHAR *searchCur = searchBuf;
WCHAR *origCur = text;
while ((p = wcsstr(searchCur, needleBuf)) != NULL) {
size_t delta = (size_t)(p - searchCur);
CopyMemory(dst, origCur, delta * sizeof(WCHAR));
dst += delta;
origCur += delta;
searchCur += delta;
if (replLen) {
CopyMemory(dst, replacement, replLen * sizeof(WCHAR));
dst += replLen;
}
origCur += needleLen;
searchCur += needleLen;
}
size_t tail = wcslen(origCur);
CopyMemory(dst, origCur, tail * sizeof(WCHAR));
dst += tail;
*dst = L'\0';
SetWindowTextW(hwndEdit, result);
HeapFree(GetProcessHeap(), 0, text);
HeapFree(GetProcessHeap(), 0, searchBuf);
HeapFree(GetProcessHeap(), 0, needleBuf);
HeapFree(GetProcessHeap(), 0, result);
SendMessageW(hwndEdit, EM_SETMODIFY, TRUE, 0);
g_app.modified = TRUE;
UpdateTitle(g_app.hwndMain);
return count;
}
static void UpdateTitle(HWND hwnd) {
WCHAR name[MAX_PATH_BUFFER];
if (g_app.currentPath[0]) {
WCHAR *fileName = wcsrchr(g_app.currentPath, L'\\');
fileName = fileName ? fileName + 1 : g_app.currentPath;
StringCchCopyW(name, MAX_PATH_BUFFER, fileName);
} else {
StringCchCopyW(name, MAX_PATH_BUFFER, UNTITLED_NAME);
}
WCHAR title[MAX_PATH_BUFFER + 32];
StringCchPrintfW(title, ARRAYSIZE(title), L"%s%s - %s", (g_app.modified ? L"*" : L""), name, APP_TITLE);
SetWindowTextW(hwnd, title);
}
static void ApplyFontToEdit(HWND hwndEdit, HFONT font) {
SendMessageW(hwndEdit, WM_SETFONT, (WPARAM)font, TRUE);
}
static void CreateEditControl(HWND hwnd) {
if (g_app.hwndEdit) {
DestroyWindow(g_app.hwndEdit);
}
DWORD style = WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_WANTRETURN | ES_NOHIDESEL;
if (!g_app.wordWrap) {
style |= WS_HSCROLL | ES_AUTOHSCROLL;
}
g_app.hwndEdit = CreateWindowExW(WS_EX_CLIENTEDGE, L"EDIT", NULL, style, 0, 0, 0, 0, hwnd, (HMENU)1, g_hInst, NULL);
if (g_app.hwndEdit && g_app.hFont) {
ApplyFontToEdit(g_app.hwndEdit, g_app.hFont);
}
SendMessageW(g_app.hwndEdit, EM_SETLIMITTEXT, 0, 0); // allow large files
UpdateLayout(hwnd);
}
static void ToggleStatusBar(HWND hwnd, BOOL visible) {
g_app.statusVisible = visible;
if (visible) {
if (!g_app.hwndStatus) {
g_app.hwndStatus = CreateStatusWindowW(WS_CHILD | SBARS_SIZEGRIP, L"", hwnd, 2);
}
ShowWindow(g_app.hwndStatus, SW_SHOW);
} else if (g_app.hwndStatus) {
ShowWindow(g_app.hwndStatus, SW_HIDE);
}
UpdateLayout(hwnd);
UpdateStatusBar(hwnd);
}
static void UpdateLayout(HWND hwnd) {
RECT rc;
GetClientRect(hwnd, &rc);
int statusHeight = 0;
if (g_app.statusVisible && g_app.hwndStatus) {
SendMessageW(g_app.hwndStatus, WM_SIZE, 0, 0);
RECT sbrc;
GetWindowRect(g_app.hwndStatus, &sbrc);
statusHeight = sbrc.bottom - sbrc.top;
MoveWindow(g_app.hwndStatus, 0, rc.bottom - statusHeight, rc.right, statusHeight, TRUE);
}
if (g_app.hwndEdit) {
MoveWindow(g_app.hwndEdit, 0, 0, rc.right, rc.bottom - statusHeight, TRUE);
}
}
static BOOL PromptSaveChanges(HWND hwnd) {
if (!g_app.modified) return TRUE;
WCHAR prompt[MAX_PATH_BUFFER + 64];
const WCHAR *name = g_app.currentPath[0] ? g_app.currentPath : UNTITLED_NAME;
StringCchPrintfW(prompt, ARRAYSIZE(prompt), L"Do you want to save changes to %s?", name);
int res = MessageBoxW(hwnd, prompt, APP_TITLE, MB_ICONQUESTION | MB_YESNOCANCEL);
if (res == IDYES) {
return DoFileSave(hwnd, FALSE);
}
return res == IDNO;
}
static BOOL LoadDocumentFromPath(HWND hwnd, LPCWSTR path) {
WCHAR *text = NULL;
TextEncoding enc = ENC_UTF8;
if (!LoadTextFile(hwnd, path, &text, NULL, &enc)) {
return FALSE;
}
SetWindowTextW(g_app.hwndEdit, text);
HeapFree(GetProcessHeap(), 0, text);
StringCchCopyW(g_app.currentPath, ARRAYSIZE(g_app.currentPath), path);
g_app.encoding = enc;
SendMessageW(g_app.hwndEdit, EM_SETMODIFY, FALSE, 0);
g_app.modified = FALSE;
UpdateTitle(hwnd);
UpdateStatusBar(hwnd);
return TRUE;
}
static BOOL DoFileOpen(HWND hwnd) {
if (!PromptSaveChanges(hwnd)) return FALSE;
WCHAR path[MAX_PATH_BUFFER] = L"";
if (!OpenFileDialog(hwnd, path, ARRAYSIZE(path))) {
return FALSE;
}
return LoadDocumentFromPath(hwnd, path);
}
static BOOL DoFileSave(HWND hwnd, BOOL saveAs) {
WCHAR path[MAX_PATH_BUFFER];
if (saveAs || g_app.currentPath[0] == L'\0') {
path[0] = L'\0';
if (g_app.currentPath[0]) {
StringCchCopyW(path, ARRAYSIZE(path), g_app.currentPath);
}
if (!SaveFileDialog(hwnd, path, ARRAYSIZE(path))) {
return FALSE;
}
StringCchCopyW(g_app.currentPath, ARRAYSIZE(g_app.currentPath), path);
} else {
StringCchCopyW(path, ARRAYSIZE(path), g_app.currentPath);
}
int len = GetWindowTextLengthW(g_app.hwndEdit);
WCHAR *buffer = (WCHAR *)HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
if (!buffer) return FALSE;
GetWindowTextW(g_app.hwndEdit, buffer, len + 1);
BOOL ok = SaveTextFile(hwnd, path, buffer, len, g_app.encoding);
HeapFree(GetProcessHeap(), 0, buffer);
if (ok) {
SendMessageW(g_app.hwndEdit, EM_SETMODIFY, FALSE, 0);
g_app.modified = FALSE;
UpdateTitle(hwnd);
}
return ok;
}
static void DoFileNew(HWND hwnd) {
if (!PromptSaveChanges(hwnd)) return;
SetWindowTextW(g_app.hwndEdit, L"");
g_app.currentPath[0] = L'\0';
g_app.encoding = ENC_UTF8;
SendMessageW(g_app.hwndEdit, EM_SETMODIFY, FALSE, 0);
g_app.modified = FALSE;
UpdateTitle(hwnd);
UpdateStatusBar(hwnd);
}
static void SetWordWrap(HWND hwnd, BOOL enabled) {
if (g_app.wordWrap == enabled) return;
g_app.wordWrap = enabled;
HWND edit = g_app.hwndEdit;
WCHAR *text = NULL;
int len = 0;
if (!GetEditText(edit, &text, &len)) {
return;
}
DWORD start = 0, end = 0;
SendMessageW(edit, EM_GETSEL, (WPARAM)&start, (LPARAM)&end);
CreateEditControl(hwnd);
SetWindowTextW(g_app.hwndEdit, text);
SendMessageW(g_app.hwndEdit, EM_SETSEL, start, end);
HeapFree(GetProcessHeap(), 0, text);
if (enabled) {
g_app.statusBeforeWrap = g_app.statusVisible;
ToggleStatusBar(hwnd, FALSE);
EnableMenuItem(GetMenu(hwnd), IDM_VIEW_STATUS_BAR, MF_BYCOMMAND | MF_GRAYED);
EnableMenuItem(GetMenu(hwnd), IDM_EDIT_GOTO, MF_BYCOMMAND | MF_GRAYED);
} else {
ToggleStatusBar(hwnd, g_app.statusBeforeWrap);
EnableMenuItem(GetMenu(hwnd), IDM_VIEW_STATUS_BAR, MF_BYCOMMAND | MF_ENABLED);
EnableMenuItem(GetMenu(hwnd), IDM_EDIT_GOTO, MF_BYCOMMAND | MF_ENABLED);
}
UpdateTitle(hwnd);
UpdateStatusBar(hwnd);
}
static void UpdateStatusBar(HWND hwnd) {
if (!g_app.statusVisible || !g_app.hwndStatus) return;
DWORD selStart = 0, selEnd = 0;
SendMessageW(g_app.hwndEdit, EM_GETSEL, (WPARAM)&selStart, (LPARAM)&selEnd);
int line = (int)SendMessageW(g_app.hwndEdit, EM_LINEFROMCHAR, selStart, 0) + 1;
int col = (int)(selStart - SendMessageW(g_app.hwndEdit, EM_LINEINDEX, line - 1, 0)) + 1;
int lines = (int)SendMessageW(g_app.hwndEdit, EM_GETLINECOUNT, 0, 0);
WCHAR status[128];
StringCchPrintfW(status, ARRAYSIZE(status), L"Ln %d, Col %d Lines: %d", line, col, lines);
SendMessageW(g_app.hwndStatus, SB_SETTEXT, 0, (LPARAM)status);
}
static void ShowFindDialog(HWND hwnd) {
if (g_app.hFindDlg) {
SetForegroundWindow(g_app.hFindDlg);
return;
}
ZeroMemory(&g_app.find, sizeof(g_app.find));
g_app.find.lStructSize = sizeof(FINDREPLACEW);
g_app.find.hwndOwner = hwnd;
g_app.find.lpstrFindWhat = g_app.findText;
g_app.find.wFindWhatLen = ARRAYSIZE(g_app.findText);
g_app.find.Flags = g_app.findFlags;
g_app.hFindDlg = FindTextW(&g_app.find);
}
static void ShowReplaceDialog(HWND hwnd) {
if (g_app.hReplaceDlg) {
SetForegroundWindow(g_app.hReplaceDlg);
return;
}
ZeroMemory(&g_app.find, sizeof(g_app.find));
g_app.find.lStructSize = sizeof(FINDREPLACEW);
g_app.find.hwndOwner = hwnd;
g_app.find.lpstrFindWhat = g_app.findText;
g_app.find.lpstrReplaceWith = g_app.replaceText;
g_app.find.wFindWhatLen = ARRAYSIZE(g_app.findText);
g_app.find.wReplaceWithLen = ARRAYSIZE(g_app.replaceText);
g_app.find.Flags = g_app.findFlags;
g_app.hReplaceDlg = ReplaceTextW(&g_app.find);
}
static BOOL DoFindNext(BOOL reverse) {
if (g_app.findText[0] == L'\0') {
ShowFindDialog(g_app.hwndMain);
return FALSE;
}
DWORD start = 0, end = 0;
SendMessageW(g_app.hwndEdit, EM_GETSEL, (WPARAM)&start, (LPARAM)&end);
BOOL matchCase = (g_app.findFlags & FR_MATCHCASE) != 0;
BOOL down = (g_app.findFlags & FR_DOWN) != 0;
if (reverse) down = !down;
DWORD searchStart = down ? end : start;
DWORD outStart = 0, outEnd = 0;
if (FindInEdit(g_app.hwndEdit, g_app.findText, matchCase, down, searchStart, &outStart, &outEnd)) {
SendMessageW(g_app.hwndEdit, EM_SETSEL, outStart, outEnd);
SendMessageW(g_app.hwndEdit, EM_SCROLLCARET, 0, 0);
return TRUE;
}
MessageBoxW(g_app.hwndMain, L"Cannot find the text.", APP_TITLE, MB_ICONINFORMATION);
return FALSE;
}
static INT_PTR CALLBACK GoToDlgProc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_INITDIALOG: {
SetDlgItemInt(dlg, IDC_GOTO_EDIT, 1, FALSE);
HWND edit = GetDlgItem(dlg, IDC_GOTO_EDIT);
SendMessageW(edit, EM_SETLIMITTEXT, 10, 0);
return TRUE;
}
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDOK: {
BOOL ok = FALSE;
UINT line = GetDlgItemInt(dlg, IDC_GOTO_EDIT, &ok, FALSE);
if (!ok || line == 0) {
MessageBoxW(dlg, L"Enter a valid line number.", APP_TITLE, MB_ICONWARNING);
return TRUE;
}
int maxLine = (int)SendMessageW(g_app.hwndEdit, EM_GETLINECOUNT, 0, 0);
if ((int)line > maxLine) line = (UINT)maxLine;
int charIndex = (int)SendMessageW(g_app.hwndEdit, EM_LINEINDEX, line - 1, 0);
if (charIndex >= 0) {
SendMessageW(g_app.hwndEdit, EM_SETSEL, charIndex, charIndex);
SendMessageW(g_app.hwndEdit, EM_SCROLLCARET, 0, 0);
}
EndDialog(dlg, IDOK);
return TRUE;
}
case IDCANCEL:
EndDialog(dlg, IDCANCEL);
return TRUE;
}
break;
}
return FALSE;
}
static void DoSelectFont(HWND hwnd) {
LOGFONTW lf = {0};
if (g_app.hFont) {
GetObjectW(g_app.hFont, sizeof(LOGFONTW), &lf);
} else {
SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(LOGFONTW), &lf, 0);
}
CHOOSEFONTW cf = {0};
cf.lStructSize = sizeof(cf);
cf.hwndOwner = hwnd;
cf.lpLogFont = &lf;
cf.Flags = CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT;
if (ChooseFontW(&cf)) {
HFONT newFont = CreateFontIndirectW(&lf);
if (newFont) {
if (g_app.hFont) DeleteObject(g_app.hFont);
g_app.hFont = newFont;
ApplyFontToEdit(g_app.hwndEdit, g_app.hFont);
UpdateLayout(hwnd);
}
}
}
static void InsertTimeDate(HWND hwnd) {
SYSTEMTIME st;
GetLocalTime(&st);
WCHAR date[64], time[64], stamp[128];
GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, NULL, date, ARRAYSIZE(date));
GetTimeFormatW(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &st, NULL, time, ARRAYSIZE(time));
StringCchPrintfW(stamp, ARRAYSIZE(stamp), L"%s %s", time, date);
SendMessageW(g_app.hwndEdit, EM_REPLACESEL, TRUE, (LPARAM)stamp);
}
static void HandleFindReplace(LPFINDREPLACE lpfr) {
if (lpfr->Flags & FR_DIALOGTERM) {
g_app.hFindDlg = NULL;
g_app.hReplaceDlg = NULL;
return;
}
g_app.findFlags = lpfr->Flags;
if (lpfr->lpstrFindWhat && lpfr->lpstrFindWhat[0]) {
StringCchCopyW(g_app.findText, ARRAYSIZE(g_app.findText), lpfr->lpstrFindWhat);
}
if (lpfr->lpstrReplaceWith) {
StringCchCopyW(g_app.replaceText, ARRAYSIZE(g_app.replaceText), lpfr->lpstrReplaceWith);
}
BOOL matchCase = (lpfr->Flags & FR_MATCHCASE) != 0;
BOOL down = (lpfr->Flags & FR_DOWN) != 0;
if (lpfr->Flags & FR_FINDNEXT) {
DWORD start = 0, end = 0;
SendMessageW(g_app.hwndEdit, EM_GETSEL, (WPARAM)&start, (LPARAM)&end);
DWORD searchStart = down ? end : start;
DWORD outStart = 0, outEnd = 0;
if (FindInEdit(g_app.hwndEdit, g_app.findText, matchCase, down, searchStart, &outStart, &outEnd)) {
SendMessageW(g_app.hwndEdit, EM_SETSEL, outStart, outEnd);
SendMessageW(g_app.hwndEdit, EM_SCROLLCARET, 0, 0);
} else {
MessageBoxW(g_app.hwndMain, L"Cannot find the text.", APP_TITLE, MB_ICONINFORMATION);
}
} else if (lpfr->Flags & FR_REPLACE) {
DWORD start = 0, end = 0;
SendMessageW(g_app.hwndEdit, EM_GETSEL, (WPARAM)&start, (LPARAM)&end);
DWORD outStart = 0, outEnd = 0;
if (FindInEdit(g_app.hwndEdit, g_app.findText, matchCase, down, start, &outStart, &outEnd)) {
SendMessageW(g_app.hwndEdit, EM_SETSEL, outStart, outEnd);
SendMessageW(g_app.hwndEdit, EM_REPLACESEL, TRUE, (LPARAM)g_app.replaceText);
SendMessageW(g_app.hwndEdit, EM_SCROLLCARET, 0, 0);
g_app.modified = TRUE;
UpdateTitle(g_app.hwndMain);
} else {
MessageBoxW(g_app.hwndMain, L"Cannot find the text.", APP_TITLE, MB_ICONINFORMATION);
}
} else if (lpfr->Flags & FR_REPLACEALL) {
int replaced = ReplaceAllOccurrences(g_app.hwndEdit, g_app.findText, g_app.replaceText, matchCase);
WCHAR msg[64];
StringCchPrintfW(msg, ARRAYSIZE(msg), L"Replaced %d occurrence%s.", replaced, replaced == 1 ? L"" : L"s");
MessageBoxW(g_app.hwndMain, msg, APP_TITLE, MB_OK | MB_ICONINFORMATION);
}
}
static void UpdateMenuStates(HWND hwnd) {
HMENU menu = GetMenu(hwnd);
if (!menu) return;
UINT wrapState = g_app.wordWrap ? MF_CHECKED : MF_UNCHECKED;
UINT statusState = g_app.statusVisible ? MF_CHECKED : MF_UNCHECKED;
CheckMenuItem(menu, IDM_FORMAT_WORD_WRAP, MF_BYCOMMAND | wrapState);
CheckMenuItem(menu, IDM_VIEW_STATUS_BAR, MF_BYCOMMAND | statusState);
BOOL canGoTo = !g_app.wordWrap;
EnableMenuItem(menu, IDM_EDIT_GOTO, MF_BYCOMMAND | (canGoTo ? MF_ENABLED : MF_GRAYED));
if (g_app.wordWrap) {
EnableMenuItem(menu, IDM_VIEW_STATUS_BAR, MF_BYCOMMAND | MF_GRAYED);
} else {
EnableMenuItem(menu, IDM_VIEW_STATUS_BAR, MF_BYCOMMAND | MF_ENABLED);
}
BOOL modified = (SendMessageW(g_app.hwndEdit, EM_GETMODIFY, 0, 0) != 0);
EnableMenuItem(menu, IDM_FILE_SAVE, MF_BYCOMMAND | (modified ? MF_ENABLED : MF_GRAYED));
}
static void HandleCommand(HWND hwnd, WPARAM wParam, LPARAM lParam) {
switch (LOWORD(wParam)) {
case IDM_FILE_NEW:
DoFileNew(hwnd);
break;
case IDM_FILE_OPEN:
DoFileOpen(hwnd);
break;
case IDM_FILE_SAVE:
DoFileSave(hwnd, FALSE);
break;
case IDM_FILE_SAVE_AS:
DoFileSave(hwnd, TRUE);
break;
case IDM_FILE_PAGE_SETUP:
case IDM_FILE_PRINT:
MessageBoxW(hwnd, L"Printing is not implemented in retropad.", APP_TITLE, MB_ICONINFORMATION);
break;
case IDM_FILE_EXIT:
PostMessageW(hwnd, WM_CLOSE, 0, 0);
break;
case IDM_EDIT_UNDO:
SendMessageW(g_app.hwndEdit, EM_UNDO, 0, 0);
break;
case IDM_EDIT_CUT:
SendMessageW(g_app.hwndEdit, WM_CUT, 0, 0);
break;
case IDM_EDIT_COPY:
SendMessageW(g_app.hwndEdit, WM_COPY, 0, 0);
break;
case IDM_EDIT_PASTE:
SendMessageW(g_app.hwndEdit, WM_PASTE, 0, 0);
break;
case IDM_EDIT_DELETE:
SendMessageW(g_app.hwndEdit, WM_CLEAR, 0, 0);
break;
case IDM_EDIT_FIND:
ShowFindDialog(hwnd);
break;
case IDM_EDIT_FIND_NEXT:
DoFindNext(FALSE);
break;
case IDM_EDIT_REPLACE:
ShowReplaceDialog(hwnd);
break;
case IDM_EDIT_GOTO:
if (g_app.wordWrap) {
MessageBoxW(hwnd, L"Go To is unavailable when Word Wrap is on.", APP_TITLE, MB_ICONINFORMATION);
} else {
DialogBoxW(g_hInst, MAKEINTRESOURCE(IDD_GOTO), hwnd, GoToDlgProc);
}
break;
case IDM_EDIT_SELECT_ALL:
SendMessageW(g_app.hwndEdit, EM_SETSEL, 0, -1);
break;
case IDM_EDIT_TIME_DATE:
InsertTimeDate(hwnd);
break;
case IDM_FORMAT_WORD_WRAP:
SetWordWrap(hwnd, !g_app.wordWrap);
break;
case IDM_FORMAT_FONT:
DoSelectFont(hwnd);
break;
case IDM_VIEW_STATUS_BAR:
ToggleStatusBar(hwnd, !g_app.statusVisible);
break;
case IDM_HELP_VIEW_HELP:
MessageBoxW(hwnd, L"No help file is available for retropad.", APP_TITLE, MB_ICONINFORMATION);
break;
case IDM_HELP_ABOUT:
DialogBoxW(g_hInst, MAKEINTRESOURCE(IDD_ABOUT), hwnd, AboutDlgProc);
break;
}
}
static INT_PTR CALLBACK AboutDlgProc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
EndDialog(dlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
static LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
if (msg == g_findMsg) {
HandleFindReplace((LPFINDREPLACE)lParam);
return 0;
}
switch (msg) {
case WM_CREATE: {
INITCOMMONCONTROLSEX icc = { sizeof(icc), ICC_BAR_CLASSES };
InitCommonControlsEx(&icc);
CreateEditControl(hwnd);
ToggleStatusBar(hwnd, TRUE);
UpdateTitle(hwnd);
UpdateStatusBar(hwnd);
DragAcceptFiles(hwnd, TRUE);
return 0;
}
case WM_SETFOCUS:
if (g_app.hwndEdit) SetFocus(g_app.hwndEdit);
return 0;
case WM_SIZE:
UpdateLayout(hwnd);
UpdateStatusBar(hwnd);
return 0;
case WM_DROPFILES: {
HDROP hDrop = (HDROP)wParam;
WCHAR path[MAX_PATH_BUFFER];
if (DragQueryFileW(hDrop, 0, path, ARRAYSIZE(path))) {
if (PromptSaveChanges(hwnd)) {
LoadDocumentFromPath(hwnd, path);
}
}
DragFinish(hDrop);
return 0;
}
case WM_COMMAND:
if (HIWORD(wParam) == EN_CHANGE && (HWND)lParam == g_app.hwndEdit) {
g_app.modified = (SendMessageW(g_app.hwndEdit, EM_GETMODIFY, 0, 0) != 0);
UpdateTitle(hwnd);
UpdateStatusBar(hwnd);
return 0;
} else if (HIWORD(wParam) == EN_UPDATE && (HWND)lParam == g_app.hwndEdit) {
UpdateStatusBar(hwnd);
return 0;
}
HandleCommand(hwnd, wParam, lParam);
return 0;
case WM_INITMENUPOPUP:
UpdateMenuStates(hwnd);
return 0;
case WM_CLOSE:
if (PromptSaveChanges(hwnd)) {
DestroyWindow(hwnd);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProcW(hwnd, msg, wParam, lParam);
}
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) {
(void)hPrevInstance;
(void)lpCmdLine;
g_hInst = hInstance;
g_findMsg = RegisterWindowMessageW(FINDMSGSTRINGW);
g_app.wordWrap = FALSE;
g_app.statusVisible = TRUE;
g_app.statusBeforeWrap = TRUE;
g_app.encoding = ENC_UTF8;
g_app.findFlags = FR_DOWN;
WNDCLASSEXW wc = {0};
wc.cbSize = sizeof(wc);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = MainWndProc;
wc.hInstance = hInstance;
wc.hIcon = LoadIconW(hInstance, MAKEINTRESOURCE(IDI_RETROPAD));
wc.hIconSm = wc.hIcon;
wc.hCursor = LoadCursorW(NULL, IDC_IBEAM);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = L"RETROPAD_WINDOW";
wc.lpszMenuName = MAKEINTRESOURCE(IDC_RETROPAD);
if (!RegisterClassExW(&wc)) {
MessageBoxW(NULL, L"Failed to register window class.", APP_TITLE, MB_ICONERROR);
return 0;
}
HWND hwnd = CreateWindowExW(0, wc.lpszClassName, APP_TITLE, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, DEFAULT_WIDTH, DEFAULT_HEIGHT,
NULL, NULL, hInstance, NULL);
if (!hwnd) {
MessageBoxW(NULL, L"Failed to create main window.", APP_TITLE, MB_ICONERROR);
return 0;
}
g_app.hwndMain = hwnd;
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
HACCEL accel = LoadAcceleratorsW(hInstance, MAKEINTRESOURCE(IDC_RETROPAD));
MSG msg;
while (GetMessageW(&msg, NULL, 0, 0)) {
if (!accel || !TranslateAcceleratorW(hwnd, accel, &msg)) {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
}
return (int)msg.wParam;
}