1 #! /usr/local/bin/python
2 #----------------------------------------------------------------------------
4 # Purpose: Calendar control display testing on panel
6 # Author: Lorne White (email: lwhite1@planet.eon.net)
9 # Version 0.5 1999/11/03
10 # Licence: wxWindows license
11 #----------------------------------------------------------------------------
13 from wxPython
.wx
import *
14 from wxPython
.lib
.calendar
import Calendar
, Month
, using_mxDateTime
17 dir_path
= os
.getcwd()
20 # highlighted days in month
33 11: [6, 9, 12, 28, 29],
34 12: [8, 9, 10, 11, 20] }
36 # test of full window calendar control functions
43 monthlist
.append(name
)
46 class TestPanel(wxPanel
):
47 def __init__(self
, parent
, log
):
48 wxPanel
.__init
__(self
, parent
, -1)
52 if using_mxDateTime
is true
:
53 self
.log
.WriteText('Using mxDateTime module\n')
55 self
.log
.WriteText('Using Built in CDate module\n')
57 self
.calend
= Calendar(self
, -1, wxPoint(100, 50), wxSize(200, 180))
62 # month list from DateTime module
64 monthlist
= GetMonthList()
66 self
.date
= wxComboBox(self
, 10, Month
[start_month
], wxPoint(100, 20), wxSize(90, -1), monthlist
, wxCB_DROPDOWN
)
67 EVT_COMBOBOX(self
, 10, self
.EvtComboBox
)
69 # set start month and year
71 self
.calend
.SetMonth(start_month
)
72 self
.calend
.SetYear(start_year
)
74 # set attributes of calendar
76 self
.calend
.HideTitle()
77 self
.calend
.HideGrid()
85 self
.Connect(self
.calend
.GetId(), -1, 2100, self
.MouseClick
)
87 # scroll bar for month selection
89 self
.scroll
= wxScrollBar(self
, 40, wxPoint(100, 240), wxSize(200, 20), wxSB_HORIZONTAL
)
90 self
.scroll
.SetScrollbar(start_month
-1, 1, 12, 1, TRUE
)
91 EVT_COMMAND_SCROLL(self
, 40, self
.Scroll
)
93 # spin control for year selection
95 self
.dtext
= wxTextCtrl(self
, -1, str(start_year
), wxPoint(200, 20), wxSize(60, -1))
96 h
= self
.dtext
.GetSize().height
98 self
.spin
= wxSpinButton(self
, 20, wxPoint(270, 20), wxSize(h
*2, h
))
99 self
.spin
.SetRange(1980, 2010)
100 self
.spin
.SetValue(start_year
)
101 EVT_SPIN(self
, 20, self
.OnSpin
)
103 # button for calendar dialog test
105 wxStaticText(self
, -1, "Test Calendar Dialog", wxPoint(350, 50)).SetBackgroundColour(wxNamedColour('Red'))
107 bmp
= wxBitmap('CalBmp/Calend.bmp', wxBITMAP_TYPE_BMP
)
108 self
.but
= wxBitmapButton(self
, 60, bmp
, wxPoint(380, 80), wxSize(30, 30))
109 EVT_BUTTON(self
, 60, self
.TestDlg
)
111 # button for calendar window test
113 wxStaticText(self
, -1, "Test Calendar Window", wxPoint(350, 150)).SetBackgroundColour(wxNamedColour('Blue'))
115 bmp
= wxBitmap('CalBmp/Calend.bmp', wxBITMAP_TYPE_BMP
)
116 self
.but
= wxBitmapButton(self
, 160, bmp
, wxPoint(380, 180), wxSize(30, 30))
117 EVT_BUTTON(self
, 160, self
.TestFrame
)
121 def TestDlg(self
, event
):
122 dlg
= CalenDlg(self
, self
.log
)
127 # calendar window test
129 def TestFrame(self
, event
):
130 frame
= CalendFrame(NULL
, -1, "Test Calendar", self
.log
)
132 self
.SetTopWindow(frame
)
135 # month and year control events
137 def OnSpin(self
, event
):
138 year
= event
.GetPosition()
139 self
.dtext
.SetValue(str(year
))
140 self
.calend
.SetYear(year
)
141 self
.calend
.Refresh()
143 def EvtComboBox(self
, event
):
144 name
= event
.GetString()
145 self
.log
.WriteText('EvtComboBox: %s\n' % name
)
146 monthval
= self
.date
.FindString(name
)
147 self
.scroll
.SetScrollbar(monthval
, 1, 12, 1, TRUE
)
149 self
.calend
.SetMonth(monthval
+1)
152 def Scroll(self
, event
):
153 value
= self
.scroll
.GetThumbPosition()
154 monthval
= int(value
)+1
155 self
.calend
.SetMonth(monthval
)
157 self
.log
.WriteText('Month: %s\n' % value
)
159 name
= Month
[monthval
]
160 self
.date
.SetValue(name
)
164 def MouseClick(self
, evt
):
165 text
= '%s CLICK %02d/%02d/%d' % (evt
.click
, evt
.day
, evt
.month
, evt
.year
) # format date
166 self
.log
.WriteText('Date Selected: ' + text
+ '\n')
168 def OnCloseWindow(self
, event
):
171 # set the highlighted days for the calendar
173 def ResetDisplay(self
):
174 month
= self
.calend
.GetMonth()
176 set_days
= test_days
[month
]
178 set_days
= [1, 5, 12]
180 self
.calend
.SetSelDay(set_days
)
181 self
.calend
.Refresh()
183 # increment and decrement toolbar controls
185 def OnIncYear(self
, event
):
186 self
.calend
.IncYear()
189 def OnDecYear(self
, event
):
190 self
.calend
.DecYear()
193 def OnIncMonth(self
, event
):
194 self
.calend
.IncMonth()
197 def OnDecMonth(self
, event
):
198 self
.calend
.DecMonth()
201 def OnCurrent(self
, event
):
202 self
.calend
.SetCurrentDay()
205 # test the calendar control in a dialog
207 class CalenDlg(wxDialog
):
208 def __init__(self
, parent
, log
):
210 wxDialog
.__init
__(self
, parent
, -1, "Test Calendar", wxPyDefaultPosition
, wxSize(280, 300))
215 # get month list from DateTime
217 monthlist
= GetMonthList()
221 self
.date
= wxComboBox(self
, 100, Month
[start_month
], wxPoint(20, 20), wxSize(90, -1), monthlist
, wxCB_DROPDOWN
)
222 EVT_COMBOBOX(self
, 100, self
.EvtComboBox
)
224 # alternate spin button to control the month
226 h
= self
.date
.GetSize().height
227 self
.m_spin
= wxSpinButton(self
, 120, wxPoint(130, 20), wxSize(h
*2, h
), wxSP_VERTICAL
)
228 self
.m_spin
.SetRange(1, 12)
229 self
.m_spin
.SetValue(start_month
)
231 EVT_SPIN(self
, 120, self
.OnMonthSpin
)
233 # spin button to conrol the year
235 self
.dtext
= wxTextCtrl(self
, -1, str(start_year
), wxPoint(160, 20), wxSize(60, -1))
236 h
= self
.dtext
.GetSize().height
238 self
.y_spin
= wxSpinButton(self
, 20, wxPoint(220, 20), wxSize(h
*2, h
), wxSP_VERTICAL
)
239 self
.y_spin
.SetRange(1980, 2010)
240 self
.y_spin
.SetValue(start_year
)
242 EVT_SPIN(self
, 20, self
.OnYrSpin
)
244 # set the calendar and attributes
246 self
.calend
= Calendar(self
, -1, wxPoint(20, 60), wxSize(240, 200))
247 self
.calend
.SetMonth(start_month
)
248 self
.calend
.SetYear(start_year
)
250 self
.calend
.HideTitle()
253 self
.Connect(self
.calend
.GetId(), -1, 2100, self
.MouseClick
)
255 # log the mouse clicks
257 def MouseClick(self
, evt
):
258 text
= '%s CLICK %02d/%02d/%d' % (evt
.click
, evt
.day
, evt
.month
, evt
.year
) # format date
259 self
.log
.WriteText('Date Selected: ' + text
+ '\n')
261 if evt
.click
== 'DLEFT':
262 self
.EndModal(wxID_OK
)
264 # month and year spin selection routines
266 def OnMonthSpin(self
, event
):
267 month
= event
.GetPosition()
268 self
.date
.SetValue(Month
[month
])
269 self
.calend
.SetMonth(month
)
270 self
.calend
.Refresh()
272 def OnYrSpin(self
, event
):
273 year
= event
.GetPosition()
274 self
.dtext
.SetValue(str(year
))
275 self
.calend
.SetYear(year
)
276 self
.calend
.Refresh()
278 def EvtComboBox(self
, event
):
279 name
= event
.GetString()
280 self
.log
.WriteText('EvtComboBox: %s\n' % name
)
281 monthval
= self
.date
.FindString(name
)
282 self
.m_spin
.SetValue(monthval
+1)
284 self
.calend
.SetMonth(monthval
+1)
287 # set the calendar for highlighted days
289 def ResetDisplay(self
):
290 month
= self
.calend
.GetMonth()
292 set_days
= test_days
[month
]
294 set_days
= [1, 5, 12]
296 self
.calend
.SetSelDay(set_days
)
297 self
.calend
.Refresh()
299 # test of full window calendar control functions
301 class CalendFrame(wxFrame
):
302 def __init__(self
, parent
, id, title
, log
):
303 wxFrame
.__init
__(self
, parent
, id, title
, wxPyDefaultPosition
, wxSize(400, 400))
306 self
.CreateStatusBar()
307 self
.mainmenu
= wxMenuBar()
310 menu
= self
.MakeFileMenu()
311 self
.mainmenu
.Append(menu
, '&File')
313 self
.MakeToolMenu() # toolbar
315 self
.SetMenuBar(self
.mainmenu
)
316 self
.calend
= Calendar(self
, -1)
317 self
.calend
.SetCurrentDay()
318 self
.calend
.grid_color
= 'BLUE'
321 self
.Connect(self
.calend
.GetId(), -1, 2100, self
.MouseClick
)
323 def MouseClick(self
, evt
):
324 text
= '%s CLICK %02d/%02d/%d' % (evt
.click
, evt
.day
, evt
.month
, evt
.year
) # format date
325 self
.log
.WriteText('Date Selected: ' + text
+ '\n')
327 def OnCloseWindow(self
, event
):
330 def ResetDisplay(self
):
331 month
= self
.calend
.GetMonth()
333 set_days
= test_days
[month
]
335 set_days
= [1, 5, 12]
337 self
.calend
.SetSelDay(set_days
)
338 self
.calend
.Refresh()
340 def OnIncYear(self
, event
):
341 self
.calend
.IncYear()
344 def OnDecYear(self
, event
):
345 self
.calend
.DecYear()
348 def OnIncMonth(self
, event
):
349 self
.calend
.IncMonth()
352 def OnDecMonth(self
, event
):
353 self
.calend
.DecMonth()
356 def OnCurrent(self
, event
):
357 self
.calend
.SetCurrentDay()
360 def MakeFileMenu(self
):
364 menu
.Append(mID
, 'Decrement', 'Next')
365 EVT_MENU(self
, mID
, self
.OnDecMonth
)
368 menu
.Append(mID
, 'Increment', 'Dec')
369 EVT_MENU(self
, mID
, self
.OnIncMonth
)
371 menu
.AppendSeparator()
374 menu
.Append(mID
, 'E&xit', 'Exit')
375 EVT_MENU(self
, mID
, self
.OnCloseWindow
)
379 def MakeToolMenu(self
):
380 tb
= self
.CreateToolBar(wxTB_HORIZONTAL|wxNO_BORDER
)
383 SetToolPath(self
, tb
, 10, bmp_path
+ 'DbDec.bmp', 'Dec Year')
384 EVT_TOOL(self
, 10, self
.OnDecYear
)
386 SetToolPath(self
, tb
, 15, bmp_path
+ 'Dec.bmp', 'Dec Month')
387 EVT_TOOL(self
, 15, self
.OnDecMonth
)
389 SetToolPath(self
, tb
, 30, bmp_path
+ 'Pt.bmp', 'Current Month')
390 EVT_TOOL(self
, 30, self
.OnCurrent
)
392 SetToolPath(self
, tb
, 40, bmp_path
+ 'Inc.bmp', 'Inc Month')
393 EVT_TOOL(self
, 40, self
.OnIncMonth
)
395 SetToolPath(self
, tb
, 45, bmp_path
+ 'DbInc.bmp', 'Inc Year')
396 EVT_TOOL(self
, 45, self
.OnIncYear
)
400 def SetToolPath(self
, tb
, id, bmp
, title
):
402 tb
.AddTool(id, wxBitmap(os
.path
.join(dir_path
, bmp
), wxBITMAP_TYPE_BMP
), wxNullBitmap
, false
, -1, -1, title
, title
)
406 frame
= CalendFrame(NULL
, -1, "Test Calendar")
408 self
.SetTopWindow(frame
)
411 #---------------------------------------------------------------------------
413 def MessageDlg(self
, message
, type = 'Message'):
414 dlg
= wxMessageDialog(self
, message
, type, wxOK | wxICON_INFORMATION
)
418 #---------------------------------------------------------------------------
425 if __name__
== '__main__':
429 #---------------------------------------------------------------------------
431 def runTest(frame
, nb
, log
):
432 win
= TestPanel(nb
, log
)
435 #---------------------------------------------------------------------------
439 This control provides a calendar control class for displaying and selecting dates.
441 See example for various methods used to set display month, year, and highlighted dates (different colour).