| 1 | |
| 2 | import wx |
| 3 | import wx.lib.popupctl as pop |
| 4 | import wx.calendar as cal |
| 5 | |
| 6 | class TestDateControl(pop.PopupControl): |
| 7 | def __init__(self,*_args,**_kwargs): |
| 8 | apply(pop.PopupControl.__init__,(self,) + _args,_kwargs) |
| 9 | |
| 10 | self.win = wx.Window(self,-1,pos = (0,0),style = 0) |
| 11 | self.cal = cal.CalendarCtrl(self.win,-1,pos = (0,0)) |
| 12 | |
| 13 | bz = self.cal.GetBestSize() |
| 14 | self.win.SetSize(bz) |
| 15 | |
| 16 | # This method is needed to set the contents that will be displayed |
| 17 | # in the popup |
| 18 | self.SetPopupContent(self.win) |
| 19 | |
| 20 | # Event registration for date selection |
| 21 | self.cal.Bind(cal.EVT_CALENDAR_DAY, self.OnCalSelected) |
| 22 | |
| 23 | # Method called when a day is selected in the calendar |
| 24 | def OnCalSelected(self,evt): |
| 25 | self.PopDown() |
| 26 | date = self.cal.GetDate() |
| 27 | |
| 28 | # Format the date that was selected for the text part of the control |
| 29 | self.SetValue('%02d/%02d/%04d' % (date.GetDay(), |
| 30 | date.GetMonth()+1, |
| 31 | date.GetYear())) |
| 32 | evt.Skip() |
| 33 | |
| 34 | # Method overridden from PopupControl |
| 35 | # This method is called just before the popup is displayed |
| 36 | # Use this method to format any controls in the popup |
| 37 | def FormatContent(self): |
| 38 | # I parse the value in the text part to resemble the correct date in |
| 39 | # the calendar control |
| 40 | txtValue = self.GetValue() |
| 41 | dmy = txtValue.split('/') |
| 42 | didSet = False |
| 43 | |
| 44 | if len(dmy) == 3: |
| 45 | date = self.cal.GetDate() |
| 46 | d = int(dmy[0]) |
| 47 | m = int(dmy[1]) - 1 |
| 48 | y = int(dmy[2]) |
| 49 | |
| 50 | if d > 0 and d < 31: |
| 51 | if m >= 0 and m < 12: |
| 52 | if y > 1000: |
| 53 | self.cal.SetDate(wx.DateTimeFromDMY(d,m,y)) |
| 54 | didSet = True |
| 55 | |
| 56 | if not didSet: |
| 57 | self.cal.SetDate(wx.DateTime_Today()) |
| 58 | |
| 59 | |
| 60 | #--------------------------------------------------------------------------- |
| 61 | |
| 62 | class TestPanel(wx.Panel): |
| 63 | def __init__(self, parent, log): |
| 64 | self.log = log |
| 65 | wx.Panel.__init__(self, parent, -1) |
| 66 | date = TestDateControl(self, -1, pos = (30,30), size = (100,22)) |
| 67 | |
| 68 | #---------------------------------------------------------------------- |
| 69 | |
| 70 | def runTest(frame, nb, log): |
| 71 | if wx.Platform == "__WXMAC__": |
| 72 | from Main import MessagePanel |
| 73 | win = MessagePanel(nb, 'This demo currently fails on the Mac.', |
| 74 | 'Sorry', wx.ICON_WARNING) |
| 75 | return win |
| 76 | else: |
| 77 | win = TestPanel(nb, log) |
| 78 | return win |
| 79 | |
| 80 | #---------------------------------------------------------------------- |
| 81 | |
| 82 | overview = """<html><body> |
| 83 | <h2><center>PopupControl</center></h2> |
| 84 | |
| 85 | PopupControl is a class that can display a value and has a button |
| 86 | that will popup another window similar to how a wx.ComboBox works. The |
| 87 | popup window can contain whatever is needed to edit the value. This |
| 88 | example uses a wx.CalendarCtrl. |
| 89 | |
| 90 | <p>Currently a wx.Dialog is used for the popup. Eventually a |
| 91 | wx.PopupWindow should be used... |
| 92 | |
| 93 | </body></html> |
| 94 | """ |
| 95 | |
| 96 | |
| 97 | if __name__ == '__main__': |
| 98 | import sys,os |
| 99 | import run |
| 100 | run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:]) |
| 101 | |