source: oup/current/Tools/Template.pas@ 86

Last change on this file since 86 was 86, checked in by alloc, 18 years ago
File size: 14.7 KB
Line 
1unit Template;
2
3interface
4
5uses
6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7 Dialogs, ExtCtrls, StdCtrls, StrUtils,
8 OniDataClass, Functions, Data, Menus, Buttons;
9
10type
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
87var
88 ToolList: TToolList;
89procedure AddToolListEntry(context, name, exts: String);
90
91implementation
92{$R *.dfm}
93uses Main, Exporters;
94
95
96procedure TForm_ToolTemplate.UpdateList;
97var
98 i: Integer;
99 fn, datatype, boxstring: String;
100 level: Integer;
101 oldcon: String;
102begin
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;
120end;
121
122procedure TForm_ToolTemplate.RecreateList;
123var
124 i: Integer;
125 fn, datatype: String;
126 level: Integer;
127begin
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);
144end;
145
146procedure TForm_ToolTemplate.RecreateExtList;
147var
148 i: LongWord;
149 exts: TStringArray;
150begin
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);
166end;
167
168
169
170
171procedure TForm_ToolTemplate.LoadFileNames;
172var
173 Extension: String;
174 no_zero_bytes: Boolean;
175 pattern: String;
176 files: TStringArray;
177 i: LongWord;
178begin
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;
198end;
199
200
201procedure TForm_ToolTemplate.popup_exportClick(Sender: TObject);
202var
203 id: Integer;
204 ext: String;
205begin
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);
212end;
213
214procedure TForm_ToolTemplate.popup_importClick(Sender: TObject);
215var
216 id: Integer;
217 finfo: TFileInfo;
218 fs: TFileStream;
219 data: TData;
220begin
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;
242end;
243
244procedure TForm_ToolTemplate.popup_opentool(Sender: TObject);
245var
246 sender_name, context: String;
247 id: Integer;
248begin
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);
253end;
254
255procedure TForm_ToolTemplate.combo_connectionChange(Sender: TObject);
256var
257 name: String;
258 nstart, nend: Integer;
259 i: Integer;
260begin
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;
283end;
284
285procedure TForm_ToolTemplate.combo_extensionClick(Sender: TObject);
286begin
287 LoadFileNames;
288end;
289
290
291constructor TForm_ToolTemplate.Create(AOwner: TComponent);
292var
293 i: Integer;
294 item: TMenuItem;
295begin
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;
313end;
314
315procedure TForm_ToolTemplate.filepopupPopup(Sender: TObject);
316var
317 ext: String;
318 i: Integer;
319begin
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;
328end;
329
330procedure TForm_ToolTemplate.check_zerobyteClick(Sender: TObject);
331begin
332 LoadFileNames;
333end;
334
335procedure TForm_ToolTemplate.btn_sortClick(Sender: TObject);
336begin
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;
350end;
351
352procedure TForm_ToolTemplate.check_filternameClick(Sender: TObject);
353begin
354 edit_filtername.Enabled := not check_filtername.Checked;
355 LoadFileNames;
356end;
357
358procedure TForm_ToolTemplate.listClick(Sender: TObject);
359var
360 fileid: Integer;
361begin
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;
370end;
371
372procedure TForm_ToolTemplate.listMouseDown(Sender: TObject; Button: TMouseButton;
373 Shift: TShiftState; X, Y: Integer);
374var
375 pt: TPoint;
376begin
377 pt.X := x;
378 pt.Y := y;
379// filelist.ItemIndex := filelist.ItemAtPos(pt, true);
380// Self.listClick(Self);
381end;
382
383
384
385procedure TForm_ToolTemplate.SelectFileID(id: Integer);
386var
387 i: Integer;
388begin
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);
398end;
399
400procedure TForm_ToolTemplate.SelectFileName(filename: String);
401var
402 i: Integer;
403begin
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);
410end;
411
412procedure TForm_ToolTemplate.SetAllowedExts(exts: String);
413begin
414 FAllowedExts := exts;
415 RecreateExtList;
416end;
417
418procedure TForm_ToolTemplate.SetFileFilters(pattern, extension: String;
419 zerobytes: Boolean);
420var
421 i: Integer;
422begin
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;
438end;
439
440procedure TForm_ToolTemplate.SetMultiSelect(allow: Boolean);
441begin
442 FAllowMultiSelect := allow;
443 filelist.MultiSelect := allow;
444end;
445
446
447procedure TForm_ToolTemplate.FormResize(Sender: TObject);
448begin
449 if Self.Width < 300 then
450 Self.Width := 300;
451 if Self.Height < 200 then
452 Self.Height := 200;
453end;
454
455
456
457procedure TForm_ToolTemplate.FormCreate(Sender: TObject);
458begin
459 Self.Width := 260;
460 Self.Height := 300;
461 FOnNewFileSelected := nil;
462 FOnNewConnection := nil;
463 FAllowedExts := '';
464 FAllowMultiSelect := False;
465end;
466
467procedure TForm_ToolTemplate.FormActivate(Sender: TObject);
468begin
469 if edit_filtername.CanFocus then
470 edit_filtername.SetFocus
471 else
472 if content.CanFocus then
473 content.SetFocus;
474end;
475
476procedure TForm_ToolTemplate.FormClose(Sender: TObject; var Action: TCloseAction);
477begin
478 Action := caFree;
479end;
480
481
482procedure AddToolListEntryExt(context, ext: String);
483var
484 i: Integer;
485begin
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;
498end;
499
500procedure AddToolListEntry(context, name, exts: String);
501var
502 i: Integer;
503begin
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;
525end;
526
527end.
Note: See TracBrowser for help on using the repository browser.