📄 pydocview.py
字号:
if t is template:
self._notebook.SetPageImage(index, iconIndex)
found = True
break
if not found:
self._notebook.SetPageImage(index, self._blankIconIndex)
# wxBug: the wxListbook used on Mac needs its tabs list resized
# whenever a new tab is added, but the only way to do this is
# to resize the entire control
if wx.Platform == "__WXMAC__":
content_size = self._notebook.GetSize()
self._notebook.SetSize((content_size.x+2, -1))
self._notebook.SetSize((content_size.x, -1))
self._notebook.Layout()
windowMenuService = wx.GetApp().GetService(WindowMenuService)
if windowMenuService:
windowMenuService.BuildWindowMenu(wx.GetApp().GetTopWindow()) # build file menu list when we open a file
def RemoveNotebookPage(self, panel):
"""
Removes a document page from the notebook.
"""
index = self.GetNotebookPageIndex(panel)
if index > -1:
if self._notebook.GetPageCount() == 1 or index < 2:
pass
elif index >= 1:
self._notebook.SetSelection(index - 1)
elif index < self._notebook.GetPageCount():
self._notebook.SetSelection(index + 1)
self._notebook.DeletePage(index)
self._notebook.GetParent().SetToolTip(wx.ToolTip(""))
windowMenuService = wx.GetApp().GetService(WindowMenuService)
if windowMenuService:
windowMenuService.BuildWindowMenu(wx.GetApp().GetTopWindow()) # build file menu list when we open a file
def ActivateNotebookPage(self, panel):
"""
Sets the notebook to the specified panel.
"""
index = self.GetNotebookPageIndex(panel)
if index > -1:
self._notebook.SetFocus()
self._notebook.SetSelection(index)
def GetNotebookPageTitle(self, panel):
index = self.GetNotebookPageIndex(panel)
if index != -1:
return self._notebook.GetPageText(self.GetNotebookPageIndex(panel))
else:
return None
def SetNotebookPageTitle(self, panel, title):
self._notebook.SetPageText(self.GetNotebookPageIndex(panel), title)
def GetNotebookPageIndex(self, panel):
"""
Returns the index of particular notebook panel.
"""
index = -1
for i in range(self._notebook.GetPageCount()):
if self._notebook.GetPage(i) == panel:
index = i
break
return index
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.
"""
if wx.GetApp().ProcessEventBeforeWindows(event):
return True
if self._docManager and self._docManager.ProcessEvent(event):
return True
return DocMDIParentFrameMixIn.ProcessEvent(self, event)
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.
"""
if wx.GetApp().ProcessUpdateUIEventBeforeWindows(event):
return True
if self._docManager and self._docManager.ProcessUpdateUIEvent(event):
return True
return DocMDIParentFrameMixIn.ProcessUpdateUIEvent(self, event)
def OnExit(self, event):
"""
Called when File/Exit is chosen and closes the window.
"""
self.Close()
def OnMRUFile(self, event):
"""
Opens the appropriate file when it is selected from the file history
menu.
"""
n = event.GetId() - wx.ID_FILE1
filename = self._docManager.GetHistoryFile(n)
if filename:
self._docManager.CreateDocument(filename, wx.lib.docview.DOC_SILENT)
else:
self._docManager.RemoveFileFromHistory(n)
msgTitle = wx.GetApp().GetAppName()
if not msgTitle:
msgTitle = _("File Error")
wx.MessageBox("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list" % FileNameFromPath(file),
msgTitle,
wx.OK | wx.ICON_EXCLAMATION,
self)
def OnSize(self, event):
"""
Called when the frame is resized and lays out the client window.
"""
# Needed in case there are splitpanels around the mdi frame
self._LayoutFrame()
def OnCloseWindow(self, event):
"""
Called when the frame is closed. Remembers the frame size.
"""
self.SaveEmbeddedWindowSizes()
# save and close services last
for service in wx.GetApp().GetServices():
if not service.OnCloseFrame(event):
return
# From docview.MDIParentFrame
if self._docManager.Clear(not event.CanVeto()):
self.Destroy()
else:
event.Veto()
class DocMDIChildFrame(wx.MDIChildFrame):
"""
The wxDocMDIChildFrame class provides a default frame for displaying
documents on separate windows. This class can only be used for MDI child
frames.
The class is part of the document/view framework supported by wxWindows,
and cooperates with the wxView, wxDocument, wxDocManager and wxDocTemplate
classes.
"""
def __init__(self, doc, view, frame, id, title, pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE, name="frame"):
"""
Constructor. Note that the event table must be rebuilt for the
frame since the EvtHandler is not virtual.
"""
wx.MDIChildFrame.__init__(self, frame, id, title, pos, size, style, name)
self._childDocument = doc
self._childView = view
if view:
view.SetFrame(self)
# self.Create(doc, view, frame, id, title, pos, size, style, name)
self._activeEvent = None
self._activated = 0
wx.EVT_ACTIVATE(self, self.OnActivate)
wx.EVT_CLOSE(self, self.OnCloseWindow)
if frame: # wxBug: For some reason the EVT_ACTIVATE event is not getting triggered for the first mdi client window that is opened so we have to do it manually
mdiChildren = filter(lambda x: isinstance(x, wx.MDIChildFrame), frame.GetChildren())
if len(mdiChildren) == 1:
self.Activate()
## # Couldn't get this to work, but seems to work fine with single stage construction
## def Create(self, doc, view, frame, id, title, pos, size, style, name):
## self._childDocument = doc
## self._childView = view
## if wx.MDIChildFrame.Create(self, frame, id, title, pos, size, style, name):
## if view:
## view.SetFrame(self)
## return True
## return False
def Activate(self): # Need this in case there are embedded sash windows and such, OnActivate is not getting called
"""
Activates the current view.
"""
if self._childView:
self._childView.Activate(True)
def OnTitleIsModified(self):
"""
Add/remove to the frame's title an indication that the document is dirty.
If the document is dirty, an '*' is appended to the title
"""
title = self.GetTitle()
if title:
if self.GetDocument().IsModified():
if title.endswith("*"):
return
else:
title = title + "*"
self.SetTitle(title)
else:
if title.endswith("*"):
title = title[:-1]
self.SetTitle(title)
else:
return
def ProcessEvent(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.
"""
if self._activeEvent == event:
return False
self._activeEvent = event # Break recursion loops
if self._childView:
self._childView.Activate(True)
if not self._childView or not self._childView.ProcessEvent(event):
if not isinstance(event, wx.CommandEvent) or not self.GetParent() or not self.GetParent().ProcessEvent(event):
ret = False
else:
ret = True
else:
ret = True
self._activeEvent = None
return ret
def OnActivate(self, event):
"""
Sets the currently active view to be the frame's view. You may need to
override (but still call) this function in order to set the keyboard
focus for your subwindow.
"""
if self._activated != 0:
return True
self._activated += 1
wx.MDIChildFrame.Activate(self)
if event.GetActive() and self._childView:
self._childView.Activate(event.GetActive())
self._activated = 0
def OnCloseWindow(self, event):
"""
Closes and deletes the current view and document.
"""
if self._childView:
ans = False
if not event.CanVeto():
ans = True
else:
ans = self._childView.Close(deleteWindow = False)
if ans:
self._childView.Activate(False)
self._childView.Destroy()
self._childView = None
if self._childDocument:
self._childDocument.Destroy() # This isn't in the wxWindows codebase but the document needs to be disposed of somehow
self._childDocument = None
self.Destroy()
else:
event.Veto()
else:
event.Veto()
def GetDocument(self):
"""
Returns the document associated with this frame.
"""
return self._childDocument
def SetDocument(self, document):
"""
Sets the document for this frame.
"""
self._childDocument = document
def GetView(self):
"""
Returns the view associated with this frame.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -