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