]> git.saurik.com Git - wxWidgets.git/blame - src/richtext/richtextxml.cpp
Regenerated configure for wxUSE_RICHTEXT
[wxWidgets.git] / src / richtext / richtextxml.cpp
CommitLineData
5d7836c4 1/////////////////////////////////////////////////////////////////////////////
7fe8059f 2// Name: richtext/richtextxml.cpp
5d7836c4
JS
3// Purpose: XML and HTML I/O for wxRichTextCtrl
4// Author: Julian Smart
7fe8059f 5// Modified by:
5d7836c4 6// Created: 2005-09-30
7fe8059f 7// RCS-ID: $Id$
5d7836c4
JS
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
37IMPLEMENT_DYNAMIC_CLASS(wxRichTextXMLHandler, wxRichTextFileHandler)
38
39#if wxUSE_STREAMS
7fe8059f 40bool wxRichTextXMLHandler::DoLoadFile(wxRichTextBuffer *buffer, wxInputStream& stream)
5d7836c4
JS
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 }
7fe8059f 70
5d7836c4
JS
71 child = child->GetNext();
72 }
73 }
74 else
75 {
76 success = false;
77 }
78 }
7fe8059f 79
5d7836c4
JS
80 delete xmlDoc;
81
82 buffer->UpdateRanges();
83
84 return success;
85}
86
87/// Recursively import an object
88bool 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);
7fe8059f 148 if (!value.empty())
5d7836c4
JS
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 }
7fe8059f 166
5d7836c4
JS
167 }
168 imageChild = imageChild->GetNext();
169 }
170
7fe8059f 171 if (!data.empty())
5d7836c4
JS
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 }
7fe8059f 195 }
5d7836c4
JS
196
197 return true;
198}
199
200
201//-----------------------------------------------------------------------------
202// xml support routines
203//-----------------------------------------------------------------------------
204
205bool wxRichTextXMLHandler::HasParam(wxXmlNode* node, const wxString& param)
206{
207 return (GetParamNode(node, param) != NULL);
208}
209
210wxXmlNode *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
226wxString 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
243wxString wxRichTextXMLHandler::GetParamValue(wxXmlNode *node, const wxString& param)
244{
7fe8059f 245 if (param.empty())
5d7836c4
JS
246 return GetNodeContent(node);
247 else
248 return GetNodeContent(GetParamNode(node, param));
249}
250
251wxString 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
1e967276
JS
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
5d7836c4
JS
269// write string to output:
270inline static void OutputString(wxOutputStream& stream, const wxString& str,
7fe8059f 271 wxMBConv *WXUNUSED_IN_UNICODE(convMem) = NULL, wxMBConv *convFile = NULL)
5d7836c4 272{
7fe8059f 273 if (str.empty()) return;
5d7836c4
JS
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;"
290static 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;
7fe8059f 296
5d7836c4
JS
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
328inline 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
336static wxOutputStream& operator <<(wxOutputStream& stream, const wxString& s)
337{
338 stream.Write(s, s.Length());
339 return stream;
340}
341
342static wxOutputStream& operator <<(wxOutputStream& stream, long l)
343{
344 wxString str;
345 str.Printf(wxT("%ld"), l);
346 return stream << str;
347}
348
349static 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
357static 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
369wxColour HexStringToColour(const wxString& hex)
370{
7fe8059f
WS
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));
5d7836c4
JS
374
375 return wxColour(r, g, b);
376}
377
7fe8059f 378bool wxRichTextXMLHandler::DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream)
5d7836c4
JS
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 ;
7fe8059f 392
5d7836c4
JS
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
7fe8059f 403
5d7836c4
JS
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);
7fe8059f 414
5d7836c4
JS
415 delete convFile;
416 delete convMem;
417
418 return true;
419}
420
421/// Recursively export an object
422bool 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;
7fe8059f 442
5d7836c4 443 wxString style = CreateStyle(obj.GetAttributes(), false);
7fe8059f 444
5d7836c4 445 stream << style << wxT(">");
7fe8059f 446
5d7836c4
JS
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;
7fe8059f 487
5d7836c4
JS
488 bool isPara = false;
489 if (objectName == wxT("paragraph") || objectName == wxT("paragraphlayout"))
490 isPara = true;
491
492 wxString style = CreateStyle(obj.GetAttributes(), isPara);
7fe8059f 493
5d7836c4 494 stream << style << wxT(">");
7fe8059f 495
5d7836c4
JS
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
514wxString 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
7fe8059f 536 if (!attr.GetCharacterStyleName().empty())
5d7836c4
JS
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
7fe8059f 552 if (!attr.GetParagraphStyleName().empty())
5d7836c4
JS
553 str << wxT(" parstyle=\"") << wxString(attr.GetParagraphStyleName()) << wxT("\"");
554 }
555
556 return str;
557}
558
559/// Get style parameters
560bool 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);
7fe8059f 572 if (!value.empty())
5d7836c4
JS
573 fontFamily = wxAtoi(value);
574
575 value = node->GetPropVal(wxT("fontstyle"), wxEmptyString);
7fe8059f 576 if (!value.empty())
5d7836c4
JS
577 fontStyle = wxAtoi(value);
578
579 value = node->GetPropVal(wxT("fontsize"), wxEmptyString);
7fe8059f 580 if (!value.empty())
5d7836c4
JS
581 fontSize = wxAtoi(value);
582
583 value = node->GetPropVal(wxT("fontweight"), wxEmptyString);
7fe8059f 584 if (!value.empty())
5d7836c4
JS
585 fontWeight = wxAtoi(value);
586
587 value = node->GetPropVal(wxT("fontunderlined"), wxEmptyString);
7fe8059f 588 if (!value.empty())
5d7836c4
JS
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);
7fe8059f 594 if (!value.empty())
5d7836c4
JS
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);
7fe8059f 603 if (!value.empty())
5d7836c4
JS
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);
7fe8059f 612 if (!value.empty())
5d7836c4
JS
613 attr.SetCharacterStyleName(value);
614
615 // Set paragraph attributes
616 if (isPara)
617 {
618 value = node->GetPropVal(wxT("alignment"), wxEmptyString);
7fe8059f 619 if (!value.empty())
5d7836c4
JS
620 attr.SetAlignment((wxTextAttrAlignment) wxAtoi(value));
621
622 int leftSubIndent = 0;
623 int leftIndent = 0;
624 value = node->GetPropVal(wxT("leftindent"), wxEmptyString);
7fe8059f 625 if (!value.empty())
5d7836c4
JS
626 leftIndent = wxAtoi(value);
627 value = node->GetPropVal(wxT("leftsubindent"), wxEmptyString);
7fe8059f 628 if (!value.empty())
5d7836c4
JS
629 leftSubIndent = wxAtoi(value);
630 attr.SetLeftIndent(leftIndent, leftSubIndent);
631
632 value = node->GetPropVal(wxT("rightindent"), wxEmptyString);
7fe8059f 633 if (!value.empty())
5d7836c4
JS
634 attr.SetRightIndent(wxAtoi(value));
635
636 value = node->GetPropVal(wxT("parspacingbefore"), wxEmptyString);
7fe8059f 637 if (!value.empty())
5d7836c4
JS
638 attr.SetParagraphSpacingBefore(wxAtoi(value));
639
640 value = node->GetPropVal(wxT("parspacingafter"), wxEmptyString);
7fe8059f 641 if (!value.empty())
5d7836c4
JS
642 attr.SetParagraphSpacingAfter(wxAtoi(value));
643
644 value = node->GetPropVal(wxT("linespacing"), wxEmptyString);
7fe8059f 645 if (!value.empty())
5d7836c4
JS
646 attr.SetLineSpacing(wxAtoi(value));
647
648 value = node->GetPropVal(wxT("bulletstyle"), wxEmptyString);
7fe8059f 649 if (!value.empty())
5d7836c4
JS
650 attr.SetBulletStyle(wxAtoi(value));
651
652 value = node->GetPropVal(wxT("bulletnumber"), wxEmptyString);
7fe8059f 653 if (!value.empty())
5d7836c4
JS
654 attr.SetBulletNumber(wxAtoi(value));
655
656 value = node->GetPropVal(wxT("bulletsymbol"), wxEmptyString);
7fe8059f 657 if (!value.empty())
5d7836c4
JS
658 attr.SetBulletSymbol(value[0]);
659
660 value = node->GetPropVal(wxT("parstyle"), wxEmptyString);
7fe8059f 661 if (!value.empty())
5d7836c4
JS
662 attr.SetParagraphStyleName(value);
663 }
664
665 return true;
666}
667
668#endif
669
670IMPLEMENT_DYNAMIC_CLASS(wxRichTextHTMLHandler, wxRichTextFileHandler)
671
672/// Can we handle this filename (if using files)? By default, checks the extension.
673bool 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
7fe8059f 683bool wxRichTextHTMLHandler::DoLoadFile(wxRichTextBuffer *WXUNUSED(buffer), wxInputStream& WXUNUSED(stream))
5d7836c4
JS
684{
685 return false;
686}
687
688/*
689 * We need to output only _changes_ in character formatting.
690 */
691
7fe8059f 692bool wxRichTextHTMLHandler::DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream)
5d7836c4
JS
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();
7fe8059f 728 }
5d7836c4
JS
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
744void 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
786void 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