]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxCalendar.py
backwards compatibility aliases can be used in the wxPython namespace
[wxWidgets.git] / wxPython / demo / wxCalendar.py
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 # Version 0.9
8 # Date: Feb 26, 2001
9 # Licence: wxWindows license
10 #----------------------------------------------------------------------------
11 # 11/15/2003 - Jeff Grimmett (grimmtooth@softhome.net)
12 #
13 # o Updated for wx namespace
14 # o Some updating of the library itself will be needed for this demo to work
15 # correctly.
16 #
17 # 11/26/2003 - Jeff Grimmett (grimmtooth@softhome.net)
18 #
19 # o Problems have changed a little. The print dialog requires
20 # a wx.Size to work with the calendar library. wx.core doesn't
21 # approve, though, so we get deprecation warnings.
22 # o Ugh. AFter updating to the Bind() method, things lock up
23 # on various control clicks. Will have to debug. Only seems
24 # to happen on windows with calendar controls, though.
25 #
26 # 11/30/2003 - Jeff Grimmett (grimmtooth@softhome.net)
27 #
28 # o Lockup issue clarification: it appears that the spinner is
29 # the culprit.
30 #
31 # 12/01/2003 - Jeff Grimmett (grimmtooth@softhome.net)
32 #
33 # o New Bind() method now fully supported.
34 #
35 # 12/17/2003 - Jeff Grimmett (grimmtooth@softhome.net)
36 #
37 # o wxCalendar renamed to Calendar
38 # o Got rid of unneeded IDs where Bind() could figure it
39 # out for itself.
40 #
41
42 import os
43
44 import wx
45 import wx.lib.calendar
46
47 import images
48
49
50 # highlighted days in month
51
52 test_days ={ 0: [],
53 1: [3, 7, 9, 21],
54 2: [2, 10, 4, 9],
55 3: [4, 20, 29],
56 4: [1, 12, 22],
57 5: [2, 10, 15],
58 6: [4, 8, 17],
59 7: [6, 7, 8],
60 8: [5, 10, 20],
61 9: [1, 2, 5, 29],
62 10: [2, 4, 6, 22],
63 11: [6, 9, 12, 28, 29],
64 12: [8, 9, 10, 11, 20] }
65
66 # test of full window calendar control functions
67
68 def GetMonthList():
69
70 monthlist = []
71
72 for i in range(13):
73 name = wx.lib.calendar.Month[i]
74
75 if name != None:
76 monthlist.append(name)
77
78 return monthlist
79
80 class TestPanel(wx.Panel):
81 def __init__(self, parent, log, frame):
82 wx.Panel.__init__(self, parent, -1)
83
84 self.log = log
85 self.frame = frame
86
87 self.calend = wx.lib.calendar.Calendar(self, -1, (100, 50), (200, 180))
88
89 # start_month = 2 # preselect the date for calendar
90 # start_year = 2001
91
92 start_month = self.calend.GetMonth() # get the current month & year
93 start_year = self.calend.GetYear()
94
95 # month list from DateTime module
96
97 monthlist = GetMonthList()
98
99 self.date = wx.ComboBox(self, -1, "",
100 (100, 20), (90, -1),
101 monthlist, wx.CB_DROPDOWN)
102
103 self.date.SetSelection(start_month-1)
104 self.Bind(wx.EVT_COMBOBOX, self.EvtComboBox, self.date)
105
106 # set start month and year
107
108 self.calend.SetMonth(start_month)
109 self.calend.SetYear(start_year)
110
111 # set attributes of calendar
112
113 self.calend.hide_title = True
114 self.calend.HideGrid()
115 self.calend.SetWeekColor('WHITE', 'BLACK')
116
117 # display routine
118
119 self.ResetDisplay()
120
121 # mouse click event
122 self.Bind(wx.lib.calendar.EVT_CALENDAR, self.MouseClick, self.calend)
123
124 # scroll bar for month selection
125 self.scroll = wx.ScrollBar(self, -1, (100, 240), (200, 20), wx.SB_HORIZONTAL)
126 self.scroll.SetScrollbar(start_month-1, 1, 12, 1, True)
127 self.Bind(wx.EVT_COMMAND_SCROLL, self.Scroll, self.scroll)
128
129 # spin control for year selection
130
131 self.dtext = wx.TextCtrl(self, -1, str(start_year), (200, 20), (60, -1))
132 h = self.dtext.GetSize().height
133
134 self.spin = wx.SpinButton(self, -1, (270, 20), (h*2, h))
135 self.spin.SetRange(1980, 2010)
136 self.spin.SetValue(start_year)
137 self.Bind(wx.EVT_SPIN, self.OnSpin, self.spin)
138
139 # button for calendar dialog test
140
141 wx.StaticText(self, -1, "Test Calendar Dialog", (350, 50), (150, -1))
142
143 bmp = images.getCalendarBitmap()
144 self.but1 = wx.BitmapButton(self, -1, bmp, (380, 80))
145 self.Bind(wx.EVT_BUTTON, self.TestDlg, self.but1)
146
147 # button for calendar window test
148
149 wx.StaticText(self, -1, "Test Calendar Window", (350, 150), (150, -1))
150
151 self.but2 = wx.BitmapButton(self, -1, bmp, (380, 180))
152 self.Bind(wx.EVT_BUTTON, self.TestFrame, self.but2)
153
154 wx.StaticText(self, -1, "Test Calendar Print", (350, 250), (150, -1))
155
156 self.but3 = wx.BitmapButton(self, -1, bmp, (380, 280))
157 self.Bind(wx.EVT_BUTTON, self.OnPreview, self.but3)
158
159 # calendar dialog
160
161 def TestDlg(self, event): # test the date dialog
162 dlg = wx.lib.calendar.CalenDlg(self)
163 dlg.Centre()
164
165 if dlg.ShowModal() == wx.ID_OK:
166 result = dlg.result
167 day = result[1]
168 month = result[2]
169 year = result[3]
170 new_date = str(month) + '/'+ str(day) + '/'+ str(year)
171 self.log.WriteText('Date Selected: %s\n' % new_date)
172 else:
173 self.log.WriteText('No Date Selected')
174
175 # calendar window test
176
177 def TestFrame(self, event):
178 frame = CalendFrame(self, -1, "Test Calendar", self.log)
179 frame.Show(True)
180 return True
181
182 # calendar print preview
183
184 def OnPreview(self, event):
185 month = self.calend.GetMonth()
186 year = self.calend.GetYear()
187
188 prt = PrintCalend(self.frame, month, year)
189 prt.Preview()
190
191 # month and year control events
192
193 def OnSpin(self, event):
194 year = event.GetPosition()
195 self.dtext.SetValue(str(year))
196 self.calend.SetYear(year)
197 self.calend.Refresh()
198
199 def EvtComboBox(self, event):
200 name = event.GetString()
201 self.log.WriteText('EvtComboBox: %s\n' % name)
202 monthval = self.date.FindString(name)
203 self.scroll.SetScrollbar(monthval, 1, 12, 1, True)
204
205 self.calend.SetMonth(monthval+1)
206 self.ResetDisplay()
207
208 def Scroll(self, event):
209 value = self.scroll.GetThumbPosition()
210 monthval = int(value)+1
211 self.calend.SetMonth(monthval)
212 self.ResetDisplay()
213 self.log.WriteText('Month: %s\n' % value)
214
215 name = wx.lib.calendar.Month[monthval]
216 self.date.SetValue(name)
217
218 # log mouse events
219
220 def MouseClick(self, evt):
221 text = '%s CLICK %02d/%02d/%d' % (evt.click, evt.day, evt.month, evt.year) # format date
222 self.log.WriteText('Date Selected: ' + text + '\n')
223
224
225 # set the highlighted days for the calendar
226
227 def ResetDisplay(self):
228 month = self.calend.GetMonth()
229
230 try:
231 set_days = test_days[month]
232 except:
233 set_days = [1, 5, 12]
234
235 self.calend.AddSelect([4, 11], 'BLUE', 'WHITE')
236 self.calend.SetSelDay(set_days)
237 self.calend.Refresh()
238
239 # increment and decrement toolbar controls
240
241 def OnIncYear(self, event):
242 self.calend.IncYear()
243 self.ResetDisplay()
244
245 def OnDecYear(self, event):
246 self.calend.DecYear()
247 self.ResetDisplay()
248
249 def OnIncMonth(self, event):
250 self.calend.IncMonth()
251 self.ResetDisplay()
252
253 def OnDecMonth(self, event):
254 self.calend.DecMonth()
255 self.ResetDisplay()
256
257 def OnCurrent(self, event):
258 self.calend.SetCurrentDay()
259 self.ResetDisplay()
260
261 # test of full window calendar control functions
262
263 class CalendFrame(wx.Frame):
264 def __init__(self, parent, id, title, log):
265 wx.Frame.__init__(self, parent, id, title, size=(400, 400),
266 style=wx.DEFAULT_FRAME_STYLE|wx.NO_FULL_REPAINT_ON_RESIZE)
267
268 self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
269
270 self.log = log
271 self.CreateStatusBar()
272 self.mainmenu = wx.MenuBar()
273 menu = wx.Menu()
274
275 menu = self.MakeFileMenu()
276 self.mainmenu.Append(menu, '&File')
277
278 self.MakeToolMenu() # toolbar
279
280 self.SetMenuBar(self.mainmenu)
281 self.calend = wx.lib.calendar.Calendar(self, -1)
282 self.calend.SetCurrentDay()
283 self.calend.grid_color = 'BLUE'
284 self.calend.SetBusType()
285 # self.calend.ShowWeekEnd()
286
287 self.ResetDisplay()
288
289 self.Bind(wx.lib.calendar.EVT_CALENDAR, self.MouseClick, self.calend)
290
291 def MouseClick(self, evt):
292 text = '%s CLICK %02d/%02d/%d' % (evt.click, evt.day, evt.month, evt.year) # format date
293 self.log.WriteText('Date Selected: ' + text + '\n')
294
295 def OnCloseWindow(self, event):
296 self.Destroy()
297
298 def ResetDisplay(self):
299 month = self.calend.GetMonth()
300
301 try:
302 set_days = test_days[month]
303 except:
304 set_days = [1, 5, 12]
305
306 self.calend.AddSelect([2, 16], 'GREEN', 'WHITE')
307
308 self.calend.SetSelDay(set_days)
309 self.calend.Refresh()
310
311 def OnIncYear(self, event):
312 self.calend.IncYear()
313 self.ResetDisplay()
314
315 def OnDecYear(self, event):
316 self.calend.DecYear()
317 self.ResetDisplay()
318
319 def OnIncMonth(self, event):
320 self.calend.IncMonth()
321 self.ResetDisplay()
322
323 def OnDecMonth(self, event):
324 self.calend.DecMonth()
325 self.ResetDisplay()
326
327 def OnCurrent(self, event):
328 self.calend.SetCurrentDay()
329 self.ResetDisplay()
330
331 def MakeFileMenu(self):
332 menu = wx.Menu()
333
334 mID = wx.NewId()
335 menu.Append(mID, 'Decrement', 'Next')
336 self.Bind(wx.EVT_MENU, self.OnDecMonth, id=mID)
337
338 mID = wx.NewId()
339 menu.Append(mID, 'Increment', 'Dec')
340 self.Bind(wx.EVT_MENU, self.OnIncMonth, id=mID)
341
342 menu.AppendSeparator()
343
344 mID = wx.NewId()
345 menu.Append(mID, 'E&xit', 'Exit')
346 self.Bind(wx.EVT_MENU, self.OnCloseWindow, id=mID)
347
348 return menu
349
350 def MakeToolMenu(self):
351 tb = self.CreateToolBar(wx.TB_HORIZONTAL|wx.NO_BORDER)
352
353 mID = wx.NewId()
354 SetToolPath(self, tb, mID, images.getDbDecBitmap(), 'Dec Year')
355 self.Bind(wx.EVT_TOOL, self.OnDecYear, id=mID)
356
357 mID = wx.NewId()
358 SetToolPath(self, tb, mID, images.getDecBitmap(), 'Dec Month')
359 self.Bind(wx.EVT_TOOL, self.OnDecMonth, id=mID)
360
361 mID = wx.NewId()
362 SetToolPath(self, tb, mID, images.getPtBitmap(), 'Current Month')
363 self.Bind(wx.EVT_TOOL, self.OnCurrent, id=mID)
364
365 mID = wx.NewId()
366 SetToolPath(self, tb, mID, images.getIncBitmap(), 'Inc Month')
367 self.Bind(wx.EVT_TOOL, self.OnIncMonth, id=mID)
368
369 mID = wx.NewId()
370 SetToolPath(self, tb, mID, images.getDbIncBitmap(), 'Inc Year')
371 self.Bind(wx.EVT_TOOL, self.OnIncYear, id=mID)
372
373 tb.Realize()
374
375 #---------------------------------------------------------------------------
376
377 # example class for printing/previewing calendars
378
379 class PrintCalend:
380 def __init__(self, parent, month, year):
381 self.frame = parent
382 self.month = month
383 self.year = year
384
385 self.SetParms()
386 self.SetCal()
387 self.printData = wx.PrintData()
388
389 def SetCal(self):
390 self.grid_color = 'BLUE'
391 self.back_color = 'WHITE'
392 self.sel_color = 'RED'
393 self.high_color = 'LIGHT BLUE'
394 self.font = wx.SWISS
395 self.bold = wx.NORMAL
396
397 self.sel_key = None # last used by
398 self.sel_lst = [] # highlighted selected days
399
400 self.size = None
401 self.hide_title = False
402 self.hide_grid = False
403 self.set_day = None
404
405 def SetParms(self):
406 self.ymax = 1
407 self.xmax = 1
408 self.page = 1
409 self.total_pg = 1
410
411 self.preview = None
412 self.scale = 1.0
413
414 self.pagew = 8.5
415 self.pageh = 11.0
416
417 self.txt_marg = 0.1
418 self.lf_marg = 0
419 self.top_marg = 0
420
421 self.page = 0
422
423 def SetDates(self, month, year):
424 self.month = month
425 self.year = year
426
427 def SetStyleDef(self, desc):
428 self.style = desc
429
430 def SetCopies(self, copies): # number of copies of label
431 self.copies = copies
432
433 def SetStart(self, start): # start position of label
434 self.start = start
435
436 def Preview(self):
437 printout = SetPrintout(self)
438 printout2 = SetPrintout(self)
439 self.preview = wx.PrintPreview(printout, printout2, self.printData)
440
441 if not self.preview.Ok():
442 wx.MessageBox("There was a problem printing!", "Printing", wx.OK)
443 return
444
445 self.preview.SetZoom(60) # initial zoom value
446
447 frame = wx.PreviewFrame(self.preview, self.frame, "Print preview")
448
449 frame.Initialize()
450 frame.SetPosition(self.frame.GetPosition())
451 frame.SetSize(self.frame.GetSize())
452 frame.Show(True)
453
454 def Print(self):
455 pdd = wx.PrintDialogData()
456 pdd.SetPrintData(self.printData)
457 printer = wx.Printer(pdd)
458 printout = SetPrintout(self)
459 frame = wx.Frame(None, -1, "Test")
460
461 if not printer.Print(frame, printout):
462 wx.MessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wx.OK)
463 else:
464 self.printData = printer.GetPrintDialogData().GetPrintData()
465
466 printout.Destroy()
467
468 def DoDrawing(self, DC):
469 size = DC.GetSize()
470 DC.BeginDrawing()
471
472 cal = wx.lib.calendar.PrtCalDraw(self)
473
474 if self.preview is None:
475 cal.SetPSize(size[0]/self.pagew, size[1]/self.pageh)
476 cal.SetPreview(False)
477
478 else:
479 if self.preview == 1:
480 cal.SetPSize(size[0]/self.pagew, size[1]/self.pageh)
481 else:
482 cal.SetPSize(self.pwidth, self.pheight)
483
484 cal.SetPreview(self.preview)
485
486 cal.hide_title = self.hide_title # set the calendar parameters
487 cal.hide_grid = self.hide_grid
488
489 cal.grid_color = self.grid_color
490 cal.high_color = self.high_color
491 cal.back_color = self.back_color
492 cal.outer_border = False
493 cal.font = self.font
494 cal.bold = self.bold
495
496 cal_size = (3.0, 3.0)
497 cal.SetSize(cal_size)
498
499 year, month = self.year, self.month
500
501 x = 1.0
502
503 for i in range(2):
504 y = 0.5
505
506 for j in range(3):
507 cal.SetCal(year, month) # current month
508 cal.SetPos(x, y)
509
510 try:
511 set_days = test_days[month]
512 except:
513 set_days = [1, 5, 12]
514
515 cal.AddSelect([2, 16], 'GREEN', 'WHITE')
516
517 cal.DrawCal(DC, set_days)
518
519 year, month = self.IncMonth(year, month)
520 y = y + 3.5
521
522 x = x + 4.0 # next column
523
524 DC.EndDrawing()
525
526 self.ymax = DC.MaxY()
527 self.xmax = DC.MaxX()
528
529 def IncMonth(self, year, month): # next month
530 month = month + 1
531
532 if month > 12:
533 month = 1
534 year = year + 1
535
536 return year, month
537
538 def GetTotalPages(self):
539 self.pg_cnt = 1
540 return self.pg_cnt
541
542 def SetPage(self, page):
543 self.page = page
544
545 def SetPageSize(self, width, height):
546 self.pwidth, self.pheight = width, height
547
548 def SetTotalSize(self, width, height):
549 self.ptwidth, self.ptheight = width, height
550
551 def SetPreview(self, preview, scale):
552 self.preview = preview
553 self.scale = scale
554
555 def SetTotalSize(self, width, height):
556 self.ptwidth = width
557 self.ptheight = height
558
559 def SetToolPath(self, tb, id, bmp, title):
560 tb.AddSimpleTool(id, bmp, title, title)
561
562 class SetPrintout(wx.Printout):
563 def __init__(self, canvas):
564 wx.Printout.__init__(self)
565 self.canvas = canvas
566 self.end_pg = 1
567
568 def OnBeginDocument(self, start, end):
569 return self.base_OnBeginDocument(start, end)
570
571 def OnEndDocument(self):
572 self.base_OnEndDocument()
573
574 def HasPage(self, page):
575 if page <= self.end_pg:
576 return True
577 else:
578 return False
579
580 def GetPageInfo(self):
581 self.end_pg = self.canvas.GetTotalPages()
582 str_pg = 1
583
584 try:
585 end_pg = self.end_pg
586 except:
587 end_pg = 1
588
589 return (str_pg, end_pg, str_pg, end_pg)
590
591 def OnPreparePrinting(self):
592 self.base_OnPreparePrinting()
593
594 def OnBeginPrinting(self):
595 dc = self.GetDC()
596
597 self.preview = self.IsPreview()
598
599 if (self.preview):
600 self.pixelsPerInch = self.GetPPIScreen()
601 else:
602 self.pixelsPerInch = self.GetPPIPrinter()
603
604 (w, h) = dc.GetSize()
605 scaleX = float(w) / 1000
606 scaleY = float(h) / 1000
607 self.printUserScale = min(scaleX, scaleY)
608
609 self.base_OnBeginPrinting()
610
611 def GetSize(self):
612 self.psizew, self.psizeh = self.GetPPIPrinter()
613 return self.psizew, self.psizeh
614
615 def GetTotalSize(self):
616 self.ptsizew, self.ptsizeh = self.GetPageSizePixels()
617 return self.ptsizew, self.ptsizeh
618
619 def OnPrintPage(self, page):
620 dc = self.GetDC()
621 (w, h) = dc.GetSize()
622 scaleX = float(w) / 1000
623 scaleY = float(h) / 1000
624 self.printUserScale = min(scaleX, scaleY)
625 dc.SetUserScale(self.printUserScale, self.printUserScale)
626
627 self.preview = self.IsPreview()
628
629 self.canvas.SetPreview(self.preview, self.printUserScale)
630 self.canvas.SetPage(page)
631
632 self.ptsizew, self.ptsizeh = self.GetPageSizePixels()
633 self.canvas.SetTotalSize(self.ptsizew, self.ptsizeh)
634
635 self.psizew, self.psizeh = self.GetPPIPrinter()
636 self.canvas.SetPageSize(self.psizew, self.psizeh)
637
638 self.canvas.DoDrawing(dc)
639 return True
640
641 class MyApp(wx.App):
642 def OnInit(self):
643 frame = CalendFrame(None, -1, "Test Calendar", log)
644 frame.Show(True)
645 self.SetTopWindow(frame)
646 return True
647
648 #---------------------------------------------------------------------------
649
650 def MessageDlg(self, message, type = 'Message'):
651 dlg = wx.MessageDialog(self, message, type, wx.OK | wx.ICON_INFORMATION)
652 dlg.ShowModal()
653 dlg.Destroy()
654
655 #---------------------------------------------------------------------------
656
657 def runTest(frame, nb, log):
658 win = TestPanel(nb, log, frame)
659 return win
660
661 #---------------------------------------------------------------------------
662
663
664 overview = """\
665 This control provides a Calendar control class for displaying and selecting dates.
666 In addition, the class is extended and can be used for printing/previewing.
667
668 Additional features include weekend highlighting and business type Monday-Sunday
669 format.
670
671 See example for various methods used to set display month, year, and highlighted
672 dates (different font and background colours).
673
674 by Lorne White
675
676 """
677
678
679
680
681 if __name__ == '__main__':
682 import sys,os
683 import run
684 run.main(['', os.path.basename(sys.argv[0])])
685