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