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