| 1 | unit 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 |
|
|---|
| 36 | interface
|
|---|
| 37 |
|
|---|
| 38 | {$I GR32.inc}
|
|---|
| 39 |
|
|---|
| 40 | uses
|
|---|
| 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 |
|
|---|
| 51 | type
|
|---|
| 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 |
|
|---|
| 120 | implementation
|
|---|
| 121 |
|
|---|
| 122 | uses
|
|---|
| 123 | Math, Graphics, GR32_Backends, GR32_Math, GR32_Blend, GR32_VectorUtils;
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 | { TCustomColorSwatch }
|
|---|
| 127 |
|
|---|
| 128 | constructor TCustomColorSwatch.Create(AOwner: TComponent);
|
|---|
| 129 | begin
|
|---|
| 130 | inherited Create(AOwner);
|
|---|
| 131 |
|
|---|
| 132 | ControlStyle := ControlStyle + [csOpaque];
|
|---|
| 133 | FBuffer := TBitmap32.Create;
|
|---|
| 134 | FColor := clSalmon32;
|
|---|
| 135 | end;
|
|---|
| 136 |
|
|---|
| 137 | destructor TCustomColorSwatch.Destroy;
|
|---|
| 138 | begin
|
|---|
| 139 | FBuffer.Free;
|
|---|
| 140 | inherited;
|
|---|
| 141 | end;
|
|---|
| 142 |
|
|---|
| 143 | procedure TCustomColorSwatch.Invalidate;
|
|---|
| 144 | begin
|
|---|
| 145 | FBufferValid := False;
|
|---|
| 146 | inherited;
|
|---|
| 147 | end;
|
|---|
| 148 |
|
|---|
| 149 | procedure TCustomColorSwatch.Paint;
|
|---|
| 150 | var
|
|---|
| 151 | X, Y: Integer;
|
|---|
| 152 | OddY: Boolean;
|
|---|
| 153 | ScanLine: PColor32Array;
|
|---|
| 154 | const
|
|---|
| 155 | CCheckerBoardColor: array [Boolean] of TColor32 = ($FFA0A0A0, $FF5F5F5F);
|
|---|
| 156 | begin
|
|---|
| 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;
|
|---|
| 199 | end;
|
|---|
| 200 |
|
|---|
| 201 | procedure TCustomColorSwatch.Resize;
|
|---|
| 202 | begin
|
|---|
| 203 | inherited;
|
|---|
| 204 |
|
|---|
| 205 | FBuffer.SetSize(Width, Height);
|
|---|
| 206 | FBufferValid := False;
|
|---|
| 207 | end;
|
|---|
| 208 |
|
|---|
| 209 | procedure TCustomColorSwatch.SetBorder(const Value: Boolean);
|
|---|
| 210 | begin
|
|---|
| 211 | if FBorder <> Value then
|
|---|
| 212 | begin
|
|---|
| 213 | FBorder := Value;
|
|---|
| 214 | Invalidate;
|
|---|
| 215 | end;
|
|---|
| 216 | end;
|
|---|
| 217 |
|
|---|
| 218 | procedure TCustomColorSwatch.SetColor(const Value: TColor32);
|
|---|
| 219 | begin
|
|---|
| 220 | if FColor <> Value then
|
|---|
| 221 | begin
|
|---|
| 222 | FColor := Value;
|
|---|
| 223 | Invalidate;
|
|---|
| 224 | end;
|
|---|
| 225 | end;
|
|---|
| 226 |
|
|---|
| 227 | procedure TCustomColorSwatch.WMEraseBkgnd(var Message: {$IFDEF FPC}TLmEraseBkgnd{$ELSE}TWmEraseBkgnd{$ENDIF});
|
|---|
| 228 | begin
|
|---|
| 229 | Message.Result := 1;
|
|---|
| 230 | end;
|
|---|
| 231 |
|
|---|
| 232 | procedure TCustomColorSwatch.WMGetDlgCode(var Msg: {$IFDEF FPC}TLMessage{$ELSE}TWmGetDlgCode{$ENDIF});
|
|---|
| 233 | begin
|
|---|
| 234 | with Msg do
|
|---|
| 235 | Result := Result or DLGC_WANTARROWS;
|
|---|
| 236 | end;
|
|---|
| 237 |
|
|---|
| 238 | end.
|
|---|