Efficiency measures; file loading bug fix
[wxWidgets.git] / src / richtext / richtextxml.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #endif
22
23 #include "wx/image.h"
24
25 #if wxUSE_RICHTEXT
26
27 #include "wx/filename.h"
28 #include "wx/clipbrd.h"
29 #include "wx/wfstream.h"
30 #include "wx/sstream.h"
31 #include "wx/module.h"
32 #include "wx/txtstrm.h"
33 #include "wx/xml/xml.h"
34
35 #include "wx/richtext/richtextxml.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->Clear();
46
47 wxXmlDocument* xmlDoc = new wxXmlDocument;
48 bool success = true;
49
50 if (!xmlDoc->Load(stream, wxT("ISO-8859-1")))
51 {
52 success = false;
53 }
54 else
55 {
56 if (xmlDoc->GetRoot() && xmlDoc->GetRoot()->GetType() == wxXML_ELEMENT_NODE && xmlDoc->GetRoot()->GetName() == wxT("richtext"))
57 {
58 wxXmlNode* child = xmlDoc->GetRoot()->GetChildren();
59 while (child)
60 {
61 if (child->GetType() == wxXML_ELEMENT_NODE)
62 {
63 wxString name = child->GetName();
64 if (name == wxT("richtext-version"))
65 {
66 }
67 else
68 ImportXML(buffer, child);
69 }
70
71 child = child->GetNext();
72 }
73 }
74 else
75 {
76 success = false;
77 }
78 }
79
80 delete xmlDoc;
81
82 buffer->UpdateRanges();
83
84 return success;
85 }
86
87 /// Recursively import an object
88 bool wxRichTextXMLHandler::ImportXML(wxRichTextBuffer* buffer, wxXmlNode* node)
89 {
90 wxString name = node->GetName();
91
92 bool doneChildren = false;
93
94 if (name == wxT("paragraphlayout"))
95 {
96 }
97 else if (name == wxT("paragraph"))
98 {
99 wxRichTextParagraph* para = new wxRichTextParagraph(buffer);
100 buffer->AppendChild(para);
101
102 GetStyle(para->GetAttributes(), node, true);
103
104 wxXmlNode* child = node->GetChildren();
105 while (child)
106 {
107 wxString childName = child->GetName();
108 if (childName == wxT("text"))
109 {
110 wxString text;
111 wxXmlNode* textChild = child->GetChildren();
112 while (textChild)
113 {
114 if (textChild->GetType() == wxXML_TEXT_NODE ||
115 textChild->GetType() == wxXML_CDATA_SECTION_NODE)
116 {
117 wxString text2 = textChild->GetContent();
118
119 // Strip whitespace from end
120 if (text2.Length() > 0 && text2[text2.Length()-1] == wxT('\n'))
121 text2 = text2.Mid(0, text2.Length()-1);
122
123 if (text2.Length() > 0 && text2[0] == wxT('"'))
124 text2 = text2.Mid(1);
125 if (text2.Length() > 0 && text2[text2.Length()-1] == wxT('"'))
126 text2 = text2.Mid(0, text2.Length() - 1);
127
128 // TODO: further entity translation
129 text2.Replace(wxT("&lt;"), wxT("<"));
130 text2.Replace(wxT("&gt;"), wxT(">"));
131 text2.Replace(wxT("&amp;"), wxT("&"));
132 text2.Replace(wxT("&quot;"), wxT("\""));
133
134 text += text2;
135 }
136 textChild = textChild->GetNext();
137 }
138
139 wxRichTextPlainText* textObject = new wxRichTextPlainText(text, para);
140 GetStyle(textObject->GetAttributes(), child, false);
141
142 para->AppendChild(textObject);
143 }
144 else if (childName == wxT("image"))
145 {
146 int imageType = wxBITMAP_TYPE_PNG;
147 wxString value = node->GetPropVal(wxT("imagetype"), wxEmptyString);
148 if (!value.empty())
149 imageType = wxAtoi(value);
150
151 wxString data;
152
153 wxXmlNode* imageChild = child->GetChildren();
154 while (imageChild)
155 {
156 wxString childName = imageChild->GetName();
157 if (childName == wxT("data"))
158 {
159 wxXmlNode* dataChild = imageChild->GetChildren();
160 while (dataChild)
161 {
162 data = dataChild->GetContent();
163 // wxLogDebug(data);
164 dataChild = dataChild->GetNext();
165 }
166
167 }
168 imageChild = imageChild->GetNext();
169 }
170
171 if (!data.empty())
172 {
173 wxRichTextImage* imageObj = new wxRichTextImage(para);
174 para->AppendChild(imageObj);
175
176 wxStringInputStream strStream(data);
177
178 imageObj->GetImageBlock().ReadHex(strStream, data.Length(), imageType);
179 }
180 }
181 child = child->GetNext();
182 }
183
184 doneChildren = true;
185 }
186
187 if (!doneChildren)
188 {
189 wxXmlNode* child = node->GetChildren();
190 while (child)
191 {
192 ImportXML(buffer, child);
193 child = child->GetNext();
194 }
195 }
196
197 return true;
198 }
199
200
201 //-----------------------------------------------------------------------------
202 // xml support routines
203 //-----------------------------------------------------------------------------
204
205 bool wxRichTextXMLHandler::HasParam(wxXmlNode* node, const wxString& param)
206 {
207 return (GetParamNode(node, param) != NULL);
208 }
209
210 wxXmlNode *wxRichTextXMLHandler::GetParamNode(wxXmlNode* node, const wxString& param)
211 {
212 wxCHECK_MSG(node, NULL, wxT("You can't access node data before it was initialized!"));
213
214 wxXmlNode *n = node->GetChildren();
215
216 while (n)
217 {
218 if (n->GetType() == wxXML_ELEMENT_NODE && n->GetName() == param)
219 return n;
220 n = n->GetNext();
221 }
222 return NULL;
223 }
224
225
226 wxString wxRichTextXMLHandler::GetNodeContent(wxXmlNode *node)
227 {
228 wxXmlNode *n = node;
229 if (n == NULL) return wxEmptyString;
230 n = n->GetChildren();
231
232 while (n)
233 {
234 if (n->GetType() == wxXML_TEXT_NODE ||
235 n->GetType() == wxXML_CDATA_SECTION_NODE)
236 return n->GetContent();
237 n = n->GetNext();
238 }
239 return wxEmptyString;
240 }
241
242
243 wxString wxRichTextXMLHandler::GetParamValue(wxXmlNode *node, const wxString& param)
244 {
245 if (param.empty())
246 return GetNodeContent(node);
247 else
248 return GetNodeContent(GetParamNode(node, param));
249 }
250
251 wxString wxRichTextXMLHandler::GetText(wxXmlNode *node, const wxString& param, bool WXUNUSED(translate))
252 {
253 wxXmlNode *parNode = GetParamNode(node, param);
254 if (!parNode)
255 parNode = node;
256 wxString str1(GetNodeContent(parNode));
257 return str1;
258 }
259
260 // For use with earlier versions of wxWidgets
261 #ifndef WXUNUSED_IN_UNICODE
262 #if wxUSE_UNICODE
263 #define WXUNUSED_IN_UNICODE(x) WXUNUSED(x)
264 #else
265 #define WXUNUSED_IN_UNICODE(x) x
266 #endif
267 #endif
268
269 // write string to output:
270 inline static void OutputString(wxOutputStream& stream, const wxString& str,
271 wxMBConv *WXUNUSED_IN_UNICODE(convMem) = NULL, wxMBConv *convFile = NULL)
272 {
273 if (str.empty()) return;
274 #if wxUSE_UNICODE
275 const wxWX2MBbuf buf(str.mb_str(convFile ? *convFile : wxConvUTF8));
276 stream.Write((const char*)buf, strlen((const char*)buf));
277 #else
278 if ( convFile == NULL )
279 stream.Write(str.mb_str(), str.Len());
280 else
281 {
282 wxString str2(str.wc_str(*convMem), *convFile);
283 stream.Write(str2.mb_str(), str2.Len());
284 }
285 #endif
286 }
287
288 // Same as above, but create entities first.
289 // Translates '<' to "&lt;", '>' to "&gt;" and '&' to "&amp;"
290 static void OutputStringEnt(wxOutputStream& stream, const wxString& str,
291 wxMBConv *convMem = NULL, wxMBConv *convFile = NULL)
292 {
293 wxString buf;
294 size_t i, last, len;
295 wxChar c;
296
297 len = str.Len();
298 last = 0;
299 for (i = 0; i < len; i++)
300 {
301 c = str.GetChar(i);
302 if (c == wxT('<') || c == wxT('>') || c == wxT('"') ||
303 (c == wxT('&') && (str.Mid(i+1, 4) != wxT("amp;"))))
304 {
305 OutputString(stream, str.Mid(last, i - last), convMem, convFile);
306 switch (c)
307 {
308 case wxT('<'):
309 OutputString(stream, wxT("&lt;"), NULL, NULL);
310 break;
311 case wxT('>'):
312 OutputString(stream, wxT("&gt;"), NULL, NULL);
313 break;
314 case wxT('&'):
315 OutputString(stream, wxT("&amp;"), NULL, NULL);
316 break;
317 case wxT('"'):
318 OutputString(stream, wxT("&quot;"), NULL, NULL);
319 break;
320 default: break;
321 }
322 last = i + 1;
323 }
324 }
325 OutputString(stream, str.Mid(last, i - last), convMem, convFile);
326 }
327
328 inline static void OutputIndentation(wxOutputStream& stream, int indent)
329 {
330 wxString str = wxT("\n");
331 for (int i = 0; i < indent; i++)
332 str << wxT(' ') << wxT(' ');
333 OutputString(stream, str, NULL, NULL);
334 }
335
336 static wxOutputStream& operator <<(wxOutputStream& stream, const wxString& s)
337 {
338 stream.Write(s, s.Length());
339 return stream;
340 }
341
342 static wxOutputStream& operator <<(wxOutputStream& stream, long l)
343 {
344 wxString str;
345 str.Printf(wxT("%ld"), l);
346 return stream << str;
347 }
348
349 static wxOutputStream& operator <<(wxOutputStream& stream, const char c)
350 {
351 wxString str;
352 str.Printf(wxT("%c"), c);
353 return stream << str;
354 }
355
356 // Convert a colour to a 6-digit hex string
357 static wxString ColourToHexString(const wxColour& col)
358 {
359 wxString hex;
360
361 hex += wxDecToHex(col.Red());
362 hex += wxDecToHex(col.Green());
363 hex += wxDecToHex(col.Blue());
364
365 return hex;
366 }
367
368 // Convert 6-digit hex string to a colour
369 wxColour HexStringToColour(const wxString& hex)
370 {
371 unsigned char r = (unsigned char)wxHexToDec(hex.Mid(0, 2));
372 unsigned char g = (unsigned char)wxHexToDec(hex.Mid(2, 2));
373 unsigned char b = (unsigned char)wxHexToDec(hex.Mid(4, 2));
374
375 return wxColour(r, g, b);
376 }
377
378 bool wxRichTextXMLHandler::DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream)
379 {
380 if (!stream.IsOk())
381 return false;
382
383 wxString version(wxT("1.0") ) ;
384 #if wxUSE_UNICODE
385 wxString fileencoding(wxT("UTF-8")) ;
386 wxString memencoding(wxT("UTF-8")) ;
387 #else
388 wxString fileencoding(wxT("ISO-8859-1")) ;
389 wxString memencoding(wxT("ISO-8859-1")) ;
390 #endif
391 wxString s ;
392
393 wxMBConv *convMem = NULL, *convFile = NULL;
394 #if wxUSE_UNICODE
395 convFile = new wxCSConv(fileencoding);
396 #else
397 if ( fileencoding != memencoding )
398 {
399 convFile = new wxCSConv(fileencoding);
400 convMem = new wxCSConv(memencoding);
401 }
402 #endif
403
404 s.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"),
405 (const wxChar*) version, (const wxChar*) fileencoding );
406 OutputString(stream, s, NULL, NULL);
407 OutputString(stream, wxT("<richtext version=\"1.0.0.0\" xmlns=\"http://www.wxwidgets.org\">") , NULL, NULL);
408
409 int level = 1;
410 ExportXML(stream, convMem, convFile, *buffer, level);
411
412 OutputString(stream, wxT("\n</richtext>") , NULL, NULL);
413 OutputString(stream, wxT("\n"), NULL, NULL);
414
415 delete convFile;
416 delete convMem;
417
418 return true;
419 }
420
421 /// Recursively export an object
422 bool wxRichTextXMLHandler::ExportXML(wxOutputStream& stream, wxMBConv* convMem, wxMBConv* convFile, wxRichTextObject& obj, int indent)
423 {
424 wxString objectName;
425 if (obj.IsKindOf(CLASSINFO(wxRichTextParagraphLayoutBox)))
426 objectName = wxT("paragraphlayout");
427 else if (obj.IsKindOf(CLASSINFO(wxRichTextParagraph)))
428 objectName = wxT("paragraph");
429 else if (obj.IsKindOf(CLASSINFO(wxRichTextPlainText)))
430 objectName = wxT("text");
431 else if (obj.IsKindOf(CLASSINFO(wxRichTextImage)))
432 objectName = wxT("image");
433 else
434 objectName = wxT("object");
435
436 if (obj.IsKindOf(CLASSINFO(wxRichTextPlainText)))
437 {
438 wxRichTextPlainText& text = (wxRichTextPlainText&) obj;
439
440 OutputIndentation(stream, indent);
441 stream << wxT("<") << objectName;
442
443 wxString style = CreateStyle(obj.GetAttributes(), false);
444
445 stream << style << wxT(">");
446
447 wxString str = text.GetText();
448 if (str.Length() > 0 && (str[0] == wxT(' ') || str[str.Length()-1] == wxT(' ')))
449 {
450 stream << wxT("\"");
451 OutputStringEnt(stream, str, convMem, convFile);
452 stream << wxT("\"");
453 }
454 else
455 OutputStringEnt(stream, str, convMem, convFile);
456 }
457 else if (obj.IsKindOf(CLASSINFO(wxRichTextImage)))
458 {
459 wxRichTextImage& imageObj = (wxRichTextImage&) obj;
460
461 if (imageObj.GetImage().Ok() && !imageObj.GetImageBlock().Ok())
462 imageObj.MakeBlock();
463
464 OutputIndentation(stream, indent);
465 stream << wxT("<") << objectName;
466 if (!imageObj.GetImageBlock().Ok())
467 {
468 // No data
469 stream << wxT(">");
470 }
471 else
472 {
473 stream << wxString::Format(wxT(" imagetype=\"%d\""), (int) imageObj.GetImageBlock().GetImageType()) << wxT(">");
474 }
475
476 OutputIndentation(stream, indent+1);
477 stream << wxT("<data>");
478
479 imageObj.GetImageBlock().WriteHex(stream);
480
481 stream << wxT("</data>");
482 }
483 else if (obj.IsKindOf(CLASSINFO(wxRichTextCompositeObject)))
484 {
485 OutputIndentation(stream, indent);
486 stream << wxT("<") << objectName;
487
488 bool isPara = false;
489 if (objectName == wxT("paragraph") || objectName == wxT("paragraphlayout"))
490 isPara = true;
491
492 wxString style = CreateStyle(obj.GetAttributes(), isPara);
493
494 stream << style << wxT(">");
495
496 wxRichTextCompositeObject& composite = (wxRichTextCompositeObject&) obj;
497 size_t i;
498 for (i = 0; i < composite.GetChildCount(); i++)
499 {
500 wxRichTextObject* child = composite.GetChild(i);
501 ExportXML(stream, convMem, convFile, *child, indent+1);
502 }
503 }
504
505 if (objectName != wxT("text"))
506 OutputIndentation(stream, indent);
507
508 stream << wxT("</") << objectName << wxT(">");
509
510 return true;
511 }
512
513 /// Create style parameters
514 wxString wxRichTextXMLHandler::CreateStyle(const wxTextAttrEx& attr, bool isPara)
515 {
516 wxString str;
517 if (attr.GetTextColour().Ok())
518 {
519 str << wxT(" textcolor=\"#") << ColourToHexString(attr.GetTextColour()) << wxT("\"");
520 }
521 if (attr.GetBackgroundColour().Ok())
522 {
523 str << wxT(" bgcolor=\"#") << ColourToHexString(attr.GetBackgroundColour()) << wxT("\"");
524 }
525
526 if (attr.GetFont().Ok())
527 {
528 str << wxT(" fontsize=\"") << attr.GetFont().GetPointSize() << wxT("\"");
529 str << wxT(" fontfamily=\"") << attr.GetFont().GetFamily() << wxT("\"");
530 str << wxT(" fontstyle=\"") << attr.GetFont().GetStyle() << wxT("\"");
531 str << wxT(" fontweight=\"") << attr.GetFont().GetWeight() << wxT("\"");
532 str << wxT(" fontunderlined=\"") << (int) attr.GetFont().GetUnderlined() << wxT("\"");
533 str << wxT(" fontface=\"") << attr.GetFont().GetFaceName() << wxT("\"");
534 }
535
536 if (!attr.GetCharacterStyleName().empty())
537 str << wxT(" charactertyle=\"") << wxString(attr.GetCharacterStyleName()) << wxT("\"");
538
539 if (isPara)
540 {
541 str << wxT(" alignment=\"") << (int) attr.GetAlignment() << wxT("\"");
542 str << wxT(" leftindent=\"") << (int) attr.GetLeftIndent() << wxT("\"");
543 str << wxT(" leftsubindent=\"") << (int) attr.GetLeftSubIndent() << wxT("\"");
544 str << wxT(" rightindent=\"") << (int) attr.GetRightIndent() << wxT("\"");
545 str << wxT(" parspacingafter=\"") << (int) attr.GetParagraphSpacingAfter() << wxT("\"");
546 str << wxT(" parspacingbefore=\"") << (int) attr.GetParagraphSpacingBefore() << wxT("\"");
547 str << wxT(" linespacing=\"") << (int) attr.GetLineSpacing() << wxT("\"");
548 str << wxT(" bulletstyle=\"") << (int) attr.GetBulletStyle() << wxT("\"");
549 str << wxT(" bulletnumber=\"") << (int) attr.GetBulletNumber() << wxT("\"");
550 str << wxT(" bulletsymbol=\"") << wxString(attr.GetBulletSymbol()) << wxT("\"");
551
552 if (!attr.GetParagraphStyleName().empty())
553 str << wxT(" parstyle=\"") << wxString(attr.GetParagraphStyleName()) << wxT("\"");
554 }
555
556 return str;
557 }
558
559 /// Get style parameters
560 bool wxRichTextXMLHandler::GetStyle(wxTextAttrEx& attr, wxXmlNode* node, bool isPara)
561 {
562 wxString fontFacename;
563 int fontSize = 12;
564 int fontFamily = wxDEFAULT;
565 int fontWeight = wxNORMAL;
566 int fontStyle = wxNORMAL;
567 bool fontUnderlined = false;
568
569 fontFacename = node->GetPropVal(wxT("fontface"), wxEmptyString);
570
571 wxString value = node->GetPropVal(wxT("fontfamily"), wxEmptyString);
572 if (!value.empty())
573 fontFamily = wxAtoi(value);
574
575 value = node->GetPropVal(wxT("fontstyle"), wxEmptyString);
576 if (!value.empty())
577 fontStyle = wxAtoi(value);
578
579 value = node->GetPropVal(wxT("fontsize"), wxEmptyString);
580 if (!value.empty())
581 fontSize = wxAtoi(value);
582
583 value = node->GetPropVal(wxT("fontweight"), wxEmptyString);
584 if (!value.empty())
585 fontWeight = wxAtoi(value);
586
587 value = node->GetPropVal(wxT("fontunderlined"), wxEmptyString);
588 if (!value.empty())
589 fontUnderlined = wxAtoi(value) != 0;
590
591 attr.SetFont(* wxTheFontList->FindOrCreateFont(fontSize, fontFamily, fontStyle, fontWeight, fontUnderlined, fontFacename));
592
593 value = node->GetPropVal(wxT("textcolor"), wxEmptyString);
594 if (!value.empty())
595 {
596 if (value[0] == wxT('#'))
597 attr.SetTextColour(HexStringToColour(value.Mid(1)));
598 else
599 attr.SetTextColour(value);
600 }
601
602 value = node->GetPropVal(wxT("backgroundcolor"), wxEmptyString);
603 if (!value.empty())
604 {
605 if (value[0] == wxT('#'))
606 attr.SetBackgroundColour(HexStringToColour(value.Mid(1)));
607 else
608 attr.SetBackgroundColour(value);
609 }
610
611 value = node->GetPropVal(wxT("characterstyle"), wxEmptyString);
612 if (!value.empty())
613 attr.SetCharacterStyleName(value);
614
615 // Set paragraph attributes
616 if (isPara)
617 {
618 value = node->GetPropVal(wxT("alignment"), wxEmptyString);
619 if (!value.empty())
620 attr.SetAlignment((wxTextAttrAlignment) wxAtoi(value));
621
622 int leftSubIndent = 0;
623 int leftIndent = 0;
624 value = node->GetPropVal(wxT("leftindent"), wxEmptyString);
625 if (!value.empty())
626 leftIndent = wxAtoi(value);
627 value = node->GetPropVal(wxT("leftsubindent"), wxEmptyString);
628 if (!value.empty())
629 leftSubIndent = wxAtoi(value);
630 attr.SetLeftIndent(leftIndent, leftSubIndent);
631
632 value = node->GetPropVal(wxT("rightindent"), wxEmptyString);
633 if (!value.empty())
634 attr.SetRightIndent(wxAtoi(value));
635
636 value = node->GetPropVal(wxT("parspacingbefore"), wxEmptyString);
637 if (!value.empty())
638 attr.SetParagraphSpacingBefore(wxAtoi(value));
639
640 value = node->GetPropVal(wxT("parspacingafter"), wxEmptyString);
641 if (!value.empty())
642 attr.SetParagraphSpacingAfter(wxAtoi(value));
643
644 value = node->GetPropVal(wxT("linespacing"), wxEmptyString);
645 if (!value.empty())
646 attr.SetLineSpacing(wxAtoi(value));
647
648 value = node->GetPropVal(wxT("bulletstyle"), wxEmptyString);
649 if (!value.empty())
650 attr.SetBulletStyle(wxAtoi(value));
651
652 value = node->GetPropVal(wxT("bulletnumber"), wxEmptyString);
653 if (!value.empty())
654 attr.SetBulletNumber(wxAtoi(value));
655
656 value = node->GetPropVal(wxT("bulletsymbol"), wxEmptyString);
657 if (!value.empty())
658 attr.SetBulletSymbol(value[0]);
659
660 value = node->GetPropVal(wxT("parstyle"), wxEmptyString);
661 if (!value.empty())
662 attr.SetParagraphStyleName(value);
663 }
664
665 return true;
666 }
667
668 #endif
669
670 IMPLEMENT_DYNAMIC_CLASS(wxRichTextHTMLHandler, wxRichTextFileHandler)
671
672 /// Can we handle this filename (if using files)? By default, checks the extension.
673 bool wxRichTextHTMLHandler::CanHandle(const wxString& filename) const
674 {
675 wxString path, file, ext;
676 wxSplitPath(filename, & path, & file, & ext);
677
678 return (ext.Lower() == wxT("html") || ext.Lower() == wxT("htm"));
679 }
680
681
682 #if wxUSE_STREAMS
683 bool wxRichTextHTMLHandler::DoLoadFile(wxRichTextBuffer *WXUNUSED(buffer), wxInputStream& WXUNUSED(stream))
684 {
685 return false;
686 }
687
688 /*
689 * We need to output only _changes_ in character formatting.
690 */
691
692 bool wxRichTextHTMLHandler::DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream)
693 {
694 buffer->Defragment();
695
696 wxTextOutputStream str(stream);
697
698 wxTextAttrEx currentParaStyle = buffer->GetAttributes();
699 wxTextAttrEx currentCharStyle = buffer->GetAttributes();
700
701 str << wxT("<html><head></head><body>\n");
702
703 wxRichTextObjectList::compatibility_iterator node = buffer->GetChildren().GetFirst();
704 while (node)
705 {
706 wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph);
707 wxASSERT (para != NULL);
708
709 if (para)
710 {
711 OutputParagraphFormatting(currentParaStyle, para->GetAttributes(), stream, true);
712
713 wxRichTextObjectList::compatibility_iterator node2 = para->GetChildren().GetFirst();
714 while (node2)
715 {
716 wxRichTextObject* obj = node2->GetData();
717 wxRichTextPlainText* textObj = wxDynamicCast(obj, wxRichTextPlainText);
718 if (textObj && !textObj->IsEmpty())
719 {
720 OutputCharacterFormatting(currentCharStyle, obj->GetAttributes(), stream, true);
721
722 str << textObj->GetText();
723
724 OutputCharacterFormatting(currentCharStyle, obj->GetAttributes(), stream, false);
725 }
726
727 node2 = node2->GetNext();
728 }
729
730 OutputParagraphFormatting(currentParaStyle, para->GetAttributes(), stream, false);
731
732 str << wxT("<P>\n");
733 }
734
735 node = node->GetNext();
736 }
737
738 str << wxT("</body></html>\n");
739
740 return true;
741 }
742
743 /// Output character formatting
744 void wxRichTextHTMLHandler::OutputCharacterFormatting(const wxTextAttrEx& WXUNUSED(currentStyle), const wxTextAttrEx& thisStyle, wxOutputStream& stream, bool start)
745 {
746 wxTextOutputStream str(stream);
747
748 bool isBold = false;
749 bool isItalic = false;
750 bool isUnderline = false;
751 wxString faceName;
752
753 if (thisStyle.GetFont().Ok())
754 {
755 if (thisStyle.GetFont().GetWeight() == wxBOLD)
756 isBold = true;
757 if (thisStyle.GetFont().GetStyle() == wxITALIC)
758 isItalic = true;
759 if (thisStyle.GetFont().GetUnderlined())
760 isUnderline = true;
761
762 faceName = thisStyle.GetFont().GetFaceName();
763 }
764
765 if (start)
766 {
767 if (isBold)
768 str << wxT("<b>");
769 if (isItalic)
770 str << wxT("<i>");
771 if (isUnderline)
772 str << wxT("<u>");
773 }
774 else
775 {
776 if (isUnderline)
777 str << wxT("</u>");
778 if (isItalic)
779 str << wxT("</i>");
780 if (isBold)
781 str << wxT("</b>");
782 }
783 }
784
785 /// Output paragraph formatting
786 void wxRichTextHTMLHandler::OutputParagraphFormatting(const wxTextAttrEx& WXUNUSED(currentStyle), const wxTextAttrEx& thisStyle, wxOutputStream& stream, bool start)
787 {
788 // TODO: lists, indentation (using tables), fonts, right-align, ...
789
790 wxTextOutputStream str(stream);
791 bool isCentered = false;
792
793 if (thisStyle.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE)
794 {
795 isCentered = true;
796 }
797
798 if (start)
799 {
800 if (isCentered)
801 str << wxT("<center>");
802 }
803 else
804 {
805 if (isCentered)
806 str << wxT("</center>");
807 }
808 }
809
810 #endif
811
812 #endif
813 // wxUSE_RICHTEXT