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
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_Parser
->OpenURL(wxHTML_URL_PAGE
, location
);
268 // try to interpret 'location' as filename instead of URL:
271 wxFileName
fn(location
);
272 wxString location2
= wxFileSystem::FileNameToURL(fn
);
273 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location2
);
278 wxLogError(_("Unable to open requested HTML document: %s"), location
.c_str());
286 wxString src
= wxEmptyString
;
288 if (m_RelatedStatusBar
!= -1)
290 wxString msg
= _("Loading : ") + location
;
291 m_RelatedFrame
->SetStatusText(msg
, m_RelatedStatusBar
);
295 node
= m_Filters
.GetFirst();
298 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
301 src
= h
->ReadFile(*f
);
304 node
= node
->GetNext();
306 if (src
== wxEmptyString
)
308 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
309 src
= m_DefaultFilter
->ReadFile(*f
);
312 m_FS
->ChangePathTo(f
->GetLocation());
313 rt_val
= SetPage(src
);
314 m_OpenedPage
= f
->GetLocation();
315 if (f
->GetAnchor() != wxEmptyString
)
317 ScrollToAnchor(f
->GetAnchor());
322 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
->SetStatusText(_("Done"), m_RelatedStatusBar
);
326 if (m_HistoryOn
) // add this page to history there:
328 int c
= m_History
->GetCount() - (m_HistoryPos
+ 1);
330 if (m_HistoryPos
< 0 ||
331 (*m_History
)[m_HistoryPos
].GetPage() != m_OpenedPage
||
332 (*m_History
)[m_HistoryPos
].GetAnchor() != m_OpenedAnchor
)
335 for (int i
= 0; i
< c
; i
++)
336 m_History
->RemoveAt(m_HistoryPos
);
337 m_History
->Add(new wxHtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
341 if (m_OpenedPageTitle
== wxEmptyString
)
342 OnSetTitle(wxFileNameFromPath(m_OpenedPage
));
356 bool wxHtmlWindow::LoadFile(const wxFileName
& filename
)
358 wxString url
= wxFileSystem::FileNameToURL(filename
);
359 return LoadPage(url
);
363 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
365 const wxHtmlCell
*c
= m_Cell
->Find(wxHTML_COND_ISANCHOR
, &anchor
);
368 wxLogWarning(_("HTML anchor %s does not exist."), anchor
.c_str());
375 for (y
= 0; c
!= NULL
; c
= c
->GetParent()) y
+= c
->GetPosY();
376 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
377 m_OpenedAnchor
= anchor
;
383 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
388 tit
.Printf(m_TitleFormat
, title
.c_str());
389 m_RelatedFrame
->SetTitle(tit
);
391 m_OpenedPageTitle
= title
;
398 void wxHtmlWindow::CreateLayout()
400 int ClientWidth
, ClientHeight
;
404 if (m_Style
& wxHW_SCROLLBAR_NEVER
)
406 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // always off
407 GetClientSize(&ClientWidth
, &ClientHeight
);
408 m_Cell
->Layout(ClientWidth
);
412 GetClientSize(&ClientWidth
, &ClientHeight
);
413 m_Cell
->Layout(ClientWidth
);
414 if (ClientHeight
< m_Cell
->GetHeight() + GetCharHeight())
417 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
418 m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
,
419 (m_Cell
->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
420 /*cheat: top-level frag is always container*/);
422 else /* we fit into window, no need for scrollbars */
424 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
425 GetClientSize(&ClientWidth
, &ClientHeight
);
426 m_Cell
->Layout(ClientWidth
); // ...and relayout
433 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
438 wxString p_fff
, p_ffn
;
440 if (path
!= wxEmptyString
)
442 oldpath
= cfg
->GetPath();
446 m_Borders
= cfg
->Read(wxT("wxHtmlWindow/Borders"), m_Borders
);
447 p_fff
= cfg
->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
448 p_ffn
= cfg
->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
449 for (int i
= 0; i
< 7; i
++)
451 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
452 p_fontsizes
[i
] = cfg
->Read(tmp
, m_Parser
->m_FontsSizes
[i
]);
454 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
456 if (path
!= wxEmptyString
)
457 cfg
->SetPath(oldpath
);
462 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
467 if (path
!= wxEmptyString
)
469 oldpath
= cfg
->GetPath();
473 cfg
->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders
);
474 cfg
->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
475 cfg
->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
476 for (int i
= 0; i
< 7; i
++)
478 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
479 cfg
->Write(tmp
, (long) m_Parser
->m_FontsSizes
[i
]);
482 if (path
!= wxEmptyString
)
483 cfg
->SetPath(oldpath
);
488 bool wxHtmlWindow::HistoryBack()
492 if (m_HistoryPos
< 1) return FALSE
;
494 // store scroll position into history item:
496 GetViewStart(&x
, &y
);
497 (*m_History
)[m_HistoryPos
].SetPos(y
);
499 // go to previous position:
502 l
= (*m_History
)[m_HistoryPos
].GetPage();
503 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
506 if (a
== wxEmptyString
) LoadPage(l
);
507 else LoadPage(l
+ wxT("#") + a
);
510 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
515 bool wxHtmlWindow::HistoryCanBack()
517 if (m_HistoryPos
< 1) return FALSE
;
522 bool wxHtmlWindow::HistoryForward()
526 if (m_HistoryPos
== -1) return FALSE
;
527 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
529 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
532 l
= (*m_History
)[m_HistoryPos
].GetPage();
533 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
536 if (a
== wxEmptyString
) LoadPage(l
);
537 else LoadPage(l
+ wxT("#") + a
);
540 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
545 bool wxHtmlWindow::HistoryCanForward()
547 if (m_HistoryPos
== -1) return FALSE
;
548 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
553 void wxHtmlWindow::HistoryClear()
559 void wxHtmlWindow::AddProcessor(wxHtmlProcessor
*processor
)
563 m_Processors
= new wxHtmlProcessorList
;
564 m_Processors
->DeleteContents(TRUE
);
566 wxHtmlProcessorList::Node
*node
;
568 for (node
= m_Processors
->GetFirst(); node
; node
= node
->GetNext())
570 if (processor
->GetPriority() > node
->GetData()->GetPriority())
572 m_Processors
->Insert(node
, processor
);
576 m_Processors
->Append(processor
);
579 /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor
*processor
)
581 if (!m_GlobalProcessors
)
583 m_GlobalProcessors
= new wxHtmlProcessorList
;
584 m_GlobalProcessors
->DeleteContents(TRUE
);
586 wxHtmlProcessorList::Node
*node
;
588 for (node
= m_GlobalProcessors
->GetFirst(); node
; node
= node
->GetNext())
590 if (processor
->GetPriority() > node
->GetData()->GetPriority())
592 m_GlobalProcessors
->Insert(node
, processor
);
596 m_GlobalProcessors
->Append(processor
);
601 wxList
wxHtmlWindow::m_Filters
;
602 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
603 wxCursor
*wxHtmlWindow::s_cur_hand
= NULL
;
604 wxCursor
*wxHtmlWindow::s_cur_arrow
= NULL
;
605 wxHtmlProcessorList
*wxHtmlWindow::m_GlobalProcessors
= NULL
;
607 void wxHtmlWindow::CleanUpStatics()
609 wxDELETE(m_DefaultFilter
);
610 m_Filters
.DeleteContents(TRUE
);
612 wxDELETE(m_GlobalProcessors
);
613 wxDELETE(s_cur_hand
);
614 wxDELETE(s_cur_arrow
);
619 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
621 m_Filters
.Append(filter
);
627 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
629 LoadPage(link
.GetHref());
632 void wxHtmlWindow::OnCellClicked(wxHtmlCell
*cell
,
633 wxCoord x
, wxCoord y
,
634 const wxMouseEvent
& event
)
636 wxCHECK_RET( cell
, _T("can't be called with NULL cell") );
638 cell
->OnMouseClick(this, x
, y
, event
);
641 void wxHtmlWindow::OnCellMouseHover(wxHtmlCell
* WXUNUSED(cell
),
642 wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
647 void wxHtmlWindow::OnDraw(wxDC
& dc
)
649 if (m_tmpCanDrawLocks
> 0 || m_Cell
== NULL
) return;
652 wxRect rect
= GetUpdateRegion().GetBox();
654 dc
.SetMapMode(wxMM_TEXT
);
655 dc
.SetBackgroundMode(wxTRANSPARENT
);
656 GetViewStart(&x
, &y
);
658 m_Cell
->Draw(dc
, 0, 0,
659 y
* wxHTML_SCROLL_STEP
+ rect
.GetTop(),
660 y
* wxHTML_SCROLL_STEP
+ rect
.GetBottom());
666 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
668 wxScrolledWindow::OnSize(event
);
674 void wxHtmlWindow::OnMouseEvent(wxMouseEvent
& event
)
676 m_tmpMouseMoved
= TRUE
;
678 if (event
.ButtonDown())
684 GetViewStart(&sx
, &sy
);
685 sx
*= wxHTML_SCROLL_STEP
;
686 sy
*= wxHTML_SCROLL_STEP
;
688 wxPoint pos
= event
.GetPosition();
692 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
694 // VZ: is it possible that we don't find anything at all?
695 // VS: yes. FindCellByPos returns terminal cell and
696 // containers may have empty borders
698 OnCellClicked(cell
, pos
.x
, pos
.y
, event
);
705 void wxHtmlWindow::OnIdle(wxIdleEvent
& WXUNUSED(event
))
707 if (s_cur_hand
== NULL
)
709 s_cur_hand
= new wxCursor(wxCURSOR_HAND
);
710 s_cur_arrow
= new wxCursor(wxCURSOR_ARROW
);
713 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
))
716 GetViewStart(&sx
, &sy
);
717 sx
*= wxHTML_SCROLL_STEP
;
718 sy
*= wxHTML_SCROLL_STEP
;
721 wxGetMousePosition(&x
, &y
);
722 ScreenToClient(&x
, &y
);
726 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(x
, y
);
727 if ( cell
!= m_tmpLastCell
)
729 wxHtmlLinkInfo
*lnk
= cell
? cell
->GetLink(x
, y
) : NULL
;
731 if (lnk
!= m_tmpLastLink
)
735 SetCursor(*s_cur_arrow
);
736 if (m_RelatedStatusBar
!= -1)
737 m_RelatedFrame
->SetStatusText(wxEmptyString
, m_RelatedStatusBar
);
741 SetCursor(*s_cur_hand
);
742 if (m_RelatedStatusBar
!= -1)
743 m_RelatedFrame
->SetStatusText(lnk
->GetHref(), m_RelatedStatusBar
);
748 m_tmpLastCell
= cell
;
750 else // mouse moved but stayed in the same cell
753 OnCellMouseHover(cell
, x
, y
);
756 m_tmpMouseMoved
= FALSE
;
761 IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor
,wxObject
)
763 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
765 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
766 EVT_SIZE(wxHtmlWindow::OnSize
)
767 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent
)
768 EVT_RIGHT_DOWN(wxHtmlWindow::OnMouseEvent
)
769 EVT_MOTION(wxHtmlWindow::OnMouseEvent
)
770 EVT_IDLE(wxHtmlWindow::OnIdle
)
777 // A module to allow initialization/cleanup
778 // without calling these functions from app.cpp or from
779 // the user's application.
781 class wxHtmlWinModule
: public wxModule
783 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
785 wxHtmlWinModule() : wxModule() {}
786 bool OnInit() { return TRUE
; }
787 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
790 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
793 // This hack forces the linker to always link in m_* files
794 // (wxHTML doesn't work without handlers from these files)
795 #include "wx/html/forcelnk.h"
796 FORCE_WXHTML_MODULES()