X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/d1dc2b32e00303839aec0077aefcbc60bd712f00..8bb039c8b2915ef8e908be9175bcd98dcd98fce3:/wxPython/wx/lib/docview.py diff --git a/wxPython/wx/lib/docview.py b/wxPython/wx/lib/docview.py index 8f5f038ba7..5a2a46ad50 100644 --- a/wxPython/wx/lib/docview.py +++ b/wxPython/wx/lib/docview.py @@ -6,7 +6,7 @@ # # 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 #---------------------------------------------------------------------------- @@ -76,6 +76,10 @@ class Document(wx.EvtHandler): 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. """ @@ -86,16 +90,17 @@ class Document(wx.EvtHandler): """ 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 = [] @@ -210,6 +215,27 @@ class Document(wx.EvtHandler): 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. @@ -334,9 +360,27 @@ class Document(wx.EvtHandler): 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) @@ -433,6 +477,7 @@ class Document(wx.EvtHandler): 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() @@ -467,6 +512,7 @@ class Document(wx.EvtHandler): self.SetFilename(filename, True) self.Modify(False) + self.SetDocumentModificationDate() self.SetDocumentSaved(True) self.UpdateAllViews() return True @@ -548,6 +594,25 @@ class Document(wx.EvtHandler): 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") @@ -646,6 +711,29 @@ class Document(wx.EvtHandler): 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 @@ -753,7 +841,7 @@ class View(wx.EvtHandler): 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 = '' @@ -1318,7 +1406,7 @@ class DocManager(wx.EvtHandler): """ Creates a new document and reads in the selected file. """ - if not self.CreateDocument('', 0): + if not self.CreateDocument('', DEFAULT_DOCMAN_FLAGS): self.OnOpenFileFailure() @@ -1478,7 +1566,7 @@ class DocManager(wx.EvtHandler): """ 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): @@ -1709,6 +1797,11 @@ class DocManager(wx.EvtHandler): 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: @@ -1720,11 +1813,6 @@ class DocManager(wx.EvtHandler): 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: