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