]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/demo/wxCalendar.py
Added a demo showing how to use wxPostEvent
[wxWidgets.git] / utils / wxPython / demo / wxCalendar.py
1 #! /usr/local/bin/python
2 #----------------------------------------------------------------------------
3 # Name: CalendPanel.py
4 # Purpose: Calendar control display testing on panel
5 #
6 # Author: Lorne White (email: lwhite1@planet.eon.net)
7 #
8 # Created:
9 # Version 0.5 1999/11/03
10 # Licence: wxWindows license
11 #----------------------------------------------------------------------------
12
13 from wxPython.wx import *
14 from wxPython.lib.calendar import wxCalendar, Month
15
16
17 import os
18 dir_path = os.getcwd()
19
20
21 # highlighted days in month
22
23 test_days ={ 0: [],
24 1: [3, 7, 9, 21],
25 2: [2, 10, 4, 9],
26 3: [4, 20, 29],
27 4: [1, 12, 22],
28 5: [2, 10, 15],
29 6: [4, 8, 17],
30 7: [6, 7, 8],
31 8: [5, 10, 20],
32 9: [1, 2, 5, 29],
33 10: [2, 4, 6, 22],
34 11: [6, 9, 12, 28, 29],
35 12: [8, 9, 10, 11, 20] }
36
37 # test of full window calendar control functions
38
39 def GetMonthList():
40 monthlist = []
41 for i in range(13):
42 name = Month[i]
43 if name != None:
44 monthlist.append(name)
45 return monthlist
46
47 class TestPanel(wxPanel):
48 def __init__(self, parent, log):
49 wxPanel.__init__(self, parent, -1)
50
51 self.log = log
52
53 self.calend = wxCalendar(self, -1, wxPoint(100, 50), wxSize(200, 180))
54
55 start_month = 11
56 start_year = 1999
57
58 # month list from DateTime module
59
60 monthlist = GetMonthList()
61
62 self.date = wxComboBox(self, 10, Month[start_month], wxPoint(100, 20), wxSize(90, -1), monthlist, wxCB_DROPDOWN)
63 EVT_COMBOBOX(self, 10, self.EvtComboBox)
64
65 # set start month and year
66
67 self.calend.SetMonth(start_month)
68 self.calend.SetYear(start_year)
69
70 # set attributes of calendar
71
72 self.calend.HideTitle()
73 self.calend.HideGrid()
74
75 # display routine
76
77 self.ResetDisplay()
78
79 # mouse click event
80
81 self.Connect(self.calend.GetId(), -1, 2100, self.MouseClick)
82
83 # scroll bar for month selection
84
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)
88
89 # spin control for year selection
90
91 self.dtext = wxTextCtrl(self, -1, str(start_year), wxPoint(200, 20), wxSize(60, -1))
92 h = self.dtext.GetSize().height
93
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)
98
99 # button for calendar dialog test
100
101 wxStaticText(self, -1, "Test Calendar Dialog", wxPoint(350, 50)).SetBackgroundColour(wxNamedColour('Red'))
102
103 bmp = wxBitmap('Calbmp/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)
106
107 # button for calendar window test
108
109 wxStaticText(self, -1, "Test Calendar Window", wxPoint(350, 150)).SetBackgroundColour(wxNamedColour('Blue'))
110
111 bmp = wxBitmap('Calbmp/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)
114
115 # calendar dialog
116
117 def TestDlg(self, event):
118 dlg = CalenDlg(self, self.log)
119 dlg.Centre()
120 dlg.ShowModal()
121 dlg.Destroy()
122
123 # calendar window test
124
125 def TestFrame(self, event):
126 frame = CalendFrame(self, -1, "Test Calendar", self.log)
127 frame.Show(true)
128 return true
129
130 # month and year control events
131
132 def OnSpin(self, event):
133 year = event.GetPosition()
134 self.dtext.SetValue(str(year))
135 self.calend.SetYear(year)
136 self.calend.Refresh()
137
138 def EvtComboBox(self, event):
139 name = event.GetString()
140 self.log.WriteText('EvtComboBox: %s\n' % name)
141 monthval = self.date.FindString(name)
142 self.scroll.SetScrollbar(monthval, 1, 12, 1, TRUE)
143
144 self.calend.SetMonth(monthval+1)
145 self.ResetDisplay()
146
147 def Scroll(self, event):
148 value = self.scroll.GetThumbPosition()
149 monthval = int(value)+1
150 self.calend.SetMonth(monthval)
151 self.ResetDisplay()
152 self.log.WriteText('Month: %s\n' % value)
153
154 name = Month[monthval]
155 self.date.SetValue(name)
156
157 # log mouse events
158
159 def MouseClick(self, evt):
160 text = '%s CLICK %02d/%02d/%d' % (evt.click, evt.day, evt.month, evt.year) # format date
161 self.log.WriteText('Date Selected: ' + text + '\n')
162
163 def OnCloseWindow(self, event):
164 self.Destroy()
165
166 # set the highlighted days for the calendar
167
168 def ResetDisplay(self):
169 month = self.calend.GetMonth()
170 try:
171 set_days = test_days[month]
172 except:
173 set_days = [1, 5, 12]
174
175 self.calend.SetSelDay(set_days)
176 self.calend.Refresh()
177
178 # increment and decrement toolbar controls
179
180 def OnIncYear(self, event):
181 self.calend.IncYear()
182 self.ResetDisplay()
183
184 def OnDecYear(self, event):
185 self.calend.DecYear()
186 self.ResetDisplay()
187
188 def OnIncMonth(self, event):
189 self.calend.IncMonth()
190 self.ResetDisplay()
191
192 def OnDecMonth(self, event):
193 self.calend.DecMonth()
194 self.ResetDisplay()
195
196 def OnCurrent(self, event):
197 self.calend.SetCurrentDay()
198 self.ResetDisplay()
199
200 # test the calendar control in a dialog
201
202 class CalenDlg(wxDialog):
203 def __init__(self, parent, log):
204 self.log = log
205 wxDialog.__init__(self, parent, -1, "Test Calendar", wxPyDefaultPosition, wxSize(280, 300))
206
207 start_month = 2
208 start_year = 1999
209
210 # get month list from DateTime
211
212 monthlist = GetMonthList()
213
214 # select the month
215
216 self.date = wxComboBox(self, 100, Month[start_month], wxPoint(20, 20), wxSize(90, -1), monthlist, wxCB_DROPDOWN)
217 EVT_COMBOBOX(self, 100, self.EvtComboBox)
218
219 # alternate spin button to control the month
220
221 h = self.date.GetSize().height
222 self.m_spin = wxSpinButton(self, 120, wxPoint(130, 20), wxSize(h*2, h), wxSP_VERTICAL)
223 self.m_spin.SetRange(1, 12)
224 self.m_spin.SetValue(start_month)
225
226 EVT_SPIN(self, 120, self.OnMonthSpin)
227
228 # spin button to conrol the year
229
230 self.dtext = wxTextCtrl(self, -1, str(start_year), wxPoint(160, 20), wxSize(60, -1))
231 h = self.dtext.GetSize().height
232
233 self.y_spin = wxSpinButton(self, 20, wxPoint(220, 20), wxSize(h*2, h), wxSP_VERTICAL)
234 self.y_spin.SetRange(1980, 2010)
235 self.y_spin.SetValue(start_year)
236
237 EVT_SPIN(self, 20, self.OnYrSpin)
238
239 # set the calendar and attributes
240
241 self.calend = wxCalendar(self, -1, wxPoint(20, 60), wxSize(240, 200))
242 self.calend.SetMonth(start_month)
243 self.calend.SetYear(start_year)
244
245 self.calend.HideTitle()
246 self.ResetDisplay()
247
248 self.Connect(self.calend.GetId(), -1, 2100, self.MouseClick)
249
250 # log the mouse clicks
251
252 def MouseClick(self, evt):
253 text = '%s CLICK %02d/%02d/%d' % (evt.click, evt.day, evt.month, evt.year) # format date
254 self.log.WriteText('Date Selected: ' + text + '\n')
255
256 if evt.click == 'DLEFT':
257 self.EndModal(wxID_OK)
258
259 # month and year spin selection routines
260
261 def OnMonthSpin(self, event):
262 month = event.GetPosition()
263 self.date.SetValue(Month[month])
264 self.calend.SetMonth(month)
265 self.calend.Refresh()
266
267 def OnYrSpin(self, event):
268 year = event.GetPosition()
269 self.dtext.SetValue(str(year))
270 self.calend.SetYear(year)
271 self.calend.Refresh()
272
273 def EvtComboBox(self, event):
274 name = event.GetString()
275 self.log.WriteText('EvtComboBox: %s\n' % name)
276 monthval = self.date.FindString(name)
277 self.m_spin.SetValue(monthval+1)
278
279 self.calend.SetMonth(monthval+1)
280 self.ResetDisplay()
281
282 # set the calendar for highlighted days
283
284 def ResetDisplay(self):
285 month = self.calend.GetMonth()
286 try:
287 set_days = test_days[month]
288 except:
289 set_days = [1, 5, 12]
290
291 self.calend.SetSelDay(set_days)
292 self.calend.Refresh()
293
294 # test of full window calendar control functions
295
296 class CalendFrame(wxFrame):
297 def __init__(self, parent, id, title, log):
298 wxFrame.__init__(self, parent, id, title, wxPyDefaultPosition, wxSize(400, 400))
299
300 self.log = log
301 self.CreateStatusBar()
302 self.mainmenu = wxMenuBar()
303 menu = wxMenu()
304
305 menu = self.MakeFileMenu()
306 self.mainmenu.Append(menu, '&File')
307
308 self.MakeToolMenu() # toolbar
309
310 self.SetMenuBar(self.mainmenu)
311 self.calend = wxCalendar(self, -1)
312 self.calend.SetCurrentDay()
313 self.calend.grid_color = 'BLUE'
314 self.ResetDisplay()
315
316 self.Connect(self.calend.GetId(), -1, 2100, self.MouseClick)
317
318 def MouseClick(self, evt):
319 text = '%s CLICK %02d/%02d/%d' % (evt.click, evt.day, evt.month, evt.year) # format date
320 self.log.WriteText('Date Selected: ' + text + '\n')
321
322 def OnCloseWindow(self, event):
323 self.Destroy()
324
325 def ResetDisplay(self):
326 month = self.calend.GetMonth()
327 try:
328 set_days = test_days[month]
329 except:
330 set_days = [1, 5, 12]
331
332 self.calend.SetSelDay(set_days)
333 self.calend.Refresh()
334
335 def OnIncYear(self, event):
336 self.calend.IncYear()
337 self.ResetDisplay()
338
339 def OnDecYear(self, event):
340 self.calend.DecYear()
341 self.ResetDisplay()
342
343 def OnIncMonth(self, event):
344 self.calend.IncMonth()
345 self.ResetDisplay()
346
347 def OnDecMonth(self, event):
348 self.calend.DecMonth()
349 self.ResetDisplay()
350
351 def OnCurrent(self, event):
352 self.calend.SetCurrentDay()
353 self.ResetDisplay()
354
355 def MakeFileMenu(self):
356 menu = wxMenu()
357
358 mID = NewId()
359 menu.Append(mID, 'Decrement', 'Next')
360 EVT_MENU(self, mID, self.OnDecMonth)
361
362 mID = NewId()
363 menu.Append(mID, 'Increment', 'Dec')
364 EVT_MENU(self, mID, self.OnIncMonth)
365
366 menu.AppendSeparator()
367
368 mID = NewId()
369 menu.Append(mID, 'E&xit', 'Exit')
370 EVT_MENU(self, mID, self.OnCloseWindow)
371
372 return menu
373
374 def MakeToolMenu(self):
375 tb = self.CreateToolBar(wxTB_HORIZONTAL|wxNO_BORDER)
376
377 bmp_path = 'Calbmp/'
378 SetToolPath(self, tb, 10, bmp_path + 'DbDec.bmp', 'Dec Year')
379 EVT_TOOL(self, 10, self.OnDecYear)
380
381 SetToolPath(self, tb, 15, bmp_path + 'Dec.bmp', 'Dec Month')
382 EVT_TOOL(self, 15, self.OnDecMonth)
383
384 SetToolPath(self, tb, 30, bmp_path + 'Pt.bmp', 'Current Month')
385 EVT_TOOL(self, 30, self.OnCurrent)
386
387 SetToolPath(self, tb, 40, bmp_path + 'Inc.bmp', 'Inc Month')
388 EVT_TOOL(self, 40, self.OnIncMonth)
389
390 SetToolPath(self, tb, 45, bmp_path + 'DbInc.bmp', 'Inc Year')
391 EVT_TOOL(self, 45, self.OnIncYear)
392
393 tb.Realize()
394
395 def SetToolPath(self, tb, id, bmp, title):
396 global dir_path
397 tb.AddTool(id, wxBitmap(os.path.join(dir_path, bmp), wxBITMAP_TYPE_BMP), wxNullBitmap, false, -1, -1, title, title)
398
399
400 class MyApp(wxApp):
401 def OnInit(self):
402 frame = CalendFrame(NULL, -1, "Test Calendar")
403 frame.Show(true)
404 self.SetTopWindow(frame)
405 return true
406
407 #---------------------------------------------------------------------------
408
409 def MessageDlg(self, message, type = 'Message'):
410 dlg = wxMessageDialog(self, message, type, wxOK | wxICON_INFORMATION)
411 dlg.ShowModal()
412 dlg.Destroy()
413
414 #---------------------------------------------------------------------------
415
416 def main():
417 app = MyApp(0)
418 app.MainLoop()
419
420
421 if __name__ == '__main__':
422 main()
423
424
425 #---------------------------------------------------------------------------
426
427 def runTest(frame, nb, log):
428 win = TestPanel(nb, log)
429 return win
430
431 #---------------------------------------------------------------------------
432
433
434 overview = """\
435 This control provides a calendar control class for displaying and selecting dates.
436
437 See example for various methods used to set display month, year, and highlighted dates (different colour).
438
439 by Lorne White
440
441 """