]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/Calendar.py
It *does* work now
[wxWidgets.git] / wxPython / demo / Calendar.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 = 0.5
502 for i in range(2):
503 y = 0.5
504
505 for j in range(3):
506 cal.SetCal(year, month) # current month
507 cal.SetPos(x, y)
508
509 try:
510 set_days = test_days[month]
511 except:
512 set_days = [1, 5, 12]
513
514 cal.AddSelect([2, 16], 'GREEN', 'WHITE')
515
516 cal.DrawCal(DC, set_days)
517
518 year, month = self.IncMonth(year, month)
519 y = y + 3.5
520
521 x = x + 4.0 # next column
522
523 DC.EndDrawing()
524
525 self.ymax = DC.MaxY()
526 self.xmax = DC.MaxX()
527
528 def IncMonth(self, year, month): # next month
529 month = month + 1
530
531 if month > 12:
532 month = 1
533 year = year + 1
534
535 return year, month
536
537 def GetTotalPages(self):
538 self.pg_cnt = 1
539 return self.pg_cnt
540
541 def SetPage(self, page):
542 self.page = page
543
544 def SetPageSize(self, width, height):
545 self.pwidth, self.pheight = width, height
546
547 def SetTotalSize(self, width, height):
548 self.ptwidth, self.ptheight = width, height
549
550 def SetPreview(self, preview, scale):
551 self.preview = preview
552 self.scale = scale
553
554 def SetTotalSize(self, width, height):
555 self.ptwidth = width
556 self.ptheight = height
557
558 def SetToolPath(self, tb, id, bmp, title):
559 tb.AddSimpleTool(id, bmp, title, title)
560
561 class SetPrintout(wx.Printout):
562 def __init__(self, canvas):
563 wx.Printout.__init__(self)
564 self.canvas = canvas
565 self.end_pg = 1
566
567 def OnBeginDocument(self, start, end):
568 return self.base_OnBeginDocument(start, end)
569
570 def OnEndDocument(self):
571 self.base_OnEndDocument()
572
573 def HasPage(self, page):
574 if page <= self.end_pg:
575 return True
576 else:
577 return False
578
579 def GetPageInfo(self):
580 self.end_pg = self.canvas.GetTotalPages()
581 str_pg = 1
582
583 try:
584 end_pg = self.end_pg
585 except:
586 end_pg = 1
587
588 return (str_pg, end_pg, str_pg, end_pg)
589
590 def OnPreparePrinting(self):
591 self.base_OnPreparePrinting()
592
593 def OnBeginPrinting(self):
594 dc = self.GetDC()
595
596 self.preview = self.IsPreview()
597
598 if (self.preview):
599 self.pixelsPerInch = self.GetPPIScreen()
600 else:
601 self.pixelsPerInch = self.GetPPIPrinter()
602
603 (w, h) = dc.GetSize()
604 scaleX = float(w) / 1000
605 scaleY = float(h) / 1000
606 self.printUserScale = min(scaleX, scaleY)
607
608 self.base_OnBeginPrinting()
609
610 def GetSize(self):
611 self.psizew, self.psizeh = self.GetPPIPrinter()
612 return self.psizew, self.psizeh
613
614 def GetTotalSize(self):
615 self.ptsizew, self.ptsizeh = self.GetPageSizePixels()
616 return self.ptsizew, self.ptsizeh
617
618 def OnPrintPage(self, page):
619 dc = self.GetDC()
620 (w, h) = dc.GetSize()
621 scaleX = float(w) / 1000
622 scaleY = float(h) / 1000
623 self.printUserScale = min(scaleX, scaleY)
624 dc.SetUserScale(self.printUserScale, self.printUserScale)
625
626 self.preview = self.IsPreview()
627
628 self.canvas.SetPreview(self.preview, self.printUserScale)
629 self.canvas.SetPage(page)
630
631 self.ptsizew, self.ptsizeh = self.GetPageSizePixels()
632 self.canvas.SetTotalSize(self.ptsizew, self.ptsizeh)
633
634 self.psizew, self.psizeh = self.GetPPIPrinter()
635 self.canvas.SetPageSize(self.psizew, self.psizeh)
636
637 self.canvas.DoDrawing(dc)
638 return True
639
640 class MyApp(wx.App):
641 def OnInit(self):
642 frame = CalendFrame(None, -1, "Test Calendar", log)
643 frame.Show(True)
644 self.SetTopWindow(frame)
645 return True
646
647 #---------------------------------------------------------------------------
648
649 def MessageDlg(self, message, type = 'Message'):
650 dlg = wx.MessageDialog(self, message, type, wx.OK | wx.ICON_INFORMATION)
651 dlg.ShowModal()
652 dlg.Destroy()
653
654 #---------------------------------------------------------------------------
655
656 def runTest(frame, nb, log):
657 win = TestPanel(nb, log, frame)
658 return win
659
660 #---------------------------------------------------------------------------
661
662
663 overview = """\
664 This control provides a Calendar control class for displaying and selecting dates.
665 In addition, the class is extended and can be used for printing/previewing.
666
667 Additional features include weekend highlighting and business type Monday-Sunday
668 format.
669
670 See example for various methods used to set display month, year, and highlighted
671 dates (different font and background colours).
672
673 by Lorne White
674
675 """
676
677
678
679
680 if __name__ == '__main__':
681 import sys,os
682 import run
683 run.main(['', os.path.basename(sys.argv[0])])
684