]>
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"
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
38 #include "wx/arrimpl.cpp"
39 WX_DEFINE_OBJARRAY(HtmlHistoryArray
)
42 wxHtmlWindow::wxHtmlWindow(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
43 long style
, const wxString
& name
) : wxScrolledWindow(parent
, id
, pos
, size
, wxVSCROLL
| wxHSCROLL
, name
)
45 m_tmpMouseMoved
= FALSE
;
47 m_tmpCanDrawLocks
= 0;
48 m_FS
= new wxFileSystem();
49 m_RelatedStatusBar
= -1;
50 m_RelatedFrame
= NULL
;
52 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= 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
;
77 void wxHtmlWindow::SetRelatedFrame(wxFrame
* frame
, const wxString
& format
)
79 m_RelatedFrame
= frame
;
80 m_TitleFormat
= format
;
85 void wxHtmlWindow::SetRelatedStatusBar(int bar
)
87 m_RelatedStatusBar
= bar
;
92 void wxHtmlWindow::SetFonts(wxString normal_face
, wxString fixed_face
, const int *sizes
)
94 wxString op
= m_OpenedPage
;
96 m_Parser
-> SetFonts(normal_face
, fixed_face
, sizes
);
97 SetPage(wxT("<html><body></body></html>")); // fonts changed => contents invalid
98 if (!op
.IsEmpty()) LoadPage(op
);
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
= m_OpenedPageTitle
= wxEmptyString
;
110 m_Parser
-> SetDC(dc
);
115 m_Cell
= (wxHtmlContainerCell
*) m_Parser
-> Parse(source
);
117 m_Cell
-> SetIndent(m_Borders
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
118 m_Cell
-> SetAlignHor(wxHTML_ALIGN_CENTER
);
120 if (m_tmpCanDrawLocks
== 0) Refresh();
125 bool wxHtmlWindow::LoadPage(const wxString
& location
)
130 SetCursor(*wxHOURGLASS_CURSOR
);
134 if (m_HistoryOn
&& (m_HistoryPos
!= -1)) { // store scroll position into history item
137 m_History
[m_HistoryPos
].SetPos(y
);
140 if (location
[0] == '#') { // local anchor
141 wxString anch
= location
.Mid(1) /*1 to end*/;
143 rt_val
= ScrollToAnchor(anch
);
148 if (m_RelatedStatusBar
!= -1) {
149 m_RelatedFrame
-> SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
153 f
= m_FS
-> OpenFile(location
);
157 err
.Printf(_("Unable to open requested location :\n\n%s"), WXSTRINGCAST location
);
160 wxMessageBox(err
, "Error");
162 SetCursor(*wxSTANDARD_CURSOR
);
168 wxString src
= wxEmptyString
;
170 if (m_RelatedStatusBar
!= -1) {
171 wxString msg
= _("Loading : ") + location
;
172 m_RelatedFrame
-> SetStatusText(msg
, m_RelatedStatusBar
);
176 node
= m_Filters
.GetFirst();
178 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
-> GetData();
179 if (h
-> CanRead(*f
)) {
180 src
= h
-> ReadFile(*f
);
183 node
= node
-> GetNext();
185 if (src
== wxEmptyString
) {
186 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
187 src
= m_DefaultFilter
-> ReadFile(*f
);
190 m_FS
-> ChangePathTo(f
-> GetLocation());
191 rt_val
= SetPage(src
);
192 m_OpenedPage
= f
-> GetLocation();
193 if (f
-> GetAnchor() != wxEmptyString
) {
194 // m_tmpCanDrawLocks--;
195 ScrollToAnchor(f
-> GetAnchor());
196 // m_tmpCanDrawLocks++;
201 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(_("Done"), m_RelatedStatusBar
);
205 if (m_HistoryOn
) { // add this page to history there:
206 int c
= m_History
.GetCount() - (m_HistoryPos
+ 1);
209 for (int i
= 0; i
< c
; i
++)
210 m_History
.Remove(m_HistoryPos
);
211 m_History
.Add(new HtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
214 SetCursor(*wxSTANDARD_CURSOR
);
224 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
226 const wxHtmlCell
*c
= m_Cell
-> Find(wxHTML_COND_ISANCHOR
, &anchor
);
227 if (!c
) return FALSE
;
231 for (y
= 0; c
!= NULL
; c
= c
-> GetParent()) y
+= c
-> GetPosY();
232 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
233 m_OpenedAnchor
= anchor
;
239 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
241 if (m_RelatedFrame
) {
243 tit
.Printf(m_TitleFormat
, title
.c_str());
244 m_RelatedFrame
-> SetTitle(tit
);
246 m_OpenedPageTitle
= title
;
253 void wxHtmlWindow::CreateLayout()
255 int ClientWidth
, ClientHeight
;
259 if (m_Style
== wxHW_SCROLLBAR_NEVER
) {
260 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
-> GetWidth() / wxHTML_SCROLL_STEP
, 0); // always off
261 GetClientSize(&ClientWidth
, &ClientHeight
);
262 m_Cell
-> Layout(ClientWidth
);
266 GetClientSize(&ClientWidth
, &ClientHeight
);
268 // VS : this looks extremely ugly under windoze, better fix needed!
269 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
-> GetWidth() / wxHTML_SCROLL_STEP
, ClientHeight
* 2); // always on
271 GetClientSize(&ClientWidth
, &ClientHeight
);
272 m_Cell
-> Layout(ClientWidth
);
273 if (ClientHeight
< m_Cell
-> GetHeight() + GetCharHeight()) {
275 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
276 m_Cell
-> GetWidth() / wxHTML_SCROLL_STEP
,
277 (m_Cell
-> GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
278 /*cheat: top-level frag is always container*/);
280 else { /* we fit into window, no need for scrollbars */
281 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
-> GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
282 GetClientSize(&ClientWidth
, &ClientHeight
);
283 m_Cell
-> Layout(ClientWidth
); // ...and relayout
290 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
295 wxString p_fff
, p_ffn
;
297 if (path
!= wxEmptyString
) {
298 oldpath
= cfg
-> GetPath();
299 cfg
-> SetPath(path
);
302 m_Borders
= cfg
-> Read("wxHtmlWindow/Borders", m_Borders
);
303 p_fff
= cfg
-> Read("wxHtmlWindow/FontFaceFixed", m_Parser
-> m_FontFaceFixed
);
304 p_ffn
= cfg
-> Read("wxHtmlWindow/FontFaceNormal", m_Parser
-> m_FontFaceNormal
);
305 for (int i
= 0; i
< 7; i
++) {
306 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
307 p_fontsizes
[i
] = cfg
-> Read(tmp
, m_Parser
-> m_FontsSizes
[i
]);
309 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
311 if (path
!= wxEmptyString
)
312 cfg
-> SetPath(oldpath
);
317 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
322 if (path
!= wxEmptyString
) {
323 oldpath
= cfg
-> GetPath();
324 cfg
-> SetPath(path
);
327 cfg
-> Write("wxHtmlWindow/Borders", (long) m_Borders
);
328 cfg
-> Write("wxHtmlWindow/FontFaceFixed", m_Parser
-> m_FontFaceFixed
);
329 cfg
-> Write("wxHtmlWindow/FontFaceNormal", m_Parser
-> m_FontFaceNormal
);
330 for (int i
= 0; i
< 7; i
++) {
331 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
332 cfg
-> Write(tmp
, (long) m_Parser
-> m_FontsSizes
[i
]);
335 if (path
!= wxEmptyString
)
336 cfg
-> SetPath(oldpath
);
341 bool wxHtmlWindow::HistoryBack()
345 if (m_HistoryPos
< 1) return FALSE
;
349 l
= m_History
[m_HistoryPos
].GetPage();
350 a
= m_History
[m_HistoryPos
].GetAnchor();
353 if (a
== wxEmptyString
) LoadPage(l
);
354 else LoadPage(l
+ "#" + a
);
358 Scroll(0, m_History
[m_HistoryPos
].GetPos());
365 bool wxHtmlWindow::HistoryForward()
369 if (m_HistoryPos
== -1) return FALSE
;
370 if (m_HistoryPos
>= (int)m_History
.GetCount() - 1)return FALSE
;
372 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
375 l
= m_History
[m_HistoryPos
].GetPage();
376 a
= m_History
[m_HistoryPos
].GetAnchor();
379 if (a
== wxEmptyString
) LoadPage(l
);
380 else LoadPage(l
+ "#" + a
);
384 Scroll(0, m_History
[m_HistoryPos
].GetPos());
391 void wxHtmlWindow::HistoryClear()
399 wxList
wxHtmlWindow::m_Filters
;
400 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
402 void wxHtmlWindow::CleanUpStatics()
404 if (m_DefaultFilter
) delete m_DefaultFilter
;
405 m_DefaultFilter
= NULL
;
406 m_Filters
.DeleteContents(TRUE
);
413 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
415 m_Filters
.Append(filter
);
421 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
423 LoadPage(link
.GetHref());
428 void wxHtmlWindow::OnDraw(wxDC
& dc
)
431 wxRegionIterator
upd(GetUpdateRegion()); // get the update rect list
434 if (m_tmpCanDrawLocks
> 0) return;
435 dc
.SetMapMode(wxMM_TEXT
);
437 /* VS - I don't think this is neccessary any longer
438 MSC_VER 1200 means MSVC 6.0 and it works fine */
439 #if defined(_MSC_VER) && (_MSC_VER == 1200)
440 ::SetMapMode((HDC
)dc
.GetHDC(), MM_TEXT
);
443 dc
.SetBackgroundMode(wxTRANSPARENT
);
449 if (m_Cell
) m_Cell
-> Draw(dc
, 0, 0, y
* wxHTML_SCROLL_STEP
+ v_y
, y
* wxHTML_SCROLL_STEP
+ v_h
+ v_y
);
457 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
459 wxScrolledWindow::OnSize(event
);
465 void wxHtmlWindow::OnKeyDown(wxKeyEvent
& event
)
470 ViewStart(&dummy
, &sty
);
471 GetClientSize(&dummy
, &cliy
); cliy
/= wxHTML_SCROLL_STEP
;
472 GetVirtualSize(&dummy
, &szy
); szy
/= wxHTML_SCROLL_STEP
;
474 switch (event
.KeyCode()) {
477 Scroll(-1, sty
- (2 * cliy
/ 3));
481 Scroll(-1, sty
+ (2 * cliy
/ 3));
487 Scroll(-1, szy
- cliy
);
500 void wxHtmlWindow::OnMouseEvent(wxMouseEvent
& event
)
502 m_tmpMouseMoved
= TRUE
;
504 if (event
.ButtonDown()) {
509 ViewStart(&sx
, &sy
); sx
*= wxHTML_SCROLL_STEP
; sy
*= wxHTML_SCROLL_STEP
;
510 pos
= event
.GetPosition();
513 m_Cell
-> OnMouseClick(this, sx
+ pos
.x
, sy
+ pos
.y
, event
);
519 void wxHtmlWindow::OnIdle(wxIdleEvent
& event
)
521 static wxCursor
cur_hand(wxCURSOR_HAND
), cur_arrow(wxCURSOR_ARROW
);
523 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
)) {
528 ViewStart(&sx
, &sy
); sx
*= wxHTML_SCROLL_STEP
; sy
*= wxHTML_SCROLL_STEP
;
529 wxGetMousePosition(&x
, &y
);
530 ScreenToClient(&x
, &y
);
531 lnk
= m_Cell
-> GetLink(sx
+ x
, sy
+ y
);
533 if (lnk
!= m_tmpLastLink
) {
535 SetCursor(cur_arrow
);
536 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(wxEmptyString
, m_RelatedStatusBar
);
540 if (m_RelatedStatusBar
!= -1)
541 m_RelatedFrame
-> SetStatusText(lnk
-> GetHref(), m_RelatedStatusBar
);
545 m_tmpMouseMoved
= FALSE
;
552 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
554 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
555 EVT_SIZE(wxHtmlWindow::OnSize
)
556 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent
)
557 EVT_MOTION(wxHtmlWindow::OnMouseEvent
)
558 EVT_IDLE(wxHtmlWindow::OnIdle
)
559 EVT_KEY_DOWN(wxHtmlWindow::OnKeyDown
)
566 // A module to allow initialization/cleanup
567 // without calling these functions from app.cpp or from
568 // the user's application.
570 class wxHtmlWinModule
: public wxModule
572 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
574 wxHtmlWinModule() : wxModule() {}
575 bool OnInit() { return TRUE
; }
576 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
579 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
584 ///// default mod handlers are forced there: