{ ------------------------------------------------------------- }
{ Purpose : Next Standard 6                                     }
{ By      : Bojan Nikolic                                       }
{ For     : BergSoft                                            }
{ ------------------------------------------------------------- }
{ Copyright (C) BergSoft.net 2012. All Rights Reserved.         }
{ ------------------------------------------------------------- }
unit NxDesignWindow6;

interface

uses
  { Standard }
  Classes, Types,
  { Controls }
  Forms,
  { Design }
  DesignIntf, DesignWindows;

type
  TNxDesignWindow = class(TDesignWindow)
  private
    FDesigner: IDesigner;
    FOwnerControl: TPersistent;
  protected
    { VCL Virtual Methods }
    procedure Notification(AComponent: TComponent; Operation:
      TOperation); override;
    procedure SetOwnerControl(const Value: TPersistent); virtual;
  public
    property Designer: IDesigner read FDesigner write FDesigner;
    property OwnerControl: TPersistent read FOwnerControl write SetOwnerControl;
  end;

  { Functions }
  function ShowForm(Owner: TPersistent; ComponentClass: TComponentClass): Boolean;

implementation

uses
  { Standard }
  Dialogs;

function ShowForm(Owner: TPersistent; ComponentClass: TComponentClass): Boolean;
var
  i: Integer;
  Form: TCustomForm;
begin
  { Try to locate TNxDesignWindow with same OwnerControl
    to show it again }
  Result := False;

  for i := 0 to Pred(Screen.FormCount) do
  begin
    Form := Screen.Forms[i];
    if Form is TNxDesignWindow then
      if (TNxDesignWindow(Form).OwnerControl = Owner)
        and (Form.ClassType = ComponentClass) then
      begin
        Form.Show;

        { Bring Form to Front }
        if Form.WindowState = wsMinimized then Form.WindowState := wsNormal;

        Result := True;

        Exit;
      end;
  end;
end;

{ TNxDesignWindow }

procedure TNxDesignWindow.Notification(AComponent: TComponent;
  Operation: TOperation);
begin
  inherited;
  { Note: Free (& Close) this Window when FOwnerControl
          is deleted. }
  if Operation = opRemove then
  begin
    if AComponent = FOwnerControl then Free;
  end;
end;

procedure TNxDesignWindow.SetOwnerControl(const Value: TPersistent);
begin
  FOwnerControl := Value;
  if (FOwnerControl is TComponent) then
    TComponent(FOwnerControl).FreeNotification(Self);
end;

end.
