]>
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"
30 #include "wx/html/forcelnk.h"
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
40 #include "wx/arrimpl.cpp"
41 WX_DEFINE_OBJARRAY(HtmlHistoryArray
)
44 wxHtmlWindow::wxHtmlWindow(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
45 long style
, const wxString
& name
) : wxScrolledWindow(parent
, id
, pos
, size
, wxVSCROLL
, name
)
47 m_tmpMouseMoved
= FALSE
;
48 m_tmpLastLink
= wxEmptyString
;
50 m_FS
= new wxFileSystem();
51 m_RelatedStatusBar
= -1;
52 m_RelatedFrame
= NULL
;
54 m_OpenedPage
= m_OpenedAnchor
= wxEmptyString
;
56 m_Parser
= new wxHtmlWinParser(this);
57 m_Parser
-> SetFS(m_FS
);
62 SetPage("<html><body></body></html>");
67 wxHtmlWindow::~wxHtmlWindow()
71 if (m_Cell
) delete m_Cell
;
73 wxList
*parser_data
= m_Parser
-> GetTempData();
74 if (parser_data
) delete parser_data
;
82 void wxHtmlWindow::SetRelatedFrame(wxFrame
* frame
, const wxString
& format
)
84 m_RelatedFrame
= frame
;
85 m_TitleFormat
= format
;
90 void wxHtmlWindow::SetRelatedStatusBar(int bar
)
92 m_RelatedStatusBar
= bar
;
97 void wxHtmlWindow::SetFonts(wxString normal_face
, int normal_italic_mode
, wxString fixed_face
, int fixed_italic_mode
, const int *sizes
)
99 m_Parser
-> SetFonts(normal_face
, normal_italic_mode
, fixed_face
, fixed_italic_mode
, sizes
);
100 if (!m_OpenedPage
.IsEmpty()) LoadPage(m_OpenedPage
);
105 bool wxHtmlWindow::SetPage(const wxString
& source
)
107 wxClientDC
*dc
= new wxClientDC(this);
109 dc
-> SetMapMode(wxMM_TEXT
);
110 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
111 m_OpenedPage
= m_OpenedAnchor
= wxEmptyString
;
112 m_Parser
-> SetDC(dc
);
117 m_Cell
= (wxHtmlContainerCell
*) m_Parser
-> Parse(source
);
119 m_Cell
-> SetIndent(m_Borders
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
120 m_Cell
-> SetAlignHor(wxHTML_ALIGN_CENTER
);
127 bool wxHtmlWindow::LoadPage(const wxString
& location
)
132 SetCursor(*wxHOURGLASS_CURSOR
);
135 m_tmpCanDraw
= 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] == '#') { // local anchor
143 wxString anch
= location
.Mid(1) /*1 to end*/;
145 rt_val
= ScrollToAnchor(anch
);
150 if (m_RelatedStatusBar
!= -1) {
151 m_RelatedFrame
-> SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
155 f
= m_FS
-> OpenFile(location
);
159 err
.Printf(_("The browser is unable to open requested location :\n\n%s"), WXSTRINGCAST location
);
162 wxMessageBox(err
, "Error");
164 SetCursor(*wxSTANDARD_CURSOR
);
170 wxString src
= wxEmptyString
;
172 if (m_RelatedStatusBar
!= -1) {
173 wxString msg
= _("Loading : ") + location
;
174 m_RelatedFrame
-> SetStatusText(msg
, m_RelatedStatusBar
);
178 node
= m_Filters
.GetFirst();
180 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
-> GetData();
181 if (h
-> CanRead(*f
)) {
182 src
= h
-> ReadFile(*f
);
185 node
= node
-> GetNext();
187 if (src
== wxEmptyString
) {
188 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
189 src
= m_DefaultFilter
-> ReadFile(*f
);
192 m_FS
-> ChangePathTo(f
-> GetLocation());
193 rt_val
= SetPage(src
);
194 m_OpenedPage
= f
-> GetLocation();
195 if (f
-> GetAnchor() != wxEmptyString
) {
197 ScrollToAnchor(f
-> GetAnchor());
198 m_tmpCanDraw
= FALSE
;
203 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(_("Done"), m_RelatedStatusBar
);
207 if (m_HistoryOn
) { // add this page to history there:
208 int c
= m_History
.GetCount() - (m_HistoryPos
+ 1);
211 for (int i
= 0; i
< c
; i
++)
212 m_History
.Remove(m_HistoryPos
);
213 m_History
.Add(new HtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
216 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::SetTitle(const wxString
& title
)
242 if (m_RelatedFrame
) {
244 tit
.Printf(m_TitleFormat
, title
.c_str());
245 m_RelatedFrame
-> SetTitle(tit
);
253 void wxHtmlWindow::CreateLayout()
255 int ClientWidth
, ClientHeight
;
259 if (m_Style
== wxHW_SCROLLBAR_NEVER
) {
260 SetScrollbars(1, 1, 0, 0); // always off
261 GetClientSize(&ClientWidth
, &ClientHeight
);
262 m_Cell
-> Layout(ClientWidth
);
266 GetClientSize(&ClientWidth
, &ClientHeight
);
268 // VS : this looks extremely ugly under windoze, better fix needed!
269 SetScrollbars(1, 1, 0, ClientHeight
* 2); // always on
271 GetClientSize(&ClientWidth
, &ClientHeight
);
272 m_Cell
-> Layout(ClientWidth
);
273 if (ClientHeight
< m_Cell
-> GetHeight()) {
274 SetScrollbars(wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
275 m_Cell
-> GetWidth() / wxHTML_SCROLL_STEP
,
276 m_Cell
-> GetHeight() / wxHTML_SCROLL_STEP
277 /*cheat: top-level frag is always container*/);
279 else { /* we fit into window, no need for scrollbars */
280 SetScrollbars(1, 1, 0, 0); // disable...
281 GetClientSize(&ClientWidth
, &ClientHeight
);
282 m_Cell
-> Layout(ClientWidth
); // ...and relayout
289 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
294 if (path
!= wxEmptyString
) {
295 oldpath
= cfg
-> GetPath();
296 cfg
-> SetPath(path
);
299 m_Borders
= cfg
-> Read("wxHtmlWindow/Borders", m_Borders
);
300 m_Parser
-> m_FontFaceFixed
= cfg
-> Read("wxHtmlWindow/FontFaceFixed", m_Parser
-> m_FontFaceFixed
);
301 m_Parser
-> m_FontFaceNormal
= cfg
-> Read("wxHtmlWindow/FontFaceNormal", m_Parser
-> m_FontFaceNormal
);
302 m_Parser
-> m_ItalicModeFixed
= cfg
-> Read("wxHtmlWindow/ItalicModeFixed", m_Parser
-> m_ItalicModeFixed
);
303 m_Parser
-> m_ItalicModeNormal
= cfg
-> Read("wxHtmlWindow/ItalicModeNormal", m_Parser
-> m_ItalicModeNormal
);
304 for (int i
= 0; i
< 7; i
++) {
305 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
306 m_Parser
-> m_FontsSizes
[i
] = cfg
-> Read(tmp
, m_Parser
-> m_FontsSizes
[i
]);
309 if (path
!= wxEmptyString
)
310 cfg
-> SetPath(oldpath
);
315 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
320 if (path
!= wxEmptyString
) {
321 oldpath
= cfg
-> GetPath();
322 cfg
-> SetPath(path
);
325 cfg
-> Write("wxHtmlWindow/Borders", (long) m_Borders
);
326 cfg
-> Write("wxHtmlWindow/FontFaceFixed", m_Parser
-> m_FontFaceFixed
);
327 cfg
-> Write("wxHtmlWindow/FontFaceNormal", m_Parser
-> m_FontFaceNormal
);
328 cfg
-> Write("wxHtmlWindow/ItalicModeFixed", (long) m_Parser
-> m_ItalicModeFixed
);
329 cfg
-> Write("wxHtmlWindow/ItalicModeNormal", (long) m_Parser
-> m_ItalicModeNormal
);
330 for (int i
= 0; i
< 7; i
++) {
331 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
332 cfg
-> Write(tmp
, (long) m_Parser
-> m_FontsSizes
[i
]);
335 if (path
!= wxEmptyString
)
336 cfg
-> SetPath(oldpath
);
341 bool wxHtmlWindow::HistoryBack()
345 if (m_HistoryPos
< 1) return FALSE
;
349 l
= m_History
[m_HistoryPos
].GetPage();
350 a
= m_History
[m_HistoryPos
].GetAnchor();
352 if (a
== wxEmptyString
) LoadPage(l
);
353 else LoadPage(l
+ "#" + a
);
355 Scroll(0, m_History
[m_HistoryPos
].GetPos());
362 bool wxHtmlWindow::HistoryForward()
366 if (m_HistoryPos
== -1) return FALSE
;
367 if (m_HistoryPos
>= (int)m_History
.GetCount() - 1)return FALSE
;
369 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
372 l
= m_History
[m_HistoryPos
].GetPage();
373 a
= m_History
[m_HistoryPos
].GetAnchor();
375 if (a
== wxEmptyString
) LoadPage(l
);
376 else LoadPage(l
+ "#" + a
);
378 Scroll(0, m_History
[m_HistoryPos
].GetPos());
385 void wxHtmlWindow::HistoryClear()
393 wxList
wxHtmlWindow::m_Filters
;
394 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
396 void wxHtmlWindow::CleanUpStatics()
398 if (m_DefaultFilter
) delete m_DefaultFilter
;
399 m_DefaultFilter
= NULL
;
400 m_Filters
.DeleteContents(TRUE
);
407 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
409 m_Filters
.Append(filter
);
415 void wxHtmlWindow::OnLinkClicked(const wxString
& link
)
422 void wxHtmlWindow::OnDraw(wxDC
& dc
)
425 wxRegionIterator
upd(GetUpdateRegion()); // get the update rect list
428 if (!m_tmpCanDraw
) return;
429 dc
.SetMapMode(wxMM_TEXT
);
430 #if defined(_MSC_VER) && (_MSC_VER == 1200)
431 ::SetMapMode((HDC
)dc
.GetHDC(), MM_TEXT
);
433 dc
.SetBackgroundMode(wxTRANSPARENT
);
439 if (m_Cell
) m_Cell
-> Draw(dc
, 0, 0, y
* wxHTML_SCROLL_STEP
+ v_y
, y
* wxHTML_SCROLL_STEP
+ v_h
+ v_y
);
447 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
449 wxScrolledWindow::OnSize(event
);
455 void wxHtmlWindow::OnKeyDown(wxKeyEvent
& event
)
460 ViewStart(&dummy
, &sty
);
461 GetClientSize(&dummy
, &cliy
); cliy
/= wxHTML_SCROLL_STEP
;
462 GetVirtualSize(&dummy
, &szy
); szy
/= wxHTML_SCROLL_STEP
;
464 switch (event
.KeyCode()) {
467 Scroll(-1, sty
- cliy
);
471 Scroll(-1, sty
+ cliy
);
477 Scroll(-1, szy
- cliy
);
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
.ButtonDown(1), event
.ButtonDown(2), event
.ButtonDown(3));
509 void wxHtmlWindow::OnIdle(wxIdleEvent
& event
)
511 static wxCursor
cur_hand(wxCURSOR_HAND
), cur_arrow(wxCURSOR_ARROW
);
513 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
)) {
518 ViewStart(&sx
, &sy
); sx
*= wxHTML_SCROLL_STEP
; sy
*= wxHTML_SCROLL_STEP
;
519 wxGetMousePosition(&x
, &y
);
520 ScreenToClient(&x
, &y
);
521 lnk
= m_Cell
-> GetLink(sx
+ x
, sy
+ y
);
523 if (lnk
!= m_tmpLastLink
) {
524 if (lnk
== wxEmptyString
) {
525 SetCursor(cur_arrow
);
526 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(wxEmptyString
, m_RelatedStatusBar
);
530 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(lnk
, m_RelatedStatusBar
);
534 m_tmpMouseMoved
= FALSE
;
541 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
543 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
544 EVT_SIZE(wxHtmlWindow::OnSize
)
545 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent
)
546 EVT_MOTION(wxHtmlWindow::OnMouseEvent
)
547 EVT_IDLE(wxHtmlWindow::OnIdle
)
548 EVT_KEY_DOWN(wxHtmlWindow::OnKeyDown
)
555 // A module to allow initialization/cleanup
556 // without calling these functions from app.cpp or from
557 // the user's application.
559 class wxHtmlWinModule
: public wxModule
561 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
563 wxHtmlWinModule() : wxModule() {}
564 bool OnInit() { return TRUE
; }
565 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
568 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
573 ///// default mod handlers are forced there:
575 FORCE_LINK(mod_layout
)
576 FORCE_LINK(mod_fonts
)
577 FORCE_LINK(mod_image
)
580 FORCE_LINK(mod_hline
)
581 FORCE_LINK(mod_links
)
582 FORCE_LINK(mod_tables
)