]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmlwin.cpp
Destructor for wxMimeTypesManager was deleting its member m_impl without checking...
[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(wxT("<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 {
114 delete m_Cell;
115 m_Cell = NULL;
116 }
117 m_Cell = (wxHtmlContainerCell*) m_Parser->Parse(source);
118 delete dc;
119 m_Cell->SetIndent(m_Borders, wxHTML_INDENT_ALL, wxHTML_UNITS_PIXELS);
120 m_Cell->SetAlignHor(wxHTML_ALIGN_CENTER);
121 CreateLayout();
122 if (m_tmpCanDrawLocks == 0) Refresh();
123 return TRUE;
124 }
125
126
127 bool wxHtmlWindow::LoadPage(const wxString& location)
128 {
129 wxFSFile *f;
130 bool rt_val;
131 bool needs_refresh = FALSE;
132
133 SetCursor(*wxHOURGLASS_CURSOR);
134 wxYield(); Refresh(FALSE);
135
136 m_tmpCanDrawLocks++;
137 if (m_HistoryOn && (m_HistoryPos != -1)) // store scroll position into history item
138 {
139 int x, y;
140 ViewStart(&x, &y);
141 m_History[m_HistoryPos].SetPos(y);
142 }
143
144 if (location[0] == wxT('#')) // local anchor
145 {
146 wxString anch = location.Mid(1) /*1 to end*/;
147 m_tmpCanDrawLocks--;
148 rt_val = ScrollToAnchor(anch);
149 m_tmpCanDrawLocks++;
150 }
151 else if (location.Find(wxT('#')) != wxNOT_FOUND && location.BeforeFirst(wxT('#')) == m_OpenedPage)
152 {
153 wxString anch = location.AfterFirst(wxT('#'));
154 m_tmpCanDrawLocks--;
155 rt_val = ScrollToAnchor(anch);
156 m_tmpCanDrawLocks++;
157 }
158 else if (location.Find(wxT('#')) != wxNOT_FOUND &&
159 (m_FS->GetPath() + location.BeforeFirst(wxT('#'))) == m_OpenedPage)
160 {
161 wxString anch = location.AfterFirst(wxT('#'));
162 m_tmpCanDrawLocks--;
163 rt_val = ScrollToAnchor(anch);
164 m_tmpCanDrawLocks++;
165 }
166
167 else
168 {
169 needs_refresh = TRUE;
170 // load&display it:
171 if (m_RelatedStatusBar != -1)
172 {
173 m_RelatedFrame->SetStatusText(_("Connecting..."), m_RelatedStatusBar);
174 Refresh(FALSE);
175 }
176
177 f = m_FS->OpenFile(location);
178
179 if (f == NULL)
180 {
181 wxString err;
182
183 wxLogError(_("Unable to open requested HTML document: %s"), location.c_str());
184 m_tmpCanDrawLocks--;
185
186 SetCursor(*wxSTANDARD_CURSOR);
187 return FALSE;
188 }
189
190 else
191 {
192 wxNode *node;
193 wxString src = wxEmptyString;
194
195 if (m_RelatedStatusBar != -1)
196 {
197 wxString msg = _("Loading : ") + location;
198 m_RelatedFrame->SetStatusText(msg, m_RelatedStatusBar);
199 Refresh(FALSE);
200 }
201
202 node = m_Filters.GetFirst();
203 while (node)
204 {
205 wxHtmlFilter *h = (wxHtmlFilter*) node->GetData();
206 if (h->CanRead(*f))
207 {
208 src = h->ReadFile(*f);
209 break;
210 }
211 node = node->GetNext();
212 }
213 if (src == wxEmptyString)
214 {
215 if (m_DefaultFilter == NULL) m_DefaultFilter = GetDefaultFilter();
216 src = m_DefaultFilter->ReadFile(*f);
217 }
218
219 m_FS->ChangePathTo(f->GetLocation());
220 rt_val = SetPage(src);
221 m_OpenedPage = f->GetLocation();
222 if (f->GetAnchor() != wxEmptyString)
223 {
224 wxYield();
225 ScrollToAnchor(f->GetAnchor());
226 }
227
228 delete f;
229
230 if (m_RelatedStatusBar != -1) m_RelatedFrame->SetStatusText(_("Done"), m_RelatedStatusBar);
231 }
232 }
233
234 if (m_HistoryOn) // add this page to history there:
235 {
236 int c = m_History.GetCount() - (m_HistoryPos + 1);
237
238 m_HistoryPos++;
239 for (int i = 0; i < c; i++)
240 m_History.Remove(m_HistoryPos);
241 m_History.Add(new HtmlHistoryItem(m_OpenedPage, m_OpenedAnchor));
242 }
243
244 if (m_OpenedPageTitle == wxEmptyString)
245 OnSetTitle(wxFileNameFromPath(m_OpenedPage));
246 SetCursor(*wxSTANDARD_CURSOR);
247
248 if (needs_refresh)
249 {
250 wxYield();
251 m_tmpCanDrawLocks--;
252 Refresh();
253 }
254 else
255 m_tmpCanDrawLocks--;
256
257 return rt_val;
258 }
259
260
261
262 bool wxHtmlWindow::ScrollToAnchor(const wxString& anchor)
263 {
264 const wxHtmlCell *c = m_Cell->Find(wxHTML_COND_ISANCHOR, &anchor);
265 if (!c)
266 {
267 wxLogWarning(_("HTML anchor %s does not exist."), anchor.c_str());
268 return FALSE;
269 }
270 else
271 {
272 int y;
273
274 for (y = 0; c != NULL; c = c->GetParent()) y += c->GetPosY();
275 Scroll(-1, y / wxHTML_SCROLL_STEP);
276 m_OpenedAnchor = anchor;
277 return TRUE;
278 }
279 }
280
281
282 void wxHtmlWindow::OnSetTitle(const wxString& title)
283 {
284 if (m_RelatedFrame)
285 {
286 wxString tit;
287 tit.Printf(m_TitleFormat, title.c_str());
288 m_RelatedFrame->SetTitle(tit);
289 }
290 m_OpenedPageTitle = title;
291 }
292
293
294
295
296
297 void wxHtmlWindow::CreateLayout()
298 {
299 int ClientWidth, ClientHeight;
300
301 if (!m_Cell) return;
302
303 if (m_Style & wxHW_SCROLLBAR_NEVER)
304 {
305 SetScrollbars(wxHTML_SCROLL_STEP, 1, m_Cell->GetWidth() / wxHTML_SCROLL_STEP, 0); // always off
306 GetClientSize(&ClientWidth, &ClientHeight);
307 m_Cell->Layout(ClientWidth);
308 }
309
310 else {
311 GetClientSize(&ClientWidth, &ClientHeight);
312 m_Cell->Layout(ClientWidth);
313 if (ClientHeight < m_Cell->GetHeight() + GetCharHeight())
314 {
315 SetScrollbars(
316 wxHTML_SCROLL_STEP, wxHTML_SCROLL_STEP,
317 m_Cell->GetWidth() / wxHTML_SCROLL_STEP,
318 (m_Cell->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
319 /*cheat: top-level frag is always container*/);
320 }
321 else /* we fit into window, no need for scrollbars */
322 {
323 SetScrollbars(wxHTML_SCROLL_STEP, 1, m_Cell->GetWidth() / wxHTML_SCROLL_STEP, 0); // disable...
324 GetClientSize(&ClientWidth, &ClientHeight);
325 m_Cell->Layout(ClientWidth); // ...and relayout
326 }
327 }
328 }
329
330
331
332 void wxHtmlWindow::ReadCustomization(wxConfigBase *cfg, wxString path)
333 {
334 wxString oldpath;
335 wxString tmp;
336 int p_fontsizes[7];
337 wxString p_fff, p_ffn;
338
339 if (path != wxEmptyString)
340 {
341 oldpath = cfg->GetPath();
342 cfg->SetPath(path);
343 }
344
345 m_Borders = cfg->Read("wxHtmlWindow/Borders", m_Borders);
346 p_fff = cfg->Read("wxHtmlWindow/FontFaceFixed", m_Parser->m_FontFaceFixed);
347 p_ffn = cfg->Read("wxHtmlWindow/FontFaceNormal", m_Parser->m_FontFaceNormal);
348 for (int i = 0; i < 7; i++)
349 {
350 tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i);
351 p_fontsizes[i] = cfg->Read(tmp, m_Parser->m_FontsSizes[i]);
352 }
353 SetFonts(p_ffn, p_fff, p_fontsizes);
354
355 if (path != wxEmptyString)
356 cfg->SetPath(oldpath);
357 }
358
359
360
361 void wxHtmlWindow::WriteCustomization(wxConfigBase *cfg, wxString path)
362 {
363 wxString oldpath;
364 wxString tmp;
365
366 if (path != wxEmptyString)
367 {
368 oldpath = cfg->GetPath();
369 cfg->SetPath(path);
370 }
371
372 cfg->Write("wxHtmlWindow/Borders", (long) m_Borders);
373 cfg->Write("wxHtmlWindow/FontFaceFixed", m_Parser->m_FontFaceFixed);
374 cfg->Write("wxHtmlWindow/FontFaceNormal", m_Parser->m_FontFaceNormal);
375 for (int i = 0; i < 7; i++)
376 {
377 tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i);
378 cfg->Write(tmp, (long) m_Parser->m_FontsSizes[i]);
379 }
380
381 if (path != wxEmptyString)
382 cfg->SetPath(oldpath);
383 }
384
385
386
387 bool wxHtmlWindow::HistoryBack()
388 {
389 wxString a, l;
390
391 if (m_HistoryPos < 1) return FALSE;
392
393 // store scroll position into history item:
394 int x, y;
395 ViewStart(&x, &y);
396 m_History[m_HistoryPos].SetPos(y);
397
398 // go to previous position:
399 m_HistoryPos--;
400
401 l = m_History[m_HistoryPos].GetPage();
402 a = m_History[m_HistoryPos].GetAnchor();
403 m_HistoryOn = FALSE;
404 m_tmpCanDrawLocks++;
405 if (a == wxEmptyString) LoadPage(l);
406 else LoadPage(l + wxT("#") + a);
407 m_HistoryOn = TRUE;
408 wxYield();
409 m_tmpCanDrawLocks--;
410 Scroll(0, m_History[m_HistoryPos].GetPos());
411 Refresh();
412 return TRUE;
413 }
414
415 bool wxHtmlWindow::HistoryCanBack()
416 {
417 if (m_HistoryPos < 1) return FALSE;
418 return TRUE ;
419 }
420
421
422 bool wxHtmlWindow::HistoryForward()
423 {
424 wxString a, l;
425
426 if (m_HistoryPos == -1) return FALSE;
427 if (m_HistoryPos >= (int)m_History.GetCount() - 1)return FALSE;
428
429 m_OpenedPage = wxEmptyString; // this will disable adding new entry into history in LoadPage()
430
431 m_HistoryPos++;
432 l = m_History[m_HistoryPos].GetPage();
433 a = m_History[m_HistoryPos].GetAnchor();
434 m_HistoryOn = FALSE;
435 m_tmpCanDrawLocks++;
436 if (a == wxEmptyString) LoadPage(l);
437 else LoadPage(l + wxT("#") + a);
438 m_HistoryOn = TRUE;
439 wxYield();
440 m_tmpCanDrawLocks--;
441 Scroll(0, m_History[m_HistoryPos].GetPos());
442 Refresh();
443 return TRUE;
444 }
445
446 bool wxHtmlWindow::HistoryCanForward()
447 {
448 if (m_HistoryPos == -1) return FALSE;
449 if (m_HistoryPos >= (int)m_History.GetCount() - 1)return FALSE;
450 return TRUE ;
451 }
452
453
454 void wxHtmlWindow::HistoryClear()
455 {
456 m_History.Empty();
457 m_HistoryPos = -1;
458 }
459
460
461
462 wxList wxHtmlWindow::m_Filters;
463 wxHtmlFilter *wxHtmlWindow::m_DefaultFilter = NULL;
464 wxCursor *wxHtmlWindow::s_cur_hand = NULL;
465 wxCursor *wxHtmlWindow::s_cur_arrow = NULL;
466
467 void wxHtmlWindow::CleanUpStatics()
468 {
469 if (m_DefaultFilter) delete m_DefaultFilter;
470 m_DefaultFilter = NULL;
471 m_Filters.DeleteContents(TRUE);
472 m_Filters.Clear();
473 if (s_cur_hand) delete s_cur_hand;
474 if (s_cur_arrow) delete s_cur_arrow;
475 }
476
477
478
479 void wxHtmlWindow::AddFilter(wxHtmlFilter *filter)
480 {
481 m_Filters.Append(filter);
482 }
483
484
485
486
487 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo& link)
488 {
489 LoadPage(link.GetHref());
490 }
491
492
493
494 void wxHtmlWindow::OnDraw(wxDC& dc)
495 {
496 int x, y;
497 wxRegionIterator upd(GetUpdateRegion()); // get the update rect list
498 int v_y, v_h;
499
500 if (m_tmpCanDrawLocks > 0) return;
501
502 dc.SetMapMode(wxMM_TEXT);
503 #if 0
504 /* VS - I don't think this is neccessary any longer
505 MSC_VER 1200 means MSVC 6.0 and it works fine */
506 #if defined(_MSC_VER) && (_MSC_VER == 1200)
507 ::SetMapMode((HDC)dc.GetHDC(), MM_TEXT);
508 #endif
509 #endif
510 dc.SetBackgroundMode(wxTRANSPARENT);
511 ViewStart(&x, &y);
512
513 while (upd)
514 {
515 v_y = upd.GetY();
516 v_h = upd.GetH();
517 if (m_Cell) m_Cell->Draw(dc, 0, 0, y * wxHTML_SCROLL_STEP + v_y, y * wxHTML_SCROLL_STEP + v_h + v_y);
518 upd++;
519 }
520 }
521
522
523
524
525 void wxHtmlWindow::OnSize(wxSizeEvent& event)
526 {
527 wxScrolledWindow::OnSize(event);
528 CreateLayout();
529 Refresh();
530 }
531
532
533 void wxHtmlWindow::OnMouseEvent(wxMouseEvent& event)
534 {
535 m_tmpMouseMoved = TRUE;
536
537 if (event.ButtonDown())
538 {
539 int sx, sy;
540 wxPoint pos;
541 wxString lnk;
542
543 ViewStart(&sx, &sy); sx *= wxHTML_SCROLL_STEP; sy *= wxHTML_SCROLL_STEP;
544 pos = event.GetPosition();
545
546 if (m_Cell)
547 m_Cell->OnMouseClick(this, sx + pos.x, sy + pos.y, event);
548 }
549 }
550
551
552
553 void wxHtmlWindow::OnIdle(wxIdleEvent& event)
554 {
555 if (s_cur_hand == NULL)
556 {
557 s_cur_hand = new wxCursor(wxCURSOR_HAND);
558 s_cur_arrow = new wxCursor(wxCURSOR_ARROW);
559 }
560
561 if (m_tmpMouseMoved && (m_Cell != NULL))
562 {
563 int sx, sy;
564 int x, y;
565 wxHtmlLinkInfo *lnk;
566
567 ViewStart(&sx, &sy); sx *= wxHTML_SCROLL_STEP; sy *= wxHTML_SCROLL_STEP;
568 wxGetMousePosition(&x, &y);
569 ScreenToClient(&x, &y);
570 lnk = m_Cell->GetLink(sx + x, sy + y);
571
572 if (lnk != m_tmpLastLink)
573 {
574 if (lnk == NULL)
575 {
576 SetCursor(*s_cur_arrow);
577 if (m_RelatedStatusBar != -1) m_RelatedFrame->SetStatusText(wxEmptyString, m_RelatedStatusBar);
578 }
579 else
580 {
581 SetCursor(*s_cur_hand);
582 if (m_RelatedStatusBar != -1)
583 m_RelatedFrame->SetStatusText(lnk->GetHref(), m_RelatedStatusBar);
584 }
585 m_tmpLastLink = lnk;
586 }
587 m_tmpMouseMoved = FALSE;
588 }
589 }
590
591
592
593
594 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow,wxScrolledWindow)
595
596 BEGIN_EVENT_TABLE(wxHtmlWindow, wxScrolledWindow)
597 EVT_SIZE(wxHtmlWindow::OnSize)
598 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent)
599 EVT_MOTION(wxHtmlWindow::OnMouseEvent)
600 EVT_IDLE(wxHtmlWindow::OnIdle)
601 END_EVENT_TABLE()
602
603
604
605
606
607 // A module to allow initialization/cleanup
608 // without calling these functions from app.cpp or from
609 // the user's application.
610
611 class wxHtmlWinModule: public wxModule
612 {
613 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule)
614 public:
615 wxHtmlWinModule() : wxModule() {}
616 bool OnInit() { return TRUE; }
617 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
618 };
619
620 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule, wxModule)
621
622
623
624
625 ///// default mod handlers are forced there:
626
627 FORCE_LINK(m_layout)
628 FORCE_LINK(m_fonts)
629 FORCE_LINK(m_image)
630 FORCE_LINK(m_list)
631 FORCE_LINK(m_dflist)
632 FORCE_LINK(m_pre)
633 FORCE_LINK(m_hline)
634 FORCE_LINK(m_links)
635 FORCE_LINK(m_tables)
636 FORCE_LINK(m_meta)
637
638
639 #endif