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