]> git.saurik.com Git - wxWidgets.git/blame - src/html/htmlwin.cpp
1. added wxMemoryConfig (private) class
[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"
5526e819 30
583f6c5c
JS
31#ifdef __WXMSW__
32#include <windows.h>
33#endif
5526e819
VS
34
35//-----------------------------------------------------------------------------
36// wxHtmlWindow
37//-----------------------------------------------------------------------------
38
39
40
3096bd2f 41#include "wx/arrimpl.cpp"
5526e819
VS
42WX_DEFINE_OBJARRAY(HtmlHistoryArray)
43
44
269e8200 45wxHtmlWindow::wxHtmlWindow(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
f3bcfd9b 46 long style, const wxString& name) : wxScrolledWindow(parent, id, pos, size, wxVSCROLL | wxHSCROLL, name)
5526e819
VS
47{
48 m_tmpMouseMoved = FALSE;
846914d1 49 m_tmpLastLink = NULL;
89de9af3 50 m_tmpCanDrawLocks = 0;
5526e819
VS
51 m_FS = new wxFileSystem();
52 m_RelatedStatusBar = -1;
53 m_RelatedFrame = NULL;
54 m_TitleFormat = "%s";
d5db80c2 55 m_OpenedPage = m_OpenedAnchor = m_OpenedPageTitle = wxEmptyString;
5526e819
VS
56 m_Cell = NULL;
57 m_Parser = new wxHtmlWinParser(this);
58 m_Parser -> SetFS(m_FS);
59 SetBorders(10);
60 m_HistoryPos = -1;
61 m_HistoryOn = TRUE;
a547ebff 62 m_Style = style;
5526e819
VS
63 SetPage("<html><body></body></html>");
64}
65
66
67
68wxHtmlWindow::~wxHtmlWindow()
69{
70 HistoryClear();
71
72 if (m_Cell) delete m_Cell;
73
5526e819
VS
74 delete m_Parser;
75 delete m_FS;
76}
77
78
79
80void wxHtmlWindow::SetRelatedFrame(wxFrame* frame, const wxString& format)
81{
82 m_RelatedFrame = frame;
83 m_TitleFormat = format;
84}
85
86
87
88void wxHtmlWindow::SetRelatedStatusBar(int bar)
89{
90 m_RelatedStatusBar = bar;
91}
269e8200
RD
92
93
94
8eb2940f 95void wxHtmlWindow::SetFonts(wxString normal_face, wxString fixed_face, const int *sizes)
5526e819 96{
d5db80c2
VS
97 wxString op = m_OpenedPage;
98
8eb2940f 99 m_Parser -> SetFonts(normal_face, fixed_face, sizes);
fb5700fe 100 SetPage(wxT("<html><body></body></html>")); // fonts changed => contents invalid
d5db80c2 101 if (!op.IsEmpty()) LoadPage(op);
5526e819
VS
102}
103
104
105
106bool wxHtmlWindow::SetPage(const wxString& source)
107{
108 wxClientDC *dc = new wxClientDC(this);
109
110 dc -> SetMapMode(wxMM_TEXT);
111 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
d5db80c2 112 m_OpenedPage = m_OpenedAnchor = m_OpenedPageTitle = wxEmptyString;
5526e819 113 m_Parser -> SetDC(dc);
f61815af 114 if (m_Cell) {
89de9af3
VS
115 delete m_Cell;
116 m_Cell = NULL;
f61815af 117 }
5526e819
VS
118 m_Cell = (wxHtmlContainerCell*) m_Parser -> Parse(source);
119 delete dc;
efba2b89
VS
120 m_Cell -> SetIndent(m_Borders, wxHTML_INDENT_ALL, wxHTML_UNITS_PIXELS);
121 m_Cell -> SetAlignHor(wxHTML_ALIGN_CENTER);
5526e819 122 CreateLayout();
89de9af3 123 if (m_tmpCanDrawLocks == 0) Refresh();
5526e819
VS
124 return TRUE;
125}
126
127
128bool wxHtmlWindow::LoadPage(const wxString& location)
129{
130 wxFSFile *f;
131 bool rt_val;
169ee06c
VS
132
133 SetCursor(*wxHOURGLASS_CURSOR);
134 wxYield();
5526e819 135
89de9af3 136 m_tmpCanDrawLocks++;
5526e819
VS
137 if (m_HistoryOn && (m_HistoryPos != -1)) { // store scroll position into history item
138 int x, y;
139 ViewStart(&x, &y);
140 m_History[m_HistoryPos].SetPos(y);
141 }
142
143 if (location[0] == '#') { // local anchor
144 wxString anch = location.Mid(1) /*1 to end*/;
89de9af3 145 m_tmpCanDrawLocks--;
5526e819
VS
146 rt_val = ScrollToAnchor(anch);
147 }
148
149 else {
150 // load&display it:
151 if (m_RelatedStatusBar != -1) {
152 m_RelatedFrame -> SetStatusText(_("Connecting..."), m_RelatedStatusBar);
153 Refresh();
154 }
155
156 f = m_FS -> OpenFile(location);
157 if (f == NULL) {
158 wxString err;
159
f3bcfd9b 160 err.Printf(_("Unable to open requested location :\n\n%s"), WXSTRINGCAST location);
89de9af3 161 m_tmpCanDrawLocks--;
483ff5a5
VS
162 Refresh();
163 wxMessageBox(err, "Error");
169ee06c
VS
164
165 SetCursor(*wxSTANDARD_CURSOR);
5526e819
VS
166 return FALSE;
167 }
168
169 else {
170 wxNode *node;
171 wxString src = wxEmptyString;
172
173 if (m_RelatedStatusBar != -1) {
174 wxString msg = _("Loading : ") + location;
175 m_RelatedFrame -> SetStatusText(msg, m_RelatedStatusBar);
176 Refresh();
177 }
178
179 node = m_Filters.GetFirst();
180 while (node){
181 wxHtmlFilter *h = (wxHtmlFilter*) node -> GetData();
182 if (h -> CanRead(*f)) {
183 src = h -> ReadFile(*f);
184 break;
185 }
186 node = node -> GetNext();
187 }
a76015e6 188 if (src == wxEmptyString) {
89de9af3
VS
189 if (m_DefaultFilter == NULL) m_DefaultFilter = GetDefaultFilter();
190 src = m_DefaultFilter -> ReadFile(*f);
191 }
5526e819
VS
192
193 m_FS -> ChangePathTo(f -> GetLocation());
194 rt_val = SetPage(src);
195 m_OpenedPage = f -> GetLocation();
196 if (f -> GetAnchor() != wxEmptyString) {
89de9af3 197// m_tmpCanDrawLocks--;
5526e819 198 ScrollToAnchor(f -> GetAnchor());
89de9af3 199// m_tmpCanDrawLocks++;
5526e819
VS
200 }
201
202 delete f;
203
204 if (m_RelatedStatusBar != -1) m_RelatedFrame -> SetStatusText(_("Done"), m_RelatedStatusBar);
205 }
206 }
207
208 if (m_HistoryOn) { // add this page to history there:
209 int c = m_History.GetCount() - (m_HistoryPos + 1);
210
211 m_HistoryPos++;
212 for (int i = 0; i < c; i++)
213 m_History.Remove(m_HistoryPos);
214 m_History.Add(new HtmlHistoryItem(m_OpenedPage, m_OpenedAnchor));
215 }
216
169ee06c
VS
217 SetCursor(*wxSTANDARD_CURSOR);
218
89de9af3
VS
219 wxYield();
220 m_tmpCanDrawLocks--;
5526e819
VS
221 Refresh();
222 return rt_val;
223}
224
225
226
227bool wxHtmlWindow::ScrollToAnchor(const wxString& anchor)
228{
efba2b89 229 const wxHtmlCell *c = m_Cell -> Find(wxHTML_COND_ISANCHOR, &anchor);
5526e819
VS
230 if (!c) return FALSE;
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
VS
438 dc.SetMapMode(wxMM_TEXT);
439#if defined(_MSC_VER) && (_MSC_VER == 1200)
440 ::SetMapMode((HDC)dc.GetHDC(), MM_TEXT);
441#endif
442 dc.SetBackgroundMode(wxTRANSPARENT);
443 ViewStart(&x, &y);
444
445 while (upd) {
446 v_y = upd.GetY();
447 v_h = upd.GetH();
efba2b89 448 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
449 upd++;
450 }
451}
452
453
454
455
456void wxHtmlWindow::OnSize(wxSizeEvent& event)
457{
458 wxScrolledWindow::OnSize(event);
459 CreateLayout();
460}
461
462
463
464void wxHtmlWindow::OnKeyDown(wxKeyEvent& event)
465{
466 int dummy;
467 int sty, szy, cliy;
468
469 ViewStart(&dummy, &sty);
efba2b89
VS
470 GetClientSize(&dummy, &cliy); cliy /= wxHTML_SCROLL_STEP;
471 GetVirtualSize(&dummy, &szy); szy /= wxHTML_SCROLL_STEP;
5526e819
VS
472
473 switch (event.KeyCode()) {
474 case WXK_PAGEUP :
6aef9caf
VS
475 case WXK_PRIOR :
476 Scroll(-1, sty - (2 * cliy / 3));
5526e819
VS
477 break;
478 case WXK_PAGEDOWN :
6aef9caf
VS
479 case WXK_NEXT :
480 Scroll(-1, sty + (2 * cliy / 3));
5526e819
VS
481 break;
482 case WXK_HOME :
483 Scroll(-1, 0);
484 break;
485 case WXK_END :
486 Scroll(-1, szy - cliy);
487 break;
488 case WXK_UP :
489 Scroll(-1, sty - 1);
490 break;
491 case WXK_DOWN :
492 Scroll(-1, sty + 1);
493 break;
494 }
495}
496
497
498
499void wxHtmlWindow::OnMouseEvent(wxMouseEvent& event)
500{
501 m_tmpMouseMoved = TRUE;
502
503 if (event.ButtonDown()) {
504 int sx, sy;
505 wxPoint pos;
506 wxString lnk;
507
efba2b89 508 ViewStart(&sx, &sy); sx *= wxHTML_SCROLL_STEP; sy *= wxHTML_SCROLL_STEP;
5526e819
VS
509 pos = event.GetPosition();
510
511 if (m_Cell)
0b2dadd3 512 m_Cell -> OnMouseClick(this, sx + pos.x, sy + pos.y, event);
5526e819
VS
513 }
514}
515
516
517
518void wxHtmlWindow::OnIdle(wxIdleEvent& event)
519{
520 static wxCursor cur_hand(wxCURSOR_HAND), cur_arrow(wxCURSOR_ARROW);
521
522 if (m_tmpMouseMoved && (m_Cell != NULL)) {
523 int sx, sy;
524 int x, y;
846914d1 525 wxHtmlLinkInfo *lnk;
5526e819 526
efba2b89 527 ViewStart(&sx, &sy); sx *= wxHTML_SCROLL_STEP; sy *= wxHTML_SCROLL_STEP;
5526e819
VS
528 wxGetMousePosition(&x, &y);
529 ScreenToClient(&x, &y);
530 lnk = m_Cell -> GetLink(sx + x, sy + y);
531
622ea783 532 if (lnk != m_tmpLastLink) {
846914d1 533 if (lnk == NULL) {
622ea783
VS
534 SetCursor(cur_arrow);
535 if (m_RelatedStatusBar != -1) m_RelatedFrame -> SetStatusText(wxEmptyString, m_RelatedStatusBar);
536 }
537 else {
538 SetCursor(cur_hand);
846914d1
VS
539 if (m_RelatedStatusBar != -1)
540 m_RelatedFrame -> SetStatusText(lnk -> GetHref(), m_RelatedStatusBar);
622ea783
VS
541 }
542 m_tmpLastLink = lnk;
5526e819
VS
543 }
544 m_tmpMouseMoved = FALSE;
545 }
546}
547
548
549
550
551IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow,wxScrolledWindow)
552
553BEGIN_EVENT_TABLE(wxHtmlWindow, wxScrolledWindow)
554 EVT_SIZE(wxHtmlWindow::OnSize)
555 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent)
556 EVT_MOTION(wxHtmlWindow::OnMouseEvent)
557 EVT_IDLE(wxHtmlWindow::OnIdle)
558 EVT_KEY_DOWN(wxHtmlWindow::OnKeyDown)
559END_EVENT_TABLE()
560
561
562
563
564
a76015e6
VS
565// A module to allow initialization/cleanup
566// without calling these functions from app.cpp or from
567// the user's application.
568
569class wxHtmlWinModule: public wxModule
570{
571DECLARE_DYNAMIC_CLASS(wxHtmlWinModule)
572public:
573 wxHtmlWinModule() : wxModule() {}
574 bool OnInit() { return TRUE; }
575 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
576};
577
578IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule, wxModule)
579
5526e819
VS
580
581
582
583///// default mod handlers are forced there:
584
c88293a4
VS
585FORCE_LINK(m_layout)
586FORCE_LINK(m_fonts)
587FORCE_LINK(m_image)
588FORCE_LINK(m_list)
589FORCE_LINK(m_dflist)
590FORCE_LINK(m_pre)
591FORCE_LINK(m_hline)
592FORCE_LINK(m_links)
593FORCE_LINK(m_tables)
fa146dd7 594FORCE_LINK(m_meta)
5526e819
VS
595
596
483ff5a5 597#endif