]> git.saurik.com Git - wxWidgets.git/blob - src/html/winpars.cpp
Don't call StartingKey if the key was F2
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
12 #pragma implementation "winpars.h"
13 #endif
14
15 #include "wx/wxprec.h"
16
17 #include "wx/defs.h"
18 #if wxUSE_HTML && wxUSE_STREAMS
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WXPRECOMP
25 #include "wx/intl.h"
26 #include "wx/dc.h"
27 #endif
28
29 #include "wx/html/htmldefs.h"
30 #include "wx/html/winpars.h"
31 #include "wx/html/htmlwin.h"
32 #include "wx/fontmap.h"
33 #include "wx/log.h"
34 #include "wx/settings.h"
35 #include "wx/uri.h"
36
37
38 //-----------------------------------------------------------------------------
39 // wxHtmlWinParser
40 //-----------------------------------------------------------------------------
41
42 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinParser, wxHtmlParser)
43
44 wxList wxHtmlWinParser::m_Modules;
45
46 wxHtmlWinParser::wxHtmlWinParser(wxHtmlWindow *wnd) : wxHtmlParser()
47 {
48 m_tmpStrBuf = NULL;
49 m_tmpStrBufSize = 0;
50 m_Window = wnd;
51 m_Container = NULL;
52 m_DC = NULL;
53 m_CharHeight = m_CharWidth = 0;
54 m_UseLink = false;
55 #if !wxUSE_UNICODE
56 m_EncConv = NULL;
57 m_InputEnc = wxFONTENCODING_ISO8859_1;
58 m_OutputEnc = wxFONTENCODING_DEFAULT;
59 #endif
60 m_lastWordCell = NULL;
61
62 {
63 int i, j, k, l, m;
64 for (i = 0; i < 2; i++)
65 for (j = 0; j < 2; j++)
66 for (k = 0; k < 2; k++)
67 for (l = 0; l < 2; l++)
68 for (m = 0; m < 7; m++)
69 {
70 m_FontsTable[i][j][k][l][m] = NULL;
71 m_FontsFacesTable[i][j][k][l][m] = wxEmptyString;
72 #if !wxUSE_UNICODE
73 m_FontsEncTable[i][j][k][l][m] = wxFONTENCODING_DEFAULT;
74 #endif
75 }
76
77 SetFonts(wxEmptyString, wxEmptyString, NULL);
78 }
79
80 // fill in wxHtmlParser's tables:
81 wxList::compatibility_iterator node = m_Modules.GetFirst();
82 while (node)
83 {
84 wxHtmlTagsModule *mod = (wxHtmlTagsModule*) node->GetData();
85 mod->FillHandlersTable(this);
86 node = node->GetNext();
87 }
88 }
89
90 wxHtmlWinParser::~wxHtmlWinParser()
91 {
92 int i, j, k, l, m;
93
94 for (i = 0; i < 2; i++)
95 for (j = 0; j < 2; j++)
96 for (k = 0; k < 2; k++)
97 for (l = 0; l < 2; l++)
98 for (m = 0; m < 7; m++)
99 {
100 if (m_FontsTable[i][j][k][l][m] != NULL)
101 delete m_FontsTable[i][j][k][l][m];
102 }
103 #if !wxUSE_UNICODE
104 delete m_EncConv;
105 #endif
106 delete[] m_tmpStrBuf;
107 }
108
109 void wxHtmlWinParser::AddModule(wxHtmlTagsModule *module)
110 {
111 m_Modules.Append(module);
112 }
113
114 void wxHtmlWinParser::RemoveModule(wxHtmlTagsModule *module)
115 {
116 m_Modules.DeleteObject(module);
117 }
118
119 void wxHtmlWinParser::SetFonts(wxString normal_face, wxString fixed_face,
120 const int *sizes)
121 {
122 static int default_sizes[7] =
123 {
124 wxHTML_FONT_SIZE_1,
125 wxHTML_FONT_SIZE_2,
126 wxHTML_FONT_SIZE_3,
127 wxHTML_FONT_SIZE_4,
128 wxHTML_FONT_SIZE_5,
129 wxHTML_FONT_SIZE_6,
130 wxHTML_FONT_SIZE_7
131 };
132
133 if (sizes == NULL) sizes = default_sizes;
134
135 int i, j, k, l, m;
136
137 for (i = 0; i < 7; i++) m_FontsSizes[i] = sizes[i];
138 m_FontFaceFixed = fixed_face;
139 m_FontFaceNormal = normal_face;
140
141 #if !wxUSE_UNICODE
142 SetInputEncoding(m_InputEnc);
143 #endif
144
145 for (i = 0; i < 2; i++)
146 for (j = 0; j < 2; j++)
147 for (k = 0; k < 2; k++)
148 for (l = 0; l < 2; l++)
149 for (m = 0; m < 7; m++) {
150 if (m_FontsTable[i][j][k][l][m] != NULL)
151 {
152 delete m_FontsTable[i][j][k][l][m];
153 m_FontsTable[i][j][k][l][m] = NULL;
154 }
155 }
156 }
157
158 void wxHtmlWinParser::SetStandardFonts(int size,
159 const wxString& normal_face,
160 const wxString& fixed_face)
161 {
162 wxFont defaultFont = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
163
164 int f_sizes[7];
165 if (size == -1)
166 size = defaultFont.GetPointSize();
167
168 f_sizes[0] = int(size * 0.6);
169 f_sizes[1] = int(size * 0.8);
170 f_sizes[2] = size;
171 f_sizes[3] = int(size * 1.2);
172 f_sizes[4] = int(size * 1.4);
173 f_sizes[5] = int(size * 1.6);
174 f_sizes[6] = int(size * 1.8);
175
176 wxString normal = normal_face.empty() ?
177 defaultFont.GetFaceName() : normal_face;
178
179 SetFonts(normal, fixed_face, f_sizes);
180 }
181
182 void wxHtmlWinParser::InitParser(const wxString& source)
183 {
184 wxHtmlParser::InitParser(source);
185 wxASSERT_MSG(m_DC != NULL, wxT("no DC assigned to wxHtmlWinParser!!"));
186
187 m_FontBold = m_FontItalic = m_FontUnderlined = m_FontFixed = FALSE;
188 m_FontSize = 3; //default one
189 CreateCurrentFont(); // we're selecting default font into
190 m_DC->GetTextExtent( wxT("H"), &m_CharWidth, &m_CharHeight);
191 /* NOTE : we're not using GetCharWidth/Height() because
192 of differences under X and win
193 */
194
195 m_UseLink = false;
196 m_Link = wxHtmlLinkInfo( wxEmptyString );
197 m_LinkColor.Set(0, 0, 0xFF);
198 m_ActualColor.Set(0, 0, 0);
199 m_Align = wxHTML_ALIGN_LEFT;
200 m_tmpLastWasSpace = false;
201 m_lastWordCell = NULL;
202
203 OpenContainer();
204 OpenContainer();
205
206 #if !wxUSE_UNICODE
207 wxString charset = ExtractCharsetInformation(source);
208 if (!charset.empty())
209 {
210 wxFontEncoding enc = wxFontMapper::Get()->CharsetToEncoding(charset);
211 if (enc != wxFONTENCODING_SYSTEM)
212 SetInputEncoding(enc);
213 }
214 #endif
215
216 m_Container->InsertCell(new wxHtmlColourCell(m_ActualColor));
217 wxColour windowColour = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW) ;
218 m_Container->InsertCell(
219 new wxHtmlColourCell(GetWindow() ?
220 GetWindow()->GetBackgroundColour() :
221 windowColour,
222 wxHTML_CLR_BACKGROUND));
223 m_Container->InsertCell(new wxHtmlFontCell(CreateCurrentFont()));
224 }
225
226 void wxHtmlWinParser::DoneParser()
227 {
228 m_Container = NULL;
229 #if !wxUSE_UNICODE
230 SetInputEncoding(wxFONTENCODING_ISO8859_1); // for next call
231 #endif
232 wxHtmlParser::DoneParser();
233 }
234
235 wxObject* wxHtmlWinParser::GetProduct()
236 {
237 wxHtmlContainerCell *top;
238
239 CloseContainer();
240 OpenContainer();
241
242 top = m_Container;
243 while (top->GetParent()) top = top->GetParent();
244 top->RemoveExtraSpacing(true, true);
245
246 return top;
247 }
248
249 wxFSFile *wxHtmlWinParser::OpenURL(wxHtmlURLType type,
250 const wxString& url) const
251 {
252 if ( m_Window )
253 {
254 wxString myurl(url);
255 wxHtmlOpeningStatus status;
256 for (;;)
257 {
258 wxString myfullurl(myurl);
259
260 // consider url as absolute path first
261 wxURI current(myurl);
262 myfullurl = current.BuildUnescapedURI();
263
264 // if not absolute then ...
265 if( current.IsReference() )
266 {
267 wxString basepath = GetFS()->GetPath();
268 wxURI base(basepath);
269
270 // ... try to apply base path if valid ...
271 if( !base.IsReference() )
272 {
273 wxURI path(myfullurl);
274 path.Resolve( base );
275 myfullurl = path.BuildUnescapedURI();
276 }
277 else
278 {
279 // ... or force such addition if not included already
280 if( !current.GetPath().Contains(base.GetPath()) )
281 {
282 basepath += myurl;
283 wxURI connected( basepath );
284 myfullurl = connected.BuildUnescapedURI();
285 }
286 }
287 }
288
289 wxString redirect;
290 status = m_Window->OnOpeningURL(type, myfullurl, &redirect);
291 if ( status != wxHTML_REDIRECT )
292 break;
293
294 myurl = redirect;
295 }
296
297 if ( status == wxHTML_BLOCK )
298 return NULL;
299
300 return GetFS()->OpenFile(myurl);
301 }
302
303 return wxHtmlParser::OpenURL(type, url);
304 }
305
306 void wxHtmlWinParser::AddText(const wxChar* txt)
307 {
308 wxHtmlCell *c;
309 size_t i = 0,
310 x,
311 lng = wxStrlen(txt);
312 register wxChar d;
313 int templen = 0;
314 wxChar nbsp = GetEntitiesParser()->GetCharForCode(160 /* nbsp */);
315
316 if (lng+1 > m_tmpStrBufSize)
317 {
318 delete[] m_tmpStrBuf;
319 m_tmpStrBuf = new wxChar[lng+1];
320 m_tmpStrBufSize = lng+1;
321 }
322 wxChar *temp = m_tmpStrBuf;
323
324 if (m_tmpLastWasSpace)
325 {
326 while ((i < lng) &&
327 ((txt[i] == wxT('\n')) || (txt[i] == wxT('\r')) || (txt[i] == wxT(' ')) ||
328 (txt[i] == wxT('\t')))) i++;
329 }
330
331 while (i < lng)
332 {
333 x = 0;
334 d = temp[templen++] = txt[i];
335 if ((d == wxT('\n')) || (d == wxT('\r')) || (d == wxT(' ')) || (d == wxT('\t')))
336 {
337 i++, x++;
338 while ((i < lng) && ((txt[i] == wxT('\n')) || (txt[i] == wxT('\r')) ||
339 (txt[i] == wxT(' ')) || (txt[i] == wxT('\t')))) i++, x++;
340 }
341 else i++;
342
343 if (x)
344 {
345 temp[templen-1] = wxT(' ');
346 temp[templen] = 0;
347 templen = 0;
348 #if !wxUSE_UNICODE
349 if (m_EncConv)
350 m_EncConv->Convert(temp);
351 #endif
352 size_t len = wxStrlen(temp);
353 for (size_t j = 0; j < len; j++)
354 if (temp[j] == nbsp)
355 temp[j] = wxT(' ');
356 c = new wxHtmlWordCell(temp, *(GetDC()));
357 if (m_UseLink)
358 c->SetLink(m_Link);
359 m_Container->InsertCell(c);
360 ((wxHtmlWordCell*)c)->SetPreviousWord(m_lastWordCell);
361 m_lastWordCell = (wxHtmlWordCell*)c;
362 m_tmpLastWasSpace = true;
363 }
364 }
365
366 if (templen && (templen > 1 || temp[0] != wxT(' ')))
367 {
368 temp[templen] = 0;
369 #if !wxUSE_UNICODE
370 if (m_EncConv)
371 m_EncConv->Convert(temp);
372 #endif
373 size_t len = wxStrlen(temp);
374 for (size_t j = 0; j < len; j++)
375 if (temp[j] == nbsp)
376 temp[j] = wxT(' ');
377 c = new wxHtmlWordCell(temp, *(GetDC()));
378 if (m_UseLink)
379 c->SetLink(m_Link);
380 m_Container->InsertCell(c);
381 ((wxHtmlWordCell*)c)->SetPreviousWord(m_lastWordCell);
382 m_lastWordCell = (wxHtmlWordCell*)c;
383 m_tmpLastWasSpace = false;
384 }
385 }
386
387
388
389 wxHtmlContainerCell* wxHtmlWinParser::OpenContainer()
390 {
391 m_Container = new wxHtmlContainerCell(m_Container);
392 m_Container->SetAlignHor(m_Align);
393 m_tmpLastWasSpace = true;
394 /* to avoid space being first character in paragraph */
395 return m_Container;
396 }
397
398
399
400 wxHtmlContainerCell* wxHtmlWinParser::SetContainer(wxHtmlContainerCell *c)
401 {
402 m_tmpLastWasSpace = true;
403 /* to avoid space being first character in paragraph */
404 return m_Container = c;
405 }
406
407
408
409 wxHtmlContainerCell* wxHtmlWinParser::CloseContainer()
410 {
411 m_Container = m_Container->GetParent();
412 return m_Container;
413 }
414
415
416 void wxHtmlWinParser::SetFontSize(int s)
417 {
418 if (s < 1) s = 1;
419 else if (s > 7) s = 7;
420 m_FontSize = s;
421 }
422
423
424
425 wxFont* wxHtmlWinParser::CreateCurrentFont()
426 {
427 int fb = GetFontBold(),
428 fi = GetFontItalic(),
429 fu = GetFontUnderlined(),
430 ff = GetFontFixed(),
431 fs = GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ;
432
433 wxString face = ff ? m_FontFaceFixed : m_FontFaceNormal;
434 wxString *faceptr = &(m_FontsFacesTable[fb][fi][fu][ff][fs]);
435 wxFont **fontptr = &(m_FontsTable[fb][fi][fu][ff][fs]);
436 #if !wxUSE_UNICODE
437 wxFontEncoding *encptr = &(m_FontsEncTable[fb][fi][fu][ff][fs]);
438 #endif
439
440 if (*fontptr != NULL && (*faceptr != face
441 #if !wxUSE_UNICODE
442 || *encptr != m_OutputEnc
443 #endif
444 ))
445 {
446 delete *fontptr;
447 *fontptr = NULL;
448 }
449
450 if (*fontptr == NULL)
451 {
452 *faceptr = face;
453 *fontptr = new wxFont(
454 (int) (m_FontsSizes[fs] * m_PixelScale),
455 ff ? wxMODERN : wxSWISS,
456 fi ? wxITALIC : wxNORMAL,
457 fb ? wxBOLD : wxNORMAL,
458 fu ? true : false, face
459 #if wxUSE_UNICODE
460 );
461 #else
462 , m_OutputEnc);
463 *encptr = m_OutputEnc;
464 #endif
465 }
466 m_DC->SetFont(**fontptr);
467 return (*fontptr);
468 }
469
470
471
472 void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo& link)
473 {
474 m_Link = link;
475 m_UseLink = (link.GetHref() != wxEmptyString);
476 }
477
478
479 void wxHtmlWinParser::SetFontFace(const wxString& face)
480 {
481 if (GetFontFixed()) m_FontFaceFixed = face;
482 else m_FontFaceNormal = face;
483
484 #if !wxUSE_UNICODE
485 if (m_InputEnc != wxFONTENCODING_DEFAULT)
486 SetInputEncoding(m_InputEnc);
487 #endif
488 }
489
490
491
492 #if !wxUSE_UNICODE
493 void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc)
494 {
495 m_InputEnc = m_OutputEnc = wxFONTENCODING_DEFAULT;
496 if (m_EncConv)
497 {
498 delete m_EncConv;
499 m_EncConv = NULL;
500 }
501
502 if (enc == wxFONTENCODING_DEFAULT) return;
503
504 wxFontEncoding altfix, altnorm;
505 bool availfix, availnorm;
506
507 // exact match?
508 availnorm = wxFontMapper::Get()->IsEncodingAvailable(enc, m_FontFaceNormal);
509 availfix = wxFontMapper::Get()->IsEncodingAvailable(enc, m_FontFaceFixed);
510 if (availnorm && availfix)
511 m_OutputEnc = enc;
512
513 // alternatives?
514 else if (wxFontMapper::Get()->GetAltForEncoding(enc, &altnorm, m_FontFaceNormal, false) &&
515 wxFontMapper::Get()->GetAltForEncoding(enc, &altfix, m_FontFaceFixed, false) &&
516 altnorm == altfix)
517 m_OutputEnc = altnorm;
518
519 // at least normal face?
520 else if (availnorm)
521 m_OutputEnc = enc;
522 else if (wxFontMapper::Get()->GetAltForEncoding(enc, &altnorm, m_FontFaceNormal, false))
523 m_OutputEnc = altnorm;
524
525 else
526 {
527 #ifndef __WXMAC__
528 // okay, let convert to ISO_8859-1, available always
529 m_OutputEnc = wxFONTENCODING_DEFAULT;
530 #else
531 m_OutputEnc = wxLocale::GetSystemEncoding() ;
532 #endif
533 }
534
535 m_InputEnc = enc;
536 if (m_OutputEnc == wxFONTENCODING_DEFAULT)
537 GetEntitiesParser()->SetEncoding(wxFONTENCODING_SYSTEM);
538 else
539 GetEntitiesParser()->SetEncoding(m_OutputEnc);
540
541 if (m_InputEnc == m_OutputEnc) return;
542
543 m_EncConv = new wxEncodingConverter();
544 if (!m_EncConv->Init(m_InputEnc,
545 (m_OutputEnc == wxFONTENCODING_DEFAULT) ?
546 wxFONTENCODING_ISO8859_1 : m_OutputEnc,
547 wxCONVERT_SUBSTITUTE))
548 { // total failture :-(
549 wxLogError(_("Failed to display HTML document in %s encoding"),
550 wxFontMapper::GetEncodingName(enc).c_str());
551 m_InputEnc = m_OutputEnc = wxFONTENCODING_DEFAULT;
552 delete m_EncConv;
553 m_EncConv = NULL;
554 }
555 }
556 #endif
557
558
559
560
561 //-----------------------------------------------------------------------------
562 // wxHtmlWinTagHandler
563 //-----------------------------------------------------------------------------
564
565 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler, wxHtmlTagHandler)
566
567 //-----------------------------------------------------------------------------
568 // wxHtmlTagsModule
569 //-----------------------------------------------------------------------------
570
571 // NB: This is *NOT* winpars.cpp's initialization and shutdown code!!
572 // This module is an ancestor for tag handlers modules defined
573 // in m_*.cpp files with TAGS_MODULE_BEGIN...TAGS_MODULE_END construct.
574 //
575 // Do not add any winpars.cpp shutdown or initialization code to it,
576 // create a new module instead!
577
578 IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule, wxModule)
579
580 bool wxHtmlTagsModule::OnInit()
581 {
582 wxHtmlWinParser::AddModule(this);
583 return true;
584 }
585
586 void wxHtmlTagsModule::OnExit()
587 {
588 wxHtmlWinParser::RemoveModule(this);
589 }
590
591 #endif
592