// === calend.c ===

// Calendar Add-In for the clySmic Icon Bar
//   (C) 1992-1995 by clySmic Software. All Rights Reserved.

// Source for Borland C++ 4.5

#include <windows.h>
#include <stdio.h>
#include <dos.h>
#include <string.h>
#include <stdlib.h>

#include "add-in.h"
#include "homedir.c"

HANDLE  hInstance;                   // DLL's instance handle
BOOL ShowDOW = TRUE;
struct dosdate_t Today;
char IWDate[80],INIFile[80];
BOOL USDateFormat,GTxInBtn;

#define CBVer "2.2"

//-------------------------------------------------------------------

// LibMain

#pragma argsused

int WINAPI LibMain(HANDLE hLibInst, WORD wDataSeg,
                       WORD cbHeapSize, LPSTR lpszCmdLine)
{
  hInstance = hLibInst;
  return 1;

} /*LibMain*/

//-------------------------------------------------------------------

// Windows Exit Procedure

#pragma argsused

int WINAPI WEP(int bSystemExit)
{
  return 1;

} /*WEP*/

//-------------------------------------------------------------------

// Utility Function to center text

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*/

//-------------------------------------------------------------------

VOID FormIWDate(void)

{
  char *MonthName[12] =
    {"January","February","March","April","May","June",
     "July","August","September","October","November","December"};

  char *Days[7] =
     {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};

  _dos_getdate(&Today);

  if (USDateFormat)
    {
      // US date fmt
      sprintf(IWDate,"C: %s, %s %d, %d",Days[Today.dayofweek],MonthName[Today.month],
                                        Today.day,Today.year);
    }
  else
    {
      // European date fmt 
      sprintf(IWDate,"C: %s, %d %s %d",Days[Today.dayofweek],Today.day,
                                       MonthName[Today.month],Today.year);
    }
} /*FormIWDate*/

//-------------------------------------------------------------------

// Perform Add-In's initialization

InitResult WINAPI AddInInit(char far *CurVer, BOOLEAN TxInBtn)

{
  // Version check
  if (strncmp(CurVer,CBVer,3) != 0)
    return InitNotOk;

  // Point at our INI file, which is in our home dir
  HomeDir(hInstance,INIFile);
  strcat(INIFile,"C-CALEND.INI");

  // Flush INI file so we can edit it
  WritePrivateProfileString(NULL,NULL,NULL,INIFile);

  // Get display mode
  USDateFormat = GetPrivateProfileInt("Settings",
                                      "USDateFormat",
                                      0,
                                      INIFile);

  GTxInBtn = TxInBtn;

  return InitOk;
} /*AddInInit*/

//-------------------------------------------------------------------

// Paint on the button (Clysbar does the background)

VOID WINAPI AddInPaint(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,ShadRect;
  HFONT NumFont,OldFont,SmlFont;
  char Tx[128];
  char StrYr[5],StrDay[5];       // or 4?
  int StrtX,StrtY;
  HICON TheIcon;

  // If add-in is never uncovered, Wnd will be NULL
  if (Wnd == NULL)
    return;

  // Get full button rect
  GetClientRect(Wnd,&Rect);

  // Place "C" on right side
  StrtX = ((Rect.right - Rect.left) - GetSystemMetrics(SM_CXICON)) / 4 * 3;
  StrtY = ((Rect.bottom - Rect.top) - GetSystemMetrics(SM_CYICON)) / 2;
  if (Pressed)
    {
      StrtX++;
      StrtY++;
    }

  TheIcon = LoadIcon(hInstance,"c");
  DrawIcon(DC,StrtX,StrtY,TheIcon);

  // If the text is in the button, choke down rect to left side only
  if (GTxInBtn)
    Rect.right = Rect.left + GetSystemMetrics(SM_CXICON) + 12;

  // Calc location of icon
  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,"turning");
      DrawIcon(DC,StrtX+1,StrtY+1,TheIcon);

      return;
    }

  // Draw "page" icon
  TheIcon = LoadIcon(hInstance,"calend");
  DrawIcon(DC,StrtX,StrtY,TheIcon);

  // Get date info
  _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;

  // Create a small font for the month/day name/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 | 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 | 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,1,1);
  DrawText(DC,StrDay,strlen(StrDay),&ShadRect,
           DT_CENTER | DT_VCENTER | DT_SINGLELINE);

  // Draw lg day number
  SetTextColor(DC,RGB(0,0,0));
  DrawText(DC,StrDay,strlen(StrDay),&Rect,
           DT_CENTER | DT_VCENTER | DT_SINGLELINE);

  // Setup for other info
  SelectObject(DC,SmlFont);

  // Draw month name
  SetTextColor(DC,RGB(255,0,0));
  strcpy(Tx,MonthName[Today.month-1]);
  TextOut(DC,CenterTx(DC,Tx,Rect),StrtY + 1,Tx,strlen(Tx));

  // either year or DOY
  if (ShowDOW)
    {
      // Display day of week
      strcpy(Tx,Days[Today.dayofweek]);
      SetTextColor(DC,RGB(0,0,128));
      TextOut(DC,CenterTx(DC,Tx,Rect),StrtY + 22,Tx,strlen(Tx));
    }
  else
    {
      // Display year
      strcpy(Tx,StrYr);
      SetTextColor(DC,RGB(128,0,128));
      TextOut(DC,CenterTx(DC,Tx,Rect),StrtY + 22,Tx,strlen(Tx));
    }

  // Clean up
  SelectObject(DC,OldFont);
  DeleteObject(SmlFont);
  DeleteObject(NumFont);

} /*AddInPaint*/

//-------------------------------------------------------------------

// Tell Clysbar what kind of timer we need

int WINAPI AddInTimerNeeded()

{
  return ait_Slow;
} /*AddInTimerNeeded*/

//-------------------------------------------------------------------

// Proc called when timer expires, perform timed duties

VOID WINAPI AddInTimerTick(HWND Wnd, HDC DC)

{
  struct dosdate_t TstDate;

  // If add-in is never uncovered, Wnd will be NULL
  if (Wnd == NULL)
    return;

  // Check for a date change
  _dos_getdate(&TstDate);

  // If different date, repaint window
  if (TstDate.day != Today.day)
    AddInPaint(Wnd,DC,FALSE);

} /*AddInTimerTick*/

//-------------------------------------------------------------------

// Proc called when button pressed and released (used for toggling states)

VOID WINAPI AddInPressed(HWND Wnd, HDC DC)

{
  // Toggle the "show day-of-week" indicator when button pressed
  ShowDOW = !ShowDOW;

  AddInPaint(Wnd,DC,FALSE);
} /*AddInPressed*/

//-------------------------------------------------------------------

// Exit processing for Add-In

VOID WINAPI AddInExit()

{
} /*AddInExit*/

//-------------------------------------------------------------------

// Clysbar queries Add-In about itself for About box

VOID WINAPI AddInAbout(char far *Str1, char far *Str2,
                           HICON far *TheIcon,
                           COLORREF far *TitleCol,
                           COLORREF far *TxCol,
                           COLORREF far *BkCol)

{
  strcpy(Str1,"C-Calend V2.20");
  strcpy(Str2,"'C' Code Demo Example\nŠ 1992 - 1995 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*/

//-------------------------------------------------------------------


// Clysbar queries Add-In whether it'll accept d'n'd

BOOL WINAPI AddInAcceptDrops()

{
  return FALSE;
} /*AddInAcceptDrops*/

//-------------------------------------------------------------------

// Clysbar informs Add-In of a d'n'd drop

#pragma argsused

VOID WINAPI AddInDrop(HANDLE hDrop)

{
} /*AddInDrop*/

//-------------------------------------------------------------------

// Clysbar queries Add-In for Info Window text

// Return a zero-length string (not NULL!) if you don't want to chg the text

VOID WINAPI AddInGetInfoWinTx(char *Tx)

{
  if (GTxInBtn)
    strcpy(Tx,"Calend");
  else
    {
      FormIWDate();
      strcpy(Tx,IWDate);
    }
} /*AddInGetInfoWinTx*/

