Library Calend;

{ Calend Add-In for the clySmic Icon Bar. (C) 1992 by clySmic Software.
                                          All Rights reserved. }

{define Debug}

{$ifndef Debug}
{$R-,I-,D-,L-,S-,V-,W-,G+}
{$endif}

Uses
  WinTypes,WinProcs,Strings,WinDOS;

{$R CALEND}
{$I ADD-IN.INC}

Const
  ShowDOW : Boolean = True;
  CLBVer : VerString = '1.70';

Var
  Yr,Mon,Day,DOW : Word;

{$D Calend Add-In for Clysbar. (C) 1992 by clySmic Software. All Rights Resv'd.}

{-----------------------------------------------}

{ --- Utility Procedures --- }

Function CenterTx(DC : HDC; Tx : PChar; Rect : TRect) : Integer;

Var
  Width,WinX,StrtX : Integer;

Begin
  { Ask Windows for the total pixel length of the string & calc starting X }
  Width := LoWord(GetTextExtent(DC,Tx,StrLen(Tx)));

  { Get total x width of window - don't add 1! }
  WinX := (Rect.Right - Rect.Left);

  { Calculate centered starting posn }
  StrtX := ((WinX - Width) Div 2) + Rect.Left;

  { Return }
  CenterTx := StrtX;
End {CenterTx};

{-----------------------------------------------}

{ --- Perform Add-In's initialization --- }

Function InitAddIn(CurVer : PChar) : InitResult; Export;

Begin
  { Version check }
  If StrComp(CurVer,CLBVer) <> 0
    Then InitAddIn := InitNotOk
    Else InitAddIn := InitOk;

End {InitAddIn};

{-----------------------------------------------}

{ --- Paint on the button (Clysbar does the background) --- }

Procedure PaintAddIn(Wnd : HWnd; DC : HDC; Pressed : Boolean); Export;

Const
  MonthName : Array [1..12] of PChar =
    ('JAN','FEB','MAR','APR','MAY','JUN',
     'JUL','AUG','SEP','OCT','NOV','DEC');

  Days : array [0..6] of PChar =
     ('SUN','MON','TUE','WED','THU','FRI','SAT');

Var
  Rect : TRect;
  NumFont,OldFont,SmlFont : HFont;
  Tx : Array[0..128] of Char;
  StrYr,StrDay : Array [0..4] of Char;
  StrtX,StrtY : Integer;
  TheIcon : HIcon;

Begin
  GetClientRect(Wnd,Rect);

  { Calc location of icon }
  StrtX := ((Rect.Right - Rect.Left) - GetSystemMetrics(sm_cxIcon)) Div 2;
  StrtY := ((Rect.Bottom - Rect.Top) - GetSystemMetrics(sm_cyIcon)) Div 2;

  { Draw turning page if pressed }
  If Pressed
    Then Begin
           TheIcon := LoadIcon(hInstance,'CALEND2');
           DrawIcon(DC,StrtX+1,StrtY+1,TheIcon);

           Exit;
         End;

  TheIcon := LoadIcon(hInstance,'CALEND');

  DrawIcon(DC,StrtX,StrtY,TheIcon);

  GetDate(Yr,Mon,Day,DOW);
  Str(Day,StrDay);
  Str(Yr,StrYr);

  SmlFont :=
  CreateFont(9,             { Height }
             0,0,0,         { Width, left 2 right, normal orientation }
             400,           { Weight }
             0,0,0,         { Italic, underlined, or strikeout }
             0,             { ANSI char set }
             0,             { Reserved precision field }
             0,             { Default clipping }
             Proof_Quality, { Quality }
             ff_Roman Or Variable_Pitch,
             'Small Fonts');

  NumFont :=
  CreateFont(17,            { Height }
             0,0,0,         { Width, left 2 right, normal orientation }
             700,           { Weight }
             0,0,0,         { Italic, underlined, or strikeout }
             0,             { ANSI char set }
             0,             { Reserved precision field }
             0,             { Default clipping }
             Proof_Quality, { Quality }
             ff_Roman Or Variable_Pitch,
             'Times New Roman');

  OldFont := SelectObject(DC,NumFont);

  SetBkMode(DC,Transparent);
  SetTextColor(DC,RGB(0,0,0));

  Inc(Rect.Top,1);

  DrawText(DC,StrDay,StrLen(StrDay),Rect,
           dt_Center or dt_VCenter or dt_SingleLine);

  SelectObject(DC,SmlFont);

  { Adjust rect to account for off-center pad image }
  Dec(Rect.Right,2);

  StrCopy(Tx,MonthName[Mon]);
  SetTextColor(DC,RGB(255,0,0));
  TextOut(DC,CenterTx(DC,Tx,Rect),StrtY + 3,Tx,StrLen(Tx));

  If ShowDOW
    Then Begin
           StrCopy(Tx,Days[DOW]);
           SetTextColor(DC,RGB(0,0,128));
           TextOut(DC,CenterTx(DC,Tx,Rect),StrtY + 22,Tx,StrLen(Tx));
         End
    Else Begin
           StrCopy(Tx,StrYr);
           SetTextColor(DC,RGB(128,0,128));
           TextOut(DC,CenterTx(DC,Tx,Rect),StrtY + 22,Tx,StrLen(Tx));
         End;

  SelectObject(DC,OldFont);
  DeleteObject(SmlFont);
  DeleteObject(NumFont);

End {PaintAddIn};

{-----------------------------------------------}

{ --- Tell Clysbar what kind of timer we need --- }

Function TimerNeeded : Integer; Export;

Begin
  TimerNeeded := ait_Slow;
End {TimerNeeded};

{-----------------------------------------------}

{ --- Proc called when timer expires, perform timed duties --- }

Procedure AddInTimer(Wnd : HWnd; DC : HDC); Export;

Var
  TimerDay : Word;

Begin
  { Check for a date change }
  GetDate(Yr,Mon,TimerDay,DOW);

  { If different, repaint window }
  If TimerDay <> Day
    Then PaintAddIn(Wnd,DC,False);

End {AddInTimer};

{-----------------------------------------------}

{ --- Proc called when button pressed --- }

Procedure AddInPressed(Wnd : HWnd; DC : HDC); Export;

Begin
  { Toggle the "show day-of-week" indicator when button pressed }
  ShowDOW := Not ShowDOW;
  PaintAddIn(Wnd,DC,False);
End {AddInPressed};

{-----------------------------------------------}

{ --- Exit processing for Add-In --- }

Procedure ExitAddIn; Export;

Begin
End {ExitAddIn};

{-----------------------------------------------}

{ --- Clysbar queries Add-In about itself --- }

Procedure AddInAbout(Str1,Str2 : PChar;
                     Var TheIcon : HIcon;
                     Var TitleCol,TxCol,BkCol : TColorRef); Export;

Begin
  StrCopy(Str1,'Calend V1.70');
  StrCopy(Str2,'A Page-per-Day Calendar'#13'© 1992 by clySmic Software.'#13'All Rights Reserved.');

  TheIcon := LoadIcon(hInstance,'ABOUT');

  TitleCol := RGB(0,0,255);
  TxCol := RGB(0,0,128);
  BkCol := RGB(192,192,192);
End {AddInAbout};

{-----------------------------------------------}

Exports InitAddIn    Index 1,
        PaintAddIn   Index 2,
        TimerNeeded  Index 3,
        AddInTimer   Index 4,
        AddInPressed Index 5,
        ExitAddIn    Index 6,
        AddInAbout   Index 7;

Begin
End.
