summaryrefslogtreecommitdiff
path: root/examples/apps/uidesigner/vfdwidgetclass.pas
blob: e1b57f501b2606b12f5343ea13fd78481bbae48a (plain)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
{
    fpGUI  -  Free Pascal GUI Library

    Copyright (C) 2006 - 2007 See the file AUTHORS.txt, included in this
    distribution, for details of the copyright.

    See the file COPYING.modifiedLGPL, included in this distribution,
    for details about redistributing fpGUI.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    Description:
      Main window functionality and designer class.
}

unit vfdwidgetclass;

{$mode objfpc}{$H+}

interface

uses
  Classes,
  SysUtils,
  gfxbase,
  fpgfx,
  gfx_widget;

type
  TWidgetClass = class of TfpgWidget;


  TVFDWidgetProperty = class;


  TVFDPropertyEditor = class(TfpgWidget)
  private
    FProp: TVFDWidgetProperty;
  public
    OnUpdate: TNotifyEvent;
    procedure   UpdateProperty(Sender: TObject);
    property    Prop: TVFDWidgetProperty read FProp;
    constructor Create(AOwner: TComponent; aprop: TVFDWidgetProperty); reintroduce;
    procedure   CreateLayout; virtual;
    procedure   LoadValue(wg: TfpgWidget); virtual;
    procedure   StoreValue(wg: TfpgWidget); virtual;
  end;


  TVFDWidgetProperty = class(TObject)
  public
    Name: string;
    Description: string;
  public
    constructor Create(aName: string); virtual;
    function    ParseSourceLine(wg: TfpgWidget; const line: string): boolean; virtual;
    function    GetPropertySource(wg: TfpgWidget; const ident: string): string; virtual;
    function    GetValueText(wg: TfpgWidget): string; virtual;
    procedure   DrawValue(wg: TfpgWidget; Canvas: TfpgCanvas; rect: TfpgRect; flags: integer); virtual;
    function    CreateEditor(AOwner: TComponent): TVFDPropertyEditor; virtual;
    procedure   OnExternalEdit(wg: TfpgWidget); virtual;
  end;


  TVFDPropertyClass = class of TVFDWidgetProperty;


  TVFDWidgetClass = class(TObject)
  private
    FProps: TList;
  public
    WidgetClass: TWidgetClass;
    Description: string;
    WidgetIconName: string;
    NameBase: string;
    Container: boolean;
    constructor Create(aClass: TWidgetClass);
    destructor  Destroy; override;
    function    AddProperty(apropname: string; apropclass: TVFDPropertyClass; desc: string): TVFDWidgetProperty;
    function    PropertyCount: integer;
    function    GetProperty(ind: integer): TVFDWidgetProperty;
    function    CreateWidget(AOwner: TComponent): TfpgWidget;
  end;


implementation

uses
  TypInfo;
  

type
  // used to get to SetDesigning() in Form Designer
  TComponentFriendClass = class(TComponent);


{ TVFDWidgetClass }

function TVFDWidgetClass.AddProperty(apropname: string; apropclass: TVFDPropertyClass;
  desc: string): TVFDWidgetProperty;
begin
  Result := apropclass.Create(apropname);
  Result.Description := desc;
  FProps.Add(Result);
end;

constructor TVFDWidgetClass.Create(aClass: TWidgetClass);
begin
  WidgetClass := aClass;
  FProps      := TList.Create;
  Description := '';
  NameBase    := 'Widget';
  Container   := False;
end;

function TVFDWidgetClass.CreateWidget(AOwner: TComponent): TfpgWidget;
begin
  Result := WidgetClass.Create(AOwner);
  TComponentFriendClass(Result).SetDesigning(True);
end;

destructor TVFDWidgetClass.Destroy;
var
  n: integer;
begin
  for n := 0 to FProps.Count - 1 do
    TVFDWidgetProperty(FProps[n]).Free;
  FProps.Free;
  inherited;
end;

function TVFDWidgetClass.GetProperty(ind: integer): TVFDWidgetProperty;
begin
  Result := TVFDWidgetProperty(FProps[ind - 1]);
end;

function TVFDWidgetClass.PropertyCount: integer;
begin
  Result := FProps.Count;
end;

{ TVFDWidgetProperty }

constructor TVFDWidgetProperty.Create(aName: string);
begin
  Name        := aName;
  Description := '';
end;

function TVFDWidgetProperty.GetPropertySource(wg: TfpgWidget; const ident: string): string;
begin

end;

function TVFDWidgetProperty.ParseSourceLine(wg: TfpgWidget; const line: string): boolean;
begin
  Result := False;
end;

function TVFDWidgetProperty.CreateEditor(AOwner: TComponent): TVFDPropertyEditor;
begin
  Result := nil;
end;

procedure TVFDWidgetProperty.DrawValue(wg: TfpgWidget; Canvas: TfpgCanvas; rect: TfpgRect; flags: integer);
var
  x, y, fy: integer;
  s: string;
begin
  x  := rect.left;
  y  := rect.top;
  fy := y + rect.Height div 2 - Canvas.Font.Height div 2;

  try
    s := GetValueText(wg);
  except
    on E: Exception do
      writeln('Detected a error: ', E.Message);
  end;
  
  Canvas.BeginDraw;
  Canvas.DrawString(x + 1, fy, s);
  Canvas.EndDraw;
end;

function TVFDWidgetProperty.GetValueText(wg: TfpgWidget): string;
begin
  Result := '[' + Name + ']';
end;

procedure TVFDWidgetProperty.OnExternalEdit(wg: TfpgWidget);
begin
  writeln('external edit');
end;

{ TVFDPropertyEditor }

constructor TVFDPropertyEditor.Create(AOwner: TComponent; aprop: TVFDWidgetProperty);
begin
  inherited Create(AOwner);
  OnUpdate := nil;
  FProp    := aprop;
end;

procedure TVFDPropertyEditor.CreateLayout;
begin
  //abstract
end;

procedure TVFDPropertyEditor.LoadValue(wg: TfpgWidget);
begin
  Writeln('abstract: editor.LoadValue');
end;

procedure TVFDPropertyEditor.UpdateProperty(Sender: TObject);
begin
  if Assigned(OnUpdate) then
    OnUpdate(self);
end;

procedure TVFDPropertyEditor.StoreValue(wg: TfpgWidget);
begin
  Writeln('abstract: editor.StoreValue');
  // check property type
  // the property must be published !
  // PPropInfo := GetPropInfo(object, 'propname');
  // if PPropInfo^.PropType^.name =
end;

end.