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