paxCompiler/help/demo_event_handlers.htm
Dalibor Marković 9d0de424e8
Init
Signed-off-by: Dalibor Marković <dalibor31@gmail.com>
2024-07-06 22:28:12 +02:00

182 lines
6.0 KiB
HTML

<html>
<head>
<link rel=stylesheet type="text/css" href="styles.css">
</head>
<body>
<H2>
paxCompiler for Delphi. Script-defined event handlers.
</H2>
<hr>
<h4>Script</h4>
<blockquote>
<pre>
<font color="blue"><b>type</b></font>
TMyHandler = <font color="blue"><b>class</b></font>
<font color="blue"><b>procedure</b></font> Handle(Sender: TObject);
<font color="blue"><b>end</b></font>;
<font color="blue"><b>procedure</b></font> TMyHandler.Handle(Sender: TObject);
<font color="blue"><b>begin</b></font>
ShowMessage(<font color="Red">'Script-defined handler. Sender: '</font> + Sender.ClassName);
<font color="blue"><b>end</b></font>;
<font color="blue"><b>procedure</b></font> TMyHandler.Dispose(Sender: TObject);
<font color="blue"><b>begin</b></font>
Free;
<font color="blue"><b>end</b></font>;
<font color="blue"><b>var</b></font>
X: TMyHandler;
E: TNotifyEvent;
<font color="blue"><b>procedure</b></font> SetHandler;
<font color="blue"><b>begin</b></font>
E := ClickMe.OnClick;
ClickMe.OnClick := X.Handle;
<font color="blue"><b>end</b></font>;
<font color="blue"><b>procedure</b></font> RestoreHandler;
<font color="blue"><b>begin</b></font>
ClickMe.OnClick := E;
<font color="blue"><b>end</b></font>;
<font color="blue"><b>begin</b></font>
X := TMyHandler.Create;
Form1.OnDestroy := X.Dispose;
ShowMessage(<font color="Red">'The script was compiled and initialized successfully.'</font>);
<font color="blue"><b>end</b></font>.
</pre>
</blockquote>
<h4>Delphi Application</h4>
<blockquote>
<pre>
<font color="blue"><b>unit</b></font> Unit1;
<font color="blue"><b>interface</b></font>
<font color="blue"><b>uses</b></font>
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, PaxProgram, PaxCompiler;
<font color="blue"><b>type</b></font>
TForm1 = <font color="blue"><b>class</b></font>(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Memo1: TMemo;
ClickMe: TButton;
PaxCompiler1: TPaxCompiler;
PaxPascalLanguage1: TPaxPascalLanguage;
PaxProgram1: TPaxProgram;
<font color="blue"><b>procedure</b></font> Button1Click(Sender: TObject);
<font color="blue"><b>procedure</b></font> ClickMeClick(Sender: TObject);
<font color="blue"><b>procedure</b></font> Button2Click(Sender: TObject);
<font color="blue"><b>procedure</b></font> Button3Click(Sender: TObject);
<font color="blue"><b>private</b></font>
{ Private declarations }
P_SetHandler: Pointer;
P_RestoreHandler: Pointer;
<font color="blue"><b>public</b></font>
{ Public declarations }
<font color="blue"><b>end</b></font>;
<font color="blue"><b>var</b></font>
Form1: TForm1;
<font color="blue"><b>implementation</b></font>
{$R *.dfm}
<font color="blue"><b>var</b></font>
H_TButton, H_TForm1: Integer;
<font color="blue"><b>type</b></font>
TProcP = <font color="blue"><b>procedure</b></font>;
// create compiled-script
<font color="blue"><b>procedure</b></font> TForm1.Button1Click(Sender: TObject);
<font color="blue"><b>var</b></font>
I: Integer;
H: Integer;
<font color="blue"><b>begin</b></font>
<font color="blue"><b>if</b></font> PaxProgram1.DataSize > 0 <font color="blue"><b>then</b></font>
<font color="blue"><b>begin</b></font>
ShowMessage(<font color="Red">'Script is already compiled.'</font>);
<font color="blue"><b>Exit</b></font>;
<font color="blue"><b>end</b></font>;
PaxCompiler1.Reset;
PaxCompiler1.RegisterLanguage(PaxPascalLanguage1);
PaxCompiler1.AddModule(<font color="Red">'1'</font>, <font color="Red">'Pascal'</font>);
PaxCompiler1.AddCode(<font color="Red">'1'</font>, Memo1.Lines.Text);
PaxCompiler1.RegisterVariable(0, <font color="Red">'ClickMe'</font>, H_TButton, @ClickMe);
PaxCompiler1.RegisterVariable(0, <font color="Red">'Form1'</font>, H_TForm1, @Form1);
PaxPascalLanguage1.SetCallConv(_ccREGISTER);
<font color="blue"><b>if</b></font> PaxCompiler1.Compile(PaxProgram1) <font color="blue"><b>then</b></font>
<font color="blue"><b>begin</b></font>
H := PaxCompiler1.GetHandle(0, <font color="Red">'SetHandler'</font>, true);
P_SetHandler := PaxProgram1.GetAddress(H);
H := PaxCompiler1.GetHandle(0, <font color="Red">'RestoreHandler'</font>, true);
P_RestoreHandler := PaxProgram1.GetAddress(H);
PaxProgram1.Run;
<font color="blue"><b>end</b></font>
<font color="blue"><b>else</b></font>
<font color="blue"><b>for</b></font> I:=0 <font color="blue"><b>to</b></font> PaxCompiler1.ErrorCount - 1 <font color="blue"><b>do</b></font>
ShowMessage(PaxCompiler1.ErrorMessage[I]);
<font color="blue"><b>end</b></font>;
// set up script-defined event handler
<font color="blue"><b>procedure</b></font> TForm1.Button2Click(Sender: TObject);
<font color="blue"><b>begin</b></font>
PaxProgram1.BeginCall;
TProcP(P_SetHandler);
PaxProgram1.EndCall;
ShowMessage(<font color="Red">'ClickMe contains script-defined event handler now.'</font>);
<font color="blue"><b>end</b></font>;
// restore host-defined event handler
<font color="blue"><b>procedure</b></font> TForm1.Button3Click(Sender: TObject);
<font color="blue"><b>begin</b></font>
PaxProgram1.BeginCall;
TProcP(P_RestoreHandler);
PaxProgram1.EndCall;
ShowMessage(<font color="Red">'Host-defined handler is restored.'</font>);
<font color="blue"><b>end</b></font>;
<font color="blue"><b>procedure</b></font> TForm1.ClickMeClick(Sender: TObject);
<font color="blue"><b>begin</b></font>
ShowMessage(<font color="Red">'Host-defined event handler. Sender: '</font> + Sender.ClassName);
<font color="blue"><b>end</b></font>;
<font color="blue"><b>initialization</b></font>
H_TButton := RegisterClassType(0, TButton);
H_TForm1 := RegisterClassType(0, TForm1);
RegisterHeader(0, <font color="Red">'procedure ShowMessage(const Msg: string);'</font>, @ShowMessage);
<font color="blue"><b>end</b></font>.
</pre>
</blockquote>
<p>
<HR>
<font size = 1 color ="gray">
Copyright &copy; 2006-2009
VIRT Laboratory. All rights reserved.
</font>
</body>
</html>