📄 docview.py
字号:
data = wx.PrintDialogData(self.printData)
printDialog = wx.PrintDialog(parentWin, data)
printDialog.GetPrintDialogData().SetSetupDialog(True)
printDialog.ShowModal()
# this makes a copy of the wx.PrintData instead of just saving
# a reference to the one inside the PrintDialogData that will
# be destroyed when the dialog is destroyed
self.printData = wx.PrintData(printDialog.GetPrintDialogData().GetPrintData())
printDialog.Destroy()
def OnPreview(self, event):
"""
Previews the current document by calling its View's OnCreatePrintout
method.
"""
view = self.GetCurrentView()
if not view:
return
printout = view.OnCreatePrintout()
if printout:
if not hasattr(self, "printData"):
self.printData = wx.PrintData()
self.printData.SetPaperId(wx.PAPER_LETTER)
self.printData.SetPrintMode(wx.PRINT_MODE_PREVIEW)
data = wx.PrintDialogData(self.printData)
# Pass two printout objects: for preview, and possible printing.
preview = wx.PrintPreview(printout, view.OnCreatePrintout(), data)
if not preview.Ok():
wx.MessageBox(_("Unable to display print preview."))
return
# wxWindows source doesn't use base frame's pos, size, and icon, but did it this way so it would work like MS Office etc.
mimicFrame = wx.GetApp().GetTopWindow()
frame = wx.PreviewFrame(preview, mimicFrame, _("Print Preview"), mimicFrame.GetPosition(), mimicFrame.GetSize())
frame.SetIcon(mimicFrame.GetIcon())
frame.SetTitle(_("%s - %s - Preview") % (mimicFrame.GetTitle(), view.GetDocument().GetPrintableName()))
frame.Initialize()
frame.Show(True)
def OnUndo(self, event):
"""
Issues an Undo command to the current document's command processor.
"""
doc = self.GetCurrentDocument()
if not doc:
return
if doc.GetCommandProcessor():
doc.GetCommandProcessor().Undo()
def OnRedo(self, event):
"""
Issues a Redo command to the current document's command processor.
"""
doc = self.GetCurrentDocument()
if not doc:
return
if doc.GetCommandProcessor():
doc.GetCommandProcessor().Redo()
def OnUpdateFileOpen(self, event):
"""
Updates the user interface for the File Open command.
"""
event.Enable(True)
def OnUpdateFileClose(self, event):
"""
Updates the user interface for the File Close command.
"""
event.Enable(self.GetCurrentDocument() != None)
def OnUpdateFileCloseAll(self, event):
"""
Updates the user interface for the File Close All command.
"""
event.Enable(self.GetCurrentDocument() != None)
def OnUpdateFileRevert(self, event):
"""
Updates the user interface for the File Revert command.
"""
event.Enable(self.GetCurrentDocument() != None)
def OnUpdateFileNew(self, event):
"""
Updates the user interface for the File New command.
"""
return True
def OnUpdateFileSave(self, event):
"""
Updates the user interface for the File Save command.
"""
doc = self.GetCurrentDocument()
event.Enable(doc != None and doc.IsModified())
def OnUpdateFileSaveAs(self, event):
"""
Updates the user interface for the File Save As command.
"""
event.Enable(self.GetCurrentDocument() != None and self.GetCurrentDocument().GetWriteable())
def OnUpdateUndo(self, event):
"""
Updates the user interface for the Undo command.
"""
doc = self.GetCurrentDocument()
event.Enable(doc != None and doc.GetCommandProcessor() != None and doc.GetCommandProcessor().CanUndo())
if doc and doc.GetCommandProcessor():
doc.GetCommandProcessor().SetMenuStrings()
else:
event.SetText(_("&Undo\tCtrl+Z"))
def OnUpdateRedo(self, event):
"""
Updates the user interface for the Redo command.
"""
doc = self.GetCurrentDocument()
event.Enable(doc != None and doc.GetCommandProcessor() != None and doc.GetCommandProcessor().CanRedo())
if doc and doc.GetCommandProcessor():
doc.GetCommandProcessor().SetMenuStrings()
else:
event.SetText(_("&Redo\tCtrl+Y"))
def OnUpdatePrint(self, event):
"""
Updates the user interface for the Print command.
"""
event.Enable(self.GetCurrentDocument() != None)
def OnUpdatePrintSetup(self, event):
"""
Updates the user interface for the Print Setup command.
"""
return True
def OnUpdatePreview(self, event):
"""
Updates the user interface for the Print Preview command.
"""
event.Enable(self.GetCurrentDocument() != None)
def GetCurrentView(self):
"""
Returns the currently active view.
"""
if self._currentView:
return self._currentView
if len(self._docs) == 1:
return self._docs[0].GetFirstView()
return None
def GetLastActiveView(self):
"""
Returns the last active view. This is used in the SDI framework where dialogs can be mistaken for a view
and causes the framework to deactivete the current view. This happens when something like a custom dialog box used
to operate on the current view is shown.
"""
if len(self._docs) >= 1:
return self._lastActiveView
else:
return None
def ProcessEvent(self, event):
"""
Processes an event, searching event tables and calling zero or more
suitable event handler function(s). Note that the ProcessEvent
method is called from the wxPython docview framework directly since
wxPython does not have a virtual ProcessEvent function.
"""
view = self.GetCurrentView()
if view:
if view.ProcessEvent(event):
return True
id = event.GetId()
if id == wx.ID_OPEN:
self.OnFileOpen(event)
return True
elif id == wx.ID_CLOSE:
self.OnFileClose(event)
return True
elif id == wx.ID_CLOSE_ALL:
self.OnFileCloseAll(event)
return True
elif id == wx.ID_REVERT:
self.OnFileRevert(event)
return True
elif id == wx.ID_NEW:
self.OnFileNew(event)
return True
elif id == wx.ID_SAVE:
self.OnFileSave(event)
return True
elif id == wx.ID_SAVEAS:
self.OnFileSaveAs(event)
return True
elif id == wx.ID_UNDO:
self.OnUndo(event)
return True
elif id == wx.ID_REDO:
self.OnRedo(event)
return True
elif id == wx.ID_PRINT:
self.OnPrint(event)
return True
elif id == wx.ID_PRINT_SETUP:
self.OnPrintSetup(event)
return True
elif id == wx.ID_PREVIEW:
self.OnPreview(event)
return True
else:
return False
def ProcessUpdateUIEvent(self, event):
"""
Processes a UI event, searching event tables and calling zero or more
suitable event handler function(s). Note that the ProcessEvent
method is called from the wxPython docview framework directly since
wxPython does not have a virtual ProcessEvent function.
"""
id = event.GetId()
view = self.GetCurrentView()
if view:
if view.ProcessUpdateUIEvent(event):
return True
if id == wx.ID_OPEN:
self.OnUpdateFileOpen(event)
return True
elif id == wx.ID_CLOSE:
self.OnUpdateFileClose(event)
return True
elif id == wx.ID_CLOSE_ALL:
self.OnUpdateFileCloseAll(event)
return True
elif id == wx.ID_REVERT:
self.OnUpdateFileRevert(event)
return True
elif id == wx.ID_NEW:
self.OnUpdateFileNew(event)
return True
elif id == wx.ID_SAVE:
self.OnUpdateFileSave(event)
return True
elif id == wx.ID_SAVEAS:
self.OnUpdateFileSaveAs(event)
return True
elif id == wx.ID_UNDO:
self.OnUpdateUndo(event)
return True
elif id == wx.ID_REDO:
self.OnUpdateRedo(event)
return True
elif id == wx.ID_PRINT:
self.OnUpdatePrint(event)
return True
elif id == wx.ID_PRINT_SETUP:
self.OnUpdatePrintSetup(event)
return True
elif id == wx.ID_PREVIEW:
self.OnUpdatePreview(event)
return True
else:
return False
def CreateDocument(self, path, flags=0):
"""
Creates a new document in a manner determined by the flags parameter,
which can be:
wx.lib.docview.DOC_NEW Creates a fresh document.
wx.lib.docview.DOC_SILENT Silently loads the given document file.
If wx.lib.docview.DOC_NEW is present, a new document will be created and returned,
possibly after asking the user for a template to use if there is more
than one document template. If wx.lib.docview.DOC_SILENT is present, a new document
will be created and the given file loaded into it. If neither of these
flags is present, the user will be presented with a file selector for
the file to load, and the template to use will be determined by the
extension (Windows) or by popping up a template choice list (other
platforms).
If the maximum number of documents has been reached, this function
will delete the oldest currently loaded document before creating a new
one.
wxPython version supports the document manager's wx.lib.docview.DOC_OPEN_ONCE
and wx.lib.docview.DOC_NO_VIEW flag.
if wx.lib.docview.DOC_OPEN_ONCE is present, trying to open the same file multiple
times will just return the same document.
if wx.lib.docview.DOC_NO_VIEW is present, opening a file will generate the document,
but not generate a corresponding view.
"""
templates = []
for temp in self._templates:
if temp.IsVisible():
templates.append(temp)
if len(templates) == 0:
return None
if len(self.GetDocuments()) >= self._maxDocsOpen:
doc = self.GetDocuments()[0]
if not self.CloseDocument(doc, False):
return None
if flags & DOC_NEW:
for temp in templates[:]:
if not temp.IsNewable():
templates.remove(temp)
if len(templates) == 1:
temp = templates[0]
else:
temp = self.SelectDocumentType(templates)
if temp:
newDoc = temp.CreateDocument(path, flags)
if newDoc:
newDoc.SetDocumentName(temp.GetDocumentName())
newDoc.SetDocumentTemplate(temp)
newDoc.OnNewDocument()
return newDoc
else:
return None
if path and flags & DOC_SILENT:
temp = self.FindTemplateForPath(path)
else:
temp, path = self.SelectDocumentPath(templates, path, flags)
# Existing document
if path and self.GetFlags() & DOC_OPEN_ONCE:
for document in self._docs:
if document.GetFilename() and os.path.normcase(document.GetFilename()) == os.path.normcase(path):
""" check for file modification outside of application """
if not document.IsDocumentModificationDateCorrect():
msgTitle = wx.GetApp().GetAppName()
if not msgTitle:
msgTitle = _("Warning")
shortName = document.GetPrintableName()
res = wx.MessageBox(_("'%s' has been modified outside of %s. Reload '%s' from file system?") % (shortName, msgTitle, shortName),
msgTitle,
wx.YES_NO | wx.ICON_QUESTION,
self.FindSuitableParent())
if res == wx.YES:
if not self.CloseDocument(document, False):
wx.MessageBox(_("Couldn't reload '%s'. Unable to close current '%s'.") % (shortName, shortName))
return None
return self.CreateDocument(path, flags)
elif res == wx.NO: # don't ask again
document.SetDocumentModificationDate()
firstView = document.GetFirstView()
if not firstView and not (flags & DOC_NO_VIEW):
document.GetDocumentTemplate().CreateView(document, flags)
document.UpdateAllViews()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -