-
Notifications
You must be signed in to change notification settings - Fork 1
/
dialog.c
962 lines (788 loc) · 27 KB
/
dialog.c
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
/*
* PROJECT: ReactOS Notepad
* LICENSE: LGPL-2.1-or-later (https://spdx.org/licenses/LGPL-2.1-or-later)
* PURPOSE: Providing a Windows-compatible simple text editor for ReactOS
* COPYRIGHT: Copyright 1998,99 Marcel Baur <[email protected]>
* Copyright 2002 Sylvain Petreolle <[email protected]>
* Copyright 2002 Andriy Palamarchuk
* Copyright 2023 Katayama Hirofumi MZ <[email protected]>
*/
#include "notepad.h"
#include <assert.h>
#include <commctrl.h>
#include <strsafe.h>
LRESULT CALLBACK EDIT_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
static const TCHAR helpfile[] = _T("notepad.hlp");
static const TCHAR empty_str[] = _T("");
static const TCHAR szDefaultExt[] = _T("txt");
static const TCHAR txt_files[] = _T("*.txt");
/* Status bar parts index */
#define SBPART_CURPOS 0
#define SBPART_EOLN 1
#define SBPART_ENCODING 2
/* Line endings - string resource ID mapping table */
static UINT EolnToStrId[] = {
STRING_CRLF,
STRING_LF,
STRING_CR
};
/* Encoding - string resource ID mapping table */
static UINT EncToStrId[] = {
STRING_ANSI,
STRING_UNICODE,
STRING_UNICODE_BE,
STRING_UTF8,
STRING_UTF8_BOM
};
VOID ShowLastError(VOID)
{
DWORD error = GetLastError();
if (error != NO_ERROR)
{
LPTSTR lpMsgBuf = NULL;
TCHAR szTitle[MAX_STRING_LEN];
LoadString(Globals.hInstance, STRING_ERROR, szTitle, _countof(szTitle));
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
error,
0,
(LPTSTR) &lpMsgBuf,
0,
NULL);
MessageBox(Globals.hMainWnd, lpMsgBuf, szTitle, MB_OK | MB_ICONERROR);
LocalFree(lpMsgBuf);
}
}
/**
* Sets the caption of the main window according to Globals.szFileTitle:
* (untitled) - Notepad if no file is open
* [filename] - Notepad if a file is given
*/
void UpdateWindowCaption(BOOL clearModifyAlert)
{
TCHAR szCaption[MAX_STRING_LEN];
TCHAR szNotepad[MAX_STRING_LEN];
TCHAR szFilename[MAX_STRING_LEN];
BOOL isModified;
if (clearModifyAlert)
{
/* When a file is being opened or created, there is no need to have
* the edited flag shown when the file has not been edited yet. */
isModified = FALSE;
}
else
{
/* Check whether the user has modified the file or not. If we are
* in the same state as before, don't change the caption. */
isModified = !!SendMessage(Globals.hEdit, EM_GETMODIFY, 0, 0);
if (isModified == Globals.bWasModified)
return;
}
/* Remember the state for later calls */
Globals.bWasModified = isModified;
/* Load the name of the application */
LoadString(Globals.hInstance, STRING_NOTEPAD, szNotepad, _countof(szNotepad));
/* Determine if the file has been saved or if this is a new file */
if (Globals.szFileTitle[0] != 0)
StringCchCopy(szFilename, _countof(szFilename), Globals.szFileTitle);
else
LoadString(Globals.hInstance, STRING_UNTITLED, szFilename, _countof(szFilename));
/* Update the window caption based upon whether the user has modified the file or not */
StringCbPrintf(szCaption, sizeof(szCaption), _T("%s%s - %s"),
(isModified ? _T("*") : _T("")), szFilename, szNotepad);
SetWindowText(Globals.hMainWnd, szCaption);
}
VOID WaitCursor(BOOL bBegin)
{
static HCURSOR s_hWaitCursor = NULL;
static HCURSOR s_hOldCursor = NULL;
static INT s_nLock = 0;
if (bBegin)
{
if (s_nLock++ == 0)
{
if (s_hWaitCursor == NULL)
s_hWaitCursor = LoadCursor(NULL, IDC_WAIT);
s_hOldCursor = SetCursor(s_hWaitCursor);
}
else
{
SetCursor(s_hWaitCursor);
}
}
else
{
if (--s_nLock == 0)
SetCursor(s_hOldCursor);
}
}
VOID DIALOG_StatusBarAlignParts(VOID)
{
static const int defaultWidths[] = {120, 120, 120};
RECT rcStatusBar;
int parts[3];
GetClientRect(Globals.hStatusBar, &rcStatusBar);
parts[0] = rcStatusBar.right - (defaultWidths[1] + defaultWidths[2]);
parts[1] = rcStatusBar.right - defaultWidths[2];
parts[2] = -1; // the right edge of the status bar
parts[0] = max(parts[0], defaultWidths[0]);
parts[1] = max(parts[1], defaultWidths[0] + defaultWidths[1]);
SendMessageW(Globals.hStatusBar, SB_SETPARTS, _countof(parts), (LPARAM)parts);
}
static VOID DIALOG_StatusBarUpdateLineEndings(VOID)
{
WCHAR szText[128];
LoadStringW(Globals.hInstance, EolnToStrId[Globals.iEoln], szText, _countof(szText));
SendMessageW(Globals.hStatusBar, SB_SETTEXTW, SBPART_EOLN, (LPARAM)szText);
}
static VOID DIALOG_StatusBarUpdateEncoding(VOID)
{
WCHAR szText[128] = L"";
if (Globals.encFile != ENCODING_AUTO)
{
LoadStringW(Globals.hInstance, EncToStrId[Globals.encFile], szText, _countof(szText));
}
SendMessageW(Globals.hStatusBar, SB_SETTEXTW, SBPART_ENCODING, (LPARAM)szText);
}
static VOID DIALOG_StatusBarUpdateAll(VOID)
{
DIALOG_StatusBarUpdateCaretPos();
DIALOG_StatusBarUpdateLineEndings();
DIALOG_StatusBarUpdateEncoding();
}
int DIALOG_StringMsgBox(HWND hParent, int formatId, LPCTSTR szString, DWORD dwFlags)
{
TCHAR szMessage[MAX_STRING_LEN];
TCHAR szResource[MAX_STRING_LEN];
/* Load and format szMessage */
LoadString(Globals.hInstance, formatId, szResource, _countof(szResource));
StringCchPrintf(szMessage, _countof(szMessage), szResource, szString);
/* Load szCaption */
if ((dwFlags & MB_ICONMASK) == MB_ICONEXCLAMATION)
LoadString(Globals.hInstance, STRING_ERROR, szResource, _countof(szResource));
else
LoadString(Globals.hInstance, STRING_NOTEPAD, szResource, _countof(szResource));
/* Display Modal Dialog */
// if (hParent == NULL)
// hParent = Globals.hMainWnd;
return MessageBox(hParent, szMessage, szResource, dwFlags);
}
static void AlertFileNotFound(LPCTSTR szFileName)
{
DIALOG_StringMsgBox(Globals.hMainWnd, STRING_NOTFOUND, szFileName, MB_ICONEXCLAMATION | MB_OK);
}
static int AlertFileNotSaved(LPCTSTR szFileName)
{
TCHAR szUntitled[MAX_STRING_LEN];
LoadString(Globals.hInstance, STRING_UNTITLED, szUntitled, _countof(szUntitled));
return DIALOG_StringMsgBox(Globals.hMainWnd, STRING_NOTSAVED,
szFileName[0] ? szFileName : szUntitled,
MB_ICONQUESTION | MB_YESNOCANCEL);
}
/**
* Returns:
* TRUE - if file exists
* FALSE - if file does not exist
*/
BOOL FileExists(LPCTSTR szFilename)
{
return GetFileAttributes(szFilename) != INVALID_FILE_ATTRIBUTES;
}
BOOL HasFileExtension(LPCTSTR szFilename)
{
LPCTSTR s;
s = _tcsrchr(szFilename, _T('\\'));
if (s)
szFilename = s;
return _tcsrchr(szFilename, _T('.')) != NULL;
}
static BOOL DoSaveFile(VOID)
{
BOOL bRet = FALSE;
HANDLE hFile;
DWORD cchText;
WaitCursor(TRUE);
/* Use OPEN_ALWAYS instead of CREATE_ALWAYS in order to succeed
* even if the file has HIDDEN or SYSTEM attributes */
hFile = CreateFileW(Globals.szFileName, GENERIC_WRITE, FILE_SHARE_READ,
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
ShowLastError();
WaitCursor(FALSE);
return FALSE;
}
cchText = GetWindowTextLengthW(Globals.hEdit);
if (cchText <= 0)
{
bRet = TRUE;
}
else
{
HLOCAL hLocal = (HLOCAL)SendMessageW(Globals.hEdit, EM_GETHANDLE, 0, 0);
LPWSTR pszText = LocalLock(hLocal);
if (pszText)
{
bRet = WriteText(hFile, pszText, cchText, Globals.encFile, Globals.iEoln);
if (!bRet)
ShowLastError();
LocalUnlock(hLocal);
}
else
{
ShowLastError();
}
}
/* Truncate the file and close it */
SetEndOfFile(hFile);
CloseHandle(hFile);
if (bRet)
{
SendMessage(Globals.hEdit, EM_SETMODIFY, FALSE, 0);
SetFileName(Globals.szFileName);
}
WaitCursor(FALSE);
return bRet;
}
/**
* Returns:
* TRUE - User agreed to close (both save/don't save)
* FALSE - User cancelled close by selecting "Cancel"
*/
BOOL DoCloseFile(VOID)
{
int nResult;
if (SendMessage(Globals.hEdit, EM_GETMODIFY, 0, 0))
{
/* prompt user to save changes */
nResult = AlertFileNotSaved(Globals.szFileName);
switch (nResult)
{
case IDYES:
if(!DIALOG_FileSave())
return FALSE;
break;
case IDNO:
break;
case IDCANCEL:
default:
return FALSE;
}
}
SetFileName(empty_str);
UpdateWindowCaption(TRUE);
return TRUE;
}
VOID DoOpenFile(LPCTSTR szFileName)
{
HANDLE hFile;
TCHAR log[5];
HLOCAL hLocal;
/* Close any files and prompt to save changes */
if (!DoCloseFile())
return;
WaitCursor(TRUE);
hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
ShowLastError();
goto done;
}
/* To make loading file quicker, we use the internal handle of EDIT control */
hLocal = (HLOCAL)SendMessageW(Globals.hEdit, EM_GETHANDLE, 0, 0);
if (!ReadText(hFile, &hLocal, &Globals.encFile, &Globals.iEoln))
{
ShowLastError();
goto done;
}
SendMessageW(Globals.hEdit, EM_SETHANDLE, (WPARAM)hLocal, 0);
/* No need of EM_SETMODIFY and EM_EMPTYUNDOBUFFER here. EM_SETHANDLE does instead. */
SetFocus(Globals.hEdit);
/* If the file starts with .LOG, add a time/date at the end and set cursor after
* See http://web.archive.org/web/20090627165105/http://support.microsoft.com/kb/260563
*/
if (GetWindowText(Globals.hEdit, log, _countof(log)) && !_tcscmp(log, _T(".LOG")))
{
static const TCHAR lf[] = _T("\r\n");
SendMessage(Globals.hEdit, EM_SETSEL, GetWindowTextLength(Globals.hEdit), -1);
SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lf);
DIALOG_EditTimeDate();
SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lf);
}
SetFileName(szFileName);
UpdateWindowCaption(TRUE);
NOTEPAD_EnableSearchMenu();
DIALOG_StatusBarUpdateAll();
done:
if (hFile != INVALID_HANDLE_VALUE)
CloseHandle(hFile);
WaitCursor(FALSE);
}
VOID DIALOG_FileNew(VOID)
{
/* Close any files and prompt to save changes */
if (!DoCloseFile())
return;
WaitCursor(TRUE);
SetWindowText(Globals.hEdit, NULL);
SendMessage(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0);
Globals.iEoln = EOLN_CRLF;
Globals.encFile = ENCODING_DEFAULT;
NOTEPAD_EnableSearchMenu();
DIALOG_StatusBarUpdateAll();
WaitCursor(FALSE);
}
VOID DIALOG_FileNewWindow(VOID)
{
TCHAR pszNotepadExe[MAX_PATH];
WaitCursor(TRUE);
GetModuleFileName(NULL, pszNotepadExe, _countof(pszNotepadExe));
ShellExecute(NULL, NULL, pszNotepadExe, NULL, NULL, SW_SHOWNORMAL);
WaitCursor(FALSE);
}
VOID DIALOG_FileOpen(VOID)
{
OPENFILENAME openfilename;
TCHAR szPath[MAX_PATH];
ZeroMemory(&openfilename, sizeof(openfilename));
if (Globals.szFileName[0] == 0)
_tcscpy(szPath, txt_files);
else
_tcscpy(szPath, Globals.szFileName);
openfilename.lStructSize = sizeof(openfilename);
openfilename.hwndOwner = Globals.hMainWnd;
openfilename.hInstance = Globals.hInstance;
openfilename.lpstrFilter = Globals.szFilter;
openfilename.lpstrFile = szPath;
openfilename.nMaxFile = _countof(szPath);
openfilename.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;
openfilename.lpstrDefExt = szDefaultExt;
if (GetOpenFileName(&openfilename)) {
if (FileExists(openfilename.lpstrFile))
DoOpenFile(openfilename.lpstrFile);
else
AlertFileNotFound(openfilename.lpstrFile);
}
}
BOOL DIALOG_FileSave(VOID)
{
if (Globals.szFileName[0] == 0)
{
return DIALOG_FileSaveAs();
}
else if (DoSaveFile())
{
UpdateWindowCaption(TRUE);
return TRUE;
}
return FALSE;
}
static UINT_PTR
CALLBACK
DIALOG_FileSaveAs_Hook(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
TCHAR szText[128];
HWND hCombo;
UNREFERENCED_PARAMETER(wParam);
switch(msg)
{
case WM_INITDIALOG:
hCombo = GetDlgItem(hDlg, ID_ENCODING);
LoadString(Globals.hInstance, STRING_ANSI, szText, _countof(szText));
SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM) szText);
LoadString(Globals.hInstance, STRING_UNICODE, szText, _countof(szText));
SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM) szText);
LoadString(Globals.hInstance, STRING_UNICODE_BE, szText, _countof(szText));
SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM) szText);
LoadString(Globals.hInstance, STRING_UTF8, szText, _countof(szText));
SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM) szText);
LoadString(Globals.hInstance, STRING_UTF8_BOM, szText, _countof(szText));
SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM) szText);
SendMessage(hCombo, CB_SETCURSEL, Globals.encFile, 0);
hCombo = GetDlgItem(hDlg, ID_EOLN);
LoadString(Globals.hInstance, STRING_CRLF, szText, _countof(szText));
SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM) szText);
LoadString(Globals.hInstance, STRING_LF, szText, _countof(szText));
SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM) szText);
LoadString(Globals.hInstance, STRING_CR, szText, _countof(szText));
SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM) szText);
SendMessage(hCombo, CB_SETCURSEL, Globals.iEoln, 0);
break;
case WM_NOTIFY:
if (((NMHDR *) lParam)->code == CDN_FILEOK)
{
hCombo = GetDlgItem(hDlg, ID_ENCODING);
if (hCombo)
Globals.encFile = (ENCODING) SendMessage(hCombo, CB_GETCURSEL, 0, 0);
hCombo = GetDlgItem(hDlg, ID_EOLN);
if (hCombo)
Globals.iEoln = (EOLN)SendMessage(hCombo, CB_GETCURSEL, 0, 0);
}
break;
}
return 0;
}
BOOL DIALOG_FileSaveAs(VOID)
{
OPENFILENAME saveas;
TCHAR szPath[MAX_PATH];
ZeroMemory(&saveas, sizeof(saveas));
if (Globals.szFileName[0] == 0)
_tcscpy(szPath, txt_files);
else
_tcscpy(szPath, Globals.szFileName);
saveas.lStructSize = sizeof(OPENFILENAME);
saveas.hwndOwner = Globals.hMainWnd;
saveas.hInstance = Globals.hInstance;
saveas.lpstrFilter = Globals.szFilter;
saveas.lpstrFile = szPath;
saveas.nMaxFile = _countof(szPath);
saveas.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY |
OFN_EXPLORER | OFN_ENABLETEMPLATE | OFN_ENABLEHOOK;
saveas.lpstrDefExt = szDefaultExt;
saveas.lpTemplateName = MAKEINTRESOURCE(DIALOG_ENCODING);
saveas.lpfnHook = DIALOG_FileSaveAs_Hook;
if (GetSaveFileName(&saveas))
{
/* HACK: Because in ROS, Save-As boxes don't check the validity
* of file names and thus, here, szPath can be invalid !! We only
* see its validity when we call DoSaveFile()... */
SetFileName(szPath);
if (DoSaveFile())
{
UpdateWindowCaption(TRUE);
DIALOG_StatusBarUpdateAll();
return TRUE;
}
else
{
SetFileName(_T(""));
return FALSE;
}
}
else
{
return FALSE;
}
}
VOID DIALOG_FileExit(VOID)
{
PostMessage(Globals.hMainWnd, WM_CLOSE, 0, 0);
}
VOID DIALOG_EditUndo(VOID)
{
SendMessage(Globals.hEdit, EM_UNDO, 0, 0);
}
VOID DIALOG_EditCut(VOID)
{
SendMessage(Globals.hEdit, WM_CUT, 0, 0);
}
VOID DIALOG_EditCopy(VOID)
{
SendMessage(Globals.hEdit, WM_COPY, 0, 0);
}
VOID DIALOG_EditPaste(VOID)
{
SendMessage(Globals.hEdit, WM_PASTE, 0, 0);
}
VOID DIALOG_EditDelete(VOID)
{
SendMessage(Globals.hEdit, WM_CLEAR, 0, 0);
}
VOID DIALOG_EditSelectAll(VOID)
{
SendMessage(Globals.hEdit, EM_SETSEL, 0, -1);
}
VOID DIALOG_EditTimeDate(VOID)
{
SYSTEMTIME st;
TCHAR szDate[MAX_STRING_LEN];
TCHAR szText[MAX_STRING_LEN * 2 + 2];
GetLocalTime(&st);
GetTimeFormat(LOCALE_USER_DEFAULT, 0, &st, NULL, szDate, MAX_STRING_LEN);
_tcscpy(szText, szDate);
_tcscat(szText, _T(" "));
GetDateFormat(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, NULL, szDate, MAX_STRING_LEN);
_tcscat(szText, szDate);
SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)szText);
}
VOID DoShowHideStatusBar(VOID)
{
/* Check if status bar object already exists. */
if (Globals.bShowStatusBar && Globals.hStatusBar == NULL)
{
/* Try to create the status bar */
Globals.hStatusBar = CreateStatusWindow(WS_CHILD | CCS_BOTTOM | SBARS_SIZEGRIP,
NULL,
Globals.hMainWnd,
CMD_STATUSBAR_WND_ID);
if (Globals.hStatusBar == NULL)
{
ShowLastError();
return;
}
/* Load the string for formatting column/row text output */
LoadString(Globals.hInstance, STRING_LINE_COLUMN, Globals.szStatusBarLineCol, MAX_PATH - 1);
}
/* Update layout of controls */
SendMessageW(Globals.hMainWnd, WM_SIZE, 0, 0);
if (Globals.hStatusBar == NULL)
return;
/* Update visibility of status bar */
ShowWindow(Globals.hStatusBar, (Globals.bShowStatusBar ? SW_SHOWNOACTIVATE : SW_HIDE));
/* Update status bar contents */
DIALOG_StatusBarUpdateAll();
}
VOID DoCreateEditWindow(VOID)
{
DWORD dwStyle;
int iSize;
LPTSTR pTemp = NULL;
BOOL bModified = FALSE;
iSize = 0;
/* If the edit control already exists, try to save its content */
if (Globals.hEdit != NULL)
{
/* number of chars currently written into the editor. */
iSize = GetWindowTextLength(Globals.hEdit);
if (iSize)
{
/* Allocates temporary buffer. */
pTemp = HeapAlloc(GetProcessHeap(), 0, (iSize + 1) * sizeof(TCHAR));
if (!pTemp)
{
ShowLastError();
return;
}
/* Recover the text into the control. */
GetWindowText(Globals.hEdit, pTemp, iSize + 1);
if (SendMessage(Globals.hEdit, EM_GETMODIFY, 0, 0))
bModified = TRUE;
}
/* Restore original window procedure */
SetWindowLongPtr(Globals.hEdit, GWLP_WNDPROC, (LONG_PTR)Globals.EditProc);
/* Destroy the edit control */
DestroyWindow(Globals.hEdit);
}
/* Update wrap status into the main menu and recover style flags */
dwStyle = (Globals.bWrapLongLines ? EDIT_STYLE_WRAP : EDIT_STYLE);
/* Create the new edit control */
Globals.hEdit = CreateWindowEx(WS_EX_CLIENTEDGE,
EDIT_CLASS,
NULL,
dwStyle,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
Globals.hMainWnd,
NULL,
Globals.hInstance,
NULL);
if (Globals.hEdit == NULL)
{
if (pTemp)
{
HeapFree(GetProcessHeap(), 0, pTemp);
}
ShowLastError();
return;
}
SendMessage(Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, FALSE);
SendMessage(Globals.hEdit, EM_LIMITTEXT, 0, 0);
/* If some text was previously saved, restore it. */
if (iSize != 0)
{
SetWindowText(Globals.hEdit, pTemp);
HeapFree(GetProcessHeap(), 0, pTemp);
if (bModified)
SendMessage(Globals.hEdit, EM_SETMODIFY, TRUE, 0);
}
/* Sub-class a new window callback for row/column detection. */
Globals.EditProc = (WNDPROC)SetWindowLongPtr(Globals.hEdit,
GWLP_WNDPROC,
(LONG_PTR)EDIT_WndProc);
/* Finally shows new edit control and set focus into it. */
ShowWindow(Globals.hEdit, SW_SHOW);
SetFocus(Globals.hEdit);
/* Re-arrange controls */
PostMessageW(Globals.hMainWnd, WM_SIZE, 0, 0);
}
VOID DIALOG_EditWrap(VOID)
{
Globals.bWrapLongLines = !Globals.bWrapLongLines;
EnableMenuItem(Globals.hMenu, CMD_GOTO, (Globals.bWrapLongLines ? MF_GRAYED : MF_ENABLED));
DoCreateEditWindow();
DoShowHideStatusBar();
}
VOID DIALOG_SelectFont(VOID)
{
CHOOSEFONT cf;
LOGFONT lf = Globals.lfFont;
ZeroMemory( &cf, sizeof(cf) );
cf.lStructSize = sizeof(cf);
cf.hwndOwner = Globals.hMainWnd;
cf.lpLogFont = &lf;
cf.Flags = CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT | CF_NOVERTFONTS;
if (ChooseFont(&cf))
{
HFONT currfont = Globals.hFont;
Globals.hFont = CreateFontIndirect(&lf);
Globals.lfFont = lf;
SendMessage(Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, TRUE);
if (currfont != NULL)
DeleteObject(currfont);
}
}
typedef HWND (WINAPI *FINDPROC)(LPFINDREPLACE lpfr);
static VOID DIALOG_SearchDialog(FINDPROC pfnProc)
{
if (Globals.hFindReplaceDlg != NULL)
{
SetFocus(Globals.hFindReplaceDlg);
return;
}
if (!Globals.find.lpstrFindWhat)
{
ZeroMemory(&Globals.find, sizeof(Globals.find));
Globals.find.lStructSize = sizeof(Globals.find);
Globals.find.hwndOwner = Globals.hMainWnd;
Globals.find.lpstrFindWhat = Globals.szFindText;
Globals.find.wFindWhatLen = _countof(Globals.szFindText);
Globals.find.lpstrReplaceWith = Globals.szReplaceText;
Globals.find.wReplaceWithLen = _countof(Globals.szReplaceText);
Globals.find.Flags = FR_DOWN;
}
/* We only need to create the modal FindReplace dialog which will */
/* notify us of incoming events using hMainWnd Window Messages */
Globals.hFindReplaceDlg = pfnProc(&Globals.find);
assert(Globals.hFindReplaceDlg != NULL);
}
VOID DIALOG_Search(VOID)
{
DIALOG_SearchDialog(FindText);
}
VOID DIALOG_SearchNext(BOOL bDown)
{
if (bDown)
Globals.find.Flags |= FR_DOWN;
else
Globals.find.Flags &= ~FR_DOWN;
if (Globals.find.lpstrFindWhat != NULL)
NOTEPAD_FindNext(&Globals.find, FALSE, TRUE);
else
DIALOG_Search();
}
VOID DIALOG_Replace(VOID)
{
DIALOG_SearchDialog(ReplaceText);
}
typedef struct tagGOTO_DATA
{
UINT iLine;
UINT cLines;
} GOTO_DATA, *PGOTO_DATA;
static INT_PTR
CALLBACK
DIALOG_GoTo_DialogProc(HWND hwndDialog, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static PGOTO_DATA s_pGotoData;
switch (uMsg)
{
case WM_INITDIALOG:
s_pGotoData = (PGOTO_DATA)lParam;
SetDlgItemInt(hwndDialog, ID_LINENUMBER, s_pGotoData->iLine, FALSE);
return TRUE; /* Set focus */
case WM_COMMAND:
{
if (LOWORD(wParam) == IDOK)
{
UINT iLine = GetDlgItemInt(hwndDialog, ID_LINENUMBER, NULL, FALSE);
if (iLine <= 0 || s_pGotoData->cLines < iLine) /* Out of range */
{
/* Show error message */
WCHAR title[128], text[256];
LoadStringW(Globals.hInstance, STRING_NOTEPAD, title, _countof(title));
LoadStringW(Globals.hInstance, STRING_LINE_NUMBER_OUT_OF_RANGE, text, _countof(text));
MessageBoxW(hwndDialog, text, title, MB_OK);
SendDlgItemMessageW(hwndDialog, ID_LINENUMBER, EM_SETSEL, 0, -1);
SetFocus(GetDlgItem(hwndDialog, ID_LINENUMBER));
break;
}
s_pGotoData->iLine = iLine;
EndDialog(hwndDialog, IDOK);
}
else if (LOWORD(wParam) == IDCANCEL)
{
EndDialog(hwndDialog, IDCANCEL);
}
break;
}
}
return 0;
}
VOID DIALOG_GoTo(VOID)
{
GOTO_DATA GotoData;
DWORD dwStart = 0, dwEnd = 0;
INT ich, cch = GetWindowTextLength(Globals.hEdit);
/* Get the current line number and the total line number */
SendMessage(Globals.hEdit, EM_GETSEL, (WPARAM) &dwStart, (LPARAM) &dwEnd);
GotoData.iLine = (UINT)SendMessage(Globals.hEdit, EM_LINEFROMCHAR, dwStart, 0) + 1;
GotoData.cLines = (UINT)SendMessage(Globals.hEdit, EM_GETLINECOUNT, 0, 0);
/* Ask the user for line number */
if (DialogBoxParam(Globals.hInstance,
MAKEINTRESOURCE(DIALOG_GOTO),
Globals.hMainWnd,
DIALOG_GoTo_DialogProc,
(LPARAM)&GotoData) != IDOK)
{
return; /* Canceled */
}
--GotoData.iLine; /* Make it zero-based */
/* Get ich (the target character index) from line number */
if (GotoData.iLine <= 0)
ich = 0;
else if (GotoData.iLine >= GotoData.cLines)
ich = cch;
else
ich = (INT)SendMessage(Globals.hEdit, EM_LINEINDEX, GotoData.iLine, 0);
/* EM_LINEINDEX can return -1 on failure */
if (ich < 0)
ich = 0;
/* Move the caret */
SendMessage(Globals.hEdit, EM_SETSEL, ich, ich);
SendMessage(Globals.hEdit, EM_SCROLLCARET, 0, 0);
}
VOID DIALOG_StatusBarUpdateCaretPos(VOID)
{
int line, ich, col;
TCHAR buff[MAX_PATH];
DWORD dwStart, dwSize;
SendMessage(Globals.hEdit, EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwSize);
line = SendMessage(Globals.hEdit, EM_LINEFROMCHAR, (WPARAM)dwStart, 0);
ich = (int)SendMessage(Globals.hEdit, EM_LINEINDEX, (WPARAM)line, 0);
/* EM_LINEINDEX can return -1 on failure */
col = ((ich < 0) ? 0 : (dwStart - ich));
StringCchPrintf(buff, _countof(buff), Globals.szStatusBarLineCol, line + 1, col + 1);
SendMessage(Globals.hStatusBar, SB_SETTEXT, SBPART_CURPOS, (LPARAM)buff);
}
VOID DIALOG_ViewStatusBar(VOID)
{
Globals.bShowStatusBar = !Globals.bShowStatusBar;
DoShowHideStatusBar();
}
VOID DIALOG_HelpContents(VOID)
{
WinHelp(Globals.hMainWnd, helpfile, HELP_INDEX, 0);
}
VOID DIALOG_HelpAboutNotepad(VOID)
{
TCHAR szNotepad[MAX_STRING_LEN];
TCHAR szNotepadAuthors[MAX_STRING_LEN];
LoadString(Globals.hInstance, STRING_NOTEPAD, szNotepad, _countof(szNotepad));
LoadString(Globals.hInstance, STRING_NOTEPAD_AUTHORS, szNotepadAuthors, _countof(szNotepadAuthors));
ShellAbout(Globals.hMainWnd, szNotepad, szNotepadAuthors,
LoadIcon(Globals.hInstance, MAKEINTRESOURCE(IDI_NPICON)));
}