]>
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};
65 static int default_sizes
[7] = {10, 12, 14, 16, 19, 24, 32};
67 SetFonts("", "", default_sizes
);
70 // fill in wxHtmlParser's tables:
71 wxNode
*node
= m_Modules
.GetFirst();
73 wxHtmlTagsModule
*mod
= (wxHtmlTagsModule
*) node
-> GetData();
74 mod
-> FillHandlersTable(this);
75 node
= node
-> GetNext();
80 wxHtmlWinParser::~wxHtmlWinParser()
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
];
92 if (m_EncConv
) delete m_EncConv
;
96 void wxHtmlWinParser::AddModule(wxHtmlTagsModule
*module)
98 m_Modules
.Append(module);
103 void wxHtmlWinParser::SetFonts(wxString normal_face
, wxString fixed_face
, const int *sizes
)
107 for (i
= 0; i
< 7; i
++) m_FontsSizes
[i
] = sizes
[i
];
108 m_FontFaceFixed
= fixed_face
;
109 m_FontFaceNormal
= normal_face
;
111 SetInputEncoding(m_InputEnc
);
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
;
127 void wxHtmlWinParser::InitParser(const wxString
& source
)
129 wxHtmlParser::InitParser(source
);
130 wxASSERT_MSG(m_DC
!= NULL
, _("no DC assigned to wxHtmlWinParser!!"));
132 m_FontBold
= m_FontItalic
= m_FontUnderlined
= m_FontFixed
= FALSE
;
133 m_FontSize
= 3; //default one
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
141 m_Link
= wxHtmlLinkInfo("", "");
142 m_LinkColor
.Set(0, 0, 0xFF);
143 m_ActualColor
.Set(0, 0, 0);
144 m_Align
= wxHTML_ALIGN_LEFT
;
145 m_tmpLastWasSpace
= FALSE
;
150 m_Container
-> InsertCell(new wxHtmlColourCell(m_ActualColor
));
151 m_Container
-> InsertCell(new wxHtmlFontCell(CreateCurrentFont()));
156 void wxHtmlWinParser::DoneParser()
159 SetInputEncoding(wxFONTENCODING_DEFAULT
); // for next call
160 wxHtmlParser::DoneParser();
165 wxObject
* wxHtmlWinParser::GetProduct()
167 wxHtmlContainerCell
*top
;
173 while (top
-> GetParent()) top
= top
-> GetParent();
179 void wxHtmlWinParser::AddText(const char* txt
)
182 int i
= 0, x
, lng
= strlen(txt
);
183 char temp
[wxHTML_BUFLEN
];
187 if (m_tmpLastWasSpace
) {
188 while ((i
< lng
) && ((txt
[i
] == '\n') || (txt
[i
] == '\r') || (txt
[i
] == ' ') || (txt
[i
] == '\t'))) i
++;
193 d
= temp
[templen
++] = txt
[i
];
194 if ((d
== '\n') || (d
== '\r') || (d
== ' ') || (d
== '\t')) {
196 while ((i
< lng
) && ((txt
[i
] == '\n') || (txt
[i
] == '\r') || (txt
[i
] == ' ') || (txt
[i
] == '\t'))) i
++, x
++;
201 temp
[templen
-1] = ' ';
204 if (m_EncConv
) m_EncConv
-> Convert(temp
);
205 c
= new wxHtmlWordCell(temp
, *(GetDC()));
206 if (m_UseLink
) c
-> SetLink(m_Link
);
207 m_Container
-> InsertCell(c
);
208 m_tmpLastWasSpace
= TRUE
;
213 if (m_EncConv
) m_EncConv
-> Convert(temp
);
214 c
= new wxHtmlWordCell(temp
, *(GetDC()));
215 if (m_UseLink
) c
-> SetLink(m_Link
);
216 m_Container
-> InsertCell(c
);
217 m_tmpLastWasSpace
= FALSE
;
223 wxHtmlContainerCell
* wxHtmlWinParser::OpenContainer()
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 */
234 wxHtmlContainerCell
* wxHtmlWinParser::SetContainer(wxHtmlContainerCell
*c
)
236 m_tmpLastWasSpace
= TRUE
;
237 /* to avoid space being first character in paragraph */
238 return m_Container
= c
;
243 wxHtmlContainerCell
* wxHtmlWinParser::CloseContainer()
245 m_Container
= m_Container
-> GetParent();
250 void wxHtmlWinParser::SetFontSize(int s
)
253 else if (s
> 7) s
= 7;
259 wxFont
* wxHtmlWinParser::CreateCurrentFont()
261 int fb
= GetFontBold(),
262 fi
= GetFontItalic(),
263 fu
= GetFontUnderlined(),
265 fs
= GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ;
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
]);
270 wxFontEncoding
*encptr
= &(m_FontsEncTable
[fb
][fi
][fu
][ff
][fs
]);
272 if (*fontptr
!= NULL
&& (*faceptr
!= face
|| *encptr
!= m_OutputEnc
)) {
277 if (*fontptr
== NULL
) {
279 *encptr
= m_OutputEnc
;
280 *fontptr
= new wxFont(
281 m_FontsSizes
[fs
] * m_PixelScale
,
282 ff
? wxMODERN
: wxSWISS
,
283 fi
? wxITALIC
: wxNORMAL
,
284 fb
? wxBOLD
: wxNORMAL
,
285 fu
? TRUE
: FALSE
, face
,
288 m_DC
-> SetFont(**fontptr
);
294 void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo
& link
)
297 m_UseLink
= (link
.GetHref() != wxEmptyString
);
301 void wxHtmlWinParser::SetFontFace(const wxString
& face
)
303 if (GetFontFixed()) m_FontFaceFixed
= face
;
304 else m_FontFaceNormal
= face
;
306 if (m_InputEnc
!= wxFONTENCODING_DEFAULT
)
307 SetInputEncoding(m_InputEnc
);
312 void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc
)
314 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
315 if (m_EncConv
) {delete m_EncConv
; m_EncConv
= NULL
;}
317 if (enc
== wxFONTENCODING_DEFAULT
) return;
319 wxFontEncoding altfix
, altnorm
;
320 bool availfix
, availnorm
;
323 availnorm
= wxTheFontMapper
-> IsEncodingAvailable(enc
, m_FontFaceNormal
);
324 availfix
= wxTheFontMapper
-> IsEncodingAvailable(enc
, m_FontFaceFixed
);
325 if (availnorm
&& availfix
)
329 else if (wxTheFontMapper
-> GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, FALSE
) &&
330 wxTheFontMapper
-> GetAltForEncoding(enc
, &altfix
, m_FontFaceFixed
, FALSE
) &&
332 m_OutputEnc
= altnorm
;
334 // at least normal face?
337 else if (wxTheFontMapper
-> GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, FALSE
))
338 m_OutputEnc
= altnorm
;
340 // okay, let convert to ISO_8859-1, available always
342 m_OutputEnc
= wxFONTENCODING_DEFAULT
;
346 if (m_InputEnc
== m_OutputEnc
) return;
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
;
364 //-----------------------------------------------------------------------------
365 // wxHtmlWinTagHandler
366 //-----------------------------------------------------------------------------
368 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler
, wxHtmlTagHandler
)
372 //-----------------------------------------------------------------------------
374 //-----------------------------------------------------------------------------
377 IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule
, wxModule
)
380 bool wxHtmlTagsModule::OnInit()
382 wxHtmlWinParser::AddModule(this);
388 void wxHtmlTagsModule::OnExit()