]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/calendar.py
Patch from Dj Gilcrease to fix gradients.
[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.x, self.y = event.GetX(), event.GetY()
755 self.shiftkey = event.ShiftDown()
756 self.ctrlkey = event.ControlDown()
757 key = self.GetDayHit(self.x, self.y)
758 self.SelectDay(key)
759
760 # tab mouse click events and process
761
762 def OnLeftEvent(self, event):
763 self.click = 'LEFT'
764 self.ProcessClick(event)
765
766 def OnLeftDEvent(self, event):
767 self.click = 'DLEFT'
768 self.ProcessClick(event)
769
770 def OnRightEvent(self, event):
771 self.click = 'RIGHT'
772 self.ProcessClick(event)
773
774 def OnRightDEvent(self, event):
775 self.click = 'DRIGHT'
776 self.ProcessClick(event)
777
778 def OnMiddleEvent(self, event):
779 self.click = 'MIDDLE'
780 self.ProcessClick(event)
781
782 def OnMiddleDEvent(self, event):
783 self.click = 'DMIDDLE'
784 self.ProcessClick(event)
785
786 def OnSetFocus(self, event):
787 self.hasFocus = True
788 self.DrawFocusIndicator(True)
789
790 def OnKillFocus(self, event):
791 self.hasFocus = False
792 self.DrawFocusIndicator(False)
793
794 def OnKeyDown(self, event):
795 if not self.hasFocus:
796 event.Skip()
797 return
798
799 key_code = event.GetKeyCode()
800
801 if key_code == wx.WXK_TAB:
802 forward = not event.ShiftDown()
803 ne = wx.NavigationKeyEvent()
804 ne.SetDirection(forward)
805 ne.SetCurrentFocus(self)
806 ne.SetEventObject(self)
807 self.GetParent().GetEventHandler().ProcessEvent(ne)
808 event.Skip()
809 return
810
811 delta = None
812
813 if key_code == wx.WXK_UP:
814 delta = -7
815 elif key_code == wx.WXK_DOWN:
816 delta = 7
817 elif key_code == wx.WXK_LEFT:
818 delta = -1
819 elif key_code == wx.WXK_RIGHT:
820 delta = 1
821 elif key_code == wx.WXK_HOME:
822 curDate = wx.DateTimeFromDMY(int(self.cal_days[self.sel_key]),self.month - 1,self.year)
823 newDate = wx.DateTime_Now()
824 ts = newDate - curDate
825 delta = ts.GetDays()
826
827 if delta <> None:
828 curDate = wx.DateTimeFromDMY(int(self.cal_days[self.sel_key]),self.month - 1,self.year)
829 timeSpan = wx.TimeSpan_Days(delta)
830 newDate = curDate + timeSpan
831
832 if curDate.GetMonth() == newDate.GetMonth():
833 self.set_day = newDate.GetDay()
834 key = self.sel_key + delta
835 self.SelectDay(key)
836 else:
837 self.month = newDate.GetMonth() + 1
838 self.year = newDate.GetYear()
839 self.set_day = newDate.GetDay()
840 self.sel_key = None
841 self.DoDrawing(wx.ClientDC(self))
842
843 event.Skip()
844
845 def SetSize(self, set_size):
846 self.size = set_size
847
848 def SetSelDay(self, sel):
849 # list of highlighted days
850 self.sel_lst = sel
851
852 # get the current date
853 def SetNow(self):
854 dt = now()
855 self.month = dt.month
856 self.year = dt.year
857 self.day = dt.day
858
859 # set the current day
860 def SetCurrentDay(self):
861 self.SetNow()
862 self.set_day = self.day
863
864 # get the date, day, month, year set in calendar
865
866 def GetDate(self):
867 return self.day, self.month, self.year
868
869 def GetDay(self):
870 return self.day
871
872 def GetMonth(self):
873 return self.month
874
875 def GetYear(self):
876 return self.year
877
878 # set the day, month, and year
879
880 def SetDayValue(self, day):
881 self.set_day = day
882 self.day = day
883
884 def SetMonth(self, month):
885 if month >= 1 and month <= 12:
886 self.month = month
887 else:
888 self.month = 1
889 self.set_day = None
890
891 def SetYear(self, year):
892 self.year = year
893
894 # increment year and month
895
896 def IncYear(self):
897 self.year = self.year + 1
898 self.set_day = None
899
900 def DecYear(self):
901 self.year = self.year - 1
902 self.set_day = None
903
904 def IncMonth(self):
905 self.month = self.month + 1
906 if self.month > 12:
907 self.month = 1
908 self.year = self.year + 1
909 self.set_day = None
910
911 def DecMonth(self):
912 self.month = self.month - 1
913 if self.month < 1:
914 self.month = 12
915 self.year = self.year - 1
916 self.set_day = None
917
918 # test to see if the selection has a date and create event
919
920 def TestDay(self, key):
921 try:
922 self.day = int(self.cal_days[key])
923 except:
924 return None
925
926 if self.day == "":
927 return None
928 else:
929 # Changed 12/1/03 by jmg (see above) to support 2.5 event binding
930 evt = wx.PyCommandEvent(wxEVT_COMMAND_PYCALENDAR_DAY_CLICKED, self.GetId())
931 evt.click, evt.day, evt.month, evt.year = self.click, self.day, self.month, self.year
932 evt.shiftkey = self.shiftkey
933 evt.ctrlkey = self.ctrlkey
934 self.GetEventHandler().ProcessEvent(evt)
935
936 self.set_day = self.day
937 return key
938
939 # find the clicked area rectangle
940
941 def GetDayHit(self, mx, my):
942 for key in self.rg.keys():
943 val = self.rg[key]
944 ms_rect = wx.Rect(mx, my, 1, 1)
945 if wx.IntersectRect(ms_rect, val) is not None:
946 result = self.TestDay(key)
947 return result
948
949 return None
950
951 # calendar drawing
952
953 def SetWeekColor(self, font_color, week_color):
954 # set font and background color for week title
955 self.colors[COLOR_HEADER_FONT] = MakeColor(font_color)
956 self.colors[COLOR_HEADER_BACKGROUND] = MakeColor(week_color)
957 self.colors[COLOR_3D_LIGHT] = MakeColor(week_color)
958 self.colors[COLOR_3D_DARK] = MakeColor(week_color)
959
960 def SetTextAlign(self, vert, horz):
961 self.num_align_horz = horz
962 self.num_align_vert = vert
963
964 def AddSelect(self, list, font_color, back_color):
965 list_val = [list, font_color, back_color]
966 self.select_list.append(list_val)
967
968 def ShowWeekEnd(self):
969 # highlight weekend
970 self.show_weekend = True
971
972 def SetBusType(self):
973 self.cal_type = "BUS"
974
975 def OnSize(self, evt):
976 self.Refresh(False)
977 evt.Skip()
978
979 def OnPaint(self, event):
980 DC = wx.PaintDC(self)
981 self.DoDrawing(DC)
982
983 def DoDrawing(self, DC):
984 #DC = wx.PaintDC(self)
985 DC.BeginDrawing()
986
987 try:
988 cal = self.caldraw
989 except:
990 self.caldraw = CalDraw(self)
991 cal = self.caldraw
992
993 cal.hide_grid = self.hide_grid
994 cal.hide_title = self.hide_title
995 cal.show_weekend = self.show_weekend
996 cal.cal_type = self.cal_type
997 cal.outer_border = self.outer_border
998 cal.num_align_horz = self.num_align_horz
999 cal.num_align_vert = self.num_align_vert
1000 cal.colors = self.colors
1001
1002 if self.size is None:
1003 size = self.GetClientSize()
1004 else:
1005 size = self.size
1006
1007 # drawing attributes
1008
1009 cal.SetSize(size)
1010 cal.SetCal(self.year, self.month)
1011
1012 # these have to set after SetCal as SetCal would overwrite them again.
1013 cal.set_x_mrg = self.set_x_mrg
1014 cal.set_y_mrg = self.set_y_mrg
1015 cal.set_y_end = self.set_y_end
1016
1017 for val in self.select_list:
1018 cal.AddSelect(val[0], val[1], val[2])
1019
1020 cal.DrawCal(DC, self.sel_lst)
1021
1022 self.rg = cal.GetRect()
1023 self.cal_days = cal.GetCal()
1024 self.st_pos = cal.GetOffset()
1025 self.ymax = DC.MaxY()
1026
1027 if self.set_day != None:
1028 self.SetDay(self.set_day)
1029
1030 DC.EndDrawing()
1031
1032 # draw the selection rectangle
1033 def DrawFocusIndicator(self, draw):
1034 DC = wx.ClientDC(self)
1035 try:
1036 if draw == True:
1037 self.caldraw.DrawFocusIndicator(DC)
1038 else:
1039 self.caldraw.DrawBorder(DC,True)
1040 except:
1041 pass
1042
1043 def DrawRect(self, key, bgcolor = 'WHITE', fgcolor= 'PINK',width = 0):
1044 if key == None:
1045 return
1046
1047 DC = wx.ClientDC(self)
1048 DC.BeginDrawing()
1049
1050 brush = wx.Brush(MakeColor(bgcolor))
1051 DC.SetBrush(brush)
1052
1053 DC.SetPen(wx.TRANSPARENT_PEN)
1054
1055 rect = self.rg[key]
1056 DC.DrawRectangle(rect.x+1, rect.y+1, rect.width-2, rect.height-2)
1057
1058 self.caldraw.DrawDayText(DC,key)
1059
1060 DC.EndDrawing()
1061
1062 def DrawRectOrg(self, key, fgcolor = 'BLACK', width = 0):
1063 if key == None:
1064 return
1065
1066 DC = wx.ClientDC(self)
1067 DC.BeginDrawing()
1068
1069 brush = wx.Brush(wx.Colour(0, 0xFF, 0x80), wx.TRANSPARENT)
1070 DC.SetBrush(brush)
1071
1072 try:
1073 DC.SetPen(wx.Pen(MakeColor(fgcolor), width))
1074 except:
1075 DC.SetPen(wx.Pen(MakeColor(self.GetColor(COLOR_GRID_LINES)), width))
1076
1077 rect = self.rg[key]
1078 DC.DrawRectangleRect(rect)
1079
1080 DC.EndDrawing()
1081
1082 # set the day selection
1083
1084 def SetDay(self, day):
1085 d = day + self.st_pos - 1
1086 self.SelectDay(d)
1087
1088 def IsDayInWeekend(self, key):
1089 try:
1090 t = Date(self.year, self.month, 1)
1091
1092 day = self.cal_days[key]
1093 day = int(day) + t.day_of_week
1094
1095 if day % 7 == 6 or day % 7 == 0:
1096 return True
1097 except:
1098 return False
1099
1100 def SelectDay(self, key):
1101 sel_size = 1
1102 # clear large selection
1103
1104 if self.sel_key != None:
1105 (cfont, bgcolor) = self.__GetColorsForDay(self.sel_key)
1106 self.DrawRect(self.sel_key, bgcolor,cfont, sel_size)
1107
1108 self.DrawRect(key, self.GetColor(COLOR_HIGHLIGHT_BACKGROUND), self.GetColor(COLOR_HIGHLIGHT_FONT), sel_size)
1109 # store last used by
1110 self.sel_key = key
1111
1112 def ClearDsp(self):
1113 self.Clear()
1114 def SetMargin(self, xmarg, ymarg):
1115 self.set_x_mrg = xmarg
1116 self.set_y_mrg = ymarg
1117 self.set_y_end = ymarg
1118 def __GetColorsForDay(self, key):
1119 cfont = self.GetColor(COLOR_FONT)
1120 bgcolor = self.GetColor(COLOR_BACKGROUND)
1121
1122 if self.IsDayInWeekend(key) is True and self.show_weekend is True:
1123 cfont = self.GetColor(COLOR_WEEKEND_FONT)
1124 bgcolor = self.GetColor(COLOR_WEEKEND_BACKGROUND)
1125
1126 try:
1127 dayIdx = int(self.cal_days[key])
1128 (cfont, bgcolor) = self.caldraw.cal_sel[dayIdx]
1129 except:
1130 pass
1131
1132 return (cfont, bgcolor)
1133
1134 class CalenDlg(wx.Dialog):
1135 def __init__(self, parent, month=None, day = None, year=None):
1136 wx.Dialog.__init__(self, parent, -1, "Event Calendar", wx.DefaultPosition, (280, 360))
1137 self.result = None
1138
1139 # set the calendar and attributes
1140 self.calend = Calendar(self, -1, (20, 60), (240, 200))
1141
1142 if month == None:
1143 self.calend.SetCurrentDay()
1144 start_month = self.calend.GetMonth()
1145 start_year = self.calend.GetYear()
1146 else:
1147 self.calend.month = start_month = month
1148 self.calend.year = start_year = year
1149 self.calend.SetDayValue(day)
1150
1151 self.calend.HideTitle()
1152 self.ResetDisplay()
1153
1154 # get month list from DateTime
1155 monthlist = GetMonthList()
1156
1157 # select the month
1158 self.date = wx.ComboBox(self, -1, Month[start_month], (20, 20), (90, -1),
1159 monthlist, wx.CB_DROPDOWN)
1160 self.Bind(wx.EVT_COMBOBOX, self.EvtComboBox, self.date)
1161
1162 # alternate spin button to control the month
1163 h = self.date.GetSize().height
1164 self.m_spin = wx.SpinButton(self, -1, (115, 20), (h*1.5, h), wx.SP_VERTICAL)
1165 self.m_spin.SetRange(1, 12)
1166 self.m_spin.SetValue(start_month)
1167 self.Bind(wx.EVT_SPIN, self.OnMonthSpin, self.m_spin)
1168
1169 # spin button to control the year
1170 self.dtext = wx.TextCtrl(self, -1, str(start_year), (160, 20), (60, -1))
1171 h = self.dtext.GetSize().height
1172
1173 self.y_spin = wx.SpinButton(self, -1, (225, 20), (h*1.5, h), wx.SP_VERTICAL)
1174 self.y_spin.SetRange(1980, 2010)
1175 self.y_spin.SetValue(start_year)
1176
1177 self.Bind(wx.EVT_SPIN, self.OnYrSpin, self.y_spin)
1178 self.Bind(EVT_CALENDAR, self.MouseClick, self.calend)
1179
1180 x_pos = 50
1181 y_pos = 280
1182 but_size = (60, 25)
1183
1184 btn = wx.Button(self, wx.ID_OK, ' Ok ', (x_pos, y_pos), but_size)
1185 self.Bind(wx.EVT_BUTTON, self.OnOk, btn)
1186
1187 btn = wx.Button(self, wx.ID_CANCEL, ' Close ', (x_pos + 120, y_pos), but_size)
1188 self.Bind(wx.EVT_BUTTON, self.OnCancel, btn)
1189
1190 def OnOk(self, evt):
1191 self.result = ['None', str(self.calend.day), Month[self.calend.month], str(self.calend.year)]
1192 self.EndModal(wx.ID_OK)
1193
1194 def OnCancel(self, event):
1195 self.EndModal(wx.ID_CANCEL)
1196
1197 # log the mouse clicks
1198 def MouseClick(self, evt):
1199 self.month = evt.month
1200 # result click type and date
1201 self.result = [evt.click, str(evt.day), Month[evt.month], str(evt.year)]
1202
1203 if evt.click == 'DLEFT':
1204 self.EndModal(wx.ID_OK)
1205
1206 # month and year spin selection routines
1207 def OnMonthSpin(self, event):
1208 month = event.GetPosition()
1209 self.date.SetValue(Month[month])
1210 self.calend.SetMonth(month)
1211 self.calend.Refresh()
1212
1213 def OnYrSpin(self, event):
1214 year = event.GetPosition()
1215 self.dtext.SetValue(str(year))
1216 self.calend.SetYear(year)
1217 self.calend.Refresh()
1218
1219 def EvtComboBox(self, event):
1220 name = event.GetString()
1221 monthval = self.date.FindString(name)
1222 self.m_spin.SetValue(monthval+1)
1223
1224 self.calend.SetMonth(monthval+1)
1225 self.ResetDisplay()
1226
1227 # set the calendar for highlighted days
1228
1229 def ResetDisplay(self):
1230 month = self.calend.GetMonth()
1231 self.calend.Refresh()
1232
1233
1234