1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
{
*****************************************************************************
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
Author: Mattias Gaertner
Abstract:
Example for RTTI controls.
Demonstrates how to write your own property editors to access readonly
properties.
}
unit Example3;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, RTTICtrls,
StdCtrls, PropEdits;
type
{ TBall - a class with some readonly properties and a procedure to set the
properties. }
TBall = class(TPersistent)
private
FX: integer;
FY: integer;
FSize: word;
public
procedure SetBall(const NewX, NewY: integer; const NewSize: word);
published
// published readonly properties
property X: integer read FX;
property Y: integer read FY;
property Size: word read FSize;
end;
{ TBallPropertyEditor - a property editor for the TBall properties }
TBallPropertyEditor = class(TIntegerPropertyEditor)
public
procedure SetValue(const NewValue: string); override;
end;
{ TForm1 }
TForm1 = class(TForm)
SizeTIEdit: TTIEdit;
XLabel: TLabel;
YLabel: TLabel;
SizeLabel: TLabel;
XTIEdit: TTIEdit;
YTIEdit: TTIEdit;
procedure Form1Create(Sender: TObject);
procedure Form1Destroy(Sender: TObject);
procedure Form1Paint(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
Ball1: TBall;
end;
var
Form1: TForm1;
implementation
{$R example3.lfm}
{ TBall }
procedure TBall.SetBall(const NewX, NewY: integer; const NewSize: word);
begin
if (FX=NewX) and (FY=NewY) and (FSize=NewSize) then exit;
FX:=NewX;
FY:=NewY;
FSize:=NewSize;
Form1.Invalidate;
end;
{ TForm1 }
procedure TForm1.Form1Create(Sender: TObject);
begin
Ball1:=TBall.Create;
Ball1.SetBall(200,100,20);
XTIEdit.Link.SetObjectAndProperty(Ball1,'X');
YTIEdit.Link.SetObjectAndProperty(Ball1,'Y');
SizeTIEdit.Link.SetObjectAndProperty(Ball1,'Size');
end;
procedure TForm1.Form1Destroy(Sender: TObject);
begin
// unlink properties
XTIEdit.Link.TIObject:=nil;
YTIEdit.Link.TIObject:=nil;
SizeTIEdit.Link.TIObject:=nil;
Ball1.Free;
end;
procedure TForm1.Form1Paint(Sender: TObject);
begin
with Canvas do begin
Brush.Color:=clBlue;
Ellipse(Ball1.X-Ball1.Size,Ball1.Y-Ball1.Size,
Ball1.X+Ball1.Size,Ball1.Y+Ball1.Size);
end;
end;
{ TBallPropertyEditor }
procedure TBallPropertyEditor.SetValue(const NewValue: string);
var
L: integer;
Ball: TBall;
X: integer;
Y: integer;
Size: word;
PropName: String;
begin
L := StrToIntDef(NewValue,0);
Ball:=GetComponent(0) as TBall;
PropName:=GetName;
if CompareText(PropName,'X')=0 then X:=L else X:=Ball.X;
if CompareText(PropName,'Y')=0 then Y:=L else Y:=Ball.Y;
if CompareText(PropName,'Size')=0 then Size:=Word(L) else Size:=Ball.Size;
Ball.SetBall(X,Y,Size);
end;
initialization
RegisterPropertyEditor(TypeInfo(integer),TBall,'X',TBallPropertyEditor);
RegisterPropertyEditor(TypeInfo(integer),TBall,'Y',TBallPropertyEditor);
RegisterPropertyEditor(TypeInfo(word),TBall,'Size',TBallPropertyEditor);
end.
|