]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wxPython/lib/calendar.py
b523932d01e51df02fc2d8634113d2a0a8630197
[wxWidgets.git] / wxPython / wxPython / lib / calendar.py
1 #----------------------------------------------------------------------------
2 # Name: calendar.py
3 # Purpose: Calendar display control
4 #
5 # Author: Lorne White (email: lorne.white@telusplanet.net)
6 #
7 # Created:
8 # Version 0.85
9 # Date: June 20, 2001
10 # Licence: wxWindows license
11 #----------------------------------------------------------------------------
12
13 from wxPython.wx import *
14
15 from CDate import *
16 import string, time
17
18
19 CalDays = [6, 0, 1, 2, 3, 4, 5]
20 AbrWeekday = {6:"Sun", 0:"Mon", 1:"Tue", 2:"Wed", 3:"Thu", 4:"Fri", 5:"Sat"}
21 _MIDSIZE = 160
22
23 BusCalDays = [0, 1, 2, 3, 4, 5, 6]
24
25 # calendar drawing routing
26
27 def GetMonthList():
28 monthlist = []
29 for i in range(13):
30 name = Month[i]
31 if name != None:
32 monthlist.append(name)
33 return monthlist
34
35 class CalDraw:
36 def __init__(self, parent):
37 self.pwidth = 1
38 self.pheight = 1
39 try:
40 self.scale = parent.scale
41 except:
42 self.scale = 1
43
44 self.DefParms()
45
46 def DefParms(self):
47 self.grid_color = 'BLACK' # grid and selection colors
48 self.back_color = 'WHITE'
49 self.sel_color = 'RED'
50
51 self.high_color = 'LIGHT BLUE'
52 self.border_color = 'BLACK'
53 self.week_color = 'LIGHT GREY'
54
55 self.week_font_color = 'BLACK' # font colors
56 self.day_font_color = 'BLACK'
57
58 self.font = wxSWISS
59 self.bold = wxNORMAL
60
61 self.hide_title = FALSE
62 self.hide_grid = FALSE
63 self.outer_border = TRUE
64
65 self.title_offset = 0
66 self.cal_week_scale = 0.7
67 self.show_weekend = FALSE
68 self.cal_type = "NORMAL"
69
70 def SetWeekColor(self, font_color, week_color): # set font and background color for week title
71 self.week_font_color = font_color
72 self.week_color = week_color
73
74 def SetSize(self, size):
75 self.set_sizew = size.width
76 self.set_sizeh = size.height
77
78 def InitValues(self): # default dimensions of various elements of the calendar
79 self.rg = {}
80 self.cal_sel = {}
81 self.set_cy_st = 0 # start position
82 self.set_cx_st = 0
83
84 self.set_y_mrg = 15 # start of vertical draw default
85 self.set_x_mrg = 10
86 self.set_y_end = 10
87
88 def SetPos(self, xpos, ypos):
89 self.set_cx_st = xpos
90 self.set_cy_st = ypos
91
92 def SetMarg(self, xmarg, ymarg):
93 self.set_x_st = xmarg
94 self.set_y_st = ymarg
95 self.set_y_end = ymarg
96
97 def InitScale(self): # scale position values
98 self.sizew = self.set_sizew * self.pwidth
99 self.sizeh = self.set_sizeh * self.pheight
100
101 self.cx_st = self.set_cx_st * self.pwidth # draw start position
102 self.cy_st = self.set_cy_st * self.pheight
103
104 self.x_mrg = self.set_x_mrg * self.pwidth # calendar draw margins
105 self.y_mrg = self.set_y_mrg * self.pheight
106 self.y_end = self.set_y_end * self.pheight
107
108 def DrawCal(self, DC, sel_lst=[]):
109 self.DC = DC
110 self.InitScale()
111
112 self.DrawBorder()
113 if self.hide_title is FALSE:
114 self.DrawMonth()
115
116 self.Center()
117
118 self.DrawGrid()
119 self.GetRect()
120
121 if self.show_weekend is TRUE: # highlight weekend dates
122 self.SetWeekEnd()
123
124 self.AddSelect(sel_lst) # overrides the weekend highlight
125
126 self.DrawSel() # highlighted days
127 self.DrawWeek()
128 self.DrawNum()
129
130 def AddSelect(self, list, cfont=None, cbackgrd = None):
131 if cfont is None:
132 cfont = self.sel_color # font digit color
133 if cbackgrd is None:
134 cbackgrd = self.high_color # select background color
135
136 for val in list:
137 self.cal_sel[val] = (cfont, cbackgrd)
138
139 def DrawBorder(self): # draw border around the outside of the main display rectangle
140 brush = wxBrush(wxNamedColour(self.back_color), wxSOLID)
141 self.DC.SetBrush(brush)
142 self.DC.SetPen(wxPen(wxNamedColour(self.border_color), 1))
143
144 if self.outer_border is TRUE:
145 rect = wxRect(self.cx_st, self.cy_st, self.sizew, self.sizeh) # full display window area
146 self.DC.DrawRectangle(rect.x, rect.y, rect.width, rect.height)
147
148 def DrawNumVal(self):
149 self.DrawNum()
150
151 # calculate the calendar days and offset position
152
153 def SetCal(self, year, month):
154 self.InitValues() # reset initial values
155
156 self.year = year
157 self.month = month
158
159 day = 1
160 t = Date(year, month, day)
161 dow = self.dow = t.day_of_week # start day in month
162 dim = self.dim = t.days_in_month # number of days in month
163 if self.cal_type == "NORMAL":
164 start_pos = dow+1
165 else:
166 start_pos = dow
167
168 self.st_pos = start_pos
169
170 self.cal = []
171 for i in range(start_pos):
172 self.cal.append('')
173 i = 1
174 while i <= dim:
175 self.cal.append(str(i))
176 i = i + 1
177 return start_pos
178
179 def SetWeekEnd(self, font_color='BLACK', backgrd = 'LIGHT GREY'):
180 date = 6 - int(self.dow) # start day of first saturday
181 while date <= self.dim:
182 self.cal_sel[date] = (font_color, backgrd) # Saturday
183 date = date + 1
184 if date <= self.dim:
185 self.cal_sel[date] = (font_color, backgrd) # Sunday
186 date = date + 6
187 else:
188 date = date + 7
189
190 def GetRect(self): # get the display rectange list of the day grid
191 cnt = 0
192 for y in self.gridy[1:-1]:
193 for x in self.gridx[:-1]:
194 rect = wxRect(x, y, self.dl_w, self.dl_h) # create rect region
195 self.rg[cnt] = rect
196 cnt = cnt + 1
197 return self.rg
198
199 def GetCal(self):
200 return self.cal
201
202 def GetOffset(self):
203 return self.st_pos
204
205 def DrawMonth(self): # month and year title
206 month = Month[self.month]
207
208 sizef = 11
209 if self.sizeh < _MIDSIZE:
210 sizef = 10
211
212 f = wxFont(sizef, self.font, wxNORMAL, self.bold)
213 self.DC.SetFont(f)
214
215 tw,th = self.DC.GetTextExtent(month)
216 adjust = self.cx_st + (self.sizew-tw)/2
217 self.DC.DrawText(month, adjust, self.cy_st + th)
218
219 year = str(self.year)
220 tw,th = self.DC.GetTextExtent(year)
221 adjust = self.sizew - tw - self.x_mrg
222
223 self.title_offset = th * 2
224
225 f = wxFont(sizef, self.font, wxNORMAL, self.bold)
226 self.DC.SetFont(f)
227 self.DC.DrawText(year, self.cx_st + adjust, self.cy_st + th)
228
229 def DrawWeek(self): # draw the week days
230 sizef = 8
231 if self.sizeh < _MIDSIZE:
232 sizef = 7
233
234 f = wxFont(sizef, self.font, wxNORMAL, self.bold)
235 self.DC.SetFont(f)
236 self.DC.SetTextForeground(wxNamedColour(self.week_font_color))
237
238 cnt_x = 0
239 cnt_y = 0
240 width = self.gridx[1]-self.gridx[0]
241 height = self.gridy[1] - self.gridy[0]
242
243 rect_w = self.gridx[7]-self.gridx[0]
244
245 brush = wxBrush(wxNamedColour(self.week_color), wxSOLID)
246 self.DC.SetBrush(brush)
247 # self.DC.DrawRectangle(self.gridx[0], self.gridy[0], rect_w+1, height)
248
249 if self.cal_type == "NORMAL":
250 cal_days = CalDays
251 else:
252 cal_days = BusCalDays
253
254 for val in cal_days:
255 day = AbrWeekday[val]
256 if self.sizew < 200:
257 day = day[0]
258 dw,dh = self.DC.GetTextExtent(day)
259 diffx = (width-dw)/2
260 diffy = (height-dh)/2
261
262 x = self.gridx[cnt_x]
263 y = self.gridy[cnt_y]
264 self.DC.DrawRectangle(self.gridx[cnt_x], self.gridy[0], width+1, height)
265 self.DC.DrawText(day, x+diffx, y+diffy)
266 cnt_x = cnt_x + 1
267
268
269 def DrawNum(self): # draw the day numbers
270 sizef = 10
271 if self.sizeh < _MIDSIZE:
272 sizef = 8
273 f = wxFont(sizef, self.font, wxNORMAL, self.bold)
274
275 cnt_x = 0
276 cnt_y = 1
277 for val in self.cal:
278 x = self.gridx[cnt_x]
279 y = self.gridy[cnt_y]
280
281 try:
282 num_val = int(val)
283 num_color = self.cal_sel[num_val][0]
284 except:
285 num_color = self.day_font_color
286
287 self.DC.SetTextForeground(wxNamedColour(num_color))
288 self.DC.SetFont(f)
289
290 self.DC.DrawText(val, x+5, y+5)
291 if cnt_x < 6:
292 cnt_x = cnt_x + 1
293 else:
294 cnt_x = 0
295 cnt_y = cnt_y + 1
296
297 def Center(self): # calculate the dimensions in the center of the drawing area
298 bdw = self.x_mrg * 2
299 bdh = self.y_mrg + self.y_end + self.title_offset
300
301 self.dl_w = int((self.sizew-bdw)/7)
302 self.dl_h = int((self.sizeh-bdh)/7)
303
304 self.dl_th = int(self.dl_h*self.cal_week_scale) # week title adjustment
305 self.cwidth = self.dl_w * 7
306 self.cheight = self.dl_h * 6 + self.dl_th
307
308 def DrawSel(self): # highlighted selected days
309 for key in self.cal_sel.keys():
310 sel_color = self.cal_sel[key][1]
311 brush = wxBrush(wxNamedColour(sel_color), wxSOLID)
312 self.DC.SetBrush(brush)
313
314 if self.hide_grid is FALSE:
315 self.DC.SetPen(wxPen(wxNamedColour(self.grid_color), 0))
316 else:
317 self.DC.SetPen(wxPen(wxNamedColour(self.back_color), 0))
318 nkey = key + self.st_pos -1
319 rect = self.rg[nkey]
320 self.DC.DrawRectangle(rect.x, rect.y, rect.width+1, rect.height+1)
321
322 def DrawGrid(self): # calculate and draw the grid lines
323 self.DC.SetPen(wxPen(wxNamedColour(self.grid_color), 0))
324
325 self.gridx = []
326 self.gridy = []
327
328 self.x_st = self.cx_st + self.x_mrg
329 self.y_st = self.cy_st + self.y_mrg + self.title_offset # start postion of draw
330
331 x1 = self.x_st
332 y1 = self.y_st
333
334 y2 = y1 + self.cheight
335 for i in range(8):
336 if self.hide_grid is FALSE:
337 self.DC.DrawLine(x1, y1, x1, y2)
338 self.gridx.append(x1)
339 x1 = x1 + self.dl_w
340
341 x1 = self.x_st
342 y1 = self.y_st
343
344 x2 = x1 + self.cwidth
345 for i in range(8):
346 if self.hide_grid is FALSE:
347 self.DC.DrawLine(x1, y1, x2, y1)
348 self.gridy.append(y1)
349 if i == 0:
350 y1 = y1 + self.dl_th
351 else:
352 y1 = y1 + self.dl_h
353
354
355 class PrtCalDraw(CalDraw):
356 def InitValues(self):
357 self.rg = {}
358 self.cal_sel = {}
359 self.set_cx_st = 1.0 # start draw border location
360 self.set_cy_st = 1.0
361
362 self.set_y_mrg = 0.2 # draw offset position
363 self.set_x_mrg = 0.2
364 self.set_y_end = 0.2
365
366 def SetPSize(self, pwidth, pheight): # calculate the dimensions in the center of the drawing area
367 self.pwidth = int(pwidth)/self.scale
368 self.pheight = int(pheight)/self.scale
369
370 def SetPreview(self, preview):
371 self.preview = preview
372
373 class wxCalendar(wxWindow):
374 def __init__(self, parent, id, pos=wxDefaultPosition, size=wxDefaultSize):
375 wxWindow.__init__(self, parent, id, pos, size)
376
377 # set the calendar control attributes
378
379 self.grid_color = 'BLACK'
380 self.back_color = 'WHITE'
381 self.hide_grid = FALSE
382 self.sel_color = 'RED'
383 self.hide_title = FALSE
384 self.show_weekend = FALSE
385 self.cal_type = "NORMAL"
386
387 self.week_color = 'LIGHT GREY'
388 self.week_font_color = 'BLACK' # font colors
389
390 self.select_list = []
391
392 self.SetBackgroundColour(wxNamedColor(self.back_color))
393 self.Connect(-1, -1, wxEVT_LEFT_DOWN, self.OnLeftEvent)
394 self.Connect(-1, -1, wxEVT_LEFT_DCLICK, self.OnLeftDEvent)
395 self.Connect(-1, -1, wxEVT_RIGHT_DOWN, self.OnRightEvent)
396 self.Connect(-1, -1, wxEVT_RIGHT_DCLICK, self.OnRightDEvent)
397
398 self.sel_key = None # last used by
399 self.sel_lst = [] # highlighted selected days
400
401 self.SetNow() # default calendar for current month
402
403 self.size = None
404 self.set_day = None
405
406 EVT_PAINT(self, self.OnPaint)
407
408
409 # control some of the main calendar attributes
410
411 def HideTitle(self):
412 self.hide_title = TRUE
413
414 def HideGrid(self):
415 self.hide_grid = TRUE
416
417 # determine the calendar rectangle click area and draw a selection
418
419 def ProcessClick(self, event):
420 self.x, self.y = event.GetX(), event.GetY()
421 key = self.GetDayHit(self.x, self.y)
422 self.SelectDay(key)
423
424 # tab mouse click events and process
425
426 def OnLeftEvent(self, event):
427 self.click = 'LEFT'
428 self.ProcessClick(event)
429
430 def OnLeftDEvent(self, event):
431 self.click = 'DLEFT'
432 self.ProcessClick(event)
433
434 def OnRightEvent(self, event):
435 self.click = 'RIGHT'
436 self.ProcessClick(event)
437
438 def OnRightDEvent(self, event):
439 self.click = 'DRIGHT'
440 self.ProcessClick(event)
441
442 def SetSize(self, set_size):
443 self.size = set_size
444
445 def SetSelDay(self, sel):
446 self.sel_lst = sel # list of highlighted days
447
448 # get the current date
449
450 def SetNow(self):
451 dt = now()
452 self.month = dt.month
453 self.year = dt.year
454 self.day = dt.day
455
456 # set the current day
457
458 def SetCurrentDay(self):
459 self.SetNow()
460 self.set_day = self.day
461
462 # get the date, day, month, year set in calendar
463
464 def GetDate(self):
465 return self.day, self.month, self.year
466
467 def GetDay(self):
468 return self.day
469
470 def GetMonth(self):
471 return self.month
472
473 def GetYear(self):
474 return self.year
475
476 # set the day, month, and year
477
478 def SetDayValue(self, day):
479 self.set_day = day
480
481 def SetMonth(self, month):
482 if month >= 1 and month <= 12:
483 self.month = month
484 else:
485 self.month = 1
486 self.set_day = None
487
488 def SetYear(self, year):
489 self.year = year
490
491 # increment year and month
492
493 def IncYear(self):
494 self.year = self.year + 1
495 self.set_day = None
496
497 def DecYear(self):
498 self.year = self.year - 1
499 self.set_day = None
500
501 def IncMonth(self):
502 self.month = self.month + 1
503 if self.month > 12:
504 self.month = 1
505 self.year = self.year + 1
506 self.set_day = None
507
508 def DecMonth(self):
509 self.month = self.month - 1
510 if self.month < 1:
511 self.month = 12
512 self.year = self.year - 1
513 self.set_day = None
514
515 # test to see if the selection has a date and create event
516
517 def TestDay(self, key):
518 try:
519 self.day = int(self.cal[key])
520 except:
521 return None
522 if self.day == "":
523 return None
524 else:
525 evt = wxPyCommandEvent(2100, self.GetId())
526 evt.click, evt.day, evt.month, evt.year = self.click, self.day, self.month, self.year
527 self.GetEventHandler().ProcessEvent(evt)
528
529 self.set_day = self.day
530 return key
531
532 # find the clicked area rectangle
533
534 def GetDayHit(self, mx, my):
535 for key in self.rg.keys():
536 val = self.rg[key]
537 ms_rect = wxRect(mx, my, 1, 1)
538 if wxIntersectRect(ms_rect, val) is not None:
539 result = self.TestDay(key)
540 return result
541 return None
542
543 # calendar drawing
544
545 def SetWeekColor(self, font_color, week_color): # set font and background color for week title
546 self.week_font_color = font_color
547 self.week_color = week_color
548
549 def AddSelect(self, list, font_color, back_color):
550 list_val = [list, font_color, back_color]
551 self.select_list.append(list_val)
552
553 def ShowWeekEnd(self):
554 self.show_weekend = TRUE # highlight weekend
555
556 def SetBusType(self):
557 self.cal_type = "BUS"
558
559 def OnPaint(self, event):
560 DC = wxPaintDC(self)
561 self.DoDrawing(DC)
562
563 def DoDrawing(self, DC):
564 DC = wxPaintDC(self)
565 DC.BeginDrawing()
566
567 self.cal = cal = CalDraw(self)
568
569 cal.grid_color = self.grid_color
570 cal.back_color = self.back_color
571 cal.hide_grid = self.hide_grid
572 cal.grid_color = self.grid_color
573 cal.hide_title = self.hide_title
574 cal.show_weekend = self.show_weekend
575 cal.cal_type = self.cal_type
576
577 if self.size is None:
578 size = self.GetClientSize()
579 else:
580 size = self.size
581
582 # drawing attributes
583
584 cal.week_font_color = self.week_font_color
585 cal.week_color = self.week_color
586
587 cal.SetSize(size)
588 cal.SetCal(self.year, self.month)
589 for val in self.select_list:
590 cal.AddSelect(val[0], val[1], val[2])
591
592 cal.DrawCal(DC, self.sel_lst)
593
594 self.rg = cal.GetRect()
595 self.cal = cal.GetCal()
596 self.st_pos = cal.GetOffset()
597 self.ymax = DC.MaxY()
598
599 if self.set_day != None:
600 self.SetDay(self.set_day)
601 DC.EndDrawing()
602
603 # draw the selection rectangle
604
605 def DrawRect(self, key, color = 'BLACK', width = 0):
606 if key == None:
607 return
608 DC = wxClientDC(self)
609 DC.BeginDrawing()
610
611 brush = wxBrush(wxColour(0, 0xFF, 0x80), wxTRANSPARENT)
612 DC.SetBrush(brush)
613 DC.SetPen(wxPen(wxNamedColour(color), width))
614
615 rect = self.rg[key]
616 DC.DrawRectangle(rect.x, rect.y, rect.width+1, rect.height+1)
617
618 DC.EndDrawing()
619
620 # set the day selection
621
622 def SetDay(self, day):
623 day = day + self.st_pos - 1
624 self.SelectDay(day)
625
626 def SelectDay(self, key):
627 sel_size = 1
628 self.DrawRect(self.sel_key, self.back_color, sel_size) # clear large selection
629 if self.hide_grid is FALSE:
630 self.DrawRect(self.sel_key, self.grid_color)
631
632 self.DrawRect(key, self.sel_color, sel_size)
633 self.sel_key = key # store last used by
634 self.select_day = None
635
636 def ClearDsp(self):
637 self.Clear()
638
639 class CalenDlg(wxDialog):
640 def __init__(self, parent, month=None, day = None, year=None):
641 wxDialog.__init__(self, parent, -1, "Event Calendar", wxPyDefaultPosition, wxSize(280, 360))
642
643 # set the calendar and attributes
644 self.calend = wxCalendar(self, -1, wxPoint(20, 60), wxSize(240, 200))
645 if month == None:
646 self.calend.SetCurrentDay()
647 start_month = self.calend.GetMonth()
648 start_year = self.calend.GetYear()
649 else:
650 self.calend.month = start_month = month
651 self.calend.year = start_year = year
652 self.calend.SetDayValue(day)
653
654 self.calend.HideTitle()
655 self.ResetDisplay()
656
657 # get month list from DateTime
658 monthlist = GetMonthList()
659
660 # select the month
661 mID = NewId()
662 self.date = wxComboBox(self, mID, Month[start_month], wxPoint(20, 20), wxSize(90, -1), monthlist, wxCB_DROPDOWN)
663 EVT_COMBOBOX(self, mID, self.EvtComboBox)
664
665 # alternate spin button to control the month
666 mID = NewId()
667 h = self.date.GetSize().height
668 self.m_spin = wxSpinButton(self, mID, wxPoint(130, 20), wxSize(h*2, h), wxSP_VERTICAL)
669 self.m_spin.SetRange(1, 12)
670 self.m_spin.SetValue(start_month)
671
672 EVT_SPIN(self, mID, self.OnMonthSpin)
673
674 # spin button to control the year
675 mID = NewId()
676 self.dtext = wxTextCtrl(self, -1, str(start_year), wxPoint(160, 20), wxSize(60, -1))
677 h = self.dtext.GetSize().height
678
679 self.y_spin = wxSpinButton(self, mID, wxPoint(220, 20), wxSize(h*2, h), wxSP_VERTICAL)
680 self.y_spin.SetRange(1980, 2010)
681 self.y_spin.SetValue(start_year)
682
683 EVT_SPIN(self, mID, self.OnYrSpin)
684
685 self.Connect(self.calend.GetId(), -1, 2100, self.MouseClick)
686
687 x_pos = 50
688 y_pos = 280
689 but_size = wxSize(60, 25)
690
691 mID = NewId()
692 wxButton(self, mID, ' Ok ', wxPoint(x_pos, y_pos), but_size)
693 EVT_BUTTON(self, mID, self.OnOk)
694
695 mID = NewId()
696 wxButton(self, mID, ' Close ', wxPoint(x_pos + 120, y_pos), but_size)
697 EVT_BUTTON(self, mID, self.OnCancel)
698
699 def OnOk(self, event):
700 self.EndModal(wxID_OK)
701
702 def OnCancel(self, event):
703 self.EndModal(wxID_CANCEL)
704
705 # log the mouse clicks
706 def MouseClick(self, evt):
707 self.month = evt.month
708 self.result = [evt.click, str(evt.day), Month[evt.month], str(evt.year)] # result click type and date
709
710 if evt.click == 'DLEFT':
711 self.EndModal(wxID_OK)
712
713 # month and year spin selection routines
714 def OnMonthSpin(self, event):
715 month = event.GetPosition()
716 self.date.SetValue(Month[month])
717 self.calend.SetMonth(month)
718 self.calend.Refresh()
719
720 def OnYrSpin(self, event):
721 year = event.GetPosition()
722 self.dtext.SetValue(str(year))
723 self.calend.SetYear(year)
724 self.calend.Refresh()
725
726 def EvtComboBox(self, event):
727 name = event.GetString()
728 monthval = self.date.FindString(name)
729 self.m_spin.SetValue(monthval+1)
730
731 self.calend.SetMonth(monthval+1)
732 self.ResetDisplay()
733
734 # set the calendar for highlighted days
735
736 def ResetDisplay(self):
737 month = self.calend.GetMonth()
738 self.calend.Refresh()
739
740
741