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
)
217 wxBusyCursor busyCursor
;
221 bool needs_refresh
= FALSE
;
224 if (m_HistoryOn
&& (m_HistoryPos
!= -1))
226 // store scroll position into history item:
228 GetViewStart(&x
, &y
);
229 (*m_History
)[m_HistoryPos
].SetPos(y
);
232 if (location
[0] == wxT('#'))
235 wxString anch
= location
.Mid(1) /*1 to end*/;
237 rt_val
= ScrollToAnchor(anch
);
240 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&& location
.BeforeFirst(wxT('#')) == m_OpenedPage
)
242 wxString anch
= location
.AfterFirst(wxT('#'));
244 rt_val
= ScrollToAnchor(anch
);
247 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&&
248 (m_FS
->GetPath() + location
.BeforeFirst(wxT('#'))) == m_OpenedPage
)
250 wxString anch
= location
.AfterFirst(wxT('#'));
252 rt_val
= ScrollToAnchor(anch
);
258 needs_refresh
= TRUE
;
260 if (m_RelatedStatusBar
!= -1)
262 m_RelatedFrame
->SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
266 f
= m_FS
->OpenFile(location
);
270 wxLogError(_("Unable to open requested HTML document: %s"), location
.c_str());
278 wxString src
= wxEmptyString
;
280 if (m_RelatedStatusBar
!= -1)
282 wxString msg
= _("Loading : ") + location
;
283 m_RelatedFrame
->SetStatusText(msg
, m_RelatedStatusBar
);
287 node
= m_Filters
.GetFirst();
290 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
293 src
= h
->ReadFile(*f
);
296 node
= node
->GetNext();
298 if (src
== wxEmptyString
)
300 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
301 src
= m_DefaultFilter
->ReadFile(*f
);
304 m_FS
->ChangePathTo(f
->GetLocation());
305 rt_val
= SetPage(src
);
306 m_OpenedPage
= f
->GetLocation();
307 if (f
->GetAnchor() != wxEmptyString
)
310 ScrollToAnchor(f
->GetAnchor());
315 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
->SetStatusText(_("Done"), m_RelatedStatusBar
);
319 if (m_HistoryOn
) // add this page to history there:
321 int c
= m_History
->GetCount() - (m_HistoryPos
+ 1);
323 if (m_HistoryPos
< 0 ||
324 (*m_History
)[m_HistoryPos
].GetPage() != m_OpenedPage
||
325 (*m_History
)[m_HistoryPos
].GetAnchor() != m_OpenedAnchor
)
328 for (int i
= 0; i
< c
; i
++)
329 m_History
->RemoveAt(m_HistoryPos
);
330 m_History
->Add(new wxHtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
334 if (m_OpenedPageTitle
== wxEmptyString
)
335 OnSetTitle(wxFileNameFromPath(m_OpenedPage
));
351 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
353 const wxHtmlCell
*c
= m_Cell
->Find(wxHTML_COND_ISANCHOR
, &anchor
);
356 wxLogWarning(_("HTML anchor %s does not exist."), anchor
.c_str());
363 for (y
= 0; c
!= NULL
; c
= c
->GetParent()) y
+= c
->GetPosY();
364 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
365 m_OpenedAnchor
= anchor
;
371 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
376 tit
.Printf(m_TitleFormat
, title
.c_str());
377 m_RelatedFrame
->SetTitle(tit
);
379 m_OpenedPageTitle
= title
;
386 void wxHtmlWindow::CreateLayout()
388 int ClientWidth
, ClientHeight
;
392 if (m_Style
& wxHW_SCROLLBAR_NEVER
)
394 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // always off
395 GetClientSize(&ClientWidth
, &ClientHeight
);
396 m_Cell
->Layout(ClientWidth
);
400 GetClientSize(&ClientWidth
, &ClientHeight
);
401 m_Cell
->Layout(ClientWidth
);
402 if (ClientHeight
< m_Cell
->GetHeight() + GetCharHeight())
405 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
406 m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
,
407 (m_Cell
->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
408 /*cheat: top-level frag is always container*/);
410 else /* we fit into window, no need for scrollbars */
412 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
413 GetClientSize(&ClientWidth
, &ClientHeight
);
414 m_Cell
->Layout(ClientWidth
); // ...and relayout
421 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
426 wxString p_fff
, p_ffn
;
428 if (path
!= wxEmptyString
)
430 oldpath
= cfg
->GetPath();
434 m_Borders
= cfg
->Read(wxT("wxHtmlWindow/Borders"), m_Borders
);
435 p_fff
= cfg
->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
436 p_ffn
= cfg
->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
437 for (int i
= 0; i
< 7; i
++)
439 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
440 p_fontsizes
[i
] = cfg
->Read(tmp
, m_Parser
->m_FontsSizes
[i
]);
442 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
444 if (path
!= wxEmptyString
)
445 cfg
->SetPath(oldpath
);
450 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
455 if (path
!= wxEmptyString
)
457 oldpath
= cfg
->GetPath();
461 cfg
->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders
);
462 cfg
->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
463 cfg
->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
464 for (int i
= 0; i
< 7; i
++)
466 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
467 cfg
->Write(tmp
, (long) m_Parser
->m_FontsSizes
[i
]);
470 if (path
!= wxEmptyString
)
471 cfg
->SetPath(oldpath
);
476 bool wxHtmlWindow::HistoryBack()
480 if (m_HistoryPos
< 1) return FALSE
;
482 // store scroll position into history item:
484 GetViewStart(&x
, &y
);
485 (*m_History
)[m_HistoryPos
].SetPos(y
);
487 // go to previous position:
490 l
= (*m_History
)[m_HistoryPos
].GetPage();
491 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
494 if (a
== wxEmptyString
) LoadPage(l
);
495 else LoadPage(l
+ wxT("#") + a
);
499 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
504 bool wxHtmlWindow::HistoryCanBack()
506 if (m_HistoryPos
< 1) return FALSE
;
511 bool wxHtmlWindow::HistoryForward()
515 if (m_HistoryPos
== -1) return FALSE
;
516 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
518 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
521 l
= (*m_History
)[m_HistoryPos
].GetPage();
522 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
525 if (a
== wxEmptyString
) LoadPage(l
);
526 else LoadPage(l
+ wxT("#") + a
);
530 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
535 bool wxHtmlWindow::HistoryCanForward()
537 if (m_HistoryPos
== -1) return FALSE
;
538 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
543 void wxHtmlWindow::HistoryClear()
549 void wxHtmlWindow::AddProcessor(wxHtmlProcessor
*processor
)
553 m_Processors
= new wxHtmlProcessorList
;
554 m_Processors
->DeleteContents(TRUE
);
556 wxHtmlProcessorList::Node
*node
;
558 for (node
= m_Processors
->GetFirst(); node
; node
= node
->GetNext())
560 if (processor
->GetPriority() > node
->GetData()->GetPriority())
562 m_Processors
->Insert(node
, processor
);
566 m_Processors
->Append(processor
);
569 /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor
*processor
)
571 if (!m_GlobalProcessors
)
573 m_GlobalProcessors
= new wxHtmlProcessorList
;
574 m_GlobalProcessors
->DeleteContents(TRUE
);
576 wxHtmlProcessorList::Node
*node
;
578 for (node
= m_GlobalProcessors
->GetFirst(); node
; node
= node
->GetNext())
580 if (processor
->GetPriority() > node
->GetData()->GetPriority())
582 m_GlobalProcessors
->Insert(node
, processor
);
586 m_GlobalProcessors
->Append(processor
);
591 wxList
wxHtmlWindow::m_Filters
;
592 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
593 wxCursor
*wxHtmlWindow::s_cur_hand
= NULL
;
594 wxCursor
*wxHtmlWindow::s_cur_arrow
= NULL
;
595 wxHtmlProcessorList
*wxHtmlWindow::m_GlobalProcessors
= NULL
;
597 void wxHtmlWindow::CleanUpStatics()
599 delete m_DefaultFilter
;
600 m_DefaultFilter
= NULL
;
601 m_Filters
.DeleteContents(TRUE
);
603 delete m_GlobalProcessors
;
604 m_GlobalProcessors
= NULL
;
611 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
613 m_Filters
.Append(filter
);
619 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
621 LoadPage(link
.GetHref());
624 void wxHtmlWindow::OnCellClicked(wxHtmlCell
*cell
,
625 wxCoord x
, wxCoord y
,
626 const wxMouseEvent
& event
)
628 wxCHECK_RET( cell
, _T("can't be called with NULL cell") );
630 cell
->OnMouseClick(this, x
, y
, event
);
633 void wxHtmlWindow::OnCellMouseHover(wxHtmlCell
* WXUNUSED(cell
),
634 wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
639 void wxHtmlWindow::OnDraw(wxDC
& dc
)
641 if (m_tmpCanDrawLocks
> 0 || m_Cell
== NULL
) return;
644 wxRect rect
= GetUpdateRegion().GetBox();
646 dc
.SetMapMode(wxMM_TEXT
);
647 dc
.SetBackgroundMode(wxTRANSPARENT
);
648 GetViewStart(&x
, &y
);
650 m_Cell
->Draw(dc
, 0, 0,
651 y
* wxHTML_SCROLL_STEP
+ rect
.GetTop(),
652 y
* wxHTML_SCROLL_STEP
+ rect
.GetBottom());
658 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
660 wxScrolledWindow::OnSize(event
);
666 void wxHtmlWindow::OnMouseEvent(wxMouseEvent
& event
)
668 m_tmpMouseMoved
= TRUE
;
670 if (event
.ButtonDown())
675 GetViewStart(&sx
, &sy
);
676 sx
*= wxHTML_SCROLL_STEP
;
677 sy
*= wxHTML_SCROLL_STEP
;
679 wxPoint pos
= event
.GetPosition();
683 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
685 // VZ: is it possible that we don't find anything at all?
686 // VS: yes. FindCellByPos returns terminal cell and
687 // containers may have empty borders
689 OnCellClicked(cell
, pos
.x
, pos
.y
, event
);
696 void wxHtmlWindow::OnIdle(wxIdleEvent
& WXUNUSED(event
))
698 if (s_cur_hand
== NULL
)
700 s_cur_hand
= new wxCursor(wxCURSOR_HAND
);
701 s_cur_arrow
= new wxCursor(wxCURSOR_ARROW
);
704 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
))
707 GetViewStart(&sx
, &sy
);
708 sx
*= wxHTML_SCROLL_STEP
;
709 sy
*= wxHTML_SCROLL_STEP
;
712 wxGetMousePosition(&x
, &y
);
713 ScreenToClient(&x
, &y
);
717 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(x
, y
);
718 if ( cell
!= m_tmpLastCell
)
720 wxHtmlLinkInfo
*lnk
= cell
? cell
->GetLink(x
, y
) : NULL
;
722 if (lnk
!= m_tmpLastLink
)
726 SetCursor(*s_cur_arrow
);
727 if (m_RelatedStatusBar
!= -1)
728 m_RelatedFrame
->SetStatusText(wxEmptyString
, m_RelatedStatusBar
);
732 SetCursor(*s_cur_hand
);
733 if (m_RelatedStatusBar
!= -1)
734 m_RelatedFrame
->SetStatusText(lnk
->GetHref(), m_RelatedStatusBar
);
739 m_tmpLastCell
= cell
;
741 else // mouse moved but stayed in the same cell
744 OnCellMouseHover(cell
, x
, y
);
747 m_tmpMouseMoved
= FALSE
;
752 IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor
,wxObject
)
754 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
756 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
757 EVT_SIZE(wxHtmlWindow::OnSize
)
758 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent
)
759 EVT_RIGHT_DOWN(wxHtmlWindow::OnMouseEvent
)
760 EVT_MOTION(wxHtmlWindow::OnMouseEvent
)
761 EVT_IDLE(wxHtmlWindow::OnIdle
)
768 // A module to allow initialization/cleanup
769 // without calling these functions from app.cpp or from
770 // the user's application.
772 class wxHtmlWinModule
: public wxModule
774 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
776 wxHtmlWinModule() : wxModule() {}
777 bool OnInit() { return TRUE
; }
778 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
781 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
784 // This hack forces the linker to always link in m_* files
785 // (wxHTML doesn't work without handlers from these files)
786 #include "wx/html/forcelnk.h"
787 FORCE_WXHTML_MODULES()