forked from TES5Edit/TES5Edit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrmScriptForm.pas
200 lines (176 loc) · 4.68 KB
/
frmScriptForm.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
unit frmScriptForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, IOUtils;
const
sNewScript = '<new script>';
sNewScriptName = '_newscript_';
sScriptExt = '.pas';
type
TfrmScript = class(TForm)
pnlTop: TPanel;
Label1: TLabel;
cmbScripts: TComboBox;
pnlBottom: TPanel;
btnCancel: TButton;
btnOK: TButton;
pnlStatus: TPanel;
lblPosition: TLabel;
btnSave: TButton;
dlgSave: TSaveDialog;
Editor: TMemo;
chkScriptsSubDir: TCheckBox;
procedure FormShow(Sender: TObject);
procedure cmbScriptsChange(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure btnSaveClick(Sender: TObject);
procedure EditorKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure EditorMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure chkScriptsSubDirClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
Path: string;
LastUsedScript: string;
Script: string;
procedure UpdateCaretPos;
procedure ReadScriptsList;
end;
var
frmScript: TfrmScript;
implementation
{$R *.dfm}
procedure TfrmScript.btnSaveClick(Sender: TObject);
var
s, s2: string;
i: integer;
begin
if cmbScripts.ItemIndex = -1 then
Exit;
s := cmbScripts.Items[cmbScripts.ItemIndex];
if s = sNewScript then begin
dlgSave.InitialDir := Path;
if dlgSave.Execute then begin
s := dlgSave.FileName;
s2 := ChangeFileExt(ExtractFileName(s), '');
i := cmbScripts.Items.IndexOf(s2);
if i = -1 then begin
cmbScripts.Items.Add(s2);
cmbScripts.ItemIndex := Pred(cmbScripts.Items.Count);
end else
cmbScripts.ItemIndex := i;
end else
Exit;
end else
s := Path + s + sScriptExt;
with TStringList.Create do try
Text := Editor.Lines.Text;
SaveToFile(s);
lblPosition.Caption := Format('Saved: %s', [ExtractFileName(s)]);
finally
Free;
end;
end;
procedure TfrmScript.cmbScriptsChange(Sender: TObject);
var
s: string;
begin
if cmbScripts.ItemIndex = -1 then
Exit;
s := cmbScripts.Items[cmbScripts.ItemIndex];
if s = sNewScript then
s := sNewScriptName;
Editor.Lines.Clear;
with TStringList.Create do try
try LoadFromFile(Path + s + sScriptExt); except end;
Editor.Lines.Text := Text;
Editor.Modified := False;
Editor.SetFocus;
UpdateCaretPos;
finally
Free;
end;
end;
procedure TfrmScript.UpdateCaretPos;
var
l, c: Word;
begin
l := SendMessage(Editor.Handle, EM_LINEFROMCHAR, -1, 0);
c := LoWord(SendMessage(Editor.Handle, EM_GETSEL, 0, 0)) - SendMessage(Editor.Handle, EM_LINEINDEX, -1, 0);
lblPosition.Caption := Format('Line:%d Col:%d', [Succ(l), Succ(c)]);
end;
procedure TfrmScript.EditorMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
UpdateCaretPos;
end;
procedure TfrmScript.EditorKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
UpdateCaretPos;
end;
procedure TfrmScript.chkScriptsSubDirClick(Sender: TObject);
begin
if Self.Active then
ReadScriptsList;
end;
procedure TfrmScript.ReadScriptsList;
var
sl1, sl2: TStringList;
f, sname: string;
so: TSearchOption;
i : Integer;
begin
sl1 := TStringList.Create;
sl2 := TStringList.Create;
try
if chkScriptsSubDir.Checked then
so := TSearchOption.soAllDirectories
else
so := TSearchOption.soTopDirectoryOnly;
for f in TDirectory.GetFiles(Path, '*' + sScriptExt, so) do begin
sname := ChangeFileExt(Copy(f, Length(Path) + 1, Length(f)), '');
if SameText(sNewScriptName, sname) then Continue;
if Pos('\', sname) <> 0 then
sl1.Add(sname)
else
sl2.Add(sname);
end;
sl1.Sort;
sl2.Sort;
sl1.Insert(0, sNewScript);
sl1.AddStrings(sl2);
cmbScripts.Items.Assign(sl1);
finally
sl1.Free;
sl2.Free;
end;
i := cmbScripts.Items.IndexOf(LastUsedScript);
if i = -1 then i := 0;
cmbScripts.ItemIndex := i;
cmbScriptsChange(Self);
end;
procedure TfrmScript.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if ModalResult = mrOk then begin
if Editor.Modified then
btnSaveClick(Sender);
Script := Editor.Lines.Text;
LastUsedScript := cmbScripts.Items[cmbScripts.ItemIndex];
end;
end;
procedure TfrmScript.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = VK_ESCAPE then
ModalResult := mrCancel;
end;
procedure TfrmScript.FormShow(Sender: TObject);
begin
ReadScriptsList;
end;
end.