Rich Text Editor in wxPython

Hi -
  I am involved with writing an editor. I am looking for a simple text
control component component that can do some rich text formatting.
Some interesting features would be :

* Ctrl-B / Ctrl-I / Ctrl-U for bold/italics/underline respectively.

* Provision to generate the formatted code as html from the control.

If any of you know a similar derived component from wxWidgets -
kindly let me know .

Thanks for the help.

Did you see the OnFileSaveAs function in the code of the RichTextCtrl as shown in the demo? Currently RichTextCtrl can save as plain text, XML, or HTML.

···

On Sat, Mar 15, 2008 at 7:46 AM, Rakesh Sinha rakesh.usenet@gmail.com wrote:

Thanks Stef.

This following piece of code was able to format the text as bold /

italic / underlined etc.

<---------------------->

class MyCtrl(rt.RichTextCtrl):

def __init__(self, parent):

    rt.RichTextCtrl.__init__(self, parent,

style=wx.VSCROLL|wx.HSCROLL|wx.NO_BORDER, size = wx.Size(800, 400))

    self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)



def OnKeyDown(self, evt):

    keycode = evt.GetKeyCode()



    if evt.ControlDown():

        if (keycode == ord('B') ):

            self.ApplyBoldToSelection()

        elif (keycode == ord('I')):

            self.ApplyItalicToSelection()

        elif (keycode == ord('U')):

            self.ApplyUnderlineToSelection()



    evt.Skip()

<--------------------->

I am still trying to figure out how to get the formatted text in

wx.RichTextCtrl to generate a HTML code.

Thanks C M .

This code snippet ( copied from the examples as pointed to , in the
thread ) - does the job.

self - class inherited from rt.RichTextCtrl : ...

    def GetHtmlContent(self):
        # 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.GetBuffer(), stream):
            return ""

        return stream.getvalue()

···

On Sat, Mar 15, 2008 at 8:50 AM, C M <cmpython@gmail.com> wrote:

On Sat, Mar 15, 2008 at 7:46 AM, Rakesh Sinha <rakesh.usenet@gmail.com> > wrote:
> Thanks Stef.
>
> This following piece of code was able to format the text as bold /
> italic / underlined etc.
>
> <---------------------->
>
> class MyCtrl(rt.RichTextCtrl):
> def __init__(self, parent):
> rt.RichTextCtrl.__init__(self, parent,
> style=wx.VSCROLL|wx.HSCROLL|wx.NO_BORDER, size = wx.Size(800, 400))
>
> self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
>
> def OnKeyDown(self, evt):
> keycode = evt.GetKeyCode()
>
> if evt.ControlDown():
> if (keycode == ord('B') ):
> self.ApplyBoldToSelection()
> elif (keycode == ord('I')):
> self.ApplyItalicToSelection()
> elif (keycode == ord('U')):
> self.ApplyUnderlineToSelection()
>
> evt.Skip()
>
> <--------------------->
>
> I am still trying to figure out how to get the formatted text in
> wx.RichTextCtrl to generate a HTML code.
>

Did you see the OnFileSaveAs function in the code of the RichTextCtrl as
shown in the demo? Currently RichTextCtrl can save as plain text, XML, or
HTML.