]> git.saurik.com Git - wxWidgets.git/blame - src/richtext/richtexthtml.cpp
New build slave for wxQt
[wxWidgets.git] / src / richtext / richtexthtml.cpp
CommitLineData
b71e9aa4 1/////////////////////////////////////////////////////////////////////////////
40989e46 2// Name: src/richtext/richtexthtml.cpp
b71e9aa4
JS
3// Purpose: HTML I/O for wxRichTextCtrl
4// Author: Julian Smart
5// Modified by:
6// Created: 2005-09-30
7// RCS-ID: $Id$
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__
40989e46 16 #pragma hdrstop
b71e9aa4
JS
17#endif
18
19#if wxUSE_RICHTEXT
20
21#include "wx/richtext/richtexthtml.h"
50f65288 22#include "wx/richtext/richtextstyles.h"
b71e9aa4
JS
23
24#ifndef WX_PRECOMP
b71e9aa4
JS
25#endif
26
27#include "wx/filename.h"
28#include "wx/wfstream.h"
29#include "wx/txtstrm.h"
30
d2d0adc7
JS
31#if wxUSE_FILESYSTEM
32#include "wx/filesys.h"
33#include "wx/fs_mem.h"
34#endif
35
b71e9aa4
JS
36IMPLEMENT_DYNAMIC_CLASS(wxRichTextHTMLHandler, wxRichTextFileHandler)
37
d2d0adc7
JS
38int wxRichTextHTMLHandler::sm_fileCounter = 1;
39
50f65288
WS
40wxRichTextHTMLHandler::wxRichTextHTMLHandler(const wxString& name, const wxString& ext, int type)
41 : wxRichTextFileHandler(name, ext, type), m_buffer(NULL), m_font(false), m_inTable(false)
42{
43 m_fontSizeMapping.Add(8);
44 m_fontSizeMapping.Add(10);
45 m_fontSizeMapping.Add(13);
46 m_fontSizeMapping.Add(17);
47 m_fontSizeMapping.Add(22);
48 m_fontSizeMapping.Add(30);
49 m_fontSizeMapping.Add(100);
50}
51
b71e9aa4
JS
52/// Can we handle this filename (if using files)? By default, checks the extension.
53bool wxRichTextHTMLHandler::CanHandle(const wxString& filename) const
54{
55 wxString path, file, ext;
bd365871 56 wxFileName::SplitPath(filename, & path, & file, & ext);
b71e9aa4
JS
57
58 return (ext.Lower() == wxT("html") || ext.Lower() == wxT("htm"));
59}
60
61
62#if wxUSE_STREAMS
63bool wxRichTextHTMLHandler::DoLoadFile(wxRichTextBuffer *WXUNUSED(buffer), wxInputStream& WXUNUSED(stream))
64{
65 return false;
66}
67
68/*
69 * We need to output only _changes_ in character formatting.
70 */
71
72bool wxRichTextHTMLHandler::DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream)
73{
50f65288
WS
74 m_buffer = buffer;
75
d2d0adc7
JS
76 ClearTemporaryImageLocations();
77
b71e9aa4 78 buffer->Defragment();
b72812a6 79
2c48f032
JS
80#if wxUSE_UNICODE
81 wxCSConv* customEncoding = NULL;
82 wxMBConv* conv = NULL;
83 if (!GetEncoding().IsEmpty())
b71e9aa4 84 {
2c48f032
JS
85 customEncoding = new wxCSConv(GetEncoding());
86 if (!customEncoding->IsOk())
b71e9aa4 87 {
5276b0a5 88 wxDELETE(customEncoding);
2c48f032
JS
89 }
90 }
91 if (customEncoding)
92 conv = customEncoding;
93 else
94 conv = & wxConvUTF8;
95#endif
40989e46 96
2c48f032
JS
97 {
98#if wxUSE_UNICODE
99 wxTextOutputStream str(stream, wxEOL_NATIVE, *conv);
100#else
101 wxTextOutputStream str(stream, wxEOL_NATIVE);
102#endif
4fdc2c5f 103
2c48f032
JS
104 wxTextAttr currentParaStyle = buffer->GetAttributes();
105 wxTextAttr currentCharStyle = buffer->GetAttributes();
4fdc2c5f 106
2c48f032
JS
107 if ((GetFlags() & wxRICHTEXT_HANDLER_NO_HEADER_FOOTER) == 0)
108 str << wxT("<html><head></head><body>\n");
4fdc2c5f 109
2c48f032 110 OutputFont(currentParaStyle, str);
4fdc2c5f 111
2c48f032
JS
112 m_font = false;
113 m_inTable = false;
4fdc2c5f 114
2c48f032
JS
115 m_indents.Clear();
116 m_listTypes.Clear();
4fdc2c5f 117
2c48f032
JS
118 wxRichTextObjectList::compatibility_iterator node = buffer->GetChildren().GetFirst();
119 while (node)
120 {
121 wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph);
122 wxASSERT (para != NULL);
4fdc2c5f 123
2c48f032 124 if (para)
b71e9aa4 125 {
2c48f032 126 wxTextAttr paraStyle(para->GetCombinedAttributes());
4fdc2c5f 127
2c48f032 128 BeginParagraphFormatting(currentParaStyle, paraStyle, str);
4fdc2c5f 129
2c48f032
JS
130 wxRichTextObjectList::compatibility_iterator node2 = para->GetChildren().GetFirst();
131 while (node2)
b71e9aa4 132 {
2c48f032
JS
133 wxRichTextObject* obj = node2->GetData();
134 wxRichTextPlainText* textObj = wxDynamicCast(obj, wxRichTextPlainText);
135 if (textObj && !textObj->IsEmpty())
136 {
137 wxTextAttr charStyle(para->GetCombinedAttributes(obj->GetAttributes()));
138 BeginCharacterFormatting(currentCharStyle, charStyle, paraStyle, str);
4fdc2c5f 139
2c48f032 140 wxString text = textObj->GetText();
4fdc2c5f 141
2c48f032
JS
142 if (charStyle.HasTextEffects() && (charStyle.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS))
143 text.MakeUpper();
4fdc2c5f 144
2c48f032
JS
145 wxString toReplace = wxRichTextLineBreakChar;
146 text.Replace(toReplace, wxT("<br>"));
4fdc2c5f 147
2c48f032 148 str << text;
4fdc2c5f 149
2c48f032
JS
150 EndCharacterFormatting(currentCharStyle, charStyle, paraStyle, str);
151 }
4fdc2c5f 152
2c48f032
JS
153 wxRichTextImage* image = wxDynamicCast(obj, wxRichTextImage);
154 if( image && (!image->IsEmpty() || image->GetImageBlock().GetData()))
155 WriteImage( image, stream );
4fdc2c5f 156
2c48f032 157 node2 = node2->GetNext();
b71e9aa4 158 }
4fdc2c5f 159
2c48f032 160 EndParagraphFormatting(currentParaStyle, paraStyle, str);
4fdc2c5f 161
2c48f032 162 str << wxT("\n");
b71e9aa4 163 }
2c48f032 164 node = node->GetNext();
b71e9aa4 165 }
4fdc2c5f 166
2c48f032 167 CloseLists(-1, str);
4fdc2c5f 168
2c48f032 169 str << wxT("</font>");
4fdc2c5f 170
2c48f032
JS
171 if ((GetFlags() & wxRICHTEXT_HANDLER_NO_HEADER_FOOTER) == 0)
172 str << wxT("</body></html>");
4fdc2c5f 173
2c48f032 174 str << wxT("\n");
b71e9aa4 175 }
b72812a6 176
2c48f032
JS
177#if wxUSE_UNICODE
178 if (customEncoding)
179 delete customEncoding;
180#endif
40989e46 181
50f65288
WS
182 m_buffer = NULL;
183
b71e9aa4
JS
184 return true;
185}
186
44cc96a8 187void wxRichTextHTMLHandler::BeginCharacterFormatting(const wxTextAttr& currentStyle, const wxTextAttr& thisStyle, const wxTextAttr& WXUNUSED(paraStyle), wxTextOutputStream& str)
b71e9aa4 188{
2dec6761 189 wxString style;
40989e46 190
d2d0adc7 191 // Is there any change in the font properties of the item?
44cc96a8 192 if (thisStyle.GetFontFaceName() != currentStyle.GetFontFaceName())
d2d0adc7 193 {
44cc96a8 194 wxString faceName(thisStyle.GetFontFaceName());
d2d0adc7
JS
195 style += wxString::Format(wxT(" face=\"%s\""), faceName.c_str());
196 }
44cc96a8
JS
197 if (thisStyle.GetFontSize() != currentStyle.GetFontSize())
198 style += wxString::Format(wxT(" size=\"%ld\""), PtToSize(thisStyle.GetFontSize()));
d2d0adc7
JS
199 if (thisStyle.GetTextColour() != currentStyle.GetTextColour() )
200 {
201 wxString color(thisStyle.GetTextColour().GetAsString(wxC2S_HTML_SYNTAX));
202 style += wxString::Format(wxT(" color=\"%s\""), color.c_str());
203 }
40989e46 204
d2d0adc7 205 if (style.size())
21e354f1
JS
206 {
207 str << wxString::Format(wxT("<font %s >"), style.c_str());
208 m_font = true;
209 }
40989e46 210
44cc96a8 211 if (thisStyle.GetFontWeight() == wxBOLD)
2dec6761 212 str << wxT("<b>");
44cc96a8 213 if (thisStyle.GetFontStyle() == wxITALIC)
2dec6761 214 str << wxT("<i>");
44cc96a8 215 if (thisStyle.GetFontUnderlined())
2dec6761 216 str << wxT("<u>");
b72812a6 217
d2d0adc7
JS
218 if (thisStyle.HasURL())
219 str << wxT("<a href=\"") << thisStyle.GetURL() << wxT("\">");
b71e9aa4
JS
220}
221
44cc96a8 222void wxRichTextHTMLHandler::EndCharacterFormatting(const wxTextAttr& WXUNUSED(currentStyle), const wxTextAttr& thisStyle, const wxTextAttr& WXUNUSED(paraStyle), wxTextOutputStream& stream)
b71e9aa4 223{
d2d0adc7 224 if (thisStyle.HasURL())
50f65288 225 stream << wxT("</a>");
d2d0adc7 226
44cc96a8 227 if (thisStyle.GetFontUnderlined())
50f65288 228 stream << wxT("</u>");
44cc96a8 229 if (thisStyle.GetFontStyle() == wxITALIC)
50f65288 230 stream << wxT("</i>");
44cc96a8 231 if (thisStyle.GetFontWeight() == wxBOLD)
50f65288 232 stream << wxT("</b>");
40989e46 233
d2d0adc7 234 if (m_font)
2dec6761
JS
235 {
236 m_font = false;
50f65288 237 stream << wxT("</font>");
2dec6761
JS
238 }
239}
b71e9aa4 240
50f65288 241/// Begin paragraph formatting
44cc96a8 242void wxRichTextHTMLHandler::BeginParagraphFormatting(const wxTextAttr& WXUNUSED(currentStyle), const wxTextAttr& thisStyle, wxTextOutputStream& str)
2dec6761 243{
42688aea
JS
244 if (thisStyle.HasPageBreak())
245 {
42688aea
JS
246 str << wxT("<div style=\"page-break-after:always\"></div>\n");
247 }
40989e46 248
7947a11d 249 if (thisStyle.HasLeftIndent() && thisStyle.GetLeftIndent() != 0)
2dec6761 250 {
50f65288 251 if (thisStyle.HasBulletStyle())
2dec6761 252 {
50f65288
WS
253 int indent = thisStyle.GetLeftIndent();
254
255 // Close levels high than this
256 CloseLists(indent, str);
b72812a6 257
50f65288
WS
258 if (m_indents.GetCount() > 0 && indent == m_indents.Last())
259 {
260 // Same level, no need to start a new list
261 }
262 else if (m_indents.GetCount() == 0 || indent > m_indents.Last())
263 {
264 m_indents.Add(indent);
b72812a6 265
50f65288
WS
266 wxString tag;
267 int listType = TypeOfList(thisStyle, tag);
268 m_listTypes.Add(listType);
b72812a6 269
e5163682
JS
270 // wxHTML needs an extra <p> before a list when using <p> ... </p> in previous paragraphs.
271 // TODO: pass a flag that indicates we're using wxHTML.
272 str << wxT("<p>\n");
b72812a6 273
50f65288
WS
274 str << tag;
275 }
b72812a6 276
50f65288 277 str << wxT("<li> ");
2dec6761 278 }
2dec6761
JS
279 else
280 {
50f65288 281 CloseLists(-1, str);
b72812a6 282
50f65288 283 wxString align = GetAlignment(thisStyle);
2b2c1044
JS
284 str << wxString::Format(wxT("<p align=\"%s\""), align.c_str());
285
4fdc2c5f
JS
286 wxString styleStr;
287
288 if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) && thisStyle.HasParagraphSpacingBefore())
289 {
290 float spacingBeforeMM = thisStyle.GetParagraphSpacingBefore() / 10.0;
291
292 styleStr += wxString::Format(wxT("margin-top: %.2fmm; "), spacingBeforeMM);
293 }
294 if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) && thisStyle.HasParagraphSpacingAfter())
295 {
296 float spacingAfterMM = thisStyle.GetParagraphSpacingAfter() / 10.0;
297
298 styleStr += wxString::Format(wxT("margin-bottom: %.2fmm; "), spacingAfterMM);
299 }
300
301 float indentLeftMM = (thisStyle.GetLeftIndent() + thisStyle.GetLeftSubIndent())/10.0;
302 if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) && (indentLeftMM > 0.0))
303 {
304 styleStr += wxString::Format(wxT("margin-left: %.2fmm; "), indentLeftMM);
305 }
306 float indentRightMM = thisStyle.GetRightIndent()/10.0;
307 if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) && thisStyle.HasRightIndent() && (indentRightMM > 0.0))
308 {
309 styleStr += wxString::Format(wxT("margin-right: %.2fmm; "), indentRightMM);
310 }
311 // First line indentation
312 float firstLineIndentMM = - thisStyle.GetLeftSubIndent() / 10.0;
313 if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) && (firstLineIndentMM > 0.0))
314 {
315 styleStr += wxString::Format(wxT("text-indent: %.2fmm; "), firstLineIndentMM);
316 }
317
318 if (!styleStr.IsEmpty())
319 str << wxT(" style=\"") << styleStr << wxT("\"");
2b2c1044
JS
320
321 str << wxT(">");
40989e46 322
50f65288 323 // TODO: convert to pixels
945c909d 324 int indentPixels = static_cast<int>(indentLeftMM*10/4);
4fdc2c5f
JS
325
326 if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) == 0)
327 {
328 // Use a table to do indenting if we don't have CSS
329 str << wxString::Format(wxT("<table border=0 cellpadding=0 cellspacing=0><tr><td width=\"%d\"></td><td>"), indentPixels);
330 m_inTable = true;
331 }
50f65288 332
4fdc2c5f 333 if (((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) == 0) && (thisStyle.GetLeftSubIndent() < 0))
d2d0adc7 334 {
50f65288 335 str << SymbolicIndent( - thisStyle.GetLeftSubIndent());
d2d0adc7 336 }
2dec6761
JS
337 }
338 }
50f65288
WS
339 else
340 {
341 CloseLists(-1, str);
342
343 wxString align = GetAlignment(thisStyle);
2b2c1044
JS
344 str << wxString::Format(wxT("<p align=\"%s\""), align.c_str());
345
4fdc2c5f
JS
346 wxString styleStr;
347
348 if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) && thisStyle.HasParagraphSpacingBefore())
349 {
350 float spacingBeforeMM = thisStyle.GetParagraphSpacingBefore() / 10.0;
351
352 styleStr += wxString::Format(wxT("margin-top: %.2fmm; "), spacingBeforeMM);
353 }
354 if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) && thisStyle.HasParagraphSpacingAfter())
355 {
356 float spacingAfterMM = thisStyle.GetParagraphSpacingAfter() / 10.0;
357
358 styleStr += wxString::Format(wxT("margin-bottom: %.2fmm; "), spacingAfterMM);
359 }
360
361 if (!styleStr.IsEmpty())
362 str << wxT(" style=\"") << styleStr << wxT("\"");
2b2c1044
JS
363
364 str << wxT(">");
b72812a6 365 }
7e7ced16 366 OutputFont(thisStyle, str);
2dec6761 367}
50f65288
WS
368
369/// End paragraph formatting
44cc96a8 370void wxRichTextHTMLHandler::EndParagraphFormatting(const wxTextAttr& WXUNUSED(currentStyle), const wxTextAttr& thisStyle, wxTextOutputStream& stream)
2dec6761 371{
7e7ced16
JS
372 if (thisStyle.HasFont())
373 stream << wxT("</font>");
374
50f65288 375 if (m_inTable)
2dec6761 376 {
c433798e 377 stream << wxT("</td></tr></table></p>\n");
50f65288 378 m_inTable = false;
2dec6761 379 }
e5163682 380 else if (!thisStyle.HasBulletStyle())
c433798e 381 stream << wxT("</p>\n");
2dec6761
JS
382}
383
50f65288
WS
384/// Closes lists to level (-1 means close all)
385void wxRichTextHTMLHandler::CloseLists(int level, wxTextOutputStream& str)
386{
387 // Close levels high than this
388 int i = m_indents.GetCount()-1;
389 while (i >= 0)
390 {
391 int l = m_indents[i];
392 if (l > level)
393 {
394 if (m_listTypes[i] == 0)
395 str << wxT("</ol>");
396 else
397 str << wxT("</ul>");
398 m_indents.RemoveAt(i);
399 m_listTypes.RemoveAt(i);
400 }
401 else
402 break;
403 i --;
404 }
405}
406
407/// Output font tag
44cc96a8 408void wxRichTextHTMLHandler::OutputFont(const wxTextAttr& style, wxTextOutputStream& stream)
2dec6761 409{
50f65288
WS
410 if (style.HasFont())
411 {
44cc96a8 412 stream << wxString::Format(wxT("<font face=\"%s\" size=\"%ld\""), style.GetFontFaceName().c_str(), PtToSize(style.GetFontSize()));
b72812a6
JS
413 if (style.HasTextColour())
414 stream << wxString::Format(wxT(" color=\"%s\""), style.GetTextColour().GetAsString(wxC2S_HTML_SYNTAX).c_str());
415 stream << wxT(" >");
50f65288 416 }
2dec6761
JS
417}
418
44cc96a8 419int wxRichTextHTMLHandler::TypeOfList( const wxTextAttr& thisStyle, wxString& tag )
2dec6761 420{
d2d0adc7
JS
421 // We can use number attribute of li tag but not all the browsers support it.
422 // also wxHtmlWindow doesn't support type attribute.
40989e46 423
50f65288 424 bool m_is_ul = false;
d2d0adc7 425 if (thisStyle.GetBulletStyle() == (wxTEXT_ATTR_BULLET_STYLE_ARABIC|wxTEXT_ATTR_BULLET_STYLE_PERIOD))
2dec6761 426 tag = wxT("<ol type=\"1\">");
d2d0adc7 427 else if (thisStyle.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER)
2dec6761 428 tag = wxT("<ol type=\"A\">");
d2d0adc7 429 else if (thisStyle.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER)
2dec6761 430 tag = wxT("<ol type=\"a\">");
d2d0adc7 431 else if (thisStyle.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER)
2dec6761 432 tag = wxT("<ol type=\"I\">");
d2d0adc7 433 else if (thisStyle.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER)
2dec6761 434 tag = wxT("<ol type=\"i\">");
b71e9aa4
JS
435 else
436 {
2dec6761
JS
437 tag = wxT("<ul>");
438 m_is_ul = true;
b71e9aa4 439 }
b72812a6 440
50f65288
WS
441 if (m_is_ul)
442 return 1;
443 else
444 return 0;
b71e9aa4
JS
445}
446
44cc96a8 447wxString wxRichTextHTMLHandler::GetAlignment( const wxTextAttr& thisStyle )
2dec6761
JS
448{
449 switch( thisStyle.GetAlignment() )
450 {
451 case wxTEXT_ALIGNMENT_LEFT:
452 return wxT("left");
453 case wxTEXT_ALIGNMENT_RIGHT:
454 return wxT("right");
455 case wxTEXT_ALIGNMENT_CENTER:
456 return wxT("center");
457 case wxTEXT_ALIGNMENT_JUSTIFIED:
458 return wxT("justify");
459 default:
460 return wxT("left");
461 }
462}
463
d2d0adc7 464void wxRichTextHTMLHandler::WriteImage(wxRichTextImage* image, wxOutputStream& stream)
2dec6761
JS
465{
466 wxTextOutputStream str(stream);
40989e46 467
2dec6761 468 str << wxT("<img src=\"");
40989e46 469
d2d0adc7
JS
470#if wxUSE_FILESYSTEM
471 if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY)
472 {
473 if (!image->GetImage().Ok() && image->GetImageBlock().GetData())
474 image->LoadFromBlock();
475 if (image->GetImage().Ok() && !image->GetImageBlock().GetData())
476 image->MakeBlock();
477
478 if (image->GetImage().Ok())
b72812a6 479 {
d2d0adc7 480 wxString ext(image->GetImageBlock().GetExtension());
86501081 481 wxString tempFilename(wxString::Format(wxT("image%d.%s"), sm_fileCounter, ext));
d2d0adc7 482 wxMemoryFSHandler::AddFile(tempFilename, image->GetImage(), image->GetImageBlock().GetImageType());
b72812a6 483
d2d0adc7 484 m_imageLocations.Add(tempFilename);
b72812a6 485
d2d0adc7
JS
486 str << wxT("memory:") << tempFilename;
487 }
488 else
489 str << wxT("memory:?");
40989e46 490
d2d0adc7
JS
491 sm_fileCounter ++;
492 }
493 else if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_FILES)
494 {
495 if (!image->GetImage().Ok() && image->GetImageBlock().GetData())
496 image->LoadFromBlock();
497 if (image->GetImage().Ok() && !image->GetImageBlock().GetData())
498 image->MakeBlock();
499
500 if (image->GetImage().Ok())
b72812a6 501 {
d2d0adc7
JS
502 wxString tempDir(GetTempDir());
503 if (tempDir.IsEmpty())
504 tempDir = wxFileName::GetTempDir();
b72812a6 505
d2d0adc7 506 wxString ext(image->GetImageBlock().GetExtension());
86501081 507 wxString tempFilename(wxString::Format(wxT("%s/image%d.%s"), tempDir, sm_fileCounter, ext));
d2d0adc7 508 image->GetImageBlock().Write(tempFilename);
b72812a6 509
d2d0adc7 510 m_imageLocations.Add(tempFilename);
b72812a6
JS
511
512 str << wxFileSystem::FileNameToURL(tempFilename);
d2d0adc7
JS
513 }
514 else
515 str << wxT("file:?");
40989e46 516
d2d0adc7
JS
517 sm_fileCounter ++;
518 }
519 else // if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_BASE64) // this is implied
520#endif
521 {
522 str << wxT("data:");
523 str << GetMimeType(image->GetImageBlock().GetImageType());
524 str << wxT(";base64,");
525
526 if (image->GetImage().Ok() && !image->GetImageBlock().GetData())
527 image->MakeBlock();
528
529 wxChar* data = b64enc( image->GetImageBlock().GetData(), image->GetImageBlock().GetDataSize() );
530 str << data;
531
532 delete[] data;
533 }
40989e46 534
2dec6761
JS
535 str << wxT("\" />");
536}
537
d2d0adc7 538long wxRichTextHTMLHandler::PtToSize(long size)
2dec6761 539{
50f65288
WS
540 int i;
541 int len = m_fontSizeMapping.GetCount();
542 for (i = 0; i < len; i++)
543 if (size <= m_fontSizeMapping[i])
544 return i+1;
b72812a6 545 return 7;
2dec6761
JS
546}
547
548wxString wxRichTextHTMLHandler::SymbolicIndent(long indent)
549{
550 wxString in;
551 for(;indent > 0; indent -= 20)
552 in.Append( wxT("&nbsp;") );
553 return in;
554}
555
21e354f1 556const wxChar* wxRichTextHTMLHandler::GetMimeType(int imageType)
2dec6761
JS
557{
558 switch(imageType)
559 {
560 case wxBITMAP_TYPE_BMP:
561 return wxT("image/bmp");
562 case wxBITMAP_TYPE_TIF:
563 return wxT("image/tiff");
564 case wxBITMAP_TYPE_GIF:
565 return wxT("image/gif");
566 case wxBITMAP_TYPE_PNG:
567 return wxT("image/png");
568 case wxBITMAP_TYPE_JPEG:
569 return wxT("image/jpeg");
570 default:
571 return wxT("image/unknown");
572 }
573}
574
d2d0adc7 575// exim-style base64 encoder
2dec6761
JS
576wxChar* wxRichTextHTMLHandler::b64enc( unsigned char* input, size_t in_len )
577{
d2d0adc7
JS
578 // elements of enc64 array must be 8 bit values
579 // otherwise encoder will fail
580 // hmmm.. Does wxT macro define a char as 16 bit value
581 // when compiling with UNICODE option?
21e354f1 582 static const wxChar enc64[] = wxT("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
2dec6761
JS
583 wxChar* output = new wxChar[4*((in_len+2)/3)+1];
584 wxChar* p = output;
40989e46 585
2dec6761
JS
586 while( in_len-- > 0 )
587 {
588 register wxChar a, b;
40989e46 589
2dec6761 590 a = *input++;
40989e46 591
2dec6761 592 *p++ = enc64[ (a >> 2) & 0x3f ];
40989e46 593
07854e5e 594 if( in_len-- == 0 )
2dec6761
JS
595 {
596 *p++ = enc64[ (a << 4 ) & 0x30 ];
597 *p++ = '=';
598 *p++ = '=';
599 break;
600 }
40989e46 601
2dec6761 602 b = *input++;
40989e46 603
2dec6761 604 *p++ = enc64[(( a << 4 ) | ((b >> 4) &0xf )) & 0x3f];
40989e46 605
07854e5e 606 if( in_len-- == 0 )
2dec6761
JS
607 {
608 *p++ = enc64[ (b << 2) & 0x3f ];
609 *p++ = '=';
610 break;
611 }
40989e46 612
2dec6761 613 a = *input++;
40989e46 614
2dec6761 615 *p++ = enc64[ ((( b << 2 ) & 0x3f ) | ((a >> 6)& 0x3)) & 0x3f ];
40989e46 616
2dec6761
JS
617 *p++ = enc64[ a & 0x3f ];
618 }
619 *p = 0;
40989e46 620
2dec6761
JS
621 return output;
622}
b71e9aa4 623#endif
2dec6761 624// wxUSE_STREAMS
b71e9aa4 625
d2d0adc7
JS
626/// Delete the in-memory or temporary files generated by the last operation
627bool wxRichTextHTMLHandler::DeleteTemporaryImages()
628{
629 return DeleteTemporaryImages(GetFlags(), m_imageLocations);
630}
631
632/// Delete the in-memory or temporary files generated by the last operation
633bool wxRichTextHTMLHandler::DeleteTemporaryImages(int flags, const wxArrayString& imageLocations)
634{
635 size_t i;
636 for (i = 0; i < imageLocations.GetCount(); i++)
637 {
638 wxString location = imageLocations[i];
b72812a6 639
d2d0adc7
JS
640 if (flags & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY)
641 {
642#if wxUSE_FILESYSTEM
643 wxMemoryFSHandler::RemoveFile(location);
644#endif
645 }
646 else if (flags & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_FILES)
647 {
648 if (wxFileExists(location))
649 wxRemoveFile(location);
650 }
651 }
b72812a6 652
d2d0adc7
JS
653 return true;
654}
655
656
b71e9aa4 657#endif
2dec6761 658// wxUSE_RICHTEXT
d2d0adc7 659