1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtmlWindow class for parsing & displaying HTML (implementation)
4 // Author: Vaclav Slavik
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #include "wx/wxprec.h"
13 #if wxUSE_HTML && wxUSE_STREAMS
22 #include "wx/dcclient.h"
26 #include "wx/html/htmlwin.h"
27 #include "wx/html/htmlproc.h"
29 #include "wx/clipbrd.h"
30 #include "wx/dataobj.h"
32 #include "wx/dcmemory.h"
33 #include "wx/settings.h"
35 #include "wx/arrimpl.cpp"
36 #include "wx/listimpl.cpp"
40 // ----------------------------------------------------------------------------
41 // wxHtmlWinAutoScrollTimer: the timer used to generate a stream of scroll
42 // events when a captured mouse is held outside the window
43 // ----------------------------------------------------------------------------
45 class wxHtmlWinAutoScrollTimer
: public wxTimer
48 wxHtmlWinAutoScrollTimer(wxScrolledWindow
*win
,
49 wxEventType eventTypeToSend
,
53 m_eventType
= eventTypeToSend
;
58 virtual void Notify();
61 wxScrolledWindow
*m_win
;
62 wxEventType m_eventType
;
66 DECLARE_NO_COPY_CLASS(wxHtmlWinAutoScrollTimer
)
69 void wxHtmlWinAutoScrollTimer::Notify()
71 // only do all this as long as the window is capturing the mouse
72 if ( wxWindow::GetCapture() != m_win
)
76 else // we still capture the mouse, continue generating events
78 // first scroll the window if we are allowed to do it
79 wxScrollWinEvent
event1(m_eventType
, m_pos
, m_orient
);
80 event1
.SetEventObject(m_win
);
81 if ( m_win
->GetEventHandler()->ProcessEvent(event1
) )
83 // and then send a pseudo mouse-move event to refresh the selection
84 wxMouseEvent
event2(wxEVT_MOTION
);
85 wxGetMousePosition(&event2
.m_x
, &event2
.m_y
);
87 // the mouse event coordinates should be client, not screen as
88 // returned by wxGetMousePosition
89 wxWindow
*parentTop
= m_win
;
90 while ( parentTop
->GetParent() )
91 parentTop
= parentTop
->GetParent();
92 wxPoint ptOrig
= parentTop
->GetPosition();
93 event2
.m_x
-= ptOrig
.x
;
94 event2
.m_y
-= ptOrig
.y
;
96 event2
.SetEventObject(m_win
);
98 // FIXME: we don't fill in the other members - ok?
99 m_win
->GetEventHandler()->ProcessEvent(event2
);
101 else // can't scroll further, stop
108 #endif // wxUSE_CLIPBOARD
112 //-----------------------------------------------------------------------------
114 //-----------------------------------------------------------------------------
116 // item of history list
117 class WXDLLIMPEXP_HTML wxHtmlHistoryItem
120 wxHtmlHistoryItem(const wxString
& p
, const wxString
& a
) {m_Page
= p
, m_Anchor
= a
, m_Pos
= 0;}
121 int GetPos() const {return m_Pos
;}
122 void SetPos(int p
) {m_Pos
= p
;}
123 const wxString
& GetPage() const {return m_Page
;}
124 const wxString
& GetAnchor() const {return m_Anchor
;}
133 //-----------------------------------------------------------------------------
134 // our private arrays:
135 //-----------------------------------------------------------------------------
137 WX_DECLARE_OBJARRAY(wxHtmlHistoryItem
, wxHtmlHistoryArray
);
138 WX_DEFINE_OBJARRAY(wxHtmlHistoryArray
)
140 WX_DECLARE_LIST(wxHtmlProcessor
, wxHtmlProcessorList
);
141 WX_DEFINE_LIST(wxHtmlProcessorList
)
143 //-----------------------------------------------------------------------------
144 // wxHtmlWindowMouseHelper
145 //-----------------------------------------------------------------------------
147 wxHtmlWindowMouseHelper::wxHtmlWindowMouseHelper(wxHtmlWindowInterface
*iface
)
148 : m_tmpMouseMoved(false),
155 void wxHtmlWindowMouseHelper::HandleMouseMoved()
157 m_tmpMouseMoved
= true;
160 bool wxHtmlWindowMouseHelper::HandleMouseClick(wxHtmlCell
*rootCell
,
162 const wxMouseEvent
& event
)
167 wxHtmlCell
*cell
= rootCell
->FindCellByPos(pos
.x
, pos
.y
);
168 // this check is needed because FindCellByPos returns terminal cell and
169 // containers may have empty borders -- in this case NULL will be
174 // adjust the coordinates to be relative to this cell:
175 wxPoint relpos
= pos
- cell
->GetAbsPos(rootCell
);
177 return OnCellClicked(cell
, relpos
.x
, relpos
.y
, event
);
180 void wxHtmlWindowMouseHelper::HandleIdle(wxHtmlCell
*rootCell
,
183 wxHtmlCell
*cell
= rootCell
? rootCell
->FindCellByPos(pos
.x
, pos
.y
) : NULL
;
185 if (cell
!= m_tmpLastCell
)
187 wxHtmlLinkInfo
*lnk
= NULL
;
190 // adjust the coordinates to be relative to this cell:
191 wxPoint relpos
= pos
- cell
->GetAbsPos(rootCell
);
192 lnk
= cell
->GetLink(relpos
.x
, relpos
.y
);
197 cur
= cell
->GetCursor();
199 cur
= *wxSTANDARD_CURSOR
;
200 m_interface
->GetHTMLWindow()->SetCursor(cur
);
202 if (lnk
!= m_tmpLastLink
)
205 m_interface
->SetHTMLStatusText(lnk
->GetHref());
207 m_interface
->SetHTMLStatusText(wxEmptyString
);
212 m_tmpLastCell
= cell
;
214 else // mouse moved but stayed in the same cell
218 OnCellMouseHover(cell
, pos
.x
, pos
.y
);
222 m_tmpMouseMoved
= false;
225 bool wxHtmlWindowMouseHelper::OnCellClicked(wxHtmlCell
*cell
,
226 wxCoord x
, wxCoord y
,
227 const wxMouseEvent
& event
)
229 wxCHECK_MSG( cell
, false, _T("can't be called with NULL cell") );
231 return cell
->ProcessMouseClick(m_interface
, wxPoint(x
, y
), event
);
234 void wxHtmlWindowMouseHelper::OnCellMouseHover(wxHtmlCell
* WXUNUSED(cell
),
241 //-----------------------------------------------------------------------------
243 //-----------------------------------------------------------------------------
246 void wxHtmlWindow::Init()
248 m_tmpCanDrawLocks
= 0;
249 m_FS
= new wxFileSystem();
251 m_RelatedStatusBar
= -1;
252 #endif // wxUSE_STATUSBAR
253 m_RelatedFrame
= NULL
;
254 m_TitleFormat
= wxT("%s");
255 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
257 m_Parser
= new wxHtmlWinParser(this);
258 m_Parser
->SetFS(m_FS
);
261 m_History
= new wxHtmlHistoryArray
;
266 m_makingSelection
= false;
268 m_timerAutoScroll
= NULL
;
269 m_lastDoubleClick
= 0;
270 #endif // wxUSE_CLIPBOARD
272 m_eraseBgInOnPaint
= false;
273 m_tmpSelFromCell
= NULL
;
276 bool wxHtmlWindow::Create(wxWindow
*parent
, wxWindowID id
,
277 const wxPoint
& pos
, const wxSize
& size
,
278 long style
, const wxString
& name
)
280 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
281 style
| wxVSCROLL
| wxHSCROLL
,
286 SetPage(wxT("<html><body></body></html>"));
291 wxHtmlWindow::~wxHtmlWindow()
295 #endif // wxUSE_CLIPBOARD
304 WX_CLEAR_LIST(wxHtmlProcessorList
, *m_Processors
);
316 void wxHtmlWindow::SetRelatedFrame(wxFrame
* frame
, const wxString
& format
)
318 m_RelatedFrame
= frame
;
319 m_TitleFormat
= format
;
325 void wxHtmlWindow::SetRelatedStatusBar(int bar
)
327 m_RelatedStatusBar
= bar
;
329 #endif // wxUSE_STATUSBAR
333 void wxHtmlWindow::SetFonts(const wxString
& normal_face
, const wxString
& fixed_face
, const int *sizes
)
335 m_Parser
->SetFonts(normal_face
, fixed_face
, sizes
);
337 // re-layout the page after changing fonts:
338 DoSetPage(*(m_Parser
->GetSource()));
341 void wxHtmlWindow::SetStandardFonts(int size
,
342 const wxString
& normal_face
,
343 const wxString
& fixed_face
)
345 m_Parser
->SetStandardFonts(size
, normal_face
, fixed_face
);
347 // re-layout the page after changing fonts:
348 DoSetPage(*(m_Parser
->GetSource()));
351 bool wxHtmlWindow::SetPage(const wxString
& source
)
353 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
354 return DoSetPage(source
);
357 bool wxHtmlWindow::DoSetPage(const wxString
& source
)
359 wxString
newsrc(source
);
361 wxDELETE(m_selection
);
363 // we will soon delete all the cells, so clear pointers to them:
364 m_tmpSelFromCell
= NULL
;
366 // pass HTML through registered processors:
367 if (m_Processors
|| m_GlobalProcessors
)
369 wxHtmlProcessorList::compatibility_iterator nodeL
, nodeG
;
373 nodeL
= m_Processors
->GetFirst();
374 if ( m_GlobalProcessors
)
375 nodeG
= m_GlobalProcessors
->GetFirst();
377 // VS: there are two lists, global and local, both of them sorted by
378 // priority. Since we have to go through _both_ lists with
379 // decreasing priority, we "merge-sort" the lists on-line by
380 // processing that one of the two heads that has higher priority
381 // in every iteration
382 while (nodeL
|| nodeG
)
384 prL
= (nodeL
) ? nodeL
->GetData()->GetPriority() : -1;
385 prG
= (nodeG
) ? nodeG
->GetData()->GetPriority() : -1;
388 if (nodeL
->GetData()->IsEnabled())
389 newsrc
= nodeL
->GetData()->Process(newsrc
);
390 nodeL
= nodeL
->GetNext();
394 if (nodeG
->GetData()->IsEnabled())
395 newsrc
= nodeG
->GetData()->Process(newsrc
);
396 nodeG
= nodeG
->GetNext();
401 // ...and run the parser on it:
402 wxClientDC
*dc
= new wxClientDC(this);
403 dc
->SetMapMode(wxMM_TEXT
);
404 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
405 SetBackgroundImage(wxNullBitmap
);
413 m_Cell
= (wxHtmlContainerCell
*) m_Parser
->Parse(newsrc
);
415 m_Cell
->SetIndent(m_Borders
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
416 m_Cell
->SetAlignHor(wxHTML_ALIGN_CENTER
);
418 if (m_tmpCanDrawLocks
== 0)
423 bool wxHtmlWindow::AppendToPage(const wxString
& source
)
425 return DoSetPage(*(GetParser()->GetSource()) + source
);
428 bool wxHtmlWindow::LoadPage(const wxString
& location
)
430 wxBusyCursor busyCursor
;
434 bool needs_refresh
= false;
437 if (m_HistoryOn
&& (m_HistoryPos
!= -1))
439 // store scroll position into history item:
441 GetViewStart(&x
, &y
);
442 (*m_History
)[m_HistoryPos
].SetPos(y
);
445 if (location
[0] == wxT('#'))
448 wxString anch
= location
.Mid(1) /*1 to end*/;
450 rt_val
= ScrollToAnchor(anch
);
453 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&& location
.BeforeFirst(wxT('#')) == m_OpenedPage
)
455 wxString anch
= location
.AfterFirst(wxT('#'));
457 rt_val
= ScrollToAnchor(anch
);
460 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&&
461 (m_FS
->GetPath() + location
.BeforeFirst(wxT('#'))) == m_OpenedPage
)
463 wxString anch
= location
.AfterFirst(wxT('#'));
465 rt_val
= ScrollToAnchor(anch
);
471 needs_refresh
= true;
474 if (m_RelatedStatusBar
!= -1)
476 m_RelatedFrame
->SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
479 #endif // wxUSE_STATUSBAR
481 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location
);
483 // try to interpret 'location' as filename instead of URL:
486 wxFileName
fn(location
);
487 wxString location2
= wxFileSystem::FileNameToURL(fn
);
488 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location2
);
493 wxLogError(_("Unable to open requested HTML document: %s"), location
.c_str());
500 wxList::compatibility_iterator node
;
501 wxString src
= wxEmptyString
;
504 if (m_RelatedStatusBar
!= -1)
506 wxString msg
= _("Loading : ") + location
;
507 m_RelatedFrame
->SetStatusText(msg
, m_RelatedStatusBar
);
510 #endif // wxUSE_STATUSBAR
512 node
= m_Filters
.GetFirst();
515 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
518 src
= h
->ReadFile(*f
);
521 node
= node
->GetNext();
523 if (src
== wxEmptyString
)
525 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
526 src
= m_DefaultFilter
->ReadFile(*f
);
529 m_FS
->ChangePathTo(f
->GetLocation());
530 rt_val
= SetPage(src
);
531 m_OpenedPage
= f
->GetLocation();
532 if (f
->GetAnchor() != wxEmptyString
)
534 ScrollToAnchor(f
->GetAnchor());
540 if (m_RelatedStatusBar
!= -1)
541 m_RelatedFrame
->SetStatusText(_("Done"), m_RelatedStatusBar
);
542 #endif // wxUSE_STATUSBAR
546 if (m_HistoryOn
) // add this page to history there:
548 int c
= m_History
->GetCount() - (m_HistoryPos
+ 1);
550 if (m_HistoryPos
< 0 ||
551 (*m_History
)[m_HistoryPos
].GetPage() != m_OpenedPage
||
552 (*m_History
)[m_HistoryPos
].GetAnchor() != m_OpenedAnchor
)
555 for (int i
= 0; i
< c
; i
++)
556 m_History
->RemoveAt(m_HistoryPos
);
557 m_History
->Add(new wxHtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
561 if (m_OpenedPageTitle
== wxEmptyString
)
562 OnSetTitle(wxFileNameFromPath(m_OpenedPage
));
576 bool wxHtmlWindow::LoadFile(const wxFileName
& filename
)
578 wxString url
= wxFileSystem::FileNameToURL(filename
);
579 return LoadPage(url
);
583 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
585 const wxHtmlCell
*c
= m_Cell
->Find(wxHTML_COND_ISANCHOR
, &anchor
);
588 wxLogWarning(_("HTML anchor %s does not exist."), anchor
.c_str());
595 for (y
= 0; c
!= NULL
; c
= c
->GetParent()) y
+= c
->GetPosY();
596 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
597 m_OpenedAnchor
= anchor
;
603 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
608 tit
.Printf(m_TitleFormat
, title
.c_str());
609 m_RelatedFrame
->SetTitle(tit
);
611 m_OpenedPageTitle
= title
;
618 void wxHtmlWindow::CreateLayout()
620 int ClientWidth
, ClientHeight
;
624 if (m_Style
& wxHW_SCROLLBAR_NEVER
)
626 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // always off
627 GetClientSize(&ClientWidth
, &ClientHeight
);
628 m_Cell
->Layout(ClientWidth
);
632 GetClientSize(&ClientWidth
, &ClientHeight
);
633 m_Cell
->Layout(ClientWidth
);
634 if (ClientHeight
< m_Cell
->GetHeight() + GetCharHeight())
637 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
638 m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
,
639 (m_Cell
->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
640 /*cheat: top-level frag is always container*/);
642 else /* we fit into window, no need for scrollbars */
644 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
645 GetClientSize(&ClientWidth
, &ClientHeight
);
646 m_Cell
->Layout(ClientWidth
); // ...and relayout
653 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
658 wxString p_fff
, p_ffn
;
660 if (path
!= wxEmptyString
)
662 oldpath
= cfg
->GetPath();
666 m_Borders
= cfg
->Read(wxT("wxHtmlWindow/Borders"), m_Borders
);
667 p_fff
= cfg
->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
668 p_ffn
= cfg
->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
669 for (int i
= 0; i
< 7; i
++)
671 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
672 p_fontsizes
[i
] = cfg
->Read(tmp
, m_Parser
->m_FontsSizes
[i
]);
674 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
676 if (path
!= wxEmptyString
)
677 cfg
->SetPath(oldpath
);
682 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
687 if (path
!= wxEmptyString
)
689 oldpath
= cfg
->GetPath();
693 cfg
->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders
);
694 cfg
->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
695 cfg
->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
696 for (int i
= 0; i
< 7; i
++)
698 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
699 cfg
->Write(tmp
, (long) m_Parser
->m_FontsSizes
[i
]);
702 if (path
!= wxEmptyString
)
703 cfg
->SetPath(oldpath
);
708 bool wxHtmlWindow::HistoryBack()
712 if (m_HistoryPos
< 1) return false;
714 // store scroll position into history item:
716 GetViewStart(&x
, &y
);
717 (*m_History
)[m_HistoryPos
].SetPos(y
);
719 // go to previous position:
722 l
= (*m_History
)[m_HistoryPos
].GetPage();
723 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
726 if (a
== wxEmptyString
) LoadPage(l
);
727 else LoadPage(l
+ wxT("#") + a
);
730 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
735 bool wxHtmlWindow::HistoryCanBack()
737 if (m_HistoryPos
< 1) return false;
742 bool wxHtmlWindow::HistoryForward()
746 if (m_HistoryPos
== -1) return false;
747 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return false;
749 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
752 l
= (*m_History
)[m_HistoryPos
].GetPage();
753 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
756 if (a
== wxEmptyString
) LoadPage(l
);
757 else LoadPage(l
+ wxT("#") + a
);
760 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
765 bool wxHtmlWindow::HistoryCanForward()
767 if (m_HistoryPos
== -1) return false;
768 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return false;
773 void wxHtmlWindow::HistoryClear()
779 void wxHtmlWindow::AddProcessor(wxHtmlProcessor
*processor
)
783 m_Processors
= new wxHtmlProcessorList
;
785 wxHtmlProcessorList::compatibility_iterator node
;
787 for (node
= m_Processors
->GetFirst(); node
; node
= node
->GetNext())
789 if (processor
->GetPriority() > node
->GetData()->GetPriority())
791 m_Processors
->Insert(node
, processor
);
795 m_Processors
->Append(processor
);
798 /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor
*processor
)
800 if (!m_GlobalProcessors
)
802 m_GlobalProcessors
= new wxHtmlProcessorList
;
804 wxHtmlProcessorList::compatibility_iterator node
;
806 for (node
= m_GlobalProcessors
->GetFirst(); node
; node
= node
->GetNext())
808 if (processor
->GetPriority() > node
->GetData()->GetPriority())
810 m_GlobalProcessors
->Insert(node
, processor
);
814 m_GlobalProcessors
->Append(processor
);
819 wxList
wxHtmlWindow::m_Filters
;
820 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
821 wxHtmlProcessorList
*wxHtmlWindow::m_GlobalProcessors
= NULL
;
823 void wxHtmlWindow::CleanUpStatics()
825 wxDELETE(m_DefaultFilter
);
826 WX_CLEAR_LIST(wxList
, m_Filters
);
827 if (m_GlobalProcessors
)
828 WX_CLEAR_LIST(wxHtmlProcessorList
, *m_GlobalProcessors
);
829 wxDELETE(m_GlobalProcessors
);
834 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
836 m_Filters
.Append(filter
);
840 bool wxHtmlWindow::IsSelectionEnabled() const
843 return !(m_Style
& wxHW_NO_SELECTION
);
851 wxString
wxHtmlWindow::DoSelectionToText(wxHtmlSelection
*sel
)
854 return wxEmptyString
;
858 const wxHtmlCell
*end
= sel
->GetToCell();
860 wxHtmlTerminalCellsInterator
i(sel
->GetFromCell(), end
);
863 text
<< i
->ConvertToText(sel
);
866 const wxHtmlCell
*prev
= *i
;
869 if ( prev
->GetParent() != i
->GetParent() )
871 text
<< i
->ConvertToText(*i
== end
? sel
: NULL
);
878 wxString
wxHtmlWindow::ToText()
883 sel
.Set(m_Cell
->GetFirstTerminal(), m_Cell
->GetLastTerminal());
884 return DoSelectionToText(&sel
);
887 return wxEmptyString
;
890 #endif // wxUSE_CLIPBOARD
892 bool wxHtmlWindow::CopySelection(ClipboardType t
)
897 #if defined(__UNIX__) && !defined(__WXMAC__)
898 wxTheClipboard
->UsePrimarySelection(t
== Primary
);
900 // Primary selection exists only under X11, so don't do anything under
901 // the other platforms when we try to access it
903 // TODO: this should be abstracted at wxClipboard level!
906 #endif // __UNIX__/!__UNIX__
908 if ( wxTheClipboard
->Open() )
910 const wxString
txt(SelectionToText());
911 wxTheClipboard
->SetData(new wxTextDataObject(txt
));
912 wxTheClipboard
->Close();
913 wxLogTrace(_T("wxhtmlselection"),
914 _("Copied to clipboard:\"%s\""), txt
.c_str());
921 #endif // wxUSE_CLIPBOARD
927 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
929 const wxMouseEvent
*e
= link
.GetEvent();
930 if (e
== NULL
|| e
->LeftUp())
931 LoadPage(link
.GetHref());
934 void wxHtmlWindow::OnEraseBackground(wxEraseEvent
& event
)
938 // don't even skip the event, if we don't have a bg bitmap we're going
939 // to overwrite background in OnPaint() below anyhow, so letting the
940 // default handling take place would only result in flicker, just set a
941 // flag to erase the background below
942 m_eraseBgInOnPaint
= true;
946 wxDC
& dc
= *event
.GetDC();
948 // if the image is not fully opaque, we have to erase the background before
949 // drawing it, however avoid doing it for opaque images as this would just
950 // result in extra flicker without any other effect as background is
951 // completely covered anyhow
952 if ( m_bmpBg
.GetMask() )
954 dc
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
958 const wxSize
sizeWin(GetClientSize());
959 const wxSize
sizeBmp(m_bmpBg
.GetWidth(), m_bmpBg
.GetHeight());
960 for ( wxCoord x
= 0; x
< sizeWin
.x
; x
+= sizeBmp
.x
)
962 for ( wxCoord y
= 0; y
< sizeWin
.y
; y
+= sizeBmp
.y
)
964 dc
.DrawBitmap(m_bmpBg
, x
, y
, true /* use mask */);
969 void wxHtmlWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
973 if (m_tmpCanDrawLocks
> 0 || m_Cell
== NULL
)
977 GetViewStart(&x
, &y
);
978 wxRect rect
= GetUpdateRegion().GetBox();
979 wxSize sz
= GetSize();
983 m_backBuffer
= new wxBitmap(sz
.x
, sz
.y
);
984 dcm
.SelectObject(*m_backBuffer
);
986 if ( m_eraseBgInOnPaint
)
988 dcm
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
991 m_eraseBgInOnPaint
= false;
993 else // someone has already erased the background, keep it
995 // preserve the existing background, otherwise we'd erase anything the
996 // user code had drawn in its EVT_ERASE_BACKGROUND handler when we do
997 // the Blit back below
998 dcm
.Blit(0, rect
.GetTop(),
999 sz
.x
, rect
.GetBottom() - rect
.GetTop() + 1,
1005 dcm
.SetMapMode(wxMM_TEXT
);
1006 dcm
.SetBackgroundMode(wxTRANSPARENT
);
1008 wxHtmlRenderingInfo rinfo
;
1009 wxDefaultHtmlRenderingStyle rstyle
;
1010 rinfo
.SetSelection(m_selection
);
1011 rinfo
.SetStyle(&rstyle
);
1012 m_Cell
->Draw(dcm
, 0, 0,
1013 y
* wxHTML_SCROLL_STEP
+ rect
.GetTop(),
1014 y
* wxHTML_SCROLL_STEP
+ rect
.GetBottom(),
1017 //#define DEBUG_HTML_SELECTION
1018 #ifdef DEBUG_HTML_SELECTION
1021 wxGetMousePosition(&xc
, &yc
);
1022 ScreenToClient(&xc
, &yc
);
1023 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
1024 wxHtmlCell
*at
= m_Cell
->FindCellByPos(x
, y
);
1025 wxHtmlCell
*before
=
1026 m_Cell
->FindCellByPos(x
, y
, wxHTML_FIND_NEAREST_BEFORE
);
1028 m_Cell
->FindCellByPos(x
, y
, wxHTML_FIND_NEAREST_AFTER
);
1030 dcm
.SetBrush(*wxTRANSPARENT_BRUSH
);
1031 dcm
.SetPen(*wxBLACK_PEN
);
1033 dcm
.DrawRectangle(at
->GetAbsPos(),
1034 wxSize(at
->GetWidth(),at
->GetHeight()));
1035 dcm
.SetPen(*wxGREEN_PEN
);
1037 dcm
.DrawRectangle(before
->GetAbsPos().x
+1, before
->GetAbsPos().y
+1,
1038 before
->GetWidth()-2,before
->GetHeight()-2);
1039 dcm
.SetPen(*wxRED_PEN
);
1041 dcm
.DrawRectangle(after
->GetAbsPos().x
+2, after
->GetAbsPos().y
+2,
1042 after
->GetWidth()-4,after
->GetHeight()-4);
1046 dcm
.SetDeviceOrigin(0,0);
1047 dc
.Blit(0, rect
.GetTop(),
1048 sz
.x
, rect
.GetBottom() - rect
.GetTop() + 1,
1056 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
1058 wxDELETE(m_backBuffer
);
1060 wxScrolledWindow::OnSize(event
);
1063 // Recompute selection if necessary:
1066 m_selection
->Set(m_selection
->GetFromCell(),
1067 m_selection
->GetToCell());
1068 m_selection
->ClearPrivPos();
1075 void wxHtmlWindow::OnMouseMove(wxMouseEvent
& WXUNUSED(event
))
1077 wxHtmlWindowMouseHelper::HandleMouseMoved();
1080 void wxHtmlWindow::OnMouseDown(wxMouseEvent
& event
)
1083 if ( event
.LeftDown() && IsSelectionEnabled() )
1085 const long TRIPLECLICK_LEN
= 200; // 0.2 sec after doubleclick
1086 if ( wxGetLocalTimeMillis() - m_lastDoubleClick
<= TRIPLECLICK_LEN
)
1088 SelectLine(CalcUnscrolledPosition(event
.GetPosition()));
1090 (void) CopySelection();
1094 m_makingSelection
= true;
1098 wxDELETE(m_selection
);
1101 m_tmpSelFromPos
= CalcUnscrolledPosition(event
.GetPosition());
1102 m_tmpSelFromCell
= NULL
;
1109 #endif // wxUSE_CLIPBOARD
1112 void wxHtmlWindow::OnMouseUp(wxMouseEvent
& event
)
1115 if ( m_makingSelection
)
1118 m_makingSelection
= false;
1120 // did the user move the mouse far enough from starting point?
1121 if ( CopySelection(Primary
) )
1123 // we don't want mouse up event that ended selecting to be
1124 // handled as mouse click and e.g. follow hyperlink:
1128 #endif // wxUSE_CLIPBOARD
1132 wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
1133 wxHtmlWindowMouseHelper::HandleMouseClick(m_Cell
, pos
, event
);
1138 void wxHtmlWindow::OnInternalIdle()
1140 wxWindow::OnInternalIdle();
1142 if (m_Cell
!= NULL
&& DidMouseMove())
1144 #ifdef DEBUG_HTML_SELECTION
1148 wxGetMousePosition(&xc
, &yc
);
1149 ScreenToClient(&xc
, &yc
);
1150 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
1152 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(x
, y
);
1154 // handle selection update:
1155 if ( m_makingSelection
)
1157 if ( !m_tmpSelFromCell
)
1158 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1159 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
);
1161 // NB: a trick - we adjust selFromPos to be upper left or bottom
1162 // right corner of the first cell of the selection depending
1163 // on whether the mouse is moving to the right or to the left.
1164 // This gives us more "natural" behaviour when selecting
1165 // a line (specifically, first cell of the next line is not
1166 // included if you drag selection from left to right over
1169 if ( !m_tmpSelFromCell
)
1171 dirFromPos
= m_tmpSelFromPos
;
1175 dirFromPos
= m_tmpSelFromCell
->GetAbsPos();
1176 if ( x
< m_tmpSelFromPos
.x
)
1178 dirFromPos
.x
+= m_tmpSelFromCell
->GetWidth();
1179 dirFromPos
.y
+= m_tmpSelFromCell
->GetHeight();
1182 bool goingDown
= dirFromPos
.y
< y
||
1183 (dirFromPos
.y
== y
&& dirFromPos
.x
< x
);
1185 // determine selection span:
1186 if ( /*still*/ !m_tmpSelFromCell
)
1190 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1191 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1192 wxHTML_FIND_NEAREST_AFTER
);
1193 if (!m_tmpSelFromCell
)
1194 m_tmpSelFromCell
= m_Cell
->GetFirstTerminal();
1198 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1199 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1200 wxHTML_FIND_NEAREST_BEFORE
);
1201 if (!m_tmpSelFromCell
)
1202 m_tmpSelFromCell
= m_Cell
->GetLastTerminal();
1206 wxHtmlCell
*selcell
= cell
;
1211 selcell
= m_Cell
->FindCellByPos(x
, y
,
1212 wxHTML_FIND_NEAREST_BEFORE
);
1214 selcell
= m_Cell
->GetLastTerminal();
1218 selcell
= m_Cell
->FindCellByPos(x
, y
,
1219 wxHTML_FIND_NEAREST_AFTER
);
1221 selcell
= m_Cell
->GetFirstTerminal();
1225 // NB: it may *rarely* happen that the code above didn't find one
1226 // of the cells, e.g. if wxHtmlWindow doesn't contain any
1228 if ( selcell
&& m_tmpSelFromCell
)
1232 // start selecting only if mouse movement was big enough
1233 // (otherwise it was meant as mouse click, not selection):
1234 const int PRECISION
= 2;
1235 wxPoint diff
= m_tmpSelFromPos
- wxPoint(x
,y
);
1236 if (abs(diff
.x
) > PRECISION
|| abs(diff
.y
) > PRECISION
)
1238 m_selection
= new wxHtmlSelection();
1243 if ( m_tmpSelFromCell
->IsBefore(selcell
) )
1245 m_selection
->Set(m_tmpSelFromPos
, m_tmpSelFromCell
,
1246 wxPoint(x
,y
), selcell
); }
1249 m_selection
->Set(wxPoint(x
,y
), selcell
,
1250 m_tmpSelFromPos
, m_tmpSelFromCell
);
1252 m_selection
->ClearPrivPos();
1258 // handle cursor and status bar text changes:
1260 // NB: because we're passing in 'cell' and not 'm_Cell' (so that the
1261 // leaf cell lookup isn't done twice), we need to adjust the
1262 // position for the new root:
1263 wxPoint
posInCell(x
, y
);
1265 posInCell
-= cell
->GetAbsPos();
1266 wxHtmlWindowMouseHelper::HandleIdle(cell
, posInCell
);
1271 void wxHtmlWindow::StopAutoScrolling()
1273 if ( m_timerAutoScroll
)
1275 wxDELETE(m_timerAutoScroll
);
1279 void wxHtmlWindow::OnMouseEnter(wxMouseEvent
& event
)
1281 StopAutoScrolling();
1285 void wxHtmlWindow::OnMouseLeave(wxMouseEvent
& event
)
1287 // don't prevent the usual processing of the event from taking place
1290 // when a captured mouse leave a scrolled window we start generate
1291 // scrolling events to allow, for example, extending selection beyond the
1292 // visible area in some controls
1293 if ( wxWindow::GetCapture() == this )
1295 // where is the mouse leaving?
1297 wxPoint pt
= event
.GetPosition();
1300 orient
= wxHORIZONTAL
;
1303 else if ( pt
.y
< 0 )
1305 orient
= wxVERTICAL
;
1308 else // we're lower or to the right of the window
1310 wxSize size
= GetClientSize();
1311 if ( pt
.x
> size
.x
)
1313 orient
= wxHORIZONTAL
;
1314 pos
= GetVirtualSize().x
/ wxHTML_SCROLL_STEP
;
1316 else if ( pt
.y
> size
.y
)
1318 orient
= wxVERTICAL
;
1319 pos
= GetVirtualSize().y
/ wxHTML_SCROLL_STEP
;
1321 else // this should be impossible
1323 // but seems to happen sometimes under wxMSW - maybe it's a bug
1324 // there but for now just ignore it
1326 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1332 // only start the auto scroll timer if the window can be scrolled in
1334 if ( !HasScrollbar(orient
) )
1337 delete m_timerAutoScroll
;
1338 m_timerAutoScroll
= new wxHtmlWinAutoScrollTimer
1341 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1342 : wxEVT_SCROLLWIN_LINEDOWN
,
1346 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1350 void wxHtmlWindow::OnKeyUp(wxKeyEvent
& event
)
1352 if ( IsSelectionEnabled() && event
.GetKeyCode() == 'C' && event
.CmdDown() )
1354 (void) CopySelection();
1358 void wxHtmlWindow::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1360 (void) CopySelection();
1363 void wxHtmlWindow::OnDoubleClick(wxMouseEvent
& event
)
1365 // select word under cursor:
1366 if ( IsSelectionEnabled() )
1368 SelectWord(CalcUnscrolledPosition(event
.GetPosition()));
1370 (void) CopySelection(Primary
);
1372 m_lastDoubleClick
= wxGetLocalTimeMillis();
1378 void wxHtmlWindow::SelectWord(const wxPoint
& pos
)
1382 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1386 m_selection
= new wxHtmlSelection();
1387 m_selection
->Set(cell
, cell
);
1388 RefreshRect(wxRect(CalcScrolledPosition(cell
->GetAbsPos()),
1389 wxSize(cell
->GetWidth(), cell
->GetHeight())));
1394 void wxHtmlWindow::SelectLine(const wxPoint
& pos
)
1398 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1401 // We use following heuristic to find a "line": let the line be all
1402 // cells in same container as the cell under mouse cursor that are
1403 // neither completely above nor completely bellow the clicked cell
1404 // (i.e. are likely to be words positioned on same line of text).
1406 int y1
= cell
->GetAbsPos().y
;
1407 int y2
= y1
+ cell
->GetHeight();
1409 const wxHtmlCell
*c
;
1410 const wxHtmlCell
*before
= NULL
;
1411 const wxHtmlCell
*after
= NULL
;
1413 // find last cell of line:
1414 for ( c
= cell
->GetNext(); c
; c
= c
->GetNext())
1416 y
= c
->GetAbsPos().y
;
1417 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1425 // find first cell of line:
1426 for ( c
= cell
->GetParent()->GetFirstChild();
1427 c
&& c
!= cell
; c
= c
->GetNext())
1429 y
= c
->GetAbsPos().y
;
1430 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1442 m_selection
= new wxHtmlSelection();
1443 m_selection
->Set(before
, after
);
1450 void wxHtmlWindow::SelectAll()
1455 m_selection
= new wxHtmlSelection();
1456 m_selection
->Set(m_Cell
->GetFirstTerminal(), m_Cell
->GetLastTerminal());
1461 #endif // wxUSE_CLIPBOARD
1465 IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor
,wxObject
)
1467 #if wxUSE_EXTENDED_RTTI
1468 IMPLEMENT_DYNAMIC_CLASS_XTI(wxHtmlWindow
, wxScrolledWindow
,"wx/html/htmlwin.h")
1470 wxBEGIN_PROPERTIES_TABLE(wxHtmlWindow
)
1473 style , wxHW_SCROLLBAR_AUTO
1474 borders , (dimension)
1478 wxEND_PROPERTIES_TABLE()
1480 wxBEGIN_HANDLERS_TABLE(wxHtmlWindow
)
1481 wxEND_HANDLERS_TABLE()
1483 wxCONSTRUCTOR_5( wxHtmlWindow
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
1485 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
1488 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
1489 EVT_SIZE(wxHtmlWindow::OnSize
)
1490 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseDown
)
1491 EVT_LEFT_UP(wxHtmlWindow::OnMouseUp
)
1492 EVT_RIGHT_UP(wxHtmlWindow::OnMouseUp
)
1493 EVT_MOTION(wxHtmlWindow::OnMouseMove
)
1494 EVT_ERASE_BACKGROUND(wxHtmlWindow::OnEraseBackground
)
1495 EVT_PAINT(wxHtmlWindow::OnPaint
)
1497 EVT_LEFT_DCLICK(wxHtmlWindow::OnDoubleClick
)
1498 EVT_ENTER_WINDOW(wxHtmlWindow::OnMouseEnter
)
1499 EVT_LEAVE_WINDOW(wxHtmlWindow::OnMouseLeave
)
1500 EVT_KEY_UP(wxHtmlWindow::OnKeyUp
)
1501 EVT_MENU(wxID_COPY
, wxHtmlWindow::OnCopy
)
1502 #endif // wxUSE_CLIPBOARD
1505 //-----------------------------------------------------------------------------
1506 // wxHtmlWindowInterface implementation in wxHtmlWindow
1507 //-----------------------------------------------------------------------------
1509 void wxHtmlWindow::SetHTMLWindowTitle(const wxString
& title
)
1514 void wxHtmlWindow::OnHTMLLinkClicked(const wxHtmlLinkInfo
& link
)
1516 OnLinkClicked(link
);
1519 wxHtmlOpeningStatus
wxHtmlWindow::OnHTMLOpeningURL(wxHtmlURLType type
,
1520 const wxString
& url
,
1521 wxString
*redirect
) const
1523 return OnOpeningURL(type
, url
, redirect
);
1526 wxPoint
wxHtmlWindow::HTMLCoordsToWindow(wxHtmlCell
*WXUNUSED(cell
),
1527 const wxPoint
& pos
) const
1529 return CalcScrolledPosition(pos
);
1532 wxWindow
* wxHtmlWindow::GetHTMLWindow()
1537 wxColour
wxHtmlWindow::GetHTMLBackgroundColour() const
1539 return GetBackgroundColour();
1542 void wxHtmlWindow::SetHTMLBackgroundColour(const wxColour
& clr
)
1544 SetBackgroundColour(clr
);
1547 void wxHtmlWindow::SetHTMLBackgroundImage(const wxBitmap
& bmpBg
)
1549 SetBackgroundImage(bmpBg
);
1552 void wxHtmlWindow::SetHTMLStatusText(const wxString
& text
)
1555 if (m_RelatedStatusBar
!= -1)
1556 m_RelatedFrame
->SetStatusText(text
, m_RelatedStatusBar
);
1557 #endif // wxUSE_STATUSBAR
1561 //-----------------------------------------------------------------------------
1563 //-----------------------------------------------------------------------------
1565 // A module to allow initialization/cleanup
1566 // without calling these functions from app.cpp or from
1567 // the user's application.
1569 class wxHtmlWinModule
: public wxModule
1571 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
1573 wxHtmlWinModule() : wxModule() {}
1574 bool OnInit() { return true; }
1575 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
1578 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
1581 // This hack forces the linker to always link in m_* files
1582 // (wxHTML doesn't work without handlers from these files)
1583 #include "wx/html/forcelnk.h"
1584 FORCE_WXHTML_MODULES()
1586 #endif // wxUSE_HTML