TFormScript : Shows how the TatPascalScripter power can be applied to allow end user form customization
Downloads :


Form before scripted modification
  Form after scripted modification

Script to adapt the form for visually impaired users :



Description :


TFormScript shows how easy it is to use the TatPascalScripter component to allow end user form customization. When used in all your forms, the end user can change the forms himself by writing a small script and meanwhile save you time and energy to create full application updates for whatever small form look issue the customer complains about. The capabilities are only limited by your imagination.

While TatPascalScripter is written with an as general as possible interface, creating a descendant component that has some built-in functionality especially for form customizing will save you time for each form you need this. TFormScript initializes the TatPascalScripter engine to allow access from the script to all form controls. Additionally it adds constants for easy color settings and methods to change font styles.

FormScript architecture :

TFormScript inherits from TatPascalScripter and does most of the form specific initializations in the Loaded method and in the Prepare routine. The Loaded method is executed each time the component is loaded from the form stream.

1) Initialization in the Loaded method

procedure TFormScript.Loaded;
begin
  inherited;
  with AddDelphiClass(TWinControl) do
  begin
    AddMethod('ControlCount',0,tkInteger,nil,GetControlCount);
    AddMethod('Controls',1,tkClass,TControl,GetControls);
  end;

  with AddDelphiClass(TFont) do
  begin
    AddMethod('SetBold',1,tkNone,nil,SetBold);
    AddMethod('SetItalic',1,tkNone,nil,SetBold);
    AddMethod('SetUnderline',1,tkNone,nil,SetBold);
  end;

   SystemLibrary.AddObject('Form', Owner);
   FOwner := TForm(Owner);

  SystemLibrary.AddConstant('cRed',clRed);
  //... initialize more color constants here

  SystemLibrary.AddConstant('False',False);
  SystemLibrary.AddConstant('True',True);

  // automatically load the script from file
  if FScriptFile <> '' then
  if FileExists(FScriptFile) then
  SourceCode.LoadFromFile(FScriptFile);
end;

As is shown the Loaded method adds the methods SetBold, SetItalic, SetUnderline to a TFont object. This is a convenient method to allow manipulation of the array property Style of a TFont object from the Pascal script. The routines take one boolean parameter and are implemented like this :

procedure TFormScript.SetBold;
begin
  if GetInputArgAsBoolean(0) then
    TFont(CurrentObject).Style := TFont(CurrentObject).Style + [fsBold]
  else
    TFont(CurrentObject).Style := TFont(CurrentObject).Style - [fsBold];
end;

From the script, whenever there is an instance of a font, you can write : Font.SetBold(true) or Font.SetBold(False);

2) Preparing for access of all form controls

To avoid the lengthy process of adding all form controls one by one to the TatPascalScripter for access to their properties, the method Prepare does this in background whenever the script is compiled (by overriding the TatPascalScripter Compile method). This Prepare method calls AddChildControls which recursively add all form controls by name to the scripter

procedure TFormScript.AddChildControls(Control: TWinControl);
var
  i: Integer;
begin
  for i := 0 to Control.ControlCount - 1 do
  begin
    SystemLibrary.AddObject(Control.Controls[i].Name,Control.Controls[i]);

    if Control.Controls[i] is TWinControl then
    if (Control.Controls[i] as TWinControl).ControlCount > 0 then
    begin
       AddChildControls(TWinControl(Control.Controls[i]));
    end;
  end;
end;

procedure TFormScript.Prepare;
begin
  AddChildControls(FOwner);
end;

3) Starting to play with scriptable forms

As you can see, there is really not much more to making forms scriptable through TatPascalScripter. You can access the form properties from the Form instance in the script, ie. you can write Form.Caption := 'Test'; or Form.Color := clRed; You can refer to the controls on the form by their name, ie. when there is an edit control named Edit1, you can access its Text property by writing Edit1.Text := 'Test'; etc ...

To show all this, a Delphi sample application that has some scripts to modify the form is included in the distribution. First install the component TFormScript, by adding FORMSCRIPT.PAS to the TatPascalScripter package file and recompile this package. Then open the CUSTFORM.DPR project from Delphi.