Library Calend;

{ Calend Add-In for the clySmic Icon Bar. (C) 1996 by clySmic Software.
                                          All Rights reserved. }

{define Debug}

{$ifndef Debug}
{$R-,I-,D-,L-,S-,V-,W-,G+}
{$endif}

{$C MOVEABLE PRELOAD DISCARDABLE}

Uses
  WinTypes,WinProcs,Strings,WinDOS,TPWTools;

{$R CALEND}
{$I ADD-IN.INC}
{$I ..\VERS}

Const
  ShowDOW : Boolean = True;
  CLBVer : VerString = AddInMasterVer;

Var
  Yr,Mon,Day,DOW : Word;
  CalendProgram,IWDate,INIFile : Array [0..80] of Char;
  GTxInBtn,USDateFormat : Boolean;

{$D Calend Add-In for Clysbar - clySmic Software}

{-----------------------------------------------}

{ --- 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};

{-----------------------------------------------}

Procedure FormIWDate;

Const
  MonthName : Array [1..12] of PChar =
    ('January','February','March','April','May','June',
     'July','August','September','October','November','December');

  Days : array [0..6] of PChar =
     ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');

Var
  StrDay,StrYr : Array [0..4] of Char;

Begin
  GetDate(Yr,Mon,Day,DOW);
  Str(Day,StrDay);
  Str(Yr,StrYr);

  If USDateFormat
    Then Begin
           { US date fmt }
           StrCopy(IWDate,Days[DOW]);
           StrCat(IWDate,', ');
           StrCat(IWDate,MonthName[Mon]);
           StrCat(IWDate,' ');
           StrCat(IWDate,StrDay);
           StrCat(IWDate,', ');
           StrCat(IWDate,StrYr);
         End
    Else Begin
           { European date fmt }
           StrCopy(IWDate,Days[DOW]);
           StrCat(IWDate,', ');
           StrCat(IWDate,StrDay);
           StrCat(IWDate,' ');
           StrCat(IWDate,MonthName[Mon]);
           StrCat(IWDate,' ');
           StrCat(IWDate,StrYr);
         End;
End {FormIWDate};

{-----------------------------------------------}

{ --- Perform Add-In's initialization --- }

Function AddInInit(CurVer : PChar; TxInBtn : Boolean) : InitResult; Export;

Begin
  { Version check }
  If StrLComp(CurVer,CLBVer,3) <> 0
    Then AddInInit := InitNotOk
    Else AddInInit := InitOk;

  { Point at our INI file, which is in our home dir }
  HomeDir(hInstance,INIFile,Sizeof(INIFile));
  StrCat(INIFile,'CALEND.INI');

  { Flush INI file so we can edit it }
  WritePrivateProfileString(Nil,Nil,Nil,INIFile);

  { Get date format }
  USDateFormat := Boolean(GetPrivateProfileInt('Settings',
                                               'USDateFormat',
                                               0,
                                               INIFile));

  { Get program to run if shift-clicked. Default = Schedule+ }
  GetPrivateProfileString('Settings',
                          'CalendProgram',
                          'SCHDPLUS.EXE',
                          CalendProgram,
                          SizeOf(CalendProgram),
                          INIFile);

  { Save text-in-button flag }
  GTxInBtn := TxInBtn;
End {AddInInit};

{-----------------------------------------------}

{ --- Paint on the button (Clysbar does the background) --- }

Procedure AddInPaint(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
  ShadRect,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
  { If add-in is never uncovered, Wnd will be NULL }
  If Wnd = 0
    Then Exit;

  GetClientRect(Wnd,Rect);
  If GTxInBtn
    Then Rect.Right := Rect.Left + GetSystemMetrics(sm_cxIcon) + 12;

  { 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,'turning');
           DrawIcon(DC,StrtX+1,StrtY+1,TheIcon);

           Exit;
         End;

  { Draw "page" icon }
  TheIcon := LoadIcon(hInstance,'calend');
  DrawIcon(DC,StrtX,StrtY,TheIcon);

  { Get date info }
  GetDate(Yr,Mon,Day,DOW);
  Str(Day,StrDay);
  Str(Yr,StrYr);

  { Create small font for the month/day/year }
  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');

  { Create large font for the day number }
  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');

  { Setup for day number }
  OldFont := SelectObject(DC,NumFont);
  SetBkMode(DC,Transparent);

  { Draw lg day number's shadow }
  SetTextColor(DC,RGB(128,128,128));
  ShadRect := Rect;
  OffsetRect(ShadRect,2,1);
  DrawText(DC,StrDay,StrLen(StrDay),ShadRect,
           dt_Center or dt_VCenter or dt_SingleLine);

  { Draw lg day number }
  SetTextColor(DC,RGB(0,0,0));
  OffsetRect(Rect,1,0);
  DrawText(DC,StrDay,StrLen(StrDay),Rect,
           dt_Center or dt_VCenter or dt_SingleLine);

  { Setup for other info }
  SelectObject(DC,SmlFont);

  { Draw month name }
  StrCopy(Tx,MonthName[Mon]);
  SetTextColor(DC,RGB(255,0,0));
  OffsetRect(Rect,-1,0);
  TextOut(DC,CenterTx(DC,Tx,Rect),StrtY + 1,Tx,StrLen(Tx));

  { Either year or doy }
  If ShowDOW
    Then Begin
           { Display day of week }
           StrCopy(Tx,Days[DOW]);
           SetTextColor(DC,RGB(0,0,128));
           OffsetRect(Rect,-1,0);
           TextOut(DC,CenterTx(DC,Tx,Rect),StrtY + 22,Tx,StrLen(Tx));
         End
    Else Begin
           { Display year }
           StrCopy(Tx,StrYr);
           SetTextColor(DC,RGB(128,0,128));
           TextOut(DC,CenterTx(DC,Tx,Rect),StrtY + 22,Tx,StrLen(Tx));
         End;

  { Clean up }
  SelectObject(DC,OldFont);
  DeleteObject(SmlFont);
  DeleteObject(NumFont);
End {AddInPaint};

{-----------------------------------------------}

{ --- Tell Clysbar what kind of timer we need --- }

Function AddInTimerNeeded : Integer; Export;

Begin
  AddInTimerNeeded := ait_Slow;
End {AddInTimerNeeded};

{-----------------------------------------------}

{ --- Proc called when timer expires, perform timed duties --- }

Procedure AddInTimerTick(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 AddInPaint(Wnd,DC,False);

End {AddInTimerTick};

{-----------------------------------------------}

{ --- Proc called when button pressed --- }

Procedure AddInPressed(Wnd : HWnd; DC : HDC); Export;

Begin
  { If shift or ctrl held, open sked+ & exit }
  If (GetKeyState(vk_Shift) < 0) or (GetKeyState(vk_Control) < 0)
    Then Begin
           WinExec(CalendProgram,sw_ShowNormal);
           Exit;
         End;

  { Toggle the "show day-of-week" indicator when button pressed }
  ShowDOW := Not ShowDOW;
  AddInPaint(Wnd,DC,False);
End {AddInPressed};

{-----------------------------------------------}

{ --- Exit processing for Add-In --- }

Procedure AddInExit; Export;

Begin
End {AddInExit};

{-----------------------------------------------}

{ --- Clysbar queries Add-In about itself --- }

Procedure AddInAbout(Str1,Str2 : PChar;
                     Var TheIcon : HIcon;
                     Var TitleCol,TxCol,BkCol : TColorRef); Export;

Begin
  StrCopy(Str1,'Calend V');
  StrCat(Str1,MasterVer);
  StrCopy(Str2,'A Page-per-Day Calendar'#13'Copyright © 1992-1996'#13'by clySmic Software');

  TheIcon := LoadIcon(hInstance,'about');

  TitleCol := RGB(0,0,255);
  TxCol := RGB(0,0,128);
  BkCol := RGB(192,192,192);
End {AddInAbout};

{-----------------------------------------------}

{ --- Clysbar queries Add-In whether it'll accept d'n'd --- }

Function AddInAcceptDrops : Boolean; Export;

Begin
  AddInAcceptDrops := False;
End {AddInAcceptDrops};

{-----------------------------------------------}

{ --- Clysbar informs Add-In of a d'n'd drop --- }

Procedure AddInDrop(hDrop : THandle); Export;

Begin
End {AddInDrop};

{-----------------------------------------------}

{ --- Clysbar queries Add-In for Info Window text --- }

{ Return a zero-length string if you don't want to chg the text }

Procedure AddInGetInfoWinTx(Tx : PChar); Export;

Var
  OutsideTx : Boolean;

Begin
  { Is text for outside or inside of button? Outside has more 'room,' so
    note we can write longer descriptions }
  If StrComp(Tx,'OUT') = 0
    Then OutsideTx := True
    Else OutsideTx := False;

  If GTxInBtn and (Not OutsideTx)
    Then StrCopy(Tx,'')
    Else Begin
           FormIWDate;
           StrCopy(Tx,IWDate);
         End;
End {AddInGetInfoWinTx};

{-----------------------------------------------}

Exports AddInInit         Index 1,
        AddInPaint        Index 2,
        AddInTimerNeeded  Index 3,
        AddInTimerTick    Index 4,
        AddInPressed      Index 5,
        AddInExit         Index 6,
        AddInAbout        Index 7,
        AddInAcceptDrops  Index 8,
        AddInDrop         Index 9,
        AddInGetInfoWinTx Index 10;

Begin
End.
