]>
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" | |
18 | #if wxUSE_HTML | |
19 | ||
20 | #ifdef __BORDLANDC__ | |
21 | #pragma hdrstop | |
22 | #endif | |
23 | ||
24 | #ifndef WXPRECOMP | |
3096bd2f | 25 | #include "wx/wx.h" |
5526e819 VS |
26 | #endif |
27 | ||
69941f05 VS |
28 | #include "wx/html/htmldefs.h" |
29 | #include "wx/html/winpars.h" | |
30 | #include "wx/html/htmlwin.h" | |
b250d384 | 31 | #include "wx/fontmap.h" |
5526e819 VS |
32 | |
33 | ||
34 | //----------------------------------------------------------------------------- | |
35 | // wxHtmlWinParser | |
36 | //----------------------------------------------------------------------------- | |
37 | ||
5526e819 VS |
38 | |
39 | wxList wxHtmlWinParser::m_Modules; | |
40 | ||
41 | wxHtmlWinParser::wxHtmlWinParser(wxWindow *wnd) : wxHtmlParser() | |
42 | { | |
43 | m_Window = wnd; | |
44 | m_Container = NULL; | |
45 | m_DC = NULL; | |
46 | m_CharHeight = m_CharWidth = 0; | |
47 | m_UseLink = FALSE; | |
b250d384 VS |
48 | m_EncConv = NULL; |
49 | m_InputEnc = m_OutputEnc = wxFONTENCODING_DEFAULT; | |
5526e819 VS |
50 | |
51 | { | |
52 | int i, j, k, l, m; | |
53 | for (i = 0; i < 2; i++) | |
54 | for (j = 0; j < 2; j++) | |
55 | for (k = 0; k < 2; k++) | |
56 | for (l = 0; l < 2; l++) | |
f1ad10f3 | 57 | for (m = 0; m < 7; m++) { |
5526e819 | 58 | m_FontsTable[i][j][k][l][m] = NULL; |
f1ad10f3 | 59 | m_FontsFacesTable[i][j][k][l][m] = wxEmptyString; |
b250d384 | 60 | m_FontsEncTable[i][j][k][l][m] = wxFONTENCODING_DEFAULT; |
f1ad10f3 | 61 | } |
5526e819 | 62 | #ifdef __WXMSW__ |
1bb27b0c | 63 | static int default_sizes[7] = {7, 8, 10, 12, 16, 22, 30}; |
5526e819 | 64 | #else |
1bb27b0c | 65 | static int default_sizes[7] = {10, 12, 14, 16, 19, 24, 32}; |
5526e819 | 66 | #endif |
8eb2940f | 67 | SetFonts("", "", default_sizes); |
5526e819 VS |
68 | } |
69 | ||
70 | // fill in wxHtmlParser's tables: | |
71 | wxNode *node = m_Modules.GetFirst(); | |
c9f56e70 | 72 | while (node) { |
5526e819 VS |
73 | wxHtmlTagsModule *mod = (wxHtmlTagsModule*) node -> GetData(); |
74 | mod -> FillHandlersTable(this); | |
75 | node = node -> GetNext(); | |
76 | } | |
77 | } | |
78 | ||
79 | ||
b250d384 VS |
80 | wxHtmlWinParser::~wxHtmlWinParser() |
81 | { | |
82 | int i, j, k, l, m; | |
83 | ||
84 | for (i = 0; i < 2; i++) | |
85 | for (j = 0; j < 2; j++) | |
86 | for (k = 0; k < 2; k++) | |
87 | for (l = 0; l < 2; l++) | |
88 | for (m = 0; m < 7; m++) { | |
89 | if (m_FontsTable[i][j][k][l][m] != NULL) | |
90 | delete m_FontsTable[i][j][k][l][m]; | |
91 | } | |
92 | if (m_EncConv) delete m_EncConv; | |
93 | } | |
94 | ||
5526e819 VS |
95 | |
96 | void wxHtmlWinParser::AddModule(wxHtmlTagsModule *module) | |
97 | { | |
98 | m_Modules.Append(module); | |
99 | } | |
100 | ||
101 | ||
102 | ||
8eb2940f | 103 | void wxHtmlWinParser::SetFonts(wxString normal_face, wxString fixed_face, const int *sizes) |
5526e819 | 104 | { |
c9f56e70 VS |
105 | int i, j, k, l, m; |
106 | ||
107 | for (i = 0; i < 7; i++) m_FontsSizes[i] = sizes[i]; | |
5526e819 VS |
108 | m_FontFaceFixed = fixed_face; |
109 | m_FontFaceNormal = normal_face; | |
b250d384 VS |
110 | |
111 | SetInputEncoding(m_InputEnc); | |
c9f56e70 VS |
112 | |
113 | for (i = 0; i < 2; i++) | |
114 | for (j = 0; j < 2; j++) | |
115 | for (k = 0; k < 2; k++) | |
116 | for (l = 0; l < 2; l++) | |
117 | for (m = 0; m < 7; m++) { | |
118 | if (m_FontsTable[i][j][k][l][m] != NULL) { | |
119 | delete m_FontsTable[i][j][k][l][m]; | |
120 | m_FontsTable[i][j][k][l][m] = NULL; | |
121 | } | |
122 | } | |
5526e819 VS |
123 | } |
124 | ||
125 | ||
126 | ||
127 | void wxHtmlWinParser::InitParser(const wxString& source) | |
128 | { | |
129 | wxHtmlParser::InitParser(source); | |
130 | wxASSERT_MSG(m_DC != NULL, _("no DC assigned to wxHtmlWinParser!!")); | |
131 | ||
132 | m_FontBold = m_FontItalic = m_FontUnderlined = m_FontFixed = FALSE; | |
f2c2fa4d | 133 | m_FontSize = 3; //default one |
5526e819 VS |
134 | CreateCurrentFont(); // we're selecting default font into |
135 | m_DC -> GetTextExtent("H", &m_CharWidth, &m_CharHeight); | |
136 | /* NOTE : we're not using GetCharWidth/Height() because | |
137 | of differences under X and win | |
138 | */ | |
139 | ||
f2c2fa4d VS |
140 | m_UseLink = FALSE; |
141 | m_Link = wxHtmlLinkInfo("", ""); | |
5526e819 VS |
142 | m_LinkColor.Set(0, 0, 0xFF); |
143 | m_ActualColor.Set(0, 0, 0); | |
efba2b89 | 144 | m_Align = wxHTML_ALIGN_LEFT; |
5526e819 VS |
145 | m_tmpLastWasSpace = FALSE; |
146 | ||
147 | OpenContainer(); | |
148 | ||
149 | OpenContainer(); | |
150 | m_Container -> InsertCell(new wxHtmlColourCell(m_ActualColor)); | |
151 | m_Container -> InsertCell(new wxHtmlFontCell(CreateCurrentFont())); | |
152 | } | |
153 | ||
154 | ||
155 | ||
156 | void wxHtmlWinParser::DoneParser() | |
157 | { | |
158 | m_Container = NULL; | |
b250d384 | 159 | SetInputEncoding(wxFONTENCODING_DEFAULT); // for next call |
5526e819 VS |
160 | wxHtmlParser::DoneParser(); |
161 | } | |
162 | ||
163 | ||
164 | ||
165 | wxObject* wxHtmlWinParser::GetProduct() | |
166 | { | |
167 | wxHtmlContainerCell *top; | |
168 | ||
169 | CloseContainer(); | |
170 | OpenContainer(); | |
67cfebc2 | 171 | |
5526e819 VS |
172 | top = m_Container; |
173 | while (top -> GetParent()) top = top -> GetParent(); | |
174 | return top; | |
175 | } | |
176 | ||
177 | ||
178 | ||
5526e819 VS |
179 | void wxHtmlWinParser::AddText(const char* txt) |
180 | { | |
181 | wxHtmlCell *c; | |
182 | int i = 0, x, lng = strlen(txt); | |
efba2b89 | 183 | char temp[wxHTML_BUFLEN]; |
5526e819 VS |
184 | register char d; |
185 | int templen = 0; | |
186 | ||
187 | if (m_tmpLastWasSpace) { | |
188 | while ((i < lng) && ((txt[i] == '\n') || (txt[i] == '\r') || (txt[i] == ' ') || (txt[i] == '\t'))) i++; | |
189 | } | |
190 | ||
191 | while (i < lng) { | |
192 | x = 0; | |
193 | d = temp[templen++] = txt[i]; | |
194 | if ((d == '\n') || (d == '\r') || (d == ' ') || (d == '\t')) { | |
195 | i++, x++; | |
196 | while ((i < lng) && ((txt[i] == '\n') || (txt[i] == '\r') || (txt[i] == ' ') || (txt[i] == '\t'))) i++, x++; | |
197 | } | |
198 | else i++; | |
199 | ||
200 | if (x) { | |
201 | temp[templen-1] = ' '; | |
202 | temp[templen] = 0; | |
203 | templen = 0; | |
b250d384 | 204 | if (m_EncConv) m_EncConv -> Convert(temp); |
5526e819 VS |
205 | c = new wxHtmlWordCell(temp, *(GetDC())); |
206 | if (m_UseLink) c -> SetLink(m_Link); | |
207 | m_Container -> InsertCell(c); | |
208 | m_tmpLastWasSpace = TRUE; | |
209 | } | |
210 | } | |
211 | if (templen) { | |
212 | temp[templen] = 0; | |
b250d384 | 213 | if (m_EncConv) m_EncConv -> Convert(temp); |
5526e819 VS |
214 | c = new wxHtmlWordCell(temp, *(GetDC())); |
215 | if (m_UseLink) c -> SetLink(m_Link); | |
216 | m_Container -> InsertCell(c); | |
217 | m_tmpLastWasSpace = FALSE; | |
218 | } | |
219 | } | |
220 | ||
221 | ||
222 | ||
223 | wxHtmlContainerCell* wxHtmlWinParser::OpenContainer() | |
224 | { | |
225 | m_Container = new wxHtmlContainerCell(m_Container); | |
226 | m_Container -> SetAlignHor(m_Align); | |
227 | m_tmpLastWasSpace = TRUE; | |
228 | /* to avoid space being first character in paragraph */ | |
229 | return m_Container; | |
230 | } | |
231 | ||
232 | ||
233 | ||
234 | wxHtmlContainerCell* wxHtmlWinParser::SetContainer(wxHtmlContainerCell *c) | |
235 | { | |
236 | m_tmpLastWasSpace = TRUE; | |
237 | /* to avoid space being first character in paragraph */ | |
238 | return m_Container = c; | |
239 | } | |
240 | ||
241 | ||
242 | ||
243 | wxHtmlContainerCell* wxHtmlWinParser::CloseContainer() | |
244 | { | |
245 | m_Container = m_Container -> GetParent(); | |
246 | return m_Container; | |
247 | } | |
248 | ||
249 | ||
f2c2fa4d VS |
250 | void wxHtmlWinParser::SetFontSize(int s) |
251 | { | |
252 | if (s < 1) s = 1; | |
253 | else if (s > 7) s = 7; | |
254 | m_FontSize = s; | |
255 | } | |
256 | ||
257 | ||
258 | ||
5526e819 VS |
259 | wxFont* wxHtmlWinParser::CreateCurrentFont() |
260 | { | |
261 | int fb = GetFontBold(), | |
262 | fi = GetFontItalic(), | |
263 | fu = GetFontUnderlined(), | |
264 | ff = GetFontFixed(), | |
f2c2fa4d | 265 | fs = GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ; |
5526e819 | 266 | |
f1ad10f3 VS |
267 | wxString face = ff ? m_FontFaceFixed : m_FontFaceNormal; |
268 | wxString *faceptr = &(m_FontsFacesTable[fb][fi][fu][ff][fs]); | |
269 | wxFont **fontptr = &(m_FontsTable[fb][fi][fu][ff][fs]); | |
b250d384 | 270 | wxFontEncoding *encptr = &(m_FontsEncTable[fb][fi][fu][ff][fs]); |
f1ad10f3 | 271 | |
b250d384 | 272 | if (*fontptr != NULL && (*faceptr != face || *encptr != m_OutputEnc)) { |
f1ad10f3 VS |
273 | delete *fontptr; |
274 | *fontptr = NULL; | |
275 | } | |
276 | ||
277 | if (*fontptr == NULL) { | |
278 | *faceptr = face; | |
b250d384 | 279 | *encptr = m_OutputEnc; |
f1ad10f3 VS |
280 | *fontptr = new wxFont( |
281 | m_FontsSizes[fs] * m_PixelScale, | |
282 | ff ? wxMODERN : wxSWISS, | |
283 | fi ? wxITALIC : wxNORMAL, | |
284 | fb ? wxBOLD : wxNORMAL, | |
b250d384 VS |
285 | fu ? TRUE : FALSE, face, |
286 | m_OutputEnc); | |
5526e819 | 287 | } |
f1ad10f3 VS |
288 | m_DC -> SetFont(**fontptr); |
289 | return (*fontptr); | |
5526e819 VS |
290 | } |
291 | ||
292 | ||
293 | ||
f2c2fa4d VS |
294 | void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo& link) |
295 | { | |
296 | m_Link = link; | |
297 | m_UseLink = (link.GetHref() != wxEmptyString); | |
298 | } | |
299 | ||
300 | ||
b250d384 VS |
301 | void wxHtmlWinParser::SetFontFace(const wxString& face) |
302 | { | |
303 | if (GetFontFixed()) m_FontFaceFixed = face; | |
304 | else m_FontFaceNormal = face; | |
305 | ||
306 | if (m_InputEnc != wxFONTENCODING_DEFAULT) | |
307 | SetInputEncoding(m_InputEnc); | |
308 | } | |
309 | ||
310 | ||
311 | ||
312 | void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc) | |
313 | { | |
314 | m_InputEnc = m_OutputEnc = wxFONTENCODING_DEFAULT; | |
315 | if (m_EncConv) {delete m_EncConv; m_EncConv = NULL;} | |
316 | ||
317 | if (enc == wxFONTENCODING_DEFAULT) return; | |
318 | ||
319 | wxFontEncoding altfix, altnorm; | |
320 | bool availfix, availnorm; | |
321 | ||
322 | // exact match? | |
323 | availnorm = wxTheFontMapper -> IsEncodingAvailable(enc, m_FontFaceNormal); | |
324 | availfix = wxTheFontMapper -> IsEncodingAvailable(enc, m_FontFaceFixed); | |
325 | if (availnorm && availfix) | |
326 | m_OutputEnc = enc; | |
327 | ||
328 | // alternatives? | |
329 | else if (wxTheFontMapper -> GetAltForEncoding(enc, &altnorm, m_FontFaceNormal, FALSE) && | |
330 | wxTheFontMapper -> GetAltForEncoding(enc, &altfix, m_FontFaceFixed, FALSE) && | |
331 | altnorm == altfix) | |
332 | m_OutputEnc = altnorm; | |
333 | ||
334 | // at least normal face? | |
335 | else if (availnorm) | |
336 | m_OutputEnc = enc; | |
337 | else if (wxTheFontMapper -> GetAltForEncoding(enc, &altnorm, m_FontFaceNormal, FALSE)) | |
338 | m_OutputEnc = altnorm; | |
339 | ||
340 | // okay, let convert to ISO_8859-1, available always | |
341 | else | |
342 | m_OutputEnc = wxFONTENCODING_DEFAULT; | |
343 | ||
344 | m_InputEnc = enc; | |
345 | ||
346 | if (m_InputEnc == m_OutputEnc) return; | |
347 | ||
348 | m_EncConv = new wxEncodingConverter(); | |
349 | if (!m_EncConv -> Init(m_InputEnc, | |
350 | (m_OutputEnc == wxFONTENCODING_DEFAULT) ? | |
351 | wxFONTENCODING_ISO8859_1 : m_OutputEnc, | |
352 | wxCONVERT_SUBSTITUTE)) | |
353 | { // total failture :-( | |
354 | m_InputEnc = m_OutputEnc = wxFONTENCODING_DEFAULT; | |
355 | delete m_EncConv; | |
356 | m_EncConv = NULL; | |
357 | } | |
358 | } | |
359 | ||
360 | ||
361 | ||
f2c2fa4d | 362 | |
5526e819 VS |
363 | |
364 | //----------------------------------------------------------------------------- | |
365 | // wxHtmlWinTagHandler | |
366 | //----------------------------------------------------------------------------- | |
367 | ||
368 | IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler, wxHtmlTagHandler) | |
369 | ||
370 | ||
371 | ||
372 | //----------------------------------------------------------------------------- | |
373 | // wxHtmlTagsModule | |
374 | //----------------------------------------------------------------------------- | |
375 | ||
376 | ||
377 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule, wxModule) | |
378 | ||
379 | ||
380 | bool wxHtmlTagsModule::OnInit() | |
381 | { | |
382 | wxHtmlWinParser::AddModule(this); | |
383 | return TRUE; | |
384 | } | |
385 | ||
386 | ||
387 | ||
388 | void wxHtmlTagsModule::OnExit() | |
389 | { | |
390 | } | |
223d09f6 | 391 | #endif |
5526e819 | 392 |