]>
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 | # | |
b25cb7db RD |
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. | |
9472490a RD |
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 | |
dba0885d RD |
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. | |
c78a1b76 RD |
74 | # |
75 | # 12/10/2006 - Walter Barnes walter_barnes05@yahoo.com | |
76 | # o Fixed CalDraw to properly render months that start on a Sunday. | |
d819fba8 RD |
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. | |
dba0885d | 83 | |
b25cb7db RD |
84 | |
85 | import wx | |
d14a1e28 RD |
86 | |
87 | from CDate import * | |
88 | ||
d14a1e28 RD |
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 | ||
b25cb7db RD |
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 | ||
d14a1e28 RD |
108 | BusCalDays = [0, 1, 2, 3, 4, 5, 6] |
109 | ||
b881fc78 RD |
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) | |
d14a1e28 RD |
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 | ||
b25cb7db RD |
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) | |
9472490a RD |
143 | colors[COLOR_HIGHLIGHT_FONT] = wx.SystemSettings_GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT) |
144 | colors[COLOR_HIGHLIGHT_BACKGROUND] = wx.SystemSettings_GetColour(wx.SYS_COLOUR_HIGHLIGHT) | |
b25cb7db | 145 | return colors |
b881fc78 RD |
146 | # calendar drawing routing |
147 | ||
d14a1e28 RD |
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 | ||
b25cb7db RD |
157 | self.gridx = [] |
158 | self.gridy = [] | |
159 | ||
d14a1e28 RD |
160 | self.DefParms() |
161 | ||
162 | def DefParms(self): | |
c1ea08d3 RD |
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 | |
d14a1e28 | 166 | |
b881fc78 RD |
167 | self.num_align_horz = wx.ALIGN_CENTRE # alignment of numbers |
168 | self.num_align_vert = wx.ALIGN_CENTRE | |
d14a1e28 RD |
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 | ||
b25cb7db | 176 | self.colors = DefaultColors() |
d14a1e28 | 177 | |
b881fc78 RD |
178 | self.font = wx.SWISS |
179 | self.bold = wx.NORMAL | |
d14a1e28 RD |
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 | ||
b25cb7db RD |
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) | |
d14a1e28 RD |
196 | |
197 | def SetSize(self, size): | |
b881fc78 RD |
198 | self.set_sizew = size[0] |
199 | self.set_sizeh = size[1] | |
d14a1e28 RD |
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 | ||
b25cb7db RD |
207 | self.set_y_mrg = 1 # start of vertical draw default |
208 | self.set_x_mrg = 1 | |
209 | self.set_y_end = 1 | |
d14a1e28 RD |
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): | |
b25cb7db RD |
215 | self.set_x_mrg = xmarg |
216 | self.set_y_mrg = ymarg | |
d14a1e28 RD |
217 | self.set_y_end = ymarg |
218 | ||
219 | def InitScale(self): # scale position values | |
c8431295 RD |
220 | self.sizew = int(self.set_sizew * self.pwidth) |
221 | self.sizeh = int(self.set_sizeh * self.pheight) | |
d14a1e28 | 222 | |
c8431295 RD |
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) | |
d14a1e28 | 225 | |
c8431295 RD |
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) | |
d14a1e28 RD |
229 | |
230 | def DrawCal(self, DC, sel_lst=[]): | |
d14a1e28 RD |
231 | self.InitScale() |
232 | ||
b25cb7db | 233 | self.DrawBorder(DC) |
b881fc78 | 234 | |
d14a1e28 | 235 | if self.hide_title is False: |
b25cb7db | 236 | self.DrawMonth(DC) |
d14a1e28 RD |
237 | |
238 | self.Center() | |
239 | ||
b25cb7db | 240 | self.DrawGrid(DC) |
d14a1e28 | 241 | self.GetRect() |
d14a1e28 RD |
242 | if self.show_weekend is True: # highlight weekend dates |
243 | self.SetWeekEnd() | |
244 | ||
245 | self.AddSelect(sel_lst) # overrides the weekend highlight | |
246 | ||
b25cb7db RD |
247 | self.DrawSel(DC) # highlighted days |
248 | self.DrawWeek(DC) | |
249 | self.DrawNum(DC) | |
d14a1e28 RD |
250 | |
251 | def AddSelect(self, list, cfont=None, cbackgrd = None): | |
252 | if cfont is None: | |
b25cb7db | 253 | cfont = self.colors[COLOR_SELECTION_FONT] # font digit color |
d14a1e28 | 254 | if cbackgrd is None: |
b25cb7db | 255 | cbackgrd = self.colors[COLOR_SELECTION_BACKGROUND] # select background color |
d14a1e28 RD |
256 | |
257 | for val in list: | |
258 | self.cal_sel[val] = (cfont, cbackgrd) | |
259 | ||
b881fc78 | 260 | # draw border around the outside of the main display rectangle |
9472490a RD |
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): | |
d14a1e28 | 274 | if self.outer_border is True: |
9472490a RD |
275 | DC.SetBrush(wx.TRANSPARENT_BRUSH) |
276 | DC.SetPen(wx.Pen(MakeColor(self.colors[COLOR_HIGHLIGHT_BACKGROUND]), style=wx.DOT)) | |
b881fc78 RD |
277 | # full display window area |
278 | rect = wx.Rect(self.cx_st, self.cy_st, self.sizew, self.sizeh) | |
b25cb7db | 279 | DC.DrawRectangleRect(rect) |
d14a1e28 RD |
280 | |
281 | def DrawNumVal(self): | |
282 | self.DrawNum() | |
283 | ||
c1ea08d3 | 284 | # calculate the calendar days and offset position |
d14a1e28 RD |
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 | |
b881fc78 | 295 | |
d14a1e28 RD |
296 | if self.cal_type == "NORMAL": |
297 | start_pos = dow+1 | |
298 | else: | |
299 | start_pos = dow | |
300 | ||
c78a1b76 RD |
301 | if start_pos > 6: |
302 | start_pos = 0 | |
303 | ||
d14a1e28 RD |
304 | self.st_pos = start_pos |
305 | ||
b25cb7db | 306 | self.cal_days = [] |
d14a1e28 | 307 | for i in range(start_pos): |
b25cb7db | 308 | self.cal_days.append('') |
b881fc78 | 309 | |
d14a1e28 RD |
310 | i = 1 |
311 | while i <= dim: | |
b25cb7db | 312 | self.cal_days.append(str(i)) |
d14a1e28 | 313 | i = i + 1 |
b881fc78 | 314 | |
f062c0e2 | 315 | self.end_pos = start_pos + dim - 1 |
3e7c6081 | 316 | |
d14a1e28 RD |
317 | return start_pos |
318 | ||
b25cb7db RD |
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 | ||
d14a1e28 | 325 | date = 6 - int(self.dow) # start day of first saturday |
c78a1b76 RD |
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 | |
b881fc78 | 329 | |
d14a1e28 | 330 | while date <= self.dim: |
b25cb7db | 331 | self.cal_sel[date] = (self.GetColor(COLOR_WEEKEND_FONT), self.GetColor(COLOR_WEEKEND_BACKGROUND)) # Saturday |
d14a1e28 | 332 | date = date + 1 |
b881fc78 | 333 | |
d14a1e28 | 334 | if date <= self.dim: |
b25cb7db | 335 | self.cal_sel[date] = (self.GetColor(COLOR_WEEKEND_FONT), self.GetColor(COLOR_WEEKEND_BACKGROUND)) # Sunday |
d14a1e28 RD |
336 | date = date + 6 |
337 | else: | |
338 | date = date + 7 | |
339 | ||
c1ea08d3 RD |
340 | # get the display rectange list of the day grid |
341 | def GetRect(self): | |
d14a1e28 | 342 | cnt = 0 |
9472490a RD |
343 | h = 0 |
344 | w = 0 | |
d14a1e28 | 345 | for y in self.gridy[1:-1]: |
b25cb7db RD |
346 | if y == self.gridy[-2]: |
347 | h = h + self.restH | |
348 | ||
d14a1e28 | 349 | for x in self.gridx[:-1]: |
c8431295 RD |
350 | assert type(y) == int |
351 | assert type(x) == int | |
b25cb7db RD |
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 | ||
d14a1e28 RD |
361 | self.rg[cnt] = rect |
362 | cnt = cnt + 1 | |
b881fc78 | 363 | |
d14a1e28 RD |
364 | return self.rg |
365 | ||
366 | def GetCal(self): | |
b25cb7db | 367 | return self.cal_days |
d14a1e28 RD |
368 | |
369 | def GetOffset(self): | |
370 | return self.st_pos | |
371 | ||
c1ea08d3 | 372 | # month and year title |
b25cb7db | 373 | def DrawMonth(self, DC): |
d14a1e28 RD |
374 | month = Month[self.month] |
375 | ||
376 | sizef = 11 | |
377 | if self.sizeh < _MIDSIZE: | |
378 | sizef = 10 | |
379 | ||
b881fc78 | 380 | f = wx.Font(sizef, self.font, wx.NORMAL, self.bold) |
b25cb7db | 381 | DC.SetFont(f) |
d14a1e28 | 382 | |
b25cb7db | 383 | tw,th = DC.GetTextExtent(month) |
d14a1e28 | 384 | adjust = self.cx_st + (self.sizew-tw)/2 |
d7403ad2 | 385 | DC.DrawText(month, adjust, self.cy_st + th) |
d14a1e28 RD |
386 | |
387 | year = str(self.year) | |
b25cb7db | 388 | tw,th = DC.GetTextExtent(year) |
d14a1e28 RD |
389 | adjust = self.sizew - tw - self.x_mrg |
390 | ||
391 | self.title_offset = th * 2 | |
392 | ||
b881fc78 | 393 | f = wx.Font(sizef, self.font, wx.NORMAL, self.bold) |
b25cb7db | 394 | DC.SetFont(f) |
d7403ad2 | 395 | DC.DrawText(year, self.cx_st + adjust, self.cy_st + th) |
d14a1e28 | 396 | |
b25cb7db RD |
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] | |
d14a1e28 | 402 | |
b881fc78 RD |
403 | f = wx.Font(10, self.font, wx.NORMAL, self.bold) # initial font setting |
404 | ||
d14a1e28 RD |
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) | |
b25cb7db RD |
410 | DC.SetFont(f) |
411 | tw,th = DC.GetTextExtent(test_day) | |
b881fc78 | 412 | |
d14a1e28 RD |
413 | if tw < width and th < height: |
414 | break | |
b881fc78 | 415 | |
d14a1e28 RD |
416 | test_size = test_size - 1 |
417 | else: | |
418 | f.SetPointSize(self.week_size) # set fixed size | |
b25cb7db | 419 | DC.SetFont(f) |
d14a1e28 | 420 | |
b25cb7db | 421 | DC.SetTextForeground(MakeColor(self.colors[COLOR_HEADER_FONT])) |
d14a1e28 RD |
422 | |
423 | cnt_x = 0 | |
424 | cnt_y = 0 | |
425 | ||
b25cb7db RD |
426 | brush = wx.Brush(MakeColor(self.colors[COLOR_HEADER_BACKGROUND]), wx.SOLID) |
427 | DC.SetBrush(brush) | |
d14a1e28 RD |
428 | |
429 | if self.cal_type == "NORMAL": | |
430 | cal_days = CalDays | |
431 | else: | |
432 | cal_days = BusCalDays | |
433 | ||
434 | for val in cal_days: | |
b25cb7db RD |
435 | if val == cal_days[-1]: |
436 | width = width + self.restW | |
437 | ||
d14a1e28 | 438 | day = AbrWeekday[val] |
b881fc78 | 439 | |
d14a1e28 RD |
440 | if self.sizew < 200: |
441 | day = day[0] | |
b881fc78 | 442 | |
b25cb7db RD |
443 | dw,dh = DC.GetTextExtent(day) |
444 | ||
d14a1e28 RD |
445 | diffx = (width-dw)/2 |
446 | diffy = (height-dh)/2 | |
447 | ||
448 | x = self.gridx[cnt_x] | |
449 | y = self.gridy[cnt_y] | |
b25cb7db RD |
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) | |
d7403ad2 | 457 | DC.DrawRectanglePointSize( pointXY, pointWH) |
b25cb7db RD |
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) | |
d7403ad2 | 466 | DC.DrawLinePoint(startPoint, endPoint ) |
b25cb7db RD |
467 | |
468 | # draw the vertical hilight | |
469 | startPoint = wx.Point(x + 1 , y + 1) | |
470 | endPoint = wx.Point(x + 1, y + height - 2) | |
d7403ad2 | 471 | DC.DrawLinePoint(startPoint, endPoint ) |
b25cb7db RD |
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) | |
d7403ad2 | 479 | DC.DrawLinePoint(startPoint, endPoint ) |
b25cb7db RD |
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) | |
d7403ad2 | 484 | DC.DrawLinePoint(startPoint, endPoint ) |
b25cb7db RD |
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) | |
d7403ad2 | 491 | DC.DrawTextPoint(day, point) |
d14a1e28 RD |
492 | cnt_x = cnt_x + 1 |
493 | ||
9472490a | 494 | def _CalcFontSize(self, DC, f): |
d14a1e28 RD |
495 | if self.num_auto == True: |
496 | test_size = self.max_num_size # max size | |
497 | test_day = ' 99 ' | |
b881fc78 | 498 | |
d14a1e28 RD |
499 | while test_size > 2: |
500 | f.SetPointSize(test_size) | |
b25cb7db RD |
501 | DC.SetFont(f) |
502 | tw,th = DC.GetTextExtent(test_day) | |
b881fc78 | 503 | |
b25cb7db | 504 | if tw < self.cellW and th < self.cellH: |
d14a1e28 RD |
505 | sizef = test_size |
506 | break | |
507 | test_size = test_size - 1 | |
508 | else: | |
509 | f.SetPointSize(self.num_size) # set fixed size | |
b25cb7db | 510 | DC.SetFont(f) |
d14a1e28 | 511 | |
9472490a RD |
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 | ||
d14a1e28 RD |
517 | cnt_x = 0 |
518 | cnt_y = 1 | |
b25cb7db | 519 | for val in self.cal_days: |
d14a1e28 RD |
520 | x = self.gridx[cnt_x] |
521 | y = self.gridy[cnt_y] | |
522 | ||
9472490a | 523 | self._DrawDayText(x, y, val, f, DC) |
d14a1e28 | 524 | |
b25cb7db RD |
525 | if cnt_x < 6: |
526 | cnt_x = cnt_x + 1 | |
d14a1e28 | 527 | else: |
b25cb7db RD |
528 | cnt_x = 0 |
529 | cnt_y = cnt_y + 1 | |
d14a1e28 | 530 | |
9472490a RD |
531 | def _DrawDayText(self, x, y, text, font, DC): |
532 | ||
b25cb7db RD |
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 | |
d14a1e28 | 550 | |
b25cb7db | 551 | adj_h = adj_h + self.num_indent_horz |
d14a1e28 | 552 | |
b25cb7db RD |
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 | |
d14a1e28 | 559 | |
b25cb7db | 560 | adj_v = adj_v + self.num_indent_vert |
b881fc78 | 561 | |
d7403ad2 | 562 | DC.DrawTextPoint(text, (x+adj_h, y+adj_v)) |
9472490a RD |
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 | ||
3e7c6081 RD |
568 | if key > self.end_pos: |
569 | key = self.end_pos | |
570 | ||
9472490a RD |
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 | ||
d14a1e28 | 578 | |
c1ea08d3 RD |
579 | # calculate the dimensions in the center of the drawing area |
580 | def Center(self): | |
b25cb7db RD |
581 | borderW = self.x_mrg * 2 |
582 | borderH = self.y_mrg + self.y_end + self.title_offset | |
583 | ||
9472490a RD |
584 | self.cellW = int((self.sizew - borderW)/7) |
585 | self.cellH = int((self.sizeh - borderH)/7) | |
d14a1e28 | 586 | |
b25cb7db | 587 | self.restW = ((self.sizew - borderW)%7 ) - 1 |
d14a1e28 | 588 | |
c1ea08d3 | 589 | # week title adjustment |
9472490a | 590 | self.weekHdrCellH = int(self.cellH * self.cal_week_scale) |
b25cb7db RD |
591 | # recalculate the cell height exkl. the week header and |
592 | # subtracting the size | |
9472490a | 593 | self.cellH = int((self.sizeh - borderH - self.weekHdrCellH)/6) |
b25cb7db | 594 | |
9472490a | 595 | self.restH = ((self.sizeh - borderH - self.weekHdrCellH)%6 ) - 1 |
b25cb7db RD |
596 | self.calW = self.cellW * 7 |
597 | self.calH = self.cellH * 6 + self.weekHdrCellH | |
d14a1e28 | 598 | |
c1ea08d3 | 599 | # highlighted selected days |
b25cb7db RD |
600 | def DrawSel(self, DC): |
601 | ||
d14a1e28 RD |
602 | for key in self.cal_sel.keys(): |
603 | sel_color = self.cal_sel[key][1] | |
b25cb7db RD |
604 | brush = wx.Brush(MakeColor(sel_color), wx.SOLID) |
605 | DC.SetBrush(brush) | |
d14a1e28 RD |
606 | |
607 | if self.hide_grid is False: | |
b25cb7db | 608 | DC.SetPen(wx.Pen(MakeColor(self.colors[COLOR_GRID_LINES]), 0)) |
d14a1e28 | 609 | else: |
b25cb7db RD |
610 | DC.SetPen(wx.Pen(MakeColor(self.colors[COLOR_BACKGROUND]), 0)) |
611 | ||
d14a1e28 RD |
612 | nkey = key + self.st_pos -1 |
613 | rect = self.rg[nkey] | |
b25cb7db | 614 | |
d7403ad2 | 615 | DC.DrawRectangleRect(rect) |
d14a1e28 | 616 | |
c1ea08d3 | 617 | # calculate and draw the grid lines |
b25cb7db RD |
618 | def DrawGrid(self, DC): |
619 | DC.SetPen(wx.Pen(MakeColor(self.colors[COLOR_GRID_LINES]), 0)) | |
d14a1e28 RD |
620 | |
621 | self.gridx = [] | |
622 | self.gridy = [] | |
623 | ||
624 | self.x_st = self.cx_st + self.x_mrg | |
c1ea08d3 RD |
625 | # start postion of draw |
626 | self.y_st = self.cy_st + self.y_mrg + self.title_offset | |
b25cb7db | 627 | |
d14a1e28 RD |
628 | x1 = self.x_st |
629 | y1 = self.y_st | |
b25cb7db | 630 | y2 = y1 + self.calH + self.restH |
b881fc78 | 631 | |
d14a1e28 | 632 | for i in range(8): |
b25cb7db RD |
633 | if i == 7: |
634 | x1 = x1 + self.restW | |
635 | ||
d14a1e28 | 636 | if self.hide_grid is False: |
d7403ad2 | 637 | DC.DrawLinePoint((x1, y1), (x1, y2)) |
b25cb7db | 638 | |
d14a1e28 | 639 | self.gridx.append(x1) |
b25cb7db RD |
640 | |
641 | x1 = x1 + self.cellW | |
d14a1e28 RD |
642 | |
643 | x1 = self.x_st | |
644 | y1 = self.y_st | |
b25cb7db | 645 | x2 = x1 + self.calW + self.restW |
b881fc78 | 646 | |
d14a1e28 | 647 | for i in range(8): |
b25cb7db RD |
648 | if i == 7: |
649 | y1 = y1 + self.restH | |
650 | ||
d14a1e28 | 651 | if self.hide_grid is False: |
d7403ad2 | 652 | DC.DrawLinePoint((x1, y1), (x2, y1)) |
b881fc78 | 653 | |
d14a1e28 | 654 | self.gridy.append(y1) |
b881fc78 | 655 | |
d14a1e28 | 656 | if i == 0: |
b25cb7db | 657 | y1 = y1 + self.weekHdrCellH |
d14a1e28 | 658 | else: |
b25cb7db RD |
659 | y1 = y1 + self.cellH |
660 | ||
661 | def GetColor(self, name): | |
662 | return MakeColor(self.colors[name]) | |
d14a1e28 | 663 | |
b25cb7db RD |
664 | def SetColor(self, name, value): |
665 | self.colors[name] = MakeColor(value) | |
d14a1e28 RD |
666 | |
667 | class PrtCalDraw(CalDraw): | |
668 | def InitValues(self): | |
669 | self.rg = {} | |
670 | self.cal_sel = {} | |
c1ea08d3 RD |
671 | # start draw border location |
672 | self.set_cx_st = 1.0 | |
d14a1e28 RD |
673 | self.set_cy_st = 1.0 |
674 | ||
c1ea08d3 RD |
675 | # draw offset position |
676 | self.set_y_mrg = 0.2 | |
d14a1e28 RD |
677 | self.set_x_mrg = 0.2 |
678 | self.set_y_end = 0.2 | |
679 | ||
b881fc78 RD |
680 | # calculate the dimensions in the center of the drawing area |
681 | def SetPSize(self, pwidth, pheight): | |
d14a1e28 RD |
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 | ||
9472490a RD |
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) | |
d14a1e28 | 693 | |
9472490a | 694 | self.hasFocus = False |
c1ea08d3 | 695 | # set the calendar control attributes |
d14a1e28 | 696 | |
d14a1e28 | 697 | self.hide_grid = False |
d14a1e28 RD |
698 | self.hide_title = False |
699 | self.show_weekend = False | |
700 | self.cal_type = "NORMAL" | |
b25cb7db RD |
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 | |
d14a1e28 RD |
708 | |
709 | self.select_list = [] | |
710 | ||
b25cb7db | 711 | self.SetBackgroundColour(MakeColor(self.colors[COLOR_BACKGROUND])) |
b881fc78 RD |
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) | |
d819fba8 RD |
716 | self.Bind(wx.EVT_MIDDLE_DOWN, self.OnMiddleEvent) |
717 | self.Bind(wx.EVT_MIDDLE_DCLICK, self.OnMiddleDEvent) | |
9472490a RD |
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) | |
d14a1e28 RD |
721 | |
722 | self.sel_key = None # last used by | |
723 | self.sel_lst = [] # highlighted selected days | |
724 | ||
c1ea08d3 RD |
725 | # default calendar for current month |
726 | self.SetNow() | |
d14a1e28 RD |
727 | |
728 | self.size = None | |
729 | self.set_day = None | |
730 | ||
b881fc78 RD |
731 | self.Bind(wx.EVT_PAINT, self.OnPaint) |
732 | self.Bind(wx.EVT_SIZE, self.OnSize) | |
9472490a RD |
733 | |
734 | def AcceptsFocus(self): | |
735 | return self.IsShown() and self.IsEnabled() | |
736 | ||
b25cb7db RD |
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 | ||
c1ea08d3 | 743 | # control some of the main calendar attributes |
d14a1e28 RD |
744 | |
745 | def HideTitle(self): | |
746 | self.hide_title = True | |
747 | ||
748 | def HideGrid(self): | |
749 | self.hide_grid = True | |
750 | ||
c1ea08d3 | 751 | # determine the calendar rectangle click area and draw a selection |
d14a1e28 RD |
752 | |
753 | def ProcessClick(self, event): | |
754 | self.x, self.y = event.GetX(), event.GetY() | |
d819fba8 RD |
755 | self.shiftkey = event.ShiftDown() |
756 | self.ctrlkey = event.ControlDown() | |
d14a1e28 RD |
757 | key = self.GetDayHit(self.x, self.y) |
758 | self.SelectDay(key) | |
759 | ||
c1ea08d3 | 760 | # tab mouse click events and process |
d14a1e28 RD |
761 | |
762 | def OnLeftEvent(self, event): | |
763 | self.click = 'LEFT' | |
d14a1e28 RD |
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 | ||
d819fba8 RD |
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 | ||
9472490a RD |
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 | ||
a3cee65e | 799 | key_code = event.GetKeyCode() |
9472490a RD |
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 | ||
d14a1e28 RD |
845 | def SetSize(self, set_size): |
846 | self.size = set_size | |
847 | ||
848 | def SetSelDay(self, sel): | |
c1ea08d3 RD |
849 | # list of highlighted days |
850 | self.sel_lst = sel | |
d14a1e28 | 851 | |
c1ea08d3 | 852 | # get the current date |
d14a1e28 RD |
853 | def SetNow(self): |
854 | dt = now() | |
855 | self.month = dt.month | |
856 | self.year = dt.year | |
857 | self.day = dt.day | |
858 | ||
c1ea08d3 | 859 | # set the current day |
d14a1e28 RD |
860 | def SetCurrentDay(self): |
861 | self.SetNow() | |
862 | self.set_day = self.day | |
863 | ||
c1ea08d3 | 864 | # get the date, day, month, year set in calendar |
d14a1e28 RD |
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 | ||
c1ea08d3 | 878 | # set the day, month, and year |
d14a1e28 RD |
879 | |
880 | def SetDayValue(self, day): | |
881 | self.set_day = day | |
02b800ce | 882 | self.day = day |
d14a1e28 RD |
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 | ||
c1ea08d3 | 894 | # increment year and month |
d14a1e28 RD |
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 | ||
c1ea08d3 | 918 | # test to see if the selection has a date and create event |
d14a1e28 RD |
919 | |
920 | def TestDay(self, key): | |
921 | try: | |
b25cb7db | 922 | self.day = int(self.cal_days[key]) |
d14a1e28 RD |
923 | except: |
924 | return None | |
b25cb7db | 925 | |
d14a1e28 RD |
926 | if self.day == "": |
927 | return None | |
928 | else: | |
b881fc78 RD |
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()) | |
d14a1e28 RD |
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 | ||
c1ea08d3 | 939 | # find the clicked area rectangle |
d14a1e28 RD |
940 | |
941 | def GetDayHit(self, mx, my): | |
942 | for key in self.rg.keys(): | |
943 | val = self.rg[key] | |
b881fc78 RD |
944 | ms_rect = wx.Rect(mx, my, 1, 1) |
945 | if wx.IntersectRect(ms_rect, val) is not None: | |
d14a1e28 RD |
946 | result = self.TestDay(key) |
947 | return result | |
b881fc78 | 948 | |
d14a1e28 RD |
949 | return None |
950 | ||
c1ea08d3 | 951 | # calendar drawing |
d14a1e28 | 952 | |
c1ea08d3 RD |
953 | def SetWeekColor(self, font_color, week_color): |
954 | # set font and background color for week title | |
b25cb7db RD |
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 | ||
d14a1e28 RD |
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): | |
c1ea08d3 RD |
969 | # highlight weekend |
970 | self.show_weekend = True | |
d14a1e28 RD |
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): | |
b881fc78 | 980 | DC = wx.PaintDC(self) |
d14a1e28 RD |
981 | self.DoDrawing(DC) |
982 | ||
983 | def DoDrawing(self, DC): | |
b25cb7db | 984 | #DC = wx.PaintDC(self) |
d14a1e28 RD |
985 | DC.BeginDrawing() |
986 | ||
b25cb7db RD |
987 | try: |
988 | cal = self.caldraw | |
989 | except: | |
990 | self.caldraw = CalDraw(self) | |
991 | cal = self.caldraw | |
d14a1e28 | 992 | |
d14a1e28 | 993 | cal.hide_grid = self.hide_grid |
d14a1e28 RD |
994 | cal.hide_title = self.hide_title |
995 | cal.show_weekend = self.show_weekend | |
996 | cal.cal_type = self.cal_type | |
b25cb7db RD |
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 | |
d14a1e28 RD |
1001 | |
1002 | if self.size is None: | |
1003 | size = self.GetClientSize() | |
1004 | else: | |
1005 | size = self.size | |
1006 | ||
c1ea08d3 | 1007 | # drawing attributes |
d14a1e28 | 1008 | |
d14a1e28 RD |
1009 | cal.SetSize(size) |
1010 | cal.SetCal(self.year, self.month) | |
b881fc78 | 1011 | |
b25cb7db RD |
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 | ||
d14a1e28 RD |
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() | |
b25cb7db | 1023 | self.cal_days = cal.GetCal() |
d14a1e28 RD |
1024 | self.st_pos = cal.GetOffset() |
1025 | self.ymax = DC.MaxY() | |
1026 | ||
1027 | if self.set_day != None: | |
1028 | self.SetDay(self.set_day) | |
b881fc78 | 1029 | |
d14a1e28 RD |
1030 | DC.EndDrawing() |
1031 | ||
c1ea08d3 | 1032 | # draw the selection rectangle |
9472490a RD |
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] | |
d7403ad2 | 1056 | DC.DrawRectangle(rect.x+1, rect.y+1, rect.width-2, rect.height-2) |
9472490a RD |
1057 | |
1058 | self.caldraw.DrawDayText(DC,key) | |
1059 | ||
1060 | DC.EndDrawing() | |
1061 | ||
1062 | def DrawRectOrg(self, key, fgcolor = 'BLACK', width = 0): | |
d14a1e28 RD |
1063 | if key == None: |
1064 | return | |
b881fc78 RD |
1065 | |
1066 | DC = wx.ClientDC(self) | |
d14a1e28 RD |
1067 | DC.BeginDrawing() |
1068 | ||
b881fc78 | 1069 | brush = wx.Brush(wx.Colour(0, 0xFF, 0x80), wx.TRANSPARENT) |
d14a1e28 | 1070 | DC.SetBrush(brush) |
b25cb7db RD |
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)) | |
d14a1e28 RD |
1076 | |
1077 | rect = self.rg[key] | |
d7403ad2 | 1078 | DC.DrawRectangleRect(rect) |
d14a1e28 RD |
1079 | |
1080 | DC.EndDrawing() | |
1081 | ||
c1ea08d3 | 1082 | # set the day selection |
d14a1e28 RD |
1083 | |
1084 | def SetDay(self, day): | |
9472490a RD |
1085 | d = day + self.st_pos - 1 |
1086 | self.SelectDay(d) | |
d14a1e28 | 1087 | |
9472490a RD |
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 | ||
d14a1e28 RD |
1100 | def SelectDay(self, key): |
1101 | sel_size = 1 | |
c1ea08d3 | 1102 | # clear large selection |
9472490a RD |
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) | |
b881fc78 | 1107 | |
9472490a | 1108 | self.DrawRect(key, self.GetColor(COLOR_HIGHLIGHT_BACKGROUND), self.GetColor(COLOR_HIGHLIGHT_FONT), sel_size) |
c1ea08d3 RD |
1109 | # store last used by |
1110 | self.sel_key = key | |
d14a1e28 RD |
1111 | |
1112 | def ClearDsp(self): | |
1113 | self.Clear() | |
b25cb7db RD |
1114 | def SetMargin(self, xmarg, ymarg): |
1115 | self.set_x_mrg = xmarg | |
1116 | self.set_y_mrg = ymarg | |
1117 | self.set_y_end = ymarg | |
9472490a RD |
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) | |
d14a1e28 | 1133 | |
b881fc78 | 1134 | class CalenDlg(wx.Dialog): |
d14a1e28 | 1135 | def __init__(self, parent, month=None, day = None, year=None): |
b881fc78 | 1136 | wx.Dialog.__init__(self, parent, -1, "Event Calendar", wx.DefaultPosition, (280, 360)) |
dba0885d RD |
1137 | self.result = None |
1138 | ||
c1ea08d3 RD |
1139 | # set the calendar and attributes |
1140 | self.calend = Calendar(self, -1, (20, 60), (240, 200)) | |
b881fc78 | 1141 | |
d14a1e28 RD |
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 | ||
c1ea08d3 | 1154 | # get month list from DateTime |
d14a1e28 RD |
1155 | monthlist = GetMonthList() |
1156 | ||
c1ea08d3 | 1157 | # select the month |
b881fc78 RD |
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) | |
d14a1e28 | 1161 | |
c1ea08d3 | 1162 | # alternate spin button to control the month |
d14a1e28 | 1163 | h = self.date.GetSize().height |
dba0885d | 1164 | self.m_spin = wx.SpinButton(self, -1, (115, 20), (h*1.5, h), wx.SP_VERTICAL) |
d14a1e28 RD |
1165 | self.m_spin.SetRange(1, 12) |
1166 | self.m_spin.SetValue(start_month) | |
b881fc78 | 1167 | self.Bind(wx.EVT_SPIN, self.OnMonthSpin, self.m_spin) |
d14a1e28 | 1168 | |
c1ea08d3 | 1169 | # spin button to control the year |
b881fc78 | 1170 | self.dtext = wx.TextCtrl(self, -1, str(start_year), (160, 20), (60, -1)) |
d14a1e28 RD |
1171 | h = self.dtext.GetSize().height |
1172 | ||
dba0885d | 1173 | self.y_spin = wx.SpinButton(self, -1, (225, 20), (h*1.5, h), wx.SP_VERTICAL) |
d14a1e28 RD |
1174 | self.y_spin.SetRange(1980, 2010) |
1175 | self.y_spin.SetValue(start_year) | |
1176 | ||
b881fc78 RD |
1177 | self.Bind(wx.EVT_SPIN, self.OnYrSpin, self.y_spin) |
1178 | self.Bind(EVT_CALENDAR, self.MouseClick, self.calend) | |
d14a1e28 RD |
1179 | |
1180 | x_pos = 50 | |
1181 | y_pos = 280 | |
b881fc78 | 1182 | but_size = (60, 25) |
d14a1e28 | 1183 | |
dba0885d | 1184 | btn = wx.Button(self, wx.ID_OK, ' Ok ', (x_pos, y_pos), but_size) |
b881fc78 | 1185 | self.Bind(wx.EVT_BUTTON, self.OnOk, btn) |
d14a1e28 | 1186 | |
dba0885d | 1187 | btn = wx.Button(self, wx.ID_CANCEL, ' Close ', (x_pos + 120, y_pos), but_size) |
b881fc78 | 1188 | self.Bind(wx.EVT_BUTTON, self.OnCancel, btn) |
d14a1e28 | 1189 | |
dba0885d RD |
1190 | def OnOk(self, evt): |
1191 | self.result = ['None', str(self.calend.day), Month[self.calend.month], str(self.calend.year)] | |
b881fc78 | 1192 | self.EndModal(wx.ID_OK) |
d14a1e28 RD |
1193 | |
1194 | def OnCancel(self, event): | |
b881fc78 | 1195 | self.EndModal(wx.ID_CANCEL) |
d14a1e28 | 1196 | |
c1ea08d3 | 1197 | # log the mouse clicks |
d14a1e28 RD |
1198 | def MouseClick(self, evt): |
1199 | self.month = evt.month | |
c1ea08d3 RD |
1200 | # result click type and date |
1201 | self.result = [evt.click, str(evt.day), Month[evt.month], str(evt.year)] | |
d14a1e28 RD |
1202 | |
1203 | if evt.click == 'DLEFT': | |
b881fc78 | 1204 | self.EndModal(wx.ID_OK) |
d14a1e28 | 1205 | |
c1ea08d3 | 1206 | # month and year spin selection routines |
d14a1e28 RD |
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 | ||
c1ea08d3 | 1227 | # set the calendar for highlighted days |
d14a1e28 RD |
1228 | |
1229 | def ResetDisplay(self): | |
1230 | month = self.calend.GetMonth() | |
1231 | self.calend.Refresh() | |
1232 | ||
1fded56b | 1233 | |
1fded56b | 1234 |