bctoolbar.pas 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 BCToolBar;
  15. {$I bgracontrols.inc}
  16. interface
  17. uses
  18. Classes, {$IFDEF FPC}LResources,{$ELSE}types, BGRAGraphics, GraphType, FPImage,{$ENDIF}
  19. Forms, Controls, Graphics, Dialogs, ComCtrls,
  20. BGRABitmap, BGRABitmapTypes, BGRAGradients, BCTypes;
  21. type
  22. { TBCToolBar }
  23. TBCToolBar = class(TToolBar)
  24. private
  25. FLimitMemoryUsage: boolean;
  26. { Private declarations }
  27. FOnRedraw: TBGRARedrawEvent;
  28. FBGRA: TBGRABitmap;
  29. procedure SetLimitMemoryUsage(AValue: boolean);
  30. protected
  31. { Protected declarations }
  32. {$IFDEF FPC}
  33. procedure Paint; override;
  34. {$ELSE}
  35. procedure Paint; virtual;
  36. procedure PaintWindow(DC: HDC); override;
  37. {$ENDIF}
  38. procedure CheckMemoryUsage; virtual;
  39. public
  40. { Public declarations }
  41. constructor Create(TheOwner: TComponent); override;
  42. destructor Destroy; override;
  43. published
  44. { Published declarations }
  45. property OnRedraw: TBGRARedrawEvent read FOnRedraw write FOnRedraw;
  46. property LimitMemoryUsage: boolean read FLimitMemoryUsage write SetLimitMemoryUsage;
  47. end;
  48. procedure DrawWindows7ToolBar(Bitmap: TBGRABitmap; AColor: TColor = clDefault);
  49. {$IFDEF FPC}procedure Register;{$ENDIF}
  50. implementation
  51. function SetHue(AColor: TBGRAPixel; g_hue: integer): TBGRAPixel;
  52. var hsla: THSLAPixel;
  53. begin
  54. if g_hue = -1 then result := AColor else
  55. begin
  56. hsla := BGRAToHSLA(AColor);
  57. hsla.hue := g_hue;
  58. result := GSBAToBGRA(hsla);
  59. end;
  60. end;
  61. procedure DrawWindows7ToolBar(Bitmap: TBGRABitmap; AColor: TColor = clDefault);
  62. var
  63. c1, c2, c3, c4: TBGRAPixel;
  64. ARect, ARect2: TRect;
  65. g_hue: integer;
  66. begin
  67. if AColor = clDefault then
  68. g_hue := -1
  69. else
  70. g_hue := BGRAToGSBA(AColor).hue;
  71. ARect := Rect(0, 0, Bitmap.Width, Bitmap.Height);
  72. // Font: RGBToColor(30,57,91)
  73. Bitmap.HorizLine(ARect.Left, ARect.Top, ARect.Right-1, SetHue(BGRA(169, 191, 214), g_hue), dmSet);
  74. Bitmap.HorizLine(ARect.Left, ARect.Top + 1, ARect.Right-1, SetHue(BGRA(250, 252, 253), g_hue), dmSet);
  75. Bitmap.HorizLine(ARect.Left, ARect.Top + 2, ARect.Right-1, SetHue(BGRA(253, 254, 255), g_hue), dmSet);
  76. c1 := SetHue(BGRA(252, 254, 255), g_hue);
  77. c2 := SetHue(BGRA(243, 248, 253), g_hue);
  78. c3 := SetHue(BGRA(238, 243, 250), g_hue);
  79. c4 := SetHue(BGRA(238, 244, 251), g_hue);
  80. ARect2 := Rect(ARect.Left, ARect.Top + 3, ARect.Right, ARect.Bottom - 3);
  81. DoubleGradientAlphaFill(Bitmap, ARect2, c1, c2, c3, c4, gdVertical,
  82. gdVertical, gdVertical, 0.5);
  83. c1 := SetHue(BGRA(249, 252, 255), g_hue);
  84. c2 := SetHue(BGRA(230, 240, 250), g_hue);
  85. c3 := SetHue(BGRA(220, 230, 244), g_hue);
  86. c4 := SetHue(BGRA(221, 233, 247), g_hue);
  87. ARect2 := Rect(ARect.Left + 1, ARect.Top + 3, ARect.Right - 1, ARect.Bottom - 3);
  88. DoubleGradientAlphaFill(Bitmap, ARect2, c1, c2, c3, c4, gdVertical,
  89. gdVertical, gdVertical, 0.5);
  90. Bitmap.HorizLine(ARect.Left, ARect.Bottom - 3, ARect.Right-1, SetHue(BGRA(228, 239, 251), g_hue), dmSet);
  91. Bitmap.HorizLine(ARect.Left, ARect.Bottom - 2, ARect.Right-1, SetHue(BGRA(205, 218, 234), g_hue), dmSet);
  92. Bitmap.HorizLine(ARect.Left, ARect.Bottom - 1, ARect.Right-1, SetHue(BGRA(160, 175, 195), g_hue), dmSet);
  93. end;
  94. {$IFDEF FPC}
  95. procedure Register;
  96. begin
  97. RegisterComponents('BGRA Controls', [TBCToolBar]);
  98. end;
  99. {$ENDIF}
  100. { TBCToolBar }
  101. constructor TBCToolBar.Create(TheOwner: TComponent);
  102. begin
  103. inherited Create(TheOwner);
  104. FBGRA := TBGRABitmap.Create;
  105. end;
  106. destructor TBCToolBar.Destroy;
  107. begin
  108. FBGRA.Free;
  109. inherited Destroy;
  110. end;
  111. procedure TBCToolBar.SetLimitMemoryUsage(AValue: boolean);
  112. begin
  113. if FLimitMemoryUsage=AValue then Exit;
  114. FLimitMemoryUsage:=AValue;
  115. CheckMemoryUsage;
  116. end;
  117. {$IFNDEF FPC}
  118. procedure TBCToolBar.PaintWindow(DC: HDC);
  119. begin
  120. Canvas.Lock;
  121. try
  122. Canvas.Handle := DC;
  123. try
  124. TControlCanvas(Canvas).UpdateTextFlags;
  125. Paint;
  126. finally
  127. Canvas.Handle := 0;
  128. end;
  129. finally
  130. Canvas.Unlock;
  131. end;
  132. end;
  133. {$ENDIF}
  134. procedure TBCToolBar.Paint;
  135. begin
  136. if (FBGRA.Width <> Width) or (FBGRA.Height <> Height) then
  137. begin
  138. FBGRA.SetSize(Width, Height);
  139. if Assigned(FOnRedraw) then
  140. { Draw using event }
  141. FOnRedraw(self, FBGRA)
  142. else
  143. { Draw this default }
  144. DrawWindows7ToolBar(FBGRA, Color);
  145. end;
  146. FBGRA.Draw(Canvas, 0, 0);
  147. CheckMemoryUsage;
  148. end;
  149. procedure TBCToolBar.CheckMemoryUsage;
  150. begin
  151. if FLimitMemoryUsage then
  152. begin
  153. if FBGRA.NbPixels <> 0 then
  154. FBGRA.SetSize(0,0);
  155. end;
  156. end;
  157. end.