G32_ProgressBar.pas 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. unit G32_ProgressBar;
  2. (* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1 or LGPL 2.1 with linking exception
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * Alternatively, the contents of this file may be used under the terms of the
  16. * Free Pascal modified version of the GNU Lesser General Public License
  17. * Version 2.1 (the "FPC modified LGPL License"), in which case the provisions
  18. * of this license are applicable instead of those above.
  19. * Please see the file LICENSE.txt for additional information concerning this
  20. * license.
  21. *
  22. * The Original Code is Graphics32
  23. *
  24. * The Initial Developer of the Original Code is
  25. * Alex A. Denisov
  26. *
  27. * Portions created by the Initial Developer are Copyright (C) 2000-2004
  28. * the Initial Developer. All Rights Reserved.
  29. *
  30. * Contributor(s):
  31. *
  32. * ***** END LICENSE BLOCK ***** *)
  33. interface
  34. uses
  35. Windows, Classes, Graphics, Forms, GR32, GR32_Image, GR32_Blend;
  36. type
  37. TG32_ProgressBar = class(TCustomPaintBox32)
  38. private
  39. FBackColor: TColor;
  40. FBorderStyle: TBorderStyle;
  41. FContrast: Integer;
  42. FFramed: Boolean;
  43. FMax: Integer;
  44. FMin: Integer;
  45. FPosition: Integer;
  46. procedure SetBackColor(Value: TColor);
  47. procedure SetBorderStyle(Value: TBorderStyle);
  48. procedure SetContrast(Value: Integer);
  49. procedure SetFramed(Value: Boolean);
  50. procedure SetMax(Value: Integer);
  51. procedure SetMin(Value: Integer);
  52. procedure SetPosition(Value: Integer);
  53. protected
  54. procedure DoPaintBuffer; override;
  55. function GetBarRect: TRect;
  56. procedure PaintBar(Buffer: TBitmap32; ARect: TRect);
  57. procedure PaintFrame(Buffer: TBitmap32; ARect: TRect);
  58. public
  59. constructor Create(AOwner: TComponent); override;
  60. published
  61. property Align;
  62. property Anchors;
  63. property BackColor: TColor read FBackColor write SetBackColor default clBtnShadow;
  64. property BorderStyle: TBorderStyle read FBorderStyle write SetBorderStyle;
  65. property Caption;
  66. property Color;
  67. property Constraints;
  68. property Contrast: Integer read FContrast write SetContrast default 64;
  69. property Cursor;
  70. property DragCursor;
  71. property DragKind;
  72. property DragMode;
  73. property Font;
  74. property Framed: Boolean read FFramed write SetFramed default True;
  75. property Height default 16;
  76. property HelpContext;
  77. property Max: Integer read FMax write SetMax default 100;
  78. property Min: Integer read FMin write SetMin default 0;
  79. property ParentColor;
  80. property ParentFont;
  81. property ParentShowHint;
  82. property PopupMenu;
  83. property Position: Integer read FPosition write SetPosition;
  84. property ShowHint;
  85. property Visible;
  86. property Width default 150;
  87. property OnDragDrop;
  88. property OnDragOver;
  89. property OnEndDrag;
  90. property OnMouseDown;
  91. property OnMouseMove;
  92. property OnMouseUp;
  93. property OnStartDrag;
  94. end;
  95. procedure Register;
  96. implementation
  97. procedure Register;
  98. begin
  99. RegisterComponents('Graphics32', [TG32_ProgressBar]);
  100. end;
  101. { TG32_ProgressBar }
  102. constructor TG32_ProgressBar.Create(AOwner: TComponent);
  103. begin
  104. inherited;
  105. Width := 150;
  106. Height := 16;
  107. FBackColor := clBtnShadow;
  108. FContrast := 64;
  109. FMax := 100;
  110. FFramed := True;
  111. end;
  112. procedure TG32_ProgressBar.DoPaintBuffer;
  113. var
  114. Rect: TRect;
  115. begin
  116. inherited;
  117. Rect := ClientRect;
  118. PaintFrame(Buffer, Rect);
  119. PaintBar(Buffer, GetBarRect);
  120. end;
  121. function TG32_ProgressBar.GetBarRect: TRect;
  122. var
  123. X: Integer;
  124. begin
  125. Result := ClientRect;
  126. if Framed then InflateRect(Result, -1, -1);
  127. with Result do
  128. if (Position > Min) and (Max > Min) then
  129. begin
  130. X := Round(Left + (Position - Min) * (Right - Left) / (Max - Min));
  131. if X < Right then Right := X;
  132. end
  133. else
  134. Right := Left; // return an empty rectagle
  135. end;
  136. procedure TG32_ProgressBar.PaintBar(Buffer: TBitmap32; ARect: TRect);
  137. var
  138. Clr, LineColor: TColor32;
  139. I, CY, H: Integer;
  140. begin
  141. if IsRectEmpty(ARect) then Exit;
  142. Clr := Color32(Color);
  143. if Contrast <> 0 then
  144. begin
  145. with ARect do
  146. try
  147. H := Bottom - Top;
  148. CY := (Top + Bottom) div 2;
  149. for I := Top to Bottom - 1 do
  150. begin
  151. LineColor := Lighten(Clr, (CY - I) * Contrast div H);
  152. Buffer.HorzLineS(Left, I, Right - 1, LineColor);
  153. end;
  154. finally
  155. EMMS; // the low-level blending function was used EMMS is required
  156. end;
  157. end
  158. else Buffer.FillRectS(ARect, Clr);
  159. Buffer.RaiseRectTS(ARect, 32);
  160. if Framed then
  161. with ARect do Buffer.VertLineS(Right, Top, Bottom, Color32(clWindowFrame));
  162. end;
  163. procedure TG32_ProgressBar.PaintFrame(Buffer: TBitmap32; ARect: TRect);
  164. var
  165. Clr: TColor32;
  166. begin
  167. if BorderStyle = bsSingle then
  168. begin
  169. end;
  170. { Paint frame border }
  171. if Framed then
  172. begin
  173. Clr := TColor32(clWindowFrame);
  174. Buffer.FrameRectS(ARect, Clr);
  175. InflateRect(ARect, -1, -1);
  176. end;
  177. { Fill the background }
  178. Clr := Color32(BackColor);
  179. Buffer.FillRectS(ARect, Clr);
  180. { Paint shadow on top of background}
  181. with ARect do
  182. begin
  183. Dec(Right);
  184. Dec(Bottom);
  185. Clr := Lighten(Clr, -32);
  186. Buffer.HorzLineS(Left, Top, Right, Clr);
  187. Buffer.VertLineS(Left, Top + 1, Bottom, Clr);
  188. if not Framed then
  189. begin
  190. Clr := Lighten(Clr, 64);
  191. Buffer.HorzLineS(Left, Bottom, Right, Clr);
  192. Buffer.VertLineS(Right, Top + 1, Bottom, Clr);
  193. end;
  194. end;
  195. end;
  196. procedure TG32_ProgressBar.SetBackColor(Value: TColor);
  197. begin
  198. FBackColor := Value;
  199. Invalidate;
  200. end;
  201. procedure TG32_ProgressBar.SetBorderStyle(Value: TBorderStyle);
  202. begin
  203. FBorderStyle := Value;
  204. Invalidate;
  205. end;
  206. procedure TG32_ProgressBar.SetContrast(Value: Integer);
  207. begin
  208. FContrast := Value;
  209. Invalidate;
  210. end;
  211. procedure TG32_ProgressBar.SetFramed(Value: Boolean);
  212. begin
  213. FFramed := Value;
  214. Invalidate;
  215. end;
  216. procedure TG32_ProgressBar.SetMax(Value: Integer);
  217. begin
  218. FMax := Value;
  219. Invalidate;
  220. end;
  221. procedure TG32_ProgressBar.SetMin(Value: Integer);
  222. begin
  223. FMin := Value;
  224. Invalidate;
  225. end;
  226. procedure TG32_ProgressBar.SetPosition(Value: Integer);
  227. begin
  228. if Value <> Position then
  229. begin
  230. FPosition := Value;
  231. Repaint;
  232. end;
  233. end;
  234. end.