]> git.saurik.com Git - wxWidgets.git/blob - src/richtext/richtextxml.cpp
516fb41a28433eb3a3eb8c69fb3283114f7c268a
[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 // write string to output:
261 inline static void OutputString(wxOutputStream& stream, const wxString& str,
262 wxMBConv *WXUNUSED_IN_UNICODE(convMem) = NULL, wxMBConv *convFile = NULL)
263 {
264 if (str.empty()) return;
265 #if wxUSE_UNICODE
266 const wxWX2MBbuf buf(str.mb_str(convFile ? *convFile : wxConvUTF8));
267 stream.Write((const char*)buf, strlen((const char*)buf));
268 #else
269 if ( convFile == NULL )
270 stream.Write(str.mb_str(), str.Len());
271 else
272 {
273 wxString str2(str.wc_str(*convMem), *convFile);
274 stream.Write(str2.mb_str(), str2.Len());
275 }
276 #endif
277 }
278
279 // Same as above, but create entities first.
280 // Translates '<' to "&lt;", '>' to "&gt;" and '&' to "&amp;"
281 static void OutputStringEnt(wxOutputStream& stream, const wxString& str,
282 wxMBConv *convMem = NULL, wxMBConv *convFile = NULL)
283 {
284 wxString buf;
285 size_t i, last, len;
286 wxChar c;
287
288 len = str.Len();
289 last = 0;
290 for (i = 0; i < len; i++)
291 {
292 c = str.GetChar(i);
293 if (c == wxT('<') || c == wxT('>') || c == wxT('"') ||
294 (c == wxT('&') && (str.Mid(i+1, 4) != wxT("amp;"))))
295 {
296 OutputString(stream, str.Mid(last, i - last), convMem, convFile);
297 switch (c)
298 {
299 case wxT('<'):
300 OutputString(stream, wxT("&lt;"), NULL, NULL);
301 break;
302 case wxT('>'):
303 OutputString(stream, wxT("&gt;"), NULL, NULL);
304 break;
305 case wxT('&'):
306 OutputString(stream, wxT("&amp;"), NULL, NULL);
307 break;
308 case wxT('"'):
309 OutputString(stream, wxT("&quot;"), NULL, NULL);
310 break;
311 default: break;
312 }
313 last = i + 1;
314 }
315 }
316 OutputString(stream, str.Mid(last, i - last), convMem, convFile);
317 }
318
319 inline static void OutputIndentation(wxOutputStream& stream, int indent)
320 {
321 wxString str = wxT("\n");
322 for (int i = 0; i < indent; i++)
323 str << wxT(' ') << wxT(' ');
324 OutputString(stream, str, NULL, NULL);
325 }
326
327 static wxOutputStream& operator <<(wxOutputStream& stream, const wxString& s)
328 {
329 stream.Write(s, s.Length());
330 return stream;
331 }
332
333 static wxOutputStream& operator <<(wxOutputStream& stream, long l)
334 {
335 wxString str;
336 str.Printf(wxT("%ld"), l);
337 return stream << str;
338 }
339
340 static wxOutputStream& operator <<(wxOutputStream& stream, const char c)
341 {
342 wxString str;
343 str.Printf(wxT("%c"), c);
344 return stream << str;
345 }
346
347 // Convert a colour to a 6-digit hex string
348 static wxString ColourToHexString(const wxColour& col)
349 {
350 wxString hex;
351
352 hex += wxDecToHex(col.Red());
353 hex += wxDecToHex(col.Green());
354 hex += wxDecToHex(col.Blue());
355
356 return hex;
357 }
358
359 // Convert 6-digit hex string to a colour
360 wxColour HexStringToColour(const wxString& hex)
361 {
362 unsigned char r = (unsigned char)wxHexToDec(hex.Mid(0, 2));
363 unsigned char g = (unsigned char)wxHexToDec(hex.Mid(2, 2));
364 unsigned char b = (unsigned char)wxHexToDec(hex.Mid(4, 2));
365
366 return wxColour(r, g, b);
367 }
368
369 bool wxRichTextXMLHandler::DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream)
370 {
371 if (!stream.IsOk())
372 return false;
373
374 wxString version(wxT("1.0") ) ;
375 #if wxUSE_UNICODE
376 wxString fileencoding(wxT("UTF-8")) ;
377 wxString memencoding(wxT("UTF-8")) ;
378 #else
379 wxString fileencoding(wxT("ISO-8859-1")) ;
380 wxString memencoding(wxT("ISO-8859-1")) ;
381 #endif
382 wxString s ;
383
384 wxMBConv *convMem = NULL, *convFile = NULL;
385 #if wxUSE_UNICODE
386 convFile = new wxCSConv(fileencoding);
387 #else
388 if ( fileencoding != memencoding )
389 {
390 convFile = new wxCSConv(fileencoding);
391 convMem = new wxCSConv(memencoding);
392 }
393 #endif
394
395 s.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"),
396 (const wxChar*) version, (const wxChar*) fileencoding );
397 OutputString(stream, s, NULL, NULL);
398 OutputString(stream, wxT("<richtext version=\"1.0.0.0\" xmlns=\"http://www.wxwidgets.org\">") , NULL, NULL);
399
400 int level = 1;
401 ExportXML(stream, convMem, convFile, *buffer, level);
402
403 OutputString(stream, wxT("\n</richtext>") , NULL, NULL);
404 OutputString(stream, wxT("\n"), NULL, NULL);
405
406 delete convFile;
407 delete convMem;
408
409 return true;
410 }
411
412 /// Recursively export an object
413 bool wxRichTextXMLHandler::ExportXML(wxOutputStream& stream, wxMBConv* convMem, wxMBConv* convFile, wxRichTextObject& obj, int indent)
414 {
415 wxString objectName;
416 if (obj.IsKindOf(CLASSINFO(wxRichTextParagraphLayoutBox)))
417 objectName = wxT("paragraphlayout");
418 else if (obj.IsKindOf(CLASSINFO(wxRichTextParagraph)))
419 objectName = wxT("paragraph");
420 else if (obj.IsKindOf(CLASSINFO(wxRichTextPlainText)))
421 objectName = wxT("text");
422 else if (obj.IsKindOf(CLASSINFO(wxRichTextImage)))
423 objectName = wxT("image");
424 else
425 objectName = wxT("object");
426
427 if (obj.IsKindOf(CLASSINFO(wxRichTextPlainText)))
428 {
429 wxRichTextPlainText& text = (wxRichTextPlainText&) obj;
430
431 OutputIndentation(stream, indent);
432 stream << wxT("<") << objectName;
433
434 wxString style = CreateStyle(obj.GetAttributes(), false);
435
436 stream << style << wxT(">");
437
438 wxString str = text.GetText();
439 if (str.Length() > 0 && (str[0] == wxT(' ') || str[str.Length()-1] == wxT(' ')))
440 {
441 stream << wxT("\"");
442 OutputStringEnt(stream, str, convMem, convFile);
443 stream << wxT("\"");
444 }
445 else
446 OutputStringEnt(stream, str, convMem, convFile);
447 }
448 else if (obj.IsKindOf(CLASSINFO(wxRichTextImage)))
449 {
450 wxRichTextImage& imageObj = (wxRichTextImage&) obj;
451
452 if (imageObj.GetImage().Ok() && !imageObj.GetImageBlock().Ok())
453 imageObj.MakeBlock();
454
455 OutputIndentation(stream, indent);
456 stream << wxT("<") << objectName;
457 if (!imageObj.GetImageBlock().Ok())
458 {
459 // No data
460 stream << wxT(">");
461 }
462 else
463 {
464 stream << wxString::Format(wxT(" imagetype=\"%d\""), (int) imageObj.GetImageBlock().GetImageType()) << wxT(">");
465 }
466
467 OutputIndentation(stream, indent+1);
468 stream << wxT("<data>");
469
470 imageObj.GetImageBlock().WriteHex(stream);
471
472 stream << wxT("</data>");
473 }
474 else if (obj.IsKindOf(CLASSINFO(wxRichTextCompositeObject)))
475 {
476 OutputIndentation(stream, indent);
477 stream << wxT("<") << objectName;
478
479 bool isPara = false;
480 if (objectName == wxT("paragraph") || objectName == wxT("paragraphlayout"))
481 isPara = true;
482
483 wxString style = CreateStyle(obj.GetAttributes(), isPara);
484
485 stream << style << wxT(">");
486
487 wxRichTextCompositeObject& composite = (wxRichTextCompositeObject&) obj;
488 size_t i;
489 for (i = 0; i < composite.GetChildCount(); i++)
490 {
491 wxRichTextObject* child = composite.GetChild(i);
492 ExportXML(stream, convMem, convFile, *child, indent+1);
493 }
494 }
495
496 if (objectName != wxT("text"))
497 OutputIndentation(stream, indent);
498
499 stream << wxT("</") << objectName << wxT(">");
500
501 return true;
502 }
503
504 /// Create style parameters
505 wxString wxRichTextXMLHandler::CreateStyle(const wxTextAttrEx& attr, bool isPara)
506 {
507 wxString str;
508 if (attr.GetTextColour().Ok())
509 {
510 str << wxT(" textcolor=\"#") << ColourToHexString(attr.GetTextColour()) << wxT("\"");
511 }
512 if (attr.GetBackgroundColour().Ok())
513 {
514 str << wxT(" bgcolor=\"#") << ColourToHexString(attr.GetBackgroundColour()) << wxT("\"");
515 }
516
517 if (attr.GetFont().Ok())
518 {
519 str << wxT(" fontsize=\"") << attr.GetFont().GetPointSize() << wxT("\"");
520 str << wxT(" fontfamily=\"") << attr.GetFont().GetFamily() << wxT("\"");
521 str << wxT(" fontstyle=\"") << attr.GetFont().GetStyle() << wxT("\"");
522 str << wxT(" fontweight=\"") << attr.GetFont().GetWeight() << wxT("\"");
523 str << wxT(" fontunderlined=\"") << (int) attr.GetFont().GetUnderlined() << wxT("\"");
524 str << wxT(" fontface=\"") << attr.GetFont().GetFaceName() << wxT("\"");
525 }
526
527 if (!attr.GetCharacterStyleName().empty())
528 str << wxT(" charactertyle=\"") << wxString(attr.GetCharacterStyleName()) << wxT("\"");
529
530 if (isPara)
531 {
532 str << wxT(" alignment=\"") << (int) attr.GetAlignment() << wxT("\"");
533 str << wxT(" leftindent=\"") << (int) attr.GetLeftIndent() << wxT("\"");
534 str << wxT(" leftsubindent=\"") << (int) attr.GetLeftSubIndent() << wxT("\"");
535 str << wxT(" rightindent=\"") << (int) attr.GetRightIndent() << wxT("\"");
536 str << wxT(" parspacingafter=\"") << (int) attr.GetParagraphSpacingAfter() << wxT("\"");
537 str << wxT(" parspacingbefore=\"") << (int) attr.GetParagraphSpacingBefore() << wxT("\"");
538 str << wxT(" linespacing=\"") << (int) attr.GetLineSpacing() << wxT("\"");
539 str << wxT(" bulletstyle=\"") << (int) attr.GetBulletStyle() << wxT("\"");
540 str << wxT(" bulletnumber=\"") << (int) attr.GetBulletNumber() << wxT("\"");
541 str << wxT(" bulletsymbol=\"") << wxString(attr.GetBulletSymbol()) << wxT("\"");
542
543 if (!attr.GetParagraphStyleName().empty())
544 str << wxT(" parstyle=\"") << wxString(attr.GetParagraphStyleName()) << wxT("\"");
545 }
546
547 return str;
548 }
549
550 /// Get style parameters
551 bool wxRichTextXMLHandler::GetStyle(wxTextAttrEx& attr, wxXmlNode* node, bool isPara)
552 {
553 wxString fontFacename;
554 int fontSize = 12;
555 int fontFamily = wxDEFAULT;
556 int fontWeight = wxNORMAL;
557 int fontStyle = wxNORMAL;
558 bool fontUnderlined = false;
559
560 fontFacename = node->GetPropVal(wxT("fontface"), wxEmptyString);
561
562 wxString value = node->GetPropVal(wxT("fontfamily"), wxEmptyString);
563 if (!value.empty())
564 fontFamily = wxAtoi(value);
565
566 value = node->GetPropVal(wxT("fontstyle"), wxEmptyString);
567 if (!value.empty())
568 fontStyle = wxAtoi(value);
569
570 value = node->GetPropVal(wxT("fontsize"), wxEmptyString);
571 if (!value.empty())
572 fontSize = wxAtoi(value);
573
574 value = node->GetPropVal(wxT("fontweight"), wxEmptyString);
575 if (!value.empty())
576 fontWeight = wxAtoi(value);
577
578 value = node->GetPropVal(wxT("fontunderlined"), wxEmptyString);
579 if (!value.empty())
580 fontUnderlined = wxAtoi(value) != 0;
581
582 attr.SetFont(* wxTheFontList->FindOrCreateFont(fontSize, fontFamily, fontStyle, fontWeight, fontUnderlined, fontFacename));
583
584 value = node->GetPropVal(wxT("textcolor"), wxEmptyString);
585 if (!value.empty())
586 {
587 if (value[0] == wxT('#'))
588 attr.SetTextColour(HexStringToColour(value.Mid(1)));
589 else
590 attr.SetTextColour(value);
591 }
592
593 value = node->GetPropVal(wxT("backgroundcolor"), wxEmptyString);
594 if (!value.empty())
595 {
596 if (value[0] == wxT('#'))
597 attr.SetBackgroundColour(HexStringToColour(value.Mid(1)));
598 else
599 attr.SetBackgroundColour(value);
600 }
601
602 value = node->GetPropVal(wxT("characterstyle"), wxEmptyString);
603 if (!value.empty())
604 attr.SetCharacterStyleName(value);
605
606 // Set paragraph attributes
607 if (isPara)
608 {
609 value = node->GetPropVal(wxT("alignment"), wxEmptyString);
610 if (!value.empty())
611 attr.SetAlignment((wxTextAttrAlignment) wxAtoi(value));
612
613 int leftSubIndent = 0;
614 int leftIndent = 0;
615 value = node->GetPropVal(wxT("leftindent"), wxEmptyString);
616 if (!value.empty())
617 leftIndent = wxAtoi(value);
618 value = node->GetPropVal(wxT("leftsubindent"), wxEmptyString);
619 if (!value.empty())
620 leftSubIndent = wxAtoi(value);
621 attr.SetLeftIndent(leftIndent, leftSubIndent);
622
623 value = node->GetPropVal(wxT("rightindent"), wxEmptyString);
624 if (!value.empty())
625 attr.SetRightIndent(wxAtoi(value));
626
627 value = node->GetPropVal(wxT("parspacingbefore"), wxEmptyString);
628 if (!value.empty())
629 attr.SetParagraphSpacingBefore(wxAtoi(value));
630
631 value = node->GetPropVal(wxT("parspacingafter"), wxEmptyString);
632 if (!value.empty())
633 attr.SetParagraphSpacingAfter(wxAtoi(value));
634
635 value = node->GetPropVal(wxT("linespacing"), wxEmptyString);
636 if (!value.empty())
637 attr.SetLineSpacing(wxAtoi(value));
638
639 value = node->GetPropVal(wxT("bulletstyle"), wxEmptyString);
640 if (!value.empty())
641 attr.SetBulletStyle(wxAtoi(value));
642
643 value = node->GetPropVal(wxT("bulletnumber"), wxEmptyString);
644 if (!value.empty())
645 attr.SetBulletNumber(wxAtoi(value));
646
647 value = node->GetPropVal(wxT("bulletsymbol"), wxEmptyString);
648 if (!value.empty())
649 attr.SetBulletSymbol(value[0]);
650
651 value = node->GetPropVal(wxT("parstyle"), wxEmptyString);
652 if (!value.empty())
653 attr.SetParagraphStyleName(value);
654 }
655
656 return true;
657 }
658
659 #endif
660
661 IMPLEMENT_DYNAMIC_CLASS(wxRichTextHTMLHandler, wxRichTextFileHandler)
662
663 /// Can we handle this filename (if using files)? By default, checks the extension.
664 bool wxRichTextHTMLHandler::CanHandle(const wxString& filename) const
665 {
666 wxString path, file, ext;
667 wxSplitPath(filename, & path, & file, & ext);
668
669 return (ext.Lower() == wxT("html") || ext.Lower() == wxT("htm"));
670 }
671
672
673 #if wxUSE_STREAMS
674 bool wxRichTextHTMLHandler::DoLoadFile(wxRichTextBuffer *WXUNUSED(buffer), wxInputStream& WXUNUSED(stream))
675 {
676 return false;
677 }
678
679 /*
680 * We need to output only _changes_ in character formatting.
681 */
682
683 bool wxRichTextHTMLHandler::DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream)
684 {
685 buffer->Defragment();
686
687 wxTextOutputStream str(stream);
688
689 wxTextAttrEx currentParaStyle = buffer->GetAttributes();
690 wxTextAttrEx currentCharStyle = buffer->GetAttributes();
691
692 str << wxT("<html><head></head><body>\n");
693
694 wxRichTextObjectList::compatibility_iterator node = buffer->GetChildren().GetFirst();
695 while (node)
696 {
697 wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph);
698 wxASSERT (para != NULL);
699
700 if (para)
701 {
702 OutputParagraphFormatting(currentParaStyle, para->GetAttributes(), stream, true);
703
704 wxRichTextObjectList::compatibility_iterator node2 = para->GetChildren().GetFirst();
705 while (node2)
706 {
707 wxRichTextObject* obj = node2->GetData();
708 wxRichTextPlainText* textObj = wxDynamicCast(obj, wxRichTextPlainText);
709 if (textObj && !textObj->IsEmpty())
710 {
711 OutputCharacterFormatting(currentCharStyle, obj->GetAttributes(), stream, true);
712
713 str << textObj->GetText();
714
715 OutputCharacterFormatting(currentCharStyle, obj->GetAttributes(), stream, false);
716 }
717
718 node2 = node2->GetNext();
719 }
720
721 OutputParagraphFormatting(currentParaStyle, para->GetAttributes(), stream, false);
722
723 str << wxT("<P>\n");
724 }
725
726 node = node->GetNext();
727 }
728
729 str << wxT("</body></html>\n");
730
731 return true;
732 }
733
734 /// Output character formatting
735 void wxRichTextHTMLHandler::OutputCharacterFormatting(const wxTextAttrEx& WXUNUSED(currentStyle), const wxTextAttrEx& thisStyle, wxOutputStream& stream, bool start)
736 {
737 wxTextOutputStream str(stream);
738
739 bool isBold = false;
740 bool isItalic = false;
741 bool isUnderline = false;
742 wxString faceName;
743
744 if (thisStyle.GetFont().Ok())
745 {
746 if (thisStyle.GetFont().GetWeight() == wxBOLD)
747 isBold = true;
748 if (thisStyle.GetFont().GetStyle() == wxITALIC)
749 isItalic = true;
750 if (thisStyle.GetFont().GetUnderlined())
751 isUnderline = true;
752
753 faceName = thisStyle.GetFont().GetFaceName();
754 }
755
756 if (start)
757 {
758 if (isBold)
759 str << wxT("<b>");
760 if (isItalic)
761 str << wxT("<i>");
762 if (isUnderline)
763 str << wxT("<u>");
764 }
765 else
766 {
767 if (isUnderline)
768 str << wxT("</u>");
769 if (isItalic)
770 str << wxT("</i>");
771 if (isBold)
772 str << wxT("</b>");
773 }
774 }
775
776 /// Output paragraph formatting
777 void wxRichTextHTMLHandler::OutputParagraphFormatting(const wxTextAttrEx& WXUNUSED(currentStyle), const wxTextAttrEx& thisStyle, wxOutputStream& stream, bool start)
778 {
779 // TODO: lists, indentation (using tables), fonts, right-align, ...
780
781 wxTextOutputStream str(stream);
782 bool isCentered = false;
783
784 if (thisStyle.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE)
785 {
786 isCentered = true;
787 }
788
789 if (start)
790 {
791 if (isCentered)
792 str << wxT("<center>");
793 }
794 else
795 {
796 if (isCentered)
797 str << wxT("</center>");
798 }
799 }
800
801 #endif
802
803 #endif
804 // wxUSE_RICHTEXT