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