{$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}

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

unit OvcAe;
  {-array editors using simple, picture and numeric edit fields}

interface

uses
  {$IFDEF Win32} Windows, {$ELSE} WinTypes, WinProcs, {$ENDIF}
  Classes, Controls, DsgnIntf, Forms, Graphics,
  Menus, Messages, SysUtils,
  OvcNf, OvcPf, OvcSf, OvcColor, OvcMisc, OvcExcpt,
  OvcBase, OvcCmd, OvcConst, OvcData, OvcEf, OvcStr;

const
  {default property values}
  DefActiveIndex   = 0;
  DefAlign         = alNone;
  DefBorderStyle   = bsSingle;
  DefColor         = clWindow;
  DefCtl3D         = True;
  DefDecimalPlaces = 0;
  DefHeight        = 150;
  DefHighlightBack = clHighlight;
  DefHighlightText = clHighlightText;
  DefLineColor     = clSilver;
  DefMaxLength     = 10;
  DefNumItems      = 0;
  DefNumRows       = 0;
  DefParentColor   = False;
  DefParentCtl3D   = True;
  DefParentFont    = False;
  DefReadOnly      = False;
  {$IFDEF Win32}
  DefRowHeight       = 17;
  {$ELSE}
  DefRowHeight       = 20;
  {$ENDIF}
  DefUseScrollBar  = True;
  DefTabStop       = True;
  DefTopIndex      = 0;
  DefWidth         = 100;

type
  {event to get a pointer to the cell's value}
  TGetItemEvent =
    procedure(Sender : TObject; Index : LongInt; var Value : Pointer)
    of object;
  {event to get color of the item cell}
  TGetItemColorEvent =
    procedure(Sender : TObject; Index : LongInt; var FG, BG : TColor)
    of object;
  {event to notify of a scroll action}
  TSelectEvent =
    procedure(Sender : TObject; NewIndex : LongInt)
    of object;

type
  {base class for array editors}
  TOvcBaseArrayEditor = class(TOvcBase)
  {.Z+}
  protected {private}
    {property variables}
    FActiveIndex       : LongInt;       {the focused cell}
    FAutoAdvanceChar   : Boolean;       {focus moves to next field after last character entered}
    FAutoAdvanceLR     : Boolean;       {L/R cursor movement at field ends moves focus to next/prev control}
    FBeepOnError       : Boolean;       {MessageBeep is called for error conditions}
    FBorderStyle       : TBorderStyle;  {border around the control}
    FHighlightColors   : TOvcColors;    {highlight colors}
    FInsertPushes      : Boolean;       {inserted chars push ending chars off}
    FLineColor         : TColor;        {color of row divider lines}
    FNumItems          : LongInt;       {total elements in array}
    FPadChar           : AnsiChar;      {character used to pad end of string}
    FReadOnly          : Boolean;       {prevents field editing}
    FRightJustify      : Boolean;       {display text right-justified}
    FRowHeight         : Integer;       {pixel height of one row}
    FTextMargin        : Integer;       {indent from left (right)}
    FTrimBlanks        : Boolean;       {remove leading and trailing blanks if not listerals}
    FUseScrollBar      : Boolean;       {true to use virtical scroll bar}

    {event variables}
    FOnChange          : TNotifyEvent;  {change notification}
    FOnError           : TValidationErrorEvent;
    FOnGetItem         : TGetItemEvent; {method to call to get item pointer}
    FOnGetItemColor    : TGetItemColorEvent;
    FOnSelect          : TSelectEvent;  {scroll notification}
    FOnUserCommand     : TUserCommandEvent;
    FOnUserValidation  : TUserValidationEvent;

    {internal/working variables}
    aeCell             : TOvcBaseEntryField;{abstract edit cell object}
    aeDivisor          : LongInt;       {divisor for scroll bar}
    aeHighIndex        : LongInt;       {highest index value}
    aeItemPtr          : Pointer;       {pointer to data element}
    aeNumRows          : Integer;       {visible rows in window}
    aeRangeLo          : TRangeType;    {low field range limit}
    aeRangeHi          : TRangeType;    {high field range limit}
    aeRangeLoaded      : Boolean;       {flag to tell when loaded}
    aeTopIndex         : LongInt;       {index of the top item}
    aeVSHigh           : Integer;       {vertical scroll limit}

    {variables to transfer to the edit cell field}
    aeDecimalPlaces    : Byte;          {max decimal places}
    aeMaxLength        : Word;          {maximum length of string}

    {property methods}
    function GetRangeHi : string;
      {-get the high field range string value}
    function GetRangeLo : string;
      {-get the low field range string value}
    procedure SetAutoAdvanceChar(Value : Boolean);
      {-set the AutoAdvanceChar option}
    procedure SetAutoAdvanceLeftRight(Value : Boolean);
      {-set the AutoAdvanceLeftRight option}
    procedure SetBeepOnError(Value : Boolean);
      {-sets the BeepOnError option}
    procedure SetBorderStyle(const Value : TBorderStyle);
      {-set the style used for the border}
    procedure SetInsertPushes(Value : Boolean);
      {-set the InsertPushes option}
    procedure SetLineColor(Value : TColor);
      {-set the color used to draw the row divider lines}
    procedure SetNumItems(Value : LongInt);
      {-set the number of items in the array}
    procedure SetPadChar(Value : AnsiChar);
      {-set the character used to pad the end of the edit string}
    procedure SetRangeHi(const Value : string);
      {-set the high field range from a string value}
    procedure SetRangeLo(const Value : string);
      {-set the low field range from a string value}
    procedure SetReadOnly(Value : Boolean);
      {-set the read only option}
    procedure SetRightJustify(Value : Boolean);
      {-set the right justify option}
    procedure SetRowHeight(Value : Integer);
      {-set the cell row height}
    procedure SetTextMargin(Value : Integer);
      {-set the text margin}
    procedure SetTrimBlanks(Value : Boolean);
      {-set the trim blanks option}
    procedure SetUseScrollBar(Value : Boolean);
      {-set use of vertical scroll bar}

    {general (internal) routines}
    procedure aeAdjustIntegralHeight;
      {-resizes the component so no partial items appear}
    procedure aeColorChanged(AColor : TObject);
      {-highlight color change-repaint}
    function aeMakeItemVisible(Index : LongInt) : Boolean;
      {-displays the Index item, scrolling as required}
    procedure aePreFocusProcess;
      {-get ready to receive focus}
    procedure aeReadRangeHi(Stream : TStream);
      {-called to read the high range from the stream}
    procedure aeReadRangeLo(Stream : TStream);
      {-called to read the low range from the stream}
    function aeScaleDown(N : LongInt) : SmallInt;
      {-returns a scaled down scroll bar value}
    function aeScaleUp(N : SmallInt) : LongInt;
      {-returns a scaled up scroll bar value}
    procedure aeSetVScrollPos;
      {-sets the vertical scroll bar position}
    procedure aeSetVScrollRange;
      {-sets the horizontal scroll bar position}
    procedure aeUpdateDisplay(Scrolled : Boolean; OldItem, NewItem : LongInt);
      {-invalidate or scroll necessary region}
    procedure aeWriteRangeHi(Stream : TStream);
      {-called to store the high range on the stream}
    procedure aeWriteRangeLo(Stream : TStream);
      {-called to store the low range on the stream}

    {VCL control messages}
    procedure CMFontChanged(var Msg : TMessage);
      message CM_FONTCHANGED;
    procedure CMGotFocus(var Msg : TMessage);
      message CM_GOTFOCUS;
    procedure CMLostFocus(var Msg : TMessage);
      message CM_LOSTFOCUS;

    {windows message methods}
    procedure WMEraseBkGnd(var Msg : TWMEraseBkGnd);
      message WM_ERASEBKGND;
    procedure WMLButtonDown(var Msg : TWMLButtonDown);
      message WM_LBUTTONDOWN;
    procedure WMMouseActivate(var Msg : TWMMouseActivate);
      message WM_MOUSEACTIVATE;
    procedure WMSetFocus(var Msg : TWMSetFocus);
      message WM_SETFOCUS;
    procedure WMSize(var Msg : TWMSize);
      message WM_SIZE;
    procedure WMVScroll(var Msg : TWMScroll);
      message WM_VSCROLL;

  protected
    procedure ChangeScale(M, D : Integer);
      override;
    procedure CreateParams(var Params: TCreateParams);
      override;
    procedure CreateWnd;
      override;
    procedure DefineProperties(Filer : TFiler);
      override;
    procedure Paint;
      override;

    {abstract internal methods}
    procedure aeCreateEditCell;
      dynamic; abstract;
      {-create the edit celll}
    function aeGetEditString : PAnsiChar;
      dynamic; abstract;
      {-return the edit cells edit string}
    procedure aeGetSampleDisplayData(P : PAnsiChar);
      dynamic; abstract;
      {-cause edit cell to display sample data}

    {event wraper methods}
    procedure DoGetCellValue(Index : LongInt);
      virtual; abstract;
      {-get the value for the "Index" cell}
    procedure DoGetItemColor(Index : LongInt; var FG, BG : TColor);
      virtual;
      {-get the color values for the "Index" cell}
    function DoPutCellValue : LongInt;
      dynamic; abstract;
      {-store the current value of the edit cell. Result is error code}
    procedure DoOnSelect(NewIndex : LongInt);
      dynamic;
      {-perform scroll action notification}

    {abstract property method}
    procedure SetActiveIndex(Value : LongInt);
      virtual; abstract;
      {-set the active array item}
  {.Z-}

    property InsertPushes : Boolean
      read FInsertPushes
      write SetInsertPushes
      default DefInsertPushes;

    property RightJustify : Boolean
      read FRightJustify
      write SetRightJustify
      default DefRightJustify;

    property TrimBlanks : Boolean
      read FTrimBlanks
      write SetTrimBlanks
      default DefTrimBlanks;

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

    property ItemIndex : LongInt
      read FActiveIndex
      write SetActiveIndex
      stored False;

  published
    property AutoAdvanceChar : Boolean
      read FAutoAdvanceChar
      write SetAutoAdvanceChar
      default DefAutoAdvanceChar;

    property AutoAdvanceLeftRight : Boolean
      read FAutoAdvanceLR
      write SetAutoAdvanceLeftRight
      default DefAutoAdvanceLR;

    property BeepOnError : Boolean
      read FBeepOnError
      write FBeepOnError
      default DefBeepOnError;

    property BorderStyle : TBorderStyle
      read FBorderStyle
      write SetBorderStyle
      default DefBorderStyle;

    property HighlightColors : TOvcColors
      read FHighlightColors
      write FHighlightColors;

    property LineColor : TColor
      read FLineColor
      write SetLineColor
      default DefLineColor;

    {placed here so RowHeight is set prior to NumItems}
    property RowHeight : Integer
      read FRowHeight
      write SetRowHeight
      default DefRowHeight;

    property NumItems : LongInt
      read FNumItems
      write SetNumItems
      default DefNumItems;

    property PadChar : AnsiChar
      read FPadChar
      write SetPadChar
      default DefPadChar;

    property RangeHi : string
      read GetRangeHi
      write SetRangeHi
      stored False;

    property RangeLo : string
      read GetRangeLo
      write SetRangeLo
      stored False;

    property ReadOnly : Boolean
      read FReadOnly
      write SetReadOnly
      default DefReadOnly;

    property TextMargin : Integer
      read FTextMargin
      write SetTextMargin
      default DefTextMargin;

    property UseScrollBar : Boolean
       read FUseScrollBar
       write SetUseScrollBar
       default DefUseScrollBar;

    {events}
    property OnGetItem : TGetItemEvent
      read FOnGetItem
      write FOnGetItem;

    property OnGetItemColor : TGetItemColorEvent
      read FOnGetItemColor
      write FOnGetItemColor;

    {events echoed to the edit field object}
    property OnChange : TNotifyEvent
      read FOnChange
      write FOnChange;

    property OnError : TValidationErrorEvent
      read FOnError
      write FOnError;

    property OnSelect : TSelectEvent
      read FOnSelect
      write FOnSelect;

    property OnUserCommand : TUserCommandEvent
      read FOnUserCommand
      write FOnUserCommand;

    property OnUserValidation : TUserValidationEvent
      read FOnUserValidation
      write FOnUserValidation;

    {inherited properties}
    property Align;
    property Color;
    property Ctl3D;
    property Controller;
    property DragCursor;
    property DragMode;
    property Enabled;
    property Font;
    property Height;
    property ParentColor;
    property ParentCtl3D;
    property ParentFont default DefParentFont;
    property ParentShowHint;
    property PopupMenu;
    property ShowHint;
    property TabOrder;
    property TabStop;
    property Visible;
    property Width;

    {inherited events}
    property OnClick;
    property OnDblClick;
    property OnDragDrop;
    property OnDragOver;
    property OnEndDrag;
    property OnEnter;
    property OnExit;
    property OnKeyDown;
    property OnKeyPress;
    property OnKeyUp;
    property OnMouseDown;
    property OnMouseMove;
    property OnMouseUp;
    {$IFDEF Win32}
    property OnStartDrag;
    {$ENDIF}
  end;

type
  TSimpleCellField = class;
  TOvcSimpleArrayEditor = class(TOvcBaseArrayEditor)
  {.Z+}
  protected {private}
    {variables to transfer to the edit cell field}
    aeDataType    : TSimpleDataType;
    aePictureMask : AnsiChar;

    {property methods}
    procedure SetArrayDataType(Value: TSimpleDataType);
      {-set the data type for this field}
    procedure SetDecimalPlaces(Value : Byte);
      {-set the number of decimal places for the edit field}
    procedure SetMaxLength(Value : Word);
      {-set the maximum length of the edit field}
    procedure SetPictureMask(Value : AnsiChar);
      {-set the picture mask}

  protected
    procedure aeCreateEditCell;
      override;
      {-create the edit celll}
    function aeGetEditString : PAnsiChar;
      override;
      {-return the edit cells edit string}
    procedure aeGetSampleDisplayData(P : PAnsiChar);
      override;
      {-obtain sample data for the edit cell to display}

    {event wrapper methods}
    procedure DoGetCellValue(Index : LongInt);
      override;
      {-get the value for the cell with "Index"}
    function DoPutCellValue : LongInt;
      override;
      {-store the current value of the edit cell. Result is error code}

    {virtual property method}
    procedure SetActiveIndex(Value : LongInt);
      override;
      {-set the active array item}
  {.Z-}

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

  published
    {properties for the edit cell field}
    property DataType : TSimpleDataType
      read aeDataType
      write SetArrayDataType
      default sftString;

    property DecimalPlaces : Byte
      read aeDecimalPlaces
      write SetDecimalPlaces
      default DefDecimalPlaces;

    property MaxLength : Word
      read aeMaxLength
      write SetMaxLength
      default DefMaxLength;

    property PictureMask : AnsiChar
      read aePictureMask
      write SetPictureMask;

    property InsertPushes;
    property TrimBlanks;
  end;

  {.Z+}
  TSimpleCellField = class(TOvcSimpleField)
  protected {private}
    procedure WMKeyDown(var Msg : TWMKeyDown);
      message WM_KEYDOWN;
    procedure WMKillFocus(var Msg : TWMKillFocus);
      message WM_KILLFOCUS;
    procedure WMSetFocus(var Msg : TWMSetFocus);
      message WM_SETFOCUS;

  protected
    procedure CreateWnd;
      override;

  public
    constructor Create(AOwner : TComponent);
      override;
  end;
  {.Z-}

type
  TPictureCellField = class;
  TOvcPictureArrayEditor = class(TOvcBaseArrayEditor)
  {.Z+}
  protected {private}
    {variables to transfer to the edit cell field}
    aeDataType    : TPictureDataType;
    aeEpoch       : Integer;          {combined epoch year and cenury}
    aePictureMask : string;

    {property methods}
    procedure SetArrayDataType(Value: TPictureDataType);
      {-set the data type for this field}
    procedure SetDecimalPlaces(Value : Byte);
      {-set the number of decimal places for the edit field}
    procedure SetEpoch(Value : Integer);
      {-set the epoch value}
    procedure SetMaxLength(Value : Word);
      {-set the maximum length of the edit field}
    procedure SetPictureMask(const Value : string);
      {-set the picture mask}

  protected
    procedure aeCreateEditCell;
      override;
      {-create the edit celll}
    function aeGetEditString : PAnsiChar;
      override;
      {-return the edit cells edit string}
    procedure aeGetSampleDisplayData(P : PAnsiChar);
      override;
      {-obtain sample data for the edit cell to display}

    {event wrapper methods}
    procedure DoGetCellValue(Index : LongInt);
      override;
      {-get the value for the cell with "Index"}
    function DoPutCellValue : LongInt;
      override;
      {-store the current value of the edit cell. Result is error code}

    {virtual property method}
    procedure SetActiveIndex(Value : LongInt);
      override;
      {-set the active array item}
  {.Z-}

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

  published
    {properties for the edit cell field}
    property DataType : TPictureDataType
      read aeDataType
      write SetArrayDataType
      default pftString;

    property DecimalPlaces : Byte
      read aeDecimalPlaces
      write SetDecimalPlaces
      default DefDecimalPlaces;

    property Epoch : Integer
      read aeEpoch
      write SetEpoch;

    property MaxLength : Word
      read aeMaxLength
      write SetMaxLength
      default DefMaxLength;

    property PictureMask : string
      read aePictureMask
      write SetPictureMask;

    property InsertPushes;
    property RightJustify;
    property TrimBlanks;
  end;

  {.Z+}
  TPictureCellField = class(TOvcPictureField)
  protected {private}
    procedure WMKeyDown(var Msg : TWMKeyDown);
      message WM_KEYDOWN;
    procedure WMKillFocus(var Msg : TWMKillFocus);
      message WM_KILLFOCUS;
    procedure WMSetFocus(var Msg : TWMSetFocus);
      message WM_SETFOCUS;

  protected
    procedure CreateWnd;
      override;

  public
    constructor Create(AOwner : TComponent);
      override;
  end;
  {.Z-}

type
  TNumericCellField = class;
  TOvcNumericArrayEditor = class(TOvcBaseArrayEditor)
  {.Z+}
  protected {private}
    {variables to transfer to the edit cell field}
    aeDataType    : TNumericDataType;
    aePictureMask : string;

    {property methods}
    procedure SetArrayDataType(Value: TNumericDataType);
      {-set the data type for this field}
    procedure SetPictureMask(const Value : string);
      {-set the picture mask}

  protected
    procedure aeCreateEditCell;
      override;
      {-create the edit celll}
    function aeGetEditString : PAnsiChar;
      override;
      {-return the edit cells edit string}
    procedure aeGetSampleDisplayData(P : PAnsiChar);
      override;
      {-obtain sample data for the edit cell to display}

    {event wrapper methods}
    procedure DoGetCellValue(Index : LongInt);
      override;
      {-get the value for the cell with "Index"}
    function DoPutCellValue : LongInt;
      override;
      {-store the current value of the edit cell. Result is error code}

    {virtual property method}
    procedure SetActiveIndex(Value : LongInt);
      override;
      {-set the active array item}
  {.Z-}

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

  published
    {properties for the edit cell field}
    property DataType : TNumericDataType
      read aeDataType
      write SetArrayDataType
      default nftLongInt;

    property PictureMask : string
      read aePictureMask
      write SetPictureMask;
  end;

  {.Z+}
  TNumericCellField = class(TOvcNumericField)
  protected {private}
    procedure WMKeyDown(var Msg : TWMKeyDown);
      message WM_KEYDOWN;
    procedure WMKillFocus(var Msg : TWMKillFocus);
      message WM_KILLFOCUS;
    procedure WMSetFocus(var Msg : TWMSetFocus);
      message WM_SETFOCUS;

  protected
    procedure CreateWnd;
      override;

  public
    constructor Create(AOwner : TComponent);
      override;
  end;
  {.Z-}


implementation
