bgragraphiccontrol.pas 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // SPDX-License-Identifier: LGPL-3.0-linking-exception
  2. {
  3. Created by BGRA Controls Team
  4. Dibo, Circular, lainz (007) and contributors.
  5. For detailed information see readme.txt
  6. Site: https://sourceforge.net/p/bgra-controls/
  7. Wiki: http://wiki.lazarus.freepascal.org/BGRAControls
  8. Forum: http://forum.lazarus.freepascal.org/index.php/board,46.0.html
  9. }
  10. {******************************* CONTRIBUTOR(S) ******************************
  11. - Edivando S. Santos Brasil | [email protected]
  12. (Compatibility with delphi VCL 11/2018)
  13. ***************************** END CONTRIBUTOR(S) *****************************}
  14. unit BGRAGraphicControl;
  15. {$I bgracontrols.inc}
  16. interface
  17. uses
  18. Classes, SysUtils, {$IFDEF FPC}LResources,{$ENDIF} Forms, Controls, Graphics, Dialogs, Types,
  19. {$IFNDEF FPC}BGRAGraphics, GraphType, FPImage, {$ENDIF}
  20. BCBaseCtrls, ExtCtrls, BGRABitmap, BCTypes;
  21. type
  22. { TCustomBGRAGraphicControl }
  23. TCustomBGRAGraphicControl = class(TBGRAGraphicCtrl)
  24. private
  25. { Private declarations }
  26. FOnRedraw: TBGRARedrawEvent;
  27. FBevelInner, FBevelOuter: TPanelBevel;
  28. FBevelWidth: TBevelWidth;
  29. FBorderWidth: TBorderWidth;
  30. FAlignment: TAlignment;
  31. FColorOpacity: byte;
  32. function GetBitmapHeight: integer;
  33. function GetBitmapScale: double;
  34. function GetBitmapWidth: integer;
  35. procedure SetAlignment(const Value: TAlignment);
  36. procedure SetBevelInner(const AValue: TPanelBevel);
  37. procedure SetBevelOuter(const AValue: TPanelBevel);
  38. procedure SetBevelWidth(const AValue: TBevelWidth);
  39. procedure SetBitmapAutoScale(AValue: boolean);
  40. procedure SetBorderWidth(const AValue: TBorderWidth);
  41. procedure SetColorOpacity(const AValue: byte);
  42. protected
  43. { Protected declarations }
  44. FBGRA: TBGRABitmap;
  45. FBitmapAutoScale: boolean;
  46. procedure Paint; override;
  47. procedure Resize; override;
  48. procedure BGRASetSize(AWidth, AHeight: integer); virtual;
  49. procedure RedrawBitmapContent; virtual;
  50. procedure SetColor(Value: TColor); override;
  51. procedure SetEnabled(Value: boolean); override;
  52. procedure TextChanged; override;
  53. property BitmapAutoScale: boolean read FBitmapAutoScale write SetBitmapAutoScale default true;
  54. property BitmapScale: double read GetBitmapScale;
  55. property BitmapWidth: integer read GetBitmapWidth;
  56. property BitmapHeight: integer read GetBitmapHeight;
  57. public
  58. { Public declarations }
  59. constructor Create(TheOwner: TComponent); override;
  60. procedure RedrawBitmap;
  61. procedure DiscardBitmap;
  62. destructor Destroy; override;
  63. property OnRedraw: TBGRARedrawEvent Read FOnRedraw Write FOnRedraw;
  64. property Bitmap: TBGRABitmap Read FBGRA;
  65. property BorderWidth: TBorderWidth Read FBorderWidth Write SetBorderWidth default 0;
  66. property BevelInner: TPanelBevel Read FBevelInner Write SetBevelInner default bvNone;
  67. property BevelOuter: TPanelBevel
  68. Read FBevelOuter Write SetBevelOuter default bvRaised;
  69. property BevelWidth: TBevelWidth Read FBevelWidth Write SetBevelWidth default 1;
  70. property ColorOpacity: byte Read FColorOpacity Write SetColorOpacity;
  71. property Alignment: TAlignment Read FAlignment Write SetAlignment;
  72. end;
  73. TBGRAGraphicControl = class(TCustomBGRAGraphicControl)
  74. published
  75. { Published declarations }
  76. property Align;
  77. property Anchors;
  78. property OnRedraw;
  79. property Bitmap;
  80. property BitmapAutoscale;
  81. property BitmapScale;
  82. property BorderWidth;
  83. property BevelInner;
  84. property BevelOuter;
  85. property BevelWidth;
  86. property Color;
  87. property ColorOpacity;
  88. property Alignment;
  89. property OnClick;
  90. property OnDblClick;
  91. property OnMouseDown;
  92. property OnMouseEnter;
  93. property OnMouseLeave;
  94. property OnMouseMove;
  95. property OnMouseUp;
  96. {$IFDEF FPC}
  97. property OnPaint;
  98. {$ENDIF}
  99. property Caption;
  100. end;
  101. {$IFDEF FPC}procedure Register;{$ENDIF}
  102. implementation
  103. uses BGRABitmapTypes, LazVersion;
  104. {$IFDEF FPC}
  105. procedure Register;
  106. begin
  107. RegisterComponents('BGRA Controls', [TBGRAGraphicControl]);
  108. end;
  109. {$ENDIF}
  110. procedure TCustomBGRAGraphicControl.SetAlignment(const Value: TAlignment);
  111. begin
  112. if FAlignment = Value then
  113. exit;
  114. FAlignment := Value;
  115. DiscardBitmap;
  116. end;
  117. function TCustomBGRAGraphicControl.GetBitmapHeight: integer;
  118. begin
  119. result := round(ClientHeight * BitmapScale);
  120. end;
  121. function TCustomBGRAGraphicControl.GetBitmapScale: double;
  122. begin
  123. {$if laz_fullversion >= 2000000}
  124. if not FBitmapAutoScale then
  125. result := GetCanvasScaleFactor
  126. else
  127. result := 1;
  128. {$else}
  129. result := 1;
  130. {$endif}
  131. end;
  132. function TCustomBGRAGraphicControl.GetBitmapWidth: integer;
  133. begin
  134. result := round(ClientWidth * BitmapScale);
  135. end;
  136. procedure TCustomBGRAGraphicControl.SetBevelInner(const AValue: TPanelBevel);
  137. begin
  138. if FBevelInner = AValue then
  139. exit;
  140. FBevelInner := AValue;
  141. DiscardBitmap;
  142. end;
  143. procedure TCustomBGRAGraphicControl.SetBevelOuter(const AValue: TPanelBevel);
  144. begin
  145. if FBevelOuter = AValue then
  146. exit;
  147. FBevelOuter := AValue;
  148. DiscardBitmap;
  149. end;
  150. procedure TCustomBGRAGraphicControl.SetBevelWidth(const AValue: TBevelWidth);
  151. begin
  152. if FBevelWidth = AValue then
  153. exit;
  154. FBevelWidth := AValue;
  155. DiscardBitmap;
  156. end;
  157. procedure TCustomBGRAGraphicControl.SetBitmapAutoScale(AValue: boolean);
  158. begin
  159. if FBitmapAutoScale=AValue then Exit;
  160. FBitmapAutoScale:=AValue;
  161. DiscardBitmap;
  162. end;
  163. procedure TCustomBGRAGraphicControl.SetBorderWidth(const AValue: TBorderWidth);
  164. begin
  165. if FBorderWidth = AValue then
  166. exit;
  167. FBorderWidth := AValue;
  168. DiscardBitmap;
  169. end;
  170. procedure TCustomBGRAGraphicControl.SetColorOpacity(const AValue: byte);
  171. begin
  172. if FColorOpacity = AValue then
  173. exit;
  174. FColorOpacity := AValue;
  175. DiscardBitmap;
  176. end;
  177. procedure TCustomBGRAGraphicControl.Paint;
  178. begin
  179. BGRASetSize(BitmapWidth, BitmapHeight);
  180. inherited Paint;
  181. FBGRA.Draw(Canvas, rect(0, 0, ClientWidth, ClientHeight), False);
  182. end;
  183. procedure TCustomBGRAGraphicControl.Resize;
  184. begin
  185. inherited Resize;
  186. DiscardBitmap;
  187. end;
  188. procedure TCustomBGRAGraphicControl.BGRASetSize(AWidth, AHeight: integer);
  189. begin
  190. if (FBGRA <> nil) and (AWidth <> FBGRA.Width) and (AHeight <> FBGRA.Height) then
  191. begin
  192. FBGRA.SetSize(AWidth, AHeight);
  193. RedrawBitmapContent;
  194. end;
  195. end;
  196. procedure TCustomBGRAGraphicControl.RedrawBitmapContent;
  197. var
  198. ARect: TRect;
  199. TS: TTextStyle;
  200. scale: Double;
  201. begin
  202. if (FBGRA <> nil) and (FBGRA.NbPixels <> 0) then
  203. begin
  204. FBGRA.Fill(ColorToBGRA(ColorToRGB(Color), FColorOpacity));
  205. scale := BitmapScale;
  206. ARect := GetClientRect;
  207. ARect.Left := round(ARect.Left*scale);
  208. ARect.Top := round(ARect.Top*scale);
  209. ARect.Right := round(ARect.Right*scale);
  210. ARect.Bottom := round(ARect.Bottom*scale);
  211. // if BevelOuter is set then draw a frame with BevelWidth
  212. if (BevelOuter <> bvNone) and (BevelWidth > 0) then
  213. FBGRA.CanvasBGRA.Frame3d(ARect, round(BevelWidth*scale), BevelOuter,
  214. BGRA(255, 255, 255, 200), BGRA(0, 0, 0, 160)); // Note: Frame3D inflates ARect
  215. InflateRect(ARect, -round(BorderWidth*scale), -round(BorderWidth*scale));
  216. // if BevelInner is set then skip the BorderWidth and draw a frame with BevelWidth
  217. if (BevelInner <> bvNone) and (BevelWidth > 0) then
  218. FBGRA.CanvasBGRA.Frame3d(ARect, round(BevelWidth*scale), BevelInner,
  219. BGRA(255, 255, 255, 160), BGRA(0, 0, 0, 160)); // Note: Frame3D inflates ARect
  220. if Caption <> '' then
  221. begin
  222. FBGRA.CanvasBGRA.Font.Assign(Canvas.Font);
  223. FBGRA.CanvasBGRA.Font.Height:= round(FBGRA.CanvasBGRA.Font.Height*scale);
  224. {$IFDEF FPC}//#
  225. TS := Canvas.TextStyle;
  226. {$ENDIF}
  227. TS.Alignment := Alignment;
  228. TS.Layout := tlCenter;
  229. TS.Opaque := False;
  230. TS.Clipping := False;
  231. {$IFDEF FPC}//#
  232. TS.SystemFont := Canvas.Font.IsDefault;
  233. {$ENDIF}
  234. FBGRA.CanvasBGRA.Font.Color := Color xor $FFFFFF;
  235. FBGRA.CanvasBGRA.Font.Opacity := 255;
  236. if not Enabled then
  237. FBGRA.CanvasBGRA.Font.Style := [fsStrikeOut]
  238. else
  239. FBGRA.CanvasBGRA.Font.Style := [];
  240. FBGRA.CanvasBGRA.TextRect(ARect, ARect.Left, ARect.Top, Caption, TS);
  241. end;
  242. if Assigned(FOnRedraw) then
  243. FOnRedraw(self, FBGRA);
  244. end;
  245. end;
  246. procedure TCustomBGRAGraphicControl.SetColor(Value: TColor);
  247. begin
  248. if Value <> Color then
  249. DiscardBitmap;
  250. inherited SetColor(Value);
  251. end;
  252. procedure TCustomBGRAGraphicControl.SetEnabled(Value: boolean);
  253. begin
  254. if Value <> Enabled then
  255. DiscardBitmap;
  256. inherited SetEnabled(Value);
  257. end;
  258. procedure TCustomBGRAGraphicControl.TextChanged;
  259. begin
  260. DiscardBitmap;
  261. end;
  262. constructor TCustomBGRAGraphicControl.Create(TheOwner: TComponent);
  263. begin
  264. inherited Create(TheOwner);
  265. with GetControlClassDefaultSize do
  266. SetInitialBounds(0, 0, CX, CY);
  267. FBGRA := TBGRABitmap.Create;
  268. FBitmapAutoScale:= true;
  269. FBevelWidth := 1;
  270. FAlignment := taCenter;
  271. Color := clWhite;
  272. FColorOpacity := 128;
  273. FBevelOuter := bvRaised;
  274. FBevelInner := bvNone;
  275. end;
  276. procedure TCustomBGRAGraphicControl.RedrawBitmap;
  277. begin
  278. RedrawBitmapContent;
  279. Repaint;
  280. end;
  281. procedure TCustomBGRAGraphicControl.DiscardBitmap;
  282. begin
  283. if (FBGRA <> nil) and (FBGRA.NbPixels <> 0) then
  284. begin
  285. FBGRA.SetSize(0, 0);
  286. Invalidate;
  287. end;
  288. end;
  289. destructor TCustomBGRAGraphicControl.Destroy;
  290. begin
  291. FBGRA.Free;
  292. inherited Destroy;
  293. end;
  294. end.