]>
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
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation
15 #include "wx/wxprec.h"
28 #include "wx/html/htmlwin.h"
29 #include "wx/html/forcelnk.h"
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
41 #include "wx/arrimpl.cpp"
42 WX_DEFINE_OBJARRAY(HtmlHistoryArray
)
45 wxHtmlWindow::wxHtmlWindow(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
46 long style
, const wxString
& name
) : wxScrolledWindow(parent
, id
, pos
, size
, wxVSCROLL
| wxHSCROLL
, name
)
48 m_tmpMouseMoved
= FALSE
;
50 m_tmpCanDrawLocks
= 0;
51 m_FS
= new wxFileSystem();
52 m_RelatedStatusBar
= -1;
53 m_RelatedFrame
= NULL
;
55 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
57 m_Parser
= new wxHtmlWinParser(this);
58 m_Parser
-> SetFS(m_FS
);
63 SetPage("<html><body></body></html>");
68 wxHtmlWindow::~wxHtmlWindow()
72 if (m_Cell
) delete m_Cell
;
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
, wxString fixed_face
, const int *sizes
)
97 wxString op
= m_OpenedPage
;
99 m_Parser
-> SetFonts(normal_face
, fixed_face
, sizes
);
100 SetPage(wxT("<html><body></body></html>")); // fonts changed => contents invalid
101 if (!op
.IsEmpty()) LoadPage(op
);
106 bool wxHtmlWindow::SetPage(const wxString
& source
)
108 wxClientDC
*dc
= new wxClientDC(this);
110 dc
-> SetMapMode(wxMM_TEXT
);
111 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
112 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
113 m_Parser
-> SetDC(dc
);
118 m_Cell
= (wxHtmlContainerCell
*) m_Parser
-> Parse(source
);
120 m_Cell
-> SetIndent(m_Borders
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
121 m_Cell
-> SetAlignHor(wxHTML_ALIGN_CENTER
);
123 if (m_tmpCanDrawLocks
== 0) Refresh();
128 bool wxHtmlWindow::LoadPage(const wxString
& location
)
133 SetCursor(*wxHOURGLASS_CURSOR
);
137 if (m_HistoryOn
&& (m_HistoryPos
!= -1)) { // store scroll position into history item
140 m_History
[m_HistoryPos
].SetPos(y
);
143 if (location
[0] == '#') { // local anchor
144 wxString anch
= location
.Mid(1) /*1 to end*/;
146 rt_val
= ScrollToAnchor(anch
);
151 if (m_RelatedStatusBar
!= -1) {
152 m_RelatedFrame
-> SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
156 f
= m_FS
-> OpenFile(location
);
160 err
.Printf(_("Unable to open requested location :\n\n%s"), WXSTRINGCAST location
);
163 wxMessageBox(err
, "Error");
165 SetCursor(*wxSTANDARD_CURSOR
);
171 wxString src
= wxEmptyString
;
173 if (m_RelatedStatusBar
!= -1) {
174 wxString msg
= _("Loading : ") + location
;
175 m_RelatedFrame
-> SetStatusText(msg
, m_RelatedStatusBar
);
179 node
= m_Filters
.GetFirst();
181 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
-> GetData();
182 if (h
-> CanRead(*f
)) {
183 src
= h
-> ReadFile(*f
);
186 node
= node
-> GetNext();
188 if (src
== wxEmptyString
) {
189 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
190 src
= m_DefaultFilter
-> ReadFile(*f
);
193 m_FS
-> ChangePathTo(f
-> GetLocation());
194 rt_val
= SetPage(src
);
195 m_OpenedPage
= f
-> GetLocation();
196 if (f
-> GetAnchor() != wxEmptyString
) {
197 // m_tmpCanDrawLocks--;
198 ScrollToAnchor(f
-> GetAnchor());
199 // m_tmpCanDrawLocks++;
204 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(_("Done"), m_RelatedStatusBar
);
208 if (m_HistoryOn
) { // add this page to history there:
209 int c
= m_History
.GetCount() - (m_HistoryPos
+ 1);
212 for (int i
= 0; i
< c
; i
++)
213 m_History
.Remove(m_HistoryPos
);
214 m_History
.Add(new HtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
217 SetCursor(*wxSTANDARD_CURSOR
);
227 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
229 const wxHtmlCell
*c
= m_Cell
-> Find(wxHTML_COND_ISANCHOR
, &anchor
);
230 if (!c
) return FALSE
;
234 for (y
= 0; c
!= NULL
; c
= c
-> GetParent()) y
+= c
-> GetPosY();
235 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
236 m_OpenedAnchor
= anchor
;
242 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
244 if (m_RelatedFrame
) {
246 tit
.Printf(m_TitleFormat
, title
.c_str());
247 m_RelatedFrame
-> SetTitle(tit
);
249 m_OpenedPageTitle
= title
;
256 void wxHtmlWindow::CreateLayout()
258 int ClientWidth
, ClientHeight
;
262 if (m_Style
== wxHW_SCROLLBAR_NEVER
) {
263 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
-> GetWidth() / wxHTML_SCROLL_STEP
, 0); // always off
264 GetClientSize(&ClientWidth
, &ClientHeight
);
265 m_Cell
-> Layout(ClientWidth
);
269 GetClientSize(&ClientWidth
, &ClientHeight
);
271 // VS : this looks extremely ugly under windoze, better fix needed!
272 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
-> GetWidth() / wxHTML_SCROLL_STEP
, ClientHeight
* 2); // always on
274 GetClientSize(&ClientWidth
, &ClientHeight
);
275 m_Cell
-> Layout(ClientWidth
);
276 if (ClientHeight
< m_Cell
-> GetHeight() + GetCharHeight()) {
278 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
279 m_Cell
-> GetWidth() / wxHTML_SCROLL_STEP
,
280 (m_Cell
-> GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
281 /*cheat: top-level frag is always container*/);
283 else { /* we fit into window, no need for scrollbars */
284 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
-> GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
285 GetClientSize(&ClientWidth
, &ClientHeight
);
286 m_Cell
-> Layout(ClientWidth
); // ...and relayout
293 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
298 wxString p_fff
, p_ffn
;
300 if (path
!= wxEmptyString
) {
301 oldpath
= cfg
-> GetPath();
302 cfg
-> SetPath(path
);
305 m_Borders
= cfg
-> Read("wxHtmlWindow/Borders", m_Borders
);
306 p_fff
= cfg
-> Read("wxHtmlWindow/FontFaceFixed", m_Parser
-> m_FontFaceFixed
);
307 p_ffn
= cfg
-> Read("wxHtmlWindow/FontFaceNormal", m_Parser
-> m_FontFaceNormal
);
308 for (int i
= 0; i
< 7; i
++) {
309 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
310 p_fontsizes
[i
] = cfg
-> Read(tmp
, m_Parser
-> m_FontsSizes
[i
]);
312 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
314 if (path
!= wxEmptyString
)
315 cfg
-> SetPath(oldpath
);
320 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
325 if (path
!= wxEmptyString
) {
326 oldpath
= cfg
-> GetPath();
327 cfg
-> SetPath(path
);
330 cfg
-> Write("wxHtmlWindow/Borders", (long) m_Borders
);
331 cfg
-> Write("wxHtmlWindow/FontFaceFixed", m_Parser
-> m_FontFaceFixed
);
332 cfg
-> Write("wxHtmlWindow/FontFaceNormal", m_Parser
-> m_FontFaceNormal
);
333 for (int i
= 0; i
< 7; i
++) {
334 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
335 cfg
-> Write(tmp
, (long) m_Parser
-> m_FontsSizes
[i
]);
338 if (path
!= wxEmptyString
)
339 cfg
-> SetPath(oldpath
);
344 bool wxHtmlWindow::HistoryBack()
348 if (m_HistoryPos
< 1) return FALSE
;
352 l
= m_History
[m_HistoryPos
].GetPage();
353 a
= m_History
[m_HistoryPos
].GetAnchor();
356 if (a
== wxEmptyString
) LoadPage(l
);
357 else LoadPage(l
+ "#" + a
);
361 Scroll(0, m_History
[m_HistoryPos
].GetPos());
368 bool wxHtmlWindow::HistoryForward()
372 if (m_HistoryPos
== -1) return FALSE
;
373 if (m_HistoryPos
>= (int)m_History
.GetCount() - 1)return FALSE
;
375 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
378 l
= m_History
[m_HistoryPos
].GetPage();
379 a
= m_History
[m_HistoryPos
].GetAnchor();
382 if (a
== wxEmptyString
) LoadPage(l
);
383 else LoadPage(l
+ "#" + a
);
387 Scroll(0, m_History
[m_HistoryPos
].GetPos());
394 void wxHtmlWindow::HistoryClear()
402 wxList
wxHtmlWindow::m_Filters
;
403 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
405 void wxHtmlWindow::CleanUpStatics()
407 if (m_DefaultFilter
) delete m_DefaultFilter
;
408 m_DefaultFilter
= NULL
;
409 m_Filters
.DeleteContents(TRUE
);
416 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
418 m_Filters
.Append(filter
);
424 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
426 LoadPage(link
.GetHref());
431 void wxHtmlWindow::OnDraw(wxDC
& dc
)
434 wxRegionIterator
upd(GetUpdateRegion()); // get the update rect list
437 if (m_tmpCanDrawLocks
> 0) return;
438 dc
.SetMapMode(wxMM_TEXT
);
439 #if defined(_MSC_VER) && (_MSC_VER == 1200)
440 ::SetMapMode((HDC
)dc
.GetHDC(), MM_TEXT
);
442 dc
.SetBackgroundMode(wxTRANSPARENT
);
448 if (m_Cell
) m_Cell
-> Draw(dc
, 0, 0, y
* wxHTML_SCROLL_STEP
+ v_y
, y
* wxHTML_SCROLL_STEP
+ v_h
+ v_y
);
456 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
458 wxScrolledWindow::OnSize(event
);
464 void wxHtmlWindow::OnKeyDown(wxKeyEvent
& event
)
469 ViewStart(&dummy
, &sty
);
470 GetClientSize(&dummy
, &cliy
); cliy
/= wxHTML_SCROLL_STEP
;
471 GetVirtualSize(&dummy
, &szy
); szy
/= wxHTML_SCROLL_STEP
;
473 switch (event
.KeyCode()) {
476 Scroll(-1, sty
- (2 * cliy
/ 3));
480 Scroll(-1, sty
+ (2 * cliy
/ 3));
486 Scroll(-1, szy
- cliy
);
499 void wxHtmlWindow::OnMouseEvent(wxMouseEvent
& event
)
501 m_tmpMouseMoved
= TRUE
;
503 if (event
.ButtonDown()) {
508 ViewStart(&sx
, &sy
); sx
*= wxHTML_SCROLL_STEP
; sy
*= wxHTML_SCROLL_STEP
;
509 pos
= event
.GetPosition();
512 m_Cell
-> OnMouseClick(this, sx
+ pos
.x
, sy
+ pos
.y
, event
);
518 void wxHtmlWindow::OnIdle(wxIdleEvent
& event
)
520 static wxCursor
cur_hand(wxCURSOR_HAND
), cur_arrow(wxCURSOR_ARROW
);
522 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
)) {
527 ViewStart(&sx
, &sy
); sx
*= wxHTML_SCROLL_STEP
; sy
*= wxHTML_SCROLL_STEP
;
528 wxGetMousePosition(&x
, &y
);
529 ScreenToClient(&x
, &y
);
530 lnk
= m_Cell
-> GetLink(sx
+ x
, sy
+ y
);
532 if (lnk
!= m_tmpLastLink
) {
534 SetCursor(cur_arrow
);
535 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(wxEmptyString
, m_RelatedStatusBar
);
539 if (m_RelatedStatusBar
!= -1)
540 m_RelatedFrame
-> SetStatusText(lnk
-> GetHref(), m_RelatedStatusBar
);
544 m_tmpMouseMoved
= FALSE
;
551 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
553 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
554 EVT_SIZE(wxHtmlWindow::OnSize
)
555 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent
)
556 EVT_MOTION(wxHtmlWindow::OnMouseEvent
)
557 EVT_IDLE(wxHtmlWindow::OnIdle
)
558 EVT_KEY_DOWN(wxHtmlWindow::OnKeyDown
)
565 // A module to allow initialization/cleanup
566 // without calling these functions from app.cpp or from
567 // the user's application.
569 class wxHtmlWinModule
: public wxModule
571 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
573 wxHtmlWinModule() : wxModule() {}
574 bool OnInit() { return TRUE
; }
575 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
578 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
583 ///// default mod handlers are forced there: