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
28 #include "wx/dcclient.h"
32 #include "wx/html/htmlwin.h"
33 #include "wx/html/htmlproc.h"
36 #include "wx/arrimpl.cpp"
37 #include "wx/listimpl.cpp"
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
43 // item of history list
44 class WXDLLEXPORT wxHtmlHistoryItem
: public wxObject
47 wxHtmlHistoryItem(const wxString
& p
, const wxString
& a
) {m_Page
= p
, m_Anchor
= a
, m_Pos
= 0;}
48 int GetPos() const {return m_Pos
;}
49 void SetPos(int p
) {m_Pos
= p
;}
50 const wxString
& GetPage() const {return m_Page
;}
51 const wxString
& GetAnchor() const {return m_Anchor
;}
60 //-----------------------------------------------------------------------------
61 // our private arrays:
62 //-----------------------------------------------------------------------------
64 WX_DECLARE_OBJARRAY(wxHtmlHistoryItem
, wxHtmlHistoryArray
);
65 WX_DEFINE_OBJARRAY(wxHtmlHistoryArray
);
67 WX_DECLARE_LIST(wxHtmlProcessor
, wxHtmlProcessorList
);
68 WX_DEFINE_LIST(wxHtmlProcessorList
);
70 //-----------------------------------------------------------------------------
72 //-----------------------------------------------------------------------------
75 wxHtmlWindow::wxHtmlWindow(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
76 long style
, const wxString
& name
) : wxScrolledWindow(parent
, id
, pos
, size
, style
| wxVSCROLL
| wxHSCROLL
, name
)
78 m_tmpMouseMoved
= FALSE
;
81 m_tmpCanDrawLocks
= 0;
82 m_FS
= new wxFileSystem();
83 m_RelatedStatusBar
= -1;
84 m_RelatedFrame
= NULL
;
85 m_TitleFormat
= wxT("%s");
86 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
88 m_Parser
= new wxHtmlWinParser(this);
89 m_Parser
->SetFS(m_FS
);
93 m_History
= new wxHtmlHistoryArray
;
96 SetPage(wxT("<html><body></body></html>"));
101 wxHtmlWindow::~wxHtmlWindow()
105 if (m_Cell
) delete m_Cell
;
115 void wxHtmlWindow::SetRelatedFrame(wxFrame
* frame
, const wxString
& format
)
117 m_RelatedFrame
= frame
;
118 m_TitleFormat
= format
;
123 void wxHtmlWindow::SetRelatedStatusBar(int bar
)
125 m_RelatedStatusBar
= bar
;
130 void wxHtmlWindow::SetFonts(wxString normal_face
, wxString fixed_face
, const int *sizes
)
132 wxString op
= m_OpenedPage
;
134 m_Parser
->SetFonts(normal_face
, fixed_face
, sizes
);
135 // fonts changed => contents invalid, so reload the page:
136 SetPage(wxT("<html><body></body></html>"));
137 if (!op
.IsEmpty()) LoadPage(op
);
142 bool wxHtmlWindow::SetPage(const wxString
& source
)
144 wxString
newsrc(source
);
146 // pass HTML through registered processors:
147 if (m_Processors
|| m_GlobalProcessors
)
149 wxHtmlProcessorList::Node
*nodeL
, *nodeG
;
152 nodeL
= (m_Processors
) ? m_Processors
->GetFirst() : NULL
;
153 nodeG
= (m_GlobalProcessors
) ? m_GlobalProcessors
->GetFirst() : NULL
;
155 // VS: there are two lists, global and local, both of them sorted by
156 // priority. Since we have to go through _both_ lists with
157 // decreasing priority, we "merge-sort" the lists on-line by
158 // processing that one of the two heads that has higher priority
159 // in every iteration
160 while (nodeL
|| nodeG
)
162 prL
= (nodeL
) ? nodeL
->GetData()->GetPriority() : -1;
163 prG
= (nodeG
) ? nodeG
->GetData()->GetPriority() : -1;
166 if (nodeL
->GetData()->IsEnabled())
167 newsrc
= nodeL
->GetData()->Process(newsrc
);
168 nodeL
= nodeL
->GetNext();
172 if (nodeG
->GetData()->IsEnabled())
173 newsrc
= nodeG
->GetData()->Process(newsrc
);
174 nodeG
= nodeG
->GetNext();
179 // ...and run the parser on it:
180 wxClientDC
*dc
= new wxClientDC(this);
181 dc
->SetMapMode(wxMM_TEXT
);
182 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
183 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
190 m_Cell
= (wxHtmlContainerCell
*) m_Parser
->Parse(newsrc
);
192 m_Cell
->SetIndent(m_Borders
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
193 m_Cell
->SetAlignHor(wxHTML_ALIGN_CENTER
);
195 if (m_tmpCanDrawLocks
== 0)
200 bool wxHtmlWindow::AppendToPage(const wxString
& source
)
202 return SetPage(*(GetParser()->GetSource()) + source
);
205 bool wxHtmlWindow::LoadPage(const wxString
& location
)
209 bool needs_refresh
= FALSE
;
211 SetCursor(*wxHOURGLASS_CURSOR
);
212 wxYield(); Refresh(FALSE
);
215 if (m_HistoryOn
&& (m_HistoryPos
!= -1))
217 // store scroll position into history item:
219 GetViewStart(&x
, &y
);
220 (*m_History
)[m_HistoryPos
].SetPos(y
);
223 if (location
[0] == wxT('#'))
226 wxString anch
= location
.Mid(1) /*1 to end*/;
228 rt_val
= ScrollToAnchor(anch
);
231 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&& location
.BeforeFirst(wxT('#')) == m_OpenedPage
)
233 wxString anch
= location
.AfterFirst(wxT('#'));
235 rt_val
= ScrollToAnchor(anch
);
238 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&&
239 (m_FS
->GetPath() + location
.BeforeFirst(wxT('#'))) == m_OpenedPage
)
241 wxString anch
= location
.AfterFirst(wxT('#'));
243 rt_val
= ScrollToAnchor(anch
);
249 needs_refresh
= TRUE
;
251 if (m_RelatedStatusBar
!= -1)
253 m_RelatedFrame
->SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
257 f
= m_FS
->OpenFile(location
);
261 wxLogError(_("Unable to open requested HTML document: %s"), location
.c_str());
264 SetCursor(*wxSTANDARD_CURSOR
);
271 wxString src
= wxEmptyString
;
273 if (m_RelatedStatusBar
!= -1)
275 wxString msg
= _("Loading : ") + location
;
276 m_RelatedFrame
->SetStatusText(msg
, m_RelatedStatusBar
);
280 node
= m_Filters
.GetFirst();
283 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
286 src
= h
->ReadFile(*f
);
289 node
= node
->GetNext();
291 if (src
== wxEmptyString
)
293 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
294 src
= m_DefaultFilter
->ReadFile(*f
);
297 m_FS
->ChangePathTo(f
->GetLocation());
298 rt_val
= SetPage(src
);
299 m_OpenedPage
= f
->GetLocation();
300 if (f
->GetAnchor() != wxEmptyString
)
303 ScrollToAnchor(f
->GetAnchor());
308 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
->SetStatusText(_("Done"), m_RelatedStatusBar
);
312 if (m_HistoryOn
) // add this page to history there:
314 int c
= m_History
->GetCount() - (m_HistoryPos
+ 1);
316 if (m_HistoryPos
< 0 ||
317 (*m_History
)[m_HistoryPos
].GetPage() != m_OpenedPage
||
318 (*m_History
)[m_HistoryPos
].GetAnchor() != m_OpenedAnchor
)
321 for (int i
= 0; i
< c
; i
++)
322 m_History
->RemoveAt(m_HistoryPos
);
323 m_History
->Add(new wxHtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
327 if (m_OpenedPageTitle
== wxEmptyString
)
328 OnSetTitle(wxFileNameFromPath(m_OpenedPage
));
329 SetCursor(*wxSTANDARD_CURSOR
);
345 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
347 const wxHtmlCell
*c
= m_Cell
->Find(wxHTML_COND_ISANCHOR
, &anchor
);
350 wxLogWarning(_("HTML anchor %s does not exist."), anchor
.c_str());
357 for (y
= 0; c
!= NULL
; c
= c
->GetParent()) y
+= c
->GetPosY();
358 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
359 m_OpenedAnchor
= anchor
;
365 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
370 tit
.Printf(m_TitleFormat
, title
.c_str());
371 m_RelatedFrame
->SetTitle(tit
);
373 m_OpenedPageTitle
= title
;
380 void wxHtmlWindow::CreateLayout()
382 int ClientWidth
, ClientHeight
;
386 if (m_Style
& wxHW_SCROLLBAR_NEVER
)
388 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // always off
389 GetClientSize(&ClientWidth
, &ClientHeight
);
390 m_Cell
->Layout(ClientWidth
);
394 GetClientSize(&ClientWidth
, &ClientHeight
);
395 m_Cell
->Layout(ClientWidth
);
396 if (ClientHeight
< m_Cell
->GetHeight() + GetCharHeight())
399 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
400 m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
,
401 (m_Cell
->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
402 /*cheat: top-level frag is always container*/);
404 else /* we fit into window, no need for scrollbars */
406 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
407 GetClientSize(&ClientWidth
, &ClientHeight
);
408 m_Cell
->Layout(ClientWidth
); // ...and relayout
415 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
420 wxString p_fff
, p_ffn
;
422 if (path
!= wxEmptyString
)
424 oldpath
= cfg
->GetPath();
428 m_Borders
= cfg
->Read(wxT("wxHtmlWindow/Borders"), m_Borders
);
429 p_fff
= cfg
->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
430 p_ffn
= cfg
->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
431 for (int i
= 0; i
< 7; i
++)
433 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
434 p_fontsizes
[i
] = cfg
->Read(tmp
, m_Parser
->m_FontsSizes
[i
]);
436 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
438 if (path
!= wxEmptyString
)
439 cfg
->SetPath(oldpath
);
444 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
449 if (path
!= wxEmptyString
)
451 oldpath
= cfg
->GetPath();
455 cfg
->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders
);
456 cfg
->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
457 cfg
->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
458 for (int i
= 0; i
< 7; i
++)
460 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
461 cfg
->Write(tmp
, (long) m_Parser
->m_FontsSizes
[i
]);
464 if (path
!= wxEmptyString
)
465 cfg
->SetPath(oldpath
);
470 bool wxHtmlWindow::HistoryBack()
474 if (m_HistoryPos
< 1) return FALSE
;
476 // store scroll position into history item:
478 GetViewStart(&x
, &y
);
479 (*m_History
)[m_HistoryPos
].SetPos(y
);
481 // go to previous position:
484 l
= (*m_History
)[m_HistoryPos
].GetPage();
485 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
488 if (a
== wxEmptyString
) LoadPage(l
);
489 else LoadPage(l
+ wxT("#") + a
);
493 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
498 bool wxHtmlWindow::HistoryCanBack()
500 if (m_HistoryPos
< 1) return FALSE
;
505 bool wxHtmlWindow::HistoryForward()
509 if (m_HistoryPos
== -1) return FALSE
;
510 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
512 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
515 l
= (*m_History
)[m_HistoryPos
].GetPage();
516 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
519 if (a
== wxEmptyString
) LoadPage(l
);
520 else LoadPage(l
+ wxT("#") + a
);
524 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
529 bool wxHtmlWindow::HistoryCanForward()
531 if (m_HistoryPos
== -1) return FALSE
;
532 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
537 void wxHtmlWindow::HistoryClear()
543 void wxHtmlWindow::AddProcessor(wxHtmlProcessor
*processor
)
547 m_Processors
= new wxHtmlProcessorList
;
548 m_Processors
->DeleteContents(TRUE
);
550 wxHtmlProcessorList::Node
*node
;
552 for (node
= m_Processors
->GetFirst(); node
; node
= node
->GetNext())
554 if (processor
->GetPriority() > node
->GetData()->GetPriority())
556 m_Processors
->Insert(node
, processor
);
560 m_Processors
->Append(processor
);
563 /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor
*processor
)
565 if (!m_GlobalProcessors
)
567 m_GlobalProcessors
= new wxHtmlProcessorList
;
568 m_GlobalProcessors
->DeleteContents(TRUE
);
570 wxHtmlProcessorList::Node
*node
;
572 for (node
= m_GlobalProcessors
->GetFirst(); node
; node
= node
->GetNext())
574 if (processor
->GetPriority() > node
->GetData()->GetPriority())
576 m_GlobalProcessors
->Insert(node
, processor
);
580 m_GlobalProcessors
->Append(processor
);
585 wxList
wxHtmlWindow::m_Filters
;
586 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
587 wxCursor
*wxHtmlWindow::s_cur_hand
= NULL
;
588 wxCursor
*wxHtmlWindow::s_cur_arrow
= NULL
;
589 wxHtmlProcessorList
*wxHtmlWindow::m_GlobalProcessors
= NULL
;
591 void wxHtmlWindow::CleanUpStatics()
593 delete m_DefaultFilter
;
594 m_DefaultFilter
= NULL
;
595 m_Filters
.DeleteContents(TRUE
);
597 delete m_GlobalProcessors
;
598 m_GlobalProcessors
= NULL
;
605 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
607 m_Filters
.Append(filter
);
613 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
615 LoadPage(link
.GetHref());
618 void wxHtmlWindow::OnCellClicked(wxHtmlCell
*cell
,
619 wxCoord x
, wxCoord y
,
620 const wxMouseEvent
& event
)
622 wxCHECK_RET( cell
, _T("can't be called with NULL cell") );
624 cell
->OnMouseClick(this, x
, y
, event
);
627 void wxHtmlWindow::OnCellMouseHover(wxHtmlCell
* WXUNUSED(cell
),
628 wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
633 void wxHtmlWindow::OnDraw(wxDC
& dc
)
635 if (m_tmpCanDrawLocks
> 0 || m_Cell
== NULL
) return;
638 wxRect rect
= GetUpdateRegion().GetBox();
640 dc
.SetMapMode(wxMM_TEXT
);
641 dc
.SetBackgroundMode(wxTRANSPARENT
);
642 GetViewStart(&x
, &y
);
644 m_Cell
->Draw(dc
, 0, 0,
645 y
* wxHTML_SCROLL_STEP
+ rect
.GetTop(),
646 y
* wxHTML_SCROLL_STEP
+ rect
.GetBottom());
652 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
654 wxScrolledWindow::OnSize(event
);
660 void wxHtmlWindow::OnMouseEvent(wxMouseEvent
& event
)
662 m_tmpMouseMoved
= TRUE
;
664 if (event
.ButtonDown())
669 GetViewStart(&sx
, &sy
);
670 sx
*= wxHTML_SCROLL_STEP
;
671 sy
*= wxHTML_SCROLL_STEP
;
673 wxPoint pos
= event
.GetPosition();
677 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
679 // VZ: is it possible that we don't find anything at all?
680 // VS: yes. FindCellByPos returns terminal cell and
681 // containers may have empty borders
683 OnCellClicked(cell
, pos
.x
, pos
.y
, event
);
690 void wxHtmlWindow::OnIdle(wxIdleEvent
& WXUNUSED(event
))
692 if (s_cur_hand
== NULL
)
694 s_cur_hand
= new wxCursor(wxCURSOR_HAND
);
695 s_cur_arrow
= new wxCursor(wxCURSOR_ARROW
);
698 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
))
701 GetViewStart(&sx
, &sy
);
702 sx
*= wxHTML_SCROLL_STEP
;
703 sy
*= wxHTML_SCROLL_STEP
;
706 wxGetMousePosition(&x
, &y
);
707 ScreenToClient(&x
, &y
);
711 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(x
, y
);
712 if ( cell
!= m_tmpLastCell
)
714 wxHtmlLinkInfo
*lnk
= cell
? cell
->GetLink(x
, y
) : NULL
;
716 if (lnk
!= m_tmpLastLink
)
720 SetCursor(*s_cur_arrow
);
721 if (m_RelatedStatusBar
!= -1)
722 m_RelatedFrame
->SetStatusText(wxEmptyString
, m_RelatedStatusBar
);
726 SetCursor(*s_cur_hand
);
727 if (m_RelatedStatusBar
!= -1)
728 m_RelatedFrame
->SetStatusText(lnk
->GetHref(), m_RelatedStatusBar
);
733 m_tmpLastCell
= cell
;
735 else // mouse moved but stayed in the same cell
738 OnCellMouseHover(cell
, x
, y
);
741 m_tmpMouseMoved
= FALSE
;
746 IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor
,wxObject
)
748 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
750 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
751 EVT_SIZE(wxHtmlWindow::OnSize
)
752 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent
)
753 EVT_RIGHT_DOWN(wxHtmlWindow::OnMouseEvent
)
754 EVT_MOTION(wxHtmlWindow::OnMouseEvent
)
755 EVT_IDLE(wxHtmlWindow::OnIdle
)
762 // A module to allow initialization/cleanup
763 // without calling these functions from app.cpp or from
764 // the user's application.
766 class wxHtmlWinModule
: public wxModule
768 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
770 wxHtmlWinModule() : wxModule() {}
771 bool OnInit() { return TRUE
; }
772 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
775 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
778 // This hack forces the linker to always link in m_* files
779 // (wxHTML doesn't work without handlers from these files)
780 #include "wx/html/forcelnk.h"
781 FORCE_WXHTML_MODULES()