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

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

unit OvcCal;
  {-Calendar component}

interface

uses
  {$IFDEF Win32} Windows, {$ELSE} WinTypes, WinProcs, {$ENDIF}
  Classes, Controls, Forms, Graphics, Menus, Messages, SysUtils,
  OvcBase, OvcCmd, OvcColor, OvcConst, OvcExcpt,
  OvcData, OvcIntl, OvcVer, OvcMisc, STDate;

const
  DefBorderStyle   = bsSingle;
  DefDayNameLen    = 3;
  DefDrawGridLines = True;
  DefDrawHeader    = True;
  DefHeaderHeight  = 0;
  DefHeight        = 130;
  DefInitialize    = False;
  DefLineColor     = clSilver;
  DefParentcolor   = False;
  DefParentFont    = True;
  DefReadOnly      = False;
  DefShortNames    = False;
  DefTabStop       = True;
  DefTextMargin    = 2;
  DefWeekStarts    = Sunday;
  DefWantDblClicks = True;
  DefWidth         = 230;

type
  TOvcCalendarColors = class(TPersistent)
  {.Z+}
  protected {private}
    FActiveColors   : TOvcColors; {active cell colors}
    FInactiveColors : TOvcColors; {inactive cell colors}
    FLabelColors    : TOvcColors; {label area colors}
    FTextColors     : TOvcColors; {normal cell colors}
  {.Z-}

  public
    constructor Create;
    destructor Destroy;
      override;

  published
    property ActiveColor   : TOvcColors
      read FActiveColors
      write FActiveColors;

    property InactiveColor : TOvcColors
      read FInactiveColors
      write FInactiveColors;

    property LabelColor    : TOvcColors
      read FLabelColors
      write FLabelColors;

    property TextColor     : TOvcColors
      read FTextColors
      write FTextColors;
  end;

type
  {draw item event}
  TDrawHeaderEvent =
    procedure(Sender : TObject; Month, Year : Integer; const Rect : TRect)
    of object;
  TDrawItemEvent =
    procedure(Sender : TObject; Day, Month, Year : Integer; const Rect : TRect)
    of object;

  TOvcCustomCalendar = class(TOvcBase)
  {.Z+}
  protected {private}
    {property variables}
    FBorderStyle   : TBorderStyle; {border style}
    FColors        : TOvcCalendarColors; {color selection object}
    FDay           : Integer;      {calendar day}
    FDayNameLen    : Byte;         {number of characters for day names}
    FDrawHeader    : Boolean;      {true to draw day name header}
    FDrawGridLines : Boolean;      {true to draw grid lines}
    FHeaderHeight  : Integer;      {height of first row-0 for auto}
    FInitialize    : Boolean;      {true to init date to current}
    FIntlSup       : TOvcIntlSup;  {international date/time support}
    FLineColor     : TColor;       {color for grid lines}
    FMonth         : Integer;      {calendar month}
    FReadOnly      : Boolean;      {true if in read only mode}
    FShortNames    : Boolean;      {true for header to show short names}
    FYear          : Integer;      {calendar year}
    FWantDblClicks : Boolean;      {true to include cs_dblclks style}
    FWeekStarts    : TDayType;     {the day that begins the week}

    {event variables}
    FOnChange     : TNotifyEvent;
    FOnDrawHeader : TDrawHeaderEvent;
    FOnDrawItem   : TDrawItemEvent;
    FOnUserCommand: TUserCommandEvent;

    {internal variables}
    clCalendar    : array[1..42] of Byte;            {current month grid}
    clFirst       : Byte;         {index for first day in current month}
    clLast        : Byte;         {index for last day in current month}
    clMask        : array[0..MaxDateLen] of AnsiChar; {default date mask}
    clRowCol      : array[0..6, 0..6] of TRect;       {cell TRect info}
    clRowCount    : Integer;      {6 if no header, otherwise 7}
    clStartRow    : Integer;                          {first row number}

    {property methods}
    function GetAsDateTime : TDateTime;
      {-returns the date as a Delphi DateTime value}
    function GetCalendarDate : TStDate;
      {-return the currently selected calendar date}
    procedure SetAsDateTime(Value : TDateTime);
      {-sets the date to a Delphi DateTime value}
    procedure SetBorderStyle(Value : TBorderStyle);
      {-change the border style}
    procedure SetCalendarDMY(Day, Month, Year : Integer);
      {-change the currently selected date}
    procedure SetDay(Value : Integer);
      {-set the currently selected day}
    procedure SetDayNameLen(Value : Byte);
      {-set the length of the display day names}
    procedure SetDrawGridLines(Value : Boolean);
      {-set the DrawLines property value}
    procedure SetDrawHeader(Value : Boolean);
      {-set the DrawHeader property value}
    procedure SetHeaderHeight(Value : Integer);
      {-set the height of the frist row}
    procedure SetInitialize(Value : Boolean);
      {-set the Initialize property value}
    procedure SetIntlSupport(Value : TOvcIntlSup);
      {-set the international support object this field will use}
    procedure SetLineColor(Value : TColor);
      {-set the color used to draw the grid lines}
    procedure SetMonth(Value : Integer);
      {-set the calendar month}
    procedure SetShortNames(Value : Boolean);
      {-set the ShortNames property value}
    procedure SetWantDblClicks(Value : Boolean);
      {-set the WantDblClicks property value}
    procedure SetWeekStarts(Value : TDayType);
      {-set which day to begin the week}
    procedure SetYear(Value : Integer);
      {-set the current year}

    {internal methods}
    procedure calColorChanged(AColor: TObject);
      {-a color has changed, refresh display}
    function calGetCurrentRectangle : TRect;
      {-get bounding rectangle for the current calendar day}
    procedure calIncCalendarDate(Months, Years : Integer);
      {-increment the calendar date by Months and Years}
    function calIsDateStored : Boolean;
      {-returns true if the date shoul;d be stored}
    procedure calRebuildCalArray;
      {-recalculate the contents of the calendar array}
    procedure calRecalcSize;
      {-calcualte new sizes for rows and columns}

    {windows message methods}
    procedure WMEraseBkGnd(var Msg : TWMEraseBkGnd);
      message WM_ERASEBKGND;
    procedure WMGetDlgCode(var Msg : TWMGetDlgCode);
      message WM_GETDLGCODE;
    procedure WMKillFocus(var Msg : TWMKillFocus);
      message WM_KILLFOCUS;
    procedure WMLButtonDown(var Msg : TWMLButtonDown);
      message WM_LBUTTONDOWN;
    procedure WMKeyDown(var Msg : TWMKeyDown);
      message WM_KEYDOWN;
    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 WMWININICHANGE(var Msg : TWMWinIniChange);
      message WM_WININICHANGE;

  protected
    {vcl methods}
    procedure CreateParams(var Params: TCreateParams);
      override;
    procedure CreateWnd;
      override;
    procedure Paint;
      override;

    {dynamic event wrappers}
    procedure DoOnChange;
      dynamic;
      {-call the method assigned to the OnChange event}
    procedure DoOnDrawHeader(Month, Year : Integer; const Rect : TRect);
      dynamic;
      {-call the method assigned to the OnDrawHeader event}
    procedure DoOnDrawItem(Day, Month, Year : Integer; const Rect : TRect);
      dynamic;
      {-call the method assigned to the OnDrawItem event}
    procedure DoOnUserCommand(Command : Word);
      dynamic;
      {-call the method assigned to the OnUserCommand event}
    function IsReadOnly : Boolean;
      dynamic;
      {-return true if the calendar is in read-only mode}
    procedure SetCalendarDate(Value : TStDate);
      {-set the calendar display date}
      virtual;
  {.Z-}

    {properties}
    property BorderStyle : TBorderStyle
      read FBorderStyle
      write SetBorderStyle
      default DefBorderStyle;

    property Colors : TOvcCalendarColors
      read FColors
      write FColors;

    property DayNameLen : Byte
      read FDayNameLen
      write SetDayNameLen
      default DefDayNameLen;

    property DrawGridLines : Boolean
      read FDrawGridLines
      write SetDrawGridLines
      default DefDrawGridLines;

    property DrawHeader : Boolean
      read FDrawHeader
      write SetDrawHeader
      default DefDrawHeader;

    property HeaderHeight : Integer
      read FHeaderHeight
      write SetHeaderHeight
      default DefHeaderHeight;

    property Initialize : Boolean
      read FInitialize
      write SetInitialize
      default DefInitialize;

    property LineColor     : TColor
      read FLineColor
      write SetLineColor
      default DefLineColor;

    property ReadOnly : Boolean
      read FReadOnly
      write FReadOnly
      default DefReadOnly;

    property ShortNames : Boolean
      read FShortNames
      write SetShortNames
      default DefShortNames;

    property WantDblClicks : Boolean
      read FWantDblClicks
      write SetWantDblClicks
      default DefWantDblClicks;

    property WeekStarts : TDayType
      read FWeekStarts
      write SetWeekStarts
      default DefWeekStarts;

    {events}
    property OnChange : TNotifyEvent
      read FOnChange
      write FOnChange;

    property OnDrawHeader : TDrawHeaderEvent
      read FOnDrawHeader
      write FOnDrawHeader;

    property OnDrawItem : TDrawItemEvent
      read FOnDrawItem
      write FOnDrawItem;

    property OnUserCommand : TUserCommandEvent
      read FOnUserCommand
      write FOnUserCommand;

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

    function DateString(const Mask : string): string;
      {-return the date string using the provided mask}
    function DayString : string;
      {-return the Day of week string}
    procedure IncDay(Delta : Integer);
      {-change the day by Delta (signed) days}
    procedure IncMonth(Delta : Integer);
      {-change the month by Delta (signed) months}
    procedure IncYear(Delta : Integer);
      {-change the year by Delta (signed) years}
    function MonthString : string;
      {-return the month string}
    procedure SetToday;
      {-set the calendar to todays date}

    property AsDateTime : TDateTime
      read GetAsDateTime
      write SetAsDateTime;

    property CalendarDate : TStDate
      read GetCalendarDate
      write SetCalendarDate;

    property Canvas;

    property Day : Integer
      read FDay
      write SetDay
      stored calIsDateStored;

    property IntlSupport : TOvcIntlSup
      read FIntlSup
      write SetIntlSupport;

    property Month : Integer
      read FMonth
      write SetMonth
      stored calIsDateStored;

    property Year : Integer
      read FYear
      write SetYear
      stored calIsDateStored;
  published
  end;

  TOvcCalendar = class(TOvcCustomCalendar)
  published
    property Initialize; {must be read before D/M/Y}
    property BorderStyle;
    property Colors;
    property Day;
    property DayNameLen;
    property DrawGridLines;
    property DrawHeader;
    property HeaderHeight;
    property LineColor;
    property Month;
    property ReadOnly;
    property ShortNames;
    property Year;
    property WantDblClicks;
    property WeekStarts;

    property OnChange;
    property OnDrawHeader;
    property OnDrawItem;
    property OnUserCommand;

    {inherited properties}
    property Align;
    property Controller;
    property Ctl3D;
    property DragCursor;
    property DragMode;
    property Enabled;
    property Font;
    property ParentCtl3D;
    property ParentFont;
    property ParentShowHint;
    property PopupMenu;
    property ShowHint;
    property TabOrder;
    property TabStop
      default DefTabStop;
    property Visible;

    {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;


implementation
