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

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