source: oup/_old_/Tools/Template.pas@ 170

Last change on this file since 170 was 92, checked in by alloc, 18 years ago

Rev86 was first after multi-cons

File size: 16.5 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(connection: TOniData; filename: String);
47 procedure SelectFileID(connection: TOniData; 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.ItemIndex := -1;
105 combo_connection.Items.Clear;
106 if Length(DataConnections) > 0 then
107 begin
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);
138 end;
139end;
140
141procedure TForm_ToolTemplate.RecreateList;
142var
143 i: Integer;
144 fn, datatype: String;
145 level: Integer;
146begin
147 combo_connection.Items.Clear;
148 if Length(DataConnections) > 0 then
149 begin
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);
172 end;
173end;
174
175procedure TForm_ToolTemplate.RecreateExtList;
176var
177 i: LongWord;
178 exts: TStringArray;
179begin
180 combo_extension.Items.Clear;
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
188 begin
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
194 combo_extension.Items.Add(exts[i]);
195 combo_extension.ItemIndex := 0;
196 combo_extensionClick(Self);
197 end;
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
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 := '';
223
224 files := FConnection.GetFilesList(extension, pattern, no_zero_bytes, FSortBy);
225
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;
233end;
234
235
236procedure TForm_ToolTemplate.popup_exportClick(Sender: TObject);
237var
238 id: Integer;
239 ext: String;
240begin
241 id := FConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]);
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
246 ExportDatFile(FConnection, id, exportd.FileName);
247end;
248
249procedure TForm_ToolTemplate.popup_importClick(Sender: TObject);
250var
251 id: Integer;
252 finfo: TFileInfo;
253 fs: TFileStream;
254 data: TData;
255begin
256 id := FConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]);
257 finfo := FConnection.GetFileInfo(id);
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);
272 FConnection.UpdateDatFile(id, data);
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;
285 id := FConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]);
286 context := MidStr(sender_name, Pos('_', sender_name) + 1, Length(sender_name) - Pos('_', sender_name));
287 Form_Main.open_child(context, FConnection, id);
288end;
289
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
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;
333 FSelectedFile := GetEmptyFileInfo;
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
396 fileid := FConnection.ExtractFileID(
397 filelist.Items.Strings[filelist.ItemIndex]);
398 FSelectedFile := FConnection.GetFileInfo(fileid);
399 if Assigned(FOnNewFileSelected) then
400 FOnNewFileSelected(FSelectedFile);
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;
411// filelist.ItemIndex := filelist.ItemAtPos(pt, true);
412// Self.listClick(Self);
413end;
414
415
416
417procedure TForm_ToolTemplate.SelectFileID(connection: TOniData; id: Integer);
418var
419 i: Integer;
420 name: String;
421 nstart, nend: Integer;
422begin
423 for i := 0 to combo_connection.Items.Count - 1 do
424 begin
425 name := combo_connection.Items.Strings[i];
426 nstart := Pos(' ', name) + 1;
427 nend := Pos('(', name) - 1;
428 name := MidStr(name, nstart, nend - nstart);
429
430 if ExtractFileName(connection.FileName) = name then
431 begin
432 combo_connection.ItemIndex := i;
433 combo_connectionChange(Self);
434 end;
435 end;
436
437 filelist.ItemIndex := -1;
438 if filelist.Items.Count > 0 then
439 for i := 0 to filelist.Items.Count - 1 do
440 if FConnection.ExtractFileID(filelist.Items.Strings[i]) = id then
441 begin
442 filelist.ItemIndex := i;
443 Break;
444 end;
445 Self.listClick(Self);
446end;
447
448procedure TForm_ToolTemplate.SelectFileName(connection: TOniData; filename: String);
449var
450 i: Integer;
451 name: String;
452 nstart, nend: Integer;
453begin
454 for i := 0 to combo_connection.Items.Count - 1 do
455 begin
456 name := combo_connection.Items.Strings[i];
457 nstart := Pos(' ', name) + 1;
458 nend := Pos('(', name) - 1;
459 name := MidStr(name, nstart, nend - nstart);
460
461 if ExtractFileName(connection.FileName) = name then
462 begin
463 combo_connection.ItemIndex := i;
464 combo_connectionChange(Self);
465 end;
466 end;
467
468 filelist.ItemIndex := -1;
469 if filelist.Items.Count > 0 then
470 for i := 0 to filelist.Items.Count - 1 do
471 if filelist.Items.Strings[i] = filename then
472 filelist.ItemIndex := i;
473 Self.listClick(Self);
474end;
475
476procedure TForm_ToolTemplate.SetAllowedExts(exts: String);
477begin
478 FAllowedExts := exts;
479 RecreateExtList;
480end;
481
482procedure TForm_ToolTemplate.SetFileFilters(pattern, extension: String;
483 zerobytes: Boolean);
484var
485 i: Integer;
486begin
487 if Length(pattern) > 0 then
488 Self.edit_filtername.Text := pattern;
489 Self.check_filtername.Checked := Length(pattern) > 0;
490 if Length(extension) > 0 then
491 begin
492 for i := 0 to Self.combo_extension.Items.Count - 1 do
493 if Pos(extension, Self.combo_extension.Items.Strings[i]) > 0 then
494 Break;
495 if i < Self.combo_extension.Items.Count then
496 Self.combo_extension.ItemIndex := i
497 else
498 Self.combo_extension.ItemIndex := -1;
499 end;
500 Self.check_zerobyte.Checked := zerobytes;
501 Self.LoadFileNames;
502end;
503
504procedure TForm_ToolTemplate.SetMultiSelect(allow: Boolean);
505begin
506 FAllowMultiSelect := allow;
507 filelist.MultiSelect := allow;
508end;
509
510
511procedure TForm_ToolTemplate.FormResize(Sender: TObject);
512begin
513 if Self.Width < 300 then
514 Self.Width := 300;
515 if Self.Height < 200 then
516 Self.Height := 200;
517end;
518
519
520
521procedure TForm_ToolTemplate.FormCreate(Sender: TObject);
522begin
523 Self.Width := 260;
524 Self.Height := 300;
525 FOnNewFileSelected := nil;
526 FOnNewConnection := nil;
527 FAllowedExts := '';
528 FAllowMultiSelect := False;
529end;
530
531procedure TForm_ToolTemplate.FormActivate(Sender: TObject);
532begin
533 if edit_filtername.CanFocus then
534 edit_filtername.SetFocus
535 else
536 if content.CanFocus then
537 content.SetFocus;
538end;
539
540procedure TForm_ToolTemplate.FormClose(Sender: TObject; var Action: TCloseAction);
541begin
542 Action := caFree;
543end;
544
545
546procedure AddToolListEntryExt(context, ext: String);
547var
548 i: Integer;
549begin
550 for i := 0 to High(ToolList) do
551 if ToolList[i].context = context then
552 begin
553 if Pos(ext, ToolList[i].exts) = 0 then
554 begin
555 if Length(ToolList[i].exts) = 0 then
556 ToolList[i].exts := ext
557 else
558 ToolList[i].exts := ToolList[i].exts + ',' + ext;
559 end;
560 Exit;
561 end;
562end;
563
564procedure AddToolListEntry(context, name, exts: String);
565var
566 i: Integer;
567begin
568 if Length(ToolList) > 0 then
569 begin
570 for i := 0 to High(ToolList) do
571 if ToolList[i].context = context then
572 begin
573 if (Length(ToolList[i].name) = 0) and (Length(name) > 0) then
574 ToolList[i].name := name;
575 if Length(exts) > 0 then
576 AddToolListEntryExt(context, exts);
577 Exit;
578 end;
579 end;
580 SetLength(ToolList, Length(ToolList) + 1);
581 for i := High(ToolList) downto 1 do
582 if ToolList[i - 1].name > name then
583 ToolList[i] := ToolList[i - 1]
584 else
585 Break;
586 ToolList[i].context := context;
587 ToolList[i].name := name;
588 ToolList[i].exts := exts;
589end;
590
591end.
Note: See TracBrowser for help on using the repository browser.