source: oup/rewrite/Tools/Template.pas@ 99

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