]>
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 | ||
44 | buffer->Clear(); | |
45 | ||
46 | wxXmlDocument* xmlDoc = new wxXmlDocument; | |
47 | bool success = true; | |
48 | ||
b71e9aa4 JS |
49 | // This is the encoding to convert to (memory encoding rather than file encoding) |
50 | wxString encoding(wxT("UTF-8")); | |
51 | ||
52 | #if !wxUSE_UNICODE && wxUSE_INTL | |
53 | encoding = wxLocale::GetSystemEncodingName(); | |
54 | #endif | |
55 | ||
56 | if (!xmlDoc->Load(stream, encoding)) | |
5d7836c4 JS |
57 | { |
58 | success = false; | |
59 | } | |
60 | else | |
61 | { | |
62 | if (xmlDoc->GetRoot() && xmlDoc->GetRoot()->GetType() == wxXML_ELEMENT_NODE && xmlDoc->GetRoot()->GetName() == wxT("richtext")) | |
63 | { | |
64 | wxXmlNode* child = xmlDoc->GetRoot()->GetChildren(); | |
65 | while (child) | |
66 | { | |
67 | if (child->GetType() == wxXML_ELEMENT_NODE) | |
68 | { | |
69 | wxString name = child->GetName(); | |
70 | if (name == wxT("richtext-version")) | |
71 | { | |
72 | } | |
73 | else | |
74 | ImportXML(buffer, child); | |
75 | } | |
7fe8059f | 76 | |
5d7836c4 JS |
77 | child = child->GetNext(); |
78 | } | |
79 | } | |
80 | else | |
81 | { | |
82 | success = false; | |
83 | } | |
84 | } | |
7fe8059f | 85 | |
5d7836c4 JS |
86 | delete xmlDoc; |
87 | ||
88 | buffer->UpdateRanges(); | |
89 | ||
90 | return success; | |
91 | } | |
92 | ||
93 | /// Recursively import an object | |
94 | bool wxRichTextXMLHandler::ImportXML(wxRichTextBuffer* buffer, wxXmlNode* node) | |
95 | { | |
96 | wxString name = node->GetName(); | |
97 | ||
98 | bool doneChildren = false; | |
99 | ||
100 | if (name == wxT("paragraphlayout")) | |
101 | { | |
0ca07313 JS |
102 | wxString partial = node->GetPropVal(wxT("partialparagraph"), wxEmptyString); |
103 | if (partial == wxT("true")) | |
104 | buffer->SetPartialParagraph(true); | |
5d7836c4 JS |
105 | } |
106 | else if (name == wxT("paragraph")) | |
107 | { | |
108 | wxRichTextParagraph* para = new wxRichTextParagraph(buffer); | |
109 | buffer->AppendChild(para); | |
110 | ||
111 | GetStyle(para->GetAttributes(), node, true); | |
112 | ||
113 | wxXmlNode* child = node->GetChildren(); | |
114 | while (child) | |
115 | { | |
116 | wxString childName = child->GetName(); | |
117 | if (childName == wxT("text")) | |
118 | { | |
119 | wxString text; | |
120 | wxXmlNode* textChild = child->GetChildren(); | |
121 | while (textChild) | |
122 | { | |
123 | if (textChild->GetType() == wxXML_TEXT_NODE || | |
124 | textChild->GetType() == wxXML_CDATA_SECTION_NODE) | |
125 | { | |
126 | wxString text2 = textChild->GetContent(); | |
127 | ||
128 | // Strip whitespace from end | |
88a7a4e1 WS |
129 | if (!text2.empty() && text2[text2.length()-1] == wxT('\n')) |
130 | text2 = text2.Mid(0, text2.length()-1); | |
5d7836c4 | 131 | |
88a7a4e1 | 132 | if (!text2.empty() && text2[0] == wxT('"')) |
5d7836c4 | 133 | text2 = text2.Mid(1); |
88a7a4e1 WS |
134 | if (!text2.empty() && text2[text2.length()-1] == wxT('"')) |
135 | text2 = text2.Mid(0, text2.length() - 1); | |
5d7836c4 | 136 | |
5d7836c4 JS |
137 | text += text2; |
138 | } | |
139 | textChild = textChild->GetNext(); | |
140 | } | |
141 | ||
142 | wxRichTextPlainText* textObject = new wxRichTextPlainText(text, para); | |
143 | GetStyle(textObject->GetAttributes(), child, false); | |
144 | ||
145 | para->AppendChild(textObject); | |
146 | } | |
147 | else if (childName == wxT("image")) | |
148 | { | |
149 | int imageType = wxBITMAP_TYPE_PNG; | |
150 | wxString value = node->GetPropVal(wxT("imagetype"), wxEmptyString); | |
7fe8059f | 151 | if (!value.empty()) |
5d7836c4 JS |
152 | imageType = wxAtoi(value); |
153 | ||
154 | wxString data; | |
155 | ||
156 | wxXmlNode* imageChild = child->GetChildren(); | |
157 | while (imageChild) | |
158 | { | |
159 | wxString childName = imageChild->GetName(); | |
160 | if (childName == wxT("data")) | |
161 | { | |
162 | wxXmlNode* dataChild = imageChild->GetChildren(); | |
163 | while (dataChild) | |
164 | { | |
165 | data = dataChild->GetContent(); | |
166 | // wxLogDebug(data); | |
167 | dataChild = dataChild->GetNext(); | |
168 | } | |
7fe8059f | 169 | |
5d7836c4 JS |
170 | } |
171 | imageChild = imageChild->GetNext(); | |
172 | } | |
173 | ||
7fe8059f | 174 | if (!data.empty()) |
5d7836c4 JS |
175 | { |
176 | wxRichTextImage* imageObj = new wxRichTextImage(para); | |
177 | para->AppendChild(imageObj); | |
178 | ||
179 | wxStringInputStream strStream(data); | |
180 | ||
88a7a4e1 | 181 | imageObj->GetImageBlock().ReadHex(strStream, data.length(), imageType); |
5d7836c4 JS |
182 | } |
183 | } | |
184 | child = child->GetNext(); | |
185 | } | |
186 | ||
187 | doneChildren = true; | |
188 | } | |
189 | ||
190 | if (!doneChildren) | |
191 | { | |
192 | wxXmlNode* child = node->GetChildren(); | |
193 | while (child) | |
194 | { | |
195 | ImportXML(buffer, child); | |
196 | child = child->GetNext(); | |
197 | } | |
7fe8059f | 198 | } |
5d7836c4 JS |
199 | |
200 | return true; | |
201 | } | |
202 | ||
203 | ||
204 | //----------------------------------------------------------------------------- | |
205 | // xml support routines | |
206 | //----------------------------------------------------------------------------- | |
207 | ||
208 | bool wxRichTextXMLHandler::HasParam(wxXmlNode* node, const wxString& param) | |
209 | { | |
210 | return (GetParamNode(node, param) != NULL); | |
211 | } | |
212 | ||
213 | wxXmlNode *wxRichTextXMLHandler::GetParamNode(wxXmlNode* node, const wxString& param) | |
214 | { | |
215 | wxCHECK_MSG(node, NULL, wxT("You can't access node data before it was initialized!")); | |
216 | ||
217 | wxXmlNode *n = node->GetChildren(); | |
218 | ||
219 | while (n) | |
220 | { | |
221 | if (n->GetType() == wxXML_ELEMENT_NODE && n->GetName() == param) | |
222 | return n; | |
223 | n = n->GetNext(); | |
224 | } | |
225 | return NULL; | |
226 | } | |
227 | ||
228 | ||
229 | wxString wxRichTextXMLHandler::GetNodeContent(wxXmlNode *node) | |
230 | { | |
231 | wxXmlNode *n = node; | |
232 | if (n == NULL) return wxEmptyString; | |
233 | n = n->GetChildren(); | |
234 | ||
235 | while (n) | |
236 | { | |
237 | if (n->GetType() == wxXML_TEXT_NODE || | |
238 | n->GetType() == wxXML_CDATA_SECTION_NODE) | |
239 | return n->GetContent(); | |
240 | n = n->GetNext(); | |
241 | } | |
242 | return wxEmptyString; | |
243 | } | |
244 | ||
245 | ||
246 | wxString wxRichTextXMLHandler::GetParamValue(wxXmlNode *node, const wxString& param) | |
247 | { | |
7fe8059f | 248 | if (param.empty()) |
5d7836c4 JS |
249 | return GetNodeContent(node); |
250 | else | |
251 | return GetNodeContent(GetParamNode(node, param)); | |
252 | } | |
253 | ||
254 | wxString wxRichTextXMLHandler::GetText(wxXmlNode *node, const wxString& param, bool WXUNUSED(translate)) | |
255 | { | |
256 | wxXmlNode *parNode = GetParamNode(node, param); | |
257 | if (!parNode) | |
258 | parNode = node; | |
259 | wxString str1(GetNodeContent(parNode)); | |
260 | return str1; | |
261 | } | |
262 | ||
1e967276 JS |
263 | // For use with earlier versions of wxWidgets |
264 | #ifndef WXUNUSED_IN_UNICODE | |
265 | #if wxUSE_UNICODE | |
266 | #define WXUNUSED_IN_UNICODE(x) WXUNUSED(x) | |
267 | #else | |
268 | #define WXUNUSED_IN_UNICODE(x) x | |
269 | #endif | |
270 | #endif | |
271 | ||
5d7836c4 JS |
272 | // write string to output: |
273 | inline static void OutputString(wxOutputStream& stream, const wxString& str, | |
7fe8059f | 274 | wxMBConv *WXUNUSED_IN_UNICODE(convMem) = NULL, wxMBConv *convFile = NULL) |
5d7836c4 | 275 | { |
7fe8059f | 276 | if (str.empty()) return; |
5d7836c4 | 277 | #if wxUSE_UNICODE |
0bab774b JS |
278 | if (convFile) |
279 | { | |
280 | const wxWX2MBbuf buf(str.mb_str(*convFile)); | |
281 | stream.Write((const char*)buf, strlen((const char*)buf)); | |
282 | } | |
283 | else | |
284 | { | |
285 | const wxWX2MBbuf buf(str.mb_str(wxConvUTF8)); | |
286 | stream.Write((const char*)buf, strlen((const char*)buf)); | |
287 | } | |
5d7836c4 JS |
288 | #else |
289 | if ( convFile == NULL ) | |
290 | stream.Write(str.mb_str(), str.Len()); | |
291 | else | |
292 | { | |
293 | wxString str2(str.wc_str(*convMem), *convFile); | |
294 | stream.Write(str2.mb_str(), str2.Len()); | |
295 | } | |
296 | #endif | |
297 | } | |
298 | ||
299 | // Same as above, but create entities first. | |
300 | // Translates '<' to "<", '>' to ">" and '&' to "&" | |
301 | static void OutputStringEnt(wxOutputStream& stream, const wxString& str, | |
302 | wxMBConv *convMem = NULL, wxMBConv *convFile = NULL) | |
303 | { | |
304 | wxString buf; | |
305 | size_t i, last, len; | |
306 | wxChar c; | |
7fe8059f | 307 | |
5d7836c4 JS |
308 | len = str.Len(); |
309 | last = 0; | |
310 | for (i = 0; i < len; i++) | |
311 | { | |
312 | c = str.GetChar(i); | |
88a7a4e1 | 313 | |
b71e9aa4 JS |
314 | // Original code excluded "&" but we _do_ want to convert |
315 | // the ampersand beginning & because otherwise when read in, | |
316 | // the original "&" becomes "&". | |
317 | ||
5d7836c4 | 318 | if (c == wxT('<') || c == wxT('>') || c == wxT('"') || |
b71e9aa4 | 319 | (c == wxT('&') /* && (str.Mid(i+1, 4) != wxT("amp;")) */ )) |
5d7836c4 JS |
320 | { |
321 | OutputString(stream, str.Mid(last, i - last), convMem, convFile); | |
322 | switch (c) | |
323 | { | |
324 | case wxT('<'): | |
325 | OutputString(stream, wxT("<"), NULL, NULL); | |
326 | break; | |
327 | case wxT('>'): | |
328 | OutputString(stream, wxT(">"), NULL, NULL); | |
329 | break; | |
330 | case wxT('&'): | |
331 | OutputString(stream, wxT("&"), NULL, NULL); | |
332 | break; | |
333 | case wxT('"'): | |
334 | OutputString(stream, wxT("""), NULL, NULL); | |
335 | break; | |
336 | default: break; | |
337 | } | |
338 | last = i + 1; | |
339 | } | |
340 | } | |
341 | OutputString(stream, str.Mid(last, i - last), convMem, convFile); | |
342 | } | |
343 | ||
344 | inline static void OutputIndentation(wxOutputStream& stream, int indent) | |
345 | { | |
346 | wxString str = wxT("\n"); | |
347 | for (int i = 0; i < indent; i++) | |
348 | str << wxT(' ') << wxT(' '); | |
349 | OutputString(stream, str, NULL, NULL); | |
350 | } | |
351 | ||
5d7836c4 JS |
352 | // Convert a colour to a 6-digit hex string |
353 | static wxString ColourToHexString(const wxColour& col) | |
354 | { | |
355 | wxString hex; | |
356 | ||
357 | hex += wxDecToHex(col.Red()); | |
358 | hex += wxDecToHex(col.Green()); | |
359 | hex += wxDecToHex(col.Blue()); | |
360 | ||
361 | return hex; | |
362 | } | |
363 | ||
364 | // Convert 6-digit hex string to a colour | |
b71e9aa4 | 365 | static wxColour HexStringToColour(const wxString& hex) |
5d7836c4 | 366 | { |
7fe8059f WS |
367 | unsigned char r = (unsigned char)wxHexToDec(hex.Mid(0, 2)); |
368 | unsigned char g = (unsigned char)wxHexToDec(hex.Mid(2, 2)); | |
369 | unsigned char b = (unsigned char)wxHexToDec(hex.Mid(4, 2)); | |
5d7836c4 JS |
370 | |
371 | return wxColour(r, g, b); | |
372 | } | |
373 | ||
7fe8059f | 374 | bool wxRichTextXMLHandler::DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream) |
5d7836c4 JS |
375 | { |
376 | if (!stream.IsOk()) | |
377 | return false; | |
378 | ||
379 | wxString version(wxT("1.0") ) ; | |
b71e9aa4 JS |
380 | |
381 | bool deleteConvFile = false; | |
382 | wxString fileEncoding; | |
383 | wxMBConv* convFile = NULL; | |
384 | ||
5d7836c4 | 385 | #if wxUSE_UNICODE |
b71e9aa4 JS |
386 | fileEncoding = wxT("UTF-8"); |
387 | convFile = & wxConvUTF8; | |
5d7836c4 | 388 | #else |
b71e9aa4 JS |
389 | fileEncoding = wxT("ISO-8859-1"); |
390 | convFile = & wxConvISO8859_1; | |
5d7836c4 | 391 | #endif |
7fe8059f | 392 | |
b71e9aa4 | 393 | // If SetEncoding has been called, change the output encoding. |
88a7a4e1 | 394 | if (!m_encoding.empty() && m_encoding.Lower() != fileEncoding.Lower()) |
b71e9aa4 JS |
395 | { |
396 | if (m_encoding == wxT("<System>")) | |
397 | { | |
398 | fileEncoding = wxLocale::GetSystemEncodingName(); | |
399 | } | |
400 | else | |
401 | { | |
402 | fileEncoding = m_encoding; | |
403 | } | |
404 | ||
405 | // GetSystemEncodingName may not have returned a name | |
88a7a4e1 | 406 | if (fileEncoding.empty()) |
5d7836c4 | 407 | #if wxUSE_UNICODE |
b71e9aa4 | 408 | fileEncoding = wxT("UTF-8"); |
5d7836c4 | 409 | #else |
b71e9aa4 JS |
410 | fileEncoding = wxT("ISO-8859-1"); |
411 | #endif | |
412 | convFile = new wxCSConv(fileEncoding); | |
413 | deleteConvFile = true; | |
5d7836c4 | 414 | } |
b71e9aa4 JS |
415 | |
416 | #if !wxUSE_UNICODE | |
417 | wxMBConv* convMem = wxConvCurrent; | |
418 | #else | |
419 | wxMBConv* convMem = NULL; | |
5d7836c4 | 420 | #endif |
7fe8059f | 421 | |
b71e9aa4 | 422 | wxString s ; |
5d7836c4 | 423 | s.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"), |
b71e9aa4 | 424 | (const wxChar*) version, (const wxChar*) fileEncoding ); |
5d7836c4 JS |
425 | OutputString(stream, s, NULL, NULL); |
426 | OutputString(stream, wxT("<richtext version=\"1.0.0.0\" xmlns=\"http://www.wxwidgets.org\">") , NULL, NULL); | |
427 | ||
428 | int level = 1; | |
b71e9aa4 | 429 | bool success = ExportXML(stream, convMem, convFile, *buffer, level); |
5d7836c4 JS |
430 | |
431 | OutputString(stream, wxT("\n</richtext>") , NULL, NULL); | |
432 | OutputString(stream, wxT("\n"), NULL, NULL); | |
7fe8059f | 433 | |
b71e9aa4 JS |
434 | if (deleteConvFile) |
435 | delete convFile; | |
88a7a4e1 | 436 | |
b71e9aa4 | 437 | return success; |
5d7836c4 JS |
438 | } |
439 | ||
440 | /// Recursively export an object | |
441 | bool wxRichTextXMLHandler::ExportXML(wxOutputStream& stream, wxMBConv* convMem, wxMBConv* convFile, wxRichTextObject& obj, int indent) | |
442 | { | |
443 | wxString objectName; | |
444 | if (obj.IsKindOf(CLASSINFO(wxRichTextParagraphLayoutBox))) | |
445 | objectName = wxT("paragraphlayout"); | |
446 | else if (obj.IsKindOf(CLASSINFO(wxRichTextParagraph))) | |
447 | objectName = wxT("paragraph"); | |
448 | else if (obj.IsKindOf(CLASSINFO(wxRichTextPlainText))) | |
449 | objectName = wxT("text"); | |
450 | else if (obj.IsKindOf(CLASSINFO(wxRichTextImage))) | |
451 | objectName = wxT("image"); | |
452 | else | |
453 | objectName = wxT("object"); | |
454 | ||
455 | if (obj.IsKindOf(CLASSINFO(wxRichTextPlainText))) | |
456 | { | |
457 | wxRichTextPlainText& text = (wxRichTextPlainText&) obj; | |
458 | ||
459 | OutputIndentation(stream, indent); | |
b71e9aa4 | 460 | OutputString(stream, wxT("<") + objectName, convMem, convFile); |
7fe8059f | 461 | |
5d7836c4 | 462 | wxString style = CreateStyle(obj.GetAttributes(), false); |
7fe8059f | 463 | |
b71e9aa4 | 464 | OutputString(stream, style + wxT(">"), convMem, convFile); |
7fe8059f | 465 | |
5d7836c4 | 466 | wxString str = text.GetText(); |
88a7a4e1 | 467 | if (!str.empty() && (str[0] == wxT(' ') || str[str.length()-1] == wxT(' '))) |
5d7836c4 | 468 | { |
b71e9aa4 | 469 | OutputString(stream, wxT("\""), convMem, convFile); |
5d7836c4 | 470 | OutputStringEnt(stream, str, convMem, convFile); |
b71e9aa4 | 471 | OutputString(stream, wxT("\""), convMem, convFile); |
5d7836c4 JS |
472 | } |
473 | else | |
474 | OutputStringEnt(stream, str, convMem, convFile); | |
475 | } | |
476 | else if (obj.IsKindOf(CLASSINFO(wxRichTextImage))) | |
477 | { | |
478 | wxRichTextImage& imageObj = (wxRichTextImage&) obj; | |
479 | ||
480 | if (imageObj.GetImage().Ok() && !imageObj.GetImageBlock().Ok()) | |
481 | imageObj.MakeBlock(); | |
482 | ||
483 | OutputIndentation(stream, indent); | |
b71e9aa4 | 484 | OutputString(stream, wxT("<") + objectName, convMem, convFile); |
5d7836c4 JS |
485 | if (!imageObj.GetImageBlock().Ok()) |
486 | { | |
487 | // No data | |
b71e9aa4 | 488 | OutputString(stream, wxT(">"), convMem, convFile); |
5d7836c4 JS |
489 | } |
490 | else | |
491 | { | |
b71e9aa4 | 492 | OutputString(stream, wxString::Format(wxT(" imagetype=\"%d\">"), (int) imageObj.GetImageBlock().GetImageType())); |
5d7836c4 JS |
493 | } |
494 | ||
495 | OutputIndentation(stream, indent+1); | |
b71e9aa4 | 496 | OutputString(stream, wxT("<data>"), convMem, convFile); |
5d7836c4 JS |
497 | |
498 | imageObj.GetImageBlock().WriteHex(stream); | |
499 | ||
b71e9aa4 | 500 | OutputString(stream, wxT("</data>"), convMem, convFile); |
5d7836c4 JS |
501 | } |
502 | else if (obj.IsKindOf(CLASSINFO(wxRichTextCompositeObject))) | |
503 | { | |
504 | OutputIndentation(stream, indent); | |
b71e9aa4 | 505 | OutputString(stream, wxT("<") + objectName, convMem, convFile); |
7fe8059f | 506 | |
5d7836c4 JS |
507 | bool isPara = false; |
508 | if (objectName == wxT("paragraph") || objectName == wxT("paragraphlayout")) | |
509 | isPara = true; | |
510 | ||
511 | wxString style = CreateStyle(obj.GetAttributes(), isPara); | |
7fe8059f | 512 | |
0ca07313 JS |
513 | if (objectName == wxT("paragraphlayout") && ((wxRichTextParagraphLayoutBox&) obj).GetPartialParagraph()) |
514 | style << wxT(" partialparagraph=\"true\""); | |
515 | ||
b71e9aa4 | 516 | OutputString(stream, style + wxT(">"), convMem, convFile); |
7fe8059f | 517 | |
5d7836c4 JS |
518 | wxRichTextCompositeObject& composite = (wxRichTextCompositeObject&) obj; |
519 | size_t i; | |
520 | for (i = 0; i < composite.GetChildCount(); i++) | |
521 | { | |
522 | wxRichTextObject* child = composite.GetChild(i); | |
523 | ExportXML(stream, convMem, convFile, *child, indent+1); | |
524 | } | |
525 | } | |
526 | ||
527 | if (objectName != wxT("text")) | |
528 | OutputIndentation(stream, indent); | |
529 | ||
b71e9aa4 | 530 | OutputString(stream, wxT("</") + objectName + wxT(">"), convMem, convFile); |
5d7836c4 JS |
531 | |
532 | return true; | |
533 | } | |
534 | ||
535 | /// Create style parameters | |
536 | wxString wxRichTextXMLHandler::CreateStyle(const wxTextAttrEx& attr, bool isPara) | |
537 | { | |
538 | wxString str; | |
0ca07313 | 539 | if (attr.HasTextColour() && attr.GetTextColour().Ok()) |
5d7836c4 JS |
540 | { |
541 | str << wxT(" textcolor=\"#") << ColourToHexString(attr.GetTextColour()) << wxT("\""); | |
542 | } | |
0ca07313 | 543 | if (attr.HasBackgroundColour() && attr.GetBackgroundColour().Ok()) |
5d7836c4 JS |
544 | { |
545 | str << wxT(" bgcolor=\"#") << ColourToHexString(attr.GetBackgroundColour()) << wxT("\""); | |
546 | } | |
547 | ||
548 | if (attr.GetFont().Ok()) | |
549 | { | |
0ca07313 JS |
550 | if (attr.HasSize()) |
551 | str << wxT(" fontsize=\"") << attr.GetFont().GetPointSize() << wxT("\""); | |
552 | ||
553 | //if (attr.HasFamily()) | |
554 | // str << wxT(" fontfamily=\"") << attr.GetFont().GetFamily() << wxT("\""); | |
555 | ||
556 | if (attr.HasItalic()) | |
557 | str << wxT(" fontstyle=\"") << attr.GetFont().GetStyle() << wxT("\""); | |
558 | ||
559 | if (attr.HasWeight()) | |
560 | str << wxT(" fontweight=\"") << attr.GetFont().GetWeight() << wxT("\""); | |
561 | ||
562 | if (attr.HasUnderlined()) | |
563 | str << wxT(" fontunderlined=\"") << (int) attr.GetFont().GetUnderlined() << wxT("\""); | |
564 | ||
565 | if (attr.HasFaceName()) | |
566 | str << wxT(" fontface=\"") << attr.GetFont().GetFaceName() << wxT("\""); | |
5d7836c4 JS |
567 | } |
568 | ||
7fe8059f | 569 | if (!attr.GetCharacterStyleName().empty()) |
5d7836c4 JS |
570 | str << wxT(" charactertyle=\"") << wxString(attr.GetCharacterStyleName()) << wxT("\""); |
571 | ||
572 | if (isPara) | |
573 | { | |
0ca07313 JS |
574 | if (attr.HasAlignment()) |
575 | str << wxT(" alignment=\"") << (int) attr.GetAlignment() << wxT("\""); | |
576 | ||
577 | if (attr.HasLeftIndent()) | |
578 | { | |
579 | str << wxT(" leftindent=\"") << (int) attr.GetLeftIndent() << wxT("\""); | |
580 | str << wxT(" leftsubindent=\"") << (int) attr.GetLeftSubIndent() << wxT("\""); | |
581 | } | |
582 | ||
583 | if (attr.HasRightIndent()) | |
584 | str << wxT(" rightindent=\"") << (int) attr.GetRightIndent() << wxT("\""); | |
585 | ||
586 | if (attr.HasParagraphSpacingAfter()) | |
587 | str << wxT(" parspacingafter=\"") << (int) attr.GetParagraphSpacingAfter() << wxT("\""); | |
588 | ||
589 | if (attr.HasParagraphSpacingBefore()) | |
590 | str << wxT(" parspacingbefore=\"") << (int) attr.GetParagraphSpacingBefore() << wxT("\""); | |
591 | ||
592 | if (attr.HasLineSpacing()) | |
593 | str << wxT(" linespacing=\"") << (int) attr.GetLineSpacing() << wxT("\""); | |
594 | ||
595 | if (attr.HasBulletStyle()) | |
596 | str << wxT(" bulletstyle=\"") << (int) attr.GetBulletStyle() << wxT("\""); | |
597 | ||
598 | if (attr.HasBulletNumber()) | |
599 | str << wxT(" bulletnumber=\"") << (int) attr.GetBulletNumber() << wxT("\""); | |
600 | ||
601 | if (attr.HasBulletSymbol()) | |
602 | str << wxT(" bulletsymbol=\"") << wxString(attr.GetBulletSymbol()) << wxT("\""); | |
5d7836c4 | 603 | |
7fe8059f | 604 | if (!attr.GetParagraphStyleName().empty()) |
5d7836c4 | 605 | str << wxT(" parstyle=\"") << wxString(attr.GetParagraphStyleName()) << wxT("\""); |
0ca07313 JS |
606 | |
607 | if (attr.HasTabs()) | |
608 | { | |
609 | str << wxT(" tabs=\""); | |
610 | size_t i; | |
611 | for (i = 0; i < attr.GetTabs().GetCount(); i++) | |
612 | { | |
613 | if (i > 0) | |
614 | str << wxT(","); | |
615 | str << attr.GetTabs()[i]; | |
616 | } | |
617 | str << wxT("\""); | |
618 | } | |
5d7836c4 JS |
619 | } |
620 | ||
621 | return str; | |
622 | } | |
623 | ||
624 | /// Get style parameters | |
625 | bool wxRichTextXMLHandler::GetStyle(wxTextAttrEx& attr, wxXmlNode* node, bool isPara) | |
626 | { | |
627 | wxString fontFacename; | |
628 | int fontSize = 12; | |
629 | int fontFamily = wxDEFAULT; | |
630 | int fontWeight = wxNORMAL; | |
631 | int fontStyle = wxNORMAL; | |
632 | bool fontUnderlined = false; | |
633 | ||
0ca07313 JS |
634 | int fontFlags = 0; |
635 | ||
5d7836c4 | 636 | fontFacename = node->GetPropVal(wxT("fontface"), wxEmptyString); |
0ca07313 JS |
637 | if (!fontFacename.IsEmpty()) |
638 | fontFlags |= wxTEXT_ATTR_FONT_FACE; | |
5d7836c4 | 639 | |
0ca07313 JS |
640 | wxString value; |
641 | //value = node->GetPropVal(wxT("fontfamily"), wxEmptyString); | |
642 | //if (!value.empty()) | |
643 | // fontFamily = wxAtoi(value); | |
5d7836c4 JS |
644 | |
645 | value = node->GetPropVal(wxT("fontstyle"), wxEmptyString); | |
7fe8059f | 646 | if (!value.empty()) |
0ca07313 | 647 | { |
5d7836c4 | 648 | fontStyle = wxAtoi(value); |
0ca07313 JS |
649 | fontFlags |= wxTEXT_ATTR_FONT_ITALIC; |
650 | } | |
5d7836c4 JS |
651 | |
652 | value = node->GetPropVal(wxT("fontsize"), wxEmptyString); | |
7fe8059f | 653 | if (!value.empty()) |
0ca07313 | 654 | { |
5d7836c4 | 655 | fontSize = wxAtoi(value); |
0ca07313 JS |
656 | fontFlags |= wxTEXT_ATTR_FONT_SIZE; |
657 | } | |
5d7836c4 JS |
658 | |
659 | value = node->GetPropVal(wxT("fontweight"), wxEmptyString); | |
7fe8059f | 660 | if (!value.empty()) |
0ca07313 | 661 | { |
5d7836c4 | 662 | fontWeight = wxAtoi(value); |
0ca07313 JS |
663 | fontFlags |= wxTEXT_ATTR_FONT_WEIGHT; |
664 | } | |
5d7836c4 JS |
665 | |
666 | value = node->GetPropVal(wxT("fontunderlined"), wxEmptyString); | |
7fe8059f | 667 | if (!value.empty()) |
0ca07313 | 668 | { |
5d7836c4 | 669 | fontUnderlined = wxAtoi(value) != 0; |
0ca07313 JS |
670 | fontFlags |= wxTEXT_ATTR_FONT_UNDERLINE; |
671 | } | |
672 | ||
673 | attr.SetFlags(fontFlags); | |
5d7836c4 | 674 | |
0ca07313 JS |
675 | if (attr.HasFlag(wxTEXT_ATTR_FONT)) |
676 | attr.SetFont(* wxTheFontList->FindOrCreateFont(fontSize, fontFamily, fontStyle, fontWeight, fontUnderlined, fontFacename)); | |
677 | ||
678 | // Restore correct font flags | |
679 | attr.SetFlags(fontFlags); | |
5d7836c4 JS |
680 | |
681 | value = node->GetPropVal(wxT("textcolor"), wxEmptyString); | |
7fe8059f | 682 | if (!value.empty()) |
5d7836c4 JS |
683 | { |
684 | if (value[0] == wxT('#')) | |
685 | attr.SetTextColour(HexStringToColour(value.Mid(1))); | |
686 | else | |
687 | attr.SetTextColour(value); | |
688 | } | |
689 | ||
690 | value = node->GetPropVal(wxT("backgroundcolor"), wxEmptyString); | |
7fe8059f | 691 | if (!value.empty()) |
5d7836c4 JS |
692 | { |
693 | if (value[0] == wxT('#')) | |
694 | attr.SetBackgroundColour(HexStringToColour(value.Mid(1))); | |
695 | else | |
696 | attr.SetBackgroundColour(value); | |
697 | } | |
698 | ||
699 | value = node->GetPropVal(wxT("characterstyle"), wxEmptyString); | |
7fe8059f | 700 | if (!value.empty()) |
5d7836c4 JS |
701 | attr.SetCharacterStyleName(value); |
702 | ||
703 | // Set paragraph attributes | |
704 | if (isPara) | |
705 | { | |
706 | value = node->GetPropVal(wxT("alignment"), wxEmptyString); | |
7fe8059f | 707 | if (!value.empty()) |
5d7836c4 JS |
708 | attr.SetAlignment((wxTextAttrAlignment) wxAtoi(value)); |
709 | ||
710 | int leftSubIndent = 0; | |
711 | int leftIndent = 0; | |
0ca07313 JS |
712 | bool hasLeftIndent = false; |
713 | ||
5d7836c4 | 714 | value = node->GetPropVal(wxT("leftindent"), wxEmptyString); |
7fe8059f | 715 | if (!value.empty()) |
0ca07313 | 716 | { |
5d7836c4 | 717 | leftIndent = wxAtoi(value); |
0ca07313 JS |
718 | hasLeftIndent = true; |
719 | } | |
720 | ||
5d7836c4 | 721 | value = node->GetPropVal(wxT("leftsubindent"), wxEmptyString); |
7fe8059f | 722 | if (!value.empty()) |
0ca07313 | 723 | { |
5d7836c4 | 724 | leftSubIndent = wxAtoi(value); |
0ca07313 JS |
725 | hasLeftIndent = true; |
726 | } | |
727 | ||
728 | if (hasLeftIndent) | |
729 | attr.SetLeftIndent(leftIndent, leftSubIndent); | |
5d7836c4 JS |
730 | |
731 | value = node->GetPropVal(wxT("rightindent"), wxEmptyString); | |
7fe8059f | 732 | if (!value.empty()) |
5d7836c4 JS |
733 | attr.SetRightIndent(wxAtoi(value)); |
734 | ||
735 | value = node->GetPropVal(wxT("parspacingbefore"), wxEmptyString); | |
7fe8059f | 736 | if (!value.empty()) |
5d7836c4 JS |
737 | attr.SetParagraphSpacingBefore(wxAtoi(value)); |
738 | ||
739 | value = node->GetPropVal(wxT("parspacingafter"), wxEmptyString); | |
7fe8059f | 740 | if (!value.empty()) |
5d7836c4 JS |
741 | attr.SetParagraphSpacingAfter(wxAtoi(value)); |
742 | ||
743 | value = node->GetPropVal(wxT("linespacing"), wxEmptyString); | |
7fe8059f | 744 | if (!value.empty()) |
5d7836c4 JS |
745 | attr.SetLineSpacing(wxAtoi(value)); |
746 | ||
747 | value = node->GetPropVal(wxT("bulletstyle"), wxEmptyString); | |
7fe8059f | 748 | if (!value.empty()) |
5d7836c4 JS |
749 | attr.SetBulletStyle(wxAtoi(value)); |
750 | ||
751 | value = node->GetPropVal(wxT("bulletnumber"), wxEmptyString); | |
7fe8059f | 752 | if (!value.empty()) |
5d7836c4 JS |
753 | attr.SetBulletNumber(wxAtoi(value)); |
754 | ||
755 | value = node->GetPropVal(wxT("bulletsymbol"), wxEmptyString); | |
7fe8059f | 756 | if (!value.empty()) |
5d7836c4 JS |
757 | attr.SetBulletSymbol(value[0]); |
758 | ||
759 | value = node->GetPropVal(wxT("parstyle"), wxEmptyString); | |
7fe8059f | 760 | if (!value.empty()) |
5d7836c4 | 761 | attr.SetParagraphStyleName(value); |
0ca07313 JS |
762 | |
763 | value = node->GetPropVal(wxT("tabs"), wxEmptyString); | |
764 | if (!value.empty()) | |
765 | { | |
766 | wxArrayInt tabs; | |
767 | wxStringTokenizer tkz(value, wxT(",")); | |
768 | while (tkz.HasMoreTokens()) | |
769 | { | |
770 | wxString token = tkz.GetNextToken(); | |
771 | tabs.Add(wxAtoi(token)); | |
772 | } | |
773 | attr.SetTabs(tabs); | |
774 | } | |
5d7836c4 JS |
775 | } |
776 | ||
777 | return true; | |
778 | } | |
779 | ||
780 | #endif | |
b71e9aa4 | 781 | // wxUSE_STREAMS |
88a7a4e1 | 782 | |
5d7836c4 | 783 | #endif |
fcdbeefa | 784 | // wxUSE_RICHTEXT && wxUSE_XML |