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