-
Notifications
You must be signed in to change notification settings - Fork 20
/
UfrmFormTree.pas
360 lines (333 loc) · 10.5 KB
/
UfrmFormTree.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
unit UfrmFormTree;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics,
Controls, Forms, Dialogs, StdCtrls, Menus, ComCtrls;
type
TIdrDfmFormTree=class(TForm)
tvForm: TTreeView;
dlgFind: TFindDialog;
mnuTree: TPopupMenu;
Expand1: TMenuItem;
Collapse1: TMenuItem;
Find1: TMenuItem;
procedure FormDestroy(Sender: TObject);
procedure tvFormKeyPress(Sender : TObject; Var Key:Char);
procedure tvFormDblClick(Sender : TObject);
procedure dlgFindFind(Sender : TObject);
procedure Expand1Click(Sender : TObject);
procedure Collapse1Click(Sender : TObject);
procedure Find1Click(Sender : TObject);
procedure FormClose(Sender : TObject; var Action:TCloseAction);
procedure FormKeyDown(Sender : TObject; var Key:Word; Shift:TShiftState);
procedure FormCreate(Sender : TObject);
private
{ Private declarations }
prevCursor:TCursor;
//TTreeNodeMap NodesMap;
Function MakeNodeCaption(curComp:TComponent):AnsiString;
Function MakeNodeEventCaption(item:Pointer):AnsiString;
Procedure BorderTheControl(aControl:TControl);
Procedure AddEventsToNode(compName:AnsiString; dstNode:TTreeNode; evList:TList);
Function FindTreeNodeByText(nodeFrom:TTreeNode; const txt:AnsiString; caseSensitive:Boolean):TTreeNode;
Function FindTreeNodeByTag(tag:Pointer):TTreeNode;
Procedure AddTreeNodeWithTag(node:TTreeNode; tag:Pointer);
Function IsEventNode(selNode:TTreeNode):Boolean;
public
{ Public declarations }
end;
implementation
{$R *.DFM}
Uses Def_res,Def_main,Main,Resources,Infos,Misc,StrUtils;
procedure TIdrDfmFormTree.AddEventsToNode(compName:AnsiString; dstNode:TTreeNode; evList:TList);
Var
m:Integer;
item:PEventListItem;
Begin
for m := 0 To evList.Count-1 do
begin
item := evList.Items[m];
if SameText(item.CompName, compName) then
tvForm.Items.AddChildObject(dstNode, MakeNodeEventCaption(item), Pointer(item.Adr));
End;
end;
procedure TIdrDfmFormTree.FormCreate(Sender : TObject);
var
compCnt,i:Integer;
frmOwner:TForm;
evList:TList;
rootNode,parentNode,parentNode2,childNode:TTreeNode;
curComp, parComp, parComp2:TComponent;
externalNames, tnCaption:AnsiString;
parName, parName2, ownName:AnsiString;
externalParent:Boolean;
begin
prevCursor:=Screen.Cursor;
Screen.Cursor:=crHourGlass;
Application.ProcessMessages;
frmOwner:=TForm(Owner);
compCnt := frmOwner.ComponentCount;
if compCnt > 1 then Caption := frmOwner.Name + ' (' + IntToStr(compCnt) + ' components)'
else Caption := frmOwner.Name + ' (1 component)';
evList:=TList.Create;
ResInfo.GetEventsList(frmOwner.Name, evList);
tvForm.Items.BeginUpdate;
try
rootNode := tvForm.Items.AddObject(Nil, frmOwner.Name, frmOwner);
AddTreeNodeWithTag(rootNode, frmOwner);
AddEventsToNode(frmOwner.Name, rootNode, evList);
externalParent := false;
for i := 0 To compCnt-1 do
begin
curComp := frmOwner.Components[i];
if curComp = Self Then continue; //Don't display Form TreeView
tnCaption := MakeNodeCaption(curComp);
parComp := curComp.GetParentComponent;
if curComp.HasParent then
begin
//in some very rare cases parent could be located in another module!
//so in this case we have true for currComp->HasParent() but NULL for parCompo
if not Assigned(parComp) then
begin
externalParent := true;
if externalNames<>'' then externalNames:= externalNames + ', ';
externalNames := externalNames + tnCaption;
End;
if Assigned(parComp) Then parName:=parComp.Name
Else parName:='N/A';
ownName := curComp.Owner.Name;
parentNode := FindTreeNodeByTag(parComp);
//tricky case for special components (eg: TPage, TTabPage)
//they are a) noname (but renamed in IDR)
// b) not present in Components[] array
if Not Assigned(parentNode) and Assigned(parComp) then
begin
parComp2 := parComp.GetParentComponent;
parName2 := parComp2.Name;
parentNode2 := FindTreeNodeByTag(parComp2);
// add noname parent
parentNode := tvForm.Items.AddChildObject(parentNode2, MakeNodeCaption(parComp), parComp);
AddTreeNodeWithTag(parentNode, parComp);
End;
end
else parentNode := FindTreeNodeByTag(curComp.Owner);
childNode := tvForm.Items.AddChildObject(parentNode, tnCaption, curComp);
AddTreeNodeWithTag(childNode, curComp);
AddEventsToNode(curComp.Name, childNode, evList);
End;
rootNode.Expand(false);
Finally
tvForm.Items.EndUpdate;
End;
if externalParent then
Application.MessageBox(PAnsiChar('Form has some component classes defined in external modules:'+#13+#10
+ externalNames + #13+#10+#13+#10+'Visualization of these components is not yet implemented'),
'Warning', MB_OK or MB_ICONWARNING);
end;
procedure TIdrDfmFormTree.FormDestroy(Sender: TObject);
begin
Screen.Cursor:=prevCursor;
end;
Function TIdrDfmFormTree.MakeNodeCaption (curComp:TComponent):AnsiString;
Var
clasName:AnsiString;
Begin
clasName := curComp.ClassName;
if curComp Is IdrDfmDefaultControl then
clasName := '!' + IdrDfmDefaultControl(curComp).OrigClassName;
Result:=curComp.Name + ':' + clasName;
end;
Function TIdrDfmFormTree.MakeNodeEventCaption (item:Pointer):AnsiString;
Var
recN:InfoRec;
Begin
if PEventListItem(item).Adr<>0 then
begin
recN := GetInfoRec(PEventListItem(item).Adr);
if Assigned(recN) and recN.HasName then
begin
Result:=PEventListItem(item).EventName + '=' + recN.Name;
Exit;
End;
End;
Result:=PEventListItem(item).EventName;
end;
Function TIdrDfmFormTree.FindTreeNodeByTag (tag:Pointer):TTreeNode;
Begin
// TTreeNodeMap::const_iterator it = NodesMap.find(tag);
// if (it != NodesMap.end()) return it->second;
Result:=Nil;
end;
Function TIdrDfmFormTree.FindTreeNodeByText (nodeFrom:TTreeNode; Const txt:AnsiString; caseSensitive:Boolean):TTreeNode;
Var
nodes:TTreeNodes;
i:Integer;
ttt:AnsiString;
startScan:Boolean;
Begin
Result := Nil;
If Assigned(nodeFrom) Then startScan:=False
Else startScan:=True;
nodes := tvForm.Items;
for i:=0 to nodes.Count-1 do
begin
if not startScan then
begin
if nodeFrom = nodes.Item[i] then startScan := true;
continue;
End;
ttt := nodes.Item[i].Text;
if ((caseSensitive and AnsiContainsStr(nodes.Item[i].Text, txt))
or (not caseSensitive and AnsiContainsText(nodes.Item[i].Text, txt))
) then
begin
Result := nodes.Item[i];
break;
end;
End;
end;
Procedure TIdrDfmFormTree.AddTreeNodeWithTag (node:TTreeNode; tag:Pointer);
Begin
//NodesMap[tag] = node;
end;
Function TIdrDfmFormTree.IsEventNode (selNode:TTreeNode):Boolean;
Begin
Result:= Assigned(selNode) and (selNode.Text<>'') and (Pos('=',selNode.Text)<>0);
end;
procedure TIdrDfmFormTree.FormKeyDown(Sender : TObject; var Key:Word; Shift:TShiftState);
begin
if Key=VK_ESCAPE then
begin
Key := 0;
Close;
TForm(Owner).SetFocus;
end
else if Key=VK_F3 then dlgFindFind(Sender)
else if (ssCtrl in Shift) and (Key=Ord('F')) then dlgFind.Execute;
end;
procedure TIdrDfmFormTree.tvFormKeyPress(Sender : TObject; Var Key:Char);
begin
if Key=#13 Then
Begin
Key:=#0;
tvFormDblClick(Sender);
end;
end;
Procedure TIdrDfmFormTree.BorderTheControl (aControl:TControl);
Var
can:TControlCanvas;
parCtrl:TWinControl;
aDC:HDC;
aRect:TRect;
aHandle:HWND;
frm:TForm;
bMenu:Boolean;
Begin
can:=TControlCanvas.create;
parCtrl := aControl.Parent;
if Not Assigned(parCtrl) Then parCtrl := TWinControl(aControl);
can.Control := parCtrl;
aHandle := parCtrl.Handle;
aDC := GetWindowDC(aHandle);
with Can do
begin
Handle := aDC;
Brush.Style := bsSolid;
Pen.Width := 1;
Pen.Color := clRed;
end;
aRect := aControl.ClientRect;
frm := parCtrl as TForm;
bMenu := Assigned(frm) and Assigned(frm.Menu);
AdjustWindowRectEx(aRect,
GetWindowLong(aHandle, GWL_STYLE),
bMenu,
GetWindowLong(aHandle, GWL_EXSTYLE));
MoveWindowOrg(aDC, -aRect.Left, -aRect.Top);
if parCtrl = aControl then aRect := aControl.ClientRect
else
begin
aRect := aControl.BoundsRect;
InflateRect(aRect, 2, 2);
End;
//C->Rectangle(aRect);
can.DrawFocusRect(aRect);
ReleaseDC(aHandle, aDC);
end;
procedure TIdrDfmFormTree.tvFormDblClick(Sender : TObject);
var
selNode:TTreeNode;
Adr,i:Integer;
recN:InfoRec;
selControl:TControl;
parControl:TWinControl;
ownerForm:TForm;
begin
selNode := tvForm.Selected;
if not Assigned(selNode) Then Exit;
if IsEventNode(selNode) then
begin
Adr := Integer(selNode.Data);
if (Adr<>0) and IsValidCodeAdr(Adr) then
begin
recN := GetInfoRec(Adr);
if Assigned(recN) then
begin
if recN.kind = ikVMT then FMain.ShowClassViewer(Adr)
else FMain.ShowCode(Adr, 0, -1, -1);
end;
End;
Exit;
End;
selControl := TControl(selNode.Data);
if Not Assigned(selControl) Then Exit;
parControl := selControl.Parent;
if Not Assigned(parControl) Then Exit;
ownerForm := TForm(Owner);
ownerForm.BringToFront;
selControl.BringToFront;
for i := 0 to 1 do
begin
BorderTheControl(selControl);
selControl.Hide;
selControl.Update;
Sleep(150);
BorderTheControl(selControl);
selControl.Show;
selControl.Update;
Sleep(150);
End;
BringToFront;
end;
procedure TIdrDfmFormTree.dlgFindFind(Sender : TObject);
var
caseSensitive:Boolean;
tn:TTreeNode;
begin
caseSensitive := frMatchCase in dlgFind.Options;
tn := FindTreeNodeByText(tvForm.Selected, dlgFind.FindText, caseSensitive);
if Not Assigned(tn) Then tn := FindTreeNodeByText(Nil, Trim(dlgFind.FindText), caseSensitive);
if Assigned(tn) Then
begin
tvForm.Selected := tn;
tn.Expand(false);
tvForm.SetFocus;
end;
end;
procedure TIdrDfmFormTree.Expand1Click(Sender : TObject);
begin
if Assigned(tvForm.Selected) Then tvForm.Selected.Expand(true);
end;
procedure TIdrDfmFormTree.Collapse1Click(Sender : TObject);
begin
if Assigned(tvForm.Selected) then tvForm.Selected.Collapse(false);
end;
procedure TIdrDfmFormTree.Find1Click(Sender : TObject);
begin
dlgFind.Execute;
end;
procedure TIdrDfmFormTree.FormClose(Sender : TObject; var Action:TCloseAction);
begin
dlgFind.CloseDialog;
end;
end.