1 #----------------------------------------------------------------------------
3 # Purpose: Calendar display control
5 # Author: Lorne White (email: lorne.white@telusplanet.net)
10 # Licence: wxWindows license
11 #----------------------------------------------------------------------------
12 # 12/01/2003 - Jeff Grimmett (grimmtooth@softhome.net)
14 # o Updated for wx namespace
15 # o Tested with updated demo
16 # o Added new event type EVT_CALENDAR. The reason for this is that the original
17 # library used a hardcoded ID of 2100 for generating events. This makes it
18 # very difficult to fathom when trying to decode the code since there's no
19 # published API. Creating the new event binder might seem like overkill -
20 # after all, you might ask, why not just use a new event ID and be done with
21 # it? However, a consistent interface is very useful at times; also it makes
22 # it clear that we're not just hunting for mouse clicks -- we're hunting
23 # wabbit^H^H^H^H (sorry bout that) for calender-driven mouse clicks. So
24 # that's my sad story. Shoot me if you must :-)
25 # o There's still one deprecation warning buried in here somewhere, but I
26 # haven't been able to find it yet. It only occurs when displaying a
27 # print preview, and only the first time. It *could* be an error in the
30 # Here's the traceback:
32 # C:\Python\lib\site-packages\wx\core.py:949: DeprecationWarning:
33 # integer argument expected, got float
34 # newobj = _core.new_Rect(*args, **kwargs)
41 CalDays
= [6, 0, 1, 2, 3, 4, 5]
42 AbrWeekday
= {6:"Sun", 0:"Mon", 1:"Tue", 2:"Wed", 3:"Thu", 4:"Fri", 5:"Sat"}
45 BusCalDays
= [0, 1, 2, 3, 4, 5, 6]
47 # Calendar click event - added 12/1/03 by jmg (see above)
48 wxEVT_COMMAND_PYCALENDAR_DAY_CLICKED
= wx
.NewEventType()
49 EVT_CALENDAR
= wx
.PyEventBinder(wxEVT_COMMAND_PYCALENDAR_DAY_CLICKED
, 1)
56 monthlist
.append(name
)
59 # calendar drawing routing
62 def __init__(self
, parent
):
66 self
.scale
= parent
.scale
73 self
.num_auto
= True # auto scale of the cal number day size
74 self
.num_size
= 12 # default size of calendar if no auto size
75 self
.max_num_size
= 12 # maximum size for calendar number
77 self
.num_align_horz
= wx
.ALIGN_CENTRE
# alignment of numbers
78 self
.num_align_vert
= wx
.ALIGN_CENTRE
79 self
.num_indent_horz
= 0 # points indent from position, used to offset if not centered
80 self
.num_indent_vert
= 0
82 self
.week_auto
= True # auto scale of week font text
84 self
.max_week_size
= 12
86 self
.grid_color
= 'BLACK' # grid and selection colors
87 self
.back_color
= 'WHITE'
88 self
.sel_color
= 'RED'
90 self
.high_color
= 'LIGHT BLUE'
91 self
.border_color
= 'BLACK'
92 self
.week_color
= 'LIGHT GREY'
94 self
.week_font_color
= 'BLACK' # font colors
95 self
.day_font_color
= 'BLACK'
100 self
.hide_title
= False
101 self
.hide_grid
= False
102 self
.outer_border
= True
104 self
.title_offset
= 0
105 self
.cal_week_scale
= 0.7
106 self
.show_weekend
= False
107 self
.cal_type
= "NORMAL"
109 def SetWeekColor(self
, font_color
, week_color
): # set font and background color for week title
110 self
.week_font_color
= font_color
111 self
.week_color
= week_color
113 def SetSize(self
, size
):
114 self
.set_sizew
= size
[0]
115 self
.set_sizeh
= size
[1]
117 def InitValues(self
): # default dimensions of various elements of the calendar
120 self
.set_cy_st
= 0 # start position
123 self
.set_y_mrg
= 15 # start of vertical draw default
127 def SetPos(self
, xpos
, ypos
):
128 self
.set_cx_st
= xpos
129 self
.set_cy_st
= ypos
131 def SetMarg(self
, xmarg
, ymarg
):
132 self
.set_x_st
= xmarg
133 self
.set_y_st
= ymarg
134 self
.set_y_end
= ymarg
136 def InitScale(self
): # scale position values
137 self
.sizew
= self
.set_sizew
* self
.pwidth
138 self
.sizeh
= self
.set_sizeh
* self
.pheight
140 self
.cx_st
= self
.set_cx_st
* self
.pwidth
# draw start position
141 self
.cy_st
= self
.set_cy_st
* self
.pheight
143 self
.x_mrg
= self
.set_x_mrg
* self
.pwidth
# calendar draw margins
144 self
.y_mrg
= self
.set_y_mrg
* self
.pheight
145 self
.y_end
= self
.set_y_end
* self
.pheight
147 def DrawCal(self
, DC
, sel_lst
=[]):
153 if self
.hide_title
is False:
161 if self
.show_weekend
is True: # highlight weekend dates
164 self
.AddSelect(sel_lst
) # overrides the weekend highlight
166 self
.DrawSel() # highlighted days
170 def AddSelect(self
, list, cfont
=None, cbackgrd
= None):
172 cfont
= self
.sel_color
# font digit color
175 cbackgrd
= self
.high_color
# select background color
178 self
.cal_sel
[val
] = (cfont
, cbackgrd
)
180 # draw border around the outside of the main display rectangle
181 def DrawBorder(self
):
182 brush
= wx
.Brush(wx
.NamedColour(self
.back_color
), wx
.SOLID
)
183 self
.DC
.SetBrush(brush
)
184 self
.DC
.SetPen(wx
.Pen(wx
.NamedColour(self
.border_color
), 1))
186 if self
.outer_border
is True:
187 # full display window area
188 rect
= wx
.Rect(self
.cx_st
, self
.cy_st
, self
.sizew
, self
.sizeh
)
189 self
.DC
.DrawRectangleRect(rect
)
191 def DrawNumVal(self
):
194 # calculate the calendar days and offset position
196 def SetCal(self
, year
, month
):
197 self
.InitValues() # reset initial values
203 t
= Date(year
, month
, day
)
204 dow
= self
.dow
= t
.day_of_week
# start day in month
205 dim
= self
.dim
= t
.days_in_month
# number of days in month
207 if self
.cal_type
== "NORMAL":
212 self
.st_pos
= start_pos
215 for i
in range(start_pos
):
220 self
.cal
.append(str(i
))
225 def SetWeekEnd(self
, font_color
='BLACK', backgrd
= 'LIGHT GREY'):
226 date
= 6 - int(self
.dow
) # start day of first saturday
228 while date
<= self
.dim
:
229 self
.cal_sel
[date
] = (font_color
, backgrd
) # Saturday
233 self
.cal_sel
[date
] = (font_color
, backgrd
) # Sunday
238 def GetRect(self
): # get the display rectange list of the day grid
240 for y
in self
.gridy
[1:-1]:
241 for x
in self
.gridx
[:-1]:
242 rect
= wx
.Rect(x
, y
, self
.dl_w
, self
.dl_h
) # create rect region
254 def DrawMonth(self
): # month and year title
255 month
= Month
[self
.month
]
258 if self
.sizeh
< _MIDSIZE
:
261 f
= wx
.Font(sizef
, self
.font
, wx
.NORMAL
, self
.bold
)
264 tw
,th
= self
.DC
.GetTextExtent(month
)
265 adjust
= self
.cx_st
+ (self
.sizew
-tw
)/2
266 self
.DC
.DrawText(month
, (adjust
, self
.cy_st
+ th
))
268 year
= str(self
.year
)
269 tw
,th
= self
.DC
.GetTextExtent(year
)
270 adjust
= self
.sizew
- tw
- self
.x_mrg
272 self
.title_offset
= th
* 2
274 f
= wx
.Font(sizef
, self
.font
, wx
.NORMAL
, self
.bold
)
276 self
.DC
.DrawText(year
, (self
.cx_st
+ adjust
, self
.cy_st
+ th
))
278 def DrawWeek(self
): # draw the week days
279 width
= self
.gridx
[1]-self
.gridx
[0]
280 height
= self
.gridy
[1] - self
.gridy
[0]
281 rect_w
= self
.gridx
[7]-self
.gridx
[0]
283 f
= wx
.Font(10, self
.font
, wx
.NORMAL
, self
.bold
) # initial font setting
285 if self
.week_auto
== True:
286 test_size
= self
.max_week_size
# max size
289 f
.SetPointSize(test_size
)
291 tw
,th
= self
.DC
.GetTextExtent(test_day
)
293 if tw
< width
and th
< height
:
296 test_size
= test_size
- 1
298 f
.SetPointSize(self
.week_size
) # set fixed size
301 self
.DC
.SetTextForeground(wx
.NamedColour(self
.week_font_color
))
306 brush
= wx
.Brush(wx
.NamedColour(self
.week_color
), wx
.SOLID
)
307 self
.DC
.SetBrush(brush
)
308 self
.DC
.DrawRectangle((self
.gridx
[0], self
.gridy
[0]), (rect_w
+1, height
))
310 if self
.cal_type
== "NORMAL":
313 cal_days
= BusCalDays
316 day
= AbrWeekday
[val
]
321 dw
,dh
= self
.DC
.GetTextExtent(day
)
323 diffy
= (height
-dh
)/2
325 x
= self
.gridx
[cnt_x
]
326 y
= self
.gridy
[cnt_y
]
327 self
.DC
.DrawRectangle((self
.gridx
[cnt_x
], self
.gridy
[0]), (width
+1, height
))
328 self
.DC
.DrawText(day
, (x
+diffx
, y
+diffy
))
331 def DrawNum(self
): # draw the day numbers
332 f
= wx
.Font(10, self
.font
, wx
.NORMAL
, self
.bold
) # initial font setting
334 if self
.num_auto
== True:
335 test_size
= self
.max_num_size
# max size
339 f
.SetPointSize(test_size
)
341 tw
,th
= self
.DC
.GetTextExtent(test_day
)
343 if tw
< self
.dl_w
and th
< self
.dl_h
:
346 test_size
= test_size
- 1
348 f
.SetPointSize(self
.num_size
) # set fixed size
354 x
= self
.gridx
[cnt_x
]
355 y
= self
.gridy
[cnt_y
]
359 num_color
= self
.cal_sel
[num_val
][0]
361 num_color
= self
.day_font_color
363 self
.DC
.SetTextForeground(wx
.NamedColour(num_color
))
366 tw
,th
= self
.DC
.GetTextExtent(val
)
368 if self
.num_align_horz
== wx
.ALIGN_CENTRE
:
369 adj_h
= (self
.dl_w
- tw
)/2
370 elif self
.num_align_horz
== wx
.ALIGN_RIGHT
:
371 adj_h
= self
.dl_w
- tw
373 adj_h
= 0 # left alignment
375 adj_h
= adj_h
+ self
.num_indent_horz
377 if self
.num_align_vert
== wx
.ALIGN_CENTRE
:
378 adj_v
= (self
.dl_h
- th
)/2
379 elif self
.num_align_horz
== wx
.ALIGN_RIGHT
:
380 adj_v
= self
.dl_h
- th
382 adj_v
= 0 # left alignment
384 adj_v
= adj_v
+ self
.num_indent_vert
386 self
.DC
.DrawText(val
, (x
+adj_h
, y
+adj_v
))
394 def Center(self
): # calculate the dimensions in the center of the drawing area
396 bdh
= self
.y_mrg
+ self
.y_end
+ self
.title_offset
398 self
.dl_w
= int((self
.sizew
-bdw
)/7)
399 self
.dl_h
= int((self
.sizeh
-bdh
)/7)
401 self
.dl_th
= int(self
.dl_h
*self
.cal_week_scale
) # week title adjustment
402 self
.cwidth
= self
.dl_w
* 7
403 self
.cheight
= self
.dl_h
* 6 + self
.dl_th
405 def DrawSel(self
): # highlighted selected days
406 for key
in self
.cal_sel
.keys():
407 sel_color
= self
.cal_sel
[key
][1]
408 brush
= wx
.Brush(wx
.NamedColour(sel_color
), wx
.SOLID
)
409 self
.DC
.SetBrush(brush
)
411 if self
.hide_grid
is False:
412 self
.DC
.SetPen(wx
.Pen(wx
.NamedColour(self
.grid_color
), 0))
414 self
.DC
.SetPen(wx
.Pen(wx
.NamedColour(self
.back_color
), 0))
416 nkey
= key
+ self
.st_pos
-1
418 self
.DC
.DrawRectangle((rect
.x
, rect
.y
), (rect
.width
+1, rect
.height
+1))
420 def DrawGrid(self
): # calculate and draw the grid lines
421 self
.DC
.SetPen(wx
.Pen(wx
.NamedColour(self
.grid_color
), 0))
426 self
.x_st
= self
.cx_st
+ self
.x_mrg
427 self
.y_st
= self
.cy_st
+ self
.y_mrg
+ self
.title_offset
# start postion of draw
431 y2
= y1
+ self
.cheight
434 if self
.hide_grid
is False:
435 self
.DC
.DrawLine((x1
, y1
), (x1
, y2
))
436 self
.gridx
.append(x1
)
441 x2
= x1
+ self
.cwidth
444 if self
.hide_grid
is False:
445 self
.DC
.DrawLine((x1
, y1
), (x2
, y1
))
447 self
.gridy
.append(y1
)
455 class PrtCalDraw(CalDraw
):
456 def InitValues(self
):
459 self
.set_cx_st
= 1.0 # start draw border location
462 self
.set_y_mrg
= 0.2 # draw offset position
466 # calculate the dimensions in the center of the drawing area
467 def SetPSize(self
, pwidth
, pheight
):
468 self
.pwidth
= int(pwidth
)/self
.scale
469 self
.pheight
= int(pheight
)/self
.scale
471 def SetPreview(self
, preview
):
472 self
.preview
= preview
474 class wxCalendar(wx
.Window
):
475 def __init__(self
, parent
, id, pos
=wx
.DefaultPosition
, size
=wx
.DefaultSize
):
476 wx
.Window
.__init
__(self
, parent
, id, pos
, size
)
478 # set the calendar control attributes
480 self
.grid_color
= 'BLACK'
481 self
.back_color
= 'WHITE'
482 self
.hide_grid
= False
483 self
.sel_color
= 'RED'
484 self
.hide_title
= False
485 self
.show_weekend
= False
486 self
.cal_type
= "NORMAL"
488 self
.week_color
= 'LIGHT GREY'
489 self
.week_font_color
= 'BLACK' # font colors
491 self
.select_list
= []
493 self
.SetBackgroundColour(self
.back_color
)
494 self
.Bind(wx
.EVT_LEFT_DOWN
, self
.OnLeftEvent
)
495 self
.Bind(wx
.EVT_LEFT_DCLICK
, self
.OnLeftDEvent
)
496 self
.Bind(wx
.EVT_RIGHT_DOWN
, self
.OnRightEvent
)
497 self
.Bind(wx
.EVT_RIGHT_DCLICK
, self
.OnRightDEvent
)
499 self
.sel_key
= None # last used by
500 self
.sel_lst
= [] # highlighted selected days
502 self
.SetNow() # default calendar for current month
507 self
.Bind(wx
.EVT_PAINT
, self
.OnPaint
)
508 self
.Bind(wx
.EVT_SIZE
, self
.OnSize
)
510 # control some of the main calendar attributes
513 self
.hide_title
= True
516 self
.hide_grid
= True
518 # determine the calendar rectangle click area and draw a selection
520 def ProcessClick(self
, event
):
521 self
.x
, self
.y
= event
.GetX(), event
.GetY()
522 key
= self
.GetDayHit(self
.x
, self
.y
)
525 # tab mouse click events and process
527 def OnLeftEvent(self
, event
):
529 self
.shiftkey
= event
.ShiftDown()
530 self
.ctrlkey
= event
.ControlDown()
531 self
.ProcessClick(event
)
533 def OnLeftDEvent(self
, event
):
535 self
.ProcessClick(event
)
537 def OnRightEvent(self
, event
):
539 self
.ProcessClick(event
)
541 def OnRightDEvent(self
, event
):
542 self
.click
= 'DRIGHT'
543 self
.ProcessClick(event
)
545 def SetSize(self
, set_size
):
548 def SetSelDay(self
, sel
):
549 self
.sel_lst
= sel
# list of highlighted days
551 # get the current date
555 self
.month
= dt
.month
559 # set the current day
561 def SetCurrentDay(self
):
563 self
.set_day
= self
.day
565 # get the date, day, month, year set in calendar
568 return self
.day
, self
.month
, self
.year
579 # set the day, month, and year
581 def SetDayValue(self
, day
):
584 def SetMonth(self
, month
):
585 if month
>= 1 and month
<= 12:
591 def SetYear(self
, year
):
594 # increment year and month
597 self
.year
= self
.year
+ 1
601 self
.year
= self
.year
- 1
605 self
.month
= self
.month
+ 1
608 self
.year
= self
.year
+ 1
612 self
.month
= self
.month
- 1
615 self
.year
= self
.year
- 1
618 # test to see if the selection has a date and create event
620 def TestDay(self
, key
):
622 self
.day
= int(self
.cal
[key
])
628 # Changed 12/1/03 by jmg (see above) to support 2.5 event binding
629 evt
= wx
.PyCommandEvent(wxEVT_COMMAND_PYCALENDAR_DAY_CLICKED
, self
.GetId())
630 evt
.click
, evt
.day
, evt
.month
, evt
.year
= self
.click
, self
.day
, self
.month
, self
.year
631 evt
.shiftkey
= self
.shiftkey
632 evt
.ctrlkey
= self
.ctrlkey
633 self
.GetEventHandler().ProcessEvent(evt
)
635 self
.set_day
= self
.day
638 # find the clicked area rectangle
640 def GetDayHit(self
, mx
, my
):
641 for key
in self
.rg
.keys():
643 ms_rect
= wx
.Rect(mx
, my
, 1, 1)
644 if wx
.IntersectRect(ms_rect
, val
) is not None:
645 result
= self
.TestDay(key
)
652 def SetWeekColor(self
, font_color
, week_color
): # set font and background color for week title
653 self
.week_font_color
= font_color
654 self
.week_color
= week_color
656 def AddSelect(self
, list, font_color
, back_color
):
657 list_val
= [list, font_color
, back_color
]
658 self
.select_list
.append(list_val
)
660 def ShowWeekEnd(self
):
661 self
.show_weekend
= True # highlight weekend
663 def SetBusType(self
):
664 self
.cal_type
= "BUS"
666 def OnSize(self
, evt
):
670 def OnPaint(self
, event
):
671 DC
= wx
.PaintDC(self
)
674 def DoDrawing(self
, DC
):
675 DC
= wx
.PaintDC(self
)
678 self
.cal
= cal
= CalDraw(self
)
680 cal
.grid_color
= self
.grid_color
681 cal
.back_color
= self
.back_color
682 cal
.hide_grid
= self
.hide_grid
683 cal
.grid_color
= self
.grid_color
684 cal
.hide_title
= self
.hide_title
685 cal
.show_weekend
= self
.show_weekend
686 cal
.cal_type
= self
.cal_type
688 if self
.size
is None:
689 size
= self
.GetClientSize()
695 cal
.week_font_color
= self
.week_font_color
696 cal
.week_color
= self
.week_color
699 cal
.SetCal(self
.year
, self
.month
)
701 for val
in self
.select_list
:
702 cal
.AddSelect(val
[0], val
[1], val
[2])
704 cal
.DrawCal(DC
, self
.sel_lst
)
706 self
.rg
= cal
.GetRect()
707 self
.cal
= cal
.GetCal()
708 self
.st_pos
= cal
.GetOffset()
709 self
.ymax
= DC
.MaxY()
711 if self
.set_day
!= None:
712 self
.SetDay(self
.set_day
)
716 # draw the selection rectangle
718 def DrawRect(self
, key
, color
= 'BLACK', width
= 0):
722 DC
= wx
.ClientDC(self
)
725 brush
= wx
.Brush(wx
.Colour(0, 0xFF, 0x80), wx
.TRANSPARENT
)
727 DC
.SetPen(wx
.Pen(wx
.NamedColour(color
), width
))
730 DC
.DrawRectangle((rect
.x
, rect
.y
), (rect
.width
+1, rect
.height
+1))
734 # set the day selection
736 def SetDay(self
, day
):
737 day
= day
+ self
.st_pos
- 1
740 def SelectDay(self
, key
):
742 self
.DrawRect(self
.sel_key
, self
.back_color
, sel_size
) # clear large selection
744 if self
.hide_grid
is False:
745 self
.DrawRect(self
.sel_key
, self
.grid_color
)
747 self
.DrawRect(key
, self
.sel_color
, sel_size
)
748 self
.sel_key
= key
# store last used by
749 self
.select_day
= None
754 class CalenDlg(wx
.Dialog
):
755 def __init__(self
, parent
, month
=None, day
= None, year
=None):
756 wx
.Dialog
.__init
__(self
, parent
, -1, "Event Calendar", wx
.DefaultPosition
, (280, 360))
758 # set the calendar and attributes
759 self
.calend
= wxCalendar(self
, -1, (20, 60), (240, 200))
762 self
.calend
.SetCurrentDay()
763 start_month
= self
.calend
.GetMonth()
764 start_year
= self
.calend
.GetYear()
766 self
.calend
.month
= start_month
= month
767 self
.calend
.year
= start_year
= year
768 self
.calend
.SetDayValue(day
)
770 self
.calend
.HideTitle()
773 # get month list from DateTime
774 monthlist
= GetMonthList()
777 self
.date
= wx
.ComboBox(self
, -1, Month
[start_month
], (20, 20), (90, -1),
778 monthlist
, wx
.CB_DROPDOWN
)
779 self
.Bind(wx
.EVT_COMBOBOX
, self
.EvtComboBox
, self
.date
)
781 # alternate spin button to control the month
782 h
= self
.date
.GetSize().height
783 self
.m_spin
= wx
.SpinButton(self
, -1, (130, 20), (h
*2, h
), wx
.SP_VERTICAL
)
784 self
.m_spin
.SetRange(1, 12)
785 self
.m_spin
.SetValue(start_month
)
786 self
.Bind(wx
.EVT_SPIN
, self
.OnMonthSpin
, self
.m_spin
)
788 # spin button to control the year
789 self
.dtext
= wx
.TextCtrl(self
, -1, str(start_year
), (160, 20), (60, -1))
790 h
= self
.dtext
.GetSize().height
792 self
.y_spin
= wx
.SpinButton(self
, -1, (220, 20), (h
*2, h
), wx
.SP_VERTICAL
)
793 self
.y_spin
.SetRange(1980, 2010)
794 self
.y_spin
.SetValue(start_year
)
796 self
.Bind(wx
.EVT_SPIN
, self
.OnYrSpin
, self
.y_spin
)
797 self
.Bind(EVT_CALENDAR
, self
.MouseClick
, self
.calend
)
803 btn
= wx
.Button(self
, -1, ' Ok ', (x_pos
, y_pos
), but_size
)
804 self
.Bind(wx
.EVT_BUTTON
, self
.OnOk
, btn
)
806 btn
= wx
.Button(self
, mID
, ' Close ', (x_pos
+ 120, y_pos
), but_size
)
807 self
.Bind(wx
.EVT_BUTTON
, self
.OnCancel
, btn
)
809 def OnOk(self
, event
):
810 self
.EndModal(wx
.ID_OK
)
812 def OnCancel(self
, event
):
813 self
.EndModal(wx
.ID_CANCEL
)
815 # log the mouse clicks
816 def MouseClick(self
, evt
):
817 self
.month
= evt
.month
818 self
.result
= [evt
.click
, str(evt
.day
), Month
[evt
.month
], str(evt
.year
)] # result click type and date
820 if evt
.click
== 'DLEFT':
821 self
.EndModal(wx
.ID_OK
)
823 # month and year spin selection routines
824 def OnMonthSpin(self
, event
):
825 month
= event
.GetPosition()
826 self
.date
.SetValue(Month
[month
])
827 self
.calend
.SetMonth(month
)
828 self
.calend
.Refresh()
830 def OnYrSpin(self
, event
):
831 year
= event
.GetPosition()
832 self
.dtext
.SetValue(str(year
))
833 self
.calend
.SetYear(year
)
834 self
.calend
.Refresh()
836 def EvtComboBox(self
, event
):
837 name
= event
.GetString()
838 monthval
= self
.date
.FindString(name
)
839 self
.m_spin
.SetValue(monthval
+1)
841 self
.calend
.SetMonth(monthval
+1)
844 # set the calendar for highlighted days
846 def ResetDisplay(self
):
847 month
= self
.calend
.GetMonth()
848 self
.calend
.Refresh()