]>
Commit | Line | Data |
---|---|---|
5526e819 VS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: htmlwin.cpp | |
3 | // Purpose: wxHtmlWindow class for parsing & displaying HTML (implementation) | |
4 | // Author: Vaclav Slavik | |
5 | // Copyright: (c) 1999 Vaclav Slavik | |
6 | // Licence: wxWindows Licence | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | ||
10 | #ifdef __GNUG__ | |
11 | #pragma implementation | |
12 | #endif | |
13 | ||
14 | #include <wx/wxprec.h> | |
15 | ||
16 | #include "wx/defs.h" | |
17 | #if wxUSE_HTML | |
18 | ||
19 | #ifdef __BORDLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #ifndef WXPRECOMP | |
24 | #include <wx/wx.h> | |
25 | #endif | |
26 | ||
27 | #include <wx/html/htmlwin.h> | |
28 | ||
29 | #include <wx/html/forcelink.h> | |
30 | ||
31 | ///// This is my own wxBusyCursor. It works only with one window. | |
32 | ||
33 | #if (defined __WXGTK__) && (wxVERSION_NUMBER < 2100) | |
34 | class wxLocalBusyCursor | |
35 | #else | |
36 | class wxLocalBusyCursor : public wxBusyCursor | |
37 | #endif | |
38 | { | |
39 | private: | |
40 | wxWindow *m_Wnd; | |
41 | public: | |
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);} | |
45 | #else | |
46 | wxLocalBusyCursor(wxWindow *w) : wxBusyCursor() {} | |
47 | #endif | |
48 | }; | |
49 | ||
50 | ||
51 | ||
52 | ||
53 | //----------------------------------------------------------------------------- | |
54 | // wxHtmlWindow | |
55 | //----------------------------------------------------------------------------- | |
56 | ||
57 | ||
58 | ||
59 | #include <wx/arrimpl.cpp> | |
60 | WX_DEFINE_OBJARRAY(HtmlHistoryArray) | |
61 | ||
62 | ||
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) | |
65 | { | |
66 | m_tmpMouseMoved = FALSE; | |
67 | m_tmpCanDraw = TRUE; | |
68 | m_FS = new wxFileSystem(); | |
69 | m_RelatedStatusBar = -1; | |
70 | m_RelatedFrame = NULL; | |
71 | m_TitleFormat = "%s"; | |
72 | m_OpenedPage = m_OpenedAnchor = wxEmptyString; | |
73 | m_Cell = NULL; | |
74 | m_Parser = new wxHtmlWinParser(this); | |
75 | m_Parser -> SetFS(m_FS); | |
76 | SetBorders(10); | |
77 | m_HistoryPos = -1; | |
78 | m_HistoryOn = TRUE; | |
79 | m_Scrollable = scrollable; | |
80 | SetPage("<html><body></body></html>"); | |
81 | } | |
82 | ||
83 | ||
84 | ||
85 | wxHtmlWindow::~wxHtmlWindow() | |
86 | { | |
87 | HistoryClear(); | |
88 | ||
89 | if (m_Cell) delete m_Cell; | |
90 | ||
91 | wxList *parser_data = m_Parser -> GetTempData(); | |
92 | if (parser_data) delete parser_data; | |
93 | ||
94 | delete m_Parser; | |
95 | delete m_FS; | |
96 | } | |
97 | ||
98 | ||
99 | ||
100 | void wxHtmlWindow::SetRelatedFrame(wxFrame* frame, const wxString& format) | |
101 | { | |
102 | m_RelatedFrame = frame; | |
103 | m_TitleFormat = format; | |
104 | } | |
105 | ||
106 | ||
107 | ||
108 | void wxHtmlWindow::SetRelatedStatusBar(int bar) | |
109 | { | |
110 | m_RelatedStatusBar = bar; | |
111 | } | |
112 | ||
113 | ||
114 | ||
115 | void wxHtmlWindow::SetFonts(wxString normal_face, int normal_italic_mode, wxString fixed_face, int fixed_italic_mode, int *sizes) | |
116 | { | |
117 | m_Parser -> SetFonts(normal_face, normal_italic_mode, fixed_face, fixed_italic_mode, sizes); | |
118 | if (!m_OpenedPage.IsEmpty()) LoadPage(m_OpenedPage); | |
119 | } | |
120 | ||
121 | ||
122 | ||
123 | bool wxHtmlWindow::SetPage(const wxString& source) | |
124 | { | |
125 | wxClientDC *dc = new wxClientDC(this); | |
126 | ||
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); | |
133 | delete dc; | |
134 | m_Cell -> SetIndent(m_Borders, HTML_INDENT_ALL, HTML_UNITS_PIXELS); | |
135 | m_Cell -> SetAlignHor(HTML_ALIGN_CENTER); | |
136 | CreateLayout(); | |
137 | Refresh(); | |
138 | return TRUE; | |
139 | } | |
140 | ||
141 | ||
142 | bool wxHtmlWindow::LoadPage(const wxString& location) | |
143 | { | |
144 | wxFSFile *f; | |
145 | bool rt_val; | |
146 | wxLocalBusyCursor b(this); | |
147 | ||
148 | m_tmpCanDraw = FALSE; | |
149 | if (m_HistoryOn && (m_HistoryPos != -1)) { // store scroll position into history item | |
150 | int x, y; | |
151 | ViewStart(&x, &y); | |
152 | m_History[m_HistoryPos].SetPos(y); | |
153 | } | |
154 | ||
155 | if (location[0] == '#') { // local anchor | |
156 | wxString anch = location.Mid(1) /*1 to end*/; | |
157 | m_tmpCanDraw = TRUE; | |
158 | rt_val = ScrollToAnchor(anch); | |
159 | } | |
160 | ||
161 | else { | |
162 | // load&display it: | |
163 | if (m_RelatedStatusBar != -1) { | |
164 | m_RelatedFrame -> SetStatusText(_("Connecting..."), m_RelatedStatusBar); | |
165 | Refresh(); | |
166 | } | |
167 | ||
168 | f = m_FS -> OpenFile(location); | |
169 | if (f == NULL) { | |
170 | wxString err; | |
171 | ||
172 | err.Printf(_("The browser is unable to open requested location :\n\n%s"), WXSTRINGCAST location); | |
5526e819 | 173 | m_tmpCanDraw = TRUE; |
483ff5a5 VS |
174 | Refresh(); |
175 | wxMessageBox(err, "Error"); | |
5526e819 VS |
176 | return FALSE; |
177 | } | |
178 | ||
179 | else { | |
180 | wxNode *node; | |
181 | wxString src = wxEmptyString; | |
182 | ||
183 | if (m_RelatedStatusBar != -1) { | |
184 | wxString msg = _("Loading : ") + location; | |
185 | m_RelatedFrame -> SetStatusText(msg, m_RelatedStatusBar); | |
186 | Refresh(); | |
187 | } | |
188 | ||
189 | node = m_Filters.GetFirst(); | |
190 | while (node){ | |
191 | wxHtmlFilter *h = (wxHtmlFilter*) node -> GetData(); | |
192 | if (h -> CanRead(*f)) { | |
193 | src = h -> ReadFile(*f); | |
194 | break; | |
195 | } | |
196 | node = node -> GetNext(); | |
197 | } | |
198 | if (src == wxEmptyString) src = m_DefaultFilter.ReadFile(*f); | |
199 | ||
200 | m_FS -> ChangePathTo(f -> GetLocation()); | |
201 | rt_val = SetPage(src); | |
202 | m_OpenedPage = f -> GetLocation(); | |
203 | if (f -> GetAnchor() != wxEmptyString) { | |
204 | m_tmpCanDraw = TRUE; | |
205 | ScrollToAnchor(f -> GetAnchor()); | |
206 | m_tmpCanDraw = FALSE; | |
207 | } | |
208 | ||
209 | delete f; | |
210 | ||
211 | if (m_RelatedStatusBar != -1) m_RelatedFrame -> SetStatusText(_("Done"), m_RelatedStatusBar); | |
212 | } | |
213 | } | |
214 | ||
215 | if (m_HistoryOn) { // add this page to history there: | |
216 | int c = m_History.GetCount() - (m_HistoryPos + 1); | |
217 | ||
218 | m_HistoryPos++; | |
219 | for (int i = 0; i < c; i++) | |
220 | m_History.Remove(m_HistoryPos); | |
221 | m_History.Add(new HtmlHistoryItem(m_OpenedPage, m_OpenedAnchor)); | |
222 | } | |
223 | ||
224 | m_tmpCanDraw = TRUE; | |
225 | Refresh(); | |
226 | return rt_val; | |
227 | } | |
228 | ||
229 | ||
230 | ||
231 | bool wxHtmlWindow::ScrollToAnchor(const wxString& anchor) | |
232 | { | |
233 | const wxHtmlCell *c = m_Cell -> Find(HTML_COND_ISANCHOR, &anchor); | |
234 | if (!c) return FALSE; | |
235 | else { | |
236 | int y; | |
237 | ||
238 | for (y = 0; c != NULL; c = c -> GetParent()) y += c -> GetPosY(); | |
239 | Scroll(-1, y / HTML_SCROLL_STEP); | |
240 | m_OpenedAnchor = anchor; | |
241 | return TRUE; | |
242 | } | |
243 | } | |
244 | ||
245 | ||
246 | void wxHtmlWindow::SetTitle(const wxString& title) | |
247 | { | |
248 | if (m_RelatedFrame) { | |
249 | wxString tit; | |
250 | tit.Printf(m_TitleFormat, title.c_str()); | |
251 | m_RelatedFrame -> SetTitle(tit); | |
252 | } | |
253 | } | |
254 | ||
255 | ||
256 | ||
257 | ||
258 | ||
259 | void wxHtmlWindow::CreateLayout() | |
260 | { | |
261 | int ClientWidth, ClientHeight; | |
262 | ||
263 | if (!m_Cell) return; | |
264 | GetClientSize(&ClientWidth, &ClientHeight); | |
265 | m_Cell -> Layout(ClientWidth); | |
266 | if (m_Scrollable) | |
267 | SetScrollbars(HTML_SCROLL_STEP, HTML_SCROLL_STEP, | |
268 | m_Cell -> GetWidth() / HTML_SCROLL_STEP, | |
269 | m_Cell -> GetHeight() / HTML_SCROLL_STEP | |
270 | /*cheat: top-level frag is always container*/ ); | |
271 | } | |
272 | ||
273 | ||
274 | ||
275 | void wxHtmlWindow::ReadCustomization(wxConfigBase *cfg, wxString path) | |
276 | { | |
277 | wxString oldpath; | |
278 | wxString tmp; | |
279 | ||
280 | if (path != wxEmptyString) { | |
281 | oldpath = cfg -> GetPath(); | |
282 | cfg -> SetPath(path); | |
283 | } | |
284 | ||
285 | m_Borders = cfg -> Read("wxHtmlWindow/Borders", m_Borders); | |
286 | m_Parser -> m_FontFaceFixed = cfg -> Read("wxHtmlWindow/FontFaceFixed", m_Parser -> m_FontFaceFixed); | |
287 | m_Parser -> m_FontFaceNormal = cfg -> Read("wxHtmlWindow/FontFaceNormal", m_Parser -> m_FontFaceNormal); | |
288 | m_Parser -> m_ItalicModeFixed = cfg -> Read("wxHtmlWindow/ItalicModeFixed", m_Parser -> m_ItalicModeFixed); | |
289 | m_Parser -> m_ItalicModeNormal = cfg -> Read("wxHtmlWindow/ItalicModeNormal", m_Parser -> m_ItalicModeNormal); | |
290 | for (int i = 0; i < 7; i++) { | |
291 | tmp.Printf("wxHtmlWindow/FontsSize%i", i); | |
292 | m_Parser -> m_FontsSizes[i] = cfg -> Read(tmp, m_Parser -> m_FontsSizes[i]); | |
293 | } | |
294 | ||
295 | if (path != wxEmptyString) | |
296 | cfg -> SetPath(oldpath); | |
297 | } | |
298 | ||
299 | ||
300 | ||
301 | void wxHtmlWindow::WriteCustomization(wxConfigBase *cfg, wxString path) | |
302 | { | |
303 | wxString oldpath; | |
304 | wxString tmp; | |
305 | ||
306 | if (path != wxEmptyString) { | |
307 | oldpath = cfg -> GetPath(); | |
308 | cfg -> SetPath(path); | |
309 | } | |
310 | ||
311 | cfg -> Write("wxHtmlWindow/Borders", (long) m_Borders); | |
312 | cfg -> Write("wxHtmlWindow/FontFaceFixed", m_Parser -> m_FontFaceFixed); | |
313 | cfg -> Write("wxHtmlWindow/FontFaceNormal", m_Parser -> m_FontFaceNormal); | |
314 | cfg -> Write("wxHtmlWindow/ItalicModeFixed", (long) m_Parser -> m_ItalicModeFixed); | |
315 | cfg -> Write("wxHtmlWindow/ItalicModeNormal", (long) m_Parser -> m_ItalicModeNormal); | |
316 | for (int i = 0; i < 7; i++) { | |
317 | tmp.Printf("wxHtmlWindow/FontsSize%i", i); | |
318 | cfg -> Write(tmp, (long) m_Parser -> m_FontsSizes[i]); | |
319 | } | |
320 | ||
321 | if (path != wxEmptyString) | |
322 | cfg -> SetPath(oldpath); | |
323 | } | |
324 | ||
325 | ||
326 | ||
327 | bool wxHtmlWindow::HistoryBack() | |
328 | { | |
329 | wxString a, l; | |
330 | ||
331 | if (m_HistoryPos < 1) return FALSE; | |
332 | ||
333 | m_HistoryPos--; | |
334 | ||
335 | l = m_History[m_HistoryPos].GetPage(); | |
336 | a = m_History[m_HistoryPos].GetAnchor(); | |
337 | m_HistoryOn = FALSE; | |
338 | if (a == wxEmptyString) LoadPage(l); | |
339 | else LoadPage(l + "#" + a); | |
340 | m_HistoryOn = TRUE; | |
341 | Scroll(0, m_History[m_HistoryPos].GetPos()); | |
342 | Refresh(); | |
343 | return TRUE; | |
344 | } | |
345 | ||
346 | ||
347 | ||
348 | bool wxHtmlWindow::HistoryForward() | |
349 | { | |
350 | wxString a, l; | |
351 | ||
352 | if (m_HistoryPos == -1) return FALSE; | |
353 | if (m_HistoryPos >= (int)m_History.GetCount() - 1)return FALSE; | |
354 | ||
355 | m_OpenedPage = wxEmptyString; // this will disable adding new entry into history in LoadPage() | |
356 | ||
357 | m_HistoryPos++; | |
358 | l = m_History[m_HistoryPos].GetPage(); | |
359 | a = m_History[m_HistoryPos].GetAnchor(); | |
360 | m_HistoryOn = FALSE; | |
361 | if (a == wxEmptyString) LoadPage(l); | |
362 | else LoadPage(l + "#" + a); | |
363 | m_HistoryOn = TRUE; | |
364 | Scroll(0, m_History[m_HistoryPos].GetPos()); | |
365 | Refresh(); | |
366 | return TRUE; | |
367 | } | |
368 | ||
369 | ||
370 | ||
371 | void wxHtmlWindow::HistoryClear() | |
372 | { | |
373 | m_History.Empty(); | |
374 | m_HistoryPos = -1; | |
375 | } | |
376 | ||
377 | ||
378 | ||
379 | wxList wxHtmlWindow::m_Filters; | |
380 | wxHtmlFilterPlainText wxHtmlWindow::m_DefaultFilter; | |
381 | ||
382 | void wxHtmlWindow::AddFilter(wxHtmlFilter *filter) | |
383 | { | |
384 | m_Filters.DeleteContents(TRUE); | |
385 | m_Filters.Append(filter); | |
386 | } | |
387 | ||
388 | ||
389 | ||
390 | ||
391 | void wxHtmlWindow::OnLinkClicked(const wxString& link) | |
392 | { | |
393 | LoadPage(link); | |
394 | } | |
395 | ||
396 | ||
397 | ||
398 | void wxHtmlWindow::OnDraw(wxDC& dc) | |
399 | { | |
400 | int x, y; | |
401 | wxRegionIterator upd(GetUpdateRegion()); // get the update rect list | |
402 | int v_y, v_h; | |
403 | ||
404 | if (!m_tmpCanDraw) return; | |
405 | dc.SetMapMode(wxMM_TEXT); | |
406 | #if defined(_MSC_VER) && (_MSC_VER == 1200) | |
407 | ::SetMapMode((HDC)dc.GetHDC(), MM_TEXT); | |
408 | #endif | |
409 | dc.SetBackgroundMode(wxTRANSPARENT); | |
410 | ViewStart(&x, &y); | |
411 | ||
412 | while (upd) { | |
413 | v_y = upd.GetY(); | |
414 | v_h = upd.GetH(); | |
415 | if (m_Cell) m_Cell -> Draw(dc, 0, 0, y * HTML_SCROLL_STEP + v_y, y * HTML_SCROLL_STEP + v_h + v_y); | |
416 | upd++; | |
417 | } | |
418 | } | |
419 | ||
420 | ||
421 | ||
422 | ||
423 | void wxHtmlWindow::OnSize(wxSizeEvent& event) | |
424 | { | |
425 | wxScrolledWindow::OnSize(event); | |
426 | CreateLayout(); | |
427 | } | |
428 | ||
429 | ||
430 | ||
431 | void wxHtmlWindow::OnKeyDown(wxKeyEvent& event) | |
432 | { | |
433 | int dummy; | |
434 | int sty, szy, cliy; | |
435 | ||
436 | ViewStart(&dummy, &sty); | |
437 | GetClientSize(&dummy, &cliy); cliy /= HTML_SCROLL_STEP; | |
438 | GetVirtualSize(&dummy, &szy); szy /= HTML_SCROLL_STEP; | |
439 | ||
440 | switch (event.KeyCode()) { | |
441 | case WXK_PAGEUP : | |
442 | case WXK_PRIOR : | |
443 | Scroll(-1, sty - cliy); | |
444 | break; | |
445 | case WXK_PAGEDOWN : | |
446 | case WXK_NEXT : | |
447 | Scroll(-1, sty + cliy); | |
448 | break; | |
449 | case WXK_HOME : | |
450 | Scroll(-1, 0); | |
451 | break; | |
452 | case WXK_END : | |
453 | Scroll(-1, szy - cliy); | |
454 | break; | |
455 | case WXK_UP : | |
456 | Scroll(-1, sty - 1); | |
457 | break; | |
458 | case WXK_DOWN : | |
459 | Scroll(-1, sty + 1); | |
460 | break; | |
461 | } | |
462 | } | |
463 | ||
464 | ||
465 | ||
466 | void wxHtmlWindow::OnMouseEvent(wxMouseEvent& event) | |
467 | { | |
468 | m_tmpMouseMoved = TRUE; | |
469 | ||
470 | if (event.ButtonDown()) { | |
471 | int sx, sy; | |
472 | wxPoint pos; | |
473 | wxString lnk; | |
474 | ||
475 | ViewStart(&sx, &sy); sx *= HTML_SCROLL_STEP; sy *= HTML_SCROLL_STEP; | |
476 | pos = event.GetPosition(); | |
477 | ||
478 | if (m_Cell) | |
479 | m_Cell -> OnMouseClick(this, sx + pos.x, sy + pos.y, event.ButtonDown(1), event.ButtonDown(2), event.ButtonDown(3)); | |
480 | } | |
481 | } | |
482 | ||
483 | ||
484 | ||
485 | void wxHtmlWindow::OnIdle(wxIdleEvent& event) | |
486 | { | |
487 | static wxCursor cur_hand(wxCURSOR_HAND), cur_arrow(wxCURSOR_ARROW); | |
488 | ||
489 | if (m_tmpMouseMoved && (m_Cell != NULL)) { | |
490 | int sx, sy; | |
491 | int x, y; | |
492 | wxString lnk; | |
493 | ||
494 | ViewStart(&sx, &sy); sx *= HTML_SCROLL_STEP; sy *= HTML_SCROLL_STEP; | |
495 | wxGetMousePosition(&x, &y); | |
496 | ScreenToClient(&x, &y); | |
497 | lnk = m_Cell -> GetLink(sx + x, sy + y); | |
498 | ||
499 | if (lnk == wxEmptyString) { | |
500 | SetCursor(cur_arrow); | |
501 | if (m_RelatedStatusBar != -1) m_RelatedFrame -> SetStatusText(wxEmptyString, m_RelatedStatusBar); | |
502 | } | |
503 | else { | |
504 | SetCursor(cur_hand); | |
505 | if (m_RelatedStatusBar != -1) m_RelatedFrame -> SetStatusText(lnk, m_RelatedStatusBar); | |
506 | } | |
507 | m_tmpMouseMoved = FALSE; | |
508 | } | |
509 | } | |
510 | ||
511 | ||
512 | ||
513 | ||
514 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow,wxScrolledWindow) | |
515 | ||
516 | BEGIN_EVENT_TABLE(wxHtmlWindow, wxScrolledWindow) | |
517 | EVT_SIZE(wxHtmlWindow::OnSize) | |
518 | EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent) | |
519 | EVT_MOTION(wxHtmlWindow::OnMouseEvent) | |
520 | EVT_IDLE(wxHtmlWindow::OnIdle) | |
521 | EVT_KEY_DOWN(wxHtmlWindow::OnKeyDown) | |
522 | END_EVENT_TABLE() | |
523 | ||
524 | ||
525 | ||
526 | ||
527 | ||
528 | ||
529 | ||
530 | ||
531 | ///// default mod handlers are forced there: | |
532 | ||
533 | FORCE_LINK(mod_layout) | |
534 | FORCE_LINK(mod_fonts) | |
535 | FORCE_LINK(mod_image) | |
536 | FORCE_LINK(mod_list) | |
537 | FORCE_LINK(mod_pre) | |
538 | FORCE_LINK(mod_hline) | |
539 | FORCE_LINK(mod_links) | |
540 | FORCE_LINK(mod_tables) | |
541 | ||
542 | ||
483ff5a5 | 543 | #endif |