mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-05-28 11:43:02 +02:00
added owner check for designer created components
git-svn-id: trunk@5131 -
This commit is contained in:
parent
0788600787
commit
573f1ed8bd
@ -1135,6 +1135,7 @@ var
|
|||||||
var
|
var
|
||||||
NewParent: TComponent;
|
NewParent: TComponent;
|
||||||
NewParentControl: TWinControl;
|
NewParentControl: TWinControl;
|
||||||
|
NewComponent: TComponent;
|
||||||
begin
|
begin
|
||||||
if MouseDownComponent=nil then exit;
|
if MouseDownComponent=nil then exit;
|
||||||
|
|
||||||
@ -1143,7 +1144,6 @@ var
|
|||||||
ControlSelection.Clear;
|
ControlSelection.Clear;
|
||||||
|
|
||||||
// find a parent for the new component
|
// find a parent for the new component
|
||||||
writeln('AddComponent A ',FLookupRoot is TCustomForm);
|
|
||||||
if FLookupRoot is TCustomForm then begin
|
if FLookupRoot is TCustomForm then begin
|
||||||
if MouseDownComponent is TWinControl then
|
if MouseDownComponent is TWinControl then
|
||||||
NewParentControl:=TWinControl(MouseDownComponent)
|
NewParentControl:=TWinControl(MouseDownComponent)
|
||||||
@ -1192,19 +1192,21 @@ writeln('AddComponent A ',FLookupRoot is TCustomForm);
|
|||||||
NewCI := TComponentInterface(TheFormEditor.CreateComponent(
|
NewCI := TComponentInterface(TheFormEditor.CreateComponent(
|
||||||
ParentCI,SelectedCompClass.ComponentClass
|
ParentCI,SelectedCompClass.ComponentClass
|
||||||
,NewLeft,NewTop,NewWidth,NewHeight));
|
,NewLeft,NewTop,NewWidth,NewHeight));
|
||||||
|
if NewCI=nil then exit;
|
||||||
|
NewComponent:=NewCI.Component;
|
||||||
|
|
||||||
// set initial properties
|
// set initial properties
|
||||||
if NewCI.Component is TControl then
|
if NewComponent is TControl then
|
||||||
TControl(NewCI.Component).Visible:=true;
|
TControl(NewComponent).Visible:=true;
|
||||||
if Assigned(FOnSetDesigning) then
|
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)
|
// tell IDE about the new component (e.g. add it to the source)
|
||||||
NotifyComponentAdded(NewCI.Component);
|
NotifyComponentAdded(NewComponent);
|
||||||
|
|
||||||
// creation completed
|
// creation completed
|
||||||
// -> select new component
|
// -> select new component
|
||||||
SelectOnlyThisComponent(TComponent(NewCI.Component));
|
SelectOnlyThisComponent(NewComponent);
|
||||||
if not (ssShift in Shift) then
|
if not (ssShift in Shift) then
|
||||||
if Assigned(FOnUnselectComponentClass) then
|
if Assigned(FOnUnselectComponentClass) then
|
||||||
// this resets the component palette to the selection tool
|
// this resets the component palette to the selection tool
|
||||||
@ -1212,7 +1214,7 @@ writeln('AddComponent A ',FLookupRoot is TCustomForm);
|
|||||||
|
|
||||||
{$IFDEF VerboseDesigner}
|
{$IFDEF VerboseDesigner}
|
||||||
writeln('NEW COMPONENT ADDED: Form.ComponentCount=',Form.ComponentCount,
|
writeln('NEW COMPONENT ADDED: Form.ComponentCount=',Form.ComponentCount,
|
||||||
' NewCI.Control.Owner.Name=',NewCI.Component.Owner.Name);
|
' NewComponent.Owner.Name=',NewComponent.Owner.Name);
|
||||||
{$ENDIF}
|
{$ENDIF}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
@ -908,115 +908,186 @@ Var
|
|||||||
Temp: TComponentInterface;
|
Temp: TComponentInterface;
|
||||||
NewJITIndex: Integer;
|
NewJITIndex: Integer;
|
||||||
CompLeft, CompTop, CompWidth, CompHeight: integer;
|
CompLeft, CompTop, CompWidth, CompHeight: integer;
|
||||||
|
NewComponent: TComponent;
|
||||||
OwnerComponent: TComponent;
|
OwnerComponent: TComponent;
|
||||||
ParentComponent: TComponent;
|
ParentComponent: TComponent;
|
||||||
JITList: TJITComponentList;
|
JITList: TJITComponentList;
|
||||||
AControl: TControl;
|
AControl: TControl;
|
||||||
|
NewComponentName: String;
|
||||||
Begin
|
Begin
|
||||||
writeln('[TCustomFormEditor.CreateComponent] Class='''+TypeClass.ClassName+'''');
|
Result:=nil;
|
||||||
{$IFDEF IDE_MEM_CHECK}CheckHeapWrtMemCnt('TCustomFormEditor.CreateComponent A');{$ENDIF}
|
Temp:=nil;
|
||||||
Temp := TComponentInterface.Create;
|
try
|
||||||
|
writeln('[TCustomFormEditor.CreateComponent] Class='''+TypeClass.ClassName+'''');
|
||||||
|
{$IFDEF IDE_MEM_CHECK}CheckHeapWrtMemCnt('TCustomFormEditor.CreateComponent A');{$ENDIF}
|
||||||
|
|
||||||
OwnerComponent:=nil;
|
OwnerComponent:=nil;
|
||||||
if Assigned(ParentCI) then
|
if Assigned(ParentCI) then
|
||||||
begin
|
begin
|
||||||
// add as child control
|
// add as child component
|
||||||
ParentComponent:=TComponentInterface(ParentCI).Component;
|
ParentComponent:=TComponentInterface(ParentCI).Component;
|
||||||
OwnerComponent:=ParentComponent;
|
OwnerComponent:=ParentComponent;
|
||||||
if OwnerComponent.Owner<>nil then
|
if OwnerComponent.Owner<>nil then
|
||||||
OwnerComponent:=OwnerComponent.Owner;
|
OwnerComponent:=OwnerComponent.Owner;
|
||||||
Temp.FComponent := TypeClass.Create(OwnerComponent);
|
try
|
||||||
// set parent
|
NewComponent := TypeClass.Create(OwnerComponent);
|
||||||
if Temp.IsTControl then begin
|
except
|
||||||
if (ParentComponent is TWinControl)
|
on e: Exception do begin
|
||||||
and (csAcceptsControls in TWinControl(ParentComponent).ControlStyle) then
|
MessageDlg('Error creating component',
|
||||||
begin
|
'Error creating component: '+TypeClass.ClassName,
|
||||||
TWinControl(Temp.Component).Parent :=
|
mtError,[mbCancel],0);
|
||||||
TWinControl(ParentComponent);
|
exit;
|
||||||
writeln('Parent is '''+TWinControl(Temp.Component).Parent.Name+'''');
|
end;
|
||||||
end
|
end;
|
||||||
else begin
|
// check if Owner was properly set
|
||||||
TControl(Temp.Component).Parent :=
|
if NewComponent.Owner<>OwnerComponent then begin
|
||||||
TControl(ParentComponent).Parent;
|
MessageDlg('Invalid component owner',
|
||||||
writeln('Parent is '''+TControl(Temp.Component).Parent.Name+'''');
|
'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)
|
||||||
|
and (csAcceptsControls in TWinControl(ParentComponent).ControlStyle) then
|
||||||
|
begin
|
||||||
|
TWinControl(Temp.Component).Parent :=
|
||||||
|
TWinControl(ParentComponent);
|
||||||
|
writeln('Parent is '''+TWinControl(Temp.Component).Parent.Name+'''');
|
||||||
|
end
|
||||||
|
else begin
|
||||||
|
TControl(Temp.Component).Parent :=
|
||||||
|
TControl(ParentComponent).Parent;
|
||||||
|
writeln('Parent is '''+TControl(Temp.Component).Parent.Name+'''');
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
end else begin
|
||||||
|
// create a toplevel control -> a form or a datamodule
|
||||||
|
ParentComponent:=nil;
|
||||||
|
JITList:=GetJITListOfType(TypeClass);
|
||||||
|
if JITList=nil then
|
||||||
|
RaiseException('TCustomFormEditor.CreateComponent '+TypeClass.ClassName);
|
||||||
|
NewJITIndex := JITList.AddNewJITComponent;
|
||||||
|
if NewJITIndex >= 0 then begin
|
||||||
|
// create component interface
|
||||||
|
Temp := TComponentInterface.Create;
|
||||||
|
Temp.FComponent := JITList[NewJITIndex]
|
||||||
|
end else begin
|
||||||
|
exit;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
end else begin
|
{$IFDEF IDE_MEM_CHECK}CheckHeapWrtMemCnt('TCustomFormEditor.CreateComponent D ');{$ENDIF}
|
||||||
// create a toplevel control -> a form or a datamodule
|
try
|
||||||
ParentComponent:=nil;
|
NewComponentName := CreateUniqueComponentName(Temp.Component);
|
||||||
JITList:=GetJITListOfType(TypeClass);
|
Temp.Component.Name := NewComponentName;
|
||||||
if JITList=nil then
|
except
|
||||||
RaiseException('TCustomFormEditor.CreateComponent '+TypeClass.ClassName);
|
on e: Exception do begin
|
||||||
NewJITIndex := JITList.AddNewJITComponent;
|
MessageDlg('Error naming component',
|
||||||
if NewJITIndex >= 0 then
|
'Error setting the name of a component '
|
||||||
Temp.FComponent := JITList[NewJITIndex]
|
+Temp.Component.Name+':'+Temp.Component.ClassName
|
||||||
else begin
|
+' to '+NewComponentName,
|
||||||
Result:=nil;
|
mtError,[mbCancel],0);
|
||||||
exit;
|
exit;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
try
|
||||||
|
// set bounds
|
||||||
|
CompLeft:=X;
|
||||||
|
CompTop:=Y;
|
||||||
|
CompWidth:=W;
|
||||||
|
CompHeight:=H;
|
||||||
|
if (Temp.Component is TControl) then
|
||||||
|
Begin
|
||||||
|
AControl:=TControl(Temp.Component);
|
||||||
|
if CompWidth<=0 then CompWidth:=Max(5,AControl.Width);
|
||||||
|
if CompHeight<=0 then CompHeight:=Max(5,AControl.Height);
|
||||||
|
if CompLeft<0 then begin
|
||||||
|
if AControl.Parent<>nil then
|
||||||
|
CompLeft:=(AControl.Parent.Width - CompWidth) div 2
|
||||||
|
else if AControl is TCustomForm then
|
||||||
|
CompLeft:=Max(1,Min(250,Screen.Width-CompWidth-50))
|
||||||
|
else
|
||||||
|
CompLeft:=0;
|
||||||
|
end;
|
||||||
|
if CompTop<0 then begin
|
||||||
|
if AControl.Parent<>nil then
|
||||||
|
CompTop:=(AControl.Parent.Height - CompHeight) div 2
|
||||||
|
else if AControl is TCustomForm then
|
||||||
|
CompTop:=Max(1,Min(250,Screen.Height-CompHeight-50))
|
||||||
|
else
|
||||||
|
CompTop:=0;
|
||||||
|
end;
|
||||||
|
AControl.SetBounds(CompLeft,CompTop,CompWidth,CompHeight);
|
||||||
|
end
|
||||||
|
else if (Temp.Component is TDataModule) then begin
|
||||||
|
// data module
|
||||||
|
with TDataModule(Temp.Component) do begin
|
||||||
|
if CompWidth<=0 then CompWidth:=Max(50,DesignSize.X);
|
||||||
|
if CompHeight<=0 then CompHeight:=Max(50,DesignSize.Y);
|
||||||
|
if CompLeft<0 then
|
||||||
|
CompLeft:=Max(1,Min(250,Screen.Width-CompWidth-50));
|
||||||
|
if CompTop<0 then
|
||||||
|
CompTop:=Max(1,Min(250,Screen.Height-CompHeight-50));
|
||||||
|
DesignOffset.X:=CompLeft;
|
||||||
|
DesignOffset.Y:=CompTop;
|
||||||
|
DesignSize.X:=CompWidth;
|
||||||
|
DesignSize.Y:=CompHeight;
|
||||||
|
end;
|
||||||
|
end
|
||||||
|
else begin
|
||||||
|
// non TControl
|
||||||
|
with LongRec(Temp.Component.DesignInfo) do begin
|
||||||
|
Lo:=word(Min(32000,CompLeft));
|
||||||
|
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
|
||||||
|
FComponentInterfaces.Add(Temp);
|
||||||
|
|
||||||
|
if Temp.Component.Owner<>nil then
|
||||||
|
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;
|
end;
|
||||||
{$IFDEF IDE_MEM_CHECK}CheckHeapWrtMemCnt('TCustomFormEditor.CreateComponent D ');{$ENDIF}
|
|
||||||
Temp.Component.Name := CreateUniqueComponentName(Temp.Component);
|
|
||||||
|
|
||||||
// set bounds
|
|
||||||
CompLeft:=X;
|
|
||||||
CompTop:=Y;
|
|
||||||
CompWidth:=W;
|
|
||||||
CompHeight:=H;
|
|
||||||
if (Temp.Component is TControl) then
|
|
||||||
Begin
|
|
||||||
AControl:=TControl(Temp.Component);
|
|
||||||
if CompWidth<=0 then CompWidth:=Max(5,AControl.Width);
|
|
||||||
if CompHeight<=0 then CompHeight:=Max(5,AControl.Height);
|
|
||||||
if CompLeft<0 then begin
|
|
||||||
if AControl.Parent<>nil then
|
|
||||||
CompLeft:=(AControl.Parent.Width - CompWidth) div 2
|
|
||||||
else if AControl is TCustomForm then
|
|
||||||
CompLeft:=Max(1,Min(250,Screen.Width-CompWidth-50))
|
|
||||||
else
|
|
||||||
CompLeft:=0;
|
|
||||||
end;
|
|
||||||
if CompTop<0 then begin
|
|
||||||
if AControl.Parent<>nil then
|
|
||||||
CompTop:=(AControl.Parent.Height - CompHeight) div 2
|
|
||||||
else if AControl is TCustomForm then
|
|
||||||
CompTop:=Max(1,Min(250,Screen.Height-CompHeight-50))
|
|
||||||
else
|
|
||||||
CompTop:=0;
|
|
||||||
end;
|
|
||||||
AControl.SetBounds(CompLeft,CompTop,CompWidth,CompHeight);
|
|
||||||
end
|
|
||||||
else if (Temp.Component is TDataModule) then begin
|
|
||||||
// data module
|
|
||||||
with TDataModule(Temp.Component) do begin
|
|
||||||
if CompWidth<=0 then CompWidth:=Max(50,DesignSize.X);
|
|
||||||
if CompHeight<=0 then CompHeight:=Max(50,DesignSize.Y);
|
|
||||||
if CompLeft<0 then
|
|
||||||
CompLeft:=Max(1,Min(250,Screen.Width-CompWidth-50));
|
|
||||||
if CompTop<0 then
|
|
||||||
CompTop:=Max(1,Min(250,Screen.Height-CompHeight-50));
|
|
||||||
DesignOffset.X:=CompLeft;
|
|
||||||
DesignOffset.Y:=CompTop;
|
|
||||||
DesignSize.X:=CompWidth;
|
|
||||||
DesignSize.Y:=CompHeight;
|
|
||||||
end;
|
|
||||||
end
|
|
||||||
else begin
|
|
||||||
// non TControl
|
|
||||||
with LongRec(Temp.Component.DesignInfo) do begin
|
|
||||||
Lo:=word(Min(32000,CompLeft));
|
|
||||||
Hi:=word(Min(32000,CompTop));
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
|
|
||||||
{$IFDEF IDE_MEM_CHECK}CheckHeapWrtMemCnt('TCustomFormEditor.CreateComponent F ');{$ENDIF}
|
|
||||||
// add to component list
|
|
||||||
FComponentInterfaces.Add(Temp);
|
|
||||||
|
|
||||||
if Temp.Component.Owner<>nil then
|
|
||||||
CreateChildComponentInterfaces(Temp.Component.Owner);
|
|
||||||
|
|
||||||
Result := Temp;
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
Function TCustomFormEditor.CreateComponentFromStream(
|
Function TCustomFormEditor.CreateComponentFromStream(
|
||||||
|
Loading…
Reference in New Issue
Block a user