]> git.saurik.com Git - wxWidgets.git/blame - src/html/htmlwin.cpp
cosmetic fixes to font encoding names
[wxWidgets.git] / src / html / htmlwin.cpp
CommitLineData
5526e819
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: htmlwin.cpp
3// Purpose: wxHtmlWindow class for parsing & displaying HTML (implementation)
4// Author: Vaclav Slavik
69941f05 5// RCS-ID: $Id$
5526e819
VS
6// Copyright: (c) 1999 Vaclav Slavik
7// Licence: wxWindows Licence
8/////////////////////////////////////////////////////////////////////////////
9
10
11#ifdef __GNUG__
12#pragma implementation
13#endif
14
3096bd2f 15#include "wx/wxprec.h"
5526e819
VS
16
17#include "wx/defs.h"
f6bcfd97 18#if wxUSE_HTML && wxUSE_STREAMS
5526e819
VS
19
20#ifdef __BORDLANDC__
21#pragma hdrstop
22#endif
23
24#ifndef WXPRECOMP
3096bd2f 25#include "wx/wx.h"
5526e819
VS
26#endif
27
69941f05 28#include "wx/html/htmlwin.h"
69941f05 29#include "wx/html/forcelnk.h"
f3c82859 30#include "wx/log.h"
5526e819 31
5526e819
VS
32
33//-----------------------------------------------------------------------------
34// wxHtmlWindow
35//-----------------------------------------------------------------------------
36
37
38
3096bd2f 39#include "wx/arrimpl.cpp"
5526e819
VS
40WX_DEFINE_OBJARRAY(HtmlHistoryArray)
41
42
269e8200 43wxHtmlWindow::wxHtmlWindow(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
f6bcfd97 44 long style, const wxString& name) : wxScrolledWindow(parent, id, pos, size, style | wxVSCROLL | wxHSCROLL, name)
5526e819
VS
45{
46 m_tmpMouseMoved = FALSE;
846914d1 47 m_tmpLastLink = NULL;
89de9af3 48 m_tmpCanDrawLocks = 0;
5526e819
VS
49 m_FS = new wxFileSystem();
50 m_RelatedStatusBar = -1;
51 m_RelatedFrame = NULL;
52 m_TitleFormat = "%s";
d5db80c2 53 m_OpenedPage = m_OpenedAnchor = m_OpenedPageTitle = wxEmptyString;
5526e819
VS
54 m_Cell = NULL;
55 m_Parser = new wxHtmlWinParser(this);
56 m_Parser -> SetFS(m_FS);
57 SetBorders(10);
58 m_HistoryPos = -1;
59 m_HistoryOn = TRUE;
a547ebff 60 m_Style = style;
5526e819
VS
61 SetPage("<html><body></body></html>");
62}
63
64
65
66wxHtmlWindow::~wxHtmlWindow()
67{
68 HistoryClear();
69
70 if (m_Cell) delete m_Cell;
71
5526e819
VS
72 delete m_Parser;
73 delete m_FS;
74}
75
76
77
78void wxHtmlWindow::SetRelatedFrame(wxFrame* frame, const wxString& format)
79{
80 m_RelatedFrame = frame;
81 m_TitleFormat = format;
82}
83
84
85
86void wxHtmlWindow::SetRelatedStatusBar(int bar)
87{
88 m_RelatedStatusBar = bar;
89}
269e8200
RD
90
91
92
8eb2940f 93void wxHtmlWindow::SetFonts(wxString normal_face, wxString fixed_face, const int *sizes)
5526e819 94{
d5db80c2
VS
95 wxString op = m_OpenedPage;
96
8eb2940f 97 m_Parser -> SetFonts(normal_face, fixed_face, sizes);
fb5700fe 98 SetPage(wxT("<html><body></body></html>")); // fonts changed => contents invalid
d5db80c2 99 if (!op.IsEmpty()) LoadPage(op);
5526e819
VS
100}
101
102
103
104bool wxHtmlWindow::SetPage(const wxString& source)
105{
106 wxClientDC *dc = new wxClientDC(this);
107
108 dc -> SetMapMode(wxMM_TEXT);
109 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
d5db80c2 110 m_OpenedPage = m_OpenedAnchor = m_OpenedPageTitle = wxEmptyString;
5526e819 111 m_Parser -> SetDC(dc);
f61815af 112 if (m_Cell) {
89de9af3
VS
113 delete m_Cell;
114 m_Cell = NULL;
f61815af 115 }
5526e819
VS
116 m_Cell = (wxHtmlContainerCell*) m_Parser -> Parse(source);
117 delete dc;
efba2b89
VS
118 m_Cell -> SetIndent(m_Borders, wxHTML_INDENT_ALL, wxHTML_UNITS_PIXELS);
119 m_Cell -> SetAlignHor(wxHTML_ALIGN_CENTER);
5526e819 120 CreateLayout();
89de9af3 121 if (m_tmpCanDrawLocks == 0) Refresh();
5526e819
VS
122 return TRUE;
123}
124
125
126bool wxHtmlWindow::LoadPage(const wxString& location)
127{
128 wxFSFile *f;
129 bool rt_val;
fc7dfaf8
VS
130 bool needs_refresh = FALSE;
131
169ee06c 132 SetCursor(*wxHOURGLASS_CURSOR);
fc7dfaf8 133 wxYield(); Refresh(FALSE);
5526e819 134
89de9af3 135 m_tmpCanDrawLocks++;
5526e819
VS
136 if (m_HistoryOn && (m_HistoryPos != -1)) { // store scroll position into history item
137 int x, y;
138 ViewStart(&x, &y);
139 m_History[m_HistoryPos].SetPos(y);
140 }
141
fc7dfaf8 142 if (location[0] == wxT('#')) { // local anchor
5526e819 143 wxString anch = location.Mid(1) /*1 to end*/;
89de9af3 144 m_tmpCanDrawLocks--;
5526e819 145 rt_val = ScrollToAnchor(anch);
fc7dfaf8
VS
146 m_tmpCanDrawLocks++;
147 }
148 else if (location.Find(wxT('#')) != wxNOT_FOUND && location.BeforeFirst(wxT('#')) == m_OpenedPage) {
149 wxString anch = location.AfterFirst(wxT('#'));
150 m_tmpCanDrawLocks--;
151 rt_val = ScrollToAnchor(anch);
152 m_tmpCanDrawLocks++;
153 }
154 else if (location.Find(wxT('#')) != wxNOT_FOUND &&
155 (m_FS -> GetPath() + location.BeforeFirst(wxT('#'))) == m_OpenedPage) {
156 wxString anch = location.AfterFirst(wxT('#'));
157 m_tmpCanDrawLocks--;
158 rt_val = ScrollToAnchor(anch);
159 m_tmpCanDrawLocks++;
5526e819
VS
160 }
161
162 else {
fc7dfaf8 163 needs_refresh = TRUE;
5526e819
VS
164 // load&display it:
165 if (m_RelatedStatusBar != -1) {
166 m_RelatedFrame -> SetStatusText(_("Connecting..."), m_RelatedStatusBar);
fc7dfaf8 167 Refresh(FALSE);
5526e819
VS
168 }
169
170 f = m_FS -> OpenFile(location);
fc7dfaf8 171
5526e819
VS
172 if (f == NULL) {
173 wxString err;
174
f6bcfd97 175 wxLogError(_("Unable to open requested HTML document: %s"), location.c_str());
89de9af3 176 m_tmpCanDrawLocks--;
169ee06c
VS
177
178 SetCursor(*wxSTANDARD_CURSOR);
5526e819
VS
179 return FALSE;
180 }
181
182 else {
183 wxNode *node;
184 wxString src = wxEmptyString;
185
186 if (m_RelatedStatusBar != -1) {
187 wxString msg = _("Loading : ") + location;
188 m_RelatedFrame -> SetStatusText(msg, m_RelatedStatusBar);
fc7dfaf8 189 Refresh(FALSE);
5526e819
VS
190 }
191
192 node = m_Filters.GetFirst();
193 while (node){
194 wxHtmlFilter *h = (wxHtmlFilter*) node -> GetData();
195 if (h -> CanRead(*f)) {
196 src = h -> ReadFile(*f);
197 break;
198 }
199 node = node -> GetNext();
200 }
a76015e6 201 if (src == wxEmptyString) {
89de9af3
VS
202 if (m_DefaultFilter == NULL) m_DefaultFilter = GetDefaultFilter();
203 src = m_DefaultFilter -> ReadFile(*f);
204 }
5526e819
VS
205
206 m_FS -> ChangePathTo(f -> GetLocation());
207 rt_val = SetPage(src);
208 m_OpenedPage = f -> GetLocation();
209 if (f -> GetAnchor() != wxEmptyString) {
fc7dfaf8 210 wxYield();
5526e819 211 ScrollToAnchor(f -> GetAnchor());
5526e819
VS
212 }
213
214 delete f;
215
216 if (m_RelatedStatusBar != -1) m_RelatedFrame -> SetStatusText(_("Done"), m_RelatedStatusBar);
217 }
218 }
219
220 if (m_HistoryOn) { // add this page to history there:
221 int c = m_History.GetCount() - (m_HistoryPos + 1);
222
223 m_HistoryPos++;
224 for (int i = 0; i < c; i++)
225 m_History.Remove(m_HistoryPos);
226 m_History.Add(new HtmlHistoryItem(m_OpenedPage, m_OpenedAnchor));
227 }
228
096824d7
VS
229 if (m_OpenedPageTitle == wxEmptyString)
230 OnSetTitle(wxFileNameFromPath(m_OpenedPage));
169ee06c 231 SetCursor(*wxSTANDARD_CURSOR);
fc7dfaf8
VS
232
233 if (needs_refresh) {
234 wxYield();
235 m_tmpCanDrawLocks--;
236 Refresh();
237 }
238 else
239 m_tmpCanDrawLocks--;
240
5526e819
VS
241 return rt_val;
242}
243
244
245
246bool wxHtmlWindow::ScrollToAnchor(const wxString& anchor)
247{
efba2b89 248 const wxHtmlCell *c = m_Cell -> Find(wxHTML_COND_ISANCHOR, &anchor);
f3c82859
VS
249 if (!c)
250 {
f6bcfd97 251 wxLogWarning(_("HTML anchor %s does not exist."), anchor.c_str());
f3c82859
VS
252 return FALSE;
253 }
5526e819
VS
254 else {
255 int y;
269e8200 256
5526e819 257 for (y = 0; c != NULL; c = c -> GetParent()) y += c -> GetPosY();
efba2b89 258 Scroll(-1, y / wxHTML_SCROLL_STEP);
5526e819
VS
259 m_OpenedAnchor = anchor;
260 return TRUE;
261 }
262}
263
264
d5db80c2 265void wxHtmlWindow::OnSetTitle(const wxString& title)
5526e819
VS
266{
267 if (m_RelatedFrame) {
268 wxString tit;
269 tit.Printf(m_TitleFormat, title.c_str());
270 m_RelatedFrame -> SetTitle(tit);
271 }
d5db80c2 272 m_OpenedPageTitle = title;
5526e819
VS
273}
274
275
276
277
278
279void wxHtmlWindow::CreateLayout()
280{
281 int ClientWidth, ClientHeight;
282
283 if (!m_Cell) return;
a547ebff 284
f6bcfd97 285 if (m_Style & wxHW_SCROLLBAR_NEVER) {
08d038e7 286 SetScrollbars(wxHTML_SCROLL_STEP, 1, m_Cell -> GetWidth() / wxHTML_SCROLL_STEP, 0); // always off
a547ebff
VS
287 GetClientSize(&ClientWidth, &ClientHeight);
288 m_Cell -> Layout(ClientWidth);
289 }
290
291 else {
292 GetClientSize(&ClientWidth, &ClientHeight);
a547ebff 293 m_Cell -> Layout(ClientWidth);
f3bcfd9b
VS
294 if (ClientHeight < m_Cell -> GetHeight() + GetCharHeight()) {
295 SetScrollbars(
296 wxHTML_SCROLL_STEP, wxHTML_SCROLL_STEP,
297 m_Cell -> GetWidth() / wxHTML_SCROLL_STEP,
298 (m_Cell -> GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
299 /*cheat: top-level frag is always container*/);
a547ebff 300 }
89de9af3 301 else { /* we fit into window, no need for scrollbars */
08d038e7 302 SetScrollbars(wxHTML_SCROLL_STEP, 1, m_Cell -> GetWidth() / wxHTML_SCROLL_STEP, 0); // disable...
89de9af3
VS
303 GetClientSize(&ClientWidth, &ClientHeight);
304 m_Cell -> Layout(ClientWidth); // ...and relayout
305 }
a547ebff 306 }
5526e819
VS
307}
308
269e8200 309
5526e819
VS
310
311void wxHtmlWindow::ReadCustomization(wxConfigBase *cfg, wxString path)
312{
313 wxString oldpath;
314 wxString tmp;
d5db80c2
VS
315 int p_fontsizes[7];
316 wxString p_fff, p_ffn;
5526e819
VS
317
318 if (path != wxEmptyString) {
319 oldpath = cfg -> GetPath();
320 cfg -> SetPath(path);
321 }
322
323 m_Borders = cfg -> Read("wxHtmlWindow/Borders", m_Borders);
d5db80c2
VS
324 p_fff = cfg -> Read("wxHtmlWindow/FontFaceFixed", m_Parser -> m_FontFaceFixed);
325 p_ffn = cfg -> Read("wxHtmlWindow/FontFaceNormal", m_Parser -> m_FontFaceNormal);
5526e819 326 for (int i = 0; i < 7; i++) {
66a77a74 327 tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i);
d5db80c2 328 p_fontsizes[i] = cfg -> Read(tmp, m_Parser -> m_FontsSizes[i]);
5526e819 329 }
8eb2940f 330 SetFonts(p_ffn, p_fff, p_fontsizes);
5526e819
VS
331
332 if (path != wxEmptyString)
333 cfg -> SetPath(oldpath);
334}
335
336
337
338void wxHtmlWindow::WriteCustomization(wxConfigBase *cfg, wxString path)
339{
340 wxString oldpath;
341 wxString tmp;
342
343 if (path != wxEmptyString) {
344 oldpath = cfg -> GetPath();
345 cfg -> SetPath(path);
346 }
347
348 cfg -> Write("wxHtmlWindow/Borders", (long) m_Borders);
349 cfg -> Write("wxHtmlWindow/FontFaceFixed", m_Parser -> m_FontFaceFixed);
350 cfg -> Write("wxHtmlWindow/FontFaceNormal", m_Parser -> m_FontFaceNormal);
5526e819 351 for (int i = 0; i < 7; i++) {
66a77a74 352 tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i);
5526e819
VS
353 cfg -> Write(tmp, (long) m_Parser -> m_FontsSizes[i]);
354 }
355
356 if (path != wxEmptyString)
357 cfg -> SetPath(oldpath);
358}
359
360
361
362bool wxHtmlWindow::HistoryBack()
363{
364 wxString a, l;
365
366 if (m_HistoryPos < 1) return FALSE;
367
bbda1088
VS
368 // store scroll position into history item:
369 int x, y;
370 ViewStart(&x, &y);
371 m_History[m_HistoryPos].SetPos(y);
372
373 // go to previous position:
5526e819
VS
374 m_HistoryPos--;
375
376 l = m_History[m_HistoryPos].GetPage();
377 a = m_History[m_HistoryPos].GetAnchor();
378 m_HistoryOn = FALSE;
89de9af3 379 m_tmpCanDrawLocks++;
5526e819 380 if (a == wxEmptyString) LoadPage(l);
fc7dfaf8 381 else LoadPage(l + wxT("#") + a);
5526e819 382 m_HistoryOn = TRUE;
c88293a4 383 wxYield();
89de9af3 384 m_tmpCanDrawLocks--;
c88293a4 385 Scroll(0, m_History[m_HistoryPos].GetPos());
5526e819
VS
386 Refresh();
387 return TRUE;
388}
389
1b113a81
VS
390bool wxHtmlWindow::HistoryCanBack()
391{
392 if (m_HistoryPos < 1) return FALSE;
393 return TRUE ;
394}
5526e819
VS
395
396
397bool wxHtmlWindow::HistoryForward()
398{
399 wxString a, l;
400
401 if (m_HistoryPos == -1) return FALSE;
402 if (m_HistoryPos >= (int)m_History.GetCount() - 1)return FALSE;
403
404 m_OpenedPage = wxEmptyString; // this will disable adding new entry into history in LoadPage()
405
406 m_HistoryPos++;
407 l = m_History[m_HistoryPos].GetPage();
408 a = m_History[m_HistoryPos].GetAnchor();
409 m_HistoryOn = FALSE;
89de9af3 410 m_tmpCanDrawLocks++;
5526e819 411 if (a == wxEmptyString) LoadPage(l);
fc7dfaf8 412 else LoadPage(l + wxT("#") + a);
5526e819 413 m_HistoryOn = TRUE;
c88293a4 414 wxYield();
89de9af3 415 m_tmpCanDrawLocks--;
c88293a4 416 Scroll(0, m_History[m_HistoryPos].GetPos());
5526e819
VS
417 Refresh();
418 return TRUE;
419}
420
1b113a81
VS
421bool wxHtmlWindow::HistoryCanForward()
422{
423 if (m_HistoryPos == -1) return FALSE;
424 if (m_HistoryPos >= (int)m_History.GetCount() - 1)return FALSE;
425 return TRUE ;
426}
5526e819
VS
427
428
429void wxHtmlWindow::HistoryClear()
430{
431 m_History.Empty();
432 m_HistoryPos = -1;
433}
434
435
436
437wxList wxHtmlWindow::m_Filters;
a76015e6 438wxHtmlFilter *wxHtmlWindow::m_DefaultFilter = NULL;
66806a0b
VS
439wxCursor *wxHtmlWindow::s_cur_hand = NULL;
440wxCursor *wxHtmlWindow::s_cur_arrow = NULL;
a76015e6
VS
441
442void wxHtmlWindow::CleanUpStatics()
443{
444 if (m_DefaultFilter) delete m_DefaultFilter;
445 m_DefaultFilter = NULL;
269e8200
RD
446 m_Filters.DeleteContents(TRUE);
447 m_Filters.Clear();
66806a0b
VS
448 if (s_cur_hand) delete s_cur_hand;
449 if (s_cur_arrow) delete s_cur_arrow;
a76015e6
VS
450}
451
452
5526e819
VS
453
454void wxHtmlWindow::AddFilter(wxHtmlFilter *filter)
455{
5526e819
VS
456 m_Filters.Append(filter);
457}
458
459
460
461
0b2dadd3 462void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo& link)
5526e819 463{
0b2dadd3 464 LoadPage(link.GetHref());
5526e819
VS
465}
466
467
468
469void wxHtmlWindow::OnDraw(wxDC& dc)
470{
471 int x, y;
472 wxRegionIterator upd(GetUpdateRegion()); // get the update rect list
473 int v_y, v_h;
474
89de9af3 475 if (m_tmpCanDrawLocks > 0) return;
fc7dfaf8 476
5526e819 477 dc.SetMapMode(wxMM_TEXT);
2faa3a6b
VS
478#if 0
479/* VS - I don't think this is neccessary any longer
480 MSC_VER 1200 means MSVC 6.0 and it works fine */
5526e819
VS
481#if defined(_MSC_VER) && (_MSC_VER == 1200)
482 ::SetMapMode((HDC)dc.GetHDC(), MM_TEXT);
2faa3a6b 483#endif
5526e819
VS
484#endif
485 dc.SetBackgroundMode(wxTRANSPARENT);
486 ViewStart(&x, &y);
487
488 while (upd) {
489 v_y = upd.GetY();
490 v_h = upd.GetH();
efba2b89 491 if (m_Cell) m_Cell -> Draw(dc, 0, 0, y * wxHTML_SCROLL_STEP + v_y, y * wxHTML_SCROLL_STEP + v_h + v_y);
5526e819
VS
492 upd++;
493 }
494}
495
496
497
498
499void wxHtmlWindow::OnSize(wxSizeEvent& event)
500{
501 wxScrolledWindow::OnSize(event);
502 CreateLayout();
f6bcfd97 503 Refresh();
5526e819
VS
504}
505
506
5526e819
VS
507void wxHtmlWindow::OnMouseEvent(wxMouseEvent& event)
508{
509 m_tmpMouseMoved = TRUE;
510
511 if (event.ButtonDown()) {
512 int sx, sy;
513 wxPoint pos;
514 wxString lnk;
515
efba2b89 516 ViewStart(&sx, &sy); sx *= wxHTML_SCROLL_STEP; sy *= wxHTML_SCROLL_STEP;
5526e819
VS
517 pos = event.GetPosition();
518
519 if (m_Cell)
0b2dadd3 520 m_Cell -> OnMouseClick(this, sx + pos.x, sy + pos.y, event);
5526e819
VS
521 }
522}
523
524
525
526void wxHtmlWindow::OnIdle(wxIdleEvent& event)
527{
66806a0b
VS
528 if (s_cur_hand == NULL)
529 {
530 s_cur_hand = new wxCursor(wxCURSOR_HAND);
531 s_cur_arrow = new wxCursor(wxCURSOR_ARROW);
532 }
5526e819
VS
533
534 if (m_tmpMouseMoved && (m_Cell != NULL)) {
535 int sx, sy;
536 int x, y;
846914d1 537 wxHtmlLinkInfo *lnk;
5526e819 538
efba2b89 539 ViewStart(&sx, &sy); sx *= wxHTML_SCROLL_STEP; sy *= wxHTML_SCROLL_STEP;
5526e819
VS
540 wxGetMousePosition(&x, &y);
541 ScreenToClient(&x, &y);
542 lnk = m_Cell -> GetLink(sx + x, sy + y);
543
622ea783 544 if (lnk != m_tmpLastLink) {
846914d1 545 if (lnk == NULL) {
66806a0b 546 SetCursor(*s_cur_arrow);
622ea783
VS
547 if (m_RelatedStatusBar != -1) m_RelatedFrame -> SetStatusText(wxEmptyString, m_RelatedStatusBar);
548 }
549 else {
66806a0b 550 SetCursor(*s_cur_hand);
846914d1
VS
551 if (m_RelatedStatusBar != -1)
552 m_RelatedFrame -> SetStatusText(lnk -> GetHref(), m_RelatedStatusBar);
622ea783
VS
553 }
554 m_tmpLastLink = lnk;
5526e819
VS
555 }
556 m_tmpMouseMoved = FALSE;
557 }
558}
559
560
561
562
563IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow,wxScrolledWindow)
564
565BEGIN_EVENT_TABLE(wxHtmlWindow, wxScrolledWindow)
566 EVT_SIZE(wxHtmlWindow::OnSize)
567 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent)
568 EVT_MOTION(wxHtmlWindow::OnMouseEvent)
569 EVT_IDLE(wxHtmlWindow::OnIdle)
5526e819
VS
570END_EVENT_TABLE()
571
572
573
574
575
a76015e6
VS
576// A module to allow initialization/cleanup
577// without calling these functions from app.cpp or from
578// the user's application.
579
580class wxHtmlWinModule: public wxModule
581{
582DECLARE_DYNAMIC_CLASS(wxHtmlWinModule)
583public:
584 wxHtmlWinModule() : wxModule() {}
585 bool OnInit() { return TRUE; }
586 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
587};
588
589IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule, wxModule)
590
5526e819
VS
591
592
593
594///// default mod handlers are forced there:
595
c88293a4
VS
596FORCE_LINK(m_layout)
597FORCE_LINK(m_fonts)
598FORCE_LINK(m_image)
599FORCE_LINK(m_list)
600FORCE_LINK(m_dflist)
601FORCE_LINK(m_pre)
602FORCE_LINK(m_hline)
603FORCE_LINK(m_links)
604FORCE_LINK(m_tables)
fa146dd7 605FORCE_LINK(m_meta)
5526e819
VS
606
607
483ff5a5 608#endif