]> git.saurik.com Git - wxWidgets.git/blame - src/richtext/richtexthtml.cpp
added operators for comparing wxString to wxCStrData
[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;
56 wxSplitPath(filename, & path, & file, & ext);
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();
50f65288 79
b71e9aa4 80 wxTextOutputStream str(stream);
40989e46 81
b71e9aa4
JS
82 wxTextAttrEx currentParaStyle = buffer->GetAttributes();
83 wxTextAttrEx currentCharStyle = buffer->GetAttributes();
40989e46 84
b774c698
JS
85 if ((GetFlags() & wxRICHTEXT_HANDLER_NO_HEADER_FOOTER) == 0)
86 str << wxT("<html><head></head><body>\n");
40989e46 87
50f65288 88 str << wxT("<table border=0 cellpadding=0 cellspacing=0><tr><td width=\"100%\">");
40989e46 89
50f65288
WS
90 OutputFont(currentParaStyle, str);
91
2dec6761 92 m_font = false;
50f65288
WS
93 m_inTable = false;
94
95 m_indents.Clear();
96 m_listTypes.Clear();
40989e46 97
b71e9aa4
JS
98 wxRichTextObjectList::compatibility_iterator node = buffer->GetChildren().GetFirst();
99 while (node)
100 {
101 wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph);
102 wxASSERT (para != NULL);
40989e46 103
b71e9aa4
JS
104 if (para)
105 {
27507b61
JS
106 wxTextAttrEx paraStyle(para->GetCombinedAttributes());
107
50f65288 108 BeginParagraphFormatting(currentParaStyle, paraStyle, str);
40989e46 109
b71e9aa4
JS
110 wxRichTextObjectList::compatibility_iterator node2 = para->GetChildren().GetFirst();
111 while (node2)
112 {
113 wxRichTextObject* obj = node2->GetData();
114 wxRichTextPlainText* textObj = wxDynamicCast(obj, wxRichTextPlainText);
115 if (textObj && !textObj->IsEmpty())
116 {
27507b61 117 wxTextAttrEx charStyle(para->GetCombinedAttributes(obj->GetAttributes()));
50f65288 118 BeginCharacterFormatting(currentCharStyle, charStyle, paraStyle, str);
42688aea
JS
119
120 wxString text = textObj->GetText();
40989e46 121
42688aea
JS
122 if (charStyle.HasTextEffects() && (charStyle.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS))
123 text.MakeUpper();
124
50f65288
WS
125 wxString toReplace = wxRichTextLineBreakChar;
126 text.Replace(toReplace, wxT("<br>"));
127
42688aea 128 str << text;
40989e46 129
50f65288 130 EndCharacterFormatting(currentCharStyle, charStyle, paraStyle, str);
b71e9aa4 131 }
40989e46 132
2dec6761
JS
133 wxRichTextImage* image = wxDynamicCast(obj, wxRichTextImage);
134 if( image && !image->IsEmpty())
d2d0adc7 135 WriteImage( image, stream );
40989e46 136
b71e9aa4
JS
137 node2 = node2->GetNext();
138 }
50f65288
WS
139
140 EndParagraphFormatting(currentParaStyle, paraStyle, str);
141
d5363f57 142 str << wxT("\n");
b71e9aa4 143 }
b71e9aa4
JS
144 node = node->GetNext();
145 }
50f65288
WS
146
147 CloseLists(-1, str);
40989e46 148
50f65288
WS
149 str << wxT("</font>");
150
151 str << wxT("</td></tr></table><p>");
b774c698
JS
152
153 if ((GetFlags() & wxRICHTEXT_HANDLER_NO_HEADER_FOOTER) == 0)
154 str << wxT("</body></html>");
155
156 str << wxT("\n");
40989e46 157
50f65288
WS
158 m_buffer = NULL;
159
b71e9aa4
JS
160 return true;
161}
162
50f65288 163void wxRichTextHTMLHandler::BeginCharacterFormatting(const wxTextAttrEx& currentStyle, const wxTextAttrEx& thisStyle, const wxTextAttrEx& WXUNUSED(paraStyle), wxTextOutputStream& str)
b71e9aa4 164{
2dec6761 165 wxString style;
40989e46 166
d2d0adc7
JS
167 // Is there any change in the font properties of the item?
168 if (thisStyle.GetFont().GetFaceName() != currentStyle.GetFont().GetFaceName())
169 {
170 wxString faceName(thisStyle.GetFont().GetFaceName());
171 style += wxString::Format(wxT(" face=\"%s\""), faceName.c_str());
172 }
173 if (thisStyle.GetFont().GetPointSize() != currentStyle.GetFont().GetPointSize())
174 style += wxString::Format(wxT(" size=\"%ld\""), PtToSize(thisStyle.GetFont().GetPointSize()));
175 if (thisStyle.GetTextColour() != currentStyle.GetTextColour() )
176 {
177 wxString color(thisStyle.GetTextColour().GetAsString(wxC2S_HTML_SYNTAX));
178 style += wxString::Format(wxT(" color=\"%s\""), color.c_str());
179 }
40989e46 180
d2d0adc7 181 if (style.size())
21e354f1
JS
182 {
183 str << wxString::Format(wxT("<font %s >"), style.c_str());
184 m_font = true;
185 }
40989e46 186
d2d0adc7 187 if (thisStyle.GetFont().GetWeight() == wxBOLD)
2dec6761 188 str << wxT("<b>");
d2d0adc7 189 if (thisStyle.GetFont().GetStyle() == wxITALIC)
2dec6761 190 str << wxT("<i>");
d2d0adc7 191 if (thisStyle.GetFont().GetUnderlined())
2dec6761 192 str << wxT("<u>");
d2d0adc7
JS
193
194 if (thisStyle.HasURL())
195 str << wxT("<a href=\"") << thisStyle.GetURL() << wxT("\">");
b71e9aa4
JS
196}
197
50f65288 198void wxRichTextHTMLHandler::EndCharacterFormatting(const wxTextAttrEx& WXUNUSED(currentStyle), const wxTextAttrEx& thisStyle, const wxTextAttrEx& WXUNUSED(paraStyle), wxTextOutputStream& stream)
b71e9aa4 199{
d2d0adc7 200 if (thisStyle.HasURL())
50f65288 201 stream << wxT("</a>");
d2d0adc7
JS
202
203 if (thisStyle.GetFont().GetUnderlined())
50f65288 204 stream << wxT("</u>");
d2d0adc7 205 if (thisStyle.GetFont().GetStyle() == wxITALIC)
50f65288 206 stream << wxT("</i>");
d2d0adc7 207 if (thisStyle.GetFont().GetWeight() == wxBOLD)
50f65288 208 stream << wxT("</b>");
40989e46 209
d2d0adc7 210 if (m_font)
2dec6761
JS
211 {
212 m_font = false;
50f65288 213 stream << wxT("</font>");
2dec6761
JS
214 }
215}
b71e9aa4 216
50f65288
WS
217/// Begin paragraph formatting
218void wxRichTextHTMLHandler::BeginParagraphFormatting(const wxTextAttrEx& WXUNUSED(currentStyle), const wxTextAttrEx& thisStyle, wxTextOutputStream& str)
2dec6761 219{
42688aea
JS
220 if (thisStyle.HasPageBreak())
221 {
50f65288 222 str << wxT("</tr></td></table>");
42688aea 223 str << wxT("<div style=\"page-break-after:always\"></div>\n");
50f65288 224 str << wxT("<table border=0 cellpadding=0 cellspacing=0><tr><td width=\"100%\">");
42688aea 225 }
40989e46 226
7947a11d 227 if (thisStyle.HasLeftIndent() && thisStyle.GetLeftIndent() != 0)
2dec6761 228 {
50f65288 229 if (thisStyle.HasBulletStyle())
2dec6761 230 {
50f65288
WS
231 int indent = thisStyle.GetLeftIndent();
232
233 // Close levels high than this
234 CloseLists(indent, str);
235
236 if (m_indents.GetCount() > 0 && indent == m_indents.Last())
237 {
238 // Same level, no need to start a new list
239 }
240 else if (m_indents.GetCount() == 0 || indent > m_indents.Last())
241 {
242 m_indents.Add(indent);
243
244 wxString tag;
245 int listType = TypeOfList(thisStyle, tag);
246 m_listTypes.Add(listType);
247
248 wxString align = GetAlignment(thisStyle);
249 str << wxString::Format(wxT("<p align=\"%s\">"), align.c_str());
250
251 str << tag;
252 }
253
254 str << wxT("<li> ");
2dec6761 255 }
2dec6761
JS
256 else
257 {
50f65288
WS
258 CloseLists(-1, str);
259
260 wxString align = GetAlignment(thisStyle);
261 str << wxString::Format(wxT("<p align=\"%s\">"), align.c_str());
40989e46 262
50f65288
WS
263 // Use a table
264 int indentTenthsMM = thisStyle.GetLeftIndent() + thisStyle.GetLeftSubIndent();
265 // TODO: convert to pixels
266 int indentPixels = indentTenthsMM/4;
267 str << wxString::Format(wxT("<table border=0 cellpadding=0 cellspacing=0><tr><td width=\"%d\"></td><td>"), indentPixels);
268
269 OutputFont(thisStyle, str);
40989e46 270
50f65288 271 if (thisStyle.GetLeftSubIndent() < 0)
d2d0adc7 272 {
50f65288 273 str << SymbolicIndent( - thisStyle.GetLeftSubIndent());
d2d0adc7 274 }
50f65288
WS
275
276 m_inTable = true;
2dec6761
JS
277 }
278 }
50f65288
WS
279 else
280 {
281 CloseLists(-1, str);
282
283 wxString align = GetAlignment(thisStyle);
284 str << wxString::Format(wxT("<p align=\"%s\">"), align.c_str());
285 }
2dec6761 286}
50f65288
WS
287
288/// End paragraph formatting
289void wxRichTextHTMLHandler::EndParagraphFormatting(const wxTextAttrEx& WXUNUSED(currentStyle), const wxTextAttrEx& thisStyle, wxTextOutputStream& stream)
2dec6761 290{
50f65288 291 if (m_inTable)
2dec6761 292 {
50f65288
WS
293 if (thisStyle.HasFont())
294 stream << wxT("</font>");
295
296 stream << wxT("</td></tr></table>\n");
297 m_inTable = false;
2dec6761
JS
298 }
299}
300
50f65288
WS
301/// Closes lists to level (-1 means close all)
302void wxRichTextHTMLHandler::CloseLists(int level, wxTextOutputStream& str)
303{
304 // Close levels high than this
305 int i = m_indents.GetCount()-1;
306 while (i >= 0)
307 {
308 int l = m_indents[i];
309 if (l > level)
310 {
311 if (m_listTypes[i] == 0)
312 str << wxT("</ol>");
313 else
314 str << wxT("</ul>");
315 m_indents.RemoveAt(i);
316 m_listTypes.RemoveAt(i);
317 }
318 else
319 break;
320 i --;
321 }
322}
323
324/// Output font tag
325void wxRichTextHTMLHandler::OutputFont(const wxTextAttrEx& style, wxTextOutputStream& stream)
2dec6761 326{
50f65288
WS
327 if (style.HasFont())
328 {
329 stream << wxString::Format(wxT("<font face=\"%s\" size=\"%ld\" color=\"%s\" >"),
330 style.GetFont().GetFaceName().c_str(), PtToSize(style.GetFont().GetPointSize()),
331 style.GetTextColour().GetAsString(wxC2S_HTML_SYNTAX).c_str());
332 }
2dec6761
JS
333}
334
50f65288 335int wxRichTextHTMLHandler::TypeOfList( const wxTextAttrEx& thisStyle, wxString& tag )
2dec6761 336{
d2d0adc7
JS
337 // We can use number attribute of li tag but not all the browsers support it.
338 // also wxHtmlWindow doesn't support type attribute.
40989e46 339
50f65288 340 bool m_is_ul = false;
d2d0adc7 341 if (thisStyle.GetBulletStyle() == (wxTEXT_ATTR_BULLET_STYLE_ARABIC|wxTEXT_ATTR_BULLET_STYLE_PERIOD))
2dec6761 342 tag = wxT("<ol type=\"1\">");
d2d0adc7 343 else if (thisStyle.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER)
2dec6761 344 tag = wxT("<ol type=\"A\">");
d2d0adc7 345 else if (thisStyle.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER)
2dec6761 346 tag = wxT("<ol type=\"a\">");
d2d0adc7 347 else if (thisStyle.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER)
2dec6761 348 tag = wxT("<ol type=\"I\">");
d2d0adc7 349 else if (thisStyle.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER)
2dec6761 350 tag = wxT("<ol type=\"i\">");
b71e9aa4
JS
351 else
352 {
2dec6761
JS
353 tag = wxT("<ul>");
354 m_is_ul = true;
b71e9aa4 355 }
50f65288
WS
356
357 if (m_is_ul)
358 return 1;
359 else
360 return 0;
b71e9aa4
JS
361}
362
2dec6761
JS
363wxString wxRichTextHTMLHandler::GetAlignment( const wxTextAttrEx& thisStyle )
364{
365 switch( thisStyle.GetAlignment() )
366 {
367 case wxTEXT_ALIGNMENT_LEFT:
368 return wxT("left");
369 case wxTEXT_ALIGNMENT_RIGHT:
370 return wxT("right");
371 case wxTEXT_ALIGNMENT_CENTER:
372 return wxT("center");
373 case wxTEXT_ALIGNMENT_JUSTIFIED:
374 return wxT("justify");
375 default:
376 return wxT("left");
377 }
378}
379
d2d0adc7 380void wxRichTextHTMLHandler::WriteImage(wxRichTextImage* image, wxOutputStream& stream)
2dec6761
JS
381{
382 wxTextOutputStream str(stream);
40989e46 383
2dec6761 384 str << wxT("<img src=\"");
40989e46 385
d2d0adc7
JS
386#if wxUSE_FILESYSTEM
387 if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY)
388 {
389 if (!image->GetImage().Ok() && image->GetImageBlock().GetData())
390 image->LoadFromBlock();
391 if (image->GetImage().Ok() && !image->GetImageBlock().GetData())
392 image->MakeBlock();
393
394 if (image->GetImage().Ok())
395 {
396 wxString ext(image->GetImageBlock().GetExtension());
397 wxString tempFilename(wxString::Format(wxT("image%d.%s"), sm_fileCounter, (const wxChar*) ext));
398 wxMemoryFSHandler::AddFile(tempFilename, image->GetImage(), image->GetImageBlock().GetImageType());
399
400 m_imageLocations.Add(tempFilename);
401
402 str << wxT("memory:") << tempFilename;
403 }
404 else
405 str << wxT("memory:?");
40989e46 406
d2d0adc7
JS
407 sm_fileCounter ++;
408 }
409 else if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_FILES)
410 {
411 if (!image->GetImage().Ok() && image->GetImageBlock().GetData())
412 image->LoadFromBlock();
413 if (image->GetImage().Ok() && !image->GetImageBlock().GetData())
414 image->MakeBlock();
415
416 if (image->GetImage().Ok())
417 {
418 wxString tempDir(GetTempDir());
419 if (tempDir.IsEmpty())
420 tempDir = wxFileName::GetTempDir();
421
422 wxString ext(image->GetImageBlock().GetExtension());
f2737255 423 wxString tempFilename(wxString::Format(wxT("%s/image%d.%s"), (const wxChar*) tempDir, sm_fileCounter, (const wxChar*) ext));
d2d0adc7
JS
424 image->GetImageBlock().Write(tempFilename);
425
426 m_imageLocations.Add(tempFilename);
427
428 str << wxFileSystem::FileNameToURL(tempFilename);
429 }
430 else
431 str << wxT("file:?");
40989e46 432
d2d0adc7
JS
433 sm_fileCounter ++;
434 }
435 else // if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_BASE64) // this is implied
436#endif
437 {
438 str << wxT("data:");
439 str << GetMimeType(image->GetImageBlock().GetImageType());
440 str << wxT(";base64,");
441
442 if (image->GetImage().Ok() && !image->GetImageBlock().GetData())
443 image->MakeBlock();
444
445 wxChar* data = b64enc( image->GetImageBlock().GetData(), image->GetImageBlock().GetDataSize() );
446 str << data;
447
448 delete[] data;
449 }
40989e46 450
2dec6761
JS
451 str << wxT("\" />");
452}
453
d2d0adc7 454long wxRichTextHTMLHandler::PtToSize(long size)
2dec6761 455{
50f65288
WS
456 int i;
457 int len = m_fontSizeMapping.GetCount();
458 for (i = 0; i < len; i++)
459 if (size <= m_fontSizeMapping[i])
460 return i+1;
461 return 7;
2dec6761
JS
462}
463
464wxString wxRichTextHTMLHandler::SymbolicIndent(long indent)
465{
466 wxString in;
467 for(;indent > 0; indent -= 20)
468 in.Append( wxT("&nbsp;") );
469 return in;
470}
471
21e354f1 472const wxChar* wxRichTextHTMLHandler::GetMimeType(int imageType)
2dec6761
JS
473{
474 switch(imageType)
475 {
476 case wxBITMAP_TYPE_BMP:
477 return wxT("image/bmp");
478 case wxBITMAP_TYPE_TIF:
479 return wxT("image/tiff");
480 case wxBITMAP_TYPE_GIF:
481 return wxT("image/gif");
482 case wxBITMAP_TYPE_PNG:
483 return wxT("image/png");
484 case wxBITMAP_TYPE_JPEG:
485 return wxT("image/jpeg");
486 default:
487 return wxT("image/unknown");
488 }
489}
490
d2d0adc7 491// exim-style base64 encoder
2dec6761
JS
492wxChar* wxRichTextHTMLHandler::b64enc( unsigned char* input, size_t in_len )
493{
d2d0adc7
JS
494 // elements of enc64 array must be 8 bit values
495 // otherwise encoder will fail
496 // hmmm.. Does wxT macro define a char as 16 bit value
497 // when compiling with UNICODE option?
21e354f1 498 static const wxChar enc64[] = wxT("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
2dec6761
JS
499 wxChar* output = new wxChar[4*((in_len+2)/3)+1];
500 wxChar* p = output;
40989e46 501
2dec6761
JS
502 while( in_len-- > 0 )
503 {
504 register wxChar a, b;
40989e46 505
2dec6761 506 a = *input++;
40989e46 507
2dec6761 508 *p++ = enc64[ (a >> 2) & 0x3f ];
40989e46 509
07854e5e 510 if( in_len-- == 0 )
2dec6761
JS
511 {
512 *p++ = enc64[ (a << 4 ) & 0x30 ];
513 *p++ = '=';
514 *p++ = '=';
515 break;
516 }
40989e46 517
2dec6761 518 b = *input++;
40989e46 519
2dec6761 520 *p++ = enc64[(( a << 4 ) | ((b >> 4) &0xf )) & 0x3f];
40989e46 521
07854e5e 522 if( in_len-- == 0 )
2dec6761
JS
523 {
524 *p++ = enc64[ (b << 2) & 0x3f ];
525 *p++ = '=';
526 break;
527 }
40989e46 528
2dec6761 529 a = *input++;
40989e46 530
2dec6761 531 *p++ = enc64[ ((( b << 2 ) & 0x3f ) | ((a >> 6)& 0x3)) & 0x3f ];
40989e46 532
2dec6761
JS
533 *p++ = enc64[ a & 0x3f ];
534 }
535 *p = 0;
40989e46 536
2dec6761
JS
537 return output;
538}
b71e9aa4 539#endif
2dec6761 540// wxUSE_STREAMS
b71e9aa4 541
d2d0adc7
JS
542/// Delete the in-memory or temporary files generated by the last operation
543bool wxRichTextHTMLHandler::DeleteTemporaryImages()
544{
545 return DeleteTemporaryImages(GetFlags(), m_imageLocations);
546}
547
548/// Delete the in-memory or temporary files generated by the last operation
549bool wxRichTextHTMLHandler::DeleteTemporaryImages(int flags, const wxArrayString& imageLocations)
550{
551 size_t i;
552 for (i = 0; i < imageLocations.GetCount(); i++)
553 {
554 wxString location = imageLocations[i];
555
556 if (flags & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY)
557 {
558#if wxUSE_FILESYSTEM
559 wxMemoryFSHandler::RemoveFile(location);
560#endif
561 }
562 else if (flags & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_FILES)
563 {
564 if (wxFileExists(location))
565 wxRemoveFile(location);
566 }
567 }
568
569 return true;
570}
571
572
b71e9aa4 573#endif
2dec6761 574// wxUSE_RICHTEXT
d2d0adc7 575