1 | unit Template;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
|
---|
7 | Dialogs, ExtCtrls, StdCtrls, StrUtils,
|
---|
8 | OniDataClass, Functions, Data, Menus, Buttons;
|
---|
9 |
|
---|
10 | type
|
---|
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 | FSelectedFile: TFileInfo;
|
---|
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;
|
---|
73 | property SelectedFile: TFileInfo read FSelectedFile;
|
---|
74 | end;
|
---|
75 |
|
---|
76 | var
|
---|
77 | ToolList: TToolList;
|
---|
78 | procedure AddToolListEntry(context, name, exts: String);
|
---|
79 |
|
---|
80 | implementation
|
---|
81 | {$R *.dfm}
|
---|
82 | uses Main, Exporters;
|
---|
83 |
|
---|
84 |
|
---|
85 | procedure TForm_ToolTemplate.RecreateList;
|
---|
86 | var
|
---|
87 | i: LongWord;
|
---|
88 | exts: TStringArray;
|
---|
89 | begin
|
---|
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);
|
---|
105 | end;
|
---|
106 |
|
---|
107 |
|
---|
108 |
|
---|
109 |
|
---|
110 | procedure TForm_ToolTemplate.LoadFileNames;
|
---|
111 | var
|
---|
112 | Extension: String;
|
---|
113 | no_zero_bytes: Boolean;
|
---|
114 | pattern: String;
|
---|
115 | files: TStringArray;
|
---|
116 | i: LongWord;
|
---|
117 | begin
|
---|
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;
|
---|
137 | end;
|
---|
138 |
|
---|
139 |
|
---|
140 | procedure TForm_ToolTemplate.popup_exportClick(Sender: TObject);
|
---|
141 | var
|
---|
142 | id: Integer;
|
---|
143 | ext: String;
|
---|
144 | begin
|
---|
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);
|
---|
151 | end;
|
---|
152 |
|
---|
153 | procedure TForm_ToolTemplate.popup_importClick(Sender: TObject);
|
---|
154 | var
|
---|
155 | id: Integer;
|
---|
156 | finfo: TFileInfo;
|
---|
157 | fs: TFileStream;
|
---|
158 | data: TData;
|
---|
159 | begin
|
---|
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;
|
---|
181 | end;
|
---|
182 |
|
---|
183 | procedure TForm_ToolTemplate.popup_opentool(Sender: TObject);
|
---|
184 | var
|
---|
185 | sender_name, context: String;
|
---|
186 | id: Integer;
|
---|
187 | begin
|
---|
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);
|
---|
192 | end;
|
---|
193 |
|
---|
194 | procedure TForm_ToolTemplate.combo_extensionClick(Sender: TObject);
|
---|
195 | begin
|
---|
196 | LoadFileNames;
|
---|
197 | end;
|
---|
198 |
|
---|
199 |
|
---|
200 | constructor TForm_ToolTemplate.Create(AOwner: TComponent);
|
---|
201 | var
|
---|
202 | i: Integer;
|
---|
203 | item: TMenuItem;
|
---|
204 | begin
|
---|
205 | inherited;
|
---|
206 | RecreateList;
|
---|
207 | FSelectedFile.ID := -1;
|
---|
208 | FSelectedFile.FileName := '';
|
---|
209 | FSelectedFile.FileNameHex := '';
|
---|
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;
|
---|
221 | end;
|
---|
222 |
|
---|
223 | procedure TForm_ToolTemplate.filepopupPopup(Sender: TObject);
|
---|
224 | var
|
---|
225 | ext: String;
|
---|
226 | i: Integer;
|
---|
227 | begin
|
---|
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;
|
---|
236 | end;
|
---|
237 |
|
---|
238 | procedure TForm_ToolTemplate.check_zerobyteClick(Sender: TObject);
|
---|
239 | begin
|
---|
240 | LoadFileNames;
|
---|
241 | end;
|
---|
242 |
|
---|
243 | procedure TForm_ToolTemplate.btn_sortClick(Sender: TObject);
|
---|
244 | begin
|
---|
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;
|
---|
258 | end;
|
---|
259 |
|
---|
260 | procedure TForm_ToolTemplate.check_filternameClick(Sender: TObject);
|
---|
261 | begin
|
---|
262 | edit_filtername.Enabled := not check_filtername.Checked;
|
---|
263 | LoadFileNames;
|
---|
264 | end;
|
---|
265 |
|
---|
266 | procedure TForm_ToolTemplate.listClick(Sender: TObject);
|
---|
267 | var
|
---|
268 | fileid: Integer;
|
---|
269 | begin
|
---|
270 | if filelist.ItemIndex > -1 then
|
---|
271 | begin
|
---|
272 | fileid := OniDataConnection.ExtractFileID(
|
---|
273 | filelist.Items.Strings[filelist.ItemIndex]);
|
---|
274 | FSelectedFile := OniDataConnection.GetFileInfo(fileid);
|
---|
275 | if Assigned(FOnNewFileSelected) then
|
---|
276 | FOnNewFileSelected(FSelectedFile);
|
---|
277 | end;
|
---|
278 | end;
|
---|
279 |
|
---|
280 | procedure TForm_ToolTemplate.listMouseDown(Sender: TObject; Button: TMouseButton;
|
---|
281 | Shift: TShiftState; X, Y: Integer);
|
---|
282 | var
|
---|
283 | pt: TPoint;
|
---|
284 | begin
|
---|
285 | pt.X := x;
|
---|
286 | pt.Y := y;
|
---|
287 | // filelist.ItemIndex := filelist.ItemAtPos(pt, true);
|
---|
288 | // Self.listClick(Self);
|
---|
289 | end;
|
---|
290 |
|
---|
291 |
|
---|
292 |
|
---|
293 | procedure TForm_ToolTemplate.SelectFileID(id: Integer);
|
---|
294 | var
|
---|
295 | i: Integer;
|
---|
296 | begin
|
---|
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);
|
---|
306 | end;
|
---|
307 |
|
---|
308 | procedure TForm_ToolTemplate.SelectFileName(filename: String);
|
---|
309 | var
|
---|
310 | i: Integer;
|
---|
311 | begin
|
---|
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);
|
---|
318 | end;
|
---|
319 |
|
---|
320 | procedure TForm_ToolTemplate.SetAllowedExts(exts: String);
|
---|
321 | begin
|
---|
322 | FAllowedExts := exts;
|
---|
323 | RecreateList;
|
---|
324 | end;
|
---|
325 |
|
---|
326 | procedure TForm_ToolTemplate.SetFileFilters(pattern, extension: String;
|
---|
327 | zerobytes: Boolean);
|
---|
328 | var
|
---|
329 | i: Integer;
|
---|
330 | begin
|
---|
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;
|
---|
346 | end;
|
---|
347 |
|
---|
348 | procedure TForm_ToolTemplate.SetMultiSelect(allow: Boolean);
|
---|
349 | begin
|
---|
350 | FAllowMultiSelect := allow;
|
---|
351 | filelist.MultiSelect := allow;
|
---|
352 | end;
|
---|
353 |
|
---|
354 |
|
---|
355 | procedure TForm_ToolTemplate.FormResize(Sender: TObject);
|
---|
356 | begin
|
---|
357 | if Self.Width < 300 then
|
---|
358 | Self.Width := 300;
|
---|
359 | if Self.Height < 200 then
|
---|
360 | Self.Height := 200;
|
---|
361 | end;
|
---|
362 |
|
---|
363 |
|
---|
364 |
|
---|
365 | procedure TForm_ToolTemplate.FormCreate(Sender: TObject);
|
---|
366 | begin
|
---|
367 | Self.Width := 260;
|
---|
368 | Self.Height := 300;
|
---|
369 | FOnNewFileSelected := nil;
|
---|
370 | FAllowedExts := '';
|
---|
371 | FAllowMultiSelect := False;
|
---|
372 | end;
|
---|
373 |
|
---|
374 | procedure TForm_ToolTemplate.FormActivate(Sender: TObject);
|
---|
375 | begin
|
---|
376 | if edit_filtername.CanFocus then
|
---|
377 | edit_filtername.SetFocus
|
---|
378 | else
|
---|
379 | if content.CanFocus then
|
---|
380 | content.SetFocus;
|
---|
381 | end;
|
---|
382 |
|
---|
383 | procedure TForm_ToolTemplate.FormClose(Sender: TObject; var Action: TCloseAction);
|
---|
384 | begin
|
---|
385 | Action := caFree;
|
---|
386 | end;
|
---|
387 |
|
---|
388 |
|
---|
389 | procedure AddToolListEntryExt(context, ext: String);
|
---|
390 | var
|
---|
391 | i: Integer;
|
---|
392 | begin
|
---|
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;
|
---|
405 | end;
|
---|
406 |
|
---|
407 | procedure AddToolListEntry(context, name, exts: String);
|
---|
408 | var
|
---|
409 | i: Integer;
|
---|
410 | begin
|
---|
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;
|
---|
432 | end;
|
---|
433 |
|
---|
434 | end.
|
---|