+// Import this object from XML
+bool wxRichTextObject::ImportFromXML(wxRichTextBuffer* WXUNUSED(buffer), wxXmlNode* node, wxRichTextXMLHandler* handler)
+{
+ handler->ImportProperties(this, node);
+ handler->ImportStyle(GetAttributes(), node, UsesParagraphAttributes());
+
+ return true;
+}
+
+#if wxRICHTEXT_HAVE_DIRECT_OUTPUT
+// Export this object directly to the given stream.
+bool wxRichTextObject::ExportXML(wxOutputStream& stream, int indent, wxRichTextXMLHandler* handler)
+{
+ ::OutputIndentation(stream, indent);
+ ::OutputString(stream, wxT("<") + GetXMLNodeName(), handler->GetConvMem(), handler->GetConvFile());
+
+ wxString style = handler->AddAttributes(GetAttributes(), true);
+
+ ::OutputString(stream, style + wxT(">"), handler->GetConvMem(), handler->GetConvFile());
+
+ if (GetProperties().GetCount() > 0)
+ {
+ handler->WriteProperties(stream, GetProperties(), indent);
+ }
+
+ wxRichTextCompositeObject* composite = wxDynamicCast(this, wxRichTextCompositeObject);
+ if (composite)
+ {
+ size_t i;
+ for (i = 0; i < composite->GetChildCount(); i++)
+ {
+ wxRichTextObject* child = composite->GetChild(i);
+ child->ExportXML(stream, indent+1, handler);
+ }
+ }
+
+ ::OutputIndentation(stream, indent);
+ ::OutputString(stream, wxT("</") + GetXMLNodeName() + wxT(">"), handler->GetConvMem(), handler->GetConvFile());
+ return true;
+}
+#endif
+
+#if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
+// Export this object to the given parent node, usually creating at least one child node.
+bool wxRichTextObject::ExportXML(wxXmlNode* parent, wxRichTextXMLHandler* handler)
+{
+ wxXmlNode* elementNode = new wxXmlNode(wxXML_ELEMENT_NODE, GetXMLNodeName());
+ parent->AddChild(elementNode);
+ handler->AddAttributes(elementNode, GetAttributes(), true);
+ handler->WriteProperties(elementNode, GetProperties());
+
+ wxRichTextCompositeObject* composite = wxDynamicCast(this, wxRichTextCompositeObject);
+ if (composite)
+ {
+ size_t i;
+ for (i = 0; i < composite->GetChildCount(); i++)
+ {
+ wxRichTextObject* child = composite->GetChild(i);
+ child->ExportXML(elementNode, handler);
+ }
+ }
+ return true;
+}
+#endif
+
+
+// Import this object from XML
+bool wxRichTextPlainText::ImportFromXML(wxRichTextBuffer* buffer, wxXmlNode* node, wxRichTextXMLHandler* handler)
+{
+ wxRichTextObject::ImportFromXML(buffer, node, handler);
+
+ if (node->GetName() == wxT("text"))
+ {
+ wxString text;
+ wxXmlNode* textChild = node->GetChildren();
+ while (textChild)
+ {
+ if (textChild->GetType() == wxXML_TEXT_NODE ||
+ textChild->GetType() == wxXML_CDATA_SECTION_NODE)
+ {
+ wxString text2 = textChild->GetContent();
+
+ // Strip whitespace from end
+ if (!text2.empty() && text2[text2.length()-1] == wxT('\n'))
+ text2 = text2.Mid(0, text2.length()-1);
+
+ if (!text2.empty() && text2[0] == wxT('"'))
+ text2 = text2.Mid(1);
+ if (!text2.empty() && text2[text2.length()-1] == wxT('"'))
+ text2 = text2.Mid(0, text2.length() - 1);
+
+ text += text2;
+ }
+ textChild = textChild->GetNext();
+ }
+
+ SetText(text);
+ }
+ else if (node->GetName() == wxT("symbol"))
+ {
+ // This is a symbol that XML can't read in the normal way
+ wxString text;
+ wxXmlNode* textChild = node->GetChildren();
+ while (textChild)
+ {
+ if (textChild->GetType() == wxXML_TEXT_NODE ||
+ textChild->GetType() == wxXML_CDATA_SECTION_NODE)
+ {
+ wxString text2 = textChild->GetContent();
+ text += text2;
+ }
+ textChild = textChild->GetNext();
+ }
+
+ wxString actualText;
+ actualText << (wxChar) wxAtoi(text);
+ SetText(actualText);
+ }
+ else
+ return false;
+
+ return true;
+}
+
+#if wxRICHTEXT_HAVE_DIRECT_OUTPUT
+// Export this object directly to the given stream.
+bool wxRichTextPlainText::ExportXML(wxOutputStream& stream, int indent, wxRichTextXMLHandler* handler)
+{
+ wxString style = handler->AddAttributes(GetAttributes(), false);
+
+ int i;
+ int last = 0;
+ const wxString& text = GetText();
+ int len = (int) text.Length();
+
+ if (len == 0)
+ {
+ i = 0;
+ ::OutputIndentation(stream, indent);
+ ::OutputString(stream, wxT("<text"), handler->GetConvMem(), handler->GetConvFile());
+ ::OutputString(stream, style + wxT(">"), handler->GetConvMem(), handler->GetConvFile());
+ if (GetProperties().GetCount() > 0)
+ {
+ handler->WriteProperties(stream, GetProperties(), indent);
+ ::OutputIndentation(stream, indent);
+ }
+ ::OutputString(stream, wxT("</text>"), handler->GetConvMem(), handler->GetConvFile());
+ }
+ else for (i = 0; i < len; i++)
+ {
+#if wxUSE_UNICODE
+ int c = (int) text[i];
+#else
+ int c = (int) wxUChar(text[i]);
+#endif
+ if ((c < 32 || c == 34) && /* c != 9 && */ c != 10 && c != 13)
+ {
+ if (i > 0)
+ {
+ wxString fragment(text.Mid(last, i-last));
+ if (!fragment.empty())
+ {
+ ::OutputIndentation(stream, indent);
+ ::OutputString(stream, wxT("<text"), handler->GetConvMem(), handler->GetConvFile());
+
+ ::OutputString(stream, style + wxT(">"), handler->GetConvMem(), handler->GetConvFile());
+
+ if (!fragment.empty() && (fragment[0] == wxT(' ') || fragment[fragment.length()-1] == wxT(' ')))
+ {
+ ::OutputString(stream, wxT("\""), handler->GetConvMem(), handler->GetConvFile());
+ ::OutputStringEnt(stream, fragment, handler->GetConvMem(), handler->GetConvFile());
+ ::OutputString(stream, wxT("\""), handler->GetConvMem(), handler->GetConvFile());
+ }
+ else
+ ::OutputStringEnt(stream, fragment, handler->GetConvMem(), handler->GetConvFile());
+
+ if (GetProperties().GetCount() > 0)
+ {
+ handler->WriteProperties(stream, GetProperties(), indent);
+ ::OutputIndentation(stream, indent);
+ }
+ ::OutputString(stream, wxT("</text>"), handler->GetConvMem(), handler->GetConvFile());
+ }
+ }
+
+
+ // Output this character as a number in a separate tag, because XML can't cope
+ // with entities below 32 except for 10 and 13
+ last = i + 1;
+ ::OutputIndentation(stream, indent);
+ ::OutputString(stream, wxT("<symbol"), handler->GetConvMem(), handler->GetConvFile());
+
+ ::OutputString(stream, style + wxT(">"), handler->GetConvMem(), handler->GetConvFile());
+ ::OutputString(stream, wxString::Format(wxT("%d"), c), handler->GetConvMem(), handler->GetConvFile());
+
+ if (GetProperties().GetCount() > 0)
+ {
+ handler->WriteProperties(stream, GetProperties(), indent);
+ ::OutputIndentation(stream, indent);
+ }
+ ::OutputString(stream, wxT("</symbol>"), handler->GetConvMem(), handler->GetConvFile());
+ }
+ }
+
+ wxString fragment;
+ if (last == 0)
+ fragment = text;
+ else
+ fragment = text.Mid(last, i-last);
+
+ if (last < len)
+ {
+ ::OutputIndentation(stream, indent);
+ ::OutputString(stream, wxT("<text"), handler->GetConvMem(), handler->GetConvFile());
+
+ ::OutputString(stream, style + wxT(">"), handler->GetConvMem(), handler->GetConvFile());
+
+ if (GetProperties().GetCount() > 0)
+ {
+ handler->WriteProperties(stream, GetProperties(), indent);
+ ::OutputIndentation(stream, indent);
+ }
+
+ if (!fragment.empty() && (fragment[0] == wxT(' ') || fragment[fragment.length()-1] == wxT(' ')))
+ {
+ ::OutputString(stream, wxT("\""), handler->GetConvMem(), handler->GetConvFile());
+ ::OutputStringEnt(stream, fragment, handler->GetConvMem(), handler->GetConvFile());
+ ::OutputString(stream, wxT("\""), handler->GetConvMem(), handler->GetConvFile());
+ }
+ else
+ ::OutputStringEnt(stream, fragment, handler->GetConvMem(), handler->GetConvFile());
+
+ ::OutputString(stream, wxT("</text>"), handler->GetConvMem(), handler->GetConvFile());
+ }
+ return true;
+}
+#endif
+
+#if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
+// Export this object to the given parent node, usually creating at least one child node.
+bool wxRichTextPlainText::ExportXML(wxXmlNode* parent, wxRichTextXMLHandler* handler)
+{
+ int i;
+ int last = 0;
+ const wxString& text = GetText();
+ int len = (int) text.Length();
+
+ if (len == 0)
+ {
+ i = 0;
+
+ wxXmlNode* elementNode = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("text"));
+ parent->AddChild(elementNode);
+
+ handler->AddAttributes(elementNode, GetAttributes(), false);
+ handler->WriteProperties(elementNode, GetProperties());
+ }
+ else for (i = 0; i < len; i++)
+ {
+#if wxUSE_UNICODE
+ int c = (int) text[i];
+#else
+ int c = (int) wxUChar(text[i]);
+#endif
+ if ((c < 32 || c == 34) && c != 10 && c != 13)
+ {
+ if (i > 0)
+ {
+ wxString fragment(text.Mid(last, i-last));
+ if (!fragment.empty())
+ {
+ // TODO: I'm assuming wxXmlDocument will output quotes if necessary
+ wxXmlNode* elementNode = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("text"));
+ parent->AddChild(elementNode);
+ handler->AddAttributes(elementNode, GetAttributes(), false);
+ handler->WriteProperties(elementNode, GetProperties());
+
+ wxXmlNode* textNode = new wxXmlNode(wxXML_TEXT_NODE, wxT("text"));
+ elementNode->AddChild(textNode);
+
+ if (fragment[0] == wxT(' ') || fragment[fragment.length()-1] == wxT(' '))
+ fragment = wxT("\"") + fragment + wxT("\"");
+
+ textNode->SetContent(fragment);
+ }
+ }
+
+
+ // Output this character as a number in a separate tag, because XML can't cope
+ // with entities below 32 except for 10 and 13
+
+ wxXmlNode* elementNode = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("symbol"));
+ parent->AddChild(elementNode);
+
+ handler->AddAttributes(elementNode, GetAttributes(), false);
+ handler->WriteProperties(elementNode, GetProperties());
+
+ wxXmlNode* textNode = new wxXmlNode(wxXML_TEXT_NODE, wxT("text"));
+ elementNode->AddChild(textNode);
+ textNode->SetContent(wxString::Format(wxT("%d"), c));
+
+ last = i + 1;
+ }
+ }
+
+ wxString fragment;
+ if (last == 0)
+ fragment = text;
+ else
+ fragment = text.Mid(last, i-last);
+
+ if (last < len)
+ {
+ wxXmlNode* elementNode = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("text"));
+ parent->AddChild(elementNode);
+ handler->AddAttributes(elementNode, GetAttributes(), false);
+
+ wxXmlNode* textNode = new wxXmlNode(wxXML_TEXT_NODE, wxT("text"));
+ elementNode->AddChild(textNode);
+
+ if (fragment[0] == wxT(' ') || fragment[fragment.length()-1] == wxT(' '))
+ fragment = wxT("\"") + fragment + wxT("\"");
+
+ textNode->SetContent(fragment);
+ }
+ return true;
+}
+#endif
+
+
+// Import this object from XML
+bool wxRichTextImage::ImportFromXML(wxRichTextBuffer* buffer, wxXmlNode* node, wxRichTextXMLHandler* handler)
+{
+ wxRichTextObject::ImportFromXML(buffer, node, handler);
+
+ wxBitmapType imageType = wxBITMAP_TYPE_PNG;
+ wxString value = node->GetAttribute(wxT("imagetype"), wxEmptyString);
+ if (!value.empty())
+ {
+ int type = wxAtoi(value);
+
+ // note: 0 == wxBITMAP_TYPE_INVALID
+ if (type <= 0 || type >= wxBITMAP_TYPE_MAX)
+ {
+ wxLogWarning("Invalid bitmap type specified for <image> tag: %d", type);
+ }
+ else
+ {
+ imageType = (wxBitmapType)type;
+ }
+ }
+
+ wxString data;
+
+ wxXmlNode* imageChild = node->GetChildren();
+ while (imageChild)
+ {
+ wxString childName = imageChild->GetName();
+ if (childName == wxT("data"))
+ {
+ wxXmlNode* dataChild = imageChild->GetChildren();
+ while (dataChild)
+ {
+ data = dataChild->GetContent();
+ // wxLogDebug(data);
+ dataChild = dataChild->GetNext();
+ }
+
+ }
+ imageChild = imageChild->GetNext();
+ }
+
+ if (!data.empty())
+ {
+ wxStringInputStream strStream(data);
+
+ GetImageBlock().ReadHex(strStream, data.length(), imageType);
+
+ return true;
+ }
+ else
+ return false;
+}
+
+#if wxRICHTEXT_HAVE_DIRECT_OUTPUT
+// Export this object directly to the given stream.
+bool wxRichTextImage::ExportXML(wxOutputStream& stream, int indent, wxRichTextXMLHandler* handler)
+{
+ wxString style = handler->AddAttributes(GetAttributes(), false);
+
+ ::OutputIndentation(stream, indent);
+ ::OutputString(stream, wxT("<image"), handler->GetConvMem(), handler->GetConvFile());
+ if (!GetImageBlock().Ok())
+ {
+ // No data
+ ::OutputString(stream, style + wxT(">"), handler->GetConvMem(), handler->GetConvFile());
+ }
+ else
+ {
+ ::OutputString(stream, wxString::Format(wxT(" imagetype=\"%d\""), (int) GetImageBlock().GetImageType()) + style + wxT(">"), handler->GetConvMem(), handler->GetConvFile());
+ }
+ if (GetProperties().GetCount() > 0)
+ {
+ handler->WriteProperties(stream, GetProperties(), indent);
+ ::OutputIndentation(stream, indent);
+ }
+
+ ::OutputIndentation(stream, indent+1);
+ ::OutputString(stream, wxT("<data>"), handler->GetConvMem(), handler->GetConvFile());
+
+ // wxStopWatch stopwatch;
+
+ GetImageBlock().WriteHex(stream);
+
+ // wxLogDebug(wxT("Image conversion to hex took %ldms"), stopwatch.Time());
+
+ ::OutputString(stream, wxT("</data>\n"), handler->GetConvMem(), handler->GetConvFile());
+ ::OutputIndentation(stream, indent);
+ ::OutputString(stream, wxT("</image>"), handler->GetConvMem(), handler->GetConvFile());
+ return true;
+}
+#endif
+
+#if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
+// Export this object to the given parent node, usually creating at least one child node.
+bool wxRichTextImage::ExportXML(wxXmlNode* parent, wxRichTextXMLHandler* handler)
+{
+ wxXmlNode* elementNode = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("image"));
+ parent->AddChild(elementNode);
+
+ if (GetImageBlock().Ok())
+ elementNode->AddAttribute(wxT("imagetype"), MakeString((int) GetImageBlock().GetImageType()));
+
+ handler->AddAttributes(elementNode, GetAttributes(), false);
+ handler->WriteProperties(elementNode, GetProperties());
+
+ wxXmlNode* dataNode = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("data"));
+ elementNode->AddChild(dataNode);
+ wxXmlNode* textNode = new wxXmlNode(wxXML_TEXT_NODE, wxT("text"));
+ dataNode->AddChild(textNode);
+
+ wxString strData;
+#if 1
+ {
+ wxMemoryOutputStream stream;
+ if (GetImageBlock().WriteHex(stream))
+ {
+ if (stream.GetSize() > 0)
+ {
+ int size = stream.GetSize();
+#ifdef __WXDEBUG__
+ int size2 = stream.GetOutputStreamBuffer()->GetIntPosition();
+ wxASSERT(size == size2);
+#endif
+ unsigned char* data = new unsigned char[size];
+ stream.CopyTo(data, size);
+ strData = wxString((const char*) data, wxConvUTF8, size);
+ delete[] data;
+ }
+ else
+ strData = wxEmptyString;
+ }
+
+ }
+#else
+ {
+ wxStringOutputStream strStream(& strData);
+ GetImageBlock().WriteHex(strStream);
+ }
+#endif
+
+ textNode->SetContent(strData);
+ textNode->SetNoConversion(true); // optimize speed
+
+ return true;
+}
+#endif
+
+
+// Import this object from XML
+bool wxRichTextParagraphLayoutBox::ImportFromXML(wxRichTextBuffer* buffer, wxXmlNode* node, wxRichTextXMLHandler* handler)
+{
+ wxRichTextObject::ImportFromXML(buffer, node, handler);
+
+ wxString partial = node->GetAttribute(wxT("partialparagraph"), wxEmptyString);
+ if (partial == wxT("true"))
+ SetPartialParagraph(true);
+
+ return true;
+}
+
+#if wxRICHTEXT_HAVE_DIRECT_OUTPUT
+// Export this object directly to the given stream.
+bool wxRichTextParagraphLayoutBox::ExportXML(wxOutputStream& stream, int indent, wxRichTextXMLHandler* handler)
+{
+ ::OutputIndentation(stream, indent);
+ ::OutputString(stream, wxT("<paragraphlayout"), handler->GetConvMem(), handler->GetConvFile());
+
+ wxString style = handler->AddAttributes(GetAttributes(), true);
+
+ if (GetPartialParagraph())
+ style << wxT(" partialparagraph=\"true\"");
+
+ ::OutputString(stream, style + wxT(">"), handler->GetConvMem(), handler->GetConvFile());
+
+ if (GetProperties().GetCount() > 0)
+ {
+ handler->WriteProperties(stream, GetProperties(), indent);
+ }
+
+ size_t i;
+ for (i = 0; i < GetChildCount(); i++)
+ {
+ wxRichTextObject* child = GetChild(i);
+ child->ExportXML(stream, indent+1, handler);
+ }
+
+ ::OutputIndentation(stream, indent);
+ ::OutputString(stream, wxT("</paragraphlayout>"), handler->GetConvMem(), handler->GetConvFile());
+ return true;
+}
+#endif
+
+#if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
+// Export this object to the given parent node, usually creating at least one child node.
+bool wxRichTextParagraphLayoutBox::ExportXML(wxXmlNode* parent, wxRichTextXMLHandler* handler)
+{
+ wxXmlNode* elementNode = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("paragraphlayout"));
+ parent->AddChild(elementNode);
+ handler->AddAttributes(elementNode, GetAttributes(), true);
+ handler->WriteProperties(elementNode, GetProperties());
+
+ if (GetPartialParagraph())
+ elementNode->AddAttribute(wxT("partialparagraph"), wxT("true"));
+
+ size_t i;
+ for (i = 0; i < GetChildCount(); i++)
+ {
+ wxRichTextObject* child = GetChild(i);
+ child->ExportXML(elementNode, handler);
+ }
+
+ return true;
+}
+#endif
+