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