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