-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathGDK.ToolsAPI.FormEditor.pas
More file actions
360 lines (305 loc) · 12 KB
/
Copy pathGDK.ToolsAPI.FormEditor.pas
File metadata and controls
360 lines (305 loc) · 12 KB
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 GDK.ToolsAPI.FormEditor;
interface
uses
System.Classes,
System.SysUtils,
System.TypInfo,
ToolsAPI,
GDK.ToolsAPI.Helper.Interfaces;
type
TToolsApiFormEditor = class(TInterfacedObject, IToolsApiFormEditor)
private
FEditor: IOTAFormEditor;
function NativeComponent(const Component: IOTAComponent): TComponent;
procedure SetPropertyValue(const Instance: TObject; const PropertyPath: string; const Value: string);
procedure SetEventHandler(const Instance: TObject; const Info: PPropInfo; const MethodName: string);
procedure SetComponentReference(const Instance: TObject; const Info: PPropInfo; const ComponentName: string);
public
constructor Create(const Editor: IOTAFormEditor);
function Get: IOTAFormEditor;
function Root: TComponent;
function Find(const ComponentName: string): TComponent;
function Components: TArray<TComponent>;
function AssignedEvents(const Component: TComponent): TArray<string>;
function AddComponent(const TypeName: string;
const ContainerName: string;
const Left: Integer;
const Top: Integer;
const Width: Integer;
const Height: Integer): TComponent;
procedure SetComponentProperty(const ComponentName: string;
const PropertyPath: string;
const Value: string);
function CaptureImage: TBytes;
procedure ShowDesigner;
procedure MarkModified;
end;
implementation
uses
Vcl.Controls,
Vcl.Forms,
Vcl.Graphics,
Vcl.Imaging.pngimage,
DesignIntf;
constructor TToolsApiFormEditor.Create(const Editor: IOTAFormEditor);
begin
inherited Create;
FEditor := Editor;
end;
function TToolsApiFormEditor.Get: IOTAFormEditor;
begin
Result := FEditor;
end;
function TToolsApiFormEditor.NativeComponent(const Component: IOTAComponent): TComponent;
var
Native: INTAComponent;
begin
Result := nil;
if Assigned(Component) and Supports(Component, INTAComponent, Native) then
Result := Native.GetComponent;
end;
function TToolsApiFormEditor.Root: TComponent;
begin
Result := NativeComponent(FEditor.GetRootComponent);
if not Assigned(Result) then
raise EToolsApiComponentNotFound.Create('Form has no root component');
end;
function TToolsApiFormEditor.Find(const ComponentName: string): TComponent;
begin
const RootComponent = Root;
if ComponentName.IsEmpty or SameText(ComponentName, RootComponent.Name) then
Exit(RootComponent);
Result := NativeComponent(FEditor.FindComponent(ComponentName));
if not Assigned(Result) then
raise EToolsApiComponentNotFound.CreateFmt('Component "%s" not found on %s', [ComponentName, RootComponent.Name]);
end;
function TToolsApiFormEditor.Components: TArray<TComponent>;
begin
const RootComponent = Root;
SetLength(Result, RootComponent.ComponentCount);
for var Index := 0 to RootComponent.ComponentCount - 1 do
Result[Index] := RootComponent.Components[Index];
end;
function TToolsApiFormEditor.AddComponent(const TypeName: string;
const ContainerName: string;
const Left: Integer;
const Top: Integer;
const Width: Integer;
const Height: Integer): TComponent;
begin
var Container := FEditor.GetRootComponent;
if not ContainerName.IsEmpty then
begin
Container := FEditor.FindComponent(ContainerName);
if not Assigned(Container) then
raise EToolsApiComponentNotFound.CreateFmt('Container "%s" not found', [ContainerName]);
end;
// Capture the native container BEFORE creating: the ToolsAPI warns that an
// IOTAComponent handle may become invalid after the form is mutated, so
// resolving it afterwards can yield a stale/wrong parent (RSP-quirk that
// lands controls on the active page instead of the requested container).
const NativeContainer = NativeComponent(Container);
const RequestedNamedContainer = (not ContainerName.IsEmpty);
if RequestedNamedContainer and (not (NativeContainer is TWinControl)) then
raise EToolsApiComponentNotFound.CreateFmt(
'Container "%s" (%s) cannot host controls; a parent must be a TWinControl (form, panel, tab sheet, group box)',
[ContainerName, NativeContainer.ClassName]);
const Created = FEditor.CreateComponent(Container, TypeName, Left, Top, Width, Height);
if not Assigned(Created) then
raise EToolsApiComponentNotCreated.CreateFmt(
'Component of type "%s" could not be created - is the component class installed and registered?', [TypeName]);
Result := NativeComponent(Created);
if Result is TControl then
begin
const Control = TControl(Result);
// Force the parent to the exact named container (using the reference we
// captured before creating), so the control lands on the right tab/page
// rather than the currently active one.
if (NativeContainer is TWinControl) and (Control.Parent <> NativeContainer) then
Control.Parent := TWinControl(NativeContainer);
// Setting Parent resets the position, so apply the bounds afterwards.
Control.Left := Left;
Control.Top := Top;
if Width > 0 then
Control.Width := Width;
if Height > 0 then
Control.Height := Height;
end
else if Assigned(Result) then
Result.DesignInfo := (Top shl 16) or (Left and $FFFF);
MarkModified;
end;
procedure TToolsApiFormEditor.SetComponentProperty(const ComponentName: string;
const PropertyPath: string;
const Value: string);
begin
const Target = Find(ComponentName);
SetPropertyValue(Target, PropertyPath, Value);
MarkModified;
end;
procedure TToolsApiFormEditor.SetPropertyValue(const Instance: TObject;
const PropertyPath: string;
const Value: string);
begin
var Current := Instance;
const Parts = PropertyPath.Split(['.']);
// Intermediate segments (like Font in Font.Size) must be object properties.
for var Index := 0 to Length(Parts) - 2 do
begin
const Info = GetPropInfo(Current, Parts[Index]);
const IsObjectProperty = Assigned(Info) and (Info^.PropType^.Kind = tkClass);
if not IsObjectProperty then
raise EToolsApiPropertyNotSupported.CreateFmt('"%s" is not an object property of %s',
[Parts[Index], Current.ClassName]);
Current := GetObjectProp(Current, Parts[Index]);
if not Assigned(Current) then
raise EToolsApiPropertyNotSupported.CreateFmt('Property "%s" of %s is nil',
[Parts[Index], PropertyPath]);
end;
const PropertyName = Parts[High(Parts)];
const Info = GetPropInfo(Current, PropertyName);
if not Assigned(Info) then
raise EToolsApiPropertyNotSupported.CreateFmt('Property "%s" not found on %s',
[PropertyName, Current.ClassName]);
case Info^.PropType^.Kind of
tkInteger:
if SameText(string(Info^.PropType^.Name), 'TColor') then
SetOrdProp(Current, Info, StringToColor(Value))
else
SetOrdProp(Current, Info, StrToInt(Value));
tkInt64:
SetInt64Prop(Current, Info, StrToInt64(Value));
tkEnumeration:
SetEnumProp(Current, Info, Value);
tkFloat:
SetFloatProp(Current, Info, StrToFloat(Value, TFormatSettings.Invariant));
tkString, tkLString, tkWString, tkUString:
SetStrProp(Current, Info, Value);
tkSet:
SetSetProp(Current, Info, Value);
tkMethod:
SetEventHandler(Current, Info, Value);
tkClass:
SetComponentReference(Current, Info, Value);
else
raise EToolsApiPropertyNotSupported.CreateFmt('Property "%s" has an unsupported type', [PropertyName]);
end;
end;
procedure TToolsApiFormEditor.SetComponentReference(const Instance: TObject;
const Info: PPropInfo;
const ComponentName: string);
begin
// Component-reference properties (ActivePage, DataSource, Images, PopupMenu,
// ...) are set by the referenced component NAME; empty means nil.
if ComponentName.IsEmpty then
begin
SetObjectProp(Instance, Info, nil);
Exit;
end;
const Referenced = NativeComponent(FEditor.FindComponent(ComponentName));
if not Assigned(Referenced) then
raise EToolsApiComponentNotFound.CreateFmt('Referenced component "%s" not found on the form', [ComponentName]);
SetObjectProp(Instance, Info, Referenced);
end;
procedure TToolsApiFormEditor.SetEventHandler(const Instance: TObject;
const Info: PPropInfo;
const MethodName: string);
var
NativeEditor: INTAFormEditor;
begin
if MethodName.IsEmpty then
begin
var Cleared: TMethod;
Cleared.Code := nil;
Cleared.Data := nil;
SetMethodProp(Instance, Info, Cleared);
Exit;
end;
const HasDesigner = Supports(FEditor, INTAFormEditor, NativeEditor) and
Assigned(NativeEditor.FormDesigner);
if not HasDesigner then
raise EToolsApiPropertyNotSupported.Create('No form designer available to bind the event handler');
// Mirrors DesignEditors.TMethodProperty.SetValue: rename the current handler
// when the new name does not exist yet, otherwise bind through CreateMethod
// (which resolves an existing handler by name or generates a new empty one).
const FormDesigner = NativeEditor.FormDesigner;
const CurrentMethod = GetMethodProp(Instance, Info);
const CurrentName = FormDesigner.GetMethodName(CurrentMethod);
const CanRename = (CurrentName <> '') and
(SameText(CurrentName, MethodName) or (not FormDesigner.MethodExists(MethodName))) and
(not FormDesigner.MethodFromAncestor(CurrentMethod));
if CanRename then
begin
FormDesigner.RenameMethod(CurrentName, MethodName);
FormDesigner.Modified;
Exit;
end;
const IsNewMethod = (not FormDesigner.MethodExists(MethodName));
const Handler = FormDesigner.CreateMethod(MethodName, GetTypeData(Info^.PropType^));
SetMethodProp(Instance, Info, Handler);
FormDesigner.Modified;
// CreateMethod only registers the handler; ShowMethod writes the empty method
// body to the source unit, as the Object Inspector does for a new event handler.
if IsNewMethod then
FormDesigner.ShowMethod(MethodName);
end;
function TToolsApiFormEditor.AssignedEvents(const Component: TComponent): TArray<string>;
var
NativeEditor: INTAFormEditor;
begin
Result := nil;
const HasDesigner = Supports(FEditor, INTAFormEditor, NativeEditor) and
Assigned(NativeEditor.FormDesigner);
if not HasDesigner then
Exit;
const Count = GetPropList(Component.ClassInfo, [tkMethod], nil);
if Count <= 0 then
Exit;
var Props: PPropList;
GetMem(Props, Count * SizeOf(PPropInfo));
try
GetPropList(Component.ClassInfo, [tkMethod], Props);
for var Index := 0 to Count - 1 do
begin
const Info = Props^[Index];
const HandlerName = NativeEditor.FormDesigner.GetMethodName(GetMethodProp(Component, Info));
if HandlerName <> '' then
Result := Result + [Format('%s=%s', [string(Info^.Name), HandlerName])];
end;
finally
FreeMem(Props);
end;
end;
function TToolsApiFormEditor.CaptureImage: TBytes;
begin
const RootComponent = Root;
if not (RootComponent is TCustomForm) then
raise EToolsApiComponentNotFound.CreateFmt('%s is not a form and cannot be captured', [RootComponent.ClassName]);
const Bitmap = TCustomForm(RootComponent).GetFormImage;
try
const Png = TPngImage.Create;
try
Png.Assign(Bitmap);
const Stream = TBytesStream.Create;
try
Png.SaveToStream(Stream);
Result := Copy(Stream.Bytes, 0, Stream.Size);
finally
Stream.Free;
end;
finally
Png.Free;
end;
finally
Bitmap.Free;
end;
end;
procedure TToolsApiFormEditor.ShowDesigner;
begin
FEditor.Show;
end;
procedure TToolsApiFormEditor.MarkModified;
begin
FEditor.MarkModified;
end;
end.