Friday, July 27, 2007

Creating Gallery Plugins for Delphi and C++Builder

When creating gallery plugins with the IOTAWizardServices service using AddService and RemoveService I found that when adding a lot of gallery items there was a lot of housekeeping that needed to happen for each wizard. So I created a simple class called TWizardList that handles the monotony of adding and removing wizards. It really is quite simple, the code is below, but it sure made life a lot easier. Just create one of these per package, and free it on unit finalization and bam, instant gallery item registration and unregistration.

Feel free to use this class in any of your Delphi or C++Builder plugins. It really will save you some time.


unit IdeHelpers;

interface

uses
ToolsAPI;

type
TWizardList = class
private
FWizardService: IOTAWizardServices;
FItems: array of Integer;

public
constructor Create;
destructor Destroy; override;

function Add(const Wizard: IOTAWizard): Integer;
procedure Remove(Value: Integer);
end;

implementation

{ TWizardList }

constructor TWizardList.Create;
begin
FWizardService := nil;
end;

function TWizardList.Add(const Wizard: IOTAWizard): Integer;
var
Index: Integer;
begin
if FWizardService = nil then
FWizardService := BorlandIDEServices as IOTAWizardServices;

Result := FWizardService.AddWizard(Wizard);
Index := Length(FItems);
SetLength(FItems, Index + 1);
FItems[Index] := Result;
end;

procedure TWizardList.Remove(Value: Integer);
var
Index: Integer;
begin
if FWizardService = nil then
FWizardService := BorlandIDEServices as IOTAWizardServices;

FWizardService.RemoveWizard(Value);

for Index := 0 to Length(FItems) - 1 do
begin
if FItems[Index] = Value then
begin
FItems[Index] := -1;
end;
end;
end;

destructor TWizardList.Destroy;
var
Index: Integer;
begin
if FWizardService <> nil then
begin
for Index := 0 to Length(FItems) - 1 do
begin
if FItems[Index] <> -1 then
FWizardService.RemoveWizard(FItems[Index]);
end;
end;

inherited;
end;

end.

No comments:

Post a Comment