1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richtextxml.cpp
3 // Purpose: XML and HTML I/O for wxRichTextCtrl
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
19 #if wxUSE_RICHTEXT && wxUSE_XML
21 #include "wx/richtext/richtextxml.h"
27 #include "wx/filename.h"
28 #include "wx/clipbrd.h"
29 #include "wx/wfstream.h"
30 #include "wx/sstream.h"
31 #include "wx/module.h"
32 #include "wx/txtstrm.h"
33 #include "wx/xml/xml.h"
35 IMPLEMENT_DYNAMIC_CLASS(wxRichTextXMLHandler
, wxRichTextFileHandler
)
38 bool wxRichTextXMLHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
45 wxXmlDocument
* xmlDoc
= new wxXmlDocument
;
48 // This is the encoding to convert to (memory encoding rather than file encoding)
49 wxString
encoding(wxT("UTF-8"));
51 #if !wxUSE_UNICODE && wxUSE_INTL
52 encoding
= wxLocale::GetSystemEncodingName();
55 if (!xmlDoc
->Load(stream
, encoding
))
61 if (xmlDoc
->GetRoot() && xmlDoc
->GetRoot()->GetType() == wxXML_ELEMENT_NODE
&& xmlDoc
->GetRoot()->GetName() == wxT("richtext"))
63 wxXmlNode
* child
= xmlDoc
->GetRoot()->GetChildren();
66 if (child
->GetType() == wxXML_ELEMENT_NODE
)
68 wxString name
= child
->GetName();
69 if (name
== wxT("richtext-version"))
73 ImportXML(buffer
, child
);
76 child
= child
->GetNext();
87 buffer
->UpdateRanges();
92 /// Recursively import an object
93 bool wxRichTextXMLHandler::ImportXML(wxRichTextBuffer
* buffer
, wxXmlNode
* node
)
95 wxString name
= node
->GetName();
97 bool doneChildren
= false;
99 if (name
== wxT("paragraphlayout"))
102 else if (name
== wxT("paragraph"))
104 wxRichTextParagraph
* para
= new wxRichTextParagraph(buffer
);
105 buffer
->AppendChild(para
);
107 GetStyle(para
->GetAttributes(), node
, true);
109 wxXmlNode
* child
= node
->GetChildren();
112 wxString childName
= child
->GetName();
113 if (childName
== wxT("text"))
116 wxXmlNode
* textChild
= child
->GetChildren();
119 if (textChild
->GetType() == wxXML_TEXT_NODE
||
120 textChild
->GetType() == wxXML_CDATA_SECTION_NODE
)
122 wxString text2
= textChild
->GetContent();
124 // Strip whitespace from end
125 if (!text2
.empty() && text2
[text2
.length()-1] == wxT('\n'))
126 text2
= text2
.Mid(0, text2
.length()-1);
128 if (!text2
.empty() && text2
[0] == wxT('"'))
129 text2
= text2
.Mid(1);
130 if (!text2
.empty() && text2
[text2
.length()-1] == wxT('"'))
131 text2
= text2
.Mid(0, text2
.length() - 1);
135 textChild
= textChild
->GetNext();
138 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, para
);
139 GetStyle(textObject
->GetAttributes(), child
, false);
141 para
->AppendChild(textObject
);
143 else if (childName
== wxT("image"))
145 int imageType
= wxBITMAP_TYPE_PNG
;
146 wxString value
= node
->GetPropVal(wxT("imagetype"), wxEmptyString
);
148 imageType
= wxAtoi(value
);
152 wxXmlNode
* imageChild
= child
->GetChildren();
155 wxString childName
= imageChild
->GetName();
156 if (childName
== wxT("data"))
158 wxXmlNode
* dataChild
= imageChild
->GetChildren();
161 data
= dataChild
->GetContent();
163 dataChild
= dataChild
->GetNext();
167 imageChild
= imageChild
->GetNext();
172 wxRichTextImage
* imageObj
= new wxRichTextImage(para
);
173 para
->AppendChild(imageObj
);
175 wxStringInputStream
strStream(data
);
177 imageObj
->GetImageBlock().ReadHex(strStream
, data
.length(), imageType
);
180 child
= child
->GetNext();
188 wxXmlNode
* child
= node
->GetChildren();
191 ImportXML(buffer
, child
);
192 child
= child
->GetNext();
200 //-----------------------------------------------------------------------------
201 // xml support routines
202 //-----------------------------------------------------------------------------
204 bool wxRichTextXMLHandler::HasParam(wxXmlNode
* node
, const wxString
& param
)
206 return (GetParamNode(node
, param
) != NULL
);
209 wxXmlNode
*wxRichTextXMLHandler::GetParamNode(wxXmlNode
* node
, const wxString
& param
)
211 wxCHECK_MSG(node
, NULL
, wxT("You can't access node data before it was initialized!"));
213 wxXmlNode
*n
= node
->GetChildren();
217 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
225 wxString
wxRichTextXMLHandler::GetNodeContent(wxXmlNode
*node
)
228 if (n
== NULL
) return wxEmptyString
;
229 n
= n
->GetChildren();
233 if (n
->GetType() == wxXML_TEXT_NODE
||
234 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
235 return n
->GetContent();
238 return wxEmptyString
;
242 wxString
wxRichTextXMLHandler::GetParamValue(wxXmlNode
*node
, const wxString
& param
)
245 return GetNodeContent(node
);
247 return GetNodeContent(GetParamNode(node
, param
));
250 wxString
wxRichTextXMLHandler::GetText(wxXmlNode
*node
, const wxString
& param
, bool WXUNUSED(translate
))
252 wxXmlNode
*parNode
= GetParamNode(node
, param
);
255 wxString
str1(GetNodeContent(parNode
));
259 // For use with earlier versions of wxWidgets
260 #ifndef WXUNUSED_IN_UNICODE
262 #define WXUNUSED_IN_UNICODE(x) WXUNUSED(x)
264 #define WXUNUSED_IN_UNICODE(x) x
268 // write string to output:
269 inline static void OutputString(wxOutputStream
& stream
, const wxString
& str
,
270 wxMBConv
*WXUNUSED_IN_UNICODE(convMem
) = NULL
, wxMBConv
*convFile
= NULL
)
272 if (str
.empty()) return;
276 const wxWX2MBbuf
buf(str
.mb_str(*convFile
));
277 stream
.Write((const char*)buf
, strlen((const char*)buf
));
281 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
282 stream
.Write((const char*)buf
, strlen((const char*)buf
));
285 if ( convFile
== NULL
)
286 stream
.Write(str
.mb_str(), str
.Len());
289 wxString
str2(str
.wc_str(*convMem
), *convFile
);
290 stream
.Write(str2
.mb_str(), str2
.Len());
295 // Same as above, but create entities first.
296 // Translates '<' to "<", '>' to ">" and '&' to "&"
297 static void OutputStringEnt(wxOutputStream
& stream
, const wxString
& str
,
298 wxMBConv
*convMem
= NULL
, wxMBConv
*convFile
= NULL
)
306 for (i
= 0; i
< len
; i
++)
310 // Original code excluded "&" but we _do_ want to convert
311 // the ampersand beginning & because otherwise when read in,
312 // the original "&" becomes "&".
314 if (c
== wxT('<') || c
== wxT('>') || c
== wxT('"') ||
315 (c
== wxT('&') /* && (str.Mid(i+1, 4) != wxT("amp;")) */ ))
317 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
321 OutputString(stream
, wxT("<"), NULL
, NULL
);
324 OutputString(stream
, wxT(">"), NULL
, NULL
);
327 OutputString(stream
, wxT("&"), NULL
, NULL
);
330 OutputString(stream
, wxT("""), NULL
, NULL
);
337 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
340 inline static void OutputIndentation(wxOutputStream
& stream
, int indent
)
342 wxString str
= wxT("\n");
343 for (int i
= 0; i
< indent
; i
++)
344 str
<< wxT(' ') << wxT(' ');
345 OutputString(stream
, str
, NULL
, NULL
);
348 // Convert a colour to a 6-digit hex string
349 static wxString
ColourToHexString(const wxColour
& col
)
353 hex
+= wxDecToHex(col
.Red());
354 hex
+= wxDecToHex(col
.Green());
355 hex
+= wxDecToHex(col
.Blue());
360 // Convert 6-digit hex string to a colour
361 static wxColour
HexStringToColour(const wxString
& hex
)
363 unsigned char r
= (unsigned char)wxHexToDec(hex
.Mid(0, 2));
364 unsigned char g
= (unsigned char)wxHexToDec(hex
.Mid(2, 2));
365 unsigned char b
= (unsigned char)wxHexToDec(hex
.Mid(4, 2));
367 return wxColour(r
, g
, b
);
370 bool wxRichTextXMLHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
375 wxString
version(wxT("1.0") ) ;
377 bool deleteConvFile
= false;
378 wxString fileEncoding
;
379 wxMBConv
* convFile
= NULL
;
382 fileEncoding
= wxT("UTF-8");
383 convFile
= & wxConvUTF8
;
385 fileEncoding
= wxT("ISO-8859-1");
386 convFile
= & wxConvISO8859_1
;
389 // If SetEncoding has been called, change the output encoding.
390 if (!m_encoding
.empty() && m_encoding
.Lower() != fileEncoding
.Lower())
392 if (m_encoding
== wxT("<System>"))
394 fileEncoding
= wxLocale::GetSystemEncodingName();
398 fileEncoding
= m_encoding
;
401 // GetSystemEncodingName may not have returned a name
402 if (fileEncoding
.empty())
404 fileEncoding
= wxT("UTF-8");
406 fileEncoding
= wxT("ISO-8859-1");
408 convFile
= new wxCSConv(fileEncoding
);
409 deleteConvFile
= true;
413 wxMBConv
* convMem
= wxConvCurrent
;
415 wxMBConv
* convMem
= NULL
;
419 s
.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"),
420 (const wxChar
*) version
, (const wxChar
*) fileEncoding
);
421 OutputString(stream
, s
, NULL
, NULL
);
422 OutputString(stream
, wxT("<richtext version=\"1.0.0.0\" xmlns=\"http://www.wxwidgets.org\">") , NULL
, NULL
);
425 bool success
= ExportXML(stream
, convMem
, convFile
, *buffer
, level
);
427 OutputString(stream
, wxT("\n</richtext>") , NULL
, NULL
);
428 OutputString(stream
, wxT("\n"), NULL
, NULL
);
436 /// Recursively export an object
437 bool wxRichTextXMLHandler::ExportXML(wxOutputStream
& stream
, wxMBConv
* convMem
, wxMBConv
* convFile
, wxRichTextObject
& obj
, int indent
)
440 if (obj
.IsKindOf(CLASSINFO(wxRichTextParagraphLayoutBox
)))
441 objectName
= wxT("paragraphlayout");
442 else if (obj
.IsKindOf(CLASSINFO(wxRichTextParagraph
)))
443 objectName
= wxT("paragraph");
444 else if (obj
.IsKindOf(CLASSINFO(wxRichTextPlainText
)))
445 objectName
= wxT("text");
446 else if (obj
.IsKindOf(CLASSINFO(wxRichTextImage
)))
447 objectName
= wxT("image");
449 objectName
= wxT("object");
451 if (obj
.IsKindOf(CLASSINFO(wxRichTextPlainText
)))
453 wxRichTextPlainText
& text
= (wxRichTextPlainText
&) obj
;
455 OutputIndentation(stream
, indent
);
456 OutputString(stream
, wxT("<") + objectName
, convMem
, convFile
);
458 wxString style
= CreateStyle(obj
.GetAttributes(), false);
460 OutputString(stream
, style
+ wxT(">"), convMem
, convFile
);
462 wxString str
= text
.GetText();
463 if (!str
.empty() && (str
[0] == wxT(' ') || str
[str
.length()-1] == wxT(' ')))
465 OutputString(stream
, wxT("\""), convMem
, convFile
);
466 OutputStringEnt(stream
, str
, convMem
, convFile
);
467 OutputString(stream
, wxT("\""), convMem
, convFile
);
470 OutputStringEnt(stream
, str
, convMem
, convFile
);
472 else if (obj
.IsKindOf(CLASSINFO(wxRichTextImage
)))
474 wxRichTextImage
& imageObj
= (wxRichTextImage
&) obj
;
476 if (imageObj
.GetImage().Ok() && !imageObj
.GetImageBlock().Ok())
477 imageObj
.MakeBlock();
479 OutputIndentation(stream
, indent
);
480 OutputString(stream
, wxT("<") + objectName
, convMem
, convFile
);
481 if (!imageObj
.GetImageBlock().Ok())
484 OutputString(stream
, wxT(">"), convMem
, convFile
);
488 OutputString(stream
, wxString::Format(wxT(" imagetype=\"%d\">"), (int) imageObj
.GetImageBlock().GetImageType()));
491 OutputIndentation(stream
, indent
+1);
492 OutputString(stream
, wxT("<data>"), convMem
, convFile
);
494 imageObj
.GetImageBlock().WriteHex(stream
);
496 OutputString(stream
, wxT("</data>"), convMem
, convFile
);
498 else if (obj
.IsKindOf(CLASSINFO(wxRichTextCompositeObject
)))
500 OutputIndentation(stream
, indent
);
501 OutputString(stream
, wxT("<") + objectName
, convMem
, convFile
);
504 if (objectName
== wxT("paragraph") || objectName
== wxT("paragraphlayout"))
507 wxString style
= CreateStyle(obj
.GetAttributes(), isPara
);
509 OutputString(stream
, style
+ wxT(">"), convMem
, convFile
);
511 wxRichTextCompositeObject
& composite
= (wxRichTextCompositeObject
&) obj
;
513 for (i
= 0; i
< composite
.GetChildCount(); i
++)
515 wxRichTextObject
* child
= composite
.GetChild(i
);
516 ExportXML(stream
, convMem
, convFile
, *child
, indent
+1);
520 if (objectName
!= wxT("text"))
521 OutputIndentation(stream
, indent
);
523 OutputString(stream
, wxT("</") + objectName
+ wxT(">"), convMem
, convFile
);
528 /// Create style parameters
529 wxString
wxRichTextXMLHandler::CreateStyle(const wxTextAttrEx
& attr
, bool isPara
)
532 if (attr
.GetTextColour().Ok())
534 str
<< wxT(" textcolor=\"#") << ColourToHexString(attr
.GetTextColour()) << wxT("\"");
536 if (attr
.GetBackgroundColour().Ok())
538 str
<< wxT(" bgcolor=\"#") << ColourToHexString(attr
.GetBackgroundColour()) << wxT("\"");
541 if (attr
.GetFont().Ok())
543 str
<< wxT(" fontsize=\"") << attr
.GetFont().GetPointSize() << wxT("\"");
544 str
<< wxT(" fontfamily=\"") << attr
.GetFont().GetFamily() << wxT("\"");
545 str
<< wxT(" fontstyle=\"") << attr
.GetFont().GetStyle() << wxT("\"");
546 str
<< wxT(" fontweight=\"") << attr
.GetFont().GetWeight() << wxT("\"");
547 str
<< wxT(" fontunderlined=\"") << (int) attr
.GetFont().GetUnderlined() << wxT("\"");
548 str
<< wxT(" fontface=\"") << attr
.GetFont().GetFaceName() << wxT("\"");
551 if (!attr
.GetCharacterStyleName().empty())
552 str
<< wxT(" charactertyle=\"") << wxString(attr
.GetCharacterStyleName()) << wxT("\"");
556 str
<< wxT(" alignment=\"") << (int) attr
.GetAlignment() << wxT("\"");
557 str
<< wxT(" leftindent=\"") << (int) attr
.GetLeftIndent() << wxT("\"");
558 str
<< wxT(" leftsubindent=\"") << (int) attr
.GetLeftSubIndent() << wxT("\"");
559 str
<< wxT(" rightindent=\"") << (int) attr
.GetRightIndent() << wxT("\"");
560 str
<< wxT(" parspacingafter=\"") << (int) attr
.GetParagraphSpacingAfter() << wxT("\"");
561 str
<< wxT(" parspacingbefore=\"") << (int) attr
.GetParagraphSpacingBefore() << wxT("\"");
562 str
<< wxT(" linespacing=\"") << (int) attr
.GetLineSpacing() << wxT("\"");
563 str
<< wxT(" bulletstyle=\"") << (int) attr
.GetBulletStyle() << wxT("\"");
564 str
<< wxT(" bulletnumber=\"") << (int) attr
.GetBulletNumber() << wxT("\"");
565 str
<< wxT(" bulletsymbol=\"") << wxString(attr
.GetBulletSymbol()) << wxT("\"");
567 if (!attr
.GetParagraphStyleName().empty())
568 str
<< wxT(" parstyle=\"") << wxString(attr
.GetParagraphStyleName()) << wxT("\"");
574 /// Get style parameters
575 bool wxRichTextXMLHandler::GetStyle(wxTextAttrEx
& attr
, wxXmlNode
* node
, bool isPara
)
577 wxString fontFacename
;
579 int fontFamily
= wxDEFAULT
;
580 int fontWeight
= wxNORMAL
;
581 int fontStyle
= wxNORMAL
;
582 bool fontUnderlined
= false;
584 fontFacename
= node
->GetPropVal(wxT("fontface"), wxEmptyString
);
586 wxString value
= node
->GetPropVal(wxT("fontfamily"), wxEmptyString
);
588 fontFamily
= wxAtoi(value
);
590 value
= node
->GetPropVal(wxT("fontstyle"), wxEmptyString
);
592 fontStyle
= wxAtoi(value
);
594 value
= node
->GetPropVal(wxT("fontsize"), wxEmptyString
);
596 fontSize
= wxAtoi(value
);
598 value
= node
->GetPropVal(wxT("fontweight"), wxEmptyString
);
600 fontWeight
= wxAtoi(value
);
602 value
= node
->GetPropVal(wxT("fontunderlined"), wxEmptyString
);
604 fontUnderlined
= wxAtoi(value
) != 0;
606 attr
.SetFont(* wxTheFontList
->FindOrCreateFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, fontUnderlined
, fontFacename
));
608 value
= node
->GetPropVal(wxT("textcolor"), wxEmptyString
);
611 if (value
[0] == wxT('#'))
612 attr
.SetTextColour(HexStringToColour(value
.Mid(1)));
614 attr
.SetTextColour(value
);
617 value
= node
->GetPropVal(wxT("backgroundcolor"), wxEmptyString
);
620 if (value
[0] == wxT('#'))
621 attr
.SetBackgroundColour(HexStringToColour(value
.Mid(1)));
623 attr
.SetBackgroundColour(value
);
626 value
= node
->GetPropVal(wxT("characterstyle"), wxEmptyString
);
628 attr
.SetCharacterStyleName(value
);
630 // Set paragraph attributes
633 value
= node
->GetPropVal(wxT("alignment"), wxEmptyString
);
635 attr
.SetAlignment((wxTextAttrAlignment
) wxAtoi(value
));
637 int leftSubIndent
= 0;
639 value
= node
->GetPropVal(wxT("leftindent"), wxEmptyString
);
641 leftIndent
= wxAtoi(value
);
642 value
= node
->GetPropVal(wxT("leftsubindent"), wxEmptyString
);
644 leftSubIndent
= wxAtoi(value
);
645 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
647 value
= node
->GetPropVal(wxT("rightindent"), wxEmptyString
);
649 attr
.SetRightIndent(wxAtoi(value
));
651 value
= node
->GetPropVal(wxT("parspacingbefore"), wxEmptyString
);
653 attr
.SetParagraphSpacingBefore(wxAtoi(value
));
655 value
= node
->GetPropVal(wxT("parspacingafter"), wxEmptyString
);
657 attr
.SetParagraphSpacingAfter(wxAtoi(value
));
659 value
= node
->GetPropVal(wxT("linespacing"), wxEmptyString
);
661 attr
.SetLineSpacing(wxAtoi(value
));
663 value
= node
->GetPropVal(wxT("bulletstyle"), wxEmptyString
);
665 attr
.SetBulletStyle(wxAtoi(value
));
667 value
= node
->GetPropVal(wxT("bulletnumber"), wxEmptyString
);
669 attr
.SetBulletNumber(wxAtoi(value
));
671 value
= node
->GetPropVal(wxT("bulletsymbol"), wxEmptyString
);
673 attr
.SetBulletSymbol(value
[0]);
675 value
= node
->GetPropVal(wxT("parstyle"), wxEmptyString
);
677 attr
.SetParagraphStyleName(value
);
687 // wxUSE_RICHTEXT && wxUSE_XML