]>
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"
28 #include "wx/html/htmldefs.h"
29 #include "wx/html/winpars.h"
30 #include "wx/html/htmlwin.h"
31 #include "wx/fontmap.h"
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
39 wxList
wxHtmlWinParser::m_Modules
;
41 wxHtmlWinParser::wxHtmlWinParser(wxWindow
*wnd
) : wxHtmlParser()
46 m_CharHeight
= m_CharWidth
= 0;
49 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
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
++)
57 for (m
= 0; m
< 7; m
++) {
58 m_FontsTable
[i
][j
][k
][l
][m
] = NULL
;
59 m_FontsFacesTable
[i
][j
][k
][l
][m
] = wxEmptyString
;
60 m_FontsEncTable
[i
][j
][k
][l
][m
] = wxFONTENCODING_DEFAULT
;
63 static int default_sizes
[7] = {7, 8, 10, 12, 16, 22, 30};
64 #elif defined(__WXMAC__)
65 static int default_sizes
[7] = {9, 12, 14, 18, 24, 30, 36};
67 static int default_sizes
[7] = {10, 12, 14, 16, 19, 24, 32};
69 SetFonts("", "", default_sizes
);
72 // fill in wxHtmlParser's tables:
73 wxNode
*node
= m_Modules
.GetFirst();
75 wxHtmlTagsModule
*mod
= (wxHtmlTagsModule
*) node
-> GetData();
76 mod
-> FillHandlersTable(this);
77 node
= node
-> GetNext();
82 wxHtmlWinParser::~wxHtmlWinParser()
86 for (i
= 0; i
< 2; i
++)
87 for (j
= 0; j
< 2; j
++)
88 for (k
= 0; k
< 2; k
++)
89 for (l
= 0; l
< 2; l
++)
90 for (m
= 0; m
< 7; m
++) {
91 if (m_FontsTable
[i
][j
][k
][l
][m
] != NULL
)
92 delete m_FontsTable
[i
][j
][k
][l
][m
];
94 if (m_EncConv
) delete m_EncConv
;
98 void wxHtmlWinParser::AddModule(wxHtmlTagsModule
*module)
100 m_Modules
.Append(module);
105 void wxHtmlWinParser::SetFonts(wxString normal_face
, wxString fixed_face
, const int *sizes
)
109 for (i
= 0; i
< 7; i
++) m_FontsSizes
[i
] = sizes
[i
];
110 m_FontFaceFixed
= fixed_face
;
111 m_FontFaceNormal
= normal_face
;
113 SetInputEncoding(m_InputEnc
);
115 for (i
= 0; i
< 2; i
++)
116 for (j
= 0; j
< 2; j
++)
117 for (k
= 0; k
< 2; k
++)
118 for (l
= 0; l
< 2; l
++)
119 for (m
= 0; m
< 7; m
++) {
120 if (m_FontsTable
[i
][j
][k
][l
][m
] != NULL
) {
121 delete m_FontsTable
[i
][j
][k
][l
][m
];
122 m_FontsTable
[i
][j
][k
][l
][m
] = NULL
;
129 void wxHtmlWinParser::InitParser(const wxString
& source
)
131 wxHtmlParser::InitParser(source
);
132 wxASSERT_MSG(m_DC
!= NULL
, _("no DC assigned to wxHtmlWinParser!!"));
134 m_FontBold
= m_FontItalic
= m_FontUnderlined
= m_FontFixed
= FALSE
;
135 m_FontSize
= 3; //default one
136 CreateCurrentFont(); // we're selecting default font into
137 m_DC
-> GetTextExtent("H", &m_CharWidth
, &m_CharHeight
);
138 /* NOTE : we're not using GetCharWidth/Height() because
139 of differences under X and win
143 m_Link
= wxHtmlLinkInfo("", "");
144 m_LinkColor
.Set(0, 0, 0xFF);
145 m_ActualColor
.Set(0, 0, 0);
146 m_Align
= wxHTML_ALIGN_LEFT
;
147 m_tmpLastWasSpace
= FALSE
;
152 m_Container
-> InsertCell(new wxHtmlColourCell(m_ActualColor
));
153 m_Container
-> InsertCell(new wxHtmlFontCell(CreateCurrentFont()));
158 void wxHtmlWinParser::DoneParser()
161 SetInputEncoding(wxFONTENCODING_DEFAULT
); // for next call
162 wxHtmlParser::DoneParser();
167 wxObject
* wxHtmlWinParser::GetProduct()
169 wxHtmlContainerCell
*top
;
175 while (top
-> GetParent()) top
= top
-> GetParent();
181 void wxHtmlWinParser::AddText(const char* txt
)
184 int i
= 0, x
, lng
= strlen(txt
);
185 char temp
[wxHTML_BUFLEN
];
189 if (m_tmpLastWasSpace
) {
190 while ((i
< lng
) && ((txt
[i
] == '\n') || (txt
[i
] == '\r') || (txt
[i
] == ' ') || (txt
[i
] == '\t'))) i
++;
195 d
= temp
[templen
++] = txt
[i
];
196 if ((d
== '\n') || (d
== '\r') || (d
== ' ') || (d
== '\t')) {
198 while ((i
< lng
) && ((txt
[i
] == '\n') || (txt
[i
] == '\r') || (txt
[i
] == ' ') || (txt
[i
] == '\t'))) i
++, x
++;
203 temp
[templen
-1] = ' ';
206 if (m_EncConv
) m_EncConv
-> Convert(temp
);
207 c
= new wxHtmlWordCell(temp
, *(GetDC()));
208 if (m_UseLink
) c
-> SetLink(m_Link
);
209 m_Container
-> InsertCell(c
);
210 m_tmpLastWasSpace
= TRUE
;
215 if (m_EncConv
) m_EncConv
-> Convert(temp
);
216 c
= new wxHtmlWordCell(temp
, *(GetDC()));
217 if (m_UseLink
) c
-> SetLink(m_Link
);
218 m_Container
-> InsertCell(c
);
219 m_tmpLastWasSpace
= FALSE
;
225 wxHtmlContainerCell
* wxHtmlWinParser::OpenContainer()
227 m_Container
= new wxHtmlContainerCell(m_Container
);
228 m_Container
-> SetAlignHor(m_Align
);
229 m_tmpLastWasSpace
= TRUE
;
230 /* to avoid space being first character in paragraph */
236 wxHtmlContainerCell
* wxHtmlWinParser::SetContainer(wxHtmlContainerCell
*c
)
238 m_tmpLastWasSpace
= TRUE
;
239 /* to avoid space being first character in paragraph */
240 return m_Container
= c
;
245 wxHtmlContainerCell
* wxHtmlWinParser::CloseContainer()
247 m_Container
= m_Container
-> GetParent();
252 void wxHtmlWinParser::SetFontSize(int s
)
255 else if (s
> 7) s
= 7;
261 wxFont
* wxHtmlWinParser::CreateCurrentFont()
263 int fb
= GetFontBold(),
264 fi
= GetFontItalic(),
265 fu
= GetFontUnderlined(),
267 fs
= GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ;
269 wxString face
= ff
? m_FontFaceFixed
: m_FontFaceNormal
;
270 wxString
*faceptr
= &(m_FontsFacesTable
[fb
][fi
][fu
][ff
][fs
]);
271 wxFont
**fontptr
= &(m_FontsTable
[fb
][fi
][fu
][ff
][fs
]);
272 wxFontEncoding
*encptr
= &(m_FontsEncTable
[fb
][fi
][fu
][ff
][fs
]);
274 if (*fontptr
!= NULL
&& (*faceptr
!= face
|| *encptr
!= m_OutputEnc
)) {
279 if (*fontptr
== NULL
) {
281 *encptr
= m_OutputEnc
;
282 *fontptr
= new wxFont(
283 m_FontsSizes
[fs
] * m_PixelScale
,
284 ff
? wxMODERN
: wxSWISS
,
285 fi
? wxITALIC
: wxNORMAL
,
286 fb
? wxBOLD
: wxNORMAL
,
287 fu
? TRUE
: FALSE
, face
,
290 m_DC
-> SetFont(**fontptr
);
296 void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo
& link
)
299 m_UseLink
= (link
.GetHref() != wxEmptyString
);
303 void wxHtmlWinParser::SetFontFace(const wxString
& face
)
305 if (GetFontFixed()) m_FontFaceFixed
= face
;
306 else m_FontFaceNormal
= face
;
308 if (m_InputEnc
!= wxFONTENCODING_DEFAULT
)
309 SetInputEncoding(m_InputEnc
);
314 void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc
)
316 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
317 if (m_EncConv
) {delete m_EncConv
; m_EncConv
= NULL
;}
319 if (enc
== wxFONTENCODING_DEFAULT
) return;
321 wxFontEncoding altfix
, altnorm
;
322 bool availfix
, availnorm
;
325 availnorm
= wxTheFontMapper
-> IsEncodingAvailable(enc
, m_FontFaceNormal
);
326 availfix
= wxTheFontMapper
-> IsEncodingAvailable(enc
, m_FontFaceFixed
);
327 if (availnorm
&& availfix
)
331 else if (wxTheFontMapper
-> GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, FALSE
) &&
332 wxTheFontMapper
-> GetAltForEncoding(enc
, &altfix
, m_FontFaceFixed
, FALSE
) &&
334 m_OutputEnc
= altnorm
;
336 // at least normal face?
339 else if (wxTheFontMapper
-> GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, FALSE
))
340 m_OutputEnc
= altnorm
;
342 // okay, let convert to ISO_8859-1, available always
344 m_OutputEnc
= wxFONTENCODING_DEFAULT
;
348 if (m_InputEnc
== m_OutputEnc
) return;
350 m_EncConv
= new wxEncodingConverter();
351 if (!m_EncConv
-> Init(m_InputEnc
,
352 (m_OutputEnc
== wxFONTENCODING_DEFAULT
) ?
353 wxFONTENCODING_ISO8859_1
: m_OutputEnc
,
354 wxCONVERT_SUBSTITUTE
))
355 { // total failture :-(
356 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
366 //-----------------------------------------------------------------------------
367 // wxHtmlWinTagHandler
368 //-----------------------------------------------------------------------------
370 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler
, wxHtmlTagHandler
)
374 //-----------------------------------------------------------------------------
376 //-----------------------------------------------------------------------------
379 IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule
, wxModule
)
382 bool wxHtmlTagsModule::OnInit()
384 wxHtmlWinParser::AddModule(this);
390 void wxHtmlTagsModule::OnExit()