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
183 m_eraseBgInOnPaint
= false;
184 m_tmpSelFromCell
= NULL
;
187 bool wxHtmlWindow::Create(wxWindow
*parent
, wxWindowID id
,
188 const wxPoint
& pos
, const wxSize
& size
,
189 long style
, const wxString
& name
)
191 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
192 style
| wxVSCROLL
| wxHSCROLL
,
197 SetPage(wxT("<html><body></body></html>"));
202 wxHtmlWindow::~wxHtmlWindow()
206 #endif // wxUSE_CLIPBOARD
215 WX_CLEAR_LIST(wxHtmlProcessorList
, *m_Processors
);
227 void wxHtmlWindow::SetRelatedFrame(wxFrame
* frame
, const wxString
& format
)
229 m_RelatedFrame
= frame
;
230 m_TitleFormat
= format
;
236 void wxHtmlWindow::SetRelatedStatusBar(int bar
)
238 m_RelatedStatusBar
= bar
;
240 #endif // wxUSE_STATUSBAR
244 void wxHtmlWindow::SetFonts(wxString normal_face
, wxString fixed_face
, const int *sizes
)
246 wxString op
= m_OpenedPage
;
248 m_Parser
->SetFonts(normal_face
, fixed_face
, sizes
);
249 // fonts changed => contents invalid, so reload the page:
250 SetPage(wxT("<html><body></body></html>"));
255 void wxHtmlWindow::SetStandardFonts(int size
,
256 const wxString
& normal_face
,
257 const wxString
& fixed_face
)
259 wxString op
= m_OpenedPage
;
261 m_Parser
->SetStandardFonts(size
, normal_face
, fixed_face
);
262 // fonts changed => contents invalid, so reload the page:
263 SetPage(wxT("<html><body></body></html>"));
269 bool wxHtmlWindow::SetPage(const wxString
& source
)
271 wxString
newsrc(source
);
273 wxDELETE(m_selection
);
275 // we will soon delete all the cells, so clear pointers to them:
276 m_tmpSelFromCell
= NULL
;
278 // pass HTML through registered processors:
279 if (m_Processors
|| m_GlobalProcessors
)
281 wxHtmlProcessorList::compatibility_iterator nodeL
, nodeG
;
284 nodeL
= (m_Processors
) ? m_Processors
->GetFirst() : wxHtmlProcessorList::compatibility_iterator();
285 nodeG
= (m_GlobalProcessors
) ? m_GlobalProcessors
->GetFirst() : wxHtmlProcessorList::compatibility_iterator();
287 // VS: there are two lists, global and local, both of them sorted by
288 // priority. Since we have to go through _both_ lists with
289 // decreasing priority, we "merge-sort" the lists on-line by
290 // processing that one of the two heads that has higher priority
291 // in every iteration
292 while (nodeL
|| nodeG
)
294 prL
= (nodeL
) ? nodeL
->GetData()->GetPriority() : -1;
295 prG
= (nodeG
) ? nodeG
->GetData()->GetPriority() : -1;
298 if (nodeL
->GetData()->IsEnabled())
299 newsrc
= nodeL
->GetData()->Process(newsrc
);
300 nodeL
= nodeL
->GetNext();
304 if (nodeG
->GetData()->IsEnabled())
305 newsrc
= nodeG
->GetData()->Process(newsrc
);
306 nodeG
= nodeG
->GetNext();
311 // ...and run the parser on it:
312 wxClientDC
*dc
= new wxClientDC(this);
313 dc
->SetMapMode(wxMM_TEXT
);
314 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
315 SetBackgroundImage(wxNullBitmap
);
316 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
323 m_Cell
= (wxHtmlContainerCell
*) m_Parser
->Parse(newsrc
);
325 m_Cell
->SetIndent(m_Borders
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
326 m_Cell
->SetAlignHor(wxHTML_ALIGN_CENTER
);
328 if (m_tmpCanDrawLocks
== 0)
333 bool wxHtmlWindow::AppendToPage(const wxString
& source
)
335 return SetPage(*(GetParser()->GetSource()) + source
);
338 bool wxHtmlWindow::LoadPage(const wxString
& location
)
340 wxBusyCursor busyCursor
;
344 bool needs_refresh
= false;
347 if (m_HistoryOn
&& (m_HistoryPos
!= -1))
349 // store scroll position into history item:
351 GetViewStart(&x
, &y
);
352 (*m_History
)[m_HistoryPos
].SetPos(y
);
355 if (location
[0] == wxT('#'))
358 wxString anch
= location
.Mid(1) /*1 to end*/;
360 rt_val
= ScrollToAnchor(anch
);
363 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&& location
.BeforeFirst(wxT('#')) == m_OpenedPage
)
365 wxString anch
= location
.AfterFirst(wxT('#'));
367 rt_val
= ScrollToAnchor(anch
);
370 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&&
371 (m_FS
->GetPath() + location
.BeforeFirst(wxT('#'))) == m_OpenedPage
)
373 wxString anch
= location
.AfterFirst(wxT('#'));
375 rt_val
= ScrollToAnchor(anch
);
381 needs_refresh
= true;
384 if (m_RelatedStatusBar
!= -1)
386 m_RelatedFrame
->SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
389 #endif // wxUSE_STATUSBAR
391 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location
);
393 // try to interpret 'location' as filename instead of URL:
396 wxFileName
fn(location
);
397 wxString location2
= wxFileSystem::FileNameToURL(fn
);
398 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location2
);
403 wxLogError(_("Unable to open requested HTML document: %s"), location
.c_str());
410 wxList::compatibility_iterator node
;
411 wxString src
= wxEmptyString
;
414 if (m_RelatedStatusBar
!= -1)
416 wxString msg
= _("Loading : ") + location
;
417 m_RelatedFrame
->SetStatusText(msg
, m_RelatedStatusBar
);
420 #endif // wxUSE_STATUSBAR
422 node
= m_Filters
.GetFirst();
425 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
428 src
= h
->ReadFile(*f
);
431 node
= node
->GetNext();
433 if (src
== wxEmptyString
)
435 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
436 src
= m_DefaultFilter
->ReadFile(*f
);
439 m_FS
->ChangePathTo(f
->GetLocation());
440 rt_val
= SetPage(src
);
441 m_OpenedPage
= f
->GetLocation();
442 if (f
->GetAnchor() != wxEmptyString
)
444 ScrollToAnchor(f
->GetAnchor());
450 if (m_RelatedStatusBar
!= -1)
451 m_RelatedFrame
->SetStatusText(_("Done"), m_RelatedStatusBar
);
452 #endif // wxUSE_STATUSBAR
456 if (m_HistoryOn
) // add this page to history there:
458 int c
= m_History
->GetCount() - (m_HistoryPos
+ 1);
460 if (m_HistoryPos
< 0 ||
461 (*m_History
)[m_HistoryPos
].GetPage() != m_OpenedPage
||
462 (*m_History
)[m_HistoryPos
].GetAnchor() != m_OpenedAnchor
)
465 for (int i
= 0; i
< c
; i
++)
466 m_History
->RemoveAt(m_HistoryPos
);
467 m_History
->Add(new wxHtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
471 if (m_OpenedPageTitle
== wxEmptyString
)
472 OnSetTitle(wxFileNameFromPath(m_OpenedPage
));
486 bool wxHtmlWindow::LoadFile(const wxFileName
& filename
)
488 wxString url
= wxFileSystem::FileNameToURL(filename
);
489 return LoadPage(url
);
493 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
495 const wxHtmlCell
*c
= m_Cell
->Find(wxHTML_COND_ISANCHOR
, &anchor
);
498 wxLogWarning(_("HTML anchor %s does not exist."), anchor
.c_str());
505 for (y
= 0; c
!= NULL
; c
= c
->GetParent()) y
+= c
->GetPosY();
506 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
507 m_OpenedAnchor
= anchor
;
513 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
518 tit
.Printf(m_TitleFormat
, title
.c_str());
519 m_RelatedFrame
->SetTitle(tit
);
521 m_OpenedPageTitle
= title
;
528 void wxHtmlWindow::CreateLayout()
530 int ClientWidth
, ClientHeight
;
534 if (m_Style
& wxHW_SCROLLBAR_NEVER
)
536 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // always off
537 GetClientSize(&ClientWidth
, &ClientHeight
);
538 m_Cell
->Layout(ClientWidth
);
542 GetClientSize(&ClientWidth
, &ClientHeight
);
543 m_Cell
->Layout(ClientWidth
);
544 if (ClientHeight
< m_Cell
->GetHeight() + GetCharHeight())
547 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
548 m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
,
549 (m_Cell
->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
550 /*cheat: top-level frag is always container*/);
552 else /* we fit into window, no need for scrollbars */
554 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
555 GetClientSize(&ClientWidth
, &ClientHeight
);
556 m_Cell
->Layout(ClientWidth
); // ...and relayout
563 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
568 wxString p_fff
, p_ffn
;
570 if (path
!= wxEmptyString
)
572 oldpath
= cfg
->GetPath();
576 m_Borders
= cfg
->Read(wxT("wxHtmlWindow/Borders"), m_Borders
);
577 p_fff
= cfg
->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
578 p_ffn
= cfg
->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
579 for (int i
= 0; i
< 7; i
++)
581 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
582 p_fontsizes
[i
] = cfg
->Read(tmp
, m_Parser
->m_FontsSizes
[i
]);
584 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
586 if (path
!= wxEmptyString
)
587 cfg
->SetPath(oldpath
);
592 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
597 if (path
!= wxEmptyString
)
599 oldpath
= cfg
->GetPath();
603 cfg
->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders
);
604 cfg
->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
605 cfg
->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
606 for (int i
= 0; i
< 7; i
++)
608 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
609 cfg
->Write(tmp
, (long) m_Parser
->m_FontsSizes
[i
]);
612 if (path
!= wxEmptyString
)
613 cfg
->SetPath(oldpath
);
618 bool wxHtmlWindow::HistoryBack()
622 if (m_HistoryPos
< 1) return false;
624 // store scroll position into history item:
626 GetViewStart(&x
, &y
);
627 (*m_History
)[m_HistoryPos
].SetPos(y
);
629 // go to previous position:
632 l
= (*m_History
)[m_HistoryPos
].GetPage();
633 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
636 if (a
== wxEmptyString
) LoadPage(l
);
637 else LoadPage(l
+ wxT("#") + a
);
640 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
645 bool wxHtmlWindow::HistoryCanBack()
647 if (m_HistoryPos
< 1) return false;
652 bool wxHtmlWindow::HistoryForward()
656 if (m_HistoryPos
== -1) return false;
657 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return false;
659 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
662 l
= (*m_History
)[m_HistoryPos
].GetPage();
663 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
666 if (a
== wxEmptyString
) LoadPage(l
);
667 else LoadPage(l
+ wxT("#") + a
);
670 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
675 bool wxHtmlWindow::HistoryCanForward()
677 if (m_HistoryPos
== -1) return false;
678 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return false;
683 void wxHtmlWindow::HistoryClear()
689 void wxHtmlWindow::AddProcessor(wxHtmlProcessor
*processor
)
693 m_Processors
= new wxHtmlProcessorList
;
695 wxHtmlProcessorList::compatibility_iterator node
;
697 for (node
= m_Processors
->GetFirst(); node
; node
= node
->GetNext())
699 if (processor
->GetPriority() > node
->GetData()->GetPriority())
701 m_Processors
->Insert(node
, processor
);
705 m_Processors
->Append(processor
);
708 /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor
*processor
)
710 if (!m_GlobalProcessors
)
712 m_GlobalProcessors
= new wxHtmlProcessorList
;
714 wxHtmlProcessorList::compatibility_iterator node
;
716 for (node
= m_GlobalProcessors
->GetFirst(); node
; node
= node
->GetNext())
718 if (processor
->GetPriority() > node
->GetData()->GetPriority())
720 m_GlobalProcessors
->Insert(node
, processor
);
724 m_GlobalProcessors
->Append(processor
);
729 wxList
wxHtmlWindow::m_Filters
;
730 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
731 wxHtmlProcessorList
*wxHtmlWindow::m_GlobalProcessors
= NULL
;
733 void wxHtmlWindow::CleanUpStatics()
735 wxDELETE(m_DefaultFilter
);
736 WX_CLEAR_LIST(wxList
, m_Filters
);
737 if (m_GlobalProcessors
)
738 WX_CLEAR_LIST(wxHtmlProcessorList
, *m_GlobalProcessors
);
739 wxDELETE(m_GlobalProcessors
);
744 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
746 m_Filters
.Append(filter
);
750 bool wxHtmlWindow::IsSelectionEnabled() const
753 return !(m_Style
& wxHW_NO_SELECTION
);
761 wxString
wxHtmlWindow::DoSelectionToText(wxHtmlSelection
*sel
)
764 return wxEmptyString
;
768 const wxHtmlCell
*end
= sel
->GetToCell();
770 wxHtmlTerminalCellsInterator
i(sel
->GetFromCell(), end
);
773 text
<< i
->ConvertToText(sel
);
776 const wxHtmlCell
*prev
= *i
;
779 if ( prev
->GetParent() != i
->GetParent() )
781 text
<< i
->ConvertToText(*i
== end
? sel
: NULL
);
788 wxString
wxHtmlWindow::ToText()
793 sel
.Set(m_Cell
->GetFirstTerminal(), m_Cell
->GetLastTerminal());
794 return DoSelectionToText(&sel
);
797 return wxEmptyString
;
800 #endif // wxUSE_CLIPBOARD
802 bool wxHtmlWindow::CopySelection(ClipboardType t
)
807 #if defined(__UNIX__) && !defined(__WXMAC__)
808 wxTheClipboard
->UsePrimarySelection(t
== Primary
);
810 // Primary selection exists only under X11, so don't do anything under
811 // the other platforms when we try to access it
813 // TODO: this should be abstracted at wxClipboard level!
816 #endif // __UNIX__/!__UNIX__
818 if ( wxTheClipboard
->Open() )
820 const wxString
txt(SelectionToText());
821 wxTheClipboard
->SetData(new wxTextDataObject(txt
));
822 wxTheClipboard
->Close();
823 wxLogTrace(_T("wxhtmlselection"),
824 _("Copied to clipboard:\"%s\""), txt
.c_str());
829 #endif // wxUSE_CLIPBOARD
835 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
837 const wxMouseEvent
*e
= link
.GetEvent();
838 if (e
== NULL
|| e
->LeftUp())
839 LoadPage(link
.GetHref());
842 void wxHtmlWindow::OnCellClicked(wxHtmlCell
*cell
,
843 wxCoord x
, wxCoord y
,
844 const wxMouseEvent
& event
)
846 wxCHECK_RET( cell
, _T("can't be called with NULL cell") );
848 cell
->OnMouseClick(this, x
, y
, event
);
851 void wxHtmlWindow::OnCellMouseHover(wxHtmlCell
* WXUNUSED(cell
),
852 wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
857 void wxHtmlWindow::OnEraseBackground(wxEraseEvent
& event
)
861 // don't even skip the event, if we don't have a bg bitmap we're going
862 // to overwrite background in OnPaint() below anyhow, so letting the
863 // default handling take place would only result in flicker, just set a
864 // flag to erase the background below
865 m_eraseBgInOnPaint
= true;
869 wxDC
& dc
= *event
.GetDC();
871 // if the image is not fully opaque, we have to erase the background before
872 // drawing it, however avoid doing it for opaque images as this would just
873 // result in extra flicker without any other effect as background is
874 // completely covered anyhow
875 if ( m_bmpBg
.GetMask() )
877 dc
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
881 const wxSize
sizeWin(GetClientSize());
882 const wxSize
sizeBmp(m_bmpBg
.GetWidth(), m_bmpBg
.GetHeight());
883 for ( wxCoord x
= 0; x
< sizeWin
.x
; x
+= sizeBmp
.x
)
885 for ( wxCoord y
= 0; y
< sizeWin
.y
; y
+= sizeBmp
.y
)
887 dc
.DrawBitmap(m_bmpBg
, x
, y
, true /* use mask */);
892 void wxHtmlWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
896 if (m_tmpCanDrawLocks
> 0 || m_Cell
== NULL
)
900 GetViewStart(&x
, &y
);
901 wxRect rect
= GetUpdateRegion().GetBox();
902 wxSize sz
= GetSize();
906 m_backBuffer
= new wxBitmap(sz
.x
, sz
.y
);
907 dcm
.SelectObject(*m_backBuffer
);
909 if ( m_eraseBgInOnPaint
)
911 dcm
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
914 m_eraseBgInOnPaint
= false;
916 else // someone has already erased the background, keep it
918 // preserve the existing background, otherwise we'd erase anything the
919 // user code had drawn in its EVT_ERASE_BACKGROUND handler when we do
920 // the Blit back below
921 dcm
.Blit(0, rect
.GetTop(),
922 sz
.x
, rect
.GetBottom() - rect
.GetTop() + 1,
928 dcm
.SetMapMode(wxMM_TEXT
);
929 dcm
.SetBackgroundMode(wxTRANSPARENT
);
931 wxHtmlRenderingInfo rinfo
;
932 wxDefaultHtmlRenderingStyle rstyle
;
933 rinfo
.SetSelection(m_selection
);
934 rinfo
.SetStyle(&rstyle
);
935 m_Cell
->Draw(dcm
, 0, 0,
936 y
* wxHTML_SCROLL_STEP
+ rect
.GetTop(),
937 y
* wxHTML_SCROLL_STEP
+ rect
.GetBottom(),
940 //#define DEBUG_HTML_SELECTION
941 #ifdef DEBUG_HTML_SELECTION
944 wxGetMousePosition(&xc
, &yc
);
945 ScreenToClient(&xc
, &yc
);
946 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
947 wxHtmlCell
*at
= m_Cell
->FindCellByPos(x
, y
);
949 m_Cell
->FindCellByPos(x
, y
, wxHTML_FIND_NEAREST_BEFORE
);
951 m_Cell
->FindCellByPos(x
, y
, wxHTML_FIND_NEAREST_AFTER
);
953 dcm
.SetBrush(*wxTRANSPARENT_BRUSH
);
954 dcm
.SetPen(*wxBLACK_PEN
);
956 dcm
.DrawRectangle(at
->GetAbsPos(),
957 wxSize(at
->GetWidth(),at
->GetHeight()));
958 dcm
.SetPen(*wxGREEN_PEN
);
960 dcm
.DrawRectangle(before
->GetAbsPos().x
+1, before
->GetAbsPos().y
+1,
961 before
->GetWidth()-2,before
->GetHeight()-2);
962 dcm
.SetPen(*wxRED_PEN
);
964 dcm
.DrawRectangle(after
->GetAbsPos().x
+2, after
->GetAbsPos().y
+2,
965 after
->GetWidth()-4,after
->GetHeight()-4);
969 dcm
.SetDeviceOrigin(0,0);
970 dc
.Blit(0, rect
.GetTop(),
971 sz
.x
, rect
.GetBottom() - rect
.GetTop() + 1,
979 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
981 wxDELETE(m_backBuffer
);
983 wxScrolledWindow::OnSize(event
);
986 // Recompute selection if necessary:
989 m_selection
->Set(m_selection
->GetFromCell(),
990 m_selection
->GetToCell());
991 m_selection
->ClearPrivPos();
998 void wxHtmlWindow::OnMouseMove(wxMouseEvent
& WXUNUSED(event
))
1000 m_tmpMouseMoved
= true;
1003 void wxHtmlWindow::OnMouseDown(wxMouseEvent
& event
)
1006 if ( event
.LeftDown() && IsSelectionEnabled() )
1008 const long TRIPLECLICK_LEN
= 200; // 0.2 sec after doubleclick
1009 if ( wxGetLocalTimeMillis() - m_lastDoubleClick
<= TRIPLECLICK_LEN
)
1011 SelectLine(CalcUnscrolledPosition(event
.GetPosition()));
1013 (void) CopySelection();
1017 m_makingSelection
= true;
1021 wxDELETE(m_selection
);
1024 m_tmpSelFromPos
= CalcUnscrolledPosition(event
.GetPosition());
1025 m_tmpSelFromCell
= NULL
;
1030 #endif // wxUSE_CLIPBOARD
1033 void wxHtmlWindow::OnMouseUp(wxMouseEvent
& event
)
1036 if ( m_makingSelection
)
1039 m_makingSelection
= false;
1041 // did the user move the mouse far enough from starting point?
1042 if ( CopySelection(Primary
) )
1044 // we don't want mouse up event that ended selecting to be
1045 // handled as mouse click and e.g. follow hyperlink:
1049 #endif // wxUSE_CLIPBOARD
1054 wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
1055 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1057 // check is needed because FindCellByPos returns terminal cell and
1058 // containers may have empty borders -- in this case NULL will be
1061 OnCellClicked(cell
, pos
.x
, pos
.y
, event
);
1067 void wxHtmlWindow::OnInternalIdle()
1069 wxWindow::OnInternalIdle();
1071 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
))
1073 #ifdef DEBUG_HTML_SELECTION
1077 wxGetMousePosition(&xc
, &yc
);
1078 ScreenToClient(&xc
, &yc
);
1079 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
1081 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(x
, y
);
1083 // handle selection update:
1084 if ( m_makingSelection
)
1086 if ( !m_tmpSelFromCell
)
1087 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1088 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
);
1090 // NB: a trick - we adjust selFromPos to be upper left or bottom
1091 // right corner of the first cell of the selection depending
1092 // on whether the mouse is moving to the right or to the left.
1093 // This gives us more "natural" behaviour when selecting
1094 // a line (specifically, first cell of the next line is not
1095 // included if you drag selection from left to right over
1098 if ( !m_tmpSelFromCell
)
1100 dirFromPos
= m_tmpSelFromPos
;
1104 dirFromPos
= m_tmpSelFromCell
->GetAbsPos();
1105 if ( x
< m_tmpSelFromPos
.x
)
1107 dirFromPos
.x
+= m_tmpSelFromCell
->GetWidth();
1108 dirFromPos
.y
+= m_tmpSelFromCell
->GetHeight();
1111 bool goingDown
= dirFromPos
.y
< y
||
1112 (dirFromPos
.y
== y
&& dirFromPos
.x
< x
);
1114 // determine selection span:
1115 if ( /*still*/ !m_tmpSelFromCell
)
1119 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1120 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1121 wxHTML_FIND_NEAREST_AFTER
);
1122 if (!m_tmpSelFromCell
)
1123 m_tmpSelFromCell
= m_Cell
->GetFirstTerminal();
1127 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1128 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1129 wxHTML_FIND_NEAREST_BEFORE
);
1130 if (!m_tmpSelFromCell
)
1131 m_tmpSelFromCell
= m_Cell
->GetLastTerminal();
1135 wxHtmlCell
*selcell
= cell
;
1140 selcell
= m_Cell
->FindCellByPos(x
, y
,
1141 wxHTML_FIND_NEAREST_BEFORE
);
1143 selcell
= m_Cell
->GetLastTerminal();
1147 selcell
= m_Cell
->FindCellByPos(x
, y
,
1148 wxHTML_FIND_NEAREST_AFTER
);
1150 selcell
= m_Cell
->GetFirstTerminal();
1154 // NB: it may *rarely* happen that the code above didn't find one
1155 // of the cells, e.g. if wxHtmlWindow doesn't contain any
1157 if ( selcell
&& m_tmpSelFromCell
)
1161 // start selecting only if mouse movement was big enough
1162 // (otherwise it was meant as mouse click, not selection):
1163 const int PRECISION
= 2;
1164 wxPoint diff
= m_tmpSelFromPos
- wxPoint(x
,y
);
1165 if (abs(diff
.x
) > PRECISION
|| abs(diff
.y
) > PRECISION
)
1167 m_selection
= new wxHtmlSelection();
1172 if ( m_tmpSelFromCell
->IsBefore(selcell
) )
1174 m_selection
->Set(m_tmpSelFromPos
, m_tmpSelFromCell
,
1175 wxPoint(x
,y
), selcell
); }
1178 m_selection
->Set(wxPoint(x
,y
), selcell
,
1179 m_tmpSelFromPos
, m_tmpSelFromCell
);
1181 m_selection
->ClearPrivPos();
1187 // handle cursor and status bar text changes:
1188 if ( cell
!= m_tmpLastCell
)
1190 wxHtmlLinkInfo
*lnk
= cell
? cell
->GetLink(x
, y
) : NULL
;
1193 cur
= cell
->GetCursor();
1195 cur
= *wxSTANDARD_CURSOR
;
1198 if (lnk
!= m_tmpLastLink
)
1203 if (m_RelatedStatusBar
!= -1)
1204 m_RelatedFrame
->SetStatusText(wxEmptyString
,
1205 m_RelatedStatusBar
);
1209 if (m_RelatedStatusBar
!= -1)
1210 m_RelatedFrame
->SetStatusText(lnk
->GetHref(),
1211 m_RelatedStatusBar
);
1213 #endif // wxUSE_STATUSBAR
1214 m_tmpLastLink
= lnk
;
1217 m_tmpLastCell
= cell
;
1219 else // mouse moved but stayed in the same cell
1222 OnCellMouseHover(cell
, x
, y
);
1225 m_tmpMouseMoved
= false;
1230 void wxHtmlWindow::StopAutoScrolling()
1232 if ( m_timerAutoScroll
)
1234 wxDELETE(m_timerAutoScroll
);
1238 void wxHtmlWindow::OnMouseEnter(wxMouseEvent
& event
)
1240 StopAutoScrolling();
1244 void wxHtmlWindow::OnMouseLeave(wxMouseEvent
& event
)
1246 // don't prevent the usual processing of the event from taking place
1249 // when a captured mouse leave a scrolled window we start generate
1250 // scrolling events to allow, for example, extending selection beyond the
1251 // visible area in some controls
1252 if ( wxWindow::GetCapture() == this )
1254 // where is the mouse leaving?
1256 wxPoint pt
= event
.GetPosition();
1259 orient
= wxHORIZONTAL
;
1262 else if ( pt
.y
< 0 )
1264 orient
= wxVERTICAL
;
1267 else // we're lower or to the right of the window
1269 wxSize size
= GetClientSize();
1270 if ( pt
.x
> size
.x
)
1272 orient
= wxHORIZONTAL
;
1273 pos
= GetVirtualSize().x
/ wxHTML_SCROLL_STEP
;
1275 else if ( pt
.y
> size
.y
)
1277 orient
= wxVERTICAL
;
1278 pos
= GetVirtualSize().y
/ wxHTML_SCROLL_STEP
;
1280 else // this should be impossible
1282 // but seems to happen sometimes under wxMSW - maybe it's a bug
1283 // there but for now just ignore it
1285 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1291 // only start the auto scroll timer if the window can be scrolled in
1293 if ( !HasScrollbar(orient
) )
1296 delete m_timerAutoScroll
;
1297 m_timerAutoScroll
= new wxHtmlWinAutoScrollTimer
1300 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1301 : wxEVT_SCROLLWIN_LINEDOWN
,
1305 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1309 void wxHtmlWindow::OnKeyUp(wxKeyEvent
& event
)
1311 if ( IsSelectionEnabled() &&
1312 event
.GetKeyCode() == 'C' && event
.ControlDown() )
1314 (void) CopySelection();
1318 void wxHtmlWindow::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1320 (void) CopySelection();
1323 void wxHtmlWindow::OnDoubleClick(wxMouseEvent
& event
)
1325 // select word under cursor:
1326 if ( IsSelectionEnabled() )
1328 SelectWord(CalcUnscrolledPosition(event
.GetPosition()));
1330 (void) CopySelection(Primary
);
1332 m_lastDoubleClick
= wxGetLocalTimeMillis();
1338 void wxHtmlWindow::SelectWord(const wxPoint
& pos
)
1342 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1346 m_selection
= new wxHtmlSelection();
1347 m_selection
->Set(cell
, cell
);
1348 RefreshRect(wxRect(CalcScrolledPosition(cell
->GetAbsPos()),
1349 wxSize(cell
->GetWidth(), cell
->GetHeight())));
1354 void wxHtmlWindow::SelectLine(const wxPoint
& pos
)
1358 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1361 // We use following heuristic to find a "line": let the line be all
1362 // cells in same container as the cell under mouse cursor that are
1363 // neither completely above nor completely bellow the clicked cell
1364 // (i.e. are likely to be words positioned on same line of text).
1366 int y1
= cell
->GetAbsPos().y
;
1367 int y2
= y1
+ cell
->GetHeight();
1369 const wxHtmlCell
*c
;
1370 const wxHtmlCell
*before
= NULL
;
1371 const wxHtmlCell
*after
= NULL
;
1373 // find last cell of line:
1374 for ( c
= cell
->GetNext(); c
; c
= c
->GetNext())
1376 y
= c
->GetAbsPos().y
;
1377 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1385 // find first cell of line:
1386 for ( c
= cell
->GetParent()->GetFirstChild();
1387 c
&& c
!= cell
; c
= c
->GetNext())
1389 y
= c
->GetAbsPos().y
;
1390 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1402 m_selection
= new wxHtmlSelection();
1403 m_selection
->Set(before
, after
);
1410 void wxHtmlWindow::SelectAll()
1415 m_selection
= new wxHtmlSelection();
1416 m_selection
->Set(m_Cell
->GetFirstTerminal(), m_Cell
->GetLastTerminal());
1421 #endif // wxUSE_CLIPBOARD
1425 IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor
,wxObject
)
1427 #if wxUSE_EXTENDED_RTTI
1428 IMPLEMENT_DYNAMIC_CLASS_XTI(wxHtmlWindow
, wxScrolledWindow
,"wx/html/htmlwin.h")
1430 wxBEGIN_PROPERTIES_TABLE(wxHtmlWindow
)
1433 style , wxHW_SCROLLBAR_AUTO
1434 borders , (dimension)
1438 wxEND_PROPERTIES_TABLE()
1440 wxBEGIN_HANDLERS_TABLE(wxHtmlWindow
)
1441 wxEND_HANDLERS_TABLE()
1443 wxCONSTRUCTOR_5( wxHtmlWindow
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
1445 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
1448 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
1449 EVT_SIZE(wxHtmlWindow::OnSize
)
1450 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseDown
)
1451 EVT_LEFT_UP(wxHtmlWindow::OnMouseUp
)
1452 EVT_RIGHT_UP(wxHtmlWindow::OnMouseUp
)
1453 EVT_MOTION(wxHtmlWindow::OnMouseMove
)
1454 EVT_ERASE_BACKGROUND(wxHtmlWindow::OnEraseBackground
)
1455 EVT_PAINT(wxHtmlWindow::OnPaint
)
1457 EVT_LEFT_DCLICK(wxHtmlWindow::OnDoubleClick
)
1458 EVT_ENTER_WINDOW(wxHtmlWindow::OnMouseEnter
)
1459 EVT_LEAVE_WINDOW(wxHtmlWindow::OnMouseLeave
)
1460 EVT_KEY_UP(wxHtmlWindow::OnKeyUp
)
1461 EVT_MENU(wxID_COPY
, wxHtmlWindow::OnCopy
)
1462 #endif // wxUSE_CLIPBOARD
1469 // A module to allow initialization/cleanup
1470 // without calling these functions from app.cpp or from
1471 // the user's application.
1473 class wxHtmlWinModule
: public wxModule
1475 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
1477 wxHtmlWinModule() : wxModule() {}
1478 bool OnInit() { return true; }
1479 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
1482 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
1485 // This hack forces the linker to always link in m_* files
1486 // (wxHTML doesn't work without handlers from these files)
1487 #include "wx/html/forcelnk.h"
1488 FORCE_WXHTML_MODULES()
1490 #endif // wxUSE_HTML