]>
git.saurik.com Git - wxWidgets.git/blob - src/html/htmlwin.cpp
493d0c35d606073c1c4e198350a3c8e99a3dde20
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>
31 ///// This is my own wxBusyCursor. It works only with one window.
33 #if (defined __WXGTK__) && (wxVERSION_NUMBER < 2100)
34 class wxLocalBusyCursor
36 class wxLocalBusyCursor
: public wxBusyCursor
42 #if (defined __WXGTK__) && (wxVERSION_NUMBER < 2100)
43 wxLocalBusyCursor(wxWindow
*w
) {m_Wnd
= w
; m_Wnd
-> SetCursor(*wxHOURGLASS_CURSOR
);}
44 ~wxLocalBusyCursor() {m_Wnd
-> SetCursor(*wxSTANDARD_CURSOR
);}
46 wxLocalBusyCursor(wxWindow
*w
) : wxBusyCursor() {}
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
59 #include <wx/arrimpl.cpp>
60 WX_DEFINE_OBJARRAY(HtmlHistoryArray
)
63 wxHtmlWindow::wxHtmlWindow(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
64 const wxString
& name
, bool scrollable
) : wxScrolledWindow(parent
, id
, pos
, size
, wxVSCROLL
, name
)
66 m_tmpMouseMoved
= FALSE
;
68 m_FS
= new wxFileSystem();
69 m_RelatedStatusBar
= -1;
70 m_RelatedFrame
= NULL
;
72 m_OpenedPage
= m_OpenedAnchor
= wxEmptyString
;
74 m_Parser
= new wxHtmlWinParser(this);
75 m_Parser
-> SetFS(m_FS
);
79 m_Scrollable
= scrollable
;
80 SetPage("<html><body></body></html>");
85 wxHtmlWindow::~wxHtmlWindow()
89 if (m_Cell
) delete m_Cell
;
91 wxList
*parser_data
= m_Parser
-> GetTempData();
92 if (parser_data
) delete parser_data
;
100 void wxHtmlWindow::SetRelatedFrame(wxFrame
* frame
, const wxString
& format
)
102 m_RelatedFrame
= frame
;
103 m_TitleFormat
= format
;
108 void wxHtmlWindow::SetRelatedStatusBar(int bar
)
110 m_RelatedStatusBar
= bar
;
115 void wxHtmlWindow::SetFonts(wxString normal_face
, int normal_italic_mode
, wxString fixed_face
, int fixed_italic_mode
, int *sizes
)
117 m_Parser
-> SetFonts(normal_face
, normal_italic_mode
, fixed_face
, fixed_italic_mode
, sizes
);
118 if (!m_OpenedPage
.IsEmpty()) LoadPage(m_OpenedPage
);
123 bool wxHtmlWindow::SetPage(const wxString
& source
)
125 wxClientDC
*dc
= new wxClientDC(this);
127 dc
-> SetMapMode(wxMM_TEXT
);
128 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
129 m_OpenedPage
= m_OpenedAnchor
= wxEmptyString
;
130 m_Parser
-> SetDC(dc
);
131 if (m_Cell
) delete m_Cell
;
132 m_Cell
= (wxHtmlContainerCell
*) m_Parser
-> Parse(source
);
134 m_Cell
-> SetIndent(m_Borders
, HTML_INDENT_ALL
, HTML_UNITS_PIXELS
);
135 m_Cell
-> SetAlignHor(HTML_ALIGN_CENTER
);
142 bool wxHtmlWindow::LoadPage(const wxString
& location
)
146 wxLocalBusyCursor
b(this);
148 m_tmpCanDraw
= FALSE
;
149 if (m_HistoryOn
&& (m_HistoryPos
!= -1)) { // store scroll position into history item
152 m_History
[m_HistoryPos
].SetPos(y
);
155 if (location
[0] == '#') { // local anchor
156 wxString anch
= location
.Mid(1) /*1 to end*/;
158 rt_val
= ScrollToAnchor(anch
);
163 if (m_RelatedStatusBar
!= -1) {
164 m_RelatedFrame
-> SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
168 f
= m_FS
-> OpenFile(location
);
172 err
.Printf(_("The browser is unable to open requested location :\n\n%s"), WXSTRINGCAST location
);
175 wxMessageBox(err
, "Error");
181 wxString src
= wxEmptyString
;
183 if (m_RelatedStatusBar
!= -1) {
184 wxString msg
= _("Loading : ") + location
;
185 m_RelatedFrame
-> SetStatusText(msg
, m_RelatedStatusBar
);
189 node
= m_Filters
.GetFirst();
191 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
-> GetData();
192 if (h
-> CanRead(*f
)) {
193 src
= h
-> ReadFile(*f
);
196 node
= node
-> GetNext();
198 if (src
== wxEmptyString
) src
= m_DefaultFilter
.ReadFile(*f
);
200 m_FS
-> ChangePathTo(f
-> GetLocation());
201 rt_val
= SetPage(src
);
202 m_OpenedPage
= f
-> GetLocation();
203 if (f
-> GetAnchor() != wxEmptyString
) {
205 ScrollToAnchor(f
-> GetAnchor());
206 m_tmpCanDraw
= FALSE
;
211 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(_("Done"), m_RelatedStatusBar
);
215 if (m_HistoryOn
) { // add this page to history there:
216 int c
= m_History
.GetCount() - (m_HistoryPos
+ 1);
219 for (int i
= 0; i
< c
; i
++)
220 m_History
.Remove(m_HistoryPos
);
221 m_History
.Add(new HtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
231 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
233 const wxHtmlCell
*c
= m_Cell
-> Find(HTML_COND_ISANCHOR
, &anchor
);
234 if (!c
) return FALSE
;
238 for (y
= 0; c
!= NULL
; c
= c
-> GetParent()) y
+= c
-> GetPosY();
239 Scroll(-1, y
/ HTML_SCROLL_STEP
);
240 m_OpenedAnchor
= anchor
;
246 void wxHtmlWindow::SetTitle(const wxString
& title
)
248 if (m_RelatedFrame
) {
250 tit
.Printf(m_TitleFormat
, title
.c_str());
251 m_RelatedFrame
-> SetTitle(tit
);
259 void wxHtmlWindow::CreateLayout()
261 int ClientWidth
, ClientHeight
;
264 GetClientSize(&ClientWidth
, &ClientHeight
);
265 m_Cell
-> Layout(ClientWidth
);
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*/ );
275 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
280 if (path
!= wxEmptyString
) {
281 oldpath
= cfg
-> GetPath();
282 cfg
-> SetPath(path
);
285 m_Borders
= cfg
-> Read("wxHtmlWindow/Borders", m_Borders
);
286 m_Parser
-> m_FontFaceFixed
= cfg
-> Read("wxHtmlWindow/FontFaceFixed", m_Parser
-> m_FontFaceFixed
);
287 m_Parser
-> m_FontFaceNormal
= cfg
-> Read("wxHtmlWindow/FontFaceNormal", m_Parser
-> m_FontFaceNormal
);
288 m_Parser
-> m_ItalicModeFixed
= cfg
-> Read("wxHtmlWindow/ItalicModeFixed", m_Parser
-> m_ItalicModeFixed
);
289 m_Parser
-> m_ItalicModeNormal
= cfg
-> Read("wxHtmlWindow/ItalicModeNormal", m_Parser
-> m_ItalicModeNormal
);
290 for (int i
= 0; i
< 7; i
++) {
291 tmp
.Printf("wxHtmlWindow/FontsSize%i", i
);
292 m_Parser
-> m_FontsSizes
[i
] = cfg
-> Read(tmp
, m_Parser
-> m_FontsSizes
[i
]);
295 if (path
!= wxEmptyString
)
296 cfg
-> SetPath(oldpath
);
301 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
306 if (path
!= wxEmptyString
) {
307 oldpath
= cfg
-> GetPath();
308 cfg
-> SetPath(path
);
311 cfg
-> Write("wxHtmlWindow/Borders", (long) m_Borders
);
312 cfg
-> Write("wxHtmlWindow/FontFaceFixed", m_Parser
-> m_FontFaceFixed
);
313 cfg
-> Write("wxHtmlWindow/FontFaceNormal", m_Parser
-> m_FontFaceNormal
);
314 cfg
-> Write("wxHtmlWindow/ItalicModeFixed", (long) m_Parser
-> m_ItalicModeFixed
);
315 cfg
-> Write("wxHtmlWindow/ItalicModeNormal", (long) m_Parser
-> m_ItalicModeNormal
);
316 for (int i
= 0; i
< 7; i
++) {
317 tmp
.Printf("wxHtmlWindow/FontsSize%i", i
);
318 cfg
-> Write(tmp
, (long) m_Parser
-> m_FontsSizes
[i
]);
321 if (path
!= wxEmptyString
)
322 cfg
-> SetPath(oldpath
);
327 bool wxHtmlWindow::HistoryBack()
331 if (m_HistoryPos
< 1) return FALSE
;
335 l
= m_History
[m_HistoryPos
].GetPage();
336 a
= m_History
[m_HistoryPos
].GetAnchor();
338 if (a
== wxEmptyString
) LoadPage(l
);
339 else LoadPage(l
+ "#" + a
);
341 Scroll(0, m_History
[m_HistoryPos
].GetPos());
348 bool wxHtmlWindow::HistoryForward()
352 if (m_HistoryPos
== -1) return FALSE
;
353 if (m_HistoryPos
>= (int)m_History
.GetCount() - 1)return FALSE
;
355 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
358 l
= m_History
[m_HistoryPos
].GetPage();
359 a
= m_History
[m_HistoryPos
].GetAnchor();
361 if (a
== wxEmptyString
) LoadPage(l
);
362 else LoadPage(l
+ "#" + a
);
364 Scroll(0, m_History
[m_HistoryPos
].GetPos());
371 void wxHtmlWindow::HistoryClear()
379 wxList
wxHtmlWindow::m_Filters
;
380 wxHtmlFilterPlainText
wxHtmlWindow::m_DefaultFilter
;
382 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
384 m_Filters
.DeleteContents(TRUE
);
385 m_Filters
.Append(filter
);
391 void wxHtmlWindow::OnLinkClicked(const wxString
& link
)
398 void wxHtmlWindow::OnDraw(wxDC
& dc
)
401 wxRegionIterator
upd(GetUpdateRegion()); // get the update rect list
404 if (!m_tmpCanDraw
) return;
405 dc
.SetMapMode(wxMM_TEXT
);
406 #if defined(_MSC_VER) && (_MSC_VER == 1200)
407 ::SetMapMode((HDC
)dc
.GetHDC(), MM_TEXT
);
409 dc
.SetBackgroundMode(wxTRANSPARENT
);
415 if (m_Cell
) m_Cell
-> Draw(dc
, 0, 0, y
* HTML_SCROLL_STEP
+ v_y
, y
* HTML_SCROLL_STEP
+ v_h
+ v_y
);
423 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
425 wxScrolledWindow::OnSize(event
);
431 void wxHtmlWindow::OnKeyDown(wxKeyEvent
& event
)
436 ViewStart(&dummy
, &sty
);
437 GetClientSize(&dummy
, &cliy
); cliy
/= HTML_SCROLL_STEP
;
438 GetVirtualSize(&dummy
, &szy
); szy
/= HTML_SCROLL_STEP
;
440 switch (event
.KeyCode()) {
443 Scroll(-1, sty
- cliy
);
447 Scroll(-1, sty
+ cliy
);
453 Scroll(-1, szy
- cliy
);
466 void wxHtmlWindow::OnMouseEvent(wxMouseEvent
& event
)
468 m_tmpMouseMoved
= TRUE
;
470 if (event
.ButtonDown()) {
475 ViewStart(&sx
, &sy
); sx
*= HTML_SCROLL_STEP
; sy
*= HTML_SCROLL_STEP
;
476 pos
= event
.GetPosition();
479 m_Cell
-> OnMouseClick(this, sx
+ pos
.x
, sy
+ pos
.y
, event
.ButtonDown(1), event
.ButtonDown(2), event
.ButtonDown(3));
485 void wxHtmlWindow::OnIdle(wxIdleEvent
& event
)
487 static wxCursor
cur_hand(wxCURSOR_HAND
), cur_arrow(wxCURSOR_ARROW
);
489 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
)) {
494 ViewStart(&sx
, &sy
); sx
*= HTML_SCROLL_STEP
; sy
*= HTML_SCROLL_STEP
;
495 wxGetMousePosition(&x
, &y
);
496 ScreenToClient(&x
, &y
);
497 lnk
= m_Cell
-> GetLink(sx
+ x
, sy
+ y
);
499 if (lnk
== wxEmptyString
) {
500 SetCursor(cur_arrow
);
501 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(wxEmptyString
, m_RelatedStatusBar
);
505 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(lnk
, m_RelatedStatusBar
);
507 m_tmpMouseMoved
= FALSE
;
514 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
516 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
517 EVT_SIZE(wxHtmlWindow::OnSize
)
518 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent
)
519 EVT_MOTION(wxHtmlWindow::OnMouseEvent
)
520 EVT_IDLE(wxHtmlWindow::OnIdle
)
521 EVT_KEY_DOWN(wxHtmlWindow::OnKeyDown
)
531 ///// default mod handlers are forced there:
533 FORCE_LINK(mod_layout
)
534 FORCE_LINK(mod_fonts
)
535 FORCE_LINK(mod_image
)
538 FORCE_LINK(mod_hline
)
539 FORCE_LINK(mod_links
)
540 FORCE_LINK(mod_tables
)