]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmlwin.cpp
fixed bug in wxHtmlWindow: HistoryBack/Forward now correctly preserves last entry...
[wxWidgets.git] / src / html / htmlwin.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: htmlwin.cpp
3 // Purpose: wxHtmlWindow class for parsing & displaying HTML (implementation)
4 // Author: Vaclav Slavik
5 // RCS-ID: $Id$
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
9
10
11 #ifdef __GNUG__
12 #pragma implementation
13 #endif
14
15 #include "wx/wxprec.h"
16
17 #include "wx/defs.h"
18 #if wxUSE_HTML && wxUSE_STREAMS
19
20 #ifdef __BORDLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WXPRECOMP
25 #include "wx/wx.h"
26 #endif
27
28 #include "wx/html/htmlwin.h"
29 #include "wx/html/forcelnk.h"
30 #include "wx/log.h"
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,
44 long style, const wxString& name) : wxScrolledWindow(parent, id, pos, size, style | wxVSCROLL | wxHSCROLL, name)
45 {
46 m_tmpMouseMoved = FALSE;
47 m_tmpLastLink = NULL;
48 m_tmpCanDrawLocks = 0;
49 m_FS = new wxFileSystem();
50 m_RelatedStatusBar = -1;
51 m_RelatedFrame = NULL;
52 m_TitleFormat = "%s";
53 m_OpenedPage = m_OpenedAnchor = m_OpenedPageTitle = wxEmptyString;
54 m_Cell = NULL;
55 m_Parser = new wxHtmlWinParser(this);
56 m_Parser -> SetFS(m_FS);
57 SetBorders(10);
58 m_HistoryPos = -1;
59 m_HistoryOn = TRUE;
60 m_Style = style;
61 SetPage("<html><body></body></html>");
62 }
63
64
65
66 wxHtmlWindow::~wxHtmlWindow()
67 {
68 HistoryClear();
69
70 if (m_Cell) delete m_Cell;
71
72 delete m_Parser;
73 delete m_FS;
74 }
75
76
77
78 void wxHtmlWindow::SetRelatedFrame(wxFrame* frame, const wxString& format)
79 {
80 m_RelatedFrame = frame;
81 m_TitleFormat = format;
82 }
83
84
85
86 void wxHtmlWindow::SetRelatedStatusBar(int bar)
87 {
88 m_RelatedStatusBar = bar;
89 }
90
91
92
93 void wxHtmlWindow::SetFonts(wxString normal_face, wxString fixed_face, const int *sizes)
94 {
95 wxString op = m_OpenedPage;
96
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);
100 }
101
102
103
104 bool wxHtmlWindow::SetPage(const wxString& source)
105 {
106 wxClientDC *dc = new wxClientDC(this);
107
108 dc -> SetMapMode(wxMM_TEXT);
109 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
110 m_OpenedPage = m_OpenedAnchor = m_OpenedPageTitle = wxEmptyString;
111 m_Parser -> SetDC(dc);
112 if (m_Cell) {
113 delete m_Cell;
114 m_Cell = NULL;
115 }
116 m_Cell = (wxHtmlContainerCell*) m_Parser -> Parse(source);
117 delete dc;
118 m_Cell -> SetIndent(m_Borders, wxHTML_INDENT_ALL, wxHTML_UNITS_PIXELS);
119 m_Cell -> SetAlignHor(wxHTML_ALIGN_CENTER);
120 CreateLayout();
121 if (m_tmpCanDrawLocks == 0) Refresh();
122 return TRUE;
123 }
124
125
126 bool wxHtmlWindow::LoadPage(const wxString& location)
127 {
128 wxFSFile *f;
129 bool rt_val;
130 bool needs_refresh = FALSE;
131
132 SetCursor(*wxHOURGLASS_CURSOR);
133 wxYield(); Refresh(FALSE);
134
135 m_tmpCanDrawLocks++;
136 if (m_HistoryOn && (m_HistoryPos != -1)) { // store scroll position into history item
137 int x, y;
138 ViewStart(&x, &y);
139 m_History[m_HistoryPos].SetPos(y);
140 }
141
142 if (location[0] == wxT('#')) { // local anchor
143 wxString anch = location.Mid(1) /*1 to end*/;
144 m_tmpCanDrawLocks--;
145 rt_val = ScrollToAnchor(anch);
146 m_tmpCanDrawLocks++;
147 }
148 else if (location.Find(wxT('#')) != wxNOT_FOUND && location.BeforeFirst(wxT('#')) == m_OpenedPage) {
149 wxString anch = location.AfterFirst(wxT('#'));
150 m_tmpCanDrawLocks--;
151 rt_val = ScrollToAnchor(anch);
152 m_tmpCanDrawLocks++;
153 }
154 else if (location.Find(wxT('#')) != wxNOT_FOUND &&
155 (m_FS -> GetPath() + location.BeforeFirst(wxT('#'))) == m_OpenedPage) {
156 wxString anch = location.AfterFirst(wxT('#'));
157 m_tmpCanDrawLocks--;
158 rt_val = ScrollToAnchor(anch);
159 m_tmpCanDrawLocks++;
160 }
161
162 else {
163 needs_refresh = TRUE;
164 // load&display it:
165 if (m_RelatedStatusBar != -1) {
166 m_RelatedFrame -> SetStatusText(_("Connecting..."), m_RelatedStatusBar);
167 Refresh(FALSE);
168 }
169
170 f = m_FS -> OpenFile(location);
171
172 if (f == NULL) {
173 wxString err;
174
175 wxLogError(_("Unable to open requested HTML document: %s"), location.c_str());
176 m_tmpCanDrawLocks--;
177
178 SetCursor(*wxSTANDARD_CURSOR);
179 return FALSE;
180 }
181
182 else {
183 wxNode *node;
184 wxString src = wxEmptyString;
185
186 if (m_RelatedStatusBar != -1) {
187 wxString msg = _("Loading : ") + location;
188 m_RelatedFrame -> SetStatusText(msg, m_RelatedStatusBar);
189 Refresh(FALSE);
190 }
191
192 node = m_Filters.GetFirst();
193 while (node){
194 wxHtmlFilter *h = (wxHtmlFilter*) node -> GetData();
195 if (h -> CanRead(*f)) {
196 src = h -> ReadFile(*f);
197 break;
198 }
199 node = node -> GetNext();
200 }
201 if (src == wxEmptyString) {
202 if (m_DefaultFilter == NULL) m_DefaultFilter = GetDefaultFilter();
203 src = m_DefaultFilter -> ReadFile(*f);
204 }
205
206 m_FS -> ChangePathTo(f -> GetLocation());
207 rt_val = SetPage(src);
208 m_OpenedPage = f -> GetLocation();
209 if (f -> GetAnchor() != wxEmptyString) {
210 wxYield();
211 ScrollToAnchor(f -> GetAnchor());
212 }
213
214 delete f;
215
216 if (m_RelatedStatusBar != -1) m_RelatedFrame -> SetStatusText(_("Done"), m_RelatedStatusBar);
217 }
218 }
219
220 if (m_HistoryOn) { // add this page to history there:
221 int c = m_History.GetCount() - (m_HistoryPos + 1);
222
223 m_HistoryPos++;
224 for (int i = 0; i < c; i++)
225 m_History.Remove(m_HistoryPos);
226 m_History.Add(new HtmlHistoryItem(m_OpenedPage, m_OpenedAnchor));
227 }
228
229 if (m_OpenedPageTitle == wxEmptyString)
230 OnSetTitle(wxFileNameFromPath(m_OpenedPage));
231 SetCursor(*wxSTANDARD_CURSOR);
232
233 if (needs_refresh) {
234 wxYield();
235 m_tmpCanDrawLocks--;
236 Refresh();
237 }
238 else
239 m_tmpCanDrawLocks--;
240
241 return rt_val;
242 }
243
244
245
246 bool wxHtmlWindow::ScrollToAnchor(const wxString& anchor)
247 {
248 const wxHtmlCell *c = m_Cell -> Find(wxHTML_COND_ISANCHOR, &anchor);
249 if (!c)
250 {
251 wxLogWarning(_("HTML anchor %s does not exist."), anchor.c_str());
252 return FALSE;
253 }
254 else {
255 int y;
256
257 for (y = 0; c != NULL; c = c -> GetParent()) y += c -> GetPosY();
258 Scroll(-1, y / wxHTML_SCROLL_STEP);
259 m_OpenedAnchor = anchor;
260 return TRUE;
261 }
262 }
263
264
265 void wxHtmlWindow::OnSetTitle(const wxString& title)
266 {
267 if (m_RelatedFrame) {
268 wxString tit;
269 tit.Printf(m_TitleFormat, title.c_str());
270 m_RelatedFrame -> SetTitle(tit);
271 }
272 m_OpenedPageTitle = title;
273 }
274
275
276
277
278
279 void wxHtmlWindow::CreateLayout()
280 {
281 int ClientWidth, ClientHeight;
282
283 if (!m_Cell) return;
284
285 if (m_Style & wxHW_SCROLLBAR_NEVER) {
286 SetScrollbars(wxHTML_SCROLL_STEP, 1, m_Cell -> GetWidth() / wxHTML_SCROLL_STEP, 0); // always off
287 GetClientSize(&ClientWidth, &ClientHeight);
288 m_Cell -> Layout(ClientWidth);
289 }
290
291 else {
292 GetClientSize(&ClientWidth, &ClientHeight);
293 m_Cell -> Layout(ClientWidth);
294 if (ClientHeight < m_Cell -> GetHeight() + GetCharHeight()) {
295 SetScrollbars(
296 wxHTML_SCROLL_STEP, wxHTML_SCROLL_STEP,
297 m_Cell -> GetWidth() / wxHTML_SCROLL_STEP,
298 (m_Cell -> GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
299 /*cheat: top-level frag is always container*/);
300 }
301 else { /* we fit into window, no need for scrollbars */
302 SetScrollbars(wxHTML_SCROLL_STEP, 1, m_Cell -> GetWidth() / wxHTML_SCROLL_STEP, 0); // disable...
303 GetClientSize(&ClientWidth, &ClientHeight);
304 m_Cell -> Layout(ClientWidth); // ...and relayout
305 }
306 }
307 }
308
309
310
311 void wxHtmlWindow::ReadCustomization(wxConfigBase *cfg, wxString path)
312 {
313 wxString oldpath;
314 wxString tmp;
315 int p_fontsizes[7];
316 wxString p_fff, p_ffn;
317
318 if (path != wxEmptyString) {
319 oldpath = cfg -> GetPath();
320 cfg -> SetPath(path);
321 }
322
323 m_Borders = cfg -> Read("wxHtmlWindow/Borders", m_Borders);
324 p_fff = cfg -> Read("wxHtmlWindow/FontFaceFixed", m_Parser -> m_FontFaceFixed);
325 p_ffn = cfg -> Read("wxHtmlWindow/FontFaceNormal", m_Parser -> m_FontFaceNormal);
326 for (int i = 0; i < 7; i++) {
327 tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i);
328 p_fontsizes[i] = cfg -> Read(tmp, m_Parser -> m_FontsSizes[i]);
329 }
330 SetFonts(p_ffn, p_fff, p_fontsizes);
331
332 if (path != wxEmptyString)
333 cfg -> SetPath(oldpath);
334 }
335
336
337
338 void wxHtmlWindow::WriteCustomization(wxConfigBase *cfg, wxString path)
339 {
340 wxString oldpath;
341 wxString tmp;
342
343 if (path != wxEmptyString) {
344 oldpath = cfg -> GetPath();
345 cfg -> SetPath(path);
346 }
347
348 cfg -> Write("wxHtmlWindow/Borders", (long) m_Borders);
349 cfg -> Write("wxHtmlWindow/FontFaceFixed", m_Parser -> m_FontFaceFixed);
350 cfg -> Write("wxHtmlWindow/FontFaceNormal", m_Parser -> m_FontFaceNormal);
351 for (int i = 0; i < 7; i++) {
352 tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i);
353 cfg -> Write(tmp, (long) m_Parser -> m_FontsSizes[i]);
354 }
355
356 if (path != wxEmptyString)
357 cfg -> SetPath(oldpath);
358 }
359
360
361
362 bool wxHtmlWindow::HistoryBack()
363 {
364 wxString a, l;
365
366 if (m_HistoryPos < 1) return FALSE;
367
368 // store scroll position into history item:
369 int x, y;
370 ViewStart(&x, &y);
371 m_History[m_HistoryPos].SetPos(y);
372
373 // go to previous position:
374 m_HistoryPos--;
375
376 l = m_History[m_HistoryPos].GetPage();
377 a = m_History[m_HistoryPos].GetAnchor();
378 m_HistoryOn = FALSE;
379 m_tmpCanDrawLocks++;
380 if (a == wxEmptyString) LoadPage(l);
381 else LoadPage(l + wxT("#") + a);
382 m_HistoryOn = TRUE;
383 wxYield();
384 m_tmpCanDrawLocks--;
385 Scroll(0, m_History[m_HistoryPos].GetPos());
386 Refresh();
387 return TRUE;
388 }
389
390
391
392 bool wxHtmlWindow::HistoryForward()
393 {
394 wxString a, l;
395
396 if (m_HistoryPos == -1) return FALSE;
397 if (m_HistoryPos >= (int)m_History.GetCount() - 1)return FALSE;
398
399 m_OpenedPage = wxEmptyString; // this will disable adding new entry into history in LoadPage()
400
401 m_HistoryPos++;
402 l = m_History[m_HistoryPos].GetPage();
403 a = m_History[m_HistoryPos].GetAnchor();
404 m_HistoryOn = FALSE;
405 m_tmpCanDrawLocks++;
406 if (a == wxEmptyString) LoadPage(l);
407 else LoadPage(l + wxT("#") + a);
408 m_HistoryOn = TRUE;
409 wxYield();
410 m_tmpCanDrawLocks--;
411 Scroll(0, m_History[m_HistoryPos].GetPos());
412 Refresh();
413 return TRUE;
414 }
415
416
417
418 void wxHtmlWindow::HistoryClear()
419 {
420 m_History.Empty();
421 m_HistoryPos = -1;
422 }
423
424
425
426 wxList wxHtmlWindow::m_Filters;
427 wxHtmlFilter *wxHtmlWindow::m_DefaultFilter = NULL;
428 wxCursor *wxHtmlWindow::s_cur_hand = NULL;
429 wxCursor *wxHtmlWindow::s_cur_arrow = NULL;
430
431 void wxHtmlWindow::CleanUpStatics()
432 {
433 if (m_DefaultFilter) delete m_DefaultFilter;
434 m_DefaultFilter = NULL;
435 m_Filters.DeleteContents(TRUE);
436 m_Filters.Clear();
437 if (s_cur_hand) delete s_cur_hand;
438 if (s_cur_arrow) delete s_cur_arrow;
439 }
440
441
442
443 void wxHtmlWindow::AddFilter(wxHtmlFilter *filter)
444 {
445 m_Filters.Append(filter);
446 }
447
448
449
450
451 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo& link)
452 {
453 LoadPage(link.GetHref());
454 }
455
456
457
458 void wxHtmlWindow::OnDraw(wxDC& dc)
459 {
460 int x, y;
461 wxRegionIterator upd(GetUpdateRegion()); // get the update rect list
462 int v_y, v_h;
463
464 if (m_tmpCanDrawLocks > 0) return;
465
466 dc.SetMapMode(wxMM_TEXT);
467 #if 0
468 /* VS - I don't think this is neccessary any longer
469 MSC_VER 1200 means MSVC 6.0 and it works fine */
470 #if defined(_MSC_VER) && (_MSC_VER == 1200)
471 ::SetMapMode((HDC)dc.GetHDC(), MM_TEXT);
472 #endif
473 #endif
474 dc.SetBackgroundMode(wxTRANSPARENT);
475 ViewStart(&x, &y);
476
477 while (upd) {
478 v_y = upd.GetY();
479 v_h = upd.GetH();
480 if (m_Cell) m_Cell -> Draw(dc, 0, 0, y * wxHTML_SCROLL_STEP + v_y, y * wxHTML_SCROLL_STEP + v_h + v_y);
481 upd++;
482 }
483 }
484
485
486
487
488 void wxHtmlWindow::OnSize(wxSizeEvent& event)
489 {
490 wxScrolledWindow::OnSize(event);
491 CreateLayout();
492 Refresh();
493 }
494
495
496 void wxHtmlWindow::OnMouseEvent(wxMouseEvent& event)
497 {
498 m_tmpMouseMoved = TRUE;
499
500 if (event.ButtonDown()) {
501 int sx, sy;
502 wxPoint pos;
503 wxString lnk;
504
505 ViewStart(&sx, &sy); sx *= wxHTML_SCROLL_STEP; sy *= wxHTML_SCROLL_STEP;
506 pos = event.GetPosition();
507
508 if (m_Cell)
509 m_Cell -> OnMouseClick(this, sx + pos.x, sy + pos.y, event);
510 }
511 }
512
513
514
515 void wxHtmlWindow::OnIdle(wxIdleEvent& event)
516 {
517 if (s_cur_hand == NULL)
518 {
519 s_cur_hand = new wxCursor(wxCURSOR_HAND);
520 s_cur_arrow = new wxCursor(wxCURSOR_ARROW);
521 }
522
523 if (m_tmpMouseMoved && (m_Cell != NULL)) {
524 int sx, sy;
525 int x, y;
526 wxHtmlLinkInfo *lnk;
527
528 ViewStart(&sx, &sy); sx *= wxHTML_SCROLL_STEP; sy *= wxHTML_SCROLL_STEP;
529 wxGetMousePosition(&x, &y);
530 ScreenToClient(&x, &y);
531 lnk = m_Cell -> GetLink(sx + x, sy + y);
532
533 if (lnk != m_tmpLastLink) {
534 if (lnk == NULL) {
535 SetCursor(*s_cur_arrow);
536 if (m_RelatedStatusBar != -1) m_RelatedFrame -> SetStatusText(wxEmptyString, m_RelatedStatusBar);
537 }
538 else {
539 SetCursor(*s_cur_hand);
540 if (m_RelatedStatusBar != -1)
541 m_RelatedFrame -> SetStatusText(lnk -> GetHref(), m_RelatedStatusBar);
542 }
543 m_tmpLastLink = lnk;
544 }
545 m_tmpMouseMoved = FALSE;
546 }
547 }
548
549
550
551
552 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow,wxScrolledWindow)
553
554 BEGIN_EVENT_TABLE(wxHtmlWindow, wxScrolledWindow)
555 EVT_SIZE(wxHtmlWindow::OnSize)
556 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent)
557 EVT_MOTION(wxHtmlWindow::OnMouseEvent)
558 EVT_IDLE(wxHtmlWindow::OnIdle)
559 END_EVENT_TABLE()
560
561
562
563
564
565 // A module to allow initialization/cleanup
566 // without calling these functions from app.cpp or from
567 // the user's application.
568
569 class wxHtmlWinModule: public wxModule
570 {
571 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule)
572 public:
573 wxHtmlWinModule() : wxModule() {}
574 bool OnInit() { return TRUE; }
575 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
576 };
577
578 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule, wxModule)
579
580
581
582
583 ///// default mod handlers are forced there:
584
585 FORCE_LINK(m_layout)
586 FORCE_LINK(m_fonts)
587 FORCE_LINK(m_image)
588 FORCE_LINK(m_list)
589 FORCE_LINK(m_dflist)
590 FORCE_LINK(m_pre)
591 FORCE_LINK(m_hline)
592 FORCE_LINK(m_links)
593 FORCE_LINK(m_tables)
594 FORCE_LINK(m_meta)
595
596
597 #endif