]>
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
);
131 if (m_Cell
) delete m_Cell
;
132 m_Cell
= (wxHtmlContainerCell
*) m_Parser
-> Parse(source
);
134 m_Cell
-> SetIndent(m_Borders
, HTML_INDENT_ALL
, HTML_UNITS_PIXELS
);
135 m_Cell
-> SetAlignHor(HTML_ALIGN_CENTER
);
142 bool wxHtmlWindow::LoadPage(const wxString
& location
)
146 wxLocalBusyCursor
b(this);
148 m_tmpCanDraw
= FALSE
;
149 if (m_HistoryOn
&& (m_HistoryPos
!= -1)) { // store scroll position into history item
152 m_History
[m_HistoryPos
].SetPos(y
);
155 if (location
[0] == '#') { // local anchor
156 wxString anch
= location
.Mid(1) /*1 to end*/;
158 rt_val
= ScrollToAnchor(anch
);
163 if (m_RelatedStatusBar
!= -1) {
164 m_RelatedFrame
-> SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
168 f
= m_FS
-> OpenFile(location
);
172 err
.Printf(_("The browser is unable to open requested location :\n\n%s"), WXSTRINGCAST location
);
175 wxMessageBox(err
, "Error");
181 wxString src
= wxEmptyString
;
183 if (m_RelatedStatusBar
!= -1) {
184 wxString msg
= _("Loading : ") + location
;
185 m_RelatedFrame
-> SetStatusText(msg
, m_RelatedStatusBar
);
189 node
= m_Filters
.GetFirst();
191 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
-> GetData();
192 if (h
-> CanRead(*f
)) {
193 src
= h
-> ReadFile(*f
);
196 node
= node
-> GetNext();
198 if (src
== wxEmptyString
) {
199 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
200 src
= m_DefaultFilter
-> ReadFile(*f
);
203 m_FS
-> ChangePathTo(f
-> GetLocation());
204 rt_val
= SetPage(src
);
205 m_OpenedPage
= f
-> GetLocation();
206 if (f
-> GetAnchor() != wxEmptyString
) {
208 ScrollToAnchor(f
-> GetAnchor());
209 m_tmpCanDraw
= FALSE
;
214 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(_("Done"), m_RelatedStatusBar
);
218 if (m_HistoryOn
) { // add this page to history there:
219 int c
= m_History
.GetCount() - (m_HistoryPos
+ 1);
222 for (int i
= 0; i
< c
; i
++)
223 m_History
.Remove(m_HistoryPos
);
224 m_History
.Add(new HtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
234 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
236 const wxHtmlCell
*c
= m_Cell
-> Find(HTML_COND_ISANCHOR
, &anchor
);
237 if (!c
) return FALSE
;
241 for (y
= 0; c
!= NULL
; c
= c
-> GetParent()) y
+= c
-> GetPosY();
242 Scroll(-1, y
/ HTML_SCROLL_STEP
);
243 m_OpenedAnchor
= anchor
;
249 void wxHtmlWindow::SetTitle(const wxString
& title
)
251 if (m_RelatedFrame
) {
253 tit
.Printf(m_TitleFormat
, title
.c_str());
254 m_RelatedFrame
-> SetTitle(tit
);
262 void wxHtmlWindow::CreateLayout()
264 int ClientWidth
, ClientHeight
;
267 GetClientSize(&ClientWidth
, &ClientHeight
);
268 m_Cell
-> Layout(ClientWidth
);
270 SetScrollbars(HTML_SCROLL_STEP
, HTML_SCROLL_STEP
,
271 m_Cell
-> GetWidth() / HTML_SCROLL_STEP
,
272 m_Cell
-> GetHeight() / HTML_SCROLL_STEP
273 /*cheat: top-level frag is always container*/ );
278 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
283 if (path
!= wxEmptyString
) {
284 oldpath
= cfg
-> GetPath();
285 cfg
-> SetPath(path
);
288 m_Borders
= cfg
-> Read("wxHtmlWindow/Borders", m_Borders
);
289 m_Parser
-> m_FontFaceFixed
= cfg
-> Read("wxHtmlWindow/FontFaceFixed", m_Parser
-> m_FontFaceFixed
);
290 m_Parser
-> m_FontFaceNormal
= cfg
-> Read("wxHtmlWindow/FontFaceNormal", m_Parser
-> m_FontFaceNormal
);
291 m_Parser
-> m_ItalicModeFixed
= cfg
-> Read("wxHtmlWindow/ItalicModeFixed", m_Parser
-> m_ItalicModeFixed
);
292 m_Parser
-> m_ItalicModeNormal
= cfg
-> Read("wxHtmlWindow/ItalicModeNormal", m_Parser
-> m_ItalicModeNormal
);
293 for (int i
= 0; i
< 7; i
++) {
294 tmp
.Printf("wxHtmlWindow/FontsSize%i", i
);
295 m_Parser
-> m_FontsSizes
[i
] = cfg
-> Read(tmp
, m_Parser
-> m_FontsSizes
[i
]);
298 if (path
!= wxEmptyString
)
299 cfg
-> SetPath(oldpath
);
304 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
309 if (path
!= wxEmptyString
) {
310 oldpath
= cfg
-> GetPath();
311 cfg
-> SetPath(path
);
314 cfg
-> Write("wxHtmlWindow/Borders", (long) m_Borders
);
315 cfg
-> Write("wxHtmlWindow/FontFaceFixed", m_Parser
-> m_FontFaceFixed
);
316 cfg
-> Write("wxHtmlWindow/FontFaceNormal", m_Parser
-> m_FontFaceNormal
);
317 cfg
-> Write("wxHtmlWindow/ItalicModeFixed", (long) m_Parser
-> m_ItalicModeFixed
);
318 cfg
-> Write("wxHtmlWindow/ItalicModeNormal", (long) m_Parser
-> m_ItalicModeNormal
);
319 for (int i
= 0; i
< 7; i
++) {
320 tmp
.Printf("wxHtmlWindow/FontsSize%i", i
);
321 cfg
-> Write(tmp
, (long) m_Parser
-> m_FontsSizes
[i
]);
324 if (path
!= wxEmptyString
)
325 cfg
-> SetPath(oldpath
);
330 bool wxHtmlWindow::HistoryBack()
334 if (m_HistoryPos
< 1) return FALSE
;
338 l
= m_History
[m_HistoryPos
].GetPage();
339 a
= m_History
[m_HistoryPos
].GetAnchor();
341 if (a
== wxEmptyString
) LoadPage(l
);
342 else LoadPage(l
+ "#" + a
);
344 Scroll(0, m_History
[m_HistoryPos
].GetPos());
351 bool wxHtmlWindow::HistoryForward()
355 if (m_HistoryPos
== -1) return FALSE
;
356 if (m_HistoryPos
>= (int)m_History
.GetCount() - 1)return FALSE
;
358 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
361 l
= m_History
[m_HistoryPos
].GetPage();
362 a
= m_History
[m_HistoryPos
].GetAnchor();
364 if (a
== wxEmptyString
) LoadPage(l
);
365 else LoadPage(l
+ "#" + a
);
367 Scroll(0, m_History
[m_HistoryPos
].GetPos());
374 void wxHtmlWindow::HistoryClear()
382 wxList
wxHtmlWindow::m_Filters
;
383 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
385 void wxHtmlWindow::CleanUpStatics()
387 if (m_DefaultFilter
) delete m_DefaultFilter
;
388 m_DefaultFilter
= NULL
;
393 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
395 m_Filters
.DeleteContents(TRUE
);
396 m_Filters
.Append(filter
);
402 void wxHtmlWindow::OnLinkClicked(const wxString
& link
)
409 void wxHtmlWindow::OnDraw(wxDC
& dc
)
412 wxRegionIterator
upd(GetUpdateRegion()); // get the update rect list
415 if (!m_tmpCanDraw
) return;
416 dc
.SetMapMode(wxMM_TEXT
);
417 #if defined(_MSC_VER) && (_MSC_VER == 1200)
418 ::SetMapMode((HDC
)dc
.GetHDC(), MM_TEXT
);
420 dc
.SetBackgroundMode(wxTRANSPARENT
);
426 if (m_Cell
) m_Cell
-> Draw(dc
, 0, 0, y
* HTML_SCROLL_STEP
+ v_y
, y
* HTML_SCROLL_STEP
+ v_h
+ v_y
);
434 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
436 wxScrolledWindow::OnSize(event
);
442 void wxHtmlWindow::OnKeyDown(wxKeyEvent
& event
)
447 ViewStart(&dummy
, &sty
);
448 GetClientSize(&dummy
, &cliy
); cliy
/= HTML_SCROLL_STEP
;
449 GetVirtualSize(&dummy
, &szy
); szy
/= HTML_SCROLL_STEP
;
451 switch (event
.KeyCode()) {
454 Scroll(-1, sty
- cliy
);
458 Scroll(-1, sty
+ cliy
);
464 Scroll(-1, szy
- cliy
);
477 void wxHtmlWindow::OnMouseEvent(wxMouseEvent
& event
)
479 m_tmpMouseMoved
= TRUE
;
481 if (event
.ButtonDown()) {
486 ViewStart(&sx
, &sy
); sx
*= HTML_SCROLL_STEP
; sy
*= HTML_SCROLL_STEP
;
487 pos
= event
.GetPosition();
490 m_Cell
-> OnMouseClick(this, sx
+ pos
.x
, sy
+ pos
.y
, event
.ButtonDown(1), event
.ButtonDown(2), event
.ButtonDown(3));
496 void wxHtmlWindow::OnIdle(wxIdleEvent
& event
)
498 static wxCursor
cur_hand(wxCURSOR_HAND
), cur_arrow(wxCURSOR_ARROW
);
500 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
)) {
505 ViewStart(&sx
, &sy
); sx
*= HTML_SCROLL_STEP
; sy
*= HTML_SCROLL_STEP
;
506 wxGetMousePosition(&x
, &y
);
507 ScreenToClient(&x
, &y
);
508 lnk
= m_Cell
-> GetLink(sx
+ x
, sy
+ y
);
510 if (lnk
== wxEmptyString
) {
511 SetCursor(cur_arrow
);
512 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(wxEmptyString
, m_RelatedStatusBar
);
516 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
-> SetStatusText(lnk
, m_RelatedStatusBar
);
518 m_tmpMouseMoved
= FALSE
;
525 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
527 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
528 EVT_SIZE(wxHtmlWindow::OnSize
)
529 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent
)
530 EVT_MOTION(wxHtmlWindow::OnMouseEvent
)
531 EVT_IDLE(wxHtmlWindow::OnIdle
)
532 EVT_KEY_DOWN(wxHtmlWindow::OnKeyDown
)
539 // A module to allow initialization/cleanup
540 // without calling these functions from app.cpp or from
541 // the user's application.
543 class wxHtmlWinModule
: public wxModule
545 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
547 wxHtmlWinModule() : wxModule() {}
548 bool OnInit() { return TRUE
; }
549 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
552 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
557 ///// default mod handlers are forced there:
559 FORCE_LINK(mod_layout
)
560 FORCE_LINK(mod_fonts
)
561 FORCE_LINK(mod_image
)
564 FORCE_LINK(mod_hline
)
565 FORCE_LINK(mod_links
)
566 FORCE_LINK(mod_tables
)