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 /////////////////////////////////////////////////////////////////////////////
10 #include "wx/wxprec.h"
13 #if wxUSE_HTML && wxUSE_STREAMS
22 #include "wx/dcclient.h"
26 #include "wx/html/htmlwin.h"
27 #include "wx/html/htmlproc.h"
29 #include "wx/clipbrd.h"
30 #include "wx/dataobj.h"
32 #include "wx/dcmemory.h"
33 #include "wx/settings.h"
35 #include "wx/arrimpl.cpp"
36 #include "wx/listimpl.cpp"
40 // ----------------------------------------------------------------------------
41 // wxHtmlWinAutoScrollTimer: the timer used to generate a stream of scroll
42 // events when a captured mouse is held outside the window
43 // ----------------------------------------------------------------------------
45 class wxHtmlWinAutoScrollTimer
: public wxTimer
48 wxHtmlWinAutoScrollTimer(wxScrolledWindow
*win
,
49 wxEventType eventTypeToSend
,
53 m_eventType
= eventTypeToSend
;
58 virtual void Notify();
61 wxScrolledWindow
*m_win
;
62 wxEventType m_eventType
;
66 DECLARE_NO_COPY_CLASS(wxHtmlWinAutoScrollTimer
)
69 void wxHtmlWinAutoScrollTimer::Notify()
71 // only do all this as long as the window is capturing the mouse
72 if ( wxWindow::GetCapture() != m_win
)
76 else // we still capture the mouse, continue generating events
78 // first scroll the window if we are allowed to do it
79 wxScrollWinEvent
event1(m_eventType
, m_pos
, m_orient
);
80 event1
.SetEventObject(m_win
);
81 if ( m_win
->GetEventHandler()->ProcessEvent(event1
) )
83 // and then send a pseudo mouse-move event to refresh the selection
84 wxMouseEvent
event2(wxEVT_MOTION
);
85 wxGetMousePosition(&event2
.m_x
, &event2
.m_y
);
87 // the mouse event coordinates should be client, not screen as
88 // returned by wxGetMousePosition
89 wxWindow
*parentTop
= m_win
;
90 while ( parentTop
->GetParent() )
91 parentTop
= parentTop
->GetParent();
92 wxPoint ptOrig
= parentTop
->GetPosition();
93 event2
.m_x
-= ptOrig
.x
;
94 event2
.m_y
-= ptOrig
.y
;
96 event2
.SetEventObject(m_win
);
98 // FIXME: we don't fill in the other members - ok?
99 m_win
->GetEventHandler()->ProcessEvent(event2
);
101 else // can't scroll further, stop
108 #endif // wxUSE_CLIPBOARD
112 //-----------------------------------------------------------------------------
114 //-----------------------------------------------------------------------------
116 // item of history list
117 class WXDLLIMPEXP_HTML wxHtmlHistoryItem
120 wxHtmlHistoryItem(const wxString
& p
, const wxString
& a
) {m_Page
= p
, m_Anchor
= a
, m_Pos
= 0;}
121 int GetPos() const {return m_Pos
;}
122 void SetPos(int p
) {m_Pos
= p
;}
123 const wxString
& GetPage() const {return m_Page
;}
124 const wxString
& GetAnchor() const {return m_Anchor
;}
133 //-----------------------------------------------------------------------------
134 // our private arrays:
135 //-----------------------------------------------------------------------------
137 WX_DECLARE_OBJARRAY(wxHtmlHistoryItem
, wxHtmlHistoryArray
);
138 WX_DEFINE_OBJARRAY(wxHtmlHistoryArray
);
140 WX_DECLARE_LIST(wxHtmlProcessor
, wxHtmlProcessorList
);
141 WX_DEFINE_LIST(wxHtmlProcessorList
);
143 //-----------------------------------------------------------------------------
145 //-----------------------------------------------------------------------------
148 void wxHtmlWindow::Init()
150 m_tmpMouseMoved
= false;
151 m_tmpLastLink
= NULL
;
152 m_tmpLastCell
= NULL
;
153 m_tmpCanDrawLocks
= 0;
154 m_FS
= new wxFileSystem();
156 m_RelatedStatusBar
= -1;
157 #endif // wxUSE_STATUSBAR
158 m_RelatedFrame
= NULL
;
159 m_TitleFormat
= wxT("%s");
160 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
162 m_Parser
= new wxHtmlWinParser(this);
163 m_Parser
->SetFS(m_FS
);
166 m_History
= new wxHtmlHistoryArray
;
171 m_makingSelection
= false;
173 m_timerAutoScroll
= NULL
;
174 m_lastDoubleClick
= 0;
175 #endif // wxUSE_CLIPBOARD
177 m_eraseBgInOnPaint
= false;
178 m_tmpSelFromCell
= NULL
;
181 bool wxHtmlWindow::Create(wxWindow
*parent
, wxWindowID id
,
182 const wxPoint
& pos
, const wxSize
& size
,
183 long style
, const wxString
& name
)
185 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
186 style
| wxVSCROLL
| wxHSCROLL
,
191 SetPage(wxT("<html><body></body></html>"));
196 wxHtmlWindow::~wxHtmlWindow()
200 #endif // wxUSE_CLIPBOARD
209 WX_CLEAR_LIST(wxHtmlProcessorList
, *m_Processors
);
221 void wxHtmlWindow::SetRelatedFrame(wxFrame
* frame
, const wxString
& format
)
223 m_RelatedFrame
= frame
;
224 m_TitleFormat
= format
;
230 void wxHtmlWindow::SetRelatedStatusBar(int bar
)
232 m_RelatedStatusBar
= bar
;
234 #endif // wxUSE_STATUSBAR
238 void wxHtmlWindow::SetFonts(wxString normal_face
, wxString fixed_face
, const int *sizes
)
240 wxString op
= m_OpenedPage
;
242 m_Parser
->SetFonts(normal_face
, fixed_face
, sizes
);
243 // fonts changed => contents invalid, so reload the page:
244 SetPage(wxT("<html><body></body></html>"));
249 void wxHtmlWindow::SetStandardFonts(int size
,
250 const wxString
& normal_face
,
251 const wxString
& fixed_face
)
253 wxString op
= m_OpenedPage
;
255 m_Parser
->SetStandardFonts(size
, normal_face
, fixed_face
);
256 // fonts changed => contents invalid, so reload the page:
257 SetPage(wxT("<html><body></body></html>"));
263 bool wxHtmlWindow::SetPage(const wxString
& source
)
265 wxString
newsrc(source
);
267 wxDELETE(m_selection
);
269 // we will soon delete all the cells, so clear pointers to them:
270 m_tmpSelFromCell
= NULL
;
272 // pass HTML through registered processors:
273 if (m_Processors
|| m_GlobalProcessors
)
275 wxHtmlProcessorList::compatibility_iterator nodeL
, nodeG
;
278 nodeL
= (m_Processors
) ? m_Processors
->GetFirst() : wxHtmlProcessorList::compatibility_iterator();
279 nodeG
= (m_GlobalProcessors
) ? m_GlobalProcessors
->GetFirst() : wxHtmlProcessorList::compatibility_iterator();
281 // VS: there are two lists, global and local, both of them sorted by
282 // priority. Since we have to go through _both_ lists with
283 // decreasing priority, we "merge-sort" the lists on-line by
284 // processing that one of the two heads that has higher priority
285 // in every iteration
286 while (nodeL
|| nodeG
)
288 prL
= (nodeL
) ? nodeL
->GetData()->GetPriority() : -1;
289 prG
= (nodeG
) ? nodeG
->GetData()->GetPriority() : -1;
292 if (nodeL
->GetData()->IsEnabled())
293 newsrc
= nodeL
->GetData()->Process(newsrc
);
294 nodeL
= nodeL
->GetNext();
298 if (nodeG
->GetData()->IsEnabled())
299 newsrc
= nodeG
->GetData()->Process(newsrc
);
300 nodeG
= nodeG
->GetNext();
305 // ...and run the parser on it:
306 wxClientDC
*dc
= new wxClientDC(this);
307 dc
->SetMapMode(wxMM_TEXT
);
308 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
309 SetBackgroundImage(wxNullBitmap
);
310 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
317 m_Cell
= (wxHtmlContainerCell
*) m_Parser
->Parse(newsrc
);
319 m_Cell
->SetIndent(m_Borders
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
320 m_Cell
->SetAlignHor(wxHTML_ALIGN_CENTER
);
322 if (m_tmpCanDrawLocks
== 0)
327 bool wxHtmlWindow::AppendToPage(const wxString
& source
)
329 return SetPage(*(GetParser()->GetSource()) + source
);
332 bool wxHtmlWindow::LoadPage(const wxString
& location
)
334 wxBusyCursor busyCursor
;
338 bool needs_refresh
= false;
341 if (m_HistoryOn
&& (m_HistoryPos
!= -1))
343 // store scroll position into history item:
345 GetViewStart(&x
, &y
);
346 (*m_History
)[m_HistoryPos
].SetPos(y
);
349 if (location
[0] == wxT('#'))
352 wxString anch
= location
.Mid(1) /*1 to end*/;
354 rt_val
= ScrollToAnchor(anch
);
357 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&& location
.BeforeFirst(wxT('#')) == m_OpenedPage
)
359 wxString anch
= location
.AfterFirst(wxT('#'));
361 rt_val
= ScrollToAnchor(anch
);
364 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&&
365 (m_FS
->GetPath() + location
.BeforeFirst(wxT('#'))) == m_OpenedPage
)
367 wxString anch
= location
.AfterFirst(wxT('#'));
369 rt_val
= ScrollToAnchor(anch
);
375 needs_refresh
= true;
378 if (m_RelatedStatusBar
!= -1)
380 m_RelatedFrame
->SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
383 #endif // wxUSE_STATUSBAR
385 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location
);
387 // try to interpret 'location' as filename instead of URL:
390 wxFileName
fn(location
);
391 wxString location2
= wxFileSystem::FileNameToURL(fn
);
392 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location2
);
397 wxLogError(_("Unable to open requested HTML document: %s"), location
.c_str());
404 wxList::compatibility_iterator node
;
405 wxString src
= wxEmptyString
;
408 if (m_RelatedStatusBar
!= -1)
410 wxString msg
= _("Loading : ") + location
;
411 m_RelatedFrame
->SetStatusText(msg
, m_RelatedStatusBar
);
414 #endif // wxUSE_STATUSBAR
416 node
= m_Filters
.GetFirst();
419 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
422 src
= h
->ReadFile(*f
);
425 node
= node
->GetNext();
427 if (src
== wxEmptyString
)
429 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
430 src
= m_DefaultFilter
->ReadFile(*f
);
433 m_FS
->ChangePathTo(f
->GetLocation());
434 rt_val
= SetPage(src
);
435 m_OpenedPage
= f
->GetLocation();
436 if (f
->GetAnchor() != wxEmptyString
)
438 ScrollToAnchor(f
->GetAnchor());
444 if (m_RelatedStatusBar
!= -1)
445 m_RelatedFrame
->SetStatusText(_("Done"), m_RelatedStatusBar
);
446 #endif // wxUSE_STATUSBAR
450 if (m_HistoryOn
) // add this page to history there:
452 int c
= m_History
->GetCount() - (m_HistoryPos
+ 1);
454 if (m_HistoryPos
< 0 ||
455 (*m_History
)[m_HistoryPos
].GetPage() != m_OpenedPage
||
456 (*m_History
)[m_HistoryPos
].GetAnchor() != m_OpenedAnchor
)
459 for (int i
= 0; i
< c
; i
++)
460 m_History
->RemoveAt(m_HistoryPos
);
461 m_History
->Add(new wxHtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
465 if (m_OpenedPageTitle
== wxEmptyString
)
466 OnSetTitle(wxFileNameFromPath(m_OpenedPage
));
480 bool wxHtmlWindow::LoadFile(const wxFileName
& filename
)
482 wxString url
= wxFileSystem::FileNameToURL(filename
);
483 return LoadPage(url
);
487 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
489 const wxHtmlCell
*c
= m_Cell
->Find(wxHTML_COND_ISANCHOR
, &anchor
);
492 wxLogWarning(_("HTML anchor %s does not exist."), anchor
.c_str());
499 for (y
= 0; c
!= NULL
; c
= c
->GetParent()) y
+= c
->GetPosY();
500 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
501 m_OpenedAnchor
= anchor
;
507 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
512 tit
.Printf(m_TitleFormat
, title
.c_str());
513 m_RelatedFrame
->SetTitle(tit
);
515 m_OpenedPageTitle
= title
;
522 void wxHtmlWindow::CreateLayout()
524 int ClientWidth
, ClientHeight
;
528 if (m_Style
& wxHW_SCROLLBAR_NEVER
)
530 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // always off
531 GetClientSize(&ClientWidth
, &ClientHeight
);
532 m_Cell
->Layout(ClientWidth
);
536 GetClientSize(&ClientWidth
, &ClientHeight
);
537 m_Cell
->Layout(ClientWidth
);
538 if (ClientHeight
< m_Cell
->GetHeight() + GetCharHeight())
541 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
542 m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
,
543 (m_Cell
->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
544 /*cheat: top-level frag is always container*/);
546 else /* we fit into window, no need for scrollbars */
548 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
549 GetClientSize(&ClientWidth
, &ClientHeight
);
550 m_Cell
->Layout(ClientWidth
); // ...and relayout
557 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
562 wxString p_fff
, p_ffn
;
564 if (path
!= wxEmptyString
)
566 oldpath
= cfg
->GetPath();
570 m_Borders
= cfg
->Read(wxT("wxHtmlWindow/Borders"), m_Borders
);
571 p_fff
= cfg
->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
572 p_ffn
= cfg
->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
573 for (int i
= 0; i
< 7; i
++)
575 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
576 p_fontsizes
[i
] = cfg
->Read(tmp
, m_Parser
->m_FontsSizes
[i
]);
578 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
580 if (path
!= wxEmptyString
)
581 cfg
->SetPath(oldpath
);
586 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
591 if (path
!= wxEmptyString
)
593 oldpath
= cfg
->GetPath();
597 cfg
->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders
);
598 cfg
->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
599 cfg
->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
600 for (int i
= 0; i
< 7; i
++)
602 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
603 cfg
->Write(tmp
, (long) m_Parser
->m_FontsSizes
[i
]);
606 if (path
!= wxEmptyString
)
607 cfg
->SetPath(oldpath
);
612 bool wxHtmlWindow::HistoryBack()
616 if (m_HistoryPos
< 1) return false;
618 // store scroll position into history item:
620 GetViewStart(&x
, &y
);
621 (*m_History
)[m_HistoryPos
].SetPos(y
);
623 // go to previous position:
626 l
= (*m_History
)[m_HistoryPos
].GetPage();
627 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
630 if (a
== wxEmptyString
) LoadPage(l
);
631 else LoadPage(l
+ wxT("#") + a
);
634 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
639 bool wxHtmlWindow::HistoryCanBack()
641 if (m_HistoryPos
< 1) return false;
646 bool wxHtmlWindow::HistoryForward()
650 if (m_HistoryPos
== -1) return false;
651 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return false;
653 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
656 l
= (*m_History
)[m_HistoryPos
].GetPage();
657 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
660 if (a
== wxEmptyString
) LoadPage(l
);
661 else LoadPage(l
+ wxT("#") + a
);
664 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
669 bool wxHtmlWindow::HistoryCanForward()
671 if (m_HistoryPos
== -1) return false;
672 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return false;
677 void wxHtmlWindow::HistoryClear()
683 void wxHtmlWindow::AddProcessor(wxHtmlProcessor
*processor
)
687 m_Processors
= new wxHtmlProcessorList
;
689 wxHtmlProcessorList::compatibility_iterator node
;
691 for (node
= m_Processors
->GetFirst(); node
; node
= node
->GetNext())
693 if (processor
->GetPriority() > node
->GetData()->GetPriority())
695 m_Processors
->Insert(node
, processor
);
699 m_Processors
->Append(processor
);
702 /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor
*processor
)
704 if (!m_GlobalProcessors
)
706 m_GlobalProcessors
= new wxHtmlProcessorList
;
708 wxHtmlProcessorList::compatibility_iterator node
;
710 for (node
= m_GlobalProcessors
->GetFirst(); node
; node
= node
->GetNext())
712 if (processor
->GetPriority() > node
->GetData()->GetPriority())
714 m_GlobalProcessors
->Insert(node
, processor
);
718 m_GlobalProcessors
->Append(processor
);
723 wxList
wxHtmlWindow::m_Filters
;
724 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
725 wxHtmlProcessorList
*wxHtmlWindow::m_GlobalProcessors
= NULL
;
727 void wxHtmlWindow::CleanUpStatics()
729 wxDELETE(m_DefaultFilter
);
730 WX_CLEAR_LIST(wxList
, m_Filters
);
731 if (m_GlobalProcessors
)
732 WX_CLEAR_LIST(wxHtmlProcessorList
, *m_GlobalProcessors
);
733 wxDELETE(m_GlobalProcessors
);
738 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
740 m_Filters
.Append(filter
);
744 bool wxHtmlWindow::IsSelectionEnabled() const
747 return !(m_Style
& wxHW_NO_SELECTION
);
755 wxString
wxHtmlWindow::DoSelectionToText(wxHtmlSelection
*sel
)
758 return wxEmptyString
;
762 const wxHtmlCell
*end
= sel
->GetToCell();
764 wxHtmlTerminalCellsInterator
i(sel
->GetFromCell(), end
);
767 text
<< i
->ConvertToText(sel
);
770 const wxHtmlCell
*prev
= *i
;
773 if ( prev
->GetParent() != i
->GetParent() )
775 text
<< i
->ConvertToText(*i
== end
? sel
: NULL
);
782 wxString
wxHtmlWindow::ToText()
787 sel
.Set(m_Cell
->GetFirstTerminal(), m_Cell
->GetLastTerminal());
788 return DoSelectionToText(&sel
);
791 return wxEmptyString
;
794 #endif // wxUSE_CLIPBOARD
796 bool wxHtmlWindow::CopySelection(ClipboardType t
)
801 #if defined(__UNIX__) && !defined(__WXMAC__)
802 wxTheClipboard
->UsePrimarySelection(t
== Primary
);
804 // Primary selection exists only under X11, so don't do anything under
805 // the other platforms when we try to access it
807 // TODO: this should be abstracted at wxClipboard level!
810 #endif // __UNIX__/!__UNIX__
812 if ( wxTheClipboard
->Open() )
814 const wxString
txt(SelectionToText());
815 wxTheClipboard
->SetData(new wxTextDataObject(txt
));
816 wxTheClipboard
->Close();
817 wxLogTrace(_T("wxhtmlselection"),
818 _("Copied to clipboard:\"%s\""), txt
.c_str());
825 #endif // wxUSE_CLIPBOARD
831 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
833 const wxMouseEvent
*e
= link
.GetEvent();
834 if (e
== NULL
|| e
->LeftUp())
835 LoadPage(link
.GetHref());
838 void wxHtmlWindow::OnCellClicked(wxHtmlCell
*cell
,
839 wxCoord x
, wxCoord y
,
840 const wxMouseEvent
& event
)
842 wxCHECK_RET( cell
, _T("can't be called with NULL cell") );
844 cell
->OnMouseClick(this, x
, y
, event
);
847 void wxHtmlWindow::OnCellMouseHover(wxHtmlCell
* WXUNUSED(cell
),
848 wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
853 void wxHtmlWindow::OnEraseBackground(wxEraseEvent
& event
)
857 // don't even skip the event, if we don't have a bg bitmap we're going
858 // to overwrite background in OnPaint() below anyhow, so letting the
859 // default handling take place would only result in flicker, just set a
860 // flag to erase the background below
861 m_eraseBgInOnPaint
= true;
865 wxDC
& dc
= *event
.GetDC();
867 // if the image is not fully opaque, we have to erase the background before
868 // drawing it, however avoid doing it for opaque images as this would just
869 // result in extra flicker without any other effect as background is
870 // completely covered anyhow
871 if ( m_bmpBg
.GetMask() )
873 dc
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
877 const wxSize
sizeWin(GetClientSize());
878 const wxSize
sizeBmp(m_bmpBg
.GetWidth(), m_bmpBg
.GetHeight());
879 for ( wxCoord x
= 0; x
< sizeWin
.x
; x
+= sizeBmp
.x
)
881 for ( wxCoord y
= 0; y
< sizeWin
.y
; y
+= sizeBmp
.y
)
883 dc
.DrawBitmap(m_bmpBg
, x
, y
, true /* use mask */);
888 void wxHtmlWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
892 if (m_tmpCanDrawLocks
> 0 || m_Cell
== NULL
)
896 GetViewStart(&x
, &y
);
897 wxRect rect
= GetUpdateRegion().GetBox();
898 wxSize sz
= GetSize();
902 m_backBuffer
= new wxBitmap(sz
.x
, sz
.y
);
903 dcm
.SelectObject(*m_backBuffer
);
905 if ( m_eraseBgInOnPaint
)
907 dcm
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
910 m_eraseBgInOnPaint
= false;
912 else // someone has already erased the background, keep it
914 // preserve the existing background, otherwise we'd erase anything the
915 // user code had drawn in its EVT_ERASE_BACKGROUND handler when we do
916 // the Blit back below
917 dcm
.Blit(0, rect
.GetTop(),
918 sz
.x
, rect
.GetBottom() - rect
.GetTop() + 1,
924 dcm
.SetMapMode(wxMM_TEXT
);
925 dcm
.SetBackgroundMode(wxTRANSPARENT
);
927 wxHtmlRenderingInfo rinfo
;
928 wxDefaultHtmlRenderingStyle rstyle
;
929 rinfo
.SetSelection(m_selection
);
930 rinfo
.SetStyle(&rstyle
);
931 m_Cell
->Draw(dcm
, 0, 0,
932 y
* wxHTML_SCROLL_STEP
+ rect
.GetTop(),
933 y
* wxHTML_SCROLL_STEP
+ rect
.GetBottom(),
936 //#define DEBUG_HTML_SELECTION
937 #ifdef DEBUG_HTML_SELECTION
940 wxGetMousePosition(&xc
, &yc
);
941 ScreenToClient(&xc
, &yc
);
942 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
943 wxHtmlCell
*at
= m_Cell
->FindCellByPos(x
, y
);
945 m_Cell
->FindCellByPos(x
, y
, wxHTML_FIND_NEAREST_BEFORE
);
947 m_Cell
->FindCellByPos(x
, y
, wxHTML_FIND_NEAREST_AFTER
);
949 dcm
.SetBrush(*wxTRANSPARENT_BRUSH
);
950 dcm
.SetPen(*wxBLACK_PEN
);
952 dcm
.DrawRectangle(at
->GetAbsPos(),
953 wxSize(at
->GetWidth(),at
->GetHeight()));
954 dcm
.SetPen(*wxGREEN_PEN
);
956 dcm
.DrawRectangle(before
->GetAbsPos().x
+1, before
->GetAbsPos().y
+1,
957 before
->GetWidth()-2,before
->GetHeight()-2);
958 dcm
.SetPen(*wxRED_PEN
);
960 dcm
.DrawRectangle(after
->GetAbsPos().x
+2, after
->GetAbsPos().y
+2,
961 after
->GetWidth()-4,after
->GetHeight()-4);
965 dcm
.SetDeviceOrigin(0,0);
966 dc
.Blit(0, rect
.GetTop(),
967 sz
.x
, rect
.GetBottom() - rect
.GetTop() + 1,
975 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
977 wxDELETE(m_backBuffer
);
979 wxScrolledWindow::OnSize(event
);
982 // Recompute selection if necessary:
985 m_selection
->Set(m_selection
->GetFromCell(),
986 m_selection
->GetToCell());
987 m_selection
->ClearPrivPos();
994 void wxHtmlWindow::OnMouseMove(wxMouseEvent
& WXUNUSED(event
))
996 m_tmpMouseMoved
= true;
999 void wxHtmlWindow::OnMouseDown(wxMouseEvent
& event
)
1002 if ( event
.LeftDown() && IsSelectionEnabled() )
1004 const long TRIPLECLICK_LEN
= 200; // 0.2 sec after doubleclick
1005 if ( wxGetLocalTimeMillis() - m_lastDoubleClick
<= TRIPLECLICK_LEN
)
1007 SelectLine(CalcUnscrolledPosition(event
.GetPosition()));
1009 (void) CopySelection();
1013 m_makingSelection
= true;
1017 wxDELETE(m_selection
);
1020 m_tmpSelFromPos
= CalcUnscrolledPosition(event
.GetPosition());
1021 m_tmpSelFromCell
= NULL
;
1028 #endif // wxUSE_CLIPBOARD
1031 void wxHtmlWindow::OnMouseUp(wxMouseEvent
& event
)
1034 if ( m_makingSelection
)
1037 m_makingSelection
= false;
1039 // did the user move the mouse far enough from starting point?
1040 if ( CopySelection(Primary
) )
1042 // we don't want mouse up event that ended selecting to be
1043 // handled as mouse click and e.g. follow hyperlink:
1047 #endif // wxUSE_CLIPBOARD
1052 wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
1053 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1055 // check is needed because FindCellByPos returns terminal cell and
1056 // containers may have empty borders -- in this case NULL will be
1059 OnCellClicked(cell
, pos
.x
, pos
.y
, event
);
1065 void wxHtmlWindow::OnInternalIdle()
1067 wxWindow::OnInternalIdle();
1069 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
))
1071 #ifdef DEBUG_HTML_SELECTION
1075 wxGetMousePosition(&xc
, &yc
);
1076 ScreenToClient(&xc
, &yc
);
1077 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
1079 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(x
, y
);
1081 // handle selection update:
1082 if ( m_makingSelection
)
1084 if ( !m_tmpSelFromCell
)
1085 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1086 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
);
1088 // NB: a trick - we adjust selFromPos to be upper left or bottom
1089 // right corner of the first cell of the selection depending
1090 // on whether the mouse is moving to the right or to the left.
1091 // This gives us more "natural" behaviour when selecting
1092 // a line (specifically, first cell of the next line is not
1093 // included if you drag selection from left to right over
1096 if ( !m_tmpSelFromCell
)
1098 dirFromPos
= m_tmpSelFromPos
;
1102 dirFromPos
= m_tmpSelFromCell
->GetAbsPos();
1103 if ( x
< m_tmpSelFromPos
.x
)
1105 dirFromPos
.x
+= m_tmpSelFromCell
->GetWidth();
1106 dirFromPos
.y
+= m_tmpSelFromCell
->GetHeight();
1109 bool goingDown
= dirFromPos
.y
< y
||
1110 (dirFromPos
.y
== y
&& dirFromPos
.x
< x
);
1112 // determine selection span:
1113 if ( /*still*/ !m_tmpSelFromCell
)
1117 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1118 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1119 wxHTML_FIND_NEAREST_AFTER
);
1120 if (!m_tmpSelFromCell
)
1121 m_tmpSelFromCell
= m_Cell
->GetFirstTerminal();
1125 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1126 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1127 wxHTML_FIND_NEAREST_BEFORE
);
1128 if (!m_tmpSelFromCell
)
1129 m_tmpSelFromCell
= m_Cell
->GetLastTerminal();
1133 wxHtmlCell
*selcell
= cell
;
1138 selcell
= m_Cell
->FindCellByPos(x
, y
,
1139 wxHTML_FIND_NEAREST_BEFORE
);
1141 selcell
= m_Cell
->GetLastTerminal();
1145 selcell
= m_Cell
->FindCellByPos(x
, y
,
1146 wxHTML_FIND_NEAREST_AFTER
);
1148 selcell
= m_Cell
->GetFirstTerminal();
1152 // NB: it may *rarely* happen that the code above didn't find one
1153 // of the cells, e.g. if wxHtmlWindow doesn't contain any
1155 if ( selcell
&& m_tmpSelFromCell
)
1159 // start selecting only if mouse movement was big enough
1160 // (otherwise it was meant as mouse click, not selection):
1161 const int PRECISION
= 2;
1162 wxPoint diff
= m_tmpSelFromPos
- wxPoint(x
,y
);
1163 if (abs(diff
.x
) > PRECISION
|| abs(diff
.y
) > PRECISION
)
1165 m_selection
= new wxHtmlSelection();
1170 if ( m_tmpSelFromCell
->IsBefore(selcell
) )
1172 m_selection
->Set(m_tmpSelFromPos
, m_tmpSelFromCell
,
1173 wxPoint(x
,y
), selcell
); }
1176 m_selection
->Set(wxPoint(x
,y
), selcell
,
1177 m_tmpSelFromPos
, m_tmpSelFromCell
);
1179 m_selection
->ClearPrivPos();
1185 // handle cursor and status bar text changes:
1186 if ( cell
!= m_tmpLastCell
)
1188 wxHtmlLinkInfo
*lnk
= cell
? cell
->GetLink(x
, y
) : NULL
;
1191 cur
= cell
->GetCursor();
1193 cur
= *wxSTANDARD_CURSOR
;
1196 if (lnk
!= m_tmpLastLink
)
1201 if (m_RelatedStatusBar
!= -1)
1202 m_RelatedFrame
->SetStatusText(wxEmptyString
,
1203 m_RelatedStatusBar
);
1207 if (m_RelatedStatusBar
!= -1)
1208 m_RelatedFrame
->SetStatusText(lnk
->GetHref(),
1209 m_RelatedStatusBar
);
1211 #endif // wxUSE_STATUSBAR
1212 m_tmpLastLink
= lnk
;
1215 m_tmpLastCell
= cell
;
1217 else // mouse moved but stayed in the same cell
1220 OnCellMouseHover(cell
, x
, y
);
1223 m_tmpMouseMoved
= false;
1228 void wxHtmlWindow::StopAutoScrolling()
1230 if ( m_timerAutoScroll
)
1232 wxDELETE(m_timerAutoScroll
);
1236 void wxHtmlWindow::OnMouseEnter(wxMouseEvent
& event
)
1238 StopAutoScrolling();
1242 void wxHtmlWindow::OnMouseLeave(wxMouseEvent
& event
)
1244 // don't prevent the usual processing of the event from taking place
1247 // when a captured mouse leave a scrolled window we start generate
1248 // scrolling events to allow, for example, extending selection beyond the
1249 // visible area in some controls
1250 if ( wxWindow::GetCapture() == this )
1252 // where is the mouse leaving?
1254 wxPoint pt
= event
.GetPosition();
1257 orient
= wxHORIZONTAL
;
1260 else if ( pt
.y
< 0 )
1262 orient
= wxVERTICAL
;
1265 else // we're lower or to the right of the window
1267 wxSize size
= GetClientSize();
1268 if ( pt
.x
> size
.x
)
1270 orient
= wxHORIZONTAL
;
1271 pos
= GetVirtualSize().x
/ wxHTML_SCROLL_STEP
;
1273 else if ( pt
.y
> size
.y
)
1275 orient
= wxVERTICAL
;
1276 pos
= GetVirtualSize().y
/ wxHTML_SCROLL_STEP
;
1278 else // this should be impossible
1280 // but seems to happen sometimes under wxMSW - maybe it's a bug
1281 // there but for now just ignore it
1283 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1289 // only start the auto scroll timer if the window can be scrolled in
1291 if ( !HasScrollbar(orient
) )
1294 delete m_timerAutoScroll
;
1295 m_timerAutoScroll
= new wxHtmlWinAutoScrollTimer
1298 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1299 : wxEVT_SCROLLWIN_LINEDOWN
,
1303 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1307 void wxHtmlWindow::OnKeyUp(wxKeyEvent
& event
)
1309 if ( IsSelectionEnabled() &&
1310 event
.GetKeyCode() == 'C' && event
.ControlDown() )
1312 (void) CopySelection();
1316 void wxHtmlWindow::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1318 (void) CopySelection();
1321 void wxHtmlWindow::OnDoubleClick(wxMouseEvent
& event
)
1323 // select word under cursor:
1324 if ( IsSelectionEnabled() )
1326 SelectWord(CalcUnscrolledPosition(event
.GetPosition()));
1328 (void) CopySelection(Primary
);
1330 m_lastDoubleClick
= wxGetLocalTimeMillis();
1336 void wxHtmlWindow::SelectWord(const wxPoint
& pos
)
1340 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1344 m_selection
= new wxHtmlSelection();
1345 m_selection
->Set(cell
, cell
);
1346 RefreshRect(wxRect(CalcScrolledPosition(cell
->GetAbsPos()),
1347 wxSize(cell
->GetWidth(), cell
->GetHeight())));
1352 void wxHtmlWindow::SelectLine(const wxPoint
& pos
)
1356 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1359 // We use following heuristic to find a "line": let the line be all
1360 // cells in same container as the cell under mouse cursor that are
1361 // neither completely above nor completely bellow the clicked cell
1362 // (i.e. are likely to be words positioned on same line of text).
1364 int y1
= cell
->GetAbsPos().y
;
1365 int y2
= y1
+ cell
->GetHeight();
1367 const wxHtmlCell
*c
;
1368 const wxHtmlCell
*before
= NULL
;
1369 const wxHtmlCell
*after
= NULL
;
1371 // find last cell of line:
1372 for ( c
= cell
->GetNext(); c
; c
= c
->GetNext())
1374 y
= c
->GetAbsPos().y
;
1375 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1383 // find first cell of line:
1384 for ( c
= cell
->GetParent()->GetFirstChild();
1385 c
&& c
!= cell
; c
= c
->GetNext())
1387 y
= c
->GetAbsPos().y
;
1388 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1400 m_selection
= new wxHtmlSelection();
1401 m_selection
->Set(before
, after
);
1408 void wxHtmlWindow::SelectAll()
1413 m_selection
= new wxHtmlSelection();
1414 m_selection
->Set(m_Cell
->GetFirstTerminal(), m_Cell
->GetLastTerminal());
1419 #endif // wxUSE_CLIPBOARD
1423 IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor
,wxObject
)
1425 #if wxUSE_EXTENDED_RTTI
1426 IMPLEMENT_DYNAMIC_CLASS_XTI(wxHtmlWindow
, wxScrolledWindow
,"wx/html/htmlwin.h")
1428 wxBEGIN_PROPERTIES_TABLE(wxHtmlWindow
)
1431 style , wxHW_SCROLLBAR_AUTO
1432 borders , (dimension)
1436 wxEND_PROPERTIES_TABLE()
1438 wxBEGIN_HANDLERS_TABLE(wxHtmlWindow
)
1439 wxEND_HANDLERS_TABLE()
1441 wxCONSTRUCTOR_5( wxHtmlWindow
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
1443 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
1446 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
1447 EVT_SIZE(wxHtmlWindow::OnSize
)
1448 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseDown
)
1449 EVT_LEFT_UP(wxHtmlWindow::OnMouseUp
)
1450 EVT_RIGHT_UP(wxHtmlWindow::OnMouseUp
)
1451 EVT_MOTION(wxHtmlWindow::OnMouseMove
)
1452 EVT_ERASE_BACKGROUND(wxHtmlWindow::OnEraseBackground
)
1453 EVT_PAINT(wxHtmlWindow::OnPaint
)
1455 EVT_LEFT_DCLICK(wxHtmlWindow::OnDoubleClick
)
1456 EVT_ENTER_WINDOW(wxHtmlWindow::OnMouseEnter
)
1457 EVT_LEAVE_WINDOW(wxHtmlWindow::OnMouseLeave
)
1458 EVT_KEY_UP(wxHtmlWindow::OnKeyUp
)
1459 EVT_MENU(wxID_COPY
, wxHtmlWindow::OnCopy
)
1460 #endif // wxUSE_CLIPBOARD
1467 // A module to allow initialization/cleanup
1468 // without calling these functions from app.cpp or from
1469 // the user's application.
1471 class wxHtmlWinModule
: public wxModule
1473 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
1475 wxHtmlWinModule() : wxModule() {}
1476 bool OnInit() { return true; }
1477 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
1480 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
1483 // This hack forces the linker to always link in m_* files
1484 // (wxHTML doesn't work without handlers from these files)
1485 #include "wx/html/forcelnk.h"
1486 FORCE_WXHTML_MODULES()
1488 #endif // wxUSE_HTML