// === calend.c ===

// Calendar Add-In for the clySmic Icon Bar. (C) 1992 by clySmic Software.
// All Rights Reserved.

#include "c:\winsdk\include\windows.h"
#include <stdio.h>
#include <dos.h>

#include "add-in.h"

HANDLE  hInstance;                   // DLL's instance handle
BOOL ShowDOW = TRUE;
struct dosdate_t Today;

#define CBVer "1.70"

/*-------------------------------------------------------------------*/

// LibMain

int FAR PASCAL LibMain(HANDLE hLibInst, WORD wDataSeg,
                       WORD cbHeapSize, LPSTR lpszCmdLine)
{
  hInstance = hLibInst;
  return 1;

} /*LibMain*/

/*-------------------------------------------------------------------*/

// Windows Exit Procedure

int FAR PASCAL WEP(int bSystemExit)
{
  return 1;

} /*WEP*/

/*-------------------------------------------------------------------*/

// Utility Function

int CenterTx(HDC DC, char *Tx, RECT Rect)

{
  int Width,WinX,StrtX;

  // 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) / 2) + Rect.left;

  return StrtX;

} /*CenterTx*/

/*-------------------------------------------------------------------*/

// Perform Add-In's initialization

InitResult FAR PASCAL InitAddIn(char far *CurVer)

{
  // Version check
  if (strcmp(CurVer,CBVer) != 0)
    return InitNotOk;
  else
    return InitOk;

} /*InitAddIn*/

/*-------------------------------------------------------------------*/

// Paint on the button (Clysbar does the background)

VOID FAR PASCAL PaintAddIn(HWND Wnd, HDC DC, BOOLEAN Pressed)

{
  char *MonthName[12] =
    {"JAN","FEB","MAR","APR","MAY","JUN",
     "JUL","AUG","SEP","OCT","NOV","DEC"};

  char *Days[7] =
     {"SUN","MON","TUE","WED","THU","FRI","SAT"};

  RECT Rect;
  HFONT NumFont,OldFont,SmlFont;
  char Tx[128];
  char StrYr[5],StrDay[5];       // or 4?
  int StrtX,StrtY;
  HICON TheIcon;

  GetClientRect(Wnd,&Rect);

  StrtX = ((Rect.right - Rect.left) - GetSystemMetrics(SM_CXICON)) / 2;
  StrtY = ((Rect.bottom - Rect.top) - GetSystemMetrics(SM_CYICON)) / 2;

  // Draw turning page if pressed
  if (Pressed)
    {
      TheIcon = LoadIcon(hInstance,"CALEND2");
      DrawIcon(DC,StrtX+1,StrtY+1,TheIcon);

      return;
    }

  TheIcon = LoadIcon(hInstance,"CALEND");
  DrawIcon(DC,StrtX,StrtY,TheIcon);

  _dos_getdate(&Today);

  itoa(Today.day,StrDay,10);
  itoa(Today.year,StrYr,10);

  // Lop off 1st two year digits
  StrYr[0] = StrYr[2];
  StrYr[1] = StrYr[3];
  StrYr[2] = NULL;

  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 | 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 | VARIABLE_PITCH,
             "Times New Roman");

  OldFont = SelectObject(DC,NumFont);

  SetBkMode(DC,TRANSPARENT);
  SetTextColor(DC,RGB(0,0,0));

  Rect.top++;

  DrawText(DC,StrDay,strlen(StrDay),&Rect,
           DT_CENTER | DT_VCENTER | DT_SINGLELINE);

  SelectObject(DC,SmlFont);

  // Adjust rect to account for off-center pad image
  Rect.right -= 2;

  SetTextColor(DC,RGB(255,0,0));
  strcpy(Tx,MonthName[Today.month-1]);
  TextOut(DC,CenterTx(DC,Tx,Rect),StrtY + 3,Tx,strlen(Tx));

  if (ShowDOW)
    {
      strcpy(Tx,Days[Today.dayofweek]);
      SetTextColor(DC,RGB(0,0,128));
      TextOut(DC,CenterTx(DC,Tx,Rect),StrtY + 22,Tx,strlen(Tx));
    }
  else
    {
      strcpy(Tx,StrYr);
      SetTextColor(DC,RGB(128,0,128));
      TextOut(DC,CenterTx(DC,Tx,Rect),StrtY + 22,Tx,strlen(Tx));
    }

  SelectObject(DC,OldFont);
  DeleteObject(SmlFont);
  DeleteObject(NumFont);

} /*PaintAddIn*/

/*-------------------------------------------------------------------*/

// Tell Clysbar what kind of timer we need

int FAR PASCAL TimerNeeded()

{
  return ait_Slow;
}

/*-------------------------------------------------------------------*/

// Proc called when timer expires, perform timed duties

VOID FAR PASCAL AddInTimer(HWND Wnd, HDC DC)

{
  struct dosdate_t TstDate;

  // Check for a date change
  _dos_getdate(&TstDate);

  // If different date, repaint window
  if (TstDate.day != Today.day)
    PaintAddIn(Wnd,DC,FALSE);

} /*AddInTimer*/

/*-------------------------------------------------------------------*/

// Proc called when button pressed and released (used for toggling states)

VOID FAR PASCAL AddInPressed(HWND Wnd, HDC DC)

{
  // Toggle the "show day-of-week" indicator when button pressed
  ShowDOW = !ShowDOW;

  PaintAddIn(Wnd,DC,FALSE);
} /*AddInPressed*/

/*-------------------------------------------------------------------*/

// Exit processing for Add-In

VOID FAR PASCAL ExitAddIn()

{
} /*ExitAddIn*/

/*-------------------------------------------------------------------*/

// Clysbar queries Add-In about itself for About box

VOID FAR PASCAL AddInAbout(char far *Str1, char far *Str2,
                           HICON far *TheIcon,
                           COLORREF far *TitleCol,
                           COLORREF far *TxCol,
                           COLORREF far *BkCol)

{
  strcpy(Str1,"C-Calend V1.00");
  strcpy(Str2,"'C' Code Example Version\nŠ 1992 by clySmic Software.\nAll Rights Reserved.");

  *TheIcon = LoadIcon(hInstance,"ABOUT");
  *TitleCol = RGB(255,255,255);
  *TxCol = RGB(192,192,192);
  *BkCol = RGB(255,0,0);
} /*AddInAbout*/
