source: oup/releases/0.33a/Tools/Template.pas@ 219

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