OTA - Component Editors

Component Editors, well edit components. The difference between a property editor and a component editor is typically component editors allow editing complex information about a component that is either to awkward or impossible it edit in the Object Inspector. For example drop an Image List component on a form then select it. You will see the properties of the Image List component in the Object Inspector. Now double click the component itself or right click the component and choose the "Image List Editor" menu item to run the built in editor for the Image List to add images to the component. Attempting to add these images through the Object Inspector would be difficult at best.

  If you want to add custom property types or redefine how a particular property is edited in the Object Inspector you would implement a Property Editor.

  We will start the Component Editor off the same way as all the demos, by creating the units for the Editor. We will also create a new component for the editor.

unit ComponentEditor;

interface

{$I Compilers.inc}

uses
  Windows,
  {$IFDEF COMPILER_6_UP}
  DesignIntf,
  DesignEditors,
  DesignMenus,
  {$ELSE}
  DsgnIntf,
  DsgnWnds,
  Menus,
  {$ENDIF}
  Classes,
  SysUtils;

type

  //
  // Sample Component that will have component editor assigned to it.
  //
  TMustangPeakComponent = class(TComponent)
  private
  FExampleProp1: Integer;
  FExampleProp2: string;
  published
  property ExampleProp1: Integer read FExampleProp1 write FExampleProp1;
  property ExampleProp2: string read FExampleProp2 write FExampleProp2;
  end;

  //
  // Component Edtior for the above Component
  //

  TMustangPeakComponentEditor = class(TComponentEditor)
  protected
  procedure Execute(VerbIndex: Integer);
  public
  procedure Edit; override;
  procedure ExecuteVerb(Index: Integer); override;
  function GetVerb(Index: Integer): string; override;
  function GetVerbCount: Integer; override;
  procedure Copy; override;
  procedure PrepareItem(Index: Integer; const AItem:{$IFDEF COMPILER_6_UP}IMenuItem{$ELSE}TMenuItem{$ENDIF}); override;
  end;

implementation

{ TMustangPeakComponentEditor }

procedure TMustangPeakComponentEditor.Copy;
begin
  // Called to allow Editor copy the component to clipboard in Custom formats.
  // Does not allow to write component to clipboard for the IDE to use
end;

procedure TMustangPeakComponentEditor.Edit;
begin
  // Called when the Component is double clicked
  Execute(-1);
end;

procedure TMustangPeakComponentEditor.Execute(VerbIndex: Integer);
begin
  MessageBox(0, PChar( 'Executed Verb ID ' + IntToStr(VerbIndex)), 'Execute It!', MB_OK);
end;

procedure TMustangPeakComponentEditor.ExecuteVerb(Index: Integer);
begin
  // Called when the Component context menu item is selected
  Execute(Index)
end;

function TMustangPeakComponentEditor.GetVerb(Index: Integer): string;
begin
 // Returns the text to place in the Context Menu at the index
 case Index of
  0: Result := 'Editor Menu 0';
  1: Result := 'Editor Menu 1';
 end;
end;

function TMustangPeakComponentEditor.GetVerbCount: Integer;
begin
  // Return the number of context menu items to add to the Component menu
  Result := 2;
end;

procedure TMustangPeakComponentEditor.PrepareItem(Index: Integer; const AItem:
  {$IFDEF COMPILER_6_UP}IMenuItem{$ELSE}TMenuItem{$ENDIF});
begin
  // Called as each context menu item is initialized before it is shown. Allows
  // for each item to be modified dynamically before it is shown.
end;

end.

  Again I like a separate registration unit to register the newly created objects for the IDE.

unit ComponentEditorReg;

interface

{$I Compilers.inc}

uses
  ToolsApi,
  Classes,
  {$IFDEF COMPILER_6_UP}
  DesignIntf,
  DesignEditors,
  {$ELSE}
  DsgnIntf,
  DsgnWnds,
  {$ENDIF}
  ComponentEditor;

procedure Register;

implementation

procedure Register;
begin
  // Register a sample Component and place it in a Component tab named "MustangPeak"
  RegisterComponents('Mustangpeak', [TMustangPeakComponent]);

  // Assign our Component Editor with our Component
  RegisterComponentEditor(TMustangPeakComponent, TMustangPeakComponentEditor);
end;

end.

  Just for completeness we will make a new image for our component. Now just as with every other OTA project we need to make a package to contain our new component and editor.

  Here is our completed package ready to install.

Component Editor Package

  Once installed create a new application and drop a new Mustangpeak component on the form and try the various ways of invoking the component editor

  • Double Clicking the Component

Editor Double Clicked

  • Selecting the Items in the Context Menu

Editor Menu

Back to OpenTools API page.

 


mustangpeak.net

  Last Modified on: