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 void wxHtmlWindow::Init()
77 m_tmpMouseMoved
= FALSE
;
80 m_tmpCanDrawLocks
= 0;
81 m_FS
= new wxFileSystem();
82 m_RelatedStatusBar
= -1;
83 m_RelatedFrame
= NULL
;
84 m_TitleFormat
= wxT("%s");
85 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
87 m_Parser
= new wxHtmlWinParser(this);
88 m_Parser
->SetFS(m_FS
);
91 m_History
= new wxHtmlHistoryArray
;
97 bool wxHtmlWindow::Create(wxWindow
*parent
, wxWindowID id
,
98 const wxPoint
& pos
, const wxSize
& size
,
99 long style
, const wxString
& name
)
101 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
102 style
| wxVSCROLL
| wxHSCROLL
, name
))
106 SetPage(wxT("<html><body></body></html>"));
111 wxHtmlWindow::~wxHtmlWindow()
115 if (m_Cell
) delete m_Cell
;
125 void wxHtmlWindow::SetRelatedFrame(wxFrame
* frame
, const wxString
& format
)
127 m_RelatedFrame
= frame
;
128 m_TitleFormat
= format
;
133 void wxHtmlWindow::SetRelatedStatusBar(int bar
)
135 m_RelatedStatusBar
= bar
;
140 void wxHtmlWindow::SetFonts(wxString normal_face
, wxString fixed_face
, const int *sizes
)
142 wxString op
= m_OpenedPage
;
144 m_Parser
->SetFonts(normal_face
, fixed_face
, sizes
);
145 // fonts changed => contents invalid, so reload the page:
146 SetPage(wxT("<html><body></body></html>"));
147 if (!op
.IsEmpty()) LoadPage(op
);
152 bool wxHtmlWindow::SetPage(const wxString
& source
)
154 wxString
newsrc(source
);
156 // pass HTML through registered processors:
157 if (m_Processors
|| m_GlobalProcessors
)
159 wxHtmlProcessorList::Node
*nodeL
, *nodeG
;
162 nodeL
= (m_Processors
) ? m_Processors
->GetFirst() : NULL
;
163 nodeG
= (m_GlobalProcessors
) ? m_GlobalProcessors
->GetFirst() : NULL
;
165 // VS: there are two lists, global and local, both of them sorted by
166 // priority. Since we have to go through _both_ lists with
167 // decreasing priority, we "merge-sort" the lists on-line by
168 // processing that one of the two heads that has higher priority
169 // in every iteration
170 while (nodeL
|| nodeG
)
172 prL
= (nodeL
) ? nodeL
->GetData()->GetPriority() : -1;
173 prG
= (nodeG
) ? nodeG
->GetData()->GetPriority() : -1;
176 if (nodeL
->GetData()->IsEnabled())
177 newsrc
= nodeL
->GetData()->Process(newsrc
);
178 nodeL
= nodeL
->GetNext();
182 if (nodeG
->GetData()->IsEnabled())
183 newsrc
= nodeG
->GetData()->Process(newsrc
);
184 nodeG
= nodeG
->GetNext();
189 // ...and run the parser on it:
190 wxClientDC
*dc
= new wxClientDC(this);
191 dc
->SetMapMode(wxMM_TEXT
);
192 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
193 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
200 m_Cell
= (wxHtmlContainerCell
*) m_Parser
->Parse(newsrc
);
202 m_Cell
->SetIndent(m_Borders
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
203 m_Cell
->SetAlignHor(wxHTML_ALIGN_CENTER
);
205 if (m_tmpCanDrawLocks
== 0)
210 bool wxHtmlWindow::AppendToPage(const wxString
& source
)
212 return SetPage(*(GetParser()->GetSource()) + source
);
215 bool wxHtmlWindow::LoadPage(const wxString
& location
)
219 bool needs_refresh
= FALSE
;
221 SetCursor(*wxHOURGLASS_CURSOR
);
222 wxYield(); Refresh(FALSE
);
225 if (m_HistoryOn
&& (m_HistoryPos
!= -1))
227 // store scroll position into history item:
229 GetViewStart(&x
, &y
);
230 (*m_History
)[m_HistoryPos
].SetPos(y
);
233 if (location
[0] == wxT('#'))
236 wxString anch
= location
.Mid(1) /*1 to end*/;
238 rt_val
= ScrollToAnchor(anch
);
241 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&& location
.BeforeFirst(wxT('#')) == m_OpenedPage
)
243 wxString anch
= location
.AfterFirst(wxT('#'));
245 rt_val
= ScrollToAnchor(anch
);
248 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&&
249 (m_FS
->GetPath() + location
.BeforeFirst(wxT('#'))) == m_OpenedPage
)
251 wxString anch
= location
.AfterFirst(wxT('#'));
253 rt_val
= ScrollToAnchor(anch
);
259 needs_refresh
= TRUE
;
261 if (m_RelatedStatusBar
!= -1)
263 m_RelatedFrame
->SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
267 f
= m_FS
->OpenFile(location
);
271 wxLogError(_("Unable to open requested HTML document: %s"), location
.c_str());
274 SetCursor(*wxSTANDARD_CURSOR
);
281 wxString src
= wxEmptyString
;
283 if (m_RelatedStatusBar
!= -1)
285 wxString msg
= _("Loading : ") + location
;
286 m_RelatedFrame
->SetStatusText(msg
, m_RelatedStatusBar
);
290 node
= m_Filters
.GetFirst();
293 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
296 src
= h
->ReadFile(*f
);
299 node
= node
->GetNext();
301 if (src
== wxEmptyString
)
303 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
304 src
= m_DefaultFilter
->ReadFile(*f
);
307 m_FS
->ChangePathTo(f
->GetLocation());
308 rt_val
= SetPage(src
);
309 m_OpenedPage
= f
->GetLocation();
310 if (f
->GetAnchor() != wxEmptyString
)
313 ScrollToAnchor(f
->GetAnchor());
318 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
->SetStatusText(_("Done"), m_RelatedStatusBar
);
322 if (m_HistoryOn
) // add this page to history there:
324 int c
= m_History
->GetCount() - (m_HistoryPos
+ 1);
326 if (m_HistoryPos
< 0 ||
327 (*m_History
)[m_HistoryPos
].GetPage() != m_OpenedPage
||
328 (*m_History
)[m_HistoryPos
].GetAnchor() != m_OpenedAnchor
)
331 for (int i
= 0; i
< c
; i
++)
332 m_History
->RemoveAt(m_HistoryPos
);
333 m_History
->Add(new wxHtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
337 if (m_OpenedPageTitle
== wxEmptyString
)
338 OnSetTitle(wxFileNameFromPath(m_OpenedPage
));
339 SetCursor(*wxSTANDARD_CURSOR
);
355 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
357 const wxHtmlCell
*c
= m_Cell
->Find(wxHTML_COND_ISANCHOR
, &anchor
);
360 wxLogWarning(_("HTML anchor %s does not exist."), anchor
.c_str());
367 for (y
= 0; c
!= NULL
; c
= c
->GetParent()) y
+= c
->GetPosY();
368 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
369 m_OpenedAnchor
= anchor
;
375 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
380 tit
.Printf(m_TitleFormat
, title
.c_str());
381 m_RelatedFrame
->SetTitle(tit
);
383 m_OpenedPageTitle
= title
;
390 void wxHtmlWindow::CreateLayout()
392 int ClientWidth
, ClientHeight
;
396 if (m_Style
& wxHW_SCROLLBAR_NEVER
)
398 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // always off
399 GetClientSize(&ClientWidth
, &ClientHeight
);
400 m_Cell
->Layout(ClientWidth
);
404 GetClientSize(&ClientWidth
, &ClientHeight
);
405 m_Cell
->Layout(ClientWidth
);
406 if (ClientHeight
< m_Cell
->GetHeight() + GetCharHeight())
409 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
410 m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
,
411 (m_Cell
->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
412 /*cheat: top-level frag is always container*/);
414 else /* we fit into window, no need for scrollbars */
416 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
417 GetClientSize(&ClientWidth
, &ClientHeight
);
418 m_Cell
->Layout(ClientWidth
); // ...and relayout
425 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
430 wxString p_fff
, p_ffn
;
432 if (path
!= wxEmptyString
)
434 oldpath
= cfg
->GetPath();
438 m_Borders
= cfg
->Read(wxT("wxHtmlWindow/Borders"), m_Borders
);
439 p_fff
= cfg
->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
440 p_ffn
= cfg
->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
441 for (int i
= 0; i
< 7; i
++)
443 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
444 p_fontsizes
[i
] = cfg
->Read(tmp
, m_Parser
->m_FontsSizes
[i
]);
446 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
448 if (path
!= wxEmptyString
)
449 cfg
->SetPath(oldpath
);
454 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
459 if (path
!= wxEmptyString
)
461 oldpath
= cfg
->GetPath();
465 cfg
->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders
);
466 cfg
->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
467 cfg
->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
468 for (int i
= 0; i
< 7; i
++)
470 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
471 cfg
->Write(tmp
, (long) m_Parser
->m_FontsSizes
[i
]);
474 if (path
!= wxEmptyString
)
475 cfg
->SetPath(oldpath
);
480 bool wxHtmlWindow::HistoryBack()
484 if (m_HistoryPos
< 1) return FALSE
;
486 // store scroll position into history item:
488 GetViewStart(&x
, &y
);
489 (*m_History
)[m_HistoryPos
].SetPos(y
);
491 // go to previous position:
494 l
= (*m_History
)[m_HistoryPos
].GetPage();
495 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
498 if (a
== wxEmptyString
) LoadPage(l
);
499 else LoadPage(l
+ wxT("#") + a
);
503 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
508 bool wxHtmlWindow::HistoryCanBack()
510 if (m_HistoryPos
< 1) return FALSE
;
515 bool wxHtmlWindow::HistoryForward()
519 if (m_HistoryPos
== -1) return FALSE
;
520 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
522 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
525 l
= (*m_History
)[m_HistoryPos
].GetPage();
526 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
529 if (a
== wxEmptyString
) LoadPage(l
);
530 else LoadPage(l
+ wxT("#") + a
);
534 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
539 bool wxHtmlWindow::HistoryCanForward()
541 if (m_HistoryPos
== -1) return FALSE
;
542 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
547 void wxHtmlWindow::HistoryClear()
553 void wxHtmlWindow::AddProcessor(wxHtmlProcessor
*processor
)
557 m_Processors
= new wxHtmlProcessorList
;
558 m_Processors
->DeleteContents(TRUE
);
560 wxHtmlProcessorList::Node
*node
;
562 for (node
= m_Processors
->GetFirst(); node
; node
= node
->GetNext())
564 if (processor
->GetPriority() > node
->GetData()->GetPriority())
566 m_Processors
->Insert(node
, processor
);
570 m_Processors
->Append(processor
);
573 /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor
*processor
)
575 if (!m_GlobalProcessors
)
577 m_GlobalProcessors
= new wxHtmlProcessorList
;
578 m_GlobalProcessors
->DeleteContents(TRUE
);
580 wxHtmlProcessorList::Node
*node
;
582 for (node
= m_GlobalProcessors
->GetFirst(); node
; node
= node
->GetNext())
584 if (processor
->GetPriority() > node
->GetData()->GetPriority())
586 m_GlobalProcessors
->Insert(node
, processor
);
590 m_GlobalProcessors
->Append(processor
);
595 wxList
wxHtmlWindow::m_Filters
;
596 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
597 wxCursor
*wxHtmlWindow::s_cur_hand
= NULL
;
598 wxCursor
*wxHtmlWindow::s_cur_arrow
= NULL
;
599 wxHtmlProcessorList
*wxHtmlWindow::m_GlobalProcessors
= NULL
;
601 void wxHtmlWindow::CleanUpStatics()
603 delete m_DefaultFilter
;
604 m_DefaultFilter
= NULL
;
605 m_Filters
.DeleteContents(TRUE
);
607 delete m_GlobalProcessors
;
608 m_GlobalProcessors
= NULL
;
615 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
617 m_Filters
.Append(filter
);
623 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
625 LoadPage(link
.GetHref());
628 void wxHtmlWindow::OnCellClicked(wxHtmlCell
*cell
,
629 wxCoord x
, wxCoord y
,
630 const wxMouseEvent
& event
)
632 wxCHECK_RET( cell
, _T("can't be called with NULL cell") );
634 cell
->OnMouseClick(this, x
, y
, event
);
637 void wxHtmlWindow::OnCellMouseHover(wxHtmlCell
* WXUNUSED(cell
),
638 wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
643 void wxHtmlWindow::OnDraw(wxDC
& dc
)
645 if (m_tmpCanDrawLocks
> 0 || m_Cell
== NULL
) return;
648 wxRect rect
= GetUpdateRegion().GetBox();
650 dc
.SetMapMode(wxMM_TEXT
);
651 dc
.SetBackgroundMode(wxTRANSPARENT
);
652 GetViewStart(&x
, &y
);
654 m_Cell
->Draw(dc
, 0, 0,
655 y
* wxHTML_SCROLL_STEP
+ rect
.GetTop(),
656 y
* wxHTML_SCROLL_STEP
+ rect
.GetBottom());
662 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
664 wxScrolledWindow::OnSize(event
);
670 void wxHtmlWindow::OnMouseEvent(wxMouseEvent
& event
)
672 m_tmpMouseMoved
= TRUE
;
674 if (event
.ButtonDown())
679 GetViewStart(&sx
, &sy
);
680 sx
*= wxHTML_SCROLL_STEP
;
681 sy
*= wxHTML_SCROLL_STEP
;
683 wxPoint pos
= event
.GetPosition();
687 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
689 // VZ: is it possible that we don't find anything at all?
690 // VS: yes. FindCellByPos returns terminal cell and
691 // containers may have empty borders
693 OnCellClicked(cell
, pos
.x
, pos
.y
, event
);
700 void wxHtmlWindow::OnIdle(wxIdleEvent
& WXUNUSED(event
))
702 if (s_cur_hand
== NULL
)
704 s_cur_hand
= new wxCursor(wxCURSOR_HAND
);
705 s_cur_arrow
= new wxCursor(wxCURSOR_ARROW
);
708 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
))
711 GetViewStart(&sx
, &sy
);
712 sx
*= wxHTML_SCROLL_STEP
;
713 sy
*= wxHTML_SCROLL_STEP
;
716 wxGetMousePosition(&x
, &y
);
717 ScreenToClient(&x
, &y
);
721 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(x
, y
);
722 if ( cell
!= m_tmpLastCell
)
724 wxHtmlLinkInfo
*lnk
= cell
? cell
->GetLink(x
, y
) : NULL
;
726 if (lnk
!= m_tmpLastLink
)
730 SetCursor(*s_cur_arrow
);
731 if (m_RelatedStatusBar
!= -1)
732 m_RelatedFrame
->SetStatusText(wxEmptyString
, m_RelatedStatusBar
);
736 SetCursor(*s_cur_hand
);
737 if (m_RelatedStatusBar
!= -1)
738 m_RelatedFrame
->SetStatusText(lnk
->GetHref(), m_RelatedStatusBar
);
743 m_tmpLastCell
= cell
;
745 else // mouse moved but stayed in the same cell
748 OnCellMouseHover(cell
, x
, y
);
751 m_tmpMouseMoved
= FALSE
;
756 IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor
,wxObject
)
758 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
760 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
761 EVT_SIZE(wxHtmlWindow::OnSize
)
762 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent
)
763 EVT_RIGHT_DOWN(wxHtmlWindow::OnMouseEvent
)
764 EVT_MOTION(wxHtmlWindow::OnMouseEvent
)
765 EVT_IDLE(wxHtmlWindow::OnIdle
)
772 // A module to allow initialization/cleanup
773 // without calling these functions from app.cpp or from
774 // the user's application.
776 class wxHtmlWinModule
: public wxModule
778 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
780 wxHtmlWinModule() : wxModule() {}
781 bool OnInit() { return TRUE
; }
782 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
785 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
788 // This hack forces the linker to always link in m_* files
789 // (wxHTML doesn't work without handlers from these files)
790 #include "wx/html/forcelnk.h"
791 FORCE_WXHTML_MODULES()