1 | { Equivalent of standard lazarus TPanel but using BGRA Controls framework for render
|
---|
2 |
|
---|
3 | Functionality:
|
---|
4 | - Customizable background (gradient etc.)
|
---|
5 | - Customizable border (frame 3D or normal border, rounding etc)
|
---|
6 | - FontEx (shadow etc.)
|
---|
7 |
|
---|
8 | Copyright (C) 2011 Krzysztof Dibowski dibowski at interia.pl
|
---|
9 |
|
---|
10 | This library is free software; you can redistribute it and/or modify it
|
---|
11 | under the terms of the GNU Library General Public License as published by
|
---|
12 | the Free Software Foundation; either version 2 of the License, or (at your
|
---|
13 | option) any later version with the following modification:
|
---|
14 |
|
---|
15 | As a special exception, the copyright holders of this library give you
|
---|
16 | permission to link this library with independent modules to produce an
|
---|
17 | executable, regardless of the license terms of these independent modules,and
|
---|
18 | to copy and distribute the resulting executable under terms of your choice,
|
---|
19 | provided that you also meet, for each linked independent module, the terms
|
---|
20 | and conditions of the license of that module. An independent module is a
|
---|
21 | module which is not derived from or based on this library. If you modify
|
---|
22 | this library, you may extend this exception to your version of the library,
|
---|
23 | but you are not obligated to do so. If you do not wish to do so, delete this
|
---|
24 | exception statement from your version.
|
---|
25 |
|
---|
26 | This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
27 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
---|
28 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
|
---|
29 | for more details.
|
---|
30 |
|
---|
31 | You should have received a copy of the GNU Library General Public License
|
---|
32 | along with this library; if not, write to the Free Software Foundation,
|
---|
33 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
---|
34 | }
|
---|
35 | unit BCPanel;
|
---|
36 |
|
---|
37 | {$mode objfpc}{$H+}
|
---|
38 |
|
---|
39 | interface
|
---|
40 |
|
---|
41 | uses
|
---|
42 | Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs,
|
---|
43 | BGRABitmap, BCBaseCtrls, BGRABitmapTypes, BCTypes, Types;
|
---|
44 |
|
---|
45 | type
|
---|
46 | TOnAfterRenderBCPanel = procedure(Sender: TObject; const ABGRA: TBGRABitmap;
|
---|
47 | ARect: TRect) of object;
|
---|
48 | TBCPanelBorderStyle = (bpsBorder, bpsFrame3d);
|
---|
49 |
|
---|
50 | { TCustomBCPanel }
|
---|
51 |
|
---|
52 | TCustomBCPanel = class(TBCStyleCustomControl)
|
---|
53 | private
|
---|
54 | { Private declarations }
|
---|
55 | {$IFDEF DEBUG}
|
---|
56 | FRenderCount: Integer;
|
---|
57 | {$ENDIF}
|
---|
58 | FBackground: TBCBackground;
|
---|
59 | FBevelWidth: Integer;
|
---|
60 | FBGRA: TBGRABitmapEx;
|
---|
61 | FBevelInner, FBevelOuter : TBevelCut;
|
---|
62 | FBorder: TBCBorder;
|
---|
63 | FBorderBCStyle: TBCPanelBorderStyle;
|
---|
64 | FFontEx: TBCFont;
|
---|
65 | FOnAfterRenderBCPanel: TOnAfterRenderBCPanel;
|
---|
66 | FRounding: TBCRounding;
|
---|
67 | procedure SetBackground(AValue: TBCBackground);
|
---|
68 | procedure SetBevelInner(AValue: TBevelCut);
|
---|
69 | procedure SetBevelOuter(AValue: TBevelCut);
|
---|
70 | procedure SetBevelWidth(AValue: Integer);
|
---|
71 | procedure SetBorder(AValue: TBCBorder);
|
---|
72 | procedure SetBorderBCStyle(AValue: TBCPanelBorderStyle);
|
---|
73 | procedure SetFontEx(AValue: TBCFont);
|
---|
74 | procedure SetRounding(AValue: TBCRounding);
|
---|
75 | procedure Render;
|
---|
76 | procedure OnChangeProperty(Sender: TObject; AData: PtrInt);
|
---|
77 | procedure OnChangeFont(Sender: TObject; AData: PtrInt);
|
---|
78 | protected
|
---|
79 | { Protected declarations }
|
---|
80 | procedure AdjustClientRect(var aRect: TRect); override;
|
---|
81 | class function GetControlClassDefaultSize: TSize; override;
|
---|
82 | function GetDefaultDockCaption: String; override;
|
---|
83 | procedure SetEnabled(Value: boolean); override;
|
---|
84 | procedure TextChanged; override;
|
---|
85 | protected
|
---|
86 | function GetStyleExtension: String; override;
|
---|
87 | {$IFDEF DEBUG}
|
---|
88 | function GetDebugText: String; override;
|
---|
89 | {$ENDIF}
|
---|
90 | procedure DrawControl; override;
|
---|
91 | procedure RenderControl; override;
|
---|
92 | protected
|
---|
93 | property Background: TBCBackground read FBackground write SetBackground;
|
---|
94 | property BevelInner: TBevelCut read FBevelInner write SetBevelInner;
|
---|
95 | property BevelOuter: TBevelCut read FBevelOuter write SetBevelOuter;
|
---|
96 | property BevelWidth: Integer read FBevelWidth write SetBevelWidth;
|
---|
97 | property Border: TBCBorder read FBorder write SetBorder;
|
---|
98 | property BorderBCStyle: TBCPanelBorderStyle
|
---|
99 | read FBorderBCStyle write SetBorderBCStyle default bpsFrame3d;
|
---|
100 | property FontEx: TBCFont read FFontEx write SetFontEx;
|
---|
101 | property Rounding: TBCRounding read FRounding write SetRounding;
|
---|
102 | protected
|
---|
103 | { Events }
|
---|
104 | property OnAfterRenderBCPanel: TOnAfterRenderBCPanel
|
---|
105 | Read FOnAfterRenderBCPanel Write FOnAfterRenderBCPanel;
|
---|
106 | public
|
---|
107 | { Public declarations }
|
---|
108 | constructor Create(TheOwner: TComponent); override;
|
---|
109 | destructor Destroy; override;
|
---|
110 | procedure UpdateControl; override; // Called by EndUpdate
|
---|
111 | end;
|
---|
112 |
|
---|
113 | { TBCPanel }
|
---|
114 |
|
---|
115 | TBCPanel = class(TCustomBCPanel)
|
---|
116 | published
|
---|
117 | property Align;
|
---|
118 | property Anchors;
|
---|
119 | property AssignStyle;
|
---|
120 | property AutoSize;
|
---|
121 | property BorderSpacing;
|
---|
122 | property Background;
|
---|
123 | property BevelInner;
|
---|
124 | property BevelOuter;
|
---|
125 | property BevelWidth;
|
---|
126 | property Border;
|
---|
127 | property BorderBCStyle;
|
---|
128 | property Caption;
|
---|
129 | property ChildSizing;
|
---|
130 | property Constraints;
|
---|
131 | property DockSite;
|
---|
132 | property DragCursor;
|
---|
133 | property DragKind;
|
---|
134 | property DragMode;
|
---|
135 | property Enabled;
|
---|
136 | property FontEx;
|
---|
137 | property PopupMenu;
|
---|
138 | property Rounding;
|
---|
139 | property ShowHint;
|
---|
140 | property TabOrder;
|
---|
141 | property TabStop;
|
---|
142 | property UseDockManager default True;
|
---|
143 | property Visible;
|
---|
144 | property OnClick;
|
---|
145 | property OnContextPopup;
|
---|
146 | property OnDockDrop;
|
---|
147 | property OnDockOver;
|
---|
148 | property OnDblClick;
|
---|
149 | property OnDragDrop;
|
---|
150 | property OnDragOver;
|
---|
151 | property OnEndDock;
|
---|
152 | property OnEndDrag;
|
---|
153 | property OnEnter;
|
---|
154 | property OnExit;
|
---|
155 | property OnGetSiteInfo;
|
---|
156 | property OnGetDockCaption;
|
---|
157 | property OnMouseDown;
|
---|
158 | property OnMouseEnter;
|
---|
159 | property OnMouseLeave;
|
---|
160 | property OnMouseMove;
|
---|
161 | property OnMouseUp;
|
---|
162 | property OnResize;
|
---|
163 | property OnStartDock;
|
---|
164 | property OnStartDrag;
|
---|
165 | property OnUnDock;
|
---|
166 | property OnAfterRenderBCPanel;
|
---|
167 | end;
|
---|
168 |
|
---|
169 | procedure Register;
|
---|
170 |
|
---|
171 | implementation
|
---|
172 |
|
---|
173 | uses BCTools;
|
---|
174 |
|
---|
175 | procedure Register;
|
---|
176 | begin
|
---|
177 | {$I bcpanel_icon.lrs}
|
---|
178 | RegisterComponents('BGRA Controls', [TBCPanel]);
|
---|
179 | end;
|
---|
180 |
|
---|
181 | { TCustomBCPanel }
|
---|
182 |
|
---|
183 | procedure TCustomBCPanel.DrawControl;
|
---|
184 | begin
|
---|
185 | inherited DrawControl;
|
---|
186 | if FBGRA.NeedRender then
|
---|
187 | Render;
|
---|
188 | if Assigned (FRounding) then
|
---|
189 | begin
|
---|
190 | if (FRounding.RoundX<>0) and (FRounding.RoundY<>0) then
|
---|
191 | FBGRA.Draw(Self.Canvas, 0, 0, False)
|
---|
192 | else
|
---|
193 | FBGRA.Draw(Self.Canvas, 0, 0);
|
---|
194 | end
|
---|
195 | else
|
---|
196 | FBGRA.Draw(Self.Canvas, 0, 0);
|
---|
197 | end;
|
---|
198 |
|
---|
199 | procedure TCustomBCPanel.RenderControl;
|
---|
200 | begin
|
---|
201 | inherited RenderControl;
|
---|
202 | if FBGRA<>nil then
|
---|
203 | FBGRA.NeedRender := True;
|
---|
204 | end;
|
---|
205 |
|
---|
206 | function TCustomBCPanel.GetStyleExtension: String;
|
---|
207 | begin
|
---|
208 | Result := 'bcpnl';
|
---|
209 | end;
|
---|
210 |
|
---|
211 | {$IFDEF DEBUG}
|
---|
212 | function TCustomBCPanel.GetDebugText: String;
|
---|
213 | begin
|
---|
214 | Result := 'R: '+IntToStr(FRenderCount);
|
---|
215 | end;
|
---|
216 | {$ENDIF}
|
---|
217 |
|
---|
218 | procedure TCustomBCPanel.Render;
|
---|
219 | var r: TRect;
|
---|
220 | begin
|
---|
221 | if (csCreating in FControlState) or IsUpdating then
|
---|
222 | Exit;
|
---|
223 |
|
---|
224 | FBGRA.NeedRender := False;
|
---|
225 |
|
---|
226 | FBGRA.SetSize(Width, Height);
|
---|
227 | FBGRA.Fill(BGRAPixelTransparent);
|
---|
228 | RenderBackground(FBGRA.ClipRect, FBackground, TBGRABitmap(FBGRA), FRounding);
|
---|
229 | r := FBGRA.ClipRect;
|
---|
230 |
|
---|
231 | case FBorderBCStyle of
|
---|
232 | bpsBorder:
|
---|
233 | begin
|
---|
234 | CalculateBorderRect(FBorder,r);
|
---|
235 | RenderBorder(r,FBorder, TBGRABitmap(FBGRA), FRounding);
|
---|
236 | end;
|
---|
237 | bpsFrame3d:
|
---|
238 | begin
|
---|
239 | // if BevelOuter is set then draw a frame with BevelWidth
|
---|
240 | if (FBevelOuter <> bvNone) and (FBevelWidth > 0) then
|
---|
241 | FBGRA.CanvasBGRA.Frame3d(r, FBevelWidth, FBevelOuter,
|
---|
242 | BGRA(255, 255, 255, 180), BGRA(0, 0, 0, 160)); // Note: Frame3D inflates ARect
|
---|
243 |
|
---|
244 | InflateRect(r, -FBevelWidth, -FBevelWidth);
|
---|
245 |
|
---|
246 | // if BevelInner is set then skip the BorderWidth and draw a frame with BevelWidth
|
---|
247 | if (FBevelInner <> bvNone) and (FBevelWidth > 0) then
|
---|
248 | FBGRA.CanvasBGRA.Frame3d(r, FBevelWidth, FBevelInner,
|
---|
249 | BGRA(255, 255, 255, 160), BGRA(0, 0, 0, 160)); // Note: Frame3D inflates ARect
|
---|
250 | end;
|
---|
251 | end;
|
---|
252 |
|
---|
253 | if Caption <> '' then
|
---|
254 | RenderText(r,FFontEx,Caption,TBGRABitmap(FBGRA));
|
---|
255 |
|
---|
256 | if Assigned(FOnAfterRenderBCPanel) then
|
---|
257 | FOnAfterRenderBCPanel(Self, FBGRA, r);
|
---|
258 |
|
---|
259 | {$IFDEF DEBUG}
|
---|
260 | FRenderCount += 1;
|
---|
261 | {$ENDIF}
|
---|
262 | end;
|
---|
263 |
|
---|
264 | procedure TCustomBCPanel.OnChangeProperty(Sender: TObject; AData: PtrInt);
|
---|
265 | begin
|
---|
266 | RenderControl;
|
---|
267 | Invalidate;
|
---|
268 | end;
|
---|
269 |
|
---|
270 | procedure TCustomBCPanel.OnChangeFont(Sender: TObject; AData: PtrInt);
|
---|
271 | begin
|
---|
272 | RenderControl;
|
---|
273 | Invalidate;
|
---|
274 | end;
|
---|
275 |
|
---|
276 | procedure TCustomBCPanel.SetRounding(AValue: TBCRounding);
|
---|
277 | begin
|
---|
278 | if FRounding = AValue then Exit;
|
---|
279 | FRounding.Assign(AValue);
|
---|
280 |
|
---|
281 | RenderControl;
|
---|
282 | Invalidate;
|
---|
283 | end;
|
---|
284 |
|
---|
285 | procedure TCustomBCPanel.AdjustClientRect(var aRect: TRect);
|
---|
286 | var BevelSize: Integer;
|
---|
287 | begin
|
---|
288 | inherited AdjustClientRect(aRect);
|
---|
289 |
|
---|
290 | BevelSize := BorderWidth;
|
---|
291 | if (BevelOuter <> bvNone) then
|
---|
292 | inc(BevelSize, BevelWidth);
|
---|
293 | if (BevelInner <> bvNone) then
|
---|
294 | inc(BevelSize, BevelWidth);
|
---|
295 |
|
---|
296 | InflateRect(aRect, -BevelSize, -BevelSize);
|
---|
297 | end;
|
---|
298 |
|
---|
299 | class function TCustomBCPanel.GetControlClassDefaultSize: TSize;
|
---|
300 | begin
|
---|
301 | Result.CX := 170;
|
---|
302 | Result.CY := 50;
|
---|
303 | end;
|
---|
304 |
|
---|
305 | function TCustomBCPanel.GetDefaultDockCaption: String;
|
---|
306 | begin
|
---|
307 | Result := Caption;
|
---|
308 | end;
|
---|
309 |
|
---|
310 | procedure TCustomBCPanel.SetBackground(AValue: TBCBackground);
|
---|
311 | begin
|
---|
312 | if FBackground = AValue then Exit;
|
---|
313 | FBackground.Assign(AValue);
|
---|
314 |
|
---|
315 | RenderControl;
|
---|
316 | Invalidate;
|
---|
317 | end;
|
---|
318 |
|
---|
319 | procedure TCustomBCPanel.SetBevelInner(AValue: TBevelCut);
|
---|
320 | begin
|
---|
321 | if FBevelInner = AValue then Exit;
|
---|
322 | FBevelInner := AValue;
|
---|
323 |
|
---|
324 | RenderControl;
|
---|
325 | Invalidate;
|
---|
326 | end;
|
---|
327 |
|
---|
328 | procedure TCustomBCPanel.SetBevelOuter(AValue: TBevelCut);
|
---|
329 | begin
|
---|
330 | if FBevelOuter = AValue then Exit;
|
---|
331 | FBevelOuter := AValue;
|
---|
332 |
|
---|
333 | RenderControl;
|
---|
334 | Invalidate;
|
---|
335 | end;
|
---|
336 |
|
---|
337 | procedure TCustomBCPanel.SetBevelWidth(AValue: Integer);
|
---|
338 | begin
|
---|
339 | if FBevelWidth = AValue then Exit;
|
---|
340 | FBevelWidth := AValue;
|
---|
341 |
|
---|
342 | RenderControl;
|
---|
343 | Invalidate;
|
---|
344 | end;
|
---|
345 |
|
---|
346 | procedure TCustomBCPanel.SetBorder(AValue: TBCBorder);
|
---|
347 | begin
|
---|
348 | if FBorder = AValue then Exit;
|
---|
349 | FBorder.Assign(AValue);
|
---|
350 |
|
---|
351 | RenderControl;
|
---|
352 | Invalidate;
|
---|
353 | end;
|
---|
354 |
|
---|
355 | procedure TCustomBCPanel.SetBorderBCStyle(AValue: TBCPanelBorderStyle);
|
---|
356 | begin
|
---|
357 | if FBorderBCStyle = AValue then Exit;
|
---|
358 | FBorderBCStyle := AValue;
|
---|
359 |
|
---|
360 | RenderControl;
|
---|
361 | Invalidate;
|
---|
362 | end;
|
---|
363 |
|
---|
364 | procedure TCustomBCPanel.SetFontEx(AValue: TBCFont);
|
---|
365 | begin
|
---|
366 | if FFontEx = AValue then Exit;
|
---|
367 | FFontEx.Assign(AValue);
|
---|
368 |
|
---|
369 | RenderControl;
|
---|
370 | Invalidate;
|
---|
371 | end;
|
---|
372 |
|
---|
373 | procedure TCustomBCPanel.SetEnabled(Value: boolean);
|
---|
374 | begin
|
---|
375 | inherited SetEnabled(Value);
|
---|
376 |
|
---|
377 | RenderControl;
|
---|
378 | Invalidate;
|
---|
379 | end;
|
---|
380 |
|
---|
381 | procedure TCustomBCPanel.TextChanged;
|
---|
382 | begin
|
---|
383 | inherited TextChanged;
|
---|
384 |
|
---|
385 | RenderControl;
|
---|
386 | Invalidate;
|
---|
387 | end;
|
---|
388 |
|
---|
389 | constructor TCustomBCPanel.Create(TheOwner: TComponent);
|
---|
390 | begin
|
---|
391 | inherited Create(TheOwner);
|
---|
392 | {$IFDEF DEBUG}
|
---|
393 | FRenderCount := 0;
|
---|
394 | {$ENDIF}
|
---|
395 | DisableAutoSizing;
|
---|
396 | Include(FControlState, csCreating);
|
---|
397 | BeginUpdate;
|
---|
398 | try
|
---|
399 | ControlStyle := ControlStyle + [csAcceptsControls, csCaptureMouse,
|
---|
400 | csClickEvents, csSetCaption, csDoubleClicks, csReplicatable,
|
---|
401 | csNoFocus, csAutoSize0x0]
|
---|
402 | - [csOpaque]; // we need the default background
|
---|
403 | //Self.DoubleBuffered := True;
|
---|
404 | with GetControlClassDefaultSize do
|
---|
405 | SetInitialBounds(0, 0, CX, CY);
|
---|
406 |
|
---|
407 | FBGRA := TBGRABitmapEx.Create;
|
---|
408 | FBorderBCStyle := bpsFrame3d;
|
---|
409 | FBackground := TBCBackground.Create(Self);
|
---|
410 | FBorder := TBCBorder.Create(Self);
|
---|
411 | FFontEx := TBCFont.Create(Self);
|
---|
412 | FBevelOuter := bvRaised;
|
---|
413 | FBevelInner := bvNone;
|
---|
414 | FBevelWidth := 1;
|
---|
415 | ParentColor := True;
|
---|
416 | UseDockManager := True;
|
---|
417 |
|
---|
418 | FBackground.OnChange := @OnChangeProperty;
|
---|
419 | FBorder.OnChange := @OnChangeProperty;
|
---|
420 | FFontEx.OnChange := @OnChangeFont;
|
---|
421 |
|
---|
422 | FBackground.Style := bbsColor;
|
---|
423 | FBackground.Color := {$ifdef UseCLDefault}clDefault{$else}clBtnFace{$endif};
|
---|
424 | FBorder.Style := bboNone;
|
---|
425 |
|
---|
426 | FRounding := TBCRounding.Create(Self);
|
---|
427 | FRounding.OnChange := @OnChangeProperty;
|
---|
428 | finally
|
---|
429 | EnableAutoSizing;
|
---|
430 | EndUpdate;
|
---|
431 | Exclude(FControlState, csCreating);
|
---|
432 | end;
|
---|
433 | end;
|
---|
434 |
|
---|
435 | destructor TCustomBCPanel.Destroy;
|
---|
436 | begin
|
---|
437 | FBackground.Free;
|
---|
438 | FBorder.Free;
|
---|
439 | FFontEx.Free;
|
---|
440 | FBGRA.Free;
|
---|
441 | FRounding.Free;
|
---|
442 | inherited Destroy;
|
---|
443 | end;
|
---|
444 |
|
---|
445 | procedure TCustomBCPanel.UpdateControl;
|
---|
446 | begin
|
---|
447 | Render;
|
---|
448 | inherited UpdateControl; // invalidate
|
---|
449 | end;
|
---|
450 |
|
---|
451 | end.
|
---|