1 |
unit Template; |
2 |
|
3 |
interface |
4 |
|
5 |
uses |
6 |
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, |
7 |
Dialogs, ExtCtrls, StdCtrls, StrUtils, |
8 |
OniDataClass, Functions, Data, Menus, Buttons; |
9 |
|
10 |
type |
11 |
TNewFileSelectedEvent = procedure(fileinfo: TFileInfo) of object; |
12 |
TNewConnectionEvent = procedure(connection: TOniData) of object; |
13 |
|
14 |
TForm_ToolTemplate = class(TForm) |
15 |
panel_files: TPanel; |
16 |
filelist: TListBox; |
17 |
panel_extension: TPanel; |
18 |
label_ext: TLabel; |
19 |
combo_extension: TComboBox; |
20 |
check_zerobyte: TCheckBox; |
21 |
edit_filtername: TEdit; |
22 |
check_filtername: TCheckBox; |
23 |
Splitter1: TSplitter; |
24 |
content: TPanel; |
25 |
filepopup: TPopupMenu; |
26 |
popup_import: TMenuItem; |
27 |
popup_export: TMenuItem; |
28 |
popup_separator: TMenuItem; |
29 |
importd: TOpenDialog; |
30 |
exportd: TSaveDialog; |
31 |
btn_sort_id_asc: TSpeedButton; |
32 |
btn_sort_id_desc: TSpeedButton; |
33 |
btn_sort_name_asc: TSpeedButton; |
34 |
btn_sort_name_desc: TSpeedButton; |
35 |
btn_sort_ext_asc: TSpeedButton; |
36 |
btn_sort_ext_desc: TSpeedButton; |
37 |
Label1: TLabel; |
38 |
Label2: TLabel; |
39 |
Label3: TLabel; |
40 |
combo_connection: TComboBox; |
41 |
Bevel1: TBevel; |
42 |
procedure RecreateList; |
43 |
procedure UpdateList; |
44 |
procedure RecreateExtList; |
45 |
procedure LoadFileNames; |
46 |
procedure SelectFileName(filename: String); |
47 |
procedure SelectFileID(id: Integer); |
48 |
procedure check_filternameClick(Sender: TObject); |
49 |
procedure check_zerobyteClick(Sender: TObject); |
50 |
procedure combo_extensionClick(Sender: TObject); |
51 |
procedure listClick(Sender: TObject); |
52 |
procedure listMouseDown(Sender: TObject; Button: TMouseButton; |
53 |
Shift: TShiftState; X, Y: Integer); |
54 |
|
55 |
procedure FormResize(Sender: TObject); |
56 |
procedure FormCreate(Sender: TObject); |
57 |
procedure FormClose(Sender: TObject; var Action: TCloseAction); |
58 |
procedure popup_importClick(Sender: TObject); |
59 |
procedure popup_exportClick(Sender: TObject); |
60 |
procedure popup_opentool(Sender: TObject); |
61 |
procedure filepopupPopup(Sender: TObject); |
62 |
procedure btn_sortClick(Sender: TObject); |
63 |
procedure FormActivate(Sender: TObject); |
64 |
procedure combo_connectionChange(Sender: TObject); |
65 |
private |
66 |
FSortBy: TSortType; |
67 |
FOnNewFileSelected: TNewFileSelectedEvent; |
68 |
FOnNewConnection: TNewConnectionEvent; |
69 |
FAllowedExts: String; |
70 |
FAllowMultiSelect: Boolean; |
71 |
FSelectedFile: TFileInfo; |
72 |
FConnection: TOniData; |
73 |
procedure SetAllowedExts(exts: String); |
74 |
procedure SetMultiSelect(allow: Boolean); |
75 |
public |
76 |
constructor Create(AOwner: TComponent); override; |
77 |
procedure SetFileFilters(pattern, extension: String; zerobytes: Boolean); |
78 |
published |
79 |
property OnNewFileSelected: TNewFileSelectedEvent read FOnNewFileSelected write FOnNewFileSelected; |
80 |
property OnNewConnection: TNewConnectionEvent read FOnNewConnection write FOnNewConnection; |
81 |
property AllowedExts: String read FAllowedExts write SetAllowedExts; |
82 |
property AllowMultiSelect: Boolean read FAllowMultiSelect write SetMultiSelect; |
83 |
property SelectedFile: TFileInfo read FSelectedFile; |
84 |
property Connection: TOniData read FConnection; |
85 |
end; |
86 |
|
87 |
var |
88 |
ToolList: TToolList; |
89 |
procedure AddToolListEntry(context, name, exts: String); |
90 |
|
91 |
implementation |
92 |
{$R *.dfm} |
93 |
uses Main, Exporters; |
94 |
|
95 |
|
96 |
procedure TForm_ToolTemplate.UpdateList; |
97 |
var |
98 |
i: Integer; |
99 |
fn, datatype, boxstring: String; |
100 |
level: Integer; |
101 |
oldcon: String; |
102 |
begin |
103 |
oldcon := combo_connection.Items.Strings[combo_connection.ItemIndex]; |
104 |
combo_connection.Items.Clear; |
105 |
for i := 0 to High(DataConnections) do |
106 |
begin |
107 |
level := DataConnections[i].LevelInfo.LevelNumber; |
108 |
fn := ExtractFileName(DataConnections[i].FileName); |
109 |
if DataConnections[i].Backend = ODB_Dat then |
110 |
datatype := 'ONI-.dat: ' |
111 |
else if DataConnections[i].Backend = ODB_ADB then |
112 |
datatype := 'OUP-DB: ' |
113 |
else |
114 |
datatype := 'Unknown: '; |
115 |
boxstring := datatype + fn + ' (Level: ' + IntToStr(level) + ')'; |
116 |
combo_connection.Items.Add(boxstring); |
117 |
if oldcon = boxstring then |
118 |
combo_connection.ItemIndex := combo_connection.Items.Count - 1; |
119 |
end; |
120 |
end; |
121 |
|
122 |
procedure TForm_ToolTemplate.RecreateList; |
123 |
var |
124 |
i: Integer; |
125 |
fn, datatype: String; |
126 |
level: Integer; |
127 |
begin |
128 |
combo_connection.Items.Clear; |
129 |
for i := 0 to High(DataConnections) do |
130 |
begin |
131 |
level := DataConnections[i].LevelInfo.LevelNumber; |
132 |
fn := ExtractFileName(DataConnections[i].FileName); |
133 |
if DataConnections[i].Backend = ODB_Dat then |
134 |
datatype := 'ONI-.dat: ' |
135 |
else if DataConnections[i].Backend = ODB_ADB then |
136 |
datatype := 'OUP-DB: ' |
137 |
else |
138 |
datatype := 'Unknown: '; |
139 |
combo_connection.Items.Add(datatype + fn + ' (Level: ' + IntToStr(level) + ')'); |
140 |
end; |
141 |
FConnection := DataConnections[0]; |
142 |
combo_connection.ItemIndex := 0; |
143 |
combo_connectionChange(Self); |
144 |
end; |
145 |
|
146 |
procedure TForm_ToolTemplate.RecreateExtList; |
147 |
var |
148 |
i: LongWord; |
149 |
exts: TStringArray; |
150 |
begin |
151 |
combo_extension.Items.Clear; |
152 |
combo_extension.Items.Add('_All files_ (' + |
153 |
IntToStr(FConnection.GetFilesCount) + ')'); |
154 |
exts := FConnection.GetExtensionsList; |
155 |
for i := 0 to High(exts) do |
156 |
if Length(FAllowedExts) > 0 then |
157 |
begin |
158 |
if Pos(MidStr(exts[i],1,4), FAllowedExts) > 0 then |
159 |
begin |
160 |
combo_extension.Items.Add(exts[i]); |
161 |
end; |
162 |
end else |
163 |
combo_extension.Items.Add(exts[i]); |
164 |
combo_extension.ItemIndex := 0; |
165 |
combo_extensionClick(Self); |
166 |
end; |
167 |
|
168 |
|
169 |
|
170 |
|
171 |
procedure TForm_ToolTemplate.LoadFileNames; |
172 |
var |
173 |
Extension: String; |
174 |
no_zero_bytes: Boolean; |
175 |
pattern: String; |
176 |
files: TStringArray; |
177 |
i: LongWord; |
178 |
begin |
179 |
Extension := MidStr(combo_extension.Items.Strings[combo_extension.ItemIndex], 1, 4); |
180 |
no_zero_bytes := not check_zerobyte.Checked; |
181 |
pattern := ''; |
182 |
if check_filtername.Checked then |
183 |
pattern := edit_filtername.Text; |
184 |
if Extension = '_All' then |
185 |
if Length(FAllowedExts) > 0 then |
186 |
Extension := FAllowedExts |
187 |
else |
188 |
Extension := ''; |
189 |
|
190 |
files := FConnection.GetFilesList(extension, pattern, no_zero_bytes, FSortBy); |
191 |
|
192 |
filelist.Visible := False; |
193 |
filelist.Items.Clear; |
194 |
if Length(files) > 0 then |
195 |
for i := 0 to High(files) do |
196 |
filelist.Items.Add(files[i]); |
197 |
filelist.Visible := True; |
198 |
end; |
199 |
|
200 |
|
201 |
procedure TForm_ToolTemplate.popup_exportClick(Sender: TObject); |
202 |
var |
203 |
id: Integer; |
204 |
ext: String; |
205 |
begin |
206 |
id := FConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]); |
207 |
ext := RightStr(filelist.Items.Strings[filelist.ItemIndex], 4); |
208 |
exportd.Filter := 'Files of matching extension (*.' + ext + ')|*.' + ext + '|All files|*.*'; |
209 |
exportd.DefaultExt := ext; |
210 |
if exportd.Execute then |
211 |
ExportDatFile(FConnection, id, exportd.FileName); |
212 |
end; |
213 |
|
214 |
procedure TForm_ToolTemplate.popup_importClick(Sender: TObject); |
215 |
var |
216 |
id: Integer; |
217 |
finfo: TFileInfo; |
218 |
fs: TFileStream; |
219 |
data: TData; |
220 |
begin |
221 |
id := FConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]); |
222 |
finfo := FConnection.GetFileInfo(id); |
223 |
|
224 |
importd.Filter := 'Files of matching extension (*.' + finfo.Extension + ')|*.' + |
225 |
finfo.Extension + '|All files|*.*'; |
226 |
if importd.Execute then |
227 |
begin |
228 |
fs := TFileStream.Create(importd.FileName, fmOpenRead); |
229 |
if fs.Size <> finfo.Size then |
230 |
ShowMessage('Can''t import ' + ExtractFilename(importd.FileName) + |
231 |
', file has to have same size as file in .dat.' + CrLf + |
232 |
'Size of file in .dat: ' + FormatFileSize(finfo.Size) + CrLf + |
233 |
'Size of chosen file: ' + FormatFileSize(fs.Size)) |
234 |
else begin |
235 |
SetLength(data, fs.Size); |
236 |
fs.Read(data[0], fs.Size); |
237 |
FConnection.UpdateDatFile(id, data); |
238 |
Self.listClick(Self); |
239 |
end; |
240 |
fs.Free; |
241 |
end; |
242 |
end; |
243 |
|
244 |
procedure TForm_ToolTemplate.popup_opentool(Sender: TObject); |
245 |
var |
246 |
sender_name, context: String; |
247 |
id: Integer; |
248 |
begin |
249 |
sender_name := TComponent(Sender).Name; |
250 |
id := FConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]); |
251 |
context := MidStr(sender_name, Pos('_', sender_name) + 1, Length(sender_name) - Pos('_', sender_name)); |
252 |
Form_Main.open_child(context, id); |
253 |
end; |
254 |
|
255 |
procedure TForm_ToolTemplate.combo_connectionChange(Sender: TObject); |
256 |
var |
257 |
name: String; |
258 |
nstart, nend: Integer; |
259 |
i: Integer; |
260 |
begin |
261 |
if combo_connection.ItemIndex >= 0 then |
262 |
begin |
263 |
name := combo_connection.Items.Strings[combo_connection.ItemIndex]; |
264 |
nstart := Pos(' ', name) + 1; |
265 |
nend := Pos('(', name) - 1; |
266 |
name := MidStr(name, nstart, nend - nstart); |
267 |
|
268 |
for i := 0 to High(DataConnections) do |
269 |
begin |
270 |
if ExtractFileName(DataConnections[i].FileName) = name then |
271 |
begin |
272 |
FConnection := DataConnections[i]; |
273 |
Break; |
274 |
end; |
275 |
end; |
276 |
if i = Length(DataConnections) then |
277 |
FConnection := nil; |
278 |
|
279 |
RecreateExtList; |
280 |
if Assigned(FOnNewConnection) then |
281 |
FOnNewConnection(FConnection); |
282 |
end; |
283 |
end; |
284 |
|
285 |
procedure TForm_ToolTemplate.combo_extensionClick(Sender: TObject); |
286 |
begin |
287 |
LoadFileNames; |
288 |
end; |
289 |
|
290 |
|
291 |
constructor TForm_ToolTemplate.Create(AOwner: TComponent); |
292 |
var |
293 |
i: Integer; |
294 |
item: TMenuItem; |
295 |
begin |
296 |
inherited; |
297 |
RecreateList; |
298 |
FConnection := nil; |
299 |
FSelectedFile.ID := -1; |
300 |
FSelectedFile.FileName := ''; |
301 |
FSelectedFile.FileNameHex := ''; |
302 |
if Length(ToolList) > 0 then |
303 |
begin |
304 |
for i := 0 to High(ToolList) do |
305 |
begin |
306 |
item := TMenuItem.Create(filepopup); |
307 |
item.Name := 'popup_' + ToolList[i].context; |
308 |
item.Caption := 'Open with ' + ToolList[i].name; |
309 |
item.OnClick := Self.popup_opentool; |
310 |
filepopup.Items.Insert(i, item); |
311 |
end; |
312 |
end; |
313 |
end; |
314 |
|
315 |
procedure TForm_ToolTemplate.filepopupPopup(Sender: TObject); |
316 |
var |
317 |
ext: String; |
318 |
i: Integer; |
319 |
begin |
320 |
ext := RightStr(filelist.Items.Strings[filelist.ItemIndex], 4); |
321 |
for i := 0 to High(ToolList) do |
322 |
begin |
323 |
filepopup.Items.Items[i].Enabled := True; |
324 |
if Length(ToolList[i].exts) > 0 then |
325 |
if Pos(ext, ToolList[i].exts) = 0 then |
326 |
filepopup.Items.Items[i].Enabled := False; |
327 |
end; |
328 |
end; |
329 |
|
330 |
procedure TForm_ToolTemplate.check_zerobyteClick(Sender: TObject); |
331 |
begin |
332 |
LoadFileNames; |
333 |
end; |
334 |
|
335 |
procedure TForm_ToolTemplate.btn_sortClick(Sender: TObject); |
336 |
begin |
337 |
if btn_sort_id_asc.Down then |
338 |
FSortBy := stIDAsc |
339 |
else if btn_sort_id_desc.Down then |
340 |
FSortBy := stIDDesc |
341 |
else if btn_sort_name_asc.Down then |
342 |
FSortBy := stNameAsc |
343 |
else if btn_sort_name_desc.Down then |
344 |
FSortBy := stNameDesc |
345 |
else if btn_sort_ext_asc.Down then |
346 |
FSortBy := stExtAsc |
347 |
else if btn_sort_ext_desc.Down then |
348 |
FSortBy := stExtDesc; |
349 |
LoadFileNames; |
350 |
end; |
351 |
|
352 |
procedure TForm_ToolTemplate.check_filternameClick(Sender: TObject); |
353 |
begin |
354 |
edit_filtername.Enabled := not check_filtername.Checked; |
355 |
LoadFileNames; |
356 |
end; |
357 |
|
358 |
procedure TForm_ToolTemplate.listClick(Sender: TObject); |
359 |
var |
360 |
fileid: Integer; |
361 |
begin |
362 |
if filelist.ItemIndex > -1 then |
363 |
begin |
364 |
fileid := FConnection.ExtractFileID( |
365 |
filelist.Items.Strings[filelist.ItemIndex]); |
366 |
FSelectedFile := FConnection.GetFileInfo(fileid); |
367 |
if Assigned(FOnNewFileSelected) then |
368 |
FOnNewFileSelected(FSelectedFile); |
369 |
end; |
370 |
end; |
371 |
|
372 |
procedure TForm_ToolTemplate.listMouseDown(Sender: TObject; Button: TMouseButton; |
373 |
Shift: TShiftState; X, Y: Integer); |
374 |
var |
375 |
pt: TPoint; |
376 |
begin |
377 |
pt.X := x; |
378 |
pt.Y := y; |
379 |
// filelist.ItemIndex := filelist.ItemAtPos(pt, true); |
380 |
// Self.listClick(Self); |
381 |
end; |
382 |
|
383 |
|
384 |
|
385 |
procedure TForm_ToolTemplate.SelectFileID(id: Integer); |
386 |
var |
387 |
i: Integer; |
388 |
begin |
389 |
filelist.ItemIndex := -1; |
390 |
if filelist.Items.Count > 0 then |
391 |
for i := 0 to filelist.Items.Count - 1 do |
392 |
if FConnection.ExtractFileID(filelist.Items.Strings[i]) = id then |
393 |
begin |
394 |
filelist.ItemIndex := i; |
395 |
Break; |
396 |
end; |
397 |
Self.listClick(Self); |
398 |
end; |
399 |
|
400 |
procedure TForm_ToolTemplate.SelectFileName(filename: String); |
401 |
var |
402 |
i: Integer; |
403 |
begin |
404 |
filelist.ItemIndex := -1; |
405 |
if filelist.Items.Count > 0 then |
406 |
for i := 0 to filelist.Items.Count - 1 do |
407 |
if filelist.Items.Strings[i] = filename then |
408 |
filelist.ItemIndex := i; |
409 |
Self.listClick(Self); |
410 |
end; |
411 |
|
412 |
procedure TForm_ToolTemplate.SetAllowedExts(exts: String); |
413 |
begin |
414 |
FAllowedExts := exts; |
415 |
RecreateExtList; |
416 |
end; |
417 |
|
418 |
procedure TForm_ToolTemplate.SetFileFilters(pattern, extension: String; |
419 |
zerobytes: Boolean); |
420 |
var |
421 |
i: Integer; |
422 |
begin |
423 |
if Length(pattern) > 0 then |
424 |
Self.edit_filtername.Text := pattern; |
425 |
Self.check_filtername.Checked := Length(pattern) > 0; |
426 |
if Length(extension) > 0 then |
427 |
begin |
428 |
for i := 0 to Self.combo_extension.Items.Count - 1 do |
429 |
if Pos(extension, Self.combo_extension.Items.Strings[i]) > 0 then |
430 |
Break; |
431 |
if i < Self.combo_extension.Items.Count then |
432 |
Self.combo_extension.ItemIndex := i |
433 |
else |
434 |
Self.combo_extension.ItemIndex := -1; |
435 |
end; |
436 |
Self.check_zerobyte.Checked := zerobytes; |
437 |
Self.LoadFileNames; |
438 |
end; |
439 |
|
440 |
procedure TForm_ToolTemplate.SetMultiSelect(allow: Boolean); |
441 |
begin |
442 |
FAllowMultiSelect := allow; |
443 |
filelist.MultiSelect := allow; |
444 |
end; |
445 |
|
446 |
|
447 |
procedure TForm_ToolTemplate.FormResize(Sender: TObject); |
448 |
begin |
449 |
if Self.Width < 300 then |
450 |
Self.Width := 300; |
451 |
if Self.Height < 200 then |
452 |
Self.Height := 200; |
453 |
end; |
454 |
|
455 |
|
456 |
|
457 |
procedure TForm_ToolTemplate.FormCreate(Sender: TObject); |
458 |
begin |
459 |
Self.Width := 260; |
460 |
Self.Height := 300; |
461 |
FOnNewFileSelected := nil; |
462 |
FOnNewConnection := nil; |
463 |
FAllowedExts := ''; |
464 |
FAllowMultiSelect := False; |
465 |
end; |
466 |
|
467 |
procedure TForm_ToolTemplate.FormActivate(Sender: TObject); |
468 |
begin |
469 |
if edit_filtername.CanFocus then |
470 |
edit_filtername.SetFocus |
471 |
else |
472 |
if content.CanFocus then |
473 |
content.SetFocus; |
474 |
end; |
475 |
|
476 |
procedure TForm_ToolTemplate.FormClose(Sender: TObject; var Action: TCloseAction); |
477 |
begin |
478 |
Action := caFree; |
479 |
end; |
480 |
|
481 |
|
482 |
procedure AddToolListEntryExt(context, ext: String); |
483 |
var |
484 |
i: Integer; |
485 |
begin |
486 |
for i := 0 to High(ToolList) do |
487 |
if ToolList[i].context = context then |
488 |
begin |
489 |
if Pos(ext, ToolList[i].exts) = 0 then |
490 |
begin |
491 |
if Length(ToolList[i].exts) = 0 then |
492 |
ToolList[i].exts := ext |
493 |
else |
494 |
ToolList[i].exts := ToolList[i].exts + ',' + ext; |
495 |
end; |
496 |
Exit; |
497 |
end; |
498 |
end; |
499 |
|
500 |
procedure AddToolListEntry(context, name, exts: String); |
501 |
var |
502 |
i: Integer; |
503 |
begin |
504 |
if Length(ToolList) > 0 then |
505 |
begin |
506 |
for i := 0 to High(ToolList) do |
507 |
if ToolList[i].context = context then |
508 |
begin |
509 |
if (Length(ToolList[i].name) = 0) and (Length(name) > 0) then |
510 |
ToolList[i].name := name; |
511 |
if Length(exts) > 0 then |
512 |
AddToolListEntryExt(context, exts); |
513 |
Exit; |
514 |
end; |
515 |
end; |
516 |
SetLength(ToolList, Length(ToolList) + 1); |
517 |
for i := High(ToolList) downto 1 do |
518 |
if ToolList[i - 1].name > name then |
519 |
ToolList[i] := ToolList[i - 1] |
520 |
else |
521 |
Break; |
522 |
ToolList[i].context := context; |
523 |
ToolList[i].name := name; |
524 |
ToolList[i].exts := exts; |
525 |
end; |
526 |
|
527 |
end. |