]>
git.saurik.com Git - wxWidgets.git/blob - src/html/htmlwin.cpp
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
15 #include "wx/wxprec.h"
18 #if wxUSE_HTML && wxUSE_STREAMS
28 #include "wx/html/htmlwin.h"
29 #include "wx/html/forcelnk.h"
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
39 #include "wx/arrimpl.cpp"
40 WX_DEFINE_OBJARRAY(HtmlHistoryArray
)
43 wxHtmlWindow::wxHtmlWindow(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
44 long style
, const wxString
& name
) : wxScrolledWindow(parent
, id
, pos
, size
, style
| wxVSCROLL
| wxHSCROLL
, name
)
46 m_tmpMouseMoved
= FALSE
;
48 m_tmpCanDrawLocks
= 0;
49 m_FS
= new wxFileSystem();
50 m_RelatedStatusBar
= -1;
51 m_RelatedFrame
= NULL
;
53 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
55 m_Parser
= new wxHtmlWinParser(this);
56 m_Parser
-> SetFS(m_FS
);
61 SetPage("<html><body></body></html>");
66 wxHtmlWindow::~wxHtmlWindow()
70 if (m_Cell
) delete m_Cell
;
78 void wxHtmlWindow::SetRelatedFrame(wxFrame
* frame
, const wxString
& format
)
80 m_RelatedFrame
= frame
;
81 m_TitleFormat
= format
;
86 void wxHtmlWindow::SetRelatedStatusBar(int bar
)
88 m_RelatedStatusBar
= bar
;
93 void wxHtmlWindow::SetFonts(wxString normal_face
, wxString fixed_face
, const int *sizes
)
95 wxString op
= m_OpenedPage
;
97 m_Parser
-> SetFonts(normal_face
, fixed_face
, sizes
);
98 SetPage(wxT("<html><body></body></html>")); // fonts changed => contents invalid
99 if (!op
.IsEmpty()) LoadPage(op
);
104 bool wxHtmlWindow::SetPage(const wxString
& source
)
106 wxClientDC
*dc
= new wxClientDC(this);
108 dc
-> SetMapMode(wxMM_TEXT
);
109 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
110 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
111 m_Parser
-> SetDC(dc
);
116 m_Cell
= (wxHtmlContainerCell
*) m_Parser
-> Parse(source
);
118 m_Cell
-> SetIndent(m_Borders
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
119 m_Cell
-> SetAlignHor(wxHTML_ALIGN_CENTER
);
121 if (m_tmpCanDrawLocks
== 0) Refresh();
126 bool wxHtmlWindow::LoadPage(const wxString
& location
)
130 bool needs_refresh
= FALSE
;
132 SetCursor(*wxHOURGLASS_CURSOR
);
133 wxYield(); Refresh(FALSE
);
136 if (m_HistoryOn
&& (m_HistoryPos
!= -1)) { // store scroll position into history item
139 m_History
[m_HistoryPos
].SetPos(y
);
142 if (location
[0] == wxT('#')) { // local anchor
143 wxString anch
= location
.Mid(1) /*1 to end*/;
145 rt_val
= ScrollToAnchor(anch
);
148 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&& location
.BeforeFirst(wxT('#')) == m_OpenedPage
) {
149 wxString anch
= location
.AfterFirst(wxT('#'));
151 rt_val
= ScrollToAnchor(anch
);
154 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&&
155 (m_FS
-> GetPath() + location
.BeforeFirst(wxT('#'))) == m_OpenedPage
) {
156 wxString anch
= location
.AfterFirst(wxT('#'));
158 rt_val
= ScrollToAnchor(anch
);
163 needs_refresh
= TRUE
;
165 if (m_RelatedStatusBar
!= -1) {
166 m_RelatedFrame
-> SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
170 f
= m_FS
-> OpenFile(location
);
175 wxLogError(_("Unable to open requested HTML document: %s"), location
.c_str());
178 SetCursor(*wxSTANDARD_CURSOR
);
184 wxString src
= wxEmptyString
;
186 if (m_RelatedStatusBar
!= -1) {
187 wxString msg
= _("Loading : ") + location
;
188 m_RelatedFrame
-> SetStatusText(msg
, m_RelatedStatusBar
);
192 node
= m_Filters
.GetFirst();
194 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
-> GetData();
195 if (h
-> CanRead(*f
)) {
196 src
= h
-> ReadFile(*f
);
199 node
= node
-> GetNext();
201 if (src
== wxEmptyString
) {
202 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
203 src
= m_DefaultFilter
-> ReadFile(*f
);
206 m_FS
-> ChangePathTo(f
-> GetLocation());
207 rt_val
= SetPage(src
);
208 m_OpenedPage
= f
-> GetLocation();
209 if (f
-> GetAnchor() != wxEmptyString
) {
211 ScrollToAnchor(f
-> GetAnchor());
216 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(_("Done"), m_RelatedStatusBar
);
220 if (m_HistoryOn
) { // add this page to history there:
221 int c
= m_History
.GetCount() - (m_HistoryPos
+ 1);
224 for (int i
= 0; i
< c
; i
++)
225 m_History
.Remove(m_HistoryPos
);
226 m_History
.Add(new HtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
229 if (m_OpenedPageTitle
== wxEmptyString
)
230 OnSetTitle(wxFileNameFromPath(m_OpenedPage
));
231 SetCursor(*wxSTANDARD_CURSOR
);
246 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
248 const wxHtmlCell
*c
= m_Cell
-> Find(wxHTML_COND_ISANCHOR
, &anchor
);
251 wxLogWarning(_("HTML anchor %s does not exist."), anchor
.c_str());
257 for (y
= 0; c
!= NULL
; c
= c
-> GetParent()) y
+= c
-> GetPosY();
258 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
259 m_OpenedAnchor
= anchor
;
265 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
267 if (m_RelatedFrame
) {
269 tit
.Printf(m_TitleFormat
, title
.c_str());
270 m_RelatedFrame
-> SetTitle(tit
);
272 m_OpenedPageTitle
= title
;
279 void wxHtmlWindow::CreateLayout()
281 int ClientWidth
, ClientHeight
;
285 if (m_Style
& wxHW_SCROLLBAR_NEVER
) {
286 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
-> GetWidth() / wxHTML_SCROLL_STEP
, 0); // always off
287 GetClientSize(&ClientWidth
, &ClientHeight
);
288 m_Cell
-> Layout(ClientWidth
);
292 GetClientSize(&ClientWidth
, &ClientHeight
);
293 m_Cell
-> Layout(ClientWidth
);
294 if (ClientHeight
< m_Cell
-> GetHeight() + GetCharHeight()) {
296 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
297 m_Cell
-> GetWidth() / wxHTML_SCROLL_STEP
,
298 (m_Cell
-> GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
299 /*cheat: top-level frag is always container*/);
301 else { /* we fit into window, no need for scrollbars */
302 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
-> GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
303 GetClientSize(&ClientWidth
, &ClientHeight
);
304 m_Cell
-> Layout(ClientWidth
); // ...and relayout
311 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
316 wxString p_fff
, p_ffn
;
318 if (path
!= wxEmptyString
) {
319 oldpath
= cfg
-> GetPath();
320 cfg
-> SetPath(path
);
323 m_Borders
= cfg
-> Read("wxHtmlWindow/Borders", m_Borders
);
324 p_fff
= cfg
-> Read("wxHtmlWindow/FontFaceFixed", m_Parser
-> m_FontFaceFixed
);
325 p_ffn
= cfg
-> Read("wxHtmlWindow/FontFaceNormal", m_Parser
-> m_FontFaceNormal
);
326 for (int i
= 0; i
< 7; i
++) {
327 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
328 p_fontsizes
[i
] = cfg
-> Read(tmp
, m_Parser
-> m_FontsSizes
[i
]);
330 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
332 if (path
!= wxEmptyString
)
333 cfg
-> SetPath(oldpath
);
338 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
343 if (path
!= wxEmptyString
) {
344 oldpath
= cfg
-> GetPath();
345 cfg
-> SetPath(path
);
348 cfg
-> Write("wxHtmlWindow/Borders", (long) m_Borders
);
349 cfg
-> Write("wxHtmlWindow/FontFaceFixed", m_Parser
-> m_FontFaceFixed
);
350 cfg
-> Write("wxHtmlWindow/FontFaceNormal", m_Parser
-> m_FontFaceNormal
);
351 for (int i
= 0; i
< 7; i
++) {
352 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
353 cfg
-> Write(tmp
, (long) m_Parser
-> m_FontsSizes
[i
]);
356 if (path
!= wxEmptyString
)
357 cfg
-> SetPath(oldpath
);
362 bool wxHtmlWindow::HistoryBack()
366 if (m_HistoryPos
< 1) return FALSE
;
368 // store scroll position into history item:
371 m_History
[m_HistoryPos
].SetPos(y
);
373 // go to previous position:
376 l
= m_History
[m_HistoryPos
].GetPage();
377 a
= m_History
[m_HistoryPos
].GetAnchor();
380 if (a
== wxEmptyString
) LoadPage(l
);
381 else LoadPage(l
+ wxT("#") + a
);
385 Scroll(0, m_History
[m_HistoryPos
].GetPos());
392 bool wxHtmlWindow::HistoryForward()
396 if (m_HistoryPos
== -1) return FALSE
;
397 if (m_HistoryPos
>= (int)m_History
.GetCount() - 1)return FALSE
;
399 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
402 l
= m_History
[m_HistoryPos
].GetPage();
403 a
= m_History
[m_HistoryPos
].GetAnchor();
406 if (a
== wxEmptyString
) LoadPage(l
);
407 else LoadPage(l
+ wxT("#") + a
);
411 Scroll(0, m_History
[m_HistoryPos
].GetPos());
418 void wxHtmlWindow::HistoryClear()
426 wxList
wxHtmlWindow::m_Filters
;
427 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
428 wxCursor
*wxHtmlWindow::s_cur_hand
= NULL
;
429 wxCursor
*wxHtmlWindow::s_cur_arrow
= NULL
;
431 void wxHtmlWindow::CleanUpStatics()
433 if (m_DefaultFilter
) delete m_DefaultFilter
;
434 m_DefaultFilter
= NULL
;
435 m_Filters
.DeleteContents(TRUE
);
437 if (s_cur_hand
) delete s_cur_hand
;
438 if (s_cur_arrow
) delete s_cur_arrow
;
443 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
445 m_Filters
.Append(filter
);
451 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
453 LoadPage(link
.GetHref());
458 void wxHtmlWindow::OnDraw(wxDC
& dc
)
461 wxRegionIterator
upd(GetUpdateRegion()); // get the update rect list
464 if (m_tmpCanDrawLocks
> 0) return;
466 dc
.SetMapMode(wxMM_TEXT
);
468 /* VS - I don't think this is neccessary any longer
469 MSC_VER 1200 means MSVC 6.0 and it works fine */
470 #if defined(_MSC_VER) && (_MSC_VER == 1200)
471 ::SetMapMode((HDC
)dc
.GetHDC(), MM_TEXT
);
474 dc
.SetBackgroundMode(wxTRANSPARENT
);
480 if (m_Cell
) m_Cell
-> Draw(dc
, 0, 0, y
* wxHTML_SCROLL_STEP
+ v_y
, y
* wxHTML_SCROLL_STEP
+ v_h
+ v_y
);
488 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
490 wxScrolledWindow::OnSize(event
);
496 void wxHtmlWindow::OnMouseEvent(wxMouseEvent
& event
)
498 m_tmpMouseMoved
= TRUE
;
500 if (event
.ButtonDown()) {
505 ViewStart(&sx
, &sy
); sx
*= wxHTML_SCROLL_STEP
; sy
*= wxHTML_SCROLL_STEP
;
506 pos
= event
.GetPosition();
509 m_Cell
-> OnMouseClick(this, sx
+ pos
.x
, sy
+ pos
.y
, event
);
515 void wxHtmlWindow::OnIdle(wxIdleEvent
& event
)
517 if (s_cur_hand
== NULL
)
519 s_cur_hand
= new wxCursor(wxCURSOR_HAND
);
520 s_cur_arrow
= new wxCursor(wxCURSOR_ARROW
);
523 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
)) {
528 ViewStart(&sx
, &sy
); sx
*= wxHTML_SCROLL_STEP
; sy
*= wxHTML_SCROLL_STEP
;
529 wxGetMousePosition(&x
, &y
);
530 ScreenToClient(&x
, &y
);
531 lnk
= m_Cell
-> GetLink(sx
+ x
, sy
+ y
);
533 if (lnk
!= m_tmpLastLink
) {
535 SetCursor(*s_cur_arrow
);
536 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(wxEmptyString
, m_RelatedStatusBar
);
539 SetCursor(*s_cur_hand
);
540 if (m_RelatedStatusBar
!= -1)
541 m_RelatedFrame
-> SetStatusText(lnk
-> GetHref(), m_RelatedStatusBar
);
545 m_tmpMouseMoved
= FALSE
;
552 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
554 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
555 EVT_SIZE(wxHtmlWindow::OnSize
)
556 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent
)
557 EVT_MOTION(wxHtmlWindow::OnMouseEvent
)
558 EVT_IDLE(wxHtmlWindow::OnIdle
)
565 // A module to allow initialization/cleanup
566 // without calling these functions from app.cpp or from
567 // the user's application.
569 class wxHtmlWinModule
: public wxModule
571 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
573 wxHtmlWinModule() : wxModule() {}
574 bool OnInit() { return TRUE
; }
575 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
578 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
583 ///// default mod handlers are forced there: