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