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 wxHtmlProcessorList
*wxHtmlWindow::m_GlobalProcessors
= NULL
;
696 void wxHtmlWindow::CleanUpStatics()
698 wxDELETE(m_DefaultFilter
);
699 m_Filters
.DeleteContents(TRUE
);
701 wxDELETE(m_GlobalProcessors
);
706 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
708 m_Filters
.Append(filter
);
712 bool wxHtmlWindow::IsSelectionEnabled() const
715 return !(m_Style
& wxHW_NO_SELECTION
);
723 wxString
wxHtmlWindow::SelectionToText()
726 return wxEmptyString
;
730 const wxHtmlCell
*end
= m_selection
->GetToCell();
732 wxHtmlTerminalCellsInterator
i(m_selection
->GetFromCell(), end
);
735 text
<< i
->ConvertToText(m_selection
);
738 const wxHtmlCell
*prev
= *i
;
741 if ( prev
->GetParent() != i
->GetParent() )
743 text
<< i
->ConvertToText(*i
== end
? m_selection
: NULL
);
750 void wxHtmlWindow::CopySelection(ClipboardType t
)
754 wxTheClipboard
->UsePrimarySelection(t
== Primary
);
755 wxString
txt(SelectionToText());
756 if ( wxTheClipboard
->Open() )
758 wxTheClipboard
->SetData(new wxTextDataObject(txt
));
759 wxTheClipboard
->Close();
760 wxLogTrace(_T("wxhtmlselection"),
761 _("Copied to clipboard:\"%s\""), txt
.c_str());
768 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
770 const wxMouseEvent
*e
= link
.GetEvent();
771 if (e
== NULL
|| e
->LeftUp())
772 LoadPage(link
.GetHref());
775 void wxHtmlWindow::OnCellClicked(wxHtmlCell
*cell
,
776 wxCoord x
, wxCoord y
,
777 const wxMouseEvent
& event
)
779 wxCHECK_RET( cell
, _T("can't be called with NULL cell") );
781 cell
->OnMouseClick(this, x
, y
, event
);
784 void wxHtmlWindow::OnCellMouseHover(wxHtmlCell
* WXUNUSED(cell
),
785 wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
790 void wxHtmlWindow::OnEraseBackground(wxEraseEvent
& event
)
794 void wxHtmlWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
798 if (m_tmpCanDrawLocks
> 0 || m_Cell
== NULL
) return;
801 GetViewStart(&x
, &y
);
802 wxRect rect
= GetUpdateRegion().GetBox();
803 wxSize sz
= GetSize();
807 m_backBuffer
= new wxBitmap(sz
.x
, sz
.y
);
808 dcm
.SelectObject(*m_backBuffer
);
809 dcm
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
812 dcm
.SetMapMode(wxMM_TEXT
);
813 dcm
.SetBackgroundMode(wxTRANSPARENT
);
815 wxHtmlRenderingInfo rinfo
;
816 wxDefaultHtmlRenderingStyle rstyle
;
817 rinfo
.SetSelection(m_selection
);
818 rinfo
.SetStyle(&rstyle
);
819 m_Cell
->Draw(dcm
, 0, 0,
820 y
* wxHTML_SCROLL_STEP
+ rect
.GetTop(),
821 y
* wxHTML_SCROLL_STEP
+ rect
.GetBottom(),
824 dcm
.SetDeviceOrigin(0,0);
825 dc
.Blit(0, rect
.GetTop(),
826 sz
.x
, rect
.GetBottom() - rect
.GetTop() + 1,
834 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
836 wxDELETE(m_backBuffer
);
838 wxScrolledWindow::OnSize(event
);
841 // Recompute selection if necessary:
844 m_selection
->Set(m_selection
->GetFromCell(),
845 m_selection
->GetToCell());
846 m_selection
->ClearPrivPos();
853 void wxHtmlWindow::OnMouseMove(wxMouseEvent
& event
)
855 m_tmpMouseMoved
= true;
858 void wxHtmlWindow::OnMouseDown(wxMouseEvent
& event
)
861 if ( event
.LeftDown() && IsSelectionEnabled() )
863 const long TRIPLECLICK_LEN
= 200; // 0.2 sec after doubleclick
864 if ( wxGetLocalTimeMillis() - m_lastDoubleClick
<= TRIPLECLICK_LEN
)
866 SelectLine(CalcUnscrolledPosition(event
.GetPosition()));
870 m_makingSelection
= true;
874 wxDELETE(m_selection
);
877 m_tmpSelFromPos
= CalcUnscrolledPosition(event
.GetPosition());
878 m_tmpSelFromCell
= NULL
;
886 void wxHtmlWindow::OnMouseUp(wxMouseEvent
& event
)
889 if ( m_makingSelection
)
892 m_makingSelection
= false;
894 // did the user move the mouse far enough from starting point?
898 CopySelection(Primary
);
900 // we don't want mouse up event that ended selecting to be
901 // handled as mouse click and e.g. follow hyperlink:
910 wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
911 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
913 // VZ: is it possible that we don't find anything at all?
914 // VS: yes. FindCellByPos returns terminal cell and
915 // containers may have empty borders
917 OnCellClicked(cell
, pos
.x
, pos
.y
, event
);
923 void wxHtmlWindow::OnIdle(wxIdleEvent
& WXUNUSED(event
))
925 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
))
928 wxGetMousePosition(&xc
, &yc
);
929 ScreenToClient(&xc
, &yc
);
930 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
932 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(x
, y
);
934 // handle selection update:
935 if ( m_makingSelection
)
937 bool goingDown
= m_tmpSelFromPos
.y
< y
||
938 m_tmpSelFromPos
.y
== y
&& m_tmpSelFromPos
.x
< x
;
940 if ( !m_tmpSelFromCell
)
944 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
945 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
946 wxHTML_FIND_NEAREST_AFTER
);
947 if (!m_tmpSelFromCell
)
948 m_tmpSelFromCell
= m_Cell
->GetFirstTerminal();
952 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
953 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
954 wxHTML_FIND_NEAREST_BEFORE
);
955 if (!m_tmpSelFromCell
)
956 m_tmpSelFromCell
= m_Cell
->GetLastTerminal();
960 wxHtmlCell
*selcell
= cell
;
965 selcell
= m_Cell
->FindCellByPos(x
, y
,
966 wxHTML_FIND_NEAREST_AFTER
);
968 selcell
= m_Cell
->GetLastTerminal();
972 selcell
= m_Cell
->FindCellByPos(x
, y
,
973 wxHTML_FIND_NEAREST_BEFORE
);
975 selcell
= m_Cell
->GetFirstTerminal();
979 // NB: it may *rarely* happen that the code above didn't find one
980 // of the cells, e.g. if wxHtmlWindow doesn't contain any
982 if ( selcell
&& m_tmpSelFromCell
)
986 // start selecting only if mouse movement was big enough
987 // (otherwise it was meant as mouse click, not selection):
988 const int PRECISION
= 2;
989 wxPoint diff
= m_tmpSelFromPos
- wxPoint(x
,y
);
990 if (abs(diff
.x
) > PRECISION
|| abs(diff
.y
) > PRECISION
)
992 m_selection
= new wxHtmlSelection();
997 if ( m_tmpSelFromCell
->IsBefore(selcell
) )
999 m_selection
->Set(m_tmpSelFromPos
, m_tmpSelFromCell
,
1000 wxPoint(x
,y
), selcell
); }
1003 m_selection
->Set(wxPoint(x
,y
), selcell
,
1004 m_tmpSelFromPos
, m_tmpSelFromCell
);
1006 m_selection
->ClearPrivPos();
1012 // handle cursor and status bar text changes:
1013 if ( cell
!= m_tmpLastCell
)
1015 wxHtmlLinkInfo
*lnk
= cell
? cell
->GetLink(x
, y
) : NULL
;
1018 cur
= cell
->GetCursor();
1020 cur
= *wxSTANDARD_CURSOR
;
1023 if (lnk
!= m_tmpLastLink
)
1027 if (m_RelatedStatusBar
!= -1)
1028 m_RelatedFrame
->SetStatusText(wxEmptyString
,
1029 m_RelatedStatusBar
);
1033 if (m_RelatedStatusBar
!= -1)
1034 m_RelatedFrame
->SetStatusText(lnk
->GetHref(),
1035 m_RelatedStatusBar
);
1037 m_tmpLastLink
= lnk
;
1040 m_tmpLastCell
= cell
;
1042 else // mouse moved but stayed in the same cell
1045 OnCellMouseHover(cell
, x
, y
);
1048 m_tmpMouseMoved
= FALSE
;
1053 void wxHtmlWindow::StopAutoScrolling()
1055 if ( m_timerAutoScroll
)
1057 wxDELETE(m_timerAutoScroll
);
1061 void wxHtmlWindow::OnMouseEnter(wxMouseEvent
& event
)
1063 StopAutoScrolling();
1067 void wxHtmlWindow::OnMouseLeave(wxMouseEvent
& event
)
1069 // don't prevent the usual processing of the event from taking place
1072 // when a captured mouse leave a scrolled window we start generate
1073 // scrolling events to allow, for example, extending selection beyond the
1074 // visible area in some controls
1075 if ( wxWindow::GetCapture() == this )
1077 // where is the mouse leaving?
1079 wxPoint pt
= event
.GetPosition();
1082 orient
= wxHORIZONTAL
;
1085 else if ( pt
.y
< 0 )
1087 orient
= wxVERTICAL
;
1090 else // we're lower or to the right of the window
1092 wxSize size
= GetClientSize();
1093 if ( pt
.x
> size
.x
)
1095 orient
= wxHORIZONTAL
;
1096 pos
= GetVirtualSize().x
/ wxHTML_SCROLL_STEP
;
1098 else if ( pt
.y
> size
.y
)
1100 orient
= wxVERTICAL
;
1101 pos
= GetVirtualSize().y
/ wxHTML_SCROLL_STEP
;
1103 else // this should be impossible
1105 // but seems to happen sometimes under wxMSW - maybe it's a bug
1106 // there but for now just ignore it
1108 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1114 // only start the auto scroll timer if the window can be scrolled in
1116 if ( !HasScrollbar(orient
) )
1119 delete m_timerAutoScroll
;
1120 m_timerAutoScroll
= new wxHtmlWinAutoScrollTimer
1123 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1124 : wxEVT_SCROLLWIN_LINEDOWN
,
1128 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1132 void wxHtmlWindow::OnKeyUp(wxKeyEvent
& event
)
1134 if ( IsSelectionEnabled() &&
1135 event
.GetKeyCode() == 'C' && event
.ControlDown() )
1142 void wxHtmlWindow::OnCopy(wxCommandEvent
& event
)
1148 void wxHtmlWindow::OnDoubleClick(wxMouseEvent
& event
)
1150 // select word under cursor:
1151 if ( IsSelectionEnabled() )
1153 SelectWord(CalcUnscrolledPosition(event
.GetPosition()));
1154 m_lastDoubleClick
= wxGetLocalTimeMillis();
1160 void wxHtmlWindow::SelectWord(const wxPoint
& pos
)
1162 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1166 m_selection
= new wxHtmlSelection();
1167 m_selection
->Set(cell
, cell
);
1168 RefreshRect(wxRect(CalcScrolledPosition(cell
->GetAbsPos()),
1169 wxSize(cell
->GetWidth(), cell
->GetHeight())));
1173 void wxHtmlWindow::SelectLine(const wxPoint
& pos
)
1175 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1178 // We use following heuristic to find a "line": let the line be all
1179 // cells in same container as the cell under mouse cursor that are
1180 // neither completely above nor completely bellow the clicked cell
1181 // (i.e. are likely to be words positioned on same line of text).
1183 int y1
= cell
->GetAbsPos().y
;
1184 int y2
= y1
+ cell
->GetHeight();
1186 const wxHtmlCell
*c
;
1187 const wxHtmlCell
*before
= NULL
;
1188 const wxHtmlCell
*after
= NULL
;
1190 // find last cell of line:
1191 for ( c
= cell
->GetNext(); c
; c
= c
->GetNext())
1193 y
= c
->GetAbsPos().y
;
1194 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1202 // find first cell of line:
1203 for ( c
= cell
->GetParent()->GetFirstChild();
1204 c
&& c
!= cell
; c
= c
->GetNext())
1206 y
= c
->GetAbsPos().y
;
1207 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1219 m_selection
= new wxHtmlSelection();
1220 m_selection
->Set(before
, after
);
1229 IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor
,wxObject
)
1231 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
1233 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
1234 EVT_SIZE(wxHtmlWindow::OnSize
)
1235 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseDown
)
1236 EVT_LEFT_UP(wxHtmlWindow::OnMouseUp
)
1237 EVT_RIGHT_UP(wxHtmlWindow::OnMouseUp
)
1238 EVT_MOTION(wxHtmlWindow::OnMouseMove
)
1239 EVT_IDLE(wxHtmlWindow::OnIdle
)
1240 EVT_ERASE_BACKGROUND(wxHtmlWindow::OnEraseBackground
)
1241 EVT_PAINT(wxHtmlWindow::OnPaint
)
1243 EVT_LEFT_DCLICK(wxHtmlWindow::OnDoubleClick
)
1244 EVT_ENTER_WINDOW(wxHtmlWindow::OnMouseEnter
)
1245 EVT_LEAVE_WINDOW(wxHtmlWindow::OnMouseLeave
)
1246 EVT_KEY_UP(wxHtmlWindow::OnKeyUp
)
1247 EVT_MENU(wxID_COPY
, wxHtmlWindow::OnCopy
)
1255 // A module to allow initialization/cleanup
1256 // without calling these functions from app.cpp or from
1257 // the user's application.
1259 class wxHtmlWinModule
: public wxModule
1261 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
1263 wxHtmlWinModule() : wxModule() {}
1264 bool OnInit() { return TRUE
; }
1265 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
1268 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
1271 // This hack forces the linker to always link in m_* files
1272 // (wxHTML doesn't work without handlers from these files)
1273 #include "wx/html/forcelnk.h"
1274 FORCE_WXHTML_MODULES()