forked from TES5Edit/TES3Edit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frmScriptForm.pas
181 lines (158 loc) · 4.29 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
unit frmScriptForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
const
sNewScript = '<new script>';
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;
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);
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 + '.pas';
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 := '_newscript_';
Editor.Lines.Clear;
with TStringList.Create do try
try LoadFromFile(Path + s + '.pas'); 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.ReadScriptsList;
var
F : TSearchRec;
sl: TStringList;
i : Integer;
begin
sl := TStringList.Create;
try
if FindFirst(Path + '*.pas', faAnyFile, F) = 0 then try
repeat
if not SameText('_newscript_.pas', F.Name) then
sl.Add(ChangeFileExt(F.Name, ''));
until FindNext(F) <> 0;
finally
FindClose(F);
end;
sl.Sort;
sl.Insert(0, sNewScript);
cmbScripts.Items.Assign(sl);
finally
sl.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.