added owner check for designer created components

git-svn-id: trunk@5131 -
This commit is contained in:
mattias 2004-02-01 23:14:48 +00:00
parent 0788600787
commit 573f1ed8bd
2 changed files with 179 additions and 106 deletions

View File

@ -1135,6 +1135,7 @@ var
var
NewParent: TComponent;
NewParentControl: TWinControl;
NewComponent: TComponent;
begin
if MouseDownComponent=nil then exit;
@ -1143,7 +1144,6 @@ var
ControlSelection.Clear;
// find a parent for the new component
writeln('AddComponent A ',FLookupRoot is TCustomForm);
if FLookupRoot is TCustomForm then begin
if MouseDownComponent is TWinControl then
NewParentControl:=TWinControl(MouseDownComponent)
@ -1192,19 +1192,21 @@ writeln('AddComponent A ',FLookupRoot is TCustomForm);
NewCI := TComponentInterface(TheFormEditor.CreateComponent(
ParentCI,SelectedCompClass.ComponentClass
,NewLeft,NewTop,NewWidth,NewHeight));
if NewCI=nil then exit;
NewComponent:=NewCI.Component;
// set initial properties
if NewCI.Component is TControl then
TControl(NewCI.Component).Visible:=true;
if NewComponent is TControl then
TControl(NewComponent).Visible:=true;
if Assigned(FOnSetDesigning) then
FOnSetDesigning(Self,NewCI.Component,True);
FOnSetDesigning(Self,NewComponent,True);
// tell IDE about the new component (e.g. add it to the source)
NotifyComponentAdded(NewCI.Component);
NotifyComponentAdded(NewComponent);
// creation completed
// -> select new component
SelectOnlyThisComponent(TComponent(NewCI.Component));
SelectOnlyThisComponent(NewComponent);
if not (ssShift in Shift) then
if Assigned(FOnUnselectComponentClass) then
// this resets the component palette to the selection tool
@ -1212,7 +1214,7 @@ writeln('AddComponent A ',FLookupRoot is TCustomForm);
{$IFDEF VerboseDesigner}
writeln('NEW COMPONENT ADDED: Form.ComponentCount=',Form.ComponentCount,
' NewCI.Control.Owner.Name=',NewCI.Component.Owner.Name);
' NewComponent.Owner.Name=',NewComponent.Owner.Name);
{$ENDIF}
end;

View File

@ -908,24 +908,51 @@ Var
Temp: TComponentInterface;
NewJITIndex: Integer;
CompLeft, CompTop, CompWidth, CompHeight: integer;
NewComponent: TComponent;
OwnerComponent: TComponent;
ParentComponent: TComponent;
JITList: TJITComponentList;
AControl: TControl;
NewComponentName: String;
Begin
Result:=nil;
Temp:=nil;
try
writeln('[TCustomFormEditor.CreateComponent] Class='''+TypeClass.ClassName+'''');
{$IFDEF IDE_MEM_CHECK}CheckHeapWrtMemCnt('TCustomFormEditor.CreateComponent A');{$ENDIF}
Temp := TComponentInterface.Create;
OwnerComponent:=nil;
if Assigned(ParentCI) then
begin
// add as child control
// add as child component
ParentComponent:=TComponentInterface(ParentCI).Component;
OwnerComponent:=ParentComponent;
if OwnerComponent.Owner<>nil then
OwnerComponent:=OwnerComponent.Owner;
Temp.FComponent := TypeClass.Create(OwnerComponent);
try
NewComponent := TypeClass.Create(OwnerComponent);
except
on e: Exception do begin
MessageDlg('Error creating component',
'Error creating component: '+TypeClass.ClassName,
mtError,[mbCancel],0);
exit;
end;
end;
// check if Owner was properly set
if NewComponent.Owner<>OwnerComponent then begin
MessageDlg('Invalid component owner',
'The component of type '+NewComponent.ClassName
+' failed to set its owner to '
+OwnerComponent.Name+':'+OwnerComponent.ClassName,
mtError,[mbCancel],0);
exit;
end;
// create component interface
Temp := TComponentInterface.Create;
Temp.FComponent:=NewComponent;
// set parent
if Temp.IsTControl then begin
if (ParentComponent is TWinControl)
@ -948,16 +975,30 @@ Begin
if JITList=nil then
RaiseException('TCustomFormEditor.CreateComponent '+TypeClass.ClassName);
NewJITIndex := JITList.AddNewJITComponent;
if NewJITIndex >= 0 then
if NewJITIndex >= 0 then begin
// create component interface
Temp := TComponentInterface.Create;
Temp.FComponent := JITList[NewJITIndex]
else begin
Result:=nil;
end else begin
exit;
end;
end;
{$IFDEF IDE_MEM_CHECK}CheckHeapWrtMemCnt('TCustomFormEditor.CreateComponent D ');{$ENDIF}
Temp.Component.Name := CreateUniqueComponentName(Temp.Component);
try
NewComponentName := CreateUniqueComponentName(Temp.Component);
Temp.Component.Name := NewComponentName;
except
on e: Exception do begin
MessageDlg('Error naming component',
'Error setting the name of a component '
+Temp.Component.Name+':'+Temp.Component.ClassName
+' to '+NewComponentName,
mtError,[mbCancel],0);
exit;
end;
end;
try
// set bounds
CompLeft:=X;
CompTop:=Y;
@ -1008,6 +1049,15 @@ Begin
Hi:=word(Min(32000,CompTop));
end;
end;
except
on e: Exception do begin
MessageDlg('Error moving component',
'Error moving component '
+Temp.Component.Name+':'+Temp.Component.ClassName,
mtError,[mbCancel],0);
exit;
end;
end;
{$IFDEF IDE_MEM_CHECK}CheckHeapWrtMemCnt('TCustomFormEditor.CreateComponent F ');{$ENDIF}
// add to component list
@ -1017,6 +1067,27 @@ Begin
CreateChildComponentInterfaces(Temp.Component.Owner);
Result := Temp;
finally
// clean up carefully
if Result=nil then begin
if Temp=nil then begin
if NewComponent<>nil then begin
try
NewComponent.Free;
NewComponent:=nil;
except
MessageDlg('Error destroying component',
'Error destroying component of type '+TypeClass.ClassName,
mtError,[mbCancel],0);
end;
end;
end;
if (Result<>Temp) then begin
Temp.Free;
Temp:=nil;
end;
end;
end;
end;
Function TCustomFormEditor.CreateComponentFromStream(