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

Last change on this file since 85 was 83, checked in by alloc, 18 years ago
File size: 12.0 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;
12
13 TForm_ToolTemplate = class(TForm)
14 panel_files: TPanel;
15 filelist: TListBox;
16 panel_extension: TPanel;
17 label_ext: TLabel;
18 combo_extension: TComboBox;
19 check_zerobyte: TCheckBox;
20 edit_filtername: TEdit;
21 check_filtername: TCheckBox;
22 Splitter1: TSplitter;
23 content: TPanel;
24 filepopup: TPopupMenu;
25 popup_import: TMenuItem;
26 popup_export: TMenuItem;
27 popup_separator: TMenuItem;
28 importd: TOpenDialog;
29 exportd: TSaveDialog;
30 btn_sort_id_asc: TSpeedButton;
31 btn_sort_id_desc: TSpeedButton;
32 btn_sort_name_asc: TSpeedButton;
33 btn_sort_name_desc: TSpeedButton;
34 btn_sort_ext_asc: TSpeedButton;
35 btn_sort_ext_desc: TSpeedButton;
[51]36 Label1: TLabel;
37 Label2: TLabel;
[46]38 procedure RecreateList;
39 procedure LoadFileNames;
40 procedure SelectFileName(filename: String);
41 procedure SelectFileID(id: Integer);
42 procedure check_filternameClick(Sender: TObject);
43 procedure check_zerobyteClick(Sender: TObject);
44 procedure combo_extensionClick(Sender: TObject);
45 procedure listClick(Sender: TObject);
46 procedure listMouseDown(Sender: TObject; Button: TMouseButton;
47 Shift: TShiftState; X, Y: Integer);
48
49 procedure FormResize(Sender: TObject);
50 procedure FormCreate(Sender: TObject);
51 procedure FormClose(Sender: TObject; var Action: TCloseAction);
52 procedure popup_importClick(Sender: TObject);
53 procedure popup_exportClick(Sender: TObject);
54 procedure popup_opentool(Sender: TObject);
55 procedure filepopupPopup(Sender: TObject);
56 procedure btn_sortClick(Sender: TObject);
[58]57 procedure FormActivate(Sender: TObject);
[46]58 private
59 FSortBy: TSortType;
60 FOnNewFileSelected: TNewFileSelectedEvent;
61 FAllowedExts: String;
62 FAllowMultiSelect: Boolean;
[83]63 FSelectedFile: TFileInfo;
[46]64 procedure SetAllowedExts(exts: String);
65 procedure SetMultiSelect(allow: Boolean);
66 public
67 constructor Create(AOwner: TComponent); override;
68 procedure SetFileFilters(pattern, extension: String; zerobytes: Boolean);
69 published
70 property OnNewFileSelected: TNewFileSelectedEvent read FOnNewFileSelected write FOnNewFileSelected;
71 property AllowedExts: String read FAllowedExts write SetAllowedExts;
72 property AllowMultiSelect: Boolean read FAllowMultiSelect write SetMultiSelect;
[83]73 property SelectedFile: TFileInfo read FSelectedFile;
[46]74 end;
75
76var
77 ToolList: TToolList;
78procedure AddToolListEntry(context, name, exts: String);
79
80implementation
81{$R *.dfm}
82uses Main, Exporters;
83
84
85procedure TForm_ToolTemplate.RecreateList;
86var
87 i: LongWord;
88 exts: TStringArray;
89begin
90 combo_extension.Items.Clear;
91 combo_extension.Items.Add('_All files_ (' +
92 IntToStr(OniDataConnection.GetFilesCount) + ')');
93 exts := OniDataConnection.GetExtensionsList;
94 for i := 0 to High(exts) do
95 if Length(FAllowedExts) > 0 then
96 begin
97 if Pos(MidStr(exts[i],1,4), FAllowedExts) > 0 then
98 begin
99 combo_extension.Items.Add(exts[i]);
100 end;
101 end else
102 combo_extension.Items.Add(exts[i]);
103 combo_extension.ItemIndex := 0;
104 combo_extensionClick(Self);
105end;
106
107
108
109
110procedure TForm_ToolTemplate.LoadFileNames;
111var
112 Extension: String;
113 no_zero_bytes: Boolean;
114 pattern: String;
115 files: TStringArray;
116 i: LongWord;
117begin
118 Extension := MidStr(combo_extension.Items.Strings[combo_extension.ItemIndex], 1, 4);
119 no_zero_bytes := not check_zerobyte.Checked;
120 pattern := '';
121 if check_filtername.Checked then
122 pattern := edit_filtername.Text;
123 if Extension = '_All' then
124 if Length(FAllowedExts) > 0 then
125 Extension := FAllowedExts
126 else
127 Extension := '';
128
129 files := OniDataConnection.GetFilesList(extension, pattern, no_zero_bytes, FSortBy);
130
131 filelist.Visible := False;
132 filelist.Items.Clear;
133 if Length(files) > 0 then
134 for i := 0 to High(files) do
135 filelist.Items.Add(files[i]);
136 filelist.Visible := True;
137end;
138
139
140procedure TForm_ToolTemplate.popup_exportClick(Sender: TObject);
141var
142 id: Integer;
143 ext: String;
144begin
145 id := OniDataConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]);
146 ext := RightStr(filelist.Items.Strings[filelist.ItemIndex], 4);
147 exportd.Filter := 'Files of matching extension (*.' + ext + ')|*.' + ext + '|All files|*.*';
148 exportd.DefaultExt := ext;
149 if exportd.Execute then
150 ExportDatFile(id, exportd.FileName);
151end;
152
153procedure TForm_ToolTemplate.popup_importClick(Sender: TObject);
154var
155 id: Integer;
156 finfo: TFileInfo;
157 fs: TFileStream;
158 data: TData;
159begin
160 id := OniDataConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]);
161 finfo := OniDataConnection.GetFileInfo(id);
162
163 importd.Filter := 'Files of matching extension (*.' + finfo.Extension + ')|*.' +
164 finfo.Extension + '|All files|*.*';
165 if importd.Execute then
166 begin
167 fs := TFileStream.Create(importd.FileName, fmOpenRead);
168 if fs.Size <> finfo.Size then
169 ShowMessage('Can''t import ' + ExtractFilename(importd.FileName) +
170 ', file has to have same size as file in .dat.' + CrLf +
171 'Size of file in .dat: ' + FormatFileSize(finfo.Size) + CrLf +
172 'Size of chosen file: ' + FormatFileSize(fs.Size))
173 else begin
174 SetLength(data, fs.Size);
175 fs.Read(data[0], fs.Size);
176 OniDataConnection.UpdateDatFile(id, data);
177 Self.listClick(Self);
178 end;
179 fs.Free;
180 end;
181end;
182
183procedure TForm_ToolTemplate.popup_opentool(Sender: TObject);
184var
185 sender_name, context: String;
186 id: Integer;
187begin
188 sender_name := TComponent(Sender).Name;
189 id := OniDataConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]);
190 context := MidStr(sender_name, Pos('_', sender_name) + 1, Length(sender_name) - Pos('_', sender_name));
191 Form_Main.open_child(context, id);
192end;
193
194procedure TForm_ToolTemplate.combo_extensionClick(Sender: TObject);
195begin
196 LoadFileNames;
197end;
198
199
200constructor TForm_ToolTemplate.Create(AOwner: TComponent);
201var
202 i: Integer;
203 item: TMenuItem;
204begin
205 inherited;
206 RecreateList;
[83]207 FSelectedFile.ID := -1;
208 FSelectedFile.FileName := '';
209 FSelectedFile.FileNameHex := '';
[46]210 if Length(ToolList) > 0 then
211 begin
212 for i := 0 to High(ToolList) do
213 begin
214 item := TMenuItem.Create(filepopup);
215 item.Name := 'popup_' + ToolList[i].context;
216 item.Caption := 'Open with ' + ToolList[i].name;
217 item.OnClick := Self.popup_opentool;
218 filepopup.Items.Insert(i, item);
219 end;
220 end;
221end;
222
223procedure TForm_ToolTemplate.filepopupPopup(Sender: TObject);
224var
225 ext: String;
226 i: Integer;
227begin
228 ext := RightStr(filelist.Items.Strings[filelist.ItemIndex], 4);
229 for i := 0 to High(ToolList) do
230 begin
231 filepopup.Items.Items[i].Enabled := True;
232 if Length(ToolList[i].exts) > 0 then
233 if Pos(ext, ToolList[i].exts) = 0 then
234 filepopup.Items.Items[i].Enabled := False;
235 end;
236end;
237
238procedure TForm_ToolTemplate.check_zerobyteClick(Sender: TObject);
239begin
240 LoadFileNames;
241end;
242
243procedure TForm_ToolTemplate.btn_sortClick(Sender: TObject);
244begin
245 if btn_sort_id_asc.Down then
246 FSortBy := stIDAsc
247 else if btn_sort_id_desc.Down then
248 FSortBy := stIDDesc
249 else if btn_sort_name_asc.Down then
250 FSortBy := stNameAsc
251 else if btn_sort_name_desc.Down then
252 FSortBy := stNameDesc
253 else if btn_sort_ext_asc.Down then
254 FSortBy := stExtAsc
255 else if btn_sort_ext_desc.Down then
256 FSortBy := stExtDesc;
257 LoadFileNames;
258end;
259
260procedure TForm_ToolTemplate.check_filternameClick(Sender: TObject);
261begin
262 edit_filtername.Enabled := not check_filtername.Checked;
263 LoadFileNames;
264end;
265
266procedure TForm_ToolTemplate.listClick(Sender: TObject);
267var
268 fileid: Integer;
269begin
270 if filelist.ItemIndex > -1 then
271 begin
272 fileid := OniDataConnection.ExtractFileID(
273 filelist.Items.Strings[filelist.ItemIndex]);
[83]274 FSelectedFile := OniDataConnection.GetFileInfo(fileid);
[46]275 if Assigned(FOnNewFileSelected) then
[83]276 FOnNewFileSelected(FSelectedFile);
[46]277 end;
278end;
279
280procedure TForm_ToolTemplate.listMouseDown(Sender: TObject; Button: TMouseButton;
281 Shift: TShiftState; X, Y: Integer);
282var
283 pt: TPoint;
284begin
285 pt.X := x;
286 pt.Y := y;
[54]287// filelist.ItemIndex := filelist.ItemAtPos(pt, true);
288// Self.listClick(Self);
[46]289end;
290
291
292
293procedure TForm_ToolTemplate.SelectFileID(id: Integer);
294var
295 i: Integer;
296begin
297 filelist.ItemIndex := -1;
298 if filelist.Items.Count > 0 then
299 for i := 0 to filelist.Items.Count - 1 do
300 if OniDataConnection.ExtractFileID(filelist.Items.Strings[i]) = id then
301 begin
302 filelist.ItemIndex := i;
303 Break;
304 end;
305 Self.listClick(Self);
306end;
307
308procedure TForm_ToolTemplate.SelectFileName(filename: String);
309var
310 i: Integer;
311begin
312 filelist.ItemIndex := -1;
313 if filelist.Items.Count > 0 then
314 for i := 0 to filelist.Items.Count - 1 do
315 if filelist.Items.Strings[i] = filename then
316 filelist.ItemIndex := i;
317 Self.listClick(Self);
318end;
319
320procedure TForm_ToolTemplate.SetAllowedExts(exts: String);
321begin
322 FAllowedExts := exts;
323 RecreateList;
324end;
325
326procedure TForm_ToolTemplate.SetFileFilters(pattern, extension: String;
327 zerobytes: Boolean);
328var
329 i: Integer;
330begin
331 if Length(pattern) > 0 then
332 Self.edit_filtername.Text := pattern;
333 Self.check_filtername.Checked := Length(pattern) > 0;
334 if Length(extension) > 0 then
335 begin
336 for i := 0 to Self.combo_extension.Items.Count - 1 do
337 if Pos(extension, Self.combo_extension.Items.Strings[i]) > 0 then
338 Break;
339 if i < Self.combo_extension.Items.Count then
340 Self.combo_extension.ItemIndex := i
341 else
342 Self.combo_extension.ItemIndex := -1;
343 end;
344 Self.check_zerobyte.Checked := zerobytes;
345 Self.LoadFileNames;
346end;
347
348procedure TForm_ToolTemplate.SetMultiSelect(allow: Boolean);
349begin
350 FAllowMultiSelect := allow;
351 filelist.MultiSelect := allow;
352end;
353
354
355procedure TForm_ToolTemplate.FormResize(Sender: TObject);
356begin
357 if Self.Width < 300 then
358 Self.Width := 300;
359 if Self.Height < 200 then
360 Self.Height := 200;
361end;
362
363
364
365procedure TForm_ToolTemplate.FormCreate(Sender: TObject);
366begin
367 Self.Width := 260;
368 Self.Height := 300;
369 FOnNewFileSelected := nil;
370 FAllowedExts := '';
371 FAllowMultiSelect := False;
372end;
373
[58]374procedure TForm_ToolTemplate.FormActivate(Sender: TObject);
375begin
[70]376 if edit_filtername.CanFocus then
377 edit_filtername.SetFocus
378 else
379 if content.CanFocus then
380 content.SetFocus;
[58]381end;
382
[46]383procedure TForm_ToolTemplate.FormClose(Sender: TObject; var Action: TCloseAction);
384begin
385 Action := caFree;
386end;
387
388
389procedure AddToolListEntryExt(context, ext: String);
390var
391 i: Integer;
392begin
393 for i := 0 to High(ToolList) do
394 if ToolList[i].context = context then
395 begin
396 if Pos(ext, ToolList[i].exts) = 0 then
397 begin
398 if Length(ToolList[i].exts) = 0 then
399 ToolList[i].exts := ext
400 else
401 ToolList[i].exts := ToolList[i].exts + ',' + ext;
402 end;
403 Exit;
404 end;
405end;
406
407procedure AddToolListEntry(context, name, exts: String);
408var
409 i: Integer;
410begin
411 if Length(ToolList) > 0 then
412 begin
413 for i := 0 to High(ToolList) do
414 if ToolList[i].context = context then
415 begin
416 if (Length(ToolList[i].name) = 0) and (Length(name) > 0) then
417 ToolList[i].name := name;
418 if Length(exts) > 0 then
419 AddToolListEntryExt(context, exts);
420 Exit;
421 end;
422 end;
423 SetLength(ToolList, Length(ToolList) + 1);
424 for i := High(ToolList) downto 1 do
425 if ToolList[i - 1].name > name then
426 ToolList[i] := ToolList[i - 1]
427 else
428 Break;
429 ToolList[i].context := context;
430 ToolList[i].name := name;
431 ToolList[i].exts := exts;
432end;
433
434end.
Note: See TracBrowser for help on using the repository browser.