]>
git.saurik.com Git - wxWidgets.git/blob - src/html/winpars.cpp
e90c35f7234c6be4685b1cbb98ba8623a3bfab6e
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
++)
60 m_FontsTable
[i
][j
][k
][l
][m
] = NULL
;
61 m_FontsFacesTable
[i
][j
][k
][l
][m
] = wxEmptyString
;
62 m_FontsEncTable
[i
][j
][k
][l
][m
] = wxFONTENCODING_DEFAULT
;
65 static int default_sizes
[7] = {7, 8, 10, 12, 16, 22, 30};
66 #elif defined(__WXMAC__)
67 static int default_sizes
[7] = {9, 12, 14, 18, 24, 30, 36};
69 static int default_sizes
[7] = {10, 12, 14, 16, 19, 24, 32};
71 SetFonts("", "", default_sizes
);
74 // fill in wxHtmlParser's tables:
75 wxNode
*node
= m_Modules
.GetFirst();
78 wxHtmlTagsModule
*mod
= (wxHtmlTagsModule
*) node
->GetData();
79 mod
->FillHandlersTable(this);
80 node
= node
->GetNext();
85 wxHtmlWinParser::~wxHtmlWinParser()
89 for (i
= 0; i
< 2; i
++)
90 for (j
= 0; j
< 2; j
++)
91 for (k
= 0; k
< 2; k
++)
92 for (l
= 0; l
< 2; l
++)
93 for (m
= 0; m
< 7; m
++)
95 if (m_FontsTable
[i
][j
][k
][l
][m
] != NULL
)
96 delete m_FontsTable
[i
][j
][k
][l
][m
];
98 if (m_EncConv
) delete m_EncConv
;
102 void wxHtmlWinParser::AddModule(wxHtmlTagsModule
*module)
104 m_Modules
.Append(module);
109 void wxHtmlWinParser::RemoveModule(wxHtmlTagsModule
*module)
111 m_Modules
.DeleteObject(module);
116 void wxHtmlWinParser::SetFonts(wxString normal_face
, wxString fixed_face
, const int *sizes
)
120 for (i
= 0; i
< 7; i
++) m_FontsSizes
[i
] = sizes
[i
];
121 m_FontFaceFixed
= fixed_face
;
122 m_FontFaceNormal
= normal_face
;
124 SetInputEncoding(m_InputEnc
);
126 for (i
= 0; i
< 2; i
++)
127 for (j
= 0; j
< 2; j
++)
128 for (k
= 0; k
< 2; k
++)
129 for (l
= 0; l
< 2; l
++)
130 for (m
= 0; m
< 7; m
++) {
131 if (m_FontsTable
[i
][j
][k
][l
][m
] != NULL
)
133 delete m_FontsTable
[i
][j
][k
][l
][m
];
134 m_FontsTable
[i
][j
][k
][l
][m
] = NULL
;
141 void wxHtmlWinParser::InitParser(const wxString
& source
)
143 wxHtmlParser::InitParser(source
);
144 wxASSERT_MSG(m_DC
!= NULL
, _T("no DC assigned to wxHtmlWinParser!!"));
146 m_FontBold
= m_FontItalic
= m_FontUnderlined
= m_FontFixed
= FALSE
;
147 m_FontSize
= 3; //default one
148 CreateCurrentFont(); // we're selecting default font into
149 m_DC
->GetTextExtent("H", &m_CharWidth
, &m_CharHeight
);
150 /* NOTE : we're not using GetCharWidth/Height() because
151 of differences under X and win
155 m_Link
= wxHtmlLinkInfo("", "");
156 m_LinkColor
.Set(0, 0, 0xFF);
157 m_ActualColor
.Set(0, 0, 0);
158 m_Align
= wxHTML_ALIGN_LEFT
;
159 m_tmpLastWasSpace
= FALSE
;
164 m_Container
->InsertCell(new wxHtmlColourCell(m_ActualColor
));
165 m_Container
->InsertCell(new wxHtmlFontCell(CreateCurrentFont()));
170 void wxHtmlWinParser::DoneParser()
173 SetInputEncoding(wxFONTENCODING_DEFAULT
); // for next call
174 wxHtmlParser::DoneParser();
179 wxObject
* wxHtmlWinParser::GetProduct()
181 wxHtmlContainerCell
*top
;
187 while (top
->GetParent()) top
= top
->GetParent();
193 void wxHtmlWinParser::AddText(const char* txt
)
196 int i
= 0, x
, lng
= strlen(txt
);
197 char temp
[wxHTML_BUFLEN
];
201 if (m_tmpLastWasSpace
)
204 ((txt
[i
] == '\n') || (txt
[i
] == '\r') || (txt
[i
] == ' ') ||
205 (txt
[i
] == '\t'))) i
++;
211 d
= temp
[templen
++] = txt
[i
];
212 if ((d
== '\n') || (d
== '\r') || (d
== ' ') || (d
== '\t'))
215 while ((i
< lng
) && ((txt
[i
] == '\n') || (txt
[i
] == '\r') ||
216 (txt
[i
] == ' ') || (txt
[i
] == '\t'))) i
++, x
++;
222 temp
[templen
-1] = ' ';
226 m_EncConv
->Convert(temp
);
227 c
= new wxHtmlWordCell(GetEntitiesParser()->Parse(temp
), *(GetDC()));
230 m_Container
->InsertCell(c
);
231 m_tmpLastWasSpace
= TRUE
;
238 m_EncConv
->Convert(temp
);
239 c
= new wxHtmlWordCell(GetEntitiesParser()->Parse(temp
), *(GetDC()));
242 m_Container
->InsertCell(c
);
243 m_tmpLastWasSpace
= FALSE
;
249 wxHtmlContainerCell
* wxHtmlWinParser::OpenContainer()
251 m_Container
= new wxHtmlContainerCell(m_Container
);
252 m_Container
->SetAlignHor(m_Align
);
253 m_tmpLastWasSpace
= TRUE
;
254 /* to avoid space being first character in paragraph */
260 wxHtmlContainerCell
* wxHtmlWinParser::SetContainer(wxHtmlContainerCell
*c
)
262 m_tmpLastWasSpace
= TRUE
;
263 /* to avoid space being first character in paragraph */
264 return m_Container
= c
;
269 wxHtmlContainerCell
* wxHtmlWinParser::CloseContainer()
271 m_Container
= m_Container
->GetParent();
276 void wxHtmlWinParser::SetFontSize(int s
)
279 else if (s
> 7) s
= 7;
285 wxFont
* wxHtmlWinParser::CreateCurrentFont()
287 int fb
= GetFontBold(),
288 fi
= GetFontItalic(),
289 fu
= GetFontUnderlined(),
291 fs
= GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ;
293 wxString face
= ff
? m_FontFaceFixed
: m_FontFaceNormal
;
294 wxString
*faceptr
= &(m_FontsFacesTable
[fb
][fi
][fu
][ff
][fs
]);
295 wxFont
**fontptr
= &(m_FontsTable
[fb
][fi
][fu
][ff
][fs
]);
296 wxFontEncoding
*encptr
= &(m_FontsEncTable
[fb
][fi
][fu
][ff
][fs
]);
298 if (*fontptr
!= NULL
&& (*faceptr
!= face
|| *encptr
!= m_OutputEnc
))
304 if (*fontptr
== NULL
)
307 *encptr
= m_OutputEnc
;
308 *fontptr
= new wxFont(
309 (int) (m_FontsSizes
[fs
] * m_PixelScale
),
310 ff
? wxMODERN
: wxSWISS
,
311 fi
? wxITALIC
: wxNORMAL
,
312 fb
? wxBOLD
: wxNORMAL
,
313 fu
? TRUE
: FALSE
, face
,
316 m_DC
->SetFont(**fontptr
);
322 void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo
& link
)
325 m_UseLink
= (link
.GetHref() != wxEmptyString
);
329 void wxHtmlWinParser::SetFontFace(const wxString
& face
)
331 if (GetFontFixed()) m_FontFaceFixed
= face
;
332 else m_FontFaceNormal
= face
;
334 if (m_InputEnc
!= wxFONTENCODING_DEFAULT
)
335 SetInputEncoding(m_InputEnc
);
340 void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc
)
342 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
349 if (enc
== wxFONTENCODING_DEFAULT
) return;
351 wxFontEncoding altfix
, altnorm
;
352 bool availfix
, availnorm
;
355 availnorm
= wxTheFontMapper
->IsEncodingAvailable(enc
, m_FontFaceNormal
);
356 availfix
= wxTheFontMapper
->IsEncodingAvailable(enc
, m_FontFaceFixed
);
357 if (availnorm
&& availfix
)
361 else if (wxTheFontMapper
->GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, FALSE
) &&
362 wxTheFontMapper
->GetAltForEncoding(enc
, &altfix
, m_FontFaceFixed
, FALSE
) &&
364 m_OutputEnc
= altnorm
;
366 // at least normal face?
369 else if (wxTheFontMapper
->GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, FALSE
))
370 m_OutputEnc
= altnorm
;
372 // okay, let convert to ISO_8859-1, available always
374 m_OutputEnc
= wxFONTENCODING_DEFAULT
;
377 if (m_OutputEnc
== wxFONTENCODING_DEFAULT
)
378 GetEntitiesParser()->SetEncoding(wxFONTENCODING_SYSTEM
);
380 GetEntitiesParser()->SetEncoding(m_OutputEnc
);
382 if (m_InputEnc
== m_OutputEnc
) return;
384 m_EncConv
= new wxEncodingConverter();
385 if (!m_EncConv
->Init(m_InputEnc
,
386 (m_OutputEnc
== wxFONTENCODING_DEFAULT
) ?
387 wxFONTENCODING_ISO8859_1
: m_OutputEnc
,
388 wxCONVERT_SUBSTITUTE
))
389 { // total failture :-(
390 wxLogError(_("Failed to display HTML document in %s encoding"),
391 wxFontMapper::GetEncodingName(enc
).c_str());
392 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
402 //-----------------------------------------------------------------------------
403 // wxHtmlWinTagHandler
404 //-----------------------------------------------------------------------------
406 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler
, wxHtmlTagHandler
)
410 //-----------------------------------------------------------------------------
412 //-----------------------------------------------------------------------------
415 IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule
, wxModule
)
418 bool wxHtmlTagsModule::OnInit()
420 wxHtmlWinParser::AddModule(this);
426 void wxHtmlTagsModule::OnExit()
428 wxHtmlWinParser::RemoveModule(this);