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 
 125         self
.but1 
= wx
.Button(self
, -1, "Test Calendar Dialog", (380, 80)) 
 126         self
.Bind(wx
.EVT_BUTTON
, self
.TestDlg
, self
.but1
) 
 128         # button for calendar window test 
 129         self
.but2 
= wx
.Button(self
, -1, "Test Calendar Window", (380, 180)) 
 130         self
.Bind(wx
.EVT_BUTTON
, self
.TestFrame
, self
.but2
) 
 132         self
.but3 
= wx
.Button(self
, -1, "Test Calendar Print", (380, 280)) 
 133         self
.Bind(wx
.EVT_BUTTON
, self
.OnPreview
, self
.but3
) 
 137     def TestDlg(self
, event
):       # test the date dialog 
 138         dlg 
= wx
.lib
.calendar
.CalenDlg(self
) 
 141         if dlg
.ShowModal() == wx
.ID_OK
: 
 146             new_date 
= str(month
) + '/'+ str(day
) + '/'+ str(year
) 
 147             self
.log
.WriteText('Date Selected: %s\n' % new_date
) 
 149             self
.log
.WriteText('No Date Selected') 
 151         # calendar window test 
 153     def TestFrame(self
, event
): 
 154         frame 
= CalendFrame(self
, -1, "Test Calendar", self
.log
) 
 158     # calendar print preview 
 160     def OnPreview(self
, event
): 
 161         month 
= self
.calend
.GetMonth() 
 162         year 
= self
.calend
.GetYear() 
 164         prt 
= PrintCalend(self
.frame
, month
, year
) 
 167     # month and year control events 
 169     def OnSpin(self
, event
): 
 170         year 
= event
.GetPosition() 
 171         self
.dtext
.SetValue(str(year
)) 
 172         self
.calend
.SetYear(year
) 
 173         self
.calend
.Refresh() 
 175     def EvtComboBox(self
, event
): 
 176         name 
= event
.GetString() 
 177         self
.log
.WriteText('EvtComboBox: %s\n' % name
) 
 178         monthval 
= self
.date
.FindString(name
) 
 179         self
.scroll
.SetScrollbar(monthval
, 1, 12, 1, True) 
 181         self
.calend
.SetMonth(monthval
+1) 
 184     def Scroll(self
, event
): 
 185         value 
= self
.scroll
.GetThumbPosition() 
 186         monthval 
= int(value
)+1 
 187         self
.calend
.SetMonth(monthval
) 
 189         self
.log
.WriteText('Month: %s\n' % value
) 
 191         name 
= wx
.lib
.calendar
.Month
[monthval
] 
 192         self
.date
.SetValue(name
) 
 196     def MouseClick(self
, evt
): 
 197         text 
= '%s CLICK   %02d/%02d/%d' % (evt
.click
, evt
.day
, evt
.month
, evt
.year
)  # format date 
 198         self
.log
.WriteText('Date Selected: ' + text 
+ '\n') 
 201     # set the highlighted days for the calendar 
 203     def ResetDisplay(self
): 
 204         month 
= self
.calend
.GetMonth() 
 207             set_days 
= test_days
[month
] 
 209             set_days 
= [1, 5, 12] 
 211         self
.calend
.AddSelect([4, 11], 'BLUE', 'WHITE') 
 212         self
.calend
.SetSelDay(set_days
) 
 213         self
.calend
.Refresh() 
 215     # increment and decrement toolbar controls 
 217     def OnIncYear(self
, event
): 
 218         self
.calend
.IncYear() 
 221     def OnDecYear(self
, event
): 
 222         self
.calend
.DecYear() 
 225     def OnIncMonth(self
, event
): 
 226         self
.calend
.IncMonth() 
 229     def OnDecMonth(self
, event
): 
 230         self
.calend
.DecMonth() 
 233     def OnCurrent(self
, event
): 
 234         self
.calend
.SetCurrentDay() 
 237 # test of full window calendar control functions 
 239 class CalendFrame(wx
.Frame
): 
 240     def __init__(self
, parent
, id, title
, log
): 
 241         wx
.Frame
.__init
__(self
, parent
, id, title
, size
=(400, 400), 
 242                          style
=wx
.DEFAULT_FRAME_STYLE|wx
.NO_FULL_REPAINT_ON_RESIZE
) 
 244         self
.Bind(wx
.EVT_CLOSE
, self
.OnCloseWindow
) 
 247         self
.CreateStatusBar() 
 248         self
.mainmenu 
= wx
.MenuBar() 
 251         menu 
= self
.MakeFileMenu() 
 252         self
.mainmenu
.Append(menu
, '&File') 
 254         self
.MakeToolMenu()             # toolbar 
 256         self
.SetMenuBar(self
.mainmenu
) 
 257         self
.calend 
= wx
.lib
.calendar
.Calendar(self
, -1) 
 258         self
.calend
.SetCurrentDay() 
 259         self
.calend
.grid_color 
= 'BLUE' 
 260         self
.calend
.SetBusType() 
 261 #        self.calend.ShowWeekEnd() 
 265         self
.Bind(wx
.lib
.calendar
.EVT_CALENDAR
, self
.MouseClick
, self
.calend
) 
 267     def MouseClick(self
, evt
): 
 268         text 
= '%s CLICK   %02d/%02d/%d' % (evt
.click
, evt
.day
, evt
.month
, evt
.year
)  # format date 
 269         self
.log
.WriteText('Date Selected: ' + text 
+ '\n') 
 271     def OnCloseWindow(self
, event
): 
 274     def ResetDisplay(self
): 
 275         month 
= self
.calend
.GetMonth() 
 278             set_days 
= test_days
[month
] 
 280             set_days 
= [1, 5, 12] 
 282         self
.calend
.AddSelect([2, 16], 'GREEN', 'WHITE') 
 284         self
.calend
.SetSelDay(set_days
) 
 285         self
.calend
.Refresh() 
 287     def OnIncYear(self
, event
): 
 288         self
.calend
.IncYear() 
 291     def OnDecYear(self
, event
): 
 292         self
.calend
.DecYear() 
 295     def OnIncMonth(self
, event
): 
 296         self
.calend
.IncMonth() 
 299     def OnDecMonth(self
, event
): 
 300         self
.calend
.DecMonth() 
 303     def OnCurrent(self
, event
): 
 304         self
.calend
.SetCurrentDay() 
 307     def MakeFileMenu(self
): 
 311         menu
.Append(mID
, 'Decrement', 'Next') 
 312         self
.Bind(wx
.EVT_MENU
, self
.OnDecMonth
, id=mID
) 
 315         menu
.Append(mID
, 'Increment', 'Dec') 
 316         self
.Bind(wx
.EVT_MENU
, self
.OnIncMonth
, id=mID
) 
 318         menu
.AppendSeparator() 
 321         menu
.Append(mID
, 'E&xit', 'Exit') 
 322         self
.Bind(wx
.EVT_MENU
, self
.OnCloseWindow
, id=mID
) 
 326     def MakeToolMenu(self
): 
 327         tb 
= self
.CreateToolBar(wx
.TB_HORIZONTAL|wx
.NO_BORDER
) 
 330         SetToolPath(self
, tb
, mID
, images
.getDbDecBitmap(), 'Dec Year') 
 331         self
.Bind(wx
.EVT_TOOL
, self
.OnDecYear
, id=mID
) 
 334         SetToolPath(self
, tb
, mID
, images
.getDecBitmap(), 'Dec Month') 
 335         self
.Bind(wx
.EVT_TOOL
, self
.OnDecMonth
, id=mID
) 
 338         SetToolPath(self
, tb
, mID
, images
.getPtBitmap(), 'Current Month') 
 339         self
.Bind(wx
.EVT_TOOL
, self
.OnCurrent
, id=mID
) 
 342         SetToolPath(self
, tb
, mID
, images
.getIncBitmap(), 'Inc Month') 
 343         self
.Bind(wx
.EVT_TOOL
, self
.OnIncMonth
, id=mID
) 
 346         SetToolPath(self
, tb
, mID
, images
.getDbIncBitmap(), 'Inc Year') 
 347         self
.Bind(wx
.EVT_TOOL
, self
.OnIncYear
, id=mID
) 
 351 #--------------------------------------------------------------------------- 
 353 # example class for printing/previewing calendars 
 356     def __init__(self
, parent
, month
, year
): 
 363         self
.printData 
= wx
.PrintData() 
 366         self
.grid_color 
= 'BLUE' 
 367         self
.back_color 
= 'WHITE' 
 368         self
.sel_color 
= 'RED' 
 369         self
.high_color 
= 'LIGHT BLUE' 
 371         self
.bold 
= wx
.NORMAL
 
 373         self
.sel_key 
= None      # last used by 
 374         self
.sel_lst 
= []        # highlighted selected days 
 377         self
.hide_title 
= False 
 378         self
.hide_grid 
= False 
 399     def SetDates(self
, month
, year
): 
 403     def SetStyleDef(self
, desc
): 
 406     def SetCopies(self
, copies
):        # number of copies of label 
 409     def SetStart(self
, start
):          # start position of label 
 413         printout 
= SetPrintout(self
) 
 414         printout2 
= SetPrintout(self
) 
 415         self
.preview 
= wx
.PrintPreview(printout
, printout2
, self
.printData
) 
 417         if not self
.preview
.Ok(): 
 418             wx
.MessageBox("There was a problem printing!", "Printing", wx
.OK
) 
 421         self
.preview
.SetZoom(60)        # initial zoom value 
 423         frame 
= wx
.PreviewFrame(self
.preview
, self
.frame
, "Print preview") 
 426         frame
.SetPosition(self
.frame
.GetPosition()) 
 427         frame
.SetSize(self
.frame
.GetSize()) 
 431         pdd 
= wx
.PrintDialogData() 
 432         pdd
.SetPrintData(self
.printData
) 
 433         printer 
= wx
.Printer(pdd
) 
 434         printout 
= SetPrintout(self
) 
 435         frame 
= wx
.Frame(None, -1, "Test") 
 437         if not printer
.Print(frame
, printout
): 
 438             wx
.MessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wx
.OK
) 
 440             self
.printData 
= printer
.GetPrintDialogData().GetPrintData() 
 444     def DoDrawing(self
, DC
): 
 448         cal 
= wx
.lib
.calendar
.PrtCalDraw(self
) 
 450         if self
.preview 
is None: 
 451             cal
.SetPSize(size
[0]/self
.pagew
, size
[1]/self
.pageh
) 
 452             cal
.SetPreview(False) 
 455             if self
.preview 
== 1: 
 456                 cal
.SetPSize(size
[0]/self
.pagew
, size
[1]/self
.pageh
) 
 458                 cal
.SetPSize(self
.pwidth
, self
.pheight
) 
 460             cal
.SetPreview(self
.preview
) 
 462         cal
.hide_title 
= self
.hide_title        
# set the calendar parameters 
 463         cal
.hide_grid 
= self
.hide_grid
 
 465         cal
.grid_color 
= self
.grid_color
 
 466         cal
.high_color 
= self
.high_color
 
 467         cal
.back_color 
= self
.back_color
 
 468         cal
.outer_border 
= False 
 472         cal_size 
= (3.0, 3.0) 
 473         cal
.SetSize(cal_size
) 
 475         year
, month 
= self
.year
, self
.month
 
 482                 cal
.SetCal(year
, month
)       # current month 
 486                     set_days 
= test_days
[month
] 
 488                     set_days 
= [1, 5, 12] 
 490                 cal
.AddSelect([2, 16], 'GREEN', 'WHITE') 
 492                 cal
.DrawCal(DC
, set_days
) 
 494                 year
, month 
= self
.IncMonth(year
, month
) 
 497             x 
= x 
+ 4.0     # next column 
 501         self
.ymax 
= DC
.MaxY() 
 502         self
.xmax 
= DC
.MaxX() 
 504     def IncMonth(self
, year
, month
):     # next month 
 513     def GetTotalPages(self
): 
 517     def SetPage(self
, page
): 
 520     def SetPageSize(self
, width
, height
): 
 521         self
.pwidth
, self
.pheight 
= width
, height
 
 523     def SetTotalSize(self
, width
, height
): 
 524         self
.ptwidth
, self
.ptheight 
= width
, height
 
 526     def SetPreview(self
, preview
, scale
): 
 527         self
.preview 
= preview
 
 530     def SetTotalSize(self
, width
, height
): 
 532         self
.ptheight 
= height
 
 534 def SetToolPath(self
, tb
, id, bmp
, title
): 
 535     tb
.AddSimpleTool(id, bmp
, title
, title
) 
 537 class SetPrintout(wx
.Printout
): 
 538     def __init__(self
, canvas
): 
 539         wx
.Printout
.__init
__(self
) 
 543     def OnBeginDocument(self
, start
, end
): 
 544         return super(SetPrintout
, self
).OnBeginDocument(start
, end
) 
 546     def OnEndDocument(self
): 
 547         super(SetPrintout
, self
).OnEndDocument() 
 549     def HasPage(self
, page
): 
 550         if page 
<= self
.end_pg
: 
 555     def GetPageInfo(self
): 
 556         self
.end_pg 
= self
.canvas
.GetTotalPages() 
 564         return (str_pg
, end_pg
, str_pg
, end_pg
) 
 566     def OnPreparePrinting(self
): 
 567         super(SetPrintout
, self
).OnPreparePrinting() 
 569     def OnBeginPrinting(self
): 
 572         self
.preview 
= self
.IsPreview() 
 575             self
.pixelsPerInch 
= self
.GetPPIScreen() 
 577             self
.pixelsPerInch 
= self
.GetPPIPrinter() 
 579         (w
, h
) = dc
.GetSize() 
 580         scaleX 
= float(w
) / 1000 
 581         scaleY 
= float(h
) / 1000 
 582         self
.printUserScale 
= min(scaleX
, scaleY
) 
 584         super(SetPrintout
, self
).OnBeginPrinting() 
 587         self
.psizew
, self
.psizeh 
= self
.GetPPIPrinter() 
 588         return self
.psizew
, self
.psizeh
 
 590     def GetTotalSize(self
): 
 591         self
.ptsizew
, self
.ptsizeh 
= self
.GetPageSizePixels() 
 592         return self
.ptsizew
, self
.ptsizeh
 
 594     def OnPrintPage(self
, page
): 
 596         (w
, h
) = dc
.GetSize() 
 597         scaleX 
= float(w
) / 1000 
 598         scaleY 
= float(h
) / 1000 
 599         self
.printUserScale 
= min(scaleX
, scaleY
) 
 600         dc
.SetUserScale(self
.printUserScale
, self
.printUserScale
) 
 602         self
.preview 
= self
.IsPreview() 
 604         self
.canvas
.SetPreview(self
.preview
, self
.printUserScale
) 
 605         self
.canvas
.SetPage(page
) 
 607         self
.ptsizew
, self
.ptsizeh 
= self
.GetPageSizePixels() 
 608         self
.canvas
.SetTotalSize(self
.ptsizew
, self
.ptsizeh
) 
 610         self
.psizew
, self
.psizeh 
= self
.GetPPIPrinter() 
 611         self
.canvas
.SetPageSize(self
.psizew
, self
.psizeh
) 
 613         self
.canvas
.DoDrawing(dc
) 
 618         frame 
= CalendFrame(None, -1, "Test Calendar", log
) 
 620         self
.SetTopWindow(frame
) 
 623 #--------------------------------------------------------------------------- 
 625 def MessageDlg(self
, message
, type = 'Message'): 
 626     dlg 
= wx
.MessageDialog(self
, message
, type, wx
.OK | wx
.ICON_INFORMATION
) 
 630 #--------------------------------------------------------------------------- 
 632 def runTest(frame
, nb
, log
): 
 633     win 
= TestPanel(nb
, log
, frame
) 
 636 #--------------------------------------------------------------------------- 
 640 This control provides a Calendar control class for displaying and selecting dates.   
 641 In addition, the class is extended and can be used for printing/previewing. 
 643 Additional features include weekend highlighting and business type Monday-Sunday  
 646 See example for various methods used to set display month, year, and highlighted  
 647 dates (different font and background colours). 
 656 if __name__ 
== '__main__': 
 659     run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])