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