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