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