]>
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
5 // Copyright: (c) 1999 Vaclav Slavik
6 // Licence: wxWindows Licence
7 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation
14 #include <wx/wxprec.h>
27 #include <wx/html/htmlwin.h>
29 #include <wx/html/forcelink.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
, name
)
46 m_tmpMouseMoved
= FALSE
;
48 m_FS
= new wxFileSystem();
49 m_RelatedStatusBar
= -1;
50 m_RelatedFrame
= NULL
;
52 m_OpenedPage
= m_OpenedAnchor
= wxEmptyString
;
54 m_Parser
= new wxHtmlWinParser(this);
55 m_Parser
-> SetFS(m_FS
);
60 SetPage("<html><body></body></html>");
65 wxHtmlWindow::~wxHtmlWindow()
69 if (m_Cell
) delete m_Cell
;
71 wxList
*parser_data
= m_Parser
-> GetTempData();
72 if (parser_data
) delete parser_data
;
80 void wxHtmlWindow::SetRelatedFrame(wxFrame
* frame
, const wxString
& format
)
82 m_RelatedFrame
= frame
;
83 m_TitleFormat
= format
;
88 void wxHtmlWindow::SetRelatedStatusBar(int bar
)
90 m_RelatedStatusBar
= bar
;
95 void wxHtmlWindow::SetFonts(wxString normal_face
, int normal_italic_mode
, wxString fixed_face
, int fixed_italic_mode
, int *sizes
)
97 m_Parser
-> SetFonts(normal_face
, normal_italic_mode
, fixed_face
, fixed_italic_mode
, sizes
);
98 if (!m_OpenedPage
.IsEmpty()) LoadPage(m_OpenedPage
);
103 bool wxHtmlWindow::SetPage(const wxString
& source
)
105 wxClientDC
*dc
= new wxClientDC(this);
107 dc
-> SetMapMode(wxMM_TEXT
);
108 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
109 m_OpenedPage
= m_OpenedAnchor
= wxEmptyString
;
110 m_Parser
-> SetDC(dc
);
115 m_Cell
= (wxHtmlContainerCell
*) m_Parser
-> Parse(source
);
117 m_Cell
-> SetIndent(m_Borders
, HTML_INDENT_ALL
, HTML_UNITS_PIXELS
);
118 m_Cell
-> SetAlignHor(HTML_ALIGN_CENTER
);
125 bool wxHtmlWindow::LoadPage(const wxString
& location
)
131 m_tmpCanDraw
= FALSE
;
132 if (m_HistoryOn
&& (m_HistoryPos
!= -1)) { // store scroll position into history item
135 m_History
[m_HistoryPos
].SetPos(y
);
138 if (location
[0] == '#') { // local anchor
139 wxString anch
= location
.Mid(1) /*1 to end*/;
141 rt_val
= ScrollToAnchor(anch
);
146 if (m_RelatedStatusBar
!= -1) {
147 m_RelatedFrame
-> SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
151 f
= m_FS
-> OpenFile(location
);
155 err
.Printf(_("The browser is unable to open requested location :\n\n%s"), WXSTRINGCAST location
);
158 wxMessageBox(err
, "Error");
164 wxString src
= wxEmptyString
;
166 if (m_RelatedStatusBar
!= -1) {
167 wxString msg
= _("Loading : ") + location
;
168 m_RelatedFrame
-> SetStatusText(msg
, m_RelatedStatusBar
);
172 node
= m_Filters
.GetFirst();
174 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
-> GetData();
175 if (h
-> CanRead(*f
)) {
176 src
= h
-> ReadFile(*f
);
179 node
= node
-> GetNext();
181 if (src
== wxEmptyString
) {
182 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
183 src
= m_DefaultFilter
-> ReadFile(*f
);
186 m_FS
-> ChangePathTo(f
-> GetLocation());
187 rt_val
= SetPage(src
);
188 m_OpenedPage
= f
-> GetLocation();
189 if (f
-> GetAnchor() != wxEmptyString
) {
191 ScrollToAnchor(f
-> GetAnchor());
192 m_tmpCanDraw
= FALSE
;
197 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(_("Done"), m_RelatedStatusBar
);
201 if (m_HistoryOn
) { // add this page to history there:
202 int c
= m_History
.GetCount() - (m_HistoryPos
+ 1);
205 for (int i
= 0; i
< c
; i
++)
206 m_History
.Remove(m_HistoryPos
);
207 m_History
.Add(new HtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
217 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
219 const wxHtmlCell
*c
= m_Cell
-> Find(HTML_COND_ISANCHOR
, &anchor
);
220 if (!c
) return FALSE
;
224 for (y
= 0; c
!= NULL
; c
= c
-> GetParent()) y
+= c
-> GetPosY();
225 Scroll(-1, y
/ HTML_SCROLL_STEP
);
226 m_OpenedAnchor
= anchor
;
232 void wxHtmlWindow::SetTitle(const wxString
& title
)
234 if (m_RelatedFrame
) {
236 tit
.Printf(m_TitleFormat
, title
.c_str());
237 m_RelatedFrame
-> SetTitle(tit
);
245 void wxHtmlWindow::CreateLayout()
247 int ClientWidth
, ClientHeight
;
251 if (m_Style
== wxHW_SCROLLBAR_NEVER
) {
252 GetClientSize(&ClientWidth
, &ClientHeight
);
253 m_Cell
-> Layout(ClientWidth
);
257 GetClientSize(&ClientWidth
, &ClientHeight
);
258 SetScrollbars(1, 1, 0, ClientHeight
* 2); // always on
259 GetClientSize(&ClientWidth
, &ClientHeight
);
260 m_Cell
-> Layout(ClientWidth
);
261 GetClientSize(&ClientWidth
, &ClientHeight
);
262 if (ClientHeight
< m_Cell
-> GetHeight()) {
263 SetScrollbars(HTML_SCROLL_STEP
, HTML_SCROLL_STEP
,
264 m_Cell
-> GetWidth() / HTML_SCROLL_STEP
,
265 m_Cell
-> GetHeight() / HTML_SCROLL_STEP
266 /*cheat: top-level frag is always container*/);
268 else { /* we fit into window, no need for scrollbars */
269 SetScrollbars(1, 1, 0, 0); // disable...
270 m_Cell
-> Layout(ClientWidth
); // ...and relayout
277 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
282 if (path
!= wxEmptyString
) {
283 oldpath
= cfg
-> GetPath();
284 cfg
-> SetPath(path
);
287 m_Borders
= cfg
-> Read("wxHtmlWindow/Borders", m_Borders
);
288 m_Parser
-> m_FontFaceFixed
= cfg
-> Read("wxHtmlWindow/FontFaceFixed", m_Parser
-> m_FontFaceFixed
);
289 m_Parser
-> m_FontFaceNormal
= cfg
-> Read("wxHtmlWindow/FontFaceNormal", m_Parser
-> m_FontFaceNormal
);
290 m_Parser
-> m_ItalicModeFixed
= cfg
-> Read("wxHtmlWindow/ItalicModeFixed", m_Parser
-> m_ItalicModeFixed
);
291 m_Parser
-> m_ItalicModeNormal
= cfg
-> Read("wxHtmlWindow/ItalicModeNormal", m_Parser
-> m_ItalicModeNormal
);
292 for (int i
= 0; i
< 7; i
++) {
293 tmp
.Printf("wxHtmlWindow/FontsSize%i", i
);
294 m_Parser
-> m_FontsSizes
[i
] = cfg
-> Read(tmp
, m_Parser
-> m_FontsSizes
[i
]);
297 if (path
!= wxEmptyString
)
298 cfg
-> SetPath(oldpath
);
303 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
308 if (path
!= wxEmptyString
) {
309 oldpath
= cfg
-> GetPath();
310 cfg
-> SetPath(path
);
313 cfg
-> Write("wxHtmlWindow/Borders", (long) m_Borders
);
314 cfg
-> Write("wxHtmlWindow/FontFaceFixed", m_Parser
-> m_FontFaceFixed
);
315 cfg
-> Write("wxHtmlWindow/FontFaceNormal", m_Parser
-> m_FontFaceNormal
);
316 cfg
-> Write("wxHtmlWindow/ItalicModeFixed", (long) m_Parser
-> m_ItalicModeFixed
);
317 cfg
-> Write("wxHtmlWindow/ItalicModeNormal", (long) m_Parser
-> m_ItalicModeNormal
);
318 for (int i
= 0; i
< 7; i
++) {
319 tmp
.Printf("wxHtmlWindow/FontsSize%i", i
);
320 cfg
-> Write(tmp
, (long) m_Parser
-> m_FontsSizes
[i
]);
323 if (path
!= wxEmptyString
)
324 cfg
-> SetPath(oldpath
);
329 bool wxHtmlWindow::HistoryBack()
333 if (m_HistoryPos
< 1) return FALSE
;
337 l
= m_History
[m_HistoryPos
].GetPage();
338 a
= m_History
[m_HistoryPos
].GetAnchor();
340 if (a
== wxEmptyString
) LoadPage(l
);
341 else LoadPage(l
+ "#" + a
);
343 Scroll(0, m_History
[m_HistoryPos
].GetPos());
350 bool wxHtmlWindow::HistoryForward()
354 if (m_HistoryPos
== -1) return FALSE
;
355 if (m_HistoryPos
>= (int)m_History
.GetCount() - 1)return FALSE
;
357 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
360 l
= m_History
[m_HistoryPos
].GetPage();
361 a
= m_History
[m_HistoryPos
].GetAnchor();
363 if (a
== wxEmptyString
) LoadPage(l
);
364 else LoadPage(l
+ "#" + a
);
366 Scroll(0, m_History
[m_HistoryPos
].GetPos());
373 void wxHtmlWindow::HistoryClear()
381 wxList
wxHtmlWindow::m_Filters
;
382 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
384 void wxHtmlWindow::CleanUpStatics()
386 if (m_DefaultFilter
) delete m_DefaultFilter
;
387 m_DefaultFilter
= NULL
;
392 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
394 m_Filters
.DeleteContents(TRUE
);
395 m_Filters
.Append(filter
);
401 void wxHtmlWindow::OnLinkClicked(const wxString
& link
)
408 void wxHtmlWindow::OnDraw(wxDC
& dc
)
411 wxRegionIterator
upd(GetUpdateRegion()); // get the update rect list
414 if (!m_tmpCanDraw
) return;
415 dc
.SetMapMode(wxMM_TEXT
);
416 #if defined(_MSC_VER) && (_MSC_VER == 1200)
417 ::SetMapMode((HDC
)dc
.GetHDC(), MM_TEXT
);
419 dc
.SetBackgroundMode(wxTRANSPARENT
);
425 if (m_Cell
) m_Cell
-> Draw(dc
, 0, 0, y
* HTML_SCROLL_STEP
+ v_y
, y
* HTML_SCROLL_STEP
+ v_h
+ v_y
);
433 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
435 wxScrolledWindow::OnSize(event
);
441 void wxHtmlWindow::OnKeyDown(wxKeyEvent
& event
)
446 ViewStart(&dummy
, &sty
);
447 GetClientSize(&dummy
, &cliy
); cliy
/= HTML_SCROLL_STEP
;
448 GetVirtualSize(&dummy
, &szy
); szy
/= HTML_SCROLL_STEP
;
450 switch (event
.KeyCode()) {
453 Scroll(-1, sty
- cliy
);
457 Scroll(-1, sty
+ cliy
);
463 Scroll(-1, szy
- cliy
);
476 void wxHtmlWindow::OnMouseEvent(wxMouseEvent
& event
)
478 m_tmpMouseMoved
= TRUE
;
480 if (event
.ButtonDown()) {
485 ViewStart(&sx
, &sy
); sx
*= HTML_SCROLL_STEP
; sy
*= HTML_SCROLL_STEP
;
486 pos
= event
.GetPosition();
489 m_Cell
-> OnMouseClick(this, sx
+ pos
.x
, sy
+ pos
.y
, event
.ButtonDown(1), event
.ButtonDown(2), event
.ButtonDown(3));
495 void wxHtmlWindow::OnIdle(wxIdleEvent
& event
)
497 static wxCursor
cur_hand(wxCURSOR_HAND
), cur_arrow(wxCURSOR_ARROW
);
499 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
)) {
504 ViewStart(&sx
, &sy
); sx
*= HTML_SCROLL_STEP
; sy
*= HTML_SCROLL_STEP
;
505 wxGetMousePosition(&x
, &y
);
506 ScreenToClient(&x
, &y
);
507 lnk
= m_Cell
-> GetLink(sx
+ x
, sy
+ y
);
509 if (lnk
== wxEmptyString
) {
510 SetCursor(cur_arrow
);
511 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(wxEmptyString
, m_RelatedStatusBar
);
515 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(lnk
, m_RelatedStatusBar
);
517 m_tmpMouseMoved
= FALSE
;
524 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
526 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
527 EVT_SIZE(wxHtmlWindow::OnSize
)
528 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent
)
529 EVT_MOTION(wxHtmlWindow::OnMouseEvent
)
530 EVT_IDLE(wxHtmlWindow::OnIdle
)
531 EVT_KEY_DOWN(wxHtmlWindow::OnKeyDown
)
538 // A module to allow initialization/cleanup
539 // without calling these functions from app.cpp or from
540 // the user's application.
542 class wxHtmlWinModule
: public wxModule
544 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
546 wxHtmlWinModule() : wxModule() {}
547 bool OnInit() { return TRUE
; }
548 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
551 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
556 ///// default mod handlers are forced there:
558 FORCE_LINK(mod_layout
)
559 FORCE_LINK(mod_fonts
)
560 FORCE_LINK(mod_image
)
563 FORCE_LINK(mod_hline
)
564 FORCE_LINK(mod_links
)
565 FORCE_LINK(mod_tables
)