views:

23

answers:

1

I have found only two methods (SaveFile, LoadFile) which save in a file. How in wxRichTextCtrl to save XML the data directly in a variable?

A: 

You can use control.GetBuffer() to get RichTextBuffer and use RichTextXMLHandler to save buffer to a stream, which could be any file type object e.g. StringIO e.g. if rt is your rich text control

import cStringIO
buf = cStringIO.StringIO()
handler = wx.richtext.RichTextXMLHandler()
handler.SetFlags(wx.richtext.RICHTEXT_HANDLER_INCLUDE_STYLESHEET)
handler.SaveStream(rt.GetBuffer(), buf)
print buf.getvalue()

here is a complete example which you can run and see it prints out xml, when you click outside rich text cntrl in frame

import wx
import wx.richtext

app = wx.App(False)
f=wx.Frame(None, title="Test")
f.Show()
rt = wx.richtext.RichTextCtrl(f, size=(200,200))
def onEvent(evt):
    import cStringIO
    buf = cStringIO.StringIO()
    handler = wx.richtext.RichTextXMLHandler()
    handler.SetFlags(wx.richtext.RICHTEXT_HANDLER_INCLUDE_STYLESHEET)
    handler.SaveStream(rt.GetBuffer(), buf)
    print buf.getvalue()

f.Bind(wx.EVT_LEFT_DOWN, onEvent)
app.MainLoop()

output:

<?xml version="1.0" encoding="UTF-8"?>
<richtext version="1.0.0.0" xmlns="http://www.wxwidgets.org"&gt;
  <paragraphlayout textcolor="#101010" fontsize="10" fontstyle="90" fontweight="90" fontunderlined="0" fontface="Sans" alignment="1" parspacingafter="10" parspacingbefore="0" linespacing="10">
    <paragraph>
      <text>sdsa</text>
    </paragraph>
  </paragraphlayout>
</richtext>
Anurag Uniyal