]> git.saurik.com Git - wxWidgets.git/blame_incremental - wxPython/demo/wxCalendar.py
ignore CodeWarrior build directory
[wxWidgets.git] / wxPython / demo / wxCalendar.py
... / ...
CommitLineData
1#----------------------------------------------------------------------------
2# Name: wxCalendar.py
3# Purpose: Calendar control display testing on panel for wxPython demo
4#
5# Author: Lorne White (email: lwhite1@planet.eon.net)
6#
7# Created:
8# Version 0.8 2000/04/16
9# Licence: wxWindows license
10#----------------------------------------------------------------------------
11
12from wxPython.wx import *
13from wxPython.lib.calendar import wxCalendar, Month, PrtCalDraw
14
15import os
16dir_path = os.getcwd()
17
18
19# highlighted days in month
20
21test_days ={ 0: [],
22 1: [3, 7, 9, 21],
23 2: [2, 10, 4, 9],
24 3: [4, 20, 29],
25 4: [1, 12, 22],
26 5: [2, 10, 15],
27 6: [4, 8, 17],
28 7: [6, 7, 8],
29 8: [5, 10, 20],
30 9: [1, 2, 5, 29],
31 10: [2, 4, 6, 22],
32 11: [6, 9, 12, 28, 29],
33 12: [8, 9, 10, 11, 20] }
34
35# test of full window calendar control functions
36
37def GetMonthList():
38 monthlist = []
39 for i in range(13):
40 name = Month[i]
41 if name != None:
42 monthlist.append(name)
43 return monthlist
44
45class TestPanel(wxPanel):
46 def __init__(self, parent, log, frame):
47 wxPanel.__init__(self, parent, -1)
48
49 self.log = log
50 self.frame = frame
51
52 self.calend = wxCalendar(self, -1, wxPoint(100, 50), wxSize(200, 180))
53
54 start_month = 11
55 start_year = 1999
56
57# month list from DateTime module
58
59 monthlist = GetMonthList()
60
61 self.date = wxComboBox(self, 10, Month[start_month], wxPoint(100, 20), wxSize(90, -1), monthlist, wxCB_DROPDOWN)
62 EVT_COMBOBOX(self, 10, self.EvtComboBox)
63
64# set start month and year
65
66 self.calend.SetMonth(start_month)
67 self.calend.SetYear(start_year)
68
69# set attributes of calendar
70
71 self.calend.hide_title = TRUE
72 self.calend.HideGrid()
73 self.calend.SetWeekColor('WHITE', 'BLACK')
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), wxSize(150, -1))
102
103 bmp = wxBitmap('bitmaps/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), wxSize(150, -1))
110
111 bmp = wxBitmap('bitmaps/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 wxStaticText(self, -1, "Test Calendar Print", wxPoint(350, 250), wxSize(150, -1))
116
117 bmp = wxBitmap('bitmaps/Calend.bmp', wxBITMAP_TYPE_BMP)
118 self.but = wxBitmapButton(self, 170, bmp, wxPoint(380, 280))#, wxSize(30, 30))
119 EVT_BUTTON(self, 170, self.OnPreview)
120
121# calendar dialog
122
123 def TestDlg(self, event):
124 dlg = CalenDlg(self, self.log)
125 dlg.Centre()
126 dlg.ShowModal()
127 dlg.Destroy()
128
129# calendar window test
130
131 def TestFrame(self, event):
132 frame = CalendFrame(self, -1, "Test Calendar", self.log)
133 frame.Show(true)
134 return true
135
136# calendar print preview
137
138 def OnPreview(self, event):
139 month = self.calend.GetMonth()
140 year = self.calend.GetYear()
141
142 prt = PrintCalend(self.frame, month, year)
143 prt.Preview()
144
145# month and year control events
146
147 def OnSpin(self, event):
148 year = event.GetPosition()
149 self.dtext.SetValue(str(year))
150 self.calend.SetYear(year)
151 self.calend.Refresh()
152
153 def EvtComboBox(self, event):
154 name = event.GetString()
155 self.log.WriteText('EvtComboBox: %s\n' % name)
156 monthval = self.date.FindString(name)
157 self.scroll.SetScrollbar(monthval, 1, 12, 1, TRUE)
158
159 self.calend.SetMonth(monthval+1)
160 self.ResetDisplay()
161
162 def Scroll(self, event):
163 value = self.scroll.GetThumbPosition()
164 monthval = int(value)+1
165 self.calend.SetMonth(monthval)
166 self.ResetDisplay()
167 self.log.WriteText('Month: %s\n' % value)
168
169 name = Month[monthval]
170 self.date.SetValue(name)
171
172# log mouse events
173
174 def MouseClick(self, evt):
175 text = '%s CLICK %02d/%02d/%d' % (evt.click, evt.day, evt.month, evt.year) # format date
176 self.log.WriteText('Date Selected: ' + text + '\n')
177
178
179# set the highlighted days for the calendar
180
181 def ResetDisplay(self):
182 month = self.calend.GetMonth()
183 try:
184 set_days = test_days[month]
185 except:
186 set_days = [1, 5, 12]
187
188 self.calend.AddSelect([4, 11], 'BLUE', 'WHITE')
189 self.calend.SetSelDay(set_days)
190 self.calend.Refresh()
191
192# increment and decrement toolbar controls
193
194 def OnIncYear(self, event):
195 self.calend.IncYear()
196 self.ResetDisplay()
197
198 def OnDecYear(self, event):
199 self.calend.DecYear()
200 self.ResetDisplay()
201
202 def OnIncMonth(self, event):
203 self.calend.IncMonth()
204 self.ResetDisplay()
205
206 def OnDecMonth(self, event):
207 self.calend.DecMonth()
208 self.ResetDisplay()
209
210 def OnCurrent(self, event):
211 self.calend.SetCurrentDay()
212 self.ResetDisplay()
213
214# test the calendar control in a dialog
215
216class CalenDlg(wxDialog):
217 def __init__(self, parent, log):
218 self.log = log
219 wxDialog.__init__(self, parent, -1, "Test Calendar", wxPyDefaultPosition, wxSize(280, 300))
220
221 start_month = 2
222 start_year = 1999
223
224# get month list from DateTime
225
226 monthlist = GetMonthList()
227
228# select the month
229
230 self.date = wxComboBox(self, 100, Month[start_month], wxPoint(20, 20), wxSize(90, -1), monthlist, wxCB_DROPDOWN)
231 EVT_COMBOBOX(self, 100, self.EvtComboBox)
232
233# alternate spin button to control the month
234
235 h = self.date.GetSize().height
236 self.m_spin = wxSpinButton(self, 120, wxPoint(120, 20), wxSize(h*2, h), wxSP_VERTICAL)
237 self.m_spin.SetRange(1, 12)
238 self.m_spin.SetValue(start_month)
239
240 EVT_SPIN(self, 120, self.OnMonthSpin)
241
242# spin button to conrol the year
243
244 self.dtext = wxTextCtrl(self, -1, str(start_year), wxPoint(160, 20), wxSize(60, -1))
245 h = self.dtext.GetSize().height
246
247 self.y_spin = wxSpinButton(self, 20, wxPoint(220, 20), wxSize(h*2, h), wxSP_VERTICAL)
248 self.y_spin.SetRange(1980, 2010)
249 self.y_spin.SetValue(start_year)
250
251 EVT_SPIN(self, 20, self.OnYrSpin)
252
253# set the calendar and attributes
254
255 self.calend = wxCalendar(self, -1, wxPoint(20, 60), wxSize(240, 200))
256 self.calend.SetMonth(start_month)
257 self.calend.SetYear(start_year)
258
259 self.calend.HideTitle()
260 self.calend.ShowWeekEnd()
261
262 self.ResetDisplay()
263
264 self.Connect(self.calend.GetId(), -1, 2100, self.MouseClick)
265
266# log the mouse clicks
267
268 def MouseClick(self, evt):
269 text = '%s CLICK %02d/%02d/%d' % (evt.click, evt.day, evt.month, evt.year) # format date
270 self.log.WriteText('Date Selected: ' + text + '\n')
271
272 if evt.click == 'DLEFT':
273 self.EndModal(wxID_OK)
274
275# month and year spin selection routines
276
277 def OnMonthSpin(self, event):
278 month = event.GetPosition()
279 if month >= 0 and month <= 12:
280 self.date.SetValue(Month[month])
281 self.calend.SetMonth(month)
282 self.calend.Refresh()
283
284 def OnYrSpin(self, event):
285 year = event.GetPosition()
286 self.dtext.SetValue(str(year))
287 self.calend.SetYear(year)
288 self.calend.Refresh()
289
290 def EvtComboBox(self, event):
291 name = event.GetString()
292 self.log.WriteText('EvtComboBox: %s\n' % name)
293 monthval = self.date.FindString(name)
294 self.m_spin.SetValue(monthval+1)
295
296 self.calend.SetMonth(monthval+1)
297 self.ResetDisplay()
298
299# set the calendar for highlighted days
300
301 def ResetDisplay(self):
302 month = self.calend.GetMonth()
303 try:
304 set_days = test_days[month]
305 except:
306 set_days = [1, 5, 12]
307
308 self.calend.AddSelect([4, 11], 'BLUE', 'WHITE')
309
310 self.calend.SetSelDay(set_days)
311 self.calend.Refresh()
312
313# test of full window calendar control functions
314
315class CalendFrame(wxFrame):
316 def __init__(self, parent, id, title, log):
317 wxFrame.__init__(self, parent, id, title, wxPyDefaultPosition, wxSize(400, 400))
318 EVT_CLOSE(self, self.OnCloseWindow)
319
320 self.log = log
321 self.CreateStatusBar()
322 self.mainmenu = wxMenuBar()
323 menu = wxMenu()
324
325 menu = self.MakeFileMenu()
326 self.mainmenu.Append(menu, '&File')
327
328 self.MakeToolMenu() # toolbar
329
330 self.SetMenuBar(self.mainmenu)
331 self.calend = wxCalendar(self, -1)
332 self.calend.SetCurrentDay()
333 self.calend.grid_color = 'BLUE'
334 self.calend.SetBusType()
335# self.calend.ShowWeekEnd()
336
337 self.ResetDisplay()
338
339 self.Connect(self.calend.GetId(), -1, 2100, self.MouseClick)
340
341 def MouseClick(self, evt):
342 text = '%s CLICK %02d/%02d/%d' % (evt.click, evt.day, evt.month, evt.year) # format date
343 self.log.WriteText('Date Selected: ' + text + '\n')
344
345 def OnCloseWindow(self, event):
346 self.Destroy()
347
348 def ResetDisplay(self):
349 month = self.calend.GetMonth()
350 try:
351 set_days = test_days[month]
352 except:
353 set_days = [1, 5, 12]
354
355 self.calend.AddSelect([2, 16], 'GREEN', 'WHITE')
356
357 self.calend.SetSelDay(set_days)
358 self.calend.Refresh()
359
360 def OnIncYear(self, event):
361 self.calend.IncYear()
362 self.ResetDisplay()
363
364 def OnDecYear(self, event):
365 self.calend.DecYear()
366 self.ResetDisplay()
367
368 def OnIncMonth(self, event):
369 self.calend.IncMonth()
370 self.ResetDisplay()
371
372 def OnDecMonth(self, event):
373 self.calend.DecMonth()
374 self.ResetDisplay()
375
376 def OnCurrent(self, event):
377 self.calend.SetCurrentDay()
378 self.ResetDisplay()
379
380 def MakeFileMenu(self):
381 menu = wxMenu()
382
383 mID = NewId()
384 menu.Append(mID, 'Decrement', 'Next')
385 EVT_MENU(self, mID, self.OnDecMonth)
386
387 mID = NewId()
388 menu.Append(mID, 'Increment', 'Dec')
389 EVT_MENU(self, mID, self.OnIncMonth)
390
391 menu.AppendSeparator()
392
393 mID = NewId()
394 menu.Append(mID, 'E&xit', 'Exit')
395 EVT_MENU(self, mID, self.OnCloseWindow)
396
397 return menu
398
399 def MakeToolMenu(self):
400 tb = self.CreateToolBar(wxTB_HORIZONTAL|wxNO_BORDER)
401
402 bmp_path = 'bitmaps/'
403 SetToolPath(self, tb, 10, bmp_path + 'DbDec.bmp', 'Dec Year')
404 EVT_TOOL(self, 10, self.OnDecYear)
405
406 SetToolPath(self, tb, 15, bmp_path + 'Dec.bmp', 'Dec Month')
407 EVT_TOOL(self, 15, self.OnDecMonth)
408
409 SetToolPath(self, tb, 30, bmp_path + 'Pt.bmp', 'Current Month')
410 EVT_TOOL(self, 30, self.OnCurrent)
411
412 SetToolPath(self, tb, 40, bmp_path + 'Inc.bmp', 'Inc Month')
413 EVT_TOOL(self, 40, self.OnIncMonth)
414
415 SetToolPath(self, tb, 45, bmp_path + 'DbInc.bmp', 'Inc Year')
416 EVT_TOOL(self, 45, self.OnIncYear)
417
418 tb.Realize()
419
420#---------------------------------------------------------------------------
421
422# example class for printing/previewing calendars
423
424class PrintCalend:
425 def __init__(self, parent, month, year):
426 self.frame = parent
427 self.month = month
428 self.year = year
429
430 self.SetParms()
431 self.SetCal()
432 self.printData = wxPrintData()
433
434 def SetCal(self):
435 self.grid_color = 'BLUE'
436 self.back_color = 'WHITE'
437 self.sel_color = 'RED'
438 self.high_color = 'LIGHT BLUE'
439 self.font = wxSWISS
440 self.bold = wxNORMAL
441
442 self.sel_key = None # last used by
443 self.sel_lst = [] # highlighted selected days
444
445 self.size = None
446 self.hide_title = FALSE
447 self.hide_grid = FALSE
448 self.set_day = None
449
450 def SetParms(self):
451 self.ymax = 1
452 self.xmax = 1
453 self.page = 1
454 self.total_pg = 1
455
456 self.preview = None
457 self.scale = 1.0
458
459 self.pagew = 8.5
460 self.pageh = 11.0
461
462 self.txt_marg = 0.1
463 self.lf_marg = 0
464 self.top_marg = 0
465
466 self.page = 0
467
468 def SetDates(self, month, year):
469 self.month = month
470 self.year = year
471
472 def SetStyleDef(self, desc):
473 self.style = desc
474
475 def SetCopies(self, copies): # number of copies of label
476 self.copies = copies
477
478 def SetStart(self, start): # start position of label
479 self.start = start
480
481 def Preview(self):
482 printout = SetPrintout(self)
483 printout2 = SetPrintout(self)
484 self.preview = wxPrintPreview(printout, printout2, self.printData)
485 if not self.preview.Ok():
486 wxMessageBox("There was a problem printing!", "Printing", wxOK)
487 return
488
489 self.preview.SetZoom(60) # initial zoom value
490
491 frame = wxPreviewFrame(self.preview, self.frame, "Print preview")
492
493 frame.Initialize()
494 frame.SetPosition(self.frame.GetPosition())
495 frame.SetSize(self.frame.GetSize())
496 frame.Show(true)
497
498 def Print(self):
499 pdd = wxPrintDialogData()
500 pdd.SetPrintData(self.printData)
501 printer = wxPrinter(pdd)
502 printout = SetPrintout(self)
503 frame = wxFrame(NULL, -1, "Test")
504 if not printer.Print(frame, printout):
505 wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK)
506 else:
507 self.printData = printer.GetPrintDialogData().GetPrintData()
508 printout.Destroy()
509
510 def DoDrawing(self, DC):
511 size = DC.GetSizeTuple()
512 DC.BeginDrawing()
513
514 cal = PrtCalDraw(self)
515
516 if self.preview is None:
517 cal.SetPSize(size[0]/self.pagew, size[1]/self.pageh)
518 cal.SetPreview(FALSE)
519
520 else:
521 if self.preview == 1:
522 cal.SetPSize(size[0]/self.pagew, size[1]/self.pageh)
523 else:
524 cal.SetPSize(self.pwidth, self.pheight)
525
526 cal.SetPreview(self.preview)
527
528 cal.hide_title = self.hide_title # set the calendar parameters
529 cal.hide_grid = self.hide_grid
530
531 cal.grid_color = self.grid_color
532 cal.high_color = self.high_color
533 cal.back_color = self.back_color
534 cal.outer_border = FALSE
535 cal.font = self.font
536 cal.bold = self.bold
537
538 cal_size = wxSize(3.0, 3.0)
539 cal.SetSize(cal_size)
540
541 year, month = self.year, self.month
542
543 x = 1.0
544 for i in range(2):
545 y = 0.5
546 for j in range(3):
547 cal.SetCal(year, month) # current month
548 cal.SetPos(x, y)
549
550 try:
551 set_days = test_days[month]
552 except:
553 set_days = [1, 5, 12]
554
555 cal.AddSelect([2, 16], 'GREEN', 'WHITE')
556
557 cal.DrawCal(DC, set_days)
558
559 year, month = self.IncMonth(year, month)
560 y = y + 3.5
561 x = x + 4.0 # next colum
562
563 DC.EndDrawing()
564
565 self.ymax = DC.MaxY()
566 self.xmax = DC.MaxX()
567
568 def IncMonth(self, year, month): # next month
569 month = month + 1
570 if month > 12:
571 month = 1
572 year = year + 1
573
574 return year, month
575
576 def GetTotalPages(self):
577 self.pg_cnt = 1
578 return self.pg_cnt
579
580 def SetPage(self, page):
581 self.page = page
582
583 def SetPageSize(self, width, height):
584 self.pwidth, self.pheight = width, height
585
586 def SetTotalSize(self, width, height):
587 self.ptwidth, self.ptheight = width, height
588
589 def SetPreview(self, preview, scale):
590 self.preview = preview
591 self.scale = scale
592
593 def SetTotalSize(self, width, height):
594 self.ptwidth = width
595 self.ptheight = height
596
597def SetToolPath(self, tb, id, bmp, title):
598 global dir_path
599 tb.AddSimpleTool(id, wxBitmap(os.path.join(dir_path, bmp), wxBITMAP_TYPE_BMP),
600 title, title)
601
602class SetPrintout(wxPrintout):
603 def __init__(self, canvas):
604 wxPrintout.__init__(self)
605 self.canvas = canvas
606 self.end_pg = 1
607
608 def OnBeginDocument(self, start, end):
609 return self.base_OnBeginDocument(start, end)
610
611 def OnEndDocument(self):
612 self.base_OnEndDocument()
613
614 def HasPage(self, page):
615 if page <= self.end_pg:
616 return true
617 else:
618 return false
619
620 def GetPageInfo(self):
621 self.end_pg = self.canvas.GetTotalPages()
622 str_pg = 1
623 try:
624 end_pg = self.end_pg
625 except:
626 end_pg = 1
627 return (str_pg, end_pg, str_pg, end_pg)
628
629 def OnPreparePrinting(self):
630 self.base_OnPreparePrinting()
631
632 def OnBeginPrinting(self):
633 dc = self.GetDC()
634
635 self.preview = self.IsPreview()
636 if (self.preview):
637 self.pixelsPerInch = self.GetPPIScreen()
638 else:
639 self.pixelsPerInch = self.GetPPIPrinter()
640
641 (w, h) = dc.GetSizeTuple()
642 scaleX = float(w) / 1000
643 scaleY = float(h) / 1000
644 self.printUserScale = min(scaleX, scaleY)
645
646 self.base_OnBeginPrinting()
647
648 def GetSize(self):
649 self.psizew, self.psizeh = self.GetPPIPrinter()
650 return self.psizew, self.psizeh
651
652 def GetTotalSize(self):
653 self.ptsizew, self.ptsizeh = self.GetPageSizePixels()
654 return self.ptsizew, self.ptsizeh
655
656 def OnPrintPage(self, page):
657 dc = self.GetDC()
658 (w, h) = dc.GetSizeTuple()
659 scaleX = float(w) / 1000
660 scaleY = float(h) / 1000
661 self.printUserScale = min(scaleX, scaleY)
662 dc.SetUserScale(self.printUserScale, self.printUserScale)
663
664 self.preview = self.IsPreview()
665
666 self.canvas.SetPreview(self.preview, self.printUserScale)
667 self.canvas.SetPage(page)
668
669 self.ptsizew, self.ptsizeh = self.GetPageSizePixels()
670 self.canvas.SetTotalSize(self.ptsizew, self.ptsizeh)
671
672 self.psizew, self.psizeh = self.GetPPIPrinter()
673 self.canvas.SetPageSize(self.psizew, self.psizeh)
674
675 self.canvas.DoDrawing(dc)
676 return true
677
678class MyApp(wxApp):
679 def OnInit(self):
680 frame = CalendFrame(NULL, -1, "Test Calendar")
681 frame.Show(true)
682 self.SetTopWindow(frame)
683 return true
684
685#---------------------------------------------------------------------------
686
687def MessageDlg(self, message, type = 'Message'):
688 dlg = wxMessageDialog(self, message, type, wxOK | wxICON_INFORMATION)
689 dlg.ShowModal()
690 dlg.Destroy()
691
692#---------------------------------------------------------------------------
693
694def main():
695 app = MyApp(0)
696 app.MainLoop()
697
698
699if __name__ == '__main__':
700 main()
701
702
703#---------------------------------------------------------------------------
704
705def runTest(frame, nb, log):
706 win = TestPanel(nb, log, frame)
707 return win
708
709#---------------------------------------------------------------------------
710
711
712overview = """\
713This control provides a calendar control class for displaying and selecting dates. In addition, the class is extended and can now be used for printing/previewing.
714
715Additional features include weekend highlighting and business type Monday-Sunday format.
716
717See example for various methods used to set display month, year, and highlighted dates (different font and background colours).
718
719by Lorne White
720
721"""