]>
Commit | Line | Data |
---|---|---|
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 | ||
43 | import wx | |
44 | ||
45 | from CDate import * | |
46 | ||
47 | CalDays = [6, 0, 1, 2, 3, 4, 5] | |
48 | AbrWeekday = {6:"Sun", 0:"Mon", 1:"Tue", 2:"Wed", 3:"Thu", 4:"Fri", 5:"Sat"} | |
49 | _MIDSIZE = 180 | |
50 | ||
51 | BusCalDays = [0, 1, 2, 3, 4, 5, 6] | |
52 | ||
53 | # Calendar click event - added 12/1/03 by jmg (see above) | |
54 | wxEVT_COMMAND_PYCALENDAR_DAY_CLICKED = wx.NewEventType() | |
55 | EVT_CALENDAR = wx.PyEventBinder(wxEVT_COMMAND_PYCALENDAR_DAY_CLICKED, 1) | |
56 | ||
57 | def GetMonthList(): | |
58 | monthlist = [] | |
59 | for i in range(13): | |
60 | name = Month[i] | |
61 | if name != None: | |
62 | monthlist.append(name) | |
63 | return monthlist | |
64 | ||
65 | # calendar drawing routing | |
66 | ||
67 | class CalDraw: | |
68 | def __init__(self, parent): | |
69 | self.pwidth = 1 | |
70 | self.pheight = 1 | |
71 | try: | |
72 | self.scale = parent.scale | |
73 | except: | |
74 | self.scale = 1 | |
75 | ||
76 | self.DefParms() | |
77 | ||
78 | def DefParms(self): | |
79 | self.num_auto = True # auto scale of the cal number day size | |
80 | self.num_size = 12 # default size of calendar if no auto size | |
81 | self.max_num_size = 12 # maximum size for calendar number | |
82 | ||
83 | self.num_align_horz = wx.ALIGN_CENTRE # alignment of numbers | |
84 | self.num_align_vert = wx.ALIGN_CENTRE | |
85 | self.num_indent_horz = 0 # points indent from position, used to offset if not centered | |
86 | self.num_indent_vert = 0 | |
87 | ||
88 | self.week_auto = True # auto scale of week font text | |
89 | self.week_size = 10 | |
90 | self.max_week_size = 12 | |
91 | ||
92 | self.grid_color = 'BLACK' # grid and selection colors | |
93 | self.back_color = 'WHITE' | |
94 | self.sel_color = 'RED' | |
95 | ||
96 | self.high_color = 'LIGHT BLUE' | |
97 | self.border_color = 'BLACK' | |
98 | self.week_color = 'LIGHT GREY' | |
99 | ||
100 | self.week_font_color = 'BLACK' # font colors | |
101 | self.day_font_color = 'BLACK' | |
102 | ||
103 | self.font = wx.SWISS | |
104 | self.bold = wx.NORMAL | |
105 | ||
106 | self.hide_title = False | |
107 | self.hide_grid = False | |
108 | self.outer_border = True | |
109 | ||
110 | self.title_offset = 0 | |
111 | self.cal_week_scale = 0.7 | |
112 | self.show_weekend = False | |
113 | self.cal_type = "NORMAL" | |
114 | ||
115 | def SetWeekColor(self, font_color, week_color): # set font and background color for week title | |
116 | self.week_font_color = font_color | |
117 | self.week_color = week_color | |
118 | ||
119 | def SetSize(self, size): | |
120 | self.set_sizew = size[0] | |
121 | self.set_sizeh = size[1] | |
122 | ||
123 | def InitValues(self): # default dimensions of various elements of the calendar | |
124 | self.rg = {} | |
125 | self.cal_sel = {} | |
126 | self.set_cy_st = 0 # start position | |
127 | self.set_cx_st = 0 | |
128 | ||
129 | self.set_y_mrg = 15 # start of vertical draw default | |
130 | self.set_x_mrg = 10 | |
131 | self.set_y_end = 10 | |
132 | ||
133 | def SetPos(self, xpos, ypos): | |
134 | self.set_cx_st = xpos | |
135 | self.set_cy_st = ypos | |
136 | ||
137 | def SetMarg(self, xmarg, ymarg): | |
138 | self.set_x_st = xmarg | |
139 | self.set_y_st = ymarg | |
140 | self.set_y_end = ymarg | |
141 | ||
142 | def InitScale(self): # scale position values | |
143 | self.sizew = int(self.set_sizew * self.pwidth) | |
144 | self.sizeh = int(self.set_sizeh * self.pheight) | |
145 | ||
146 | self.cx_st = int(self.set_cx_st * self.pwidth) # draw start position | |
147 | self.cy_st = int(self.set_cy_st * self.pheight) | |
148 | ||
149 | self.x_mrg = int(self.set_x_mrg * self.pwidth) # calendar draw margins | |
150 | self.y_mrg = int(self.set_y_mrg * self.pheight) | |
151 | self.y_end = int(self.set_y_end * self.pheight) | |
152 | ||
153 | def DrawCal(self, DC, sel_lst=[]): | |
154 | self.DC = DC | |
155 | self.InitScale() | |
156 | ||
157 | self.DrawBorder() | |
158 | ||
159 | if self.hide_title is False: | |
160 | self.DrawMonth() | |
161 | ||
162 | self.Center() | |
163 | ||
164 | self.DrawGrid() | |
165 | self.GetRect() | |
166 | ||
167 | if self.show_weekend is True: # highlight weekend dates | |
168 | self.SetWeekEnd() | |
169 | ||
170 | self.AddSelect(sel_lst) # overrides the weekend highlight | |
171 | ||
172 | self.DrawSel() # highlighted days | |
173 | self.DrawWeek() | |
174 | self.DrawNum() | |
175 | ||
176 | def AddSelect(self, list, cfont=None, cbackgrd = None): | |
177 | if cfont is None: | |
178 | cfont = self.sel_color # font digit color | |
179 | ||
180 | if cbackgrd is None: | |
181 | cbackgrd = self.high_color # select background color | |
182 | ||
183 | for val in list: | |
184 | self.cal_sel[val] = (cfont, cbackgrd) | |
185 | ||
186 | # draw border around the outside of the main display rectangle | |
187 | def DrawBorder(self): | |
188 | brush = wx.Brush(wx.NamedColour(self.back_color), wx.SOLID) | |
189 | self.DC.SetBrush(brush) | |
190 | self.DC.SetPen(wx.Pen(wx.NamedColour(self.border_color), 1)) | |
191 | ||
192 | if self.outer_border is True: | |
193 | # full display window area | |
194 | rect = wx.Rect(self.cx_st, self.cy_st, self.sizew, self.sizeh) | |
195 | self.DC.DrawRectangleRect(rect) | |
196 | ||
197 | def DrawNumVal(self): | |
198 | self.DrawNum() | |
199 | ||
200 | # calculate the calendar days and offset position | |
201 | ||
202 | def SetCal(self, year, month): | |
203 | self.InitValues() # reset initial values | |
204 | ||
205 | self.year = year | |
206 | self.month = month | |
207 | ||
208 | day = 1 | |
209 | t = Date(year, month, day) | |
210 | dow = self.dow = t.day_of_week # start day in month | |
211 | dim = self.dim = t.days_in_month # number of days in month | |
212 | ||
213 | if self.cal_type == "NORMAL": | |
214 | start_pos = dow+1 | |
215 | else: | |
216 | start_pos = dow | |
217 | ||
218 | self.st_pos = start_pos | |
219 | ||
220 | self.cal = [] | |
221 | for i in range(start_pos): | |
222 | self.cal.append('') | |
223 | ||
224 | i = 1 | |
225 | while i <= dim: | |
226 | self.cal.append(str(i)) | |
227 | i = i + 1 | |
228 | ||
229 | return start_pos | |
230 | ||
231 | def SetWeekEnd(self, font_color='BLACK', backgrd = 'LIGHT GREY'): | |
232 | date = 6 - int(self.dow) # start day of first saturday | |
233 | ||
234 | while date <= self.dim: | |
235 | self.cal_sel[date] = (font_color, backgrd) # Saturday | |
236 | date = date + 1 | |
237 | ||
238 | if date <= self.dim: | |
239 | self.cal_sel[date] = (font_color, backgrd) # Sunday | |
240 | date = date + 6 | |
241 | else: | |
242 | date = date + 7 | |
243 | ||
244 | # get the display rectange list of the day grid | |
245 | def GetRect(self): | |
246 | cnt = 0 | |
247 | for y in self.gridy[1:-1]: | |
248 | for x in self.gridx[:-1]: | |
249 | assert type(y) == int | |
250 | assert type(x) == int | |
251 | rect = wx.Rect(x, y, self.dl_w, self.dl_h) # create rect region | |
252 | self.rg[cnt] = rect | |
253 | cnt = cnt + 1 | |
254 | ||
255 | return self.rg | |
256 | ||
257 | def GetCal(self): | |
258 | return self.cal | |
259 | ||
260 | def GetOffset(self): | |
261 | return self.st_pos | |
262 | ||
263 | # month and year title | |
264 | def DrawMonth(self): | |
265 | month = Month[self.month] | |
266 | ||
267 | sizef = 11 | |
268 | if self.sizeh < _MIDSIZE: | |
269 | sizef = 10 | |
270 | ||
271 | f = wx.Font(sizef, self.font, wx.NORMAL, self.bold) | |
272 | self.DC.SetFont(f) | |
273 | ||
274 | tw,th = self.DC.GetTextExtent(month) | |
275 | adjust = self.cx_st + (self.sizew-tw)/2 | |
276 | self.DC.DrawText(month, (adjust, self.cy_st + th)) | |
277 | ||
278 | year = str(self.year) | |
279 | tw,th = self.DC.GetTextExtent(year) | |
280 | adjust = self.sizew - tw - self.x_mrg | |
281 | ||
282 | self.title_offset = th * 2 | |
283 | ||
284 | f = wx.Font(sizef, self.font, wx.NORMAL, self.bold) | |
285 | self.DC.SetFont(f) | |
286 | self.DC.DrawText(year, (self.cx_st + adjust, self.cy_st + th)) | |
287 | ||
288 | def DrawWeek(self): # draw the week days | |
289 | width = self.gridx[1]-self.gridx[0] | |
290 | height = self.gridy[1] - self.gridy[0] | |
291 | rect_w = self.gridx[7]-self.gridx[0] | |
292 | ||
293 | f = wx.Font(10, self.font, wx.NORMAL, self.bold) # initial font setting | |
294 | ||
295 | if self.week_auto == True: | |
296 | test_size = self.max_week_size # max size | |
297 | test_day = ' Sun ' | |
298 | while test_size > 2: | |
299 | f.SetPointSize(test_size) | |
300 | self.DC.SetFont(f) | |
301 | tw,th = self.DC.GetTextExtent(test_day) | |
302 | ||
303 | if tw < width and th < height: | |
304 | break | |
305 | ||
306 | test_size = test_size - 1 | |
307 | else: | |
308 | f.SetPointSize(self.week_size) # set fixed size | |
309 | self.DC.SetFont(f) | |
310 | ||
311 | self.DC.SetTextForeground(wx.NamedColour(self.week_font_color)) | |
312 | ||
313 | cnt_x = 0 | |
314 | cnt_y = 0 | |
315 | ||
316 | brush = wx.Brush(wx.NamedColour(self.week_color), wx.SOLID) | |
317 | self.DC.SetBrush(brush) | |
318 | self.DC.DrawRectangle((self.gridx[0], self.gridy[0]), (rect_w+1, height)) | |
319 | ||
320 | if self.cal_type == "NORMAL": | |
321 | cal_days = CalDays | |
322 | else: | |
323 | cal_days = BusCalDays | |
324 | ||
325 | for val in cal_days: | |
326 | day = AbrWeekday[val] | |
327 | ||
328 | if self.sizew < 200: | |
329 | day = day[0] | |
330 | ||
331 | dw,dh = self.DC.GetTextExtent(day) | |
332 | diffx = (width-dw)/2 | |
333 | diffy = (height-dh)/2 | |
334 | ||
335 | x = self.gridx[cnt_x] | |
336 | y = self.gridy[cnt_y] | |
337 | self.DC.DrawRectangle((self.gridx[cnt_x], self.gridy[0]), (width+1, height)) | |
338 | self.DC.DrawText(day, (x+diffx, y+diffy)) | |
339 | cnt_x = cnt_x + 1 | |
340 | ||
341 | # draw the day numbers | |
342 | def DrawNum(self): | |
343 | f = wx.Font(10, self.font, wx.NORMAL, self.bold) # initial font setting | |
344 | ||
345 | if self.num_auto == True: | |
346 | test_size = self.max_num_size # max size | |
347 | test_day = ' 99 ' | |
348 | ||
349 | while test_size > 2: | |
350 | f.SetPointSize(test_size) | |
351 | self.DC.SetFont(f) | |
352 | tw,th = self.DC.GetTextExtent(test_day) | |
353 | ||
354 | if tw < self.dl_w and th < self.dl_h: | |
355 | sizef = test_size | |
356 | break | |
357 | test_size = test_size - 1 | |
358 | else: | |
359 | f.SetPointSize(self.num_size) # set fixed size | |
360 | self.DC.SetFont(f) | |
361 | ||
362 | cnt_x = 0 | |
363 | cnt_y = 1 | |
364 | for val in self.cal: | |
365 | x = self.gridx[cnt_x] | |
366 | y = self.gridy[cnt_y] | |
367 | ||
368 | try: | |
369 | num_val = int(val) | |
370 | num_color = self.cal_sel[num_val][0] | |
371 | except: | |
372 | num_color = self.day_font_color | |
373 | ||
374 | self.DC.SetTextForeground(wx.NamedColour(num_color)) | |
375 | self.DC.SetFont(f) | |
376 | ||
377 | tw,th = self.DC.GetTextExtent(val) | |
378 | ||
379 | if self.num_align_horz == wx.ALIGN_CENTRE: | |
380 | adj_h = (self.dl_w - tw)/2 | |
381 | elif self.num_align_horz == wx.ALIGN_RIGHT: | |
382 | adj_h = self.dl_w - tw | |
383 | else: | |
384 | adj_h = 0 # left alignment | |
385 | ||
386 | adj_h = adj_h + self.num_indent_horz | |
387 | ||
388 | if self.num_align_vert == wx.ALIGN_CENTRE: | |
389 | adj_v = (self.dl_h - th)/2 | |
390 | elif self.num_align_horz == wx.ALIGN_RIGHT: | |
391 | adj_v = self.dl_h - th | |
392 | else: | |
393 | adj_v = 0 # left alignment | |
394 | ||
395 | adj_v = adj_v + self.num_indent_vert | |
396 | ||
397 | self.DC.DrawText(val, (x+adj_h, y+adj_v)) | |
398 | ||
399 | if cnt_x < 6: | |
400 | cnt_x = cnt_x + 1 | |
401 | else: | |
402 | cnt_x = 0 | |
403 | cnt_y = cnt_y + 1 | |
404 | ||
405 | # calculate the dimensions in the center of the drawing area | |
406 | def Center(self): | |
407 | bdw = self.x_mrg * 2 | |
408 | bdh = self.y_mrg + self.y_end + self.title_offset | |
409 | ||
410 | self.dl_w = int((self.sizew-bdw)/7) | |
411 | self.dl_h = int((self.sizeh-bdh)/7) | |
412 | ||
413 | # week title adjustment | |
414 | self.dl_th = int(self.dl_h*self.cal_week_scale) | |
415 | self.cwidth = self.dl_w * 7 | |
416 | self.cheight = self.dl_h * 6 + self.dl_th | |
417 | ||
418 | # highlighted selected days | |
419 | def DrawSel(self): | |
420 | for key in self.cal_sel.keys(): | |
421 | sel_color = self.cal_sel[key][1] | |
422 | brush = wx.Brush(wx.NamedColour(sel_color), wx.SOLID) | |
423 | self.DC.SetBrush(brush) | |
424 | ||
425 | if self.hide_grid is False: | |
426 | self.DC.SetPen(wx.Pen(wx.NamedColour(self.grid_color), 0)) | |
427 | else: | |
428 | self.DC.SetPen(wx.Pen(wx.NamedColour(self.back_color), 0)) | |
429 | ||
430 | nkey = key + self.st_pos -1 | |
431 | rect = self.rg[nkey] | |
432 | self.DC.DrawRectangle((rect.x, rect.y), (rect.width+1, rect.height+1)) | |
433 | ||
434 | # calculate and draw the grid lines | |
435 | def DrawGrid(self): | |
436 | self.DC.SetPen(wx.Pen(wx.NamedColour(self.grid_color), 0)) | |
437 | ||
438 | self.gridx = [] | |
439 | self.gridy = [] | |
440 | ||
441 | self.x_st = self.cx_st + self.x_mrg | |
442 | # start postion of draw | |
443 | self.y_st = self.cy_st + self.y_mrg + self.title_offset | |
444 | ||
445 | x1 = self.x_st | |
446 | y1 = self.y_st | |
447 | y2 = y1 + self.cheight | |
448 | ||
449 | for i in range(8): | |
450 | if self.hide_grid is False: | |
451 | self.DC.DrawLine((x1, y1), (x1, y2)) | |
452 | self.gridx.append(x1) | |
453 | x1 = x1 + self.dl_w | |
454 | ||
455 | x1 = self.x_st | |
456 | y1 = self.y_st | |
457 | x2 = x1 + self.cwidth | |
458 | ||
459 | for i in range(8): | |
460 | if self.hide_grid is False: | |
461 | self.DC.DrawLine((x1, y1), (x2, y1)) | |
462 | ||
463 | self.gridy.append(y1) | |
464 | ||
465 | if i == 0: | |
466 | y1 = y1 + self.dl_th | |
467 | else: | |
468 | y1 = y1 + self.dl_h | |
469 | ||
470 | ||
471 | class PrtCalDraw(CalDraw): | |
472 | def InitValues(self): | |
473 | self.rg = {} | |
474 | self.cal_sel = {} | |
475 | # start draw border location | |
476 | self.set_cx_st = 1.0 | |
477 | self.set_cy_st = 1.0 | |
478 | ||
479 | # draw offset position | |
480 | self.set_y_mrg = 0.2 | |
481 | self.set_x_mrg = 0.2 | |
482 | self.set_y_end = 0.2 | |
483 | ||
484 | # calculate the dimensions in the center of the drawing area | |
485 | def SetPSize(self, pwidth, pheight): | |
486 | self.pwidth = int(pwidth)/self.scale | |
487 | self.pheight = int(pheight)/self.scale | |
488 | ||
489 | def SetPreview(self, preview): | |
490 | self.preview = preview | |
491 | ||
492 | class Calendar(wx.Window): | |
493 | def __init__(self, parent, id, pos=wx.DefaultPosition, size=wx.DefaultSize): | |
494 | wx.Window.__init__(self, parent, id, pos, size) | |
495 | ||
496 | # set the calendar control attributes | |
497 | ||
498 | self.grid_color = 'BLACK' | |
499 | self.back_color = 'WHITE' | |
500 | self.hide_grid = False | |
501 | self.sel_color = 'RED' | |
502 | self.hide_title = False | |
503 | self.show_weekend = False | |
504 | self.cal_type = "NORMAL" | |
505 | ||
506 | # font colors | |
507 | self.week_color = 'LIGHT GREY' | |
508 | self.week_font_color = 'BLACK' | |
509 | ||
510 | self.select_list = [] | |
511 | ||
512 | self.SetBackgroundColour(self.back_color) | |
513 | self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftEvent) | |
514 | self.Bind(wx.EVT_LEFT_DCLICK, self.OnLeftDEvent) | |
515 | self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightEvent) | |
516 | self.Bind(wx.EVT_RIGHT_DCLICK, self.OnRightDEvent) | |
517 | ||
518 | self.sel_key = None # last used by | |
519 | self.sel_lst = [] # highlighted selected days | |
520 | ||
521 | # default calendar for current month | |
522 | self.SetNow() | |
523 | ||
524 | self.size = None | |
525 | self.set_day = None | |
526 | ||
527 | self.Bind(wx.EVT_PAINT, self.OnPaint) | |
528 | self.Bind(wx.EVT_SIZE, self.OnSize) | |
529 | ||
530 | # control some of the main calendar attributes | |
531 | ||
532 | def HideTitle(self): | |
533 | self.hide_title = True | |
534 | ||
535 | def HideGrid(self): | |
536 | self.hide_grid = True | |
537 | ||
538 | # determine the calendar rectangle click area and draw a selection | |
539 | ||
540 | def ProcessClick(self, event): | |
541 | self.x, self.y = event.GetX(), event.GetY() | |
542 | key = self.GetDayHit(self.x, self.y) | |
543 | self.SelectDay(key) | |
544 | ||
545 | # tab mouse click events and process | |
546 | ||
547 | def OnLeftEvent(self, event): | |
548 | self.click = 'LEFT' | |
549 | self.shiftkey = event.ShiftDown() | |
550 | self.ctrlkey = event.ControlDown() | |
551 | self.ProcessClick(event) | |
552 | ||
553 | def OnLeftDEvent(self, event): | |
554 | self.click = 'DLEFT' | |
555 | self.ProcessClick(event) | |
556 | ||
557 | def OnRightEvent(self, event): | |
558 | self.click = 'RIGHT' | |
559 | self.ProcessClick(event) | |
560 | ||
561 | def OnRightDEvent(self, event): | |
562 | self.click = 'DRIGHT' | |
563 | self.ProcessClick(event) | |
564 | ||
565 | def SetSize(self, set_size): | |
566 | self.size = set_size | |
567 | ||
568 | def SetSelDay(self, sel): | |
569 | # list of highlighted days | |
570 | self.sel_lst = sel | |
571 | ||
572 | # get the current date | |
573 | def SetNow(self): | |
574 | dt = now() | |
575 | self.month = dt.month | |
576 | self.year = dt.year | |
577 | self.day = dt.day | |
578 | ||
579 | # set the current day | |
580 | def SetCurrentDay(self): | |
581 | self.SetNow() | |
582 | self.set_day = self.day | |
583 | ||
584 | # get the date, day, month, year set in calendar | |
585 | ||
586 | def GetDate(self): | |
587 | return self.day, self.month, self.year | |
588 | ||
589 | def GetDay(self): | |
590 | return self.day | |
591 | ||
592 | def GetMonth(self): | |
593 | return self.month | |
594 | ||
595 | def GetYear(self): | |
596 | return self.year | |
597 | ||
598 | # set the day, month, and year | |
599 | ||
600 | def SetDayValue(self, day): | |
601 | self.set_day = day | |
602 | ||
603 | def SetMonth(self, month): | |
604 | if month >= 1 and month <= 12: | |
605 | self.month = month | |
606 | else: | |
607 | self.month = 1 | |
608 | self.set_day = None | |
609 | ||
610 | def SetYear(self, year): | |
611 | self.year = year | |
612 | ||
613 | # increment year and month | |
614 | ||
615 | def IncYear(self): | |
616 | self.year = self.year + 1 | |
617 | self.set_day = None | |
618 | ||
619 | def DecYear(self): | |
620 | self.year = self.year - 1 | |
621 | self.set_day = None | |
622 | ||
623 | def IncMonth(self): | |
624 | self.month = self.month + 1 | |
625 | if self.month > 12: | |
626 | self.month = 1 | |
627 | self.year = self.year + 1 | |
628 | self.set_day = None | |
629 | ||
630 | def DecMonth(self): | |
631 | self.month = self.month - 1 | |
632 | if self.month < 1: | |
633 | self.month = 12 | |
634 | self.year = self.year - 1 | |
635 | self.set_day = None | |
636 | ||
637 | # test to see if the selection has a date and create event | |
638 | ||
639 | def TestDay(self, key): | |
640 | try: | |
641 | self.day = int(self.cal[key]) | |
642 | except: | |
643 | return None | |
644 | if self.day == "": | |
645 | return None | |
646 | else: | |
647 | # Changed 12/1/03 by jmg (see above) to support 2.5 event binding | |
648 | evt = wx.PyCommandEvent(wxEVT_COMMAND_PYCALENDAR_DAY_CLICKED, self.GetId()) | |
649 | evt.click, evt.day, evt.month, evt.year = self.click, self.day, self.month, self.year | |
650 | evt.shiftkey = self.shiftkey | |
651 | evt.ctrlkey = self.ctrlkey | |
652 | self.GetEventHandler().ProcessEvent(evt) | |
653 | ||
654 | self.set_day = self.day | |
655 | return key | |
656 | ||
657 | # find the clicked area rectangle | |
658 | ||
659 | def GetDayHit(self, mx, my): | |
660 | for key in self.rg.keys(): | |
661 | val = self.rg[key] | |
662 | ms_rect = wx.Rect(mx, my, 1, 1) | |
663 | if wx.IntersectRect(ms_rect, val) is not None: | |
664 | result = self.TestDay(key) | |
665 | return result | |
666 | ||
667 | return None | |
668 | ||
669 | # calendar drawing | |
670 | ||
671 | def SetWeekColor(self, font_color, week_color): | |
672 | # set font and background color for week title | |
673 | self.week_font_color = font_color | |
674 | self.week_color = week_color | |
675 | ||
676 | def AddSelect(self, list, font_color, back_color): | |
677 | list_val = [list, font_color, back_color] | |
678 | self.select_list.append(list_val) | |
679 | ||
680 | def ShowWeekEnd(self): | |
681 | # highlight weekend | |
682 | self.show_weekend = True | |
683 | ||
684 | def SetBusType(self): | |
685 | self.cal_type = "BUS" | |
686 | ||
687 | def OnSize(self, evt): | |
688 | self.Refresh(False) | |
689 | evt.Skip() | |
690 | ||
691 | def OnPaint(self, event): | |
692 | DC = wx.PaintDC(self) | |
693 | self.DoDrawing(DC) | |
694 | ||
695 | def DoDrawing(self, DC): | |
696 | DC = wx.PaintDC(self) | |
697 | DC.BeginDrawing() | |
698 | ||
699 | self.cal = cal = CalDraw(self) | |
700 | ||
701 | cal.grid_color = self.grid_color | |
702 | cal.back_color = self.back_color | |
703 | cal.hide_grid = self.hide_grid | |
704 | cal.grid_color = self.grid_color | |
705 | cal.hide_title = self.hide_title | |
706 | cal.show_weekend = self.show_weekend | |
707 | cal.cal_type = self.cal_type | |
708 | ||
709 | if self.size is None: | |
710 | size = self.GetClientSize() | |
711 | else: | |
712 | size = self.size | |
713 | ||
714 | # drawing attributes | |
715 | ||
716 | cal.week_font_color = self.week_font_color | |
717 | cal.week_color = self.week_color | |
718 | ||
719 | cal.SetSize(size) | |
720 | cal.SetCal(self.year, self.month) | |
721 | ||
722 | for val in self.select_list: | |
723 | cal.AddSelect(val[0], val[1], val[2]) | |
724 | ||
725 | cal.DrawCal(DC, self.sel_lst) | |
726 | ||
727 | self.rg = cal.GetRect() | |
728 | self.cal = cal.GetCal() | |
729 | self.st_pos = cal.GetOffset() | |
730 | self.ymax = DC.MaxY() | |
731 | ||
732 | if self.set_day != None: | |
733 | self.SetDay(self.set_day) | |
734 | ||
735 | DC.EndDrawing() | |
736 | ||
737 | # draw the selection rectangle | |
738 | def DrawRect(self, key, color = 'BLACK', width = 0): | |
739 | if key == None: | |
740 | return | |
741 | ||
742 | DC = wx.ClientDC(self) | |
743 | DC.BeginDrawing() | |
744 | ||
745 | brush = wx.Brush(wx.Colour(0, 0xFF, 0x80), wx.TRANSPARENT) | |
746 | DC.SetBrush(brush) | |
747 | DC.SetPen(wx.Pen(wx.NamedColour(color), width)) | |
748 | ||
749 | rect = self.rg[key] | |
750 | DC.DrawRectangle((rect.x, rect.y), (rect.width+1, rect.height+1)) | |
751 | ||
752 | DC.EndDrawing() | |
753 | ||
754 | # set the day selection | |
755 | ||
756 | def SetDay(self, day): | |
757 | day = day + self.st_pos - 1 | |
758 | self.SelectDay(day) | |
759 | ||
760 | def SelectDay(self, key): | |
761 | sel_size = 1 | |
762 | # clear large selection | |
763 | self.DrawRect(self.sel_key, self.back_color, sel_size) | |
764 | ||
765 | if self.hide_grid is False: | |
766 | self.DrawRect(self.sel_key, self.grid_color) | |
767 | ||
768 | self.DrawRect(key, self.sel_color, sel_size) | |
769 | # store last used by | |
770 | self.sel_key = key | |
771 | self.select_day = None | |
772 | ||
773 | def ClearDsp(self): | |
774 | self.Clear() | |
775 | ||
776 | class CalenDlg(wx.Dialog): | |
777 | def __init__(self, parent, month=None, day = None, year=None): | |
778 | wx.Dialog.__init__(self, parent, -1, "Event Calendar", wx.DefaultPosition, (280, 360)) | |
779 | ||
780 | # set the calendar and attributes | |
781 | self.calend = Calendar(self, -1, (20, 60), (240, 200)) | |
782 | ||
783 | if month == None: | |
784 | self.calend.SetCurrentDay() | |
785 | start_month = self.calend.GetMonth() | |
786 | start_year = self.calend.GetYear() | |
787 | else: | |
788 | self.calend.month = start_month = month | |
789 | self.calend.year = start_year = year | |
790 | self.calend.SetDayValue(day) | |
791 | ||
792 | self.calend.HideTitle() | |
793 | self.ResetDisplay() | |
794 | ||
795 | # get month list from DateTime | |
796 | monthlist = GetMonthList() | |
797 | ||
798 | # select the month | |
799 | self.date = wx.ComboBox(self, -1, Month[start_month], (20, 20), (90, -1), | |
800 | monthlist, wx.CB_DROPDOWN) | |
801 | self.Bind(wx.EVT_COMBOBOX, self.EvtComboBox, self.date) | |
802 | ||
803 | # alternate spin button to control the month | |
804 | h = self.date.GetSize().height | |
805 | self.m_spin = wx.SpinButton(self, -1, (130, 20), (h*2, h), wx.SP_VERTICAL) | |
806 | self.m_spin.SetRange(1, 12) | |
807 | self.m_spin.SetValue(start_month) | |
808 | self.Bind(wx.EVT_SPIN, self.OnMonthSpin, self.m_spin) | |
809 | ||
810 | # spin button to control the year | |
811 | self.dtext = wx.TextCtrl(self, -1, str(start_year), (160, 20), (60, -1)) | |
812 | h = self.dtext.GetSize().height | |
813 | ||
814 | self.y_spin = wx.SpinButton(self, -1, (220, 20), (h*2, h), wx.SP_VERTICAL) | |
815 | self.y_spin.SetRange(1980, 2010) | |
816 | self.y_spin.SetValue(start_year) | |
817 | ||
818 | self.Bind(wx.EVT_SPIN, self.OnYrSpin, self.y_spin) | |
819 | self.Bind(EVT_CALENDAR, self.MouseClick, self.calend) | |
820 | ||
821 | x_pos = 50 | |
822 | y_pos = 280 | |
823 | but_size = (60, 25) | |
824 | ||
825 | btn = wx.Button(self, -1, ' Ok ', (x_pos, y_pos), but_size) | |
826 | self.Bind(wx.EVT_BUTTON, self.OnOk, btn) | |
827 | ||
828 | btn = wx.Button(self, -1, ' Close ', (x_pos + 120, y_pos), but_size) | |
829 | self.Bind(wx.EVT_BUTTON, self.OnCancel, btn) | |
830 | ||
831 | def OnOk(self, event): | |
832 | self.EndModal(wx.ID_OK) | |
833 | ||
834 | def OnCancel(self, event): | |
835 | self.EndModal(wx.ID_CANCEL) | |
836 | ||
837 | # log the mouse clicks | |
838 | def MouseClick(self, evt): | |
839 | self.month = evt.month | |
840 | # result click type and date | |
841 | self.result = [evt.click, str(evt.day), Month[evt.month], str(evt.year)] | |
842 | ||
843 | if evt.click == 'DLEFT': | |
844 | self.EndModal(wx.ID_OK) | |
845 | ||
846 | # month and year spin selection routines | |
847 | def OnMonthSpin(self, event): | |
848 | month = event.GetPosition() | |
849 | self.date.SetValue(Month[month]) | |
850 | self.calend.SetMonth(month) | |
851 | self.calend.Refresh() | |
852 | ||
853 | def OnYrSpin(self, event): | |
854 | year = event.GetPosition() | |
855 | self.dtext.SetValue(str(year)) | |
856 | self.calend.SetYear(year) | |
857 | self.calend.Refresh() | |
858 | ||
859 | def EvtComboBox(self, event): | |
860 | name = event.GetString() | |
861 | monthval = self.date.FindString(name) | |
862 | self.m_spin.SetValue(monthval+1) | |
863 | ||
864 | self.calend.SetMonth(monthval+1) | |
865 | self.ResetDisplay() | |
866 | ||
867 | # set the calendar for highlighted days | |
868 | ||
869 | def ResetDisplay(self): | |
870 | month = self.calend.GetMonth() | |
871 | self.calend.Refresh() | |
872 | ||
873 | ||
874 |