]>
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>
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
);
135 m_Cell
= (wxHtmlContainerCell
*) m_Parser
-> Parse(source
);
137 m_Cell
-> SetIndent(m_Borders
, HTML_INDENT_ALL
, HTML_UNITS_PIXELS
);
138 m_Cell
-> SetAlignHor(HTML_ALIGN_CENTER
);
145 bool wxHtmlWindow::LoadPage(const wxString
& location
)
149 wxLocalBusyCursor
b(this);
151 m_tmpCanDraw
= FALSE
;
152 if (m_HistoryOn
&& (m_HistoryPos
!= -1)) { // store scroll position into history item
155 m_History
[m_HistoryPos
].SetPos(y
);
158 if (location
[0] == '#') { // local anchor
159 wxString anch
= location
.Mid(1) /*1 to end*/;
161 rt_val
= ScrollToAnchor(anch
);
166 if (m_RelatedStatusBar
!= -1) {
167 m_RelatedFrame
-> SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
171 f
= m_FS
-> OpenFile(location
);
175 err
.Printf(_("The browser is unable to open requested location :\n\n%s"), WXSTRINGCAST location
);
178 wxMessageBox(err
, "Error");
184 wxString src
= wxEmptyString
;
186 if (m_RelatedStatusBar
!= -1) {
187 wxString msg
= _("Loading : ") + location
;
188 m_RelatedFrame
-> SetStatusText(msg
, m_RelatedStatusBar
);
192 node
= m_Filters
.GetFirst();
194 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
-> GetData();
195 if (h
-> CanRead(*f
)) {
196 src
= h
-> ReadFile(*f
);
199 node
= node
-> GetNext();
201 if (src
== wxEmptyString
) {
202 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
203 src
= m_DefaultFilter
-> ReadFile(*f
);
206 m_FS
-> ChangePathTo(f
-> GetLocation());
207 rt_val
= SetPage(src
);
208 m_OpenedPage
= f
-> GetLocation();
209 if (f
-> GetAnchor() != wxEmptyString
) {
211 ScrollToAnchor(f
-> GetAnchor());
212 m_tmpCanDraw
= FALSE
;
217 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(_("Done"), m_RelatedStatusBar
);
221 if (m_HistoryOn
) { // add this page to history there:
222 int c
= m_History
.GetCount() - (m_HistoryPos
+ 1);
225 for (int i
= 0; i
< c
; i
++)
226 m_History
.Remove(m_HistoryPos
);
227 m_History
.Add(new HtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
237 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
239 const wxHtmlCell
*c
= m_Cell
-> Find(HTML_COND_ISANCHOR
, &anchor
);
240 if (!c
) return FALSE
;
244 for (y
= 0; c
!= NULL
; c
= c
-> GetParent()) y
+= c
-> GetPosY();
245 Scroll(-1, y
/ HTML_SCROLL_STEP
);
246 m_OpenedAnchor
= anchor
;
252 void wxHtmlWindow::SetTitle(const wxString
& title
)
254 if (m_RelatedFrame
) {
256 tit
.Printf(m_TitleFormat
, title
.c_str());
257 m_RelatedFrame
-> SetTitle(tit
);
265 void wxHtmlWindow::CreateLayout()
267 int ClientWidth
, ClientHeight
;
270 GetClientSize(&ClientWidth
, &ClientHeight
);
271 m_Cell
-> Layout(ClientWidth
);
273 SetScrollbars(HTML_SCROLL_STEP
, HTML_SCROLL_STEP
,
274 m_Cell
-> GetWidth() / HTML_SCROLL_STEP
,
275 m_Cell
-> GetHeight() / HTML_SCROLL_STEP
276 /*cheat: top-level frag is always container*/ );
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
)