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