unit GXModuleCreator;

interface

procedure Register;

implementation

uses SysUtils, Windows, Dialogs, DesignIntf, ToolsAPI;

{$IF CompilerVersion >= 16}
  {$DEFINE D2005PLUS}
{$IFEND}

type
  TGxModuleCreatorWizard = class(TNotifierObject, IOTAWizard, IOTARepositoryWizard,
    IOTARepositoryWizard60, {$IFDEF D2005PLUS}IOTARepositoryWizard80,{$ENDIF} IOTAFormWizard)
  public
    // IOTAWizard
    function GetIDString: string;
    function GetName: string;
    function GetState: TWizardState;
    procedure Execute;
    // IOTARepositoryWizard
    function GetAuthor: string;
    function GetComment: string;
    function GetPage: string;
    function GetGlyph: Cardinal;
    // IOTARepositoryWizard60
    function GetDesigner: string;
    {$IFDEF D2005PLUS}
    // IOTARepositoryWizard80
    function GetPersonality: string;
    function GetGalleryCategory: IOTAGalleryCategory;
    {$ENDIF}
  end;

  TGxModuleCreator = class(TInterfacedObject, IOTACreator, IOTAModuleCreator)
  public
    // IOTACreator
    function GetCreatorType: string;
    function GetExisting: Boolean;
    function GetFileSystem: string;
    function GetOwner: IOTAModule;
    function GetUnnamed: Boolean;
    // IOTAModuleCreator
    function GetAncestorName: string;
    function GetImplFileName: string;
    function GetIntfFileName: string;
    function GetFormName: string;
    function GetMainForm: Boolean;
    function GetShowForm: Boolean;
    function GetShowSource: Boolean;
    function NewFormFile(const FormIdent, AncestorIdent: string): IOTAFile;
    function NewImplSource(const ModuleIdent, FormIdent, AncestorIdent: string): IOTAFile;
    function NewIntfSource(const ModuleIdent, FormIdent, AncestorIdent: string): IOTAFile;
    procedure FormCreated(const FormEditor: IOTAFormEditor);
  end;

  TGxSourceFile = class(TInterfacedObject, IOTAFile)
  private
    FSource: string;
  public
    function GetSource: string;
    function GetAge: TDateTime;
    constructor Create(const Source: string);
  end;

procedure Register;
begin
  RegisterPackageWizard(TGxModuleCreatorWizard.Create);
end;

{ TGxModuleCreatorWizard }

procedure TGxModuleCreatorWizard.Execute;
begin
  (BorlandIDEServices as IOTAModuleServices).CreateModule(TGxModuleCreator.Create);
end;

function TGxModuleCreatorWizard.GetAuthor: string;
begin
  Result := 'Erik.Berry';
end;

function TGxModuleCreatorWizard.GetComment: string;
begin
  Result := 'GX Module Creator';
end;

function TGxModuleCreatorWizard.GetDesigner: string;
begin
  Result := dVCL;
end;

{$IFDEF D2005PLUS}
function TGxModuleCreatorWizard.GetGalleryCategory: IOTAGalleryCategory;
var
  Category: IOTAGalleryCategory;
  CatManager: IOTAGalleryCategoryManager;
begin
  CatManager := (BorlandIDEServices as IOTAGalleryCategoryManager);
  Assert(Assigned(CatManager));
  Category := CatManager.FindCategory(sCategoryDelphiNewFiles);
  Assert(Assigned(Category));
  Result := Category;
end;

function TGxModuleCreatorWizard.GetPersonality: string;
begin
  Result := sDelphiPersonality;
end;
{$ENDIF}

function TGxModuleCreatorWizard.GetGlyph: Cardinal;
begin
  Result := 0;
end;

function TGxModuleCreatorWizard.GetIDString: string;
begin
  Result := 'EB.GXModuleCreatorWizard';
end;

function TGxModuleCreatorWizard.GetName: string;
begin
  Result := 'GX ModuleCreator';
end;

function TGxModuleCreatorWizard.GetPage: string;
begin
  Result := 'New';
end;

function TGxModuleCreatorWizard.GetState: TWizardState;
begin
  Result := [wsEnabled];
end;

{ TGxModuleCreator }

procedure TGxModuleCreator.FormCreated(const FormEditor: IOTAFormEditor);
begin
  // Nothing
end;

function TGxModuleCreator.GetAncestorName: string;
begin
  Result := 'Form';
end;

function TGxModuleCreator.GetCreatorType: string;
begin
  // Return sUnit or sText as appropriate
  Result := sForm;
end;

function TGxModuleCreator.GetExisting: Boolean;
begin
  Result := False;
end;

function TGxModuleCreator.GetFileSystem: string;
begin
  Result := '';
end;

function TGxModuleCreator.GetFormName: string;
begin
  Result := '';
end;

function TGxModuleCreator.GetImplFileName: string;
begin
  Result := '';
end;

function TGxModuleCreator.GetIntfFileName: string;
begin
  Result := '';
end;

function TGxModuleCreator.GetMainForm: Boolean;
begin
  Result := False;
end;

function TGxModuleCreator.GetOwner: IOTAModule;
var
  ModuleServices: IOTAModuleServices;
  Module: IOTAModule;
  NewModule: IOTAModule;
begin
  // You may prefer to return the project group's ActiveProject instead
  Result := nil;
  ModuleServices := (BorlandIDEServices as IOTAModuleServices);
  Module := ModuleServices.CurrentModule;

  if Module <> nil then
  begin
    if Module.QueryInterface(IOTAProject, NewModule) = S_OK then
      Result := NewModule

    else if Module.OwnerModuleCount > 0 then
    begin
      NewModule := Module.OwnerModules[0];
      if NewModule <> nil then
        if NewModule.QueryInterface(IOTAProject, Result) <> S_OK then
          Result := nil;
    end;
  end;
end;

function TGxModuleCreator.GetShowForm: Boolean;
begin
  Result := True;
end;

function TGxModuleCreator.GetShowSource: Boolean;
begin
  Result := True;
end;

function TGxModuleCreator.GetUnnamed: Boolean;
begin
  Result := True;
end;

function TGxModuleCreator.NewFormFile(const FormIdent, AncestorIdent: string): IOTAFile;
begin
  Result := nil;
end;

function TGxModuleCreator.NewImplSource(const ModuleIdent, FormIdent, AncestorIdent: string): IOTAFile;
const
  sSource =
  'unit %s;' + #13#10 +
  '// Created with TGxModuleCreator' + #13#10 +
  '' + #13#10 +
  'interface' + #13#10 +
  '' + #13#10 +
  'uses' + #13#10 +
  '  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;' + #13#10 +
  '' + #13#10 +
  'type' + #13#10 +
  '  T%s = class(T%s)' + #13#10 +
  '  private' + #13#10 +
  '    { Private declarations }' + #13#10 +
  '  public' + #13#10 +
  '    { Public declarations }' + #13#10 +
  '  end;' + #13#10 +
  '' + #13#10 +
  'var' + #13#10 +
  '  %s: T%s;' + #13#10 +
  '' + #13#10 +
  'implementation' + #13#10 +
  '' + #13#10 +
  '{$R *.DFM}' + #13#10 +
  '' + #13#10 +
  'end.';
begin
  Result := TGxSourceFile.Create(Format(sSource, [ModuleIdent, FormIdent,
                           AncestorIdent, FormIdent, FormIdent]));
  // or Result := nil; for the default unit
end;

function TGxModuleCreator.NewIntfSource(const ModuleIdent, FormIdent, AncestorIdent: string): IOTAFile;
begin
  Result := nil;
end;

{ TGxSourceFile }

constructor TGxSourceFile.Create(const Source: string);
begin
  FSource := Source;
end;

function TGxSourceFile.GetAge: TDateTime;
begin
  Result := -1;
end;

function TGxSourceFile.GetSource: string;
begin
  Result := FSource;
end;

end.

