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