source: oup/releases/0.29a3/Unit8_binedit.pas@ 173

Last change on this file since 173 was 31, checked in by alloc, 18 years ago
File size: 29.4 KB
RevLine 
[31]1UNIT Unit8_binedit;
2INTERFACE
3USES
4 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
5 Dialogs, Wrapgrid, StdCtrls, Grids, StrUtils, MPHexEditor, ExtCtrls, Clipbrd,
6 Unit3_data, Unit2_functions, Unit9_data_structures, Unit4_exporters, Menus, Math,
7 VirtualTrees, VTHeaderPopup;
8
9TYPE
10 TForm8 = Class(TForm)
11 Splitter1: TSplitter;
12 panel_data: TPanel;
13 hex: TMPHexEditor;
14 Splitter2: TSplitter;
15 panel_files: TPanel;
16 list: TListBox;
17 panel_extension: TPanel;
18 lbl_filter: TLabel;
19 combo_extension: TComboBox;
20 Bevel1: TBevel;
21 panel_imexport: TPanel;
22 btn_export: TButton;
23 btn_import: TButton;
24 opend: TOpenDialog;
25 saved: TSaveDialog;
26 value_viewer: TWrapGrid;
27 Splitter3: TSplitter;
28 value_viewer_context: TPopupMenu;
29 value_viewer_context_copy: TMenuItem;
30 value_viewer_context_copyashex: TMenuItem;
31 value_viewer_context_copyasdec: TMenuItem;
32 value_viewer_context_copyasfloat: TMenuItem;
33 value_viewer_context_copyasbitset: TMenuItem;
34 value_viewer_context_copyasstring: TMenuItem;
35 check_zerobyte: TCheckBox;
36 edit_filtername: TEdit;
37 check_filtername: TCheckBox;
38 VST: TVirtualStringTree;
39 VTHPopup: TVTHeaderPopupMenu;
40 procedure VSTFocusChanged(Sender: TBaseVirtualTree; Node: PVirtualNode;
41 Column: TColumnIndex);
42 procedure VSTDblClick(Sender: TObject);
43 procedure VTHPopupColumnChange(const Sender: TBaseVirtualTree;
44 const Column: TColumnIndex; Visible: Boolean);
45 procedure VSTHeaderDragged(Sender: TVTHeader; Column: TColumnIndex;
46 OldPosition: Integer);
47 procedure VSTGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
48 Column: TColumnIndex; TextType: TVSTTextType; var CellText: WideString);
49 procedure hexKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
50 PROCEDURE LoadDat(_fileid:LongWord);
51 PROCEDURE LoadFileNames;
52 PROCEDURE check_filternameClick(Sender: TObject);
53 PROCEDURE check_zerobyteClick(Sender: TObject);
54 PROCEDURE combo_extensionClick(Sender: TObject);
55 PROCEDURE panel_extensionResize(Sender: TObject);
56 PROCEDURE listClick(Sender: TObject);
57 PROCEDURE Recreatelist;
58
59 PROCEDURE FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
60 PROCEDURE value_viewerDblClick(Sender: TObject);
61 PROCEDURE value_viewer_context_copyClick(Sender: TObject);
62 PROCEDURE value_viewerMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
63 PROCEDURE value_viewer_contextPopup(Sender: TObject);
64 PROCEDURE FormActivate(Sender: TObject);
65 PROCEDURE btn_importClick(Sender: TObject);
66 PROCEDURE btn_exportClick(Sender: TObject);
67 PROCEDURE panel_imexportResize(Sender: TObject);
68 FUNCTION Save:Boolean;
69 PROCEDURE FormClose(Sender: TObject; var Action: TCloseAction);
70 FUNCTION GetValue(datatype:Word; offset:LongWord):String;
71 PROCEDURE WriteStructureInfos; //(structinfoid:Integer);
72 PROCEDURE hexSelectionChanged(Sender: TObject);
73 PROCEDURE hexChange(Sender: TObject);
74 PROCEDURE FormResize(Sender: TObject);
75 PROCEDURE ClearStructViewer;
76 PROCEDURE FormCloseQuery(Sender: TObject; var CanClose: Boolean);
77 PROCEDURE FormCreate(Sender: TObject);
78 PROCEDURE ClearValues;
79 PROCEDURE WriteValues;
80 PROCEDURE SetNewValue(datatype:Word; offset:LongWord; value:String);
81 PRIVATE
82 PUBLIC
83 END;
84
85VAR
86 Form8: TForm8;
87
88IMPLEMENTATION
89{$R *.dfm}
90USES Unit1_main, Unit12_ValueEdit, Unit13_rawedit;
91VAR
92 fileid:LongWord;
93
94TYPE
95 PNodeData = ^TNodeData;
96 TNodeData = record
97 Caption:String;
98 Offset:LongInt;
99 DataType:Word;
100 Value:String;
101 Description:String;
102 end;
103
104
105function AddVSTEntry(AVST:TCustomVirtualStringTree; ANode:PVirtualNode; ARecord:TNodeData):PVirtualNode;
106 var
107 data:PNodeData;
108 begin
109 Result:=AVST.AddChild(ANode);
110 data:=AVST.GetNodeData(Result);
111 AVST.ValidateNode(Result,False);
112 data^:=ARecord;
113 end;
114
115
116
117PROCEDURE TForm8.LoadDat(_fileid:LongWord);
118 VAR
119 i:LongWord;
120 mem:TMemoryStream;
121 data:Tdata;
122 BEGIN
123 IF hex.Modified THEN BEGIN
124 IF NOT Save THEN BEGIN
125 FOR i:=0 TO list.Count-1 DO BEGIN
126 IF GetFileIDByName(list.Items.Strings[i])=fileid THEN BEGIN
127 list.ItemIndex:=i;
128 Exit;
129 END;
130 END;
131 END;
132 END;
133 fileid:=_fileid;
134 FOR i:=0 TO list.Count-1 DO
135 IF GetFileIDByName(list.Items.Strings[i])=fileid THEN
136 list.ItemIndex:=i;
137 Self.ClearStructViewer;
138 data:=LoadDatFile(fileid);
139 IF Length(data)>0 THEN BEGIN
140 mem:=TMemoryStream.Create;
141 mem.Write(data[0],Length(data));
142 mem.Seek(0,soFromBeginning);
143 hex.LoadFromStream(mem);
144 mem.Free;
145 WriteStructureInfos;
146 END ELSE BEGIN
147 ClearValues;
148 hex.DataSize:=0;
149 END;
150 END;
151
152PROCEDURE TForm8.Recreatelist;
153 VAR
154 i:LongWord;
155 exts:TStringList;
156 BEGIN
157 combo_extension.Items.Clear;
158 combo_extension.Items.Add('_All files_ ('+IntToStr(GetFilesCount)+')');
159 exts:=GetExtensionsList;
160 FOR i:=0 TO High(exts) DO
161 combo_extension.Items.Add(exts[i]);
162 combo_extension.ItemIndex:=0;
163 combo_extensionClick(Self);
164 END;
165
166PROCEDURE TForm8.LoadFileNames;
167 VAR
168 Extension:String[4];
169 no_zero_bytes:Boolean;
170 pattern:String;
171 files:TStringList;
172 i:LongWord;
173 BEGIN
174 Extension:=MidStr(combo_extension.Items.Strings[combo_extension.ItemIndex],1,4);
175 no_zero_bytes:=NOT check_zerobyte.Checked;
176 pattern:='';
177 IF check_filtername.Checked THEN pattern:=edit_filtername.Text;
178 IF Extension='_All' THEN Extension:='';
179
180 files:=GetFilesList(extension,pattern,no_zero_bytes);
181 list.Items.Clear;
182 IF Length(files)>0 THEN
183 FOR i:=0 TO High(files) DO
184 list.Items.Add(files[i]);
185 END;
186
187PROCEDURE TForm8.panel_extensionResize(Sender: TObject);
188 BEGIN
189 combo_extension.Width:=panel_extension.Width-5;
190 edit_filtername.Width:=panel_extension.Width-5;
191 END;
192
193PROCEDURE TForm8.combo_extensionClick(Sender: TObject);
194 BEGIN
195 LoadFileNames;
196 END;
197
198PROCEDURE TForm8.check_zerobyteClick(Sender: TObject);
199 VAR
200 i:Byte;
201 BEGIN
202 LoadFileNames;
203 END;
204
205PROCEDURE TForm8.check_filternameClick(Sender: TObject);
206 BEGIN
207 edit_filtername.Enabled:=NOT check_filtername.Checked;
208 LoadFileNames;
209 END;
210
211PROCEDURE TForm8.listClick(Sender: TObject);
212 VAR
213 mem:TMemoryStream;
214 data:Tdata;
215 i:LongWord;
216 BEGIN
217 LoadDat(GetFileIDByName(list.Items.Strings[list.ItemIndex]));
218 END;
219
220
221
222
223FUNCTION IntToBin(value:Byte):String;
224 VAR i:Byte;
225 BEGIN
226 Result:='';
227 FOR i:=7 DOWNTO 0 DO BEGIN
228 Result:=Result+IntToStr((value SHR i) AND $01);
229 END;
230 END;
231
232FUNCTION TForm8.GetValue(datatype:Word; offset:LongWord):String;
233 VAR
234 data:Tdata;
235 i:Word;
236 BEGIN
237 CASE datatype OF
238 1: Result:=IntToStr(hex.data[offset]);
239 2: Result:=IntToStr(hex.data[offset]+hex.data[offset+1]*256);
240 3: Result:=IntToStr(hex.data[offset]+hex.data[offset+1]*256+hex.data[offset+2]*256*256);
241 4: Result:=IntToStr(hex.data[offset]+hex.data[offset+1]*256+hex.data[offset+2]*256*256+hex.data[offset+3]*256*256*256);
242 5: Result:='0x'+IntToHex(hex.data[offset],2);
243 6: Result:='0x'+IntToHex(hex.data[offset]+hex.data[offset+1]*256,4);
244 7: Result:='0x'+IntToHex(hex.data[offset]+hex.data[offset+1]*256+hex.data[offset+2]*256*256,6);
245 8: Result:='0x'+IntToHex(hex.data[offset]+hex.data[offset+1]*256+hex.data[offset+2]*256*256+hex.data[offset+3]*256*256*256,8);
246 9: BEGIN
247 SetLength(data,4);
248 data[0]:=hex.data[offset];
249 data[1]:=hex.data[offset+1];
250 data[2]:=hex.data[offset+2];
251 data[3]:=hex.data[offset+3];
252 Result:=FloatToStr(Decode_Float(data));
253 END;
254 10: Result:=IntToBin(hex.data[offset]);
255 11: Result:='0x'+IntToHex(GetRawInfo(fileid,offset).raw_addr,8);
256 12: Result:=FormatNumber(hex.data[offset+1]+hex.data[offset+2]*256+hex.data[offset+3]*256*256,5,'0');
257 13: Result:=IntToStr(hex.data[offset]);
258 14: Result:=IntToStr(hex.data[offset]+hex.data[offset+1]*256);
259 15: Result:=IntToStr(hex.data[offset]+hex.data[offset+1]*256+hex.data[offset+2]*256*256);
260 16: Result:=IntToStr(hex.data[offset]+hex.data[offset+1]*256+hex.data[offset+2]*256*256+hex.data[offset+3]*256*256*256);
261 1000..9999: BEGIN
262 Result:='';
263 FOR i:=1 TO datatype-1000 DO BEGIN
264 IF hex.Data[offset+i-1]>=32 THEN
265 Result:=Result+Chr(hex.Data[offset+i-1])
266 ELSE
267 Result:=Result+'.';
268 END;
269 END;
270 10000..65535: BEGIN
271 Result:='';
272 FOR i:=1 TO datatype-10000 DO BEGIN
273 IF hex.Data[offset+i-1]>=32 THEN
274 Result:=Result+Chr(hex.Data[offset+i-1])
275 ELSE
276 Result:=Result+'.';
277 END;
278 END;
279 END;
280 END;
281
282PROCEDURE TForm8.WriteStructureInfos;
283 VAR
284 i,j:LongWord;
285 pdata: PNodeData;
286 data: TNodeData;
287 node: PVirtualNode;
288 structs: TStructDef;
289 BEGIN
290 VST.BeginUpdate;
291 IF VST.RootNodeCount=0 THEN BEGIN
292 structs:=LoadStructureDefinition(fileid);
293 IF structs.data THEN BEGIN
294 IF Length(structs.Global)>0 THEN BEGIN
295 FOR i:=0 TO High(structs.Global) DO BEGIN
296 data.Caption:=structs.Global[i].name;
297 data.Offset:=structs.Global[i].offset;
298 data.DataType:=structs.Global[i].datatype;
299 data.Value:=GetValue(structs.Global[i].datatype, structs.Global[i].offset);
300 data.Description:=structs.Global[i].description;
301 AddVSTEntry(VST, nil, data);
302 END;
303 END;
304 IF Length(structs.Subs)>0 THEN BEGIN
305 FOR i:=0 TO High(structs.Subs) DO BEGIN
306 WITH structs.Subs[i] DO BEGIN
307 IF Length(Entries)>0 THEN BEGIN
308 IF Pos('#',SubName)>0 THEN BEGIN
309 data.Offset:=HexToLong(MidStr(SubName, Pos('#',SubName)+1, 8));
310 data.Value:=MidStr(SubName, PosEx('#',SubName,Pos('#',SubName)+1)+1, 8);
311 data.Caption:=MidStr(SubName, 1, Pos('#',SubName)-1);
312 data.Description:=SubDesc;
313 END ELSE BEGIN
314 data.Caption:=SubName;
315 data.Description:=SubDesc;
316 data.Offset:=0;
317 data.Value:='';
318 END;
319 data.DataType:=0;
320 node:=AddVSTEntry(VST, nil, data);
321 data.Description:='';
322 FOR j:=0 TO High(Entries) DO BEGIN
323 data.Caption:=Entries[j].name;
324 data.Offset:=Entries[j].offset;
325 data.DataType:=Entries[j].datatype;
326 data.Value:=GetValue(Entries[j].datatype, Entries[j].offset);
327 data.Description:=Entries[j].description;
328 AddVSTEntry(VST, node, data);
329 END;
330 END;
331 END;
332 END;
333 END;
334 END;
335 IF VST.RootNodeCount>0 THEN
336 VST.FocusedNode:=VST.GetFirst;
337 END ELSE BEGIN
338 Node:=VST.GetFirst;
339 WHILE Assigned(Node) DO BEGIN
340 pdata:=VST.GetNodeData(Node);
341 IF pdata.DataType>0 THEN
342 pdata.Value:=GetValue(pdata.Datatype, pdata.Offset);
343 Node:=VST.GetNext(Node);
344 END;
345 END;
346 VST.EndUpdate;
347 END;
348
349PROCEDURE TForm8.ClearValues;
350 VAR
351 i:Byte;
352 BEGIN
353 FOR i:=1 TO value_viewer.RowCount-1 DO BEGIN
354 value_viewer.Cells[1,i]:='';
355 END;
356 END;
357
358PROCEDURE TForm8.WriteValues;
359 VAR
360 i,j:Byte;
361 data:Tdata;
362 str:String;
363 value:LongWord;
364 BEGIN
365 FOR i:=1 TO value_viewer.RowCount-1 DO BEGIN
366 IF value_viewer.Cells[0,i]='1 byte, unsigned' THEN BEGIN
367 IF ((hex.SelCount=1) OR (hex.SelCount=0)) AND NOT ((hex.SelStart+1)>hex.DataSize) THEN BEGIN
368 value:=hex.Data[hex.SelStart];
369 value_viewer.Cells[1,i]:=IntToStr( value )+' / 0x'+IntToHex( value , 2 );
370 END ELSE
371 value_viewer.Cells[1,i]:='';
372 END;
373 IF value_viewer.Cells[0,i]='2 bytes, unsigned' THEN BEGIN
374 IF ((hex.SelCount=2) OR (hex.SelCount=0)) AND NOT ((hex.SelStart+2)>hex.DataSize) THEN BEGIN
375 value:=hex.Data[hex.SelStart] + hex.Data[hex.SelStart+1]*256;
376 value_viewer.Cells[1,i]:=IntToStr( value )+' / 0x'+IntToHex( value , 4 );
377 END ELSE
378 value_viewer.Cells[1,i]:='';
379 END;
380 IF value_viewer.Cells[0,i]='4 bytes, unsigned' THEN BEGIN
381 IF ((hex.SelCount=4) OR (hex.SelCount=0)) AND NOT ((hex.SelStart+4)>hex.DataSize) THEN BEGIN
382 value:=hex.Data[hex.SelStart]+hex.Data[hex.SelStart+1]*256+hex.Data[hex.SelStart+2]*256*256+hex.Data[hex.SelStart+3]*256*256*256;
383 value_viewer.Cells[1,i]:=IntToStr( value )+' / 0x'+IntToHex( value , 8 );
384 END ELSE
385 value_viewer.Cells[1,i]:='';
386 END;
387 IF value_viewer.Cells[0,i]='Bitset' THEN BEGIN
388 IF (hex.SelCount<=8) THEN BEGIN
389 IF hex.SelCount=0 THEN BEGIN
390 SetLength(data,1);
391 data[0]:=hex.Data[hex.SelStart];
392 END ELSE BEGIN
393 SetLength(data,hex.SelCount);
394 FOR j:=0 TO hex.SelCount-1 DO
395 data[j]:=hex.Data[hex.SelStart+j];
396 END;
397 value_viewer.Cells[1,i]:=DataToBin(data);
398 END ELSE
399 value_viewer.Cells[1,i]:='';
400 END;
401 IF value_viewer.Cells[0,i]='Float' THEN BEGIN
402 IF ((hex.SelCount=4) OR (hex.SelCount=0)) AND NOT ((hex.SelStart+4)>hex.DataSize) THEN BEGIN
403 SetLength(data,4);
404 FOR j:=0 TO 3 DO
405 data[j]:=hex.Data[hex.SelStart+j];
406 value_viewer.Cells[1,i]:=FloatToStr(Decode_Float(data));
407 END ELSE
408 value_viewer.Cells[1,i]:='';
409 END;
410 IF value_viewer.Cells[0,i]='Selected length' THEN BEGIN
411 value_viewer.Cells[1,i]:=IntToStr(hex.SelCount)+' bytes';
412 END;
413 IF value_viewer.Cells[0,i]='String' THEN BEGIN
414 j:=0;
415 str:='';
416 IF hex.SelCount=0 THEN BEGIN
417{ WHILE (hex.Data[hex.SelStart+j]>0) AND ((hex.SelStart+j)<hex.DataSize) DO BEGIN
418 IF hex.Data[hex.selstart+j]>=32 THEN
419 str:=str+Char(hex.Data[hex.SelStart+j])
420 ELSE
421 str:=str+'.';
422 Inc(j);
423 END;
424} END ELSE BEGIN
425 FOR j:=1 TO hex.SelCount DO
426 str:=str+Char(hex.Data[hex.SelStart+j-1]);
427 END;
428 value_viewer.Cells[1,i]:=str;
429 END;
430 END;
431 END;
432
433PROCEDURE TForm8.FormCreate(Sender: TObject);
434 BEGIN
435 Self.Caption:='';
436 fileid:=0;
437 VST.NodeDataSize:=SizeOf(TNodeData);
438 value_viewer.ColCount:=2;
439 value_viewer.RowCount:=8;
440 value_viewer.FixedRows:=1;
441 value_viewer.Cells[0,0]:='Type';
442 value_viewer.Cells[1,0]:='Value';
443 value_viewer.Cells[0,1]:='1 byte, unsigned';
444 value_viewer.Cells[0,2]:='2 bytes, unsigned';
445 value_viewer.Cells[0,3]:='4 bytes, unsigned';
446 value_viewer.Cells[0,4]:='Bitset';
447 value_viewer.Cells[0,5]:='Float';
448 value_viewer.Cells[0,6]:='String';
449 value_viewer.Cells[0,7]:='Selected length';
450 value_viewer.ColWidths[0]:=100;
451 hex.Height:=panel_data.Height-215;
452 END;
453
454FUNCTION TForm8.Save:Boolean;
455 VAR
456 mem:TMemoryStream;
457 data:Tdata;
458 i:LongWord;
459 BEGIN
460 CASE MessageBox(Self.Handle,PChar('Save changes to file '+GetFileInfo(fileid).FileName+'?'),PChar('Data changed...'),MB_YESNOCANCEL) OF
461 IDYES: BEGIN
462 mem:=TMemoryStream.Create;
463 hex.SaveToStream(mem);
464 mem.Seek(0,soFromBeginning);
465 SetLength(data,mem.Size);
466 mem.Read(data[0],mem.Size);
467 mem.Free;
468 UpdateDatFile(fileid,data);
469 hex.Modified:=False;
470 FOR i:=0 TO hex.Datasize-1 DO hex.ByteChanged[i]:=False;
471 Result:=True;
472 END;
473 IDNO: Result:=True;
474 IDCANCEL: BEGIN
475 Result:=False;
476 END;
477 END;
478 END;
479
480PROCEDURE TForm8.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
481 BEGIN
482 IF hex.Modified THEN BEGIN
483 IF NOT Save THEN CanClose:=False;
484 END;
485 END;
486
487PROCEDURE TForm8.ClearStructViewer;
488 BEGIN
489 VST.Clear;
490 END;
491
492PROCEDURE TForm8.FormResize(Sender: TObject);
493 BEGIN
494 IF Self.Width>=650 THEN BEGIN
495 END ELSE Self.Width:=650;
496 IF Self.Height>=450 THEN BEGIN
497 END ELSE Self.Height:=450;
498 END;
499
500PROCEDURE TForm8.hexChange(Sender: TObject);
501 BEGIN
502 ClearValues;
503 IF hex.DataSize>0 THEN BEGIN
504 WriteStructureInfos;
505 WriteValues;
506 END;
507 END;
508
509PROCEDURE TForm8.hexKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
510 VAR
511 temps: String;
512 BEGIN
513 IF (Shift=[ssCtrl]) AND (Key=Ord('C')) THEN BEGIN
514 IF hex.SelCount>0 THEN BEGIN
515 IF hex.InCharField THEN
516 Clipboard.AsText:=hex.SelectionAsText
517 ELSE
518 Clipboard.AsText:=hex.SelectionAsHex;
519 END;
520 END;
521 IF (Shift=[ssCtrl]) AND (Key=Ord('V')) THEN BEGIN
522{ temps:=Clipboard.AsText;
523 IF hex.SelStart+Length(temps)>hex.DataSize THEN
524 SetLength(temps, hex.DataSize-hex.SelStart);
525 hex.Sel
526 hex.SelCount:=Length(temps);
527 hex.ReplaceSelection(temps,Length(temps));
528} END;
529 END;
530
531PROCEDURE TForm8.hexSelectionChanged(Sender: TObject);
532 VAR
533 selstart:Integer;
534 i,j:Word;
535
536 node:PVirtualNode;
537 pdata:PNodeData;
538 BEGIN
539 IF hex.DataSize>0 THEN BEGIN
540 WriteValues;
541 selstart:=hex.SelStart;
542 IF VST.RootNodeCount>0 THEN BEGIN
543 Node:=VST.GetFirst;
544 WHILE Assigned(Node) DO BEGIN
545 pdata:=VST.GetNodeData(Node);
546 IF pdata.DataType>0 THEN BEGIN
547 IF ((selstart-pdata.Offset)<GetTypeDataLength(pdata.DataType)) AND ((selstart-pdata.Offset)>=0) THEN BEGIN
548 VST.FocusedNode:=Node;
549 VST.Selected[Node]:=True;
550 Break;
551 END;
552 END;
553 Node:=VST.GetNext(Node);
554 END;
555 END;
556 END;
557 END;
558
559PROCEDURE TForm8.FormClose(Sender: TObject; var Action: TCloseAction);
560 BEGIN
561 Action:=caFree;
562 Form1.close_window(Self.Name);
563 END;
564
565PROCEDURE TForm8.panel_imexportResize(Sender: TObject);
566 BEGIN
567 btn_import.Width:=panel_imexport.Width-8;
568 btn_export.Width:=panel_imexport.Width-8;
569 END;
570
571PROCEDURE TForm8.btn_exportClick(Sender: TObject);
572 BEGIN
573 saved.Filter:='Files of matching extension (*.'+GetFileInfo(fileid).Extension+')|*.'+dat_files[fileid].Extension+'|All files|*.*';
574 saved.DefaultExt:=GetFileInfo(fileid).Extension;
575 IF saved.Execute THEN BEGIN
576 ExportDatFile(fileid,saved.FileName);
577 END;
578 END;
579
580PROCEDURE TForm8.btn_importClick(Sender: TObject);
581 VAR
582 data:Tdata;
583 fs:TFileStream;
584 BEGIN
585 opend.Filter:='Files of matching extension (*.'+GetFileInfo(fileid).Extension+')|*.'+dat_files[fileid].Extension+'|All files|*.*';
586 IF opend.Execute THEN BEGIN
587 fs:=TFileStream.Create(opend.FileName,fmOpenRead);
588 IF fs.Size<>hex.DataSize THEN BEGIN
589 ShowMessage('Can''t import '+ExtractFilename(opend.FileName)+
590 ', file has to have same size as file in .dat.'+CrLf+
591 'Size of file in .dat: '+FormatFileSize(hex.datasize)+CrLf+
592 'Size of chosen file: '+FormatFileSize(fs.Size));
593 END ELSE BEGIN
594 hex.LoadFromStream(fs);
595 hex.Modified:=True;
596 END;
597 fs.Free;
598 END;
599 END;
600
601PROCEDURE TForm8.FormActivate(Sender: TObject);
602 BEGIN
603 Form1.SetActiveWindow(Self.Name);
604 END;
605
606PROCEDURE TForm8.value_viewer_contextPopup(Sender: TObject);
607 VAR
608 i:Byte;
609 BEGIN
610 FOR i:=0 TO value_viewer_context.Items.Count-1 DO
611 value_viewer_context.Items.Items[i].Visible:=False;
612 WITH value_viewer DO BEGIN
613 IF (Col=1) AND (Row>0) AND (Length(Cells[Col,Row])>0) THEN BEGIN
614 IF Pos(' byte',Cells[0,Row])=2 THEN BEGIN
615 value_viewer_context.Items.Find('Copy to &clipboard').Visible:=True;
616 value_viewer_context.Items.Find('Copy to clipboard (as &dec)').Visible:=True;
617 value_viewer_context.Items.Find('Copy to clipboard (as &hex)').Visible:=True;
618 END;
619 IF Pos('Float',Cells[0,Row])=1 THEN
620 value_viewer_context.Items.Find('Copy to clipboard (as &float)').Visible:=True;
621 IF Pos('Bitset',Cells[0,Row])=1 THEN
622 value_viewer_context.Items.Find('Copy to clipboard (as &bitset)').Visible:=True;
623 IF Pos('String',Cells[0,Row])=1 THEN
624 value_viewer_context.Items.Find('Copy to clipboard (as &string)').Visible:=True;
625 IF Pos('Selected length',Cells[0,Row])=1 THEN
626 value_viewer_context.Items.Find('Copy to &clipboard').Visible:=True;
627 END;
628 END;
629 END;
630
631PROCEDURE TForm8.value_viewerMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
632 VAR
633 ACol,ARow:Integer;
634 BEGIN
635 IF Button=mbRight THEN BEGIN
636 value_viewer.MouseToCell(x,y,ACol,ARow);
637 IF ARow>0 THEN BEGIN
638 value_viewer.Col:=ACol;
639 value_viewer.Row:=ARow;
640 END;
641 END;
642 END;
643
644PROCEDURE TForm8.value_viewer_context_copyClick(Sender: TObject);
645 VAR
646 i:Byte;
647 name:String;
648 value:LongWord;
649 BEGIN
650 name:=TMenuItem(Sender).Name;
651 IF Pos('asstring',name)>0 THEN BEGIN
652 Clipboard.AsText:=value_viewer.Cells[value_viewer.Col,value_viewer.Row];
653 END ELSE
654 IF Pos('asfloat',name)>0 THEN BEGIN
655 Clipboard.AsText:=value_viewer.Cells[value_viewer.Col,value_viewer.Row];
656 END ELSE
657 IF Pos('asbitset',name)>0 THEN BEGIN
658 Clipboard.AsText:=value_viewer.Cells[value_viewer.Col,value_viewer.Row];
659 END ELSE
660 IF (Pos('ashex',name)>0) OR (Pos('asdec',name)>0) THEN BEGIN
661 IF value_viewer.Cells[0,value_viewer.Row]='1 byte, unsigned' THEN BEGIN
662 IF ((hex.SelCount=1) OR (hex.SelCount=0)) AND NOT ((hex.SelStart+1)>hex.DataSize) THEN
663 value:=hex.Data[hex.SelStart];
664 END;
665 IF value_viewer.Cells[0,value_viewer.Row]='2 bytes, unsigned' THEN BEGIN
666 IF ((hex.SelCount=2) OR (hex.SelCount=0)) AND NOT ((hex.SelStart+2)>hex.DataSize) THEN
667 value:=hex.Data[hex.SelStart] + hex.Data[hex.SelStart+1]*256;
668 END;
669 IF value_viewer.Cells[0,value_viewer.Row]='4 bytes, unsigned' THEN BEGIN
670 IF ((hex.SelCount=4) OR (hex.SelCount=0)) AND NOT ((hex.SelStart+4)>hex.DataSize) THEN
671 value:=hex.Data[hex.SelStart]+hex.Data[hex.SelStart+1]*256+hex.Data[hex.SelStart+2]*256*256+hex.Data[hex.SelStart+3]*256*256*256;
672 END;
673 IF Pos('asdec',name)>0 THEN BEGIN
674 Clipboard.AsText:=IntToStr(value);
675 END ELSE BEGIN
676 IF value_viewer.Cells[0,value_viewer.Row]='1 byte, unsigned' THEN
677 Clipboard.AsText:='0x'+IntToHex(value,2);
678 IF value_viewer.Cells[0,value_viewer.Row]='2 bytes, unsigned' THEN
679 Clipboard.AsText:='0x'+IntToHex(value,4);
680 IF value_viewer.Cells[0,value_viewer.Row]='4 bytes, unsigned' THEN
681 Clipboard.AsText:='0x'+IntToHex(value,8);
682 END;
683 END ELSE BEGIN
684 Clipboard.AsText:=value_viewer.Cells[value_viewer.Col,value_viewer.Row];
685 END;
686 END;
687
688procedure TForm8.VSTDblClick(Sender: TObject);
689 var
690 node:PVirtualNode;
691 nodedata:PNodeData;
692 begin
693 if VST.FocusedColumn=3 then begin
694 node:=VST.FocusedNode;
695 nodedata:=VST.GetNodeData(node);
696
697 IF NOT (nodedata.datatype IN [11,12]) THEN BEGIN
698 Form12.MakeVarInput(nodedata.Caption,nodedata.offset,nodedata.datatype,nodedata.value,Self);
699 END ELSE BEGIN
700 IF nodedata.DataType=11 THEN BEGIN
701 IF GetRawInfo(fileid,nodedata.offset).raw_size>0 THEN BEGIN
702 IF Form1.open_child('rawedit') THEN BEGIN
703 TForm13(Form1.ActiveMDIChild).LoadRaw(GetRawInfo(fileid,nodedata.offset));
704 END;
705 END;
706 END;
707 IF nodedata.DataType=12 THEN BEGIN
708 IF (StrToInt(nodedata.Value)<GetFilesCount) AND
709 (StrToInt(nodedata.Value)>0) AND
710 (StrToInt(nodedata.Value)<>fileid) THEN BEGIN
711 IF GetFileInfo(StrToInt(nodedata.Value)).Size>0 THEN BEGIN
712 IF Form1.open_child('binedit') THEN BEGIN
713 TForm8(Form1.ActiveMDIChild).LoadDat(StrToInt(nodedata.Value));
714 END;
715 END ELSE BEGIN
716 ShowMessage('Linked filed is a zero-byte-file');
717 END;
718 END;
719 END;
720 END;
721
722 end;
723 end;
724
725procedure TForm8.VSTFocusChanged(Sender: TBaseVirtualTree; Node: PVirtualNode;
726 Column: TColumnIndex);
727 var
728 data:PNodeData;
729 begin
730 data:=VST.GetNodeData(node);
731 IF data.DataType>0 THEN BEGIN
732 hex.SelStart:=data.Offset;
733 hex.SelEnd:=data.Offset+GetTypeDataLength(data.DataType)-1;
734 END ELSE BEGIN
735 hex.SelStart:=data.Offset;
736 hex.SelEnd:=data.Offset+HexToLong(data.Value)-1;
737 END;
738 end;
739
740procedure TForm8.VSTGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
741 Column: TColumnIndex; TextType: TVSTTextType; var CellText: WideString);
742 var
743 data:PNodeData;
744 begin
745 data := Sender.GetNodeData(Node);
746 CellText := '';
747 if TextType = ttNormal then begin
748 case Column of
749 0: CellText := data.Caption;
750 1:
751 if data.DataType>0 then
752 CellText := '0x'+IntToHex(data.Offset,8)
753 else
754 if data.Offset>0 then
755 CellText := '0x'+IntToHex(data.Offset,8);
756 2:
757 if data.DataType>0 then
758 CellText := GetDataType(data.DataType);
759 3:
760 if data.DataType>0 then
761 CellText := GetValue(data.DataType, data.Offset)
762 else
763 if Length(data.Value)>0 then
764 CellText := IntToStr(HexToLong(data.Value))+' Bytes';
765 4:
766 CellText := data.Description;
767 end;
768 end;
769 end;
770
771procedure TForm8.VSTHeaderDragged(Sender: TVTHeader; Column: TColumnIndex;
772 OldPosition: Integer);
773 begin
774 if Sender.Columns.Items[column].Position<1 then
775 Sender.Columns.Items[column].Position:=OldPosition;
776 end;
777
778procedure TForm8.VTHPopupColumnChange(const Sender: TBaseVirtualTree;
779 const Column: TColumnIndex; Visible: Boolean);
780 begin
781 if column=0 then
782 TVirtualStringTree(Sender).Header.Columns.Items[column].Options:=TVirtualStringTree(Sender).Header.Columns.Items[column].Options+[coVisible];
783 end;
784
785PROCEDURE TForm8.SetNewValue(datatype:Word; offset:LongWord; value:String);
786 VAR
787 data:Tdata;
788 value_int:LongWord;
789 value_float:Single;
790 i:Word;
791 BEGIN
792 CASE datatype OF
793 1..4: BEGIN
794 value_int:=StrToInt(value);
795 SetLength(data,datatype);
796 FOR i:=0 TO datatype-1 DO BEGIN
797 data[i]:=value_int MOD 256;
798 value_int:=value_int DIV 256;
799 END;
800 END;
801 5..8: BEGIN
802 value_int:=StrToInt('$'+value);
803 SetLength(data,datatype-4);
804 FOR i:=0 TO datatype-5 DO BEGIN
805 data[i]:=value_int MOD 256;
806 value_int:=value_int DIV 256;
807 END;
808 END;
809 9: BEGIN
810 value_float:=StrToFloat(value);
811 data:=Encode_Float(value_float);
812 END;
813 10: BEGIN
814 value_int:=BinToInt(value);
815 SetLength(data,1);
816 data[0]:=value_int;
817 END;
818 10000..65535: BEGIN
819 SetLength(data,datatype-10000);
820 FOR i:=1 TO datatype-10000 DO BEGIN
821 IF i<=Length(value) THEN
822 data[i-1]:=Ord(value[i])
823 ELSE
824 data[i-1]:=0;
825 END;
826 END;
827 END;
828 FOR i:=0 TO High(data) DO BEGIN
829 IF hex.Data[offset+i]<>data[i] THEN hex.ByteChanged[offset+i]:=True;
830 hex.Data[offset+i]:=data[i];
831 END;
832 hex.Modified:=True;
833 hexChange(Self);
834 hex.Repaint;
835 END;
836
837PROCEDURE TForm8.value_viewerDblClick(Sender: TObject);
838 VAR
839 offset:LongWord;
840 datatype:Word;
841 objectname:String;
842 value:String;
843 BEGIN
844 IF (value_viewer.Col=1) AND (Length(value_viewer.Cells[1,value_viewer.Row])>0) THEN BEGIN
845 offset:=hex.SelStart;
846 IF value_viewer.Cells[0,value_viewer.Row]='1 byte, unsigned' THEN
847 datatype:=1;
848 IF value_viewer.Cells[0,value_viewer.Row]='2 bytes, unsigned' THEN
849 datatype:=2;
850 IF value_viewer.Cells[0,value_viewer.Row]='4 bytes, unsigned' THEN
851 datatype:=4;
852 IF value_viewer.Cells[0,value_viewer.Row]='Bitset' THEN
853 datatype:=10;
854 IF value_viewer.Cells[0,value_viewer.Row]='Float' THEN
855 datatype:=9;
856 IF value_viewer.Cells[0,value_viewer.Row]='Selected length' THEN
857 Exit;
858 IF value_viewer.Cells[0,value_viewer.Row]='String' THEN BEGIN
859 IF hex.SelCount>0 THEN
860 datatype:=10000+hex.SelCount
861 ELSE
862 datatype:=10000+Length(value_viewer.Cells[1,value_viewer.Row]);
863 END;
864 objectname:='';
865 value:=GetValue(datatype,offset);
866 Form12.MakeVarInput(objectname,offset,datatype,value,Self);
867 END;
868 END;
869
870PROCEDURE TForm8.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
871 BEGIN
872 IF (Shift=[ssCtrl]) AND (Key=83) THEN
873 IF hex.Modified THEN
874 IF NOT Save THEN
875 Exit;
876 END;
877
878
879END.
Note: See TracBrowser for help on using the repository browser.