]> git.saurik.com Git - wxWidgets.git/blame - src/html/htmlwin.cpp
no real changes
[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
VS
20
21#ifdef __BORDLANDC__
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
44class WXDLLEXPORT wxHtmlHistoryItem : public wxObject
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
97bool wxHtmlWindow::Create(wxWindow *parent, wxWindowID id,
98 const wxPoint& pos, const wxSize& size,
99 long style, const wxString& name)
100{
101 if (!wxScrolledWindow::Create(parent, id, pos, size,
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{
217 wxFSFile *f;
218 bool rt_val;
fc7dfaf8 219 bool needs_refresh = FALSE;
33ac7e6f 220
169ee06c 221 SetCursor(*wxHOURGLASS_CURSOR);
fc7dfaf8 222 wxYield(); Refresh(FALSE);
5526e819 223
89de9af3 224 m_tmpCanDrawLocks++;
e421922f 225 if (m_HistoryOn && (m_HistoryPos != -1))
4f9297b0 226 {
960ba969 227 // store scroll position into history item:
5526e819 228 int x, y;
e421922f 229 GetViewStart(&x, &y);
892aeafc 230 (*m_History)[m_HistoryPos].SetPos(y);
5526e819
VS
231 }
232
e421922f 233 if (location[0] == wxT('#'))
4f9297b0 234 {
960ba969 235 // local anchor:
5526e819 236 wxString anch = location.Mid(1) /*1 to end*/;
89de9af3 237 m_tmpCanDrawLocks--;
5526e819 238 rt_val = ScrollToAnchor(anch);
fc7dfaf8
VS
239 m_tmpCanDrawLocks++;
240 }
33ac7e6f 241 else if (location.Find(wxT('#')) != wxNOT_FOUND && location.BeforeFirst(wxT('#')) == m_OpenedPage)
4f9297b0 242 {
fc7dfaf8
VS
243 wxString anch = location.AfterFirst(wxT('#'));
244 m_tmpCanDrawLocks--;
245 rt_val = ScrollToAnchor(anch);
246 m_tmpCanDrawLocks++;
247 }
33ac7e6f
KB
248 else if (location.Find(wxT('#')) != wxNOT_FOUND &&
249 (m_FS->GetPath() + location.BeforeFirst(wxT('#'))) == m_OpenedPage)
e421922f 250 {
fc7dfaf8
VS
251 wxString anch = location.AfterFirst(wxT('#'));
252 m_tmpCanDrawLocks--;
253 rt_val = ScrollToAnchor(anch);
254 m_tmpCanDrawLocks++;
5526e819
VS
255 }
256
33ac7e6f 257 else
4f9297b0 258 {
fc7dfaf8 259 needs_refresh = TRUE;
5526e819 260 // load&display it:
33ac7e6f 261 if (m_RelatedStatusBar != -1)
e421922f 262 {
4f9297b0 263 m_RelatedFrame->SetStatusText(_("Connecting..."), m_RelatedStatusBar);
fc7dfaf8 264 Refresh(FALSE);
5526e819
VS
265 }
266
4f9297b0 267 f = m_FS->OpenFile(location);
33ac7e6f
KB
268
269 if (f == NULL)
e421922f 270 {
f6bcfd97 271 wxLogError(_("Unable to open requested HTML document: %s"), location.c_str());
89de9af3 272 m_tmpCanDrawLocks--;
169ee06c
VS
273
274 SetCursor(*wxSTANDARD_CURSOR);
5526e819
VS
275 return FALSE;
276 }
277
33ac7e6f 278 else
e421922f 279 {
5526e819
VS
280 wxNode *node;
281 wxString src = wxEmptyString;
282
33ac7e6f 283 if (m_RelatedStatusBar != -1)
e421922f 284 {
5526e819 285 wxString msg = _("Loading : ") + location;
4f9297b0 286 m_RelatedFrame->SetStatusText(msg, m_RelatedStatusBar);
fc7dfaf8 287 Refresh(FALSE);
5526e819
VS
288 }
289
290 node = m_Filters.GetFirst();
4f9297b0 291 while (node)
e421922f 292 {
4f9297b0
VS
293 wxHtmlFilter *h = (wxHtmlFilter*) node->GetData();
294 if (h->CanRead(*f))
e421922f 295 {
4f9297b0 296 src = h->ReadFile(*f);
5526e819
VS
297 break;
298 }
4f9297b0 299 node = node->GetNext();
5526e819 300 }
33ac7e6f 301 if (src == wxEmptyString)
e421922f 302 {
89de9af3 303 if (m_DefaultFilter == NULL) m_DefaultFilter = GetDefaultFilter();
4f9297b0 304 src = m_DefaultFilter->ReadFile(*f);
89de9af3 305 }
5526e819 306
4f9297b0 307 m_FS->ChangePathTo(f->GetLocation());
5526e819 308 rt_val = SetPage(src);
4f9297b0 309 m_OpenedPage = f->GetLocation();
33ac7e6f 310 if (f->GetAnchor() != wxEmptyString)
e421922f 311 {
fc7dfaf8 312 wxYield();
4f9297b0 313 ScrollToAnchor(f->GetAnchor());
5526e819
VS
314 }
315
316 delete f;
317
4f9297b0 318 if (m_RelatedStatusBar != -1) m_RelatedFrame->SetStatusText(_("Done"), m_RelatedStatusBar);
5526e819
VS
319 }
320 }
321
4f9297b0
VS
322 if (m_HistoryOn) // add this page to history there:
323 {
892aeafc 324 int c = m_History->GetCount() - (m_HistoryPos + 1);
5526e819 325
0cb9cfb2 326 if (m_HistoryPos < 0 ||
ee19c324
VS
327 (*m_History)[m_HistoryPos].GetPage() != m_OpenedPage ||
328 (*m_History)[m_HistoryPos].GetAnchor() != m_OpenedAnchor)
329 {
330 m_HistoryPos++;
331 for (int i = 0; i < c; i++)
b54e41c5 332 m_History->RemoveAt(m_HistoryPos);
ee19c324
VS
333 m_History->Add(new wxHtmlHistoryItem(m_OpenedPage, m_OpenedAnchor));
334 }
5526e819
VS
335 }
336
096824d7
VS
337 if (m_OpenedPageTitle == wxEmptyString)
338 OnSetTitle(wxFileNameFromPath(m_OpenedPage));
169ee06c 339 SetCursor(*wxSTANDARD_CURSOR);
fc7dfaf8 340
33ac7e6f 341 if (needs_refresh)
4f9297b0 342 {
fc7dfaf8
VS
343 wxYield();
344 m_tmpCanDrawLocks--;
345 Refresh();
346 }
347 else
348 m_tmpCanDrawLocks--;
349
5526e819
VS
350 return rt_val;
351}
352
353
354
355bool wxHtmlWindow::ScrollToAnchor(const wxString& anchor)
356{
4f9297b0 357 const wxHtmlCell *c = m_Cell->Find(wxHTML_COND_ISANCHOR, &anchor);
f3c82859
VS
358 if (!c)
359 {
f6bcfd97 360 wxLogWarning(_("HTML anchor %s does not exist."), anchor.c_str());
f3c82859
VS
361 return FALSE;
362 }
33ac7e6f 363 else
4f9297b0 364 {
5526e819 365 int y;
269e8200 366
4f9297b0 367 for (y = 0; c != NULL; c = c->GetParent()) y += c->GetPosY();
efba2b89 368 Scroll(-1, y / wxHTML_SCROLL_STEP);
5526e819
VS
369 m_OpenedAnchor = anchor;
370 return TRUE;
371 }
372}
373
374
d5db80c2 375void wxHtmlWindow::OnSetTitle(const wxString& title)
5526e819 376{
33ac7e6f 377 if (m_RelatedFrame)
4f9297b0 378 {
5526e819
VS
379 wxString tit;
380 tit.Printf(m_TitleFormat, title.c_str());
4f9297b0 381 m_RelatedFrame->SetTitle(tit);
5526e819 382 }
d5db80c2 383 m_OpenedPageTitle = title;
5526e819
VS
384}
385
386
387
388
389
390void wxHtmlWindow::CreateLayout()
391{
392 int ClientWidth, ClientHeight;
393
394 if (!m_Cell) return;
a547ebff 395
33ac7e6f 396 if (m_Style & wxHW_SCROLLBAR_NEVER)
4f9297b0
VS
397 {
398 SetScrollbars(wxHTML_SCROLL_STEP, 1, m_Cell->GetWidth() / wxHTML_SCROLL_STEP, 0); // always off
a547ebff 399 GetClientSize(&ClientWidth, &ClientHeight);
4f9297b0 400 m_Cell->Layout(ClientWidth);
a547ebff
VS
401 }
402
403 else {
404 GetClientSize(&ClientWidth, &ClientHeight);
4f9297b0 405 m_Cell->Layout(ClientWidth);
33ac7e6f 406 if (ClientHeight < m_Cell->GetHeight() + GetCharHeight())
e421922f 407 {
f3bcfd9b
VS
408 SetScrollbars(
409 wxHTML_SCROLL_STEP, wxHTML_SCROLL_STEP,
4f9297b0
VS
410 m_Cell->GetWidth() / wxHTML_SCROLL_STEP,
411 (m_Cell->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
f3bcfd9b 412 /*cheat: top-level frag is always container*/);
a547ebff 413 }
4f9297b0 414 else /* we fit into window, no need for scrollbars */
e421922f 415 {
4f9297b0 416 SetScrollbars(wxHTML_SCROLL_STEP, 1, m_Cell->GetWidth() / wxHTML_SCROLL_STEP, 0); // disable...
89de9af3 417 GetClientSize(&ClientWidth, &ClientHeight);
4f9297b0 418 m_Cell->Layout(ClientWidth); // ...and relayout
89de9af3 419 }
a547ebff 420 }
5526e819
VS
421}
422
269e8200 423
5526e819
VS
424
425void wxHtmlWindow::ReadCustomization(wxConfigBase *cfg, wxString path)
426{
427 wxString oldpath;
428 wxString tmp;
d5db80c2
VS
429 int p_fontsizes[7];
430 wxString p_fff, p_ffn;
5526e819 431
33ac7e6f 432 if (path != wxEmptyString)
4f9297b0
VS
433 {
434 oldpath = cfg->GetPath();
435 cfg->SetPath(path);
5526e819
VS
436 }
437
892aeafc
VS
438 m_Borders = cfg->Read(wxT("wxHtmlWindow/Borders"), m_Borders);
439 p_fff = cfg->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser->m_FontFaceFixed);
440 p_ffn = cfg->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser->m_FontFaceNormal);
bfb9ee96 441 for (int i = 0; i < 7; i++)
4f9297b0 442 {
66a77a74 443 tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i);
4f9297b0 444 p_fontsizes[i] = cfg->Read(tmp, m_Parser->m_FontsSizes[i]);
5526e819 445 }
8eb2940f 446 SetFonts(p_ffn, p_fff, p_fontsizes);
5526e819
VS
447
448 if (path != wxEmptyString)
4f9297b0 449 cfg->SetPath(oldpath);
5526e819
VS
450}
451
452
453
454void wxHtmlWindow::WriteCustomization(wxConfigBase *cfg, wxString path)
455{
456 wxString oldpath;
457 wxString tmp;
458
33ac7e6f 459 if (path != wxEmptyString)
4f9297b0
VS
460 {
461 oldpath = cfg->GetPath();
462 cfg->SetPath(path);
5526e819
VS
463 }
464
892aeafc
VS
465 cfg->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders);
466 cfg->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser->m_FontFaceFixed);
467 cfg->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser->m_FontFaceNormal);
bfb9ee96 468 for (int i = 0; i < 7; i++)
4f9297b0 469 {
66a77a74 470 tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i);
4f9297b0 471 cfg->Write(tmp, (long) m_Parser->m_FontsSizes[i]);
5526e819
VS
472 }
473
474 if (path != wxEmptyString)
4f9297b0 475 cfg->SetPath(oldpath);
5526e819
VS
476}
477
478
479
480bool wxHtmlWindow::HistoryBack()
481{
482 wxString a, l;
483
484 if (m_HistoryPos < 1) return FALSE;
485
bbda1088
VS
486 // store scroll position into history item:
487 int x, y;
e421922f 488 GetViewStart(&x, &y);
892aeafc 489 (*m_History)[m_HistoryPos].SetPos(y);
bbda1088
VS
490
491 // go to previous position:
5526e819
VS
492 m_HistoryPos--;
493
892aeafc
VS
494 l = (*m_History)[m_HistoryPos].GetPage();
495 a = (*m_History)[m_HistoryPos].GetAnchor();
5526e819 496 m_HistoryOn = FALSE;
89de9af3 497 m_tmpCanDrawLocks++;
5526e819 498 if (a == wxEmptyString) LoadPage(l);
fc7dfaf8 499 else LoadPage(l + wxT("#") + a);
5526e819 500 m_HistoryOn = TRUE;
c88293a4 501 wxYield();
89de9af3 502 m_tmpCanDrawLocks--;
892aeafc 503 Scroll(0, (*m_History)[m_HistoryPos].GetPos());
5526e819
VS
504 Refresh();
505 return TRUE;
506}
507
1b113a81
VS
508bool wxHtmlWindow::HistoryCanBack()
509{
510 if (m_HistoryPos < 1) return FALSE;
511 return TRUE ;
512}
5526e819
VS
513
514
515bool wxHtmlWindow::HistoryForward()
516{
517 wxString a, l;
518
519 if (m_HistoryPos == -1) return FALSE;
892aeafc 520 if (m_HistoryPos >= (int)m_History->GetCount() - 1)return FALSE;
5526e819
VS
521
522 m_OpenedPage = wxEmptyString; // this will disable adding new entry into history in LoadPage()
523
524 m_HistoryPos++;
892aeafc
VS
525 l = (*m_History)[m_HistoryPos].GetPage();
526 a = (*m_History)[m_HistoryPos].GetAnchor();
5526e819 527 m_HistoryOn = FALSE;
89de9af3 528 m_tmpCanDrawLocks++;
5526e819 529 if (a == wxEmptyString) LoadPage(l);
fc7dfaf8 530 else LoadPage(l + wxT("#") + a);
5526e819 531 m_HistoryOn = TRUE;
c88293a4 532 wxYield();
89de9af3 533 m_tmpCanDrawLocks--;
892aeafc 534 Scroll(0, (*m_History)[m_HistoryPos].GetPos());
5526e819
VS
535 Refresh();
536 return TRUE;
537}
538
1b113a81
VS
539bool wxHtmlWindow::HistoryCanForward()
540{
541 if (m_HistoryPos == -1) return FALSE;
892aeafc 542 if (m_HistoryPos >= (int)m_History->GetCount() - 1)return FALSE;
1b113a81
VS
543 return TRUE ;
544}
5526e819
VS
545
546
547void wxHtmlWindow::HistoryClear()
548{
892aeafc 549 m_History->Empty();
5526e819
VS
550 m_HistoryPos = -1;
551}
552
892aeafc
VS
553void wxHtmlWindow::AddProcessor(wxHtmlProcessor *processor)
554{
555 if (!m_Processors)
556 {
557 m_Processors = new wxHtmlProcessorList;
558 m_Processors->DeleteContents(TRUE);
559 }
560 wxHtmlProcessorList::Node *node;
bfb9ee96 561
892aeafc
VS
562 for (node = m_Processors->GetFirst(); node; node = node->GetNext())
563 {
bfb9ee96 564 if (processor->GetPriority() > node->GetData()->GetPriority())
892aeafc
VS
565 {
566 m_Processors->Insert(node, processor);
960ba969 567 return;
892aeafc
VS
568 }
569 }
960ba969 570 m_Processors->Append(processor);
892aeafc
VS
571}
572
960ba969 573/*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor *processor)
892aeafc 574{
960ba969 575 if (!m_GlobalProcessors)
892aeafc 576 {
960ba969
VS
577 m_GlobalProcessors = new wxHtmlProcessorList;
578 m_GlobalProcessors->DeleteContents(TRUE);
892aeafc
VS
579 }
580 wxHtmlProcessorList::Node *node;
e421922f 581
960ba969 582 for (node = m_GlobalProcessors->GetFirst(); node; node = node->GetNext())
892aeafc 583 {
bfb9ee96 584 if (processor->GetPriority() > node->GetData()->GetPriority())
892aeafc 585 {
960ba969
VS
586 m_GlobalProcessors->Insert(node, processor);
587 return;
892aeafc
VS
588 }
589 }
960ba969 590 m_GlobalProcessors->Append(processor);
892aeafc
VS
591}
592
5526e819
VS
593
594
595wxList wxHtmlWindow::m_Filters;
a76015e6 596wxHtmlFilter *wxHtmlWindow::m_DefaultFilter = NULL;
66806a0b
VS
597wxCursor *wxHtmlWindow::s_cur_hand = NULL;
598wxCursor *wxHtmlWindow::s_cur_arrow = NULL;
960ba969 599wxHtmlProcessorList *wxHtmlWindow::m_GlobalProcessors = NULL;
a76015e6
VS
600
601void wxHtmlWindow::CleanUpStatics()
602{
892aeafc 603 delete m_DefaultFilter;
a76015e6 604 m_DefaultFilter = NULL;
269e8200
RD
605 m_Filters.DeleteContents(TRUE);
606 m_Filters.Clear();
960ba969
VS
607 delete m_GlobalProcessors;
608 m_GlobalProcessors = NULL;
892aeafc
VS
609 delete s_cur_hand;
610 delete s_cur_arrow;
a76015e6
VS
611}
612
613
5526e819
VS
614
615void wxHtmlWindow::AddFilter(wxHtmlFilter *filter)
616{
5526e819
VS
617 m_Filters.Append(filter);
618}
619
620
621
622
0b2dadd3 623void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo& link)
5526e819 624{
0b2dadd3 625 LoadPage(link.GetHref());
5526e819
VS
626}
627
f6010d8f
VZ
628void wxHtmlWindow::OnCellClicked(wxHtmlCell *cell,
629 wxCoord x, wxCoord y,
630 const wxMouseEvent& event)
19193a2c 631{
f6010d8f 632 wxCHECK_RET( cell, _T("can't be called with NULL cell") );
5526e819 633
f6010d8f
VZ
634 cell->OnMouseClick(this, x, y, event);
635}
636
637void wxHtmlWindow::OnCellMouseHover(wxHtmlCell * WXUNUSED(cell),
638 wxCoord WXUNUSED(x), wxCoord WXUNUSED(y))
639{
640 // do nothing here
641}
5526e819
VS
642
643void wxHtmlWindow::OnDraw(wxDC& dc)
644{
b835e9bf 645 if (m_tmpCanDrawLocks > 0 || m_Cell == NULL) return;
5526e819 646
b835e9bf
VS
647 int x, y;
648 wxRect rect = GetUpdateRegion().GetBox();
33ac7e6f 649
5526e819 650 dc.SetMapMode(wxMM_TEXT);
5526e819 651 dc.SetBackgroundMode(wxTRANSPARENT);
e421922f 652 GetViewStart(&x, &y);
5526e819 653
b835e9bf
VS
654 m_Cell->Draw(dc, 0, 0,
655 y * wxHTML_SCROLL_STEP + rect.GetTop(),
656 y * wxHTML_SCROLL_STEP + rect.GetBottom());
5526e819
VS
657}
658
659
660
661
662void wxHtmlWindow::OnSize(wxSizeEvent& event)
663{
664 wxScrolledWindow::OnSize(event);
665 CreateLayout();
f6bcfd97 666 Refresh();
5526e819
VS
667}
668
669
5526e819
VS
670void wxHtmlWindow::OnMouseEvent(wxMouseEvent& event)
671{
672 m_tmpMouseMoved = TRUE;
673
33ac7e6f 674 if (event.ButtonDown())
4f9297b0 675 {
f6010d8f
VZ
676 if ( m_Cell )
677 {
678 int sx, sy;
679 GetViewStart(&sx, &sy);
680 sx *= wxHTML_SCROLL_STEP;
681 sy *= wxHTML_SCROLL_STEP;
5526e819 682
f6010d8f
VZ
683 wxPoint pos = event.GetPosition();
684 pos.x += sx;
685 pos.y += sy;
5526e819 686
f6010d8f
VZ
687 wxHtmlCell *cell = m_Cell->FindCellByPos(pos.x, pos.y);
688
689 // VZ: is it possible that we don't find anything at all?
d168485f
VS
690 // VS: yes. FindCellByPos returns terminal cell and
691 // containers may have empty borders
f6010d8f
VZ
692 if ( cell )
693 OnCellClicked(cell, pos.x, pos.y, event);
694 }
5526e819
VS
695 }
696}
697
698
699
33ac7e6f 700void wxHtmlWindow::OnIdle(wxIdleEvent& WXUNUSED(event))
5526e819 701{
33ac7e6f 702 if (s_cur_hand == NULL)
66806a0b
VS
703 {
704 s_cur_hand = new wxCursor(wxCURSOR_HAND);
705 s_cur_arrow = new wxCursor(wxCURSOR_ARROW);
706 }
5526e819 707
33ac7e6f 708 if (m_tmpMouseMoved && (m_Cell != NULL))
4f9297b0 709 {
5526e819 710 int sx, sy;
0cb9cfb2
VZ
711 GetViewStart(&sx, &sy);
712 sx *= wxHTML_SCROLL_STEP;
713 sy *= wxHTML_SCROLL_STEP;
5526e819 714
0cb9cfb2 715 int x, y;
5526e819
VS
716 wxGetMousePosition(&x, &y);
717 ScreenToClient(&x, &y);
f6010d8f
VZ
718 x += sx;
719 y += sy;
0cb9cfb2 720
f6010d8f
VZ
721 wxHtmlCell *cell = m_Cell->FindCellByPos(x, y);
722 if ( cell != m_tmpLastCell )
e421922f 723 {
f6010d8f
VZ
724 wxHtmlLinkInfo *lnk = cell ? cell->GetLink(x, y) : NULL;
725
726 if (lnk != m_tmpLastLink)
e421922f 727 {
f6010d8f
VZ
728 if (lnk == NULL)
729 {
730 SetCursor(*s_cur_arrow);
731 if (m_RelatedStatusBar != -1)
732 m_RelatedFrame->SetStatusText(wxEmptyString, m_RelatedStatusBar);
733 }
734 else
735 {
736 SetCursor(*s_cur_hand);
737 if (m_RelatedStatusBar != -1)
738 m_RelatedFrame->SetStatusText(lnk->GetHref(), m_RelatedStatusBar);
739 }
740 m_tmpLastLink = lnk;
622ea783 741 }
f6010d8f
VZ
742
743 m_tmpLastCell = cell;
5526e819 744 }
f6010d8f
VZ
745 else // mouse moved but stayed in the same cell
746 {
747 if ( cell )
748 OnCellMouseHover(cell, x, y);
749 }
750
5526e819
VS
751 m_tmpMouseMoved = FALSE;
752 }
753}
754
755
bfb9ee96 756IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor,wxObject)
5526e819
VS
757
758IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow,wxScrolledWindow)
759
760BEGIN_EVENT_TABLE(wxHtmlWindow, wxScrolledWindow)
761 EVT_SIZE(wxHtmlWindow::OnSize)
762 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent)
0cb9cfb2 763 EVT_RIGHT_DOWN(wxHtmlWindow::OnMouseEvent)
5526e819
VS
764 EVT_MOTION(wxHtmlWindow::OnMouseEvent)
765 EVT_IDLE(wxHtmlWindow::OnIdle)
5526e819
VS
766END_EVENT_TABLE()
767
768
769
770
771
a76015e6
VS
772// A module to allow initialization/cleanup
773// without calling these functions from app.cpp or from
774// the user's application.
775
776class wxHtmlWinModule: public wxModule
777{
778DECLARE_DYNAMIC_CLASS(wxHtmlWinModule)
779public:
780 wxHtmlWinModule() : wxModule() {}
781 bool OnInit() { return TRUE; }
782 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
783};
784
785IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule, wxModule)
786
5526e819 787
0cecad31
VS
788// This hack forces the linker to always link in m_* files
789// (wxHTML doesn't work without handlers from these files)
790#include "wx/html/forcelnk.h"
791FORCE_WXHTML_MODULES()
5526e819 792
b835e9bf 793#endif