Component Editor Writing
Any common control editor (it can be called from element context menu or by double-clicking) creates OnClick blank event handler by default. This behavior can be replaced by writing a custom editor. In addition, the editor allows adding your own items to component context menu.
Basic class for all editors is declared in 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 contextual menu, you will need to override two methods, i.e. Edit
and HasEditor
. The first method performs necessary actions (for example, displays dialogue box) and returns “True,” if component state was modified. HasEditor
method should return “True” if your component has editor. If it either returns “False” or you do not override this method, editor will not be called. This becomes necessary, if your component does not have an editor and you wish to add items into component context menu.
If editor adds items into context menu, you should override GetMenuItems
(in this method, you can create a menu with help of calling AddItem
function) and Execute
(this method is called, when you select one of your items in component menu; response to selected menu item should be described here) methods.
Editor registration is performed via procedure described in “frxDsgnIntf” file:
frxComponentEditors.Register(ComponentClass: TfrxComponentClass; ComponentEditor: TfrxComponentEditorClass);
The first parameter is class name, for which editor is to be created. The second parameter is editor class name.
Let us examine simple editor for our common control, which will display a window with our element name and add "Enabled" and "Visible" items to element context menu (when items are selected, Enabled
and Visible
properties will change). Editor code, according to FastReport requirements, can be placed in a file having the same name as file with the component’s code, adding Editor suffix (for example, 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.