Writing Component Editors

Top  Previous  Next

 

All common control editors (opened from a control's context menu or by double-clicking) create blank OnClick event handlers by default. This behavior can be altered by writing a custom editor. Also, the custom editor can add extra items to the component's context menu.

 

The base class for all editors is “TfrxComponentEditor”, declared in the frxDsgnIntf file:

 

TfrxComponentEditor = class(TObject)

protected

function AddItem(Caption: String; Tag: Integer;

                  Checked: Boolean = False): TMenuItem;

public

function Edit: Boolean; virtual;

function HasEditor: Boolean; virtual;

function Execute(Tag: Integer; Checked: Boolean): Boolean; virtual;

procedure GetMenuItems; virtual;

property Component: TfrxComponent readonly;

property Designer: TfrxCustomDesigner readonly;

end;

 

If your editor does not create its own items in the contextual menu you will need to override two methods, “Edit” and “HasEditor.” The first method performs essential actions (for example, displays the dialogue box) and returns “True” if the component's content was modified. The “HasEditor” method should return “True” if your component has an editor. If it either returns “False” or the method is not overridden the editor will not be opened. It should return “False” if your component does not have an editor and you wish to add items to the component's context menu.

 

If the editor adds items to the context menu you should override “GetMenuItems” (where you can create a menu using the AddItem function) and “Execute” (where you can create the actions initiated by selecting the items in the component menu).

 

Editor registration is performed using the procedure defined in the “frxDsgnIntf” file:

 

frxComponentEditors.Register(ComponentClass: TfrxComponentClass;

                            ComponentEditor: TfrxComponentEditorClass);

 

The first parameter is the class name of the component for which the editor is being created. The second parameter is the class name of the editor.

 

Let's look at a simple editor for our common control, which will display a window with our element name and also add "Enabled" and "Visible" items to control's context menu (which change the “Enabled” and “Visible” properties). FastReport requires that the Editor code is placed in a file having the same name as the file having the component’s code, with 'Editor' added as suffix (frxBitBtnEditor.pas in our case).

 

uses frxClass, frxDsgnIntf, frxBitBtn;

 

type

 TfrxBitBtnEditor = class(TfrxComponentEditor)

public

  function Edit: Boolean; override;

  function HasEditor: Boolean; override;

  function Execute(Tag: Integer; Checked: Boolean): Boolean; override;

  procedure GetMenuItems; override;

end;

 

function TfrxBitBtnEditor.Edit: Boolean;

var

 c: TfrxBitBtnControl;

begin

 Result := False;

 { Component property is edited component;

   in this case, it is  TfrxBitBtnControl }

 c := TfrxBitBtnControl(Component);

 ShowMessage('This is ' + c.Name);

end;

 

function TfrxBitBtnEditor.HasEditor: Boolean;

begin

 Result := True;

end;

 

function TfrxBitBtnEditor.Execute(Tag: Integer; Checked: Boolean): Boolean;

var

 c: TfrxBitBtnControl;

begin

 Result := True;

 c := TfrxBitBtnControl(Component);

if Tag = 1 then

   c.Enabled := Checked

else if Tag = 2 then

   c.Visible := Checked;

end;

 

procedure TfrxBitBtnEditor.GetMenuItems;

var

 c: TfrxBitBtnControl;

begin

 c := TfrxBitBtnControl(Component);

 { AddItem method parameters: menu item name,

   its tag and Checked/Unchecked condition }

 AddItem('Enabled', 1, c.Enabled);

 AddItem('Visible', 2, c.Visible);

end;

 

initialization

 frxComponentEditors.Register(TfrxBitBtnControl, TfrxBitBtnEditor);

 

end.