[Solved] Serialize wxTextAttrEx Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
heinermueller
Experienced Solver
Experienced Solver
Posts: 69
Joined: Sat Oct 26, 2013 11:54 am

[Solved] Serialize wxTextAttrEx

Post by heinermueller » Wed Jan 22, 2020 9:35 pm

Hi everybody,

i would like to serialize wxTextAttrEx object which i get from a wxRichTextCtrl. Purpose would be to offer the last used style again when the user opens the application again. Is there a built-in method to serialize this class? I could not find any. I know that wxFont has

Code: Select all

wxString wxFont::GetNativeFontInfoDesc	() const
for this purpose. And, if nothing helps, maybe someone with some code insight could give me a hint if i could try serializing and restoring the raw binary (in other words, is it a POD or is there some caching in the background)

With the best regards
Heiner
Last edited by heinermueller on Sun Jan 26, 2020 10:31 pm, edited 1 time in total.

heinermueller
Experienced Solver
Experienced Solver
Posts: 69
Joined: Sat Oct 26, 2013 11:54 am

Re: Serialize wxTextAttrEx

Post by heinermueller » Sun Jan 26, 2020 10:31 pm

I solved the issue with

Code: Select all

wxRichTextXMLHandler x;
wxString st = x.CreateStyle(rtc->GetDefaultStyle());
somehow_store_string(st);
and later reading it back with

Code: Select all

wxString ss = somehow_read_string();
if (ss.length() > 0)
{
	ss.Replace("\"", "");
	wxXmlNode xn;
	wxStringTokenizer t(ss);
	while (t.HasMoreTokens())
	{
		auto attr = t.GetNextToken();
		auto a_name = attr.BeforeFirst('=');
		auto a_val = attr.AfterFirst('=');
		xn.AddProperty(a_name, a_val);
	}

	wxTextAttrEx ex;
	wxRichTextXMLHandler x;
	if (x.GetStyle(ex, &xn))
	{
		richedit_dialogptr->GetRichTextControl()->SetDefaultStyle(ex);
	}
}
I am sure the xml part could be smarter but it works. The wxRichTextXMLHandler::CreateStyle is not documented, by the way.

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 4424
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: [Solved] Serialize wxTextAttrEx

Post by ONEEYEMAN » Mon Jan 27, 2020 4:43 pm

Hi,
About the un-documented function - please post to wx-users ML, where you can reach core wx developer.
This is the forum from users for users of the library.

Thank you.

Post Reply