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 |
|
---|
31 | unit BCBaseCtrls;
|
---|
32 |
|
---|
33 | {$mode objfpc}{$H+}
|
---|
34 |
|
---|
35 | interface
|
---|
36 |
|
---|
37 | uses
|
---|
38 | Classes, SysUtils, Controls, BGRABitmap, BGRABitmapTypes;
|
---|
39 |
|
---|
40 | type
|
---|
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 |
|
---|
193 | procedure Register;
|
---|
194 |
|
---|
195 | implementation
|
---|
196 |
|
---|
197 | {$IFDEF DEBUG} uses Graphics; {$ENDIF}
|
---|
198 |
|
---|
199 | procedure Register;
|
---|
200 | begin
|
---|
201 | RegisterNoIcon([TBCCustomControl]);
|
---|
202 | end;
|
---|
203 |
|
---|
204 | { TBCStyleCustomControl }
|
---|
205 |
|
---|
206 | constructor TBCStyleCustomControl.Create(AOwner: TComponent);
|
---|
207 | begin
|
---|
208 | inherited Create(AOwner);
|
---|
209 |
|
---|
210 | FAssignStyle := TBCStyleDummyProperty.Create(Self);
|
---|
211 | end;
|
---|
212 |
|
---|
213 | destructor TBCStyleCustomControl.Destroy;
|
---|
214 | begin
|
---|
215 | FAssignStyle.Free;
|
---|
216 | inherited Destroy;
|
---|
217 | end;
|
---|
218 |
|
---|
219 | { TBCStyleGraphicControl }
|
---|
220 |
|
---|
221 | constructor TBCStyleGraphicControl.Create(AOwner: TComponent);
|
---|
222 | begin
|
---|
223 | inherited Create(AOwner);
|
---|
224 |
|
---|
225 | FAssignStyle := TBCStyleDummyProperty.Create(Self);
|
---|
226 | end;
|
---|
227 |
|
---|
228 | destructor TBCStyleGraphicControl.Destroy;
|
---|
229 | begin
|
---|
230 | FAssignStyle.Free;
|
---|
231 | inherited Destroy;
|
---|
232 | end;
|
---|
233 |
|
---|
234 | { TBCCustomControl }
|
---|
235 |
|
---|
236 | procedure TBCCustomControl.DoOnResize;
|
---|
237 | begin
|
---|
238 | inherited DoOnResize;
|
---|
239 | RenderControl;
|
---|
240 | end;
|
---|
241 |
|
---|
242 | {$IFDEF DEBUG}
|
---|
243 | function TBCCustomControl.GetDebugText: String;
|
---|
244 | begin
|
---|
245 | Result := EmptyStr;
|
---|
246 | end;
|
---|
247 | {$ENDIF}
|
---|
248 |
|
---|
249 | procedure TBCCustomControl.Paint;
|
---|
250 | begin
|
---|
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;
|
---|
263 | end;
|
---|
264 |
|
---|
265 | procedure TBCCustomControl.DrawControl;
|
---|
266 | begin
|
---|
267 |
|
---|
268 | end;
|
---|
269 |
|
---|
270 | procedure TBCCustomControl.RenderControl;
|
---|
271 | begin
|
---|
272 |
|
---|
273 | end;
|
---|
274 |
|
---|
275 | constructor TBCCustomControl.Create(AOwner: TComponent);
|
---|
276 | begin
|
---|
277 | inherited Create(AOwner);
|
---|
278 | {$IFDEF DEBUG}
|
---|
279 | FPaintCount := 0;
|
---|
280 | {$ENDIF}
|
---|
281 | end;
|
---|
282 |
|
---|
283 | procedure TBCCustomControl.BeginUpdate;
|
---|
284 | begin
|
---|
285 | FUpdateCount += 1;
|
---|
286 | end;
|
---|
287 |
|
---|
288 | procedure TBCCustomControl.EndUpdate;
|
---|
289 | begin
|
---|
290 | if FUpdateCount > 0 then
|
---|
291 | begin
|
---|
292 | FUpdateCount -= 1;
|
---|
293 | if FUpdateCount=0 then
|
---|
294 | UpdateControl;
|
---|
295 | end;
|
---|
296 | end;
|
---|
297 |
|
---|
298 | procedure TBCCustomControl.UpdateControl;
|
---|
299 | begin
|
---|
300 | Self.Invalidate;
|
---|
301 | end;
|
---|
302 |
|
---|
303 | function TBCCustomControl.IsUpdating: Boolean;
|
---|
304 | begin
|
---|
305 | Result := FUpdateCount>0;
|
---|
306 | end;
|
---|
307 |
|
---|
308 | { TBCProperty }
|
---|
309 |
|
---|
310 | procedure TBCProperty.Change(AData: PtrInt);
|
---|
311 | begin
|
---|
312 | if Assigned(FOnChange) then
|
---|
313 | FOnChange(Self,AData);
|
---|
314 | end;
|
---|
315 |
|
---|
316 | constructor TBCProperty.Create(AControl: TControl);
|
---|
317 | begin
|
---|
318 | FControl := AControl;
|
---|
319 |
|
---|
320 | inherited Create;
|
---|
321 | end;
|
---|
322 |
|
---|
323 | { TBCGraphicControl }
|
---|
324 |
|
---|
325 | procedure TBCGraphicControl.DoOnResize;
|
---|
326 | begin
|
---|
327 | inherited DoOnResize;
|
---|
328 | RenderControl;
|
---|
329 | end;
|
---|
330 |
|
---|
331 | {$IFDEF DEBUG}
|
---|
332 | function TBCGraphicControl.GetDebugText: String;
|
---|
333 | begin
|
---|
334 | Result := EmptyStr;
|
---|
335 | end;
|
---|
336 | {$ENDIF}
|
---|
337 |
|
---|
338 | procedure TBCGraphicControl.Paint;
|
---|
339 | begin
|
---|
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}
|
---|
351 | end;
|
---|
352 |
|
---|
353 | procedure TBCGraphicControl.DrawControl;
|
---|
354 | begin
|
---|
355 |
|
---|
356 | end;
|
---|
357 |
|
---|
358 | procedure TBCGraphicControl.RenderControl;
|
---|
359 | begin
|
---|
360 |
|
---|
361 | end;
|
---|
362 |
|
---|
363 | constructor TBCGraphicControl.Create(AOwner: TComponent);
|
---|
364 | begin
|
---|
365 | inherited Create(AOwner);
|
---|
366 | {$IFDEF DEBUG}
|
---|
367 | FPaintCount := 0;
|
---|
368 | {$ENDIF}
|
---|
369 | end;
|
---|
370 |
|
---|
371 | procedure TBCGraphicControl.BeginUpdate;
|
---|
372 | begin
|
---|
373 | FUpdateCount += 1;
|
---|
374 | end;
|
---|
375 |
|
---|
376 | procedure TBCGraphicControl.EndUpdate;
|
---|
377 | begin
|
---|
378 | if FUpdateCount > 0 then
|
---|
379 | begin
|
---|
380 | FUpdateCount -= 1;
|
---|
381 | if FUpdateCount=0 then
|
---|
382 | UpdateControl;
|
---|
383 | end;
|
---|
384 | end;
|
---|
385 |
|
---|
386 | procedure TBCGraphicControl.UpdateControl;
|
---|
387 | begin
|
---|
388 | Invalidate;
|
---|
389 | end;
|
---|
390 |
|
---|
391 | function TBCGraphicControl.IsUpdating: Boolean;
|
---|
392 | begin
|
---|
393 | Result := FUpdateCount>0;
|
---|
394 | end;
|
---|
395 |
|
---|
396 | { TBGRABitmapEx }
|
---|
397 |
|
---|
398 | procedure TBGRABitmapEx.Init;
|
---|
399 | begin
|
---|
400 | inherited Init;
|
---|
401 | FNeedRender := True;
|
---|
402 | FCustomData := 0;
|
---|
403 | end;
|
---|
404 |
|
---|
405 | end.
|
---|
406 |
|
---|