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

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

unit OvcTimer;
  {-Timer Pool Component}

interface

uses
  {$IFDEF Win32} Windows, {$ELSE} WinTypes, WinProcs, {$ENDIF}
  Classes, Forms, Messages, SysUtils,
  OvcConst, OvcData, OvcExcpt, OvcMisc;

type
  TTriggerEvent =
    procedure(Sender : TObject; Handle : Integer; Interval : Word; ElapsedTime : LongInt)
    of object;

const
  DefEnabled      = True;  {by default, new triggers are enabled}
  MinInterval     = 55;    {smallest timer interval allowed}
  HalfMinInterval = MinInterval div 2;

type
  PEventRec      = ^TEventRec;
  TEventRec       = record
    erEnabled     : Boolean;        {true if trigger is active}
    erHandle      : Integer;        {handle of this event record}
    erInitTime    : LongInt;        {time when trigger was created}
    erInterval    : Word;           {trigger interval}
    erLastTrigger : LongInt;        {time last trigger was fired}
    erOnTrigger   : TTriggerEvent;  {method to call when fired}
    erRecurring   : Boolean;        {false for one time trigger}
  end;

type
  TOvcTimerPool = class(TComponent)
  {.Z+}
  protected {private}
    FOnAllTriggers : TTriggerEvent;

    ttList         : TList;    {list of event TEventRec records}
    ttHandle       : hWnd;     {our window handle}
    ttInterval     : Word;     {the actual Window's timer interval}
    ttEnabledCount : Word;     {count of active triggers}

    function GetElapsedTriggerTime(Handle : Integer) : LongInt;
      {-return the number of miliseconds since the timer trigger was created}
    function GetElapsedTriggerTimeSec(Handle : Integer) : LongInt;
      {-return the number of seconds since the timer trigger was created}
    function GetOnTrigger(Handle : Integer) : TTriggerEvent;
      {-returns the timer trigger's event method address}
    function GetTriggerCount : Integer;
      {-returns the number of maintained timer triggers}
    function GetTriggerEnabled(Handle : Integer) : Boolean;
      {-returns the timer trigger's enabled status}
    function GetTriggerInterval(Handle : Integer) : Word;
      {-returns the interval for the timer trigger with Handle}
    procedure SetOnTrigger(Handle : Integer; Value: TTriggerEvent);
      {-sets the method to call when the timer trigger fires}
    procedure SetTriggerEnabled(Handle : Integer; Value: Boolean);
      {-sets the timer trigger's enabled status}
    procedure SetTriggerInterval(Handle : Integer; Value: Word);
      {-sets the timer trigger's interval}

    procedure ttCalcNewInterval;
      {-calculates the needed interval for the window's timer}
    function ttCountEnabledTriggers : Integer;
      {-returns the number of enabled/active timer triggers}
    function ttCreateTriggerHandle : Integer;
      {-returns a unique timer trigger handle}
    function ttEventIndex(Handle : Integer) : Integer;
      {-returns the internal list index corresponding to the trigger handle}
    procedure ttSortTriggers;
      {-sorts the internal list of timer trigger event records}
    procedure ttTimerWndProc(var Msg : TMessage);
      {-window procedure to catch timer messages}
    procedure ttUpdateTimer;
      {-re-create the windows timer with a new timer interval}

  protected
    procedure DoTriggerNotification;
      virtual;
      {-conditionally sends notification of all events}
  {.Z-}

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

    function AddOneTime(OnTrigger : TTriggerEvent; Interval : Word) : Integer;
      {-adds a new timer trigger. removed automatically after one firing}
    function Add(OnTrigger : TTriggerEvent; Interval : Word) : Integer;
      {-adds a new timer trigger and returns a handle}
    procedure Remove(Handle : Integer);
      {-removes the timer trigger}
    procedure RemoveAll;
      {-disable and destroyes all timer triggers}

    {public properties}
    property Count : Integer
      read GetTriggerCount;

    property ElapsedTime[Handle : Integer] : LongInt
      read GetElapsedTriggerTime;

    property ElapsedTimeSec[Handle : Integer] : LongInt
      read GetElapsedTriggerTimeSec;

    property Enabled[Handle : Integer] : Boolean
      read GetTriggerEnabled
      write SetTriggerEnabled;

    property Interval[Handle : Integer] : Word
      read GetTriggerInterval
      write SetTriggerInterval;

    {events}
    property OnTrigger[Handle : Integer] : TTriggerEvent
      read GetOnTrigger
      write SetOnTrigger;

  published
    {method to call when any trigger is fired}
    property OnAllTriggers : TTriggerEvent
      read FOnAllTriggers
      write FOnAllTriggers;
  end;


implementation
