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 #----------------------------------------------------------------------------
12 from wxPython
.wx
import *
13 from wxPython
.lib
.calendar
import wxCalendar
, Month
, PrtCalDraw
, CalenDlg
19 # highlighted days in month
32 11: [6, 9, 12, 28, 29],
33 12: [8, 9, 10, 11, 20] }
35 # test of full window calendar control functions
42 monthlist
.append(name
)
45 class TestPanel(wxPanel
):
46 def __init__(self
, parent
, log
, frame
):
47 wxPanel
.__init
__(self
, parent
, -1)
52 self
.calend
= wxCalendar(self
, -1, wxPoint(100, 50), wxSize(200, 180))
54 # start_month = 2 # preselect the date for calendar
57 start_month
= self
.calend
.GetMonth() # get the current month & year
58 start_year
= self
.calend
.GetYear()
60 # month list from DateTime module
62 monthlist
= GetMonthList()
65 self
.date
= wxComboBox(self
, mID
, "",
66 wxPoint(100, 20), wxSize(90, -1),
67 monthlist
, wxCB_DROPDOWN
)
68 self
.date
.SetSelection(start_month
-1)
69 EVT_COMBOBOX(self
, mID
, self
.EvtComboBox
)
71 # set start month and year
73 self
.calend
.SetMonth(start_month
)
74 self
.calend
.SetYear(start_year
)
76 # set attributes of calendar
78 self
.calend
.hide_title
= TRUE
79 self
.calend
.HideGrid()
80 self
.calend
.SetWeekColor('WHITE', 'BLACK')
88 self
.Connect(self
.calend
.GetId(), -1, 2100, self
.MouseClick
)
90 # scroll bar for month selection
93 self
.scroll
= wxScrollBar(self
, mID
, wxPoint(100, 240), wxSize(200, 20), wxSB_HORIZONTAL
)
94 self
.scroll
.SetScrollbar(start_month
-1, 1, 12, 1, TRUE
)
95 EVT_COMMAND_SCROLL(self
, mID
, self
.Scroll
)
97 # spin control for year selection
99 self
.dtext
= wxTextCtrl(self
, -1, str(start_year
), wxPoint(200, 20), wxSize(60, -1))
100 h
= self
.dtext
.GetSize().height
103 self
.spin
= wxSpinButton(self
, mID
, wxPoint(270, 20), wxSize(h
*2, h
))
104 self
.spin
.SetRange(1980, 2010)
105 self
.spin
.SetValue(start_year
)
106 EVT_SPIN(self
, mID
, self
.OnSpin
)
108 # button for calendar dialog test
110 wxStaticText(self
, -1, "Test Calendar Dialog", wxPoint(350, 50), wxSize(150, -1))
113 bmp
= images
.getCalendarBitmap()
114 self
.but
= wxBitmapButton(self
, mID
, bmp
, wxPoint(380, 80))#, wxSize(30, 30))
115 EVT_BUTTON(self
, mID
, self
.TestDlg
)
117 # button for calendar window test
119 wxStaticText(self
, -1, "Test Calendar Window", wxPoint(350, 150), wxSize(150, -1))
122 self
.but
= wxBitmapButton(self
, mID
, bmp
, wxPoint(380, 180))#, wxSize(30, 30))
123 EVT_BUTTON(self
, mID
, self
.TestFrame
)
125 wxStaticText(self
, -1, "Test Calendar Print", wxPoint(350, 250), wxSize(150, -1))
128 self
.but
= wxBitmapButton(self
, mID
, bmp
, wxPoint(380, 280))#, wxSize(30, 30))
129 EVT_BUTTON(self
, mID
, self
.OnPreview
)
133 def TestDlg(self
, event
): # test the date dialog
136 if dlg
.ShowModal() == wxID_OK
:
141 new_date
= str(month
) + '/'+ str(day
) + '/'+ str(year
)
142 self
.log
.WriteText('Date Selected: %s\n' % new_date
)
144 self
.log
.WriteText('No Date Selected')
146 # calendar window test
148 def TestFrame(self
, event
):
149 frame
= CalendFrame(self
, -1, "Test Calendar", self
.log
)
153 # calendar print preview
155 def OnPreview(self
, event
):
156 month
= self
.calend
.GetMonth()
157 year
= self
.calend
.GetYear()
159 prt
= PrintCalend(self
.frame
, month
, year
)
162 # month and year control events
164 def OnSpin(self
, event
):
165 year
= event
.GetPosition()
166 self
.dtext
.SetValue(str(year
))
167 self
.calend
.SetYear(year
)
168 self
.calend
.Refresh()
170 def EvtComboBox(self
, event
):
171 name
= event
.GetString()
172 self
.log
.WriteText('EvtComboBox: %s\n' % name
)
173 monthval
= self
.date
.FindString(name
)
174 self
.scroll
.SetScrollbar(monthval
, 1, 12, 1, TRUE
)
176 self
.calend
.SetMonth(monthval
+1)
179 def Scroll(self
, event
):
180 value
= self
.scroll
.GetThumbPosition()
181 monthval
= int(value
)+1
182 self
.calend
.SetMonth(monthval
)
184 self
.log
.WriteText('Month: %s\n' % value
)
186 name
= Month
[monthval
]
187 self
.date
.SetValue(name
)
191 def MouseClick(self
, evt
):
192 text
= '%s CLICK %02d/%02d/%d' % (evt
.click
, evt
.day
, evt
.month
, evt
.year
) # format date
193 self
.log
.WriteText('Date Selected: ' + text
+ '\n')
196 # set the highlighted days for the calendar
198 def ResetDisplay(self
):
199 month
= self
.calend
.GetMonth()
201 set_days
= test_days
[month
]
203 set_days
= [1, 5, 12]
205 self
.calend
.AddSelect([4, 11], 'BLUE', 'WHITE')
206 self
.calend
.SetSelDay(set_days
)
207 self
.calend
.Refresh()
209 # increment and decrement toolbar controls
211 def OnIncYear(self
, event
):
212 self
.calend
.IncYear()
215 def OnDecYear(self
, event
):
216 self
.calend
.DecYear()
219 def OnIncMonth(self
, event
):
220 self
.calend
.IncMonth()
223 def OnDecMonth(self
, event
):
224 self
.calend
.DecMonth()
227 def OnCurrent(self
, event
):
228 self
.calend
.SetCurrentDay()
231 # test of full window calendar control functions
233 class CalendFrame(wxFrame
):
234 def __init__(self
, parent
, id, title
, log
):
235 wxFrame
.__init
__(self
, parent
, id, title
, size
=wxSize(400, 400),
236 style
=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE
)
237 EVT_CLOSE(self
, self
.OnCloseWindow
)
240 self
.CreateStatusBar()
241 self
.mainmenu
= wxMenuBar()
244 menu
= self
.MakeFileMenu()
245 self
.mainmenu
.Append(menu
, '&File')
247 self
.MakeToolMenu() # toolbar
249 self
.SetMenuBar(self
.mainmenu
)
250 self
.calend
= wxCalendar(self
, -1)
251 self
.calend
.SetCurrentDay()
252 self
.calend
.grid_color
= 'BLUE'
253 self
.calend
.SetBusType()
254 # self.calend.ShowWeekEnd()
258 self
.Connect(self
.calend
.GetId(), -1, 2100, self
.MouseClick
)
260 def MouseClick(self
, evt
):
261 text
= '%s CLICK %02d/%02d/%d' % (evt
.click
, evt
.day
, evt
.month
, evt
.year
) # format date
262 self
.log
.WriteText('Date Selected: ' + text
+ '\n')
264 def OnCloseWindow(self
, event
):
267 def ResetDisplay(self
):
268 month
= self
.calend
.GetMonth()
270 set_days
= test_days
[month
]
272 set_days
= [1, 5, 12]
274 self
.calend
.AddSelect([2, 16], 'GREEN', 'WHITE')
276 self
.calend
.SetSelDay(set_days
)
277 self
.calend
.Refresh()
279 def OnIncYear(self
, event
):
280 self
.calend
.IncYear()
283 def OnDecYear(self
, event
):
284 self
.calend
.DecYear()
287 def OnIncMonth(self
, event
):
288 self
.calend
.IncMonth()
291 def OnDecMonth(self
, event
):
292 self
.calend
.DecMonth()
295 def OnCurrent(self
, event
):
296 self
.calend
.SetCurrentDay()
299 def MakeFileMenu(self
):
303 menu
.Append(mID
, 'Decrement', 'Next')
304 EVT_MENU(self
, mID
, self
.OnDecMonth
)
307 menu
.Append(mID
, 'Increment', 'Dec')
308 EVT_MENU(self
, mID
, self
.OnIncMonth
)
310 menu
.AppendSeparator()
313 menu
.Append(mID
, 'E&xit', 'Exit')
314 EVT_MENU(self
, mID
, self
.OnCloseWindow
)
318 def MakeToolMenu(self
):
319 tb
= self
.CreateToolBar(wxTB_HORIZONTAL|wxNO_BORDER
)
322 SetToolPath(self
, tb
, mID
, images
.getDbDecBitmap(), 'Dec Year')
323 EVT_TOOL(self
, mID
, self
.OnDecYear
)
326 SetToolPath(self
, tb
, mID
, images
.getDecBitmap(), 'Dec Month')
327 EVT_TOOL(self
, mID
, self
.OnDecMonth
)
330 SetToolPath(self
, tb
, mID
, images
.getPtBitmap(), 'Current Month')
331 EVT_TOOL(self
, mID
, self
.OnCurrent
)
334 SetToolPath(self
, tb
, mID
, images
.getIncBitmap(), 'Inc Month')
335 EVT_TOOL(self
, mID
, self
.OnIncMonth
)
338 SetToolPath(self
, tb
, mID
, images
.getDbIncBitmap(), 'Inc Year')
339 EVT_TOOL(self
, mID
, self
.OnIncYear
)
343 #---------------------------------------------------------------------------
345 # example class for printing/previewing calendars
348 def __init__(self
, parent
, month
, year
):
355 self
.printData
= wxPrintData()
358 self
.grid_color
= 'BLUE'
359 self
.back_color
= 'WHITE'
360 self
.sel_color
= 'RED'
361 self
.high_color
= 'LIGHT BLUE'
365 self
.sel_key
= None # last used by
366 self
.sel_lst
= [] # highlighted selected days
369 self
.hide_title
= FALSE
370 self
.hide_grid
= FALSE
391 def SetDates(self
, month
, year
):
395 def SetStyleDef(self
, desc
):
398 def SetCopies(self
, copies
): # number of copies of label
401 def SetStart(self
, start
): # start position of label
405 printout
= SetPrintout(self
)
406 printout2
= SetPrintout(self
)
407 self
.preview
= wxPrintPreview(printout
, printout2
, self
.printData
)
408 if not self
.preview
.Ok():
409 wxMessageBox("There was a problem printing!", "Printing", wxOK
)
412 self
.preview
.SetZoom(60) # initial zoom value
414 frame
= wxPreviewFrame(self
.preview
, self
.frame
, "Print preview")
417 frame
.SetPosition(self
.frame
.GetPosition())
418 frame
.SetSize(self
.frame
.GetSize())
422 pdd
= wxPrintDialogData()
423 pdd
.SetPrintData(self
.printData
)
424 printer
= wxPrinter(pdd
)
425 printout
= SetPrintout(self
)
426 frame
= wxFrame(None, -1, "Test")
427 if not printer
.Print(frame
, printout
):
428 wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK
)
430 self
.printData
= printer
.GetPrintDialogData().GetPrintData()
433 def DoDrawing(self
, DC
):
434 size
= DC
.GetSizeTuple()
437 cal
= PrtCalDraw(self
)
439 if self
.preview
is None:
440 cal
.SetPSize(size
[0]/self
.pagew
, size
[1]/self
.pageh
)
441 cal
.SetPreview(FALSE
)
444 if self
.preview
== 1:
445 cal
.SetPSize(size
[0]/self
.pagew
, size
[1]/self
.pageh
)
447 cal
.SetPSize(self
.pwidth
, self
.pheight
)
449 cal
.SetPreview(self
.preview
)
451 cal
.hide_title
= self
.hide_title
# set the calendar parameters
452 cal
.hide_grid
= self
.hide_grid
454 cal
.grid_color
= self
.grid_color
455 cal
.high_color
= self
.high_color
456 cal
.back_color
= self
.back_color
457 cal
.outer_border
= FALSE
461 cal_size
= wxSize(3.0, 3.0)
462 cal
.SetSize(cal_size
)
464 year
, month
= self
.year
, self
.month
470 cal
.SetCal(year
, month
) # current month
474 set_days
= test_days
[month
]
476 set_days
= [1, 5, 12]
478 cal
.AddSelect([2, 16], 'GREEN', 'WHITE')
480 cal
.DrawCal(DC
, set_days
)
482 year
, month
= self
.IncMonth(year
, month
)
484 x
= x
+ 4.0 # next colum
488 self
.ymax
= DC
.MaxY()
489 self
.xmax
= DC
.MaxX()
491 def IncMonth(self
, year
, month
): # next month
499 def GetTotalPages(self
):
503 def SetPage(self
, page
):
506 def SetPageSize(self
, width
, height
):
507 self
.pwidth
, self
.pheight
= width
, height
509 def SetTotalSize(self
, width
, height
):
510 self
.ptwidth
, self
.ptheight
= width
, height
512 def SetPreview(self
, preview
, scale
):
513 self
.preview
= preview
516 def SetTotalSize(self
, width
, height
):
518 self
.ptheight
= height
520 def SetToolPath(self
, tb
, id, bmp
, title
):
521 tb
.AddSimpleTool(id, bmp
, title
, title
)
523 class SetPrintout(wxPrintout
):
524 def __init__(self
, canvas
):
525 wxPrintout
.__init
__(self
)
529 def OnBeginDocument(self
, start
, end
):
530 return self
.base_OnBeginDocument(start
, end
)
532 def OnEndDocument(self
):
533 self
.base_OnEndDocument()
535 def HasPage(self
, page
):
536 if page
<= self
.end_pg
:
541 def GetPageInfo(self
):
542 self
.end_pg
= self
.canvas
.GetTotalPages()
548 return (str_pg
, end_pg
, str_pg
, end_pg
)
550 def OnPreparePrinting(self
):
551 self
.base_OnPreparePrinting()
553 def OnBeginPrinting(self
):
556 self
.preview
= self
.IsPreview()
558 self
.pixelsPerInch
= self
.GetPPIScreen()
560 self
.pixelsPerInch
= self
.GetPPIPrinter()
562 (w
, h
) = dc
.GetSizeTuple()
563 scaleX
= float(w
) / 1000
564 scaleY
= float(h
) / 1000
565 self
.printUserScale
= min(scaleX
, scaleY
)
567 self
.base_OnBeginPrinting()
570 self
.psizew
, self
.psizeh
= self
.GetPPIPrinter()
571 return self
.psizew
, self
.psizeh
573 def GetTotalSize(self
):
574 self
.ptsizew
, self
.ptsizeh
= self
.GetPageSizePixels()
575 return self
.ptsizew
, self
.ptsizeh
577 def OnPrintPage(self
, page
):
579 (w
, h
) = dc
.GetSizeTuple()
580 scaleX
= float(w
) / 1000
581 scaleY
= float(h
) / 1000
582 self
.printUserScale
= min(scaleX
, scaleY
)
583 dc
.SetUserScale(self
.printUserScale
, self
.printUserScale
)
585 self
.preview
= self
.IsPreview()
587 self
.canvas
.SetPreview(self
.preview
, self
.printUserScale
)
588 self
.canvas
.SetPage(page
)
590 self
.ptsizew
, self
.ptsizeh
= self
.GetPageSizePixels()
591 self
.canvas
.SetTotalSize(self
.ptsizew
, self
.ptsizeh
)
593 self
.psizew
, self
.psizeh
= self
.GetPPIPrinter()
594 self
.canvas
.SetPageSize(self
.psizew
, self
.psizeh
)
596 self
.canvas
.DoDrawing(dc
)
601 frame
= CalendFrame(None, -1, "Test Calendar")
603 self
.SetTopWindow(frame
)
606 #---------------------------------------------------------------------------
608 def MessageDlg(self
, message
, type = 'Message'):
609 dlg
= wxMessageDialog(self
, message
, type, wxOK | wxICON_INFORMATION
)
613 #---------------------------------------------------------------------------
620 if __name__
== '__main__':
624 #---------------------------------------------------------------------------
626 def runTest(frame
, nb
, log
):
627 win
= TestPanel(nb
, log
, frame
)
630 #---------------------------------------------------------------------------
634 This control provides a calendar control class for displaying and selecting dates. In addition, the class is extended and can now be used for printing/previewing.
636 Additional features include weekend highlighting and business type Monday-Sunday format.
638 See example for various methods used to set display month, year, and highlighted dates (different font and background colours).