| 1 |
unit mbBasicPicker; |
| 2 |
|
| 3 |
{$mode objfpc}{$H+} |
| 4 |
|
| 5 |
interface |
| 6 |
|
| 7 |
uses |
| 8 |
LMessages, Classes, SysUtils, Graphics, Controls, ExtCtrls, Forms; |
| 9 |
|
| 10 |
type |
| 11 |
THintState = (hsOff, hsWaitingToShow, hsWaitingToHide); |
| 12 |
TGetHintStrEvent = procedure (Sender: TObject; X, Y: Integer; var AText: String) of object; |
| 13 |
|
| 14 |
{ TmbBasicPicker } |
| 15 |
|
| 16 |
TmbBasicPicker = class(TCustomControl) |
| 17 |
private |
| 18 |
FOnChange: TNotifyEvent; |
| 19 |
FOnGetHintStr: TGetHintStrEvent; |
| 20 |
FLockChange: Integer; |
| 21 |
protected |
| 22 |
FBufferBmp: TBitmap; |
| 23 |
FGradientWidth: Integer; |
| 24 |
FGradientHeight: Integer; |
| 25 |
FHintShown: Boolean; |
| 26 |
procedure CreateGradient; virtual; |
| 27 |
procedure DoChange; virtual; |
| 28 |
function GetColorUnderCursor: TColor; virtual; |
| 29 |
function GetGradientColor({%H-}AValue: Integer): TColor; virtual; |
| 30 |
function GetGradientColor2D({%H-}X, {%H-}Y: Integer): TColor; virtual; |
| 31 |
function GetHintPos(X, Y: Integer): TPoint; virtual; |
| 32 |
function GetHintStr(X, Y: Integer): String; virtual; |
| 33 |
function GetSelectedColor: TColor; virtual; abstract; |
| 34 |
procedure PaintParentBack; virtual; overload; |
| 35 |
procedure PaintParentBack(ACanvas: TCanvas); overload; |
| 36 |
procedure PaintParentBack(ACanvas: TCanvas; ARect: TRect); overload; |
| 37 |
procedure PaintParentBack(ABitmap: TBitmap); overload; |
| 38 |
procedure SetSelectedColor(c: TColor); virtual; abstract; |
| 39 |
procedure CMHintShow(var Message: TCMHintShow); message CM_HINTSHOW; |
| 40 |
procedure CMParentColorChanged(var Message: TLMessage); message CM_PARENTCOLORCHANGED; |
| 41 |
property ColorUnderCursor: TColor read GetColorUnderCursor; |
| 42 |
property OnChange: TNotifyEvent read FOnChange write FOnChange; |
| 43 |
property OnGetHintStr: TGetHintStrEvent read FOnGetHintStr write FOnGetHintStr; |
| 44 |
public |
| 45 |
constructor Create(AOwner: TComponent); override; |
| 46 |
destructor Destroy; override; |
| 47 |
function GetColorAtPoint(X, Y: Integer): TColor; virtual; |
| 48 |
function GetHexColorAtPoint(X, Y: integer): string; |
| 49 |
function GetHexColorUnderCursor: string; virtual; |
| 50 |
procedure Lock; |
| 51 |
function IsLocked: Boolean; |
| 52 |
procedure Unlock; |
| 53 |
published |
| 54 |
property ParentColor default true; |
| 55 |
property SelectedColor: TColor read GetSelectedColor write SetSelectedColor; |
| 56 |
end; |
| 57 |
|
| 58 |
implementation |
| 59 |
|
| 60 |
uses |
| 61 |
LCLIntf, |
| 62 |
HTMLColors, mbUtils; |
| 63 |
|
| 64 |
constructor TmbBasicPicker.Create(AOwner: TComponent); |
| 65 |
begin |
| 66 |
inherited Create(AOwner); |
| 67 |
// ControlStyle := ControlStyle - [csOpaque]; |
| 68 |
ParentColor := true; |
| 69 |
{$IFDEF WINDOWS} |
| 70 |
DoubleBuffered := true; |
| 71 |
{$ENDIF} |
| 72 |
end; |
| 73 |
|
| 74 |
destructor TmbBasicPicker.Destroy; |
| 75 |
begin |
| 76 |
FBufferBmp.Free; |
| 77 |
inherited; |
| 78 |
end; |
| 79 |
|
| 80 |
procedure TmbBasicPicker.CMHintShow(var Message: TCMHintShow); |
| 81 |
var |
| 82 |
cp: TPoint; |
| 83 |
hp: TPoint; |
| 84 |
begin |
| 85 |
if GetColorUnderCursor <> clNone then |
| 86 |
with TCMHintShow(Message) do |
| 87 |
if not ShowHint then |
| 88 |
Message.Result := 1 |
| 89 |
else |
| 90 |
if Hint <> '' then |
| 91 |
Message.Result := 0 |
| 92 |
else |
| 93 |
begin |
| 94 |
cp := HintInfo^.CursorPos; |
| 95 |
hp := GetHintPos(cp.X, cp.Y); |
| 96 |
HintInfo^.ReshowTimeout := 0; // must be zero! |
| 97 |
HintInfo^.HideTimeout := Application.HintHidePause; |
| 98 |
HintInfo^.HintStr := GetHintStr(cp.X, cp.Y); |
| 99 |
HintInfo^.HintPos := ClientToScreen(Point(hp.X + 16, hp.Y)); |
| 100 |
HintInfo^.CursorRect := Rect(cp.X, cp.Y, cp.X+1, cp.Y+1); |
| 101 |
Result := 0; // 0 means: show hint |
| 102 |
end; |
| 103 |
|
| 104 |
inherited; |
| 105 |
end; |
| 106 |
|
| 107 |
procedure TmbBasicPicker.CMParentColorChanged(var Message: TLMessage); |
| 108 |
begin |
| 109 |
{ |
| 110 |
if ParentColor then |
| 111 |
ControlStyle := ControlStyle - [csOpaque] |
| 112 |
else |
| 113 |
ControlStyle := ControlStyle + [csOpaque]; |
| 114 |
} |
| 115 |
inherited; |
| 116 |
end; |
| 117 |
|
| 118 |
procedure TmbBasicPicker.CreateGradient; |
| 119 |
begin |
| 120 |
// to be implemented by descendants |
| 121 |
end; |
| 122 |
|
| 123 |
procedure TmbBasicPicker.DoChange; |
| 124 |
begin |
| 125 |
if (FLockChange = 0) and Assigned(FOnChange) and (ComponentState = []) then |
| 126 |
FOnChange(self); |
| 127 |
end; |
| 128 |
|
| 129 |
function TmbBasicPicker.GetColorAtPoint(x, y: integer): TColor; |
| 130 |
begin |
| 131 |
Result := Canvas.Pixels[x, y]; // valid for most descendents |
| 132 |
end; |
| 133 |
|
| 134 |
function TmbBasicPicker.GetColorUnderCursor: TColor; |
| 135 |
var |
| 136 |
P: TPoint; |
| 137 |
begin |
| 138 |
P := ScreenToClient(Mouse.CursorPos); |
| 139 |
Result := GetColorAtPoint(P.X, P.Y); |
| 140 |
end; |
| 141 |
|
| 142 |
function TmbBasicPicker.GetGradientColor(AValue: Integer): TColor; |
| 143 |
begin |
| 144 |
Result := clNone; |
| 145 |
end; |
| 146 |
|
| 147 |
function TmbBasicPicker.GetGradientColor2D(X, Y: Integer): TColor; |
| 148 |
begin |
| 149 |
Result := clNone; |
| 150 |
end; |
| 151 |
|
| 152 |
function TmbBasicPicker.GetHexColorAtPoint(X, Y: integer): string; |
| 153 |
begin |
| 154 |
Result := ColorToHex(GetColorAtPoint(x, y)); |
| 155 |
end; |
| 156 |
|
| 157 |
function TmbBasicPicker.GetHexColorUnderCursor: string; |
| 158 |
begin |
| 159 |
Result := ColorToHex(GetColorUnderCursor); |
| 160 |
end; |
| 161 |
|
| 162 |
function TmbBasicPicker.GetHintPos(X, Y: Integer): TPoint; |
| 163 |
begin |
| 164 |
Result := Point(X, Y); |
| 165 |
end; |
| 166 |
|
| 167 |
function TmbBasicPicker.GetHintStr(X, Y: Integer): String; |
| 168 |
begin |
| 169 |
Result := ''; |
| 170 |
if Assigned(FOnGetHintStr) then |
| 171 |
FOnGetHintStr(Self, X, Y, Result); |
| 172 |
end; |
| 173 |
|
| 174 |
function TmbBasicPicker.IsLocked: Boolean; |
| 175 |
begin |
| 176 |
Result := FLockChange > 0; |
| 177 |
end; |
| 178 |
|
| 179 |
procedure TmbBasicPicker.Lock; |
| 180 |
begin |
| 181 |
inc(FLockChange); |
| 182 |
end; |
| 183 |
|
| 184 |
procedure TmbBasicPicker.PaintParentBack; |
| 185 |
begin |
| 186 |
PaintParentBack(Canvas); |
| 187 |
end; |
| 188 |
|
| 189 |
procedure TmbBasicPicker.PaintParentBack(ABitmap: TBitmap); |
| 190 |
begin |
| 191 |
ABitmap.Width := Width; |
| 192 |
ABitmap.Height := Height; |
| 193 |
if Color = clDefault then begin |
| 194 |
ABitmap.Transparent := true; |
| 195 |
ABitmap.TransparentColor := clForm; |
| 196 |
ABitmap.Canvas.Brush.Color := clForm; |
| 197 |
end else |
| 198 |
ABitmap.Canvas.Brush.Color := Color; |
| 199 |
ABitmap.Canvas.FillRect(ABitmap.Canvas.ClipRect); |
| 200 |
end; |
| 201 |
|
| 202 |
procedure TmbBasicPicker.PaintParentBack(ACanvas: TCanvas); |
| 203 |
var |
| 204 |
R: TRect; |
| 205 |
begin |
| 206 |
R := Rect(0, 0, Width, Height); |
| 207 |
PaintParentBack(ACanvas, R); |
| 208 |
end; |
| 209 |
|
| 210 |
procedure TmbBasicPicker.PaintParentBack(ACanvas: TCanvas; ARect: TRect); |
| 211 |
var |
| 212 |
OffScreen: TBitmap; |
| 213 |
begin |
| 214 |
Offscreen := TBitmap.Create; |
| 215 |
try |
| 216 |
if Color = clDefault then begin |
| 217 |
Offscreen.Transparent := true; |
| 218 |
Offscreen.TransparentColor := clForm; |
| 219 |
end; |
| 220 |
Offscreen.Width := WidthOfRect(ARect); |
| 221 |
Offscreen.Height := HeightOfRect(ARect); |
| 222 |
PaintParentBack(Offscreen); |
| 223 |
ACanvas.Draw(ARect.Left, ARect.Top, Offscreen); |
| 224 |
finally |
| 225 |
Offscreen.Free; |
| 226 |
end; |
| 227 |
end; |
| 228 |
|
| 229 |
procedure TmbBasicPicker.Unlock; |
| 230 |
begin |
| 231 |
dec(FLockChange); |
| 232 |
end; |
| 233 |
|
| 234 |
end. |
| 235 |
|