]> git.saurik.com Git - wxWidgets.git/blame - src/html/winpars.cpp
moved wxHtmlWindow flags out of defs.h
[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
VS
6// Copyright: (c) 1999 Vaclav Slavik
7// Licence: wxWindows Licence
8/////////////////////////////////////////////////////////////////////////////
9
10
11#ifdef __GNUG__
12#pragma implementation
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
VS
19
20#ifdef __BORDLANDC__
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"
5526e819
VS
34
35
36//-----------------------------------------------------------------------------
37// wxHtmlWinParser
38//-----------------------------------------------------------------------------
39
5526e819
VS
40
41wxList wxHtmlWinParser::m_Modules;
42
04db5c3f 43wxHtmlWinParser::wxHtmlWinParser(wxHtmlWindow *wnd) : wxHtmlParser()
5526e819 44{
211dfedd
VS
45 m_tmpStrBuf = NULL;
46 m_tmpStrBufSize = 0;
5526e819
VS
47 m_Window = wnd;
48 m_Container = NULL;
49 m_DC = NULL;
50 m_CharHeight = m_CharWidth = 0;
51 m_UseLink = FALSE;
b250d384
VS
52 m_EncConv = NULL;
53 m_InputEnc = m_OutputEnc = wxFONTENCODING_DEFAULT;
5526e819
VS
54
55 {
56 int i, j, k, l, m;
57 for (i = 0; i < 2; i++)
58 for (j = 0; j < 2; j++)
59 for (k = 0; k < 2; k++)
60 for (l = 0; l < 2; l++)
3c8c8da2 61 for (m = 0; m < 7; m++)
e3c7fd79 62 {
5526e819 63 m_FontsTable[i][j][k][l][m] = NULL;
f1ad10f3 64 m_FontsFacesTable[i][j][k][l][m] = wxEmptyString;
b250d384 65 m_FontsEncTable[i][j][k][l][m] = wxFONTENCODING_DEFAULT;
f1ad10f3 66 }
5526e819 67#ifdef __WXMSW__
1bb27b0c 68 static int default_sizes[7] = {7, 8, 10, 12, 16, 22, 30};
cc638fc6
VS
69#elif defined(__WXMAC__)
70 static int default_sizes[7] = {9, 12, 14, 18, 24, 30, 36};
5526e819 71#else
1bb27b0c 72 static int default_sizes[7] = {10, 12, 14, 16, 19, 24, 32};
5526e819 73#endif
8eb2940f 74 SetFonts("", "", default_sizes);
5526e819
VS
75 }
76
77 // fill in wxHtmlParser's tables:
78 wxNode *node = m_Modules.GetFirst();
3c8c8da2 79 while (node)
4f9297b0
VS
80 {
81 wxHtmlTagsModule *mod = (wxHtmlTagsModule*) node->GetData();
82 mod->FillHandlersTable(this);
83 node = node->GetNext();
5526e819
VS
84 }
85}
86
b250d384
VS
87wxHtmlWinParser::~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++)
3c8c8da2 95 for (m = 0; m < 7; m++)
e3c7fd79 96 {
3c8c8da2 97 if (m_FontsTable[i][j][k][l][m] != NULL)
b250d384
VS
98 delete m_FontsTable[i][j][k][l][m];
99 }
211dfedd
VS
100 delete m_EncConv;
101 delete[] m_tmpStrBuf;
b250d384
VS
102}
103
5526e819
VS
104void wxHtmlWinParser::AddModule(wxHtmlTagsModule *module)
105{
106 m_Modules.Append(module);
107}
108
f6bcfd97
BP
109void wxHtmlWinParser::RemoveModule(wxHtmlTagsModule *module)
110{
111 m_Modules.DeleteObject(module);
112}
113
8eb2940f 114void wxHtmlWinParser::SetFonts(wxString normal_face, wxString fixed_face, const int *sizes)
5526e819 115{
c9f56e70
VS
116 int i, j, k, l, m;
117
118 for (i = 0; i < 7; i++) m_FontsSizes[i] = sizes[i];
5526e819
VS
119 m_FontFaceFixed = fixed_face;
120 m_FontFaceNormal = normal_face;
3c8c8da2 121
b250d384 122 SetInputEncoding(m_InputEnc);
c9f56e70
VS
123
124 for (i = 0; i < 2; i++)
125 for (j = 0; j < 2; j++)
126 for (k = 0; k < 2; k++)
127 for (l = 0; l < 2; l++)
128 for (m = 0; m < 7; m++) {
3c8c8da2 129 if (m_FontsTable[i][j][k][l][m] != NULL)
e3c7fd79 130 {
c9f56e70
VS
131 delete m_FontsTable[i][j][k][l][m];
132 m_FontsTable[i][j][k][l][m] = NULL;
133 }
134 }
5526e819
VS
135}
136
5526e819
VS
137void wxHtmlWinParser::InitParser(const wxString& source)
138{
139 wxHtmlParser::InitParser(source);
0e8c8233 140 wxASSERT_MSG(m_DC != NULL, _T("no DC assigned to wxHtmlWinParser!!"));
5526e819
VS
141
142 m_FontBold = m_FontItalic = m_FontUnderlined = m_FontFixed = FALSE;
f2c2fa4d 143 m_FontSize = 3; //default one
5526e819 144 CreateCurrentFont(); // we're selecting default font into
4f9297b0 145 m_DC->GetTextExtent("H", &m_CharWidth, &m_CharHeight);
5526e819 146 /* NOTE : we're not using GetCharWidth/Height() because
0e8c8233 147 of differences under X and win
5526e819
VS
148 */
149
f2c2fa4d
VS
150 m_UseLink = FALSE;
151 m_Link = wxHtmlLinkInfo("", "");
5526e819
VS
152 m_LinkColor.Set(0, 0, 0xFF);
153 m_ActualColor.Set(0, 0, 0);
efba2b89 154 m_Align = wxHTML_ALIGN_LEFT;
5526e819
VS
155 m_tmpLastWasSpace = FALSE;
156
157 OpenContainer();
158
159 OpenContainer();
4f9297b0
VS
160 m_Container->InsertCell(new wxHtmlColourCell(m_ActualColor));
161 m_Container->InsertCell(new wxHtmlFontCell(CreateCurrentFont()));
5526e819
VS
162}
163
5526e819
VS
164void wxHtmlWinParser::DoneParser()
165{
166 m_Container = NULL;
b250d384 167 SetInputEncoding(wxFONTENCODING_DEFAULT); // for next call
5526e819
VS
168 wxHtmlParser::DoneParser();
169}
170
5526e819
VS
171wxObject* wxHtmlWinParser::GetProduct()
172{
173 wxHtmlContainerCell *top;
174
175 CloseContainer();
176 OpenContainer();
67cfebc2 177
5526e819 178 top = m_Container;
4f9297b0 179 while (top->GetParent()) top = top->GetParent();
5526e819
VS
180 return top;
181}
182
04db5c3f
VS
183bool wxHtmlWinParser::CanOpenURL(const wxString& url) const
184{
185 // FIXME - normalize the URL to full path before passing to
186 // OnOpeningURL!!
187 if ( m_Window )
188 return m_Window->OnOpeningURL(url);
189 else
190 return TRUE;
191}
5526e819 192
211dfedd 193void wxHtmlWinParser::AddText(const wxChar* txt)
5526e819
VS
194{
195 wxHtmlCell *c;
e3c7fd79
VZ
196 size_t i = 0,
197 x,
198 lng = wxStrlen(txt);
211dfedd 199 register wxChar d;
5526e819 200 int templen = 0;
f23e92e7 201 wxChar nbsp = GetEntitiesParser()->GetCharForCode(160 /* nbsp */);
211dfedd
VS
202
203 if (lng+1 > m_tmpStrBufSize)
204 {
205 delete[] m_tmpStrBuf;
206 m_tmpStrBuf = new wxChar[lng+1];
207 m_tmpStrBufSize = lng+1;
208 }
209 wxChar *temp = m_tmpStrBuf;
3c8c8da2
VZ
210
211 if (m_tmpLastWasSpace)
4f9297b0 212 {
3c8c8da2
VZ
213 while ((i < lng) &&
214 ((txt[i] == wxT('\n')) || (txt[i] == wxT('\r')) || (txt[i] == wxT(' ')) ||
211dfedd 215 (txt[i] == wxT('\t')))) i++;
5526e819
VS
216 }
217
3c8c8da2 218 while (i < lng)
4f9297b0 219 {
5526e819
VS
220 x = 0;
221 d = temp[templen++] = txt[i];
3c8c8da2 222 if ((d == wxT('\n')) || (d == wxT('\r')) || (d == wxT(' ')) || (d == wxT('\t')))
e3c7fd79 223 {
5526e819 224 i++, x++;
3c8c8da2 225 while ((i < lng) && ((txt[i] == wxT('\n')) || (txt[i] == wxT('\r')) ||
211dfedd 226 (txt[i] == wxT(' ')) || (txt[i] == wxT('\t')))) i++, x++;
5526e819
VS
227 }
228 else i++;
229
3c8c8da2 230 if (x)
e3c7fd79 231 {
211dfedd 232 temp[templen-1] = wxT(' ');
5526e819 233 temp[templen] = 0;
af035b26 234 if (templen == 1) continue;
5526e819 235 templen = 0;
3c8c8da2 236 if (m_EncConv)
daa616fc 237 m_EncConv->Convert(temp);
88dcf47c 238 size_t len = wxStrlen(temp);
f23e92e7 239 for (size_t j = 0; j < len; j++)
88dcf47c
VS
240 if (temp[j] == nbsp)
241 temp[j] = wxT(' ');
242 c = new wxHtmlWordCell(temp, *(GetDC()));
3c8c8da2 243 if (m_UseLink)
daa616fc 244 c->SetLink(m_Link);
4f9297b0 245 m_Container->InsertCell(c);
5526e819
VS
246 m_tmpLastWasSpace = TRUE;
247 }
248 }
af035b26
VS
249
250 if (templen && (templen > 1 || temp[0] != wxT(' ')))
4f9297b0 251 {
5526e819 252 temp[templen] = 0;
3c8c8da2 253 if (m_EncConv)
daa616fc 254 m_EncConv->Convert(temp);
88dcf47c 255 size_t len = wxStrlen(temp);
f23e92e7 256 for (size_t j = 0; j < len; j++)
88dcf47c
VS
257 if (temp[j] == nbsp)
258 temp[j] = wxT(' ');
259 c = new wxHtmlWordCell(temp, *(GetDC()));
211dfedd 260 if (m_UseLink)
daa616fc 261 c->SetLink(m_Link);
4f9297b0 262 m_Container->InsertCell(c);
5526e819
VS
263 m_tmpLastWasSpace = FALSE;
264 }
265}
266
267
268
269wxHtmlContainerCell* wxHtmlWinParser::OpenContainer()
270{
271 m_Container = new wxHtmlContainerCell(m_Container);
4f9297b0 272 m_Container->SetAlignHor(m_Align);
5526e819
VS
273 m_tmpLastWasSpace = TRUE;
274 /* to avoid space being first character in paragraph */
275 return m_Container;
276}
277
278
279
280wxHtmlContainerCell* wxHtmlWinParser::SetContainer(wxHtmlContainerCell *c)
281{
282 m_tmpLastWasSpace = TRUE;
283 /* to avoid space being first character in paragraph */
284 return m_Container = c;
285}
286
287
288
289wxHtmlContainerCell* wxHtmlWinParser::CloseContainer()
290{
4f9297b0 291 m_Container = m_Container->GetParent();
5526e819
VS
292 return m_Container;
293}
294
295
f2c2fa4d
VS
296void wxHtmlWinParser::SetFontSize(int s)
297{
298 if (s < 1) s = 1;
299 else if (s > 7) s = 7;
300 m_FontSize = s;
301}
302
303
304
5526e819
VS
305wxFont* wxHtmlWinParser::CreateCurrentFont()
306{
307 int fb = GetFontBold(),
308 fi = GetFontItalic(),
309 fu = GetFontUnderlined(),
310 ff = GetFontFixed(),
f2c2fa4d 311 fs = GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ;
5526e819 312
f1ad10f3
VS
313 wxString face = ff ? m_FontFaceFixed : m_FontFaceNormal;
314 wxString *faceptr = &(m_FontsFacesTable[fb][fi][fu][ff][fs]);
315 wxFont **fontptr = &(m_FontsTable[fb][fi][fu][ff][fs]);
b250d384 316 wxFontEncoding *encptr = &(m_FontsEncTable[fb][fi][fu][ff][fs]);
f1ad10f3 317
3c8c8da2 318 if (*fontptr != NULL && (*faceptr != face || *encptr != m_OutputEnc))
4f9297b0 319 {
f1ad10f3
VS
320 delete *fontptr;
321 *fontptr = NULL;
322 }
323
3c8c8da2 324 if (*fontptr == NULL)
4f9297b0 325 {
f1ad10f3 326 *faceptr = face;
b250d384 327 *encptr = m_OutputEnc;
f1ad10f3 328 *fontptr = new wxFont(
7a5e6267 329 (int) (m_FontsSizes[fs] * m_PixelScale),
f1ad10f3
VS
330 ff ? wxMODERN : wxSWISS,
331 fi ? wxITALIC : wxNORMAL,
332 fb ? wxBOLD : wxNORMAL,
b250d384
VS
333 fu ? TRUE : FALSE, face,
334 m_OutputEnc);
5526e819 335 }
4f9297b0 336 m_DC->SetFont(**fontptr);
f1ad10f3 337 return (*fontptr);
5526e819
VS
338}
339
340
341
f2c2fa4d
VS
342void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo& link)
343{
3c8c8da2 344 m_Link = link;
f2c2fa4d
VS
345 m_UseLink = (link.GetHref() != wxEmptyString);
346}
347
348
3c8c8da2 349void wxHtmlWinParser::SetFontFace(const wxString& face)
b250d384 350{
3c8c8da2 351 if (GetFontFixed()) m_FontFaceFixed = face;
b250d384
VS
352 else m_FontFaceNormal = face;
353
354 if (m_InputEnc != wxFONTENCODING_DEFAULT)
355 SetInputEncoding(m_InputEnc);
356}
357
358
359
360void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc)
361{
362 m_InputEnc = m_OutputEnc = wxFONTENCODING_DEFAULT;
3c8c8da2 363 if (m_EncConv)
daa616fc 364 {
3c8c8da2 365 delete m_EncConv;
daa616fc
VS
366 m_EncConv = NULL;
367 }
b250d384
VS
368
369 if (enc == wxFONTENCODING_DEFAULT) return;
370
371 wxFontEncoding altfix, altnorm;
372 bool availfix, availnorm;
3c8c8da2
VZ
373
374 // exact match?
4f9297b0
VS
375 availnorm = wxTheFontMapper->IsEncodingAvailable(enc, m_FontFaceNormal);
376 availfix = wxTheFontMapper->IsEncodingAvailable(enc, m_FontFaceFixed);
3c8c8da2 377 if (availnorm && availfix)
b250d384 378 m_OutputEnc = enc;
3c8c8da2 379
b250d384 380 // alternatives?
4f9297b0
VS
381 else if (wxTheFontMapper->GetAltForEncoding(enc, &altnorm, m_FontFaceNormal, FALSE) &&
382 wxTheFontMapper->GetAltForEncoding(enc, &altfix, m_FontFaceFixed, FALSE) &&
b250d384
VS
383 altnorm == altfix)
384 m_OutputEnc = altnorm;
3c8c8da2 385
b250d384
VS
386 // at least normal face?
387 else if (availnorm)
388 m_OutputEnc = enc;
4f9297b0 389 else if (wxTheFontMapper->GetAltForEncoding(enc, &altnorm, m_FontFaceNormal, FALSE))
b250d384 390 m_OutputEnc = altnorm;
3c8c8da2 391
b250d384
VS
392 // okay, let convert to ISO_8859-1, available always
393 else
394 m_OutputEnc = wxFONTENCODING_DEFAULT;
3c8c8da2 395
b250d384 396 m_InputEnc = enc;
daa616fc
VS
397 if (m_OutputEnc == wxFONTENCODING_DEFAULT)
398 GetEntitiesParser()->SetEncoding(wxFONTENCODING_SYSTEM);
399 else
400 GetEntitiesParser()->SetEncoding(m_OutputEnc);
3c8c8da2 401
b250d384
VS
402 if (m_InputEnc == m_OutputEnc) return;
403
404 m_EncConv = new wxEncodingConverter();
3c8c8da2 405 if (!m_EncConv->Init(m_InputEnc,
b250d384
VS
406 (m_OutputEnc == wxFONTENCODING_DEFAULT) ?
407 wxFONTENCODING_ISO8859_1 : m_OutputEnc,
3c8c8da2 408 wxCONVERT_SUBSTITUTE))
b250d384 409 { // total failture :-(
3c8c8da2
VZ
410 wxLogError(_("Failed to display HTML document in %s encoding"),
411 wxFontMapper::GetEncodingName(enc).c_str());
b250d384
VS
412 m_InputEnc = m_OutputEnc = wxFONTENCODING_DEFAULT;
413 delete m_EncConv;
414 m_EncConv = NULL;
415 }
416}
417
418
419
f2c2fa4d 420
5526e819
VS
421
422//-----------------------------------------------------------------------------
423// wxHtmlWinTagHandler
424//-----------------------------------------------------------------------------
425
426IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler, wxHtmlTagHandler)
427
428
429
430//-----------------------------------------------------------------------------
431// wxHtmlTagsModule
432//-----------------------------------------------------------------------------
433
434
435IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule, wxModule)
436
437
438bool wxHtmlTagsModule::OnInit()
439{
440 wxHtmlWinParser::AddModule(this);
441 return TRUE;
442}
443
444
445
446void wxHtmlTagsModule::OnExit()
447{
f6bcfd97 448 wxHtmlWinParser::RemoveModule(this);
5526e819 449}
223d09f6 450#endif
5526e819 451