{$I OVC.INC}

{$B-} {Complete Boolean Evaluation}
{$I+} {Input/Output-Checking}
{$P+} {Open Parameters}
{$T-} {Typed @ Operator}
{$W-} {Windows Stack Frame}
{$X+} {Extended Syntax}

{$IFNDEF Win32}
{$G+} {286 Instructions}
{$N+} {Numeric Coprocessor}
{$C MOVEABLE,DEMANDLOAD,DISCARDABLE}
{$ENDIF}

{*********************************************************}
{*                    OVCSC.PAS 2.00                     *}
{*     Copyright (c) TurboPower Software Co 1995-96      *}
{*                 All rights reserved.                  *}
{*********************************************************}

unit OvcSc;
  {-Spin controls}

interface

uses
  {$IFDEF Win32} Windows, {$ELSE} WinTypes, WinProcs, {$ENDIF}
  Buttons, Classes, Controls, Forms, Graphics, Messages,
  OvcBase, OvcConst, OvcData, OvcEf, OvcMisc, OvcExcpt;

type
  TDirection = (dUp, dDown, dRight, dLeft);

  TSpinOrientation = (soVertical, soHorizontal);

  TSpinState = (ssNone,
                ssNormal,
                ssUpBtn,
                ssDownBtn,
                ssLeftBtn,
                ssRightBtn,
                ssCenterBtn);

  TSpinClickEvent = procedure(Sender : TObject; State : TSpinState;
                              Delta : Double; Wrap : Boolean)
                    of object;

const
  DefAcceleration   = 5;
  DefAutoRepeat     = True;
  DefDelayTime      = 500;
  DefDelta          = 1;
  DefDrawStarBorder = False;
  DefForceOddSize   = False;
  DefForceSquare    = False;
  DefHeight         = 25;
  DefMinSize        = 13;
  DefOrientation    = soVertical;
  DefShowArrows     = True;
  DefSize           = 25;
  DefStyle          = bsAutoDetect;
  DefWidth          = 16;
  DefWrapMode       = True;

type
  TOvcBaseSpinner = class(TOvcBase)
  {.Z+}
  protected {private}
    {property variables}
    FAcceleration  : Byte;          {value used to determine acceleration}
    FAutoRepeat    : Boolean;       {repeat if button held}
    FDelta         : Double;        {amount to change by}
    FFocusedControl: TWinControl;   {the control to give the focus to}
    FForceOddSize  : Boolean;       {force height to odd value}
    FShowArrows    : Boolean;
    FStyle         : TButtonStyle;  {determines how buttons look}
    FWrapMode      : Boolean;       {wrap at field bounderies}

    {events}
    FOnClick       : TSpinClickEvent;    {method to call on click events}

    {internal variables}
    scCurrentState : TSpinState;
    scLButton      : Byte;
    scMouseOverBtn : Boolean;
    scNextMsgTime  : LongInt;
    scPrevState    : TSpinState;
    scRepeatCount  : LongInt;

    {property methods}
    procedure SetAcceleration(Value : Byte);
      {-set the Acceleration property}
    procedure SetForceOddSize(Value : Boolean);
      {-set the ForceOddSize property}
    procedure SetShowArrows(Value : Boolean);
      {-set the ShowArrows property}
    procedure SetStyle(Value : TButtonStyle);
      {-set the Style property}

    procedure scDrawArrow(R: TRect; Pressed: Boolean; Direction: TDirection);
      {-utility routine for spinners}


    {windows message handling methods}
    procedure WMGetDlgCode(var Msg : TWMGetDlgCode);
      message WM_GETDLGCODE;
    procedure WMLButtonDown(var Msg : TWMLButtonDown);
      message WM_LBUTTONDOWN;

  protected
    procedure CreateParams(var Params : TCreateParams);
      override;
    procedure CreateWnd;
      override;
    procedure Notification(AComponent : TComponent; Operation : TOperation);
      override;

    procedure scDoMouseDown(XPos, YPos: Integer);
      virtual; abstract;
      {-perform mouse down notification}
    procedure scDoMouseUp;
      virtual; abstract;
      {-perform mouse up actions}
    procedure scUpdateSizes;
      virtual; abstract;
      {-update spin button region sizes}
  {.Z-}

  public
    constructor Create(AOwner : TComponent);
      override;

  published
    {properties}
    property Acceleration : Byte
      read FAcceleration
      write SetAcceleration
      default DefAcceleration;

    property AutoRepeat : Boolean
      read FAutoRepeat
      write FAutoRepeat
      default DefAutoRepeat;

    property Delta : Double
      read FDelta
      write FDelta;

    property FocusedControl : TWinControl
      read FFocusedControl
      write FFocusedControl;

    property ForceOddSize : Boolean
      read FForceOddSize
      write SetForceOddSize
      default DefForceOddSize;

    property WrapMode : Boolean
      read FWrapMode
      write FWrapMode
      default DefWrapMode;

    {inherited properties}
    property Enabled;
    property ParentShowHint;
    property ShowHint;
    property Visible;

    {events}
    property OnClick : TSpinClickEvent
      read FOnClick
      write FOnClick;

    property OnMouseDown;
    property OnMouseMove;
    property OnMouseUp;
  end;

type
  TOvcSpinner = class(TOvcBaseSpinner)
  {.Z+}
  protected {private}
    {property variable}
    FOrientation   : TSpinOrientation;

    {internal variables}
    scCurrentRect  : TRect;
    scDownRect     : TRect;
    scFullRect     : TRect;
    scHeight       : Integer;      {Height of client area}
    scMinBtnSize   : TPoint;
    scUpRect       : TRect;
    scWidth        : Integer;      {Width of client area}

    {internal methods}
    function scCheckMousePos : TSpinState;
    procedure scDoAutoRepeat;
    procedure scDrawButton;

    {property method}
    procedure SetOrientation(Value : TSpinOrientation);

    {windows message handling methods}
    procedure WMLButtonUp(var Msg : TWMLButtonUp);
      message WM_LBUTTONUP;
    procedure WMSize(var Msg : TWMSize);
      message WM_SIZE;

  protected
    procedure Paint;
      override;
    procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
      override;

    {dynamic event wrappers}
    procedure DoOnClick(State : TSpinState);
      dynamic;

    procedure scDoMouseDown(XPos, YPos: Integer);
      override;
    procedure scDoMouseUp;
      override;
    procedure scUpdateSizes;
      override;
  {.Z-}

  published
    property Orientation : TSpinOrientation
      read FOrientation
      write SetOrientation
      default DefOrientation;

    property ShowArrows : Boolean
      read FShowArrows
      write SetShowArrows
      default DefShowArrows;

    property Style : TButtonStyle
      read FStyle
      write SetStyle
      default DefStyle;

  end;

type
  TOvcFourWaySpinner = class(TOvcBaseSpinner)
  {.Z+}
  protected {private}
    {property variables}
    FForceSquare   : Boolean;       {true to resize forcing square}

    {regions for the four spin button sections}
    scUpRgn        : hRgn;
    scDownRgn      : hRgn;
    scLeftRgn      : hRgn;
    scRightRgn     : hRgn;

    {property methods}
    procedure SetForceSquare(Value : Boolean);
      {-sets the ForceSquare property}

    {internal methods}
    function scCheckMousePos : TSpinState;
    procedure scDeleteRegions;
    procedure scDoAutoRepeat;
    procedure scDrawButton(Redraw : Boolean);

    {windows message handling methods}
    procedure WMLButtonUp(var Msg : TWMLButtonUp);
      message WM_LBUTTONUP;
    procedure WMSize(var Msg : TWMSize);
      message WM_SIZE;

  protected
    procedure Paint;
      override;
    procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
      override;

    {dynamic event wrappers}
    procedure DoOnClick(State : TSpinState);
      dynamic;

    procedure scDoMouseDown(XPos, YPos: Integer);
      override;
    procedure scDoMouseUp;
      override;
    procedure scUpdateSizes;
      override;
  {.Z-}

  public
    constructor Create(AOwner : TComponent);
      override;
    destructor Destroy;
      override;

  published
    {properties}
    property ForceSquare : Boolean
      read FForceSquare
      write SetForceSquare
      default DefForceSquare;

    property ShowArrows : Boolean
      read FShowArrows
      write SetShowArrows
      default DefShowArrows;

    property Style : TButtonStyle
      read FStyle
      write SetStyle
      default DefStyle;

  end;

type
  TOvcStarSpinner = class(TOvcBaseSpinner)
  {.Z+}
  protected {private}
    {property variables}
    FDrawStarBorder: Boolean;       {true to draw a border around the star}
    FForceSquare   : Boolean;       {true to resize forcing square}

    {regions for the five spin button sections}
    scUpRgn        : hRgn;
    scDownRgn      : hRgn;
    scLeftRgn      : hRgn;
    scRightRgn     : hRgn;
    scCenterRgn    : hRgn;

    {property methods}
    procedure SetDrawStarBorder(Value : Boolean);
      {-set mthe DrawStarBorder property}
    procedure SetForceSquare(Value : Boolean);
      {-set the ForceSquare property}

    {internal methods}
    function scCheckMousePos : TSpinState;
    procedure scDeleteRegions;
    procedure scDoAutoRepeat;
    procedure scDrawButton;
    procedure scInvalidateButton(State : TSpinState);

    {windows message handling methods}
    procedure WMSize(var Msg : TWMSize);
      message WM_SIZE;

  protected
    procedure Paint;
      override;
    procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
      override;

    {dynamic event wrappers}
    procedure DoOnClick(State : TSpinState);
      dynamic;

    procedure scDoMouseDown(XPos, YPos: Integer);
      override;
    procedure scDoMouseUp;
      override;
    procedure scUpdateSizes;
      override;
  {.Z-}

  public
    constructor Create(AOwner : TComponent);
      override;
    destructor Destroy;
      override;

  published
    {properties}
    property DrawStarBorder : Boolean
      read FDrawStarBorder
      write SetDrawStarBorder
      default DefDrawStarBorder;

    property ForceSquare : Boolean
      read FForceSquare
      write SetForceSquare
      default DefForceSquare;
  end;


implementation
