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