]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/richtext/richtextxml.cpp
added wxWindowDisabler ctor for conditionally disabling all windows and use it in...
[wxWidgets.git] / src / richtext / richtextxml.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/richtext/richtextxml.cpp
3// Purpose: XML and HTML I/O for wxRichTextCtrl
4// Author: Julian Smart
5// Modified by:
6// Created: 2005-09-30
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16 #pragma hdrstop
17#endif
18
19#if wxUSE_RICHTEXT && wxUSE_XML
20
21#include "wx/richtext/richtextxml.h"
22
23#ifndef WX_PRECOMP
24 #include "wx/intl.h"
25 #include "wx/module.h"
26#endif
27
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"
35
36IMPLEMENT_DYNAMIC_CLASS(wxRichTextXMLHandler, wxRichTextFileHandler)
37
38#if wxUSE_STREAMS
39bool wxRichTextXMLHandler::DoLoadFile(wxRichTextBuffer *buffer, wxInputStream& stream)
40{
41 if (!stream.IsOk())
42 return false;
43
44 buffer->ResetAndClearCommands();
45 buffer->Clear();
46
47 wxXmlDocument* xmlDoc = new wxXmlDocument;
48 bool success = true;
49
50 // This is the encoding to convert to (memory encoding rather than file encoding)
51 wxString encoding(wxT("UTF-8"));
52
53#if !wxUSE_UNICODE && wxUSE_INTL
54 encoding = wxLocale::GetSystemEncodingName();
55#endif
56
57 if (!xmlDoc->Load(stream, encoding))
58 {
59 buffer->ResetAndClearCommands();
60 success = false;
61 }
62 else
63 {
64 if (xmlDoc->GetRoot() && xmlDoc->GetRoot()->GetType() == wxXML_ELEMENT_NODE && xmlDoc->GetRoot()->GetName() == wxT("richtext"))
65 {
66 wxXmlNode* child = xmlDoc->GetRoot()->GetChildren();
67 while (child)
68 {
69 if (child->GetType() == wxXML_ELEMENT_NODE)
70 {
71 wxString name = child->GetName();
72 if (name == wxT("richtext-version"))
73 {
74 }
75 else
76 ImportXML(buffer, child);
77 }
78
79 child = child->GetNext();
80 }
81 }
82 else
83 {
84 success = false;
85 }
86 }
87
88 delete xmlDoc;
89
90 buffer->UpdateRanges();
91
92 return success;
93}
94
95/// Recursively import an object
96bool wxRichTextXMLHandler::ImportXML(wxRichTextBuffer* buffer, wxXmlNode* node)
97{
98 wxString name = node->GetName();
99
100 bool doneChildren = false;
101
102 if (name == wxT("paragraphlayout"))
103 {
104 wxString partial = node->GetAttribute(wxT("partialparagraph"), wxEmptyString);
105 if (partial == wxT("true"))
106 buffer->SetPartialParagraph(true);
107 }
108 else if (name == wxT("paragraph"))
109 {
110 wxRichTextParagraph* para = new wxRichTextParagraph(buffer);
111 buffer->AppendChild(para);
112
113 GetStyle(para->GetAttributes(), node, true);
114
115 wxXmlNode* child = node->GetChildren();
116 while (child)
117 {
118 wxString childName = child->GetName();
119 if (childName == wxT("text"))
120 {
121 wxString text;
122 wxXmlNode* textChild = child->GetChildren();
123 while (textChild)
124 {
125 if (textChild->GetType() == wxXML_TEXT_NODE ||
126 textChild->GetType() == wxXML_CDATA_SECTION_NODE)
127 {
128 wxString text2 = textChild->GetContent();
129
130 // Strip whitespace from end
131 if (!text2.empty() && text2[text2.length()-1] == wxT('\n'))
132 text2 = text2.Mid(0, text2.length()-1);
133
134 if (!text2.empty() && text2[0] == wxT('"'))
135 text2 = text2.Mid(1);
136 if (!text2.empty() && text2[text2.length()-1] == wxT('"'))
137 text2 = text2.Mid(0, text2.length() - 1);
138
139 text += text2;
140 }
141 textChild = textChild->GetNext();
142 }
143
144 wxRichTextPlainText* textObject = new wxRichTextPlainText(text, para);
145 GetStyle(textObject->GetAttributes(), child, false);
146
147 para->AppendChild(textObject);
148 }
149 else if (childName == wxT("symbol"))
150 {
151 // This is a symbol that XML can't read in the normal way
152 wxString text;
153 wxXmlNode* textChild = child->GetChildren();
154 while (textChild)
155 {
156 if (textChild->GetType() == wxXML_TEXT_NODE ||
157 textChild->GetType() == wxXML_CDATA_SECTION_NODE)
158 {
159 wxString text2 = textChild->GetContent();
160 text += text2;
161 }
162 textChild = textChild->GetNext();
163 }
164
165 wxString actualText;
166 actualText << (wxChar) wxAtoi(text);
167
168 wxRichTextPlainText* textObject = new wxRichTextPlainText(actualText, para);
169 GetStyle(textObject->GetAttributes(), child, false);
170
171 para->AppendChild(textObject);
172 }
173 else if (childName == wxT("image"))
174 {
175 int imageType = wxBITMAP_TYPE_PNG;
176 wxString value = node->GetAttribute(wxT("imagetype"), wxEmptyString);
177 if (!value.empty())
178 imageType = wxAtoi(value);
179
180 wxString data;
181
182 wxXmlNode* imageChild = child->GetChildren();
183 while (imageChild)
184 {
185 wxString childName = imageChild->GetName();
186 if (childName == wxT("data"))
187 {
188 wxXmlNode* dataChild = imageChild->GetChildren();
189 while (dataChild)
190 {
191 data = dataChild->GetContent();
192 // wxLogDebug(data);
193 dataChild = dataChild->GetNext();
194 }
195
196 }
197 imageChild = imageChild->GetNext();
198 }
199
200 if (!data.empty())
201 {
202 wxRichTextImage* imageObj = new wxRichTextImage(para);
203 GetStyle(imageObj->GetAttributes(), child, false);
204 para->AppendChild(imageObj);
205
206 wxStringInputStream strStream(data);
207
208 imageObj->GetImageBlock().ReadHex(strStream, data.length(), imageType);
209 }
210 }
211 child = child->GetNext();
212 }
213
214 doneChildren = true;
215 }
216 else if (name == wxT("stylesheet"))
217 {
218 if (GetFlags() & wxRICHTEXT_HANDLER_INCLUDE_STYLESHEET)
219 {
220 wxRichTextStyleSheet* sheet = new wxRichTextStyleSheet;
221 wxString sheetName = node->GetAttribute(wxT("name"), wxEmptyString);
222 wxString sheetDescription = node->GetAttribute(wxT("description"), wxEmptyString);
223 sheet->SetName(sheetName);
224 sheet->SetDescription(sheetDescription);
225
226 wxXmlNode* child = node->GetChildren();
227 while (child)
228 {
229 ImportStyleDefinition(sheet, child);
230
231 child = child->GetNext();
232 }
233
234 // Notify that styles have changed. If this is vetoed by the app,
235 // the new sheet will be deleted. If it is not vetoed, the
236 // old sheet will be deleted and replaced with the new one.
237 buffer->SetStyleSheetAndNotify(sheet);
238 }
239 doneChildren = true;
240 }
241
242 if (!doneChildren)
243 {
244 wxXmlNode* child = node->GetChildren();
245 while (child)
246 {
247 ImportXML(buffer, child);
248 child = child->GetNext();
249 }
250 }
251
252 return true;
253}
254
255bool wxRichTextXMLHandler::ImportStyleDefinition(wxRichTextStyleSheet* sheet, wxXmlNode* node)
256{
257 wxString styleType = node->GetName();
258 wxString styleName = node->GetAttribute(wxT("name"), wxEmptyString);
259 wxString baseStyleName = node->GetAttribute(wxT("basestyle"), wxEmptyString);
260
261 if (styleName.IsEmpty())
262 return false;
263
264 if (styleType == wxT("characterstyle"))
265 {
266 wxRichTextCharacterStyleDefinition* def = new wxRichTextCharacterStyleDefinition(styleName);
267 def->SetBaseStyle(baseStyleName);
268
269 wxXmlNode* child = node->GetChildren();
270 while (child)
271 {
272 if (child->GetName() == wxT("style"))
273 {
274 wxTextAttr attr;
275 GetStyle(attr, child, false);
276 def->SetStyle(attr);
277 }
278 child = child->GetNext();
279 }
280
281 sheet->AddCharacterStyle(def);
282 }
283 else if (styleType == wxT("paragraphstyle"))
284 {
285 wxRichTextParagraphStyleDefinition* def = new wxRichTextParagraphStyleDefinition(styleName);
286
287 wxString nextStyleName = node->GetAttribute(wxT("nextstyle"), wxEmptyString);
288 def->SetNextStyle(nextStyleName);
289 def->SetBaseStyle(baseStyleName);
290
291 wxXmlNode* child = node->GetChildren();
292 while (child)
293 {
294 if (child->GetName() == wxT("style"))
295 {
296 wxTextAttr attr;
297 GetStyle(attr, child, false);
298 def->SetStyle(attr);
299 }
300 child = child->GetNext();
301 }
302
303 sheet->AddParagraphStyle(def);
304 }
305 else if (styleType == wxT("liststyle"))
306 {
307 wxRichTextListStyleDefinition* def = new wxRichTextListStyleDefinition(styleName);
308
309 wxString nextStyleName = node->GetAttribute(wxT("nextstyle"), wxEmptyString);
310 def->SetNextStyle(nextStyleName);
311 def->SetBaseStyle(baseStyleName);
312
313 wxXmlNode* child = node->GetChildren();
314 while (child)
315 {
316 if (child->GetName() == wxT("style"))
317 {
318 wxTextAttr attr;
319 GetStyle(attr, child, false);
320
321 wxString styleLevel = child->GetAttribute(wxT("level"), wxEmptyString);
322 if (styleLevel.IsEmpty())
323 {
324 def->SetStyle(attr);
325 }
326 else
327 {
328 int level = wxAtoi(styleLevel);
329 if (level > 0 && level <= 10)
330 {
331 def->SetLevelAttributes(level-1, attr);
332 }
333 }
334 }
335 child = child->GetNext();
336 }
337
338 sheet->AddListStyle(def);
339 }
340
341 return true;
342}
343
344//-----------------------------------------------------------------------------
345// xml support routines
346//-----------------------------------------------------------------------------
347
348bool wxRichTextXMLHandler::HasParam(wxXmlNode* node, const wxString& param)
349{
350 return (GetParamNode(node, param) != NULL);
351}
352
353wxXmlNode *wxRichTextXMLHandler::GetParamNode(wxXmlNode* node, const wxString& param)
354{
355 wxCHECK_MSG(node, NULL, wxT("You can't access node data before it was initialized!"));
356
357 wxXmlNode *n = node->GetChildren();
358
359 while (n)
360 {
361 if (n->GetType() == wxXML_ELEMENT_NODE && n->GetName() == param)
362 return n;
363 n = n->GetNext();
364 }
365 return NULL;
366}
367
368
369wxString wxRichTextXMLHandler::GetNodeContent(wxXmlNode *node)
370{
371 wxXmlNode *n = node;
372 if (n == NULL) return wxEmptyString;
373 n = n->GetChildren();
374
375 while (n)
376 {
377 if (n->GetType() == wxXML_TEXT_NODE ||
378 n->GetType() == wxXML_CDATA_SECTION_NODE)
379 return n->GetContent();
380 n = n->GetNext();
381 }
382 return wxEmptyString;
383}
384
385
386wxString wxRichTextXMLHandler::GetParamValue(wxXmlNode *node, const wxString& param)
387{
388 if (param.empty())
389 return GetNodeContent(node);
390 else
391 return GetNodeContent(GetParamNode(node, param));
392}
393
394wxString wxRichTextXMLHandler::GetText(wxXmlNode *node, const wxString& param, bool WXUNUSED(translate))
395{
396 wxXmlNode *parNode = GetParamNode(node, param);
397 if (!parNode)
398 parNode = node;
399 wxString str1(GetNodeContent(parNode));
400 return str1;
401}
402
403// For use with earlier versions of wxWidgets
404#ifndef WXUNUSED_IN_UNICODE
405#if wxUSE_UNICODE
406#define WXUNUSED_IN_UNICODE(x) WXUNUSED(x)
407#else
408#define WXUNUSED_IN_UNICODE(x) x
409#endif
410#endif
411
412// write string to output:
413inline static void OutputString(wxOutputStream& stream, const wxString& str,
414 wxMBConv *WXUNUSED_IN_UNICODE(convMem) = NULL, wxMBConv *convFile = NULL)
415{
416 if (str.empty()) return;
417#if wxUSE_UNICODE
418 if (convFile)
419 {
420 const wxWX2MBbuf buf(str.mb_str(*convFile));
421 stream.Write((const char*)buf, strlen((const char*)buf));
422 }
423 else
424 {
425 const wxWX2MBbuf buf(str.mb_str(wxConvUTF8));
426 stream.Write((const char*)buf, strlen((const char*)buf));
427 }
428#else
429 if ( convFile == NULL )
430 stream.Write(str.mb_str(), str.Len());
431 else
432 {
433 wxString str2(str.wc_str(*convMem), *convFile);
434 stream.Write(str2.mb_str(), str2.Len());
435 }
436#endif
437}
438
439// Same as above, but create entities first.
440// Translates '<' to "&lt;", '>' to "&gt;" and '&' to "&amp;"
441static void OutputStringEnt(wxOutputStream& stream, const wxString& str,
442 wxMBConv *convMem = NULL, wxMBConv *convFile = NULL)
443{
444 wxString buf;
445 size_t i, last, len;
446 wxChar c;
447
448 len = str.Len();
449 last = 0;
450 for (i = 0; i < len; i++)
451 {
452 c = str.GetChar(i);
453
454 // Original code excluded "&amp;" but we _do_ want to convert
455 // the ampersand beginning &amp; because otherwise when read in,
456 // the original "&amp;" becomes "&".
457
458 if (c == wxT('<') || c == wxT('>') || c == wxT('"') ||
459 (c == wxT('&') /* && (str.Mid(i+1, 4) != wxT("amp;")) */ ))
460 {
461 OutputString(stream, str.Mid(last, i - last), convMem, convFile);
462 switch (c)
463 {
464 case wxT('<'):
465 OutputString(stream, wxT("&lt;"), NULL, NULL);
466 break;
467 case wxT('>'):
468 OutputString(stream, wxT("&gt;"), NULL, NULL);
469 break;
470 case wxT('&'):
471 OutputString(stream, wxT("&amp;"), NULL, NULL);
472 break;
473 case wxT('"'):
474 OutputString(stream, wxT("&quot;"), NULL, NULL);
475 break;
476 default: break;
477 }
478 last = i + 1;
479 }
480 else if (wxUChar(c) > 127)
481 {
482 OutputString(stream, str.Mid(last, i - last), convMem, convFile);
483
484 wxString s(wxT("&#"));
485 s << (int) c;
486 s << wxT(";");
487 OutputString(stream, s, NULL, NULL);
488 last = i + 1;
489 }
490 }
491 OutputString(stream, str.Mid(last, i - last), convMem, convFile);
492}
493
494static wxString AttributeToXML(const wxString& str)
495{
496 wxString str1;
497 size_t i, last, len;
498 wxChar c;
499
500 len = str.Len();
501 last = 0;
502 for (i = 0; i < len; i++)
503 {
504 c = str.GetChar(i);
505
506 // Original code excluded "&amp;" but we _do_ want to convert
507 // the ampersand beginning &amp; because otherwise when read in,
508 // the original "&amp;" becomes "&".
509
510 if (c == wxT('<') || c == wxT('>') || c == wxT('"') ||
511 (c == wxT('&') /* && (str.Mid(i+1, 4) != wxT("amp;")) */ ))
512 {
513 str1 += str.Mid(last, i - last);
514 switch (c)
515 {
516 case wxT('<'):
517 str1 += wxT("&lt;");
518 break;
519 case wxT('>'):
520 str1 += wxT("&gt;");
521 break;
522 case wxT('&'):
523 str1 += wxT("&amp;");
524 break;
525 case wxT('"'):
526 str1 += wxT("&quot;");
527 break;
528 default: break;
529 }
530 last = i + 1;
531 }
532 else if (wxUChar(c) > 127)
533 {
534 str1 += str.Mid(last, i - last);
535
536 wxString s(wxT("&#"));
537 s << (int) c;
538 s << wxT(";");
539 str1 += s;
540 last = i + 1;
541 }
542 }
543 str1 += str.Mid(last, i - last);
544 return str1;
545}
546
547inline static void OutputIndentation(wxOutputStream& stream, int indent)
548{
549 wxString str = wxT("\n");
550 for (int i = 0; i < indent; i++)
551 str << wxT(' ') << wxT(' ');
552 OutputString(stream, str, NULL, NULL);
553}
554
555// Convert a colour to a 6-digit hex string
556static wxString ColourToHexString(const wxColour& col)
557{
558 wxString hex;
559
560 hex += wxDecToHex(col.Red());
561 hex += wxDecToHex(col.Green());
562 hex += wxDecToHex(col.Blue());
563
564 return hex;
565}
566
567// Convert 6-digit hex string to a colour
568static wxColour HexStringToColour(const wxString& hex)
569{
570 unsigned char r = (unsigned char)wxHexToDec(hex.Mid(0, 2));
571 unsigned char g = (unsigned char)wxHexToDec(hex.Mid(2, 2));
572 unsigned char b = (unsigned char)wxHexToDec(hex.Mid(4, 2));
573
574 return wxColour(r, g, b);
575}
576
577bool wxRichTextXMLHandler::DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream)
578{
579 if (!stream.IsOk())
580 return false;
581
582 wxString version(wxT("1.0") ) ;
583
584 bool deleteConvFile = false;
585 wxString fileEncoding;
586 wxMBConv* convFile = NULL;
587
588#if wxUSE_UNICODE
589 fileEncoding = wxT("UTF-8");
590 convFile = & wxConvUTF8;
591#else
592 fileEncoding = wxT("ISO-8859-1");
593 convFile = & wxConvISO8859_1;
594#endif
595
596 // If SetEncoding has been called, change the output encoding.
597 if (!m_encoding.empty() && m_encoding.Lower() != fileEncoding.Lower())
598 {
599 if (m_encoding == wxT("<System>"))
600 {
601#if wxUSE_INTL
602 fileEncoding = wxLocale::GetSystemEncodingName();
603 // if !wxUSE_INTL, we fall back to UTF-8 or ISO-8859-1 below
604#endif
605 }
606 else
607 {
608 fileEncoding = m_encoding;
609 }
610
611 // GetSystemEncodingName may not have returned a name
612 if (fileEncoding.empty())
613#if wxUSE_UNICODE
614 fileEncoding = wxT("UTF-8");
615#else
616 fileEncoding = wxT("ISO-8859-1");
617#endif
618 convFile = new wxCSConv(fileEncoding);
619 deleteConvFile = true;
620 }
621
622#if !wxUSE_UNICODE
623 wxMBConv* convMem = wxConvCurrent;
624#else
625 wxMBConv* convMem = NULL;
626#endif
627
628 wxString s ;
629 s.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"),
630 version, fileEncoding);
631 OutputString(stream, s, NULL, NULL);
632 OutputString(stream, wxT("<richtext version=\"1.0.0.0\" xmlns=\"http://www.wxwidgets.org\">") , NULL, NULL);
633
634 int level = 1;
635
636 if (buffer->GetStyleSheet() && (GetFlags() & wxRICHTEXT_HANDLER_INCLUDE_STYLESHEET))
637 {
638 OutputIndentation(stream, level);
639 wxString nameAndDescr;
640 if (!buffer->GetStyleSheet()->GetName().IsEmpty())
641 nameAndDescr << wxT(" name=\"") << buffer->GetStyleSheet()->GetName() << wxT("\"");
642 if (!buffer->GetStyleSheet()->GetDescription().IsEmpty())
643 nameAndDescr << wxT(" description=\"") << buffer->GetStyleSheet()->GetDescription() << wxT("\"");
644 OutputString(stream, wxString(wxT("<stylesheet")) + nameAndDescr + wxT(">"), convMem, convFile);
645
646 int i;
647
648 for (i = 0; i < (int) buffer->GetStyleSheet()->GetCharacterStyleCount(); i++)
649 {
650 wxRichTextCharacterStyleDefinition* def = buffer->GetStyleSheet()->GetCharacterStyle(i);
651 ExportStyleDefinition(stream, convMem, convFile, def, level + 1);
652 }
653
654 for (i = 0; i < (int) buffer->GetStyleSheet()->GetParagraphStyleCount(); i++)
655 {
656 wxRichTextParagraphStyleDefinition* def = buffer->GetStyleSheet()->GetParagraphStyle(i);
657 ExportStyleDefinition(stream, convMem, convFile, def, level + 1);
658 }
659
660 for (i = 0; i < (int) buffer->GetStyleSheet()->GetListStyleCount(); i++)
661 {
662 wxRichTextListStyleDefinition* def = buffer->GetStyleSheet()->GetListStyle(i);
663 ExportStyleDefinition(stream, convMem, convFile, def, level + 1);
664 }
665
666 OutputIndentation(stream, level);
667 OutputString(stream, wxT("</stylesheet>"), convMem, convFile);
668 }
669
670
671 bool success = ExportXML(stream, convMem, convFile, *buffer, level);
672
673 OutputString(stream, wxT("\n</richtext>") , NULL, NULL);
674 OutputString(stream, wxT("\n"), NULL, NULL);
675
676 if (deleteConvFile)
677 delete convFile;
678
679 return success;
680}
681
682/// Recursively export an object
683bool wxRichTextXMLHandler::ExportXML(wxOutputStream& stream, wxMBConv* convMem, wxMBConv* convFile, wxRichTextObject& obj, int indent)
684{
685 wxString objectName;
686 if (obj.IsKindOf(CLASSINFO(wxRichTextParagraphLayoutBox)))
687 objectName = wxT("paragraphlayout");
688 else if (obj.IsKindOf(CLASSINFO(wxRichTextParagraph)))
689 objectName = wxT("paragraph");
690 else if (obj.IsKindOf(CLASSINFO(wxRichTextPlainText)))
691 objectName = wxT("text");
692 else if (obj.IsKindOf(CLASSINFO(wxRichTextImage)))
693 objectName = wxT("image");
694 else
695 objectName = wxT("object");
696
697 bool terminateTag = true;
698
699 if (obj.IsKindOf(CLASSINFO(wxRichTextPlainText)))
700 {
701 wxRichTextPlainText& textObj = (wxRichTextPlainText&) obj;
702
703 wxString style = CreateStyle(obj.GetAttributes(), false);
704
705 int i;
706 int last = 0;
707 const wxString& text = textObj.GetText();
708 int len = (int) text.Length();
709
710 if (len == 0)
711 {
712 i = 0;
713 OutputIndentation(stream, indent);
714 OutputString(stream, wxT("<") + objectName, convMem, convFile);
715 OutputString(stream, style + wxT(">"), convMem, convFile);
716 OutputString(stream, wxT("</text>"), convMem, convFile);
717 }
718 else for (i = 0; i < len; i++)
719 {
720 int c = (int) text[i];
721 if (c < 32 && c != 9 && c != 10 && c != 13)
722 {
723 if (i > 0)
724 {
725 OutputIndentation(stream, indent);
726 OutputString(stream, wxT("<") + objectName, convMem, convFile);
727
728 OutputString(stream, style + wxT(">"), convMem, convFile);
729
730 wxString fragment(text.Mid(last, i-last));
731 if (!fragment.empty() && (fragment[0] == wxT(' ') || fragment[fragment.length()-1] == wxT(' ')))
732 {
733 OutputString(stream, wxT("\""), convMem, convFile);
734 OutputStringEnt(stream, fragment, convMem, convFile);
735 OutputString(stream, wxT("\""), convMem, convFile);
736 }
737 else
738 OutputStringEnt(stream, fragment, convMem, convFile);
739
740 OutputString(stream, wxT("</text>"), convMem, convFile);
741 }
742
743
744 // Output this character as a number in a separate tag, because XML can't cope
745 // with entities below 32 except for 9, 10 and 13
746 last = i + 1;
747 OutputIndentation(stream, indent);
748 OutputString(stream, wxT("<symbol"), convMem, convFile);
749
750 OutputString(stream, style + wxT(">"), convMem, convFile);
751 OutputString(stream, wxString::Format(wxT("%d"), c), convMem, convFile);
752
753 OutputString(stream, wxT("</symbol>"), convMem, convFile);
754 }
755 }
756
757 wxString fragment;
758 if (last == 0)
759 fragment = text;
760 else
761 fragment = text.Mid(last, i-last);
762
763 if (last < len)
764 {
765 OutputIndentation(stream, indent);
766 OutputString(stream, wxT("<") + objectName, convMem, convFile);
767
768 OutputString(stream, style + wxT(">"), convMem, convFile);
769
770 if (!fragment.empty() && (fragment[0] == wxT(' ') || fragment[fragment.length()-1] == wxT(' ')))
771 {
772 OutputString(stream, wxT("\""), convMem, convFile);
773 OutputStringEnt(stream, fragment, convMem, convFile);
774 OutputString(stream, wxT("\""), convMem, convFile);
775 }
776 else
777 OutputStringEnt(stream, fragment, convMem, convFile);
778 }
779 else
780 terminateTag = false;
781 }
782 else if (obj.IsKindOf(CLASSINFO(wxRichTextImage)))
783 {
784 wxRichTextImage& imageObj = (wxRichTextImage&) obj;
785
786 wxString style = CreateStyle(obj.GetAttributes(), false);
787
788 if (imageObj.GetImage().Ok() && !imageObj.GetImageBlock().Ok())
789 imageObj.MakeBlock();
790
791 OutputIndentation(stream, indent);
792 OutputString(stream, wxT("<") + objectName, convMem, convFile);
793 if (!imageObj.GetImageBlock().Ok())
794 {
795 // No data
796 OutputString(stream, style + wxT(">"), convMem, convFile);
797 }
798 else
799 {
800 OutputString(stream, wxString::Format(wxT(" imagetype=\"%d\"") + style + wxT(">"), (int) imageObj.GetImageBlock().GetImageType()));
801 }
802
803 OutputIndentation(stream, indent+1);
804 OutputString(stream, wxT("<data>"), convMem, convFile);
805
806 imageObj.GetImageBlock().WriteHex(stream);
807
808 OutputString(stream, wxT("</data>"), convMem, convFile);
809 }
810 else if (obj.IsKindOf(CLASSINFO(wxRichTextCompositeObject)))
811 {
812 OutputIndentation(stream, indent);
813 OutputString(stream, wxT("<") + objectName, convMem, convFile);
814
815 bool isPara = false;
816 if (objectName == wxT("paragraph") || objectName == wxT("paragraphlayout"))
817 isPara = true;
818
819 wxString style = CreateStyle(obj.GetAttributes(), isPara);
820
821 if (objectName == wxT("paragraphlayout") && ((wxRichTextParagraphLayoutBox&) obj).GetPartialParagraph())
822 style << wxT(" partialparagraph=\"true\"");
823
824 OutputString(stream, style + wxT(">"), convMem, convFile);
825
826 wxRichTextCompositeObject& composite = (wxRichTextCompositeObject&) obj;
827 size_t i;
828 for (i = 0; i < composite.GetChildCount(); i++)
829 {
830 wxRichTextObject* child = composite.GetChild(i);
831 ExportXML(stream, convMem, convFile, *child, indent+1);
832 }
833 }
834
835 if (objectName != wxT("text"))
836 OutputIndentation(stream, indent);
837
838 if (terminateTag)
839 OutputString(stream, wxT("</") + objectName + wxT(">"), convMem, convFile);
840
841 return true;
842}
843
844bool wxRichTextXMLHandler::ExportStyleDefinition(wxOutputStream& stream, wxMBConv* convMem, wxMBConv* convFile, wxRichTextStyleDefinition* def, int level)
845{
846 wxRichTextCharacterStyleDefinition* charDef = wxDynamicCast(def, wxRichTextCharacterStyleDefinition);
847 wxRichTextParagraphStyleDefinition* paraDef = wxDynamicCast(def, wxRichTextParagraphStyleDefinition);
848 wxRichTextListStyleDefinition* listDef = wxDynamicCast(def, wxRichTextListStyleDefinition);
849
850 wxString baseStyle = def->GetBaseStyle();
851 wxString baseStyleProp;
852 if (!baseStyle.IsEmpty())
853 baseStyleProp = wxT(" basestyle=\"") + baseStyle + wxT("\"");
854
855 wxString descr = def->GetDescription();
856 wxString descrProp;
857 if (!descr.IsEmpty())
858 descrProp = wxT(" description=\"") + descr + wxT("\"");
859
860 if (charDef)
861 {
862 OutputIndentation(stream, level);
863 OutputString(stream, wxT("<characterstyle") + baseStyleProp + descrProp + wxT(">"), convMem, convFile);
864
865 level ++;
866
867 wxString style = CreateStyle(def->GetStyle(), false);
868
869 OutputIndentation(stream, level);
870 OutputString(stream, wxT("<style ") + style + wxT(">"), convMem, convFile);
871
872 OutputIndentation(stream, level);
873 OutputString(stream, wxT("</style>"), convMem, convFile);
874
875 level --;
876
877 OutputIndentation(stream, level);
878 OutputString(stream, wxT("</characterstyle>"), convMem, convFile);
879 }
880 else if (listDef)
881 {
882 OutputIndentation(stream, level);
883
884 if (!listDef->GetNextStyle().IsEmpty())
885 baseStyleProp << wxT(" basestyle=\"") << listDef->GetNextStyle() << wxT("\"");
886
887 OutputString(stream, wxT("<liststyle") + baseStyleProp + descrProp + wxT(">"), convMem, convFile);
888
889 level ++;
890
891 wxString style = CreateStyle(def->GetStyle(), false);
892
893 OutputIndentation(stream, level);
894 OutputString(stream, wxT("<style ") + style + wxT(">"), convMem, convFile);
895
896 OutputIndentation(stream, level);
897 OutputString(stream, wxT("</style>"), convMem, convFile);
898
899 int i;
900 for (i = 0; i < 10; i ++)
901 {
902 wxTextAttr* levelAttr = listDef->GetLevelAttributes(i);
903 if (levelAttr)
904 {
905 wxString style = CreateStyle(def->GetStyle(), false);
906 wxString levelStr = wxString::Format(wxT(" level=\"%d\" "), (i+1));
907
908 OutputIndentation(stream, level);
909 OutputString(stream, wxT("<style ") + levelStr + style + wxT(">"), convMem, convFile);
910
911 OutputIndentation(stream, level);
912 OutputString(stream, wxT("</style>"), convMem, convFile);
913 }
914 }
915
916 level --;
917
918 OutputIndentation(stream, level);
919 OutputString(stream, wxT("</liststyle>"), convMem, convFile);
920 }
921 else if (paraDef)
922 {
923 OutputIndentation(stream, level);
924
925 if (!paraDef->GetNextStyle().IsEmpty())
926 baseStyleProp << wxT(" basestyle=\"") << paraDef->GetNextStyle() << wxT("\"");
927
928 OutputString(stream, wxT("<paragraphstyle") + baseStyleProp + descrProp + wxT(">"), convMem, convFile);
929
930 level ++;
931
932 wxString style = CreateStyle(def->GetStyle(), false);
933
934 OutputIndentation(stream, level);
935 OutputString(stream, wxT("<style ") + style + wxT(">"), convMem, convFile);
936
937 OutputIndentation(stream, level);
938 OutputString(stream, wxT("</style>"), convMem, convFile);
939
940 level --;
941
942 OutputIndentation(stream, level);
943 OutputString(stream, wxT("</paragraphstyle>"), convMem, convFile);
944 }
945
946 return true;
947}
948
949/// Create style parameters
950wxString wxRichTextXMLHandler::CreateStyle(const wxTextAttr& attr, bool isPara)
951{
952 wxString str;
953 if (attr.HasTextColour() && attr.GetTextColour().Ok())
954 {
955 str << wxT(" textcolor=\"#") << ColourToHexString(attr.GetTextColour()) << wxT("\"");
956 }
957 if (attr.HasBackgroundColour() && attr.GetBackgroundColour().Ok())
958 {
959 str << wxT(" bgcolor=\"#") << ColourToHexString(attr.GetBackgroundColour()) << wxT("\"");
960 }
961
962 if (attr.HasFontSize())
963 str << wxT(" fontsize=\"") << attr.GetFontSize() << wxT("\"");
964
965 //if (attr.HasFontFamily())
966 // str << wxT(" fontfamily=\"") << attr.GetFont().GetFamily() << wxT("\"");
967
968 if (attr.HasFontItalic())
969 str << wxT(" fontstyle=\"") << attr.GetFontStyle() << wxT("\"");
970
971 if (attr.HasFontWeight())
972 str << wxT(" fontweight=\"") << attr.GetFontWeight() << wxT("\"");
973
974 if (attr.HasFontUnderlined())
975 str << wxT(" fontunderlined=\"") << (int) attr.GetFontUnderlined() << wxT("\"");
976
977 if (attr.HasFontFaceName())
978 str << wxT(" fontface=\"") << attr.GetFontFaceName() << wxT("\"");
979
980 if (attr.HasTextEffects())
981 {
982 str << wxT(" texteffects=\"");
983 str << attr.GetTextEffects();
984 str << wxT("\"");
985
986 str << wxT(" texteffectflags=\"");
987 str << attr.GetTextEffectFlags();
988 str << wxT("\"");
989 }
990
991 if (!attr.GetCharacterStyleName().empty())
992 str << wxT(" characterstyle=\"") << wxString(attr.GetCharacterStyleName()) << wxT("\"");
993
994 if (attr.HasURL())
995 str << wxT(" url=\"") << AttributeToXML(attr.GetURL()) << wxT("\"");
996
997 if (isPara)
998 {
999 if (attr.HasAlignment())
1000 str << wxT(" alignment=\"") << (int) attr.GetAlignment() << wxT("\"");
1001
1002 if (attr.HasLeftIndent())
1003 {
1004 str << wxT(" leftindent=\"") << (int) attr.GetLeftIndent() << wxT("\"");
1005 str << wxT(" leftsubindent=\"") << (int) attr.GetLeftSubIndent() << wxT("\"");
1006 }
1007
1008 if (attr.HasRightIndent())
1009 str << wxT(" rightindent=\"") << (int) attr.GetRightIndent() << wxT("\"");
1010
1011 if (attr.HasParagraphSpacingAfter())
1012 str << wxT(" parspacingafter=\"") << (int) attr.GetParagraphSpacingAfter() << wxT("\"");
1013
1014 if (attr.HasParagraphSpacingBefore())
1015 str << wxT(" parspacingbefore=\"") << (int) attr.GetParagraphSpacingBefore() << wxT("\"");
1016
1017 if (attr.HasLineSpacing())
1018 str << wxT(" linespacing=\"") << (int) attr.GetLineSpacing() << wxT("\"");
1019
1020 if (attr.HasBulletStyle())
1021 str << wxT(" bulletstyle=\"") << (int) attr.GetBulletStyle() << wxT("\"");
1022
1023 if (attr.HasBulletNumber())
1024 str << wxT(" bulletnumber=\"") << (int) attr.GetBulletNumber() << wxT("\"");
1025
1026 if (attr.HasBulletText())
1027 {
1028 // If using a bullet symbol, convert to integer in case it's a non-XML-friendly character.
1029 // Otherwise, assume it's XML-friendly text such as outline numbering, e.g. 1.2.3.1
1030 if (!attr.GetBulletText().IsEmpty() && (attr.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL))
1031 str << wxT(" bulletsymbol=\"") << (int) (attr.GetBulletText()[0]) << wxT("\"");
1032 else
1033 str << wxT(" bullettext=\"") << attr.GetBulletText() << wxT("\"");
1034
1035 str << wxT(" bulletfont=\"") << attr.GetBulletFont() << wxT("\"");
1036 }
1037
1038 if (attr.HasBulletName())
1039 str << wxT(" bulletname=\"") << attr.GetBulletName() << wxT("\"");
1040
1041 if (!attr.GetParagraphStyleName().empty())
1042 str << wxT(" parstyle=\"") << wxString(attr.GetParagraphStyleName()) << wxT("\"");
1043
1044 if (!attr.GetListStyleName().empty())
1045 str << wxT(" liststyle=\"") << wxString(attr.GetListStyleName()) << wxT("\"");
1046
1047 if (attr.HasTabs())
1048 {
1049 str << wxT(" tabs=\"");
1050 size_t i;
1051 for (i = 0; i < attr.GetTabs().GetCount(); i++)
1052 {
1053 if (i > 0)
1054 str << wxT(",");
1055 str << attr.GetTabs()[i];
1056 }
1057 str << wxT("\"");
1058 }
1059
1060 if (attr.HasPageBreak())
1061 {
1062 str << wxT(" pagebreak=\"1\"");
1063 }
1064
1065 if (attr.HasOutlineLevel())
1066 str << wxT(" outlinelevel=\"") << (int) attr.GetOutlineLevel() << wxT("\"");
1067
1068 }
1069
1070 return str;
1071}
1072
1073/// Get style parameters
1074bool wxRichTextXMLHandler::GetStyle(wxTextAttr& attr, wxXmlNode* node, bool isPara)
1075{
1076 wxString fontFacename;
1077 int fontSize = 12;
1078 // int fontFamily = wxDEFAULT;
1079 int fontWeight = wxNORMAL;
1080 int fontStyle = wxNORMAL;
1081 bool fontUnderlined = false;
1082
1083 // int fontFlags = 0;
1084
1085 fontFacename = node->GetAttribute(wxT("fontface"), wxEmptyString);
1086 if (!fontFacename.IsEmpty())
1087 attr.SetFontFaceName(fontFacename);
1088
1089 wxString value;
1090 //value = node->GetAttribute(wxT("fontfamily"), wxEmptyString);
1091 //if (!value.empty())
1092 // fontFamily = wxAtoi(value);
1093
1094 value = node->GetAttribute(wxT("fontstyle"), wxEmptyString);
1095 if (!value.empty())
1096 {
1097 fontStyle = wxAtoi(value);
1098 attr.SetFontStyle(fontStyle);
1099 }
1100
1101 value = node->GetAttribute(wxT("fontsize"), wxEmptyString);
1102 if (!value.empty())
1103 {
1104 fontSize = wxAtoi(value);
1105 attr.SetFontSize(fontSize);
1106 }
1107
1108 value = node->GetAttribute(wxT("fontweight"), wxEmptyString);
1109 if (!value.empty())
1110 {
1111 fontWeight = wxAtoi(value);
1112 attr.SetFontWeight(fontWeight);
1113 }
1114
1115 value = node->GetAttribute(wxT("fontunderlined"), wxEmptyString);
1116 if (!value.empty())
1117 {
1118 fontUnderlined = wxAtoi(value) != 0;
1119 attr.SetFontUnderlined(fontUnderlined);
1120 }
1121
1122 value = node->GetAttribute(wxT("textcolor"), wxEmptyString);
1123 if (!value.empty())
1124 {
1125 if (value[0] == wxT('#'))
1126 attr.SetTextColour(HexStringToColour(value.Mid(1)));
1127 else
1128 attr.SetTextColour(value);
1129 }
1130
1131 value = node->GetAttribute(wxT("bgcolor"), wxEmptyString);
1132 if (!value.empty())
1133 {
1134 if (value[0] == wxT('#'))
1135 attr.SetBackgroundColour(HexStringToColour(value.Mid(1)));
1136 else
1137 attr.SetBackgroundColour(value);
1138 }
1139
1140 value = node->GetAttribute(wxT("characterstyle"), wxEmptyString);
1141 if (!value.empty())
1142 attr.SetCharacterStyleName(value);
1143
1144 value = node->GetAttribute(wxT("texteffects"), wxEmptyString);
1145 if (!value.IsEmpty())
1146 {
1147 attr.SetTextEffects(wxAtoi(value));
1148 }
1149
1150 value = node->GetAttribute(wxT("texteffectflags"), wxEmptyString);
1151 if (!value.IsEmpty())
1152 {
1153 attr.SetTextEffectFlags(wxAtoi(value));
1154 }
1155
1156 value = node->GetAttribute(wxT("url"), wxEmptyString);
1157 if (!value.empty())
1158 attr.SetURL(value);
1159
1160 // Set paragraph attributes
1161 if (isPara)
1162 {
1163 value = node->GetAttribute(wxT("alignment"), wxEmptyString);
1164 if (!value.empty())
1165 attr.SetAlignment((wxTextAttrAlignment) wxAtoi(value));
1166
1167 int leftSubIndent = 0;
1168 int leftIndent = 0;
1169 bool hasLeftIndent = false;
1170
1171 value = node->GetAttribute(wxT("leftindent"), wxEmptyString);
1172 if (!value.empty())
1173 {
1174 leftIndent = wxAtoi(value);
1175 hasLeftIndent = true;
1176 }
1177
1178 value = node->GetAttribute(wxT("leftsubindent"), wxEmptyString);
1179 if (!value.empty())
1180 {
1181 leftSubIndent = wxAtoi(value);
1182 hasLeftIndent = true;
1183 }
1184
1185 if (hasLeftIndent)
1186 attr.SetLeftIndent(leftIndent, leftSubIndent);
1187
1188 value = node->GetAttribute(wxT("rightindent"), wxEmptyString);
1189 if (!value.empty())
1190 attr.SetRightIndent(wxAtoi(value));
1191
1192 value = node->GetAttribute(wxT("parspacingbefore"), wxEmptyString);
1193 if (!value.empty())
1194 attr.SetParagraphSpacingBefore(wxAtoi(value));
1195
1196 value = node->GetAttribute(wxT("parspacingafter"), wxEmptyString);
1197 if (!value.empty())
1198 attr.SetParagraphSpacingAfter(wxAtoi(value));
1199
1200 value = node->GetAttribute(wxT("linespacing"), wxEmptyString);
1201 if (!value.empty())
1202 attr.SetLineSpacing(wxAtoi(value));
1203
1204 value = node->GetAttribute(wxT("bulletstyle"), wxEmptyString);
1205 if (!value.empty())
1206 attr.SetBulletStyle(wxAtoi(value));
1207
1208 value = node->GetAttribute(wxT("bulletnumber"), wxEmptyString);
1209 if (!value.empty())
1210 attr.SetBulletNumber(wxAtoi(value));
1211
1212 value = node->GetAttribute(wxT("bulletsymbol"), wxEmptyString);
1213 if (!value.empty())
1214 {
1215 wxChar ch = wxAtoi(value);
1216 wxString s;
1217 s << ch;
1218 attr.SetBulletText(s);
1219 }
1220
1221 value = node->GetAttribute(wxT("bullettext"), wxEmptyString);
1222 if (!value.empty())
1223 attr.SetBulletText(value);
1224
1225 value = node->GetAttribute(wxT("bulletfont"), wxEmptyString);
1226 if (!value.empty())
1227 attr.SetBulletFont(value);
1228
1229 value = node->GetAttribute(wxT("bulletname"), wxEmptyString);
1230 if (!value.empty())
1231 attr.SetBulletName(value);
1232
1233 value = node->GetAttribute(wxT("parstyle"), wxEmptyString);
1234 if (!value.empty())
1235 attr.SetParagraphStyleName(value);
1236
1237 value = node->GetAttribute(wxT("liststyle"), wxEmptyString);
1238 if (!value.empty())
1239 attr.SetListStyleName(value);
1240
1241 value = node->GetAttribute(wxT("tabs"), wxEmptyString);
1242 if (!value.empty())
1243 {
1244 wxArrayInt tabs;
1245 wxStringTokenizer tkz(value, wxT(","));
1246 while (tkz.HasMoreTokens())
1247 {
1248 wxString token = tkz.GetNextToken();
1249 tabs.Add(wxAtoi(token));
1250 }
1251 attr.SetTabs(tabs);
1252 }
1253
1254 value = node->GetAttribute(wxT("pagebreak"), wxEmptyString);
1255 if (!value.IsEmpty())
1256 {
1257 attr.SetPageBreak(wxAtoi(value) != 0);
1258 }
1259
1260 value = node->GetAttribute(wxT("outlinelevel"), wxEmptyString);
1261 if (!value.IsEmpty())
1262 {
1263 attr.SetOutlineLevel(wxAtoi(value));
1264 }
1265 }
1266
1267 return true;
1268}
1269
1270#endif
1271 // wxUSE_STREAMS
1272
1273#endif
1274 // wxUSE_RICHTEXT && wxUSE_XML
1275