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