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 if (nodeL
->GetData()->IsEnabled())
164 newsrc
= nodeL
->GetData()->Process(newsrc
);
165 nodeL
= nodeL
->GetNext();
169 if (nodeG
->GetData()->IsEnabled())
170 newsrc
= nodeG
->GetData()->Process(newsrc
);
171 nodeG
= nodeG
->GetNext();
176 // ...and run the parser on it:
177 wxClientDC
*dc
= new wxClientDC(this);
178 dc
->SetMapMode(wxMM_TEXT
);
179 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
180 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
187 m_Cell
= (wxHtmlContainerCell
*) m_Parser
->Parse(newsrc
);
189 m_Cell
->SetIndent(m_Borders
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
190 m_Cell
->SetAlignHor(wxHTML_ALIGN_CENTER
);
192 if (m_tmpCanDrawLocks
== 0)
198 bool wxHtmlWindow::LoadPage(const wxString
& location
)
202 bool needs_refresh
= FALSE
;
204 SetCursor(*wxHOURGLASS_CURSOR
);
205 wxYield(); Refresh(FALSE
);
208 if (m_HistoryOn
&& (m_HistoryPos
!= -1))
210 // store scroll position into history item:
213 (*m_History
)[m_HistoryPos
].SetPos(y
);
216 if (location
[0] == wxT('#'))
219 wxString anch
= location
.Mid(1) /*1 to end*/;
221 rt_val
= ScrollToAnchor(anch
);
224 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&& location
.BeforeFirst(wxT('#')) == m_OpenedPage
)
226 wxString anch
= location
.AfterFirst(wxT('#'));
228 rt_val
= ScrollToAnchor(anch
);
231 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&&
232 (m_FS
->GetPath() + location
.BeforeFirst(wxT('#'))) == m_OpenedPage
)
234 wxString anch
= location
.AfterFirst(wxT('#'));
236 rt_val
= ScrollToAnchor(anch
);
242 needs_refresh
= TRUE
;
244 if (m_RelatedStatusBar
!= -1)
246 m_RelatedFrame
->SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
250 f
= m_FS
->OpenFile(location
);
256 wxLogError(_("Unable to open requested HTML document: %s"), location
.c_str());
259 SetCursor(*wxSTANDARD_CURSOR
);
266 wxString src
= wxEmptyString
;
268 if (m_RelatedStatusBar
!= -1)
270 wxString msg
= _("Loading : ") + location
;
271 m_RelatedFrame
->SetStatusText(msg
, m_RelatedStatusBar
);
275 node
= m_Filters
.GetFirst();
278 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
281 src
= h
->ReadFile(*f
);
284 node
= node
->GetNext();
286 if (src
== wxEmptyString
)
288 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
289 src
= m_DefaultFilter
->ReadFile(*f
);
292 m_FS
->ChangePathTo(f
->GetLocation());
293 rt_val
= SetPage(src
);
294 m_OpenedPage
= f
->GetLocation();
295 if (f
->GetAnchor() != wxEmptyString
)
298 ScrollToAnchor(f
->GetAnchor());
303 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
->SetStatusText(_("Done"), m_RelatedStatusBar
);
307 if (m_HistoryOn
) // add this page to history there:
309 int c
= m_History
->GetCount() - (m_HistoryPos
+ 1);
312 for (int i
= 0; i
< c
; i
++)
313 m_History
->Remove(m_HistoryPos
);
314 m_History
->Add(new wxHtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
317 if (m_OpenedPageTitle
== wxEmptyString
)
318 OnSetTitle(wxFileNameFromPath(m_OpenedPage
));
319 SetCursor(*wxSTANDARD_CURSOR
);
335 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
337 const wxHtmlCell
*c
= m_Cell
->Find(wxHTML_COND_ISANCHOR
, &anchor
);
340 wxLogWarning(_("HTML anchor %s does not exist."), anchor
.c_str());
347 for (y
= 0; c
!= NULL
; c
= c
->GetParent()) y
+= c
->GetPosY();
348 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
349 m_OpenedAnchor
= anchor
;
355 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
360 tit
.Printf(m_TitleFormat
, title
.c_str());
361 m_RelatedFrame
->SetTitle(tit
);
363 m_OpenedPageTitle
= title
;
370 void wxHtmlWindow::CreateLayout()
372 int ClientWidth
, ClientHeight
;
376 if (m_Style
& wxHW_SCROLLBAR_NEVER
)
378 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // always off
379 GetClientSize(&ClientWidth
, &ClientHeight
);
380 m_Cell
->Layout(ClientWidth
);
384 GetClientSize(&ClientWidth
, &ClientHeight
);
385 m_Cell
->Layout(ClientWidth
);
386 if (ClientHeight
< m_Cell
->GetHeight() + GetCharHeight())
389 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
390 m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
,
391 (m_Cell
->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
392 /*cheat: top-level frag is always container*/);
394 else /* we fit into window, no need for scrollbars */
396 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
397 GetClientSize(&ClientWidth
, &ClientHeight
);
398 m_Cell
->Layout(ClientWidth
); // ...and relayout
405 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
410 wxString p_fff
, p_ffn
;
412 if (path
!= wxEmptyString
)
414 oldpath
= cfg
->GetPath();
418 m_Borders
= cfg
->Read(wxT("wxHtmlWindow/Borders"), m_Borders
);
419 p_fff
= cfg
->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
420 p_ffn
= cfg
->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
421 for (int i
= 0; i
< 7; i
++)
423 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
424 p_fontsizes
[i
] = cfg
->Read(tmp
, m_Parser
->m_FontsSizes
[i
]);
426 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
428 if (path
!= wxEmptyString
)
429 cfg
->SetPath(oldpath
);
434 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
439 if (path
!= wxEmptyString
)
441 oldpath
= cfg
->GetPath();
445 cfg
->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders
);
446 cfg
->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
447 cfg
->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
448 for (int i
= 0; i
< 7; i
++)
450 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
451 cfg
->Write(tmp
, (long) m_Parser
->m_FontsSizes
[i
]);
454 if (path
!= wxEmptyString
)
455 cfg
->SetPath(oldpath
);
460 bool wxHtmlWindow::HistoryBack()
464 if (m_HistoryPos
< 1) return FALSE
;
466 // store scroll position into history item:
469 (*m_History
)[m_HistoryPos
].SetPos(y
);
471 // go to previous position:
474 l
= (*m_History
)[m_HistoryPos
].GetPage();
475 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
478 if (a
== wxEmptyString
) LoadPage(l
);
479 else LoadPage(l
+ wxT("#") + a
);
483 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
488 bool wxHtmlWindow::HistoryCanBack()
490 if (m_HistoryPos
< 1) return FALSE
;
495 bool wxHtmlWindow::HistoryForward()
499 if (m_HistoryPos
== -1) return FALSE
;
500 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
502 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
505 l
= (*m_History
)[m_HistoryPos
].GetPage();
506 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
509 if (a
== wxEmptyString
) LoadPage(l
);
510 else LoadPage(l
+ wxT("#") + a
);
514 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
519 bool wxHtmlWindow::HistoryCanForward()
521 if (m_HistoryPos
== -1) return FALSE
;
522 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
527 void wxHtmlWindow::HistoryClear()
533 void wxHtmlWindow::AddProcessor(wxHtmlProcessor
*processor
)
537 m_Processors
= new wxHtmlProcessorList
;
538 m_Processors
->DeleteContents(TRUE
);
540 wxHtmlProcessorList::Node
*node
;
542 for (node
= m_Processors
->GetFirst(); node
; node
= node
->GetNext())
544 if (processor
->GetPriority() > node
->GetData()->GetPriority())
546 m_Processors
->Insert(node
, processor
);
550 m_Processors
->Append(processor
);
553 /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor
*processor
)
555 if (!m_GlobalProcessors
)
557 m_GlobalProcessors
= new wxHtmlProcessorList
;
558 m_GlobalProcessors
->DeleteContents(TRUE
);
560 wxHtmlProcessorList::Node
*node
;
562 for (node
= m_GlobalProcessors
->GetFirst(); node
; node
= node
->GetNext())
564 if (processor
->GetPriority() > node
->GetData()->GetPriority())
566 m_GlobalProcessors
->Insert(node
, processor
);
570 m_GlobalProcessors
->Append(processor
);
575 wxList
wxHtmlWindow::m_Filters
;
576 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
577 wxCursor
*wxHtmlWindow::s_cur_hand
= NULL
;
578 wxCursor
*wxHtmlWindow::s_cur_arrow
= NULL
;
579 wxHtmlProcessorList
*wxHtmlWindow::m_GlobalProcessors
= NULL
;
581 void wxHtmlWindow::CleanUpStatics()
583 delete m_DefaultFilter
;
584 m_DefaultFilter
= NULL
;
585 m_Filters
.DeleteContents(TRUE
);
587 delete m_GlobalProcessors
;
588 m_GlobalProcessors
= NULL
;
595 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
597 m_Filters
.Append(filter
);
603 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
605 LoadPage(link
.GetHref());
610 void wxHtmlWindow::OnDraw(wxDC
& dc
)
613 wxRegionIterator
upd(GetUpdateRegion()); // get the update rect list
616 if (m_tmpCanDrawLocks
> 0) return;
618 dc
.SetMapMode(wxMM_TEXT
);
620 /* VS - I don't think this is neccessary any longer
621 MSC_VER 1200 means MSVC 6.0 and it works fine */
622 #if defined(_MSC_VER) && (_MSC_VER == 1200)
623 ::SetMapMode((HDC
)dc
.GetHDC(), MM_TEXT
);
626 dc
.SetBackgroundMode(wxTRANSPARENT
);
633 if (m_Cell
) m_Cell
->Draw(dc
, 0, 0, y
* wxHTML_SCROLL_STEP
+ v_y
, y
* wxHTML_SCROLL_STEP
+ v_h
+ v_y
);
641 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
643 wxScrolledWindow::OnSize(event
);
649 void wxHtmlWindow::OnMouseEvent(wxMouseEvent
& event
)
651 m_tmpMouseMoved
= TRUE
;
653 if (event
.ButtonDown())
659 ViewStart(&sx
, &sy
); sx
*= wxHTML_SCROLL_STEP
; sy
*= wxHTML_SCROLL_STEP
;
660 pos
= event
.GetPosition();
663 m_Cell
->OnMouseClick(this, sx
+ pos
.x
, sy
+ pos
.y
, event
);
669 void wxHtmlWindow::OnIdle(wxIdleEvent
& WXUNUSED(event
))
671 if (s_cur_hand
== NULL
)
673 s_cur_hand
= new wxCursor(wxCURSOR_HAND
);
674 s_cur_arrow
= new wxCursor(wxCURSOR_ARROW
);
677 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
))
683 ViewStart(&sx
, &sy
); sx
*= wxHTML_SCROLL_STEP
; sy
*= wxHTML_SCROLL_STEP
;
684 wxGetMousePosition(&x
, &y
);
685 ScreenToClient(&x
, &y
);
686 lnk
= m_Cell
->GetLink(sx
+ x
, sy
+ y
);
688 if (lnk
!= m_tmpLastLink
)
692 SetCursor(*s_cur_arrow
);
693 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
->SetStatusText(wxEmptyString
, m_RelatedStatusBar
);
697 SetCursor(*s_cur_hand
);
698 if (m_RelatedStatusBar
!= -1)
699 m_RelatedFrame
->SetStatusText(lnk
->GetHref(), m_RelatedStatusBar
);
703 m_tmpMouseMoved
= FALSE
;
708 IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor
,wxObject
)
710 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
712 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
713 EVT_SIZE(wxHtmlWindow::OnSize
)
714 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent
)
715 EVT_MOTION(wxHtmlWindow::OnMouseEvent
)
716 EVT_IDLE(wxHtmlWindow::OnIdle
)
723 // A module to allow initialization/cleanup
724 // without calling these functions from app.cpp or from
725 // the user's application.
727 class wxHtmlWinModule
: public wxModule
729 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
731 wxHtmlWinModule() : wxModule() {}
732 bool OnInit() { return TRUE
; }
733 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
736 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
741 ///// default mod handlers are forced there: