File: calendar.py

package info (click to toggle)
wxpython4.0 4.0.4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 211,112 kB
  • sloc: cpp: 888,355; python: 223,130; makefile: 52,087; ansic: 45,780; sh: 3,012; xml: 1,534; perl: 264
file content (1563 lines) | stat: -rw-r--r-- 47,369 bytes parent folder | download
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
# -*- coding: utf-8 -*-
# #----------------------------------------------------------------------------
# Name:         calendar.py
# Purpose:      Calendar display control
#
# Author:       Lorne White (email: lorne.white@telusplanet.net)
#
# Created:
# Version:      0.92
# Date:         Nov 26, 2001
# Licence:      wxWindows license
# Tags:         phoenix-port, documented, unittest, py3-port
#----------------------------------------------------------------------------
# 12/01/2003 - Jeff Grimmett (grimmtooth@softhome.net)
#
# o Updated for wx namespace
# o Tested with updated demo
# o Added new event type EVT_CALENDAR. The reason for this is that the original
#   library used a hardcoded ID of 2100 for generating events. This makes it
#   very difficult to fathom when trying to decode the code since there's no
#   published API. Creating the new event binder might seem like overkill -
#   after all, you might ask, why not just use a new event ID and be done with
#   it? However, a consistent interface is very useful at times; also it makes
#   it clear that we're not just hunting for mouse clicks -- we're hunting
#   wabbit^H^H^H^H (sorry bout that) for calender-driven mouse clicks. So
#   that's my sad story. Shoot me if you must :-)
# o There's still one deprecation warning buried in here somewhere, but I
#   haven't been able to find it yet. It only occurs when displaying a
#   print preview, and only the first time. It *could* be an error in the
#   demo, I suppose.
#
#   Here's the traceback:
#
#   C:\Python\lib\site-packages\wx\core.py:949: DeprecationWarning:
#       integer argument expected, got float
#   newobj = _core.new_Rect(*args, **kwargs)
#
# 12/17/2003 - Jeff Grimmett (grimmtooth@softhome.net)
#
# o A few style-guide nips and tucks
# o Renamed wxCalendar to Calendar
# o Couple of bugfixes
#
# 06/02/2004 - Joerg "Adi" Sieker adi@sieker.info
#
# o Changed color handling, use dictionary instead of members.
#   This causes all color changes to be ignored if they manipluate the members directly.
#   SetWeekColor and other method color methods were adapted to use the new dictionary.
# o Added COLOR_* constants
# o Added SetColor method for Calendar class
# o Added 3D look of week header
# o Added colors for 3D look of header
# o Fixed width calculation.
#   Because of rounding difference the total width and height of the
#   calendar could be up to 6 pixels to small. The last column and row
#   are now wider/taller by the missing amount.
# o Added SetTextAlign method to wxCalendar. This exposes logic
#   which was already there.
# o Fixed CalDraw.SetMarg which set set_x_st and set_y_st which don't get used anywhere.
#   Instead set set_x_mrg and set_y_mrg
# o Changed default X and Y Margin to 0.
# o Added wxCalendar.SetMargin.
#
# 17/03/2004 - Joerg "Adi" Sieker adi@sieker.info
# o Added keyboard navigation to the control.
#   Use the cursor keys to navigate through the ages. :)
#   The Home key function as go to today
# o select day is now a filled rect instead of just an outline
#
# 15/04/2005 - Joe "shmengie" Brown joebrown@podiatryfl.com
# o Adjusted spin control size/placement (On Windows ctrls were overlapping).
# o Set Ok/Cancel buttons to wx.ID_OK & wx.ID_CANCEL to provide default dialog
#   behaviour.
# o If no date has been clicked clicked, OnOk set the result to calend's date,
#   important if keyboard only navigation is used.
#
# 12/10/2006 - Walter Barnes walter_barnes05@yahoo.com
# o Fixed CalDraw to properly render months that start on a Sunday.
#
# 21/10/2006 - Walter Barnes walter_barnes05@yahoo.com
# o Fixed a bug in Calendar: Shift and Control key status was only recorded for
#   left-down events.
# o Added handlers for wxEVT_MIDDLE_DOWN and wxEVT_MIDDLE_DCLICK to generate
#   EVT_CALENDAR for these mouse events.
"""A module with a calendar control and a calendar dialog and some utility
methods and classes.
"""

import wx
_ = wx.GetTranslation

from .CDate import *

CalDays = [6, 0, 1, 2, 3, 4, 5]
AbrWeekday = {6: _("Sun"), 0: _("Mon"), 1: _("Tue"), 2: _("Wed"), 3: _("Thu"),
              4: _("Fri"), 5: _("Sat")}
_MIDSIZE = 180

COLOR_GRID_LINES = "grid_lines"
COLOR_BACKGROUND = "background"
COLOR_SELECTION_FONT = "selection_font"
COLOR_SELECTION_BACKGROUND = "selection_background"
COLOR_BORDER = "border"
COLOR_HEADER_BACKGROUND = "header_background"
COLOR_HEADER_FONT = "header_font"
COLOR_WEEKEND_BACKGROUND = "weekend_background"
COLOR_WEEKEND_FONT = "weekend_font"
COLOR_FONT = "font"
COLOR_3D_LIGHT = "3d_light"
COLOR_3D_DARK = "3d_dark"
COLOR_HIGHLIGHT_FONT = "highlight_font"
COLOR_HIGHLIGHT_BACKGROUND = "highlight_background"

BusCalDays = [0, 1, 2, 3, 4, 5, 6]

# Calendar click event - added 12/1/03 by jmg (see above)
wxEVT_COMMAND_PYCALENDAR_DAY_CLICKED = wx.NewEventType()
EVT_CALENDAR = wx.PyEventBinder(wxEVT_COMMAND_PYCALENDAR_DAY_CLICKED, 1)


def GetMonthList():
    """
    Get a list of the defined month names.

    rtype: `list`
    """

    monthlist = []
    for i in range(13):
        name = Month[i]
        if name is not None:
            monthlist.append(name)
    return monthlist


def MakeColor(in_color):
    """
    Try and create a :class:`wx.Colour`.

    :returns: a :class:`wx.Colour` instance to `in_colour`
    """
    try:
        color = wx.Colour(in_color)
    except Exception:
        color = in_color
    return color


def DefaultColors():
    """Define some default colors."""
    colors = {}
    colors[COLOR_GRID_LINES] = 'BLACK'
    colors[COLOR_BACKGROUND] = 'WHITE'
    colors[COLOR_SELECTION_FONT] = wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT)
    colors[COLOR_SELECTION_BACKGROUND] = wx.Colour(255, 255, 225)
    colors[COLOR_BORDER] = 'BLACK'
    colors[COLOR_HEADER_BACKGROUND] = wx.SystemSettings.GetColour(wx.SYS_COLOUR_3DFACE)
    colors[COLOR_HEADER_FONT] = wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT)
    colors[COLOR_WEEKEND_BACKGROUND] = 'LIGHT GREY'
    colors[COLOR_WEEKEND_FONT] = wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT)
    colors[COLOR_FONT] = wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT)
    colors[COLOR_3D_LIGHT] = wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNHIGHLIGHT)
    colors[COLOR_3D_DARK] = wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNSHADOW)
    colors[COLOR_HIGHLIGHT_FONT] = wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT)
    colors[COLOR_HIGHLIGHT_BACKGROUND] = wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHT)
    return colors


class CalDraw:
    """A class to draw a calendar."""
    def __init__(self, parent):
        """
        Default class constructor

        :param wx.Window `parent`: parent window.

        """
        self.pwidth = 1
        self.pheight = 1
        try:
            self.scale = parent.scale
        except Exception:
            self.scale = 1

        self.gridx = []
        self.gridy = []

        self.DefParms()

    def DefParms(self):
        """Setup the default parameters."""
        self.num_auto = True    # auto scale of the cal number day size
        self.num_size = 12      # default size of calendar if no auto size
        self.max_num_size = 12  # maximum size for calendar number

        self.num_align_horz = wx.ALIGN_CENTRE    # alignment of numbers
        self.num_align_vert = wx.ALIGN_CENTRE
        self.num_indent_horz = 0     # points indent from position, used to offset if not centered
        self.num_indent_vert = 0

        self.week_auto = True       # auto scale of week font text
        self.week_size = 10
        self.max_week_size = 12

        self.colors = DefaultColors()

        self.fontfamily = wx.FONTFAMILY_SWISS
        self.fontstyle = wx.FONTSTYLE_NORMAL
        self.fontweight = wx.FONTWEIGHT_NORMAL

        self.hide_title = False
        self.hide_grid = False
        self.outer_border = True

        self.title_offset = 0
        self.cal_week_scale = 0.7
        self.show_weekend = False
        self.cal_type = "NORMAL"

    def SetWeekColor(self, font_color, week_color):
        """
        Set the font and background color of the week title.

        :param `font_color`: the font color, a value as is accepted by :class:`wx.Colour`
        :param `week_color`: the week color, a value as is accepted by :class:`wx.Colour`
        """
        self.colors[COLOR_HEADER_FONT] = MakeColor(font_color)
        self.colors[COLOR_HEADER_BACKGROUND] = MakeColor(week_color)
        self.colors[COLOR_3D_LIGHT] = MakeColor(week_color)
        self.colors[COLOR_3D_DARK] = MakeColor(week_color)

    def SetSize(self, size):
        """
        Set the size.

        :param `size`: a tuple/list with width and height

        """
        self.set_sizew = size[0]
        self.set_sizeh = size[1]

    def InitValues(self):
        """Default dimensions of various elements of the calendar."""
        self.rg = {}
        self.cal_sel = {}
        self.set_cy_st = 0      # start position
        self.set_cx_st = 0

        self.set_y_mrg = 1      # start of vertical draw default
        self.set_x_mrg = 1
        self.set_y_end = 1

    def SetPos(self, xpos, ypos):
        """
        Set the position.

        :param int `xpos`: the x position
        :param int `ypos`: the y position

        """
        self.set_cx_st = xpos
        self.set_cy_st = ypos

    def SetMarg(self, xmarg, ymarg):
        """
        Set the margins.

        :param `xmarg`: the x margin
        :param `ymarg`: the y margin, also used for the end margin

        """
        self.set_x_mrg = xmarg
        self.set_y_mrg = ymarg
        self.set_y_end = ymarg

    def InitScale(self):
        """Set the default scale values."""
        self.sizew = int(self.set_sizew * self.pwidth)
        self.sizeh = int(self.set_sizeh * self.pheight)

        self.cx_st = int(self.set_cx_st * self.pwidth)       # draw start position
        self.cy_st = int(self.set_cy_st * self.pheight)

        self.x_mrg = int(self.set_x_mrg * self.pwidth)         # calendar draw margins
        self.y_mrg = int(self.set_y_mrg * self.pheight)
        self.y_end = int(self.set_y_end * self.pheight)

    def DrawCal(self, DC, sel_lst=[]):
        """
        Draw the calendar.

        :param `DC`: the :class:`wx.DC` to use to draw upon.
        :param `sel_list`: a list of days to override the weekend highlight.
        """
        self.InitScale()

        self.DrawBorder(DC)

        if self.hide_title is False:
            self.DrawMonth(DC)

        self.Center()

        self.DrawGrid(DC)
        self.GetRect()
        if self.show_weekend is True:       # highlight weekend dates
            self.SetWeekEnd()

        self.AddSelect(sel_lst)     # overrides the weekend highlight

        self.DrawSel(DC)      # highlighted days
        self.DrawWeek(DC)
        self.DrawNum(DC)

    def AddSelect(self, list, cfont=None, cbackgrd=None):
        """
        Add a selection of days.

        :param `list`: a list of days to select
        :param `cfont`: the font color to use
        :param `cbackgrd`: the background color to use

        """
        if cfont is None:
            cfont = self.colors[COLOR_SELECTION_FONT]      # font digit color
        if cbackgrd is None:
            cbackgrd = self.colors[COLOR_SELECTION_BACKGROUND]     # select background color

        for val in list:
            self.cal_sel[val] = (cfont, cbackgrd)

    def DrawBorder(self, DC, transparent=False):
        """
        Draw a border around the outside of the main display rectangle.

        :param `DC`: the :class:`wx.DC` to use
        :param `transparent`: use a transparent brush, default is ``False``.

        """
        if self.outer_border is True:
            if transparent is False:
                brush = wx.Brush(MakeColor(self.colors[COLOR_BACKGROUND]), wx.BRUSHSTYLE_SOLID)
            else:
                brush = wx.TRANSPARENT_BRUSH
            DC.SetBrush(brush)
            DC.SetPen(wx.Pen(MakeColor(self.colors[COLOR_BORDER])))
            # full display window area
            rect = wx.Rect(self.cx_st, self.cy_st, self.sizew, self.sizeh)
            DC.DrawRectangle(rect)

    def DrawFocusIndicator(self, DC):
        """
        Draw the focus indicator

        :param `DC`: the :class:`wx.DC` to use

        """
        if self.outer_border is True:
            DC.SetBrush(wx.TRANSPARENT_BRUSH)
            DC.SetPen(wx.Pen(MakeColor(self.colors[COLOR_HIGHLIGHT_BACKGROUND]), style=wx.PENSTYLE_DOT))
            # full display window area
            rect = wx.Rect(self.cx_st, self.cy_st, self.sizew, self.sizeh)
            DC.DrawRectangle(rect)

    def DrawNumVal(self):
        """Draw the numeric values."""
        self.DrawNum()

    def SetCal(self, year, month):
        """
        Calculate the calendar days and offset position.

        :param int `year`: the year to calculate.
        :param int `month`: the month to calculate.

        """
        self.InitValues()       # reset initial values

        self.year = year
        self.month = month

        day = 1
        t = Date(year, month, day)
        dow = self.dow = t.day_of_week     # start day in month
        dim = self.dim = t.days_in_month   # number of days in month

        if self.cal_type == "NORMAL":
            start_pos = dow + 1
        else:
            start_pos = dow

        if start_pos > 6:
            start_pos = 0

        self.st_pos = start_pos

        self.cal_days = []
        for i in range(start_pos):
            self.cal_days.append('')

        i = 1
        while i <= dim:
            self.cal_days.append(str(i))
            i = i + 1

        self.end_pos = start_pos + dim - 1

        return start_pos

    def SetWeekEnd(self, font_color=None, backgrd=None):
        """
        Set the weekend backgrounds.

        :param `font_color`: the font color to use, if ``None`` the default is used.
        :param `backgrd`: the background color to use, if ``None`` the default is used.

        """
        if font_color is not None:
            self.SetColor(COLOR_WEEKEND_FONT, MakeColor(font_color))
        if backgrd is not None:
            self.SetColor(COLOR_WEEKEND_BACKGROUND, MakeColor(backgrd))

        date = 6 - int(self.dow)     # start day of first saturday
        if date == 0:                # ...unless we start on Sunday
            self.cal_sel[1] = (self.GetColor(COLOR_WEEKEND_FONT), self.GetColor(COLOR_WEEKEND_BACKGROUND))
            date = 7

        while date <= self.dim:
            self.cal_sel[date] = (self.GetColor(COLOR_WEEKEND_FONT), self.GetColor(COLOR_WEEKEND_BACKGROUND))  # Saturday
            date = date + 1

            if date <= self.dim:
                self.cal_sel[date] = (self.GetColor(COLOR_WEEKEND_FONT), self.GetColor(COLOR_WEEKEND_BACKGROUND))      # Sunday
                date = date + 6
            else:
                date = date + 7

    def GetRect(self):
        """Get the display rectange list of the day grid."""
        cnt = 0
        h = 0
        w = 0
        for y in self.gridy[1:-1]:
            if y == self.gridy[-2]:
                h = h + self.restH

            for x in self.gridx[:-1]:
                assert type(y) == int
                assert type(x) == int

                w = self.cellW
                h = self.cellH

                if x == self.gridx[-2]:
                    w = w + self.restW

                rect = wx.Rect(x, y, w + 1, h + 1)  # create rect region

                self.rg[cnt] = rect
                cnt = cnt + 1

        return self.rg

    def GetCal(self):
        """Get the calendar days."""
        return self.cal_days

    def GetOffset(self):
        """Get the offset position."""
        return self.st_pos

    def DrawMonth(self, DC):
        """
        Draw the month and year titles.

        :param `DC`: the :class:`wx.DC` to use.

        """
        month = Month[self.month]

        sizef = 11
        if self.sizeh < _MIDSIZE:
            sizef = 10

        f = wx.Font(sizef, self.fontfamily, self.fontstyle, self.fontweight)
        DC.SetFont(f)

        tw, th = DC.GetTextExtent(month)
        adjust = self.cx_st + (self.sizew - tw) / 2
        DC.DrawText(month, adjust, self.cy_st + th)

        year = str(self.year)
        tw, th = DC.GetTextExtent(year)
        adjust = self.sizew - tw - self.x_mrg

        self.title_offset = th * 2

        f = wx.Font(sizef, self.fontfamily, self.fontstyle, self.fontweight)
        DC.SetFont(f)
        DC.DrawText(year, self.cx_st + adjust, self.cy_st + th)

    def DrawWeek(self, DC):
        """
        Draw the week days.

        :param `DC`: the :class:`wx.DC` to use.

        """
        # increase by 1 to include all gridlines
        width = self.gridx[1] - self.gridx[0] + 1
        height = self.gridy[1] - self.gridy[0] + 1
        rect_w = self.gridx[-1] - self.gridx[0]

        f = wx.Font(10, self.fontfamily, self.fontstyle, self.fontweight)  # initial font setting

        if self.week_auto is True:
            test_size = self.max_week_size      # max size
            test_day = ' Sun '
            while test_size > 2:
                f.SetPointSize(test_size)
                DC.SetFont(f)
                tw, th = DC.GetTextExtent(test_day)

                if tw < width and th < height:
                    break

                test_size = test_size - 1
        else:
            f.SetPointSize(self.week_size)   # set fixed size
            DC.SetFont(f)

        DC.SetTextForeground(MakeColor(self.colors[COLOR_HEADER_FONT]))

        cnt_x = 0
        cnt_y = 0

        brush = wx.Brush(MakeColor(self.colors[COLOR_HEADER_BACKGROUND]), wx.BRUSHSTYLE_SOLID)
        DC.SetBrush(brush)

        if self.cal_type == "NORMAL":
            cal_days = CalDays
        else:
            cal_days = BusCalDays

        for val in cal_days:
            if val == cal_days[-1]:
                width = width + self.restW

            day = AbrWeekday[val]

            if self.sizew < 200:
                day = day[0]

            dw, dh = DC.GetTextExtent(day)

            diffx = (width - dw) / 2
            diffy = (height - dh) / 2

            x = self.gridx[cnt_x]
            y = self.gridy[cnt_y]
            pointXY = (x, y)
            pointWH = (width, height)
            if self.hide_grid is False:
                pen = wx.Pen(MakeColor(self.GetColor(COLOR_GRID_LINES)), 1, wx.PENSTYLE_SOLID)
            else:
                pen = wx.Pen(MakeColor(self.GetColor(COLOR_BACKGROUND)), 1, wx.PENSTYLE_SOLID)
            DC.SetPen(pen)
            DC.DrawRectangle(pointXY, pointWH)

            old_pen = DC.GetPen()

            pen = wx.Pen(MakeColor(self.colors[COLOR_3D_LIGHT]), 1, wx.PENSTYLE_SOLID)
            DC.SetPen(pen)
            # draw the horizontal hilight
            startPoint = wx.Point(x + 1, y + 1)
            endPoint = wx.Point(x + width - 1, y + 1)
            DC.DrawLine(startPoint, endPoint)

            # draw the vertical hilight
            startPoint = wx.Point(x + 1, y + 1)
            endPoint = wx.Point(x + 1, y + height - 2)
            DC.DrawLine(startPoint, endPoint)

            pen = wx.Pen(MakeColor(self.colors[COLOR_3D_DARK]), 1, wx.PENSTYLE_SOLID)
            DC.SetPen(pen)

            # draw the horizontal lowlight
            startPoint = wx.Point(x + 1, y + height - 2)
            endPoint = wx.Point(x + width - 1, y + height - 2)
            DC.DrawLine(startPoint, endPoint)

            # draw the vertical lowlight
            startPoint = wx.Point(x + width - 2, y + 2)
            endPoint = wx.Point(x + width - 2, y + height - 2)
            DC.DrawLine(startPoint, endPoint)

            pen = wx.Pen(MakeColor(self.colors[COLOR_FONT]), 1, wx.PENSTYLE_SOLID)

            DC.SetPen(pen)

            point = (x + diffx, y + diffy)
            DC.DrawText(day, point)
            cnt_x = cnt_x + 1

    def _CalcFontSize(self, DC, f):
        if self.num_auto is True:
            test_size = self.max_num_size      # max size
            test_day = ' 99 '

            while test_size > 2:
                f.SetPointSize(test_size)
                DC.SetFont(f)
                tw, th = DC.GetTextExtent(test_day)

                if tw < self.cellW and th < self.cellH:
                    sizef = test_size
                    break
                test_size = test_size - 1
        else:
            f.SetPointSize(self.num_size)   # set fixed size
            DC.SetFont(f)

    def DrawNum(self, DC):
        """
        Draw the day numbers

        :param `DC`: the :class:`wx.DC` to use.

        """
        f = wx.Font(10, self.fontfamily, self.fontstyle, self.fontweight)  # initial font setting
        self._CalcFontSize(DC, f)

        cnt_x = 0
        cnt_y = 1
        for val in self.cal_days:
            x = self.gridx[cnt_x]
            y = self.gridy[cnt_y]

            self._DrawDayText(x, y, val, f, DC)

            if cnt_x < 6:
                cnt_x = cnt_x + 1
            else:
                cnt_x = 0
                cnt_y = cnt_y + 1

    def _DrawDayText(self, x, y, text, font, DC):

        try:
            num_val = int(text)
            num_color = self.cal_sel[num_val][0]
        except Exception:
            num_color = self.colors[COLOR_FONT]

        DC.SetTextForeground(MakeColor(num_color))
        DC.SetFont(font)

        tw, th = DC.GetTextExtent(text)

        if self.num_align_horz == wx.ALIGN_CENTRE:
            adj_h = (self.cellW - tw) / 2
        elif self.num_align_horz == wx.ALIGN_RIGHT:
            adj_h = self.cellW - tw
        else:
            adj_h = 0   # left alignment

        adj_h = adj_h + self.num_indent_horz

        if self.num_align_vert == wx.ALIGN_CENTRE:
            adj_v = (self.cellH - th) / 2
        elif self.num_align_vert == wx.ALIGN_BOTTOM:
            adj_v = self.cellH - th
        else:
            adj_v = 0   # left alignment

        adj_v = adj_v + self.num_indent_vert

        DC.DrawText(text, (x + adj_h, y + adj_v))

    def DrawDayText(self, DC, key):
        """
        Draw the day text.

        :param `DC`: the :class:`wx.DC` to use.
        :param `key`: the day to draw

        """

        f = wx.Font(10, self.fontfamily, self.fontstyle, self.fontweight)  # initial font setting
        self._CalcFontSize(DC, f)

        if key > self.end_pos:
            key = self.end_pos

        val = self.cal_days[key]
        cnt_x = key % 7
        cnt_y = int(key / 7) + 1
        x = self.gridx[cnt_x]
        y = self.gridy[cnt_y]
        self._DrawDayText(x, y, val, f, DC)

    def Center(self):
        """Calculate the dimensions in the center of the drawing area."""
        borderW = self.x_mrg * 2
        borderH = self.y_mrg + self.y_end + self.title_offset

        self.cellW = int((self.sizew - borderW) / 7)
        self.cellH = int((self.sizeh - borderH) / 7)

        self.restW = ((self.sizew - borderW) % 7) - 1

        # week title adjustment
        self.weekHdrCellH = int(self.cellH * self.cal_week_scale)
        # recalculate the cell height exkl. the week header and
        # subtracting the size
        self.cellH = int((self.sizeh - borderH - self.weekHdrCellH) / 6)

        self.restH = ((self.sizeh - borderH - self.weekHdrCellH) % 6) - 1
        self.calW = self.cellW * 7
        self.calH = self.cellH * 6 + self.weekHdrCellH

    # highlighted selected days
    def DrawSel(self, DC):
        """
        Highlight selected days.

        :param `DC`: the :class:`wx.DC` to use

        """
        for key in self.cal_sel.keys():
            sel_color = self.cal_sel[key][1]
            brush = wx.Brush(MakeColor(sel_color), wx.BRUSHSTYLE_SOLID)
            DC.SetBrush(brush)

            if self.hide_grid is False:
                DC.SetPen(wx.Pen(MakeColor(self.colors[COLOR_GRID_LINES]), 0))
            else:
                DC.SetPen(wx.Pen(MakeColor(self.colors[COLOR_BACKGROUND]), 0))

            nkey = key + self.st_pos - 1
            rect = self.rg[nkey]

            DC.DrawRectangle(rect)

    def DrawGrid(self, DC):
        """
        Calculate and draw the grid lines.

        :param `DC`: the :class:`wx.DC` to use

        """
        DC.SetPen(wx.Pen(MakeColor(self.colors[COLOR_GRID_LINES]), 0))

        self.gridx = []
        self.gridy = []

        self.x_st = self.cx_st + self.x_mrg
        # start postion of draw
        self.y_st = self.cy_st + self.y_mrg + self.title_offset

        x1 = self.x_st
        y1 = self.y_st
        y2 = y1 + self.calH + self.restH

        for i in range(8):
            if i == 7:
                x1 = x1 + self.restW

            if self.hide_grid is False:
                DC.DrawLine((x1, y1), (x1, y2))

            self.gridx.append(x1)

            x1 = x1 + self.cellW

        x1 = self.x_st
        y1 = self.y_st
        x2 = x1 + self.calW + self.restW

        for i in range(8):
            if i == 7:
                y1 = y1 + self.restH

            if self.hide_grid is False:
                DC.DrawLine((x1, y1), (x2, y1))

            self.gridy.append(y1)

            if i == 0:
                y1 = y1 + self.weekHdrCellH
            else:
                y1 = y1 + self.cellH

    def GetColor(self, name):
        """
        Get a color.

        :param `name`: one of the defined color names.

        """
        return MakeColor(self.colors[name])

    def SetColor(self, name, value):
        """
        Set a color.

        :param `name`: the name to assign the color too.
        :param `value`: the color to use, see :class:`wx.Colour`

        """
        self.colors[name] = MakeColor(value)


class PrtCalDraw(CalDraw):
    """A class to optimize :class:`CalDraw` for printing."""
    def InitValues(self):
        """Set initial values."""
        self.rg = {}
        self.cal_sel = {}
        # start draw border location
        self.set_cx_st = 1.0
        self.set_cy_st = 1.0

        # draw offset position
        self.set_y_mrg = 0.2
        self.set_x_mrg = 0.2
        self.set_y_end = 0.2

    def SetPSize(self, pwidth, pheight):
        """Calculate the dimensions in the center of the drawing area."""
        self.pwidth = int(pwidth) / self.scale
        self.pheight = int(pheight) / self.scale

    def SetPreview(self, preview):
        """
        Set the preview.

        :param `preview`: set the preview state???

        """
        self.preview = preview


class Calendar(wx.Control):
    """A calendar control class."""
    def __init__(self, parent, id=-1, pos=wx.DefaultPosition,
                 size=wx.Size(200, 200), style=0, validator=wx.DefaultValidator,
                 name="calendar"):
        """
        Default class constructor.

        :param wx.Window `parent`: parent window. Must not be ``None``;
        :param integer `id`: window identifier. A value of -1 indicates a default value;
        :param `pos`: the control position. A value of (-1, -1) indicates a default position,
         chosen by either the windowing system or wxPython, depending on platform;
        :type `pos`: tuple or :class:`wx.Point`
        :param `size`: the control size. A value of (-1, -1) indicates a default size,
         chosen by either the windowing system or wxPython, depending on platform;
        :type `size`: tuple or :class:`wx.Size`
        :param integer `style`: the button style (unused);
        :param wx.Validator `validator`: the validator associated to the button;
        :param string `name`: the calendar name.

        """
        wx.Control.__init__(self, parent, id, pos, size, style | wx.WANTS_CHARS, validator, name)

        self.hasFocus = False
        # set the calendar control attributes

        self.hide_grid = False
        self.hide_title = False
        self.show_weekend = False
        self.cal_type = "NORMAL"
        self.outer_border = True
        self.num_align_horz = wx.ALIGN_CENTRE
        self.num_align_vert = wx.ALIGN_CENTRE
        self.colors = DefaultColors()
        self.set_x_mrg = 1
        self.set_y_mrg = 1
        self.set_y_end = 1

        self.select_list = []

        self.SetBackgroundColour(MakeColor(self.colors[COLOR_BACKGROUND]))
        self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftEvent)
        self.Bind(wx.EVT_LEFT_DCLICK, self.OnLeftDEvent)
        self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightEvent)
        self.Bind(wx.EVT_RIGHT_DCLICK, self.OnRightDEvent)
        self.Bind(wx.EVT_MIDDLE_DOWN, self.OnMiddleEvent)
        self.Bind(wx.EVT_MIDDLE_DCLICK, self.OnMiddleDEvent)
        self.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus)
        self.Bind(wx.EVT_KILL_FOCUS, self.OnKillFocus)
        self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)

        self.sel_key = None      # last used by
        self.sel_lst = []        # highlighted selected days

        # default calendar for current month
        self.SetNow()

        self.size = None
        self.set_day = None

        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Bind(wx.EVT_SIZE, self.OnSize)

    def AcceptsFocus(self):
        """Can it accept focus?"""
        return self.IsShown() and self.IsEnabled()

    def GetColor(self, name):
        """
        Get a color.

        :param `name`: a valid color name, can be defined using :meth:`SetColor`

        """
        return MakeColor(self.colors[name])

    def SetColor(self, name, value):
        """
        Set a color.

        :param `name`: the name to be assigned to the color.
        :param `value`: the color value, see :class:`wx.Colour` for valid values

        """
        self.colors[name] = MakeColor(value)

    def HideTitle(self):
        """Hide the calendar title."""
        self.hide_title = True

    def HideGrid(self):
        """Hide the calendar grid."""
        self.hide_grid = True

    def ProcessClick(self, event):
        """Determine the calendar rectangle click area and draw a selection."""
        self.SetFocus()
        self.x, self.y = event.GetX(), event.GetY()
        self.shiftkey = event.ShiftDown()
        self.ctrlkey = event.ControlDown()
        key = self.GetDayHit(self.x, self.y)
        self.SelectDay(key)

    def OnLeftEvent(self, event):
        """Left mouse click event handler."""
        self.click = 'LEFT'
        self.ProcessClick(event)

    def OnLeftDEvent(self, event):
        """Left double mouse click event handler."""
        self.click = 'DLEFT'
        self.ProcessClick(event)

    def OnRightEvent(self, event):
        """Right mouse click event handler."""
        self.click = 'RIGHT'
        self.ProcessClick(event)

    def OnRightDEvent(self, event):
        """Right double mouse click event handler."""
        self.click = 'DRIGHT'
        self.ProcessClick(event)

    def OnMiddleEvent(self, event):
        """Middle mouse click event  handler."""
        self.click = 'MIDDLE'
        self.ProcessClick(event)

    def OnMiddleDEvent(self, event):
        """Middle double mouse click event handler."""
        self.click = 'DMIDDLE'
        self.ProcessClick(event)

    def OnSetFocus(self, event):
        """Set focus event handler."""
        self.hasFocus = True
        self.DrawFocusIndicator(True)

    def OnKillFocus(self, event):
        """Kill focus event handler."""
        self.hasFocus = False
        self.DrawFocusIndicator(False)

    def OnKeyDown(self, event):
        """Key down event handler."""
        if not self.hasFocus:
            event.Skip()
            return

        key_code = event.GetKeyCode()

        if key_code == wx.WXK_TAB:
            forward = not event.ShiftDown()
            ne = wx.NavigationKeyEvent()
            ne.SetDirection(forward)
            ne.SetCurrentFocus(self)
            ne.SetEventObject(self)
            self.GetParent().GetEventHandler().ProcessEvent(ne)
            event.Skip()
            return

        delta = None

        if key_code == wx.WXK_UP:
            delta = -7
        elif key_code == wx.WXK_DOWN:
            delta = 7
        elif key_code == wx.WXK_LEFT:
            delta = -1
        elif key_code == wx.WXK_RIGHT:
            delta = 1
        elif key_code == wx.WXK_HOME:
            curDate = wx.DateTime.FromDMY(int(self.cal_days[self.sel_key]), self.month - 1, self.year)
            newDate = wx.DateTime.Now()
            ts = newDate - curDate
            delta = ts.GetDays()

        if delta is not None:
            curDate = wx.DateTime.FromDMY(int(self.cal_days[self.sel_key]), self.month - 1, self.year)
            timeSpan = wx.TimeSpan.Days(delta)
            newDate = curDate + timeSpan

            if curDate.GetMonth() == newDate.GetMonth():
                self.set_day = newDate.GetDay()
                key = self.sel_key + delta
                self.SelectDay(key)
            else:
                self.month = newDate.GetMonth() + 1
                self.year = newDate.GetYear()
                self.set_day = newDate.GetDay()
                self.sel_key = None
                self.DoDrawing(wx.ClientDC(self))

        event.Skip()

    def SetSize(self, set_size):
        """
        Set the size.

        :param `set_size`: the control size. A value of (-1, -1) indicates a default size,
         chosen by either the windowing system or wxPython, depending on platform;
        :type `set_size`: tuple or :class:`wx.Size`

        """
        self.size = set_size

    def SetSelDay(self, sel):
        """
        Set the days to highlight.

        :param list `sel`: the list of days to highlight

        """
        self.sel_lst = sel

    def SetNow(self):
        """Get the current day."""
        dt = now()
        self.month = dt.month
        self.year = dt.year
        self.day = dt.day

    def SetCurrentDay(self):
        """Set the current day to today."""
        self.SetNow()
        self.set_day = self.day

    # get the date, day, month, year set in calendar

    def GetDate(self):
        """
        Get the set calendar date.

        :returns: the day, the month and the year

        """
        return self.day, self.month, self.year

    def GetDay(self):
        """
        Get the set calendar day.

        :returns: the day

        """
        return self.day

    def GetMonth(self):
        """
        Get the set calendar month.

        :returns: the month

        """
        return self.month

    def GetYear(self):
        """
        Get the set calendar year.

        :returns: the year

        """
        return self.year

    def SetDayValue(self, day):
        """
        Set the day.

        :param int `day`: the day

        """
        self.set_day = day
        self.day = day

    def SetMonth(self, month):
        """
        Set the Month.

        :param int `month`: the month

        """
        if month >= 1 and month <= 12:
            self.month = month
        else:
            self.month = 1
        self.set_day = None

    def SetYear(self, year):
        """
        Set the year.

        :param int `year`: the year

        """
        self.year = year

    def IncYear(self):
        """Increment the year by 1."""
        self.year = self.year + 1
        self.set_day = None

    def DecYear(self):
        """Decrement the year by 1."""
        self.year = self.year - 1
        self.set_day = None

    def IncMonth(self):
        """Increment the month by 1."""
        self.month = self.month + 1
        if self.month > 12:
            self.month = 1
            self.year = self.year + 1
        self.set_day = None

    def DecMonth(self):
        """Decrement the month by 1."""
        self.month = self.month - 1
        if self.month < 1:
            self.month = 12
            self.year = self.year - 1
        self.set_day = None

    def TestDay(self, key):
        """
        Test to see if the selection has a date and create event.

        :param `key`: the day to test

        """
        try:
            self.day = int(self.cal_days[key])
        except Exception:
            return None

        if self.day == "":
            return None
        else:
            # Changed 12/1/03 by jmg (see above) to support 2.5 event binding
            evt = wx.PyCommandEvent(wxEVT_COMMAND_PYCALENDAR_DAY_CLICKED, self.GetId())
            evt.click, evt.day, evt.month, evt.year = self.click, self.day, self.month, self.year
            evt.shiftkey = self.shiftkey
            evt.ctrlkey = self.ctrlkey
            self.GetEventHandler().ProcessEvent(evt)

            self.set_day = self.day
            return key

    def GetDayHit(self, mx, my):
        """
        Find the clicked area rectangle.

        :param `mx`: the x position
        :param `my`: the y positon

        """
        for key in self.rg.keys():
            val = self.rg[key]
            ms_rect = wx.Rect(mx, my, 1, 1)
            if wx.IntersectRect(ms_rect, val) is not None:
                result = self.TestDay(key)
                return result

        return None

    def SetWeekColor(self, font_color, week_color):
        """
        Set the week title color.

        :param `font_color`: the font color to use.
        :param `week_color`: the week color to use for the background.
        """
        self.colors[COLOR_HEADER_FONT] = MakeColor(font_color)
        self.colors[COLOR_HEADER_BACKGROUND] = MakeColor(week_color)
        self.colors[COLOR_3D_LIGHT] = MakeColor(week_color)
        self.colors[COLOR_3D_DARK] = MakeColor(week_color)

    def SetTextAlign(self, vert, horz):
        """
        Set the text allignment.

        :param `vert`: the vertical allignment
        :param `horz`: the horizontal allignment

        """
        self.num_align_horz = horz
        self.num_align_vert = vert

    def AddSelect(self, list, font_color, back_color):
        """
        Add a selection.

        :param `list`: list of days to select
        :param `font_color`: the font color to use
        :param `back_color`: the back color to use

        """
        list_val = [list, font_color, back_color]
        self.select_list.append(list_val)

    def ShowWeekEnd(self):
        """Highlight the weekend."""
        self.show_weekend = True

    def SetBusType(self):
        """Set the calendar type to 'BUS'."""
        self.cal_type = "BUS"

    def OnSize(self, evt):
        """The on size event handler."""
        self.Refresh(False)
        evt.Skip()

    def OnPaint(self, event):
        """The on paint event handler."""
        DC = wx.PaintDC(self)
        self.DoDrawing(DC)

    def DoDrawing(self, DC):
        """
        Do the drawing.

        :param `DC`: the :class:`wx.DC` to draw

        """
        DC = wx.PaintDC(self)

        try:
            cal = self.caldraw
        except Exception:
            self.caldraw = CalDraw(self)
            cal = self.caldraw

        cal.hide_grid = self.hide_grid
        cal.hide_title = self.hide_title
        cal.show_weekend = self.show_weekend
        cal.cal_type = self.cal_type
        cal.outer_border = self.outer_border
        cal.num_align_horz = self.num_align_horz
        cal.num_align_vert = self.num_align_vert
        cal.colors = self.colors

        if self.size is None:
            size = self.GetClientSize()
        else:
            size = self.size

        # drawing attributes

        cal.SetSize(size)
        cal.SetCal(self.year, self.month)

        # these have to set after SetCal as SetCal would overwrite them again.
        cal.set_x_mrg = self.set_x_mrg
        cal.set_y_mrg = self.set_y_mrg
        cal.set_y_end = self.set_y_end

        for val in self.select_list:
            cal.AddSelect(val[0], val[1], val[2])

        cal.DrawCal(DC, self.sel_lst)

        self.rg = cal.GetRect()
        self.cal_days = cal.GetCal()
        self.st_pos = cal.GetOffset()
        self.ymax = DC.MaxY()

        if self.set_day is not None:
            self.SetDay(self.set_day)

    def DrawFocusIndicator(self, draw):
        """
        Draw the focus indicator or a border.

        :param `draw`: True draws the focus indicator, False a border

        """
        DC = wx.ClientDC(self)
        try:
            if draw is True:
                self.caldraw.DrawFocusIndicator(DC)
            else:
                self.caldraw.DrawBorder(DC, True)
        except Exception:
            pass

    def DrawRect(self, key, bgcolor='WHITE', fgcolor='PINK', width=0):
        """
        Draw a rectangle.

        :param `key`: the day to draw the rectangle on
        :param `bgcolor`: the background color

        """
        if key is None:
            return

        DC = wx.ClientDC(self)

        brush = wx.Brush(MakeColor(bgcolor))
        DC.SetBrush(brush)

        DC.SetPen(wx.TRANSPARENT_PEN)

        rect = self.rg[key]
        DC.DrawRectangle(rect.x + 1, rect.y + 1, rect.width - 2, rect.height - 2)

        self.caldraw.DrawDayText(DC, key)

    def DrawRectOrg(self, key, fgcolor='BLACK', width=0):
        """
        Draw a rectangle.

        :param `key`: the day to draw the rectangle on
        :param `fgcolor`: the color for the pen
        :param `width`: the width for the pen

        """
        if key is None:
            return

        DC = wx.ClientDC(self)

        brush = wx.Brush(wx.Colour(0, 0xFF, 0x80), wx.TRANSPARENT)
        DC.SetBrush(brush)

        try:
            DC.SetPen(wx.Pen(MakeColor(fgcolor), width))
        except Exception:
            DC.SetPen(wx.Pen(MakeColor(self.GetColor(COLOR_GRID_LINES)), width))

        rect = self.rg[key]
        DC.DrawRectangle(rect)

    def SetDay(self, day):
        """
        Set the day.

        :param `day`: the day to select

        """
        d = day + self.st_pos - 1
        self.SelectDay(d)

    def IsDayInWeekend(self, key):
        """
        Is the day in the weekend

        :param `key`: the day to check

        """
        try:
            t = Date(self.year, self.month, 1)

            day = self.cal_days[key]
            day = int(day) + t.day_of_week

            if day % 7 == 6 or day % 7 == 0:
                return True
        except Exception:
            return False

    def SelectDay(self, key):
        """
        Select the day.

        :param `key`: The day to select

        """
        sel_size = 1
        # clear large selection

        if self.sel_key is not None:
            (cfont, bgcolor) = self.__GetColorsForDay(self.sel_key)
            self.DrawRect(self.sel_key, bgcolor, cfont, sel_size)

        self.DrawRect(key, self.GetColor(COLOR_HIGHLIGHT_BACKGROUND), self.GetColor(COLOR_HIGHLIGHT_FONT), sel_size)
        # store last used by
        self.sel_key = key

    def SetMargin(self, xmarg, ymarg):
        """
        Set the margins

        :param `xmarg`: the 'x' margin
        :param `ymarg`: the 'y' margin

        """
        self.set_x_mrg = xmarg
        self.set_y_mrg = ymarg
        self.set_y_end = ymarg

    def __GetColorsForDay(self, key):
        cfont = self.GetColor(COLOR_FONT)
        bgcolor = self.GetColor(COLOR_BACKGROUND)

        if self.IsDayInWeekend(key) is True and self.show_weekend is True:
            cfont = self.GetColor(COLOR_WEEKEND_FONT)
            bgcolor = self.GetColor(COLOR_WEEKEND_BACKGROUND)

        try:
            dayIdx = int(self.cal_days[key])
            (cfont, bgcolor) = self.caldraw.cal_sel[dayIdx]
        except Exception:
            pass

        return (cfont, bgcolor)


class CalenDlg(wx.Dialog):
    """A dialog with a calendar control."""
    def __init__(self, parent, month=None, day=None, year=None):
        """
        Default class constructor.

        :param wx.Window `parent`: parent window. Must not be ``None``;
        :param integer `month`: the month, if None the current day will be used
        :param integer `day`: the day
        :param integer `year`: the year

        """
        wx.Dialog.__init__(self, parent, -1, "Event Calendar", wx.DefaultPosition, (280, 360))
        self.result = None

        # set the calendar and attributes
        self.calend = Calendar(self, -1, (20, 60), (240, 200))

        if month is None:
            self.calend.SetCurrentDay()
            start_month = self.calend.GetMonth()
            start_year = self.calend.GetYear()
        else:
            self.calend.month = start_month = month
            self.calend.year = start_year = year
            self.calend.SetDayValue(day)

        self.calend.HideTitle()
        self.ResetDisplay()

        # get month list from DateTime
        monthlist = GetMonthList()

        # select the month
        self.date = wx.ComboBox(self, -1, Month[start_month], (20, 20), (90, -1),
                                monthlist, wx.CB_DROPDOWN)
        self.Bind(wx.EVT_COMBOBOX, self.EvtComboBox, self.date)

        # alternate spin button to control the month
        h = self.date.GetSize().height
        self.m_spin = wx.SpinButton(self, -1, (115, 20), (h * 1.5, h), wx.SP_VERTICAL)
        self.m_spin.SetRange(1, 12)
        self.m_spin.SetValue(start_month)
        self.Bind(wx.EVT_SPIN, self.OnMonthSpin, self.m_spin)

        # spin button to control the year
        self.dtext = wx.TextCtrl(self, -1, str(start_year), (160, 20), (60, -1))
        h = self.dtext.GetSize().height

        self.y_spin = wx.SpinButton(self, -1, (225, 20), (h * 1.5, h), wx.SP_VERTICAL)
        self.y_spin.SetRange(1980, 2010)
        self.y_spin.SetValue(start_year)

        self.Bind(wx.EVT_SPIN, self.OnYrSpin, self.y_spin)
        self.Bind(EVT_CALENDAR, self.MouseClick, self.calend)

        x_pos = 50
        y_pos = 280
        but_size = (60, 25)

        btn = wx.Button(self, wx.ID_OK, ' Ok ', (x_pos, y_pos), but_size)
        self.Bind(wx.EVT_BUTTON, self.OnOk, btn)

        btn = wx.Button(self, wx.ID_CANCEL, ' Close ', (x_pos + 120, y_pos), but_size)
        self.Bind(wx.EVT_BUTTON, self.OnCancel, btn)

    def OnOk(self, evt):
        """The OK event handler."""
        self.result = ['None', str(self.calend.day), Month[self.calend.month], str(self.calend.year)]
        self.EndModal(wx.ID_OK)

    def OnCancel(self, event):
        """The Cancel event handler."""
        self.EndModal(wx.ID_CANCEL)

    def MouseClick(self, evt):
        """The mouse click event handler."""
        self.month = evt.month
        # result click type and date
        self.result = [evt.click, str(evt.day), Month[evt.month], str(evt.year)]

        if evt.click == 'DLEFT':
            self.EndModal(wx.ID_OK)

    def OnMonthSpin(self, event):
        """The month spin control event handler."""
        month = event.GetPosition()
        self.date.SetValue(Month[month])
        self.calend.SetMonth(month)
        self.calend.Refresh()

    def OnYrSpin(self, event):
        """The year spin control event handler."""
        year = event.GetPosition()
        self.dtext.SetValue(str(year))
        self.calend.SetYear(year)
        self.calend.Refresh()

    def EvtComboBox(self, event):
        """The month combobox event handler."""
        name = event.GetString()
        monthval = self.date.FindString(name)
        self.m_spin.SetValue(monthval + 1)

        self.calend.SetMonth(monthval + 1)
        self.ResetDisplay()

    def ResetDisplay(self):
        """Reset the display."""
        month = self.calend.GetMonth()
        self.calend.Refresh()