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
;
177 m_lastDoubleClick
= 0;
182 bool wxHtmlWindow::Create(wxWindow
*parent
, wxWindowID id
,
183 const wxPoint
& pos
, const wxSize
& size
,
184 long style
, const wxString
& name
)
186 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
187 style
| wxVSCROLL
| wxHSCROLL
, name
))
191 SetPage(wxT("<html><body></body></html>"));
196 wxHtmlWindow::~wxHtmlWindow()
203 if (m_Cell
) delete m_Cell
;
214 void wxHtmlWindow::SetRelatedFrame(wxFrame
* frame
, const wxString
& format
)
216 m_RelatedFrame
= frame
;
217 m_TitleFormat
= format
;
222 void wxHtmlWindow::SetRelatedStatusBar(int bar
)
224 m_RelatedStatusBar
= bar
;
229 void wxHtmlWindow::SetFonts(wxString normal_face
, wxString fixed_face
, const int *sizes
)
231 wxString op
= m_OpenedPage
;
233 m_Parser
->SetFonts(normal_face
, fixed_face
, sizes
);
234 // fonts changed => contents invalid, so reload the page:
235 SetPage(wxT("<html><body></body></html>"));
236 if (!op
.IsEmpty()) LoadPage(op
);
241 bool wxHtmlWindow::SetPage(const wxString
& source
)
243 wxString
newsrc(source
);
245 wxDELETE(m_selection
);
247 // pass HTML through registered processors:
248 if (m_Processors
|| m_GlobalProcessors
)
250 wxHtmlProcessorList::Node
*nodeL
, *nodeG
;
253 nodeL
= (m_Processors
) ? m_Processors
->GetFirst() : NULL
;
254 nodeG
= (m_GlobalProcessors
) ? m_GlobalProcessors
->GetFirst() : NULL
;
256 // VS: there are two lists, global and local, both of them sorted by
257 // priority. Since we have to go through _both_ lists with
258 // decreasing priority, we "merge-sort" the lists on-line by
259 // processing that one of the two heads that has higher priority
260 // in every iteration
261 while (nodeL
|| nodeG
)
263 prL
= (nodeL
) ? nodeL
->GetData()->GetPriority() : -1;
264 prG
= (nodeG
) ? nodeG
->GetData()->GetPriority() : -1;
267 if (nodeL
->GetData()->IsEnabled())
268 newsrc
= nodeL
->GetData()->Process(newsrc
);
269 nodeL
= nodeL
->GetNext();
273 if (nodeG
->GetData()->IsEnabled())
274 newsrc
= nodeG
->GetData()->Process(newsrc
);
275 nodeG
= nodeG
->GetNext();
280 // ...and run the parser on it:
281 wxClientDC
*dc
= new wxClientDC(this);
282 dc
->SetMapMode(wxMM_TEXT
);
283 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
284 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
291 m_Cell
= (wxHtmlContainerCell
*) m_Parser
->Parse(newsrc
);
293 m_Cell
->SetIndent(m_Borders
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
294 m_Cell
->SetAlignHor(wxHTML_ALIGN_CENTER
);
296 if (m_tmpCanDrawLocks
== 0)
301 bool wxHtmlWindow::AppendToPage(const wxString
& source
)
303 return SetPage(*(GetParser()->GetSource()) + source
);
306 bool wxHtmlWindow::LoadPage(const wxString
& location
)
308 wxBusyCursor busyCursor
;
312 bool needs_refresh
= FALSE
;
315 if (m_HistoryOn
&& (m_HistoryPos
!= -1))
317 // store scroll position into history item:
319 GetViewStart(&x
, &y
);
320 (*m_History
)[m_HistoryPos
].SetPos(y
);
323 if (location
[0] == wxT('#'))
326 wxString anch
= location
.Mid(1) /*1 to end*/;
328 rt_val
= ScrollToAnchor(anch
);
331 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&& location
.BeforeFirst(wxT('#')) == m_OpenedPage
)
333 wxString anch
= location
.AfterFirst(wxT('#'));
335 rt_val
= ScrollToAnchor(anch
);
338 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&&
339 (m_FS
->GetPath() + location
.BeforeFirst(wxT('#'))) == m_OpenedPage
)
341 wxString anch
= location
.AfterFirst(wxT('#'));
343 rt_val
= ScrollToAnchor(anch
);
349 needs_refresh
= TRUE
;
351 if (m_RelatedStatusBar
!= -1)
353 m_RelatedFrame
->SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
357 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location
);
359 // try to interpret 'location' as filename instead of URL:
362 wxFileName
fn(location
);
363 wxString location2
= wxFileSystem::FileNameToURL(fn
);
364 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location2
);
369 wxLogError(_("Unable to open requested HTML document: %s"), location
.c_str());
377 wxString src
= wxEmptyString
;
379 if (m_RelatedStatusBar
!= -1)
381 wxString msg
= _("Loading : ") + location
;
382 m_RelatedFrame
->SetStatusText(msg
, m_RelatedStatusBar
);
386 node
= m_Filters
.GetFirst();
389 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
392 src
= h
->ReadFile(*f
);
395 node
= node
->GetNext();
397 if (src
== wxEmptyString
)
399 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
400 src
= m_DefaultFilter
->ReadFile(*f
);
403 m_FS
->ChangePathTo(f
->GetLocation());
404 rt_val
= SetPage(src
);
405 m_OpenedPage
= f
->GetLocation();
406 if (f
->GetAnchor() != wxEmptyString
)
408 ScrollToAnchor(f
->GetAnchor());
413 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
->SetStatusText(_("Done"), m_RelatedStatusBar
);
417 if (m_HistoryOn
) // add this page to history there:
419 int c
= m_History
->GetCount() - (m_HistoryPos
+ 1);
421 if (m_HistoryPos
< 0 ||
422 (*m_History
)[m_HistoryPos
].GetPage() != m_OpenedPage
||
423 (*m_History
)[m_HistoryPos
].GetAnchor() != m_OpenedAnchor
)
426 for (int i
= 0; i
< c
; i
++)
427 m_History
->RemoveAt(m_HistoryPos
);
428 m_History
->Add(new wxHtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
432 if (m_OpenedPageTitle
== wxEmptyString
)
433 OnSetTitle(wxFileNameFromPath(m_OpenedPage
));
447 bool wxHtmlWindow::LoadFile(const wxFileName
& filename
)
449 wxString url
= wxFileSystem::FileNameToURL(filename
);
450 return LoadPage(url
);
454 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
456 const wxHtmlCell
*c
= m_Cell
->Find(wxHTML_COND_ISANCHOR
, &anchor
);
459 wxLogWarning(_("HTML anchor %s does not exist."), anchor
.c_str());
466 for (y
= 0; c
!= NULL
; c
= c
->GetParent()) y
+= c
->GetPosY();
467 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
468 m_OpenedAnchor
= anchor
;
474 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
479 tit
.Printf(m_TitleFormat
, title
.c_str());
480 m_RelatedFrame
->SetTitle(tit
);
482 m_OpenedPageTitle
= title
;
489 void wxHtmlWindow::CreateLayout()
491 int ClientWidth
, ClientHeight
;
495 if (m_Style
& wxHW_SCROLLBAR_NEVER
)
497 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // always off
498 GetClientSize(&ClientWidth
, &ClientHeight
);
499 m_Cell
->Layout(ClientWidth
);
503 GetClientSize(&ClientWidth
, &ClientHeight
);
504 m_Cell
->Layout(ClientWidth
);
505 if (ClientHeight
< m_Cell
->GetHeight() + GetCharHeight())
508 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
509 m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
,
510 (m_Cell
->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
511 /*cheat: top-level frag is always container*/);
513 else /* we fit into window, no need for scrollbars */
515 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
516 GetClientSize(&ClientWidth
, &ClientHeight
);
517 m_Cell
->Layout(ClientWidth
); // ...and relayout
524 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
529 wxString p_fff
, p_ffn
;
531 if (path
!= wxEmptyString
)
533 oldpath
= cfg
->GetPath();
537 m_Borders
= cfg
->Read(wxT("wxHtmlWindow/Borders"), m_Borders
);
538 p_fff
= cfg
->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
539 p_ffn
= cfg
->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
540 for (int i
= 0; i
< 7; i
++)
542 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
543 p_fontsizes
[i
] = cfg
->Read(tmp
, m_Parser
->m_FontsSizes
[i
]);
545 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
547 if (path
!= wxEmptyString
)
548 cfg
->SetPath(oldpath
);
553 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
558 if (path
!= wxEmptyString
)
560 oldpath
= cfg
->GetPath();
564 cfg
->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders
);
565 cfg
->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
566 cfg
->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
567 for (int i
= 0; i
< 7; i
++)
569 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
570 cfg
->Write(tmp
, (long) m_Parser
->m_FontsSizes
[i
]);
573 if (path
!= wxEmptyString
)
574 cfg
->SetPath(oldpath
);
579 bool wxHtmlWindow::HistoryBack()
583 if (m_HistoryPos
< 1) return FALSE
;
585 // store scroll position into history item:
587 GetViewStart(&x
, &y
);
588 (*m_History
)[m_HistoryPos
].SetPos(y
);
590 // go to previous position:
593 l
= (*m_History
)[m_HistoryPos
].GetPage();
594 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
597 if (a
== wxEmptyString
) LoadPage(l
);
598 else LoadPage(l
+ wxT("#") + a
);
601 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
606 bool wxHtmlWindow::HistoryCanBack()
608 if (m_HistoryPos
< 1) return FALSE
;
613 bool wxHtmlWindow::HistoryForward()
617 if (m_HistoryPos
== -1) return FALSE
;
618 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
620 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
623 l
= (*m_History
)[m_HistoryPos
].GetPage();
624 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
627 if (a
== wxEmptyString
) LoadPage(l
);
628 else LoadPage(l
+ wxT("#") + a
);
631 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
636 bool wxHtmlWindow::HistoryCanForward()
638 if (m_HistoryPos
== -1) return FALSE
;
639 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
644 void wxHtmlWindow::HistoryClear()
650 void wxHtmlWindow::AddProcessor(wxHtmlProcessor
*processor
)
654 m_Processors
= new wxHtmlProcessorList
;
655 m_Processors
->DeleteContents(TRUE
);
657 wxHtmlProcessorList::Node
*node
;
659 for (node
= m_Processors
->GetFirst(); node
; node
= node
->GetNext())
661 if (processor
->GetPriority() > node
->GetData()->GetPriority())
663 m_Processors
->Insert(node
, processor
);
667 m_Processors
->Append(processor
);
670 /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor
*processor
)
672 if (!m_GlobalProcessors
)
674 m_GlobalProcessors
= new wxHtmlProcessorList
;
675 m_GlobalProcessors
->DeleteContents(TRUE
);
677 wxHtmlProcessorList::Node
*node
;
679 for (node
= m_GlobalProcessors
->GetFirst(); node
; node
= node
->GetNext())
681 if (processor
->GetPriority() > node
->GetData()->GetPriority())
683 m_GlobalProcessors
->Insert(node
, processor
);
687 m_GlobalProcessors
->Append(processor
);
692 wxList
wxHtmlWindow::m_Filters
;
693 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
694 wxCursor
*wxHtmlWindow::s_cur_hand
= NULL
;
695 wxCursor
*wxHtmlWindow::s_cur_arrow
= NULL
;
696 wxHtmlProcessorList
*wxHtmlWindow::m_GlobalProcessors
= NULL
;
698 void wxHtmlWindow::CleanUpStatics()
700 wxDELETE(m_DefaultFilter
);
701 m_Filters
.DeleteContents(TRUE
);
703 wxDELETE(m_GlobalProcessors
);
704 wxDELETE(s_cur_hand
);
705 wxDELETE(s_cur_arrow
);
710 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
712 m_Filters
.Append(filter
);
716 bool wxHtmlWindow::IsSelectionEnabled() const
719 return !(m_Style
& wxHW_NO_SELECTION
);
727 wxString
wxHtmlWindow::SelectionToText()
730 return wxEmptyString
;
734 const wxHtmlCell
*end
= m_selection
->GetToCell();
736 wxHtmlTerminalCellsInterator
i(m_selection
->GetFromCell(), end
);
739 text
<< i
->ConvertToText(m_selection
);
742 const wxHtmlCell
*prev
= *i
;
745 if ( prev
->GetParent() != i
->GetParent() )
747 text
<< i
->ConvertToText(*i
== end
? m_selection
: NULL
);
754 void wxHtmlWindow::CopySelection(ClipboardType t
)
758 wxTheClipboard
->UsePrimarySelection(t
== Primary
);
759 wxString
txt(SelectionToText());
760 if ( wxTheClipboard
->Open() )
762 wxTheClipboard
->SetData(new wxTextDataObject(txt
));
763 wxTheClipboard
->Close();
764 wxLogTrace(_T("wxhtmlselection"),
765 _("Copied to clipboard:\"%s\""), txt
.c_str());
772 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
774 const wxMouseEvent
*e
= link
.GetEvent();
775 if (e
== NULL
|| e
->LeftUp())
776 LoadPage(link
.GetHref());
779 void wxHtmlWindow::OnCellClicked(wxHtmlCell
*cell
,
780 wxCoord x
, wxCoord y
,
781 const wxMouseEvent
& event
)
783 wxCHECK_RET( cell
, _T("can't be called with NULL cell") );
785 cell
->OnMouseClick(this, x
, y
, event
);
788 void wxHtmlWindow::OnCellMouseHover(wxHtmlCell
* WXUNUSED(cell
),
789 wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
794 void wxHtmlWindow::OnEraseBackground(wxEraseEvent
& event
)
798 void wxHtmlWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
802 if (m_tmpCanDrawLocks
> 0 || m_Cell
== NULL
) return;
805 GetViewStart(&x
, &y
);
806 wxRect rect
= GetUpdateRegion().GetBox();
807 wxSize sz
= GetSize();
811 m_backBuffer
= new wxBitmap(sz
.x
, sz
.y
);
812 dcm
.SelectObject(*m_backBuffer
);
813 dcm
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
816 dcm
.SetMapMode(wxMM_TEXT
);
817 dcm
.SetBackgroundMode(wxTRANSPARENT
);
819 wxHtmlRenderingInfo rinfo
;
820 wxDefaultHtmlRenderingStyle rstyle
;
821 rinfo
.SetSelection(m_selection
);
822 rinfo
.SetStyle(&rstyle
);
823 m_Cell
->Draw(dcm
, 0, 0,
824 y
* wxHTML_SCROLL_STEP
+ rect
.GetTop(),
825 y
* wxHTML_SCROLL_STEP
+ rect
.GetBottom(),
828 dcm
.SetDeviceOrigin(0,0);
829 dc
.Blit(0, rect
.GetTop(),
830 sz
.x
, rect
.GetBottom() - rect
.GetTop() + 1,
838 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
840 wxDELETE(m_backBuffer
);
842 wxScrolledWindow::OnSize(event
);
845 // Recompute selection if necessary:
848 m_selection
->Set(m_selection
->GetFromCell(),
849 m_selection
->GetToCell());
850 m_selection
->ClearPrivPos();
857 void wxHtmlWindow::OnMouseMove(wxMouseEvent
& event
)
859 m_tmpMouseMoved
= true;
862 void wxHtmlWindow::OnMouseDown(wxMouseEvent
& event
)
865 if ( event
.LeftDown() && IsSelectionEnabled() )
867 const long TRIPLECLICK_LEN
= 200; // 0.2 sec after doubleclick
868 if ( wxGetLocalTimeMillis() - m_lastDoubleClick
<= TRIPLECLICK_LEN
)
870 SelectLine(CalcUnscrolledPosition(event
.GetPosition()));
874 m_makingSelection
= true;
878 wxDELETE(m_selection
);
881 m_tmpSelFromPos
= CalcUnscrolledPosition(event
.GetPosition());
882 m_tmpSelFromCell
= NULL
;
890 void wxHtmlWindow::OnMouseUp(wxMouseEvent
& event
)
893 if ( m_makingSelection
)
896 m_makingSelection
= false;
898 // did the user move the mouse far enough from starting point?
902 CopySelection(Primary
);
904 // we don't want mouse up event that ended selecting to be
905 // handled as mouse click and e.g. follow hyperlink:
914 wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
915 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
917 // VZ: is it possible that we don't find anything at all?
918 // VS: yes. FindCellByPos returns terminal cell and
919 // containers may have empty borders
921 OnCellClicked(cell
, pos
.x
, pos
.y
, event
);
927 void wxHtmlWindow::OnIdle(wxIdleEvent
& WXUNUSED(event
))
929 if (s_cur_hand
== NULL
)
931 s_cur_hand
= new wxCursor(wxCURSOR_HAND
);
932 s_cur_arrow
= new wxCursor(wxCURSOR_ARROW
);
935 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
))
938 wxGetMousePosition(&xc
, &yc
);
939 ScreenToClient(&xc
, &yc
);
940 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
942 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(x
, y
);
944 // handle selection update:
945 if ( m_makingSelection
)
947 bool goingDown
= m_tmpSelFromPos
.y
< y
||
948 m_tmpSelFromPos
.y
== y
&& m_tmpSelFromPos
.x
< x
;
950 if ( !m_tmpSelFromCell
)
954 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
955 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
956 wxHTML_FIND_NEAREST_AFTER
);
957 if (!m_tmpSelFromCell
)
958 m_tmpSelFromCell
= m_Cell
->GetFirstTerminal();
962 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
963 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
964 wxHTML_FIND_NEAREST_BEFORE
);
965 if (!m_tmpSelFromCell
)
966 m_tmpSelFromCell
= m_Cell
->GetLastTerminal();
970 wxHtmlCell
*selcell
= cell
;
975 selcell
= m_Cell
->FindCellByPos(x
, y
,
976 wxHTML_FIND_NEAREST_AFTER
);
978 selcell
= m_Cell
->GetLastTerminal();
982 selcell
= m_Cell
->FindCellByPos(x
, y
,
983 wxHTML_FIND_NEAREST_BEFORE
);
985 selcell
= m_Cell
->GetFirstTerminal();
989 // NB: it may *rarely* happen that the code above didn't find one
990 // of the cells, e.g. if wxHtmlWindow doesn't contain any
992 if ( selcell
&& m_tmpSelFromCell
)
996 // start selecting only if mouse movement was big enough
997 // (otherwise it was meant as mouse click, not selection):
998 const int PRECISION
= 2;
999 wxPoint diff
= m_tmpSelFromPos
- wxPoint(x
,y
);
1000 if (abs(diff
.x
) > PRECISION
|| abs(diff
.y
) > PRECISION
)
1002 m_selection
= new wxHtmlSelection();
1007 if ( m_tmpSelFromCell
->IsBefore(selcell
) )
1009 m_selection
->Set(m_tmpSelFromPos
, m_tmpSelFromCell
,
1010 wxPoint(x
,y
), selcell
); }
1013 m_selection
->Set(wxPoint(x
,y
), selcell
,
1014 m_tmpSelFromPos
, m_tmpSelFromCell
);
1016 m_selection
->ClearPrivPos();
1022 // handle cursor and status bar text changes:
1023 if ( cell
!= m_tmpLastCell
)
1025 wxHtmlLinkInfo
*lnk
= cell
? cell
->GetLink(x
, y
) : NULL
;
1027 if (lnk
!= m_tmpLastLink
)
1031 SetCursor(*s_cur_arrow
);
1032 if (m_RelatedStatusBar
!= -1)
1033 m_RelatedFrame
->SetStatusText(wxEmptyString
,
1034 m_RelatedStatusBar
);
1038 SetCursor(*s_cur_hand
);
1039 if (m_RelatedStatusBar
!= -1)
1040 m_RelatedFrame
->SetStatusText(lnk
->GetHref(),
1041 m_RelatedStatusBar
);
1043 m_tmpLastLink
= lnk
;
1046 m_tmpLastCell
= cell
;
1048 else // mouse moved but stayed in the same cell
1051 OnCellMouseHover(cell
, x
, y
);
1054 m_tmpMouseMoved
= FALSE
;
1059 void wxHtmlWindow::StopAutoScrolling()
1061 if ( m_timerAutoScroll
)
1063 wxDELETE(m_timerAutoScroll
);
1067 void wxHtmlWindow::OnMouseEnter(wxMouseEvent
& event
)
1069 StopAutoScrolling();
1073 void wxHtmlWindow::OnMouseLeave(wxMouseEvent
& event
)
1075 // don't prevent the usual processing of the event from taking place
1078 // when a captured mouse leave a scrolled window we start generate
1079 // scrolling events to allow, for example, extending selection beyond the
1080 // visible area in some controls
1081 if ( wxWindow::GetCapture() == this )
1083 // where is the mouse leaving?
1085 wxPoint pt
= event
.GetPosition();
1088 orient
= wxHORIZONTAL
;
1091 else if ( pt
.y
< 0 )
1093 orient
= wxVERTICAL
;
1096 else // we're lower or to the right of the window
1098 wxSize size
= GetClientSize();
1099 if ( pt
.x
> size
.x
)
1101 orient
= wxHORIZONTAL
;
1102 pos
= GetVirtualSize().x
/ wxHTML_SCROLL_STEP
;
1104 else if ( pt
.y
> size
.y
)
1106 orient
= wxVERTICAL
;
1107 pos
= GetVirtualSize().y
/ wxHTML_SCROLL_STEP
;
1109 else // this should be impossible
1111 // but seems to happen sometimes under wxMSW - maybe it's a bug
1112 // there but for now just ignore it
1114 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1120 // only start the auto scroll timer if the window can be scrolled in
1122 if ( !HasScrollbar(orient
) )
1125 delete m_timerAutoScroll
;
1126 m_timerAutoScroll
= new wxHtmlWinAutoScrollTimer
1129 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1130 : wxEVT_SCROLLWIN_LINEDOWN
,
1134 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1138 void wxHtmlWindow::OnKeyUp(wxKeyEvent
& event
)
1140 if ( IsSelectionEnabled() &&
1141 event
.GetKeyCode() == 'C' && event
.ControlDown() )
1148 void wxHtmlWindow::OnCopy(wxCommandEvent
& event
)
1154 void wxHtmlWindow::OnDoubleClick(wxMouseEvent
& event
)
1156 // select word under cursor:
1157 if ( IsSelectionEnabled() )
1159 SelectWord(CalcUnscrolledPosition(event
.GetPosition()));
1160 m_lastDoubleClick
= wxGetLocalTimeMillis();
1166 void wxHtmlWindow::SelectWord(const wxPoint
& pos
)
1168 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1172 m_selection
= new wxHtmlSelection();
1173 m_selection
->Set(cell
, cell
);
1174 RefreshRect(wxRect(CalcScrolledPosition(cell
->GetAbsPos()),
1175 wxSize(cell
->GetWidth(), cell
->GetHeight())));
1179 void wxHtmlWindow::SelectLine(const wxPoint
& pos
)
1181 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1184 // We use following heuristic to find a "line": let the line be all
1185 // cells in same container as the cell under mouse cursor that are
1186 // neither completely above nor completely bellow the clicked cell
1187 // (i.e. are likely to be words positioned on same line of text).
1189 int y1
= cell
->GetAbsPos().y
;
1190 int y2
= y1
+ cell
->GetHeight();
1192 const wxHtmlCell
*c
;
1193 const wxHtmlCell
*before
= NULL
;
1194 const wxHtmlCell
*after
= NULL
;
1196 // find last cell of line:
1197 for ( c
= cell
->GetNext(); c
; c
= c
->GetNext())
1199 y
= c
->GetAbsPos().y
;
1200 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1208 // find first cell of line:
1209 for ( c
= cell
->GetParent()->GetFirstChild();
1210 c
&& c
!= cell
; c
= c
->GetNext())
1212 y
= c
->GetAbsPos().y
;
1213 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1225 m_selection
= new wxHtmlSelection();
1226 m_selection
->Set(before
, after
);
1235 IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor
,wxObject
)
1237 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
1239 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
1240 EVT_SIZE(wxHtmlWindow::OnSize
)
1241 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseDown
)
1242 EVT_LEFT_UP(wxHtmlWindow::OnMouseUp
)
1243 EVT_RIGHT_UP(wxHtmlWindow::OnMouseUp
)
1244 EVT_MOTION(wxHtmlWindow::OnMouseMove
)
1245 EVT_IDLE(wxHtmlWindow::OnIdle
)
1246 EVT_ERASE_BACKGROUND(wxHtmlWindow::OnEraseBackground
)
1247 EVT_PAINT(wxHtmlWindow::OnPaint
)
1249 EVT_LEFT_DCLICK(wxHtmlWindow::OnDoubleClick
)
1250 EVT_ENTER_WINDOW(wxHtmlWindow::OnMouseEnter
)
1251 EVT_LEAVE_WINDOW(wxHtmlWindow::OnMouseLeave
)
1252 EVT_KEY_UP(wxHtmlWindow::OnKeyUp
)
1253 EVT_MENU(wxID_COPY
, wxHtmlWindow::OnCopy
)
1261 // A module to allow initialization/cleanup
1262 // without calling these functions from app.cpp or from
1263 // the user's application.
1265 class wxHtmlWinModule
: public wxModule
1267 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
1269 wxHtmlWinModule() : wxModule() {}
1270 bool OnInit() { return TRUE
; }
1271 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
1274 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
1277 // This hack forces the linker to always link in m_* files
1278 // (wxHTML doesn't work without handlers from these files)
1279 #include "wx/html/forcelnk.h"
1280 FORCE_WXHTML_MODULES()