The main problem of integration designer environment and script engine is the ability to create and maintain event handlers. EControl Form Designer Pro contains special set of events and included in library method property editor which allow easy and event handlers creation and navigation in the same manner as we have in Delphi IDE.
Associations between event (procedural property) and script procedure may be saved in TStrings object as:
<ObjectName>.<EventPropertyName>=<ScriptProcedureName>. It is the easiest way to store associations.
Set StoreEvents property to True to handle events by designer. In this case event associations will be saved in Events property and designer's I/O functions will save these events in file or stream automatically.
TzFormDesigner events for managing procedural properties:
OnGetScriptProc - called to get name of the script procedure assigned to the particular event of the object. Use this event handler for manual events processing.
OnSetScriptProc - called to assign script procedure to the particular event of the object. If the script procedure with the given name does not exist you should add it to the code editor. It occurs when user in the object inspector enters name, selects procedure in drop down list or double click on the empty event.
procedure TForm4.zFormDesigner1SetScriptProc(Sender, Instance: TObject; pInfo: PPropInfo; const EventProc: String); begin if EventProc <> '' then begin // Creating event handler text body in code editor // .......... end; end;
OnShowMethod - called when in the object inspector user double clicks on the event with assigned script procedure. Example below shows how to find and highlight script procedure using parsing results of EControl TSyntaxMemo.
// Show script procedure in code editor function TForm4.ShowMethod(const MethodName: string): Boolean; var R: TTextRange; st, en: integer; begin R := FindMethod(MethodName); Result := R <> nil; if Result then with CodeEditor.SyntObj do begin st := Tags[R.StartIdx].StartPos; if R.EndIdx <> -1 then en := Tags[R.EndIdx].EndPos else en := st; Windows.SetFocus(CodeEditor.Handle); CodeEditor.CaretStrPos := st; CodeEditor.CaretStrPos := en; CodeEditor.SetSelection(st , en - st); end; end; // Check existing script procedure function TForm4.FindMethod(const MethName: string): TTextRange; var i: integer; R: TTagBlockCondition; begin if MethName <> '' then with CodeEditor.SyntObj do begin R := TTagBlockCondition(Owner.BlockRules.ItemByName('function')); if R <> nil then for i := 0 to RangeCount - 1 do if (Ranges[i].Rule = R) and SameText(TagStr[Ranges[i].StartIdx + 1], MethName) then begin Result := Ranges[i]; Exit; end; end; Result := nil; end;
OnGetMethodNames - called when user drop down pop-up list with events in the object inspector. Example below shows how to extract script procedure names in code editor using results of text analysis by the EControl Syntax Editor SDK.
// Returns existed script procedures // Syntax Analyzer is used to extract available functions // this allows correc event processing if the code contains erros procedure TForm4.zFormDesigner1GetMethodNames(Sender: TObject; TypeData: PTypeData; Proc: TGetStrProc); var i: integer; R: TTagBlockCondition; begin with CodeEditor.SyntObj do begin // Looking for all text ranges with rule "function" R := TTagBlockCondition(Owner.BlockRules.ItemByName('function')); if R <> nil then for i := 0 to RangeCount - 1 do if (Ranges[i].Rule = R) then Proc(TagStr[Ranges[i].StartIdx + 1]); end; end;
Using text analysis provided by EControl Syntax Editor SDK allows you to make language independent text processing (for example, to make this demo working with other script language you should only have a rule "function" in the lexer of that language).
Script source (TStrings) and events associations (TStrings) may be stored with form which may be saved to any storage (file - text or binary resource, database - in BLOB or MEMO field, etc.).
Example of form declaration with scripting support:
TScriptForm = class(TForm) ... published property Code: TStrings ... property Events: TStrings ... end;
Using such approach such form may be an independent all-sufficient unit.
The best solution is to combine EControl Form Designer Pro and EControl Syntax Editor SDK to develop convenient design environment in your applications!