source: trunk/Packages/bgracontrols/bcpanel.pas

Last change on this file was 2, checked in by chronos, 5 years ago
File size: 11.9 KB
Line 
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}
35unit BCPanel;
36
37{$mode objfpc}{$H+}
38
39interface
40
41uses
42 Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs,
43 BGRABitmap, BCBaseCtrls, BGRABitmapTypes, BCTypes, Types;
44
45type
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
169procedure Register;
170
171implementation
172
173uses BCTools;
174
175procedure Register;
176begin
177 {$I bcpanel_icon.lrs}
178 RegisterComponents('BGRA Controls', [TBCPanel]);
179end;
180
181{ TCustomBCPanel }
182
183procedure TCustomBCPanel.DrawControl;
184begin
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);
197end;
198
199procedure TCustomBCPanel.RenderControl;
200begin
201 inherited RenderControl;
202 if FBGRA<>nil then
203 FBGRA.NeedRender := True;
204end;
205
206function TCustomBCPanel.GetStyleExtension: String;
207begin
208 Result := 'bcpnl';
209end;
210
211{$IFDEF DEBUG}
212function TCustomBCPanel.GetDebugText: String;
213begin
214 Result := 'R: '+IntToStr(FRenderCount);
215end;
216{$ENDIF}
217
218procedure TCustomBCPanel.Render;
219var r: TRect;
220begin
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}
262end;
263
264procedure TCustomBCPanel.OnChangeProperty(Sender: TObject; AData: PtrInt);
265begin
266 RenderControl;
267 Invalidate;
268end;
269
270procedure TCustomBCPanel.OnChangeFont(Sender: TObject; AData: PtrInt);
271begin
272 RenderControl;
273 Invalidate;
274end;
275
276procedure TCustomBCPanel.SetRounding(AValue: TBCRounding);
277begin
278 if FRounding = AValue then Exit;
279 FRounding.Assign(AValue);
280
281 RenderControl;
282 Invalidate;
283end;
284
285procedure TCustomBCPanel.AdjustClientRect(var aRect: TRect);
286var BevelSize: Integer;
287begin
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);
297end;
298
299class function TCustomBCPanel.GetControlClassDefaultSize: TSize;
300begin
301 Result.CX := 170;
302 Result.CY := 50;
303end;
304
305function TCustomBCPanel.GetDefaultDockCaption: String;
306begin
307 Result := Caption;
308end;
309
310procedure TCustomBCPanel.SetBackground(AValue: TBCBackground);
311begin
312 if FBackground = AValue then Exit;
313 FBackground.Assign(AValue);
314
315 RenderControl;
316 Invalidate;
317end;
318
319procedure TCustomBCPanel.SetBevelInner(AValue: TBevelCut);
320begin
321 if FBevelInner = AValue then Exit;
322 FBevelInner := AValue;
323
324 RenderControl;
325 Invalidate;
326end;
327
328procedure TCustomBCPanel.SetBevelOuter(AValue: TBevelCut);
329begin
330 if FBevelOuter = AValue then Exit;
331 FBevelOuter := AValue;
332
333 RenderControl;
334 Invalidate;
335end;
336
337procedure TCustomBCPanel.SetBevelWidth(AValue: Integer);
338begin
339 if FBevelWidth = AValue then Exit;
340 FBevelWidth := AValue;
341
342 RenderControl;
343 Invalidate;
344end;
345
346procedure TCustomBCPanel.SetBorder(AValue: TBCBorder);
347begin
348 if FBorder = AValue then Exit;
349 FBorder.Assign(AValue);
350
351 RenderControl;
352 Invalidate;
353end;
354
355procedure TCustomBCPanel.SetBorderBCStyle(AValue: TBCPanelBorderStyle);
356begin
357 if FBorderBCStyle = AValue then Exit;
358 FBorderBCStyle := AValue;
359
360 RenderControl;
361 Invalidate;
362end;
363
364procedure TCustomBCPanel.SetFontEx(AValue: TBCFont);
365begin
366 if FFontEx = AValue then Exit;
367 FFontEx.Assign(AValue);
368
369 RenderControl;
370 Invalidate;
371end;
372
373procedure TCustomBCPanel.SetEnabled(Value: boolean);
374begin
375 inherited SetEnabled(Value);
376
377 RenderControl;
378 Invalidate;
379end;
380
381procedure TCustomBCPanel.TextChanged;
382begin
383 inherited TextChanged;
384
385 RenderControl;
386 Invalidate;
387end;
388
389constructor TCustomBCPanel.Create(TheOwner: TComponent);
390begin
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;
433end;
434
435destructor TCustomBCPanel.Destroy;
436begin
437 FBackground.Free;
438 FBorder.Free;
439 FFontEx.Free;
440 FBGRA.Free;
441 FRounding.Free;
442 inherited Destroy;
443end;
444
445procedure TCustomBCPanel.UpdateControl;
446begin
447 Render;
448 inherited UpdateControl; // invalidate
449end;
450
451end.
Note: See TracBrowser for help on using the repository browser.