GR32_ColorSwatch.pas 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. unit GR32_ColorSwatch;
  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-2009
  28. * the Initial Developer. All Rights Reserved.
  29. *
  30. * ***** END LICENSE BLOCK ***** *)
  31. interface
  32. {$include GR32.inc}
  33. uses
  34. {$IFDEF FPC}
  35. LCLIntf, LCLType, LMessages, Types,
  36. {$IFDEF MSWINDOWS}
  37. Windows,
  38. {$ENDIF}
  39. {$ELSE}
  40. Windows, Messages,
  41. {$ENDIF}
  42. Classes, Controls, Forms, GR32, GR32_Containers;
  43. type
  44. TCustomColorSwatch = class(TCustomControl)
  45. private
  46. FBuffer: TBitmap32;
  47. FColor: TColor32;
  48. FBufferValid: Boolean;
  49. FBorder: Boolean;
  50. procedure SetBorder(const Value: Boolean);
  51. procedure SetColor(const Value: TColor32); reintroduce;
  52. {$IFDEF FPC}
  53. procedure WMEraseBkgnd(var Message: TLMEraseBkgnd); message LM_ERASEBKGND;
  54. procedure WMGetDlgCode(var Msg: TLMessage); message LM_GETDLGCODE;
  55. {$ELSE}
  56. procedure WMEraseBkgnd(var Message: TWmEraseBkgnd); message WM_ERASEBKGND;
  57. procedure WMGetDlgCode(var Msg: TWmGetDlgCode); message WM_GETDLGCODE;
  58. {$ENDIF}
  59. protected
  60. procedure Paint; override;
  61. public
  62. constructor Create(AOwner: TComponent); override;
  63. destructor Destroy; override;
  64. procedure Assign(Source: TPersistent); override;
  65. procedure Invalidate; override;
  66. procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer); override;
  67. property Border: Boolean read FBorder write SetBorder default False;
  68. property Color: TColor32 read FColor write SetColor;
  69. end;
  70. TColorSwatch = class(TCustomColorSwatch)
  71. protected
  72. procedure DefineProperties(Filer: TFiler); override;
  73. procedure SkipValue(Reader: TReader);
  74. published
  75. property Border;
  76. property Color;
  77. property Align;
  78. property Anchors;
  79. property DragCursor;
  80. property DragKind;
  81. property Enabled;
  82. property ParentShowHint;
  83. property PopupMenu;
  84. property TabOrder;
  85. property TabStop;
  86. {$IFNDEF PLATFORM_INDEPENDENT}
  87. property OnCanResize;
  88. {$ENDIF}
  89. property OnClick;
  90. property OnDblClick;
  91. property OnDragDrop;
  92. property OnDragOver;
  93. property OnEndDrag;
  94. property OnMouseDown;
  95. property OnMouseMove;
  96. property OnMouseUp;
  97. property OnMouseWheel;
  98. property OnMouseWheelDown;
  99. property OnMouseWheelUp;
  100. property OnMouseEnter;
  101. property OnMouseLeave;
  102. property OnResize;
  103. property OnStartDrag;
  104. end;
  105. implementation
  106. uses
  107. Math, Graphics, GR32_Backends, GR32_Math, GR32_Blend, GR32_VectorUtils;
  108. { TCustomColorSwatch }
  109. procedure TCustomColorSwatch.Assign(Source: TPersistent);
  110. begin
  111. inherited;
  112. if (Source is TCustomColorSwatch) then
  113. begin
  114. FBorder := TCustomColorSwatch(Source).Border;
  115. FColor := TCustomColorSwatch(Source).Color;
  116. Invalidate;
  117. end;
  118. end;
  119. constructor TCustomColorSwatch.Create(AOwner: TComponent);
  120. begin
  121. inherited Create(AOwner);
  122. ControlStyle := [csOpaque, csClickEvents, csDoubleClicks];
  123. Width := 32;
  124. Height := 32;
  125. FBuffer := TBitmap32.Create;
  126. FColor := clSalmon32;
  127. end;
  128. destructor TCustomColorSwatch.Destroy;
  129. begin
  130. FBuffer.Free;
  131. inherited;
  132. end;
  133. procedure TCustomColorSwatch.Invalidate;
  134. begin
  135. FBufferValid := False;
  136. inherited;
  137. end;
  138. procedure TCustomColorSwatch.Paint;
  139. var
  140. X, Y: Integer;
  141. OddY: Boolean;
  142. ScanLine: PColor32Array;
  143. const
  144. CCheckerBoardColor: array [Boolean] of TColor32 = ($FFA0A0A0, $FF5F5F5F);
  145. begin
  146. // if not Assigned(Parent) then
  147. // Exit;
  148. if (FBuffer.Empty) then
  149. exit;
  150. if not FBufferValid then
  151. begin
  152. (FBuffer.Backend as IPaintSupport).ImageNeeded;
  153. // draw checker board
  154. if not (FColor and $FF000000 = $FF000000) then
  155. begin
  156. Y := 0;
  157. while Y < FBuffer.Height do
  158. begin
  159. ScanLine := FBuffer.Scanline[Y];
  160. OddY := Odd(Y shr 2);
  161. for X := 0 to FBuffer.Width - 1 do
  162. ScanLine[X] := CCheckerBoardColor[Odd(X shr 2) = OddY];
  163. Inc(Y);
  164. end;
  165. end;
  166. // draw color
  167. FBuffer.FillRectT(0, 0, FBuffer.Width, FBuffer.Height, FColor);
  168. // eventually draw border
  169. if FBorder then
  170. FBuffer.FrameRectTS(0, 0, FBuffer.Width, FBuffer.Height, $DF000000);
  171. (FBuffer.Backend as IPaintSupport).CheckPixmap;
  172. FBufferValid := True;
  173. end;
  174. FBuffer.Lock;
  175. try
  176. (FBuffer.Backend as IDeviceContextSupport).DrawTo(Canvas.Handle, 0, 0);
  177. finally
  178. FBuffer.Unlock;
  179. end;
  180. end;
  181. procedure TCustomColorSwatch.SetBorder(const Value: Boolean);
  182. begin
  183. if FBorder <> Value then
  184. begin
  185. FBorder := Value;
  186. Invalidate;
  187. end;
  188. end;
  189. procedure TCustomColorSwatch.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
  190. begin
  191. inherited;
  192. if (FBuffer <> nil) then
  193. FBuffer.SetSize(Width, Height);
  194. FBufferValid := False;
  195. end;
  196. procedure TCustomColorSwatch.SetColor(const Value: TColor32);
  197. begin
  198. if FColor <> Value then
  199. begin
  200. FColor := Value;
  201. Invalidate;
  202. end;
  203. end;
  204. procedure TCustomColorSwatch.WMEraseBkgnd(var Message: {$IFDEF FPC}TLmEraseBkgnd{$ELSE}TWmEraseBkgnd{$ENDIF});
  205. begin
  206. Message.Result := 1;
  207. end;
  208. procedure TCustomColorSwatch.WMGetDlgCode(var Msg: {$IFDEF FPC}TLMessage{$ELSE}TWmGetDlgCode{$ENDIF});
  209. begin
  210. Msg.Result := Msg.Result or DLGC_WANTARROWS;
  211. end;
  212. { TColorSwatch }
  213. procedure TColorSwatch.DefineProperties(Filer: TFiler);
  214. begin
  215. inherited;
  216. // Previously, but no longer, published properties
  217. Filer.DefineProperty('ParentBackground', SkipValue, nil, False);
  218. Filer.DefineProperty('ParentColor', SkipValue, nil, False);
  219. end;
  220. procedure TColorSwatch.SkipValue(Reader: TReader);
  221. begin
  222. {$ifndef FPC}
  223. Reader.SkipValue;
  224. {$else}
  225. Reader.Driver.SkipValue;
  226. {$endif}
  227. end;
  228. end.