forked from marcocantu/ObjectDebugger
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathObjectDebuggerForm.pas
1557 lines (1398 loc) · 42.2 KB
/
ObjectDebuggerForm.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit ObjectDebuggerForm;
{ ******************************* }
{ ORIGINAL AUTHOR }
{ ******************************* }
{ Delphi ObjectDebugger }
{ MPL 2.0 License }
{ Copyright 2016 Marco Cantu }
{ marco.cantu@gmail.com }
{ ******************************* }
{ ************************************************************************************************ }
{ CONTRIBUTIONS }
{ ************************************************************************************************ }
{ MPL 2.0 License }
{ Copyright 2016-2018 Alessandro Fragnani }
{ github.com/alefragnani }
{ * ShowOnStartup Property (https://github.com/marcocantu/ObjectDebugger/pull/1) }
{ * Berlin Support (https://github.com/marcocantu/ObjectDebugger/pull/3) }
{ * Filter Properties/Events/Data (https://github.com/marcocantu/ObjectDebugger/pull/4) }
{ * Tokyo Support (https://github.com/marcocantu/ObjectDebugger/pull/5) }
{ * Allow Form Close (https://github.com/marcocantu/ObjectDebugger/pull/6) }
{ * Hex Color (https://github.com/marcocantu/ObjectDebugger/pull/7) }
{ ************************************************************************************************ }
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls, TypInfo, ExtCtrls, Grids,
Buttons, Menus, ComCtrls, Vcl.WinXCtrls, System.ImageList, Vcl.ImgList,
ObjectDebugger.ClickDetector, ObjectDebugger.Inspector;
////// component //////
type
TCantObjectDebugger = class(TComponent)
private
fOnTop: Boolean;
fCopyright, fNull: string;
FActive: Boolean;
FShowOnStartup: Boolean;
FAllowFormClose: Boolean;
FOnClose: TNotifyEvent;
FClickDetector: TClickDetector;
procedure SetActive(const Value: Boolean);
public
constructor Create (AOwner: TComponent); override;
destructor Destroy; override;
procedure Show;
procedure CreateClickDetector;
published
property OnTop: Boolean
read fOnTop write fOnTop;
property Copyright: string
read fCopyright write fNull;
property Active: Boolean read FActive write SetActive default True;
property ShowOnStartup: Boolean
read FShowOnStartup write FShowOnStartup default True;
property AllowFormClose: Boolean
read FAllowFormClose write FAllowFormClose default False;
property OnClose: TNotifyEvent read FOnClose write FOnClose;
end;
procedure Register;
////// form //////
type
TCantObjDebForm = class(TForm, IObjectInspector)
ColorDialog1: TColorDialog;
FontDialog1: TFontDialog;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
sgProp: TStringGrid;
Panel1: TPanel;
cbComps: TComboBox;
MainMenu1: TMainMenu;
cbForms: TComboBox;
sgEvt: TStringGrid;
Options1: TMenuItem;
RefreshForms1: TMenuItem;
RefreshComponents1: TMenuItem;
Help1: TMenuItem;
About1: TMenuItem;
RefreshValues1: TMenuItem;
ComboColor: TComboBox;
ComboCursor: TComboBox;
ComboEnum: TComboBox;
EditNum: TEdit;
EditStr: TEdit;
N1: TMenuItem;
TopMost1: TMenuItem;
EditCh: TEdit;
ListSet: TListBox;
Info1: TMenuItem;
Timer1: TTimer;
TabSheet3: TTabSheet;
sgData: TStringGrid;
edFilter: TButtonedEdit;
ImageList1: TImageList;
procedure cbFormsChange(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure cbCompsChange(Sender: TObject);
procedure RefreshForms1Click(Sender: TObject);
procedure RefreshComponents1Click(Sender: TObject);
procedure About1Click(Sender: TObject);
procedure RefreshValues1Click(Sender: TObject);
procedure sgMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure EditStrExit(Sender: TObject);
procedure EditNumExit(Sender: TObject);
procedure ComboColorDblClick(Sender: TObject);
procedure ComboColorChange(Sender: TObject);
procedure ComboCursorChange(Sender: TObject);
procedure ComboEnumChange(Sender: TObject);
procedure EditNumKeyPress(Sender: TObject; var Key: Char);
procedure ComboEnumDblClick(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure TopMost1Click(Sender: TObject);
procedure EditChExit(Sender: TObject);
procedure ListSetClick(Sender: TObject);
procedure RefreshOnExit(Sender: TObject);
procedure sgPropDblClick(Sender: TObject);
procedure Info1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Timer1Timer(Sender: TObject);
procedure EditChange(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure SearchBox1Change(Sender: TObject);
procedure edFilterRightButtonClick(Sender: TObject);
procedure PageControl1Change(Sender: TObject);
procedure edFilterKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure sgPropSelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
procedure sgDataSelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
procedure FormDestroy(Sender: TObject);
private
// the current component
CurrComp: TComponent;
// the real component (if a subproperty is active)
RealComp: TComponent;
// are we editing a subproperty?
EditingSub: Boolean;
// current form: TForm or TDataModule
CurrForm: TComponent;
// current property
CurrProp: PPropInfo;
// current row in grid
CurrRow: Integer;
// combo box used by AddToCombo method
Combo: TComboBox;
// edit box has been modified?
EditModified: Boolean;
// the debugger component
ODebugger: TCantObjectDebugger;
// filter props/events/data
FFilterCriteria: string;
function MatchFilter(const value: string): boolean;
procedure ClearFilterEdit;
public
procedure UpdateFormsCombo;
procedure UpdateCompsCombo;
procedure UpdateProps;
procedure UpdateData;
procedure EditStringList (Str: TStrings);
procedure AddToCombo (const S: String);
procedure Inspect(control: TControl);
end;
var
CantObjDebForm: TCantObjDebForm;
implementation
{$R *.DFM}
uses
Math, System.UITypes, System.RegularExpressions;
const
VersionDescription = 'Object Debugger for Delphi';
VersionRelease = 'Release 6.00';
CopyrightString = 'Marco Cant� 1996-2016 / Alessandro Fragnani 2016-2018';
/////////// support code //////////////
/////////// code from DdhRttiH, previoulsy RttiHelp,
/////////// the RTTI helper functions
// redeclaration of RTTI type
type
TParamData = record
Flags: TParamFlags;
ParamName: ShortString;
TypeName: ShortString;
// beware: string length varies!!!
end;
PParamData = ^TParamData;
// show RTTI information for method pointers
procedure ShowMethod (pti: PTypeInfo; sList: TStrings);
var
ptd: PTypeData;
pParam: PParamData;
nParam: Integer;
Line: string;
pTypeString, pReturnString: ^ShortString;
begin
// protect against misuse
if pti^.Kind <> tkMethod then
raise Exception.Create ('Invalid type information');
// get a pointer to the TTypeData structure
ptd := GetTypeData (pti);
// 1: access the TTypeInfo structure
sList.Add ('Type Name: ' + string(pti^.Name));
sList.Add ('Type Kind: ' + GetEnumName (
TypeInfo (TTypeKind),
Integer (pti^.Kind)));
// 2: access the TTypeData structure
sList.Add ('Method Kind: ' + GetEnumName (
TypeInfo (TMethodKind),
Integer (ptd^.MethodKind)));
sList.Add ('Number of parameter: ' +
IntToStr (ptd^.ParamCount));
// 3: access to the ParamList
// get the initial pointer and
// reset the parameters counter
pParam := PParamData (@(ptd^.ParamList));
nParam := 1;
// loop until all parameters are done
while nParam <= ptd^.ParamCount do
begin
// read the information
Line := 'Param ' + IntToStr (nParam) + ' > ';
// add type of parameter
if pfVar in pParam^.Flags then
Line := Line + 'var ';
if pfConst in pParam^.Flags then
Line := Line + 'const ';
if pfOut in pParam^.Flags then
Line := Line + 'out ';
// get the parameter name
Line := Line + string(pParam^.ParamName) + ': ';
// one more type of parameter
if pfArray in pParam^.Flags then
Line := Line + ' array of ';
// the type name string must be located...
// moving a pointer past the params and
// the string (including its size byte)
pTypeString := Pointer (Integer (pParam) +
sizeof (TParamFlags) +
Length (pParam^.ParamName) + 1);
// add the type name
Line := Line + string(pTypeString^);
// finally, output the string
sList.Add (Line);
// move the pointer to the next structure,
// past the two strings (including size byte)
pParam := PParamData (Integer (pParam) +
sizeof (TParamFlags) +
Length (pParam^.ParamName) + 1 +
Length (pTypeString^) + 1);
// increase the parameters counter
Inc (nParam);
end;
// show the return type if a function
if ptd^.MethodKind = mkFunction then
begin
// at the end, instead of a param data,
// there is the return string
pReturnString := Pointer (pParam);
sList.Add ('Returns > ' + string(pReturnString^));
end;
end;
// show RTTI information for class type
procedure ShowClass (pti: PTypeInfo; sList: TStrings);
var
ptd: PTypeData;
ppi: PPropInfo;
pProps: PPropList;
nProps, I: Integer;
ParentClass: TClass;
begin
// protect against misuse
if pti.Kind <> tkClass then
raise Exception.Create ('Invalid type information');
// get a pointer to the TTypeData structure
ptd := GetTypeData (pti);
// access the TTypeInfo structure
sList.Add ('Type Name: ' + string(pti.Name));
sList.Add ('Type Kind: ' + GetEnumName (
TypeInfo (TTypeKind),
Integer (pti.Kind)));
// access the TTypeData structure
{omitted: the same information of pti^.Name...
sList.Add ('ClassType: ' + ptd^.ClassType.ClassName);}
sList.Add ('Size: ' + IntToStr (
ptd.ClassType.InstanceSize) + ' bytes');
sList.Add ('Defined in: ' + string(ptd.UnitName) + '.pas');
// add the list of parent classes (if any)
ParentClass := ptd.ClassType.ClassParent;
if ParentClass <> nil then
begin
sList.Add ('');
sList.Add ('=== Parent classes ===');
while ParentClass <> nil do
begin
sList.Add (ParentClass.ClassName);
ParentClass := ParentClass.ClassParent;
end;
end;
// add the list of properties (if any)
nProps := ptd.PropCount;
if nProps > 0 then
begin
// format the initial output
sList.Add ('');
sList.Add ('=== Properties (' +
IntToStr (nProps) + ') ===');
// allocate the required memory
GetMem (pProps, sizeof (PPropInfo) * nProps);
// protect the memory allocation
try
// fill the TPropList structure
// pointed to by pProps
GetPropInfos(pti, pProps);
// sort the properties
SortPropList(pProps, nProps);
// show name and data type of each property
for I := 0 to nProps - 1 do
begin
ppi := pProps [I];
sList.Add (string(ppi.Name) + ': ' +
string(ppi.PropType^.Name));
end;
finally
// free the allocated memmory
FreeMem (pProps, sizeof (PPropInfo) * nProps);
end;
end;
end;
// list enumerated values (used by next routine)
procedure ListEnum (pti: PTypeInfo;
sList: TStrings; ShowIndex: Boolean);
var
I: Integer;
begin
with GetTypeData(pti)^ do
for I := MinValue to MaxValue do
if ShowIndex then
sList.Add (' ' + IntToStr (I) + '. ' +
GetEnumName (pti, I))
else
sList.Add (GetEnumName (pti, I));
end;
// show RTTI information for ordinal types
procedure ShowOrdinal (pti: PTypeInfo; sList: TStrings);
var
ptd: PTypeData;
begin
// protect against misuse
if not (pti^.Kind in [tkInteger, tkChar,
tkEnumeration, tkSet, tkWChar]) then
raise Exception.Create ('Invalid type information');
// get a pointer to the TTypeData structure
ptd := GetTypeData (pti);
// access the TTypeInfo structure
sList.Add ('Type Name: ' + string(pti^.Name));
sList.Add ('Type Kind: ' + GetEnumName (
TypeInfo (TTypeKind),
Integer (pti^.Kind)));
// access the TTypeData structure
sList.Add ('Implement: ' + GetEnumName (
TypeInfo (TOrdType),
Integer (ptd^.OrdType)));
// a set has no min and max
if pti^.Kind <> tkSet then
begin
sList.Add ('Min Value: ' + IntToStr (ptd^.MinValue));
sList.Add ('Max Value: ' + IntToStr (ptd^.MaxValue));
end;
// add the enumeration base type
// and the list of the values
if pti^.Kind = tkEnumeration then
begin
sList.Add ('Base Type: ' + string((ptd^.BaseType)^.Name));
sList.Add ('');
sList.Add ('Values...');
ListEnum (pti, sList, True);
end;
// show RRTI info about set base type
if pti^.Kind = tkSet then
begin
sList.Add ('');
sList.Add ('Set base type information...');
ShowOrdinal (ptd^.CompType^, sList);
end;
end;
// generic procedure, calling the other ones
procedure ShowRTTI (pti: PTypeInfo; sList: TStrings);
begin
case pti^.Kind of
tkInteger, tkChar, tkEnumeration, tkSet, tkWChar:
ShowOrdinal (pti, sList);
tkMethod:
ShowMethod (pti, sList);
tkClass:
Showclass (pti, sList);
tkString, tkLString:
begin
sList.Add ('Type Name: ' + string(pti^.Name));
sList.Add ('Type Kind: ' + GetEnumName (
TypeInfo (TTypeKind), Integer (pti^.Kind)));
end
else
sList.Add ('Undefined type information');
end;
end;
// show the RTTI information inside a modal dialog box
procedure ShowRttiDetail (pti: PTypeInfo);
var
Form: TForm;
begin
Form := TForm.Create (Application);
try
Form.Width := 250;
Form.Height := 300;
// middle of the screen
Form.Left := Screen.Width div 2 - 125;
Form.Top := Screen.Height div 2 - 150;
Form.Caption := 'RTTI Details for ' + string(pti.Name);
Form.BorderStyle := bsDialog;
with TMemo.Create (Form) do
begin
Parent := Form;
Width := Form.ClientWidth;
Height := Form.ClientHeight - 35;
ReadOnly := True;
Color := clBtnFace;
ShowRTTI (pti, Lines);
end;
with TBitBtn.Create (Form) do
begin
Parent := Form;
Left := Form.ClientWidth div 3;
Width := Form.ClientWidth div 3;
Top := Form.ClientHeight - 32;
Height := 30;
Kind := bkOK;
end;
Form.ShowModal;
finally
Form.Free;
end;
end;
// support function: get the form (or data module)
// owning the component
function GetOwnerForm (Comp: TComponent): TComponent;
begin
while not (Comp is TForm) and
not (Comp is TDataModule) do
Comp := Comp.Owner;
Result := Comp;
end;
// from the Bits1 example (Chapter 1)
function IsBitOn (Value: Integer; Bit: Byte): Boolean;
begin
Result := (Value and (1 shl Bit)) <> 0;
end;
// support function: convert set value
// into a string as in the Object Inspector
function SetToString (Value: Cardinal;
pti: PTypeInfo): string;
var
Res: String; // result
BaseType: PTypeInfo;
I: Integer;
Found: Boolean;
begin
Found := False;
// open the expression
Res := '[';
// get the type of the enumeration
// the set is based onto
BaseType := GetTypeData(pti).CompType^;
// for each possible value
for I := GetTypeData (BaseType).MinValue
to GetTypeData (BaseType).MaxValue do
// if the bit I (computed as 1 shl I) is set,
// then the corresponding element is in the set
// (the and is a bitwise and, not a boolean operation)
if IsBitOn (Value, I) then
begin
// add the name of the element
Res := Res + GetEnumName (BaseType, I) + ', ';
Found := True;
end;
if Found then
// remove the final comma and space (2 chars)
Res := Copy (Res, 1, Length (Res) - 2);
// close the expression
Result := Res + ']';
end;
// return the property value as a string
function GetPropValAsString (Obj: TObject;
PropInfo: PPropInfo): string;
var
Pt: Pointer;
Word: Cardinal;
begin
case PropInfo.PropType^.Kind of
tkUnknown:
Result := 'Unknown type';
tkChar:
begin
Word := GetOrdProp (Obj, PropInfo);
if Word > 32 then
Result := Char (Word)
else
Result := '#' + IntToStr (Word);
end;
tkWChar:
begin
Word := GetOrdProp (Obj, PropInfo);
if Word > 32 then
Result := WideChar (Word)
else
Result := '#' + IntToStr (Word);
end;
tkInteger:
if PropInfo.PropType^.Name = 'TColor' then
Result := ColorToString (GetOrdProp (Obj, PropInfo))
else if PropInfo.PropType^.Name = 'TCursor' then
Result := CursorToString (GetOrdProp (Obj, PropInfo))
else
Result := Format ('%d', [GetOrdProp (Obj, PropInfo)]);
tkEnumeration:
Result := GetEnumName (PropInfo.PropType^,
GetOrdProp (Obj, PropInfo));
tkFloat:
Result := FloatToStr (GetFloatProp (Obj, PropInfo));
tkString, tkLString, tkUString, tkWString:
Result := GetStrProp (Obj, PropInfo);
tkSet:
Result := SetToString (GetOrdProp (Obj, PropInfo),
PropInfo.PropType^);
tkClass:
begin
Pt := Pointer (GetOrdProp (Obj, PropInfo));
if Pt = nil then
Result := '(None)'
else
Result := Format ('(Object %p)', [Pt]);
end;
tkMethod:
begin
Pt := GetMethodProp (Obj, PropInfo).Code;
if Pt <> nil then
Result := GetOwnerForm (Obj as TComponent).
MethodName (Pt)
else
Result := '';
end;
tkVariant:
Result := GetVariantProp (Obj, PropInfo);
tkArray, tkRecord, tkInterface:
Result := 'Unsupported type';
else
Result := 'Undefined type';
end;
end;
////// component //////
var
Created: Boolean = False;
constructor TCantObjectDebugger.Create (AOwner: TComponent);
begin
if Created then
raise Exception.Create ('Only one debugger, please!')
else
Created := True;
inherited Create (AOwner);
fActive := True;
fCopyright := CopyrightString;
FShowOnStartup := True;
FAllowFormClose := False;
FOnClose := nil;
if not (csDesigning in ComponentState) then
begin
CantObjDebForm := TCantObjDebForm.Create (Application);
CantObjDebForm.ODebugger := self;
if fOnTop then
begin
// set topmost style
CantObjDebForm.FormStyle := fsStayOnTop;
CantObjDebForm.TopMost1.Checked := True
end;
CantObjDebForm.Timer1.Enabled := True;
end;
end;
destructor TCantObjectDebugger.Destroy;
begin
if Assigned(FOnClose) then
FOnClose(Self);
FClickDetector := nil;
Created := False;
inherited;
end;
procedure TCantObjectDebugger.SetActive(const Value: Boolean);
begin
FActive := Value;
end;
procedure TCantObjectDebugger.CreateClickDetector;
begin
FClickDetector := TClickDetector.Create(CantObjDebForm, CantobjDebForm);
end;
procedure TCantObjectDebugger.Show;
begin
CantObjDebForm.Show;
CantObjDebForm.UpdateFormsCombo;
FActive := True;
end;
procedure Register;
begin
RegisterComponents('Cantools', [TCantObjectDebugger]);
end;
////// form //////
{initialize the local data to nil and so on...}
procedure TCantObjDebForm.FormCreate(Sender: TObject);
begin
CurrForm := nil;
CurrComp := nil;
RealComp := nil;
EditingSub := False;
// show the first page
PageControl1.ActivePage := TabSheet1;
// set first line
sgProp.Cells [0, 0] := 'Type: (click for detail)';
sgEvt.Cells [0, 0] := 'Type: (click for detail)';
sgData.Cells [0, 0] := 'Type:';
// fill input combos
Combo := ComboCursor;
GetCursorValues (AddToCombo);
Combo := ComboColor;
GetColorValues (AddToCombo);
// initialize filter
FFilterCriteria := '';
edFilter.TextHint := 'Search ' + PageControl1.ActivePage.Caption;
end;
procedure TCantObjDebForm.FormDestroy(Sender: TObject);
begin
ODebugger.Free;
end;
{call-back used in the code above...}
procedure TCantObjDebForm.AddToCombo (const S: String);
begin
Combo.Items.Add (S);
end;
procedure TCantObjDebForm.Inspect(control: TControl);
var
nIdx: integer;
begin
if control = nil then
exit;
nIdx := cbComps.Items.IndexOf(control.Name + ': ' + control.ClassName);
if nIdx > -1 then
begin
cbComps.ItemIndex := nIdx;
cbCompsChange(cbComps);
end;
end;
{fill the FormsCombo with the names of the forms of the
current project keep the curent element selected, unless it
has been destroyed. In this last case use the MainForm
as selected form.}
procedure TCantObjDebForm.UpdateFormsCombo;
var
I, nForm, Pos: Integer;
Form: TForm;
begin
Screen.Cursor := crHourglass;
cbForms.Items.BeginUpdate;
try
cbForms.Items.Clear;
// for each form of the program
for nForm := 0 to Screen.FormCount - 1 do
begin
Form := Screen.Forms [nForm];
// if the form is not the one of the ObjectDebugger, add it
if Form <> self then
cbForms.Items.AddObject (
Format ('%s (%s)', [Form.Caption, Form.ClassName]),
Form);
end;
// for each data module
for I := 0 to Screen.DataModuleCount - 1 do
cbForms.Items.AddObject (
Format ('%s (%s)',
[Screen.DataModules [I].Name,
Screen.DataModules [I].ClassName]),
Screen.DataModules [I]);
// re-select the current form, if exists
if not Assigned (CurrForm) then
CurrForm := Application.MainForm;
Pos := cbForms.Items.IndexOfObject (CurrForm);
if Pos < 0 then
begin
// was a destroyed form, retry...
CurrForm := Application.MainForm;
Pos := cbForms.Items.IndexOfObject (CurrForm);
end;
cbForms.ItemIndex := Pos;
finally
cbForms.Items.EndUpdate;
Screen.Cursor := crDefault;
end;
UpdateCompsCombo;
end;
procedure TCantObjDebForm.cbFormsChange(Sender: TObject);
begin
// save the current form or data module
CurrForm := cbForms.Items.Objects [
cbForms.ItemIndex] as TComponent;
// clear the filter
ClearFilterEdit;
// update the list of components
UpdateCompsCombo;
end;
procedure TCantObjDebForm.UpdateCompsCombo;
var
nComp, Pos: Integer;
Comp: TComponent;
begin
cbComps.Items.Clear;
cbComps.Items.AddObject (Format ('%s: %s',
[CurrForm.Name, CurrForm.ClassName]), CurrForm);
for nComp := 0 to CurrForm.ComponentCount - 1 do
begin
Comp := CurrForm.Components [nComp];
cbComps.Items.AddObject (Format ('%s: %s',
[Comp.Name, Comp.ClassName]), Comp);
end;
// reselect the current component, if any
if not Assigned (CurrComp) then
CurrComp := CurrForm;
Pos := cbComps.Items.IndexOfObject (CurrComp);
if Pos < 0 then
Pos := cbComps.Items.IndexOfObject (CurrForm);
cbComps.ItemIndex := Pos;
UpdateProps;
UpdateData;
end;
procedure TCantObjDebForm.cbCompsChange(Sender: TObject);
begin
// select the new component
CurrComp := cbComps.Items.Objects [
cbComps.ItemIndex] as TComponent;
// clear the filter
ClearFilterEdit;
// update the grids
UpdateProps;
UpdateData;
end;
function TCantObjDebForm.MatchFilter(const value: string): boolean;
begin
result := true;
if Trim(FFilterCriteria) = '' then
exit;
result := Pos(FFilterCriteria, AnsiLowerCase(value)) > 0;
end;
procedure TCantObjDebForm.PageControl1Change(Sender: TObject);
begin
ClearFilterEdit;
end;
procedure TCantObjDebForm.UpdateProps;
// update property and event pages
var
PropList, SubPropList: TPropList;
NumberOfProps, NumberOfSubProps, // total number of properties
nProp, nSubProp, // property loop counter
nRowProp, nRowEvt: Integer; // items actually added
SubObj: TPersistent;
begin
// reset the type
sgProp.Cells [1, 0] := '';
sgEvt.Cells [1, 0] := '';
// get the number of properties
NumberOfProps := GetTypeData(CurrComp.ClassInfo).PropCount;
// exaggerate in size...
sgProp.RowCount := NumberOfProps;
sgEvt.RowCount := NumberOfProps;
// get the list of properties and sort it
GetPropInfos (CurrComp.ClassInfo, @PropList);
SortPropList(@PropList, NumberOfProps);
// show the name of each property
// adding it to the proper page
nRowProp := 1;
nRowEvt := 1;
for nProp := 0 to NumberOfProps - 1 do
begin
// if it is a real property
if PropList[nProp].PropType^.Kind <> tkMethod then
begin
// filtering
if MatchFilter(string(PropList[nProp].Name)) then
begin
// name
sgProp.Cells [0, nRowProp] := string(PropList[nProp].Name);
// value
sgProp.Cells [1, nRowProp] := GetPropValAsString (
CurrComp, PropList [nProp]);
// data
sgProp.Objects [0, nRowProp] := TObject (PropList[nProp]);
sgProp.Objects [1, nRowProp] := nil;
// move to the next line
Inc (nRowProp);
// if the property is a class
if (PropList[nProp].PropType^.Kind = tkClass) then
begin
SubObj := TPersistent (GetOrdProp (
CurrComp, PropList[nProp]));
if (SubObj <> nil) and not (SubObj is TComponent) then
begin
NumberOfSubProps := GetTypeData(SubObj.ClassInfo).PropCount;
if NumberOfSubProps > 0 then
begin
// add plus sign
sgProp.Cells [0, nRowProp - 1] := '+' +
sgProp.Cells [0, nRowProp - 1];
// add space for subproperties...
sgProp.RowCount := sgProp.RowCount + NumberOfSubProps;
// get the list of subproperties and sort it
GetPropInfos (subObj.ClassInfo, @SubPropList);
SortPropList(@SubPropList, NumberOfSubProps);
// show the name of each subproperty
for nSubProp := 0 to NumberOfSubProps - 1 do
begin
// if it is a real property
if SubPropList[nSubProp].PropType^.Kind <> tkMethod then
begin
// name (indented)
sgProp.Cells [0, nRowProp] :=
' ' + string(SubPropList[nSubProp].Name);
// value
sgProp.Cells [1, nRowProp] := GetPropValAsString (
SubObj, SubPropList [nSubProp]);
// data
sgProp.Objects [0, nRowProp] :=
TObject (SubPropList[nSubProp]);
sgProp.Objects [1, nRowProp] := SubObj;
Inc (nRowProp);
end; // if
end; // for
end;
end;
end; // adding subproperties
end;
end
else // it is an event
begin
// filtering
if MatchFilter(string(PropList[nProp].Name)) then
begin
// name
sgEvt.Cells [0, nRowEvt] := string(PropList[nProp].Name);
// value
sgEvt.Cells [1, nRowEvt] := GetPropValAsString (
CurrComp, PropList [nProp]);
// data
sgEvt.Objects [0, nRowEvt] := TObject (PropList[nProp]);
// next
Inc (nRowEvt);
end;
end;
end; // for
// set the actual rows
sgProp.RowCount := nRowProp;
sgEvt.RowCount := nRowEvt;
end;
procedure TCantObjDebForm.UpdateData;
var
nRow: Integer;
procedure AddLine(Name, Value: string; pti: PTypeInfo);
begin
// filtering
if MatchFilter(Name) then
begin
sgData.Cells [0, nRow] := Name;
sgData.Cells [1, nRow] := Value;
sgData.Objects [0, nRow] := Pointer (pti);
sgProp.Objects [1, nRow] := nil;
Inc (nRow);
end;
end;
begin
// reset type
sgEvt.Cells [1, 0] := '';
nRow := 1;
// exaggerate...
sgData.RowCount := 15;
// add component runtime properties
AddLine ('ComponentCount',
IntToStr (CurrComp.ComponentCount),
TypeInfo (Integer));
{useless... AddLine ('ComponentState', SetToString (
Byte (CurrComp.ComponentState), TypeInfo (TComponentState)));}
AddLine ('ComponentIndex',
IntToStr (CurrComp.ComponentIndex),
TypeInfo (Integer));
AddLine ('ComponentStyle',
SetToString (Byte (CurrComp.ComponentStyle),
TypeInfo (TComponentStyle)),
TypeInfo (TComponentStyle));
if CurrComp.Owner <> nil then
if CurrComp.Owner = Application then
AddLine ('Owner',