source: oup/releases/0.32a/Unit8_binedit.pas@ 754

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