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)
201 bool wxHtmlWindow::LoadPage(const wxString
& location
)
205 bool needs_refresh
= FALSE
;
207 SetCursor(*wxHOURGLASS_CURSOR
);
208 wxYield(); Refresh(FALSE
);
211 if (m_HistoryOn
&& (m_HistoryPos
!= -1))
213 // store scroll position into history item:
215 GetViewStart(&x
, &y
);
216 (*m_History
)[m_HistoryPos
].SetPos(y
);
219 if (location
[0] == wxT('#'))
222 wxString anch
= location
.Mid(1) /*1 to end*/;
224 rt_val
= ScrollToAnchor(anch
);
227 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&& location
.BeforeFirst(wxT('#')) == m_OpenedPage
)
229 wxString anch
= location
.AfterFirst(wxT('#'));
231 rt_val
= ScrollToAnchor(anch
);
234 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&&
235 (m_FS
->GetPath() + location
.BeforeFirst(wxT('#'))) == m_OpenedPage
)
237 wxString anch
= location
.AfterFirst(wxT('#'));
239 rt_val
= ScrollToAnchor(anch
);
245 needs_refresh
= TRUE
;
247 if (m_RelatedStatusBar
!= -1)
249 m_RelatedFrame
->SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
253 f
= m_FS
->OpenFile(location
);
257 wxLogError(_("Unable to open requested HTML document: %s"), location
.c_str());
260 SetCursor(*wxSTANDARD_CURSOR
);
267 wxString src
= wxEmptyString
;
269 if (m_RelatedStatusBar
!= -1)
271 wxString msg
= _("Loading : ") + location
;
272 m_RelatedFrame
->SetStatusText(msg
, m_RelatedStatusBar
);
276 node
= m_Filters
.GetFirst();
279 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
282 src
= h
->ReadFile(*f
);
285 node
= node
->GetNext();
287 if (src
== wxEmptyString
)
289 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
290 src
= m_DefaultFilter
->ReadFile(*f
);
293 m_FS
->ChangePathTo(f
->GetLocation());
294 rt_val
= SetPage(src
);
295 m_OpenedPage
= f
->GetLocation();
296 if (f
->GetAnchor() != wxEmptyString
)
299 ScrollToAnchor(f
->GetAnchor());
304 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
->SetStatusText(_("Done"), m_RelatedStatusBar
);
308 if (m_HistoryOn
) // add this page to history there:
310 int c
= m_History
->GetCount() - (m_HistoryPos
+ 1);
312 if (m_HistoryPos
< 0 ||
313 (*m_History
)[m_HistoryPos
].GetPage() != m_OpenedPage
||
314 (*m_History
)[m_HistoryPos
].GetAnchor() != m_OpenedAnchor
)
317 for (int i
= 0; i
< c
; i
++)
318 m_History
->RemoveAt(m_HistoryPos
);
319 m_History
->Add(new wxHtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
323 if (m_OpenedPageTitle
== wxEmptyString
)
324 OnSetTitle(wxFileNameFromPath(m_OpenedPage
));
325 SetCursor(*wxSTANDARD_CURSOR
);
341 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
343 const wxHtmlCell
*c
= m_Cell
->Find(wxHTML_COND_ISANCHOR
, &anchor
);
346 wxLogWarning(_("HTML anchor %s does not exist."), anchor
.c_str());
353 for (y
= 0; c
!= NULL
; c
= c
->GetParent()) y
+= c
->GetPosY();
354 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
355 m_OpenedAnchor
= anchor
;
361 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
366 tit
.Printf(m_TitleFormat
, title
.c_str());
367 m_RelatedFrame
->SetTitle(tit
);
369 m_OpenedPageTitle
= title
;
376 void wxHtmlWindow::CreateLayout()
378 int ClientWidth
, ClientHeight
;
382 if (m_Style
& wxHW_SCROLLBAR_NEVER
)
384 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // always off
385 GetClientSize(&ClientWidth
, &ClientHeight
);
386 m_Cell
->Layout(ClientWidth
);
390 GetClientSize(&ClientWidth
, &ClientHeight
);
391 m_Cell
->Layout(ClientWidth
);
392 if (ClientHeight
< m_Cell
->GetHeight() + GetCharHeight())
395 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
396 m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
,
397 (m_Cell
->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
398 /*cheat: top-level frag is always container*/);
400 else /* we fit into window, no need for scrollbars */
402 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
403 GetClientSize(&ClientWidth
, &ClientHeight
);
404 m_Cell
->Layout(ClientWidth
); // ...and relayout
411 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
416 wxString p_fff
, p_ffn
;
418 if (path
!= wxEmptyString
)
420 oldpath
= cfg
->GetPath();
424 m_Borders
= cfg
->Read(wxT("wxHtmlWindow/Borders"), m_Borders
);
425 p_fff
= cfg
->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
426 p_ffn
= cfg
->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
427 for (int i
= 0; i
< 7; i
++)
429 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
430 p_fontsizes
[i
] = cfg
->Read(tmp
, m_Parser
->m_FontsSizes
[i
]);
432 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
434 if (path
!= wxEmptyString
)
435 cfg
->SetPath(oldpath
);
440 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
445 if (path
!= wxEmptyString
)
447 oldpath
= cfg
->GetPath();
451 cfg
->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders
);
452 cfg
->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
453 cfg
->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
454 for (int i
= 0; i
< 7; i
++)
456 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
457 cfg
->Write(tmp
, (long) m_Parser
->m_FontsSizes
[i
]);
460 if (path
!= wxEmptyString
)
461 cfg
->SetPath(oldpath
);
466 bool wxHtmlWindow::HistoryBack()
470 if (m_HistoryPos
< 1) return FALSE
;
472 // store scroll position into history item:
474 GetViewStart(&x
, &y
);
475 (*m_History
)[m_HistoryPos
].SetPos(y
);
477 // go to previous position:
480 l
= (*m_History
)[m_HistoryPos
].GetPage();
481 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
484 if (a
== wxEmptyString
) LoadPage(l
);
485 else LoadPage(l
+ wxT("#") + a
);
489 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
494 bool wxHtmlWindow::HistoryCanBack()
496 if (m_HistoryPos
< 1) return FALSE
;
501 bool wxHtmlWindow::HistoryForward()
505 if (m_HistoryPos
== -1) return FALSE
;
506 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
508 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
511 l
= (*m_History
)[m_HistoryPos
].GetPage();
512 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
515 if (a
== wxEmptyString
) LoadPage(l
);
516 else LoadPage(l
+ wxT("#") + a
);
520 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
525 bool wxHtmlWindow::HistoryCanForward()
527 if (m_HistoryPos
== -1) return FALSE
;
528 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
533 void wxHtmlWindow::HistoryClear()
539 void wxHtmlWindow::AddProcessor(wxHtmlProcessor
*processor
)
543 m_Processors
= new wxHtmlProcessorList
;
544 m_Processors
->DeleteContents(TRUE
);
546 wxHtmlProcessorList::Node
*node
;
548 for (node
= m_Processors
->GetFirst(); node
; node
= node
->GetNext())
550 if (processor
->GetPriority() > node
->GetData()->GetPriority())
552 m_Processors
->Insert(node
, processor
);
556 m_Processors
->Append(processor
);
559 /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor
*processor
)
561 if (!m_GlobalProcessors
)
563 m_GlobalProcessors
= new wxHtmlProcessorList
;
564 m_GlobalProcessors
->DeleteContents(TRUE
);
566 wxHtmlProcessorList::Node
*node
;
568 for (node
= m_GlobalProcessors
->GetFirst(); node
; node
= node
->GetNext())
570 if (processor
->GetPriority() > node
->GetData()->GetPriority())
572 m_GlobalProcessors
->Insert(node
, processor
);
576 m_GlobalProcessors
->Append(processor
);
581 wxList
wxHtmlWindow::m_Filters
;
582 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
583 wxCursor
*wxHtmlWindow::s_cur_hand
= NULL
;
584 wxCursor
*wxHtmlWindow::s_cur_arrow
= NULL
;
585 wxHtmlProcessorList
*wxHtmlWindow::m_GlobalProcessors
= NULL
;
587 void wxHtmlWindow::CleanUpStatics()
589 delete m_DefaultFilter
;
590 m_DefaultFilter
= NULL
;
591 m_Filters
.DeleteContents(TRUE
);
593 delete m_GlobalProcessors
;
594 m_GlobalProcessors
= NULL
;
601 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
603 m_Filters
.Append(filter
);
609 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
611 LoadPage(link
.GetHref());
614 void wxHtmlWindow::OnCellClicked(wxHtmlCell
*cell
,
615 wxCoord x
, wxCoord y
,
616 const wxMouseEvent
& event
)
618 wxCHECK_RET( cell
, _T("can't be called with NULL cell") );
620 cell
->OnMouseClick(this, x
, y
, event
);
623 void wxHtmlWindow::OnCellMouseHover(wxHtmlCell
* WXUNUSED(cell
),
624 wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
629 void wxHtmlWindow::OnDraw(wxDC
& dc
)
632 wxRegionIterator
upd(GetUpdateRegion()); // get the update rect list
635 if (m_tmpCanDrawLocks
> 0) return;
637 dc
.SetMapMode(wxMM_TEXT
);
638 dc
.SetBackgroundMode(wxTRANSPARENT
);
639 GetViewStart(&x
, &y
);
645 if (m_Cell
) m_Cell
->Draw(dc
, 0, 0, y
* wxHTML_SCROLL_STEP
+ v_y
, y
* wxHTML_SCROLL_STEP
+ v_h
+ v_y
);
653 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
655 wxScrolledWindow::OnSize(event
);
661 void wxHtmlWindow::OnMouseEvent(wxMouseEvent
& event
)
663 m_tmpMouseMoved
= TRUE
;
665 if (event
.ButtonDown())
670 GetViewStart(&sx
, &sy
);
671 sx
*= wxHTML_SCROLL_STEP
;
672 sy
*= wxHTML_SCROLL_STEP
;
674 wxPoint pos
= event
.GetPosition();
678 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
680 // VZ: is it possible that we don't find anything at all?
681 // VS: yes. FindCellByPos returns terminal cell and
682 // containers may have empty borders
684 OnCellClicked(cell
, pos
.x
, pos
.y
, event
);
691 void wxHtmlWindow::OnIdle(wxIdleEvent
& WXUNUSED(event
))
693 if (s_cur_hand
== NULL
)
695 s_cur_hand
= new wxCursor(wxCURSOR_HAND
);
696 s_cur_arrow
= new wxCursor(wxCURSOR_ARROW
);
699 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
))
702 GetViewStart(&sx
, &sy
);
703 sx
*= wxHTML_SCROLL_STEP
;
704 sy
*= wxHTML_SCROLL_STEP
;
707 wxGetMousePosition(&x
, &y
);
708 ScreenToClient(&x
, &y
);
712 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(x
, y
);
713 if ( cell
!= m_tmpLastCell
)
715 wxHtmlLinkInfo
*lnk
= cell
? cell
->GetLink(x
, y
) : NULL
;
717 if (lnk
!= m_tmpLastLink
)
721 SetCursor(*s_cur_arrow
);
722 if (m_RelatedStatusBar
!= -1)
723 m_RelatedFrame
->SetStatusText(wxEmptyString
, m_RelatedStatusBar
);
727 SetCursor(*s_cur_hand
);
728 if (m_RelatedStatusBar
!= -1)
729 m_RelatedFrame
->SetStatusText(lnk
->GetHref(), m_RelatedStatusBar
);
734 m_tmpLastCell
= cell
;
736 else // mouse moved but stayed in the same cell
739 OnCellMouseHover(cell
, x
, y
);
742 m_tmpMouseMoved
= FALSE
;
747 IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor
,wxObject
)
749 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
751 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
752 EVT_SIZE(wxHtmlWindow::OnSize
)
753 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent
)
754 EVT_RIGHT_DOWN(wxHtmlWindow::OnMouseEvent
)
755 EVT_MOTION(wxHtmlWindow::OnMouseEvent
)
756 EVT_IDLE(wxHtmlWindow::OnIdle
)
763 // A module to allow initialization/cleanup
764 // without calling these functions from app.cpp or from
765 // the user's application.
767 class wxHtmlWinModule
: public wxModule
769 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
771 wxHtmlWinModule() : wxModule() {}
772 bool OnInit() { return TRUE
; }
773 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
776 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
779 // This hack forces the linker to always link in m_* files
780 // (wxHTML doesn't work without handlers from these files)
781 #include "wx/html/forcelnk.h"
782 FORCE_WXHTML_MODULES()