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