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
,
193 SetPage(wxT("<html><body></body></html>"));
198 wxHtmlWindow::~wxHtmlWindow()
202 #endif // wxUSE_CLIPBOARD
211 WX_CLEAR_LIST(wxHtmlProcessorList
, *m_Processors
);
223 void wxHtmlWindow::SetRelatedFrame(wxFrame
* frame
, const wxString
& format
)
225 m_RelatedFrame
= frame
;
226 m_TitleFormat
= format
;
231 void wxHtmlWindow::SetRelatedStatusBar(int bar
)
233 m_RelatedStatusBar
= bar
;
238 void wxHtmlWindow::SetFonts(wxString normal_face
, wxString fixed_face
, const int *sizes
)
240 wxString op
= m_OpenedPage
;
242 m_Parser
->SetFonts(normal_face
, fixed_face
, sizes
);
243 // fonts changed => contents invalid, so reload the page:
244 SetPage(wxT("<html><body></body></html>"));
245 if (!op
.IsEmpty()) LoadPage(op
);
250 bool wxHtmlWindow::SetPage(const wxString
& source
)
252 wxString
newsrc(source
);
254 wxDELETE(m_selection
);
256 // pass HTML through registered processors:
257 if (m_Processors
|| m_GlobalProcessors
)
259 wxHtmlProcessorList::compatibility_iterator nodeL
, nodeG
;
262 nodeL
= (m_Processors
) ? m_Processors
->GetFirst() : wxHtmlProcessorList::compatibility_iterator();
263 nodeG
= (m_GlobalProcessors
) ? m_GlobalProcessors
->GetFirst() : wxHtmlProcessorList::compatibility_iterator();
265 // VS: there are two lists, global and local, both of them sorted by
266 // priority. Since we have to go through _both_ lists with
267 // decreasing priority, we "merge-sort" the lists on-line by
268 // processing that one of the two heads that has higher priority
269 // in every iteration
270 while (nodeL
|| nodeG
)
272 prL
= (nodeL
) ? nodeL
->GetData()->GetPriority() : -1;
273 prG
= (nodeG
) ? nodeG
->GetData()->GetPriority() : -1;
276 if (nodeL
->GetData()->IsEnabled())
277 newsrc
= nodeL
->GetData()->Process(newsrc
);
278 nodeL
= nodeL
->GetNext();
282 if (nodeG
->GetData()->IsEnabled())
283 newsrc
= nodeG
->GetData()->Process(newsrc
);
284 nodeG
= nodeG
->GetNext();
289 // ...and run the parser on it:
290 wxClientDC
*dc
= new wxClientDC(this);
291 dc
->SetMapMode(wxMM_TEXT
);
292 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
293 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
300 m_Cell
= (wxHtmlContainerCell
*) m_Parser
->Parse(newsrc
);
302 m_Cell
->SetIndent(m_Borders
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
303 m_Cell
->SetAlignHor(wxHTML_ALIGN_CENTER
);
305 if (m_tmpCanDrawLocks
== 0)
310 bool wxHtmlWindow::AppendToPage(const wxString
& source
)
312 return SetPage(*(GetParser()->GetSource()) + source
);
315 bool wxHtmlWindow::LoadPage(const wxString
& location
)
317 wxBusyCursor busyCursor
;
321 bool needs_refresh
= FALSE
;
324 if (m_HistoryOn
&& (m_HistoryPos
!= -1))
326 // store scroll position into history item:
328 GetViewStart(&x
, &y
);
329 (*m_History
)[m_HistoryPos
].SetPos(y
);
332 if (location
[0] == wxT('#'))
335 wxString anch
= location
.Mid(1) /*1 to end*/;
337 rt_val
= ScrollToAnchor(anch
);
340 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&& location
.BeforeFirst(wxT('#')) == m_OpenedPage
)
342 wxString anch
= location
.AfterFirst(wxT('#'));
344 rt_val
= ScrollToAnchor(anch
);
347 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&&
348 (m_FS
->GetPath() + location
.BeforeFirst(wxT('#'))) == m_OpenedPage
)
350 wxString anch
= location
.AfterFirst(wxT('#'));
352 rt_val
= ScrollToAnchor(anch
);
358 needs_refresh
= TRUE
;
360 if (m_RelatedStatusBar
!= -1)
362 m_RelatedFrame
->SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
366 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location
);
368 // try to interpret 'location' as filename instead of URL:
371 wxFileName
fn(location
);
372 wxString location2
= wxFileSystem::FileNameToURL(fn
);
373 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location2
);
378 wxLogError(_("Unable to open requested HTML document: %s"), location
.c_str());
385 wxList::compatibility_iterator node
;
386 wxString src
= wxEmptyString
;
388 if (m_RelatedStatusBar
!= -1)
390 wxString msg
= _("Loading : ") + location
;
391 m_RelatedFrame
->SetStatusText(msg
, m_RelatedStatusBar
);
395 node
= m_Filters
.GetFirst();
398 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
401 src
= h
->ReadFile(*f
);
404 node
= node
->GetNext();
406 if (src
== wxEmptyString
)
408 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
409 src
= m_DefaultFilter
->ReadFile(*f
);
412 m_FS
->ChangePathTo(f
->GetLocation());
413 rt_val
= SetPage(src
);
414 m_OpenedPage
= f
->GetLocation();
415 if (f
->GetAnchor() != wxEmptyString
)
417 ScrollToAnchor(f
->GetAnchor());
422 if (m_RelatedStatusBar
!= -1) m_RelatedFrame
->SetStatusText(_("Done"), m_RelatedStatusBar
);
426 if (m_HistoryOn
) // add this page to history there:
428 int c
= m_History
->GetCount() - (m_HistoryPos
+ 1);
430 if (m_HistoryPos
< 0 ||
431 (*m_History
)[m_HistoryPos
].GetPage() != m_OpenedPage
||
432 (*m_History
)[m_HistoryPos
].GetAnchor() != m_OpenedAnchor
)
435 for (int i
= 0; i
< c
; i
++)
436 m_History
->RemoveAt(m_HistoryPos
);
437 m_History
->Add(new wxHtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
441 if (m_OpenedPageTitle
== wxEmptyString
)
442 OnSetTitle(wxFileNameFromPath(m_OpenedPage
));
456 bool wxHtmlWindow::LoadFile(const wxFileName
& filename
)
458 wxString url
= wxFileSystem::FileNameToURL(filename
);
459 return LoadPage(url
);
463 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
465 const wxHtmlCell
*c
= m_Cell
->Find(wxHTML_COND_ISANCHOR
, &anchor
);
468 wxLogWarning(_("HTML anchor %s does not exist."), anchor
.c_str());
475 for (y
= 0; c
!= NULL
; c
= c
->GetParent()) y
+= c
->GetPosY();
476 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
477 m_OpenedAnchor
= anchor
;
483 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
488 tit
.Printf(m_TitleFormat
, title
.c_str());
489 m_RelatedFrame
->SetTitle(tit
);
491 m_OpenedPageTitle
= title
;
498 void wxHtmlWindow::CreateLayout()
500 int ClientWidth
, ClientHeight
;
504 if (m_Style
& wxHW_SCROLLBAR_NEVER
)
506 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // always off
507 GetClientSize(&ClientWidth
, &ClientHeight
);
508 m_Cell
->Layout(ClientWidth
);
512 GetClientSize(&ClientWidth
, &ClientHeight
);
513 m_Cell
->Layout(ClientWidth
);
514 if (ClientHeight
< m_Cell
->GetHeight() + GetCharHeight())
517 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
518 m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
,
519 (m_Cell
->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
520 /*cheat: top-level frag is always container*/);
522 else /* we fit into window, no need for scrollbars */
524 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
525 GetClientSize(&ClientWidth
, &ClientHeight
);
526 m_Cell
->Layout(ClientWidth
); // ...and relayout
533 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
538 wxString p_fff
, p_ffn
;
540 if (path
!= wxEmptyString
)
542 oldpath
= cfg
->GetPath();
546 m_Borders
= cfg
->Read(wxT("wxHtmlWindow/Borders"), m_Borders
);
547 p_fff
= cfg
->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
548 p_ffn
= cfg
->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
549 for (int i
= 0; i
< 7; i
++)
551 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
552 p_fontsizes
[i
] = cfg
->Read(tmp
, m_Parser
->m_FontsSizes
[i
]);
554 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
556 if (path
!= wxEmptyString
)
557 cfg
->SetPath(oldpath
);
562 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
567 if (path
!= wxEmptyString
)
569 oldpath
= cfg
->GetPath();
573 cfg
->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders
);
574 cfg
->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
575 cfg
->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
576 for (int i
= 0; i
< 7; i
++)
578 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
579 cfg
->Write(tmp
, (long) m_Parser
->m_FontsSizes
[i
]);
582 if (path
!= wxEmptyString
)
583 cfg
->SetPath(oldpath
);
588 bool wxHtmlWindow::HistoryBack()
592 if (m_HistoryPos
< 1) return FALSE
;
594 // store scroll position into history item:
596 GetViewStart(&x
, &y
);
597 (*m_History
)[m_HistoryPos
].SetPos(y
);
599 // go to previous position:
602 l
= (*m_History
)[m_HistoryPos
].GetPage();
603 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
606 if (a
== wxEmptyString
) LoadPage(l
);
607 else LoadPage(l
+ wxT("#") + a
);
610 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
615 bool wxHtmlWindow::HistoryCanBack()
617 if (m_HistoryPos
< 1) return FALSE
;
622 bool wxHtmlWindow::HistoryForward()
626 if (m_HistoryPos
== -1) return FALSE
;
627 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
629 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
632 l
= (*m_History
)[m_HistoryPos
].GetPage();
633 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
636 if (a
== wxEmptyString
) LoadPage(l
);
637 else LoadPage(l
+ wxT("#") + a
);
640 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
645 bool wxHtmlWindow::HistoryCanForward()
647 if (m_HistoryPos
== -1) return FALSE
;
648 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
653 void wxHtmlWindow::HistoryClear()
659 void wxHtmlWindow::AddProcessor(wxHtmlProcessor
*processor
)
663 m_Processors
= new wxHtmlProcessorList
;
665 wxHtmlProcessorList::compatibility_iterator node
;
667 for (node
= m_Processors
->GetFirst(); node
; node
= node
->GetNext())
669 if (processor
->GetPriority() > node
->GetData()->GetPriority())
671 m_Processors
->Insert(node
, processor
);
675 m_Processors
->Append(processor
);
678 /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor
*processor
)
680 if (!m_GlobalProcessors
)
682 m_GlobalProcessors
= new wxHtmlProcessorList
;
684 wxHtmlProcessorList::compatibility_iterator node
;
686 for (node
= m_GlobalProcessors
->GetFirst(); node
; node
= node
->GetNext())
688 if (processor
->GetPriority() > node
->GetData()->GetPriority())
690 m_GlobalProcessors
->Insert(node
, processor
);
694 m_GlobalProcessors
->Append(processor
);
699 wxList
wxHtmlWindow::m_Filters
;
700 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
701 wxHtmlProcessorList
*wxHtmlWindow::m_GlobalProcessors
= NULL
;
703 void wxHtmlWindow::CleanUpStatics()
705 wxDELETE(m_DefaultFilter
);
706 WX_CLEAR_LIST(wxList
, m_Filters
);
707 if (m_GlobalProcessors
)
708 WX_CLEAR_LIST(wxHtmlProcessorList
, *m_GlobalProcessors
);
709 wxDELETE(m_GlobalProcessors
);
714 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
716 m_Filters
.Append(filter
);
720 bool wxHtmlWindow::IsSelectionEnabled() const
723 return !(m_Style
& wxHW_NO_SELECTION
);
731 wxString
wxHtmlWindow::DoSelectionToText(wxHtmlSelection
*sel
)
734 return wxEmptyString
;
738 const wxHtmlCell
*end
= sel
->GetToCell();
740 wxHtmlTerminalCellsInterator
i(sel
->GetFromCell(), end
);
743 text
<< i
->ConvertToText(sel
);
746 const wxHtmlCell
*prev
= *i
;
749 if ( prev
->GetParent() != i
->GetParent() )
751 text
<< i
->ConvertToText(*i
== end
? sel
: NULL
);
758 wxString
wxHtmlWindow::ToText()
763 sel
.Set(m_Cell
->GetFirstTerminal(), m_Cell
->GetLastTerminal());
764 return DoSelectionToText(&sel
);
767 return wxEmptyString
;
770 #endif // wxUSE_CLIPBOARD
772 bool wxHtmlWindow::CopySelection(ClipboardType t
)
778 wxTheClipboard
->UsePrimarySelection(t
== Primary
);
780 // Primary selection exists only under X11, so don't do anything under
781 // the other platforms when we try to access it
783 // TODO: this should be abstracted at wxClipboard level!
786 #endif // __UNIX__/!__UNIX__
788 if ( wxTheClipboard
->Open() )
790 const wxString
txt(SelectionToText());
791 wxTheClipboard
->SetData(new wxTextDataObject(txt
));
792 wxTheClipboard
->Close();
793 wxLogTrace(_T("wxhtmlselection"),
794 _("Copied to clipboard:\"%s\""), txt
.c_str());
799 #endif // wxUSE_CLIPBOARD
805 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
807 const wxMouseEvent
*e
= link
.GetEvent();
808 if (e
== NULL
|| e
->LeftUp())
809 LoadPage(link
.GetHref());
812 void wxHtmlWindow::OnCellClicked(wxHtmlCell
*cell
,
813 wxCoord x
, wxCoord y
,
814 const wxMouseEvent
& event
)
816 wxCHECK_RET( cell
, _T("can't be called with NULL cell") );
818 cell
->OnMouseClick(this, x
, y
, event
);
821 void wxHtmlWindow::OnCellMouseHover(wxHtmlCell
* WXUNUSED(cell
),
822 wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
827 void wxHtmlWindow::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
831 void wxHtmlWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
835 if (m_tmpCanDrawLocks
> 0 || m_Cell
== NULL
) return;
838 GetViewStart(&x
, &y
);
839 wxRect rect
= GetUpdateRegion().GetBox();
840 wxSize sz
= GetSize();
844 m_backBuffer
= new wxBitmap(sz
.x
, sz
.y
);
845 dcm
.SelectObject(*m_backBuffer
);
846 dcm
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
849 dcm
.SetMapMode(wxMM_TEXT
);
850 dcm
.SetBackgroundMode(wxTRANSPARENT
);
852 wxHtmlRenderingInfo rinfo
;
853 wxDefaultHtmlRenderingStyle rstyle
;
854 rinfo
.SetSelection(m_selection
);
855 rinfo
.SetStyle(&rstyle
);
856 m_Cell
->Draw(dcm
, 0, 0,
857 y
* wxHTML_SCROLL_STEP
+ rect
.GetTop(),
858 y
* wxHTML_SCROLL_STEP
+ rect
.GetBottom(),
861 //#define DEBUG_HTML_SELECTION
862 #ifdef DEBUG_HTML_SELECTION
865 wxGetMousePosition(&xc
, &yc
);
866 ScreenToClient(&xc
, &yc
);
867 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
868 wxHtmlCell
*at
= m_Cell
->FindCellByPos(x
, y
);
870 m_Cell
->FindCellByPos(x
, y
, wxHTML_FIND_NEAREST_BEFORE
);
872 m_Cell
->FindCellByPos(x
, y
, wxHTML_FIND_NEAREST_AFTER
);
874 dcm
.SetBrush(*wxTRANSPARENT_BRUSH
);
875 dcm
.SetPen(*wxBLACK_PEN
);
877 dcm
.DrawRectangle(at
->GetAbsPos(),
878 wxSize(at
->GetWidth(),at
->GetHeight()));
879 dcm
.SetPen(*wxGREEN_PEN
);
881 dcm
.DrawRectangle(before
->GetAbsPos().x
+1, before
->GetAbsPos().y
+1,
882 before
->GetWidth()-2,before
->GetHeight()-2);
883 dcm
.SetPen(*wxRED_PEN
);
885 dcm
.DrawRectangle(after
->GetAbsPos().x
+2, after
->GetAbsPos().y
+2,
886 after
->GetWidth()-4,after
->GetHeight()-4);
890 dcm
.SetDeviceOrigin(0,0);
891 dc
.Blit(0, rect
.GetTop(),
892 sz
.x
, rect
.GetBottom() - rect
.GetTop() + 1,
900 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
902 wxDELETE(m_backBuffer
);
904 wxScrolledWindow::OnSize(event
);
907 // Recompute selection if necessary:
910 m_selection
->Set(m_selection
->GetFromCell(),
911 m_selection
->GetToCell());
912 m_selection
->ClearPrivPos();
919 void wxHtmlWindow::OnMouseMove(wxMouseEvent
& WXUNUSED(event
))
921 m_tmpMouseMoved
= true;
924 void wxHtmlWindow::OnMouseDown(wxMouseEvent
& event
)
927 if ( event
.LeftDown() && IsSelectionEnabled() )
929 const long TRIPLECLICK_LEN
= 200; // 0.2 sec after doubleclick
930 if ( wxGetLocalTimeMillis() - m_lastDoubleClick
<= TRIPLECLICK_LEN
)
932 SelectLine(CalcUnscrolledPosition(event
.GetPosition()));
934 (void) CopySelection();
938 m_makingSelection
= true;
942 wxDELETE(m_selection
);
945 m_tmpSelFromPos
= CalcUnscrolledPosition(event
.GetPosition());
946 m_tmpSelFromCell
= NULL
;
951 #endif // wxUSE_CLIPBOARD
954 void wxHtmlWindow::OnMouseUp(wxMouseEvent
& event
)
957 if ( m_makingSelection
)
960 m_makingSelection
= false;
962 // did the user move the mouse far enough from starting point?
963 if ( CopySelection(Primary
) )
965 // we don't want mouse up event that ended selecting to be
966 // handled as mouse click and e.g. follow hyperlink:
970 #endif // wxUSE_CLIPBOARD
975 wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
976 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
978 // check is needed because FindCellByPos returns terminal cell and
979 // containers may have empty borders -- in this case NULL will be
982 OnCellClicked(cell
, pos
.x
, pos
.y
, event
);
988 void wxHtmlWindow::OnInternalIdle()
990 wxWindow::OnInternalIdle();
992 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
))
994 #ifdef DEBUG_HTML_SELECTION
998 wxGetMousePosition(&xc
, &yc
);
999 ScreenToClient(&xc
, &yc
);
1000 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
1002 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(x
, y
);
1004 // handle selection update:
1005 if ( m_makingSelection
)
1007 if ( !m_tmpSelFromCell
)
1008 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1009 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
);
1011 // NB: a trick - we adjust selFromPos to be upper left or bottom
1012 // right corner of the first cell of the selection depending
1013 // on whether the mouse is moving to the right or to the left.
1014 // This gives us more "natural" behaviour when selecting
1015 // a line (specifically, first cell of the next line is not
1016 // included if you drag selection from left to right over
1019 if ( !m_tmpSelFromCell
)
1021 dirFromPos
= m_tmpSelFromPos
;
1025 dirFromPos
= m_tmpSelFromCell
->GetAbsPos();
1026 if ( x
< m_tmpSelFromPos
.x
)
1028 dirFromPos
.x
+= m_tmpSelFromCell
->GetWidth();
1029 dirFromPos
.y
+= m_tmpSelFromCell
->GetHeight();
1032 bool goingDown
= dirFromPos
.y
< y
||
1033 (dirFromPos
.y
== y
&& dirFromPos
.x
< x
);
1035 // determine selection span:
1036 if ( /*still*/ !m_tmpSelFromCell
)
1040 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1041 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1042 wxHTML_FIND_NEAREST_AFTER
);
1043 if (!m_tmpSelFromCell
)
1044 m_tmpSelFromCell
= m_Cell
->GetFirstTerminal();
1048 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1049 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1050 wxHTML_FIND_NEAREST_BEFORE
);
1051 if (!m_tmpSelFromCell
)
1052 m_tmpSelFromCell
= m_Cell
->GetLastTerminal();
1056 wxHtmlCell
*selcell
= cell
;
1061 selcell
= m_Cell
->FindCellByPos(x
, y
,
1062 wxHTML_FIND_NEAREST_BEFORE
);
1064 selcell
= m_Cell
->GetLastTerminal();
1068 selcell
= m_Cell
->FindCellByPos(x
, y
,
1069 wxHTML_FIND_NEAREST_AFTER
);
1071 selcell
= m_Cell
->GetFirstTerminal();
1075 // NB: it may *rarely* happen that the code above didn't find one
1076 // of the cells, e.g. if wxHtmlWindow doesn't contain any
1078 if ( selcell
&& m_tmpSelFromCell
)
1082 // start selecting only if mouse movement was big enough
1083 // (otherwise it was meant as mouse click, not selection):
1084 const int PRECISION
= 2;
1085 wxPoint diff
= m_tmpSelFromPos
- wxPoint(x
,y
);
1086 if (abs(diff
.x
) > PRECISION
|| abs(diff
.y
) > PRECISION
)
1088 m_selection
= new wxHtmlSelection();
1093 if ( m_tmpSelFromCell
->IsBefore(selcell
) )
1095 m_selection
->Set(m_tmpSelFromPos
, m_tmpSelFromCell
,
1096 wxPoint(x
,y
), selcell
); }
1099 m_selection
->Set(wxPoint(x
,y
), selcell
,
1100 m_tmpSelFromPos
, m_tmpSelFromCell
);
1102 m_selection
->ClearPrivPos();
1108 // handle cursor and status bar text changes:
1109 if ( cell
!= m_tmpLastCell
)
1111 wxHtmlLinkInfo
*lnk
= cell
? cell
->GetLink(x
, y
) : NULL
;
1114 cur
= cell
->GetCursor();
1116 cur
= *wxSTANDARD_CURSOR
;
1119 if (lnk
!= m_tmpLastLink
)
1123 if (m_RelatedStatusBar
!= -1)
1124 m_RelatedFrame
->SetStatusText(wxEmptyString
,
1125 m_RelatedStatusBar
);
1129 if (m_RelatedStatusBar
!= -1)
1130 m_RelatedFrame
->SetStatusText(lnk
->GetHref(),
1131 m_RelatedStatusBar
);
1133 m_tmpLastLink
= lnk
;
1136 m_tmpLastCell
= cell
;
1138 else // mouse moved but stayed in the same cell
1141 OnCellMouseHover(cell
, x
, y
);
1144 m_tmpMouseMoved
= FALSE
;
1149 void wxHtmlWindow::StopAutoScrolling()
1151 if ( m_timerAutoScroll
)
1153 wxDELETE(m_timerAutoScroll
);
1157 void wxHtmlWindow::OnMouseEnter(wxMouseEvent
& event
)
1159 StopAutoScrolling();
1163 void wxHtmlWindow::OnMouseLeave(wxMouseEvent
& event
)
1165 // don't prevent the usual processing of the event from taking place
1168 // when a captured mouse leave a scrolled window we start generate
1169 // scrolling events to allow, for example, extending selection beyond the
1170 // visible area in some controls
1171 if ( wxWindow::GetCapture() == this )
1173 // where is the mouse leaving?
1175 wxPoint pt
= event
.GetPosition();
1178 orient
= wxHORIZONTAL
;
1181 else if ( pt
.y
< 0 )
1183 orient
= wxVERTICAL
;
1186 else // we're lower or to the right of the window
1188 wxSize size
= GetClientSize();
1189 if ( pt
.x
> size
.x
)
1191 orient
= wxHORIZONTAL
;
1192 pos
= GetVirtualSize().x
/ wxHTML_SCROLL_STEP
;
1194 else if ( pt
.y
> size
.y
)
1196 orient
= wxVERTICAL
;
1197 pos
= GetVirtualSize().y
/ wxHTML_SCROLL_STEP
;
1199 else // this should be impossible
1201 // but seems to happen sometimes under wxMSW - maybe it's a bug
1202 // there but for now just ignore it
1204 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1210 // only start the auto scroll timer if the window can be scrolled in
1212 if ( !HasScrollbar(orient
) )
1215 delete m_timerAutoScroll
;
1216 m_timerAutoScroll
= new wxHtmlWinAutoScrollTimer
1219 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1220 : wxEVT_SCROLLWIN_LINEDOWN
,
1224 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1228 void wxHtmlWindow::OnKeyUp(wxKeyEvent
& event
)
1230 if ( IsSelectionEnabled() &&
1231 event
.GetKeyCode() == 'C' && event
.ControlDown() )
1233 (void) CopySelection();
1237 void wxHtmlWindow::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1239 (void) CopySelection();
1242 void wxHtmlWindow::OnDoubleClick(wxMouseEvent
& event
)
1244 // select word under cursor:
1245 if ( IsSelectionEnabled() )
1247 SelectWord(CalcUnscrolledPosition(event
.GetPosition()));
1249 (void) CopySelection(Primary
);
1251 m_lastDoubleClick
= wxGetLocalTimeMillis();
1257 void wxHtmlWindow::SelectWord(const wxPoint
& pos
)
1261 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1265 m_selection
= new wxHtmlSelection();
1266 m_selection
->Set(cell
, cell
);
1267 RefreshRect(wxRect(CalcScrolledPosition(cell
->GetAbsPos()),
1268 wxSize(cell
->GetWidth(), cell
->GetHeight())));
1273 void wxHtmlWindow::SelectLine(const wxPoint
& pos
)
1277 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1280 // We use following heuristic to find a "line": let the line be all
1281 // cells in same container as the cell under mouse cursor that are
1282 // neither completely above nor completely bellow the clicked cell
1283 // (i.e. are likely to be words positioned on same line of text).
1285 int y1
= cell
->GetAbsPos().y
;
1286 int y2
= y1
+ cell
->GetHeight();
1288 const wxHtmlCell
*c
;
1289 const wxHtmlCell
*before
= NULL
;
1290 const wxHtmlCell
*after
= NULL
;
1292 // find last cell of line:
1293 for ( c
= cell
->GetNext(); c
; c
= c
->GetNext())
1295 y
= c
->GetAbsPos().y
;
1296 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1304 // find first cell of line:
1305 for ( c
= cell
->GetParent()->GetFirstChild();
1306 c
&& c
!= cell
; c
= c
->GetNext())
1308 y
= c
->GetAbsPos().y
;
1309 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1321 m_selection
= new wxHtmlSelection();
1322 m_selection
->Set(before
, after
);
1329 void wxHtmlWindow::SelectAll()
1334 m_selection
= new wxHtmlSelection();
1335 m_selection
->Set(m_Cell
->GetFirstTerminal(), m_Cell
->GetLastTerminal());
1340 #endif // wxUSE_CLIPBOARD
1344 IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor
,wxObject
)
1346 #if wxUSE_EXTENDED_RTTI
1347 IMPLEMENT_DYNAMIC_CLASS_XTI(wxHtmlWindow
, wxScrolledWindow
,"wx/html/htmlwin.h")
1349 wxBEGIN_PROPERTIES_TABLE(wxHtmlWindow
)
1352 style , wxHW_SCROLLBAR_AUTO
1353 borders , (dimension)
1357 wxEND_PROPERTIES_TABLE()
1359 wxBEGIN_HANDLERS_TABLE(wxHtmlWindow
)
1360 wxEND_HANDLERS_TABLE()
1362 wxCONSTRUCTOR_5( wxHtmlWindow
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
1364 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
1367 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
1368 EVT_SIZE(wxHtmlWindow::OnSize
)
1369 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseDown
)
1370 EVT_LEFT_UP(wxHtmlWindow::OnMouseUp
)
1371 EVT_RIGHT_UP(wxHtmlWindow::OnMouseUp
)
1372 EVT_MOTION(wxHtmlWindow::OnMouseMove
)
1373 EVT_ERASE_BACKGROUND(wxHtmlWindow::OnEraseBackground
)
1374 EVT_PAINT(wxHtmlWindow::OnPaint
)
1376 EVT_LEFT_DCLICK(wxHtmlWindow::OnDoubleClick
)
1377 EVT_ENTER_WINDOW(wxHtmlWindow::OnMouseEnter
)
1378 EVT_LEAVE_WINDOW(wxHtmlWindow::OnMouseLeave
)
1379 EVT_KEY_UP(wxHtmlWindow::OnKeyUp
)
1380 EVT_MENU(wxID_COPY
, wxHtmlWindow::OnCopy
)
1381 #endif // wxUSE_CLIPBOARD
1388 // A module to allow initialization/cleanup
1389 // without calling these functions from app.cpp or from
1390 // the user's application.
1392 class wxHtmlWinModule
: public wxModule
1394 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
1396 wxHtmlWinModule() : wxModule() {}
1397 bool OnInit() { return TRUE
; }
1398 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
1401 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
1404 // This hack forces the linker to always link in m_* files
1405 // (wxHTML doesn't work without handlers from these files)
1406 #include "wx/html/forcelnk.h"
1407 FORCE_WXHTML_MODULES()
1409 #endif // wxUSE_HTML