1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/html/htmlwin.cpp
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"
16 #if wxUSE_HTML && wxUSE_STREAMS
22 #include "wx/dcclient.h"
24 #include "wx/dcmemory.h"
26 #include "wx/settings.h"
27 #include "wx/dataobj.h"
30 #include "wx/html/htmlwin.h"
31 #include "wx/html/htmlproc.h"
32 #include "wx/clipbrd.h"
34 #include "wx/arrimpl.cpp"
35 #include "wx/listimpl.cpp"
38 IMPLEMENT_DYNAMIC_CLASS(wxHtmlLinkEvent
, wxCommandEvent
)
39 IMPLEMENT_DYNAMIC_CLASS(wxHtmlCellEvent
, wxCommandEvent
)
41 DEFINE_EVENT_TYPE(wxEVT_COMMAND_HTML_CELL_CLICKED
)
42 DEFINE_EVENT_TYPE(wxEVT_COMMAND_HTML_CELL_HOVER
)
43 DEFINE_EVENT_TYPE(wxEVT_COMMAND_HTML_LINK_CLICKED
)
47 // ----------------------------------------------------------------------------
48 // wxHtmlWinAutoScrollTimer: the timer used to generate a stream of scroll
49 // events when a captured mouse is held outside the window
50 // ----------------------------------------------------------------------------
52 class wxHtmlWinAutoScrollTimer
: public wxTimer
55 wxHtmlWinAutoScrollTimer(wxScrolledWindow
*win
,
56 wxEventType eventTypeToSend
,
60 m_eventType
= eventTypeToSend
;
65 virtual void Notify();
68 wxScrolledWindow
*m_win
;
69 wxEventType m_eventType
;
73 DECLARE_NO_COPY_CLASS(wxHtmlWinAutoScrollTimer
)
76 void wxHtmlWinAutoScrollTimer::Notify()
78 // only do all this as long as the window is capturing the mouse
79 if ( wxWindow::GetCapture() != m_win
)
83 else // we still capture the mouse, continue generating events
85 // first scroll the window if we are allowed to do it
86 wxScrollWinEvent
event1(m_eventType
, m_pos
, m_orient
);
87 event1
.SetEventObject(m_win
);
88 if ( m_win
->GetEventHandler()->ProcessEvent(event1
) )
90 // and then send a pseudo mouse-move event to refresh the selection
91 wxMouseEvent
event2(wxEVT_MOTION
);
92 wxGetMousePosition(&event2
.m_x
, &event2
.m_y
);
94 // the mouse event coordinates should be client, not screen as
95 // returned by wxGetMousePosition
96 wxWindow
*parentTop
= m_win
;
97 while ( parentTop
->GetParent() )
98 parentTop
= parentTop
->GetParent();
99 wxPoint ptOrig
= parentTop
->GetPosition();
100 event2
.m_x
-= ptOrig
.x
;
101 event2
.m_y
-= ptOrig
.y
;
103 event2
.SetEventObject(m_win
);
105 // FIXME: we don't fill in the other members - ok?
106 m_win
->GetEventHandler()->ProcessEvent(event2
);
108 else // can't scroll further, stop
115 #endif // wxUSE_CLIPBOARD
119 //-----------------------------------------------------------------------------
121 //-----------------------------------------------------------------------------
123 // item of history list
124 class WXDLLIMPEXP_HTML wxHtmlHistoryItem
127 wxHtmlHistoryItem(const wxString
& p
, const wxString
& a
) {m_Page
= p
, m_Anchor
= a
, m_Pos
= 0;}
128 int GetPos() const {return m_Pos
;}
129 void SetPos(int p
) {m_Pos
= p
;}
130 const wxString
& GetPage() const {return m_Page
;}
131 const wxString
& GetAnchor() const {return m_Anchor
;}
140 //-----------------------------------------------------------------------------
141 // our private arrays:
142 //-----------------------------------------------------------------------------
144 WX_DECLARE_OBJARRAY(wxHtmlHistoryItem
, wxHtmlHistoryArray
);
145 WX_DEFINE_OBJARRAY(wxHtmlHistoryArray
)
147 WX_DECLARE_LIST(wxHtmlProcessor
, wxHtmlProcessorList
);
148 WX_DEFINE_LIST(wxHtmlProcessorList
)
150 //-----------------------------------------------------------------------------
151 // wxHtmlWindowMouseHelper
152 //-----------------------------------------------------------------------------
154 wxHtmlWindowMouseHelper::wxHtmlWindowMouseHelper(wxHtmlWindowInterface
*iface
)
155 : m_tmpMouseMoved(false),
162 void wxHtmlWindowMouseHelper::HandleMouseMoved()
164 m_tmpMouseMoved
= true;
167 bool wxHtmlWindowMouseHelper::HandleMouseClick(wxHtmlCell
*rootCell
,
169 const wxMouseEvent
& event
)
174 wxHtmlCell
*cell
= rootCell
->FindCellByPos(pos
.x
, pos
.y
);
175 // this check is needed because FindCellByPos returns terminal cell and
176 // containers may have empty borders -- in this case NULL will be
181 // adjust the coordinates to be relative to this cell:
182 wxPoint relpos
= pos
- cell
->GetAbsPos(rootCell
);
184 return OnCellClicked(cell
, relpos
.x
, relpos
.y
, event
);
187 void wxHtmlWindowMouseHelper::HandleIdle(wxHtmlCell
*rootCell
,
190 wxHtmlCell
*cell
= rootCell
? rootCell
->FindCellByPos(pos
.x
, pos
.y
) : NULL
;
192 if (cell
!= m_tmpLastCell
)
194 wxHtmlLinkInfo
*lnk
= NULL
;
197 // adjust the coordinates to be relative to this cell:
198 wxPoint relpos
= pos
- cell
->GetAbsPos(rootCell
);
199 lnk
= cell
->GetLink(relpos
.x
, relpos
.y
);
204 cur
= cell
->GetMouseCursor(m_interface
);
206 cur
= m_interface
->GetHTMLCursor(
207 wxHtmlWindowInterface::HTMLCursor_Default
);
209 m_interface
->GetHTMLWindow()->SetCursor(cur
);
211 if (lnk
!= m_tmpLastLink
)
214 m_interface
->SetHTMLStatusText(lnk
->GetHref());
216 m_interface
->SetHTMLStatusText(wxEmptyString
);
221 m_tmpLastCell
= cell
;
223 else // mouse moved but stayed in the same cell
227 OnCellMouseHover(cell
, pos
.x
, pos
.y
);
231 m_tmpMouseMoved
= false;
234 bool wxHtmlWindowMouseHelper::OnCellClicked(wxHtmlCell
*cell
,
235 wxCoord x
, wxCoord y
,
236 const wxMouseEvent
& event
)
238 wxHtmlCellEvent
ev(wxEVT_COMMAND_HTML_CELL_CLICKED
,
239 m_interface
->GetHTMLWindow()->GetId(),
240 cell
, wxPoint(x
,y
), event
);
242 if (!m_interface
->GetHTMLWindow()->GetEventHandler()->ProcessEvent(ev
))
244 // if the event wasn't handled, do the default processing here:
246 wxASSERT_MSG( cell
, _T("can't be called with NULL cell") );
248 cell
->ProcessMouseClick(m_interface
, ev
.GetPoint(), ev
.GetMouseEvent());
251 // true if a link was clicked, false otherwise
252 return ev
.GetLinkClicked();
255 void wxHtmlWindowMouseHelper::OnCellMouseHover(wxHtmlCell
* cell
,
259 wxHtmlCellEvent
ev(wxEVT_COMMAND_HTML_CELL_HOVER
,
260 m_interface
->GetHTMLWindow()->GetId(),
261 cell
, wxPoint(x
,y
), wxMouseEvent());
262 m_interface
->GetHTMLWindow()->GetEventHandler()->ProcessEvent(ev
);
268 //-----------------------------------------------------------------------------
270 //-----------------------------------------------------------------------------
272 wxList
wxHtmlWindow::m_Filters
;
273 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
274 wxHtmlProcessorList
*wxHtmlWindow::m_GlobalProcessors
= NULL
;
275 wxCursor
*wxHtmlWindow::ms_cursorLink
= NULL
;
276 wxCursor
*wxHtmlWindow::ms_cursorText
= NULL
;
278 void wxHtmlWindow::CleanUpStatics()
280 wxDELETE(m_DefaultFilter
);
281 WX_CLEAR_LIST(wxList
, m_Filters
);
282 if (m_GlobalProcessors
)
283 WX_CLEAR_LIST(wxHtmlProcessorList
, *m_GlobalProcessors
);
284 wxDELETE(m_GlobalProcessors
);
285 wxDELETE(ms_cursorLink
);
286 wxDELETE(ms_cursorText
);
289 void wxHtmlWindow::Init()
291 m_tmpCanDrawLocks
= 0;
292 m_FS
= new wxFileSystem();
294 m_RelatedStatusBar
= -1;
295 #endif // wxUSE_STATUSBAR
296 m_RelatedFrame
= NULL
;
297 m_TitleFormat
= wxT("%s");
298 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
300 m_Parser
= new wxHtmlWinParser(this);
301 m_Parser
->SetFS(m_FS
);
304 m_History
= new wxHtmlHistoryArray
;
308 m_makingSelection
= false;
310 m_timerAutoScroll
= NULL
;
311 m_lastDoubleClick
= 0;
312 #endif // wxUSE_CLIPBOARD
314 m_eraseBgInOnPaint
= false;
315 m_tmpSelFromCell
= NULL
;
318 bool wxHtmlWindow::Create(wxWindow
*parent
, wxWindowID id
,
319 const wxPoint
& pos
, const wxSize
& size
,
320 long style
, const wxString
& name
)
322 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
323 style
| wxVSCROLL
| wxHSCROLL
,
327 SetPage(wxT("<html><body></body></html>"));
332 wxHtmlWindow::~wxHtmlWindow()
336 #endif // wxUSE_CLIPBOARD
345 WX_CLEAR_LIST(wxHtmlProcessorList
, *m_Processors
);
357 void wxHtmlWindow::SetRelatedFrame(wxFrame
* frame
, const wxString
& format
)
359 m_RelatedFrame
= frame
;
360 m_TitleFormat
= format
;
366 void wxHtmlWindow::SetRelatedStatusBar(int bar
)
368 m_RelatedStatusBar
= bar
;
370 #endif // wxUSE_STATUSBAR
374 void wxHtmlWindow::SetFonts(const wxString
& normal_face
, const wxString
& fixed_face
, const int *sizes
)
376 m_Parser
->SetFonts(normal_face
, fixed_face
, sizes
);
378 // re-layout the page after changing fonts:
379 DoSetPage(*(m_Parser
->GetSource()));
382 void wxHtmlWindow::SetStandardFonts(int size
,
383 const wxString
& normal_face
,
384 const wxString
& fixed_face
)
386 m_Parser
->SetStandardFonts(size
, normal_face
, fixed_face
);
388 // re-layout the page after changing fonts:
389 DoSetPage(*(m_Parser
->GetSource()));
392 bool wxHtmlWindow::SetPage(const wxString
& source
)
394 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
395 return DoSetPage(source
);
398 bool wxHtmlWindow::DoSetPage(const wxString
& source
)
400 wxString
newsrc(source
);
402 wxDELETE(m_selection
);
404 // we will soon delete all the cells, so clear pointers to them:
405 m_tmpSelFromCell
= NULL
;
407 // pass HTML through registered processors:
408 if (m_Processors
|| m_GlobalProcessors
)
410 wxHtmlProcessorList::compatibility_iterator nodeL
, nodeG
;
414 nodeL
= m_Processors
->GetFirst();
415 if ( m_GlobalProcessors
)
416 nodeG
= m_GlobalProcessors
->GetFirst();
418 // VS: there are two lists, global and local, both of them sorted by
419 // priority. Since we have to go through _both_ lists with
420 // decreasing priority, we "merge-sort" the lists on-line by
421 // processing that one of the two heads that has higher priority
422 // in every iteration
423 while (nodeL
|| nodeG
)
425 prL
= (nodeL
) ? nodeL
->GetData()->GetPriority() : -1;
426 prG
= (nodeG
) ? nodeG
->GetData()->GetPriority() : -1;
429 if (nodeL
->GetData()->IsEnabled())
430 newsrc
= nodeL
->GetData()->Process(newsrc
);
431 nodeL
= nodeL
->GetNext();
435 if (nodeG
->GetData()->IsEnabled())
436 newsrc
= nodeG
->GetData()->Process(newsrc
);
437 nodeG
= nodeG
->GetNext();
442 // ...and run the parser on it:
443 wxClientDC
*dc
= new wxClientDC(this);
444 dc
->SetMapMode(wxMM_TEXT
);
445 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
446 SetBackgroundImage(wxNullBitmap
);
454 m_Cell
= (wxHtmlContainerCell
*) m_Parser
->Parse(newsrc
);
456 m_Cell
->SetIndent(m_Borders
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
457 m_Cell
->SetAlignHor(wxHTML_ALIGN_CENTER
);
459 if (m_tmpCanDrawLocks
== 0)
464 bool wxHtmlWindow::AppendToPage(const wxString
& source
)
466 return DoSetPage(*(GetParser()->GetSource()) + source
);
469 bool wxHtmlWindow::LoadPage(const wxString
& location
)
471 wxBusyCursor busyCursor
;
475 bool needs_refresh
= false;
478 if (m_HistoryOn
&& (m_HistoryPos
!= -1))
480 // store scroll position into history item:
482 GetViewStart(&x
, &y
);
483 (*m_History
)[m_HistoryPos
].SetPos(y
);
486 if (location
[0] == wxT('#'))
489 wxString anch
= location
.Mid(1) /*1 to end*/;
491 rt_val
= ScrollToAnchor(anch
);
494 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&& location
.BeforeFirst(wxT('#')) == m_OpenedPage
)
496 wxString anch
= location
.AfterFirst(wxT('#'));
498 rt_val
= ScrollToAnchor(anch
);
501 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&&
502 (m_FS
->GetPath() + location
.BeforeFirst(wxT('#'))) == m_OpenedPage
)
504 wxString anch
= location
.AfterFirst(wxT('#'));
506 rt_val
= ScrollToAnchor(anch
);
512 needs_refresh
= true;
515 if (m_RelatedStatusBar
!= -1)
517 m_RelatedFrame
->SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
520 #endif // wxUSE_STATUSBAR
522 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location
);
524 // try to interpret 'location' as filename instead of URL:
527 wxFileName
fn(location
);
528 wxString location2
= wxFileSystem::FileNameToURL(fn
);
529 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location2
);
534 wxLogError(_("Unable to open requested HTML document: %s"), location
.c_str());
541 wxList::compatibility_iterator node
;
542 wxString src
= wxEmptyString
;
545 if (m_RelatedStatusBar
!= -1)
547 wxString msg
= _("Loading : ") + location
;
548 m_RelatedFrame
->SetStatusText(msg
, m_RelatedStatusBar
);
551 #endif // wxUSE_STATUSBAR
553 node
= m_Filters
.GetFirst();
556 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
559 src
= h
->ReadFile(*f
);
562 node
= node
->GetNext();
564 if (src
== wxEmptyString
)
566 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
567 src
= m_DefaultFilter
->ReadFile(*f
);
570 m_FS
->ChangePathTo(f
->GetLocation());
571 rt_val
= SetPage(src
);
572 m_OpenedPage
= f
->GetLocation();
573 if (f
->GetAnchor() != wxEmptyString
)
575 ScrollToAnchor(f
->GetAnchor());
581 if (m_RelatedStatusBar
!= -1)
582 m_RelatedFrame
->SetStatusText(_("Done"), m_RelatedStatusBar
);
583 #endif // wxUSE_STATUSBAR
587 if (m_HistoryOn
) // add this page to history there:
589 int c
= m_History
->GetCount() - (m_HistoryPos
+ 1);
591 if (m_HistoryPos
< 0 ||
592 (*m_History
)[m_HistoryPos
].GetPage() != m_OpenedPage
||
593 (*m_History
)[m_HistoryPos
].GetAnchor() != m_OpenedAnchor
)
596 for (int i
= 0; i
< c
; i
++)
597 m_History
->RemoveAt(m_HistoryPos
);
598 m_History
->Add(new wxHtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
602 if (m_OpenedPageTitle
== wxEmptyString
)
603 OnSetTitle(wxFileNameFromPath(m_OpenedPage
));
617 bool wxHtmlWindow::LoadFile(const wxFileName
& filename
)
619 wxString url
= wxFileSystem::FileNameToURL(filename
);
620 return LoadPage(url
);
624 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
626 const wxHtmlCell
*c
= m_Cell
->Find(wxHTML_COND_ISANCHOR
, &anchor
);
629 wxLogWarning(_("HTML anchor %s does not exist."), anchor
.c_str());
636 for (y
= 0; c
!= NULL
; c
= c
->GetParent()) y
+= c
->GetPosY();
637 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
638 m_OpenedAnchor
= anchor
;
644 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
649 tit
.Printf(m_TitleFormat
, title
.c_str());
650 m_RelatedFrame
->SetTitle(tit
);
652 m_OpenedPageTitle
= title
;
659 void wxHtmlWindow::CreateLayout()
661 int ClientWidth
, ClientHeight
;
665 if ( HasFlag(wxHW_SCROLLBAR_NEVER
) )
667 SetScrollbars(1, 1, 0, 0); // always off
668 GetClientSize(&ClientWidth
, &ClientHeight
);
669 m_Cell
->Layout(ClientWidth
);
671 else // !wxHW_SCROLLBAR_NEVER
673 GetClientSize(&ClientWidth
, &ClientHeight
);
674 m_Cell
->Layout(ClientWidth
);
675 if (ClientHeight
< m_Cell
->GetHeight() + GetCharHeight())
678 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
679 m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
,
680 (m_Cell
->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
681 /*cheat: top-level frag is always container*/);
683 else /* we fit into window, no need for scrollbars */
685 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
686 GetClientSize(&ClientWidth
, &ClientHeight
);
687 m_Cell
->Layout(ClientWidth
); // ...and relayout
694 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
699 wxString p_fff
, p_ffn
;
701 if (path
!= wxEmptyString
)
703 oldpath
= cfg
->GetPath();
707 m_Borders
= cfg
->Read(wxT("wxHtmlWindow/Borders"), m_Borders
);
708 p_fff
= cfg
->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
709 p_ffn
= cfg
->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
710 for (int i
= 0; i
< 7; i
++)
712 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
713 p_fontsizes
[i
] = cfg
->Read(tmp
, m_Parser
->m_FontsSizes
[i
]);
715 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
717 if (path
!= wxEmptyString
)
718 cfg
->SetPath(oldpath
);
723 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
728 if (path
!= wxEmptyString
)
730 oldpath
= cfg
->GetPath();
734 cfg
->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders
);
735 cfg
->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
736 cfg
->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
737 for (int i
= 0; i
< 7; i
++)
739 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
740 cfg
->Write(tmp
, (long) m_Parser
->m_FontsSizes
[i
]);
743 if (path
!= wxEmptyString
)
744 cfg
->SetPath(oldpath
);
749 bool wxHtmlWindow::HistoryBack()
753 if (m_HistoryPos
< 1) return false;
755 // store scroll position into history item:
757 GetViewStart(&x
, &y
);
758 (*m_History
)[m_HistoryPos
].SetPos(y
);
760 // go to previous position:
763 l
= (*m_History
)[m_HistoryPos
].GetPage();
764 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
767 if (a
== wxEmptyString
) LoadPage(l
);
768 else LoadPage(l
+ wxT("#") + a
);
771 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
776 bool wxHtmlWindow::HistoryCanBack()
778 if (m_HistoryPos
< 1) return false;
783 bool wxHtmlWindow::HistoryForward()
787 if (m_HistoryPos
== -1) return false;
788 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return false;
790 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
793 l
= (*m_History
)[m_HistoryPos
].GetPage();
794 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
797 if (a
== wxEmptyString
) LoadPage(l
);
798 else LoadPage(l
+ wxT("#") + a
);
801 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
806 bool wxHtmlWindow::HistoryCanForward()
808 if (m_HistoryPos
== -1) return false;
809 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return false;
814 void wxHtmlWindow::HistoryClear()
820 void wxHtmlWindow::AddProcessor(wxHtmlProcessor
*processor
)
824 m_Processors
= new wxHtmlProcessorList
;
826 wxHtmlProcessorList::compatibility_iterator node
;
828 for (node
= m_Processors
->GetFirst(); node
; node
= node
->GetNext())
830 if (processor
->GetPriority() > node
->GetData()->GetPriority())
832 m_Processors
->Insert(node
, processor
);
836 m_Processors
->Append(processor
);
839 /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor
*processor
)
841 if (!m_GlobalProcessors
)
843 m_GlobalProcessors
= new wxHtmlProcessorList
;
845 wxHtmlProcessorList::compatibility_iterator node
;
847 for (node
= m_GlobalProcessors
->GetFirst(); node
; node
= node
->GetNext())
849 if (processor
->GetPriority() > node
->GetData()->GetPriority())
851 m_GlobalProcessors
->Insert(node
, processor
);
855 m_GlobalProcessors
->Append(processor
);
860 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
862 m_Filters
.Append(filter
);
866 bool wxHtmlWindow::IsSelectionEnabled() const
869 return !HasFlag(wxHW_NO_SELECTION
);
877 wxString
wxHtmlWindow::DoSelectionToText(wxHtmlSelection
*sel
)
880 return wxEmptyString
;
884 const wxHtmlCell
*end
= sel
->GetToCell();
886 wxHtmlTerminalCellsInterator
i(sel
->GetFromCell(), end
);
889 text
<< i
->ConvertToText(sel
);
892 const wxHtmlCell
*prev
= *i
;
895 if ( prev
->GetParent() != i
->GetParent() )
897 text
<< i
->ConvertToText(*i
== end
? sel
: NULL
);
904 wxString
wxHtmlWindow::ToText()
909 sel
.Set(m_Cell
->GetFirstTerminal(), m_Cell
->GetLastTerminal());
910 return DoSelectionToText(&sel
);
913 return wxEmptyString
;
916 #endif // wxUSE_CLIPBOARD
918 bool wxHtmlWindow::CopySelection(ClipboardType t
)
923 #if defined(__UNIX__) && !defined(__WXMAC__)
924 wxTheClipboard
->UsePrimarySelection(t
== Primary
);
926 // Primary selection exists only under X11, so don't do anything under
927 // the other platforms when we try to access it
929 // TODO: this should be abstracted at wxClipboard level!
932 #endif // __UNIX__/!__UNIX__
934 if ( wxTheClipboard
->Open() )
936 const wxString
txt(SelectionToText());
937 wxTheClipboard
->SetData(new wxTextDataObject(txt
));
938 wxTheClipboard
->Close();
939 wxLogTrace(_T("wxhtmlselection"),
940 _("Copied to clipboard:\"%s\""), txt
.c_str());
947 #endif // wxUSE_CLIPBOARD
953 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
955 wxHtmlLinkEvent
event(GetId(), link
);
956 event
.SetEventObject(this);
957 if (!GetEventHandler()->ProcessEvent(event
))
959 // the default behaviour is to load the URL in this window
960 const wxMouseEvent
*e
= event
.GetLinkInfo().GetEvent();
961 if (e
== NULL
|| e
->LeftUp())
962 LoadPage(event
.GetLinkInfo().GetHref());
966 void wxHtmlWindow::OnEraseBackground(wxEraseEvent
& event
)
970 // don't even skip the event, if we don't have a bg bitmap we're going
971 // to overwrite background in OnPaint() below anyhow, so letting the
972 // default handling take place would only result in flicker, just set a
973 // flag to erase the background below
974 m_eraseBgInOnPaint
= true;
978 wxDC
& dc
= *event
.GetDC();
980 // if the image is not fully opaque, we have to erase the background before
981 // drawing it, however avoid doing it for opaque images as this would just
982 // result in extra flicker without any other effect as background is
983 // completely covered anyhow
984 if ( m_bmpBg
.GetMask() )
986 dc
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
990 const wxSize
sizeWin(GetClientSize());
991 const wxSize
sizeBmp(m_bmpBg
.GetWidth(), m_bmpBg
.GetHeight());
992 for ( wxCoord x
= 0; x
< sizeWin
.x
; x
+= sizeBmp
.x
)
994 for ( wxCoord y
= 0; y
< sizeWin
.y
; y
+= sizeBmp
.y
)
996 dc
.DrawBitmap(m_bmpBg
, x
, y
, true /* use mask */);
1001 void wxHtmlWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
1005 if (m_tmpCanDrawLocks
> 0 || m_Cell
== NULL
)
1009 GetViewStart(&x
, &y
);
1010 wxRect rect
= GetUpdateRegion().GetBox();
1011 wxSize sz
= GetSize();
1014 if ( !m_backBuffer
)
1015 m_backBuffer
= new wxBitmap(sz
.x
, sz
.y
);
1016 dcm
.SelectObject(*m_backBuffer
);
1018 if ( m_eraseBgInOnPaint
)
1020 dcm
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
1023 m_eraseBgInOnPaint
= false;
1025 else // someone has already erased the background, keep it
1027 // preserve the existing background, otherwise we'd erase anything the
1028 // user code had drawn in its EVT_ERASE_BACKGROUND handler when we do
1029 // the Blit back below
1030 dcm
.Blit(0, rect
.GetTop(),
1031 sz
.x
, rect
.GetBottom() - rect
.GetTop() + 1,
1037 dcm
.SetMapMode(wxMM_TEXT
);
1038 dcm
.SetBackgroundMode(wxTRANSPARENT
);
1040 wxHtmlRenderingInfo rinfo
;
1041 wxDefaultHtmlRenderingStyle rstyle
;
1042 rinfo
.SetSelection(m_selection
);
1043 rinfo
.SetStyle(&rstyle
);
1044 m_Cell
->Draw(dcm
, 0, 0,
1045 y
* wxHTML_SCROLL_STEP
+ rect
.GetTop(),
1046 y
* wxHTML_SCROLL_STEP
+ rect
.GetBottom(),
1049 //#define DEBUG_HTML_SELECTION
1050 #ifdef DEBUG_HTML_SELECTION
1053 wxGetMousePosition(&xc
, &yc
);
1054 ScreenToClient(&xc
, &yc
);
1055 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
1056 wxHtmlCell
*at
= m_Cell
->FindCellByPos(x
, y
);
1057 wxHtmlCell
*before
=
1058 m_Cell
->FindCellByPos(x
, y
, wxHTML_FIND_NEAREST_BEFORE
);
1060 m_Cell
->FindCellByPos(x
, y
, wxHTML_FIND_NEAREST_AFTER
);
1062 dcm
.SetBrush(*wxTRANSPARENT_BRUSH
);
1063 dcm
.SetPen(*wxBLACK_PEN
);
1065 dcm
.DrawRectangle(at
->GetAbsPos(),
1066 wxSize(at
->GetWidth(),at
->GetHeight()));
1067 dcm
.SetPen(*wxGREEN_PEN
);
1069 dcm
.DrawRectangle(before
->GetAbsPos().x
+1, before
->GetAbsPos().y
+1,
1070 before
->GetWidth()-2,before
->GetHeight()-2);
1071 dcm
.SetPen(*wxRED_PEN
);
1073 dcm
.DrawRectangle(after
->GetAbsPos().x
+2, after
->GetAbsPos().y
+2,
1074 after
->GetWidth()-4,after
->GetHeight()-4);
1078 dcm
.SetDeviceOrigin(0,0);
1079 dc
.Blit(0, rect
.GetTop(),
1080 sz
.x
, rect
.GetBottom() - rect
.GetTop() + 1,
1088 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
1090 wxDELETE(m_backBuffer
);
1092 wxScrolledWindow::OnSize(event
);
1095 // Recompute selection if necessary:
1098 m_selection
->Set(m_selection
->GetFromCell(),
1099 m_selection
->GetToCell());
1100 m_selection
->ClearPrivPos();
1107 void wxHtmlWindow::OnMouseMove(wxMouseEvent
& WXUNUSED(event
))
1109 wxHtmlWindowMouseHelper::HandleMouseMoved();
1112 void wxHtmlWindow::OnMouseDown(wxMouseEvent
& event
)
1115 if ( event
.LeftDown() && IsSelectionEnabled() )
1117 const long TRIPLECLICK_LEN
= 200; // 0.2 sec after doubleclick
1118 if ( wxGetLocalTimeMillis() - m_lastDoubleClick
<= TRIPLECLICK_LEN
)
1120 SelectLine(CalcUnscrolledPosition(event
.GetPosition()));
1122 (void) CopySelection();
1126 m_makingSelection
= true;
1130 wxDELETE(m_selection
);
1133 m_tmpSelFromPos
= CalcUnscrolledPosition(event
.GetPosition());
1134 m_tmpSelFromCell
= NULL
;
1139 #endif // wxUSE_CLIPBOARD
1141 // in any case, let the default handler set focus to this window
1145 void wxHtmlWindow::OnMouseUp(wxMouseEvent
& event
)
1148 if ( m_makingSelection
)
1151 m_makingSelection
= false;
1153 // if m_selection=NULL, the user didn't move the mouse far enough from
1154 // starting point and the mouse up event is part of a click, the user
1155 // is not selecting text:
1158 CopySelection(Primary
);
1160 // we don't want mouse up event that ended selecting to be
1161 // handled as mouse click and e.g. follow hyperlink:
1165 #endif // wxUSE_CLIPBOARD
1167 wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
1168 wxHtmlWindowMouseHelper::HandleMouseClick(m_Cell
, pos
, event
);
1172 void wxHtmlWindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
1174 if ( !m_makingSelection
)
1177 // discard the selecting operation
1178 m_makingSelection
= false;
1179 wxDELETE(m_selection
);
1180 m_tmpSelFromCell
= NULL
;
1183 #endif // wxUSE_CLIPBOARD
1186 void wxHtmlWindow::OnInternalIdle()
1188 wxWindow::OnInternalIdle();
1190 if (m_Cell
!= NULL
&& DidMouseMove())
1192 #ifdef DEBUG_HTML_SELECTION
1196 wxGetMousePosition(&xc
, &yc
);
1197 ScreenToClient(&xc
, &yc
);
1198 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
1200 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(x
, y
);
1202 // handle selection update:
1203 if ( m_makingSelection
)
1205 if ( !m_tmpSelFromCell
)
1206 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1207 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
);
1209 // NB: a trick - we adjust selFromPos to be upper left or bottom
1210 // right corner of the first cell of the selection depending
1211 // on whether the mouse is moving to the right or to the left.
1212 // This gives us more "natural" behaviour when selecting
1213 // a line (specifically, first cell of the next line is not
1214 // included if you drag selection from left to right over
1217 if ( !m_tmpSelFromCell
)
1219 dirFromPos
= m_tmpSelFromPos
;
1223 dirFromPos
= m_tmpSelFromCell
->GetAbsPos();
1224 if ( x
< m_tmpSelFromPos
.x
)
1226 dirFromPos
.x
+= m_tmpSelFromCell
->GetWidth();
1227 dirFromPos
.y
+= m_tmpSelFromCell
->GetHeight();
1230 bool goingDown
= dirFromPos
.y
< y
||
1231 (dirFromPos
.y
== y
&& dirFromPos
.x
< x
);
1233 // determine selection span:
1234 if ( /*still*/ !m_tmpSelFromCell
)
1238 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1239 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1240 wxHTML_FIND_NEAREST_AFTER
);
1241 if (!m_tmpSelFromCell
)
1242 m_tmpSelFromCell
= m_Cell
->GetFirstTerminal();
1246 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1247 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1248 wxHTML_FIND_NEAREST_BEFORE
);
1249 if (!m_tmpSelFromCell
)
1250 m_tmpSelFromCell
= m_Cell
->GetLastTerminal();
1254 wxHtmlCell
*selcell
= cell
;
1259 selcell
= m_Cell
->FindCellByPos(x
, y
,
1260 wxHTML_FIND_NEAREST_BEFORE
);
1262 selcell
= m_Cell
->GetLastTerminal();
1266 selcell
= m_Cell
->FindCellByPos(x
, y
,
1267 wxHTML_FIND_NEAREST_AFTER
);
1269 selcell
= m_Cell
->GetFirstTerminal();
1273 // NB: it may *rarely* happen that the code above didn't find one
1274 // of the cells, e.g. if wxHtmlWindow doesn't contain any
1276 if ( selcell
&& m_tmpSelFromCell
)
1280 // start selecting only if mouse movement was big enough
1281 // (otherwise it was meant as mouse click, not selection):
1282 const int PRECISION
= 2;
1283 wxPoint diff
= m_tmpSelFromPos
- wxPoint(x
,y
);
1284 if (abs(diff
.x
) > PRECISION
|| abs(diff
.y
) > PRECISION
)
1286 m_selection
= new wxHtmlSelection();
1291 if ( m_tmpSelFromCell
->IsBefore(selcell
) )
1293 m_selection
->Set(m_tmpSelFromPos
, m_tmpSelFromCell
,
1294 wxPoint(x
,y
), selcell
);
1298 m_selection
->Set(wxPoint(x
,y
), selcell
,
1299 m_tmpSelFromPos
, m_tmpSelFromCell
);
1301 m_selection
->ClearPrivPos();
1307 // handle cursor and status bar text changes:
1309 // NB: because we're passing in 'cell' and not 'm_Cell' (so that the
1310 // leaf cell lookup isn't done twice), we need to adjust the
1311 // position for the new root:
1312 wxPoint
posInCell(x
, y
);
1314 posInCell
-= cell
->GetAbsPos();
1315 wxHtmlWindowMouseHelper::HandleIdle(cell
, posInCell
);
1320 void wxHtmlWindow::StopAutoScrolling()
1322 if ( m_timerAutoScroll
)
1324 wxDELETE(m_timerAutoScroll
);
1328 void wxHtmlWindow::OnMouseEnter(wxMouseEvent
& event
)
1330 StopAutoScrolling();
1334 void wxHtmlWindow::OnMouseLeave(wxMouseEvent
& event
)
1336 // don't prevent the usual processing of the event from taking place
1339 // when a captured mouse leave a scrolled window we start generate
1340 // scrolling events to allow, for example, extending selection beyond the
1341 // visible area in some controls
1342 if ( wxWindow::GetCapture() == this )
1344 // where is the mouse leaving?
1346 wxPoint pt
= event
.GetPosition();
1349 orient
= wxHORIZONTAL
;
1352 else if ( pt
.y
< 0 )
1354 orient
= wxVERTICAL
;
1357 else // we're lower or to the right of the window
1359 wxSize size
= GetClientSize();
1360 if ( pt
.x
> size
.x
)
1362 orient
= wxHORIZONTAL
;
1363 pos
= GetVirtualSize().x
/ wxHTML_SCROLL_STEP
;
1365 else if ( pt
.y
> size
.y
)
1367 orient
= wxVERTICAL
;
1368 pos
= GetVirtualSize().y
/ wxHTML_SCROLL_STEP
;
1370 else // this should be impossible
1372 // but seems to happen sometimes under wxMSW - maybe it's a bug
1373 // there but for now just ignore it
1375 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1381 // only start the auto scroll timer if the window can be scrolled in
1383 if ( !HasScrollbar(orient
) )
1386 delete m_timerAutoScroll
;
1387 m_timerAutoScroll
= new wxHtmlWinAutoScrollTimer
1390 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1391 : wxEVT_SCROLLWIN_LINEDOWN
,
1395 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1399 void wxHtmlWindow::OnKeyUp(wxKeyEvent
& event
)
1401 if ( IsSelectionEnabled() &&
1402 (event
.GetKeyCode() == 'C' && event
.CmdDown()) )
1404 wxClipboardTextEvent
evt(wxEVT_COMMAND_TEXT_COPY
, GetId());
1406 evt
.SetEventObject(this);
1408 GetEventHandler()->ProcessEvent(evt
);
1412 void wxHtmlWindow::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1414 (void) CopySelection();
1417 void wxHtmlWindow::OnClipboardEvent(wxClipboardTextEvent
& WXUNUSED(event
))
1419 (void) CopySelection();
1422 void wxHtmlWindow::OnDoubleClick(wxMouseEvent
& event
)
1424 // select word under cursor:
1425 if ( IsSelectionEnabled() )
1427 SelectWord(CalcUnscrolledPosition(event
.GetPosition()));
1429 (void) CopySelection(Primary
);
1431 m_lastDoubleClick
= wxGetLocalTimeMillis();
1437 void wxHtmlWindow::SelectWord(const wxPoint
& pos
)
1441 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1445 m_selection
= new wxHtmlSelection();
1446 m_selection
->Set(cell
, cell
);
1447 RefreshRect(wxRect(CalcScrolledPosition(cell
->GetAbsPos()),
1448 wxSize(cell
->GetWidth(), cell
->GetHeight())));
1453 void wxHtmlWindow::SelectLine(const wxPoint
& pos
)
1457 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1460 // We use following heuristic to find a "line": let the line be all
1461 // cells in same container as the cell under mouse cursor that are
1462 // neither completely above nor completely bellow the clicked cell
1463 // (i.e. are likely to be words positioned on same line of text).
1465 int y1
= cell
->GetAbsPos().y
;
1466 int y2
= y1
+ cell
->GetHeight();
1468 const wxHtmlCell
*c
;
1469 const wxHtmlCell
*before
= NULL
;
1470 const wxHtmlCell
*after
= NULL
;
1472 // find last cell of line:
1473 for ( c
= cell
->GetNext(); c
; c
= c
->GetNext())
1475 y
= c
->GetAbsPos().y
;
1476 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1484 // find first cell of line:
1485 for ( c
= cell
->GetParent()->GetFirstChild();
1486 c
&& c
!= cell
; c
= c
->GetNext())
1488 y
= c
->GetAbsPos().y
;
1489 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1501 m_selection
= new wxHtmlSelection();
1502 m_selection
->Set(before
, after
);
1509 void wxHtmlWindow::SelectAll()
1514 m_selection
= new wxHtmlSelection();
1515 m_selection
->Set(m_Cell
->GetFirstTerminal(), m_Cell
->GetLastTerminal());
1520 #endif // wxUSE_CLIPBOARD
1524 IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor
,wxObject
)
1526 #if wxUSE_EXTENDED_RTTI
1527 IMPLEMENT_DYNAMIC_CLASS_XTI(wxHtmlWindow
, wxScrolledWindow
,"wx/html/htmlwin.h")
1529 wxBEGIN_PROPERTIES_TABLE(wxHtmlWindow
)
1532 style , wxHW_SCROLLBAR_AUTO
1533 borders , (dimension)
1537 wxEND_PROPERTIES_TABLE()
1539 wxBEGIN_HANDLERS_TABLE(wxHtmlWindow
)
1540 wxEND_HANDLERS_TABLE()
1542 wxCONSTRUCTOR_5( wxHtmlWindow
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
1544 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
1547 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
1548 EVT_SIZE(wxHtmlWindow::OnSize
)
1549 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseDown
)
1550 EVT_LEFT_UP(wxHtmlWindow::OnMouseUp
)
1551 EVT_RIGHT_UP(wxHtmlWindow::OnMouseUp
)
1552 EVT_MOTION(wxHtmlWindow::OnMouseMove
)
1553 EVT_ERASE_BACKGROUND(wxHtmlWindow::OnEraseBackground
)
1554 EVT_PAINT(wxHtmlWindow::OnPaint
)
1556 EVT_LEFT_DCLICK(wxHtmlWindow::OnDoubleClick
)
1557 EVT_ENTER_WINDOW(wxHtmlWindow::OnMouseEnter
)
1558 EVT_LEAVE_WINDOW(wxHtmlWindow::OnMouseLeave
)
1559 EVT_MOUSE_CAPTURE_LOST(wxHtmlWindow::OnMouseCaptureLost
)
1560 EVT_KEY_UP(wxHtmlWindow::OnKeyUp
)
1561 EVT_MENU(wxID_COPY
, wxHtmlWindow::OnCopy
)
1562 EVT_TEXT_COPY(wxID_ANY
, wxHtmlWindow::OnClipboardEvent
)
1563 #endif // wxUSE_CLIPBOARD
1566 //-----------------------------------------------------------------------------
1567 // wxHtmlWindowInterface implementation in wxHtmlWindow
1568 //-----------------------------------------------------------------------------
1570 void wxHtmlWindow::SetHTMLWindowTitle(const wxString
& title
)
1575 void wxHtmlWindow::OnHTMLLinkClicked(const wxHtmlLinkInfo
& link
)
1577 OnLinkClicked(link
);
1580 wxHtmlOpeningStatus
wxHtmlWindow::OnHTMLOpeningURL(wxHtmlURLType type
,
1581 const wxString
& url
,
1582 wxString
*redirect
) const
1584 return OnOpeningURL(type
, url
, redirect
);
1587 wxPoint
wxHtmlWindow::HTMLCoordsToWindow(wxHtmlCell
*WXUNUSED(cell
),
1588 const wxPoint
& pos
) const
1590 return CalcScrolledPosition(pos
);
1593 wxWindow
* wxHtmlWindow::GetHTMLWindow()
1598 wxColour
wxHtmlWindow::GetHTMLBackgroundColour() const
1600 return GetBackgroundColour();
1603 void wxHtmlWindow::SetHTMLBackgroundColour(const wxColour
& clr
)
1605 SetBackgroundColour(clr
);
1608 void wxHtmlWindow::SetHTMLBackgroundImage(const wxBitmap
& bmpBg
)
1610 SetBackgroundImage(bmpBg
);
1613 void wxHtmlWindow::SetHTMLStatusText(const wxString
& text
)
1616 if (m_RelatedStatusBar
!= -1)
1617 m_RelatedFrame
->SetStatusText(text
, m_RelatedStatusBar
);
1620 #endif // wxUSE_STATUSBAR
1624 wxCursor
wxHtmlWindow::GetDefaultHTMLCursor(HTMLCursor type
)
1628 case HTMLCursor_Link
:
1629 if ( !ms_cursorLink
)
1630 ms_cursorLink
= new wxCursor(wxCURSOR_HAND
);
1631 return *ms_cursorLink
;
1633 case HTMLCursor_Text
:
1634 if ( !ms_cursorText
)
1635 ms_cursorText
= new wxCursor(wxCURSOR_IBEAM
);
1636 return *ms_cursorText
;
1638 case HTMLCursor_Default
:
1640 return *wxSTANDARD_CURSOR
;
1644 wxCursor
wxHtmlWindow::GetHTMLCursor(HTMLCursor type
) const
1646 return GetDefaultHTMLCursor(type
);
1650 //-----------------------------------------------------------------------------
1652 //-----------------------------------------------------------------------------
1654 // A module to allow initialization/cleanup
1655 // without calling these functions from app.cpp or from
1656 // the user's application.
1658 class wxHtmlWinModule
: public wxModule
1660 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
1662 wxHtmlWinModule() : wxModule() {}
1663 bool OnInit() { return true; }
1664 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
1667 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
1670 // This hack forces the linker to always link in m_* files
1671 // (wxHTML doesn't work without handlers from these files)
1672 #include "wx/html/forcelnk.h"
1673 FORCE_WXHTML_MODULES()
1675 #endif // wxUSE_HTML