-
Notifications
You must be signed in to change notification settings - Fork 12
/
CtwOopf.pas
127 lines (109 loc) · 3.68 KB
/
CtwOopf.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
unit CtwOopf;
interface
uses
CtwManager, SysUtils, Classes, ToolsApi, TypInfo, Forms, Dialogs;
type
TCanWizOopForm = class (TCanWiz)
public
function MenuText: string; override;
procedure Execute (Sender: TObject); override;
end;
procedure Register;
implementation
procedure Register;
begin
if Assigned (CantoolsManager) then
CantoolsManager.Add(TCanWizOopForm.Create);
end;
procedure TCanWizOopForm.Execute (Sender: TObject);
var
iModule: IOTAModule;
iSourceEditor: IOTASourceEditor;
iIntFormEditor: INTAFormEditor;
iView: IOTAEditView;
aForm: TComponent;
iWriter: IOTAEditWriter;
I: Integer;
strCode: UTF8String; // changed for 2009
strName, strType: string;
ListTypes: TStringList;
StartPos: Integer;
CurPos: TOTAEditPos;
CurCharPos: TOTACharPos;
CreateMethod: TMethod;
begin
ListTypes := TStringList.Create;
try
iModule := (BorlandIDEServices as IOTAModuleServices).CurrentModule;
if iModule = nil then
begin
ShowMessage ('Cantools error: File is closed');
Exit;
end;
// get the interface to the form editor.
if not Supports (iModule.CurrentEditor, INTAFormEditor, iIntFormEditor) then
begin
ShowMessage ('Cantools error: You must select a form');
Exit;
end;
if iIntFormEditor.FormDesigner = nil then
begin
ShowMessage ('Cantools error: No designer for module');
Exit;
end;
// get the list of components, preparing the source code
strCode := UTF8String (' // FormCreate event'#10#13);
aForm := iIntFormEditor.FormDesigner.Root;
for I := 0 to aForm.ComponentCount - 1 do
begin
strName := aForm.Components[i].Name;
strType := aForm.Components[i].ClassName;
strCode := strCode + UTF8String(' ' + strName + ' := ' +
'FindComponent (''' + strName + ''') as ' + strType + ';'#10#13);
// add unique class names to list
if ListTypes.IndexOf (strType) < 0 then
ListTypes.Add (strType);
end;
// TODO: move!
strCode := strCode + UTF8String( '{'#10#13 + 'initialization'#10#13);
strCode := strCode + UTF8String(' RegisterClasses (['#10#13);
// add all but the last
for I := 0 to ListTypes.Count - 2 do
strCode := strCode + UTF8String(' ' + ListTypes [I] + ','#10#13);
// add the last and close
if ListTypes.Count > 0 then
strCode := strCode + UTF8String(' ' + ListTypes [ListTypes.Count - 1] +
']);'#10#13'}');
// select or add the FormCreate method
iIntFormEditor.FormDesigner.Activate;
if not iIntFormEditor.FormDesigner.MethodExists ('FormCreate') then
begin
CreateMethod := iIntFormEditor.FormDesigner.CreateMethod('FormCreate',
GetTypeData (TypeInfo(TNotifyEvent)));
if aForm is TForm then
TForm(aForm).OnCreate := TNotifyEvent(Createmethod);
end;
iIntFormEditor.FormDesigner.ShowMethod('FormCreate');
if not Supports (iModule.CurrentEditor, IOTASourceEditor, iSourceEditor) then
begin
ShowMessage ('Cantools error: No editor found');
Exit;
end;
// add the source ocde in the current position
// TODO: find a simplified way for positioning!
iView := iSourceEditor.GetEditView (0);
CurPos := iView.CursorPos;
iView.ConvertPos(True, CurPos, CurCharPos);
StartPos := iView.CharPosToPos(CurCharPos);
iWriter := iSourceEditor.CreateUndoableWriter;
iWriter.CopyTo (StartPos);
iWriter.Insert(PAnsiChar(strCode));
finally
ListTypes.Free;
end;
end;
function TCanWizOopForm.MenuText: string;
begin
Result := 'OOP Form Wizard'; // useless
end;
end.