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