]>
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
;
370 l
= m_History
[m_HistoryPos
].GetPage();
371 a
= m_History
[m_HistoryPos
].GetAnchor();
374 if (a
== wxEmptyString
) LoadPage(l
);
375 else LoadPage(l
+ wxT("#") + a
);
379 Scroll(0, m_History
[m_HistoryPos
].GetPos());
386 bool wxHtmlWindow::HistoryForward()
390 if (m_HistoryPos
== -1) return FALSE
;
391 if (m_HistoryPos
>= (int)m_History
.GetCount() - 1)return FALSE
;
393 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
396 l
= m_History
[m_HistoryPos
].GetPage();
397 a
= m_History
[m_HistoryPos
].GetAnchor();
400 if (a
== wxEmptyString
) LoadPage(l
);
401 else LoadPage(l
+ wxT("#") + a
);
405 Scroll(0, m_History
[m_HistoryPos
].GetPos());
412 void wxHtmlWindow::HistoryClear()
420 wxList
wxHtmlWindow::m_Filters
;
421 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
422 wxCursor
*wxHtmlWindow::s_cur_hand
= NULL
;
423 wxCursor
*wxHtmlWindow::s_cur_arrow
= NULL
;
425 void wxHtmlWindow::CleanUpStatics()
427 if (m_DefaultFilter
) delete m_DefaultFilter
;
428 m_DefaultFilter
= NULL
;
429 m_Filters
.DeleteContents(TRUE
);
431 if (s_cur_hand
) delete s_cur_hand
;
432 if (s_cur_arrow
) delete s_cur_arrow
;
437 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
439 m_Filters
.Append(filter
);
445 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
447 LoadPage(link
.GetHref());
452 void wxHtmlWindow::OnDraw(wxDC
& dc
)
455 wxRegionIterator
upd(GetUpdateRegion()); // get the update rect list
458 if (m_tmpCanDrawLocks
> 0) return;
460 dc
.SetMapMode(wxMM_TEXT
);
462 /* VS - I don't think this is neccessary any longer
463 MSC_VER 1200 means MSVC 6.0 and it works fine */
464 #if defined(_MSC_VER) && (_MSC_VER == 1200)
465 ::SetMapMode((HDC
)dc
.GetHDC(), MM_TEXT
);
468 dc
.SetBackgroundMode(wxTRANSPARENT
);
474 if (m_Cell
) m_Cell
-> Draw(dc
, 0, 0, y
* wxHTML_SCROLL_STEP
+ v_y
, y
* wxHTML_SCROLL_STEP
+ v_h
+ v_y
);
482 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
484 wxScrolledWindow::OnSize(event
);
490 void wxHtmlWindow::OnMouseEvent(wxMouseEvent
& event
)
492 m_tmpMouseMoved
= TRUE
;
494 if (event
.ButtonDown()) {
499 ViewStart(&sx
, &sy
); sx
*= wxHTML_SCROLL_STEP
; sy
*= wxHTML_SCROLL_STEP
;
500 pos
= event
.GetPosition();
503 m_Cell
-> OnMouseClick(this, sx
+ pos
.x
, sy
+ pos
.y
, event
);
509 void wxHtmlWindow::OnIdle(wxIdleEvent
& event
)
511 if (s_cur_hand
== NULL
)
513 s_cur_hand
= new wxCursor(wxCURSOR_HAND
);
514 s_cur_arrow
= new wxCursor(wxCURSOR_ARROW
);
517 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
)) {
522 ViewStart(&sx
, &sy
); sx
*= wxHTML_SCROLL_STEP
; sy
*= wxHTML_SCROLL_STEP
;
523 wxGetMousePosition(&x
, &y
);
524 ScreenToClient(&x
, &y
);
525 lnk
= m_Cell
-> GetLink(sx
+ x
, sy
+ y
);
527 if (lnk
!= m_tmpLastLink
) {
529 SetCursor(*s_cur_arrow
);
530 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(wxEmptyString
, m_RelatedStatusBar
);
533 SetCursor(*s_cur_hand
);
534 if (m_RelatedStatusBar
!= -1)
535 m_RelatedFrame
-> SetStatusText(lnk
-> GetHref(), m_RelatedStatusBar
);
539 m_tmpMouseMoved
= FALSE
;
546 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
548 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
549 EVT_SIZE(wxHtmlWindow::OnSize
)
550 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent
)
551 EVT_MOTION(wxHtmlWindow::OnMouseEvent
)
552 EVT_IDLE(wxHtmlWindow::OnIdle
)
559 // A module to allow initialization/cleanup
560 // without calling these functions from app.cpp or from
561 // the user's application.
563 class wxHtmlWinModule
: public wxModule
565 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
567 wxHtmlWinModule() : wxModule() {}
568 bool OnInit() { return TRUE
; }
569 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
572 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
577 ///// default mod handlers are forced there: