forked from douglasimcabral/pyscripter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdlgFileTemplates.pas
316 lines (287 loc) · 10.1 KB
/
dlgFileTemplates.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
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
{-----------------------------------------------------------------------------
Unit Name: dlgCodeTemplates
Author: Kiriakos Vlahos
Date: 08-Aug-2006
Purpose: Customization dialog for Code Templates
History:
-----------------------------------------------------------------------------}
unit dlgFileTemplates;
interface
uses
System.UITypes, Windows, Messages, SysUtils, Variants, Classes, Controls,
Forms, Dialogs, System.Contnrs, StdCtrls,
SynEdit, ActnList, cFileTemplates, SpTBXControls,
dlgPyIDEBase, SpTBXEditors, SpTBXItem, EasyListview,
MPCommonObjects, MPCommonUtilities, System.Actions;
type
TFileTemplatesDialog = class(TPyIDEDlgBase)
Panel: TSpTBXPanel;
GroupBox: TSpTBXGroupBox;
SynTemplate: TSynEdit;
edName: TSpTBXEdit;
edCategory: TSpTBXEdit;
edExtension: TSpTBXEdit;
CBHighlighters: TSpTBXComboBox;
TBXButton1: TSpTBXButton;
TBXButton3: TSpTBXButton;
TBXButton4: TSpTBXButton;
TBXButton5: TSpTBXButton;
TBXButton2: TSpTBXButton;
btnCancel: TSpTBXButton;
btnOK: TSpTBXButton;
btnHelp: TSpTBXButton;
ActionList: TActionList;
actUpdateItem: TAction;
actMoveDown: TAction;
actMoveUp: TAction;
actDeleteItem: TAction;
actAddItem: TAction;
Label1: TSpTBXLabel;
Label2: TSpTBXLabel;
Label5: TSpTBXLabel;
Label4: TSpTBXLabel;
Label3: TSpTBXLabel;
Label6: TSpTBXLabel;
Label7: TSpTBXLabel;
lvItems: TEasyListview;
procedure FormDestroy(Sender: TObject);
procedure edNameKeyPress(Sender: TObject; var Key: Char);
procedure ActionListUpdate(Action: TBasicAction; var Handled: Boolean);
procedure actAddItemExecute(Sender: TObject);
procedure actDeleteItemExecute(Sender: TObject);
procedure actUpdateItemExecute(Sender: TObject);
procedure actMoveUpExecute(Sender: TObject);
procedure actMoveDownExecute(Sender: TObject);
procedure btnHelpClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure CBHighlightersChange(Sender: TObject);
procedure lvItemsItemSelectionsChanged(Sender: TCustomEasyListview);
procedure lvItemsColumnClick(Sender: TCustomEasyListview;
Button: TCommonMouseButton; ShiftState: TShiftState;
const Column: TEasyColumn);
private
procedure StoreFieldsToFileTemplate(FileTemplate: TFileTemplate);
{ Private declarations }
public
{ Public declarations }
TempFileTemplates : TFileTemplates;
procedure SetItems;
procedure GetItems;
end;
implementation
uses dmCommands, SynEditHighlighter, gnugettext, StringResources;
{$R *.dfm}
procedure TFileTemplatesDialog.FormCreate(Sender: TObject);
var
i : integer;
begin
inherited;
TempFileTemplates := TFileTemplates.Create;
for i := 0 to CommandsDataModule.Highlighters.Count - 1 do
cbHighlighters.Items.AddObject(CommandsDataModule.Highlighters[i],
CommandsDataModule.Highlighters.Objects[i]);
SynTemplate.Highlighter := nil;
CommandsDataModule.ParameterCompletion.Editor := SynTemplate;
CommandsDataModule.ModifierCompletion.Editor := SynTemplate;
end;
procedure TFileTemplatesDialog.FormDestroy(Sender: TObject);
begin
CommandsDataModule.ParameterCompletion.RemoveEditor(SynTemplate);
CommandsDataModule.ModifierCompletion.RemoveEditor(SynTemplate);
SynTemplate.Highlighter := nil;
TempFileTemplates.Free;
end;
procedure TFileTemplatesDialog.edNameKeyPress(Sender: TObject; var Key: Char);
begin
if not CharInSet(Key, ['a'..'z', 'A'..'Z', '0'..'9', #8]) then
Key := #0;
inherited;
end;
procedure TFileTemplatesDialog.GetItems;
begin
FileTemplates.Assign(TempFileTemplates);
end;
procedure TFileTemplatesDialog.StoreFieldsToFileTemplate(FileTemplate: TFileTemplate);
begin
FileTemplate.Name := edName.Text;
FileTemplate.Extension := edExtension.Text;
FileTemplate.Category := edCategory.Text;
FileTemplate.Highlighter := CBHighlighters.Text;
FileTemplate.Template := SynTemplate.Text;
end;
procedure TFileTemplatesDialog.SetItems;
Var
i : integer;
begin
TempFileTemplates.Assign(FileTemplates);
lvItems.BeginUpdate;
try
lvItems.Items.Clear;
for i := 0 to TempFileTemplates.Count - 1 do
with lvItems.Items.Add() do begin
Caption := (TempFileTemplates[i] as TFileTemplate).Name;
Data := TempFileTemplates[i];
Captions[1] := TFileTemplate(TempFileTemplates[i]).Category;
end;
finally
lvItems.EndUpdate;
end;
end;
procedure TFileTemplatesDialog.ActionListUpdate(Action: TBasicAction;
var Handled: Boolean);
begin
actDeleteItem.Enabled := Assigned(lvItems.Selection.First());
actMoveUp.Enabled := Assigned(lvItems.Selection.First()) and (lvItems.Selection.First.Index >= 1);
actMoveDown.Enabled := Assigned(lvItems.Selection.First()) and
(lvItems.Selection.First.Index < lvItems.Items.Count - 1);
actAddItem.Enabled := (edName.Text <> '') and (edCategory.Text <> '');
actUpdateItem.Enabled := (edName.Text <> '') and Assigned(lvItems.Selection.First());
Handled := True;
end;
procedure TFileTemplatesDialog.actAddItemExecute(Sender: TObject);
Var
Item : TEasyItem;
FileTemplate : TFileTemplate;
i : Integer;
begin
if (edName.Text <> '') and (edCategory.Text <> '') then begin
for i := 0 to lvItems.Items.Count - 1 do
if (CompareText(lvItems.Items[i].Caption, edName.Text) = 0) and
(CompareText(lvItems.Items[i].Captions[1], edCategory.Text) = 0) then
begin
Item := lvItems.Items[i];
FileTemplate := TFileTemplate(Item.Data);
StoreFieldsToFileTemplate(FileTemplate);
Item.Caption := edName.Text;
Item.Captions[1] := edCategory.Text;
Item.Selected := True;
Item.MakeVisible(emvAuto);
Exit;
end;
FileTemplate := TFileTemplate.Create;
TempFileTemplates.Add(FileTemplate);
StoreFieldsToFileTemplate(FileTemplate);
with lvItems.Items.Add() do begin
Caption := edName.Text;
Captions[1] := edCategory.Text;
Data := FileTemplate;
Selected := True;
MakeVisible(emvAuto);
end;
end;
end;
procedure TFileTemplatesDialog.actDeleteItemExecute(Sender: TObject);
begin
if Assigned(lvItems.Selection.First()) then begin
TempFileTemplates.Delete(lvItems.Selection.First.Index);
lvItems.Items.Delete(lvItems.Selection.First.Index);
end;
end;
procedure TFileTemplatesDialog.actUpdateItemExecute(Sender: TObject);
Var
i : integer;
begin
if (edName.Text <> '') and Assigned(lvItems.Selection.First()) then begin
for i := 0 to lvItems.Items.Count - 1 do
if (CompareText(lvItems.Items[i].Caption, edName.Text) = 0) and
(CompareText(lvItems.Items[i].Captions[1], edCategory.Text) = 0) and
(i <> lvItems.Selection.First.Index) then
begin
Dialogs.MessageDlg(_(SSameName), mtError, [mbOK], 0);
Exit;
end;
with lvItems.Items[lvItems.Selection.First.Index] do begin
StoreFieldsToFileTemplate(TFileTemplate(Data));
Caption := edName.Text;
Captions[1] := edCategory.Text;
end;
end;
end;
procedure TFileTemplatesDialog.btnHelpClick(Sender: TObject);
begin
Application.HelpContext(HelpContext);
end;
procedure TFileTemplatesDialog.CBHighlightersChange(Sender: TObject);
begin
if CBHighlighters.ItemIndex < 0 then
SynTemplate.Highlighter := nil
else
SynTemplate.Highlighter :=
CBHighlighters.Items.Objects[CBHighlighters.ItemIndex] as TSynCustomHighlighter;
end;
procedure TFileTemplatesDialog.lvItemsColumnClick(Sender: TCustomEasyListview;
Button: TCommonMouseButton; ShiftState: TShiftState;
const Column: TEasyColumn);
begin
if Column.Clickable then lvItems.Sort.SortAll;
end;
procedure TFileTemplatesDialog.lvItemsItemSelectionsChanged(
Sender: TCustomEasyListview);
var
FileTemplate : TFileTemplate;
begin
if Assigned(lvItems.Selection.First()) then begin
FileTemplate := TFileTemplate(lvItems.Selection.First.Data);
edName.Text := FileTemplate.Name;
edCategory.Text := FileTemplate.Category;
edExtension.Text := FileTemplate.Extension;
CBHighlighters.ItemIndex := CBHighlighters.Items.IndexOf(FileTemplate.Highlighter);
SynTemplate.Text := FileTemplate.Template;
CBHighlightersChange(Self);
end else begin
edName.Text := '';
edCategory.Text := '';
edExtension.Text := '';
CBHighlighters.ItemIndex := -1;
SynTemplate.Text := '';
end;
end;
procedure TFileTemplatesDialog.actMoveUpExecute(Sender: TObject);
Var
Name, Value : string;
P : Pointer;
Index : integer;
begin
if Assigned(lvItems.Selection.First()) and (lvItems.Selection.First.Index >= 1) then begin
lvItems.Header.Columns[0].SortDirection := esdNone;
lvItems.Header.Columns[1].SortDirection := esdNone;
Index := lvItems.Selection.First.Index;
Name := lvItems.Items[Index].Caption;
Value := lvItems.Items[Index].Captions[1];
P := lvItems.Items[Index].Data;
lvItems.Items.Delete(Index);
with lvItems.Items.Insert(Index - 1) do begin
Caption := Name;
Captions[1] := Value;
Data := P;
Selected := True;
end;
TempFileTemplates.Move(Index, Index - 1);
end;
end;
procedure TFileTemplatesDialog.actMoveDownExecute(Sender: TObject);
Var
Name, Value : string;
P : Pointer;
Index : integer;
begin
if Assigned(lvItems.Selection.First()) and
(lvItems.Selection.First.Index < lvItems.Items.Count - 1) then
begin
lvItems.Header.Columns[0].SortDirection := esdNone;
lvItems.Header.Columns[1].SortDirection := esdNone;
Index := lvItems.Selection.First.Index;
Name := lvItems.Items[Index].Caption;
Value := lvItems.Items[Index].Captions[1];
P := lvItems.Items[Index].Data;
lvItems.Items.Delete(Index);
with lvItems.Items.Insert(Index + 1) do begin
Caption := Name;
Captions[1] := Value;
Data := P;
Selected := True;
end;
TempFileTemplates.Move(Index, Index + 1);
end;
end;
end.