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 //-----------------------------------------------------------------------------
145 //-----------------------------------------------------------------------------
148 void wxHtmlWindow::Init()
150 m_tmpMouseMoved
= false;
151 m_tmpLastLink
= NULL
;
152 m_tmpLastCell
= NULL
;
153 m_tmpCanDrawLocks
= 0;
154 m_FS
= new wxFileSystem();
156 m_RelatedStatusBar
= -1;
157 #endif // wxUSE_STATUSBAR
158 m_RelatedFrame
= NULL
;
159 m_TitleFormat
= wxT("%s");
160 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
162 m_Parser
= new wxHtmlWinParser(this);
163 m_Parser
->SetFS(m_FS
);
166 m_History
= new wxHtmlHistoryArray
;
171 m_makingSelection
= false;
173 m_timerAutoScroll
= NULL
;
174 m_lastDoubleClick
= 0;
175 #endif // wxUSE_CLIPBOARD
177 m_eraseBgInOnPaint
= false;
178 m_tmpSelFromCell
= NULL
;
181 bool wxHtmlWindow::Create(wxWindow
*parent
, wxWindowID id
,
182 const wxPoint
& pos
, const wxSize
& size
,
183 long style
, const wxString
& name
)
185 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
186 style
| wxVSCROLL
| wxHSCROLL
,
191 SetPage(wxT("<html><body></body></html>"));
196 wxHtmlWindow::~wxHtmlWindow()
200 #endif // wxUSE_CLIPBOARD
209 WX_CLEAR_LIST(wxHtmlProcessorList
, *m_Processors
);
221 void wxHtmlWindow::SetRelatedFrame(wxFrame
* frame
, const wxString
& format
)
223 m_RelatedFrame
= frame
;
224 m_TitleFormat
= format
;
230 void wxHtmlWindow::SetRelatedStatusBar(int bar
)
232 m_RelatedStatusBar
= bar
;
234 #endif // wxUSE_STATUSBAR
238 void wxHtmlWindow::SetFonts(const wxString
& normal_face
, const wxString
& fixed_face
, const int *sizes
)
240 m_Parser
->SetFonts(normal_face
, fixed_face
, sizes
);
242 // re-layout the page after changing fonts:
243 DoSetPage(*(m_Parser
->GetSource()));
246 void wxHtmlWindow::SetStandardFonts(int size
,
247 const wxString
& normal_face
,
248 const wxString
& fixed_face
)
250 m_Parser
->SetStandardFonts(size
, normal_face
, fixed_face
);
252 // re-layout the page after changing fonts:
253 DoSetPage(*(m_Parser
->GetSource()));
256 bool wxHtmlWindow::SetPage(const wxString
& source
)
258 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
259 return DoSetPage(source
);
262 bool wxHtmlWindow::DoSetPage(const wxString
& source
)
264 wxString
newsrc(source
);
266 wxDELETE(m_selection
);
268 // we will soon delete all the cells, so clear pointers to them:
269 m_tmpSelFromCell
= NULL
;
271 // pass HTML through registered processors:
272 if (m_Processors
|| m_GlobalProcessors
)
274 wxHtmlProcessorList::compatibility_iterator nodeL
, nodeG
;
278 nodeL
= m_Processors
->GetFirst();
279 if ( m_GlobalProcessors
)
280 nodeG
= m_GlobalProcessors
->GetFirst();
282 // VS: there are two lists, global and local, both of them sorted by
283 // priority. Since we have to go through _both_ lists with
284 // decreasing priority, we "merge-sort" the lists on-line by
285 // processing that one of the two heads that has higher priority
286 // in every iteration
287 while (nodeL
|| nodeG
)
289 prL
= (nodeL
) ? nodeL
->GetData()->GetPriority() : -1;
290 prG
= (nodeG
) ? nodeG
->GetData()->GetPriority() : -1;
293 if (nodeL
->GetData()->IsEnabled())
294 newsrc
= nodeL
->GetData()->Process(newsrc
);
295 nodeL
= nodeL
->GetNext();
299 if (nodeG
->GetData()->IsEnabled())
300 newsrc
= nodeG
->GetData()->Process(newsrc
);
301 nodeG
= nodeG
->GetNext();
306 // ...and run the parser on it:
307 wxClientDC
*dc
= new wxClientDC(this);
308 dc
->SetMapMode(wxMM_TEXT
);
309 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
310 SetBackgroundImage(wxNullBitmap
);
318 m_Cell
= (wxHtmlContainerCell
*) m_Parser
->Parse(newsrc
);
320 m_Cell
->SetIndent(m_Borders
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
321 m_Cell
->SetAlignHor(wxHTML_ALIGN_CENTER
);
323 if (m_tmpCanDrawLocks
== 0)
328 bool wxHtmlWindow::AppendToPage(const wxString
& source
)
330 return DoSetPage(*(GetParser()->GetSource()) + source
);
333 bool wxHtmlWindow::LoadPage(const wxString
& location
)
335 wxBusyCursor busyCursor
;
339 bool needs_refresh
= false;
342 if (m_HistoryOn
&& (m_HistoryPos
!= -1))
344 // store scroll position into history item:
346 GetViewStart(&x
, &y
);
347 (*m_History
)[m_HistoryPos
].SetPos(y
);
350 if (location
[0] == wxT('#'))
353 wxString anch
= location
.Mid(1) /*1 to end*/;
355 rt_val
= ScrollToAnchor(anch
);
358 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&& location
.BeforeFirst(wxT('#')) == m_OpenedPage
)
360 wxString anch
= location
.AfterFirst(wxT('#'));
362 rt_val
= ScrollToAnchor(anch
);
365 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&&
366 (m_FS
->GetPath() + location
.BeforeFirst(wxT('#'))) == m_OpenedPage
)
368 wxString anch
= location
.AfterFirst(wxT('#'));
370 rt_val
= ScrollToAnchor(anch
);
376 needs_refresh
= true;
379 if (m_RelatedStatusBar
!= -1)
381 m_RelatedFrame
->SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
384 #endif // wxUSE_STATUSBAR
386 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location
);
388 // try to interpret 'location' as filename instead of URL:
391 wxFileName
fn(location
);
392 wxString location2
= wxFileSystem::FileNameToURL(fn
);
393 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location2
);
398 wxLogError(_("Unable to open requested HTML document: %s"), location
.c_str());
405 wxList::compatibility_iterator node
;
406 wxString src
= wxEmptyString
;
409 if (m_RelatedStatusBar
!= -1)
411 wxString msg
= _("Loading : ") + location
;
412 m_RelatedFrame
->SetStatusText(msg
, m_RelatedStatusBar
);
415 #endif // wxUSE_STATUSBAR
417 node
= m_Filters
.GetFirst();
420 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
423 src
= h
->ReadFile(*f
);
426 node
= node
->GetNext();
428 if (src
== wxEmptyString
)
430 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
431 src
= m_DefaultFilter
->ReadFile(*f
);
434 m_FS
->ChangePathTo(f
->GetLocation());
435 rt_val
= SetPage(src
);
436 m_OpenedPage
= f
->GetLocation();
437 if (f
->GetAnchor() != wxEmptyString
)
439 ScrollToAnchor(f
->GetAnchor());
445 if (m_RelatedStatusBar
!= -1)
446 m_RelatedFrame
->SetStatusText(_("Done"), m_RelatedStatusBar
);
447 #endif // wxUSE_STATUSBAR
451 if (m_HistoryOn
) // add this page to history there:
453 int c
= m_History
->GetCount() - (m_HistoryPos
+ 1);
455 if (m_HistoryPos
< 0 ||
456 (*m_History
)[m_HistoryPos
].GetPage() != m_OpenedPage
||
457 (*m_History
)[m_HistoryPos
].GetAnchor() != m_OpenedAnchor
)
460 for (int i
= 0; i
< c
; i
++)
461 m_History
->RemoveAt(m_HistoryPos
);
462 m_History
->Add(new wxHtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
466 if (m_OpenedPageTitle
== wxEmptyString
)
467 OnSetTitle(wxFileNameFromPath(m_OpenedPage
));
481 bool wxHtmlWindow::LoadFile(const wxFileName
& filename
)
483 wxString url
= wxFileSystem::FileNameToURL(filename
);
484 return LoadPage(url
);
488 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
490 const wxHtmlCell
*c
= m_Cell
->Find(wxHTML_COND_ISANCHOR
, &anchor
);
493 wxLogWarning(_("HTML anchor %s does not exist."), anchor
.c_str());
500 for (y
= 0; c
!= NULL
; c
= c
->GetParent()) y
+= c
->GetPosY();
501 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
502 m_OpenedAnchor
= anchor
;
508 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
513 tit
.Printf(m_TitleFormat
, title
.c_str());
514 m_RelatedFrame
->SetTitle(tit
);
516 m_OpenedPageTitle
= title
;
523 void wxHtmlWindow::CreateLayout()
525 int ClientWidth
, ClientHeight
;
529 if (m_Style
& wxHW_SCROLLBAR_NEVER
)
531 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // always off
532 GetClientSize(&ClientWidth
, &ClientHeight
);
533 m_Cell
->Layout(ClientWidth
);
537 GetClientSize(&ClientWidth
, &ClientHeight
);
538 m_Cell
->Layout(ClientWidth
);
539 if (ClientHeight
< m_Cell
->GetHeight() + GetCharHeight())
542 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
543 m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
,
544 (m_Cell
->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
545 /*cheat: top-level frag is always container*/);
547 else /* we fit into window, no need for scrollbars */
549 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
550 GetClientSize(&ClientWidth
, &ClientHeight
);
551 m_Cell
->Layout(ClientWidth
); // ...and relayout
558 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
563 wxString p_fff
, p_ffn
;
565 if (path
!= wxEmptyString
)
567 oldpath
= cfg
->GetPath();
571 m_Borders
= cfg
->Read(wxT("wxHtmlWindow/Borders"), m_Borders
);
572 p_fff
= cfg
->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
573 p_ffn
= cfg
->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
574 for (int i
= 0; i
< 7; i
++)
576 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
577 p_fontsizes
[i
] = cfg
->Read(tmp
, m_Parser
->m_FontsSizes
[i
]);
579 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
581 if (path
!= wxEmptyString
)
582 cfg
->SetPath(oldpath
);
587 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
592 if (path
!= wxEmptyString
)
594 oldpath
= cfg
->GetPath();
598 cfg
->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders
);
599 cfg
->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
600 cfg
->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
601 for (int i
= 0; i
< 7; i
++)
603 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
604 cfg
->Write(tmp
, (long) m_Parser
->m_FontsSizes
[i
]);
607 if (path
!= wxEmptyString
)
608 cfg
->SetPath(oldpath
);
613 bool wxHtmlWindow::HistoryBack()
617 if (m_HistoryPos
< 1) return false;
619 // store scroll position into history item:
621 GetViewStart(&x
, &y
);
622 (*m_History
)[m_HistoryPos
].SetPos(y
);
624 // go to previous position:
627 l
= (*m_History
)[m_HistoryPos
].GetPage();
628 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
631 if (a
== wxEmptyString
) LoadPage(l
);
632 else LoadPage(l
+ wxT("#") + a
);
635 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
640 bool wxHtmlWindow::HistoryCanBack()
642 if (m_HistoryPos
< 1) return false;
647 bool wxHtmlWindow::HistoryForward()
651 if (m_HistoryPos
== -1) return false;
652 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return false;
654 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
657 l
= (*m_History
)[m_HistoryPos
].GetPage();
658 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
661 if (a
== wxEmptyString
) LoadPage(l
);
662 else LoadPage(l
+ wxT("#") + a
);
665 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
670 bool wxHtmlWindow::HistoryCanForward()
672 if (m_HistoryPos
== -1) return false;
673 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return false;
678 void wxHtmlWindow::HistoryClear()
684 void wxHtmlWindow::AddProcessor(wxHtmlProcessor
*processor
)
688 m_Processors
= new wxHtmlProcessorList
;
690 wxHtmlProcessorList::compatibility_iterator node
;
692 for (node
= m_Processors
->GetFirst(); node
; node
= node
->GetNext())
694 if (processor
->GetPriority() > node
->GetData()->GetPriority())
696 m_Processors
->Insert(node
, processor
);
700 m_Processors
->Append(processor
);
703 /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor
*processor
)
705 if (!m_GlobalProcessors
)
707 m_GlobalProcessors
= new wxHtmlProcessorList
;
709 wxHtmlProcessorList::compatibility_iterator node
;
711 for (node
= m_GlobalProcessors
->GetFirst(); node
; node
= node
->GetNext())
713 if (processor
->GetPriority() > node
->GetData()->GetPriority())
715 m_GlobalProcessors
->Insert(node
, processor
);
719 m_GlobalProcessors
->Append(processor
);
724 wxList
wxHtmlWindow::m_Filters
;
725 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
726 wxHtmlProcessorList
*wxHtmlWindow::m_GlobalProcessors
= NULL
;
728 void wxHtmlWindow::CleanUpStatics()
730 wxDELETE(m_DefaultFilter
);
731 WX_CLEAR_LIST(wxList
, m_Filters
);
732 if (m_GlobalProcessors
)
733 WX_CLEAR_LIST(wxHtmlProcessorList
, *m_GlobalProcessors
);
734 wxDELETE(m_GlobalProcessors
);
739 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
741 m_Filters
.Append(filter
);
745 bool wxHtmlWindow::IsSelectionEnabled() const
748 return !(m_Style
& wxHW_NO_SELECTION
);
756 wxString
wxHtmlWindow::DoSelectionToText(wxHtmlSelection
*sel
)
759 return wxEmptyString
;
763 const wxHtmlCell
*end
= sel
->GetToCell();
765 wxHtmlTerminalCellsInterator
i(sel
->GetFromCell(), end
);
768 text
<< i
->ConvertToText(sel
);
771 const wxHtmlCell
*prev
= *i
;
774 if ( prev
->GetParent() != i
->GetParent() )
776 text
<< i
->ConvertToText(*i
== end
? sel
: NULL
);
783 wxString
wxHtmlWindow::ToText()
788 sel
.Set(m_Cell
->GetFirstTerminal(), m_Cell
->GetLastTerminal());
789 return DoSelectionToText(&sel
);
792 return wxEmptyString
;
795 #endif // wxUSE_CLIPBOARD
797 bool wxHtmlWindow::CopySelection(ClipboardType t
)
802 #if defined(__UNIX__) && !defined(__WXMAC__)
803 wxTheClipboard
->UsePrimarySelection(t
== Primary
);
805 // Primary selection exists only under X11, so don't do anything under
806 // the other platforms when we try to access it
808 // TODO: this should be abstracted at wxClipboard level!
811 #endif // __UNIX__/!__UNIX__
813 if ( wxTheClipboard
->Open() )
815 const wxString
txt(SelectionToText());
816 wxTheClipboard
->SetData(new wxTextDataObject(txt
));
817 wxTheClipboard
->Close();
818 wxLogTrace(_T("wxhtmlselection"),
819 _("Copied to clipboard:\"%s\""), txt
.c_str());
826 #endif // wxUSE_CLIPBOARD
832 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
834 const wxMouseEvent
*e
= link
.GetEvent();
835 if (e
== NULL
|| e
->LeftUp())
836 LoadPage(link
.GetHref());
839 void wxHtmlWindow::OnCellClicked(wxHtmlCell
*cell
,
840 wxCoord x
, wxCoord y
,
841 const wxMouseEvent
& event
)
843 wxCHECK_RET( cell
, _T("can't be called with NULL cell") );
845 cell
->OnMouseClick(this, x
, y
, event
);
848 void wxHtmlWindow::OnCellMouseHover(wxHtmlCell
* WXUNUSED(cell
),
849 wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
854 void wxHtmlWindow::OnEraseBackground(wxEraseEvent
& event
)
858 // don't even skip the event, if we don't have a bg bitmap we're going
859 // to overwrite background in OnPaint() below anyhow, so letting the
860 // default handling take place would only result in flicker, just set a
861 // flag to erase the background below
862 m_eraseBgInOnPaint
= true;
866 wxDC
& dc
= *event
.GetDC();
868 // if the image is not fully opaque, we have to erase the background before
869 // drawing it, however avoid doing it for opaque images as this would just
870 // result in extra flicker without any other effect as background is
871 // completely covered anyhow
872 if ( m_bmpBg
.GetMask() )
874 dc
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
878 const wxSize
sizeWin(GetClientSize());
879 const wxSize
sizeBmp(m_bmpBg
.GetWidth(), m_bmpBg
.GetHeight());
880 for ( wxCoord x
= 0; x
< sizeWin
.x
; x
+= sizeBmp
.x
)
882 for ( wxCoord y
= 0; y
< sizeWin
.y
; y
+= sizeBmp
.y
)
884 dc
.DrawBitmap(m_bmpBg
, x
, y
, true /* use mask */);
889 void wxHtmlWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
893 if (m_tmpCanDrawLocks
> 0 || m_Cell
== NULL
)
897 GetViewStart(&x
, &y
);
898 wxRect rect
= GetUpdateRegion().GetBox();
899 wxSize sz
= GetSize();
903 m_backBuffer
= new wxBitmap(sz
.x
, sz
.y
);
904 dcm
.SelectObject(*m_backBuffer
);
906 if ( m_eraseBgInOnPaint
)
908 dcm
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
911 m_eraseBgInOnPaint
= false;
913 else // someone has already erased the background, keep it
915 // preserve the existing background, otherwise we'd erase anything the
916 // user code had drawn in its EVT_ERASE_BACKGROUND handler when we do
917 // the Blit back below
918 dcm
.Blit(0, rect
.GetTop(),
919 sz
.x
, rect
.GetBottom() - rect
.GetTop() + 1,
925 dcm
.SetMapMode(wxMM_TEXT
);
926 dcm
.SetBackgroundMode(wxTRANSPARENT
);
928 wxHtmlRenderingInfo rinfo
;
929 wxDefaultHtmlRenderingStyle rstyle
;
930 rinfo
.SetSelection(m_selection
);
931 rinfo
.SetStyle(&rstyle
);
932 m_Cell
->Draw(dcm
, 0, 0,
933 y
* wxHTML_SCROLL_STEP
+ rect
.GetTop(),
934 y
* wxHTML_SCROLL_STEP
+ rect
.GetBottom(),
937 //#define DEBUG_HTML_SELECTION
938 #ifdef DEBUG_HTML_SELECTION
941 wxGetMousePosition(&xc
, &yc
);
942 ScreenToClient(&xc
, &yc
);
943 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
944 wxHtmlCell
*at
= m_Cell
->FindCellByPos(x
, y
);
946 m_Cell
->FindCellByPos(x
, y
, wxHTML_FIND_NEAREST_BEFORE
);
948 m_Cell
->FindCellByPos(x
, y
, wxHTML_FIND_NEAREST_AFTER
);
950 dcm
.SetBrush(*wxTRANSPARENT_BRUSH
);
951 dcm
.SetPen(*wxBLACK_PEN
);
953 dcm
.DrawRectangle(at
->GetAbsPos(),
954 wxSize(at
->GetWidth(),at
->GetHeight()));
955 dcm
.SetPen(*wxGREEN_PEN
);
957 dcm
.DrawRectangle(before
->GetAbsPos().x
+1, before
->GetAbsPos().y
+1,
958 before
->GetWidth()-2,before
->GetHeight()-2);
959 dcm
.SetPen(*wxRED_PEN
);
961 dcm
.DrawRectangle(after
->GetAbsPos().x
+2, after
->GetAbsPos().y
+2,
962 after
->GetWidth()-4,after
->GetHeight()-4);
966 dcm
.SetDeviceOrigin(0,0);
967 dc
.Blit(0, rect
.GetTop(),
968 sz
.x
, rect
.GetBottom() - rect
.GetTop() + 1,
976 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
978 wxDELETE(m_backBuffer
);
980 wxScrolledWindow::OnSize(event
);
983 // Recompute selection if necessary:
986 m_selection
->Set(m_selection
->GetFromCell(),
987 m_selection
->GetToCell());
988 m_selection
->ClearPrivPos();
995 void wxHtmlWindow::OnMouseMove(wxMouseEvent
& WXUNUSED(event
))
997 m_tmpMouseMoved
= true;
1000 void wxHtmlWindow::OnMouseDown(wxMouseEvent
& event
)
1003 if ( event
.LeftDown() && IsSelectionEnabled() )
1005 const long TRIPLECLICK_LEN
= 200; // 0.2 sec after doubleclick
1006 if ( wxGetLocalTimeMillis() - m_lastDoubleClick
<= TRIPLECLICK_LEN
)
1008 SelectLine(CalcUnscrolledPosition(event
.GetPosition()));
1010 (void) CopySelection();
1014 m_makingSelection
= true;
1018 wxDELETE(m_selection
);
1021 m_tmpSelFromPos
= CalcUnscrolledPosition(event
.GetPosition());
1022 m_tmpSelFromCell
= NULL
;
1029 #endif // wxUSE_CLIPBOARD
1032 void wxHtmlWindow::OnMouseUp(wxMouseEvent
& event
)
1035 if ( m_makingSelection
)
1038 m_makingSelection
= false;
1040 // did the user move the mouse far enough from starting point?
1041 if ( CopySelection(Primary
) )
1043 // we don't want mouse up event that ended selecting to be
1044 // handled as mouse click and e.g. follow hyperlink:
1048 #endif // wxUSE_CLIPBOARD
1053 wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
1054 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1056 // check is needed because FindCellByPos returns terminal cell and
1057 // containers may have empty borders -- in this case NULL will be
1060 OnCellClicked(cell
, pos
.x
, pos
.y
, event
);
1066 void wxHtmlWindow::OnInternalIdle()
1068 wxWindow::OnInternalIdle();
1070 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
))
1072 #ifdef DEBUG_HTML_SELECTION
1076 wxGetMousePosition(&xc
, &yc
);
1077 ScreenToClient(&xc
, &yc
);
1078 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
1080 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(x
, y
);
1082 // handle selection update:
1083 if ( m_makingSelection
)
1085 if ( !m_tmpSelFromCell
)
1086 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1087 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
);
1089 // NB: a trick - we adjust selFromPos to be upper left or bottom
1090 // right corner of the first cell of the selection depending
1091 // on whether the mouse is moving to the right or to the left.
1092 // This gives us more "natural" behaviour when selecting
1093 // a line (specifically, first cell of the next line is not
1094 // included if you drag selection from left to right over
1097 if ( !m_tmpSelFromCell
)
1099 dirFromPos
= m_tmpSelFromPos
;
1103 dirFromPos
= m_tmpSelFromCell
->GetAbsPos();
1104 if ( x
< m_tmpSelFromPos
.x
)
1106 dirFromPos
.x
+= m_tmpSelFromCell
->GetWidth();
1107 dirFromPos
.y
+= m_tmpSelFromCell
->GetHeight();
1110 bool goingDown
= dirFromPos
.y
< y
||
1111 (dirFromPos
.y
== y
&& dirFromPos
.x
< x
);
1113 // determine selection span:
1114 if ( /*still*/ !m_tmpSelFromCell
)
1118 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1119 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1120 wxHTML_FIND_NEAREST_AFTER
);
1121 if (!m_tmpSelFromCell
)
1122 m_tmpSelFromCell
= m_Cell
->GetFirstTerminal();
1126 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1127 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1128 wxHTML_FIND_NEAREST_BEFORE
);
1129 if (!m_tmpSelFromCell
)
1130 m_tmpSelFromCell
= m_Cell
->GetLastTerminal();
1134 wxHtmlCell
*selcell
= cell
;
1139 selcell
= m_Cell
->FindCellByPos(x
, y
,
1140 wxHTML_FIND_NEAREST_BEFORE
);
1142 selcell
= m_Cell
->GetLastTerminal();
1146 selcell
= m_Cell
->FindCellByPos(x
, y
,
1147 wxHTML_FIND_NEAREST_AFTER
);
1149 selcell
= m_Cell
->GetFirstTerminal();
1153 // NB: it may *rarely* happen that the code above didn't find one
1154 // of the cells, e.g. if wxHtmlWindow doesn't contain any
1156 if ( selcell
&& m_tmpSelFromCell
)
1160 // start selecting only if mouse movement was big enough
1161 // (otherwise it was meant as mouse click, not selection):
1162 const int PRECISION
= 2;
1163 wxPoint diff
= m_tmpSelFromPos
- wxPoint(x
,y
);
1164 if (abs(diff
.x
) > PRECISION
|| abs(diff
.y
) > PRECISION
)
1166 m_selection
= new wxHtmlSelection();
1171 if ( m_tmpSelFromCell
->IsBefore(selcell
) )
1173 m_selection
->Set(m_tmpSelFromPos
, m_tmpSelFromCell
,
1174 wxPoint(x
,y
), selcell
); }
1177 m_selection
->Set(wxPoint(x
,y
), selcell
,
1178 m_tmpSelFromPos
, m_tmpSelFromCell
);
1180 m_selection
->ClearPrivPos();
1186 // handle cursor and status bar text changes:
1187 if ( cell
!= m_tmpLastCell
)
1189 wxHtmlLinkInfo
*lnk
= cell
? cell
->GetLink(x
, y
) : NULL
;
1192 cur
= cell
->GetCursor();
1194 cur
= *wxSTANDARD_CURSOR
;
1197 if (lnk
!= m_tmpLastLink
)
1202 if (m_RelatedStatusBar
!= -1)
1203 m_RelatedFrame
->SetStatusText(wxEmptyString
,
1204 m_RelatedStatusBar
);
1208 if (m_RelatedStatusBar
!= -1)
1209 m_RelatedFrame
->SetStatusText(lnk
->GetHref(),
1210 m_RelatedStatusBar
);
1212 #endif // wxUSE_STATUSBAR
1213 m_tmpLastLink
= lnk
;
1216 m_tmpLastCell
= cell
;
1218 else // mouse moved but stayed in the same cell
1221 OnCellMouseHover(cell
, x
, y
);
1224 m_tmpMouseMoved
= false;
1229 void wxHtmlWindow::StopAutoScrolling()
1231 if ( m_timerAutoScroll
)
1233 wxDELETE(m_timerAutoScroll
);
1237 void wxHtmlWindow::OnMouseEnter(wxMouseEvent
& event
)
1239 StopAutoScrolling();
1243 void wxHtmlWindow::OnMouseLeave(wxMouseEvent
& event
)
1245 // don't prevent the usual processing of the event from taking place
1248 // when a captured mouse leave a scrolled window we start generate
1249 // scrolling events to allow, for example, extending selection beyond the
1250 // visible area in some controls
1251 if ( wxWindow::GetCapture() == this )
1253 // where is the mouse leaving?
1255 wxPoint pt
= event
.GetPosition();
1258 orient
= wxHORIZONTAL
;
1261 else if ( pt
.y
< 0 )
1263 orient
= wxVERTICAL
;
1266 else // we're lower or to the right of the window
1268 wxSize size
= GetClientSize();
1269 if ( pt
.x
> size
.x
)
1271 orient
= wxHORIZONTAL
;
1272 pos
= GetVirtualSize().x
/ wxHTML_SCROLL_STEP
;
1274 else if ( pt
.y
> size
.y
)
1276 orient
= wxVERTICAL
;
1277 pos
= GetVirtualSize().y
/ wxHTML_SCROLL_STEP
;
1279 else // this should be impossible
1281 // but seems to happen sometimes under wxMSW - maybe it's a bug
1282 // there but for now just ignore it
1284 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1290 // only start the auto scroll timer if the window can be scrolled in
1292 if ( !HasScrollbar(orient
) )
1295 delete m_timerAutoScroll
;
1296 m_timerAutoScroll
= new wxHtmlWinAutoScrollTimer
1299 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1300 : wxEVT_SCROLLWIN_LINEDOWN
,
1304 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1308 void wxHtmlWindow::OnKeyUp(wxKeyEvent
& event
)
1310 if ( IsSelectionEnabled() && event
.GetKeyCode() == 'C' && event
.CmdDown() )
1312 (void) CopySelection();
1316 void wxHtmlWindow::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1318 (void) CopySelection();
1321 void wxHtmlWindow::OnDoubleClick(wxMouseEvent
& event
)
1323 // select word under cursor:
1324 if ( IsSelectionEnabled() )
1326 SelectWord(CalcUnscrolledPosition(event
.GetPosition()));
1328 (void) CopySelection(Primary
);
1330 m_lastDoubleClick
= wxGetLocalTimeMillis();
1336 void wxHtmlWindow::SelectWord(const wxPoint
& pos
)
1340 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1344 m_selection
= new wxHtmlSelection();
1345 m_selection
->Set(cell
, cell
);
1346 RefreshRect(wxRect(CalcScrolledPosition(cell
->GetAbsPos()),
1347 wxSize(cell
->GetWidth(), cell
->GetHeight())));
1352 void wxHtmlWindow::SelectLine(const wxPoint
& pos
)
1356 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1359 // We use following heuristic to find a "line": let the line be all
1360 // cells in same container as the cell under mouse cursor that are
1361 // neither completely above nor completely bellow the clicked cell
1362 // (i.e. are likely to be words positioned on same line of text).
1364 int y1
= cell
->GetAbsPos().y
;
1365 int y2
= y1
+ cell
->GetHeight();
1367 const wxHtmlCell
*c
;
1368 const wxHtmlCell
*before
= NULL
;
1369 const wxHtmlCell
*after
= NULL
;
1371 // find last cell of line:
1372 for ( c
= cell
->GetNext(); c
; c
= c
->GetNext())
1374 y
= c
->GetAbsPos().y
;
1375 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1383 // find first cell of line:
1384 for ( c
= cell
->GetParent()->GetFirstChild();
1385 c
&& c
!= cell
; c
= c
->GetNext())
1387 y
= c
->GetAbsPos().y
;
1388 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1400 m_selection
= new wxHtmlSelection();
1401 m_selection
->Set(before
, after
);
1408 void wxHtmlWindow::SelectAll()
1413 m_selection
= new wxHtmlSelection();
1414 m_selection
->Set(m_Cell
->GetFirstTerminal(), m_Cell
->GetLastTerminal());
1419 #endif // wxUSE_CLIPBOARD
1423 IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor
,wxObject
)
1425 #if wxUSE_EXTENDED_RTTI
1426 IMPLEMENT_DYNAMIC_CLASS_XTI(wxHtmlWindow
, wxScrolledWindow
,"wx/html/htmlwin.h")
1428 wxBEGIN_PROPERTIES_TABLE(wxHtmlWindow
)
1431 style , wxHW_SCROLLBAR_AUTO
1432 borders , (dimension)
1436 wxEND_PROPERTIES_TABLE()
1438 wxBEGIN_HANDLERS_TABLE(wxHtmlWindow
)
1439 wxEND_HANDLERS_TABLE()
1441 wxCONSTRUCTOR_5( wxHtmlWindow
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
1443 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
1446 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
1447 EVT_SIZE(wxHtmlWindow::OnSize
)
1448 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseDown
)
1449 EVT_LEFT_UP(wxHtmlWindow::OnMouseUp
)
1450 EVT_RIGHT_UP(wxHtmlWindow::OnMouseUp
)
1451 EVT_MOTION(wxHtmlWindow::OnMouseMove
)
1452 EVT_ERASE_BACKGROUND(wxHtmlWindow::OnEraseBackground
)
1453 EVT_PAINT(wxHtmlWindow::OnPaint
)
1455 EVT_LEFT_DCLICK(wxHtmlWindow::OnDoubleClick
)
1456 EVT_ENTER_WINDOW(wxHtmlWindow::OnMouseEnter
)
1457 EVT_LEAVE_WINDOW(wxHtmlWindow::OnMouseLeave
)
1458 EVT_KEY_UP(wxHtmlWindow::OnKeyUp
)
1459 EVT_MENU(wxID_COPY
, wxHtmlWindow::OnCopy
)
1460 #endif // wxUSE_CLIPBOARD
1467 // A module to allow initialization/cleanup
1468 // without calling these functions from app.cpp or from
1469 // the user's application.
1471 class wxHtmlWinModule
: public wxModule
1473 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
1475 wxHtmlWinModule() : wxModule() {}
1476 bool OnInit() { return true; }
1477 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
1480 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
1483 // This hack forces the linker to always link in m_* files
1484 // (wxHTML doesn't work without handlers from these files)
1485 #include "wx/html/forcelnk.h"
1486 FORCE_WXHTML_MODULES()
1488 #endif // wxUSE_HTML