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