2
0

G32_ProgressBar.pas 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. begin
  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. end;
  155. end
  156. else
  157. Buffer.FillRectS(ARect, Clr);
  158. Buffer.RaiseRectTS(ARect, 32);
  159. if Framed then
  160. with ARect do Buffer.VertLineS(Right, Top, Bottom, Color32(clWindowFrame));
  161. end;
  162. procedure TG32_ProgressBar.PaintFrame(Buffer: TBitmap32; ARect: TRect);
  163. var
  164. Clr: TColor32;
  165. begin
  166. if BorderStyle = bsSingle then
  167. begin
  168. end;
  169. { Paint frame border }
  170. if Framed then
  171. begin
  172. Clr := TColor32(clWindowFrame);
  173. Buffer.FrameRectS(ARect, Clr);
  174. InflateRect(ARect, -1, -1);
  175. end;
  176. { Fill the background }
  177. Clr := Color32(BackColor);
  178. Buffer.FillRectS(ARect, Clr);
  179. { Paint shadow on top of background}
  180. with ARect do
  181. begin
  182. Dec(Right);
  183. Dec(Bottom);
  184. Clr := Lighten(Clr, -32);
  185. Buffer.HorzLineS(Left, Top, Right, Clr);
  186. Buffer.VertLineS(Left, Top + 1, Bottom, Clr);
  187. if not Framed then
  188. begin
  189. Clr := Lighten(Clr, 64);
  190. Buffer.HorzLineS(Left, Bottom, Right, Clr);
  191. Buffer.VertLineS(Right, Top + 1, Bottom, Clr);
  192. end;
  193. end;
  194. end;
  195. procedure TG32_ProgressBar.SetBackColor(Value: TColor);
  196. begin
  197. FBackColor := Value;
  198. Invalidate;
  199. end;
  200. procedure TG32_ProgressBar.SetBorderStyle(Value: TBorderStyle);
  201. begin
  202. FBorderStyle := Value;
  203. Invalidate;
  204. end;
  205. procedure TG32_ProgressBar.SetContrast(Value: Integer);
  206. begin
  207. FContrast := Value;
  208. Invalidate;
  209. end;
  210. procedure TG32_ProgressBar.SetFramed(Value: Boolean);
  211. begin
  212. FFramed := Value;
  213. Invalidate;
  214. end;
  215. procedure TG32_ProgressBar.SetMax(Value: Integer);
  216. begin
  217. FMax := Value;
  218. Invalidate;
  219. end;
  220. procedure TG32_ProgressBar.SetMin(Value: Integer);
  221. begin
  222. FMin := Value;
  223. Invalidate;
  224. end;
  225. procedure TG32_ProgressBar.SetPosition(Value: Integer);
  226. begin
  227. if Value <> Position then
  228. begin
  229. FPosition := Value;
  230. Repaint;
  231. end;
  232. end;
  233. end.