# Name: pydocview.py
# Purpose: Python extensions to the wxWindows docview framework
#
-# Author: Peter Yared
+# Author: Peter Yared, Morgan Hua
#
# Created: 5/15/03
# CVS-ID: $Id$
-# Copyright: (c) 2003-2004 ActiveGrid, Inc.
+# Copyright: (c) 2003-2005 ActiveGrid, Inc.
# License: wxWindows license
#----------------------------------------------------------------------------
import time
import string
import pickle
-import getpass
import tempfile
import mmap
_ = wx.GetTranslation
-
+if wx.Platform == '__WXMSW__':
+ _WINDOWS = True
+else:
+ _WINDOWS = False
#----------------------------------------------------------------------------
# Constants
WINDOW_MENU_NUM_ITEMS = 9
-class DocService(wx.EvtHandler):
+class DocFrameMixIn:
"""
- An abstract class used to add reusable services to a docview application.
+ Class with common code used by DocMDIParentFrame, DocTabbedParentFrame, and
+ DocSDIFrame.
"""
-
-
- def __init__(self):
- """Initializes the DocService."""
- pass
-
+
def GetDocumentManager(self):
- """Returns the DocManager for the docview application."""
- return self._docManager
-
-
- def SetDocumentManager(self, docManager):
- """Sets the DocManager for the docview application."""
- self._docManager = docManager
-
-
- def InstallControls(self, frame, menuBar = None, toolBar = None, statusBar = None, document = None):
- """Called to install controls into the menubar and toolbar of a SDI or MDI window. Override this method for a particular service."""
- pass
-
-
- def ProcessEventBeforeWindows(self, event):
"""
- Processes an event before the main window has a chance to process the window.
- Override this method for a particular service.
+ Returns the document manager associated with the DocMDIParentFrame.
"""
- return False
+ return self._docManager
- def ProcessUpdateUIEventBeforeWindows(self, event):
+ def InitializePrintData(self):
"""
- Processes a UI event before the main window has a chance to process the window.
- Override this method for a particular service.
+ Initializes the PrintData that is used when printing.
"""
- return False
+ self._printData = wx.PrintData()
+ self._printData.SetPaperId(wx.PAPER_LETTER)
- def ProcessEvent(self, event):
+ def CreateDefaultMenuBar(self, sdi=False):
"""
- 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.
+ Creates the default MenuBar. Contains File, Edit, View, Tools, and Help menus.
"""
- return False
+ menuBar = wx.MenuBar()
+
+ fileMenu = wx.Menu()
+ fileMenu.Append(wx.ID_NEW, _("&New...\tCtrl+N"), _("Creates a new document"))
+ fileMenu.Append(wx.ID_OPEN, _("&Open...\tCtrl+O"), _("Opens an existing document"))
+ fileMenu.Append(wx.ID_CLOSE, _("&Close"), _("Closes the active document"))
+ if not sdi:
+ fileMenu.Append(wx.ID_CLOSE_ALL, _("Close A&ll"), _("Closes all open documents"))
+ fileMenu.AppendSeparator()
+ fileMenu.Append(wx.ID_SAVE, _("&Save\tCtrl+S"), _("Saves the active document"))
+ fileMenu.Append(wx.ID_SAVEAS, _("Save &As..."), _("Saves the active document with a new name"))
+ fileMenu.Append(SAVEALL_ID, _("Save All\tCtrl+Shift+A"), _("Saves the all active documents"))
+ wx.EVT_MENU(self, SAVEALL_ID, self.ProcessEvent)
+ wx.EVT_UPDATE_UI(self, SAVEALL_ID, self.ProcessUpdateUIEvent)
+ fileMenu.AppendSeparator()
+ fileMenu.Append(wx.ID_PRINT, _("&Print\tCtrl+P"), _("Prints the active document"))
+ fileMenu.Append(wx.ID_PREVIEW, _("Print Pre&view"), _("Displays full pages"))
+ fileMenu.Append(wx.ID_PRINT_SETUP, _("Page Set&up"), _("Changes page layout settings"))
+ fileMenu.AppendSeparator()
+ if wx.Platform == '__WXMAC__':
+ fileMenu.Append(wx.ID_EXIT, _("&Quit"), _("Closes this program"))
+ else:
+ fileMenu.Append(wx.ID_EXIT, _("E&xit"), _("Closes this program"))
+ self._docManager.FileHistoryUseMenu(fileMenu)
+ self._docManager.FileHistoryAddFilesToMenu()
+ menuBar.Append(fileMenu, _("&File"));
+ editMenu = wx.Menu()
+ editMenu.Append(wx.ID_UNDO, _("&Undo\tCtrl+Z"), _("Reverses the last action"))
+ editMenu.Append(wx.ID_REDO, _("&Redo\tCtrl+Y"), _("Reverses the last undo"))
+ editMenu.AppendSeparator()
+ #item = wxMenuItem(self.editMenu, wxID_CUT, _("Cu&t\tCtrl+X"), _("Cuts the selection and puts it on the Clipboard"))
+ #item.SetBitmap(getCutBitmap())
+ #editMenu.AppendItem(item)
+ editMenu.Append(wx.ID_CUT, _("Cu&t\tCtrl+X"), _("Cuts the selection and puts it on the Clipboard"))
+ wx.EVT_MENU(self, wx.ID_CUT, self.ProcessEvent)
+ wx.EVT_UPDATE_UI(self, wx.ID_CUT, self.ProcessUpdateUIEvent)
+ editMenu.Append(wx.ID_COPY, _("&Copy\tCtrl+C"), _("Copies the selection and puts it on the Clipboard"))
+ wx.EVT_MENU(self, wx.ID_COPY, self.ProcessEvent)
+ wx.EVT_UPDATE_UI(self, wx.ID_COPY, self.ProcessUpdateUIEvent)
+ editMenu.Append(wx.ID_PASTE, _("&Paste\tCtrl+V"), _("Inserts Clipboard contents"))
+ wx.EVT_MENU(self, wx.ID_PASTE, self.ProcessEvent)
+ wx.EVT_UPDATE_UI(self, wx.ID_PASTE, self.ProcessUpdateUIEvent)
+ editMenu.Append(wx.ID_CLEAR, _("&Delete"), _("Erases the selection"))
+ wx.EVT_MENU(self, wx.ID_CLEAR, self.ProcessEvent)
+ wx.EVT_UPDATE_UI(self, wx.ID_CLEAR, self.ProcessUpdateUIEvent)
+ editMenu.AppendSeparator()
+ editMenu.Append(wx.ID_SELECTALL, _("Select A&ll\tCtrl+A"), _("Selects all available data"))
+ wx.EVT_MENU(self, wx.ID_SELECTALL, self.ProcessEvent)
+ wx.EVT_UPDATE_UI(self, wx.ID_SELECTALL, self.ProcessUpdateUIEvent)
+ menuBar.Append(editMenu, _("&Edit"))
+ if sdi:
+ if self.GetDocument() and self.GetDocument().GetCommandProcessor():
+ self.GetDocument().GetCommandProcessor().SetEditMenu(editMenu)
+
+ viewMenu = wx.Menu()
+ viewMenu.AppendCheckItem(VIEW_TOOLBAR_ID, _("&Toolbar"), _("Shows or hides the toolbar"))
+ wx.EVT_MENU(self, VIEW_TOOLBAR_ID, self.OnViewToolBar)
+ wx.EVT_UPDATE_UI(self, VIEW_TOOLBAR_ID, self.OnUpdateViewToolBar)
+ viewMenu.AppendCheckItem(VIEW_STATUSBAR_ID, _("&Status Bar"), _("Shows or hides the status bar"))
+ wx.EVT_MENU(self, VIEW_STATUSBAR_ID, self.OnViewStatusBar)
+ wx.EVT_UPDATE_UI(self, VIEW_STATUSBAR_ID, self.OnUpdateViewStatusBar)
+ menuBar.Append(viewMenu, _("&View"))
- 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.
- """
- return False
+ helpMenu = wx.Menu()
+ helpMenu.Append(wx.ID_ABOUT, _("&About" + " " + wx.GetApp().GetAppName()), _("Displays program information, version number, and copyright"))
+ menuBar.Append(helpMenu, _("&Help"))
+ wx.EVT_MENU(self, wx.ID_ABOUT, self.OnAbout)
+ wx.EVT_UPDATE_UI(self, wx.ID_ABOUT, self.ProcessUpdateUIEvent) # Using ID_ABOUT to update the window menu, the window menu items are not triggering
- def OnCloseFrame(self, event):
- """
- Called when the a docview frame is being closed. Override this method
- so a service can either do cleanup or veto the frame being closed by
- returning false.
- """
- return True
+ if sdi: # TODO: Is this really needed?
+ wx.EVT_COMMAND_FIND_CLOSE(self, -1, self.ProcessEvent)
+
+ return menuBar
- def OnExit(self):
+ def CreateDefaultStatusBar(self):
"""
- Called when the the docview application is being closed. Override this method
- so a service can either do cleanup or veto the frame being closed by
- returning false.
+ Creates the default StatusBar.
"""
- pass
+ wx.Frame.CreateStatusBar(self)
+ self.GetStatusBar().Show(wx.ConfigBase_Get().ReadInt("ViewStatusBar", True))
+ self.UpdateStatus()
+ return self.GetStatusBar()
- def GetMenuItemPos(self, menu, id):
+ def CreateDefaultToolBar(self):
"""
- Utility method used to find the position of a menu item so that services can
- easily find where to insert a menu item in InstallControls.
+ Creates the default ToolBar.
"""
- menuItems = menu.GetMenuItems()
- for i, menuItem in enumerate(menuItems):
- if menuItem.GetId() == id:
- return i
- return i
+ self._toolBar = self.CreateToolBar(wx.TB_HORIZONTAL | wx.NO_BORDER | wx.TB_FLAT)
+ self._toolBar.AddSimpleTool(wx.ID_NEW, getNewBitmap(), _("New"), _("Creates a new document"))
+ self._toolBar.AddSimpleTool(wx.ID_OPEN, getOpenBitmap(), _("Open"), _("Opens an existing document"))
+ self._toolBar.AddSimpleTool(wx.ID_SAVE, getSaveBitmap(), _("Save"), _("Saves the active document"))
+ self._toolBar.AddSimpleTool(SAVEALL_ID, getSaveAllBitmap(), _("Save All"), _("Saves all the active documents"))
+ self._toolBar.AddSeparator()
+ self._toolBar.AddSimpleTool(wx.ID_PRINT, getPrintBitmap(), _("Print"), _("Displays full pages"))
+ self._toolBar.AddSimpleTool(wx.ID_PREVIEW, getPrintPreviewBitmap(), _("Print Preview"), _("Prints the active document"))
+ self._toolBar.AddSeparator()
+ self._toolBar.AddSimpleTool(wx.ID_CUT, getCutBitmap(), _("Cut"), _("Cuts the selection and puts it on the Clipboard"))
+ self._toolBar.AddSimpleTool(wx.ID_COPY, getCopyBitmap(), _("Copy"), _("Copies the selection and puts it on the Clipboard"))
+ self._toolBar.AddSimpleTool(wx.ID_PASTE, getPasteBitmap(), _("Paste"), _("Inserts Clipboard contents"))
+ self._toolBar.AddSimpleTool(wx.ID_UNDO, getUndoBitmap(), _("Undo"), _("Reverses the last action"))
+ self._toolBar.AddSimpleTool(wx.ID_REDO, getRedoBitmap(), _("Redo"), _("Reverses the last undo"))
+ self._toolBar.Realize()
+ self._toolBar.Show(wx.ConfigBase_Get().ReadInt("ViewToolBar", True))
+ return self._toolBar
- def GetView(self):
+
+ def OnFileSaveAll(self, event):
"""
- Called by WindowMenuService to get views for services that don't
- have dedicated documents such as the Outline Service.
+ Saves all of the currently open documents.
"""
- return None
-
-
-class DocOptionsService(DocService):
- """
- A service that implements an options menu item and an options dialog with
- notebook tabs. New tabs can be added by other services by calling the
- "AddOptionsPanel" method.
- """
+ docs = wx.GetApp().GetDocumentManager().GetDocuments()
+ for doc in docs:
+ doc.Save()
- def __init__(self, showGeneralOptions = True):
+ def OnAbout(self, event):
"""
- Initializes the options service with the option of suppressing the default
- general options pane that is included with the options service by setting
- showGeneralOptions to False.
+ Invokes the about dialog.
"""
- DocService.__init__(self)
- self.ClearOptionsPanels()
- self._toolOptionsID = wx.NewId()
- if showGeneralOptions:
- self.AddOptionsPanel(GeneralOptionsPanel)
+ aboutService = wx.GetApp().GetService(AboutService)
+ if aboutService:
+ aboutService.ShowAbout()
- def InstallControls(self, frame, menuBar = None, toolBar = None, statusBar = None, document = None):
+ def OnViewToolBar(self, event):
"""
- Installs a "Tools" menu with an "Options" menu item.
+ Toggles whether the ToolBar is visible.
"""
- toolsMenuIndex = menuBar.FindMenu(_("&Tools"))
- if toolsMenuIndex > -1:
- toolsMenu = menuBar.GetMenu(toolsMenuIndex)
- else:
- toolsMenu = wx.Menu()
- if toolsMenuIndex == -1:
- formatMenuIndex = menuBar.FindMenu(_("&Format"))
- menuBar.Insert(formatMenuIndex + 1, toolsMenu, _("&Tools"))
- if toolsMenu:
- if toolsMenu.GetMenuItemCount():
- toolsMenu.AppendSeparator()
- toolsMenu.Append(self._toolOptionsID, _("&Options..."), _("Sets options"))
- wx.EVT_MENU(frame, self._toolOptionsID, frame.ProcessEvent)
-
+ self._toolBar.Show(not self._toolBar.IsShown())
+ self._LayoutFrame()
- def ProcessEvent(self, event):
+
+ def OnUpdateViewToolBar(self, event):
"""
- Checks to see if the "Options" menu item has been selected.
+ Updates the View ToolBar menu item.
"""
- id = event.GetId()
- if id == self._toolOptionsID:
- self.OnOptions(event)
- return True
- else:
- return False
+ event.Check(self.GetToolBar().IsShown())
- def ClearOptionsPanels(self):
+ def OnViewStatusBar(self, event):
"""
- Clears all of the options panels that have been added into the
- options dialog.
+ Toggles whether the StatusBar is visible.
"""
- self._optionsPanels = []
+ self.GetStatusBar().Show(not self.GetStatusBar().IsShown())
+ self._LayoutFrame()
- def AddOptionsPanel(self, optionsPanel):
+ def OnUpdateViewStatusBar(self, event):
"""
- Adds an options panel to the options dialog.
+ Updates the View StatusBar menu item.
"""
- self._optionsPanels.append(optionsPanel)
+ event.Check(self.GetStatusBar().IsShown())
- def OnOptions(self, event):
+ def UpdateStatus(self, message = _("Ready")):
"""
- Shows the options dialog, called when the "Options" menu item is selected.
+ Updates the StatusBar.
"""
- if len(self._optionsPanels) == 0:
- return
- optionsDialog = OptionsDialog(wx.GetApp().GetTopWindow(), self._optionsPanels, self._docManager)
- if optionsDialog.ShowModal() == wx.ID_OK:
- optionsDialog.OnOK(optionsDialog) # wxBug: wxDialog should be calling this automatically but doesn't
- optionsDialog.Destroy()
+ # wxBug: Menubar and toolbar help strings don't pop the status text back
+ if self.GetStatusBar().GetStatusText() != message:
+ self.GetStatusBar().PushStatusText(message)
-class OptionsDialog(wx.Dialog):
+class DocMDIParentFrameMixIn:
"""
- A default options dialog used by the OptionsService that hosts a notebook
- tab of options panels.
+ Class with common code used by DocMDIParentFrame and DocTabbedParentFrame.
"""
+
-
- def __init__(self, parent, optionsPanelClasses, docManager):
+ def _GetPosSizeFromConfig(self, pos, size):
"""
- Initializes the options dialog with a notebook page that contains new
- instances of the passed optionsPanelClasses.
+ Adjusts the position and size of the frame using the saved config position and size.
"""
- wx.Dialog.__init__(self, parent, -1, _("Options"), size = (310, 400))
-
- self._optionsPanels = []
- self._docManager = docManager
-
- HALF_SPACE = 5
- SPACE = 10
-
- sizer = wx.BoxSizer(wx.VERTICAL)
-
- optionsNotebook = wx.Notebook(self, -1, size = (310, 375), style = wx.NB_MULTILINE)
- optionsNotebookSizer = wx.NotebookSizer(optionsNotebook)
- sizer.Add(optionsNotebookSizer, 0, wx.ALL | wx.EXPAND, SPACE)
- for optionsPanelClass in optionsPanelClasses:
- optionsPanel = optionsPanelClass(optionsNotebook, -1)
- self._optionsPanels.append(optionsPanel)
- sizer.Add(self.CreateButtonSizer(wx.OK | wx.CANCEL), 0, wx.ALIGN_RIGHT | wx.RIGHT | wx.BOTTOM, HALF_SPACE)
- self.SetSizer(sizer)
- self.Layout()
- self.Fit()
- wx.CallAfter(self.DoRefresh)
-
-
- def DoRefresh(self):
- """
- wxBug: On Windows XP when using a multiline notebook the default page doesn't get
- drawn, but it works when using a single line notebook.
- """
- self.Refresh()
-
-
- def GetDocManager(self):
- """
- Returns the document manager passed to the OptionsDialog constructor.
- """
- return self._docManager
-
-
- def OnOK(self, event):
- """
- Calls the OnOK method of all of the OptionDialog's embedded panels
- """
- for optionsPanel in self._optionsPanels:
- optionsPanel.OnOK(event)
-
-
-class GeneralOptionsPanel(wx.Panel):
- """
- A general options panel that is used in the OptionDialog to configure the
- generic properties of a pydocview application, such as "show tips at startup"
- and whether to use SDI or MDI for the application.
- """
-
-
- def __init__(self, parent, id):
- """
- Initializes the panel by adding an "Options" folder tab to the parent notebook and
- populating the panel with the generic properties of a pydocview application.
- """
- wx.Panel.__init__(self, parent, id)
- SPACE = 10
- HALF_SPACE = 5
- backgroundColor = wx.WHITE
config = wx.ConfigBase_Get()
- self._showTipsCheckBox = wx.CheckBox(self, -1, _("Show tips at start up"))
- self._showTipsCheckBox.SetBackgroundColour(backgroundColor) # wxBUG: uses wrong background color
- self._showTipsCheckBox.SetValue(config.ReadInt("ShowTipAtStartup", True))
- self._documentRadioBox = wx.RadioBox(self, -1, _("Document interface"),
- choices = [_("Show each document in its own window (SDI)"),
- _("Show All documents in a single window (MDI)")],
- majorDimension=1,
- )
- if config.ReadInt("UseMDI", True):
- self._documentRadioBox.SetSelection(1)
+ if pos == wx.DefaultPosition and size == wx.DefaultSize and config.ReadInt("MDIFrameMaximized", False):
+ pos = [0, 0]
+ size = wx.DisplaySize()
+ # wxBug: Need to set to fill screen to get around bug where maximize is leaving shadow of statusbar, check out maximize call at end of this function
else:
- self._documentRadioBox.SetSelection(0)
- def OnDocumentInterfaceSelect(event):
- if not self._documentInterfaceMessageShown:
- msgTitle = wx.GetApp().GetAppName()
- if not msgTitle:
- msgTitle = _("Document Options")
- wx.MessageBox("Document interface changes will not appear until the application is restarted.",
- msgTitle,
- wx.OK | wx.ICON_INFORMATION,
- self.GetParent())
- self._documentInterfaceMessageShown = True
- wx.EVT_RADIOBOX(self, self._documentRadioBox.GetId(), OnDocumentInterfaceSelect)
- optionsBorderSizer = wx.BoxSizer(wx.VERTICAL)
- optionsSizer = wx.BoxSizer(wx.VERTICAL)
- optionsSizer.Add(self._showTipsCheckBox, 0, wx.ALL, HALF_SPACE)
- optionsSizer.Add(self._documentRadioBox, 0, wx.ALL, HALF_SPACE)
- optionsBorderSizer.Add(optionsSizer, 0, wx.ALL, SPACE)
- self.SetSizer(optionsBorderSizer)
- self.Layout()
- self._documentInterfaceMessageShown = False
- parent.AddPage(self, _("Options"))
-
- def OnOK(self, optionsDialog):
- """
- Updates the config based on the selections in the options panel.
- """
- config = wx.ConfigBase_Get()
- config.WriteInt("ShowTipAtStartup", self._showTipsCheckBox.GetValue())
- config.WriteInt("UseMDI", self._documentRadioBox.GetSelection())
-
-
-class DocApp(wx.PySimpleApp):
- """
- The DocApp class serves as the base class for pydocview applications and offers
- functionality such as services, creation of SDI and MDI frames, show tips,
- and a splash screen.
- """
-
-
- def OnInit(self):
- """
- Initializes the DocApp.
- """
- self._services = []
- self._defaultIcon = None
- self._registeredCloseEvent = False
- if not hasattr(self, "_debug"): # only set if not already initialized
- self._debug = False
- if not hasattr(self, "_singleInstance"): # only set if not already initialized
- self._singleInstance = True
-
- # if _singleInstance is TRUE only allow one single instance of app to run.
- # When user tries to run a second instance of the app, abort startup,
- # But if user also specifies files to open in command line, send message to running app to open those files
- if self._singleInstance:
- # create shared memory temporary file
- if wx.Platform == '__WXMSW__':
- tfile = tempfile.TemporaryFile(prefix="ag", suffix="tmp")
- fno = tfile.fileno()
- self._sharedMemory = mmap.mmap(fno, 1024, "shared_memory")
- else:
- tfile = file(os.path.join(tempfile.gettempdir(), tempfile.gettempprefix() + getpass.getuser() + "AGSharedMemory"), 'w+b')
- tfile.write("*")
- tfile.seek(1024)
- tfile.write(" ")
- tfile.flush()
- fno = tfile.fileno()
- self._sharedMemory = mmap.mmap(fno, 1024)
+ if pos == wx.DefaultPosition:
+ pos = config.ReadInt("MDIFrameXLoc", -1), config.ReadInt("MDIFrameYLoc", -1)
- self._singleInstanceChecker = wx.SingleInstanceChecker(self.GetAppName() + '-' + wx.GetUserId())
- if self._singleInstanceChecker.IsAnotherRunning():
- # have running single instance open file arguments
- foundArgs = False
- args = sys.argv[1:]
- for arg in args:
- if arg[0] != '/' and arg[0] != '-':
- foundArgs = True
- break
-
- if foundArgs:
- data = pickle.dumps(args)
- while 1:
- self._sharedMemory.seek(0)
- marker = self._sharedMemory.read_byte()
- if marker == '\0' or marker == '*': # available buffer
- self._sharedMemory.seek(0)
- self._sharedMemory.write_byte('-') # set writing marker
- self._sharedMemory.write(data) # write files we tried to open to shared memory
- self._sharedMemory.seek(0)
- self._sharedMemory.write_byte('+') # set finished writing marker
- self._sharedMemory.flush()
- break
- else:
- time.sleep(1) # give enough time for buffer to be available
-
- return False
- else:
- self._timer = wx.PyTimer(self.DoBackgroundListenAndLoad)
- self._timer.Start(250)
+ if wx.Display_GetFromPoint(pos) == -1: # Check if the frame position is offscreen
+ pos = wx.DefaultPosition
+
+ if size == wx.DefaultSize:
+ size = wx.Size(config.ReadInt("MDIFrameXSize", 450), config.ReadInt("MDIFrameYSize", 300))
+ return pos, size
- return True
-
- def DoBackgroundListenAndLoad(self):
+ def _InitFrame(self, embeddedWindows):
"""
- Open any files specified in the given command line argument passed in via shared memory
+ Initializes the frame and creates the default menubar, toolbar, and status bar.
"""
+ self._embeddedWindows = []
+ self.SetDropTarget(_DocFrameFileDropTarget(self._docManager, self))
- self._sharedMemory.seek(0)
- if self._sharedMemory.read_byte() == '+': # available data
- data = self._sharedMemory.read(1024-1)
- self._sharedMemory.seek(0)
- self._sharedMemory.write_byte("*") # finished reading, set buffer free marker
- self._sharedMemory.flush()
- args = pickle.loads(data)
- for arg in args:
- if arg[0] != '/' and arg[0] != '-':
- self.GetDocumentManager().CreateDocument(arg, wx.lib.docview.DOC_SILENT)
-
- # force display of running app
- topWindow = wx.GetApp().GetTopWindow()
- if topWindow.IsIconized():
- topWindow.Iconize(False)
- else:
- topWindow.Raise()
-
-
- self._timer.Start(1000) # 1 second interval
-
-
- def OpenCommandLineArgs(self):
- """
- Called to open files that have been passed to the application from the
- command line.
- """
- args = sys.argv[1:]
- for arg in args:
- if arg[0] != '/' and arg[0] != '-':
- self.GetDocumentManager().CreateDocument(arg, wx.lib.docview.DOC_SILENT)
+ if wx.GetApp().GetDefaultIcon():
+ self.SetIcon(wx.GetApp().GetDefaultIcon())
+ wx.EVT_MENU(self, wx.ID_ABOUT, self.OnAbout)
+ wx.EVT_SIZE(self, self.OnSize)
- def GetDocumentManager(self):
- """
- Returns the document manager associated to the DocApp.
- """
- return self._docManager
+ self.InitializePrintData()
+ toolBar = self.CreateDefaultToolBar()
+ self.SetToolBar(toolBar)
+ menuBar = self.CreateDefaultMenuBar()
+ statusBar = self.CreateDefaultStatusBar()
- def SetDocumentManager(self, docManager):
- """
- Sets the document manager associated with the DocApp and loads the
- DocApp's file history into the document manager.
- """
- self._docManager = docManager
config = wx.ConfigBase_Get()
- self.GetDocumentManager().FileHistoryLoad(config)
+ if config.ReadInt("MDIFrameMaximized", False):
+ # wxBug: On maximize, statusbar leaves a residual that needs to be refereshed, happens even when user does it
+ self.Maximize()
+ self.CreateEmbeddedWindows(embeddedWindows)
+ self._LayoutFrame()
- def ProcessEventBeforeWindows(self, event):
- """
- Enables services to process an event before the main window has a chance to
- process the window.
- """
- for service in self._services:
- if service.ProcessEventBeforeWindows(event):
- return True
- return False
+ if wx.Platform == '__WXMAC__':
+ self.SetMenuBar(menuBar) # wxBug: Have to set the menubar at the very end or the automatic MDI "window" menu doesn't get put in the right place when the services add new menus to the menubar
+ wx.GetApp().SetTopWindow(self) # Need to do this here in case the services are looking for wx.GetApp().GetTopWindow()
+ for service in wx.GetApp().GetServices():
+ service.InstallControls(self, menuBar = menuBar, toolBar = toolBar, statusBar = statusBar)
+ if hasattr(service, "ShowWindow"):
+ service.ShowWindow() # instantiate service windows for correct positioning, we'll hide/show them later based on user preference
- def ProcessUpdateUIEventBeforeWindows(self, event):
- """
- Enables services to process a UI event before the main window has a chance
- to process the window.
- """
- for service in self._services:
- if service.ProcessUpdateUIEventBeforeWindows(event):
- return True
- return False
+ if wx.Platform != '__WXMAC__':
+ self.SetMenuBar(menuBar) # wxBug: Have to set the menubar at the very end or the automatic MDI "window" menu doesn't get put in the right place when the services add new menus to the menubar
def ProcessEvent(self, event):
method is called from the wxPython docview framework directly since
wxPython does not have a virtual ProcessEvent function.
"""
- for service in self._services:
- if service.ProcessEvent(event):
- return True
- return False
+ id = event.GetId()
+ if id == SAVEALL_ID:
+ self.OnFileSaveAll(event)
+ return True
+
+ return wx.GetApp().ProcessEvent(event)
def ProcessUpdateUIEvent(self, event):
method is called from the wxPython docview framework directly since
wxPython does not have a virtual ProcessEvent function.
"""
- for service in self._services:
- if service.ProcessUpdateUIEvent(event):
- return True
- return False
+ id = event.GetId()
+ if id == wx.ID_CUT:
+ event.Enable(False)
+ return True
+ elif id == wx.ID_COPY:
+ event.Enable(False)
+ return True
+ elif id == wx.ID_PASTE:
+ event.Enable(False)
+ return True
+ elif id == wx.ID_CLEAR:
+ event.Enable(False)
+ return True
+ elif id == wx.ID_SELECTALL:
+ event.Enable(False)
+ return True
+ elif id == SAVEALL_ID:
+ filesModified = False
+ docs = wx.GetApp().GetDocumentManager().GetDocuments()
+ for doc in docs:
+ if doc.IsModified():
+ filesModified = True
+ break
+
+ event.Enable(filesModified)
+ return True
+ else:
+ return wx.GetApp().ProcessUpdateUIEvent(event)
- def InstallService(self, service):
+ def CreateEmbeddedWindows(self, windows=0):
"""
- Installs an instance of a DocService into the DocApp.
+ Create the specified embedded windows around the edges of the frame.
"""
- service.SetDocumentManager(self._docManager)
- self._services.append(service)
- return service
+ frameSize = self.GetSize() # TODO: GetClientWindow.GetSize is still returning 0,0 since the frame isn't fully constructed yet, so using full frame size
+ defaultHSize = int(frameSize[0] / 6)
+ defaultVSize = int(frameSize[1] / 7)
+ defaultSubVSize = int(frameSize[1] / 2)
+ config = wx.ConfigBase_Get()
+ if windows & (EMBEDDED_WINDOW_LEFT | EMBEDDED_WINDOW_TOPLEFT | EMBEDDED_WINDOW_BOTTOMLEFT):
+ self._leftEmbWindow = self._CreateEmbeddedWindow(self, (config.ReadInt("MDIEmbedLeftSize", defaultHSize), -1), wx.LAYOUT_VERTICAL, wx.LAYOUT_LEFT, visible = config.ReadInt("MDIEmbedLeftVisible", 1), sash = wx.SASH_RIGHT)
+ else:
+ self._leftEmbWindow = None
+ if windows & EMBEDDED_WINDOW_TOPLEFT:
+ self._topLeftEmbWindow = self._CreateEmbeddedWindow(self._leftEmbWindow, (-1, config.ReadInt("MDIEmbedTopLeftSize", defaultSubVSize)), wx.LAYOUT_HORIZONTAL, wx.LAYOUT_TOP, visible = config.ReadInt("MDIEmbedTopLeftVisible", 1), sash = wx.SASH_BOTTOM)
+ else:
+ self._topLeftEmbWindow = None
+ if windows & EMBEDDED_WINDOW_BOTTOMLEFT:
+ self._bottomLeftEmbWindow = self._CreateEmbeddedWindow(self._leftEmbWindow, (-1, config.ReadInt("MDIEmbedBottomLeftSize", defaultSubVSize)), wx.LAYOUT_HORIZONTAL, wx.LAYOUT_BOTTOM, visible = config.ReadInt("MDIEmbedBottomLeftVisible", 1))
+ else:
+ self._bottomLeftEmbWindow = None
+ if windows & (EMBEDDED_WINDOW_RIGHT | EMBEDDED_WINDOW_TOPRIGHT | EMBEDDED_WINDOW_BOTTOMRIGHT):
+ self._rightEmbWindow = self._CreateEmbeddedWindow(self, (config.ReadInt("MDIEmbedRightSize", defaultHSize), -1), wx.LAYOUT_VERTICAL, wx.LAYOUT_RIGHT, visible = config.ReadInt("MDIEmbedRightVisible", 1), sash = wx.SASH_LEFT)
+ else:
+ self._rightEmbWindow = None
+ if windows & EMBEDDED_WINDOW_TOPRIGHT:
+ self._topRightEmbWindow = self._CreateEmbeddedWindow(self._rightEmbWindow, (-1, config.ReadInt("MDIEmbedTopRightSize", defaultSubVSize)), wx.LAYOUT_HORIZONTAL, wx.LAYOUT_TOP, visible = config.ReadInt("MDIEmbedTopRightVisible", 1), sash = wx.SASH_BOTTOM)
+ else:
+ self._topRightEmbWindow = None
+ if windows & EMBEDDED_WINDOW_BOTTOMRIGHT:
+ self._bottomRightEmbWindow = self._CreateEmbeddedWindow(self._rightEmbWindow, (-1, config.ReadInt("MDIEmbedBottomRightSize", defaultSubVSize)), wx.LAYOUT_HORIZONTAL, wx.LAYOUT_BOTTOM, visible = config.ReadInt("MDIEmbedBottomRightVisible", 1))
+ else:
+ self._bottomRightEmbWindow = None
+ if windows & EMBEDDED_WINDOW_TOP:
+ self._topEmbWindow = self._CreateEmbeddedWindow(self, (-1, config.ReadInt("MDIEmbedTopSize", defaultVSize)), wx.LAYOUT_HORIZONTAL, wx.LAYOUT_TOP, visible = config.ReadInt("MDIEmbedTopVisible", 1), sash = wx.SASH_BOTTOM)
+ else:
+ self._topEmbWindow = None
+ if windows & EMBEDDED_WINDOW_BOTTOM:
+ self._bottomEmbWindow = self._CreateEmbeddedWindow(self, (-1, config.ReadInt("MDIEmbedBottomSize", defaultVSize)), wx.LAYOUT_HORIZONTAL, wx.LAYOUT_BOTTOM, visible = config.ReadInt("MDIEmbedBottomVisible", 1), sash = wx.SASH_TOP)
+ else:
+ self._bottomEmbWindow = None
+
+
+ def SaveEmbeddedWindowSizes(self):
+ """
+ Saves the sizes of the embedded windows.
+ """
+ config = wx.ConfigBase_Get()
+ if not self.IsMaximized():
+ config.WriteInt("MDIFrameXLoc", self.GetPositionTuple()[0])
+ config.WriteInt("MDIFrameYLoc", self.GetPositionTuple()[1])
+ config.WriteInt("MDIFrameXSize", self.GetSizeTuple()[0])
+ config.WriteInt("MDIFrameYSize", self.GetSizeTuple()[1])
+ config.WriteInt("MDIFrameMaximized", self.IsMaximized())
+ config.WriteInt("ViewToolBar", self._toolBar.IsShown())
+ config.WriteInt("ViewStatusBar", self.GetStatusBar().IsShown())
+
+ if self._leftEmbWindow:
+ config.WriteInt("MDIEmbedLeftSize", self._leftEmbWindow.GetSize()[0])
+ config.WriteInt("MDIEmbedLeftVisible", self._leftEmbWindow.IsShown())
+ if self._topLeftEmbWindow:
+ if self._topLeftEmbWindow._sizeBeforeHidden:
+ size = self._topLeftEmbWindow._sizeBeforeHidden[1]
+ else:
+ size = self._topLeftEmbWindow.GetSize()[1]
+ config.WriteInt("MDIEmbedTopLeftSize", size)
+ config.WriteInt("MDIEmbedTopLeftVisible", self._topLeftEmbWindow.IsShown())
+ if self._bottomLeftEmbWindow:
+ if self._bottomLeftEmbWindow._sizeBeforeHidden:
+ size = self._bottomLeftEmbWindow._sizeBeforeHidden[1]
+ else:
+ size = self._bottomLeftEmbWindow.GetSize()[1]
+ config.WriteInt("MDIEmbedBottomLeftSize", size)
+ config.WriteInt("MDIEmbedBottomLeftVisible", self._bottomLeftEmbWindow.IsShown())
+ if self._rightEmbWindow:
+ config.WriteInt("MDIEmbedRightSize", self._rightEmbWindow.GetSize()[0])
+ config.WriteInt("MDIEmbedRightVisible", self._rightEmbWindow.IsShown())
+ if self._topRightEmbWindow:
+ if self._topRightEmbWindow._sizeBeforeHidden:
+ size = self._topRightEmbWindow._sizeBeforeHidden[1]
+ else:
+ size = self._topRightEmbWindow.GetSize()[1]
+ config.WriteInt("MDIEmbedTopRightSize", size)
+ config.WriteInt("MDIEmbedTopRightVisible", self._topRightEmbWindow.IsShown())
+ if self._bottomRightEmbWindow:
+ if self._bottomRightEmbWindow._sizeBeforeHidden:
+ size = self._bottomRightEmbWindow._sizeBeforeHidden[1]
+ else:
+ size = self._bottomRightEmbWindow.GetSize()[1]
+ config.WriteInt("MDIEmbedBottomRightSize", size)
+ config.WriteInt("MDIEmbedBottomRightVisible", self._bottomRightEmbWindow.IsShown())
+ if self._topEmbWindow:
+ config.WriteInt("MDIEmbedTopSize", self._topEmbWindow.GetSize()[1])
+ config.WriteInt("MDIEmbedTopVisible", self._topEmbWindow.IsShown())
+ if self._bottomEmbWindow:
+ config.WriteInt("MDIEmbedBottomSize", self._bottomEmbWindow.GetSize()[1])
+ config.WriteInt("MDIEmbedBottomVisible", self._bottomEmbWindow.IsShown())
+
+
+ def GetEmbeddedWindow(self, loc):
+ """
+ Returns the instance of the embedded window specified by the embedded window location constant.
+ """
+ if loc == EMBEDDED_WINDOW_TOP:
+ return self._topEmbWindow
+ elif loc == EMBEDDED_WINDOW_BOTTOM:
+ return self._bottomEmbWindow
+ elif loc == EMBEDDED_WINDOW_LEFT:
+ return self._leftEmbWindow
+ elif loc == EMBEDDED_WINDOW_RIGHT:
+ return self._rightEmbWindow
+ elif loc == EMBEDDED_WINDOW_TOPLEFT:
+ return self._topLeftEmbWindow
+ elif loc == EMBEDDED_WINDOW_BOTTOMLEFT:
+ return self._bottomLeftEmbWindow
+ elif loc == EMBEDDED_WINDOW_TOPRIGHT:
+ return self._topRightEmbWindow
+ elif loc == EMBEDDED_WINDOW_BOTTOMRIGHT:
+ return self._bottomRightEmbWindow
+ return None
+
+
+ def _CreateEmbeddedWindow(self, parent, size, orientation, alignment, visible=True, sash=None):
+ """
+ Creates the embedded window with the specified size, orientation, and alignment. If the
+ window is not visible it will retain the size with which it was last viewed.
+ """
+ window = wx.SashLayoutWindow(parent, wx.NewId(), style = wx.NO_BORDER | wx.SW_3D)
+ window.SetDefaultSize(size)
+ window.SetOrientation(orientation)
+ window.SetAlignment(alignment)
+ if sash != None: # wx.SASH_TOP is 0 so check for None instead of just doing "if sash:"
+ window.SetSashVisible(sash, True)
+ ####
+ def OnEmbeddedWindowSashDrag(event):
+ if event.GetDragStatus() == wx.SASH_STATUS_OUT_OF_RANGE:
+ return
+ sashWindow = event.GetEventObject()
+ if sashWindow.GetAlignment() == wx.LAYOUT_TOP or sashWindow.GetAlignment() == wx.LAYOUT_BOTTOM:
+ size = wx.Size(-1, event.GetDragRect().height)
+ else:
+ size = wx.Size(event.GetDragRect().width, -1)
+ event.GetEventObject().SetDefaultSize(size)
+ self._LayoutFrame()
+ sashWindow.Refresh()
+ if isinstance(sashWindow.GetParent(), wx.SashLayoutWindow):
+ sashWindow.Show()
+ parentSashWindow = sashWindow.GetParent() # Force a refresh
+ parentSashWindow.Layout()
+ parentSashWindow.Refresh()
+ parentSashWindow.SetSize((parentSashWindow.GetSize().width + 1, parentSashWindow.GetSize().height + 1))
+ ####
+ wx.EVT_SASH_DRAGGED(window, window.GetId(), OnEmbeddedWindowSashDrag)
+ window._sizeBeforeHidden = None
+ if not visible:
+ window.Show(False)
+ if isinstance(parent, wx.SashLayoutWindow): # It's a window embedded in another sash window so remember its actual size to show it again
+ window._sizeBeforeHidden = size
+ return window
+
+
+ def ShowEmbeddedWindow(self, window, show=True):
+ """
+ Shows or hides the embedded window specified by the embedded window location constant.
+ """
+ window.Show(show)
+ if isinstance(window.GetParent(), wx.SashLayoutWindow): # It is a parent sashwindow with multiple embedded sashwindows
+ parentSashWindow = window.GetParent()
+ if show: # Make sure it is visible in case all of the subwindows were hidden
+ parentSashWindow.Show()
+ if show and window._sizeBeforeHidden:
+ if window._sizeBeforeHidden[1] == parentSashWindow.GetClientSize()[1]:
+ if window == self.GetEmbeddedWindow(EMBEDDED_WINDOW_BOTTOMLEFT) and self.GetEmbeddedWindow(EMBEDDED_WINDOW_TOPLEFT).IsShown():
+ window.SetDefaultSize((window._sizeBeforeHidden[0], window._sizeBeforeHidden[0] - self.GetEmbeddedWindow(EMBEDDED_WINDOW_TOPLEFT).GetSize()[1]))
+ elif window == self.GetEmbeddedWindow(EMBEDDED_WINDOW_TOPLEFT) and self.GetEmbeddedWindow(EMBEDDED_WINDOW_BOTTOMLEFT).IsShown():
+ window.SetDefaultSize((window._sizeBeforeHidden[0], window._sizeBeforeHidden[0] - self.GetEmbeddedWindow(EMBEDDED_WINDOW_BOTTOMLEFT).GetSize()[1]))
+ elif window == self.GetEmbeddedWindow(EMBEDDED_WINDOW_BOTTOMRIGHT) and self.GetEmbeddedWindow(EMBEDDED_WINDOW_TOPRIGHT).IsShown():
+ window.SetDefaultSize((window._sizeBeforeHidden[0], window._sizeBeforeHidden[0] - self.GetEmbeddedWindow(EMBEDDED_WINDOW_TOPRIGHT).GetSize()[1]))
+ elif window == self.GetEmbeddedWindow(EMBEDDED_WINDOW_TOPRIGHT) and self.GetEmbeddedWindow(EMBEDDED_WINDOW_BOTTOMRIGHT).IsShown():
+ window.SetDefaultSize((window._sizeBeforeHidden[0], window._sizeBeforeHidden[0] - self.GetEmbeddedWindow(EMBEDDED_WINDOW_BOTTOMRIGHT).GetSize()[1]))
+ else:
+ window.SetDefaultSize(window._sizeBeforeHidden)
+ # If it is not the size of the full parent sashwindow set the other window's size so that if it gets shown it will have a cooresponding size
+ if window._sizeBeforeHidden[1] < parentSashWindow.GetClientSize()[1]:
+ otherWindowSize = (-1, parentSashWindow.GetClientSize()[1] - window._sizeBeforeHidden[1])
+ if window == self.GetEmbeddedWindow(EMBEDDED_WINDOW_BOTTOMLEFT):
+ self.GetEmbeddedWindow(EMBEDDED_WINDOW_TOPLEFT).SetDefaultSize(otherWindowSize)
+ elif window == self.GetEmbeddedWindow(EMBEDDED_WINDOW_TOPLEFT):
+ self.GetEmbeddedWindow(EMBEDDED_WINDOW_BOTTOMLEFT).SetDefaultSize(otherWindowSize)
+ elif window == self.GetEmbeddedWindow(EMBEDDED_WINDOW_BOTTOMRIGHT):
+ self.GetEmbeddedWindow(EMBEDDED_WINDOW_TOPRIGHT).SetDefaultSize(otherWindowSize)
+ elif window == self.GetEmbeddedWindow(EMBEDDED_WINDOW_TOPRIGHT):
+ self.GetEmbeddedWindow(EMBEDDED_WINDOW_BOTTOMRIGHT).SetDefaultSize(otherWindowSize)
+
+ if not show:
+ if window == self.GetEmbeddedWindow(EMBEDDED_WINDOW_BOTTOMRIGHT) and not self.GetEmbeddedWindow(EMBEDDED_WINDOW_TOPRIGHT).IsShown() \
+ or window == self.GetEmbeddedWindow(EMBEDDED_WINDOW_TOPRIGHT) and not self.GetEmbeddedWindow(EMBEDDED_WINDOW_BOTTOMRIGHT).IsShown() \
+ or window == self.GetEmbeddedWindow(EMBEDDED_WINDOW_BOTTOMLEFT) and not self.GetEmbeddedWindow(EMBEDDED_WINDOW_TOPLEFT).IsShown() \
+ or window == self.GetEmbeddedWindow(EMBEDDED_WINDOW_TOPLEFT) and not self.GetEmbeddedWindow(EMBEDDED_WINDOW_BOTTOMLEFT).IsShown():
+ parentSashWindow.Hide() # Hide the parent sashwindow if all of the children are hidden
+ parentSashWindow.Layout() # Force a refresh
+ parentSashWindow.Refresh()
+ parentSashWindow.SetSize((parentSashWindow.GetSize().width + 1, parentSashWindow.GetSize().height + 1))
+ self._LayoutFrame()
+
+
+ def HideEmbeddedWindow(self):
+ """
+ Hides the embedded window specified by the embedded window location constant.
+ """
+ self.ShowEmbeddedWindow(show = False)
+
+
+class DocTabbedChildFrame(wx.Panel):
+ """
+ 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.Panel.__init__(self, frame.GetNotebook(), id)
+ self._childDocument = doc
+ self._childView = view
+ frame.AddNotebookPage(self, doc.GetPrintableName())
+ if view:
+ view.SetFrame(self)
+
+
+ def GetIcon(self):
+ """
+ Dummy method since the icon of tabbed frames are managed by the notebook.
+ """
+ return None
+
+
+ def SetIcon(self, icon):
+ """
+ Dummy method since the icon of tabbed frames are managed by the notebook.
+ """
+ pass
+
+
+ def Destroy(self):
+ """
+ Removes the current notebook page.
+ """
+ wx.GetApp().GetTopWindow().RemoveNotebookPage(self)
+
+
+ def SetFocus(self):
+ """
+ Activates the current notebook page.
+ """
+ wx.GetApp().GetTopWindow().ActivateNotebookPage(self)
+
+
+ def Activate(self): # Need this in case there are embedded sash windows and such, OnActivate is not getting called
+ """
+ Activates the current view.
+ """
+ # Called by Project Editor
+ if self._childView:
+ self._childView.Activate(True)
+
+
+ def GetTitle(self):
+ """
+ Returns the frame's title.
+ """
+ wx.GetApp().GetTopWindow().GetNotebookPageTitle(self)
+
+
+ def SetTitle(self, title):
+ """
+ Sets the frame's title.
+ """
+ wx.GetApp().GetTopWindow().SetNotebookPageTitle(self, title)
+
+
+ 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 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):
+ return False
+ else:
+ return True
+ else:
+ return True
+
+
+ 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.
+ """
+ return self._childView
+
+
+ def SetView(self, view):
+ """
+ Sets the view for this frame.
+ """
+ self._childView = view
+
+
+class DocTabbedParentFrame(wx.Frame, DocFrameMixIn, DocMDIParentFrameMixIn):
+ """
+ The DocTabbedParentFrame class provides a default top-level frame for
+ applications using the document/view framework. This class can only be
+ used for MDI parent frames that use a tabbed interface.
+
+ It cooperates with the wxView, wxDocument, wxDocManager and wxDocTemplate
+ classes.
+ """
+
+
+ def __init__(self, docManager, frame, id, title, pos = wx.DefaultPosition, size = wx.DefaultSize, style = wx.DEFAULT_FRAME_STYLE, name = "DocTabbedParentFrame", embeddedWindows = 0):
+ """
+ Constructor. Note that the event table must be rebuilt for the
+ frame since the EvtHandler is not virtual.
+ """
+ pos, size = self._GetPosSizeFromConfig(pos, size)
+ wx.Frame.__init__(self, frame, id, title, pos, size, style, name)
+
+ # From docview.MDIParentFrame
+ self._docManager = docManager
+
+ wx.EVT_CLOSE(self, self.OnCloseWindow)
+
+ wx.EVT_MENU(self, wx.ID_EXIT, self.OnExit)
+ wx.EVT_MENU_RANGE(self, wx.ID_FILE1, wx.ID_FILE9, self.OnMRUFile)
+
+ wx.EVT_MENU(self, wx.ID_NEW, self.ProcessEvent)
+ wx.EVT_MENU(self, wx.ID_OPEN, self.ProcessEvent)
+ wx.EVT_MENU(self, wx.ID_CLOSE_ALL, self.ProcessEvent)
+ wx.EVT_MENU(self, wx.ID_CLOSE, self.ProcessEvent)
+ wx.EVT_MENU(self, wx.ID_REVERT, self.ProcessEvent)
+ wx.EVT_MENU(self, wx.ID_SAVE, self.ProcessEvent)
+ wx.EVT_MENU(self, wx.ID_SAVEAS, self.ProcessEvent)
+ wx.EVT_MENU(self, wx.ID_UNDO, self.ProcessEvent)
+ wx.EVT_MENU(self, wx.ID_REDO, self.ProcessEvent)
+ wx.EVT_MENU(self, wx.ID_PRINT, self.ProcessEvent)
+ wx.EVT_MENU(self, wx.ID_PRINT_SETUP, self.ProcessEvent)
+ wx.EVT_MENU(self, wx.ID_PREVIEW, self.ProcessEvent)
+ wx.EVT_MENU(self, wx.ID_ABOUT, self.OnAbout)
+
+ wx.EVT_UPDATE_UI(self, wx.ID_NEW, self.ProcessUpdateUIEvent)
+ wx.EVT_UPDATE_UI(self, wx.ID_OPEN, self.ProcessUpdateUIEvent)
+ wx.EVT_UPDATE_UI(self, wx.ID_CLOSE_ALL, self.ProcessUpdateUIEvent)
+ wx.EVT_UPDATE_UI(self, wx.ID_CLOSE, self.ProcessUpdateUIEvent)
+ wx.EVT_UPDATE_UI(self, wx.ID_REVERT, self.ProcessUpdateUIEvent)
+ wx.EVT_UPDATE_UI(self, wx.ID_SAVE, self.ProcessUpdateUIEvent)
+ wx.EVT_UPDATE_UI(self, wx.ID_SAVEAS, self.ProcessUpdateUIEvent)
+ wx.EVT_UPDATE_UI(self, wx.ID_UNDO, self.ProcessUpdateUIEvent)
+ wx.EVT_UPDATE_UI(self, wx.ID_REDO, self.ProcessUpdateUIEvent)
+ wx.EVT_UPDATE_UI(self, wx.ID_PRINT, self.ProcessUpdateUIEvent)
+ wx.EVT_UPDATE_UI(self, wx.ID_PRINT_SETUP, self.ProcessUpdateUIEvent)
+ wx.EVT_UPDATE_UI(self, wx.ID_PREVIEW, self.ProcessUpdateUIEvent)
+ # End From docview.MDIParentFrame
+
+ self.CreateNotebook()
+ self._InitFrame(embeddedWindows)
+
+
+ def _LayoutFrame(self):
+ """
+ Lays out the frame.
+ """
+ wx.LayoutAlgorithm().LayoutFrame(self, self._notebook)
+
+
+ def CreateNotebook(self):
+ """
+ Creates the notebook to use for the tabbed document interface.
+ """
+ self._notebook = wx.Notebook(self, wx.NewId())
+ # self._notebook.SetSizer(wx.NotebookSizer(self._notebook))
+ wx.EVT_NOTEBOOK_PAGE_CHANGED(self, self._notebook.GetId(), self.OnNotebookPageChanged)
+ wx.EVT_RIGHT_DOWN(self._notebook, self.OnNotebookRightClick)
+ wx.EVT_MOTION(self._notebook, self.OnNotebookMouseOver)
+
+ templates = wx.GetApp().GetDocumentManager().GetTemplates()
+ iconList = wx.ImageList(16, 16, initialCount = len(templates))
+ self._iconIndexLookup = []
+ for template in templates:
+ icon = template.GetIcon()
+ if icon:
+ if icon.GetHeight() != 16 or icon.GetWidth() != 16:
+ icon.SetHeight(16)
+ icon.SetWidth(16)
+ if wx.GetApp().GetDebug():
+ print "Warning: icon for '%s' isn't 16x16, not crossplatform" % template._docTypeName
+ iconIndex = iconList.AddIcon(icon)
+ self._iconIndexLookup.append((template, iconIndex))
+
+ icon = getBlankIcon()
+ if icon.GetHeight() != 16 or icon.GetWidth() != 16:
+ icon.SetHeight(16)
+ icon.SetWidth(16)
+ if wx.GetApp().GetDebug():
+ print "Warning: getBlankIcon isn't 16x16, not crossplatform"
+ self._blankIconIndex = iconList.AddIcon(icon)
+ self._notebook.AssignImageList(iconList)
+
+
+ def GetNotebook(self):
+ """
+ Returns the notebook used by the tabbed document interface.
+ """
+ return self._notebook
+
+
+ def GetActiveChild(self):
+ """
+ Returns the active notebook page, which to the framework is treated as
+ a document frame.
+ """
+ index = self._notebook.GetSelection()
+ if index == -1:
+ return None
+ return self._notebook.GetPage(index)
+
+
+ def OnNotebookPageChanged(self, event):
+ """
+ Activates a notebook page's view when it is selected.
+ """
+ index = self._notebook.GetSelection()
+ if index > -1:
+ self._notebook.GetPage(index).GetView().Activate()
+
+
+ def OnNotebookMouseOver(self, event):
+ # wxBug: On Windows XP the tooltips don't automatically disappear when you move the mouse and it is on a notebook tab, has nothing to do with this code!!!
+ index, type = self._notebook.HitTest(event.GetPosition())
+ if index > -1:
+ doc = self._notebook.GetPage(index).GetView().GetDocument()
+ self._notebook.SetToolTip(wx.ToolTip(doc.GetFilename()))
+ else:
+ self._notebook.SetToolTip(wx.ToolTip(""))
+ event.Skip()
+
+
+ def OnNotebookRightClick(self, event):
+ """
+ Handles right clicks for the notebook, enabling users to either close
+ a tab or select from the available documents if the user clicks on the
+ notebook's white space.
+ """
+ index, type = self._notebook.HitTest(event.GetPosition())
+ menu = wx.Menu()
+ x, y = event.GetX(), event.GetY()
+ if index > -1:
+ doc = self._notebook.GetPage(index).GetView().GetDocument()
+ id = wx.NewId()
+ menu.Append(id, _("Close"))
+ def OnRightMenuSelect(event):
+ doc.DeleteAllViews()
+ wx.EVT_MENU(self, id, OnRightMenuSelect)
+ if self._notebook.GetPageCount() > 1:
+ id = wx.NewId()
+ menu.Append(id, _("Close All but \"%s\"" % doc.GetPrintableName()))
+ def OnRightMenuSelect(event):
+ for i in range(self._notebook.GetPageCount()-1, -1, -1): # Go from len-1 to 0
+ if i != index:
+ doc = self._notebook.GetPage(i).GetView().GetDocument()
+ if not self.GetDocumentManager().CloseDocument(doc, False):
+ return
+ wx.EVT_MENU(self, id, OnRightMenuSelect)
+ menu.AppendSeparator()
+ tabsMenu = wx.Menu()
+ menu.AppendMenu(wx.NewId(), _("Select Tab"), tabsMenu)
+ else:
+ y = y - 25 # wxBug: It is offsetting click events in the blank notebook area
+ tabsMenu = menu
+
+ if self._notebook.GetPageCount() > 1:
+ selectIDs = {}
+ for i in range(0, self._notebook.GetPageCount()):
+ id = wx.NewId()
+ selectIDs[id] = i
+ tabsMenu.Append(id, self._notebook.GetPageText(i))
+ def OnRightMenuSelect(event):
+ self._notebook.SetSelection(selectIDs[event.GetId()])
+ wx.EVT_MENU(self, id, OnRightMenuSelect)
+
+ self._notebook.PopupMenu(menu, wx.Point(x, y))
+ menu.Destroy()
+
+
+ def AddNotebookPage(self, panel, title):
+ """
+ Adds a document page to the notebook.
+ """
+ self._notebook.AddPage(panel, title)
+ index = self._notebook.GetPageCount() - 1
+ self._notebook.SetSelection(index)
+
+ found = False # Now set the icon
+ template = panel.GetDocument().GetDocumentTemplate()
+ if template:
+ for t, iconIndex in self._iconIndexLookup:
+ if t is template:
+ self._notebook.SetPageImage(index, iconIndex)
+ found = True
+ break
+ if not found:
+ self._notebook.SetPageImage(index, self._blankIconIndex)
+ self._notebook.Layout()
+
+
+ def RemoveNotebookPage(self, panel):
+ """
+ Removes a document page from the notebook.
+ """
+ index = self.GetNotebookPageIndex(panel)
+ if index > -1:
+ self._notebook.DeletePage(index)
+
+
+ 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):
+ self._notebook.GetPageText(self.GetNotebookPageIndex(panel))
+
+
+ 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 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 GetServices(self):
+
+ def GetDocument(self):
"""
- Returns the DocService instances that have been installed into the DocApp.
+ Returns the document associated with this frame.
"""
- return self._services
+ return self._childDocument
- def GetService(self, type):
+ def SetDocument(self, document):
"""
- Returns the instance of a particular type of service that has been installed
- into the DocApp. For example, "wx.GetApp().GetService(pydocview.OptionsService)"
- returns the isntance of the OptionsService that is running within the DocApp.
+ Sets the document for this frame.
"""
- for service in self._services:
- if isinstance(service, type):
- return service
- return None
+ self._childDocument = document
+
+
+ def GetView(self):
+ """
+ Returns the view associated with this frame.
+ """
+ return self._childView
+
+
+ def SetView(self, view):
+ """
+ Sets the view for this frame.
+ """
+ self._childView = view
+
+
+
+
+
+class DocService(wx.EvtHandler):
+ """
+ An abstract class used to add reusable services to a docview application.
+ """
+
+
+ def __init__(self):
+ """Initializes the DocService."""
+ pass
+
+
+ def GetDocumentManager(self):
+ """Returns the DocManager for the docview application."""
+ return self._docManager
+
+
+ def SetDocumentManager(self, docManager):
+ """Sets the DocManager for the docview application."""
+ self._docManager = docManager
+
+
+ def InstallControls(self, frame, menuBar=None, toolBar=None, statusBar=None, document=None):
+ """Called to install controls into the menubar and toolbar of a SDI or MDI window. Override this method for a particular service."""
+ pass
+
+
+ def ProcessEventBeforeWindows(self, event):
+ """
+ Processes an event before the main window has a chance to process the window.
+ Override this method for a particular service.
+ """
+ return False
+
+
+ def ProcessUpdateUIEventBeforeWindows(self, event):
+ """
+ Processes a UI event before the main window has a chance to process the window.
+ Override this method for a particular service.
+ """
+ return False
+
+
+ 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.
+ """
+ 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.
+ """
+ return False
+
+
+ def OnCloseFrame(self, event):
+ """
+ Called when the a docview frame is being closed. Override this method
+ so a service can either do cleanup or veto the frame being closed by
+ returning false.
+ """
+ return True
def OnExit(self):
"""
- Called when the DocApp is exited, enables the installed DocServices to exit
- and saves the DocManager's file history.
+ Called when the the docview application is being closed. Override this method
+ so a service can either do cleanup or veto the frame being closed by
+ returning false.
"""
- for service in self._services:
- service.OnExit()
- config = wx.ConfigBase_Get()
- self._docManager.FileHistorySave(config)
+ pass
-
- def GetDefaultDocManagerFlags(self):
+
+ def GetMenuItemPos(self, menu, id):
"""
- Returns the default flags to use when creating the DocManager.
+ Utility method used to find the position of a menu item so that services can
+ easily find where to insert a menu item in InstallControls.
"""
- config = wx.ConfigBase_Get()
- if config.ReadInt("UseMDI", True):
- flags = wx.lib.docview.DOC_MDI | wx.lib.docview.DOC_OPEN_ONCE
+ menuItems = menu.GetMenuItems()
+ for i, menuItem in enumerate(menuItems):
+ if menuItem.GetId() == id:
+ return i
+ return i
+
+
+ def GetView(self):
+ """
+ Called by WindowMenuService to get views for services that don't
+ have dedicated documents such as the Outline Service.
+ """
+ return None
+
+
+class DocOptionsService(DocService):
+ """
+ A service that implements an options menu item and an options dialog with
+ notebook tabs. New tabs can be added by other services by calling the
+ "AddOptionsPanel" method.
+ """
+
+
+ def __init__(self, showGeneralOptions=True, supportedModes=wx.lib.docview.DOC_SDI & wx.lib.docview.DOC_MDI):
+ """
+ Initializes the options service with the option of suppressing the default
+ general options pane that is included with the options service by setting
+ showGeneralOptions to False. It allowModeChanges is set to False, the
+ default general options pane will allow users to change the document
+ interface mode between SDI and MDI modes.
+ """
+ DocService.__init__(self)
+ self.ClearOptionsPanels()
+ self._supportedModes = supportedModes
+ self._toolOptionsID = wx.NewId()
+ if showGeneralOptions:
+ self.AddOptionsPanel(GeneralOptionsPanel)
+
+
+ def InstallControls(self, frame, menuBar=None, toolBar=None, statusBar=None, document=None):
+ """
+ Installs a "Tools" menu with an "Options" menu item.
+ """
+ toolsMenuIndex = menuBar.FindMenu(_("&Tools"))
+ if toolsMenuIndex > -1:
+ toolsMenu = menuBar.GetMenu(toolsMenuIndex)
else:
- flags = wx.lib.docview.DOC_SDI | wx.lib.docview.DOC_OPEN_ONCE
- return flags
+ toolsMenu = wx.Menu()
+ if toolsMenuIndex == -1:
+ formatMenuIndex = menuBar.FindMenu(_("&Format"))
+ menuBar.Insert(formatMenuIndex + 1, toolsMenu, _("&Tools"))
+ if toolsMenu:
+ if toolsMenu.GetMenuItemCount():
+ toolsMenu.AppendSeparator()
+ toolsMenu.Append(self._toolOptionsID, _("&Options..."), _("Sets options"))
+ wx.EVT_MENU(frame, self._toolOptionsID, frame.ProcessEvent)
+
+
+ def ProcessEvent(self, event):
+ """
+ Checks to see if the "Options" menu item has been selected.
+ """
+ id = event.GetId()
+ if id == self._toolOptionsID:
+ self.OnOptions(event)
+ return True
+ else:
+ return False
- def ShowTip(self, frame, tipProvider):
+ def GetSupportedModes(self):
"""
- Shows the tip window, generally this is called when an application starts.
- A wx.TipProvider must be passed.
+ Return the modes supported by the application. Use docview.DOC_SDI and
+ docview.DOC_MDI flags to check if SDI and/or MDI modes are supported.
"""
- config = wx.ConfigBase_Get()
- showTip = config.ReadInt("ShowTipAtStartup", 1)
- if showTip:
- index = config.ReadInt("TipIndex", 0)
- showTipResult = wx.ShowTip(frame, tipProvider, showAtStartup = showTip)
- if showTipResult != showTip:
- config.WriteInt("ShowTipAtStartup", showTipResult)
+ return self._supportedModes
- def GetEditMenu(self, frame):
+ def SetSupportedModes(self, _supportedModessupportedModes):
"""
- Utility method that finds the Edit menu within the menubar of a frame.
+ Sets the modes supported by the application. Use docview.DOC_SDI and
+ docview.DOC_MDI flags to set if SDI and/or MDI modes are supported.
"""
- menuBar = frame.GetMenuBar()
- if not menuBar:
- return None
- editMenuIndex = menuBar.FindMenu(_("&Edit"))
- if editMenuIndex == -1:
- return None
- return menuBar.GetMenu(editMenuIndex)
+ self._supportedModes = supportedModes
+
+
+ def ClearOptionsPanels(self):
+ """
+ Clears all of the options panels that have been added into the
+ options dialog.
+ """
+ self._optionsPanels = []
+
+
+ def AddOptionsPanel(self, optionsPanel):
+ """
+ Adds an options panel to the options dialog.
+ """
+ self._optionsPanels.append(optionsPanel)
+
+
+ def OnOptions(self, event):
+ """
+ Shows the options dialog, called when the "Options" menu item is selected.
+ """
+ if len(self._optionsPanels) == 0:
+ return
+ optionsDialog = OptionsDialog(wx.GetApp().GetTopWindow(), self._optionsPanels, self._docManager)
+ if optionsDialog.ShowModal() == wx.ID_OK:
+ optionsDialog.OnOK(optionsDialog) # wxBug: wxDialog should be calling this automatically but doesn't
+ optionsDialog.Destroy()
+
+
+class OptionsDialog(wx.Dialog):
+ """
+ A default options dialog used by the OptionsService that hosts a notebook
+ tab of options panels.
+ """
+
+
+ def __init__(self, parent, optionsPanelClasses, docManager):
+ """
+ Initializes the options dialog with a notebook page that contains new
+ instances of the passed optionsPanelClasses.
+ """
+ wx.Dialog.__init__(self, parent, -1, _("Options"), size = (570, 365))
+
+ self._optionsPanels = []
+ self._docManager = docManager
+
+ HALF_SPACE = 5
+ SPACE = 10
+
+ sizer = wx.BoxSizer(wx.VERTICAL)
+
+ optionsNotebook = wx.Notebook(self, -1, size=(560, 325))
+ sizer.Add(optionsNotebook, 0, wx.ALL | wx.EXPAND, SPACE)
+ for optionsPanelClass in optionsPanelClasses:
+ optionsPanel = optionsPanelClass(optionsNotebook, -1)
+ self._optionsPanels.append(optionsPanel)
+ sizer.Add(self.CreateButtonSizer(wx.OK | wx.CANCEL), 0, wx.ALIGN_RIGHT | wx.RIGHT | wx.BOTTOM, HALF_SPACE)
+ self.SetSizer(sizer)
+ self.Layout()
+ if wx.Platform != '__WXMAC__' or len(optionsPanelClasses) < 6: # wxBug: Notebook tabs are truncated and user can't get to them on the Mac
+ self.Fit()
+ wx.CallAfter(self.DoRefresh)
- def CreateDocumentFrame(self, view, doc, flags, id = -1, title = "", pos = wx.DefaultPosition, size = wx.DefaultSize, style = wx.DEFAULT_FRAME_STYLE):
+ def DoRefresh(self):
"""
- Called by the DocManager to create and return a new Frame for a Document.
- Chooses whether to create an MDIChildFrame or SDI Frame based on the
- DocManager's flags.
+ wxBug: On Windows XP when using a multiline notebook the default page doesn't get
+ drawn, but it works when using a single line notebook.
"""
- docflags = self.GetDocumentManager().GetFlags()
- if docflags & wx.lib.docview.DOC_SDI:
- frame = self.CreateSDIDocumentFrame(doc, view, id, title, pos, size, style)
- frame.Show()
-
- # wxBug: operating system bug, first window is set to the position of last window closed, ignoring passed in position on frame creation
- # also, initial size is incorrect for the same reasons
- if frame.GetPosition() != pos:
- frame.Move(pos)
- if frame.GetSize() != size:
- frame.SetSize(size)
-
- if doc and doc.GetCommandProcessor():
- doc.GetCommandProcessor().SetEditMenu(self.GetEditMenu(frame))
- elif docflags & wx.lib.docview.DOC_MDI:
- frame = self.CreateMDIDocumentFrame(doc, view, id, title, pos, size, style)
- if doc and doc.GetDocumentTemplate().GetIcon():
- frame.SetIcon(doc.GetDocumentTemplate().GetIcon())
- if doc and doc.GetCommandProcessor():
- doc.GetCommandProcessor().SetEditMenu(self.GetEditMenu(wx.GetApp().GetTopWindow()))
- if not frame.GetIcon() and self._defaultIcon:
- frame.SetIcon(self.GetDefaultIcon())
- view.SetFrame(frame)
- return frame
+ self.Refresh()
- def CreateSDIDocumentFrame(self, doc, view, id = -1, title = "", pos = wx.DefaultPosition, size = wx.DefaultSize, style = wx.DEFAULT_FRAME_STYLE):
+ def GetDocManager(self):
"""
- Creates and returns an SDI Document Frame.
+ Returns the document manager passed to the OptionsDialog constructor.
"""
- frame = DocSDIFrame(doc, view, None, id, title, pos, size, style)
- return frame
+ return self._docManager
- def CreateMDIDocumentFrame(self, doc, view, id = -1, title = "", pos = wx.DefaultPosition, size = wx.DefaultSize, style = wx.DEFAULT_FRAME_STYLE):
+ def OnOK(self, event):
"""
- Creates and returns an MDI Document Frame.
+ Calls the OnOK method of all of the OptionDialog's embedded panels
"""
- # if any child windows are maximized, then user must want any new children maximized
- # if no children exist, then use the default value from registry
- # wxBug: Only current window is maximized, so need to check every child frame
- parentFrame = wx.GetApp().GetTopWindow()
- childrenMaximized = filter(lambda child: isinstance(child, wx.MDIChildFrame) and child.IsMaximized(), parentFrame.GetChildren())
- if childrenMaximized:
- maximize = True
- else:
- children = filter(lambda child: isinstance(child, wx.MDIChildFrame), parentFrame.GetChildren())
- if children:
- # other windows exist and none are maximized
- maximize = False
- else:
- # get default setting from registry
- maximize = wx.ConfigBase_Get().ReadInt("MDIChildFrameMaximized", False)
-
- frame = wx.lib.docview.DocMDIChildFrame(doc, view, wx.GetApp().GetTopWindow(), id, title, pos, size, style)
- if maximize: # wxBug: Should already be maximizing new child frames if one is maximized but it's not so we have to force it to
- frame.Maximize(True)
+ for optionsPanel in self._optionsPanels:
+ optionsPanel.OnOK(event)
-## wx.EVT_MAXIMIZE(frame, self.OnMaximize) # wxBug: This doesn't work, need to save MDIChildFrameMaximized state on close of windows instead
- wx.EVT_CLOSE(frame, self.OnCloseChildWindow)
- if not self._registeredCloseEvent:
- wx.EVT_CLOSE(parentFrame, self.OnCloseMainWindow) # need to check on this, but only once
- self._registeredCloseEvent = True
- return frame
+class GeneralOptionsPanel(wx.Panel):
+ """
+ A general options panel that is used in the OptionDialog to configure the
+ generic properties of a pydocview application, such as "show tips at startup"
+ and whether to use SDI or MDI for the application.
+ """
- def SaveMDIDocumentFrameMaximizedState(self, maximized):
+ def __init__(self, parent, id):
"""
- Remember in the config whether the MDI Frame is maximized so that it can be restored
- on open.
+ Initializes the panel by adding an "Options" folder tab to the parent notebook and
+ populating the panel with the generic properties of a pydocview application.
"""
+ wx.Panel.__init__(self, parent, id)
+ SPACE = 10
+ HALF_SPACE = 5
config = wx.ConfigBase_Get()
- maximizeFlag = config.ReadInt("MDIChildFrameMaximized", False)
- if maximized != maximizeFlag:
- config.WriteInt("MDIChildFrameMaximized", maximized)
+ self._showTipsCheckBox = wx.CheckBox(self, -1, _("Show tips at start up"))
+ self._showTipsCheckBox.SetValue(config.ReadInt("ShowTipAtStartup", True))
+ if self._AllowModeChanges():
+ supportedModes = wx.GetApp().GetService(DocOptionsService).GetSupportedModes()
+ choices = []
+ self._sdiChoice = _("Show each document in its own window")
+ self._mdiChoice = _("Show all documents in a single window with tabs")
+ self._winMdiChoice = _("Show all documents in a single window with child windows")
+ if supportedModes & wx.lib.docview.DOC_SDI:
+ choices.append(self._sdiChoice)
+ choices.append(self._mdiChoice)
+ if wx.Platform == "__WXMSW__":
+ choices.append(self._winMdiChoice)
+ self._documentRadioBox = wx.RadioBox(self, -1, _("Document Interface"),
+ choices = choices,
+ majorDimension=1,
+ )
+ if config.ReadInt("UseWinMDI", False):
+ self._documentRadioBox.SetStringSelection(self._winMdiChoice)
+ elif config.ReadInt("UseMDI", True):
+ self._documentRadioBox.SetStringSelection(self._mdiChoice)
+ else:
+ self._documentRadioBox.SetStringSelection(self._sdiChoice)
+ def OnDocumentInterfaceSelect(event):
+ if not self._documentInterfaceMessageShown:
+ msgTitle = wx.GetApp().GetAppName()
+ if not msgTitle:
+ msgTitle = _("Document Options")
+ wx.MessageBox(_("Document interface changes will not appear until the application is restarted."),
+ msgTitle,
+ wx.OK | wx.ICON_INFORMATION,
+ self.GetParent())
+ self._documentInterfaceMessageShown = True
+ wx.EVT_RADIOBOX(self, self._documentRadioBox.GetId(), OnDocumentInterfaceSelect)
+ optionsBorderSizer = wx.BoxSizer(wx.VERTICAL)
+ optionsSizer = wx.BoxSizer(wx.VERTICAL)
+ if self._AllowModeChanges():
+ optionsSizer.Add(self._documentRadioBox, 0, wx.ALL, HALF_SPACE)
+ optionsSizer.Add(self._showTipsCheckBox, 0, wx.ALL, HALF_SPACE)
+ optionsBorderSizer.Add(optionsSizer, 0, wx.ALL, SPACE)
+ self.SetSizer(optionsBorderSizer)
+ self.Layout()
+ self._documentInterfaceMessageShown = False
+ parent.AddPage(self, _("Options"))
- def OnCloseChildWindow(self, event):
- """
- Called when an MDI Child Frame is closed. Calls SaveMDIDocumentFrameMaximizedState to
- remember whether the MDI Frame is maximized so that it can be restored on open.
- """
- self.SaveMDIDocumentFrameMaximizedState(event.GetEventObject().IsMaximized())
- event.Skip()
+ def _AllowModeChanges(self):
+ supportedModes = wx.GetApp().GetService(DocOptionsService).GetSupportedModes()
+ return supportedModes & wx.lib.docview.DOC_SDI and supportedModes & wx.lib.docview.DOC_MDI or wx.Platform == "__WXMSW__" and supportedModes & wx.lib.docview.DOC_MDI # More than one mode is supported, allow selection
- def OnCloseMainWindow(self, event):
+ def OnOK(self, optionsDialog):
"""
- Called when the MDI Parent Frame is closed. Remembers whether the MDI Parent Frame is
- maximized.
+ Updates the config based on the selections in the options panel.
"""
- children = event.GetEventObject().GetChildren()
- childrenMaximized = filter(lambda child: isinstance(child, wx.MDIChildFrame)and child.IsMaximized(), children)
- if childrenMaximized:
- self.SaveMDIDocumentFrameMaximizedState(True)
- else:
- childrenNotMaximized = filter(lambda child: isinstance(child, wx.MDIChildFrame), children)
+ config = wx.ConfigBase_Get()
+ config.WriteInt("ShowTipAtStartup", self._showTipsCheckBox.GetValue())
+ if self._AllowModeChanges():
+ config.WriteInt("UseMDI", (self._documentRadioBox.GetStringSelection() == self._mdiChoice))
+ config.WriteInt("UseWinMDI", (self._documentRadioBox.GetStringSelection() == self._winMdiChoice))
- if childrenNotMaximized:
- # other windows exist and none are maximized
- self.SaveMDIDocumentFrameMaximizedState(False)
- event.Skip()
+class DocApp(wx.PySimpleApp):
+ """
+ The DocApp class serves as the base class for pydocview applications and offers
+ functionality such as services, creation of SDI and MDI frames, show tips,
+ and a splash screen.
+ """
- def GetDefaultIcon(self):
+ def OnInit(self):
"""
- Returns the application's default icon.
+ Initializes the DocApp.
"""
- return self._defaultIcon
+ self._services = []
+ self._defaultIcon = None
+ self._registeredCloseEvent = False
+ self._useTabbedMDI = True
+ if not hasattr(self, "_debug"): # only set if not already initialized
+ self._debug = False
+ if not hasattr(self, "_singleInstance"): # only set if not already initialized
+ self._singleInstance = True
+
+ # if _singleInstance is TRUE only allow one single instance of app to run.
+ # When user tries to run a second instance of the app, abort startup,
+ # But if user also specifies files to open in command line, send message to running app to open those files
+ if self._singleInstance:
+ # create shared memory temporary file
+ if wx.Platform == '__WXMSW__':
+ tfile = tempfile.TemporaryFile(prefix="ag", suffix="tmp")
+ fno = tfile.fileno()
+ self._sharedMemory = mmap.mmap(fno, 1024, "shared_memory")
+ else:
+ tfile = file(os.path.join(tempfile.gettempdir(), tempfile.gettempprefix() + self.GetAppName() + '-' + wx.GetUserId() + "AGSharedMemory"), 'w+b')
+ tfile.write("*")
+ tfile.seek(1024)
+ tfile.write(" ")
+ tfile.flush()
+ fno = tfile.fileno()
+ self._sharedMemory = mmap.mmap(fno, 1024)
- def SetDefaultIcon(self, icon):
- """
- Sets the application's default icon.
- """
- self._defaultIcon = icon
+ self._singleInstanceChecker = wx.SingleInstanceChecker(self.GetAppName() + '-' + wx.GetUserId(), tempfile.gettempdir())
+ if self._singleInstanceChecker.IsAnotherRunning():
+ # have running single instance open file arguments
+ data = pickle.dumps(sys.argv[1:])
+ while 1:
+ self._sharedMemory.seek(0)
+ marker = self._sharedMemory.read_byte()
+ if marker == '\0' or marker == '*': # available buffer
+ self._sharedMemory.seek(0)
+ self._sharedMemory.write_byte('-') # set writing marker
+ self._sharedMemory.write(data) # write files we tried to open to shared memory
+ self._sharedMemory.seek(0)
+ self._sharedMemory.write_byte('+') # set finished writing marker
+ self._sharedMemory.flush()
+ break
+ else:
+ time.sleep(1) # give enough time for buffer to be available
+
+ return False
+ else:
+ self._timer = wx.PyTimer(self.DoBackgroundListenAndLoad)
+ self._timer.Start(250)
+ return True
+
- def GetDebug(self):
+ def OpenMainFrame(self):
+ docManager = self.GetDocumentManager()
+ if docManager.GetFlags() & wx.lib.docview.DOC_MDI:
+ if self.GetUseTabbedMDI():
+ frame = wx.lib.pydocview.DocTabbedParentFrame(docManager, None, -1, self.GetAppName())
+ else:
+ frame = wx.lib.pydocview.DocMDIParentFrame(docManager, None, -1, self.GetAppName())
+ frame.Show(True)
+
+
+ def DoBackgroundListenAndLoad(self):
"""
- Returns True if the application is in debug mode.
+ Open any files specified in the given command line argument passed in via shared memory
"""
- return self._debug
+ self._timer.Stop()
+
+ self._sharedMemory.seek(0)
+ if self._sharedMemory.read_byte() == '+': # available data
+ data = self._sharedMemory.read(1024-1)
+ self._sharedMemory.seek(0)
+ self._sharedMemory.write_byte("*") # finished reading, set buffer free marker
+ self._sharedMemory.flush()
+ args = pickle.loads(data)
+ for arg in args:
+ if arg[0] != '/' and arg[0] != '-' and os.path.exists(arg):
+ self.GetDocumentManager().CreateDocument(os.path.normpath(arg), wx.lib.docview.DOC_SILENT)
+
+ # force display of running app
+ topWindow = wx.GetApp().GetTopWindow()
+ if topWindow.IsIconized():
+ topWindow.Iconize(False)
+ else:
+ topWindow.Raise()
+
+
+ self._timer.Start(1000) # 1 second interval
- def SetDebug(self, debug):
+ def OpenCommandLineArgs(self):
"""
- Sets the application's debug mode.
+ Called to open files that have been passed to the application from the
+ command line.
"""
- self._debug = debug
+ args = sys.argv[1:]
+ for arg in args:
+ if arg[0] != '/' and arg[0] != '-' and os.path.exists(arg):
+ self.GetDocumentManager().CreateDocument(os.path.normpath(arg), wx.lib.docview.DOC_SILENT)
- def GetSingleInstance(self):
+ def GetDocumentManager(self):
"""
- Returns True if the application is in single instance mode. Used to determine if multiple instances of the application is allowed to launch.
+ Returns the document manager associated to the DocApp.
"""
- return self._singleInstance
+ return self._docManager
- def SetSingleInstance(self, singleInstance):
+ def SetDocumentManager(self, docManager):
"""
- Sets application's single instance mode.
+ Sets the document manager associated with the DocApp and loads the
+ DocApp's file history into the document manager.
"""
- self._singleInstance = singleInstance
-
+ self._docManager = docManager
+ config = wx.ConfigBase_Get()
+ self.GetDocumentManager().FileHistoryLoad(config)
- def CreateChildDocument(self, parentDocument, documentType, objectToEdit, path = ''):
+ def ProcessEventBeforeWindows(self, event):
"""
- Creates a child window of a document that edits an object. The child window
- is managed by the parent document frame, so it will be prompted to close if its
- parent is closed, etc. Child Documents are useful when there are complicated
- Views of a Document and users will need to tunnel into the View.
+ Enables services to process an event before the main window has a chance to
+ process the window.
"""
- for document in self.GetDocumentManager().GetDocuments()[:]: # Cloning list to make sure we go through all docs even as they are deleted
- if isinstance(document, ChildDocument) and document.GetParentDocument() == parentDocument:
- if document.GetData() == objectToEdit:
- if hasattr(document.GetFirstView().GetFrame(), "SetFocus"):
- document.GetFirstView().GetFrame().SetFocus()
- return document
- for temp in wx.GetApp().GetDocumentManager().GetTemplates():
- if temp.GetDocumentType() == documentType:
- break
- temp = None
- newDoc = temp.CreateDocument(path, 0, data = objectToEdit, parentDocument = parentDocument)
- newDoc.SetDocumentName(temp.GetDocumentName())
- newDoc.SetDocumentTemplate(temp)
- if path == '':
- newDoc.OnNewDocument()
- else:
- if not newDoc.OnOpenDocument(path):
- newDoc.DeleteAllViews() # Implicitly deleted by DeleteAllViews
- return None
- return newDoc
+ for service in self._services:
+ if service.ProcessEventBeforeWindows(event):
+ return True
+ return False
- def CloseChildDocuments(self, parentDocument):
+ def ProcessUpdateUIEventBeforeWindows(self, event):
"""
- Closes the child windows of a Document.
+ Enables services to process a UI event before the main window has a chance
+ to process the window.
"""
- for document in self.GetDocumentManager().GetDocuments()[:]: # Cloning list to make sure we go through all docs even as they are deleted
- if isinstance(document, ChildDocument) and document.GetParentDocument() == parentDocument:
- if document.GetFirstView().GetFrame():
- document.GetFirstView().GetFrame().SetFocus()
- if document.GetFirstView().OnClose(deleteWindow = False):
- if document.GetFirstView().GetFrame():
- document.GetFirstView().GetFrame().Close() # wxBug: Need to do this for some random reason
- else:
- return False
- return True
+ for service in self._services:
+ if service.ProcessUpdateUIEventBeforeWindows(event):
+ return True
+ return False
- def IsMDI(self):
+ def ProcessEvent(self, event):
"""
- Returns True if the application is in MDI mode.
+ 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.
"""
- return self.GetDocumentManager().GetFlags() & wx.lib.docview.DOC_MDI
+ for service in self._services:
+ if service.ProcessEvent(event):
+ return True
+ return False
- def IsSDI(self):
+ def ProcessUpdateUIEvent(self, event):
"""
- Returns True if the application is in SDI mode.
+ 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.
"""
- return self.GetDocumentManager().GetFlags() & wx.lib.docview.DOC_SDI
+ for service in self._services:
+ if service.ProcessUpdateUIEvent(event):
+ return True
+ return False
- def ShowSplash(self, image):
+ def InstallService(self, service):
"""
- Shows a splash window with the given image. Input parameter 'image' can either be a wx.Bitmap or a filename.
+ Installs an instance of a DocService into the DocApp.
"""
- if isinstance(image, wx.Bitmap):
- splash_bmp = image
- else:
- splash_bmp = wx.Image(image).ConvertToBitmap()
- self._splash = wx.SplashScreen(splash_bmp,wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_NO_TIMEOUT,0, None, -1)
- self._splash.Show()
+ service.SetDocumentManager(self._docManager)
+ self._services.append(service)
+ return service
- def CloseSplash(self):
+ def GetServices(self):
"""
- Closes the splash window.
+ Returns the DocService instances that have been installed into the DocApp.
"""
- if self._splash:
- self._splash.Close(True)
-
-
-class _DocFrameFileDropTarget(wx.FileDropTarget):
- """
- Class used to handle drops into the document frame.
- """
+ return self._services
- def __init__(self, docManager, docFrame):
+
+ def GetService(self, type):
"""
- Initializes the FileDropTarget class with the active docManager and the docFrame.
+ Returns the instance of a particular type of service that has been installed
+ into the DocApp. For example, "wx.GetApp().GetService(pydocview.OptionsService)"
+ returns the isntance of the OptionsService that is running within the DocApp.
"""
- wx.FileDropTarget.__init__(self)
- self._docManager = docManager
- self._docFrame = docFrame
+ for service in self._services:
+ if isinstance(service, type):
+ return service
+ return None
- def OnDropFiles(self, x, y, filenames):
+ def OnExit(self):
"""
- Called when files are dropped in the drop target and tells the docManager to open
- the files.
+ Called when the DocApp is exited, enables the installed DocServices to exit
+ and saves the DocManager's file history.
"""
- try:
- for file in filenames:
- self._docManager.CreateDocument(file, wx.lib.docview.DOC_SILENT)
- except:
- msgTitle = wx.GetApp().GetAppName()
- if not msgTitle:
- msgTitle = _("File Error")
- wx.MessageBox("Could not open '%s'. '%s'" % (docview.FileNameFromPath(file), sys.exc_value),
- msgTitle,
- wx.OK | wx.ICON_EXCLAMATION,
- self._docManager.FindSuitableParent())
-
+ for service in self._services:
+ service.OnExit()
+ config = wx.ConfigBase_Get()
+ self._docManager.FileHistorySave(config)
+
+ if hasattr(self, "_singleInstanceChecker"):
+ del self._singleInstanceChecker
-class DocMDIParentFrame(wx.lib.docview.DocMDIParentFrame):
- """
- The DocMDIParentFrame is the primary frame which the DocApp uses to host MDI child windows. It offers
- features such as a default menubar, toolbar, and status bar, and a mechanism to manage embedded windows
- on the edges of the DocMDIParentFrame.
- """
-
- def __init__(self, docManager, parent, id, title, pos = wx.DefaultPosition, size = wx.DefaultSize, style = wx.DEFAULT_FRAME_STYLE, name = "DocMDIFrame", embeddedWindows = 0):
+ def GetDefaultDocManagerFlags(self):
"""
- Initializes the DocMDIParentFrame with the default menubar, toolbar, and status bar. Use the
- optional embeddedWindows parameter with the embedded window constants to create embedded
- windows around the edges of the DocMDIParentFrame.
+ Returns the default flags to use when creating the DocManager.
"""
config = wx.ConfigBase_Get()
- if pos == wx.DefaultPosition and size == wx.DefaultSize and config.ReadInt("MDIFrameMaximized", False):
- pos = [0, 0]
- size = wx.DisplaySize()
- # wxBug: Need to set to fill screen to get around bug where maximize is leaving shadow of statusbar, check out maximize call at end of this function
+ if config.ReadInt("UseMDI", True) or config.ReadInt("UseWinMDI", False):
+ flags = wx.lib.docview.DOC_MDI | wx.lib.docview.DOC_OPEN_ONCE
+ if config.ReadInt("UseWinMDI", False):
+ self.SetUseTabbedMDI(False)
else:
- if pos == wx.DefaultPosition:
- pos = config.ReadInt("MDIFrameXLoc", -1), config.ReadInt("MDIFrameYLoc", -1)
-
- if wx.Display_GetFromPoint(pos) == -1: # Check if the frame position is offscreen
- pos = wx.DefaultPosition
-
- if size == wx.DefaultSize:
- size = wx.Size(config.ReadInt("MDIFrameXSize", 450), config.ReadInt("MDIFrameYSize", 300))
-
- wx.lib.docview.DocMDIParentFrame.__init__(self, docManager, parent, id, title, pos, size, style, name)
- self._embeddedWindows = []
- self.SetDropTarget(_DocFrameFileDropTarget(docManager, self))
-
- if wx.GetApp().GetDefaultIcon():
- self.SetIcon(wx.GetApp().GetDefaultIcon())
-
- wx.EVT_MENU(self, wx.ID_ABOUT, self.OnAbout)
- wx.EVT_SIZE(self, self.OnSize)
-
- self.InitializePrintData()
-
- toolBar = self.CreateDefaultToolBar()
- self.SetToolBar(toolBar)
- menuBar = self.CreateDefaultMenuBar()
- statusBar = self.CreateDefaultStatusBar()
-
- if config.ReadInt("MDIFrameMaximized", False):
- # wxBug: On maximize, statusbar leaves a residual that needs to be refereshed, happens even when user does it
- self.Maximize()
-
- self.CreateEmbeddedWindows(embeddedWindows)
-
- wx.GetApp().SetTopWindow(self) # Need to do this here in case the services are looking for wx.GetApp().GetTopWindow()
- for service in wx.GetApp().GetServices():
- service.InstallControls(self, menuBar = menuBar, toolBar = toolBar, statusBar = statusBar)
- if hasattr(service, "ShowWindow"):
- service.ShowWindow() # instantiate service windows for correct positioning, we'll hide/show them later based on user preference
-
- self.SetMenuBar(menuBar) # wxBug: Have to set the menubar at the very end or the automatic MDI "window" menu doesn't get put in the right place when the services add new menus to the menubar
+ flags = wx.lib.docview.DOC_SDI | wx.lib.docview.DOC_OPEN_ONCE
+ return flags
- def CreateEmbeddedWindows(self, windows = 0):
+ def ShowTip(self, frame, tipProvider):
"""
- Create the specified embedded windows around the edges of the DocMDIParentFrame.
+ Shows the tip window, generally this is called when an application starts.
+ A wx.TipProvider must be passed.
"""
- frameSize = self.GetSize() # TODO: GetClientWindow.GetSize is still returning 0,0 since the frame isn't fully constructed yet, so using full frame size
- defaultHSize = int(frameSize[0] / 6)
- defaultVSize = int(frameSize[1] / 7)
- defaultSubVSize = int(frameSize[1] / 2)
- #print defaultHSize, defaultVSize, defaultSubVSize
config = wx.ConfigBase_Get()
- if windows & (EMBEDDED_WINDOW_LEFT | EMBEDDED_WINDOW_TOPLEFT | EMBEDDED_WINDOW_BOTTOMLEFT):
- self._leftEmbWindow = self._CreateEmbeddedWindow(self, (config.ReadInt("MDIEmbedLeftSize", defaultHSize), -1), wx.LAYOUT_VERTICAL, wx.LAYOUT_LEFT, visible = config.ReadInt("MDIEmbedLeftVisible", 1), sash = wx.SASH_RIGHT)
- else:
- self._leftEmbWindow = None
- if windows & EMBEDDED_WINDOW_TOPLEFT:
- self._topLeftEmbWindow = self._CreateEmbeddedWindow(self._leftEmbWindow, (-1, config.ReadInt("MDIEmbedTopLeftSize", defaultSubVSize)), wx.LAYOUT_HORIZONTAL, wx.LAYOUT_TOP, visible = config.ReadInt("MDIEmbedTopLeftVisible", 1), sash = wx.SASH_BOTTOM)
- else:
- self._topLeftEmbWindow = None
- if windows & EMBEDDED_WINDOW_BOTTOMLEFT:
- self._bottomLeftEmbWindow = self._CreateEmbeddedWindow(self._leftEmbWindow, (-1, config.ReadInt("MDIEmbedBottomLeftSize", defaultSubVSize)), wx.LAYOUT_HORIZONTAL, wx.LAYOUT_BOTTOM, visible = config.ReadInt("MDIEmbedBottomLeftVisible", 1))
- else:
- self._bottomLeftEmbWindow = None
- if windows & (EMBEDDED_WINDOW_RIGHT | EMBEDDED_WINDOW_TOPRIGHT | EMBEDDED_WINDOW_BOTTOMRIGHT):
- self._rightEmbWindow = self._CreateEmbeddedWindow(self, (config.ReadInt("MDIEmbedRightSize", defaultHSize), -1), wx.LAYOUT_VERTICAL, wx.LAYOUT_RIGHT, visible = config.ReadInt("MDIEmbedRightVisible", 1), sash = wx.SASH_LEFT)
- else:
- self._rightEmbWindow = None
- if windows & EMBEDDED_WINDOW_TOPRIGHT:
- self._topRightEmbWindow = self._CreateEmbeddedWindow(self._rightEmbWindow, (-1, config.ReadInt("MDIEmbedTopRightSize", defaultSubVSize)), wx.LAYOUT_HORIZONTAL, wx.LAYOUT_TOP, visible = config.ReadInt("MDIEmbedTopRightVisible", 1), sash = wx.SASH_BOTTOM)
- else:
- self._topRightEmbWindow = None
- if windows & EMBEDDED_WINDOW_BOTTOMRIGHT:
- self._bottomRightEmbWindow = self._CreateEmbeddedWindow(self._rightEmbWindow, (-1, config.ReadInt("MDIEmbedBottomRightSize", defaultSubVSize)), wx.LAYOUT_HORIZONTAL, wx.LAYOUT_BOTTOM, visible = config.ReadInt("MDIEmbedBottomRightVisible", 1))
- else:
- self._bottomRightEmbWindow = None
- if windows & EMBEDDED_WINDOW_TOP:
- self._topEmbWindow = self._CreateEmbeddedWindow(self, (-1, config.ReadInt("MDIEmbedTopSize", defaultVSize)), wx.LAYOUT_HORIZONTAL, wx.LAYOUT_TOP, visible = config.ReadInt("MDIEmbedTopVisible", 1), sash = wx.SASH_BOTTOM)
- else:
- self._topEmbWindow = None
- if windows & EMBEDDED_WINDOW_BOTTOM:
- self._bottomEmbWindow = self._CreateEmbeddedWindow(self, (-1, config.ReadInt("MDIEmbedBottomSize", defaultVSize)), wx.LAYOUT_HORIZONTAL, wx.LAYOUT_BOTTOM, visible = config.ReadInt("MDIEmbedBottomVisible", 1), sash = wx.SASH_TOP)
- else:
- self._bottomEmbWindow = None
- wx.LayoutAlgorithm().LayoutMDIFrame(self)
- self.GetClientWindow().Refresh()
+ showTip = config.ReadInt("ShowTipAtStartup", 1)
+ if showTip:
+ index = config.ReadInt("TipIndex", 0)
+ showTipResult = wx.ShowTip(wx.GetApp().GetTopWindow(), tipProvider, showAtStartup = showTip)
+ if showTipResult != showTip:
+ config.WriteInt("ShowTipAtStartup", showTipResult)
- def SaveEmbeddedWindowSizes(self):
+ def GetEditMenu(self, frame):
"""
- Saves the sizes of the embedded windows.
+ Utility method that finds the Edit menu within the menubar of a frame.
"""
- config = wx.ConfigBase_Get()
- if self._leftEmbWindow:
- config.WriteInt("MDIEmbedLeftSize", self._leftEmbWindow.GetSize()[0])
- config.WriteInt("MDIEmbedLeftVisible", self._leftEmbWindow.IsShown())
- if self._topLeftEmbWindow:
- if self._topLeftEmbWindow._sizeBeforeHidden:
- size = self._topLeftEmbWindow._sizeBeforeHidden[1]
- else:
- size = self._topLeftEmbWindow.GetSize()[1]
- config.WriteInt("MDIEmbedTopLeftSize", size)
- config.WriteInt("MDIEmbedTopLeftVisible", self._topLeftEmbWindow.IsShown())
- if self._bottomLeftEmbWindow:
- if self._bottomLeftEmbWindow._sizeBeforeHidden:
- size = self._bottomLeftEmbWindow._sizeBeforeHidden[1]
- else:
- size = self._bottomLeftEmbWindow.GetSize()[1]
- config.WriteInt("MDIEmbedBottomLeftSize", size)
- config.WriteInt("MDIEmbedBottomLeftVisible", self._bottomLeftEmbWindow.IsShown())
- if self._rightEmbWindow:
- config.WriteInt("MDIEmbedRightSize", self._rightEmbWindow.GetSize()[0])
- config.WriteInt("MDIEmbedRightVisible", self._rightEmbWindow.IsShown())
- if self._topRightEmbWindow:
- if self._topRightEmbWindow._sizeBeforeHidden:
- size = self._topRightEmbWindow._sizeBeforeHidden[1]
- else:
- size = self._topRightEmbWindow.GetSize()[1]
- config.WriteInt("MDIEmbedTopRightSize", size)
- config.WriteInt("MDIEmbedTopRightVisible", self._topRightEmbWindow.IsShown())
- if self._bottomRightEmbWindow:
- if self._bottomRightEmbWindow._sizeBeforeHidden:
- size = self._bottomRightEmbWindow._sizeBeforeHidden[1]
- else:
- size = self._bottomRightEmbWindow.GetSize()[1]
- config.WriteInt("MDIEmbedBottomRightSize", size)
- config.WriteInt("MDIEmbedBottomRightVisible", self._bottomRightEmbWindow.IsShown())
- if self._topEmbWindow:
- config.WriteInt("MDIEmbedTopSize", self._topEmbWindow.GetSize()[1])
- config.WriteInt("MDIEmbedTopVisible", self._topEmbWindow.IsShown())
- if self._bottomEmbWindow:
- config.WriteInt("MDIEmbedBottomSize", self._bottomEmbWindow.GetSize()[1])
- config.WriteInt("MDIEmbedBottomVisible", self._bottomEmbWindow.IsShown())
+ menuBar = frame.GetMenuBar()
+ if not menuBar:
+ return None
+ editMenuIndex = menuBar.FindMenu(_("&Edit"))
+ if editMenuIndex == -1:
+ return None
+ return menuBar.GetMenu(editMenuIndex)
- def GetEmbeddedWindow(self, loc):
+ def GetUseTabbedMDI(self):
"""
- Returns the instance of the embedded window specified by the embedded window location constant.
+ Returns True if Windows MDI should use folder tabs instead of child windows.
"""
- if loc == EMBEDDED_WINDOW_TOP:
- return self._topEmbWindow
- elif loc == EMBEDDED_WINDOW_BOTTOM:
- return self._bottomEmbWindow
- elif loc == EMBEDDED_WINDOW_LEFT:
- return self._leftEmbWindow
- elif loc == EMBEDDED_WINDOW_RIGHT:
- return self._rightEmbWindow
- elif loc == EMBEDDED_WINDOW_TOPLEFT:
- return self._topLeftEmbWindow
- elif loc == EMBEDDED_WINDOW_BOTTOMLEFT:
- return self._bottomLeftEmbWindow
- elif loc == EMBEDDED_WINDOW_TOPRIGHT:
- return self._topRightEmbWindow
- elif loc == EMBEDDED_WINDOW_BOTTOMRIGHT:
- return self._bottomRightEmbWindow
- return None
-
+ return self._useTabbedMDI
+
- def _CreateEmbeddedWindow(self, parent, size, orientation, alignment, visible = True, sash = None):
+ def SetUseTabbedMDI(self, useTabbedMDI):
"""
- Creates the embedded window with the specified size, orientation, and alignment. If the
- window is not visible it will retain the size with which it was last viewed.
+ Set to True if Windows MDI should use folder tabs instead of child windows.
"""
- window = wx.SashLayoutWindow(parent, wx.NewId(), style = wx.NO_BORDER | wx.SW_3D)
- window.SetDefaultSize(size)
- window.SetOrientation(orientation)
- window.SetAlignment(alignment)
- if sash != None: # wx.SASH_TOP is 0 so check for None instead of just doing "if sash:"
- window.SetSashVisible(sash, True)
- ####
- def OnEmbeddedWindowSashDrag(event):
- if event.GetDragStatus() == wx.SASH_STATUS_OUT_OF_RANGE:
- return
- sashWindow = event.GetEventObject()
- if sashWindow.GetAlignment() == wx.LAYOUT_TOP or sashWindow.GetAlignment() == wx.LAYOUT_BOTTOM:
- size = wx.Size(-1, event.GetDragRect().height)
- else:
- size = wx.Size(event.GetDragRect().width, -1)
- event.GetEventObject().SetDefaultSize(size)
- wx.LayoutAlgorithm().LayoutMDIFrame(self)
- self.GetClientWindow().Refresh()
- if isinstance(sashWindow.GetParent(), wx.SashLayoutWindow):
- sashWindow.Show()
- parentSashWindow = sashWindow.GetParent() # Force a refresh
- parentSashWindow.Layout()
- parentSashWindow.Refresh()
- parentSashWindow.SetSize((parentSashWindow.GetSize().width + 1, parentSashWindow.GetSize().height + 1))
- ####
- wx.EVT_SASH_DRAGGED(window, window.GetId(), OnEmbeddedWindowSashDrag)
- window._sizeBeforeHidden = None
- if not visible:
- window.Show(False)
- if isinstance(parent, wx.SashLayoutWindow): # It's a window embedded in another sash window so remember its actual size to show it again
- window._sizeBeforeHidden = size
- return window
+ self._useTabbedMDI = useTabbedMDI
- def ShowEmbeddedWindow(self, window, show = True):
+ def CreateDocumentFrame(self, view, doc, flags, id = -1, title = "", pos = wx.DefaultPosition, size = wx.DefaultSize, style = wx.DEFAULT_FRAME_STYLE):
"""
- Shows or hides the embedded window specified by the embedded window location constant.
+ Called by the DocManager to create and return a new Frame for a Document.
+ Chooses whether to create an MDIChildFrame or SDI Frame based on the
+ DocManager's flags.
"""
- window.Show(show)
- if isinstance(window.GetParent(), wx.SashLayoutWindow): # It is a parent sashwindow with multiple embedded sashwindows
- parentSashWindow = window.GetParent()
- if show: # Make sure it is visible in case all of the subwindows were hidden
- parentSashWindow.Show()
- if show and window._sizeBeforeHidden:
- if window._sizeBeforeHidden[1] == parentSashWindow.GetClientSize()[1]:
- if window == self.GetEmbeddedWindow(EMBEDDED_WINDOW_BOTTOMLEFT) and self.GetEmbeddedWindow(EMBEDDED_WINDOW_TOPLEFT).IsShown():
- window.SetDefaultSize((window._sizeBeforeHidden[0], window._sizeBeforeHidden[0] - self.GetEmbeddedWindow(EMBEDDED_WINDOW_TOPLEFT).GetSize()[1]))
- elif window == self.GetEmbeddedWindow(EMBEDDED_WINDOW_TOPLEFT) and self.GetEmbeddedWindow(EMBEDDED_WINDOW_BOTTOMLEFT).IsShown():
- window.SetDefaultSize((window._sizeBeforeHidden[0], window._sizeBeforeHidden[0] - self.GetEmbeddedWindow(EMBEDDED_WINDOW_BOTTOMLEFT).GetSize()[1]))
- elif window == self.GetEmbeddedWindow(EMBEDDED_WINDOW_BOTTOMRIGHT) and self.GetEmbeddedWindow(EMBEDDED_WINDOW_TOPRIGHT).IsShown():
- window.SetDefaultSize((window._sizeBeforeHidden[0], window._sizeBeforeHidden[0] - self.GetEmbeddedWindow(EMBEDDED_WINDOW_TOPRIGHT).GetSize()[1]))
- elif window == self.GetEmbeddedWindow(EMBEDDED_WINDOW_TOPRIGHT) and self.GetEmbeddedWindow(EMBEDDED_WINDOW_BOTTOMRIGHT).IsShown():
- window.SetDefaultSize((window._sizeBeforeHidden[0], window._sizeBeforeHidden[0] - self.GetEmbeddedWindow(EMBEDDED_WINDOW_BOTTOMRIGHT).GetSize()[1]))
- print window.GetSize()
- else:
- window.SetDefaultSize(window._sizeBeforeHidden)
- # If it is not the size of the full parent sashwindow set the other window's size so that if it gets shown it will have a cooresponding size
- print "Parent size, size before hidden ", parentSashWindow.GetClientSize()[1], window._sizeBeforeHidden[1]
- if window._sizeBeforeHidden[1] < parentSashWindow.GetClientSize()[1]:
- otherWindowSize = (-1, parentSashWindow.GetClientSize()[1] - window._sizeBeforeHidden[1])
- print "Other", otherWindowSize
- if window == self.GetEmbeddedWindow(EMBEDDED_WINDOW_BOTTOMLEFT):
- self.GetEmbeddedWindow(EMBEDDED_WINDOW_TOPLEFT).SetDefaultSize(otherWindowSize)
- elif window == self.GetEmbeddedWindow(EMBEDDED_WINDOW_TOPLEFT):
- self.GetEmbeddedWindow(EMBEDDED_WINDOW_BOTTOMLEFT).SetDefaultSize(otherWindowSize)
- elif window == self.GetEmbeddedWindow(EMBEDDED_WINDOW_BOTTOMRIGHT):
- self.GetEmbeddedWindow(EMBEDDED_WINDOW_TOPRIGHT).SetDefaultSize(otherWindowSize)
- elif window == self.GetEmbeddedWindow(EMBEDDED_WINDOW_TOPRIGHT):
- self.GetEmbeddedWindow(EMBEDDED_WINDOW_BOTTOMRIGHT).SetDefaultSize(otherWindowSize)
-
- if not show:
- if window == self.GetEmbeddedWindow(EMBEDDED_WINDOW_BOTTOMRIGHT) and not self.GetEmbeddedWindow(EMBEDDED_WINDOW_TOPRIGHT).IsShown() \
- or window == self.GetEmbeddedWindow(EMBEDDED_WINDOW_TOPRIGHT) and not self.GetEmbeddedWindow(EMBEDDED_WINDOW_BOTTOMRIGHT).IsShown() \
- or window == self.GetEmbeddedWindow(EMBEDDED_WINDOW_BOTTOMLEFT) and not self.GetEmbeddedWindow(EMBEDDED_WINDOW_TOPLEFT).IsShown() \
- or window == self.GetEmbeddedWindow(EMBEDDED_WINDOW_TOPLEFT) and not self.GetEmbeddedWindow(EMBEDDED_WINDOW_BOTTOMLEFT).IsShown():
- parentSashWindow.Hide() # Hide the parent sashwindow if all of the children are hidden
- parentSashWindow.Layout() # Force a refresh
- parentSashWindow.Refresh()
- parentSashWindow.SetSize((parentSashWindow.GetSize().width + 1, parentSashWindow.GetSize().height + 1))
- wx.LayoutAlgorithm().LayoutMDIFrame(self)
- self.GetClientWindow().Refresh()
+ docflags = self.GetDocumentManager().GetFlags()
+ if docflags & wx.lib.docview.DOC_SDI:
+ frame = self.CreateSDIDocumentFrame(doc, view, id, title, pos, size, style)
+ frame.Show()
+ # wxBug: operating system bug, first window is set to the position of last window closed, ignoring passed in position on frame creation
+ # also, initial size is incorrect for the same reasons
+ if frame.GetPosition() != pos:
+ frame.Move(pos)
+ if frame.GetSize() != size:
+ frame.SetSize(size)
- def HideEmbeddedWindow(self):
+ if doc and doc.GetCommandProcessor():
+ doc.GetCommandProcessor().SetEditMenu(self.GetEditMenu(frame))
+ elif docflags & wx.lib.docview.DOC_MDI:
+ if self.GetUseTabbedMDI():
+ frame = self.CreateTabbedDocumentFrame(doc, view, id, title, pos, size, style)
+ else:
+ frame = self.CreateMDIDocumentFrame(doc, view, id, title, pos, size, style)
+ if doc:
+ if doc.GetDocumentTemplate().GetIcon():
+ frame.SetIcon(doc.GetDocumentTemplate().GetIcon())
+ elif wx.GetApp().GetTopWindow().GetIcon():
+ frame.SetIcon(wx.GetApp().GetTopWindow().GetIcon())
+ if doc and doc.GetCommandProcessor():
+ doc.GetCommandProcessor().SetEditMenu(self.GetEditMenu(wx.GetApp().GetTopWindow()))
+ if not frame.GetIcon() and self._defaultIcon:
+ frame.SetIcon(self.GetDefaultIcon())
+ view.SetFrame(frame)
+ return frame
+
+
+ def CreateSDIDocumentFrame(self, doc, view, id=-1, title="", pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE):
"""
- Hides the embedded window specified by the embedded window location constant.
+ Creates and returns an SDI Document Frame.
"""
- self.ShowEmbeddedWindow(show = False)
+ frame = DocSDIFrame(doc, view, None, id, title, pos, size, style)
+ return frame
- def GetDocumentManager(self):
+ def CreateTabbedDocumentFrame(self, doc, view, id=-1, title="", pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE):
"""
- Returns the document manager associated with the DocMDIParentFrame.
+ Creates and returns an MDI Document Frame for a Tabbed MDI view
"""
- return self._docManager
+ frame = DocTabbedChildFrame(doc, view, wx.GetApp().GetTopWindow(), id, title, pos, size, style)
+ return frame
- def InitializePrintData(self):
+ def CreateMDIDocumentFrame(self, doc, view, id=-1, title="", pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE):
"""
- Initializes the PrintData that is used when printing.
+ Creates and returns an MDI Document Frame.
"""
- self._printData = wx.PrintData()
- self._printData.SetPaperId(wx.PAPER_LETTER)
+ # if any child windows are maximized, then user must want any new children maximized
+ # if no children exist, then use the default value from registry
+ # wxBug: Only current window is maximized, so need to check every child frame
+ parentFrame = wx.GetApp().GetTopWindow()
+ childrenMaximized = filter(lambda child: isinstance(child, wx.MDIChildFrame) and child.IsMaximized(), parentFrame.GetChildren())
+ if childrenMaximized:
+ maximize = True
+ else:
+ children = filter(lambda child: isinstance(child, wx.MDIChildFrame), parentFrame.GetChildren())
+ if children:
+ # other windows exist and none are maximized
+ maximize = False
+ else:
+ # get default setting from registry
+ maximize = wx.ConfigBase_Get().ReadInt("MDIChildFrameMaximized", False)
+
+ frame = wx.lib.docview.DocMDIChildFrame(doc, view, wx.GetApp().GetTopWindow(), id, title, pos, size, style)
+ if maximize: # wxBug: Should already be maximizing new child frames if one is maximized but it's not so we have to force it to
+ frame.Maximize(True)
+## wx.EVT_MAXIMIZE(frame, self.OnMaximize) # wxBug: This doesn't work, need to save MDIChildFrameMaximized state on close of windows instead
+ wx.EVT_CLOSE(frame, self.OnCloseChildWindow)
+ if not self._registeredCloseEvent:
+ wx.EVT_CLOSE(parentFrame, self.OnCloseMainWindow) # need to check on this, but only once
+ self._registeredCloseEvent = True
- def CreateDefaultStatusBar(self):
+ return frame
+
+
+ def SaveMDIDocumentFrameMaximizedState(self, maximized):
"""
- Creates the default StatusBar.
+ Remember in the config whether the MDI Frame is maximized so that it can be restored
+ on open.
"""
- self.CreateStatusBar()
- self.GetStatusBar().Show(wx.ConfigBase_Get().ReadInt("ViewStatusBar", True))
- self.UpdateStatus()
- return self.GetStatusBar()
+ config = wx.ConfigBase_Get()
+ maximizeFlag = config.ReadInt("MDIChildFrameMaximized", False)
+ if maximized != maximizeFlag:
+ config.WriteInt("MDIChildFrameMaximized", maximized)
- def CreateDefaultToolBar(self):
+ def OnCloseChildWindow(self, event):
"""
- Creates the default ToolBar.
+ Called when an MDI Child Frame is closed. Calls SaveMDIDocumentFrameMaximizedState to
+ remember whether the MDI Frame is maximized so that it can be restored on open.
"""
- self._toolBar = self.CreateToolBar(wx.TB_HORIZONTAL | wx.NO_BORDER | wx.TB_FLAT)
- self._toolBar.AddSimpleTool(wx.ID_NEW, getNewBitmap(), _("New"), _("Creates a new document"))
- self._toolBar.AddSimpleTool(wx.ID_OPEN, getOpenBitmap(), _("Open"), _("Opens an existing document"))
- self._toolBar.AddSimpleTool(wx.ID_SAVE, getSaveBitmap(), _("Save"), _("Saves the active document"))
- self._toolBar.AddSimpleTool(SAVEALL_ID, getSaveAllBitmap(), _("Save All"), _("Saves all the active documents"))
- self._toolBar.AddSeparator()
- self._toolBar.AddSimpleTool(wx.ID_PRINT, getPrintBitmap(), _("Print"), _("Displays full pages"))
- self._toolBar.AddSimpleTool(wx.ID_PREVIEW, getPrintPreviewBitmap(), _("Print Preview"), _("Prints the active document"))
- self._toolBar.AddSeparator()
- self._toolBar.AddSimpleTool(wx.ID_CUT, getCutBitmap(), _("Cut"), _("Cuts the selection and puts it on the Clipboard"))
- self._toolBar.AddSimpleTool(wx.ID_COPY, getCopyBitmap(), _("Copy"), _("Copies the selection and puts it on the Clipboard"))
- self._toolBar.AddSimpleTool(wx.ID_PASTE, getPasteBitmap(), _("Paste"), _("Inserts Clipboard contents"))
- self._toolBar.AddSimpleTool(wx.ID_UNDO, getUndoBitmap(), _("Undo"), _("Reverses the last action"))
- self._toolBar.AddSimpleTool(wx.ID_REDO, getRedoBitmap(), _("Redo"), _("Reverses the last undo"))
- self._toolBar.Realize()
- self._toolBar.Show(wx.ConfigBase_Get().ReadInt("ViewToolBar", True))
-
- return self._toolBar
+ self.SaveMDIDocumentFrameMaximizedState(event.GetEventObject().IsMaximized())
+ event.Skip()
- def CreateDefaultMenuBar(self):
+ def OnCloseMainWindow(self, event):
"""
- Creates the default MenuBar. Contains File, Edit, View, Tools, and Help menus.
+ Called when the MDI Parent Frame is closed. Remembers whether the MDI Parent Frame is
+ maximized.
"""
- menuBar = wx.MenuBar()
-
- fileMenu = wx.Menu()
- fileMenu.Append(wx.ID_NEW, _("&New...\tCtrl+N"), _("Creates a new document"))
- fileMenu.Append(wx.ID_OPEN, _("&Open...\tCtrl+O"), _("Opens an existing document"))
- fileMenu.Append(wx.ID_CLOSE, _("&Close"), _("Closes the active document"))
- fileMenu.Append(wx.ID_CLOSE_ALL, _("Close A&ll"), _("Closes all open documents"))
- fileMenu.AppendSeparator()
- fileMenu.Append(wx.ID_SAVE, _("&Save\tCtrl+S"), _("Saves the active document"))
- fileMenu.Append(wx.ID_SAVEAS, _("Save &As..."), _("Saves the active document with a new name"))
- fileMenu.Append(SAVEALL_ID, _("Save All\tCtrl+Shift+A"), _("Saves the all active documents"))
- wx.EVT_MENU(self, SAVEALL_ID, self.ProcessEvent)
- wx.EVT_UPDATE_UI(self, SAVEALL_ID, self.ProcessUpdateUIEvent)
- fileMenu.AppendSeparator()
- fileMenu.Append(wx.ID_PRINT, _("&Print\tCtrl+P"), _("Prints the active document"))
- fileMenu.Append(wx.ID_PREVIEW, _("Print Pre&view"), _("Displays full pages"))
- fileMenu.Append(wx.ID_PRINT_SETUP, _("Page Set&up"), _("Changes page layout settings"))
- fileMenu.AppendSeparator()
- if wx.Platform == '__WXMAC__':
- fileMenu.Append(wx.ID_EXIT, _("&Quit"), _("Closes this program"))
+ children = event.GetEventObject().GetChildren()
+ childrenMaximized = filter(lambda child: isinstance(child, wx.MDIChildFrame)and child.IsMaximized(), children)
+ if childrenMaximized:
+ self.SaveMDIDocumentFrameMaximizedState(True)
else:
- fileMenu.Append(wx.ID_EXIT, _("E&xit"), _("Closes this program"))
- self._docManager.FileHistoryUseMenu(fileMenu)
- self._docManager.FileHistoryAddFilesToMenu()
- menuBar.Append(fileMenu, _("&File"));
+ childrenNotMaximized = filter(lambda child: isinstance(child, wx.MDIChildFrame), children)
- editMenu = wx.Menu()
- editMenu.Append(wx.ID_UNDO, _("&Undo\tCtrl+Z"), _("Reverses the last action"))
- editMenu.Append(wx.ID_REDO, _("&Redo\tCtrl+Y"), _("Reverses the last undo"))
- editMenu.AppendSeparator()
- #item = wxMenuItem(self.editMenu, wxID_CUT, _("Cu&t\tCtrl+X"), _("Cuts the selection and puts it on the Clipboard"))
- #item.SetBitmap(getCutBitmap())
- #editMenu.AppendItem(item)
- editMenu.Append(wx.ID_CUT, _("Cu&t\tCtrl+X"), _("Cuts the selection and puts it on the Clipboard"))
- wx.EVT_MENU(self, wx.ID_CUT, self.ProcessEvent)
- wx.EVT_UPDATE_UI(self, wx.ID_CUT, self.ProcessUpdateUIEvent)
- editMenu.Append(wx.ID_COPY, _("&Copy\tCtrl+C"), _("Copies the selection and puts it on the Clipboard"))
- wx.EVT_MENU(self, wx.ID_COPY, self.ProcessEvent)
- wx.EVT_UPDATE_UI(self, wx.ID_COPY, self.ProcessUpdateUIEvent)
- editMenu.Append(wx.ID_PASTE, _("&Paste\tCtrl+V"), _("Inserts Clipboard contents"))
- wx.EVT_MENU(self, wx.ID_PASTE, self.ProcessEvent)
- wx.EVT_UPDATE_UI(self, wx.ID_PASTE, self.ProcessUpdateUIEvent)
- editMenu.Append(wx.ID_CLEAR, _("Cle&ar\tDel"), _("Erases the selection"))
- wx.EVT_MENU(self, wx.ID_CLEAR, self.ProcessEvent)
- wx.EVT_UPDATE_UI(self, wx.ID_CLEAR, self.ProcessUpdateUIEvent)
- editMenu.AppendSeparator()
- editMenu.Append(wx.ID_SELECTALL, _("Select A&ll\tCtrl+A"), _("Selects all available data"))
- wx.EVT_MENU(self, wx.ID_SELECTALL, self.ProcessEvent)
- wx.EVT_UPDATE_UI(self, wx.ID_SELECTALL, self.ProcessUpdateUIEvent)
- menuBar.Append(editMenu, _("&Edit"))
+ if childrenNotMaximized:
+ # other windows exist and none are maximized
+ self.SaveMDIDocumentFrameMaximizedState(False)
- viewMenu = wx.Menu()
- viewMenu.AppendCheckItem(VIEW_TOOLBAR_ID, _("&Toolbar"), _("Shows or hides the toolbar"))
- wx.EVT_MENU(self, VIEW_TOOLBAR_ID, self.OnViewToolBar)
- wx.EVT_UPDATE_UI(self, VIEW_TOOLBAR_ID, self.OnUpdateViewToolBar)
- viewMenu.AppendCheckItem(VIEW_STATUSBAR_ID, _("&Status Bar"), _("Shows or hides the status bar"))
- wx.EVT_MENU(self, VIEW_STATUSBAR_ID, self.OnViewStatusBar)
- wx.EVT_UPDATE_UI(self, VIEW_STATUSBAR_ID, self.OnUpdateViewStatusBar)
- menuBar.Append(viewMenu, _("&View"))
+ event.Skip()
- helpMenu = wx.Menu()
- helpMenu.Append(wx.ID_ABOUT, _("&About" + " " + wx.GetApp().GetAppName()), _("Displays program information, version number, and copyright"))
- wx.EVT_MENU(self, wx.ID_ABOUT, self.OnAbout)
- menuBar.Append(helpMenu, _("&Help"))
-## windowMenu = wx.Menu()
-## menuBar.Append(windowMenu, _("&Window"))
-## # self.SetWindowMenu(windowMenu)
-##
- wx.EVT_UPDATE_UI(self, wx.ID_ABOUT, self.ProcessUpdateUIEvent) # Using ID_ABOUT to update the window menu, the window menu items are not triggering
+ def GetDefaultIcon(self):
+ """
+ Returns the application's default icon.
+ """
+ return self._defaultIcon
- return menuBar
-## accelTable = wx.AcceleratorTable([
-## eval(_("wx.ACCEL_CTRL, ord('Z'), wx.ID_UNDO")),
-## eval(_("wx.ACCEL_CTRL, ord('Y'), wx.ID_REDO")),
-## eval(_("wx.ACCEL_CTRL, ord('X'), wx.ID_CUT")),
-## eval(_("wx.ACCEL_CTRL, ord('C'), wx.ID_COPY")),
-## eval(_("wx.ACCEL_CTRL, ord('V'), wx.ID_PASTE")),
-## (wx.ACCEL_NORMAL, wx.WXK_DELETE, wx.ID_CLEAR),
-## eval(_("wx.ACCEL_CTRL, ord('A'), wx.ID_SELECTALL")),
-## eval(_("wx.ACCEL_CTRL, ord('N'), wx.ID_NEW")),
-## eval(_("wx.ACCEL_CTRL, ord('O'), wx.ID_OPEN")),
-## eval(_("wx.ACCEL_CTRL, ord('S'), wx.ID_SAVE"))
-## ])
-## self.SetAcceleratorTable(accelTable)
+ def SetDefaultIcon(self, icon):
+ """
+ Sets the application's default icon.
+ """
+ self._defaultIcon = icon
- def ProcessEvent(self, event):
+ def GetDebug(self):
"""
- 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.
+ Returns True if the application is in debug mode.
"""
- if wx.GetApp().ProcessEventBeforeWindows(event):
- return True
- if wx.lib.docview.DocMDIParentFrame.ProcessEvent(self, event):
- return True
-
- id = event.GetId()
- if id == SAVEALL_ID:
- self.OnFileSaveAll(event)
- return True
-
- return wx.GetApp().ProcessEvent(event)
+ return self._debug
- def ProcessUpdateUIEvent(self, event):
+ def SetDebug(self, debug):
"""
- 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.
+ Sets the application's debug mode.
"""
- if wx.GetApp().ProcessUpdateUIEventBeforeWindows(event):
- return True
- if wx.lib.docview.DocMDIParentFrame.ProcessUpdateUIEvent(self, event): # Let the views handle the event before the services
- return True
- id = event.GetId()
- if id == wx.ID_CUT:
- event.Enable(False)
- return True
- elif id == wx.ID_COPY:
- event.Enable(False)
- return True
- elif id == wx.ID_PASTE:
- event.Enable(False)
- return True
- elif id == wx.ID_CLEAR:
- event.Enable(False)
- return True
- elif id == wx.ID_SELECTALL:
- event.Enable(False)
- return True
- elif id == wx.ID_ABOUT: # Using ID_ABOUT to update the window menu, the window menu items are not triggering
- self.UpdateWindowMenu()
- return True
- elif id == SAVEALL_ID:
- filesModified = False
- docs = wx.GetApp().GetDocumentManager().GetDocuments()
- for doc in docs:
- if doc.IsModified():
- filesModified = True
- break
-
- event.Enable(filesModified)
- return True
+ self._debug = debug
+
+
+ def GetSingleInstance(self):
+ """
+ Returns True if the application is in single instance mode. Used to determine if multiple instances of the application is allowed to launch.
+ """
+ return self._singleInstance
+
+
+ def SetSingleInstance(self, singleInstance):
+ """
+ Sets application's single instance mode.
+ """
+ self._singleInstance = singleInstance
+
+
+
+ def CreateChildDocument(self, parentDocument, documentType, objectToEdit, path=''):
+ """
+ Creates a child window of a document that edits an object. The child window
+ is managed by the parent document frame, so it will be prompted to close if its
+ parent is closed, etc. Child Documents are useful when there are complicated
+ Views of a Document and users will need to tunnel into the View.
+ """
+ for document in self.GetDocumentManager().GetDocuments()[:]: # Cloning list to make sure we go through all docs even as they are deleted
+ if isinstance(document, ChildDocument) and document.GetParentDocument() == parentDocument:
+ if document.GetData() == objectToEdit:
+ if hasattr(document.GetFirstView().GetFrame(), "SetFocus"):
+ document.GetFirstView().GetFrame().SetFocus()
+ return document
+ for temp in wx.GetApp().GetDocumentManager().GetTemplates():
+ if temp.GetDocumentType() == documentType:
+ break
+ temp = None
+ newDoc = temp.CreateDocument(path, 0, data = objectToEdit, parentDocument = parentDocument)
+ newDoc.SetDocumentName(temp.GetDocumentName())
+ newDoc.SetDocumentTemplate(temp)
+ if path == '':
+ newDoc.OnNewDocument()
else:
- return wx.GetApp().ProcessUpdateUIEvent(event)
+ if not newDoc.OnOpenDocument(path):
+ newDoc.DeleteAllViews() # Implicitly deleted by DeleteAllViews
+ return None
+ return newDoc
- def UpdateWindowMenu(self):
+ def CloseChildDocuments(self, parentDocument):
"""
- Updates the WindowMenu on Windows platforms.
+ Closes the child windows of a Document.
"""
- if wx.Platform == '__WXMSW__':
- children = filter(lambda child: isinstance(child, wx.MDIChildFrame), self.GetChildren())
- windowCount = len(children)
- hasWindow = windowCount >= 1
- has2OrMoreWindows = windowCount >= 2
+ for document in self.GetDocumentManager().GetDocuments()[:]: # Cloning list to make sure we go through all docs even as they are deleted
+ if isinstance(document, ChildDocument) and document.GetParentDocument() == parentDocument:
+ if document.GetFirstView().GetFrame():
+ document.GetFirstView().GetFrame().SetFocus()
+ if not document.GetFirstView().OnClose():
+ return False
+ return True
- windowMenu = self.GetWindowMenu()
- windowMenu.Enable(wx.IDM_WINDOWTILE, hasWindow)
- windowMenu.Enable(wx.IDM_WINDOWTILEHOR, hasWindow)
- windowMenu.Enable(wx.IDM_WINDOWCASCADE, hasWindow)
- windowMenu.Enable(wx.IDM_WINDOWICONS, hasWindow)
- windowMenu.Enable(wx.IDM_WINDOWTILEVERT, hasWindow)
- wx.IDM_WINDOWPREV = 4006 # wxBug: Not defined for some reason
- windowMenu.Enable(wx.IDM_WINDOWPREV, has2OrMoreWindows)
- windowMenu.Enable(wx.IDM_WINDOWNEXT, has2OrMoreWindows)
+ def IsMDI(self):
+ """
+ Returns True if the application is in MDI mode.
+ """
+ return self.GetDocumentManager().GetFlags() & wx.lib.docview.DOC_MDI
- def OnFileSaveAll(self, event):
+
+ def IsSDI(self):
"""
- Saves all of the currently open documents.
+ Returns True if the application is in SDI mode.
"""
- docs = wx.GetApp().GetDocumentManager().GetDocuments()
- for doc in docs:
- doc.Save()
-
+ return self.GetDocumentManager().GetFlags() & wx.lib.docview.DOC_SDI
- def OnCloseWindow(self, event):
+
+ def ShowSplash(self, image):
"""
- Called when the DocMDIParentFrame is closed. Remembers the frame size.
+ Shows a splash window with the given image. Input parameter 'image' can either be a wx.Bitmap or a filename.
"""
- config = wx.ConfigBase_Get()
- if not self.IsMaximized():
- config.WriteInt("MDIFrameXLoc", self.GetPositionTuple()[0])
- config.WriteInt("MDIFrameYLoc", self.GetPositionTuple()[1])
- config.WriteInt("MDIFrameXSize", self.GetSizeTuple()[0])
- config.WriteInt("MDIFrameYSize", self.GetSizeTuple()[1])
- config.WriteInt("MDIFrameMaximized", self.IsMaximized())
- config.WriteInt("ViewToolBar", self._toolBar.IsShown())
- config.WriteInt("ViewStatusBar", self.GetStatusBar().IsShown())
+ if isinstance(image, wx.Bitmap):
+ splash_bmp = image
+ else:
+ splash_bmp = wx.Image(image).ConvertToBitmap()
+ self._splash = wx.SplashScreen(splash_bmp,wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_NO_TIMEOUT,0, None, -1)
+ self._splash.Show()
- self.SaveEmbeddedWindowSizes()
- # save and close services last.
- for service in wx.GetApp().GetServices():
- if not service.OnCloseFrame(event):
- return
+ def CloseSplash(self):
+ """
+ Closes the splash window.
+ """
+ if self._splash:
+ self._splash.Close(True)
+
+
+class _DocFrameFileDropTarget(wx.FileDropTarget):
+ """
+ Class used to handle drops into the document frame.
+ """
- # save and close documents
- # documents with a common view, e.g. project view, should save the document, but not close the window
- # and let the service close the window.
- wx.lib.docview.DocMDIParentFrame.OnCloseWindow(self, event)
+ def __init__(self, docManager, docFrame):
+ """
+ Initializes the FileDropTarget class with the active docManager and the docFrame.
+ """
+ wx.FileDropTarget.__init__(self)
+ self._docManager = docManager
+ self._docFrame = docFrame
- def OnAbout(self, event):
+ def OnDropFiles(self, x, y, filenames):
"""
- Invokes the about dialog.
+ Called when files are dropped in the drop target and tells the docManager to open
+ the files.
"""
- aboutService = wx.GetApp().GetService(AboutService)
- if aboutService:
- aboutService.ShowAbout()
+ try:
+ for file in filenames:
+ self._docManager.CreateDocument(file, wx.lib.docview.DOC_SILENT)
+ except:
+ msgTitle = wx.GetApp().GetAppName()
+ if not msgTitle:
+ msgTitle = _("File Error")
+ wx.MessageBox("Could not open '%s'. '%s'" % (docview.FileNameFromPath(file), sys.exc_value),
+ msgTitle,
+ wx.OK | wx.ICON_EXCLAMATION,
+ self._docManager.FindSuitableParent())
- def OnViewToolBar(self, event):
+class DocMDIParentFrame(wx.lib.docview.DocMDIParentFrame, DocFrameMixIn, DocMDIParentFrameMixIn):
+ """
+ The DocMDIParentFrame is the primary frame which the DocApp uses to host MDI child windows. It offers
+ features such as a default menubar, toolbar, and status bar, and a mechanism to manage embedded windows
+ on the edges of the DocMDIParentFrame.
+ """
+
+
+ def __init__(self, docManager, parent, id, title, pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE, name="DocMDIFrame", embeddedWindows=0):
"""
- Toggles whether the ToolBar is visible.
+ Initializes the DocMDIParentFrame with the default menubar, toolbar, and status bar. Use the
+ optional embeddedWindows parameter with the embedded window constants to create embedded
+ windows around the edges of the DocMDIParentFrame.
+ """
+ pos, size = self._GetPosSizeFromConfig(pos, size)
+ wx.lib.docview.DocMDIParentFrame.__init__(self, docManager, parent, id, title, pos, size, style, name)
+ self._InitFrame(embeddedWindows)
+
+
+ def _LayoutFrame(self):
+ """
+ Lays out the frame.
"""
- self._toolBar.Show(not self._toolBar.IsShown())
wx.LayoutAlgorithm().LayoutMDIFrame(self)
self.GetClientWindow().Refresh()
+
+
+ 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 wx.lib.docview.DocMDIParentFrame.ProcessEvent(self, event):
+ return True
+ return DocMDIParentFrameMixIn.ProcessEvent(self, event)
- def OnUpdateViewToolBar(self, event):
+ def ProcessUpdateUIEvent(self, event):
"""
- Updates the View ToolBar menu item.
+ 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.
"""
- event.Check(self.GetToolBar().IsShown())
+ if wx.GetApp().ProcessUpdateUIEventBeforeWindows(event):
+ return True
+ if wx.lib.docview.DocMDIParentFrame.ProcessUpdateUIEvent(self, event): # Let the views handle the event before the services
+ return True
+ if event.GetId() == wx.ID_ABOUT: # Using ID_ABOUT to update the window menu, the window menu items are not triggering
+ self.UpdateWindowMenu()
+ return True
+ return DocMDIParentFrameMixIn.ProcessUpdateUIEvent(self, event)
- def OnViewStatusBar(self, event):
+ def UpdateWindowMenu(self):
"""
- Toggles whether the StatusBar is visible.
+ Updates the WindowMenu on Windows platforms.
"""
- self.GetStatusBar().Show(not self.GetStatusBar().IsShown())
- self.Layout()
- wx.LayoutAlgorithm().LayoutMDIFrame(self)
- self.GetClientWindow().Refresh()
+ if wx.Platform == '__WXMSW__':
+ children = filter(lambda child: isinstance(child, wx.MDIChildFrame), self.GetChildren())
+ windowCount = len(children)
+ hasWindow = windowCount >= 1
+ has2OrMoreWindows = windowCount >= 2
+ windowMenu = self.GetWindowMenu()
+ if windowMenu:
+ windowMenu.Enable(wx.IDM_WINDOWTILE, hasWindow)
+ windowMenu.Enable(wx.IDM_WINDOWTILEHOR, hasWindow)
+ windowMenu.Enable(wx.IDM_WINDOWCASCADE, hasWindow)
+ windowMenu.Enable(wx.IDM_WINDOWICONS, hasWindow)
+ windowMenu.Enable(wx.IDM_WINDOWTILEVERT, hasWindow)
+ wx.IDM_WINDOWPREV = 4006 # wxBug: Not defined for some reason
+ windowMenu.Enable(wx.IDM_WINDOWPREV, has2OrMoreWindows)
+ windowMenu.Enable(wx.IDM_WINDOWNEXT, has2OrMoreWindows)
+
- def OnUpdateViewStatusBar(self, event):
+
+ def OnSize(self, event):
"""
- Updates the View StatusBar menu item.
+ Called when the DocMDIParentFrame is resized and lays out the MDI client window.
"""
- event.Check(self.GetStatusBar().IsShown())
+ # Needed in case there are splitpanels around the mdi frame
+ self._LayoutFrame()
- def UpdateStatus(self, message = _("Ready")):
+ def OnCloseWindow(self, event):
"""
- Updates the StatusBar.
+ Called when the DocMDIParentFrame is closed. Remembers the frame size.
"""
- # wxBug: Menubar and toolbar help strings don't pop the status text back
- if self.GetStatusBar().GetStatusText() != message:
- self.GetStatusBar().PushStatusText(message)
+ self.SaveEmbeddedWindowSizes()
+ # save and close services last.
+ for service in wx.GetApp().GetServices():
+ if not service.OnCloseFrame(event):
+ return
- def OnSize(self, event):
- """
- Called when the DocMDIParentFrame is resized and lays out the MDI client window.
- """
- # Needed in case there are splitpanels around the mdi frame
- wx.LayoutAlgorithm().LayoutMDIFrame(self)
- self.GetClientWindow().Refresh()
+ # save and close documents
+ # documents with a common view, e.g. project view, should save the document, but not close the window
+ # and let the service close the window.
+ wx.lib.docview.DocMDIParentFrame.OnCloseWindow(self, event)
-class DocSDIFrame(wx.lib.docview.DocChildFrame):
+class DocSDIFrame(wx.lib.docview.DocChildFrame, DocFrameMixIn):
"""
The DocSDIFrame host DocManager Document windows. It offers features such as a default menubar,
toolbar, and status bar.
"""
- def __init__(self, doc, view, parent, id, title, pos = wx.DefaultPosition, size = wx.DefaultSize, style = wx.DEFAULT_FRAME_STYLE, name = "DocSDIFrame"):
+ def __init__(self, doc, view, parent, id, title, pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE, name="DocSDIFrame"):
"""
Initializes the DocSDIFrame with the default menubar, toolbar, and status bar.
"""
self.InitializePrintData()
- menuBar = self.CreateDefaultMenuBar()
+ menuBar = self.CreateDefaultMenuBar(sdi=True)
toolBar = self.CreateDefaultToolBar()
self.SetToolBar(toolBar)
statusBar = self.CreateDefaultStatusBar()
self.SetMenuBar(menuBar) # wxBug: Need to do this in SDI to mimic MDI... because have to set the menubar at the very end or the automatic MDI "window" menu doesn't get put in the right place when the services add new menus to the menubar
- def GetDocumentManager(self):
+ def _LayoutFrame(self):
"""
- Returns the document manager associated with the DocSDIFrame.
+ Lays out the Frame.
"""
- return self._docManager
-
+ self.Layout()
+
def OnExit(self, event):
"""
self)
- def InitializePrintData(self):
- """
- Initializes the PrintData that is used when printing.
- """
- self._printData = wx.PrintData()
- self._printData.SetPaperId(wx.PAPER_LETTER)
-
-
- def CreateDefaultStatusBar(self):
- """
- Creates the default StatusBar.
- """
- wx.lib.docview.DocChildFrame.CreateStatusBar(self)
- self.GetStatusBar().Show(wx.ConfigBase_Get().ReadInt("ViewStatusBar", True))
- self.UpdateStatus()
- return self.GetStatusBar()
-
-
- def CreateDefaultToolBar(self):
- """
- Creates the default ToolBar.
- """
- self._toolBar = self.CreateToolBar(wx.TB_HORIZONTAL | wx.NO_BORDER | wx.TB_FLAT)
- self._toolBar.AddSimpleTool(wx.ID_NEW, getNewBitmap(), _("New"), _("Creates a new document"))
- self._toolBar.AddSimpleTool(wx.ID_OPEN, getOpenBitmap(), _("Open"), _("Opens an existing document"))
- self._toolBar.AddSimpleTool(wx.ID_SAVE, getSaveBitmap(), _("Save"), _("Saves the active document"))
- self._toolBar.AddSimpleTool(SAVEALL_ID, getSaveAllBitmap(), _("Save All"), _("Saves all the active documents"))
- self._toolBar.AddSeparator()
- self._toolBar.AddSimpleTool(wx.ID_PRINT, getPrintBitmap(), _("Print"), _("Displays full pages"))
- self._toolBar.AddSimpleTool(wx.ID_PREVIEW, getPrintPreviewBitmap(), _("Print Preview"), _("Prints the active document"))
- self._toolBar.AddSeparator()
- self._toolBar.AddSimpleTool(wx.ID_CUT, getCutBitmap(), _("Cut"), _("Cuts the selection and puts it on the Clipboard"))
- self._toolBar.AddSimpleTool(wx.ID_COPY, getCopyBitmap(), _("Copy"), _("Copies the selection and puts it on the Clipboard"))
- self._toolBar.AddSimpleTool(wx.ID_PASTE, getPasteBitmap(), _("Paste"), _("Inserts Clipboard contents"))
- self._toolBar.AddSimpleTool(wx.ID_UNDO, getUndoBitmap(), _("Undo"), _("Reverses the last action"))
- self._toolBar.AddSimpleTool(wx.ID_REDO, getRedoBitmap(), _("Redo"), _("Reverses the last undo"))
- self._toolBar.Realize()
- self._toolBar.Show(wx.ConfigBase_Get().ReadInt("ViewToolBar", True))
- return self._toolBar
-
-
- def CreateDefaultMenuBar(self):
- """
- Creates the default MenuBar. Contains File, Edit, View, Tools, and Help menus.
- """
- menuBar = wx.MenuBar()
-
- fileMenu = wx.Menu()
- self._fileMenu = fileMenu
- fileMenu.Append(wx.ID_NEW, _("&New...\tCtrl+N"), _("Creates a new document"))
- fileMenu.Append(wx.ID_OPEN, _("&Open...\tCtrl+O"), _("Opens an existing document"))
- fileMenu.Append(wx.ID_CLOSE, _("&Close"), _("Closes the active document"))
- # fileMenu.Append(wx.ID_CLOSE_ALL, _("Close A&ll"), _("Closes all open documents"))
- fileMenu.AppendSeparator()
- fileMenu.Append(wx.ID_SAVE, _("&Save\tCtrl+S"), _("Saves the active document"))
- fileMenu.Append(wx.ID_SAVEAS, _("Save &As..."), _("Saves the active document with a new name"))
- fileMenu.Append(SAVEALL_ID, _("Save All\tCtrl+Shift+A"), _("Saves the all active documents"))
- wx.EVT_MENU(self, SAVEALL_ID, self.ProcessEvent)
- wx.EVT_UPDATE_UI(self, SAVEALL_ID, self.ProcessUpdateUIEvent)
- fileMenu.AppendSeparator()
- fileMenu.Append(wx.ID_PRINT, _("&Print\tCtrl+P"), _("Prints the active document"))
- fileMenu.Append(wx.ID_PREVIEW, _("Print Pre&view"), _("Displays full pages"))
- fileMenu.Append(wx.ID_PRINT_SETUP, _("Page Set&up"), _("Changes page layout settings"))
- fileMenu.AppendSeparator()
- if wx.Platform == '__WXMAC__':
- fileMenu.Append(wx.ID_EXIT, _("&Quit"), _("Closes this program"))
- else:
- fileMenu.Append(wx.ID_EXIT, _("E&xit"), _("Closes this program"))
- if self._docManager:
- self._docManager.FileHistoryUseMenu(fileMenu)
- self._docManager.FileHistoryAddFilesToMenu(fileMenu)
- menuBar.Append(fileMenu, _("&File"));
-
- editMenu = wx.Menu()
- editMenu.Append(wx.ID_UNDO, _("&Undo\tCtrl+Z"), _("Reverses the last action"))
- editMenu.Append(wx.ID_REDO, _("&Redo\tCtrl+Y"), _("Reverses the last undo"))
- editMenu.AppendSeparator()
- editMenu.Append(wx.ID_CUT, _("Cu&t\tCtrl+X"), _("Cuts the selection and puts it on the Clipboard"))
- wx.EVT_MENU(self, wx.ID_CUT, self.ProcessEvent)
- wx.EVT_UPDATE_UI(self, wx.ID_CUT, self.ProcessUpdateUIEvent)
- editMenu.Append(wx.ID_COPY, _("&Copy\tCtrl+C"), _("Copies the selection and puts it on the Clipboard"))
- wx.EVT_MENU(self, wx.ID_COPY, self.ProcessEvent)
- wx.EVT_UPDATE_UI(self, wx.ID_COPY, self.ProcessUpdateUIEvent)
- editMenu.Append(wx.ID_PASTE, _("&Paste\tCtrl+V"), _("Inserts Clipboard contents"))
- wx.EVT_MENU(self, wx.ID_PASTE, self.ProcessEvent)
- wx.EVT_UPDATE_UI(self, wx.ID_PASTE, self.ProcessUpdateUIEvent)
- editMenu.Append(wx.ID_CLEAR, _("Cle&ar\tDel"), _("Erases the selection"))
- wx.EVT_MENU(self, wx.ID_CLEAR, self.ProcessEvent)
- wx.EVT_UPDATE_UI(self, wx.ID_CLEAR, self.ProcessUpdateUIEvent)
- editMenu.AppendSeparator()
- editMenu.Append(wx.ID_SELECTALL, _("Select A&ll\tCtrl+A"), _("Selects all available data"))
- wx.EVT_MENU(self, wx.ID_SELECTALL, self.ProcessEvent)
- wx.EVT_UPDATE_UI(self, wx.ID_SELECTALL, self.ProcessUpdateUIEvent)
- menuBar.Append(editMenu, _("&Edit"))
- if self.GetDocument() and self.GetDocument().GetCommandProcessor():
- self.GetDocument().GetCommandProcessor().SetEditMenu(editMenu)
-
- viewMenu = wx.Menu()
- viewMenu.AppendCheckItem(VIEW_TOOLBAR_ID, _("&Toolbar"), _("Shows or hides the toolbar"))
- wx.EVT_MENU(self, VIEW_TOOLBAR_ID, self.OnViewToolBar)
- wx.EVT_UPDATE_UI(self, VIEW_TOOLBAR_ID, self.OnUpdateViewToolBar)
- viewMenu.AppendCheckItem(VIEW_STATUSBAR_ID, _("&Status Bar"), _("Shows or hides the status bar"))
- wx.EVT_MENU(self, VIEW_STATUSBAR_ID, self.OnViewStatusBar)
- wx.EVT_UPDATE_UI(self, VIEW_STATUSBAR_ID, self.OnUpdateViewStatusBar)
- menuBar.Append(viewMenu, _("&View"))
-
- helpMenu = wx.Menu()
- helpMenu.Append(wx.ID_ABOUT, _("&About" + " " + wx.GetApp().GetAppName()), _("Displays program information, version number, and copyright"))
- wx.EVT_MENU(self, wx.ID_ABOUT, self.OnAbout)
- menuBar.Append(helpMenu, _("&Help"))
-
- wx.EVT_COMMAND_FIND_CLOSE(self, -1, self.ProcessEvent)
-
- return menuBar
-## accelTable = wx.AcceleratorTable([
-## eval(_("wx.ACCEL_CTRL, ord('N'), wx.ID_NEW")),
-## eval(_("wx.ACCEL_CTRL, ord('O'), wx.ID_OPEN")),
-## eval(_("wx.ACCEL_CTRL, ord('S'), wx.ID_SAVE")),
-## eval(_("wx.ACCEL_CTRL, ord('Z'), wx.ID_UNDO")),
-## eval(_("wx.ACCEL_CTRL, ord('Y'), wx.ID_REDO")),
-## eval(_("wx.ACCEL_CTRL, ord('X'), wx.ID_CUT")),
-## eval(_("wx.ACCEL_CTRL, ord('C'), wx.ID_COPY")),
-## eval(_("wx.ACCEL_CTRL, ord('Z'), wx.ID_PASTE")),
-## (wx.ACCEL_NORMAL, wx.WXK_DELETE, wx.ID_CLEAR),
-## eval(_("wx.ACCEL_CTRL, ord('A'), wx.ID_SELECTALL"))
-## ])
-## self.SetAcceleratorTable(accelTable)
-
-
def ProcessEvent(self, event):
"""
Processes an event, searching event tables and calling zero or more
return wx.GetApp().ProcessUpdateUIEvent(event)
- def OnFileSaveAll(self, event):
- """
- Saves all of the currently open documents.
- """
- docs = wx.GetApp().GetDocumentManager().GetDocuments()
- for doc in docs:
- doc.Save()
-
-
- def OnAbout(self, event):
- """
- Invokes the about dialog.
- """
- aboutService = wx.GetApp().GetService(AboutService)
- if aboutService:
- aboutService.ShowAbout()
-
-
- def OnViewToolBar(self, event):
- """
- Toggles whether the ToolBar is visible.
- """
- self._toolBar.Show(not self._toolBar.IsShown())
- self.Layout()
-
-
- def OnUpdateViewToolBar(self, event):
- """
- Updates the View ToolBar menu item.
- """
- event.Check(self.GetToolBar().IsShown())
-
-
- def OnViewStatusBar(self, event):
- """
- Toggles whether the StatusBar is visible.
- """
- self.GetStatusBar().Show(not self.GetStatusBar().IsShown())
- self.Layout()
-
-
- def OnUpdateViewStatusBar(self, event):
- """
- Updates the View StatusBar menu item.
- """
- event.Check(self.GetStatusBar().IsShown())
-
-
- def UpdateStatus(self, message = _("Ready")):
- """
- Updates the StatusBar.
- """
- # wxBug: Menubar and toolbar help strings don't pop the status text back
- if self.GetStatusBar().GetStatusText() != message:
- self.GetStatusBar().PushStatusText(message)
-
-
def OnCloseWindow(self, event):
"""
Called when the window is saved. Enables services to help close the frame.
About Dialog Service that installs under the Help menu to show the properties of the current application.
"""
- def __init__(self, aboutDialog = None):
+ def __init__(self, aboutDialog=None, image=None):
"""
Initializes the AboutService.
"""
if aboutDialog:
self._dlg = aboutDialog
+ self._image = None
else:
self._dlg = AboutDialog # use default AboutDialog
+ self._image = image
def ShowAbout(self):
"""
Show the AboutDialog
"""
- dlg = self._dlg(wx.GetApp().GetTopWindow())
+ if self._image:
+ dlg = self._dlg(wx.GetApp().GetTopWindow(), self._image)
+ else:
+ dlg = self._dlg(wx.GetApp().GetTopWindow())
dlg.CenterOnScreen()
dlg.ShowModal()
dlg.Destroy()
Opens an AboutDialog. Shared by DocMDIParentFrame and DocSDIFrame.
"""
- def __init__(self, parent):
+ def __init__(self, parent, image=None):
"""
Initializes the about dialog.
"""
wx.Dialog.__init__(self, parent, -1, _("About ") + wx.GetApp().GetAppName(), style = wx.DEFAULT_DIALOG_STYLE)
- self.SetBackgroundColour(wx.WHITE)
sizer = wx.BoxSizer(wx.VERTICAL)
- splash_bmp = wx.Image("activegrid/tool/images/splash.jpg").ConvertToBitmap()
- image = wx.StaticBitmap(self, -1, splash_bmp, (0,0), (splash_bmp.GetWidth(), splash_bmp.GetHeight()))
- sizer.Add(image, 0, wx.ALIGN_CENTER|wx.ALL, 0)
- sizer.Add(wx.StaticText(self, -1, wx.GetApp().GetAppName()), 0, wx.ALIGN_LEFT|wx.ALL, 5)
+ if image:
+ imageItem = wx.StaticBitmap(self, -1, image.ConvertToBitmap(), (0,0), (image.GetWidth(), image.GetHeight()))
+ sizer.Add(imageItem, 0, wx.ALIGN_CENTER|wx.ALL, 0)
+ sizer.Add(wx.StaticText(self, -1, wx.GetApp().GetAppName()), 0, wx.ALIGN_CENTRE|wx.ALL, 5)
btn = wx.Button(self, wx.ID_OK)
sizer.Add(btn, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
self._customEventHandlers = []
- def InstallControls(self, frame, menuBar = None, toolBar = None, statusBar = None, document = None):
+ def InstallControls(self, frame, menuBar=None, toolBar=None, statusBar=None, document=None):
"""
Installs a File/Properties menu item.
"""
return False
- def ShowPropertiesDialog(self, filename = None):
+ def ShowPropertiesDialog(self, filename=None):
"""
Shows the PropertiesDialog for the specified file.
"""
self._customEventHandlers.remove(handler)
- def chopPath(self, text, length = 36):
+ def chopPath(self, text, length=36):
"""
Simple version of textwrap. textwrap.fill() unfortunately chops lines at spaces
and creates odd word boundaries. Instead, we will chop the path without regard to
"""
Initializes the properties dialog.
"""
- wx.Dialog.__init__(self, parent, -1, _("File Properties"), size = (310, 330))
+ wx.Dialog.__init__(self, parent, -1, _("File Properties"), size=(310, 330))
HALF_SPACE = 5
SPACE = 10
spacerGrid.Add(gridSizer, 0, wx.ALL, SPACE);
tab.SetSizer(spacerGrid)
notebook.AddPage(tab, _("General"))
- notebook.SetPageSize((310,200))
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(notebook, 0, wx.ALL | wx.EXPAND, SPACE)
return True
+ def Save(self):
+ """
+ Called when the ChildDocument is saved and does the minimum such that the
+ ChildDocument looks like a real Document to the framework.
+ """
+ return self.OnSaveDocument(self._documentFile)
+
+
+ def SaveAs(self):
+ """
+ Called when the ChildDocument is saved and does the minimum such that the
+ ChildDocument looks like a real Document to the framework.
+ """
+ return self.OnSaveDocument(self._documentFile)
+
+
class ChildDocTemplate(wx.lib.docview.DocTemplate):
"""
A ChildDocTemplate is a DocTemplate subclass that enables the creation of ChildDocuments
"""
- def __init__(self, manager, description, filter, dir, ext, docTypeName, viewTypeName, docType, viewType, flags = wx.lib.docview.TEMPLATE_INVISIBLE, icon = None):
+ def __init__(self, manager, description, filter, dir, ext, docTypeName, viewTypeName, docType, viewType, flags=wx.lib.docview.TEMPLATE_INVISIBLE, icon=None):
"""
Initializes the ChildDocTemplate.
"""
- wx.lib.docview.DocTemplate.__init__(self, manager, description, filter, dir, ext, docTypeName, viewTypeName, docType, viewType, flags = flags, icon = icon)
+ wx.lib.docview.DocTemplate.__init__(self, manager, description, filter, dir, ext, docTypeName, viewTypeName, docType, viewType, flags=flags, icon=icon)
- def CreateDocument(self, path, flags, data = None, parentDocument = None):
+ def CreateDocument(self, path, flags, data=None, parentDocument=None):
"""
Called when a ChildDocument is to be created and does the minimum such that the
ChildDocument looks like a real Document to the framework.
self.SELECT_MORE_WINDOWS_ID = wx.NewId()
- def InstallControls(self, frame, menuBar = None, toolBar = None, statusBar = None, document = None):
+ def InstallControls(self, frame, menuBar=None, toolBar=None, statusBar=None, document=None):
"""
Installs the Window menu.
"""
if not self.GetDocumentManager().GetFlags() & wx.lib.docview.DOC_SDI:
return # Only need windows menu for SDI mode, MDI frame automatically creates one
+ if not _WINDOWS: # Arrange All and window navigation doesn't work on Linux
+ return
+
windowMenu = wx.Menu()
- windowMenu.Append(self.ARRANGE_WINDOWS_ID, _("&Arrange All"), _("Arrange the open windows"))
+ item = windowMenu.Append(self.ARRANGE_WINDOWS_ID, _("&Arrange All"), _("Arrange the open windows"))
windowMenu.AppendSeparator()
wx.EVT_MENU(frame, self.ARRANGE_WINDOWS_ID, frame.ProcessEvent)
return [self.SELECT_WINDOW_1_ID, self.SELECT_WINDOW_2_ID, self.SELECT_WINDOW_3_ID, self.SELECT_WINDOW_4_ID, self.SELECT_WINDOW_5_ID, self.SELECT_WINDOW_6_ID, self.SELECT_WINDOW_7_ID, self.SELECT_WINDOW_8_ID, self.SELECT_WINDOW_9_ID]
- def _GetWindowMenuFrameList(self, currentFrame = None):
+ def _GetWindowMenuFrameList(self, currentFrame=None):
"""
Returns the Frame associated with each menu item in the Window menu.
"""
def getRedoImage():
stream = cStringIO.StringIO(getRedoData())
return ImageFromStream(stream)
+
+#----------------------------------------------------------------------------
+
+def getBlankData():
+ return \
+'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x10\x00\x00\x00\x10\x08\x06\
+\x00\x00\x00\x1f\xf3\xffa\x00\x00\x00\x04sBIT\x08\x08\x08\x08|\x08d\x88\x00\
+\x00\x00]IDAT8\x8d\xed\x931\x0e\xc00\x08\x03m\x92\xff\xff8q\x87\xb6C\x11\x89\
+\xa8X:\xd4\x13\x03:\x1b\x01\xa45T\xd4\xefBsh\xd7Hk\xdc\x02\x00@\x8a\x19$\xa1\
+9\x14A,\x95\xf3\x82G)\xd3\x00\xf24\xf7\x90\x1ev\x07\xee\x1e\xf4:\xc1J?\xe0\
+\x0b\x80\xc7\x1d\xf8\x1dg\xc4\xea7\x96G8\x00\xa8\x91\x19(\x85#P\x7f\x00\x00\
+\x00\x00IEND\xaeB`\x82'
+
+
+def getBlankBitmap():
+ return BitmapFromImage(getBlankImage())
+
+def getBlankImage():
+ stream = cStringIO.StringIO(getBlankData())
+ return ImageFromStream(stream)
+
+def getBlankIcon():
+ return wx.IconFromBitmap(getBlankBitmap())
+
+