]>
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++) | |
4f9297b0 VS |
61 | for (m = 0; m < 7; m++) |
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(); | |
4f9297b0 VS |
79 | while (node) |
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++) | |
4f9297b0 VS |
96 | for (m = 0; m < 7; m++) |
97 | { | |
b250d384 VS |
98 | if (m_FontsTable[i][j][k][l][m] != NULL) |
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; | |
b250d384 VS |
127 | |
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++) { | |
4f9297b0 VS |
135 | if (m_FontsTable[i][j][k][l][m] != NULL) |
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; | |
211dfedd VS |
199 | int i = 0, x, lng = wxStrlen(txt); |
200 | register wxChar d; | |
5526e819 | 201 | int templen = 0; |
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; | |
daa616fc | 210 | |
4f9297b0 VS |
211 | if (m_tmpLastWasSpace) |
212 | { | |
daa616fc | 213 | while ((i < lng) && |
211dfedd VS |
214 | ((txt[i] == wxT('\n')) || (txt[i] == wxT('\r')) || (txt[i] == wxT(' ')) || |
215 | (txt[i] == wxT('\t')))) i++; | |
5526e819 VS |
216 | } |
217 | ||
4f9297b0 VS |
218 | while (i < lng) |
219 | { | |
5526e819 VS |
220 | x = 0; |
221 | d = temp[templen++] = txt[i]; | |
211dfedd | 222 | if ((d == wxT('\n')) || (d == wxT('\r')) || (d == wxT(' ')) || (d == wxT('\t'))) |
4f9297b0 | 223 | { |
5526e819 | 224 | i++, x++; |
211dfedd VS |
225 | while ((i < lng) && ((txt[i] == wxT('\n')) || (txt[i] == wxT('\r')) || |
226 | (txt[i] == wxT(' ')) || (txt[i] == wxT('\t')))) i++, x++; | |
5526e819 VS |
227 | } |
228 | else i++; | |
229 | ||
4f9297b0 VS |
230 | if (x) |
231 | { | |
211dfedd | 232 | temp[templen-1] = wxT(' '); |
5526e819 VS |
233 | temp[templen] = 0; |
234 | templen = 0; | |
daa616fc VS |
235 | if (m_EncConv) |
236 | m_EncConv->Convert(temp); | |
237 | c = new wxHtmlWordCell(GetEntitiesParser()->Parse(temp), *(GetDC())); | |
238 | if (m_UseLink) | |
239 | c->SetLink(m_Link); | |
4f9297b0 | 240 | m_Container->InsertCell(c); |
5526e819 VS |
241 | m_tmpLastWasSpace = TRUE; |
242 | } | |
243 | } | |
4f9297b0 VS |
244 | if (templen) |
245 | { | |
5526e819 | 246 | temp[templen] = 0; |
daa616fc VS |
247 | if (m_EncConv) |
248 | m_EncConv->Convert(temp); | |
249 | c = new wxHtmlWordCell(GetEntitiesParser()->Parse(temp), *(GetDC())); | |
211dfedd | 250 | if (m_UseLink) |
daa616fc | 251 | c->SetLink(m_Link); |
4f9297b0 | 252 | m_Container->InsertCell(c); |
5526e819 VS |
253 | m_tmpLastWasSpace = FALSE; |
254 | } | |
255 | } | |
256 | ||
257 | ||
258 | ||
259 | wxHtmlContainerCell* wxHtmlWinParser::OpenContainer() | |
260 | { | |
261 | m_Container = new wxHtmlContainerCell(m_Container); | |
4f9297b0 | 262 | m_Container->SetAlignHor(m_Align); |
5526e819 VS |
263 | m_tmpLastWasSpace = TRUE; |
264 | /* to avoid space being first character in paragraph */ | |
265 | return m_Container; | |
266 | } | |
267 | ||
268 | ||
269 | ||
270 | wxHtmlContainerCell* wxHtmlWinParser::SetContainer(wxHtmlContainerCell *c) | |
271 | { | |
272 | m_tmpLastWasSpace = TRUE; | |
273 | /* to avoid space being first character in paragraph */ | |
274 | return m_Container = c; | |
275 | } | |
276 | ||
277 | ||
278 | ||
279 | wxHtmlContainerCell* wxHtmlWinParser::CloseContainer() | |
280 | { | |
4f9297b0 | 281 | m_Container = m_Container->GetParent(); |
5526e819 VS |
282 | return m_Container; |
283 | } | |
284 | ||
285 | ||
f2c2fa4d VS |
286 | void wxHtmlWinParser::SetFontSize(int s) |
287 | { | |
288 | if (s < 1) s = 1; | |
289 | else if (s > 7) s = 7; | |
290 | m_FontSize = s; | |
291 | } | |
292 | ||
293 | ||
294 | ||
5526e819 VS |
295 | wxFont* wxHtmlWinParser::CreateCurrentFont() |
296 | { | |
297 | int fb = GetFontBold(), | |
298 | fi = GetFontItalic(), | |
299 | fu = GetFontUnderlined(), | |
300 | ff = GetFontFixed(), | |
f2c2fa4d | 301 | fs = GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ; |
5526e819 | 302 | |
f1ad10f3 VS |
303 | wxString face = ff ? m_FontFaceFixed : m_FontFaceNormal; |
304 | wxString *faceptr = &(m_FontsFacesTable[fb][fi][fu][ff][fs]); | |
305 | wxFont **fontptr = &(m_FontsTable[fb][fi][fu][ff][fs]); | |
b250d384 | 306 | wxFontEncoding *encptr = &(m_FontsEncTable[fb][fi][fu][ff][fs]); |
f1ad10f3 | 307 | |
4f9297b0 VS |
308 | if (*fontptr != NULL && (*faceptr != face || *encptr != m_OutputEnc)) |
309 | { | |
f1ad10f3 VS |
310 | delete *fontptr; |
311 | *fontptr = NULL; | |
312 | } | |
313 | ||
4f9297b0 VS |
314 | if (*fontptr == NULL) |
315 | { | |
f1ad10f3 | 316 | *faceptr = face; |
b250d384 | 317 | *encptr = m_OutputEnc; |
f1ad10f3 | 318 | *fontptr = new wxFont( |
7a5e6267 | 319 | (int) (m_FontsSizes[fs] * m_PixelScale), |
f1ad10f3 VS |
320 | ff ? wxMODERN : wxSWISS, |
321 | fi ? wxITALIC : wxNORMAL, | |
322 | fb ? wxBOLD : wxNORMAL, | |
b250d384 VS |
323 | fu ? TRUE : FALSE, face, |
324 | m_OutputEnc); | |
5526e819 | 325 | } |
4f9297b0 | 326 | m_DC->SetFont(**fontptr); |
f1ad10f3 | 327 | return (*fontptr); |
5526e819 VS |
328 | } |
329 | ||
330 | ||
331 | ||
f2c2fa4d VS |
332 | void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo& link) |
333 | { | |
334 | m_Link = link; | |
335 | m_UseLink = (link.GetHref() != wxEmptyString); | |
336 | } | |
337 | ||
338 | ||
b250d384 VS |
339 | void wxHtmlWinParser::SetFontFace(const wxString& face) |
340 | { | |
341 | if (GetFontFixed()) m_FontFaceFixed = face; | |
342 | else m_FontFaceNormal = face; | |
343 | ||
344 | if (m_InputEnc != wxFONTENCODING_DEFAULT) | |
345 | SetInputEncoding(m_InputEnc); | |
346 | } | |
347 | ||
348 | ||
349 | ||
350 | void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc) | |
351 | { | |
352 | m_InputEnc = m_OutputEnc = wxFONTENCODING_DEFAULT; | |
daa616fc VS |
353 | if (m_EncConv) |
354 | { | |
355 | delete m_EncConv; | |
356 | m_EncConv = NULL; | |
357 | } | |
b250d384 VS |
358 | |
359 | if (enc == wxFONTENCODING_DEFAULT) return; | |
360 | ||
361 | wxFontEncoding altfix, altnorm; | |
362 | bool availfix, availnorm; | |
363 | ||
364 | // exact match? | |
4f9297b0 VS |
365 | availnorm = wxTheFontMapper->IsEncodingAvailable(enc, m_FontFaceNormal); |
366 | availfix = wxTheFontMapper->IsEncodingAvailable(enc, m_FontFaceFixed); | |
b250d384 VS |
367 | if (availnorm && availfix) |
368 | m_OutputEnc = enc; | |
369 | ||
370 | // alternatives? | |
4f9297b0 VS |
371 | else if (wxTheFontMapper->GetAltForEncoding(enc, &altnorm, m_FontFaceNormal, FALSE) && |
372 | wxTheFontMapper->GetAltForEncoding(enc, &altfix, m_FontFaceFixed, FALSE) && | |
b250d384 VS |
373 | altnorm == altfix) |
374 | m_OutputEnc = altnorm; | |
375 | ||
376 | // at least normal face? | |
377 | else if (availnorm) | |
378 | m_OutputEnc = enc; | |
4f9297b0 | 379 | else if (wxTheFontMapper->GetAltForEncoding(enc, &altnorm, m_FontFaceNormal, FALSE)) |
b250d384 VS |
380 | m_OutputEnc = altnorm; |
381 | ||
382 | // okay, let convert to ISO_8859-1, available always | |
383 | else | |
384 | m_OutputEnc = wxFONTENCODING_DEFAULT; | |
385 | ||
386 | m_InputEnc = enc; | |
daa616fc VS |
387 | if (m_OutputEnc == wxFONTENCODING_DEFAULT) |
388 | GetEntitiesParser()->SetEncoding(wxFONTENCODING_SYSTEM); | |
389 | else | |
390 | GetEntitiesParser()->SetEncoding(m_OutputEnc); | |
b250d384 VS |
391 | |
392 | if (m_InputEnc == m_OutputEnc) return; | |
393 | ||
394 | m_EncConv = new wxEncodingConverter(); | |
4f9297b0 | 395 | if (!m_EncConv->Init(m_InputEnc, |
b250d384 VS |
396 | (m_OutputEnc == wxFONTENCODING_DEFAULT) ? |
397 | wxFONTENCODING_ISO8859_1 : m_OutputEnc, | |
398 | wxCONVERT_SUBSTITUTE)) | |
399 | { // total failture :-( | |
f3c82859 | 400 | wxLogError(_("Failed to display HTML document in %s encoding"), |
f6bcfd97 | 401 | wxFontMapper::GetEncodingName(enc).c_str()); |
b250d384 VS |
402 | m_InputEnc = m_OutputEnc = wxFONTENCODING_DEFAULT; |
403 | delete m_EncConv; | |
404 | m_EncConv = NULL; | |
405 | } | |
406 | } | |
407 | ||
408 | ||
409 | ||
f2c2fa4d | 410 | |
5526e819 VS |
411 | |
412 | //----------------------------------------------------------------------------- | |
413 | // wxHtmlWinTagHandler | |
414 | //----------------------------------------------------------------------------- | |
415 | ||
416 | IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler, wxHtmlTagHandler) | |
417 | ||
418 | ||
419 | ||
420 | //----------------------------------------------------------------------------- | |
421 | // wxHtmlTagsModule | |
422 | //----------------------------------------------------------------------------- | |
423 | ||
424 | ||
425 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule, wxModule) | |
426 | ||
427 | ||
428 | bool wxHtmlTagsModule::OnInit() | |
429 | { | |
430 | wxHtmlWinParser::AddModule(this); | |
431 | return TRUE; | |
432 | } | |
433 | ||
434 | ||
435 | ||
436 | void wxHtmlTagsModule::OnExit() | |
437 | { | |
f6bcfd97 | 438 | wxHtmlWinParser::RemoveModule(this); |
5526e819 | 439 | } |
223d09f6 | 440 | #endif |
5526e819 | 441 |