wxWidgets: wxRichTextCtrl

So I've subclassed wxRichTextCtrl (inherited from it) in order to be able to use it. What I need to do is get all the text (styles and all) that is stored within the wxRichTextCtrl as a single string.

The problem I'm having is that I don't seem to be able to do this. wxRichTextCtrl must have more than a hundred member functions, and I'm softed through a lot of it. I've tried using the buffer, but that didn't work. I read that it apparently isn't updated (for reasons beyond my comprehension) and the "solution" I tried wasn't a solution at all. So, I'm stuck in this boat without a paddle, and I am out of ideas...

Is there anyone that has used wxRichTextCtrl that can help? I don't want to write directly to a file because the contents of the control are part of a larger set of data.
nvm...

After spending maybe a week on this stupid thing, I just came accross the answer, as unintentionally well hidden as it was.

I guess I'm supposed to use a file handler ( http://docs.wxwidgets.org/3.0/classwx_rich_text_x_m_l_handler.html ) to "export" and "import" into the buffer...

So, for anyone who comes accross this thread and wonders "what the heck the solution was?", here is the solution:

Within a class that inherits from wxRichTextCtrl:

1
2
3
4
5
6
7
8
9
10
11
std::wstring my_object::buffer_contents()
    {
        wxStringOutputStream s;
        wxRichTextXMLHandler handler{"", ""};
        handler.SetFlags(handler.GetFlags() | wxRICHTEXT_HANDLER_INCLUDE_STYLESHEET);
        if(!handler.ExportXML(s, this->GetBuffer(), 1))
        {
            throw std::runtime_error{"Error, could not extract contents of wxRichTextCtrl!"};
        }
        return s.GetString().ToStdWstring();
    }


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void my_object::content(const wxString& s)
{
    wxStringInputStream sis{s};
    wxRichTextXMLHandler handler;
    wxXmlDocument doc;
    
    doc.Load(sis, "UTF-8", wxXMLDOC_KEEP_WHITESPACE_NODES);
    handler.ImportXML(nullptr, &this->GetBuffer(), doc.GetRoot());
    this->DiscardEdits();
    this->SetInsertionPoint(0);
    this->LayoutContent();
    this->PositionCaret();
    this->SetupScrollbars(true);
    this->Refresh(false);
}


My only problem now is that a newline is inserted at the beginning of the control... Any suggestions?
Last edited on
I'm not quite sure what you are trying to accomplish?

The easier way to get a string from the control would be using the wxRichTextBuffer:

http://docs.wxwidgets.org/trunk/classwx_rich_text_buffer.html

The members of wxRichTextBuffer LoadFile(...)/SaveFile(...) reads/writes the data from/to a stream.

You can use wxStringOutputStream to get a string from this.
I tried that, but it produced an empty string.

What I'm trying to accomplish: I need everything that is in the control as a string, and I need to be able to set it equal to a string.
Last edited on
Does this work for you?
1
2
3
4
auto buf = m_richTextCtrl->GetBuffer();
	buf.AddHandler(new wxRichTextXMLHandler());
	wxStringOutputStream out;
	buf.SaveFile(out, wxRICHTEXT_TYPE_XML);
@naraku9333
That seems to work. But it doesn't work when I call buf.LoadFile(in, wxRICHTEXT_TYPE_XML) interestingly enough, using that method seems to negate my solution... Now when I try to fill the buffer, it doesn't no matter what I do.
Last edited on
Can you give this a try?
1
2
3
wxStringInputStream in(...);
wxRichTextXMLHandler h2;
if (!h2.LoadFile(&m_richTextCtrl->GetBuffer(), in)) { wxMessageBox("failed to load text"); }
Wow, it works!

So now my solution is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void my_class::content(const wxString& s)
{
    wxStringInputStream in{s};
    wxRichTextXMLHandler handler;
    
    if(!handler.LoadFile(&this->GetBuffer(), in))
    {
        throw std::runtime_error{"Failed to set the contents of wxRichTextCtrl!"};
    }
    
    this->DiscardEdits();
    this->SetInsertionPoint(0);
    this->LayoutContent();
    this->PositionCaret();
    this->SetupScrollbars(true);
    this->Refresh(false);
}


1
2
3
4
5
6
7
8
9
std::wstring my_class::content()
{
    wxRichTextXMLHandler *handler{new wxRichTextXMLHandler};
    wxStringOutputStream out;
    handler->SetFlags(handler->GetFlags() | wxRICHTEXT_HANDLER_INCLUDE_STYLESHEET);
    this->GetBuffer().AddHandler(handler);
    this->GetBuffer().SaveFile(out, wxRICHTEXT_TYPE_XML);
    return out.GetString().ToStdWstring();
}


I'm surprised this isn't a straight-forward procedure. It seems to me that this operation should be part of the wxRichTextCtrl...
Topic archived. No new replies allowed.