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