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"
25 #include "wx/module.h"
28 #include "wx/filename.h"
29 #include "wx/clipbrd.h"
30 #include "wx/wfstream.h"
31 #include "wx/sstream.h"
32 #include "wx/txtstrm.h"
33 #include "wx/tokenzr.h"
34 #include "wx/xml/xml.h"
36 IMPLEMENT_DYNAMIC_CLASS(wxRichTextXMLHandler
, wxRichTextFileHandler
)
39 bool wxRichTextXMLHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
46 wxXmlDocument
* xmlDoc
= new wxXmlDocument
;
49 // This is the encoding to convert to (memory encoding rather than file encoding)
50 wxString
encoding(wxT("UTF-8"));
52 #if !wxUSE_UNICODE && wxUSE_INTL
53 encoding
= wxLocale::GetSystemEncodingName();
56 if (!xmlDoc
->Load(stream
, encoding
))
62 if (xmlDoc
->GetRoot() && xmlDoc
->GetRoot()->GetType() == wxXML_ELEMENT_NODE
&& xmlDoc
->GetRoot()->GetName() == wxT("richtext"))
64 wxXmlNode
* child
= xmlDoc
->GetRoot()->GetChildren();
67 if (child
->GetType() == wxXML_ELEMENT_NODE
)
69 wxString name
= child
->GetName();
70 if (name
== wxT("richtext-version"))
74 ImportXML(buffer
, child
);
77 child
= child
->GetNext();
88 buffer
->UpdateRanges();
93 /// Recursively import an object
94 bool wxRichTextXMLHandler::ImportXML(wxRichTextBuffer
* buffer
, wxXmlNode
* node
)
96 wxString name
= node
->GetName();
98 bool doneChildren
= false;
100 if (name
== wxT("paragraphlayout"))
102 wxString partial
= node
->GetPropVal(wxT("partialparagraph"), wxEmptyString
);
103 if (partial
== wxT("true"))
104 buffer
->SetPartialParagraph(true);
106 else if (name
== wxT("paragraph"))
108 wxRichTextParagraph
* para
= new wxRichTextParagraph(buffer
);
109 buffer
->AppendChild(para
);
111 GetStyle(para
->GetAttributes(), node
, true);
113 wxXmlNode
* child
= node
->GetChildren();
116 wxString childName
= child
->GetName();
117 if (childName
== wxT("text"))
120 wxXmlNode
* textChild
= child
->GetChildren();
123 if (textChild
->GetType() == wxXML_TEXT_NODE
||
124 textChild
->GetType() == wxXML_CDATA_SECTION_NODE
)
126 wxString text2
= textChild
->GetContent();
128 // Strip whitespace from end
129 if (!text2
.empty() && text2
[text2
.length()-1] == wxT('\n'))
130 text2
= text2
.Mid(0, text2
.length()-1);
132 if (!text2
.empty() && text2
[0] == wxT('"'))
133 text2
= text2
.Mid(1);
134 if (!text2
.empty() && text2
[text2
.length()-1] == wxT('"'))
135 text2
= text2
.Mid(0, text2
.length() - 1);
139 textChild
= textChild
->GetNext();
142 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, para
);
143 GetStyle(textObject
->GetAttributes(), child
, false);
145 para
->AppendChild(textObject
);
147 else if (childName
== wxT("image"))
149 int imageType
= wxBITMAP_TYPE_PNG
;
150 wxString value
= node
->GetPropVal(wxT("imagetype"), wxEmptyString
);
152 imageType
= wxAtoi(value
);
156 wxXmlNode
* imageChild
= child
->GetChildren();
159 wxString childName
= imageChild
->GetName();
160 if (childName
== wxT("data"))
162 wxXmlNode
* dataChild
= imageChild
->GetChildren();
165 data
= dataChild
->GetContent();
167 dataChild
= dataChild
->GetNext();
171 imageChild
= imageChild
->GetNext();
176 wxRichTextImage
* imageObj
= new wxRichTextImage(para
);
177 para
->AppendChild(imageObj
);
179 wxStringInputStream
strStream(data
);
181 imageObj
->GetImageBlock().ReadHex(strStream
, data
.length(), imageType
);
184 child
= child
->GetNext();
192 wxXmlNode
* child
= node
->GetChildren();
195 ImportXML(buffer
, child
);
196 child
= child
->GetNext();
204 //-----------------------------------------------------------------------------
205 // xml support routines
206 //-----------------------------------------------------------------------------
208 bool wxRichTextXMLHandler::HasParam(wxXmlNode
* node
, const wxString
& param
)
210 return (GetParamNode(node
, param
) != NULL
);
213 wxXmlNode
*wxRichTextXMLHandler::GetParamNode(wxXmlNode
* node
, const wxString
& param
)
215 wxCHECK_MSG(node
, NULL
, wxT("You can't access node data before it was initialized!"));
217 wxXmlNode
*n
= node
->GetChildren();
221 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
229 wxString
wxRichTextXMLHandler::GetNodeContent(wxXmlNode
*node
)
232 if (n
== NULL
) return wxEmptyString
;
233 n
= n
->GetChildren();
237 if (n
->GetType() == wxXML_TEXT_NODE
||
238 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
239 return n
->GetContent();
242 return wxEmptyString
;
246 wxString
wxRichTextXMLHandler::GetParamValue(wxXmlNode
*node
, const wxString
& param
)
249 return GetNodeContent(node
);
251 return GetNodeContent(GetParamNode(node
, param
));
254 wxString
wxRichTextXMLHandler::GetText(wxXmlNode
*node
, const wxString
& param
, bool WXUNUSED(translate
))
256 wxXmlNode
*parNode
= GetParamNode(node
, param
);
259 wxString
str1(GetNodeContent(parNode
));
263 // For use with earlier versions of wxWidgets
264 #ifndef WXUNUSED_IN_UNICODE
266 #define WXUNUSED_IN_UNICODE(x) WXUNUSED(x)
268 #define WXUNUSED_IN_UNICODE(x) x
272 // write string to output:
273 inline static void OutputString(wxOutputStream
& stream
, const wxString
& str
,
274 wxMBConv
*WXUNUSED_IN_UNICODE(convMem
) = NULL
, wxMBConv
*convFile
= NULL
)
276 if (str
.empty()) return;
280 const wxWX2MBbuf
buf(str
.mb_str(*convFile
));
281 stream
.Write((const char*)buf
, strlen((const char*)buf
));
285 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
286 stream
.Write((const char*)buf
, strlen((const char*)buf
));
289 if ( convFile
== NULL
)
290 stream
.Write(str
.mb_str(), str
.Len());
293 wxString
str2(str
.wc_str(*convMem
), *convFile
);
294 stream
.Write(str2
.mb_str(), str2
.Len());
299 // Same as above, but create entities first.
300 // Translates '<' to "<", '>' to ">" and '&' to "&"
301 static void OutputStringEnt(wxOutputStream
& stream
, const wxString
& str
,
302 wxMBConv
*convMem
= NULL
, wxMBConv
*convFile
= NULL
)
310 for (i
= 0; i
< len
; i
++)
314 // Original code excluded "&" but we _do_ want to convert
315 // the ampersand beginning & because otherwise when read in,
316 // the original "&" becomes "&".
318 if (c
== wxT('<') || c
== wxT('>') || c
== wxT('"') ||
319 (c
== wxT('&') /* && (str.Mid(i+1, 4) != wxT("amp;")) */ ))
321 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
325 OutputString(stream
, wxT("<"), NULL
, NULL
);
328 OutputString(stream
, wxT(">"), NULL
, NULL
);
331 OutputString(stream
, wxT("&"), NULL
, NULL
);
334 OutputString(stream
, wxT("""), NULL
, NULL
);
341 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
344 inline static void OutputIndentation(wxOutputStream
& stream
, int indent
)
346 wxString str
= wxT("\n");
347 for (int i
= 0; i
< indent
; i
++)
348 str
<< wxT(' ') << wxT(' ');
349 OutputString(stream
, str
, NULL
, NULL
);
352 // Convert a colour to a 6-digit hex string
353 static wxString
ColourToHexString(const wxColour
& col
)
357 hex
+= wxDecToHex(col
.Red());
358 hex
+= wxDecToHex(col
.Green());
359 hex
+= wxDecToHex(col
.Blue());
364 // Convert 6-digit hex string to a colour
365 static wxColour
HexStringToColour(const wxString
& hex
)
367 unsigned char r
= (unsigned char)wxHexToDec(hex
.Mid(0, 2));
368 unsigned char g
= (unsigned char)wxHexToDec(hex
.Mid(2, 2));
369 unsigned char b
= (unsigned char)wxHexToDec(hex
.Mid(4, 2));
371 return wxColour(r
, g
, b
);
374 bool wxRichTextXMLHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
379 wxString
version(wxT("1.0") ) ;
381 bool deleteConvFile
= false;
382 wxString fileEncoding
;
383 wxMBConv
* convFile
= NULL
;
386 fileEncoding
= wxT("UTF-8");
387 convFile
= & wxConvUTF8
;
389 fileEncoding
= wxT("ISO-8859-1");
390 convFile
= & wxConvISO8859_1
;
393 // If SetEncoding has been called, change the output encoding.
394 if (!m_encoding
.empty() && m_encoding
.Lower() != fileEncoding
.Lower())
396 if (m_encoding
== wxT("<System>"))
398 fileEncoding
= wxLocale::GetSystemEncodingName();
402 fileEncoding
= m_encoding
;
405 // GetSystemEncodingName may not have returned a name
406 if (fileEncoding
.empty())
408 fileEncoding
= wxT("UTF-8");
410 fileEncoding
= wxT("ISO-8859-1");
412 convFile
= new wxCSConv(fileEncoding
);
413 deleteConvFile
= true;
417 wxMBConv
* convMem
= wxConvCurrent
;
419 wxMBConv
* convMem
= NULL
;
423 s
.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"),
424 (const wxChar
*) version
, (const wxChar
*) fileEncoding
);
425 OutputString(stream
, s
, NULL
, NULL
);
426 OutputString(stream
, wxT("<richtext version=\"1.0.0.0\" xmlns=\"http://www.wxwidgets.org\">") , NULL
, NULL
);
429 bool success
= ExportXML(stream
, convMem
, convFile
, *buffer
, level
);
431 OutputString(stream
, wxT("\n</richtext>") , NULL
, NULL
);
432 OutputString(stream
, wxT("\n"), NULL
, NULL
);
440 /// Recursively export an object
441 bool wxRichTextXMLHandler::ExportXML(wxOutputStream
& stream
, wxMBConv
* convMem
, wxMBConv
* convFile
, wxRichTextObject
& obj
, int indent
)
444 if (obj
.IsKindOf(CLASSINFO(wxRichTextParagraphLayoutBox
)))
445 objectName
= wxT("paragraphlayout");
446 else if (obj
.IsKindOf(CLASSINFO(wxRichTextParagraph
)))
447 objectName
= wxT("paragraph");
448 else if (obj
.IsKindOf(CLASSINFO(wxRichTextPlainText
)))
449 objectName
= wxT("text");
450 else if (obj
.IsKindOf(CLASSINFO(wxRichTextImage
)))
451 objectName
= wxT("image");
453 objectName
= wxT("object");
455 if (obj
.IsKindOf(CLASSINFO(wxRichTextPlainText
)))
457 wxRichTextPlainText
& text
= (wxRichTextPlainText
&) obj
;
459 OutputIndentation(stream
, indent
);
460 OutputString(stream
, wxT("<") + objectName
, convMem
, convFile
);
462 wxString style
= CreateStyle(obj
.GetAttributes(), false);
464 OutputString(stream
, style
+ wxT(">"), convMem
, convFile
);
466 wxString str
= text
.GetText();
467 if (!str
.empty() && (str
[0] == wxT(' ') || str
[str
.length()-1] == wxT(' ')))
469 OutputString(stream
, wxT("\""), convMem
, convFile
);
470 OutputStringEnt(stream
, str
, convMem
, convFile
);
471 OutputString(stream
, wxT("\""), convMem
, convFile
);
474 OutputStringEnt(stream
, str
, convMem
, convFile
);
476 else if (obj
.IsKindOf(CLASSINFO(wxRichTextImage
)))
478 wxRichTextImage
& imageObj
= (wxRichTextImage
&) obj
;
480 if (imageObj
.GetImage().Ok() && !imageObj
.GetImageBlock().Ok())
481 imageObj
.MakeBlock();
483 OutputIndentation(stream
, indent
);
484 OutputString(stream
, wxT("<") + objectName
, convMem
, convFile
);
485 if (!imageObj
.GetImageBlock().Ok())
488 OutputString(stream
, wxT(">"), convMem
, convFile
);
492 OutputString(stream
, wxString::Format(wxT(" imagetype=\"%d\">"), (int) imageObj
.GetImageBlock().GetImageType()));
495 OutputIndentation(stream
, indent
+1);
496 OutputString(stream
, wxT("<data>"), convMem
, convFile
);
498 imageObj
.GetImageBlock().WriteHex(stream
);
500 OutputString(stream
, wxT("</data>"), convMem
, convFile
);
502 else if (obj
.IsKindOf(CLASSINFO(wxRichTextCompositeObject
)))
504 OutputIndentation(stream
, indent
);
505 OutputString(stream
, wxT("<") + objectName
, convMem
, convFile
);
508 if (objectName
== wxT("paragraph") || objectName
== wxT("paragraphlayout"))
511 wxString style
= CreateStyle(obj
.GetAttributes(), isPara
);
513 if (objectName
== wxT("paragraphlayout") && ((wxRichTextParagraphLayoutBox
&) obj
).GetPartialParagraph())
514 style
<< wxT(" partialparagraph=\"true\"");
516 OutputString(stream
, style
+ wxT(">"), convMem
, convFile
);
518 wxRichTextCompositeObject
& composite
= (wxRichTextCompositeObject
&) obj
;
520 for (i
= 0; i
< composite
.GetChildCount(); i
++)
522 wxRichTextObject
* child
= composite
.GetChild(i
);
523 ExportXML(stream
, convMem
, convFile
, *child
, indent
+1);
527 if (objectName
!= wxT("text"))
528 OutputIndentation(stream
, indent
);
530 OutputString(stream
, wxT("</") + objectName
+ wxT(">"), convMem
, convFile
);
535 /// Create style parameters
536 wxString
wxRichTextXMLHandler::CreateStyle(const wxTextAttrEx
& attr
, bool isPara
)
539 if (attr
.HasTextColour() && attr
.GetTextColour().Ok())
541 str
<< wxT(" textcolor=\"#") << ColourToHexString(attr
.GetTextColour()) << wxT("\"");
543 if (attr
.HasBackgroundColour() && attr
.GetBackgroundColour().Ok())
545 str
<< wxT(" bgcolor=\"#") << ColourToHexString(attr
.GetBackgroundColour()) << wxT("\"");
548 if (attr
.GetFont().Ok())
551 str
<< wxT(" fontsize=\"") << attr
.GetFont().GetPointSize() << wxT("\"");
553 //if (attr.HasFamily())
554 // str << wxT(" fontfamily=\"") << attr.GetFont().GetFamily() << wxT("\"");
556 if (attr
.HasItalic())
557 str
<< wxT(" fontstyle=\"") << attr
.GetFont().GetStyle() << wxT("\"");
559 if (attr
.HasWeight())
560 str
<< wxT(" fontweight=\"") << attr
.GetFont().GetWeight() << wxT("\"");
562 if (attr
.HasUnderlined())
563 str
<< wxT(" fontunderlined=\"") << (int) attr
.GetFont().GetUnderlined() << wxT("\"");
565 if (attr
.HasFaceName())
566 str
<< wxT(" fontface=\"") << attr
.GetFont().GetFaceName() << wxT("\"");
569 if (!attr
.GetCharacterStyleName().empty())
570 str
<< wxT(" charactertyle=\"") << wxString(attr
.GetCharacterStyleName()) << wxT("\"");
574 if (attr
.HasAlignment())
575 str
<< wxT(" alignment=\"") << (int) attr
.GetAlignment() << wxT("\"");
577 if (attr
.HasLeftIndent())
579 str
<< wxT(" leftindent=\"") << (int) attr
.GetLeftIndent() << wxT("\"");
580 str
<< wxT(" leftsubindent=\"") << (int) attr
.GetLeftSubIndent() << wxT("\"");
583 if (attr
.HasRightIndent())
584 str
<< wxT(" rightindent=\"") << (int) attr
.GetRightIndent() << wxT("\"");
586 if (attr
.HasParagraphSpacingAfter())
587 str
<< wxT(" parspacingafter=\"") << (int) attr
.GetParagraphSpacingAfter() << wxT("\"");
589 if (attr
.HasParagraphSpacingBefore())
590 str
<< wxT(" parspacingbefore=\"") << (int) attr
.GetParagraphSpacingBefore() << wxT("\"");
592 if (attr
.HasLineSpacing())
593 str
<< wxT(" linespacing=\"") << (int) attr
.GetLineSpacing() << wxT("\"");
595 if (attr
.HasBulletStyle())
596 str
<< wxT(" bulletstyle=\"") << (int) attr
.GetBulletStyle() << wxT("\"");
598 if (attr
.HasBulletNumber())
599 str
<< wxT(" bulletnumber=\"") << (int) attr
.GetBulletNumber() << wxT("\"");
601 if (attr
.HasBulletSymbol())
602 str
<< wxT(" bulletsymbol=\"") << wxString(attr
.GetBulletSymbol()) << wxT("\"");
604 if (!attr
.GetParagraphStyleName().empty())
605 str
<< wxT(" parstyle=\"") << wxString(attr
.GetParagraphStyleName()) << wxT("\"");
609 str
<< wxT(" tabs=\"");
611 for (i
= 0; i
< attr
.GetTabs().GetCount(); i
++)
615 str
<< attr
.GetTabs()[i
];
624 /// Get style parameters
625 bool wxRichTextXMLHandler::GetStyle(wxTextAttrEx
& attr
, wxXmlNode
* node
, bool isPara
)
627 wxString fontFacename
;
629 int fontFamily
= wxDEFAULT
;
630 int fontWeight
= wxNORMAL
;
631 int fontStyle
= wxNORMAL
;
632 bool fontUnderlined
= false;
636 fontFacename
= node
->GetPropVal(wxT("fontface"), wxEmptyString
);
637 if (!fontFacename
.IsEmpty())
638 fontFlags
|= wxTEXT_ATTR_FONT_FACE
;
641 //value = node->GetPropVal(wxT("fontfamily"), wxEmptyString);
642 //if (!value.empty())
643 // fontFamily = wxAtoi(value);
645 value
= node
->GetPropVal(wxT("fontstyle"), wxEmptyString
);
648 fontStyle
= wxAtoi(value
);
649 fontFlags
|= wxTEXT_ATTR_FONT_ITALIC
;
652 value
= node
->GetPropVal(wxT("fontsize"), wxEmptyString
);
655 fontSize
= wxAtoi(value
);
656 fontFlags
|= wxTEXT_ATTR_FONT_SIZE
;
659 value
= node
->GetPropVal(wxT("fontweight"), wxEmptyString
);
662 fontWeight
= wxAtoi(value
);
663 fontFlags
|= wxTEXT_ATTR_FONT_WEIGHT
;
666 value
= node
->GetPropVal(wxT("fontunderlined"), wxEmptyString
);
669 fontUnderlined
= wxAtoi(value
) != 0;
670 fontFlags
|= wxTEXT_ATTR_FONT_UNDERLINE
;
673 attr
.SetFlags(fontFlags
);
675 if (attr
.HasFlag(wxTEXT_ATTR_FONT
))
676 attr
.SetFont(* wxTheFontList
->FindOrCreateFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, fontUnderlined
, fontFacename
));
678 // Restore correct font flags
679 attr
.SetFlags(fontFlags
);
681 value
= node
->GetPropVal(wxT("textcolor"), wxEmptyString
);
684 if (value
[0] == wxT('#'))
685 attr
.SetTextColour(HexStringToColour(value
.Mid(1)));
687 attr
.SetTextColour(value
);
690 value
= node
->GetPropVal(wxT("backgroundcolor"), wxEmptyString
);
693 if (value
[0] == wxT('#'))
694 attr
.SetBackgroundColour(HexStringToColour(value
.Mid(1)));
696 attr
.SetBackgroundColour(value
);
699 value
= node
->GetPropVal(wxT("characterstyle"), wxEmptyString
);
701 attr
.SetCharacterStyleName(value
);
703 // Set paragraph attributes
706 value
= node
->GetPropVal(wxT("alignment"), wxEmptyString
);
708 attr
.SetAlignment((wxTextAttrAlignment
) wxAtoi(value
));
710 int leftSubIndent
= 0;
712 bool hasLeftIndent
= false;
714 value
= node
->GetPropVal(wxT("leftindent"), wxEmptyString
);
717 leftIndent
= wxAtoi(value
);
718 hasLeftIndent
= true;
721 value
= node
->GetPropVal(wxT("leftsubindent"), wxEmptyString
);
724 leftSubIndent
= wxAtoi(value
);
725 hasLeftIndent
= true;
729 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
731 value
= node
->GetPropVal(wxT("rightindent"), wxEmptyString
);
733 attr
.SetRightIndent(wxAtoi(value
));
735 value
= node
->GetPropVal(wxT("parspacingbefore"), wxEmptyString
);
737 attr
.SetParagraphSpacingBefore(wxAtoi(value
));
739 value
= node
->GetPropVal(wxT("parspacingafter"), wxEmptyString
);
741 attr
.SetParagraphSpacingAfter(wxAtoi(value
));
743 value
= node
->GetPropVal(wxT("linespacing"), wxEmptyString
);
745 attr
.SetLineSpacing(wxAtoi(value
));
747 value
= node
->GetPropVal(wxT("bulletstyle"), wxEmptyString
);
749 attr
.SetBulletStyle(wxAtoi(value
));
751 value
= node
->GetPropVal(wxT("bulletnumber"), wxEmptyString
);
753 attr
.SetBulletNumber(wxAtoi(value
));
755 value
= node
->GetPropVal(wxT("bulletsymbol"), wxEmptyString
);
757 attr
.SetBulletSymbol(value
[0]);
759 value
= node
->GetPropVal(wxT("parstyle"), wxEmptyString
);
761 attr
.SetParagraphStyleName(value
);
763 value
= node
->GetPropVal(wxT("tabs"), wxEmptyString
);
767 wxStringTokenizer
tkz(value
, wxT(","));
768 while (tkz
.HasMoreTokens())
770 wxString token
= tkz
.GetNextToken();
771 tabs
.Add(wxAtoi(token
));
784 // wxUSE_RICHTEXT && wxUSE_XML