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