#
# Created: 5/15/03
# CVS-ID: $Id$
-# Copyright: (c) 2003-2004 ActiveGrid, Inc. (Port of wxWindows classes by Julian Smart et al)
+# Copyright: (c) 2003-2005 ActiveGrid, Inc. (Port of wxWindows classes by Julian Smart et al)
# License: wxWindows license
#----------------------------------------------------------------------------
The document class can be used to model an application's file-based data. It
is part of the document/view framework supported by wxWindows, and cooperates
with the wxView, wxDocTemplate and wxDocManager classes.
+
+ Note this wxPython version also keeps track of the modification date of the
+ document and if it changes on disk outside of the application, we will warn the
+ user before saving to avoid clobbering the file.
"""
"""
wx.EvtHandler.__init__(self)
- self._documentModified = False
self._documentParent = parent
self._documentTemplate = None
self._commandProcessor = None
self._savedYet = False
+ self._writeable = True
self._documentTitle = None
self._documentFile = None
self._documentTypeName = None
self._documentModified = False
+ self._documentModificationDate = None
self._documentViews = []
self._documentModified = modify
+ def SetDocumentModificationDate(self, filename=None):
+ """
+ Saves the file's last modification date.
+ This is used to check if the file has been modified outside of the application.
+ This method has been added to wxPython and is not in wxWindows.
+ """
+ if not filename:
+ filename = self.GetFilename()
+ self._documentModificationDate = os.path.getmtime(filename)
+ print "debug print, file: %s set modification date to %s" % (filename, self._documentModificationDate)
+
+
+ def GetDocumentModificationDate(self):
+ """
+ Returns the file's modification date when it was loaded from disk.
+ This is used to check if the file has been modified outside of the application.
+ This method has been added to wxPython and is not in wxWindows.
+ """
+ return self._documentModificationDate
+
+
def GetViews(self):
"""
Returns the list whose elements are the views on the document.
Saves the document by calling OnSaveDocument if there is an associated
filename, or SaveAs if there is no filename.
"""
- if not self.IsModified() and self._savedYet:
+ if not self.IsModified(): # and self._savedYet: This was here, but if it is not modified who cares if it hasn't been saved yet?
return True
+ """ check for file modification outside of application """
+ if os.path.exists(self.GetFilename()) and os.path.getmtime(self.GetFilename()) != self.GetDocumentModificationDate():
+ print "debug print, File %s: new mod date %s, original mod date %s" % (self.GetFilename(), os.path.getmtime(self.GetFilename()), self.GetDocumentModificationDate())
+ msgTitle = wx.GetApp().GetAppName()
+ if not msgTitle:
+ msgTitle = _("Application")
+ res = wx.MessageBox(_("'%s' has been modified outside of %s. Overwrite '%s' with current changes?") % (self.GetPrintableName(), msgTitle, self.GetPrintableName()),
+ msgTitle,
+ wx.YES_NO | wx.CANCEL | wx.ICON_QUESTION,
+ self.GetDocumentWindow())
+
+ if res == wx.NO:
+ return True
+ elif res == wx.YES:
+ pass
+ else: # elif res == wx.CANCEL:
+ return False
+
if not self._documentFile or not self._savedYet:
return self.SaveAs()
return self.OnSaveDocument(self._documentFile)
self.SetFilename(filename, True)
self.Modify(False)
+ self.SetDocumentModificationDate()
self.SetDocumentSaved(True)
#if wx.Platform == '__WXMAC__': # Not yet implemented in wxPython
# wx.FileName(file).MacSetDefaultTypeAndCreator()
self.SetFilename(filename, True)
self.Modify(False)
+ self.SetDocumentModificationDate()
self.SetDocumentSaved(True)
self.UpdateAllViews()
return True
if not self.IsModified():
return True
+ """ check for file modification outside of application """
+ if os.path.exists(self.GetFilename()) and os.path.getmtime(self.GetFilename()) != self.GetDocumentModificationDate():
+ print "debug print, File %s: new mod date %s, original mod date %s" % (self.GetFilename(), os.path.getmtime(self.GetFilename()), self.GetDocumentModificationDate())
+ msgTitle = wx.GetApp().GetAppName()
+ if not msgTitle:
+ msgTitle = _("Warning")
+ res = wx.MessageBox(_("'%s' has been modified outside of %s. Overwrite '%s' with current changes?") % (self.GetPrintableName(), msgTitle, self.GetPrintableName()),
+ msgTitle,
+ wx.YES_NO | wx.CANCEL | wx.ICON_QUESTION,
+ self.GetDocumentWindow())
+
+ if res == wx.NO:
+ self.Modify(False)
+ return True
+ elif res == wx.YES:
+ return wx.lib.docview.Document.Save(self)
+ else: # elif res == wx.CANCEL:
+ return False
+
msgTitle = wx.GetApp().GetAppName()
if not msgTitle:
msgTitle = _("Warning")
view.OnChangeFilename()
+ def GetWriteable(self):
+ """
+ Returns true if the document can be written to its accociated file path.
+ This method has been added to wxPython and is not in wxWindows.
+ """
+ if not self._writeable:
+ return False
+ if not self._documentFile: # Doesn't exist, do a save as
+ return True
+ else:
+ return os.access(self._documentFile, os.W_OK)
+
+
+ def SetWriteable(self, writeable):
+ """
+ Set to False if the document can not be saved. This will disable the ID_SAVE_AS
+ event and is useful for custom documents that should not be saveable. The ID_SAVE
+ event can be disabled by never Modifying the document. This method has been added
+ to wxPython and is not in wxWindows.
+ """
+ self._writeable = writeable
+
+
class View(wx.EvtHandler):
"""
The view class can be used to model the viewing and editing component of
else:
return
else:
- if appName and not isinstance(self.GetFrame(), DocMDIChildFrame): # Don't need appname in title for MDI
+ if appName and isinstance(self.GetFrame(), DocChildFrame): # Only need app name in title for SDI
title = appName + _(" - ")
else:
title = ''
"""
Creates a new document and reads in the selected file.
"""
- if not self.CreateDocument('', 0):
+ if not self.CreateDocument('', DEFAULT_DOCMAN_FLAGS):
self.OnOpenFileFailure()
"""
Updates the user interface for the File Save As command.
"""
- event.Enable(self.GetCurrentDocument() != None)
+ event.Enable(self.GetCurrentDocument() != None and self.GetCurrentDocument().GetWriteable())
def OnUpdateUndo(self, event):
else:
return None
+ if path and flags & DOC_SILENT:
+ temp = self.FindTemplateForPath(path)
+ else:
+ temp, path = self.SelectDocumentPath(templates, path, flags)
+
# Existing document
if self.GetFlags() & DOC_OPEN_ONCE:
for document in self._docs:
firstView.GetFrame().Iconize(False)
return None
- if flags & DOC_SILENT:
- temp = self.FindTemplateForPath(path)
- else:
- temp, path = self.SelectDocumentPath(templates, path, flags)
-
if temp:
newDoc = temp.CreateDocument(path, flags)
if newDoc: