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