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