]> git.saurik.com Git - wxWidgets.git/blame - src/html/htmlwin.cpp
new rendering customization api fixes
[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
f30e67db
VS
645 wxClientDC dc(this);
646
e3774124
VS
647 const wxHtmlCell *end = m_selection->GetToCell();
648 wxString text;
649 wxHtmlTerminalCellsInterator i(m_selection->GetFromCell(), end);
650 if ( i )
651 {
652 text << i->ConvertToText(m_selection);
653 ++i;
654 }
655 const wxHtmlCell *prev = *i;
656 while ( i )
657 {
658 if ( prev->GetParent() != i->GetParent() )
659 text << _T('\n');
660 text << i->ConvertToText(*i == end ? m_selection : NULL);
661 prev = *i;
662 ++i;
663 }
664 return text;
665}
666
667void wxHtmlWindow::CopySelection(ClipboardType t)
668{
669#if wxUSE_CLIPBOARD
670 if ( m_selection )
671 {
672 wxTheClipboard->UsePrimarySelection(t == Primary);
673 wxString txt(SelectionToText());
674 if ( wxTheClipboard->Open() )
675 {
676 wxTheClipboard->SetData(new wxTextDataObject(txt));
677 wxTheClipboard->Close();
678 wxLogTrace(_T("wxhtmlselection"),
679 _("Copied to clipboard:\"%s\""), txt.c_str());
680 }
681 }
682#endif
683}
5526e819
VS
684
685
0b2dadd3 686void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo& link)
5526e819 687{
31d8b4ad
VS
688 const wxMouseEvent *e = link.GetEvent();
689 if (e == NULL || e->LeftUp())
690 LoadPage(link.GetHref());
5526e819
VS
691}
692
f6010d8f
VZ
693void wxHtmlWindow::OnCellClicked(wxHtmlCell *cell,
694 wxCoord x, wxCoord y,
695 const wxMouseEvent& event)
19193a2c 696{
f6010d8f 697 wxCHECK_RET( cell, _T("can't be called with NULL cell") );
5526e819 698
f6010d8f
VZ
699 cell->OnMouseClick(this, x, y, event);
700}
701
702void wxHtmlWindow::OnCellMouseHover(wxHtmlCell * WXUNUSED(cell),
703 wxCoord WXUNUSED(x), wxCoord WXUNUSED(y))
704{
705 // do nothing here
706}
5526e819
VS
707
708void wxHtmlWindow::OnDraw(wxDC& dc)
709{
b835e9bf 710 if (m_tmpCanDrawLocks > 0 || m_Cell == NULL) return;
5526e819 711
b835e9bf
VS
712 int x, y;
713 wxRect rect = GetUpdateRegion().GetBox();
33ac7e6f 714
5526e819 715 dc.SetMapMode(wxMM_TEXT);
5526e819 716 dc.SetBackgroundMode(wxTRANSPARENT);
e421922f 717 GetViewStart(&x, &y);
5526e819 718
f30e67db
VS
719 wxHtmlRenderingInfo rinfo;
720 wxDefaultHtmlRenderingStyle rstyle;
721 rinfo.SetSelection(m_selection);
722 rinfo.SetStyle(&rstyle);
790dbce3
RD
723 m_Cell->Draw(dc, 0, 0,
724 y * wxHTML_SCROLL_STEP + rect.GetTop(),
36c4ff4d 725 y * wxHTML_SCROLL_STEP + rect.GetBottom(),
f30e67db 726 rinfo);
5526e819
VS
727}
728
729
730
731
732void wxHtmlWindow::OnSize(wxSizeEvent& event)
733{
734 wxScrolledWindow::OnSize(event);
735 CreateLayout();
f6bcfd97 736 Refresh();
5526e819
VS
737}
738
739
31d8b4ad 740void wxHtmlWindow::OnMouseMove(wxMouseEvent& event)
5526e819 741{
31d8b4ad
VS
742 m_tmpMouseMoved = true;
743}
5526e819 744
adf2eb2d 745void wxHtmlWindow::OnMouseDown(wxMouseEvent& event)
31d8b4ad 746{
adf2eb2d 747 if ( event.LeftDown() && IsSelectionEnabled() )
4f9297b0 748 {
adf2eb2d
VS
749 m_makingSelection = true;
750
751 if ( m_selection )
752 {
753 wxDELETE(m_selection);
754 Refresh();
755 }
756 m_tmpSelFromPos = CalcUnscrolledPosition(event.GetPosition());
757 m_tmpSelFromCell = NULL;
5526e819 758
adf2eb2d
VS
759 CaptureMouse();
760 }
761}
5526e819 762
adf2eb2d
VS
763void wxHtmlWindow::OnMouseUp(wxMouseEvent& event)
764{
765 if ( m_makingSelection )
766 {
767 ReleaseMouse();
768 m_makingSelection = false;
769
770 // did the user move the mouse far enough from starting point?
771 if ( m_selection )
772 {
f30e67db
VS
773#ifdef __UNIX__
774 CopySelection(Primary);
775#endif
adf2eb2d
VS
776 // we don't want mouse up event that ended selecting to be
777 // handled as mouse click and e.g. follow hyperlink:
778 return;
779 }
780 }
781
782 SetFocus();
783 if ( m_Cell )
784 {
785 wxPoint pos = CalcUnscrolledPosition(event.GetPosition());
31d8b4ad 786 wxHtmlCell *cell = m_Cell->FindCellByPos(pos.x, pos.y);
f6010d8f 787
31d8b4ad
VS
788 // VZ: is it possible that we don't find anything at all?
789 // VS: yes. FindCellByPos returns terminal cell and
790 // containers may have empty borders
791 if ( cell )
792 OnCellClicked(cell, pos.x, pos.y, event);
5526e819
VS
793 }
794}
795
796
797
33ac7e6f 798void wxHtmlWindow::OnIdle(wxIdleEvent& WXUNUSED(event))
5526e819 799{
33ac7e6f 800 if (s_cur_hand == NULL)
66806a0b
VS
801 {
802 s_cur_hand = new wxCursor(wxCURSOR_HAND);
803 s_cur_arrow = new wxCursor(wxCURSOR_ARROW);
804 }
5526e819 805
33ac7e6f 806 if (m_tmpMouseMoved && (m_Cell != NULL))
4f9297b0 807 {
adf2eb2d
VS
808 int xc, yc, x, y;
809 wxGetMousePosition(&xc, &yc);
810 ScreenToClient(&xc, &yc);
811 CalcUnscrolledPosition(xc, yc, &x, &y);
0cb9cfb2 812
f6010d8f 813 wxHtmlCell *cell = m_Cell->FindCellByPos(x, y);
adf2eb2d
VS
814
815 // handle selection update:
816 if ( m_makingSelection )
817 {
818 bool goingDown = m_tmpSelFromPos.y < y ||
819 m_tmpSelFromPos.y == y && m_tmpSelFromPos.x < x;
820
821 if ( !m_tmpSelFromCell )
822 {
823 if (goingDown)
824 {
5a1597e9
VS
825 m_tmpSelFromCell = m_Cell->FindCellByPos(
826 m_tmpSelFromPos.x,m_tmpSelFromPos.y,
827 wxHTML_FIND_NEAREST_AFTER);
adf2eb2d
VS
828 if (!m_tmpSelFromCell)
829 m_tmpSelFromCell = m_Cell->GetFirstTerminal();
830 }
831 else
832 {
5a1597e9
VS
833 m_tmpSelFromCell = m_Cell->FindCellByPos(
834 m_tmpSelFromPos.x,m_tmpSelFromPos.y,
835 wxHTML_FIND_NEAREST_BEFORE);
adf2eb2d
VS
836 if (!m_tmpSelFromCell)
837 m_tmpSelFromCell = m_Cell->GetLastTerminal();
838 }
839 }
840
841 wxHtmlCell *selcell = cell;
842 if (!selcell)
843 {
844 if (goingDown)
845 {
846 selcell = m_Cell->FindCellByPos(x, y,
847 wxHTML_FIND_NEAREST_AFTER);
848 if (!selcell)
849 selcell = m_Cell->GetLastTerminal();
850 }
851 else
852 {
853 selcell = m_Cell->FindCellByPos(x, y,
854 wxHTML_FIND_NEAREST_BEFORE);
855 if (!selcell)
856 selcell = m_Cell->GetFirstTerminal();
857 }
858 }
859
860 // NB: it may *rarely* happen that the code above didn't find one
861 // of the cells, e.g. if wxHtmlWindow doesn't contain any
862 // visible cells.
863 if ( selcell && m_tmpSelFromCell )
864 {
865 if ( !m_selection )
866 {
867 // start selecting only if mouse movement was big enough
868 // (otherwise it was meant as mouse click, not selection):
869 const int PRECISION = 2;
870 wxPoint diff = m_tmpSelFromPos - wxPoint(x,y);
871 if (abs(diff.x) > PRECISION || abs(diff.y) > PRECISION)
872 {
873 m_selection = new wxHtmlSelection();
874 }
875 }
876 if ( m_selection )
877 {
e3774124 878 if ( m_tmpSelFromCell->IsBefore(selcell) )
adf2eb2d
VS
879 {
880 m_selection->Set(m_tmpSelFromPos, m_tmpSelFromCell,
f30e67db 881 wxPoint(x,y), selcell); }
adf2eb2d
VS
882 else
883 {
884 m_selection->Set(wxPoint(x,y), selcell,
885 m_tmpSelFromPos, m_tmpSelFromCell);
886 }
f30e67db
VS
887 {
888 wxClientDC dc(this);
889 m_selection->GetFromCell()->SetSelectionPrivPos(
890 dc, m_selection);
891 m_selection->GetToCell()->SetSelectionPrivPos(
892 dc, m_selection);
893 }
adf2eb2d
VS
894 Refresh(); // FIXME - optimize!
895 }
896 }
897 }
898
899 // handle cursor and status bar text changes:
f6010d8f 900 if ( cell != m_tmpLastCell )
e421922f 901 {
f6010d8f
VZ
902 wxHtmlLinkInfo *lnk = cell ? cell->GetLink(x, y) : NULL;
903
904 if (lnk != m_tmpLastLink)
e421922f 905 {
f6010d8f
VZ
906 if (lnk == NULL)
907 {
908 SetCursor(*s_cur_arrow);
909 if (m_RelatedStatusBar != -1)
910 m_RelatedFrame->SetStatusText(wxEmptyString, m_RelatedStatusBar);
911 }
912 else
913 {
914 SetCursor(*s_cur_hand);
915 if (m_RelatedStatusBar != -1)
916 m_RelatedFrame->SetStatusText(lnk->GetHref(), m_RelatedStatusBar);
917 }
918 m_tmpLastLink = lnk;
622ea783 919 }
f6010d8f
VZ
920
921 m_tmpLastCell = cell;
5526e819 922 }
f6010d8f
VZ
923 else // mouse moved but stayed in the same cell
924 {
925 if ( cell )
926 OnCellMouseHover(cell, x, y);
927 }
928
5526e819
VS
929 m_tmpMouseMoved = FALSE;
930 }
931}
932
e3774124
VS
933#if wxUSE_CLIPBOARD
934void wxHtmlWindow::OnKeyUp(wxKeyEvent& event)
935{
936 if ( event.GetKeyCode() == 'C' && event.ControlDown() )
937 {
938 if ( m_selection )
939 CopySelection();
940 }
941 else
942 event.Skip();
943}
944
945void wxHtmlWindow::OnCopy(wxCommandEvent& event)
946{
947 if ( m_selection )
948 CopySelection();
949}
950#endif
951
952
5526e819 953
bfb9ee96 954IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor,wxObject)
5526e819
VS
955
956IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow,wxScrolledWindow)
957
958BEGIN_EVENT_TABLE(wxHtmlWindow, wxScrolledWindow)
959 EVT_SIZE(wxHtmlWindow::OnSize)
adf2eb2d
VS
960 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseDown)
961 EVT_LEFT_UP(wxHtmlWindow::OnMouseUp)
962 EVT_RIGHT_UP(wxHtmlWindow::OnMouseUp)
31d8b4ad 963 EVT_MOTION(wxHtmlWindow::OnMouseMove)
5526e819 964 EVT_IDLE(wxHtmlWindow::OnIdle)
e3774124
VS
965#if wxUSE_CLIPBOARD
966 EVT_KEY_UP(wxHtmlWindow::OnKeyUp)
967 EVT_MENU(wxID_COPY, wxHtmlWindow::OnCopy)
968#endif
5526e819
VS
969END_EVENT_TABLE()
970
971
972
973
974
a76015e6
VS
975// A module to allow initialization/cleanup
976// without calling these functions from app.cpp or from
977// the user's application.
978
979class wxHtmlWinModule: public wxModule
980{
981DECLARE_DYNAMIC_CLASS(wxHtmlWinModule)
982public:
983 wxHtmlWinModule() : wxModule() {}
984 bool OnInit() { return TRUE; }
985 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
986};
987
988IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule, wxModule)
989
5526e819 990
0cecad31
VS
991// This hack forces the linker to always link in m_* files
992// (wxHTML doesn't work without handlers from these files)
993#include "wx/html/forcelnk.h"
994FORCE_WXHTML_MODULES()
5526e819 995
b835e9bf 996#endif