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 if ( !m_Parser
->CanOpenURL(location
) )
268 wxLogError(_("Access denied to document '%s'!"), location
.c_str());
272 f
= m_FS
->OpenFile(location
);
276 wxLogError(_("Unable to open requested HTML document: %s"), location
.c_str());
284 wxString src
= wxEmptyString
;
286 if (m_RelatedStatusBar
!= -1)
288 wxString msg
= _("Loading : ") + location
;
289 m_RelatedFrame
->SetStatusText(msg
, m_RelatedStatusBar
);
293 node
= m_Filters
.GetFirst();
296 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
299 src
= h
->ReadFile(*f
);
302 node
= node
->GetNext();
304 if (src
== wxEmptyString
)
306 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
307 src
= m_DefaultFilter
->ReadFile(*f
);
310 m_FS
->ChangePathTo(f
->GetLocation());
311 rt_val
= SetPage(src
);
312 m_OpenedPage
= f
->GetLocation();
313 if (f
->GetAnchor() != wxEmptyString
)
315 ScrollToAnchor(f
->GetAnchor());
320 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
->SetStatusText(_("Done"), m_RelatedStatusBar
);
324 if (m_HistoryOn
) // add this page to history there:
326 int c
= m_History
->GetCount() - (m_HistoryPos
+ 1);
328 if (m_HistoryPos
< 0 ||
329 (*m_History
)[m_HistoryPos
].GetPage() != m_OpenedPage
||
330 (*m_History
)[m_HistoryPos
].GetAnchor() != m_OpenedAnchor
)
333 for (int i
= 0; i
< c
; i
++)
334 m_History
->RemoveAt(m_HistoryPos
);
335 m_History
->Add(new wxHtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
339 if (m_OpenedPageTitle
== wxEmptyString
)
340 OnSetTitle(wxFileNameFromPath(m_OpenedPage
));
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
);
502 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
507 bool wxHtmlWindow::HistoryCanBack()
509 if (m_HistoryPos
< 1) return FALSE
;
514 bool wxHtmlWindow::HistoryForward()
518 if (m_HistoryPos
== -1) return FALSE
;
519 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
521 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
524 l
= (*m_History
)[m_HistoryPos
].GetPage();
525 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
528 if (a
== wxEmptyString
) LoadPage(l
);
529 else LoadPage(l
+ wxT("#") + a
);
532 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
537 bool wxHtmlWindow::HistoryCanForward()
539 if (m_HistoryPos
== -1) return FALSE
;
540 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
545 void wxHtmlWindow::HistoryClear()
551 void wxHtmlWindow::AddProcessor(wxHtmlProcessor
*processor
)
555 m_Processors
= new wxHtmlProcessorList
;
556 m_Processors
->DeleteContents(TRUE
);
558 wxHtmlProcessorList::Node
*node
;
560 for (node
= m_Processors
->GetFirst(); node
; node
= node
->GetNext())
562 if (processor
->GetPriority() > node
->GetData()->GetPriority())
564 m_Processors
->Insert(node
, processor
);
568 m_Processors
->Append(processor
);
571 /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor
*processor
)
573 if (!m_GlobalProcessors
)
575 m_GlobalProcessors
= new wxHtmlProcessorList
;
576 m_GlobalProcessors
->DeleteContents(TRUE
);
578 wxHtmlProcessorList::Node
*node
;
580 for (node
= m_GlobalProcessors
->GetFirst(); node
; node
= node
->GetNext())
582 if (processor
->GetPriority() > node
->GetData()->GetPriority())
584 m_GlobalProcessors
->Insert(node
, processor
);
588 m_GlobalProcessors
->Append(processor
);
593 wxList
wxHtmlWindow::m_Filters
;
594 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
595 wxCursor
*wxHtmlWindow::s_cur_hand
= NULL
;
596 wxCursor
*wxHtmlWindow::s_cur_arrow
= NULL
;
597 wxHtmlProcessorList
*wxHtmlWindow::m_GlobalProcessors
= NULL
;
599 void wxHtmlWindow::CleanUpStatics()
601 delete m_DefaultFilter
;
602 m_DefaultFilter
= NULL
;
603 m_Filters
.DeleteContents(TRUE
);
605 delete m_GlobalProcessors
;
606 m_GlobalProcessors
= NULL
;
613 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
615 m_Filters
.Append(filter
);
621 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
623 LoadPage(link
.GetHref());
626 void wxHtmlWindow::OnCellClicked(wxHtmlCell
*cell
,
627 wxCoord x
, wxCoord y
,
628 const wxMouseEvent
& event
)
630 wxCHECK_RET( cell
, _T("can't be called with NULL cell") );
632 cell
->OnMouseClick(this, x
, y
, event
);
635 void wxHtmlWindow::OnCellMouseHover(wxHtmlCell
* WXUNUSED(cell
),
636 wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
641 void wxHtmlWindow::OnDraw(wxDC
& dc
)
643 if (m_tmpCanDrawLocks
> 0 || m_Cell
== NULL
) return;
646 wxRect rect
= GetUpdateRegion().GetBox();
648 dc
.SetMapMode(wxMM_TEXT
);
649 dc
.SetBackgroundMode(wxTRANSPARENT
);
650 GetViewStart(&x
, &y
);
652 m_Cell
->Draw(dc
, 0, 0,
653 y
* wxHTML_SCROLL_STEP
+ rect
.GetTop(),
654 y
* wxHTML_SCROLL_STEP
+ rect
.GetBottom());
660 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
662 wxScrolledWindow::OnSize(event
);
668 void wxHtmlWindow::OnMouseEvent(wxMouseEvent
& event
)
670 m_tmpMouseMoved
= TRUE
;
672 if (event
.ButtonDown())
677 GetViewStart(&sx
, &sy
);
678 sx
*= wxHTML_SCROLL_STEP
;
679 sy
*= wxHTML_SCROLL_STEP
;
681 wxPoint pos
= event
.GetPosition();
685 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
687 // VZ: is it possible that we don't find anything at all?
688 // VS: yes. FindCellByPos returns terminal cell and
689 // containers may have empty borders
691 OnCellClicked(cell
, pos
.x
, pos
.y
, event
);
698 void wxHtmlWindow::OnIdle(wxIdleEvent
& WXUNUSED(event
))
700 if (s_cur_hand
== NULL
)
702 s_cur_hand
= new wxCursor(wxCURSOR_HAND
);
703 s_cur_arrow
= new wxCursor(wxCURSOR_ARROW
);
706 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
))
709 GetViewStart(&sx
, &sy
);
710 sx
*= wxHTML_SCROLL_STEP
;
711 sy
*= wxHTML_SCROLL_STEP
;
714 wxGetMousePosition(&x
, &y
);
715 ScreenToClient(&x
, &y
);
719 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(x
, y
);
720 if ( cell
!= m_tmpLastCell
)
722 wxHtmlLinkInfo
*lnk
= cell
? cell
->GetLink(x
, y
) : NULL
;
724 if (lnk
!= m_tmpLastLink
)
728 SetCursor(*s_cur_arrow
);
729 if (m_RelatedStatusBar
!= -1)
730 m_RelatedFrame
->SetStatusText(wxEmptyString
, m_RelatedStatusBar
);
734 SetCursor(*s_cur_hand
);
735 if (m_RelatedStatusBar
!= -1)
736 m_RelatedFrame
->SetStatusText(lnk
->GetHref(), m_RelatedStatusBar
);
741 m_tmpLastCell
= cell
;
743 else // mouse moved but stayed in the same cell
746 OnCellMouseHover(cell
, x
, y
);
749 m_tmpMouseMoved
= FALSE
;
754 IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor
,wxObject
)
756 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
758 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
759 EVT_SIZE(wxHtmlWindow::OnSize
)
760 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent
)
761 EVT_RIGHT_DOWN(wxHtmlWindow::OnMouseEvent
)
762 EVT_MOTION(wxHtmlWindow::OnMouseEvent
)
763 EVT_IDLE(wxHtmlWindow::OnIdle
)
770 // A module to allow initialization/cleanup
771 // without calling these functions from app.cpp or from
772 // the user's application.
774 class wxHtmlWinModule
: public wxModule
776 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
778 wxHtmlWinModule() : wxModule() {}
779 bool OnInit() { return TRUE
; }
780 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
783 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
786 // This hack forces the linker to always link in m_* files
787 // (wxHTML doesn't work without handlers from these files)
788 #include "wx/html/forcelnk.h"
789 FORCE_WXHTML_MODULES()