]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxCalendar.py
correction for Mac OS X compilation
[wxWidgets.git] / wxPython / demo / wxCalendar.py
1 #----------------------------------------------------------------------------
2 # Name: wxCalendar.py
3 # Purpose: Calendar control display testing on panel for wxPython demo
4 #
5 # Author: Lorne White (email: lwhite1@planet.eon.net)
6 #
7 # Version 0.9
8 # Date: Feb 26, 2001
9 # Licence: wxWindows license
10 #----------------------------------------------------------------------------
11
12 from wxPython.wx import *
13 from wxPython.lib.calendar import wxCalendar, Month, PrtCalDraw, CalenDlg
14
15 import images
16 import os
17
18
19 # highlighted days in month
20
21 test_days ={ 0: [],
22 1: [3, 7, 9, 21],
23 2: [2, 10, 4, 9],
24 3: [4, 20, 29],
25 4: [1, 12, 22],
26 5: [2, 10, 15],
27 6: [4, 8, 17],
28 7: [6, 7, 8],
29 8: [5, 10, 20],
30 9: [1, 2, 5, 29],
31 10: [2, 4, 6, 22],
32 11: [6, 9, 12, 28, 29],
33 12: [8, 9, 10, 11, 20] }
34
35 # test of full window calendar control functions
36
37 def GetMonthList():
38 monthlist = []
39 for i in range(13):
40 name = Month[i]
41 if name != None:
42 monthlist.append(name)
43 return monthlist
44
45 class TestPanel(wxPanel):
46 def __init__(self, parent, log, frame):
47 wxPanel.__init__(self, parent, -1)
48
49 self.log = log
50 self.frame = frame
51
52 self.calend = wxCalendar(self, -1, wxPoint(100, 50), wxSize(200, 180))
53
54 # start_month = 2 # preselect the date for calendar
55 # start_year = 2001
56
57 start_month = self.calend.GetMonth() # get the current month & year
58 start_year = self.calend.GetYear()
59
60 # month list from DateTime module
61
62 monthlist = GetMonthList()
63
64 mID = NewId()
65 self.date = wxComboBox(self, mID, Month[start_month], wxPoint(100, 20), wxSize(90, -1), monthlist, wxCB_DROPDOWN)
66 EVT_COMBOBOX(self, mID, self.EvtComboBox)
67
68 # set start month and year
69
70 self.calend.SetMonth(start_month)
71 self.calend.SetYear(start_year)
72
73 # set attributes of calendar
74
75 self.calend.hide_title = TRUE
76 self.calend.HideGrid()
77 self.calend.SetWeekColor('WHITE', 'BLACK')
78
79 # display routine
80
81 self.ResetDisplay()
82
83 # mouse click event
84
85 self.Connect(self.calend.GetId(), -1, 2100, self.MouseClick)
86
87 # scroll bar for month selection
88
89 mID = NewId()
90 self.scroll = wxScrollBar(self, mID, wxPoint(100, 240), wxSize(200, 20), wxSB_HORIZONTAL)
91 self.scroll.SetScrollbar(start_month-1, 1, 12, 1, TRUE)
92 EVT_COMMAND_SCROLL(self, mID, self.Scroll)
93
94 # spin control for year selection
95
96 self.dtext = wxTextCtrl(self, -1, str(start_year), wxPoint(200, 20), wxSize(60, -1))
97 h = self.dtext.GetSize().height
98
99 mID = NewId()
100 self.spin = wxSpinButton(self, mID, wxPoint(270, 20), wxSize(h*2, h))
101 self.spin.SetRange(1980, 2010)
102 self.spin.SetValue(start_year)
103 EVT_SPIN(self, mID, self.OnSpin)
104
105 # button for calendar dialog test
106
107 wxStaticText(self, -1, "Test Calendar Dialog", wxPoint(350, 50), wxSize(150, -1))
108
109 mID = NewId()
110 bmp = images.getCalendarBitmap()
111 self.but = wxBitmapButton(self, mID, bmp, wxPoint(380, 80))#, wxSize(30, 30))
112 EVT_BUTTON(self, mID, self.TestDlg)
113
114 # button for calendar window test
115
116 wxStaticText(self, -1, "Test Calendar Window", wxPoint(350, 150), wxSize(150, -1))
117
118 mID = NewId()
119 self.but = wxBitmapButton(self, mID, bmp, wxPoint(380, 180))#, wxSize(30, 30))
120 EVT_BUTTON(self, mID, self.TestFrame)
121
122 wxStaticText(self, -1, "Test Calendar Print", wxPoint(350, 250), wxSize(150, -1))
123
124 mID = NewId()
125 self.but = wxBitmapButton(self, mID, bmp, wxPoint(380, 280))#, wxSize(30, 30))
126 EVT_BUTTON(self, mID, self.OnPreview)
127
128 # calendar dialog
129
130 def TestDlg(self, event): # test the date dialog
131 dlg = CalenDlg(self)
132 dlg.Centre()
133 if dlg.ShowModal() == wxID_OK:
134 result = dlg.result
135 day = result[1]
136 month = result[2]
137 year = result[3]
138 new_date = str(month) + '/'+ str(day) + '/'+ str(year)
139 self.log.WriteText('Date Selected: %s\n' % new_date)
140 else:
141 self.log.WriteText('No Date Selected')
142
143 # calendar window test
144
145 def TestFrame(self, event):
146 frame = CalendFrame(self, -1, "Test Calendar", self.log)
147 frame.Show(true)
148 return true
149
150 # calendar print preview
151
152 def OnPreview(self, event):
153 month = self.calend.GetMonth()
154 year = self.calend.GetYear()
155
156 prt = PrintCalend(self.frame, month, year)
157 prt.Preview()
158
159 # month and year control events
160
161 def OnSpin(self, event):
162 year = event.GetPosition()
163 self.dtext.SetValue(str(year))
164 self.calend.SetYear(year)
165 self.calend.Refresh()
166
167 def EvtComboBox(self, event):
168 name = event.GetString()
169 self.log.WriteText('EvtComboBox: %s\n' % name)
170 monthval = self.date.FindString(name)
171 self.scroll.SetScrollbar(monthval, 1, 12, 1, TRUE)
172
173 self.calend.SetMonth(monthval+1)
174 self.ResetDisplay()
175
176 def Scroll(self, event):
177 value = self.scroll.GetThumbPosition()
178 monthval = int(value)+1
179 self.calend.SetMonth(monthval)
180 self.ResetDisplay()
181 self.log.WriteText('Month: %s\n' % value)
182
183 name = Month[monthval]
184 self.date.SetValue(name)
185
186 # log mouse events
187
188 def MouseClick(self, evt):
189 text = '%s CLICK %02d/%02d/%d' % (evt.click, evt.day, evt.month, evt.year) # format date
190 self.log.WriteText('Date Selected: ' + text + '\n')
191
192
193 # set the highlighted days for the calendar
194
195 def ResetDisplay(self):
196 month = self.calend.GetMonth()
197 try:
198 set_days = test_days[month]
199 except:
200 set_days = [1, 5, 12]
201
202 self.calend.AddSelect([4, 11], 'BLUE', 'WHITE')
203 self.calend.SetSelDay(set_days)
204 self.calend.Refresh()
205
206 # increment and decrement toolbar controls
207
208 def OnIncYear(self, event):
209 self.calend.IncYear()
210 self.ResetDisplay()
211
212 def OnDecYear(self, event):
213 self.calend.DecYear()
214 self.ResetDisplay()
215
216 def OnIncMonth(self, event):
217 self.calend.IncMonth()
218 self.ResetDisplay()
219
220 def OnDecMonth(self, event):
221 self.calend.DecMonth()
222 self.ResetDisplay()
223
224 def OnCurrent(self, event):
225 self.calend.SetCurrentDay()
226 self.ResetDisplay()
227
228 # test of full window calendar control functions
229
230 class CalendFrame(wxFrame):
231 def __init__(self, parent, id, title, log):
232 wxFrame.__init__(self, parent, id, title, wxPyDefaultPosition, wxSize(400, 400))
233 EVT_CLOSE(self, self.OnCloseWindow)
234
235 self.log = log
236 self.CreateStatusBar()
237 self.mainmenu = wxMenuBar()
238 menu = wxMenu()
239
240 menu = self.MakeFileMenu()
241 self.mainmenu.Append(menu, '&File')
242
243 self.MakeToolMenu() # toolbar
244
245 self.SetMenuBar(self.mainmenu)
246 self.calend = wxCalendar(self, -1)
247 self.calend.SetCurrentDay()
248 self.calend.grid_color = 'BLUE'
249 self.calend.SetBusType()
250 # self.calend.ShowWeekEnd()
251
252 self.ResetDisplay()
253
254 self.Connect(self.calend.GetId(), -1, 2100, self.MouseClick)
255
256 def MouseClick(self, evt):
257 text = '%s CLICK %02d/%02d/%d' % (evt.click, evt.day, evt.month, evt.year) # format date
258 self.log.WriteText('Date Selected: ' + text + '\n')
259
260 def OnCloseWindow(self, event):
261 self.Destroy()
262
263 def ResetDisplay(self):
264 month = self.calend.GetMonth()
265 try:
266 set_days = test_days[month]
267 except:
268 set_days = [1, 5, 12]
269
270 self.calend.AddSelect([2, 16], 'GREEN', 'WHITE')
271
272 self.calend.SetSelDay(set_days)
273 self.calend.Refresh()
274
275 def OnIncYear(self, event):
276 self.calend.IncYear()
277 self.ResetDisplay()
278
279 def OnDecYear(self, event):
280 self.calend.DecYear()
281 self.ResetDisplay()
282
283 def OnIncMonth(self, event):
284 self.calend.IncMonth()
285 self.ResetDisplay()
286
287 def OnDecMonth(self, event):
288 self.calend.DecMonth()
289 self.ResetDisplay()
290
291 def OnCurrent(self, event):
292 self.calend.SetCurrentDay()
293 self.ResetDisplay()
294
295 def MakeFileMenu(self):
296 menu = wxMenu()
297
298 mID = NewId()
299 menu.Append(mID, 'Decrement', 'Next')
300 EVT_MENU(self, mID, self.OnDecMonth)
301
302 mID = NewId()
303 menu.Append(mID, 'Increment', 'Dec')
304 EVT_MENU(self, mID, self.OnIncMonth)
305
306 menu.AppendSeparator()
307
308 mID = NewId()
309 menu.Append(mID, 'E&xit', 'Exit')
310 EVT_MENU(self, mID, self.OnCloseWindow)
311
312 return menu
313
314 def MakeToolMenu(self):
315 tb = self.CreateToolBar(wxTB_HORIZONTAL|wxNO_BORDER)
316
317 mID = NewId()
318 SetToolPath(self, tb, mID, images.getDbDecBitmap(), 'Dec Year')
319 EVT_TOOL(self, mID, self.OnDecYear)
320
321 mID = NewId()
322 SetToolPath(self, tb, mID, images.getDecBitmap(), 'Dec Month')
323 EVT_TOOL(self, mID, self.OnDecMonth)
324
325 mID = NewId()
326 SetToolPath(self, tb, mID, images.getPtBitmap(), 'Current Month')
327 EVT_TOOL(self, mID, self.OnCurrent)
328
329 mID = NewId()
330 SetToolPath(self, tb, mID, images.getIncBitmap(), 'Inc Month')
331 EVT_TOOL(self, mID, self.OnIncMonth)
332
333 mID = NewId()
334 SetToolPath(self, tb, mID, images.getDbIncBitmap(), 'Inc Year')
335 EVT_TOOL(self, mID, self.OnIncYear)
336
337 tb.Realize()
338
339 #---------------------------------------------------------------------------
340
341 # example class for printing/previewing calendars
342
343 class PrintCalend:
344 def __init__(self, parent, month, year):
345 self.frame = parent
346 self.month = month
347 self.year = year
348
349 self.SetParms()
350 self.SetCal()
351 self.printData = wxPrintData()
352
353 def SetCal(self):
354 self.grid_color = 'BLUE'
355 self.back_color = 'WHITE'
356 self.sel_color = 'RED'
357 self.high_color = 'LIGHT BLUE'
358 self.font = wxSWISS
359 self.bold = wxNORMAL
360
361 self.sel_key = None # last used by
362 self.sel_lst = [] # highlighted selected days
363
364 self.size = None
365 self.hide_title = FALSE
366 self.hide_grid = FALSE
367 self.set_day = None
368
369 def SetParms(self):
370 self.ymax = 1
371 self.xmax = 1
372 self.page = 1
373 self.total_pg = 1
374
375 self.preview = None
376 self.scale = 1.0
377
378 self.pagew = 8.5
379 self.pageh = 11.0
380
381 self.txt_marg = 0.1
382 self.lf_marg = 0
383 self.top_marg = 0
384
385 self.page = 0
386
387 def SetDates(self, month, year):
388 self.month = month
389 self.year = year
390
391 def SetStyleDef(self, desc):
392 self.style = desc
393
394 def SetCopies(self, copies): # number of copies of label
395 self.copies = copies
396
397 def SetStart(self, start): # start position of label
398 self.start = start
399
400 def Preview(self):
401 printout = SetPrintout(self)
402 printout2 = SetPrintout(self)
403 self.preview = wxPrintPreview(printout, printout2, self.printData)
404 if not self.preview.Ok():
405 wxMessageBox("There was a problem printing!", "Printing", wxOK)
406 return
407
408 self.preview.SetZoom(60) # initial zoom value
409
410 frame = wxPreviewFrame(self.preview, self.frame, "Print preview")
411
412 frame.Initialize()
413 frame.SetPosition(self.frame.GetPosition())
414 frame.SetSize(self.frame.GetSize())
415 frame.Show(true)
416
417 def Print(self):
418 pdd = wxPrintDialogData()
419 pdd.SetPrintData(self.printData)
420 printer = wxPrinter(pdd)
421 printout = SetPrintout(self)
422 frame = wxFrame(None, -1, "Test")
423 if not printer.Print(frame, printout):
424 wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK)
425 else:
426 self.printData = printer.GetPrintDialogData().GetPrintData()
427 printout.Destroy()
428
429 def DoDrawing(self, DC):
430 size = DC.GetSizeTuple()
431 DC.BeginDrawing()
432
433 cal = PrtCalDraw(self)
434
435 if self.preview is None:
436 cal.SetPSize(size[0]/self.pagew, size[1]/self.pageh)
437 cal.SetPreview(FALSE)
438
439 else:
440 if self.preview == 1:
441 cal.SetPSize(size[0]/self.pagew, size[1]/self.pageh)
442 else:
443 cal.SetPSize(self.pwidth, self.pheight)
444
445 cal.SetPreview(self.preview)
446
447 cal.hide_title = self.hide_title # set the calendar parameters
448 cal.hide_grid = self.hide_grid
449
450 cal.grid_color = self.grid_color
451 cal.high_color = self.high_color
452 cal.back_color = self.back_color
453 cal.outer_border = FALSE
454 cal.font = self.font
455 cal.bold = self.bold
456
457 cal_size = wxSize(3.0, 3.0)
458 cal.SetSize(cal_size)
459
460 year, month = self.year, self.month
461
462 x = 1.0
463 for i in range(2):
464 y = 0.5
465 for j in range(3):
466 cal.SetCal(year, month) # current month
467 cal.SetPos(x, y)
468
469 try:
470 set_days = test_days[month]
471 except:
472 set_days = [1, 5, 12]
473
474 cal.AddSelect([2, 16], 'GREEN', 'WHITE')
475
476 cal.DrawCal(DC, set_days)
477
478 year, month = self.IncMonth(year, month)
479 y = y + 3.5
480 x = x + 4.0 # next colum
481
482 DC.EndDrawing()
483
484 self.ymax = DC.MaxY()
485 self.xmax = DC.MaxX()
486
487 def IncMonth(self, year, month): # next month
488 month = month + 1
489 if month > 12:
490 month = 1
491 year = year + 1
492
493 return year, month
494
495 def GetTotalPages(self):
496 self.pg_cnt = 1
497 return self.pg_cnt
498
499 def SetPage(self, page):
500 self.page = page
501
502 def SetPageSize(self, width, height):
503 self.pwidth, self.pheight = width, height
504
505 def SetTotalSize(self, width, height):
506 self.ptwidth, self.ptheight = width, height
507
508 def SetPreview(self, preview, scale):
509 self.preview = preview
510 self.scale = scale
511
512 def SetTotalSize(self, width, height):
513 self.ptwidth = width
514 self.ptheight = height
515
516 def SetToolPath(self, tb, id, bmp, title):
517 tb.AddSimpleTool(id, bmp, title, title)
518
519 class SetPrintout(wxPrintout):
520 def __init__(self, canvas):
521 wxPrintout.__init__(self)
522 self.canvas = canvas
523 self.end_pg = 1
524
525 def OnBeginDocument(self, start, end):
526 return self.base_OnBeginDocument(start, end)
527
528 def OnEndDocument(self):
529 self.base_OnEndDocument()
530
531 def HasPage(self, page):
532 if page <= self.end_pg:
533 return true
534 else:
535 return false
536
537 def GetPageInfo(self):
538 self.end_pg = self.canvas.GetTotalPages()
539 str_pg = 1
540 try:
541 end_pg = self.end_pg
542 except:
543 end_pg = 1
544 return (str_pg, end_pg, str_pg, end_pg)
545
546 def OnPreparePrinting(self):
547 self.base_OnPreparePrinting()
548
549 def OnBeginPrinting(self):
550 dc = self.GetDC()
551
552 self.preview = self.IsPreview()
553 if (self.preview):
554 self.pixelsPerInch = self.GetPPIScreen()
555 else:
556 self.pixelsPerInch = self.GetPPIPrinter()
557
558 (w, h) = dc.GetSizeTuple()
559 scaleX = float(w) / 1000
560 scaleY = float(h) / 1000
561 self.printUserScale = min(scaleX, scaleY)
562
563 self.base_OnBeginPrinting()
564
565 def GetSize(self):
566 self.psizew, self.psizeh = self.GetPPIPrinter()
567 return self.psizew, self.psizeh
568
569 def GetTotalSize(self):
570 self.ptsizew, self.ptsizeh = self.GetPageSizePixels()
571 return self.ptsizew, self.ptsizeh
572
573 def OnPrintPage(self, page):
574 dc = self.GetDC()
575 (w, h) = dc.GetSizeTuple()
576 scaleX = float(w) / 1000
577 scaleY = float(h) / 1000
578 self.printUserScale = min(scaleX, scaleY)
579 dc.SetUserScale(self.printUserScale, self.printUserScale)
580
581 self.preview = self.IsPreview()
582
583 self.canvas.SetPreview(self.preview, self.printUserScale)
584 self.canvas.SetPage(page)
585
586 self.ptsizew, self.ptsizeh = self.GetPageSizePixels()
587 self.canvas.SetTotalSize(self.ptsizew, self.ptsizeh)
588
589 self.psizew, self.psizeh = self.GetPPIPrinter()
590 self.canvas.SetPageSize(self.psizew, self.psizeh)
591
592 self.canvas.DoDrawing(dc)
593 return true
594
595 class MyApp(wxApp):
596 def OnInit(self):
597 frame = CalendFrame(None, -1, "Test Calendar")
598 frame.Show(true)
599 self.SetTopWindow(frame)
600 return true
601
602 #---------------------------------------------------------------------------
603
604 def MessageDlg(self, message, type = 'Message'):
605 dlg = wxMessageDialog(self, message, type, wxOK | wxICON_INFORMATION)
606 dlg.ShowModal()
607 dlg.Destroy()
608
609 #---------------------------------------------------------------------------
610
611 def main():
612 app = MyApp(0)
613 app.MainLoop()
614
615
616 if __name__ == '__main__':
617 main()
618
619
620 #---------------------------------------------------------------------------
621
622 def runTest(frame, nb, log):
623 win = TestPanel(nb, log, frame)
624 return win
625
626 #---------------------------------------------------------------------------
627
628
629 overview = """\
630 This control provides a calendar control class for displaying and selecting dates. In addition, the class is extended and can now be used for printing/previewing.
631
632 Additional features include weekend highlighting and business type Monday-Sunday format.
633
634 See example for various methods used to set display month, year, and highlighted dates (different font and background colours).
635
636 by Lorne White
637
638 """