]>
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 | ||
5526e819 VS |
31 | |
32 | ||
33 | //----------------------------------------------------------------------------- | |
34 | // wxHtmlWindow | |
35 | //----------------------------------------------------------------------------- | |
36 | ||
37 | ||
38 | ||
39 | #include <wx/arrimpl.cpp> | |
40 | WX_DEFINE_OBJARRAY(HtmlHistoryArray) | |
41 | ||
42 | ||
43 | wxHtmlWindow::wxHtmlWindow(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, | |
a547ebff | 44 | long style, const wxString& name) : wxScrolledWindow(parent, id, pos, size, wxVSCROLL, name) |
5526e819 VS |
45 | { |
46 | m_tmpMouseMoved = FALSE; | |
47 | m_tmpCanDraw = TRUE; | |
48 | m_FS = new wxFileSystem(); | |
49 | m_RelatedStatusBar = -1; | |
50 | m_RelatedFrame = NULL; | |
51 | m_TitleFormat = "%s"; | |
52 | m_OpenedPage = m_OpenedAnchor = wxEmptyString; | |
53 | m_Cell = NULL; | |
54 | m_Parser = new wxHtmlWinParser(this); | |
55 | m_Parser -> SetFS(m_FS); | |
56 | SetBorders(10); | |
57 | m_HistoryPos = -1; | |
58 | m_HistoryOn = TRUE; | |
a547ebff | 59 | m_Style = style; |
5526e819 VS |
60 | SetPage("<html><body></body></html>"); |
61 | } | |
62 | ||
63 | ||
64 | ||
65 | wxHtmlWindow::~wxHtmlWindow() | |
66 | { | |
67 | HistoryClear(); | |
68 | ||
69 | if (m_Cell) delete m_Cell; | |
70 | ||
71 | wxList *parser_data = m_Parser -> GetTempData(); | |
72 | if (parser_data) delete parser_data; | |
73 | ||
74 | delete m_Parser; | |
75 | delete m_FS; | |
76 | } | |
77 | ||
78 | ||
79 | ||
80 | void wxHtmlWindow::SetRelatedFrame(wxFrame* frame, const wxString& format) | |
81 | { | |
82 | m_RelatedFrame = frame; | |
83 | m_TitleFormat = format; | |
84 | } | |
85 | ||
86 | ||
87 | ||
88 | void wxHtmlWindow::SetRelatedStatusBar(int bar) | |
89 | { | |
90 | m_RelatedStatusBar = bar; | |
91 | } | |
92 | ||
93 | ||
94 | ||
95 | void wxHtmlWindow::SetFonts(wxString normal_face, int normal_italic_mode, wxString fixed_face, int fixed_italic_mode, int *sizes) | |
96 | { | |
97 | m_Parser -> SetFonts(normal_face, normal_italic_mode, fixed_face, fixed_italic_mode, sizes); | |
98 | if (!m_OpenedPage.IsEmpty()) LoadPage(m_OpenedPage); | |
99 | } | |
100 | ||
101 | ||
102 | ||
103 | bool wxHtmlWindow::SetPage(const wxString& source) | |
104 | { | |
105 | wxClientDC *dc = new wxClientDC(this); | |
106 | ||
107 | dc -> SetMapMode(wxMM_TEXT); | |
108 | SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF)); | |
109 | m_OpenedPage = m_OpenedAnchor = wxEmptyString; | |
110 | m_Parser -> SetDC(dc); | |
f61815af GL |
111 | if (m_Cell) { |
112 | delete m_Cell; | |
113 | m_Cell = NULL; | |
114 | } | |
5526e819 VS |
115 | m_Cell = (wxHtmlContainerCell*) m_Parser -> Parse(source); |
116 | delete dc; | |
117 | m_Cell -> SetIndent(m_Borders, HTML_INDENT_ALL, HTML_UNITS_PIXELS); | |
118 | m_Cell -> SetAlignHor(HTML_ALIGN_CENTER); | |
119 | CreateLayout(); | |
120 | Refresh(); | |
121 | return TRUE; | |
122 | } | |
123 | ||
124 | ||
125 | bool wxHtmlWindow::LoadPage(const wxString& location) | |
126 | { | |
127 | wxFSFile *f; | |
128 | bool rt_val; | |
a547ebff | 129 | wxBusyCursor b; |
5526e819 VS |
130 | |
131 | m_tmpCanDraw = FALSE; | |
132 | if (m_HistoryOn && (m_HistoryPos != -1)) { // store scroll position into history item | |
133 | int x, y; | |
134 | ViewStart(&x, &y); | |
135 | m_History[m_HistoryPos].SetPos(y); | |
136 | } | |
137 | ||
138 | if (location[0] == '#') { // local anchor | |
139 | wxString anch = location.Mid(1) /*1 to end*/; | |
140 | m_tmpCanDraw = TRUE; | |
141 | rt_val = ScrollToAnchor(anch); | |
142 | } | |
143 | ||
144 | else { | |
145 | // load&display it: | |
146 | if (m_RelatedStatusBar != -1) { | |
147 | m_RelatedFrame -> SetStatusText(_("Connecting..."), m_RelatedStatusBar); | |
148 | Refresh(); | |
149 | } | |
150 | ||
151 | f = m_FS -> OpenFile(location); | |
152 | if (f == NULL) { | |
153 | wxString err; | |
154 | ||
155 | err.Printf(_("The browser is unable to open requested location :\n\n%s"), WXSTRINGCAST location); | |
5526e819 | 156 | m_tmpCanDraw = TRUE; |
483ff5a5 VS |
157 | Refresh(); |
158 | wxMessageBox(err, "Error"); | |
5526e819 VS |
159 | return FALSE; |
160 | } | |
161 | ||
162 | else { | |
163 | wxNode *node; | |
164 | wxString src = wxEmptyString; | |
165 | ||
166 | if (m_RelatedStatusBar != -1) { | |
167 | wxString msg = _("Loading : ") + location; | |
168 | m_RelatedFrame -> SetStatusText(msg, m_RelatedStatusBar); | |
169 | Refresh(); | |
170 | } | |
171 | ||
172 | node = m_Filters.GetFirst(); | |
173 | while (node){ | |
174 | wxHtmlFilter *h = (wxHtmlFilter*) node -> GetData(); | |
175 | if (h -> CanRead(*f)) { | |
176 | src = h -> ReadFile(*f); | |
177 | break; | |
178 | } | |
179 | node = node -> GetNext(); | |
180 | } | |
a76015e6 VS |
181 | if (src == wxEmptyString) { |
182 | if (m_DefaultFilter == NULL) m_DefaultFilter = GetDefaultFilter(); | |
183 | src = m_DefaultFilter -> ReadFile(*f); | |
184 | } | |
5526e819 VS |
185 | |
186 | m_FS -> ChangePathTo(f -> GetLocation()); | |
187 | rt_val = SetPage(src); | |
188 | m_OpenedPage = f -> GetLocation(); | |
189 | if (f -> GetAnchor() != wxEmptyString) { | |
190 | m_tmpCanDraw = TRUE; | |
191 | ScrollToAnchor(f -> GetAnchor()); | |
192 | m_tmpCanDraw = FALSE; | |
193 | } | |
194 | ||
195 | delete f; | |
196 | ||
197 | if (m_RelatedStatusBar != -1) m_RelatedFrame -> SetStatusText(_("Done"), m_RelatedStatusBar); | |
198 | } | |
199 | } | |
200 | ||
201 | if (m_HistoryOn) { // add this page to history there: | |
202 | int c = m_History.GetCount() - (m_HistoryPos + 1); | |
203 | ||
204 | m_HistoryPos++; | |
205 | for (int i = 0; i < c; i++) | |
206 | m_History.Remove(m_HistoryPos); | |
207 | m_History.Add(new HtmlHistoryItem(m_OpenedPage, m_OpenedAnchor)); | |
208 | } | |
209 | ||
210 | m_tmpCanDraw = TRUE; | |
211 | Refresh(); | |
212 | return rt_val; | |
213 | } | |
214 | ||
215 | ||
216 | ||
217 | bool wxHtmlWindow::ScrollToAnchor(const wxString& anchor) | |
218 | { | |
219 | const wxHtmlCell *c = m_Cell -> Find(HTML_COND_ISANCHOR, &anchor); | |
220 | if (!c) return FALSE; | |
221 | else { | |
222 | int y; | |
223 | ||
224 | for (y = 0; c != NULL; c = c -> GetParent()) y += c -> GetPosY(); | |
225 | Scroll(-1, y / HTML_SCROLL_STEP); | |
226 | m_OpenedAnchor = anchor; | |
227 | return TRUE; | |
228 | } | |
229 | } | |
230 | ||
231 | ||
232 | void wxHtmlWindow::SetTitle(const wxString& title) | |
233 | { | |
234 | if (m_RelatedFrame) { | |
235 | wxString tit; | |
236 | tit.Printf(m_TitleFormat, title.c_str()); | |
237 | m_RelatedFrame -> SetTitle(tit); | |
238 | } | |
239 | } | |
240 | ||
241 | ||
242 | ||
243 | ||
244 | ||
245 | void wxHtmlWindow::CreateLayout() | |
246 | { | |
247 | int ClientWidth, ClientHeight; | |
248 | ||
249 | if (!m_Cell) return; | |
a547ebff VS |
250 | |
251 | if (m_Style == wxHW_SCROLLBAR_NEVER) { | |
60811dc6 | 252 | SetScrollbars(1, 1, 0, 0); // always off |
a547ebff VS |
253 | GetClientSize(&ClientWidth, &ClientHeight); |
254 | m_Cell -> Layout(ClientWidth); | |
255 | } | |
256 | ||
257 | else { | |
258 | GetClientSize(&ClientWidth, &ClientHeight); | |
259 | SetScrollbars(1, 1, 0, ClientHeight * 2); // always on | |
260 | GetClientSize(&ClientWidth, &ClientHeight); | |
261 | m_Cell -> Layout(ClientWidth); | |
262 | GetClientSize(&ClientWidth, &ClientHeight); | |
263 | if (ClientHeight < m_Cell -> GetHeight()) { | |
264 | SetScrollbars(HTML_SCROLL_STEP, HTML_SCROLL_STEP, | |
265 | m_Cell -> GetWidth() / HTML_SCROLL_STEP, | |
266 | m_Cell -> GetHeight() / HTML_SCROLL_STEP | |
267 | /*cheat: top-level frag is always container*/); | |
268 | } | |
269 | else { /* we fit into window, no need for scrollbars */ | |
270 | SetScrollbars(1, 1, 0, 0); // disable... | |
271 | m_Cell -> Layout(ClientWidth); // ...and relayout | |
272 | } | |
273 | } | |
5526e819 VS |
274 | } |
275 | ||
276 | ||
277 | ||
278 | void wxHtmlWindow::ReadCustomization(wxConfigBase *cfg, wxString path) | |
279 | { | |
280 | wxString oldpath; | |
281 | wxString tmp; | |
282 | ||
283 | if (path != wxEmptyString) { | |
284 | oldpath = cfg -> GetPath(); | |
285 | cfg -> SetPath(path); | |
286 | } | |
287 | ||
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]); | |
296 | } | |
297 | ||
298 | if (path != wxEmptyString) | |
299 | cfg -> SetPath(oldpath); | |
300 | } | |
301 | ||
302 | ||
303 | ||
304 | void wxHtmlWindow::WriteCustomization(wxConfigBase *cfg, wxString path) | |
305 | { | |
306 | wxString oldpath; | |
307 | wxString tmp; | |
308 | ||
309 | if (path != wxEmptyString) { | |
310 | oldpath = cfg -> GetPath(); | |
311 | cfg -> SetPath(path); | |
312 | } | |
313 | ||
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]); | |
322 | } | |
323 | ||
324 | if (path != wxEmptyString) | |
325 | cfg -> SetPath(oldpath); | |
326 | } | |
327 | ||
328 | ||
329 | ||
330 | bool wxHtmlWindow::HistoryBack() | |
331 | { | |
332 | wxString a, l; | |
333 | ||
334 | if (m_HistoryPos < 1) return FALSE; | |
335 | ||
336 | m_HistoryPos--; | |
337 | ||
338 | l = m_History[m_HistoryPos].GetPage(); | |
339 | a = m_History[m_HistoryPos].GetAnchor(); | |
340 | m_HistoryOn = FALSE; | |
341 | if (a == wxEmptyString) LoadPage(l); | |
342 | else LoadPage(l + "#" + a); | |
343 | m_HistoryOn = TRUE; | |
344 | Scroll(0, m_History[m_HistoryPos].GetPos()); | |
345 | Refresh(); | |
346 | return TRUE; | |
347 | } | |
348 | ||
349 | ||
350 | ||
351 | bool wxHtmlWindow::HistoryForward() | |
352 | { | |
353 | wxString a, l; | |
354 | ||
355 | if (m_HistoryPos == -1) return FALSE; | |
356 | if (m_HistoryPos >= (int)m_History.GetCount() - 1)return FALSE; | |
357 | ||
358 | m_OpenedPage = wxEmptyString; // this will disable adding new entry into history in LoadPage() | |
359 | ||
360 | m_HistoryPos++; | |
361 | l = m_History[m_HistoryPos].GetPage(); | |
362 | a = m_History[m_HistoryPos].GetAnchor(); | |
363 | m_HistoryOn = FALSE; | |
364 | if (a == wxEmptyString) LoadPage(l); | |
365 | else LoadPage(l + "#" + a); | |
366 | m_HistoryOn = TRUE; | |
367 | Scroll(0, m_History[m_HistoryPos].GetPos()); | |
368 | Refresh(); | |
369 | return TRUE; | |
370 | } | |
371 | ||
372 | ||
373 | ||
374 | void wxHtmlWindow::HistoryClear() | |
375 | { | |
376 | m_History.Empty(); | |
377 | m_HistoryPos = -1; | |
378 | } | |
379 | ||
380 | ||
381 | ||
382 | wxList wxHtmlWindow::m_Filters; | |
a76015e6 VS |
383 | wxHtmlFilter *wxHtmlWindow::m_DefaultFilter = NULL; |
384 | ||
385 | void wxHtmlWindow::CleanUpStatics() | |
386 | { | |
387 | if (m_DefaultFilter) delete m_DefaultFilter; | |
388 | m_DefaultFilter = NULL; | |
389 | } | |
390 | ||
391 | ||
5526e819 VS |
392 | |
393 | void wxHtmlWindow::AddFilter(wxHtmlFilter *filter) | |
394 | { | |
395 | m_Filters.DeleteContents(TRUE); | |
396 | m_Filters.Append(filter); | |
397 | } | |
398 | ||
399 | ||
400 | ||
401 | ||
402 | void wxHtmlWindow::OnLinkClicked(const wxString& link) | |
403 | { | |
404 | LoadPage(link); | |
405 | } | |
406 | ||
407 | ||
408 | ||
409 | void wxHtmlWindow::OnDraw(wxDC& dc) | |
410 | { | |
411 | int x, y; | |
412 | wxRegionIterator upd(GetUpdateRegion()); // get the update rect list | |
413 | int v_y, v_h; | |
414 | ||
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); | |
419 | #endif | |
420 | dc.SetBackgroundMode(wxTRANSPARENT); | |
421 | ViewStart(&x, &y); | |
422 | ||
423 | while (upd) { | |
424 | v_y = upd.GetY(); | |
425 | v_h = upd.GetH(); | |
426 | if (m_Cell) m_Cell -> Draw(dc, 0, 0, y * HTML_SCROLL_STEP + v_y, y * HTML_SCROLL_STEP + v_h + v_y); | |
427 | upd++; | |
428 | } | |
429 | } | |
430 | ||
431 | ||
432 | ||
433 | ||
434 | void wxHtmlWindow::OnSize(wxSizeEvent& event) | |
435 | { | |
436 | wxScrolledWindow::OnSize(event); | |
437 | CreateLayout(); | |
438 | } | |
439 | ||
440 | ||
441 | ||
442 | void wxHtmlWindow::OnKeyDown(wxKeyEvent& event) | |
443 | { | |
444 | int dummy; | |
445 | int sty, szy, cliy; | |
446 | ||
447 | ViewStart(&dummy, &sty); | |
448 | GetClientSize(&dummy, &cliy); cliy /= HTML_SCROLL_STEP; | |
449 | GetVirtualSize(&dummy, &szy); szy /= HTML_SCROLL_STEP; | |
450 | ||
451 | switch (event.KeyCode()) { | |
452 | case WXK_PAGEUP : | |
453 | case WXK_PRIOR : | |
454 | Scroll(-1, sty - cliy); | |
455 | break; | |
456 | case WXK_PAGEDOWN : | |
457 | case WXK_NEXT : | |
458 | Scroll(-1, sty + cliy); | |
459 | break; | |
460 | case WXK_HOME : | |
461 | Scroll(-1, 0); | |
462 | break; | |
463 | case WXK_END : | |
464 | Scroll(-1, szy - cliy); | |
465 | break; | |
466 | case WXK_UP : | |
467 | Scroll(-1, sty - 1); | |
468 | break; | |
469 | case WXK_DOWN : | |
470 | Scroll(-1, sty + 1); | |
471 | break; | |
472 | } | |
473 | } | |
474 | ||
475 | ||
476 | ||
477 | void wxHtmlWindow::OnMouseEvent(wxMouseEvent& event) | |
478 | { | |
479 | m_tmpMouseMoved = TRUE; | |
480 | ||
481 | if (event.ButtonDown()) { | |
482 | int sx, sy; | |
483 | wxPoint pos; | |
484 | wxString lnk; | |
485 | ||
486 | ViewStart(&sx, &sy); sx *= HTML_SCROLL_STEP; sy *= HTML_SCROLL_STEP; | |
487 | pos = event.GetPosition(); | |
488 | ||
489 | if (m_Cell) | |
490 | m_Cell -> OnMouseClick(this, sx + pos.x, sy + pos.y, event.ButtonDown(1), event.ButtonDown(2), event.ButtonDown(3)); | |
491 | } | |
492 | } | |
493 | ||
494 | ||
495 | ||
496 | void wxHtmlWindow::OnIdle(wxIdleEvent& event) | |
497 | { | |
498 | static wxCursor cur_hand(wxCURSOR_HAND), cur_arrow(wxCURSOR_ARROW); | |
499 | ||
500 | if (m_tmpMouseMoved && (m_Cell != NULL)) { | |
501 | int sx, sy; | |
502 | int x, y; | |
503 | wxString lnk; | |
504 | ||
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); | |
509 | ||
510 | if (lnk == wxEmptyString) { | |
511 | SetCursor(cur_arrow); | |
512 | if (m_RelatedStatusBar != -1) m_RelatedFrame -> SetStatusText(wxEmptyString, m_RelatedStatusBar); | |
513 | } | |
514 | else { | |
515 | SetCursor(cur_hand); | |
516 | if (m_RelatedStatusBar != -1) m_RelatedFrame -> SetStatusText(lnk, m_RelatedStatusBar); | |
517 | } | |
518 | m_tmpMouseMoved = FALSE; | |
519 | } | |
520 | } | |
521 | ||
522 | ||
523 | ||
524 | ||
525 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow,wxScrolledWindow) | |
526 | ||
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) | |
533 | END_EVENT_TABLE() | |
534 | ||
535 | ||
536 | ||
537 | ||
538 | ||
a76015e6 VS |
539 | // A module to allow initialization/cleanup |
540 | // without calling these functions from app.cpp or from | |
541 | // the user's application. | |
542 | ||
543 | class wxHtmlWinModule: public wxModule | |
544 | { | |
545 | DECLARE_DYNAMIC_CLASS(wxHtmlWinModule) | |
546 | public: | |
547 | wxHtmlWinModule() : wxModule() {} | |
548 | bool OnInit() { return TRUE; } | |
549 | void OnExit() { wxHtmlWindow::CleanUpStatics(); } | |
550 | }; | |
551 | ||
552 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule, wxModule) | |
553 | ||
5526e819 VS |
554 | |
555 | ||
556 | ||
557 | ///// default mod handlers are forced there: | |
558 | ||
559 | FORCE_LINK(mod_layout) | |
560 | FORCE_LINK(mod_fonts) | |
561 | FORCE_LINK(mod_image) | |
562 | FORCE_LINK(mod_list) | |
563 | FORCE_LINK(mod_pre) | |
564 | FORCE_LINK(mod_hline) | |
565 | FORCE_LINK(mod_links) | |
566 | FORCE_LINK(mod_tables) | |
567 | ||
568 | ||
483ff5a5 | 569 | #endif |