]> git.saurik.com Git - wxWidgets.git/blob - src/html/winpars.cpp
WinCE build fixes.
[wxWidgets.git] / src / html / winpars.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: winpars.cpp
3 // Purpose: wxHtmlParser class (generic parser)
4 // Author: Vaclav Slavik
5 // RCS-ID: $Id$
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #include "wx/wxprec.h"
11
12 #include "wx/defs.h"
13 #if wxUSE_HTML && wxUSE_STREAMS
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WXPRECOMP
20 #include "wx/intl.h"
21 #include "wx/dc.h"
22 #endif
23
24 #include "wx/html/htmldefs.h"
25 #include "wx/html/winpars.h"
26 #include "wx/html/htmlwin.h"
27 #include "wx/fontmap.h"
28 #include "wx/log.h"
29 #include "wx/settings.h"
30 #include "wx/uri.h"
31
32
33 //-----------------------------------------------------------------------------
34 // wxHtmlWinParser
35 //-----------------------------------------------------------------------------
36
37 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinParser, wxHtmlParser)
38
39 wxList wxHtmlWinParser::m_Modules;
40
41 wxHtmlWinParser::wxHtmlWinParser(wxHtmlWindow *wnd) : wxHtmlParser()
42 {
43 m_tmpStrBuf = NULL;
44 m_tmpStrBufSize = 0;
45 m_Window = wnd;
46 m_Container = NULL;
47 m_DC = NULL;
48 m_CharHeight = m_CharWidth = 0;
49 m_UseLink = false;
50 #if !wxUSE_UNICODE
51 m_EncConv = NULL;
52 m_InputEnc = wxFONTENCODING_ISO8859_1;
53 m_OutputEnc = wxFONTENCODING_DEFAULT;
54 #endif
55 m_lastWordCell = NULL;
56
57 {
58 int i, j, k, l, m;
59 for (i = 0; i < 2; i++)
60 for (j = 0; j < 2; j++)
61 for (k = 0; k < 2; k++)
62 for (l = 0; l < 2; l++)
63 for (m = 0; m < 7; m++)
64 {
65 m_FontsTable[i][j][k][l][m] = NULL;
66 m_FontsFacesTable[i][j][k][l][m] = wxEmptyString;
67 #if !wxUSE_UNICODE
68 m_FontsEncTable[i][j][k][l][m] = wxFONTENCODING_DEFAULT;
69 #endif
70 }
71
72 SetFonts(wxEmptyString, wxEmptyString, NULL);
73 }
74
75 // fill in wxHtmlParser's tables:
76 wxList::compatibility_iterator node = m_Modules.GetFirst();
77 while (node)
78 {
79 wxHtmlTagsModule *mod = (wxHtmlTagsModule*) node->GetData();
80 mod->FillHandlersTable(this);
81 node = node->GetNext();
82 }
83 }
84
85 wxHtmlWinParser::~wxHtmlWinParser()
86 {
87 int i, j, k, l, m;
88
89 for (i = 0; i < 2; i++)
90 for (j = 0; j < 2; j++)
91 for (k = 0; k < 2; k++)
92 for (l = 0; l < 2; l++)
93 for (m = 0; m < 7; m++)
94 {
95 if (m_FontsTable[i][j][k][l][m] != NULL)
96 delete m_FontsTable[i][j][k][l][m];
97 }
98 #if !wxUSE_UNICODE
99 delete m_EncConv;
100 #endif
101 delete[] m_tmpStrBuf;
102 }
103
104 void wxHtmlWinParser::AddModule(wxHtmlTagsModule *module)
105 {
106 m_Modules.Append(module);
107 }
108
109 void wxHtmlWinParser::RemoveModule(wxHtmlTagsModule *module)
110 {
111 m_Modules.DeleteObject(module);
112 }
113
114 void wxHtmlWinParser::SetFonts(const wxString& normal_face, const wxString& fixed_face,
115 const int *sizes)
116 {
117 static int default_sizes[7] =
118 {
119 wxHTML_FONT_SIZE_1,
120 wxHTML_FONT_SIZE_2,
121 wxHTML_FONT_SIZE_3,
122 wxHTML_FONT_SIZE_4,
123 wxHTML_FONT_SIZE_5,
124 wxHTML_FONT_SIZE_6,
125 wxHTML_FONT_SIZE_7
126 };
127
128 if (sizes == NULL) sizes = default_sizes;
129
130 int i, j, k, l, m;
131
132 for (i = 0; i < 7; i++) m_FontsSizes[i] = sizes[i];
133 m_FontFaceFixed = fixed_face;
134 m_FontFaceNormal = normal_face;
135
136 #if !wxUSE_UNICODE
137 SetInputEncoding(m_InputEnc);
138 #endif
139
140 for (i = 0; i < 2; i++)
141 for (j = 0; j < 2; j++)
142 for (k = 0; k < 2; k++)
143 for (l = 0; l < 2; l++)
144 for (m = 0; m < 7; m++) {
145 if (m_FontsTable[i][j][k][l][m] != NULL)
146 {
147 delete m_FontsTable[i][j][k][l][m];
148 m_FontsTable[i][j][k][l][m] = NULL;
149 }
150 }
151 }
152
153 void wxHtmlWinParser::SetStandardFonts(int size,
154 const wxString& normal_face,
155 const wxString& fixed_face)
156 {
157 wxFont defaultFont = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
158
159 int f_sizes[7];
160 if (size == -1)
161 size = defaultFont.GetPointSize();
162
163 f_sizes[0] = int(size * 0.6);
164 f_sizes[1] = int(size * 0.8);
165 f_sizes[2] = size;
166 f_sizes[3] = int(size * 1.2);
167 f_sizes[4] = int(size * 1.4);
168 f_sizes[5] = int(size * 1.6);
169 f_sizes[6] = int(size * 1.8);
170
171 wxString normal = normal_face.empty() ?
172 defaultFont.GetFaceName() : normal_face;
173
174 SetFonts(normal, fixed_face, f_sizes);
175 }
176
177 void wxHtmlWinParser::InitParser(const wxString& source)
178 {
179 wxHtmlParser::InitParser(source);
180 wxASSERT_MSG(m_DC != NULL, wxT("no DC assigned to wxHtmlWinParser!!"));
181
182 m_FontBold = m_FontItalic = m_FontUnderlined = m_FontFixed = FALSE;
183 m_FontSize = 3; //default one
184 CreateCurrentFont(); // we're selecting default font into
185 m_DC->GetTextExtent( wxT("H"), &m_CharWidth, &m_CharHeight);
186 /* NOTE : we're not using GetCharWidth/Height() because
187 of differences under X and win
188 */
189
190 m_UseLink = false;
191 m_Link = wxHtmlLinkInfo( wxEmptyString );
192 m_LinkColor.Set(0, 0, 0xFF);
193 m_ActualColor.Set(0, 0, 0);
194 m_Align = wxHTML_ALIGN_LEFT;
195 m_tmpLastWasSpace = false;
196 m_lastWordCell = NULL;
197
198 OpenContainer();
199 OpenContainer();
200
201 #if !wxUSE_UNICODE
202 wxString charset = ExtractCharsetInformation(source);
203 if (!charset.empty())
204 {
205 wxFontEncoding enc = wxFontMapper::Get()->CharsetToEncoding(charset);
206 if (enc != wxFONTENCODING_SYSTEM)
207 SetInputEncoding(enc);
208 }
209 #endif
210
211 m_Container->InsertCell(new wxHtmlColourCell(m_ActualColor));
212 wxColour windowColour = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW) ;
213 m_Container->InsertCell(
214 new wxHtmlColourCell(GetWindow() ?
215 GetWindow()->GetBackgroundColour() :
216 windowColour,
217 wxHTML_CLR_BACKGROUND));
218 m_Container->InsertCell(new wxHtmlFontCell(CreateCurrentFont()));
219 }
220
221 void wxHtmlWinParser::DoneParser()
222 {
223 m_Container = NULL;
224 #if !wxUSE_UNICODE
225 SetInputEncoding(wxFONTENCODING_ISO8859_1); // for next call
226 #endif
227 wxHtmlParser::DoneParser();
228 }
229
230 wxObject* wxHtmlWinParser::GetProduct()
231 {
232 wxHtmlContainerCell *top;
233
234 CloseContainer();
235 OpenContainer();
236
237 top = m_Container;
238 while (top->GetParent()) top = top->GetParent();
239 top->RemoveExtraSpacing(true, true);
240
241 return top;
242 }
243
244 wxFSFile *wxHtmlWinParser::OpenURL(wxHtmlURLType type,
245 const wxString& url) const
246 {
247 if ( m_Window )
248 {
249 wxString myurl(url);
250 wxHtmlOpeningStatus status;
251 for (;;)
252 {
253 wxString myfullurl(myurl);
254
255 // consider url as absolute path first
256 wxURI current(myurl);
257 myfullurl = current.BuildUnescapedURI();
258
259 // if not absolute then ...
260 if( current.IsReference() )
261 {
262 wxString basepath = GetFS()->GetPath();
263 wxURI base(basepath);
264
265 // ... try to apply base path if valid ...
266 if( !base.IsReference() )
267 {
268 wxURI path(myfullurl);
269 path.Resolve( base );
270 myfullurl = path.BuildUnescapedURI();
271 }
272 else
273 {
274 // ... or force such addition if not included already
275 if( !current.GetPath().Contains(base.GetPath()) )
276 {
277 basepath += myurl;
278 wxURI connected( basepath );
279 myfullurl = connected.BuildUnescapedURI();
280 }
281 }
282 }
283
284 wxString redirect;
285 status = m_Window->OnOpeningURL(type, myfullurl, &redirect);
286 if ( status != wxHTML_REDIRECT )
287 break;
288
289 myurl = redirect;
290 }
291
292 if ( status == wxHTML_BLOCK )
293 return NULL;
294
295 return GetFS()->OpenFile(myurl);
296 }
297
298 return wxHtmlParser::OpenURL(type, url);
299 }
300
301 void wxHtmlWinParser::AddText(const wxChar* txt)
302 {
303 wxHtmlCell *c;
304 size_t i = 0,
305 x,
306 lng = wxStrlen(txt);
307 register wxChar d;
308 int templen = 0;
309 wxChar nbsp = GetEntitiesParser()->GetCharForCode(160 /* nbsp */);
310
311 if (lng+1 > m_tmpStrBufSize)
312 {
313 delete[] m_tmpStrBuf;
314 m_tmpStrBuf = new wxChar[lng+1];
315 m_tmpStrBufSize = lng+1;
316 }
317 wxChar *temp = m_tmpStrBuf;
318
319 if (m_tmpLastWasSpace)
320 {
321 while ((i < lng) &&
322 ((txt[i] == wxT('\n')) || (txt[i] == wxT('\r')) || (txt[i] == wxT(' ')) ||
323 (txt[i] == wxT('\t')))) i++;
324 }
325
326 while (i < lng)
327 {
328 x = 0;
329 d = temp[templen++] = txt[i];
330 if ((d == wxT('\n')) || (d == wxT('\r')) || (d == wxT(' ')) || (d == wxT('\t')))
331 {
332 i++, x++;
333 while ((i < lng) && ((txt[i] == wxT('\n')) || (txt[i] == wxT('\r')) ||
334 (txt[i] == wxT(' ')) || (txt[i] == wxT('\t')))) i++, x++;
335 }
336 else i++;
337
338 if (x)
339 {
340 temp[templen-1] = wxT(' ');
341 temp[templen] = 0;
342 templen = 0;
343 #if !wxUSE_UNICODE
344 if (m_EncConv)
345 m_EncConv->Convert(temp);
346 #endif
347 size_t len = wxStrlen(temp);
348 for (size_t j = 0; j < len; j++)
349 if (temp[j] == nbsp)
350 temp[j] = wxT(' ');
351 c = new wxHtmlWordCell(temp, *(GetDC()));
352 if (m_UseLink)
353 c->SetLink(m_Link);
354 m_Container->InsertCell(c);
355 ((wxHtmlWordCell*)c)->SetPreviousWord(m_lastWordCell);
356 m_lastWordCell = (wxHtmlWordCell*)c;
357 m_tmpLastWasSpace = true;
358 }
359 }
360
361 if (templen && (templen > 1 || temp[0] != wxT(' ')))
362 {
363 temp[templen] = 0;
364 #if !wxUSE_UNICODE
365 if (m_EncConv)
366 m_EncConv->Convert(temp);
367 #endif
368 size_t len = wxStrlen(temp);
369 for (size_t j = 0; j < len; j++)
370 if (temp[j] == nbsp)
371 temp[j] = wxT(' ');
372 c = new wxHtmlWordCell(temp, *(GetDC()));
373 if (m_UseLink)
374 c->SetLink(m_Link);
375 m_Container->InsertCell(c);
376 ((wxHtmlWordCell*)c)->SetPreviousWord(m_lastWordCell);
377 m_lastWordCell = (wxHtmlWordCell*)c;
378 m_tmpLastWasSpace = false;
379 }
380 }
381
382
383
384 wxHtmlContainerCell* wxHtmlWinParser::OpenContainer()
385 {
386 m_Container = new wxHtmlContainerCell(m_Container);
387 m_Container->SetAlignHor(m_Align);
388 m_tmpLastWasSpace = true;
389 /* to avoid space being first character in paragraph */
390 return m_Container;
391 }
392
393
394
395 wxHtmlContainerCell* wxHtmlWinParser::SetContainer(wxHtmlContainerCell *c)
396 {
397 m_tmpLastWasSpace = true;
398 /* to avoid space being first character in paragraph */
399 return m_Container = c;
400 }
401
402
403
404 wxHtmlContainerCell* wxHtmlWinParser::CloseContainer()
405 {
406 m_Container = m_Container->GetParent();
407 return m_Container;
408 }
409
410
411 void wxHtmlWinParser::SetFontSize(int s)
412 {
413 if (s < 1) s = 1;
414 else if (s > 7) s = 7;
415 m_FontSize = s;
416 }
417
418
419
420 wxFont* wxHtmlWinParser::CreateCurrentFont()
421 {
422 int fb = GetFontBold(),
423 fi = GetFontItalic(),
424 fu = GetFontUnderlined(),
425 ff = GetFontFixed(),
426 fs = GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ;
427
428 wxString face = ff ? m_FontFaceFixed : m_FontFaceNormal;
429 wxString *faceptr = &(m_FontsFacesTable[fb][fi][fu][ff][fs]);
430 wxFont **fontptr = &(m_FontsTable[fb][fi][fu][ff][fs]);
431 #if !wxUSE_UNICODE
432 wxFontEncoding *encptr = &(m_FontsEncTable[fb][fi][fu][ff][fs]);
433 #endif
434
435 if (*fontptr != NULL && (*faceptr != face
436 #if !wxUSE_UNICODE
437 || *encptr != m_OutputEnc
438 #endif
439 ))
440 {
441 delete *fontptr;
442 *fontptr = NULL;
443 }
444
445 if (*fontptr == NULL)
446 {
447 *faceptr = face;
448 *fontptr = new wxFont(
449 (int) (m_FontsSizes[fs] * m_PixelScale),
450 ff ? wxMODERN : wxSWISS,
451 fi ? wxITALIC : wxNORMAL,
452 fb ? wxBOLD : wxNORMAL,
453 fu ? true : false, face
454 #if wxUSE_UNICODE
455 );
456 #else
457 , m_OutputEnc);
458 *encptr = m_OutputEnc;
459 #endif
460 }
461 m_DC->SetFont(**fontptr);
462 return (*fontptr);
463 }
464
465
466
467 void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo& link)
468 {
469 m_Link = link;
470 m_UseLink = (link.GetHref() != wxEmptyString);
471 }
472
473
474 void wxHtmlWinParser::SetFontFace(const wxString& face)
475 {
476 if (GetFontFixed()) m_FontFaceFixed = face;
477 else m_FontFaceNormal = face;
478
479 #if !wxUSE_UNICODE
480 if (m_InputEnc != wxFONTENCODING_DEFAULT)
481 SetInputEncoding(m_InputEnc);
482 #endif
483 }
484
485
486
487 #if !wxUSE_UNICODE
488 void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc)
489 {
490 m_InputEnc = m_OutputEnc = wxFONTENCODING_DEFAULT;
491 if (m_EncConv)
492 {
493 delete m_EncConv;
494 m_EncConv = NULL;
495 }
496
497 if (enc == wxFONTENCODING_DEFAULT) return;
498
499 wxFontEncoding altfix, altnorm;
500 bool availfix, availnorm;
501
502 // exact match?
503 availnorm = wxFontMapper::Get()->IsEncodingAvailable(enc, m_FontFaceNormal);
504 availfix = wxFontMapper::Get()->IsEncodingAvailable(enc, m_FontFaceFixed);
505 if (availnorm && availfix)
506 m_OutputEnc = enc;
507
508 // alternatives?
509 else if (wxFontMapper::Get()->GetAltForEncoding(enc, &altnorm, m_FontFaceNormal, false) &&
510 wxFontMapper::Get()->GetAltForEncoding(enc, &altfix, m_FontFaceFixed, false) &&
511 altnorm == altfix)
512 m_OutputEnc = altnorm;
513
514 // at least normal face?
515 else if (availnorm)
516 m_OutputEnc = enc;
517 else if (wxFontMapper::Get()->GetAltForEncoding(enc, &altnorm, m_FontFaceNormal, false))
518 m_OutputEnc = altnorm;
519
520 else
521 {
522 #ifndef __WXMAC__
523 // okay, let convert to ISO_8859-1, available always
524 m_OutputEnc = wxFONTENCODING_DEFAULT;
525 #else
526 m_OutputEnc = wxLocale::GetSystemEncoding() ;
527 #endif
528 }
529
530 m_InputEnc = enc;
531 if (m_OutputEnc == wxFONTENCODING_DEFAULT)
532 GetEntitiesParser()->SetEncoding(wxFONTENCODING_SYSTEM);
533 else
534 GetEntitiesParser()->SetEncoding(m_OutputEnc);
535
536 if (m_InputEnc == m_OutputEnc) return;
537
538 m_EncConv = new wxEncodingConverter();
539 if (!m_EncConv->Init(m_InputEnc,
540 (m_OutputEnc == wxFONTENCODING_DEFAULT) ?
541 wxFONTENCODING_ISO8859_1 : m_OutputEnc,
542 wxCONVERT_SUBSTITUTE))
543 { // total failture :-(
544 wxLogError(_("Failed to display HTML document in %s encoding"),
545 wxFontMapper::GetEncodingName(enc).c_str());
546 m_InputEnc = m_OutputEnc = wxFONTENCODING_DEFAULT;
547 delete m_EncConv;
548 m_EncConv = NULL;
549 }
550 }
551 #endif
552
553
554
555
556 //-----------------------------------------------------------------------------
557 // wxHtmlWinTagHandler
558 //-----------------------------------------------------------------------------
559
560 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler, wxHtmlTagHandler)
561
562 //-----------------------------------------------------------------------------
563 // wxHtmlTagsModule
564 //-----------------------------------------------------------------------------
565
566 // NB: This is *NOT* winpars.cpp's initialization and shutdown code!!
567 // This module is an ancestor for tag handlers modules defined
568 // in m_*.cpp files with TAGS_MODULE_BEGIN...TAGS_MODULE_END construct.
569 //
570 // Do not add any winpars.cpp shutdown or initialization code to it,
571 // create a new module instead!
572
573 IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule, wxModule)
574
575 bool wxHtmlTagsModule::OnInit()
576 {
577 wxHtmlWinParser::AddModule(this);
578 return true;
579 }
580
581 void wxHtmlTagsModule::OnExit()
582 {
583 wxHtmlWinParser::RemoveModule(this);
584 }
585
586 #endif
587