/
Notepad3.c
9845 lines (8071 loc) · 314 KB
/
Notepad3.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
/******************************************************************************
* *
* *
* Notepad3 *
* *
* Notepad3.c *
* Main application window functionality *
* Based on code from Notepad2, (c) Florian Balmer 1996-2011 *
* *
* (c) Rizonesoft 2008-2017 *
* https://rizonesoft.com *
* *
* *
*******************************************************************************/
#if !defined(WINVER)
#define WINVER 0x601 /*_WIN32_WINNT_WIN7*/
#endif
#if !defined(_WIN32_WINNT)
#define _WIN32_WINNT 0x601 /*_WIN32_WINNT_WIN7*/
#endif
#if !defined(NTDDI_VERSION)
#define NTDDI_VERSION 0x06010000 /*NTDDI_WIN7*/
#endif
#define VC_EXTRALEAN 1
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
#include <commctrl.h>
#include <shlobj.h>
#include <shlwapi.h>
#include <shellapi.h>
#include <commdlg.h>
#include <stdio.h>
#include <string.h>
//#include <pathcch.h>
#include <time.h>
#include <muiload.h>
#include "scintilla.h"
#include "scilexer.h"
#include "edit.h"
#include "styles.h"
#include "dialogs.h"
#include "resource.h"
#include "../crypto/crypto.h"
#include "../uthash/utarray.h"
#include "../uthash/utlist.h"
#include "encoding.h"
#include "helpers.h"
#include "VersionEx.h"
#include "SciCall.h"
#include "notepad3.h"
/******************************************************************************
*
* Local and global Variables for Notepad3.c
*
*/
HWND g_hwndMain = NULL;
HWND g_hwndStatus = NULL;
HWND g_hwndToolbar = NULL;
HWND g_hwndDlgFindReplace = NULL;
HWND g_hwndDlgCustomizeSchemes = NULL;
HWND g_hwndReBar = NULL;
HWND hwndEditFrame = NULL;
HWND hwndNextCBChain = NULL;
HICON g_hDlgIcon = NULL;
bool g_bExternalBitmap = false;
#define INISECTIONBUFCNT 32
TBBUTTON tbbMainWnd[] = { { 0,IDT_FILE_NEW,TBSTATE_ENABLED,BTNS_BUTTON,0,0 },
{ 1,IDT_FILE_OPEN,TBSTATE_ENABLED,BTNS_BUTTON,0,0 },
{ 3,IDT_FILE_SAVE,TBSTATE_ENABLED,BTNS_BUTTON,0,0 },
{ 2,IDT_FILE_BROWSE,TBSTATE_ENABLED,BTNS_BUTTON,0,0 },
{ 0,0,0,BTNS_SEP,0,0 },
{ 4,IDT_EDIT_UNDO,TBSTATE_ENABLED,BTNS_BUTTON,0,0 },
{ 5,IDT_EDIT_REDO,TBSTATE_ENABLED,BTNS_BUTTON,0,0 },
{ 0,0,0,BTNS_SEP,0,0 },
{ 6,IDT_EDIT_CUT,TBSTATE_ENABLED,BTNS_BUTTON,0,0 },
{ 7,IDT_EDIT_COPY,TBSTATE_ENABLED,BTNS_BUTTON,0,0 },
{ 8,IDT_EDIT_PASTE,TBSTATE_ENABLED,BTNS_BUTTON,0,0 },
{ 0,0,0,BTNS_SEP,0,0 },
{ 9,IDT_EDIT_FIND,TBSTATE_ENABLED,BTNS_BUTTON,0,0 },
{ 10,IDT_EDIT_REPLACE,TBSTATE_ENABLED,BTNS_BUTTON,0,0 },
{ 0,0,0,BTNS_SEP,0,0 },
{ 11,IDT_VIEW_WORDWRAP,TBSTATE_ENABLED,BTNS_BUTTON,0,0 },
{ 0,0,0,BTNS_SEP,0,0 },
{ 23,IDT_VIEW_TOGGLEFOLDS,TBSTATE_ENABLED,BTNS_BUTTON,0,0 },
{ 25,IDT_VIEW_TOGGLE_VIEW,TBSTATE_ENABLED,BTNS_BUTTON,0,0 },
{ 0,0,0,BTNS_SEP,0,0 },
{ 21,IDT_FILE_OPENFAV,TBSTATE_ENABLED,BTNS_BUTTON,0,0 },
{ 22,IDT_FILE_ADDTOFAV,TBSTATE_ENABLED,BTNS_BUTTON,0,0 },
{ 0,0,0,BTNS_SEP,0,0 },
{ 12,IDT_VIEW_ZOOMIN,TBSTATE_ENABLED,BTNS_BUTTON,0,0 },
{ 13,IDT_VIEW_ZOOMOUT,TBSTATE_ENABLED,BTNS_BUTTON,0,0 },
{ 0,0,0,BTNS_SEP,0,0 },
{ 26,IDT_VIEW_CHASING_DOCTAIL,TBSTATE_ENABLED,BTNS_BUTTON,0,0 },
{ 0,0,0,BTNS_SEP,0,0 },
{ 14,IDT_VIEW_SCHEME,TBSTATE_ENABLED,BTNS_BUTTON,0,0 },
{ 0,0,0,BTNS_SEP,0,0 },
{ 24,IDT_FILE_LAUNCH,TBSTATE_ENABLED,BTNS_BUTTON,0,0 },
{ 0,0,0,BTNS_SEP,0,0 },
{ 16,IDT_FILE_EXIT,TBSTATE_ENABLED,BTNS_BUTTON,0,0 },
{ 0,0,0,BTNS_SEP,0,0 },
{ 15,IDT_VIEW_SCHEMECONFIG,TBSTATE_ENABLED,BTNS_BUTTON,0,0 },
{ 0,0,0,BTNS_SEP,0,0 },
{ 17,IDT_FILE_SAVEAS,TBSTATE_ENABLED,BTNS_BUTTON,0,0 },
{ 18,IDT_FILE_SAVECOPY,TBSTATE_ENABLED,BTNS_BUTTON,0,0 },
{ 19,IDT_EDIT_CLEAR,TBSTATE_ENABLED,BTNS_BUTTON,0,0 },
{ 20,IDT_FILE_PRINT,TBSTATE_ENABLED,BTNS_BUTTON,0,0 }
};
#define NUMTOOLBITMAPS 27
#define NUMINITIALTOOLS 33
#define TBBUTTON_DEFAULT_IDS L"1 2 4 3 0 5 6 0 7 8 9 0 10 11 0 12 0 24 26 0 22 23 0 13 14 0 27 0 15 0 25 0 17"
WCHAR g_wchIniFile[MAX_PATH] = { L'\0' };
WCHAR g_wchIniFile2[MAX_PATH] = { L'\0' };
static WCHAR g_szTmpFilePath[MAX_PATH] = { L'\0' };
static bool g_bSaveSettings;
static bool g_bEnableSaveSettings;
bool g_bIniFileFromScratch = false;
bool g_bSaveRecentFiles;
bool g_bPreserveCaretPos;
bool g_bSaveFindReplace;
bool g_bFindReplCopySelOrClip = true;
WCHAR g_tchPrefLngLocName[MINI_BUFFER];
LANGID g_iPrefLngLocID = MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT);
HMODULE g_hLngResContainer = NULL;
#define LNG_AVAILABLE_COUNT 6
static WCHAR* const g_tchAvailableLanguages = L"af-ZA de-DE es-ES en-GB fr-FR nl-NL"; // en-US internal
static LANGID const g_iAvailableLanguages[LNG_AVAILABLE_COUNT] = { 1078, 1031, 3082, 2057, 1036, 1043 }; // 1033 internal
WCHAR g_tchFileDlgFilters[XXXL_BUFFER] = { L'\0' };
WCHAR g_tchLastSaveCopyDir[MAX_PATH] = { L'\0' };
WCHAR g_tchOpenWithDir[MAX_PATH] = { L'\0' };
WCHAR g_tchFavoritesDir[MAX_PATH] = { L'\0' };
WCHAR g_tchAdministrationExe[MAX_PATH] = { L'\0' };
static WCHAR g_tchDefaultExtension[64] = { L'\0' };
static WCHAR g_tchDefaultDir[MAX_PATH] = { L'\0' };
static WCHAR g_tchToolbarButtons[MIDSZ_BUFFER] = { L'\0' };
static WCHAR g_tchStatusbarPrefixes[MIDSZ_BUFFER] = { L'\0' };
static prefix_t g_mxStatusBarPrefix[STATUS_SECTOR_COUNT];
static WCHAR g_tchStatusbarSections[SMALL_BUFFER] = { L'\0' };
static int g_iStatusbarSections[STATUS_SECTOR_COUNT] = SBS_INIT_MINUS;
static bool g_iStatusbarVisible[STATUS_SECTOR_COUNT] = SBS_INIT_ZERO;
static int g_vSBSOrder[STATUS_SECTOR_COUNT] = SBS_INIT_MINUS;
static WCHAR g_tchStatusbarWidthSpec[SMALL_BUFFER] = { L'\0' };
static int g_iStatusbarWidthSpec[STATUS_SECTOR_COUNT] = SBS_INIT_ZERO;
static WCHAR g_tchToolbarBitmap[MAX_PATH] = { L'\0' };
static WCHAR g_tchToolbarBitmapHot[MAX_PATH] = { L'\0' };
static WCHAR g_tchToolbarBitmapDisabled[MAX_PATH] = { L'\0' };
int iPathNameFormat;
bool g_bWordWrap;
bool bWordWrapG;
int iWordWrapMode;
int iWordWrapIndent;
int iWordWrapSymbols;
bool bShowWordWrapSymbols;
bool bMatchBraces;
bool bAutoIndent;
bool bAutoCloseTags;
bool bShowIndentGuides;
bool bHiliteCurrentLine;
bool g_bHyperlinkHotspot;
bool bScrollPastEOF;
bool g_bTabsAsSpaces;
bool bTabsAsSpacesG;
bool g_bTabIndents;
bool bTabIndentsG;
bool bBackspaceUnindents;
int g_iTabWidth;
int iTabWidthG;
int g_iIndentWidth;
int iIndentWidthG;
bool bMarkLongLines;
int g_iLongLinesLimit;
int iLongLinesLimitG;
int iLongLineMode;
int iWrapCol = 0;
bool g_bShowSelectionMargin;
bool bShowLineNumbers;
int iReplacedOccurrences;
int g_iMarkOccurrences;
int g_iMarkOccurrencesCount;
int g_iMarkOccurrencesMaxCount;
bool g_bMarkOccurrencesMatchVisible;
bool bMarkOccurrencesMatchCase;
bool bMarkOccurrencesMatchWords;
bool bMarkOccurrencesCurrentWord;
bool bUseOldStyleBraceMatching;
bool bAutoCompleteWords;
bool bAccelWordNavigation;
bool bDenyVirtualSpaceAccess;
bool g_bCodeFoldingAvailable;
bool g_bShowCodeFolding;
bool bViewWhiteSpace;
bool bViewEOLs;
bool bUseDefaultForFileEncoding;
bool bSkipUnicodeDetection;
bool bSkipANSICodePageDetection;
bool bLoadASCIIasUTF8;
bool bLoadNFOasOEM;
bool bNoEncodingTags;
bool bFixLineEndings;
bool bAutoStripBlanks;
int iPrintHeader;
int iPrintFooter;
int iPrintColor;
int iPrintZoom;
RECT pagesetupMargin;
bool bSaveBeforeRunningTools;
int g_iFileWatchingMode;
bool g_bResetFileWatching;
DWORD dwFileCheckInverval;
DWORD dwAutoReloadTimeout;
int iEscFunction;
bool bAlwaysOnTop;
bool bMinimizeToTray;
bool bTransparentMode;
bool bTransparentModeAvailable;
bool bShowToolbar;
bool bShowStatusbar;
int iHighDpiToolBar;
int iUpdateDelayHyperlinkStyling;
int iUpdateDelayMarkAllCoccurrences;
int iCurrentLineHorizontalSlop = 0;
int iCurrentLineVerticalSlop = 0;
bool g_bChasingDocTail = false;
static int g_iSciDirectWriteTech = 0;
static bool bEnableBidirectionalSupport = false;
const int DirectWriteTechnology[4] = {
SC_TECHNOLOGY_DEFAULT
, SC_TECHNOLOGY_DIRECTWRITE
, SC_TECHNOLOGY_DIRECTWRITERETAIN
, SC_TECHNOLOGY_DIRECTWRITEDC
};
int g_iSciFontQuality;
const int FontQuality[4] = {
SC_EFF_QUALITY_DEFAULT
, SC_EFF_QUALITY_NON_ANTIALIASED
, SC_EFF_QUALITY_ANTIALIASED
, SC_EFF_QUALITY_LCD_OPTIMIZED
};
static WININFO g_WinInfo = INIT_WININFO;
static int g_WinCurrentWidth = 0;
bool g_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;
int xCustomSchemesDlg;
int yCustomSchemesDlg;
LPWSTR lpFileList[32] = { NULL };
int cFileList = 0;
int cchiFileList = 0;
LPWSTR lpFileArg = NULL;
LPWSTR lpSchemeArg = NULL;
LPWSTR lpMatchArg = NULL;
LPWSTR lpEncodingArg = NULL;
LPMRULIST g_pFileMRU;
LPMRULIST g_pMRUfind;
LPMRULIST g_pMRUreplace;
DWORD dwLastIOError;
int g_iDefaultNewFileEncoding;
int g_iDefaultCharSet;
int g_iEOLMode;
int g_iDefaultEOLMode;
int iInitialLine;
int iInitialColumn;
int iInitialLexer;
bool bLastCopyFromMe = false;
DWORD dwLastCopyTime;
UINT uidsAppTitle = IDS_MUI_APPTITLE;
WCHAR szTitleExcerpt[MIDSZ_BUFFER] = { L'\0' };
int fKeepTitleExcerpt = 0;
HANDLE hChangeHandle = NULL;
bool dwChangeNotifyTime = 0;
WIN32_FIND_DATA fdCurFile;
UINT msgTaskbarCreated = 0;
HMODULE hModUxTheme = NULL;
HMODULE hRichEdit = NULL;
static bool g_bRunningWatch = false;
static EDITFINDREPLACE g_efrData = EFR_INIT_DATA;
bool bReplaceInitialized = false;
int iLineEndings[3] = {
SC_EOL_CRLF,
SC_EOL_LF,
SC_EOL_CR
};
WCHAR wchPrefixSelection[256] = { L'\0' };
WCHAR wchAppendSelection[256] = { L'\0' };
WCHAR wchPrefixLines[256] = { L'\0' };
WCHAR wchAppendLines[256] = { L'\0' };
static int iSortOptions = 0;
static int iAlignMode = 0;
bool flagIsElevated = false;
WCHAR wchWndClass[16] = L"" APPNAME;
HINSTANCE g_hInstance = NULL;
HANDLE g_hScintilla = NULL;
HANDLE g_hwndEdit = NULL;
WCHAR g_wchAppUserModelID[32] = { L'\0' };
WCHAR g_wchWorkingDirectory[MAX_PATH+2] = { L'\0' };
WCHAR g_wchCurFile[FILE_ARG_BUF] = { L'\0' };
FILEVARS fvCurFile;
bool g_bFileReadOnly = false;
// temporary line buffer for fast line ops
static char g_pTempLineBufferMain[TEMPLINE_BUFFER];
// declarations
// undo / redo selections
static UT_icd UndoRedoSelection_icd = { sizeof(UndoRedoSelection_t), NULL, NULL, NULL };
static UT_array* UndoRedoSelectionUTArray = NULL;
static bool __fastcall _InUndoRedoTransaction();
static void __fastcall _SaveRedoSelection(int token);
static int __fastcall _SaveUndoSelection();
static int __fastcall _UndoRedoActionMap(int token, UndoRedoSelection_t* const selection);
static CLIPFORMAT cfDrpF = CF_HDROP;
static POINTL ptDummy = { 0, 0 };
static PDROPTARGET pDropTarget = NULL;
static DWORD DropFilesProc(CLIPFORMAT cf, HGLOBAL hData, HWND hWnd, DWORD dwKeyState, POINTL pt, void *pUserData);
//=============================================================================
//
// IgnoreNotifyChangeEvent(), ObserveNotifyChangeEvent(), CheckNotifyChangeEvent()
//
static volatile LONG iNotifyChangeStackCounter = 0L;
bool CheckNotifyChangeEvent()
{
return (InterlockedExchange(&iNotifyChangeStackCounter, iNotifyChangeStackCounter) == 0L);
}
void IgnoreNotifyChangeEvent()
{
InterlockedIncrement(&iNotifyChangeStackCounter);
}
void ObserveNotifyChangeEvent()
{
if (!CheckNotifyChangeEvent()) {
InterlockedDecrement(&iNotifyChangeStackCounter);
}
if (CheckNotifyChangeEvent()) {
UpdateToolbar();
UpdateStatusbar(false);
UpdateLineNumberWidth();
}
}
// SCN_UPDATEUI notification
#define SC_UPDATE_NP3_INTERNAL_NOTIFY (SC_UPDATE_H_SCROLL << 1)
//=============================================================================
//
// Delay Message Queue Handling (TODO: MultiThreading)
//
static CmdMessageQueue_t* MessageQueue = NULL;
// ----------------------------------------------------------------------------
static int msgcmp(void* mqc1, void* mqc2)
{
CmdMessageQueue_t* const pMQC1 = (CmdMessageQueue_t*)mqc1;
CmdMessageQueue_t* const pMQC2 = (CmdMessageQueue_t*)mqc2;
if ((pMQC1->hwnd == pMQC2->hwnd)
&& (pMQC1->cmd == pMQC2->cmd)
&& (pMQC1->wparam == pMQC2->wparam)
&& (pMQC1->lparam == pMQC2->lparam))
{
return 0;
}
return 1;
}
// ----------------------------------------------------------------------------
#define _MQ_ms(T) ((T) / USER_TIMER_MINIMUM)
static void __fastcall _MQ_AppendCmd(CmdMessageQueue_t* const pMsgQCmd, int cycles)
{
CmdMessageQueue_t* pmqc = NULL;
DL_SEARCH(MessageQueue, pmqc, pMsgQCmd, msgcmp);
if (!pmqc) { // NOT found
pmqc = AllocMem(sizeof(CmdMessageQueue_t), HEAP_ZERO_MEMORY);
pmqc->hwnd = pMsgQCmd->hwnd;
pmqc->cmd = pMsgQCmd->cmd;
pmqc->wparam = pMsgQCmd->wparam;
pmqc->lparam = pMsgQCmd->lparam;
pmqc->delay = cycles;
DL_APPEND(MessageQueue, pmqc);
}
if (cycles < 2) {
pmqc->delay = -1; // execute now (do not use PostMessage() here)
SendMessage(pMsgQCmd->hwnd, pMsgQCmd->cmd, pMsgQCmd->wparam, pMsgQCmd->lparam);
}
else {
pmqc->delay = (pmqc->delay + cycles) / 2; // increase delay
}
}
// ----------------------------------------------------------------------------
static void __fastcall _MQ_RemoveCmd(CmdMessageQueue_t* const pMsgQCmd)
{
CmdMessageQueue_t* pmqc;
DL_FOREACH(MessageQueue, pmqc)
{
if ((pMsgQCmd->hwnd == pmqc->hwnd)
&& (pMsgQCmd->cmd == pmqc->cmd)
&& (pMsgQCmd->wparam == pmqc->wparam))
{
pmqc->delay = -1;
}
}
}
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
//
// called by Timer(IDT_TIMER_MRKALL)
//
static void CALLBACK MQ_ExecuteNext(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
UNUSED(hwnd); // must be main wnd
UNUSED(uMsg); // must be WM_TIMER
UNUSED(idEvent); // must be IDT_TIMER_MRKALL
UNUSED(dwTime); // This is the value returned by the GetTickCount function
CmdMessageQueue_t* pmqc;
DL_FOREACH(MessageQueue, pmqc)
{
if (pmqc->delay == 0) {
pmqc->delay = -1;
SendMessage(pmqc->hwnd, pmqc->cmd, pmqc->wparam, pmqc->lparam);
}
else if (pmqc->delay >= 0) {
pmqc->delay -= 1;
}
}
}
//=============================================================================
//
// Flags
//
int g_flagNoFadeHidden = 0;
int g_flagSimpleIndentGuides = 0;
int g_flagNoHTMLGuess = 0;
int g_flagNoCGIGuess = 0;
int g_flagNoFileVariables = 0;
int g_flagUseSystemMRU = 0;
int g_flagPrintFileAndLeave = 0;
static int g_flagNoReuseWindow = 0;
static int g_flagReuseWindow = 0;
static int g_flagMultiFileArg = 0;
static int g_flagSingleFileInstance = 0;
static int g_flagStartAsTrayIcon = 0;
static int g_flagAlwaysOnTop = 0;
static int g_flagRelativeFileMRU = 0;
static int g_flagPortableMyDocs = 0;
static int g_flagToolbarLook = 0;
static int g_flagPosParam = 0;
static int g_flagDefaultPos = 0;
static int g_flagNewFromClipboard = 0;
static int g_flagPasteBoard = 0;
static int g_flagSetEncoding = 0;
static int g_flagSetEOLMode = 0;
static int g_flagJumpTo = 0;
static int g_flagMatchText = 0;
static int g_flagChangeNotify = 0;
static int g_flagLexerSpecified = 0;
static int g_flagQuietCreate = 0;
static int g_flagRelaunchElevated = 0;
static int g_flagDisplayHelp = 0;
static int g_flagBufferFile = 0;
//==============================================================================
// decalarations
static void __fastcall _UpdateStatusbarDelayed(bool bForceRedraw);
static void __fastcall _UpdateToolbarDelayed();
static HMODULE __fastcall _LoadLanguageResources(LANGID const langID);
static bool __fastcall _RegisterWndClass(HINSTANCE hInstance);
//==============================================================================
//
// Document Modified Flag
//
//
static bool IsDocumentModified = false;
static void __fastcall _SetDocumentModified(bool bModified)
{
if (IsDocumentModified != bModified) {
IsDocumentModified = bModified;
UpdateToolbar();
UpdateStatusbar(false);
}
if (bModified) {
if (IsWindow(g_hwndDlgFindReplace)) {
SendMessage(g_hwndDlgFindReplace, WM_COMMAND, MAKELONG(IDC_DOC_MODIFIED, 1), 0);
}
}
}
//==============================================================================
//=============================================================================
//
// WinMain()
//
//
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nCmdShow)
{
UNUSED(hPrevInst);
HACCEL hAccMain;
HACCEL hAccFindReplace;
HACCEL hAccCoustomizeSchemes;
INITCOMMONCONTROLSEX icex;
//HMODULE hSciLexer;
WCHAR wchAppDir[2*MAX_PATH+4] = { L'\0' };
// Set global variable g_hInstance
g_hInstance = hInstance;
GetModuleFileName(NULL,wchAppDir,COUNTOF(wchAppDir));
PathRemoveFileSpec(wchAppDir);
PathCanonicalizeEx(wchAppDir,COUNTOF(wchAppDir));
if (!GetCurrentDirectory(COUNTOF(g_wchWorkingDirectory),g_wchWorkingDirectory)) {
StringCchCopy(g_wchWorkingDirectory,COUNTOF(g_wchWorkingDirectory),wchAppDir);
}
// Don't keep working directory locked
SetCurrentDirectory(wchAppDir);
SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOOPENFILEERRORBOX);
// check if running at least on Windows 7
if (!IsWin7()) {
GetLastErrorToMsgBox(L"WinMain", ERROR_OLD_WIN_VERSION);
return 1; // exit
}
// Check if running with elevated privileges
flagIsElevated = IsUserAdmin() || 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 (g_flagDisplayHelp) {
DisplayCmdLineHelp(NULL);
return 0;
}
// Adapt window class name
if (flagIsElevated)
StringCchCat(wchWndClass,COUNTOF(wchWndClass),L"U");
if (g_flagPasteBoard)
StringCchCat(wchWndClass,COUNTOF(wchWndClass),L"B");
// Relaunch with elevated privileges
if (RelaunchElevated(NULL))
return 0;
// Try to run multiple instances
if (RelaunchMultiInst())
return 0;
// Try to activate another window
if (ActivatePrevInst())
return 0;
// Load Settings
LoadSettings();
// ----------------------------------------------------
// MultiLingual
//
g_iPrefLngLocID = GetUserDefaultUILanguage();
bool bPrefLngNotAvail = false;
if (StringCchLenW(g_tchPrefLngLocName, COUNTOF(g_tchPrefLngLocName)) > 0)
{
DWORD dwLangID = 0;
GetLocaleInfoEx(g_tchPrefLngLocName, LOCALE_ILANGUAGE | LOCALE_RETURN_NUMBER, (LPWSTR)&dwLangID, sizeof(DWORD));
g_iPrefLngLocID = (LANGID)dwLangID;
}
g_hLngResContainer = _LoadLanguageResources(g_iPrefLngLocID);
if (!g_hLngResContainer) // fallback en-US (1033)
{
g_hLngResContainer = g_hInstance;
if (g_iPrefLngLocID != 1033) { bPrefLngNotAvail = true; }
}
// ----------------------------------------------------
// 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");
if (!IsWin8()) { hModUxTheme = LoadLibrary(L"uxtheme.dll"); }
hRichEdit = LoadLibrary(L"RICHED20.DLL"); // Use "RichEdit20W" for control in .rc
//hRichEdit = LoadLibrary(L"MSFTEDIT.DLL"); // Use "RichEdit50W" for control in .rc
if (!_RegisterWndClass(g_hInstance)) { return 1; }
Scintilla_RegisterClasses(g_hInstance);
HMENU hMainMenu = NULL;
if (g_hLngResContainer != g_hInstance) {
hMainMenu = LoadMenu(g_hLngResContainer, MAKEINTRESOURCE(IDR_MUI_MAINMENU));
if (!hMainMenu) {
GetLastErrorToMsgBox(L"LoadMenu()", 0);
}
}
HWND hwnd = InitInstance(g_hInstance, lpCmdLine, nCmdShow);
if (!hwnd) { return 1; }
if (hMainMenu) { SetMenu(hwnd, hMainMenu); }
// init DragnDrop handler
DragAndDropInit(NULL);
hAccMain = LoadAccelerators(hInstance,MAKEINTRESOURCE(IDR_MAINWND));
hAccFindReplace = LoadAccelerators(hInstance,MAKEINTRESOURCE(IDR_ACCFINDREPLACE));
hAccCoustomizeSchemes = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDR_ACCCUSTOMSCHEMES));
SetTimer(hwnd, IDT_TIMER_MRKALL, USER_TIMER_MINIMUM, (TIMERPROC)MQ_ExecuteNext);
if (bPrefLngNotAvail) {
InfoBoxLng(MBWARN, L"MsgPrefLanguageNotAvailable", IDS_WARN_PREF_LNG_NOT_AVAIL, g_tchPrefLngLocName);
}
MSG msg;
while (GetMessage(&msg,NULL,0,0))
{
if (IsWindow(g_hwndDlgFindReplace) && ((msg.hwnd == g_hwndDlgFindReplace) || IsChild(g_hwndDlgFindReplace, msg.hwnd)))
{
const int iTr = TranslateAccelerator(g_hwndDlgFindReplace, hAccFindReplace, &msg);
if (iTr || IsDialogMessage(g_hwndDlgFindReplace, &msg))
continue;
}
if (IsWindow(g_hwndDlgCustomizeSchemes) && ((msg.hwnd == g_hwndDlgCustomizeSchemes) || IsChild(g_hwndDlgCustomizeSchemes, msg.hwnd))) {
const int iTr = TranslateAccelerator(g_hwndDlgCustomizeSchemes, hAccCoustomizeSchemes, &msg);
if (iTr || IsDialogMessage(g_hwndDlgCustomizeSchemes, &msg))
continue;
}
if (!TranslateAccelerator(hwnd,hAccMain,&msg)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
KillTimer(hwnd, IDT_TIMER_MRKALL);
CmdMessageQueue_t* pmqc = NULL;
CmdMessageQueue_t* dummy;
DL_FOREACH_SAFE(MessageQueue, pmqc, dummy)
{
DL_DELETE(MessageQueue, pmqc);
FreeMem(pmqc);
}
// Save Settings is done elsewhere
DestroyMenu(hMainMenu);
Scintilla_ReleaseResources();
UnregisterClass(wchWndClass, g_hInstance);
if (hModUxTheme) { FreeLibrary(hModUxTheme); }
OleUninitialize();
if (g_hLngResContainer != g_hInstance) { FreeMUILibrary(g_hLngResContainer); }
return (int)(msg.wParam);
}
//=============================================================================
//
// _RegisterWndClass()
//
//
static bool __fastcall _RegisterWndClass(HINSTANCE hInstance)
{
if (!g_hDlgIcon) {
g_hDlgIcon = LoadImage(g_hInstance, MAKEINTRESOURCE(IDR_MAINWND), IMAGE_ICON,
GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
}
WNDCLASS wc;
ZeroMemory(&wc, sizeof(WNDCLASS));
wc.style = CS_BYTEALIGNWINDOW | CS_DBLCLKS;
wc.lpfnWndProc = (WNDPROC)MainWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = g_hDlgIcon;
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MUI_MAINMENU);
wc.lpszClassName = wchWndClass;
return RegisterClass(&wc);
}
//=============================================================================
//
// _LngStrToMultiLngStr
//
//
static bool __fastcall _LngStrToMultiLngStr(WCHAR* pLngStr, WCHAR* pLngMultiStr, size_t lngMultiStrSize)
{
bool rtnVal = true;
size_t strLen = (size_t)lstrlenW(pLngStr);
if ((strLen > 0) && pLngMultiStr && (lngMultiStrSize > 0)) {
WCHAR* lngMultiStrPtr = pLngMultiStr;
WCHAR* last = pLngStr + (pLngStr[0] == 0xFEFF ? 1 : 0); // if read from unicode (UTF-16 LE) file
while (last && rtnVal) {
// make sure you validate the user input
WCHAR* next = StrNextTok(last, L",; :");
if (next) { *next = L'\0'; }
strLen = (size_t)StringCchLenW(last, LOCALE_NAME_MAX_LENGTH);
if ((strLen > 0) && IsValidLocaleName(last)) {
lngMultiStrPtr[0] = L'\0';
rtnVal &= SUCCEEDED(StringCchCatW(lngMultiStrPtr, (lngMultiStrSize - (lngMultiStrPtr - pLngMultiStr)), last));
lngMultiStrPtr += strLen + 1;
}
last = (next ? next + 1 : next);
}
if (rtnVal && (lngMultiStrSize - (lngMultiStrPtr - pLngMultiStr))) // make sure there is a double null term for the multi-string
{
lngMultiStrPtr[0] = L'\0';
}
else // fail and guard anyone whom might use the multi-string
{
lngMultiStrPtr[0] = L'\0';
lngMultiStrPtr[1] = L'\0';
}
}
return rtnVal;
}
//=============================================================================
//
// _LoadLanguageResources
//
//
static HMODULE __fastcall _LoadLanguageResources(LANGID const langID)
{
bool bLngAvailable = false;
for (int i = 0; i < COUNTOF(g_iAvailableLanguages); ++i) {
if (g_iAvailableLanguages[i] == langID) {
bLngAvailable = true;
break;
}
}
if (!bLngAvailable) { return NULL; }
WCHAR tchAvailLngs[SMALL_BUFFER] = { L'\0' };
StringCchCopyW(tchAvailLngs, SMALL_BUFFER, g_tchAvailableLanguages);
WCHAR tchUserLangMultiStrg[SMALL_BUFFER] = { L'\0' };
if (!_LngStrToMultiLngStr(tchAvailLngs, tchUserLangMultiStrg, LARGE_BUFFER))
{
GetLastErrorToMsgBox(L"_LngStrToMultiLngStr()", ERROR_MUI_INVALID_LOCALE_NAME);
return NULL;
}
// set the appropriate fallback list
DWORD langCount = 0;
// using SetProcessPreferredUILanguages is recommended for new applications (esp. multi-threaded applications)
if (!SetThreadPreferredUILanguages(MUI_LANGUAGE_NAME, tchUserLangMultiStrg, &langCount) || (langCount == 0))
{
GetLastErrorToMsgBox(L"SetProcessPreferredUILanguages()", 0);
return NULL;
}
SetThreadUILanguage(langID);
// NOTES:
// an application developer that makes the assumption the fallback list provided by the
// system / OS is entirely sufficient may or may not be making a good assumption based mostly on:
// A. your choice of languages installed with your application
// B. the languages on the OS at application install time
// C. the OS users propensity to install/uninstall language packs
// D. the OS users propensity to change language settings
// obtains access to the proper resource container
// for standard Win32 resource loading this is normally a PE module - use LoadLibraryEx
HMODULE hLangResourceContainer = LoadMUILibraryW(L"lng/np3lng.dll", MUI_LANGUAGE_NAME, langID);
//if (!hLangResourceContainer)
//{
// GetLastErrorToMsgBox(L"LoadMUILibrary", 0);
// return NULL;
//}
return hLangResourceContainer;
}
//=============================================================================
//
// WaitCursorStack
//
//
static volatile LONG iWaitCursorStackCounter = 0L;
//=============================================================================
//
// CheckWaitCursorStack()
//
//
static bool __fastcall CheckWaitCursorStack()
{
return (InterlockedExchange(&iWaitCursorStackCounter, iWaitCursorStackCounter) == 0L);
}
//=============================================================================
//
// BeginWaitCursor()
//
//
void BeginWaitCursor(LPCWSTR text)
{
if (CheckWaitCursorStack())
{
SciCall_SetCursor(SC_CURSORWAIT); // delayed to SCN_DWELLSTART
StatusSetText(g_hwndStatus, STATUS_HELP, (LPCWSTR)text);
//StatusSetTextID(g_hwndStatus, STATUS_HELP, uid);
}
InterlockedIncrement(&iWaitCursorStackCounter);
}
//=============================================================================
//
// EndWaitCursor()
//
//
void EndWaitCursor()
{
if (!CheckWaitCursorStack()) {
InterlockedDecrement(&iWaitCursorStackCounter);
}
if (CheckWaitCursorStack())
{
POINT pt;
SciCall_SetCursor(SC_CURSORNORMAL);
GetCursorPos(&pt); SetCursorPos(pt.x, pt.y);
StatusSetSimple(g_hwndStatus, false);
UpdateStatusbar(false);
}
}
//=============================================================================
//
// _InitWindowPosition()
//
//
static void __fastcall _InitWindowPosition(HWND hwnd)
{
RECT rc;
if (hwnd) {
GetWindowRect(hwnd, &rc);
}
else {
rc.left = g_WinInfo.x;
rc.top = g_WinInfo.y;
rc.right = g_WinInfo.x + g_WinInfo.cx;
rc.bottom = g_WinInfo.y + g_WinInfo.cy;
}
if (g_flagDefaultPos == 1)
{
g_WinInfo.x = g_WinInfo.y = g_WinInfo.cx = g_WinInfo.cy = CW_USEDEFAULT;
g_WinInfo.max = 0;
g_WinInfo.zoom = 0;
}
else if (g_flagDefaultPos >= 4)
{
SystemParametersInfo(SPI_GETWORKAREA, 0, &rc, 0);
if (g_flagDefaultPos & 8)
g_WinInfo.x = (rc.right - rc.left) / 2;
else
g_WinInfo.x = rc.left;
g_WinInfo.cx = rc.right - rc.left;
if (g_flagDefaultPos & (4 | 8))
g_WinInfo.cx /= 2;
if (g_flagDefaultPos & 32)
g_WinInfo.y = (rc.bottom - rc.top) / 2;
else
g_WinInfo.y = rc.top;
g_WinInfo.cy = rc.bottom - rc.top;
if (g_flagDefaultPos & (16 | 32))
g_WinInfo.cy /= 2;