forked from rizonesoft/Notepad3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStyles.c
4633 lines (3937 loc) · 157 KB
/
Styles.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 *
* *
* Styles.c *
* Scintilla Style Management *
* Based on code from Notepad2, (c) Florian Balmer 1996-2011 *
* Mostly taken from SciTE, (c) Neil Hodgson *
* *
* (c) Rizonesoft 2008-2019 *
* http://www.rizonesoft.com *
* *
* *
*******************************************************************************/
#include "Helpers.h"
#include <assert.h>
#include <commctrl.h>
#include <commdlg.h>
#include <shlobj.h>
#include <shellapi.h>
#include <shlwapi.h>
#include <stdio.h>
#include "SciLexer.h"
#include "SciXLexer.h"
#include "Edit.h"
#include "Dialogs.h"
#include "resource.h"
#include "Encoding.h"
#include "MuiLanguage.h"
#include "Notepad3.h"
#include "Config/Config.h"
#include "SciCall.h"
#include "Styles.h"
extern const int g_FontQuality[4];
extern COLORREF g_colorCustom[16];
bool ChooseFontDirectWrite(HWND hwnd, const WCHAR* localeName, DPI_T dpi, LPCHOOSEFONT lpCF);
// ----------------------------------------------------------------------------
// This array holds all the lexers...
// Don't forget to change the number of the lexer for HTML and XML
// in Notepad2.c ParseCommandLine() if you change this array!
static PEDITLEXER g_pLexArray[NUMLEXERS] =
{
&lexStandard, // Default Text
&lexStandard2nd, // 2nd Default Text
&lexTEXT, // Pure Text Files (Constants.StdDefaultLexerID = 2)
&lexANSI, // ANSI Files
&lexCONF, // Apache Config Files
&lexASM, // Assembly Script
&lexAHKL, // AutoHotkey L Script
&lexAU3, // AutoIt3 Script
&lexAVS, // AviSynth Script
&lexAwk, // Awk Script
&lexBAT, // Batch Files
&lexCS, // C# Source Code
&lexCPP, // C/C++ Source Code
&lexCmake, // Cmake Script
&lexCOFFEESCRIPT, // Coffeescript
&lexPROPS, // Configuration Files
&lexCSS, // CSS Style Sheets
&lexD, // D Source Code
&lexDIFF, // Diff Files
&lexGo, // Go Source Code
&lexINNO, // Inno Setup Script
&lexJAVA, // Java Source Code
&lexJS, // JavaScript
&lexJSON, // JSON
&lexLATEX, // LaTeX Files
&lexLUA, // Lua Script
&lexMAK, // Makefiles
&lexMARKDOWN, // Markdown
&lexMATLAB, // MATLAB
&lexNim, // Nim(rod)
&lexNSIS, // NSIS Script
&lexPAS, // Pascal Source Code
&lexPL, // Perl Script
&lexPS, // PowerShell Script
&lexPY, // Python Script
&lexRegistry, // Registry Files
&lexRC, // Resource Script
&lexR, // R Statistics Code
&lexRUBY, // Ruby Script
&lexRust, // Rust Script
&lexBASH, // Shell Script
&lexSQL, // SQL Query
&lexTCL, // Tcl Script
&lexTOML, // TOML Config Script
&lexVBS, // VBScript
&lexVHDL, // VHDL
&lexVB, // Visual Basic
&lexHTML, // Web Source Code
&lexXML, // XML Document
&lexYAML // YAML
};
// Currently used lexer
static int s_iDefaultLexer = 2; // (Constants.StdDefaultLexerID) Pure Text Files
static PEDITLEXER s_pLexCurrent = &lexStandard;
const COLORREF s_colorDefault[16] =
{
RGB(0x00, 0x00, 0x00),
RGB(0x0A, 0x24, 0x6A),
RGB(0x3A, 0x6E, 0xA5),
RGB(0x00, 0x3C, 0xE6),
RGB(0x00, 0x66, 0x33),
RGB(0x60, 0x80, 0x20),
RGB(0x64, 0x80, 0x00),
RGB(0xA4, 0x60, 0x00),
RGB(0xFF, 0xFF, 0xFF),
RGB(0xFF, 0xFF, 0xE2),
RGB(0xFF, 0xF1, 0xA8),
RGB(0xFF, 0xC0, 0x00),
RGB(0xFF, 0x40, 0x00),
RGB(0xC8, 0x00, 0x00),
RGB(0xB0, 0x00, 0xB0),
RGB(0xB2, 0x8B, 0x40)
};
static bool s_bAutoSelect = true;
#define STYLESELECTDLG_X 304
#define STYLESELECTDLG_Y 344
static int s_cxStyleSelectDlg = STYLESELECTDLG_X;
static int s_cyStyleSelectDlg = STYLESELECTDLG_Y;
//=============================================================================
typedef struct _themeFiles
{
UINT rid;
WCHAR szName[80];
WCHAR szFilePath[MAX_PATH];
} THEMEFILES, * PTHEMEFILES;
static THEMEFILES Theme_Files[] =
{
{ 0, L"Default", L"" },
{ 0, L"Standard", L"" },
{ 0, L"", L"" },
{ 0, L"", L"" },
{ 0, L"", L"" },
{ 0, L"", L"" },
{ 0, L"", L"" },
{ 0, L"", L"" },
{ 0, L"", L"" },
{ 0, L"", L"" },
{ 0, L"", L"" },
{ 0, L"", L"" },
{ 0, L"", L"" },
{ 0, L"", L"" },
{ 0, L"", L"" },
{ 0, L"", L"" },
{ 0, L"", L"" },
{ 0, L"", L"" },
{ 0, L"", L"" },
{ 0, L"", L"" },
{ 0, L"", L"" },
{ 0, L"", L"" },
{ 0, L"", L"" },
{ 0, L"", L"" },
{ 0, L"", L"" },
{ 0, L"", L"" }
};
unsigned ThemeItems_CountOf() { return COUNTOF(Theme_Files); }
static unsigned s_idxSelectedTheme = 1; // Default(0), Standard(1)
const WCHAR* const STYLING_THEME_NAME = L"ThemeFileName";
static void _FillThemesMenuTable()
{
Theme_Files[0].rid = IDM_THEMES_DEFAULT; // factory default
Theme_Files[1].rid = IDM_THEMES_FILE_ITEM; // NP3.ini settings
// names are filled by Style_InsertThemesMenu()
StringCchCopy(Theme_Files[1].szFilePath, COUNTOF(Theme_Files[1].szFilePath), Globals.IniFile);
unsigned iTheme = 1; // Standard
WCHAR tchThemeDir[MAX_PATH] = { L'\0' };
// find "themes" sub-dir (side-by-side to Notepad3.ini)
if (StrIsNotEmpty(Globals.IniFile)) {
StringCchCopy(tchThemeDir, COUNTOF(tchThemeDir), Globals.IniFile);
}
else if (StrIsNotEmpty(Globals.IniFileDefault)) {
StringCchCopy(tchThemeDir, COUNTOF(tchThemeDir), Globals.IniFileDefault);
}
if (StrIsNotEmpty(tchThemeDir)) {
PathCchRemoveFileSpec(tchThemeDir, COUNTOF(tchThemeDir));
PathCchAppend(tchThemeDir, COUNTOF(tchThemeDir), L"themes");
}
if (PathIsDirectory(tchThemeDir))
{
WCHAR tchThemePath[MAX_PATH] = { L'\0' };
StringCchCopy(tchThemePath, COUNTOF(tchThemePath), tchThemeDir);
PathCchAppend(tchThemePath, COUNTOF(tchThemePath), L"*.ini");
WIN32_FIND_DATA FindFileData;
ZeroMemory(&FindFileData, sizeof(WIN32_FIND_DATA));
HANDLE hFindFile = FindFirstFile(tchThemePath, &FindFileData);
if (hFindFile != INVALID_HANDLE_VALUE)
{
// --- fill table by directory entries ---
for (iTheme = 2; iTheme < ThemeItems_CountOf(); ++iTheme)
{
Theme_Files[iTheme].rid = (iTheme + IDM_THEMES_DEFAULT);
StringCchCopy(tchThemePath, COUNTOF(tchThemePath), PathFindFileName(FindFileData.cFileName));
PathRemoveExtension(tchThemePath);
StringCchCopy(Theme_Files[iTheme].szName, COUNTOF(Theme_Files[iTheme].szName), tchThemePath);
StringCchCopy(tchThemePath, COUNTOF(tchThemePath), tchThemeDir);
PathCchAppend(tchThemePath, COUNTOF(tchThemePath), FindFileData.cFileName);
StringCchCopy(Theme_Files[iTheme].szFilePath, COUNTOF(Theme_Files[iTheme].szFilePath), tchThemePath);
if (!FindNextFile(hFindFile, &FindFileData)) { break; }
}
}
FindClose(hFindFile);
}
for (++iTheme; iTheme < ThemeItems_CountOf(); ++iTheme)
{
Theme_Files[iTheme].rid = 0; // no themes available
Theme_Files[iTheme].szName[0] = L'\0';
Theme_Files[iTheme].szFilePath[0] = L'\0';
}
}
//=============================================================================
//
// Style_SetIniFile()
//
void Style_SetIniFile(LPCWSTR szIniFile)
{
StringCchCopy(Theme_Files[1].szFilePath, COUNTOF(Theme_Files[1].szFilePath), szIniFile);
_FillThemesMenuTable();
}
//=============================================================================
//
// Style_InsertThemesMenu()
//
static HMENU s_hmenuThemes = NULL;
bool Style_InsertThemesMenu(HMENU hMenuBar)
{
if (s_hmenuThemes) { DestroyMenu(s_hmenuThemes); }
s_hmenuThemes = CreatePopupMenu();
//int const pos = GetMenuItemCount(hMenuBar) - 2;
GetLngString(Theme_Files[0].rid, Theme_Files[0].szName, COUNTOF(Theme_Files[0].szName));
GetLngString(Theme_Files[1].rid, Theme_Files[1].szName, COUNTOF(Theme_Files[1].szName));
for (unsigned i = 0; i < ThemeItems_CountOf(); ++i)
{
if (i == 2) {
AppendMenu(s_hmenuThemes, MF_SEPARATOR, 0, 0);
}
if (Theme_Files[i].rid > 0) {
AppendMenu(s_hmenuThemes, MF_ENABLED | MF_STRING, Theme_Files[i].rid, Theme_Files[i].szName);
}
else {
break; // done
}
}
// --- insert ---
WCHAR wchMenuItemStrg[80] = { L'\0' };
GetLngString(IDS_MUI_MENU_THEMES, wchMenuItemStrg, COUNTOF(wchMenuItemStrg));
//bool const res = InsertMenu(hMenuBar, pos, MF_BYPOSITION | MF_POPUP | MF_STRING, (UINT_PTR)s_hmenuThemes, wchMenuItemStrg);
bool const res = InsertMenu(hMenuBar, IDM_VIEW_SCHEMECONFIG, MF_BYCOMMAND | MF_POPUP | MF_STRING, (UINT_PTR)s_hmenuThemes, wchMenuItemStrg);
CheckCmd(hMenuBar, Theme_Files[s_idxSelectedTheme].rid, true);
if (StrIsEmpty(Theme_Files[s_idxSelectedTheme].szFilePath))
{
EnableCmd(hMenuBar, Theme_Files[s_idxSelectedTheme].rid, false);
}
return res;
}
//=============================================================================
//
// Style_DynamicThemesMenuCmd() - Handles IDS_MUI_MENU_THEMES messages
//
//
static void _EnableSchemeConfig(const bool bEnable)
{
EnableCmd(GetMenu(Globals.hwndMain), IDM_VIEW_SCHEMECONFIG, bEnable);
EnableTool(Globals.hwndToolbar, IDT_VIEW_SCHEMECONFIG, bEnable);
}
void Style_DynamicThemesMenuCmd(int cmd, bool bEnableSaveSettings)
{
unsigned const iThemeIdx = (unsigned)(cmd - IDM_THEMES_DEFAULT); // consecutive IDs
if ((iThemeIdx < 0) || (iThemeIdx >= ThemeItems_CountOf())) {
return;
}
if (iThemeIdx == s_idxSelectedTheme) { return; }
CheckCmd(Globals.hMainMenu, Theme_Files[s_idxSelectedTheme].rid, false);
if (Settings.SaveSettings) {
if (s_idxSelectedTheme == 0) {
// internal defaults
}
else if (s_idxSelectedTheme == 1) {
if (bEnableSaveSettings) {
CreateIniFile();
if (StrIsNotEmpty(Globals.IniFile)) {
Style_ExportToFile(Globals.IniFile, false);
}
}
}
else if (PathFileExists(Theme_Files[s_idxSelectedTheme].szFilePath))
{
bool const bIndependentFromStandardSettings = true;
Style_ExportToFile(Theme_Files[s_idxSelectedTheme].szFilePath, bIndependentFromStandardSettings);
}
}
s_idxSelectedTheme = iThemeIdx;
bool result = true;
if ((s_idxSelectedTheme > 1) && PathFileExists(Theme_Files[s_idxSelectedTheme].szFilePath))
{
result = Style_ImportFromFile(Theme_Files[s_idxSelectedTheme].szFilePath);
}
else if (s_idxSelectedTheme == 1) {
result = Style_ImportFromFile(Globals.IniFile);
}
else {
result = Style_ImportFromFile(L"");
}
if (result) {
Style_ResetCurrentLexer(Globals.hwndEdit);
SendWMSize(Globals.hwndMain, NULL);
UpdateUI();
_EnableSchemeConfig(s_idxSelectedTheme != 0);
UpdateAllBars(true);
}
CheckCmd(Globals.hMainMenu, Theme_Files[s_idxSelectedTheme].rid, true);
}
//=============================================================================
//
// IsLexerStandard()
//
bool IsLexerStandard(PEDITLEXER pLexer)
{
return ( pLexer && ((pLexer == &lexStandard) || (pLexer == &lexStandard2nd)) );
}
PEDITLEXER GetCurrentStdLexer()
{
return (Style_GetUse2ndDefault() ? &lexStandard2nd : &lexStandard);
}
bool IsStyleStandardDefault(PEDITSTYLE pStyle)
{
return (pStyle && ((pStyle->rid == IDS_LEX_STD_STYLE) || (pStyle->rid == IDS_LEX_2ND_STYLE)));
}
bool IsStyleSchemeDefault(PEDITSTYLE pStyle)
{
return (pStyle && (pStyle->rid == IDS_LEX_STR_63126));
}
PEDITLEXER GetDefaultLexer()
{
return g_pLexArray[s_iDefaultLexer];
}
//=============================================================================
//
// IsLexerStandard()
//
bool Style_IsCurLexerStandard()
{
return IsLexerStandard(s_pLexCurrent);
}
//=============================================================================
//
// Style_GetBaseFontSize()
//
static float _SetBaseFontSize(float fSize)
{
static float fBaseFontSize = 10.0f;
if (fSize >= 0.0f) {
fBaseFontSize = Round10th(fSize);
}
return fBaseFontSize;
}
//=============================================================================
//
// Style_GetBaseFontSize()
//
float Style_GetBaseFontSize()
{
return _SetBaseFontSize(-1.0);
}
//=============================================================================
//
// Style_RgbAlpha()
//
int Style_RgbAlpha(int rgbFore, int rgbBack, int alpha)
{
return (int)RGB(\
(0xFF - alpha) * (int)GetRValue(rgbBack) / 0xFF + alpha * (int)GetRValue(rgbFore) / 0xFF, \
(0xFF - alpha) * (int)GetGValue(rgbBack) / 0xFF + alpha * (int)GetGValue(rgbFore) / 0xFF, \
(0xFF - alpha) * (int)GetBValue(rgbBack) / 0xFF + alpha * (int)GetBValue(rgbFore) / 0xFF);
}
//=============================================================================
//
// _SetCurrentFontSize(), _GetCurrentFontSize()
//
static float _SetCurrentFontSize(float fSize)
{
static float fCurrentFontSize = 10.0f;
if (signbit(fSize) == 0) {
float const fSizeR10th = Round10th(fSize);
fCurrentFontSize = (0.5f < fSizeR10th) ? fSizeR10th : 0.5f;
}
return fCurrentFontSize;
}
float Style_GetCurrentFontSize()
{
return _SetCurrentFontSize(-1.0f);
}
//=============================================================================
//
// Style_Load()
//
void Style_Load()
{
float const fBFS = GetBaseFontSize(Globals.hwndMain);
_SetBaseFontSize(fBFS);
_SetCurrentFontSize(fBFS);
for (int i = 0; i < 16; ++i) {
g_colorCustom[i] = s_colorDefault[i];
}
_FillThemesMenuTable();
// get theme name from settings
WCHAR wchThemeName[80];
IniFileGetString(Globals.IniFile, L"Styles", STYLING_THEME_NAME, L"", wchThemeName, COUNTOF(wchThemeName));
unsigned iTheme = 1;
if (StrIsNotEmpty(wchThemeName)) {
for (; iTheme < ThemeItems_CountOf(); ++iTheme)
{
if (StringCchCompareXI(wchThemeName, Theme_Files[iTheme].szName) == 0) { break; }
}
}
s_idxSelectedTheme = (iTheme < ThemeItems_CountOf()) ? iTheme : 1;
Style_ImportFromFile(Theme_Files[s_idxSelectedTheme].szFilePath);
}
//=============================================================================
//
// Style_Import()
//
bool Style_Import(HWND hwnd)
{
WCHAR szFile[MAX_PATH] = { L'\0' };
WCHAR szFilter[MAX_PATH] = { L'\0' };
OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(OPENFILENAME));
GetLngString(IDS_MUI_FILTER_INI, szFilter, COUNTOF(szFilter));
PrepareFilterStr(szFilter);
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = szFilter;
ofn.lpstrFile = szFile;
ofn.lpstrDefExt = L"ini";
ofn.nMaxFile = COUNTOF(szFile);
ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_NOCHANGEDIR | OFN_DONTADDTORECENT
| OFN_PATHMUSTEXIST | OFN_SHAREAWARE /*| OFN_NODEREFERENCELINKS*/;
bool result = false;
if (GetOpenFileName(&ofn))
{
result = Style_ImportFromFile(szFile);
}
return result;
}
//=============================================================================
//
// Style_ImportFromFile()
//
bool Style_ImportFromFile(const WCHAR* szFile)
{
bool bResetToDefault = (!szFile || szFile[0] == L'\0') ? true : false;
if (bResetToDefault) {
ReleaseIniFile();
}
bool result = !bResetToDefault ? LoadIniFile(szFile) : true;
if (result) {
if (bResetToDefault)
{
for (int i = 0; i < 16; i++) {
g_colorCustom[i] = s_colorDefault[i];
}
}
else {
const WCHAR* const CustomColors_Section = L"Custom Colors";
WCHAR tch[32] = { L'\0' };
for (int i = 0; i < 16; i++) {
WCHAR wch[32] = { L'\0' };
StringCchPrintf(tch, COUNTOF(tch), L"%02i", i + 1);
int itok = 0;
if (IniSectionGetString(CustomColors_Section, tch, L"", wch, COUNTOF(wch))) {
if (wch[0] == L'#') {
unsigned int irgb;
itok = swscanf_s(CharNext(wch), L"%x", &irgb);
if (itok == 1) {
g_colorCustom[i] = RGB((irgb & 0xFF0000) >> 16, (irgb & 0xFF00) >> 8, irgb & 0xFF);
}
}
}
if (itok != 1) {
g_colorCustom[i] = s_colorDefault[i];
}
}
}
// Styles
const WCHAR* const Styles_Section = L"Styles";
// 2nd default
Style_SetUse2ndDefault(IniSectionGetBool(Styles_Section, L"Use2ndDefaultStyle", false));
// default scheme
s_iDefaultLexer = clampi(IniSectionGetInt(Styles_Section, L"DefaultScheme", Constants.StdDefaultLexerID), 0, COUNTOF(g_pLexArray) - 1);
// auto select
s_bAutoSelect = IniSectionGetBool(Styles_Section, L"AutoSelect", true);
// scheme select dlg dimensions
s_cxStyleSelectDlg = clampi(IniSectionGetInt(Styles_Section, L"SelectDlgSizeX", STYLESELECTDLG_X), 0, 8192);
s_cyStyleSelectDlg = clampi(IniSectionGetInt(Styles_Section, L"SelectDlgSizeY", STYLESELECTDLG_Y), 0, 8192);
// Lexer
for (int iLexer = 0; iLexer < COUNTOF(g_pLexArray); iLexer++) {
LPCWSTR Lexer_Section = g_pLexArray[iLexer]->pszName;
if ((Globals.iCfgVersionRead < CFG_VER_0004) && (iLexer < 2))
{
Lexer_Section = (iLexer == 0) ? L"Default Text" : L"2nd Default Text";
}
IniSectionGetString(Lexer_Section, L"FileNameExtensions", g_pLexArray[iLexer]->pszDefExt,
g_pLexArray[iLexer]->szExtensions, COUNTOF(g_pLexArray[iLexer]->szExtensions));
// don't allow empty extensions settings => use default ext
if (StrIsEmpty(g_pLexArray[iLexer]->szExtensions)) {
StringCchCopy(g_pLexArray[iLexer]->szExtensions, COUNTOF(g_pLexArray[iLexer]->szExtensions), g_pLexArray[iLexer]->pszDefExt);
}
unsigned i = 0;
while (g_pLexArray[iLexer]->Styles[i].iStyle != -1)
{
IniSectionGetString(Lexer_Section, g_pLexArray[iLexer]->Styles[i].pszName,
g_pLexArray[iLexer]->Styles[i].pszDefault,
g_pLexArray[iLexer]->Styles[i].szValue,
COUNTOF(g_pLexArray[iLexer]->Styles[i].szValue));
++i;
}
if (Globals.iCfgVersionRead < CFG_VER_0004)
{
// handling "Text Files" lexer
if (StringCchCompareXI(L"Text Files", g_pLexArray[iLexer]->pszName) == 0)
{
if (StrIsNotEmpty(g_pLexArray[0]->szExtensions)) {
StringCchCopy(g_pLexArray[iLexer]->szExtensions, COUNTOF(g_pLexArray[iLexer]->szExtensions), g_pLexArray[0]->szExtensions);
StrTrim(g_pLexArray[iLexer]->szExtensions, L"; ");
}
lexStandard.szExtensions[0] = L'\0';
lexStandard2nd.szExtensions[0] = L'\0';
// copy default style
StringCchCopy(g_pLexArray[iLexer]->Styles[0].szValue, COUNTOF(g_pLexArray[iLexer]->Styles[0].szValue), g_pLexArray[0]->Styles[0].szValue);
}
}
}
ReleaseIniFile();
result = true;
}
return result;
}
//=============================================================================
//
// Style_Save()
//
void Style_Save()
{
Style_ExportToFile(Theme_Files[s_idxSelectedTheme].szFilePath, Globals.bIniFileFromScratch);
if (s_idxSelectedTheme > 1) {
IniFileSetString(Globals.IniFile, L"Styles", STYLING_THEME_NAME, Theme_Files[s_idxSelectedTheme].szName);
}
else {
IniFileDelete(Globals.IniFile, L"Styles", STYLING_THEME_NAME, false);
}
}
//=============================================================================
//
// Style_Export()
//
bool Style_Export(HWND hwnd)
{
WCHAR szFile[MAX_PATH] = { L'\0' };
WCHAR szFilter[256] = { L'\0' };
OPENFILENAME ofn;
ZeroMemory(&ofn,sizeof(OPENFILENAME));
GetLngString(IDS_MUI_FILTER_INI,szFilter,COUNTOF(szFilter));
PrepareFilterStr(szFilter);
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = szFilter;
ofn.lpstrFile = szFile;
ofn.lpstrDefExt = L"ini";
ofn.nMaxFile = COUNTOF(szFile);
ofn.Flags = /*OFN_FILEMUSTEXIST |*/ OFN_HIDEREADONLY | OFN_NOCHANGEDIR | OFN_DONTADDTORECENT
| OFN_PATHMUSTEXIST | OFN_SHAREAWARE /*| OFN_NODEREFERENCELINKS*/ | OFN_OVERWRITEPROMPT;
bool ok = false;
if (GetSaveFileName(&ofn))
{
ok = Style_ExportToFile(szFile, true);
if (!ok) {
InfoBoxLng(MB_ICONERROR, NULL, IDS_MUI_EXPORT_FAIL, PathFindFileName(szFile));
}
}
return ok;
}
//=============================================================================
//
// Style_ExportToFile()
//
#define SAVE_STYLE_IF_NOT_EQ_DEFAULT(TYPE, VARNAME, VALUE, DEFAULT) \
if ((VALUE) != (DEFAULT)) { \
IniSectionSet##TYPE(Styles_Section, _W(_STRG(VARNAME)), (VALUE)); \
} else { \
IniSectionDelete(Styles_Section, _W(_STRG(VARNAME)), false); \
}
// ----------------------------------------------------------------------------
bool Style_ExportToFile(const WCHAR* szFile, bool bForceAll)
{
if (StrIsEmpty(szFile)) {
if (s_idxSelectedTheme != 0) {
InfoBoxLng(MB_ICONWARNING, NULL, IDS_MUI_SETTINGSNOTSAVED);
}
return false;
}
LoadIniFile(szFile); // reset
// Custom colors
const WCHAR* const CustomColors_Section = L"Custom Colors";
for (int i = 0; i < 16; i++) {
WCHAR tch[32] = { L'\0' };
WCHAR wch[32] = { L'\0' };
StringCchPrintf(tch, COUNTOF(tch), L"%02i", i + 1);
if (bForceAll || (g_colorCustom[i] != s_colorDefault[i]))
{
StringCchPrintf(wch, COUNTOF(wch), L"#%02X%02X%02X",
(int)GetRValue(g_colorCustom[i]), (int)GetGValue(g_colorCustom[i]), (int)GetBValue(g_colorCustom[i]));
IniSectionSetString(CustomColors_Section, tch, wch);
}
else {
IniSectionDelete(CustomColors_Section, tch, false);
}
}
const WCHAR* const Styles_Section = L"Styles";
// auto select
bool const bUse2ndSty = Style_GetUse2ndDefault();
SAVE_STYLE_IF_NOT_EQ_DEFAULT(Bool, Use2ndDefaultStyle, bUse2ndSty, false);
// default scheme
SAVE_STYLE_IF_NOT_EQ_DEFAULT(Int, DefaultScheme, s_iDefaultLexer, Constants.StdDefaultLexerID);
// auto select
SAVE_STYLE_IF_NOT_EQ_DEFAULT(Bool, AutoSelect, s_bAutoSelect, true);
// scheme select dlg dimensions
SAVE_STYLE_IF_NOT_EQ_DEFAULT(Int, SelectDlgSizeX, s_cxStyleSelectDlg, STYLESELECTDLG_X);
SAVE_STYLE_IF_NOT_EQ_DEFAULT(Int, SelectDlgSizeY, s_cyStyleSelectDlg, STYLESELECTDLG_Y);
// create canonical order of lexer sections
if (bForceAll) {
for (int iLexer = 0; iLexer < COUNTOF(g_pLexArray); iLexer++)
{
IniSectionClear(g_pLexArray[iLexer]->pszName, true);
}
for (int iLexer = 0; iLexer < COUNTOF(g_pLexArray); iLexer++)
{
IniSectionSetString(g_pLexArray[iLexer]->pszName, NULL, NULL);
}
bForceAll = !Globals.bIniFileFromScratch;
}
// ----------------------------------------------------------------
WCHAR szTmpStyle[BUFSIZE_STYLE_VALUE];
for (int iLexer = 0; iLexer < COUNTOF(g_pLexArray); ++iLexer)
{
LPCWSTR const Lexer_Section = g_pLexArray[iLexer]->pszName;
if (bForceAll || (StringCchCompareXI(g_pLexArray[iLexer]->szExtensions, g_pLexArray[iLexer]->pszDefExt) != 0))
{
IniSectionSetString(Lexer_Section, L"FileNameExtensions", g_pLexArray[iLexer]->szExtensions);
}
else {
IniSectionDelete(Lexer_Section, L"FileNameExtensions", false);
}
unsigned i = 0;
while (g_pLexArray[iLexer]->Styles[i].iStyle != -1)
{
// normalize defaults
szTmpStyle[0] = L'\0'; // clear
Style_CopyStyles_IfNotDefined(g_pLexArray[iLexer]->Styles[i].pszDefault, szTmpStyle, COUNTOF(szTmpStyle), true, true);
if (bForceAll || (StringCchCompareXI(g_pLexArray[iLexer]->Styles[i].szValue, szTmpStyle) != 0))
{
// normalize value
szTmpStyle[0] = L'\0'; // clear
Style_CopyStyles_IfNotDefined(g_pLexArray[iLexer]->Styles[i].szValue, szTmpStyle, COUNTOF(szTmpStyle), true, true);
IniSectionSetString(Lexer_Section, g_pLexArray[iLexer]->Styles[i].pszName, szTmpStyle);
}
else {
IniSectionDelete(Lexer_Section, g_pLexArray[iLexer]->Styles[i].pszName, false);
}
++i;
}
}
// cleanup old (< v4) stuff
IniSectionDelete(L"Default Text", NULL, true);
IniSectionDelete(L"2nd Default Text", NULL, true);
return SaveIniFile(szFile);
}
//=============================================================================
//
// Style_SetFoldingAvailability()
//
void Style_SetFoldingAvailability(PEDITLEXER pLexer)
{
switch (pLexer->lexerID)
{
case SCLEX_NULL:
case SCLEX_CONTAINER:
case SCLEX_BATCH:
case SCLEX_CONF:
case SCLEX_MAKEFILE:
case SCLEX_MARKDOWN:
FocusedView.CodeFoldingAvailable = false;
break;
default:
FocusedView.CodeFoldingAvailable = true;
break;
}
}
//=============================================================================
//
// Style_SetFoldingProperties()
//
void Style_SetFoldingProperties(bool active)
{
if (active) {
SciCall_SetProperty("fold", "1");
SciCall_SetProperty("fold.comment", "1");
SciCall_SetProperty("fold.compact", "0");
SciCall_SetProperty("fold.foldsyntaxbased", "1");
SciCall_SetProperty("fold.html", "1");
SciCall_SetProperty("fold.preprocessor", "1");
SciCall_SetProperty("fold.cpp.comment.explicit", "0");
}
else {
SciCall_SetProperty("fold", "0");
}
}
//=============================================================================
//
// Style_SetFoldingFocusedView()
//
void Style_SetFoldingFocusedView()
{
SciCall_SetProperty("fold", "0"); // disable folding by lexers
SciCall_SetProperty("fold.comment", "1");
SciCall_SetProperty("fold.compact", "0");
}
//=============================================================================
//
// void Style_SetLexerSpecificProperties()
//
void Style_SetLexerSpecificProperties(const int lexerId)
{
switch (lexerId)
{
case SCLEX_XML:
SciCall_SetProperty("lexer.xml.allow.scripts", "1");
break;
case SCLEX_CPP:
SciCall_SetProperty("styling.within.preprocessor", "1");
SciCall_SetProperty("lexer.cpp.track.preprocessor", "0");
SciCall_SetProperty("lexer.cpp.update.preprocessor", "0");
break;
case SCLEX_PASCAL:
SciCall_SetProperty("lexer.pascal.smart.highlighting", "1");
break;
case SCLEX_SQL:
SciCall_SetProperty("sql.backslash.escapes", "1");
SciCall_SetProperty("lexer.sql.backticks.identifier", "1");
SciCall_SetProperty("lexer.sql.numbersign.comment", "1");
break;
case SCLEX_NSIS:
SciCall_SetProperty("nsis.ignorecase", "1");
break;
case SCLEX_CSS:
SciCall_SetProperty("lexer.css.scss.language", "1");
SciCall_SetProperty("lexer.css.less.language", "1");
break;
case SCLEX_JSON:
SciCall_SetProperty("json.allow.comments", "1");
SciCall_SetProperty("json.escape.sequence", "1");
break;
case SCLEX_PYTHON:
SciCall_SetProperty("tab.timmy.whinge.level", "1");
break;
default:
break;
}
}
//=============================================================================
//
// _IsItemInStyleString()
//
static inline bool _IsItemInStyleString(LPCWSTR lpszStyleStrg, LPCWSTR item)
{
LPCWSTR pFound = StrStrI(lpszStyleStrg, item);
while (pFound) {
WCHAR const pre = (pFound == lpszStyleStrg) ? L' ' : pFound[-1];
if ((pre == L' ') || (pre == L';')) {
WCHAR const end = pFound[StringCchLenW(item, 0)];
if ((end == L'\0') || (end == L' ') || (end == L';')) {
return true;
}
}
pFound = StrStrI(pFound + 1, item);
}
return false;
}
//=============================================================================
//
// Style_SetLexer()
//
void Style_SetLexer(HWND hwnd, PEDITLEXER pLexNew)
{
// Select standard if NULL is specified
if (!pLexNew) {
pLexNew = GetDefaultLexer();
if (IsLexerStandard(pLexNew)) {
pLexNew = GetCurrentStdLexer();
}
}
// ! dont check for (pLexNew == s_pLexCurrent) <= "reapply current lexer"
// assert(pLexNew != s_pLexCurrent);
bool const bFocusedView = FocusedView.HideNonMatchedLines;
if (bFocusedView) { EditToggleView(Globals.hwndEdit); }
// first set standard lexer's default values
const PEDITLEXER pCurrentStandard = (IsLexerStandard(pLexNew)) ? pLexNew : GetCurrentStdLexer();
Style_SetUse2ndDefault(pCurrentStandard == &lexStandard2nd); // sync if forced
// Lexer
SendMessage(hwnd, SCI_SETLEXER, pLexNew->lexerID, 0);
// Lexer very specific styles
Style_SetLexerSpecificProperties(pLexNew->lexerID);
// Code folding
Style_SetFoldingAvailability(pLexNew);
Style_SetFoldingProperties(FocusedView.CodeFoldingAvailable);
// Add KeyWord Lists
for (int i = 0; i < (KEYWORDSET_MAX + 1); i++) {
SciCall_SetKeywords(i, pLexNew->pKeyWords->pszKeyWords[i]);
}
// --------------------------------------------------------------------------
// Clear
SendMessage(hwnd, SCI_CLEARDOCUMENTSTYLE, 0, 0);
// Default Values are always set
SendMessage(hwnd, SCI_STYLERESETDEFAULT, 0, 0);