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