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(const wxString
& normal_face
, const wxString
& fixed_face
, const int *sizes
)
240 m_Parser
->SetFonts(normal_face
, fixed_face
, sizes
);
242 // re-layout the page after changing fonts:
243 DoSetPage(*(m_Parser
->GetSource()));
246 void wxHtmlWindow::SetStandardFonts(int size
,
247 const wxString
& normal_face
,
248 const wxString
& fixed_face
)
250 m_Parser
->SetStandardFonts(size
, normal_face
, fixed_face
);
252 // re-layout the page after changing fonts:
253 DoSetPage(*(m_Parser
->GetSource()));
256 bool wxHtmlWindow::SetPage(const wxString
& source
)
258 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
259 return DoSetPage(source
);
262 bool wxHtmlWindow::DoSetPage(const wxString
& source
)
264 wxString
newsrc(source
);
266 wxDELETE(m_selection
);
268 // we will soon delete all the cells, so clear pointers to them:
269 m_tmpSelFromCell
= NULL
;
271 // pass HTML through registered processors:
272 if (m_Processors
|| m_GlobalProcessors
)
274 wxHtmlProcessorList::compatibility_iterator nodeL
, nodeG
;
277 nodeL
= (m_Processors
) ? m_Processors
->GetFirst() : wxHtmlProcessorList::compatibility_iterator();
278 nodeG
= (m_GlobalProcessors
) ? m_GlobalProcessors
->GetFirst() : wxHtmlProcessorList::compatibility_iterator();
280 // VS: there are two lists, global and local, both of them sorted by
281 // priority. Since we have to go through _both_ lists with
282 // decreasing priority, we "merge-sort" the lists on-line by
283 // processing that one of the two heads that has higher priority
284 // in every iteration
285 while (nodeL
|| nodeG
)
287 prL
= (nodeL
) ? nodeL
->GetData()->GetPriority() : -1;
288 prG
= (nodeG
) ? nodeG
->GetData()->GetPriority() : -1;
291 if (nodeL
->GetData()->IsEnabled())
292 newsrc
= nodeL
->GetData()->Process(newsrc
);
293 nodeL
= nodeL
->GetNext();
297 if (nodeG
->GetData()->IsEnabled())
298 newsrc
= nodeG
->GetData()->Process(newsrc
);
299 nodeG
= nodeG
->GetNext();
304 // ...and run the parser on it:
305 wxClientDC
*dc
= new wxClientDC(this);
306 dc
->SetMapMode(wxMM_TEXT
);
307 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
308 SetBackgroundImage(wxNullBitmap
);
316 m_Cell
= (wxHtmlContainerCell
*) m_Parser
->Parse(newsrc
);
318 m_Cell
->SetIndent(m_Borders
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
319 m_Cell
->SetAlignHor(wxHTML_ALIGN_CENTER
);
321 if (m_tmpCanDrawLocks
== 0)
326 bool wxHtmlWindow::AppendToPage(const wxString
& source
)
328 return DoSetPage(*(GetParser()->GetSource()) + source
);
331 bool wxHtmlWindow::LoadPage(const wxString
& location
)
333 wxBusyCursor busyCursor
;
337 bool needs_refresh
= false;
340 if (m_HistoryOn
&& (m_HistoryPos
!= -1))
342 // store scroll position into history item:
344 GetViewStart(&x
, &y
);
345 (*m_History
)[m_HistoryPos
].SetPos(y
);
348 if (location
[0] == wxT('#'))
351 wxString anch
= location
.Mid(1) /*1 to end*/;
353 rt_val
= ScrollToAnchor(anch
);
356 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&& location
.BeforeFirst(wxT('#')) == m_OpenedPage
)
358 wxString anch
= location
.AfterFirst(wxT('#'));
360 rt_val
= ScrollToAnchor(anch
);
363 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&&
364 (m_FS
->GetPath() + location
.BeforeFirst(wxT('#'))) == m_OpenedPage
)
366 wxString anch
= location
.AfterFirst(wxT('#'));
368 rt_val
= ScrollToAnchor(anch
);
374 needs_refresh
= true;
377 if (m_RelatedStatusBar
!= -1)
379 m_RelatedFrame
->SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
382 #endif // wxUSE_STATUSBAR
384 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location
);
386 // try to interpret 'location' as filename instead of URL:
389 wxFileName
fn(location
);
390 wxString location2
= wxFileSystem::FileNameToURL(fn
);
391 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location2
);
396 wxLogError(_("Unable to open requested HTML document: %s"), location
.c_str());
403 wxList::compatibility_iterator node
;
404 wxString src
= wxEmptyString
;
407 if (m_RelatedStatusBar
!= -1)
409 wxString msg
= _("Loading : ") + location
;
410 m_RelatedFrame
->SetStatusText(msg
, m_RelatedStatusBar
);
413 #endif // wxUSE_STATUSBAR
415 node
= m_Filters
.GetFirst();
418 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
421 src
= h
->ReadFile(*f
);
424 node
= node
->GetNext();
426 if (src
== wxEmptyString
)
428 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
429 src
= m_DefaultFilter
->ReadFile(*f
);
432 m_FS
->ChangePathTo(f
->GetLocation());
433 rt_val
= SetPage(src
);
434 m_OpenedPage
= f
->GetLocation();
435 if (f
->GetAnchor() != wxEmptyString
)
437 ScrollToAnchor(f
->GetAnchor());
443 if (m_RelatedStatusBar
!= -1)
444 m_RelatedFrame
->SetStatusText(_("Done"), m_RelatedStatusBar
);
445 #endif // wxUSE_STATUSBAR
449 if (m_HistoryOn
) // add this page to history there:
451 int c
= m_History
->GetCount() - (m_HistoryPos
+ 1);
453 if (m_HistoryPos
< 0 ||
454 (*m_History
)[m_HistoryPos
].GetPage() != m_OpenedPage
||
455 (*m_History
)[m_HistoryPos
].GetAnchor() != m_OpenedAnchor
)
458 for (int i
= 0; i
< c
; i
++)
459 m_History
->RemoveAt(m_HistoryPos
);
460 m_History
->Add(new wxHtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
464 if (m_OpenedPageTitle
== wxEmptyString
)
465 OnSetTitle(wxFileNameFromPath(m_OpenedPage
));
479 bool wxHtmlWindow::LoadFile(const wxFileName
& filename
)
481 wxString url
= wxFileSystem::FileNameToURL(filename
);
482 return LoadPage(url
);
486 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
488 const wxHtmlCell
*c
= m_Cell
->Find(wxHTML_COND_ISANCHOR
, &anchor
);
491 wxLogWarning(_("HTML anchor %s does not exist."), anchor
.c_str());
498 for (y
= 0; c
!= NULL
; c
= c
->GetParent()) y
+= c
->GetPosY();
499 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
500 m_OpenedAnchor
= anchor
;
506 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
511 tit
.Printf(m_TitleFormat
, title
.c_str());
512 m_RelatedFrame
->SetTitle(tit
);
514 m_OpenedPageTitle
= title
;
521 void wxHtmlWindow::CreateLayout()
523 int ClientWidth
, ClientHeight
;
527 if (m_Style
& wxHW_SCROLLBAR_NEVER
)
529 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // always off
530 GetClientSize(&ClientWidth
, &ClientHeight
);
531 m_Cell
->Layout(ClientWidth
);
535 GetClientSize(&ClientWidth
, &ClientHeight
);
536 m_Cell
->Layout(ClientWidth
);
537 if (ClientHeight
< m_Cell
->GetHeight() + GetCharHeight())
540 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
541 m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
,
542 (m_Cell
->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
543 /*cheat: top-level frag is always container*/);
545 else /* we fit into window, no need for scrollbars */
547 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
548 GetClientSize(&ClientWidth
, &ClientHeight
);
549 m_Cell
->Layout(ClientWidth
); // ...and relayout
556 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
561 wxString p_fff
, p_ffn
;
563 if (path
!= wxEmptyString
)
565 oldpath
= cfg
->GetPath();
569 m_Borders
= cfg
->Read(wxT("wxHtmlWindow/Borders"), m_Borders
);
570 p_fff
= cfg
->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
571 p_ffn
= cfg
->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
572 for (int i
= 0; i
< 7; i
++)
574 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
575 p_fontsizes
[i
] = cfg
->Read(tmp
, m_Parser
->m_FontsSizes
[i
]);
577 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
579 if (path
!= wxEmptyString
)
580 cfg
->SetPath(oldpath
);
585 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
590 if (path
!= wxEmptyString
)
592 oldpath
= cfg
->GetPath();
596 cfg
->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders
);
597 cfg
->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
598 cfg
->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
599 for (int i
= 0; i
< 7; i
++)
601 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
602 cfg
->Write(tmp
, (long) m_Parser
->m_FontsSizes
[i
]);
605 if (path
!= wxEmptyString
)
606 cfg
->SetPath(oldpath
);
611 bool wxHtmlWindow::HistoryBack()
615 if (m_HistoryPos
< 1) return false;
617 // store scroll position into history item:
619 GetViewStart(&x
, &y
);
620 (*m_History
)[m_HistoryPos
].SetPos(y
);
622 // go to previous position:
625 l
= (*m_History
)[m_HistoryPos
].GetPage();
626 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
629 if (a
== wxEmptyString
) LoadPage(l
);
630 else LoadPage(l
+ wxT("#") + a
);
633 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
638 bool wxHtmlWindow::HistoryCanBack()
640 if (m_HistoryPos
< 1) return false;
645 bool wxHtmlWindow::HistoryForward()
649 if (m_HistoryPos
== -1) return false;
650 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return false;
652 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
655 l
= (*m_History
)[m_HistoryPos
].GetPage();
656 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
659 if (a
== wxEmptyString
) LoadPage(l
);
660 else LoadPage(l
+ wxT("#") + a
);
663 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
668 bool wxHtmlWindow::HistoryCanForward()
670 if (m_HistoryPos
== -1) return false;
671 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return false;
676 void wxHtmlWindow::HistoryClear()
682 void wxHtmlWindow::AddProcessor(wxHtmlProcessor
*processor
)
686 m_Processors
= new wxHtmlProcessorList
;
688 wxHtmlProcessorList::compatibility_iterator node
;
690 for (node
= m_Processors
->GetFirst(); node
; node
= node
->GetNext())
692 if (processor
->GetPriority() > node
->GetData()->GetPriority())
694 m_Processors
->Insert(node
, processor
);
698 m_Processors
->Append(processor
);
701 /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor
*processor
)
703 if (!m_GlobalProcessors
)
705 m_GlobalProcessors
= new wxHtmlProcessorList
;
707 wxHtmlProcessorList::compatibility_iterator node
;
709 for (node
= m_GlobalProcessors
->GetFirst(); node
; node
= node
->GetNext())
711 if (processor
->GetPriority() > node
->GetData()->GetPriority())
713 m_GlobalProcessors
->Insert(node
, processor
);
717 m_GlobalProcessors
->Append(processor
);
722 wxList
wxHtmlWindow::m_Filters
;
723 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
724 wxHtmlProcessorList
*wxHtmlWindow::m_GlobalProcessors
= NULL
;
726 void wxHtmlWindow::CleanUpStatics()
728 wxDELETE(m_DefaultFilter
);
729 WX_CLEAR_LIST(wxList
, m_Filters
);
730 if (m_GlobalProcessors
)
731 WX_CLEAR_LIST(wxHtmlProcessorList
, *m_GlobalProcessors
);
732 wxDELETE(m_GlobalProcessors
);
737 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
739 m_Filters
.Append(filter
);
743 bool wxHtmlWindow::IsSelectionEnabled() const
746 return !(m_Style
& wxHW_NO_SELECTION
);
754 wxString
wxHtmlWindow::DoSelectionToText(wxHtmlSelection
*sel
)
757 return wxEmptyString
;
761 const wxHtmlCell
*end
= sel
->GetToCell();
763 wxHtmlTerminalCellsInterator
i(sel
->GetFromCell(), end
);
766 text
<< i
->ConvertToText(sel
);
769 const wxHtmlCell
*prev
= *i
;
772 if ( prev
->GetParent() != i
->GetParent() )
774 text
<< i
->ConvertToText(*i
== end
? sel
: NULL
);
781 wxString
wxHtmlWindow::ToText()
786 sel
.Set(m_Cell
->GetFirstTerminal(), m_Cell
->GetLastTerminal());
787 return DoSelectionToText(&sel
);
790 return wxEmptyString
;
793 #endif // wxUSE_CLIPBOARD
795 bool wxHtmlWindow::CopySelection(ClipboardType t
)
800 #if defined(__UNIX__) && !defined(__WXMAC__)
801 wxTheClipboard
->UsePrimarySelection(t
== Primary
);
803 // Primary selection exists only under X11, so don't do anything under
804 // the other platforms when we try to access it
806 // TODO: this should be abstracted at wxClipboard level!
809 #endif // __UNIX__/!__UNIX__
811 if ( wxTheClipboard
->Open() )
813 const wxString
txt(SelectionToText());
814 wxTheClipboard
->SetData(new wxTextDataObject(txt
));
815 wxTheClipboard
->Close();
816 wxLogTrace(_T("wxhtmlselection"),
817 _("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 // don't even skip the event, if we don't have a bg bitmap we're going
857 // to overwrite background in OnPaint() below anyhow, so letting the
858 // default handling take place would only result in flicker, just set a
859 // flag to erase the background below
860 m_eraseBgInOnPaint
= true;
864 wxDC
& dc
= *event
.GetDC();
866 // if the image is not fully opaque, we have to erase the background before
867 // drawing it, however avoid doing it for opaque images as this would just
868 // result in extra flicker without any other effect as background is
869 // completely covered anyhow
870 if ( m_bmpBg
.GetMask() )
872 dc
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
876 const wxSize
sizeWin(GetClientSize());
877 const wxSize
sizeBmp(m_bmpBg
.GetWidth(), m_bmpBg
.GetHeight());
878 for ( wxCoord x
= 0; x
< sizeWin
.x
; x
+= sizeBmp
.x
)
880 for ( wxCoord y
= 0; y
< sizeWin
.y
; y
+= sizeBmp
.y
)
882 dc
.DrawBitmap(m_bmpBg
, x
, y
, true /* use mask */);
887 void wxHtmlWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
891 if (m_tmpCanDrawLocks
> 0 || m_Cell
== NULL
)
895 GetViewStart(&x
, &y
);
896 wxRect rect
= GetUpdateRegion().GetBox();
897 wxSize sz
= GetSize();
901 m_backBuffer
= new wxBitmap(sz
.x
, sz
.y
);
902 dcm
.SelectObject(*m_backBuffer
);
904 if ( m_eraseBgInOnPaint
)
906 dcm
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
909 m_eraseBgInOnPaint
= false;
911 else // someone has already erased the background, keep it
913 // preserve the existing background, otherwise we'd erase anything the
914 // user code had drawn in its EVT_ERASE_BACKGROUND handler when we do
915 // the Blit back below
916 dcm
.Blit(0, rect
.GetTop(),
917 sz
.x
, rect
.GetBottom() - rect
.GetTop() + 1,
923 dcm
.SetMapMode(wxMM_TEXT
);
924 dcm
.SetBackgroundMode(wxTRANSPARENT
);
926 wxHtmlRenderingInfo rinfo
;
927 wxDefaultHtmlRenderingStyle rstyle
;
928 rinfo
.SetSelection(m_selection
);
929 rinfo
.SetStyle(&rstyle
);
930 m_Cell
->Draw(dcm
, 0, 0,
931 y
* wxHTML_SCROLL_STEP
+ rect
.GetTop(),
932 y
* wxHTML_SCROLL_STEP
+ rect
.GetBottom(),
935 //#define DEBUG_HTML_SELECTION
936 #ifdef DEBUG_HTML_SELECTION
939 wxGetMousePosition(&xc
, &yc
);
940 ScreenToClient(&xc
, &yc
);
941 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
942 wxHtmlCell
*at
= m_Cell
->FindCellByPos(x
, y
);
944 m_Cell
->FindCellByPos(x
, y
, wxHTML_FIND_NEAREST_BEFORE
);
946 m_Cell
->FindCellByPos(x
, y
, wxHTML_FIND_NEAREST_AFTER
);
948 dcm
.SetBrush(*wxTRANSPARENT_BRUSH
);
949 dcm
.SetPen(*wxBLACK_PEN
);
951 dcm
.DrawRectangle(at
->GetAbsPos(),
952 wxSize(at
->GetWidth(),at
->GetHeight()));
953 dcm
.SetPen(*wxGREEN_PEN
);
955 dcm
.DrawRectangle(before
->GetAbsPos().x
+1, before
->GetAbsPos().y
+1,
956 before
->GetWidth()-2,before
->GetHeight()-2);
957 dcm
.SetPen(*wxRED_PEN
);
959 dcm
.DrawRectangle(after
->GetAbsPos().x
+2, after
->GetAbsPos().y
+2,
960 after
->GetWidth()-4,after
->GetHeight()-4);
964 dcm
.SetDeviceOrigin(0,0);
965 dc
.Blit(0, rect
.GetTop(),
966 sz
.x
, rect
.GetBottom() - rect
.GetTop() + 1,
974 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
976 wxDELETE(m_backBuffer
);
978 wxScrolledWindow::OnSize(event
);
981 // Recompute selection if necessary:
984 m_selection
->Set(m_selection
->GetFromCell(),
985 m_selection
->GetToCell());
986 m_selection
->ClearPrivPos();
993 void wxHtmlWindow::OnMouseMove(wxMouseEvent
& WXUNUSED(event
))
995 m_tmpMouseMoved
= true;
998 void wxHtmlWindow::OnMouseDown(wxMouseEvent
& event
)
1001 if ( event
.LeftDown() && IsSelectionEnabled() )
1003 const long TRIPLECLICK_LEN
= 200; // 0.2 sec after doubleclick
1004 if ( wxGetLocalTimeMillis() - m_lastDoubleClick
<= TRIPLECLICK_LEN
)
1006 SelectLine(CalcUnscrolledPosition(event
.GetPosition()));
1008 (void) CopySelection();
1012 m_makingSelection
= true;
1016 wxDELETE(m_selection
);
1019 m_tmpSelFromPos
= CalcUnscrolledPosition(event
.GetPosition());
1020 m_tmpSelFromCell
= NULL
;
1027 #endif // wxUSE_CLIPBOARD
1030 void wxHtmlWindow::OnMouseUp(wxMouseEvent
& event
)
1033 if ( m_makingSelection
)
1036 m_makingSelection
= false;
1038 // did the user move the mouse far enough from starting point?
1039 if ( CopySelection(Primary
) )
1041 // we don't want mouse up event that ended selecting to be
1042 // handled as mouse click and e.g. follow hyperlink:
1046 #endif // wxUSE_CLIPBOARD
1051 wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
1052 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1054 // check is needed because FindCellByPos returns terminal cell and
1055 // containers may have empty borders -- in this case NULL will be
1058 OnCellClicked(cell
, pos
.x
, pos
.y
, event
);
1064 void wxHtmlWindow::OnInternalIdle()
1066 wxWindow::OnInternalIdle();
1068 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
))
1070 #ifdef DEBUG_HTML_SELECTION
1074 wxGetMousePosition(&xc
, &yc
);
1075 ScreenToClient(&xc
, &yc
);
1076 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
1078 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(x
, y
);
1080 // handle selection update:
1081 if ( m_makingSelection
)
1083 if ( !m_tmpSelFromCell
)
1084 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1085 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
);
1087 // NB: a trick - we adjust selFromPos to be upper left or bottom
1088 // right corner of the first cell of the selection depending
1089 // on whether the mouse is moving to the right or to the left.
1090 // This gives us more "natural" behaviour when selecting
1091 // a line (specifically, first cell of the next line is not
1092 // included if you drag selection from left to right over
1095 if ( !m_tmpSelFromCell
)
1097 dirFromPos
= m_tmpSelFromPos
;
1101 dirFromPos
= m_tmpSelFromCell
->GetAbsPos();
1102 if ( x
< m_tmpSelFromPos
.x
)
1104 dirFromPos
.x
+= m_tmpSelFromCell
->GetWidth();
1105 dirFromPos
.y
+= m_tmpSelFromCell
->GetHeight();
1108 bool goingDown
= dirFromPos
.y
< y
||
1109 (dirFromPos
.y
== y
&& dirFromPos
.x
< x
);
1111 // determine selection span:
1112 if ( /*still*/ !m_tmpSelFromCell
)
1116 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1117 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1118 wxHTML_FIND_NEAREST_AFTER
);
1119 if (!m_tmpSelFromCell
)
1120 m_tmpSelFromCell
= m_Cell
->GetFirstTerminal();
1124 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1125 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1126 wxHTML_FIND_NEAREST_BEFORE
);
1127 if (!m_tmpSelFromCell
)
1128 m_tmpSelFromCell
= m_Cell
->GetLastTerminal();
1132 wxHtmlCell
*selcell
= cell
;
1137 selcell
= m_Cell
->FindCellByPos(x
, y
,
1138 wxHTML_FIND_NEAREST_BEFORE
);
1140 selcell
= m_Cell
->GetLastTerminal();
1144 selcell
= m_Cell
->FindCellByPos(x
, y
,
1145 wxHTML_FIND_NEAREST_AFTER
);
1147 selcell
= m_Cell
->GetFirstTerminal();
1151 // NB: it may *rarely* happen that the code above didn't find one
1152 // of the cells, e.g. if wxHtmlWindow doesn't contain any
1154 if ( selcell
&& m_tmpSelFromCell
)
1158 // start selecting only if mouse movement was big enough
1159 // (otherwise it was meant as mouse click, not selection):
1160 const int PRECISION
= 2;
1161 wxPoint diff
= m_tmpSelFromPos
- wxPoint(x
,y
);
1162 if (abs(diff
.x
) > PRECISION
|| abs(diff
.y
) > PRECISION
)
1164 m_selection
= new wxHtmlSelection();
1169 if ( m_tmpSelFromCell
->IsBefore(selcell
) )
1171 m_selection
->Set(m_tmpSelFromPos
, m_tmpSelFromCell
,
1172 wxPoint(x
,y
), selcell
); }
1175 m_selection
->Set(wxPoint(x
,y
), selcell
,
1176 m_tmpSelFromPos
, m_tmpSelFromCell
);
1178 m_selection
->ClearPrivPos();
1184 // handle cursor and status bar text changes:
1185 if ( cell
!= m_tmpLastCell
)
1187 wxHtmlLinkInfo
*lnk
= cell
? cell
->GetLink(x
, y
) : NULL
;
1190 cur
= cell
->GetCursor();
1192 cur
= *wxSTANDARD_CURSOR
;
1195 if (lnk
!= m_tmpLastLink
)
1200 if (m_RelatedStatusBar
!= -1)
1201 m_RelatedFrame
->SetStatusText(wxEmptyString
,
1202 m_RelatedStatusBar
);
1206 if (m_RelatedStatusBar
!= -1)
1207 m_RelatedFrame
->SetStatusText(lnk
->GetHref(),
1208 m_RelatedStatusBar
);
1210 #endif // wxUSE_STATUSBAR
1211 m_tmpLastLink
= lnk
;
1214 m_tmpLastCell
= cell
;
1216 else // mouse moved but stayed in the same cell
1219 OnCellMouseHover(cell
, x
, y
);
1222 m_tmpMouseMoved
= false;
1227 void wxHtmlWindow::StopAutoScrolling()
1229 if ( m_timerAutoScroll
)
1231 wxDELETE(m_timerAutoScroll
);
1235 void wxHtmlWindow::OnMouseEnter(wxMouseEvent
& event
)
1237 StopAutoScrolling();
1241 void wxHtmlWindow::OnMouseLeave(wxMouseEvent
& event
)
1243 // don't prevent the usual processing of the event from taking place
1246 // when a captured mouse leave a scrolled window we start generate
1247 // scrolling events to allow, for example, extending selection beyond the
1248 // visible area in some controls
1249 if ( wxWindow::GetCapture() == this )
1251 // where is the mouse leaving?
1253 wxPoint pt
= event
.GetPosition();
1256 orient
= wxHORIZONTAL
;
1259 else if ( pt
.y
< 0 )
1261 orient
= wxVERTICAL
;
1264 else // we're lower or to the right of the window
1266 wxSize size
= GetClientSize();
1267 if ( pt
.x
> size
.x
)
1269 orient
= wxHORIZONTAL
;
1270 pos
= GetVirtualSize().x
/ wxHTML_SCROLL_STEP
;
1272 else if ( pt
.y
> size
.y
)
1274 orient
= wxVERTICAL
;
1275 pos
= GetVirtualSize().y
/ wxHTML_SCROLL_STEP
;
1277 else // this should be impossible
1279 // but seems to happen sometimes under wxMSW - maybe it's a bug
1280 // there but for now just ignore it
1282 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1288 // only start the auto scroll timer if the window can be scrolled in
1290 if ( !HasScrollbar(orient
) )
1293 delete m_timerAutoScroll
;
1294 m_timerAutoScroll
= new wxHtmlWinAutoScrollTimer
1297 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1298 : wxEVT_SCROLLWIN_LINEDOWN
,
1302 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1306 void wxHtmlWindow::OnKeyUp(wxKeyEvent
& event
)
1308 if ( IsSelectionEnabled() && event
.GetKeyCode() == 'C' && event
.CmdDown() )
1310 (void) CopySelection();
1314 void wxHtmlWindow::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1316 (void) CopySelection();
1319 void wxHtmlWindow::OnDoubleClick(wxMouseEvent
& event
)
1321 // select word under cursor:
1322 if ( IsSelectionEnabled() )
1324 SelectWord(CalcUnscrolledPosition(event
.GetPosition()));
1326 (void) CopySelection(Primary
);
1328 m_lastDoubleClick
= wxGetLocalTimeMillis();
1334 void wxHtmlWindow::SelectWord(const wxPoint
& pos
)
1338 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1342 m_selection
= new wxHtmlSelection();
1343 m_selection
->Set(cell
, cell
);
1344 RefreshRect(wxRect(CalcScrolledPosition(cell
->GetAbsPos()),
1345 wxSize(cell
->GetWidth(), cell
->GetHeight())));
1350 void wxHtmlWindow::SelectLine(const wxPoint
& pos
)
1354 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1357 // We use following heuristic to find a "line": let the line be all
1358 // cells in same container as the cell under mouse cursor that are
1359 // neither completely above nor completely bellow the clicked cell
1360 // (i.e. are likely to be words positioned on same line of text).
1362 int y1
= cell
->GetAbsPos().y
;
1363 int y2
= y1
+ cell
->GetHeight();
1365 const wxHtmlCell
*c
;
1366 const wxHtmlCell
*before
= NULL
;
1367 const wxHtmlCell
*after
= NULL
;
1369 // find last cell of line:
1370 for ( c
= cell
->GetNext(); c
; c
= c
->GetNext())
1372 y
= c
->GetAbsPos().y
;
1373 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1381 // find first cell of line:
1382 for ( c
= cell
->GetParent()->GetFirstChild();
1383 c
&& c
!= cell
; c
= c
->GetNext())
1385 y
= c
->GetAbsPos().y
;
1386 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1398 m_selection
= new wxHtmlSelection();
1399 m_selection
->Set(before
, after
);
1406 void wxHtmlWindow::SelectAll()
1411 m_selection
= new wxHtmlSelection();
1412 m_selection
->Set(m_Cell
->GetFirstTerminal(), m_Cell
->GetLastTerminal());
1417 #endif // wxUSE_CLIPBOARD
1421 IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor
,wxObject
)
1423 #if wxUSE_EXTENDED_RTTI
1424 IMPLEMENT_DYNAMIC_CLASS_XTI(wxHtmlWindow
, wxScrolledWindow
,"wx/html/htmlwin.h")
1426 wxBEGIN_PROPERTIES_TABLE(wxHtmlWindow
)
1429 style , wxHW_SCROLLBAR_AUTO
1430 borders , (dimension)
1434 wxEND_PROPERTIES_TABLE()
1436 wxBEGIN_HANDLERS_TABLE(wxHtmlWindow
)
1437 wxEND_HANDLERS_TABLE()
1439 wxCONSTRUCTOR_5( wxHtmlWindow
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
1441 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
1444 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
1445 EVT_SIZE(wxHtmlWindow::OnSize
)
1446 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseDown
)
1447 EVT_LEFT_UP(wxHtmlWindow::OnMouseUp
)
1448 EVT_RIGHT_UP(wxHtmlWindow::OnMouseUp
)
1449 EVT_MOTION(wxHtmlWindow::OnMouseMove
)
1450 EVT_ERASE_BACKGROUND(wxHtmlWindow::OnEraseBackground
)
1451 EVT_PAINT(wxHtmlWindow::OnPaint
)
1453 EVT_LEFT_DCLICK(wxHtmlWindow::OnDoubleClick
)
1454 EVT_ENTER_WINDOW(wxHtmlWindow::OnMouseEnter
)
1455 EVT_LEAVE_WINDOW(wxHtmlWindow::OnMouseLeave
)
1456 EVT_KEY_UP(wxHtmlWindow::OnKeyUp
)
1457 EVT_MENU(wxID_COPY
, wxHtmlWindow::OnCopy
)
1458 #endif // wxUSE_CLIPBOARD
1465 // A module to allow initialization/cleanup
1466 // without calling these functions from app.cpp or from
1467 // the user's application.
1469 class wxHtmlWinModule
: public wxModule
1471 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
1473 wxHtmlWinModule() : wxModule() {}
1474 bool OnInit() { return true; }
1475 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
1478 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
1481 // This hack forces the linker to always link in m_* files
1482 // (wxHTML doesn't work without handlers from these files)
1483 #include "wx/html/forcelnk.h"
1484 FORCE_WXHTML_MODULES()
1486 #endif // wxUSE_HTML