source: trunk/Packages/Graphics32/GR32_ColorSwatch.pas

Last change on this file was 2, checked in by chronos, 5 years ago
File size: 5.7 KB
Line 
1unit GR32_ColorSwatch;
2
3(* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1 or LGPL 2.1 with linking exception
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Alternatively, the contents of this file may be used under the terms of the
17 * Free Pascal modified version of the GNU Lesser General Public License
18 * Version 2.1 (the "FPC modified LGPL License"), in which case the provisions
19 * of this license are applicable instead of those above.
20 * Please see the file LICENSE.txt for additional information concerning this
21 * license.
22 *
23 * The Original Code is Graphics32
24 *
25 * The Initial Developer of the Original Code is
26 * Alex A. Denisov
27 *
28 * Portions created by the Initial Developer are Copyright (C) 2000-2009
29 * the Initial Developer. All Rights Reserved.
30 *
31 * Contributor(s):
32 * Christan-W. Budde <Christian@savioursofsoul.de>
33 *
34 * ***** END LICENSE BLOCK ***** *)
35
36interface
37
38{$I GR32.inc}
39
40uses
41{$IFDEF FPC}
42 LCLIntf, LCLType, LMessages, Types,
43 {$IFDEF MSWINDOWS}
44 Windows,
45 {$ENDIF}
46{$ELSE}
47 Windows, Messages,
48{$ENDIF}
49 Classes, Controls, Forms, GR32, GR32_Containers;
50
51type
52 TCustomColorSwatch = class(TCustomControl)
53 private
54 FBuffer: TBitmap32;
55 FColor: TColor32;
56 FBufferValid: Boolean;
57 FBorder: Boolean;
58 procedure SetBorder(const Value: Boolean);
59 procedure SetColor(const Value: TColor32); reintroduce;
60{$IFDEF FPC}
61 procedure WMEraseBkgnd(var Message: TLMEraseBkgnd); message LM_ERASEBKGND;
62 procedure WMGetDlgCode(var Msg: TLMessage); message LM_GETDLGCODE;
63{$ELSE}
64 procedure WMEraseBkgnd(var Message: TWmEraseBkgnd); message WM_ERASEBKGND;
65 procedure WMGetDlgCode(var Msg: TWmGetDlgCode); message WM_GETDLGCODE;
66{$ENDIF}
67 protected
68 procedure Paint; override;
69 public
70 constructor Create(AOwner: TComponent); override;
71 destructor Destroy; override;
72
73 procedure Invalidate; override;
74 procedure Resize; override;
75
76 property Border: Boolean read FBorder write SetBorder default False;
77 property Color: TColor32 read FColor write SetColor;
78 end;
79
80 TColorSwatch = class(TCustomColorSwatch)
81 published
82 property Align;
83 property Anchors;
84 property Border;
85 property Color;
86 property DragCursor;
87 property DragKind;
88 property Enabled;
89{$IFNDEF FPC}
90 property ParentBackground;
91{$ENDIF}
92 property ParentColor;
93 property ParentShowHint;
94 property PopupMenu;
95 property TabOrder;
96 property TabStop;
97
98{$IFNDEF PLATFORM_INDEPENDENT}
99 property OnCanResize;
100{$ENDIF}
101 property OnClick;
102 property OnDblClick;
103 property OnDragDrop;
104 property OnDragOver;
105 property OnEndDrag;
106 property OnMouseDown;
107 property OnMouseMove;
108 property OnMouseUp;
109 property OnMouseWheel;
110 property OnMouseWheelDown;
111 property OnMouseWheelUp;
112{$IFDEF COMPILER2005_UP}
113 property OnMouseEnter;
114 property OnMouseLeave;
115{$ENDIF}
116 property OnResize;
117 property OnStartDrag;
118 end;
119
120implementation
121
122uses
123 Math, Graphics, GR32_Backends, GR32_Math, GR32_Blend, GR32_VectorUtils;
124
125
126{ TCustomColorSwatch }
127
128constructor TCustomColorSwatch.Create(AOwner: TComponent);
129begin
130 inherited Create(AOwner);
131
132 ControlStyle := ControlStyle + [csOpaque];
133 FBuffer := TBitmap32.Create;
134 FColor := clSalmon32;
135end;
136
137destructor TCustomColorSwatch.Destroy;
138begin
139 FBuffer.Free;
140 inherited;
141end;
142
143procedure TCustomColorSwatch.Invalidate;
144begin
145 FBufferValid := False;
146 inherited;
147end;
148
149procedure TCustomColorSwatch.Paint;
150var
151 X, Y: Integer;
152 OddY: Boolean;
153 ScanLine: PColor32Array;
154const
155 CCheckerBoardColor: array [Boolean] of TColor32 = ($FFA0A0A0, $FF5F5F5F);
156begin
157 if not Assigned(Parent) then
158 Exit;
159
160 if not FBufferValid then
161 begin
162 (FBuffer.Backend as IPaintSupport).ImageNeeded;
163
164 // draw checker board
165 if not (FColor and $FF000000 = $FF000000) then
166 begin
167 Y := 0;
168 while Y < FBuffer.Height do
169 begin
170 ScanLine := FBuffer.Scanline[Y];
171 OddY := Odd(Y shr 2);
172 for X := 0 to FBuffer.Width - 1 do
173 ScanLine[X] := CCheckerBoardColor[Odd(X shr 2) = OddY];
174 Inc(Y);
175 end;
176 end;
177
178 // draw color
179 FBuffer.FillRectT(0, 0, FBuffer.Width, FBuffer.Height, FColor);
180
181 // eventually draw border
182 if FBorder then
183 begin
184 FBuffer.FrameRectTS(0, 0, FBuffer.Width, FBuffer.Height, $DF000000);
185 FBuffer.RaiseRectTS(1, 1, FBuffer.Width - 1, FBuffer.Height - 1, 20);
186 end;
187
188 (FBuffer.Backend as IPaintSupport).CheckPixmap;
189 FBufferValid := True;
190 end;
191
192 FBuffer.Lock;
193 with Canvas do
194 try
195 (FBuffer.Backend as IDeviceContextSupport).DrawTo(Canvas.Handle, 0, 0);
196 finally
197 FBuffer.Unlock;
198 end;
199end;
200
201procedure TCustomColorSwatch.Resize;
202begin
203 inherited;
204
205 FBuffer.SetSize(Width, Height);
206 FBufferValid := False;
207end;
208
209procedure TCustomColorSwatch.SetBorder(const Value: Boolean);
210begin
211 if FBorder <> Value then
212 begin
213 FBorder := Value;
214 Invalidate;
215 end;
216end;
217
218procedure TCustomColorSwatch.SetColor(const Value: TColor32);
219begin
220 if FColor <> Value then
221 begin
222 FColor := Value;
223 Invalidate;
224 end;
225end;
226
227procedure TCustomColorSwatch.WMEraseBkgnd(var Message: {$IFDEF FPC}TLmEraseBkgnd{$ELSE}TWmEraseBkgnd{$ENDIF});
228begin
229 Message.Result := 1;
230end;
231
232procedure TCustomColorSwatch.WMGetDlgCode(var Msg: {$IFDEF FPC}TLMessage{$ELSE}TWmGetDlgCode{$ENDIF});
233begin
234 with Msg do
235 Result := Result or DLGC_WANTARROWS;
236end;
237
238end.
Note: See TracBrowser for help on using the repository browser.