Skip to content

Commit

Permalink
In case paste component failed, nicer error message and no mem leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
michaliskambi committed Nov 16, 2018
1 parent ae6288f commit da8472d
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions tools/castle-editor/code/framedesign.pas
Expand Up @@ -1078,9 +1078,17 @@ procedure TDesignFrame.PasteComponent;
SelectedCount: Integer;
ParentComponent, NewComponent: TComponent;
begin
// TODO: rename to unique names

NewComponent := StringToComponent(Clipboard.AsText, DesignOwner);
try
NewComponent := StringToComponent(Clipboard.AsText, DesignOwner);
except
on E: Exception do
begin
ErrorBox('Cliboard doesn''t seem to contain a copied component.' + NL + NL +
'Trying to deserialize it failed with the error:' + NL + NL +
ExceptMessage(E));
Exit;
end;
end;

// calculate ParentComponent
GetSelected(Selected, SelectedCount);
Expand All @@ -1096,6 +1104,7 @@ procedure TDesignFrame.PasteComponent;
if not (ParentComponent is TCastleUserInterface) then
begin
ErrorBox('Clipboard contains a TCastleUserInterface instance, you need to select a TCastleUserInterface as a parent before doing "Paste Component"');
FreeAndNil(NewComponent);
Exit;
end;
(ParentComponent as TCastleUserInterface).InsertFront(NewComponent as TCastleUserInterface);
Expand All @@ -1106,13 +1115,17 @@ procedure TDesignFrame.PasteComponent;
if not (ParentComponent is TCastleTransform) then
begin
ErrorBox('Clipboard contains a TCastleTransform instance, you need to select a TCastleTransform as a parent before doing "Paste Component"');
FreeAndNil(NewComponent);
Exit;
end;
(ParentComponent as TCastleTransform).Add(NewComponent as TCastleTransform);
FinishAddingComponent(NewComponent);
end else
begin
ErrorBox(Format('Clipboard contains an instance of %s class, cannot insert it into the design',
[NewComponent.ClassName]));
FreeAndNil(NewComponent);
end;
end;

function TDesignFrame.ForEachSelectedSceneManager(
Expand Down

0 comments on commit da8472d

Please sign in to comment.