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

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

unit OvcNbk;

interface

uses
  {$IFDEF Win32} Windows, {$ELSE} WinTypes, WinProcs, {$ENDIF}
  Buttons, Classes, Controls, DsgnIntf, Forms, Graphics,
  Menus, Messages, SysUtils, StdCtrls,
  OvcBase, OvcCmd, OvcConst, OvcData, OvcExcpt, OvcMisc;

type
  TTabOrientation = (toTop, toRight);

const
  DefColor             = clBtnFace;
  DefConserveResources = False;
  DefCtl3D             = False;
  DefEchoPageHint      = False;
  DefEchoPageHelp      = False;
  DefHeight            = 185;
  DefHighlightColor    = clBtnHighlight;
  DefHitWidth          = 6;
  DefLeftMargin        = 4;
  DefMinTabWidth       = 20;
  DefPageIndex         = 0;
  DefParentColor       = False;
  DefParentCtl3D       = False;
  DefShadowColor       = clBtnShadow;
  DefShadowedText      = False;
  DefStyle             = bsAutoDetect;
  DefTabAutoHeight     = True;
  DefTabHeight         = 20;
  DefTabIndent         = 8;
  DefTabStop           = True;
  DefTabTextColor      = clBtnText;
  DefTabWidth          = 65;
  DefTabOrientation    = toTop;
  DefTabRowCount       = 0;
  DefTextShadowColor   = clBtnShadow;
  DefTopMargin         = 2;
  DefWidth             = 220;

type
  {event types}
  TDrawTabEvent =
    procedure(Sender : TObject; Index : Integer; const Title : string;
              R : TRect; Enabled, Active : Boolean)
    of object;
  {event generated when drawing the tab text}
  TMouseOverTabEvent =
    procedure(Sender : TObject; Index : Integer)
    of object;
  {event generated when the mouse passes over a tab}
  TPageChangeEvent =
    procedure(Sender : TObject; Index : Integer; var AllowChange : Boolean)
    of object;
  {event generated when the tab is about to be changed}
  TPageChangedEvent =
    procedure(Sender : TObject; Index : Integer)
    of object;
  {event generated after a new tab has been selected}
  TTabClickEvent =
    procedure(Sender : TObject; Index : Integer)
    of object;
  {event generated when a tab is clicked with the left mouse button}

type
  {forward declaration for folder tab pages}
  TOvcNotebookPage = class;

  TOvcNotebook = class(TOvcBase)
  {.Z+}
  protected {private}
    {property varialbes}
    FActiveTabFont     : TFont;        {font for the active tab}
    FConserveResources : Boolean;      {if true, non visual page handles are destroyed}
    FDefaultPageIndex  : Integer;      {page to display when first created}
    FEchoPageHelp      : Boolean;      {echo page help to notebook}
    FEchoPageHint      : Boolean;      {echo page hint to notebook}
    FHighlightColor    : TColor;       {color for border highlight}
    FPageAccess        : TStrings;     {allows acces to tab pages and info}
    FPageIndex         : Integer;      {current notebook page index}
    FShadowColor       : TColor;       {color for tab shadow}
    FShadowedText      : Boolean;      {true to draw shadowed tab text}
    FStyle             : TButtonStyle; {determines how tabs and pages look}
    FTabAutoHeight     : Boolean;      {true to enable height auto adjust}
    FTabHeight         : LongInt;      {the height of each tab}
    FTabIndent         : LongInt;      {pixels to indent each tab row}
    FTabOrientation    : TTabOrientation;
    FTabRowCount       : Integer;      {number of tab rows. 0=autocalc}
    FTabWidth          : LongInt;      {width of notebook tabs}
    FTextShadowColor   : TColor;       {color for text shadowing}

    {event variables}
    FOnDrawTab         : TDrawTabEvent;
    FOnMouseOverTab    : TMouseOverTabEvent;
    FOnPageChange      : TPageChangeEvent;
    FOnPageChanged     : TPageChangedEvent;
    FOnTabClick        : TTabClickEvent;
    FOnUserCommand     : TUserCommandEvent;

    {internal variables}
    nbFocusedRow  : Integer;    {index of the row containing the active tab}
    nbHitTest     : TPoint;     {location of mouse cursor}
    nbLastRow     : Integer;    {the last row index -- nbTotalRows-1}
    nbOverTab     : Boolean;    {true if the mouse os over a tab}
    nbResizeWidth : Boolean;    {true if resizing tab widths}
    nbResizeIndent: Boolean;    {true if resizing tab indents}
    nbPageList    : TList;      {list of TOvcNotebookPage objects}
    nbTabCursor   : HCursor;    {design-time tab slecting cursor handle}
    nbTabSelecting: Boolean;    {true while moving through tabs}
    nbTabsPerRow  : LongInt;    {number of tabs per row}
    nbTotalRows   : LongInt;    {total count of tab rows}

    {property methods}
    function GetActivePageName : string;
    function GetClientHeight : Integer;
    function GetClientWidth : Integer;
    function GetPageEnabled : Boolean;
    function GetPageHelpContext : THelpContext;
    function GetPageHelpContexts(Index : Integer) : THelpContext;
    function GetPageHint : string;
    function GetPageHints(Index : Integer) : string;
    function GetPageName : string;
    function GetPageNames(Index : Integer) : string;
    function GetPageObjects(Index : Integer) : TOvcNotebookPage;
    function GetPagesEnabled(Index : Integer) : Boolean;
    function GetPagesVisible(Index : Integer) : Boolean;
    function GetTabOrientation : TTabOrientation;
    function GetTabTextColor : TColor;
    function GetTabsTextColor(Index : Integer) : TColor;
    procedure SetActivePageName(const Value : string);
    procedure SetActiveTabFont(Value : TFont);
    procedure SetConserveResources(Value : Boolean);
    procedure SetDefaultPageIndex(Value : Integer);
    procedure SetHighlightColor(const Value : TColor);
    procedure SetPageEnabled(Value : Boolean);
    procedure SetPageHelpContext(Value : THelpContext);
    procedure SetPageHelpContexts(Index : Integer; Value : THelpContext);
    procedure SetPageHint(const Value : string);
    procedure SetPageHints(Index : Integer; const Value : string);
    procedure SetPageIndex(Value : Integer);
    procedure SetPageName(const Value : string);
    procedure SetPageNames(Index : Integer; const Value : string);
    procedure SetPages(Strings : TStrings);
    procedure SetPagesEnabled(Index : Integer; Value : Boolean);
    procedure SetPagesVisible(Index : Integer; Value : Boolean);
    procedure SetShadowColor(const Value : TColor);
    procedure SetShadowedText(Value : Boolean);
    procedure SetStyle(Value : TButtonStyle);
    procedure SetTabAutoHeight(Value : Boolean);
    procedure SetTabHeight(Value : LongInt);
    procedure SetTabIndent(Value : LongInt);
    procedure SetTabOrientation(Value : TTabOrientation);
    procedure SetTabRowCount(Value : Integer);
    procedure SetTabsTextColor(Index : Integer; Value : TColor);
    procedure SetTabTextColor(Value : TColor);
    procedure SetTabWidth(Value : LongInt);
    procedure SetTextShadowColor(const Value : TColor);

    {internal methods}
    procedure nbAdjustPageSize;
    procedure nbCalcTabInfo;
    procedure nbDoRefresh;
    procedure nbDrawFocusRect(Index : Integer);
    procedure nbFontChanged(Sender : TObject);
    function nbGetTabRect(Index : Integer) : TRect;
    procedure nbInvalidateTab(Index : Integer);
    function nbIsValidIndex(Index : Integer) : Boolean;
    function nbNextValidIndex(Index : Integer) : Integer;
    procedure nbPaintTopTabs;
    procedure nbPaintRightTabs;
    function nbPrevValidIndex(Index : Integer) : Integer;

    {VCL control methods}
    procedure CMDesignHitTest(var Msg : TCMDesignHitTest);
      message CM_DESIGNHITTEST;
    procedure CMDialogChar(var Msg : TCMDialogChar);
      message CM_DIALOGCHAR;
    procedure CMFontChanged(var Msg : TMessage);
      message CM_FONTCHANGED;
    procedure CMParentColorChanged(var Msg : TMessage);
      message CM_PARENTCOLORCHANGED;
    procedure CMShowHintChanged(var Msg : TMessage);
      message CM_SHOWHINTCHANGED;

    {message response methods}
    procedure WMEraseBkGnd(var Msg : TWMEraseBkGnd);
      message WM_ERASEBKGND;
    procedure WMGetDlgCode(var Msg : TWMGetDlgCode);
      message WM_GETDLGCODE;
    procedure WMKeyDown(var Msg : TWMKeyDown);
      message WM_KEYDOWN;
    procedure WMKillFocus(var Msg : TWMKillFocus);
      message WM_KILLFOCUS;
    procedure WMMouseActivate(var Msg : TWMMouseActivate);
      message WM_MOUSEACTIVATE;
    procedure WMNCHitTest(var Msg : TWMNCHitTest);
      message WM_NCHITTEST;
    procedure WMSetCursor(var Msg : TWMSetCursor);
      message WM_SETCURSOR;
    procedure WMSetFocus(var Msg : TWMSetFocus);
      message WM_SETFOCUS;

  protected
    procedure ChangeScale(M, D : Integer);
      override;
    procedure CreateParams(var Params : TCreateParams);
      override;
    procedure CreateWnd;
      override;
    {$IFDEF Win32}
    function GetChildOwner : TComponent;
      override;
    procedure GetChildren(Proc : TGetChildProc);
      override;
    {$ENDIF}
    procedure Loaded;
      override;
    procedure Paint;
      override;
    procedure ReadState(Reader : TReader);
      override;
    procedure SetBounds(ALeft, ATop, AWidth, AHeight : Integer);
      override;
    procedure ShowControl(AControl : TControl);
      override;
    {$IFNDEF Win32}
    procedure WriteComponents(Writer : TWriter);
      override;
    {$ENDIF}

    {dynamic event handlers}
    function DoOnDrawTab(Index : Integer; const Title : string;
             Enabled, Active : Boolean) : Boolean;
      dynamic;
      {-fire the OnDrawTab event if assigned}
    procedure DoOnMouseOverTab(Index : Integer);
      dynamic;
      {-fire the OnMouseOverTab event if assigned}
    procedure DoOnPageChange(Index : Integer; var AllowChange : Boolean);
      dynamic;
      {-fire the OnPageChange event if assigned}
    procedure DoOnPageChanged(Index : Integer);
      dynamic;
      {-fire the OnPageChanged event if assigned}
    procedure DoOnTabClick(Index : Integer);
      dynamic;
      {-fire the OnTabClick event if assigned}
    procedure DoOnUserCommand(Command : Word);
      dynamic;
      {-perform notification of a user command}

    {vcl methods}
    procedure MouseDown(Button : TMouseButton; Shift : TShiftState; X, Y : Integer);
      override;
    procedure MouseMove(Shift : TShiftState; X, Y: Integer);
      override;
    procedure MouseUp(Button : TMouseButton; Shift : TShiftState; X, Y : Integer);
      override;
  {.Z-}

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

    function GetTabRect(Index : Integer) : TRect;
      {-return the area of the specified tab}
    procedure DeletePage(Index : Integer);
      {-delete the specfied notebook page}
    function InsertPage(const Name : string;
                        Index : Integer) : TOvcNotebookPage;
      {-insert a notebook page at the specified location}
    procedure InvalidateTab(Index : Integer);
      {-invalidate the specified tab}
    function IsValid(Index : Integer) : Boolean;
      {-return true if the Index tab is valid}
    procedure NextPage;
      {-display the next valid notebook page}
    procedure PrevPage;
      {-display the previous valid notebook page}
    function PageNameToIndex(const Name : string) : Integer;
      {-return page index given the page name}
    property Canvas;

    property ClientHeight : Integer
      read GetClientHeight;

    property ClientWidth : Integer
      read GetClientWidth;

    property PageHelpContexts[Index : Integer] : THelpContext
      read GetPageHelpContexts
      write SetPageHelpContexts;

    property PageHints[Index : Integer] : string
      read GetPageHints
      write SetPageHints;

    property PageNames[Index : Integer] : string
      read GetPageNames
      write SetPageNames;

    property PageObjects[Index : Integer] : TOvcNotebookPage
      read GetPageObjects;

    property PagesEnabled[Index : Integer] : Boolean
      read GetPagesEnabled
      write SetPagesEnabled;

    property PagesVisible[Index : Integer] : Boolean
      read GetPagesVisible
      write SetPagesVisible;

    property TabsTextColor[Index : Integer] : TColor
      read GetTabsTextColor
      write SetTabsTextColor;

  published
    property ActivePageName : string
      read GetActivePageName
      write SetActivePageName
      stored False;

    property ActiveTabFont : TFont
      read FActiveTabFont
      write SetActiveTabFont;

    property ConserveResources : Boolean
      read FConserveResources
      write SetConserveResources;

    property DefaultPageIndex : Integer
      read FDefaultPageIndex
      write SetDefaultPageIndex
      default DefPageIndex;

    property EchoPageHelp : Boolean
      read FEchoPageHelp
      write FEchoPageHelp
      default DefEchoPageHelp;

    property EchoPageHint : Boolean
      read FEchoPageHint
      write FEchoPageHint
      default DefEchoPageHint;

    property HighlightColor : TColor
      read FHighlightColor
      write SetHighlightColor
      default DefHighlightColor;

  {.Z+}
    property PageEnabled : Boolean
      read GetPageEnabled
      write SetPageEnabled
      stored False;

    property PageHelpContext : THelpContext
      read GetPageHelpContext
      write SetPageHelpContext;

    property PageHint : string
      read GetPageHint
      write SetPageHint;
  {.Z-}

    property PageIndex : Integer
      read FPageIndex
      write SetPageIndex
      stored False;

    property PageName : string
      read GetPageName
      write SetPageName
      stored False;

    property Pages : TStrings
      read FPageAccess
      write SetPages
      stored False;

    property ShadowColor : TColor
      read FShadowColor
      write SetShadowColor
      default DefShadowColor;

    property ShadowedText : Boolean
      read FShadowedText
      write SetShadowedText
      default DefShadowedText;

    property Style : TButtonStyle
      read FStyle
      write SetStyle
      default DefStyle;

    property TabAutoHeight : Boolean
      read FTabAutoHeight
      write SetTabAutoHeight
      default DefTabAutoHeight;

    property TabHeight : LongInt
      read FTabHeight
      write SetTabHeight
      default DefTabHeight;

    property TabIndent : LongInt
      read FTabIndent
      write SetTabIndent
      default DefTabIndent;

    property TabOrientation : TTabOrientation
      read GetTabOrientation
      write SetTabOrientation
      default DefTabOrientation;

    property TabRowCount : Integer
      read FTabRowCount
      write SetTabRowCount
      default DefTabRowCount;

  {.Z+}
    property TabTextColor : TColor
      read GetTabTextColor
      write SetTabTextColor
      stored False;
  {.Z-}

    property TabWidth : LongInt
      read FTabWidth
      write SetTabWidth
      default DefTabWidth;

    property TextShadowColor : TColor
      read FTextShadowColor
      write SetTextShadowColor
      default DefTextShadowColor;

    {event properties}
    property OnDrawTab : TDrawTabEvent
      read FOnDrawTab
      write FOnDrawTab;

    property OnMouseOverTab : TMouseOverTabEvent
      read FOnMouseOverTab
      write FOnMouseOverTab;

    property OnPageChange : TPageChangeEvent
      read FOnPageChange
      write FOnPageChange;

    property OnPageChanged : TPageChangedEvent
      read FOnPageChanged
      write FOnPageChanged;

    property OnTabClick : TTabClickEvent
      read FOnTabClick
      write FOnTabClick;

    property OnUserCommand : TUserCommandEvent
      read FOnUserCommand
      write FOnUserCommand;

    {inherited properties}
    property Align;
    property Controller;
    property Color default DefColor;
    property Ctl3D default DefCtl3D;
    property DragCursor;
    property DragMode;
    property Enabled;
    property Font;
    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;

  {invisible page control to act as parent for other components}
  TOvcNotebookPage = class(TWinControl)
  {.Z+}
  protected {private}
    FArea            : TRect;    {tab dimensions}
    FRow             : Integer;  {row this tab is in}
    FPageEnabled     : Boolean;  {true if tab not disabled}
    FTabTextColor    : TColor;   {text color for tab text}
    FTabVisible      : Boolean;  {true if tab is to be shown}

    procedure CMParentShowHintChanged(var Msg : TMessage);
      message CM_PARENTSHOWHINTCHANGED;
    procedure WMNCHitTest(var Msg : TWMNCHitTest);
      message WM_NCHITTEST;

  protected
    procedure ReadState(Reader : TReader);
      override;
  {.Z-}

  public
    constructor Create(AOwner : TComponent);
      override;

    property Area : TRect
      read FArea
      write FArea
      stored False;

  published
    property PageEnabled : Boolean
      read FPageEnabled
      write FPageEnabled
      default True;

    property Row : Integer
      read FRow
      write FRow
      stored False;

    property TabTextColor : TColor
      read FTabTextColor
      write FTabTextColor
      default DefTabTextColor;

    property TabVisible : Boolean
      read FTabVisible
      write FTabVisible
      default True;

    property Caption;
    property Height stored False;
    property Left stored False;
    property TabOrder stored False;
    property Top stored False;
    property Visible stored False;
    property Width stored False;
  end;

type
  TPageAccess = class(TStrings)
  {.Z+}
  protected {private}
    paPageList  : TList;
    paNotebook  : TOvcNotebook;

  protected
    function GetCount : Integer;
      override;
    function Get(Index : Integer) : string;
      override;
    procedure Put(Index : Integer; const S : string);
      override;
    function GetObject(Index : Integer) : TObject;
      override;
    procedure SetUpdateState(Updating : Boolean);
      override;
  {.Z-}

  public
    constructor Create(APageList : TList; ANotebook : TOvcNotebook);
    procedure Clear;
      override;
    procedure Delete(Index : Integer);
      override;
    procedure Insert(Index : Integer; const S : string);
      override;
    procedure Move(CurIndex, NewIndex : Integer);
      override;
    procedure Exchange(Index1, Index2 : Integer);
      override;
  end;


implementation
