]>
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 | ||
494 | inline static void OutputIndentation(wxOutputStream& stream, int indent) | |
495 | { | |
496 | wxString str = wxT("\n"); | |
497 | for (int i = 0; i < indent; i++) | |
498 | str << wxT(' ') << wxT(' '); | |
499 | OutputString(stream, str, NULL, NULL); | |
500 | } | |
501 | ||
5d7836c4 JS |
502 | // Convert a colour to a 6-digit hex string |
503 | static wxString ColourToHexString(const wxColour& col) | |
504 | { | |
505 | wxString hex; | |
506 | ||
507 | hex += wxDecToHex(col.Red()); | |
508 | hex += wxDecToHex(col.Green()); | |
509 | hex += wxDecToHex(col.Blue()); | |
510 | ||
511 | return hex; | |
512 | } | |
513 | ||
514 | // Convert 6-digit hex string to a colour | |
b71e9aa4 | 515 | static wxColour HexStringToColour(const wxString& hex) |
5d7836c4 | 516 | { |
7fe8059f WS |
517 | unsigned char r = (unsigned char)wxHexToDec(hex.Mid(0, 2)); |
518 | unsigned char g = (unsigned char)wxHexToDec(hex.Mid(2, 2)); | |
519 | unsigned char b = (unsigned char)wxHexToDec(hex.Mid(4, 2)); | |
5d7836c4 JS |
520 | |
521 | return wxColour(r, g, b); | |
522 | } | |
523 | ||
7fe8059f | 524 | bool wxRichTextXMLHandler::DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream) |
5d7836c4 JS |
525 | { |
526 | if (!stream.IsOk()) | |
527 | return false; | |
528 | ||
529 | wxString version(wxT("1.0") ) ; | |
b71e9aa4 JS |
530 | |
531 | bool deleteConvFile = false; | |
532 | wxString fileEncoding; | |
533 | wxMBConv* convFile = NULL; | |
534 | ||
5d7836c4 | 535 | #if wxUSE_UNICODE |
b71e9aa4 JS |
536 | fileEncoding = wxT("UTF-8"); |
537 | convFile = & wxConvUTF8; | |
5d7836c4 | 538 | #else |
b71e9aa4 JS |
539 | fileEncoding = wxT("ISO-8859-1"); |
540 | convFile = & wxConvISO8859_1; | |
5d7836c4 | 541 | #endif |
7fe8059f | 542 | |
b71e9aa4 | 543 | // If SetEncoding has been called, change the output encoding. |
88a7a4e1 | 544 | if (!m_encoding.empty() && m_encoding.Lower() != fileEncoding.Lower()) |
b71e9aa4 JS |
545 | { |
546 | if (m_encoding == wxT("<System>")) | |
547 | { | |
4aae00c6 | 548 | #if wxUSE_INTL |
b71e9aa4 | 549 | fileEncoding = wxLocale::GetSystemEncodingName(); |
4aae00c6 VS |
550 | // if !wxUSE_INTL, we fall back to UTF-8 or ISO-8859-1 below |
551 | #endif | |
b71e9aa4 JS |
552 | } |
553 | else | |
554 | { | |
555 | fileEncoding = m_encoding; | |
556 | } | |
557 | ||
558 | // GetSystemEncodingName may not have returned a name | |
88a7a4e1 | 559 | if (fileEncoding.empty()) |
5d7836c4 | 560 | #if wxUSE_UNICODE |
b71e9aa4 | 561 | fileEncoding = wxT("UTF-8"); |
5d7836c4 | 562 | #else |
b71e9aa4 JS |
563 | fileEncoding = wxT("ISO-8859-1"); |
564 | #endif | |
565 | convFile = new wxCSConv(fileEncoding); | |
566 | deleteConvFile = true; | |
5d7836c4 | 567 | } |
b71e9aa4 JS |
568 | |
569 | #if !wxUSE_UNICODE | |
570 | wxMBConv* convMem = wxConvCurrent; | |
571 | #else | |
572 | wxMBConv* convMem = NULL; | |
5d7836c4 | 573 | #endif |
7fe8059f | 574 | |
b71e9aa4 | 575 | wxString s ; |
5d7836c4 | 576 | s.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"), |
86501081 | 577 | version, fileEncoding); |
5d7836c4 JS |
578 | OutputString(stream, s, NULL, NULL); |
579 | OutputString(stream, wxT("<richtext version=\"1.0.0.0\" xmlns=\"http://www.wxwidgets.org\">") , NULL, NULL); | |
580 | ||
581 | int level = 1; | |
5d7836c4 | 582 | |
d2d0adc7 JS |
583 | if (buffer->GetStyleSheet() && (GetFlags() & wxRICHTEXT_HANDLER_INCLUDE_STYLESHEET)) |
584 | { | |
585 | OutputIndentation(stream, level); | |
42688aea JS |
586 | wxString nameAndDescr; |
587 | if (!buffer->GetStyleSheet()->GetName().IsEmpty()) | |
588 | nameAndDescr << wxT(" name=\"") << buffer->GetStyleSheet()->GetName() << wxT("\""); | |
589 | if (!buffer->GetStyleSheet()->GetDescription().IsEmpty()) | |
590 | nameAndDescr << wxT(" description=\"") << buffer->GetStyleSheet()->GetDescription() << wxT("\""); | |
591 | OutputString(stream, wxString(wxT("<stylesheet")) + nameAndDescr + wxT(">"), convMem, convFile); | |
d2d0adc7 JS |
592 | |
593 | int i; | |
594 | ||
595 | for (i = 0; i < (int) buffer->GetStyleSheet()->GetCharacterStyleCount(); i++) | |
596 | { | |
597 | wxRichTextCharacterStyleDefinition* def = buffer->GetStyleSheet()->GetCharacterStyle(i); | |
598 | ExportStyleDefinition(stream, convMem, convFile, def, level + 1); | |
599 | } | |
600 | ||
601 | for (i = 0; i < (int) buffer->GetStyleSheet()->GetParagraphStyleCount(); i++) | |
602 | { | |
603 | wxRichTextParagraphStyleDefinition* def = buffer->GetStyleSheet()->GetParagraphStyle(i); | |
604 | ExportStyleDefinition(stream, convMem, convFile, def, level + 1); | |
605 | } | |
606 | ||
607 | for (i = 0; i < (int) buffer->GetStyleSheet()->GetListStyleCount(); i++) | |
608 | { | |
609 | wxRichTextListStyleDefinition* def = buffer->GetStyleSheet()->GetListStyle(i); | |
610 | ExportStyleDefinition(stream, convMem, convFile, def, level + 1); | |
611 | } | |
612 | ||
613 | OutputIndentation(stream, level); | |
614 | OutputString(stream, wxT("</stylesheet>"), convMem, convFile); | |
615 | } | |
616 | ||
617 | ||
618 | bool success = ExportXML(stream, convMem, convFile, *buffer, level); | |
87eaa6f6 | 619 | |
5d7836c4 JS |
620 | OutputString(stream, wxT("\n</richtext>") , NULL, NULL); |
621 | OutputString(stream, wxT("\n"), NULL, NULL); | |
7fe8059f | 622 | |
b71e9aa4 JS |
623 | if (deleteConvFile) |
624 | delete convFile; | |
88a7a4e1 | 625 | |
b71e9aa4 | 626 | return success; |
5d7836c4 JS |
627 | } |
628 | ||
629 | /// Recursively export an object | |
630 | bool wxRichTextXMLHandler::ExportXML(wxOutputStream& stream, wxMBConv* convMem, wxMBConv* convFile, wxRichTextObject& obj, int indent) | |
631 | { | |
632 | wxString objectName; | |
633 | if (obj.IsKindOf(CLASSINFO(wxRichTextParagraphLayoutBox))) | |
634 | objectName = wxT("paragraphlayout"); | |
635 | else if (obj.IsKindOf(CLASSINFO(wxRichTextParagraph))) | |
636 | objectName = wxT("paragraph"); | |
637 | else if (obj.IsKindOf(CLASSINFO(wxRichTextPlainText))) | |
638 | objectName = wxT("text"); | |
639 | else if (obj.IsKindOf(CLASSINFO(wxRichTextImage))) | |
640 | objectName = wxT("image"); | |
641 | else | |
642 | objectName = wxT("object"); | |
87eaa6f6 | 643 | |
7b907278 | 644 | bool terminateTag = true; |
5d7836c4 JS |
645 | |
646 | if (obj.IsKindOf(CLASSINFO(wxRichTextPlainText))) | |
647 | { | |
7b907278 | 648 | wxRichTextPlainText& textObj = (wxRichTextPlainText&) obj; |
87eaa6f6 | 649 | |
7b907278 | 650 | wxString style = CreateStyle(obj.GetAttributes(), false); |
87eaa6f6 | 651 | |
7b907278 JS |
652 | int i; |
653 | int last = 0; | |
654 | const wxString& text = textObj.GetText(); | |
655 | int len = (int) text.Length(); | |
656 | for (i = 0; i < len; i++) | |
657 | { | |
658 | int c = (int) text[i]; | |
659 | if (c < 32 && c != 9 && c != 10 && c != 13) | |
660 | { | |
661 | if (i > 0) | |
662 | { | |
663 | OutputIndentation(stream, indent); | |
664 | OutputString(stream, wxT("<") + objectName, convMem, convFile); | |
5d7836c4 | 665 | |
7b907278 | 666 | OutputString(stream, style + wxT(">"), convMem, convFile); |
7fe8059f | 667 | |
7b907278 JS |
668 | wxString fragment(text.Mid(last, i-last)); |
669 | if (!fragment.empty() && (fragment[0] == wxT(' ') || fragment[fragment.length()-1] == wxT(' '))) | |
670 | { | |
671 | OutputString(stream, wxT("\""), convMem, convFile); | |
672 | OutputStringEnt(stream, fragment, convMem, convFile); | |
673 | OutputString(stream, wxT("\""), convMem, convFile); | |
674 | } | |
675 | else | |
676 | OutputStringEnt(stream, fragment, convMem, convFile); | |
7fe8059f | 677 | |
7b907278 | 678 | OutputString(stream, wxT("</text>"), convMem, convFile); |
87eaa6f6 JS |
679 | } |
680 | ||
7b907278 JS |
681 | |
682 | // Output this character as a number in a separate tag, because XML can't cope | |
87eaa6f6 | 683 | // with entities below 32 except for 9, 10 and 13 |
7b907278 JS |
684 | last = i + 1; |
685 | OutputIndentation(stream, indent); | |
686 | OutputString(stream, wxT("<symbol"), convMem, convFile); | |
7fe8059f | 687 | |
7b907278 | 688 | OutputString(stream, style + wxT(">"), convMem, convFile); |
87eaa6f6 | 689 | OutputString(stream, wxString::Format(wxT("%d"), c), convMem, convFile); |
7b907278 JS |
690 | |
691 | OutputString(stream, wxT("</symbol>"), convMem, convFile); | |
692 | } | |
693 | } | |
87eaa6f6 | 694 | |
7b907278 JS |
695 | wxString fragment; |
696 | if (last == 0) | |
697 | fragment = text; | |
698 | else | |
699 | fragment = text.Mid(last, i-last); | |
700 | ||
701 | if (last < len) | |
5d7836c4 | 702 | { |
7b907278 JS |
703 | OutputIndentation(stream, indent); |
704 | OutputString(stream, wxT("<") + objectName, convMem, convFile); | |
705 | ||
706 | OutputString(stream, style + wxT(">"), convMem, convFile); | |
707 | ||
708 | if (!fragment.empty() && (fragment[0] == wxT(' ') || fragment[fragment.length()-1] == wxT(' '))) | |
709 | { | |
710 | OutputString(stream, wxT("\""), convMem, convFile); | |
711 | OutputStringEnt(stream, fragment, convMem, convFile); | |
712 | OutputString(stream, wxT("\""), convMem, convFile); | |
713 | } | |
714 | else | |
715 | OutputStringEnt(stream, fragment, convMem, convFile); | |
5d7836c4 JS |
716 | } |
717 | else | |
7b907278 | 718 | terminateTag = false; |
5d7836c4 JS |
719 | } |
720 | else if (obj.IsKindOf(CLASSINFO(wxRichTextImage))) | |
721 | { | |
722 | wxRichTextImage& imageObj = (wxRichTextImage&) obj; | |
723 | ||
8cc448d3 JS |
724 | wxString style = CreateStyle(obj.GetAttributes(), false); |
725 | ||
5d7836c4 JS |
726 | if (imageObj.GetImage().Ok() && !imageObj.GetImageBlock().Ok()) |
727 | imageObj.MakeBlock(); | |
728 | ||
729 | OutputIndentation(stream, indent); | |
b71e9aa4 | 730 | OutputString(stream, wxT("<") + objectName, convMem, convFile); |
5d7836c4 JS |
731 | if (!imageObj.GetImageBlock().Ok()) |
732 | { | |
733 | // No data | |
8cc448d3 | 734 | OutputString(stream, style + wxT(">"), convMem, convFile); |
5d7836c4 JS |
735 | } |
736 | else | |
737 | { | |
8cc448d3 | 738 | OutputString(stream, wxString::Format(wxT(" imagetype=\"%d\"") + style + wxT(">"), (int) imageObj.GetImageBlock().GetImageType())); |
5d7836c4 JS |
739 | } |
740 | ||
741 | OutputIndentation(stream, indent+1); | |
b71e9aa4 | 742 | OutputString(stream, wxT("<data>"), convMem, convFile); |
5d7836c4 JS |
743 | |
744 | imageObj.GetImageBlock().WriteHex(stream); | |
745 | ||
b71e9aa4 | 746 | OutputString(stream, wxT("</data>"), convMem, convFile); |
5d7836c4 JS |
747 | } |
748 | else if (obj.IsKindOf(CLASSINFO(wxRichTextCompositeObject))) | |
749 | { | |
750 | OutputIndentation(stream, indent); | |
b71e9aa4 | 751 | OutputString(stream, wxT("<") + objectName, convMem, convFile); |
7fe8059f | 752 | |
5d7836c4 JS |
753 | bool isPara = false; |
754 | if (objectName == wxT("paragraph") || objectName == wxT("paragraphlayout")) | |
755 | isPara = true; | |
756 | ||
757 | wxString style = CreateStyle(obj.GetAttributes(), isPara); | |
87eaa6f6 | 758 | |
0ca07313 JS |
759 | if (objectName == wxT("paragraphlayout") && ((wxRichTextParagraphLayoutBox&) obj).GetPartialParagraph()) |
760 | style << wxT(" partialparagraph=\"true\""); | |
761 | ||
b71e9aa4 | 762 | OutputString(stream, style + wxT(">"), convMem, convFile); |
7fe8059f | 763 | |
5d7836c4 JS |
764 | wxRichTextCompositeObject& composite = (wxRichTextCompositeObject&) obj; |
765 | size_t i; | |
766 | for (i = 0; i < composite.GetChildCount(); i++) | |
767 | { | |
768 | wxRichTextObject* child = composite.GetChild(i); | |
769 | ExportXML(stream, convMem, convFile, *child, indent+1); | |
770 | } | |
771 | } | |
772 | ||
773 | if (objectName != wxT("text")) | |
774 | OutputIndentation(stream, indent); | |
775 | ||
7b907278 JS |
776 | if (terminateTag) |
777 | OutputString(stream, wxT("</") + objectName + wxT(">"), convMem, convFile); | |
5d7836c4 JS |
778 | |
779 | return true; | |
780 | } | |
781 | ||
d2d0adc7 JS |
782 | bool wxRichTextXMLHandler::ExportStyleDefinition(wxOutputStream& stream, wxMBConv* convMem, wxMBConv* convFile, wxRichTextStyleDefinition* def, int level) |
783 | { | |
87eaa6f6 | 784 | wxRichTextCharacterStyleDefinition* charDef = wxDynamicCast(def, wxRichTextCharacterStyleDefinition); |
d2d0adc7 JS |
785 | wxRichTextParagraphStyleDefinition* paraDef = wxDynamicCast(def, wxRichTextParagraphStyleDefinition); |
786 | wxRichTextListStyleDefinition* listDef = wxDynamicCast(def, wxRichTextListStyleDefinition); | |
87eaa6f6 | 787 | |
d2d0adc7 JS |
788 | wxString baseStyle = def->GetBaseStyle(); |
789 | wxString baseStyleProp; | |
790 | if (!baseStyle.IsEmpty()) | |
791 | baseStyleProp = wxT(" basestyle=\"") + baseStyle + wxT("\""); | |
87eaa6f6 | 792 | |
42688aea JS |
793 | wxString descr = def->GetDescription(); |
794 | wxString descrProp; | |
795 | if (!descr.IsEmpty()) | |
796 | descrProp = wxT(" description=\"") + descr + wxT("\""); | |
87eaa6f6 | 797 | |
d2d0adc7 JS |
798 | if (charDef) |
799 | { | |
800 | OutputIndentation(stream, level); | |
42688aea | 801 | OutputString(stream, wxT("<characterstyle") + baseStyleProp + descrProp + wxT(">"), convMem, convFile); |
87eaa6f6 | 802 | |
d2d0adc7 JS |
803 | level ++; |
804 | ||
805 | wxString style = CreateStyle(def->GetStyle(), false); | |
806 | ||
807 | OutputIndentation(stream, level); | |
808 | OutputString(stream, wxT("<style ") + style + wxT(">"), convMem, convFile); | |
87eaa6f6 | 809 | |
d2d0adc7 JS |
810 | OutputIndentation(stream, level); |
811 | OutputString(stream, wxT("</style>"), convMem, convFile); | |
812 | ||
813 | level --; | |
814 | ||
815 | OutputIndentation(stream, level); | |
816 | OutputString(stream, wxT("</characterstyle>"), convMem, convFile); | |
817 | } | |
818 | else if (listDef) | |
819 | { | |
820 | OutputIndentation(stream, level); | |
87eaa6f6 | 821 | |
d2d0adc7 JS |
822 | if (!listDef->GetNextStyle().IsEmpty()) |
823 | baseStyleProp << wxT(" basestyle=\"") << listDef->GetNextStyle() << wxT("\""); | |
87eaa6f6 | 824 | |
42688aea | 825 | OutputString(stream, wxT("<liststyle") + baseStyleProp + descrProp + wxT(">"), convMem, convFile); |
87eaa6f6 | 826 | |
d2d0adc7 JS |
827 | level ++; |
828 | ||
829 | wxString style = CreateStyle(def->GetStyle(), false); | |
830 | ||
831 | OutputIndentation(stream, level); | |
832 | OutputString(stream, wxT("<style ") + style + wxT(">"), convMem, convFile); | |
87eaa6f6 | 833 | |
d2d0adc7 JS |
834 | OutputIndentation(stream, level); |
835 | OutputString(stream, wxT("</style>"), convMem, convFile); | |
836 | ||
837 | int i; | |
838 | for (i = 0; i < 10; i ++) | |
839 | { | |
44cc96a8 | 840 | wxTextAttr* levelAttr = listDef->GetLevelAttributes(i); |
d2d0adc7 JS |
841 | if (levelAttr) |
842 | { | |
843 | wxString style = CreateStyle(def->GetStyle(), false); | |
844 | wxString levelStr = wxString::Format(wxT(" level=\"%d\" "), (i+1)); | |
845 | ||
846 | OutputIndentation(stream, level); | |
847 | OutputString(stream, wxT("<style ") + levelStr + style + wxT(">"), convMem, convFile); | |
87eaa6f6 | 848 | |
d2d0adc7 JS |
849 | OutputIndentation(stream, level); |
850 | OutputString(stream, wxT("</style>"), convMem, convFile); | |
851 | } | |
852 | } | |
853 | ||
854 | level --; | |
855 | ||
856 | OutputIndentation(stream, level); | |
857 | OutputString(stream, wxT("</liststyle>"), convMem, convFile); | |
858 | } | |
859 | else if (paraDef) | |
860 | { | |
861 | OutputIndentation(stream, level); | |
87eaa6f6 JS |
862 | |
863 | if (!paraDef->GetNextStyle().IsEmpty()) | |
864 | baseStyleProp << wxT(" basestyle=\"") << paraDef->GetNextStyle() << wxT("\""); | |
865 | ||
42688aea | 866 | OutputString(stream, wxT("<paragraphstyle") + baseStyleProp + descrProp + wxT(">"), convMem, convFile); |
87eaa6f6 | 867 | |
d2d0adc7 JS |
868 | level ++; |
869 | ||
870 | wxString style = CreateStyle(def->GetStyle(), false); | |
871 | ||
872 | OutputIndentation(stream, level); | |
873 | OutputString(stream, wxT("<style ") + style + wxT(">"), convMem, convFile); | |
87eaa6f6 | 874 | |
d2d0adc7 JS |
875 | OutputIndentation(stream, level); |
876 | OutputString(stream, wxT("</style>"), convMem, convFile); | |
87eaa6f6 | 877 | |
d2d0adc7 JS |
878 | level --; |
879 | ||
880 | OutputIndentation(stream, level); | |
881 | OutputString(stream, wxT("</paragraphstyle>"), convMem, convFile); | |
882 | } | |
883 | ||
884 | return true; | |
885 | } | |
886 | ||
5d7836c4 | 887 | /// Create style parameters |
44cc96a8 | 888 | wxString wxRichTextXMLHandler::CreateStyle(const wxTextAttr& attr, bool isPara) |
5d7836c4 JS |
889 | { |
890 | wxString str; | |
0ca07313 | 891 | if (attr.HasTextColour() && attr.GetTextColour().Ok()) |
5d7836c4 JS |
892 | { |
893 | str << wxT(" textcolor=\"#") << ColourToHexString(attr.GetTextColour()) << wxT("\""); | |
894 | } | |
0ca07313 | 895 | if (attr.HasBackgroundColour() && attr.GetBackgroundColour().Ok()) |
5d7836c4 JS |
896 | { |
897 | str << wxT(" bgcolor=\"#") << ColourToHexString(attr.GetBackgroundColour()) << wxT("\""); | |
898 | } | |
899 | ||
44cc96a8 JS |
900 | if (attr.HasFontSize()) |
901 | str << wxT(" fontsize=\"") << attr.GetFontSize() << wxT("\""); | |
87eaa6f6 | 902 | |
44cc96a8 JS |
903 | //if (attr.HasFontFamily()) |
904 | // str << wxT(" fontfamily=\"") << attr.GetFont().GetFamily() << wxT("\""); | |
0ca07313 | 905 | |
44cc96a8 JS |
906 | if (attr.HasFontItalic()) |
907 | str << wxT(" fontstyle=\"") << attr.GetFontStyle() << wxT("\""); | |
0ca07313 | 908 | |
44cc96a8 JS |
909 | if (attr.HasFontWeight()) |
910 | str << wxT(" fontweight=\"") << attr.GetFontWeight() << wxT("\""); | |
0ca07313 | 911 | |
44cc96a8 JS |
912 | if (attr.HasFontUnderlined()) |
913 | str << wxT(" fontunderlined=\"") << (int) attr.GetFontUnderlined() << wxT("\""); | |
0ca07313 | 914 | |
44cc96a8 JS |
915 | if (attr.HasFontFaceName()) |
916 | str << wxT(" fontface=\"") << attr.GetFontFaceName() << wxT("\""); | |
5d7836c4 | 917 | |
42688aea JS |
918 | if (attr.HasTextEffects()) |
919 | { | |
920 | str << wxT(" texteffects=\""); | |
921 | str << attr.GetTextEffects(); | |
922 | str << wxT("\""); | |
1f65137f JS |
923 | |
924 | str << wxT(" texteffectflags=\""); | |
925 | str << attr.GetTextEffectFlags(); | |
926 | str << wxT("\""); | |
42688aea JS |
927 | } |
928 | ||
7fe8059f | 929 | if (!attr.GetCharacterStyleName().empty()) |
f089713f | 930 | str << wxT(" characterstyle=\"") << wxString(attr.GetCharacterStyleName()) << wxT("\""); |
5d7836c4 | 931 | |
03cd2124 JS |
932 | if (attr.HasURL()) |
933 | str << wxT(" url=\"") << attr.GetURL() << wxT("\""); | |
934 | ||
5d7836c4 JS |
935 | if (isPara) |
936 | { | |
0ca07313 JS |
937 | if (attr.HasAlignment()) |
938 | str << wxT(" alignment=\"") << (int) attr.GetAlignment() << wxT("\""); | |
939 | ||
940 | if (attr.HasLeftIndent()) | |
941 | { | |
942 | str << wxT(" leftindent=\"") << (int) attr.GetLeftIndent() << wxT("\""); | |
943 | str << wxT(" leftsubindent=\"") << (int) attr.GetLeftSubIndent() << wxT("\""); | |
944 | } | |
945 | ||
946 | if (attr.HasRightIndent()) | |
947 | str << wxT(" rightindent=\"") << (int) attr.GetRightIndent() << wxT("\""); | |
948 | ||
949 | if (attr.HasParagraphSpacingAfter()) | |
950 | str << wxT(" parspacingafter=\"") << (int) attr.GetParagraphSpacingAfter() << wxT("\""); | |
951 | ||
952 | if (attr.HasParagraphSpacingBefore()) | |
953 | str << wxT(" parspacingbefore=\"") << (int) attr.GetParagraphSpacingBefore() << wxT("\""); | |
954 | ||
955 | if (attr.HasLineSpacing()) | |
956 | str << wxT(" linespacing=\"") << (int) attr.GetLineSpacing() << wxT("\""); | |
957 | ||
958 | if (attr.HasBulletStyle()) | |
959 | str << wxT(" bulletstyle=\"") << (int) attr.GetBulletStyle() << wxT("\""); | |
960 | ||
961 | if (attr.HasBulletNumber()) | |
962 | str << wxT(" bulletnumber=\"") << (int) attr.GetBulletNumber() << wxT("\""); | |
963 | ||
d2d0adc7 | 964 | if (attr.HasBulletText()) |
7b907278 | 965 | { |
d2d0adc7 JS |
966 | // If using a bullet symbol, convert to integer in case it's a non-XML-friendly character. |
967 | // Otherwise, assume it's XML-friendly text such as outline numbering, e.g. 1.2.3.1 | |
968 | if (!attr.GetBulletText().IsEmpty() && (attr.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL)) | |
969 | str << wxT(" bulletsymbol=\"") << (int) (attr.GetBulletText()[0]) << wxT("\""); | |
970 | else | |
971 | str << wxT(" bullettext=\"") << attr.GetBulletText() << wxT("\""); | |
87eaa6f6 | 972 | |
7b907278 JS |
973 | str << wxT(" bulletfont=\"") << attr.GetBulletFont() << wxT("\""); |
974 | } | |
5d7836c4 | 975 | |
f089713f JS |
976 | if (attr.HasBulletName()) |
977 | str << wxT(" bulletname=\"") << attr.GetBulletName() << wxT("\""); | |
978 | ||
7fe8059f | 979 | if (!attr.GetParagraphStyleName().empty()) |
5d7836c4 | 980 | str << wxT(" parstyle=\"") << wxString(attr.GetParagraphStyleName()) << wxT("\""); |
87eaa6f6 | 981 | |
f089713f JS |
982 | if (!attr.GetListStyleName().empty()) |
983 | str << wxT(" liststyle=\"") << wxString(attr.GetListStyleName()) << wxT("\""); | |
87eaa6f6 | 984 | |
0ca07313 JS |
985 | if (attr.HasTabs()) |
986 | { | |
987 | str << wxT(" tabs=\""); | |
988 | size_t i; | |
989 | for (i = 0; i < attr.GetTabs().GetCount(); i++) | |
990 | { | |
991 | if (i > 0) | |
992 | str << wxT(","); | |
993 | str << attr.GetTabs()[i]; | |
994 | } | |
87eaa6f6 | 995 | str << wxT("\""); |
0ca07313 | 996 | } |
87eaa6f6 | 997 | |
42688aea JS |
998 | if (attr.HasPageBreak()) |
999 | { | |
1000 | str << wxT(" pagebreak=\"1\""); | |
1001 | } | |
4d6d8bf4 JS |
1002 | |
1003 | if (attr.HasOutlineLevel()) | |
1004 | str << wxT(" outlinelevel=\"") << (int) attr.GetOutlineLevel() << wxT("\""); | |
1005 | ||
5d7836c4 JS |
1006 | } |
1007 | ||
1008 | return str; | |
1009 | } | |
1010 | ||
1011 | /// Get style parameters | |
44cc96a8 | 1012 | bool wxRichTextXMLHandler::GetStyle(wxTextAttr& attr, wxXmlNode* node, bool isPara) |
5d7836c4 JS |
1013 | { |
1014 | wxString fontFacename; | |
1015 | int fontSize = 12; | |
44cc96a8 | 1016 | // int fontFamily = wxDEFAULT; |
5d7836c4 JS |
1017 | int fontWeight = wxNORMAL; |
1018 | int fontStyle = wxNORMAL; | |
1019 | bool fontUnderlined = false; | |
87eaa6f6 | 1020 | |
44cc96a8 | 1021 | // int fontFlags = 0; |
0ca07313 | 1022 | |
288b6107 | 1023 | fontFacename = node->GetAttribute(wxT("fontface"), wxEmptyString); |
0ca07313 | 1024 | if (!fontFacename.IsEmpty()) |
44cc96a8 | 1025 | attr.SetFontFaceName(fontFacename); |
5d7836c4 | 1026 | |
0ca07313 | 1027 | wxString value; |
288b6107 | 1028 | //value = node->GetAttribute(wxT("fontfamily"), wxEmptyString); |
0ca07313 JS |
1029 | //if (!value.empty()) |
1030 | // fontFamily = wxAtoi(value); | |
5d7836c4 | 1031 | |
288b6107 | 1032 | value = node->GetAttribute(wxT("fontstyle"), wxEmptyString); |
7fe8059f | 1033 | if (!value.empty()) |
0ca07313 | 1034 | { |
5d7836c4 | 1035 | fontStyle = wxAtoi(value); |
44cc96a8 | 1036 | attr.SetFontStyle(fontStyle); |
0ca07313 | 1037 | } |
5d7836c4 | 1038 | |
288b6107 | 1039 | value = node->GetAttribute(wxT("fontsize"), wxEmptyString); |
7fe8059f | 1040 | if (!value.empty()) |
0ca07313 | 1041 | { |
5d7836c4 | 1042 | fontSize = wxAtoi(value); |
44cc96a8 | 1043 | attr.SetFontSize(fontSize); |
0ca07313 | 1044 | } |
5d7836c4 | 1045 | |
288b6107 | 1046 | value = node->GetAttribute(wxT("fontweight"), wxEmptyString); |
7fe8059f | 1047 | if (!value.empty()) |
0ca07313 | 1048 | { |
5d7836c4 | 1049 | fontWeight = wxAtoi(value); |
44cc96a8 | 1050 | attr.SetFontWeight(fontWeight); |
0ca07313 | 1051 | } |
5d7836c4 | 1052 | |
288b6107 | 1053 | value = node->GetAttribute(wxT("fontunderlined"), wxEmptyString); |
7fe8059f | 1054 | if (!value.empty()) |
0ca07313 | 1055 | { |
5d7836c4 | 1056 | fontUnderlined = wxAtoi(value) != 0; |
44cc96a8 | 1057 | attr.SetFontUnderlined(fontUnderlined); |
0ca07313 | 1058 | } |
87eaa6f6 | 1059 | |
288b6107 | 1060 | value = node->GetAttribute(wxT("textcolor"), wxEmptyString); |
7fe8059f | 1061 | if (!value.empty()) |
5d7836c4 JS |
1062 | { |
1063 | if (value[0] == wxT('#')) | |
1064 | attr.SetTextColour(HexStringToColour(value.Mid(1))); | |
1065 | else | |
1066 | attr.SetTextColour(value); | |
1067 | } | |
1068 | ||
6cced968 | 1069 | value = node->GetAttribute(wxT("bgcolor"), wxEmptyString); |
7fe8059f | 1070 | if (!value.empty()) |
5d7836c4 JS |
1071 | { |
1072 | if (value[0] == wxT('#')) | |
1073 | attr.SetBackgroundColour(HexStringToColour(value.Mid(1))); | |
1074 | else | |
1075 | attr.SetBackgroundColour(value); | |
1076 | } | |
1077 | ||
288b6107 | 1078 | value = node->GetAttribute(wxT("characterstyle"), wxEmptyString); |
7fe8059f | 1079 | if (!value.empty()) |
5d7836c4 JS |
1080 | attr.SetCharacterStyleName(value); |
1081 | ||
288b6107 | 1082 | value = node->GetAttribute(wxT("texteffects"), wxEmptyString); |
42688aea JS |
1083 | if (!value.IsEmpty()) |
1084 | { | |
1085 | attr.SetTextEffects(wxAtoi(value)); | |
1086 | } | |
1087 | ||
288b6107 | 1088 | value = node->GetAttribute(wxT("texteffectflags"), wxEmptyString); |
1f65137f JS |
1089 | if (!value.IsEmpty()) |
1090 | { | |
1091 | attr.SetTextEffectFlags(wxAtoi(value)); | |
1092 | } | |
1093 | ||
03cd2124 JS |
1094 | value = node->GetAttribute(wxT("url"), wxEmptyString); |
1095 | if (!value.empty()) | |
1096 | attr.SetURL(value); | |
1097 | ||
5d7836c4 JS |
1098 | // Set paragraph attributes |
1099 | if (isPara) | |
1100 | { | |
288b6107 | 1101 | value = node->GetAttribute(wxT("alignment"), wxEmptyString); |
7fe8059f | 1102 | if (!value.empty()) |
5d7836c4 JS |
1103 | attr.SetAlignment((wxTextAttrAlignment) wxAtoi(value)); |
1104 | ||
1105 | int leftSubIndent = 0; | |
1106 | int leftIndent = 0; | |
0ca07313 | 1107 | bool hasLeftIndent = false; |
87eaa6f6 | 1108 | |
288b6107 | 1109 | value = node->GetAttribute(wxT("leftindent"), wxEmptyString); |
7fe8059f | 1110 | if (!value.empty()) |
0ca07313 | 1111 | { |
5d7836c4 | 1112 | leftIndent = wxAtoi(value); |
0ca07313 JS |
1113 | hasLeftIndent = true; |
1114 | } | |
1115 | ||
288b6107 | 1116 | value = node->GetAttribute(wxT("leftsubindent"), wxEmptyString); |
7fe8059f | 1117 | if (!value.empty()) |
0ca07313 | 1118 | { |
5d7836c4 | 1119 | leftSubIndent = wxAtoi(value); |
0ca07313 JS |
1120 | hasLeftIndent = true; |
1121 | } | |
1122 | ||
1123 | if (hasLeftIndent) | |
1124 | attr.SetLeftIndent(leftIndent, leftSubIndent); | |
5d7836c4 | 1125 | |
288b6107 | 1126 | value = node->GetAttribute(wxT("rightindent"), wxEmptyString); |
7fe8059f | 1127 | if (!value.empty()) |
5d7836c4 JS |
1128 | attr.SetRightIndent(wxAtoi(value)); |
1129 | ||
288b6107 | 1130 | value = node->GetAttribute(wxT("parspacingbefore"), wxEmptyString); |
7fe8059f | 1131 | if (!value.empty()) |
5d7836c4 JS |
1132 | attr.SetParagraphSpacingBefore(wxAtoi(value)); |
1133 | ||
288b6107 | 1134 | value = node->GetAttribute(wxT("parspacingafter"), wxEmptyString); |
7fe8059f | 1135 | if (!value.empty()) |
5d7836c4 JS |
1136 | attr.SetParagraphSpacingAfter(wxAtoi(value)); |
1137 | ||
288b6107 | 1138 | value = node->GetAttribute(wxT("linespacing"), wxEmptyString); |
7fe8059f | 1139 | if (!value.empty()) |
5d7836c4 JS |
1140 | attr.SetLineSpacing(wxAtoi(value)); |
1141 | ||
288b6107 | 1142 | value = node->GetAttribute(wxT("bulletstyle"), wxEmptyString); |
7fe8059f | 1143 | if (!value.empty()) |
5d7836c4 JS |
1144 | attr.SetBulletStyle(wxAtoi(value)); |
1145 | ||
288b6107 | 1146 | value = node->GetAttribute(wxT("bulletnumber"), wxEmptyString); |
7fe8059f | 1147 | if (!value.empty()) |
5d7836c4 JS |
1148 | attr.SetBulletNumber(wxAtoi(value)); |
1149 | ||
288b6107 | 1150 | value = node->GetAttribute(wxT("bulletsymbol"), wxEmptyString); |
7fe8059f | 1151 | if (!value.empty()) |
d2d0adc7 JS |
1152 | { |
1153 | wxChar ch = wxAtoi(value); | |
1154 | wxString s; | |
1155 | s << ch; | |
1156 | attr.SetBulletText(s); | |
1157 | } | |
1158 | ||
288b6107 | 1159 | value = node->GetAttribute(wxT("bullettext"), wxEmptyString); |
d2d0adc7 JS |
1160 | if (!value.empty()) |
1161 | attr.SetBulletText(value); | |
7b907278 | 1162 | |
288b6107 | 1163 | value = node->GetAttribute(wxT("bulletfont"), wxEmptyString); |
7b907278 JS |
1164 | if (!value.empty()) |
1165 | attr.SetBulletFont(value); | |
5d7836c4 | 1166 | |
288b6107 | 1167 | value = node->GetAttribute(wxT("bulletname"), wxEmptyString); |
f089713f JS |
1168 | if (!value.empty()) |
1169 | attr.SetBulletName(value); | |
1170 | ||
288b6107 | 1171 | value = node->GetAttribute(wxT("parstyle"), wxEmptyString); |
7fe8059f | 1172 | if (!value.empty()) |
5d7836c4 | 1173 | attr.SetParagraphStyleName(value); |
87eaa6f6 | 1174 | |
288b6107 | 1175 | value = node->GetAttribute(wxT("liststyle"), wxEmptyString); |
f089713f JS |
1176 | if (!value.empty()) |
1177 | attr.SetListStyleName(value); | |
87eaa6f6 | 1178 | |
288b6107 | 1179 | value = node->GetAttribute(wxT("tabs"), wxEmptyString); |
0ca07313 JS |
1180 | if (!value.empty()) |
1181 | { | |
1182 | wxArrayInt tabs; | |
1183 | wxStringTokenizer tkz(value, wxT(",")); | |
1184 | while (tkz.HasMoreTokens()) | |
1185 | { | |
1186 | wxString token = tkz.GetNextToken(); | |
1187 | tabs.Add(wxAtoi(token)); | |
1188 | } | |
1189 | attr.SetTabs(tabs); | |
1190 | } | |
87eaa6f6 | 1191 | |
288b6107 | 1192 | value = node->GetAttribute(wxT("pagebreak"), wxEmptyString); |
42688aea JS |
1193 | if (!value.IsEmpty()) |
1194 | { | |
1195 | attr.SetPageBreak(wxAtoi(value) != 0); | |
1196 | } | |
4d6d8bf4 | 1197 | |
288b6107 | 1198 | value = node->GetAttribute(wxT("outlinelevel"), wxEmptyString); |
4d6d8bf4 JS |
1199 | if (!value.IsEmpty()) |
1200 | { | |
aa097547 | 1201 | attr.SetOutlineLevel(wxAtoi(value)); |
4d6d8bf4 | 1202 | } |
5d7836c4 JS |
1203 | } |
1204 | ||
1205 | return true; | |
1206 | } | |
1207 | ||
1208 | #endif | |
b71e9aa4 | 1209 | // wxUSE_STREAMS |
88a7a4e1 | 1210 | |
5d7836c4 | 1211 | #endif |
fcdbeefa | 1212 | // wxUSE_RICHTEXT && wxUSE_XML |
7b907278 | 1213 |