]> git.saurik.com Git - wxWidgets.git/blob - src/richtext/richtextxml.cpp
Fix tracking rectangles in 64-bit build by remembering the tag as the 64-bit integer...
[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 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
255 bool 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
348 bool wxRichTextXMLHandler::HasParam(wxXmlNode* node, const wxString& param)
349 {
350 return (GetParamNode(node, param) != NULL);
351 }
352
353 wxXmlNode *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
369 wxString 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
386 wxString 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
394 wxString 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:
413 inline 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;"
441 static 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
494 inline static void OutputIndentation(wxOutputStream& stream, int indent)
495 {
496 wxString str = wxT("\n");
497 for (int i = 0; i < indent; i++)
498 str << wxT(' ') << wxT(' ');
499 OutputString(stream, str, NULL, NULL);
500 }
501
502 // Convert a colour to a 6-digit hex string
503 static wxString ColourToHexString(const wxColour& col)
504 {
505 wxString hex;
506
507 hex += wxDecToHex(col.Red());
508 hex += wxDecToHex(col.Green());
509 hex += wxDecToHex(col.Blue());
510
511 return hex;
512 }
513
514 // Convert 6-digit hex string to a colour
515 static wxColour HexStringToColour(const wxString& hex)
516 {
517 unsigned char r = (unsigned char)wxHexToDec(hex.Mid(0, 2));
518 unsigned char g = (unsigned char)wxHexToDec(hex.Mid(2, 2));
519 unsigned char b = (unsigned char)wxHexToDec(hex.Mid(4, 2));
520
521 return wxColour(r, g, b);
522 }
523
524 bool wxRichTextXMLHandler::DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream)
525 {
526 if (!stream.IsOk())
527 return false;
528
529 wxString version(wxT("1.0") ) ;
530
531 bool deleteConvFile = false;
532 wxString fileEncoding;
533 wxMBConv* convFile = NULL;
534
535 #if wxUSE_UNICODE
536 fileEncoding = wxT("UTF-8");
537 convFile = & wxConvUTF8;
538 #else
539 fileEncoding = wxT("ISO-8859-1");
540 convFile = & wxConvISO8859_1;
541 #endif
542
543 // If SetEncoding has been called, change the output encoding.
544 if (!m_encoding.empty() && m_encoding.Lower() != fileEncoding.Lower())
545 {
546 if (m_encoding == wxT("<System>"))
547 {
548 #if wxUSE_INTL
549 fileEncoding = wxLocale::GetSystemEncodingName();
550 // if !wxUSE_INTL, we fall back to UTF-8 or ISO-8859-1 below
551 #endif
552 }
553 else
554 {
555 fileEncoding = m_encoding;
556 }
557
558 // GetSystemEncodingName may not have returned a name
559 if (fileEncoding.empty())
560 #if wxUSE_UNICODE
561 fileEncoding = wxT("UTF-8");
562 #else
563 fileEncoding = wxT("ISO-8859-1");
564 #endif
565 convFile = new wxCSConv(fileEncoding);
566 deleteConvFile = true;
567 }
568
569 #if !wxUSE_UNICODE
570 wxMBConv* convMem = wxConvCurrent;
571 #else
572 wxMBConv* convMem = NULL;
573 #endif
574
575 wxString s ;
576 s.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"),
577 version, fileEncoding);
578 OutputString(stream, s, NULL, NULL);
579 OutputString(stream, wxT("<richtext version=\"1.0.0.0\" xmlns=\"http://www.wxwidgets.org\">") , NULL, NULL);
580
581 int level = 1;
582
583 if (buffer->GetStyleSheet() && (GetFlags() & wxRICHTEXT_HANDLER_INCLUDE_STYLESHEET))
584 {
585 OutputIndentation(stream, level);
586 wxString nameAndDescr;
587 if (!buffer->GetStyleSheet()->GetName().IsEmpty())
588 nameAndDescr << wxT(" name=\"") << buffer->GetStyleSheet()->GetName() << wxT("\"");
589 if (!buffer->GetStyleSheet()->GetDescription().IsEmpty())
590 nameAndDescr << wxT(" description=\"") << buffer->GetStyleSheet()->GetDescription() << wxT("\"");
591 OutputString(stream, wxString(wxT("<stylesheet")) + nameAndDescr + wxT(">"), convMem, convFile);
592
593 int i;
594
595 for (i = 0; i < (int) buffer->GetStyleSheet()->GetCharacterStyleCount(); i++)
596 {
597 wxRichTextCharacterStyleDefinition* def = buffer->GetStyleSheet()->GetCharacterStyle(i);
598 ExportStyleDefinition(stream, convMem, convFile, def, level + 1);
599 }
600
601 for (i = 0; i < (int) buffer->GetStyleSheet()->GetParagraphStyleCount(); i++)
602 {
603 wxRichTextParagraphStyleDefinition* def = buffer->GetStyleSheet()->GetParagraphStyle(i);
604 ExportStyleDefinition(stream, convMem, convFile, def, level + 1);
605 }
606
607 for (i = 0; i < (int) buffer->GetStyleSheet()->GetListStyleCount(); i++)
608 {
609 wxRichTextListStyleDefinition* def = buffer->GetStyleSheet()->GetListStyle(i);
610 ExportStyleDefinition(stream, convMem, convFile, def, level + 1);
611 }
612
613 OutputIndentation(stream, level);
614 OutputString(stream, wxT("</stylesheet>"), convMem, convFile);
615 }
616
617
618 bool success = ExportXML(stream, convMem, convFile, *buffer, level);
619
620 OutputString(stream, wxT("\n</richtext>") , NULL, NULL);
621 OutputString(stream, wxT("\n"), NULL, NULL);
622
623 if (deleteConvFile)
624 delete convFile;
625
626 return success;
627 }
628
629 /// Recursively export an object
630 bool wxRichTextXMLHandler::ExportXML(wxOutputStream& stream, wxMBConv* convMem, wxMBConv* convFile, wxRichTextObject& obj, int indent)
631 {
632 wxString objectName;
633 if (obj.IsKindOf(CLASSINFO(wxRichTextParagraphLayoutBox)))
634 objectName = wxT("paragraphlayout");
635 else if (obj.IsKindOf(CLASSINFO(wxRichTextParagraph)))
636 objectName = wxT("paragraph");
637 else if (obj.IsKindOf(CLASSINFO(wxRichTextPlainText)))
638 objectName = wxT("text");
639 else if (obj.IsKindOf(CLASSINFO(wxRichTextImage)))
640 objectName = wxT("image");
641 else
642 objectName = wxT("object");
643
644 bool terminateTag = true;
645
646 if (obj.IsKindOf(CLASSINFO(wxRichTextPlainText)))
647 {
648 wxRichTextPlainText& textObj = (wxRichTextPlainText&) obj;
649
650 wxString style = CreateStyle(obj.GetAttributes(), false);
651
652 int i;
653 int last = 0;
654 const wxString& text = textObj.GetText();
655 int len = (int) text.Length();
656 for (i = 0; i < len; i++)
657 {
658 int c = (int) text[i];
659 if (c < 32 && c != 9 && c != 10 && c != 13)
660 {
661 if (i > 0)
662 {
663 OutputIndentation(stream, indent);
664 OutputString(stream, wxT("<") + objectName, convMem, convFile);
665
666 OutputString(stream, style + wxT(">"), convMem, convFile);
667
668 wxString fragment(text.Mid(last, i-last));
669 if (!fragment.empty() && (fragment[0] == wxT(' ') || fragment[fragment.length()-1] == wxT(' ')))
670 {
671 OutputString(stream, wxT("\""), convMem, convFile);
672 OutputStringEnt(stream, fragment, convMem, convFile);
673 OutputString(stream, wxT("\""), convMem, convFile);
674 }
675 else
676 OutputStringEnt(stream, fragment, convMem, convFile);
677
678 OutputString(stream, wxT("</text>"), convMem, convFile);
679 }
680
681
682 // Output this character as a number in a separate tag, because XML can't cope
683 // with entities below 32 except for 9, 10 and 13
684 last = i + 1;
685 OutputIndentation(stream, indent);
686 OutputString(stream, wxT("<symbol"), convMem, convFile);
687
688 OutputString(stream, style + wxT(">"), convMem, convFile);
689 OutputString(stream, wxString::Format(wxT("%d"), c), convMem, convFile);
690
691 OutputString(stream, wxT("</symbol>"), convMem, convFile);
692 }
693 }
694
695 wxString fragment;
696 if (last == 0)
697 fragment = text;
698 else
699 fragment = text.Mid(last, i-last);
700
701 if (last < len)
702 {
703 OutputIndentation(stream, indent);
704 OutputString(stream, wxT("<") + objectName, convMem, convFile);
705
706 OutputString(stream, style + wxT(">"), convMem, convFile);
707
708 if (!fragment.empty() && (fragment[0] == wxT(' ') || fragment[fragment.length()-1] == wxT(' ')))
709 {
710 OutputString(stream, wxT("\""), convMem, convFile);
711 OutputStringEnt(stream, fragment, convMem, convFile);
712 OutputString(stream, wxT("\""), convMem, convFile);
713 }
714 else
715 OutputStringEnt(stream, fragment, convMem, convFile);
716 }
717 else
718 terminateTag = false;
719 }
720 else if (obj.IsKindOf(CLASSINFO(wxRichTextImage)))
721 {
722 wxRichTextImage& imageObj = (wxRichTextImage&) obj;
723
724 wxString style = CreateStyle(obj.GetAttributes(), false);
725
726 if (imageObj.GetImage().Ok() && !imageObj.GetImageBlock().Ok())
727 imageObj.MakeBlock();
728
729 OutputIndentation(stream, indent);
730 OutputString(stream, wxT("<") + objectName, convMem, convFile);
731 if (!imageObj.GetImageBlock().Ok())
732 {
733 // No data
734 OutputString(stream, style + wxT(">"), convMem, convFile);
735 }
736 else
737 {
738 OutputString(stream, wxString::Format(wxT(" imagetype=\"%d\"") + style + wxT(">"), (int) imageObj.GetImageBlock().GetImageType()));
739 }
740
741 OutputIndentation(stream, indent+1);
742 OutputString(stream, wxT("<data>"), convMem, convFile);
743
744 imageObj.GetImageBlock().WriteHex(stream);
745
746 OutputString(stream, wxT("</data>"), convMem, convFile);
747 }
748 else if (obj.IsKindOf(CLASSINFO(wxRichTextCompositeObject)))
749 {
750 OutputIndentation(stream, indent);
751 OutputString(stream, wxT("<") + objectName, convMem, convFile);
752
753 bool isPara = false;
754 if (objectName == wxT("paragraph") || objectName == wxT("paragraphlayout"))
755 isPara = true;
756
757 wxString style = CreateStyle(obj.GetAttributes(), isPara);
758
759 if (objectName == wxT("paragraphlayout") && ((wxRichTextParagraphLayoutBox&) obj).GetPartialParagraph())
760 style << wxT(" partialparagraph=\"true\"");
761
762 OutputString(stream, style + wxT(">"), convMem, convFile);
763
764 wxRichTextCompositeObject& composite = (wxRichTextCompositeObject&) obj;
765 size_t i;
766 for (i = 0; i < composite.GetChildCount(); i++)
767 {
768 wxRichTextObject* child = composite.GetChild(i);
769 ExportXML(stream, convMem, convFile, *child, indent+1);
770 }
771 }
772
773 if (objectName != wxT("text"))
774 OutputIndentation(stream, indent);
775
776 if (terminateTag)
777 OutputString(stream, wxT("</") + objectName + wxT(">"), convMem, convFile);
778
779 return true;
780 }
781
782 bool wxRichTextXMLHandler::ExportStyleDefinition(wxOutputStream& stream, wxMBConv* convMem, wxMBConv* convFile, wxRichTextStyleDefinition* def, int level)
783 {
784 wxRichTextCharacterStyleDefinition* charDef = wxDynamicCast(def, wxRichTextCharacterStyleDefinition);
785 wxRichTextParagraphStyleDefinition* paraDef = wxDynamicCast(def, wxRichTextParagraphStyleDefinition);
786 wxRichTextListStyleDefinition* listDef = wxDynamicCast(def, wxRichTextListStyleDefinition);
787
788 wxString baseStyle = def->GetBaseStyle();
789 wxString baseStyleProp;
790 if (!baseStyle.IsEmpty())
791 baseStyleProp = wxT(" basestyle=\"") + baseStyle + wxT("\"");
792
793 wxString descr = def->GetDescription();
794 wxString descrProp;
795 if (!descr.IsEmpty())
796 descrProp = wxT(" description=\"") + descr + wxT("\"");
797
798 if (charDef)
799 {
800 OutputIndentation(stream, level);
801 OutputString(stream, wxT("<characterstyle") + baseStyleProp + descrProp + wxT(">"), convMem, convFile);
802
803 level ++;
804
805 wxString style = CreateStyle(def->GetStyle(), false);
806
807 OutputIndentation(stream, level);
808 OutputString(stream, wxT("<style ") + style + wxT(">"), convMem, convFile);
809
810 OutputIndentation(stream, level);
811 OutputString(stream, wxT("</style>"), convMem, convFile);
812
813 level --;
814
815 OutputIndentation(stream, level);
816 OutputString(stream, wxT("</characterstyle>"), convMem, convFile);
817 }
818 else if (listDef)
819 {
820 OutputIndentation(stream, level);
821
822 if (!listDef->GetNextStyle().IsEmpty())
823 baseStyleProp << wxT(" basestyle=\"") << listDef->GetNextStyle() << wxT("\"");
824
825 OutputString(stream, wxT("<liststyle") + baseStyleProp + descrProp + wxT(">"), convMem, convFile);
826
827 level ++;
828
829 wxString style = CreateStyle(def->GetStyle(), false);
830
831 OutputIndentation(stream, level);
832 OutputString(stream, wxT("<style ") + style + wxT(">"), convMem, convFile);
833
834 OutputIndentation(stream, level);
835 OutputString(stream, wxT("</style>"), convMem, convFile);
836
837 int i;
838 for (i = 0; i < 10; i ++)
839 {
840 wxTextAttr* levelAttr = listDef->GetLevelAttributes(i);
841 if (levelAttr)
842 {
843 wxString style = CreateStyle(def->GetStyle(), false);
844 wxString levelStr = wxString::Format(wxT(" level=\"%d\" "), (i+1));
845
846 OutputIndentation(stream, level);
847 OutputString(stream, wxT("<style ") + levelStr + style + wxT(">"), convMem, convFile);
848
849 OutputIndentation(stream, level);
850 OutputString(stream, wxT("</style>"), convMem, convFile);
851 }
852 }
853
854 level --;
855
856 OutputIndentation(stream, level);
857 OutputString(stream, wxT("</liststyle>"), convMem, convFile);
858 }
859 else if (paraDef)
860 {
861 OutputIndentation(stream, level);
862
863 if (!paraDef->GetNextStyle().IsEmpty())
864 baseStyleProp << wxT(" basestyle=\"") << paraDef->GetNextStyle() << wxT("\"");
865
866 OutputString(stream, wxT("<paragraphstyle") + baseStyleProp + descrProp + wxT(">"), convMem, convFile);
867
868 level ++;
869
870 wxString style = CreateStyle(def->GetStyle(), false);
871
872 OutputIndentation(stream, level);
873 OutputString(stream, wxT("<style ") + style + wxT(">"), convMem, convFile);
874
875 OutputIndentation(stream, level);
876 OutputString(stream, wxT("</style>"), convMem, convFile);
877
878 level --;
879
880 OutputIndentation(stream, level);
881 OutputString(stream, wxT("</paragraphstyle>"), convMem, convFile);
882 }
883
884 return true;
885 }
886
887 /// Create style parameters
888 wxString wxRichTextXMLHandler::CreateStyle(const wxTextAttr& attr, bool isPara)
889 {
890 wxString str;
891 if (attr.HasTextColour() && attr.GetTextColour().Ok())
892 {
893 str << wxT(" textcolor=\"#") << ColourToHexString(attr.GetTextColour()) << wxT("\"");
894 }
895 if (attr.HasBackgroundColour() && attr.GetBackgroundColour().Ok())
896 {
897 str << wxT(" bgcolor=\"#") << ColourToHexString(attr.GetBackgroundColour()) << wxT("\"");
898 }
899
900 if (attr.HasFontSize())
901 str << wxT(" fontsize=\"") << attr.GetFontSize() << wxT("\"");
902
903 //if (attr.HasFontFamily())
904 // str << wxT(" fontfamily=\"") << attr.GetFont().GetFamily() << wxT("\"");
905
906 if (attr.HasFontItalic())
907 str << wxT(" fontstyle=\"") << attr.GetFontStyle() << wxT("\"");
908
909 if (attr.HasFontWeight())
910 str << wxT(" fontweight=\"") << attr.GetFontWeight() << wxT("\"");
911
912 if (attr.HasFontUnderlined())
913 str << wxT(" fontunderlined=\"") << (int) attr.GetFontUnderlined() << wxT("\"");
914
915 if (attr.HasFontFaceName())
916 str << wxT(" fontface=\"") << attr.GetFontFaceName() << wxT("\"");
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(wxTextAttr& 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 attr.SetFontFaceName(fontFacename);
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 attr.SetFontStyle(fontStyle);
1037 }
1038
1039 value = node->GetAttribute(wxT("fontsize"), wxEmptyString);
1040 if (!value.empty())
1041 {
1042 fontSize = wxAtoi(value);
1043 attr.SetFontSize(fontSize);
1044 }
1045
1046 value = node->GetAttribute(wxT("fontweight"), wxEmptyString);
1047 if (!value.empty())
1048 {
1049 fontWeight = wxAtoi(value);
1050 attr.SetFontWeight(fontWeight);
1051 }
1052
1053 value = node->GetAttribute(wxT("fontunderlined"), wxEmptyString);
1054 if (!value.empty())
1055 {
1056 fontUnderlined = wxAtoi(value) != 0;
1057 attr.SetFontUnderlined(fontUnderlined);
1058 }
1059
1060 value = node->GetAttribute(wxT("textcolor"), wxEmptyString);
1061 if (!value.empty())
1062 {
1063 if (value[0] == wxT('#'))
1064 attr.SetTextColour(HexStringToColour(value.Mid(1)));
1065 else
1066 attr.SetTextColour(value);
1067 }
1068
1069 value = node->GetAttribute(wxT("bgcolor"), wxEmptyString);
1070 if (!value.empty())
1071 {
1072 if (value[0] == wxT('#'))
1073 attr.SetBackgroundColour(HexStringToColour(value.Mid(1)));
1074 else
1075 attr.SetBackgroundColour(value);
1076 }
1077
1078 value = node->GetAttribute(wxT("characterstyle"), wxEmptyString);
1079 if (!value.empty())
1080 attr.SetCharacterStyleName(value);
1081
1082 value = node->GetAttribute(wxT("texteffects"), wxEmptyString);
1083 if (!value.IsEmpty())
1084 {
1085 attr.SetTextEffects(wxAtoi(value));
1086 }
1087
1088 value = node->GetAttribute(wxT("texteffectflags"), wxEmptyString);
1089 if (!value.IsEmpty())
1090 {
1091 attr.SetTextEffectFlags(wxAtoi(value));
1092 }
1093
1094 value = node->GetAttribute(wxT("url"), wxEmptyString);
1095 if (!value.empty())
1096 attr.SetURL(value);
1097
1098 // Set paragraph attributes
1099 if (isPara)
1100 {
1101 value = node->GetAttribute(wxT("alignment"), wxEmptyString);
1102 if (!value.empty())
1103 attr.SetAlignment((wxTextAttrAlignment) wxAtoi(value));
1104
1105 int leftSubIndent = 0;
1106 int leftIndent = 0;
1107 bool hasLeftIndent = false;
1108
1109 value = node->GetAttribute(wxT("leftindent"), wxEmptyString);
1110 if (!value.empty())
1111 {
1112 leftIndent = wxAtoi(value);
1113 hasLeftIndent = true;
1114 }
1115
1116 value = node->GetAttribute(wxT("leftsubindent"), wxEmptyString);
1117 if (!value.empty())
1118 {
1119 leftSubIndent = wxAtoi(value);
1120 hasLeftIndent = true;
1121 }
1122
1123 if (hasLeftIndent)
1124 attr.SetLeftIndent(leftIndent, leftSubIndent);
1125
1126 value = node->GetAttribute(wxT("rightindent"), wxEmptyString);
1127 if (!value.empty())
1128 attr.SetRightIndent(wxAtoi(value));
1129
1130 value = node->GetAttribute(wxT("parspacingbefore"), wxEmptyString);
1131 if (!value.empty())
1132 attr.SetParagraphSpacingBefore(wxAtoi(value));
1133
1134 value = node->GetAttribute(wxT("parspacingafter"), wxEmptyString);
1135 if (!value.empty())
1136 attr.SetParagraphSpacingAfter(wxAtoi(value));
1137
1138 value = node->GetAttribute(wxT("linespacing"), wxEmptyString);
1139 if (!value.empty())
1140 attr.SetLineSpacing(wxAtoi(value));
1141
1142 value = node->GetAttribute(wxT("bulletstyle"), wxEmptyString);
1143 if (!value.empty())
1144 attr.SetBulletStyle(wxAtoi(value));
1145
1146 value = node->GetAttribute(wxT("bulletnumber"), wxEmptyString);
1147 if (!value.empty())
1148 attr.SetBulletNumber(wxAtoi(value));
1149
1150 value = node->GetAttribute(wxT("bulletsymbol"), wxEmptyString);
1151 if (!value.empty())
1152 {
1153 wxChar ch = wxAtoi(value);
1154 wxString s;
1155 s << ch;
1156 attr.SetBulletText(s);
1157 }
1158
1159 value = node->GetAttribute(wxT("bullettext"), wxEmptyString);
1160 if (!value.empty())
1161 attr.SetBulletText(value);
1162
1163 value = node->GetAttribute(wxT("bulletfont"), wxEmptyString);
1164 if (!value.empty())
1165 attr.SetBulletFont(value);
1166
1167 value = node->GetAttribute(wxT("bulletname"), wxEmptyString);
1168 if (!value.empty())
1169 attr.SetBulletName(value);
1170
1171 value = node->GetAttribute(wxT("parstyle"), wxEmptyString);
1172 if (!value.empty())
1173 attr.SetParagraphStyleName(value);
1174
1175 value = node->GetAttribute(wxT("liststyle"), wxEmptyString);
1176 if (!value.empty())
1177 attr.SetListStyleName(value);
1178
1179 value = node->GetAttribute(wxT("tabs"), wxEmptyString);
1180 if (!value.empty())
1181 {
1182 wxArrayInt tabs;
1183 wxStringTokenizer tkz(value, wxT(","));
1184 while (tkz.HasMoreTokens())
1185 {
1186 wxString token = tkz.GetNextToken();
1187 tabs.Add(wxAtoi(token));
1188 }
1189 attr.SetTabs(tabs);
1190 }
1191
1192 value = node->GetAttribute(wxT("pagebreak"), wxEmptyString);
1193 if (!value.IsEmpty())
1194 {
1195 attr.SetPageBreak(wxAtoi(value) != 0);
1196 }
1197
1198 value = node->GetAttribute(wxT("outlinelevel"), wxEmptyString);
1199 if (!value.IsEmpty())
1200 {
1201 attr.SetOutlineLevel(wxAtoi(value));
1202 }
1203 }
1204
1205 return true;
1206 }
1207
1208 #endif
1209 // wxUSE_STREAMS
1210
1211 #endif
1212 // wxUSE_RICHTEXT && wxUSE_XML
1213