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"
39 #include "wx/settings.h"
41 #include "wx/arrimpl.cpp"
42 #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();
162 m_RelatedStatusBar
= -1;
163 #endif // wxUSE_STATUSBAR
164 m_RelatedFrame
= NULL
;
165 m_TitleFormat
= wxT("%s");
166 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
168 m_Parser
= new wxHtmlWinParser(this);
169 m_Parser
->SetFS(m_FS
);
172 m_History
= new wxHtmlHistoryArray
;
177 m_makingSelection
= false;
179 m_timerAutoScroll
= NULL
;
180 m_lastDoubleClick
= 0;
181 #endif // wxUSE_CLIPBOARD
185 bool wxHtmlWindow::Create(wxWindow
*parent
, wxWindowID id
,
186 const wxPoint
& pos
, const wxSize
& size
,
187 long style
, const wxString
& name
)
189 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
190 style
| wxVSCROLL
| wxHSCROLL
,
195 SetPage(wxT("<html><body></body></html>"));
200 wxHtmlWindow::~wxHtmlWindow()
204 #endif // wxUSE_CLIPBOARD
213 WX_CLEAR_LIST(wxHtmlProcessorList
, *m_Processors
);
225 void wxHtmlWindow::SetRelatedFrame(wxFrame
* frame
, const wxString
& format
)
227 m_RelatedFrame
= frame
;
228 m_TitleFormat
= format
;
234 void wxHtmlWindow::SetRelatedStatusBar(int bar
)
236 m_RelatedStatusBar
= bar
;
238 #endif // wxUSE_STATUSBAR
242 void wxHtmlWindow::SetFonts(wxString normal_face
, wxString fixed_face
, const int *sizes
)
244 wxString op
= m_OpenedPage
;
246 m_Parser
->SetFonts(normal_face
, fixed_face
, sizes
);
247 // fonts changed => contents invalid, so reload the page:
248 SetPage(wxT("<html><body></body></html>"));
253 void wxHtmlWindow::SetStandardFonts(int size
,
254 const wxString
& normal_face
,
255 const wxString
& fixed_face
)
257 wxString op
= m_OpenedPage
;
259 m_Parser
->SetStandardFonts(size
, normal_face
, fixed_face
);
260 // fonts changed => contents invalid, so reload the page:
261 SetPage(wxT("<html><body></body></html>"));
267 bool wxHtmlWindow::SetPage(const wxString
& source
)
269 wxString
newsrc(source
);
271 wxDELETE(m_selection
);
273 // pass HTML through registered processors:
274 if (m_Processors
|| m_GlobalProcessors
)
276 wxHtmlProcessorList::compatibility_iterator nodeL
, nodeG
;
279 nodeL
= (m_Processors
) ? m_Processors
->GetFirst() : wxHtmlProcessorList::compatibility_iterator();
280 nodeG
= (m_GlobalProcessors
) ? m_GlobalProcessors
->GetFirst() : wxHtmlProcessorList::compatibility_iterator();
282 // VS: there are two lists, global and local, both of them sorted by
283 // priority. Since we have to go through _both_ lists with
284 // decreasing priority, we "merge-sort" the lists on-line by
285 // processing that one of the two heads that has higher priority
286 // in every iteration
287 while (nodeL
|| nodeG
)
289 prL
= (nodeL
) ? nodeL
->GetData()->GetPriority() : -1;
290 prG
= (nodeG
) ? nodeG
->GetData()->GetPriority() : -1;
293 if (nodeL
->GetData()->IsEnabled())
294 newsrc
= nodeL
->GetData()->Process(newsrc
);
295 nodeL
= nodeL
->GetNext();
299 if (nodeG
->GetData()->IsEnabled())
300 newsrc
= nodeG
->GetData()->Process(newsrc
);
301 nodeG
= nodeG
->GetNext();
306 // ...and run the parser on it:
307 wxClientDC
*dc
= new wxClientDC(this);
308 dc
->SetMapMode(wxMM_TEXT
);
309 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
310 SetBackgroundImage(wxNullBitmap
);
311 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
318 m_Cell
= (wxHtmlContainerCell
*) m_Parser
->Parse(newsrc
);
320 m_Cell
->SetIndent(m_Borders
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
321 m_Cell
->SetAlignHor(wxHTML_ALIGN_CENTER
);
323 if (m_tmpCanDrawLocks
== 0)
328 bool wxHtmlWindow::AppendToPage(const wxString
& source
)
330 return SetPage(*(GetParser()->GetSource()) + source
);
333 bool wxHtmlWindow::LoadPage(const wxString
& location
)
335 wxBusyCursor busyCursor
;
339 bool needs_refresh
= false;
342 if (m_HistoryOn
&& (m_HistoryPos
!= -1))
344 // store scroll position into history item:
346 GetViewStart(&x
, &y
);
347 (*m_History
)[m_HistoryPos
].SetPos(y
);
350 if (location
[0] == wxT('#'))
353 wxString anch
= location
.Mid(1) /*1 to end*/;
355 rt_val
= ScrollToAnchor(anch
);
358 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&& location
.BeforeFirst(wxT('#')) == m_OpenedPage
)
360 wxString anch
= location
.AfterFirst(wxT('#'));
362 rt_val
= ScrollToAnchor(anch
);
365 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&&
366 (m_FS
->GetPath() + location
.BeforeFirst(wxT('#'))) == m_OpenedPage
)
368 wxString anch
= location
.AfterFirst(wxT('#'));
370 rt_val
= ScrollToAnchor(anch
);
376 needs_refresh
= true;
379 if (m_RelatedStatusBar
!= -1)
381 m_RelatedFrame
->SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
384 #endif // wxUSE_STATUSBAR
386 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location
);
388 // try to interpret 'location' as filename instead of URL:
391 wxFileName
fn(location
);
392 wxString location2
= wxFileSystem::FileNameToURL(fn
);
393 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location2
);
398 wxLogError(_("Unable to open requested HTML document: %s"), location
.c_str());
405 wxList::compatibility_iterator node
;
406 wxString src
= wxEmptyString
;
409 if (m_RelatedStatusBar
!= -1)
411 wxString msg
= _("Loading : ") + location
;
412 m_RelatedFrame
->SetStatusText(msg
, m_RelatedStatusBar
);
415 #endif // wxUSE_STATUSBAR
417 node
= m_Filters
.GetFirst();
420 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
423 src
= h
->ReadFile(*f
);
426 node
= node
->GetNext();
428 if (src
== wxEmptyString
)
430 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
431 src
= m_DefaultFilter
->ReadFile(*f
);
434 m_FS
->ChangePathTo(f
->GetLocation());
435 rt_val
= SetPage(src
);
436 m_OpenedPage
= f
->GetLocation();
437 if (f
->GetAnchor() != wxEmptyString
)
439 ScrollToAnchor(f
->GetAnchor());
445 if (m_RelatedStatusBar
!= -1)
446 m_RelatedFrame
->SetStatusText(_("Done"), m_RelatedStatusBar
);
447 #endif // wxUSE_STATUSBAR
451 if (m_HistoryOn
) // add this page to history there:
453 int c
= m_History
->GetCount() - (m_HistoryPos
+ 1);
455 if (m_HistoryPos
< 0 ||
456 (*m_History
)[m_HistoryPos
].GetPage() != m_OpenedPage
||
457 (*m_History
)[m_HistoryPos
].GetAnchor() != m_OpenedAnchor
)
460 for (int i
= 0; i
< c
; i
++)
461 m_History
->RemoveAt(m_HistoryPos
);
462 m_History
->Add(new wxHtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
466 if (m_OpenedPageTitle
== wxEmptyString
)
467 OnSetTitle(wxFileNameFromPath(m_OpenedPage
));
481 bool wxHtmlWindow::LoadFile(const wxFileName
& filename
)
483 wxString url
= wxFileSystem::FileNameToURL(filename
);
484 return LoadPage(url
);
488 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
490 const wxHtmlCell
*c
= m_Cell
->Find(wxHTML_COND_ISANCHOR
, &anchor
);
493 wxLogWarning(_("HTML anchor %s does not exist."), anchor
.c_str());
500 for (y
= 0; c
!= NULL
; c
= c
->GetParent()) y
+= c
->GetPosY();
501 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
502 m_OpenedAnchor
= anchor
;
508 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
513 tit
.Printf(m_TitleFormat
, title
.c_str());
514 m_RelatedFrame
->SetTitle(tit
);
516 m_OpenedPageTitle
= title
;
523 void wxHtmlWindow::CreateLayout()
525 int ClientWidth
, ClientHeight
;
529 if (m_Style
& wxHW_SCROLLBAR_NEVER
)
531 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // always off
532 GetClientSize(&ClientWidth
, &ClientHeight
);
533 m_Cell
->Layout(ClientWidth
);
537 GetClientSize(&ClientWidth
, &ClientHeight
);
538 m_Cell
->Layout(ClientWidth
);
539 if (ClientHeight
< m_Cell
->GetHeight() + GetCharHeight())
542 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
543 m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
,
544 (m_Cell
->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
545 /*cheat: top-level frag is always container*/);
547 else /* we fit into window, no need for scrollbars */
549 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
550 GetClientSize(&ClientWidth
, &ClientHeight
);
551 m_Cell
->Layout(ClientWidth
); // ...and relayout
558 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
563 wxString p_fff
, p_ffn
;
565 if (path
!= wxEmptyString
)
567 oldpath
= cfg
->GetPath();
571 m_Borders
= cfg
->Read(wxT("wxHtmlWindow/Borders"), m_Borders
);
572 p_fff
= cfg
->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
573 p_ffn
= cfg
->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
574 for (int i
= 0; i
< 7; i
++)
576 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
577 p_fontsizes
[i
] = cfg
->Read(tmp
, m_Parser
->m_FontsSizes
[i
]);
579 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
581 if (path
!= wxEmptyString
)
582 cfg
->SetPath(oldpath
);
587 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
592 if (path
!= wxEmptyString
)
594 oldpath
= cfg
->GetPath();
598 cfg
->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders
);
599 cfg
->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
600 cfg
->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
601 for (int i
= 0; i
< 7; i
++)
603 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
604 cfg
->Write(tmp
, (long) m_Parser
->m_FontsSizes
[i
]);
607 if (path
!= wxEmptyString
)
608 cfg
->SetPath(oldpath
);
613 bool wxHtmlWindow::HistoryBack()
617 if (m_HistoryPos
< 1) return false;
619 // store scroll position into history item:
621 GetViewStart(&x
, &y
);
622 (*m_History
)[m_HistoryPos
].SetPos(y
);
624 // go to previous position:
627 l
= (*m_History
)[m_HistoryPos
].GetPage();
628 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
631 if (a
== wxEmptyString
) LoadPage(l
);
632 else LoadPage(l
+ wxT("#") + a
);
635 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
640 bool wxHtmlWindow::HistoryCanBack()
642 if (m_HistoryPos
< 1) return false;
647 bool wxHtmlWindow::HistoryForward()
651 if (m_HistoryPos
== -1) return false;
652 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return false;
654 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
657 l
= (*m_History
)[m_HistoryPos
].GetPage();
658 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
661 if (a
== wxEmptyString
) LoadPage(l
);
662 else LoadPage(l
+ wxT("#") + a
);
665 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
670 bool wxHtmlWindow::HistoryCanForward()
672 if (m_HistoryPos
== -1) return false;
673 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return false;
678 void wxHtmlWindow::HistoryClear()
684 void wxHtmlWindow::AddProcessor(wxHtmlProcessor
*processor
)
688 m_Processors
= new wxHtmlProcessorList
;
690 wxHtmlProcessorList::compatibility_iterator node
;
692 for (node
= m_Processors
->GetFirst(); node
; node
= node
->GetNext())
694 if (processor
->GetPriority() > node
->GetData()->GetPriority())
696 m_Processors
->Insert(node
, processor
);
700 m_Processors
->Append(processor
);
703 /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor
*processor
)
705 if (!m_GlobalProcessors
)
707 m_GlobalProcessors
= new wxHtmlProcessorList
;
709 wxHtmlProcessorList::compatibility_iterator node
;
711 for (node
= m_GlobalProcessors
->GetFirst(); node
; node
= node
->GetNext())
713 if (processor
->GetPriority() > node
->GetData()->GetPriority())
715 m_GlobalProcessors
->Insert(node
, processor
);
719 m_GlobalProcessors
->Append(processor
);
724 wxList
wxHtmlWindow::m_Filters
;
725 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
726 wxHtmlProcessorList
*wxHtmlWindow::m_GlobalProcessors
= NULL
;
728 void wxHtmlWindow::CleanUpStatics()
730 wxDELETE(m_DefaultFilter
);
731 WX_CLEAR_LIST(wxList
, m_Filters
);
732 if (m_GlobalProcessors
)
733 WX_CLEAR_LIST(wxHtmlProcessorList
, *m_GlobalProcessors
);
734 wxDELETE(m_GlobalProcessors
);
739 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
741 m_Filters
.Append(filter
);
745 bool wxHtmlWindow::IsSelectionEnabled() const
748 return !(m_Style
& wxHW_NO_SELECTION
);
756 wxString
wxHtmlWindow::DoSelectionToText(wxHtmlSelection
*sel
)
759 return wxEmptyString
;
763 const wxHtmlCell
*end
= sel
->GetToCell();
765 wxHtmlTerminalCellsInterator
i(sel
->GetFromCell(), end
);
768 text
<< i
->ConvertToText(sel
);
771 const wxHtmlCell
*prev
= *i
;
774 if ( prev
->GetParent() != i
->GetParent() )
776 text
<< i
->ConvertToText(*i
== end
? sel
: NULL
);
783 wxString
wxHtmlWindow::ToText()
788 sel
.Set(m_Cell
->GetFirstTerminal(), m_Cell
->GetLastTerminal());
789 return DoSelectionToText(&sel
);
792 return wxEmptyString
;
795 #endif // wxUSE_CLIPBOARD
797 bool wxHtmlWindow::CopySelection(ClipboardType t
)
803 wxTheClipboard
->UsePrimarySelection(t
== Primary
);
805 // Primary selection exists only under X11, so don't do anything under
806 // the other platforms when we try to access it
808 // TODO: this should be abstracted at wxClipboard level!
811 #endif // __UNIX__/!__UNIX__
813 if ( wxTheClipboard
->Open() )
815 const wxString
txt(SelectionToText());
816 wxTheClipboard
->SetData(new wxTextDataObject(txt
));
817 wxTheClipboard
->Close();
818 wxLogTrace(_T("wxhtmlselection"),
819 _("Copied to clipboard:\"%s\""), txt
.c_str());
824 #endif // wxUSE_CLIPBOARD
830 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
832 const wxMouseEvent
*e
= link
.GetEvent();
833 if (e
== NULL
|| e
->LeftUp())
834 LoadPage(link
.GetHref());
837 void wxHtmlWindow::OnCellClicked(wxHtmlCell
*cell
,
838 wxCoord x
, wxCoord y
,
839 const wxMouseEvent
& event
)
841 wxCHECK_RET( cell
, _T("can't be called with NULL cell") );
843 cell
->OnMouseClick(this, x
, y
, event
);
846 void wxHtmlWindow::OnCellMouseHover(wxHtmlCell
* WXUNUSED(cell
),
847 wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
852 void wxHtmlWindow::OnEraseBackground(wxEraseEvent
& event
)
856 // we used to do nothing at all here but we do have to erase background
857 // now that we reuse it (instead of overwriting it) in OnPaint() below
863 wxDC
& dc
= *event
.GetDC();
865 // if the image is not fully opaque, we have to erase the background before
866 // drawing it, however avoid doing it for opaque images as this would just
867 // result in extra flicker without any other effect as background is
868 // completely covered anyhow
869 if ( m_bmpBg
.GetMask() )
871 dc
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
875 const wxSize
sizeWin(GetClientSize());
876 const wxSize
sizeBmp(m_bmpBg
.GetWidth(), m_bmpBg
.GetHeight());
877 for ( wxCoord x
= 0; x
< sizeWin
.x
; x
+= sizeBmp
.x
)
879 for ( wxCoord y
= 0; y
< sizeWin
.y
; y
+= sizeBmp
.y
)
881 dc
.DrawBitmap(m_bmpBg
, x
, y
, true /* use mask */);
886 void wxHtmlWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
890 if (m_tmpCanDrawLocks
> 0 || m_Cell
== NULL
) return;
893 GetViewStart(&x
, &y
);
894 wxRect rect
= GetUpdateRegion().GetBox();
895 wxSize sz
= GetSize();
899 m_backBuffer
= new wxBitmap(sz
.x
, sz
.y
);
900 dcm
.SelectObject(*m_backBuffer
);
902 // preserve the existing background, otherwise we'd erase anything the user
903 // code had drawn in its EVT_ERASE_BACKGROUND handler when we do the Blit
905 dcm
.Blit(0, rect
.GetTop(),
906 sz
.x
, rect
.GetBottom() - rect
.GetTop() + 1,
911 dcm
.SetMapMode(wxMM_TEXT
);
912 dcm
.SetBackgroundMode(wxTRANSPARENT
);
914 wxHtmlRenderingInfo rinfo
;
915 wxDefaultHtmlRenderingStyle rstyle
;
916 rinfo
.SetSelection(m_selection
);
917 rinfo
.SetStyle(&rstyle
);
918 m_Cell
->Draw(dcm
, 0, 0,
919 y
* wxHTML_SCROLL_STEP
+ rect
.GetTop(),
920 y
* wxHTML_SCROLL_STEP
+ rect
.GetBottom(),
923 //#define DEBUG_HTML_SELECTION
924 #ifdef DEBUG_HTML_SELECTION
927 wxGetMousePosition(&xc
, &yc
);
928 ScreenToClient(&xc
, &yc
);
929 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
930 wxHtmlCell
*at
= m_Cell
->FindCellByPos(x
, y
);
932 m_Cell
->FindCellByPos(x
, y
, wxHTML_FIND_NEAREST_BEFORE
);
934 m_Cell
->FindCellByPos(x
, y
, wxHTML_FIND_NEAREST_AFTER
);
936 dcm
.SetBrush(*wxTRANSPARENT_BRUSH
);
937 dcm
.SetPen(*wxBLACK_PEN
);
939 dcm
.DrawRectangle(at
->GetAbsPos(),
940 wxSize(at
->GetWidth(),at
->GetHeight()));
941 dcm
.SetPen(*wxGREEN_PEN
);
943 dcm
.DrawRectangle(before
->GetAbsPos().x
+1, before
->GetAbsPos().y
+1,
944 before
->GetWidth()-2,before
->GetHeight()-2);
945 dcm
.SetPen(*wxRED_PEN
);
947 dcm
.DrawRectangle(after
->GetAbsPos().x
+2, after
->GetAbsPos().y
+2,
948 after
->GetWidth()-4,after
->GetHeight()-4);
952 dcm
.SetDeviceOrigin(0,0);
953 dc
.Blit(0, rect
.GetTop(),
954 sz
.x
, rect
.GetBottom() - rect
.GetTop() + 1,
962 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
964 wxDELETE(m_backBuffer
);
966 wxScrolledWindow::OnSize(event
);
969 // Recompute selection if necessary:
972 m_selection
->Set(m_selection
->GetFromCell(),
973 m_selection
->GetToCell());
974 m_selection
->ClearPrivPos();
981 void wxHtmlWindow::OnMouseMove(wxMouseEvent
& WXUNUSED(event
))
983 m_tmpMouseMoved
= true;
986 void wxHtmlWindow::OnMouseDown(wxMouseEvent
& event
)
989 if ( event
.LeftDown() && IsSelectionEnabled() )
991 const long TRIPLECLICK_LEN
= 200; // 0.2 sec after doubleclick
992 if ( wxGetLocalTimeMillis() - m_lastDoubleClick
<= TRIPLECLICK_LEN
)
994 SelectLine(CalcUnscrolledPosition(event
.GetPosition()));
996 (void) CopySelection();
1000 m_makingSelection
= true;
1004 wxDELETE(m_selection
);
1007 m_tmpSelFromPos
= CalcUnscrolledPosition(event
.GetPosition());
1008 m_tmpSelFromCell
= NULL
;
1013 #endif // wxUSE_CLIPBOARD
1016 void wxHtmlWindow::OnMouseUp(wxMouseEvent
& event
)
1019 if ( m_makingSelection
)
1022 m_makingSelection
= false;
1024 // did the user move the mouse far enough from starting point?
1025 if ( CopySelection(Primary
) )
1027 // we don't want mouse up event that ended selecting to be
1028 // handled as mouse click and e.g. follow hyperlink:
1032 #endif // wxUSE_CLIPBOARD
1037 wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
1038 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1040 // check is needed because FindCellByPos returns terminal cell and
1041 // containers may have empty borders -- in this case NULL will be
1044 OnCellClicked(cell
, pos
.x
, pos
.y
, event
);
1050 void wxHtmlWindow::OnInternalIdle()
1052 wxWindow::OnInternalIdle();
1054 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
))
1056 #ifdef DEBUG_HTML_SELECTION
1060 wxGetMousePosition(&xc
, &yc
);
1061 ScreenToClient(&xc
, &yc
);
1062 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
1064 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(x
, y
);
1066 // handle selection update:
1067 if ( m_makingSelection
)
1069 if ( !m_tmpSelFromCell
)
1070 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1071 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
);
1073 // NB: a trick - we adjust selFromPos to be upper left or bottom
1074 // right corner of the first cell of the selection depending
1075 // on whether the mouse is moving to the right or to the left.
1076 // This gives us more "natural" behaviour when selecting
1077 // a line (specifically, first cell of the next line is not
1078 // included if you drag selection from left to right over
1081 if ( !m_tmpSelFromCell
)
1083 dirFromPos
= m_tmpSelFromPos
;
1087 dirFromPos
= m_tmpSelFromCell
->GetAbsPos();
1088 if ( x
< m_tmpSelFromPos
.x
)
1090 dirFromPos
.x
+= m_tmpSelFromCell
->GetWidth();
1091 dirFromPos
.y
+= m_tmpSelFromCell
->GetHeight();
1094 bool goingDown
= dirFromPos
.y
< y
||
1095 (dirFromPos
.y
== y
&& dirFromPos
.x
< x
);
1097 // determine selection span:
1098 if ( /*still*/ !m_tmpSelFromCell
)
1102 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1103 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1104 wxHTML_FIND_NEAREST_AFTER
);
1105 if (!m_tmpSelFromCell
)
1106 m_tmpSelFromCell
= m_Cell
->GetFirstTerminal();
1110 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1111 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1112 wxHTML_FIND_NEAREST_BEFORE
);
1113 if (!m_tmpSelFromCell
)
1114 m_tmpSelFromCell
= m_Cell
->GetLastTerminal();
1118 wxHtmlCell
*selcell
= cell
;
1123 selcell
= m_Cell
->FindCellByPos(x
, y
,
1124 wxHTML_FIND_NEAREST_BEFORE
);
1126 selcell
= m_Cell
->GetLastTerminal();
1130 selcell
= m_Cell
->FindCellByPos(x
, y
,
1131 wxHTML_FIND_NEAREST_AFTER
);
1133 selcell
= m_Cell
->GetFirstTerminal();
1137 // NB: it may *rarely* happen that the code above didn't find one
1138 // of the cells, e.g. if wxHtmlWindow doesn't contain any
1140 if ( selcell
&& m_tmpSelFromCell
)
1144 // start selecting only if mouse movement was big enough
1145 // (otherwise it was meant as mouse click, not selection):
1146 const int PRECISION
= 2;
1147 wxPoint diff
= m_tmpSelFromPos
- wxPoint(x
,y
);
1148 if (abs(diff
.x
) > PRECISION
|| abs(diff
.y
) > PRECISION
)
1150 m_selection
= new wxHtmlSelection();
1155 if ( m_tmpSelFromCell
->IsBefore(selcell
) )
1157 m_selection
->Set(m_tmpSelFromPos
, m_tmpSelFromCell
,
1158 wxPoint(x
,y
), selcell
); }
1161 m_selection
->Set(wxPoint(x
,y
), selcell
,
1162 m_tmpSelFromPos
, m_tmpSelFromCell
);
1164 m_selection
->ClearPrivPos();
1170 // handle cursor and status bar text changes:
1171 if ( cell
!= m_tmpLastCell
)
1173 wxHtmlLinkInfo
*lnk
= cell
? cell
->GetLink(x
, y
) : NULL
;
1176 cur
= cell
->GetCursor();
1178 cur
= *wxSTANDARD_CURSOR
;
1181 if (lnk
!= m_tmpLastLink
)
1186 if (m_RelatedStatusBar
!= -1)
1187 m_RelatedFrame
->SetStatusText(wxEmptyString
,
1188 m_RelatedStatusBar
);
1192 if (m_RelatedStatusBar
!= -1)
1193 m_RelatedFrame
->SetStatusText(lnk
->GetHref(),
1194 m_RelatedStatusBar
);
1196 #endif // wxUSE_STATUSBAR
1197 m_tmpLastLink
= lnk
;
1200 m_tmpLastCell
= cell
;
1202 else // mouse moved but stayed in the same cell
1205 OnCellMouseHover(cell
, x
, y
);
1208 m_tmpMouseMoved
= false;
1213 void wxHtmlWindow::StopAutoScrolling()
1215 if ( m_timerAutoScroll
)
1217 wxDELETE(m_timerAutoScroll
);
1221 void wxHtmlWindow::OnMouseEnter(wxMouseEvent
& event
)
1223 StopAutoScrolling();
1227 void wxHtmlWindow::OnMouseLeave(wxMouseEvent
& event
)
1229 // don't prevent the usual processing of the event from taking place
1232 // when a captured mouse leave a scrolled window we start generate
1233 // scrolling events to allow, for example, extending selection beyond the
1234 // visible area in some controls
1235 if ( wxWindow::GetCapture() == this )
1237 // where is the mouse leaving?
1239 wxPoint pt
= event
.GetPosition();
1242 orient
= wxHORIZONTAL
;
1245 else if ( pt
.y
< 0 )
1247 orient
= wxVERTICAL
;
1250 else // we're lower or to the right of the window
1252 wxSize size
= GetClientSize();
1253 if ( pt
.x
> size
.x
)
1255 orient
= wxHORIZONTAL
;
1256 pos
= GetVirtualSize().x
/ wxHTML_SCROLL_STEP
;
1258 else if ( pt
.y
> size
.y
)
1260 orient
= wxVERTICAL
;
1261 pos
= GetVirtualSize().y
/ wxHTML_SCROLL_STEP
;
1263 else // this should be impossible
1265 // but seems to happen sometimes under wxMSW - maybe it's a bug
1266 // there but for now just ignore it
1268 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1274 // only start the auto scroll timer if the window can be scrolled in
1276 if ( !HasScrollbar(orient
) )
1279 delete m_timerAutoScroll
;
1280 m_timerAutoScroll
= new wxHtmlWinAutoScrollTimer
1283 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1284 : wxEVT_SCROLLWIN_LINEDOWN
,
1288 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1292 void wxHtmlWindow::OnKeyUp(wxKeyEvent
& event
)
1294 if ( IsSelectionEnabled() &&
1295 event
.GetKeyCode() == 'C' && event
.ControlDown() )
1297 (void) CopySelection();
1301 void wxHtmlWindow::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1303 (void) CopySelection();
1306 void wxHtmlWindow::OnDoubleClick(wxMouseEvent
& event
)
1308 // select word under cursor:
1309 if ( IsSelectionEnabled() )
1311 SelectWord(CalcUnscrolledPosition(event
.GetPosition()));
1313 (void) CopySelection(Primary
);
1315 m_lastDoubleClick
= wxGetLocalTimeMillis();
1321 void wxHtmlWindow::SelectWord(const wxPoint
& pos
)
1325 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1329 m_selection
= new wxHtmlSelection();
1330 m_selection
->Set(cell
, cell
);
1331 RefreshRect(wxRect(CalcScrolledPosition(cell
->GetAbsPos()),
1332 wxSize(cell
->GetWidth(), cell
->GetHeight())));
1337 void wxHtmlWindow::SelectLine(const wxPoint
& pos
)
1341 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1344 // We use following heuristic to find a "line": let the line be all
1345 // cells in same container as the cell under mouse cursor that are
1346 // neither completely above nor completely bellow the clicked cell
1347 // (i.e. are likely to be words positioned on same line of text).
1349 int y1
= cell
->GetAbsPos().y
;
1350 int y2
= y1
+ cell
->GetHeight();
1352 const wxHtmlCell
*c
;
1353 const wxHtmlCell
*before
= NULL
;
1354 const wxHtmlCell
*after
= NULL
;
1356 // find last cell of line:
1357 for ( c
= cell
->GetNext(); c
; c
= c
->GetNext())
1359 y
= c
->GetAbsPos().y
;
1360 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1368 // find first cell of line:
1369 for ( c
= cell
->GetParent()->GetFirstChild();
1370 c
&& c
!= cell
; c
= c
->GetNext())
1372 y
= c
->GetAbsPos().y
;
1373 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1385 m_selection
= new wxHtmlSelection();
1386 m_selection
->Set(before
, after
);
1393 void wxHtmlWindow::SelectAll()
1398 m_selection
= new wxHtmlSelection();
1399 m_selection
->Set(m_Cell
->GetFirstTerminal(), m_Cell
->GetLastTerminal());
1404 #endif // wxUSE_CLIPBOARD
1408 IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor
,wxObject
)
1410 #if wxUSE_EXTENDED_RTTI
1411 IMPLEMENT_DYNAMIC_CLASS_XTI(wxHtmlWindow
, wxScrolledWindow
,"wx/html/htmlwin.h")
1413 wxBEGIN_PROPERTIES_TABLE(wxHtmlWindow
)
1416 style , wxHW_SCROLLBAR_AUTO
1417 borders , (dimension)
1421 wxEND_PROPERTIES_TABLE()
1423 wxBEGIN_HANDLERS_TABLE(wxHtmlWindow
)
1424 wxEND_HANDLERS_TABLE()
1426 wxCONSTRUCTOR_5( wxHtmlWindow
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
1428 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
1431 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
1432 EVT_SIZE(wxHtmlWindow::OnSize
)
1433 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseDown
)
1434 EVT_LEFT_UP(wxHtmlWindow::OnMouseUp
)
1435 EVT_RIGHT_UP(wxHtmlWindow::OnMouseUp
)
1436 EVT_MOTION(wxHtmlWindow::OnMouseMove
)
1437 EVT_ERASE_BACKGROUND(wxHtmlWindow::OnEraseBackground
)
1438 EVT_PAINT(wxHtmlWindow::OnPaint
)
1440 EVT_LEFT_DCLICK(wxHtmlWindow::OnDoubleClick
)
1441 EVT_ENTER_WINDOW(wxHtmlWindow::OnMouseEnter
)
1442 EVT_LEAVE_WINDOW(wxHtmlWindow::OnMouseLeave
)
1443 EVT_KEY_UP(wxHtmlWindow::OnKeyUp
)
1444 EVT_MENU(wxID_COPY
, wxHtmlWindow::OnCopy
)
1445 #endif // wxUSE_CLIPBOARD
1452 // A module to allow initialization/cleanup
1453 // without calling these functions from app.cpp or from
1454 // the user's application.
1456 class wxHtmlWinModule
: public wxModule
1458 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
1460 wxHtmlWinModule() : wxModule() {}
1461 bool OnInit() { return true; }
1462 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
1465 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
1468 // This hack forces the linker to always link in m_* files
1469 // (wxHTML doesn't work without handlers from these files)
1470 #include "wx/html/forcelnk.h"
1471 FORCE_WXHTML_MODULES()
1473 #endif // wxUSE_HTML