-
Notifications
You must be signed in to change notification settings - Fork 29
/
uEditor.pas
191 lines (167 loc) · 4.69 KB
/
uEditor.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
unit uEditor;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, JvEditor, JvHLEditor, ComCtrls, ActnList, Menus, ToolWin,
ImgList, StdCtrls, ExtCtrls, JvExControls, JvComponent, JvEditorCommon;
type
TfEditor = class(TForm)
al: TActionList;
ToolBar1: TToolBar;
aFileClose: TAction;
aFileSave: TAction;
ToolButton1: TToolButton;
ToolButton2: TToolButton;
he: TJvHLEditor;
tUpdateStatus: TTimer;
aFileRevertToSaved: TAction;
ToolButton3: TToolButton;
ToolButton4: TToolButton;
procedure aFileCloseExecute(Sender: TObject);
procedure aFileSaveUpdate(Sender: TObject);
procedure aFileSaveExecute(Sender: TObject);
procedure tUpdateStatusTimer(Sender: TObject);
procedure aFileRevertToSavedUpdate(Sender: TObject);
procedure aFileRevertToSavedExecute(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure heGetLineAttr(Sender: TObject; var Line: String;
Index: Integer; var Attrs: TLineAttrs);
private
FFileName: string;
FHighlightedLine: Integer;
procedure SetHighlightedLine(const Value: Integer);
{ Private declarations }
public
{ Public declarations }
property FileName: string read FFileName;
property HighlightedLine: Integer read FHighlightedLine write SetHighlightedLine;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure CloseFile;
procedure Open(AFileName: string);
procedure Save;
end;
implementation
{$R *.dfm}
uses uMain;
{ TfEditor }
procedure TfEditor.CloseFile;
begin
if FileName <> '' then begin
if he.Modified then begin
if MessageDlg(ExtractFileName(FileName) +' has been modified.'
+ ' Do you want to save the changes?', mtConfirmation,
[mbYes, mbNo], 0) = idYes then
Save;
end;
he.Lines.Clear;
he.ReadOnly := True;
FFileName := '';
Caption := 'No file';
HighlightedLine := -1;
end;
end;
constructor TfEditor.Create(AOwner: TComponent);
begin
inherited;
he.ReadOnly := True;
end;
destructor TfEditor.Destroy;
begin
CloseFile;
inherited;
end;
procedure TfEditor.Open(AFileName: string);
begin
CloseFile;
he.Lines.LoadFromFile(AFileName);
FFileName := AFileName;
he.ReadOnly := False;
Caption := FFileName;
HighlightedLine := -1;
end;
procedure TfEditor.Save;
begin
he.Lines.SaveToFile(FileName);
he.Modified := False;
end;
procedure TfEditor.aFileCloseExecute(Sender: TObject);
begin
Close;
end;
procedure TfEditor.aFileSaveUpdate(Sender: TObject);
begin
aFileSave.Enabled := (FileName <> '') and (he.Modified);
end;
procedure TfEditor.aFileSaveExecute(Sender: TObject);
begin
Save;
end;
procedure TfEditor.tUpdateStatusTimer(Sender: TObject);
begin
if FileName = '' then
Hint := '|No opened file.'
else begin
Hint := '|Column '+ IntToStr(he.CaretX+1) + ' Line '
+ IntToStr(he.CaretY+1);
if he.CaretY <> HighlightedLine then
HighlightedLine := -1;
end;
end;
procedure TfEditor.aFileRevertToSavedUpdate(Sender: TObject);
begin
aFileRevertToSaved.Enabled := (FileName <> '') and (he.Modified);
end;
procedure TfEditor.aFileRevertToSavedExecute(Sender: TObject);
begin
if MessageDlg('Reload file and discards any changes?',
mtConfirmation, [mbYes, mbNo], 0) = idYes then
begin
he.Modified := False;
Open(FileName);
end;
end;
procedure TfEditor.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;
procedure TfEditor.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
var
Res: Integer;
begin
if FileName <> '' then begin
if he.Modified then begin
Res := MessageDlg(ExtractFileName(FileName) +' has been modified.'
+ ' Do you want to save the changes?', mtConfirmation,
[mbYes, mbNo, mbCancel], 0);
case Res of
idYes: Save;
idCancel: begin
CanClose := False;
Exit;
end;
end;
end;
he.Lines.Clear;
he.ReadOnly := True;
FFileName := '';
Caption := 'No file';
end;
end;
procedure TfEditor.heGetLineAttr(Sender: TObject; var Line: String;
Index: Integer; var Attrs: TLineAttrs);
var
I: Integer;
begin
if Index = HighlightedLine then begin
for I := Low(Attrs) to High(Attrs) do
Attrs[I].BC := clBtnFace;
end;
end;
procedure TfEditor.SetHighlightedLine(const Value: Integer);
begin
FHighlightedLine := Value;
he.Invalidate;
end;
end.