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
506 cal
.SetCal(year
, month
) # current month
510 set_days
= test_days
[month
]
512 set_days
= [1, 5, 12]
514 cal
.AddSelect([2, 16], 'GREEN', 'WHITE')
516 cal
.DrawCal(DC
, set_days
)
518 year
, month
= self
.IncMonth(year
, month
)
521 x
= x
+ 4.0 # next column
525 self
.ymax
= DC
.MaxY()
526 self
.xmax
= DC
.MaxX()
528 def IncMonth(self
, year
, month
): # next month
537 def GetTotalPages(self
):
541 def SetPage(self
, page
):
544 def SetPageSize(self
, width
, height
):
545 self
.pwidth
, self
.pheight
= width
, height
547 def SetTotalSize(self
, width
, height
):
548 self
.ptwidth
, self
.ptheight
= width
, height
550 def SetPreview(self
, preview
, scale
):
551 self
.preview
= preview
554 def SetTotalSize(self
, width
, height
):
556 self
.ptheight
= height
558 def SetToolPath(self
, tb
, id, bmp
, title
):
559 tb
.AddSimpleTool(id, bmp
, title
, title
)
561 class SetPrintout(wx
.Printout
):
562 def __init__(self
, canvas
):
563 wx
.Printout
.__init
__(self
)
567 def OnBeginDocument(self
, start
, end
):
568 return self
.base_OnBeginDocument(start
, end
)
570 def OnEndDocument(self
):
571 self
.base_OnEndDocument()
573 def HasPage(self
, page
):
574 if page
<= self
.end_pg
:
579 def GetPageInfo(self
):
580 self
.end_pg
= self
.canvas
.GetTotalPages()
588 return (str_pg
, end_pg
, str_pg
, end_pg
)
590 def OnPreparePrinting(self
):
591 self
.base_OnPreparePrinting()
593 def OnBeginPrinting(self
):
596 self
.preview
= self
.IsPreview()
599 self
.pixelsPerInch
= self
.GetPPIScreen()
601 self
.pixelsPerInch
= self
.GetPPIPrinter()
603 (w
, h
) = dc
.GetSize()
604 scaleX
= float(w
) / 1000
605 scaleY
= float(h
) / 1000
606 self
.printUserScale
= min(scaleX
, scaleY
)
608 self
.base_OnBeginPrinting()
611 self
.psizew
, self
.psizeh
= self
.GetPPIPrinter()
612 return self
.psizew
, self
.psizeh
614 def GetTotalSize(self
):
615 self
.ptsizew
, self
.ptsizeh
= self
.GetPageSizePixels()
616 return self
.ptsizew
, self
.ptsizeh
618 def OnPrintPage(self
, page
):
620 (w
, h
) = dc
.GetSize()
621 scaleX
= float(w
) / 1000
622 scaleY
= float(h
) / 1000
623 self
.printUserScale
= min(scaleX
, scaleY
)
624 dc
.SetUserScale(self
.printUserScale
, self
.printUserScale
)
626 self
.preview
= self
.IsPreview()
628 self
.canvas
.SetPreview(self
.preview
, self
.printUserScale
)
629 self
.canvas
.SetPage(page
)
631 self
.ptsizew
, self
.ptsizeh
= self
.GetPageSizePixels()
632 self
.canvas
.SetTotalSize(self
.ptsizew
, self
.ptsizeh
)
634 self
.psizew
, self
.psizeh
= self
.GetPPIPrinter()
635 self
.canvas
.SetPageSize(self
.psizew
, self
.psizeh
)
637 self
.canvas
.DoDrawing(dc
)
642 frame
= CalendFrame(None, -1, "Test Calendar", log
)
644 self
.SetTopWindow(frame
)
647 #---------------------------------------------------------------------------
649 def MessageDlg(self
, message
, type = 'Message'):
650 dlg
= wx
.MessageDialog(self
, message
, type, wx
.OK | wx
.ICON_INFORMATION
)
654 #---------------------------------------------------------------------------
656 def runTest(frame
, nb
, log
):
657 win
= TestPanel(nb
, log
, frame
)
660 #---------------------------------------------------------------------------
664 This control provides a Calendar control class for displaying and selecting dates.
665 In addition, the class is extended and can be used for printing/previewing.
667 Additional features include weekend highlighting and business type Monday-Sunday
670 See example for various methods used to set display month, year, and highlighted
671 dates (different font and background colours).
680 if __name__
== '__main__':
683 run
.main(['', os
.path
.basename(sys
.argv
[0])])