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

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

{$IFDEF Win32}
  {$R OVCBASE.R32}
{$ELSE}
  {$R OVCBASE.R16}
{$ENDIF}

unit OvcBase;
  {-Base unit for Orpheus visual components}

interface

uses
  {$IFDEF Win32} Windows, {$ELSE} WinTypes, WinProcs, {$ENDIF}
  Classes, Controls, Dialogs, Forms, Messages, StdCtrls, SysUtils,
  {$IFDEF NVBUG} LibIntf, {$ENDIF}
  OvcCmd, OvcData, OvcMisc, OvcConst, OvcExcpt, STDate;

{$IFDEF NVBUG}
  {$IFDEF Win32}
  var
  {$ELSE}
  const
  {$ENDIF}
    ICompLib : TILibrary = nil;
{$ENDIF}

const
  DefEpoch        = 1900;
  DefErrorPending = False;
  DefInsertMode   = True;

type
  {event method types}
  TDataErrorEvent =
    procedure(Sender : TObject; ErrorCode : Word; const ErrorMsg : string)
    of object;
  TPostEditEvent =
    procedure(Sender : TObject; GainingControl : TWinControl)
    of object;
  TPreEditEvent =
    procedure(Sender : TObject; LosingControl : TWinControl)
    of object;

type
  TOvcController = class(TComponent)
  {.Z+}
  protected {private}
    FBtnCancel     : TButton;               {entry fields can ignore errors}
    FBtnHelp       : TButton;               {if the focus is moving to}
    FBtnRestore    : TButton;               {one of these three buttons}
    FEntryCommands : TOvcCommandProcessor;  {command processor}
    FEpoch         : Integer;               {combined epoch year and cenury}
    FErrorPending  : Boolean;               {an error is pending for an ef}
    FErrorText     : string;                {text of last error}
    FHandle        : hWnd;                  {our window handle}
    FInsertMode    : Boolean;               {global insert mode flag}

    {$IFDEF MoveableComponents}
    FAllowMove     : Boolean;               {allow components to be moved}
    FOnControlMove : TNotifyEvent;
    {$ENDIF}

    {events}
    FOnError       : TDataErrorEvent;
    FOnPostEdit    : TPostEditEvent;
    FOnPreEdit     : TPreEditEvent;

    {property methods}
    procedure SetEpoch(Value : Integer);
      {-set the epoch value--used by entry fields}
    function GetHandle : hWnd;
      {-return our window handle. create if necessary}

    {internal methods}
    procedure cWndProc(var Msg : TMessage);
      {-window procedure}

  protected
    procedure Notification(AComponent : TComponent; Operation: TOperation);
      override;
  {.Z-}

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

  {.Z+}
    procedure DestroyHandle;
      {-destroy the window handle}

    {wrappers for event handlers}
    procedure DoOnError(Sender : TObject; ErrorCode : Word; ErrorMsg : string);
      {-call the method assigned to the OnError event}
    procedure DoOnPostEdit(Sender : TObject; GainingControl : TWinControl);
      {-call the method assigned to the OnPostEdit event}
    procedure DoOnPreEdit(Sender : TObject; LosingControl : TWinControl);
      {-call the method assigned to the OnPreEdit event}
  {.Z-}

    function IsSpecialButton(H : hWnd) : Boolean;
      dynamic;
      {-return true if H is btnCancel, btnHelp, or btnRestore}
    procedure MarkAsUninitialized(Uninitialized : Boolean);
      {-mark all entry fields on form as uninitialized}
    function ValidateEntryFields : TComponent;
      {-ask each entry field to validate its contents. Return nil
        if no error, else return pointer to field with error}
    function ValidateTheseEntryFields(Fields : array of TComponent) : TComponent;
      {-ask the specified entry fields to validate their contents. Return nil
        if no error, else return pointer to field with error}

  {.Z+}
    {$IFDEF MoveableComponents}
    property AllowMove : Boolean
      read FAllowMove
      write FAllowMove;

    property OnControlMove : TNotifyEvent
      read FOnControlMove
      write FOnControlMove;
    {$ENDIF}
  {.Z-}

    property ErrorPending : Boolean
      read FErrorPending
      write FErrorPending
      default DefErrorPending;

    property ErrorText : string
      read FErrorText
      write FErrorText;

    property Handle : hWnd
      read GetHandle;

    property InsertMode : Boolean
      read FInsertMode
      write FInsertMode
      default DefInsertMode;

  published
    {properties}
    property BtnCancel  : TButton
      read FBtnCancel
      write FBtnCancel
      stored True;

    property BtnHelp    : TButton
      read FBtnHelp
      write FBtnHelp
      stored True;

    property BtnRestore : TButton
      read FBtnRestore
      write FBtnRestore
      stored True;

    property EntryCommands : TOvcCommandProcessor
      read FEntryCommands
      write FEntryCommands
      stored True;

    property Epoch : Integer
      read FEpoch
      write SetEpoch
      default DefEpoch;

    {events}
    property OnError : TDataErrorEvent
      read FOnError
      write FOnError
      stored True;

    property OnPostEdit : TPostEditEvent
      read FOnPostEdit
      write FOnPostEdit
      stored True;

    property OnPreEdit : TPreEditEvent
      read FOnPreEdit
      write FOnPreEdit
      stored True;

    property Tag;
  end;

type
  {base class for Orpheus components. Provides controller access}
  TOvcBase = class(TCustomControl)
  {.Z+}
  protected {private}
    FController    : TOvcController;

    function ControllerAssigned : Boolean;
    procedure SetController(Value : TOvcController);

  protected
    procedure CreateWnd;
      override;
    procedure Notification(AComponent : TComponent; Operation: TOperation);
      override;

    {$IFDEF MoveableComponents}
    procedure WMNCLButtonDblClk(var Msg : TWMNCLButtonDblClk);
      message WM_NCLBUTTONDBLCLK;
    procedure WMNCHitTest(var Msg : TWMNCHitTest);
      message WM_NCHITTEST;
    procedure WMNCLButtonDown(var Msg : TWMNCLButtonDown);
      message WM_NCLBUTTONDOWN;
    {$ENDIF}

  {.Z-}

  public
    property Controller : TOvcController
      read FController
      write SetController;

    {$IFDEF ORPHEUSDEMO}
    destructor Destroy;
      override;
    {$ENDIF}

  end;


implementation
