source: branches/Xvcl/Xvcl.Controls.pas

Last change on this file was 24, checked in by chronos, 12 years ago
  • Fixed: Make only one form focused at once.
  • Modified: Enhanced TList generic class to support more methods.
File size: 7.3 KB
Line 
1unit Xvcl.Controls;
2
3interface
4
5uses
6 Xvcl.Generics, Xvcl.Classes, Xvcl.Graphics;
7
8type
9 TControl = class;
10 TWinControl = class;
11
12 TMessage = class
13 Number: Integer;
14 Handled: Boolean;
15 end;
16
17 TMouseButton = (mbLeft, mbRight, mbMiddle);
18 TMouseButtonSet = set of TMouseButton;
19 TMessageMouse = class(TMessage)
20 Position: TPoint;
21 Buttons: TMouseButtonSet;
22 end;
23 TMessageMouseDown = class(TMessageMouse);
24 TMessageMouseUp = class(TMessageMouse);
25 TMessageMouseMove = class(TMessageMouse);
26
27 TKeyState = (ksShift, ksAlt, ksOS);
28 TKeyStateSet = set of TKeyState;
29 TMessageKeyboard = class(TMessage)
30 KeyCode: Integer;
31 States: TKeyStateSet;
32 end;
33
34 TControlCanvas = class(TCanvas)
35 protected
36 function GetVideoDevice: TVideoDevice; override;
37 public
38 Control: TControl;
39 function AdjustPos(Position: TPoint): TPoint; override;
40 end;
41
42 TControlMove = class
43 Control: TControl;
44 StartControlPos: TPoint;
45 StartMousePos: TPoint;
46 Active: Boolean;
47 end;
48
49 TControl = class(TComponent)
50 private
51 FCanvas: TControlCanvas;
52 FParent: TWinControl;
53 FVisible: Boolean;
54 FBounds: TRectangle;
55 FOnClick: TNotifyEvent;
56 FColor: TColor;
57 FOnMouseDown: TNotifyEvent;
58 FOnMouseUp: TNotifyEvent;
59 function GetCanvas: TCanvas;
60 procedure SetParent(const Value: TWinControl); virtual;
61 procedure SetColor(const Value: TColor);
62 protected
63 function GetVideoDevice: TVideoDevice; virtual;
64 function HandleMessage(Message: TMessage): Boolean; virtual;
65 public
66 Move: TControlMove;
67 function ClientToScreen(Position: TPoint): TPoint; virtual;
68 function ScreenToClient(Position: TPoint): TPoint; virtual;
69 procedure Paint; virtual;
70 constructor Create; override;
71 destructor Destroy; override;
72 property Bounds: TRectangle read FBounds write FBounds;
73 property Visible: Boolean read FVisible write FVisible;
74 property Canvas: TCanvas read GetCanvas;
75 property Parent: TWinControl read FParent write SetParent;
76 property VideoDevice: TVideoDevice read GetVideoDevice;
77 property Color: TColor read FColor write SetColor;
78 property OnClick: TNotifyEvent read FOnClick write FOnClick;
79 property OnMouseDown: TNotifyEvent read FOnMouseDown write FOnMouseDown;
80 property OnMouseUp: TNotifyEvent read FOnMouseUp write FOnMouseUp;
81 end;
82
83 TWinControl = class(TControl)
84 protected
85 function HandleMessage(Message: TMessage): Boolean; override;
86 public
87 Controls: TList<TControl>;
88 procedure Paint; override;
89 constructor Create; override;
90 destructor Destroy; override;
91 end;
92
93 TButton = class(TControl)
94 private
95 FCaption: string;
96 procedure SetCaption(const Value: string);
97 public
98 procedure Paint; override;
99 constructor Create; override;
100 property Caption: string read FCaption write SetCaption;
101 end;
102
103 TLabel = class(TControl)
104 private
105 FCaption: string;
106 procedure SetCaption(const Value: string);
107 public
108 procedure Paint; override;
109 property Caption: string read FCaption write SetCaption;
110 end;
111
112
113implementation
114
115{ TControl }
116
117function TControl.ClientToScreen(Position: TPoint): TPoint;
118begin
119 Result := Position.Add(Bounds.TopLeft);
120 if Assigned(Parent) then Result := Parent.ClientToScreen(Result);
121end;
122
123constructor TControl.Create;
124begin
125 inherited;
126 Move := TControlMove.Create;
127 FColor := clWhite;
128end;
129
130destructor TControl.Destroy;
131begin
132 Move.Destroy;
133 if Assigned(FCanvas) then FCanvas.Destroy;
134 inherited;
135end;
136
137function TControl.GetCanvas: TCanvas;
138begin
139 if not Assigned(FCanvas) then begin
140 FCanvas := TControlCanvas.Create;
141 FCanvas.Control := Self;
142 end;
143 Result := FCanvas;
144end;
145
146function TControl.GetVideoDevice: TVideoDevice;
147begin
148 if Assigned(Parent) then Result := Parent.VideoDevice
149 else Result := nil;
150end;
151
152function TControl.HandleMessage(Message: TMessage): Boolean;
153begin
154 Result := False;
155 if Message is TMessageMouseDown then begin
156 if Assigned(FOnMouseDown) then FOnMouseDown(Self);
157 Color := clGray;
158 Paint;
159 Result := True;
160 end else
161 if Message is TMessageMouseUp then begin
162 if Assigned(FOnMouseUp) then FOnMouseUp(Self);
163 Color := clSilver;
164 Paint;
165 if Assigned(FOnClick) then FOnClick(Self);
166 Result := True;
167 end;
168end;
169
170procedure TControl.Paint;
171begin
172 with Canvas do begin
173 Brush.Color := Color;
174 FillRect(TRectangle.Create(0, 0, Bounds.Width, Bounds.Height));
175 end;
176end;
177
178function TControl.ScreenToClient(Position: TPoint): TPoint;
179begin
180 Result := Position.Substract(Bounds.TopLeft);
181 if Assigned(Parent) then Result := Parent.ClientToScreen(Result);
182end;
183
184procedure TControl.SetColor(const Value: TColor);
185begin
186 if FColor <> Value then begin
187 FColor := Value;
188 Paint;
189 end;
190end;
191
192procedure TControl.SetParent(const Value: TWinControl);
193begin
194 if FParent <> Value then begin
195 if Assigned(FParent) then Parent.Controls.Remove(Self);
196 FParent := Value;
197 if Assigned(FParent) then Parent.Controls.Add(Self);
198 end;
199end;
200
201{ TButton }
202
203constructor TButton.Create;
204begin
205 inherited;
206 FColor := clSilver;
207end;
208
209procedure TButton.Paint;
210begin
211 inherited;
212 with Canvas do begin
213 MoveTo(TPoint.Create(0, 0));
214 LineTo(TPoint.Create(Bounds.Width - 1, 0));
215 LineTo(TPoint.Create(Bounds.Width - 1, Bounds.Height - 1));
216 LineTo(TPoint.Create(0, Bounds.Height - 1));
217 LineTo(TPoint.Create(0, 0));
218 TextOut(TPoint.Create((Bounds.Width - GetTextSize(Caption).X) div 2,
219 (Bounds.Height - GetTextSize(Caption).Y) div 2), Caption);
220 end;
221end;
222
223procedure TButton.SetCaption(const Value: string);
224begin
225 if FCaption <> Value then begin
226 FCaption := Value;
227 Paint;
228 end;
229end;
230
231{ TControlCanvas }
232
233function TControlCanvas.AdjustPos(Position: TPoint): TPoint;
234begin
235 if Assigned(Control) then
236 Result := Control.ClientToScreen(Position)
237 else Result := inherited;
238end;
239
240function TControlCanvas.GetVideoDevice: TVideoDevice;
241begin
242 if Assigned(Control) then Result := Control.VideoDevice
243 else Result := nil;
244end;
245
246{ TWinControl }
247
248constructor TWinControl.Create;
249begin
250 inherited;
251 Controls := TList<TControl>.Create;
252end;
253
254destructor TWinControl.Destroy;
255begin
256 Controls.Destroy;
257 inherited;
258end;
259
260function TWinControl.HandleMessage(Message: TMessage): Boolean;
261var
262 Control: TControl;
263begin
264 Result := False;
265 for Control in Controls do begin
266 if Message is TMessageMouse then
267 with TMessageMouse(Message) do begin
268 if Control.Bounds.Contains(ScreenToClient(Position)) then begin
269 if TWinControl(Control).HandleMessage(Message) then begin
270 Result := True;
271 Break;
272 end;
273 end;
274 end;
275 end;
276end;
277
278procedure TWinControl.Paint;
279var
280 C: TControl;
281begin
282 inherited;
283 for C in Controls do C.Paint;
284end;
285
286{ TLabel }
287
288procedure TLabel.Paint;
289begin
290 inherited;
291 with Canvas do begin
292 TextOut(TPoint.Create((Bounds.Width - GetTextSize(Caption).X) div 2,
293 (Bounds.Height - GetTextSize(Caption).Y) div 2), Caption);
294 end;
295end;
296
297procedure TLabel.SetCaption(const Value: string);
298begin
299 if FCaption <> Value then begin
300 FCaption := Value;
301 Paint;
302 end;
303end;
304
305end.
Note: See TracBrowser for help on using the repository browser.