1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
{
*********************************************************************
This file is part of the Lazarus Component Library (LCL)
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*********************************************************************
Author: H. Page-Clark
Abstract:
Show an arrow. Its size, direction, color and shadow can be adjusted.
}
unit Arrow;
{$mode objfpc}{$H+}
interface
uses
Classes, types, math, Controls, Graphics, IndustrialBase;
type
TArrowType = (atUp, atDown, atLeft, atRight);
TShadowType = (stNone, stIn, stOut, stEtchedIn, stEtchedOut, stFilled);
TTriPts = (ptA, ptB, ptC);
TTrianglePoints = array[TTriPts] of TPoint;
{ TArrow }
TArrow = class(TIndustrialBase)
private
FArrowColor: TColor;
FArrowType: TArrowType;
FArrowAngle: integer;
FShadowType: TShadowType;
FShadowColor: TColor;
FR: TRect;
FT: TTrianglePoints;
procedure CalcTrianglePoints;
procedure SetArrowAngle(AValue: integer);
procedure SetArrowColor(AValue: TColor);
procedure SetArrowType(AValue: TArrowType);
procedure SetShadowColor(AValue: TColor);
procedure SetShadowType(AValue: TShadowType);
protected
class function GetControlClassDefaultSize: TSize; override;
procedure Paint; override;
public
constructor Create(aOwner: TComponent); override;
published
property Align;
property Anchors;
property ArrowColor: TColor read FArrowColor write SetArrowColor default clBlack;
property ArrowType: TArrowType read FArrowType write SetArrowType default atLeft;
property ArrowPointerAngle: integer read FArrowAngle write SetArrowAngle default 60;
property BorderSpacing;
property Color;
property Constraints;
property Enabled;
property Hint;
property OnChangeBounds;
property OnClick;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDrag;
property OnMouseDown;
property OnMouseEnter;
property OnMouseLeave;
property OnMouseMove;
property OnMouseUp;
property OnMouseWheel;
property OnMouseWheelDown;
property OnMouseWheelUp;
property OnMouseWheelHorz;
property OnMouseWheelLeft;
property OnMouseWheelRight;
property OnPaint;
property OnResize;
property OnStartDrag;
property ParentColor;
property ParentShowHint;
property PopupMenu;
property ShadowType: TShadowType read FShadowType write SetShadowType default stEtchedIn;
property ShadowColor: TColor read FShadowColor write SetShadowColor default cl3DShadow;
property ShowHint;
property Visible;
end;
procedure Register;
implementation
const
cDefaultControlSize = 20;
cMinArrowSize = 8;
cMinAngle = 20;
cMaxAngle = 160;
cShadowColors: array[TShadowType] of TColor =
(clWindow, cl3DShadow, cl3DShadow, cl3DHiLight, cl3DHiLight, clBlue{not used});
cInnerOffset = 2;
cShadowSize = 2; //must be <= cInnerOffset
procedure Register;
begin
RegisterComponents('Misc',[TArrow]);
end;
{ TArrow }
procedure TArrow.CalcTrianglePoints;
var
midY, midX: integer;
ratioNeed, ratioThis: double;
size: TSize;
begin
FR:= ClientRect;
InflateRect(FR, -cInnerOffset, -cInnerOffset);
Dec(FR.Bottom); // for "filled" shadow
midX:= (FR.Left + FR.Right) div 2;
midY:= (FR.Top + FR.Bottom) div 2;
size:= Types.Size(FR);
ratioNeed:= 2*Tan(FArrowAngle*pi/(180*2));
if FArrowType in [atLeft, atRight] then
ratioNeed:= 1/ratioNeed;
ratioThis:= size.cx/size.cy;
if ratioThis>=ratioNeed then
size.cx:= Trunc(size.cx*ratioNeed/ratioThis)
else
size.cy:= Trunc(size.cy*ratioThis/ratioNeed);
FR.Top:= midY - size.cy div 2;
FR.Bottom:= FR.Top + size.cy;
FR.Left:= midX - size.cx div 2;
FR.Right:= FR.Left + size.cx;
// angle=90: 1pixel shift appears (reason: float math)
// workaround:
if FArrowAngle=90 then
begin
if FArrowType in [atUp, atDown] then
begin
FR.Left:= midX-size.cy;
FR.Right:= midX+size.cy;
end
else
begin
FR.Top:= midY-size.cx;
FR.Bottom:= midY+size.cx;
end;
end;
case FArrowType of
atUp: begin
FT[ptC] := Point(midX, FR.Top);
FT[ptA] := Point(FR.Left, FR.Bottom);
FT[ptB] := FR.BottomRight;
end;
atDown: begin
FT[ptA] := FR.TopLeft;
FT[ptB] := Point(FR.Right, FR.Top);
FT[ptC] := Point(midX, FR.Bottom);
end;
atLeft: begin
FT[ptA] := Point(FR.Right, FR.Top);
FT[ptB] := FR.BottomRight;
FT[ptC] := Point(FR.Left, midY);
end;
atRight: begin
FT[ptA] := FR.TopLeft;
FT[ptB] := Point(FR.Right, midY);
FT[ptC] := Point(FR.Left, FR.Bottom);
end;
end;
end;
procedure TArrow.SetArrowColor(AValue: TColor);
begin
if FArrowColor=AValue then Exit;
FArrowColor:=AValue;
GraphicChanged;
end;
procedure TArrow.SetArrowType(AValue: TArrowType);
begin
if FArrowType=AValue then Exit;
FArrowType:=AValue;
GraphicChanged;
end;
procedure TArrow.SetShadowColor(AValue: TColor);
begin
if FShadowColor=AValue then Exit;
FShadowColor:= AValue;
GraphicChanged;
end;
procedure TArrow.SetArrowAngle(AValue: integer);
begin
if FArrowAngle=AValue then Exit;
FArrowAngle:=Max(Min(AValue, cMaxAngle), cMinAngle);
GraphicChanged;
end;
procedure TArrow.SetShadowType(AValue: TShadowType);
begin
if FShadowType=AValue then Exit;
FShadowType:=AValue;
GraphicChanged;
end;
class function TArrow.GetControlClassDefaultSize: TSize;
begin
Result.cx:=cDefaultControlSize;
Result.cy:=cDefaultControlSize;
end;
procedure TArrow.Paint;
procedure Offset(var ptA, ptB: TPoint);
begin
case FArrowType of
atUp: begin Inc(ptA.x); Dec(ptA.y); Inc(ptB.x); Dec(ptB.y); end;
atDown: begin Inc(ptA.x); Inc(ptA.y); Inc(ptB.x); Inc(ptB.y); end;
atLeft: begin Dec(ptA.x); Inc(ptA.y); Dec(ptB.x); Inc(ptB.y); end;
atRight: begin Inc(ptA.x); Inc(ptA.y); Inc(ptB.x); Inc(ptB.y); end;
end;
end;
procedure ShadowLine(p1, p2: TPoint);
begin
Canvas.Pen.Color:= cShadowColors[FShadowType];
Canvas.MoveTo(p1);
Canvas.LineTo(p2);
Offset(p1, p2);
Canvas.Pen.Color:= FShadowColor;
Canvas.MoveTo(p1);
Canvas.LineTo(p2);
if (Height>13) then
begin
Offset(p1, p2);
Canvas.MoveTo(p1);
Canvas.LineTo(p2);
end;
end;
procedure ShadowTriangle;
var
Pts: TTrianglePoints;
begin
Pts:= FT;
Inc(Pts[ptA].x, cShadowSize);
Inc(Pts[ptA].y, cShadowSize);
Inc(Pts[ptB].x, cShadowSize);
Inc(Pts[ptB].y, cShadowSize);
Inc(Pts[ptC].x, cShadowSize);
Inc(Pts[ptC].y, cShadowSize);
Canvas.Pen.Color:= FShadowColor;
Canvas.Brush.Color:= FShadowColor;
Canvas.Polygon(Pts);
end;
begin
CalcTrianglePoints;
Canvas.AntialiasingMode := AntiAliasingMode;
// Paint background
Canvas.Brush.Color := Color;
Canvas.FillRect(ClientRect);
// Paint shadow area
if (FShadowType=stFilled) then
ShadowTriangle;
// Paint arrow
Canvas.Pen.Color:= FArrowColor;
Canvas.Brush.Color:= FArrowColor;
Canvas.Polygon(FT);
if not (FShadowType in [stNone, stFilled]) then
ShadowLine(FT[ptB], FT[ptC]);
inherited Paint;
end;
constructor TArrow.Create(aOwner: TComponent);
begin
inherited Create(aOwner);
Constraints.MinHeight:= cMinArrowSize;
Constraints.MinWidth:= cMinArrowSize;
FArrowType:= atLeft; // set defaults to match TArrow component
FArrowAngle:= 60; // angle of equal side triangle
FShadowType:= stEtchedIn;
FShadowColor:= cl3DShadow;
FArrowColor:= clBlack;
end;
end.
|