-
Notifications
You must be signed in to change notification settings - Fork 10
/
calendar.pas
1088 lines (1007 loc) · 26.1 KB
/
calendar.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{/////////////////////////////////////////////////////////////////////////
//
// Dos Navigator Open Source
//
// Version history:
//
// CALENDAR.PAS from DN OSP 3.7.0
// lite edition for DN/2 2.09 by Max Piwamoto at 30-Jan-2006
// lite edition for DN/2 2.09 by AK155 at 02-Feb-2006
//
//////////////////////////////////////////////////////////////////////////}
{$I STDEFINE.INC}
unit Calendar;
{$F+,O+,X+,S-}
interface
uses
Defines, Drivers, Objects2, Streams, Views
;
type
PCalendarView = ^TCalendarView;
TCalendarView = object(TView)
Year: AWord;
Month: Byte;
Days: Byte;
CurYear: AWord;
CurMonth: Byte;
CurDay: Byte;
FDay: Byte;
RomanEaster: integer;
OrthodoxEaster: integer;
constructor Init(Bounds: TRect);
constructor Load(var S: TStream);
destructor Done; virtual;
procedure HandleEvent(var Event: TEvent); virtual;
procedure Draw; virtual;
procedure Store(var S: TStream);
procedure NextYear;
procedure PrevYear;
procedure NextMonth;
procedure PrevMonth;
procedure NextWeek;
procedure PrevWeek;
procedure CurDate;
function GetDateText: String;
private
procedure SetDay(ADay: Byte);
procedure _NextDay;
procedure _PrevDay;
procedure FixNext;
procedure FixPrev;
procedure YearChanged;
end;
PCalendarWindow = ^TCalendarWindow;
TCalendarWindow = object(TWindow)
CalendarView: PCalendarView;
constructor Init;
procedure HandleEvent(var Event: TEvent); virtual;
procedure Awaken; virtual;
function GetTitle(MaxSize: integer): TTitleStr; virtual;
destructor Done; virtual;
function GetPalette: PPalette; virtual;
end;
procedure InsertCalendar;
implementation
uses
Commands, DNApp, Dos, Dialogs, advance1, DNHelp, DnIni, advance, xTime,
advance7, dnutil
;
var
SundayFirst: Boolean;
const
MinYear = 1;
MaxYear = High(AWord);
CalendarViewWidth = 28;
CalendarViewHeight = 8;
GregorianThreshold = 1582; { 5..14 October skipped }
{ TCalendarWindow object }
const
CCalendarWindow = #32#33#34#35#36#205
+#206#207#208#209
+#210#211;
{ Palette layout }
{ 1 = Frame passive }
{ 2 = Frame active }
{ 3 = Frame icon }
{ 4 = ScrollBar page area }
{ 5 = ScrollBar controls }
{ 6 = StaticText/Category 0 }
{ 7 = Category 1 }
{ 8 = Category 2 }
{ 9 = Category 3 }
{ 10 = Category 4 }
{ 11 = Header }
{ 12 = Info panel }
const
Calend: PCalendarWindow = nil;
(*****************************************************************
*
* Calendar Tools
*
*****************************************************************)
(*****************************************************************
*
* FUNCTION: GetEasterDate
*
* PURPOSE: This function returns Easter Sunday day and month
* for a specified year and method.
*
* INPUTS: Year - Any year between 326 and 4099.
* Method - 1 = the original calculation based on the
* Julian calendar
* 2 = the original calculation, with the
* Julian date converted to the
* equivalent Gregorian calendar
* 3 = the revised calculation based on the
* Gregorian calendar
*
* OUTPUTS: None.
*
* RETURNS: 0 - Error; invalid arguments
* Hi(result) - Month
* Lo(result) - Day
*
* ORIGINAL NOTES:
*
* This algorithm is an arithmetic interpretation
* of the 3 step Easter Dating Method developed
* by Ron Mallen 1985, as a vast improvement on
* the method described in the Common Prayer Book
*
* Published Australian Almanac 1988
* Refer to this publication, or the Canberra Library
* for a clear understanding of the method used
*
* Because this algorithm is a direct translation of
* the official tables, it can be easily proved to be
* 100% correct
*
* It's free! Please do not modify code or comments!
*
* HISTORY:
*
*****************************************************************)
function GetEasterDate(Year: word; Method: word): word;
var
FirstDig: integer; { intermediate results }
Remain19: integer;
temp: integer;
tA: integer; { table A to E results }
tB: integer;
tC: integer;
tD: integer;
tE: integer;
d: integer; { Easter Sunday day }
begin
GetEasterDate := 0; { default invalid result }
if (Year < 326) or (Year > 4099) then
{ Easter dates are valid }
Exit; { for years 326..4099 }
if (Year < 1583) and (Method <> 1) then
{ Wester or Orthodox Easter }
Exit; { is valid since 1583 }
FirstDig := Year div 100; { first two digits of year }
Remain19 := Year mod 19; { remainder of year / 19 }
if (Method = 1) or (Method = 2) then
begin
{ calculate PFM date }
tA := ((225-11*Remain19) mod 30)+21;
{ find the next Sunday }
tB := (tA-19) mod 7;
tC := (40-FirstDig) mod 7;
temp := Year mod 100;
tD := (temp+temp div 4) mod 7;
tE := ((20-tB-tC-tD) mod 7)+1;
d := tA+tE;
if Method = 2 then
begin
{ convert Julian to Gregorian date }
{ 10 days were skipped in the Gregorian calendar from 5-14 Oct 1582 }
temp := 10;
{ only 1 in every 4 century years are leap years in the Gregorian }
{ calendar (every century is a leap year in the Julian calendar) }
if Year > 1600 then
temp := temp+FirstDig-16-((FirstDig-16) div 4);
d := d+temp;
end;
end
else if Method = 3 then
begin
{ calculate PFM date }
temp := (FirstDig-15) div 2+202-11*Remain19;
if (FirstDig > 26) then
temp := temp-1;
if (FirstDig > 38) then
temp := temp-1;
if FirstDig in [21, 24, 25, 33, 36, 37] then
temp := temp-1;
temp := temp mod 30;
tA := temp+21;
if temp = 29 then
tA := tA-1;
if (temp = 28) and (Remain19 > 10) then
tA := tA-1;
{ find the next Sunday }
tB := (tA-19) mod 7;
tC := (40-FirstDig) mod 4;
if tC = 3 then
tC := tC+1;
if tC > 1 then
tC := tC+1;
temp := Year mod 100;
tD := (temp+temp div 4) mod 7;
tE := ((20-tB-tC-tD) mod 7)+1;
d := tA+tE;
end
else
begin
Exit;
end;
if d > 61 then
begin
{ when the original calculation is converted to the Gregorian }
{ calendar, Easter Sunday can occur in May }
GetEasterDate := $0500+(d-61);
end
else if d > 31 then
begin
GetEasterDate := $0400+(d-31);
end
else
begin
GetEasterDate := $0300+d;
end;
end { GetEasterDate };
(*****************************************************************
*
* FUNCTION: IsLeapYear
*
* PURPOSE: This function checks if given year is a leap year.
*
* INPUTS: Year - A year to check.
*
* OUTPUTS: None.
*
* RETURNS: True - The given year is a leap year.
* False - The given year is not a leap year.
*
* NOTES:
*
* HISTORY:
*
*****************************************************************)
function IsLeapYear(Year: word): Boolean;
begin
if Year > GregorianThreshold then
IsLeapYear := ((Year mod 4) = 0)
and (((Year mod 100) <> 0) or ((Year mod 400) = 0))
else
IsLeapYear := (Year mod 4) = 0;
end;
(*****************************************************************
*
* FUNCTION: DaysInMonth
*
* PURPOSE: This function returns number of days in given
* month. The year is important to calculating
* February days.
*
* INPUTS: Month - A month number (1..12).
* Year - A year.
*
* OUTPUTS: None.
*
* RETURNS: Number of days for selected month.
*
* NOTES:
*
* HISTORY:
*
*****************************************************************)
const
DefaultDaysInMonth: array[1..12] of Word =
(31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31);
function DaysInMonth(Month, Year: word): word;
begin
Result := DefaultDaysInMonth[Month];
if (Month=2) and IsLeapYear(Year) then
Inc(Result);
end;
(*****************************************************************
*
* FUNCTION: DayOfYear
*
* PURPOSE: This function return the day number in a year.
*
* INPUTS: Day - A day in month.
* Month - A month in a year (1..12).
* Year - A year.
*
* OUTPUTS: None.
*
* RETURNS: The day number in a year:
* 1 - 1 January
* ...
* 365 - 31 December (ordinary year)
* 366 - 31 December (leap year)
*
* NOTES:
*
* HISTORY:
*
*****************************************************************)
function DayOfYear(Day, Month, Year: word): word;
begin
if (Year = GregorianThreshold)
and (((Month = 10) and (Day > 14)) or (Month > 10))
then
Dec(Day, 10);
while Month > 1 do
begin
Dec(Month);
Inc(Day, DaysInMonth(Month, Year));
end;
DayOfYear := Day;
end;
(*****************************************************************
*
* FUNCTION: JulianDay
*
* PURPOSE: This function calculates day number since 1.1.1.
*
* INPUTS: Day - A day in month.
* Month - A month in a year (1..12).
* Year - A year.
*
* OUTPUTS: None.
*
* RETURNS: 1 = 1.1.1
* 365 = 31.12.1
* 366 = 1.1.2
* ...
*
* NOTES:
*
* HISTORY:
*
*****************************************************************)
function JulianDay(Day, Month, Year: word): LongInt;
begin
Dec(Year);
Result := LongInt(365)*Year+Year div 4;
if Year >= GregorianThreshold then
Dec(Result, Year div 100-Year div 400-2);
Inc(Result, DayOfYear(Day, Month, Year+1));
end;
(*****************************************************************
*
* FUNCTION: DayOfWeek
*
* PURPOSE: This function returns weekday for given date.
*
* INPUTS: Day - A day in month.
* Month - A month in a year (1..12).
* Year - A year.
*
* OUTPUTS: None.
*
* RETURNS: 0 - Sunday
* 1 - Monday
* 2 - Tuesday
* 3 - Wednesday
* 4 - Thursday
* 5 - Friday
* 6 - Saturday
*
* NOTES:
*
* HISTORY:
*
*****************************************************************)
function DayOfWeek(Day, Month, Year: word): word;
begin
DayOfWeek := (JulianDay(Day, Month, Year)+5) mod 7;
end;
(*****************************************************************
*
* FUNCTION: WeekNumber
*
* PURPOSE: This function returns week number in a year for
* a given day.
*
* INPUTS: Day - A day in month.
* Month - A month in a year (1..12).
* Year - A year.
* SundayFirst - Is a Sunday first day of week?
*
* OUTPUTS: None.
*
* RETURNS: Week number from 1 to 53.
*
* NOTES: 1) What Is the Week Number?
* -----------------------------
* International standard IS-8601 (mentioned in
* section 5.5) assigns a number to each week of
* the year. A week that lies partly in one year
* and partly in another is assigned a number in
* the year in which most of its days lie. This
* means that
*
* Week 1 of any year is the week that contains
* 4 January,
*
* or equivalently
*
* Week 1 of any year is the week that contains
* the first Thursday in January.
*
* Most years have 52 weeks, but years that start
* on a Thursday and leap years that start on a
* Wednesday have 53 weeks.
*
* 2) According to note 1) Sunday is a last day in
* a week. But there is SundayFirst argument that
* calculates weeks with Sunday as a first day of
* week. This is not compatible with ISO standard
* but is compatible with "week-rows" in DN's
* calendar :).
*
* HISTORY:
*
*****************************************************************)
function WeekNumber(Day, Month, Year: word; SundayFirst: Boolean): word;
var
w: word;
d: word;
begin
d := DayOfYear(Day, Month, Year); { current day in a year }
w := DayOfWeek(Day, Month, Year);
if (w = 0) and not SundayFirst then
w := 7;
{ look for thursday and its year in the week }
if w < 4 then
begin { forward }
Inc(d, 4-w);
w := DayOfYear(31, 12, Year);
if d > w then
begin
Inc(Year);
Dec(d, w);
end;
end
else if w > 4 then
begin { backward }
Dec(w, 4);
if d <= w then
begin
Dec(Year);
Inc(d, DayOfYear(31, 12, Year));
end;
Dec(d, w);
end;
{ look for the first thurstay in the year }
w := (11-DayOfWeek(1, 1, Year)) mod 7;
{ calculate week number }
WeekNumber := (d-w) div 7+1;
end { WeekNumber };
(*****************************************************************
*
* TCalendarWindow
*
*****************************************************************)
constructor TCalendarWindow.Init;
var
R: TRect;
begin
if CalSundayFirst < 2
then
SundayFirst := (CalSundayFirst = 1)
else
SundayFirst := not ((UpStrg(ActiveLanguage) = 'RUSSIAN') or
(UpStrg(ActiveLanguage) = 'UKRAIN'));
R.Assign(1, 1, CalendarViewWidth+3, CalendarViewHeight+3);
inherited Init(R, GetString(dlcTitle), 0);
Options := Options or ofVersion20;
GrowMode := 0;
Flags := Flags and not (wfZoom+wfGrow) or (wfMove+wfClose);
{ Not resizeable }
MoveTo(25, 7);
GetExtent(R);
R.Grow(-1, -1);
CalendarView := New(PCalendarView, Init(R));
Insert(CalendarView);
Calend := @Self;
HelpCtx := hcCalendar;
end { TCalendarWindow.Init };
function TCalendarWindow.GetTitle(MaxSize: integer): TTitleStr;
begin
GetTitle := GetString(dlcTitle);
end;
procedure TCalendarWindow.HandleEvent(var Event: TEvent);
var
S: String;
begin
case Event.What of
evCommand:
repeat
case Event.Command of
cmGetName:
PString(Event.InfoPtr)^:= GetString(dlcTitle);
else {case}
break; { skip ClearEvent without using goto statement }
end {case};
ClearEvent(Event);
until True;
evKeyDown:
begin
case Event.KeyCode of
kbESC:
Close;
kbEnter:
begin
InterfaceStr := CalendarView^.GetDateText;
Event.What := evCommand;
Event.Command := cmInsertText;
Application^.PutEvent(Event);
ClearEvent(Event);
Close;
Exit;
end;
kbCtrlIns:
begin
PutInClip(CalendarView^.GetDateText);
ClearEvent(Event);
end;
end {case};
end;
end {case};
inherited HandleEvent(Event);
end { TCalendarWindow.HandleEvent };
procedure TCalendarWindow.Awaken;
begin
inherited Awaken;
Calend := @Self;
end;
destructor TCalendarWindow.Done;
begin
Calend := nil;
inherited Done;
end;
function TCalendarWindow.GetPalette: PPalette;
const
P: String[Length(CCalendarWindow)] = CCalendarWindow;
begin
GetPalette := @P;
end;
(*****************************************************************
*
* TCalendarView
*
*****************************************************************)
constructor TCalendarView.Init(Bounds: TRect);
begin
inherited Init(Bounds);
Options := Options or ofSelectable;
EventMask := EventMask or evMouseAuto or evBroadcast;
UpdTicks := 20;
RegisterToBackground(@Self);
YearChanged;
CurDate;
DrawView;
end;
constructor TCalendarView.Load(var S: TStream);
var
H, y, m, d: word;
begin
inherited Load(S);
GetDate(y, m, d, H);
CurYear := y;
CurMonth := Lo(m);
CurDay := Lo(d);
UpdTicks := 20;
RegisterToBackground(@Self);
S.Read(Year, SizeOf(Year));
S.Read(Month, SizeOf(Month));
S.Read(FDay, SizeOf(FDay));
Days := DaysInMonth(Month, Year);
YearChanged;
DrawView;
end;
destructor TCalendarView.Done;
begin
inherited Done;
end;
procedure TCalendarView.Draw;
const
uPalette = #3#6#7#8#9#10#11#12;
var
Colors: array[1..8] of Byte;
B: array[0..CalendarViewWidth] of AWord;
S1: String[CalendarViewWidth];
S2: String[3];
w: integer;
i, j, d: integer;
c: Byte;
DayOf: Byte;
Width: integer;
begin
{0123456789012345678901234567}
{ July 2000 < > = }
{ Mo Tu We Th Fr Sa Su }
Width := Size.X;
S1 := uPalette;
for i := Low(Colors) to High(Colors) do
Colors[i] := GetColor(Ord(S1[i]));
{ draw header }
MoveChar(B[0], ' ', Colors[7], Width);
S1 := GetString(TStrIdx(Ord(dlcJanuary)+Month-1));
MoveStr(B[1], S1, Colors[7]);
Str(Year: 4, S1);
MoveStr(B[15], S1, Colors[7]);
MoveStr(B[19], ' < > = ', Colors[8]);
WriteLine(0, 0, Width, 1, B);
{ draw weekdays }
c := Colors[7];
MoveChar(B[0], ' ', 2, Width);
S1 := DaysOfWeek;
if (Length(S1) <> 14) and (Length(S1) <> 21) then
begin
S1 := GetString(stDaysWeek);
end;
if Length(S1) = 14 then
begin
for i := 6 DownTo 0 do
Insert(' ', S1, i*2+1);
end;
if SundayFirst then
w := 0
else
w := 1;
for i := 0 to 6 do
begin
j := (i+w) mod 7;
MoveStr(B[i*4], Copy(S1, j*3+1, 3)+' ', c);
end;
WriteLine(0, 1, Width, 1, B);
{ draw days }
DayOf := DayOfWeek(1, Month, Year);
w := 2-DayOf;
if SundayFirst then
Dec(w, 1);
if w > 1 then
Dec(w, 7);
for i := 1 to 6 do
begin
MoveChar(B[0], ' ', Colors[2], Width);
for j := 0 to 6 do
begin
c := Colors[2];
if (w < 1) or (w > Days) then
begin
S1 := ' ';
end
else
begin
Str(w: 2, S1);
if (w = CurDay) and (Month=CurMonth) and (Year=CurYear) then
begin
if w = FDay then
c := Colors[6]
else
c := Colors[5];
end
else if w = FDay then
c := Colors[4];
S1 := ' '+S1+' ';
DayOf := (DayOf+1) mod 7;
end;
MoveStr(B[j*4], S1, c);
Inc(w);
if (Year = GregorianThreshold) and (Month = 10)
and (w in [5..14])
then
w := 15;
end;
{ week/day indicator }
if (i = 6) and ((CalOptionFlags and $0008) <> 0) then
begin
w := WeekNumber(FDay, Month, Year, SundayFirst and
((CalOptionFlags and $0010) <> 0));
d := DayOfYear(FDay, Month, Year);
Str(w: 2, S1);
Str(d: 3, S2);
S1 := '['+S1+'/'+S2+']';
MoveStr(B[20], S1, Colors[2]);
end;
WriteLine(0, i+1, Width, 1, B);
end;
end { TCalendarView.Draw };
procedure TCalendarView.HandleEvent(var Event: TEvent);
var
Point: TPoint;
SelectDay: integer;
begin
inherited HandleEvent(Event);
if (State and sfSelected <> 0) then
begin
if Event.What and (evMouseDown+evMouseAuto) <> 0 then
begin
MakeLocal(Event.Where, Point);
if Point.Y = 0 then
repeat
case Point.X of
18, 19, 20:
begin
if (Event.Buttons and mbLeftButton) <> 0 then
PrevMonth
else if (Event.Buttons and mbRightButton) <> 0 then
PrevYear
else
break;
end;
21, 22, 23:
begin
if (Event.Buttons and mbLeftButton) <> 0 then
NextMonth
else if (Event.Buttons and mbRightButton) <> 0 then
NextYear
else
break;
end;
24, 25, 26:
begin
CurDate;
end;
else {case}
break;
end {case};
DrawView;
until True
else if (Point.Y >= 2) and (Point.Y <= 7) then
begin
SelectDay := 2-DayOfWeek(1, Month, Year);
if SundayFirst then
Dec(SelectDay, 1);
if SelectDay > 1 then
Dec(SelectDay, 7);
Inc(SelectDay, (Point.Y-2)*7+(Point.X div 4));
if (Year = GregorianThreshold) and (Month = 10)
and (SelectDay > 4)
then
Inc(SelectDay, 10);
if (SelectDay >= 1) and (SelectDay <= DaysInMonth(Month, Year))
then
begin
SetDay(SelectDay);
DrawView;
end;
end;
end
else if Event.What = evKeyDown then
begin
repeat
case Event.KeyCode of
kbRight, kbGrayPlus:
_NextDay;
kbLeft, kbGrayMinus:
_PrevDay;
kbUp:
PrevWeek;
kbDown:
NextWeek;
kbCtrlLeft, kbPgUp:
PrevMonth;
kbCtrlRight, kbPgDn:
NextMonth;
kbAltLeft, kbCtrlPgUp:
PrevYear;
kbAltRight, kbCtrlPgDn:
NextYear;
kbSpace, kbHome:
CurDate;
else {case}
break;
end {case};
ClearEvent(Event);
DrawView;
until True;
end;
end;
end { TCalendarView.HandleEvent };
procedure TCalendarView.Store(var S: TStream);
begin
inherited Store(S);
S.Write(Year, SizeOf(Year));
S.Write(Month, SizeOf(Month));
S.Write(FDay, SizeOf(FDay));
end;
procedure TCalendarView.NextYear;
begin
if Year < MaxYear then
begin
Inc(Year);
if Month = 2 then
begin
Days := DaysInMonth(Month, Year);
if FDay > Days then
FDay := Days;
end;
YearChanged;
end
else
begin
Month := 12;
FDay := 31;
end;
FixNext;
end;
procedure TCalendarView.PrevYear;
begin
if Year > MinYear then
begin
Dec(Year);
if Month = 2 then
begin
Days := DaysInMonth(Month, Year);
if FDay > Days then
FDay := Days;
end;
YearChanged;
end
else
begin
Month := 1;
FDay := 1;
end;
FixPrev;
end;
procedure TCalendarView.NextMonth;
begin
if Month < 12 then
begin
Inc(Month);
end
else if Year < MaxYear then
begin
Month := 1;
Inc(Year);
YearChanged;
end
else
begin
FDay := 31;
end;
Days := DaysInMonth(Month, Year);
if FDay > Days then
FDay := Days;
FixNext;
end;
procedure TCalendarView.PrevMonth;
begin
if Month > 1 then
begin
Dec(Month);
end
else if Year > MinYear then
begin
Month := 12;
Dec(Year);
YearChanged;
end
else
begin
FDay := 1;
end;
Days := DaysInMonth(Month, Year);
if FDay > Days then
FDay := Days;
FixPrev;
end;
procedure TCalendarView.NextWeek;
var
i: integer;
begin
for i := 1 to 7 do
_NextDay;
end;
procedure TCalendarView.PrevWeek;
var
i: integer;
begin
for i := 1 to 7 do
_PrevDay;
end;
procedure TCalendarView.CurDate;
var
h: word;
y: word;
m: word;
d: word;
b1: Boolean;
begin
GetDate(y, m, d, h);
CurYear := y;
CurMonth := m;
CurDay := d;
b1 := CurYear <> Year;
Year := CurYear;
Month := CurMonth;
Days := DaysInMonth(Month, Year);
FDay := CurDay;
if b1 then
YearChanged;
end;
function TCalendarView.GetDateText: String;
begin
MakeDateFull(FDay, Month, Year, 0, 0, Result, True);
SetLength(Result, 10);
end;
procedure TCalendarView.SetDay(ADay: Byte);
begin
if FDay = ADay then
Exit;
FDay := ADay;
end;