{##
  TimeLine v1.0 -> a component to display and manipulate (Drag&Drop) periods of time

  written 1997 by Markus Plesser (e9300600@stud1.tuwien.ac.at)
  (c)1997 by MANAGERsoft
  (still not an official company YET - so please dont blame me ;-)

  This component is ShareWare:
  You are NOT ALLOWED NOT USE this component in a COMMERCIAL APPLICATION if you
  have not registered!

  You can now download the latest version (& other stuff) from
  "http://stud1.tuwien.ac.at/~e9300600"

  Properties:
  ===========

    BorderStyle ... What kind of border to display for the time-periods
    CellWidth   ... Width in pixels for a "time-unit" (only days right now)
    Cursor      ... No effect, but I dont know how to get rid of it :-(
    DateLabel   ... You can link a label to display the date that is currently
                    "under" the cursor
    Height      ... Height of the component (will normally be reduced to grant
                    a better fit for the component)
    Hint        ... No effect, but I dont know how to get rid of it :-(
    HScrollBar  ... You can link a scrollbar to move in horizontal direction
    LineHeight  ... Better: "CellHeight", the height of each "time-unit"
    Lines       ... StringList for the text to be displayed for each line
    Spacing     ... Currently only for HalfDays, more to come!
    VScrollBar  ... You can link a scrollbar to move through the lines
    Width       ... Width of the component (will normally be reduced to grant
                    a better fit for the component)


  Events:   (see interface-part for detailed parameters)
  =======

    onChange    ... Occurs whenever a time-period has changed and provides you
                    with a way to accept it or not
    onSelect    ... Occurs whenever a time-period is selected


  Methods:  (see interface-part for detailed parameters)
  ========

    ClearEntrys ... Delete all lines
    AddEntry    ... Add a time-period
    DelEntry    ... Delete a time-period
    GetEntry    ... Returns the time-period with the given id
    MoveBy      ... Move horizontally
    MoveLinesBy ... Move vertically
    ChangeDisplayTextIndex ... Change the Text to display for a time-period
    ChangeEntryColor ... Change the displaycolor of a time-period
    RepaintTimeLine  ... Repaints the Component


  Warrenties
  ==========
  There are _no_ warrenties at all for this component. If it doesnt work,
  dont blame me. If you want any special functionality, Im not planning to
  implement, you have to work yourself through the code youll get after
  registration. (Youll gain all the help I can give...)
  Im planning to enhance this component as soon as possible, but I dont
  promise anything!


  How to register
  ===============
  While you are not registered, you can not run this component outside Delphi!
  There are two ways of registering:
  1) Either you send me a component you've made on your own or a component you
     think that is worth looking at (pls. no components you can find everywhere
     and/or worthless comps. ).
     Please remember - I only accept components INCLUDING _FULL_ SOURCE-CODE !!!

  2) I charge a fee of US$ 20,- (ATS 260,- / DM 38,-).
     Please transfer your money _DIRECTLY_ to the following account:
       CREDITANSTALT
       BLZ.:    11000
       Kto.Nr.: 12412420700
     or send the money in an envelope (better put a piece of paper around the cash,
     so no postman will see and take it...) to the following adress:
       Markus Plesser
       Waehringer Guertel 69/4
       A-1180 Wien
       Austria/Europe
     Please inform me via e-mail, before you send the money, in case Ive moved
     to another location.


  In either ways youll get the password via e-mail.
  If you want me to send you the password by mail, add another 5 US$ for s&h.

  Im very interested in components like editors (>64k, Fontsupport),
  Database-Comps (stuff like InfoPower or cbPlus, but freeware or reg. to me),
  network & winsock stuff, etc.

  If you have any suggestions, comments or bugs please mail me in english or
  german to the above adress.

  Why I wrote this stuff:
  =======================
  Ive been writing a program for hotels and I was in need for a component to
  show the occupation of rooms -> well, thats the result.


  By the way:
  Take a look at my DateBox and Led components located at my homepage or many
  Delphi-Sites like Torrys or DSP as well.


  Revision history:
  =================

  Date       Rel. Edit Who Changes
  ========== ==== ==== === ==================================================
  04.02.1997 1.00 0000 MP  Created, Initial Release



  Known bugs:
  ===========
  Component will not be drawn, if the lines-property is empty.


  Planned enhancements:
  =====================
  More kinds of spacing, like FullDays, Minutes and so on.
  A request-event, to read the data for specified time-range, to speed-up
  database-access.
  A Help-file.
  This is just the initial release - so please dont expect too much ;-)

  If you want to send me a postcard etc., you can reach me at:
    Markus Plesser
    Waehringer Guertel 69/4
    A-1180 Wien
    Austria/Europe
}
unit TimeLine;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls;

const tlSeperator: char = '';   {## Not used yet! }

type
  TEntry = record
    Id:          LongInt;   {## Id MUST be unique - NOT checked! }
    Line:        Integer;   {## With which line to associate (=display) }
    StartDate,              {## Beginning of Entry }
    EndDate:     TDateTime; {## End of Entry }
    Color:       TColor;    {## Color of the Entry }
    Description: String;    {## Text shown, when overlapping ; in Selection-Dlg}
    Display1,               {## Text displayed on Entry }
    Display2:    String;    {## Variation of -"-      }
  end;

  PEntry = ^TEntry;

  TSpacing = (spHalfDays
  {, spFullDays, spHours, spHalfHours, spMinutes}  {## Maybe more to come ... }
  );

  TTLBorderStyle = (bsTriangle, bsRound, bsFlat, bsArrowLeft, bsArrowRight);

  TTLDragMode = (dmNone, dmLeft, dmRight, dmAll);

  TEntryChangeEvent = procedure(Sender: TObject; Id: LongInt; StartDate,
    EndDate: TDateTime; Line: Integer; var AcceptIt: Boolean) of object;
  TEntrySelectEvent = procedure(Sender: TObject; Id: LongInt) of object;

  TTimeLine = class(TGraphicControl)
  private
    FSavedOnIdle: TIdleEvent;
    FFocused:     Boolean;     {## Have we got the Focus ???}
    FLineHeight:  Integer;     {## Height of each line }
    FLines:       TStringList; {## Displayed lines = description}
    FLineDescWidth: Integer;   {## width of descriptions }
    FTitleHeight: Integer;     {## height of the title-area }
    FTitleBackGround: TColor;  {## color of --"-- }
    FClientBackGround: TColor; {## color of the client-area }
    FDescBackGround:   TColor; {## color of descriptions (lines) }

    FBorderStyle: TTLBorderStyle; {## What kind of borders to draw }

    FLinesShown:  Integer;     {## Number of lines shown }
    FTopLine:     Integer;     {## currently displayed topline }

    FSpacing:     TSpacing;    {## time-scale }
    FCellWidth:   Integer;     {## width of each cell }

    FCurrDate:    TDateTime;   {## date displayed on the left side }

    FList:        TList;       {## List containing all entries }
    FSelectedId:  LongInt;     {## currently selected entry }
    FSelectArea:  TRect;       {## extends of the currently selected entry }
    FOrgSelEntry: TEntry;      {## starting-position }
    FResEntries:  array[0..100] of PEntry;  {## filled, if entries are overlapping }
    FDispTxtIdx:  Integer;     {## variation# of the text being displayed }

    FXCells: array[0..200] of Integer; {## Cell-coordinates }
    FYCells: array[0..100] of Integer; {## Idx 0=count of cells }

    FInDragging:    Boolean;   {## In drag-mode ???                   }
    FDragStartDate: TDateTime; {## the date we started dragging       }
    FDragId:        Integer;   {## Id          -"-                    }
    FDragLine:      Integer;   {## Line        -"-                    }
    FDragMode:      TTLDragMode; {## What kind of dragging is active ?}

    FNeedRebuild:   Boolean;   {## Needs the image to be rebuilt    ? }

    FDateLabel:     TLabel;    {## A label that can be assigned to show the Date
                                   under the mouse-cursor }
    FHScrollBar,
    FVScrollBar:    TScrollBar; {## Scrollbars, that can be assigned for movement }
    FHScrollOldPos: Integer;

    FOnChange: TEntryChangeEvent; {## s.t. has changed }
    FOnSelect: TEntrySelectEvent; {## Entry is selected/none is selected (=-1) }

    DrawBuffer:   TBitMap;  {## doublebuffering to prevent flickering }


    procedure GetFocus;   {## We got the Focus }
    procedure LooseFocus; {## We lost --"--    }
    procedure ReCalcLines;{## Calculate new line-dimensions }
  protected
    procedure Paint; override;

    procedure BelEnter(var Message: TCMEnter); message CM_ENTER;
    procedure BelExit(var Message: TCMEnter); message CM_EXIT;
    procedure MouseUp(Button: TMouseButton;Shift:TShiftState;X,Y:Integer); override;
    procedure MouseDown(Button: TMouseButton;Shift:TShiftState;X,Y:Integer); override;
    procedure MouseMove(Shift:TShiftState;X,Y:Integer); override;

    procedure SetLineHeight(Value: Integer);
    procedure SetLines(Value: TStringList);
    procedure SetSpacing(Value: TSpacing);
    procedure SetCellWidth(Value: Integer);

    procedure SetVScrollBar(Value: TScrollBar);
    procedure VScrollBarScroll(Sender: TObject;ScrollCode: TScrollCode; var ScrollPos: Integer);
    procedure SetHScrollBar(Value: TScrollBar);
    procedure HScrollBarScroll(Sender: TObject;ScrollCode: TScrollCode; var ScrollPos: Integer);
    procedure SetDateLabel(Value: TLabel);

    procedure SetBorderStyle(Value: TTLBorderStyle);

    procedure TLAppIdle(Sender: TObject; var Done: Boolean);

    procedure DrawEntry(Entry: PEntry);
      {## Draw an entry }
    function  GetEntryAtPos(EDate: TDateTime; ELine: Integer): PEntry;
      {## get entry at a given date and line }
    procedure RebuildImage;
      {## rebuild and draw the buffer }
  public
    constructor Create(AOwner: TComponent); override;
    destructor  Destroy; override;

    procedure ClearEntrys;
      {## Delete all entries from the list }
    procedure AddEntry(Id: LongInt; Line: Integer; StartDate, EndDate: TDateTime;
                       Color: TColor; Description, Display1, Display2: String);
      {## insert an entry }
    procedure DelEntry(Id: LongInt);
      {## delete a special entry }
    function  GetEntry(Id: LongInt): PEntry;
      {## search for the id of an entry }
    procedure MoveBy(Dist: Integer);
      {## move horizontally }
    procedure MoveLinesBy(Dist: Integer);
      {## move vertically }
    procedure ChangeDisplayTextIndex(Value: Integer);
      {## Change the Text to display }
    procedure ChangeEntryColor(Id: LongInt; NewColor: TColor);
      {## Change the displaycolor of an entry }
    procedure RepaintTimeLine;
      {## Repaints the Image }
    procedure GoToDate(StartDate: TDateTime);
      {## Start the display at a given day }  

  published
    property Top;
    property Left;
    property Width;
    property Height;

    property LineHeight: Integer read FLineHeight write SetLineHeight;
    property Lines: TStringList read FLines write SetLines;
    property Spacing: TSpacing read FSpacing write SetSpacing default spHalfDays;
    property CellWidth: Integer read FCellWidth write SetCellWidth default 15;
    property BorderStyle: TTLBorderStyle read FBorderStyle write SetBorderStyle;

    property VScrollBar: TScrollbar read FVScrollBar write SetVScrollBar default nil;
    property HScrollBar: TScrollbar read FHScrollBar write SetHScrollBar;
    property DateLabel:  TLabel read FDateLabel write SetDateLabel;

    property onChange: TEntryChangeEvent read FOnChange write FOnChange;
    property onSelect: TEntrySelectEvent read FOnSelect write FOnSelect;
  end;

procedure Register;

implementation
