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