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
114 #endif // wxUSE_CLIPBOARD
118 //-----------------------------------------------------------------------------
120 //-----------------------------------------------------------------------------
122 // item of history list
123 class WXDLLIMPEXP_HTML wxHtmlHistoryItem
126 wxHtmlHistoryItem(const wxString
& p
, const wxString
& a
) {m_Page
= p
, m_Anchor
= a
, m_Pos
= 0;}
127 int GetPos() const {return m_Pos
;}
128 void SetPos(int p
) {m_Pos
= p
;}
129 const wxString
& GetPage() const {return m_Page
;}
130 const wxString
& GetAnchor() const {return m_Anchor
;}
139 //-----------------------------------------------------------------------------
140 // our private arrays:
141 //-----------------------------------------------------------------------------
143 WX_DECLARE_OBJARRAY(wxHtmlHistoryItem
, wxHtmlHistoryArray
);
144 WX_DEFINE_OBJARRAY(wxHtmlHistoryArray
);
146 WX_DECLARE_LIST(wxHtmlProcessor
, wxHtmlProcessorList
);
147 WX_DEFINE_LIST(wxHtmlProcessorList
);
149 //-----------------------------------------------------------------------------
151 //-----------------------------------------------------------------------------
154 void wxHtmlWindow::Init()
156 m_tmpMouseMoved
= FALSE
;
157 m_tmpLastLink
= NULL
;
158 m_tmpLastCell
= NULL
;
159 m_tmpCanDrawLocks
= 0;
160 m_FS
= new wxFileSystem();
161 m_RelatedStatusBar
= -1;
162 m_RelatedFrame
= NULL
;
163 m_TitleFormat
= wxT("%s");
164 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
166 m_Parser
= new wxHtmlWinParser(this);
167 m_Parser
->SetFS(m_FS
);
170 m_History
= new wxHtmlHistoryArray
;
175 m_makingSelection
= false;
177 m_timerAutoScroll
= NULL
;
178 m_lastDoubleClick
= 0;
179 #endif // wxUSE_CLIPBOARD
183 bool wxHtmlWindow::Create(wxWindow
*parent
, wxWindowID id
,
184 const wxPoint
& pos
, const wxSize
& size
,
185 long style
, const wxString
& name
)
187 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
188 style
| wxVSCROLL
| wxHSCROLL
, name
))
192 SetPage(wxT("<html><body></body></html>"));
197 wxHtmlWindow::~wxHtmlWindow()
201 #endif // wxUSE_CLIPBOARD
204 if (m_Cell
) delete m_Cell
;
215 void wxHtmlWindow::SetRelatedFrame(wxFrame
* frame
, const wxString
& format
)
217 m_RelatedFrame
= frame
;
218 m_TitleFormat
= format
;
223 void wxHtmlWindow::SetRelatedStatusBar(int bar
)
225 m_RelatedStatusBar
= bar
;
230 void wxHtmlWindow::SetFonts(wxString normal_face
, wxString fixed_face
, const int *sizes
)
232 wxString op
= m_OpenedPage
;
234 m_Parser
->SetFonts(normal_face
, fixed_face
, sizes
);
235 // fonts changed => contents invalid, so reload the page:
236 SetPage(wxT("<html><body></body></html>"));
237 if (!op
.IsEmpty()) LoadPage(op
);
242 bool wxHtmlWindow::SetPage(const wxString
& source
)
244 wxString
newsrc(source
);
246 wxDELETE(m_selection
);
248 // pass HTML through registered processors:
249 if (m_Processors
|| m_GlobalProcessors
)
251 wxHtmlProcessorList::Node
*nodeL
, *nodeG
;
254 nodeL
= (m_Processors
) ? m_Processors
->GetFirst() : NULL
;
255 nodeG
= (m_GlobalProcessors
) ? m_GlobalProcessors
->GetFirst() : NULL
;
257 // VS: there are two lists, global and local, both of them sorted by
258 // priority. Since we have to go through _both_ lists with
259 // decreasing priority, we "merge-sort" the lists on-line by
260 // processing that one of the two heads that has higher priority
261 // in every iteration
262 while (nodeL
|| nodeG
)
264 prL
= (nodeL
) ? nodeL
->GetData()->GetPriority() : -1;
265 prG
= (nodeG
) ? nodeG
->GetData()->GetPriority() : -1;
268 if (nodeL
->GetData()->IsEnabled())
269 newsrc
= nodeL
->GetData()->Process(newsrc
);
270 nodeL
= nodeL
->GetNext();
274 if (nodeG
->GetData()->IsEnabled())
275 newsrc
= nodeG
->GetData()->Process(newsrc
);
276 nodeG
= nodeG
->GetNext();
281 // ...and run the parser on it:
282 wxClientDC
*dc
= new wxClientDC(this);
283 dc
->SetMapMode(wxMM_TEXT
);
284 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
285 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
292 m_Cell
= (wxHtmlContainerCell
*) m_Parser
->Parse(newsrc
);
294 m_Cell
->SetIndent(m_Borders
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
295 m_Cell
->SetAlignHor(wxHTML_ALIGN_CENTER
);
297 if (m_tmpCanDrawLocks
== 0)
302 bool wxHtmlWindow::AppendToPage(const wxString
& source
)
304 return SetPage(*(GetParser()->GetSource()) + source
);
307 bool wxHtmlWindow::LoadPage(const wxString
& location
)
309 wxBusyCursor busyCursor
;
313 bool needs_refresh
= FALSE
;
316 if (m_HistoryOn
&& (m_HistoryPos
!= -1))
318 // store scroll position into history item:
320 GetViewStart(&x
, &y
);
321 (*m_History
)[m_HistoryPos
].SetPos(y
);
324 if (location
[0] == wxT('#'))
327 wxString anch
= location
.Mid(1) /*1 to end*/;
329 rt_val
= ScrollToAnchor(anch
);
332 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&& location
.BeforeFirst(wxT('#')) == m_OpenedPage
)
334 wxString anch
= location
.AfterFirst(wxT('#'));
336 rt_val
= ScrollToAnchor(anch
);
339 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&&
340 (m_FS
->GetPath() + location
.BeforeFirst(wxT('#'))) == m_OpenedPage
)
342 wxString anch
= location
.AfterFirst(wxT('#'));
344 rt_val
= ScrollToAnchor(anch
);
350 needs_refresh
= TRUE
;
352 if (m_RelatedStatusBar
!= -1)
354 m_RelatedFrame
->SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
358 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location
);
360 // try to interpret 'location' as filename instead of URL:
363 wxFileName
fn(location
);
364 wxString location2
= wxFileSystem::FileNameToURL(fn
);
365 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location2
);
370 wxLogError(_("Unable to open requested HTML document: %s"), location
.c_str());
378 wxString src
= wxEmptyString
;
380 if (m_RelatedStatusBar
!= -1)
382 wxString msg
= _("Loading : ") + location
;
383 m_RelatedFrame
->SetStatusText(msg
, m_RelatedStatusBar
);
387 node
= m_Filters
.GetFirst();
390 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
393 src
= h
->ReadFile(*f
);
396 node
= node
->GetNext();
398 if (src
== wxEmptyString
)
400 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
401 src
= m_DefaultFilter
->ReadFile(*f
);
404 m_FS
->ChangePathTo(f
->GetLocation());
405 rt_val
= SetPage(src
);
406 m_OpenedPage
= f
->GetLocation();
407 if (f
->GetAnchor() != wxEmptyString
)
409 ScrollToAnchor(f
->GetAnchor());
414 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
->SetStatusText(_("Done"), m_RelatedStatusBar
);
418 if (m_HistoryOn
) // add this page to history there:
420 int c
= m_History
->GetCount() - (m_HistoryPos
+ 1);
422 if (m_HistoryPos
< 0 ||
423 (*m_History
)[m_HistoryPos
].GetPage() != m_OpenedPage
||
424 (*m_History
)[m_HistoryPos
].GetAnchor() != m_OpenedAnchor
)
427 for (int i
= 0; i
< c
; i
++)
428 m_History
->RemoveAt(m_HistoryPos
);
429 m_History
->Add(new wxHtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
433 if (m_OpenedPageTitle
== wxEmptyString
)
434 OnSetTitle(wxFileNameFromPath(m_OpenedPage
));
448 bool wxHtmlWindow::LoadFile(const wxFileName
& filename
)
450 wxString url
= wxFileSystem::FileNameToURL(filename
);
451 return LoadPage(url
);
455 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
457 const wxHtmlCell
*c
= m_Cell
->Find(wxHTML_COND_ISANCHOR
, &anchor
);
460 wxLogWarning(_("HTML anchor %s does not exist."), anchor
.c_str());
467 for (y
= 0; c
!= NULL
; c
= c
->GetParent()) y
+= c
->GetPosY();
468 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
469 m_OpenedAnchor
= anchor
;
475 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
480 tit
.Printf(m_TitleFormat
, title
.c_str());
481 m_RelatedFrame
->SetTitle(tit
);
483 m_OpenedPageTitle
= title
;
490 void wxHtmlWindow::CreateLayout()
492 int ClientWidth
, ClientHeight
;
496 if (m_Style
& wxHW_SCROLLBAR_NEVER
)
498 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // always off
499 GetClientSize(&ClientWidth
, &ClientHeight
);
500 m_Cell
->Layout(ClientWidth
);
504 GetClientSize(&ClientWidth
, &ClientHeight
);
505 m_Cell
->Layout(ClientWidth
);
506 if (ClientHeight
< m_Cell
->GetHeight() + GetCharHeight())
509 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
510 m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
,
511 (m_Cell
->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
512 /*cheat: top-level frag is always container*/);
514 else /* we fit into window, no need for scrollbars */
516 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
517 GetClientSize(&ClientWidth
, &ClientHeight
);
518 m_Cell
->Layout(ClientWidth
); // ...and relayout
525 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
530 wxString p_fff
, p_ffn
;
532 if (path
!= wxEmptyString
)
534 oldpath
= cfg
->GetPath();
538 m_Borders
= cfg
->Read(wxT("wxHtmlWindow/Borders"), m_Borders
);
539 p_fff
= cfg
->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
540 p_ffn
= cfg
->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
541 for (int i
= 0; i
< 7; i
++)
543 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
544 p_fontsizes
[i
] = cfg
->Read(tmp
, m_Parser
->m_FontsSizes
[i
]);
546 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
548 if (path
!= wxEmptyString
)
549 cfg
->SetPath(oldpath
);
554 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
559 if (path
!= wxEmptyString
)
561 oldpath
= cfg
->GetPath();
565 cfg
->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders
);
566 cfg
->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
567 cfg
->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
568 for (int i
= 0; i
< 7; i
++)
570 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
571 cfg
->Write(tmp
, (long) m_Parser
->m_FontsSizes
[i
]);
574 if (path
!= wxEmptyString
)
575 cfg
->SetPath(oldpath
);
580 bool wxHtmlWindow::HistoryBack()
584 if (m_HistoryPos
< 1) return FALSE
;
586 // store scroll position into history item:
588 GetViewStart(&x
, &y
);
589 (*m_History
)[m_HistoryPos
].SetPos(y
);
591 // go to previous position:
594 l
= (*m_History
)[m_HistoryPos
].GetPage();
595 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
598 if (a
== wxEmptyString
) LoadPage(l
);
599 else LoadPage(l
+ wxT("#") + a
);
602 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
607 bool wxHtmlWindow::HistoryCanBack()
609 if (m_HistoryPos
< 1) return FALSE
;
614 bool wxHtmlWindow::HistoryForward()
618 if (m_HistoryPos
== -1) return FALSE
;
619 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
621 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
624 l
= (*m_History
)[m_HistoryPos
].GetPage();
625 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
628 if (a
== wxEmptyString
) LoadPage(l
);
629 else LoadPage(l
+ wxT("#") + a
);
632 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
637 bool wxHtmlWindow::HistoryCanForward()
639 if (m_HistoryPos
== -1) return FALSE
;
640 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
645 void wxHtmlWindow::HistoryClear()
651 void wxHtmlWindow::AddProcessor(wxHtmlProcessor
*processor
)
655 m_Processors
= new wxHtmlProcessorList
;
656 m_Processors
->DeleteContents(TRUE
);
658 wxHtmlProcessorList::Node
*node
;
660 for (node
= m_Processors
->GetFirst(); node
; node
= node
->GetNext())
662 if (processor
->GetPriority() > node
->GetData()->GetPriority())
664 m_Processors
->Insert(node
, processor
);
668 m_Processors
->Append(processor
);
671 /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor
*processor
)
673 if (!m_GlobalProcessors
)
675 m_GlobalProcessors
= new wxHtmlProcessorList
;
676 m_GlobalProcessors
->DeleteContents(TRUE
);
678 wxHtmlProcessorList::Node
*node
;
680 for (node
= m_GlobalProcessors
->GetFirst(); node
; node
= node
->GetNext())
682 if (processor
->GetPriority() > node
->GetData()->GetPriority())
684 m_GlobalProcessors
->Insert(node
, processor
);
688 m_GlobalProcessors
->Append(processor
);
693 wxList
wxHtmlWindow::m_Filters
;
694 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
695 wxHtmlProcessorList
*wxHtmlWindow::m_GlobalProcessors
= NULL
;
697 void wxHtmlWindow::CleanUpStatics()
699 wxDELETE(m_DefaultFilter
);
700 m_Filters
.DeleteContents(TRUE
);
702 wxDELETE(m_GlobalProcessors
);
707 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
709 m_Filters
.Append(filter
);
713 bool wxHtmlWindow::IsSelectionEnabled() const
716 return !(m_Style
& wxHW_NO_SELECTION
);
724 wxString
wxHtmlWindow::SelectionToText()
727 return wxEmptyString
;
731 const wxHtmlCell
*end
= m_selection
->GetToCell();
733 wxHtmlTerminalCellsInterator
i(m_selection
->GetFromCell(), end
);
736 text
<< i
->ConvertToText(m_selection
);
739 const wxHtmlCell
*prev
= *i
;
742 if ( prev
->GetParent() != i
->GetParent() )
744 text
<< i
->ConvertToText(*i
== end
? m_selection
: NULL
);
751 #endif // wxUSE_CLIPBOARD
753 void wxHtmlWindow::CopySelection(ClipboardType t
)
759 wxTheClipboard
->UsePrimarySelection(t
== Primary
);
761 // Primary selection exists only under X11, so don't do anything under
762 // the other platforms when we try to access it
764 // TODO: this should be abstracted at wxClipboard level!
767 #endif // __UNIX__/!__UNIX__
769 if ( wxTheClipboard
->Open() )
771 const wxString
txt(SelectionToText());
772 wxTheClipboard
->SetData(new wxTextDataObject(txt
));
773 wxTheClipboard
->Close();
774 wxLogTrace(_T("wxhtmlselection"),
775 _("Copied to clipboard:\"%s\""), txt
.c_str());
778 #endif // wxUSE_CLIPBOARD
782 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
784 const wxMouseEvent
*e
= link
.GetEvent();
785 if (e
== NULL
|| e
->LeftUp())
786 LoadPage(link
.GetHref());
789 void wxHtmlWindow::OnCellClicked(wxHtmlCell
*cell
,
790 wxCoord x
, wxCoord y
,
791 const wxMouseEvent
& event
)
793 wxCHECK_RET( cell
, _T("can't be called with NULL cell") );
795 cell
->OnMouseClick(this, x
, y
, event
);
798 void wxHtmlWindow::OnCellMouseHover(wxHtmlCell
* WXUNUSED(cell
),
799 wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
804 void wxHtmlWindow::OnEraseBackground(wxEraseEvent
& event
)
808 void wxHtmlWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
812 if (m_tmpCanDrawLocks
> 0 || m_Cell
== NULL
) return;
815 GetViewStart(&x
, &y
);
816 wxRect rect
= GetUpdateRegion().GetBox();
817 wxSize sz
= GetSize();
821 m_backBuffer
= new wxBitmap(sz
.x
, sz
.y
);
822 dcm
.SelectObject(*m_backBuffer
);
823 dcm
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
826 dcm
.SetMapMode(wxMM_TEXT
);
827 dcm
.SetBackgroundMode(wxTRANSPARENT
);
829 wxHtmlRenderingInfo rinfo
;
830 wxDefaultHtmlRenderingStyle rstyle
;
831 rinfo
.SetSelection(m_selection
);
832 rinfo
.SetStyle(&rstyle
);
833 m_Cell
->Draw(dcm
, 0, 0,
834 y
* wxHTML_SCROLL_STEP
+ rect
.GetTop(),
835 y
* wxHTML_SCROLL_STEP
+ rect
.GetBottom(),
838 //#define DEBUG_HTML_SELECTION
839 #ifdef DEBUG_HTML_SELECTION
842 wxGetMousePosition(&xc
, &yc
);
843 ScreenToClient(&xc
, &yc
);
844 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
845 wxHtmlCell
*at
= m_Cell
->FindCellByPos(x
, y
);
847 m_Cell
->FindCellByPos(x
, y
, wxHTML_FIND_NEAREST_BEFORE
);
849 m_Cell
->FindCellByPos(x
, y
, wxHTML_FIND_NEAREST_AFTER
);
851 dcm
.SetBrush(*wxTRANSPARENT_BRUSH
);
852 dcm
.SetPen(*wxBLACK_PEN
);
854 dcm
.DrawRectangle(at
->GetAbsPos(),
855 wxSize(at
->GetWidth(),at
->GetHeight()));
856 dcm
.SetPen(*wxGREEN_PEN
);
858 dcm
.DrawRectangle(before
->GetAbsPos().x
+1, before
->GetAbsPos().y
+1,
859 before
->GetWidth()-2,before
->GetHeight()-2);
860 dcm
.SetPen(*wxRED_PEN
);
862 dcm
.DrawRectangle(after
->GetAbsPos().x
+2, after
->GetAbsPos().y
+2,
863 after
->GetWidth()-4,after
->GetHeight()-4);
867 dcm
.SetDeviceOrigin(0,0);
868 dc
.Blit(0, rect
.GetTop(),
869 sz
.x
, rect
.GetBottom() - rect
.GetTop() + 1,
877 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
879 wxDELETE(m_backBuffer
);
881 wxScrolledWindow::OnSize(event
);
884 // Recompute selection if necessary:
887 m_selection
->Set(m_selection
->GetFromCell(),
888 m_selection
->GetToCell());
889 m_selection
->ClearPrivPos();
896 void wxHtmlWindow::OnMouseMove(wxMouseEvent
& event
)
898 m_tmpMouseMoved
= true;
901 void wxHtmlWindow::OnMouseDown(wxMouseEvent
& event
)
904 if ( event
.LeftDown() && IsSelectionEnabled() )
906 const long TRIPLECLICK_LEN
= 200; // 0.2 sec after doubleclick
907 if ( wxGetLocalTimeMillis() - m_lastDoubleClick
<= TRIPLECLICK_LEN
)
909 SelectLine(CalcUnscrolledPosition(event
.GetPosition()));
915 m_makingSelection
= true;
919 wxDELETE(m_selection
);
922 m_tmpSelFromPos
= CalcUnscrolledPosition(event
.GetPosition());
923 m_tmpSelFromCell
= NULL
;
928 #endif // wxUSE_CLIPBOARD
931 void wxHtmlWindow::OnMouseUp(wxMouseEvent
& event
)
934 if ( m_makingSelection
)
937 m_makingSelection
= false;
939 // did the user move the mouse far enough from starting point?
942 CopySelection(Primary
);
944 // we don't want mouse up event that ended selecting to be
945 // handled as mouse click and e.g. follow hyperlink:
949 #endif // wxUSE_CLIPBOARD
954 wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
955 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
957 // check is needed because FindCellByPos returns terminal cell and
958 // containers may have empty borders -- in this case NULL will be
961 OnCellClicked(cell
, pos
.x
, pos
.y
, event
);
967 void wxHtmlWindow::OnInternalIdle()
969 wxWindow::OnInternalIdle();
971 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
))
973 #ifdef DEBUG_HTML_SELECTION
977 wxGetMousePosition(&xc
, &yc
);
978 ScreenToClient(&xc
, &yc
);
979 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
981 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(x
, y
);
983 // handle selection update:
984 if ( m_makingSelection
)
986 if ( !m_tmpSelFromCell
)
987 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
988 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
);
990 // NB: a trick - we adjust selFromPos to be upper left or bottom
991 // right corner of the first cell of the selection depending
992 // on whether the mouse is moving to the right or to the left.
993 // This gives us more "natural" behaviour when selecting
994 // a line (specifically, first cell of the next line is not
995 // included if you drag selection from left to right over
998 if ( !m_tmpSelFromCell
)
1000 dirFromPos
= m_tmpSelFromPos
;
1004 dirFromPos
= m_tmpSelFromCell
->GetAbsPos();
1005 if ( x
< m_tmpSelFromPos
.x
)
1007 dirFromPos
.x
+= m_tmpSelFromCell
->GetWidth();
1008 dirFromPos
.y
+= m_tmpSelFromCell
->GetHeight();
1011 bool goingDown
= dirFromPos
.y
< y
||
1012 (dirFromPos
.y
== y
&& dirFromPos
.x
< x
);
1014 // determine selection span:
1015 if ( /*still*/ !m_tmpSelFromCell
)
1019 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1020 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1021 wxHTML_FIND_NEAREST_AFTER
);
1022 if (!m_tmpSelFromCell
)
1023 m_tmpSelFromCell
= m_Cell
->GetFirstTerminal();
1027 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1028 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1029 wxHTML_FIND_NEAREST_BEFORE
);
1030 if (!m_tmpSelFromCell
)
1031 m_tmpSelFromCell
= m_Cell
->GetLastTerminal();
1035 wxHtmlCell
*selcell
= cell
;
1040 selcell
= m_Cell
->FindCellByPos(x
, y
,
1041 wxHTML_FIND_NEAREST_BEFORE
);
1043 selcell
= m_Cell
->GetLastTerminal();
1047 selcell
= m_Cell
->FindCellByPos(x
, y
,
1048 wxHTML_FIND_NEAREST_AFTER
);
1050 selcell
= m_Cell
->GetFirstTerminal();
1054 // NB: it may *rarely* happen that the code above didn't find one
1055 // of the cells, e.g. if wxHtmlWindow doesn't contain any
1057 if ( selcell
&& m_tmpSelFromCell
)
1061 // start selecting only if mouse movement was big enough
1062 // (otherwise it was meant as mouse click, not selection):
1063 const int PRECISION
= 2;
1064 wxPoint diff
= m_tmpSelFromPos
- wxPoint(x
,y
);
1065 if (abs(diff
.x
) > PRECISION
|| abs(diff
.y
) > PRECISION
)
1067 m_selection
= new wxHtmlSelection();
1072 if ( m_tmpSelFromCell
->IsBefore(selcell
) )
1074 m_selection
->Set(m_tmpSelFromPos
, m_tmpSelFromCell
,
1075 wxPoint(x
,y
), selcell
); }
1078 m_selection
->Set(wxPoint(x
,y
), selcell
,
1079 m_tmpSelFromPos
, m_tmpSelFromCell
);
1081 m_selection
->ClearPrivPos();
1087 // handle cursor and status bar text changes:
1088 if ( cell
!= m_tmpLastCell
)
1090 wxHtmlLinkInfo
*lnk
= cell
? cell
->GetLink(x
, y
) : NULL
;
1093 cur
= cell
->GetCursor();
1095 cur
= *wxSTANDARD_CURSOR
;
1098 if (lnk
!= m_tmpLastLink
)
1102 if (m_RelatedStatusBar
!= -1)
1103 m_RelatedFrame
->SetStatusText(wxEmptyString
,
1104 m_RelatedStatusBar
);
1108 if (m_RelatedStatusBar
!= -1)
1109 m_RelatedFrame
->SetStatusText(lnk
->GetHref(),
1110 m_RelatedStatusBar
);
1112 m_tmpLastLink
= lnk
;
1115 m_tmpLastCell
= cell
;
1117 else // mouse moved but stayed in the same cell
1120 OnCellMouseHover(cell
, x
, y
);
1123 m_tmpMouseMoved
= FALSE
;
1128 void wxHtmlWindow::StopAutoScrolling()
1130 if ( m_timerAutoScroll
)
1132 wxDELETE(m_timerAutoScroll
);
1136 void wxHtmlWindow::OnMouseEnter(wxMouseEvent
& event
)
1138 StopAutoScrolling();
1142 void wxHtmlWindow::OnMouseLeave(wxMouseEvent
& event
)
1144 // don't prevent the usual processing of the event from taking place
1147 // when a captured mouse leave a scrolled window we start generate
1148 // scrolling events to allow, for example, extending selection beyond the
1149 // visible area in some controls
1150 if ( wxWindow::GetCapture() == this )
1152 // where is the mouse leaving?
1154 wxPoint pt
= event
.GetPosition();
1157 orient
= wxHORIZONTAL
;
1160 else if ( pt
.y
< 0 )
1162 orient
= wxVERTICAL
;
1165 else // we're lower or to the right of the window
1167 wxSize size
= GetClientSize();
1168 if ( pt
.x
> size
.x
)
1170 orient
= wxHORIZONTAL
;
1171 pos
= GetVirtualSize().x
/ wxHTML_SCROLL_STEP
;
1173 else if ( pt
.y
> size
.y
)
1175 orient
= wxVERTICAL
;
1176 pos
= GetVirtualSize().y
/ wxHTML_SCROLL_STEP
;
1178 else // this should be impossible
1180 // but seems to happen sometimes under wxMSW - maybe it's a bug
1181 // there but for now just ignore it
1183 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1189 // only start the auto scroll timer if the window can be scrolled in
1191 if ( !HasScrollbar(orient
) )
1194 delete m_timerAutoScroll
;
1195 m_timerAutoScroll
= new wxHtmlWinAutoScrollTimer
1198 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1199 : wxEVT_SCROLLWIN_LINEDOWN
,
1203 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1207 void wxHtmlWindow::OnKeyUp(wxKeyEvent
& event
)
1209 if ( IsSelectionEnabled() &&
1210 event
.GetKeyCode() == 'C' && event
.ControlDown() )
1217 void wxHtmlWindow::OnCopy(wxCommandEvent
& event
)
1223 void wxHtmlWindow::OnDoubleClick(wxMouseEvent
& event
)
1225 // select word under cursor:
1226 if ( IsSelectionEnabled() )
1228 SelectWord(CalcUnscrolledPosition(event
.GetPosition()));
1230 CopySelection(Primary
);
1232 m_lastDoubleClick
= wxGetLocalTimeMillis();
1238 void wxHtmlWindow::SelectWord(const wxPoint
& pos
)
1240 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1244 m_selection
= new wxHtmlSelection();
1245 m_selection
->Set(cell
, cell
);
1246 RefreshRect(wxRect(CalcScrolledPosition(cell
->GetAbsPos()),
1247 wxSize(cell
->GetWidth(), cell
->GetHeight())));
1251 void wxHtmlWindow::SelectLine(const wxPoint
& pos
)
1253 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1256 // We use following heuristic to find a "line": let the line be all
1257 // cells in same container as the cell under mouse cursor that are
1258 // neither completely above nor completely bellow the clicked cell
1259 // (i.e. are likely to be words positioned on same line of text).
1261 int y1
= cell
->GetAbsPos().y
;
1262 int y2
= y1
+ cell
->GetHeight();
1264 const wxHtmlCell
*c
;
1265 const wxHtmlCell
*before
= NULL
;
1266 const wxHtmlCell
*after
= NULL
;
1268 // find last cell of line:
1269 for ( c
= cell
->GetNext(); c
; c
= c
->GetNext())
1271 y
= c
->GetAbsPos().y
;
1272 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1280 // find first cell of line:
1281 for ( c
= cell
->GetParent()->GetFirstChild();
1282 c
&& c
!= cell
; c
= c
->GetNext())
1284 y
= c
->GetAbsPos().y
;
1285 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1297 m_selection
= new wxHtmlSelection();
1298 m_selection
->Set(before
, after
);
1303 #endif // wxUSE_CLIPBOARD
1307 IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor
,wxObject
)
1309 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
1311 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
1312 EVT_SIZE(wxHtmlWindow::OnSize
)
1313 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseDown
)
1314 EVT_LEFT_UP(wxHtmlWindow::OnMouseUp
)
1315 EVT_RIGHT_UP(wxHtmlWindow::OnMouseUp
)
1316 EVT_MOTION(wxHtmlWindow::OnMouseMove
)
1317 EVT_ERASE_BACKGROUND(wxHtmlWindow::OnEraseBackground
)
1318 EVT_PAINT(wxHtmlWindow::OnPaint
)
1320 EVT_LEFT_DCLICK(wxHtmlWindow::OnDoubleClick
)
1321 EVT_ENTER_WINDOW(wxHtmlWindow::OnMouseEnter
)
1322 EVT_LEAVE_WINDOW(wxHtmlWindow::OnMouseLeave
)
1323 EVT_KEY_UP(wxHtmlWindow::OnKeyUp
)
1324 EVT_MENU(wxID_COPY
, wxHtmlWindow::OnCopy
)
1325 #endif // wxUSE_CLIPBOARD
1332 // A module to allow initialization/cleanup
1333 // without calling these functions from app.cpp or from
1334 // the user's application.
1336 class wxHtmlWinModule
: public wxModule
1338 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
1340 wxHtmlWinModule() : wxModule() {}
1341 bool OnInit() { return TRUE
; }
1342 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
1345 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
1348 // This hack forces the linker to always link in m_* files
1349 // (wxHTML doesn't work without handlers from these files)
1350 #include "wx/html/forcelnk.h"
1351 FORCE_WXHTML_MODULES()
1353 #endif // wxUSE_HTML