]>
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 SetScrollbars(1, 1, 0, 0); // always off
253 GetClientSize(&ClientWidth
, &ClientHeight
);
254 m_Cell
-> Layout(ClientWidth
);
258 GetClientSize(&ClientWidth
, &ClientHeight
);
260 // VS : this looks extremely ugly under windoze, better fix needed!
261 SetScrollbars(1, 1, 0, ClientHeight
* 2); // always on
263 GetClientSize(&ClientWidth
, &ClientHeight
);
264 m_Cell
-> Layout(ClientWidth
);
265 if (ClientHeight
< m_Cell
-> GetHeight()) {
266 SetScrollbars(HTML_SCROLL_STEP
, HTML_SCROLL_STEP
,
267 m_Cell
-> GetWidth() / HTML_SCROLL_STEP
,
268 m_Cell
-> GetHeight() / HTML_SCROLL_STEP
269 /*cheat: top-level frag is always container*/);
271 else { /* we fit into window, no need for scrollbars */
272 SetScrollbars(1, 1, 0, 0); // disable...
273 GetClientSize(&ClientWidth
, &ClientHeight
);
274 m_Cell
-> Layout(ClientWidth
); // ...and relayout
281 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
286 if (path
!= wxEmptyString
) {
287 oldpath
= cfg
-> GetPath();
288 cfg
-> SetPath(path
);
291 m_Borders
= cfg
-> Read("wxHtmlWindow/Borders", m_Borders
);
292 m_Parser
-> m_FontFaceFixed
= cfg
-> Read("wxHtmlWindow/FontFaceFixed", m_Parser
-> m_FontFaceFixed
);
293 m_Parser
-> m_FontFaceNormal
= cfg
-> Read("wxHtmlWindow/FontFaceNormal", m_Parser
-> m_FontFaceNormal
);
294 m_Parser
-> m_ItalicModeFixed
= cfg
-> Read("wxHtmlWindow/ItalicModeFixed", m_Parser
-> m_ItalicModeFixed
);
295 m_Parser
-> m_ItalicModeNormal
= cfg
-> Read("wxHtmlWindow/ItalicModeNormal", m_Parser
-> m_ItalicModeNormal
);
296 for (int i
= 0; i
< 7; i
++) {
297 tmp
.Printf("wxHtmlWindow/FontsSize%i", i
);
298 m_Parser
-> m_FontsSizes
[i
] = cfg
-> Read(tmp
, m_Parser
-> m_FontsSizes
[i
]);
301 if (path
!= wxEmptyString
)
302 cfg
-> SetPath(oldpath
);
307 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
312 if (path
!= wxEmptyString
) {
313 oldpath
= cfg
-> GetPath();
314 cfg
-> SetPath(path
);
317 cfg
-> Write("wxHtmlWindow/Borders", (long) m_Borders
);
318 cfg
-> Write("wxHtmlWindow/FontFaceFixed", m_Parser
-> m_FontFaceFixed
);
319 cfg
-> Write("wxHtmlWindow/FontFaceNormal", m_Parser
-> m_FontFaceNormal
);
320 cfg
-> Write("wxHtmlWindow/ItalicModeFixed", (long) m_Parser
-> m_ItalicModeFixed
);
321 cfg
-> Write("wxHtmlWindow/ItalicModeNormal", (long) m_Parser
-> m_ItalicModeNormal
);
322 for (int i
= 0; i
< 7; i
++) {
323 tmp
.Printf("wxHtmlWindow/FontsSize%i", i
);
324 cfg
-> Write(tmp
, (long) m_Parser
-> m_FontsSizes
[i
]);
327 if (path
!= wxEmptyString
)
328 cfg
-> SetPath(oldpath
);
333 bool wxHtmlWindow::HistoryBack()
337 if (m_HistoryPos
< 1) return FALSE
;
341 l
= m_History
[m_HistoryPos
].GetPage();
342 a
= m_History
[m_HistoryPos
].GetAnchor();
344 if (a
== wxEmptyString
) LoadPage(l
);
345 else LoadPage(l
+ "#" + a
);
347 Scroll(0, m_History
[m_HistoryPos
].GetPos());
354 bool wxHtmlWindow::HistoryForward()
358 if (m_HistoryPos
== -1) return FALSE
;
359 if (m_HistoryPos
>= (int)m_History
.GetCount() - 1)return FALSE
;
361 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
364 l
= m_History
[m_HistoryPos
].GetPage();
365 a
= m_History
[m_HistoryPos
].GetAnchor();
367 if (a
== wxEmptyString
) LoadPage(l
);
368 else LoadPage(l
+ "#" + a
);
370 Scroll(0, m_History
[m_HistoryPos
].GetPos());
377 void wxHtmlWindow::HistoryClear()
385 wxList
wxHtmlWindow::m_Filters
;
386 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
388 void wxHtmlWindow::CleanUpStatics()
390 if (m_DefaultFilter
) delete m_DefaultFilter
;
391 m_DefaultFilter
= NULL
;
392 wxNode
* node
= m_Filters
.GetFirst();
393 m_Filters
.DeleteContents(TRUE
);
400 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
402 m_Filters
.Append(filter
);
408 void wxHtmlWindow::OnLinkClicked(const wxString
& link
)
415 void wxHtmlWindow::OnDraw(wxDC
& dc
)
418 wxRegionIterator
upd(GetUpdateRegion()); // get the update rect list
421 if (!m_tmpCanDraw
) return;
422 dc
.SetMapMode(wxMM_TEXT
);
423 #if defined(_MSC_VER) && (_MSC_VER == 1200)
424 ::SetMapMode((HDC
)dc
.GetHDC(), MM_TEXT
);
426 dc
.SetBackgroundMode(wxTRANSPARENT
);
432 if (m_Cell
) m_Cell
-> Draw(dc
, 0, 0, y
* HTML_SCROLL_STEP
+ v_y
, y
* HTML_SCROLL_STEP
+ v_h
+ v_y
);
440 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
442 wxScrolledWindow::OnSize(event
);
448 void wxHtmlWindow::OnKeyDown(wxKeyEvent
& event
)
453 ViewStart(&dummy
, &sty
);
454 GetClientSize(&dummy
, &cliy
); cliy
/= HTML_SCROLL_STEP
;
455 GetVirtualSize(&dummy
, &szy
); szy
/= HTML_SCROLL_STEP
;
457 switch (event
.KeyCode()) {
460 Scroll(-1, sty
- cliy
);
464 Scroll(-1, sty
+ cliy
);
470 Scroll(-1, szy
- cliy
);
483 void wxHtmlWindow::OnMouseEvent(wxMouseEvent
& event
)
485 m_tmpMouseMoved
= TRUE
;
487 if (event
.ButtonDown()) {
492 ViewStart(&sx
, &sy
); sx
*= HTML_SCROLL_STEP
; sy
*= HTML_SCROLL_STEP
;
493 pos
= event
.GetPosition();
496 m_Cell
-> OnMouseClick(this, sx
+ pos
.x
, sy
+ pos
.y
, event
.ButtonDown(1), event
.ButtonDown(2), event
.ButtonDown(3));
502 void wxHtmlWindow::OnIdle(wxIdleEvent
& event
)
504 static wxCursor
cur_hand(wxCURSOR_HAND
), cur_arrow(wxCURSOR_ARROW
);
506 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
)) {
511 ViewStart(&sx
, &sy
); sx
*= HTML_SCROLL_STEP
; sy
*= HTML_SCROLL_STEP
;
512 wxGetMousePosition(&x
, &y
);
513 ScreenToClient(&x
, &y
);
514 lnk
= m_Cell
-> GetLink(sx
+ x
, sy
+ y
);
516 if (lnk
== wxEmptyString
) {
517 SetCursor(cur_arrow
);
518 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(wxEmptyString
, m_RelatedStatusBar
);
522 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(lnk
, m_RelatedStatusBar
);
524 m_tmpMouseMoved
= FALSE
;
531 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
533 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
534 EVT_SIZE(wxHtmlWindow::OnSize
)
535 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent
)
536 EVT_MOTION(wxHtmlWindow::OnMouseEvent
)
537 EVT_IDLE(wxHtmlWindow::OnIdle
)
538 EVT_KEY_DOWN(wxHtmlWindow::OnKeyDown
)
545 // A module to allow initialization/cleanup
546 // without calling these functions from app.cpp or from
547 // the user's application.
549 class wxHtmlWinModule
: public wxModule
551 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
553 wxHtmlWinModule() : wxModule() {}
554 bool OnInit() { return TRUE
; }
555 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
558 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
563 ///// default mod handlers are forced there:
565 FORCE_LINK(mod_layout
)
566 FORCE_LINK(mod_fonts
)
567 FORCE_LINK(mod_image
)
570 FORCE_LINK(mod_hline
)
571 FORCE_LINK(mod_links
)
572 FORCE_LINK(mod_tables
)