X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/b792147db7e5ceb5da741f5f6e27fea79944a04c..1f3b7817ebf3dd04a72645fe3f5817ec6ee9570c:/wxPython/samples/pydocview/TextEditor.py diff --git a/wxPython/samples/pydocview/TextEditor.py b/wxPython/samples/pydocview/TextEditor.py index 496c489802..28acf21868 100644 --- a/wxPython/samples/pydocview/TextEditor.py +++ b/wxPython/samples/pydocview/TextEditor.py @@ -17,45 +17,50 @@ import FindService _ = wx.GetTranslation class TextDocument(wx.lib.docview.Document): + + def __init__(self): + wx.lib.docview.Document .__init__(self) + self._inModify = False - def OnSaveDocument(self, filename): + def SaveObject(self, fileObject): view = self.GetFirstView() - if not view.GetTextCtrl().SaveFile(filename): - return False - self.Modify(False) - self.SetDocumentSaved(True) - #if wx.Platform == "__WXMAC__": - # fn = wx.Filename(filename) - # fn.MacSetDefaultTypeAndCreator() + val = view.GetTextCtrl().GetValue() + if wx.USE_UNICODE: + val = val.encode('utf-8') + fileObject.write(val) return True - def OnOpenDocument(self, filename): + def LoadObject(self, fileObject): view = self.GetFirstView() - if not view.GetTextCtrl().LoadFile(filename): - return False - self.SetFilename(filename, True) - self.Modify(False) - self.UpdateAllViews() - self._savedYet = True + data = fileObject.read() + if wx.USE_UNICODE: + data = data.decode('utf-8') + view.GetTextCtrl().SetValue(data) return True def IsModified(self): view = self.GetFirstView() if view and view.GetTextCtrl(): - return wx.lib.docview.Document.IsModified(self) or view.GetTextCtrl().IsModified() - else: - return wx.lib.docview.Document.IsModified(self) + return view.GetTextCtrl().IsModified() + return False - def Modify(self, mod): + def Modify(self, modify): + if self._inModify: + return + self._inModify = True + view = self.GetFirstView() - wx.lib.docview.Document.Modify(self, mod) - if not mod and view and view.GetTextCtrl(): + if not modify and view and view.GetTextCtrl(): view.GetTextCtrl().DiscardEdits() + wx.lib.docview.Document.Modify(self, modify) # this must called be after the DiscardEdits call above. + + self._inModify = False + class TextView(wx.lib.docview.View): @@ -75,6 +80,7 @@ class TextView(wx.lib.docview.View): sizer = wx.BoxSizer() font, color = self._GetFontAndColorFromConfig() self._textCtrl = self._BuildTextCtrl(frame, font, color = color) + self._textCtrl.Bind(wx.EVT_TEXT, self.OnModify) sizer.Add(self._textCtrl, 1, wx.EXPAND, 0) frame.SetSizer(sizer) frame.Layout() @@ -83,12 +89,16 @@ class TextView(wx.lib.docview.View): return True + def OnModify(self, event): + self.GetDocument().Modify(True) + + def _BuildTextCtrl(self, parent, font, color = wx.BLACK, value = "", selection = [0, 0]): if self._wordWrap: wordWrapStyle = wx.TE_WORDWRAP else: wordWrapStyle = wx.TE_DONTWRAP - textCtrl = wx.TextCtrl(parent, -1, pos = wx.DefaultPosition, size = parent.GetClientSize(), style = wx.TE_MULTILINE | wordWrapStyle) + textCtrl = wx.TextCtrl(parent, -1, pos = wx.DefaultPosition, size = parent.GetClientSize(), style = wx.TE_MULTILINE | wx.TE_RICH | wordWrapStyle) textCtrl.SetFont(font) textCtrl.SetForegroundColour(color) textCtrl.SetValue(value) @@ -131,6 +141,9 @@ class TextView(wx.lib.docview.View): def OnUpdate(self, sender = None, hint = None): + if wx.lib.docview.View.OnUpdate(self, sender, hint): + return + if hint == "Word Wrap": self.SetWordWrap(wx.ConfigBase_Get().ReadInt("TextEditorWordWrap", True)) elif hint == "Font": @@ -513,7 +526,7 @@ class TextOptionsPanel(wx.Panel): nativeFont.FromString(self._textFont.GetNativeFontInfoDesc()) font = wx.NullFont font.SetNativeFontInfo(nativeFont) - font.SetPointSize(self._sampleTextCtrl.GetFont().GetPointSize()) # Use the standard point size + #font.SetPointSize(self._sampleTextCtrl.GetFont().GetPointSize()) # Use the standard point size self._sampleTextCtrl.SetFont(font) self._sampleTextCtrl.SetForegroundColour(self._textColor) self._sampleTextCtrl.SetValue(_("%d pt. %s") % (self._textFont.GetPointSize(), self._textFont.GetFaceName()))