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