1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtmlWindow class for parsing & displaying HTML (implementation)
4 // Author: Vaclav Slavik
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #include "wx/wxprec.h"
13 #if wxUSE_HTML && wxUSE_STREAMS
22 #include "wx/dcclient.h"
26 #include "wx/html/htmlwin.h"
27 #include "wx/html/htmlproc.h"
29 #include "wx/clipbrd.h"
30 #include "wx/dataobj.h"
32 #include "wx/dcmemory.h"
33 #include "wx/settings.h"
35 #include "wx/arrimpl.cpp"
36 #include "wx/listimpl.cpp"
40 // ----------------------------------------------------------------------------
41 // wxHtmlWinAutoScrollTimer: the timer used to generate a stream of scroll
42 // events when a captured mouse is held outside the window
43 // ----------------------------------------------------------------------------
45 class wxHtmlWinAutoScrollTimer
: public wxTimer
48 wxHtmlWinAutoScrollTimer(wxScrolledWindow
*win
,
49 wxEventType eventTypeToSend
,
53 m_eventType
= eventTypeToSend
;
58 virtual void Notify();
61 wxScrolledWindow
*m_win
;
62 wxEventType m_eventType
;
66 DECLARE_NO_COPY_CLASS(wxHtmlWinAutoScrollTimer
)
69 void wxHtmlWinAutoScrollTimer::Notify()
71 // only do all this as long as the window is capturing the mouse
72 if ( wxWindow::GetCapture() != m_win
)
76 else // we still capture the mouse, continue generating events
78 // first scroll the window if we are allowed to do it
79 wxScrollWinEvent
event1(m_eventType
, m_pos
, m_orient
);
80 event1
.SetEventObject(m_win
);
81 if ( m_win
->GetEventHandler()->ProcessEvent(event1
) )
83 // and then send a pseudo mouse-move event to refresh the selection
84 wxMouseEvent
event2(wxEVT_MOTION
);
85 wxGetMousePosition(&event2
.m_x
, &event2
.m_y
);
87 // the mouse event coordinates should be client, not screen as
88 // returned by wxGetMousePosition
89 wxWindow
*parentTop
= m_win
;
90 while ( parentTop
->GetParent() )
91 parentTop
= parentTop
->GetParent();
92 wxPoint ptOrig
= parentTop
->GetPosition();
93 event2
.m_x
-= ptOrig
.x
;
94 event2
.m_y
-= ptOrig
.y
;
96 event2
.SetEventObject(m_win
);
98 // FIXME: we don't fill in the other members - ok?
99 m_win
->GetEventHandler()->ProcessEvent(event2
);
101 else // can't scroll further, stop
108 #endif // wxUSE_CLIPBOARD
112 //-----------------------------------------------------------------------------
114 //-----------------------------------------------------------------------------
116 // item of history list
117 class WXDLLIMPEXP_HTML wxHtmlHistoryItem
120 wxHtmlHistoryItem(const wxString
& p
, const wxString
& a
) {m_Page
= p
, m_Anchor
= a
, m_Pos
= 0;}
121 int GetPos() const {return m_Pos
;}
122 void SetPos(int p
) {m_Pos
= p
;}
123 const wxString
& GetPage() const {return m_Page
;}
124 const wxString
& GetAnchor() const {return m_Anchor
;}
133 //-----------------------------------------------------------------------------
134 // our private arrays:
135 //-----------------------------------------------------------------------------
137 WX_DECLARE_OBJARRAY(wxHtmlHistoryItem
, wxHtmlHistoryArray
);
138 WX_DEFINE_OBJARRAY(wxHtmlHistoryArray
)
140 WX_DECLARE_LIST(wxHtmlProcessor
, wxHtmlProcessorList
);
141 WX_DEFINE_LIST(wxHtmlProcessorList
)
143 //-----------------------------------------------------------------------------
144 // wxHtmlWindowMouseHelper
145 //-----------------------------------------------------------------------------
147 wxHtmlWindowMouseHelper::wxHtmlWindowMouseHelper(wxHtmlWindowInterface
*iface
)
148 : m_tmpMouseMoved(false),
155 void wxHtmlWindowMouseHelper::HandleMouseMoved()
157 m_tmpMouseMoved
= true;
160 bool wxHtmlWindowMouseHelper::HandleMouseClick(wxHtmlCell
*rootCell
,
162 const wxMouseEvent
& event
)
167 wxHtmlCell
*cell
= rootCell
->FindCellByPos(pos
.x
, pos
.y
);
168 // this check is needed because FindCellByPos returns terminal cell and
169 // containers may have empty borders -- in this case NULL will be
174 // adjust the coordinates to be relative to this cell:
175 wxPoint relpos
= pos
- cell
->GetAbsPos(rootCell
);
177 return OnCellClicked(cell
, relpos
.x
, relpos
.y
, event
);
180 void wxHtmlWindowMouseHelper::HandleIdle(wxHtmlCell
*rootCell
,
183 wxHtmlCell
*cell
= rootCell
? rootCell
->FindCellByPos(pos
.x
, pos
.y
) : NULL
;
185 if (cell
!= m_tmpLastCell
)
187 wxHtmlLinkInfo
*lnk
= NULL
;
190 // adjust the coordinates to be relative to this cell:
191 wxPoint relpos
= pos
- cell
->GetAbsPos(rootCell
);
192 lnk
= cell
->GetLink(relpos
.x
, relpos
.y
);
197 cur
= cell
->GetMouseCursor(m_interface
);
199 cur
= m_interface
->GetHTMLCursor(
200 wxHtmlWindowInterface::HTMLCursor_Default
);
202 m_interface
->GetHTMLWindow()->SetCursor(cur
);
204 if (lnk
!= m_tmpLastLink
)
207 m_interface
->SetHTMLStatusText(lnk
->GetHref());
209 m_interface
->SetHTMLStatusText(wxEmptyString
);
214 m_tmpLastCell
= cell
;
216 else // mouse moved but stayed in the same cell
220 OnCellMouseHover(cell
, pos
.x
, pos
.y
);
224 m_tmpMouseMoved
= false;
227 bool wxHtmlWindowMouseHelper::OnCellClicked(wxHtmlCell
*cell
,
228 wxCoord x
, wxCoord y
,
229 const wxMouseEvent
& event
)
231 wxCHECK_MSG( cell
, false, _T("can't be called with NULL cell") );
233 return cell
->ProcessMouseClick(m_interface
, wxPoint(x
, y
), event
);
236 void wxHtmlWindowMouseHelper::OnCellMouseHover(wxHtmlCell
* WXUNUSED(cell
),
243 //-----------------------------------------------------------------------------
245 //-----------------------------------------------------------------------------
247 wxList
wxHtmlWindow::m_Filters
;
248 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
249 wxHtmlProcessorList
*wxHtmlWindow::m_GlobalProcessors
= NULL
;
250 wxCursor
*wxHtmlWindow::ms_cursorLink
= NULL
;
251 wxCursor
*wxHtmlWindow::ms_cursorText
= NULL
;
253 void wxHtmlWindow::CleanUpStatics()
255 wxDELETE(m_DefaultFilter
);
256 WX_CLEAR_LIST(wxList
, m_Filters
);
257 if (m_GlobalProcessors
)
258 WX_CLEAR_LIST(wxHtmlProcessorList
, *m_GlobalProcessors
);
259 wxDELETE(m_GlobalProcessors
);
260 wxDELETE(ms_cursorLink
);
261 wxDELETE(ms_cursorText
);
264 void wxHtmlWindow::Init()
266 m_tmpCanDrawLocks
= 0;
267 m_FS
= new wxFileSystem();
269 m_RelatedStatusBar
= -1;
270 #endif // wxUSE_STATUSBAR
271 m_RelatedFrame
= NULL
;
272 m_TitleFormat
= wxT("%s");
273 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
275 m_Parser
= new wxHtmlWinParser(this);
276 m_Parser
->SetFS(m_FS
);
279 m_History
= new wxHtmlHistoryArray
;
284 m_makingSelection
= false;
286 m_timerAutoScroll
= NULL
;
287 m_lastDoubleClick
= 0;
288 #endif // wxUSE_CLIPBOARD
290 m_eraseBgInOnPaint
= false;
291 m_tmpSelFromCell
= NULL
;
294 bool wxHtmlWindow::Create(wxWindow
*parent
, wxWindowID id
,
295 const wxPoint
& pos
, const wxSize
& size
,
296 long style
, const wxString
& name
)
298 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
299 style
| wxVSCROLL
| wxHSCROLL
,
304 SetPage(wxT("<html><body></body></html>"));
309 wxHtmlWindow::~wxHtmlWindow()
313 #endif // wxUSE_CLIPBOARD
322 WX_CLEAR_LIST(wxHtmlProcessorList
, *m_Processors
);
334 void wxHtmlWindow::SetRelatedFrame(wxFrame
* frame
, const wxString
& format
)
336 m_RelatedFrame
= frame
;
337 m_TitleFormat
= format
;
343 void wxHtmlWindow::SetRelatedStatusBar(int bar
)
345 m_RelatedStatusBar
= bar
;
347 #endif // wxUSE_STATUSBAR
351 void wxHtmlWindow::SetFonts(const wxString
& normal_face
, const wxString
& fixed_face
, const int *sizes
)
353 m_Parser
->SetFonts(normal_face
, fixed_face
, sizes
);
355 // re-layout the page after changing fonts:
356 DoSetPage(*(m_Parser
->GetSource()));
359 void wxHtmlWindow::SetStandardFonts(int size
,
360 const wxString
& normal_face
,
361 const wxString
& fixed_face
)
363 m_Parser
->SetStandardFonts(size
, normal_face
, fixed_face
);
365 // re-layout the page after changing fonts:
366 DoSetPage(*(m_Parser
->GetSource()));
369 bool wxHtmlWindow::SetPage(const wxString
& source
)
371 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
372 return DoSetPage(source
);
375 bool wxHtmlWindow::DoSetPage(const wxString
& source
)
377 wxString
newsrc(source
);
379 wxDELETE(m_selection
);
381 // we will soon delete all the cells, so clear pointers to them:
382 m_tmpSelFromCell
= NULL
;
384 // pass HTML through registered processors:
385 if (m_Processors
|| m_GlobalProcessors
)
387 wxHtmlProcessorList::compatibility_iterator nodeL
, nodeG
;
391 nodeL
= m_Processors
->GetFirst();
392 if ( m_GlobalProcessors
)
393 nodeG
= m_GlobalProcessors
->GetFirst();
395 // VS: there are two lists, global and local, both of them sorted by
396 // priority. Since we have to go through _both_ lists with
397 // decreasing priority, we "merge-sort" the lists on-line by
398 // processing that one of the two heads that has higher priority
399 // in every iteration
400 while (nodeL
|| nodeG
)
402 prL
= (nodeL
) ? nodeL
->GetData()->GetPriority() : -1;
403 prG
= (nodeG
) ? nodeG
->GetData()->GetPriority() : -1;
406 if (nodeL
->GetData()->IsEnabled())
407 newsrc
= nodeL
->GetData()->Process(newsrc
);
408 nodeL
= nodeL
->GetNext();
412 if (nodeG
->GetData()->IsEnabled())
413 newsrc
= nodeG
->GetData()->Process(newsrc
);
414 nodeG
= nodeG
->GetNext();
419 // ...and run the parser on it:
420 wxClientDC
*dc
= new wxClientDC(this);
421 dc
->SetMapMode(wxMM_TEXT
);
422 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
423 SetBackgroundImage(wxNullBitmap
);
431 m_Cell
= (wxHtmlContainerCell
*) m_Parser
->Parse(newsrc
);
433 m_Cell
->SetIndent(m_Borders
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
434 m_Cell
->SetAlignHor(wxHTML_ALIGN_CENTER
);
436 if (m_tmpCanDrawLocks
== 0)
441 bool wxHtmlWindow::AppendToPage(const wxString
& source
)
443 return DoSetPage(*(GetParser()->GetSource()) + source
);
446 bool wxHtmlWindow::LoadPage(const wxString
& location
)
448 wxBusyCursor busyCursor
;
452 bool needs_refresh
= false;
455 if (m_HistoryOn
&& (m_HistoryPos
!= -1))
457 // store scroll position into history item:
459 GetViewStart(&x
, &y
);
460 (*m_History
)[m_HistoryPos
].SetPos(y
);
463 if (location
[0] == wxT('#'))
466 wxString anch
= location
.Mid(1) /*1 to end*/;
468 rt_val
= ScrollToAnchor(anch
);
471 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&& location
.BeforeFirst(wxT('#')) == m_OpenedPage
)
473 wxString anch
= location
.AfterFirst(wxT('#'));
475 rt_val
= ScrollToAnchor(anch
);
478 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&&
479 (m_FS
->GetPath() + location
.BeforeFirst(wxT('#'))) == m_OpenedPage
)
481 wxString anch
= location
.AfterFirst(wxT('#'));
483 rt_val
= ScrollToAnchor(anch
);
489 needs_refresh
= true;
492 if (m_RelatedStatusBar
!= -1)
494 m_RelatedFrame
->SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
497 #endif // wxUSE_STATUSBAR
499 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location
);
501 // try to interpret 'location' as filename instead of URL:
504 wxFileName
fn(location
);
505 wxString location2
= wxFileSystem::FileNameToURL(fn
);
506 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location2
);
511 wxLogError(_("Unable to open requested HTML document: %s"), location
.c_str());
518 wxList::compatibility_iterator node
;
519 wxString src
= wxEmptyString
;
522 if (m_RelatedStatusBar
!= -1)
524 wxString msg
= _("Loading : ") + location
;
525 m_RelatedFrame
->SetStatusText(msg
, m_RelatedStatusBar
);
528 #endif // wxUSE_STATUSBAR
530 node
= m_Filters
.GetFirst();
533 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
536 src
= h
->ReadFile(*f
);
539 node
= node
->GetNext();
541 if (src
== wxEmptyString
)
543 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
544 src
= m_DefaultFilter
->ReadFile(*f
);
547 m_FS
->ChangePathTo(f
->GetLocation());
548 rt_val
= SetPage(src
);
549 m_OpenedPage
= f
->GetLocation();
550 if (f
->GetAnchor() != wxEmptyString
)
552 ScrollToAnchor(f
->GetAnchor());
558 if (m_RelatedStatusBar
!= -1)
559 m_RelatedFrame
->SetStatusText(_("Done"), m_RelatedStatusBar
);
560 #endif // wxUSE_STATUSBAR
564 if (m_HistoryOn
) // add this page to history there:
566 int c
= m_History
->GetCount() - (m_HistoryPos
+ 1);
568 if (m_HistoryPos
< 0 ||
569 (*m_History
)[m_HistoryPos
].GetPage() != m_OpenedPage
||
570 (*m_History
)[m_HistoryPos
].GetAnchor() != m_OpenedAnchor
)
573 for (int i
= 0; i
< c
; i
++)
574 m_History
->RemoveAt(m_HistoryPos
);
575 m_History
->Add(new wxHtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
579 if (m_OpenedPageTitle
== wxEmptyString
)
580 OnSetTitle(wxFileNameFromPath(m_OpenedPage
));
594 bool wxHtmlWindow::LoadFile(const wxFileName
& filename
)
596 wxString url
= wxFileSystem::FileNameToURL(filename
);
597 return LoadPage(url
);
601 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
603 const wxHtmlCell
*c
= m_Cell
->Find(wxHTML_COND_ISANCHOR
, &anchor
);
606 wxLogWarning(_("HTML anchor %s does not exist."), anchor
.c_str());
613 for (y
= 0; c
!= NULL
; c
= c
->GetParent()) y
+= c
->GetPosY();
614 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
615 m_OpenedAnchor
= anchor
;
621 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
626 tit
.Printf(m_TitleFormat
, title
.c_str());
627 m_RelatedFrame
->SetTitle(tit
);
629 m_OpenedPageTitle
= title
;
636 void wxHtmlWindow::CreateLayout()
638 int ClientWidth
, ClientHeight
;
642 if (m_Style
& wxHW_SCROLLBAR_NEVER
)
644 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // always off
645 GetClientSize(&ClientWidth
, &ClientHeight
);
646 m_Cell
->Layout(ClientWidth
);
650 GetClientSize(&ClientWidth
, &ClientHeight
);
651 m_Cell
->Layout(ClientWidth
);
652 if (ClientHeight
< m_Cell
->GetHeight() + GetCharHeight())
655 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
656 m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
,
657 (m_Cell
->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
658 /*cheat: top-level frag is always container*/);
660 else /* we fit into window, no need for scrollbars */
662 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
663 GetClientSize(&ClientWidth
, &ClientHeight
);
664 m_Cell
->Layout(ClientWidth
); // ...and relayout
671 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
676 wxString p_fff
, p_ffn
;
678 if (path
!= wxEmptyString
)
680 oldpath
= cfg
->GetPath();
684 m_Borders
= cfg
->Read(wxT("wxHtmlWindow/Borders"), m_Borders
);
685 p_fff
= cfg
->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
686 p_ffn
= cfg
->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
687 for (int i
= 0; i
< 7; i
++)
689 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
690 p_fontsizes
[i
] = cfg
->Read(tmp
, m_Parser
->m_FontsSizes
[i
]);
692 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
694 if (path
!= wxEmptyString
)
695 cfg
->SetPath(oldpath
);
700 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
705 if (path
!= wxEmptyString
)
707 oldpath
= cfg
->GetPath();
711 cfg
->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders
);
712 cfg
->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
713 cfg
->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
714 for (int i
= 0; i
< 7; i
++)
716 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
717 cfg
->Write(tmp
, (long) m_Parser
->m_FontsSizes
[i
]);
720 if (path
!= wxEmptyString
)
721 cfg
->SetPath(oldpath
);
726 bool wxHtmlWindow::HistoryBack()
730 if (m_HistoryPos
< 1) return false;
732 // store scroll position into history item:
734 GetViewStart(&x
, &y
);
735 (*m_History
)[m_HistoryPos
].SetPos(y
);
737 // go to previous position:
740 l
= (*m_History
)[m_HistoryPos
].GetPage();
741 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
744 if (a
== wxEmptyString
) LoadPage(l
);
745 else LoadPage(l
+ wxT("#") + a
);
748 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
753 bool wxHtmlWindow::HistoryCanBack()
755 if (m_HistoryPos
< 1) return false;
760 bool wxHtmlWindow::HistoryForward()
764 if (m_HistoryPos
== -1) return false;
765 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return false;
767 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
770 l
= (*m_History
)[m_HistoryPos
].GetPage();
771 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
774 if (a
== wxEmptyString
) LoadPage(l
);
775 else LoadPage(l
+ wxT("#") + a
);
778 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
783 bool wxHtmlWindow::HistoryCanForward()
785 if (m_HistoryPos
== -1) return false;
786 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return false;
791 void wxHtmlWindow::HistoryClear()
797 void wxHtmlWindow::AddProcessor(wxHtmlProcessor
*processor
)
801 m_Processors
= new wxHtmlProcessorList
;
803 wxHtmlProcessorList::compatibility_iterator node
;
805 for (node
= m_Processors
->GetFirst(); node
; node
= node
->GetNext())
807 if (processor
->GetPriority() > node
->GetData()->GetPriority())
809 m_Processors
->Insert(node
, processor
);
813 m_Processors
->Append(processor
);
816 /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor
*processor
)
818 if (!m_GlobalProcessors
)
820 m_GlobalProcessors
= new wxHtmlProcessorList
;
822 wxHtmlProcessorList::compatibility_iterator node
;
824 for (node
= m_GlobalProcessors
->GetFirst(); node
; node
= node
->GetNext())
826 if (processor
->GetPriority() > node
->GetData()->GetPriority())
828 m_GlobalProcessors
->Insert(node
, processor
);
832 m_GlobalProcessors
->Append(processor
);
837 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
839 m_Filters
.Append(filter
);
843 bool wxHtmlWindow::IsSelectionEnabled() const
846 return !(m_Style
& wxHW_NO_SELECTION
);
854 wxString
wxHtmlWindow::DoSelectionToText(wxHtmlSelection
*sel
)
857 return wxEmptyString
;
861 const wxHtmlCell
*end
= sel
->GetToCell();
863 wxHtmlTerminalCellsInterator
i(sel
->GetFromCell(), end
);
866 text
<< i
->ConvertToText(sel
);
869 const wxHtmlCell
*prev
= *i
;
872 if ( prev
->GetParent() != i
->GetParent() )
874 text
<< i
->ConvertToText(*i
== end
? sel
: NULL
);
881 wxString
wxHtmlWindow::ToText()
886 sel
.Set(m_Cell
->GetFirstTerminal(), m_Cell
->GetLastTerminal());
887 return DoSelectionToText(&sel
);
890 return wxEmptyString
;
893 #endif // wxUSE_CLIPBOARD
895 bool wxHtmlWindow::CopySelection(ClipboardType t
)
900 #if defined(__UNIX__) && !defined(__WXMAC__)
901 wxTheClipboard
->UsePrimarySelection(t
== Primary
);
903 // Primary selection exists only under X11, so don't do anything under
904 // the other platforms when we try to access it
906 // TODO: this should be abstracted at wxClipboard level!
909 #endif // __UNIX__/!__UNIX__
911 if ( wxTheClipboard
->Open() )
913 const wxString
txt(SelectionToText());
914 wxTheClipboard
->SetData(new wxTextDataObject(txt
));
915 wxTheClipboard
->Close();
916 wxLogTrace(_T("wxhtmlselection"),
917 _("Copied to clipboard:\"%s\""), txt
.c_str());
924 #endif // wxUSE_CLIPBOARD
930 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
932 const wxMouseEvent
*e
= link
.GetEvent();
933 if (e
== NULL
|| e
->LeftUp())
934 LoadPage(link
.GetHref());
937 void wxHtmlWindow::OnEraseBackground(wxEraseEvent
& event
)
941 // don't even skip the event, if we don't have a bg bitmap we're going
942 // to overwrite background in OnPaint() below anyhow, so letting the
943 // default handling take place would only result in flicker, just set a
944 // flag to erase the background below
945 m_eraseBgInOnPaint
= true;
949 wxDC
& dc
= *event
.GetDC();
951 // if the image is not fully opaque, we have to erase the background before
952 // drawing it, however avoid doing it for opaque images as this would just
953 // result in extra flicker without any other effect as background is
954 // completely covered anyhow
955 if ( m_bmpBg
.GetMask() )
957 dc
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
961 const wxSize
sizeWin(GetClientSize());
962 const wxSize
sizeBmp(m_bmpBg
.GetWidth(), m_bmpBg
.GetHeight());
963 for ( wxCoord x
= 0; x
< sizeWin
.x
; x
+= sizeBmp
.x
)
965 for ( wxCoord y
= 0; y
< sizeWin
.y
; y
+= sizeBmp
.y
)
967 dc
.DrawBitmap(m_bmpBg
, x
, y
, true /* use mask */);
972 void wxHtmlWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
976 if (m_tmpCanDrawLocks
> 0 || m_Cell
== NULL
)
980 GetViewStart(&x
, &y
);
981 wxRect rect
= GetUpdateRegion().GetBox();
982 wxSize sz
= GetSize();
986 m_backBuffer
= new wxBitmap(sz
.x
, sz
.y
);
987 dcm
.SelectObject(*m_backBuffer
);
989 if ( m_eraseBgInOnPaint
)
991 dcm
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
994 m_eraseBgInOnPaint
= false;
996 else // someone has already erased the background, keep it
998 // preserve the existing background, otherwise we'd erase anything the
999 // user code had drawn in its EVT_ERASE_BACKGROUND handler when we do
1000 // the Blit back below
1001 dcm
.Blit(0, rect
.GetTop(),
1002 sz
.x
, rect
.GetBottom() - rect
.GetTop() + 1,
1008 dcm
.SetMapMode(wxMM_TEXT
);
1009 dcm
.SetBackgroundMode(wxTRANSPARENT
);
1011 wxHtmlRenderingInfo rinfo
;
1012 wxDefaultHtmlRenderingStyle rstyle
;
1013 rinfo
.SetSelection(m_selection
);
1014 rinfo
.SetStyle(&rstyle
);
1015 m_Cell
->Draw(dcm
, 0, 0,
1016 y
* wxHTML_SCROLL_STEP
+ rect
.GetTop(),
1017 y
* wxHTML_SCROLL_STEP
+ rect
.GetBottom(),
1020 //#define DEBUG_HTML_SELECTION
1021 #ifdef DEBUG_HTML_SELECTION
1024 wxGetMousePosition(&xc
, &yc
);
1025 ScreenToClient(&xc
, &yc
);
1026 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
1027 wxHtmlCell
*at
= m_Cell
->FindCellByPos(x
, y
);
1028 wxHtmlCell
*before
=
1029 m_Cell
->FindCellByPos(x
, y
, wxHTML_FIND_NEAREST_BEFORE
);
1031 m_Cell
->FindCellByPos(x
, y
, wxHTML_FIND_NEAREST_AFTER
);
1033 dcm
.SetBrush(*wxTRANSPARENT_BRUSH
);
1034 dcm
.SetPen(*wxBLACK_PEN
);
1036 dcm
.DrawRectangle(at
->GetAbsPos(),
1037 wxSize(at
->GetWidth(),at
->GetHeight()));
1038 dcm
.SetPen(*wxGREEN_PEN
);
1040 dcm
.DrawRectangle(before
->GetAbsPos().x
+1, before
->GetAbsPos().y
+1,
1041 before
->GetWidth()-2,before
->GetHeight()-2);
1042 dcm
.SetPen(*wxRED_PEN
);
1044 dcm
.DrawRectangle(after
->GetAbsPos().x
+2, after
->GetAbsPos().y
+2,
1045 after
->GetWidth()-4,after
->GetHeight()-4);
1049 dcm
.SetDeviceOrigin(0,0);
1050 dc
.Blit(0, rect
.GetTop(),
1051 sz
.x
, rect
.GetBottom() - rect
.GetTop() + 1,
1059 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
1061 wxDELETE(m_backBuffer
);
1063 wxScrolledWindow::OnSize(event
);
1066 // Recompute selection if necessary:
1069 m_selection
->Set(m_selection
->GetFromCell(),
1070 m_selection
->GetToCell());
1071 m_selection
->ClearPrivPos();
1078 void wxHtmlWindow::OnMouseMove(wxMouseEvent
& WXUNUSED(event
))
1080 wxHtmlWindowMouseHelper::HandleMouseMoved();
1083 void wxHtmlWindow::OnMouseDown(wxMouseEvent
& event
)
1086 if ( event
.LeftDown() && IsSelectionEnabled() )
1088 const long TRIPLECLICK_LEN
= 200; // 0.2 sec after doubleclick
1089 if ( wxGetLocalTimeMillis() - m_lastDoubleClick
<= TRIPLECLICK_LEN
)
1091 SelectLine(CalcUnscrolledPosition(event
.GetPosition()));
1093 (void) CopySelection();
1097 m_makingSelection
= true;
1101 wxDELETE(m_selection
);
1104 m_tmpSelFromPos
= CalcUnscrolledPosition(event
.GetPosition());
1105 m_tmpSelFromCell
= NULL
;
1112 #endif // wxUSE_CLIPBOARD
1115 void wxHtmlWindow::OnMouseUp(wxMouseEvent
& event
)
1118 if ( m_makingSelection
)
1121 m_makingSelection
= false;
1123 // did the user move the mouse far enough from starting point?
1124 if ( CopySelection(Primary
) )
1126 // we don't want mouse up event that ended selecting to be
1127 // handled as mouse click and e.g. follow hyperlink:
1131 #endif // wxUSE_CLIPBOARD
1135 wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
1136 wxHtmlWindowMouseHelper::HandleMouseClick(m_Cell
, pos
, event
);
1141 void wxHtmlWindow::OnInternalIdle()
1143 wxWindow::OnInternalIdle();
1145 if (m_Cell
!= NULL
&& DidMouseMove())
1147 #ifdef DEBUG_HTML_SELECTION
1151 wxGetMousePosition(&xc
, &yc
);
1152 ScreenToClient(&xc
, &yc
);
1153 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
1155 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(x
, y
);
1157 // handle selection update:
1158 if ( m_makingSelection
)
1160 if ( !m_tmpSelFromCell
)
1161 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1162 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
);
1164 // NB: a trick - we adjust selFromPos to be upper left or bottom
1165 // right corner of the first cell of the selection depending
1166 // on whether the mouse is moving to the right or to the left.
1167 // This gives us more "natural" behaviour when selecting
1168 // a line (specifically, first cell of the next line is not
1169 // included if you drag selection from left to right over
1172 if ( !m_tmpSelFromCell
)
1174 dirFromPos
= m_tmpSelFromPos
;
1178 dirFromPos
= m_tmpSelFromCell
->GetAbsPos();
1179 if ( x
< m_tmpSelFromPos
.x
)
1181 dirFromPos
.x
+= m_tmpSelFromCell
->GetWidth();
1182 dirFromPos
.y
+= m_tmpSelFromCell
->GetHeight();
1185 bool goingDown
= dirFromPos
.y
< y
||
1186 (dirFromPos
.y
== y
&& dirFromPos
.x
< x
);
1188 // determine selection span:
1189 if ( /*still*/ !m_tmpSelFromCell
)
1193 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1194 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1195 wxHTML_FIND_NEAREST_AFTER
);
1196 if (!m_tmpSelFromCell
)
1197 m_tmpSelFromCell
= m_Cell
->GetFirstTerminal();
1201 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1202 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1203 wxHTML_FIND_NEAREST_BEFORE
);
1204 if (!m_tmpSelFromCell
)
1205 m_tmpSelFromCell
= m_Cell
->GetLastTerminal();
1209 wxHtmlCell
*selcell
= cell
;
1214 selcell
= m_Cell
->FindCellByPos(x
, y
,
1215 wxHTML_FIND_NEAREST_BEFORE
);
1217 selcell
= m_Cell
->GetLastTerminal();
1221 selcell
= m_Cell
->FindCellByPos(x
, y
,
1222 wxHTML_FIND_NEAREST_AFTER
);
1224 selcell
= m_Cell
->GetFirstTerminal();
1228 // NB: it may *rarely* happen that the code above didn't find one
1229 // of the cells, e.g. if wxHtmlWindow doesn't contain any
1231 if ( selcell
&& m_tmpSelFromCell
)
1235 // start selecting only if mouse movement was big enough
1236 // (otherwise it was meant as mouse click, not selection):
1237 const int PRECISION
= 2;
1238 wxPoint diff
= m_tmpSelFromPos
- wxPoint(x
,y
);
1239 if (abs(diff
.x
) > PRECISION
|| abs(diff
.y
) > PRECISION
)
1241 m_selection
= new wxHtmlSelection();
1246 if ( m_tmpSelFromCell
->IsBefore(selcell
) )
1248 m_selection
->Set(m_tmpSelFromPos
, m_tmpSelFromCell
,
1249 wxPoint(x
,y
), selcell
); }
1252 m_selection
->Set(wxPoint(x
,y
), selcell
,
1253 m_tmpSelFromPos
, m_tmpSelFromCell
);
1255 m_selection
->ClearPrivPos();
1261 // handle cursor and status bar text changes:
1263 // NB: because we're passing in 'cell' and not 'm_Cell' (so that the
1264 // leaf cell lookup isn't done twice), we need to adjust the
1265 // position for the new root:
1266 wxPoint
posInCell(x
, y
);
1268 posInCell
-= cell
->GetAbsPos();
1269 wxHtmlWindowMouseHelper::HandleIdle(cell
, posInCell
);
1274 void wxHtmlWindow::StopAutoScrolling()
1276 if ( m_timerAutoScroll
)
1278 wxDELETE(m_timerAutoScroll
);
1282 void wxHtmlWindow::OnMouseEnter(wxMouseEvent
& event
)
1284 StopAutoScrolling();
1288 void wxHtmlWindow::OnMouseLeave(wxMouseEvent
& event
)
1290 // don't prevent the usual processing of the event from taking place
1293 // when a captured mouse leave a scrolled window we start generate
1294 // scrolling events to allow, for example, extending selection beyond the
1295 // visible area in some controls
1296 if ( wxWindow::GetCapture() == this )
1298 // where is the mouse leaving?
1300 wxPoint pt
= event
.GetPosition();
1303 orient
= wxHORIZONTAL
;
1306 else if ( pt
.y
< 0 )
1308 orient
= wxVERTICAL
;
1311 else // we're lower or to the right of the window
1313 wxSize size
= GetClientSize();
1314 if ( pt
.x
> size
.x
)
1316 orient
= wxHORIZONTAL
;
1317 pos
= GetVirtualSize().x
/ wxHTML_SCROLL_STEP
;
1319 else if ( pt
.y
> size
.y
)
1321 orient
= wxVERTICAL
;
1322 pos
= GetVirtualSize().y
/ wxHTML_SCROLL_STEP
;
1324 else // this should be impossible
1326 // but seems to happen sometimes under wxMSW - maybe it's a bug
1327 // there but for now just ignore it
1329 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1335 // only start the auto scroll timer if the window can be scrolled in
1337 if ( !HasScrollbar(orient
) )
1340 delete m_timerAutoScroll
;
1341 m_timerAutoScroll
= new wxHtmlWinAutoScrollTimer
1344 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1345 : wxEVT_SCROLLWIN_LINEDOWN
,
1349 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1353 void wxHtmlWindow::OnKeyUp(wxKeyEvent
& event
)
1355 if ( IsSelectionEnabled() && event
.GetKeyCode() == 'C' && event
.CmdDown() )
1357 (void) CopySelection();
1361 void wxHtmlWindow::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1363 (void) CopySelection();
1366 void wxHtmlWindow::OnDoubleClick(wxMouseEvent
& event
)
1368 // select word under cursor:
1369 if ( IsSelectionEnabled() )
1371 SelectWord(CalcUnscrolledPosition(event
.GetPosition()));
1373 (void) CopySelection(Primary
);
1375 m_lastDoubleClick
= wxGetLocalTimeMillis();
1381 void wxHtmlWindow::SelectWord(const wxPoint
& pos
)
1385 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1389 m_selection
= new wxHtmlSelection();
1390 m_selection
->Set(cell
, cell
);
1391 RefreshRect(wxRect(CalcScrolledPosition(cell
->GetAbsPos()),
1392 wxSize(cell
->GetWidth(), cell
->GetHeight())));
1397 void wxHtmlWindow::SelectLine(const wxPoint
& pos
)
1401 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1404 // We use following heuristic to find a "line": let the line be all
1405 // cells in same container as the cell under mouse cursor that are
1406 // neither completely above nor completely bellow the clicked cell
1407 // (i.e. are likely to be words positioned on same line of text).
1409 int y1
= cell
->GetAbsPos().y
;
1410 int y2
= y1
+ cell
->GetHeight();
1412 const wxHtmlCell
*c
;
1413 const wxHtmlCell
*before
= NULL
;
1414 const wxHtmlCell
*after
= NULL
;
1416 // find last cell of line:
1417 for ( c
= cell
->GetNext(); c
; c
= c
->GetNext())
1419 y
= c
->GetAbsPos().y
;
1420 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1428 // find first cell of line:
1429 for ( c
= cell
->GetParent()->GetFirstChild();
1430 c
&& c
!= cell
; c
= c
->GetNext())
1432 y
= c
->GetAbsPos().y
;
1433 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1445 m_selection
= new wxHtmlSelection();
1446 m_selection
->Set(before
, after
);
1453 void wxHtmlWindow::SelectAll()
1458 m_selection
= new wxHtmlSelection();
1459 m_selection
->Set(m_Cell
->GetFirstTerminal(), m_Cell
->GetLastTerminal());
1464 #endif // wxUSE_CLIPBOARD
1468 IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor
,wxObject
)
1470 #if wxUSE_EXTENDED_RTTI
1471 IMPLEMENT_DYNAMIC_CLASS_XTI(wxHtmlWindow
, wxScrolledWindow
,"wx/html/htmlwin.h")
1473 wxBEGIN_PROPERTIES_TABLE(wxHtmlWindow
)
1476 style , wxHW_SCROLLBAR_AUTO
1477 borders , (dimension)
1481 wxEND_PROPERTIES_TABLE()
1483 wxBEGIN_HANDLERS_TABLE(wxHtmlWindow
)
1484 wxEND_HANDLERS_TABLE()
1486 wxCONSTRUCTOR_5( wxHtmlWindow
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
1488 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
1491 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
1492 EVT_SIZE(wxHtmlWindow::OnSize
)
1493 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseDown
)
1494 EVT_LEFT_UP(wxHtmlWindow::OnMouseUp
)
1495 EVT_RIGHT_UP(wxHtmlWindow::OnMouseUp
)
1496 EVT_MOTION(wxHtmlWindow::OnMouseMove
)
1497 EVT_ERASE_BACKGROUND(wxHtmlWindow::OnEraseBackground
)
1498 EVT_PAINT(wxHtmlWindow::OnPaint
)
1500 EVT_LEFT_DCLICK(wxHtmlWindow::OnDoubleClick
)
1501 EVT_ENTER_WINDOW(wxHtmlWindow::OnMouseEnter
)
1502 EVT_LEAVE_WINDOW(wxHtmlWindow::OnMouseLeave
)
1503 EVT_KEY_UP(wxHtmlWindow::OnKeyUp
)
1504 EVT_MENU(wxID_COPY
, wxHtmlWindow::OnCopy
)
1505 #endif // wxUSE_CLIPBOARD
1508 //-----------------------------------------------------------------------------
1509 // wxHtmlWindowInterface implementation in wxHtmlWindow
1510 //-----------------------------------------------------------------------------
1512 void wxHtmlWindow::SetHTMLWindowTitle(const wxString
& title
)
1517 void wxHtmlWindow::OnHTMLLinkClicked(const wxHtmlLinkInfo
& link
)
1519 OnLinkClicked(link
);
1522 wxHtmlOpeningStatus
wxHtmlWindow::OnHTMLOpeningURL(wxHtmlURLType type
,
1523 const wxString
& url
,
1524 wxString
*redirect
) const
1526 return OnOpeningURL(type
, url
, redirect
);
1529 wxPoint
wxHtmlWindow::HTMLCoordsToWindow(wxHtmlCell
*WXUNUSED(cell
),
1530 const wxPoint
& pos
) const
1532 return CalcScrolledPosition(pos
);
1535 wxWindow
* wxHtmlWindow::GetHTMLWindow()
1540 wxColour
wxHtmlWindow::GetHTMLBackgroundColour() const
1542 return GetBackgroundColour();
1545 void wxHtmlWindow::SetHTMLBackgroundColour(const wxColour
& clr
)
1547 SetBackgroundColour(clr
);
1550 void wxHtmlWindow::SetHTMLBackgroundImage(const wxBitmap
& bmpBg
)
1552 SetBackgroundImage(bmpBg
);
1555 void wxHtmlWindow::SetHTMLStatusText(const wxString
& text
)
1558 if (m_RelatedStatusBar
!= -1)
1559 m_RelatedFrame
->SetStatusText(text
, m_RelatedStatusBar
);
1560 #endif // wxUSE_STATUSBAR
1564 wxCursor
wxHtmlWindow::GetDefaultHTMLCursor(HTMLCursor type
)
1568 case HTMLCursor_Link
:
1569 if ( !ms_cursorLink
)
1570 ms_cursorLink
= new wxCursor(wxCURSOR_HAND
);
1571 return *ms_cursorLink
;
1573 case HTMLCursor_Text
:
1574 if ( !ms_cursorText
)
1575 ms_cursorText
= new wxCursor(wxCURSOR_IBEAM
);
1576 return *ms_cursorText
;
1578 case HTMLCursor_Default
:
1580 return *wxSTANDARD_CURSOR
;
1584 wxCursor
wxHtmlWindow::GetHTMLCursor(HTMLCursor type
) const
1586 return GetDefaultHTMLCursor(type
);
1590 //-----------------------------------------------------------------------------
1592 //-----------------------------------------------------------------------------
1594 // A module to allow initialization/cleanup
1595 // without calling these functions from app.cpp or from
1596 // the user's application.
1598 class wxHtmlWinModule
: public wxModule
1600 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
1602 wxHtmlWinModule() : wxModule() {}
1603 bool OnInit() { return true; }
1604 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
1607 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
1610 // This hack forces the linker to always link in m_* files
1611 // (wxHTML doesn't work without handlers from these files)
1612 #include "wx/html/forcelnk.h"
1613 FORCE_WXHTML_MODULES()
1615 #endif // wxUSE_HTML