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