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

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