]> git.saurik.com Git - wxWidgets.git/blame - src/html/htmlwin.cpp
compilation fix (not all paths returned a value)
[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);
adf2eb2d
VS
95 m_selection = NULL;
96 m_makingSelection = false;
4f417130
VS
97}
98
790dbce3 99bool wxHtmlWindow::Create(wxWindow *parent, wxWindowID id,
4f417130 100 const wxPoint& pos, const wxSize& size,
790dbce3 101 long style, const wxString& name)
4f417130 102{
790dbce3 103 if (!wxScrolledWindow::Create(parent, id, pos, size,
4f417130
VS
104 style | wxVSCROLL | wxHSCROLL, name))
105 return FALSE;
106
a547ebff 107 m_Style = style;
4f9297b0 108 SetPage(wxT("<html><body></body></html>"));
4f417130 109 return TRUE;
5526e819
VS
110}
111
112
5526e819
VS
113wxHtmlWindow::~wxHtmlWindow()
114{
115 HistoryClear();
116
117 if (m_Cell) delete m_Cell;
118
5526e819
VS
119 delete m_Parser;
120 delete m_FS;
892aeafc
VS
121 delete m_History;
122 delete m_Processors;
5526e819
VS
123}
124
125
126
127void wxHtmlWindow::SetRelatedFrame(wxFrame* frame, const wxString& format)
128{
129 m_RelatedFrame = frame;
130 m_TitleFormat = format;
131}
132
133
134
135void wxHtmlWindow::SetRelatedStatusBar(int bar)
136{
137 m_RelatedStatusBar = bar;
138}
269e8200
RD
139
140
141
8eb2940f 142void wxHtmlWindow::SetFonts(wxString normal_face, wxString fixed_face, const int *sizes)
5526e819 143{
d5db80c2
VS
144 wxString op = m_OpenedPage;
145
4f9297b0 146 m_Parser->SetFonts(normal_face, fixed_face, sizes);
892aeafc 147 // fonts changed => contents invalid, so reload the page:
bfb9ee96 148 SetPage(wxT("<html><body></body></html>"));
d5db80c2 149 if (!op.IsEmpty()) LoadPage(op);
5526e819
VS
150}
151
152
153
154bool wxHtmlWindow::SetPage(const wxString& source)
155{
892aeafc
VS
156 wxString newsrc(source);
157
adf2eb2d
VS
158 wxDELETE(m_selection);
159
892aeafc 160 // pass HTML through registered processors:
960ba969 161 if (m_Processors || m_GlobalProcessors)
892aeafc 162 {
960ba969
VS
163 wxHtmlProcessorList::Node *nodeL, *nodeG;
164 int prL, prG;
892aeafc
VS
165
166 nodeL = (m_Processors) ? m_Processors->GetFirst() : NULL;
960ba969
VS
167 nodeG = (m_GlobalProcessors) ? m_GlobalProcessors->GetFirst() : NULL;
168
169 // VS: there are two lists, global and local, both of them sorted by
e421922f 170 // priority. Since we have to go through _both_ lists with
960ba969
VS
171 // decreasing priority, we "merge-sort" the lists on-line by
172 // processing that one of the two heads that has higher priority
173 // in every iteration
174 while (nodeL || nodeG)
892aeafc
VS
175 {
176 prL = (nodeL) ? nodeL->GetData()->GetPriority() : -1;
960ba969
VS
177 prG = (nodeG) ? nodeG->GetData()->GetPriority() : -1;
178 if (prL > prG)
892aeafc 179 {
73348d09
VS
180 if (nodeL->GetData()->IsEnabled())
181 newsrc = nodeL->GetData()->Process(newsrc);
892aeafc
VS
182 nodeL = nodeL->GetNext();
183 }
960ba969 184 else // prL <= prG
892aeafc 185 {
73348d09
VS
186 if (nodeG->GetData()->IsEnabled())
187 newsrc = nodeG->GetData()->Process(newsrc);
960ba969 188 nodeG = nodeG->GetNext();
892aeafc
VS
189 }
190 }
191 }
5526e819 192
892aeafc
VS
193 // ...and run the parser on it:
194 wxClientDC *dc = new wxClientDC(this);
4f9297b0 195 dc->SetMapMode(wxMM_TEXT);
5526e819 196 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
d5db80c2 197 m_OpenedPage = m_OpenedAnchor = m_OpenedPageTitle = wxEmptyString;
4f9297b0 198 m_Parser->SetDC(dc);
33ac7e6f 199 if (m_Cell)
4f9297b0 200 {
89de9af3
VS
201 delete m_Cell;
202 m_Cell = NULL;
f61815af 203 }
892aeafc 204 m_Cell = (wxHtmlContainerCell*) m_Parser->Parse(newsrc);
5526e819 205 delete dc;
4f9297b0
VS
206 m_Cell->SetIndent(m_Borders, wxHTML_INDENT_ALL, wxHTML_UNITS_PIXELS);
207 m_Cell->SetAlignHor(wxHTML_ALIGN_CENTER);
5526e819 208 CreateLayout();
bfb9ee96 209 if (m_tmpCanDrawLocks == 0)
892aeafc 210 Refresh();
5526e819
VS
211 return TRUE;
212}
213
39029898
VS
214bool wxHtmlWindow::AppendToPage(const wxString& source)
215{
216 return SetPage(*(GetParser()->GetSource()) + source);
217}
5526e819
VS
218
219bool wxHtmlWindow::LoadPage(const wxString& location)
220{
1ccabb81 221 wxBusyCursor busyCursor;
790dbce3 222
5526e819
VS
223 wxFSFile *f;
224 bool rt_val;
fc7dfaf8 225 bool needs_refresh = FALSE;
33ac7e6f 226
89de9af3 227 m_tmpCanDrawLocks++;
e421922f 228 if (m_HistoryOn && (m_HistoryPos != -1))
4f9297b0 229 {
960ba969 230 // store scroll position into history item:
5526e819 231 int x, y;
e421922f 232 GetViewStart(&x, &y);
892aeafc 233 (*m_History)[m_HistoryPos].SetPos(y);
5526e819
VS
234 }
235
e421922f 236 if (location[0] == wxT('#'))
4f9297b0 237 {
960ba969 238 // local anchor:
5526e819 239 wxString anch = location.Mid(1) /*1 to end*/;
89de9af3 240 m_tmpCanDrawLocks--;
5526e819 241 rt_val = ScrollToAnchor(anch);
fc7dfaf8
VS
242 m_tmpCanDrawLocks++;
243 }
33ac7e6f 244 else if (location.Find(wxT('#')) != wxNOT_FOUND && location.BeforeFirst(wxT('#')) == m_OpenedPage)
4f9297b0 245 {
fc7dfaf8
VS
246 wxString anch = location.AfterFirst(wxT('#'));
247 m_tmpCanDrawLocks--;
248 rt_val = ScrollToAnchor(anch);
249 m_tmpCanDrawLocks++;
250 }
33ac7e6f
KB
251 else if (location.Find(wxT('#')) != wxNOT_FOUND &&
252 (m_FS->GetPath() + location.BeforeFirst(wxT('#'))) == m_OpenedPage)
e421922f 253 {
fc7dfaf8
VS
254 wxString anch = location.AfterFirst(wxT('#'));
255 m_tmpCanDrawLocks--;
256 rt_val = ScrollToAnchor(anch);
257 m_tmpCanDrawLocks++;
5526e819
VS
258 }
259
33ac7e6f 260 else
4f9297b0 261 {
fc7dfaf8 262 needs_refresh = TRUE;
5526e819 263 // load&display it:
33ac7e6f 264 if (m_RelatedStatusBar != -1)
e421922f 265 {
4f9297b0 266 m_RelatedFrame->SetStatusText(_("Connecting..."), m_RelatedStatusBar);
fc7dfaf8 267 Refresh(FALSE);
5526e819 268 }
790dbce3 269
6cc4e6b8 270 f = m_Parser->OpenURL(wxHTML_URL_PAGE, location);
33ac7e6f 271
7cb9cf89
VS
272 // try to interpret 'location' as filename instead of URL:
273 if (f == NULL)
274 {
275 wxFileName fn(location);
276 wxString location2 = wxFileSystem::FileNameToURL(fn);
277 f = m_Parser->OpenURL(wxHTML_URL_PAGE, location2);
278 }
279
33ac7e6f 280 if (f == NULL)
e421922f 281 {
f6bcfd97 282 wxLogError(_("Unable to open requested HTML document: %s"), location.c_str());
89de9af3 283 m_tmpCanDrawLocks--;
5526e819
VS
284 return FALSE;
285 }
286
33ac7e6f 287 else
e421922f 288 {
5526e819
VS
289 wxNode *node;
290 wxString src = wxEmptyString;
291
33ac7e6f 292 if (m_RelatedStatusBar != -1)
e421922f 293 {
5526e819 294 wxString msg = _("Loading : ") + location;
4f9297b0 295 m_RelatedFrame->SetStatusText(msg, m_RelatedStatusBar);
fc7dfaf8 296 Refresh(FALSE);
5526e819
VS
297 }
298
299 node = m_Filters.GetFirst();
4f9297b0 300 while (node)
e421922f 301 {
4f9297b0
VS
302 wxHtmlFilter *h = (wxHtmlFilter*) node->GetData();
303 if (h->CanRead(*f))
e421922f 304 {
4f9297b0 305 src = h->ReadFile(*f);
5526e819
VS
306 break;
307 }
4f9297b0 308 node = node->GetNext();
5526e819 309 }
33ac7e6f 310 if (src == wxEmptyString)
e421922f 311 {
89de9af3 312 if (m_DefaultFilter == NULL) m_DefaultFilter = GetDefaultFilter();
4f9297b0 313 src = m_DefaultFilter->ReadFile(*f);
89de9af3 314 }
5526e819 315
4f9297b0 316 m_FS->ChangePathTo(f->GetLocation());
5526e819 317 rt_val = SetPage(src);
4f9297b0 318 m_OpenedPage = f->GetLocation();
33ac7e6f 319 if (f->GetAnchor() != wxEmptyString)
e421922f 320 {
4f9297b0 321 ScrollToAnchor(f->GetAnchor());
5526e819
VS
322 }
323
324 delete f;
325
4f9297b0 326 if (m_RelatedStatusBar != -1) m_RelatedFrame->SetStatusText(_("Done"), m_RelatedStatusBar);
5526e819
VS
327 }
328 }
329
4f9297b0
VS
330 if (m_HistoryOn) // add this page to history there:
331 {
892aeafc 332 int c = m_History->GetCount() - (m_HistoryPos + 1);
5526e819 333
0cb9cfb2 334 if (m_HistoryPos < 0 ||
ee19c324
VS
335 (*m_History)[m_HistoryPos].GetPage() != m_OpenedPage ||
336 (*m_History)[m_HistoryPos].GetAnchor() != m_OpenedAnchor)
337 {
338 m_HistoryPos++;
339 for (int i = 0; i < c; i++)
b54e41c5 340 m_History->RemoveAt(m_HistoryPos);
ee19c324
VS
341 m_History->Add(new wxHtmlHistoryItem(m_OpenedPage, m_OpenedAnchor));
342 }
5526e819
VS
343 }
344
096824d7
VS
345 if (m_OpenedPageTitle == wxEmptyString)
346 OnSetTitle(wxFileNameFromPath(m_OpenedPage));
fc7dfaf8 347
33ac7e6f 348 if (needs_refresh)
4f9297b0 349 {
fc7dfaf8
VS
350 m_tmpCanDrawLocks--;
351 Refresh();
352 }
353 else
354 m_tmpCanDrawLocks--;
355
5526e819
VS
356 return rt_val;
357}
358
359
7cb9cf89
VS
360bool wxHtmlWindow::LoadFile(const wxFileName& filename)
361{
362 wxString url = wxFileSystem::FileNameToURL(filename);
363 return LoadPage(url);
364}
365
5526e819
VS
366
367bool wxHtmlWindow::ScrollToAnchor(const wxString& anchor)
368{
4f9297b0 369 const wxHtmlCell *c = m_Cell->Find(wxHTML_COND_ISANCHOR, &anchor);
f3c82859
VS
370 if (!c)
371 {
f6bcfd97 372 wxLogWarning(_("HTML anchor %s does not exist."), anchor.c_str());
f3c82859
VS
373 return FALSE;
374 }
33ac7e6f 375 else
4f9297b0 376 {
5526e819 377 int y;
269e8200 378
4f9297b0 379 for (y = 0; c != NULL; c = c->GetParent()) y += c->GetPosY();
efba2b89 380 Scroll(-1, y / wxHTML_SCROLL_STEP);
5526e819
VS
381 m_OpenedAnchor = anchor;
382 return TRUE;
383 }
384}
385
386
d5db80c2 387void wxHtmlWindow::OnSetTitle(const wxString& title)
5526e819 388{
33ac7e6f 389 if (m_RelatedFrame)
4f9297b0 390 {
5526e819
VS
391 wxString tit;
392 tit.Printf(m_TitleFormat, title.c_str());
4f9297b0 393 m_RelatedFrame->SetTitle(tit);
5526e819 394 }
d5db80c2 395 m_OpenedPageTitle = title;
5526e819
VS
396}
397
398
399
400
401
402void wxHtmlWindow::CreateLayout()
403{
404 int ClientWidth, ClientHeight;
405
406 if (!m_Cell) return;
a547ebff 407
33ac7e6f 408 if (m_Style & wxHW_SCROLLBAR_NEVER)
4f9297b0
VS
409 {
410 SetScrollbars(wxHTML_SCROLL_STEP, 1, m_Cell->GetWidth() / wxHTML_SCROLL_STEP, 0); // always off
a547ebff 411 GetClientSize(&ClientWidth, &ClientHeight);
4f9297b0 412 m_Cell->Layout(ClientWidth);
a547ebff
VS
413 }
414
415 else {
416 GetClientSize(&ClientWidth, &ClientHeight);
4f9297b0 417 m_Cell->Layout(ClientWidth);
33ac7e6f 418 if (ClientHeight < m_Cell->GetHeight() + GetCharHeight())
e421922f 419 {
f3bcfd9b
VS
420 SetScrollbars(
421 wxHTML_SCROLL_STEP, wxHTML_SCROLL_STEP,
4f9297b0
VS
422 m_Cell->GetWidth() / wxHTML_SCROLL_STEP,
423 (m_Cell->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
f3bcfd9b 424 /*cheat: top-level frag is always container*/);
a547ebff 425 }
4f9297b0 426 else /* we fit into window, no need for scrollbars */
e421922f 427 {
4f9297b0 428 SetScrollbars(wxHTML_SCROLL_STEP, 1, m_Cell->GetWidth() / wxHTML_SCROLL_STEP, 0); // disable...
89de9af3 429 GetClientSize(&ClientWidth, &ClientHeight);
4f9297b0 430 m_Cell->Layout(ClientWidth); // ...and relayout
89de9af3 431 }
a547ebff 432 }
5526e819
VS
433}
434
269e8200 435
5526e819
VS
436
437void wxHtmlWindow::ReadCustomization(wxConfigBase *cfg, wxString path)
438{
439 wxString oldpath;
440 wxString tmp;
d5db80c2
VS
441 int p_fontsizes[7];
442 wxString p_fff, p_ffn;
5526e819 443
33ac7e6f 444 if (path != wxEmptyString)
4f9297b0
VS
445 {
446 oldpath = cfg->GetPath();
447 cfg->SetPath(path);
5526e819
VS
448 }
449
892aeafc
VS
450 m_Borders = cfg->Read(wxT("wxHtmlWindow/Borders"), m_Borders);
451 p_fff = cfg->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser->m_FontFaceFixed);
452 p_ffn = cfg->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser->m_FontFaceNormal);
bfb9ee96 453 for (int i = 0; i < 7; i++)
4f9297b0 454 {
66a77a74 455 tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i);
4f9297b0 456 p_fontsizes[i] = cfg->Read(tmp, m_Parser->m_FontsSizes[i]);
5526e819 457 }
8eb2940f 458 SetFonts(p_ffn, p_fff, p_fontsizes);
5526e819
VS
459
460 if (path != wxEmptyString)
4f9297b0 461 cfg->SetPath(oldpath);
5526e819
VS
462}
463
464
465
466void wxHtmlWindow::WriteCustomization(wxConfigBase *cfg, wxString path)
467{
468 wxString oldpath;
469 wxString tmp;
470
33ac7e6f 471 if (path != wxEmptyString)
4f9297b0
VS
472 {
473 oldpath = cfg->GetPath();
474 cfg->SetPath(path);
5526e819
VS
475 }
476
892aeafc
VS
477 cfg->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders);
478 cfg->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser->m_FontFaceFixed);
479 cfg->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser->m_FontFaceNormal);
bfb9ee96 480 for (int i = 0; i < 7; i++)
4f9297b0 481 {
66a77a74 482 tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i);
4f9297b0 483 cfg->Write(tmp, (long) m_Parser->m_FontsSizes[i]);
5526e819
VS
484 }
485
486 if (path != wxEmptyString)
4f9297b0 487 cfg->SetPath(oldpath);
5526e819
VS
488}
489
490
491
492bool wxHtmlWindow::HistoryBack()
493{
494 wxString a, l;
495
496 if (m_HistoryPos < 1) return FALSE;
497
bbda1088
VS
498 // store scroll position into history item:
499 int x, y;
e421922f 500 GetViewStart(&x, &y);
892aeafc 501 (*m_History)[m_HistoryPos].SetPos(y);
bbda1088
VS
502
503 // go to previous position:
5526e819
VS
504 m_HistoryPos--;
505
892aeafc
VS
506 l = (*m_History)[m_HistoryPos].GetPage();
507 a = (*m_History)[m_HistoryPos].GetAnchor();
5526e819 508 m_HistoryOn = FALSE;
89de9af3 509 m_tmpCanDrawLocks++;
5526e819 510 if (a == wxEmptyString) LoadPage(l);
fc7dfaf8 511 else LoadPage(l + wxT("#") + a);
5526e819 512 m_HistoryOn = TRUE;
89de9af3 513 m_tmpCanDrawLocks--;
892aeafc 514 Scroll(0, (*m_History)[m_HistoryPos].GetPos());
5526e819
VS
515 Refresh();
516 return TRUE;
517}
518
1b113a81
VS
519bool wxHtmlWindow::HistoryCanBack()
520{
521 if (m_HistoryPos < 1) return FALSE;
522 return TRUE ;
523}
5526e819
VS
524
525
526bool wxHtmlWindow::HistoryForward()
527{
528 wxString a, l;
529
530 if (m_HistoryPos == -1) return FALSE;
892aeafc 531 if (m_HistoryPos >= (int)m_History->GetCount() - 1)return FALSE;
5526e819
VS
532
533 m_OpenedPage = wxEmptyString; // this will disable adding new entry into history in LoadPage()
534
535 m_HistoryPos++;
892aeafc
VS
536 l = (*m_History)[m_HistoryPos].GetPage();
537 a = (*m_History)[m_HistoryPos].GetAnchor();
5526e819 538 m_HistoryOn = FALSE;
89de9af3 539 m_tmpCanDrawLocks++;
5526e819 540 if (a == wxEmptyString) LoadPage(l);
fc7dfaf8 541 else LoadPage(l + wxT("#") + a);
5526e819 542 m_HistoryOn = TRUE;
89de9af3 543 m_tmpCanDrawLocks--;
892aeafc 544 Scroll(0, (*m_History)[m_HistoryPos].GetPos());
5526e819
VS
545 Refresh();
546 return TRUE;
547}
548
1b113a81
VS
549bool wxHtmlWindow::HistoryCanForward()
550{
551 if (m_HistoryPos == -1) return FALSE;
892aeafc 552 if (m_HistoryPos >= (int)m_History->GetCount() - 1)return FALSE;
1b113a81
VS
553 return TRUE ;
554}
5526e819
VS
555
556
557void wxHtmlWindow::HistoryClear()
558{
892aeafc 559 m_History->Empty();
5526e819
VS
560 m_HistoryPos = -1;
561}
562
892aeafc
VS
563void wxHtmlWindow::AddProcessor(wxHtmlProcessor *processor)
564{
565 if (!m_Processors)
566 {
567 m_Processors = new wxHtmlProcessorList;
568 m_Processors->DeleteContents(TRUE);
569 }
570 wxHtmlProcessorList::Node *node;
bfb9ee96 571
892aeafc
VS
572 for (node = m_Processors->GetFirst(); node; node = node->GetNext())
573 {
bfb9ee96 574 if (processor->GetPriority() > node->GetData()->GetPriority())
892aeafc
VS
575 {
576 m_Processors->Insert(node, processor);
960ba969 577 return;
892aeafc
VS
578 }
579 }
960ba969 580 m_Processors->Append(processor);
892aeafc
VS
581}
582
960ba969 583/*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor *processor)
892aeafc 584{
960ba969 585 if (!m_GlobalProcessors)
892aeafc 586 {
960ba969
VS
587 m_GlobalProcessors = new wxHtmlProcessorList;
588 m_GlobalProcessors->DeleteContents(TRUE);
892aeafc
VS
589 }
590 wxHtmlProcessorList::Node *node;
e421922f 591
960ba969 592 for (node = m_GlobalProcessors->GetFirst(); node; node = node->GetNext())
892aeafc 593 {
bfb9ee96 594 if (processor->GetPriority() > node->GetData()->GetPriority())
892aeafc 595 {
960ba969
VS
596 m_GlobalProcessors->Insert(node, processor);
597 return;
892aeafc
VS
598 }
599 }
960ba969 600 m_GlobalProcessors->Append(processor);
892aeafc
VS
601}
602
5526e819
VS
603
604
605wxList wxHtmlWindow::m_Filters;
a76015e6 606wxHtmlFilter *wxHtmlWindow::m_DefaultFilter = NULL;
66806a0b
VS
607wxCursor *wxHtmlWindow::s_cur_hand = NULL;
608wxCursor *wxHtmlWindow::s_cur_arrow = NULL;
960ba969 609wxHtmlProcessorList *wxHtmlWindow::m_GlobalProcessors = NULL;
a76015e6
VS
610
611void wxHtmlWindow::CleanUpStatics()
612{
eeb029a9 613 wxDELETE(m_DefaultFilter);
269e8200
RD
614 m_Filters.DeleteContents(TRUE);
615 m_Filters.Clear();
eeb029a9
VS
616 wxDELETE(m_GlobalProcessors);
617 wxDELETE(s_cur_hand);
618 wxDELETE(s_cur_arrow);
a76015e6
VS
619}
620
621
5526e819
VS
622
623void wxHtmlWindow::AddFilter(wxHtmlFilter *filter)
624{
5526e819
VS
625 m_Filters.Append(filter);
626}
627
628
36c4ff4d
VS
629bool wxHtmlWindow::IsSelectionEnabled() const
630{
631#if wxUSE_CLIPBOARD
632 return !(m_Style & wxHW_NO_SELECTION);
633#else
634 return false;
635#endif
636}
5526e819
VS
637
638
0b2dadd3 639void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo& link)
5526e819 640{
31d8b4ad
VS
641 const wxMouseEvent *e = link.GetEvent();
642 if (e == NULL || e->LeftUp())
643 LoadPage(link.GetHref());
5526e819
VS
644}
645
f6010d8f
VZ
646void wxHtmlWindow::OnCellClicked(wxHtmlCell *cell,
647 wxCoord x, wxCoord y,
648 const wxMouseEvent& event)
19193a2c 649{
f6010d8f 650 wxCHECK_RET( cell, _T("can't be called with NULL cell") );
5526e819 651
f6010d8f
VZ
652 cell->OnMouseClick(this, x, y, event);
653}
654
655void wxHtmlWindow::OnCellMouseHover(wxHtmlCell * WXUNUSED(cell),
656 wxCoord WXUNUSED(x), wxCoord WXUNUSED(y))
657{
658 // do nothing here
659}
5526e819
VS
660
661void wxHtmlWindow::OnDraw(wxDC& dc)
662{
b835e9bf 663 if (m_tmpCanDrawLocks > 0 || m_Cell == NULL) return;
5526e819 664
b835e9bf
VS
665 int x, y;
666 wxRect rect = GetUpdateRegion().GetBox();
33ac7e6f 667
5526e819 668 dc.SetMapMode(wxMM_TEXT);
5526e819 669 dc.SetBackgroundMode(wxTRANSPARENT);
e421922f 670 GetViewStart(&x, &y);
5526e819 671
adf2eb2d 672 wxHtmlRenderingState rstate(m_selection);
790dbce3
RD
673 m_Cell->Draw(dc, 0, 0,
674 y * wxHTML_SCROLL_STEP + rect.GetTop(),
36c4ff4d
VS
675 y * wxHTML_SCROLL_STEP + rect.GetBottom(),
676 rstate);
5526e819
VS
677}
678
679
680
681
682void wxHtmlWindow::OnSize(wxSizeEvent& event)
683{
684 wxScrolledWindow::OnSize(event);
685 CreateLayout();
f6bcfd97 686 Refresh();
5526e819
VS
687}
688
689
31d8b4ad 690void wxHtmlWindow::OnMouseMove(wxMouseEvent& event)
5526e819 691{
31d8b4ad
VS
692 m_tmpMouseMoved = true;
693}
5526e819 694
adf2eb2d 695void wxHtmlWindow::OnMouseDown(wxMouseEvent& event)
31d8b4ad 696{
adf2eb2d 697 if ( event.LeftDown() && IsSelectionEnabled() )
4f9297b0 698 {
adf2eb2d
VS
699 m_makingSelection = true;
700
701 if ( m_selection )
702 {
703 wxDELETE(m_selection);
704 Refresh();
705 }
706 m_tmpSelFromPos = CalcUnscrolledPosition(event.GetPosition());
707 m_tmpSelFromCell = NULL;
5526e819 708
adf2eb2d
VS
709 CaptureMouse();
710 }
711}
5526e819 712
adf2eb2d
VS
713void wxHtmlWindow::OnMouseUp(wxMouseEvent& event)
714{
715 if ( m_makingSelection )
716 {
717 ReleaseMouse();
718 m_makingSelection = false;
719
720 // did the user move the mouse far enough from starting point?
721 if ( m_selection )
722 {
723 // we don't want mouse up event that ended selecting to be
724 // handled as mouse click and e.g. follow hyperlink:
725 return;
726 }
727 }
728
729 SetFocus();
730 if ( m_Cell )
731 {
732 wxPoint pos = CalcUnscrolledPosition(event.GetPosition());
31d8b4ad 733 wxHtmlCell *cell = m_Cell->FindCellByPos(pos.x, pos.y);
f6010d8f 734
31d8b4ad
VS
735 // VZ: is it possible that we don't find anything at all?
736 // VS: yes. FindCellByPos returns terminal cell and
737 // containers may have empty borders
738 if ( cell )
739 OnCellClicked(cell, pos.x, pos.y, event);
5526e819
VS
740 }
741}
742
743
744
33ac7e6f 745void wxHtmlWindow::OnIdle(wxIdleEvent& WXUNUSED(event))
5526e819 746{
33ac7e6f 747 if (s_cur_hand == NULL)
66806a0b
VS
748 {
749 s_cur_hand = new wxCursor(wxCURSOR_HAND);
750 s_cur_arrow = new wxCursor(wxCURSOR_ARROW);
751 }
5526e819 752
33ac7e6f 753 if (m_tmpMouseMoved && (m_Cell != NULL))
4f9297b0 754 {
adf2eb2d
VS
755 int xc, yc, x, y;
756 wxGetMousePosition(&xc, &yc);
757 ScreenToClient(&xc, &yc);
758 CalcUnscrolledPosition(xc, yc, &x, &y);
0cb9cfb2 759
f6010d8f 760 wxHtmlCell *cell = m_Cell->FindCellByPos(x, y);
adf2eb2d
VS
761
762 // handle selection update:
763 if ( m_makingSelection )
764 {
765 bool goingDown = m_tmpSelFromPos.y < y ||
766 m_tmpSelFromPos.y == y && m_tmpSelFromPos.x < x;
767
768 if ( !m_tmpSelFromCell )
769 {
770 if (goingDown)
771 {
772 m_tmpSelFromCell = m_Cell->FindCellByPos(x, y,
773 wxHTML_FIND_NEAREST_BEFORE);
774 if (!m_tmpSelFromCell)
775 m_tmpSelFromCell = m_Cell->GetFirstTerminal();
776 }
777 else
778 {
779 m_tmpSelFromCell = m_Cell->FindCellByPos(x, y,
780 wxHTML_FIND_NEAREST_AFTER);
781 if (!m_tmpSelFromCell)
782 m_tmpSelFromCell = m_Cell->GetLastTerminal();
783 }
784 }
785
786 wxHtmlCell *selcell = cell;
787 if (!selcell)
788 {
789 if (goingDown)
790 {
791 selcell = m_Cell->FindCellByPos(x, y,
792 wxHTML_FIND_NEAREST_AFTER);
793 if (!selcell)
794 selcell = m_Cell->GetLastTerminal();
795 }
796 else
797 {
798 selcell = m_Cell->FindCellByPos(x, y,
799 wxHTML_FIND_NEAREST_BEFORE);
800 if (!selcell)
801 selcell = m_Cell->GetFirstTerminal();
802 }
803 }
804
805 // NB: it may *rarely* happen that the code above didn't find one
806 // of the cells, e.g. if wxHtmlWindow doesn't contain any
807 // visible cells.
808 if ( selcell && m_tmpSelFromCell )
809 {
810 if ( !m_selection )
811 {
812 // start selecting only if mouse movement was big enough
813 // (otherwise it was meant as mouse click, not selection):
814 const int PRECISION = 2;
815 wxPoint diff = m_tmpSelFromPos - wxPoint(x,y);
816 if (abs(diff.x) > PRECISION || abs(diff.y) > PRECISION)
817 {
818 m_selection = new wxHtmlSelection();
819 }
820 }
821 if ( m_selection )
822 {
823 if (goingDown)
824 {
825 m_selection->Set(m_tmpSelFromPos, m_tmpSelFromCell,
826 wxPoint(x,y), selcell);
827 }
828 else
829 {
830 m_selection->Set(wxPoint(x,y), selcell,
831 m_tmpSelFromPos, m_tmpSelFromCell);
832 }
833 Refresh(); // FIXME - optimize!
834 }
835 }
836 }
837
838 // handle cursor and status bar text changes:
f6010d8f 839 if ( cell != m_tmpLastCell )
e421922f 840 {
f6010d8f
VZ
841 wxHtmlLinkInfo *lnk = cell ? cell->GetLink(x, y) : NULL;
842
843 if (lnk != m_tmpLastLink)
e421922f 844 {
f6010d8f
VZ
845 if (lnk == NULL)
846 {
847 SetCursor(*s_cur_arrow);
848 if (m_RelatedStatusBar != -1)
849 m_RelatedFrame->SetStatusText(wxEmptyString, m_RelatedStatusBar);
850 }
851 else
852 {
853 SetCursor(*s_cur_hand);
854 if (m_RelatedStatusBar != -1)
855 m_RelatedFrame->SetStatusText(lnk->GetHref(), m_RelatedStatusBar);
856 }
857 m_tmpLastLink = lnk;
622ea783 858 }
f6010d8f
VZ
859
860 m_tmpLastCell = cell;
5526e819 861 }
f6010d8f
VZ
862 else // mouse moved but stayed in the same cell
863 {
864 if ( cell )
865 OnCellMouseHover(cell, x, y);
866 }
867
5526e819
VS
868 m_tmpMouseMoved = FALSE;
869 }
870}
871
872
bfb9ee96 873IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor,wxObject)
5526e819
VS
874
875IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow,wxScrolledWindow)
876
877BEGIN_EVENT_TABLE(wxHtmlWindow, wxScrolledWindow)
878 EVT_SIZE(wxHtmlWindow::OnSize)
adf2eb2d
VS
879 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseDown)
880 EVT_LEFT_UP(wxHtmlWindow::OnMouseUp)
881 EVT_RIGHT_UP(wxHtmlWindow::OnMouseUp)
31d8b4ad 882 EVT_MOTION(wxHtmlWindow::OnMouseMove)
5526e819 883 EVT_IDLE(wxHtmlWindow::OnIdle)
5526e819
VS
884END_EVENT_TABLE()
885
886
887
888
889
a76015e6
VS
890// A module to allow initialization/cleanup
891// without calling these functions from app.cpp or from
892// the user's application.
893
894class wxHtmlWinModule: public wxModule
895{
896DECLARE_DYNAMIC_CLASS(wxHtmlWinModule)
897public:
898 wxHtmlWinModule() : wxModule() {}
899 bool OnInit() { return TRUE; }
900 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
901};
902
903IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule, wxModule)
904
5526e819 905
0cecad31
VS
906// This hack forces the linker to always link in m_* files
907// (wxHTML doesn't work without handlers from these files)
908#include "wx/html/forcelnk.h"
909FORCE_WXHTML_MODULES()
5526e819 910
b835e9bf 911#endif