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