home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 Secrets (4th Edition) / Windows95Secrets4thEdition.iso / utility / desktop / clysbar / c-calend.c_ / c-calend.c
Encoding:
C/C++ Source or Header  |  1995-03-01  |  9.6 KB  |  389 lines

  1. // === calend.c ===
  2.  
  3. // Calendar Add-In for the clySmic Icon Bar
  4. //   (C) 1992-1995 by clySmic Software. All Rights Reserved.
  5.  
  6. // Source for Borland C++ 4.5
  7.  
  8. #include <windows.h>
  9. #include <stdio.h>
  10. #include <dos.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13.  
  14. #include "add-in.h"
  15. #include "homedir.c"
  16.  
  17. HANDLE  hInstance;                   // DLL's instance handle
  18. BOOL ShowDOW = TRUE;
  19. struct dosdate_t Today;
  20. char IWDate[80],INIFile[80];
  21. BOOL USDateFormat,GTxInBtn;
  22.  
  23. #define CBVer "2.2"
  24.  
  25. //-------------------------------------------------------------------
  26.  
  27. // LibMain
  28.  
  29. #pragma argsused
  30.  
  31. int WINAPI LibMain(HANDLE hLibInst, WORD wDataSeg,
  32.                        WORD cbHeapSize, LPSTR lpszCmdLine)
  33. {
  34.   hInstance = hLibInst;
  35.   return 1;
  36.  
  37. } /*LibMain*/
  38.  
  39. //-------------------------------------------------------------------
  40.  
  41. // Windows Exit Procedure
  42.  
  43. #pragma argsused
  44.  
  45. int WINAPI WEP(int bSystemExit)
  46. {
  47.   return 1;
  48.  
  49. } /*WEP*/
  50.  
  51. //-------------------------------------------------------------------
  52.  
  53. // Utility Function to center text
  54.  
  55. int CenterTx(HDC DC, char *Tx, RECT Rect)
  56.  
  57. {
  58.   int Width,WinX,StrtX;
  59.  
  60.   // Ask Windows for the total pixel length of the string & calc starting X
  61.   Width = LOWORD(GetTextExtent(DC,Tx,strlen(Tx)));
  62.  
  63.   // Get total x width of window - don"t add 1!
  64.   WinX = (Rect.right - Rect.left);
  65.  
  66.   // Calculate centered starting posn
  67.   StrtX = ((WinX - Width) / 2) + Rect.left;
  68.  
  69.   return StrtX;
  70.  
  71. } /*CenterTx*/
  72.  
  73. //-------------------------------------------------------------------
  74.  
  75. VOID FormIWDate(void)
  76.  
  77. {
  78.   char *MonthName[12] =
  79.     {"January","February","March","April","May","June",
  80.      "July","August","September","October","November","December"};
  81.  
  82.   char *Days[7] =
  83.      {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
  84.  
  85.   _dos_getdate(&Today);
  86.  
  87.   if (USDateFormat)
  88.     {
  89.       // US date fmt
  90.       sprintf(IWDate,"C: %s, %s %d, %d",Days[Today.dayofweek],MonthName[Today.month],
  91.                                         Today.day,Today.year);
  92.     }
  93.   else
  94.     {
  95.       // European date fmt 
  96.       sprintf(IWDate,"C: %s, %d %s %d",Days[Today.dayofweek],Today.day,
  97.                                        MonthName[Today.month],Today.year);
  98.     }
  99. } /*FormIWDate*/
  100.  
  101. //-------------------------------------------------------------------
  102.  
  103. // Perform Add-In's initialization
  104.  
  105. InitResult WINAPI AddInInit(char far *CurVer, BOOLEAN TxInBtn)
  106.  
  107. {
  108.   // Version check
  109.   if (strncmp(CurVer,CBVer,3) != 0)
  110.     return InitNotOk;
  111.  
  112.   // Point at our INI file, which is in our home dir
  113.   HomeDir(hInstance,INIFile);
  114.   strcat(INIFile,"C-CALEND.INI");
  115.  
  116.   // Flush INI file so we can edit it
  117.   WritePrivateProfileString(NULL,NULL,NULL,INIFile);
  118.  
  119.   // Get display mode
  120.   USDateFormat = GetPrivateProfileInt("Settings",
  121.                                       "USDateFormat",
  122.                                       0,
  123.                                       INIFile);
  124.  
  125.   GTxInBtn = TxInBtn;
  126.  
  127.   return InitOk;
  128. } /*AddInInit*/
  129.  
  130. //-------------------------------------------------------------------
  131.  
  132. // Paint on the button (Clysbar does the background)
  133.  
  134. VOID WINAPI AddInPaint(HWND Wnd, HDC DC, BOOLEAN Pressed)
  135.  
  136. {
  137.   char *MonthName[12] =
  138.     {"JAN","FEB","MAR","APR","MAY","JUN",
  139.      "JUL","AUG","SEP","OCT","NOV","DEC"};
  140.  
  141.   char *Days[7] =
  142.      {"SUN","MON","TUE","WED","THU","FRI","SAT"};
  143.  
  144.   RECT Rect,ShadRect;
  145.   HFONT NumFont,OldFont,SmlFont;
  146.   char Tx[128];
  147.   char StrYr[5],StrDay[5];       // or 4?
  148.   int StrtX,StrtY;
  149.   HICON TheIcon;
  150.  
  151.   // If add-in is never uncovered, Wnd will be NULL
  152.   if (Wnd == NULL)
  153.     return;
  154.  
  155.   // Get full button rect
  156.   GetClientRect(Wnd,&Rect);
  157.  
  158.   // Place "C" on right side
  159.   StrtX = ((Rect.right - Rect.left) - GetSystemMetrics(SM_CXICON)) / 4 * 3;
  160.   StrtY = ((Rect.bottom - Rect.top) - GetSystemMetrics(SM_CYICON)) / 2;
  161.   if (Pressed)
  162.     {
  163.       StrtX++;
  164.       StrtY++;
  165.     }
  166.  
  167.   TheIcon = LoadIcon(hInstance,"c");
  168.   DrawIcon(DC,StrtX,StrtY,TheIcon);
  169.  
  170.   // If the text is in the button, choke down rect to left side only
  171.   if (GTxInBtn)
  172.     Rect.right = Rect.left + GetSystemMetrics(SM_CXICON) + 12;
  173.  
  174.   // Calc location of icon
  175.   StrtX = ((Rect.right - Rect.left) - GetSystemMetrics(SM_CXICON)) / 2;
  176.   StrtY = ((Rect.bottom - Rect.top) - GetSystemMetrics(SM_CYICON)) / 2;
  177.  
  178.   // Draw turning page if pressed
  179.   if (Pressed)
  180.     {
  181.       TheIcon = LoadIcon(hInstance,"turning");
  182.       DrawIcon(DC,StrtX+1,StrtY+1,TheIcon);
  183.  
  184.       return;
  185.     }
  186.  
  187.   // Draw "page" icon
  188.   TheIcon = LoadIcon(hInstance,"calend");
  189.   DrawIcon(DC,StrtX,StrtY,TheIcon);
  190.  
  191.   // Get date info
  192.   _dos_getdate(&Today);
  193.  
  194.   itoa(Today.day,StrDay,10);
  195.   itoa(Today.year,StrYr,10);
  196.  
  197.   // Lop off 1st two year digits
  198.   //StrYr[0] = StrYr[2];
  199.   //StrYr[1] = StrYr[3];
  200.   //StrYr[2] = NULL;
  201.  
  202.   // Create a small font for the month/day name/year
  203.   SmlFont =
  204.   CreateFont(9,             // Height
  205.              0,0,0,         // Width, left 2 right, normal orientation
  206.              400,           // Weight
  207.              0,0,0,         // Italic, underlined, or strikeout
  208.              0,             // ANSI char set
  209.              0,             // Reserved precision field
  210.              0,             // Default clipping
  211.              PROOF_QUALITY, // Quality
  212.              FF_ROMAN | VARIABLE_PITCH,
  213.              "Small Fonts");
  214.  
  215.   // Create large font for the day number
  216.   NumFont =
  217.   CreateFont(17,            // Height
  218.              0,0,0,         // Width, left 2 right, normal orientation
  219.              700,           // Weight
  220.              0,0,0,         // Italic, underlined, or strikeout
  221.              0,             // ANSI char set
  222.              0,             // Reserved precision field
  223.              0,             // Default clipping
  224.              PROOF_QUALITY, // Quality
  225.              FF_ROMAN | VARIABLE_PITCH,
  226.              "Times New Roman");
  227.  
  228.   // Setup for day number
  229.   OldFont = SelectObject(DC,NumFont);
  230.   SetBkMode(DC,TRANSPARENT);
  231.  
  232.   // Draw lg day number's shadow
  233.   SetTextColor(DC,RGB(128,128,128));
  234.   ShadRect = Rect;
  235.   OffsetRect(&ShadRect,1,1);
  236.   DrawText(DC,StrDay,strlen(StrDay),&ShadRect,
  237.            DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  238.  
  239.   // Draw lg day number
  240.   SetTextColor(DC,RGB(0,0,0));
  241.   DrawText(DC,StrDay,strlen(StrDay),&Rect,
  242.            DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  243.  
  244.   // Setup for other info
  245.   SelectObject(DC,SmlFont);
  246.  
  247.   // Draw month name
  248.   SetTextColor(DC,RGB(255,0,0));
  249.   strcpy(Tx,MonthName[Today.month-1]);
  250.   TextOut(DC,CenterTx(DC,Tx,Rect),StrtY + 1,Tx,strlen(Tx));
  251.  
  252.   // either year or DOY
  253.   if (ShowDOW)
  254.     {
  255.       // Display day of week
  256.       strcpy(Tx,Days[Today.dayofweek]);
  257.       SetTextColor(DC,RGB(0,0,128));
  258.       TextOut(DC,CenterTx(DC,Tx,Rect),StrtY + 22,Tx,strlen(Tx));
  259.     }
  260.   else
  261.     {
  262.       // Display year
  263.       strcpy(Tx,StrYr);
  264.       SetTextColor(DC,RGB(128,0,128));
  265.       TextOut(DC,CenterTx(DC,Tx,Rect),StrtY + 22,Tx,strlen(Tx));
  266.     }
  267.  
  268.   // Clean up
  269.   SelectObject(DC,OldFont);
  270.   DeleteObject(SmlFont);
  271.   DeleteObject(NumFont);
  272.  
  273. } /*AddInPaint*/
  274.  
  275. //-------------------------------------------------------------------
  276.  
  277. // Tell Clysbar what kind of timer we need
  278.  
  279. int WINAPI AddInTimerNeeded()
  280.  
  281. {
  282.   return ait_Slow;
  283. } /*AddInTimerNeeded*/
  284.  
  285. //-------------------------------------------------------------------
  286.  
  287. // Proc called when timer expires, perform timed duties
  288.  
  289. VOID WINAPI AddInTimerTick(HWND Wnd, HDC DC)
  290.  
  291. {
  292.   struct dosdate_t TstDate;
  293.  
  294.   // If add-in is never uncovered, Wnd will be NULL
  295.   if (Wnd == NULL)
  296.     return;
  297.  
  298.   // Check for a date change
  299.   _dos_getdate(&TstDate);
  300.  
  301.   // If different date, repaint window
  302.   if (TstDate.day != Today.day)
  303.     AddInPaint(Wnd,DC,FALSE);
  304.  
  305. } /*AddInTimerTick*/
  306.  
  307. //-------------------------------------------------------------------
  308.  
  309. // Proc called when button pressed and released (used for toggling states)
  310.  
  311. VOID WINAPI AddInPressed(HWND Wnd, HDC DC)
  312.  
  313. {
  314.   // Toggle the "show day-of-week" indicator when button pressed
  315.   ShowDOW = !ShowDOW;
  316.  
  317.   AddInPaint(Wnd,DC,FALSE);
  318. } /*AddInPressed*/
  319.  
  320. //-------------------------------------------------------------------
  321.  
  322. // Exit processing for Add-In
  323.  
  324. VOID WINAPI AddInExit()
  325.  
  326. {
  327. } /*AddInExit*/
  328.  
  329. //-------------------------------------------------------------------
  330.  
  331. // Clysbar queries Add-In about itself for About box
  332.  
  333. VOID WINAPI AddInAbout(char far *Str1, char far *Str2,
  334.                            HICON far *TheIcon,
  335.                            COLORREF far *TitleCol,
  336.                            COLORREF far *TxCol,
  337.                            COLORREF far *BkCol)
  338.  
  339. {
  340.   strcpy(Str1,"C-Calend V2.20");
  341.   strcpy(Str2,"'C' Code Demo Example\n⌐ 1992 - 1995 by clySmic Software.\nAll Rights Reserved.");
  342.  
  343.   *TheIcon = LoadIcon(hInstance,"about");
  344.   *TitleCol = RGB(255,255,255);
  345.   *TxCol = RGB(192,192,192);
  346.   *BkCol = RGB(255,0,0);
  347. } /*AddInAbout*/
  348.  
  349. //-------------------------------------------------------------------
  350.  
  351.  
  352. // Clysbar queries Add-In whether it'll accept d'n'd
  353.  
  354. BOOL WINAPI AddInAcceptDrops()
  355.  
  356. {
  357.   return FALSE;
  358. } /*AddInAcceptDrops*/
  359.  
  360. //-------------------------------------------------------------------
  361.  
  362. // Clysbar informs Add-In of a d'n'd drop
  363.  
  364. #pragma argsused
  365.  
  366. VOID WINAPI AddInDrop(HANDLE hDrop)
  367.  
  368. {
  369. } /*AddInDrop*/
  370.  
  371. //-------------------------------------------------------------------
  372.  
  373. // Clysbar queries Add-In for Info Window text
  374.  
  375. // Return a zero-length string (not NULL!) if you don't want to chg the text
  376.  
  377. VOID WINAPI AddInGetInfoWinTx(char *Tx)
  378.  
  379. {
  380.   if (GTxInBtn)
  381.     strcpy(Tx,"Calend");
  382.   else
  383.     {
  384.       FormIWDate();
  385.       strcpy(Tx,IWDate);
  386.     }
  387. } /*AddInGetInfoWinTx*/
  388.  
  389.