Menu

[r4761]: / branches / parsnip / Src / Main / CS.UI.Dialogs.HiliteThemesEditor.pas  Maximize  Restore  History

Download this file

934 lines (854 with data), 27.2 kB

  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
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2013-2014, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Implements a dialogue box where the user can create, edit and delete syntax
* highlighter themes.
}
unit CS.UI.Dialogs.HiliteThemesEditor;
interface
uses
// Delphi
Classes,
ActnList,
Forms,
StdCtrls,
Controls,
ExtCtrls,
Graphics,
// Project
CS.SourceCode.Hiliter.Brushes,
CS.SourceCode.Hiliter.Themes,
CS.UI.Helper.CollectionCtrlKVMgr,
FmGenericViewDlg,
FrRTFShowCase,
UBaseObjects,
UColorBoxEx,
UColorDialogEx;
type
THiliteThemesEditorDlg = class(TGenericViewDlg, INoPublicConstruct)
lblThemes: TLabel;
cbThemes: TComboBox;
lblBrushes: TLabel;
cbBrushes: TComboBox;
gbThemeProps: TGroupBox;
lblFontName: TLabel;
cbFontName: TComboBox;
cbFontSize: TComboBox;
lblDefForeground: TLabel;
lblDefBackground: TLabel;
lblFontSize: TLabel;
btnDelete: TButton;
btnSave: TButton;
gbElements: TGroupBox;
lblElements: TLabel;
lblForeground: TLabel;
lblExample: TLabel;
lbElements: TListBox;
gbFontStyle: TGroupBox;
chkBold: TCheckBox;
chkItalics: TCheckBox;
chkUnderline: TCheckBox;
frmExample: TRTFShowCaseFrame;
lblBackground: TLabel;
btnPreview: TButton;
alDlg: TActionList;
actDelete: TAction;
actSave: TAction;
actPreview: TAction;
btnNew: TButton;
actNew: TAction;
btnDefaultStyle: TButton;
actDefaultStyle: TAction;
btnUpdate: TButton;
actUpdate: TAction;
procedure actDeleteExecute(Sender: TObject);
procedure actSaveExecute(Sender: TObject);
procedure actPreviewExecute(Sender: TObject);
procedure actDeleteUpdate(Sender: TObject);
procedure actPreviewUpdate(Sender: TObject);
procedure cbFontSizeKeyPress(Sender: TObject; var Key: Char);
procedure FormDestroy(Sender: TObject);
procedure cbBrushesChange(Sender: TObject);
procedure actNewExecute(Sender: TObject);
procedure cbThemesChange(Sender: TObject);
procedure cbFontNameChange(Sender: TObject);
procedure lbElementsClick(Sender: TObject);
procedure chkBoldClick(Sender: TObject);
procedure chkItalicsClick(Sender: TObject);
procedure chkUnderlineClick(Sender: TObject);
procedure actDefaultStyleUpdate(Sender: TObject);
procedure cbFontSizeChange(Sender: TObject);
procedure actDefaultStyleExecute(Sender: TObject);
procedure actUpdateExecute(Sender: TObject);
procedure actUpdateUpdate(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
strict private
type
TAttrStyleInfo = record
BrushID: string;
AttrID: string;
AttrStyle: TSyntaxHiliteAttrStyle;
InheritedAttrStyle: TSyntaxHiliteAttrStyle;
constructor Create(const ABrushID, AAttrID: string;
const AAttrStyle, AInheritedAttrStyle: TSyntaxHiliteAttrStyle);
class function CreateNull: TAttrStyleInfo; static;
function IsDefaultStyle: Boolean;
function IsNull: Boolean;
end;
strict private
const
/// <summary>Value used to identify that a theme's default brush is
/// being edited.</summary>
DefaultBrushID = '<DEFAULT>';
var
cbDefForeground: TColorBoxEx;
cbDefBackground: TColorBoxEx;
cbForeground: TColorBoxEx;
cbBackground: TColorBoxEx;
dlgColour: TColorDialogEx;
/// <summary>Shorthand reference TConfig.Instance.HiliterThemes.
/// </summary>
fThemes: TSyntaxHiliteThemes;
fBlockedThemeIDs: TArray<TSyntaxHiliteThemeID>;
fUsedThemeChanged: Boolean;
fWorkingTheme: TSyntaxHiliteTheme;
fWorkingAttrStyle: TAttrStyleInfo;
fWorkingThemeDirty: Boolean;
fThemesComboMgr: TSortedCollectionCtrlKVMgr<TSyntaxHiliteTheme>;
fFontNameComboMgr: TSortedCollectionCtrlKVMgr<string>;
fFontSizeComboMgr: TUnsortedCollectionCtrlKVMgr<Integer>;
fBrushesComboMgr: TUnsortedCollectionCtrlKVMgr<string>;
fElementsLBMgr: TSortedCollectionCtrlKVMgr<TSyntaxHiliterAttr>;
procedure cbDefForegroundChange(Sender: TObject);
procedure cbDefBackgroundChange(Sender: TObject);
procedure cbForegroundChange(Sender: TObject);
procedure cbBackgroundChange(Sender: TObject);
procedure CreateColourCombos;
procedure PopulateFontNameCombo;
procedure PopulateFontSizeCombo;
procedure PopulateThemeCombo;
procedure PopulateBrushesCombo;
procedure PopulateElementsList(const BrushID: string);
function IsDefaultBrushID(const BrushID: string): Boolean;
function IsDefaultBrushSelected: Boolean;
procedure ChangeTheme;
procedure ChangeBrush(const BrushID: string);
procedure ChangeAttrStyle;
procedure UpdateThemePropControls;
procedure UpdateAttrControls;
procedure UpdateAttrFontStyle(const Checked: Boolean;
const FontStyle: TFontStyle);
procedure UpdateWorkingThemeStyles;
procedure SaveWorkingTheme;
procedure SetWorkingThemeDirty;
strict protected
procedure ConfigForm; override;
procedure ArrangeForm; override;
procedure InitForm; override;
public
/// <summary>Displays highlight themes editor dialogue box.</summary>
/// <param name="AOwner">[in] Component that owns this dialogue box.
/// </param>
/// <param name="AThemesInUse">[in] String array of IDs of themes that are
/// currently in use and therefore musn't be deleted.</param>
/// <returns>True if any themes in use were changed, False otherwise.
/// </returns>
class function Execute(AOwner: TComponent;
const BlockedThemeIDs: array of TSyntaxHiliteThemeID): Boolean;
end;
// TODO: Implement example pane
implementation
uses
// Delphi
SysUtils,
Character,
Math,
Dialogs,
// Project
CS.Config,
CS.SourceCode.Hiliter.Renderers,
CS.UI.Dialogs.HiliteThemeName,
CS.Utils.Sound,
FmPreviewDlg,
UConsts,
UContainers,
UCtrlArranger,
UFontHelper,
UMessageBox,
UStrUtils;
{$R *.dfm}
{ THiliteThemesEditorDlg }
procedure THiliteThemesEditorDlg.actDefaultStyleExecute(Sender: TObject);
begin
fWorkingAttrStyle.AttrStyle := fWorkingAttrStyle.InheritedAttrStyle;
UpdateWorkingThemeStyles;
end;
procedure THiliteThemesEditorDlg.actDefaultStyleUpdate(Sender: TObject);
begin
actDefaultStyle.Enabled := not IsDefaultBrushSelected and
not fWorkingAttrStyle.IsDefaultStyle;
end;
procedure THiliteThemesEditorDlg.actDeleteExecute(Sender: TObject);
var
SelTheme: TSyntaxHiliteTheme;
begin
SelTheme := fThemesComboMgr.GetSelected;
fThemesComboMgr.Delete(SelTheme);
fThemes.Delete(SelTheme.ID);
ChangeTheme;
end;
procedure THiliteThemesEditorDlg.actDeleteUpdate(Sender: TObject);
begin
actDelete.Enabled := fThemesComboMgr.HasSelection
and not fThemesComboMgr.GetSelected.BuiltIn
and not TArrayHelper.Contains<TSyntaxHiliteThemeID>(
fBlockedThemeIDs,
fThemesComboMgr.GetSelected.ID,
function (const Left, Right: TSyntaxHiliteThemeID): Boolean
begin
Result := Left = Right;
end
);
end;
procedure THiliteThemesEditorDlg.actNewExecute(Sender: TObject);
begin
fThemesComboMgr.ClearSelection;
ChangeTheme;
end;
procedure THiliteThemesEditorDlg.actPreviewExecute(Sender: TObject);
var
Brush: TSyntaxHiliterBrush;
resourcestring
sTitle = '%s Highlighting Preview';
begin
Brush := TSyntaxHiliterBrushes.CreateBrush(fBrushesComboMgr.GetSelected);
try
TPreviewDlg.Execute(
Self,
TXHTMLDocumentHiliter.Hilite(
Brush.SampleSourceCode, Brush, fWorkingTheme
),
dtHTML,
Format(sTitle, [Brush.FriendlyName])
);
finally
Brush.Free;
end;
end;
procedure THiliteThemesEditorDlg.actPreviewUpdate(Sender: TObject);
begin
actPreview.Enabled := not IsDefaultBrushSelected;
end;
procedure THiliteThemesEditorDlg.actSaveExecute(Sender: TObject);
begin
SaveWorkingTheme;
fWorkingThemeDirty := False;
end;
procedure THiliteThemesEditorDlg.actUpdateExecute(Sender: TObject);
begin
Assert(fWorkingTheme.ID = fThemesComboMgr.GetSelected.ID,
ClassName + '.actUpdateExecute: Working theme ID <> selected theme ID'
);
fThemesComboMgr.GetSelected.Assign(fWorkingTheme, False, True);
fWorkingThemeDirty := False;
end;
procedure THiliteThemesEditorDlg.actUpdateUpdate(Sender: TObject);
begin
actUpdate.Enabled := fThemesComboMgr.HasSelection
and not fThemesComboMgr.GetSelected.BuiltIn;
end;
procedure THiliteThemesEditorDlg.ArrangeForm;
begin
// set main group box sizes
gbThemeProps.Width := pnlBody.ClientWidth;
gbElements.Width := gbThemeProps.Width;
// Highlighter theme row
TCtrlArranger.AlignLefts(
[lblThemes, gbThemeProps, lblBrushes, gbElements], 0
);
TCtrlArranger.MoveToRightOf(lblThemes, cbThemes, 8);
TCtrlArranger.AlignRights(
[btnDelete, btnSave], TCtrlArranger.RightOf(gbThemeProps)
);
TCtrlArranger.AlignRights(
[btnNew, btnUpdate], TCtrlArranger.LeftOf([btnDelete, btnSave], 6)
);
cbThemes.Width := btnNew.Left - cbThemes.Left - 16;
TCtrlArranger.AlignTops([btnNew, btnDelete], 0);
TCtrlArranger.AlignTops(
[btnUpdate, btnSave], TCtrlArranger.BottomOf([btnNew, btnDelete], 6)
);
TCtrlArranger.AlignVCentresTo([btnNew, btnUpdate], [lblThemes, cbThemes]);
// Theme properties group box
TCtrlArranger.MoveBelow(
[lblThemes, cbThemes, btnNew, btnDelete, btnSave], gbThemeProps, 12
);
TCtrlArranger.AlignVCentres(
22, [lblFontName, cbFontName, lblDefForeground, cbDefForeground]
);
TCtrlArranger.AlignVCentres(
TCtrlArranger.BottomOf(
[lblFontName, cbFontName, lblDefForeground, cbDefForeground], 12
),
[lblFontSize, cbFontSize, lblDefBackground, cbDefBackground]
);
TCtrlArranger.AlignLefts([lblFontName, lblFontSize], 8);
TCtrlArranger.AlignLefts(
[cbFontName, cbFontSize],
TCtrlArranger.RightOf([lblFontName, lblFontSize], 8)
);
TCtrlArranger.AlignLefts(
[cbDefForeground, cbDefBackground],
gbThemeProps.Width - Max(cbDefForeground.Width, cbDefBackground.Width) - 8
);
TCtrlArranger.AlignLefts(
[lblDefForeground, lblDefBackground],
TCtrlArranger.LeftOf([cbDefForeground, cbDefBackground])
- Max(lblDefForeground.Width, lblDefBackground.Width) - 8
);
gbThemeProps.Height := TCtrlArranger.TotalControlHeight(gbThemeProps) + 10;
// Brush row
TCtrlArranger.AlignVCentres(
TCtrlArranger.BottomOf(gbThemeProps, 16),
[lblBrushes, cbBrushes, btnPreview]
);
TCtrlArranger.MoveToRightOf(lblBrushes, cbBrushes, 8);
TCtrlArranger.MoveToRightOf(cbBrushes, btnPreview, 16);
// Element style group box
TCtrlArranger.MoveBelow(
[lblBrushes, cbBrushes, btnPreview], gbElements, 12
);
TCtrlArranger.AlignVCentres(22, [lblElements, lblForeground, cbForeground]);
gbFontStyle.Top := lblElements.Top;
TCtrlArranger.MoveBelow(lblElements, lbElements, 6);
TCtrlArranger.AlignBottoms([frmExample], TCtrlArranger.BottomOf(lbElements));
lblExample.Top := frmExample.Top - lblExample.Height - 6;
TCtrlArranger.AlignVCentres(
TCtrlArranger.BottomOf([lblForeground, cbForeground], 12),
[lblBackground, cbBackground]
);
TCtrlArranger.MoveBelow([lblBackground, cbBackground], btnDefaultStyle, 12);
TCtrlArranger.AlignLefts(
[cbForeground, cbBackground],
gbElements.Width - Max(cbForeground.Width, cbBackground.Width) - 8
);
TCtrlArranger.AlignLefts(
[lblForeground, lblBackground],
TCtrlArranger.LeftOf([cbForeground, cbBackground])
- Max(lblForeground.Width, lblBackground.Width) - 8
);
TCtrlArranger.AlignRights([cbForeground, btnDefaultStyle]);
gbElements.Height := TCtrlArranger.TotalControlHeight(gbElements) + 10;
// Size body panel
pnlBody.ClientHeight := TCtrlArranger.TotalControlHeight(pnlBody);
inherited;
end;
procedure THiliteThemesEditorDlg.cbBackgroundChange(Sender: TObject);
begin
fWorkingAttrStyle.AttrStyle.Background := cbBackground.Selected;
UpdateWorkingThemeStyles;
end;
procedure THiliteThemesEditorDlg.cbBrushesChange(Sender: TObject);
begin
ChangeBrush(fBrushesComboMgr.GetSelected);
end;
procedure THiliteThemesEditorDlg.cbDefBackgroundChange(Sender: TObject);
begin
fWorkingTheme.DefaultBackground := cbDefBackground.Selected;
SetWorkingThemeDirty;
end;
procedure THiliteThemesEditorDlg.cbDefForegroundChange(Sender: TObject);
begin
fWorkingTheme.DefaultForeground := cbDefForeground.Selected;
SetWorkingThemeDirty;
end;
procedure THiliteThemesEditorDlg.cbFontNameChange(Sender: TObject);
begin
if fFontNameComboMgr.HasSelection then
begin
fWorkingTheme.FontName := fFontNameComboMgr.GetSelected;
SetWorkingThemeDirty;
end;
end;
procedure THiliteThemesEditorDlg.cbFontSizeChange(Sender: TObject);
begin
if fFontSizeComboMgr.HasSelection then
begin
fWorkingTheme.FontSize := fFontSizeComboMgr.GetSelected;
SetWorkingThemeDirty;
end;
end;
procedure THiliteThemesEditorDlg.cbFontSizeKeyPress(Sender: TObject;
var Key: Char);
begin
if not TCharacter.IsDigit(Key) and (Key <> BACKSPACE) then
begin
Key := #0;
KeyErrorBeep;
end;
end;
procedure THiliteThemesEditorDlg.cbForegroundChange(Sender: TObject);
begin
fWorkingAttrStyle.AttrStyle.Foreground := cbForeground.Selected;
UpdateWorkingThemeStyles;
end;
procedure THiliteThemesEditorDlg.cbThemesChange(Sender: TObject);
begin
ChangeTheme;
end;
procedure THiliteThemesEditorDlg.ChangeAttrStyle;
var
DefaultStyle: TSyntaxHiliteAttrStyle;
SelStyle: TSyntaxHiliteAttrStyle;
AttrID: string;
begin
AttrID := fElementsLBMgr.GetSelected.ID;
DefaultStyle := fWorkingTheme.GetDefaultStyle(AttrID);
if IsDefaultBrushSelected then
SelStyle := DefaultStyle
else
SelStyle := fWorkingTheme.GetStyle(fBrushesComboMgr.GetSelected, AttrID);
fWorkingAttrStyle := TAttrStyleInfo.Create(
fBrushesComboMgr.GetSelected,
AttrID,
SelStyle,
DefaultStyle
);
UpdateAttrControls;
end;
procedure THiliteThemesEditorDlg.ChangeBrush(const BrushID: string);
begin
Assert(fBrushesComboMgr.ContainsKey(BrushID),
ClassName + '.ChangeBrush: Invalid BrushID');
if not fBrushesComboMgr.HasSelection
or not StrSameText(fBrushesComboMgr.GetSelected, BrushID) then
fBrushesComboMgr.Select(BrushID);
PopulateElementsList(fBrushesComboMgr.GetSelected);
ChangeAttrStyle;
end;
procedure THiliteThemesEditorDlg.ChangeTheme;
resourcestring
sQuerySave = 'The current theme has been changed. Would you like to save it?';
begin
if fWorkingThemeDirty then
begin
if TMessageBox.Confirm(Self, sQuerySave) then
SaveWorkingTheme;
end;
fWorkingThemeDirty := False;
if fThemesComboMgr.HasSelection then
fWorkingTheme.Assign(fThemesComboMgr.GetSelected)
else
fWorkingTheme.Assign(fThemes.NullTheme);
UpdateThemePropControls;
ChangeBrush(DefaultBrushID);
end;
procedure THiliteThemesEditorDlg.chkBoldClick(Sender: TObject);
begin
UpdateAttrFontStyle(chkBold.Checked, fsBold);
end;
procedure THiliteThemesEditorDlg.chkItalicsClick(Sender: TObject);
begin
UpdateAttrFontStyle(chkItalics.Checked, fsItalic);
end;
procedure THiliteThemesEditorDlg.chkUnderlineClick(Sender: TObject);
begin
UpdateAttrFontStyle(chkUnderline.Checked, fsUnderline);
end;
procedure THiliteThemesEditorDlg.ConfigForm;
begin
inherited;
fThemes := TConfig.Instance.HiliterThemes;
fWorkingTheme := TSyntaxHiliteTheme.Create(
TSyntaxHiliteThemeID.CreateNull, ''
);
CreateColourCombos;
fThemesComboMgr := TSortedCollectionCtrlKVMgr<TSyntaxHiliteTheme>.Create(
TComboBoxAdapter.Create(cbThemes),
True,
function (const Left, Right: TSyntaxHiliteTheme): Boolean
begin
Result := Left.ID = Right.ID;
end,
stIgnoreCase
);
fFontNameComboMgr := TSortedCollectionCtrlKVMgr<string>.Create(
TComboBoxAdapter.Create(cbFontName),
True,
function (const Left, Right: string): Boolean
begin
Result := StrSameText(Left, Right);
end,
stIgnoreCase
);
fFontSizeComboMgr := TUnsortedCollectionCtrlKVMgr<Integer>.Create(
TComboBoxAdapter.Create(cbFontSize),
True,
function (const Left, Right: Integer): Boolean
begin
Result := Left = Right;
end
);
fBrushesComboMgr := TUnsortedCollectionCtrlKVMgr<string>.Create(
TComboBoxAdapter.Create(cbBrushes),
True,
function (const Left, Right: string): Boolean
begin
Result := StrSameText(Left, Right);
end
);
fElementsLBMgr := TSortedCollectionCtrlKVMgr<TSyntaxHiliterAttr>.Create(
TListBoxAdapter.Create(lbElements),
True,
function (const Left, Right: TSyntaxHiliterAttr): Boolean
begin
Result := TSyntaxHiliterAttr.Compare(Left, Right) = 0;
end,
stIgnoreCase
);
end;
procedure THiliteThemesEditorDlg.CreateColourCombos;
procedure SetCommonProps(CB: TColorBoxEx);
begin
// cbCustomColor not included in fColorBox.Style since assigning ColorDialog
// property sets this style
CB.Style := [cbStandardColors, cbExtendedColors, cbSystemColors,
cbIncludeNone, cbPrettyNames];
CB.ItemHeight := 16;
CB.NoneColorColor := clNone;
CB.ColorDialog := dlgColour;
end;
resourcestring
// Colour dialogue style
sDlgTitle = 'Choose Element Colour'; // colour dialogue title
begin
// Create and initialise custom colour dialogue box
dlgColour := TColorDialogEx.Create(Self);
dlgColour.Title := sDlgTitle;
// cdShowHelp not included in fColorDlg.Options since setting HelpKeyword
// property causes this style to be used
dlgColour.Options := [cdFullOpen];
dlgColour.HelpKeyword := 'HiliteThemeColourDlg';
cbDefForeground := TColorBoxEx.Create(Self);
with cbDefForeground do
begin
Parent := gbThemeProps;
Width := 127;
Height := 21;
TabOrder := 2;
OnChange := cbDefForegroundChange;
end;
SetCommonProps(cbDefForeground);
lblDefForeground.FocusControl := cbDefForeground;
cbDefBackground := TColorBoxEx.Create(Self);
with cbDefBackground do
begin
Parent := gbThemeProps;
Width := 127;
Height := 21;
TabOrder := 3;
OnChange := cbDefBackgroundChange;
end;
SetCommonProps(cbDefBackground);
lblDefBackground.FocusControl := cbDefBackground;
cbForeground := TColorBoxEx.Create(Self);
with cbForeground do
begin
Parent := gbElements;
Width := 127;
Height := 21;
TabOrder := 2;
OnChange := cbForegroundChange;
end;
SetCommonProps(cbForeground);
lblForeground.FocusControl := cbForeground;
cbBackground := TColorBoxEx.Create(Self);
with cbBackground do
begin
Parent := gbElements;
Width := 127;
Height := 21;
TabOrder := 3;
OnChange := cbBackgroundChange;
end;
SetCommonProps(cbBackground);
lblBackground.FocusControl := cbBackground;
end;
class function THiliteThemesEditorDlg.Execute(AOwner: TComponent;
const BlockedThemeIDs: array of TSyntaxHiliteThemeID): Boolean;
begin
with THiliteThemesEditorDlg.InternalCreate(AOwner) do
try
fBlockedThemeIDs := TArrayHelper.Copy<TSyntaxHiliteThemeID>(
BlockedThemeIDs
);
ShowModal;
Result := fUsedThemeChanged;
finally
Free;
end;
end;
procedure THiliteThemesEditorDlg.FormCloseQuery(Sender: TObject;
var CanClose: Boolean);
resourcestring
sQueryExit = 'The current theme has been changed. '
+ 'If you close the dialogue box now you will loose all your changes.'
+ EOL2
+ 'Are you sure you want to close the dialogue box?';
begin
if fWorkingThemeDirty and not TMessageBox.Confirm(Self, sQueryExit) then
CanClose := False;
end;
procedure THiliteThemesEditorDlg.FormDestroy(Sender: TObject);
begin
fElementsLBMgr.Free;
fBrushesComboMgr.Free;
fFontSizeComboMgr.Free;
fFontNameComboMgr.Free;
fThemesComboMgr.Free;
fWorkingTheme.Free;
inherited;
end;
procedure THiliteThemesEditorDlg.InitForm;
begin
inherited;
// change ParentFont on check boxes: must be done *after* form's font set
chkBold.ParentFont := False;
chkItalics.ParentFont := False;
chkUnderline.ParentFont := False;
PopulateFontNameCombo;
PopulateFontSizeCombo;
PopulateThemeCombo;
PopulateBrushesCombo;
ChangeTheme;
end;
function THiliteThemesEditorDlg.IsDefaultBrushID(const BrushID: string):
Boolean;
begin
Result := StrSameText(BrushID, DefaultBrushID);
end;
function THiliteThemesEditorDlg.IsDefaultBrushSelected: Boolean;
begin
Result := fBrushesComboMgr.HasSelection and
IsDefaultBrushID(fBrushesComboMgr.GetSelected);
end;
procedure THiliteThemesEditorDlg.lbElementsClick(Sender: TObject);
begin
ChangeAttrStyle;
end;
procedure THiliteThemesEditorDlg.PopulateBrushesCombo;
var
BrushID: string;
Brush: TSyntaxHiliterBrush;
resourcestring
sDefaultBrushFriendlyName = 'Default';
begin
fBrushesComboMgr.Clear;
fBrushesComboMgr.Add(DefaultBrushID, sDefaultBrushFriendlyName);
for BrushID in TSyntaxHiliterBrushes.SupportedBrushIDs do
begin
Brush := TSyntaxHiliterBrushes.CreateBrush(BrushID);
try
fBrushesComboMgr.Add(BrushID, Brush.FriendlyName);
finally
Brush.Free;
end;
end;
fBrushesComboMgr.Select(DefaultBrushID);
PopulateElementsList(fBrushesComboMgr.GetSelected);
end;
procedure THiliteThemesEditorDlg.PopulateElementsList(const BrushID: string);
var
Brush: TSyntaxHiliterBrush;
Attrs: TArray<TSyntaxHiliterAttr>;
Attr: TSyntaxHiliterAttr;
begin
if StrSameText(BrushID, DefaultBrushID) then
Attrs := TSyntaxHiliterBrushes.AllSupportedAttrs
else
begin
Brush := TSyntaxHiliterBrushes.CreateBrush(BrushID);
try
Attrs := Brush.SupportedAttrs;
finally
Brush.Free;
end;
end;
fElementsLBMgr.Clear;
for Attr in Attrs do
fElementsLBMgr.Add(Attr, Attr.FriendlyName);
fElementsLBMgr.Select(fElementsLBMgr.GetFirstKey);
end;
procedure THiliteThemesEditorDlg.PopulateFontNameCombo;
var
FontName: string;
begin
for FontName in TFontHelper.ListMonoSpaceFonts do
fFontNameComboMgr.Add(FontName, FontName);
end;
procedure THiliteThemesEditorDlg.PopulateFontSizeCombo;
var
FontSize: Integer;
begin
for FontSize in TSyntaxHiliteTheme.ValidFontSizes do
fFontSizeComboMgr.Add(FontSize, IntToStr(FontSize));
end;
procedure THiliteThemesEditorDlg.PopulateThemeCombo;
var
Theme: TSyntaxHiliteTheme;
begin
fThemesComboMgr.Clear;
for Theme in TConfig.Instance.HiliterThemes do
fThemesComboMgr.Add(Theme, Theme.FriendlyName);
fThemesComboMgr.Select(fThemes.DefaultTheme);
end;
procedure THiliteThemesEditorDlg.SaveWorkingTheme;
var
ThemeName: string;
Theme: TSyntaxHiliteTheme;
begin
if not THiliteThemeNameDlg.Execute(Self, fThemes, ThemeName) then
Exit;
fWorkingTheme.FriendlyName := ThemeName;
fWorkingTheme.BuiltIn := False;
Theme := fThemes.FindThemeByFriendlyName(ThemeName);
if Assigned(Theme) then
Theme.Assign(fWorkingTheme, False, True)
else
begin
Theme := fWorkingTheme.Clone(fThemes.UniqueIDString);
fThemes.Add(Theme);
fThemesComboMgr.Add(Theme, Theme.FriendlyName);
end;
fWorkingTheme.Assign(Theme);
if not fThemesComboMgr.HasSelection or
(Theme.ID <> fThemesComboMgr.GetSelected.ID) then
fThemesComboMgr.Select(Theme);
end;
procedure THiliteThemesEditorDlg.SetWorkingThemeDirty;
begin
fWorkingThemeDirty := True;
end;
procedure THiliteThemesEditorDlg.UpdateAttrControls;
// Ticks or clears a check box without triggering an OnClick event. To fire
// event would mean that frame would be marked as changed when it should not
// be.
procedure SafeCheck(const CB: TCheckBox; const State: Boolean);
var
OnClickSave: TNotifyEvent;
begin
OnClickSave := CB.OnClick;
try
CB.OnClick := nil;
CB.Checked := State;
finally
CB.OnClick := OnClickSave;
end;
end;
var
FontStyles: TFontStyles;
begin
// Font style group
FontStyles := fWorkingAttrStyle.AttrStyle.FontStyles.Styles;
SafeCheck(chkBold, fsBold in FontStyles);
SafeCheck(chkItalics, fsItalic in FontStyles);
SafeCheck(chkUnderline, fsUnderline in FontStyles);
if IsDefaultBrushID(fWorkingAttrStyle.BrushID)
or (FontStyles
<> fWorkingAttrStyle.InheritedAttrStyle.FontStyles.Styles) then
gbFontStyle.Font.Style := []
else
gbFontStyle.Font.Style := [fsItalic];
// Foreground colour
cbForeground.Selected := fWorkingAttrStyle.AttrStyle.Foreground;
if IsDefaultBrushID(fWorkingAttrStyle.BrushID)
or (fWorkingAttrStyle.AttrStyle.Foreground
<> fWorkingAttrStyle.InheritedAttrStyle.Foreground) then
lblForeground.Font.Style := []
else
lblForeground.Font.Style := [fsItalic];
// Background colour
cbBackground.Selected := fWorkingAttrStyle.AttrStyle.Background;
if IsDefaultBrushID(fWorkingAttrStyle.BrushID)
or (fWorkingAttrStyle.AttrStyle.Background
<> fWorkingAttrStyle.InheritedAttrStyle.Background) then
lblBackground.Font.Style := []
else
lblBackground.Font.Style := [fsItalic];
end;
procedure THiliteThemesEditorDlg.UpdateAttrFontStyle(const Checked: Boolean;
const FontStyle: TFontStyle);
var
FS: TFontStyles;
begin
FS := fWorkingAttrStyle.AttrStyle.FontStyles.Styles;
if Checked then
Include(FS, FontStyle)
else
Exclude(FS, FontStyle);
fWorkingAttrStyle.AttrStyle.FontStyles :=
TSyntaxHiliteFontStyles.CreateStyles(FS);
UpdateWorkingThemeStyles;
end;
procedure THiliteThemesEditorDlg.UpdateThemePropControls;
begin
fFontNameComboMgr.Select(fWorkingTheme.FontName);
fFontSizeComboMgr.Select(fWorkingTheme.FontSize);
end;
procedure THiliteThemesEditorDlg.UpdateWorkingThemeStyles;
var
AttrID: string;
BrushID: string;
NewStyle: TSyntaxHiliteAttrStyle;
BrushStyle: TSyntaxHiliteBrushStyle;
begin
AttrID := fWorkingAttrStyle.AttrID;
BrushID := fWorkingAttrStyle.BrushID;
NewStyle := fWorkingAttrStyle.AttrStyle;
if IsDefaultBrushID(BrushID) then
fWorkingTheme.DefaultBrushStyle.AttrStyles[AttrID] := NewStyle
else
begin
if not fWorkingTheme.IsBrushSupported(BrushID) then
fWorkingTheme.AddBrushStyle(BrushID, TSyntaxHiliteBrushStyle.Create);
BrushStyle := fWorkingTheme.BrushStyles[BrushID];
BrushStyle.AttrStyles[AttrID] := NewStyle;
end;
SetWorkingThemeDirty;
UpdateAttrControls;
end;
{ THiliteThemesEditorDlg.TAttrStyleInfo }
constructor THiliteThemesEditorDlg.TAttrStyleInfo.Create(
const ABrushID, AAttrID: string;
const AAttrStyle, AInheritedAttrStyle: TSyntaxHiliteAttrStyle);
begin
BrushID := ABrushID;
AttrID := AAttrID;
AttrStyle := AAttrStyle;
InheritedAttrStyle := AInheritedAttrStyle;
end;
class function THiliteThemesEditorDlg.TAttrStyleInfo.CreateNull: TAttrStyleInfo;
begin
Result := TAttrStyleInfo.Create(
EmptyStr,
EmptyStr,
TSyntaxHiliteAttrStyle.CreateNull,
TSyntaxHiliteAttrStyle.CreateNull
);
end;
function THiliteThemesEditorDlg.TAttrStyleInfo.IsDefaultStyle: Boolean;
begin
Result := AttrStyle = InheritedAttrStyle;
end;
function THiliteThemesEditorDlg.TAttrStyleInfo.IsNull: Boolean;
begin
Result := AttrID = EmptyStr;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.