customdrawn: Implements Orientation for TCDTrackBar

git-svn-id: trunk@33410 -
This commit is contained in:
sekelsenmat 2011-11-08 10:12:43 +00:00
parent 730c9a5206
commit 495c99b66c
3 changed files with 144 additions and 55 deletions

View File

@ -12,7 +12,7 @@ uses
// LCL -> Use only TForm, TWinControl, TCanvas and TLazIntfImage // LCL -> Use only TForm, TWinControl, TCanvas and TLazIntfImage
Graphics, Controls, LCLType, Graphics, Controls, LCLType,
// Others only for types // Others only for types
StdCtrls, StdCtrls, ComCtrls,
// //
customdrawndrawers; customdrawndrawers;
@ -38,6 +38,7 @@ type
procedure DrawRaisedFrame(ADest: TCanvas; ADestPos: TPoint; ASize: TSize); override; procedure DrawRaisedFrame(ADest: TCanvas; ADestPos: TPoint; ASize: TSize); override;
procedure DrawSunkenFrame(ADest: TCanvas; ADestPos: TPoint; ASize: TSize); override; procedure DrawSunkenFrame(ADest: TCanvas; ADestPos: TPoint; ASize: TSize); override;
procedure DrawTickmark(ADest: TCanvas; ADestPos: TPoint); override; procedure DrawTickmark(ADest: TCanvas; ADestPos: TPoint); override;
procedure DrawSlider(ADest: TCanvas; ADestPos: TPoint; ASize: TSize; AOrientation: TTrackBarOrientation); override;
// =================================== // ===================================
// Standard Tab // Standard Tab
// =================================== // ===================================
@ -286,6 +287,72 @@ begin
ADest.Line(ADestPos.X+2+i, ADestPos.Y+2+6-i, ADestPos.X+2+i, ADestPos.Y+5+6-i); ADest.Line(ADestPos.X+2+i, ADestPos.Y+2+6-i, ADestPos.X+2+i, ADestPos.Y+5+6-i);
end; end;
procedure TCDDrawerCommon.DrawSlider(ADest: TCanvas; ADestPos: TPoint;
ASize: TSize; AOrientation: TTrackBarOrientation);
var
lPoints: array[0..4] of TPoint;
lSliderBottom: Integer;
begin
ADest.Brush.Color := Palette.BtnFace;
ADest.Brush.Style := bsSolid;
ADest.Pen.Color := WIN2000_FRAME_WHITE;
if AOrientation = trHorizontal then
begin
lSliderBottom := ADestPos.Y+ASize.CY;
// outter white frame
lPoints[0] := Point(ADestPos.X+5, lSliderBottom);
lPoints[1] := Point(ADestPos.X, lSliderBottom-5);
lPoints[2] := Point(ADestPos.X, ADestPos.Y);
lPoints[3] := Point(ADestPos.X+10, ADestPos.Y);
lPoints[4] := Point(ADestPos.X+10, lSliderBottom-5);
ADest.Polygon(lPoints);
// left-top inner frame
ADest.Pen.Color := WIN2000_FRAME_LIGHT_GRAY;
ADest.MoveTo(ADestPos.X+5, lSliderBottom-1);
ADest.LineTo(ADestPos.X+1, lSliderBottom-5);
ADest.LineTo(ADestPos.X+1, ADestPos.Y+1);
ADest.LineTo(ADestPos.X+9, ADestPos.Y+1);
// right inner frame
ADest.Pen.Color := WIN2000_FRAME_GRAY;
ADest.MoveTo(ADestPos.X+5, lSliderBottom-1);
ADest.LineTo(ADestPos.X+9, lSliderBottom-5);
ADest.LineTo(ADestPos.X+9, ADestPos.Y);
// right outter frame
ADest.Pen.Color := WIN2000_FRAME_DARK_GRAY;
ADest.MoveTo(ADestPos.X+5, lSliderBottom);
ADest.LineTo(ADestPos.X+10, lSliderBottom-5);
ADest.LineTo(ADestPos.X+10, ADestPos.Y-1);
end
else
begin
lSliderBottom := ADestPos.Y+ASize.CY;
// outter white frame
lPoints[0] := Point(lSliderBottom, ADestPos.X+5);
lPoints[1] := Point(lSliderBottom-5, ADestPos.X);
lPoints[2] := Point(ADestPos.Y, ADestPos.X);
lPoints[3] := Point(ADestPos.Y, ADestPos.X+10);
lPoints[4] := Point(lSliderBottom-5, ADestPos.X+10);
ADest.Polygon(lPoints);
// left-top inner frame
ADest.Pen.Color := WIN2000_FRAME_LIGHT_GRAY;
ADest.MoveTo(lSliderBottom-1, ADestPos.X+5);
ADest.LineTo(lSliderBottom-5, ADestPos.X+1);
ADest.LineTo(ADestPos.Y+1, ADestPos.X+1);
ADest.LineTo(ADestPos.Y+1, ADestPos.X+9);
// right inner frame
ADest.Pen.Color := WIN2000_FRAME_GRAY;
ADest.MoveTo(lSliderBottom-1, ADestPos.X+5);
ADest.LineTo(lSliderBottom-5, ADestPos.X+9);
ADest.LineTo(ADestPos.Y, ADestPos.X+9);
// right outter frame
ADest.Pen.Color := WIN2000_FRAME_DARK_GRAY;
ADest.MoveTo(lSliderBottom, ADestPos.X+5);
ADest.LineTo(lSliderBottom-5, ADestPos.X+10);
ADest.LineTo(ADestPos.Y-1, ADestPos.X+10);
end;
end;
procedure TCDDrawerCommon.DrawButton(ADest: TCanvas; ADestPos: TPoint; procedure TCDDrawerCommon.DrawButton(ADest: TCanvas; ADestPos: TPoint;
ASize: TSize; AState: TCDControlState; AStateEx: TCDControlStateEx); ASize: TSize; AState: TCDControlState; AStateEx: TCDControlStateEx);
var var
@ -708,17 +775,18 @@ end;
procedure TCDDrawerCommon.DrawTrackBar(ADest: TCanvas; ADestPos: TPoint; procedure TCDDrawerCommon.DrawTrackBar(ADest: TCanvas; ADestPos: TPoint;
ASize: TSize; AState: TCDControlState; AStateEx: TCDTrackBarStateEx); ASize: TSize; AState: TCDControlState; AStateEx: TCDTrackBarStateEx);
var var
lDrawingBottom, StepsCount, i: Integer; StepsCount, i: Integer;
lTickmarkLeft, lTickmarkTop: integer; // for drawing the decorative bars lTickmarkLeft, lTickmarkTop: integer; // for drawing the decorative bars
dRect: TRect; dRect: TRect;
pStepWidth, CDBarEdge: Integer; pStepWidth, CDBarSpacing: Integer;
lPoints: array[0..4] of TPoint;
lSliderBottom: Integer;
lPoint: TPoint; lPoint: TPoint;
lSize: TSize; lSize, lMeasureSize: TSize;
begin begin
CDBarEdge := GetMeasures(TCDTRACKBAR_LEFT_SPACING) // The orientation i
+ GetMeasures(TCDTRACKBAR_RIGHT_SPACING); if AStateEx.Orientation = trHorizontal then lMeasureSize := ASize
else lMeasureSize := Size(ASize.CY, ASize.CX);
CDBarSpacing := GetMeasures(TCDTRACKBAR_LEFT_SPACING) + GetMeasures(TCDTRACKBAR_RIGHT_SPACING);
// Sanity check // Sanity check
if AStateEx.Max - AStateEx.Min <= 0 then if AStateEx.Max - AStateEx.Min <= 0 then
@ -726,12 +794,9 @@ begin
// Preparations // Preparations
StepsCount := AStateEx.Max - AStateEx.Min + 1; StepsCount := AStateEx.Max - AStateEx.Min + 1;
if StepsCount > 0 then pStepWidth := (ASize.cx - CDBarEdge) div (StepsCount-1) if StepsCount > 0 then pStepWidth := (lMeasureSize.cx - CDBarSpacing) div (StepsCount-1)
else pStepWidth := 0; else pStepWidth := 0;
// The bottom part of the drawing
lDrawingBottom := ASize.cy - 10;
// Background // Background
ADest.Brush.Color := AStateEx.ParentRGBColor; ADest.Brush.Color := AStateEx.ParentRGBColor;
@ -741,9 +806,18 @@ begin
ADest.Rectangle(0, 0, ASize.cx, ASize.cy); ADest.Rectangle(0, 0, ASize.cx, ASize.cy);
// Draws the frame and its inner white area // Draws the frame and its inner white area
lPoint := Point(ADestPos.X + GetMeasures(TCDTRACKBAR_LEFT_SPACING), if AStateEx.Orientation = trHorizontal then
ADestPos.Y + GetMeasures(TCDTRACKBAR_TOP_SPACING)); begin
lSize := Size(ASize.CX - CDBarEdge, GetMeasures(TCDTRACKBAR_FRAME_HEIGHT)); lPoint := Point(ADestPos.X + GetMeasures(TCDTRACKBAR_LEFT_SPACING),
ADestPos.Y + GetMeasures(TCDTRACKBAR_TOP_SPACING));
lSize := Size(ASize.CX - CDBarSpacing, GetMeasures(TCDTRACKBAR_FRAME_HEIGHT));
end
else
begin
lPoint := Point(ADestPos.X + GetMeasures(TCDTRACKBAR_TOP_SPACING),
ADestPos.Y + GetMeasures(TCDTRACKBAR_LEFT_SPACING));
lSize := Size(GetMeasures(TCDTRACKBAR_FRAME_HEIGHT), ASize.CY - CDBarSpacing);
end;
ADest.Brush.Color := Palette.Window; ADest.Brush.Color := Palette.Window;
ADest.Pen.Style := psClear; ADest.Pen.Style := psClear;
ADest.Rectangle(Bounds(lPoint.X, lPoint.Y, lSize.cx, lSize.cy)); ADest.Rectangle(Bounds(lPoint.X, lPoint.Y, lSize.cx, lSize.cy));
@ -756,39 +830,17 @@ begin
for i := 0 to StepsCount - 1 do for i := 0 to StepsCount - 1 do
begin begin
ADest.Pen.Color := clBlack; ADest.Pen.Color := clBlack;
ADest.Line(lTickmarkLeft, lTickmarkTop, lTickmarkLeft, lTickmarkTop+3); if AStateEx.Orientation = trHorizontal then
ADest.Line(lTickmarkLeft, lTickmarkTop, lTickmarkLeft, lTickmarkTop+3)
else
ADest.Line(lTickmarkTop, lTickmarkLeft, lTickmarkTop+3, lTickmarkLeft);
// Draw the slider // Draw the slider
if i + AStateEx.Min = AStateEx.Position then if i + AStateEx.Min = AStateEx.Position then
begin DrawSlider(ADest,
ADest.Brush.Color := Palette.BtnFace; Point(lTickmarkLeft-5, GetMeasures(TCDTRACKBAR_TOP_SPACING)-2),
ADest.Brush.Style := bsSolid; Size(11, GetMeasures(TCDTRACKBAR_FRAME_HEIGHT)+5), AStateEx.Orientation);
ADest.Pen.Color := WIN2000_FRAME_WHITE;
lSliderBottom := GetMeasures(TCDTRACKBAR_TOP_SPACING) + GetMeasures(TCDTRACKBAR_FRAME_HEIGHT)+4;
lPoints[0] := Point(lTickmarkLeft, lSliderBottom);
lPoints[1] := Point(lTickmarkLeft-5, GetMeasures(TCDTRACKBAR_TOP_SPACING) + GetMeasures(TCDTRACKBAR_FRAME_HEIGHT)-1);
lPoints[2] := Point(lTickmarkLeft-5, GetMeasures(TCDTRACKBAR_TOP_SPACING)-2);
lPoints[3] := Point(lTickmarkLeft+5, GetMeasures(TCDTRACKBAR_TOP_SPACING)-2);
lPoints[4] := Point(lTickmarkLeft+5, GetMeasures(TCDTRACKBAR_TOP_SPACING) + GetMeasures(TCDTRACKBAR_FRAME_HEIGHT)-1);
ADest.Polygon(lPoints);
// left-top inner frame
ADest.Pen.Color := WIN2000_FRAME_LIGHT_GRAY;
ADest.MoveTo(lTickmarkLeft, lSliderBottom-1);
ADest.LineTo(lTickmarkLeft-4, GetMeasures(TCDTRACKBAR_TOP_SPACING) + GetMeasures(TCDTRACKBAR_FRAME_HEIGHT)-1);
ADest.LineTo(lTickmarkLeft-4, GetMeasures(TCDTRACKBAR_TOP_SPACING)-1);
ADest.LineTo(lTickmarkLeft+5, GetMeasures(TCDTRACKBAR_TOP_SPACING)-1);
// right inner frame
ADest.Pen.Color := WIN2000_FRAME_GRAY;
ADest.MoveTo(lTickmarkLeft, lSliderBottom-1);
ADest.LineTo(lTickmarkLeft+4, GetMeasures(TCDTRACKBAR_TOP_SPACING) + GetMeasures(TCDTRACKBAR_FRAME_HEIGHT)-1);
ADest.LineTo(lTickmarkLeft+4, GetMeasures(TCDTRACKBAR_TOP_SPACING)-2);
// right outter frame
ADest.Pen.Color := WIN2000_FRAME_DARK_GRAY;
ADest.MoveTo(lTickmarkLeft, lSliderBottom);
ADest.LineTo(lTickmarkLeft+5, GetMeasures(TCDTRACKBAR_TOP_SPACING) + GetMeasures(TCDTRACKBAR_FRAME_HEIGHT)-1);
ADest.LineTo(lTickmarkLeft+5, GetMeasures(TCDTRACKBAR_TOP_SPACING)-3);
end;
lTickmarkLeft := lTickmarkLeft + pStepWidth; lTickmarkLeft := lTickmarkLeft + pStepWidth;
end; end;

View File

@ -275,10 +275,12 @@ type
// fields // fields
FMin: integer; FMin: integer;
FMax: integer; FMax: integer;
FOrientation: TTrackBarOrientation;
FPosition: integer; FPosition: integer;
FOnChange: TNotifyEvent; FOnChange: TNotifyEvent;
procedure SetMax(Value: integer); procedure SetMax(Value: integer);
procedure SetMin(Value: integer); procedure SetMin(Value: integer);
procedure SetOrientation(AValue: TTrackBarOrientation);
procedure SetPosition(Value: integer); procedure SetPosition(Value: integer);
// //
function GetPositionFromMousePos(X, Y: Integer): integer; function GetPositionFromMousePos(X, Y: Integer): integer;
@ -310,6 +312,7 @@ type
property Max: integer read FMax write SetMax default 10; property Max: integer read FMax write SetMax default 10;
property Min: integer read FMin write SetMin default 0; property Min: integer read FMin write SetMin default 0;
property OnChange: TNotifyEvent read FOnChange write FOnChange; property OnChange: TNotifyEvent read FOnChange write FOnChange;
property Orientation: TTrackBarOrientation read FOrientation write SetOrientation default trHorizontal;
property Position: integer read FPosition write SetPosition; property Position: integer read FPosition write SetPosition;
property TabStop default True; property TabStop default True;
end; end;
@ -1180,17 +1183,38 @@ end;
procedure TCDTrackBar.SetMax(Value: integer); procedure TCDTrackBar.SetMax(Value: integer);
begin begin
if Value = FMax then if Value = FMax then Exit;
Exit;
FMax := Value; FMax := Value;
PrepareControlStateEx;
Invalidate; Invalidate;
end; end;
procedure TCDTrackBar.SetMin(Value: integer); procedure TCDTrackBar.SetMin(Value: integer);
begin begin
if Value = FMin then if Value = FMin then Exit;
Exit;
FMin := Value; FMin := Value;
PrepareControlStateEx;
Invalidate;
end;
procedure TCDTrackBar.SetOrientation(AValue: TTrackBarOrientation);
var
lOldWidth: Integer;
begin
if FOrientation=AValue then Exit;
// Invert the width and the height, but not if the property comes from the LFM
// because the width was already inverted in the designer and stored in the new value
if not (csLoading in ComponentState) then
begin
lOldWidth := Width;
Width := Height;
Height := lOldWidth;
end;
// Set the property and redraw
FOrientation:=AValue;
PrepareControlStateEx;
Invalidate; Invalidate;
end; end;
@ -1198,19 +1222,32 @@ procedure TCDTrackBar.SetPosition(Value: integer);
begin begin
if Value = FPosition then Exit; if Value = FPosition then Exit;
FPosition := Value; FPosition := Value;
PrepareControlStateEx;
Invalidate; Invalidate;
end; end;
function TCDTrackBar.GetPositionFromMousePos(X, Y: integer): integer; function TCDTrackBar.GetPositionFromMousePos(X, Y: integer): integer;
var var
lLeftBorder, lRightBorder: Integer; lLeftBorder, lRightBorder: Integer;
lCoord, lSize: Integer;
begin begin
lLeftBorder := FDrawer.GetMeasures(TCDTRACKBAR_LEFT_SPACING); lLeftBorder := FDrawer.GetMeasures(TCDTRACKBAR_LEFT_SPACING);
lRightBorder := FDrawer.GetMeasures(TCDTRACKBAR_RIGHT_SPACING); lRightBorder := FDrawer.GetMeasures(TCDTRACKBAR_RIGHT_SPACING);
if X > Width - lRightBorder then Result := FMax if FOrientation = trHorizontal then
else if X < lLeftBorder then Result := FMin begin
else Result := FMin + (X - lLeftBorder) * (FMax - FMin + 1) div (Width - lRightBorder - lLeftBorder); lCoord := X;
lSize := Width;
end
else
begin
lCoord := Y;
lSize := Height;
end;
if lCoord > lSize - lRightBorder then Result := FMax
else if lCoord < lLeftBorder then Result := FMin
else Result := FMin + (lCoord - lLeftBorder) * (FMax - FMin + 1) div (lSize - lRightBorder - lLeftBorder);
// sanity check // sanity check
if Result > FMax then Result := FMax; if Result > FMax then Result := FMax;
@ -1234,6 +1271,7 @@ begin
FTBState.Min := FMin; FTBState.Min := FMin;
FTBState.Max := FMax; FTBState.Max := FMax;
FTBState.Position := FPosition; FTBState.Position := FPosition;
FTBState.Orientation := FOrientation;
end; end;
procedure TCDTrackBar.Changed; procedure TCDTrackBar.Changed;
@ -1283,11 +1321,8 @@ var
NewPosition: Integer; NewPosition: Integer;
begin begin
SetFocus; SetFocus;
NewPosition := GetPositionFromMousePos(X, Y); NewPosition := GetPositionFromMousePos(X, Y);
DragDropStarted := True; DragDropStarted := True;
Position := NewPosition; Position := NewPosition;
inherited MouseDown(Button, Shift, X, Y); inherited MouseDown(Button, Shift, X, Y);

View File

@ -124,6 +124,7 @@ type
Min: integer; Min: integer;
Max: integer; Max: integer;
Position: integer; Position: integer;
Orientation: TTrackBarOrientation;
end; end;
TCDCTabControlStateEx = class(TCDControlStateEx) TCDCTabControlStateEx = class(TCDControlStateEx)
@ -212,6 +213,7 @@ type
procedure DrawRaisedFrame(ADest: TCanvas; ADestPos: TPoint; ASize: TSize); virtual; abstract; procedure DrawRaisedFrame(ADest: TCanvas; ADestPos: TPoint; ASize: TSize); virtual; abstract;
procedure DrawSunkenFrame(ADest: TCanvas; ADestPos: TPoint; ASize: TSize); virtual; abstract; procedure DrawSunkenFrame(ADest: TCanvas; ADestPos: TPoint; ASize: TSize); virtual; abstract;
procedure DrawTickmark(ADest: TCanvas; ADestPos: TPoint); virtual; abstract; procedure DrawTickmark(ADest: TCanvas; ADestPos: TPoint); virtual; abstract;
procedure DrawSlider(ADest: TCanvas; ADestPos: TPoint; ASize: TSize; AOrientation: TTrackBarOrientation); virtual; abstract;
// TCDButton // TCDButton
procedure DrawButton(ADest: TCanvas; ADestPos: TPoint; ASize: TSize; procedure DrawButton(ADest: TCanvas; ADestPos: TPoint; ASize: TSize;
AState: TCDControlState; AStateEx: TCDControlStateEx); virtual; abstract; AState: TCDControlState; AStateEx: TCDControlStateEx); virtual; abstract;