source: trunk/Packages/bgracontrols/bcbasectrls.pas

Last change on this file was 2, checked in by chronos, 5 years ago
File size: 9.8 KB
Line 
1{ Base framework classes
2
3 Copyright (C) 2012 Krzysztof Dibowski dibowski at interia.pl
4
5 This library is free software; you can redistribute it and/or modify it
6 under the terms of the GNU Library General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or (at your
8 option) any later version with the following modification:
9
10 As a special exception, the copyright holders of this library give you
11 permission to link this library with independent modules to produce an
12 executable, regardless of the license terms of these independent modules,and
13 to copy and distribute the resulting executable under terms of your choice,
14 provided that you also meet, for each linked independent module, the terms
15 and conditions of the license of that module. An independent module is a
16 module which is not derived from or based on this library. If you modify
17 this library, you may extend this exception to your version of the library,
18 but you are not obligated to do so. If you do not wish to do so, delete this
19 exception statement from your version.
20
21 This program is distributed in the hope that it will be useful, but WITHOUT
22 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
24 for more details.
25
26 You should have received a copy of the GNU Library General Public License
27 along with this library; if not, write to the Free Software Foundation,
28 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29}
30
31unit BCBaseCtrls;
32
33{$mode objfpc}{$H+}
34
35interface
36
37uses
38 Classes, SysUtils, Controls, BGRABitmap, BGRABitmapTypes;
39
40type
41
42 TOnBCPropertyChange = procedure(ASender: TObject; AData: PtrInt) of object;
43
44 { TBCProperty
45 Base BC Property with OnChange event support
46 }
47
48 TBCProperty = class(TPersistent)
49 private
50 FOnChange: TOnBCPropertyChange;
51 protected
52 FControl: TControl;
53 procedure Change(AData: PtrInt = 0); virtual;
54 public
55 constructor Create(AControl: TControl); virtual;
56 public
57 property Control: TControl read FControl;
58 property OnChange: TOnBCPropertyChange read FOnChange write FOnChange;
59 end;
60
61 { TBGRABitmapEx
62 Some BGRABitmap descendant which can store custom data and has NeedRender flag
63 }
64
65 TBGRABitmapEx = class(TBGRABitmap)
66 private
67 FCustomData: PtrInt;
68 FNeedRender: Boolean;
69 protected
70 procedure Init; override;
71 public
72 property NeedRender: Boolean read FNeedRender write FNeedRender;
73 property CustomData: PtrInt read FCustomData write FCustomData;
74 end;
75
76 { TBCGraphicControl
77 BC graphic control with some basic functionality like begin/end update and
78 debug functions
79 }
80
81 TBCGraphicControl = class(TGraphicControl)
82 private
83 {$IFDEF DEBUG}
84 FPaintCount: Integer;
85 {$ENDIF}
86 FUpdateCount: Integer;
87 protected
88 procedure DoOnResize; override;
89 protected
90 {$IFDEF DEBUG}
91 function GetDebugText: String; virtual;
92 {$ENDIF}
93 procedure Paint; override; // do not override in descendants!
94 // All descendants should use DrawControl method instead of Paint.
95 // DrawControl is not called between BeginUpdate and EndUpdate
96 procedure DrawControl; virtual;
97 // This method is called when control should be rendered (when some
98 // general action occur which change "body" e.g. resize)
99 procedure RenderControl; virtual;
100 public
101 constructor Create(AOwner: TComponent); override;
102 // This disable DrawControl method
103 procedure BeginUpdate; virtual;
104 // This enable DrawControl method
105 procedure EndUpdate; virtual;
106 // Called on EndUpdate if FUpdateCount is 0
107 procedure UpdateControl; virtual;
108 // Check if BeginUpdate was called
109 function IsUpdating: Boolean;
110 end;
111
112 { TBCStyleDummyProperty
113 This is only dummy property type for access to style editor from
114 object inspector
115 }
116
117 TBCStyleDummyProperty = class(TBCProperty)
118
119 end;
120
121 { TBCStyleGraphicControl
122 All descendants of this class have support for saving and loading styles and
123 access to style editor from object inspector or component context menu
124 }
125
126 TBCStyleGraphicControl = class(TBCGraphicControl)
127 private
128 FAssignStyle: TBCStyleDummyProperty;
129 protected
130 function GetStyleExtension: String; virtual; abstract;
131 // Dummy property for access to style editor dialog
132 property AssignStyle: TBCStyleDummyProperty read FAssignStyle;
133 public
134 constructor Create(AOwner: TComponent); override;
135 destructor Destroy; override;
136 property StyleExtension: String read GetStyleExtension;
137 end;
138
139 { TBCCustomControl
140 BC custom control with some basic functionality like begin/end update and
141 debug functions
142 }
143
144 TBCCustomControl = class(TCustomControl)
145 private
146 {$IFDEF DEBUG}
147 FPaintCount: Integer;
148 {$ENDIF}
149 FUpdateCount: Integer;
150 protected
151 procedure DoOnResize; override;
152 protected
153 {$IFDEF DEBUG}
154 function GetDebugText: String; virtual;
155 {$ENDIF}
156 procedure Paint; override; // do not override in descendants!
157 // All descendants should use DrawControl method instead of Paint.
158 // DrawControl is not called between BeginUpdate and EndUpdate
159 procedure DrawControl; virtual;
160 // This method is called when control should be rendered (when some
161 // general action occur which change "body" e.g. resize)
162 procedure RenderControl; virtual;
163 public
164 constructor Create(AOwner: TComponent); override;
165 // This disable DrawControl method
166 procedure BeginUpdate; virtual;
167 // This enable DrawControl method
168 procedure EndUpdate; virtual;
169 // Called on EndUpdate if FUpdateCount is 0
170 procedure UpdateControl; virtual;
171 // Check if BeginUpdate was called
172 function IsUpdating: Boolean;
173 end;
174
175 { TBCStyleCustomControl
176 All descendants of this class have support for saving and loading styles and
177 access to style editor from object inspector or component context menu
178 }
179
180 TBCStyleCustomControl = class(TBCCustomControl)
181 private
182 FAssignStyle: TBCStyleDummyProperty;
183 protected
184 function GetStyleExtension: String; virtual; abstract;
185 // Dummy property for access to style editor dialog
186 property AssignStyle: TBCStyleDummyProperty read FAssignStyle;
187 public
188 constructor Create(AOwner: TComponent); override;
189 destructor Destroy; override;
190 property StyleExtension: String read GetStyleExtension;
191 end;
192
193procedure Register;
194
195implementation
196
197{$IFDEF DEBUG} uses Graphics; {$ENDIF}
198
199procedure Register;
200begin
201 RegisterNoIcon([TBCCustomControl]);
202end;
203
204{ TBCStyleCustomControl }
205
206constructor TBCStyleCustomControl.Create(AOwner: TComponent);
207begin
208 inherited Create(AOwner);
209
210 FAssignStyle := TBCStyleDummyProperty.Create(Self);
211end;
212
213destructor TBCStyleCustomControl.Destroy;
214begin
215 FAssignStyle.Free;
216 inherited Destroy;
217end;
218
219{ TBCStyleGraphicControl }
220
221constructor TBCStyleGraphicControl.Create(AOwner: TComponent);
222begin
223 inherited Create(AOwner);
224
225 FAssignStyle := TBCStyleDummyProperty.Create(Self);
226end;
227
228destructor TBCStyleGraphicControl.Destroy;
229begin
230 FAssignStyle.Free;
231 inherited Destroy;
232end;
233
234{ TBCCustomControl }
235
236procedure TBCCustomControl.DoOnResize;
237begin
238 inherited DoOnResize;
239 RenderControl;
240end;
241
242{$IFDEF DEBUG}
243function TBCCustomControl.GetDebugText: String;
244begin
245 Result := EmptyStr;
246end;
247{$ENDIF}
248
249procedure TBCCustomControl.Paint;
250begin
251 if (csCreating in FControlState) or IsUpdating then
252 Exit;
253
254 DrawControl;
255 {$IFDEF DEBUG}
256 FPaintCount += 1;
257 Canvas.Brush.Style := bsSolid;
258 Canvas.Brush.Color := clBlack;
259 Canvas.Font.Color := clWhite;
260 Canvas.TextOut(1,1,'P: '+IntToStr(FPaintCount)+' '+GetDebugText);
261 {$ENDIF}
262 inherited Paint;
263end;
264
265procedure TBCCustomControl.DrawControl;
266begin
267
268end;
269
270procedure TBCCustomControl.RenderControl;
271begin
272
273end;
274
275constructor TBCCustomControl.Create(AOwner: TComponent);
276begin
277 inherited Create(AOwner);
278 {$IFDEF DEBUG}
279 FPaintCount := 0;
280 {$ENDIF}
281end;
282
283procedure TBCCustomControl.BeginUpdate;
284begin
285 FUpdateCount += 1;
286end;
287
288procedure TBCCustomControl.EndUpdate;
289begin
290 if FUpdateCount > 0 then
291 begin
292 FUpdateCount -= 1;
293 if FUpdateCount=0 then
294 UpdateControl;
295 end;
296end;
297
298procedure TBCCustomControl.UpdateControl;
299begin
300 Self.Invalidate;
301end;
302
303function TBCCustomControl.IsUpdating: Boolean;
304begin
305 Result := FUpdateCount>0;
306end;
307
308{ TBCProperty }
309
310procedure TBCProperty.Change(AData: PtrInt);
311begin
312 if Assigned(FOnChange) then
313 FOnChange(Self,AData);
314end;
315
316constructor TBCProperty.Create(AControl: TControl);
317begin
318 FControl := AControl;
319
320 inherited Create;
321end;
322
323{ TBCGraphicControl }
324
325procedure TBCGraphicControl.DoOnResize;
326begin
327 inherited DoOnResize;
328 RenderControl;
329end;
330
331{$IFDEF DEBUG}
332function TBCGraphicControl.GetDebugText: String;
333begin
334 Result := EmptyStr;
335end;
336{$ENDIF}
337
338procedure TBCGraphicControl.Paint;
339begin
340 //inherited Paint;
341 if (csCreating in FControlState) or IsUpdating then
342 Exit;
343 DrawControl;
344 {$IFDEF DEBUG}
345 FPaintCount += 1;
346 Canvas.Brush.Style := bsSolid;
347 Canvas.Brush.Color := clBlack;
348 Canvas.Font.Color := clWhite;
349 Canvas.TextOut(1,1,'P: '+IntToStr(FPaintCount)+' '+GetDebugText);
350 {$ENDIF}
351end;
352
353procedure TBCGraphicControl.DrawControl;
354begin
355
356end;
357
358procedure TBCGraphicControl.RenderControl;
359begin
360
361end;
362
363constructor TBCGraphicControl.Create(AOwner: TComponent);
364begin
365 inherited Create(AOwner);
366 {$IFDEF DEBUG}
367 FPaintCount := 0;
368 {$ENDIF}
369end;
370
371procedure TBCGraphicControl.BeginUpdate;
372begin
373 FUpdateCount += 1;
374end;
375
376procedure TBCGraphicControl.EndUpdate;
377begin
378 if FUpdateCount > 0 then
379 begin
380 FUpdateCount -= 1;
381 if FUpdateCount=0 then
382 UpdateControl;
383 end;
384end;
385
386procedure TBCGraphicControl.UpdateControl;
387begin
388 Invalidate;
389end;
390
391function TBCGraphicControl.IsUpdating: Boolean;
392begin
393 Result := FUpdateCount>0;
394end;
395
396{ TBGRABitmapEx }
397
398procedure TBGRABitmapEx.Init;
399begin
400 inherited Init;
401 FNeedRender := True;
402 FCustomData := 0;
403end;
404
405end.
406
Note: See TracBrowser for help on using the repository browser.