]> git.saurik.com Git - wxWidgets.git/blob - src/html/winpars.cpp
3de90241e001ee2c052da51de66063461fba891e
[wxWidgets.git] / src / html / winpars.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: winpars.cpp
3 // Purpose: wxHtmlParser class (generic parser)
4 // Author: Vaclav Slavik
5 // RCS-ID: $Id$
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
9
10
11 #ifdef __GNUG__
12 #pragma implementation
13 #endif
14
15 #include "wx/wxprec.h"
16
17 #include "wx/defs.h"
18 #if wxUSE_HTML
19
20 #ifdef __BORDLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WXPRECOMP
25 #include "wx/wx.h"
26 #endif
27
28 #include "wx/html/htmldefs.h"
29 #include "wx/html/winpars.h"
30 #include "wx/html/htmlwin.h"
31
32
33 //-----------------------------------------------------------------------------
34 // wxHtmlWinParser
35 //-----------------------------------------------------------------------------
36
37
38 wxList wxHtmlWinParser::m_Modules;
39
40 wxHtmlWinParser::wxHtmlWinParser(wxWindow *wnd) : wxHtmlParser()
41 {
42 m_Window = wnd;
43 m_Container = NULL;
44 m_DC = NULL;
45 m_CharHeight = m_CharWidth = 0;
46 m_UseLink = FALSE;
47
48 {
49 int i, j, k, l, m;
50 for (i = 0; i < 2; i++)
51 for (j = 0; j < 2; j++)
52 for (k = 0; k < 2; k++)
53 for (l = 0; l < 2; l++)
54 for (m = 0; m < 7; m++)
55 m_FontsTable[i][j][k][l][m] = NULL;
56 #ifdef __WXMSW__
57 static int default_sizes[7] = {7, 8, 10, 12, 16, 22, 30};
58 #else
59 static int default_sizes[7] = {10, 12, 14, 16, 19, 24, 32};
60 #endif
61 SetFonts("", "", default_sizes);
62 }
63
64 // fill in wxHtmlParser's tables:
65 wxNode *node = m_Modules.GetFirst();
66 while (node) {
67 wxHtmlTagsModule *mod = (wxHtmlTagsModule*) node -> GetData();
68 mod -> FillHandlersTable(this);
69 node = node -> GetNext();
70 }
71 }
72
73
74
75 void wxHtmlWinParser::AddModule(wxHtmlTagsModule *module)
76 {
77 m_Modules.Append(module);
78 }
79
80
81
82 void wxHtmlWinParser::SetFonts(wxString normal_face, wxString fixed_face, const int *sizes)
83 {
84 int i, j, k, l, m;
85
86 for (i = 0; i < 7; i++) m_FontsSizes[i] = sizes[i];
87 m_FontFaceFixed = fixed_face;
88 m_FontFaceNormal = normal_face;
89
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++) {
95 if (m_FontsTable[i][j][k][l][m] != NULL) {
96 delete m_FontsTable[i][j][k][l][m];
97 m_FontsTable[i][j][k][l][m] = NULL;
98 }
99 }
100 }
101
102
103
104 void wxHtmlWinParser::InitParser(const wxString& source)
105 {
106 wxHtmlParser::InitParser(source);
107 wxASSERT_MSG(m_DC != NULL, _("no DC assigned to wxHtmlWinParser!!"));
108
109 m_FontBold = m_FontItalic = m_FontUnderlined = m_FontFixed = FALSE;
110 m_FontSize = 3; //default one
111 CreateCurrentFont(); // we're selecting default font into
112 m_DC -> GetTextExtent("H", &m_CharWidth, &m_CharHeight);
113 /* NOTE : we're not using GetCharWidth/Height() because
114 of differences under X and win
115 */
116
117 m_UseLink = FALSE;
118 m_Link = wxHtmlLinkInfo("", "");
119 m_LinkColor.Set(0, 0, 0xFF);
120 m_ActualColor.Set(0, 0, 0);
121 m_Align = wxHTML_ALIGN_LEFT;
122 m_tmpLastWasSpace = FALSE;
123
124 OpenContainer();
125
126 OpenContainer();
127 m_Container -> InsertCell(new wxHtmlColourCell(m_ActualColor));
128 m_Container -> InsertCell(new wxHtmlFontCell(CreateCurrentFont()));
129 }
130
131
132
133 void wxHtmlWinParser::DoneParser()
134 {
135 m_Container = NULL;
136 wxHtmlParser::DoneParser();
137 }
138
139
140
141 wxObject* wxHtmlWinParser::GetProduct()
142 {
143 wxHtmlContainerCell *top;
144
145 CloseContainer();
146 OpenContainer();
147 GetContainer() -> SetIndent(m_CharHeight, wxHTML_INDENT_TOP);
148 top = m_Container;
149 while (top -> GetParent()) top = top -> GetParent();
150 return top;
151 }
152
153
154
155 wxList* wxHtmlWinParser::GetTempData()
156 {
157 int i, j, k, l, m;
158 wxFont *f;
159 wxList *lst = wxHtmlParser::GetTempData();
160
161 if (lst == NULL) lst = new wxList;
162 lst -> DeleteContents(TRUE);
163
164 for (i = 0; i < 2; i++)
165 for (j = 0; j < 2; j++)
166 for (k = 0; k < 2; k++)
167 for (l = 0; l < 2; l++)
168 for (m = 0; m < 7; m++) {
169 f = m_FontsTable[i][j][k][l][m];
170 if (f) lst -> Append(f);
171 }
172 return lst;
173 }
174
175
176
177 void wxHtmlWinParser::AddText(const char* txt)
178 {
179 wxHtmlCell *c;
180 int i = 0, x, lng = strlen(txt);
181 char temp[wxHTML_BUFLEN];
182 register char d;
183 int templen = 0;
184
185 if (m_tmpLastWasSpace) {
186 while ((i < lng) && ((txt[i] == '\n') || (txt[i] == '\r') || (txt[i] == ' ') || (txt[i] == '\t'))) i++;
187 }
188
189 while (i < lng) {
190 x = 0;
191 d = temp[templen++] = txt[i];
192 if ((d == '\n') || (d == '\r') || (d == ' ') || (d == '\t')) {
193 i++, x++;
194 while ((i < lng) && ((txt[i] == '\n') || (txt[i] == '\r') || (txt[i] == ' ') || (txt[i] == '\t'))) i++, x++;
195 }
196 else i++;
197
198 if (x) {
199 temp[templen-1] = ' ';
200 temp[templen] = 0;
201 templen = 0;
202 c = new wxHtmlWordCell(temp, *(GetDC()));
203 if (m_UseLink) c -> SetLink(m_Link);
204 m_Container -> InsertCell(c);
205 m_tmpLastWasSpace = TRUE;
206 }
207 }
208 if (templen) {
209 temp[templen] = 0;
210 c = new wxHtmlWordCell(temp, *(GetDC()));
211 if (m_UseLink) c -> SetLink(m_Link);
212 m_Container -> InsertCell(c);
213 m_tmpLastWasSpace = FALSE;
214 }
215 }
216
217
218
219 wxHtmlContainerCell* wxHtmlWinParser::OpenContainer()
220 {
221 m_Container = new wxHtmlContainerCell(m_Container);
222 m_Container -> SetAlignHor(m_Align);
223 m_tmpLastWasSpace = TRUE;
224 /* to avoid space being first character in paragraph */
225 return m_Container;
226 }
227
228
229
230 wxHtmlContainerCell* wxHtmlWinParser::SetContainer(wxHtmlContainerCell *c)
231 {
232 m_tmpLastWasSpace = TRUE;
233 /* to avoid space being first character in paragraph */
234 return m_Container = c;
235 }
236
237
238
239 wxHtmlContainerCell* wxHtmlWinParser::CloseContainer()
240 {
241 m_Container = m_Container -> GetParent();
242 return m_Container;
243 }
244
245
246 void wxHtmlWinParser::SetFontSize(int s)
247 {
248 if (s < 1) s = 1;
249 else if (s > 7) s = 7;
250 m_FontSize = s;
251 }
252
253
254
255 wxFont* wxHtmlWinParser::CreateCurrentFont()
256 {
257 int fb = GetFontBold(),
258 fi = GetFontItalic(),
259 fu = GetFontUnderlined(),
260 ff = GetFontFixed(),
261 fs = GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ;
262
263 if (m_FontsTable[fb][fi][fu][ff][fs] == NULL) {
264 m_FontsTable[fb][fi][fu][ff][fs] =
265 new wxFont(
266 m_FontsSizes[fs] * m_PixelScale,
267 ff ? wxMODERN : wxSWISS,
268 fi ? wxITALIC : wxNORMAL,
269 fb ? wxBOLD : wxNORMAL,
270 fu ? TRUE : FALSE, ff ? m_FontFaceFixed : m_FontFaceNormal);
271 }
272 m_DC -> SetFont(*(m_FontsTable[fb][fi][fu][ff][fs]));
273 return (m_FontsTable[fb][fi][fu][ff][fs]);
274 }
275
276
277
278 void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo& link)
279 {
280 m_Link = link;
281 m_UseLink = (link.GetHref() != wxEmptyString);
282 }
283
284
285
286
287 //-----------------------------------------------------------------------------
288 // wxHtmlWinTagHandler
289 //-----------------------------------------------------------------------------
290
291 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler, wxHtmlTagHandler)
292
293
294
295 //-----------------------------------------------------------------------------
296 // wxHtmlTagsModule
297 //-----------------------------------------------------------------------------
298
299
300 IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule, wxModule)
301
302
303 bool wxHtmlTagsModule::OnInit()
304 {
305 wxHtmlWinParser::AddModule(this);
306 return TRUE;
307 }
308
309
310
311 void wxHtmlTagsModule::OnExit()
312 {
313 }
314 #endif
315