forked from windows-source/MS-Notepad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notepad.c
2167 lines (1770 loc) · 71.4 KB
/
notepad.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
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
/*
* Notepad application
* Copyright (C) 1984-2001 Microsoft Inc.
*/
#include "precomp.h"
#include <htmlhelp.h>
#define DeepTrouble() MessageBox(hwndNP, szErrSpace, szNN, MB_SYSTEMMODAL|MB_OK|MB_ICONHAND);
UINT lGotoLine; /* line number to goto to */
TCHAR chMerge;
HWND hwndNP = 0; /* handle to notepad parent window */
HWND hwndStatus = 0; /* handle to notepad status window */
HWND hwndEdit = 0; /* handle to main text control item */
HANDLE hEdit; /* Handle to storage for edit item */
HWND hDlgFind = NULL; /* handle to modeless FindText window */
HANDLE hStdCursor; /* handle to arrow or beam cursor */
HANDLE hWaitCursor; /* handle to hour glass cursor */
HANDLE hInstanceNP; /* Module instance handle */
HANDLE hFont; /* handle to Unicode font */
LOGFONT FontStruct; /* font dialog structure */
INT iPointSize=120; /* current point size unit=1/10 pts */
TCHAR szFileName[MAX_PATH+1]; /* Current notepad filename */
TCHAR szSearch[CCHKEYMAX]; /* Search string */
TCHAR szReplace[CCHKEYMAX]; /* replace string */
BOOL fUntitled = TRUE; /* TRUE iff notepad has no title */
BOOL fStatus = FALSE; /* status bar shown? */
BOOL fLastStatus = FALSE; /* status bar status when wordwrap was turned off */
INT dyStatus; /* height of status bar */
HMENU hSysMenuSetup; /* Save Away for disabled Minimize */
DWORD dwEmSetHandle = 0; /* Is EM_SETHANDLE in process? */
HANDLE hAccel; /* Handle to accelerator table */
BOOL fRunBySetup = FALSE; /* Did SlipUp WinExec us?? */
BOOL fWrap = 0; /* Flag for word wrap */
TCHAR szNotepad[] = TEXT("Notepad");/* Name of notepad window class */
BOOL fInSaveAsDlg = FALSE;
// Edit control used to AV is EM_FMTLINES was turned off when cursor was near the end
// To get around this, notepad moved to cursor to 0,0 when it turned off wordwrap.
// Users were not happy, so we will put up with possible AVs. Note: as of June 27, 2001
// we could not repro the AV behavior, so perhaps it is not there anymore.
BOOL fMLE_is_broken= FALSE;
/* variables for the new File/Open, File/Saveas,Find Text and Print dialogs */
OPENFILENAME OFN; /* passed to the File Open/save APIs */
TCHAR szOpenFilterSpec[CCHFILTERMAX]; /* default open filter spec */
TCHAR szSaveFilterSpec[CCHFILTERMAX]; /* default save filter spec */
NP_FILETYPE g_ftOpenedAs=FT_UNKNOWN; /* current file was opened */
NP_FILETYPE g_ftSaveAs; /* current file was opened */
FINDREPLACE FR; /* Passed to FindText() */
PAGESETUPDLG g_PageSetupDlg;
UINT wFRMsg; /* message used in communicating */
/* with Find/Replace dialog */
DWORD dwCurrentSelectionStart = 0L; /* WM_ACTIVATEAPP selection pos */
DWORD dwCurrentSelectionEnd = 0L; /* WM_ACTIVATEAPP selection pos */
UINT wHlpMsg; /* message used in invoking help */
/* Strings loaded from resource file passed to LoadString at initialization time */
/* To add resource string:
* 1) create IDS_ macro definition in notepad.h
* 2) create string in resource file
* 3) create 'TCHAR*' variable directly below and in notepad.h file
* 4) add &variable to rgsz
* 5) increment CSTRINGS
*/
TCHAR *szDiskError =(TCHAR *)IDS_DISKERROR; /* Can't open File, check disk */
TCHAR *szFNF =(TCHAR *)IDS_FNF; /* File not found */
TCHAR *szSCBC =(TCHAR *)IDS_SCBC; /* Save changes before closing? */
TCHAR *szUntitled =(TCHAR *)IDS_UNTITLED; /* Untitled */
TCHAR *szNpTitle =(TCHAR *)IDS_NOTEPAD; /* Notepad - */
TCHAR *szCFS =(TCHAR *)IDS_CFS; /* Can't find string */
TCHAR *szErrSpace =(TCHAR *)IDS_ERRSPACE; /* Memory space exhausted */
TCHAR *szFTL =(TCHAR *)IDS_FTL; /* File too large for notepad */
TCHAR *szNN =(TCHAR *)IDS_NN; /* Notepad name */
TCHAR *szCommDlgInitErr = (TCHAR*)IDS_COMMDLGINIT; /* common dialog error %x */
TCHAR *szPDIE =(TCHAR*) IDS_PRINTDLGINIT; /* Print dialog init error */
TCHAR *szCP =(TCHAR*) IDS_CANTPRINT; /* Can't print */
TCHAR *szNVF =(TCHAR*) IDS_NVF; /* Not a valid filename. */
TCHAR *szCREATEERR =(TCHAR*) IDS_CREATEERR; /* cannot create file */
TCHAR *szNoWW =(TCHAR*) IDS_NOWW; /* Too much text to word wrap */
TCHAR *szMerge =(TCHAR*) IDS_MERGE1; /* search string for merge */
TCHAR *szHelpFile =(TCHAR*) IDS_HELPFILE; /* Name of helpfile. */
TCHAR *szHeader =(TCHAR*) IDS_HEADER;
TCHAR *szFooter =(TCHAR*) IDS_FOOTER;
TCHAR *szLetters =(TCHAR*) IDS_LETTERS; /* formatting letters in pagesetup */
TCHAR *szAnsiText = (TCHAR*)IDS_ANSITEXT; /* File/Open ANSI filter spec. string */
TCHAR *szAllFiles = (TCHAR*)IDS_ALLFILES; /* File/Open Filter spec. string */
TCHAR *szOpenCaption = (TCHAR*)IDS_OPENCAPTION; /* caption for File/Open dlg */
TCHAR *szSaveCaption = (TCHAR*)IDS_SAVECAPTION; /* caption for File/Save dlg */
TCHAR *szCannotQuit = (TCHAR*)IDS_CANNOTQUIT; /* cannot quit during a WM_QUERYENDSESSION */
TCHAR *szLoadDrvFail = (TCHAR*)IDS_LOADDRVFAIL; /* LOADDRVFAIL from PrintDlg */
TCHAR *szACCESSDENY = (TCHAR*)IDS_ACCESSDENY; /* Access denied on Open */
TCHAR *szErrUnicode = (TCHAR*)IDS_ERRUNICODE; /* Unicode character existence error */
TCHAR *szFontTooBig = (TCHAR*)IDS_FONTTOOBIG; /* font too big or page too small */
TCHAR *szCommDlgErr = (TCHAR*) IDS_COMMDLGERR; /* common dialog error %x */
TCHAR *szLineError = (TCHAR*) IDS_LINEERROR; /* line number error */
TCHAR *szLineTooLarge = (TCHAR*) IDS_LINETOOLARGE;/* line number out of range */
TCHAR *szFtAnsi = (TCHAR*) IDS_FT_ANSI;
TCHAR *szFtUnicode = (TCHAR*) IDS_FT_UNICODE;
TCHAR *szFtUnicodeBe = (TCHAR*) IDS_FT_UNICODEBE;
TCHAR *szFtUtf8 = (TCHAR*) IDS_FT_UTF8;
TCHAR *szCurrentPage = (TCHAR*) IDS_CURRENT_PAGE;
// strings for the status bar
TCHAR *szLineCol = (TCHAR*) IDS_LINECOL;
TCHAR *szCompressedFile = (TCHAR*) IDS_COMPRESSED_FILE;
TCHAR *szEncryptedFile = (TCHAR*) IDS_ENCRYPTED_FILE;
TCHAR *szHiddenFile = (TCHAR*) IDS_HIDDEN_FILE;
TCHAR *szOfflineFile = (TCHAR*) IDS_OFFLINE_FILE;
TCHAR *szReadOnlyFile = (TCHAR*) IDS_READONLY_FILE;
TCHAR *szSystemFile = (TCHAR*) IDS_SYSTEM_FILE;
TCHAR *szFile = (TCHAR*) IDS_FILE;
TCHAR **rgsz[CSTRINGS] = {
&szDiskError,
&szFNF,
&szSCBC,
&szUntitled,
&szErrSpace,
&szCFS,
&szNpTitle,
&szFTL,
&szNN,
&szCommDlgInitErr,
&szPDIE,
&szCP,
&szNVF,
&szCREATEERR,
&szNoWW,
&szMerge,
&szHelpFile,
&szAnsiText,
&szAllFiles,
&szOpenCaption,
&szSaveCaption,
&szCannotQuit,
&szLoadDrvFail,
&szACCESSDENY,
&szErrUnicode,
&szCommDlgErr,
&szFontTooBig,
&szLineError,
&szLineTooLarge,
&szFtAnsi,
&szFtUnicode,
&szFtUnicodeBe,
&szFtUtf8,
&szCurrentPage,
&szHeader,
&szFooter,
&szLineCol,
&szCompressedFile,
&szEncryptedFile,
&szHiddenFile,
&szOfflineFile,
&szReadOnlyFile,
&szSystemFile,
&szFile,
&szLetters,
};
HANDLE fp; /* file pointer */
#if 0
VOID DisplayFont( LOGFONT* pf )
{
TCHAR dbuf[100];
ODS(TEXT("-----------------------\n"));
wsprintf(dbuf,TEXT("lfHeight %d\n"),pf->lfHeight); ODS(dbuf);
wsprintf(dbuf,TEXT("lfWidth %d\n"),pf->lfWidth ); ODS(dbuf);
wsprintf(dbuf,TEXT("lfEscapement %d\n"),pf->lfEscapement); ODS(dbuf);
wsprintf(dbuf,TEXT("lfOrientation %d\n"),pf->lfOrientation); ODS(dbuf);
wsprintf(dbuf,TEXT("lfWeight %d\n"),pf->lfWeight); ODS(dbuf);
wsprintf(dbuf,TEXT("lfItalic %d\n"),pf->lfItalic); ODS(dbuf);
wsprintf(dbuf,TEXT("lfUnderLine %d\n"),pf->lfUnderline); ODS(dbuf);
wsprintf(dbuf,TEXT("lfStrikeOut %d\n"),pf->lfStrikeOut); ODS(dbuf);
wsprintf(dbuf,TEXT("lfCharSet %d\n"),pf->lfCharSet); ODS(dbuf);
wsprintf(dbuf,TEXT("lfOutPrecision %d\n"),pf->lfOutPrecision); ODS(dbuf);
wsprintf(dbuf,TEXT("lfClipPrecision %d\n"),pf->lfClipPrecision); ODS(dbuf);
wsprintf(dbuf,TEXT("lfQuality %d\n"),pf->lfQuality); ODS(dbuf);
wsprintf(dbuf,TEXT("lfPitchAndFamily %d\n"),pf->lfPitchAndFamily); ODS(dbuf);
wsprintf(dbuf,TEXT("lfFaceName %s\n"),pf->lfFaceName); ODS(dbuf);
}
#endif
static TCHAR szPath[MAX_PATH];
void FileDragOpen(void);
VOID NpResetMenu(HWND hWnd);
BOOL SignalCommDlgError(VOID);
VOID ReplaceSel( BOOL bView );
/* FreeGlobal, frees all global memory allocated. */
void NEAR PASCAL FreeGlobal()
{
if(g_PageSetupDlg.hDevMode)
{
GlobalFree(g_PageSetupDlg.hDevMode);
}
if(g_PageSetupDlg.hDevNames)
{
GlobalFree(g_PageSetupDlg.hDevNames);
}
g_PageSetupDlg.hDevMode= NULL; // make sure they are zero for PrintDlg
g_PageSetupDlg.hDevNames= NULL;
}
VOID PASCAL SetPageSetupDefaults( VOID )
{
TCHAR szIMeasure[ 2 ];
g_PageSetupDlg.lpfnPageSetupHook= PageSetupHookProc;
g_PageSetupDlg.lpPageSetupTemplateName= MAKEINTRESOURCE(IDD_PAGESETUP);
GetLocaleInfo( LOCALE_USER_DEFAULT, LOCALE_IMEASURE, szIMeasure, 2 );
g_PageSetupDlg.Flags= PSD_MARGINS |
PSD_ENABLEPAGESETUPHOOK | PSD_ENABLEPAGESETUPTEMPLATE;
if (szIMeasure[ 0 ] == TEXT( '1' ))
{
// English measure (in thousandths of inches).
g_PageSetupDlg.Flags |= PSD_INTHOUSANDTHSOFINCHES;
g_PageSetupDlg.rtMargin.top = 1000;
g_PageSetupDlg.rtMargin.bottom = 1000;
g_PageSetupDlg.rtMargin.left = 750;
g_PageSetupDlg.rtMargin.right = 750;
}
else
{
// Metric measure (in hundreths of millimeters).
g_PageSetupDlg.Flags |= PSD_INHUNDREDTHSOFMILLIMETERS;
g_PageSetupDlg.rtMargin.top = 2500;
g_PageSetupDlg.rtMargin.bottom = 2500;
g_PageSetupDlg.rtMargin.left = 2000;
g_PageSetupDlg.rtMargin.right = 2000;
}
}
/* Standard window size proc */
void NPSize (int cxNew, int cyNew)
{
/* Invalidate the edit control window so that it is redrawn with the new
* margins. Needed when comming up from iconic and when doing word wrap so
* the new margins are accounted for.
*/
InvalidateRect(hwndEdit, (LPRECT)NULL, TRUE);
// the height of the edit window depends on whether the status bar is
// displayed.
MoveWindow (hwndEdit, 0, 0, cxNew, cyNew - (fStatus?dyStatus:0), TRUE);
}
// NpSaveDialogHookProc
//
// Common dialog hook procedure for handling
// the file type while saving.
//
const DWORD s_SaveAsHelpIDs[]=
{
IDC_FILETYPE, IDH_FILETYPE,
IDC_ENCODING, IDH_FILETYPE,
0, 0
};
UINT_PTR APIENTRY NpSaveDialogHookProc(
HWND hWnd,
UINT msg,
WPARAM wParam,
LPARAM lParam)
{
INT id;
POINT pt;
TCHAR* szSelect; // selected type
switch( msg )
{
case WM_INITDIALOG:
// Warning: the order here must be the same as NP_FILETYPE
SendDlgItemMessage(hWnd, IDC_FILETYPE,CB_ADDSTRING, 0, (LPARAM) szFtAnsi );
SendDlgItemMessage(hWnd, IDC_FILETYPE,CB_ADDSTRING, 0, (LPARAM) szFtUnicode );
SendDlgItemMessage(hWnd, IDC_FILETYPE,CB_ADDSTRING, 0, (LPARAM) szFtUnicodeBe );
SendDlgItemMessage(hWnd, IDC_FILETYPE,CB_ADDSTRING, 0, (LPARAM) szFtUtf8 );
szSelect= szFtAnsi; // default
g_ftSaveAs= g_ftOpenedAs; // default: save as same type as opened
switch( g_ftSaveAs )
{
case FT_UNICODE: szSelect= szFtUnicode; break;
case FT_UNICODEBE: szSelect= szFtUnicodeBe; break;
case FT_UTF8: szSelect= szFtUtf8; break;
default: break;
}
SendDlgItemMessage( hWnd, IDC_FILETYPE, CB_SELECTSTRING, (WPARAM) -1, (LPARAM)szSelect );
break;
case WM_COMMAND:
g_ftSaveAs= (NP_FILETYPE) SendDlgItemMessage( hWnd, IDC_FILETYPE, CB_GETCURSEL, 0, 0 );
break;
case WM_HELP:
//
// We only want to intercept help messages for controls that we are
// responsible for.
//
id = GetDlgCtrlID(((LPHELPINFO) lParam)-> hItemHandle);
if ( id != IDC_FILETYPE && id != IDC_ENCODING)
break;
WinHelp(((LPHELPINFO) lParam)-> hItemHandle,
szHelpFile,
HELP_WM_HELP,
(ULONG_PTR) s_SaveAsHelpIDs);
return TRUE;
case WM_CONTEXTMENU:
//
// If the user clicks on any of our labels, then the wParam will
// be the hwnd of the dialog, not the static control. WinHelp()
// handles this, but because we hook the dialog, we must catch it
// first.
//
if( hWnd == (HWND) wParam )
{
GetCursorPos(&pt);
ScreenToClient(hWnd, &pt);
wParam = (WPARAM) ChildWindowFromPoint(hWnd, pt);
}
//
// We only want to intercept help messages for controls that we are
// responsible for.
//
id = GetDlgCtrlID((HWND) wParam);
if ( id != IDC_FILETYPE && id != IDC_ENCODING)
break;
WinHelp( (HWND) wParam,
szHelpFile,
HELP_CONTEXTMENU,
(ULONG_PTR) s_SaveAsHelpIDs);
return TRUE;
}
return( FALSE );
}
// NpOpenDialogHookProc
//
// Common dialog hook procedure for handling
// the file type while opening.
//
UINT_PTR APIENTRY NpOpenDialogHookProc(
HWND hWnd,
UINT msg,
WPARAM wParam,
LPARAM lParam)
{
INT id;
POINT pt;
TCHAR* szSelect; // selected type
static TCHAR szPrevFileName[MAX_PATH] = TEXT("");
switch( msg )
{
case WM_INITDIALOG:
// Warning: the order here must be the same as NP_FILETYPE
SendDlgItemMessage(hWnd, IDC_FILETYPE,CB_ADDSTRING, 0, (LPARAM) szFtAnsi );
SendDlgItemMessage(hWnd, IDC_FILETYPE,CB_ADDSTRING, 0, (LPARAM) szFtUnicode );
SendDlgItemMessage(hWnd, IDC_FILETYPE,CB_ADDSTRING, 0, (LPARAM) szFtUnicodeBe );
SendDlgItemMessage(hWnd, IDC_FILETYPE,CB_ADDSTRING, 0, (LPARAM) szFtUtf8 );
szSelect= szFtAnsi; // default
switch( g_ftOpenedAs )
{
case FT_UNICODE: szSelect= szFtUnicode; break;
case FT_UNICODEBE: szSelect= szFtUnicodeBe; break;
case FT_UTF8: szSelect= szFtUtf8; break;
default: break;
}
// set the current filetype.
SendDlgItemMessage( hWnd, IDC_FILETYPE, CB_SELECTSTRING, (WPARAM) -1, (LPARAM)szSelect );
break;
case WM_COMMAND:
g_ftOpenedAs= (NP_FILETYPE) SendDlgItemMessage( hWnd, IDC_FILETYPE, CB_GETCURSEL, 0, 0 );
break;
case WM_HELP:
//
// We only want to intercept help messages for controls that we are
// responsible for.
//
id = GetDlgCtrlID(((LPHELPINFO) lParam)-> hItemHandle);
if ( id != IDC_FILETYPE && id != IDC_ENCODING)
break;
WinHelp(((LPHELPINFO) lParam)-> hItemHandle,
szHelpFile,
HELP_WM_HELP,
(ULONG_PTR) s_SaveAsHelpIDs);
return TRUE;
case WM_CONTEXTMENU:
//
// If the user clicks on any of our labels, then the wParam will
// be the hwnd of the dialog, not the static control. WinHelp()
// handles this, but because we hook the dialog, we must catch it
// first.
//
if( hWnd == (HWND) wParam )
{
GetCursorPos(&pt);
ScreenToClient(hWnd, &pt);
wParam = (WPARAM) ChildWindowFromPoint(hWnd, pt);
}
//
// We only want to intercept help messages for controls that we are
// responsible for.
//
id = GetDlgCtrlID((HWND) wParam);
if ( id != IDC_FILETYPE && id != IDC_ENCODING)
break;
WinHelp( (HWND) wParam,
szHelpFile,
HELP_CONTEXTMENU,
(ULONG_PTR) s_SaveAsHelpIDs);
return TRUE;
case WM_NOTIFY:
{
LPOFNOTIFY pofn;
TCHAR szFileName[MAX_PATH];
BYTE szFileBuffer[BUFFER_TEST_SIZE];
HANDLE hFile;
DWORD dwBytesRead;
// process the message when the file selection changes.
pofn = (LPOFNOTIFY)lParam;
switch (pofn->hdr.code)
{
case CDN_SELCHANGE:
{
// get the filename.
if (CommDlg_OpenSave_GetFilePath(GetParent(hWnd), szFileName, sizeof(szFileName)/sizeof(TCHAR)) > 0)
{
// if same file as the previous file, don't do anything.
if (lstrcmpi(szFileName, szPrevFileName) == 0)
break;
// open the file.
hFile = CreateFile(szFileName,GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
// if the file read fails, just quit.
if ( (ReadFile(hFile, szFileBuffer, BUFFER_TEST_SIZE, &dwBytesRead, NULL) <= 0) || dwBytesRead == 0)
{
CloseHandle(hFile);
break;
}
// determine the file type based on dwBytesRead bytes of the file.
g_ftOpenedAs = fDetermineFileType(szFileBuffer, dwBytesRead);
// set the dropdown filetype to the filetype we think based on the initial part of the file.
szSelect = szFtAnsi; // default
switch( g_ftOpenedAs )
{
case FT_UNICODE: szSelect= szFtUnicode; break;
case FT_UNICODEBE: szSelect= szFtUnicodeBe; break;
case FT_UTF8: szSelect= szFtUtf8; break;
default: break;
}
SendDlgItemMessage( hWnd, IDC_FILETYPE, CB_SELECTSTRING, (WPARAM) -1, (LPARAM)szSelect );
// cleanup.
lstrcpy(szPrevFileName, szFileName);
CloseHandle(hFile);
}
}
break;
}
}
}
}
return( FALSE );
}
// GotoAndScrollInView
//
// Put the cursor at the begining of a line, and scroll the
// editbox so the user can see it.
//
// If there is a failure, it just leaves the cursor where it is.
//
VOID GotoAndScrollInView( INT OneBasedLineNumber )
{
UINT CharIndex;
CharIndex= (UINT) SendMessage( hwndEdit,
EM_LINEINDEX,
OneBasedLineNumber-1,
0 );
if( CharIndex != (UINT) -1 )
{
SendMessage( hwndEdit, EM_SETSEL, CharIndex, CharIndex);
SendMessage( hwndEdit, EM_SCROLLCARET, 0, 0 );
}
}
/* ** Notepad command proc - called whenever notepad gets WM_COMMAND
message. wParam passed as cmd */
INT NPCommand(
HWND hwnd,
WPARAM wParam,
LPARAM lParam )
{
HWND hwndFocus;
LONG lSel;
TCHAR szNewName[MAX_PATH] = TEXT(""); /* New file name */
FARPROC lpfn;
LONG style;
DWORD rc;
RECT rcClient;
switch (LOWORD(wParam))
{
case M_EXIT:
PostMessage(hwnd, WM_CLOSE, 0, 0L);
break;
case M_NEW:
New(TRUE);
break;
case M_OPEN:
if (CheckSave(FALSE))
{
NP_FILETYPE g_ftOldOpenedAs = g_ftOpenedAs;
/* set up the variable fields of the OPENFILENAME struct.
* (the constant fields have been set in NPInit()
*/
OFN.lpstrFile = szNewName;
lstrcpy(szNewName, TEXT("*.txt") ); /* set default selection */
OFN.lpstrTitle = szOpenCaption;
/* ALL non-zero long pointers must be defined immediately
* before the call, as the DS might move otherwise.
* 12 February 1991 clarkc
*/
OFN.lpstrFilter = szOpenFilterSpec;
OFN.lpstrDefExt = TEXT("txt");
/* Added OFN_FILEMUSTEXIST to eliminate problems in LoadFile.
* 12 February 1991 clarkc
*/
OFN.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST |
OFN_EXPLORER |
OFN_ENABLESIZING |
OFN_ENABLETEMPLATE | OFN_ENABLEHOOK;
OFN.nFilterIndex = FILE_TEXT;
// show encoding listbox
OFN.lpTemplateName= TEXT("NpEncodingDialog");
OFN.lpfnHook= NpOpenDialogHookProc;
if( GetOpenFileName( (LPOPENFILENAME)&OFN ) )
{
HANDLE oldfp= fp;
fp= CreateFile( szNewName, // filename
GENERIC_READ, // access mode
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL, // security descriptor
OPEN_EXISTING, // how to create
FILE_ATTRIBUTE_NORMAL,// file attributes
NULL); // hnd to file attrs
/* Try to load the file and reset fp if failed */
if( !LoadFile( szNewName, g_ftOpenedAs ) )
{
fp= oldfp;
g_ftOpenedAs = g_ftOldOpenedAs;
}
}
else
{
g_ftOpenedAs = g_ftOldOpenedAs;
SignalCommDlgError();
}
}
break;
case M_SAVE:
/* set up the variable fields of the OPENFILENAME struct.
* (the constant fields have been sel in NPInit()
*/
g_ftSaveAs = g_ftOpenedAs;
if( !fUntitled && SaveFile( hwndNP, szFileName, FALSE ) )
break;
/* fall through */
case M_SAVEAS:
OFN.lpstrFile = szNewName;
OFN.lpstrTitle = szSaveCaption;
/* Added OFN_PATHMUSTEXIST to eliminate problems in SaveFile.
* 12 February 1991 clarkc
*/
OFN.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT |
OFN_NOREADONLYRETURN | OFN_PATHMUSTEXIST |
OFN_EXPLORER |
OFN_ENABLESIZING |
OFN_ENABLETEMPLATE | OFN_ENABLEHOOK;
OFN.lpTemplateName= TEXT("NpEncodingDialog");
OFN.lpfnHook= NpSaveDialogHookProc;
/* ALL non-zero long pointers must be defined immediately
* before the call, as the DS might move otherwise.
* 12 February 1991 clarkc
*/
OFN.lpstrFilter = szSaveFilterSpec;
OFN.lpstrDefExt = TEXT("txt");
if (!fUntitled)
{
lstrcpyn(szNewName, szFileName, MAX_PATH); /* set default selection */
}
else
{
lstrcpy (szNewName, TEXT("*.txt") );
}
fInSaveAsDlg = TRUE;
OFN.nFilterIndex= FILE_TEXT;
//
// Do common dialog to save file
//
if (GetSaveFileName(&OFN))
{
if( SaveFile(hwnd, szNewName, TRUE) )
{
lstrcpyn( szFileName, szNewName, MAX_PATH);
g_ftOpenedAs= g_ftSaveAs;
}
}
else
{
SignalCommDlgError();
}
fInSaveAsDlg = FALSE;
break;
case M_SELECTALL:
{
HMENU hMenu;
hMenu = GetMenu(hwndNP);
lSel = (LONG) SendMessage (hwndEdit, WM_GETTEXTLENGTH, 0, 0L);
SendMessage (hwndEdit, EM_SETSEL, 0, lSel );
SendMessage(hwndEdit, EM_SCROLLCARET, 0, 0);
EnableMenuItem(GetSubMenu(hMenu, 1), M_SELECTALL, MF_GRAYED);
break;
}
case M_REPLACE:
if( hDlgFind )
{
SetFocus( hDlgFind );
}
else
{
FR.Flags= FR_HIDEWHOLEWORD | FR_REPLACE;
FR.lpstrReplaceWith= szReplace;
FR.wReplaceWithLen= CCHKEYMAX;
FR.lpstrFindWhat = szSearch;
FR.wFindWhatLen = CCHKEYMAX;
hDlgFind = ReplaceText( &FR );
}
break;
case M_FINDNEXT:
if (szSearch[0])
{
Search(szSearch);
break;
}
/* else fall thro' a,d bring up "find" dialog */
case M_FIND:
if (hDlgFind)
{
SetFocus(hDlgFind);
}
else
{
FR.Flags= FR_DOWN | FR_HIDEWHOLEWORD;
FR.lpstrReplaceWith= NULL;
FR.wReplaceWithLen= 0;
FR.lpstrFindWhat = szSearch;
FR.wFindWhatLen = CCHKEYMAX;
hDlgFind = FindText((LPFINDREPLACE)&FR);
}
break;
case M_GOTO:
{
INT Result;
Result= (INT)DialogBox( hInstanceNP,
MAKEINTRESOURCE(IDD_GOTODIALOG),
hwndNP,
GotoDlgProc );
//
// move cursor only if ok pressed and line number ok
//
if( Result == 0 )
{
GotoAndScrollInView( lGotoLine );
}
}
break;
case M_ABOUT:
ShellAbout(hwndNP,
szNN,
TEXT(""),
LoadIcon(hInstanceNP,
(LPTSTR)MAKEINTRESOURCE(ID_ICON)));
break;
case M_HELP:
HtmlHelpA(GetDesktopWindow(), "notepad.chm", HH_DISPLAY_TOPIC, 0L);
break;
case M_CUT:
case M_COPY:
case M_CLEAR:
lSel = (LONG)SendMessage (hwndEdit, EM_GETSEL, 0, 0L);
if (LOWORD(lSel) == HIWORD(lSel))
break;
case M_PASTE:
/* If notepad parent or edit window has the focus,
pass command to edit window.
make sure line resulting from paste will not be too long. */
hwndFocus = GetFocus();
if (hwndFocus == hwndEdit || hwndFocus == hwndNP)
{
PostMessage(hwndEdit, LOWORD(wParam), 0, 0);
}
break;
case M_DATETIME:
InsertDateTime(FALSE);
break;
case M_UNDO:
SendMessage (hwndEdit, EM_UNDO, 0, 0L);
break;
case M_WW:
style= (!fWrap) ? ES_STD : (ES_STD | WS_HSCROLL);
if( NpReCreate( style ) )
{
fWrap= !fWrap;
}
else
{
MessageBox(hwndNP, szNoWW, szNN,
MB_APPLMODAL | MB_OK | MB_ICONEXCLAMATION);
}
// disable the status bar
// Line numbers when wordwrap is on are very confusing for now. Just turn them
// off until we better understand what the user wants to see.
if (fWrap)
{
HMENU hMenu;
// Uncheck the StatusBar and remove it.
fLastStatus= fStatus; // remember for when wordwrap gets turned off
if( fStatus )
{
SendMessage(hwnd, WM_COMMAND, M_STATUSBAR, 0L);
}
hMenu = GetMenu(hwndNP);
CheckMenuItem (GetSubMenu(hMenu, 3), M_STATUSBAR, MF_UNCHECKED);
EnableMenuItem(GetSubMenu(hMenu, 3), M_STATUSBAR, MF_GRAYED);
}
// enable the status bar
else
{
HMENU hMenu;
hMenu = GetMenu(hwndNP);
EnableMenuItem(GetSubMenu(hMenu, 3), M_STATUSBAR, MF_ENABLED);
// change the statusbar status to what it was before wordwrap was turned on
if( fLastStatus )
{
SendMessage( hwnd, WM_COMMAND, M_STATUSBAR, 0L);
}
}
break;
case M_STATUSBAR:
// hide/show the statusbar and also redraw the edit window accordingly.
GetClientRect(hwndNP, &rcClient);
if ( fStatus )
{
fStatus = FALSE;
ShowWindow ( hwndStatus, SW_HIDE );
NPSize(rcClient.right - rcClient.left, rcClient.bottom - rcClient.top);
}
else
{
fStatus = TRUE;
NPSize(rcClient.right - rcClient.left, rcClient.bottom - rcClient.top);
UpdateStatusBar( TRUE );
ShowWindow( hwndStatus, SW_SHOW );
}
break;
case ID_EDIT:
break;
case M_PRINT:
PrintIt( UseDialog );
break;
case M_PAGESETUP:
TryPrintDlgAgain:
if( PageSetupDlg(&g_PageSetupDlg) )
{
// We know it's okay to copy these strings over...
lstrcpy(chPageText[HEADER], chPageTextTemp[HEADER]);
lstrcpy(chPageText[FOOTER], chPageTextTemp[FOOTER]);
}
else
{
rc= CommDlgExtendedError();
if( rc == PDERR_PRINTERNOTFOUND ||
rc == PDERR_DNDMMISMATCH ||
rc == PDERR_DEFAULTDIFFERENT )
{
FreeGlobal();
g_PageSetupDlg.hDevMode= g_PageSetupDlg.hDevNames= 0;
goto TryPrintDlgAgain;
}
// Check for Dialog Failure
SignalCommDlgError( );
}
break;
case M_SETFONT:
{
CHOOSEFONT cf;
HFONT hFontNew;
HDC hDisplayDC; // display DC
hDisplayDC= GetDC(NULL); // try to get display DC
if( !hDisplayDC )
break;
// calls the font chooser (in commdlg)
// We set lfHeight; choosefont returns ipointsize
//
cf.lStructSize = sizeof(CHOOSEFONT);
cf.hwndOwner = hwnd;
cf.lpLogFont = &FontStruct; // filled in by init
FontStruct.lfHeight= -MulDiv(iPointSize,GetDeviceCaps(hDisplayDC,LOGPIXELSY),720);
// We filter out useless stuff here
// We tried CF_NOSCRIPTSEL, but the FE had fits.
// Even though it looks useless, it changes the font that gets mapped on FE builds.
// Even though we ignore the lfCharSet that gets returned, we have the "right"
// font according to the FE guys. It might make sense to use lfCharSet to
// convert the ansi file when it is converted to Unicode, but this might be
// confusing.
cf.Flags = CF_INITTOLOGFONTSTRUCT |
CF_SCREENFONTS |
CF_NOVERTFONTS |
// CF_NOSCRIPTSEL | // windows bug# 7770 (April 10,2001)
0;
cf.rgbColors = 0; // only if cf_effects
cf.lCustData = 0; // for hook function
cf.lpfnHook = (LPCFHOOKPROC) NULL;
cf.lpTemplateName = (LPTSTR) NULL;
cf.hInstance = NULL;
cf.lpszStyle = NULL; // iff cf_usestyle
cf.nFontType = SCREEN_FONTTYPE;
cf.nSizeMin = 0; // iff cf_limitsize
cf.nSizeMax = 0; // iff cf_limitsize
ReleaseDC( NULL, hDisplayDC );
if( ChooseFont(&cf) )
{
SetCursor( hWaitCursor ); // may take some time
hFontNew= CreateFontIndirect(&FontStruct);
if( hFontNew )
{
DeleteObject( hFont );
hFont= hFontNew;
SendMessage( hwndEdit, WM_SETFONT,
(WPARAM)hFont, MAKELPARAM(TRUE, 0));
iPointSize= cf.iPointSize; // remember for printer
}
SetCursor( hStdCursor );
}
break;
}
default:
return FALSE;
}
return TRUE;
}
// for some reason, this procedure tries to maintain
// a valid 'fp' even though I believe it does not need
// to be.
void FileDragOpen(void)
{
HANDLE oldfp;