1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtmlWindow class for parsing & displaying HTML (implementation)
4 // Author: Vaclav Slavik
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "htmlwin.h"
13 #pragma implementation "htmlproc.h"
16 #include "wx/wxprec.h"
19 #if wxUSE_HTML && wxUSE_STREAMS
29 #include "wx/html/htmlwin.h"
30 #include "wx/html/forcelnk.h"
31 #include "wx/html/htmlproc.h"
33 #include "wx/arrimpl.cpp"
35 #include "wx/listimpl.cpp"
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 // item of history list
42 class WXDLLEXPORT wxHtmlHistoryItem
: public wxObject
45 wxHtmlHistoryItem(const wxString
& p
, const wxString
& a
) {m_Page
= p
, m_Anchor
= a
, m_Pos
= 0;}
46 int GetPos() const {return m_Pos
;}
47 void SetPos(int p
) {m_Pos
= p
;}
48 const wxString
& GetPage() const {return m_Page
;}
49 const wxString
& GetAnchor() const {return m_Anchor
;}
58 //-----------------------------------------------------------------------------
59 // our private arrays:
60 //-----------------------------------------------------------------------------
62 WX_DECLARE_OBJARRAY(wxHtmlHistoryItem
, wxHtmlHistoryArray
);
63 WX_DEFINE_OBJARRAY(wxHtmlHistoryArray
);
65 WX_DECLARE_LIST(wxHtmlProcessor
, wxHtmlProcessorList
);
66 WX_DEFINE_LIST(wxHtmlProcessorList
);
68 //-----------------------------------------------------------------------------
70 //-----------------------------------------------------------------------------
73 wxHtmlWindow::wxHtmlWindow(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
74 long style
, const wxString
& name
) : wxScrolledWindow(parent
, id
, pos
, size
, style
| wxVSCROLL
| wxHSCROLL
, name
)
76 m_tmpMouseMoved
= FALSE
;
78 m_tmpCanDrawLocks
= 0;
79 m_FS
= new wxFileSystem();
80 m_RelatedStatusBar
= -1;
81 m_RelatedFrame
= NULL
;
82 m_TitleFormat
= wxT("%s");
83 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
85 m_Parser
= new wxHtmlWinParser(this);
86 m_Parser
->SetFS(m_FS
);
90 m_History
= new wxHtmlHistoryArray
;
93 SetPage(wxT("<html><body></body></html>"));
98 wxHtmlWindow::~wxHtmlWindow()
102 if (m_Cell
) delete m_Cell
;
112 void wxHtmlWindow::SetRelatedFrame(wxFrame
* frame
, const wxString
& format
)
114 m_RelatedFrame
= frame
;
115 m_TitleFormat
= format
;
120 void wxHtmlWindow::SetRelatedStatusBar(int bar
)
122 m_RelatedStatusBar
= bar
;
127 void wxHtmlWindow::SetFonts(wxString normal_face
, wxString fixed_face
, const int *sizes
)
129 wxString op
= m_OpenedPage
;
131 m_Parser
->SetFonts(normal_face
, fixed_face
, sizes
);
132 // fonts changed => contents invalid, so reload the page:
133 SetPage(wxT("<html><body></body></html>"));
134 if (!op
.IsEmpty()) LoadPage(op
);
139 bool wxHtmlWindow::SetPage(const wxString
& source
)
141 wxString
newsrc(source
);
143 // pass HTML through registered processors:
144 if (m_Processors
|| m_SharedProcessors
)
146 wxHtmlProcessorList::Node
*nodeL
, *nodeS
;
149 nodeL
= (m_Processors
) ? m_Processors
->GetFirst() : NULL
;
150 nodeS
= (m_SharedProcessors
) ? m_SharedProcessors
->GetFirst() : NULL
;
152 while (nodeL
|| nodeS
)
154 prL
= (nodeL
) ? nodeL
->GetData()->GetPriority() : -1;
155 prS
= (nodeS
) ? nodeS
->GetData()->GetPriority() : -1;
158 newsrc
= nodeL
->GetData()->Process(newsrc
);
159 nodeL
= nodeL
->GetNext();
163 newsrc
= nodeS
->GetData()->Process(newsrc
);
164 nodeS
= nodeS
->GetNext();
169 // ...and run the parser on it:
170 wxClientDC
*dc
= new wxClientDC(this);
171 dc
->SetMapMode(wxMM_TEXT
);
172 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
173 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
180 m_Cell
= (wxHtmlContainerCell
*) m_Parser
->Parse(newsrc
);
182 m_Cell
->SetIndent(m_Borders
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
183 m_Cell
->SetAlignHor(wxHTML_ALIGN_CENTER
);
185 if (m_tmpCanDrawLocks
== 0)
191 bool wxHtmlWindow::LoadPage(const wxString
& location
)
195 bool needs_refresh
= FALSE
;
197 SetCursor(*wxHOURGLASS_CURSOR
);
198 wxYield(); Refresh(FALSE
);
201 if (m_HistoryOn
&& (m_HistoryPos
!= -1)) // store scroll position into history item
205 (*m_History
)[m_HistoryPos
].SetPos(y
);
208 if (location
[0] == wxT('#')) // local anchor
210 wxString anch
= location
.Mid(1) /*1 to end*/;
212 rt_val
= ScrollToAnchor(anch
);
215 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&& location
.BeforeFirst(wxT('#')) == m_OpenedPage
)
217 wxString anch
= location
.AfterFirst(wxT('#'));
219 rt_val
= ScrollToAnchor(anch
);
222 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&&
223 (m_FS
->GetPath() + location
.BeforeFirst(wxT('#'))) == m_OpenedPage
)
225 wxString anch
= location
.AfterFirst(wxT('#'));
227 rt_val
= ScrollToAnchor(anch
);
233 needs_refresh
= TRUE
;
235 if (m_RelatedStatusBar
!= -1)
237 m_RelatedFrame
->SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
241 f
= m_FS
->OpenFile(location
);
247 wxLogError(_("Unable to open requested HTML document: %s"), location
.c_str());
250 SetCursor(*wxSTANDARD_CURSOR
);
257 wxString src
= wxEmptyString
;
259 if (m_RelatedStatusBar
!= -1)
261 wxString msg
= _("Loading : ") + location
;
262 m_RelatedFrame
->SetStatusText(msg
, m_RelatedStatusBar
);
266 node
= m_Filters
.GetFirst();
269 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
272 src
= h
->ReadFile(*f
);
275 node
= node
->GetNext();
277 if (src
== wxEmptyString
)
279 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
280 src
= m_DefaultFilter
->ReadFile(*f
);
283 m_FS
->ChangePathTo(f
->GetLocation());
284 rt_val
= SetPage(src
);
285 m_OpenedPage
= f
->GetLocation();
286 if (f
->GetAnchor() != wxEmptyString
)
289 ScrollToAnchor(f
->GetAnchor());
294 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
->SetStatusText(_("Done"), m_RelatedStatusBar
);
298 if (m_HistoryOn
) // add this page to history there:
300 int c
= m_History
->GetCount() - (m_HistoryPos
+ 1);
303 for (int i
= 0; i
< c
; i
++)
304 m_History
->Remove(m_HistoryPos
);
305 m_History
->Add(new wxHtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
308 if (m_OpenedPageTitle
== wxEmptyString
)
309 OnSetTitle(wxFileNameFromPath(m_OpenedPage
));
310 SetCursor(*wxSTANDARD_CURSOR
);
326 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
328 const wxHtmlCell
*c
= m_Cell
->Find(wxHTML_COND_ISANCHOR
, &anchor
);
331 wxLogWarning(_("HTML anchor %s does not exist."), anchor
.c_str());
338 for (y
= 0; c
!= NULL
; c
= c
->GetParent()) y
+= c
->GetPosY();
339 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
340 m_OpenedAnchor
= anchor
;
346 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
351 tit
.Printf(m_TitleFormat
, title
.c_str());
352 m_RelatedFrame
->SetTitle(tit
);
354 m_OpenedPageTitle
= title
;
361 void wxHtmlWindow::CreateLayout()
363 int ClientWidth
, ClientHeight
;
367 if (m_Style
& wxHW_SCROLLBAR_NEVER
)
369 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // always off
370 GetClientSize(&ClientWidth
, &ClientHeight
);
371 m_Cell
->Layout(ClientWidth
);
375 GetClientSize(&ClientWidth
, &ClientHeight
);
376 m_Cell
->Layout(ClientWidth
);
377 if (ClientHeight
< m_Cell
->GetHeight() + GetCharHeight())
380 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
381 m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
,
382 (m_Cell
->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
383 /*cheat: top-level frag is always container*/);
385 else /* we fit into window, no need for scrollbars */
387 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
388 GetClientSize(&ClientWidth
, &ClientHeight
);
389 m_Cell
->Layout(ClientWidth
); // ...and relayout
396 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
401 wxString p_fff
, p_ffn
;
403 if (path
!= wxEmptyString
)
405 oldpath
= cfg
->GetPath();
409 m_Borders
= cfg
->Read(wxT("wxHtmlWindow/Borders"), m_Borders
);
410 p_fff
= cfg
->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
411 p_ffn
= cfg
->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
412 for (int i
= 0; i
< 7; i
++)
414 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
415 p_fontsizes
[i
] = cfg
->Read(tmp
, m_Parser
->m_FontsSizes
[i
]);
417 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
419 if (path
!= wxEmptyString
)
420 cfg
->SetPath(oldpath
);
425 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
430 if (path
!= wxEmptyString
)
432 oldpath
= cfg
->GetPath();
436 cfg
->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders
);
437 cfg
->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
438 cfg
->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
439 for (int i
= 0; i
< 7; i
++)
441 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
442 cfg
->Write(tmp
, (long) m_Parser
->m_FontsSizes
[i
]);
445 if (path
!= wxEmptyString
)
446 cfg
->SetPath(oldpath
);
451 bool wxHtmlWindow::HistoryBack()
455 if (m_HistoryPos
< 1) return FALSE
;
457 // store scroll position into history item:
460 (*m_History
)[m_HistoryPos
].SetPos(y
);
462 // go to previous position:
465 l
= (*m_History
)[m_HistoryPos
].GetPage();
466 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
469 if (a
== wxEmptyString
) LoadPage(l
);
470 else LoadPage(l
+ wxT("#") + a
);
474 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
479 bool wxHtmlWindow::HistoryCanBack()
481 if (m_HistoryPos
< 1) return FALSE
;
486 bool wxHtmlWindow::HistoryForward()
490 if (m_HistoryPos
== -1) return FALSE
;
491 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
493 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
496 l
= (*m_History
)[m_HistoryPos
].GetPage();
497 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
500 if (a
== wxEmptyString
) LoadPage(l
);
501 else LoadPage(l
+ wxT("#") + a
);
505 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
510 bool wxHtmlWindow::HistoryCanForward()
512 if (m_HistoryPos
== -1) return FALSE
;
513 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
518 void wxHtmlWindow::HistoryClear()
524 void wxHtmlWindow::AddProcessor(wxHtmlProcessor
*processor
)
528 m_Processors
= new wxHtmlProcessorList
;
529 m_Processors
->DeleteContents(TRUE
);
531 wxHtmlProcessorList::Node
*node
;
533 for (node
= m_Processors
->GetFirst(); node
; node
= node
->GetNext())
535 if (processor
->GetPriority() > node
->GetData()->GetPriority())
537 m_Processors
->Insert(node
, processor
);
543 /*static */ void wxHtmlWindow::AddSharedProcessor(wxHtmlProcessor
*processor
)
545 if (!m_SharedProcessors
)
547 m_SharedProcessors
= new wxHtmlProcessorList
;
548 m_SharedProcessors
->DeleteContents(TRUE
);
550 wxHtmlProcessorList::Node
*node
;
552 for (node
= m_SharedProcessors
->GetFirst(); node
; node
= node
->GetNext())
554 if (processor
->GetPriority() > node
->GetData()->GetPriority())
556 m_SharedProcessors
->Insert(node
, processor
);
564 wxList
wxHtmlWindow::m_Filters
;
565 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
566 wxCursor
*wxHtmlWindow::s_cur_hand
= NULL
;
567 wxCursor
*wxHtmlWindow::s_cur_arrow
= NULL
;
568 wxHtmlProcessorList
*wxHtmlWindow::m_SharedProcessors
= NULL
;
570 void wxHtmlWindow::CleanUpStatics()
572 delete m_DefaultFilter
;
573 m_DefaultFilter
= NULL
;
574 m_Filters
.DeleteContents(TRUE
);
577 delete m_SharedProcessors
;
578 m_SharedProcessors
= NULL
;
586 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
588 m_Filters
.Append(filter
);
594 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
596 LoadPage(link
.GetHref());
601 void wxHtmlWindow::OnDraw(wxDC
& dc
)
604 wxRegionIterator
upd(GetUpdateRegion()); // get the update rect list
607 if (m_tmpCanDrawLocks
> 0) return;
609 dc
.SetMapMode(wxMM_TEXT
);
611 /* VS - I don't think this is neccessary any longer
612 MSC_VER 1200 means MSVC 6.0 and it works fine */
613 #if defined(_MSC_VER) && (_MSC_VER == 1200)
614 ::SetMapMode((HDC
)dc
.GetHDC(), MM_TEXT
);
617 dc
.SetBackgroundMode(wxTRANSPARENT
);
624 if (m_Cell
) m_Cell
->Draw(dc
, 0, 0, y
* wxHTML_SCROLL_STEP
+ v_y
, y
* wxHTML_SCROLL_STEP
+ v_h
+ v_y
);
632 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
634 wxScrolledWindow::OnSize(event
);
640 void wxHtmlWindow::OnMouseEvent(wxMouseEvent
& event
)
642 m_tmpMouseMoved
= TRUE
;
644 if (event
.ButtonDown())
650 ViewStart(&sx
, &sy
); sx
*= wxHTML_SCROLL_STEP
; sy
*= wxHTML_SCROLL_STEP
;
651 pos
= event
.GetPosition();
654 m_Cell
->OnMouseClick(this, sx
+ pos
.x
, sy
+ pos
.y
, event
);
660 void wxHtmlWindow::OnIdle(wxIdleEvent
& WXUNUSED(event
))
662 if (s_cur_hand
== NULL
)
664 s_cur_hand
= new wxCursor(wxCURSOR_HAND
);
665 s_cur_arrow
= new wxCursor(wxCURSOR_ARROW
);
668 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
))
674 ViewStart(&sx
, &sy
); sx
*= wxHTML_SCROLL_STEP
; sy
*= wxHTML_SCROLL_STEP
;
675 wxGetMousePosition(&x
, &y
);
676 ScreenToClient(&x
, &y
);
677 lnk
= m_Cell
->GetLink(sx
+ x
, sy
+ y
);
679 if (lnk
!= m_tmpLastLink
)
683 SetCursor(*s_cur_arrow
);
684 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
->SetStatusText(wxEmptyString
, m_RelatedStatusBar
);
688 SetCursor(*s_cur_hand
);
689 if (m_RelatedStatusBar
!= -1)
690 m_RelatedFrame
->SetStatusText(lnk
->GetHref(), m_RelatedStatusBar
);
694 m_tmpMouseMoved
= FALSE
;
701 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
703 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
704 EVT_SIZE(wxHtmlWindow::OnSize
)
705 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent
)
706 EVT_MOTION(wxHtmlWindow::OnMouseEvent
)
707 EVT_IDLE(wxHtmlWindow::OnIdle
)
714 // A module to allow initialization/cleanup
715 // without calling these functions from app.cpp or from
716 // the user's application.
718 class wxHtmlWinModule
: public wxModule
720 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
722 wxHtmlWinModule() : wxModule() {}
723 bool OnInit() { return TRUE
; }
724 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
727 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
732 ///// default mod handlers are forced there: