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
);
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
)
309 ScrollToAnchor(f
->GetAnchor());
314 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
->SetStatusText(_("Done"), m_RelatedStatusBar
);
318 if (m_HistoryOn
) // add this page to history there:
320 int c
= m_History
->GetCount() - (m_HistoryPos
+ 1);
322 if (m_HistoryPos
< 0 ||
323 (*m_History
)[m_HistoryPos
].GetPage() != m_OpenedPage
||
324 (*m_History
)[m_HistoryPos
].GetAnchor() != m_OpenedAnchor
)
327 for (int i
= 0; i
< c
; i
++)
328 m_History
->RemoveAt(m_HistoryPos
);
329 m_History
->Add(new wxHtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
333 if (m_OpenedPageTitle
== wxEmptyString
)
334 OnSetTitle(wxFileNameFromPath(m_OpenedPage
));
349 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
351 const wxHtmlCell
*c
= m_Cell
->Find(wxHTML_COND_ISANCHOR
, &anchor
);
354 wxLogWarning(_("HTML anchor %s does not exist."), anchor
.c_str());
361 for (y
= 0; c
!= NULL
; c
= c
->GetParent()) y
+= c
->GetPosY();
362 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
363 m_OpenedAnchor
= anchor
;
369 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
374 tit
.Printf(m_TitleFormat
, title
.c_str());
375 m_RelatedFrame
->SetTitle(tit
);
377 m_OpenedPageTitle
= title
;
384 void wxHtmlWindow::CreateLayout()
386 int ClientWidth
, ClientHeight
;
390 if (m_Style
& wxHW_SCROLLBAR_NEVER
)
392 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // always off
393 GetClientSize(&ClientWidth
, &ClientHeight
);
394 m_Cell
->Layout(ClientWidth
);
398 GetClientSize(&ClientWidth
, &ClientHeight
);
399 m_Cell
->Layout(ClientWidth
);
400 if (ClientHeight
< m_Cell
->GetHeight() + GetCharHeight())
403 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
404 m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
,
405 (m_Cell
->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
406 /*cheat: top-level frag is always container*/);
408 else /* we fit into window, no need for scrollbars */
410 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
411 GetClientSize(&ClientWidth
, &ClientHeight
);
412 m_Cell
->Layout(ClientWidth
); // ...and relayout
419 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
424 wxString p_fff
, p_ffn
;
426 if (path
!= wxEmptyString
)
428 oldpath
= cfg
->GetPath();
432 m_Borders
= cfg
->Read(wxT("wxHtmlWindow/Borders"), m_Borders
);
433 p_fff
= cfg
->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
434 p_ffn
= cfg
->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
435 for (int i
= 0; i
< 7; i
++)
437 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
438 p_fontsizes
[i
] = cfg
->Read(tmp
, m_Parser
->m_FontsSizes
[i
]);
440 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
442 if (path
!= wxEmptyString
)
443 cfg
->SetPath(oldpath
);
448 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
453 if (path
!= wxEmptyString
)
455 oldpath
= cfg
->GetPath();
459 cfg
->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders
);
460 cfg
->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
461 cfg
->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
462 for (int i
= 0; i
< 7; i
++)
464 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
465 cfg
->Write(tmp
, (long) m_Parser
->m_FontsSizes
[i
]);
468 if (path
!= wxEmptyString
)
469 cfg
->SetPath(oldpath
);
474 bool wxHtmlWindow::HistoryBack()
478 if (m_HistoryPos
< 1) return FALSE
;
480 // store scroll position into history item:
482 GetViewStart(&x
, &y
);
483 (*m_History
)[m_HistoryPos
].SetPos(y
);
485 // go to previous position:
488 l
= (*m_History
)[m_HistoryPos
].GetPage();
489 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
492 if (a
== wxEmptyString
) LoadPage(l
);
493 else LoadPage(l
+ wxT("#") + a
);
496 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
501 bool wxHtmlWindow::HistoryCanBack()
503 if (m_HistoryPos
< 1) return FALSE
;
508 bool wxHtmlWindow::HistoryForward()
512 if (m_HistoryPos
== -1) return FALSE
;
513 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
515 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
518 l
= (*m_History
)[m_HistoryPos
].GetPage();
519 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
522 if (a
== wxEmptyString
) LoadPage(l
);
523 else LoadPage(l
+ wxT("#") + a
);
526 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
531 bool wxHtmlWindow::HistoryCanForward()
533 if (m_HistoryPos
== -1) return FALSE
;
534 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
539 void wxHtmlWindow::HistoryClear()
545 void wxHtmlWindow::AddProcessor(wxHtmlProcessor
*processor
)
549 m_Processors
= new wxHtmlProcessorList
;
550 m_Processors
->DeleteContents(TRUE
);
552 wxHtmlProcessorList::Node
*node
;
554 for (node
= m_Processors
->GetFirst(); node
; node
= node
->GetNext())
556 if (processor
->GetPriority() > node
->GetData()->GetPriority())
558 m_Processors
->Insert(node
, processor
);
562 m_Processors
->Append(processor
);
565 /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor
*processor
)
567 if (!m_GlobalProcessors
)
569 m_GlobalProcessors
= new wxHtmlProcessorList
;
570 m_GlobalProcessors
->DeleteContents(TRUE
);
572 wxHtmlProcessorList::Node
*node
;
574 for (node
= m_GlobalProcessors
->GetFirst(); node
; node
= node
->GetNext())
576 if (processor
->GetPriority() > node
->GetData()->GetPriority())
578 m_GlobalProcessors
->Insert(node
, processor
);
582 m_GlobalProcessors
->Append(processor
);
587 wxList
wxHtmlWindow::m_Filters
;
588 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
589 wxCursor
*wxHtmlWindow::s_cur_hand
= NULL
;
590 wxCursor
*wxHtmlWindow::s_cur_arrow
= NULL
;
591 wxHtmlProcessorList
*wxHtmlWindow::m_GlobalProcessors
= NULL
;
593 void wxHtmlWindow::CleanUpStatics()
595 delete m_DefaultFilter
;
596 m_DefaultFilter
= NULL
;
597 m_Filters
.DeleteContents(TRUE
);
599 delete m_GlobalProcessors
;
600 m_GlobalProcessors
= NULL
;
607 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
609 m_Filters
.Append(filter
);
615 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
617 LoadPage(link
.GetHref());
620 void wxHtmlWindow::OnCellClicked(wxHtmlCell
*cell
,
621 wxCoord x
, wxCoord y
,
622 const wxMouseEvent
& event
)
624 wxCHECK_RET( cell
, _T("can't be called with NULL cell") );
626 cell
->OnMouseClick(this, x
, y
, event
);
629 void wxHtmlWindow::OnCellMouseHover(wxHtmlCell
* WXUNUSED(cell
),
630 wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
635 void wxHtmlWindow::OnDraw(wxDC
& dc
)
637 if (m_tmpCanDrawLocks
> 0 || m_Cell
== NULL
) return;
640 wxRect rect
= GetUpdateRegion().GetBox();
642 dc
.SetMapMode(wxMM_TEXT
);
643 dc
.SetBackgroundMode(wxTRANSPARENT
);
644 GetViewStart(&x
, &y
);
646 m_Cell
->Draw(dc
, 0, 0,
647 y
* wxHTML_SCROLL_STEP
+ rect
.GetTop(),
648 y
* wxHTML_SCROLL_STEP
+ rect
.GetBottom());
654 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
656 wxScrolledWindow::OnSize(event
);
662 void wxHtmlWindow::OnMouseEvent(wxMouseEvent
& event
)
664 m_tmpMouseMoved
= TRUE
;
666 if (event
.ButtonDown())
672 GetViewStart(&sx
, &sy
);
673 sx
*= wxHTML_SCROLL_STEP
;
674 sy
*= wxHTML_SCROLL_STEP
;
676 wxPoint pos
= event
.GetPosition();
680 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
682 // VZ: is it possible that we don't find anything at all?
683 // VS: yes. FindCellByPos returns terminal cell and
684 // containers may have empty borders
686 OnCellClicked(cell
, pos
.x
, pos
.y
, event
);
693 void wxHtmlWindow::OnIdle(wxIdleEvent
& WXUNUSED(event
))
695 if (s_cur_hand
== NULL
)
697 s_cur_hand
= new wxCursor(wxCURSOR_HAND
);
698 s_cur_arrow
= new wxCursor(wxCURSOR_ARROW
);
701 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
))
704 GetViewStart(&sx
, &sy
);
705 sx
*= wxHTML_SCROLL_STEP
;
706 sy
*= wxHTML_SCROLL_STEP
;
709 wxGetMousePosition(&x
, &y
);
710 ScreenToClient(&x
, &y
);
714 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(x
, y
);
715 if ( cell
!= m_tmpLastCell
)
717 wxHtmlLinkInfo
*lnk
= cell
? cell
->GetLink(x
, y
) : NULL
;
719 if (lnk
!= m_tmpLastLink
)
723 SetCursor(*s_cur_arrow
);
724 if (m_RelatedStatusBar
!= -1)
725 m_RelatedFrame
->SetStatusText(wxEmptyString
, m_RelatedStatusBar
);
729 SetCursor(*s_cur_hand
);
730 if (m_RelatedStatusBar
!= -1)
731 m_RelatedFrame
->SetStatusText(lnk
->GetHref(), m_RelatedStatusBar
);
736 m_tmpLastCell
= cell
;
738 else // mouse moved but stayed in the same cell
741 OnCellMouseHover(cell
, x
, y
);
744 m_tmpMouseMoved
= FALSE
;
749 IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor
,wxObject
)
751 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
753 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
754 EVT_SIZE(wxHtmlWindow::OnSize
)
755 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent
)
756 EVT_RIGHT_DOWN(wxHtmlWindow::OnMouseEvent
)
757 EVT_MOTION(wxHtmlWindow::OnMouseEvent
)
758 EVT_IDLE(wxHtmlWindow::OnIdle
)
765 // A module to allow initialization/cleanup
766 // without calling these functions from app.cpp or from
767 // the user's application.
769 class wxHtmlWinModule
: public wxModule
771 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
773 wxHtmlWinModule() : wxModule() {}
774 bool OnInit() { return TRUE
; }
775 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
778 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
781 // This hack forces the linker to always link in m_* files
782 // (wxHTML doesn't work without handlers from these files)
783 #include "wx/html/forcelnk.h"
784 FORCE_WXHTML_MODULES()