]> git.saurik.com Git - wxWidgets.git/blame - src/html/htmlwin.cpp
digital mars updated
[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__
892aeafc
VS
12#pragma implementation "htmlwin.h"
13#pragma implementation "htmlproc.h"
5526e819
VS
14#endif
15
3096bd2f 16#include "wx/wxprec.h"
5526e819
VS
17
18#include "wx/defs.h"
f6bcfd97 19#if wxUSE_HTML && wxUSE_STREAMS
5526e819 20
2b5f62a0 21#ifdef __BORLANDC__
5526e819
VS
22#pragma hdrstop
23#endif
24
25#ifndef WXPRECOMP
04dbb646
VZ
26 #include "wx/log.h"
27 #include "wx/intl.h"
28 #include "wx/dcclient.h"
29 #include "wx/frame.h"
5526e819
VS
30#endif
31
69941f05 32#include "wx/html/htmlwin.h"
892aeafc 33#include "wx/html/htmlproc.h"
892aeafc 34#include "wx/list.h"
04dbb646
VZ
35
36#include "wx/arrimpl.cpp"
892aeafc
VS
37#include "wx/listimpl.cpp"
38
39//-----------------------------------------------------------------------------
40// wxHtmlHistoryItem
41//-----------------------------------------------------------------------------
42
43// item of history list
05f9197a 44class WXDLLEXPORT wxHtmlHistoryItem
892aeafc
VS
45{
46public:
47 wxHtmlHistoryItem(const wxString& p, const wxString& a) {m_Page = p, m_Anchor = a, m_Pos = 0;}
48 int GetPos() const {return m_Pos;}
49 void SetPos(int p) {m_Pos = p;}
50 const wxString& GetPage() const {return m_Page;}
51 const wxString& GetAnchor() const {return m_Anchor;}
52
53private:
54 wxString m_Page;
55 wxString m_Anchor;
56 int m_Pos;
57};
5526e819 58
5526e819
VS
59
60//-----------------------------------------------------------------------------
892aeafc 61// our private arrays:
5526e819
VS
62//-----------------------------------------------------------------------------
63
892aeafc
VS
64WX_DECLARE_OBJARRAY(wxHtmlHistoryItem, wxHtmlHistoryArray);
65WX_DEFINE_OBJARRAY(wxHtmlHistoryArray);
5526e819 66
892aeafc
VS
67WX_DECLARE_LIST(wxHtmlProcessor, wxHtmlProcessorList);
68WX_DEFINE_LIST(wxHtmlProcessorList);
5526e819 69
892aeafc
VS
70//-----------------------------------------------------------------------------
71// wxHtmlWindow
72//-----------------------------------------------------------------------------
5526e819
VS
73
74
4f417130 75void wxHtmlWindow::Init()
5526e819
VS
76{
77 m_tmpMouseMoved = FALSE;
846914d1 78 m_tmpLastLink = NULL;
f6010d8f 79 m_tmpLastCell = NULL;
89de9af3 80 m_tmpCanDrawLocks = 0;
5526e819
VS
81 m_FS = new wxFileSystem();
82 m_RelatedStatusBar = -1;
83 m_RelatedFrame = NULL;
892aeafc 84 m_TitleFormat = wxT("%s");
d5db80c2 85 m_OpenedPage = m_OpenedAnchor = m_OpenedPageTitle = wxEmptyString;
5526e819
VS
86 m_Cell = NULL;
87 m_Parser = new wxHtmlWinParser(this);
4f9297b0 88 m_Parser->SetFS(m_FS);
5526e819
VS
89 m_HistoryPos = -1;
90 m_HistoryOn = TRUE;
892aeafc
VS
91 m_History = new wxHtmlHistoryArray;
92 m_Processors = NULL;
4f417130
VS
93 m_Style = 0;
94 SetBorders(10);
95}
96
790dbce3 97bool wxHtmlWindow::Create(wxWindow *parent, wxWindowID id,
4f417130 98 const wxPoint& pos, const wxSize& size,
790dbce3 99 long style, const wxString& name)
4f417130 100{
790dbce3 101 if (!wxScrolledWindow::Create(parent, id, pos, size,
4f417130
VS
102 style | wxVSCROLL | wxHSCROLL, name))
103 return FALSE;
104
a547ebff 105 m_Style = style;
4f9297b0 106 SetPage(wxT("<html><body></body></html>"));
4f417130 107 return TRUE;
5526e819
VS
108}
109
110
5526e819
VS
111wxHtmlWindow::~wxHtmlWindow()
112{
113 HistoryClear();
114
115 if (m_Cell) delete m_Cell;
116
5526e819
VS
117 delete m_Parser;
118 delete m_FS;
892aeafc
VS
119 delete m_History;
120 delete m_Processors;
5526e819
VS
121}
122
123
124
125void wxHtmlWindow::SetRelatedFrame(wxFrame* frame, const wxString& format)
126{
127 m_RelatedFrame = frame;
128 m_TitleFormat = format;
129}
130
131
132
133void wxHtmlWindow::SetRelatedStatusBar(int bar)
134{
135 m_RelatedStatusBar = bar;
136}
269e8200
RD
137
138
139
8eb2940f 140void wxHtmlWindow::SetFonts(wxString normal_face, wxString fixed_face, const int *sizes)
5526e819 141{
d5db80c2
VS
142 wxString op = m_OpenedPage;
143
4f9297b0 144 m_Parser->SetFonts(normal_face, fixed_face, sizes);
892aeafc 145 // fonts changed => contents invalid, so reload the page:
bfb9ee96 146 SetPage(wxT("<html><body></body></html>"));
d5db80c2 147 if (!op.IsEmpty()) LoadPage(op);
5526e819
VS
148}
149
150
151
152bool wxHtmlWindow::SetPage(const wxString& source)
153{
892aeafc
VS
154 wxString newsrc(source);
155
156 // pass HTML through registered processors:
960ba969 157 if (m_Processors || m_GlobalProcessors)
892aeafc 158 {
960ba969
VS
159 wxHtmlProcessorList::Node *nodeL, *nodeG;
160 int prL, prG;
892aeafc
VS
161
162 nodeL = (m_Processors) ? m_Processors->GetFirst() : NULL;
960ba969
VS
163 nodeG = (m_GlobalProcessors) ? m_GlobalProcessors->GetFirst() : NULL;
164
165 // VS: there are two lists, global and local, both of them sorted by
e421922f 166 // priority. Since we have to go through _both_ lists with
960ba969
VS
167 // decreasing priority, we "merge-sort" the lists on-line by
168 // processing that one of the two heads that has higher priority
169 // in every iteration
170 while (nodeL || nodeG)
892aeafc
VS
171 {
172 prL = (nodeL) ? nodeL->GetData()->GetPriority() : -1;
960ba969
VS
173 prG = (nodeG) ? nodeG->GetData()->GetPriority() : -1;
174 if (prL > prG)
892aeafc 175 {
73348d09
VS
176 if (nodeL->GetData()->IsEnabled())
177 newsrc = nodeL->GetData()->Process(newsrc);
892aeafc
VS
178 nodeL = nodeL->GetNext();
179 }
960ba969 180 else // prL <= prG
892aeafc 181 {
73348d09
VS
182 if (nodeG->GetData()->IsEnabled())
183 newsrc = nodeG->GetData()->Process(newsrc);
960ba969 184 nodeG = nodeG->GetNext();
892aeafc
VS
185 }
186 }
187 }
5526e819 188
892aeafc
VS
189 // ...and run the parser on it:
190 wxClientDC *dc = new wxClientDC(this);
4f9297b0 191 dc->SetMapMode(wxMM_TEXT);
5526e819 192 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
d5db80c2 193 m_OpenedPage = m_OpenedAnchor = m_OpenedPageTitle = wxEmptyString;
4f9297b0 194 m_Parser->SetDC(dc);
33ac7e6f 195 if (m_Cell)
4f9297b0 196 {
89de9af3
VS
197 delete m_Cell;
198 m_Cell = NULL;
f61815af 199 }
892aeafc 200 m_Cell = (wxHtmlContainerCell*) m_Parser->Parse(newsrc);
5526e819 201 delete dc;
4f9297b0
VS
202 m_Cell->SetIndent(m_Borders, wxHTML_INDENT_ALL, wxHTML_UNITS_PIXELS);
203 m_Cell->SetAlignHor(wxHTML_ALIGN_CENTER);
5526e819 204 CreateLayout();
bfb9ee96 205 if (m_tmpCanDrawLocks == 0)
892aeafc 206 Refresh();
5526e819
VS
207 return TRUE;
208}
209
39029898
VS
210bool wxHtmlWindow::AppendToPage(const wxString& source)
211{
212 return SetPage(*(GetParser()->GetSource()) + source);
213}
5526e819
VS
214
215bool wxHtmlWindow::LoadPage(const wxString& location)
216{
1ccabb81 217 wxBusyCursor busyCursor;
790dbce3 218
5526e819
VS
219 wxFSFile *f;
220 bool rt_val;
fc7dfaf8 221 bool needs_refresh = FALSE;
33ac7e6f 222
89de9af3 223 m_tmpCanDrawLocks++;
e421922f 224 if (m_HistoryOn && (m_HistoryPos != -1))
4f9297b0 225 {
960ba969 226 // store scroll position into history item:
5526e819 227 int x, y;
e421922f 228 GetViewStart(&x, &y);
892aeafc 229 (*m_History)[m_HistoryPos].SetPos(y);
5526e819
VS
230 }
231
e421922f 232 if (location[0] == wxT('#'))
4f9297b0 233 {
960ba969 234 // local anchor:
5526e819 235 wxString anch = location.Mid(1) /*1 to end*/;
89de9af3 236 m_tmpCanDrawLocks--;
5526e819 237 rt_val = ScrollToAnchor(anch);
fc7dfaf8
VS
238 m_tmpCanDrawLocks++;
239 }
33ac7e6f 240 else if (location.Find(wxT('#')) != wxNOT_FOUND && location.BeforeFirst(wxT('#')) == m_OpenedPage)
4f9297b0 241 {
fc7dfaf8
VS
242 wxString anch = location.AfterFirst(wxT('#'));
243 m_tmpCanDrawLocks--;
244 rt_val = ScrollToAnchor(anch);
245 m_tmpCanDrawLocks++;
246 }
33ac7e6f
KB
247 else if (location.Find(wxT('#')) != wxNOT_FOUND &&
248 (m_FS->GetPath() + location.BeforeFirst(wxT('#'))) == m_OpenedPage)
e421922f 249 {
fc7dfaf8
VS
250 wxString anch = location.AfterFirst(wxT('#'));
251 m_tmpCanDrawLocks--;
252 rt_val = ScrollToAnchor(anch);
253 m_tmpCanDrawLocks++;
5526e819
VS
254 }
255
33ac7e6f 256 else
4f9297b0 257 {
fc7dfaf8 258 needs_refresh = TRUE;
5526e819 259 // load&display it:
33ac7e6f 260 if (m_RelatedStatusBar != -1)
e421922f 261 {
4f9297b0 262 m_RelatedFrame->SetStatusText(_("Connecting..."), m_RelatedStatusBar);
fc7dfaf8 263 Refresh(FALSE);
5526e819 264 }
790dbce3 265
6cc4e6b8 266 f = m_Parser->OpenURL(wxHTML_URL_PAGE, location);
33ac7e6f 267
7cb9cf89
VS
268 // try to interpret 'location' as filename instead of URL:
269 if (f == NULL)
270 {
271 wxFileName fn(location);
272 wxString location2 = wxFileSystem::FileNameToURL(fn);
273 f = m_Parser->OpenURL(wxHTML_URL_PAGE, location2);
274 }
275
33ac7e6f 276 if (f == NULL)
e421922f 277 {
f6bcfd97 278 wxLogError(_("Unable to open requested HTML document: %s"), location.c_str());
89de9af3 279 m_tmpCanDrawLocks--;
5526e819
VS
280 return FALSE;
281 }
282
33ac7e6f 283 else
e421922f 284 {
5526e819
VS
285 wxNode *node;
286 wxString src = wxEmptyString;
287
33ac7e6f 288 if (m_RelatedStatusBar != -1)
e421922f 289 {
5526e819 290 wxString msg = _("Loading : ") + location;
4f9297b0 291 m_RelatedFrame->SetStatusText(msg, m_RelatedStatusBar);
fc7dfaf8 292 Refresh(FALSE);
5526e819
VS
293 }
294
295 node = m_Filters.GetFirst();
4f9297b0 296 while (node)
e421922f 297 {
4f9297b0
VS
298 wxHtmlFilter *h = (wxHtmlFilter*) node->GetData();
299 if (h->CanRead(*f))
e421922f 300 {
4f9297b0 301 src = h->ReadFile(*f);
5526e819
VS
302 break;
303 }
4f9297b0 304 node = node->GetNext();
5526e819 305 }
33ac7e6f 306 if (src == wxEmptyString)
e421922f 307 {
89de9af3 308 if (m_DefaultFilter == NULL) m_DefaultFilter = GetDefaultFilter();
4f9297b0 309 src = m_DefaultFilter->ReadFile(*f);
89de9af3 310 }
5526e819 311
4f9297b0 312 m_FS->ChangePathTo(f->GetLocation());
5526e819 313 rt_val = SetPage(src);
4f9297b0 314 m_OpenedPage = f->GetLocation();
33ac7e6f 315 if (f->GetAnchor() != wxEmptyString)
e421922f 316 {
4f9297b0 317 ScrollToAnchor(f->GetAnchor());
5526e819
VS
318 }
319
320 delete f;
321
4f9297b0 322 if (m_RelatedStatusBar != -1) m_RelatedFrame->SetStatusText(_("Done"), m_RelatedStatusBar);
5526e819
VS
323 }
324 }
325
4f9297b0
VS
326 if (m_HistoryOn) // add this page to history there:
327 {
892aeafc 328 int c = m_History->GetCount() - (m_HistoryPos + 1);
5526e819 329
0cb9cfb2 330 if (m_HistoryPos < 0 ||
ee19c324
VS
331 (*m_History)[m_HistoryPos].GetPage() != m_OpenedPage ||
332 (*m_History)[m_HistoryPos].GetAnchor() != m_OpenedAnchor)
333 {
334 m_HistoryPos++;
335 for (int i = 0; i < c; i++)
b54e41c5 336 m_History->RemoveAt(m_HistoryPos);
ee19c324
VS
337 m_History->Add(new wxHtmlHistoryItem(m_OpenedPage, m_OpenedAnchor));
338 }
5526e819
VS
339 }
340
096824d7
VS
341 if (m_OpenedPageTitle == wxEmptyString)
342 OnSetTitle(wxFileNameFromPath(m_OpenedPage));
fc7dfaf8 343
33ac7e6f 344 if (needs_refresh)
4f9297b0 345 {
fc7dfaf8
VS
346 m_tmpCanDrawLocks--;
347 Refresh();
348 }
349 else
350 m_tmpCanDrawLocks--;
351
5526e819
VS
352 return rt_val;
353}
354
355
7cb9cf89
VS
356bool wxHtmlWindow::LoadFile(const wxFileName& filename)
357{
358 wxString url = wxFileSystem::FileNameToURL(filename);
359 return LoadPage(url);
360}
361
5526e819
VS
362
363bool wxHtmlWindow::ScrollToAnchor(const wxString& anchor)
364{
4f9297b0 365 const wxHtmlCell *c = m_Cell->Find(wxHTML_COND_ISANCHOR, &anchor);
f3c82859
VS
366 if (!c)
367 {
f6bcfd97 368 wxLogWarning(_("HTML anchor %s does not exist."), anchor.c_str());
f3c82859
VS
369 return FALSE;
370 }
33ac7e6f 371 else
4f9297b0 372 {
5526e819 373 int y;
269e8200 374
4f9297b0 375 for (y = 0; c != NULL; c = c->GetParent()) y += c->GetPosY();
efba2b89 376 Scroll(-1, y / wxHTML_SCROLL_STEP);
5526e819
VS
377 m_OpenedAnchor = anchor;
378 return TRUE;
379 }
380}
381
382
d5db80c2 383void wxHtmlWindow::OnSetTitle(const wxString& title)
5526e819 384{
33ac7e6f 385 if (m_RelatedFrame)
4f9297b0 386 {
5526e819
VS
387 wxString tit;
388 tit.Printf(m_TitleFormat, title.c_str());
4f9297b0 389 m_RelatedFrame->SetTitle(tit);
5526e819 390 }
d5db80c2 391 m_OpenedPageTitle = title;
5526e819
VS
392}
393
394
395
396
397
398void wxHtmlWindow::CreateLayout()
399{
400 int ClientWidth, ClientHeight;
401
402 if (!m_Cell) return;
a547ebff 403
33ac7e6f 404 if (m_Style & wxHW_SCROLLBAR_NEVER)
4f9297b0
VS
405 {
406 SetScrollbars(wxHTML_SCROLL_STEP, 1, m_Cell->GetWidth() / wxHTML_SCROLL_STEP, 0); // always off
a547ebff 407 GetClientSize(&ClientWidth, &ClientHeight);
4f9297b0 408 m_Cell->Layout(ClientWidth);
a547ebff
VS
409 }
410
411 else {
412 GetClientSize(&ClientWidth, &ClientHeight);
4f9297b0 413 m_Cell->Layout(ClientWidth);
33ac7e6f 414 if (ClientHeight < m_Cell->GetHeight() + GetCharHeight())
e421922f 415 {
f3bcfd9b
VS
416 SetScrollbars(
417 wxHTML_SCROLL_STEP, wxHTML_SCROLL_STEP,
4f9297b0
VS
418 m_Cell->GetWidth() / wxHTML_SCROLL_STEP,
419 (m_Cell->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
f3bcfd9b 420 /*cheat: top-level frag is always container*/);
a547ebff 421 }
4f9297b0 422 else /* we fit into window, no need for scrollbars */
e421922f 423 {
4f9297b0 424 SetScrollbars(wxHTML_SCROLL_STEP, 1, m_Cell->GetWidth() / wxHTML_SCROLL_STEP, 0); // disable...
89de9af3 425 GetClientSize(&ClientWidth, &ClientHeight);
4f9297b0 426 m_Cell->Layout(ClientWidth); // ...and relayout
89de9af3 427 }
a547ebff 428 }
5526e819
VS
429}
430
269e8200 431
5526e819
VS
432
433void wxHtmlWindow::ReadCustomization(wxConfigBase *cfg, wxString path)
434{
435 wxString oldpath;
436 wxString tmp;
d5db80c2
VS
437 int p_fontsizes[7];
438 wxString p_fff, p_ffn;
5526e819 439
33ac7e6f 440 if (path != wxEmptyString)
4f9297b0
VS
441 {
442 oldpath = cfg->GetPath();
443 cfg->SetPath(path);
5526e819
VS
444 }
445
892aeafc
VS
446 m_Borders = cfg->Read(wxT("wxHtmlWindow/Borders"), m_Borders);
447 p_fff = cfg->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser->m_FontFaceFixed);
448 p_ffn = cfg->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser->m_FontFaceNormal);
bfb9ee96 449 for (int i = 0; i < 7; i++)
4f9297b0 450 {
66a77a74 451 tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i);
4f9297b0 452 p_fontsizes[i] = cfg->Read(tmp, m_Parser->m_FontsSizes[i]);
5526e819 453 }
8eb2940f 454 SetFonts(p_ffn, p_fff, p_fontsizes);
5526e819
VS
455
456 if (path != wxEmptyString)
4f9297b0 457 cfg->SetPath(oldpath);
5526e819
VS
458}
459
460
461
462void wxHtmlWindow::WriteCustomization(wxConfigBase *cfg, wxString path)
463{
464 wxString oldpath;
465 wxString tmp;
466
33ac7e6f 467 if (path != wxEmptyString)
4f9297b0
VS
468 {
469 oldpath = cfg->GetPath();
470 cfg->SetPath(path);
5526e819
VS
471 }
472
892aeafc
VS
473 cfg->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders);
474 cfg->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser->m_FontFaceFixed);
475 cfg->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser->m_FontFaceNormal);
bfb9ee96 476 for (int i = 0; i < 7; i++)
4f9297b0 477 {
66a77a74 478 tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i);
4f9297b0 479 cfg->Write(tmp, (long) m_Parser->m_FontsSizes[i]);
5526e819
VS
480 }
481
482 if (path != wxEmptyString)
4f9297b0 483 cfg->SetPath(oldpath);
5526e819
VS
484}
485
486
487
488bool wxHtmlWindow::HistoryBack()
489{
490 wxString a, l;
491
492 if (m_HistoryPos < 1) return FALSE;
493
bbda1088
VS
494 // store scroll position into history item:
495 int x, y;
e421922f 496 GetViewStart(&x, &y);
892aeafc 497 (*m_History)[m_HistoryPos].SetPos(y);
bbda1088
VS
498
499 // go to previous position:
5526e819
VS
500 m_HistoryPos--;
501
892aeafc
VS
502 l = (*m_History)[m_HistoryPos].GetPage();
503 a = (*m_History)[m_HistoryPos].GetAnchor();
5526e819 504 m_HistoryOn = FALSE;
89de9af3 505 m_tmpCanDrawLocks++;
5526e819 506 if (a == wxEmptyString) LoadPage(l);
fc7dfaf8 507 else LoadPage(l + wxT("#") + a);
5526e819 508 m_HistoryOn = TRUE;
89de9af3 509 m_tmpCanDrawLocks--;
892aeafc 510 Scroll(0, (*m_History)[m_HistoryPos].GetPos());
5526e819
VS
511 Refresh();
512 return TRUE;
513}
514
1b113a81
VS
515bool wxHtmlWindow::HistoryCanBack()
516{
517 if (m_HistoryPos < 1) return FALSE;
518 return TRUE ;
519}
5526e819
VS
520
521
522bool wxHtmlWindow::HistoryForward()
523{
524 wxString a, l;
525
526 if (m_HistoryPos == -1) return FALSE;
892aeafc 527 if (m_HistoryPos >= (int)m_History->GetCount() - 1)return FALSE;
5526e819
VS
528
529 m_OpenedPage = wxEmptyString; // this will disable adding new entry into history in LoadPage()
530
531 m_HistoryPos++;
892aeafc
VS
532 l = (*m_History)[m_HistoryPos].GetPage();
533 a = (*m_History)[m_HistoryPos].GetAnchor();
5526e819 534 m_HistoryOn = FALSE;
89de9af3 535 m_tmpCanDrawLocks++;
5526e819 536 if (a == wxEmptyString) LoadPage(l);
fc7dfaf8 537 else LoadPage(l + wxT("#") + a);
5526e819 538 m_HistoryOn = TRUE;
89de9af3 539 m_tmpCanDrawLocks--;
892aeafc 540 Scroll(0, (*m_History)[m_HistoryPos].GetPos());
5526e819
VS
541 Refresh();
542 return TRUE;
543}
544
1b113a81
VS
545bool wxHtmlWindow::HistoryCanForward()
546{
547 if (m_HistoryPos == -1) return FALSE;
892aeafc 548 if (m_HistoryPos >= (int)m_History->GetCount() - 1)return FALSE;
1b113a81
VS
549 return TRUE ;
550}
5526e819
VS
551
552
553void wxHtmlWindow::HistoryClear()
554{
892aeafc 555 m_History->Empty();
5526e819
VS
556 m_HistoryPos = -1;
557}
558
892aeafc
VS
559void wxHtmlWindow::AddProcessor(wxHtmlProcessor *processor)
560{
561 if (!m_Processors)
562 {
563 m_Processors = new wxHtmlProcessorList;
564 m_Processors->DeleteContents(TRUE);
565 }
566 wxHtmlProcessorList::Node *node;
bfb9ee96 567
892aeafc
VS
568 for (node = m_Processors->GetFirst(); node; node = node->GetNext())
569 {
bfb9ee96 570 if (processor->GetPriority() > node->GetData()->GetPriority())
892aeafc
VS
571 {
572 m_Processors->Insert(node, processor);
960ba969 573 return;
892aeafc
VS
574 }
575 }
960ba969 576 m_Processors->Append(processor);
892aeafc
VS
577}
578
960ba969 579/*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor *processor)
892aeafc 580{
960ba969 581 if (!m_GlobalProcessors)
892aeafc 582 {
960ba969
VS
583 m_GlobalProcessors = new wxHtmlProcessorList;
584 m_GlobalProcessors->DeleteContents(TRUE);
892aeafc
VS
585 }
586 wxHtmlProcessorList::Node *node;
e421922f 587
960ba969 588 for (node = m_GlobalProcessors->GetFirst(); node; node = node->GetNext())
892aeafc 589 {
bfb9ee96 590 if (processor->GetPriority() > node->GetData()->GetPriority())
892aeafc 591 {
960ba969
VS
592 m_GlobalProcessors->Insert(node, processor);
593 return;
892aeafc
VS
594 }
595 }
960ba969 596 m_GlobalProcessors->Append(processor);
892aeafc
VS
597}
598
5526e819
VS
599
600
601wxList wxHtmlWindow::m_Filters;
a76015e6 602wxHtmlFilter *wxHtmlWindow::m_DefaultFilter = NULL;
66806a0b
VS
603wxCursor *wxHtmlWindow::s_cur_hand = NULL;
604wxCursor *wxHtmlWindow::s_cur_arrow = NULL;
960ba969 605wxHtmlProcessorList *wxHtmlWindow::m_GlobalProcessors = NULL;
a76015e6
VS
606
607void wxHtmlWindow::CleanUpStatics()
608{
eeb029a9 609 wxDELETE(m_DefaultFilter);
269e8200
RD
610 m_Filters.DeleteContents(TRUE);
611 m_Filters.Clear();
eeb029a9
VS
612 wxDELETE(m_GlobalProcessors);
613 wxDELETE(s_cur_hand);
614 wxDELETE(s_cur_arrow);
a76015e6
VS
615}
616
617
5526e819
VS
618
619void wxHtmlWindow::AddFilter(wxHtmlFilter *filter)
620{
5526e819
VS
621 m_Filters.Append(filter);
622}
623
624
625
626
0b2dadd3 627void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo& link)
5526e819 628{
0b2dadd3 629 LoadPage(link.GetHref());
5526e819
VS
630}
631
f6010d8f
VZ
632void wxHtmlWindow::OnCellClicked(wxHtmlCell *cell,
633 wxCoord x, wxCoord y,
634 const wxMouseEvent& event)
19193a2c 635{
f6010d8f 636 wxCHECK_RET( cell, _T("can't be called with NULL cell") );
5526e819 637
f6010d8f
VZ
638 cell->OnMouseClick(this, x, y, event);
639}
640
641void wxHtmlWindow::OnCellMouseHover(wxHtmlCell * WXUNUSED(cell),
642 wxCoord WXUNUSED(x), wxCoord WXUNUSED(y))
643{
644 // do nothing here
645}
5526e819
VS
646
647void wxHtmlWindow::OnDraw(wxDC& dc)
648{
b835e9bf 649 if (m_tmpCanDrawLocks > 0 || m_Cell == NULL) return;
5526e819 650
b835e9bf
VS
651 int x, y;
652 wxRect rect = GetUpdateRegion().GetBox();
33ac7e6f 653
5526e819 654 dc.SetMapMode(wxMM_TEXT);
5526e819 655 dc.SetBackgroundMode(wxTRANSPARENT);
e421922f 656 GetViewStart(&x, &y);
5526e819 657
790dbce3
RD
658 m_Cell->Draw(dc, 0, 0,
659 y * wxHTML_SCROLL_STEP + rect.GetTop(),
b835e9bf 660 y * wxHTML_SCROLL_STEP + rect.GetBottom());
5526e819
VS
661}
662
663
664
665
666void wxHtmlWindow::OnSize(wxSizeEvent& event)
667{
668 wxScrolledWindow::OnSize(event);
669 CreateLayout();
f6bcfd97 670 Refresh();
5526e819
VS
671}
672
673
5526e819
VS
674void wxHtmlWindow::OnMouseEvent(wxMouseEvent& event)
675{
676 m_tmpMouseMoved = TRUE;
677
33ac7e6f 678 if (event.ButtonDown())
4f9297b0 679 {
790dbce3 680 SetFocus();
f6010d8f
VZ
681 if ( m_Cell )
682 {
683 int sx, sy;
684 GetViewStart(&sx, &sy);
685 sx *= wxHTML_SCROLL_STEP;
686 sy *= wxHTML_SCROLL_STEP;
5526e819 687
f6010d8f
VZ
688 wxPoint pos = event.GetPosition();
689 pos.x += sx;
690 pos.y += sy;
5526e819 691
f6010d8f
VZ
692 wxHtmlCell *cell = m_Cell->FindCellByPos(pos.x, pos.y);
693
694 // VZ: is it possible that we don't find anything at all?
d168485f
VS
695 // VS: yes. FindCellByPos returns terminal cell and
696 // containers may have empty borders
f6010d8f
VZ
697 if ( cell )
698 OnCellClicked(cell, pos.x, pos.y, event);
699 }
5526e819
VS
700 }
701}
702
703
704
33ac7e6f 705void wxHtmlWindow::OnIdle(wxIdleEvent& WXUNUSED(event))
5526e819 706{
33ac7e6f 707 if (s_cur_hand == NULL)
66806a0b
VS
708 {
709 s_cur_hand = new wxCursor(wxCURSOR_HAND);
710 s_cur_arrow = new wxCursor(wxCURSOR_ARROW);
711 }
5526e819 712
33ac7e6f 713 if (m_tmpMouseMoved && (m_Cell != NULL))
4f9297b0 714 {
5526e819 715 int sx, sy;
0cb9cfb2
VZ
716 GetViewStart(&sx, &sy);
717 sx *= wxHTML_SCROLL_STEP;
718 sy *= wxHTML_SCROLL_STEP;
5526e819 719
0cb9cfb2 720 int x, y;
5526e819
VS
721 wxGetMousePosition(&x, &y);
722 ScreenToClient(&x, &y);
f6010d8f
VZ
723 x += sx;
724 y += sy;
0cb9cfb2 725
f6010d8f
VZ
726 wxHtmlCell *cell = m_Cell->FindCellByPos(x, y);
727 if ( cell != m_tmpLastCell )
e421922f 728 {
f6010d8f
VZ
729 wxHtmlLinkInfo *lnk = cell ? cell->GetLink(x, y) : NULL;
730
731 if (lnk != m_tmpLastLink)
e421922f 732 {
f6010d8f
VZ
733 if (lnk == NULL)
734 {
735 SetCursor(*s_cur_arrow);
736 if (m_RelatedStatusBar != -1)
737 m_RelatedFrame->SetStatusText(wxEmptyString, m_RelatedStatusBar);
738 }
739 else
740 {
741 SetCursor(*s_cur_hand);
742 if (m_RelatedStatusBar != -1)
743 m_RelatedFrame->SetStatusText(lnk->GetHref(), m_RelatedStatusBar);
744 }
745 m_tmpLastLink = lnk;
622ea783 746 }
f6010d8f
VZ
747
748 m_tmpLastCell = cell;
5526e819 749 }
f6010d8f
VZ
750 else // mouse moved but stayed in the same cell
751 {
752 if ( cell )
753 OnCellMouseHover(cell, x, y);
754 }
755
5526e819
VS
756 m_tmpMouseMoved = FALSE;
757 }
758}
759
760
bfb9ee96 761IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor,wxObject)
5526e819
VS
762
763IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow,wxScrolledWindow)
764
765BEGIN_EVENT_TABLE(wxHtmlWindow, wxScrolledWindow)
766 EVT_SIZE(wxHtmlWindow::OnSize)
767 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent)
0cb9cfb2 768 EVT_RIGHT_DOWN(wxHtmlWindow::OnMouseEvent)
5526e819
VS
769 EVT_MOTION(wxHtmlWindow::OnMouseEvent)
770 EVT_IDLE(wxHtmlWindow::OnIdle)
5526e819
VS
771END_EVENT_TABLE()
772
773
774
775
776
a76015e6
VS
777// A module to allow initialization/cleanup
778// without calling these functions from app.cpp or from
779// the user's application.
780
781class wxHtmlWinModule: public wxModule
782{
783DECLARE_DYNAMIC_CLASS(wxHtmlWinModule)
784public:
785 wxHtmlWinModule() : wxModule() {}
786 bool OnInit() { return TRUE; }
787 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
788};
789
790IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule, wxModule)
791
5526e819 792
0cecad31
VS
793// This hack forces the linker to always link in m_* files
794// (wxHTML doesn't work without handlers from these files)
795#include "wx/html/forcelnk.h"
796FORCE_WXHTML_MODULES()
5526e819 797
b835e9bf 798#endif