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

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