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