home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 24 / CD_ASCQ_24_0995.iso / vrac / dflt20.zip / CALENDAR.C < prev    next >
Text File  |  1991-12-05  |  5KB  |  166 lines

  1. /* ------------- calendar.c ------------- */
  2. #include "dflat.h"
  3.  
  4. #ifndef TURBOC
  5.  
  6. #define CALHEIGHT 17
  7. #define CALWIDTH 33
  8.  
  9. static int DyMo[] = {31,28,31,30,31,30,31,31,30,31,30,31};
  10. static struct tm ttm;
  11. static int dys[42];
  12. static WINDOW Cwnd;
  13.  
  14. static void FixDate(void)
  15. {
  16.     /* ---- adjust Feb for leap year ---- */
  17.     DyMo[1] = (ttm.tm_year % 4) ? 28 : 29;
  18.     ttm.tm_mday = min(ttm.tm_mday, DyMo[ttm.tm_mon]);
  19. }
  20.  
  21. /* ---- build calendar dates array ---- */
  22. static void BuildDateArray(void)
  23. {
  24.     int offset, dy = 0;
  25.     memset(dys, 0, sizeof dys);
  26.     FixDate();
  27.     /* ----- compute the weekday for the 1st ----- */
  28.     offset = ((ttm.tm_mday-1) - ttm.tm_wday) % 7;
  29.     if (offset < 0)
  30.         offset += 7;
  31.     if (offset)
  32.         offset = (offset - 7) * -1;
  33.     /* ----- build the dates into the array ---- */
  34.     for (dy = 1; dy <= DyMo[ttm.tm_mon]; dy++)
  35.         dys[offset++] = dy;
  36. }
  37.  
  38. static void CreateWindowMsg(WINDOW wnd)
  39. {
  40.     int x, y;
  41.     DrawBox(wnd, 1, 2, CALHEIGHT-4, CALWIDTH-4);
  42.     for (x = 5; x < CALWIDTH-4; x += 4)
  43.         DrawVector(wnd, x, 2, CALHEIGHT-4, FALSE);
  44.     for (y = 4; y < CALHEIGHT-3; y+=2)
  45.         DrawVector(wnd, 1, y, CALWIDTH-4, TRUE);
  46. }
  47.  
  48. static void DisplayDates(WINDOW wnd)
  49. {
  50.     int week, day;
  51.     char dyln[10];
  52.     int offset;
  53.     char banner[CALWIDTH-1];
  54.     char banner1[30];
  55.  
  56.     SetStandardColor(wnd);
  57.     PutWindowLine(wnd, "Sun Mon Tue Wed Thu Fri Sat", 2, 1);
  58.     memset(banner, ' ', CALWIDTH-2);
  59.     strftime(banner1, 16, "%B, %Y", &ttm);
  60.     offset = (CALWIDTH-2 - strlen(banner1)) / 2;
  61.     strcpy(banner+offset, banner1);
  62.     strcat(banner, "    ");
  63.     PutWindowLine(wnd, banner, 0, 0);
  64.     BuildDateArray();
  65.     for (week = 0; week < 6; week++)    {
  66.         for (day = 0; day < 7; day++)    {
  67.             int dy = dys[week*7+day];
  68.             if (dy == 0)
  69.                 strcpy(dyln, "   ");
  70.             else    {
  71.                 if (dy == ttm.tm_mday)
  72.                     sprintf(dyln, "%c%c%c%2d %c",
  73.                         CHANGECOLOR,
  74.                         SelectForeground(wnd)+0x80,
  75.                         SelectBackground(wnd)+0x80,
  76.                         dy, RESETCOLOR);
  77.                 else
  78.                     sprintf(dyln, "%2d ", dy);
  79.             }
  80.             SetStandardColor(wnd);
  81.             PutWindowLine(wnd, dyln, 2 + day * 4, 3 + week*2);
  82.         }
  83.     }
  84. }
  85.  
  86. static int KeyboardMsg(WINDOW wnd, PARAM p1)
  87. {
  88.     switch ((int)p1)    {
  89.         case PGUP:
  90.             if (ttm.tm_mon == 0)    {
  91.                 ttm.tm_mon = 12;
  92.                 ttm.tm_year--;
  93.             }
  94.             ttm.tm_mon--;
  95.             FixDate();
  96.             mktime(&ttm);
  97.             DisplayDates(wnd);
  98.             return TRUE;
  99.         case PGDN:
  100.             ttm.tm_mon++;
  101.             if (ttm.tm_mon == 12)    {
  102.                 ttm.tm_mon = 0;
  103.                 ttm.tm_year++;
  104.             }
  105.             FixDate();
  106.             mktime(&ttm);
  107.             DisplayDates(wnd);
  108.             return TRUE;
  109.         default:
  110.             break;
  111.     }
  112.     return FALSE;
  113. }
  114.  
  115. static int CalendarProc(WINDOW wnd,MESSAGE msg,
  116.                                 PARAM p1,PARAM p2)
  117. {
  118.     switch (msg)    {
  119.         case CREATE_WINDOW:
  120.             DefaultWndProc(wnd, msg, p1, p2);
  121.             CreateWindowMsg(wnd);
  122.             return TRUE;
  123.         case KEYBOARD:
  124.             if (KeyboardMsg(wnd, p1))
  125.                 return TRUE;
  126.             break;
  127.         case PAINT:
  128.             DefaultWndProc(wnd, msg, p1, p2);
  129.             DisplayDates(wnd);
  130.             return TRUE;
  131.         case COMMAND:
  132.             if ((int)p1 == ID_HELP)    {
  133.                 DisplayHelp(wnd, "Calendar");
  134.                 return TRUE;
  135.             }
  136.             break;
  137.         case CLOSE_WINDOW:
  138.             Cwnd = NULL;
  139.             break;
  140.         default:
  141.             break;
  142.     }
  143.     return DefaultWndProc(wnd, msg, p1, p2);
  144. }
  145.  
  146. void Calendar(WINDOW pwnd)
  147. {
  148.     if (Cwnd == NULL)    {
  149.         time_t tim = time(NULL);
  150.         ttm = *localtime(&tim);
  151.         Cwnd = CreateWindow(PICTUREBOX,
  152.                     "Calendar",
  153.                     -1, -1, CALHEIGHT, CALWIDTH,
  154.                     NULL, pwnd, CalendarProc,
  155.                     SHADOW     |
  156.                     MINMAXBOX  |
  157.                     CONTROLBOX |
  158.                     MOVEABLE   |
  159.                     HASBORDER
  160.         );
  161.     }
  162.     SendMessage(Cwnd, SETFOCUS, TRUE, 0);
  163. }
  164.  
  165. #endif
  166.