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
15 # 11/26/2003 - Jeff Grimmett (grimmtooth@softhome.net)
17 # o Ugh. AFter updating to the Bind() method, things lock up
18 # on various control clicks. Will have to debug. Only seems
19 # to happen on windows with calendar controls, though.
21 # 11/30/2003 - Jeff Grimmett (grimmtooth@softhome.net)
23 # o Lockup issue clarification: it appears that the spinner is
30 import wx
.lib
.calendar
35 # highlighted days in month
48 11: [6, 9, 12, 28, 29],
49 12: [8, 9, 10, 11, 20] }
51 # test of full window calendar control functions
58 name
= wx
.lib
.calendar
.Month
[i
]
61 monthlist
.append(name
)
65 class TestPanel(wx
.Panel
):
66 def __init__(self
, parent
, log
, frame
):
67 wx
.Panel
.__init
__(self
, parent
, -1)
72 self
.calend
= wx
.lib
.calendar
.Calendar(self
, -1, (100, 50), (200, 180))
74 # start_month = 2 # preselect the date for calendar
77 start_month
= self
.calend
.GetMonth() # get the current month & year
78 start_year
= self
.calend
.GetYear()
80 # month list from DateTime module
82 monthlist
= GetMonthList()
84 self
.date
= wx
.ComboBox(self
, -1, "",
86 monthlist
, wx
.CB_DROPDOWN
)
88 self
.date
.SetSelection(start_month
-1)
89 self
.Bind(wx
.EVT_COMBOBOX
, self
.EvtComboBox
, self
.date
)
91 # set start month and year
93 self
.calend
.SetMonth(start_month
)
94 self
.calend
.SetYear(start_year
)
96 # set attributes of calendar
98 self
.calend
.hide_title
= True
99 self
.calend
.HideGrid()
100 self
.calend
.SetWeekColor('WHITE', 'BLACK')
107 self
.Bind(wx
.lib
.calendar
.EVT_CALENDAR
, self
.MouseClick
, self
.calend
)
109 # scroll bar for month selection
110 self
.scroll
= wx
.ScrollBar(self
, -1, (100, 240), (200, 20), wx
.SB_HORIZONTAL
)
111 self
.scroll
.SetScrollbar(start_month
-1, 1, 12, 1, True)
112 self
.Bind(wx
.EVT_COMMAND_SCROLL
, self
.Scroll
, self
.scroll
)
114 # spin control for year selection
116 self
.dtext
= wx
.TextCtrl(self
, -1, str(start_year
), (200, 20), (60, -1))
117 h
= self
.dtext
.GetSize().height
119 self
.spin
= wx
.SpinButton(self
, -1, (270, 20), (h
*2, h
))
120 self
.spin
.SetRange(1980, 2010)
121 self
.spin
.SetValue(start_year
)
122 self
.Bind(wx
.EVT_SPIN
, self
.OnSpin
, self
.spin
)
124 # button for calendar dialog test
126 wx
.StaticText(self
, -1, "Test Calendar Dialog", (350, 50), (150, -1))
128 bmp
= images
.getCalendarBitmap()
129 self
.but1
= wx
.BitmapButton(self
, -1, bmp
, (380, 80))
130 self
.Bind(wx
.EVT_BUTTON
, self
.TestDlg
, self
.but1
)
132 # button for calendar window test
134 wx
.StaticText(self
, -1, "Test Calendar Window", (350, 150), (150, -1))
136 self
.but2
= wx
.BitmapButton(self
, -1, bmp
, (380, 180))
137 self
.Bind(wx
.EVT_BUTTON
, self
.TestFrame
, self
.but2
)
139 wx
.StaticText(self
, -1, "Test Calendar Print", (350, 250), (150, -1))
141 self
.but3
= wx
.BitmapButton(self
, -1, bmp
, (380, 280))
142 self
.Bind(wx
.EVT_BUTTON
, self
.OnPreview
, self
.but3
)
146 def TestDlg(self
, event
): # test the date dialog
147 dlg
= wx
.lib
.calendar
.CalenDlg(self
)
150 if dlg
.ShowModal() == wx
.ID_OK
:
155 new_date
= str(month
) + '/'+ str(day
) + '/'+ str(year
)
156 self
.log
.WriteText('Date Selected: %s\n' % new_date
)
158 self
.log
.WriteText('No Date Selected')
160 # calendar window test
162 def TestFrame(self
, event
):
163 frame
= CalendFrame(self
, -1, "Test Calendar", self
.log
)
167 # calendar print preview
169 def OnPreview(self
, event
):
170 month
= self
.calend
.GetMonth()
171 year
= self
.calend
.GetYear()
173 prt
= PrintCalend(self
.frame
, month
, year
)
176 # month and year control events
178 def OnSpin(self
, event
):
179 year
= event
.GetPosition()
180 self
.dtext
.SetValue(str(year
))
181 self
.calend
.SetYear(year
)
182 self
.calend
.Refresh()
184 def EvtComboBox(self
, event
):
185 name
= event
.GetString()
186 self
.log
.WriteText('EvtComboBox: %s\n' % name
)
187 monthval
= self
.date
.FindString(name
)
188 self
.scroll
.SetScrollbar(monthval
, 1, 12, 1, True)
190 self
.calend
.SetMonth(monthval
+1)
193 def Scroll(self
, event
):
194 value
= self
.scroll
.GetThumbPosition()
195 monthval
= int(value
)+1
196 self
.calend
.SetMonth(monthval
)
198 self
.log
.WriteText('Month: %s\n' % value
)
200 name
= wx
.lib
.calendar
.Month
[monthval
]
201 self
.date
.SetValue(name
)
205 def MouseClick(self
, evt
):
206 text
= '%s CLICK %02d/%02d/%d' % (evt
.click
, evt
.day
, evt
.month
, evt
.year
) # format date
207 self
.log
.WriteText('Date Selected: ' + text
+ '\n')
210 # set the highlighted days for the calendar
212 def ResetDisplay(self
):
213 month
= self
.calend
.GetMonth()
216 set_days
= test_days
[month
]
218 set_days
= [1, 5, 12]
220 self
.calend
.AddSelect([4, 11], 'BLUE', 'WHITE')
221 self
.calend
.SetSelDay(set_days
)
222 self
.calend
.Refresh()
224 # increment and decrement toolbar controls
226 def OnIncYear(self
, event
):
227 self
.calend
.IncYear()
230 def OnDecYear(self
, event
):
231 self
.calend
.DecYear()
234 def OnIncMonth(self
, event
):
235 self
.calend
.IncMonth()
238 def OnDecMonth(self
, event
):
239 self
.calend
.DecMonth()
242 def OnCurrent(self
, event
):
243 self
.calend
.SetCurrentDay()
246 # test of full window calendar control functions
248 class CalendFrame(wx
.Frame
):
249 def __init__(self
, parent
, id, title
, log
):
250 wx
.Frame
.__init
__(self
, parent
, id, title
, size
=(400, 400),
251 style
=wx
.DEFAULT_FRAME_STYLE|wx
.NO_FULL_REPAINT_ON_RESIZE
)
253 self
.Bind(wx
.EVT_CLOSE
, self
.OnCloseWindow
)
256 self
.CreateStatusBar()
257 self
.mainmenu
= wx
.MenuBar()
260 menu
= self
.MakeFileMenu()
261 self
.mainmenu
.Append(menu
, '&File')
263 self
.MakeToolMenu() # toolbar
265 self
.SetMenuBar(self
.mainmenu
)
266 self
.calend
= wx
.lib
.calendar
.Calendar(self
, -1)
267 self
.calend
.SetCurrentDay()
268 self
.calend
.grid_color
= 'BLUE'
269 self
.calend
.SetBusType()
270 # self.calend.ShowWeekEnd()
274 self
.Bind(wx
.lib
.calendar
.EVT_CALENDAR
, self
.MouseClick
, self
.calend
)
276 def MouseClick(self
, evt
):
277 text
= '%s CLICK %02d/%02d/%d' % (evt
.click
, evt
.day
, evt
.month
, evt
.year
) # format date
278 self
.log
.WriteText('Date Selected: ' + text
+ '\n')
280 def OnCloseWindow(self
, event
):
283 def ResetDisplay(self
):
284 month
= self
.calend
.GetMonth()
287 set_days
= test_days
[month
]
289 set_days
= [1, 5, 12]
291 self
.calend
.AddSelect([2, 16], 'GREEN', 'WHITE')
293 self
.calend
.SetSelDay(set_days
)
294 self
.calend
.Refresh()
296 def OnIncYear(self
, event
):
297 self
.calend
.IncYear()
300 def OnDecYear(self
, event
):
301 self
.calend
.DecYear()
304 def OnIncMonth(self
, event
):
305 self
.calend
.IncMonth()
308 def OnDecMonth(self
, event
):
309 self
.calend
.DecMonth()
312 def OnCurrent(self
, event
):
313 self
.calend
.SetCurrentDay()
316 def MakeFileMenu(self
):
320 menu
.Append(mID
, 'Decrement', 'Next')
321 self
.Bind(wx
.EVT_MENU
, self
.OnDecMonth
, id=mID
)
324 menu
.Append(mID
, 'Increment', 'Dec')
325 self
.Bind(wx
.EVT_MENU
, self
.OnIncMonth
, id=mID
)
327 menu
.AppendSeparator()
330 menu
.Append(mID
, 'E&xit', 'Exit')
331 self
.Bind(wx
.EVT_MENU
, self
.OnCloseWindow
, id=mID
)
335 def MakeToolMenu(self
):
336 tb
= self
.CreateToolBar(wx
.TB_HORIZONTAL|wx
.NO_BORDER
)
339 SetToolPath(self
, tb
, mID
, images
.getDbDecBitmap(), 'Dec Year')
340 self
.Bind(wx
.EVT_TOOL
, self
.OnDecYear
, id=mID
)
343 SetToolPath(self
, tb
, mID
, images
.getDecBitmap(), 'Dec Month')
344 self
.Bind(wx
.EVT_TOOL
, self
.OnDecMonth
, id=mID
)
347 SetToolPath(self
, tb
, mID
, images
.getPtBitmap(), 'Current Month')
348 self
.Bind(wx
.EVT_TOOL
, self
.OnCurrent
, id=mID
)
351 SetToolPath(self
, tb
, mID
, images
.getIncBitmap(), 'Inc Month')
352 self
.Bind(wx
.EVT_TOOL
, self
.OnIncMonth
, id=mID
)
355 SetToolPath(self
, tb
, mID
, images
.getDbIncBitmap(), 'Inc Year')
356 self
.Bind(wx
.EVT_TOOL
, self
.OnIncYear
, id=mID
)
360 #---------------------------------------------------------------------------
362 # example class for printing/previewing calendars
365 def __init__(self
, parent
, month
, year
):
372 self
.printData
= wx
.PrintData()
375 self
.grid_color
= 'BLUE'
376 self
.back_color
= 'WHITE'
377 self
.sel_color
= 'RED'
378 self
.high_color
= 'LIGHT BLUE'
380 self
.bold
= wx
.NORMAL
382 self
.sel_key
= None # last used by
383 self
.sel_lst
= [] # highlighted selected days
386 self
.hide_title
= False
387 self
.hide_grid
= False
408 def SetDates(self
, month
, year
):
412 def SetStyleDef(self
, desc
):
415 def SetCopies(self
, copies
): # number of copies of label
418 def SetStart(self
, start
): # start position of label
422 printout
= SetPrintout(self
)
423 printout2
= SetPrintout(self
)
424 self
.preview
= wx
.PrintPreview(printout
, printout2
, self
.printData
)
426 if not self
.preview
.Ok():
427 wx
.MessageBox("There was a problem printing!", "Printing", wx
.OK
)
430 self
.preview
.SetZoom(60) # initial zoom value
432 frame
= wx
.PreviewFrame(self
.preview
, self
.frame
, "Print preview")
435 frame
.SetPosition(self
.frame
.GetPosition())
436 frame
.SetSize(self
.frame
.GetSize())
440 pdd
= wx
.PrintDialogData()
441 pdd
.SetPrintData(self
.printData
)
442 printer
= wx
.Printer(pdd
)
443 printout
= SetPrintout(self
)
444 frame
= wx
.Frame(None, -1, "Test")
446 if not printer
.Print(frame
, printout
):
447 wx
.MessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wx
.OK
)
449 self
.printData
= printer
.GetPrintDialogData().GetPrintData()
453 def DoDrawing(self
, DC
):
457 cal
= wx
.lib
.calendar
.PrtCalDraw(self
)
459 if self
.preview
is None:
460 cal
.SetPSize(size
[0]/self
.pagew
, size
[1]/self
.pageh
)
461 cal
.SetPreview(False)
464 if self
.preview
== 1:
465 cal
.SetPSize(size
[0]/self
.pagew
, size
[1]/self
.pageh
)
467 cal
.SetPSize(self
.pwidth
, self
.pheight
)
469 cal
.SetPreview(self
.preview
)
471 cal
.hide_title
= self
.hide_title
# set the calendar parameters
472 cal
.hide_grid
= self
.hide_grid
474 cal
.grid_color
= self
.grid_color
475 cal
.high_color
= self
.high_color
476 cal
.back_color
= self
.back_color
477 cal
.outer_border
= False
481 cal_size
= (3.0, 3.0)
482 cal
.SetSize(cal_size
)
484 year
, month
= self
.year
, self
.month
491 cal
.SetCal(year
, month
) # current month
495 set_days
= test_days
[month
]
497 set_days
= [1, 5, 12]
499 cal
.AddSelect([2, 16], 'GREEN', 'WHITE')
501 cal
.DrawCal(DC
, set_days
)
503 year
, month
= self
.IncMonth(year
, month
)
506 x
= x
+ 4.0 # next column
510 self
.ymax
= DC
.MaxY()
511 self
.xmax
= DC
.MaxX()
513 def IncMonth(self
, year
, month
): # next month
522 def GetTotalPages(self
):
526 def SetPage(self
, page
):
529 def SetPageSize(self
, width
, height
):
530 self
.pwidth
, self
.pheight
= width
, height
532 def SetTotalSize(self
, width
, height
):
533 self
.ptwidth
, self
.ptheight
= width
, height
535 def SetPreview(self
, preview
, scale
):
536 self
.preview
= preview
539 def SetTotalSize(self
, width
, height
):
541 self
.ptheight
= height
543 def SetToolPath(self
, tb
, id, bmp
, title
):
544 tb
.AddSimpleTool(id, bmp
, title
, title
)
546 class SetPrintout(wx
.Printout
):
547 def __init__(self
, canvas
):
548 wx
.Printout
.__init
__(self
)
552 def OnBeginDocument(self
, start
, end
):
553 return self
.base_OnBeginDocument(start
, end
)
555 def OnEndDocument(self
):
556 self
.base_OnEndDocument()
558 def HasPage(self
, page
):
559 if page
<= self
.end_pg
:
564 def GetPageInfo(self
):
565 self
.end_pg
= self
.canvas
.GetTotalPages()
573 return (str_pg
, end_pg
, str_pg
, end_pg
)
575 def OnPreparePrinting(self
):
576 self
.base_OnPreparePrinting()
578 def OnBeginPrinting(self
):
581 self
.preview
= self
.IsPreview()
584 self
.pixelsPerInch
= self
.GetPPIScreen()
586 self
.pixelsPerInch
= self
.GetPPIPrinter()
588 (w
, h
) = dc
.GetSize()
589 scaleX
= float(w
) / 1000
590 scaleY
= float(h
) / 1000
591 self
.printUserScale
= min(scaleX
, scaleY
)
593 self
.base_OnBeginPrinting()
596 self
.psizew
, self
.psizeh
= self
.GetPPIPrinter()
597 return self
.psizew
, self
.psizeh
599 def GetTotalSize(self
):
600 self
.ptsizew
, self
.ptsizeh
= self
.GetPageSizePixels()
601 return self
.ptsizew
, self
.ptsizeh
603 def OnPrintPage(self
, page
):
605 (w
, h
) = dc
.GetSize()
606 scaleX
= float(w
) / 1000
607 scaleY
= float(h
) / 1000
608 self
.printUserScale
= min(scaleX
, scaleY
)
609 dc
.SetUserScale(self
.printUserScale
, self
.printUserScale
)
611 self
.preview
= self
.IsPreview()
613 self
.canvas
.SetPreview(self
.preview
, self
.printUserScale
)
614 self
.canvas
.SetPage(page
)
616 self
.ptsizew
, self
.ptsizeh
= self
.GetPageSizePixels()
617 self
.canvas
.SetTotalSize(self
.ptsizew
, self
.ptsizeh
)
619 self
.psizew
, self
.psizeh
= self
.GetPPIPrinter()
620 self
.canvas
.SetPageSize(self
.psizew
, self
.psizeh
)
622 self
.canvas
.DoDrawing(dc
)
627 frame
= CalendFrame(None, -1, "Test Calendar", log
)
629 self
.SetTopWindow(frame
)
632 #---------------------------------------------------------------------------
634 def MessageDlg(self
, message
, type = 'Message'):
635 dlg
= wx
.MessageDialog(self
, message
, type, wx
.OK | wx
.ICON_INFORMATION
)
639 #---------------------------------------------------------------------------
641 def runTest(frame
, nb
, log
):
642 win
= TestPanel(nb
, log
, frame
)
645 #---------------------------------------------------------------------------
649 This control provides a Calendar control class for displaying and selecting dates.
650 In addition, the class is extended and can be used for printing/previewing.
652 Additional features include weekend highlighting and business type Monday-Sunday
655 See example for various methods used to set display month, year, and highlighted
656 dates (different font and background colours).
665 if __name__
== '__main__':
668 run
.main(['', os
.path
.basename(sys
.argv
[0])])