]>
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
;
396 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
398 m_Filters
.DeleteContents(TRUE
);
399 m_Filters
.Append(filter
);
405 void wxHtmlWindow::OnLinkClicked(const wxString
& link
)
412 void wxHtmlWindow::OnDraw(wxDC
& dc
)
415 wxRegionIterator
upd(GetUpdateRegion()); // get the update rect list
418 if (!m_tmpCanDraw
) return;
419 dc
.SetMapMode(wxMM_TEXT
);
420 #if defined(_MSC_VER) && (_MSC_VER == 1200)
421 ::SetMapMode((HDC
)dc
.GetHDC(), MM_TEXT
);
423 dc
.SetBackgroundMode(wxTRANSPARENT
);
429 if (m_Cell
) m_Cell
-> Draw(dc
, 0, 0, y
* HTML_SCROLL_STEP
+ v_y
, y
* HTML_SCROLL_STEP
+ v_h
+ v_y
);
437 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
439 wxScrolledWindow::OnSize(event
);
445 void wxHtmlWindow::OnKeyDown(wxKeyEvent
& event
)
450 ViewStart(&dummy
, &sty
);
451 GetClientSize(&dummy
, &cliy
); cliy
/= HTML_SCROLL_STEP
;
452 GetVirtualSize(&dummy
, &szy
); szy
/= HTML_SCROLL_STEP
;
454 switch (event
.KeyCode()) {
457 Scroll(-1, sty
- cliy
);
461 Scroll(-1, sty
+ cliy
);
467 Scroll(-1, szy
- cliy
);
480 void wxHtmlWindow::OnMouseEvent(wxMouseEvent
& event
)
482 m_tmpMouseMoved
= TRUE
;
484 if (event
.ButtonDown()) {
489 ViewStart(&sx
, &sy
); sx
*= HTML_SCROLL_STEP
; sy
*= HTML_SCROLL_STEP
;
490 pos
= event
.GetPosition();
493 m_Cell
-> OnMouseClick(this, sx
+ pos
.x
, sy
+ pos
.y
, event
.ButtonDown(1), event
.ButtonDown(2), event
.ButtonDown(3));
499 void wxHtmlWindow::OnIdle(wxIdleEvent
& event
)
501 static wxCursor
cur_hand(wxCURSOR_HAND
), cur_arrow(wxCURSOR_ARROW
);
503 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
)) {
508 ViewStart(&sx
, &sy
); sx
*= HTML_SCROLL_STEP
; sy
*= HTML_SCROLL_STEP
;
509 wxGetMousePosition(&x
, &y
);
510 ScreenToClient(&x
, &y
);
511 lnk
= m_Cell
-> GetLink(sx
+ x
, sy
+ y
);
513 if (lnk
== wxEmptyString
) {
514 SetCursor(cur_arrow
);
515 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(wxEmptyString
, m_RelatedStatusBar
);
519 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(lnk
, m_RelatedStatusBar
);
521 m_tmpMouseMoved
= FALSE
;
528 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
530 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
531 EVT_SIZE(wxHtmlWindow::OnSize
)
532 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent
)
533 EVT_MOTION(wxHtmlWindow::OnMouseEvent
)
534 EVT_IDLE(wxHtmlWindow::OnIdle
)
535 EVT_KEY_DOWN(wxHtmlWindow::OnKeyDown
)
542 // A module to allow initialization/cleanup
543 // without calling these functions from app.cpp or from
544 // the user's application.
546 class wxHtmlWinModule
: public wxModule
548 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
550 wxHtmlWinModule() : wxModule() {}
551 bool OnInit() { return TRUE
; }
552 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
555 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
560 ///// default mod handlers are forced there:
562 FORCE_LINK(mod_layout
)
563 FORCE_LINK(mod_fonts
)
564 FORCE_LINK(mod_image
)
567 FORCE_LINK(mod_hline
)
568 FORCE_LINK(mod_links
)
569 FORCE_LINK(mod_tables
)