]> git.saurik.com Git - wxWidgets.git/blob - src/richtext/richtexthtml.cpp
fix compilation both for Robert's cross-compiler and for all normal compilers by...
[wxWidgets.git] / src / richtext / richtexthtml.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richtexthtml.cpp
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__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_RICHTEXT
20
21 #include "wx/richtext/richtexthtml.h"
22
23 #ifndef WX_PRECOMP
24 #endif
25
26 #include "wx/filename.h"
27 #include "wx/wfstream.h"
28 #include "wx/txtstrm.h"
29
30 IMPLEMENT_DYNAMIC_CLASS(wxRichTextHTMLHandler, wxRichTextFileHandler)
31
32 /// Can we handle this filename (if using files)? By default, checks the extension.
33 bool wxRichTextHTMLHandler::CanHandle(const wxString& filename) const
34 {
35 wxString path, file, ext;
36 wxSplitPath(filename, & path, & file, & ext);
37
38 return (ext.Lower() == wxT("html") || ext.Lower() == wxT("htm"));
39 }
40
41
42 #if wxUSE_STREAMS
43 bool wxRichTextHTMLHandler::DoLoadFile(wxRichTextBuffer *WXUNUSED(buffer), wxInputStream& WXUNUSED(stream))
44 {
45 return false;
46 }
47
48 /*
49 * We need to output only _changes_ in character formatting.
50 */
51
52 bool wxRichTextHTMLHandler::DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream)
53 {
54 buffer->Defragment();
55
56 wxTextOutputStream str(stream);
57
58 wxTextAttrEx currentParaStyle = buffer->GetAttributes();
59 wxTextAttrEx currentCharStyle = buffer->GetAttributes();
60
61 str << wxT("<html><head></head><body>\n");
62
63 /*
64 wxRichText may be support paper formats like a1/a2/a3/a4
65 when this widget grown enough, i should turn back and support its new features
66 but not yet
67
68 str << wxT("<table border=0 cellpadding=0 cellspacing=0><tr><td>");
69
70 wxString left_indent = SymbolicIndent(currentParaStyle.GetLeftIndent());
71 wxString right_indent = SymbolicIndent(currentParaStyle.GetRightIndent());
72
73 str << wxString::Format(wxT("%s</td><td></td><td>%s</td></tr><tr>"),
74 left_indent.c_str(), //Document-Wide Left Indent
75 right_indent.c_str()); //Document-Wide Right Indent
76
77 str << wxT("<td></td><td width=\"100%\">");
78 */
79
80 str << wxT("<table border=0 cellpadding=0 cellspacing=0><tr><td width=\"100%\">");
81
82 str << wxString::Format(wxT("<font face=\"%s\" size=\"%ld\" color=\"%s\" >"),
83 currentParaStyle.GetFont().GetFaceName().c_str(), Pt_To_Size( currentParaStyle.GetFont().GetPointSize() ),
84 currentParaStyle.GetTextColour().GetAsString(wxC2S_HTML_SYNTAX).c_str());
85
86 //wxString align = GetAlignment( currentParaStyle.GetAlignment() );
87 //str << wxString::Format(wxT("<p align=\"%s\">"), align );
88
89 m_font = false;
90 m_indent = 0;
91 m_list = false;
92
93 wxRichTextObjectList::compatibility_iterator node = buffer->GetChildren().GetFirst();
94 while (node)
95 {
96 wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph);
97 wxASSERT (para != NULL);
98
99 if (para)
100 {
101 OutputParagraphFormatting(currentParaStyle, para->GetAttributes(), stream);
102
103 wxRichTextObjectList::compatibility_iterator node2 = para->GetChildren().GetFirst();
104 while (node2)
105 {
106 wxRichTextObject* obj = node2->GetData();
107 wxRichTextPlainText* textObj = wxDynamicCast(obj, wxRichTextPlainText);
108 if (textObj && !textObj->IsEmpty())
109 {
110 BeginCharacterFormatting(currentCharStyle, obj->GetAttributes(), stream);
111
112 str << textObj->GetText();
113
114 EndCharacterFormatting(currentCharStyle, obj->GetAttributes(), stream);
115 }
116
117 wxRichTextImage* image = wxDynamicCast(obj, wxRichTextImage);
118 if( image && !image->IsEmpty())
119 Image_to_Base64( image, stream );
120
121 node2 = node2->GetNext();
122 }
123 str << wxT("\n");
124 //OutputParagraphFormatting(currentParaStyle, para->GetAttributes(), stream, false);
125 }
126 node = node->GetNext();
127 }
128
129 str << wxT("</font></td></tr></table></body></html>\n");
130
131 return true;
132 }
133
134 void wxRichTextHTMLHandler::BeginCharacterFormatting(const wxTextAttrEx& currentStyle, const wxTextAttrEx& thisStyle, wxOutputStream& stream)
135 {
136 wxTextOutputStream str(stream);
137
138 //Is the item bulleted one?
139 if( thisStyle.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE )
140 {
141 //Is there any opened list?
142 if( m_list )
143 {
144 //Yes there is
145
146 //Is the item among the previous ones
147 //Is the item one of the previous list tag's child items
148 if( (thisStyle.GetLeftIndent() == (m_indent + 100)) || (thisStyle.GetLeftIndent() < 100) )
149 str << wxT("<li>");//Yes it is
150 else
151 {
152 //No it isn't
153
154 //So we should close the list tag
155 str << (m_is_ul ? wxT("</ul>") : wxT("</ol>"));
156
157 //And renavigate to new list's horizontal position
158 NavigateToListPosition(thisStyle, str);
159 //Ok it's done
160
161 //Get the appropriate tag, an ol for numerical values, an ul for dot, square etc.
162 wxString tag;
163 TypeOfList(thisStyle, tag);
164 str << tag << wxT("<li>");
165 }
166 }
167 else
168 {
169 //No there isn't a list
170
171 //navigate to new list's horizontal position(indent)
172 NavigateToListPosition(thisStyle, str);
173
174 //Get the appropriate tag, an ol for numerical values, an ul for dot, square etc.
175 wxString tag;
176 TypeOfList(thisStyle, tag);
177 str << tag << wxT("<li>");
178
179 //Now we have a list, mark it.
180 m_list = true;
181 }
182 }
183 else if( m_list )
184 {
185 //The item is not bulleted and there is a list what should be closed now.
186 //So close the list
187
188 str << (m_is_ul ? wxT("</ul>") : wxT("</ol>"));
189 //And mark as there is no an opened list
190 m_list = false;
191 }
192
193 // does the item have an indentation ?
194 if( thisStyle.GetLeftIndent() )
195 {
196 if( thisStyle.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE )
197 {
198 if( m_indent )
199 {
200 if( (thisStyle.GetLeftIndent() + thisStyle.GetLeftSubIndent()) == m_indent )
201 {
202 if( thisStyle.GetLeftSubIndent() < 0 )
203 {
204 str << SymbolicIndent(~thisStyle.GetLeftSubIndent());
205 }
206 }
207 else
208 {
209 if( thisStyle.GetLeftIndent() + thisStyle.GetLeftSubIndent() > m_indent )
210 {
211 Indent(thisStyle, str);
212 m_indent = thisStyle.GetLeftIndent() + thisStyle.GetLeftSubIndent();
213 m_indents.Add( m_indent );
214 }
215 else
216 {
217 int i = m_indents.size() - 1;
218 for(; i > -1; i--)
219 {
220 if( m_indent < (thisStyle.GetLeftIndent() + thisStyle.GetLeftSubIndent()) )
221 {
222 Indent(thisStyle, str);
223 m_indent = thisStyle.GetLeftIndent() + thisStyle.GetLeftSubIndent();
224 m_indents.Add( m_indent );
225
226 break;
227 }
228 else if( m_indent == (thisStyle.GetLeftIndent() + thisStyle.GetLeftSubIndent()) )
229 {
230 if( thisStyle.GetLeftSubIndent() < 0 )
231 {
232 str << SymbolicIndent(~thisStyle.GetLeftSubIndent());
233 }
234 break;
235 }
236 else
237 {
238 str << wxT("</td></tr></table>");
239
240 m_indents.RemoveAt(i);
241
242 if( i < 1 ){m_indent=0; break;}
243 m_indent = m_indents[i-1];
244 }
245 }
246 }
247 }
248 }
249 else
250 {
251 Indent(thisStyle, str);
252 m_indent = thisStyle.GetLeftIndent() + thisStyle.GetLeftSubIndent();
253 m_indents.Add( m_indent );
254 }
255 }
256 }
257 else if( m_indent )
258 {
259 //The item is not indented and there is a table(s) what should be closed now.
260
261 //So close them
262 for(unsigned int i = 0; i < m_indents.size(); i++ )
263 str << wxT("</td></tr></table>");
264
265 m_indent = 0;
266 m_indents.Clear();
267 }
268
269
270 wxString style;
271
272 //Is there any change on the font properties of the item
273 if( thisStyle.GetFont().GetFaceName() != currentStyle.GetFont().GetFaceName() )
274 style += wxString::Format(wxT(" face=\"%s\""), thisStyle.GetFont().GetFaceName().c_str());
275 if( thisStyle.GetFont().GetPointSize() != currentStyle.GetFont().GetPointSize() )
276 style += wxString::Format(wxT(" size=\"%ld\""), Pt_To_Size(thisStyle.GetFont().GetPointSize()) );
277 if( thisStyle.GetTextColour() != currentStyle.GetTextColour() )
278 style += wxString::Format(wxT(" color=\"%s\""), thisStyle.GetTextColour().GetAsString(wxC2S_HTML_SYNTAX).c_str());
279
280 if( style.size() )
281 {
282 str << wxString::Format(wxT("<font %s >"), style.c_str());
283 m_font = true;
284 }
285
286 if( thisStyle.GetFont().GetWeight() == wxBOLD )
287 str << wxT("<b>");
288 if( thisStyle.GetFont().GetStyle() == wxITALIC )
289 str << wxT("<i>");
290 if( thisStyle.GetFont().GetUnderlined() )
291 str << wxT("<u>");
292 }
293
294 void wxRichTextHTMLHandler::EndCharacterFormatting(const wxTextAttrEx& WXUNUSED(currentStyle), const wxTextAttrEx& thisStyle, wxOutputStream& stream)
295 {
296 wxTextOutputStream str(stream);
297
298 if( thisStyle.GetFont().GetUnderlined() )
299 str << wxT("</u>");
300 if( thisStyle.GetFont().GetStyle() == wxITALIC )
301 str << wxT("</i>");
302 if( thisStyle.GetFont().GetWeight() == wxBOLD )
303 str << wxT("</b>");
304
305 if( m_font )
306 {
307 m_font = false;
308 str << wxT("</font>");
309 }
310 }
311
312 /// Output paragraph formatting
313 void wxRichTextHTMLHandler::OutputParagraphFormatting(const wxTextAttrEx& WXUNUSED(currentStyle), const wxTextAttrEx& thisStyle, wxOutputStream& stream)
314 {
315 //If there is no opened list currently, insert a <p> after every paragraph
316 if(!m_list)
317 {
318 wxTextOutputStream str(stream);
319 wxString align = GetAlignment( thisStyle );
320 str << wxString::Format(wxT("<p align=\"%s\">"), align.c_str());
321 }
322 }
323
324 void wxRichTextHTMLHandler::NavigateToListPosition(const wxTextAttrEx& thisStyle, wxTextOutputStream& str)
325 {
326 //indenting an item using an ul/ol tag is equal to inserting 5 x &nbsp; on its left side.
327 //so we should start from 100 point left
328
329 //Is the second td's left wall of the current indentaion table at the 100+ point-left-side
330 //of the item, horizontally?
331 if( m_indent + 100 < thisStyle.GetLeftIndent() )
332 {
333 //yes it is
334 LIndent(thisStyle, str);
335 m_indent = thisStyle.GetLeftIndent() - 100;
336 m_indents.Add( m_indent );
337 return;
338 }
339 //No it isn't
340
341 int i = m_indents.size() - 1;
342 for(; i > -1; i--)
343 {
344 //Is the second td's left wall of the current indentaion table at the 100+ point-left-side
345 //of the item ?
346 if( m_indent + 100 < thisStyle.GetLeftIndent() )
347 {
348 //Yes it is
349 LIndent(thisStyle, str);
350 m_indent = thisStyle.GetLeftIndent() - 100;
351 m_indents.Add( m_indent );
352 break;
353 }
354 else if( m_indent + 100 == thisStyle.GetLeftIndent() )
355 break;//exact match
356 else
357 {
358 //No it is not, the second td's left wall of the current indentaion table is at the
359 //right side of the current item horizontally, so close it.
360 str << wxT("</td></tr></table>");
361
362 m_indents.RemoveAt(i);
363
364 if( i < 1 ){m_indent=0; break;}
365 m_indent = m_indents[i-1];
366 }
367 }
368 }
369 void wxRichTextHTMLHandler::Indent( const wxTextAttrEx& thisStyle, wxTextOutputStream& str )
370 {
371 //As a five year experienced web developer i assure you there is no way to indent an item
372 //in html way, but we can use tables.
373
374
375
376 //Item -> "Hello world"
377 //Its Left Indentation -> 100
378 //Its Left Sub-Indentation ->40
379 //A typical indentation-table for the item will be construct as the following
380
381 //3 x nbsp = 60
382 //2 x nbsp = 40
383 //LSI = Left Sub Indent
384 //LI = Left Indent - LSI
385 //
386 //-------------------------------------------
387 //|&nbsp;&nbsp;nbsp;|nbsp;nbsp;Hello World |
388 //| | | | |
389 //| V | V |
390 //| --LI-- | --LSI-- |
391 //-------------------------------------------
392
393 str << wxT("<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>");
394
395 wxString symbolic_indent = SymbolicIndent( (thisStyle.GetLeftIndent() + thisStyle.GetLeftSubIndent()) - m_indent );
396 str << wxString::Format( wxT("<td>%s</td>"), symbolic_indent.c_str() );
397 str << wxT("<td width=\"100%\">");
398
399 if( thisStyle.GetLeftSubIndent() < 0 )
400 {
401 str << SymbolicIndent(~thisStyle.GetLeftSubIndent());
402 }
403 }
404
405 void wxRichTextHTMLHandler::LIndent( const wxTextAttrEx& thisStyle, wxTextOutputStream& str )
406 {
407 //Code:
408 //r.BeginNumberedBullet(1, 200, 60);
409 //r.Newline();
410 //r.WriteText(wxT("first item"));
411 //r.EndNumberedBullet();
412 //r.BeginNumberedBullet(2, 200, 60);
413 //r.Newline();
414 //r.WriteText(wxT("second item."));
415 //r.EndNumberedBullet();
416 //
417 //A typical indentation-table for the item will be construct as the following
418
419 //1 x nbsp = 20 point
420 //ULI -> 100pt (UL/OL tag indents its sub element by 100 point)
421 //<--------- 100 pt ---------->|
422 //------------------------------------------------------
423 //|&nbsp;&nbsp;nbsp;&nbsp;nbsp;|<ul> |
424 //| |<-ULI-><li>first item |
425 //| |<-ULI-><li>second item |
426 //| |</ul> |
427 //------------------------------------------------------
428 // |<-100->|
429
430
431 str << wxT("<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>");
432
433 wxString symbolic_indent = SymbolicIndent( (thisStyle.GetLeftIndent() - m_indent) - 100);
434 str << wxString::Format( wxT("<td>%s</td>"), symbolic_indent.c_str() );
435 str << wxT("<td width=\"100%\">");
436 }
437
438 void wxRichTextHTMLHandler::TypeOfList( const wxTextAttrEx& thisStyle, wxString& tag )
439 {
440 //We can use number attribute of li tag but not all the browsers support it.
441 //also wxHtmlWindow doesn't support type attribute.
442
443 m_is_ul = false;
444 if( thisStyle.GetBulletStyle() == (wxTEXT_ATTR_BULLET_STYLE_ARABIC|wxTEXT_ATTR_BULLET_STYLE_PERIOD))
445 tag = wxT("<ol type=\"1\">");
446 else if( thisStyle.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER )
447 tag = wxT("<ol type=\"A\">");
448 else if( thisStyle.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER )
449 tag = wxT("<ol type=\"a\">");
450 else if( thisStyle.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER )
451 tag = wxT("<ol type=\"I\">");
452 else if( thisStyle.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER )
453 tag = wxT("<ol type=\"i\">");
454 else
455 {
456 tag = wxT("<ul>");
457 m_is_ul = true;
458 }
459 }
460
461 wxString wxRichTextHTMLHandler::GetAlignment( const wxTextAttrEx& thisStyle )
462 {
463 switch( thisStyle.GetAlignment() )
464 {
465 case wxTEXT_ALIGNMENT_LEFT:
466 return wxT("left");
467 case wxTEXT_ALIGNMENT_RIGHT:
468 return wxT("right");
469 case wxTEXT_ALIGNMENT_CENTER:
470 return wxT("center");
471 case wxTEXT_ALIGNMENT_JUSTIFIED:
472 return wxT("justify");
473 default:
474 return wxT("left");
475 }
476 }
477
478 void wxRichTextHTMLHandler::Image_to_Base64(wxRichTextImage* image, wxOutputStream& stream)
479 {
480 wxTextOutputStream str(stream);
481
482 str << wxT("<img src=\"");
483 str << wxT("data:");
484 str << GetMimeType(image->GetImageBlock().GetImageType());
485 str << wxT(";base64,");
486
487 if (image->GetImage().Ok() && !image->GetImageBlock().GetData())
488 image->MakeBlock();
489
490 wxChar* data = b64enc( image->GetImageBlock().GetData(), image->GetImageBlock().GetDataSize() );
491 str << data;
492
493 delete[] data;
494
495 str << wxT("\" />");
496 }
497
498 long wxRichTextHTMLHandler::Pt_To_Size(long size)
499 {
500 //return most approximate size
501 if(size < 9 ) return 1;
502 else if( size < 11 ) return 2;
503 else if( size < 14 ) return 3;
504 else if( size < 18 ) return 4;
505 else if( size < 23 ) return 5;
506 else if( size < 30 ) return 6;
507 else return 7;
508 }
509
510 wxString wxRichTextHTMLHandler::SymbolicIndent(long indent)
511 {
512 wxString in;
513 for(;indent > 0; indent -= 20)
514 in.Append( wxT("&nbsp;") );
515 return in;
516 }
517
518 const wxChar* wxRichTextHTMLHandler::GetMimeType(int imageType)
519 {
520 switch(imageType)
521 {
522 case wxBITMAP_TYPE_BMP:
523 return wxT("image/bmp");
524 case wxBITMAP_TYPE_TIF:
525 return wxT("image/tiff");
526 case wxBITMAP_TYPE_GIF:
527 return wxT("image/gif");
528 case wxBITMAP_TYPE_PNG:
529 return wxT("image/png");
530 case wxBITMAP_TYPE_JPEG:
531 return wxT("image/jpeg");
532 default:
533 return wxT("image/unknown");
534 }
535 }
536
537 //exim-style base64 encoder
538 wxChar* wxRichTextHTMLHandler::b64enc( unsigned char* input, size_t in_len )
539 {
540 //elements of enc64 array must be 8 bit values
541 //otherwise encoder will fail
542 //hmmm.. Does wxT macro define a char as 16 bit value
543 //when compiling with UNICODE option?
544 static const wxChar enc64[] = wxT("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
545 wxChar* output = new wxChar[4*((in_len+2)/3)+1];
546 wxChar* p = output;
547
548 while( in_len-- > 0 )
549 {
550 register wxChar a, b;
551
552 a = *input++;
553
554 *p++ = enc64[ (a >> 2) & 0x3f ];
555
556 if( in_len-- <= 0 )
557 {
558 *p++ = enc64[ (a << 4 ) & 0x30 ];
559 *p++ = '=';
560 *p++ = '=';
561 break;
562 }
563
564 b = *input++;
565
566 *p++ = enc64[(( a << 4 ) | ((b >> 4) &0xf )) & 0x3f];
567
568 if( in_len-- <= 0 )
569 {
570 *p++ = enc64[ (b << 2) & 0x3f ];
571 *p++ = '=';
572 break;
573 }
574
575 a = *input++;
576
577 *p++ = enc64[ ((( b << 2 ) & 0x3f ) | ((a >> 6)& 0x3)) & 0x3f ];
578
579 *p++ = enc64[ a & 0x3f ];
580 }
581 *p = 0;
582
583 return output;
584 }
585 #endif
586 // wxUSE_STREAMS
587
588 #endif
589 // wxUSE_RICHTEXT