]> git.saurik.com Git - wxWidgets.git/blame - src/html/winpars.cpp
fixed menubar attaching: create it first, THEN set pointer to it
[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
0423bdc7 183wxFSFile *wxHtmlWinParser::OpenURL(wxHtmlURLType type,
6cc4e6b8 184 const wxString& url) const
04db5c3f
VS
185{
186 // FIXME - normalize the URL to full path before passing to
187 // OnOpeningURL!!
188 if ( m_Window )
6cc4e6b8 189 {
6cc4e6b8
VS
190 wxString myurl(url);
191 wxHtmlOpeningStatus status;
192 for (;;)
193 {
0423bdc7
VZ
194 wxString redirect;
195 status = m_Window->OnOpeningURL(type, myurl, &redirect);
196 if ( status != wxHTML_REDIRECT )
6cc4e6b8 197 break;
0423bdc7
VZ
198
199 myurl = redirect;
6cc4e6b8 200 }
0423bdc7 201
6cc4e6b8
VS
202 if ( status == wxHTML_BLOCK )
203 return NULL;
2c892c0b
VZ
204
205 return GetFS()->OpenFile(myurl);
6cc4e6b8 206 }
2c892c0b
VZ
207
208 return wxHtmlParser::OpenURL(type, url);
04db5c3f 209}
5526e819 210
211dfedd 211void wxHtmlWinParser::AddText(const wxChar* txt)
5526e819
VS
212{
213 wxHtmlCell *c;
e3c7fd79
VZ
214 size_t i = 0,
215 x,
216 lng = wxStrlen(txt);
211dfedd 217 register wxChar d;
5526e819 218 int templen = 0;
f23e92e7 219 wxChar nbsp = GetEntitiesParser()->GetCharForCode(160 /* nbsp */);
211dfedd
VS
220
221 if (lng+1 > m_tmpStrBufSize)
222 {
223 delete[] m_tmpStrBuf;
224 m_tmpStrBuf = new wxChar[lng+1];
225 m_tmpStrBufSize = lng+1;
226 }
227 wxChar *temp = m_tmpStrBuf;
3c8c8da2
VZ
228
229 if (m_tmpLastWasSpace)
4f9297b0 230 {
3c8c8da2
VZ
231 while ((i < lng) &&
232 ((txt[i] == wxT('\n')) || (txt[i] == wxT('\r')) || (txt[i] == wxT(' ')) ||
211dfedd 233 (txt[i] == wxT('\t')))) i++;
5526e819
VS
234 }
235
3c8c8da2 236 while (i < lng)
4f9297b0 237 {
5526e819
VS
238 x = 0;
239 d = temp[templen++] = txt[i];
3c8c8da2 240 if ((d == wxT('\n')) || (d == wxT('\r')) || (d == wxT(' ')) || (d == wxT('\t')))
e3c7fd79 241 {
5526e819 242 i++, x++;
3c8c8da2 243 while ((i < lng) && ((txt[i] == wxT('\n')) || (txt[i] == wxT('\r')) ||
211dfedd 244 (txt[i] == wxT(' ')) || (txt[i] == wxT('\t')))) i++, x++;
5526e819
VS
245 }
246 else i++;
247
3c8c8da2 248 if (x)
e3c7fd79 249 {
211dfedd 250 temp[templen-1] = wxT(' ');
5526e819 251 temp[templen] = 0;
af035b26 252 if (templen == 1) continue;
5526e819 253 templen = 0;
3c8c8da2 254 if (m_EncConv)
daa616fc 255 m_EncConv->Convert(temp);
88dcf47c 256 size_t len = wxStrlen(temp);
f23e92e7 257 for (size_t j = 0; j < len; j++)
88dcf47c
VS
258 if (temp[j] == nbsp)
259 temp[j] = wxT(' ');
260 c = new wxHtmlWordCell(temp, *(GetDC()));
3c8c8da2 261 if (m_UseLink)
daa616fc 262 c->SetLink(m_Link);
4f9297b0 263 m_Container->InsertCell(c);
5526e819
VS
264 m_tmpLastWasSpace = TRUE;
265 }
266 }
af035b26
VS
267
268 if (templen && (templen > 1 || temp[0] != wxT(' ')))
4f9297b0 269 {
5526e819 270 temp[templen] = 0;
3c8c8da2 271 if (m_EncConv)
daa616fc 272 m_EncConv->Convert(temp);
88dcf47c 273 size_t len = wxStrlen(temp);
f23e92e7 274 for (size_t j = 0; j < len; j++)
88dcf47c
VS
275 if (temp[j] == nbsp)
276 temp[j] = wxT(' ');
277 c = new wxHtmlWordCell(temp, *(GetDC()));
211dfedd 278 if (m_UseLink)
daa616fc 279 c->SetLink(m_Link);
4f9297b0 280 m_Container->InsertCell(c);
5526e819
VS
281 m_tmpLastWasSpace = FALSE;
282 }
283}
284
285
286
287wxHtmlContainerCell* wxHtmlWinParser::OpenContainer()
288{
289 m_Container = new wxHtmlContainerCell(m_Container);
4f9297b0 290 m_Container->SetAlignHor(m_Align);
5526e819
VS
291 m_tmpLastWasSpace = TRUE;
292 /* to avoid space being first character in paragraph */
293 return m_Container;
294}
295
296
297
298wxHtmlContainerCell* wxHtmlWinParser::SetContainer(wxHtmlContainerCell *c)
299{
300 m_tmpLastWasSpace = TRUE;
301 /* to avoid space being first character in paragraph */
302 return m_Container = c;
303}
304
305
306
307wxHtmlContainerCell* wxHtmlWinParser::CloseContainer()
308{
4f9297b0 309 m_Container = m_Container->GetParent();
5526e819
VS
310 return m_Container;
311}
312
313
f2c2fa4d
VS
314void wxHtmlWinParser::SetFontSize(int s)
315{
316 if (s < 1) s = 1;
317 else if (s > 7) s = 7;
318 m_FontSize = s;
319}
320
321
322
5526e819
VS
323wxFont* wxHtmlWinParser::CreateCurrentFont()
324{
325 int fb = GetFontBold(),
326 fi = GetFontItalic(),
327 fu = GetFontUnderlined(),
328 ff = GetFontFixed(),
f2c2fa4d 329 fs = GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ;
5526e819 330
f1ad10f3
VS
331 wxString face = ff ? m_FontFaceFixed : m_FontFaceNormal;
332 wxString *faceptr = &(m_FontsFacesTable[fb][fi][fu][ff][fs]);
333 wxFont **fontptr = &(m_FontsTable[fb][fi][fu][ff][fs]);
b250d384 334 wxFontEncoding *encptr = &(m_FontsEncTable[fb][fi][fu][ff][fs]);
f1ad10f3 335
3c8c8da2 336 if (*fontptr != NULL && (*faceptr != face || *encptr != m_OutputEnc))
4f9297b0 337 {
f1ad10f3
VS
338 delete *fontptr;
339 *fontptr = NULL;
340 }
341
3c8c8da2 342 if (*fontptr == NULL)
4f9297b0 343 {
f1ad10f3 344 *faceptr = face;
b250d384 345 *encptr = m_OutputEnc;
f1ad10f3 346 *fontptr = new wxFont(
7a5e6267 347 (int) (m_FontsSizes[fs] * m_PixelScale),
f1ad10f3
VS
348 ff ? wxMODERN : wxSWISS,
349 fi ? wxITALIC : wxNORMAL,
350 fb ? wxBOLD : wxNORMAL,
b250d384
VS
351 fu ? TRUE : FALSE, face,
352 m_OutputEnc);
5526e819 353 }
4f9297b0 354 m_DC->SetFont(**fontptr);
f1ad10f3 355 return (*fontptr);
5526e819
VS
356}
357
358
359
f2c2fa4d
VS
360void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo& link)
361{
3c8c8da2 362 m_Link = link;
f2c2fa4d
VS
363 m_UseLink = (link.GetHref() != wxEmptyString);
364}
365
366
3c8c8da2 367void wxHtmlWinParser::SetFontFace(const wxString& face)
b250d384 368{
3c8c8da2 369 if (GetFontFixed()) m_FontFaceFixed = face;
b250d384
VS
370 else m_FontFaceNormal = face;
371
372 if (m_InputEnc != wxFONTENCODING_DEFAULT)
373 SetInputEncoding(m_InputEnc);
374}
375
376
377
378void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc)
379{
380 m_InputEnc = m_OutputEnc = wxFONTENCODING_DEFAULT;
3c8c8da2 381 if (m_EncConv)
daa616fc 382 {
3c8c8da2 383 delete m_EncConv;
daa616fc
VS
384 m_EncConv = NULL;
385 }
b250d384
VS
386
387 if (enc == wxFONTENCODING_DEFAULT) return;
388
389 wxFontEncoding altfix, altnorm;
390 bool availfix, availnorm;
3c8c8da2
VZ
391
392 // exact match?
4f9297b0
VS
393 availnorm = wxTheFontMapper->IsEncodingAvailable(enc, m_FontFaceNormal);
394 availfix = wxTheFontMapper->IsEncodingAvailable(enc, m_FontFaceFixed);
3c8c8da2 395 if (availnorm && availfix)
b250d384 396 m_OutputEnc = enc;
3c8c8da2 397
b250d384 398 // alternatives?
4f9297b0
VS
399 else if (wxTheFontMapper->GetAltForEncoding(enc, &altnorm, m_FontFaceNormal, FALSE) &&
400 wxTheFontMapper->GetAltForEncoding(enc, &altfix, m_FontFaceFixed, FALSE) &&
b250d384
VS
401 altnorm == altfix)
402 m_OutputEnc = altnorm;
3c8c8da2 403
b250d384
VS
404 // at least normal face?
405 else if (availnorm)
406 m_OutputEnc = enc;
4f9297b0 407 else if (wxTheFontMapper->GetAltForEncoding(enc, &altnorm, m_FontFaceNormal, FALSE))
b250d384 408 m_OutputEnc = altnorm;
3c8c8da2 409
b250d384
VS
410 // okay, let convert to ISO_8859-1, available always
411 else
412 m_OutputEnc = wxFONTENCODING_DEFAULT;
3c8c8da2 413
b250d384 414 m_InputEnc = enc;
daa616fc
VS
415 if (m_OutputEnc == wxFONTENCODING_DEFAULT)
416 GetEntitiesParser()->SetEncoding(wxFONTENCODING_SYSTEM);
417 else
418 GetEntitiesParser()->SetEncoding(m_OutputEnc);
3c8c8da2 419
b250d384
VS
420 if (m_InputEnc == m_OutputEnc) return;
421
422 m_EncConv = new wxEncodingConverter();
3c8c8da2 423 if (!m_EncConv->Init(m_InputEnc,
b250d384
VS
424 (m_OutputEnc == wxFONTENCODING_DEFAULT) ?
425 wxFONTENCODING_ISO8859_1 : m_OutputEnc,
3c8c8da2 426 wxCONVERT_SUBSTITUTE))
b250d384 427 { // total failture :-(
3c8c8da2
VZ
428 wxLogError(_("Failed to display HTML document in %s encoding"),
429 wxFontMapper::GetEncodingName(enc).c_str());
b250d384
VS
430 m_InputEnc = m_OutputEnc = wxFONTENCODING_DEFAULT;
431 delete m_EncConv;
432 m_EncConv = NULL;
433 }
434}
435
436
437
f2c2fa4d 438
5526e819
VS
439
440//-----------------------------------------------------------------------------
441// wxHtmlWinTagHandler
442//-----------------------------------------------------------------------------
443
444IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler, wxHtmlTagHandler)
445
5526e819
VS
446//-----------------------------------------------------------------------------
447// wxHtmlTagsModule
448//-----------------------------------------------------------------------------
449
d6a6d666
VS
450// NB: This is *NOT* winpars.cpp's initialization and shutdown code!!
451// This module is an ancestor for tag handlers modules defined
452// in m_*.cpp files with TAGS_MODULE_BEGIN...TAGS_MODULE_END construct.
453//
454// Do not add any winpars.cpp shutdown or initialization code to it,
455// create a new module instead!
5526e819
VS
456
457IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule, wxModule)
458
5526e819
VS
459bool wxHtmlTagsModule::OnInit()
460{
461 wxHtmlWinParser::AddModule(this);
462 return TRUE;
463}
464
5526e819
VS
465void wxHtmlTagsModule::OnExit()
466{
f6bcfd97 467 wxHtmlWinParser::RemoveModule(this);
5526e819 468}
d6a6d666 469
223d09f6 470#endif
5526e819 471