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