]>
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"
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
, 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
)
131 SetCursor(*wxHOURGLASS_CURSOR
);
135 if (m_HistoryOn
&& (m_HistoryPos
!= -1)) { // store scroll position into history item
138 m_History
[m_HistoryPos
].SetPos(y
);
141 if (location
[0] == '#') { // local anchor
142 wxString anch
= location
.Mid(1) /*1 to end*/;
144 rt_val
= ScrollToAnchor(anch
);
149 if (m_RelatedStatusBar
!= -1) {
150 m_RelatedFrame
-> SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
154 f
= m_FS
-> OpenFile(location
);
158 err
.Printf(_("Unable to open requested location :\n\n%s"), WXSTRINGCAST location
);
161 wxMessageBox(err
, "Error");
163 SetCursor(*wxSTANDARD_CURSOR
);
169 wxString src
= wxEmptyString
;
171 if (m_RelatedStatusBar
!= -1) {
172 wxString msg
= _("Loading : ") + location
;
173 m_RelatedFrame
-> SetStatusText(msg
, m_RelatedStatusBar
);
177 node
= m_Filters
.GetFirst();
179 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
-> GetData();
180 if (h
-> CanRead(*f
)) {
181 src
= h
-> ReadFile(*f
);
184 node
= node
-> GetNext();
186 if (src
== wxEmptyString
) {
187 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
188 src
= m_DefaultFilter
-> ReadFile(*f
);
191 m_FS
-> ChangePathTo(f
-> GetLocation());
192 rt_val
= SetPage(src
);
193 m_OpenedPage
= f
-> GetLocation();
194 if (f
-> GetAnchor() != wxEmptyString
) {
195 // m_tmpCanDrawLocks--;
196 ScrollToAnchor(f
-> GetAnchor());
197 // m_tmpCanDrawLocks++;
202 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(_("Done"), m_RelatedStatusBar
);
206 if (m_HistoryOn
) { // add this page to history there:
207 int c
= m_History
.GetCount() - (m_HistoryPos
+ 1);
210 for (int i
= 0; i
< c
; i
++)
211 m_History
.Remove(m_HistoryPos
);
212 m_History
.Add(new HtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
215 SetCursor(*wxSTANDARD_CURSOR
);
225 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
227 const wxHtmlCell
*c
= m_Cell
-> Find(wxHTML_COND_ISANCHOR
, &anchor
);
228 if (!c
) return FALSE
;
232 for (y
= 0; c
!= NULL
; c
= c
-> GetParent()) y
+= c
-> GetPosY();
233 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
234 m_OpenedAnchor
= anchor
;
240 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
242 if (m_RelatedFrame
) {
244 tit
.Printf(m_TitleFormat
, title
.c_str());
245 m_RelatedFrame
-> SetTitle(tit
);
247 m_OpenedPageTitle
= title
;
254 void wxHtmlWindow::CreateLayout()
256 int ClientWidth
, ClientHeight
;
260 if (m_Style
== wxHW_SCROLLBAR_NEVER
) {
261 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
-> GetWidth() / wxHTML_SCROLL_STEP
, 0); // always off
262 GetClientSize(&ClientWidth
, &ClientHeight
);
263 m_Cell
-> Layout(ClientWidth
);
267 GetClientSize(&ClientWidth
, &ClientHeight
);
269 // VS : this looks extremely ugly under windoze, better fix needed!
270 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
-> GetWidth() / wxHTML_SCROLL_STEP
, ClientHeight
* 2); // always on
272 GetClientSize(&ClientWidth
, &ClientHeight
);
273 m_Cell
-> Layout(ClientWidth
);
274 if (ClientHeight
< m_Cell
-> GetHeight() + GetCharHeight()) {
276 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
277 m_Cell
-> GetWidth() / wxHTML_SCROLL_STEP
,
278 (m_Cell
-> GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
279 /*cheat: top-level frag is always container*/);
281 else { /* we fit into window, no need for scrollbars */
282 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
-> GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
283 GetClientSize(&ClientWidth
, &ClientHeight
);
284 m_Cell
-> Layout(ClientWidth
); // ...and relayout
291 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
296 wxString p_fff
, p_ffn
;
298 if (path
!= wxEmptyString
) {
299 oldpath
= cfg
-> GetPath();
300 cfg
-> SetPath(path
);
303 m_Borders
= cfg
-> Read("wxHtmlWindow/Borders", m_Borders
);
304 p_fff
= cfg
-> Read("wxHtmlWindow/FontFaceFixed", m_Parser
-> m_FontFaceFixed
);
305 p_ffn
= cfg
-> Read("wxHtmlWindow/FontFaceNormal", m_Parser
-> m_FontFaceNormal
);
306 for (int i
= 0; i
< 7; i
++) {
307 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
308 p_fontsizes
[i
] = cfg
-> Read(tmp
, m_Parser
-> m_FontsSizes
[i
]);
310 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
312 if (path
!= wxEmptyString
)
313 cfg
-> SetPath(oldpath
);
318 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
323 if (path
!= wxEmptyString
) {
324 oldpath
= cfg
-> GetPath();
325 cfg
-> SetPath(path
);
328 cfg
-> Write("wxHtmlWindow/Borders", (long) m_Borders
);
329 cfg
-> Write("wxHtmlWindow/FontFaceFixed", m_Parser
-> m_FontFaceFixed
);
330 cfg
-> Write("wxHtmlWindow/FontFaceNormal", m_Parser
-> m_FontFaceNormal
);
331 for (int i
= 0; i
< 7; i
++) {
332 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
333 cfg
-> Write(tmp
, (long) m_Parser
-> m_FontsSizes
[i
]);
336 if (path
!= wxEmptyString
)
337 cfg
-> SetPath(oldpath
);
342 bool wxHtmlWindow::HistoryBack()
346 if (m_HistoryPos
< 1) return FALSE
;
350 l
= m_History
[m_HistoryPos
].GetPage();
351 a
= m_History
[m_HistoryPos
].GetAnchor();
354 if (a
== wxEmptyString
) LoadPage(l
);
355 else LoadPage(l
+ "#" + a
);
359 Scroll(0, m_History
[m_HistoryPos
].GetPos());
366 bool wxHtmlWindow::HistoryForward()
370 if (m_HistoryPos
== -1) return FALSE
;
371 if (m_HistoryPos
>= (int)m_History
.GetCount() - 1)return FALSE
;
373 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
376 l
= m_History
[m_HistoryPos
].GetPage();
377 a
= m_History
[m_HistoryPos
].GetAnchor();
380 if (a
== wxEmptyString
) LoadPage(l
);
381 else LoadPage(l
+ "#" + a
);
385 Scroll(0, m_History
[m_HistoryPos
].GetPos());
392 void wxHtmlWindow::HistoryClear()
400 wxList
wxHtmlWindow::m_Filters
;
401 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
403 void wxHtmlWindow::CleanUpStatics()
405 if (m_DefaultFilter
) delete m_DefaultFilter
;
406 m_DefaultFilter
= NULL
;
407 m_Filters
.DeleteContents(TRUE
);
414 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
416 m_Filters
.Append(filter
);
422 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
424 LoadPage(link
.GetHref());
429 void wxHtmlWindow::OnDraw(wxDC
& dc
)
432 wxRegionIterator
upd(GetUpdateRegion()); // get the update rect list
435 if (m_tmpCanDrawLocks
> 0) return;
436 dc
.SetMapMode(wxMM_TEXT
);
437 #if defined(_MSC_VER) && (_MSC_VER == 1200)
438 ::SetMapMode((HDC
)dc
.GetHDC(), MM_TEXT
);
440 dc
.SetBackgroundMode(wxTRANSPARENT
);
446 if (m_Cell
) m_Cell
-> Draw(dc
, 0, 0, y
* wxHTML_SCROLL_STEP
+ v_y
, y
* wxHTML_SCROLL_STEP
+ v_h
+ v_y
);
454 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
456 wxScrolledWindow::OnSize(event
);
462 void wxHtmlWindow::OnKeyDown(wxKeyEvent
& event
)
467 ViewStart(&dummy
, &sty
);
468 GetClientSize(&dummy
, &cliy
); cliy
/= wxHTML_SCROLL_STEP
;
469 GetVirtualSize(&dummy
, &szy
); szy
/= wxHTML_SCROLL_STEP
;
471 switch (event
.KeyCode()) {
474 Scroll(-1, sty
- cliy
);
478 Scroll(-1, sty
+ cliy
);
484 Scroll(-1, szy
- cliy
);
497 void wxHtmlWindow::OnMouseEvent(wxMouseEvent
& event
)
499 m_tmpMouseMoved
= TRUE
;
501 if (event
.ButtonDown()) {
506 ViewStart(&sx
, &sy
); sx
*= wxHTML_SCROLL_STEP
; sy
*= wxHTML_SCROLL_STEP
;
507 pos
= event
.GetPosition();
510 m_Cell
-> OnMouseClick(this, sx
+ pos
.x
, sy
+ pos
.y
, event
);
516 void wxHtmlWindow::OnIdle(wxIdleEvent
& event
)
518 static wxCursor
cur_hand(wxCURSOR_HAND
), cur_arrow(wxCURSOR_ARROW
);
520 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
)) {
525 ViewStart(&sx
, &sy
); sx
*= wxHTML_SCROLL_STEP
; sy
*= wxHTML_SCROLL_STEP
;
526 wxGetMousePosition(&x
, &y
);
527 ScreenToClient(&x
, &y
);
528 lnk
= m_Cell
-> GetLink(sx
+ x
, sy
+ y
);
530 if (lnk
!= m_tmpLastLink
) {
532 SetCursor(cur_arrow
);
533 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(wxEmptyString
, m_RelatedStatusBar
);
537 if (m_RelatedStatusBar
!= -1)
538 m_RelatedFrame
-> SetStatusText(lnk
-> GetHref(), m_RelatedStatusBar
);
542 m_tmpMouseMoved
= FALSE
;
549 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
551 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
552 EVT_SIZE(wxHtmlWindow::OnSize
)
553 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent
)
554 EVT_MOTION(wxHtmlWindow::OnMouseEvent
)
555 EVT_IDLE(wxHtmlWindow::OnIdle
)
556 EVT_KEY_DOWN(wxHtmlWindow::OnKeyDown
)
563 // A module to allow initialization/cleanup
564 // without calling these functions from app.cpp or from
565 // the user's application.
567 class wxHtmlWinModule
: public wxModule
569 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
571 wxHtmlWinModule() : wxModule() {}
572 bool OnInit() { return TRUE
; }
573 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
576 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
581 ///// default mod handlers are forced there: