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