]> git.saurik.com Git - wxWidgets.git/blame - src/html/htmlwin.cpp
correction to last commit: don't test unsetenv() return value, it's void under Darwin
[wxWidgets.git] / src / html / htmlwin.cpp
CommitLineData
5526e819 1/////////////////////////////////////////////////////////////////////////////
93763ad5 2// Name: src/html/htmlwin.cpp
5526e819
VS
3// Purpose: wxHtmlWindow class for parsing & displaying HTML (implementation)
4// Author: Vaclav Slavik
69941f05 5// RCS-ID: $Id$
5526e819 6// Copyright: (c) 1999 Vaclav Slavik
65571936 7// Licence: wxWindows licence
5526e819
VS
8/////////////////////////////////////////////////////////////////////////////
9
3096bd2f 10#include "wx/wxprec.h"
5526e819 11
2b5f62a0 12#ifdef __BORLANDC__
93763ad5 13 #pragma hdrstop
5526e819
VS
14#endif
15
93763ad5
WS
16#if wxUSE_HTML && wxUSE_STREAMS
17
b4f4d3dd 18#ifndef WX_PRECOMP
8ecff181 19 #include "wx/list.h"
04dbb646
VZ
20 #include "wx/log.h"
21 #include "wx/intl.h"
22 #include "wx/dcclient.h"
23 #include "wx/frame.h"
f38924e8 24 #include "wx/dcmemory.h"
c0badb70 25 #include "wx/timer.h"
9eddec69 26 #include "wx/settings.h"
28f92d74 27 #include "wx/dataobj.h"
5526e819
VS
28#endif
29
69941f05 30#include "wx/html/htmlwin.h"
892aeafc 31#include "wx/html/htmlproc.h"
e3774124 32#include "wx/clipbrd.h"
04dbb646
VZ
33
34#include "wx/arrimpl.cpp"
892aeafc
VS
35#include "wx/listimpl.cpp"
36
a1c3cdc4
VS
37// HTML events:
38IMPLEMENT_DYNAMIC_CLASS(wxHtmlLinkEvent, wxCommandEvent)
39IMPLEMENT_DYNAMIC_CLASS(wxHtmlCellEvent, wxCommandEvent)
40
41DEFINE_EVENT_TYPE(wxEVT_COMMAND_HTML_CELL_CLICKED)
42DEFINE_EVENT_TYPE(wxEVT_COMMAND_HTML_CELL_HOVER)
43DEFINE_EVENT_TYPE(wxEVT_COMMAND_HTML_LINK_CLICKED)
44
1338c59a 45
1338c59a
VS
46#if wxUSE_CLIPBOARD
47// ----------------------------------------------------------------------------
48// wxHtmlWinAutoScrollTimer: the timer used to generate a stream of scroll
49// events when a captured mouse is held outside the window
50// ----------------------------------------------------------------------------
51
52class wxHtmlWinAutoScrollTimer : public wxTimer
53{
54public:
55 wxHtmlWinAutoScrollTimer(wxScrolledWindow *win,
56 wxEventType eventTypeToSend,
57 int pos, int orient)
58 {
59 m_win = win;
60 m_eventType = eventTypeToSend;
61 m_pos = pos;
62 m_orient = orient;
63 }
64
65 virtual void Notify();
66
67private:
68 wxScrolledWindow *m_win;
69 wxEventType m_eventType;
70 int m_pos,
71 m_orient;
72
73 DECLARE_NO_COPY_CLASS(wxHtmlWinAutoScrollTimer)
74};
75
76void wxHtmlWinAutoScrollTimer::Notify()
77{
78 // only do all this as long as the window is capturing the mouse
79 if ( wxWindow::GetCapture() != m_win )
80 {
81 Stop();
82 }
83 else // we still capture the mouse, continue generating events
84 {
85 // first scroll the window if we are allowed to do it
86 wxScrollWinEvent event1(m_eventType, m_pos, m_orient);
87 event1.SetEventObject(m_win);
88 if ( m_win->GetEventHandler()->ProcessEvent(event1) )
89 {
90 // and then send a pseudo mouse-move event to refresh the selection
91 wxMouseEvent event2(wxEVT_MOTION);
92 wxGetMousePosition(&event2.m_x, &event2.m_y);
93
94 // the mouse event coordinates should be client, not screen as
95 // returned by wxGetMousePosition
96 wxWindow *parentTop = m_win;
97 while ( parentTop->GetParent() )
98 parentTop = parentTop->GetParent();
99 wxPoint ptOrig = parentTop->GetPosition();
100 event2.m_x -= ptOrig.x;
101 event2.m_y -= ptOrig.y;
102
103 event2.SetEventObject(m_win);
104
105 // FIXME: we don't fill in the other members - ok?
106 m_win->GetEventHandler()->ProcessEvent(event2);
107 }
108 else // can't scroll further, stop
109 {
110 Stop();
111 }
112 }
113}
d659d703
VZ
114
115#endif // wxUSE_CLIPBOARD
1338c59a
VS
116
117
118
892aeafc
VS
119//-----------------------------------------------------------------------------
120// wxHtmlHistoryItem
121//-----------------------------------------------------------------------------
122
123// item of history list
4460b6c4 124class WXDLLIMPEXP_HTML wxHtmlHistoryItem
892aeafc
VS
125{
126public:
127 wxHtmlHistoryItem(const wxString& p, const wxString& a) {m_Page = p, m_Anchor = a, m_Pos = 0;}
128 int GetPos() const {return m_Pos;}
129 void SetPos(int p) {m_Pos = p;}
130 const wxString& GetPage() const {return m_Page;}
131 const wxString& GetAnchor() const {return m_Anchor;}
132
133private:
134 wxString m_Page;
135 wxString m_Anchor;
136 int m_Pos;
137};
5526e819 138
5526e819
VS
139
140//-----------------------------------------------------------------------------
892aeafc 141// our private arrays:
5526e819
VS
142//-----------------------------------------------------------------------------
143
892aeafc 144WX_DECLARE_OBJARRAY(wxHtmlHistoryItem, wxHtmlHistoryArray);
17a1ebd1 145WX_DEFINE_OBJARRAY(wxHtmlHistoryArray)
5526e819 146
892aeafc 147WX_DECLARE_LIST(wxHtmlProcessor, wxHtmlProcessorList);
17a1ebd1 148WX_DEFINE_LIST(wxHtmlProcessorList)
5526e819 149
bc55e31b
VS
150//-----------------------------------------------------------------------------
151// wxHtmlWindowMouseHelper
152//-----------------------------------------------------------------------------
153
154wxHtmlWindowMouseHelper::wxHtmlWindowMouseHelper(wxHtmlWindowInterface *iface)
155 : m_tmpMouseMoved(false),
156 m_tmpLastLink(NULL),
157 m_tmpLastCell(NULL),
158 m_interface(iface)
159{
160}
161
162void wxHtmlWindowMouseHelper::HandleMouseMoved()
163{
164 m_tmpMouseMoved = true;
165}
166
167bool wxHtmlWindowMouseHelper::HandleMouseClick(wxHtmlCell *rootCell,
168 const wxPoint& pos,
169 const wxMouseEvent& event)
170{
171 if (!rootCell)
172 return false;
173
174 wxHtmlCell *cell = rootCell->FindCellByPos(pos.x, pos.y);
175 // this check is needed because FindCellByPos returns terminal cell and
176 // containers may have empty borders -- in this case NULL will be
177 // returned
178 if (!cell)
179 return false;
180
181 // adjust the coordinates to be relative to this cell:
182 wxPoint relpos = pos - cell->GetAbsPos(rootCell);
183
184 return OnCellClicked(cell, relpos.x, relpos.y, event);
185}
186
187void wxHtmlWindowMouseHelper::HandleIdle(wxHtmlCell *rootCell,
188 const wxPoint& pos)
189{
190 wxHtmlCell *cell = rootCell ? rootCell->FindCellByPos(pos.x, pos.y) : NULL;
191
192 if (cell != m_tmpLastCell)
193 {
194 wxHtmlLinkInfo *lnk = NULL;
195 if (cell)
196 {
197 // adjust the coordinates to be relative to this cell:
198 wxPoint relpos = pos - cell->GetAbsPos(rootCell);
199 lnk = cell->GetLink(relpos.x, relpos.y);
200 }
201
202 wxCursor cur;
203 if (cell)
88a1b648 204 cur = cell->GetMouseCursor(m_interface);
bc55e31b 205 else
88a1b648
VS
206 cur = m_interface->GetHTMLCursor(
207 wxHtmlWindowInterface::HTMLCursor_Default);
208
bc55e31b
VS
209 m_interface->GetHTMLWindow()->SetCursor(cur);
210
211 if (lnk != m_tmpLastLink)
212 {
213 if (lnk)
214 m_interface->SetHTMLStatusText(lnk->GetHref());
215 else
216 m_interface->SetHTMLStatusText(wxEmptyString);
217
218 m_tmpLastLink = lnk;
219 }
220
221 m_tmpLastCell = cell;
222 }
223 else // mouse moved but stayed in the same cell
224 {
225 if ( cell )
226 {
227 OnCellMouseHover(cell, pos.x, pos.y);
228 }
229 }
230
231 m_tmpMouseMoved = false;
232}
233
234bool wxHtmlWindowMouseHelper::OnCellClicked(wxHtmlCell *cell,
235 wxCoord x, wxCoord y,
236 const wxMouseEvent& event)
237{
a1c3cdc4
VS
238 wxHtmlCellEvent ev(wxEVT_COMMAND_HTML_CELL_CLICKED,
239 m_interface->GetHTMLWindow()->GetId(),
240 cell, wxPoint(x,y), event);
241
242 if (!m_interface->GetHTMLWindow()->GetEventHandler()->ProcessEvent(ev))
243 {
244 // if the event wasn't handled, do the default processing here:
245
246 wxASSERT_MSG( cell, _T("can't be called with NULL cell") );
247
248 cell->ProcessMouseClick(m_interface, ev.GetPoint(), ev.GetMouseEvent());
249 }
bc55e31b 250
a1c3cdc4
VS
251 // true if a link was clicked, false otherwise
252 return ev.GetLinkClicked();
bc55e31b
VS
253}
254
a1c3cdc4
VS
255void wxHtmlWindowMouseHelper::OnCellMouseHover(wxHtmlCell * cell,
256 wxCoord x,
257 wxCoord y)
bc55e31b 258{
a1c3cdc4
VS
259 wxHtmlCellEvent ev(wxEVT_COMMAND_HTML_CELL_HOVER,
260 m_interface->GetHTMLWindow()->GetId(),
261 cell, wxPoint(x,y), wxMouseEvent());
262 m_interface->GetHTMLWindow()->GetEventHandler()->ProcessEvent(ev);
bc55e31b
VS
263}
264
a1c3cdc4
VS
265
266
267
892aeafc
VS
268//-----------------------------------------------------------------------------
269// wxHtmlWindow
270//-----------------------------------------------------------------------------
5526e819 271
88a1b648
VS
272wxList wxHtmlWindow::m_Filters;
273wxHtmlFilter *wxHtmlWindow::m_DefaultFilter = NULL;
274wxHtmlProcessorList *wxHtmlWindow::m_GlobalProcessors = NULL;
275wxCursor *wxHtmlWindow::ms_cursorLink = NULL;
276wxCursor *wxHtmlWindow::ms_cursorText = NULL;
277
278void wxHtmlWindow::CleanUpStatics()
279{
280 wxDELETE(m_DefaultFilter);
281 WX_CLEAR_LIST(wxList, m_Filters);
282 if (m_GlobalProcessors)
283 WX_CLEAR_LIST(wxHtmlProcessorList, *m_GlobalProcessors);
284 wxDELETE(m_GlobalProcessors);
285 wxDELETE(ms_cursorLink);
286 wxDELETE(ms_cursorText);
287}
5526e819 288
4f417130 289void wxHtmlWindow::Init()
5526e819 290{
89de9af3 291 m_tmpCanDrawLocks = 0;
5526e819 292 m_FS = new wxFileSystem();
67a99992 293#if wxUSE_STATUSBAR
5526e819 294 m_RelatedStatusBar = -1;
67a99992 295#endif // wxUSE_STATUSBAR
5526e819 296 m_RelatedFrame = NULL;
892aeafc 297 m_TitleFormat = wxT("%s");
d5db80c2 298 m_OpenedPage = m_OpenedAnchor = m_OpenedPageTitle = wxEmptyString;
5526e819
VS
299 m_Cell = NULL;
300 m_Parser = new wxHtmlWinParser(this);
4f9297b0 301 m_Parser->SetFS(m_FS);
5526e819 302 m_HistoryPos = -1;
d1da8872 303 m_HistoryOn = true;
892aeafc
VS
304 m_History = new wxHtmlHistoryArray;
305 m_Processors = NULL;
4f417130 306 SetBorders(10);
adf2eb2d
VS
307 m_selection = NULL;
308 m_makingSelection = false;
1338c59a
VS
309#if wxUSE_CLIPBOARD
310 m_timerAutoScroll = NULL;
6ce51985 311 m_lastDoubleClick = 0;
d659d703 312#endif // wxUSE_CLIPBOARD
1338c59a 313 m_backBuffer = NULL;
518ba663 314 m_eraseBgInOnPaint = false;
b3c03420 315 m_tmpSelFromCell = NULL;
4f417130
VS
316}
317
790dbce3 318bool wxHtmlWindow::Create(wxWindow *parent, wxWindowID id,
4f417130 319 const wxPoint& pos, const wxSize& size,
790dbce3 320 long style, const wxString& name)
4f417130 321{
790dbce3 322 if (!wxScrolledWindow::Create(parent, id, pos, size,
2a536376
VS
323 style | wxVSCROLL | wxHSCROLL,
324 name))
d1da8872 325 return false;
4f417130 326
4f9297b0 327 SetPage(wxT("<html><body></body></html>"));
d1da8872 328 return true;
5526e819
VS
329}
330
331
5526e819
VS
332wxHtmlWindow::~wxHtmlWindow()
333{
1338c59a
VS
334#if wxUSE_CLIPBOARD
335 StopAutoScrolling();
d659d703 336#endif // wxUSE_CLIPBOARD
5526e819
VS
337 HistoryClear();
338
689c4829
VZ
339 delete m_selection;
340
341 delete m_Cell;
5526e819 342
d80096a2
VZ
343 if ( m_Processors )
344 {
345 WX_CLEAR_LIST(wxHtmlProcessorList, *m_Processors);
346 }
222ed1d6 347
5526e819
VS
348 delete m_Parser;
349 delete m_FS;
892aeafc
VS
350 delete m_History;
351 delete m_Processors;
1338c59a 352 delete m_backBuffer;
5526e819
VS
353}
354
355
356
357void wxHtmlWindow::SetRelatedFrame(wxFrame* frame, const wxString& format)
358{
359 m_RelatedFrame = frame;
360 m_TitleFormat = format;
361}
362
363
364
67a99992 365#if wxUSE_STATUSBAR
5526e819
VS
366void wxHtmlWindow::SetRelatedStatusBar(int bar)
367{
368 m_RelatedStatusBar = bar;
369}
67a99992 370#endif // wxUSE_STATUSBAR
269e8200
RD
371
372
373
fbfb8bcc 374void wxHtmlWindow::SetFonts(const wxString& normal_face, const wxString& fixed_face, const int *sizes)
5526e819 375{
4f9297b0 376 m_Parser->SetFonts(normal_face, fixed_face, sizes);
73de5077
VS
377
378 // re-layout the page after changing fonts:
379 DoSetPage(*(m_Parser->GetSource()));
5526e819
VS
380}
381
10e5c7ea
VS
382void wxHtmlWindow::SetStandardFonts(int size,
383 const wxString& normal_face,
384 const wxString& fixed_face)
7acd3625 385{
10e5c7ea 386 m_Parser->SetStandardFonts(size, normal_face, fixed_face);
5526e819 387
73de5077
VS
388 // re-layout the page after changing fonts:
389 DoSetPage(*(m_Parser->GetSource()));
390}
5526e819
VS
391
392bool wxHtmlWindow::SetPage(const wxString& source)
73de5077
VS
393{
394 m_OpenedPage = m_OpenedAnchor = m_OpenedPageTitle = wxEmptyString;
395 return DoSetPage(source);
396}
397
398bool wxHtmlWindow::DoSetPage(const wxString& source)
5526e819 399{
892aeafc
VS
400 wxString newsrc(source);
401
adf2eb2d
VS
402 wxDELETE(m_selection);
403
b3c03420
VS
404 // we will soon delete all the cells, so clear pointers to them:
405 m_tmpSelFromCell = NULL;
406
892aeafc 407 // pass HTML through registered processors:
960ba969 408 if (m_Processors || m_GlobalProcessors)
892aeafc 409 {
222ed1d6 410 wxHtmlProcessorList::compatibility_iterator nodeL, nodeG;
960ba969 411 int prL, prG;
892aeafc 412
0cc70962
VZ
413 if ( m_Processors )
414 nodeL = m_Processors->GetFirst();
415 if ( m_GlobalProcessors )
416 nodeG = m_GlobalProcessors->GetFirst();
960ba969
VS
417
418 // VS: there are two lists, global and local, both of them sorted by
e421922f 419 // priority. Since we have to go through _both_ lists with
960ba969
VS
420 // decreasing priority, we "merge-sort" the lists on-line by
421 // processing that one of the two heads that has higher priority
422 // in every iteration
423 while (nodeL || nodeG)
892aeafc
VS
424 {
425 prL = (nodeL) ? nodeL->GetData()->GetPriority() : -1;
960ba969
VS
426 prG = (nodeG) ? nodeG->GetData()->GetPriority() : -1;
427 if (prL > prG)
892aeafc 428 {
73348d09
VS
429 if (nodeL->GetData()->IsEnabled())
430 newsrc = nodeL->GetData()->Process(newsrc);
892aeafc
VS
431 nodeL = nodeL->GetNext();
432 }
960ba969 433 else // prL <= prG
892aeafc 434 {
73348d09
VS
435 if (nodeG->GetData()->IsEnabled())
436 newsrc = nodeG->GetData()->Process(newsrc);
960ba969 437 nodeG = nodeG->GetNext();
892aeafc
VS
438 }
439 }
440 }
5526e819 441
892aeafc
VS
442 // ...and run the parser on it:
443 wxClientDC *dc = new wxClientDC(this);
4f9297b0 444 dc->SetMapMode(wxMM_TEXT);
5526e819 445 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
97e490f8 446 SetBackgroundImage(wxNullBitmap);
73de5077 447
4f9297b0 448 m_Parser->SetDC(dc);
33ac7e6f 449 if (m_Cell)
4f9297b0 450 {
89de9af3
VS
451 delete m_Cell;
452 m_Cell = NULL;
f61815af 453 }
892aeafc 454 m_Cell = (wxHtmlContainerCell*) m_Parser->Parse(newsrc);
5526e819 455 delete dc;
4f9297b0
VS
456 m_Cell->SetIndent(m_Borders, wxHTML_INDENT_ALL, wxHTML_UNITS_PIXELS);
457 m_Cell->SetAlignHor(wxHTML_ALIGN_CENTER);
5526e819 458 CreateLayout();
bfb9ee96 459 if (m_tmpCanDrawLocks == 0)
892aeafc 460 Refresh();
d1da8872 461 return true;
5526e819
VS
462}
463
39029898
VS
464bool wxHtmlWindow::AppendToPage(const wxString& source)
465{
73de5077 466 return DoSetPage(*(GetParser()->GetSource()) + source);
39029898 467}
5526e819
VS
468
469bool wxHtmlWindow::LoadPage(const wxString& location)
470{
1ccabb81 471 wxBusyCursor busyCursor;
790dbce3 472
5526e819
VS
473 wxFSFile *f;
474 bool rt_val;
d1da8872 475 bool needs_refresh = false;
33ac7e6f 476
89de9af3 477 m_tmpCanDrawLocks++;
e421922f 478 if (m_HistoryOn && (m_HistoryPos != -1))
4f9297b0 479 {
960ba969 480 // store scroll position into history item:
5526e819 481 int x, y;
e421922f 482 GetViewStart(&x, &y);
892aeafc 483 (*m_History)[m_HistoryPos].SetPos(y);
5526e819
VS
484 }
485
e421922f 486 if (location[0] == wxT('#'))
4f9297b0 487 {
960ba969 488 // local anchor:
5526e819 489 wxString anch = location.Mid(1) /*1 to end*/;
89de9af3 490 m_tmpCanDrawLocks--;
5526e819 491 rt_val = ScrollToAnchor(anch);
fc7dfaf8
VS
492 m_tmpCanDrawLocks++;
493 }
33ac7e6f 494 else if (location.Find(wxT('#')) != wxNOT_FOUND && location.BeforeFirst(wxT('#')) == m_OpenedPage)
4f9297b0 495 {
fc7dfaf8
VS
496 wxString anch = location.AfterFirst(wxT('#'));
497 m_tmpCanDrawLocks--;
498 rt_val = ScrollToAnchor(anch);
499 m_tmpCanDrawLocks++;
500 }
33ac7e6f
KB
501 else if (location.Find(wxT('#')) != wxNOT_FOUND &&
502 (m_FS->GetPath() + location.BeforeFirst(wxT('#'))) == m_OpenedPage)
e421922f 503 {
fc7dfaf8
VS
504 wxString anch = location.AfterFirst(wxT('#'));
505 m_tmpCanDrawLocks--;
506 rt_val = ScrollToAnchor(anch);
507 m_tmpCanDrawLocks++;
5526e819
VS
508 }
509
33ac7e6f 510 else
4f9297b0 511 {
67a99992
WS
512 needs_refresh = true;
513#if wxUSE_STATUSBAR
5526e819 514 // load&display it:
33ac7e6f 515 if (m_RelatedStatusBar != -1)
e421922f 516 {
4f9297b0 517 m_RelatedFrame->SetStatusText(_("Connecting..."), m_RelatedStatusBar);
67a99992 518 Refresh(false);
5526e819 519 }
67a99992 520#endif // wxUSE_STATUSBAR
790dbce3 521
6cc4e6b8 522 f = m_Parser->OpenURL(wxHTML_URL_PAGE, location);
33ac7e6f 523
7cb9cf89
VS
524 // try to interpret 'location' as filename instead of URL:
525 if (f == NULL)
526 {
527 wxFileName fn(location);
528 wxString location2 = wxFileSystem::FileNameToURL(fn);
529 f = m_Parser->OpenURL(wxHTML_URL_PAGE, location2);
530 }
531
33ac7e6f 532 if (f == NULL)
e421922f 533 {
f6bcfd97 534 wxLogError(_("Unable to open requested HTML document: %s"), location.c_str());
89de9af3 535 m_tmpCanDrawLocks--;
67a99992 536 return false;
5526e819
VS
537 }
538
33ac7e6f 539 else
e421922f 540 {
222ed1d6 541 wxList::compatibility_iterator node;
5526e819
VS
542 wxString src = wxEmptyString;
543
67a99992 544#if wxUSE_STATUSBAR
33ac7e6f 545 if (m_RelatedStatusBar != -1)
e421922f 546 {
5526e819 547 wxString msg = _("Loading : ") + location;
4f9297b0 548 m_RelatedFrame->SetStatusText(msg, m_RelatedStatusBar);
67a99992 549 Refresh(false);
5526e819 550 }
67a99992 551#endif // wxUSE_STATUSBAR
5526e819
VS
552
553 node = m_Filters.GetFirst();
4f9297b0 554 while (node)
e421922f 555 {
4f9297b0
VS
556 wxHtmlFilter *h = (wxHtmlFilter*) node->GetData();
557 if (h->CanRead(*f))
e421922f 558 {
4f9297b0 559 src = h->ReadFile(*f);
5526e819
VS
560 break;
561 }
4f9297b0 562 node = node->GetNext();
5526e819 563 }
33ac7e6f 564 if (src == wxEmptyString)
e421922f 565 {
89de9af3 566 if (m_DefaultFilter == NULL) m_DefaultFilter = GetDefaultFilter();
4f9297b0 567 src = m_DefaultFilter->ReadFile(*f);
89de9af3 568 }
5526e819 569
4f9297b0 570 m_FS->ChangePathTo(f->GetLocation());
5526e819 571 rt_val = SetPage(src);
4f9297b0 572 m_OpenedPage = f->GetLocation();
33ac7e6f 573 if (f->GetAnchor() != wxEmptyString)
e421922f 574 {
4f9297b0 575 ScrollToAnchor(f->GetAnchor());
5526e819
VS
576 }
577
578 delete f;
579
67a99992 580#if wxUSE_STATUSBAR
d1da8872 581 if (m_RelatedStatusBar != -1)
67a99992
WS
582 m_RelatedFrame->SetStatusText(_("Done"), m_RelatedStatusBar);
583#endif // wxUSE_STATUSBAR
5526e819
VS
584 }
585 }
586
4f9297b0
VS
587 if (m_HistoryOn) // add this page to history there:
588 {
892aeafc 589 int c = m_History->GetCount() - (m_HistoryPos + 1);
5526e819 590
0cb9cfb2 591 if (m_HistoryPos < 0 ||
ee19c324
VS
592 (*m_History)[m_HistoryPos].GetPage() != m_OpenedPage ||
593 (*m_History)[m_HistoryPos].GetAnchor() != m_OpenedAnchor)
594 {
595 m_HistoryPos++;
596 for (int i = 0; i < c; i++)
b54e41c5 597 m_History->RemoveAt(m_HistoryPos);
ee19c324
VS
598 m_History->Add(new wxHtmlHistoryItem(m_OpenedPage, m_OpenedAnchor));
599 }
5526e819
VS
600 }
601
096824d7
VS
602 if (m_OpenedPageTitle == wxEmptyString)
603 OnSetTitle(wxFileNameFromPath(m_OpenedPage));
fc7dfaf8 604
33ac7e6f 605 if (needs_refresh)
4f9297b0 606 {
fc7dfaf8
VS
607 m_tmpCanDrawLocks--;
608 Refresh();
609 }
610 else
611 m_tmpCanDrawLocks--;
612
5526e819
VS
613 return rt_val;
614}
615
616
7cb9cf89
VS
617bool wxHtmlWindow::LoadFile(const wxFileName& filename)
618{
619 wxString url = wxFileSystem::FileNameToURL(filename);
620 return LoadPage(url);
621}
622
5526e819
VS
623
624bool wxHtmlWindow::ScrollToAnchor(const wxString& anchor)
625{
4f9297b0 626 const wxHtmlCell *c = m_Cell->Find(wxHTML_COND_ISANCHOR, &anchor);
f3c82859
VS
627 if (!c)
628 {
f6bcfd97 629 wxLogWarning(_("HTML anchor %s does not exist."), anchor.c_str());
d1da8872 630 return false;
f3c82859 631 }
33ac7e6f 632 else
4f9297b0 633 {
5526e819 634 int y;
269e8200 635
4f9297b0 636 for (y = 0; c != NULL; c = c->GetParent()) y += c->GetPosY();
efba2b89 637 Scroll(-1, y / wxHTML_SCROLL_STEP);
5526e819 638 m_OpenedAnchor = anchor;
d1da8872 639 return true;
5526e819
VS
640 }
641}
642
643
d5db80c2 644void wxHtmlWindow::OnSetTitle(const wxString& title)
5526e819 645{
33ac7e6f 646 if (m_RelatedFrame)
4f9297b0 647 {
5526e819
VS
648 wxString tit;
649 tit.Printf(m_TitleFormat, title.c_str());
4f9297b0 650 m_RelatedFrame->SetTitle(tit);
5526e819 651 }
d5db80c2 652 m_OpenedPageTitle = title;
5526e819
VS
653}
654
655
656
657
658
659void wxHtmlWindow::CreateLayout()
660{
661 int ClientWidth, ClientHeight;
662
663 if (!m_Cell) return;
a547ebff 664
cc87fbed 665 if ( HasFlag(wxHW_SCROLLBAR_NEVER) )
4f9297b0 666 {
689f42f4 667 SetScrollbars(1, 1, 0, 0); // always off
a547ebff 668 GetClientSize(&ClientWidth, &ClientHeight);
4f9297b0 669 m_Cell->Layout(ClientWidth);
a547ebff 670 }
cc87fbed
VZ
671 else // !wxHW_SCROLLBAR_NEVER
672 {
a547ebff 673 GetClientSize(&ClientWidth, &ClientHeight);
4f9297b0 674 m_Cell->Layout(ClientWidth);
33ac7e6f 675 if (ClientHeight < m_Cell->GetHeight() + GetCharHeight())
e421922f 676 {
f3bcfd9b
VS
677 SetScrollbars(
678 wxHTML_SCROLL_STEP, wxHTML_SCROLL_STEP,
4f9297b0
VS
679 m_Cell->GetWidth() / wxHTML_SCROLL_STEP,
680 (m_Cell->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
f3bcfd9b 681 /*cheat: top-level frag is always container*/);
a547ebff 682 }
4f9297b0 683 else /* we fit into window, no need for scrollbars */
e421922f 684 {
4f9297b0 685 SetScrollbars(wxHTML_SCROLL_STEP, 1, m_Cell->GetWidth() / wxHTML_SCROLL_STEP, 0); // disable...
89de9af3 686 GetClientSize(&ClientWidth, &ClientHeight);
4f9297b0 687 m_Cell->Layout(ClientWidth); // ...and relayout
89de9af3 688 }
a547ebff 689 }
5526e819
VS
690}
691
269e8200 692
5526e819
VS
693
694void wxHtmlWindow::ReadCustomization(wxConfigBase *cfg, wxString path)
695{
696 wxString oldpath;
697 wxString tmp;
d5db80c2
VS
698 int p_fontsizes[7];
699 wxString p_fff, p_ffn;
5526e819 700
33ac7e6f 701 if (path != wxEmptyString)
4f9297b0
VS
702 {
703 oldpath = cfg->GetPath();
704 cfg->SetPath(path);
5526e819
VS
705 }
706
892aeafc
VS
707 m_Borders = cfg->Read(wxT("wxHtmlWindow/Borders"), m_Borders);
708 p_fff = cfg->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser->m_FontFaceFixed);
709 p_ffn = cfg->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser->m_FontFaceNormal);
bfb9ee96 710 for (int i = 0; i < 7; i++)
4f9297b0 711 {
66a77a74 712 tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i);
4f9297b0 713 p_fontsizes[i] = cfg->Read(tmp, m_Parser->m_FontsSizes[i]);
5526e819 714 }
8eb2940f 715 SetFonts(p_ffn, p_fff, p_fontsizes);
5526e819
VS
716
717 if (path != wxEmptyString)
4f9297b0 718 cfg->SetPath(oldpath);
5526e819
VS
719}
720
721
722
723void wxHtmlWindow::WriteCustomization(wxConfigBase *cfg, wxString path)
724{
725 wxString oldpath;
726 wxString tmp;
727
33ac7e6f 728 if (path != wxEmptyString)
4f9297b0
VS
729 {
730 oldpath = cfg->GetPath();
731 cfg->SetPath(path);
5526e819
VS
732 }
733
892aeafc
VS
734 cfg->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders);
735 cfg->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser->m_FontFaceFixed);
736 cfg->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser->m_FontFaceNormal);
bfb9ee96 737 for (int i = 0; i < 7; i++)
4f9297b0 738 {
66a77a74 739 tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i);
4f9297b0 740 cfg->Write(tmp, (long) m_Parser->m_FontsSizes[i]);
5526e819
VS
741 }
742
743 if (path != wxEmptyString)
4f9297b0 744 cfg->SetPath(oldpath);
5526e819
VS
745}
746
747
748
749bool wxHtmlWindow::HistoryBack()
750{
751 wxString a, l;
752
d1da8872 753 if (m_HistoryPos < 1) return false;
5526e819 754
bbda1088
VS
755 // store scroll position into history item:
756 int x, y;
e421922f 757 GetViewStart(&x, &y);
892aeafc 758 (*m_History)[m_HistoryPos].SetPos(y);
bbda1088
VS
759
760 // go to previous position:
5526e819
VS
761 m_HistoryPos--;
762
892aeafc
VS
763 l = (*m_History)[m_HistoryPos].GetPage();
764 a = (*m_History)[m_HistoryPos].GetAnchor();
d1da8872 765 m_HistoryOn = false;
89de9af3 766 m_tmpCanDrawLocks++;
5526e819 767 if (a == wxEmptyString) LoadPage(l);
fc7dfaf8 768 else LoadPage(l + wxT("#") + a);
d1da8872 769 m_HistoryOn = true;
89de9af3 770 m_tmpCanDrawLocks--;
892aeafc 771 Scroll(0, (*m_History)[m_HistoryPos].GetPos());
5526e819 772 Refresh();
d1da8872 773 return true;
5526e819
VS
774}
775
1b113a81
VS
776bool wxHtmlWindow::HistoryCanBack()
777{
d1da8872
WS
778 if (m_HistoryPos < 1) return false;
779 return true ;
1b113a81 780}
5526e819
VS
781
782
783bool wxHtmlWindow::HistoryForward()
784{
785 wxString a, l;
786
d1da8872
WS
787 if (m_HistoryPos == -1) return false;
788 if (m_HistoryPos >= (int)m_History->GetCount() - 1)return false;
5526e819
VS
789
790 m_OpenedPage = wxEmptyString; // this will disable adding new entry into history in LoadPage()
791
792 m_HistoryPos++;
892aeafc
VS
793 l = (*m_History)[m_HistoryPos].GetPage();
794 a = (*m_History)[m_HistoryPos].GetAnchor();
d1da8872 795 m_HistoryOn = false;
89de9af3 796 m_tmpCanDrawLocks++;
5526e819 797 if (a == wxEmptyString) LoadPage(l);
fc7dfaf8 798 else LoadPage(l + wxT("#") + a);
d1da8872 799 m_HistoryOn = true;
89de9af3 800 m_tmpCanDrawLocks--;
892aeafc 801 Scroll(0, (*m_History)[m_HistoryPos].GetPos());
5526e819 802 Refresh();
d1da8872 803 return true;
5526e819
VS
804}
805
1b113a81
VS
806bool wxHtmlWindow::HistoryCanForward()
807{
d1da8872
WS
808 if (m_HistoryPos == -1) return false;
809 if (m_HistoryPos >= (int)m_History->GetCount() - 1)return false;
810 return true ;
1b113a81 811}
5526e819
VS
812
813
814void wxHtmlWindow::HistoryClear()
815{
892aeafc 816 m_History->Empty();
5526e819
VS
817 m_HistoryPos = -1;
818}
819
892aeafc
VS
820void wxHtmlWindow::AddProcessor(wxHtmlProcessor *processor)
821{
822 if (!m_Processors)
823 {
824 m_Processors = new wxHtmlProcessorList;
892aeafc 825 }
222ed1d6 826 wxHtmlProcessorList::compatibility_iterator node;
bfb9ee96 827
892aeafc
VS
828 for (node = m_Processors->GetFirst(); node; node = node->GetNext())
829 {
bfb9ee96 830 if (processor->GetPriority() > node->GetData()->GetPriority())
892aeafc
VS
831 {
832 m_Processors->Insert(node, processor);
960ba969 833 return;
892aeafc
VS
834 }
835 }
960ba969 836 m_Processors->Append(processor);
892aeafc
VS
837}
838
960ba969 839/*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor *processor)
892aeafc 840{
960ba969 841 if (!m_GlobalProcessors)
892aeafc 842 {
960ba969 843 m_GlobalProcessors = new wxHtmlProcessorList;
892aeafc 844 }
222ed1d6 845 wxHtmlProcessorList::compatibility_iterator node;
e421922f 846
960ba969 847 for (node = m_GlobalProcessors->GetFirst(); node; node = node->GetNext())
892aeafc 848 {
bfb9ee96 849 if (processor->GetPriority() > node->GetData()->GetPriority())
892aeafc 850 {
960ba969
VS
851 m_GlobalProcessors->Insert(node, processor);
852 return;
892aeafc
VS
853 }
854 }
960ba969 855 m_GlobalProcessors->Append(processor);
892aeafc
VS
856}
857
5526e819
VS
858
859
5526e819
VS
860void wxHtmlWindow::AddFilter(wxHtmlFilter *filter)
861{
5526e819
VS
862 m_Filters.Append(filter);
863}
864
865
36c4ff4d
VS
866bool wxHtmlWindow::IsSelectionEnabled() const
867{
868#if wxUSE_CLIPBOARD
cc87fbed 869 return !HasFlag(wxHW_NO_SELECTION);
36c4ff4d
VS
870#else
871 return false;
872#endif
873}
d659d703 874
e3774124 875
1338c59a 876#if wxUSE_CLIPBOARD
977b867e 877wxString wxHtmlWindow::DoSelectionToText(wxHtmlSelection *sel)
e3774124 878{
977b867e 879 if ( !sel )
e3774124
VS
880 return wxEmptyString;
881
f30e67db
VS
882 wxClientDC dc(this);
883
977b867e 884 const wxHtmlCell *end = sel->GetToCell();
e3774124 885 wxString text;
977b867e 886 wxHtmlTerminalCellsInterator i(sel->GetFromCell(), end);
e3774124
VS
887 if ( i )
888 {
977b867e 889 text << i->ConvertToText(sel);
e3774124
VS
890 ++i;
891 }
892 const wxHtmlCell *prev = *i;
893 while ( i )
894 {
895 if ( prev->GetParent() != i->GetParent() )
896 text << _T('\n');
977b867e 897 text << i->ConvertToText(*i == end ? sel : NULL);
e3774124
VS
898 prev = *i;
899 ++i;
900 }
901 return text;
902}
903
977b867e
VS
904wxString wxHtmlWindow::ToText()
905{
906 if (m_Cell)
907 {
908 wxHtmlSelection sel;
909 sel.Set(m_Cell->GetFirstTerminal(), m_Cell->GetLastTerminal());
910 return DoSelectionToText(&sel);
911 }
912 else
913 return wxEmptyString;
914}
915
d659d703
VZ
916#endif // wxUSE_CLIPBOARD
917
8feaa81f 918bool wxHtmlWindow::CopySelection(ClipboardType t)
e3774124 919{
d659d703 920#if wxUSE_CLIPBOARD
e3774124
VS
921 if ( m_selection )
922 {
28b2ac5b 923#if defined(__UNIX__) && !defined(__WXMAC__)
e3774124 924 wxTheClipboard->UsePrimarySelection(t == Primary);
d659d703
VZ
925#else // !__UNIX__
926 // Primary selection exists only under X11, so don't do anything under
927 // the other platforms when we try to access it
928 //
929 // TODO: this should be abstracted at wxClipboard level!
930 if ( t == Primary )
8feaa81f 931 return false;
d659d703
VZ
932#endif // __UNIX__/!__UNIX__
933
e3774124
VS
934 if ( wxTheClipboard->Open() )
935 {
d659d703 936 const wxString txt(SelectionToText());
e3774124
VS
937 wxTheClipboard->SetData(new wxTextDataObject(txt));
938 wxTheClipboard->Close();
939 wxLogTrace(_T("wxhtmlselection"),
940 _("Copied to clipboard:\"%s\""), txt.c_str());
8feaa81f
VZ
941
942 return true;
e3774124
VS
943 }
944 }
caf448e3
WS
945#else
946 wxUnusedVar(t);
d659d703 947#endif // wxUSE_CLIPBOARD
8feaa81f
VZ
948
949 return false;
e3774124 950}
5526e819
VS
951
952
0b2dadd3 953void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo& link)
5526e819 954{
a1c3cdc4 955 wxHtmlLinkEvent event(GetId(), link);
33c2a4b7 956 event.SetEventObject(this);
a1c3cdc4
VS
957 if (!GetEventHandler()->ProcessEvent(event))
958 {
959 // the default behaviour is to load the URL in this window
960 const wxMouseEvent *e = event.GetLinkInfo().GetEvent();
961 if (e == NULL || e->LeftUp())
962 LoadPage(event.GetLinkInfo().GetHref());
963 }
5526e819
VS
964}
965
12148be2 966void wxHtmlWindow::OnEraseBackground(wxEraseEvent& event)
1338c59a 967{
97e490f8
VZ
968 if ( !m_bmpBg.Ok() )
969 {
518ba663
VZ
970 // don't even skip the event, if we don't have a bg bitmap we're going
971 // to overwrite background in OnPaint() below anyhow, so letting the
972 // default handling take place would only result in flicker, just set a
973 // flag to erase the background below
974 m_eraseBgInOnPaint = true;
97e490f8
VZ
975 return;
976 }
977
978 wxDC& dc = *event.GetDC();
979
980 // if the image is not fully opaque, we have to erase the background before
981 // drawing it, however avoid doing it for opaque images as this would just
982 // result in extra flicker without any other effect as background is
983 // completely covered anyhow
984 if ( m_bmpBg.GetMask() )
985 {
986 dc.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID));
987 dc.Clear();
988 }
989
990 const wxSize sizeWin(GetClientSize());
991 const wxSize sizeBmp(m_bmpBg.GetWidth(), m_bmpBg.GetHeight());
992 for ( wxCoord x = 0; x < sizeWin.x; x += sizeBmp.x )
993 {
994 for ( wxCoord y = 0; y < sizeWin.y; y += sizeBmp.y )
995 {
996 dc.DrawBitmap(m_bmpBg, x, y, true /* use mask */);
997 }
998 }
1338c59a
VS
999}
1000
1001void wxHtmlWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
5526e819 1002{
1338c59a
VS
1003 wxPaintDC dc(this);
1004
518ba663
VZ
1005 if (m_tmpCanDrawLocks > 0 || m_Cell == NULL)
1006 return;
5526e819 1007
b835e9bf 1008 int x, y;
e421922f 1009 GetViewStart(&x, &y);
1338c59a
VS
1010 wxRect rect = GetUpdateRegion().GetBox();
1011 wxSize sz = GetSize();
1012
1013 wxMemoryDC dcm;
1014 if ( !m_backBuffer )
1015 m_backBuffer = new wxBitmap(sz.x, sz.y);
d659d703 1016 dcm.SelectObject(*m_backBuffer);
a03ae172 1017
518ba663
VZ
1018 if ( m_eraseBgInOnPaint )
1019 {
1020 dcm.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID));
1021 dcm.Clear();
1022
1023 m_eraseBgInOnPaint = false;
1024 }
1025 else // someone has already erased the background, keep it
1026 {
1027 // preserve the existing background, otherwise we'd erase anything the
1028 // user code had drawn in its EVT_ERASE_BACKGROUND handler when we do
1029 // the Blit back below
1030 dcm.Blit(0, rect.GetTop(),
1031 sz.x, rect.GetBottom() - rect.GetTop() + 1,
1032 &dc,
1033 0, rect.GetTop());
1034 }
a03ae172 1035
7f461f8a 1036 PrepareDC(dcm);
1338c59a
VS
1037 dcm.SetMapMode(wxMM_TEXT);
1038 dcm.SetBackgroundMode(wxTRANSPARENT);
d659d703 1039
f30e67db
VS
1040 wxHtmlRenderingInfo rinfo;
1041 wxDefaultHtmlRenderingStyle rstyle;
1042 rinfo.SetSelection(m_selection);
1043 rinfo.SetStyle(&rstyle);
1338c59a 1044 m_Cell->Draw(dcm, 0, 0,
790dbce3 1045 y * wxHTML_SCROLL_STEP + rect.GetTop(),
36c4ff4d 1046 y * wxHTML_SCROLL_STEP + rect.GetBottom(),
f30e67db 1047 rinfo);
d1da8872
WS
1048
1049//#define DEBUG_HTML_SELECTION
03693319
VS
1050#ifdef DEBUG_HTML_SELECTION
1051 {
1052 int xc, yc, x, y;
1053 wxGetMousePosition(&xc, &yc);
1054 ScreenToClient(&xc, &yc);
1055 CalcUnscrolledPosition(xc, yc, &x, &y);
1056 wxHtmlCell *at = m_Cell->FindCellByPos(x, y);
d1da8872 1057 wxHtmlCell *before =
03693319 1058 m_Cell->FindCellByPos(x, y, wxHTML_FIND_NEAREST_BEFORE);
d1da8872 1059 wxHtmlCell *after =
03693319 1060 m_Cell->FindCellByPos(x, y, wxHTML_FIND_NEAREST_AFTER);
d1da8872 1061
03693319
VS
1062 dcm.SetBrush(*wxTRANSPARENT_BRUSH);
1063 dcm.SetPen(*wxBLACK_PEN);
1064 if (at)
1065 dcm.DrawRectangle(at->GetAbsPos(),
1066 wxSize(at->GetWidth(),at->GetHeight()));
1067 dcm.SetPen(*wxGREEN_PEN);
1068 if (before)
1069 dcm.DrawRectangle(before->GetAbsPos().x+1, before->GetAbsPos().y+1,
1070 before->GetWidth()-2,before->GetHeight()-2);
1071 dcm.SetPen(*wxRED_PEN);
1072 if (after)
1073 dcm.DrawRectangle(after->GetAbsPos().x+2, after->GetAbsPos().y+2,
1074 after->GetWidth()-4,after->GetHeight()-4);
1075 }
1076#endif
d1da8872 1077
1338c59a
VS
1078 dcm.SetDeviceOrigin(0,0);
1079 dc.Blit(0, rect.GetTop(),
1080 sz.x, rect.GetBottom() - rect.GetTop() + 1,
1081 &dcm,
1082 0, rect.GetTop());
5526e819
VS
1083}
1084
1085
1086
1087
1088void wxHtmlWindow::OnSize(wxSizeEvent& event)
1089{
1338c59a
VS
1090 wxDELETE(m_backBuffer);
1091
5526e819
VS
1092 wxScrolledWindow::OnSize(event);
1093 CreateLayout();
1338c59a
VS
1094
1095 // Recompute selection if necessary:
1096 if ( m_selection )
1097 {
1098 m_selection->Set(m_selection->GetFromCell(),
1099 m_selection->GetToCell());
1100 m_selection->ClearPrivPos();
1101 }
d659d703 1102
f6bcfd97 1103 Refresh();
5526e819
VS
1104}
1105
1106
fc7a2a60 1107void wxHtmlWindow::OnMouseMove(wxMouseEvent& WXUNUSED(event))
5526e819 1108{
bc55e31b 1109 wxHtmlWindowMouseHelper::HandleMouseMoved();
31d8b4ad 1110}
5526e819 1111
adf2eb2d 1112void wxHtmlWindow::OnMouseDown(wxMouseEvent& event)
31d8b4ad 1113{
0994d968 1114#if wxUSE_CLIPBOARD
adf2eb2d 1115 if ( event.LeftDown() && IsSelectionEnabled() )
4f9297b0 1116 {
0994d968
VS
1117 const long TRIPLECLICK_LEN = 200; // 0.2 sec after doubleclick
1118 if ( wxGetLocalTimeMillis() - m_lastDoubleClick <= TRIPLECLICK_LEN )
adf2eb2d 1119 {
0994d968 1120 SelectLine(CalcUnscrolledPosition(event.GetPosition()));
d659d703 1121
5de65c69 1122 (void) CopySelection();
adf2eb2d 1123 }
0994d968 1124 else
d659d703 1125 {
0994d968 1126 m_makingSelection = true;
d659d703 1127
0994d968
VS
1128 if ( m_selection )
1129 {
1130 wxDELETE(m_selection);
1131 Refresh();
1132 }
1133 m_tmpSelFromPos = CalcUnscrolledPosition(event.GetPosition());
1134 m_tmpSelFromCell = NULL;
5526e819 1135
0994d968
VS
1136 CaptureMouse();
1137 }
adf2eb2d 1138 }
caf448e3
WS
1139#else
1140 wxUnusedVar(event);
d659d703 1141#endif // wxUSE_CLIPBOARD
adf2eb2d 1142}
5526e819 1143
adf2eb2d
VS
1144void wxHtmlWindow::OnMouseUp(wxMouseEvent& event)
1145{
1338c59a 1146#if wxUSE_CLIPBOARD
adf2eb2d
VS
1147 if ( m_makingSelection )
1148 {
1149 ReleaseMouse();
1150 m_makingSelection = false;
1151
907f2fab
VS
1152 // if m_selection=NULL, the user didn't move the mouse far enough from
1153 // starting point and the mouse up event is part of a click, the user
1154 // is not selecting text:
1155 if ( m_selection )
adf2eb2d 1156 {
907f2fab
VS
1157 CopySelection(Primary);
1158
adf2eb2d
VS
1159 // we don't want mouse up event that ended selecting to be
1160 // handled as mouse click and e.g. follow hyperlink:
1161 return;
1162 }
1163 }
d659d703
VZ
1164#endif // wxUSE_CLIPBOARD
1165
adf2eb2d 1166 SetFocus();
f6010d8f 1167
bc55e31b
VS
1168 wxPoint pos = CalcUnscrolledPosition(event.GetPosition());
1169 wxHtmlWindowMouseHelper::HandleMouseClick(m_Cell, pos, event);
5526e819
VS
1170}
1171
63e819f2
VS
1172#if wxUSE_CLIPBOARD
1173void wxHtmlWindow::OnMouseCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event))
1174{
1175 if ( !m_makingSelection )
1176 return;
1177
1178 // discard the selecting operation
1179 m_makingSelection = false;
1180 wxDELETE(m_selection);
1181 m_tmpSelFromCell = NULL;
1182 Refresh();
1183}
1184#endif // wxUSE_CLIPBOARD
5526e819
VS
1185
1186
5180055b
JS
1187void wxHtmlWindow::OnInternalIdle()
1188{
1189 wxWindow::OnInternalIdle();
d1da8872 1190
bc55e31b 1191 if (m_Cell != NULL && DidMouseMove())
4f9297b0 1192 {
03693319
VS
1193#ifdef DEBUG_HTML_SELECTION
1194 Refresh();
1195#endif
adf2eb2d
VS
1196 int xc, yc, x, y;
1197 wxGetMousePosition(&xc, &yc);
1198 ScreenToClient(&xc, &yc);
1199 CalcUnscrolledPosition(xc, yc, &x, &y);
0cb9cfb2 1200
f6010d8f 1201 wxHtmlCell *cell = m_Cell->FindCellByPos(x, y);
adf2eb2d
VS
1202
1203 // handle selection update:
1204 if ( m_makingSelection )
1205 {
adf2eb2d 1206 if ( !m_tmpSelFromCell )
748418c0
VS
1207 m_tmpSelFromCell = m_Cell->FindCellByPos(
1208 m_tmpSelFromPos.x,m_tmpSelFromPos.y);
d1da8872 1209
03693319
VS
1210 // NB: a trick - we adjust selFromPos to be upper left or bottom
1211 // right corner of the first cell of the selection depending
1212 // on whether the mouse is moving to the right or to the left.
1213 // This gives us more "natural" behaviour when selecting
1214 // a line (specifically, first cell of the next line is not
1215 // included if you drag selection from left to right over
1216 // entire line):
1217 wxPoint dirFromPos;
1218 if ( !m_tmpSelFromCell )
1219 {
1220 dirFromPos = m_tmpSelFromPos;
1221 }
1222 else
1223 {
1224 dirFromPos = m_tmpSelFromCell->GetAbsPos();
1225 if ( x < m_tmpSelFromPos.x )
1226 {
1227 dirFromPos.x += m_tmpSelFromCell->GetWidth();
1228 dirFromPos.y += m_tmpSelFromCell->GetHeight();
1229 }
1230 }
d1da8872 1231 bool goingDown = dirFromPos.y < y ||
03693319
VS
1232 (dirFromPos.y == y && dirFromPos.x < x);
1233
1234 // determine selection span:
748418c0 1235 if ( /*still*/ !m_tmpSelFromCell )
adf2eb2d
VS
1236 {
1237 if (goingDown)
1238 {
5a1597e9
VS
1239 m_tmpSelFromCell = m_Cell->FindCellByPos(
1240 m_tmpSelFromPos.x,m_tmpSelFromPos.y,
1241 wxHTML_FIND_NEAREST_AFTER);
adf2eb2d
VS
1242 if (!m_tmpSelFromCell)
1243 m_tmpSelFromCell = m_Cell->GetFirstTerminal();
1244 }
1245 else
1246 {
5a1597e9
VS
1247 m_tmpSelFromCell = m_Cell->FindCellByPos(
1248 m_tmpSelFromPos.x,m_tmpSelFromPos.y,
1249 wxHTML_FIND_NEAREST_BEFORE);
adf2eb2d
VS
1250 if (!m_tmpSelFromCell)
1251 m_tmpSelFromCell = m_Cell->GetLastTerminal();
1252 }
1253 }
1254
1255 wxHtmlCell *selcell = cell;
1256 if (!selcell)
1257 {
1258 if (goingDown)
1259 {
1260 selcell = m_Cell->FindCellByPos(x, y,
03693319 1261 wxHTML_FIND_NEAREST_BEFORE);
adf2eb2d
VS
1262 if (!selcell)
1263 selcell = m_Cell->GetLastTerminal();
1264 }
1265 else
1266 {
1267 selcell = m_Cell->FindCellByPos(x, y,
03693319 1268 wxHTML_FIND_NEAREST_AFTER);
adf2eb2d
VS
1269 if (!selcell)
1270 selcell = m_Cell->GetFirstTerminal();
1271 }
1272 }
1273
1274 // NB: it may *rarely* happen that the code above didn't find one
1275 // of the cells, e.g. if wxHtmlWindow doesn't contain any
d659d703 1276 // visible cells.
adf2eb2d 1277 if ( selcell && m_tmpSelFromCell )
d659d703 1278 {
adf2eb2d
VS
1279 if ( !m_selection )
1280 {
1281 // start selecting only if mouse movement was big enough
1282 // (otherwise it was meant as mouse click, not selection):
1283 const int PRECISION = 2;
1284 wxPoint diff = m_tmpSelFromPos - wxPoint(x,y);
1285 if (abs(diff.x) > PRECISION || abs(diff.y) > PRECISION)
1286 {
1287 m_selection = new wxHtmlSelection();
1288 }
1289 }
1290 if ( m_selection )
1291 {
e3774124 1292 if ( m_tmpSelFromCell->IsBefore(selcell) )
adf2eb2d
VS
1293 {
1294 m_selection->Set(m_tmpSelFromPos, m_tmpSelFromCell,
a1c3cdc4
VS
1295 wxPoint(x,y), selcell);
1296 }
adf2eb2d
VS
1297 else
1298 {
1299 m_selection->Set(wxPoint(x,y), selcell,
1300 m_tmpSelFromPos, m_tmpSelFromCell);
1301 }
1338c59a
VS
1302 m_selection->ClearPrivPos();
1303 Refresh();
adf2eb2d
VS
1304 }
1305 }
1306 }
d659d703 1307
adf2eb2d 1308 // handle cursor and status bar text changes:
f6010d8f 1309
bc55e31b
VS
1310 // NB: because we're passing in 'cell' and not 'm_Cell' (so that the
1311 // leaf cell lookup isn't done twice), we need to adjust the
1312 // position for the new root:
1313 wxPoint posInCell(x, y);
1314 if (cell)
1315 posInCell -= cell->GetAbsPos();
1316 wxHtmlWindowMouseHelper::HandleIdle(cell, posInCell);
5526e819
VS
1317 }
1318}
1319
e3774124 1320#if wxUSE_CLIPBOARD
1338c59a
VS
1321void wxHtmlWindow::StopAutoScrolling()
1322{
1323 if ( m_timerAutoScroll )
1324 {
1325 wxDELETE(m_timerAutoScroll);
1326 }
1327}
1328
1329void wxHtmlWindow::OnMouseEnter(wxMouseEvent& event)
1330{
1331 StopAutoScrolling();
1332 event.Skip();
1333}
1334
1335void wxHtmlWindow::OnMouseLeave(wxMouseEvent& event)
1336{
1337 // don't prevent the usual processing of the event from taking place
1338 event.Skip();
1339
1340 // when a captured mouse leave a scrolled window we start generate
1341 // scrolling events to allow, for example, extending selection beyond the
1342 // visible area in some controls
1343 if ( wxWindow::GetCapture() == this )
1344 {
1345 // where is the mouse leaving?
1346 int pos, orient;
1347 wxPoint pt = event.GetPosition();
1348 if ( pt.x < 0 )
1349 {
1350 orient = wxHORIZONTAL;
1351 pos = 0;
1352 }
1353 else if ( pt.y < 0 )
1354 {
1355 orient = wxVERTICAL;
1356 pos = 0;
1357 }
1358 else // we're lower or to the right of the window
1359 {
1360 wxSize size = GetClientSize();
1361 if ( pt.x > size.x )
1362 {
1363 orient = wxHORIZONTAL;
1364 pos = GetVirtualSize().x / wxHTML_SCROLL_STEP;
1365 }
1366 else if ( pt.y > size.y )
1367 {
1368 orient = wxVERTICAL;
1369 pos = GetVirtualSize().y / wxHTML_SCROLL_STEP;
1370 }
1371 else // this should be impossible
1372 {
1373 // but seems to happen sometimes under wxMSW - maybe it's a bug
1374 // there but for now just ignore it
1375
1376 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1377
1378 return;
1379 }
1380 }
1381
1382 // only start the auto scroll timer if the window can be scrolled in
1383 // this direction
1384 if ( !HasScrollbar(orient) )
1385 return;
1386
1387 delete m_timerAutoScroll;
1388 m_timerAutoScroll = new wxHtmlWinAutoScrollTimer
1389 (
1390 this,
1391 pos == 0 ? wxEVT_SCROLLWIN_LINEUP
1392 : wxEVT_SCROLLWIN_LINEDOWN,
1393 pos,
1394 orient
1395 );
1396 m_timerAutoScroll->Start(50); // FIXME: make configurable
1397 }
1398}
1399
e3774124
VS
1400void wxHtmlWindow::OnKeyUp(wxKeyEvent& event)
1401{
0f11c233
VZ
1402 if ( IsSelectionEnabled() &&
1403 (event.GetKeyCode() == 'C' && event.CmdDown()) )
e3774124 1404 {
0f11c233
VZ
1405 wxClipboardTextEvent evt(wxEVT_COMMAND_TEXT_COPY, GetId());
1406
1407 evt.SetEventObject(this);
1408
1409 GetEventHandler()->ProcessEvent(evt);
e3774124 1410 }
e3774124
VS
1411}
1412
fc7a2a60 1413void wxHtmlWindow::OnCopy(wxCommandEvent& WXUNUSED(event))
e3774124 1414{
5de65c69 1415 (void) CopySelection();
e3774124 1416}
d659d703 1417
0f11c233
VZ
1418void wxHtmlWindow::OnClipboardEvent(wxClipboardTextEvent& WXUNUSED(event))
1419{
1420 (void) CopySelection();
1421}
1422
31eefb99
VS
1423void wxHtmlWindow::OnDoubleClick(wxMouseEvent& event)
1424{
1425 // select word under cursor:
1426 if ( IsSelectionEnabled() )
1427 {
0994d968 1428 SelectWord(CalcUnscrolledPosition(event.GetPosition()));
d659d703 1429
5de65c69 1430 (void) CopySelection(Primary);
d659d703 1431
0994d968 1432 m_lastDoubleClick = wxGetLocalTimeMillis();
31eefb99
VS
1433 }
1434 else
1435 event.Skip();
1436}
0994d968
VS
1437
1438void wxHtmlWindow::SelectWord(const wxPoint& pos)
1439{
2a536376 1440 if ( m_Cell )
0994d968 1441 {
2a536376
VS
1442 wxHtmlCell *cell = m_Cell->FindCellByPos(pos.x, pos.y);
1443 if ( cell )
1444 {
1445 delete m_selection;
1446 m_selection = new wxHtmlSelection();
1447 m_selection->Set(cell, cell);
1448 RefreshRect(wxRect(CalcScrolledPosition(cell->GetAbsPos()),
1449 wxSize(cell->GetWidth(), cell->GetHeight())));
1450 }
0994d968
VS
1451 }
1452}
1453
1454void wxHtmlWindow::SelectLine(const wxPoint& pos)
1455{
2a536376 1456 if ( m_Cell )
0994d968 1457 {
2a536376
VS
1458 wxHtmlCell *cell = m_Cell->FindCellByPos(pos.x, pos.y);
1459 if ( cell )
0994d968 1460 {
2a536376
VS
1461 // We use following heuristic to find a "line": let the line be all
1462 // cells in same container as the cell under mouse cursor that are
1463 // neither completely above nor completely bellow the clicked cell
1464 // (i.e. are likely to be words positioned on same line of text).
1465
1466 int y1 = cell->GetAbsPos().y;
1467 int y2 = y1 + cell->GetHeight();
1468 int y;
1469 const wxHtmlCell *c;
1470 const wxHtmlCell *before = NULL;
1471 const wxHtmlCell *after = NULL;
1472
1473 // find last cell of line:
1474 for ( c = cell->GetNext(); c; c = c->GetNext())
1475 {
1476 y = c->GetAbsPos().y;
1477 if ( y + c->GetHeight() > y1 && y < y2 )
1478 after = c;
1479 else
1480 break;
1481 }
1482 if ( !after )
1483 after = cell;
0994d968 1484
2a536376
VS
1485 // find first cell of line:
1486 for ( c = cell->GetParent()->GetFirstChild();
1487 c && c != cell; c = c->GetNext())
0994d968 1488 {
2a536376
VS
1489 y = c->GetAbsPos().y;
1490 if ( y + c->GetHeight() > y1 && y < y2 )
1491 {
1492 if ( ! before )
1493 before = c;
1494 }
1495 else
1496 before = NULL;
0994d968 1497 }
2a536376
VS
1498 if ( !before )
1499 before = cell;
1500
1501 delete m_selection;
1502 m_selection = new wxHtmlSelection();
1503 m_selection->Set(before, after);
1504
1505 Refresh();
0994d968 1506 }
2a536376
VS
1507 }
1508}
d659d703 1509
2a536376
VS
1510void wxHtmlWindow::SelectAll()
1511{
1512 if ( m_Cell )
1513 {
0994d968
VS
1514 delete m_selection;
1515 m_selection = new wxHtmlSelection();
2a536376 1516 m_selection->Set(m_Cell->GetFirstTerminal(), m_Cell->GetLastTerminal());
0994d968
VS
1517 Refresh();
1518 }
1519}
2a536376 1520
d659d703 1521#endif // wxUSE_CLIPBOARD
e3774124
VS
1522
1523
5526e819 1524
bfb9ee96 1525IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor,wxObject)
5526e819 1526
786a2425
SC
1527#if wxUSE_EXTENDED_RTTI
1528IMPLEMENT_DYNAMIC_CLASS_XTI(wxHtmlWindow, wxScrolledWindow,"wx/html/htmlwin.h")
1529
459f2add 1530wxBEGIN_PROPERTIES_TABLE(wxHtmlWindow)
786a2425 1531/*
d1da8872
WS
1532 TODO PROPERTIES
1533 style , wxHW_SCROLLBAR_AUTO
1534 borders , (dimension)
1535 url , string
1536 htmlcode , string
786a2425 1537*/
459f2add 1538wxEND_PROPERTIES_TABLE()
786a2425 1539
459f2add
SC
1540wxBEGIN_HANDLERS_TABLE(wxHtmlWindow)
1541wxEND_HANDLERS_TABLE()
786a2425 1542
d1da8872 1543wxCONSTRUCTOR_5( wxHtmlWindow , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle )
786a2425 1544#else
5526e819 1545IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow,wxScrolledWindow)
786a2425 1546#endif
5526e819
VS
1547
1548BEGIN_EVENT_TABLE(wxHtmlWindow, wxScrolledWindow)
1549 EVT_SIZE(wxHtmlWindow::OnSize)
adf2eb2d
VS
1550 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseDown)
1551 EVT_LEFT_UP(wxHtmlWindow::OnMouseUp)
1552 EVT_RIGHT_UP(wxHtmlWindow::OnMouseUp)
31d8b4ad 1553 EVT_MOTION(wxHtmlWindow::OnMouseMove)
1338c59a
VS
1554 EVT_ERASE_BACKGROUND(wxHtmlWindow::OnEraseBackground)
1555 EVT_PAINT(wxHtmlWindow::OnPaint)
e3774124 1556#if wxUSE_CLIPBOARD
31eefb99 1557 EVT_LEFT_DCLICK(wxHtmlWindow::OnDoubleClick)
1338c59a
VS
1558 EVT_ENTER_WINDOW(wxHtmlWindow::OnMouseEnter)
1559 EVT_LEAVE_WINDOW(wxHtmlWindow::OnMouseLeave)
63e819f2 1560 EVT_MOUSE_CAPTURE_LOST(wxHtmlWindow::OnMouseCaptureLost)
e3774124
VS
1561 EVT_KEY_UP(wxHtmlWindow::OnKeyUp)
1562 EVT_MENU(wxID_COPY, wxHtmlWindow::OnCopy)
0f11c233 1563 EVT_TEXT_COPY(wxID_ANY, wxHtmlWindow::OnClipboardEvent)
d659d703 1564#endif // wxUSE_CLIPBOARD
5526e819
VS
1565END_EVENT_TABLE()
1566
bc55e31b
VS
1567//-----------------------------------------------------------------------------
1568// wxHtmlWindowInterface implementation in wxHtmlWindow
1569//-----------------------------------------------------------------------------
1570
1571void wxHtmlWindow::SetHTMLWindowTitle(const wxString& title)
1572{
1573 OnSetTitle(title);
1574}
1575
1576void wxHtmlWindow::OnHTMLLinkClicked(const wxHtmlLinkInfo& link)
1577{
1578 OnLinkClicked(link);
1579}
1580
1581wxHtmlOpeningStatus wxHtmlWindow::OnHTMLOpeningURL(wxHtmlURLType type,
1582 const wxString& url,
1583 wxString *redirect) const
1584{
1585 return OnOpeningURL(type, url, redirect);
1586}
1587
1588wxPoint wxHtmlWindow::HTMLCoordsToWindow(wxHtmlCell *WXUNUSED(cell),
1589 const wxPoint& pos) const
1590{
1591 return CalcScrolledPosition(pos);
1592}
1593
1594wxWindow* wxHtmlWindow::GetHTMLWindow()
1595{
1596 return this;
1597}
1598
1599wxColour wxHtmlWindow::GetHTMLBackgroundColour() const
1600{
1601 return GetBackgroundColour();
1602}
1603
1604void wxHtmlWindow::SetHTMLBackgroundColour(const wxColour& clr)
1605{
1606 SetBackgroundColour(clr);
1607}
1608
1609void wxHtmlWindow::SetHTMLBackgroundImage(const wxBitmap& bmpBg)
1610{
1611 SetBackgroundImage(bmpBg);
1612}
5526e819 1613
bc55e31b
VS
1614void wxHtmlWindow::SetHTMLStatusText(const wxString& text)
1615{
1616#if wxUSE_STATUSBAR
1617 if (m_RelatedStatusBar != -1)
1618 m_RelatedFrame->SetStatusText(text, m_RelatedStatusBar);
80d99525
WS
1619#else
1620 wxUnusedVar(text);
bc55e31b
VS
1621#endif // wxUSE_STATUSBAR
1622}
5526e819 1623
88a1b648
VS
1624/*static*/
1625wxCursor wxHtmlWindow::GetDefaultHTMLCursor(HTMLCursor type)
1626{
1627 switch (type)
1628 {
1629 case HTMLCursor_Link:
1630 if ( !ms_cursorLink )
1631 ms_cursorLink = new wxCursor(wxCURSOR_HAND);
1632 return *ms_cursorLink;
1633
1634 case HTMLCursor_Text:
1635 if ( !ms_cursorText )
1636 ms_cursorText = new wxCursor(wxCURSOR_IBEAM);
1637 return *ms_cursorText;
1638
1639 case HTMLCursor_Default:
1640 default:
1641 return *wxSTANDARD_CURSOR;
1642 }
1643}
1644
1645wxCursor wxHtmlWindow::GetHTMLCursor(HTMLCursor type) const
1646{
1647 return GetDefaultHTMLCursor(type);
1648}
1649
5526e819 1650
bc55e31b
VS
1651//-----------------------------------------------------------------------------
1652// wxHtmlWinModule
1653//-----------------------------------------------------------------------------
5526e819 1654
a76015e6
VS
1655// A module to allow initialization/cleanup
1656// without calling these functions from app.cpp or from
1657// the user's application.
1658
1659class wxHtmlWinModule: public wxModule
1660{
1661DECLARE_DYNAMIC_CLASS(wxHtmlWinModule)
1662public:
1663 wxHtmlWinModule() : wxModule() {}
d1da8872 1664 bool OnInit() { return true; }
a76015e6
VS
1665 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
1666};
1667
1668IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule, wxModule)
1669
5526e819 1670
0cecad31
VS
1671// This hack forces the linker to always link in m_* files
1672// (wxHTML doesn't work without handlers from these files)
1673#include "wx/html/forcelnk.h"
1674FORCE_WXHTML_MODULES()
5526e819 1675
d659d703 1676#endif // wxUSE_HTML