home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tv20cpp.zip / demos / calendar.cpp < prev    next >
C/C++ Source or Header  |  1998-01-19  |  7KB  |  288 lines

  1. /*---------------------------------------------------------*/
  2. /*                                                         */
  3. /*   Calendar.cpp:  TCalenderWindow member functions.      */
  4. /*                                                         */
  5. /*---------------------------------------------------------*/
  6. /*
  7.  *      Turbo Vision - Version 2.0
  8.  *
  9.  *      Copyright (c) 1994 by Borland International
  10.  *      All Rights Reserved.
  11.  *
  12.  */
  13. /*
  14.  * Modified by Sergio Sigala <ssigala@globalnet.it>
  15.  */
  16.  
  17. #define Uses_TRect
  18. #define Uses_TEvent
  19. #define Uses_TKeys
  20. #define Uses_TDrawBuffer
  21. #define Uses_TStreamableClass
  22. #define Uses_TStreamable
  23. #define Uses_TView
  24. #define Uses_TWindow
  25. #include <tvision/tv.h>
  26. __link( RView )
  27. __link( RWindow )
  28.  
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include <ctype.h>
  32. #include <strstream.h>
  33. #include <iomanip.h>
  34. #include <time.h>
  35.  
  36. #include "calendar.h"
  37.  
  38.  
  39. static char *monthNames[] = {
  40.     "",
  41.     "January",  "February", "March",    "April",    "May",      "June",
  42.     "July",     "August",   "September","October",  "November", "December"
  43. };
  44.  
  45.  
  46. static unsigned char daysInMonth[] = {
  47.     0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  48. };
  49.  
  50.  
  51. //
  52. // TCalendarView functions
  53. //
  54.  
  55. const char * const TCalendarView::name = "TCalendarView";
  56.  
  57.  
  58. void TCalendarView::write( opstream& os )
  59. {
  60.     TView::write( os );
  61.     os << days << month << year << curDay << curMonth << curYear;
  62. }
  63.  
  64.  
  65. void *TCalendarView::read( ipstream& is )
  66. {
  67.     TView::read( is );
  68.     is >> days >> month >> year >> curDay >> curMonth >> curYear;
  69.     return this;
  70. }
  71.  
  72.  
  73. TStreamable *TCalendarView::build()
  74. {
  75.     return new TCalendarView( streamableInit );
  76. }
  77.  
  78.  
  79. TStreamableClass RCalendarView( TCalendarView::name,
  80.                                 TCalendarView::build,
  81.                                 __DELTA(TCalendarView)
  82.                               );
  83.  
  84.  
  85. TCalendarView::TCalendarView(TRect& r) : TView( r )
  86. {
  87.     /* SS: little change */
  88.  
  89. //    struct date d;
  90.  
  91.     options |= ofSelectable;
  92.     eventMask |= evMouseAuto;
  93.  
  94.     time_t now = time(NULL);
  95.     tm *broken = localtime(&now);
  96.  
  97.     year = curYear = broken->tm_year + 1900;
  98.     month = curMonth = broken->tm_mon + 1;
  99.     curDay = broken->tm_mday;
  100.  
  101. //    getdate( &d );
  102. //    year = curYear = d.da_year;
  103. //    month = curMonth = d.da_mon;
  104. //    curDay = d.da_day;
  105.  
  106.     drawView();
  107. }
  108.  
  109.  
  110. int dayOfWeek(int day, int month, int year)
  111. {
  112.     int century, yr, dw;
  113.  
  114.     if(month < 3)
  115.         {
  116.         month += 10;
  117.         --year;
  118.         }
  119.     else
  120.         month -= 2;
  121.  
  122.     century = year / 100;
  123.     yr = year % 100;
  124.     dw = (((26 * (int)month - 2) / 10) + (int)day + yr + (yr / 4) + (century / 4) -
  125.                 (2 * century)) % 7;
  126.  
  127.     if(dw < 0)
  128.         dw += 7;
  129.  
  130.     return((int)dw);
  131. }
  132.  
  133.  
  134. void TCalendarView::draw()
  135. {
  136.     char str[23];
  137.     char current = (char)(1 - dayOfWeek(1, month, year));
  138.     char days = (char)( daysInMonth[month] +
  139.                         ((year % 4 == 0 && month == 2) ? 1 : 0) );
  140.     char color, boldColor;
  141.     short  i, j;
  142.     TDrawBuffer buf;
  143.  
  144.     color = getColor(6);
  145.     boldColor = getColor(7);
  146.  
  147.     buf.moveChar(0, ' ', color, 22);
  148.  
  149.     ostrstream( str, sizeof str)
  150.       << setw(9) << monthNames[month] << " " << setw(4) << year
  151.       << " " << (char) 30 << "  " << (char) 31 << " " << ends;
  152.  
  153.     buf.moveStr(0, str, color);
  154.     writeLine(0, 0, 22, 1, buf);
  155.  
  156.     buf.moveChar(0, ' ', color, 22);
  157.     buf.moveStr(0, "Su Mo Tu We Th Fr Sa", color);
  158.     writeLine(0, 1, 22, 1, buf);
  159.  
  160.     for(i = 1; i <= 6; i++)
  161.         {
  162.         buf.moveChar(0, ' ', color, 22);
  163.         for(j = 0; j <= 6; j++)
  164.             {
  165.             if(current < 1 || current > days)
  166.                 buf.moveStr((short)(j*3), "   ", color);
  167.             else
  168.                 {
  169.                 ostrstream( str, sizeof str )
  170.                   << setw(2) << (int) current << ends;
  171.                 if(year == curYear && month == curMonth && current == curDay)
  172.                     buf.moveStr((short)(j*3), str, boldColor);
  173.                 else
  174.                     buf.moveStr((short)(j*3), str, color);
  175.                 }
  176.             current++;
  177.             }
  178.         writeLine(0, (short)(i+1), 22, 1, buf);
  179.         }
  180. }
  181.  
  182.  
  183. void TCalendarView::handleEvent(TEvent& event)
  184. {
  185.     TPoint point;
  186.  
  187.     TView::handleEvent(event);
  188.     if (state && sfSelected)
  189.         {
  190.         if ( (event.what & evMouse) && (evMouseDown || evMouseAuto) )
  191.             {
  192.             point = makeLocal(event.mouse.where);
  193.             if (point.x == 15 && point.y == 0)
  194.                 {
  195.                 ++month;
  196.                 if (month > 12)
  197.                     {
  198.                     ++year;
  199.                     month = 1;
  200.                     }
  201.                 drawView();
  202.                 }
  203.             else if (point.x == 18 && point.y == 0)
  204.                 {
  205.                 --month;
  206.                 if (month < 1)
  207.                     {
  208.                     --year;
  209.                     month = 12;
  210.                     }
  211.                 drawView();
  212.                 }
  213.             }
  214.         else if (event.what == evKeyboard)
  215.             {
  216.             if ( (loByte(event.keyDown.keyCode) == '+') ||
  217.               event.keyDown.keyCode == kbDown)
  218.                 {
  219.                 ++month;
  220.                 if (month > 12)
  221.                     {
  222.                     ++year;
  223.                     month = 1;
  224.                     }
  225.                 }
  226.             else if ( (loByte(event.keyDown.keyCode) == '-') ||
  227.               event.keyDown.keyCode == kbUp)
  228.                 {
  229.                 --month;
  230.                 if (month < 1)
  231.                     {
  232.                     --year;
  233.                     month = 12;
  234.                     }
  235.                 }
  236.             drawView();
  237.             }
  238.         }
  239. }
  240.  
  241.  
  242. //
  243. // TCalendarWindow functions
  244. //
  245.  
  246. const char * const TCalendarWindow::name = "TCalendarWindow";
  247.  
  248.  
  249. void TCalendarWindow::write( opstream& os )
  250. {
  251.     TWindow::write( os );
  252. }
  253.  
  254.  
  255. void *TCalendarWindow::read( ipstream& is )
  256. {
  257.     TWindow::read( is );
  258.     return this;
  259. }
  260.  
  261.  
  262. TStreamable *TCalendarWindow::build()
  263. {
  264.     return new TCalendarWindow( streamableInit );
  265. }
  266.  
  267.  
  268. TStreamableClass RCalendarWindow( TCalendarWindow::name,
  269.                                   TCalendarWindow::build,
  270.                                   __DELTA(TCalendarWindow)
  271.                                 );
  272.  
  273.  
  274. TCalendarWindow::TCalendarWindow() :
  275.     TWindow( TRect(1, 1, 23, 11), "Calendar", wnNoNumber ),
  276.     TWindowInit( &TCalendarWindow::initFrame )
  277. {
  278.     TRect r(getExtent());
  279.  
  280.     flags &= ~(wfZoom | wfGrow);
  281.     growMode = 0;
  282.  
  283.     palette = wpCyanWindow;
  284.  
  285.     r.grow(-1, -1);
  286.     insert( new TCalendarView( r ));
  287. }
  288.