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