]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/calendar.py
Patch from Andrea: fixes no page chang* events with mouse right click,
[wxWidgets.git] / wxPython / wx / 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.92
9 # Date: Nov 26, 2001
10 # Licence: wxWindows license
11 #----------------------------------------------------------------------------
12 # 12/01/2003 - Jeff Grimmett (grimmtooth@softhome.net)
13 #
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
28 # demo, I suppose.
29 #
30 # Here's the traceback:
31 #
32 # C:\Python\lib\site-packages\wx\core.py:949: DeprecationWarning:
33 # integer argument expected, got float
34 # newobj = _core.new_Rect(*args, **kwargs)
35 #
36 # 12/17/2003 - Jeff Grimmett (grimmtooth@softhome.net)
37 #
38 # o A few style-guide nips and tucks
39 # o Renamed wxCalendar to Calendar
40 # o Couple of bugfixes
41 #
42 # 06/02/2004 - Joerg "Adi" Sieker adi@sieker.info
43 #
44 # o Changed color handling, use dictionary instead of members.
45 # This causes all color changes to be ignored if they manipluate the members directly.
46 # SetWeekColor and other method color methods were adapted to use the new dictionary.
47 # o Added COLOR_* constants
48 # o Added SetColor method for Calendar class
49 # o Added 3D look of week header
50 # o Added colors for 3D look of header
51 # o Fixed width calculation.
52 # Because of rounding difference the total width and height of the
53 # calendar could be up to 6 pixels to small. The last column and row
54 # are now wider/taller by the missing amount.
55 # o Added SetTextAlign method to wxCalendar. This exposes logic
56 # which was already there.
57 # o Fixed CalDraw.SetMarg which set set_x_st and set_y_st which don't get used anywhere.
58 # Instead set set_x_mrg and set_y_mrg
59 # o Changed default X and Y Margin to 0.
60 # o Added wxCalendar.SetMargin.
61 #
62 # 17/03/2004 - Joerg "Adi" Sieker adi@sieker.info
63 # o Added keyboard navigation to the control.
64 # Use the cursor keys to navigate through the ages. :)
65 # The Home key function as go to today
66 # o select day is now a filled rect instead of just an outline
67 #
68 # 15/04/2005 - Joe "shmengie" Brown joebrown@podiatryfl.com
69 # o Adjusted spin control size/placement (On Windows ctrls were overlapping).
70 # o Set Ok/Cancel buttons to wx.ID_OK & wx.ID_CANCEL to provide default dialog
71 # behaviour.
72 # o If no date has been clicked clicked, OnOk set the result to calend's date,
73 # important if keyboard only navigation is used.
74 #
75 # 12/10/2006 - Walter Barnes walter_barnes05@yahoo.com
76 # o Fixed CalDraw to properly render months that start on a Sunday.
77
78
79 import wx
80
81 from CDate import *
82
83 CalDays = [6, 0, 1, 2, 3, 4, 5]
84 AbrWeekday = {6:"Sun", 0:"Mon", 1:"Tue", 2:"Wed", 3:"Thu", 4:"Fri", 5:"Sat"}
85 _MIDSIZE = 180
86
87 COLOR_GRID_LINES = "grid_lines"
88 COLOR_BACKGROUND = "background"
89 COLOR_SELECTION_FONT = "selection_font"
90 COLOR_SELECTION_BACKGROUND = "selection_background"
91 COLOR_BORDER = "border"
92 COLOR_HEADER_BACKGROUND = "header_background"
93 COLOR_HEADER_FONT = "header_font"
94 COLOR_WEEKEND_BACKGROUND = "weekend_background"
95 COLOR_WEEKEND_FONT = "weekend_font"
96 COLOR_FONT = "font"
97 COLOR_3D_LIGHT = "3d_light"
98 COLOR_3D_DARK = "3d_dark"
99 COLOR_HIGHLIGHT_FONT = "highlight_font"
100 COLOR_HIGHLIGHT_BACKGROUND = "highlight_background"
101
102 BusCalDays = [0, 1, 2, 3, 4, 5, 6]
103
104 # Calendar click event - added 12/1/03 by jmg (see above)
105 wxEVT_COMMAND_PYCALENDAR_DAY_CLICKED = wx.NewEventType()
106 EVT_CALENDAR = wx.PyEventBinder(wxEVT_COMMAND_PYCALENDAR_DAY_CLICKED, 1)
107
108 def GetMonthList():
109 monthlist = []
110 for i in range(13):
111 name = Month[i]
112 if name != None:
113 monthlist.append(name)
114 return monthlist
115
116 def MakeColor(in_color):
117 try:
118 color = wxNamedColour(in_color)
119 except:
120 color = in_color
121 return color
122
123 def DefaultColors():
124 colors = {}
125 colors[COLOR_GRID_LINES] = 'BLACK'
126 colors[COLOR_BACKGROUND] = 'WHITE'
127 colors[COLOR_SELECTION_FONT] = wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOWTEXT)
128 colors[COLOR_SELECTION_BACKGROUND] =wx.Colour(255,255,225)
129 colors[COLOR_BORDER] = 'BLACK'
130 colors[COLOR_HEADER_BACKGROUND] = wx.SystemSettings_GetColour(wx.SYS_COLOUR_3DFACE)
131 colors[COLOR_HEADER_FONT] = wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOWTEXT)
132 colors[COLOR_WEEKEND_BACKGROUND] = 'LIGHT GREY'
133 colors[COLOR_WEEKEND_FONT] = wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOWTEXT)
134 colors[COLOR_FONT] = wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOWTEXT)
135 colors[COLOR_3D_LIGHT] = wx.SystemSettings_GetColour(wx.SYS_COLOUR_BTNHIGHLIGHT)
136 colors[COLOR_3D_DARK] = wx.SystemSettings_GetColour(wx.SYS_COLOUR_BTNSHADOW)
137 colors[COLOR_HIGHLIGHT_FONT] = wx.SystemSettings_GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT)
138 colors[COLOR_HIGHLIGHT_BACKGROUND] = wx.SystemSettings_GetColour(wx.SYS_COLOUR_HIGHLIGHT)
139 return colors
140 # calendar drawing routing
141
142 class CalDraw:
143 def __init__(self, parent):
144 self.pwidth = 1
145 self.pheight = 1
146 try:
147 self.scale = parent.scale
148 except:
149 self.scale = 1
150
151 self.gridx = []
152 self.gridy = []
153
154 self.DefParms()
155
156 def DefParms(self):
157 self.num_auto = True # auto scale of the cal number day size
158 self.num_size = 12 # default size of calendar if no auto size
159 self.max_num_size = 12 # maximum size for calendar number
160
161 self.num_align_horz = wx.ALIGN_CENTRE # alignment of numbers
162 self.num_align_vert = wx.ALIGN_CENTRE
163 self.num_indent_horz = 0 # points indent from position, used to offset if not centered
164 self.num_indent_vert = 0
165
166 self.week_auto = True # auto scale of week font text
167 self.week_size = 10
168 self.max_week_size = 12
169
170 self.colors = DefaultColors()
171
172 self.font = wx.SWISS
173 self.bold = wx.NORMAL
174
175 self.hide_title = False
176 self.hide_grid = False
177 self.outer_border = True
178
179 self.title_offset = 0
180 self.cal_week_scale = 0.7
181 self.show_weekend = False
182 self.cal_type = "NORMAL"
183
184 def SetWeekColor(self, font_color, week_color):
185 # set font and background color for week title
186 self.colors[COLOR_HEADER_FONT] = MakeColor(font_color)
187 self.colors[COLOR_HEADER_BACKGROUND] = MakeColor(week_color)
188 self.colors[COLOR_3D_LIGHT] = MakeColor(week_color)
189 self.colors[COLOR_3D_DARK] = MakeColor(week_color)
190
191 def SetSize(self, size):
192 self.set_sizew = size[0]
193 self.set_sizeh = size[1]
194
195 def InitValues(self): # default dimensions of various elements of the calendar
196 self.rg = {}
197 self.cal_sel = {}
198 self.set_cy_st = 0 # start position
199 self.set_cx_st = 0
200
201 self.set_y_mrg = 1 # start of vertical draw default
202 self.set_x_mrg = 1
203 self.set_y_end = 1
204 def SetPos(self, xpos, ypos):
205 self.set_cx_st = xpos
206 self.set_cy_st = ypos
207
208 def SetMarg(self, xmarg, ymarg):
209 self.set_x_mrg = xmarg
210 self.set_y_mrg = ymarg
211 self.set_y_end = ymarg
212
213 def InitScale(self): # scale position values
214 self.sizew = int(self.set_sizew * self.pwidth)
215 self.sizeh = int(self.set_sizeh * self.pheight)
216
217 self.cx_st = int(self.set_cx_st * self.pwidth) # draw start position
218 self.cy_st = int(self.set_cy_st * self.pheight)
219
220 self.x_mrg = int(self.set_x_mrg * self.pwidth) # calendar draw margins
221 self.y_mrg = int(self.set_y_mrg * self.pheight)
222 self.y_end = int(self.set_y_end * self.pheight)
223
224 def DrawCal(self, DC, sel_lst=[]):
225 self.InitScale()
226
227 self.DrawBorder(DC)
228
229 if self.hide_title is False:
230 self.DrawMonth(DC)
231
232 self.Center()
233
234 self.DrawGrid(DC)
235 self.GetRect()
236 if self.show_weekend is True: # highlight weekend dates
237 self.SetWeekEnd()
238
239 self.AddSelect(sel_lst) # overrides the weekend highlight
240
241 self.DrawSel(DC) # highlighted days
242 self.DrawWeek(DC)
243 self.DrawNum(DC)
244
245 def AddSelect(self, list, cfont=None, cbackgrd = None):
246 if cfont is None:
247 cfont = self.colors[COLOR_SELECTION_FONT] # font digit color
248 if cbackgrd is None:
249 cbackgrd = self.colors[COLOR_SELECTION_BACKGROUND] # select background color
250
251 for val in list:
252 self.cal_sel[val] = (cfont, cbackgrd)
253
254 # draw border around the outside of the main display rectangle
255 def DrawBorder(self, DC, transparent = False):
256 if self.outer_border is True:
257 if transparent == False:
258 brush = wx.Brush(MakeColor(self.colors[COLOR_BACKGROUND]), wx.SOLID)
259 else:
260 brush = wx.TRANSPARENT_BRUSH
261 DC.SetBrush(brush)
262 DC.SetPen(wx.Pen(MakeColor(self.colors[COLOR_BORDER])))
263 # full display window area
264 rect = wx.Rect(self.cx_st, self.cy_st, self.sizew, self.sizeh)
265 DC.DrawRectangleRect(rect)
266
267 def DrawFocusIndicator(self, DC):
268 if self.outer_border is True:
269 DC.SetBrush(wx.TRANSPARENT_BRUSH)
270 DC.SetPen(wx.Pen(MakeColor(self.colors[COLOR_HIGHLIGHT_BACKGROUND]), style=wx.DOT))
271 # full display window area
272 rect = wx.Rect(self.cx_st, self.cy_st, self.sizew, self.sizeh)
273 DC.DrawRectangleRect(rect)
274
275 def DrawNumVal(self):
276 self.DrawNum()
277
278 # calculate the calendar days and offset position
279 def SetCal(self, year, month):
280 self.InitValues() # reset initial values
281
282 self.year = year
283 self.month = month
284
285 day = 1
286 t = Date(year, month, day)
287 dow = self.dow = t.day_of_week # start day in month
288 dim = self.dim = t.days_in_month # number of days in month
289
290 if self.cal_type == "NORMAL":
291 start_pos = dow+1
292 else:
293 start_pos = dow
294
295 if start_pos > 6:
296 start_pos = 0
297
298 self.st_pos = start_pos
299
300 self.cal_days = []
301 for i in range(start_pos):
302 self.cal_days.append('')
303
304 i = 1
305 while i <= dim:
306 self.cal_days.append(str(i))
307 i = i + 1
308
309 self.end_pos = start_pos + dim - 1
310
311 return start_pos
312
313 def SetWeekEnd(self, font_color=None, backgrd = None):
314 if font_color != None:
315 self.SetColor(COLOR_WEEKEND_FONT, MakeColor(font_color))
316 if backgrd != None:
317 self.SetColor(COLOR_WEEKEND_BACKGROUND, MakeColor(backgrd))
318
319 date = 6 - int(self.dow) # start day of first saturday
320 if date == 0: #...unless we start on Sunday
321 self.cal_sel[1] = (self.GetColor(COLOR_WEEKEND_FONT), self.GetColor(COLOR_WEEKEND_BACKGROUND))
322 date = 7
323
324 while date <= self.dim:
325 self.cal_sel[date] = (self.GetColor(COLOR_WEEKEND_FONT), self.GetColor(COLOR_WEEKEND_BACKGROUND)) # Saturday
326 date = date + 1
327
328 if date <= self.dim:
329 self.cal_sel[date] = (self.GetColor(COLOR_WEEKEND_FONT), self.GetColor(COLOR_WEEKEND_BACKGROUND)) # Sunday
330 date = date + 6
331 else:
332 date = date + 7
333
334 # get the display rectange list of the day grid
335 def GetRect(self):
336 cnt = 0
337 h = 0
338 w = 0
339 for y in self.gridy[1:-1]:
340 if y == self.gridy[-2]:
341 h = h + self.restH
342
343 for x in self.gridx[:-1]:
344 assert type(y) == int
345 assert type(x) == int
346
347 w = self.cellW
348 h = self.cellH
349
350 if x == self.gridx[-2]:
351 w = w + self.restW
352
353 rect = wx.Rect(x, y, w+1, h+1) # create rect region
354
355 self.rg[cnt] = rect
356 cnt = cnt + 1
357
358 return self.rg
359
360 def GetCal(self):
361 return self.cal_days
362
363 def GetOffset(self):
364 return self.st_pos
365
366 # month and year title
367 def DrawMonth(self, DC):
368 month = Month[self.month]
369
370 sizef = 11
371 if self.sizeh < _MIDSIZE:
372 sizef = 10
373
374 f = wx.Font(sizef, self.font, wx.NORMAL, self.bold)
375 DC.SetFont(f)
376
377 tw,th = DC.GetTextExtent(month)
378 adjust = self.cx_st + (self.sizew-tw)/2
379 DC.DrawText(month, adjust, self.cy_st + th)
380
381 year = str(self.year)
382 tw,th = DC.GetTextExtent(year)
383 adjust = self.sizew - tw - self.x_mrg
384
385 self.title_offset = th * 2
386
387 f = wx.Font(sizef, self.font, wx.NORMAL, self.bold)
388 DC.SetFont(f)
389 DC.DrawText(year, self.cx_st + adjust, self.cy_st + th)
390
391 def DrawWeek(self, DC): # draw the week days
392 # increase by 1 to include all gridlines
393 width = self.gridx[1] - self.gridx[0] + 1
394 height = self.gridy[1] - self.gridy[0] + 1
395 rect_w = self.gridx[-1] - self.gridx[0]
396
397 f = wx.Font(10, self.font, wx.NORMAL, self.bold) # initial font setting
398
399 if self.week_auto == True:
400 test_size = self.max_week_size # max size
401 test_day = ' Sun '
402 while test_size > 2:
403 f.SetPointSize(test_size)
404 DC.SetFont(f)
405 tw,th = DC.GetTextExtent(test_day)
406
407 if tw < width and th < height:
408 break
409
410 test_size = test_size - 1
411 else:
412 f.SetPointSize(self.week_size) # set fixed size
413 DC.SetFont(f)
414
415 DC.SetTextForeground(MakeColor(self.colors[COLOR_HEADER_FONT]))
416
417 cnt_x = 0
418 cnt_y = 0
419
420 brush = wx.Brush(MakeColor(self.colors[COLOR_HEADER_BACKGROUND]), wx.SOLID)
421 DC.SetBrush(brush)
422
423 if self.cal_type == "NORMAL":
424 cal_days = CalDays
425 else:
426 cal_days = BusCalDays
427
428 for val in cal_days:
429 if val == cal_days[-1]:
430 width = width + self.restW
431
432 day = AbrWeekday[val]
433
434 if self.sizew < 200:
435 day = day[0]
436
437 dw,dh = DC.GetTextExtent(day)
438
439 diffx = (width-dw)/2
440 diffy = (height-dh)/2
441
442 x = self.gridx[cnt_x]
443 y = self.gridy[cnt_y]
444 pointXY = (x, y)
445 pointWH = (width, height)
446 if self.hide_grid == False:
447 pen = wx.Pen(MakeColor(self.GetColor(COLOR_GRID_LINES)), 1, wx.SOLID)
448 else:
449 pen = wx.Pen(MakeColor(self.GetColor(COLOR_BACKGROUND)), 1, wx.SOLID)
450 DC.SetPen(pen)
451 DC.DrawRectanglePointSize( pointXY, pointWH)
452
453 old_pen = DC.GetPen()
454
455 pen = wx.Pen(MakeColor(self.colors[COLOR_3D_LIGHT]), 1, wx.SOLID)
456 DC.SetPen(pen)
457 # draw the horizontal hilight
458 startPoint = wx.Point(x + 1 , y + 1)
459 endPoint = wx.Point(x + width - 1, y + 1)
460 DC.DrawLinePoint(startPoint, endPoint )
461
462 # draw the vertical hilight
463 startPoint = wx.Point(x + 1 , y + 1)
464 endPoint = wx.Point(x + 1, y + height - 2)
465 DC.DrawLinePoint(startPoint, endPoint )
466
467 pen = wx.Pen(MakeColor(self.colors[COLOR_3D_DARK]), 1, wx.SOLID)
468 DC.SetPen(pen)
469
470 # draw the horizontal lowlight
471 startPoint = wx.Point(x + 1, y + height - 2)
472 endPoint = wx.Point(x + width - 1, y + height - 2)
473 DC.DrawLinePoint(startPoint, endPoint )
474
475 # draw the vertical lowlight
476 startPoint = wx.Point(x + width - 2 , y + 2)
477 endPoint = wx.Point(x + width - 2, y + height - 2)
478 DC.DrawLinePoint(startPoint, endPoint )
479
480 pen = wx.Pen(MakeColor(self.colors[COLOR_FONT]), 1, wx.SOLID)
481
482 DC.SetPen(pen)
483
484 point = (x+diffx, y+diffy)
485 DC.DrawTextPoint(day, point)
486 cnt_x = cnt_x + 1
487
488 def _CalcFontSize(self, DC, f):
489 if self.num_auto == True:
490 test_size = self.max_num_size # max size
491 test_day = ' 99 '
492
493 while test_size > 2:
494 f.SetPointSize(test_size)
495 DC.SetFont(f)
496 tw,th = DC.GetTextExtent(test_day)
497
498 if tw < self.cellW and th < self.cellH:
499 sizef = test_size
500 break
501 test_size = test_size - 1
502 else:
503 f.SetPointSize(self.num_size) # set fixed size
504 DC.SetFont(f)
505
506 # draw the day numbers
507 def DrawNum(self, DC):
508 f = wx.Font(10, self.font, wx.NORMAL, self.bold) # initial font setting
509 self._CalcFontSize(DC, f)
510
511 cnt_x = 0
512 cnt_y = 1
513 for val in self.cal_days:
514 x = self.gridx[cnt_x]
515 y = self.gridy[cnt_y]
516
517 self._DrawDayText(x, y, val, f, DC)
518
519 if cnt_x < 6:
520 cnt_x = cnt_x + 1
521 else:
522 cnt_x = 0
523 cnt_y = cnt_y + 1
524
525 def _DrawDayText(self, x, y, text, font, DC):
526
527 try:
528 num_val = int(text)
529 num_color = self.cal_sel[num_val][0]
530 except:
531 num_color = self.colors[COLOR_FONT]
532
533 DC.SetTextForeground(MakeColor(num_color))
534 DC.SetFont(font)
535
536 tw,th = DC.GetTextExtent(text)
537
538 if self.num_align_horz == wx.ALIGN_CENTRE:
539 adj_h = (self.cellW - tw)/2
540 elif self.num_align_horz == wx.ALIGN_RIGHT:
541 adj_h = self.cellW - tw
542 else:
543 adj_h = 0 # left alignment
544
545 adj_h = adj_h + self.num_indent_horz
546
547 if self.num_align_vert == wx.ALIGN_CENTRE:
548 adj_v = (self.cellH - th)/2
549 elif self.num_align_vert == wx.ALIGN_BOTTOM:
550 adj_v = self.cellH - th
551 else:
552 adj_v = 0 # left alignment
553
554 adj_v = adj_v + self.num_indent_vert
555
556 DC.DrawTextPoint(text, (x+adj_h, y+adj_v))
557
558 def DrawDayText(self, DC, key):
559 f = wx.Font(10, self.font, wx.NORMAL, self.bold) # initial font setting
560 self._CalcFontSize(DC, f)
561
562 if key > self.end_pos:
563 key = self.end_pos
564
565 val = self.cal_days[key]
566 cnt_x = key % 7
567 cnt_y = int(key / 7)+1
568 x = self.gridx[cnt_x]
569 y = self.gridy[cnt_y]
570 self._DrawDayText(x, y, val, f, DC)
571
572
573 # calculate the dimensions in the center of the drawing area
574 def Center(self):
575 borderW = self.x_mrg * 2
576 borderH = self.y_mrg + self.y_end + self.title_offset
577
578 self.cellW = int((self.sizew - borderW)/7)
579 self.cellH = int((self.sizeh - borderH)/7)
580
581 self.restW = ((self.sizew - borderW)%7 ) - 1
582
583 # week title adjustment
584 self.weekHdrCellH = int(self.cellH * self.cal_week_scale)
585 # recalculate the cell height exkl. the week header and
586 # subtracting the size
587 self.cellH = int((self.sizeh - borderH - self.weekHdrCellH)/6)
588
589 self.restH = ((self.sizeh - borderH - self.weekHdrCellH)%6 ) - 1
590 self.calW = self.cellW * 7
591 self.calH = self.cellH * 6 + self.weekHdrCellH
592
593 # highlighted selected days
594 def DrawSel(self, DC):
595
596 for key in self.cal_sel.keys():
597 sel_color = self.cal_sel[key][1]
598 brush = wx.Brush(MakeColor(sel_color), wx.SOLID)
599 DC.SetBrush(brush)
600
601 if self.hide_grid is False:
602 DC.SetPen(wx.Pen(MakeColor(self.colors[COLOR_GRID_LINES]), 0))
603 else:
604 DC.SetPen(wx.Pen(MakeColor(self.colors[COLOR_BACKGROUND]), 0))
605
606 nkey = key + self.st_pos -1
607 rect = self.rg[nkey]
608
609 DC.DrawRectangleRect(rect)
610
611 # calculate and draw the grid lines
612 def DrawGrid(self, DC):
613 DC.SetPen(wx.Pen(MakeColor(self.colors[COLOR_GRID_LINES]), 0))
614
615 self.gridx = []
616 self.gridy = []
617
618 self.x_st = self.cx_st + self.x_mrg
619 # start postion of draw
620 self.y_st = self.cy_st + self.y_mrg + self.title_offset
621
622 x1 = self.x_st
623 y1 = self.y_st
624 y2 = y1 + self.calH + self.restH
625
626 for i in range(8):
627 if i == 7:
628 x1 = x1 + self.restW
629
630 if self.hide_grid is False:
631 DC.DrawLinePoint((x1, y1), (x1, y2))
632
633 self.gridx.append(x1)
634
635 x1 = x1 + self.cellW
636
637 x1 = self.x_st
638 y1 = self.y_st
639 x2 = x1 + self.calW + self.restW
640
641 for i in range(8):
642 if i == 7:
643 y1 = y1 + self.restH
644
645 if self.hide_grid is False:
646 DC.DrawLinePoint((x1, y1), (x2, y1))
647
648 self.gridy.append(y1)
649
650 if i == 0:
651 y1 = y1 + self.weekHdrCellH
652 else:
653 y1 = y1 + self.cellH
654
655 def GetColor(self, name):
656 return MakeColor(self.colors[name])
657
658 def SetColor(self, name, value):
659 self.colors[name] = MakeColor(value)
660
661 class PrtCalDraw(CalDraw):
662 def InitValues(self):
663 self.rg = {}
664 self.cal_sel = {}
665 # start draw border location
666 self.set_cx_st = 1.0
667 self.set_cy_st = 1.0
668
669 # draw offset position
670 self.set_y_mrg = 0.2
671 self.set_x_mrg = 0.2
672 self.set_y_end = 0.2
673
674 # calculate the dimensions in the center of the drawing area
675 def SetPSize(self, pwidth, pheight):
676 self.pwidth = int(pwidth)/self.scale
677 self.pheight = int(pheight)/self.scale
678
679 def SetPreview(self, preview):
680 self.preview = preview
681
682 class Calendar( wx.PyControl ):
683 def __init__(self, parent, id, pos=wx.DefaultPosition, size=wx.Size(200,200),
684 style= 0, validator=wx.DefaultValidator,
685 name= "calendar"):
686 wx.PyControl.__init__(self, parent, id, pos, size, style | wx.WANTS_CHARS, validator, name)
687
688 self.hasFocus = False
689 # set the calendar control attributes
690
691 self.hide_grid = False
692 self.hide_title = False
693 self.show_weekend = False
694 self.cal_type = "NORMAL"
695 self.outer_border = True
696 self.num_align_horz = wx.ALIGN_CENTRE
697 self.num_align_vert = wx.ALIGN_CENTRE
698 self.colors = DefaultColors()
699 self.set_x_mrg = 1
700 self.set_y_mrg = 1
701 self.set_y_end = 1
702
703 self.select_list = []
704
705 self.SetBackgroundColour(MakeColor(self.colors[COLOR_BACKGROUND]))
706 self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftEvent)
707 self.Bind(wx.EVT_LEFT_DCLICK, self.OnLeftDEvent)
708 self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightEvent)
709 self.Bind(wx.EVT_RIGHT_DCLICK, self.OnRightDEvent)
710 self.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus)
711 self.Bind(wx.EVT_KILL_FOCUS, self.OnKillFocus)
712 self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
713
714 self.sel_key = None # last used by
715 self.sel_lst = [] # highlighted selected days
716
717 # default calendar for current month
718 self.SetNow()
719
720 self.size = None
721 self.set_day = None
722
723 self.Bind(wx.EVT_PAINT, self.OnPaint)
724 self.Bind(wx.EVT_SIZE, self.OnSize)
725
726 def AcceptsFocus(self):
727 return self.IsShown() and self.IsEnabled()
728
729 def GetColor(self, name):
730 return MakeColor(self.colors[name])
731
732 def SetColor(self, name, value):
733 self.colors[name] = MakeColor(value)
734
735 # control some of the main calendar attributes
736
737 def HideTitle(self):
738 self.hide_title = True
739
740 def HideGrid(self):
741 self.hide_grid = True
742
743 # determine the calendar rectangle click area and draw a selection
744
745 def ProcessClick(self, event):
746 self.x, self.y = event.GetX(), event.GetY()
747 key = self.GetDayHit(self.x, self.y)
748 self.SelectDay(key)
749
750 # tab mouse click events and process
751
752 def OnLeftEvent(self, event):
753 self.click = 'LEFT'
754 self.shiftkey = event.ShiftDown()
755 self.ctrlkey = event.ControlDown()
756 self.ProcessClick(event)
757
758 def OnLeftDEvent(self, event):
759 self.click = 'DLEFT'
760 self.ProcessClick(event)
761
762 def OnRightEvent(self, event):
763 self.click = 'RIGHT'
764 self.ProcessClick(event)
765
766 def OnRightDEvent(self, event):
767 self.click = 'DRIGHT'
768 self.ProcessClick(event)
769
770 def OnSetFocus(self, event):
771 self.hasFocus = True
772 self.DrawFocusIndicator(True)
773
774 def OnKillFocus(self, event):
775 self.hasFocus = False
776 self.DrawFocusIndicator(False)
777
778 def OnKeyDown(self, event):
779 if not self.hasFocus:
780 event.Skip()
781 return
782
783 key_code = event.GetKeyCode()
784
785 if key_code == wx.WXK_TAB:
786 forward = not event.ShiftDown()
787 ne = wx.NavigationKeyEvent()
788 ne.SetDirection(forward)
789 ne.SetCurrentFocus(self)
790 ne.SetEventObject(self)
791 self.GetParent().GetEventHandler().ProcessEvent(ne)
792 event.Skip()
793 return
794
795 delta = None
796
797 if key_code == wx.WXK_UP:
798 delta = -7
799 elif key_code == wx.WXK_DOWN:
800 delta = 7
801 elif key_code == wx.WXK_LEFT:
802 delta = -1
803 elif key_code == wx.WXK_RIGHT:
804 delta = 1
805 elif key_code == wx.WXK_HOME:
806 curDate = wx.DateTimeFromDMY(int(self.cal_days[self.sel_key]),self.month - 1,self.year)
807 newDate = wx.DateTime_Now()
808 ts = newDate - curDate
809 delta = ts.GetDays()
810
811 if delta <> None:
812 curDate = wx.DateTimeFromDMY(int(self.cal_days[self.sel_key]),self.month - 1,self.year)
813 timeSpan = wx.TimeSpan_Days(delta)
814 newDate = curDate + timeSpan
815
816 if curDate.GetMonth() == newDate.GetMonth():
817 self.set_day = newDate.GetDay()
818 key = self.sel_key + delta
819 self.SelectDay(key)
820 else:
821 self.month = newDate.GetMonth() + 1
822 self.year = newDate.GetYear()
823 self.set_day = newDate.GetDay()
824 self.sel_key = None
825 self.DoDrawing(wx.ClientDC(self))
826
827 event.Skip()
828
829 def SetSize(self, set_size):
830 self.size = set_size
831
832 def SetSelDay(self, sel):
833 # list of highlighted days
834 self.sel_lst = sel
835
836 # get the current date
837 def SetNow(self):
838 dt = now()
839 self.month = dt.month
840 self.year = dt.year
841 self.day = dt.day
842
843 # set the current day
844 def SetCurrentDay(self):
845 self.SetNow()
846 self.set_day = self.day
847
848 # get the date, day, month, year set in calendar
849
850 def GetDate(self):
851 return self.day, self.month, self.year
852
853 def GetDay(self):
854 return self.day
855
856 def GetMonth(self):
857 return self.month
858
859 def GetYear(self):
860 return self.year
861
862 # set the day, month, and year
863
864 def SetDayValue(self, day):
865 self.set_day = day
866 self.day = day
867
868 def SetMonth(self, month):
869 if month >= 1 and month <= 12:
870 self.month = month
871 else:
872 self.month = 1
873 self.set_day = None
874
875 def SetYear(self, year):
876 self.year = year
877
878 # increment year and month
879
880 def IncYear(self):
881 self.year = self.year + 1
882 self.set_day = None
883
884 def DecYear(self):
885 self.year = self.year - 1
886 self.set_day = None
887
888 def IncMonth(self):
889 self.month = self.month + 1
890 if self.month > 12:
891 self.month = 1
892 self.year = self.year + 1
893 self.set_day = None
894
895 def DecMonth(self):
896 self.month = self.month - 1
897 if self.month < 1:
898 self.month = 12
899 self.year = self.year - 1
900 self.set_day = None
901
902 # test to see if the selection has a date and create event
903
904 def TestDay(self, key):
905 try:
906 self.day = int(self.cal_days[key])
907 except:
908 return None
909
910 if self.day == "":
911 return None
912 else:
913 # Changed 12/1/03 by jmg (see above) to support 2.5 event binding
914 evt = wx.PyCommandEvent(wxEVT_COMMAND_PYCALENDAR_DAY_CLICKED, self.GetId())
915 evt.click, evt.day, evt.month, evt.year = self.click, self.day, self.month, self.year
916 evt.shiftkey = self.shiftkey
917 evt.ctrlkey = self.ctrlkey
918 self.GetEventHandler().ProcessEvent(evt)
919
920 self.set_day = self.day
921 return key
922
923 # find the clicked area rectangle
924
925 def GetDayHit(self, mx, my):
926 for key in self.rg.keys():
927 val = self.rg[key]
928 ms_rect = wx.Rect(mx, my, 1, 1)
929 if wx.IntersectRect(ms_rect, val) is not None:
930 result = self.TestDay(key)
931 return result
932
933 return None
934
935 # calendar drawing
936
937 def SetWeekColor(self, font_color, week_color):
938 # set font and background color for week title
939 self.colors[COLOR_HEADER_FONT] = MakeColor(font_color)
940 self.colors[COLOR_HEADER_BACKGROUND] = MakeColor(week_color)
941 self.colors[COLOR_3D_LIGHT] = MakeColor(week_color)
942 self.colors[COLOR_3D_DARK] = MakeColor(week_color)
943
944 def SetTextAlign(self, vert, horz):
945 self.num_align_horz = horz
946 self.num_align_vert = vert
947
948 def AddSelect(self, list, font_color, back_color):
949 list_val = [list, font_color, back_color]
950 self.select_list.append(list_val)
951
952 def ShowWeekEnd(self):
953 # highlight weekend
954 self.show_weekend = True
955
956 def SetBusType(self):
957 self.cal_type = "BUS"
958
959 def OnSize(self, evt):
960 self.Refresh(False)
961 evt.Skip()
962
963 def OnPaint(self, event):
964 DC = wx.PaintDC(self)
965 self.DoDrawing(DC)
966
967 def DoDrawing(self, DC):
968 #DC = wx.PaintDC(self)
969 DC.BeginDrawing()
970
971 try:
972 cal = self.caldraw
973 except:
974 self.caldraw = CalDraw(self)
975 cal = self.caldraw
976
977 cal.hide_grid = self.hide_grid
978 cal.hide_title = self.hide_title
979 cal.show_weekend = self.show_weekend
980 cal.cal_type = self.cal_type
981 cal.outer_border = self.outer_border
982 cal.num_align_horz = self.num_align_horz
983 cal.num_align_vert = self.num_align_vert
984 cal.colors = self.colors
985
986 if self.size is None:
987 size = self.GetClientSize()
988 else:
989 size = self.size
990
991 # drawing attributes
992
993 cal.SetSize(size)
994 cal.SetCal(self.year, self.month)
995
996 # these have to set after SetCal as SetCal would overwrite them again.
997 cal.set_x_mrg = self.set_x_mrg
998 cal.set_y_mrg = self.set_y_mrg
999 cal.set_y_end = self.set_y_end
1000
1001 for val in self.select_list:
1002 cal.AddSelect(val[0], val[1], val[2])
1003
1004 cal.DrawCal(DC, self.sel_lst)
1005
1006 self.rg = cal.GetRect()
1007 self.cal_days = cal.GetCal()
1008 self.st_pos = cal.GetOffset()
1009 self.ymax = DC.MaxY()
1010
1011 if self.set_day != None:
1012 self.SetDay(self.set_day)
1013
1014 DC.EndDrawing()
1015
1016 # draw the selection rectangle
1017 def DrawFocusIndicator(self, draw):
1018 DC = wx.ClientDC(self)
1019 try:
1020 if draw == True:
1021 self.caldraw.DrawFocusIndicator(DC)
1022 else:
1023 self.caldraw.DrawBorder(DC,True)
1024 except:
1025 pass
1026
1027 def DrawRect(self, key, bgcolor = 'WHITE', fgcolor= 'PINK',width = 0):
1028 if key == None:
1029 return
1030
1031 DC = wx.ClientDC(self)
1032 DC.BeginDrawing()
1033
1034 brush = wx.Brush(MakeColor(bgcolor))
1035 DC.SetBrush(brush)
1036
1037 DC.SetPen(wx.TRANSPARENT_PEN)
1038
1039 rect = self.rg[key]
1040 DC.DrawRectangle(rect.x+1, rect.y+1, rect.width-2, rect.height-2)
1041
1042 self.caldraw.DrawDayText(DC,key)
1043
1044 DC.EndDrawing()
1045
1046 def DrawRectOrg(self, key, fgcolor = 'BLACK', width = 0):
1047 if key == None:
1048 return
1049
1050 DC = wx.ClientDC(self)
1051 DC.BeginDrawing()
1052
1053 brush = wx.Brush(wx.Colour(0, 0xFF, 0x80), wx.TRANSPARENT)
1054 DC.SetBrush(brush)
1055
1056 try:
1057 DC.SetPen(wx.Pen(MakeColor(fgcolor), width))
1058 except:
1059 DC.SetPen(wx.Pen(MakeColor(self.GetColor(COLOR_GRID_LINES)), width))
1060
1061 rect = self.rg[key]
1062 DC.DrawRectangleRect(rect)
1063
1064 DC.EndDrawing()
1065
1066 # set the day selection
1067
1068 def SetDay(self, day):
1069 d = day + self.st_pos - 1
1070 self.SelectDay(d)
1071
1072 def IsDayInWeekend(self, key):
1073 try:
1074 t = Date(self.year, self.month, 1)
1075
1076 day = self.cal_days[key]
1077 day = int(day) + t.day_of_week
1078
1079 if day % 7 == 6 or day % 7 == 0:
1080 return True
1081 except:
1082 return False
1083
1084 def SelectDay(self, key):
1085 sel_size = 1
1086 # clear large selection
1087
1088 if self.sel_key != None:
1089 (cfont, bgcolor) = self.__GetColorsForDay(self.sel_key)
1090 self.DrawRect(self.sel_key, bgcolor,cfont, sel_size)
1091
1092 self.DrawRect(key, self.GetColor(COLOR_HIGHLIGHT_BACKGROUND), self.GetColor(COLOR_HIGHLIGHT_FONT), sel_size)
1093 # store last used by
1094 self.sel_key = key
1095
1096 def ClearDsp(self):
1097 self.Clear()
1098 def SetMargin(self, xmarg, ymarg):
1099 self.set_x_mrg = xmarg
1100 self.set_y_mrg = ymarg
1101 self.set_y_end = ymarg
1102 def __GetColorsForDay(self, key):
1103 cfont = self.GetColor(COLOR_FONT)
1104 bgcolor = self.GetColor(COLOR_BACKGROUND)
1105
1106 if self.IsDayInWeekend(key) is True and self.show_weekend is True:
1107 cfont = self.GetColor(COLOR_WEEKEND_FONT)
1108 bgcolor = self.GetColor(COLOR_WEEKEND_BACKGROUND)
1109
1110 try:
1111 dayIdx = int(self.cal_days[key])
1112 (cfont, bgcolor) = self.caldraw.cal_sel[dayIdx]
1113 except:
1114 pass
1115
1116 return (cfont, bgcolor)
1117
1118 class CalenDlg(wx.Dialog):
1119 def __init__(self, parent, month=None, day = None, year=None):
1120 wx.Dialog.__init__(self, parent, -1, "Event Calendar", wx.DefaultPosition, (280, 360))
1121 self.result = None
1122
1123 # set the calendar and attributes
1124 self.calend = Calendar(self, -1, (20, 60), (240, 200))
1125
1126 if month == None:
1127 self.calend.SetCurrentDay()
1128 start_month = self.calend.GetMonth()
1129 start_year = self.calend.GetYear()
1130 else:
1131 self.calend.month = start_month = month
1132 self.calend.year = start_year = year
1133 self.calend.SetDayValue(day)
1134
1135 self.calend.HideTitle()
1136 self.ResetDisplay()
1137
1138 # get month list from DateTime
1139 monthlist = GetMonthList()
1140
1141 # select the month
1142 self.date = wx.ComboBox(self, -1, Month[start_month], (20, 20), (90, -1),
1143 monthlist, wx.CB_DROPDOWN)
1144 self.Bind(wx.EVT_COMBOBOX, self.EvtComboBox, self.date)
1145
1146 # alternate spin button to control the month
1147 h = self.date.GetSize().height
1148 self.m_spin = wx.SpinButton(self, -1, (115, 20), (h*1.5, h), wx.SP_VERTICAL)
1149 self.m_spin.SetRange(1, 12)
1150 self.m_spin.SetValue(start_month)
1151 self.Bind(wx.EVT_SPIN, self.OnMonthSpin, self.m_spin)
1152
1153 # spin button to control the year
1154 self.dtext = wx.TextCtrl(self, -1, str(start_year), (160, 20), (60, -1))
1155 h = self.dtext.GetSize().height
1156
1157 self.y_spin = wx.SpinButton(self, -1, (225, 20), (h*1.5, h), wx.SP_VERTICAL)
1158 self.y_spin.SetRange(1980, 2010)
1159 self.y_spin.SetValue(start_year)
1160
1161 self.Bind(wx.EVT_SPIN, self.OnYrSpin, self.y_spin)
1162 self.Bind(EVT_CALENDAR, self.MouseClick, self.calend)
1163
1164 x_pos = 50
1165 y_pos = 280
1166 but_size = (60, 25)
1167
1168 btn = wx.Button(self, wx.ID_OK, ' Ok ', (x_pos, y_pos), but_size)
1169 self.Bind(wx.EVT_BUTTON, self.OnOk, btn)
1170
1171 btn = wx.Button(self, wx.ID_CANCEL, ' Close ', (x_pos + 120, y_pos), but_size)
1172 self.Bind(wx.EVT_BUTTON, self.OnCancel, btn)
1173
1174 def OnOk(self, evt):
1175 self.result = ['None', str(self.calend.day), Month[self.calend.month], str(self.calend.year)]
1176 self.EndModal(wx.ID_OK)
1177
1178 def OnCancel(self, event):
1179 self.EndModal(wx.ID_CANCEL)
1180
1181 # log the mouse clicks
1182 def MouseClick(self, evt):
1183 self.month = evt.month
1184 # result click type and date
1185 self.result = [evt.click, str(evt.day), Month[evt.month], str(evt.year)]
1186
1187 if evt.click == 'DLEFT':
1188 self.EndModal(wx.ID_OK)
1189
1190 # month and year spin selection routines
1191 def OnMonthSpin(self, event):
1192 month = event.GetPosition()
1193 self.date.SetValue(Month[month])
1194 self.calend.SetMonth(month)
1195 self.calend.Refresh()
1196
1197 def OnYrSpin(self, event):
1198 year = event.GetPosition()
1199 self.dtext.SetValue(str(year))
1200 self.calend.SetYear(year)
1201 self.calend.Refresh()
1202
1203 def EvtComboBox(self, event):
1204 name = event.GetString()
1205 monthval = self.date.FindString(name)
1206 self.m_spin.SetValue(monthval+1)
1207
1208 self.calend.SetMonth(monthval+1)
1209 self.ResetDisplay()
1210
1211 # set the calendar for highlighted days
1212
1213 def ResetDisplay(self):
1214 month = self.calend.GetMonth()
1215 self.calend.Refresh()
1216
1217
1218