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