1 #----------------------------------------------------------------------------
3 # Purpose: Calendar control display testing on panel for wxPython demo
5 # Author: Lorne White (email: lwhite1@planet.eon.net)
9 # Licence: wxWindows license
10 #----------------------------------------------------------------------------
11 # 11/15/2003 - Jeff Grimmett (grimmtooth@softhome.net)
13 # o Updated for wx namespace
14 # o Some updating of the library itself will be needed for this demo to work
17 # 11/26/2003 - Jeff Grimmett (grimmtooth@softhome.net)
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.
26 # 11/30/2003 - Jeff Grimmett (grimmtooth@softhome.net)
28 # o Lockup issue clarification: it appears that the spinner is
31 # 12/01/2003 - Jeff Grimmett (grimmtooth@softhome.net)
33 # o New Bind() method now fully supported.
35 # 12/17/2003 - Jeff Grimmett (grimmtooth@softhome.net)
37 # o wxCalendar renamed to Calendar
38 # o Got rid of unneeded IDs where Bind() could figure it
45 import wx
.lib
.calendar
50 # highlighted days in month
63 11: [6, 9, 12, 28, 29],
64 12: [8, 9, 10, 11, 20] }
66 # test of full window calendar control functions
73 name
= wx
.lib
.calendar
.Month
[i
]
76 monthlist
.append(name
)
80 class TestPanel(wx
.Panel
):
81 def __init__(self
, parent
, log
, frame
):
82 wx
.Panel
.__init
__(self
, parent
, -1)
87 self
.calend
= wx
.lib
.calendar
.Calendar(self
, -1, (100, 50), (200, 180))
89 # start_month = 2 # preselect the date for calendar
92 start_month
= self
.calend
.GetMonth() # get the current month & year
93 start_year
= self
.calend
.GetYear()
95 # month list from DateTime module
97 monthlist
= GetMonthList()
99 self
.date
= wx
.ComboBox(self
, -1, "",
101 monthlist
, wx
.CB_DROPDOWN
)
103 self
.date
.SetSelection(start_month
-1)
104 self
.Bind(wx
.EVT_COMBOBOX
, self
.EvtComboBox
, self
.date
)
106 # set start month and year
108 self
.calend
.SetMonth(start_month
)
109 self
.calend
.SetYear(start_year
)
111 # set attributes of calendar
113 self
.calend
.hide_title
= True
114 self
.calend
.HideGrid()
115 self
.calend
.SetWeekColor('WHITE', 'BLACK')
122 self
.Bind(wx
.lib
.calendar
.EVT_CALENDAR
, self
.MouseClick
, self
.calend
)
124 # scroll bar for month selection
125 self
.scroll
= wx
.ScrollBar(self
, -1, (100, 240), (200, 20), wx
.SB_HORIZONTAL
)
126 self
.scroll
.SetScrollbar(start_month
-1, 1, 12, 1, True)
127 self
.Bind(wx
.EVT_COMMAND_SCROLL
, self
.Scroll
, self
.scroll
)
129 # spin control for year selection
131 self
.dtext
= wx
.TextCtrl(self
, -1, str(start_year
), (200, 20), (60, -1))
132 h
= self
.dtext
.GetSize().height
134 self
.spin
= wx
.SpinButton(self
, -1, (270, 20), (h
*2, h
))
135 self
.spin
.SetRange(1980, 2010)
136 self
.spin
.SetValue(start_year
)
137 self
.Bind(wx
.EVT_SPIN
, self
.OnSpin
, self
.spin
)
139 # button for calendar dialog test
141 wx
.StaticText(self
, -1, "Test Calendar Dialog", (350, 50), (150, -1))
143 bmp
= images
.getCalendarBitmap()
144 self
.but1
= wx
.BitmapButton(self
, -1, bmp
, (380, 80))
145 self
.Bind(wx
.EVT_BUTTON
, self
.TestDlg
, self
.but1
)
147 # button for calendar window test
149 wx
.StaticText(self
, -1, "Test Calendar Window", (350, 150), (150, -1))
151 self
.but2
= wx
.BitmapButton(self
, -1, bmp
, (380, 180))
152 self
.Bind(wx
.EVT_BUTTON
, self
.TestFrame
, self
.but2
)
154 wx
.StaticText(self
, -1, "Test Calendar Print", (350, 250), (150, -1))
156 self
.but3
= wx
.BitmapButton(self
, -1, bmp
, (380, 280))
157 self
.Bind(wx
.EVT_BUTTON
, self
.OnPreview
, self
.but3
)
161 def TestDlg(self
, event
): # test the date dialog
162 dlg
= wx
.lib
.calendar
.CalenDlg(self
)
165 if dlg
.ShowModal() == wx
.ID_OK
:
170 new_date
= str(month
) + '/'+ str(day
) + '/'+ str(year
)
171 self
.log
.WriteText('Date Selected: %s\n' % new_date
)
173 self
.log
.WriteText('No Date Selected')
175 # calendar window test
177 def TestFrame(self
, event
):
178 frame
= CalendFrame(self
, -1, "Test Calendar", self
.log
)
182 # calendar print preview
184 def OnPreview(self
, event
):
185 month
= self
.calend
.GetMonth()
186 year
= self
.calend
.GetYear()
188 prt
= PrintCalend(self
.frame
, month
, year
)
191 # month and year control events
193 def OnSpin(self
, event
):
194 year
= event
.GetPosition()
195 self
.dtext
.SetValue(str(year
))
196 self
.calend
.SetYear(year
)
197 self
.calend
.Refresh()
199 def EvtComboBox(self
, event
):
200 name
= event
.GetString()
201 self
.log
.WriteText('EvtComboBox: %s\n' % name
)
202 monthval
= self
.date
.FindString(name
)
203 self
.scroll
.SetScrollbar(monthval
, 1, 12, 1, True)
205 self
.calend
.SetMonth(monthval
+1)
208 def Scroll(self
, event
):
209 value
= self
.scroll
.GetThumbPosition()
210 monthval
= int(value
)+1
211 self
.calend
.SetMonth(monthval
)
213 self
.log
.WriteText('Month: %s\n' % value
)
215 name
= wx
.lib
.calendar
.Month
[monthval
]
216 self
.date
.SetValue(name
)
220 def MouseClick(self
, evt
):
221 text
= '%s CLICK %02d/%02d/%d' % (evt
.click
, evt
.day
, evt
.month
, evt
.year
) # format date
222 self
.log
.WriteText('Date Selected: ' + text
+ '\n')
225 # set the highlighted days for the calendar
227 def ResetDisplay(self
):
228 month
= self
.calend
.GetMonth()
231 set_days
= test_days
[month
]
233 set_days
= [1, 5, 12]
235 self
.calend
.AddSelect([4, 11], 'BLUE', 'WHITE')
236 self
.calend
.SetSelDay(set_days
)
237 self
.calend
.Refresh()
239 # increment and decrement toolbar controls
241 def OnIncYear(self
, event
):
242 self
.calend
.IncYear()
245 def OnDecYear(self
, event
):
246 self
.calend
.DecYear()
249 def OnIncMonth(self
, event
):
250 self
.calend
.IncMonth()
253 def OnDecMonth(self
, event
):
254 self
.calend
.DecMonth()
257 def OnCurrent(self
, event
):
258 self
.calend
.SetCurrentDay()
261 # test of full window calendar control functions
263 class CalendFrame(wx
.Frame
):
264 def __init__(self
, parent
, id, title
, log
):
265 wx
.Frame
.__init
__(self
, parent
, id, title
, size
=(400, 400),
266 style
=wx
.DEFAULT_FRAME_STYLE|wx
.NO_FULL_REPAINT_ON_RESIZE
)
268 self
.Bind(wx
.EVT_CLOSE
, self
.OnCloseWindow
)
271 self
.CreateStatusBar()
272 self
.mainmenu
= wx
.MenuBar()
275 menu
= self
.MakeFileMenu()
276 self
.mainmenu
.Append(menu
, '&File')
278 self
.MakeToolMenu() # toolbar
280 self
.SetMenuBar(self
.mainmenu
)
281 self
.calend
= wx
.lib
.calendar
.Calendar(self
, -1)
282 self
.calend
.SetCurrentDay()
283 self
.calend
.grid_color
= 'BLUE'
284 self
.calend
.SetBusType()
285 # self.calend.ShowWeekEnd()
289 self
.Bind(wx
.lib
.calendar
.EVT_CALENDAR
, self
.MouseClick
, self
.calend
)
291 def MouseClick(self
, evt
):
292 text
= '%s CLICK %02d/%02d/%d' % (evt
.click
, evt
.day
, evt
.month
, evt
.year
) # format date
293 self
.log
.WriteText('Date Selected: ' + text
+ '\n')
295 def OnCloseWindow(self
, event
):
298 def ResetDisplay(self
):
299 month
= self
.calend
.GetMonth()
302 set_days
= test_days
[month
]
304 set_days
= [1, 5, 12]
306 self
.calend
.AddSelect([2, 16], 'GREEN', 'WHITE')
308 self
.calend
.SetSelDay(set_days
)
309 self
.calend
.Refresh()
311 def OnIncYear(self
, event
):
312 self
.calend
.IncYear()
315 def OnDecYear(self
, event
):
316 self
.calend
.DecYear()
319 def OnIncMonth(self
, event
):
320 self
.calend
.IncMonth()
323 def OnDecMonth(self
, event
):
324 self
.calend
.DecMonth()
327 def OnCurrent(self
, event
):
328 self
.calend
.SetCurrentDay()
331 def MakeFileMenu(self
):
335 menu
.Append(mID
, 'Decrement', 'Next')
336 self
.Bind(wx
.EVT_MENU
, self
.OnDecMonth
, id=mID
)
339 menu
.Append(mID
, 'Increment', 'Dec')
340 self
.Bind(wx
.EVT_MENU
, self
.OnIncMonth
, id=mID
)
342 menu
.AppendSeparator()
345 menu
.Append(mID
, 'E&xit', 'Exit')
346 self
.Bind(wx
.EVT_MENU
, self
.OnCloseWindow
, id=mID
)
350 def MakeToolMenu(self
):
351 tb
= self
.CreateToolBar(wx
.TB_HORIZONTAL|wx
.NO_BORDER
)
354 SetToolPath(self
, tb
, mID
, images
.getDbDecBitmap(), 'Dec Year')
355 self
.Bind(wx
.EVT_TOOL
, self
.OnDecYear
, id=mID
)
358 SetToolPath(self
, tb
, mID
, images
.getDecBitmap(), 'Dec Month')
359 self
.Bind(wx
.EVT_TOOL
, self
.OnDecMonth
, id=mID
)
362 SetToolPath(self
, tb
, mID
, images
.getPtBitmap(), 'Current Month')
363 self
.Bind(wx
.EVT_TOOL
, self
.OnCurrent
, id=mID
)
366 SetToolPath(self
, tb
, mID
, images
.getIncBitmap(), 'Inc Month')
367 self
.Bind(wx
.EVT_TOOL
, self
.OnIncMonth
, id=mID
)
370 SetToolPath(self
, tb
, mID
, images
.getDbIncBitmap(), 'Inc Year')
371 self
.Bind(wx
.EVT_TOOL
, self
.OnIncYear
, id=mID
)
375 #---------------------------------------------------------------------------
377 # example class for printing/previewing calendars
380 def __init__(self
, parent
, month
, year
):
387 self
.printData
= wx
.PrintData()
390 self
.grid_color
= 'BLUE'
391 self
.back_color
= 'WHITE'
392 self
.sel_color
= 'RED'
393 self
.high_color
= 'LIGHT BLUE'
395 self
.bold
= wx
.NORMAL
397 self
.sel_key
= None # last used by
398 self
.sel_lst
= [] # highlighted selected days
401 self
.hide_title
= False
402 self
.hide_grid
= False
423 def SetDates(self
, month
, year
):
427 def SetStyleDef(self
, desc
):
430 def SetCopies(self
, copies
): # number of copies of label
433 def SetStart(self
, start
): # start position of label
437 printout
= SetPrintout(self
)
438 printout2
= SetPrintout(self
)
439 self
.preview
= wx
.PrintPreview(printout
, printout2
, self
.printData
)
441 if not self
.preview
.Ok():
442 wx
.MessageBox("There was a problem printing!", "Printing", wx
.OK
)
445 self
.preview
.SetZoom(60) # initial zoom value
447 frame
= wx
.PreviewFrame(self
.preview
, self
.frame
, "Print preview")
450 frame
.SetPosition(self
.frame
.GetPosition())
451 frame
.SetSize(self
.frame
.GetSize())
455 pdd
= wx
.PrintDialogData()
456 pdd
.SetPrintData(self
.printData
)
457 printer
= wx
.Printer(pdd
)
458 printout
= SetPrintout(self
)
459 frame
= wx
.Frame(None, -1, "Test")
461 if not printer
.Print(frame
, printout
):
462 wx
.MessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wx
.OK
)
464 self
.printData
= printer
.GetPrintDialogData().GetPrintData()
468 def DoDrawing(self
, DC
):
472 cal
= wx
.lib
.calendar
.PrtCalDraw(self
)
474 if self
.preview
is None:
475 cal
.SetPSize(size
[0]/self
.pagew
, size
[1]/self
.pageh
)
476 cal
.SetPreview(False)
479 if self
.preview
== 1:
480 cal
.SetPSize(size
[0]/self
.pagew
, size
[1]/self
.pageh
)
482 cal
.SetPSize(self
.pwidth
, self
.pheight
)
484 cal
.SetPreview(self
.preview
)
486 cal
.hide_title
= self
.hide_title
# set the calendar parameters
487 cal
.hide_grid
= self
.hide_grid
489 cal
.grid_color
= self
.grid_color
490 cal
.high_color
= self
.high_color
491 cal
.back_color
= self
.back_color
492 cal
.outer_border
= False
496 cal_size
= (3.0, 3.0)
497 cal
.SetSize(cal_size
)
499 year
, month
= self
.year
, self
.month
507 cal
.SetCal(year
, month
) # current month
511 set_days
= test_days
[month
]
513 set_days
= [1, 5, 12]
515 cal
.AddSelect([2, 16], 'GREEN', 'WHITE')
517 cal
.DrawCal(DC
, set_days
)
519 year
, month
= self
.IncMonth(year
, month
)
522 x
= x
+ 4.0 # next column
526 self
.ymax
= DC
.MaxY()
527 self
.xmax
= DC
.MaxX()
529 def IncMonth(self
, year
, month
): # next month
538 def GetTotalPages(self
):
542 def SetPage(self
, page
):
545 def SetPageSize(self
, width
, height
):
546 self
.pwidth
, self
.pheight
= width
, height
548 def SetTotalSize(self
, width
, height
):
549 self
.ptwidth
, self
.ptheight
= width
, height
551 def SetPreview(self
, preview
, scale
):
552 self
.preview
= preview
555 def SetTotalSize(self
, width
, height
):
557 self
.ptheight
= height
559 def SetToolPath(self
, tb
, id, bmp
, title
):
560 tb
.AddSimpleTool(id, bmp
, title
, title
)
562 class SetPrintout(wx
.Printout
):
563 def __init__(self
, canvas
):
564 wx
.Printout
.__init
__(self
)
568 def OnBeginDocument(self
, start
, end
):
569 return self
.base_OnBeginDocument(start
, end
)
571 def OnEndDocument(self
):
572 self
.base_OnEndDocument()
574 def HasPage(self
, page
):
575 if page
<= self
.end_pg
:
580 def GetPageInfo(self
):
581 self
.end_pg
= self
.canvas
.GetTotalPages()
589 return (str_pg
, end_pg
, str_pg
, end_pg
)
591 def OnPreparePrinting(self
):
592 self
.base_OnPreparePrinting()
594 def OnBeginPrinting(self
):
597 self
.preview
= self
.IsPreview()
600 self
.pixelsPerInch
= self
.GetPPIScreen()
602 self
.pixelsPerInch
= self
.GetPPIPrinter()
604 (w
, h
) = dc
.GetSize()
605 scaleX
= float(w
) / 1000
606 scaleY
= float(h
) / 1000
607 self
.printUserScale
= min(scaleX
, scaleY
)
609 self
.base_OnBeginPrinting()
612 self
.psizew
, self
.psizeh
= self
.GetPPIPrinter()
613 return self
.psizew
, self
.psizeh
615 def GetTotalSize(self
):
616 self
.ptsizew
, self
.ptsizeh
= self
.GetPageSizePixels()
617 return self
.ptsizew
, self
.ptsizeh
619 def OnPrintPage(self
, page
):
621 (w
, h
) = dc
.GetSize()
622 scaleX
= float(w
) / 1000
623 scaleY
= float(h
) / 1000
624 self
.printUserScale
= min(scaleX
, scaleY
)
625 dc
.SetUserScale(self
.printUserScale
, self
.printUserScale
)
627 self
.preview
= self
.IsPreview()
629 self
.canvas
.SetPreview(self
.preview
, self
.printUserScale
)
630 self
.canvas
.SetPage(page
)
632 self
.ptsizew
, self
.ptsizeh
= self
.GetPageSizePixels()
633 self
.canvas
.SetTotalSize(self
.ptsizew
, self
.ptsizeh
)
635 self
.psizew
, self
.psizeh
= self
.GetPPIPrinter()
636 self
.canvas
.SetPageSize(self
.psizew
, self
.psizeh
)
638 self
.canvas
.DoDrawing(dc
)
643 frame
= CalendFrame(None, -1, "Test Calendar", log
)
645 self
.SetTopWindow(frame
)
648 #---------------------------------------------------------------------------
650 def MessageDlg(self
, message
, type = 'Message'):
651 dlg
= wx
.MessageDialog(self
, message
, type, wx
.OK | wx
.ICON_INFORMATION
)
655 #---------------------------------------------------------------------------
657 def runTest(frame
, nb
, log
):
658 win
= TestPanel(nb
, log
, frame
)
661 #---------------------------------------------------------------------------
665 This control provides a Calendar control class for displaying and selecting dates.
666 In addition, the class is extended and can be used for printing/previewing.
668 Additional features include weekend highlighting and business type Monday-Sunday
671 See example for various methods used to set display month, year, and highlighted
672 dates (different font and background colours).
681 if __name__
== '__main__':
684 run
.main(['', os
.path
.basename(sys
.argv
[0])])