1 #----------------------------------------------------------------------------
3 # Purpose: Calendar control display testing on panel for wxPython demo
5 # Author: Lorne White (email: lwhite1@planet.eon.net)
8 # Version 0.8 2000/04/16
9 # Licence: wxWindows license
10 #----------------------------------------------------------------------------
12 from wxPython
.wx
import *
13 from wxPython
.lib
.calendar
import wxCalendar
, Month
, PrtCalDraw
16 dir_path
= os
.getcwd()
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))
57 # month list from DateTime module
59 monthlist
= GetMonthList()
61 self
.date
= wxComboBox(self
, 10, Month
[start_month
], wxPoint(100, 20), wxSize(90, -1), monthlist
, wxCB_DROPDOWN
)
62 EVT_COMBOBOX(self
, 10, self
.EvtComboBox
)
64 # set start month and year
66 self
.calend
.SetMonth(start_month
)
67 self
.calend
.SetYear(start_year
)
69 # set attributes of calendar
71 self
.calend
.hide_title
= TRUE
72 self
.calend
.HideGrid()
73 self
.calend
.SetWeekColor('WHITE', 'BLACK')
81 self
.Connect(self
.calend
.GetId(), -1, 2100, self
.MouseClick
)
83 # scroll bar for month selection
85 self
.scroll
= wxScrollBar(self
, 40, wxPoint(100, 240), wxSize(200, 20), wxSB_HORIZONTAL
)
86 self
.scroll
.SetScrollbar(start_month
-1, 1, 12, 1, TRUE
)
87 EVT_COMMAND_SCROLL(self
, 40, self
.Scroll
)
89 # spin control for year selection
91 self
.dtext
= wxTextCtrl(self
, -1, str(start_year
), wxPoint(200, 20), wxSize(60, -1))
92 h
= self
.dtext
.GetSize().height
94 self
.spin
= wxSpinButton(self
, 20, wxPoint(270, 20), wxSize(h
*2, h
))
95 self
.spin
.SetRange(1980, 2010)
96 self
.spin
.SetValue(start_year
)
97 EVT_SPIN(self
, 20, self
.OnSpin
)
99 # button for calendar dialog test
101 wxStaticText(self
, -1, "Test Calendar Dialog", wxPoint(350, 50), wxSize(150, -1))
103 bmp
= wxBitmap('bitmaps/Calend.bmp', wxBITMAP_TYPE_BMP
)
104 self
.but
= wxBitmapButton(self
, 60, bmp
, wxPoint(380, 80))#, wxSize(30, 30))
105 EVT_BUTTON(self
, 60, self
.TestDlg
)
107 # button for calendar window test
109 wxStaticText(self
, -1, "Test Calendar Window", wxPoint(350, 150), wxSize(150, -1))
111 bmp
= wxBitmap('bitmaps/Calend.bmp', wxBITMAP_TYPE_BMP
)
112 self
.but
= wxBitmapButton(self
, 160, bmp
, wxPoint(380, 180))#, wxSize(30, 30))
113 EVT_BUTTON(self
, 160, self
.TestFrame
)
115 wxStaticText(self
, -1, "Test Calendar Print", wxPoint(350, 250), wxSize(150, -1))
117 bmp
= wxBitmap('bitmaps/Calend.bmp', wxBITMAP_TYPE_BMP
)
118 self
.but
= wxBitmapButton(self
, 170, bmp
, wxPoint(380, 280))#, wxSize(30, 30))
119 EVT_BUTTON(self
, 170, self
.OnPreview
)
123 def TestDlg(self
, event
):
124 dlg
= CalenDlg(self
, self
.log
)
129 # calendar window test
131 def TestFrame(self
, event
):
132 frame
= CalendFrame(self
, -1, "Test Calendar", self
.log
)
136 # calendar print preview
138 def OnPreview(self
, event
):
139 month
= self
.calend
.GetMonth()
140 year
= self
.calend
.GetYear()
142 prt
= PrintCalend(self
.frame
, month
, year
)
145 # month and year control events
147 def OnSpin(self
, event
):
148 year
= event
.GetPosition()
149 self
.dtext
.SetValue(str(year
))
150 self
.calend
.SetYear(year
)
151 self
.calend
.Refresh()
153 def EvtComboBox(self
, event
):
154 name
= event
.GetString()
155 self
.log
.WriteText('EvtComboBox: %s\n' % name
)
156 monthval
= self
.date
.FindString(name
)
157 self
.scroll
.SetScrollbar(monthval
, 1, 12, 1, TRUE
)
159 self
.calend
.SetMonth(monthval
+1)
162 def Scroll(self
, event
):
163 value
= self
.scroll
.GetThumbPosition()
164 monthval
= int(value
)+1
165 self
.calend
.SetMonth(monthval
)
167 self
.log
.WriteText('Month: %s\n' % value
)
169 name
= Month
[monthval
]
170 self
.date
.SetValue(name
)
174 def MouseClick(self
, evt
):
175 text
= '%s CLICK %02d/%02d/%d' % (evt
.click
, evt
.day
, evt
.month
, evt
.year
) # format date
176 self
.log
.WriteText('Date Selected: ' + text
+ '\n')
179 # set the highlighted days for the calendar
181 def ResetDisplay(self
):
182 month
= self
.calend
.GetMonth()
184 set_days
= test_days
[month
]
186 set_days
= [1, 5, 12]
188 self
.calend
.AddSelect([4, 11], 'BLUE', 'WHITE')
189 self
.calend
.SetSelDay(set_days
)
190 self
.calend
.Refresh()
192 # increment and decrement toolbar controls
194 def OnIncYear(self
, event
):
195 self
.calend
.IncYear()
198 def OnDecYear(self
, event
):
199 self
.calend
.DecYear()
202 def OnIncMonth(self
, event
):
203 self
.calend
.IncMonth()
206 def OnDecMonth(self
, event
):
207 self
.calend
.DecMonth()
210 def OnCurrent(self
, event
):
211 self
.calend
.SetCurrentDay()
214 # test the calendar control in a dialog
216 class CalenDlg(wxDialog
):
217 def __init__(self
, parent
, log
):
219 wxDialog
.__init
__(self
, parent
, -1, "Test Calendar", wxPyDefaultPosition
, wxSize(280, 300))
224 # get month list from DateTime
226 monthlist
= GetMonthList()
230 self
.date
= wxComboBox(self
, 100, Month
[start_month
], wxPoint(20, 20), wxSize(90, -1), monthlist
, wxCB_DROPDOWN
)
231 EVT_COMBOBOX(self
, 100, self
.EvtComboBox
)
233 # alternate spin button to control the month
235 h
= self
.date
.GetSize().height
236 self
.m_spin
= wxSpinButton(self
, 120, wxPoint(120, 20), wxSize(h
*2, h
), wxSP_VERTICAL
)
237 self
.m_spin
.SetRange(1, 12)
238 self
.m_spin
.SetValue(start_month
)
240 EVT_SPIN(self
, 120, self
.OnMonthSpin
)
242 # spin button to conrol the year
244 self
.dtext
= wxTextCtrl(self
, -1, str(start_year
), wxPoint(160, 20), wxSize(60, -1))
245 h
= self
.dtext
.GetSize().height
247 self
.y_spin
= wxSpinButton(self
, 20, wxPoint(220, 20), wxSize(h
*2, h
), wxSP_VERTICAL
)
248 self
.y_spin
.SetRange(1980, 2010)
249 self
.y_spin
.SetValue(start_year
)
251 EVT_SPIN(self
, 20, self
.OnYrSpin
)
253 # set the calendar and attributes
255 self
.calend
= wxCalendar(self
, -1, wxPoint(20, 60), wxSize(240, 200))
256 self
.calend
.SetMonth(start_month
)
257 self
.calend
.SetYear(start_year
)
259 self
.calend
.HideTitle()
260 self
.calend
.ShowWeekEnd()
264 self
.Connect(self
.calend
.GetId(), -1, 2100, self
.MouseClick
)
266 # log the mouse clicks
268 def MouseClick(self
, evt
):
269 text
= '%s CLICK %02d/%02d/%d' % (evt
.click
, evt
.day
, evt
.month
, evt
.year
) # format date
270 self
.log
.WriteText('Date Selected: ' + text
+ '\n')
272 if evt
.click
== 'DLEFT':
273 self
.EndModal(wxID_OK
)
275 # month and year spin selection routines
277 def OnMonthSpin(self
, event
):
278 month
= event
.GetPosition()
279 if month
>= 0 and month
<= 12:
280 self
.date
.SetValue(Month
[month
])
281 self
.calend
.SetMonth(month
)
282 self
.calend
.Refresh()
284 def OnYrSpin(self
, event
):
285 year
= event
.GetPosition()
286 self
.dtext
.SetValue(str(year
))
287 self
.calend
.SetYear(year
)
288 self
.calend
.Refresh()
290 def EvtComboBox(self
, event
):
291 name
= event
.GetString()
292 self
.log
.WriteText('EvtComboBox: %s\n' % name
)
293 monthval
= self
.date
.FindString(name
)
294 self
.m_spin
.SetValue(monthval
+1)
296 self
.calend
.SetMonth(monthval
+1)
299 # set the calendar for highlighted days
301 def ResetDisplay(self
):
302 month
= self
.calend
.GetMonth()
304 set_days
= test_days
[month
]
306 set_days
= [1, 5, 12]
308 self
.calend
.AddSelect([4, 11], 'BLUE', 'WHITE')
310 self
.calend
.SetSelDay(set_days
)
311 self
.calend
.Refresh()
313 # test of full window calendar control functions
315 class CalendFrame(wxFrame
):
316 def __init__(self
, parent
, id, title
, log
):
317 wxFrame
.__init
__(self
, parent
, id, title
, wxPyDefaultPosition
, wxSize(400, 400))
318 EVT_CLOSE(self
, self
.OnCloseWindow
)
321 self
.CreateStatusBar()
322 self
.mainmenu
= wxMenuBar()
325 menu
= self
.MakeFileMenu()
326 self
.mainmenu
.Append(menu
, '&File')
328 self
.MakeToolMenu() # toolbar
330 self
.SetMenuBar(self
.mainmenu
)
331 self
.calend
= wxCalendar(self
, -1)
332 self
.calend
.SetCurrentDay()
333 self
.calend
.grid_color
= 'BLUE'
334 self
.calend
.SetBusType()
335 # self.calend.ShowWeekEnd()
339 self
.Connect(self
.calend
.GetId(), -1, 2100, self
.MouseClick
)
341 def MouseClick(self
, evt
):
342 text
= '%s CLICK %02d/%02d/%d' % (evt
.click
, evt
.day
, evt
.month
, evt
.year
) # format date
343 self
.log
.WriteText('Date Selected: ' + text
+ '\n')
345 def OnCloseWindow(self
, event
):
348 def ResetDisplay(self
):
349 month
= self
.calend
.GetMonth()
351 set_days
= test_days
[month
]
353 set_days
= [1, 5, 12]
355 self
.calend
.AddSelect([2, 16], 'GREEN', 'WHITE')
357 self
.calend
.SetSelDay(set_days
)
358 self
.calend
.Refresh()
360 def OnIncYear(self
, event
):
361 self
.calend
.IncYear()
364 def OnDecYear(self
, event
):
365 self
.calend
.DecYear()
368 def OnIncMonth(self
, event
):
369 self
.calend
.IncMonth()
372 def OnDecMonth(self
, event
):
373 self
.calend
.DecMonth()
376 def OnCurrent(self
, event
):
377 self
.calend
.SetCurrentDay()
380 def MakeFileMenu(self
):
384 menu
.Append(mID
, 'Decrement', 'Next')
385 EVT_MENU(self
, mID
, self
.OnDecMonth
)
388 menu
.Append(mID
, 'Increment', 'Dec')
389 EVT_MENU(self
, mID
, self
.OnIncMonth
)
391 menu
.AppendSeparator()
394 menu
.Append(mID
, 'E&xit', 'Exit')
395 EVT_MENU(self
, mID
, self
.OnCloseWindow
)
399 def MakeToolMenu(self
):
400 tb
= self
.CreateToolBar(wxTB_HORIZONTAL|wxNO_BORDER
)
402 bmp_path
= 'bitmaps/'
403 SetToolPath(self
, tb
, 10, bmp_path
+ 'DbDec.bmp', 'Dec Year')
404 EVT_TOOL(self
, 10, self
.OnDecYear
)
406 SetToolPath(self
, tb
, 15, bmp_path
+ 'Dec.bmp', 'Dec Month')
407 EVT_TOOL(self
, 15, self
.OnDecMonth
)
409 SetToolPath(self
, tb
, 30, bmp_path
+ 'Pt.bmp', 'Current Month')
410 EVT_TOOL(self
, 30, self
.OnCurrent
)
412 SetToolPath(self
, tb
, 40, bmp_path
+ 'Inc.bmp', 'Inc Month')
413 EVT_TOOL(self
, 40, self
.OnIncMonth
)
415 SetToolPath(self
, tb
, 45, bmp_path
+ 'DbInc.bmp', 'Inc Year')
416 EVT_TOOL(self
, 45, self
.OnIncYear
)
420 #---------------------------------------------------------------------------
422 # example class for printing/previewing calendars
425 def __init__(self
, parent
, month
, year
):
432 self
.printData
= wxPrintData()
435 self
.grid_color
= 'BLUE'
436 self
.back_color
= 'WHITE'
437 self
.sel_color
= 'RED'
438 self
.high_color
= 'LIGHT BLUE'
442 self
.sel_key
= None # last used by
443 self
.sel_lst
= [] # highlighted selected days
446 self
.hide_title
= FALSE
447 self
.hide_grid
= FALSE
468 def SetDates(self
, month
, year
):
472 def SetStyleDef(self
, desc
):
475 def SetCopies(self
, copies
): # number of copies of label
478 def SetStart(self
, start
): # start position of label
482 printout
= SetPrintout(self
)
483 printout2
= SetPrintout(self
)
484 self
.preview
= wxPrintPreview(printout
, printout2
, self
.printData
)
485 if not self
.preview
.Ok():
486 wxMessageBox("There was a problem printing!", "Printing", wxOK
)
489 self
.preview
.SetZoom(60) # initial zoom value
491 frame
= wxPreviewFrame(self
.preview
, self
.frame
, "Print preview")
494 frame
.SetPosition(self
.frame
.GetPosition())
495 frame
.SetSize(self
.frame
.GetSize())
499 pdd
= wxPrintDialogData()
500 pdd
.SetPrintData(self
.printData
)
501 printer
= wxPrinter(pdd
)
502 printout
= SetPrintout(self
)
503 frame
= wxFrame(NULL
, -1, "Test")
504 if not printer
.Print(frame
, printout
):
505 wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK
)
507 self
.printData
= printer
.GetPrintDialogData().GetPrintData()
510 def DoDrawing(self
, DC
):
511 size
= DC
.GetSizeTuple()
514 cal
= PrtCalDraw(self
)
516 if self
.preview
is None:
517 cal
.SetPSize(size
[0]/self
.pagew
, size
[1]/self
.pageh
)
518 cal
.SetPreview(FALSE
)
521 if self
.preview
== 1:
522 cal
.SetPSize(size
[0]/self
.pagew
, size
[1]/self
.pageh
)
524 cal
.SetPSize(self
.pwidth
, self
.pheight
)
526 cal
.SetPreview(self
.preview
)
528 cal
.hide_title
= self
.hide_title
# set the calendar parameters
529 cal
.hide_grid
= self
.hide_grid
531 cal
.grid_color
= self
.grid_color
532 cal
.high_color
= self
.high_color
533 cal
.back_color
= self
.back_color
534 cal
.outer_border
= FALSE
538 cal_size
= wxSize(3.0, 3.0)
539 cal
.SetSize(cal_size
)
541 year
, month
= self
.year
, self
.month
547 cal
.SetCal(year
, month
) # current month
551 set_days
= test_days
[month
]
553 set_days
= [1, 5, 12]
555 cal
.AddSelect([2, 16], 'GREEN', 'WHITE')
557 cal
.DrawCal(DC
, set_days
)
559 year
, month
= self
.IncMonth(year
, month
)
561 x
= x
+ 4.0 # next colum
565 self
.ymax
= DC
.MaxY()
566 self
.xmax
= DC
.MaxX()
568 def IncMonth(self
, year
, month
): # next month
576 def GetTotalPages(self
):
580 def SetPage(self
, page
):
583 def SetPageSize(self
, width
, height
):
584 self
.pwidth
, self
.pheight
= width
, height
586 def SetTotalSize(self
, width
, height
):
587 self
.ptwidth
, self
.ptheight
= width
, height
589 def SetPreview(self
, preview
, scale
):
590 self
.preview
= preview
593 def SetTotalSize(self
, width
, height
):
595 self
.ptheight
= height
597 def SetToolPath(self
, tb
, id, bmp
, title
):
599 tb
.AddSimpleTool(id, wxBitmap(os
.path
.join(dir_path
, bmp
), wxBITMAP_TYPE_BMP
),
602 class SetPrintout(wxPrintout
):
603 def __init__(self
, canvas
):
604 wxPrintout
.__init
__(self
)
608 def OnBeginDocument(self
, start
, end
):
609 return self
.base_OnBeginDocument(start
, end
)
611 def OnEndDocument(self
):
612 self
.base_OnEndDocument()
614 def HasPage(self
, page
):
615 if page
<= self
.end_pg
:
620 def GetPageInfo(self
):
621 self
.end_pg
= self
.canvas
.GetTotalPages()
627 return (str_pg
, end_pg
, str_pg
, end_pg
)
629 def OnPreparePrinting(self
):
630 self
.base_OnPreparePrinting()
632 def OnBeginPrinting(self
):
635 self
.preview
= self
.IsPreview()
637 self
.pixelsPerInch
= self
.GetPPIScreen()
639 self
.pixelsPerInch
= self
.GetPPIPrinter()
641 (w
, h
) = dc
.GetSizeTuple()
642 scaleX
= float(w
) / 1000
643 scaleY
= float(h
) / 1000
644 self
.printUserScale
= min(scaleX
, scaleY
)
646 self
.base_OnBeginPrinting()
649 self
.psizew
, self
.psizeh
= self
.GetPPIPrinter()
650 return self
.psizew
, self
.psizeh
652 def GetTotalSize(self
):
653 self
.ptsizew
, self
.ptsizeh
= self
.GetPageSizePixels()
654 return self
.ptsizew
, self
.ptsizeh
656 def OnPrintPage(self
, page
):
658 (w
, h
) = dc
.GetSizeTuple()
659 scaleX
= float(w
) / 1000
660 scaleY
= float(h
) / 1000
661 self
.printUserScale
= min(scaleX
, scaleY
)
662 dc
.SetUserScale(self
.printUserScale
, self
.printUserScale
)
664 self
.preview
= self
.IsPreview()
666 self
.canvas
.SetPreview(self
.preview
, self
.printUserScale
)
667 self
.canvas
.SetPage(page
)
669 self
.ptsizew
, self
.ptsizeh
= self
.GetPageSizePixels()
670 self
.canvas
.SetTotalSize(self
.ptsizew
, self
.ptsizeh
)
672 self
.psizew
, self
.psizeh
= self
.GetPPIPrinter()
673 self
.canvas
.SetPageSize(self
.psizew
, self
.psizeh
)
675 self
.canvas
.DoDrawing(dc
)
680 frame
= CalendFrame(NULL
, -1, "Test Calendar")
682 self
.SetTopWindow(frame
)
685 #---------------------------------------------------------------------------
687 def MessageDlg(self
, message
, type = 'Message'):
688 dlg
= wxMessageDialog(self
, message
, type, wxOK | wxICON_INFORMATION
)
692 #---------------------------------------------------------------------------
699 if __name__
== '__main__':
703 #---------------------------------------------------------------------------
705 def runTest(frame
, nb
, log
):
706 win
= TestPanel(nb
, log
, frame
)
709 #---------------------------------------------------------------------------
713 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.
715 Additional features include weekend highlighting and business type Monday-Sunday format.
717 See example for various methods used to set display month, year, and highlighted dates (different font and background colours).