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