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

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