]>
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
;
49 m_FS
= new wxFileSystem();
50 m_RelatedStatusBar
= -1;
51 m_RelatedFrame
= NULL
;
53 m_OpenedPage
= m_OpenedAnchor
= 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
;
72 wxList
*parser_data
= m_Parser
-> GetTempData();
73 if (parser_data
) delete parser_data
;
81 void wxHtmlWindow::SetRelatedFrame(wxFrame
* frame
, const wxString
& format
)
83 m_RelatedFrame
= frame
;
84 m_TitleFormat
= format
;
89 void wxHtmlWindow::SetRelatedStatusBar(int bar
)
91 m_RelatedStatusBar
= bar
;
96 void wxHtmlWindow::SetFonts(wxString normal_face
, int normal_italic_mode
, wxString fixed_face
, int fixed_italic_mode
, int *sizes
)
98 m_Parser
-> SetFonts(normal_face
, normal_italic_mode
, fixed_face
, fixed_italic_mode
, sizes
);
99 if (!m_OpenedPage
.IsEmpty()) LoadPage(m_OpenedPage
);
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
= wxEmptyString
;
111 m_Parser
-> SetDC(dc
);
116 m_Cell
= (wxHtmlContainerCell
*) m_Parser
-> Parse(source
);
118 m_Cell
-> SetIndent(m_Borders
, HTML_INDENT_ALL
, HTML_UNITS_PIXELS
);
119 m_Cell
-> SetAlignHor(HTML_ALIGN_CENTER
);
126 bool wxHtmlWindow::LoadPage(const wxString
& location
)
132 m_tmpCanDraw
= FALSE
;
133 if (m_HistoryOn
&& (m_HistoryPos
!= -1)) { // store scroll position into history item
136 m_History
[m_HistoryPos
].SetPos(y
);
139 if (location
[0] == '#') { // local anchor
140 wxString anch
= location
.Mid(1) /*1 to end*/;
142 rt_val
= ScrollToAnchor(anch
);
147 if (m_RelatedStatusBar
!= -1) {
148 m_RelatedFrame
-> SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
152 f
= m_FS
-> OpenFile(location
);
156 err
.Printf(_("The browser is unable to open requested location :\n\n%s"), WXSTRINGCAST location
);
159 wxMessageBox(err
, "Error");
165 wxString src
= wxEmptyString
;
167 if (m_RelatedStatusBar
!= -1) {
168 wxString msg
= _("Loading : ") + location
;
169 m_RelatedFrame
-> SetStatusText(msg
, m_RelatedStatusBar
);
173 node
= m_Filters
.GetFirst();
175 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
-> GetData();
176 if (h
-> CanRead(*f
)) {
177 src
= h
-> ReadFile(*f
);
180 node
= node
-> GetNext();
182 if (src
== wxEmptyString
) {
183 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
184 src
= m_DefaultFilter
-> ReadFile(*f
);
187 m_FS
-> ChangePathTo(f
-> GetLocation());
188 rt_val
= SetPage(src
);
189 m_OpenedPage
= f
-> GetLocation();
190 if (f
-> GetAnchor() != wxEmptyString
) {
192 ScrollToAnchor(f
-> GetAnchor());
193 m_tmpCanDraw
= FALSE
;
198 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(_("Done"), m_RelatedStatusBar
);
202 if (m_HistoryOn
) { // add this page to history there:
203 int c
= m_History
.GetCount() - (m_HistoryPos
+ 1);
206 for (int i
= 0; i
< c
; i
++)
207 m_History
.Remove(m_HistoryPos
);
208 m_History
.Add(new HtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
218 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
220 const wxHtmlCell
*c
= m_Cell
-> Find(HTML_COND_ISANCHOR
, &anchor
);
221 if (!c
) return FALSE
;
225 for (y
= 0; c
!= NULL
; c
= c
-> GetParent()) y
+= c
-> GetPosY();
226 Scroll(-1, y
/ HTML_SCROLL_STEP
);
227 m_OpenedAnchor
= anchor
;
233 void wxHtmlWindow::SetTitle(const wxString
& title
)
235 if (m_RelatedFrame
) {
237 tit
.Printf(m_TitleFormat
, title
.c_str());
238 m_RelatedFrame
-> SetTitle(tit
);
246 void wxHtmlWindow::CreateLayout()
248 int ClientWidth
, ClientHeight
;
252 if (m_Style
== wxHW_SCROLLBAR_NEVER
) {
253 SetScrollbars(1, 1, 0, 0); // always off
254 GetClientSize(&ClientWidth
, &ClientHeight
);
255 m_Cell
-> Layout(ClientWidth
);
259 GetClientSize(&ClientWidth
, &ClientHeight
);
261 // VS : this looks extremely ugly under windoze, better fix needed!
262 SetScrollbars(1, 1, 0, ClientHeight
* 2); // always on
264 GetClientSize(&ClientWidth
, &ClientHeight
);
265 m_Cell
-> Layout(ClientWidth
);
266 if (ClientHeight
< m_Cell
-> GetHeight()) {
267 SetScrollbars(HTML_SCROLL_STEP
, HTML_SCROLL_STEP
,
268 m_Cell
-> GetWidth() / HTML_SCROLL_STEP
,
269 m_Cell
-> GetHeight() / HTML_SCROLL_STEP
270 /*cheat: top-level frag is always container*/);
272 else { /* we fit into window, no need for scrollbars */
273 SetScrollbars(1, 1, 0, 0); // disable...
274 GetClientSize(&ClientWidth
, &ClientHeight
);
275 m_Cell
-> Layout(ClientWidth
); // ...and relayout
282 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
287 if (path
!= wxEmptyString
) {
288 oldpath
= cfg
-> GetPath();
289 cfg
-> SetPath(path
);
292 m_Borders
= cfg
-> Read("wxHtmlWindow/Borders", m_Borders
);
293 m_Parser
-> m_FontFaceFixed
= cfg
-> Read("wxHtmlWindow/FontFaceFixed", m_Parser
-> m_FontFaceFixed
);
294 m_Parser
-> m_FontFaceNormal
= cfg
-> Read("wxHtmlWindow/FontFaceNormal", m_Parser
-> m_FontFaceNormal
);
295 m_Parser
-> m_ItalicModeFixed
= cfg
-> Read("wxHtmlWindow/ItalicModeFixed", m_Parser
-> m_ItalicModeFixed
);
296 m_Parser
-> m_ItalicModeNormal
= cfg
-> Read("wxHtmlWindow/ItalicModeNormal", m_Parser
-> m_ItalicModeNormal
);
297 for (int i
= 0; i
< 7; i
++) {
298 tmp
.Printf("wxHtmlWindow/FontsSize%i", i
);
299 m_Parser
-> m_FontsSizes
[i
] = cfg
-> Read(tmp
, m_Parser
-> m_FontsSizes
[i
]);
302 if (path
!= wxEmptyString
)
303 cfg
-> SetPath(oldpath
);
308 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
313 if (path
!= wxEmptyString
) {
314 oldpath
= cfg
-> GetPath();
315 cfg
-> SetPath(path
);
318 cfg
-> Write("wxHtmlWindow/Borders", (long) m_Borders
);
319 cfg
-> Write("wxHtmlWindow/FontFaceFixed", m_Parser
-> m_FontFaceFixed
);
320 cfg
-> Write("wxHtmlWindow/FontFaceNormal", m_Parser
-> m_FontFaceNormal
);
321 cfg
-> Write("wxHtmlWindow/ItalicModeFixed", (long) m_Parser
-> m_ItalicModeFixed
);
322 cfg
-> Write("wxHtmlWindow/ItalicModeNormal", (long) m_Parser
-> m_ItalicModeNormal
);
323 for (int i
= 0; i
< 7; i
++) {
324 tmp
.Printf("wxHtmlWindow/FontsSize%i", i
);
325 cfg
-> Write(tmp
, (long) m_Parser
-> m_FontsSizes
[i
]);
328 if (path
!= wxEmptyString
)
329 cfg
-> SetPath(oldpath
);
334 bool wxHtmlWindow::HistoryBack()
338 if (m_HistoryPos
< 1) return FALSE
;
342 l
= m_History
[m_HistoryPos
].GetPage();
343 a
= m_History
[m_HistoryPos
].GetAnchor();
345 if (a
== wxEmptyString
) LoadPage(l
);
346 else LoadPage(l
+ "#" + a
);
348 Scroll(0, m_History
[m_HistoryPos
].GetPos());
355 bool wxHtmlWindow::HistoryForward()
359 if (m_HistoryPos
== -1) return FALSE
;
360 if (m_HistoryPos
>= (int)m_History
.GetCount() - 1)return FALSE
;
362 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
365 l
= m_History
[m_HistoryPos
].GetPage();
366 a
= m_History
[m_HistoryPos
].GetAnchor();
368 if (a
== wxEmptyString
) LoadPage(l
);
369 else LoadPage(l
+ "#" + a
);
371 Scroll(0, m_History
[m_HistoryPos
].GetPos());
378 void wxHtmlWindow::HistoryClear()
386 wxList
wxHtmlWindow::m_Filters
;
387 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
389 void wxHtmlWindow::CleanUpStatics()
391 if (m_DefaultFilter
) delete m_DefaultFilter
;
392 m_DefaultFilter
= NULL
;
393 wxNode
* node
= m_Filters
.GetFirst();
394 m_Filters
.DeleteContents(TRUE
);
401 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
403 m_Filters
.Append(filter
);
409 void wxHtmlWindow::OnLinkClicked(const wxString
& link
)
416 void wxHtmlWindow::OnDraw(wxDC
& dc
)
419 wxRegionIterator
upd(GetUpdateRegion()); // get the update rect list
422 if (!m_tmpCanDraw
) return;
423 dc
.SetMapMode(wxMM_TEXT
);
424 #if defined(_MSC_VER) && (_MSC_VER == 1200)
425 ::SetMapMode((HDC
)dc
.GetHDC(), MM_TEXT
);
427 dc
.SetBackgroundMode(wxTRANSPARENT
);
433 if (m_Cell
) m_Cell
-> Draw(dc
, 0, 0, y
* HTML_SCROLL_STEP
+ v_y
, y
* HTML_SCROLL_STEP
+ v_h
+ v_y
);
441 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
443 wxScrolledWindow::OnSize(event
);
449 void wxHtmlWindow::OnKeyDown(wxKeyEvent
& event
)
454 ViewStart(&dummy
, &sty
);
455 GetClientSize(&dummy
, &cliy
); cliy
/= HTML_SCROLL_STEP
;
456 GetVirtualSize(&dummy
, &szy
); szy
/= HTML_SCROLL_STEP
;
458 switch (event
.KeyCode()) {
461 Scroll(-1, sty
- cliy
);
465 Scroll(-1, sty
+ cliy
);
471 Scroll(-1, szy
- cliy
);
484 void wxHtmlWindow::OnMouseEvent(wxMouseEvent
& event
)
486 m_tmpMouseMoved
= TRUE
;
488 if (event
.ButtonDown()) {
493 ViewStart(&sx
, &sy
); sx
*= HTML_SCROLL_STEP
; sy
*= HTML_SCROLL_STEP
;
494 pos
= event
.GetPosition();
497 m_Cell
-> OnMouseClick(this, sx
+ pos
.x
, sy
+ pos
.y
, event
.ButtonDown(1), event
.ButtonDown(2), event
.ButtonDown(3));
503 void wxHtmlWindow::OnIdle(wxIdleEvent
& event
)
505 static wxCursor
cur_hand(wxCURSOR_HAND
), cur_arrow(wxCURSOR_ARROW
);
507 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
)) {
512 ViewStart(&sx
, &sy
); sx
*= HTML_SCROLL_STEP
; sy
*= HTML_SCROLL_STEP
;
513 wxGetMousePosition(&x
, &y
);
514 ScreenToClient(&x
, &y
);
515 lnk
= m_Cell
-> GetLink(sx
+ x
, sy
+ y
);
517 if (lnk
== wxEmptyString
) {
518 SetCursor(cur_arrow
);
519 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(wxEmptyString
, m_RelatedStatusBar
);
523 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(lnk
, m_RelatedStatusBar
);
525 m_tmpMouseMoved
= FALSE
;
532 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
534 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
535 EVT_SIZE(wxHtmlWindow::OnSize
)
536 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent
)
537 EVT_MOTION(wxHtmlWindow::OnMouseEvent
)
538 EVT_IDLE(wxHtmlWindow::OnIdle
)
539 EVT_KEY_DOWN(wxHtmlWindow::OnKeyDown
)
546 // A module to allow initialization/cleanup
547 // without calling these functions from app.cpp or from
548 // the user's application.
550 class wxHtmlWinModule
: public wxModule
552 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
554 wxHtmlWinModule() : wxModule() {}
555 bool OnInit() { return TRUE
; }
556 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
559 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
564 ///// default mod handlers are forced there:
566 FORCE_LINK(mod_layout
)
567 FORCE_LINK(mod_fonts
)
568 FORCE_LINK(mod_image
)
571 FORCE_LINK(mod_hline
)
572 FORCE_LINK(mod_links
)
573 FORCE_LINK(mod_tables
)