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

Last change on this file since 101 was 101, checked in by alloc, 18 years ago
File size: 15.9 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) + ')';
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 FConnectionID := combo_connection.ItemIndex
279 else
280 FConnectionID := -1;
281 RecreateExtList;
282 if Assigned(FOnNewConnection) then
283 FOnNewConnection(FConnectionID);
284end;
285
286procedure TForm_ToolTemplate.combo_extensionClick(Sender: TObject);
287begin
288 LoadFileNames;
289end;
290
291
292constructor TForm_ToolTemplate.Create(AOwner: TComponent);
293var
294 i: Integer;
295 item: TMenuItem;
296begin
297 inherited;
298 Self.Width := 260;
299 Self.Height := 300;
300 FAllowedExts := '';
301 FAllowMultiSelect := False;
302 FOnNewFileSelected := nil;
303 FOnNewConnection := nil;
304 FOnCheckCloseable := nil;
305 FConnectionID := -1;
306 FSelectedFile.ID := -1;
307 UpdateConList;
308 if Length(ToolList) > 0 then
309 begin
310 for i := 0 to High(ToolList) do
311 begin
312 item := TMenuItem.Create(filepopup);
313 item.Name := 'popup_' + ToolList[i].context;
314 item.Caption := 'Open with ' + ToolList[i].name;
315 item.OnClick := Self.popup_opentool;
316 filepopup.Items.Insert(i, item);
317 end;
318 end;
319end;
320
321procedure TForm_ToolTemplate.filepopupPopup(Sender: TObject);
322var
323 ext: String;
324 i: Integer;
325begin
326 ext := RightStr(filelist.Items.Strings[filelist.ItemIndex], 4);
327 for i := 0 to High(ToolList) do
328 begin
329 filepopup.Items.Items[i].Enabled := True;
330 if Length(ToolList[i].exts) > 0 then
331 if Pos(ext, ToolList[i].exts) = 0 then
332 filepopup.Items.Items[i].Enabled := False;
333 end;
334end;
335
336procedure TForm_ToolTemplate.check_zerobyteClick(Sender: TObject);
337begin
338 LoadFileNames;
339end;
340
341procedure TForm_ToolTemplate.btn_sortClick(Sender: TObject);
342begin
343 if btn_sort_id_asc.Down then
344 FSortBy := ST_IDAsc
345 else if btn_sort_id_desc.Down then
346 FSortBy := ST_IDDesc
347 else if btn_sort_name_asc.Down then
348 FSortBy := ST_NameAsc
349 else if btn_sort_name_desc.Down then
350 FSortBy := ST_NameDesc
351 else if btn_sort_ext_asc.Down then
352 FSortBy := ST_ExtAsc
353 else if btn_sort_ext_desc.Down then
354 FSortBy := ST_ExtDesc;
355 LoadFileNames;
356end;
357
358procedure TForm_ToolTemplate.check_filternameClick(Sender: TObject);
359begin
360 edit_filtername.Enabled := not check_filtername.Checked;
361 LoadFileNames;
362end;
363
364procedure TForm_ToolTemplate.listClick(Sender: TObject);
365var
366 fileid: Integer;
367begin
368 if filelist.ItemIndex > -1 then
369 begin
370 fileid := ConManager.Connection[FConnectionID].ExtractFileIDOfName(
371 filelist.Items.Strings[filelist.ItemIndex]);
372 FSelectedFile := ConManager.Connection[FConnectionID].GetFileInfo(fileid);
373 if Assigned(FOnNewFileSelected) then
374 FOnNewFileSelected(FSelectedFile);
375 end;
376end;
377
378procedure TForm_ToolTemplate.listMouseDown(Sender: TObject; Button: TMouseButton;
379 Shift: TShiftState; X, Y: Integer);
380var
381 pt: TPoint;
382begin
383 pt.X := x;
384 pt.Y := y;
385// filelist.ItemIndex := filelist.ItemAtPos(pt, true);
386// Self.listClick(Self);
387end;
388
389
390
391procedure TForm_ToolTemplate.SelectConnection(ConnectionID: Integer);
392begin
393 if FConnectionID <> ConnectionID then
394 begin
395 combo_connection.ItemIndex := ConManager.ConnectionIndexByID[ConnectionID];
396 combo_connectionChange(Self);
397 end;
398end;
399
400procedure TForm_ToolTemplate.SelectFileID(ConnectionID, FileID: Integer);
401var
402 i: Integer;
403begin
404 if FConnectionID <> ConnectionID then
405 SelectConnection(ConnectionID);
406
407 filelist.ItemIndex := -1;
408 if filelist.Items.Count > 0 then
409 for i := 0 to filelist.Items.Count - 1 do
410 if ConManager.Connection[FConnectionID].ExtractFileIDOfName(filelist.Items.Strings[i]) = FileID then
411 begin
412 filelist.ItemIndex := i;
413 Break;
414 end;
415 Self.listClick(Self);
416end;
417
418procedure TForm_ToolTemplate.SelectFileName(ConnectionID: Integer; filename: String);
419var
420 i: Integer;
421begin
422 if FConnectionID <> ConnectionID then
423 SelectConnection(ConnectionID);
424
425 filelist.ItemIndex := -1;
426 if filelist.Items.Count > 0 then
427 for i := 0 to filelist.Items.Count - 1 do
428 if filelist.Items.Strings[i] = filename then
429 filelist.ItemIndex := i;
430 Self.listClick(Self);
431end;
432
433procedure TForm_ToolTemplate.SetAllowedExts(exts: String);
434begin
435 FAllowedExts := exts;
436 RecreateExtList;
437end;
438
439procedure TForm_ToolTemplate.SetFileFilters(pattern, extension: String;
440 zerobytes: Boolean);
441var
442 i: Integer;
443begin
444 if Length(pattern) > 0 then
445 Self.edit_filtername.Text := pattern;
446 Self.check_filtername.Checked := Length(pattern) > 0;
447 if Length(extension) > 0 then
448 begin
449 for i := 0 to Self.combo_extension.Items.Count - 1 do
450 if Pos(extension, Self.combo_extension.Items.Strings[i]) > 0 then
451 Break;
452 if i < Self.combo_extension.Items.Count then
453 Self.combo_extension.ItemIndex := i
454 else
455 Self.combo_extension.ItemIndex := -1;
456 end;
457 Self.check_zerobyte.Checked := zerobytes;
458 Self.LoadFileNames;
459end;
460
461procedure TForm_ToolTemplate.SetMultiSelect(allow: Boolean);
462begin
463 FAllowMultiSelect := allow;
464 filelist.MultiSelect := allow;
465end;
466
467
468function TForm_ToolTemplate.GetToolCloseable: Boolean;
469begin
470 if Assigned(FOnCheckCloseable) then
471 Result := FOnCheckCloseable
472 else
473 Result := True;
474end;
475
476procedure TForm_ToolTemplate.FormActivate(Sender: TObject);
477begin
478 if edit_filtername.CanFocus then
479 edit_filtername.SetFocus
480 else
481 if content.CanFocus then
482 content.SetFocus;
483end;
484
485procedure TForm_ToolTemplate.FormClose(Sender: TObject; var Action: TCloseAction);
486begin
487 Action := caFree;
488end;
489
490
491procedure AddToolListEntryExt(context, ext: String);
492var
493 i: Integer;
494begin
495 for i := 0 to High(ToolList) do
496 if ToolList[i].context = context then
497 begin
498 if Pos(ext, ToolList[i].exts) = 0 then
499 begin
500 if Length(ToolList[i].exts) = 0 then
501 ToolList[i].exts := ext
502 else
503 ToolList[i].exts := ToolList[i].exts + ',' + ext;
504 end;
505 Exit;
506 end;
507end;
508
509procedure AddToolListEntry(context, name, exts: String);
510var
511 i: Integer;
512begin
513 if Length(ToolList) > 0 then
514 begin
515 for i := 0 to High(ToolList) do
516 if ToolList[i].context = context then
517 begin
518 if (Length(ToolList[i].name) = 0) and (Length(name) > 0) then
519 ToolList[i].name := name;
520 if Length(exts) > 0 then
521 AddToolListEntryExt(context, exts);
522 Exit;
523 end;
524 end;
525 SetLength(ToolList, Length(ToolList) + 1);
526 for i := High(ToolList) downto 1 do
527 if ToolList[i - 1].name > name then
528 ToolList[i] := ToolList[i - 1]
529 else
530 Break;
531 ToolList[i].context := context;
532 ToolList[i].name := name;
533 ToolList[i].exts := exts;
534end;
535
536end.
Note: See TracBrowser for help on using the repository browser.