| 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 |
|
|---|
| 37 | unit BCLabel;
|
|---|
| 38 |
|
|---|
| 39 | {$mode objfpc}{$H+}
|
|---|
| 40 |
|
|---|
| 41 | interface
|
|---|
| 42 |
|
|---|
| 43 | uses
|
|---|
| 44 | Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs,
|
|---|
| 45 | BCBasectrls, BGRABitmap, BGRABitmapTypes, BCTypes, types;
|
|---|
| 46 |
|
|---|
| 47 | type
|
|---|
| 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 |
|
|---|
| 129 | procedure Register;
|
|---|
| 130 |
|
|---|
| 131 | implementation
|
|---|
| 132 |
|
|---|
| 133 | uses BCTools;
|
|---|
| 134 |
|
|---|
| 135 | procedure Register;
|
|---|
| 136 | begin
|
|---|
| 137 | {$I bclabel_icon.lrs}
|
|---|
| 138 | RegisterComponents('BGRA Controls',[TBCLabel]);
|
|---|
| 139 | end;
|
|---|
| 140 |
|
|---|
| 141 | { TCustomBCLabel }
|
|---|
| 142 |
|
|---|
| 143 | procedure TCustomBCLabel.Render;
|
|---|
| 144 | var r: TRect;
|
|---|
| 145 | begin
|
|---|
| 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}
|
|---|
| 163 | end;
|
|---|
| 164 |
|
|---|
| 165 | procedure TCustomBCLabel.SetRounding(AValue: TBCRounding);
|
|---|
| 166 | begin
|
|---|
| 167 | if FRounding = AValue then Exit;
|
|---|
| 168 | FRounding.Assign(AValue);
|
|---|
| 169 |
|
|---|
| 170 | RenderControl;
|
|---|
| 171 | Invalidate;
|
|---|
| 172 | end;
|
|---|
| 173 |
|
|---|
| 174 | procedure TCustomBCLabel.UpdateSize;
|
|---|
| 175 | begin
|
|---|
| 176 | InvalidatePreferredSize;
|
|---|
| 177 | AdjustSize;
|
|---|
| 178 | end;
|
|---|
| 179 |
|
|---|
| 180 | procedure TCustomBCLabel.SetBackground(AValue: TBCBackground);
|
|---|
| 181 | begin
|
|---|
| 182 | FBackground.Assign(AValue);
|
|---|
| 183 |
|
|---|
| 184 | RenderControl;
|
|---|
| 185 | Invalidate;
|
|---|
| 186 | end;
|
|---|
| 187 |
|
|---|
| 188 | procedure TCustomBCLabel.SetBorder(AValue: TBCBorder);
|
|---|
| 189 | begin
|
|---|
| 190 | FBorder.Assign(AValue);
|
|---|
| 191 |
|
|---|
| 192 | RenderControl;
|
|---|
| 193 | Invalidate;
|
|---|
| 194 | end;
|
|---|
| 195 |
|
|---|
| 196 | procedure TCustomBCLabel.SetFontEx(AValue: TBCFont);
|
|---|
| 197 | begin
|
|---|
| 198 | FFontEx.Assign(AValue);
|
|---|
| 199 |
|
|---|
| 200 | RenderControl;
|
|---|
| 201 | Invalidate;
|
|---|
| 202 | end;
|
|---|
| 203 |
|
|---|
| 204 | procedure TCustomBCLabel.OnChangeProperty(Sender: TObject; Data: PtrInt);
|
|---|
| 205 | begin
|
|---|
| 206 | RenderControl;
|
|---|
| 207 | if (Sender = FBorder) and AutoSize then
|
|---|
| 208 | UpdateSize;
|
|---|
| 209 | Invalidate;
|
|---|
| 210 | end;
|
|---|
| 211 |
|
|---|
| 212 | procedure TCustomBCLabel.OnChangeFont(Sender: TObject; AData: PtrInt);
|
|---|
| 213 | begin
|
|---|
| 214 | RenderControl;
|
|---|
| 215 | UpdateSize;
|
|---|
| 216 | Invalidate;
|
|---|
| 217 | end;
|
|---|
| 218 |
|
|---|
| 219 | procedure TCustomBCLabel.CalculatePreferredSize(var PreferredWidth,
|
|---|
| 220 | PreferredHeight: integer; WithThemeSpace: boolean);
|
|---|
| 221 | begin
|
|---|
| 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;
|
|---|
| 232 | end;
|
|---|
| 233 |
|
|---|
| 234 | class function TCustomBCLabel.GetControlClassDefaultSize: TSize;
|
|---|
| 235 | begin
|
|---|
| 236 | Result.cx := 100;
|
|---|
| 237 | Result.cy := 25;
|
|---|
| 238 | end;
|
|---|
| 239 |
|
|---|
| 240 | procedure TCustomBCLabel.TextChanged;
|
|---|
| 241 | begin
|
|---|
| 242 | inherited TextChanged;
|
|---|
| 243 | RenderControl;
|
|---|
| 244 | UpdateSize;
|
|---|
| 245 | Invalidate;
|
|---|
| 246 | end;
|
|---|
| 247 |
|
|---|
| 248 | {$IFDEF DEBUG}
|
|---|
| 249 | function TCustomBCLabel.GetDebugText: String;
|
|---|
| 250 | begin
|
|---|
| 251 | Result := 'R: '+IntToStr(FRenderCount);
|
|---|
| 252 | end;
|
|---|
| 253 | {$ENDIF}
|
|---|
| 254 |
|
|---|
| 255 | procedure TCustomBCLabel.DrawControl;
|
|---|
| 256 | begin
|
|---|
| 257 | inherited DrawControl;
|
|---|
| 258 | if FBGRA.NeedRender then
|
|---|
| 259 | Render;
|
|---|
| 260 | FBGRA.Draw(Self.Canvas,0,0,False);
|
|---|
| 261 | end;
|
|---|
| 262 |
|
|---|
| 263 | procedure TCustomBCLabel.RenderControl;
|
|---|
| 264 | begin
|
|---|
| 265 | inherited RenderControl;
|
|---|
| 266 | if FBGRA<>nil then
|
|---|
| 267 | FBGRA.NeedRender := True;
|
|---|
| 268 | end;
|
|---|
| 269 |
|
|---|
| 270 | function TCustomBCLabel.GetStyleExtension: String;
|
|---|
| 271 | begin
|
|---|
| 272 | Result := 'bclbl';
|
|---|
| 273 | end;
|
|---|
| 274 |
|
|---|
| 275 | procedure TCustomBCLabel.UpdateControl;
|
|---|
| 276 | begin
|
|---|
| 277 | RenderControl;
|
|---|
| 278 | inherited UpdateControl; // invalidate
|
|---|
| 279 | end;
|
|---|
| 280 |
|
|---|
| 281 | constructor TCustomBCLabel.Create(AOwner: TComponent);
|
|---|
| 282 | begin
|
|---|
| 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;
|
|---|
| 315 | end;
|
|---|
| 316 |
|
|---|
| 317 | destructor TCustomBCLabel.Destroy;
|
|---|
| 318 | begin
|
|---|
| 319 | FBGRA.Free;
|
|---|
| 320 | FBackground.Free;
|
|---|
| 321 | FBorder.Free;
|
|---|
| 322 | FFontEx.Free;
|
|---|
| 323 | FRounding.Free;
|
|---|
| 324 | inherited Destroy;
|
|---|
| 325 | end;
|
|---|
| 326 |
|
|---|
| 327 | end.
|
|---|