import os
import os.path
+import shutil
import wx
import sys
_ = wx.GetTranslation
msgTitle = _("File Error")
backupFilename = None
+ fileObject = None
+ copied = False
try:
# if current file exists, move it to a safe place temporarily
if os.path.exists(filename):
while os.path.exists(backupFilename):
i += 1
backupFilename = "%s.bak%s" % (filename, i)
- os.rename(filename, backupFilename)
+ shutil.copy(filename, backupFilename)
+ copied = True
fileObject = file(filename, 'w')
self.SaveObject(fileObject)
fileObject.close()
-
+ fileObject = None
+
if backupFilename:
os.remove(backupFilename)
except:
- # save failed, restore old file
- if backupFilename:
- os.remove(filename)
- os.rename(backupFilename, filename)
- self.SetDocumentModificationDate()
+ # for debugging purposes
+ import traceback
+ traceback.print_exc()
+
+ if fileObject:
+ fileObject.close() # file is still open, close it, need to do this before removal
+
+ # save failed, remove copied file
+ if backupFilename and copied:
+ os.remove(backupFilename)
wx.MessageBox("Could not save '%s'. %s" % (FileNameFromPath(filename), sys.exc_value),
msgTitle,
fileObject = file(filename, 'r')
try:
self.LoadObject(fileObject)
+ fileObject.close()
+ fileObject = None
except:
+ # for debugging purposes
+ import traceback
+ traceback.print_exc()
+
+ if fileObject:
+ fileObject.close() # file is still open, close it
+
wx.MessageBox("Could not open '%s'. %s" % (FileNameFromPath(filename), sys.exc_value),
msgTitle,
wx.OK | wx.ICON_EXCLAMATION,
Override to return an instance of a class other than wxDocPrintout.
"""
- return DocPrintout(self)
+ return DocPrintout(self, self.GetDocument().GetPrintableName())
def GetFrame(self):
printout = view.OnCreatePrintout()
if printout:
- pdd = wx.PrintDialogData()
+ if not hasattr(self, "printData"):
+ self.printData = wx.PrintData()
+ self.printData.SetPaperId(wx.PAPER_LETTER)
+ self.printData.SetPrintMode(wx.PRINT_MODE_PRINTER)
+
+ pdd = wx.PrintDialogData(self.printData)
printer = wx.Printer(pdd)
- printer.Print(view.GetFrame(), printout) # , True)
+ printer.Print(view.GetFrame(), printout)
def OnPrintSetup(self, event):
else:
parentWin = wx.GetApp().GetTopWindow()
- data = wx.PrintDialogData()
+ if not hasattr(self, "printData"):
+ self.printData = wx.PrintData()
+ self.printData.SetPaperId(wx.PAPER_LETTER)
+
+ data = wx.PrintDialogData(self.printData)
printDialog = wx.PrintDialog(parentWin, data)
printDialog.GetPrintDialogData().SetSetupDialog(True)
printDialog.ShowModal()
- # TODO: Confirm that we don't have to remember PrintDialogData
+
+ # this makes a copy of the wx.PrintData instead of just saving
+ # a reference to the one inside the PrintDialogData that will
+ # be destroyed when the dialog is destroyed
+ self.printData = wx.PrintData(printDialog.GetPrintDialogData().GetPrintData())
+
+ printDialog.Destroy()
def OnPreview(self, event):
printout = view.OnCreatePrintout()
if printout:
+ if not hasattr(self, "printData"):
+ self.printData = wx.PrintData()
+ self.printData.SetPaperId(wx.PAPER_LETTER)
+ self.printData.SetPrintMode(wx.PRINT_MODE_PREVIEW)
+
+ data = wx.PrintDialogData(self.printData)
# Pass two printout objects: for preview, and possible printing.
- preview = wx.PrintPreview(printout, view.OnCreatePrintout())
+ preview = wx.PrintPreview(printout, view.OnCreatePrintout(), data)
+ if not preview.Ok():
+ wx.MessageBox(_("Unable to display print preview."))
+ return
# wxWindows source doesn't use base frame's pos, size, and icon, but did it this way so it would work like MS Office etc.
mimicFrame = wx.GetApp().GetTopWindow()
frame = wx.PreviewFrame(preview, mimicFrame, _("Print Preview"), mimicFrame.GetPosition(), mimicFrame.GetSize())
frame.SetIcon(mimicFrame.GetIcon())
- frame.SetTitle(mimicFrame.GetTitle() + _(" - Preview"))
+ frame.SetTitle(_("%s - %s - Preview") % (mimicFrame.GetTitle(), view.GetDocument().GetPrintableName()))
frame.Initialize()
frame.Show(True)
if doc and doc.GetCommandProcessor():
doc.GetCommandProcessor().SetMenuStrings()
else:
- event.SetText(_("Undo") + '\t' + _('Ctrl+Z'))
+ event.SetText(_("&Undo\tCtrl+Z"))
def OnUpdateRedo(self, event):
if doc and doc.GetCommandProcessor():
doc.GetCommandProcessor().SetMenuStrings()
else:
- event.SetText(_("Redo") + '\t' + _('Ctrl+Y'))
+ event.SetText(_("&Redo\tCtrl+Y"))
def OnUpdatePrint(self, event):
temp, path = self.SelectDocumentPath(templates, path, flags)
# Existing document
- if self.GetFlags() & DOC_OPEN_ONCE:
+ if path and self.GetFlags() & DOC_OPEN_ONCE:
for document in self._docs:
- if document.GetFilename() == path:
+ if document.GetFilename() and os.path.normcase(document.GetFilename()) == os.path.normcase(path):
""" check for file modification outside of application """
if os.path.exists(path) and os.path.getmtime(path) != document.GetDocumentModificationDate():
msgTitle = wx.GetApp().GetAppName()
return None
- def CreateView(self, document, flags=0):
+ def CreateView(self, doc, flags=0):
"""
Creates a new view for the given document. If more than one view is
allowed for the document (by virtue of multiple templates mentioning
Given a path, try to find template that matches the extension. This is
only an approximate method of finding a template for creating a
document.
+
+ Note this wxPython verson looks for and returns a default template if no specific template is found.
"""
+ default = None
for temp in self._templates:
if temp.FileMatchesTemplate(path):
return temp
- return None
+
+ if "*.*" in temp.GetFileFilter():
+ default = temp
+ return default
def FindSuitableParent(self):
allfilter = allfilter + _(';')
descr = descr + temp.GetDescription() + _(" (") + temp.GetFileFilter() + _(") |") + temp.GetFileFilter() # spacing is important, make sure there is no space after the "|", it causes a bug on wx_gtk
allfilter = allfilter + temp.GetFileFilter()
- descr = _("All") + _(" (") + allfilter + _(") |") + allfilter + _('|') + descr # spacing is important, make sure there is no space after the "|", it causes a bug on wx_gtk
+ descr = _("All (%s)|%s|%s|Any (*.*) | *.*") % (allfilter, allfilter, descr) # spacing is important, make sure there is no space after the "|", it causes a bug on wx_gtk
else:
descr = _("*.*")
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
+ if self._childDocument: # This isn't in the wxWindows codebase but the document needs to be disposed of somehow
+ self._childDocument.DeleteContents()
+ if self._childDocument.GetDocumentManager():
+ self._childDocument.GetDocumentManager().RemoveDocument(self._childDocument)
self._childDocument = None
self.Destroy()
else:
"""
Constructor.
"""
- wx.Printout.__init__(self)
+ wx.Printout.__init__(self, title)
self._printoutView = view
else:
redoAccel = ''
if undoCommand and undoItem and undoCommand.CanUndo():
- undoItem.SetText(_("Undo ") + undoCommand.GetName() + undoAccel)
+ undoItem.SetText(_("&Undo ") + undoCommand.GetName() + undoAccel)
#elif undoCommand and not undoCommand.CanUndo():
# undoItem.SetText(_("Can't Undo") + undoAccel)
else:
- undoItem.SetText(_("Undo" + undoAccel))
+ undoItem.SetText(_("&Undo" + undoAccel))
if redoCommand and redoItem:
- redoItem.SetText(_("Redo ") + redoCommand.GetName() + redoAccel)
+ redoItem.SetText(_("&Redo ") + redoCommand.GetName() + redoAccel)
else:
- redoItem.SetText(_("Redo") + redoAccel)
+ redoItem.SetText(_("&Redo") + redoAccel)
def CanUndo(self):