1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtmlWindow class for parsing & displaying HTML (implementation)
4 // Author: Vaclav Slavik
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
12 #pragma implementation "htmlwin.h"
13 #pragma implementation "htmlproc.h"
16 #include "wx/wxprec.h"
19 #if wxUSE_HTML && wxUSE_STREAMS
28 #include "wx/dcclient.h"
32 #include "wx/html/htmlwin.h"
33 #include "wx/html/htmlproc.h"
35 #include "wx/clipbrd.h"
36 #include "wx/dataobj.h"
38 #include "wx/dcmemory.h"
39 #include "wx/settings.h"
41 #include "wx/arrimpl.cpp"
42 #include "wx/listimpl.cpp"
46 // ----------------------------------------------------------------------------
47 // wxHtmlWinAutoScrollTimer: the timer used to generate a stream of scroll
48 // events when a captured mouse is held outside the window
49 // ----------------------------------------------------------------------------
51 class wxHtmlWinAutoScrollTimer
: public wxTimer
54 wxHtmlWinAutoScrollTimer(wxScrolledWindow
*win
,
55 wxEventType eventTypeToSend
,
59 m_eventType
= eventTypeToSend
;
64 virtual void Notify();
67 wxScrolledWindow
*m_win
;
68 wxEventType m_eventType
;
72 DECLARE_NO_COPY_CLASS(wxHtmlWinAutoScrollTimer
)
75 void wxHtmlWinAutoScrollTimer::Notify()
77 // only do all this as long as the window is capturing the mouse
78 if ( wxWindow::GetCapture() != m_win
)
82 else // we still capture the mouse, continue generating events
84 // first scroll the window if we are allowed to do it
85 wxScrollWinEvent
event1(m_eventType
, m_pos
, m_orient
);
86 event1
.SetEventObject(m_win
);
87 if ( m_win
->GetEventHandler()->ProcessEvent(event1
) )
89 // and then send a pseudo mouse-move event to refresh the selection
90 wxMouseEvent
event2(wxEVT_MOTION
);
91 wxGetMousePosition(&event2
.m_x
, &event2
.m_y
);
93 // the mouse event coordinates should be client, not screen as
94 // returned by wxGetMousePosition
95 wxWindow
*parentTop
= m_win
;
96 while ( parentTop
->GetParent() )
97 parentTop
= parentTop
->GetParent();
98 wxPoint ptOrig
= parentTop
->GetPosition();
99 event2
.m_x
-= ptOrig
.x
;
100 event2
.m_y
-= ptOrig
.y
;
102 event2
.SetEventObject(m_win
);
104 // FIXME: we don't fill in the other members - ok?
105 m_win
->GetEventHandler()->ProcessEvent(event2
);
107 else // can't scroll further, stop
114 #endif // wxUSE_CLIPBOARD
118 //-----------------------------------------------------------------------------
120 //-----------------------------------------------------------------------------
122 // item of history list
123 class WXDLLIMPEXP_HTML wxHtmlHistoryItem
126 wxHtmlHistoryItem(const wxString
& p
, const wxString
& a
) {m_Page
= p
, m_Anchor
= a
, m_Pos
= 0;}
127 int GetPos() const {return m_Pos
;}
128 void SetPos(int p
) {m_Pos
= p
;}
129 const wxString
& GetPage() const {return m_Page
;}
130 const wxString
& GetAnchor() const {return m_Anchor
;}
139 //-----------------------------------------------------------------------------
140 // our private arrays:
141 //-----------------------------------------------------------------------------
143 WX_DECLARE_OBJARRAY(wxHtmlHistoryItem
, wxHtmlHistoryArray
);
144 WX_DEFINE_OBJARRAY(wxHtmlHistoryArray
);
146 WX_DECLARE_LIST(wxHtmlProcessor
, wxHtmlProcessorList
);
147 WX_DEFINE_LIST(wxHtmlProcessorList
);
149 //-----------------------------------------------------------------------------
151 //-----------------------------------------------------------------------------
154 void wxHtmlWindow::Init()
156 m_tmpMouseMoved
= FALSE
;
157 m_tmpLastLink
= NULL
;
158 m_tmpLastCell
= NULL
;
159 m_tmpCanDrawLocks
= 0;
160 m_FS
= new wxFileSystem();
162 m_RelatedStatusBar
= -1;
163 #endif // wxUSE_STATUSBAR
164 m_RelatedFrame
= NULL
;
165 m_TitleFormat
= wxT("%s");
166 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
168 m_Parser
= new wxHtmlWinParser(this);
169 m_Parser
->SetFS(m_FS
);
172 m_History
= new wxHtmlHistoryArray
;
177 m_makingSelection
= false;
179 m_timerAutoScroll
= NULL
;
180 m_lastDoubleClick
= 0;
181 #endif // wxUSE_CLIPBOARD
185 bool wxHtmlWindow::Create(wxWindow
*parent
, wxWindowID id
,
186 const wxPoint
& pos
, const wxSize
& size
,
187 long style
, const wxString
& name
)
189 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
190 style
| wxVSCROLL
| wxHSCROLL
,
195 SetPage(wxT("<html><body></body></html>"));
200 wxHtmlWindow::~wxHtmlWindow()
204 #endif // wxUSE_CLIPBOARD
213 WX_CLEAR_LIST(wxHtmlProcessorList
, *m_Processors
);
225 void wxHtmlWindow::SetRelatedFrame(wxFrame
* frame
, const wxString
& format
)
227 m_RelatedFrame
= frame
;
228 m_TitleFormat
= format
;
234 void wxHtmlWindow::SetRelatedStatusBar(int bar
)
236 m_RelatedStatusBar
= bar
;
238 #endif // wxUSE_STATUSBAR
242 void wxHtmlWindow::SetFonts(wxString normal_face
, wxString fixed_face
, const int *sizes
)
244 wxString op
= m_OpenedPage
;
246 m_Parser
->SetFonts(normal_face
, fixed_face
, sizes
);
247 // fonts changed => contents invalid, so reload the page:
248 SetPage(wxT("<html><body></body></html>"));
253 void wxHtmlWindow::SetStandardFonts(int size
,
254 const wxString
& normal_face
,
255 const wxString
& fixed_face
)
257 wxString op
= m_OpenedPage
;
259 m_Parser
->SetStandardFonts(size
, normal_face
, fixed_face
);
260 // fonts changed => contents invalid, so reload the page:
261 SetPage(wxT("<html><body></body></html>"));
267 bool wxHtmlWindow::SetPage(const wxString
& source
)
269 wxString
newsrc(source
);
271 wxDELETE(m_selection
);
273 // pass HTML through registered processors:
274 if (m_Processors
|| m_GlobalProcessors
)
276 wxHtmlProcessorList::compatibility_iterator nodeL
, nodeG
;
279 nodeL
= (m_Processors
) ? m_Processors
->GetFirst() : wxHtmlProcessorList::compatibility_iterator();
280 nodeG
= (m_GlobalProcessors
) ? m_GlobalProcessors
->GetFirst() : wxHtmlProcessorList::compatibility_iterator();
282 // VS: there are two lists, global and local, both of them sorted by
283 // priority. Since we have to go through _both_ lists with
284 // decreasing priority, we "merge-sort" the lists on-line by
285 // processing that one of the two heads that has higher priority
286 // in every iteration
287 while (nodeL
|| nodeG
)
289 prL
= (nodeL
) ? nodeL
->GetData()->GetPriority() : -1;
290 prG
= (nodeG
) ? nodeG
->GetData()->GetPriority() : -1;
293 if (nodeL
->GetData()->IsEnabled())
294 newsrc
= nodeL
->GetData()->Process(newsrc
);
295 nodeL
= nodeL
->GetNext();
299 if (nodeG
->GetData()->IsEnabled())
300 newsrc
= nodeG
->GetData()->Process(newsrc
);
301 nodeG
= nodeG
->GetNext();
306 // ...and run the parser on it:
307 wxClientDC
*dc
= new wxClientDC(this);
308 dc
->SetMapMode(wxMM_TEXT
);
309 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
310 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
317 m_Cell
= (wxHtmlContainerCell
*) m_Parser
->Parse(newsrc
);
319 m_Cell
->SetIndent(m_Borders
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
320 m_Cell
->SetAlignHor(wxHTML_ALIGN_CENTER
);
322 if (m_tmpCanDrawLocks
== 0)
327 bool wxHtmlWindow::AppendToPage(const wxString
& source
)
329 return SetPage(*(GetParser()->GetSource()) + source
);
332 bool wxHtmlWindow::LoadPage(const wxString
& location
)
334 wxBusyCursor busyCursor
;
338 bool needs_refresh
= FALSE
;
341 if (m_HistoryOn
&& (m_HistoryPos
!= -1))
343 // store scroll position into history item:
345 GetViewStart(&x
, &y
);
346 (*m_History
)[m_HistoryPos
].SetPos(y
);
349 if (location
[0] == wxT('#'))
352 wxString anch
= location
.Mid(1) /*1 to end*/;
354 rt_val
= ScrollToAnchor(anch
);
357 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&& location
.BeforeFirst(wxT('#')) == m_OpenedPage
)
359 wxString anch
= location
.AfterFirst(wxT('#'));
361 rt_val
= ScrollToAnchor(anch
);
364 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&&
365 (m_FS
->GetPath() + location
.BeforeFirst(wxT('#'))) == m_OpenedPage
)
367 wxString anch
= location
.AfterFirst(wxT('#'));
369 rt_val
= ScrollToAnchor(anch
);
375 needs_refresh
= true;
378 if (m_RelatedStatusBar
!= -1)
380 m_RelatedFrame
->SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
383 #endif // wxUSE_STATUSBAR
385 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location
);
387 // try to interpret 'location' as filename instead of URL:
390 wxFileName
fn(location
);
391 wxString location2
= wxFileSystem::FileNameToURL(fn
);
392 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location2
);
397 wxLogError(_("Unable to open requested HTML document: %s"), location
.c_str());
404 wxList::compatibility_iterator node
;
405 wxString src
= wxEmptyString
;
408 if (m_RelatedStatusBar
!= -1)
410 wxString msg
= _("Loading : ") + location
;
411 m_RelatedFrame
->SetStatusText(msg
, m_RelatedStatusBar
);
414 #endif // wxUSE_STATUSBAR
416 node
= m_Filters
.GetFirst();
419 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
422 src
= h
->ReadFile(*f
);
425 node
= node
->GetNext();
427 if (src
== wxEmptyString
)
429 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
430 src
= m_DefaultFilter
->ReadFile(*f
);
433 m_FS
->ChangePathTo(f
->GetLocation());
434 rt_val
= SetPage(src
);
435 m_OpenedPage
= f
->GetLocation();
436 if (f
->GetAnchor() != wxEmptyString
)
438 ScrollToAnchor(f
->GetAnchor());
444 if (m_RelatedStatusBar
!= -1)
445 m_RelatedFrame
->SetStatusText(_("Done"), m_RelatedStatusBar
);
446 #endif // wxUSE_STATUSBAR
450 if (m_HistoryOn
) // add this page to history there:
452 int c
= m_History
->GetCount() - (m_HistoryPos
+ 1);
454 if (m_HistoryPos
< 0 ||
455 (*m_History
)[m_HistoryPos
].GetPage() != m_OpenedPage
||
456 (*m_History
)[m_HistoryPos
].GetAnchor() != m_OpenedAnchor
)
459 for (int i
= 0; i
< c
; i
++)
460 m_History
->RemoveAt(m_HistoryPos
);
461 m_History
->Add(new wxHtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
465 if (m_OpenedPageTitle
== wxEmptyString
)
466 OnSetTitle(wxFileNameFromPath(m_OpenedPage
));
480 bool wxHtmlWindow::LoadFile(const wxFileName
& filename
)
482 wxString url
= wxFileSystem::FileNameToURL(filename
);
483 return LoadPage(url
);
487 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
489 const wxHtmlCell
*c
= m_Cell
->Find(wxHTML_COND_ISANCHOR
, &anchor
);
492 wxLogWarning(_("HTML anchor %s does not exist."), anchor
.c_str());
499 for (y
= 0; c
!= NULL
; c
= c
->GetParent()) y
+= c
->GetPosY();
500 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
501 m_OpenedAnchor
= anchor
;
507 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
512 tit
.Printf(m_TitleFormat
, title
.c_str());
513 m_RelatedFrame
->SetTitle(tit
);
515 m_OpenedPageTitle
= title
;
522 void wxHtmlWindow::CreateLayout()
524 int ClientWidth
, ClientHeight
;
528 if (m_Style
& wxHW_SCROLLBAR_NEVER
)
530 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // always off
531 GetClientSize(&ClientWidth
, &ClientHeight
);
532 m_Cell
->Layout(ClientWidth
);
536 GetClientSize(&ClientWidth
, &ClientHeight
);
537 m_Cell
->Layout(ClientWidth
);
538 if (ClientHeight
< m_Cell
->GetHeight() + GetCharHeight())
541 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
542 m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
,
543 (m_Cell
->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
544 /*cheat: top-level frag is always container*/);
546 else /* we fit into window, no need for scrollbars */
548 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
549 GetClientSize(&ClientWidth
, &ClientHeight
);
550 m_Cell
->Layout(ClientWidth
); // ...and relayout
557 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
562 wxString p_fff
, p_ffn
;
564 if (path
!= wxEmptyString
)
566 oldpath
= cfg
->GetPath();
570 m_Borders
= cfg
->Read(wxT("wxHtmlWindow/Borders"), m_Borders
);
571 p_fff
= cfg
->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
572 p_ffn
= cfg
->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
573 for (int i
= 0; i
< 7; i
++)
575 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
576 p_fontsizes
[i
] = cfg
->Read(tmp
, m_Parser
->m_FontsSizes
[i
]);
578 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
580 if (path
!= wxEmptyString
)
581 cfg
->SetPath(oldpath
);
586 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
591 if (path
!= wxEmptyString
)
593 oldpath
= cfg
->GetPath();
597 cfg
->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders
);
598 cfg
->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
599 cfg
->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
600 for (int i
= 0; i
< 7; i
++)
602 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
603 cfg
->Write(tmp
, (long) m_Parser
->m_FontsSizes
[i
]);
606 if (path
!= wxEmptyString
)
607 cfg
->SetPath(oldpath
);
612 bool wxHtmlWindow::HistoryBack()
616 if (m_HistoryPos
< 1) return FALSE
;
618 // store scroll position into history item:
620 GetViewStart(&x
, &y
);
621 (*m_History
)[m_HistoryPos
].SetPos(y
);
623 // go to previous position:
626 l
= (*m_History
)[m_HistoryPos
].GetPage();
627 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
630 if (a
== wxEmptyString
) LoadPage(l
);
631 else LoadPage(l
+ wxT("#") + a
);
634 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
639 bool wxHtmlWindow::HistoryCanBack()
641 if (m_HistoryPos
< 1) return FALSE
;
646 bool wxHtmlWindow::HistoryForward()
650 if (m_HistoryPos
== -1) return FALSE
;
651 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
653 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
656 l
= (*m_History
)[m_HistoryPos
].GetPage();
657 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
660 if (a
== wxEmptyString
) LoadPage(l
);
661 else LoadPage(l
+ wxT("#") + a
);
664 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
669 bool wxHtmlWindow::HistoryCanForward()
671 if (m_HistoryPos
== -1) return FALSE
;
672 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
677 void wxHtmlWindow::HistoryClear()
683 void wxHtmlWindow::AddProcessor(wxHtmlProcessor
*processor
)
687 m_Processors
= new wxHtmlProcessorList
;
689 wxHtmlProcessorList::compatibility_iterator node
;
691 for (node
= m_Processors
->GetFirst(); node
; node
= node
->GetNext())
693 if (processor
->GetPriority() > node
->GetData()->GetPriority())
695 m_Processors
->Insert(node
, processor
);
699 m_Processors
->Append(processor
);
702 /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor
*processor
)
704 if (!m_GlobalProcessors
)
706 m_GlobalProcessors
= new wxHtmlProcessorList
;
708 wxHtmlProcessorList::compatibility_iterator node
;
710 for (node
= m_GlobalProcessors
->GetFirst(); node
; node
= node
->GetNext())
712 if (processor
->GetPriority() > node
->GetData()->GetPriority())
714 m_GlobalProcessors
->Insert(node
, processor
);
718 m_GlobalProcessors
->Append(processor
);
723 wxList
wxHtmlWindow::m_Filters
;
724 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
725 wxHtmlProcessorList
*wxHtmlWindow::m_GlobalProcessors
= NULL
;
727 void wxHtmlWindow::CleanUpStatics()
729 wxDELETE(m_DefaultFilter
);
730 WX_CLEAR_LIST(wxList
, m_Filters
);
731 if (m_GlobalProcessors
)
732 WX_CLEAR_LIST(wxHtmlProcessorList
, *m_GlobalProcessors
);
733 wxDELETE(m_GlobalProcessors
);
738 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
740 m_Filters
.Append(filter
);
744 bool wxHtmlWindow::IsSelectionEnabled() const
747 return !(m_Style
& wxHW_NO_SELECTION
);
755 wxString
wxHtmlWindow::DoSelectionToText(wxHtmlSelection
*sel
)
758 return wxEmptyString
;
762 const wxHtmlCell
*end
= sel
->GetToCell();
764 wxHtmlTerminalCellsInterator
i(sel
->GetFromCell(), end
);
767 text
<< i
->ConvertToText(sel
);
770 const wxHtmlCell
*prev
= *i
;
773 if ( prev
->GetParent() != i
->GetParent() )
775 text
<< i
->ConvertToText(*i
== end
? sel
: NULL
);
782 wxString
wxHtmlWindow::ToText()
787 sel
.Set(m_Cell
->GetFirstTerminal(), m_Cell
->GetLastTerminal());
788 return DoSelectionToText(&sel
);
791 return wxEmptyString
;
794 #endif // wxUSE_CLIPBOARD
796 bool wxHtmlWindow::CopySelection(ClipboardType t
)
802 wxTheClipboard
->UsePrimarySelection(t
== Primary
);
804 // Primary selection exists only under X11, so don't do anything under
805 // the other platforms when we try to access it
807 // TODO: this should be abstracted at wxClipboard level!
810 #endif // __UNIX__/!__UNIX__
812 if ( wxTheClipboard
->Open() )
814 const wxString
txt(SelectionToText());
815 wxTheClipboard
->SetData(new wxTextDataObject(txt
));
816 wxTheClipboard
->Close();
817 wxLogTrace(_T("wxhtmlselection"),
818 _("Copied to clipboard:\"%s\""), txt
.c_str());
823 #endif // wxUSE_CLIPBOARD
829 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
831 const wxMouseEvent
*e
= link
.GetEvent();
832 if (e
== NULL
|| e
->LeftUp())
833 LoadPage(link
.GetHref());
836 void wxHtmlWindow::OnCellClicked(wxHtmlCell
*cell
,
837 wxCoord x
, wxCoord y
,
838 const wxMouseEvent
& event
)
840 wxCHECK_RET( cell
, _T("can't be called with NULL cell") );
842 cell
->OnMouseClick(this, x
, y
, event
);
845 void wxHtmlWindow::OnCellMouseHover(wxHtmlCell
* WXUNUSED(cell
),
846 wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
851 void wxHtmlWindow::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
855 void wxHtmlWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
859 if (m_tmpCanDrawLocks
> 0 || m_Cell
== NULL
) return;
862 GetViewStart(&x
, &y
);
863 wxRect rect
= GetUpdateRegion().GetBox();
864 wxSize sz
= GetSize();
868 m_backBuffer
= new wxBitmap(sz
.x
, sz
.y
);
869 dcm
.SelectObject(*m_backBuffer
);
870 dcm
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
873 dcm
.SetMapMode(wxMM_TEXT
);
874 dcm
.SetBackgroundMode(wxTRANSPARENT
);
876 wxHtmlRenderingInfo rinfo
;
877 wxDefaultHtmlRenderingStyle rstyle
;
878 rinfo
.SetSelection(m_selection
);
879 rinfo
.SetStyle(&rstyle
);
880 m_Cell
->Draw(dcm
, 0, 0,
881 y
* wxHTML_SCROLL_STEP
+ rect
.GetTop(),
882 y
* wxHTML_SCROLL_STEP
+ rect
.GetBottom(),
885 //#define DEBUG_HTML_SELECTION
886 #ifdef DEBUG_HTML_SELECTION
889 wxGetMousePosition(&xc
, &yc
);
890 ScreenToClient(&xc
, &yc
);
891 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
892 wxHtmlCell
*at
= m_Cell
->FindCellByPos(x
, y
);
894 m_Cell
->FindCellByPos(x
, y
, wxHTML_FIND_NEAREST_BEFORE
);
896 m_Cell
->FindCellByPos(x
, y
, wxHTML_FIND_NEAREST_AFTER
);
898 dcm
.SetBrush(*wxTRANSPARENT_BRUSH
);
899 dcm
.SetPen(*wxBLACK_PEN
);
901 dcm
.DrawRectangle(at
->GetAbsPos(),
902 wxSize(at
->GetWidth(),at
->GetHeight()));
903 dcm
.SetPen(*wxGREEN_PEN
);
905 dcm
.DrawRectangle(before
->GetAbsPos().x
+1, before
->GetAbsPos().y
+1,
906 before
->GetWidth()-2,before
->GetHeight()-2);
907 dcm
.SetPen(*wxRED_PEN
);
909 dcm
.DrawRectangle(after
->GetAbsPos().x
+2, after
->GetAbsPos().y
+2,
910 after
->GetWidth()-4,after
->GetHeight()-4);
914 dcm
.SetDeviceOrigin(0,0);
915 dc
.Blit(0, rect
.GetTop(),
916 sz
.x
, rect
.GetBottom() - rect
.GetTop() + 1,
924 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
926 wxDELETE(m_backBuffer
);
928 wxScrolledWindow::OnSize(event
);
931 // Recompute selection if necessary:
934 m_selection
->Set(m_selection
->GetFromCell(),
935 m_selection
->GetToCell());
936 m_selection
->ClearPrivPos();
943 void wxHtmlWindow::OnMouseMove(wxMouseEvent
& WXUNUSED(event
))
945 m_tmpMouseMoved
= true;
948 void wxHtmlWindow::OnMouseDown(wxMouseEvent
& event
)
951 if ( event
.LeftDown() && IsSelectionEnabled() )
953 const long TRIPLECLICK_LEN
= 200; // 0.2 sec after doubleclick
954 if ( wxGetLocalTimeMillis() - m_lastDoubleClick
<= TRIPLECLICK_LEN
)
956 SelectLine(CalcUnscrolledPosition(event
.GetPosition()));
958 (void) CopySelection();
962 m_makingSelection
= true;
966 wxDELETE(m_selection
);
969 m_tmpSelFromPos
= CalcUnscrolledPosition(event
.GetPosition());
970 m_tmpSelFromCell
= NULL
;
975 #endif // wxUSE_CLIPBOARD
978 void wxHtmlWindow::OnMouseUp(wxMouseEvent
& event
)
981 if ( m_makingSelection
)
984 m_makingSelection
= false;
986 // did the user move the mouse far enough from starting point?
987 if ( CopySelection(Primary
) )
989 // we don't want mouse up event that ended selecting to be
990 // handled as mouse click and e.g. follow hyperlink:
994 #endif // wxUSE_CLIPBOARD
999 wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
1000 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1002 // check is needed because FindCellByPos returns terminal cell and
1003 // containers may have empty borders -- in this case NULL will be
1006 OnCellClicked(cell
, pos
.x
, pos
.y
, event
);
1012 void wxHtmlWindow::OnInternalIdle()
1014 wxWindow::OnInternalIdle();
1016 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
))
1018 #ifdef DEBUG_HTML_SELECTION
1022 wxGetMousePosition(&xc
, &yc
);
1023 ScreenToClient(&xc
, &yc
);
1024 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
1026 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(x
, y
);
1028 // handle selection update:
1029 if ( m_makingSelection
)
1031 if ( !m_tmpSelFromCell
)
1032 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1033 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
);
1035 // NB: a trick - we adjust selFromPos to be upper left or bottom
1036 // right corner of the first cell of the selection depending
1037 // on whether the mouse is moving to the right or to the left.
1038 // This gives us more "natural" behaviour when selecting
1039 // a line (specifically, first cell of the next line is not
1040 // included if you drag selection from left to right over
1043 if ( !m_tmpSelFromCell
)
1045 dirFromPos
= m_tmpSelFromPos
;
1049 dirFromPos
= m_tmpSelFromCell
->GetAbsPos();
1050 if ( x
< m_tmpSelFromPos
.x
)
1052 dirFromPos
.x
+= m_tmpSelFromCell
->GetWidth();
1053 dirFromPos
.y
+= m_tmpSelFromCell
->GetHeight();
1056 bool goingDown
= dirFromPos
.y
< y
||
1057 (dirFromPos
.y
== y
&& dirFromPos
.x
< x
);
1059 // determine selection span:
1060 if ( /*still*/ !m_tmpSelFromCell
)
1064 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1065 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1066 wxHTML_FIND_NEAREST_AFTER
);
1067 if (!m_tmpSelFromCell
)
1068 m_tmpSelFromCell
= m_Cell
->GetFirstTerminal();
1072 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1073 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1074 wxHTML_FIND_NEAREST_BEFORE
);
1075 if (!m_tmpSelFromCell
)
1076 m_tmpSelFromCell
= m_Cell
->GetLastTerminal();
1080 wxHtmlCell
*selcell
= cell
;
1085 selcell
= m_Cell
->FindCellByPos(x
, y
,
1086 wxHTML_FIND_NEAREST_BEFORE
);
1088 selcell
= m_Cell
->GetLastTerminal();
1092 selcell
= m_Cell
->FindCellByPos(x
, y
,
1093 wxHTML_FIND_NEAREST_AFTER
);
1095 selcell
= m_Cell
->GetFirstTerminal();
1099 // NB: it may *rarely* happen that the code above didn't find one
1100 // of the cells, e.g. if wxHtmlWindow doesn't contain any
1102 if ( selcell
&& m_tmpSelFromCell
)
1106 // start selecting only if mouse movement was big enough
1107 // (otherwise it was meant as mouse click, not selection):
1108 const int PRECISION
= 2;
1109 wxPoint diff
= m_tmpSelFromPos
- wxPoint(x
,y
);
1110 if (abs(diff
.x
) > PRECISION
|| abs(diff
.y
) > PRECISION
)
1112 m_selection
= new wxHtmlSelection();
1117 if ( m_tmpSelFromCell
->IsBefore(selcell
) )
1119 m_selection
->Set(m_tmpSelFromPos
, m_tmpSelFromCell
,
1120 wxPoint(x
,y
), selcell
); }
1123 m_selection
->Set(wxPoint(x
,y
), selcell
,
1124 m_tmpSelFromPos
, m_tmpSelFromCell
);
1126 m_selection
->ClearPrivPos();
1132 // handle cursor and status bar text changes:
1133 if ( cell
!= m_tmpLastCell
)
1135 wxHtmlLinkInfo
*lnk
= cell
? cell
->GetLink(x
, y
) : NULL
;
1138 cur
= cell
->GetCursor();
1140 cur
= *wxSTANDARD_CURSOR
;
1143 if (lnk
!= m_tmpLastLink
)
1148 if (m_RelatedStatusBar
!= -1)
1149 m_RelatedFrame
->SetStatusText(wxEmptyString
,
1150 m_RelatedStatusBar
);
1154 if (m_RelatedStatusBar
!= -1)
1155 m_RelatedFrame
->SetStatusText(lnk
->GetHref(),
1156 m_RelatedStatusBar
);
1158 #endif // wxUSE_STATUSBAR
1159 m_tmpLastLink
= lnk
;
1162 m_tmpLastCell
= cell
;
1164 else // mouse moved but stayed in the same cell
1167 OnCellMouseHover(cell
, x
, y
);
1170 m_tmpMouseMoved
= FALSE
;
1175 void wxHtmlWindow::StopAutoScrolling()
1177 if ( m_timerAutoScroll
)
1179 wxDELETE(m_timerAutoScroll
);
1183 void wxHtmlWindow::OnMouseEnter(wxMouseEvent
& event
)
1185 StopAutoScrolling();
1189 void wxHtmlWindow::OnMouseLeave(wxMouseEvent
& event
)
1191 // don't prevent the usual processing of the event from taking place
1194 // when a captured mouse leave a scrolled window we start generate
1195 // scrolling events to allow, for example, extending selection beyond the
1196 // visible area in some controls
1197 if ( wxWindow::GetCapture() == this )
1199 // where is the mouse leaving?
1201 wxPoint pt
= event
.GetPosition();
1204 orient
= wxHORIZONTAL
;
1207 else if ( pt
.y
< 0 )
1209 orient
= wxVERTICAL
;
1212 else // we're lower or to the right of the window
1214 wxSize size
= GetClientSize();
1215 if ( pt
.x
> size
.x
)
1217 orient
= wxHORIZONTAL
;
1218 pos
= GetVirtualSize().x
/ wxHTML_SCROLL_STEP
;
1220 else if ( pt
.y
> size
.y
)
1222 orient
= wxVERTICAL
;
1223 pos
= GetVirtualSize().y
/ wxHTML_SCROLL_STEP
;
1225 else // this should be impossible
1227 // but seems to happen sometimes under wxMSW - maybe it's a bug
1228 // there but for now just ignore it
1230 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1236 // only start the auto scroll timer if the window can be scrolled in
1238 if ( !HasScrollbar(orient
) )
1241 delete m_timerAutoScroll
;
1242 m_timerAutoScroll
= new wxHtmlWinAutoScrollTimer
1245 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1246 : wxEVT_SCROLLWIN_LINEDOWN
,
1250 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1254 void wxHtmlWindow::OnKeyUp(wxKeyEvent
& event
)
1256 if ( IsSelectionEnabled() &&
1257 event
.GetKeyCode() == 'C' && event
.ControlDown() )
1259 (void) CopySelection();
1263 void wxHtmlWindow::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1265 (void) CopySelection();
1268 void wxHtmlWindow::OnDoubleClick(wxMouseEvent
& event
)
1270 // select word under cursor:
1271 if ( IsSelectionEnabled() )
1273 SelectWord(CalcUnscrolledPosition(event
.GetPosition()));
1275 (void) CopySelection(Primary
);
1277 m_lastDoubleClick
= wxGetLocalTimeMillis();
1283 void wxHtmlWindow::SelectWord(const wxPoint
& pos
)
1287 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1291 m_selection
= new wxHtmlSelection();
1292 m_selection
->Set(cell
, cell
);
1293 RefreshRect(wxRect(CalcScrolledPosition(cell
->GetAbsPos()),
1294 wxSize(cell
->GetWidth(), cell
->GetHeight())));
1299 void wxHtmlWindow::SelectLine(const wxPoint
& pos
)
1303 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1306 // We use following heuristic to find a "line": let the line be all
1307 // cells in same container as the cell under mouse cursor that are
1308 // neither completely above nor completely bellow the clicked cell
1309 // (i.e. are likely to be words positioned on same line of text).
1311 int y1
= cell
->GetAbsPos().y
;
1312 int y2
= y1
+ cell
->GetHeight();
1314 const wxHtmlCell
*c
;
1315 const wxHtmlCell
*before
= NULL
;
1316 const wxHtmlCell
*after
= NULL
;
1318 // find last cell of line:
1319 for ( c
= cell
->GetNext(); c
; c
= c
->GetNext())
1321 y
= c
->GetAbsPos().y
;
1322 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1330 // find first cell of line:
1331 for ( c
= cell
->GetParent()->GetFirstChild();
1332 c
&& c
!= cell
; c
= c
->GetNext())
1334 y
= c
->GetAbsPos().y
;
1335 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1347 m_selection
= new wxHtmlSelection();
1348 m_selection
->Set(before
, after
);
1355 void wxHtmlWindow::SelectAll()
1360 m_selection
= new wxHtmlSelection();
1361 m_selection
->Set(m_Cell
->GetFirstTerminal(), m_Cell
->GetLastTerminal());
1366 #endif // wxUSE_CLIPBOARD
1370 IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor
,wxObject
)
1372 #if wxUSE_EXTENDED_RTTI
1373 IMPLEMENT_DYNAMIC_CLASS_XTI(wxHtmlWindow
, wxScrolledWindow
,"wx/html/htmlwin.h")
1375 wxBEGIN_PROPERTIES_TABLE(wxHtmlWindow
)
1378 style , wxHW_SCROLLBAR_AUTO
1379 borders , (dimension)
1383 wxEND_PROPERTIES_TABLE()
1385 wxBEGIN_HANDLERS_TABLE(wxHtmlWindow
)
1386 wxEND_HANDLERS_TABLE()
1388 wxCONSTRUCTOR_5( wxHtmlWindow
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
1390 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
1393 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
1394 EVT_SIZE(wxHtmlWindow::OnSize
)
1395 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseDown
)
1396 EVT_LEFT_UP(wxHtmlWindow::OnMouseUp
)
1397 EVT_RIGHT_UP(wxHtmlWindow::OnMouseUp
)
1398 EVT_MOTION(wxHtmlWindow::OnMouseMove
)
1399 EVT_ERASE_BACKGROUND(wxHtmlWindow::OnEraseBackground
)
1400 EVT_PAINT(wxHtmlWindow::OnPaint
)
1402 EVT_LEFT_DCLICK(wxHtmlWindow::OnDoubleClick
)
1403 EVT_ENTER_WINDOW(wxHtmlWindow::OnMouseEnter
)
1404 EVT_LEAVE_WINDOW(wxHtmlWindow::OnMouseLeave
)
1405 EVT_KEY_UP(wxHtmlWindow::OnKeyUp
)
1406 EVT_MENU(wxID_COPY
, wxHtmlWindow::OnCopy
)
1407 #endif // wxUSE_CLIPBOARD
1414 // A module to allow initialization/cleanup
1415 // without calling these functions from app.cpp or from
1416 // the user's application.
1418 class wxHtmlWinModule
: public wxModule
1420 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
1422 wxHtmlWinModule() : wxModule() {}
1423 bool OnInit() { return TRUE
; }
1424 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
1427 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
1430 // This hack forces the linker to always link in m_* files
1431 // (wxHTML doesn't work without handlers from these files)
1432 #include "wx/html/forcelnk.h"
1433 FORCE_WXHTML_MODULES()
1435 #endif // wxUSE_HTML