-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCodeTemplates.pas
271 lines (243 loc) · 6.69 KB
/
CodeTemplates.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
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
(*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* The Initial Developer of this code is John Hansen.
* Portions created by John Hansen are Copyright (C) 2009-2013 John Hansen.
* All Rights Reserved.
*
*)
unit CodeTemplates;
{$IFDEF FPC}
{$MODE Delphi}
{$ENDIF}
{$I bricxcc.inc}
interface
uses
{$IFNDEF FPC}
Windows,
{$ELSE}
LResources,
{$ENDIF}
Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
GX_IDECodeTemplates, StdCtrls, Grids, Math;
type
TfrmCodeTemplates = class(TForm)
dlgOpen: TOpenDialog;
grpTemplates: TGroupBox;
grdTemplates: TStringGrid;
btnAdd: TButton;
btnEdit: TButton;
btnDelete: TButton;
lblTemplate: TLabel;
lblCode: TLabel;
mmoCode: TMemo;
dlgSave: TSaveDialog;
btnOK: TButton;
btnCancel: TButton;
btnLoad: TButton;
btnSave: TButton;
btnHelp: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure btnLoadClick(Sender: TObject);
procedure grdTemplatesSelectCell(Sender: TObject; ACol, ARow: Integer;
var CanSelect: Boolean);
procedure btnSaveClick(Sender: TObject);
procedure btnDeleteClick(Sender: TObject);
procedure btnEditClick(Sender: TObject);
procedure btnAddClick(Sender: TObject);
procedure mmoCodeChange(Sender: TObject);
procedure btnHelpClick(Sender: TObject);
private
{ Private declarations }
FTemplates : TIDECodeTemplateList;
procedure RefreshTemplates;
procedure RefreshCode(const aRow : integer);
procedure RefreshButtons;
function CurrentTemplate : integer;
procedure SetCurrentTemplate(i : integer);
function GetCodeTemplates: TStrings;
procedure SetCodeTemplates(const Value: TStrings);
public
{ Public declarations }
property CodeTemplates : TStrings read GetCodeTemplates write SetCodeTemplates;
end;
var
frmCodeTemplates: TfrmCodeTemplates;
implementation
{$IFNDEF FPC}
{$R *.DFM}
{$ENDIF}
uses
EditCodeTemplate;
procedure TfrmCodeTemplates.FormCreate(Sender: TObject);
begin
fTemplates := TIDECodeTemplateList.Create;
grdTemplates.Cells[0,0] := 'Name';
grdTemplates.Cells[1,0] := 'Description';
RefreshTemplates;
end;
procedure TfrmCodeTemplates.FormDestroy(Sender: TObject);
begin
FTemplates.Free;
end;
procedure TfrmCodeTemplates.btnLoadClick(Sender: TObject);
begin
if dlgOpen.Execute then
begin
FTemplates.LoadFromFile(dlgOpen.FileName);
end;
RefreshTemplates;
end;
procedure TfrmCodeTemplates.RefreshTemplates;
var
i : integer;
begin
grdTemplates.RowCount := Max(FTemplates.Count + 1, 2);
if FTemplates.Count = 0 then begin
grdTemplates.Cells[0, 1] := '';
grdTemplates.Cells[1, 1] := '';
end
else begin
for i := 0 to FTemplates.Count - 1 do begin
grdTemplates.Cells[0, i+1] := FTemplates.Templates[i].Name;
grdTemplates.Cells[1, i+1] := FTemplates.Templates[i].Description;
end;
end;
if FTemplates.Count > 0 then begin
SetCurrentTemplate(0);
end;
RefreshButtons;
end;
procedure TfrmCodeTemplates.grdTemplatesSelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
begin
RefreshCode(ARow);
end;
procedure TfrmCodeTemplates.btnSaveClick(Sender: TObject);
begin
if dlgSave.Execute then begin
FTemplates.SaveToFile(dlgSave.Filename);
end;
end;
procedure TfrmCodeTemplates.btnDeleteClick(Sender: TObject);
var
i : integer;
begin
i := CurrentTemplate;
if i < FTemplates.Count then begin
FTemplates.Delete(i);
RefreshTemplates;
grdTemplates.Row := Min(i+1, grdTemplates.RowCount-1);
end;
end;
procedure TfrmCodeTemplates.RefreshCode(const aRow : integer);
var
e : TNotifyEvent;
begin
e := mmoCode.OnChange;
try
mmoCode.OnChange := nil;
mmoCode.Lines.Clear;
if FTemplates.Count >= aRow then
mmoCode.Lines.Assign(FTemplates.Templates[aRow-1].Code);
mmoCode.ReadOnly := FTemplates.Count = 0;
finally
mmoCode.OnChange := e;
end;
end;
procedure TfrmCodeTemplates.RefreshButtons;
var
bEnabled : boolean;
begin
bEnabled := FTemplates.Count > 0;
btnDelete.Enabled := bEnabled;
btnEdit.Enabled := bEnabled;
end;
procedure TfrmCodeTemplates.btnEditClick(Sender: TObject);
var
i : integer;
begin
i := CurrentTemplate;
if i < FTemplates.Count then begin
with TfrmEditCodeTemplate.Create(nil) do
try
TemplateName := FTemplates.Templates[i].Name;
Description := FTemplates.Templates[i].Description;
if ShowModal = mrOK then begin
FTemplates.Templates[i].Name := TemplateName;
FTemplates.Templates[i].Description := Description;
end;
finally
Free;
end;
end;
end;
procedure TfrmCodeTemplates.btnAddClick(Sender: TObject);
var
t : TIDECodeTemplate;
i : integer;
begin
with TfrmEditCodeTemplate.Create(nil) do
try
NewTemplate := True;
if ShowModal = mrOK then begin
t := FTemplates.Add;
t.Name := TemplateName;
t.Description := Description;
t.Code.Text := '';
FTemplates.Sort;
i := FTemplates.IndexOf(TemplateName);
RefreshTemplates;
SetCurrentTemplate(i);
end;
finally
Free;
end;
end;
function TfrmCodeTemplates.CurrentTemplate: integer;
begin
Result := grdTemplates.Row - 1;
end;
procedure TfrmCodeTemplates.mmoCodeChange(Sender: TObject);
var
i : integer;
begin
i := CurrentTemplate;
if i < FTemplates.Count then begin
if FTemplates.Templates[i].Code.Text <> mmoCode.Lines.Text then begin
FTemplates.Templates[i].Code.Text := mmoCode.Lines.Text;
end;
end;
end;
procedure TfrmCodeTemplates.SetCurrentTemplate(i: integer);
begin
grdTemplates.Row := i + 1;
RefreshCode(grdTemplates.Row);
end;
function TfrmCodeTemplates.GetCodeTemplates: TStrings;
begin
Result := FTemplates.AsStrings;
end;
procedure TfrmCodeTemplates.SetCodeTemplates(const Value: TStrings);
begin
FTemplates.AsStrings := Value;
RefreshTemplates;
end;
procedure TfrmCodeTemplates.btnHelpClick(Sender: TObject);
begin
Application.HelpContext(HelpContext);
end;
{$IFDEF FPC}
initialization
{$i CodeTemplates.lrs}
{$ENDIF}
end.