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;
186 bool wxHtmlWindow::Create(wxWindow
*parent
, wxWindowID id
,
187 const wxPoint
& pos
, const wxSize
& size
,
188 long style
, const wxString
& name
)
190 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
191 style
| wxVSCROLL
| wxHSCROLL
,
196 SetPage(wxT("<html><body></body></html>"));
201 wxHtmlWindow::~wxHtmlWindow()
205 #endif // wxUSE_CLIPBOARD
214 WX_CLEAR_LIST(wxHtmlProcessorList
, *m_Processors
);
226 void wxHtmlWindow::SetRelatedFrame(wxFrame
* frame
, const wxString
& format
)
228 m_RelatedFrame
= frame
;
229 m_TitleFormat
= format
;
235 void wxHtmlWindow::SetRelatedStatusBar(int bar
)
237 m_RelatedStatusBar
= bar
;
239 #endif // wxUSE_STATUSBAR
243 void wxHtmlWindow::SetFonts(wxString normal_face
, wxString fixed_face
, const int *sizes
)
245 wxString op
= m_OpenedPage
;
247 m_Parser
->SetFonts(normal_face
, fixed_face
, sizes
);
248 // fonts changed => contents invalid, so reload the page:
249 SetPage(wxT("<html><body></body></html>"));
254 void wxHtmlWindow::SetStandardFonts(int size
,
255 const wxString
& normal_face
,
256 const wxString
& fixed_face
)
258 wxString op
= m_OpenedPage
;
260 m_Parser
->SetStandardFonts(size
, normal_face
, fixed_face
);
261 // fonts changed => contents invalid, so reload the page:
262 SetPage(wxT("<html><body></body></html>"));
268 bool wxHtmlWindow::SetPage(const wxString
& source
)
270 wxString
newsrc(source
);
272 wxDELETE(m_selection
);
274 // pass HTML through registered processors:
275 if (m_Processors
|| m_GlobalProcessors
)
277 wxHtmlProcessorList::compatibility_iterator nodeL
, nodeG
;
280 nodeL
= (m_Processors
) ? m_Processors
->GetFirst() : wxHtmlProcessorList::compatibility_iterator();
281 nodeG
= (m_GlobalProcessors
) ? m_GlobalProcessors
->GetFirst() : wxHtmlProcessorList::compatibility_iterator();
283 // VS: there are two lists, global and local, both of them sorted by
284 // priority. Since we have to go through _both_ lists with
285 // decreasing priority, we "merge-sort" the lists on-line by
286 // processing that one of the two heads that has higher priority
287 // in every iteration
288 while (nodeL
|| nodeG
)
290 prL
= (nodeL
) ? nodeL
->GetData()->GetPriority() : -1;
291 prG
= (nodeG
) ? nodeG
->GetData()->GetPriority() : -1;
294 if (nodeL
->GetData()->IsEnabled())
295 newsrc
= nodeL
->GetData()->Process(newsrc
);
296 nodeL
= nodeL
->GetNext();
300 if (nodeG
->GetData()->IsEnabled())
301 newsrc
= nodeG
->GetData()->Process(newsrc
);
302 nodeG
= nodeG
->GetNext();
307 // ...and run the parser on it:
308 wxClientDC
*dc
= new wxClientDC(this);
309 dc
->SetMapMode(wxMM_TEXT
);
310 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
311 SetBackgroundImage(wxNullBitmap
);
312 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
319 m_Cell
= (wxHtmlContainerCell
*) m_Parser
->Parse(newsrc
);
321 m_Cell
->SetIndent(m_Borders
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
322 m_Cell
->SetAlignHor(wxHTML_ALIGN_CENTER
);
324 if (m_tmpCanDrawLocks
== 0)
329 bool wxHtmlWindow::AppendToPage(const wxString
& source
)
331 return SetPage(*(GetParser()->GetSource()) + source
);
334 bool wxHtmlWindow::LoadPage(const wxString
& location
)
336 wxBusyCursor busyCursor
;
340 bool needs_refresh
= false;
343 if (m_HistoryOn
&& (m_HistoryPos
!= -1))
345 // store scroll position into history item:
347 GetViewStart(&x
, &y
);
348 (*m_History
)[m_HistoryPos
].SetPos(y
);
351 if (location
[0] == wxT('#'))
354 wxString anch
= location
.Mid(1) /*1 to end*/;
356 rt_val
= ScrollToAnchor(anch
);
359 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&& location
.BeforeFirst(wxT('#')) == m_OpenedPage
)
361 wxString anch
= location
.AfterFirst(wxT('#'));
363 rt_val
= ScrollToAnchor(anch
);
366 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&&
367 (m_FS
->GetPath() + location
.BeforeFirst(wxT('#'))) == m_OpenedPage
)
369 wxString anch
= location
.AfterFirst(wxT('#'));
371 rt_val
= ScrollToAnchor(anch
);
377 needs_refresh
= true;
380 if (m_RelatedStatusBar
!= -1)
382 m_RelatedFrame
->SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
385 #endif // wxUSE_STATUSBAR
387 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location
);
389 // try to interpret 'location' as filename instead of URL:
392 wxFileName
fn(location
);
393 wxString location2
= wxFileSystem::FileNameToURL(fn
);
394 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location2
);
399 wxLogError(_("Unable to open requested HTML document: %s"), location
.c_str());
406 wxList::compatibility_iterator node
;
407 wxString src
= wxEmptyString
;
410 if (m_RelatedStatusBar
!= -1)
412 wxString msg
= _("Loading : ") + location
;
413 m_RelatedFrame
->SetStatusText(msg
, m_RelatedStatusBar
);
416 #endif // wxUSE_STATUSBAR
418 node
= m_Filters
.GetFirst();
421 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
424 src
= h
->ReadFile(*f
);
427 node
= node
->GetNext();
429 if (src
== wxEmptyString
)
431 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
432 src
= m_DefaultFilter
->ReadFile(*f
);
435 m_FS
->ChangePathTo(f
->GetLocation());
436 rt_val
= SetPage(src
);
437 m_OpenedPage
= f
->GetLocation();
438 if (f
->GetAnchor() != wxEmptyString
)
440 ScrollToAnchor(f
->GetAnchor());
446 if (m_RelatedStatusBar
!= -1)
447 m_RelatedFrame
->SetStatusText(_("Done"), m_RelatedStatusBar
);
448 #endif // wxUSE_STATUSBAR
452 if (m_HistoryOn
) // add this page to history there:
454 int c
= m_History
->GetCount() - (m_HistoryPos
+ 1);
456 if (m_HistoryPos
< 0 ||
457 (*m_History
)[m_HistoryPos
].GetPage() != m_OpenedPage
||
458 (*m_History
)[m_HistoryPos
].GetAnchor() != m_OpenedAnchor
)
461 for (int i
= 0; i
< c
; i
++)
462 m_History
->RemoveAt(m_HistoryPos
);
463 m_History
->Add(new wxHtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
467 if (m_OpenedPageTitle
== wxEmptyString
)
468 OnSetTitle(wxFileNameFromPath(m_OpenedPage
));
482 bool wxHtmlWindow::LoadFile(const wxFileName
& filename
)
484 wxString url
= wxFileSystem::FileNameToURL(filename
);
485 return LoadPage(url
);
489 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
491 const wxHtmlCell
*c
= m_Cell
->Find(wxHTML_COND_ISANCHOR
, &anchor
);
494 wxLogWarning(_("HTML anchor %s does not exist."), anchor
.c_str());
501 for (y
= 0; c
!= NULL
; c
= c
->GetParent()) y
+= c
->GetPosY();
502 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
503 m_OpenedAnchor
= anchor
;
509 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
514 tit
.Printf(m_TitleFormat
, title
.c_str());
515 m_RelatedFrame
->SetTitle(tit
);
517 m_OpenedPageTitle
= title
;
524 void wxHtmlWindow::CreateLayout()
526 int ClientWidth
, ClientHeight
;
530 if (m_Style
& wxHW_SCROLLBAR_NEVER
)
532 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // always off
533 GetClientSize(&ClientWidth
, &ClientHeight
);
534 m_Cell
->Layout(ClientWidth
);
538 GetClientSize(&ClientWidth
, &ClientHeight
);
539 m_Cell
->Layout(ClientWidth
);
540 if (ClientHeight
< m_Cell
->GetHeight() + GetCharHeight())
543 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
544 m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
,
545 (m_Cell
->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
546 /*cheat: top-level frag is always container*/);
548 else /* we fit into window, no need for scrollbars */
550 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
551 GetClientSize(&ClientWidth
, &ClientHeight
);
552 m_Cell
->Layout(ClientWidth
); // ...and relayout
559 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
564 wxString p_fff
, p_ffn
;
566 if (path
!= wxEmptyString
)
568 oldpath
= cfg
->GetPath();
572 m_Borders
= cfg
->Read(wxT("wxHtmlWindow/Borders"), m_Borders
);
573 p_fff
= cfg
->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
574 p_ffn
= cfg
->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
575 for (int i
= 0; i
< 7; i
++)
577 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
578 p_fontsizes
[i
] = cfg
->Read(tmp
, m_Parser
->m_FontsSizes
[i
]);
580 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
582 if (path
!= wxEmptyString
)
583 cfg
->SetPath(oldpath
);
588 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
593 if (path
!= wxEmptyString
)
595 oldpath
= cfg
->GetPath();
599 cfg
->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders
);
600 cfg
->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
601 cfg
->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
602 for (int i
= 0; i
< 7; i
++)
604 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
605 cfg
->Write(tmp
, (long) m_Parser
->m_FontsSizes
[i
]);
608 if (path
!= wxEmptyString
)
609 cfg
->SetPath(oldpath
);
614 bool wxHtmlWindow::HistoryBack()
618 if (m_HistoryPos
< 1) return false;
620 // store scroll position into history item:
622 GetViewStart(&x
, &y
);
623 (*m_History
)[m_HistoryPos
].SetPos(y
);
625 // go to previous position:
628 l
= (*m_History
)[m_HistoryPos
].GetPage();
629 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
632 if (a
== wxEmptyString
) LoadPage(l
);
633 else LoadPage(l
+ wxT("#") + a
);
636 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
641 bool wxHtmlWindow::HistoryCanBack()
643 if (m_HistoryPos
< 1) return false;
648 bool wxHtmlWindow::HistoryForward()
652 if (m_HistoryPos
== -1) return false;
653 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return false;
655 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
658 l
= (*m_History
)[m_HistoryPos
].GetPage();
659 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
662 if (a
== wxEmptyString
) LoadPage(l
);
663 else LoadPage(l
+ wxT("#") + a
);
666 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
671 bool wxHtmlWindow::HistoryCanForward()
673 if (m_HistoryPos
== -1) return false;
674 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return false;
679 void wxHtmlWindow::HistoryClear()
685 void wxHtmlWindow::AddProcessor(wxHtmlProcessor
*processor
)
689 m_Processors
= new wxHtmlProcessorList
;
691 wxHtmlProcessorList::compatibility_iterator node
;
693 for (node
= m_Processors
->GetFirst(); node
; node
= node
->GetNext())
695 if (processor
->GetPriority() > node
->GetData()->GetPriority())
697 m_Processors
->Insert(node
, processor
);
701 m_Processors
->Append(processor
);
704 /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor
*processor
)
706 if (!m_GlobalProcessors
)
708 m_GlobalProcessors
= new wxHtmlProcessorList
;
710 wxHtmlProcessorList::compatibility_iterator node
;
712 for (node
= m_GlobalProcessors
->GetFirst(); node
; node
= node
->GetNext())
714 if (processor
->GetPriority() > node
->GetData()->GetPriority())
716 m_GlobalProcessors
->Insert(node
, processor
);
720 m_GlobalProcessors
->Append(processor
);
725 wxList
wxHtmlWindow::m_Filters
;
726 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
727 wxHtmlProcessorList
*wxHtmlWindow::m_GlobalProcessors
= NULL
;
729 void wxHtmlWindow::CleanUpStatics()
731 wxDELETE(m_DefaultFilter
);
732 WX_CLEAR_LIST(wxList
, m_Filters
);
733 if (m_GlobalProcessors
)
734 WX_CLEAR_LIST(wxHtmlProcessorList
, *m_GlobalProcessors
);
735 wxDELETE(m_GlobalProcessors
);
740 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
742 m_Filters
.Append(filter
);
746 bool wxHtmlWindow::IsSelectionEnabled() const
749 return !(m_Style
& wxHW_NO_SELECTION
);
757 wxString
wxHtmlWindow::DoSelectionToText(wxHtmlSelection
*sel
)
760 return wxEmptyString
;
764 const wxHtmlCell
*end
= sel
->GetToCell();
766 wxHtmlTerminalCellsInterator
i(sel
->GetFromCell(), end
);
769 text
<< i
->ConvertToText(sel
);
772 const wxHtmlCell
*prev
= *i
;
775 if ( prev
->GetParent() != i
->GetParent() )
777 text
<< i
->ConvertToText(*i
== end
? sel
: NULL
);
784 wxString
wxHtmlWindow::ToText()
789 sel
.Set(m_Cell
->GetFirstTerminal(), m_Cell
->GetLastTerminal());
790 return DoSelectionToText(&sel
);
793 return wxEmptyString
;
796 #endif // wxUSE_CLIPBOARD
798 bool wxHtmlWindow::CopySelection(ClipboardType t
)
804 wxTheClipboard
->UsePrimarySelection(t
== Primary
);
806 // Primary selection exists only under X11, so don't do anything under
807 // the other platforms when we try to access it
809 // TODO: this should be abstracted at wxClipboard level!
812 #endif // __UNIX__/!__UNIX__
814 if ( wxTheClipboard
->Open() )
816 const wxString
txt(SelectionToText());
817 wxTheClipboard
->SetData(new wxTextDataObject(txt
));
818 wxTheClipboard
->Close();
819 wxLogTrace(_T("wxhtmlselection"),
820 _("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
;
1026 #endif // wxUSE_CLIPBOARD
1029 void wxHtmlWindow::OnMouseUp(wxMouseEvent
& event
)
1032 if ( m_makingSelection
)
1035 m_makingSelection
= false;
1037 // did the user move the mouse far enough from starting point?
1038 if ( CopySelection(Primary
) )
1040 // we don't want mouse up event that ended selecting to be
1041 // handled as mouse click and e.g. follow hyperlink:
1045 #endif // wxUSE_CLIPBOARD
1050 wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
1051 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1053 // check is needed because FindCellByPos returns terminal cell and
1054 // containers may have empty borders -- in this case NULL will be
1057 OnCellClicked(cell
, pos
.x
, pos
.y
, event
);
1063 void wxHtmlWindow::OnInternalIdle()
1065 wxWindow::OnInternalIdle();
1067 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
))
1069 #ifdef DEBUG_HTML_SELECTION
1073 wxGetMousePosition(&xc
, &yc
);
1074 ScreenToClient(&xc
, &yc
);
1075 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
1077 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(x
, y
);
1079 // handle selection update:
1080 if ( m_makingSelection
)
1082 if ( !m_tmpSelFromCell
)
1083 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1084 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
);
1086 // NB: a trick - we adjust selFromPos to be upper left or bottom
1087 // right corner of the first cell of the selection depending
1088 // on whether the mouse is moving to the right or to the left.
1089 // This gives us more "natural" behaviour when selecting
1090 // a line (specifically, first cell of the next line is not
1091 // included if you drag selection from left to right over
1094 if ( !m_tmpSelFromCell
)
1096 dirFromPos
= m_tmpSelFromPos
;
1100 dirFromPos
= m_tmpSelFromCell
->GetAbsPos();
1101 if ( x
< m_tmpSelFromPos
.x
)
1103 dirFromPos
.x
+= m_tmpSelFromCell
->GetWidth();
1104 dirFromPos
.y
+= m_tmpSelFromCell
->GetHeight();
1107 bool goingDown
= dirFromPos
.y
< y
||
1108 (dirFromPos
.y
== y
&& dirFromPos
.x
< x
);
1110 // determine selection span:
1111 if ( /*still*/ !m_tmpSelFromCell
)
1115 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1116 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1117 wxHTML_FIND_NEAREST_AFTER
);
1118 if (!m_tmpSelFromCell
)
1119 m_tmpSelFromCell
= m_Cell
->GetFirstTerminal();
1123 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1124 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1125 wxHTML_FIND_NEAREST_BEFORE
);
1126 if (!m_tmpSelFromCell
)
1127 m_tmpSelFromCell
= m_Cell
->GetLastTerminal();
1131 wxHtmlCell
*selcell
= cell
;
1136 selcell
= m_Cell
->FindCellByPos(x
, y
,
1137 wxHTML_FIND_NEAREST_BEFORE
);
1139 selcell
= m_Cell
->GetLastTerminal();
1143 selcell
= m_Cell
->FindCellByPos(x
, y
,
1144 wxHTML_FIND_NEAREST_AFTER
);
1146 selcell
= m_Cell
->GetFirstTerminal();
1150 // NB: it may *rarely* happen that the code above didn't find one
1151 // of the cells, e.g. if wxHtmlWindow doesn't contain any
1153 if ( selcell
&& m_tmpSelFromCell
)
1157 // start selecting only if mouse movement was big enough
1158 // (otherwise it was meant as mouse click, not selection):
1159 const int PRECISION
= 2;
1160 wxPoint diff
= m_tmpSelFromPos
- wxPoint(x
,y
);
1161 if (abs(diff
.x
) > PRECISION
|| abs(diff
.y
) > PRECISION
)
1163 m_selection
= new wxHtmlSelection();
1168 if ( m_tmpSelFromCell
->IsBefore(selcell
) )
1170 m_selection
->Set(m_tmpSelFromPos
, m_tmpSelFromCell
,
1171 wxPoint(x
,y
), selcell
); }
1174 m_selection
->Set(wxPoint(x
,y
), selcell
,
1175 m_tmpSelFromPos
, m_tmpSelFromCell
);
1177 m_selection
->ClearPrivPos();
1183 // handle cursor and status bar text changes:
1184 if ( cell
!= m_tmpLastCell
)
1186 wxHtmlLinkInfo
*lnk
= cell
? cell
->GetLink(x
, y
) : NULL
;
1189 cur
= cell
->GetCursor();
1191 cur
= *wxSTANDARD_CURSOR
;
1194 if (lnk
!= m_tmpLastLink
)
1199 if (m_RelatedStatusBar
!= -1)
1200 m_RelatedFrame
->SetStatusText(wxEmptyString
,
1201 m_RelatedStatusBar
);
1205 if (m_RelatedStatusBar
!= -1)
1206 m_RelatedFrame
->SetStatusText(lnk
->GetHref(),
1207 m_RelatedStatusBar
);
1209 #endif // wxUSE_STATUSBAR
1210 m_tmpLastLink
= lnk
;
1213 m_tmpLastCell
= cell
;
1215 else // mouse moved but stayed in the same cell
1218 OnCellMouseHover(cell
, x
, y
);
1221 m_tmpMouseMoved
= false;
1226 void wxHtmlWindow::StopAutoScrolling()
1228 if ( m_timerAutoScroll
)
1230 wxDELETE(m_timerAutoScroll
);
1234 void wxHtmlWindow::OnMouseEnter(wxMouseEvent
& event
)
1236 StopAutoScrolling();
1240 void wxHtmlWindow::OnMouseLeave(wxMouseEvent
& event
)
1242 // don't prevent the usual processing of the event from taking place
1245 // when a captured mouse leave a scrolled window we start generate
1246 // scrolling events to allow, for example, extending selection beyond the
1247 // visible area in some controls
1248 if ( wxWindow::GetCapture() == this )
1250 // where is the mouse leaving?
1252 wxPoint pt
= event
.GetPosition();
1255 orient
= wxHORIZONTAL
;
1258 else if ( pt
.y
< 0 )
1260 orient
= wxVERTICAL
;
1263 else // we're lower or to the right of the window
1265 wxSize size
= GetClientSize();
1266 if ( pt
.x
> size
.x
)
1268 orient
= wxHORIZONTAL
;
1269 pos
= GetVirtualSize().x
/ wxHTML_SCROLL_STEP
;
1271 else if ( pt
.y
> size
.y
)
1273 orient
= wxVERTICAL
;
1274 pos
= GetVirtualSize().y
/ wxHTML_SCROLL_STEP
;
1276 else // this should be impossible
1278 // but seems to happen sometimes under wxMSW - maybe it's a bug
1279 // there but for now just ignore it
1281 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1287 // only start the auto scroll timer if the window can be scrolled in
1289 if ( !HasScrollbar(orient
) )
1292 delete m_timerAutoScroll
;
1293 m_timerAutoScroll
= new wxHtmlWinAutoScrollTimer
1296 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1297 : wxEVT_SCROLLWIN_LINEDOWN
,
1301 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1305 void wxHtmlWindow::OnKeyUp(wxKeyEvent
& event
)
1307 if ( IsSelectionEnabled() &&
1308 event
.GetKeyCode() == 'C' && event
.ControlDown() )
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