X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/30fc5e8fee1557e4049d35d416d76a2bebdd8780..a15107c002d8dee9e64692ecb307e41c5f90d70d:/wxPython/demo/RichTextCtrl.py diff --git a/wxPython/demo/RichTextCtrl.py b/wxPython/demo/RichTextCtrl.py index c19c0c7207..a913bea699 100644 --- a/wxPython/demo/RichTextCtrl.py +++ b/wxPython/demo/RichTextCtrl.py @@ -17,7 +17,7 @@ class RichTextFrame(wx.Frame): self.rtc = rt.RichTextCtrl(self, style=wx.VSCROLL|wx.HSCROLL|wx.NO_BORDER); wx.CallAfter(self.rtc.SetFocus) - + self.rtc.Freeze() self.rtc.BeginSuppressUndo() self.rtc.BeginParagraphSpacing(0, 20) @@ -86,12 +86,12 @@ class RichTextFrame(wx.Frame): self.rtc.BeginLeftIndent(100, -40) self.rtc.Newline() - self.rtc.WriteText("It was in January, the most down-trodden month of an Edinburgh winteself.rtc. An attractive woman came into the cafe, which is nothing remarkable.") + self.rtc.WriteText("It was in January, the most down-trodden month of an Edinburgh winter. An attractive woman came into the cafe, which is nothing remarkable.") self.rtc.EndLeftIndent() self.rtc.Newline() - self.rtc.WriteText("Numbered bullets are possible, again using subindents:") + self.rtc.WriteText("Numbered bullets are possible, again using sub-indents:") self.rtc.BeginNumberedBullet(1, 100, 60) self.rtc.Newline() @@ -172,18 +172,21 @@ class RichTextFrame(wx.Frame): self.rtc.EndParagraphSpacing() self.rtc.EndSuppressUndo() - + self.rtc.Thaw() + def OnFileOpen(self, evt): - # TODO: Use RichTextBuffer.GetExtWildcard to get the wildcard string + # This gives us a string suitable for the file dialog based on + # the file handlers that are loaded + wildcard, types = rt.RichTextBuffer.GetExtWildcard(save=False) dlg = wx.FileDialog(self, "Choose a filename", - wildcard="All files (*.*)|*.*", + wildcard=wildcard, style=wx.OPEN) if dlg.ShowModal() == wx.ID_OK: path = dlg.GetPath() if path: - # TODO: use the filter index to determine what file type to use - self.rtc.LoadFile(path, rt.RICHTEXT_TYPE_TEXT) + fileType = types[dlg.GetFilterIndex()] + self.rtc.LoadFile(path, fileType) dlg.Destroy() @@ -192,19 +195,53 @@ class RichTextFrame(wx.Frame): self.OnFileSaveAs(evt) return self.rtc.SaveFile() + def OnFileSaveAs(self, evt): - # TODO: Use RichTextBuffer.GetExtWildcard to get the wildcard string + wildcard, types = rt.RichTextBuffer.GetExtWildcard(save=True) + dlg = wx.FileDialog(self, "Choose a filename", - wildcard="All files (*.*)|*.*", - style=wx.OPEN) + wildcard=wildcard, + style=wx.SAVE) if dlg.ShowModal() == wx.ID_OK: path = dlg.GetPath() if path: - self.rtc.SaveFile(path) - + fileType = types[dlg.GetFilterIndex()] + ext = rt.RichTextBuffer.FindHandlerByType(fileType).GetExtension() + if not path.endswith(ext): + path += '.' + ext + self.rtc.SaveFile(path, fileType) + dlg.Destroy() + - def OnFileViewHTML(self, evt): pass + def OnFileViewHTML(self, evt): + # Get an instance of the html file handler, use it to save the + # document to a StringIO stream, and then display the + # resulting html text in a dialog with a HtmlWindow. + handler = rt.RichTextHTMLHandler() + handler.SetFlags(rt.RICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY) + handler.SetFontSizeMapping([7,9,11,12,14,22,100]) + + import cStringIO + stream = cStringIO.StringIO() + if not handler.SaveStream(self.rtc.GetBuffer(), stream): + return + + import wx.html + dlg = wx.Dialog(self, title="HTML", style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER) + html = wx.html.HtmlWindow(dlg, size=(500,400), style=wx.BORDER_SUNKEN) + html.SetPage(stream.getvalue()) + btn = wx.Button(dlg, wx.ID_CANCEL) + sizer = wx.BoxSizer(wx.VERTICAL) + sizer.Add(html, 1, wx.ALL|wx.EXPAND, 5) + sizer.Add(btn, 0, wx.ALL|wx.CENTER, 10) + dlg.SetSizer(sizer) + sizer.Fit(dlg) + + dlg.ShowModal() + + handler.DeleteTemporaryImages() + def OnFileExit(self, evt): @@ -335,6 +372,7 @@ class RichTextFrame(wx.Frame): r = self.rtc.GetSelectionRange() fontData = wx.FontData() + fontData.EnableEffects(False) attr = rt.RichTextAttr() attr.SetFlags(rt.TEXT_ATTR_FONT) if self.rtc.GetStyle(self.rtc.GetInsertionPoint(), attr): @@ -351,6 +389,29 @@ class RichTextFrame(wx.Frame): dlg.Destroy() + def OnColour(self, evt): + colourData = wx.ColourData() + attr = rt.RichTextAttr() + attr.SetFlags(rt.TEXT_ATTR_TEXT_COLOUR) + if self.rtc.GetStyle(self.rtc.GetInsertionPoint(), attr): + colourData.SetColour(attr.GetTextColour()) + + dlg = wx.ColourDialog(self, colourData) + if dlg.ShowModal() == wx.ID_OK: + colourData = dlg.GetColourData() + colour = colourData.GetColour() + if colour: + if not self.rtc.HasSelection(): + self.rtc.BeginTextColour(colour) + else: + r = self.rtc.GetSelectionRange() + attr.SetFlags(rt.TEXT_ATTR_TEXT_COLOUR) + attr.SetTextColour(colour) + self.rtc.SetStyle(r, attr) + dlg.Destroy() + + + def OnUpdateBold(self, evt): evt.Check(self.rtc.IsSelectionBold()) @@ -498,6 +559,8 @@ class RichTextFrame(wx.Frame): tbar.AddSeparator() doBind( tbar.AddTool(-1, images.get_rt_fontBitmap(), shortHelpString="Font"), self.OnFont) + doBind( tbar.AddTool(-1, images.get_rt_colourBitmap(), + shortHelpString="Font Colour"), self.OnColour) tbar.Realize() @@ -505,6 +568,7 @@ class RichTextFrame(wx.Frame): #---------------------------------------------------------------------- + class TestPanel(wx.Panel): def __init__(self, parent, log): self.log = log @@ -513,6 +577,30 @@ class TestPanel(wx.Panel): b = wx.Button(self, -1, "Show the RichTextCtrl sample", (50,50)) self.Bind(wx.EVT_BUTTON, self.OnButton, b) + self.AddRTCHandlers() + + + def AddRTCHandlers(self): + # make sure we haven't already added them. + if rt.RichTextBuffer.FindHandlerByType(rt.RICHTEXT_TYPE_HTML) is not None: + return + + # This would normally go in your app's OnInit method. I'm + # not sure why these file handlers are not loaded by + # default by the C++ richtext code, I guess it's so you + # can change the name or extension if you wanted... + rt.RichTextBuffer.AddHandler(rt.RichTextHTMLHandler()) + rt.RichTextBuffer.AddHandler(rt.RichTextXMLHandler()) + + # ...like this + rt.RichTextBuffer.AddHandler(rt.RichTextXMLHandler(name="Other XML", + ext="ox", + type=99)) + + # This is needed for the view as HTML option since we tell it + # to store the images in the memory file system. + wx.FileSystem.AddHandler(wx.MemoryFSHandler()) + def OnButton(self, evt): win = RichTextFrame(self, -1, "wx.richtext.RichTextCtrl", @@ -520,10 +608,12 @@ class TestPanel(wx.Panel): style = wx.DEFAULT_FRAME_STYLE) win.Show(True) - # give easy access to PyShell if it's running + # give easy access to the demo's PyShell if it's running self.rtfrm = win self.rtc = win.rtc + + #---------------------------------------------------------------------- def runTest(frame, nb, log):