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