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.
39 import wx
.lib
.calendar
as calendar
44 # highlighted days in month
57 11: [6, 9, 12, 28, 29],
58 12: [8, 9, 10, 11, 20] }
60 # test of full window calendar control functions
67 name
= calendar
.Month
[i
]
70 monthlist
.append(name
)
74 class TestPanel(wx
.Panel
):
75 def __init__(self
, parent
, log
, frame
):
76 wx
.Panel
.__init
__(self
, parent
, -1)
81 self
.calend
= calendar
.wxCalendar(self
, -1, (100, 50), (200, 180))
83 # start_month = 2 # preselect the date for calendar
86 start_month
= self
.calend
.GetMonth() # get the current month & year
87 start_year
= self
.calend
.GetYear()
89 # month list from DateTime module
91 monthlist
= GetMonthList()
94 self
.date
= wx
.ComboBox(self
, mID
, "",
96 monthlist
, wx
.CB_DROPDOWN
)
98 self
.date
.SetSelection(start_month
-1)
99 self
.Bind(wx
.EVT_COMBOBOX
, self
.EvtComboBox
, id=mID
)
101 # set start month and year
103 self
.calend
.SetMonth(start_month
)
104 self
.calend
.SetYear(start_year
)
106 # set attributes of calendar
108 self
.calend
.hide_title
= True
109 self
.calend
.HideGrid()
110 self
.calend
.SetWeekColor('WHITE', 'BLACK')
117 self
.Bind(calendar
.EVT_CALENDAR
, self
.MouseClick
, self
.calend
)
119 # scroll bar for month selection
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
)
127 # spin control for year selection
129 self
.dtext
= wx
.TextCtrl(self
, -1, str(start_year
), (200, 20), (60, -1))
130 h
= self
.dtext
.GetSize().height
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
)
138 # button for calendar dialog test
140 wx
.StaticText(self
, -1, "Test Calendar Dialog", (350, 50), (150, -1))
143 bmp
= images
.getCalendarBitmap()
144 self
.but
= wx
.BitmapButton(self
, mID
, bmp
, (380, 80))
145 self
.Bind(wx
.EVT_BUTTON
, self
.TestDlg
, id=mID
)
147 # button for calendar window test
149 wx
.StaticText(self
, -1, "Test Calendar Window", (350, 150), (150, -1))
152 self
.but
= wx
.BitmapButton(self
, mID
, bmp
, (380, 180))
153 self
.Bind(wx
.EVT_BUTTON
, self
.TestFrame
, id=mID
)
155 wx
.StaticText(self
, -1, "Test Calendar Print", (350, 250), (150, -1))
158 self
.but
= wx
.BitmapButton(self
, mID
, bmp
, (380, 280))
159 self
.Bind(wx
.EVT_BUTTON
, self
.OnPreview
, id=mID
)
163 def TestDlg(self
, event
): # test the date dialog
164 dlg
= calendar
.CalenDlg(self
)
167 if dlg
.ShowModal() == wx
.ID_OK
:
172 new_date
= str(month
) + '/'+ str(day
) + '/'+ str(year
)
173 self
.log
.WriteText('Date Selected: %s\n' % new_date
)
175 self
.log
.WriteText('No Date Selected')
177 # calendar window test
179 def TestFrame(self
, event
):
180 frame
= CalendFrame(self
, -1, "Test Calendar", self
.log
)
184 # calendar print preview
186 def OnPreview(self
, event
):
187 month
= self
.calend
.GetMonth()
188 year
= self
.calend
.GetYear()
190 prt
= PrintCalend(self
.frame
, month
, year
)
193 # month and year control events
195 def OnSpin(self
, event
):
196 year
= event
.GetPosition()
197 self
.dtext
.SetValue(str(year
))
198 self
.calend
.SetYear(year
)
199 self
.calend
.Refresh()
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)
207 self
.calend
.SetMonth(monthval
+1)
210 def Scroll(self
, event
):
211 value
= self
.scroll
.GetThumbPosition()
212 monthval
= int(value
)+1
213 self
.calend
.SetMonth(monthval
)
215 self
.log
.WriteText('Month: %s\n' % value
)
217 name
= calendar
.Month
[monthval
]
218 self
.date
.SetValue(name
)
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')
227 # set the highlighted days for the calendar
229 def ResetDisplay(self
):
230 month
= self
.calend
.GetMonth()
233 set_days
= test_days
[month
]
235 set_days
= [1, 5, 12]
237 self
.calend
.AddSelect([4, 11], 'BLUE', 'WHITE')
238 self
.calend
.SetSelDay(set_days
)
239 self
.calend
.Refresh()
241 # increment and decrement toolbar controls
243 def OnIncYear(self
, event
):
244 self
.calend
.IncYear()
247 def OnDecYear(self
, event
):
248 self
.calend
.DecYear()
251 def OnIncMonth(self
, event
):
252 self
.calend
.IncMonth()
255 def OnDecMonth(self
, event
):
256 self
.calend
.DecMonth()
259 def OnCurrent(self
, event
):
260 self
.calend
.SetCurrentDay()
263 # test of full window calendar control functions
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
)
270 self
.Bind(wx
.EVT_CLOSE
, self
.OnCloseWindow
)
273 self
.CreateStatusBar()
274 self
.mainmenu
= wx
.MenuBar()
277 menu
= self
.MakeFileMenu()
278 self
.mainmenu
.Append(menu
, '&File')
280 self
.MakeToolMenu() # toolbar
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()
291 self
.Bind(calendar
.EVT_CALENDAR
, self
.MouseClick
, self
.calend
)
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')
297 def OnCloseWindow(self
, event
):
300 def ResetDisplay(self
):
301 month
= self
.calend
.GetMonth()
304 set_days
= test_days
[month
]
306 set_days
= [1, 5, 12]
308 self
.calend
.AddSelect([2, 16], 'GREEN', 'WHITE')
310 self
.calend
.SetSelDay(set_days
)
311 self
.calend
.Refresh()
313 def OnIncYear(self
, event
):
314 self
.calend
.IncYear()
317 def OnDecYear(self
, event
):
318 self
.calend
.DecYear()
321 def OnIncMonth(self
, event
):
322 self
.calend
.IncMonth()
325 def OnDecMonth(self
, event
):
326 self
.calend
.DecMonth()
329 def OnCurrent(self
, event
):
330 self
.calend
.SetCurrentDay()
333 def MakeFileMenu(self
):
337 menu
.Append(mID
, 'Decrement', 'Next')
338 self
.Bind(wx
.EVT_MENU
, self
.OnDecMonth
, id=mID
)
341 menu
.Append(mID
, 'Increment', 'Dec')
342 self
.Bind(wx
.EVT_MENU
, self
.OnIncMonth
, id=mID
)
344 menu
.AppendSeparator()
347 menu
.Append(mID
, 'E&xit', 'Exit')
348 self
.Bind(wx
.EVT_MENU
, self
.OnCloseWindow
, id=mID
)
352 def MakeToolMenu(self
):
353 tb
= self
.CreateToolBar(wx
.TB_HORIZONTAL|wx
.NO_BORDER
)
356 SetToolPath(self
, tb
, mID
, images
.getDbDecBitmap(), 'Dec Year')
357 self
.Bind(wx
.EVT_TOOL
, self
.OnDecYear
, id=mID
)
360 SetToolPath(self
, tb
, mID
, images
.getDecBitmap(), 'Dec Month')
361 self
.Bind(wx
.EVT_TOOL
, self
.OnDecMonth
, id=mID
)
364 SetToolPath(self
, tb
, mID
, images
.getPtBitmap(), 'Current Month')
365 self
.Bind(wx
.EVT_TOOL
, self
.OnCurrent
, id=mID
)
368 SetToolPath(self
, tb
, mID
, images
.getIncBitmap(), 'Inc Month')
369 self
.Bind(wx
.EVT_TOOL
, self
.OnIncMonth
, id=mID
)
372 SetToolPath(self
, tb
, mID
, images
.getDbIncBitmap(), 'Inc Year')
373 self
.Bind(wx
.EVT_TOOL
, self
.OnIncYear
, id=mID
)
377 #---------------------------------------------------------------------------
379 # example class for printing/previewing calendars
382 def __init__(self
, parent
, month
, year
):
389 self
.printData
= wx
.PrintData()
392 self
.grid_color
= 'BLUE'
393 self
.back_color
= 'WHITE'
394 self
.sel_color
= 'RED'
395 self
.high_color
= 'LIGHT BLUE'
397 self
.bold
= wx
.NORMAL
399 self
.sel_key
= None # last used by
400 self
.sel_lst
= [] # highlighted selected days
403 self
.hide_title
= False
404 self
.hide_grid
= False
425 def SetDates(self
, month
, year
):
429 def SetStyleDef(self
, desc
):
432 def SetCopies(self
, copies
): # number of copies of label
435 def SetStart(self
, start
): # start position of label
439 printout
= SetPrintout(self
)
440 printout2
= SetPrintout(self
)
441 self
.preview
= wx
.PrintPreview(printout
, printout2
, self
.printData
)
443 if not self
.preview
.Ok():
444 wx
.MessageBox("There was a problem printing!", "Printing", wx
.OK
)
447 self
.preview
.SetZoom(60) # initial zoom value
449 frame
= wx
.PreviewFrame(self
.preview
, self
.frame
, "Print preview")
452 frame
.SetPosition(self
.frame
.GetPosition())
453 frame
.SetSize(self
.frame
.GetSize())
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")
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
)
466 self
.printData
= printer
.GetPrintDialogData().GetPrintData()
470 def DoDrawing(self
, DC
):
474 cal
= calendar
.PrtCalDraw(self
)
476 if self
.preview
is None:
477 cal
.SetPSize(size
[0]/self
.pagew
, size
[1]/self
.pageh
)
478 cal
.SetPreview(False)
481 if self
.preview
== 1:
482 cal
.SetPSize(size
[0]/self
.pagew
, size
[1]/self
.pageh
)
484 cal
.SetPSize(self
.pwidth
, self
.pheight
)
486 cal
.SetPreview(self
.preview
)
488 cal
.hide_title
= self
.hide_title
# set the calendar parameters
489 cal
.hide_grid
= self
.hide_grid
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
498 cal_size
= (3.0, 3.0)
499 cal
.SetSize(cal_size
)
501 year
, month
= self
.year
, self
.month
509 cal
.SetCal(year
, month
) # current month
513 set_days
= test_days
[month
]
515 set_days
= [1, 5, 12]
517 cal
.AddSelect([2, 16], 'GREEN', 'WHITE')
519 cal
.DrawCal(DC
, set_days
)
521 year
, month
= self
.IncMonth(year
, month
)
524 x
= x
+ 4.0 # next column
528 self
.ymax
= DC
.MaxY()
529 self
.xmax
= DC
.MaxX()
531 def IncMonth(self
, year
, month
): # next month
540 def GetTotalPages(self
):
544 def SetPage(self
, page
):
547 def SetPageSize(self
, width
, height
):
548 self
.pwidth
, self
.pheight
= width
, height
550 def SetTotalSize(self
, width
, height
):
551 self
.ptwidth
, self
.ptheight
= width
, height
553 def SetPreview(self
, preview
, scale
):
554 self
.preview
= preview
557 def SetTotalSize(self
, width
, height
):
559 self
.ptheight
= height
561 def SetToolPath(self
, tb
, id, bmp
, title
):
562 tb
.AddSimpleTool(id, bmp
, title
, title
)
564 class SetPrintout(wx
.Printout
):
565 def __init__(self
, canvas
):
566 wx
.Printout
.__init
__(self
)
570 def OnBeginDocument(self
, start
, end
):
571 return self
.base_OnBeginDocument(start
, end
)
573 def OnEndDocument(self
):
574 self
.base_OnEndDocument()
576 def HasPage(self
, page
):
577 if page
<= self
.end_pg
:
582 def GetPageInfo(self
):
583 self
.end_pg
= self
.canvas
.GetTotalPages()
591 return (str_pg
, end_pg
, str_pg
, end_pg
)
593 def OnPreparePrinting(self
):
594 self
.base_OnPreparePrinting()
596 def OnBeginPrinting(self
):
599 self
.preview
= self
.IsPreview()
602 self
.pixelsPerInch
= self
.GetPPIScreen()
604 self
.pixelsPerInch
= self
.GetPPIPrinter()
606 (w
, h
) = dc
.GetSize()
607 scaleX
= float(w
) / 1000
608 scaleY
= float(h
) / 1000
609 self
.printUserScale
= min(scaleX
, scaleY
)
611 self
.base_OnBeginPrinting()
614 self
.psizew
, self
.psizeh
= self
.GetPPIPrinter()
615 return self
.psizew
, self
.psizeh
617 def GetTotalSize(self
):
618 self
.ptsizew
, self
.ptsizeh
= self
.GetPageSizePixels()
619 return self
.ptsizew
, self
.ptsizeh
621 def OnPrintPage(self
, page
):
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
)
629 self
.preview
= self
.IsPreview()
631 self
.canvas
.SetPreview(self
.preview
, self
.printUserScale
)
632 self
.canvas
.SetPage(page
)
634 self
.ptsizew
, self
.ptsizeh
= self
.GetPageSizePixels()
635 self
.canvas
.SetTotalSize(self
.ptsizew
, self
.ptsizeh
)
637 self
.psizew
, self
.psizeh
= self
.GetPPIPrinter()
638 self
.canvas
.SetPageSize(self
.psizew
, self
.psizeh
)
640 self
.canvas
.DoDrawing(dc
)
645 frame
= CalendFrame(None, -1, "Test Calendar", log
)
647 self
.SetTopWindow(frame
)
650 #---------------------------------------------------------------------------
652 def MessageDlg(self
, message
, type = 'Message'):
653 dlg
= wx
.MessageDialog(self
, message
, type, wx
.OK | wx
.ICON_INFORMATION
)
657 #---------------------------------------------------------------------------
659 def runTest(frame
, nb
, log
):
660 win
= TestPanel(nb
, log
, frame
)
663 #---------------------------------------------------------------------------
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.
670 Additional features include weekend highlighting and business type Monday-Sunday
673 See example for various methods used to set display month, year, and highlighted
674 dates (different font and background colours).
683 if __name__
== '__main__':
686 run
.main(['', os
.path
.basename(sys
.argv
[0])])