]>
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()
50 m_CharHeight
= m_CharWidth
= 0;
53 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
57 for (i
= 0; i
< 2; i
++)
58 for (j
= 0; j
< 2; j
++)
59 for (k
= 0; k
< 2; k
++)
60 for (l
= 0; l
< 2; l
++)
61 for (m
= 0; m
< 7; m
++)
63 m_FontsTable
[i
][j
][k
][l
][m
] = NULL
;
64 m_FontsFacesTable
[i
][j
][k
][l
][m
] = wxEmptyString
;
65 m_FontsEncTable
[i
][j
][k
][l
][m
] = wxFONTENCODING_DEFAULT
;
68 static int default_sizes
[7] = {7, 8, 10, 12, 16, 22, 30};
69 #elif defined(__WXMAC__)
70 static int default_sizes
[7] = {9, 12, 14, 18, 24, 30, 36};
72 static int default_sizes
[7] = {10, 12, 14, 16, 19, 24, 32};
74 SetFonts("", "", default_sizes
);
77 // fill in wxHtmlParser's tables:
78 wxNode
*node
= m_Modules
.GetFirst();
81 wxHtmlTagsModule
*mod
= (wxHtmlTagsModule
*) node
->GetData();
82 mod
->FillHandlersTable(this);
83 node
= node
->GetNext();
88 wxHtmlWinParser::~wxHtmlWinParser()
92 for (i
= 0; i
< 2; i
++)
93 for (j
= 0; j
< 2; j
++)
94 for (k
= 0; k
< 2; k
++)
95 for (l
= 0; l
< 2; l
++)
96 for (m
= 0; m
< 7; m
++)
98 if (m_FontsTable
[i
][j
][k
][l
][m
] != NULL
)
99 delete m_FontsTable
[i
][j
][k
][l
][m
];
102 delete[] m_tmpStrBuf
;
106 void wxHtmlWinParser::AddModule(wxHtmlTagsModule
*module)
108 m_Modules
.Append(module);
113 void wxHtmlWinParser::RemoveModule(wxHtmlTagsModule
*module)
115 m_Modules
.DeleteObject(module);
120 void wxHtmlWinParser::SetFonts(wxString normal_face
, wxString fixed_face
, const int *sizes
)
124 for (i
= 0; i
< 7; i
++) m_FontsSizes
[i
] = sizes
[i
];
125 m_FontFaceFixed
= fixed_face
;
126 m_FontFaceNormal
= normal_face
;
128 SetInputEncoding(m_InputEnc
);
130 for (i
= 0; i
< 2; i
++)
131 for (j
= 0; j
< 2; j
++)
132 for (k
= 0; k
< 2; k
++)
133 for (l
= 0; l
< 2; l
++)
134 for (m
= 0; m
< 7; m
++) {
135 if (m_FontsTable
[i
][j
][k
][l
][m
] != NULL
)
137 delete m_FontsTable
[i
][j
][k
][l
][m
];
138 m_FontsTable
[i
][j
][k
][l
][m
] = NULL
;
145 void wxHtmlWinParser::InitParser(const wxString
& source
)
147 wxHtmlParser::InitParser(source
);
148 wxASSERT_MSG(m_DC
!= NULL
, _T("no DC assigned to wxHtmlWinParser!!"));
150 m_FontBold
= m_FontItalic
= m_FontUnderlined
= m_FontFixed
= FALSE
;
151 m_FontSize
= 3; //default one
152 CreateCurrentFont(); // we're selecting default font into
153 m_DC
->GetTextExtent("H", &m_CharWidth
, &m_CharHeight
);
154 /* NOTE : we're not using GetCharWidth/Height() because
155 of differences under X and win
159 m_Link
= wxHtmlLinkInfo("", "");
160 m_LinkColor
.Set(0, 0, 0xFF);
161 m_ActualColor
.Set(0, 0, 0);
162 m_Align
= wxHTML_ALIGN_LEFT
;
163 m_tmpLastWasSpace
= FALSE
;
168 m_Container
->InsertCell(new wxHtmlColourCell(m_ActualColor
));
169 m_Container
->InsertCell(new wxHtmlFontCell(CreateCurrentFont()));
174 void wxHtmlWinParser::DoneParser()
177 SetInputEncoding(wxFONTENCODING_DEFAULT
); // for next call
178 wxHtmlParser::DoneParser();
183 wxObject
* wxHtmlWinParser::GetProduct()
185 wxHtmlContainerCell
*top
;
191 while (top
->GetParent()) top
= top
->GetParent();
196 void wxHtmlWinParser::AddText(const wxChar
* txt
)
204 wxChar nbsp
= GetEntitiesParser()->GetCharForCode(160 /* nbsp */);
206 if (lng
+1 > m_tmpStrBufSize
)
208 delete[] m_tmpStrBuf
;
209 m_tmpStrBuf
= new wxChar
[lng
+1];
210 m_tmpStrBufSize
= lng
+1;
212 wxChar
*temp
= m_tmpStrBuf
;
214 if (m_tmpLastWasSpace
)
217 ((txt
[i
] == wxT('\n')) || (txt
[i
] == wxT('\r')) || (txt
[i
] == wxT(' ')) ||
218 (txt
[i
] == wxT('\t')))) i
++;
224 d
= temp
[templen
++] = txt
[i
];
225 if ((d
== wxT('\n')) || (d
== wxT('\r')) || (d
== wxT(' ')) || (d
== wxT('\t')))
228 while ((i
< lng
) && ((txt
[i
] == wxT('\n')) || (txt
[i
] == wxT('\r')) ||
229 (txt
[i
] == wxT(' ')) || (txt
[i
] == wxT('\t')))) i
++, x
++;
235 temp
[templen
-1] = wxT(' ');
237 if (templen
== 1) continue;
240 m_EncConv
->Convert(temp
);
241 wxString str
= GetEntitiesParser()->Parse(temp
);
242 size_t len
= str
.Len();
243 for (size_t j
= 0; j
< len
; j
++)
244 if (str
.GetChar(j
) == nbsp
)
246 c
= new wxHtmlWordCell(str
, *(GetDC()));
249 m_Container
->InsertCell(c
);
250 m_tmpLastWasSpace
= TRUE
;
254 if (templen
&& (templen
> 1 || temp
[0] != wxT(' ')))
258 m_EncConv
->Convert(temp
);
259 wxString str
= GetEntitiesParser()->Parse(temp
);
260 size_t len
= str
.Len();
261 for (size_t j
= 0; j
< len
; j
++)
262 if (str
.GetChar(j
) == nbsp
)
264 c
= new wxHtmlWordCell(str
, *(GetDC()));
267 m_Container
->InsertCell(c
);
268 m_tmpLastWasSpace
= FALSE
;
274 wxHtmlContainerCell
* wxHtmlWinParser::OpenContainer()
276 m_Container
= new wxHtmlContainerCell(m_Container
);
277 m_Container
->SetAlignHor(m_Align
);
278 m_tmpLastWasSpace
= TRUE
;
279 /* to avoid space being first character in paragraph */
285 wxHtmlContainerCell
* wxHtmlWinParser::SetContainer(wxHtmlContainerCell
*c
)
287 m_tmpLastWasSpace
= TRUE
;
288 /* to avoid space being first character in paragraph */
289 return m_Container
= c
;
294 wxHtmlContainerCell
* wxHtmlWinParser::CloseContainer()
296 m_Container
= m_Container
->GetParent();
301 void wxHtmlWinParser::SetFontSize(int s
)
304 else if (s
> 7) s
= 7;
310 wxFont
* wxHtmlWinParser::CreateCurrentFont()
312 int fb
= GetFontBold(),
313 fi
= GetFontItalic(),
314 fu
= GetFontUnderlined(),
316 fs
= GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ;
318 wxString face
= ff
? m_FontFaceFixed
: m_FontFaceNormal
;
319 wxString
*faceptr
= &(m_FontsFacesTable
[fb
][fi
][fu
][ff
][fs
]);
320 wxFont
**fontptr
= &(m_FontsTable
[fb
][fi
][fu
][ff
][fs
]);
321 wxFontEncoding
*encptr
= &(m_FontsEncTable
[fb
][fi
][fu
][ff
][fs
]);
323 if (*fontptr
!= NULL
&& (*faceptr
!= face
|| *encptr
!= m_OutputEnc
))
329 if (*fontptr
== NULL
)
332 *encptr
= m_OutputEnc
;
333 *fontptr
= new wxFont(
334 (int) (m_FontsSizes
[fs
] * m_PixelScale
),
335 ff
? wxMODERN
: wxSWISS
,
336 fi
? wxITALIC
: wxNORMAL
,
337 fb
? wxBOLD
: wxNORMAL
,
338 fu
? TRUE
: FALSE
, face
,
341 m_DC
->SetFont(**fontptr
);
347 void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo
& link
)
350 m_UseLink
= (link
.GetHref() != wxEmptyString
);
354 void wxHtmlWinParser::SetFontFace(const wxString
& face
)
356 if (GetFontFixed()) m_FontFaceFixed
= face
;
357 else m_FontFaceNormal
= face
;
359 if (m_InputEnc
!= wxFONTENCODING_DEFAULT
)
360 SetInputEncoding(m_InputEnc
);
365 void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc
)
367 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
374 if (enc
== wxFONTENCODING_DEFAULT
) return;
376 wxFontEncoding altfix
, altnorm
;
377 bool availfix
, availnorm
;
380 availnorm
= wxTheFontMapper
->IsEncodingAvailable(enc
, m_FontFaceNormal
);
381 availfix
= wxTheFontMapper
->IsEncodingAvailable(enc
, m_FontFaceFixed
);
382 if (availnorm
&& availfix
)
386 else if (wxTheFontMapper
->GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, FALSE
) &&
387 wxTheFontMapper
->GetAltForEncoding(enc
, &altfix
, m_FontFaceFixed
, FALSE
) &&
389 m_OutputEnc
= altnorm
;
391 // at least normal face?
394 else if (wxTheFontMapper
->GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, FALSE
))
395 m_OutputEnc
= altnorm
;
397 // okay, let convert to ISO_8859-1, available always
399 m_OutputEnc
= wxFONTENCODING_DEFAULT
;
402 if (m_OutputEnc
== wxFONTENCODING_DEFAULT
)
403 GetEntitiesParser()->SetEncoding(wxFONTENCODING_SYSTEM
);
405 GetEntitiesParser()->SetEncoding(m_OutputEnc
);
407 if (m_InputEnc
== m_OutputEnc
) return;
409 m_EncConv
= new wxEncodingConverter();
410 if (!m_EncConv
->Init(m_InputEnc
,
411 (m_OutputEnc
== wxFONTENCODING_DEFAULT
) ?
412 wxFONTENCODING_ISO8859_1
: m_OutputEnc
,
413 wxCONVERT_SUBSTITUTE
))
414 { // total failture :-(
415 wxLogError(_("Failed to display HTML document in %s encoding"),
416 wxFontMapper::GetEncodingName(enc
).c_str());
417 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
427 //-----------------------------------------------------------------------------
428 // wxHtmlWinTagHandler
429 //-----------------------------------------------------------------------------
431 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler
, wxHtmlTagHandler
)
435 //-----------------------------------------------------------------------------
437 //-----------------------------------------------------------------------------
440 IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule
, wxModule
)
443 bool wxHtmlTagsModule::OnInit()
445 wxHtmlWinParser::AddModule(this);
451 void wxHtmlTagsModule::OnExit()
453 wxHtmlWinParser::RemoveModule(this);