home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yacl-012.zip / ui / monthcal.cxx < prev    next >
C/C++ Source or Header  |  1995-04-10  |  7KB  |  222 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6. /*
  7.  *
  8.  *          Copyright (C) 1994, M. A. Sridhar
  9.  *  
  10.  *
  11.  *     This software is Copyright M. A. Sridhar, 1994. You are free
  12.  *     to copy, modify or distribute this software  as you see fit,
  13.  *     and to use  it  for  any  purpose, provided   this copyright
  14.  *     notice and the following   disclaimer are included  with all
  15.  *     copies.
  16.  *
  17.  *                        DISCLAIMER
  18.  *
  19.  *     The author makes no warranties, either expressed or implied,
  20.  *     with respect  to  this  software, its  quality, performance,
  21.  *     merchantability, or fitness for any particular purpose. This
  22.  *     software is distributed  AS IS.  The  user of this  software
  23.  *     assumes all risks  as to its quality  and performance. In no
  24.  *     event shall the author be liable for any direct, indirect or
  25.  *     consequential damages, even if the  author has been  advised
  26.  *     as to the possibility of such damages.
  27.  *
  28.  */
  29.  
  30.  
  31.  
  32.  
  33. #if defined(__GNUC__)
  34. #pragma implementation
  35. #endif
  36.  
  37.  
  38.  
  39. #include "ui/monthcal.h"
  40. #include "ui/dsplsurf.h"
  41. #include "ui/composit.h"
  42. #include "ui/stddlg.h"
  43.  
  44. #if defined(__X_MOTIF__)
  45. #include <Xm/DrawingA.h>
  46. #endif
  47.  
  48.  
  49. // ----------------  Look-and-feel configuration parameters -----------------
  50.  
  51. // Labels for the week day columns:
  52. static char* days[7] = {"S", "M", "T", "W", "T", "F", "S"};
  53.  
  54. // Top left corner of the month label: (the label rectangle spans the width
  55. // of the calendar, and the name of month is centered in this rectangle)
  56. static UI_Point CalendarOrigin (10,5);
  57.  
  58. // Vertical gap between month label and row of day labels:
  59. static short GapBelowMonthLabel = 8;
  60.  
  61. // Vertical gap between row of day labels and 6x7 rectangle of dates:
  62. static short GapBelowDayNames = 3;
  63.  
  64. // Gaps between cells in the 6x7 rectangular array:
  65. static short HorzGap=5, VertGap = 3;
  66.  
  67. // ----------------- End configuration parameters --------------------------
  68.  
  69.  
  70. UI_MonthCalendar::UI_MonthCalendar
  71.     (UI_VObjCollection* parent, 
  72.      const UI_Rectangle& shape, UI_ViewID id, const CL_Date& date)
  73. : UI_SimpleVObject (parent,  shape, id)
  74. {
  75.     _model = new CL_Date;
  76.     if (*_model == date)
  77.         *_model = CL_Date::Today();
  78.     CL_Date* dt = (CL_Date*) _model;
  79. #if defined (__MS_WINDOWS__)
  80.     _style = WS_CHILD | WS_VISIBLE;
  81. #elif defined(__OS2__)
  82.     _style = WS_VISIBLE;
  83. #endif
  84.     _month = dt->Month();
  85.     _year = dt->Year();
  86.     _daysInMonth = dt->DaysInMonth();
  87.     TabStop (FALSE); // We're not a tab stop
  88. }
  89.  
  90.  
  91. void UI_MonthCalendar::AdvanceMonth ()
  92. {
  93.     CL_Date& our_model = *(CL_Date*) _model;
  94.     _SetModelValue (our_model.AddMonths (1));
  95.     _month = our_model.Month();
  96.     _year  = our_model.Year();
  97.     _daysInMonth = CL_Date::DaysInMonth (_month, _year);
  98.     Paint();
  99. }
  100.  
  101. void UI_MonthCalendar::PreviousMonth()
  102. {
  103.     CL_Date& our_model = *(CL_Date*) _model;
  104.     _SetModelValue (our_model.AddMonths (-1));
  105.     _month = our_model.Month();
  106.     _year  = our_model.Year();
  107.     _daysInMonth = CL_Date::DaysInMonth (_month, _year);
  108.     Paint();
  109. }
  110.  
  111.  
  112.  
  113. bool UI_MonthCalendar::Paint ()
  114. {
  115.     CL_Date dt (*(CL_Date*) _model);
  116.     Background (UIColor_White);
  117.     Foreground (UIColor_Black);
  118.     UI_DisplaySurface& dc = CreateDisplaySurface();
  119.  
  120.     dc.ClearDisplay();
  121.     _charWidth = dc.Font().Width();
  122.     _charHeight = dc.Font().Height();
  123.     _cellWidth = 3*_charWidth+4;
  124.     _cellHeight = _charHeight+4;
  125.  
  126.     // Paint the month name
  127.     UI_Point topLeft = CalendarOrigin;
  128.     UI_Rectangle label (topLeft, 7*(_cellWidth+HorzGap), _cellHeight);
  129.     dc.WriteString (CL_Date::MonthName((CL_Date::MonthEnum)_month) + " "
  130.                     + CL_String (_year), label, UIText_Center);
  131.     topLeft += UI_Point (0, _cellHeight + GapBelowMonthLabel);
  132.  
  133.     // Paint the names of the weekdays
  134.     UI_Rectangle cell (topLeft,  _cellWidth, _cellHeight);
  135.     for (short i = 0; i < 7; i++) {
  136.         dc.WriteString (days[i], cell, UIText_Center);
  137.         cell += UI_Point (_cellWidth+HorzGap, 0);
  138.     }
  139.     topLeft += UI_Point (0, _charHeight + GapBelowDayNames);
  140.  
  141.     // Paint the month's days
  142.     CL_Date firstDay (_year, _month, 1);
  143.     short dayOfWeek = firstDay.DayOfWeek();
  144.     _dayOfFirst = dayOfWeek;
  145.     cell = UI_Rectangle (topLeft+UI_Point (2,2),
  146.                          _cellWidth-2, _cellHeight-2) +
  147.         UI_Point ((dayOfWeek-1)*(_cellWidth+HorzGap), 0);
  148.     CL_Date today = CL_Date::Today();
  149.     for (i = 1; i <= _daysInMonth; i++) {
  150.         dc.WriteString (CL_String (i, 2, ' '), cell, UIText_Right);
  151.         if (i == dt.Day() && _month == today.Month()
  152.             && _year == today.Year()) {
  153.             UI_Rectangle r (cell.Origin(), cell.Width(), cell.Height());
  154.             dc.InvertRectangle (r);
  155.         }
  156.         cell += UI_Point (_cellWidth + HorzGap, 0);
  157.         dayOfWeek++;
  158.         if (dayOfWeek > 7) {
  159.             dayOfWeek = 1;
  160.             cell.Origin(UI_Point (topLeft.XCoord(), cell.TopLeftY()
  161.                                   + _cellHeight + VertGap));
  162.         }
  163.     }
  164.     dc.DrawRectangle (UI_Rectangle (topLeft, 7*(_cellWidth+HorzGap),
  165.                                     6*(_cellHeight+VertGap)));
  166.     DestroyDisplaySurface();
  167.     return TRUE;
  168. }
  169.  
  170.  
  171. bool UI_MonthCalendar::ButtonDown (const UI_Point& p, UI_MouseButton btn,
  172.                                    bool, bool)
  173. {
  174.     if (btn != UIM_Left)
  175.         return FALSE;
  176.     UI_Rectangle monthRect
  177.         (CalendarOrigin + UI_Point
  178.          (0, 2*_charHeight + GapBelowMonthLabel + GapBelowDayNames),
  179.          7*(_cellWidth+HorzGap), 6*(_cellHeight+VertGap));
  180.     if (!monthRect.Includes (p)) {
  181.         return FALSE;
  182.     }
  183.  
  184.     UI_Point topLeft = CalendarOrigin +
  185.         UI_Point (0, 2*_charHeight + GapBelowDayNames + GapBelowMonthLabel);
  186.     UI_Rectangle cell  (topLeft,  _cellWidth, _cellHeight);
  187.     cell += UI_Point ((_dayOfFirst-1)*(_cellWidth+HorzGap), 0);
  188.     short dayOfWeek = _dayOfFirst;
  189.     for (short i = 1; i <= _daysInMonth; i++) {
  190.         if (cell.Includes (p)) {
  191.             ClickOnDay (i);
  192.             return TRUE;
  193.         }
  194.         cell += UI_Point (_cellWidth + HorzGap, 0);
  195.         dayOfWeek++;
  196.         if (dayOfWeek > 7) {
  197.             dayOfWeek = 1;
  198.             cell.Origin (UI_Point (topLeft.XCoord(), cell.TopLeftY()
  199.                                    + _cellHeight + VertGap));
  200.         }
  201.     }
  202.     return TRUE;
  203. }
  204.  
  205.  
  206.  
  207. UI_WindowClass UI_MonthCalendar::WindowClass () const
  208. {
  209. #if defined(__MS_WINDOWS__) || defined(__OS2__)
  210.     return _YACLWindowClassName;
  211. #elif defined(__X_MOTIF__)
  212.     return xmDrawingAreaWidgetClass;
  213. #endif
  214. }
  215.  
  216.  
  217. void UI_MonthCalendar::ClickOnDay (short /* day */ )
  218. {
  219. }
  220.  
  221.  
  222.