]>
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"
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
| wxHSCROLL
, name
)
46 m_tmpMouseMoved
= FALSE
;
48 m_tmpCanDrawLocks
= 0;
49 m_FS
= new wxFileSystem();
50 m_RelatedStatusBar
= -1;
51 m_RelatedFrame
= NULL
;
53 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
55 m_Parser
= new wxHtmlWinParser(this);
56 m_Parser
-> SetFS(m_FS
);
61 SetPage("<html><body></body></html>");
66 wxHtmlWindow::~wxHtmlWindow()
70 if (m_Cell
) delete m_Cell
;
78 void wxHtmlWindow::SetRelatedFrame(wxFrame
* frame
, const wxString
& format
)
80 m_RelatedFrame
= frame
;
81 m_TitleFormat
= format
;
86 void wxHtmlWindow::SetRelatedStatusBar(int bar
)
88 m_RelatedStatusBar
= bar
;
93 void wxHtmlWindow::SetFonts(wxString normal_face
, wxString fixed_face
, const int *sizes
)
95 wxString op
= m_OpenedPage
;
97 m_Parser
-> SetFonts(normal_face
, fixed_face
, sizes
);
98 SetPage(wxT("<html><body></body></html>")); // fonts changed => contents invalid
99 if (!op
.IsEmpty()) LoadPage(op
);
104 bool wxHtmlWindow::SetPage(const wxString
& source
)
106 wxClientDC
*dc
= new wxClientDC(this);
108 dc
-> SetMapMode(wxMM_TEXT
);
109 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
110 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
111 m_Parser
-> SetDC(dc
);
116 m_Cell
= (wxHtmlContainerCell
*) m_Parser
-> Parse(source
);
118 m_Cell
-> SetIndent(m_Borders
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
119 m_Cell
-> SetAlignHor(wxHTML_ALIGN_CENTER
);
121 if (m_tmpCanDrawLocks
== 0) Refresh();
126 bool wxHtmlWindow::LoadPage(const wxString
& location
)
131 SetCursor(*wxHOURGLASS_CURSOR
);
135 if (m_HistoryOn
&& (m_HistoryPos
!= -1)) { // store scroll position into history item
138 m_History
[m_HistoryPos
].SetPos(y
);
141 if (location
[0] == '#') { // local anchor
142 wxString anch
= location
.Mid(1) /*1 to end*/;
144 rt_val
= ScrollToAnchor(anch
);
149 if (m_RelatedStatusBar
!= -1) {
150 m_RelatedFrame
-> SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
154 f
= m_FS
-> OpenFile(location
);
158 wxLogError(_("Unable to open requested HTML document: %s"), location
.mb_str());
161 SetCursor(*wxSTANDARD_CURSOR
);
167 wxString src
= wxEmptyString
;
169 if (m_RelatedStatusBar
!= -1) {
170 wxString msg
= _("Loading : ") + location
;
171 m_RelatedFrame
-> SetStatusText(msg
, m_RelatedStatusBar
);
175 node
= m_Filters
.GetFirst();
177 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
-> GetData();
178 if (h
-> CanRead(*f
)) {
179 src
= h
-> ReadFile(*f
);
182 node
= node
-> GetNext();
184 if (src
== wxEmptyString
) {
185 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
186 src
= m_DefaultFilter
-> ReadFile(*f
);
189 m_FS
-> ChangePathTo(f
-> GetLocation());
190 rt_val
= SetPage(src
);
191 m_OpenedPage
= f
-> GetLocation();
192 if (f
-> GetAnchor() != wxEmptyString
) {
193 // m_tmpCanDrawLocks--;
194 ScrollToAnchor(f
-> GetAnchor());
195 // m_tmpCanDrawLocks++;
200 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(_("Done"), m_RelatedStatusBar
);
204 if (m_HistoryOn
) { // add this page to history there:
205 int c
= m_History
.GetCount() - (m_HistoryPos
+ 1);
208 for (int i
= 0; i
< c
; i
++)
209 m_History
.Remove(m_HistoryPos
);
210 m_History
.Add(new HtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
213 if (m_OpenedPageTitle
== wxEmptyString
)
214 OnSetTitle(wxFileNameFromPath(m_OpenedPage
));
215 SetCursor(*wxSTANDARD_CURSOR
);
225 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
227 const wxHtmlCell
*c
= m_Cell
-> Find(wxHTML_COND_ISANCHOR
, &anchor
);
230 wxLogWarning(_("HTML anchor %s does not exist."), anchor
.mb_str());
236 for (y
= 0; c
!= NULL
; c
= c
-> GetParent()) y
+= c
-> GetPosY();
237 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
238 m_OpenedAnchor
= anchor
;
244 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
246 if (m_RelatedFrame
) {
248 tit
.Printf(m_TitleFormat
, title
.c_str());
249 m_RelatedFrame
-> SetTitle(tit
);
251 m_OpenedPageTitle
= title
;
258 void wxHtmlWindow::CreateLayout()
260 int ClientWidth
, ClientHeight
;
264 if (m_Style
== wxHW_SCROLLBAR_NEVER
) {
265 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
-> GetWidth() / wxHTML_SCROLL_STEP
, 0); // always off
266 GetClientSize(&ClientWidth
, &ClientHeight
);
267 m_Cell
-> Layout(ClientWidth
);
271 GetClientSize(&ClientWidth
, &ClientHeight
);
273 // VS : this looks extremely ugly under windoze, better fix needed!
274 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
-> GetWidth() / wxHTML_SCROLL_STEP
, ClientHeight
* 2); // always on
276 GetClientSize(&ClientWidth
, &ClientHeight
);
277 m_Cell
-> Layout(ClientWidth
);
278 if (ClientHeight
< m_Cell
-> GetHeight() + GetCharHeight()) {
280 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
281 m_Cell
-> GetWidth() / wxHTML_SCROLL_STEP
,
282 (m_Cell
-> GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
283 /*cheat: top-level frag is always container*/);
285 else { /* we fit into window, no need for scrollbars */
286 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
-> GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
287 GetClientSize(&ClientWidth
, &ClientHeight
);
288 m_Cell
-> Layout(ClientWidth
); // ...and relayout
295 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
300 wxString p_fff
, p_ffn
;
302 if (path
!= wxEmptyString
) {
303 oldpath
= cfg
-> GetPath();
304 cfg
-> SetPath(path
);
307 m_Borders
= cfg
-> Read("wxHtmlWindow/Borders", m_Borders
);
308 p_fff
= cfg
-> Read("wxHtmlWindow/FontFaceFixed", m_Parser
-> m_FontFaceFixed
);
309 p_ffn
= cfg
-> Read("wxHtmlWindow/FontFaceNormal", m_Parser
-> m_FontFaceNormal
);
310 for (int i
= 0; i
< 7; i
++) {
311 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
312 p_fontsizes
[i
] = cfg
-> Read(tmp
, m_Parser
-> m_FontsSizes
[i
]);
314 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
316 if (path
!= wxEmptyString
)
317 cfg
-> SetPath(oldpath
);
322 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
327 if (path
!= wxEmptyString
) {
328 oldpath
= cfg
-> GetPath();
329 cfg
-> SetPath(path
);
332 cfg
-> Write("wxHtmlWindow/Borders", (long) m_Borders
);
333 cfg
-> Write("wxHtmlWindow/FontFaceFixed", m_Parser
-> m_FontFaceFixed
);
334 cfg
-> Write("wxHtmlWindow/FontFaceNormal", m_Parser
-> m_FontFaceNormal
);
335 for (int i
= 0; i
< 7; i
++) {
336 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
337 cfg
-> Write(tmp
, (long) m_Parser
-> m_FontsSizes
[i
]);
340 if (path
!= wxEmptyString
)
341 cfg
-> SetPath(oldpath
);
346 bool wxHtmlWindow::HistoryBack()
350 if (m_HistoryPos
< 1) return FALSE
;
354 l
= m_History
[m_HistoryPos
].GetPage();
355 a
= m_History
[m_HistoryPos
].GetAnchor();
358 if (a
== wxEmptyString
) LoadPage(l
);
359 else LoadPage(l
+ "#" + a
);
363 Scroll(0, m_History
[m_HistoryPos
].GetPos());
370 bool wxHtmlWindow::HistoryForward()
374 if (m_HistoryPos
== -1) return FALSE
;
375 if (m_HistoryPos
>= (int)m_History
.GetCount() - 1)return FALSE
;
377 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
380 l
= m_History
[m_HistoryPos
].GetPage();
381 a
= m_History
[m_HistoryPos
].GetAnchor();
384 if (a
== wxEmptyString
) LoadPage(l
);
385 else LoadPage(l
+ "#" + a
);
389 Scroll(0, m_History
[m_HistoryPos
].GetPos());
396 void wxHtmlWindow::HistoryClear()
404 wxList
wxHtmlWindow::m_Filters
;
405 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
407 void wxHtmlWindow::CleanUpStatics()
409 if (m_DefaultFilter
) delete m_DefaultFilter
;
410 m_DefaultFilter
= NULL
;
411 m_Filters
.DeleteContents(TRUE
);
418 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
420 m_Filters
.Append(filter
);
426 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
428 LoadPage(link
.GetHref());
433 void wxHtmlWindow::OnDraw(wxDC
& dc
)
436 wxRegionIterator
upd(GetUpdateRegion()); // get the update rect list
439 if (m_tmpCanDrawLocks
> 0) return;
440 dc
.SetMapMode(wxMM_TEXT
);
442 /* VS - I don't think this is neccessary any longer
443 MSC_VER 1200 means MSVC 6.0 and it works fine */
444 #if defined(_MSC_VER) && (_MSC_VER == 1200)
445 ::SetMapMode((HDC
)dc
.GetHDC(), MM_TEXT
);
448 dc
.SetBackgroundMode(wxTRANSPARENT
);
454 if (m_Cell
) m_Cell
-> Draw(dc
, 0, 0, y
* wxHTML_SCROLL_STEP
+ v_y
, y
* wxHTML_SCROLL_STEP
+ v_h
+ v_y
);
462 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
464 wxScrolledWindow::OnSize(event
);
470 void wxHtmlWindow::OnKeyDown(wxKeyEvent
& event
)
475 ViewStart(&dummy
, &sty
);
476 GetClientSize(&dummy
, &cliy
); cliy
/= wxHTML_SCROLL_STEP
;
477 GetVirtualSize(&dummy
, &szy
); szy
/= wxHTML_SCROLL_STEP
;
479 switch (event
.KeyCode()) {
482 Scroll(-1, sty
- (5 * cliy
/ 6));
486 Scroll(-1, sty
+ (5 * cliy
/ 6));
492 Scroll(-1, szy
- cliy
);
505 void wxHtmlWindow::OnMouseEvent(wxMouseEvent
& event
)
507 m_tmpMouseMoved
= TRUE
;
509 if (event
.ButtonDown()) {
514 ViewStart(&sx
, &sy
); sx
*= wxHTML_SCROLL_STEP
; sy
*= wxHTML_SCROLL_STEP
;
515 pos
= event
.GetPosition();
518 m_Cell
-> OnMouseClick(this, sx
+ pos
.x
, sy
+ pos
.y
, event
);
524 void wxHtmlWindow::OnIdle(wxIdleEvent
& event
)
526 static wxCursor
cur_hand(wxCURSOR_HAND
), cur_arrow(wxCURSOR_ARROW
);
528 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
)) {
533 ViewStart(&sx
, &sy
); sx
*= wxHTML_SCROLL_STEP
; sy
*= wxHTML_SCROLL_STEP
;
534 wxGetMousePosition(&x
, &y
);
535 ScreenToClient(&x
, &y
);
536 lnk
= m_Cell
-> GetLink(sx
+ x
, sy
+ y
);
538 if (lnk
!= m_tmpLastLink
) {
540 SetCursor(cur_arrow
);
541 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(wxEmptyString
, m_RelatedStatusBar
);
545 if (m_RelatedStatusBar
!= -1)
546 m_RelatedFrame
-> SetStatusText(lnk
-> GetHref(), m_RelatedStatusBar
);
550 m_tmpMouseMoved
= FALSE
;
557 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
559 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
560 EVT_SIZE(wxHtmlWindow::OnSize
)
561 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent
)
562 EVT_MOTION(wxHtmlWindow::OnMouseEvent
)
563 EVT_IDLE(wxHtmlWindow::OnIdle
)
564 EVT_KEY_DOWN(wxHtmlWindow::OnKeyDown
)
571 // A module to allow initialization/cleanup
572 // without calling these functions from app.cpp or from
573 // the user's application.
575 class wxHtmlWinModule
: public wxModule
577 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
579 wxHtmlWinModule() : wxModule() {}
580 bool OnInit() { return TRUE
; }
581 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
584 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
589 ///// default mod handlers are forced there: