home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l040 / 8.ddi / TVDEMO.ZIP / CALENDAR.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-27  |  5.8 KB  |  257 lines

  1. {************************************************}
  2. {                                                }
  3. {   Turbo Vision Demo                            }
  4. {   Copyright (c) 1990 by Borland International  }
  5. {                                                }
  6. {************************************************}
  7.  
  8. unit Calendar;
  9.  
  10. {$F+,O+,X+,S-,D-}
  11.  
  12. { Calendar object for viewing a month at a time. See TVDEMO.PAS
  13.   for an example program that uses this unit.
  14. }
  15.  
  16. interface
  17.  
  18. uses Drivers, Objects, App, Views, Dos, Dialogs;
  19.  
  20. const
  21.  
  22.    DaysInMonth: array[1..12] of Byte =
  23.      (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  24.  
  25.    MonthStr: array[1..12] of string[10] =
  26.      ('January   ',
  27.       'February  ',
  28.       'March     ',
  29.       'April     ',
  30.       'May       ',
  31.       'June      ',
  32.       'July      ',
  33.       'August    ',
  34.       'September ',
  35.       'October   ',
  36.       'November  ',
  37.       'December  ');
  38.  
  39. type
  40.  
  41.  PCalendarView = ^TCalendarView;
  42.  TCalendarView = object(TView)
  43.    Year, Month, Days: Word;
  44.    CurYear, CurMonth, CurDay : Word;
  45.    constructor Init(Bounds: TRect);
  46.    constructor Load(var S: TStream);
  47.    procedure HandleEvent(var Event: TEvent); virtual;
  48.    procedure Draw; virtual;
  49.    procedure Store(var S: TStream);
  50.  end;
  51.  
  52.  PCalendarWindow = ^TCalendarWindow;
  53.  TCalendarWindow = object(TWindow)
  54.    constructor Init;
  55.  end;
  56.  
  57. const
  58.   RCalendarView: TStreamRec = (
  59.      ObjType: 10020;
  60.      VmtLink: Ofs(TypeOf(TCalendarView)^);
  61.      Load:    @TCalendarView.Load;
  62.      Store:   @TCalendarView.Store
  63.   );
  64.  
  65. const
  66.   RCalendarWindow: TStreamRec = (
  67.      ObjType: 10021;
  68.      VmtLink: Ofs(TypeOf(TCalendarWindow)^);
  69.      Load:    @TCalendarWindow.Load;
  70.      Store:   @TCalendarWindow.Store
  71.   );
  72.  
  73. procedure RegisterCalendar;
  74.  
  75. implementation
  76.  
  77. { TCalendarWindow }
  78. constructor TCalendarWindow.Init;
  79. var
  80.   R:TRect;
  81. begin
  82.   R.Assign(1, 1, 23, 11);
  83.   inherited Init(R, 'Calendar', 0);
  84.   Flags := Flags and not (wfZoom + wfGrow);    { Not resizeable }
  85.   GrowMode :=0;
  86.   Palette := wpCyanWindow;
  87.  
  88.   GetExtent(R);
  89.   R.Grow(-1, -1);
  90.   Insert(New(PCalendarView, Init(R)));
  91. end;
  92.  
  93. { TCalendarView }
  94. constructor TCalendarView.Init(Bounds: TRect);
  95. var
  96.   H: Word;
  97. begin
  98.   inherited Init(Bounds);
  99.   Options := Options or ofSelectable;
  100.   EventMask := EventMask or evMouseAuto;
  101.   GetDate(CurYear, CurMonth, CurDay, H);
  102.   Year := CurYear;
  103.   Month := CurMonth;
  104.   DrawView;
  105. end;
  106.  
  107. constructor TCalendarView.Load(var S: TStream);
  108. var
  109.   H: Word;
  110. begin
  111.   inherited Load(S);
  112.   GetDate(CurYear, CurMonth, CurDay, H);
  113.   S.Read(Year, SizeOf(Year));
  114.   S.Read(Month, SizeOf(Month));
  115. end;
  116.  
  117. function DayOfWeek(Day, Month, Year: Integer) : Integer;
  118. var
  119.   century, yr, dw: Integer;
  120. begin
  121.   if Month < 3 then
  122.   begin
  123.     Inc(Month, 10);
  124.     Dec(Year);
  125.   end
  126.   else
  127.      Dec(Month, 2);
  128.   century := Year div 100;
  129.   yr := year mod 100;
  130.   dw := (((26 * month - 2) div 10) + day + yr + (yr div 4) +
  131.     (century div 4) - (2 * century)) mod 7;
  132.   if dw < 0 then DayOfWeek := dw + 7
  133.   else DayOfWeek := dw;
  134. end;
  135.  
  136. procedure TCalendarView.Draw;
  137. const
  138.   Width = 20;
  139. var
  140.   i, j, DayOf, CurDays: Integer;
  141.   S: String;
  142.   B: array[0..Width] of Word;
  143.   Color, BoldColor, SpecialColor: Byte;
  144.  
  145. function Num2Str(I: Integer): String;
  146. var
  147.   S:String;
  148. begin
  149.   Str(i:2, S);
  150.   Num2Str := S;
  151. end;
  152.  
  153. begin
  154.   Color:= GetColor(6);
  155.   BoldColor:= GetColor(7);
  156.   DayOf := DayOfWeek(1, Month, Year);
  157.   Days := DaysInMonth[Month] + Byte((Year mod 4 = 0) and (Month = 2));
  158.   Str(Year:4, S);
  159.   MoveChar(B, ' ', Color, Width);
  160.   MoveStr(B, MonthStr[Month] + S+' '#30'  '#31, Color);
  161.   WriteLine(0, 0, Width, 1, B);
  162.   MoveChar(B, ' ', Color, Width);
  163.   MoveStr(B, 'Su Mo Tu We Th Fr Sa', Color);
  164.   WriteLine(0, 1, Width, 1, B);
  165.   CurDays := 1 - DayOf;
  166.   for i := 1 to 6 do
  167.   begin
  168.     MoveChar(B, ' ', Color, Width);
  169.     for j := 0 to 6 do
  170.     begin
  171.       if (CurDays < 1) or (CurDays > Days) then
  172.         MoveStr(B[J * 3], '   ', Color)
  173.       else
  174.         { if it is the current day }
  175.         if (Year = CurYear) and (Month = CurMonth) and
  176.           (CurDays = CurDay) then
  177.           MoveStr(B[J * 3], Num2Str(CurDays), BoldColor)
  178.         else
  179.           MoveStr(B[J * 3], Num2Str(CurDays), Color);
  180.       Inc(CurDays);
  181.     end;
  182.     WriteLine(0, i + 1, Width, 1, B);
  183.   end;
  184. end;
  185.  
  186. procedure TCalendarView.HandleEvent(var Event: TEvent);
  187. var
  188.   Point:TPoint;
  189.   SelectDay: Word;
  190. begin
  191.   inherited HandleEvent(Event);
  192.   if (State and sfSelected <> 0) then
  193.   begin
  194.     if Event.What and (evMouseDown + evMouseAuto) <> 0 then
  195.     begin
  196.       MakeLocal(Event.Where, Point);
  197.       if ((Point.X = 15) and (Point.Y = 0)) then
  198.       begin
  199.         Inc(Month);
  200.         if Month > 12 then
  201.         begin
  202.           Inc(Year);
  203.           Month := 1;
  204.         end;
  205.         DrawView;
  206.       end;
  207.       if ((Point.X = 18) and (Point.Y = 0)) then
  208.       begin
  209.         Dec(Month);
  210.         if Month < 1 then
  211.         begin
  212.           Dec(Year);
  213.           Month := 12;
  214.         end;
  215.         DrawView;
  216.       end;
  217.     end
  218.     else if Event.What = evKeyDown then
  219.     begin
  220.       if (Lo(Event.KeyCode) = byte('+')) or (Event.KeyCode = kbDown) then
  221.       begin
  222.         Inc(Month);
  223.         if Month > 12 then
  224.         begin
  225.           Inc(Year);
  226.           Month := 1;
  227.         end;
  228.       end;
  229.       if (Lo(Event.KeyCode) = Byte('-')) or (Event.KeyCode = kbUp) then
  230.       begin
  231.         Dec(Month);
  232.         if Month < 1 then
  233.         begin
  234.           Dec(Year);
  235.           Month := 12;
  236.         end;
  237.       end;
  238.       DrawView;
  239.     end;
  240.   end;
  241. end;
  242.  
  243. procedure TCalendarView.Store(var S: TStream);
  244. begin
  245.   inherited Store(S);
  246.   S.Write(Year, SizeOf(Year));
  247.   S.Write(Month, SizeOf(Month));
  248. end;
  249.  
  250. procedure RegisterCalendar;
  251. begin
  252.   RegisterType(RCalendarView);
  253.   RegisterType(RCalendarWindow);
  254. end;
  255.  
  256. end.
  257.