source: trunk/Packages/bgracontrols/bclabel.pas

Last change on this file was 2, checked in by chronos, 5 years ago
File size: 8.2 KB
Line 
1{ Equivalent of standard lazarus TLabel but using BGRA Controls framework for text
2 render.
3
4 Functionality:
5 - Customizable background (gradients etc.)
6 - Customizable border (rounding etc.)
7 - FontEx (shadow, word wrap, etc.)
8
9 Copyright (C) 2012 Krzysztof Dibowski dibowski at interia.pl
10
11 This library is free software; you can redistribute it and/or modify it
12 under the terms of the GNU Library General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or (at your
14 option) any later version with the following modification:
15
16 As a special exception, the copyright holders of this library give you
17 permission to link this library with independent modules to produce an
18 executable, regardless of the license terms of these independent modules,and
19 to copy and distribute the resulting executable under terms of your choice,
20 provided that you also meet, for each linked independent module, the terms
21 and conditions of the license of that module. An independent module is a
22 module which is not derived from or based on this library. If you modify
23 this library, you may extend this exception to your version of the library,
24 but you are not obligated to do so. If you do not wish to do so, delete this
25 exception statement from your version.
26
27 This program is distributed in the hope that it will be useful, but WITHOUT
28 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
29 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
30 for more details.
31
32 You should have received a copy of the GNU Library General Public License
33 along with this library; if not, write to the Free Software Foundation,
34 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35}
36
37unit BCLabel;
38
39{$mode objfpc}{$H+}
40
41interface
42
43uses
44 Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs,
45 BCBasectrls, BGRABitmap, BGRABitmapTypes, BCTypes, types;
46
47type
48
49 { TCustomBCLabel }
50
51 TCustomBCLabel = class(TBCStyleGraphicControl)
52 private
53 { Private declarations }
54 {$IFDEF DEBUG}
55 FRenderCount: Integer;
56 {$ENDIF}
57 FBackground: TBCBackground;
58 FBGRA: TBGRABitmapEx;
59 FBorder: TBCBorder;
60 FFontEx: TBCFont;
61 FRounding: TBCRounding;
62 procedure Render;
63 procedure SetRounding(AValue: TBCRounding);
64 procedure UpdateSize;
65 procedure SetBackground(AValue: TBCBackground);
66 procedure SetBorder(AValue: TBCBorder);
67 procedure SetFontEx(AValue: TBCFont);
68 procedure OnChangeProperty(Sender: TObject; Data: PtrInt);
69 procedure OnChangeFont(Sender: TObject; AData: PtrInt);
70 protected
71 procedure CalculatePreferredSize(var PreferredWidth, PreferredHeight: integer;
72 WithThemeSpace: boolean); override;
73 class function GetControlClassDefaultSize: TSize; override;
74 procedure TextChanged; override;
75 protected
76 {$IFDEF DEBUG}
77 function GetDebugText: String; override;
78 {$ENDIF}
79 procedure DrawControl; override;
80 procedure RenderControl; override;
81 function GetStyleExtension: String; override;
82 protected
83 { Protected declarations }
84 property AutoSize default True;
85 property Background: TBCBackground read FBackground write SetBackground;
86 property Border: TBCBorder read FBorder write SetBorder;
87 property FontEx: TBCFont read FFontEx write SetFontEx;
88 property Rounding: TBCRounding read FRounding write SetRounding;
89 public
90 { Public declarations }
91 constructor Create(AOwner: TComponent); override;
92 destructor Destroy; override;
93 procedure UpdateControl; override; // Called by EndUpdate
94 published
95 { Published declarations }
96 end;
97
98 { TBCLabel }
99
100 TBCLabel = class(TCustomBCLabel)
101 published
102 property Action;
103 property Align;
104 property Anchors;
105 property AssignStyle;
106 property AutoSize;
107 property Background;
108 property Border;
109 property BorderSpacing;
110 property Caption;
111 property Cursor;
112 property Enabled;
113 property FontEx;
114 property Height;
115 property HelpContext;
116 property HelpKeyword;
117 property HelpType;
118 property Hint;
119 property Left;
120 property PopupMenu;
121 property Rounding;
122 property ShowHint;
123 property Tag;
124 property Top;
125 property Visible;
126 property Width;
127 end;
128
129procedure Register;
130
131implementation
132
133uses BCTools;
134
135procedure Register;
136begin
137 {$I bclabel_icon.lrs}
138 RegisterComponents('BGRA Controls',[TBCLabel]);
139end;
140
141{ TCustomBCLabel }
142
143procedure TCustomBCLabel.Render;
144var r: TRect;
145begin
146 if (csCreating in FControlState) or IsUpdating then
147 Exit;
148
149 FBGRA.NeedRender := False;
150
151 FBGRA.SetSize(Width, Height);
152 FBGRA.Fill(BGRAPixelTransparent); // Clear;
153 r := FBGRA.ClipRect;
154 CalculateBorderRect(FBorder,r);
155
156 RenderBackground(FBGRA.ClipRect, FBackground, TBGRABitmap(FBGRA), FRounding);
157 RenderBorder(r, FBorder, TBGRABitmap(FBGRA), FRounding);
158 RenderText(FBGRA.ClipRect, FFontEx, Caption, TBGRABitmap(FBGRA));
159
160 {$IFDEF DEBUG}
161 FRenderCount += 1;
162 {$ENDIF}
163end;
164
165procedure TCustomBCLabel.SetRounding(AValue: TBCRounding);
166begin
167 if FRounding = AValue then Exit;
168 FRounding.Assign(AValue);
169
170 RenderControl;
171 Invalidate;
172end;
173
174procedure TCustomBCLabel.UpdateSize;
175begin
176 InvalidatePreferredSize;
177 AdjustSize;
178end;
179
180procedure TCustomBCLabel.SetBackground(AValue: TBCBackground);
181begin
182 FBackground.Assign(AValue);
183
184 RenderControl;
185 Invalidate;
186end;
187
188procedure TCustomBCLabel.SetBorder(AValue: TBCBorder);
189begin
190 FBorder.Assign(AValue);
191
192 RenderControl;
193 Invalidate;
194end;
195
196procedure TCustomBCLabel.SetFontEx(AValue: TBCFont);
197begin
198 FFontEx.Assign(AValue);
199
200 RenderControl;
201 Invalidate;
202end;
203
204procedure TCustomBCLabel.OnChangeProperty(Sender: TObject; Data: PtrInt);
205begin
206 RenderControl;
207 if (Sender = FBorder) and AutoSize then
208 UpdateSize;
209 Invalidate;
210end;
211
212procedure TCustomBCLabel.OnChangeFont(Sender: TObject; AData: PtrInt);
213begin
214 RenderControl;
215 UpdateSize;
216 Invalidate;
217end;
218
219procedure TCustomBCLabel.CalculatePreferredSize(var PreferredWidth,
220 PreferredHeight: integer; WithThemeSpace: boolean);
221begin
222 if (Parent = nil) or (not Parent.HandleAllocated) then
223 Exit;
224
225 CalculateTextSize(Caption, FFontEx, PreferredWidth, PreferredHeight);
226
227 if AutoSize and (FBorder.Style<>bboNone) then
228 begin
229 Inc(PreferredHeight, 2 * FBorder.Width);
230 Inc(PreferredWidth, 2 * FBorder.Width);
231 end;
232end;
233
234class function TCustomBCLabel.GetControlClassDefaultSize: TSize;
235begin
236 Result.cx := 100;
237 Result.cy := 25;
238end;
239
240procedure TCustomBCLabel.TextChanged;
241begin
242 inherited TextChanged;
243 RenderControl;
244 UpdateSize;
245 Invalidate;
246end;
247
248{$IFDEF DEBUG}
249function TCustomBCLabel.GetDebugText: String;
250begin
251 Result := 'R: '+IntToStr(FRenderCount);
252end;
253{$ENDIF}
254
255procedure TCustomBCLabel.DrawControl;
256begin
257 inherited DrawControl;
258 if FBGRA.NeedRender then
259 Render;
260 FBGRA.Draw(Self.Canvas,0,0,False);
261end;
262
263procedure TCustomBCLabel.RenderControl;
264begin
265 inherited RenderControl;
266 if FBGRA<>nil then
267 FBGRA.NeedRender := True;
268end;
269
270function TCustomBCLabel.GetStyleExtension: String;
271begin
272 Result := 'bclbl';
273end;
274
275procedure TCustomBCLabel.UpdateControl;
276begin
277 RenderControl;
278 inherited UpdateControl; // invalidate
279end;
280
281constructor TCustomBCLabel.Create(AOwner: TComponent);
282begin
283 inherited Create(AOwner);
284 {$IFDEF DEBUG}
285 FRenderCount := 0;
286 {$ENDIF}
287 DisableAutoSizing;
288 Include(FControlState, csCreating);
289 BeginUpdate;
290 try
291 with GetControlClassDefaultSize do
292 SetInitialBounds(0, 0, CX, CY);
293 FBGRA := TBGRABitmapEx.Create(Width, Height);
294 FBackground := TBCBackground.Create(Self);
295 FBorder := TBCBorder.Create(Self);
296 FFontEx := TBCFont.Create(Self);
297 ParentColor := True;
298
299 FBackground.OnChange := @OnChangeProperty;
300 FBorder.OnChange := @OnChangeProperty;
301 FFontEx.OnChange := @OnChangeFont;
302
303 FBackground.Style := bbsClear;
304 FBorder.Style := bboNone;
305
306 FRounding := TBCRounding.Create(Self);
307 FRounding.OnChange := @OnChangeProperty;
308
309 AutoSize := True;
310 finally
311 EnableAutoSizing;
312 EndUpdate;
313 Exclude(FControlState, csCreating);
314 end;
315end;
316
317destructor TCustomBCLabel.Destroy;
318begin
319 FBGRA.Free;
320 FBackground.Free;
321 FBorder.Free;
322 FFontEx.Free;
323 FRounding.Free;
324 inherited Destroy;
325end;
326
327end.
Note: See TracBrowser for help on using the repository browser.