]>
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
, name
)
46 m_tmpMouseMoved
= FALSE
;
47 m_tmpLastLink
= wxEmptyString
;
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
;
72 wxList
*parser_data
= m_Parser
-> GetTempData();
73 if (parser_data
) delete parser_data
;
81 void wxHtmlWindow::SetRelatedFrame(wxFrame
* frame
, const wxString
& format
)
83 m_RelatedFrame
= frame
;
84 m_TitleFormat
= format
;
89 void wxHtmlWindow::SetRelatedStatusBar(int bar
)
91 m_RelatedStatusBar
= bar
;
96 void wxHtmlWindow::SetFonts(wxString normal_face
, int normal_italic_mode
, wxString fixed_face
, int fixed_italic_mode
, const int *sizes
)
98 wxString op
= m_OpenedPage
;
100 m_Parser
-> SetFonts(normal_face
, normal_italic_mode
, fixed_face
, fixed_italic_mode
, sizes
);
101 SetPage(wxT("<html><body></body></html>")); // fonts changed => contents invalid
102 if (!op
.IsEmpty()) LoadPage(op
);
107 bool wxHtmlWindow::SetPage(const wxString
& source
)
109 wxClientDC
*dc
= new wxClientDC(this);
111 dc
-> SetMapMode(wxMM_TEXT
);
112 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
113 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
114 m_Parser
-> SetDC(dc
);
119 m_Cell
= (wxHtmlContainerCell
*) m_Parser
-> Parse(source
);
121 m_Cell
-> SetIndent(m_Borders
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
122 m_Cell
-> SetAlignHor(wxHTML_ALIGN_CENTER
);
124 if (m_tmpCanDrawLocks
== 0) Refresh();
129 bool wxHtmlWindow::LoadPage(const wxString
& location
)
134 SetCursor(*wxHOURGLASS_CURSOR
);
138 if (m_HistoryOn
&& (m_HistoryPos
!= -1)) { // store scroll position into history item
141 m_History
[m_HistoryPos
].SetPos(y
);
144 if (location
[0] == '#') { // local anchor
145 wxString anch
= location
.Mid(1) /*1 to end*/;
147 rt_val
= ScrollToAnchor(anch
);
152 if (m_RelatedStatusBar
!= -1) {
153 m_RelatedFrame
-> SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
157 f
= m_FS
-> OpenFile(location
);
161 err
.Printf(_("The browser is unable to open requested location :\n\n%s"), WXSTRINGCAST location
);
164 wxMessageBox(err
, "Error");
166 SetCursor(*wxSTANDARD_CURSOR
);
172 wxString src
= wxEmptyString
;
174 if (m_RelatedStatusBar
!= -1) {
175 wxString msg
= _("Loading : ") + location
;
176 m_RelatedFrame
-> SetStatusText(msg
, m_RelatedStatusBar
);
180 node
= m_Filters
.GetFirst();
182 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
-> GetData();
183 if (h
-> CanRead(*f
)) {
184 src
= h
-> ReadFile(*f
);
187 node
= node
-> GetNext();
189 if (src
== wxEmptyString
) {
190 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
191 src
= m_DefaultFilter
-> ReadFile(*f
);
194 m_FS
-> ChangePathTo(f
-> GetLocation());
195 rt_val
= SetPage(src
);
196 m_OpenedPage
= f
-> GetLocation();
197 if (f
-> GetAnchor() != wxEmptyString
) {
198 // m_tmpCanDrawLocks--;
199 ScrollToAnchor(f
-> GetAnchor());
200 // m_tmpCanDrawLocks++;
205 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(_("Done"), m_RelatedStatusBar
);
209 if (m_HistoryOn
) { // add this page to history there:
210 int c
= m_History
.GetCount() - (m_HistoryPos
+ 1);
213 for (int i
= 0; i
< c
; i
++)
214 m_History
.Remove(m_HistoryPos
);
215 m_History
.Add(new HtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
218 SetCursor(*wxSTANDARD_CURSOR
);
228 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
230 const wxHtmlCell
*c
= m_Cell
-> Find(wxHTML_COND_ISANCHOR
, &anchor
);
231 if (!c
) return FALSE
;
235 for (y
= 0; c
!= NULL
; c
= c
-> GetParent()) y
+= c
-> GetPosY();
236 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
237 m_OpenedAnchor
= anchor
;
243 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
245 if (m_RelatedFrame
) {
247 tit
.Printf(m_TitleFormat
, title
.c_str());
248 m_RelatedFrame
-> SetTitle(tit
);
250 m_OpenedPageTitle
= title
;
257 void wxHtmlWindow::CreateLayout()
259 int ClientWidth
, ClientHeight
;
263 if (m_Style
== wxHW_SCROLLBAR_NEVER
) {
264 SetScrollbars(1, 1, 0, 0); // always off
265 GetClientSize(&ClientWidth
, &ClientHeight
);
266 m_Cell
-> Layout(ClientWidth
);
270 GetClientSize(&ClientWidth
, &ClientHeight
);
272 // VS : this looks extremely ugly under windoze, better fix needed!
273 SetScrollbars(1, 1, 0, ClientHeight
* 2); // always on
275 GetClientSize(&ClientWidth
, &ClientHeight
);
276 m_Cell
-> Layout(ClientWidth
);
277 if (ClientHeight
< m_Cell
-> GetHeight()) {
278 SetScrollbars(wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
279 m_Cell
-> GetWidth() / wxHTML_SCROLL_STEP
,
280 m_Cell
-> GetHeight() / wxHTML_SCROLL_STEP
281 /*cheat: top-level frag is always container*/);
283 else { /* we fit into window, no need for scrollbars */
284 SetScrollbars(1, 1, 0, 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
;
301 if (path
!= wxEmptyString
) {
302 oldpath
= cfg
-> GetPath();
303 cfg
-> SetPath(path
);
306 m_Borders
= cfg
-> Read("wxHtmlWindow/Borders", m_Borders
);
307 p_fff
= cfg
-> Read("wxHtmlWindow/FontFaceFixed", m_Parser
-> m_FontFaceFixed
);
308 p_ffn
= cfg
-> Read("wxHtmlWindow/FontFaceNormal", m_Parser
-> m_FontFaceNormal
);
309 p_imf
= cfg
-> Read("wxHtmlWindow/ItalicModeFixed", m_Parser
-> m_ItalicModeFixed
);
310 p_imn
= cfg
-> Read("wxHtmlWindow/ItalicModeNormal", m_Parser
-> m_ItalicModeNormal
);
311 for (int i
= 0; i
< 7; i
++) {
312 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
313 p_fontsizes
[i
] = cfg
-> Read(tmp
, m_Parser
-> m_FontsSizes
[i
]);
315 SetFonts(p_ffn
, p_imn
, p_fff
, p_imf
, p_fontsizes
);
317 if (path
!= wxEmptyString
)
318 cfg
-> SetPath(oldpath
);
323 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
328 if (path
!= wxEmptyString
) {
329 oldpath
= cfg
-> GetPath();
330 cfg
-> SetPath(path
);
333 cfg
-> Write("wxHtmlWindow/Borders", (long) m_Borders
);
334 cfg
-> Write("wxHtmlWindow/FontFaceFixed", m_Parser
-> m_FontFaceFixed
);
335 cfg
-> Write("wxHtmlWindow/FontFaceNormal", m_Parser
-> m_FontFaceNormal
);
336 cfg
-> Write("wxHtmlWindow/ItalicModeFixed", (long) m_Parser
-> m_ItalicModeFixed
);
337 cfg
-> Write("wxHtmlWindow/ItalicModeNormal", (long) m_Parser
-> m_ItalicModeNormal
);
338 for (int i
= 0; i
< 7; i
++) {
339 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
340 cfg
-> Write(tmp
, (long) m_Parser
-> m_FontsSizes
[i
]);
343 if (path
!= wxEmptyString
)
344 cfg
-> SetPath(oldpath
);
349 bool wxHtmlWindow::HistoryBack()
353 if (m_HistoryPos
< 1) return FALSE
;
357 l
= m_History
[m_HistoryPos
].GetPage();
358 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());
373 bool wxHtmlWindow::HistoryForward()
377 if (m_HistoryPos
== -1) return FALSE
;
378 if (m_HistoryPos
>= (int)m_History
.GetCount() - 1)return FALSE
;
380 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
383 l
= m_History
[m_HistoryPos
].GetPage();
384 a
= m_History
[m_HistoryPos
].GetAnchor();
387 if (a
== wxEmptyString
) LoadPage(l
);
388 else LoadPage(l
+ "#" + a
);
390 Scroll(0, m_History
[m_HistoryPos
].GetPos());
399 void wxHtmlWindow::HistoryClear()
407 wxList
wxHtmlWindow::m_Filters
;
408 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
410 void wxHtmlWindow::CleanUpStatics()
412 if (m_DefaultFilter
) delete m_DefaultFilter
;
413 m_DefaultFilter
= NULL
;
414 m_Filters
.DeleteContents(TRUE
);
421 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
423 m_Filters
.Append(filter
);
429 void wxHtmlWindow::OnLinkClicked(const wxString
& link
)
436 void wxHtmlWindow::OnDraw(wxDC
& dc
)
439 wxRegionIterator
upd(GetUpdateRegion()); // get the update rect list
442 if (m_tmpCanDrawLocks
> 0) return;
443 dc
.SetMapMode(wxMM_TEXT
);
444 #if defined(_MSC_VER) && (_MSC_VER == 1200)
445 ::SetMapMode((HDC
)dc
.GetHDC(), MM_TEXT
);
447 dc
.SetBackgroundMode(wxTRANSPARENT
);
453 if (m_Cell
) m_Cell
-> Draw(dc
, 0, 0, y
* wxHTML_SCROLL_STEP
+ v_y
, y
* wxHTML_SCROLL_STEP
+ v_h
+ v_y
);
461 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
463 wxScrolledWindow::OnSize(event
);
469 void wxHtmlWindow::OnKeyDown(wxKeyEvent
& event
)
474 ViewStart(&dummy
, &sty
);
475 GetClientSize(&dummy
, &cliy
); cliy
/= wxHTML_SCROLL_STEP
;
476 GetVirtualSize(&dummy
, &szy
); szy
/= wxHTML_SCROLL_STEP
;
478 switch (event
.KeyCode()) {
481 Scroll(-1, sty
- cliy
);
485 Scroll(-1, sty
+ cliy
);
491 Scroll(-1, szy
- cliy
);
504 void wxHtmlWindow::OnMouseEvent(wxMouseEvent
& event
)
506 m_tmpMouseMoved
= TRUE
;
508 if (event
.ButtonDown()) {
513 ViewStart(&sx
, &sy
); sx
*= wxHTML_SCROLL_STEP
; sy
*= wxHTML_SCROLL_STEP
;
514 pos
= event
.GetPosition();
517 m_Cell
-> OnMouseClick(this, sx
+ pos
.x
, sy
+ pos
.y
, event
.ButtonDown(1), event
.ButtonDown(2), event
.ButtonDown(3));
523 void wxHtmlWindow::OnIdle(wxIdleEvent
& event
)
525 static wxCursor
cur_hand(wxCURSOR_HAND
), cur_arrow(wxCURSOR_ARROW
);
527 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
)) {
532 ViewStart(&sx
, &sy
); sx
*= wxHTML_SCROLL_STEP
; sy
*= wxHTML_SCROLL_STEP
;
533 wxGetMousePosition(&x
, &y
);
534 ScreenToClient(&x
, &y
);
535 lnk
= m_Cell
-> GetLink(sx
+ x
, sy
+ y
);
537 if (lnk
!= m_tmpLastLink
) {
538 if (lnk
== wxEmptyString
) {
539 SetCursor(cur_arrow
);
540 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(wxEmptyString
, m_RelatedStatusBar
);
544 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(lnk
, m_RelatedStatusBar
);
548 m_tmpMouseMoved
= FALSE
;
555 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
557 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
558 EVT_SIZE(wxHtmlWindow::OnSize
)
559 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent
)
560 EVT_MOTION(wxHtmlWindow::OnMouseEvent
)
561 EVT_IDLE(wxHtmlWindow::OnIdle
)
562 EVT_KEY_DOWN(wxHtmlWindow::OnKeyDown
)
569 // A module to allow initialization/cleanup
570 // without calling these functions from app.cpp or from
571 // the user's application.
573 class wxHtmlWinModule
: public wxModule
575 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
577 wxHtmlWinModule() : wxModule() {}
578 bool OnInit() { return TRUE
; }
579 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
582 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
587 ///// default mod handlers are forced there:
589 FORCE_LINK(mod_layout
)
590 FORCE_LINK(mod_fonts
)
591 FORCE_LINK(mod_image
)
594 FORCE_LINK(mod_hline
)
595 FORCE_LINK(mod_links
)
596 FORCE_LINK(mod_tables
)