]>
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
29 #include "wx/html/htmldefs.h"
30 #include "wx/html/winpars.h"
31 #include "wx/html/htmlwin.h"
32 #include "wx/fontmap.h"
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
41 wxList
wxHtmlWinParser::m_Modules
;
43 wxHtmlWinParser::wxHtmlWinParser(wxWindow
*wnd
) : wxHtmlParser()
48 m_CharHeight
= m_CharWidth
= 0;
51 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
55 for (i
= 0; i
< 2; i
++)
56 for (j
= 0; j
< 2; j
++)
57 for (k
= 0; k
< 2; k
++)
58 for (l
= 0; l
< 2; l
++)
59 for (m
= 0; m
< 7; m
++)
61 m_FontsTable
[i
][j
][k
][l
][m
] = NULL
;
62 m_FontsFacesTable
[i
][j
][k
][l
][m
] = wxEmptyString
;
63 m_FontsEncTable
[i
][j
][k
][l
][m
] = wxFONTENCODING_DEFAULT
;
66 static int default_sizes
[7] = {7, 8, 10, 12, 16, 22, 30};
67 #elif defined(__WXMAC__)
68 static int default_sizes
[7] = {9, 12, 14, 18, 24, 30, 36};
70 static int default_sizes
[7] = {10, 12, 14, 16, 19, 24, 32};
72 SetFonts("", "", default_sizes
);
75 // fill in wxHtmlParser's tables:
76 wxNode
*node
= m_Modules
.GetFirst();
79 wxHtmlTagsModule
*mod
= (wxHtmlTagsModule
*) node
->GetData();
80 mod
->FillHandlersTable(this);
81 node
= node
->GetNext();
86 wxHtmlWinParser::~wxHtmlWinParser()
90 for (i
= 0; i
< 2; i
++)
91 for (j
= 0; j
< 2; j
++)
92 for (k
= 0; k
< 2; k
++)
93 for (l
= 0; l
< 2; l
++)
94 for (m
= 0; m
< 7; m
++)
96 if (m_FontsTable
[i
][j
][k
][l
][m
] != NULL
)
97 delete m_FontsTable
[i
][j
][k
][l
][m
];
99 if (m_EncConv
) delete m_EncConv
;
103 void wxHtmlWinParser::AddModule(wxHtmlTagsModule
*module)
105 m_Modules
.Append(module);
110 void wxHtmlWinParser::RemoveModule(wxHtmlTagsModule
*module)
112 m_Modules
.DeleteObject(module);
117 void wxHtmlWinParser::SetFonts(wxString normal_face
, wxString fixed_face
, const int *sizes
)
121 for (i
= 0; i
< 7; i
++) m_FontsSizes
[i
] = sizes
[i
];
122 m_FontFaceFixed
= fixed_face
;
123 m_FontFaceNormal
= normal_face
;
125 SetInputEncoding(m_InputEnc
);
127 for (i
= 0; i
< 2; i
++)
128 for (j
= 0; j
< 2; j
++)
129 for (k
= 0; k
< 2; k
++)
130 for (l
= 0; l
< 2; l
++)
131 for (m
= 0; m
< 7; m
++) {
132 if (m_FontsTable
[i
][j
][k
][l
][m
] != NULL
)
134 delete m_FontsTable
[i
][j
][k
][l
][m
];
135 m_FontsTable
[i
][j
][k
][l
][m
] = NULL
;
142 void wxHtmlWinParser::InitParser(const wxString
& source
)
144 wxHtmlParser::InitParser(source
);
145 wxASSERT_MSG(m_DC
!= NULL
, _T("no DC assigned to wxHtmlWinParser!!"));
147 m_FontBold
= m_FontItalic
= m_FontUnderlined
= m_FontFixed
= FALSE
;
148 m_FontSize
= 3; //default one
149 CreateCurrentFont(); // we're selecting default font into
150 m_DC
->GetTextExtent("H", &m_CharWidth
, &m_CharHeight
);
151 /* NOTE : we're not using GetCharWidth/Height() because
152 of differences under X and win
156 m_Link
= wxHtmlLinkInfo("", "");
157 m_LinkColor
.Set(0, 0, 0xFF);
158 m_ActualColor
.Set(0, 0, 0);
159 m_Align
= wxHTML_ALIGN_LEFT
;
160 m_tmpLastWasSpace
= FALSE
;
165 m_Container
->InsertCell(new wxHtmlColourCell(m_ActualColor
));
166 m_Container
->InsertCell(new wxHtmlFontCell(CreateCurrentFont()));
171 void wxHtmlWinParser::DoneParser()
174 SetInputEncoding(wxFONTENCODING_DEFAULT
); // for next call
175 wxHtmlParser::DoneParser();
180 wxObject
* wxHtmlWinParser::GetProduct()
182 wxHtmlContainerCell
*top
;
188 while (top
->GetParent()) top
= top
->GetParent();
194 void wxHtmlWinParser::AddText(const char* txt
)
197 int i
= 0, x
, lng
= strlen(txt
);
198 char temp
[wxHTML_BUFLEN
];
202 if (m_tmpLastWasSpace
)
205 ((txt
[i
] == '\n') || (txt
[i
] == '\r') || (txt
[i
] == ' ') ||
206 (txt
[i
] == '\t'))) i
++;
212 d
= temp
[templen
++] = txt
[i
];
213 if ((d
== '\n') || (d
== '\r') || (d
== ' ') || (d
== '\t'))
216 while ((i
< lng
) && ((txt
[i
] == '\n') || (txt
[i
] == '\r') ||
217 (txt
[i
] == ' ') || (txt
[i
] == '\t'))) i
++, x
++;
223 temp
[templen
-1] = ' ';
227 m_EncConv
->Convert(temp
);
228 c
= new wxHtmlWordCell(GetEntitiesParser()->Parse(temp
), *(GetDC()));
231 m_Container
->InsertCell(c
);
232 m_tmpLastWasSpace
= TRUE
;
239 m_EncConv
->Convert(temp
);
240 c
= new wxHtmlWordCell(GetEntitiesParser()->Parse(temp
), *(GetDC()));
243 m_Container
->InsertCell(c
);
244 m_tmpLastWasSpace
= FALSE
;
250 wxHtmlContainerCell
* wxHtmlWinParser::OpenContainer()
252 m_Container
= new wxHtmlContainerCell(m_Container
);
253 m_Container
->SetAlignHor(m_Align
);
254 m_tmpLastWasSpace
= TRUE
;
255 /* to avoid space being first character in paragraph */
261 wxHtmlContainerCell
* wxHtmlWinParser::SetContainer(wxHtmlContainerCell
*c
)
263 m_tmpLastWasSpace
= TRUE
;
264 /* to avoid space being first character in paragraph */
265 return m_Container
= c
;
270 wxHtmlContainerCell
* wxHtmlWinParser::CloseContainer()
272 m_Container
= m_Container
->GetParent();
277 void wxHtmlWinParser::SetFontSize(int s
)
280 else if (s
> 7) s
= 7;
286 wxFont
* wxHtmlWinParser::CreateCurrentFont()
288 int fb
= GetFontBold(),
289 fi
= GetFontItalic(),
290 fu
= GetFontUnderlined(),
292 fs
= GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ;
294 wxString face
= ff
? m_FontFaceFixed
: m_FontFaceNormal
;
295 wxString
*faceptr
= &(m_FontsFacesTable
[fb
][fi
][fu
][ff
][fs
]);
296 wxFont
**fontptr
= &(m_FontsTable
[fb
][fi
][fu
][ff
][fs
]);
297 wxFontEncoding
*encptr
= &(m_FontsEncTable
[fb
][fi
][fu
][ff
][fs
]);
299 if (*fontptr
!= NULL
&& (*faceptr
!= face
|| *encptr
!= m_OutputEnc
))
305 if (*fontptr
== NULL
)
308 *encptr
= m_OutputEnc
;
309 *fontptr
= new wxFont(
310 (int) (m_FontsSizes
[fs
] * m_PixelScale
),
311 ff
? wxMODERN
: wxSWISS
,
312 fi
? wxITALIC
: wxNORMAL
,
313 fb
? wxBOLD
: wxNORMAL
,
314 fu
? TRUE
: FALSE
, face
,
317 m_DC
->SetFont(**fontptr
);
323 void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo
& link
)
326 m_UseLink
= (link
.GetHref() != wxEmptyString
);
330 void wxHtmlWinParser::SetFontFace(const wxString
& face
)
332 if (GetFontFixed()) m_FontFaceFixed
= face
;
333 else m_FontFaceNormal
= face
;
335 if (m_InputEnc
!= wxFONTENCODING_DEFAULT
)
336 SetInputEncoding(m_InputEnc
);
341 void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc
)
343 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
350 if (enc
== wxFONTENCODING_DEFAULT
) return;
352 wxFontEncoding altfix
, altnorm
;
353 bool availfix
, availnorm
;
356 availnorm
= wxTheFontMapper
->IsEncodingAvailable(enc
, m_FontFaceNormal
);
357 availfix
= wxTheFontMapper
->IsEncodingAvailable(enc
, m_FontFaceFixed
);
358 if (availnorm
&& availfix
)
362 else if (wxTheFontMapper
->GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, FALSE
) &&
363 wxTheFontMapper
->GetAltForEncoding(enc
, &altfix
, m_FontFaceFixed
, FALSE
) &&
365 m_OutputEnc
= altnorm
;
367 // at least normal face?
370 else if (wxTheFontMapper
->GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, FALSE
))
371 m_OutputEnc
= altnorm
;
373 // okay, let convert to ISO_8859-1, available always
375 m_OutputEnc
= wxFONTENCODING_DEFAULT
;
378 if (m_OutputEnc
== wxFONTENCODING_DEFAULT
)
379 GetEntitiesParser()->SetEncoding(wxFONTENCODING_SYSTEM
);
381 GetEntitiesParser()->SetEncoding(m_OutputEnc
);
383 if (m_InputEnc
== m_OutputEnc
) return;
385 m_EncConv
= new wxEncodingConverter();
386 if (!m_EncConv
->Init(m_InputEnc
,
387 (m_OutputEnc
== wxFONTENCODING_DEFAULT
) ?
388 wxFONTENCODING_ISO8859_1
: m_OutputEnc
,
389 wxCONVERT_SUBSTITUTE
))
390 { // total failture :-(
391 wxLogError(_("Failed to display HTML document in %s encoding"),
392 wxFontMapper::GetEncodingName(enc
).c_str());
393 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
403 //-----------------------------------------------------------------------------
404 // wxHtmlWinTagHandler
405 //-----------------------------------------------------------------------------
407 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler
, wxHtmlTagHandler
)
411 //-----------------------------------------------------------------------------
413 //-----------------------------------------------------------------------------
416 IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule
, wxModule
)
419 bool wxHtmlTagsModule::OnInit()
421 wxHtmlWinParser::AddModule(this);
427 void wxHtmlTagsModule::OnExit()
429 wxHtmlWinParser::RemoveModule(this);