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 /////////////////////////////////////////////////////////////////////////////
11 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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
;
208 WX_CLEAR_LIST(wxHtmlProcessorList
, *m_Processors
);
220 void wxHtmlWindow::SetRelatedFrame(wxFrame
* frame
, const wxString
& format
)
222 m_RelatedFrame
= frame
;
223 m_TitleFormat
= format
;
228 void wxHtmlWindow::SetRelatedStatusBar(int bar
)
230 m_RelatedStatusBar
= bar
;
235 void wxHtmlWindow::SetFonts(wxString normal_face
, wxString fixed_face
, const int *sizes
)
237 wxString op
= m_OpenedPage
;
239 m_Parser
->SetFonts(normal_face
, fixed_face
, sizes
);
240 // fonts changed => contents invalid, so reload the page:
241 SetPage(wxT("<html><body></body></html>"));
242 if (!op
.IsEmpty()) LoadPage(op
);
247 bool wxHtmlWindow::SetPage(const wxString
& source
)
249 wxString
newsrc(source
);
251 wxDELETE(m_selection
);
253 // pass HTML through registered processors:
254 if (m_Processors
|| m_GlobalProcessors
)
256 wxHtmlProcessorList::compatibility_iterator nodeL
, nodeG
;
259 nodeL
= (m_Processors
) ? m_Processors
->GetFirst() : wxHtmlProcessorList::compatibility_iterator();
260 nodeG
= (m_GlobalProcessors
) ? m_GlobalProcessors
->GetFirst() : wxHtmlProcessorList::compatibility_iterator();
262 // VS: there are two lists, global and local, both of them sorted by
263 // priority. Since we have to go through _both_ lists with
264 // decreasing priority, we "merge-sort" the lists on-line by
265 // processing that one of the two heads that has higher priority
266 // in every iteration
267 while (nodeL
|| nodeG
)
269 prL
= (nodeL
) ? nodeL
->GetData()->GetPriority() : -1;
270 prG
= (nodeG
) ? nodeG
->GetData()->GetPriority() : -1;
273 if (nodeL
->GetData()->IsEnabled())
274 newsrc
= nodeL
->GetData()->Process(newsrc
);
275 nodeL
= nodeL
->GetNext();
279 if (nodeG
->GetData()->IsEnabled())
280 newsrc
= nodeG
->GetData()->Process(newsrc
);
281 nodeG
= nodeG
->GetNext();
286 // ...and run the parser on it:
287 wxClientDC
*dc
= new wxClientDC(this);
288 dc
->SetMapMode(wxMM_TEXT
);
289 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
290 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
297 m_Cell
= (wxHtmlContainerCell
*) m_Parser
->Parse(newsrc
);
299 m_Cell
->SetIndent(m_Borders
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
300 m_Cell
->SetAlignHor(wxHTML_ALIGN_CENTER
);
302 if (m_tmpCanDrawLocks
== 0)
307 bool wxHtmlWindow::AppendToPage(const wxString
& source
)
309 return SetPage(*(GetParser()->GetSource()) + source
);
312 bool wxHtmlWindow::LoadPage(const wxString
& location
)
314 wxBusyCursor busyCursor
;
318 bool needs_refresh
= FALSE
;
321 if (m_HistoryOn
&& (m_HistoryPos
!= -1))
323 // store scroll position into history item:
325 GetViewStart(&x
, &y
);
326 (*m_History
)[m_HistoryPos
].SetPos(y
);
329 if (location
[0] == wxT('#'))
332 wxString anch
= location
.Mid(1) /*1 to end*/;
334 rt_val
= ScrollToAnchor(anch
);
337 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&& location
.BeforeFirst(wxT('#')) == m_OpenedPage
)
339 wxString anch
= location
.AfterFirst(wxT('#'));
341 rt_val
= ScrollToAnchor(anch
);
344 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&&
345 (m_FS
->GetPath() + location
.BeforeFirst(wxT('#'))) == m_OpenedPage
)
347 wxString anch
= location
.AfterFirst(wxT('#'));
349 rt_val
= ScrollToAnchor(anch
);
355 needs_refresh
= TRUE
;
357 if (m_RelatedStatusBar
!= -1)
359 m_RelatedFrame
->SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
363 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location
);
365 // try to interpret 'location' as filename instead of URL:
368 wxFileName
fn(location
);
369 wxString location2
= wxFileSystem::FileNameToURL(fn
);
370 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location2
);
375 wxLogError(_("Unable to open requested HTML document: %s"), location
.c_str());
382 wxList::compatibility_iterator node
;
383 wxString src
= wxEmptyString
;
385 if (m_RelatedStatusBar
!= -1)
387 wxString msg
= _("Loading : ") + location
;
388 m_RelatedFrame
->SetStatusText(msg
, m_RelatedStatusBar
);
392 node
= m_Filters
.GetFirst();
395 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
398 src
= h
->ReadFile(*f
);
401 node
= node
->GetNext();
403 if (src
== wxEmptyString
)
405 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
406 src
= m_DefaultFilter
->ReadFile(*f
);
409 m_FS
->ChangePathTo(f
->GetLocation());
410 rt_val
= SetPage(src
);
411 m_OpenedPage
= f
->GetLocation();
412 if (f
->GetAnchor() != wxEmptyString
)
414 ScrollToAnchor(f
->GetAnchor());
419 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
->SetStatusText(_("Done"), m_RelatedStatusBar
);
423 if (m_HistoryOn
) // add this page to history there:
425 int c
= m_History
->GetCount() - (m_HistoryPos
+ 1);
427 if (m_HistoryPos
< 0 ||
428 (*m_History
)[m_HistoryPos
].GetPage() != m_OpenedPage
||
429 (*m_History
)[m_HistoryPos
].GetAnchor() != m_OpenedAnchor
)
432 for (int i
= 0; i
< c
; i
++)
433 m_History
->RemoveAt(m_HistoryPos
);
434 m_History
->Add(new wxHtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
438 if (m_OpenedPageTitle
== wxEmptyString
)
439 OnSetTitle(wxFileNameFromPath(m_OpenedPage
));
453 bool wxHtmlWindow::LoadFile(const wxFileName
& filename
)
455 wxString url
= wxFileSystem::FileNameToURL(filename
);
456 return LoadPage(url
);
460 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
462 const wxHtmlCell
*c
= m_Cell
->Find(wxHTML_COND_ISANCHOR
, &anchor
);
465 wxLogWarning(_("HTML anchor %s does not exist."), anchor
.c_str());
472 for (y
= 0; c
!= NULL
; c
= c
->GetParent()) y
+= c
->GetPosY();
473 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
474 m_OpenedAnchor
= anchor
;
480 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
485 tit
.Printf(m_TitleFormat
, title
.c_str());
486 m_RelatedFrame
->SetTitle(tit
);
488 m_OpenedPageTitle
= title
;
495 void wxHtmlWindow::CreateLayout()
497 int ClientWidth
, ClientHeight
;
501 if (m_Style
& wxHW_SCROLLBAR_NEVER
)
503 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // always off
504 GetClientSize(&ClientWidth
, &ClientHeight
);
505 m_Cell
->Layout(ClientWidth
);
509 GetClientSize(&ClientWidth
, &ClientHeight
);
510 m_Cell
->Layout(ClientWidth
);
511 if (ClientHeight
< m_Cell
->GetHeight() + GetCharHeight())
514 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
515 m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
,
516 (m_Cell
->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
517 /*cheat: top-level frag is always container*/);
519 else /* we fit into window, no need for scrollbars */
521 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
522 GetClientSize(&ClientWidth
, &ClientHeight
);
523 m_Cell
->Layout(ClientWidth
); // ...and relayout
530 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
535 wxString p_fff
, p_ffn
;
537 if (path
!= wxEmptyString
)
539 oldpath
= cfg
->GetPath();
543 m_Borders
= cfg
->Read(wxT("wxHtmlWindow/Borders"), m_Borders
);
544 p_fff
= cfg
->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
545 p_ffn
= cfg
->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
546 for (int i
= 0; i
< 7; i
++)
548 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
549 p_fontsizes
[i
] = cfg
->Read(tmp
, m_Parser
->m_FontsSizes
[i
]);
551 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
553 if (path
!= wxEmptyString
)
554 cfg
->SetPath(oldpath
);
559 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
564 if (path
!= wxEmptyString
)
566 oldpath
= cfg
->GetPath();
570 cfg
->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders
);
571 cfg
->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
572 cfg
->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
573 for (int i
= 0; i
< 7; i
++)
575 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
576 cfg
->Write(tmp
, (long) m_Parser
->m_FontsSizes
[i
]);
579 if (path
!= wxEmptyString
)
580 cfg
->SetPath(oldpath
);
585 bool wxHtmlWindow::HistoryBack()
589 if (m_HistoryPos
< 1) return FALSE
;
591 // store scroll position into history item:
593 GetViewStart(&x
, &y
);
594 (*m_History
)[m_HistoryPos
].SetPos(y
);
596 // go to previous position:
599 l
= (*m_History
)[m_HistoryPos
].GetPage();
600 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
603 if (a
== wxEmptyString
) LoadPage(l
);
604 else LoadPage(l
+ wxT("#") + a
);
607 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
612 bool wxHtmlWindow::HistoryCanBack()
614 if (m_HistoryPos
< 1) return FALSE
;
619 bool wxHtmlWindow::HistoryForward()
623 if (m_HistoryPos
== -1) return FALSE
;
624 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
626 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
629 l
= (*m_History
)[m_HistoryPos
].GetPage();
630 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
633 if (a
== wxEmptyString
) LoadPage(l
);
634 else LoadPage(l
+ wxT("#") + a
);
637 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
642 bool wxHtmlWindow::HistoryCanForward()
644 if (m_HistoryPos
== -1) return FALSE
;
645 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
650 void wxHtmlWindow::HistoryClear()
656 void wxHtmlWindow::AddProcessor(wxHtmlProcessor
*processor
)
660 m_Processors
= new wxHtmlProcessorList
;
662 wxHtmlProcessorList::compatibility_iterator node
;
664 for (node
= m_Processors
->GetFirst(); node
; node
= node
->GetNext())
666 if (processor
->GetPriority() > node
->GetData()->GetPriority())
668 m_Processors
->Insert(node
, processor
);
672 m_Processors
->Append(processor
);
675 /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor
*processor
)
677 if (!m_GlobalProcessors
)
679 m_GlobalProcessors
= new wxHtmlProcessorList
;
681 wxHtmlProcessorList::compatibility_iterator node
;
683 for (node
= m_GlobalProcessors
->GetFirst(); node
; node
= node
->GetNext())
685 if (processor
->GetPriority() > node
->GetData()->GetPriority())
687 m_GlobalProcessors
->Insert(node
, processor
);
691 m_GlobalProcessors
->Append(processor
);
696 wxList
wxHtmlWindow::m_Filters
;
697 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
698 wxHtmlProcessorList
*wxHtmlWindow::m_GlobalProcessors
= NULL
;
700 void wxHtmlWindow::CleanUpStatics()
702 wxDELETE(m_DefaultFilter
);
703 WX_CLEAR_LIST(wxList
, m_Filters
);
704 if (m_GlobalProcessors
)
705 WX_CLEAR_LIST(wxHtmlProcessorList
, *m_GlobalProcessors
);
706 wxDELETE(m_GlobalProcessors
);
711 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
713 m_Filters
.Append(filter
);
717 bool wxHtmlWindow::IsSelectionEnabled() const
720 return !(m_Style
& wxHW_NO_SELECTION
);
728 wxString
wxHtmlWindow::SelectionToText()
731 return wxEmptyString
;
735 const wxHtmlCell
*end
= m_selection
->GetToCell();
737 wxHtmlTerminalCellsInterator
i(m_selection
->GetFromCell(), end
);
740 text
<< i
->ConvertToText(m_selection
);
743 const wxHtmlCell
*prev
= *i
;
746 if ( prev
->GetParent() != i
->GetParent() )
748 text
<< i
->ConvertToText(*i
== end
? m_selection
: NULL
);
755 #endif // wxUSE_CLIPBOARD
757 void wxHtmlWindow::CopySelection(ClipboardType t
)
763 wxTheClipboard
->UsePrimarySelection(t
== Primary
);
765 // Primary selection exists only under X11, so don't do anything under
766 // the other platforms when we try to access it
768 // TODO: this should be abstracted at wxClipboard level!
771 #endif // __UNIX__/!__UNIX__
773 if ( wxTheClipboard
->Open() )
775 const wxString
txt(SelectionToText());
776 wxTheClipboard
->SetData(new wxTextDataObject(txt
));
777 wxTheClipboard
->Close();
778 wxLogTrace(_T("wxhtmlselection"),
779 _("Copied to clipboard:\"%s\""), txt
.c_str());
782 #endif // wxUSE_CLIPBOARD
786 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
788 const wxMouseEvent
*e
= link
.GetEvent();
789 if (e
== NULL
|| e
->LeftUp())
790 LoadPage(link
.GetHref());
793 void wxHtmlWindow::OnCellClicked(wxHtmlCell
*cell
,
794 wxCoord x
, wxCoord y
,
795 const wxMouseEvent
& event
)
797 wxCHECK_RET( cell
, _T("can't be called with NULL cell") );
799 cell
->OnMouseClick(this, x
, y
, event
);
802 void wxHtmlWindow::OnCellMouseHover(wxHtmlCell
* WXUNUSED(cell
),
803 wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
808 void wxHtmlWindow::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
812 void wxHtmlWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
816 if (m_tmpCanDrawLocks
> 0 || m_Cell
== NULL
) return;
819 GetViewStart(&x
, &y
);
820 wxRect rect
= GetUpdateRegion().GetBox();
821 wxSize sz
= GetSize();
825 m_backBuffer
= new wxBitmap(sz
.x
, sz
.y
);
826 dcm
.SelectObject(*m_backBuffer
);
827 dcm
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
830 dcm
.SetMapMode(wxMM_TEXT
);
831 dcm
.SetBackgroundMode(wxTRANSPARENT
);
833 wxHtmlRenderingInfo rinfo
;
834 wxDefaultHtmlRenderingStyle rstyle
;
835 rinfo
.SetSelection(m_selection
);
836 rinfo
.SetStyle(&rstyle
);
837 m_Cell
->Draw(dcm
, 0, 0,
838 y
* wxHTML_SCROLL_STEP
+ rect
.GetTop(),
839 y
* wxHTML_SCROLL_STEP
+ rect
.GetBottom(),
842 //#define DEBUG_HTML_SELECTION
843 #ifdef DEBUG_HTML_SELECTION
846 wxGetMousePosition(&xc
, &yc
);
847 ScreenToClient(&xc
, &yc
);
848 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
849 wxHtmlCell
*at
= m_Cell
->FindCellByPos(x
, y
);
851 m_Cell
->FindCellByPos(x
, y
, wxHTML_FIND_NEAREST_BEFORE
);
853 m_Cell
->FindCellByPos(x
, y
, wxHTML_FIND_NEAREST_AFTER
);
855 dcm
.SetBrush(*wxTRANSPARENT_BRUSH
);
856 dcm
.SetPen(*wxBLACK_PEN
);
858 dcm
.DrawRectangle(at
->GetAbsPos(),
859 wxSize(at
->GetWidth(),at
->GetHeight()));
860 dcm
.SetPen(*wxGREEN_PEN
);
862 dcm
.DrawRectangle(before
->GetAbsPos().x
+1, before
->GetAbsPos().y
+1,
863 before
->GetWidth()-2,before
->GetHeight()-2);
864 dcm
.SetPen(*wxRED_PEN
);
866 dcm
.DrawRectangle(after
->GetAbsPos().x
+2, after
->GetAbsPos().y
+2,
867 after
->GetWidth()-4,after
->GetHeight()-4);
871 dcm
.SetDeviceOrigin(0,0);
872 dc
.Blit(0, rect
.GetTop(),
873 sz
.x
, rect
.GetBottom() - rect
.GetTop() + 1,
881 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
883 wxDELETE(m_backBuffer
);
885 wxScrolledWindow::OnSize(event
);
888 // Recompute selection if necessary:
891 m_selection
->Set(m_selection
->GetFromCell(),
892 m_selection
->GetToCell());
893 m_selection
->ClearPrivPos();
900 void wxHtmlWindow::OnMouseMove(wxMouseEvent
& WXUNUSED(event
))
902 m_tmpMouseMoved
= true;
905 void wxHtmlWindow::OnMouseDown(wxMouseEvent
& event
)
908 if ( event
.LeftDown() && IsSelectionEnabled() )
910 const long TRIPLECLICK_LEN
= 200; // 0.2 sec after doubleclick
911 if ( wxGetLocalTimeMillis() - m_lastDoubleClick
<= TRIPLECLICK_LEN
)
913 SelectLine(CalcUnscrolledPosition(event
.GetPosition()));
919 m_makingSelection
= true;
923 wxDELETE(m_selection
);
926 m_tmpSelFromPos
= CalcUnscrolledPosition(event
.GetPosition());
927 m_tmpSelFromCell
= NULL
;
932 #endif // wxUSE_CLIPBOARD
935 void wxHtmlWindow::OnMouseUp(wxMouseEvent
& event
)
938 if ( m_makingSelection
)
941 m_makingSelection
= false;
943 // did the user move the mouse far enough from starting point?
946 CopySelection(Primary
);
948 // we don't want mouse up event that ended selecting to be
949 // handled as mouse click and e.g. follow hyperlink:
953 #endif // wxUSE_CLIPBOARD
958 wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
959 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
961 // check is needed because FindCellByPos returns terminal cell and
962 // containers may have empty borders -- in this case NULL will be
965 OnCellClicked(cell
, pos
.x
, pos
.y
, event
);
971 void wxHtmlWindow::OnInternalIdle()
973 wxWindow::OnInternalIdle();
975 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
))
977 #ifdef DEBUG_HTML_SELECTION
981 wxGetMousePosition(&xc
, &yc
);
982 ScreenToClient(&xc
, &yc
);
983 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
985 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(x
, y
);
987 // handle selection update:
988 if ( m_makingSelection
)
990 if ( !m_tmpSelFromCell
)
991 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
992 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
);
994 // NB: a trick - we adjust selFromPos to be upper left or bottom
995 // right corner of the first cell of the selection depending
996 // on whether the mouse is moving to the right or to the left.
997 // This gives us more "natural" behaviour when selecting
998 // a line (specifically, first cell of the next line is not
999 // included if you drag selection from left to right over
1002 if ( !m_tmpSelFromCell
)
1004 dirFromPos
= m_tmpSelFromPos
;
1008 dirFromPos
= m_tmpSelFromCell
->GetAbsPos();
1009 if ( x
< m_tmpSelFromPos
.x
)
1011 dirFromPos
.x
+= m_tmpSelFromCell
->GetWidth();
1012 dirFromPos
.y
+= m_tmpSelFromCell
->GetHeight();
1015 bool goingDown
= dirFromPos
.y
< y
||
1016 (dirFromPos
.y
== y
&& dirFromPos
.x
< x
);
1018 // determine selection span:
1019 if ( /*still*/ !m_tmpSelFromCell
)
1023 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1024 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1025 wxHTML_FIND_NEAREST_AFTER
);
1026 if (!m_tmpSelFromCell
)
1027 m_tmpSelFromCell
= m_Cell
->GetFirstTerminal();
1031 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1032 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1033 wxHTML_FIND_NEAREST_BEFORE
);
1034 if (!m_tmpSelFromCell
)
1035 m_tmpSelFromCell
= m_Cell
->GetLastTerminal();
1039 wxHtmlCell
*selcell
= cell
;
1044 selcell
= m_Cell
->FindCellByPos(x
, y
,
1045 wxHTML_FIND_NEAREST_BEFORE
);
1047 selcell
= m_Cell
->GetLastTerminal();
1051 selcell
= m_Cell
->FindCellByPos(x
, y
,
1052 wxHTML_FIND_NEAREST_AFTER
);
1054 selcell
= m_Cell
->GetFirstTerminal();
1058 // NB: it may *rarely* happen that the code above didn't find one
1059 // of the cells, e.g. if wxHtmlWindow doesn't contain any
1061 if ( selcell
&& m_tmpSelFromCell
)
1065 // start selecting only if mouse movement was big enough
1066 // (otherwise it was meant as mouse click, not selection):
1067 const int PRECISION
= 2;
1068 wxPoint diff
= m_tmpSelFromPos
- wxPoint(x
,y
);
1069 if (abs(diff
.x
) > PRECISION
|| abs(diff
.y
) > PRECISION
)
1071 m_selection
= new wxHtmlSelection();
1076 if ( m_tmpSelFromCell
->IsBefore(selcell
) )
1078 m_selection
->Set(m_tmpSelFromPos
, m_tmpSelFromCell
,
1079 wxPoint(x
,y
), selcell
); }
1082 m_selection
->Set(wxPoint(x
,y
), selcell
,
1083 m_tmpSelFromPos
, m_tmpSelFromCell
);
1085 m_selection
->ClearPrivPos();
1091 // handle cursor and status bar text changes:
1092 if ( cell
!= m_tmpLastCell
)
1094 wxHtmlLinkInfo
*lnk
= cell
? cell
->GetLink(x
, y
) : NULL
;
1097 cur
= cell
->GetCursor();
1099 cur
= *wxSTANDARD_CURSOR
;
1102 if (lnk
!= m_tmpLastLink
)
1106 if (m_RelatedStatusBar
!= -1)
1107 m_RelatedFrame
->SetStatusText(wxEmptyString
,
1108 m_RelatedStatusBar
);
1112 if (m_RelatedStatusBar
!= -1)
1113 m_RelatedFrame
->SetStatusText(lnk
->GetHref(),
1114 m_RelatedStatusBar
);
1116 m_tmpLastLink
= lnk
;
1119 m_tmpLastCell
= cell
;
1121 else // mouse moved but stayed in the same cell
1124 OnCellMouseHover(cell
, x
, y
);
1127 m_tmpMouseMoved
= FALSE
;
1132 void wxHtmlWindow::StopAutoScrolling()
1134 if ( m_timerAutoScroll
)
1136 wxDELETE(m_timerAutoScroll
);
1140 void wxHtmlWindow::OnMouseEnter(wxMouseEvent
& event
)
1142 StopAutoScrolling();
1146 void wxHtmlWindow::OnMouseLeave(wxMouseEvent
& event
)
1148 // don't prevent the usual processing of the event from taking place
1151 // when a captured mouse leave a scrolled window we start generate
1152 // scrolling events to allow, for example, extending selection beyond the
1153 // visible area in some controls
1154 if ( wxWindow::GetCapture() == this )
1156 // where is the mouse leaving?
1158 wxPoint pt
= event
.GetPosition();
1161 orient
= wxHORIZONTAL
;
1164 else if ( pt
.y
< 0 )
1166 orient
= wxVERTICAL
;
1169 else // we're lower or to the right of the window
1171 wxSize size
= GetClientSize();
1172 if ( pt
.x
> size
.x
)
1174 orient
= wxHORIZONTAL
;
1175 pos
= GetVirtualSize().x
/ wxHTML_SCROLL_STEP
;
1177 else if ( pt
.y
> size
.y
)
1179 orient
= wxVERTICAL
;
1180 pos
= GetVirtualSize().y
/ wxHTML_SCROLL_STEP
;
1182 else // this should be impossible
1184 // but seems to happen sometimes under wxMSW - maybe it's a bug
1185 // there but for now just ignore it
1187 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1193 // only start the auto scroll timer if the window can be scrolled in
1195 if ( !HasScrollbar(orient
) )
1198 delete m_timerAutoScroll
;
1199 m_timerAutoScroll
= new wxHtmlWinAutoScrollTimer
1202 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1203 : wxEVT_SCROLLWIN_LINEDOWN
,
1207 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1211 void wxHtmlWindow::OnKeyUp(wxKeyEvent
& event
)
1213 if ( IsSelectionEnabled() &&
1214 event
.GetKeyCode() == 'C' && event
.ControlDown() )
1221 void wxHtmlWindow::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1227 void wxHtmlWindow::OnDoubleClick(wxMouseEvent
& event
)
1229 // select word under cursor:
1230 if ( IsSelectionEnabled() )
1232 SelectWord(CalcUnscrolledPosition(event
.GetPosition()));
1234 CopySelection(Primary
);
1236 m_lastDoubleClick
= wxGetLocalTimeMillis();
1242 void wxHtmlWindow::SelectWord(const wxPoint
& pos
)
1244 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1248 m_selection
= new wxHtmlSelection();
1249 m_selection
->Set(cell
, cell
);
1250 RefreshRect(wxRect(CalcScrolledPosition(cell
->GetAbsPos()),
1251 wxSize(cell
->GetWidth(), cell
->GetHeight())));
1255 void wxHtmlWindow::SelectLine(const wxPoint
& pos
)
1257 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1260 // We use following heuristic to find a "line": let the line be all
1261 // cells in same container as the cell under mouse cursor that are
1262 // neither completely above nor completely bellow the clicked cell
1263 // (i.e. are likely to be words positioned on same line of text).
1265 int y1
= cell
->GetAbsPos().y
;
1266 int y2
= y1
+ cell
->GetHeight();
1268 const wxHtmlCell
*c
;
1269 const wxHtmlCell
*before
= NULL
;
1270 const wxHtmlCell
*after
= NULL
;
1272 // find last cell of line:
1273 for ( c
= cell
->GetNext(); c
; c
= c
->GetNext())
1275 y
= c
->GetAbsPos().y
;
1276 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1284 // find first cell of line:
1285 for ( c
= cell
->GetParent()->GetFirstChild();
1286 c
&& c
!= cell
; c
= c
->GetNext())
1288 y
= c
->GetAbsPos().y
;
1289 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1301 m_selection
= new wxHtmlSelection();
1302 m_selection
->Set(before
, after
);
1307 #endif // wxUSE_CLIPBOARD
1311 IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor
,wxObject
)
1313 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
1315 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
1316 EVT_SIZE(wxHtmlWindow::OnSize
)
1317 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseDown
)
1318 EVT_LEFT_UP(wxHtmlWindow::OnMouseUp
)
1319 EVT_RIGHT_UP(wxHtmlWindow::OnMouseUp
)
1320 EVT_MOTION(wxHtmlWindow::OnMouseMove
)
1321 EVT_ERASE_BACKGROUND(wxHtmlWindow::OnEraseBackground
)
1322 EVT_PAINT(wxHtmlWindow::OnPaint
)
1324 EVT_LEFT_DCLICK(wxHtmlWindow::OnDoubleClick
)
1325 EVT_ENTER_WINDOW(wxHtmlWindow::OnMouseEnter
)
1326 EVT_LEAVE_WINDOW(wxHtmlWindow::OnMouseLeave
)
1327 EVT_KEY_UP(wxHtmlWindow::OnKeyUp
)
1328 EVT_MENU(wxID_COPY
, wxHtmlWindow::OnCopy
)
1329 #endif // wxUSE_CLIPBOARD
1336 // A module to allow initialization/cleanup
1337 // without calling these functions from app.cpp or from
1338 // the user's application.
1340 class wxHtmlWinModule
: public wxModule
1342 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
1344 wxHtmlWinModule() : wxModule() {}
1345 bool OnInit() { return TRUE
; }
1346 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
1349 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
1352 // This hack forces the linker to always link in m_* files
1353 // (wxHTML doesn't work without handlers from these files)
1354 #include "wx/html/forcelnk.h"
1355 FORCE_WXHTML_MODULES()
1357 #endif // wxUSE_HTML