forked from digao-dalpiaz/DzNoteEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUFrmNoteEditorColors.pas
175 lines (138 loc) · 4.57 KB
/
UFrmNoteEditorColors.pas
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
unit UFrmNoteEditorColors;
interface
uses Vcl.Forms, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Controls, SynEdit, Vcl.Buttons,
System.Classes,
//
System.Types, SynEditHighlighter, Vcl.Graphics;
type
TFrmNoteEditorColors = class(TForm)
LbLang: TLabel;
LbSel: TLabel;
Preview: TSynEdit;
L: TListBox;
EdColorText: TColorBox;
EdColorBg: TColorBox;
Bevel1: TBevel;
BtnBold: TSpeedButton;
BtnItalic: TSpeedButton;
BtnUnderline: TSpeedButton;
BtnRestoreDefaults: TSpeedButton;
BtnOK: TButton;
BtnCancel: TButton;
procedure LClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure LDrawItem(Control: TWinControl; Index: Integer; Rect: TRect;
State: TOwnerDrawState);
procedure OnAttributeChange(Sender: TObject);
procedure BtnRestoreDefaultsClick(Sender: TObject);
private
H: TSynCustomHighlighter;
ColorBgOriginal: TColor;
procedure CheckCorSpace(A: TSynHighlighterAttributes);
procedure CreateCopyOfSyn(CopiarFormatacao: Boolean);
public
SynEdit: TSynEdit;
function Run: Boolean;
end;
implementation
{$R *.dfm}
uses Vcl.Dialogs;
function TFrmNoteEditorColors.Run: Boolean;
begin
L.Font.Assign(SynEdit.Font);
L.Color := SynEdit.Color;
Preview.Font.Assign(SynEdit.Font);
Preview.Color := SynEdit.Color;
ColorBgOriginal := SynEdit.Color;
CreateCopyOfSyn(True); //create H as copy of Highlighter of SynEdit
Preview.Text := H.SampleSource;
Result := ShowModal = mrOk;
if Result then
SynEdit.Highlighter.Assign(H); //save syntax format
end;
procedure TFrmNoteEditorColors.CreateCopyOfSyn(CopiarFormatacao: Boolean);
var SynClass: TSynCustomHighlighterClass;
I: Integer;
A: TSynHighlighterAttributes;
begin
SynClass := TSynCustomHighlighterClass(SynEdit.Highlighter.ClassType);
H := SynClass.Create(Self); //create new class to use on this form
if CopiarFormatacao then H.Assign(SynEdit.Highlighter);
Preview.Highlighter := H;
for I := 0 to H.AttrCount-1 do
begin
A := H.Attribute[I];
L.Items.AddObject(A.FriendlyName, A);
CheckCorSpace(A);
end;
end;
procedure TFrmNoteEditorColors.FormShow(Sender: TObject);
begin
L.Canvas.Font.Assign(L.Font);
L.ItemHeight := L.Canvas.TextHeight('A') + 3;
L.ItemIndex := 0;
LClick(nil);
end;
procedure TFrmNoteEditorColors.LDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var A: TSynHighlighterAttributes;
begin
L.Canvas.FillRect(Rect);
A := TSynHighlighterAttributes( L.Items.Objects[Index] );
if A.Background<>clNone then
L.Canvas.Brush.Color := A.Background;
if A.Foreground<>clNone then
L.Canvas.Font.Color := A.Foreground;
L.Canvas.Font.Style := A.Style;
L.Canvas.TextOut(3, Rect.Top+1, L.Items[Index]);
end;
procedure TFrmNoteEditorColors.LClick(Sender: TObject);
var A: TSynHighlighterAttributes;
begin
A := TSynHighlighterAttributes( L.Items.Objects[L.ItemIndex] );
LbSel.Caption := A.FriendlyName;
EdColorText.Selected := A.Foreground;
EdColorBg.Selected := A.Background;
BtnBold.Down := (fsBold in A.Style);
BtnItalic.Down := (fsItalic in A.Style);
BtnUnderline.Down := (fsUnderline in A.Style);
end;
procedure TFrmNoteEditorColors.OnAttributeChange(Sender: TObject);
var A: TSynHighlighterAttributes;
Estilo: TFontStyles;
begin
A := TSynHighlighterAttributes( L.Items.Objects[L.ItemIndex] );
A.Foreground := EdColorText.Selected;
A.Background := EdColorBg.Selected;
Estilo := [];
if BtnBold.Down then Include(Estilo, fsBold);
if BtnItalic.Down then Include(Estilo, fsItalic);
if BtnUnderline.Down then Include(Estilo, fsUnderline);
A.Style := Estilo;
CheckCorSpace(A);
L.Invalidate;
end;
procedure TFrmNoteEditorColors.BtnRestoreDefaultsClick(Sender: TObject);
var Index: Integer;
begin
if MessageDlg('Do you want to restore defaults?', mtConfirmation, mbYesNo, 0) = mrYes then
begin
Index := L.ItemIndex;
L.Clear; //limpar atributos
H.Free;
CreateCopyOfSyn(False);
L.ItemIndex := Index;
LClick(nil);
end;
end;
procedure TFrmNoteEditorColors.CheckCorSpace(A: TSynHighlighterAttributes);
begin
if (A.Name = 'Space') or (A.Name = 'Whitespace') then
begin
if A.Background<>clNone then
L.Color := A.Background
else
L.Color := ColorBgOriginal;
end;
end;
end.