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"
40 #include "wx/arrimpl.cpp"
41 #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();
161 m_RelatedStatusBar
= -1;
162 m_RelatedFrame
= NULL
;
163 m_TitleFormat
= wxT("%s");
164 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
166 m_Parser
= new wxHtmlWinParser(this);
167 m_Parser
->SetFS(m_FS
);
170 m_History
= new wxHtmlHistoryArray
;
175 m_makingSelection
= false;
177 m_timerAutoScroll
= NULL
;
178 m_lastDoubleClick
= 0;
179 #endif // wxUSE_CLIPBOARD
183 bool wxHtmlWindow::Create(wxWindow
*parent
, wxWindowID id
,
184 const wxPoint
& pos
, const wxSize
& size
,
185 long style
, const wxString
& name
)
187 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
188 style
| wxVSCROLL
| wxHSCROLL
, name
))
192 SetPage(wxT("<html><body></body></html>"));
197 wxHtmlWindow::~wxHtmlWindow()
201 #endif // wxUSE_CLIPBOARD
210 WX_CLEAR_LIST(wxHtmlProcessorList
, *m_Processors
);
222 void wxHtmlWindow::SetRelatedFrame(wxFrame
* frame
, const wxString
& format
)
224 m_RelatedFrame
= frame
;
225 m_TitleFormat
= format
;
230 void wxHtmlWindow::SetRelatedStatusBar(int bar
)
232 m_RelatedStatusBar
= bar
;
237 void wxHtmlWindow::SetFonts(wxString normal_face
, wxString fixed_face
, const int *sizes
)
239 wxString op
= m_OpenedPage
;
241 m_Parser
->SetFonts(normal_face
, fixed_face
, sizes
);
242 // fonts changed => contents invalid, so reload the page:
243 SetPage(wxT("<html><body></body></html>"));
244 if (!op
.IsEmpty()) LoadPage(op
);
249 bool wxHtmlWindow::SetPage(const wxString
& source
)
251 wxString
newsrc(source
);
253 wxDELETE(m_selection
);
255 // pass HTML through registered processors:
256 if (m_Processors
|| m_GlobalProcessors
)
258 wxHtmlProcessorList::compatibility_iterator nodeL
, nodeG
;
261 nodeL
= (m_Processors
) ? m_Processors
->GetFirst() : wxHtmlProcessorList::compatibility_iterator();
262 nodeG
= (m_GlobalProcessors
) ? m_GlobalProcessors
->GetFirst() : wxHtmlProcessorList::compatibility_iterator();
264 // VS: there are two lists, global and local, both of them sorted by
265 // priority. Since we have to go through _both_ lists with
266 // decreasing priority, we "merge-sort" the lists on-line by
267 // processing that one of the two heads that has higher priority
268 // in every iteration
269 while (nodeL
|| nodeG
)
271 prL
= (nodeL
) ? nodeL
->GetData()->GetPriority() : -1;
272 prG
= (nodeG
) ? nodeG
->GetData()->GetPriority() : -1;
275 if (nodeL
->GetData()->IsEnabled())
276 newsrc
= nodeL
->GetData()->Process(newsrc
);
277 nodeL
= nodeL
->GetNext();
281 if (nodeG
->GetData()->IsEnabled())
282 newsrc
= nodeG
->GetData()->Process(newsrc
);
283 nodeG
= nodeG
->GetNext();
288 // ...and run the parser on it:
289 wxClientDC
*dc
= new wxClientDC(this);
290 dc
->SetMapMode(wxMM_TEXT
);
291 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
292 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
299 m_Cell
= (wxHtmlContainerCell
*) m_Parser
->Parse(newsrc
);
301 m_Cell
->SetIndent(m_Borders
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
302 m_Cell
->SetAlignHor(wxHTML_ALIGN_CENTER
);
304 if (m_tmpCanDrawLocks
== 0)
309 bool wxHtmlWindow::AppendToPage(const wxString
& source
)
311 return SetPage(*(GetParser()->GetSource()) + source
);
314 bool wxHtmlWindow::LoadPage(const wxString
& location
)
316 wxBusyCursor busyCursor
;
320 bool needs_refresh
= FALSE
;
323 if (m_HistoryOn
&& (m_HistoryPos
!= -1))
325 // store scroll position into history item:
327 GetViewStart(&x
, &y
);
328 (*m_History
)[m_HistoryPos
].SetPos(y
);
331 if (location
[0] == wxT('#'))
334 wxString anch
= location
.Mid(1) /*1 to end*/;
336 rt_val
= ScrollToAnchor(anch
);
339 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&& location
.BeforeFirst(wxT('#')) == m_OpenedPage
)
341 wxString anch
= location
.AfterFirst(wxT('#'));
343 rt_val
= ScrollToAnchor(anch
);
346 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&&
347 (m_FS
->GetPath() + location
.BeforeFirst(wxT('#'))) == m_OpenedPage
)
349 wxString anch
= location
.AfterFirst(wxT('#'));
351 rt_val
= ScrollToAnchor(anch
);
357 needs_refresh
= TRUE
;
359 if (m_RelatedStatusBar
!= -1)
361 m_RelatedFrame
->SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
365 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location
);
367 // try to interpret 'location' as filename instead of URL:
370 wxFileName
fn(location
);
371 wxString location2
= wxFileSystem::FileNameToURL(fn
);
372 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location2
);
377 wxLogError(_("Unable to open requested HTML document: %s"), location
.c_str());
384 wxList::compatibility_iterator node
;
385 wxString src
= wxEmptyString
;
387 if (m_RelatedStatusBar
!= -1)
389 wxString msg
= _("Loading : ") + location
;
390 m_RelatedFrame
->SetStatusText(msg
, m_RelatedStatusBar
);
394 node
= m_Filters
.GetFirst();
397 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
400 src
= h
->ReadFile(*f
);
403 node
= node
->GetNext();
405 if (src
== wxEmptyString
)
407 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
408 src
= m_DefaultFilter
->ReadFile(*f
);
411 m_FS
->ChangePathTo(f
->GetLocation());
412 rt_val
= SetPage(src
);
413 m_OpenedPage
= f
->GetLocation();
414 if (f
->GetAnchor() != wxEmptyString
)
416 ScrollToAnchor(f
->GetAnchor());
421 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
->SetStatusText(_("Done"), m_RelatedStatusBar
);
425 if (m_HistoryOn
) // add this page to history there:
427 int c
= m_History
->GetCount() - (m_HistoryPos
+ 1);
429 if (m_HistoryPos
< 0 ||
430 (*m_History
)[m_HistoryPos
].GetPage() != m_OpenedPage
||
431 (*m_History
)[m_HistoryPos
].GetAnchor() != m_OpenedAnchor
)
434 for (int i
= 0; i
< c
; i
++)
435 m_History
->RemoveAt(m_HistoryPos
);
436 m_History
->Add(new wxHtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
440 if (m_OpenedPageTitle
== wxEmptyString
)
441 OnSetTitle(wxFileNameFromPath(m_OpenedPage
));
455 bool wxHtmlWindow::LoadFile(const wxFileName
& filename
)
457 wxString url
= wxFileSystem::FileNameToURL(filename
);
458 return LoadPage(url
);
462 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
464 const wxHtmlCell
*c
= m_Cell
->Find(wxHTML_COND_ISANCHOR
, &anchor
);
467 wxLogWarning(_("HTML anchor %s does not exist."), anchor
.c_str());
474 for (y
= 0; c
!= NULL
; c
= c
->GetParent()) y
+= c
->GetPosY();
475 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
476 m_OpenedAnchor
= anchor
;
482 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
487 tit
.Printf(m_TitleFormat
, title
.c_str());
488 m_RelatedFrame
->SetTitle(tit
);
490 m_OpenedPageTitle
= title
;
497 void wxHtmlWindow::CreateLayout()
499 int ClientWidth
, ClientHeight
;
503 if (m_Style
& wxHW_SCROLLBAR_NEVER
)
505 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // always off
506 GetClientSize(&ClientWidth
, &ClientHeight
);
507 m_Cell
->Layout(ClientWidth
);
511 GetClientSize(&ClientWidth
, &ClientHeight
);
512 m_Cell
->Layout(ClientWidth
);
513 if (ClientHeight
< m_Cell
->GetHeight() + GetCharHeight())
516 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
517 m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
,
518 (m_Cell
->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
519 /*cheat: top-level frag is always container*/);
521 else /* we fit into window, no need for scrollbars */
523 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
524 GetClientSize(&ClientWidth
, &ClientHeight
);
525 m_Cell
->Layout(ClientWidth
); // ...and relayout
532 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
537 wxString p_fff
, p_ffn
;
539 if (path
!= wxEmptyString
)
541 oldpath
= cfg
->GetPath();
545 m_Borders
= cfg
->Read(wxT("wxHtmlWindow/Borders"), m_Borders
);
546 p_fff
= cfg
->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
547 p_ffn
= cfg
->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
548 for (int i
= 0; i
< 7; i
++)
550 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
551 p_fontsizes
[i
] = cfg
->Read(tmp
, m_Parser
->m_FontsSizes
[i
]);
553 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
555 if (path
!= wxEmptyString
)
556 cfg
->SetPath(oldpath
);
561 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
566 if (path
!= wxEmptyString
)
568 oldpath
= cfg
->GetPath();
572 cfg
->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders
);
573 cfg
->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
574 cfg
->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
575 for (int i
= 0; i
< 7; i
++)
577 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
578 cfg
->Write(tmp
, (long) m_Parser
->m_FontsSizes
[i
]);
581 if (path
!= wxEmptyString
)
582 cfg
->SetPath(oldpath
);
587 bool wxHtmlWindow::HistoryBack()
591 if (m_HistoryPos
< 1) return FALSE
;
593 // store scroll position into history item:
595 GetViewStart(&x
, &y
);
596 (*m_History
)[m_HistoryPos
].SetPos(y
);
598 // go to previous position:
601 l
= (*m_History
)[m_HistoryPos
].GetPage();
602 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
605 if (a
== wxEmptyString
) LoadPage(l
);
606 else LoadPage(l
+ wxT("#") + a
);
609 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
614 bool wxHtmlWindow::HistoryCanBack()
616 if (m_HistoryPos
< 1) return FALSE
;
621 bool wxHtmlWindow::HistoryForward()
625 if (m_HistoryPos
== -1) return FALSE
;
626 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
628 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
631 l
= (*m_History
)[m_HistoryPos
].GetPage();
632 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
635 if (a
== wxEmptyString
) LoadPage(l
);
636 else LoadPage(l
+ wxT("#") + a
);
639 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
644 bool wxHtmlWindow::HistoryCanForward()
646 if (m_HistoryPos
== -1) return FALSE
;
647 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
652 void wxHtmlWindow::HistoryClear()
658 void wxHtmlWindow::AddProcessor(wxHtmlProcessor
*processor
)
662 m_Processors
= new wxHtmlProcessorList
;
664 wxHtmlProcessorList::compatibility_iterator node
;
666 for (node
= m_Processors
->GetFirst(); node
; node
= node
->GetNext())
668 if (processor
->GetPriority() > node
->GetData()->GetPriority())
670 m_Processors
->Insert(node
, processor
);
674 m_Processors
->Append(processor
);
677 /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor
*processor
)
679 if (!m_GlobalProcessors
)
681 m_GlobalProcessors
= new wxHtmlProcessorList
;
683 wxHtmlProcessorList::compatibility_iterator node
;
685 for (node
= m_GlobalProcessors
->GetFirst(); node
; node
= node
->GetNext())
687 if (processor
->GetPriority() > node
->GetData()->GetPriority())
689 m_GlobalProcessors
->Insert(node
, processor
);
693 m_GlobalProcessors
->Append(processor
);
698 wxList
wxHtmlWindow::m_Filters
;
699 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
700 wxHtmlProcessorList
*wxHtmlWindow::m_GlobalProcessors
= NULL
;
702 void wxHtmlWindow::CleanUpStatics()
704 wxDELETE(m_DefaultFilter
);
705 WX_CLEAR_LIST(wxList
, m_Filters
);
706 if (m_GlobalProcessors
)
707 WX_CLEAR_LIST(wxHtmlProcessorList
, *m_GlobalProcessors
);
708 wxDELETE(m_GlobalProcessors
);
713 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
715 m_Filters
.Append(filter
);
719 bool wxHtmlWindow::IsSelectionEnabled() const
722 return !(m_Style
& wxHW_NO_SELECTION
);
730 wxString
wxHtmlWindow::SelectionToText()
733 return wxEmptyString
;
737 const wxHtmlCell
*end
= m_selection
->GetToCell();
739 wxHtmlTerminalCellsInterator
i(m_selection
->GetFromCell(), end
);
742 text
<< i
->ConvertToText(m_selection
);
745 const wxHtmlCell
*prev
= *i
;
748 if ( prev
->GetParent() != i
->GetParent() )
750 text
<< i
->ConvertToText(*i
== end
? m_selection
: NULL
);
757 #endif // wxUSE_CLIPBOARD
759 bool wxHtmlWindow::CopySelection(ClipboardType t
)
765 wxTheClipboard
->UsePrimarySelection(t
== Primary
);
767 // Primary selection exists only under X11, so don't do anything under
768 // the other platforms when we try to access it
770 // TODO: this should be abstracted at wxClipboard level!
773 #endif // __UNIX__/!__UNIX__
775 if ( wxTheClipboard
->Open() )
777 const wxString
txt(SelectionToText());
778 wxTheClipboard
->SetData(new wxTextDataObject(txt
));
779 wxTheClipboard
->Close();
780 wxLogTrace(_T("wxhtmlselection"),
781 _("Copied to clipboard:\"%s\""), txt
.c_str());
786 #endif // wxUSE_CLIPBOARD
792 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
794 const wxMouseEvent
*e
= link
.GetEvent();
795 if (e
== NULL
|| e
->LeftUp())
796 LoadPage(link
.GetHref());
799 void wxHtmlWindow::OnCellClicked(wxHtmlCell
*cell
,
800 wxCoord x
, wxCoord y
,
801 const wxMouseEvent
& event
)
803 wxCHECK_RET( cell
, _T("can't be called with NULL cell") );
805 cell
->OnMouseClick(this, x
, y
, event
);
808 void wxHtmlWindow::OnCellMouseHover(wxHtmlCell
* WXUNUSED(cell
),
809 wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
814 void wxHtmlWindow::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
818 void wxHtmlWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
822 if (m_tmpCanDrawLocks
> 0 || m_Cell
== NULL
) return;
825 GetViewStart(&x
, &y
);
826 wxRect rect
= GetUpdateRegion().GetBox();
827 wxSize sz
= GetSize();
831 m_backBuffer
= new wxBitmap(sz
.x
, sz
.y
);
832 dcm
.SelectObject(*m_backBuffer
);
833 dcm
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
836 dcm
.SetMapMode(wxMM_TEXT
);
837 dcm
.SetBackgroundMode(wxTRANSPARENT
);
839 wxHtmlRenderingInfo rinfo
;
840 wxDefaultHtmlRenderingStyle rstyle
;
841 rinfo
.SetSelection(m_selection
);
842 rinfo
.SetStyle(&rstyle
);
843 m_Cell
->Draw(dcm
, 0, 0,
844 y
* wxHTML_SCROLL_STEP
+ rect
.GetTop(),
845 y
* wxHTML_SCROLL_STEP
+ rect
.GetBottom(),
848 //#define DEBUG_HTML_SELECTION
849 #ifdef DEBUG_HTML_SELECTION
852 wxGetMousePosition(&xc
, &yc
);
853 ScreenToClient(&xc
, &yc
);
854 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
855 wxHtmlCell
*at
= m_Cell
->FindCellByPos(x
, y
);
857 m_Cell
->FindCellByPos(x
, y
, wxHTML_FIND_NEAREST_BEFORE
);
859 m_Cell
->FindCellByPos(x
, y
, wxHTML_FIND_NEAREST_AFTER
);
861 dcm
.SetBrush(*wxTRANSPARENT_BRUSH
);
862 dcm
.SetPen(*wxBLACK_PEN
);
864 dcm
.DrawRectangle(at
->GetAbsPos(),
865 wxSize(at
->GetWidth(),at
->GetHeight()));
866 dcm
.SetPen(*wxGREEN_PEN
);
868 dcm
.DrawRectangle(before
->GetAbsPos().x
+1, before
->GetAbsPos().y
+1,
869 before
->GetWidth()-2,before
->GetHeight()-2);
870 dcm
.SetPen(*wxRED_PEN
);
872 dcm
.DrawRectangle(after
->GetAbsPos().x
+2, after
->GetAbsPos().y
+2,
873 after
->GetWidth()-4,after
->GetHeight()-4);
877 dcm
.SetDeviceOrigin(0,0);
878 dc
.Blit(0, rect
.GetTop(),
879 sz
.x
, rect
.GetBottom() - rect
.GetTop() + 1,
887 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
889 wxDELETE(m_backBuffer
);
891 wxScrolledWindow::OnSize(event
);
894 // Recompute selection if necessary:
897 m_selection
->Set(m_selection
->GetFromCell(),
898 m_selection
->GetToCell());
899 m_selection
->ClearPrivPos();
906 void wxHtmlWindow::OnMouseMove(wxMouseEvent
& WXUNUSED(event
))
908 m_tmpMouseMoved
= true;
911 void wxHtmlWindow::OnMouseDown(wxMouseEvent
& event
)
914 if ( event
.LeftDown() && IsSelectionEnabled() )
916 const long TRIPLECLICK_LEN
= 200; // 0.2 sec after doubleclick
917 if ( wxGetLocalTimeMillis() - m_lastDoubleClick
<= TRIPLECLICK_LEN
)
919 SelectLine(CalcUnscrolledPosition(event
.GetPosition()));
921 (void) CopySelection();
925 m_makingSelection
= true;
929 wxDELETE(m_selection
);
932 m_tmpSelFromPos
= CalcUnscrolledPosition(event
.GetPosition());
933 m_tmpSelFromCell
= NULL
;
938 #endif // wxUSE_CLIPBOARD
941 void wxHtmlWindow::OnMouseUp(wxMouseEvent
& event
)
944 if ( m_makingSelection
)
947 m_makingSelection
= false;
949 // did the user move the mouse far enough from starting point?
950 if ( CopySelection(Primary
) )
952 // we don't want mouse up event that ended selecting to be
953 // handled as mouse click and e.g. follow hyperlink:
957 #endif // wxUSE_CLIPBOARD
962 wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
963 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
965 // check is needed because FindCellByPos returns terminal cell and
966 // containers may have empty borders -- in this case NULL will be
969 OnCellClicked(cell
, pos
.x
, pos
.y
, event
);
975 void wxHtmlWindow::OnInternalIdle()
977 wxWindow::OnInternalIdle();
979 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
))
981 #ifdef DEBUG_HTML_SELECTION
985 wxGetMousePosition(&xc
, &yc
);
986 ScreenToClient(&xc
, &yc
);
987 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
989 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(x
, y
);
991 // handle selection update:
992 if ( m_makingSelection
)
994 if ( !m_tmpSelFromCell
)
995 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
996 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
);
998 // NB: a trick - we adjust selFromPos to be upper left or bottom
999 // right corner of the first cell of the selection depending
1000 // on whether the mouse is moving to the right or to the left.
1001 // This gives us more "natural" behaviour when selecting
1002 // a line (specifically, first cell of the next line is not
1003 // included if you drag selection from left to right over
1006 if ( !m_tmpSelFromCell
)
1008 dirFromPos
= m_tmpSelFromPos
;
1012 dirFromPos
= m_tmpSelFromCell
->GetAbsPos();
1013 if ( x
< m_tmpSelFromPos
.x
)
1015 dirFromPos
.x
+= m_tmpSelFromCell
->GetWidth();
1016 dirFromPos
.y
+= m_tmpSelFromCell
->GetHeight();
1019 bool goingDown
= dirFromPos
.y
< y
||
1020 (dirFromPos
.y
== y
&& dirFromPos
.x
< x
);
1022 // determine selection span:
1023 if ( /*still*/ !m_tmpSelFromCell
)
1027 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1028 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1029 wxHTML_FIND_NEAREST_AFTER
);
1030 if (!m_tmpSelFromCell
)
1031 m_tmpSelFromCell
= m_Cell
->GetFirstTerminal();
1035 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1036 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1037 wxHTML_FIND_NEAREST_BEFORE
);
1038 if (!m_tmpSelFromCell
)
1039 m_tmpSelFromCell
= m_Cell
->GetLastTerminal();
1043 wxHtmlCell
*selcell
= cell
;
1048 selcell
= m_Cell
->FindCellByPos(x
, y
,
1049 wxHTML_FIND_NEAREST_BEFORE
);
1051 selcell
= m_Cell
->GetLastTerminal();
1055 selcell
= m_Cell
->FindCellByPos(x
, y
,
1056 wxHTML_FIND_NEAREST_AFTER
);
1058 selcell
= m_Cell
->GetFirstTerminal();
1062 // NB: it may *rarely* happen that the code above didn't find one
1063 // of the cells, e.g. if wxHtmlWindow doesn't contain any
1065 if ( selcell
&& m_tmpSelFromCell
)
1069 // start selecting only if mouse movement was big enough
1070 // (otherwise it was meant as mouse click, not selection):
1071 const int PRECISION
= 2;
1072 wxPoint diff
= m_tmpSelFromPos
- wxPoint(x
,y
);
1073 if (abs(diff
.x
) > PRECISION
|| abs(diff
.y
) > PRECISION
)
1075 m_selection
= new wxHtmlSelection();
1080 if ( m_tmpSelFromCell
->IsBefore(selcell
) )
1082 m_selection
->Set(m_tmpSelFromPos
, m_tmpSelFromCell
,
1083 wxPoint(x
,y
), selcell
); }
1086 m_selection
->Set(wxPoint(x
,y
), selcell
,
1087 m_tmpSelFromPos
, m_tmpSelFromCell
);
1089 m_selection
->ClearPrivPos();
1095 // handle cursor and status bar text changes:
1096 if ( cell
!= m_tmpLastCell
)
1098 wxHtmlLinkInfo
*lnk
= cell
? cell
->GetLink(x
, y
) : NULL
;
1101 cur
= cell
->GetCursor();
1103 cur
= *wxSTANDARD_CURSOR
;
1106 if (lnk
!= m_tmpLastLink
)
1110 if (m_RelatedStatusBar
!= -1)
1111 m_RelatedFrame
->SetStatusText(wxEmptyString
,
1112 m_RelatedStatusBar
);
1116 if (m_RelatedStatusBar
!= -1)
1117 m_RelatedFrame
->SetStatusText(lnk
->GetHref(),
1118 m_RelatedStatusBar
);
1120 m_tmpLastLink
= lnk
;
1123 m_tmpLastCell
= cell
;
1125 else // mouse moved but stayed in the same cell
1128 OnCellMouseHover(cell
, x
, y
);
1131 m_tmpMouseMoved
= FALSE
;
1136 void wxHtmlWindow::StopAutoScrolling()
1138 if ( m_timerAutoScroll
)
1140 wxDELETE(m_timerAutoScroll
);
1144 void wxHtmlWindow::OnMouseEnter(wxMouseEvent
& event
)
1146 StopAutoScrolling();
1150 void wxHtmlWindow::OnMouseLeave(wxMouseEvent
& event
)
1152 // don't prevent the usual processing of the event from taking place
1155 // when a captured mouse leave a scrolled window we start generate
1156 // scrolling events to allow, for example, extending selection beyond the
1157 // visible area in some controls
1158 if ( wxWindow::GetCapture() == this )
1160 // where is the mouse leaving?
1162 wxPoint pt
= event
.GetPosition();
1165 orient
= wxHORIZONTAL
;
1168 else if ( pt
.y
< 0 )
1170 orient
= wxVERTICAL
;
1173 else // we're lower or to the right of the window
1175 wxSize size
= GetClientSize();
1176 if ( pt
.x
> size
.x
)
1178 orient
= wxHORIZONTAL
;
1179 pos
= GetVirtualSize().x
/ wxHTML_SCROLL_STEP
;
1181 else if ( pt
.y
> size
.y
)
1183 orient
= wxVERTICAL
;
1184 pos
= GetVirtualSize().y
/ wxHTML_SCROLL_STEP
;
1186 else // this should be impossible
1188 // but seems to happen sometimes under wxMSW - maybe it's a bug
1189 // there but for now just ignore it
1191 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1197 // only start the auto scroll timer if the window can be scrolled in
1199 if ( !HasScrollbar(orient
) )
1202 delete m_timerAutoScroll
;
1203 m_timerAutoScroll
= new wxHtmlWinAutoScrollTimer
1206 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1207 : wxEVT_SCROLLWIN_LINEDOWN
,
1211 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1215 void wxHtmlWindow::OnKeyUp(wxKeyEvent
& event
)
1217 if ( IsSelectionEnabled() &&
1218 event
.GetKeyCode() == 'C' && event
.ControlDown() )
1220 (void) CopySelection();
1224 void wxHtmlWindow::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1226 (void) CopySelection();
1229 void wxHtmlWindow::OnDoubleClick(wxMouseEvent
& event
)
1231 // select word under cursor:
1232 if ( IsSelectionEnabled() )
1234 SelectWord(CalcUnscrolledPosition(event
.GetPosition()));
1236 (void) CopySelection(Primary
);
1238 m_lastDoubleClick
= wxGetLocalTimeMillis();
1244 void wxHtmlWindow::SelectWord(const wxPoint
& pos
)
1246 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1250 m_selection
= new wxHtmlSelection();
1251 m_selection
->Set(cell
, cell
);
1252 RefreshRect(wxRect(CalcScrolledPosition(cell
->GetAbsPos()),
1253 wxSize(cell
->GetWidth(), cell
->GetHeight())));
1257 void wxHtmlWindow::SelectLine(const wxPoint
& pos
)
1259 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1262 // We use following heuristic to find a "line": let the line be all
1263 // cells in same container as the cell under mouse cursor that are
1264 // neither completely above nor completely bellow the clicked cell
1265 // (i.e. are likely to be words positioned on same line of text).
1267 int y1
= cell
->GetAbsPos().y
;
1268 int y2
= y1
+ cell
->GetHeight();
1270 const wxHtmlCell
*c
;
1271 const wxHtmlCell
*before
= NULL
;
1272 const wxHtmlCell
*after
= NULL
;
1274 // find last cell of line:
1275 for ( c
= cell
->GetNext(); c
; c
= c
->GetNext())
1277 y
= c
->GetAbsPos().y
;
1278 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1286 // find first cell of line:
1287 for ( c
= cell
->GetParent()->GetFirstChild();
1288 c
&& c
!= cell
; c
= c
->GetNext())
1290 y
= c
->GetAbsPos().y
;
1291 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1303 m_selection
= new wxHtmlSelection();
1304 m_selection
->Set(before
, after
);
1309 #endif // wxUSE_CLIPBOARD
1313 IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor
,wxObject
)
1315 #if wxUSE_EXTENDED_RTTI
1316 IMPLEMENT_DYNAMIC_CLASS_XTI(wxHtmlWindow
, wxScrolledWindow
,"wx/html/htmlwin.h")
1318 wxBEGIN_PROPERTIES_TABLE(wxHtmlWindow
)
1321 style , wxHW_SCROLLBAR_AUTO
1322 borders , (dimension)
1326 wxEND_PROPERTIES_TABLE()
1328 wxBEGIN_HANDLERS_TABLE(wxHtmlWindow
)
1329 wxEND_HANDLERS_TABLE()
1331 wxCONSTRUCTOR_5( wxHtmlWindow
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
1333 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
1336 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
1337 EVT_SIZE(wxHtmlWindow::OnSize
)
1338 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseDown
)
1339 EVT_LEFT_UP(wxHtmlWindow::OnMouseUp
)
1340 EVT_RIGHT_UP(wxHtmlWindow::OnMouseUp
)
1341 EVT_MOTION(wxHtmlWindow::OnMouseMove
)
1342 EVT_ERASE_BACKGROUND(wxHtmlWindow::OnEraseBackground
)
1343 EVT_PAINT(wxHtmlWindow::OnPaint
)
1345 EVT_LEFT_DCLICK(wxHtmlWindow::OnDoubleClick
)
1346 EVT_ENTER_WINDOW(wxHtmlWindow::OnMouseEnter
)
1347 EVT_LEAVE_WINDOW(wxHtmlWindow::OnMouseLeave
)
1348 EVT_KEY_UP(wxHtmlWindow::OnKeyUp
)
1349 EVT_MENU(wxID_COPY
, wxHtmlWindow::OnCopy
)
1350 #endif // wxUSE_CLIPBOARD
1357 // A module to allow initialization/cleanup
1358 // without calling these functions from app.cpp or from
1359 // the user's application.
1361 class wxHtmlWinModule
: public wxModule
1363 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
1365 wxHtmlWinModule() : wxModule() {}
1366 bool OnInit() { return TRUE
; }
1367 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
1370 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
1373 // This hack forces the linker to always link in m_* files
1374 // (wxHTML doesn't work without handlers from these files)
1375 #include "wx/html/forcelnk.h"
1376 FORCE_WXHTML_MODULES()
1378 #endif // wxUSE_HTML