Menu

[r3717]: / branches / parsnip / Src / Main / CS.UI.Frames.CodeEditor.pas  Maximize  Restore  History

Download this file

197 lines (174 with data), 5.7 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
{
* 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, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Implements a frame that contains and controls access to a TSynEdit control.
}
unit CS.UI.Frames.CodeEditor;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,
SynEdit,
SynEditHighlighter,
CS.SourceCode.Languages,
CS.SourceCode.Hiliter.Brushes,
CS.SourceCode.Hiliter.Themes;
type
TCodeEditorFrame = class(TFrame)
strict private
var
fSynEditCmp: TSynEdit;
fTheme: TSyntaxHiliteTheme;
fBrush: TSyntaxHiliterBrush;
function GetSourceCode: string;
procedure SetSourceCode(const Code: string);
procedure SetTheme(const ATheme: TSyntaxHiliteTheme);
procedure SetBrush(const ABrush: TSyntaxHiliterBrush);
function GetTabSize: Integer;
procedure SetTabSize(const ATabSize: Integer);
procedure ApplyTheme;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure ApplyLanguage(const Language: TSourceCodeLanguage);
property Theme: TSyntaxHiliteTheme read fTheme write SetTheme;
property Brush: TSyntaxHiliterBrush read fBrush write SetBrush;
property TabSize: Integer read GetTabSize write SetTabSize;
property SourceCode: string read GetSourceCode write SetSourceCode;
end;
implementation
{$R *.dfm}
{ TTCodeEditorFrame }
procedure TCodeEditorFrame.ApplyLanguage(const Language: TSourceCodeLanguage);
var
Brush: TSyntaxHiliterBrush;
begin
Brush := TSyntaxHiliterBrushes.CreateBrush(Language.HiliterBrushID);
try
SetBrush(Brush);
finally
Brush.Free;
end;
SetTabSize(Language.EditorTabSize);
end;
procedure TCodeEditorFrame.ApplyTheme;
var
Highlighter: TSynCustomHighlighter;
I: Integer;
BrushID: string;
AttrID: string;
AttrStyle: TSyntaxHiliteAttrStyle;
begin
if not Assigned(fTheme) then
Exit;
fSynEditCmp.Font.Name := fTheme.FontName;
fSynEditCmp.Font.Size := fTheme.FontSize;
if fTheme.DefaultForeground = clNone then
fSynEditCmp.Font.Color := clWindowText
else
fSynEditCmp.Font.Color := fTheme.DefaultForeground;
if fTheme.DefaultBackground = clNone then
fSynEditCmp.Color := clWindow
else
fSynEditCmp.Color := fTheme.DefaultBackground;
Highlighter := fSynEditCmp.Highlighter;
if not Assigned(Highlighter) then
Exit;
BrushID := fBrush.ID;
Assert(Highlighter.GetLanguageName = fBrush.ID,
ClassName + '.ApplyThemes: Highlighter language name <> brush ID');
for I := 0 to Pred(Highlighter.AttrCount) do
begin
AttrID := Highlighter.Attribute[I].Name;
AttrStyle := fTheme.GetStyle(fBrush.ID, AttrID);
Highlighter.Attribute[I].Background := AttrStyle.Background;
Highlighter.Attribute[I].Foreground := AttrStyle.Foreground;
Highlighter.Attribute[I].Style := AttrStyle.FontStyles;
end;
end;
constructor TCodeEditorFrame.Create(AOwner: TComponent);
begin
inherited;
fBrush := TSyntaxHiliterBrushes.CreateNullBrush;
fSynEditCmp := TSynEdit.Create(Self);
fSynEditCmp.Parent := Self;
fSynEditCmp.Align := alClient;
fSynEditCmp.WantReturns := True;
fSynEditCmp.WantTabs := True;
fSynEditCmp.Gutter.LeftOffset := 0; // change this if use glyphs in gutter
fSynEditCmp.Gutter.ShowLineNumbers := True; // TODO: make option
fSynEditCmp.WordWrap := False;
fSynEditCmp.TabWidth := 2;
fSynEditCmp.ActiveLineColor := clNone; {default} // TODO: make option
fSynEditCmp.Options := [
eoAutoIndent, eoDragDropEditing, eoEnhanceEndKey, eoGroupUndo,
eoScrollPastEol, eoShowScrollHint, eoSmartTabDelete, eoTabsToSpaces
]; // = default - [eoSmartTab]
// No highligher (default): setting .Highlighter = nil turns off highlighting
fSynEditCmp.Highlighter := nil;
// Turn off book mark keys & glyphs
{ TODO: could make option, in which case need to restore default gutter's
right offset. If option included glyphs we'll need some nicer ones
to give to BookmMarkOptions.BookmarkImages }
fSynEditCmp.BookMarkOptions.EnableKeys := False;
fSynEditCmp.BookMarkOptions.GlyphsVisible := False;
fSynEditCmp.Gutter.Font.Color := clGray;
ApplyTheme;
end;
destructor TCodeEditorFrame.Destroy;
var
OldHighlighter: TSynCustomHighlighter;
begin
OldHighlighter := fSynEditCmp.Highlighter;
if Assigned(OldHighlighter) then
begin
fSynEditCmp.Highlighter := nil;
OldHighlighter.Free;
end;
fBrush.Free;
inherited;
end;
function TCodeEditorFrame.GetSourceCode: string;
begin
Result := fSynEditCmp.Text;
end;
function TCodeEditorFrame.GetTabSize: Integer;
begin
Result := fSynEditCmp.TabWidth;
end;
procedure TCodeEditorFrame.SetBrush(const ABrush: TSyntaxHiliterBrush);
var
OldBrush: TSyntaxHiliterBrush;
OldHighlighter: TSynCustomHighlighter;
begin
Assert(Assigned(ABrush), ClassName + '.SetBrush: ABrush not assigned');
OldBrush := fBrush;
fBrush := ABrush.Clone;
OldBrush.Free;
OldHighlighter := fSynEditCmp.Highlighter;
fSynEditCmp.Highlighter := ABrush.CreateHighlighter;
OldHighlighter.Free; // may be nil, but that's OK with Free
ApplyTheme;
end;
procedure TCodeEditorFrame.SetSourceCode(const Code: string);
begin
fSynEditCmp.Text := Code;
end;
procedure TCodeEditorFrame.SetTabSize(const ATabSize: Integer);
begin
fSynEditCmp.TabWidth := ATabSize;
end;
procedure TCodeEditorFrame.SetTheme(const ATheme: TSyntaxHiliteTheme);
begin
Assert(Assigned(ATheme), ClassName + '.ATheme not assigned');
fTheme := ATheme;
ApplyTheme;
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.