]>
Commit | Line | Data |
---|---|---|
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 | ||
51 | class wxHtmlWinAutoScrollTimer : public wxTimer | |
52 | { | |
53 | public: | |
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 | ||
66 | private: | |
67 | wxScrolledWindow *m_win; | |
68 | wxEventType m_eventType; | |
69 | int m_pos, | |
70 | m_orient; | |
71 | ||
72 | DECLARE_NO_COPY_CLASS(wxHtmlWinAutoScrollTimer) | |
73 | }; | |
74 | ||
75 | void 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 | 123 | class WXDLLIMPEXP_HTML wxHtmlHistoryItem |
892aeafc VS |
124 | { |
125 | public: | |
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 | ||
132 | private: | |
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 |
143 | WX_DECLARE_OBJARRAY(wxHtmlHistoryItem, wxHtmlHistoryArray); |
144 | WX_DEFINE_OBJARRAY(wxHtmlHistoryArray); | |
5526e819 | 145 | |
892aeafc VS |
146 | WX_DECLARE_LIST(wxHtmlProcessor, wxHtmlProcessorList); |
147 | WX_DEFINE_LIST(wxHtmlProcessorList); | |
5526e819 | 148 | |
892aeafc VS |
149 | //----------------------------------------------------------------------------- |
150 | // wxHtmlWindow | |
151 | //----------------------------------------------------------------------------- | |
5526e819 VS |
152 | |
153 | ||
4f417130 | 154 | void 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 | 185 | bool 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 |
200 | wxHtmlWindow::~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 | ||
225 | void 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 |
234 | void wxHtmlWindow::SetRelatedStatusBar(int bar) |
235 | { | |
236 | m_RelatedStatusBar = bar; | |
237 | } | |
67a99992 | 238 | #endif // wxUSE_STATUSBAR |
269e8200 RD |
239 | |
240 | ||
241 | ||
8eb2940f | 242 | void 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 |
253 | void 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 | ||
267 | bool 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)); |
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 |
327 | bool wxHtmlWindow::AppendToPage(const wxString& source) |
328 | { | |
329 | return SetPage(*(GetParser()->GetSource()) + source); | |
330 | } | |
5526e819 VS |
331 | |
332 | bool 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 |
480 | bool wxHtmlWindow::LoadFile(const wxFileName& filename) |
481 | { | |
482 | wxString url = wxFileSystem::FileNameToURL(filename); | |
483 | return LoadPage(url); | |
484 | } | |
485 | ||
5526e819 VS |
486 | |
487 | bool 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 | 507 | void 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 | ||
522 | void 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 | |
557 | void 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 | ||
586 | void 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 | ||
612 | bool 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 |
639 | bool wxHtmlWindow::HistoryCanBack() |
640 | { | |
d1da8872 WS |
641 | if (m_HistoryPos < 1) return false; |
642 | return true ; | |
1b113a81 | 643 | } |
5526e819 VS |
644 | |
645 | ||
646 | bool 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 |
669 | bool 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 | ||
677 | void wxHtmlWindow::HistoryClear() | |
678 | { | |
892aeafc | 679 | m_History->Empty(); |
5526e819 VS |
680 | m_HistoryPos = -1; |
681 | } | |
682 | ||
892aeafc VS |
683 | void 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 | ||
723 | wxList wxHtmlWindow::m_Filters; | |
a76015e6 | 724 | wxHtmlFilter *wxHtmlWindow::m_DefaultFilter = NULL; |
960ba969 | 725 | wxHtmlProcessorList *wxHtmlWindow::m_GlobalProcessors = NULL; |
a76015e6 VS |
726 | |
727 | void 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 | |
738 | void wxHtmlWindow::AddFilter(wxHtmlFilter *filter) | |
739 | { | |
5526e819 VS |
740 | m_Filters.Append(filter); |
741 | } | |
742 | ||
743 | ||
36c4ff4d VS |
744 | bool 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 | 755 | wxString 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 |
782 | wxString 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 | 796 | bool wxHtmlWindow::CopySelection(ClipboardType t) |
e3774124 | 797 | { |
d659d703 | 798 | #if wxUSE_CLIPBOARD |
e3774124 VS |
799 | if ( m_selection ) |
800 | { | |
d659d703 | 801 | #ifdef __UNIX__ |
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 | } | |
d659d703 | 823 | #endif // wxUSE_CLIPBOARD |
8feaa81f VZ |
824 | |
825 | return false; | |
e3774124 | 826 | } |
5526e819 VS |
827 | |
828 | ||
0b2dadd3 | 829 | void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo& link) |
5526e819 | 830 | { |
31d8b4ad VS |
831 | const wxMouseEvent *e = link.GetEvent(); |
832 | if (e == NULL || e->LeftUp()) | |
833 | LoadPage(link.GetHref()); | |
5526e819 VS |
834 | } |
835 | ||
f6010d8f VZ |
836 | void wxHtmlWindow::OnCellClicked(wxHtmlCell *cell, |
837 | wxCoord x, wxCoord y, | |
838 | const wxMouseEvent& event) | |
19193a2c | 839 | { |
f6010d8f | 840 | wxCHECK_RET( cell, _T("can't be called with NULL cell") ); |
5526e819 | 841 | |
f6010d8f VZ |
842 | cell->OnMouseClick(this, x, y, event); |
843 | } | |
844 | ||
845 | void wxHtmlWindow::OnCellMouseHover(wxHtmlCell * WXUNUSED(cell), | |
846 | wxCoord WXUNUSED(x), wxCoord WXUNUSED(y)) | |
847 | { | |
848 | // do nothing here | |
849 | } | |
5526e819 | 850 | |
fc7a2a60 | 851 | void wxHtmlWindow::OnEraseBackground(wxEraseEvent& WXUNUSED(event)) |
1338c59a VS |
852 | { |
853 | } | |
854 | ||
855 | void wxHtmlWindow::OnPaint(wxPaintEvent& WXUNUSED(event)) | |
5526e819 | 856 | { |
1338c59a VS |
857 | wxPaintDC dc(this); |
858 | ||
b835e9bf | 859 | if (m_tmpCanDrawLocks > 0 || m_Cell == NULL) return; |
5526e819 | 860 | |
b835e9bf | 861 | int x, y; |
e421922f | 862 | GetViewStart(&x, &y); |
1338c59a VS |
863 | wxRect rect = GetUpdateRegion().GetBox(); |
864 | wxSize sz = GetSize(); | |
865 | ||
866 | wxMemoryDC dcm; | |
867 | if ( !m_backBuffer ) | |
868 | m_backBuffer = new wxBitmap(sz.x, sz.y); | |
d659d703 | 869 | dcm.SelectObject(*m_backBuffer); |
1338c59a | 870 | PrepareDC(dcm); |
a03ae172 VZ |
871 | |
872 | // preserve the existing background, otherwise we'd erase anything the user | |
873 | // code had drawn in its EVT_ERASE_BACKGROUND handler when we do the Blit | |
874 | // back below | |
875 | dcm.Blit(0, rect.GetTop(), | |
876 | sz.x, rect.GetBottom() - rect.GetTop() + 1, | |
877 | &dc, | |
878 | 0, rect.GetTop()); | |
879 | ||
1338c59a VS |
880 | dcm.SetMapMode(wxMM_TEXT); |
881 | dcm.SetBackgroundMode(wxTRANSPARENT); | |
d659d703 | 882 | |
f30e67db VS |
883 | wxHtmlRenderingInfo rinfo; |
884 | wxDefaultHtmlRenderingStyle rstyle; | |
885 | rinfo.SetSelection(m_selection); | |
886 | rinfo.SetStyle(&rstyle); | |
1338c59a | 887 | m_Cell->Draw(dcm, 0, 0, |
790dbce3 | 888 | y * wxHTML_SCROLL_STEP + rect.GetTop(), |
36c4ff4d | 889 | y * wxHTML_SCROLL_STEP + rect.GetBottom(), |
f30e67db | 890 | rinfo); |
d1da8872 WS |
891 | |
892 | //#define DEBUG_HTML_SELECTION | |
03693319 VS |
893 | #ifdef DEBUG_HTML_SELECTION |
894 | { | |
895 | int xc, yc, x, y; | |
896 | wxGetMousePosition(&xc, &yc); | |
897 | ScreenToClient(&xc, &yc); | |
898 | CalcUnscrolledPosition(xc, yc, &x, &y); | |
899 | wxHtmlCell *at = m_Cell->FindCellByPos(x, y); | |
d1da8872 | 900 | wxHtmlCell *before = |
03693319 | 901 | m_Cell->FindCellByPos(x, y, wxHTML_FIND_NEAREST_BEFORE); |
d1da8872 | 902 | wxHtmlCell *after = |
03693319 | 903 | m_Cell->FindCellByPos(x, y, wxHTML_FIND_NEAREST_AFTER); |
d1da8872 | 904 | |
03693319 VS |
905 | dcm.SetBrush(*wxTRANSPARENT_BRUSH); |
906 | dcm.SetPen(*wxBLACK_PEN); | |
907 | if (at) | |
908 | dcm.DrawRectangle(at->GetAbsPos(), | |
909 | wxSize(at->GetWidth(),at->GetHeight())); | |
910 | dcm.SetPen(*wxGREEN_PEN); | |
911 | if (before) | |
912 | dcm.DrawRectangle(before->GetAbsPos().x+1, before->GetAbsPos().y+1, | |
913 | before->GetWidth()-2,before->GetHeight()-2); | |
914 | dcm.SetPen(*wxRED_PEN); | |
915 | if (after) | |
916 | dcm.DrawRectangle(after->GetAbsPos().x+2, after->GetAbsPos().y+2, | |
917 | after->GetWidth()-4,after->GetHeight()-4); | |
918 | } | |
919 | #endif | |
d1da8872 | 920 | |
1338c59a VS |
921 | dcm.SetDeviceOrigin(0,0); |
922 | dc.Blit(0, rect.GetTop(), | |
923 | sz.x, rect.GetBottom() - rect.GetTop() + 1, | |
924 | &dcm, | |
925 | 0, rect.GetTop()); | |
5526e819 VS |
926 | } |
927 | ||
928 | ||
929 | ||
930 | ||
931 | void wxHtmlWindow::OnSize(wxSizeEvent& event) | |
932 | { | |
1338c59a VS |
933 | wxDELETE(m_backBuffer); |
934 | ||
5526e819 VS |
935 | wxScrolledWindow::OnSize(event); |
936 | CreateLayout(); | |
1338c59a VS |
937 | |
938 | // Recompute selection if necessary: | |
939 | if ( m_selection ) | |
940 | { | |
941 | m_selection->Set(m_selection->GetFromCell(), | |
942 | m_selection->GetToCell()); | |
943 | m_selection->ClearPrivPos(); | |
944 | } | |
d659d703 | 945 | |
f6bcfd97 | 946 | Refresh(); |
5526e819 VS |
947 | } |
948 | ||
949 | ||
fc7a2a60 | 950 | void wxHtmlWindow::OnMouseMove(wxMouseEvent& WXUNUSED(event)) |
5526e819 | 951 | { |
31d8b4ad VS |
952 | m_tmpMouseMoved = true; |
953 | } | |
5526e819 | 954 | |
adf2eb2d | 955 | void wxHtmlWindow::OnMouseDown(wxMouseEvent& event) |
31d8b4ad | 956 | { |
0994d968 | 957 | #if wxUSE_CLIPBOARD |
adf2eb2d | 958 | if ( event.LeftDown() && IsSelectionEnabled() ) |
4f9297b0 | 959 | { |
0994d968 VS |
960 | const long TRIPLECLICK_LEN = 200; // 0.2 sec after doubleclick |
961 | if ( wxGetLocalTimeMillis() - m_lastDoubleClick <= TRIPLECLICK_LEN ) | |
adf2eb2d | 962 | { |
0994d968 | 963 | SelectLine(CalcUnscrolledPosition(event.GetPosition())); |
d659d703 | 964 | |
5de65c69 | 965 | (void) CopySelection(); |
adf2eb2d | 966 | } |
0994d968 | 967 | else |
d659d703 | 968 | { |
0994d968 | 969 | m_makingSelection = true; |
d659d703 | 970 | |
0994d968 VS |
971 | if ( m_selection ) |
972 | { | |
973 | wxDELETE(m_selection); | |
974 | Refresh(); | |
975 | } | |
976 | m_tmpSelFromPos = CalcUnscrolledPosition(event.GetPosition()); | |
977 | m_tmpSelFromCell = NULL; | |
5526e819 | 978 | |
0994d968 VS |
979 | CaptureMouse(); |
980 | } | |
adf2eb2d | 981 | } |
d659d703 | 982 | #endif // wxUSE_CLIPBOARD |
adf2eb2d | 983 | } |
5526e819 | 984 | |
adf2eb2d VS |
985 | void wxHtmlWindow::OnMouseUp(wxMouseEvent& event) |
986 | { | |
1338c59a | 987 | #if wxUSE_CLIPBOARD |
adf2eb2d VS |
988 | if ( m_makingSelection ) |
989 | { | |
990 | ReleaseMouse(); | |
991 | m_makingSelection = false; | |
992 | ||
993 | // did the user move the mouse far enough from starting point? | |
8feaa81f | 994 | if ( CopySelection(Primary) ) |
adf2eb2d VS |
995 | { |
996 | // we don't want mouse up event that ended selecting to be | |
997 | // handled as mouse click and e.g. follow hyperlink: | |
998 | return; | |
999 | } | |
1000 | } | |
d659d703 VZ |
1001 | #endif // wxUSE_CLIPBOARD |
1002 | ||
adf2eb2d VS |
1003 | SetFocus(); |
1004 | if ( m_Cell ) | |
1005 | { | |
1006 | wxPoint pos = CalcUnscrolledPosition(event.GetPosition()); | |
31d8b4ad | 1007 | wxHtmlCell *cell = m_Cell->FindCellByPos(pos.x, pos.y); |
f6010d8f | 1008 | |
d659d703 VZ |
1009 | // check is needed because FindCellByPos returns terminal cell and |
1010 | // containers may have empty borders -- in this case NULL will be | |
1011 | // returned | |
31d8b4ad VS |
1012 | if ( cell ) |
1013 | OnCellClicked(cell, pos.x, pos.y, event); | |
5526e819 VS |
1014 | } |
1015 | } | |
1016 | ||
1017 | ||
1018 | ||
5180055b JS |
1019 | void wxHtmlWindow::OnInternalIdle() |
1020 | { | |
1021 | wxWindow::OnInternalIdle(); | |
d1da8872 | 1022 | |
33ac7e6f | 1023 | if (m_tmpMouseMoved && (m_Cell != NULL)) |
4f9297b0 | 1024 | { |
03693319 VS |
1025 | #ifdef DEBUG_HTML_SELECTION |
1026 | Refresh(); | |
1027 | #endif | |
adf2eb2d VS |
1028 | int xc, yc, x, y; |
1029 | wxGetMousePosition(&xc, &yc); | |
1030 | ScreenToClient(&xc, &yc); | |
1031 | CalcUnscrolledPosition(xc, yc, &x, &y); | |
0cb9cfb2 | 1032 | |
f6010d8f | 1033 | wxHtmlCell *cell = m_Cell->FindCellByPos(x, y); |
adf2eb2d VS |
1034 | |
1035 | // handle selection update: | |
1036 | if ( m_makingSelection ) | |
1037 | { | |
adf2eb2d | 1038 | if ( !m_tmpSelFromCell ) |
748418c0 VS |
1039 | m_tmpSelFromCell = m_Cell->FindCellByPos( |
1040 | m_tmpSelFromPos.x,m_tmpSelFromPos.y); | |
d1da8872 | 1041 | |
03693319 VS |
1042 | // NB: a trick - we adjust selFromPos to be upper left or bottom |
1043 | // right corner of the first cell of the selection depending | |
1044 | // on whether the mouse is moving to the right or to the left. | |
1045 | // This gives us more "natural" behaviour when selecting | |
1046 | // a line (specifically, first cell of the next line is not | |
1047 | // included if you drag selection from left to right over | |
1048 | // entire line): | |
1049 | wxPoint dirFromPos; | |
1050 | if ( !m_tmpSelFromCell ) | |
1051 | { | |
1052 | dirFromPos = m_tmpSelFromPos; | |
1053 | } | |
1054 | else | |
1055 | { | |
1056 | dirFromPos = m_tmpSelFromCell->GetAbsPos(); | |
1057 | if ( x < m_tmpSelFromPos.x ) | |
1058 | { | |
1059 | dirFromPos.x += m_tmpSelFromCell->GetWidth(); | |
1060 | dirFromPos.y += m_tmpSelFromCell->GetHeight(); | |
1061 | } | |
1062 | } | |
d1da8872 | 1063 | bool goingDown = dirFromPos.y < y || |
03693319 VS |
1064 | (dirFromPos.y == y && dirFromPos.x < x); |
1065 | ||
1066 | // determine selection span: | |
748418c0 | 1067 | if ( /*still*/ !m_tmpSelFromCell ) |
adf2eb2d VS |
1068 | { |
1069 | if (goingDown) | |
1070 | { | |
5a1597e9 VS |
1071 | m_tmpSelFromCell = m_Cell->FindCellByPos( |
1072 | m_tmpSelFromPos.x,m_tmpSelFromPos.y, | |
1073 | wxHTML_FIND_NEAREST_AFTER); | |
adf2eb2d VS |
1074 | if (!m_tmpSelFromCell) |
1075 | m_tmpSelFromCell = m_Cell->GetFirstTerminal(); | |
1076 | } | |
1077 | else | |
1078 | { | |
5a1597e9 VS |
1079 | m_tmpSelFromCell = m_Cell->FindCellByPos( |
1080 | m_tmpSelFromPos.x,m_tmpSelFromPos.y, | |
1081 | wxHTML_FIND_NEAREST_BEFORE); | |
adf2eb2d VS |
1082 | if (!m_tmpSelFromCell) |
1083 | m_tmpSelFromCell = m_Cell->GetLastTerminal(); | |
1084 | } | |
1085 | } | |
1086 | ||
1087 | wxHtmlCell *selcell = cell; | |
1088 | if (!selcell) | |
1089 | { | |
1090 | if (goingDown) | |
1091 | { | |
1092 | selcell = m_Cell->FindCellByPos(x, y, | |
03693319 | 1093 | wxHTML_FIND_NEAREST_BEFORE); |
adf2eb2d VS |
1094 | if (!selcell) |
1095 | selcell = m_Cell->GetLastTerminal(); | |
1096 | } | |
1097 | else | |
1098 | { | |
1099 | selcell = m_Cell->FindCellByPos(x, y, | |
03693319 | 1100 | wxHTML_FIND_NEAREST_AFTER); |
adf2eb2d VS |
1101 | if (!selcell) |
1102 | selcell = m_Cell->GetFirstTerminal(); | |
1103 | } | |
1104 | } | |
1105 | ||
1106 | // NB: it may *rarely* happen that the code above didn't find one | |
1107 | // of the cells, e.g. if wxHtmlWindow doesn't contain any | |
d659d703 | 1108 | // visible cells. |
adf2eb2d | 1109 | if ( selcell && m_tmpSelFromCell ) |
d659d703 | 1110 | { |
adf2eb2d VS |
1111 | if ( !m_selection ) |
1112 | { | |
1113 | // start selecting only if mouse movement was big enough | |
1114 | // (otherwise it was meant as mouse click, not selection): | |
1115 | const int PRECISION = 2; | |
1116 | wxPoint diff = m_tmpSelFromPos - wxPoint(x,y); | |
1117 | if (abs(diff.x) > PRECISION || abs(diff.y) > PRECISION) | |
1118 | { | |
1119 | m_selection = new wxHtmlSelection(); | |
1120 | } | |
1121 | } | |
1122 | if ( m_selection ) | |
1123 | { | |
e3774124 | 1124 | if ( m_tmpSelFromCell->IsBefore(selcell) ) |
adf2eb2d VS |
1125 | { |
1126 | m_selection->Set(m_tmpSelFromPos, m_tmpSelFromCell, | |
f30e67db | 1127 | wxPoint(x,y), selcell); } |
adf2eb2d VS |
1128 | else |
1129 | { | |
1130 | m_selection->Set(wxPoint(x,y), selcell, | |
1131 | m_tmpSelFromPos, m_tmpSelFromCell); | |
1132 | } | |
1338c59a VS |
1133 | m_selection->ClearPrivPos(); |
1134 | Refresh(); | |
adf2eb2d VS |
1135 | } |
1136 | } | |
1137 | } | |
d659d703 | 1138 | |
adf2eb2d | 1139 | // handle cursor and status bar text changes: |
f6010d8f | 1140 | if ( cell != m_tmpLastCell ) |
e421922f | 1141 | { |
f6010d8f | 1142 | wxHtmlLinkInfo *lnk = cell ? cell->GetLink(x, y) : NULL; |
77bae5e2 VS |
1143 | wxCursor cur; |
1144 | if (cell) | |
1145 | cur = cell->GetCursor(); | |
1146 | else | |
1147 | cur = *wxSTANDARD_CURSOR; | |
1148 | SetCursor(cur); | |
f6010d8f VZ |
1149 | |
1150 | if (lnk != m_tmpLastLink) | |
e421922f | 1151 | { |
67a99992 | 1152 | #if wxUSE_STATUSBAR |
f6010d8f VZ |
1153 | if (lnk == NULL) |
1154 | { | |
f6010d8f | 1155 | if (m_RelatedStatusBar != -1) |
31eefb99 VS |
1156 | m_RelatedFrame->SetStatusText(wxEmptyString, |
1157 | m_RelatedStatusBar); | |
f6010d8f VZ |
1158 | } |
1159 | else | |
1160 | { | |
f6010d8f | 1161 | if (m_RelatedStatusBar != -1) |
31eefb99 VS |
1162 | m_RelatedFrame->SetStatusText(lnk->GetHref(), |
1163 | m_RelatedStatusBar); | |
f6010d8f | 1164 | } |
67a99992 | 1165 | #endif // wxUSE_STATUSBAR |
f6010d8f | 1166 | m_tmpLastLink = lnk; |
622ea783 | 1167 | } |
f6010d8f VZ |
1168 | |
1169 | m_tmpLastCell = cell; | |
5526e819 | 1170 | } |
f6010d8f VZ |
1171 | else // mouse moved but stayed in the same cell |
1172 | { | |
1173 | if ( cell ) | |
1174 | OnCellMouseHover(cell, x, y); | |
1175 | } | |
1176 | ||
d1da8872 | 1177 | m_tmpMouseMoved = false; |
5526e819 VS |
1178 | } |
1179 | } | |
1180 | ||
e3774124 | 1181 | #if wxUSE_CLIPBOARD |
1338c59a VS |
1182 | void wxHtmlWindow::StopAutoScrolling() |
1183 | { | |
1184 | if ( m_timerAutoScroll ) | |
1185 | { | |
1186 | wxDELETE(m_timerAutoScroll); | |
1187 | } | |
1188 | } | |
1189 | ||
1190 | void wxHtmlWindow::OnMouseEnter(wxMouseEvent& event) | |
1191 | { | |
1192 | StopAutoScrolling(); | |
1193 | event.Skip(); | |
1194 | } | |
1195 | ||
1196 | void wxHtmlWindow::OnMouseLeave(wxMouseEvent& event) | |
1197 | { | |
1198 | // don't prevent the usual processing of the event from taking place | |
1199 | event.Skip(); | |
1200 | ||
1201 | // when a captured mouse leave a scrolled window we start generate | |
1202 | // scrolling events to allow, for example, extending selection beyond the | |
1203 | // visible area in some controls | |
1204 | if ( wxWindow::GetCapture() == this ) | |
1205 | { | |
1206 | // where is the mouse leaving? | |
1207 | int pos, orient; | |
1208 | wxPoint pt = event.GetPosition(); | |
1209 | if ( pt.x < 0 ) | |
1210 | { | |
1211 | orient = wxHORIZONTAL; | |
1212 | pos = 0; | |
1213 | } | |
1214 | else if ( pt.y < 0 ) | |
1215 | { | |
1216 | orient = wxVERTICAL; | |
1217 | pos = 0; | |
1218 | } | |
1219 | else // we're lower or to the right of the window | |
1220 | { | |
1221 | wxSize size = GetClientSize(); | |
1222 | if ( pt.x > size.x ) | |
1223 | { | |
1224 | orient = wxHORIZONTAL; | |
1225 | pos = GetVirtualSize().x / wxHTML_SCROLL_STEP; | |
1226 | } | |
1227 | else if ( pt.y > size.y ) | |
1228 | { | |
1229 | orient = wxVERTICAL; | |
1230 | pos = GetVirtualSize().y / wxHTML_SCROLL_STEP; | |
1231 | } | |
1232 | else // this should be impossible | |
1233 | { | |
1234 | // but seems to happen sometimes under wxMSW - maybe it's a bug | |
1235 | // there but for now just ignore it | |
1236 | ||
1237 | //wxFAIL_MSG( _T("can't understand where has mouse gone") ); | |
1238 | ||
1239 | return; | |
1240 | } | |
1241 | } | |
1242 | ||
1243 | // only start the auto scroll timer if the window can be scrolled in | |
1244 | // this direction | |
1245 | if ( !HasScrollbar(orient) ) | |
1246 | return; | |
1247 | ||
1248 | delete m_timerAutoScroll; | |
1249 | m_timerAutoScroll = new wxHtmlWinAutoScrollTimer | |
1250 | ( | |
1251 | this, | |
1252 | pos == 0 ? wxEVT_SCROLLWIN_LINEUP | |
1253 | : wxEVT_SCROLLWIN_LINEDOWN, | |
1254 | pos, | |
1255 | orient | |
1256 | ); | |
1257 | m_timerAutoScroll->Start(50); // FIXME: make configurable | |
1258 | } | |
1259 | } | |
1260 | ||
e3774124 VS |
1261 | void wxHtmlWindow::OnKeyUp(wxKeyEvent& event) |
1262 | { | |
31eefb99 VS |
1263 | if ( IsSelectionEnabled() && |
1264 | event.GetKeyCode() == 'C' && event.ControlDown() ) | |
e3774124 | 1265 | { |
5de65c69 | 1266 | (void) CopySelection(); |
e3774124 | 1267 | } |
e3774124 VS |
1268 | } |
1269 | ||
fc7a2a60 | 1270 | void wxHtmlWindow::OnCopy(wxCommandEvent& WXUNUSED(event)) |
e3774124 | 1271 | { |
5de65c69 | 1272 | (void) CopySelection(); |
e3774124 | 1273 | } |
d659d703 | 1274 | |
31eefb99 VS |
1275 | void wxHtmlWindow::OnDoubleClick(wxMouseEvent& event) |
1276 | { | |
1277 | // select word under cursor: | |
1278 | if ( IsSelectionEnabled() ) | |
1279 | { | |
0994d968 | 1280 | SelectWord(CalcUnscrolledPosition(event.GetPosition())); |
d659d703 | 1281 | |
5de65c69 | 1282 | (void) CopySelection(Primary); |
d659d703 | 1283 | |
0994d968 | 1284 | m_lastDoubleClick = wxGetLocalTimeMillis(); |
31eefb99 VS |
1285 | } |
1286 | else | |
1287 | event.Skip(); | |
1288 | } | |
0994d968 VS |
1289 | |
1290 | void wxHtmlWindow::SelectWord(const wxPoint& pos) | |
1291 | { | |
2a536376 | 1292 | if ( m_Cell ) |
0994d968 | 1293 | { |
2a536376 VS |
1294 | wxHtmlCell *cell = m_Cell->FindCellByPos(pos.x, pos.y); |
1295 | if ( cell ) | |
1296 | { | |
1297 | delete m_selection; | |
1298 | m_selection = new wxHtmlSelection(); | |
1299 | m_selection->Set(cell, cell); | |
1300 | RefreshRect(wxRect(CalcScrolledPosition(cell->GetAbsPos()), | |
1301 | wxSize(cell->GetWidth(), cell->GetHeight()))); | |
1302 | } | |
0994d968 VS |
1303 | } |
1304 | } | |
1305 | ||
1306 | void wxHtmlWindow::SelectLine(const wxPoint& pos) | |
1307 | { | |
2a536376 | 1308 | if ( m_Cell ) |
0994d968 | 1309 | { |
2a536376 VS |
1310 | wxHtmlCell *cell = m_Cell->FindCellByPos(pos.x, pos.y); |
1311 | if ( cell ) | |
0994d968 | 1312 | { |
2a536376 VS |
1313 | // We use following heuristic to find a "line": let the line be all |
1314 | // cells in same container as the cell under mouse cursor that are | |
1315 | // neither completely above nor completely bellow the clicked cell | |
1316 | // (i.e. are likely to be words positioned on same line of text). | |
1317 | ||
1318 | int y1 = cell->GetAbsPos().y; | |
1319 | int y2 = y1 + cell->GetHeight(); | |
1320 | int y; | |
1321 | const wxHtmlCell *c; | |
1322 | const wxHtmlCell *before = NULL; | |
1323 | const wxHtmlCell *after = NULL; | |
1324 | ||
1325 | // find last cell of line: | |
1326 | for ( c = cell->GetNext(); c; c = c->GetNext()) | |
1327 | { | |
1328 | y = c->GetAbsPos().y; | |
1329 | if ( y + c->GetHeight() > y1 && y < y2 ) | |
1330 | after = c; | |
1331 | else | |
1332 | break; | |
1333 | } | |
1334 | if ( !after ) | |
1335 | after = cell; | |
0994d968 | 1336 | |
2a536376 VS |
1337 | // find first cell of line: |
1338 | for ( c = cell->GetParent()->GetFirstChild(); | |
1339 | c && c != cell; c = c->GetNext()) | |
0994d968 | 1340 | { |
2a536376 VS |
1341 | y = c->GetAbsPos().y; |
1342 | if ( y + c->GetHeight() > y1 && y < y2 ) | |
1343 | { | |
1344 | if ( ! before ) | |
1345 | before = c; | |
1346 | } | |
1347 | else | |
1348 | before = NULL; | |
0994d968 | 1349 | } |
2a536376 VS |
1350 | if ( !before ) |
1351 | before = cell; | |
1352 | ||
1353 | delete m_selection; | |
1354 | m_selection = new wxHtmlSelection(); | |
1355 | m_selection->Set(before, after); | |
1356 | ||
1357 | Refresh(); | |
0994d968 | 1358 | } |
2a536376 VS |
1359 | } |
1360 | } | |
d659d703 | 1361 | |
2a536376 VS |
1362 | void wxHtmlWindow::SelectAll() |
1363 | { | |
1364 | if ( m_Cell ) | |
1365 | { | |
0994d968 VS |
1366 | delete m_selection; |
1367 | m_selection = new wxHtmlSelection(); | |
2a536376 | 1368 | m_selection->Set(m_Cell->GetFirstTerminal(), m_Cell->GetLastTerminal()); |
0994d968 VS |
1369 | Refresh(); |
1370 | } | |
1371 | } | |
2a536376 | 1372 | |
d659d703 | 1373 | #endif // wxUSE_CLIPBOARD |
e3774124 VS |
1374 | |
1375 | ||
5526e819 | 1376 | |
bfb9ee96 | 1377 | IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor,wxObject) |
5526e819 | 1378 | |
786a2425 SC |
1379 | #if wxUSE_EXTENDED_RTTI |
1380 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxHtmlWindow, wxScrolledWindow,"wx/html/htmlwin.h") | |
1381 | ||
459f2add | 1382 | wxBEGIN_PROPERTIES_TABLE(wxHtmlWindow) |
786a2425 | 1383 | /* |
d1da8872 WS |
1384 | TODO PROPERTIES |
1385 | style , wxHW_SCROLLBAR_AUTO | |
1386 | borders , (dimension) | |
1387 | url , string | |
1388 | htmlcode , string | |
786a2425 | 1389 | */ |
459f2add | 1390 | wxEND_PROPERTIES_TABLE() |
786a2425 | 1391 | |
459f2add SC |
1392 | wxBEGIN_HANDLERS_TABLE(wxHtmlWindow) |
1393 | wxEND_HANDLERS_TABLE() | |
786a2425 | 1394 | |
d1da8872 | 1395 | wxCONSTRUCTOR_5( wxHtmlWindow , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle ) |
786a2425 | 1396 | #else |
5526e819 | 1397 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow,wxScrolledWindow) |
786a2425 | 1398 | #endif |
5526e819 VS |
1399 | |
1400 | BEGIN_EVENT_TABLE(wxHtmlWindow, wxScrolledWindow) | |
1401 | EVT_SIZE(wxHtmlWindow::OnSize) | |
adf2eb2d VS |
1402 | EVT_LEFT_DOWN(wxHtmlWindow::OnMouseDown) |
1403 | EVT_LEFT_UP(wxHtmlWindow::OnMouseUp) | |
1404 | EVT_RIGHT_UP(wxHtmlWindow::OnMouseUp) | |
31d8b4ad | 1405 | EVT_MOTION(wxHtmlWindow::OnMouseMove) |
1338c59a VS |
1406 | EVT_ERASE_BACKGROUND(wxHtmlWindow::OnEraseBackground) |
1407 | EVT_PAINT(wxHtmlWindow::OnPaint) | |
e3774124 | 1408 | #if wxUSE_CLIPBOARD |
31eefb99 | 1409 | EVT_LEFT_DCLICK(wxHtmlWindow::OnDoubleClick) |
1338c59a VS |
1410 | EVT_ENTER_WINDOW(wxHtmlWindow::OnMouseEnter) |
1411 | EVT_LEAVE_WINDOW(wxHtmlWindow::OnMouseLeave) | |
e3774124 VS |
1412 | EVT_KEY_UP(wxHtmlWindow::OnKeyUp) |
1413 | EVT_MENU(wxID_COPY, wxHtmlWindow::OnCopy) | |
d659d703 | 1414 | #endif // wxUSE_CLIPBOARD |
5526e819 VS |
1415 | END_EVENT_TABLE() |
1416 | ||
1417 | ||
1418 | ||
1419 | ||
1420 | ||
a76015e6 VS |
1421 | // A module to allow initialization/cleanup |
1422 | // without calling these functions from app.cpp or from | |
1423 | // the user's application. | |
1424 | ||
1425 | class wxHtmlWinModule: public wxModule | |
1426 | { | |
1427 | DECLARE_DYNAMIC_CLASS(wxHtmlWinModule) | |
1428 | public: | |
1429 | wxHtmlWinModule() : wxModule() {} | |
d1da8872 | 1430 | bool OnInit() { return true; } |
a76015e6 VS |
1431 | void OnExit() { wxHtmlWindow::CleanUpStatics(); } |
1432 | }; | |
1433 | ||
1434 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule, wxModule) | |
1435 | ||
5526e819 | 1436 | |
0cecad31 VS |
1437 | // This hack forces the linker to always link in m_* files |
1438 | // (wxHTML doesn't work without handlers from these files) | |
1439 | #include "wx/html/forcelnk.h" | |
1440 | FORCE_WXHTML_MODULES() | |
5526e819 | 1441 | |
d659d703 VZ |
1442 | #endif // wxUSE_HTML |
1443 |