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 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "htmlwin.h"
13 #pragma implementation "htmlproc.h"
16 #include "wx/wxprec.h"
19 #if wxUSE_HTML && wxUSE_STREAMS
28 #include "wx/dcclient.h"
32 #include "wx/html/htmlwin.h"
33 #include "wx/html/htmlproc.h"
35 #include "wx/clipbrd.h"
36 #include "wx/dataobj.h"
38 #include "wx/dcmemory.h"
40 #include "wx/arrimpl.cpp"
41 #include "wx/listimpl.cpp"
46 // ----------------------------------------------------------------------------
47 // wxHtmlWinAutoScrollTimer: the timer used to generate a stream of scroll
48 // events when a captured mouse is held outside the window
49 // ----------------------------------------------------------------------------
51 class wxHtmlWinAutoScrollTimer
: public wxTimer
54 wxHtmlWinAutoScrollTimer(wxScrolledWindow
*win
,
55 wxEventType eventTypeToSend
,
59 m_eventType
= eventTypeToSend
;
64 virtual void Notify();
67 wxScrolledWindow
*m_win
;
68 wxEventType m_eventType
;
72 DECLARE_NO_COPY_CLASS(wxHtmlWinAutoScrollTimer
)
75 void wxHtmlWinAutoScrollTimer::Notify()
77 // only do all this as long as the window is capturing the mouse
78 if ( wxWindow::GetCapture() != m_win
)
82 else // we still capture the mouse, continue generating events
84 // first scroll the window if we are allowed to do it
85 wxScrollWinEvent
event1(m_eventType
, m_pos
, m_orient
);
86 event1
.SetEventObject(m_win
);
87 if ( m_win
->GetEventHandler()->ProcessEvent(event1
) )
89 // and then send a pseudo mouse-move event to refresh the selection
90 wxMouseEvent
event2(wxEVT_MOTION
);
91 wxGetMousePosition(&event2
.m_x
, &event2
.m_y
);
93 // the mouse event coordinates should be client, not screen as
94 // returned by wxGetMousePosition
95 wxWindow
*parentTop
= m_win
;
96 while ( parentTop
->GetParent() )
97 parentTop
= parentTop
->GetParent();
98 wxPoint ptOrig
= parentTop
->GetPosition();
99 event2
.m_x
-= ptOrig
.x
;
100 event2
.m_y
-= ptOrig
.y
;
102 event2
.SetEventObject(m_win
);
104 // FIXME: we don't fill in the other members - ok?
105 m_win
->GetEventHandler()->ProcessEvent(event2
);
107 else // can't scroll further, stop
117 //-----------------------------------------------------------------------------
119 //-----------------------------------------------------------------------------
121 // item of history list
122 class WXDLLEXPORT wxHtmlHistoryItem
125 wxHtmlHistoryItem(const wxString
& p
, const wxString
& a
) {m_Page
= p
, m_Anchor
= a
, m_Pos
= 0;}
126 int GetPos() const {return m_Pos
;}
127 void SetPos(int p
) {m_Pos
= p
;}
128 const wxString
& GetPage() const {return m_Page
;}
129 const wxString
& GetAnchor() const {return m_Anchor
;}
138 //-----------------------------------------------------------------------------
139 // our private arrays:
140 //-----------------------------------------------------------------------------
142 WX_DECLARE_OBJARRAY(wxHtmlHistoryItem
, wxHtmlHistoryArray
);
143 WX_DEFINE_OBJARRAY(wxHtmlHistoryArray
);
145 WX_DECLARE_LIST(wxHtmlProcessor
, wxHtmlProcessorList
);
146 WX_DEFINE_LIST(wxHtmlProcessorList
);
148 //-----------------------------------------------------------------------------
150 //-----------------------------------------------------------------------------
153 void wxHtmlWindow::Init()
155 m_tmpMouseMoved
= FALSE
;
156 m_tmpLastLink
= NULL
;
157 m_tmpLastCell
= NULL
;
158 m_tmpCanDrawLocks
= 0;
159 m_FS
= new wxFileSystem();
160 m_RelatedStatusBar
= -1;
161 m_RelatedFrame
= NULL
;
162 m_TitleFormat
= wxT("%s");
163 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
165 m_Parser
= new wxHtmlWinParser(this);
166 m_Parser
->SetFS(m_FS
);
169 m_History
= new wxHtmlHistoryArray
;
174 m_makingSelection
= false;
176 m_timerAutoScroll
= 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
, name
))
190 SetPage(wxT("<html><body></body></html>"));
195 wxHtmlWindow::~wxHtmlWindow()
202 if (m_Cell
) delete m_Cell
;
213 void wxHtmlWindow::SetRelatedFrame(wxFrame
* frame
, const wxString
& format
)
215 m_RelatedFrame
= frame
;
216 m_TitleFormat
= format
;
221 void wxHtmlWindow::SetRelatedStatusBar(int bar
)
223 m_RelatedStatusBar
= bar
;
228 void wxHtmlWindow::SetFonts(wxString normal_face
, wxString fixed_face
, const int *sizes
)
230 wxString op
= m_OpenedPage
;
232 m_Parser
->SetFonts(normal_face
, fixed_face
, sizes
);
233 // fonts changed => contents invalid, so reload the page:
234 SetPage(wxT("<html><body></body></html>"));
235 if (!op
.IsEmpty()) LoadPage(op
);
240 bool wxHtmlWindow::SetPage(const wxString
& source
)
242 wxString
newsrc(source
);
244 wxDELETE(m_selection
);
246 // pass HTML through registered processors:
247 if (m_Processors
|| m_GlobalProcessors
)
249 wxHtmlProcessorList::Node
*nodeL
, *nodeG
;
252 nodeL
= (m_Processors
) ? m_Processors
->GetFirst() : NULL
;
253 nodeG
= (m_GlobalProcessors
) ? m_GlobalProcessors
->GetFirst() : NULL
;
255 // VS: there are two lists, global and local, both of them sorted by
256 // priority. Since we have to go through _both_ lists with
257 // decreasing priority, we "merge-sort" the lists on-line by
258 // processing that one of the two heads that has higher priority
259 // in every iteration
260 while (nodeL
|| nodeG
)
262 prL
= (nodeL
) ? nodeL
->GetData()->GetPriority() : -1;
263 prG
= (nodeG
) ? nodeG
->GetData()->GetPriority() : -1;
266 if (nodeL
->GetData()->IsEnabled())
267 newsrc
= nodeL
->GetData()->Process(newsrc
);
268 nodeL
= nodeL
->GetNext();
272 if (nodeG
->GetData()->IsEnabled())
273 newsrc
= nodeG
->GetData()->Process(newsrc
);
274 nodeG
= nodeG
->GetNext();
279 // ...and run the parser on it:
280 wxClientDC
*dc
= new wxClientDC(this);
281 dc
->SetMapMode(wxMM_TEXT
);
282 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
283 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
290 m_Cell
= (wxHtmlContainerCell
*) m_Parser
->Parse(newsrc
);
292 m_Cell
->SetIndent(m_Borders
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
293 m_Cell
->SetAlignHor(wxHTML_ALIGN_CENTER
);
295 if (m_tmpCanDrawLocks
== 0)
300 bool wxHtmlWindow::AppendToPage(const wxString
& source
)
302 return SetPage(*(GetParser()->GetSource()) + source
);
305 bool wxHtmlWindow::LoadPage(const wxString
& location
)
307 wxBusyCursor busyCursor
;
311 bool needs_refresh
= FALSE
;
314 if (m_HistoryOn
&& (m_HistoryPos
!= -1))
316 // store scroll position into history item:
318 GetViewStart(&x
, &y
);
319 (*m_History
)[m_HistoryPos
].SetPos(y
);
322 if (location
[0] == wxT('#'))
325 wxString anch
= location
.Mid(1) /*1 to end*/;
327 rt_val
= ScrollToAnchor(anch
);
330 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&& location
.BeforeFirst(wxT('#')) == m_OpenedPage
)
332 wxString anch
= location
.AfterFirst(wxT('#'));
334 rt_val
= ScrollToAnchor(anch
);
337 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&&
338 (m_FS
->GetPath() + location
.BeforeFirst(wxT('#'))) == m_OpenedPage
)
340 wxString anch
= location
.AfterFirst(wxT('#'));
342 rt_val
= ScrollToAnchor(anch
);
348 needs_refresh
= TRUE
;
350 if (m_RelatedStatusBar
!= -1)
352 m_RelatedFrame
->SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
356 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location
);
358 // try to interpret 'location' as filename instead of URL:
361 wxFileName
fn(location
);
362 wxString location2
= wxFileSystem::FileNameToURL(fn
);
363 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location2
);
368 wxLogError(_("Unable to open requested HTML document: %s"), location
.c_str());
376 wxString src
= wxEmptyString
;
378 if (m_RelatedStatusBar
!= -1)
380 wxString msg
= _("Loading : ") + location
;
381 m_RelatedFrame
->SetStatusText(msg
, m_RelatedStatusBar
);
385 node
= m_Filters
.GetFirst();
388 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
391 src
= h
->ReadFile(*f
);
394 node
= node
->GetNext();
396 if (src
== wxEmptyString
)
398 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
399 src
= m_DefaultFilter
->ReadFile(*f
);
402 m_FS
->ChangePathTo(f
->GetLocation());
403 rt_val
= SetPage(src
);
404 m_OpenedPage
= f
->GetLocation();
405 if (f
->GetAnchor() != wxEmptyString
)
407 ScrollToAnchor(f
->GetAnchor());
412 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
->SetStatusText(_("Done"), m_RelatedStatusBar
);
416 if (m_HistoryOn
) // add this page to history there:
418 int c
= m_History
->GetCount() - (m_HistoryPos
+ 1);
420 if (m_HistoryPos
< 0 ||
421 (*m_History
)[m_HistoryPos
].GetPage() != m_OpenedPage
||
422 (*m_History
)[m_HistoryPos
].GetAnchor() != m_OpenedAnchor
)
425 for (int i
= 0; i
< c
; i
++)
426 m_History
->RemoveAt(m_HistoryPos
);
427 m_History
->Add(new wxHtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
431 if (m_OpenedPageTitle
== wxEmptyString
)
432 OnSetTitle(wxFileNameFromPath(m_OpenedPage
));
446 bool wxHtmlWindow::LoadFile(const wxFileName
& filename
)
448 wxString url
= wxFileSystem::FileNameToURL(filename
);
449 return LoadPage(url
);
453 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
455 const wxHtmlCell
*c
= m_Cell
->Find(wxHTML_COND_ISANCHOR
, &anchor
);
458 wxLogWarning(_("HTML anchor %s does not exist."), anchor
.c_str());
465 for (y
= 0; c
!= NULL
; c
= c
->GetParent()) y
+= c
->GetPosY();
466 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
467 m_OpenedAnchor
= anchor
;
473 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
478 tit
.Printf(m_TitleFormat
, title
.c_str());
479 m_RelatedFrame
->SetTitle(tit
);
481 m_OpenedPageTitle
= title
;
488 void wxHtmlWindow::CreateLayout()
490 int ClientWidth
, ClientHeight
;
494 if (m_Style
& wxHW_SCROLLBAR_NEVER
)
496 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // always off
497 GetClientSize(&ClientWidth
, &ClientHeight
);
498 m_Cell
->Layout(ClientWidth
);
502 GetClientSize(&ClientWidth
, &ClientHeight
);
503 m_Cell
->Layout(ClientWidth
);
504 if (ClientHeight
< m_Cell
->GetHeight() + GetCharHeight())
507 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
508 m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
,
509 (m_Cell
->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
510 /*cheat: top-level frag is always container*/);
512 else /* we fit into window, no need for scrollbars */
514 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
515 GetClientSize(&ClientWidth
, &ClientHeight
);
516 m_Cell
->Layout(ClientWidth
); // ...and relayout
523 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
528 wxString p_fff
, p_ffn
;
530 if (path
!= wxEmptyString
)
532 oldpath
= cfg
->GetPath();
536 m_Borders
= cfg
->Read(wxT("wxHtmlWindow/Borders"), m_Borders
);
537 p_fff
= cfg
->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
538 p_ffn
= cfg
->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
539 for (int i
= 0; i
< 7; i
++)
541 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
542 p_fontsizes
[i
] = cfg
->Read(tmp
, m_Parser
->m_FontsSizes
[i
]);
544 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
546 if (path
!= wxEmptyString
)
547 cfg
->SetPath(oldpath
);
552 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
557 if (path
!= wxEmptyString
)
559 oldpath
= cfg
->GetPath();
563 cfg
->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders
);
564 cfg
->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
565 cfg
->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
566 for (int i
= 0; i
< 7; i
++)
568 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
569 cfg
->Write(tmp
, (long) m_Parser
->m_FontsSizes
[i
]);
572 if (path
!= wxEmptyString
)
573 cfg
->SetPath(oldpath
);
578 bool wxHtmlWindow::HistoryBack()
582 if (m_HistoryPos
< 1) return FALSE
;
584 // store scroll position into history item:
586 GetViewStart(&x
, &y
);
587 (*m_History
)[m_HistoryPos
].SetPos(y
);
589 // go to previous position:
592 l
= (*m_History
)[m_HistoryPos
].GetPage();
593 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
596 if (a
== wxEmptyString
) LoadPage(l
);
597 else LoadPage(l
+ wxT("#") + a
);
600 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
605 bool wxHtmlWindow::HistoryCanBack()
607 if (m_HistoryPos
< 1) return FALSE
;
612 bool wxHtmlWindow::HistoryForward()
616 if (m_HistoryPos
== -1) return FALSE
;
617 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
619 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
622 l
= (*m_History
)[m_HistoryPos
].GetPage();
623 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
626 if (a
== wxEmptyString
) LoadPage(l
);
627 else LoadPage(l
+ wxT("#") + a
);
630 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
635 bool wxHtmlWindow::HistoryCanForward()
637 if (m_HistoryPos
== -1) return FALSE
;
638 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
643 void wxHtmlWindow::HistoryClear()
649 void wxHtmlWindow::AddProcessor(wxHtmlProcessor
*processor
)
653 m_Processors
= new wxHtmlProcessorList
;
654 m_Processors
->DeleteContents(TRUE
);
656 wxHtmlProcessorList::Node
*node
;
658 for (node
= m_Processors
->GetFirst(); node
; node
= node
->GetNext())
660 if (processor
->GetPriority() > node
->GetData()->GetPriority())
662 m_Processors
->Insert(node
, processor
);
666 m_Processors
->Append(processor
);
669 /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor
*processor
)
671 if (!m_GlobalProcessors
)
673 m_GlobalProcessors
= new wxHtmlProcessorList
;
674 m_GlobalProcessors
->DeleteContents(TRUE
);
676 wxHtmlProcessorList::Node
*node
;
678 for (node
= m_GlobalProcessors
->GetFirst(); node
; node
= node
->GetNext())
680 if (processor
->GetPriority() > node
->GetData()->GetPriority())
682 m_GlobalProcessors
->Insert(node
, processor
);
686 m_GlobalProcessors
->Append(processor
);
691 wxList
wxHtmlWindow::m_Filters
;
692 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
693 wxCursor
*wxHtmlWindow::s_cur_hand
= NULL
;
694 wxCursor
*wxHtmlWindow::s_cur_arrow
= NULL
;
695 wxHtmlProcessorList
*wxHtmlWindow::m_GlobalProcessors
= NULL
;
697 void wxHtmlWindow::CleanUpStatics()
699 wxDELETE(m_DefaultFilter
);
700 m_Filters
.DeleteContents(TRUE
);
702 wxDELETE(m_GlobalProcessors
);
703 wxDELETE(s_cur_hand
);
704 wxDELETE(s_cur_arrow
);
709 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
711 m_Filters
.Append(filter
);
715 bool wxHtmlWindow::IsSelectionEnabled() const
718 return !(m_Style
& wxHW_NO_SELECTION
);
726 wxString
wxHtmlWindow::SelectionToText()
729 return wxEmptyString
;
733 const wxHtmlCell
*end
= m_selection
->GetToCell();
735 wxHtmlTerminalCellsInterator
i(m_selection
->GetFromCell(), end
);
738 text
<< i
->ConvertToText(m_selection
);
741 const wxHtmlCell
*prev
= *i
;
744 if ( prev
->GetParent() != i
->GetParent() )
746 text
<< i
->ConvertToText(*i
== end
? m_selection
: NULL
);
753 void wxHtmlWindow::CopySelection(ClipboardType t
)
757 wxTheClipboard
->UsePrimarySelection(t
== Primary
);
758 wxString
txt(SelectionToText());
759 if ( wxTheClipboard
->Open() )
761 wxTheClipboard
->SetData(new wxTextDataObject(txt
));
762 wxTheClipboard
->Close();
763 wxLogTrace(_T("wxhtmlselection"),
764 _("Copied to clipboard:\"%s\""), txt
.c_str());
771 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
773 const wxMouseEvent
*e
= link
.GetEvent();
774 if (e
== NULL
|| e
->LeftUp())
775 LoadPage(link
.GetHref());
778 void wxHtmlWindow::OnCellClicked(wxHtmlCell
*cell
,
779 wxCoord x
, wxCoord y
,
780 const wxMouseEvent
& event
)
782 wxCHECK_RET( cell
, _T("can't be called with NULL cell") );
784 cell
->OnMouseClick(this, x
, y
, event
);
787 void wxHtmlWindow::OnCellMouseHover(wxHtmlCell
* WXUNUSED(cell
),
788 wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
793 void wxHtmlWindow::OnEraseBackground(wxEraseEvent
& event
)
797 void wxHtmlWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
801 if (m_tmpCanDrawLocks
> 0 || m_Cell
== NULL
) return;
804 GetViewStart(&x
, &y
);
805 wxRect rect
= GetUpdateRegion().GetBox();
806 wxSize sz
= GetSize();
810 m_backBuffer
= new wxBitmap(sz
.x
, sz
.y
);
811 dcm
.SelectObject(*m_backBuffer
);
812 dcm
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
815 dcm
.SetMapMode(wxMM_TEXT
);
816 dcm
.SetBackgroundMode(wxTRANSPARENT
);
818 wxHtmlRenderingInfo rinfo
;
819 wxDefaultHtmlRenderingStyle rstyle
;
820 rinfo
.SetSelection(m_selection
);
821 rinfo
.SetStyle(&rstyle
);
822 m_Cell
->Draw(dcm
, 0, 0,
823 y
* wxHTML_SCROLL_STEP
+ rect
.GetTop(),
824 y
* wxHTML_SCROLL_STEP
+ rect
.GetBottom(),
827 dcm
.SetDeviceOrigin(0,0);
828 dc
.Blit(0, rect
.GetTop(),
829 sz
.x
, rect
.GetBottom() - rect
.GetTop() + 1,
837 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
839 wxDELETE(m_backBuffer
);
841 wxScrolledWindow::OnSize(event
);
844 // Recompute selection if necessary:
847 m_selection
->Set(m_selection
->GetFromCell(),
848 m_selection
->GetToCell());
849 m_selection
->ClearPrivPos();
856 void wxHtmlWindow::OnMouseMove(wxMouseEvent
& event
)
858 m_tmpMouseMoved
= true;
861 void wxHtmlWindow::OnMouseDown(wxMouseEvent
& event
)
863 if ( event
.LeftDown() && IsSelectionEnabled() )
865 m_makingSelection
= true;
869 wxDELETE(m_selection
);
872 m_tmpSelFromPos
= CalcUnscrolledPosition(event
.GetPosition());
873 m_tmpSelFromCell
= NULL
;
879 void wxHtmlWindow::OnMouseUp(wxMouseEvent
& event
)
882 if ( m_makingSelection
)
885 m_makingSelection
= false;
887 // did the user move the mouse far enough from starting point?
891 CopySelection(Primary
);
893 // we don't want mouse up event that ended selecting to be
894 // handled as mouse click and e.g. follow hyperlink:
903 wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
904 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
906 // VZ: is it possible that we don't find anything at all?
907 // VS: yes. FindCellByPos returns terminal cell and
908 // containers may have empty borders
910 OnCellClicked(cell
, pos
.x
, pos
.y
, event
);
916 void wxHtmlWindow::OnIdle(wxIdleEvent
& WXUNUSED(event
))
918 if (s_cur_hand
== NULL
)
920 s_cur_hand
= new wxCursor(wxCURSOR_HAND
);
921 s_cur_arrow
= new wxCursor(wxCURSOR_ARROW
);
924 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
))
927 wxGetMousePosition(&xc
, &yc
);
928 ScreenToClient(&xc
, &yc
);
929 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
931 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(x
, y
);
933 // handle selection update:
934 if ( m_makingSelection
)
936 bool goingDown
= m_tmpSelFromPos
.y
< y
||
937 m_tmpSelFromPos
.y
== y
&& m_tmpSelFromPos
.x
< x
;
939 if ( !m_tmpSelFromCell
)
943 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
944 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
945 wxHTML_FIND_NEAREST_AFTER
);
946 if (!m_tmpSelFromCell
)
947 m_tmpSelFromCell
= m_Cell
->GetFirstTerminal();
951 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
952 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
953 wxHTML_FIND_NEAREST_BEFORE
);
954 if (!m_tmpSelFromCell
)
955 m_tmpSelFromCell
= m_Cell
->GetLastTerminal();
959 wxHtmlCell
*selcell
= cell
;
964 selcell
= m_Cell
->FindCellByPos(x
, y
,
965 wxHTML_FIND_NEAREST_AFTER
);
967 selcell
= m_Cell
->GetLastTerminal();
971 selcell
= m_Cell
->FindCellByPos(x
, y
,
972 wxHTML_FIND_NEAREST_BEFORE
);
974 selcell
= m_Cell
->GetFirstTerminal();
978 // NB: it may *rarely* happen that the code above didn't find one
979 // of the cells, e.g. if wxHtmlWindow doesn't contain any
981 if ( selcell
&& m_tmpSelFromCell
)
985 // start selecting only if mouse movement was big enough
986 // (otherwise it was meant as mouse click, not selection):
987 const int PRECISION
= 2;
988 wxPoint diff
= m_tmpSelFromPos
- wxPoint(x
,y
);
989 if (abs(diff
.x
) > PRECISION
|| abs(diff
.y
) > PRECISION
)
991 m_selection
= new wxHtmlSelection();
996 if ( m_tmpSelFromCell
->IsBefore(selcell
) )
998 m_selection
->Set(m_tmpSelFromPos
, m_tmpSelFromCell
,
999 wxPoint(x
,y
), selcell
); }
1002 m_selection
->Set(wxPoint(x
,y
), selcell
,
1003 m_tmpSelFromPos
, m_tmpSelFromCell
);
1005 m_selection
->ClearPrivPos();
1011 // handle cursor and status bar text changes:
1012 if ( cell
!= m_tmpLastCell
)
1014 wxHtmlLinkInfo
*lnk
= cell
? cell
->GetLink(x
, y
) : NULL
;
1016 if (lnk
!= m_tmpLastLink
)
1020 SetCursor(*s_cur_arrow
);
1021 if (m_RelatedStatusBar
!= -1)
1022 m_RelatedFrame
->SetStatusText(wxEmptyString
,
1023 m_RelatedStatusBar
);
1027 SetCursor(*s_cur_hand
);
1028 if (m_RelatedStatusBar
!= -1)
1029 m_RelatedFrame
->SetStatusText(lnk
->GetHref(),
1030 m_RelatedStatusBar
);
1032 m_tmpLastLink
= lnk
;
1035 m_tmpLastCell
= cell
;
1037 else // mouse moved but stayed in the same cell
1040 OnCellMouseHover(cell
, x
, y
);
1043 m_tmpMouseMoved
= FALSE
;
1048 void wxHtmlWindow::StopAutoScrolling()
1050 if ( m_timerAutoScroll
)
1052 wxDELETE(m_timerAutoScroll
);
1056 void wxHtmlWindow::OnMouseEnter(wxMouseEvent
& event
)
1058 StopAutoScrolling();
1062 void wxHtmlWindow::OnMouseLeave(wxMouseEvent
& event
)
1064 // don't prevent the usual processing of the event from taking place
1067 // when a captured mouse leave a scrolled window we start generate
1068 // scrolling events to allow, for example, extending selection beyond the
1069 // visible area in some controls
1070 if ( wxWindow::GetCapture() == this )
1072 // where is the mouse leaving?
1074 wxPoint pt
= event
.GetPosition();
1077 orient
= wxHORIZONTAL
;
1080 else if ( pt
.y
< 0 )
1082 orient
= wxVERTICAL
;
1085 else // we're lower or to the right of the window
1087 wxSize size
= GetClientSize();
1088 if ( pt
.x
> size
.x
)
1090 orient
= wxHORIZONTAL
;
1091 pos
= GetVirtualSize().x
/ wxHTML_SCROLL_STEP
;
1093 else if ( pt
.y
> size
.y
)
1095 orient
= wxVERTICAL
;
1096 pos
= GetVirtualSize().y
/ wxHTML_SCROLL_STEP
;
1098 else // this should be impossible
1100 // but seems to happen sometimes under wxMSW - maybe it's a bug
1101 // there but for now just ignore it
1103 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1109 // only start the auto scroll timer if the window can be scrolled in
1111 if ( !HasScrollbar(orient
) )
1114 delete m_timerAutoScroll
;
1115 m_timerAutoScroll
= new wxHtmlWinAutoScrollTimer
1118 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1119 : wxEVT_SCROLLWIN_LINEDOWN
,
1123 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1127 void wxHtmlWindow::OnKeyUp(wxKeyEvent
& event
)
1129 if ( IsSelectionEnabled() &&
1130 event
.GetKeyCode() == 'C' && event
.ControlDown() )
1137 void wxHtmlWindow::OnCopy(wxCommandEvent
& event
)
1143 void wxHtmlWindow::OnDoubleClick(wxMouseEvent
& event
)
1145 // select word under cursor:
1146 if ( IsSelectionEnabled() )
1148 wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
1149 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1153 m_selection
= new wxHtmlSelection();
1154 m_selection
->Set(cell
, cell
);
1155 RefreshRect(wxRect(CalcScrolledPosition(cell
->GetAbsPos()),
1156 wxSize(cell
->GetWidth(), cell
->GetHeight())));
1166 IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor
,wxObject
)
1168 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
1170 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
1171 EVT_SIZE(wxHtmlWindow::OnSize
)
1172 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseDown
)
1173 EVT_LEFT_UP(wxHtmlWindow::OnMouseUp
)
1174 EVT_RIGHT_UP(wxHtmlWindow::OnMouseUp
)
1175 EVT_MOTION(wxHtmlWindow::OnMouseMove
)
1176 EVT_IDLE(wxHtmlWindow::OnIdle
)
1177 EVT_ERASE_BACKGROUND(wxHtmlWindow::OnEraseBackground
)
1178 EVT_PAINT(wxHtmlWindow::OnPaint
)
1180 EVT_LEFT_DCLICK(wxHtmlWindow::OnDoubleClick
)
1181 EVT_ENTER_WINDOW(wxHtmlWindow::OnMouseEnter
)
1182 EVT_LEAVE_WINDOW(wxHtmlWindow::OnMouseLeave
)
1183 EVT_KEY_UP(wxHtmlWindow::OnKeyUp
)
1184 EVT_MENU(wxID_COPY
, wxHtmlWindow::OnCopy
)
1192 // A module to allow initialization/cleanup
1193 // without calling these functions from app.cpp or from
1194 // the user's application.
1196 class wxHtmlWinModule
: public wxModule
1198 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
1200 wxHtmlWinModule() : wxModule() {}
1201 bool OnInit() { return TRUE
; }
1202 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
1205 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
1208 // This hack forces the linker to always link in m_* files
1209 // (wxHTML doesn't work without handlers from these files)
1210 #include "wx/html/forcelnk.h"
1211 FORCE_WXHTML_MODULES()