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

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


unit OvcDbAE;
  {-Data aware array editors}

interface

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

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

type
  {event to get color of the item cell}
  TGetItemColorEvent =
    procedure(Sender : TObject; Field : TField; Row : Integer; var FG, BG : TColor)
    of object;
  {event to notify of a scroll action}
  TIndicatorClickEvent =
    procedure(Sender : TObject; Row : LongInt)
    of object;

type
  {.Z+}
  TOvcDbSimpleCell = class(TOvcDbSimpleField)
  private
  protected
    procedure CreateWnd;
      override;
    procedure WMKeyDown(var Msg : TWMKeyDown);
      message WM_KEYDOWN;
  public
    constructor Create(AOwner : TComponent);
      override;
  end;

  TOvcDbPictureCell = class(TOvcDbPictureField)
  private
  protected
    procedure CreateWnd;
      override;
    procedure WMKeyDown(var Msg : TWMKeyDown);
      message WM_KEYDOWN;
  public
    constructor Create(AOwner : TComponent);
      override;
  end;

  TOvcDbNumericCell = class(TOvcDbNumericField)
  private
  protected
    procedure CreateWnd;
      override;
    procedure WMKeyDown(var Msg : TWMKeyDown);
      message WM_KEYDOWN;
  public
    constructor Create(AOwner : TComponent);
      override;
  end;
  {.Z-}

  {base class for data-aware array editors}
  TOvcBaseDbArrayEditor = class(TOvcBase)
  {.Z+}
  protected {private}
    FActiveRow           : LongInt;      {the row index of the active item}
    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}
    FAutoRowHeight       : Boolean;      {true to automatically adjust row height based on font}
    FCaretToEnd          : Boolean;      {true to place caret at end of string}
    FBeepOnError         : Boolean;      {MessageBeep is called for error conditions}
    FBorderStyle         : TBorderStyle; {border around the control}
    FDataField           : string;       {database field name}
    FDataSource          : TDataSource;  {data source}
    FDateOrTime          : TDateOrTime;  {determines if editing dates or times}
    FDecimalPlaces       : Byte;         {max decimal places}
    FEpoch               : Integer;      {combined epoch year and cenury}
    FFieldType           : TFieldType;   {field data type}
    FHighlightColors     : TOvcColors;   {highlight colors}
    FIndicatorWidth      : Integer;      {width of row indicators}
    FInsertPushes        : Boolean;      {inserted chars push ending chars off}
    FLineColor           : TColor;       {color of row divider lines}
    FMaxLength           : Word;         {maximum length of string}
    FPadChar             : AnsiChar;     {character used to pad end of string}
    FPageScroll          : Boolean;      {true to scroll like Delphi's grids}
    FPictureMask         : string;       {picture mask}
    FReadOnly            : Boolean;      {prevents field editing}
    FRightJustify        : Boolean;      {display text right-justified}
    FRowHeight           : Integer;      {pixel height of one row}
    FShowIndicator       : Boolean;      {true to show row indicators}
    FTextMargin          : Integer;      {indent from left (right)}
    FTrimBlanks          : Boolean;      {remove leading and trailing blanks if not listerals}
    FUseScrollBar        : Boolean;      {true to use vertical scroll bar}
    FZeroAsNull          : Boolean;      {true to store zero value as null}

    {cell datalink event hook variables}
    FCellOnActiveChange  : TNotifyEvent;
    FCellOnDataChange    : TNotifyEvent;
    FCellOnEditingChange : TNotifyEvent;
    FCellOnUpdateData    : TNotifyEvent;

    {event variables}
    FOnChange            : TNotifyEvent;
    FOnError             : TValidationErrorEvent;
    FOnGetItemColor      : TGetItemColorEvent;
    FOnIndicatorClick    : TIndicatorClickEvent;
    FOnUserCommand       : TUserCommandEvent;
    FOnUserValidation    : TUserValidationEvent;

    {internal/working variables}
    aeIndicators         : TImageList;    {list of indicators}
    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}
    aePainting           : Boolean;       {True when painting}
    aeFocusing           : Boolean;       {True when changing focus}

    {property methods}
    function GetDataLink : TFieldDataLink;
      {-return the cell datalink}
    function GetField : TField;
      {-return the associated field object}
    function GetRangeHi : string;
      {-get the high field range string value}
    function GetRangeLo : string;
      {-get the low field range string value}
    procedure SetActiveRow(Value : LongInt);
      {-set the active row index}
    procedure SetAutoAdvanceChar(Value : Boolean);
      {-sets the AutoAdvanceChar option}
    procedure SetAutoAdvanceLeftRight(Value : Boolean);
      {-sets the AutoAdvanceLeftRight option}
    procedure SetAutoRowHeight(Value : Boolean);
      {-sets the AutoRowHeight option}
    procedure SetBeepOnError(Value : Boolean);
      {-sets the BeepOnError option}
    procedure SetBorderStyle(Value : TBorderStyle);
      {-set the style used for the border}
    procedure SetCaretToEnd(Value : Boolean);
      {-set the CaretToEnd option}
    procedure SetDataField(Value : string);
      {-set the field name}
    procedure SetDataSource(Value : TDataSource);
      {-set the data source}
    procedure SetDateOrTime(Value : TDateOrTime);
      {-set the date or time property value}
    procedure SetDecimalPlaces(Value : Byte);
      {-set the number of decimal places for the edit field}
    procedure SetEpoch(Value : Integer);
      {-set the epoch value}
    procedure SetFieldType(Value : TFieldType);
      {-set the data type for this field}
    procedure SetIndicatorWidth(Value : Integer);
      {-set the row indicator width}
    procedure SetInsertPushes(Value : Boolean);
      {-set the InsertPushes option}
    procedure SetLineColor(Value : TColor);
      {-set the color used to draw the row divider lines}
    procedure SetMaxLength(Value : Word);
      {-set the maximum length of the edit field}
    procedure SetPadChar(Value : AnsiChar);
      {-set the character used to pad the end of the edit string}
    procedure SetPictureMask(Value : string);
      {-set the picture mask}
    procedure SetRangeHi(Value : string);
      {-set the high field range from a string value}
    procedure SetRangeLo(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 SetShowIndicator(Value : Boolean);
      {-set the show indicator option}
    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}
    procedure SetZeroAsNull(Value : Boolean);
      {-set option to store zero as null}

    {general (internal) routines}
    procedure aeAdjustIntegralHeight;
      {-resizes the component so no partial items appear}
    procedure aeAdjustRowHeight;
      {-adjust the row height based on the current font}
    procedure aeColorChanged(AColor : TObject);
      {-highlight color change-repaint}
    procedure aeMoveCell(NewIndex : LongInt);
      {-moves the cell to the specified row Index}
    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}
    procedure aeUpdateActive;
      {-update the active record number}
    procedure aeUpdateNumRows;
      {-update the number of rows}
    procedure aeUpdateScrollBar;
      {-sets the vertical scroll bar position}
    procedure aeWriteRangeHi(Stream : TStream);
      {-called to store the high range value}
    procedure aeWriteRangeLo(Stream : TStream);
      {-called to store the low range value}

    {event handlers for attached cell datalink}
    procedure aeActiveChange(Sender : TObject);
      {-notification routine called by an aeCell event}
    procedure aeDataChange(Sender : TObject);
      {-notification routine called by an aeCell event}
    procedure aeEditingChange(Sender : TObject);
      {-notification routine called by an aeCell event}
    procedure aeUpdateData(Sender : TObject);
      {-notification routine called by an aeCell event}

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

    {windows message methods}
    procedure WMCancelMode(var Msg : TWMCancelMode);
      message WM_CANCELMODE;
    procedure WMGetMinMaxInfo(var Msg : TWMGetMinMaxInfo);
      message WM_GETMINMAXINFO;
    procedure WMMouseActivate(var Msg : TWMMouseActivate);
      message WM_MOUSEACTIVATE;
    procedure WMEraseBkGnd(var Msg : TWMEraseBkGnd);
      message WM_ERASEBKGND;
    procedure WMLButtonDown(var Msg : TWMLButtonDown);
      message WM_LBUTTONDOWN;
    procedure WMNCHitTest(var Msg : TWMNCHitTest);
      message WM_NCHITTEST;
    procedure WMSize(var Msg : TWMSize);
      message WM_SIZE;
    procedure WMSetFocus(var Msg : TWMSetFocus);
      message WM_SETFOCUS;
    procedure WMVScroll(var Msg : TWMScroll);
      message WM_VSCROLL;

  protected
    {internal/working variables}
    aeCell      : TOvcBaseEntryField;

    {VCL methods}
    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;
      virtual; abstract;
      {-create the edit cell}
    procedure aeGetCellProperties;
      virtual; abstract;
      {-freshen our copy of the cell properties}

    {general internal methods}
    function aeGetEditString : PChar;
      {-return the edit cells edit string}
    procedure aeGetSampleDisplayData(P : PChar);
      {-return a sample display string}
    procedure aeRefresh;
      {-called to conditionally call refresh}

    {event wrapper methods}
    procedure DoGetItemColor(AField : TField; ARow : Integer; var FG, BG : TColor);
      virtual;
      {-get the color values for the cell}
    procedure DoOnIndicatorClick(Row : LongInt);
      dynamic;
      {-perform indicator click notification}
  {.Z-}

    property AutoAdvanceChar : Boolean
      read FAutoAdvanceChar
      write SetAutoAdvanceChar
      default DefAutoAdvanceChar;

    property AutoAdvanceLeftRight : Boolean
      read FAutoAdvanceLR
      write SetAutoAdvanceLeftRight
      default DefAutoAdvanceLR;

    property AutoRowHeight : Boolean
      read FAutoRowHeight
      write SetAutoRowHeight
      default DefAutoRowHeight;

    property BeepOnError : Boolean
      read FBeepOnError
      write FBeepOnError
      default DefBeepOnError;

    property BorderStyle : TBorderStyle
      read FBorderStyle
      write SetBorderStyle
      default DefBorderStyle;

    property CaretToEnd : Boolean
      read FCaretToEnd
      write SetCaretToEnd
      default DefCaretToEnd;

    property DataField : string
      read FDataField
      write SetDataField;

    property DataSource : TDataSource
      read FDataSource
      write SetDataSource;

    property DateOrTime : TDateOrTime
      read FDateOrTime
      write SetDateOrTime
      default DefDateOrTime;

    property DecimalPlaces : Byte
      read FDecimalPlaces
      write SetDecimalPlaces
      default DefDecimalPlaces;

    property Epoch : Integer
      read FEpoch
      write SetEpoch;

    property FieldType : TFieldType
      read FFieldType
      write SetFieldType;

    property HighlightColors : TOvcColors
      read FHighlightColors
      write FHighlightColors;

    property IndicatorWidth : Integer
      read FIndicatorWidth
      write SetIndicatorWidth
      default DefIndicatorWidth;

    property InsertPushes : Boolean
      read FInsertPushes
      write SetInsertPushes
      default DefInsertPushes;

    property LineColor : TColor
      read FLineColor
      write SetLineColor
      default DefLineColor;

    property MaxLength : Word
      read FMaxLength
      write SetMaxLength
      default DefMaxLength;

    property PadChar : AnsiChar
      read FPadChar
      write SetPadChar
      default DefPadChar;

    property PageScroll : Boolean
      read FPageScroll
      write FPageScroll
      default DefPageScroll;

  {.Z+}
    property PictureMask : string
      read FPictureMask
      write SetPictureMask;
  {.Z-}

    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 RightJustify : Boolean
      read FRightJustify
      write SetRightJustify
      default DefRightJustify;

    property RowHeight : Integer
      read FRowHeight
      write SetRowHeight
      default DefRowHeight;

    property ShowIndicator : Boolean
      read FShowIndicator
      write SetShowIndicator
      default DefShowIndicator;

    property TextMargin : Integer
      read FTextMargin
      write SetTextMargin
      default DefTextMargin;

    property TrimBlanks : Boolean
      read FTrimBlanks
      write SetTrimBlanks
      default DefTrimBlanks;

    property UseScrollBar : Boolean
      read FUseScrollBar
      write SetUseScrollBar
      default DefUseScrollBar;

    property ZeroAsNull : Boolean
      read FZeroAsNull
      write SetZeroAsNull
      default DefZeroAsNull;

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

    property OnError : TValidationErrorEvent
      read FOnError
      write FOnError;

    property OnGetItemColor : TGetItemColorEvent
      read FOnGetItemColor
      write FOnGetItemColor;

    property OnIndicatorClick : TIndicatorClickEvent
      read FOnIndicatorClick
      write FOnIndicatorClick;

    property OnUserCommand : TUserCommandEvent
      read FOnUserCommand
      write FOnUserCommand;

    property OnUserValidation : TUserValidationEvent
      read FOnUserValidation
      write FOnUserValidation;

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

    procedure Reset;
      {-discard current changes (if any) and obtain new data from TField}
    procedure Scroll(Delta : Integer);
      {-scroll the datasource by Distance (signed)}
    procedure UpdateRecord;
      {-force cell to update its underlying TField}

    property Canvas;

  {.Z+}
    property ActiveRow : LongInt
      read FActiveRow
      write SetActiveRow
      stored False;

    property DataLink : TFieldDataLink
      read GetDataLink;
  {.Z-}

    property Field : TField
      read GetField;

  published
  end;

  TOvcDbSimpleArrayEditor = class(TOvcBaseDbArrayEditor)
  {.Z+}
  protected {private}
    {virtual methods}
    procedure aeCreateEditCell;
      override;
      {-create the edit cell}
    procedure aeGetCellProperties;
      override;
      {-freshen our copies of the cell properties}
  {.Z-}

  public
    constructor Create(AOwner : TComponent);
      override;

  published
    property FieldType;
    property DataSource;
    property DataField;

    property AutoAdvanceChar;
    property AutoAdvanceLeftRight;
    property AutoRowHeight;
    property BeepOnError;
    property BorderStyle;
    property CaretToEnd;
    property Color;
    property Controller;
    property Ctl3D;
    property DecimalPlaces;
    property DragCursor;
    property DragMode;
    property Enabled;
    property Font;
    property Height;
    property HighlightColors;
    property IndicatorWidth;
    property InsertPushes;
    property LineColor;
    property MaxLength;
    property PadChar;
    property PageScroll;
    property ParentColor;
    property ParentCtl3D;
    property ParentFont default DefParentFont;
    property ParentShowHint;
    property PictureMask;
    property PopupMenu;
    property RangeHi stored False;
    property RangeLo stored False;
    property ReadOnly;
    property RowHeight;
    property ShowHint;
    property ShowIndicator;
    property TabOrder;
    property TabStop;
    property Tag;
    property TextMargin;
    property TrimBlanks;
    property UseScrollBar;
    property Visible;
    property Width;
    property ZeroAsNull;

    {events}
    property OnChange;
    property OnClick;
    property OnDblClick;
    property OnDragDrop;
    property OnDragOver;
    property OnEndDrag;
    property OnEnter;
    property OnError;
    property OnExit;
    property OnGetItemColor;
    property OnIndicatorClick;
    property OnKeyDown;
    property OnKeyPress;
    property OnKeyUp;
    property OnMouseDown;
    property OnMouseMove;
    property OnMouseUp;
    {$IFDEF Win32}
    property OnStartDrag;
    {$ENDIF}
    property OnUserCommand;
    property OnUserValidation;
  end;

  TOvcDbPictureArrayEditor = class(TOvcBaseDbArrayEditor)
  {.Z+}
  protected {private}
    {virtual methods}
    procedure aeCreateEditCell;
      override;
      {-create the edit cell}
    procedure aeGetCellProperties;
      override;
      {-freshen our copies of the cell properties}
  {.Z-}

  public
    constructor Create(AOwner : TComponent);
      override;

  published
    property FieldType;
    property DataSource;
    property DataField;
    property DateOrTime;

    property AutoAdvanceChar;
    property AutoAdvanceLeftRight;
    property AutoRowHeight;
    property BeepOnError;
    property BorderStyle;
    property CaretToEnd;
    property Color;
    property Controller;
    property Ctl3D;
    property DecimalPlaces;
    property DragCursor;
    property DragMode;
    property Enabled;
    property Epoch;
    property Font;
    property Height;
    property HighlightColors;
    property InsertPushes;
    property IndicatorWidth;
    property LineColor;
    property MaxLength;
    property PadChar;
    property PageScroll;
    property ParentColor;
    property ParentCtl3D;
    property ParentFont default DefParentFont;
    property ParentShowHint;
    property PictureMask;
    property PopupMenu;
    property RangeHi stored False;
    property RangeLo stored False;
    property ReadOnly;
    property RightJustify;
    property RowHeight;
    property ShowHint;
    property ShowIndicator;
    property TabOrder;
    property TabStop;
    property Tag;
    property TextMargin;
    property TrimBlanks;
    property UseScrollBar;
    property Visible;
    property Width;
    property ZeroAsNull;

    property OnChange;
    property OnClick;
    property OnDblClick;
    property OnDragDrop;
    property OnDragOver;
    property OnEndDrag;
    property OnEnter;
    property OnError;
    property OnExit;
    property OnGetItemColor;
    property OnIndicatorClick;
    property OnKeyDown;
    property OnKeyPress;
    property OnKeyUp;
    property OnMouseDown;
    property OnMouseMove;
    property OnMouseUp;
    {$IFDEF Win32}
    property OnStartDrag;
    {$ENDIF}
    property OnUserCommand;
    property OnUserValidation;
  end;

  TOvcDbNumericArrayEditor = class(TOvcBaseDbArrayEditor)
  {.Z+}
  protected {private}
    {virtual methods}
    procedure aeCreateEditCell;
      override;
      {-create the edit cell}
    procedure aeGetCellProperties;
      override;
      {-freshen our copies of the cell properties}
  {.Z-}

  public
    constructor Create(AOwner : TComponent);
      override;

  published
    property FieldType;
    property DataSource;
    property DataField;

    property AutoAdvanceChar;
    property AutoAdvanceLeftRight;
    property AutoRowHeight;
    property BeepOnError;
    property BorderStyle;
    property CaretToEnd;
    property Color;
    property Controller;
    property Ctl3D;
    property DragCursor;
    property DragMode;
    property Enabled;
    property Font;
    property Height;
    property HighlightColors;
    property IndicatorWidth;
    property LineColor;
    property PadChar;
    property PageScroll;
    property ParentColor;
    property ParentCtl3D;
    property ParentFont default DefParentFont;
    property ParentShowHint;
    property PictureMask;
    property PopupMenu;
    property RangeHi stored False;
    property RangeLo stored False;
    property ReadOnly;
    property RowHeight;
    property ShowHint;
    property ShowIndicator;
    property TabOrder;
    property TabStop;
    property Tag;
    property TextMargin;
    property UseScrollBar;
    property Visible;
    property Width;
    property ZeroAsNull;

    property OnChange;
    property OnClick;
    property OnDblClick;
    property OnDragDrop;
    property OnDragOver;
    property OnEndDrag;
    property OnEnter;
    property OnError;
    property OnExit;
    property OnGetItemColor;
    property OnIndicatorClick;
    property OnKeyDown;
    property OnKeyPress;
    property OnKeyUp;
    property OnMouseDown;
    property OnMouseMove;
    property OnMouseUp;
    {$IFDEF Win32}
    property OnStartDrag;
    {$ENDIF}
    property OnUserCommand;
    property OnUserValidation;
  end;


implementation
