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