]>
git.saurik.com Git - wxWidgets.git/blob - src/html/winpars.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtmlParser class (generic parser)
4 // Author: Vaclav Slavik
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation
15 #include "wx/wxprec.h"
18 #if wxUSE_HTML && wxUSE_STREAMS
28 #include "wx/html/htmldefs.h"
29 #include "wx/html/winpars.h"
30 #include "wx/html/htmlwin.h"
31 #include "wx/fontmap.h"
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
40 wxList
wxHtmlWinParser::m_Modules
;
42 wxHtmlWinParser::wxHtmlWinParser(wxWindow
*wnd
) : wxHtmlParser()
47 m_CharHeight
= m_CharWidth
= 0;
50 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
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
++)
58 for (m
= 0; m
< 7; m
++) {
59 m_FontsTable
[i
][j
][k
][l
][m
] = NULL
;
60 m_FontsFacesTable
[i
][j
][k
][l
][m
] = wxEmptyString
;
61 m_FontsEncTable
[i
][j
][k
][l
][m
] = wxFONTENCODING_DEFAULT
;
64 static int default_sizes
[7] = {7, 8, 10, 12, 16, 22, 30};
65 #elif defined(__WXMAC__)
66 static int default_sizes
[7] = {9, 12, 14, 18, 24, 30, 36};
68 static int default_sizes
[7] = {10, 12, 14, 16, 19, 24, 32};
70 SetFonts("", "", default_sizes
);
73 // fill in wxHtmlParser's tables:
74 wxNode
*node
= m_Modules
.GetFirst();
76 wxHtmlTagsModule
*mod
= (wxHtmlTagsModule
*) node
-> GetData();
77 mod
-> FillHandlersTable(this);
78 node
= node
-> GetNext();
83 wxHtmlWinParser::~wxHtmlWinParser()
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
];
95 if (m_EncConv
) delete m_EncConv
;
99 void wxHtmlWinParser::AddModule(wxHtmlTagsModule
*module)
101 m_Modules
.Append(module);
106 void wxHtmlWinParser::RemoveModule(wxHtmlTagsModule
*module)
108 m_Modules
.DeleteObject(module);
113 void wxHtmlWinParser::SetFonts(wxString normal_face
, wxString fixed_face
, const int *sizes
)
117 for (i
= 0; i
< 7; i
++) m_FontsSizes
[i
] = sizes
[i
];
118 m_FontFaceFixed
= fixed_face
;
119 m_FontFaceNormal
= normal_face
;
121 SetInputEncoding(m_InputEnc
);
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
;
137 void wxHtmlWinParser::InitParser(const wxString
& source
)
139 wxHtmlParser::InitParser(source
);
140 wxASSERT_MSG(m_DC
!= NULL
, _T("no DC assigned to wxHtmlWinParser!!"));
142 m_FontBold
= m_FontItalic
= m_FontUnderlined
= m_FontFixed
= FALSE
;
143 m_FontSize
= 3; //default one
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
147 of differences under X and win
151 m_Link
= wxHtmlLinkInfo("", "");
152 m_LinkColor
.Set(0, 0, 0xFF);
153 m_ActualColor
.Set(0, 0, 0);
154 m_Align
= wxHTML_ALIGN_LEFT
;
155 m_tmpLastWasSpace
= FALSE
;
160 m_Container
-> InsertCell(new wxHtmlColourCell(m_ActualColor
));
161 m_Container
-> InsertCell(new wxHtmlFontCell(CreateCurrentFont()));
166 void wxHtmlWinParser::DoneParser()
169 SetInputEncoding(wxFONTENCODING_DEFAULT
); // for next call
170 wxHtmlParser::DoneParser();
175 wxObject
* wxHtmlWinParser::GetProduct()
177 wxHtmlContainerCell
*top
;
183 while (top
-> GetParent()) top
= top
-> GetParent();
189 void wxHtmlWinParser::AddText(const char* txt
)
192 int i
= 0, x
, lng
= strlen(txt
);
193 char temp
[wxHTML_BUFLEN
];
197 if (m_tmpLastWasSpace
) {
198 while ((i
< lng
) && ((txt
[i
] == '\n') || (txt
[i
] == '\r') || (txt
[i
] == ' ') || (txt
[i
] == '\t'))) i
++;
203 d
= temp
[templen
++] = txt
[i
];
204 if ((d
== '\n') || (d
== '\r') || (d
== ' ') || (d
== '\t')) {
206 while ((i
< lng
) && ((txt
[i
] == '\n') || (txt
[i
] == '\r') || (txt
[i
] == ' ') || (txt
[i
] == '\t'))) i
++, x
++;
211 temp
[templen
-1] = ' ';
214 if (m_EncConv
) m_EncConv
-> Convert(temp
);
215 c
= new wxHtmlWordCell(temp
, *(GetDC()));
216 if (m_UseLink
) c
-> SetLink(m_Link
);
217 m_Container
-> InsertCell(c
);
218 m_tmpLastWasSpace
= TRUE
;
223 if (m_EncConv
) m_EncConv
-> Convert(temp
);
224 c
= new wxHtmlWordCell(temp
, *(GetDC()));
225 if (m_UseLink
) c
-> SetLink(m_Link
);
226 m_Container
-> InsertCell(c
);
227 m_tmpLastWasSpace
= FALSE
;
233 wxHtmlContainerCell
* wxHtmlWinParser::OpenContainer()
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 */
244 wxHtmlContainerCell
* wxHtmlWinParser::SetContainer(wxHtmlContainerCell
*c
)
246 m_tmpLastWasSpace
= TRUE
;
247 /* to avoid space being first character in paragraph */
248 return m_Container
= c
;
253 wxHtmlContainerCell
* wxHtmlWinParser::CloseContainer()
255 m_Container
= m_Container
-> GetParent();
260 void wxHtmlWinParser::SetFontSize(int s
)
263 else if (s
> 7) s
= 7;
269 wxFont
* wxHtmlWinParser::CreateCurrentFont()
271 int fb
= GetFontBold(),
272 fi
= GetFontItalic(),
273 fu
= GetFontUnderlined(),
275 fs
= GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ;
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
]);
280 wxFontEncoding
*encptr
= &(m_FontsEncTable
[fb
][fi
][fu
][ff
][fs
]);
282 if (*fontptr
!= NULL
&& (*faceptr
!= face
|| *encptr
!= m_OutputEnc
)) {
287 if (*fontptr
== NULL
) {
289 *encptr
= m_OutputEnc
;
290 *fontptr
= new wxFont(
291 m_FontsSizes
[fs
] * m_PixelScale
,
292 ff
? wxMODERN
: wxSWISS
,
293 fi
? wxITALIC
: wxNORMAL
,
294 fb
? wxBOLD
: wxNORMAL
,
295 fu
? TRUE
: FALSE
, face
,
298 m_DC
-> SetFont(**fontptr
);
304 void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo
& link
)
307 m_UseLink
= (link
.GetHref() != wxEmptyString
);
311 void wxHtmlWinParser::SetFontFace(const wxString
& face
)
313 if (GetFontFixed()) m_FontFaceFixed
= face
;
314 else m_FontFaceNormal
= face
;
316 if (m_InputEnc
!= wxFONTENCODING_DEFAULT
)
317 SetInputEncoding(m_InputEnc
);
322 void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc
)
324 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
325 if (m_EncConv
) {delete m_EncConv
; m_EncConv
= NULL
;}
327 if (enc
== wxFONTENCODING_DEFAULT
) return;
329 wxFontEncoding altfix
, altnorm
;
330 bool availfix
, availnorm
;
333 availnorm
= wxTheFontMapper
-> IsEncodingAvailable(enc
, m_FontFaceNormal
);
334 availfix
= wxTheFontMapper
-> IsEncodingAvailable(enc
, m_FontFaceFixed
);
335 if (availnorm
&& availfix
)
339 else if (wxTheFontMapper
-> GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, FALSE
) &&
340 wxTheFontMapper
-> GetAltForEncoding(enc
, &altfix
, m_FontFaceFixed
, FALSE
) &&
342 m_OutputEnc
= altnorm
;
344 // at least normal face?
347 else if (wxTheFontMapper
-> GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, FALSE
))
348 m_OutputEnc
= altnorm
;
350 // okay, let convert to ISO_8859-1, available always
352 m_OutputEnc
= wxFONTENCODING_DEFAULT
;
356 if (m_InputEnc
== m_OutputEnc
) return;
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 :-(
364 wxLogError(_("Failed to display HTML document in %s encoding"),
365 wxFontMapper::GetEncodingName(enc
).c_str());
366 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
376 //-----------------------------------------------------------------------------
377 // wxHtmlWinTagHandler
378 //-----------------------------------------------------------------------------
380 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler
, wxHtmlTagHandler
)
384 //-----------------------------------------------------------------------------
386 //-----------------------------------------------------------------------------
389 IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule
, wxModule
)
392 bool wxHtmlTagsModule::OnInit()
394 wxHtmlWinParser::AddModule(this);
400 void wxHtmlTagsModule::OnExit()
402 wxHtmlWinParser::RemoveModule(this);