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
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <vcl/svapp.hxx>
#include <vcl/help.hxx>
#include <vcl/menu.hxx>
#include <vcl/settings.hxx>
#include <vcl/event.hxx>
#include <vcl/commandevent.hxx>
#include <unotools/calendarwrapper.hxx>
#include <unotools/localedatawrapper.hxx>
#include <com/sun/star/i18n/Weekdays.hpp>
#include <com/sun/star/i18n/CalendarDisplayIndex.hpp>
#include <com/sun/star/i18n/CalendarFieldIndex.hpp>
#include <sal/log.hxx>
#include <vcl/calendar.hxx>
#include <svdata.hxx>
#include <strings.hrc>
#include <memory>
#define DAY_OFFX 4
#define DAY_OFFY 2
#define MONTH_BORDERX 4
#define MONTH_OFFY 3
#define WEEKDAY_OFFY 3
#define TITLE_OFFY 3
#define TITLE_BORDERY 2
#define SPIN_OFFX 4
#define SPIN_OFFY TITLE_BORDERY
#define CALENDAR_HITTEST_DAY (sal_uInt16(0x0001))
#define CALENDAR_HITTEST_MONTHTITLE (sal_uInt16(0x0004))
#define CALENDAR_HITTEST_PREV (sal_uInt16(0x0008))
#define CALENDAR_HITTEST_NEXT (sal_uInt16(0x0010))
#define MENU_YEAR_COUNT 3
using namespace ::com::sun::star;
static void ImplCalendarSelectDate( IntDateSet* pTable, const Date& rDate, bool bSelect )
{
if ( bSelect )
pTable->insert( rDate.GetDate() );
else
pTable->erase( rDate.GetDate() );
}
void Calendar::ImplInit( WinBits nWinStyle )
{
mpSelectTable.reset(new IntDateSet);
mnDayCount = 0;
mnWinStyle = nWinStyle;
mnFirstYear = 0;
mnLastYear = 0;
mbCalc = true;
mbFormat = true;
mbDrag = false;
mbSelection = false;
mbMenuDown = false;
mbSpinDown = false;
mbPrevIn = false;
mbNextIn = false;
mbTravelSelect = false;
mbAllSel = false;
OUString aGregorian( "gregorian");
maCalendarWrapper.loadCalendar( aGregorian,
Application::GetAppLocaleDataWrapper().getLanguageTag().getLocale());
if (maCalendarWrapper.getUniqueID() != aGregorian)
{
SAL_WARN( "vcl.control", "Calendar::ImplInit: No ``gregorian'' calendar available for locale ``"
<< Application::GetAppLocaleDataWrapper().getLanguageTag().getBcp47()
<< "'' and other calendars aren't supported. Using en-US fallback." );
/* If we ever wanted to support other calendars than Gregorian a lot of
* rewrite would be necessary to internally replace use of class Date
* with proper class CalendarWrapper methods, get rid of fixed 12
* months, fixed 7 days, ... */
maCalendarWrapper.loadCalendar( aGregorian, lang::Locale( "en", "US", ""));
}
SetFirstDate( maCurDate );
ImplCalendarSelectDate( mpSelectTable.get(), maCurDate, true );
// Sonstige Strings erzeugen
maDayText = VclResId(STR_SVT_CALENDAR_DAY);
maWeekText = VclResId(STR_SVT_CALENDAR_WEEK);
// Tagestexte anlegen
for (sal_Int32 i = 0; i < 31; ++i)
maDayTexts[i] = OUString::number(i+1);
ImplInitSettings();
}
void Calendar::ApplySettings(vcl::RenderContext& rRenderContext)
{
const StyleSettings& rStyleSettings = rRenderContext.GetSettings().GetStyleSettings();
maSelColor = rStyleSettings.GetHighlightTextColor();
SetPointFont(rRenderContext, rStyleSettings.GetToolFont());
rRenderContext.SetTextColor(rStyleSettings.GetFieldTextColor());
rRenderContext.SetBackground(Wallpaper(rStyleSettings.GetFieldColor()));
}
void Calendar::ImplInitSettings()
{
const StyleSettings& rStyleSettings = GetSettings().GetStyleSettings();
maSelColor = rStyleSettings.GetHighlightTextColor();
SetPointFont(*this, rStyleSettings.GetToolFont());
SetTextColor(rStyleSettings.GetFieldTextColor());
SetBackground(Wallpaper(rStyleSettings.GetFieldColor()));
}
Calendar::Calendar( vcl::Window* pParent, WinBits nWinStyle ) :
Control( pParent, nWinStyle & (WB_TABSTOP | WB_GROUP | WB_BORDER | WB_3DLOOK) ),
maCalendarWrapper( Application::GetAppLocaleDataWrapper().getComponentContext() ),
maOldFormatFirstDate( 0, 0, 1900 ),
maOldFormatLastDate( 0, 0, 1900 ),
maFirstDate( 0, 0, 1900 ),
maOldFirstDate( 0, 0, 1900 ),
maCurDate( Date::SYSTEM ),
maOldCurDate( 0, 0, 1900 )
{
ImplInit( nWinStyle );
}
Calendar::~Calendar()
{
disposeOnce();
}
void Calendar::dispose()
{
mpSelectTable.reset();
mpOldSelectTable.reset();
Control::dispose();
}
DayOfWeek Calendar::ImplGetWeekStart() const
{
// Map i18n::Weekdays to Date DayOfWeek
DayOfWeek eDay;
sal_Int16 nDay = maCalendarWrapper.getFirstDayOfWeek();
switch (nDay)
{
case i18n::Weekdays::SUNDAY :
eDay = SUNDAY;
break;
case i18n::Weekdays::MONDAY :
eDay = MONDAY;
break;
case i18n::Weekdays::TUESDAY :
eDay = TUESDAY;
break;
case i18n::Weekdays::WEDNESDAY :
eDay = WEDNESDAY;
break;
case i18n::Weekdays::THURSDAY :
eDay = THURSDAY;
break;
case i18n::Weekdays::FRIDAY :
eDay = FRIDAY;
break;
case i18n::Weekdays::SATURDAY :
eDay = SATURDAY;
break;
default:
SAL_WARN( "vcl.control", "Calendar::ImplGetWeekStart: broken i18n Gregorian calendar (getFirstDayOfWeek())");
eDay = SUNDAY;
}
return eDay;
}
void Calendar::ImplFormat()
{
if ( !mbFormat )
return;
if ( mbCalc )
{
Size aOutSize = GetOutputSizePixel();
if ( (aOutSize.Width() <= 1) || (aOutSize.Height() <= 1) )
return;
OUString const a99Text("99");
long n99TextWidth = GetTextWidth( a99Text );
long nTextHeight = GetTextHeight();
// calculate width and x-position
mnDayWidth = n99TextWidth+DAY_OFFX;
mnMonthWidth = mnDayWidth*7;
mnMonthWidth += MONTH_BORDERX*2;
mnMonthPerLine = aOutSize.Width() / mnMonthWidth;
if ( !mnMonthPerLine )
mnMonthPerLine = 1;
long nOver = (aOutSize.Width()-(mnMonthPerLine*mnMonthWidth)) / mnMonthPerLine;
mnMonthWidth += nOver;
mnDaysOffX = MONTH_BORDERX;
mnDaysOffX += nOver/2;
// calculate height and y-position
mnDayHeight = nTextHeight + DAY_OFFY;
mnWeekDayOffY = nTextHeight + TITLE_OFFY + (TITLE_BORDERY*2);
mnDaysOffY = mnWeekDayOffY + nTextHeight + WEEKDAY_OFFY;
mnMonthHeight = (mnDayHeight*6) + mnDaysOffY;
mnMonthHeight += MONTH_OFFY;
mnLines = aOutSize.Height() / mnMonthHeight;
if ( !mnLines )
mnLines = 1;
mnMonthHeight += (aOutSize.Height()-(mnLines*mnMonthHeight)) / mnLines;
// calculate spinfields
long nSpinSize = nTextHeight+TITLE_BORDERY-SPIN_OFFY;
maPrevRect.SetLeft( SPIN_OFFX );
maPrevRect.SetTop( SPIN_OFFY );
maPrevRect.SetRight( maPrevRect.Left()+nSpinSize );
maPrevRect.SetBottom( maPrevRect.Top()+nSpinSize );
maNextRect.SetLeft( aOutSize.Width()-SPIN_OFFX-nSpinSize-1 );
maNextRect.SetTop( SPIN_OFFY );
maNextRect.SetRight( maNextRect.Left()+nSpinSize );
maNextRect.SetBottom( maNextRect.Top()+nSpinSize );
// Calculate DayOfWeekText (gets displayed in a narrow font)
maDayOfWeekText.clear();
long nStartOffX = 0;
sal_Int16 nDay = maCalendarWrapper.getFirstDayOfWeek();
for ( sal_Int16 nDayOfWeek = 0; nDayOfWeek < 7; nDayOfWeek++ )
{
// Use narrow name.
OUString aDayOfWeek( maCalendarWrapper.getDisplayName(
i18n::CalendarDisplayIndex::DAY, nDay, 2));
long nOffX = (mnDayWidth-GetTextWidth( aDayOfWeek ))/2;
if ( !nDayOfWeek )
nStartOffX = nOffX;
else
nOffX -= nStartOffX;
nOffX += nDayOfWeek * mnDayWidth;
mnDayOfWeekAry[nDayOfWeek] = nOffX;
maDayOfWeekText += aDayOfWeek;
nDay++;
nDay %= 7;
}
mbCalc = false;
}
// calculate number of days
DayOfWeek eStartDay = ImplGetWeekStart();
sal_uInt16 nWeekDay;
Date aTempDate = GetFirstMonth();
maFirstDate = aTempDate;
nWeekDay = static_cast<sal_uInt16>(aTempDate.GetDayOfWeek());
nWeekDay = (nWeekDay+(7-static_cast<sal_uInt16>(eStartDay))) % 7;
maFirstDate.AddDays( -nWeekDay );
mnDayCount = nWeekDay;
sal_uInt16 nDaysInMonth;
sal_uInt16 nMonthCount = static_cast<sal_uInt16>(mnMonthPerLine*mnLines);
for ( sal_uInt16 i = 0; i < nMonthCount; i++ )
{
nDaysInMonth = aTempDate.GetDaysInMonth();
mnDayCount += nDaysInMonth;
aTempDate.AddDays( nDaysInMonth );
}
Date aTempDate2 = aTempDate;
--aTempDate2;
nDaysInMonth = aTempDate2.GetDaysInMonth();
aTempDate2.AddDays( -(nDaysInMonth-1) );
nWeekDay = static_cast<sal_uInt16>(aTempDate2.GetDayOfWeek());
nWeekDay = (nWeekDay+(7-static_cast<sal_uInt16>(eStartDay))) % 7;
mnDayCount += 42-nDaysInMonth-nWeekDay;
// determine colours
maOtherColor = COL_LIGHTGRAY;
if ( maOtherColor.IsRGBEqual( GetBackground().GetColor() ) )
maOtherColor = COL_GRAY;
Date aLastDate = GetLastDate();
if ( (maOldFormatLastDate != aLastDate) ||
(maOldFormatFirstDate != maFirstDate) )
{
maOldFormatFirstDate = maFirstDate;
maOldFormatLastDate = aLastDate;
}
// get DateInfo
sal_Int16 nNewFirstYear = maFirstDate.GetYear();
sal_Int16 nNewLastYear = GetLastDate().GetYear();
if ( mnFirstYear )
{
if ( nNewFirstYear < mnFirstYear )
{
mnFirstYear = nNewFirstYear;
}
if ( nNewLastYear > mnLastYear )
{
mnLastYear = nNewLastYear;
}
}
else
{
mnFirstYear = nNewFirstYear;
mnLastYear = nNewLastYear;
}
mbFormat = false;
}
sal_uInt16 Calendar::ImplHitTest( const Point& rPos, Date& rDate ) const
{
if ( mbFormat )
return 0;
if ( maPrevRect.IsInside( rPos ) )
return CALENDAR_HITTEST_PREV;
else if ( maNextRect.IsInside( rPos ) )
return CALENDAR_HITTEST_NEXT;
long nY;
long nOffX;
sal_Int32 nDay;
DayOfWeek eStartDay = ImplGetWeekStart();
rDate = GetFirstMonth();
nY = 0;
for ( long i = 0; i < mnLines; i++ )
{
if ( rPos.Y() < nY )
return 0;
long nX = 0;
long nYMonth = nY+mnMonthHeight;
for ( long j = 0; j < mnMonthPerLine; j++ )
{
if ( (rPos.X() < nX) && (rPos.Y() < nYMonth) )
return 0;
sal_uInt16 nDaysInMonth = rDate.GetDaysInMonth();
// matching month was found
if ( (rPos.X() > nX) && (rPos.Y() < nYMonth) &&
(rPos.X() < nX+mnMonthWidth) )
{
if ( rPos.Y() < (nY+(TITLE_BORDERY*2)+mnDayHeight))
return CALENDAR_HITTEST_MONTHTITLE;
else
{
long nDayX = nX+mnDaysOffX;
long nDayY = nY+mnDaysOffY;
if ( rPos.Y() < nDayY )
return 0;
sal_Int32 nDayIndex = static_cast<sal_Int32>(rDate.GetDayOfWeek());
nDayIndex = (nDayIndex+(7-static_cast<sal_Int32>(eStartDay))) % 7;
if ( (i == 0) && (j == 0) )
{
Date aTempDate = rDate;
aTempDate.AddDays( -nDayIndex );
for ( nDay = 0; nDay < nDayIndex; nDay++ )
{
nOffX = nDayX + (nDay*mnDayWidth);
if ( (rPos.Y() >= nDayY) && (rPos.Y() < nDayY+mnDayHeight) &&
(rPos.X() >= nOffX) && (rPos.X() < nOffX+mnDayWidth) )
{
rDate = aTempDate;
rDate.AddDays( nDay );
return CALENDAR_HITTEST_DAY;
}
}
}
for ( nDay = 1; nDay <= nDaysInMonth; nDay++ )
{
if ( rPos.Y() < nDayY )
{
rDate.AddDays( nDayIndex );
return 0;
}
nOffX = nDayX + (nDayIndex*mnDayWidth);
if ( (rPos.Y() >= nDayY) && (rPos.Y() < nDayY+mnDayHeight) &&
(rPos.X() >= nOffX) && (rPos.X() < nOffX+mnDayWidth) )
{
rDate.AddDays( nDay-1 );
return CALENDAR_HITTEST_DAY;
}
if ( nDayIndex == 6 )
{
nDayIndex = 0;
nDayY += mnDayHeight;
}
else
nDayIndex++;
}
if ( (i == mnLines-1) && (j == mnMonthPerLine-1) )
{
sal_uInt16 nWeekDay = static_cast<sal_uInt16>(rDate.GetDayOfWeek());
nWeekDay = (nWeekDay+(7-static_cast<sal_uInt16>(eStartDay))) % 7;
sal_Int32 nDayCount = 42-nDaysInMonth-nWeekDay;
Date aTempDate = rDate;
aTempDate.AddDays( nDaysInMonth );
for ( nDay = 1; nDay <= nDayCount; nDay++ )
{
if ( rPos.Y() < nDayY )
{
rDate.AddDays( nDayIndex );
return 0;
}
nOffX = nDayX + (nDayIndex*mnDayWidth);
if ( (rPos.Y() >= nDayY) && (rPos.Y() < nDayY+mnDayHeight) &&
(rPos.X() >= nOffX) && (rPos.X() < nOffX+mnDayWidth) )
{
rDate = aTempDate;
rDate.AddDays( nDay-1 );
return CALENDAR_HITTEST_DAY;
}
if ( nDayIndex == 6 )
{
nDayIndex = 0;
nDayY += mnDayHeight;
}
else
nDayIndex++;
}
}
}
}
rDate.AddDays( nDaysInMonth );
nX += mnMonthWidth;
}
nY += mnMonthHeight;
}
return 0;
}
namespace
{
void ImplDrawSpinArrow(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect, bool bPrev)
{
long i;
long n;
long nLines;
long nHeight = rRect.GetHeight();
long nWidth = rRect.GetWidth();
if (nWidth < nHeight)
n = nWidth;
else
n = nHeight;
if (!(n & 0x01))
n--;
nLines = n/2;
tools::Rectangle aRect(Point( rRect.Left() + (nWidth / 2) - (nLines / 2),
rRect.Top() + (nHeight / 2) ),
Size(1, 1));
if (!bPrev)
{
aRect.AdjustLeft(nLines );
aRect.AdjustRight(nLines );
}
rRenderContext.DrawRect(aRect);
for (i = 0; i < nLines; i++)
{
if (bPrev)
{
aRect.AdjustLeft( 1 );
aRect.AdjustRight( 1 );
}
else
{
aRect.AdjustLeft( -1 );
aRect.AdjustRight( -1 );
}
aRect.AdjustTop( -1 );
aRect.AdjustBottom( 1 );
rRenderContext.DrawRect(aRect);
}
}
} //end anonymous namespace
void Calendar::ImplDrawSpin(vcl::RenderContext& rRenderContext )
{
rRenderContext.SetLineColor();
rRenderContext.SetFillColor(rRenderContext.GetSettings().GetStyleSettings().GetButtonTextColor());
tools::Rectangle aOutRect = maPrevRect;
aOutRect.AdjustLeft(3 );
aOutRect.AdjustTop(3 );
aOutRect.AdjustRight( -3 );
aOutRect.AdjustBottom( -3 );
ImplDrawSpinArrow(rRenderContext, aOutRect, true);
aOutRect = maNextRect;
aOutRect.AdjustLeft(3 );
aOutRect.AdjustTop(3 );
aOutRect.AdjustRight( -3 );
aOutRect.AdjustBottom( -3 );
ImplDrawSpinArrow(rRenderContext, aOutRect, false);
}
void Calendar::ImplDrawDate(vcl::RenderContext& rRenderContext,
long nX, long nY,
sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear,
bool bOther, sal_Int32 nToday )
{
Color const * pTextColor = nullptr;
const OUString& rDay = maDayTexts[nDay - 1];
tools::Rectangle aDateRect(nX, nY, nX + mnDayWidth - 1, nY + mnDayHeight - 1);
bool bSel = false;
bool bFocus = false;
// actual day
if ((nDay == maCurDate.GetDay()) &&
(nMonth == maCurDate.GetMonth()) &&
(nYear == maCurDate.GetYear()))
{
bFocus = true;
}
if (mpSelectTable)
{
if (mpSelectTable->find(Date(nDay, nMonth, nYear).GetDate()) != mpSelectTable->end())
bSel = true;
}
// get textcolour
if (bSel)
pTextColor = &maSelColor;
else if (bOther)
pTextColor = &maOtherColor;
if (bFocus)
HideFocus();
// display background
const StyleSettings& rStyleSettings = rRenderContext.GetSettings().GetStyleSettings();
if (bSel)
{
rRenderContext.SetLineColor();
rRenderContext.SetFillColor(rStyleSettings.GetHighlightColor());
rRenderContext.DrawRect(aDateRect);
}
// display text
long nTextX = nX + (mnDayWidth - GetTextWidth(rDay)) - (DAY_OFFX / 2);
long nTextY = nY + (mnDayHeight - GetTextHeight()) / 2;
if (pTextColor)
{
Color aOldColor = rRenderContext.GetTextColor();
rRenderContext.SetTextColor(*pTextColor);
rRenderContext.DrawText(Point(nTextX, nTextY), rDay);
rRenderContext.SetTextColor(aOldColor);
}
else
rRenderContext.DrawText(Point(nTextX, nTextY), rDay);
// today
Date aTodayDate(maCurDate);
if (nToday)
aTodayDate.SetDate(nToday);
else
aTodayDate = Date(Date::SYSTEM);
if ((nDay == aTodayDate.GetDay()) &&
(nMonth == aTodayDate.GetMonth()) &&
(nYear == aTodayDate.GetYear()))
{
rRenderContext.SetLineColor(rStyleSettings.GetWindowTextColor());
rRenderContext.SetFillColor();
rRenderContext.DrawRect(aDateRect);
}
// if needed do FocusRect
if (bFocus && HasFocus())
ShowFocus(aDateRect);
}
void Calendar::ImplDraw(vcl::RenderContext& rRenderContext)
{
ImplFormat();
const StyleSettings& rStyleSettings = rRenderContext.GetSettings().GetStyleSettings();
Size aOutSize(GetOutputSizePixel());
long i;
long j;
long nY;
long nDeltaX;
long nDeltaY;
long nDayX;
long nDayY;
sal_Int32 nToday = Date(Date::SYSTEM).GetDate();
sal_uInt16 nDay;
sal_uInt16 nMonth;
sal_Int16 nYear;
Date aDate = GetFirstMonth();
DayOfWeek eStartDay = ImplGetWeekStart();
HideFocus();
nY = 0;
for (i = 0; i < mnLines; i++)
{
// display title bar
rRenderContext.SetLineColor();
rRenderContext.SetFillColor(rStyleSettings.GetFaceColor());
tools::Rectangle aTitleRect(0, nY, aOutSize.Width() - 1, nY + mnDayHeight - DAY_OFFY + TITLE_BORDERY * 2);
rRenderContext.DrawRect(aTitleRect);
Point aTopLeft1(aTitleRect.Left(), aTitleRect.Top());
Point aTopLeft2(aTitleRect.Left(), aTitleRect.Top() + 1);
Point aBottomRight1(aTitleRect.Right(), aTitleRect.Bottom());
Point aBottomRight2(aTitleRect.Right(), aTitleRect.Bottom() - 1);
rRenderContext.SetLineColor(rStyleSettings.GetDarkShadowColor());
rRenderContext.DrawLine(aTopLeft1, Point(aBottomRight1.X(), aTopLeft1.Y()));
rRenderContext.SetLineColor(rStyleSettings.GetLightColor() );
rRenderContext.DrawLine(aTopLeft2, Point(aBottomRight2.X(), aTopLeft2.Y()));
rRenderContext.DrawLine(aTopLeft2, Point(aTopLeft2.X(), aBottomRight2.Y()));
rRenderContext.SetLineColor(rStyleSettings.GetShadowColor() );
rRenderContext.DrawLine(Point(aTopLeft2.X(), aBottomRight2.Y()), aBottomRight2);
rRenderContext.DrawLine(Point(aBottomRight2.X(), aTopLeft2.Y()), aBottomRight2);
rRenderContext.SetLineColor(rStyleSettings.GetDarkShadowColor());
rRenderContext.DrawLine(Point(aTopLeft1.X(), aBottomRight1.Y()), aBottomRight1);
Point aSepPos1(0, aTitleRect.Top() + TITLE_BORDERY);
Point aSepPos2(0, aTitleRect.Bottom() - TITLE_BORDERY);
for (j = 0; j < mnMonthPerLine-1; j++)
{
aSepPos1.AdjustX(mnMonthWidth-1 );
aSepPos2.setX( aSepPos1.X() );
rRenderContext.SetLineColor(rStyleSettings.GetShadowColor());
rRenderContext.DrawLine(aSepPos1, aSepPos2);
aSepPos1.AdjustX( 1 );
aSepPos2.setX( aSepPos1.X() );
rRenderContext.SetLineColor(rStyleSettings.GetLightColor());
rRenderContext.DrawLine(aSepPos1, aSepPos2);
}
long nX = 0;
for (j = 0; j < mnMonthPerLine; j++)
{
nMonth = aDate.GetMonth();
nYear = aDate.GetYear();
// display month in title bar
nDeltaX = nX;
nDeltaY = nY + TITLE_BORDERY;
OUString aMonthText = maCalendarWrapper.getDisplayName(i18n::CalendarDisplayIndex::MONTH, nMonth - 1, 1)
+ " "
+ OUString::number(nYear);
long nMonthTextWidth = rRenderContext.GetTextWidth(aMonthText);
long nMonthOffX1 = 0;
long nMonthOffX2 = 0;
if (i == 0)
{
if (j == 0)
nMonthOffX1 = maPrevRect.Right() + 1;
if (j == mnMonthPerLine - 1)
nMonthOffX2 = aOutSize.Width() - maNextRect.Left() + 1;
}
long nMaxMonthWidth = mnMonthWidth - nMonthOffX1 - nMonthOffX2 - 4;
if (nMonthTextWidth > nMaxMonthWidth)
{
// Abbreviated month name.
aMonthText = maCalendarWrapper.getDisplayName(i18n::CalendarDisplayIndex::MONTH, nMonth - 1, 0)
+ " "
+ OUString::number(nYear);
nMonthTextWidth = rRenderContext.GetTextWidth(aMonthText);
}
long nTempOff = (mnMonthWidth - nMonthTextWidth + 1) / 2;
if (nTempOff < nMonthOffX1)
nDeltaX += nMonthOffX1 + 1;
else
{
if (nTempOff + nMonthTextWidth > mnMonthWidth - nMonthOffX2)
nDeltaX += mnMonthWidth - nMonthOffX2 - nMonthTextWidth;
else
nDeltaX += nTempOff;
}
rRenderContext.SetTextColor(rStyleSettings.GetButtonTextColor());
rRenderContext.DrawText(Point(nDeltaX, nDeltaY), aMonthText);
rRenderContext.SetTextColor(rStyleSettings.GetWindowTextColor());
// display week bar
nDayX = nX + mnDaysOffX;
nDayY = nY + mnWeekDayOffY;
nDeltaY = nDayY + mnDayHeight;
rRenderContext.SetLineColor(rStyleSettings.GetWindowTextColor());
Point aStartPos(nDayX, nDeltaY);
rRenderContext.DrawLine(aStartPos, Point(nDayX + (7 * mnDayWidth), nDeltaY));
rRenderContext.DrawTextArray(Point(nDayX + mnDayOfWeekAry[0], nDayY), maDayOfWeekText, &(mnDayOfWeekAry[1]));
// display days
sal_uInt16 nDaysInMonth = aDate.GetDaysInMonth();
nDayX = nX + mnDaysOffX;
nDayY = nY + mnDaysOffY;
sal_uInt16 nDayIndex = static_cast<sal_uInt16>(aDate.GetDayOfWeek());
nDayIndex = (nDayIndex + (7 - static_cast<sal_uInt16>(eStartDay))) % 7;
if (i == 0 && j == 0)
{
Date aTempDate = aDate;
aTempDate.AddDays( -nDayIndex );
for (nDay = 0; nDay < nDayIndex; ++nDay)
{
nDeltaX = nDayX + (nDay * mnDayWidth);
ImplDrawDate(rRenderContext, nDeltaX, nDayY, nDay + aTempDate.GetDay(),
aTempDate.GetMonth(), aTempDate.GetYear(),
true, nToday);
}
}
for (nDay = 1; nDay <= nDaysInMonth; nDay++)
{
nDeltaX = nDayX + (nDayIndex * mnDayWidth);
ImplDrawDate(rRenderContext, nDeltaX, nDayY, nDay, nMonth, nYear,
false, nToday);
if (nDayIndex == 6)
{
nDayIndex = 0;
nDayY += mnDayHeight;
}
else
nDayIndex++;
}
if ((i == mnLines - 1) && (j == mnMonthPerLine - 1))
{
sal_uInt16 nWeekDay = static_cast<sal_uInt16>(aDate.GetDayOfWeek());
nWeekDay = (nWeekDay + (7 - static_cast<sal_uInt16>(eStartDay))) % 7;
sal_uInt16 nDayCount = 42 - nDaysInMonth - nWeekDay;
Date aTempDate = aDate;
aTempDate.AddDays( nDaysInMonth );
for (nDay = 1; nDay <= nDayCount; ++nDay)
{
nDeltaX = nDayX + (nDayIndex * mnDayWidth);
ImplDrawDate(rRenderContext, nDeltaX, nDayY, nDay,
aTempDate.GetMonth(), aTempDate.GetYear(),
true, nToday);
if (nDayIndex == 6)
{
nDayIndex = 0;
nDayY += mnDayHeight;
}
else
nDayIndex++;
}
}
aDate.AddDays( nDaysInMonth );
nX += mnMonthWidth;
}
nY += mnMonthHeight;
}
// draw spin buttons
ImplDrawSpin(rRenderContext);
}
void Calendar::ImplUpdateDate( const Date& rDate )
{
if (IsReallyVisible() && IsUpdateMode())
{
tools::Rectangle aDateRect(GetDateRect(rDate));
if (!aDateRect.IsEmpty())
{
Invalidate(aDateRect);
}
}
}
void Calendar::ImplUpdateSelection( IntDateSet* pOld )
{
IntDateSet* pNew = mpSelectTable.get();
for (auto const& nKey : *pOld)
{
if ( pNew->find(nKey) == pNew->end() )
{
Date aTempDate(nKey);
ImplUpdateDate(aTempDate);
}
}
for (auto const& nKey : *pNew)
{
if ( pOld->find(nKey) == pOld->end() )
{
Date aTempDate(nKey);
ImplUpdateDate(aTempDate);
}
}
}
void Calendar::ImplMouseSelect( const Date& rDate, sal_uInt16 nHitTest )
{
std::unique_ptr<IntDateSet> pOldSel(new IntDateSet( *mpSelectTable ));
Date aOldDate = maCurDate;
Date aTempDate = rDate;
if ( !(nHitTest & CALENDAR_HITTEST_DAY) )
--aTempDate;
if ( !(nHitTest & CALENDAR_HITTEST_DAY) )
aTempDate = maOldCurDate;
if ( aTempDate != maCurDate )
{
maCurDate = aTempDate;
ImplCalendarSelectDate( mpSelectTable.get(), aOldDate, false );
ImplCalendarSelectDate( mpSelectTable.get(), maCurDate, true );
}
bool bNewSel = *pOldSel != *mpSelectTable;
if ( (maCurDate != aOldDate) || bNewSel )
{
HideFocus();
if ( bNewSel )
ImplUpdateSelection( pOldSel.get() );
if ( !bNewSel || pOldSel->find( aOldDate.GetDate() ) == pOldSel->end() )
ImplUpdateDate( aOldDate );
// assure focus rectangle is displayed again
if ( HasFocus() || !bNewSel
|| mpSelectTable->find( maCurDate.GetDate() ) == mpSelectTable->end() )
ImplUpdateDate( maCurDate );
}
}
void Calendar::ImplUpdate( bool bCalcNew )
{
if (IsReallyVisible() && IsUpdateMode())
{
if (bCalcNew && !mbCalc)
{
Invalidate();
}
else if (!mbFormat && !mbCalc)
{
Invalidate();
}
}
if (bCalcNew)
mbCalc = true;
mbFormat = true;
}
void Calendar::ImplScroll( bool bPrev )
{
Date aNewFirstMonth = GetFirstMonth();
if ( bPrev )
{
--aNewFirstMonth;
aNewFirstMonth.AddDays( -(aNewFirstMonth.GetDaysInMonth()-1));
}
else
aNewFirstMonth.AddDays( aNewFirstMonth.GetDaysInMonth());
SetFirstDate( aNewFirstMonth );
}
void Calendar::ImplShowMenu( const Point& rPos, const Date& rDate )
{
EndSelection();
Date aOldFirstDate = GetFirstMonth();
ScopedVclPtrInstance<PopupMenu> aPopupMenu;
sal_uInt16 nMonthOff;
sal_uInt16 nCurItemId;
sal_uInt16 nYear = rDate.GetYear()-1;
sal_uInt16 i;
sal_uInt16 j;
sal_uInt16 nYearIdCount = 1000;
nMonthOff = (rDate.GetYear()-aOldFirstDate.GetYear())*12;
if ( aOldFirstDate.GetMonth() < rDate.GetMonth() )
nMonthOff += rDate.GetMonth()-aOldFirstDate.GetMonth();
else
nMonthOff -= aOldFirstDate.GetMonth()-rDate.GetMonth();
// construct menu (include years with different months)
for ( i = 0; i < MENU_YEAR_COUNT; i++ )
{
VclPtrInstance<PopupMenu> pYearPopupMenu;
for ( j = 1; j <= 12; j++ )
pYearPopupMenu->InsertItem( nYearIdCount+j,
maCalendarWrapper.getDisplayName(
i18n::CalendarDisplayIndex::MONTH, j-1, 1));
aPopupMenu->InsertItem( 10+i, OUString::number( nYear+i ) );
aPopupMenu->SetPopupMenu( 10+i, pYearPopupMenu );
nYearIdCount += 1000;
}
mbMenuDown = true;
nCurItemId = aPopupMenu->Execute( this, rPos );
mbMenuDown = false;
if ( !nCurItemId )
return;
sal_uInt16 nTempMonthOff = nMonthOff % 12;
sal_uInt16 nTempYearOff = nMonthOff / 12;
sal_uInt16 nNewMonth = nCurItemId % 1000;
sal_uInt16 nNewYear = nYear+((nCurItemId-1000)/1000);
if ( nTempMonthOff < nNewMonth )
nNewMonth = nNewMonth - nTempMonthOff;
else
{
nNewYear--;
nNewMonth = 12-(nTempMonthOff-nNewMonth);
}
nNewYear = nNewYear - nTempYearOff;
SetFirstDate( Date( 1, nNewMonth, nNewYear ) );
}
void Calendar::ImplTracking( const Point& rPos, bool bRepeat )
{
Date aTempDate = maCurDate;
sal_uInt16 nHitTest = ImplHitTest( rPos, aTempDate );
if ( mbSpinDown )
{
mbPrevIn = (nHitTest & CALENDAR_HITTEST_PREV) != 0;
mbNextIn = (nHitTest & CALENDAR_HITTEST_NEXT) != 0;
if ( bRepeat && (mbPrevIn || mbNextIn) )
{
ImplScroll( mbPrevIn );
}
}
else
ImplMouseSelect( aTempDate, nHitTest );
}
void Calendar::ImplEndTracking( bool bCancel )
{
bool bSelection = mbSelection;
bool bSpinDown = mbSpinDown;
mbDrag = false;
mbSelection = false;
mbSpinDown = false;
mbPrevIn = false;
mbNextIn = false;
if ( bCancel )
{
if ( maOldFirstDate != maFirstDate )
SetFirstDate( maOldFirstDate );
if ( !bSpinDown )
{
std::unique_ptr<IntDateSet> pOldSel(new IntDateSet( *mpSelectTable ));
Date aOldDate = maCurDate;
maCurDate = maOldCurDate;
*mpSelectTable = *mpOldSelectTable;
HideFocus();
ImplUpdateSelection( pOldSel.get() );
if ( pOldSel->find( aOldDate.GetDate() ) == pOldSel->end() )
ImplUpdateDate( aOldDate );
// assure focus rectangle is displayed again
if ( HasFocus() || mpSelectTable->find( maCurDate.GetDate() ) == mpSelectTable->end() )
ImplUpdateDate( maCurDate );
}
}
if ( bSpinDown )
return;
if ( !bCancel )
{
// determine if we should scroll the visible area
if ( !mpSelectTable->empty() )
{
Date aFirstSelDate( *mpSelectTable->begin() );
Date aLastSelDate( *mpSelectTable->rbegin() );
if ( aLastSelDate < GetFirstMonth() )
ImplScroll( true );
else if ( GetLastMonth() < aFirstSelDate )
ImplScroll( false );
}
}
if ( mbAllSel ||
(!bCancel && ((maCurDate != maOldCurDate) || (*mpOldSelectTable != *mpSelectTable))) )
Select();
if ( !bSelection && (mnWinStyle & WB_TABSTOP) && !bCancel )
GrabFocus();
mpOldSelectTable.reset();
}
void Calendar::MouseButtonDown( const MouseEvent& rMEvt )
{
if ( rMEvt.IsLeft() && !mbMenuDown )
{
Date aTempDate = maCurDate;
sal_uInt16 nHitTest = ImplHitTest( rMEvt.GetPosPixel(), aTempDate );
if ( nHitTest )
{
if ( nHitTest & CALENDAR_HITTEST_MONTHTITLE )
ImplShowMenu( rMEvt.GetPosPixel(), aTempDate );
else
{
maOldFirstDate = maFirstDate;
mbPrevIn = (nHitTest & CALENDAR_HITTEST_PREV) != 0;
mbNextIn = (nHitTest & CALENDAR_HITTEST_NEXT) != 0;
if ( mbPrevIn || mbNextIn )
{
mbSpinDown = true;
ImplScroll( mbPrevIn );
// it should really read BUTTONREPEAT, therefore do not
// change it to SCROLLREPEAT, check with TH,
// why it could be different (71775)
StartTracking( StartTrackingFlags::ButtonRepeat );
}
else
{
if ( (rMEvt.GetClicks() != 2) || !(nHitTest & CALENDAR_HITTEST_DAY) )
{
maOldCurDate = maCurDate;
mpOldSelectTable.reset(new IntDateSet( *mpSelectTable ));
if ( !mbSelection )
{
mbDrag = true;
StartTracking();
}
ImplMouseSelect( aTempDate, nHitTest );
}
if (rMEvt.GetClicks() == 2)
maActivateHdl.Call(this);
}
}
}
return;
}
Control::MouseButtonDown( rMEvt );
}
void Calendar::MouseButtonUp( const MouseEvent& rMEvt )
{
if ( rMEvt.IsLeft() && mbSelection )
ImplEndTracking( false );
else
Control::MouseButtonUp( rMEvt );
}
void Calendar::MouseMove( const MouseEvent& rMEvt )
{
if ( mbSelection && rMEvt.GetButtons() )
ImplTracking( rMEvt.GetPosPixel(), false );
else
Control::MouseMove( rMEvt );
}
void Calendar::Tracking( const TrackingEvent& rTEvt )
{
Point aMousePos = rTEvt.GetMouseEvent().GetPosPixel();
if ( rTEvt.IsTrackingEnded() )
ImplEndTracking( rTEvt.IsTrackingCanceled() );
else
ImplTracking( aMousePos, rTEvt.IsTrackingRepeat() );
}
void Calendar::KeyInput( const KeyEvent& rKEvt )
{
Date aNewDate = maCurDate;
switch ( rKEvt.GetKeyCode().GetCode() )
{
case KEY_HOME:
aNewDate.SetDay( 1 );
break;
case KEY_END:
aNewDate.SetDay( aNewDate.GetDaysInMonth() );
break;
case KEY_LEFT:
--aNewDate;
break;
case KEY_RIGHT:
++aNewDate;
break;
case KEY_UP:
aNewDate.AddDays( -7 );
break;
case KEY_DOWN:
aNewDate.AddDays( 7 );
break;
case KEY_PAGEUP:
{
Date aTempDate = aNewDate;
aTempDate.AddDays( -(aNewDate.GetDay()+1) );
aNewDate.AddDays( -aTempDate.GetDaysInMonth() );
}
break;
case KEY_PAGEDOWN:
aNewDate.AddDays( aNewDate.GetDaysInMonth() );
break;
case KEY_RETURN:
break;
default:
Control::KeyInput( rKEvt );
break;
}
if ( aNewDate != maCurDate )
{
SetCurDate( aNewDate );
mbTravelSelect = true;
Select();
mbTravelSelect = false;
}
if (rKEvt.GetKeyCode().GetCode() == KEY_RETURN)
{
if (maActivateHdl.IsSet())
maActivateHdl.Call(this);
else
Control::KeyInput(rKEvt);
}
}
void Calendar::Paint( vcl::RenderContext& rRenderContext, const tools::Rectangle& )
{
ImplDraw(rRenderContext);
}
void Calendar::GetFocus()
{
ImplUpdateDate( maCurDate );
Control::GetFocus();
}
void Calendar::LoseFocus()
{
HideFocus();
Control::LoseFocus();
}
void Calendar::Resize()
{
ImplUpdate( true );
Control::Resize();
}
void Calendar::RequestHelp( const HelpEvent& rHEvt )
{
if ( rHEvt.GetMode() & (HelpEventMode::QUICK | HelpEventMode::BALLOON) )
{
Date aDate = maCurDate;
if ( GetDate( ScreenToOutputPixel( rHEvt.GetMousePosPixel() ), aDate ) )
{
tools::Rectangle aDateRect = GetDateRect( aDate );
Point aPt = OutputToScreenPixel( aDateRect.TopLeft() );
aDateRect.SetLeft( aPt.X() );
aDateRect.SetTop( aPt.Y() );
aPt = OutputToScreenPixel( aDateRect.BottomRight() );
aDateRect.SetRight( aPt.X() );
aDateRect.SetBottom( aPt.Y() );
if ( rHEvt.GetMode() & HelpEventMode::QUICK )
{
maCalendarWrapper.setGregorianDateTime( aDate);
sal_uInt16 nWeek = static_cast<sal_uInt16>(maCalendarWrapper.getValue( i18n::CalendarFieldIndex::WEEK_OF_YEAR));
sal_uInt16 nMonth = aDate.GetMonth();
OUString aStr = maDayText
+ ": "
+ OUString::number(aDate.GetDayOfYear())
+ " / "
+ maWeekText
+ ": "
+ OUString::number(nWeek);
// if year is not the same, add it
if ( (nMonth == 12) && (nWeek == 1) )
{
aStr += ", " + OUString::number(aDate.GetNextYear());
}
else if ( (nMonth == 1) && (nWeek > 50) )
{
aStr += ", " + OUString::number(aDate.GetYear()-1);
}
Help::ShowQuickHelp( this, aDateRect, aStr );
return;
}
}
}
Control::RequestHelp( rHEvt );
}
void Calendar::Command( const CommandEvent& rCEvt )
{
if ( rCEvt.GetCommand() == CommandEventId::ContextMenu )
{
if ( !mbSelection && rCEvt.IsMouseEvent() )
{
Date aTempDate = maCurDate;
sal_uInt16 nHitTest = ImplHitTest( rCEvt.GetMousePosPixel(), aTempDate );
if ( nHitTest & CALENDAR_HITTEST_MONTHTITLE )
{
ImplShowMenu( rCEvt.GetMousePosPixel(), aTempDate );
return;
}
}
}
else if ( rCEvt.GetCommand() == CommandEventId::Wheel )
{
const CommandWheelData* pData = rCEvt.GetWheelData();
if ( pData->GetMode() == CommandWheelMode::SCROLL )
{
long nNotchDelta = pData->GetNotchDelta();
if ( nNotchDelta < 0 )
{
while ( nNotchDelta < 0 )
{
ImplScroll( true );
nNotchDelta++;
}
}
else
{
while ( nNotchDelta > 0 )
{
ImplScroll( false );
nNotchDelta--;
}
}
return;
}
}
Control::Command( rCEvt );
}
void Calendar::StateChanged( StateChangedType nType )
{
Control::StateChanged( nType );
if ( nType == StateChangedType::InitShow )
ImplFormat();
}
void Calendar::DataChanged( const DataChangedEvent& rDCEvt )
{
Control::DataChanged( rDCEvt );
if ( (rDCEvt.GetType() == DataChangedEventType::FONTS) ||
(rDCEvt.GetType() == DataChangedEventType::FONTSUBSTITUTION) ||
((rDCEvt.GetType() == DataChangedEventType::SETTINGS) &&
(rDCEvt.GetFlags() & AllSettingsFlags::STYLE)) )
{
ImplInitSettings();
Invalidate();
}
}
void Calendar::Select()
{
maSelectHdl.Call( this );
}
Date Calendar::GetFirstSelectedDate() const
{
if ( !mpSelectTable->empty() )
return Date( *mpSelectTable->begin() );
else
{
Date aDate( 0, 0, 0 );
return aDate;
}
}
void Calendar::SetCurDate( const Date& rNewDate )
{
if ( !rNewDate.IsValidAndGregorian() )
return;
if ( maCurDate == rNewDate )
return;
bool bUpdate = IsVisible() && IsUpdateMode();
Date aOldDate = maCurDate;
maCurDate = rNewDate;
ImplCalendarSelectDate( mpSelectTable.get(), aOldDate, false );
ImplCalendarSelectDate( mpSelectTable.get(), maCurDate, true );
// shift actual date in the visible area
if ( mbFormat || (maCurDate < GetFirstMonth()) )
SetFirstDate( maCurDate );
else if ( maCurDate > GetLastMonth() )
{
Date aTempDate = GetLastMonth();
long nDateOff = maCurDate-aTempDate;
if ( nDateOff < 365 )
{
Date aFirstDate = GetFirstMonth();
aFirstDate.AddDays( aFirstDate.GetDaysInMonth() );
++aTempDate;
while ( nDateOff > aTempDate.GetDaysInMonth() )
{
aFirstDate.AddDays( aFirstDate.GetDaysInMonth() );
sal_Int32 nDaysInMonth = aTempDate.GetDaysInMonth();
aTempDate.AddDays( nDaysInMonth );
nDateOff -= nDaysInMonth;
}
SetFirstDate( aFirstDate );
}
else
SetFirstDate( maCurDate );
}
else
{
if ( bUpdate )
{
HideFocus();
ImplUpdateDate( aOldDate );
ImplUpdateDate( maCurDate );
}
}
}
void Calendar::SetFirstDate( const Date& rNewFirstDate )
{
if ( maFirstDate != rNewFirstDate )
{
maFirstDate = Date( 1, rNewFirstDate.GetMonth(), rNewFirstDate.GetYear() );
ImplUpdate();
}
}
Date Calendar::GetFirstMonth() const
{
if ( maFirstDate.GetDay() > 1 )
{
if ( maFirstDate.GetMonth() == 12 )
return Date( 1, 1, maFirstDate.GetNextYear() );
else
return Date( 1, maFirstDate.GetMonth()+1, maFirstDate.GetYear() );
}
else
return maFirstDate;
}
Date Calendar::GetLastMonth() const
{
Date aDate = GetFirstMonth();
sal_uInt16 nMonthCount = GetMonthCount();
for ( sal_uInt16 i = 0; i < nMonthCount; i++ )
aDate.AddDays( aDate.GetDaysInMonth() );
--aDate;
return aDate;
}
sal_uInt16 Calendar::GetMonthCount() const
{
if ( mbFormat )
return 1;
else
return static_cast<sal_uInt16>(mnMonthPerLine*mnLines);
}
bool Calendar::GetDate( const Point& rPos, Date& rDate ) const
{
Date aDate = maCurDate;
sal_uInt16 nHitTest = ImplHitTest( rPos, aDate );
if ( nHitTest & CALENDAR_HITTEST_DAY )
{
rDate = aDate;
return true;
}
else
return false;
}
tools::Rectangle Calendar::GetDateRect( const Date& rDate ) const
{
tools::Rectangle aRect;
if ( mbFormat || (rDate < maFirstDate) || (rDate > (maFirstDate+mnDayCount)) )
return aRect;
long nX;
long nY;
sal_Int32 nDaysOff;
sal_uInt16 nDayIndex;
Date aDate = GetFirstMonth();
if ( rDate < aDate )
{
aRect = GetDateRect( aDate );
nDaysOff = aDate-rDate;
nX = nDaysOff*mnDayWidth;
aRect.AdjustLeft( -nX );
aRect.AdjustRight( -nX );
return aRect;
}
else
{
Date aLastDate = GetLastMonth();
if ( rDate > aLastDate )
{
sal_Int32 nWeekDay = static_cast<sal_Int32>(aLastDate.GetDayOfWeek());
nWeekDay = (nWeekDay+(7-ImplGetWeekStart())) % 7;
aLastDate.AddDays( -nWeekDay );
aRect = GetDateRect( aLastDate );
nDaysOff = rDate-aLastDate;
nDayIndex = 0;
for ( sal_Int32 i = 0; i <= nDaysOff; i++ )
{
if ( aLastDate == rDate )
{
aRect.AdjustLeft(nDayIndex*mnDayWidth );
aRect.SetRight( aRect.Left()+mnDayWidth );
return aRect;
}
if ( nDayIndex == 6 )
{
nDayIndex = 0;
aRect.AdjustTop(mnDayHeight );
aRect.AdjustBottom(mnDayHeight );
}
else
nDayIndex++;
++aLastDate;
}
}
}
nY = 0;
for ( long i = 0; i < mnLines; i++ )
{
nX = 0;
for ( long j = 0; j < mnMonthPerLine; j++ )
{
sal_uInt16 nDaysInMonth = aDate.GetDaysInMonth();
// month is called
if ( (aDate.GetMonth() == rDate.GetMonth()) &&
(aDate.GetYear() == rDate.GetYear()) )
{
long nDayX = nX+mnDaysOffX;
long nDayY = nY+mnDaysOffY;
nDayIndex = static_cast<sal_uInt16>(aDate.GetDayOfWeek());
nDayIndex = (nDayIndex+(7-static_cast<sal_uInt16>(ImplGetWeekStart()))) % 7;
for ( sal_uInt16 nDay = 1; nDay <= nDaysInMonth; nDay++ )
{
if ( nDay == rDate.GetDay() )
{
aRect.SetLeft( nDayX + (nDayIndex*mnDayWidth) );
aRect.SetTop( nDayY );
aRect.SetRight( aRect.Left()+mnDayWidth );
aRect.SetBottom( aRect.Top()+mnDayHeight );
break;
}
if ( nDayIndex == 6 )
{
nDayIndex = 0;
nDayY += mnDayHeight;
}
else
nDayIndex++;
}
}
aDate.AddDays( nDaysInMonth );
nX += mnMonthWidth;
}
nY += mnMonthHeight;
}
return aRect;
}
void Calendar::StartSelection()
{
maOldCurDate = maCurDate;
mpOldSelectTable.reset(new IntDateSet( *mpSelectTable ));
mbSelection = true;
}
void Calendar::EndSelection()
{
if ( mbDrag || mbSpinDown || mbSelection )
{
if ( !mbSelection )
ReleaseMouse();
mbDrag = false;
mbSelection = false;
mbSpinDown = false;
mbPrevIn = false;
mbNextIn = false;
}
}
Size Calendar::CalcWindowSizePixel() const
{
OUString const a99Text("99");
Size aSize;
long n99TextWidth = GetTextWidth( a99Text );
long nTextHeight = GetTextHeight();
aSize.AdjustWidth((n99TextWidth+DAY_OFFX)*7);
aSize.AdjustWidth(MONTH_BORDERX*2 );
aSize.setHeight( nTextHeight + TITLE_OFFY + (TITLE_BORDERY*2) );
aSize.AdjustHeight(nTextHeight + WEEKDAY_OFFY );
aSize.AdjustHeight((nTextHeight+DAY_OFFY)*6);
aSize.AdjustHeight(MONTH_OFFY );
return aSize;
}
Size Calendar::GetOptimalSize() const
{
return CalcWindowSizePixel();
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|