-
Notifications
You must be signed in to change notification settings - Fork 168
/
Notepad2.c
7797 lines (6714 loc) · 225 KB
/
Notepad2.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
/******************************************************************************
*
*
* Notepad2
*
* Notepad2.c
* Main application window functionality
*
* See Readme.txt for more information about this source code.
* Please send me your comments to this work.
*
* See License.txt for details about distribution and modification.
*
* (c) Florian Balmer 1996-2011
* florian.balmer@gmail.com
* http://www.flos-freeware.ch
*
*
******************************************************************************/
#include <windows.h>
#include <shlwapi.h>
#include <shlobj.h>
#include <shellapi.h>
#include <commctrl.h>
#include <commdlg.h>
#include <stdio.h>
#include <time.h>
#include "Notepad2.h"
#include "Edit.h"
#include "Styles.h"
#include "Dialogs.h"
#include "Helpers.h"
#include "SciCall.h"
#include "resource.h"
// show fold level
#define NP2_DEBUG_FOLD_LEVEL 0
/******************************************************************************
*
* Local and global Variables for Notepad2.c
*
*/
HWND hwndStatus;
HWND hwndToolbar;
HWND hwndReBar;
HWND hwndEdit;
HWND hwndEditFrame;
HWND hwndMain;
HWND hwndNextCBChain = NULL;
HWND hDlgFindReplace = NULL;
#define NUMTOOLBITMAPS 25
#define NUMINITIALTOOLS 24
#define MARGIN_FOLD_INDEX 2
TBBUTTON tbbMainWnd[] = {
{0, IDT_FILE_NEW, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
{1, IDT_FILE_OPEN, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
{2, IDT_FILE_BROWSE, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
{3, IDT_FILE_SAVE, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
{0, 0, 0, TBSTYLE_SEP, 0, 0},
{4, IDT_EDIT_UNDO, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
{5, IDT_EDIT_REDO, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
{0, 0, 0, TBSTYLE_SEP, 0, 0},
{6, IDT_EDIT_CUT, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
{7, IDT_EDIT_COPY, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
{8, IDT_EDIT_PASTE, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
{0, 0, 0, TBSTYLE_SEP, 0, 0},
{9, IDT_EDIT_FIND, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
{10, IDT_EDIT_REPLACE, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
{0, 0, 0, TBSTYLE_SEP, 0, 0},
{11, IDT_VIEW_WORDWRAP, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
{0, 0, 0, TBSTYLE_SEP, 0, 0},
{12, IDT_VIEW_ZOOMIN, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
{13, IDT_VIEW_ZOOMOUT, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
{0, 0, 0, TBSTYLE_SEP, 0, 0},
{14, IDT_VIEW_SCHEME, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
{15, IDT_VIEW_SCHEMECONFIG, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
{0, 0, 0, TBSTYLE_SEP, 0, 0},
{16, IDT_FILE_EXIT, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
{17, IDT_FILE_SAVEAS, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
{18, IDT_FILE_SAVECOPY, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
{19, IDT_EDIT_CLEAR, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
{20, IDT_FILE_PRINT, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
{21, IDT_FILE_OPENFAV, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
{22, IDT_FILE_ADDTOFAV, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
{23, IDT_VIEW_TOGGLEFOLDS, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
{24, IDT_FILE_LAUNCH, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0}
};
WCHAR szIniFile[MAX_PATH] = L"";
WCHAR szIniFile2[MAX_PATH] = L"";
BOOL bSaveSettings;
BOOL bSaveRecentFiles;
BOOL bSaveFindReplace;
WCHAR tchLastSaveCopyDir[MAX_PATH] = L"";
WCHAR tchOpenWithDir[MAX_PATH];
WCHAR tchFavoritesDir[MAX_PATH];
WCHAR tchDefaultDir[MAX_PATH];
WCHAR tchDefaultExtension[64];
WCHAR tchFileDlgFilters[5 * 1024];
WCHAR tchToolbarButtons[512];
WCHAR tchToolbarBitmap[MAX_PATH];
WCHAR tchToolbarBitmapHot[MAX_PATH];
WCHAR tchToolbarBitmapDisabled[MAX_PATH];
int iPathNameFormat;
BOOL fWordWrap;
BOOL fWordWrapG;
int iWordWrapMode;
int iWordWrapIndent;
int iWordWrapSymbols;
BOOL bShowWordWrapSymbols;
BOOL bMatchBraces;
BOOL bAutoIndent;
BOOL bAutoCloseTags;
BOOL bShowIndentGuides;
BOOL bHiliteCurrentLine;
BOOL bTabsAsSpaces;
BOOL bTabsAsSpacesG;
BOOL bTabIndents;
BOOL bTabIndentsG;
BOOL bBackspaceUnindents;
int iTabWidth;
int iTabWidthG;
int iIndentWidth;
int iIndentWidthG;
BOOL bMarkLongLines;
int iLongLinesLimit;
int iLongLinesLimitG;
int iLongLineMode;
int iWrapCol = 0;
BOOL bShowSelectionMargin;
BOOL bShowLineNumbers;
int iMarkOccurrences;
BOOL bMarkOccurrencesMatchCase;
BOOL bMarkOccurrencesMatchWords;
BOOL bAutoCompleteWords;
int iAutoCDefaultShowItemCount = 15;
int iAutoCMinWordLength = 1;
int iAutoCMinNumberLength = 3;
BOOL bAutoCIncludeDocWord = TRUE;
BOOL bAutoCloseBracesQuotes;
BOOL bShowCodeFolding;
BOOL bShowCallTips = FALSE;
int iCallTipsWaitTime = 500; // 500 ms
BOOL bViewWhiteSpace;
BOOL bViewEOLs;
int iDefaultEncoding;
BOOL bSkipUnicodeDetection;
BOOL bLoadASCIIasUTF8;
BOOL bLoadNFOasOEM;
BOOL bNoEncodingTags;
int iSrcEncoding = -1;
int iWeakSrcEncoding = -1;
int iDefaultEOLMode;
BOOL bFixLineEndings;
BOOL bAutoStripBlanks;
int iPrintHeader;
int iPrintFooter;
int iPrintColor;
int iPrintZoom;
RECT pagesetupMargin;
BOOL bSaveBeforeRunningTools;
int iFileWatchingMode;
BOOL bResetFileWatching;
DWORD dwFileCheckInverval;
DWORD dwAutoReloadTimeout;
int iEscFunction;
BOOL bAlwaysOnTop;
BOOL bMinimizeToTray;
BOOL bTransparentMode;
BOOL bTransparentModeAvailable;
BOOL bShowToolbar;
BOOL bShowStatusbar;
typedef struct _wi {
int x;
int y;
int cx;
int cy;
int max;
} WININFO;
WININFO wi;
BOOL bStickyWinPos;
BOOL bIsAppThemed;
int cyReBar;
int cyReBarFrame;
int cxEditFrame;
int cyEditFrame;
int cxEncodingDlg;
int cyEncodingDlg;
int cxRecodeDlg;
int cyRecodeDlg;
int cxFileMRUDlg;
int cyFileMRUDlg;
int cxOpenWithDlg;
int cyOpenWithDlg;
int cxFavoritesDlg;
int cyFavoritesDlg;
int xFindReplaceDlg;
int yFindReplaceDlg;
LPWSTR lpFileList[32];
int cFileList = 0;
int cchiFileList = 0;
LPWSTR lpFileArg = NULL;
LPWSTR lpSchemeArg = NULL;
LPWSTR lpMatchArg = NULL;
LPWSTR lpEncodingArg = NULL;
LPMRULIST pFileMRU;
LPMRULIST mruFind;
LPMRULIST mruReplace;
DWORD dwLastIOError;
WCHAR szCurFile[MAX_PATH + 40];
FILEVARS fvCurFile;
BOOL bModified;
BOOL bReadOnly = FALSE;
int iOriginalEncoding;
int iEOLMode;
int iEncoding;
int iDefaultCodePage;
int iDefaultCharSet;
int iInitialLine;
int iInitialColumn;
int iInitialLexer;
BOOL bLastCopyFromMe = FALSE;
DWORD dwLastCopyTime;
UINT uidsAppTitle = IDS_APPTITLE;
WCHAR szTitleExcerpt[128] = L"";
int fKeepTitleExcerpt = 0;
HANDLE hChangeHandle = NULL;
BOOL bRunningWatch = FALSE;
BOOL dwChangeNotifyTime = 0;
UINT msgTaskbarCreated = 0;
HMODULE hModUxTheme = NULL;
WIN32_FIND_DATA fdCurFile;
static EDITFINDREPLACE efrData;
UINT cpLastFind = 0;
BOOL bReplaceInitialized = FALSE;
extern NP2ENCODING mEncoding[];
int iLineEndings[3] = {
SC_EOL_CRLF,
SC_EOL_LF,
SC_EOL_CR
};
WCHAR wchPrefixSelection[256] = L"";
WCHAR wchAppendSelection[256] = L"";
WCHAR wchPrefixLines[256] = L"";
WCHAR wchAppendLines[256] = L"";
int iSortOptions = 0;
int iAlignMode = 0;
int iMatchesCount = 0;
int iAutoCItemCount = 0;
BOOL fIsElevated = FALSE;
WCHAR wchWndClass[16] = WC_NOTEPAD2;
HINSTANCE g_hInstance;
HANDLE g_hDefaultHeap;
HANDLE g_hScintilla;
UINT16 g_uWinVer;
WCHAR g_wchAppUserModelID[38] = L"";
WCHAR g_wchWorkingDirectory[MAX_PATH] = L"";
#define NP2_BookmarkLineForeColor (0xff << 8)
#define NP2_BookmarkLineColorAlpha 40
#ifdef BOOKMARK_EDITION
//Graphics for bookmark indicator
/* XPM */
static char *bookmark_pixmap[] = {
"11 11 44 1",
" c #EBE9ED",
". c #E5E3E7",
"+ c #767C6D",
"@ c #2A3120",
"# c #1B2312",
"$ c #333B28",
"% c #E3E1E5",
"& c #D8D6DA",
"* c #444D38",
"= c #3F5C19",
"- c #63AD00",
"; c #73C900",
"> c #64AF00",
", c #3D5718",
"' c #3E4634",
") c #7B8172",
"! c #42601A",
"~ c #74CB00",
"{ c #71C600",
"] c #3A5317",
"^ c #707668",
"/ c #3F4931",
"( c #262C1D",
"_ c #2F3A1E",
": c #72C700",
"< c #74CA00",
"[ c #0E1109",
"} c #3C462F",
"| c #62AC00",
"1 c #21271A",
"2 c #7A8071",
"3 c #405D19",
"4 c #3D5A18",
"5 c #D9D7DB",
"6 c #4E5841",
"7 c #72C800",
"8 c #63AC00",
"9 c #3F5B19",
"0 c #3D4533",
"a c #DFDDE0",
"b c #353E29",
"c c #29331B",
"d c #7B8272",
"e c #DDDBDF",
" ",
" .+@#$+% ",
" &*=-;>, ' ",
")!~~~~{]^ ",
" /-~~~~~>(",
" _:~~~~~<[ ",
" }|~~~~~|1 ",
" 23~~~~;4+ ",
" 56=|7890 ",
" a2bc}de ",
" "
};
#endif
//=============================================================================
//
// Flags
//
int flagNoReuseWindow = 0;
int flagReuseWindow = 0;
int flagMultiFileArg = 0;
int flagSingleFileInstance = 1;
int flagStartAsTrayIcon = 0;
int flagAlwaysOnTop = 0;
int flagRelativeFileMRU = 0;
int flagPortableMyDocs = 0;
int flagNoFadeHidden = 0;
int flagToolbarLook = 0;
int flagSimpleIndentGuides = 0;
int fNoHTMLGuess = 0;
int fNoCGIGuess = 0;
int fNoFileVariables = 0;
int flagPosParam = 0;
int flagDefaultPos = 0;
int flagNewFromClipboard = 0;
int flagPasteBoard = 0;
int flagSetEncoding = 0;
int flagSetEOLMode = 0;
int flagJumpTo = 0;
int flagMatchText = 0;
int flagChangeNotify = 0;
int flagLexerSpecified = 0;
int flagQuietCreate = 0;
int flagUseSystemMRU = 0;
int flagRelaunchElevated = 0;
int flagDisplayHelp = 0;
//==============================================================================
//
// Folding Functions
//
//
#include "Edit_Fold.c"
//=============================================================================
//
// WinMain()
//
//
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow) {
MSG msg;
HWND hwnd;
HACCEL hAccMain;
HACCEL hAccFindReplace;
INITCOMMONCONTROLSEX icex;
//HMODULE hSciLexer;
WCHAR wchWorkingDirectory[MAX_PATH];
// Set global variable g_hInstance
g_hInstance = hInstance;
// Set the Windows version global variable
g_uWinVer = LOWORD(GetVersion());
g_uWinVer = MAKEWORD(HIBYTE(g_uWinVer), LOBYTE(g_uWinVer));
g_hDefaultHeap = GetProcessHeap();
#ifdef NDEBUG
if (IsWinXPAndAbove()) {
// Enable the low-fragmenation heap (LFH).
ULONG HeapInformation = 2;//HEAP_LFH;
HeapSetInformation(g_hDefaultHeap, HeapCompatibilityInformation, &HeapInformation, sizeof(HeapInformation));
// Enable heap terminate-on-corruption.
HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
}
#endif
// Don't keep working directory locked
GetCurrentDirectory(COUNTOF(g_wchWorkingDirectory), g_wchWorkingDirectory);
GetModuleFileName(NULL, wchWorkingDirectory, COUNTOF(wchWorkingDirectory));
PathRemoveFileSpec(wchWorkingDirectory);
SetCurrentDirectory(wchWorkingDirectory);
SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
// check if running at least on Windows 2000
if (!IsWin2KAndAbove()) {
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
ERROR_OLD_WIN_VERSION,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPWSTR)&lpMsgBuf,
0,
NULL);
MessageBox(NULL, (LPCWSTR)lpMsgBuf, L"Notepad2", MB_OK | MB_ICONEXCLAMATION);
LocalFree(lpMsgBuf);
return 0;
}
// Check if running with elevated privileges
fIsElevated = IsElevated();
// Default Encodings (may already be used for command line parsing)
Encoding_InitDefaults();
// Command Line, Ini File and Flags
ParseCommandLine();
FindIniFile();
TestIniFile();
CreateIniFile();
LoadFlags();
// set AppUserModelID
PrivateSetCurrentProcessExplicitAppUserModelID(g_wchAppUserModelID);
// Command Line Help Dialog
if (flagDisplayHelp) {
DisplayCmdLineHelp(NULL);
return 0;
}
// Adapt window class name
if (fIsElevated) {
StrCat(wchWndClass, L"U");
}
if (flagPasteBoard) {
StrCat(wchWndClass, L"B");
}
// Relaunch with elevated privileges
if (RelaunchElevated()) {
return 0;
}
// Try to run multiple instances
if (RelaunchMultiInst()) {
return 0;
}
// Try to activate another window
if (ActivatePrevInst()) {
return 0;
}
// Init OLE and Common Controls
OleInitialize(NULL);
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_WIN95_CLASSES | ICC_COOL_CLASSES | ICC_BAR_CLASSES | ICC_USEREX_CLASSES;
InitCommonControlsEx(&icex);
msgTaskbarCreated = RegisterWindowMessage(L"TaskbarCreated");
hModUxTheme = LoadLibrary(L"uxtheme.dll");
Scintilla_RegisterClasses(hInstance);
// Load Settings
LoadSettings();
if (!InitApplication(hInstance)) {
OleUninitialize();
return FALSE;
}
if ((hwnd = InitInstance(hInstance, lpCmdLine, nCmdShow)) == NULL) {
OleUninitialize();
return FALSE;
}
hAccMain = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDR_MAINWND));
hAccFindReplace = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDR_ACCFINDREPLACE));
while (GetMessage(&msg, NULL, 0, 0)) {
if (IsWindow(hDlgFindReplace) && (msg.hwnd == hDlgFindReplace || IsChild(hDlgFindReplace, msg.hwnd)))
if (TranslateAccelerator(hDlgFindReplace, hAccFindReplace, &msg) || IsDialogMessage(hDlgFindReplace, &msg)) {
continue;
}
if (!TranslateAccelerator(hwnd, hAccMain, &msg)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
// Save Settings is done elsewhere
Scintilla_ReleaseResources();
UnregisterClass(wchWndClass, hInstance);
if (hModUxTheme) {
FreeLibrary(hModUxTheme);
}
OleUninitialize();
return (int)(msg.wParam);
}
//=============================================================================
//
// InitApplication()
//
//
BOOL InitApplication(HINSTANCE hInstance) {
WNDCLASS wc;
wc.style = CS_BYTEALIGNWINDOW | CS_DBLCLKS;
wc.lpfnWndProc = (WNDPROC)MainWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDR_MAINWND));
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MAINWND);
wc.lpszClassName = wchWndClass;
return RegisterClass(&wc);
}
//=============================================================================
//
// InitInstance()
//
//
HWND InitInstance(HINSTANCE hInstance, LPSTR pszCmdLine, int nCmdShow) {
RECT rc = { wi.x, wi.y, wi.x + wi.cx, wi.y + wi.cy };
RECT rc2;
MONITORINFO mi;
BOOL bFileLoadCalled = FALSE;
HMONITOR hMonitor = MonitorFromRect(&rc, MONITOR_DEFAULTTONEAREST);
mi.cbSize = sizeof(mi);
GetMonitorInfo(hMonitor, &mi);
if (flagDefaultPos == 1) {
wi.x = wi.y = wi.cx = wi.cy = CW_USEDEFAULT;
wi.max = 0;
} else if (flagDefaultPos >= 4) {
SystemParametersInfo(SPI_GETWORKAREA, 0, &rc, 0);
if (flagDefaultPos & 8) {
wi.x = (rc.right - rc.left) / 2;
} else {
wi.x = rc.left;
}
wi.cx = rc.right - rc.left;
if (flagDefaultPos & (4 | 8)) {
wi.cx /= 2;
}
if (flagDefaultPos & 32) {
wi.y = (rc.bottom - rc.top) / 2;
} else {
wi.y = rc.top;
}
wi.cy = rc.bottom - rc.top;
if (flagDefaultPos & (16 | 32)) {
wi.cy /= 2;
}
if (flagDefaultPos & 64) {
wi.x = rc.left;
wi.y = rc.top;
wi.cx = rc.right - rc.left;
wi.cy = rc.bottom - rc.top;
}
if (flagDefaultPos & 128) {
wi.x += (flagDefaultPos & 8) ? 4 : 8;
wi.cx -= (flagDefaultPos & (4 | 8)) ? 12 : 16;
wi.y += (flagDefaultPos & 32) ? 4 : 8;
wi.cy -= (flagDefaultPos & (16 | 32)) ? 12 : 16;
}
} else if (flagDefaultPos == 2 || flagDefaultPos == 3 ||
wi.x == CW_USEDEFAULT || wi.y == CW_USEDEFAULT ||
wi.cx == CW_USEDEFAULT || wi.cy == CW_USEDEFAULT) {
// default window position
SystemParametersInfo(SPI_GETWORKAREA, 0, &rc, 0);
wi.y = rc.top + 16;
wi.cy = rc.bottom - rc.top - 32;
wi.cx = min_i(rc.right - rc.left - 32, wi.cy);
wi.x = (flagDefaultPos == 3) ? rc.left + 16 : rc.right - wi.cx - 16;
} else {
// fit window into working area of current monitor
wi.x += (mi.rcWork.left - mi.rcMonitor.left);
wi.y += (mi.rcWork.top - mi.rcMonitor.top);
if (wi.x < mi.rcWork.left) {
wi.x = mi.rcWork.left;
}
if (wi.y < mi.rcWork.top) {
wi.y = mi.rcWork.top;
}
if (wi.x + wi.cx > mi.rcWork.right) {
wi.x -= (wi.x + wi.cx - mi.rcWork.right);
if (wi.x < mi.rcWork.left) {
wi.x = mi.rcWork.left;
}
if (wi.x + wi.cx > mi.rcWork.right) {
wi.cx = mi.rcWork.right - wi.x;
}
}
if (wi.y + wi.cy > mi.rcWork.bottom) {
wi.y -= (wi.y + wi.cy - mi.rcWork.bottom);
if (wi.y < mi.rcWork.top) {
wi.y = mi.rcWork.top;
}
if (wi.y + wi.cy > mi.rcWork.bottom) {
wi.cy = mi.rcWork.bottom - wi.y;
}
}
SetRect(&rc, wi.x, wi.y, wi.x + wi.cx, wi.y + wi.cy);
if (!IntersectRect(&rc2, &rc, &mi.rcWork)) {
wi.y = mi.rcWork.top + 16;
wi.cy = mi.rcWork.bottom - mi.rcWork.top - 32;
wi.cx = min_i(mi.rcWork.right - mi.rcWork.left - 32, wi.cy);
wi.x = mi.rcWork.right - wi.cx - 16;
}
}
hwndMain = CreateWindowEx(
0,
wchWndClass,
L"Notepad2",
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
wi.x,
wi.y,
wi.cx,
wi.cy,
NULL,
NULL,
hInstance,
NULL);
if (wi.max) {
nCmdShow = SW_SHOWMAXIMIZED;
}
if ((bAlwaysOnTop || flagAlwaysOnTop == 2) && flagAlwaysOnTop != 1) {
SetWindowPos(hwndMain, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
if (bTransparentMode) {
SetWindowTransparentMode(hwndMain, TRUE);
}
// Current file information -- moved in front of ShowWindow()
//FileLoad(TRUE, TRUE, FALSE, FALSE, L"");
//
if (!flagStartAsTrayIcon) {
ShowWindow(hwndMain, nCmdShow);
UpdateWindow(hwndMain);
} else {
ShowWindow(hwndMain, SW_HIDE); // trick ShowWindow()
ShowNotifyIcon(hwndMain, TRUE);
}
// Source Encoding
if (lpEncodingArg) {
iSrcEncoding = Encoding_MatchW(lpEncodingArg);
}
// Pathname parameter
if (lpFileArg /*&& !flagNewFromClipboard*/) {
BOOL bOpened = FALSE;
// Open from Directory
if (PathIsDirectory(lpFileArg)) {
WCHAR tchFile[MAX_PATH];
if (OpenFileDlg(hwndMain, tchFile, COUNTOF(tchFile), lpFileArg)) {
bOpened = FileLoad(FALSE, FALSE, FALSE, FALSE, tchFile);
bFileLoadCalled = TRUE;
}
} else {
if ((bOpened = FileLoad(FALSE, FALSE, FALSE, FALSE, lpFileArg)) != FALSE) {
bFileLoadCalled = TRUE;
if (flagJumpTo) { // Jump to position
EditJumpTo(hwndEdit, iInitialLine, iInitialColumn);
EditEnsureSelectionVisible(hwndEdit);
}
}
}
GlobalFree(lpFileArg);
if (bOpened) {
if (flagChangeNotify == 1) {
iFileWatchingMode = 0;
bResetFileWatching = TRUE;
InstallFileWatching(szCurFile);
} else if (flagChangeNotify == 2) {
iFileWatchingMode = 2;
bResetFileWatching = TRUE;
InstallFileWatching(szCurFile);
}
}
} else {
if (iSrcEncoding != -1) {
iEncoding = iSrcEncoding;
iOriginalEncoding = iSrcEncoding;
SendMessage(hwndEdit, SCI_SETCODEPAGE, (iEncoding == CPI_DEFAULT) ? iDefaultCodePage : SC_CP_UTF8, 0);
}
}
if (!bFileLoadCalled) {
FileLoad(TRUE, TRUE, FALSE, FALSE, L"");
}
// reset
iSrcEncoding = -1;
flagQuietCreate = 0;
fKeepTitleExcerpt = 0;
// Check for /c [if no file is specified] -- even if a file is specified
/*else */
if (flagNewFromClipboard) {
if (SendMessage(hwndEdit, SCI_CANPASTE, 0, 0)) {
BOOL bAutoIndent2 = bAutoIndent;
bAutoIndent = 0;
EditJumpTo(hwndEdit, -1, 0);
SendMessage(hwndEdit, SCI_BEGINUNDOACTION, 0, 0);
if (SendMessage(hwndEdit, SCI_GETLENGTH, 0, 0) > 0) {
SendMessage(hwndEdit, SCI_NEWLINE, 0, 0);
}
SendMessage(hwndEdit, SCI_PASTE, 0, 0);
SendMessage(hwndEdit, SCI_NEWLINE, 0, 0);
SendMessage(hwndEdit, SCI_ENDUNDOACTION, 0, 0);
bAutoIndent = bAutoIndent2;
if (flagJumpTo) {
EditJumpTo(hwndEdit, iInitialLine, iInitialColumn);
}
EditEnsureSelectionVisible(hwndEdit);
}
}
// Encoding
if (0 != flagSetEncoding) {
SendMessage(
hwndMain,
WM_COMMAND,
MAKELONG(IDM_ENCODING_ANSI + flagSetEncoding - 1, 1),
0);
flagSetEncoding = 0;
}
// EOL mode
if (0 != flagSetEOLMode) {
SendMessage(
hwndMain,
WM_COMMAND,
MAKELONG(IDM_LINEENDINGS_CRLF + flagSetEOLMode - 1, 1),
0);
flagSetEOLMode = 0;
}
// Match Text
if (flagMatchText && lpMatchArg) {
if (lstrlen(lpMatchArg) && SendMessage(hwndEdit, SCI_GETLENGTH, 0, 0)) {
UINT cp = (UINT)SendMessage(hwndEdit, SCI_GETCODEPAGE, 0, 0);
WideCharToMultiByte(cp, 0, lpMatchArg, -1, efrData.szFind, COUNTOF(efrData.szFind), NULL, NULL);
WideCharToMultiByte(CP_UTF8, 0, lpMatchArg, -1, efrData.szFindUTF8, COUNTOF(efrData.szFindUTF8), NULL, NULL);
cpLastFind = cp;
if (flagMatchText & 4) {
efrData.fuFlags |= SCFIND_REGEXP | SCFIND_POSIX;
} else if (flagMatchText & 8) {
efrData.bTransformBS = TRUE;
}
if (flagMatchText & 2) {
if (!flagJumpTo) {
EditJumpTo(hwndEdit, -1, 0);
}
EditFindPrev(hwndEdit, &efrData, FALSE);
EditEnsureSelectionVisible(hwndEdit);
} else {
if (!flagJumpTo) {
SendMessage(hwndEdit, SCI_DOCUMENTSTART, 0, 0);
}
EditFindNext(hwndEdit, &efrData, FALSE);
EditEnsureSelectionVisible(hwndEdit);
}
}
GlobalFree(lpMatchArg);
}
// Check for Paste Board option -- after loading files
if (flagPasteBoard) {
bLastCopyFromMe = TRUE;
hwndNextCBChain = SetClipboardViewer(hwndMain);
uidsAppTitle = IDS_APPTITLE_PASTEBOARD;
SetWindowTitle(hwndMain, uidsAppTitle, fIsElevated, IDS_UNTITLED, szCurFile,
iPathNameFormat, bModified || iEncoding != iOriginalEncoding,
IDS_READONLY, bReadOnly, szTitleExcerpt);
bLastCopyFromMe = FALSE;
dwLastCopyTime = 0;
SetTimer(hwndMain, ID_PASTEBOARDTIMER, 100, PasteBoardTimer);
}
// check if a lexer was specified from the command line
if (flagLexerSpecified) {
if (lpSchemeArg) {
Style_SetLexerFromName(hwndEdit, szCurFile, lpSchemeArg);
LocalFree(lpSchemeArg);
} else if (iInitialLexer >= 0 && iInitialLexer < NUMLEXERS) {
Style_SetLexerFromID(hwndEdit, iInitialLexer);
}
flagLexerSpecified = 0;
}
// If start as tray icon, set current filename as tooltip
if (flagStartAsTrayIcon) {
SetNotifyIconTitle(hwndMain);
}
UpdateToolbar();
UpdateStatusbar();
return hwndMain;
}
//=============================================================================
//
// MainWndProc()
//
// Messages are distributed to the MsgXXX-handlers
//
//
__forceinline void NP2MinimizeWind(HWND hwnd) {
MinimizeWndToTray(hwnd);
ShowNotifyIcon(hwnd, TRUE);
SetNotifyIconTitle(hwnd);
}
__forceinline void NP2RestoreWind(HWND hwnd) {
ShowNotifyIcon(hwnd, FALSE);
RestoreWndFromTray(hwnd);
ShowOwnedPopups(hwnd, TRUE);
}
__forceinline void NP2ExitWind(HWND hwnd) {
if (FileSave(FALSE, TRUE, FALSE, FALSE)) {
DestroyWindow(hwnd);
}
}
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam) {
static BOOL bShutdownOK;
switch (umsg) {
// Quickly handle painting and sizing messages, found in ScintillaWin.cxx
// Cool idea, don't know if this has any effect... ;-)
case WM_MOVE:
case WM_MOUSEACTIVATE:
case WM_NCHITTEST:
case WM_NCCALCSIZE:
case WM_NCPAINT:
case WM_PAINT:
case WM_ERASEBKGND:
case WM_NCMOUSEMOVE:
case WM_NCLBUTTONDOWN:
case WM_WINDOWPOSCHANGING:
case WM_WINDOWPOSCHANGED:
return DefWindowProc(hwnd, umsg, wParam, lParam);
case WM_CREATE:
return MsgCreate(hwnd, wParam, lParam);
case WM_DESTROY:
case WM_ENDSESSION:
if (!bShutdownOK) {
WINDOWPLACEMENT wndpl;
// Terminate file watching
InstallFileWatching(NULL);
// GetWindowPlacement
wndpl.length = sizeof(WINDOWPLACEMENT);
GetWindowPlacement(hwnd, &wndpl);
wi.x = wndpl.rcNormalPosition.left;
wi.y = wndpl.rcNormalPosition.top;
wi.cx = wndpl.rcNormalPosition.right - wndpl.rcNormalPosition.left;
wi.cy = wndpl.rcNormalPosition.bottom - wndpl.rcNormalPosition.top;
wi.max = (IsZoomed(hwnd) || (wndpl.flags & WPF_RESTORETOMAXIMIZED));
DragAcceptFiles(hwnd, FALSE);
// Terminate clipboard watching
if (flagPasteBoard) {
KillTimer(hwnd, ID_PASTEBOARDTIMER);
ChangeClipboardChain(hwnd, hwndNextCBChain);
}
// Destroy find / replace dialog
if (IsWindow(hDlgFindReplace)) {
DestroyWindow(hDlgFindReplace);
}
// call SaveSettings() when hwndToolbar is still valid
SaveSettings(FALSE);
if (lstrlen(szIniFile) != 0) {
// Cleanup unwanted MRU's
if (!bSaveRecentFiles) {
MRU_Empty(pFileMRU);
MRU_Save(pFileMRU);
} else {
MRU_MergeSave(pFileMRU, TRUE, flagRelativeFileMRU, flagPortableMyDocs);
}
MRU_Destroy(pFileMRU);
if (!bSaveFindReplace) {
MRU_Empty(mruFind);
MRU_Empty(mruReplace);
MRU_Save(mruFind);
MRU_Save(mruReplace);
} else {
MRU_MergeSave(mruFind, FALSE, FALSE, FALSE);
MRU_MergeSave(mruReplace, FALSE, FALSE, FALSE);
}
MRU_Destroy(mruFind);
MRU_Destroy(mruReplace);
}
// Remove tray icon if necessary
ShowNotifyIcon(hwnd, FALSE);
bShutdownOK = TRUE;
}
if (umsg == WM_DESTROY) {
PostQuitMessage(0);
}
break;
case WM_CLOSE:
if (bMinimizeToTray) {
NP2MinimizeWind(hwnd);
} else {
NP2ExitWind(hwnd);
}
break;
case WM_QUERYENDSESSION:
if (FileSave(FALSE, TRUE, FALSE, FALSE)) {
return TRUE;
}
return FALSE;
// Reinitialize theme-dependent values and resize windows
case WM_THEMECHANGED:
MsgThemeChanged(hwnd, wParam, lParam);
break;