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