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