summaryrefslogtreecommitdiff
path: root/src/gui/fpg_popupcalendar.pas
blob: 87b9f3ade2411ffed5165bc1daf4bdd5c39e8700 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
{
    fpGUI  -  Free Pascal GUI Toolkit

    Copyright (C) 2006 - 2010 See the file AUTHORS.txt, included in this
    distribution, for details of the copyright.

    See the file COPYING.modifiedLGPL, included in this distribution,
    for details about redistributing fpGUI.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    Description:
      A calendar component. Soon it would be possible to use it in a
      popup windows like Calender Combobox, or directly in a Form.
}

unit fpg_popupcalendar;

{$mode objfpc}{$H+}

{.$Define DEBUG} // while developing the component

{
    *****************************************************************
    **********   This is still under heavy development!   ***********
    *****************************************************************
}


{ todo: Support highlighting special days. }
{ todo: Support custom colors. }
{ todo: Create a TfpgDateTimeEdit component with options for Date, Time or Date & Time. }
{ todo: Changing months and checking min/max limits takes into account the
        original date, not the selected day in the grid. It should use the
        selected day in grid. }
{ todo: Paint days out of min/max range in grey. }

interface

uses
  SysUtils,
  Classes,
  fpg_base,
  fpg_main,
  fpg_widget,
  fpg_popupwindow,
  fpg_edit,
  fpg_button,
  fpg_combobox,
  fpg_basegrid,
  fpg_grid,
  fpg_dialogs,
  fpg_menu,
  fpg_hyperlink,
  fpg_panel;

type

  TfpgOnDateSetEvent = procedure(Sender: TObject; const ADate: TDateTime) of object;
  TfpgOnCheckboxChangedEvent = procedure(Sender: TObject; const AIsChecked: Boolean) of object;

  TYearSelectForm = class(TfpgPopupWindow)
  private
    {@VFD_HEAD_BEGIN: YearSelectForm}
    btnMinus10: TfpgButton;
    btnPlus10: TfpgButton;
    Bevel1: TfpgBevel;
    Label1: TfpgHyperlink;
    Label2: TfpgHyperlink;
    Label3: TfpgHyperlink;
    Label4: TfpgHyperlink;
    Label5: TfpgHyperlink;
    Label6: TfpgHyperlink;
    Label7: TfpgHyperlink;
    Label8: TfpgHyperlink;
    Label9: TfpgHyperlink;
    Label10: TfpgHyperlink;
    {@VFD_HEAD_END: YearSelectForm}
    FYear: Word;
    FOriginalYear: Word;
    FMinYear: Word;
    FMaxYear: Word;
    procedure   YearClicked(Sender: TObject);
    procedure   SetYear(const AValue: Word);
    procedure   Minus10Clicked(Sender: TObject);
    procedure   Plus10Clicked(Sender: TObject);
  protected
    procedure   HandlePaint; override;
  public
    constructor CreateCustom(AOwner: TComponent; const MinYear, MaxYear: Word);
    procedure   AfterConstruction; override;
    procedure   AfterCreate;
    property    Year: Word read FYear write SetYear;
    property    MinYear: Word read FMinYear;
    property    MaxYear: Word read FMaxYear;
  end;


  TfpgPopupCalendar = class(TfpgPopupWindow)
  private
    {@VFD_HEAD_BEGIN: fpgPopupCalendar}
    edtYear: TfpgEdit;
    btnYearUp: TfpgButton;
    btnYearDown: TfpgButton;
    edtMonth: TfpgEdit;
    btnMonthUp: TfpgButton;
    btnMonthDown: TfpgButton;
    btnToday: TfpgButton;
    grdName1: TfpgStringGrid;
    {@VFD_HEAD_END: fpgPopupCalendar}
    FMonthOffset: integer;
    FDate: TDateTime;
    FMaxDate: TDateTime;
    FMinDate: TDateTime;
    FWeekStartDay: integer;
    FCallerWidget: TfpgWidget;
    FOnValueSet: TfpgOnDateSetEvent;
    FCloseOnSelect: boolean;
    FThisMonthDays: array[0..6,0..5] of boolean;
    FWeeklyHoliday: integer;
    FDayColor: TfpgColor;
    FHolidayColor: TfpgColor;
    FSelectedColor: TfpgColor;
    FSingleClickSelect: boolean;
    FMonthsPopupMenu: TfpgPopupMenu;
    FYearPopupWindow: TYearSelectForm;
    procedure   YearPopupWindowClose(Sender: TObject);
    function    GetDateElement(Index: integer): Word;
    procedure   PopulateDays;
    procedure   CalculateMonthOffset;
    function    CalculateCellDay(const ACol, ARow: Integer): Integer;
    procedure   SetDateElement(AIndex: integer; const AValue: Word);
    procedure   SetDateValue(const AValue: TDateTime);
    procedure   SetMaxDate(const AValue: TDateTime);
    procedure   SetMinDate(const AValue: TDateTime);
    procedure   SetWeekStartDay(const AValue: integer);
    procedure   SetWeeklyHoliday(const AValue: integer);
    procedure   SetDayColor(const AValue: TfpgColor);
    procedure   SetHolidayColor(const AValue: TfpgColor);
    procedure   SetSelectedColor(const AValue: TfpgColor);
    procedure   SetCloseOnSelect(const AValue: boolean);
    procedure   UpdateCalendar;
    procedure   btnYearUpClicked(Sender: TObject);
    procedure   btnYearDownClicked(Sender: TObject);
    procedure   btnMonthUpClicked(Sender: TObject);
    procedure   btnMonthDownClicked(Sender: TObject);
    procedure   btnTodayClicked(Sender: TObject);
    procedure   grdName1Clicked(Sender: TObject);
    procedure   grdName1DoubleClick(Sender: TObject; AButton: TMouseButton; AShift: TShiftState; const AMousePos: TPoint);
    procedure   grdName1KeyPress(Sender: TObject; var KeyCode: word; var ShiftState: TShiftState; var Consumed: boolean);
    procedure   grdName1DrawCell(Sender: TObject; const ARow, ACol: Integer; const ARect: TfpgRect; const AFlags: TfpgGridDrawState; var ADefaultDrawing: boolean);
    procedure   edtMonthClicked(Sender: TObject);
    procedure   edtYearClicked(Sender: TObject);
    procedure   miMonthClicked(Sender: TObject);
    procedure   TearDown;
    procedure   SetSingleClickSelect(const AValue: boolean);
    procedure   ClosePopupMenusWindows;
  protected
    FntNorm, FntBold: TfpgFont;
    FOrigFocusWin: TfpgWidget;
    procedure   HandlePaint; override;
    procedure   HandleKeyPress(var keycode: word; var shiftstate: TShiftState; var consumed: boolean); override;
    procedure   HandleShow; override;
    procedure   HandleHide; override;
    procedure   ShowDefaultPopupMenu; virtual;
    property    CallerWidget: TfpgWidget read FCallerWidget write FCallerWidget;
  public
    constructor Create(AOwner: TComponent; AOrigFocusWin: TfpgWidget); reintroduce;
    destructor  Destroy; override;
    procedure   AfterCreate;
    property    CloseOnSelect: boolean read FCloseOnSelect write SetCloseOnSelect default True;
    property    SingleClickSelect: boolean read FSingleClickSelect write SetSingleClickSelect default False;
    property    Day: Word index 1 read GetDateElement write SetDateElement;
    property    Month: Word index 2 read GetDateElement write SetDateElement;
    property    Year: Word index 3 read GetDateElement write SetDateElement;
    property    OnValueSet: TfpgOnDateSetEvent read FOnValueSet write FOnValueSet;
  published
    property    DateValue: TDateTime read FDate write SetDateValue;
    property    MinDate: TDateTime read FMinDate write SetMinDate;
    property    MaxDate: TDateTime read FMaxDate write SetMaxDate;
    property    WeekStartDay: integer read FWeekStartDay write SetWeekStartDay default 0;
    property    WeeklyHoliday: integer read FWeeklyHoliday write SetWeeklyHoliday default -1;
    property    DayColor: TfpgColor read FDayColor write SetDayColor;
    property    HolidayColor: TfpgColor read FHolidayColor write SetHolidayColor;
    property    SelectedColor: TfpgColor read FSelectedColor write SetSelectedColor;
  end;
  
  
  TfpgCalendarCombo = class(TfpgBaseStaticCombo)
  private
    FDate: TDateTime;
    FDateFormat: string;
    FMaxDate: TDateTime;
    FMinDate: TDateTime;
    FWeekStartDay: integer;
    FWeeklyHoliday: integer;
    FDayColor: TfpgColor;
    FHolidayColor: TfpgColor;
    FSelectedColor: TfpgColor;
    FCloseOnSelect: boolean;
    FSingleClickSelect: boolean;
    procedure   SetDateFormat(const AValue: string);
    procedure   SetDateValue(const AValue: TDateTime);
    procedure   SetMaxDate(const AValue: TDateTime);
    procedure   SetMinDate(const AValue: TDateTime);
    procedure   SetWeekStartDay(const AValue: integer);
    procedure   SetWeeklyHoliday(const AValue: integer);
    procedure   SetDayColor(const AValue: TfpgColor);
    procedure   SetHolidayColor(const AValue: TfpgColor);
    procedure   SetSelectedColor(const AValue: TfpgColor);
    procedure   SetCloseOnSelect(const AValue: boolean);
    procedure   SetSingleClickSelect(const AValue: boolean);
  protected
    function    GetText: string; override;
    procedure   SetText(const AValue: string); override;
    procedure   InternalOnValueSet(Sender: TObject; const ADate: TDateTime); virtual;
    function    HasText: boolean; override;
    procedure   DoDropDown; override;
  public
    constructor Create(AOwner: TComponent); override;
  published
    property    Align;
    property    BackgroundColor;
    { Clicking on calendar Today button will close the popup calendar by default }
    property    CloseOnSelect: boolean read FCloseOnSelect write SetCloseOnSelect default True;
    property    DateFormat: string read FDateFormat write SetDateFormat;
    property    DateValue: TDateTime read FDate write SetDateValue;
    property    DayColor: TfpgColor read FDayColor write SetDayColor;
    property    Enabled;
    property    FontDesc;
    property    Hint;
    property    HolidayColor: TfpgColor read FHolidayColor write SetHolidayColor;
    property    MaxDate: TDateTime read FMaxDate write SetMaxDate;
    property    MinDate: TDateTime read FMinDate write SetMinDate;
    property    ParentShowHint;
    property    ReadOnly;
    property    SelectedColor: TfpgColor read FSelectedColor write SetSelectedColor;
    property    SingleClickSelect: boolean read FSingleClickSelect write SetSingleClickSelect default False;
    property    ShowHint;
    property    WeeklyHoliday: integer read FWeeklyHoliday write SetWeeklyHoliday default -1;
    property    WeekStartDay: integer read FWeekStartDay write SetWeekStartDay default 0;
    property    TabOrder;
    property    OnChange;
    property    OnCloseUp;
    property    OnDropDown;
    property    OnEnter;
    property    OnExit;
    property    OnShowHint;
  end;


  TfpgCalendarCheckCombo = class(TfpgCalendarCombo)
  private
    FChecked: boolean;
    FCheckBoxRect: TfpgRect;
    FCheckboxChanged: TfpgOnCheckboxChangedEvent;
    procedure   SetChecked(const AValue: Boolean);
    procedure   DoCheckboxChanged;
  protected
    procedure   DoDrawText(const ARect: TfpgRect); override;
    procedure   InternalOnValueSet(Sender: TObject; const ADate: TDateTime); override;
    procedure   HandleKeyPress(var keycode: word; var shiftstate: TShiftState; var consumed: boolean); override;
    procedure   HandlePaint; override;
    procedure   HandleResize(AWidth, AHeight: TfpgCoord); override;
    procedure   HandleLMouseDown(x, y: integer; shiftstate: TShiftState); override;
    procedure   HandleLMouseUp(x, y: integer; shiftstate: TShiftState); override;
  public
    constructor Create(AOwner: TComponent); override;
  published
    property    Checked: Boolean read FChecked write SetChecked;
    property    OnCheckboxChanged: TfpgOnCheckboxChangedEvent read FCheckboxChanged write FCheckboxChanged;
    property    OnKeyPress;
  end;


{@VFD_NEWFORM_DECL}


implementation

uses
  dateutils,
  fpg_scrollbar,
  fpg_constants;

type
  // friend class to get access to Protected methods
  TPopupMenuFriend = class(TfpgPopupMenu);


{@VFD_NEWFORM_IMPL}

procedure TYearSelectForm.YearClicked(Sender: TObject);
begin
  FYear := StrToInt(TfpgHyperlink(Sender).Text);
  Close;
end;

procedure TYearSelectForm.SetYear(const AValue: Word);

  function IsInRange(const AYear: word): boolean;
  begin
    // always one year less on either side (min and max) so we don't go over
    // any possible month limits.
    Result := (AYear >= MinYear) and (AYear <= MaxYear);
  end;

begin
  if FYear = AValue then exit;
  FYear := AValue;
  if FOriginalYear = 0 then
    FOriginalYear := FYear;
  Label1.Text := IntToStr(FYear-4);
  Label1.Enabled := IsInRange(FYear-4);
  Label2.Text := IntToStr(FYear-3);
  Label2.Enabled := IsInRange(FYear-3);
  Label3.Text := IntToStr(FYear-2);
  Label3.Enabled := IsInRange(FYear-2);
  Label4.Text := IntToStr(FYear-1);
  Label4.Enabled := IsInRange(FYear-1);
  Label5.Text := IntToStr(FYear);
  if FYear = FOriginalYear then
    Label5.FontDesc := '#Label2'
  else
    Label5.FontDesc := '#Label1';
  Label5.Enabled := IsInRange(FYear);
  Label6.Text := IntToStr(FYear+1);
  Label6.Enabled := IsInRange(FYear+1);
  Label7.Text := IntToStr(FYear+2);
  Label7.Enabled := IsInRange(FYear+2);
  Label8.Text := IntToStr(FYear+3);
  Label8.Enabled := IsInRange(FYear+3);
  Label9.Text := IntToStr(FYear+4);
  Label9.Enabled := IsInRange(FYear+4);
  Label10.Text := IntToStr(FYear+5);
  Label10.Enabled := IsInRange(FYear+5);
end;

procedure TYearSelectForm.Minus10Clicked(Sender: TObject);
begin
  SetYear(FYear-10);
end;

procedure TYearSelectForm.Plus10Clicked(Sender: TObject);
begin
  SetYear(FYear+10);
end;

procedure TYearSelectForm.HandlePaint;
begin
//  inherited HandlePaint;
  Canvas.BeginDraw;
  Canvas.Clear(BackgroundColor);
//  Canvas.SetColor(clWindowBackground);
//  Canvas.DrawRectangle(0, 0, Width, Height);  // black rectangle border
  Canvas.DrawButtonFace(0, 0, Width, Height, []);  // 3d rectangle inside black border
  Canvas.EndDraw;
end;

constructor TYearSelectForm.CreateCustom(AOwner: TComponent; const MinYear, MaxYear: Word);
begin
  Create(AOwner);
  FYear := 0;
  FOriginalYear := 0;
  FMinYear := MinYear;
  FMaxYear := MaxYear;
end;

procedure TYearSelectForm.AfterConstruction;
begin
  inherited AfterConstruction;
  AfterCreate;
end;

procedure TYearSelectForm.AfterCreate;
begin
  {%region 'Auto-generated GUI code' -fold}
  {@VFD_BODY_BEGIN: YearSelectForm}
  Name := 'YearSelectForm';
  SetPosition(439, 401, 130, 122);
//  WindowTitle := 'YearSelectForm';
//  Hint := '';
//  Sizeable := False;

  btnMinus10 := TfpgButton.Create(self);
  with btnMinus10 do
  begin
    Name := 'btnMinus10';
    SetPosition(4, 4, 24, 24);
    Text := '';
    FontDesc := '#Label1';
    Hint := '';
    ImageMargin := -1;
    ImageName := 'sys.sb.left';
    ImageSpacing := 0;
    TabOrder := 1;
    OnClick := @Minus10Clicked;
  end;

  btnPlus10 := TfpgButton.Create(self);
  with btnPlus10 do
  begin
    Name := 'btnPlus10';
    SetPosition(104, 4, 24, 24);
    Text := '';
    FontDesc := '#Label1';
    Hint := '';
    ImageMargin := -1;
    ImageName := 'sys.sb.right';
    ImageSpacing := 0;
    TabOrder := 2;
    OnClick := @Plus10Clicked;
  end;

  Bevel1 := TfpgBevel.Create(self);
  with Bevel1 do
  begin
    Name := 'Bevel1';
    SetPosition(64, 32, 2, 85);
    Hint := '';
    Style := bsLowered;
  end;

  Label1 := TfpgHyperlink.Create(self);
  with Label1 do
  begin
    Name := 'Label1';
    SetPosition(8, 32, 44, 16);
    Alignment := taCenter;
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Label';
    TextColor := clText1;
    HotTrackFont := '#Label1';
    OnClick := @YearClicked;
  end;

  Label2 := TfpgHyperlink.Create(self);
  with Label2 do
  begin
    Name := 'Label2';
    SetPosition(8, 48, 44, 16);
    Alignment := taCenter;
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Label';
    TextColor := clText1;
    HotTrackFont := '#Label1';
    OnClick := @YearClicked;
  end;

  Label3 := TfpgHyperlink.Create(self);
  with Label3 do
  begin
    Name := 'Label3';
    SetPosition(8, 64, 44, 16);
    Alignment := taCenter;
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Label';
    TextColor := clText1;
    HotTrackFont := '#Label1';
    OnClick := @YearClicked;
  end;

  Label4 := TfpgHyperlink.Create(self);
  with Label4 do
  begin
    Name := 'Label4';
    SetPosition(8, 80, 44, 16);
    Alignment := taCenter;
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Label';
    TextColor := clText1;
    HotTrackFont := '#Label1';
    OnClick := @YearClicked;
  end;

  Label5 := TfpgHyperlink.Create(self);
  with Label5 do
  begin
    Name := 'Label5';
    SetPosition(8, 96, 44, 16);
    Alignment := taCenter;
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Label';
    TextColor := clText1;
    HotTrackFont := '#Label1';
    OnClick := @YearClicked;
  end;

  Label6 := TfpgHyperlink.Create(self);
  with Label6 do
  begin
    Name := 'Label6';
    SetPosition(76, 32, 44, 16);
    Alignment := taCenter;
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Label';
    TextColor := clText1;
    HotTrackFont := '#Label1';
    OnClick := @YearClicked;
  end;

  Label7 := TfpgHyperlink.Create(self);
  with Label7 do
  begin
    Name := 'Label7';
    SetPosition(76, 48, 44, 16);
    Alignment := taCenter;
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Label';
    TextColor := clText1;
    HotTrackFont := '#Label1';
    OnClick := @YearClicked;
  end;

  Label8 := TfpgHyperlink.Create(self);
  with Label8 do
  begin
    Name := 'Label8';
    SetPosition(76, 64, 44, 16);
    Alignment := taCenter;
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Label';
    TextColor := clText1;
    HotTrackFont := '#Label1';
    OnClick := @YearClicked;
  end;

  Label9 := TfpgHyperlink.Create(self);
  with Label9 do
  begin
    Name := 'Label9';
    SetPosition(76, 80, 44, 16);
    Alignment := taCenter;
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Label';
    TextColor := clText1;
    HotTrackFont := '#Label1';
    OnClick := @YearClicked;
  end;

  Label10 := TfpgHyperlink.Create(self);
  with Label10 do
  begin
    Name := 'Label10';
    SetPosition(76, 96, 44, 16);
    Alignment := taCenter;
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Label';
    TextColor := clText1;
    HotTrackFont := '#Label1';
    OnClick := @YearClicked;
  end;

  {@VFD_BODY_END: YearSelectForm}
  {%endregion}
end;


procedure TfpgPopupCalendar.PopulateDays;
var
  r, c: integer;
  lCellDay: Integer;
begin
  grdName1.BeginUpdate;
  for r := -1 to 5 do
    for c := 0 to 6 do
    begin
      if r = -1 then
        grdName1.ColumnTitle[c] := ShortDayNames[Succ((c+FWeekStartDay) mod 7)]  // ShortDayNames is 1-based indexing
      else
      begin
        lCellDay := CalculateCellDay(c, r);
        grdName1.Cells[c, r] := IntToStr(lCellDay);
      end;
    end;
  grdName1.EndUpdate;
end;

procedure TfpgPopupCalendar.grdName1DoubleClick(Sender: TObject;
  AButton: TMouseButton; AShift: TShiftState; const AMousePos: TPoint);
begin
  ClosePopupMenusWindows;
  TearDown;
end;

procedure TfpgPopupCalendar.grdName1KeyPress(Sender: TObject;
  var KeyCode: word; var ShiftState: TShiftState; var Consumed: boolean);
begin
  // Pass the grid event on to the TfpgPopupCalender instance.
  HandleKeyPress(KeyCode, ShiftState, consumed);
  Consumed := True;
end;

procedure TfpgPopupCalendar.grdName1DrawCell(Sender: TObject; const ARow, ACol: Integer; const ARect: TfpgRect;
          const AFlags: TfpgGridDrawState; var ADefaultDrawing: boolean);
begin
  with grdName1 do
  begin
    if FThisMonthDays[ACol,ARow] then
      if (ACol = FocusCol) and (ARow = FocusRow) then
        Canvas.SetTextColor(FSelectedColor)
      else
        Canvas.SetTextColor(FDayColor)
    else
      if (ACol = FocusCol) and (ARow = FocusRow) then
        Canvas.SetTextColor(FSelectedColor)
      else
        Canvas.SetTextColor(clShadow1);
    if FWeeklyHoliday >= FWeekStartDay then
      if ACol = FWeeklyHoliday - FWeekStartDay then
      begin
        Canvas.Font := FntBold;
        Canvas.SetTextColor(FHolidayColor);
      end
      else
        Canvas.Font := FntNorm
    else
      if (FWeeklyHoliday > -1) and (ACol = FWeeklyHoliday - FWeekStartDay + 7) then
      begin
        Canvas.Font := FntBold;
        Canvas.SetTextColor(FHolidayColor);
      end
      else
        Canvas.Font := FntNorm;
  end;
end;

procedure TfpgPopupCalendar.edtMonthClicked(Sender: TObject);
begin
  ClosePopupMenusWindows;
  ShowDefaultPopupMenu;
end;

procedure TfpgPopupCalendar.edtYearClicked(Sender: TObject);
begin
  ClosePopupMenusWindows;
  if not Assigned(FYearPopupWindow) then
  begin
    FYearPopupWindow := TYearSelectForm.CreateCustom(nil, YearOf(MinDate), YearOf(MaxDate));
    FYearPopupWindow.OnClose  := @YearPopupWindowClose;
    FYearPopupWindow.Year := Year;
  end;

  FYearPopupWindow.ShowAt(self, edtYear.Left, edtYear.Bottom);
end;

procedure TfpgPopupCalendar.miMonthClicked(Sender: TObject);
var
  itm: TfpgMenuItem;
begin
  itm := Sender as TfpgMenuItem;
  SetDateElement(2 {month index}, itm.Tag);
end;

procedure TfpgPopupCalendar.TearDown;
var
  lD: Word;
  s: string;
  d: TDateTime;
begin
  s := grdName1.Cells[grdName1.FocusCol, grdName1.FocusRow];
  if s = '' then
    Exit; //==>
  lD := StrToInt(s);
  if (grdName1.FocusRow = 0) and (lD > 7) then
    Exit; // clicked in previous month
  if (grdName1.FocusRow >= 4) and (lD < 15) then
    Exit; // clicked in next month
  d := EncodeDate(Year, Month, lD);
  if (d >= FMinDate) and (d <= FMaxDate) then
  begin
    DateValue := d;
    if Assigned(OnValueSet) then
      OnValueSet(self, DateValue);
    {$IFDEF DEBUG}
    writeln('Selected date: ', FormatDateTime('yyyy-mm-dd', DateValue));
    {$ENDIF}
    if CloseOnSelect then
      Close;
  end;
end;

procedure TfpgPopupCalendar.SetSingleClickSelect(const AValue: boolean);
begin
  if FSingleClickSelect = AValue then exit;
  FSingleClickSelect := AValue;
end;

procedure TfpgPopupCalendar.ClosePopupMenusWindows;
begin
  if Assigned(FMonthsPopupMenu) then
  begin
    FMonthsPopupMenu.Close;
    FreeAndNil(FMonthsPopupMenu);
  end;

  if Assigned(FYearPopupWindow) then
  begin
    FYearPopupWindow.Close;
    FreeAndNil(FYearPopupWindow);
  end;
end;

procedure TfpgPopupCalendar.YearPopupWindowClose(Sender: TObject);
begin
  Year := FYearPopupWindow.Year;
end;

function TfpgPopupCalendar.GetDateElement(Index: integer): Word;
var
  lD, lM, lY: Word;
begin
  DecodeDate(FDate, lY, lM, lD);
  case Index of
    1: Result := lD;
    2: Result := lM;
    3: Result := lY;
  end;
end;

procedure TfpgPopupCalendar.CalculateMonthOffset;
var
  lD, lM, lY: Word;
  lTheFirst: TDateTime;
begin
  DecodeDate(FDate, lY, lM, lD);
  lTheFirst := EncodeDate(lY, lM, 1);
  FMonthOffset := 2 - DayOfWeek(lTheFirst);
end;

function TfpgPopupCalendar.CalculateCellDay(const ACol, ARow: Integer): Integer;
begin
  if (FMonthOffset + FWeekStartDay) > 1 then
    Result :=  FMonthOffset - 7 + FWeekStartDay + ACol + ARow * 7
  else
    Result :=  FMonthOffset + FWeekStartDay + ACol + ARow * 7;
  if Result < 1 then
  begin
    if Month > 1 then
      Result := MonthDays[IsLeapYear(Year), Pred(Month)] + Result
    else
      Result := 31 + Result;
    FThisMonthDays[ACol,ARow] := False;
  end
  else
    if Result > MonthDays[IsLeapYear(Year), Month] then
    begin
      Result := Result - MonthDays[IsLeapYear(Year), Month];
      FThisMonthDays[ACol,ARow] := False;
    end
    else
      FThisMonthDays[ACol,ARow] := True;
end;

procedure TfpgPopupCalendar.SetDateElement(AIndex: integer; const AValue: Word);
var
  lD, lM, lY: Word;
  lDate: TDateTime;
  d: Word;
begin
  if AValue > 0 then
  begin
    DecodeDate(FDate, lY, lM, lD);
    case AIndex of
      1:  lD := AValue;
      2:  begin
            lM := AValue;
            d := DaysInAMonth(lY, lM);
            if lD > d then // If original day value is larger than days in new month
              lD := d;
          end;
      3:  lY := AValue;
    end;
    try
      lDate := EncodeDate(lY, lM, lD);
      SetDateValue(lDate);
    except
      // do nothing!  Not nice?
    end;
  end;
end;

{ If AValue is out of range (min or max), then set it to the limit value }
procedure TfpgPopupCalendar.SetDateValue(const AValue: TDateTime);
var
  lDate: TDateTime;
begin
  if FDate = AValue then
    Exit; //==>
  lDate := AValue;

  if (trunc(AValue) >= trunc(FMinDate)) then
    // do nothing - test passed
  else
    lDate := FMinDate;
    
  if (trunc(AValue) <= trunc(FMaxDate)) then
    // do nothing - test passed
  else
    lDate := FMaxDate;
    
  FDate := lDate;
  UpdateCalendar;
end;

procedure TfpgPopupCalendar.SetMaxDate(const AValue: TDateTime);
begin
  if FMaxDate = AValue then
    Exit; //==>
  FMaxDate := AValue;

  // correct min/max values
  if FMinDate > AValue then
    FMinDate := IncMonth(AValue, -12); // one year less

  if FDate > FMaxDate then
  begin
    FDate := FMaxDate;
    UpdateCalendar;
  end;
end;

procedure TfpgPopupCalendar.SetMinDate(const AValue: TDateTime);
begin
  if FMinDate = AValue then
    Exit; //==>
  FMinDate := AValue;

  // correct min/max values
  if AValue > FMaxDate then
    FMaxDate := IncMonth(AValue, 12); // one year more

  if FDate < FMinDate then
  begin
    FDate := FMinDate;
    UpdateCalendar;
  end;
end;

procedure TfpgPopupCalendar.SetWeekStartDay(const AValue: integer);
begin
  if FWeekStartDay <> AValue then
    FWeekStartDay := AValue;
end;

procedure TfpgPopupCalendar.SetWeeklyHoliday(const AValue: integer);
begin
  if FWeeklyHoliday <> AValue then
    FWeeklyHoliday := AValue;
end;

procedure TfpgPopupCalendar.SetDayColor(const AValue: TfpgColor);
begin
  if FDayColor <> AValue then
    FDayColor := AValue;
end;

procedure TfpgPopupCalendar.SetHolidayColor(const AValue: TfpgColor);
begin
  if FHolidayColor <> AValue then
    FHolidayColor := AValue;
end;

procedure TfpgPopupCalendar.SetSelectedColor(const AValue: TfpgColor);
begin
  if FSelectedColor <> AValue then
    FSelectedColor := AValue;
end;

procedure TfpgPopupCalendar.SetCloseOnSelect(const AValue: boolean);
begin
  if FCloseOnSelect = AValue then
    Exit;
  FCloseOnSelect := AValue;
end;

procedure TfpgPopupCalendar.UpdateCalendar;
var
  lD, lM, lY: Word;
begin
  try
    grdName1.BeginUpdate;
    if (FDate >= FMinDate) and (FDate <= FMaxDate) then
    begin
      CalculateMonthOffset;
      PopulateDays;
      edtYear.Text := IntToStr(Year);
      edtMonth.Text := LongMonthNames[Month];
      DecodeDate(FDate, lY, lM, lD);

      grdName1.FocusCol := (lD - FMonthOffset - FWeekStartDay) mod 7;
      grdName1.FocusRow := (lD - FMonthOffset - FWeekStartDay) div 7;
      if (FMonthOffset + FWeekStartDay) > 1 then
        grdName1.FocusRow := grdName1.FocusRow + 1;
//      grdName1.Invalidate;
    end;
  finally
    grdName1.EndUpdate;
  end;
end;

procedure TfpgPopupCalendar.btnYearUpClicked(Sender: TObject);
var
  d: TDateTime;
begin
  ClosePopupMenusWindows;
  d := IncMonth(FDate, 12);
  if d <= FMaxDate then
    DateValue := d;
end;

procedure TfpgPopupCalendar.btnYearDownClicked(Sender: TObject);
var
  d: TDateTime;
begin
  ClosePopupMenusWindows;
  d := IncMonth(FDate, -12);
  if d >= FMinDate then
    DateValue := d;
end;

procedure TfpgPopupCalendar.btnMonthUpClicked(Sender: TObject);
var
  d: TDateTime;
begin
  ClosePopupMenusWindows;
  d := IncMonth(FDate);
  if d <= FMaxDate then
    DateValue := d;
end;

procedure TfpgPopupCalendar.btnMonthDownClicked(Sender: TObject);
var
  d: TDateTime;
begin
  ClosePopupMenusWindows;
  d := IncMonth(FDate, -1);
  if d >= FMinDate then
    DateValue := d;
end;

procedure TfpgPopupCalendar.btnTodayClicked(Sender: TObject);
begin
  ClosePopupMenusWindows;
  if Now >= FMinDate then
  begin
    DateValue := Now;
    TearDown;
  end;
end;

procedure TfpgPopupCalendar.grdName1Clicked(Sender: TObject);
begin
  ClosePopupMenusWindows;
  if FSingleClickSelect then
    TearDown;
end;

procedure TfpgPopupCalendar.HandlePaint;
begin
  Canvas.BeginDraw;
  inherited HandlePaint;
  if PopupFrame then
    Canvas.SetClipRect(fpgRect(1, 1, Width-2, Height-2));
  Canvas.Clear(clWindowBackground);
  Canvas.ClearClipRect;
  Canvas.EndDraw;
end;

procedure TfpgPopupCalendar.HandleKeyPress(var keycode: word;
  var shiftstate: TShiftState; var consumed: boolean);
begin
  case keycode of
    keyUp:
        begin
          if (ssCtrl in shiftstate) then
          begin
            btnYearUpClicked(nil);    // Ctrl+Up Arrow
            consumed := True;
          end;
        end;
    keyDown:
        begin
          if (ssCtrl in shiftstate) then
          begin
            btnYearDownClicked(nil);  // Ctrl+Down Arrow
            consumed := True;
          end;
        end;
    keyLeft:
        begin
          if (ssCtrl in shiftstate) then
          begin
            btnMonthDownClicked(nil); // Ctrl+Left Arrow
            consumed := True;
          end;
        end;
    keyRight:
        begin
          if (ssCtrl in shiftstate) then
          begin
            btnMonthUpClicked(nil);   // Ctrl+Right Arrow
            consumed := True;
          end;
        end;
    keyPageUp:
        begin
          if (ssCtrl in shiftstate) then
            btnYearUpClicked(nil)   // Ctrl+PageUp
          else
            btnMonthUpClicked(nil); // PageUp
          consumed := True;
        end;
    keyPageDown:
        begin
          if (ssCtrl in shiftstate) then
            btnYearDownClicked(nil)     // Ctrl+PageDown
          else
            btnMonthDownClicked(nil);   // PageDown
          consumed := True;
        end;
  end;
  
  if not consumed then
  begin
    if keycode = keyEnter then
    begin
      consumed := True;
      TearDown;
    end
    else if keycode = keyEscape then
    begin
      consumed := True;
      Close;
    end;
  end;
  
  if not consumed then
    inherited HandleKeyPress(keycode, shiftstate, consumed);
end;

procedure TfpgPopupCalendar.HandleShow;
begin
  inherited HandleShow;
  grdName1.SetFocus;
  {$IFDEF DEBUG}
  writeln('Min: ', FormatDateTime('yyyy-mm-dd', MinDate),
          '   Max: ', FormatDateTime('yyyy-mm-dd', MaxDate));
  {$ENDIF}
end;

procedure TfpgPopupCalendar.HandleHide;
begin
  FocusRootWidget := FOrigFocusWin;
  FOrigFocusWin := nil;
  inherited HandleHide;
  if Assigned(FocusRootWidget) then
    FocusRootWidget.SetFocus;
end;

procedure TfpgPopupCalendar.ShowDefaultPopupMenu;
var
  itm: TfpgMenuItem;
  pt: TPoint;
begin
  if not Assigned(FMonthsPopupMenu) then
  begin
    FMonthsPopupMenu := TfpgPopupMenu.Create(nil);
    itm := FMonthsPopupMenu.AddMenuItem(rslongjan, '', @miMonthClicked);
    itm.Tag := 1;
    itm := FMonthsPopupMenu.AddMenuItem(rslongfeb, '', @miMonthClicked);
    itm.Tag := 2;
    itm := FMonthsPopupMenu.AddMenuItem(rslongmar, '', @miMonthClicked);
    itm.Tag := 3;
    itm := FMonthsPopupMenu.AddMenuItem(rslongapr, '', @miMonthClicked);
    itm.Tag := 4;
    itm := FMonthsPopupMenu.AddMenuItem(rsLongMay, '', @miMonthClicked);
    itm.Tag := 5;
    itm := FMonthsPopupMenu.AddMenuItem(rslongjun, '', @miMonthClicked);
    itm.Tag := 6;
    itm := FMonthsPopupMenu.AddMenuItem(rslongjul, '', @miMonthClicked);
    itm.Tag := 7;
    itm := FMonthsPopupMenu.AddMenuItem(rslongaug, '', @miMonthClicked);
    itm.Tag := 8;
    itm := FMonthsPopupMenu.AddMenuItem(rslongsep, '', @miMonthClicked);
    itm.Tag := 9;
    itm := FMonthsPopupMenu.AddMenuItem(rslongoct, '', @miMonthClicked);
    itm.Tag := 10;
    itm := FMonthsPopupMenu.AddMenuItem(rslongnov, '', @miMonthClicked);
    itm.Tag := 11;
    itm := FMonthsPopupMenu.AddMenuItem(rslongdec, '', @miMonthClicked);
    itm.Tag := 12;
  end;

  // translate Edit coordinates
  pt := WindowToScreen(self, Point(edtMonth.Left, edtMonth.Bottom));
  TPopupMenuFriend(FMonthsPopupMenu).PrepareToShow;  // forces height calculation
  // If dropdown will not fit below Edit, then we place it above
  if (pt.y + FMonthsPopupMenu.Height) > fpgApplication.ScreenHeight then
    pt.y := pt.y - edtMonth.Height - FMonthsPopupMenu.Height;

//  SetDefaultPopupMenuItemsState;
  FMonthsPopupMenu.ShowAt(nil, pt.x, pt.y);
end;

constructor TfpgPopupCalendar.Create(AOwner: TComponent; AOrigFocusWin: TfpgWidget);
begin
  inherited Create(AOwner);
  FntNorm:= fpgApplication.GetFont('arial-9');
  FntBold:= fpgApplication.GetFont('arial-9:bold');

  FOrigFocusWin := AOrigFocusWin;
  AfterCreate;
  FDate := Date;
  FWeekStartDay := 0;
  FWeeklyHoliday := -1;
  FDayColor := clText1;
  FHolidayColor := clText1;
  FSelectedColor := clWhite;
  FMonthOffset := 0;
  FCloseOnSelect := True;
  FSingleClickSelect := False;
  UpdateCalendar;
end;

destructor TfpgPopupCalendar.Destroy;
begin
  if Assigned(FMonthsPopupMenu) then
    FMonthsPopupMenu.Free;
  if Assigned(FYearPopupWindow) then
    FYearPopupWindow.Free;
  FntBold.Free;
  FntNorm.Free;
  inherited Destroy;
end;

procedure TfpgPopupCalendar.AfterCreate;
begin
  {%region 'Auto-generated GUI code' -fold}
  {@VFD_BODY_BEGIN: fpgPopupCalendar}
  Name := 'fpgPopupCalendar';
  SetPosition(370, 182, 233, 142);
  Hint := '';

  edtYear := TfpgEdit.Create(self);
  with edtYear do
  begin
    Name := 'edtYear';
    SetPosition(0, 0, 37, 22);
    AutoSize := False;
    BorderStyle := ebsSingle;
    Hint := '';
    TabOrder := 1;
    Text := '';
    FontDesc := '#Edit1';
    IgnoreMouseCursor := True;
    Focusable := False;
    OnClick := @edtYearClicked;
  end;

  btnYearUp := TfpgButton.Create(self);
  with btnYearUp do
  begin
    Name := 'btnYearUp';
    SetPosition(37, 0, 13, 11);
    Text := '';
    Embedded := True;
    FontDesc := '#Label1';
    Hint := '';
    ImageMargin := 0;
    ImageName := 'sys.sb.up';
    TabOrder := 2;
    Focusable := False;
    OnClick := @btnYearUpClicked;
  end;

  btnYearDown := TfpgButton.Create(self);
  with btnYearDown do
  begin
    Name := 'btnYearDown';
    SetPosition(37, 11, 13, 11);
    Text := '';
    Embedded := True;
    FontDesc := '#Label1';
    Hint := '';
    ImageMargin := 0;
    ImageName := 'sys.sb.down';
    TabOrder := 3;
    Focusable := False;
    OnClick := @btnYearDownClicked;
  end;

  edtMonth := TfpgEdit.Create(self);
  with edtMonth do
  begin
    Name := 'edtMonth';
    SetPosition(50, 0, 100, 22);
    AutoSize := False;
    BorderStyle := ebsSingle;
    Hint := '';
    TabOrder := 4;
    Text := '';
    FontDesc := '#Edit1';
    IgnoreMouseCursor := True;
    Focusable := False;
    OnClick  := @edtMonthClicked;
  end;

  btnMonthUp := TfpgButton.Create(self);
  with btnMonthUp do
  begin
    Name := 'btnMonthUp';
    SetPosition(150, 0, 13, 11);
    Text := '';
    Embedded := True;
    FontDesc := '#Label1';
    Hint := '';
    ImageMargin := 0;
    ImageName := 'sys.sb.up';
    TabOrder := 5;
    Focusable := False;
    OnClick := @btnMonthUpClicked;
  end;

  btnMonthDown := TfpgButton.Create(self);
  with btnMonthDown do
  begin
    Name := 'btnMonthDown';
    SetPosition(150, 11, 13, 11);
    Text := '';
    Embedded := True;
    FontDesc := '#Label1';
    Hint := '';
    ImageMargin := 0;
    ImageName := 'sys.sb.down';
    TabOrder := 6;
    Focusable := False;
    OnClick := @btnMonthDownClicked;
  end;

  btnToday := TfpgButton.Create(self);
  with btnToday do
  begin
    Name := 'btnToday';
    SetPosition(164, 0, 70, 22);
    Text := 'Today';
    FontDesc := '#Label1';
    Hint := '';
    ImageName := '';
    TabOrder := 7;
    Focusable := True;
    OnClick := @btnTodayClicked;
  end;

  grdName1 := TfpgStringGrid.Create(self);
  with grdName1 do
  begin
    Name := 'grdName1';
    SetPosition(0, 23, 233, 119);
    AddColumn('Sun', 33, taCenter);
    AddColumn('Mon', 32, taCenter);
    AddColumn('Tue', 33, taCenter);
    AddColumn('Wed', 32, taCenter);
    AddColumn('Thu', 33, taCenter);
    AddColumn('Fri', 32, taCenter);
    AddColumn('Sat', 33, taCenter);
    FontDesc := '#Grid';
    HeaderFontDesc := '#GridHeader';
    Hint := '';
    RowCount := 6;
    RowSelect := False;
    TabOrder := 8;
    ScrollBarStyle := ssNone;
    OnClick  := @grdName1Clicked;
    OnDoubleClick := @grdName1DoubleClick;
    OnKeyPress := @grdName1KeyPress;
    OnDrawCell := @grdName1DrawCell;
  end;

  {@VFD_BODY_END: fpgPopupCalendar}
  {%endregion}

  btnToday.Text := rsToday;
end;


{ TfpgCalendarCombo }

procedure TfpgCalendarCombo.SetDateValue(const AValue: TDateTime);
begin
  if ReadOnly then
    Exit;
  if FDate = AValue then
    Exit; //==>
  FDate := AValue;
  RePaint;
end;

procedure TfpgCalendarCombo.SetMaxDate(const AValue: TDateTime);
begin
  if FMaxDate = AValue then
    Exit; //==>
  FMaxDate := AValue;

  // correct min/max values
  if FMinDate > AValue then
    FMinDate := IncMonth(AValue, -12); // one year less

  if FDate > FMaxDate then
  begin
    FDate := FMaxDate;
    Repaint;
  end;
end;

procedure TfpgCalendarCombo.SetMinDate(const AValue: TDateTime);
begin
  if FMinDate = AValue then
    Exit; //==>
  FMinDate := AValue;
  
  // correct min/max values
  if AValue > FMaxDate then
    FMaxDate := IncMonth(AValue, 12); // one year more

  if FDate < FMinDate then
  begin
    FDate := FMinDate;
    Repaint;
  end;
end;

procedure TfpgCalendarCombo.SetWeekStartDay(const AValue: integer);
begin
  if FWeekStartDay <> AValue then
    FWeekStartDay := AValue;
end;

procedure TfpgCalendarCombo.SetWeeklyHoliday(const AValue: integer);
begin
  if FWeeklyHoliday <> AValue then
    FWeeklyHoliday := AValue;
end;

procedure TfpgCalendarCombo.SetSelectedColor(const AValue: TfpgColor);
begin
  if FSelectedColor <> AValue then
    FSelectedColor := AValue;
end;

procedure TfpgCalendarCombo.SetDayColor(const AValue: TfpgColor);
begin
  if FDayColor <> AValue then
    FDayColor := AValue;
end;

procedure TfpgCalendarCombo.SetHolidayColor(const AValue: TfpgColor);
begin
  if FHolidayColor <> AValue then
    FHolidayColor := AValue;
end;

procedure TfpgCalendarCombo.SetText(const AValue: string);
begin
  try
    FDate := StrToDateTime(AValue);
  except
    on E: Exception do
    begin
      ShowMessage(E.Message);
    end;
  end;
end;

function TfpgCalendarCombo.GetText: string;
begin
  Result := FormatDateTime(FDateFormat, FDate);
end;

procedure TfpgCalendarCombo.SetCloseOnSelect(const AValue: boolean);
begin
  if FCloseOnSelect = AValue then
    Exit; //==>
  FCloseOnSelect := AValue;
end;

procedure TfpgCalendarCombo.SetSingleClickSelect(const AValue: boolean);
begin
  if FSingleClickSelect = AValue then exit;
  FSingleClickSelect := AValue;
end;

function TfpgCalendarCombo.HasText: boolean;
begin
  Result := FDate >= FMinDate;
end;

constructor TfpgCalendarCombo.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FMinDate := EncodeDate(1900, 01, 01);
  FMaxDate := EncodeDate(2100, 01, 31);
  FWeeklyHoliday := -1;
  FDate := Now;
  FCloseOnSelect := True;
  FSingleClickSelect := False;
  DateFormat := ShortDateFormat;
end;

procedure TfpgCalendarCombo.InternalOnValueSet(Sender: TObject;
  const ADate: TDateTime);
begin
  DateValue := ADate;
  if Assigned(OnChange) then
    OnChange(self);
  {$IFDEF DEBUG}
  writeln('New value: ', FormatDateTime(FDateFormat, ADate));
  {$ENDIF}
end;

procedure TfpgCalendarCombo.SetDateFormat(const AValue: string);
var
  OldFormat: string;
begin
  if FDateFormat = AValue then
    Exit; //==>
  OldFormat := FDateFormat;
  FDateFormat := AValue;
  try
    FormatDateTime(FDateFormat, FDate);
    RePaint;
  except
    on E: Exception do
    begin
      FDateFormat := OldFormat;
      fpgApplication.HandleException(self);
    end;
  end;
end;

procedure TfpgCalendarCombo.DoDropDown;
var
  ddw: TfpgPopupCalendar;
begin
  if (not Assigned(FDropDown)) or (not FDropDown.HasHandle) then
  begin
    FreeAndNil(FDropDown);  // safety measure
    FDropDown := TfpgPopupCalendar.Create(nil, FocusRootWidget);
    ddw := TfpgPopupCalendar(FDropDown);
    ddw.DontCloseWidget := self;
    { Set to false CloseOnSelect to leave opened popup calendar menu }
    ddw.CloseOnSelect := CloseOnSelect;
    ddw.SingleClickSelect := SingleClickSelect;
    ddw.CallerWidget  := self;

    if Assigned(OnDropDown) then
      OnDropDown(self);

    ddw.MinDate       := FMinDate;
    ddw.MaxDate       := FMaxDate;
    ddw.DateValue     := FDate;
    ddw.WeekStartDay  := FWeekStartDay;
    ddw.WeeklyHoliday := FWeeklyHoliday;
    ddw.DayColor        := FDayColor;
    ddw.HolidayColor    := FHolidayColor;
    ddw.SelectedColor   := FSelectedColor;
    ddw.ShowAt(Parent, Left, Top+Height);
    { I added this call to UpdateCalendar because sometimes after
      btnTodayClicked event, reopeing the dropdown menu gave an empty calendar }
    ddw.UpdateCalendar; //slapshot
    ddw.PopupFrame    := True;
    ddw.OnValueSet    := @InternalOnValueSet;
    ddw.OnClose       := @InternalOnClose;
  end
  else
  begin
    FBtnPressed := False;
    FDropDown.Close;
    FreeAndNil(FDropDown);
  end;
end;

{ TfpgCalendarCheckCombo }

procedure TfpgCalendarCheckCombo.SetChecked(const AValue: Boolean);
begin
  if AValue = FChecked then
    Exit; //==>
  FChecked := Avalue;
  Repaint;
end;

procedure TfpgCalendarCheckCombo.DoCheckboxChanged;
begin
  if Assigned(FCheckboxChanged) then
    FCheckboxChanged(self, FChecked);
end;

procedure TfpgCalendarCheckCombo.DoDrawText(const ARect: TfpgRect);
var
  lRect: TfpgRect;
var
  flags: TfpgTextFlags;
begin
  lRect := ARect;
  lRect.Left := lRect.Left+FCheckBoxRect.Width + 1;
  lRect.Width := lRect.Width - (FCheckBoxRect.Width + 1);
  flags := [txtLeft, txtVCenter];
  if not Enabled then
    flags += [txtDisabled];
  if HasText then
  begin
    if not FChecked then
      Canvas.SetTextColor(clShadow1)
    else
    begin
      if Focused then
        Canvas.SetTextColor(clSelectionText)
      else
        Canvas.SetTextColor(TextColor);
    end;
    Canvas.DrawText(lRect, Text, flags)
  end
  else
  begin
    Canvas.SetTextColor(clShadow1);
    Canvas.DrawText(lRect, ExtraHint, flags);
  end;
end;

procedure TfpgCalendarCheckCombo.InternalOnValueSet(Sender: TObject;
  const ADate: TDateTime);
begin
  inherited InternalOnValueSet(Sender, ADate);
  Checked := True;
end;

procedure TfpgCalendarCheckCombo.HandleKeyPress(var keycode: word;
  var shiftstate: TShiftState; var consumed: boolean);
begin
  if keycode = keyEscape then
  begin
    consumed := True;
    Checked := False;
    DoCheckboxChanged;
  end;
  inherited HandleKeyPress(keycode, shiftstate, consumed);
end;

procedure TfpgCalendarCheckCombo.HandlePaint;
var
  img: TfpgImage;
  ix: integer;
begin
  inherited HandlePaint;
  // calculate which image to paint.
  if Enabled then
    ix := Ord(FChecked)
  else
    ix := (2 + (Ord(FChecked) * 2)) - Ord(FChecked);

  // paint the check (in this case a X)
  img := fpgImages.GetImage('sys.checkboxes');    // Do NOT localize
  Canvas.DrawImagePart(FCheckBoxRect.Left, FCheckBoxRect.Top, img, ix*13, 0, 13, 13);
end;

procedure TfpgCalendarCheckCombo.HandleResize(AWidth, AHeight: TfpgCoord);
begin
  inherited HandleResize(AWidth, AHeight);
  FCheckBoxRect.Top := (AHeight - FCheckBoxRect.Height) div 2;
  OffsetRect(FCheckboxRect, 0, 3);  // frame border must be taken into consideration
end;

procedure TfpgCalendarCheckCombo.HandleLMouseDown(x, y: integer;
  shiftstate: TShiftState);
begin
  if PtInRect(FCheckBoxRect, Point(x,y)) then
    // do nothing
  else
    inherited HandleLMouseDown(x, y, shiftstate);
end;

procedure TfpgCalendarCheckCombo.HandleLMouseUp(x, y: integer;
  shiftstate: TShiftState);
begin
  if PtInRect(FCheckBoxRect, Point(x,y)) then
  begin
    Checked := not FChecked;
    DoCheckboxChanged;
    Repaint;
  end
  else
    inherited HandleLMouseUp(x, y, shiftstate);
end;

constructor TfpgCalendarCheckCombo.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FChecked := True;
  FCheckBoxRect.SetRect(2, 0, 17, 17);
  FCheckboxRect.Top := (FHeight - FCheckBoxRect.Height) div 2;
  OffsetRect(FCheckboxRect, 2, 3);  // frame border must be taken into consideration
end;


end.