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
29 #include "wx/html/htmlwin.h"
30 #include "wx/html/forcelnk.h"
31 #include "wx/html/htmlproc.h"
33 #include "wx/arrimpl.cpp"
35 #include "wx/listimpl.cpp"
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 // item of history list
42 class WXDLLEXPORT wxHtmlHistoryItem
: public wxObject
45 wxHtmlHistoryItem(const wxString
& p
, const wxString
& a
) {m_Page
= p
, m_Anchor
= a
, m_Pos
= 0;}
46 int GetPos() const {return m_Pos
;}
47 void SetPos(int p
) {m_Pos
= p
;}
48 const wxString
& GetPage() const {return m_Page
;}
49 const wxString
& GetAnchor() const {return m_Anchor
;}
58 //-----------------------------------------------------------------------------
59 // our private arrays:
60 //-----------------------------------------------------------------------------
62 WX_DECLARE_OBJARRAY(wxHtmlHistoryItem
, wxHtmlHistoryArray
);
63 WX_DEFINE_OBJARRAY(wxHtmlHistoryArray
);
65 WX_DECLARE_LIST(wxHtmlProcessor
, wxHtmlProcessorList
);
66 WX_DEFINE_LIST(wxHtmlProcessorList
);
68 //-----------------------------------------------------------------------------
70 //-----------------------------------------------------------------------------
73 wxHtmlWindow::wxHtmlWindow(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
74 long style
, const wxString
& name
) : wxScrolledWindow(parent
, id
, pos
, size
, style
| wxVSCROLL
| wxHSCROLL
, name
)
76 m_tmpMouseMoved
= FALSE
;
78 m_tmpCanDrawLocks
= 0;
79 m_FS
= new wxFileSystem();
80 m_RelatedStatusBar
= -1;
81 m_RelatedFrame
= NULL
;
82 m_TitleFormat
= wxT("%s");
83 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
85 m_Parser
= new wxHtmlWinParser(this);
86 m_Parser
->SetFS(m_FS
);
90 m_History
= new wxHtmlHistoryArray
;
93 SetPage(wxT("<html><body></body></html>"));
98 wxHtmlWindow::~wxHtmlWindow()
102 if (m_Cell
) delete m_Cell
;
112 void wxHtmlWindow::SetRelatedFrame(wxFrame
* frame
, const wxString
& format
)
114 m_RelatedFrame
= frame
;
115 m_TitleFormat
= format
;
120 void wxHtmlWindow::SetRelatedStatusBar(int bar
)
122 m_RelatedStatusBar
= bar
;
127 void wxHtmlWindow::SetFonts(wxString normal_face
, wxString fixed_face
, const int *sizes
)
129 wxString op
= m_OpenedPage
;
131 m_Parser
->SetFonts(normal_face
, fixed_face
, sizes
);
132 // fonts changed => contents invalid, so reload the page:
133 SetPage(wxT("<html><body></body></html>"));
134 if (!op
.IsEmpty()) LoadPage(op
);
139 bool wxHtmlWindow::SetPage(const wxString
& source
)
141 wxString
newsrc(source
);
143 // pass HTML through registered processors:
144 if (m_Processors
|| m_GlobalProcessors
)
146 wxHtmlProcessorList::Node
*nodeL
, *nodeG
;
149 nodeL
= (m_Processors
) ? m_Processors
->GetFirst() : NULL
;
150 nodeG
= (m_GlobalProcessors
) ? m_GlobalProcessors
->GetFirst() : NULL
;
152 // VS: there are two lists, global and local, both of them sorted by
153 // priority. Since we have to go through _both_ lists with
154 // decreasing priority, we "merge-sort" the lists on-line by
155 // processing that one of the two heads that has higher priority
156 // in every iteration
157 while (nodeL
|| nodeG
)
159 prL
= (nodeL
) ? nodeL
->GetData()->GetPriority() : -1;
160 prG
= (nodeG
) ? nodeG
->GetData()->GetPriority() : -1;
163 newsrc
= nodeL
->GetData()->Process(newsrc
);
164 nodeL
= nodeL
->GetNext();
168 newsrc
= nodeG
->GetData()->Process(newsrc
);
169 nodeG
= nodeG
->GetNext();
174 // ...and run the parser on it:
175 wxClientDC
*dc
= new wxClientDC(this);
176 dc
->SetMapMode(wxMM_TEXT
);
177 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
178 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
185 m_Cell
= (wxHtmlContainerCell
*) m_Parser
->Parse(newsrc
);
187 m_Cell
->SetIndent(m_Borders
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
188 m_Cell
->SetAlignHor(wxHTML_ALIGN_CENTER
);
190 if (m_tmpCanDrawLocks
== 0)
196 bool wxHtmlWindow::LoadPage(const wxString
& location
)
200 bool needs_refresh
= FALSE
;
202 SetCursor(*wxHOURGLASS_CURSOR
);
203 wxYield(); Refresh(FALSE
);
206 if (m_HistoryOn
&& (m_HistoryPos
!= -1))
208 // store scroll position into history item:
211 (*m_History
)[m_HistoryPos
].SetPos(y
);
214 if (location
[0] == wxT('#'))
217 wxString anch
= location
.Mid(1) /*1 to end*/;
219 rt_val
= ScrollToAnchor(anch
);
222 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&& location
.BeforeFirst(wxT('#')) == m_OpenedPage
)
224 wxString anch
= location
.AfterFirst(wxT('#'));
226 rt_val
= ScrollToAnchor(anch
);
229 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&&
230 (m_FS
->GetPath() + location
.BeforeFirst(wxT('#'))) == m_OpenedPage
)
232 wxString anch
= location
.AfterFirst(wxT('#'));
234 rt_val
= ScrollToAnchor(anch
);
240 needs_refresh
= TRUE
;
242 if (m_RelatedStatusBar
!= -1)
244 m_RelatedFrame
->SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
248 f
= m_FS
->OpenFile(location
);
254 wxLogError(_("Unable to open requested HTML document: %s"), location
.c_str());
257 SetCursor(*wxSTANDARD_CURSOR
);
264 wxString src
= wxEmptyString
;
266 if (m_RelatedStatusBar
!= -1)
268 wxString msg
= _("Loading : ") + location
;
269 m_RelatedFrame
->SetStatusText(msg
, m_RelatedStatusBar
);
273 node
= m_Filters
.GetFirst();
276 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
279 src
= h
->ReadFile(*f
);
282 node
= node
->GetNext();
284 if (src
== wxEmptyString
)
286 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
287 src
= m_DefaultFilter
->ReadFile(*f
);
290 m_FS
->ChangePathTo(f
->GetLocation());
291 rt_val
= SetPage(src
);
292 m_OpenedPage
= f
->GetLocation();
293 if (f
->GetAnchor() != wxEmptyString
)
296 ScrollToAnchor(f
->GetAnchor());
301 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
->SetStatusText(_("Done"), m_RelatedStatusBar
);
305 if (m_HistoryOn
) // add this page to history there:
307 int c
= m_History
->GetCount() - (m_HistoryPos
+ 1);
310 for (int i
= 0; i
< c
; i
++)
311 m_History
->Remove(m_HistoryPos
);
312 m_History
->Add(new wxHtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
315 if (m_OpenedPageTitle
== wxEmptyString
)
316 OnSetTitle(wxFileNameFromPath(m_OpenedPage
));
317 SetCursor(*wxSTANDARD_CURSOR
);
333 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
335 const wxHtmlCell
*c
= m_Cell
->Find(wxHTML_COND_ISANCHOR
, &anchor
);
338 wxLogWarning(_("HTML anchor %s does not exist."), anchor
.c_str());
345 for (y
= 0; c
!= NULL
; c
= c
->GetParent()) y
+= c
->GetPosY();
346 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
347 m_OpenedAnchor
= anchor
;
353 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
358 tit
.Printf(m_TitleFormat
, title
.c_str());
359 m_RelatedFrame
->SetTitle(tit
);
361 m_OpenedPageTitle
= title
;
368 void wxHtmlWindow::CreateLayout()
370 int ClientWidth
, ClientHeight
;
374 if (m_Style
& wxHW_SCROLLBAR_NEVER
)
376 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // always off
377 GetClientSize(&ClientWidth
, &ClientHeight
);
378 m_Cell
->Layout(ClientWidth
);
382 GetClientSize(&ClientWidth
, &ClientHeight
);
383 m_Cell
->Layout(ClientWidth
);
384 if (ClientHeight
< m_Cell
->GetHeight() + GetCharHeight())
387 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
388 m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
,
389 (m_Cell
->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
390 /*cheat: top-level frag is always container*/);
392 else /* we fit into window, no need for scrollbars */
394 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
395 GetClientSize(&ClientWidth
, &ClientHeight
);
396 m_Cell
->Layout(ClientWidth
); // ...and relayout
403 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
408 wxString p_fff
, p_ffn
;
410 if (path
!= wxEmptyString
)
412 oldpath
= cfg
->GetPath();
416 m_Borders
= cfg
->Read(wxT("wxHtmlWindow/Borders"), m_Borders
);
417 p_fff
= cfg
->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
418 p_ffn
= cfg
->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
419 for (int i
= 0; i
< 7; i
++)
421 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
422 p_fontsizes
[i
] = cfg
->Read(tmp
, m_Parser
->m_FontsSizes
[i
]);
424 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
426 if (path
!= wxEmptyString
)
427 cfg
->SetPath(oldpath
);
432 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
437 if (path
!= wxEmptyString
)
439 oldpath
= cfg
->GetPath();
443 cfg
->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders
);
444 cfg
->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
445 cfg
->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
446 for (int i
= 0; i
< 7; i
++)
448 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
449 cfg
->Write(tmp
, (long) m_Parser
->m_FontsSizes
[i
]);
452 if (path
!= wxEmptyString
)
453 cfg
->SetPath(oldpath
);
458 bool wxHtmlWindow::HistoryBack()
462 if (m_HistoryPos
< 1) return FALSE
;
464 // store scroll position into history item:
467 (*m_History
)[m_HistoryPos
].SetPos(y
);
469 // go to previous position:
472 l
= (*m_History
)[m_HistoryPos
].GetPage();
473 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
476 if (a
== wxEmptyString
) LoadPage(l
);
477 else LoadPage(l
+ wxT("#") + a
);
481 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
486 bool wxHtmlWindow::HistoryCanBack()
488 if (m_HistoryPos
< 1) return FALSE
;
493 bool wxHtmlWindow::HistoryForward()
497 if (m_HistoryPos
== -1) return FALSE
;
498 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
500 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
503 l
= (*m_History
)[m_HistoryPos
].GetPage();
504 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
507 if (a
== wxEmptyString
) LoadPage(l
);
508 else LoadPage(l
+ wxT("#") + a
);
512 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
517 bool wxHtmlWindow::HistoryCanForward()
519 if (m_HistoryPos
== -1) return FALSE
;
520 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
525 void wxHtmlWindow::HistoryClear()
531 void wxHtmlWindow::AddProcessor(wxHtmlProcessor
*processor
)
535 m_Processors
= new wxHtmlProcessorList
;
536 m_Processors
->DeleteContents(TRUE
);
538 wxHtmlProcessorList::Node
*node
;
540 for (node
= m_Processors
->GetFirst(); node
; node
= node
->GetNext())
542 if (processor
->GetPriority() > node
->GetData()->GetPriority())
544 m_Processors
->Insert(node
, processor
);
548 m_Processors
->Append(processor
);
551 /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor
*processor
)
553 if (!m_GlobalProcessors
)
555 m_GlobalProcessors
= new wxHtmlProcessorList
;
556 m_GlobalProcessors
->DeleteContents(TRUE
);
558 wxHtmlProcessorList::Node
*node
;
560 for (node
= m_GlobalProcessors
->GetFirst(); node
; node
= node
->GetNext())
562 if (processor
->GetPriority() > node
->GetData()->GetPriority())
564 m_GlobalProcessors
->Insert(node
, processor
);
568 m_GlobalProcessors
->Append(processor
);
573 wxList
wxHtmlWindow::m_Filters
;
574 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
575 wxCursor
*wxHtmlWindow::s_cur_hand
= NULL
;
576 wxCursor
*wxHtmlWindow::s_cur_arrow
= NULL
;
577 wxHtmlProcessorList
*wxHtmlWindow::m_GlobalProcessors
= NULL
;
579 void wxHtmlWindow::CleanUpStatics()
581 delete m_DefaultFilter
;
582 m_DefaultFilter
= NULL
;
583 m_Filters
.DeleteContents(TRUE
);
585 delete m_GlobalProcessors
;
586 m_GlobalProcessors
= NULL
;
593 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
595 m_Filters
.Append(filter
);
601 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
603 LoadPage(link
.GetHref());
608 void wxHtmlWindow::OnDraw(wxDC
& dc
)
611 wxRegionIterator
upd(GetUpdateRegion()); // get the update rect list
614 if (m_tmpCanDrawLocks
> 0) return;
616 dc
.SetMapMode(wxMM_TEXT
);
618 /* VS - I don't think this is neccessary any longer
619 MSC_VER 1200 means MSVC 6.0 and it works fine */
620 #if defined(_MSC_VER) && (_MSC_VER == 1200)
621 ::SetMapMode((HDC
)dc
.GetHDC(), MM_TEXT
);
624 dc
.SetBackgroundMode(wxTRANSPARENT
);
631 if (m_Cell
) m_Cell
->Draw(dc
, 0, 0, y
* wxHTML_SCROLL_STEP
+ v_y
, y
* wxHTML_SCROLL_STEP
+ v_h
+ v_y
);
639 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
641 wxScrolledWindow::OnSize(event
);
647 void wxHtmlWindow::OnMouseEvent(wxMouseEvent
& event
)
649 m_tmpMouseMoved
= TRUE
;
651 if (event
.ButtonDown())
657 ViewStart(&sx
, &sy
); sx
*= wxHTML_SCROLL_STEP
; sy
*= wxHTML_SCROLL_STEP
;
658 pos
= event
.GetPosition();
661 m_Cell
->OnMouseClick(this, sx
+ pos
.x
, sy
+ pos
.y
, event
);
667 void wxHtmlWindow::OnIdle(wxIdleEvent
& WXUNUSED(event
))
669 if (s_cur_hand
== NULL
)
671 s_cur_hand
= new wxCursor(wxCURSOR_HAND
);
672 s_cur_arrow
= new wxCursor(wxCURSOR_ARROW
);
675 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
))
681 ViewStart(&sx
, &sy
); sx
*= wxHTML_SCROLL_STEP
; sy
*= wxHTML_SCROLL_STEP
;
682 wxGetMousePosition(&x
, &y
);
683 ScreenToClient(&x
, &y
);
684 lnk
= m_Cell
->GetLink(sx
+ x
, sy
+ y
);
686 if (lnk
!= m_tmpLastLink
)
690 SetCursor(*s_cur_arrow
);
691 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
->SetStatusText(wxEmptyString
, m_RelatedStatusBar
);
695 SetCursor(*s_cur_hand
);
696 if (m_RelatedStatusBar
!= -1)
697 m_RelatedFrame
->SetStatusText(lnk
->GetHref(), m_RelatedStatusBar
);
701 m_tmpMouseMoved
= FALSE
;
706 IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor
,wxObject
)
708 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
710 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
711 EVT_SIZE(wxHtmlWindow::OnSize
)
712 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent
)
713 EVT_MOTION(wxHtmlWindow::OnMouseEvent
)
714 EVT_IDLE(wxHtmlWindow::OnIdle
)
721 // A module to allow initialization/cleanup
722 // without calling these functions from app.cpp or from
723 // the user's application.
725 class wxHtmlWinModule
: public wxModule
727 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
729 wxHtmlWinModule() : wxModule() {}
730 bool OnInit() { return TRUE
; }
731 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
734 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
739 ///// default mod handlers are forced there: